* config/tc-xtensa.c (xg_symbolic_immeds_fit): Do not relax calls to ...
[deliverable/binutils-gdb.git] / gas / config / tc-xtensa.c
1 /* tc-xtensa.c -- Assemble Xtensa instructions.
2 Copyright 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include <limits.h>
22 #include "as.h"
23 #include "sb.h"
24 #include "safe-ctype.h"
25 #include "tc-xtensa.h"
26 #include "subsegs.h"
27 #include "xtensa-relax.h"
28 #include "dwarf2dbg.h"
29 #include "xtensa-istack.h"
30 #include "struc-symbol.h"
31 #include "xtensa-config.h"
32
33 /* Provide default values for new configuration settings. */
34 #ifndef XSHAL_ABI
35 #define XSHAL_ABI 0
36 #endif
37
38 #ifndef uint32
39 #define uint32 unsigned int
40 #endif
41 #ifndef int32
42 #define int32 signed int
43 #endif
44
45 /* Notes:
46
47 Naming conventions (used somewhat inconsistently):
48 The xtensa_ functions are exported
49 The xg_ functions are internal
50
51 We also have a couple of different extensibility mechanisms.
52 1) The idiom replacement:
53 This is used when a line is first parsed to
54 replace an instruction pattern with another instruction
55 It is currently limited to replacements of instructions
56 with constant operands.
57 2) The xtensa-relax.c mechanism that has stronger instruction
58 replacement patterns. When an instruction's immediate field
59 does not fit the next instruction sequence is attempted.
60 In addition, "narrow" opcodes are supported this way. */
61
62
63 /* Define characters with special meanings to GAS. */
64 const char comment_chars[] = "#";
65 const char line_comment_chars[] = "#";
66 const char line_separator_chars[] = ";";
67 const char EXP_CHARS[] = "eE";
68 const char FLT_CHARS[] = "rRsSfFdDxXpP";
69
70
71 /* Flags to indicate whether the hardware supports the density and
72 absolute literals options. */
73
74 bfd_boolean density_supported = XCHAL_HAVE_DENSITY;
75 bfd_boolean absolute_literals_supported = XSHAL_USE_ABSOLUTE_LITERALS;
76
77 /* Maximum width we would pad an unreachable frag to get alignment. */
78 #define UNREACHABLE_MAX_WIDTH 8
79
80 static vliw_insn cur_vinsn;
81
82 unsigned xtensa_fetch_width = XCHAL_INST_FETCH_WIDTH;
83
84 static enum debug_info_type xt_saved_debug_type = DEBUG_NONE;
85
86 /* Some functions are only valid in the front end. This variable
87 allows us to assert that we haven't crossed over into the
88 back end. */
89 static bfd_boolean past_xtensa_end = FALSE;
90
91 /* Flags for properties of the last instruction in a segment. */
92 #define FLAG_IS_A0_WRITER 0x1
93 #define FLAG_IS_BAD_LOOPEND 0x2
94
95
96 /* We define a special segment names ".literal" to place literals
97 into. The .fini and .init sections are special because they
98 contain code that is moved together by the linker. We give them
99 their own special .fini.literal and .init.literal sections. */
100
101 #define LITERAL_SECTION_NAME xtensa_section_rename (".literal")
102 #define LIT4_SECTION_NAME xtensa_section_rename (".lit4")
103 #define INIT_SECTION_NAME xtensa_section_rename (".init")
104 #define FINI_SECTION_NAME xtensa_section_rename (".fini")
105
106
107 /* This type is used for the directive_stack to keep track of the
108 state of the literal collection pools. If lit_prefix is set, it is
109 used to determine the literal section names; otherwise, the literal
110 sections are determined based on the current text section. The
111 lit_seg and lit4_seg fields cache these literal sections, with the
112 current_text_seg field used a tag to indicate whether the cached
113 values are valid. */
114
115 typedef struct lit_state_struct
116 {
117 char *lit_prefix;
118 segT current_text_seg;
119 segT lit_seg;
120 segT lit4_seg;
121 } lit_state;
122
123 static lit_state default_lit_sections;
124
125
126 /* We keep a list of literal segments. The seg_list type is the node
127 for this list. The literal_head pointer is the head of the list,
128 with the literal_head_h dummy node at the start. */
129
130 typedef struct seg_list_struct
131 {
132 struct seg_list_struct *next;
133 segT seg;
134 } seg_list;
135
136 static seg_list literal_head_h;
137 static seg_list *literal_head = &literal_head_h;
138
139
140 /* Lists of symbols. We keep a list of symbols that label the current
141 instruction, so that we can adjust the symbols when inserting alignment
142 for various instructions. We also keep a list of all the symbols on
143 literals, so that we can fix up those symbols when the literals are
144 later moved into the text sections. */
145
146 typedef struct sym_list_struct
147 {
148 struct sym_list_struct *next;
149 symbolS *sym;
150 } sym_list;
151
152 static sym_list *insn_labels = NULL;
153 static sym_list *free_insn_labels = NULL;
154 static sym_list *saved_insn_labels = NULL;
155
156 static sym_list *literal_syms;
157
158
159 /* Flags to determine whether to prefer const16 or l32r
160 if both options are available. */
161 int prefer_const16 = 0;
162 int prefer_l32r = 0;
163
164 /* Global flag to indicate when we are emitting literals. */
165 int generating_literals = 0;
166
167 /* The following PROPERTY table definitions are copied from
168 <elf/xtensa.h> and must be kept in sync with the code there. */
169
170 /* Flags in the property tables to specify whether blocks of memory
171 are literals, instructions, data, or unreachable. For
172 instructions, blocks that begin loop targets and branch targets are
173 designated. Blocks that do not allow density, instruction
174 reordering or transformation are also specified. Finally, for
175 branch targets, branch target alignment priority is included.
176 Alignment of the next block is specified in the current block
177 and the size of the current block does not include any fill required
178 to align to the next block. */
179
180 #define XTENSA_PROP_LITERAL 0x00000001
181 #define XTENSA_PROP_INSN 0x00000002
182 #define XTENSA_PROP_DATA 0x00000004
183 #define XTENSA_PROP_UNREACHABLE 0x00000008
184 /* Instruction only properties at beginning of code. */
185 #define XTENSA_PROP_INSN_LOOP_TARGET 0x00000010
186 #define XTENSA_PROP_INSN_BRANCH_TARGET 0x00000020
187 /* Instruction only properties about code. */
188 #define XTENSA_PROP_INSN_NO_DENSITY 0x00000040
189 #define XTENSA_PROP_INSN_NO_REORDER 0x00000080
190 /* Historically, NO_TRANSFORM was a property of instructions,
191 but it should apply to literals under certain circumstances. */
192 #define XTENSA_PROP_NO_TRANSFORM 0x00000100
193
194 /* Branch target alignment information. This transmits information
195 to the linker optimization about the priority of aligning a
196 particular block for branch target alignment: None, low priority,
197 high priority, or required. These only need to be checked in
198 instruction blocks marked as XTENSA_PROP_INSN_BRANCH_TARGET.
199 Common usage is
200
201 switch (GET_XTENSA_PROP_BT_ALIGN (flags))
202 case XTENSA_PROP_BT_ALIGN_NONE:
203 case XTENSA_PROP_BT_ALIGN_LOW:
204 case XTENSA_PROP_BT_ALIGN_HIGH:
205 case XTENSA_PROP_BT_ALIGN_REQUIRE:
206 */
207 #define XTENSA_PROP_BT_ALIGN_MASK 0x00000600
208
209 /* No branch target alignment. */
210 #define XTENSA_PROP_BT_ALIGN_NONE 0x0
211 /* Low priority branch target alignment. */
212 #define XTENSA_PROP_BT_ALIGN_LOW 0x1
213 /* High priority branch target alignment. */
214 #define XTENSA_PROP_BT_ALIGN_HIGH 0x2
215 /* Required branch target alignment. */
216 #define XTENSA_PROP_BT_ALIGN_REQUIRE 0x3
217
218 #define GET_XTENSA_PROP_BT_ALIGN(flag) \
219 (((unsigned) ((flag) & (XTENSA_PROP_BT_ALIGN_MASK))) >> 9)
220 #define SET_XTENSA_PROP_BT_ALIGN(flag, align) \
221 (((flag) & (~XTENSA_PROP_BT_ALIGN_MASK)) | \
222 (((align) << 9) & XTENSA_PROP_BT_ALIGN_MASK))
223
224
225 /* Alignment is specified in the block BEFORE the one that needs
226 alignment. Up to 5 bits. Use GET_XTENSA_PROP_ALIGNMENT(flags) to
227 get the required alignment specified as a power of 2. Use
228 SET_XTENSA_PROP_ALIGNMENT(flags, pow2) to set the required
229 alignment. Be careful of side effects since the SET will evaluate
230 flags twice. Also, note that the SIZE of a block in the property
231 table does not include the alignment size, so the alignment fill
232 must be calculated to determine if two blocks are contiguous.
233 TEXT_ALIGN is not currently implemented but is a placeholder for a
234 possible future implementation. */
235
236 #define XTENSA_PROP_ALIGN 0x00000800
237
238 #define XTENSA_PROP_ALIGNMENT_MASK 0x0001f000
239
240 #define GET_XTENSA_PROP_ALIGNMENT(flag) \
241 (((unsigned) ((flag) & (XTENSA_PROP_ALIGNMENT_MASK))) >> 12)
242 #define SET_XTENSA_PROP_ALIGNMENT(flag, align) \
243 (((flag) & (~XTENSA_PROP_ALIGNMENT_MASK)) | \
244 (((align) << 12) & XTENSA_PROP_ALIGNMENT_MASK))
245
246 #define XTENSA_PROP_INSN_ABSLIT 0x00020000
247
248
249 /* Structure for saving instruction and alignment per-fragment data
250 that will be written to the object file. This structure is
251 equivalent to the actual data that will be written out to the file
252 but is easier to use. We provide a conversion to file flags
253 in frag_flags_to_number. */
254
255 typedef struct frag_flags_struct frag_flags;
256
257 struct frag_flags_struct
258 {
259 /* is_literal should only be used after xtensa_move_literals.
260 If you need to check if you are generating a literal fragment,
261 then use the generating_literals global. */
262
263 unsigned is_literal : 1;
264 unsigned is_insn : 1;
265 unsigned is_data : 1;
266 unsigned is_unreachable : 1;
267
268 /* is_specific_opcode implies no_transform. */
269 unsigned is_no_transform : 1;
270
271 struct
272 {
273 unsigned is_loop_target : 1;
274 unsigned is_branch_target : 1; /* Branch targets have a priority. */
275 unsigned bt_align_priority : 2;
276
277 unsigned is_no_density : 1;
278 /* no_longcalls flag does not need to be placed in the object file. */
279
280 unsigned is_no_reorder : 1;
281
282 /* Uses absolute literal addressing for l32r. */
283 unsigned is_abslit : 1;
284 } insn;
285 unsigned is_align : 1;
286 unsigned alignment : 5;
287 };
288
289
290 /* Structure for saving information about a block of property data
291 for frags that have the same flags. */
292 struct xtensa_block_info_struct
293 {
294 segT sec;
295 bfd_vma offset;
296 size_t size;
297 frag_flags flags;
298 struct xtensa_block_info_struct *next;
299 };
300
301
302 /* Structure for saving the current state before emitting literals. */
303 typedef struct emit_state_struct
304 {
305 const char *name;
306 segT now_seg;
307 subsegT now_subseg;
308 int generating_literals;
309 } emit_state;
310
311
312 /* Opcode placement information */
313
314 typedef unsigned long long bitfield;
315 #define bit_is_set(bit, bf) ((bf) & (0x01ll << (bit)))
316 #define set_bit(bit, bf) ((bf) |= (0x01ll << (bit)))
317 #define clear_bit(bit, bf) ((bf) &= ~(0x01ll << (bit)))
318
319 #define MAX_FORMATS 32
320
321 typedef struct op_placement_info_struct
322 {
323 int num_formats;
324 /* A number describing how restrictive the issue is for this
325 opcode. For example, an opcode that fits lots of different
326 formats has a high freedom, as does an opcode that fits
327 only one format but many slots in that format. The most
328 restrictive is the opcode that fits only one slot in one
329 format. */
330 int issuef;
331 xtensa_format narrowest;
332 char narrowest_size;
333 char narrowest_slot;
334
335 /* formats is a bitfield with the Nth bit set
336 if the opcode fits in the Nth xtensa_format. */
337 bitfield formats;
338
339 /* slots[N]'s Mth bit is set if the op fits in the
340 Mth slot of the Nth xtensa_format. */
341 bitfield slots[MAX_FORMATS];
342
343 /* A count of the number of slots in a given format
344 an op can fit (i.e., the bitcount of the slot field above). */
345 char slots_in_format[MAX_FORMATS];
346
347 } op_placement_info, *op_placement_info_table;
348
349 op_placement_info_table op_placement_table;
350
351
352 /* Extra expression types. */
353
354 #define O_pltrel O_md1 /* like O_symbol but use a PLT reloc */
355 #define O_hi16 O_md2 /* use high 16 bits of symbolic value */
356 #define O_lo16 O_md3 /* use low 16 bits of symbolic value */
357 #define O_pcrel O_md4 /* value is a PC-relative offset */
358
359 struct suffix_reloc_map
360 {
361 char *suffix;
362 int length;
363 bfd_reloc_code_real_type reloc;
364 unsigned char operator;
365 };
366
367 #define SUFFIX_MAP(str, reloc, op) { str, sizeof (str) - 1, reloc, op }
368
369 static struct suffix_reloc_map suffix_relocs[] =
370 {
371 SUFFIX_MAP ("l", BFD_RELOC_LO16, O_lo16),
372 SUFFIX_MAP ("h", BFD_RELOC_HI16, O_hi16),
373 SUFFIX_MAP ("plt", BFD_RELOC_XTENSA_PLT, O_pltrel),
374 SUFFIX_MAP ("pcrel", BFD_RELOC_32_PCREL, O_pcrel),
375 { (char *) 0, 0, BFD_RELOC_UNUSED, 0 }
376 };
377
378
379 /* Directives. */
380
381 typedef enum
382 {
383 directive_none = 0,
384 directive_literal,
385 directive_density,
386 directive_transform,
387 directive_freeregs,
388 directive_longcalls,
389 directive_literal_prefix,
390 directive_schedule,
391 directive_absolute_literals,
392 directive_last_directive
393 } directiveE;
394
395 typedef struct
396 {
397 const char *name;
398 bfd_boolean can_be_negated;
399 } directive_infoS;
400
401 const directive_infoS directive_info[] =
402 {
403 { "none", FALSE },
404 { "literal", FALSE },
405 { "density", TRUE },
406 { "transform", TRUE },
407 { "freeregs", FALSE },
408 { "longcalls", TRUE },
409 { "literal_prefix", FALSE },
410 { "schedule", TRUE },
411 { "absolute-literals", TRUE }
412 };
413
414 bfd_boolean directive_state[] =
415 {
416 FALSE, /* none */
417 FALSE, /* literal */
418 #if !XCHAL_HAVE_DENSITY
419 FALSE, /* density */
420 #else
421 TRUE, /* density */
422 #endif
423 TRUE, /* transform */
424 FALSE, /* freeregs */
425 FALSE, /* longcalls */
426 FALSE, /* literal_prefix */
427 FALSE, /* schedule */
428 #if XSHAL_USE_ABSOLUTE_LITERALS
429 TRUE /* absolute_literals */
430 #else
431 FALSE /* absolute_literals */
432 #endif
433 };
434
435
436 /* Directive functions. */
437
438 static void xtensa_begin_directive (int);
439 static void xtensa_end_directive (int);
440 static void xtensa_literal_prefix (void);
441 static void xtensa_literal_position (int);
442 static void xtensa_literal_pseudo (int);
443 static void xtensa_frequency_pseudo (int);
444 static void xtensa_elf_cons (int);
445
446 /* Parsing and Idiom Translation. */
447
448 static bfd_reloc_code_real_type xtensa_elf_suffix (char **, expressionS *);
449
450 /* Various Other Internal Functions. */
451
452 extern bfd_boolean xg_is_single_relaxable_insn (TInsn *, TInsn *, bfd_boolean);
453 static bfd_boolean xg_build_to_insn (TInsn *, TInsn *, BuildInstr *);
454 static void xtensa_mark_literal_pool_location (void);
455 static addressT get_expanded_loop_offset (xtensa_opcode);
456 static fragS *get_literal_pool_location (segT);
457 static void set_literal_pool_location (segT, fragS *);
458 static void xtensa_set_frag_assembly_state (fragS *);
459 static void finish_vinsn (vliw_insn *);
460 static bfd_boolean emit_single_op (TInsn *);
461 static int total_frag_text_expansion (fragS *);
462
463 /* Alignment Functions. */
464
465 static int get_text_align_power (unsigned);
466 static int get_text_align_max_fill_size (int, bfd_boolean, bfd_boolean);
467 static int branch_align_power (segT);
468
469 /* Helpers for xtensa_relax_frag(). */
470
471 static long relax_frag_add_nop (fragS *);
472
473 /* Accessors for additional per-subsegment information. */
474
475 static unsigned get_last_insn_flags (segT, subsegT);
476 static void set_last_insn_flags (segT, subsegT, unsigned, bfd_boolean);
477 static float get_subseg_total_freq (segT, subsegT);
478 static float get_subseg_target_freq (segT, subsegT);
479 static void set_subseg_freq (segT, subsegT, float, float);
480
481 /* Segment list functions. */
482
483 static void xtensa_move_literals (void);
484 static void xtensa_reorder_segments (void);
485 static void xtensa_switch_to_literal_fragment (emit_state *);
486 static void xtensa_switch_to_non_abs_literal_fragment (emit_state *);
487 static void xtensa_switch_section_emit_state (emit_state *, segT, subsegT);
488 static void xtensa_restore_emit_state (emit_state *);
489 static segT cache_literal_section (bfd_boolean);
490
491 /* Import from elf32-xtensa.c in BFD library. */
492
493 extern asection *xtensa_get_property_section (asection *, const char *);
494
495 /* op_placement_info functions. */
496
497 static void init_op_placement_info_table (void);
498 extern bfd_boolean opcode_fits_format_slot (xtensa_opcode, xtensa_format, int);
499 static int xg_get_single_size (xtensa_opcode);
500 static xtensa_format xg_get_single_format (xtensa_opcode);
501 static int xg_get_single_slot (xtensa_opcode);
502
503 /* TInsn and IStack functions. */
504
505 static bfd_boolean tinsn_has_symbolic_operands (const TInsn *);
506 static bfd_boolean tinsn_has_invalid_symbolic_operands (const TInsn *);
507 static bfd_boolean tinsn_has_complex_operands (const TInsn *);
508 static bfd_boolean tinsn_to_insnbuf (TInsn *, xtensa_insnbuf);
509 static bfd_boolean tinsn_check_arguments (const TInsn *);
510 static void tinsn_from_chars (TInsn *, char *, int);
511 static void tinsn_immed_from_frag (TInsn *, fragS *, int);
512 static int get_num_stack_text_bytes (IStack *);
513 static int get_num_stack_literal_bytes (IStack *);
514
515 /* vliw_insn functions. */
516
517 static void xg_init_vinsn (vliw_insn *);
518 static void xg_clear_vinsn (vliw_insn *);
519 static bfd_boolean vinsn_has_specific_opcodes (vliw_insn *);
520 static void xg_free_vinsn (vliw_insn *);
521 static bfd_boolean vinsn_to_insnbuf
522 (vliw_insn *, char *, fragS *, bfd_boolean);
523 static void vinsn_from_chars (vliw_insn *, char *);
524
525 /* Expression Utilities. */
526
527 bfd_boolean expr_is_const (const expressionS *);
528 offsetT get_expr_const (const expressionS *);
529 void set_expr_const (expressionS *, offsetT);
530 bfd_boolean expr_is_register (const expressionS *);
531 offsetT get_expr_register (const expressionS *);
532 void set_expr_symbol_offset (expressionS *, symbolS *, offsetT);
533 bfd_boolean expr_is_equal (expressionS *, expressionS *);
534 static void copy_expr (expressionS *, const expressionS *);
535
536 /* Section renaming. */
537
538 static void build_section_rename (const char *);
539
540
541 /* ISA imported from bfd. */
542 extern xtensa_isa xtensa_default_isa;
543
544 extern int target_big_endian;
545
546 static xtensa_opcode xtensa_addi_opcode;
547 static xtensa_opcode xtensa_addmi_opcode;
548 static xtensa_opcode xtensa_call0_opcode;
549 static xtensa_opcode xtensa_call4_opcode;
550 static xtensa_opcode xtensa_call8_opcode;
551 static xtensa_opcode xtensa_call12_opcode;
552 static xtensa_opcode xtensa_callx0_opcode;
553 static xtensa_opcode xtensa_callx4_opcode;
554 static xtensa_opcode xtensa_callx8_opcode;
555 static xtensa_opcode xtensa_callx12_opcode;
556 static xtensa_opcode xtensa_const16_opcode;
557 static xtensa_opcode xtensa_entry_opcode;
558 static xtensa_opcode xtensa_extui_opcode;
559 static xtensa_opcode xtensa_movi_opcode;
560 static xtensa_opcode xtensa_movi_n_opcode;
561 static xtensa_opcode xtensa_isync_opcode;
562 static xtensa_opcode xtensa_jx_opcode;
563 static xtensa_opcode xtensa_l32r_opcode;
564 static xtensa_opcode xtensa_loop_opcode;
565 static xtensa_opcode xtensa_loopnez_opcode;
566 static xtensa_opcode xtensa_loopgtz_opcode;
567 static xtensa_opcode xtensa_nop_opcode;
568 static xtensa_opcode xtensa_nop_n_opcode;
569 static xtensa_opcode xtensa_or_opcode;
570 static xtensa_opcode xtensa_ret_opcode;
571 static xtensa_opcode xtensa_ret_n_opcode;
572 static xtensa_opcode xtensa_retw_opcode;
573 static xtensa_opcode xtensa_retw_n_opcode;
574 static xtensa_opcode xtensa_rsr_lcount_opcode;
575 static xtensa_opcode xtensa_waiti_opcode;
576
577 \f
578 /* Command-line Options. */
579
580 bfd_boolean use_literal_section = TRUE;
581 static bfd_boolean align_targets = TRUE;
582 static bfd_boolean warn_unaligned_branch_targets = FALSE;
583 static bfd_boolean has_a0_b_retw = FALSE;
584 static bfd_boolean workaround_a0_b_retw = FALSE;
585 static bfd_boolean workaround_b_j_loop_end = FALSE;
586 static bfd_boolean workaround_short_loop = FALSE;
587 static bfd_boolean maybe_has_short_loop = FALSE;
588 static bfd_boolean workaround_close_loop_end = FALSE;
589 static bfd_boolean maybe_has_close_loop_end = FALSE;
590 static bfd_boolean enforce_three_byte_loop_align = FALSE;
591
592 /* When workaround_short_loops is TRUE, all loops with early exits must
593 have at least 3 instructions. workaround_all_short_loops is a modifier
594 to the workaround_short_loop flag. In addition to the
595 workaround_short_loop actions, all straightline loopgtz and loopnez
596 must have at least 3 instructions. */
597
598 static bfd_boolean workaround_all_short_loops = FALSE;
599
600
601 static void
602 xtensa_setup_hw_workarounds (int earliest, int latest)
603 {
604 if (earliest > latest)
605 as_fatal (_("illegal range of target hardware versions"));
606
607 /* Enable all workarounds for pre-T1050.0 hardware. */
608 if (earliest < 105000 || latest < 105000)
609 {
610 workaround_a0_b_retw |= TRUE;
611 workaround_b_j_loop_end |= TRUE;
612 workaround_short_loop |= TRUE;
613 workaround_close_loop_end |= TRUE;
614 workaround_all_short_loops |= TRUE;
615 enforce_three_byte_loop_align = TRUE;
616 }
617 }
618
619
620 enum
621 {
622 option_density = OPTION_MD_BASE,
623 option_no_density,
624
625 option_relax,
626 option_no_relax,
627
628 option_link_relax,
629 option_no_link_relax,
630
631 option_generics,
632 option_no_generics,
633
634 option_transform,
635 option_no_transform,
636
637 option_text_section_literals,
638 option_no_text_section_literals,
639
640 option_absolute_literals,
641 option_no_absolute_literals,
642
643 option_align_targets,
644 option_no_align_targets,
645
646 option_warn_unaligned_targets,
647
648 option_longcalls,
649 option_no_longcalls,
650
651 option_workaround_a0_b_retw,
652 option_no_workaround_a0_b_retw,
653
654 option_workaround_b_j_loop_end,
655 option_no_workaround_b_j_loop_end,
656
657 option_workaround_short_loop,
658 option_no_workaround_short_loop,
659
660 option_workaround_all_short_loops,
661 option_no_workaround_all_short_loops,
662
663 option_workaround_close_loop_end,
664 option_no_workaround_close_loop_end,
665
666 option_no_workarounds,
667
668 option_rename_section_name,
669
670 option_prefer_l32r,
671 option_prefer_const16,
672
673 option_target_hardware
674 };
675
676 const char *md_shortopts = "";
677
678 struct option md_longopts[] =
679 {
680 { "density", no_argument, NULL, option_density },
681 { "no-density", no_argument, NULL, option_no_density },
682
683 /* Both "relax" and "generics" are deprecated and treated as equivalent
684 to the "transform" option. */
685 { "relax", no_argument, NULL, option_relax },
686 { "no-relax", no_argument, NULL, option_no_relax },
687 { "generics", no_argument, NULL, option_generics },
688 { "no-generics", no_argument, NULL, option_no_generics },
689
690 { "transform", no_argument, NULL, option_transform },
691 { "no-transform", no_argument, NULL, option_no_transform },
692 { "text-section-literals", no_argument, NULL, option_text_section_literals },
693 { "no-text-section-literals", no_argument, NULL,
694 option_no_text_section_literals },
695 { "absolute-literals", no_argument, NULL, option_absolute_literals },
696 { "no-absolute-literals", no_argument, NULL, option_no_absolute_literals },
697 /* This option was changed from -align-target to -target-align
698 because it conflicted with the "-al" option. */
699 { "target-align", no_argument, NULL, option_align_targets },
700 { "no-target-align", no_argument, NULL, option_no_align_targets },
701 { "warn-unaligned-targets", no_argument, NULL,
702 option_warn_unaligned_targets },
703 { "longcalls", no_argument, NULL, option_longcalls },
704 { "no-longcalls", no_argument, NULL, option_no_longcalls },
705
706 { "no-workaround-a0-b-retw", no_argument, NULL,
707 option_no_workaround_a0_b_retw },
708 { "workaround-a0-b-retw", no_argument, NULL, option_workaround_a0_b_retw },
709
710 { "no-workaround-b-j-loop-end", no_argument, NULL,
711 option_no_workaround_b_j_loop_end },
712 { "workaround-b-j-loop-end", no_argument, NULL,
713 option_workaround_b_j_loop_end },
714
715 { "no-workaround-short-loops", no_argument, NULL,
716 option_no_workaround_short_loop },
717 { "workaround-short-loops", no_argument, NULL,
718 option_workaround_short_loop },
719
720 { "no-workaround-all-short-loops", no_argument, NULL,
721 option_no_workaround_all_short_loops },
722 { "workaround-all-short-loop", no_argument, NULL,
723 option_workaround_all_short_loops },
724
725 { "prefer-l32r", no_argument, NULL, option_prefer_l32r },
726 { "prefer-const16", no_argument, NULL, option_prefer_const16 },
727
728 { "no-workarounds", no_argument, NULL, option_no_workarounds },
729
730 { "no-workaround-close-loop-end", no_argument, NULL,
731 option_no_workaround_close_loop_end },
732 { "workaround-close-loop-end", no_argument, NULL,
733 option_workaround_close_loop_end },
734
735 { "rename-section", required_argument, NULL, option_rename_section_name },
736
737 { "link-relax", no_argument, NULL, option_link_relax },
738 { "no-link-relax", no_argument, NULL, option_no_link_relax },
739
740 { "target-hardware", required_argument, NULL, option_target_hardware },
741
742 { NULL, no_argument, NULL, 0 }
743 };
744
745 size_t md_longopts_size = sizeof md_longopts;
746
747
748 int
749 md_parse_option (int c, char *arg)
750 {
751 switch (c)
752 {
753 case option_density:
754 as_warn (_("--density option is ignored"));
755 return 1;
756 case option_no_density:
757 as_warn (_("--no-density option is ignored"));
758 return 1;
759 case option_link_relax:
760 linkrelax = 1;
761 return 1;
762 case option_no_link_relax:
763 linkrelax = 0;
764 return 1;
765 case option_generics:
766 as_warn (_("--generics is deprecated; use --transform instead"));
767 return md_parse_option (option_transform, arg);
768 case option_no_generics:
769 as_warn (_("--no-generics is deprecated; use --no-transform instead"));
770 return md_parse_option (option_no_transform, arg);
771 case option_relax:
772 as_warn (_("--relax is deprecated; use --transform instead"));
773 return md_parse_option (option_transform, arg);
774 case option_no_relax:
775 as_warn (_("--no-relax is deprecated; use --no-transform instead"));
776 return md_parse_option (option_no_transform, arg);
777 case option_longcalls:
778 directive_state[directive_longcalls] = TRUE;
779 return 1;
780 case option_no_longcalls:
781 directive_state[directive_longcalls] = FALSE;
782 return 1;
783 case option_text_section_literals:
784 use_literal_section = FALSE;
785 return 1;
786 case option_no_text_section_literals:
787 use_literal_section = TRUE;
788 return 1;
789 case option_absolute_literals:
790 if (!absolute_literals_supported)
791 {
792 as_fatal (_("--absolute-literals option not supported in this Xtensa configuration"));
793 return 0;
794 }
795 directive_state[directive_absolute_literals] = TRUE;
796 return 1;
797 case option_no_absolute_literals:
798 directive_state[directive_absolute_literals] = FALSE;
799 return 1;
800
801 case option_workaround_a0_b_retw:
802 workaround_a0_b_retw = TRUE;
803 return 1;
804 case option_no_workaround_a0_b_retw:
805 workaround_a0_b_retw = FALSE;
806 return 1;
807 case option_workaround_b_j_loop_end:
808 workaround_b_j_loop_end = TRUE;
809 return 1;
810 case option_no_workaround_b_j_loop_end:
811 workaround_b_j_loop_end = FALSE;
812 return 1;
813
814 case option_workaround_short_loop:
815 workaround_short_loop = TRUE;
816 return 1;
817 case option_no_workaround_short_loop:
818 workaround_short_loop = FALSE;
819 return 1;
820
821 case option_workaround_all_short_loops:
822 workaround_all_short_loops = TRUE;
823 return 1;
824 case option_no_workaround_all_short_loops:
825 workaround_all_short_loops = FALSE;
826 return 1;
827
828 case option_workaround_close_loop_end:
829 workaround_close_loop_end = TRUE;
830 return 1;
831 case option_no_workaround_close_loop_end:
832 workaround_close_loop_end = FALSE;
833 return 1;
834
835 case option_no_workarounds:
836 workaround_a0_b_retw = FALSE;
837 workaround_b_j_loop_end = FALSE;
838 workaround_short_loop = FALSE;
839 workaround_all_short_loops = FALSE;
840 workaround_close_loop_end = FALSE;
841 return 1;
842
843 case option_align_targets:
844 align_targets = TRUE;
845 return 1;
846 case option_no_align_targets:
847 align_targets = FALSE;
848 return 1;
849
850 case option_warn_unaligned_targets:
851 warn_unaligned_branch_targets = TRUE;
852 return 1;
853
854 case option_rename_section_name:
855 build_section_rename (arg);
856 return 1;
857
858 case 'Q':
859 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
860 should be emitted or not. FIXME: Not implemented. */
861 return 1;
862
863 case option_prefer_l32r:
864 if (prefer_const16)
865 as_fatal (_("prefer-l32r conflicts with prefer-const16"));
866 prefer_l32r = 1;
867 return 1;
868
869 case option_prefer_const16:
870 if (prefer_l32r)
871 as_fatal (_("prefer-const16 conflicts with prefer-l32r"));
872 prefer_const16 = 1;
873 return 1;
874
875 case option_target_hardware:
876 {
877 int earliest, latest = 0;
878 if (*arg == 0 || *arg == '-')
879 as_fatal (_("invalid target hardware version"));
880
881 earliest = strtol (arg, &arg, 0);
882
883 if (*arg == 0)
884 latest = earliest;
885 else if (*arg == '-')
886 {
887 if (*++arg == 0)
888 as_fatal (_("invalid target hardware version"));
889 latest = strtol (arg, &arg, 0);
890 }
891 if (*arg != 0)
892 as_fatal (_("invalid target hardware version"));
893
894 xtensa_setup_hw_workarounds (earliest, latest);
895 return 1;
896 }
897
898 case option_transform:
899 /* This option has no affect other than to use the defaults,
900 which are already set. */
901 return 1;
902
903 case option_no_transform:
904 /* This option turns off all transformations of any kind.
905 However, because we want to preserve the state of other
906 directives, we only change its own field. Thus, before
907 you perform any transformation, always check if transform
908 is available. If you use the functions we provide for this
909 purpose, you will be ok. */
910 directive_state[directive_transform] = FALSE;
911 return 1;
912
913 default:
914 return 0;
915 }
916 }
917
918
919 void
920 md_show_usage (FILE *stream)
921 {
922 fputs ("\n\
923 Xtensa options:\n\
924 --[no-]text-section-literals\n\
925 [Do not] put literals in the text section\n\
926 --[no-]absolute-literals\n\
927 [Do not] default to use non-PC-relative literals\n\
928 --[no-]target-align [Do not] try to align branch targets\n\
929 --[no-]longcalls [Do not] emit 32-bit call sequences\n\
930 --[no-]transform [Do not] transform instructions\n\
931 --rename-section old=new Rename section 'old' to 'new'\n", stream);
932 }
933
934 \f
935 /* Functions related to the list of current label symbols. */
936
937 static void
938 xtensa_add_insn_label (symbolS *sym)
939 {
940 sym_list *l;
941
942 if (!free_insn_labels)
943 l = (sym_list *) xmalloc (sizeof (sym_list));
944 else
945 {
946 l = free_insn_labels;
947 free_insn_labels = l->next;
948 }
949
950 l->sym = sym;
951 l->next = insn_labels;
952 insn_labels = l;
953 }
954
955
956 static void
957 xtensa_clear_insn_labels (void)
958 {
959 sym_list **pl;
960
961 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
962 ;
963 *pl = insn_labels;
964 insn_labels = NULL;
965 }
966
967
968 static void
969 xtensa_move_labels (fragS *new_frag, valueT new_offset)
970 {
971 sym_list *lit;
972
973 for (lit = insn_labels; lit; lit = lit->next)
974 {
975 symbolS *lit_sym = lit->sym;
976 S_SET_VALUE (lit_sym, new_offset);
977 symbol_set_frag (lit_sym, new_frag);
978 }
979 }
980
981 \f
982 /* Directive data and functions. */
983
984 typedef struct state_stackS_struct
985 {
986 directiveE directive;
987 bfd_boolean negated;
988 bfd_boolean old_state;
989 const char *file;
990 unsigned int line;
991 const void *datum;
992 struct state_stackS_struct *prev;
993 } state_stackS;
994
995 state_stackS *directive_state_stack;
996
997 const pseudo_typeS md_pseudo_table[] =
998 {
999 { "align", s_align_bytes, 0 }, /* Defaulting is invalid (0). */
1000 { "literal_position", xtensa_literal_position, 0 },
1001 { "frame", s_ignore, 0 }, /* Formerly used for STABS debugging. */
1002 { "long", xtensa_elf_cons, 4 },
1003 { "word", xtensa_elf_cons, 4 },
1004 { "4byte", xtensa_elf_cons, 4 },
1005 { "short", xtensa_elf_cons, 2 },
1006 { "2byte", xtensa_elf_cons, 2 },
1007 { "begin", xtensa_begin_directive, 0 },
1008 { "end", xtensa_end_directive, 0 },
1009 { "literal", xtensa_literal_pseudo, 0 },
1010 { "frequency", xtensa_frequency_pseudo, 0 },
1011 { NULL, 0, 0 },
1012 };
1013
1014
1015 static bfd_boolean
1016 use_transform (void)
1017 {
1018 /* After md_end, you should be checking frag by frag, rather
1019 than state directives. */
1020 assert (!past_xtensa_end);
1021 return directive_state[directive_transform];
1022 }
1023
1024
1025 static bfd_boolean
1026 do_align_targets (void)
1027 {
1028 /* Do not use this function after md_end; just look at align_targets
1029 instead. There is no target-align directive, so alignment is either
1030 enabled for all frags or not done at all. */
1031 assert (!past_xtensa_end);
1032 return align_targets && use_transform ();
1033 }
1034
1035
1036 static void
1037 directive_push (directiveE directive, bfd_boolean negated, const void *datum)
1038 {
1039 char *file;
1040 unsigned int line;
1041 state_stackS *stack = (state_stackS *) xmalloc (sizeof (state_stackS));
1042
1043 as_where (&file, &line);
1044
1045 stack->directive = directive;
1046 stack->negated = negated;
1047 stack->old_state = directive_state[directive];
1048 stack->file = file;
1049 stack->line = line;
1050 stack->datum = datum;
1051 stack->prev = directive_state_stack;
1052 directive_state_stack = stack;
1053
1054 directive_state[directive] = !negated;
1055 }
1056
1057
1058 static void
1059 directive_pop (directiveE *directive,
1060 bfd_boolean *negated,
1061 const char **file,
1062 unsigned int *line,
1063 const void **datum)
1064 {
1065 state_stackS *top = directive_state_stack;
1066
1067 if (!directive_state_stack)
1068 {
1069 as_bad (_("unmatched end directive"));
1070 *directive = directive_none;
1071 return;
1072 }
1073
1074 directive_state[directive_state_stack->directive] = top->old_state;
1075 *directive = top->directive;
1076 *negated = top->negated;
1077 *file = top->file;
1078 *line = top->line;
1079 *datum = top->datum;
1080 directive_state_stack = top->prev;
1081 free (top);
1082 }
1083
1084
1085 static void
1086 directive_balance (void)
1087 {
1088 while (directive_state_stack)
1089 {
1090 directiveE directive;
1091 bfd_boolean negated;
1092 const char *file;
1093 unsigned int line;
1094 const void *datum;
1095
1096 directive_pop (&directive, &negated, &file, &line, &datum);
1097 as_warn_where ((char *) file, line,
1098 _(".begin directive with no matching .end directive"));
1099 }
1100 }
1101
1102
1103 static bfd_boolean
1104 inside_directive (directiveE dir)
1105 {
1106 state_stackS *top = directive_state_stack;
1107
1108 while (top && top->directive != dir)
1109 top = top->prev;
1110
1111 return (top != NULL);
1112 }
1113
1114
1115 static void
1116 get_directive (directiveE *directive, bfd_boolean *negated)
1117 {
1118 int len;
1119 unsigned i;
1120 char *directive_string;
1121
1122 if (strncmp (input_line_pointer, "no-", 3) != 0)
1123 *negated = FALSE;
1124 else
1125 {
1126 *negated = TRUE;
1127 input_line_pointer += 3;
1128 }
1129
1130 len = strspn (input_line_pointer,
1131 "abcdefghijklmnopqrstuvwxyz_-/0123456789.");
1132
1133 /* This code is a hack to make .begin [no-][generics|relax] exactly
1134 equivalent to .begin [no-]transform. We should remove it when
1135 we stop accepting those options. */
1136
1137 if (strncmp (input_line_pointer, "generics", strlen ("generics")) == 0)
1138 {
1139 as_warn (_("[no-]generics is deprecated; use [no-]transform instead"));
1140 directive_string = "transform";
1141 }
1142 else if (strncmp (input_line_pointer, "relax", strlen ("relax")) == 0)
1143 {
1144 as_warn (_("[no-]relax is deprecated; use [no-]transform instead"));
1145 directive_string = "transform";
1146 }
1147 else
1148 directive_string = input_line_pointer;
1149
1150 for (i = 0; i < sizeof (directive_info) / sizeof (*directive_info); ++i)
1151 {
1152 if (strncmp (directive_string, directive_info[i].name, len) == 0)
1153 {
1154 input_line_pointer += len;
1155 *directive = (directiveE) i;
1156 if (*negated && !directive_info[i].can_be_negated)
1157 as_bad (_("directive %s cannot be negated"),
1158 directive_info[i].name);
1159 return;
1160 }
1161 }
1162
1163 as_bad (_("unknown directive"));
1164 *directive = (directiveE) XTENSA_UNDEFINED;
1165 }
1166
1167
1168 static void
1169 xtensa_begin_directive (int ignore ATTRIBUTE_UNUSED)
1170 {
1171 directiveE directive;
1172 bfd_boolean negated;
1173 emit_state *state;
1174 lit_state *ls;
1175
1176 get_directive (&directive, &negated);
1177 if (directive == (directiveE) XTENSA_UNDEFINED)
1178 {
1179 discard_rest_of_line ();
1180 return;
1181 }
1182
1183 if (cur_vinsn.inside_bundle)
1184 as_bad (_("directives are not valid inside bundles"));
1185
1186 switch (directive)
1187 {
1188 case directive_literal:
1189 if (!inside_directive (directive_literal))
1190 {
1191 /* Previous labels go with whatever follows this directive, not with
1192 the literal, so save them now. */
1193 saved_insn_labels = insn_labels;
1194 insn_labels = NULL;
1195 }
1196 as_warn (_(".begin literal is deprecated; use .literal instead"));
1197 state = (emit_state *) xmalloc (sizeof (emit_state));
1198 xtensa_switch_to_literal_fragment (state);
1199 directive_push (directive_literal, negated, state);
1200 break;
1201
1202 case directive_literal_prefix:
1203 /* Have to flush pending output because a movi relaxed to an l32r
1204 might produce a literal. */
1205 md_flush_pending_output ();
1206 /* Check to see if the current fragment is a literal
1207 fragment. If it is, then this operation is not allowed. */
1208 if (generating_literals)
1209 {
1210 as_bad (_("cannot set literal_prefix inside literal fragment"));
1211 return;
1212 }
1213
1214 /* Allocate the literal state for this section and push
1215 onto the directive stack. */
1216 ls = xmalloc (sizeof (lit_state));
1217 assert (ls);
1218
1219 *ls = default_lit_sections;
1220 directive_push (directive_literal_prefix, negated, ls);
1221
1222 /* Process the new prefix. */
1223 xtensa_literal_prefix ();
1224 break;
1225
1226 case directive_freeregs:
1227 /* This information is currently unused, but we'll accept the statement
1228 and just discard the rest of the line. This won't check the syntax,
1229 but it will accept every correct freeregs directive. */
1230 input_line_pointer += strcspn (input_line_pointer, "\n");
1231 directive_push (directive_freeregs, negated, 0);
1232 break;
1233
1234 case directive_schedule:
1235 md_flush_pending_output ();
1236 frag_var (rs_fill, 0, 0, frag_now->fr_subtype,
1237 frag_now->fr_symbol, frag_now->fr_offset, NULL);
1238 directive_push (directive_schedule, negated, 0);
1239 xtensa_set_frag_assembly_state (frag_now);
1240 break;
1241
1242 case directive_density:
1243 as_warn (_(".begin [no-]density is ignored"));
1244 break;
1245
1246 case directive_absolute_literals:
1247 md_flush_pending_output ();
1248 if (!absolute_literals_supported && !negated)
1249 {
1250 as_warn (_("Xtensa absolute literals option not supported; ignored"));
1251 break;
1252 }
1253 xtensa_set_frag_assembly_state (frag_now);
1254 directive_push (directive, negated, 0);
1255 break;
1256
1257 default:
1258 md_flush_pending_output ();
1259 xtensa_set_frag_assembly_state (frag_now);
1260 directive_push (directive, negated, 0);
1261 break;
1262 }
1263
1264 demand_empty_rest_of_line ();
1265 }
1266
1267
1268 static void
1269 xtensa_end_directive (int ignore ATTRIBUTE_UNUSED)
1270 {
1271 directiveE begin_directive, end_directive;
1272 bfd_boolean begin_negated, end_negated;
1273 const char *file;
1274 unsigned int line;
1275 emit_state *state;
1276 emit_state **state_ptr;
1277 lit_state *s;
1278
1279 if (cur_vinsn.inside_bundle)
1280 as_bad (_("directives are not valid inside bundles"));
1281
1282 get_directive (&end_directive, &end_negated);
1283
1284 md_flush_pending_output ();
1285
1286 switch (end_directive)
1287 {
1288 case (directiveE) XTENSA_UNDEFINED:
1289 discard_rest_of_line ();
1290 return;
1291
1292 case directive_density:
1293 as_warn (_(".end [no-]density is ignored"));
1294 demand_empty_rest_of_line ();
1295 break;
1296
1297 case directive_absolute_literals:
1298 if (!absolute_literals_supported && !end_negated)
1299 {
1300 as_warn (_("Xtensa absolute literals option not supported; ignored"));
1301 demand_empty_rest_of_line ();
1302 return;
1303 }
1304 break;
1305
1306 default:
1307 break;
1308 }
1309
1310 state_ptr = &state; /* use state_ptr to avoid type-punning warning */
1311 directive_pop (&begin_directive, &begin_negated, &file, &line,
1312 (const void **) state_ptr);
1313
1314 if (begin_directive != directive_none)
1315 {
1316 if (begin_directive != end_directive || begin_negated != end_negated)
1317 {
1318 as_bad (_("does not match begin %s%s at %s:%d"),
1319 begin_negated ? "no-" : "",
1320 directive_info[begin_directive].name, file, line);
1321 }
1322 else
1323 {
1324 switch (end_directive)
1325 {
1326 case directive_literal:
1327 frag_var (rs_fill, 0, 0, 0, NULL, 0, NULL);
1328 xtensa_restore_emit_state (state);
1329 xtensa_set_frag_assembly_state (frag_now);
1330 free (state);
1331 if (!inside_directive (directive_literal))
1332 {
1333 /* Restore the list of current labels. */
1334 xtensa_clear_insn_labels ();
1335 insn_labels = saved_insn_labels;
1336 }
1337 break;
1338
1339 case directive_literal_prefix:
1340 /* Restore the default collection sections from saved state. */
1341 s = (lit_state *) state;
1342 assert (s);
1343 default_lit_sections = *s;
1344
1345 /* Free the state storage. */
1346 free (s->lit_prefix);
1347 free (s);
1348 break;
1349
1350 case directive_schedule:
1351 case directive_freeregs:
1352 break;
1353
1354 default:
1355 xtensa_set_frag_assembly_state (frag_now);
1356 break;
1357 }
1358 }
1359 }
1360
1361 demand_empty_rest_of_line ();
1362 }
1363
1364
1365 /* Place an aligned literal fragment at the current location. */
1366
1367 static void
1368 xtensa_literal_position (int ignore ATTRIBUTE_UNUSED)
1369 {
1370 md_flush_pending_output ();
1371
1372 if (inside_directive (directive_literal))
1373 as_warn (_(".literal_position inside literal directive; ignoring"));
1374 xtensa_mark_literal_pool_location ();
1375
1376 demand_empty_rest_of_line ();
1377 xtensa_clear_insn_labels ();
1378 }
1379
1380
1381 /* Support .literal label, expr, ... */
1382
1383 static void
1384 xtensa_literal_pseudo (int ignored ATTRIBUTE_UNUSED)
1385 {
1386 emit_state state;
1387 char *p, *base_name;
1388 char c;
1389 segT dest_seg;
1390
1391 if (inside_directive (directive_literal))
1392 {
1393 as_bad (_(".literal not allowed inside .begin literal region"));
1394 ignore_rest_of_line ();
1395 return;
1396 }
1397
1398 md_flush_pending_output ();
1399
1400 /* Previous labels go with whatever follows this directive, not with
1401 the literal, so save them now. */
1402 saved_insn_labels = insn_labels;
1403 insn_labels = NULL;
1404
1405 /* If we are using text-section literals, then this is the right value... */
1406 dest_seg = now_seg;
1407
1408 base_name = input_line_pointer;
1409
1410 xtensa_switch_to_literal_fragment (&state);
1411
1412 /* ...but if we aren't using text-section-literals, then we
1413 need to put them in the section we just switched to. */
1414 if (use_literal_section || directive_state[directive_absolute_literals])
1415 dest_seg = now_seg;
1416
1417 /* All literals are aligned to four-byte boundaries. */
1418 frag_align (2, 0, 0);
1419 record_alignment (now_seg, 2);
1420
1421 c = get_symbol_end ();
1422 /* Just after name is now '\0'. */
1423 p = input_line_pointer;
1424 *p = c;
1425 SKIP_WHITESPACE ();
1426
1427 if (*input_line_pointer != ',' && *input_line_pointer != ':')
1428 {
1429 as_bad (_("expected comma or colon after symbol name; "
1430 "rest of line ignored"));
1431 ignore_rest_of_line ();
1432 xtensa_restore_emit_state (&state);
1433 return;
1434 }
1435 *p = 0;
1436
1437 colon (base_name);
1438
1439 *p = c;
1440 input_line_pointer++; /* skip ',' or ':' */
1441
1442 xtensa_elf_cons (4);
1443
1444 xtensa_restore_emit_state (&state);
1445
1446 /* Restore the list of current labels. */
1447 xtensa_clear_insn_labels ();
1448 insn_labels = saved_insn_labels;
1449 }
1450
1451
1452 static void
1453 xtensa_literal_prefix (void)
1454 {
1455 char *name;
1456 int len;
1457
1458 /* Parse the new prefix from the input_line_pointer. */
1459 SKIP_WHITESPACE ();
1460 len = strspn (input_line_pointer,
1461 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1462 "abcdefghijklmnopqrstuvwxyz_/0123456789.$");
1463
1464 /* Get a null-terminated copy of the name. */
1465 name = xmalloc (len + 1);
1466 assert (name);
1467 strncpy (name, input_line_pointer, len);
1468 name[len] = 0;
1469
1470 /* Skip the name in the input line. */
1471 input_line_pointer += len;
1472
1473 default_lit_sections.lit_prefix = name;
1474
1475 /* Clear cached literal sections, since the prefix has changed. */
1476 default_lit_sections.lit_seg = NULL;
1477 default_lit_sections.lit4_seg = NULL;
1478 }
1479
1480
1481 /* Support ".frequency branch_target_frequency fall_through_frequency". */
1482
1483 static void
1484 xtensa_frequency_pseudo (int ignored ATTRIBUTE_UNUSED)
1485 {
1486 float fall_through_f, target_f;
1487
1488 fall_through_f = (float) strtod (input_line_pointer, &input_line_pointer);
1489 if (fall_through_f < 0)
1490 {
1491 as_bad (_("fall through frequency must be greater than 0"));
1492 ignore_rest_of_line ();
1493 return;
1494 }
1495
1496 target_f = (float) strtod (input_line_pointer, &input_line_pointer);
1497 if (target_f < 0)
1498 {
1499 as_bad (_("branch target frequency must be greater than 0"));
1500 ignore_rest_of_line ();
1501 return;
1502 }
1503
1504 set_subseg_freq (now_seg, now_subseg, target_f + fall_through_f, target_f);
1505
1506 demand_empty_rest_of_line ();
1507 }
1508
1509
1510 /* Like normal .long/.short/.word, except support @plt, etc.
1511 Clobbers input_line_pointer, checks end-of-line. */
1512
1513 static void
1514 xtensa_elf_cons (int nbytes)
1515 {
1516 expressionS exp;
1517 bfd_reloc_code_real_type reloc;
1518
1519 md_flush_pending_output ();
1520
1521 if (cur_vinsn.inside_bundle)
1522 as_bad (_("directives are not valid inside bundles"));
1523
1524 if (is_it_end_of_statement ())
1525 {
1526 demand_empty_rest_of_line ();
1527 return;
1528 }
1529
1530 do
1531 {
1532 expression (&exp);
1533 if (exp.X_op == O_symbol
1534 && *input_line_pointer == '@'
1535 && ((reloc = xtensa_elf_suffix (&input_line_pointer, &exp))
1536 != BFD_RELOC_NONE))
1537 {
1538 reloc_howto_type *reloc_howto =
1539 bfd_reloc_type_lookup (stdoutput, reloc);
1540
1541 if (reloc == BFD_RELOC_UNUSED || !reloc_howto)
1542 as_bad (_("unsupported relocation"));
1543 else if ((reloc >= BFD_RELOC_XTENSA_SLOT0_OP
1544 && reloc <= BFD_RELOC_XTENSA_SLOT14_OP)
1545 || (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT
1546 && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT))
1547 as_bad (_("opcode-specific %s relocation used outside "
1548 "an instruction"), reloc_howto->name);
1549 else if (nbytes != (int) bfd_get_reloc_size (reloc_howto))
1550 as_bad (_("%s relocations do not fit in %d bytes"),
1551 reloc_howto->name, nbytes);
1552 else
1553 {
1554 char *p = frag_more ((int) nbytes);
1555 xtensa_set_frag_assembly_state (frag_now);
1556 fix_new_exp (frag_now, p - frag_now->fr_literal,
1557 nbytes, &exp, reloc_howto->pc_relative, reloc);
1558 }
1559 }
1560 else
1561 emit_expr (&exp, (unsigned int) nbytes);
1562 }
1563 while (*input_line_pointer++ == ',');
1564
1565 input_line_pointer--; /* Put terminator back into stream. */
1566 demand_empty_rest_of_line ();
1567 }
1568
1569 \f
1570 /* Parsing and Idiom Translation. */
1571
1572 /* Parse @plt, etc. and return the desired relocation. */
1573 static bfd_reloc_code_real_type
1574 xtensa_elf_suffix (char **str_p, expressionS *exp_p)
1575 {
1576 char ident[20];
1577 char *str = *str_p;
1578 char *str2;
1579 int ch;
1580 int len;
1581 struct suffix_reloc_map *ptr;
1582
1583 if (*str++ != '@')
1584 return BFD_RELOC_NONE;
1585
1586 for (ch = *str, str2 = ident;
1587 (str2 < ident + sizeof (ident) - 1
1588 && (ISALNUM (ch) || ch == '@'));
1589 ch = *++str)
1590 {
1591 *str2++ = (ISLOWER (ch)) ? ch : TOLOWER (ch);
1592 }
1593
1594 *str2 = '\0';
1595 len = str2 - ident;
1596
1597 ch = ident[0];
1598 for (ptr = &suffix_relocs[0]; ptr->length > 0; ptr++)
1599 if (ch == ptr->suffix[0]
1600 && len == ptr->length
1601 && memcmp (ident, ptr->suffix, ptr->length) == 0)
1602 {
1603 /* Now check for "identifier@suffix+constant". */
1604 if (*str == '-' || *str == '+')
1605 {
1606 char *orig_line = input_line_pointer;
1607 expressionS new_exp;
1608
1609 input_line_pointer = str;
1610 expression (&new_exp);
1611 if (new_exp.X_op == O_constant)
1612 {
1613 exp_p->X_add_number += new_exp.X_add_number;
1614 str = input_line_pointer;
1615 }
1616
1617 if (&input_line_pointer != str_p)
1618 input_line_pointer = orig_line;
1619 }
1620
1621 *str_p = str;
1622 return ptr->reloc;
1623 }
1624
1625 return BFD_RELOC_UNUSED;
1626 }
1627
1628
1629 /* Find the matching operator type. */
1630 static unsigned char
1631 map_suffix_reloc_to_operator (bfd_reloc_code_real_type reloc)
1632 {
1633 struct suffix_reloc_map *sfx;
1634 unsigned char operator = (unsigned char) -1;
1635
1636 for (sfx = &suffix_relocs[0]; sfx->suffix; sfx++)
1637 {
1638 if (sfx->reloc == reloc)
1639 {
1640 operator = sfx->operator;
1641 break;
1642 }
1643 }
1644 assert (operator != (unsigned char) -1);
1645 return operator;
1646 }
1647
1648
1649 /* Find the matching reloc type. */
1650 static bfd_reloc_code_real_type
1651 map_operator_to_reloc (unsigned char operator)
1652 {
1653 struct suffix_reloc_map *sfx;
1654 bfd_reloc_code_real_type reloc = BFD_RELOC_UNUSED;
1655
1656 for (sfx = &suffix_relocs[0]; sfx->suffix; sfx++)
1657 {
1658 if (sfx->operator == operator)
1659 {
1660 reloc = sfx->reloc;
1661 break;
1662 }
1663 }
1664
1665 if (reloc == BFD_RELOC_UNUSED)
1666 return BFD_RELOC_32;
1667
1668 return reloc;
1669 }
1670
1671
1672 static const char *
1673 expression_end (const char *name)
1674 {
1675 while (1)
1676 {
1677 switch (*name)
1678 {
1679 case '}':
1680 case ';':
1681 case '\0':
1682 case ',':
1683 case ':':
1684 return name;
1685 case ' ':
1686 case '\t':
1687 ++name;
1688 continue;
1689 default:
1690 return 0;
1691 }
1692 }
1693 }
1694
1695
1696 #define ERROR_REG_NUM ((unsigned) -1)
1697
1698 static unsigned
1699 tc_get_register (const char *prefix)
1700 {
1701 unsigned reg;
1702 const char *next_expr;
1703 const char *old_line_pointer;
1704
1705 SKIP_WHITESPACE ();
1706 old_line_pointer = input_line_pointer;
1707
1708 if (*input_line_pointer == '$')
1709 ++input_line_pointer;
1710
1711 /* Accept "sp" as a synonym for "a1". */
1712 if (input_line_pointer[0] == 's' && input_line_pointer[1] == 'p'
1713 && expression_end (input_line_pointer + 2))
1714 {
1715 input_line_pointer += 2;
1716 return 1; /* AR[1] */
1717 }
1718
1719 while (*input_line_pointer++ == *prefix++)
1720 ;
1721 --input_line_pointer;
1722 --prefix;
1723
1724 if (*prefix)
1725 {
1726 as_bad (_("bad register name: %s"), old_line_pointer);
1727 return ERROR_REG_NUM;
1728 }
1729
1730 if (!ISDIGIT ((unsigned char) *input_line_pointer))
1731 {
1732 as_bad (_("bad register number: %s"), input_line_pointer);
1733 return ERROR_REG_NUM;
1734 }
1735
1736 reg = 0;
1737
1738 while (ISDIGIT ((int) *input_line_pointer))
1739 reg = reg * 10 + *input_line_pointer++ - '0';
1740
1741 if (!(next_expr = expression_end (input_line_pointer)))
1742 {
1743 as_bad (_("bad register name: %s"), old_line_pointer);
1744 return ERROR_REG_NUM;
1745 }
1746
1747 input_line_pointer = (char *) next_expr;
1748
1749 return reg;
1750 }
1751
1752
1753 static void
1754 expression_maybe_register (xtensa_opcode opc, int opnd, expressionS *tok)
1755 {
1756 xtensa_isa isa = xtensa_default_isa;
1757
1758 /* Check if this is an immediate operand. */
1759 if (xtensa_operand_is_register (isa, opc, opnd) == 0)
1760 {
1761 bfd_reloc_code_real_type reloc;
1762 segT t = expression (tok);
1763 if (t == absolute_section
1764 && xtensa_operand_is_PCrelative (isa, opc, opnd) == 1)
1765 {
1766 assert (tok->X_op == O_constant);
1767 tok->X_op = O_symbol;
1768 tok->X_add_symbol = &abs_symbol;
1769 }
1770
1771 if ((tok->X_op == O_constant || tok->X_op == O_symbol)
1772 && ((reloc = xtensa_elf_suffix (&input_line_pointer, tok))
1773 != BFD_RELOC_NONE))
1774 {
1775 switch (reloc)
1776 {
1777 case BFD_RELOC_LO16:
1778 if (tok->X_op == O_constant)
1779 {
1780 tok->X_add_number &= 0xffff;
1781 return;
1782 }
1783 break;
1784 case BFD_RELOC_HI16:
1785 if (tok->X_op == O_constant)
1786 {
1787 tok->X_add_number = ((unsigned) tok->X_add_number) >> 16;
1788 return;
1789 }
1790 break;
1791 case BFD_RELOC_UNUSED:
1792 as_bad (_("unsupported relocation"));
1793 return;
1794 case BFD_RELOC_32_PCREL:
1795 as_bad (_("pcrel relocation not allowed in an instruction"));
1796 return;
1797 default:
1798 break;
1799 }
1800 tok->X_op = map_suffix_reloc_to_operator (reloc);
1801 }
1802 }
1803 else
1804 {
1805 xtensa_regfile opnd_rf = xtensa_operand_regfile (isa, opc, opnd);
1806 unsigned reg = tc_get_register (xtensa_regfile_shortname (isa, opnd_rf));
1807
1808 if (reg != ERROR_REG_NUM) /* Already errored */
1809 {
1810 uint32 buf = reg;
1811 if (xtensa_operand_encode (isa, opc, opnd, &buf))
1812 as_bad (_("register number out of range"));
1813 }
1814
1815 tok->X_op = O_register;
1816 tok->X_add_symbol = 0;
1817 tok->X_add_number = reg;
1818 }
1819 }
1820
1821
1822 /* Split up the arguments for an opcode or pseudo-op. */
1823
1824 static int
1825 tokenize_arguments (char **args, char *str)
1826 {
1827 char *old_input_line_pointer;
1828 bfd_boolean saw_comma = FALSE;
1829 bfd_boolean saw_arg = FALSE;
1830 bfd_boolean saw_colon = FALSE;
1831 int num_args = 0;
1832 char *arg_end, *arg;
1833 int arg_len;
1834
1835 /* Save and restore input_line_pointer around this function. */
1836 old_input_line_pointer = input_line_pointer;
1837 input_line_pointer = str;
1838
1839 while (*input_line_pointer)
1840 {
1841 SKIP_WHITESPACE ();
1842 switch (*input_line_pointer)
1843 {
1844 case '\0':
1845 case '}':
1846 goto fini;
1847
1848 case ':':
1849 input_line_pointer++;
1850 if (saw_comma || saw_colon || !saw_arg)
1851 goto err;
1852 saw_colon = TRUE;
1853 break;
1854
1855 case ',':
1856 input_line_pointer++;
1857 if (saw_comma || saw_colon || !saw_arg)
1858 goto err;
1859 saw_comma = TRUE;
1860 break;
1861
1862 default:
1863 if (!saw_comma && !saw_colon && saw_arg)
1864 goto err;
1865
1866 arg_end = input_line_pointer + 1;
1867 while (!expression_end (arg_end))
1868 arg_end += 1;
1869
1870 arg_len = arg_end - input_line_pointer;
1871 arg = (char *) xmalloc ((saw_colon ? 1 : 0) + arg_len + 1);
1872 args[num_args] = arg;
1873
1874 if (saw_colon)
1875 *arg++ = ':';
1876 strncpy (arg, input_line_pointer, arg_len);
1877 arg[arg_len] = '\0';
1878
1879 input_line_pointer = arg_end;
1880 num_args += 1;
1881 saw_comma = FALSE;
1882 saw_colon = FALSE;
1883 saw_arg = TRUE;
1884 break;
1885 }
1886 }
1887
1888 fini:
1889 if (saw_comma || saw_colon)
1890 goto err;
1891 input_line_pointer = old_input_line_pointer;
1892 return num_args;
1893
1894 err:
1895 if (saw_comma)
1896 as_bad (_("extra comma"));
1897 else if (saw_colon)
1898 as_bad (_("extra colon"));
1899 else if (!saw_arg)
1900 as_bad (_("missing argument"));
1901 else
1902 as_bad (_("missing comma or colon"));
1903 input_line_pointer = old_input_line_pointer;
1904 return -1;
1905 }
1906
1907
1908 /* Parse the arguments to an opcode. Return TRUE on error. */
1909
1910 static bfd_boolean
1911 parse_arguments (TInsn *insn, int num_args, char **arg_strings)
1912 {
1913 expressionS *tok, *last_tok;
1914 xtensa_opcode opcode = insn->opcode;
1915 bfd_boolean had_error = TRUE;
1916 xtensa_isa isa = xtensa_default_isa;
1917 int n, num_regs = 0;
1918 int opcode_operand_count;
1919 int opnd_cnt, last_opnd_cnt;
1920 unsigned int next_reg = 0;
1921 char *old_input_line_pointer;
1922
1923 if (insn->insn_type == ITYPE_LITERAL)
1924 opcode_operand_count = 1;
1925 else
1926 opcode_operand_count = xtensa_opcode_num_operands (isa, opcode);
1927
1928 tok = insn->tok;
1929 memset (tok, 0, sizeof (*tok) * MAX_INSN_ARGS);
1930
1931 /* Save and restore input_line_pointer around this function. */
1932 old_input_line_pointer = input_line_pointer;
1933
1934 last_tok = 0;
1935 last_opnd_cnt = -1;
1936 opnd_cnt = 0;
1937
1938 /* Skip invisible operands. */
1939 while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0)
1940 {
1941 opnd_cnt += 1;
1942 tok++;
1943 }
1944
1945 for (n = 0; n < num_args; n++)
1946 {
1947 input_line_pointer = arg_strings[n];
1948 if (*input_line_pointer == ':')
1949 {
1950 xtensa_regfile opnd_rf;
1951 input_line_pointer++;
1952 if (num_regs == 0)
1953 goto err;
1954 assert (opnd_cnt > 0);
1955 num_regs--;
1956 opnd_rf = xtensa_operand_regfile (isa, opcode, last_opnd_cnt);
1957 if (next_reg
1958 != tc_get_register (xtensa_regfile_shortname (isa, opnd_rf)))
1959 as_warn (_("incorrect register number, ignoring"));
1960 next_reg++;
1961 }
1962 else
1963 {
1964 if (opnd_cnt >= opcode_operand_count)
1965 {
1966 as_warn (_("too many arguments"));
1967 goto err;
1968 }
1969 assert (opnd_cnt < MAX_INSN_ARGS);
1970
1971 expression_maybe_register (opcode, opnd_cnt, tok);
1972 next_reg = tok->X_add_number + 1;
1973
1974 if (tok->X_op == O_illegal || tok->X_op == O_absent)
1975 goto err;
1976 if (xtensa_operand_is_register (isa, opcode, opnd_cnt) == 1)
1977 {
1978 num_regs = xtensa_operand_num_regs (isa, opcode, opnd_cnt) - 1;
1979 /* minus 1 because we are seeing one right now */
1980 }
1981 else
1982 num_regs = 0;
1983
1984 last_tok = tok;
1985 last_opnd_cnt = opnd_cnt;
1986
1987 do
1988 {
1989 opnd_cnt += 1;
1990 tok++;
1991 }
1992 while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0);
1993 }
1994 }
1995
1996 if (num_regs > 0 && ((int) next_reg != last_tok->X_add_number + 1))
1997 goto err;
1998
1999 insn->ntok = tok - insn->tok;
2000 had_error = FALSE;
2001
2002 err:
2003 input_line_pointer = old_input_line_pointer;
2004 return had_error;
2005 }
2006
2007
2008 static int
2009 get_invisible_operands (TInsn *insn)
2010 {
2011 xtensa_isa isa = xtensa_default_isa;
2012 static xtensa_insnbuf slotbuf = NULL;
2013 xtensa_format fmt;
2014 xtensa_opcode opc = insn->opcode;
2015 int slot, opnd, fmt_found;
2016 unsigned val;
2017
2018 if (!slotbuf)
2019 slotbuf = xtensa_insnbuf_alloc (isa);
2020
2021 /* Find format/slot where this can be encoded. */
2022 fmt_found = 0;
2023 slot = 0;
2024 for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
2025 {
2026 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
2027 {
2028 if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opc) == 0)
2029 {
2030 fmt_found = 1;
2031 break;
2032 }
2033 }
2034 if (fmt_found) break;
2035 }
2036
2037 if (!fmt_found)
2038 {
2039 as_bad (_("cannot encode opcode \"%s\""), xtensa_opcode_name (isa, opc));
2040 return -1;
2041 }
2042
2043 /* First encode all the visible operands
2044 (to deal with shared field operands). */
2045 for (opnd = 0; opnd < insn->ntok; opnd++)
2046 {
2047 if (xtensa_operand_is_visible (isa, opc, opnd) == 1
2048 && (insn->tok[opnd].X_op == O_register
2049 || insn->tok[opnd].X_op == O_constant))
2050 {
2051 val = insn->tok[opnd].X_add_number;
2052 xtensa_operand_encode (isa, opc, opnd, &val);
2053 xtensa_operand_set_field (isa, opc, opnd, fmt, slot, slotbuf, val);
2054 }
2055 }
2056
2057 /* Then pull out the values for the invisible ones. */
2058 for (opnd = 0; opnd < insn->ntok; opnd++)
2059 {
2060 if (xtensa_operand_is_visible (isa, opc, opnd) == 0)
2061 {
2062 xtensa_operand_get_field (isa, opc, opnd, fmt, slot, slotbuf, &val);
2063 xtensa_operand_decode (isa, opc, opnd, &val);
2064 insn->tok[opnd].X_add_number = val;
2065 if (xtensa_operand_is_register (isa, opc, opnd) == 1)
2066 insn->tok[opnd].X_op = O_register;
2067 else
2068 insn->tok[opnd].X_op = O_constant;
2069 }
2070 }
2071
2072 return 0;
2073 }
2074
2075
2076 static void
2077 xg_reverse_shift_count (char **cnt_argp)
2078 {
2079 char *cnt_arg, *new_arg;
2080 cnt_arg = *cnt_argp;
2081
2082 /* replace the argument with "31-(argument)" */
2083 new_arg = (char *) xmalloc (strlen (cnt_arg) + 6);
2084 sprintf (new_arg, "31-(%s)", cnt_arg);
2085
2086 free (cnt_arg);
2087 *cnt_argp = new_arg;
2088 }
2089
2090
2091 /* If "arg" is a constant expression, return non-zero with the value
2092 in *valp. */
2093
2094 static int
2095 xg_arg_is_constant (char *arg, offsetT *valp)
2096 {
2097 expressionS exp;
2098 char *save_ptr = input_line_pointer;
2099
2100 input_line_pointer = arg;
2101 expression (&exp);
2102 input_line_pointer = save_ptr;
2103
2104 if (exp.X_op == O_constant)
2105 {
2106 *valp = exp.X_add_number;
2107 return 1;
2108 }
2109
2110 return 0;
2111 }
2112
2113
2114 static void
2115 xg_replace_opname (char **popname, char *newop)
2116 {
2117 free (*popname);
2118 *popname = (char *) xmalloc (strlen (newop) + 1);
2119 strcpy (*popname, newop);
2120 }
2121
2122
2123 static int
2124 xg_check_num_args (int *pnum_args,
2125 int expected_num,
2126 char *opname,
2127 char **arg_strings)
2128 {
2129 int num_args = *pnum_args;
2130
2131 if (num_args < expected_num)
2132 {
2133 as_bad (_("not enough operands (%d) for '%s'; expected %d"),
2134 num_args, opname, expected_num);
2135 return -1;
2136 }
2137
2138 if (num_args > expected_num)
2139 {
2140 as_warn (_("too many operands (%d) for '%s'; expected %d"),
2141 num_args, opname, expected_num);
2142 while (num_args-- > expected_num)
2143 {
2144 free (arg_strings[num_args]);
2145 arg_strings[num_args] = 0;
2146 }
2147 *pnum_args = expected_num;
2148 return -1;
2149 }
2150
2151 return 0;
2152 }
2153
2154
2155 /* If the register is not specified as part of the opcode,
2156 then get it from the operand and move it to the opcode. */
2157
2158 static int
2159 xg_translate_sysreg_op (char **popname, int *pnum_args, char **arg_strings)
2160 {
2161 xtensa_isa isa = xtensa_default_isa;
2162 xtensa_sysreg sr;
2163 char *opname, *new_opname;
2164 const char *sr_name;
2165 int is_user, is_write;
2166
2167 opname = *popname;
2168 if (*opname == '_')
2169 opname += 1;
2170 is_user = (opname[1] == 'u');
2171 is_write = (opname[0] == 'w');
2172
2173 /* Opname == [rw]ur or [rwx]sr... */
2174
2175 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
2176 return -1;
2177
2178 /* Check if the argument is a symbolic register name. */
2179 sr = xtensa_sysreg_lookup_name (isa, arg_strings[1]);
2180 /* Handle WSR to "INTSET" as a special case. */
2181 if (sr == XTENSA_UNDEFINED && is_write && !is_user
2182 && !strcasecmp (arg_strings[1], "intset"))
2183 sr = xtensa_sysreg_lookup_name (isa, "interrupt");
2184 if (sr == XTENSA_UNDEFINED
2185 || (xtensa_sysreg_is_user (isa, sr) == 1) != is_user)
2186 {
2187 /* Maybe it's a register number.... */
2188 offsetT val;
2189 if (!xg_arg_is_constant (arg_strings[1], &val))
2190 {
2191 as_bad (_("invalid register '%s' for '%s' instruction"),
2192 arg_strings[1], opname);
2193 return -1;
2194 }
2195 sr = xtensa_sysreg_lookup (isa, val, is_user);
2196 if (sr == XTENSA_UNDEFINED)
2197 {
2198 as_bad (_("invalid register number (%ld) for '%s' instruction"),
2199 (long) val, opname);
2200 return -1;
2201 }
2202 }
2203
2204 /* Remove the last argument, which is now part of the opcode. */
2205 free (arg_strings[1]);
2206 arg_strings[1] = 0;
2207 *pnum_args = 1;
2208
2209 /* Translate the opcode. */
2210 sr_name = xtensa_sysreg_name (isa, sr);
2211 /* Another special case for "WSR.INTSET".... */
2212 if (is_write && !is_user && !strcasecmp ("interrupt", sr_name))
2213 sr_name = "intset";
2214 new_opname = (char *) xmalloc (strlen (sr_name) + 6);
2215 sprintf (new_opname, "%s.%s", *popname, sr_name);
2216 free (*popname);
2217 *popname = new_opname;
2218
2219 return 0;
2220 }
2221
2222
2223 static int
2224 xtensa_translate_old_userreg_ops (char **popname)
2225 {
2226 xtensa_isa isa = xtensa_default_isa;
2227 xtensa_sysreg sr;
2228 char *opname, *new_opname;
2229 const char *sr_name;
2230 bfd_boolean has_underbar = FALSE;
2231
2232 opname = *popname;
2233 if (opname[0] == '_')
2234 {
2235 has_underbar = TRUE;
2236 opname += 1;
2237 }
2238
2239 sr = xtensa_sysreg_lookup_name (isa, opname + 1);
2240 if (sr != XTENSA_UNDEFINED)
2241 {
2242 /* The new default name ("nnn") is different from the old default
2243 name ("URnnn"). The old default is handled below, and we don't
2244 want to recognize [RW]nnn, so do nothing if the name is the (new)
2245 default. */
2246 static char namebuf[10];
2247 sprintf (namebuf, "%d", xtensa_sysreg_number (isa, sr));
2248 if (strcmp (namebuf, opname + 1) == 0)
2249 return 0;
2250 }
2251 else
2252 {
2253 offsetT val;
2254 char *end;
2255
2256 /* Only continue if the reg name is "URnnn". */
2257 if (opname[1] != 'u' || opname[2] != 'r')
2258 return 0;
2259 val = strtoul (opname + 3, &end, 10);
2260 if (*end != '\0')
2261 return 0;
2262
2263 sr = xtensa_sysreg_lookup (isa, val, 1);
2264 if (sr == XTENSA_UNDEFINED)
2265 {
2266 as_bad (_("invalid register number (%ld) for '%s'"),
2267 (long) val, opname);
2268 return -1;
2269 }
2270 }
2271
2272 /* Translate the opcode. */
2273 sr_name = xtensa_sysreg_name (isa, sr);
2274 new_opname = (char *) xmalloc (strlen (sr_name) + 6);
2275 sprintf (new_opname, "%s%cur.%s", (has_underbar ? "_" : ""),
2276 opname[0], sr_name);
2277 free (*popname);
2278 *popname = new_opname;
2279
2280 return 0;
2281 }
2282
2283
2284 static int
2285 xtensa_translate_zero_immed (char *old_op,
2286 char *new_op,
2287 char **popname,
2288 int *pnum_args,
2289 char **arg_strings)
2290 {
2291 char *opname;
2292 offsetT val;
2293
2294 opname = *popname;
2295 assert (opname[0] != '_');
2296
2297 if (strcmp (opname, old_op) != 0)
2298 return 0;
2299
2300 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2301 return -1;
2302 if (xg_arg_is_constant (arg_strings[1], &val) && val == 0)
2303 {
2304 xg_replace_opname (popname, new_op);
2305 free (arg_strings[1]);
2306 arg_strings[1] = arg_strings[2];
2307 arg_strings[2] = 0;
2308 *pnum_args = 2;
2309 }
2310
2311 return 0;
2312 }
2313
2314
2315 /* If the instruction is an idiom (i.e., a built-in macro), translate it.
2316 Returns non-zero if an error was found. */
2317
2318 static int
2319 xg_translate_idioms (char **popname, int *pnum_args, char **arg_strings)
2320 {
2321 char *opname = *popname;
2322 bfd_boolean has_underbar = FALSE;
2323
2324 if (*opname == '_')
2325 {
2326 has_underbar = TRUE;
2327 opname += 1;
2328 }
2329
2330 if (strcmp (opname, "mov") == 0)
2331 {
2332 if (use_transform () && !has_underbar && density_supported)
2333 xg_replace_opname (popname, "mov.n");
2334 else
2335 {
2336 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
2337 return -1;
2338 xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
2339 arg_strings[2] = (char *) xmalloc (strlen (arg_strings[1]) + 1);
2340 strcpy (arg_strings[2], arg_strings[1]);
2341 *pnum_args = 3;
2342 }
2343 return 0;
2344 }
2345
2346 if (strcmp (opname, "bbsi.l") == 0)
2347 {
2348 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2349 return -1;
2350 xg_replace_opname (popname, (has_underbar ? "_bbsi" : "bbsi"));
2351 if (target_big_endian)
2352 xg_reverse_shift_count (&arg_strings[1]);
2353 return 0;
2354 }
2355
2356 if (strcmp (opname, "bbci.l") == 0)
2357 {
2358 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2359 return -1;
2360 xg_replace_opname (popname, (has_underbar ? "_bbci" : "bbci"));
2361 if (target_big_endian)
2362 xg_reverse_shift_count (&arg_strings[1]);
2363 return 0;
2364 }
2365
2366 /* Don't do anything special with NOPs inside FLIX instructions. They
2367 are handled elsewhere. Real NOP instructions are always available
2368 in configurations with FLIX, so this should never be an issue but
2369 check for it anyway. */
2370 if (!cur_vinsn.inside_bundle && xtensa_nop_opcode == XTENSA_UNDEFINED
2371 && strcmp (opname, "nop") == 0)
2372 {
2373 if (use_transform () && !has_underbar && density_supported)
2374 xg_replace_opname (popname, "nop.n");
2375 else
2376 {
2377 if (xg_check_num_args (pnum_args, 0, opname, arg_strings))
2378 return -1;
2379 xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
2380 arg_strings[0] = (char *) xmalloc (3);
2381 arg_strings[1] = (char *) xmalloc (3);
2382 arg_strings[2] = (char *) xmalloc (3);
2383 strcpy (arg_strings[0], "a1");
2384 strcpy (arg_strings[1], "a1");
2385 strcpy (arg_strings[2], "a1");
2386 *pnum_args = 3;
2387 }
2388 return 0;
2389 }
2390
2391 /* Recognize [RW]UR and [RWX]SR. */
2392 if ((((opname[0] == 'r' || opname[0] == 'w')
2393 && (opname[1] == 'u' || opname[1] == 's'))
2394 || (opname[0] == 'x' && opname[1] == 's'))
2395 && opname[2] == 'r'
2396 && opname[3] == '\0')
2397 return xg_translate_sysreg_op (popname, pnum_args, arg_strings);
2398
2399 /* Backward compatibility for RUR and WUR: Recognize [RW]UR<nnn> and
2400 [RW]<name> if <name> is the non-default name of a user register. */
2401 if ((opname[0] == 'r' || opname[0] == 'w')
2402 && xtensa_opcode_lookup (xtensa_default_isa, opname) == XTENSA_UNDEFINED)
2403 return xtensa_translate_old_userreg_ops (popname);
2404
2405 /* Relax branches that don't allow comparisons against an immediate value
2406 of zero to the corresponding branches with implicit zero immediates. */
2407 if (!has_underbar && use_transform ())
2408 {
2409 if (xtensa_translate_zero_immed ("bnei", "bnez", popname,
2410 pnum_args, arg_strings))
2411 return -1;
2412
2413 if (xtensa_translate_zero_immed ("beqi", "beqz", popname,
2414 pnum_args, arg_strings))
2415 return -1;
2416
2417 if (xtensa_translate_zero_immed ("bgei", "bgez", popname,
2418 pnum_args, arg_strings))
2419 return -1;
2420
2421 if (xtensa_translate_zero_immed ("blti", "bltz", popname,
2422 pnum_args, arg_strings))
2423 return -1;
2424 }
2425
2426 return 0;
2427 }
2428
2429 \f
2430 /* Functions for dealing with the Xtensa ISA. */
2431
2432 /* Currently the assembler only allows us to use a single target per
2433 fragment. Because of this, only one operand for a given
2434 instruction may be symbolic. If there is a PC-relative operand,
2435 the last one is chosen. Otherwise, the result is the number of the
2436 last immediate operand, and if there are none of those, we fail and
2437 return -1. */
2438
2439 static int
2440 get_relaxable_immed (xtensa_opcode opcode)
2441 {
2442 int last_immed = -1;
2443 int noperands, opi;
2444
2445 if (opcode == XTENSA_UNDEFINED)
2446 return -1;
2447
2448 noperands = xtensa_opcode_num_operands (xtensa_default_isa, opcode);
2449 for (opi = noperands - 1; opi >= 0; opi--)
2450 {
2451 if (xtensa_operand_is_visible (xtensa_default_isa, opcode, opi) == 0)
2452 continue;
2453 if (xtensa_operand_is_PCrelative (xtensa_default_isa, opcode, opi) == 1)
2454 return opi;
2455 if (last_immed == -1
2456 && xtensa_operand_is_register (xtensa_default_isa, opcode, opi) == 0)
2457 last_immed = opi;
2458 }
2459 return last_immed;
2460 }
2461
2462
2463 static xtensa_opcode
2464 get_opcode_from_buf (const char *buf, int slot)
2465 {
2466 static xtensa_insnbuf insnbuf = NULL;
2467 static xtensa_insnbuf slotbuf = NULL;
2468 xtensa_isa isa = xtensa_default_isa;
2469 xtensa_format fmt;
2470
2471 if (!insnbuf)
2472 {
2473 insnbuf = xtensa_insnbuf_alloc (isa);
2474 slotbuf = xtensa_insnbuf_alloc (isa);
2475 }
2476
2477 xtensa_insnbuf_from_chars (isa, insnbuf, (const unsigned char *) buf, 0);
2478 fmt = xtensa_format_decode (isa, insnbuf);
2479 if (fmt == XTENSA_UNDEFINED)
2480 return XTENSA_UNDEFINED;
2481
2482 if (slot >= xtensa_format_num_slots (isa, fmt))
2483 return XTENSA_UNDEFINED;
2484
2485 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
2486 return xtensa_opcode_decode (isa, fmt, slot, slotbuf);
2487 }
2488
2489
2490 #ifdef TENSILICA_DEBUG
2491
2492 /* For debugging, print out the mapping of opcode numbers to opcodes. */
2493
2494 static void
2495 xtensa_print_insn_table (void)
2496 {
2497 int num_opcodes, num_operands;
2498 xtensa_opcode opcode;
2499 xtensa_isa isa = xtensa_default_isa;
2500
2501 num_opcodes = xtensa_isa_num_opcodes (xtensa_default_isa);
2502 for (opcode = 0; opcode < num_opcodes; opcode++)
2503 {
2504 int opn;
2505 fprintf (stderr, "%d: %s: ", opcode, xtensa_opcode_name (isa, opcode));
2506 num_operands = xtensa_opcode_num_operands (isa, opcode);
2507 for (opn = 0; opn < num_operands; opn++)
2508 {
2509 if (xtensa_operand_is_visible (isa, opcode, opn) == 0)
2510 continue;
2511 if (xtensa_operand_is_register (isa, opcode, opn) == 1)
2512 {
2513 xtensa_regfile opnd_rf =
2514 xtensa_operand_regfile (isa, opcode, opn);
2515 fprintf (stderr, "%s ", xtensa_regfile_shortname (isa, opnd_rf));
2516 }
2517 else if (xtensa_operand_is_PCrelative (isa, opcode, opn) == 1)
2518 fputs ("[lLr] ", stderr);
2519 else
2520 fputs ("i ", stderr);
2521 }
2522 fprintf (stderr, "\n");
2523 }
2524 }
2525
2526
2527 static void
2528 print_vliw_insn (xtensa_insnbuf vbuf)
2529 {
2530 xtensa_isa isa = xtensa_default_isa;
2531 xtensa_format f = xtensa_format_decode (isa, vbuf);
2532 xtensa_insnbuf sbuf = xtensa_insnbuf_alloc (isa);
2533 int op;
2534
2535 fprintf (stderr, "format = %d\n", f);
2536
2537 for (op = 0; op < xtensa_format_num_slots (isa, f); op++)
2538 {
2539 xtensa_opcode opcode;
2540 const char *opname;
2541 int operands;
2542
2543 xtensa_format_get_slot (isa, f, op, vbuf, sbuf);
2544 opcode = xtensa_opcode_decode (isa, f, op, sbuf);
2545 opname = xtensa_opcode_name (isa, opcode);
2546
2547 fprintf (stderr, "op in slot %i is %s;\n", op, opname);
2548 fprintf (stderr, " operands = ");
2549 for (operands = 0;
2550 operands < xtensa_opcode_num_operands (isa, opcode);
2551 operands++)
2552 {
2553 unsigned int val;
2554 if (xtensa_operand_is_visible (isa, opcode, operands) == 0)
2555 continue;
2556 xtensa_operand_get_field (isa, opcode, operands, f, op, sbuf, &val);
2557 xtensa_operand_decode (isa, opcode, operands, &val);
2558 fprintf (stderr, "%d ", val);
2559 }
2560 fprintf (stderr, "\n");
2561 }
2562 xtensa_insnbuf_free (isa, sbuf);
2563 }
2564
2565 #endif /* TENSILICA_DEBUG */
2566
2567
2568 static bfd_boolean
2569 is_direct_call_opcode (xtensa_opcode opcode)
2570 {
2571 xtensa_isa isa = xtensa_default_isa;
2572 int n, num_operands;
2573
2574 if (xtensa_opcode_is_call (isa, opcode) != 1)
2575 return FALSE;
2576
2577 num_operands = xtensa_opcode_num_operands (isa, opcode);
2578 for (n = 0; n < num_operands; n++)
2579 {
2580 if (xtensa_operand_is_register (isa, opcode, n) == 0
2581 && xtensa_operand_is_PCrelative (isa, opcode, n) == 1)
2582 return TRUE;
2583 }
2584 return FALSE;
2585 }
2586
2587
2588 /* Convert from BFD relocation type code to slot and operand number.
2589 Returns non-zero on failure. */
2590
2591 static int
2592 decode_reloc (bfd_reloc_code_real_type reloc, int *slot, bfd_boolean *is_alt)
2593 {
2594 if (reloc >= BFD_RELOC_XTENSA_SLOT0_OP
2595 && reloc <= BFD_RELOC_XTENSA_SLOT14_OP)
2596 {
2597 *slot = reloc - BFD_RELOC_XTENSA_SLOT0_OP;
2598 *is_alt = FALSE;
2599 }
2600 else if (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT
2601 && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT)
2602 {
2603 *slot = reloc - BFD_RELOC_XTENSA_SLOT0_ALT;
2604 *is_alt = TRUE;
2605 }
2606 else
2607 return -1;
2608
2609 return 0;
2610 }
2611
2612
2613 /* Convert from slot number to BFD relocation type code for the
2614 standard PC-relative relocations. Return BFD_RELOC_NONE on
2615 failure. */
2616
2617 static bfd_reloc_code_real_type
2618 encode_reloc (int slot)
2619 {
2620 if (slot < 0 || slot > 14)
2621 return BFD_RELOC_NONE;
2622
2623 return BFD_RELOC_XTENSA_SLOT0_OP + slot;
2624 }
2625
2626
2627 /* Convert from slot numbers to BFD relocation type code for the
2628 "alternate" relocations. Return BFD_RELOC_NONE on failure. */
2629
2630 static bfd_reloc_code_real_type
2631 encode_alt_reloc (int slot)
2632 {
2633 if (slot < 0 || slot > 14)
2634 return BFD_RELOC_NONE;
2635
2636 return BFD_RELOC_XTENSA_SLOT0_ALT + slot;
2637 }
2638
2639
2640 static void
2641 xtensa_insnbuf_set_operand (xtensa_insnbuf slotbuf,
2642 xtensa_format fmt,
2643 int slot,
2644 xtensa_opcode opcode,
2645 int operand,
2646 uint32 value,
2647 const char *file,
2648 unsigned int line)
2649 {
2650 uint32 valbuf = value;
2651
2652 if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf))
2653 {
2654 if (xtensa_operand_is_PCrelative (xtensa_default_isa, opcode, operand)
2655 == 1)
2656 as_bad_where ((char *) file, line,
2657 _("operand %d of '%s' has out of range value '%u'"),
2658 operand + 1,
2659 xtensa_opcode_name (xtensa_default_isa, opcode),
2660 value);
2661 else
2662 as_bad_where ((char *) file, line,
2663 _("operand %d of '%s' has invalid value '%u'"),
2664 operand + 1,
2665 xtensa_opcode_name (xtensa_default_isa, opcode),
2666 value);
2667 return;
2668 }
2669
2670 xtensa_operand_set_field (xtensa_default_isa, opcode, operand, fmt, slot,
2671 slotbuf, valbuf);
2672 }
2673
2674
2675 static uint32
2676 xtensa_insnbuf_get_operand (xtensa_insnbuf slotbuf,
2677 xtensa_format fmt,
2678 int slot,
2679 xtensa_opcode opcode,
2680 int opnum)
2681 {
2682 uint32 val = 0;
2683 (void) xtensa_operand_get_field (xtensa_default_isa, opcode, opnum,
2684 fmt, slot, slotbuf, &val);
2685 (void) xtensa_operand_decode (xtensa_default_isa, opcode, opnum, &val);
2686 return val;
2687 }
2688
2689 \f
2690 /* Checks for rules from xtensa-relax tables. */
2691
2692 /* The routine xg_instruction_matches_option_term must return TRUE
2693 when a given option term is true. The meaning of all of the option
2694 terms is given interpretation by this function. This is needed when
2695 an option depends on the state of a directive, but there are no such
2696 options in use right now. */
2697
2698 static bfd_boolean
2699 xg_instruction_matches_option_term (TInsn *insn ATTRIBUTE_UNUSED,
2700 const ReqOrOption *option)
2701 {
2702 if (strcmp (option->option_name, "realnop") == 0
2703 || strncmp (option->option_name, "IsaUse", 6) == 0)
2704 {
2705 /* These conditions were evaluated statically when building the
2706 relaxation table. There's no need to reevaluate them now. */
2707 return TRUE;
2708 }
2709 else
2710 {
2711 as_fatal (_("internal error: unknown option name '%s'"),
2712 option->option_name);
2713 }
2714 }
2715
2716
2717 static bfd_boolean
2718 xg_instruction_matches_or_options (TInsn *insn,
2719 const ReqOrOptionList *or_option)
2720 {
2721 const ReqOrOption *option;
2722 /* Must match each of the AND terms. */
2723 for (option = or_option; option != NULL; option = option->next)
2724 {
2725 if (xg_instruction_matches_option_term (insn, option))
2726 return TRUE;
2727 }
2728 return FALSE;
2729 }
2730
2731
2732 static bfd_boolean
2733 xg_instruction_matches_options (TInsn *insn, const ReqOptionList *options)
2734 {
2735 const ReqOption *req_options;
2736 /* Must match each of the AND terms. */
2737 for (req_options = options;
2738 req_options != NULL;
2739 req_options = req_options->next)
2740 {
2741 /* Must match one of the OR clauses. */
2742 if (!xg_instruction_matches_or_options (insn,
2743 req_options->or_option_terms))
2744 return FALSE;
2745 }
2746 return TRUE;
2747 }
2748
2749
2750 /* Return the transition rule that matches or NULL if none matches. */
2751
2752 static bfd_boolean
2753 xg_instruction_matches_rule (TInsn *insn, TransitionRule *rule)
2754 {
2755 PreconditionList *condition_l;
2756
2757 if (rule->opcode != insn->opcode)
2758 return FALSE;
2759
2760 for (condition_l = rule->conditions;
2761 condition_l != NULL;
2762 condition_l = condition_l->next)
2763 {
2764 expressionS *exp1;
2765 expressionS *exp2;
2766 Precondition *cond = condition_l->precond;
2767
2768 switch (cond->typ)
2769 {
2770 case OP_CONSTANT:
2771 /* The expression must be the constant. */
2772 assert (cond->op_num < insn->ntok);
2773 exp1 = &insn->tok[cond->op_num];
2774 if (expr_is_const (exp1))
2775 {
2776 switch (cond->cmp)
2777 {
2778 case OP_EQUAL:
2779 if (get_expr_const (exp1) != cond->op_data)
2780 return FALSE;
2781 break;
2782 case OP_NOTEQUAL:
2783 if (get_expr_const (exp1) == cond->op_data)
2784 return FALSE;
2785 break;
2786 default:
2787 return FALSE;
2788 }
2789 }
2790 else if (expr_is_register (exp1))
2791 {
2792 switch (cond->cmp)
2793 {
2794 case OP_EQUAL:
2795 if (get_expr_register (exp1) != cond->op_data)
2796 return FALSE;
2797 break;
2798 case OP_NOTEQUAL:
2799 if (get_expr_register (exp1) == cond->op_data)
2800 return FALSE;
2801 break;
2802 default:
2803 return FALSE;
2804 }
2805 }
2806 else
2807 return FALSE;
2808 break;
2809
2810 case OP_OPERAND:
2811 assert (cond->op_num < insn->ntok);
2812 assert (cond->op_data < insn->ntok);
2813 exp1 = &insn->tok[cond->op_num];
2814 exp2 = &insn->tok[cond->op_data];
2815
2816 switch (cond->cmp)
2817 {
2818 case OP_EQUAL:
2819 if (!expr_is_equal (exp1, exp2))
2820 return FALSE;
2821 break;
2822 case OP_NOTEQUAL:
2823 if (expr_is_equal (exp1, exp2))
2824 return FALSE;
2825 break;
2826 }
2827 break;
2828
2829 case OP_LITERAL:
2830 case OP_LABEL:
2831 default:
2832 return FALSE;
2833 }
2834 }
2835 if (!xg_instruction_matches_options (insn, rule->options))
2836 return FALSE;
2837
2838 return TRUE;
2839 }
2840
2841
2842 static int
2843 transition_rule_cmp (const TransitionRule *a, const TransitionRule *b)
2844 {
2845 bfd_boolean a_greater = FALSE;
2846 bfd_boolean b_greater = FALSE;
2847
2848 ReqOptionList *l_a = a->options;
2849 ReqOptionList *l_b = b->options;
2850
2851 /* We only care if they both are the same except for
2852 a const16 vs. an l32r. */
2853
2854 while (l_a && l_b && ((l_a->next == NULL) == (l_b->next == NULL)))
2855 {
2856 ReqOrOptionList *l_or_a = l_a->or_option_terms;
2857 ReqOrOptionList *l_or_b = l_b->or_option_terms;
2858 while (l_or_a && l_or_b && ((l_a->next == NULL) == (l_b->next == NULL)))
2859 {
2860 if (l_or_a->is_true != l_or_b->is_true)
2861 return 0;
2862 if (strcmp (l_or_a->option_name, l_or_b->option_name) != 0)
2863 {
2864 /* This is the case we care about. */
2865 if (strcmp (l_or_a->option_name, "IsaUseConst16") == 0
2866 && strcmp (l_or_b->option_name, "IsaUseL32R") == 0)
2867 {
2868 if (prefer_const16)
2869 a_greater = TRUE;
2870 else
2871 b_greater = TRUE;
2872 }
2873 else if (strcmp (l_or_a->option_name, "IsaUseL32R") == 0
2874 && strcmp (l_or_b->option_name, "IsaUseConst16") == 0)
2875 {
2876 if (prefer_const16)
2877 b_greater = TRUE;
2878 else
2879 a_greater = TRUE;
2880 }
2881 else
2882 return 0;
2883 }
2884 l_or_a = l_or_a->next;
2885 l_or_b = l_or_b->next;
2886 }
2887 if (l_or_a || l_or_b)
2888 return 0;
2889
2890 l_a = l_a->next;
2891 l_b = l_b->next;
2892 }
2893 if (l_a || l_b)
2894 return 0;
2895
2896 /* Incomparable if the substitution was used differently in two cases. */
2897 if (a_greater && b_greater)
2898 return 0;
2899
2900 if (b_greater)
2901 return 1;
2902 if (a_greater)
2903 return -1;
2904
2905 return 0;
2906 }
2907
2908
2909 static TransitionRule *
2910 xg_instruction_match (TInsn *insn)
2911 {
2912 TransitionTable *table = xg_build_simplify_table (&transition_rule_cmp);
2913 TransitionList *l;
2914 assert (insn->opcode < table->num_opcodes);
2915
2916 /* Walk through all of the possible transitions. */
2917 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
2918 {
2919 TransitionRule *rule = l->rule;
2920 if (xg_instruction_matches_rule (insn, rule))
2921 return rule;
2922 }
2923 return NULL;
2924 }
2925
2926 \f
2927 /* Various Other Internal Functions. */
2928
2929 static bfd_boolean
2930 is_unique_insn_expansion (TransitionRule *r)
2931 {
2932 if (!r->to_instr || r->to_instr->next != NULL)
2933 return FALSE;
2934 if (r->to_instr->typ != INSTR_INSTR)
2935 return FALSE;
2936 return TRUE;
2937 }
2938
2939
2940 /* Check if there is exactly one relaxation for INSN that converts it to
2941 another instruction of equal or larger size. If so, and if TARG is
2942 non-null, go ahead and generate the relaxed instruction into TARG. If
2943 NARROW_ONLY is true, then only consider relaxations that widen a narrow
2944 instruction, i.e., ignore relaxations that convert to an instruction of
2945 equal size. In some contexts where this function is used, only
2946 a single widening is allowed and the NARROW_ONLY argument is used to
2947 exclude cases like ADDI being "widened" to an ADDMI, which may
2948 later be relaxed to an ADDMI/ADDI pair. */
2949
2950 bfd_boolean
2951 xg_is_single_relaxable_insn (TInsn *insn, TInsn *targ, bfd_boolean narrow_only)
2952 {
2953 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
2954 TransitionList *l;
2955 TransitionRule *match = 0;
2956
2957 assert (insn->insn_type == ITYPE_INSN);
2958 assert (insn->opcode < table->num_opcodes);
2959
2960 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
2961 {
2962 TransitionRule *rule = l->rule;
2963
2964 if (xg_instruction_matches_rule (insn, rule)
2965 && is_unique_insn_expansion (rule)
2966 && (xg_get_single_size (insn->opcode) + (narrow_only ? 1 : 0)
2967 <= xg_get_single_size (rule->to_instr->opcode)))
2968 {
2969 if (match)
2970 return FALSE;
2971 match = rule;
2972 }
2973 }
2974 if (!match)
2975 return FALSE;
2976
2977 if (targ)
2978 xg_build_to_insn (targ, insn, match->to_instr);
2979 return TRUE;
2980 }
2981
2982
2983 /* Return the maximum number of bytes this opcode can expand to. */
2984
2985 static int
2986 xg_get_max_insn_widen_size (xtensa_opcode opcode)
2987 {
2988 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
2989 TransitionList *l;
2990 int max_size = xg_get_single_size (opcode);
2991
2992 assert (opcode < table->num_opcodes);
2993
2994 for (l = table->table[opcode]; l != NULL; l = l->next)
2995 {
2996 TransitionRule *rule = l->rule;
2997 BuildInstr *build_list;
2998 int this_size = 0;
2999
3000 if (!rule)
3001 continue;
3002 build_list = rule->to_instr;
3003 if (is_unique_insn_expansion (rule))
3004 {
3005 assert (build_list->typ == INSTR_INSTR);
3006 this_size = xg_get_max_insn_widen_size (build_list->opcode);
3007 }
3008 else
3009 for (; build_list != NULL; build_list = build_list->next)
3010 {
3011 switch (build_list->typ)
3012 {
3013 case INSTR_INSTR:
3014 this_size += xg_get_single_size (build_list->opcode);
3015 break;
3016 case INSTR_LITERAL_DEF:
3017 case INSTR_LABEL_DEF:
3018 default:
3019 break;
3020 }
3021 }
3022 if (this_size > max_size)
3023 max_size = this_size;
3024 }
3025 return max_size;
3026 }
3027
3028
3029 /* Return the maximum number of literal bytes this opcode can generate. */
3030
3031 static int
3032 xg_get_max_insn_widen_literal_size (xtensa_opcode opcode)
3033 {
3034 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3035 TransitionList *l;
3036 int max_size = 0;
3037
3038 assert (opcode < table->num_opcodes);
3039
3040 for (l = table->table[opcode]; l != NULL; l = l->next)
3041 {
3042 TransitionRule *rule = l->rule;
3043 BuildInstr *build_list;
3044 int this_size = 0;
3045
3046 if (!rule)
3047 continue;
3048 build_list = rule->to_instr;
3049 if (is_unique_insn_expansion (rule))
3050 {
3051 assert (build_list->typ == INSTR_INSTR);
3052 this_size = xg_get_max_insn_widen_literal_size (build_list->opcode);
3053 }
3054 else
3055 for (; build_list != NULL; build_list = build_list->next)
3056 {
3057 switch (build_list->typ)
3058 {
3059 case INSTR_LITERAL_DEF:
3060 /* Hard-coded 4-byte literal. */
3061 this_size += 4;
3062 break;
3063 case INSTR_INSTR:
3064 case INSTR_LABEL_DEF:
3065 default:
3066 break;
3067 }
3068 }
3069 if (this_size > max_size)
3070 max_size = this_size;
3071 }
3072 return max_size;
3073 }
3074
3075
3076 static bfd_boolean
3077 xg_is_relaxable_insn (TInsn *insn, int lateral_steps)
3078 {
3079 int steps_taken = 0;
3080 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3081 TransitionList *l;
3082
3083 assert (insn->insn_type == ITYPE_INSN);
3084 assert (insn->opcode < table->num_opcodes);
3085
3086 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3087 {
3088 TransitionRule *rule = l->rule;
3089
3090 if (xg_instruction_matches_rule (insn, rule))
3091 {
3092 if (steps_taken == lateral_steps)
3093 return TRUE;
3094 steps_taken++;
3095 }
3096 }
3097 return FALSE;
3098 }
3099
3100
3101 static symbolS *
3102 get_special_literal_symbol (void)
3103 {
3104 static symbolS *sym = NULL;
3105
3106 if (sym == NULL)
3107 sym = symbol_find_or_make ("SPECIAL_LITERAL0\001");
3108 return sym;
3109 }
3110
3111
3112 static symbolS *
3113 get_special_label_symbol (void)
3114 {
3115 static symbolS *sym = NULL;
3116
3117 if (sym == NULL)
3118 sym = symbol_find_or_make ("SPECIAL_LABEL0\001");
3119 return sym;
3120 }
3121
3122
3123 static bfd_boolean
3124 xg_valid_literal_expression (const expressionS *exp)
3125 {
3126 switch (exp->X_op)
3127 {
3128 case O_constant:
3129 case O_symbol:
3130 case O_big:
3131 case O_uminus:
3132 case O_subtract:
3133 case O_pltrel:
3134 case O_pcrel:
3135 return TRUE;
3136 default:
3137 return FALSE;
3138 }
3139 }
3140
3141
3142 /* This will check to see if the value can be converted into the
3143 operand type. It will return TRUE if it does not fit. */
3144
3145 static bfd_boolean
3146 xg_check_operand (int32 value, xtensa_opcode opcode, int operand)
3147 {
3148 uint32 valbuf = value;
3149 if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf))
3150 return TRUE;
3151 return FALSE;
3152 }
3153
3154
3155 /* Assumes: All immeds are constants. Check that all constants fit
3156 into their immeds; return FALSE if not. */
3157
3158 static bfd_boolean
3159 xg_immeds_fit (const TInsn *insn)
3160 {
3161 xtensa_isa isa = xtensa_default_isa;
3162 int i;
3163
3164 int n = insn->ntok;
3165 assert (insn->insn_type == ITYPE_INSN);
3166 for (i = 0; i < n; ++i)
3167 {
3168 const expressionS *expr = &insn->tok[i];
3169 if (xtensa_operand_is_register (isa, insn->opcode, i) == 1)
3170 continue;
3171
3172 switch (expr->X_op)
3173 {
3174 case O_register:
3175 case O_constant:
3176 if (xg_check_operand (expr->X_add_number, insn->opcode, i))
3177 return FALSE;
3178 break;
3179
3180 default:
3181 /* The symbol should have a fixup associated with it. */
3182 assert (FALSE);
3183 break;
3184 }
3185 }
3186 return TRUE;
3187 }
3188
3189
3190 /* This should only be called after we have an initial
3191 estimate of the addresses. */
3192
3193 static bfd_boolean
3194 xg_symbolic_immeds_fit (const TInsn *insn,
3195 segT pc_seg,
3196 fragS *pc_frag,
3197 offsetT pc_offset,
3198 long stretch)
3199 {
3200 xtensa_isa isa = xtensa_default_isa;
3201 symbolS *symbolP;
3202 fragS *sym_frag;
3203 offsetT target, pc;
3204 uint32 new_offset;
3205 int i;
3206 int n = insn->ntok;
3207
3208 assert (insn->insn_type == ITYPE_INSN);
3209
3210 for (i = 0; i < n; ++i)
3211 {
3212 const expressionS *expr = &insn->tok[i];
3213 if (xtensa_operand_is_register (isa, insn->opcode, i) == 1)
3214 continue;
3215
3216 switch (expr->X_op)
3217 {
3218 case O_register:
3219 case O_constant:
3220 if (xg_check_operand (expr->X_add_number, insn->opcode, i))
3221 return FALSE;
3222 break;
3223
3224 case O_lo16:
3225 case O_hi16:
3226 /* Check for the worst case. */
3227 if (xg_check_operand (0xffff, insn->opcode, i))
3228 return FALSE;
3229 break;
3230
3231 case O_symbol:
3232 /* We only allow symbols for PC-relative references.
3233 If pc_frag == 0, then we don't have frag locations yet. */
3234 if (pc_frag == 0
3235 || xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 0)
3236 return FALSE;
3237
3238 /* If it is a weak symbol or a symbol in a different section,
3239 it cannot be known to fit at assembly time. */
3240 if (S_IS_WEAK (expr->X_add_symbol)
3241 || S_GET_SEGMENT (expr->X_add_symbol) != pc_seg)
3242 {
3243 /* For a direct call with --no-longcalls, be optimistic and
3244 assume it will be in range. */
3245 if (is_direct_call_opcode (insn->opcode)
3246 && ! pc_frag->tc_frag_data.use_longcalls)
3247 return TRUE;
3248
3249 return FALSE;
3250 }
3251
3252 symbolP = expr->X_add_symbol;
3253 sym_frag = symbol_get_frag (symbolP);
3254 target = S_GET_VALUE (symbolP) + expr->X_add_number;
3255 pc = pc_frag->fr_address + pc_offset;
3256
3257 /* If frag has yet to be reached on this pass, assume it
3258 will move by STRETCH just as we did. If this is not so,
3259 it will be because some frag between grows, and that will
3260 force another pass. Beware zero-length frags. There
3261 should be a faster way to do this. */
3262
3263 if (stretch != 0
3264 && sym_frag->relax_marker != pc_frag->relax_marker
3265 && S_GET_SEGMENT (symbolP) == pc_seg)
3266 {
3267 target += stretch;
3268 }
3269
3270 new_offset = target;
3271 xtensa_operand_do_reloc (isa, insn->opcode, i, &new_offset, pc);
3272 if (xg_check_operand (new_offset, insn->opcode, i))
3273 return FALSE;
3274 break;
3275
3276 default:
3277 /* The symbol should have a fixup associated with it. */
3278 return FALSE;
3279 }
3280 }
3281
3282 return TRUE;
3283 }
3284
3285
3286 /* Return TRUE on success. */
3287
3288 static bfd_boolean
3289 xg_build_to_insn (TInsn *targ, TInsn *insn, BuildInstr *bi)
3290 {
3291 BuildOp *op;
3292 symbolS *sym;
3293
3294 tinsn_init (targ);
3295 targ->debug_line = insn->debug_line;
3296 targ->loc_directive_seen = insn->loc_directive_seen;
3297 switch (bi->typ)
3298 {
3299 case INSTR_INSTR:
3300 op = bi->ops;
3301 targ->opcode = bi->opcode;
3302 targ->insn_type = ITYPE_INSN;
3303 targ->is_specific_opcode = FALSE;
3304
3305 for (; op != NULL; op = op->next)
3306 {
3307 int op_num = op->op_num;
3308 int op_data = op->op_data;
3309
3310 assert (op->op_num < MAX_INSN_ARGS);
3311
3312 if (targ->ntok <= op_num)
3313 targ->ntok = op_num + 1;
3314
3315 switch (op->typ)
3316 {
3317 case OP_CONSTANT:
3318 set_expr_const (&targ->tok[op_num], op_data);
3319 break;
3320 case OP_OPERAND:
3321 assert (op_data < insn->ntok);
3322 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3323 break;
3324 case OP_LITERAL:
3325 sym = get_special_literal_symbol ();
3326 set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
3327 break;
3328 case OP_LABEL:
3329 sym = get_special_label_symbol ();
3330 set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
3331 break;
3332 case OP_OPERAND_HI16U:
3333 case OP_OPERAND_LOW16U:
3334 assert (op_data < insn->ntok);
3335 if (expr_is_const (&insn->tok[op_data]))
3336 {
3337 long val;
3338 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3339 val = xg_apply_userdef_op_fn (op->typ,
3340 targ->tok[op_num].
3341 X_add_number);
3342 targ->tok[op_num].X_add_number = val;
3343 }
3344 else
3345 {
3346 /* For const16 we can create relocations for these. */
3347 if (targ->opcode == XTENSA_UNDEFINED
3348 || (targ->opcode != xtensa_const16_opcode))
3349 return FALSE;
3350 assert (op_data < insn->ntok);
3351 /* Need to build a O_lo16 or O_hi16. */
3352 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3353 if (targ->tok[op_num].X_op == O_symbol)
3354 {
3355 if (op->typ == OP_OPERAND_HI16U)
3356 targ->tok[op_num].X_op = O_hi16;
3357 else if (op->typ == OP_OPERAND_LOW16U)
3358 targ->tok[op_num].X_op = O_lo16;
3359 else
3360 return FALSE;
3361 }
3362 }
3363 break;
3364 default:
3365 /* currently handles:
3366 OP_OPERAND_LOW8
3367 OP_OPERAND_HI24S
3368 OP_OPERAND_F32MINUS */
3369 if (xg_has_userdef_op_fn (op->typ))
3370 {
3371 assert (op_data < insn->ntok);
3372 if (expr_is_const (&insn->tok[op_data]))
3373 {
3374 long val;
3375 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3376 val = xg_apply_userdef_op_fn (op->typ,
3377 targ->tok[op_num].
3378 X_add_number);
3379 targ->tok[op_num].X_add_number = val;
3380 }
3381 else
3382 return FALSE; /* We cannot use a relocation for this. */
3383 break;
3384 }
3385 assert (0);
3386 break;
3387 }
3388 }
3389 break;
3390
3391 case INSTR_LITERAL_DEF:
3392 op = bi->ops;
3393 targ->opcode = XTENSA_UNDEFINED;
3394 targ->insn_type = ITYPE_LITERAL;
3395 targ->is_specific_opcode = FALSE;
3396 for (; op != NULL; op = op->next)
3397 {
3398 int op_num = op->op_num;
3399 int op_data = op->op_data;
3400 assert (op->op_num < MAX_INSN_ARGS);
3401
3402 if (targ->ntok <= op_num)
3403 targ->ntok = op_num + 1;
3404
3405 switch (op->typ)
3406 {
3407 case OP_OPERAND:
3408 assert (op_data < insn->ntok);
3409 /* We can only pass resolvable literals through. */
3410 if (!xg_valid_literal_expression (&insn->tok[op_data]))
3411 return FALSE;
3412 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3413 break;
3414 case OP_LITERAL:
3415 case OP_CONSTANT:
3416 case OP_LABEL:
3417 default:
3418 assert (0);
3419 break;
3420 }
3421 }
3422 break;
3423
3424 case INSTR_LABEL_DEF:
3425 op = bi->ops;
3426 targ->opcode = XTENSA_UNDEFINED;
3427 targ->insn_type = ITYPE_LABEL;
3428 targ->is_specific_opcode = FALSE;
3429 /* Literal with no ops is a label? */
3430 assert (op == NULL);
3431 break;
3432
3433 default:
3434 assert (0);
3435 }
3436
3437 return TRUE;
3438 }
3439
3440
3441 /* Return TRUE on success. */
3442
3443 static bfd_boolean
3444 xg_build_to_stack (IStack *istack, TInsn *insn, BuildInstr *bi)
3445 {
3446 for (; bi != NULL; bi = bi->next)
3447 {
3448 TInsn *next_insn = istack_push_space (istack);
3449
3450 if (!xg_build_to_insn (next_insn, insn, bi))
3451 return FALSE;
3452 }
3453 return TRUE;
3454 }
3455
3456
3457 /* Return TRUE on valid expansion. */
3458
3459 static bfd_boolean
3460 xg_expand_to_stack (IStack *istack, TInsn *insn, int lateral_steps)
3461 {
3462 int stack_size = istack->ninsn;
3463 int steps_taken = 0;
3464 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3465 TransitionList *l;
3466
3467 assert (insn->insn_type == ITYPE_INSN);
3468 assert (insn->opcode < table->num_opcodes);
3469
3470 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3471 {
3472 TransitionRule *rule = l->rule;
3473
3474 if (xg_instruction_matches_rule (insn, rule))
3475 {
3476 if (lateral_steps == steps_taken)
3477 {
3478 int i;
3479
3480 /* This is it. Expand the rule to the stack. */
3481 if (!xg_build_to_stack (istack, insn, rule->to_instr))
3482 return FALSE;
3483
3484 /* Check to see if it fits. */
3485 for (i = stack_size; i < istack->ninsn; i++)
3486 {
3487 TInsn *insn = &istack->insn[i];
3488
3489 if (insn->insn_type == ITYPE_INSN
3490 && !tinsn_has_symbolic_operands (insn)
3491 && !xg_immeds_fit (insn))
3492 {
3493 istack->ninsn = stack_size;
3494 return FALSE;
3495 }
3496 }
3497 return TRUE;
3498 }
3499 steps_taken++;
3500 }
3501 }
3502 return FALSE;
3503 }
3504
3505 \f
3506 /* Relax the assembly instruction at least "min_steps".
3507 Return the number of steps taken.
3508
3509 For relaxation to correctly terminate, every relaxation chain must
3510 terminate in one of two ways:
3511
3512 1. If the chain from one instruction to the next consists entirely of
3513 single instructions, then the chain *must* handle all possible
3514 immediates without failing. It must not ever fail because an
3515 immediate is out of range. The MOVI.N -> MOVI -> L32R relaxation
3516 chain is one example. L32R loads 32 bits, and there cannot be an
3517 immediate larger than 32 bits, so it satisfies this condition.
3518 Single instruction relaxation chains are as defined by
3519 xg_is_single_relaxable_instruction.
3520
3521 2. Otherwise, the chain must end in a multi-instruction expansion: e.g.,
3522 BNEZ.N -> BNEZ -> BNEZ.W15 -> BENZ.N/J
3523
3524 Strictly speaking, in most cases you can violate condition 1 and be OK
3525 -- in particular when the last two instructions have the same single
3526 size. But nevertheless, you should guarantee the above two conditions.
3527
3528 We could fix this so that single-instruction expansions correctly
3529 terminate when they can't handle the range, but the error messages are
3530 worse, and it actually turns out that in every case but one (18-bit wide
3531 branches), you need a multi-instruction expansion to get the full range
3532 anyway. And because 18-bit branches are handled identically to 15-bit
3533 branches, there isn't any point in changing it. */
3534
3535 static int
3536 xg_assembly_relax (IStack *istack,
3537 TInsn *insn,
3538 segT pc_seg,
3539 fragS *pc_frag, /* if pc_frag == 0, not pc-relative */
3540 offsetT pc_offset, /* offset in fragment */
3541 int min_steps, /* minimum conversion steps */
3542 long stretch) /* number of bytes stretched so far */
3543 {
3544 int steps_taken = 0;
3545
3546 /* Some of its immeds don't fit. Try to build a relaxed version.
3547 This may go through a couple of stages of single instruction
3548 transformations before we get there. */
3549
3550 TInsn single_target;
3551 TInsn current_insn;
3552 int lateral_steps = 0;
3553 int istack_size = istack->ninsn;
3554
3555 if (xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch)
3556 && steps_taken >= min_steps)
3557 {
3558 istack_push (istack, insn);
3559 return steps_taken;
3560 }
3561 current_insn = *insn;
3562
3563 /* Walk through all of the single instruction expansions. */
3564 while (xg_is_single_relaxable_insn (&current_insn, &single_target, FALSE))
3565 {
3566 steps_taken++;
3567 if (xg_symbolic_immeds_fit (&single_target, pc_seg, pc_frag, pc_offset,
3568 stretch))
3569 {
3570 if (steps_taken >= min_steps)
3571 {
3572 istack_push (istack, &single_target);
3573 return steps_taken;
3574 }
3575 }
3576 current_insn = single_target;
3577 }
3578
3579 /* Now check for a multi-instruction expansion. */
3580 while (xg_is_relaxable_insn (&current_insn, lateral_steps))
3581 {
3582 if (xg_symbolic_immeds_fit (&current_insn, pc_seg, pc_frag, pc_offset,
3583 stretch))
3584 {
3585 if (steps_taken >= min_steps)
3586 {
3587 istack_push (istack, &current_insn);
3588 return steps_taken;
3589 }
3590 }
3591 steps_taken++;
3592 if (xg_expand_to_stack (istack, &current_insn, lateral_steps))
3593 {
3594 if (steps_taken >= min_steps)
3595 return steps_taken;
3596 }
3597 lateral_steps++;
3598 istack->ninsn = istack_size;
3599 }
3600
3601 /* It's not going to work -- use the original. */
3602 istack_push (istack, insn);
3603 return steps_taken;
3604 }
3605
3606
3607 static void
3608 xg_finish_frag (char *last_insn,
3609 enum xtensa_relax_statesE frag_state,
3610 enum xtensa_relax_statesE slot0_state,
3611 int max_growth,
3612 bfd_boolean is_insn)
3613 {
3614 /* Finish off this fragment so that it has at LEAST the desired
3615 max_growth. If it doesn't fit in this fragment, close this one
3616 and start a new one. In either case, return a pointer to the
3617 beginning of the growth area. */
3618
3619 fragS *old_frag;
3620
3621 frag_grow (max_growth);
3622 old_frag = frag_now;
3623
3624 frag_now->fr_opcode = last_insn;
3625 if (is_insn)
3626 frag_now->tc_frag_data.is_insn = TRUE;
3627
3628 frag_var (rs_machine_dependent, max_growth, max_growth,
3629 frag_state, frag_now->fr_symbol, frag_now->fr_offset, last_insn);
3630
3631 old_frag->tc_frag_data.slot_subtypes[0] = slot0_state;
3632 xtensa_set_frag_assembly_state (frag_now);
3633
3634 /* Just to make sure that we did not split it up. */
3635 assert (old_frag->fr_next == frag_now);
3636 }
3637
3638
3639 /* Return TRUE if the target frag is one of the next non-empty frags. */
3640
3641 static bfd_boolean
3642 is_next_frag_target (const fragS *fragP, const fragS *target)
3643 {
3644 if (fragP == NULL)
3645 return FALSE;
3646
3647 for (; fragP; fragP = fragP->fr_next)
3648 {
3649 if (fragP == target)
3650 return TRUE;
3651 if (fragP->fr_fix != 0)
3652 return FALSE;
3653 if (fragP->fr_type == rs_fill && fragP->fr_offset != 0)
3654 return FALSE;
3655 if ((fragP->fr_type == rs_align || fragP->fr_type == rs_align_code)
3656 && ((fragP->fr_address % (1 << fragP->fr_offset)) != 0))
3657 return FALSE;
3658 if (fragP->fr_type == rs_space)
3659 return FALSE;
3660 }
3661 return FALSE;
3662 }
3663
3664
3665 static bfd_boolean
3666 is_branch_jmp_to_next (TInsn *insn, fragS *fragP)
3667 {
3668 xtensa_isa isa = xtensa_default_isa;
3669 int i;
3670 int num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
3671 int target_op = -1;
3672 symbolS *sym;
3673 fragS *target_frag;
3674
3675 if (xtensa_opcode_is_branch (isa, insn->opcode) != 1
3676 && xtensa_opcode_is_jump (isa, insn->opcode) != 1)
3677 return FALSE;
3678
3679 for (i = 0; i < num_ops; i++)
3680 {
3681 if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1)
3682 {
3683 target_op = i;
3684 break;
3685 }
3686 }
3687 if (target_op == -1)
3688 return FALSE;
3689
3690 if (insn->ntok <= target_op)
3691 return FALSE;
3692
3693 if (insn->tok[target_op].X_op != O_symbol)
3694 return FALSE;
3695
3696 sym = insn->tok[target_op].X_add_symbol;
3697 if (sym == NULL)
3698 return FALSE;
3699
3700 if (insn->tok[target_op].X_add_number != 0)
3701 return FALSE;
3702
3703 target_frag = symbol_get_frag (sym);
3704 if (target_frag == NULL)
3705 return FALSE;
3706
3707 if (is_next_frag_target (fragP->fr_next, target_frag)
3708 && S_GET_VALUE (sym) == target_frag->fr_address)
3709 return TRUE;
3710
3711 return FALSE;
3712 }
3713
3714
3715 static void
3716 xg_add_branch_and_loop_targets (TInsn *insn)
3717 {
3718 xtensa_isa isa = xtensa_default_isa;
3719 int num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
3720
3721 if (xtensa_opcode_is_loop (isa, insn->opcode) == 1)
3722 {
3723 int i = 1;
3724 if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1
3725 && insn->tok[i].X_op == O_symbol)
3726 symbol_get_tc (insn->tok[i].X_add_symbol)->is_loop_target = TRUE;
3727 return;
3728 }
3729
3730 if (xtensa_opcode_is_branch (isa, insn->opcode) == 1
3731 || xtensa_opcode_is_loop (isa, insn->opcode) == 1)
3732 {
3733 int i;
3734
3735 for (i = 0; i < insn->ntok && i < num_ops; i++)
3736 {
3737 if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1
3738 && insn->tok[i].X_op == O_symbol)
3739 {
3740 symbolS *sym = insn->tok[i].X_add_symbol;
3741 symbol_get_tc (sym)->is_branch_target = TRUE;
3742 if (S_IS_DEFINED (sym))
3743 symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
3744 }
3745 }
3746 }
3747 }
3748
3749
3750 /* Return FALSE if no error. */
3751
3752 static bfd_boolean
3753 xg_build_token_insn (BuildInstr *instr_spec, TInsn *old_insn, TInsn *new_insn)
3754 {
3755 int num_ops = 0;
3756 BuildOp *b_op;
3757
3758 switch (instr_spec->typ)
3759 {
3760 case INSTR_INSTR:
3761 new_insn->insn_type = ITYPE_INSN;
3762 new_insn->opcode = instr_spec->opcode;
3763 break;
3764 case INSTR_LITERAL_DEF:
3765 new_insn->insn_type = ITYPE_LITERAL;
3766 new_insn->opcode = XTENSA_UNDEFINED;
3767 break;
3768 case INSTR_LABEL_DEF:
3769 abort ();
3770 }
3771 new_insn->is_specific_opcode = FALSE;
3772 new_insn->debug_line = old_insn->debug_line;
3773 new_insn->loc_directive_seen = old_insn->loc_directive_seen;
3774
3775 for (b_op = instr_spec->ops; b_op != NULL; b_op = b_op->next)
3776 {
3777 expressionS *exp;
3778 const expressionS *src_exp;
3779
3780 num_ops++;
3781 switch (b_op->typ)
3782 {
3783 case OP_CONSTANT:
3784 /* The expression must be the constant. */
3785 assert (b_op->op_num < MAX_INSN_ARGS);
3786 exp = &new_insn->tok[b_op->op_num];
3787 set_expr_const (exp, b_op->op_data);
3788 break;
3789
3790 case OP_OPERAND:
3791 assert (b_op->op_num < MAX_INSN_ARGS);
3792 assert (b_op->op_data < (unsigned) old_insn->ntok);
3793 src_exp = &old_insn->tok[b_op->op_data];
3794 exp = &new_insn->tok[b_op->op_num];
3795 copy_expr (exp, src_exp);
3796 break;
3797
3798 case OP_LITERAL:
3799 case OP_LABEL:
3800 as_bad (_("can't handle generation of literal/labels yet"));
3801 assert (0);
3802
3803 default:
3804 as_bad (_("can't handle undefined OP TYPE"));
3805 assert (0);
3806 }
3807 }
3808
3809 new_insn->ntok = num_ops;
3810 return FALSE;
3811 }
3812
3813
3814 /* Return TRUE if it was simplified. */
3815
3816 static bfd_boolean
3817 xg_simplify_insn (TInsn *old_insn, TInsn *new_insn)
3818 {
3819 TransitionRule *rule;
3820 BuildInstr *insn_spec;
3821
3822 if (old_insn->is_specific_opcode || !density_supported)
3823 return FALSE;
3824
3825 rule = xg_instruction_match (old_insn);
3826 if (rule == NULL)
3827 return FALSE;
3828
3829 insn_spec = rule->to_instr;
3830 /* There should only be one. */
3831 assert (insn_spec != NULL);
3832 assert (insn_spec->next == NULL);
3833 if (insn_spec->next != NULL)
3834 return FALSE;
3835
3836 xg_build_token_insn (insn_spec, old_insn, new_insn);
3837
3838 return TRUE;
3839 }
3840
3841
3842 /* xg_expand_assembly_insn: (1) Simplify the instruction, i.e., l32i ->
3843 l32i.n. (2) Check the number of operands. (3) Place the instruction
3844 tokens into the stack or relax it and place multiple
3845 instructions/literals onto the stack. Return FALSE if no error. */
3846
3847 static bfd_boolean
3848 xg_expand_assembly_insn (IStack *istack, TInsn *orig_insn)
3849 {
3850 int noperands;
3851 TInsn new_insn;
3852 bfd_boolean do_expand;
3853
3854 tinsn_init (&new_insn);
3855
3856 /* Narrow it if we can. xg_simplify_insn now does all the
3857 appropriate checking (e.g., for the density option). */
3858 if (xg_simplify_insn (orig_insn, &new_insn))
3859 orig_insn = &new_insn;
3860
3861 noperands = xtensa_opcode_num_operands (xtensa_default_isa,
3862 orig_insn->opcode);
3863 if (orig_insn->ntok < noperands)
3864 {
3865 as_bad (_("found %d operands for '%s': Expected %d"),
3866 orig_insn->ntok,
3867 xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
3868 noperands);
3869 return TRUE;
3870 }
3871 if (orig_insn->ntok > noperands)
3872 as_warn (_("found too many (%d) operands for '%s': Expected %d"),
3873 orig_insn->ntok,
3874 xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
3875 noperands);
3876
3877 /* If there are not enough operands, we will assert above. If there
3878 are too many, just cut out the extras here. */
3879 orig_insn->ntok = noperands;
3880
3881 if (tinsn_has_invalid_symbolic_operands (orig_insn))
3882 return TRUE;
3883
3884 /* Special case for extui opcode which has constraints not handled
3885 by the ordinary operand encoding checks. The number of operands
3886 and related syntax issues have already been checked. */
3887 if (orig_insn->opcode == xtensa_extui_opcode)
3888 {
3889 int shiftimm = orig_insn->tok[2].X_add_number;
3890 int maskimm = orig_insn->tok[3].X_add_number;
3891 if (shiftimm + maskimm > 32)
3892 {
3893 as_bad (_("immediate operands sum to greater than 32"));
3894 return TRUE;
3895 }
3896 }
3897
3898 /* If the instruction will definitely need to be relaxed, it is better
3899 to expand it now for better scheduling. Decide whether to expand
3900 now.... */
3901 do_expand = (!orig_insn->is_specific_opcode && use_transform ());
3902
3903 /* Calls should be expanded to longcalls only in the backend relaxation
3904 so that the assembly scheduler will keep the L32R/CALLX instructions
3905 adjacent. */
3906 if (is_direct_call_opcode (orig_insn->opcode))
3907 do_expand = FALSE;
3908
3909 if (tinsn_has_symbolic_operands (orig_insn))
3910 {
3911 /* The values of symbolic operands are not known yet, so only expand
3912 now if an operand is "complex" (e.g., difference of symbols) and
3913 will have to be stored as a literal regardless of the value. */
3914 if (!tinsn_has_complex_operands (orig_insn))
3915 do_expand = FALSE;
3916 }
3917 else if (xg_immeds_fit (orig_insn))
3918 do_expand = FALSE;
3919
3920 if (do_expand)
3921 xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0);
3922 else
3923 istack_push (istack, orig_insn);
3924
3925 return FALSE;
3926 }
3927
3928
3929 /* Return TRUE if the section flags are marked linkonce
3930 or the name is .gnu.linkonce.*. */
3931
3932 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
3933
3934 static bfd_boolean
3935 get_is_linkonce_section (bfd *abfd ATTRIBUTE_UNUSED, segT sec)
3936 {
3937 flagword flags, link_once_flags;
3938
3939 flags = bfd_get_section_flags (abfd, sec);
3940 link_once_flags = (flags & SEC_LINK_ONCE);
3941
3942 /* Flags might not be set yet. */
3943 if (!link_once_flags
3944 && strncmp (segment_name (sec), ".gnu.linkonce.", linkonce_len) == 0)
3945 link_once_flags = SEC_LINK_ONCE;
3946
3947 return (link_once_flags != 0);
3948 }
3949
3950
3951 static void
3952 xtensa_add_literal_sym (symbolS *sym)
3953 {
3954 sym_list *l;
3955
3956 l = (sym_list *) xmalloc (sizeof (sym_list));
3957 l->sym = sym;
3958 l->next = literal_syms;
3959 literal_syms = l;
3960 }
3961
3962
3963 static symbolS *
3964 xtensa_create_literal_symbol (segT sec, fragS *frag)
3965 {
3966 static int lit_num = 0;
3967 static char name[256];
3968 symbolS *symbolP;
3969
3970 sprintf (name, ".L_lit_sym%d", lit_num);
3971
3972 /* Create a local symbol. If it is in a linkonce section, we have to
3973 be careful to make sure that if it is used in a relocation that the
3974 symbol will be in the output file. */
3975 if (get_is_linkonce_section (stdoutput, sec))
3976 {
3977 symbolP = symbol_new (name, sec, 0, frag);
3978 S_CLEAR_EXTERNAL (symbolP);
3979 /* symbolP->local = 1; */
3980 }
3981 else
3982 symbolP = symbol_new (name, sec, 0, frag);
3983
3984 xtensa_add_literal_sym (symbolP);
3985
3986 lit_num++;
3987 return symbolP;
3988 }
3989
3990
3991 /* Currently all literals that are generated here are 32-bit L32R targets. */
3992
3993 static symbolS *
3994 xg_assemble_literal (/* const */ TInsn *insn)
3995 {
3996 emit_state state;
3997 symbolS *lit_sym = NULL;
3998 bfd_reloc_code_real_type reloc;
3999 bfd_boolean pcrel = FALSE;
4000 char *p;
4001
4002 /* size = 4 for L32R. It could easily be larger when we move to
4003 larger constants. Add a parameter later. */
4004 offsetT litsize = 4;
4005 offsetT litalign = 2; /* 2^2 = 4 */
4006 expressionS saved_loc;
4007 expressionS * emit_val;
4008
4009 set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ());
4010
4011 assert (insn->insn_type == ITYPE_LITERAL);
4012 assert (insn->ntok == 1); /* must be only one token here */
4013
4014 xtensa_switch_to_literal_fragment (&state);
4015
4016 emit_val = &insn->tok[0];
4017 if (emit_val->X_op == O_big)
4018 {
4019 int size = emit_val->X_add_number * CHARS_PER_LITTLENUM;
4020 if (size > litsize)
4021 {
4022 /* This happens when someone writes a "movi a2, big_number". */
4023 as_bad_where (frag_now->fr_file, frag_now->fr_line,
4024 _("invalid immediate"));
4025 xtensa_restore_emit_state (&state);
4026 return NULL;
4027 }
4028 }
4029
4030 /* Force a 4-byte align here. Note that this opens a new frag, so all
4031 literals done with this function have a frag to themselves. That's
4032 important for the way text section literals work. */
4033 frag_align (litalign, 0, 0);
4034 record_alignment (now_seg, litalign);
4035
4036 switch (emit_val->X_op)
4037 {
4038 case O_pcrel:
4039 pcrel = TRUE;
4040 /* fall through */
4041 case O_pltrel:
4042 p = frag_more (litsize);
4043 xtensa_set_frag_assembly_state (frag_now);
4044 reloc = map_operator_to_reloc (emit_val->X_op);
4045 if (emit_val->X_add_symbol)
4046 emit_val->X_op = O_symbol;
4047 else
4048 emit_val->X_op = O_constant;
4049 fix_new_exp (frag_now, p - frag_now->fr_literal,
4050 litsize, emit_val, pcrel, reloc);
4051 break;
4052
4053 default:
4054 emit_expr (emit_val, litsize);
4055 break;
4056 }
4057
4058 assert (frag_now->tc_frag_data.literal_frag == NULL);
4059 frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
4060 frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
4061 lit_sym = frag_now->fr_symbol;
4062
4063 /* Go back. */
4064 xtensa_restore_emit_state (&state);
4065 return lit_sym;
4066 }
4067
4068
4069 static void
4070 xg_assemble_literal_space (/* const */ int size, int slot)
4071 {
4072 emit_state state;
4073 /* We might have to do something about this alignment. It only
4074 takes effect if something is placed here. */
4075 offsetT litalign = 2; /* 2^2 = 4 */
4076 fragS *lit_saved_frag;
4077
4078 assert (size % 4 == 0);
4079
4080 xtensa_switch_to_literal_fragment (&state);
4081
4082 /* Force a 4-byte align here. */
4083 frag_align (litalign, 0, 0);
4084 record_alignment (now_seg, litalign);
4085
4086 frag_grow (size);
4087
4088 lit_saved_frag = frag_now;
4089 frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
4090 frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
4091 xg_finish_frag (0, RELAX_LITERAL, 0, size, FALSE);
4092
4093 /* Go back. */
4094 xtensa_restore_emit_state (&state);
4095 frag_now->tc_frag_data.literal_frags[slot] = lit_saved_frag;
4096 }
4097
4098
4099 /* Put in a fixup record based on the opcode.
4100 Return TRUE on success. */
4101
4102 static bfd_boolean
4103 xg_add_opcode_fix (TInsn *tinsn,
4104 int opnum,
4105 xtensa_format fmt,
4106 int slot,
4107 expressionS *expr,
4108 fragS *fragP,
4109 offsetT offset)
4110 {
4111 xtensa_opcode opcode = tinsn->opcode;
4112 bfd_reloc_code_real_type reloc;
4113 reloc_howto_type *howto;
4114 int fmt_length;
4115 fixS *the_fix;
4116
4117 reloc = BFD_RELOC_NONE;
4118
4119 /* First try the special cases for "alternate" relocs. */
4120 if (opcode == xtensa_l32r_opcode)
4121 {
4122 if (fragP->tc_frag_data.use_absolute_literals)
4123 reloc = encode_alt_reloc (slot);
4124 }
4125 else if (opcode == xtensa_const16_opcode)
4126 {
4127 if (expr->X_op == O_lo16)
4128 {
4129 reloc = encode_reloc (slot);
4130 expr->X_op = O_symbol;
4131 }
4132 else if (expr->X_op == O_hi16)
4133 {
4134 reloc = encode_alt_reloc (slot);
4135 expr->X_op = O_symbol;
4136 }
4137 }
4138
4139 if (opnum != get_relaxable_immed (opcode))
4140 {
4141 as_bad (_("invalid relocation for operand %i of '%s'"),
4142 opnum + 1, xtensa_opcode_name (xtensa_default_isa, opcode));
4143 return FALSE;
4144 }
4145
4146 /* Handle erroneous "@h" and "@l" expressions here before they propagate
4147 into the symbol table where the generic portions of the assembler
4148 won't know what to do with them. */
4149 if (expr->X_op == O_lo16 || expr->X_op == O_hi16)
4150 {
4151 as_bad (_("invalid expression for operand %i of '%s'"),
4152 opnum + 1, xtensa_opcode_name (xtensa_default_isa, opcode));
4153 return FALSE;
4154 }
4155
4156 /* Next try the generic relocs. */
4157 if (reloc == BFD_RELOC_NONE)
4158 reloc = encode_reloc (slot);
4159 if (reloc == BFD_RELOC_NONE)
4160 {
4161 as_bad (_("invalid relocation in instruction slot %i"), slot);
4162 return FALSE;
4163 }
4164
4165 howto = bfd_reloc_type_lookup (stdoutput, reloc);
4166 if (!howto)
4167 {
4168 as_bad (_("undefined symbol for opcode \"%s\""),
4169 xtensa_opcode_name (xtensa_default_isa, opcode));
4170 return FALSE;
4171 }
4172
4173 fmt_length = xtensa_format_length (xtensa_default_isa, fmt);
4174 the_fix = fix_new_exp (fragP, offset, fmt_length, expr,
4175 howto->pc_relative, reloc);
4176 the_fix->fx_no_overflow = 1;
4177 the_fix->tc_fix_data.X_add_symbol = expr->X_add_symbol;
4178 the_fix->tc_fix_data.X_add_number = expr->X_add_number;
4179 the_fix->tc_fix_data.slot = slot;
4180
4181 return TRUE;
4182 }
4183
4184
4185 static bfd_boolean
4186 xg_emit_insn_to_buf (TInsn *tinsn,
4187 char *buf,
4188 fragS *fragP,
4189 offsetT offset,
4190 bfd_boolean build_fix)
4191 {
4192 static xtensa_insnbuf insnbuf = NULL;
4193 bfd_boolean has_symbolic_immed = FALSE;
4194 bfd_boolean ok = TRUE;
4195
4196 if (!insnbuf)
4197 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4198
4199 has_symbolic_immed = tinsn_to_insnbuf (tinsn, insnbuf);
4200 if (has_symbolic_immed && build_fix)
4201 {
4202 /* Add a fixup. */
4203 xtensa_format fmt = xg_get_single_format (tinsn->opcode);
4204 int slot = xg_get_single_slot (tinsn->opcode);
4205 int opnum = get_relaxable_immed (tinsn->opcode);
4206 expressionS *exp = &tinsn->tok[opnum];
4207
4208 if (!xg_add_opcode_fix (tinsn, opnum, fmt, slot, exp, fragP, offset))
4209 ok = FALSE;
4210 }
4211 fragP->tc_frag_data.is_insn = TRUE;
4212 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf,
4213 (unsigned char *) buf, 0);
4214 return ok;
4215 }
4216
4217
4218 static void
4219 xg_resolve_literals (TInsn *insn, symbolS *lit_sym)
4220 {
4221 symbolS *sym = get_special_literal_symbol ();
4222 int i;
4223 if (lit_sym == 0)
4224 return;
4225 assert (insn->insn_type == ITYPE_INSN);
4226 for (i = 0; i < insn->ntok; i++)
4227 if (insn->tok[i].X_add_symbol == sym)
4228 insn->tok[i].X_add_symbol = lit_sym;
4229
4230 }
4231
4232
4233 static void
4234 xg_resolve_labels (TInsn *insn, symbolS *label_sym)
4235 {
4236 symbolS *sym = get_special_label_symbol ();
4237 int i;
4238 for (i = 0; i < insn->ntok; i++)
4239 if (insn->tok[i].X_add_symbol == sym)
4240 insn->tok[i].X_add_symbol = label_sym;
4241
4242 }
4243
4244
4245 /* Return TRUE if the instruction can write to the specified
4246 integer register. */
4247
4248 static bfd_boolean
4249 is_register_writer (const TInsn *insn, const char *regset, int regnum)
4250 {
4251 int i;
4252 int num_ops;
4253 xtensa_isa isa = xtensa_default_isa;
4254
4255 num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
4256
4257 for (i = 0; i < num_ops; i++)
4258 {
4259 char inout;
4260 inout = xtensa_operand_inout (isa, insn->opcode, i);
4261 if ((inout == 'o' || inout == 'm')
4262 && xtensa_operand_is_register (isa, insn->opcode, i) == 1)
4263 {
4264 xtensa_regfile opnd_rf =
4265 xtensa_operand_regfile (isa, insn->opcode, i);
4266 if (!strcmp (xtensa_regfile_shortname (isa, opnd_rf), regset))
4267 {
4268 if ((insn->tok[i].X_op == O_register)
4269 && (insn->tok[i].X_add_number == regnum))
4270 return TRUE;
4271 }
4272 }
4273 }
4274 return FALSE;
4275 }
4276
4277
4278 static bfd_boolean
4279 is_bad_loopend_opcode (const TInsn *tinsn)
4280 {
4281 xtensa_opcode opcode = tinsn->opcode;
4282
4283 if (opcode == XTENSA_UNDEFINED)
4284 return FALSE;
4285
4286 if (opcode == xtensa_call0_opcode
4287 || opcode == xtensa_callx0_opcode
4288 || opcode == xtensa_call4_opcode
4289 || opcode == xtensa_callx4_opcode
4290 || opcode == xtensa_call8_opcode
4291 || opcode == xtensa_callx8_opcode
4292 || opcode == xtensa_call12_opcode
4293 || opcode == xtensa_callx12_opcode
4294 || opcode == xtensa_isync_opcode
4295 || opcode == xtensa_ret_opcode
4296 || opcode == xtensa_ret_n_opcode
4297 || opcode == xtensa_retw_opcode
4298 || opcode == xtensa_retw_n_opcode
4299 || opcode == xtensa_waiti_opcode
4300 || opcode == xtensa_rsr_lcount_opcode)
4301 return TRUE;
4302
4303 return FALSE;
4304 }
4305
4306
4307 /* Labels that begin with ".Ln" or ".LM" are unaligned.
4308 This allows the debugger to add unaligned labels.
4309 Also, the assembler generates stabs labels that need
4310 not be aligned: FAKE_LABEL_NAME . {"F", "L", "endfunc"}. */
4311
4312 static bfd_boolean
4313 is_unaligned_label (symbolS *sym)
4314 {
4315 const char *name = S_GET_NAME (sym);
4316 static size_t fake_size = 0;
4317
4318 if (name
4319 && name[0] == '.'
4320 && name[1] == 'L' && (name[2] == 'n' || name[2] == 'M'))
4321 return TRUE;
4322
4323 /* FAKE_LABEL_NAME followed by "F", "L" or "endfunc" */
4324 if (fake_size == 0)
4325 fake_size = strlen (FAKE_LABEL_NAME);
4326
4327 if (name
4328 && strncmp (FAKE_LABEL_NAME, name, fake_size) == 0
4329 && (name[fake_size] == 'F'
4330 || name[fake_size] == 'L'
4331 || (name[fake_size] == 'e'
4332 && strncmp ("endfunc", name+fake_size, 7) == 0)))
4333 return TRUE;
4334
4335 return FALSE;
4336 }
4337
4338
4339 static fragS *
4340 next_non_empty_frag (const fragS *fragP)
4341 {
4342 fragS *next_fragP = fragP->fr_next;
4343
4344 /* Sometimes an empty will end up here due storage allocation issues.
4345 So we have to skip until we find something legit. */
4346 while (next_fragP && next_fragP->fr_fix == 0)
4347 next_fragP = next_fragP->fr_next;
4348
4349 if (next_fragP == NULL || next_fragP->fr_fix == 0)
4350 return NULL;
4351
4352 return next_fragP;
4353 }
4354
4355
4356 static bfd_boolean
4357 next_frag_opcode_is_loop (const fragS *fragP, xtensa_opcode *opcode)
4358 {
4359 xtensa_opcode out_opcode;
4360 const fragS *next_fragP = next_non_empty_frag (fragP);
4361
4362 if (next_fragP == NULL)
4363 return FALSE;
4364
4365 out_opcode = get_opcode_from_buf (next_fragP->fr_literal, 0);
4366 if (xtensa_opcode_is_loop (xtensa_default_isa, out_opcode) == 1)
4367 {
4368 *opcode = out_opcode;
4369 return TRUE;
4370 }
4371 return FALSE;
4372 }
4373
4374
4375 static int
4376 frag_format_size (const fragS *fragP)
4377 {
4378 static xtensa_insnbuf insnbuf = NULL;
4379 xtensa_isa isa = xtensa_default_isa;
4380 xtensa_format fmt;
4381 int fmt_size;
4382
4383 if (!insnbuf)
4384 insnbuf = xtensa_insnbuf_alloc (isa);
4385
4386 if (fragP == NULL)
4387 return XTENSA_UNDEFINED;
4388
4389 xtensa_insnbuf_from_chars (isa, insnbuf,
4390 (unsigned char *) fragP->fr_literal, 0);
4391
4392 fmt = xtensa_format_decode (isa, insnbuf);
4393 if (fmt == XTENSA_UNDEFINED)
4394 return XTENSA_UNDEFINED;
4395 fmt_size = xtensa_format_length (isa, fmt);
4396
4397 /* If the next format won't be changing due to relaxation, just
4398 return the length of the first format. */
4399 if (fragP->fr_opcode != fragP->fr_literal)
4400 return fmt_size;
4401
4402 /* If during relaxation we have to pull an instruction out of a
4403 multi-slot instruction, we will return the more conservative
4404 number. This works because alignment on bigger instructions
4405 is more restrictive than alignment on smaller instructions.
4406 This is more conservative than we would like, but it happens
4407 infrequently. */
4408
4409 if (xtensa_format_num_slots (xtensa_default_isa, fmt) > 1)
4410 return fmt_size;
4411
4412 /* If we aren't doing one of our own relaxations or it isn't
4413 slot-based, then the insn size won't change. */
4414 if (fragP->fr_type != rs_machine_dependent)
4415 return fmt_size;
4416 if (fragP->fr_subtype != RELAX_SLOTS)
4417 return fmt_size;
4418
4419 /* If an instruction is about to grow, return the longer size. */
4420 if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP1
4421 || fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP2
4422 || fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP3)
4423 {
4424 /* For most frags at RELAX_IMMED_STEPX, with X > 0, the first
4425 instruction in the relaxed version is of length 3. (The case
4426 where we have to pull the instruction out of a FLIX bundle
4427 is handled conservatively above.) However, frags with opcodes
4428 that are expanding to wide branches end up having formats that
4429 are not determinable by the RELAX_IMMED_STEPX enumeration, and
4430 we can't tell directly what format the relaxer picked. This
4431 is a wart in the design of the relaxer that should someday be
4432 fixed, but would require major changes, or at least should
4433 be accompanied by major changes to make use of that data.
4434
4435 In any event, we can tell that we are expanding from a single-slot
4436 three-byte format to a wider one with the logic below. */
4437
4438 if (fmt_size <= 3 && fragP->tc_frag_data.text_expansion[0] != 3)
4439 return 3 + fragP->tc_frag_data.text_expansion[0];
4440 else
4441 return 3;
4442 }
4443
4444 if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
4445 return 2 + fragP->tc_frag_data.text_expansion[0];
4446
4447 return fmt_size;
4448 }
4449
4450
4451 static int
4452 next_frag_format_size (const fragS *fragP)
4453 {
4454 const fragS *next_fragP = next_non_empty_frag (fragP);
4455 return frag_format_size (next_fragP);
4456 }
4457
4458
4459 /* In early Xtensa Processors, for reasons that are unclear, the ISA
4460 required two-byte instructions to be treated as three-byte instructions
4461 for loop instruction alignment. This restriction was removed beginning
4462 with Xtensa LX. Now the only requirement on loop instruction alignment
4463 is that the first instruction of the loop must appear at an address that
4464 does not cross a fetch boundary. */
4465
4466 static int
4467 get_loop_align_size (int insn_size)
4468 {
4469 if (insn_size == XTENSA_UNDEFINED)
4470 return xtensa_fetch_width;
4471
4472 if (enforce_three_byte_loop_align && insn_size == 2)
4473 return 3;
4474
4475 return insn_size;
4476 }
4477
4478
4479 /* If the next legit fragment is an end-of-loop marker,
4480 switch its state so it will instantiate a NOP. */
4481
4482 static void
4483 update_next_frag_state (fragS *fragP)
4484 {
4485 fragS *next_fragP = fragP->fr_next;
4486 fragS *new_target = NULL;
4487
4488 if (align_targets)
4489 {
4490 /* We are guaranteed there will be one of these... */
4491 while (!(next_fragP->fr_type == rs_machine_dependent
4492 && (next_fragP->fr_subtype == RELAX_MAYBE_UNREACHABLE
4493 || next_fragP->fr_subtype == RELAX_UNREACHABLE)))
4494 next_fragP = next_fragP->fr_next;
4495
4496 assert (next_fragP->fr_type == rs_machine_dependent
4497 && (next_fragP->fr_subtype == RELAX_MAYBE_UNREACHABLE
4498 || next_fragP->fr_subtype == RELAX_UNREACHABLE));
4499
4500 /* ...and one of these. */
4501 new_target = next_fragP->fr_next;
4502 while (!(new_target->fr_type == rs_machine_dependent
4503 && (new_target->fr_subtype == RELAX_MAYBE_DESIRE_ALIGN
4504 || new_target->fr_subtype == RELAX_DESIRE_ALIGN)))
4505 new_target = new_target->fr_next;
4506
4507 assert (new_target->fr_type == rs_machine_dependent
4508 && (new_target->fr_subtype == RELAX_MAYBE_DESIRE_ALIGN
4509 || new_target->fr_subtype == RELAX_DESIRE_ALIGN));
4510 }
4511
4512 while (next_fragP && next_fragP->fr_fix == 0)
4513 {
4514 if (next_fragP->fr_type == rs_machine_dependent
4515 && next_fragP->fr_subtype == RELAX_LOOP_END)
4516 {
4517 next_fragP->fr_subtype = RELAX_LOOP_END_ADD_NOP;
4518 return;
4519 }
4520
4521 next_fragP = next_fragP->fr_next;
4522 }
4523 }
4524
4525
4526 static bfd_boolean
4527 next_frag_is_branch_target (const fragS *fragP)
4528 {
4529 /* Sometimes an empty will end up here due to storage allocation issues,
4530 so we have to skip until we find something legit. */
4531 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4532 {
4533 if (fragP->tc_frag_data.is_branch_target)
4534 return TRUE;
4535 if (fragP->fr_fix != 0)
4536 break;
4537 }
4538 return FALSE;
4539 }
4540
4541
4542 static bfd_boolean
4543 next_frag_is_loop_target (const fragS *fragP)
4544 {
4545 /* Sometimes an empty will end up here due storage allocation issues.
4546 So we have to skip until we find something legit. */
4547 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4548 {
4549 if (fragP->tc_frag_data.is_loop_target)
4550 return TRUE;
4551 if (fragP->fr_fix != 0)
4552 break;
4553 }
4554 return FALSE;
4555 }
4556
4557
4558 static addressT
4559 next_frag_pre_opcode_bytes (const fragS *fragp)
4560 {
4561 const fragS *next_fragp = fragp->fr_next;
4562 xtensa_opcode next_opcode;
4563
4564 if (!next_frag_opcode_is_loop (fragp, &next_opcode))
4565 return 0;
4566
4567 /* Sometimes an empty will end up here due to storage allocation issues,
4568 so we have to skip until we find something legit. */
4569 while (next_fragp->fr_fix == 0)
4570 next_fragp = next_fragp->fr_next;
4571
4572 if (next_fragp->fr_type != rs_machine_dependent)
4573 return 0;
4574
4575 /* There is some implicit knowledge encoded in here.
4576 The LOOP instructions that are NOT RELAX_IMMED have
4577 been relaxed. Note that we can assume that the LOOP
4578 instruction is in slot 0 because loops aren't bundleable. */
4579 if (next_fragp->tc_frag_data.slot_subtypes[0] > RELAX_IMMED)
4580 return get_expanded_loop_offset (next_opcode);
4581
4582 return 0;
4583 }
4584
4585
4586 /* Mark a location where we can later insert literal frags. Update
4587 the section's literal_pool_loc, so subsequent literals can be
4588 placed nearest to their use. */
4589
4590 static void
4591 xtensa_mark_literal_pool_location (void)
4592 {
4593 /* Any labels pointing to the current location need
4594 to be adjusted to after the literal pool. */
4595 emit_state s;
4596 fragS *pool_location;
4597
4598 if (use_literal_section)
4599 return;
4600
4601 /* We stash info in these frags so we can later move the literal's
4602 fixes into this frchain's fix list. */
4603 pool_location = frag_now;
4604 frag_now->tc_frag_data.lit_frchain = frchain_now;
4605 frag_now->tc_frag_data.literal_frag = frag_now;
4606 frag_variant (rs_machine_dependent, 0, 0,
4607 RELAX_LITERAL_POOL_BEGIN, NULL, 0, NULL);
4608 xtensa_set_frag_assembly_state (frag_now);
4609 frag_now->tc_frag_data.lit_seg = now_seg;
4610 frag_variant (rs_machine_dependent, 0, 0,
4611 RELAX_LITERAL_POOL_END, NULL, 0, NULL);
4612 xtensa_set_frag_assembly_state (frag_now);
4613
4614 /* Now put a frag into the literal pool that points to this location. */
4615 set_literal_pool_location (now_seg, pool_location);
4616 xtensa_switch_to_non_abs_literal_fragment (&s);
4617 frag_align (2, 0, 0);
4618 record_alignment (now_seg, 2);
4619
4620 /* Close whatever frag is there. */
4621 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4622 xtensa_set_frag_assembly_state (frag_now);
4623 frag_now->tc_frag_data.literal_frag = pool_location;
4624 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4625 xtensa_restore_emit_state (&s);
4626 xtensa_set_frag_assembly_state (frag_now);
4627 }
4628
4629
4630 /* Build a nop of the correct size into tinsn. */
4631
4632 static void
4633 build_nop (TInsn *tinsn, int size)
4634 {
4635 tinsn_init (tinsn);
4636 switch (size)
4637 {
4638 case 2:
4639 tinsn->opcode = xtensa_nop_n_opcode;
4640 tinsn->ntok = 0;
4641 if (tinsn->opcode == XTENSA_UNDEFINED)
4642 as_fatal (_("opcode 'NOP.N' unavailable in this configuration"));
4643 break;
4644
4645 case 3:
4646 if (xtensa_nop_opcode == XTENSA_UNDEFINED)
4647 {
4648 tinsn->opcode = xtensa_or_opcode;
4649 set_expr_const (&tinsn->tok[0], 1);
4650 set_expr_const (&tinsn->tok[1], 1);
4651 set_expr_const (&tinsn->tok[2], 1);
4652 tinsn->ntok = 3;
4653 }
4654 else
4655 tinsn->opcode = xtensa_nop_opcode;
4656
4657 assert (tinsn->opcode != XTENSA_UNDEFINED);
4658 }
4659 }
4660
4661
4662 /* Assemble a NOP of the requested size in the buffer. User must have
4663 allocated "buf" with at least "size" bytes. */
4664
4665 static void
4666 assemble_nop (int size, char *buf)
4667 {
4668 static xtensa_insnbuf insnbuf = NULL;
4669 TInsn tinsn;
4670
4671 build_nop (&tinsn, size);
4672
4673 if (!insnbuf)
4674 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4675
4676 tinsn_to_insnbuf (&tinsn, insnbuf);
4677 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf,
4678 (unsigned char *) buf, 0);
4679 }
4680
4681
4682 /* Return the number of bytes for the offset of the expanded loop
4683 instruction. This should be incorporated into the relaxation
4684 specification but is hard-coded here. This is used to auto-align
4685 the loop instruction. It is invalid to call this function if the
4686 configuration does not have loops or if the opcode is not a loop
4687 opcode. */
4688
4689 static addressT
4690 get_expanded_loop_offset (xtensa_opcode opcode)
4691 {
4692 /* This is the OFFSET of the loop instruction in the expanded loop.
4693 This MUST correspond directly to the specification of the loop
4694 expansion. It will be validated on fragment conversion. */
4695 assert (opcode != XTENSA_UNDEFINED);
4696 if (opcode == xtensa_loop_opcode)
4697 return 0;
4698 if (opcode == xtensa_loopnez_opcode)
4699 return 3;
4700 if (opcode == xtensa_loopgtz_opcode)
4701 return 6;
4702 as_fatal (_("get_expanded_loop_offset: invalid opcode"));
4703 return 0;
4704 }
4705
4706
4707 static fragS *
4708 get_literal_pool_location (segT seg)
4709 {
4710 return seg_info (seg)->tc_segment_info_data.literal_pool_loc;
4711 }
4712
4713
4714 static void
4715 set_literal_pool_location (segT seg, fragS *literal_pool_loc)
4716 {
4717 seg_info (seg)->tc_segment_info_data.literal_pool_loc = literal_pool_loc;
4718 }
4719
4720
4721 /* Set frag assembly state should be called when a new frag is
4722 opened and after a frag has been closed. */
4723
4724 static void
4725 xtensa_set_frag_assembly_state (fragS *fragP)
4726 {
4727 if (!density_supported)
4728 fragP->tc_frag_data.is_no_density = TRUE;
4729
4730 /* This function is called from subsegs_finish, which is called
4731 after xtensa_end, so we can't use "use_transform" or
4732 "use_schedule" here. */
4733 if (!directive_state[directive_transform])
4734 fragP->tc_frag_data.is_no_transform = TRUE;
4735 if (directive_state[directive_longcalls])
4736 fragP->tc_frag_data.use_longcalls = TRUE;
4737 fragP->tc_frag_data.use_absolute_literals =
4738 directive_state[directive_absolute_literals];
4739 fragP->tc_frag_data.is_assembly_state_set = TRUE;
4740 }
4741
4742
4743 static bfd_boolean
4744 relaxable_section (asection *sec)
4745 {
4746 return ((sec->flags & SEC_DEBUGGING) == 0
4747 && strcmp (sec->name, ".eh_frame") != 0);
4748 }
4749
4750
4751 static void
4752 xtensa_mark_frags_for_org (void)
4753 {
4754 segT *seclist;
4755
4756 /* Walk over each fragment of all of the current segments. If we find
4757 a .org frag in any of the segments, mark all frags prior to it as
4758 "no transform", which will prevent linker optimizations from messing
4759 up the .org distance. This should be done after
4760 xtensa_find_unmarked_state_frags, because we don't want to worry here
4761 about that function trashing the data we save here. */
4762
4763 for (seclist = &stdoutput->sections;
4764 seclist && *seclist;
4765 seclist = &(*seclist)->next)
4766 {
4767 segT sec = *seclist;
4768 segment_info_type *seginfo;
4769 fragS *fragP;
4770 flagword flags;
4771 flags = bfd_get_section_flags (stdoutput, sec);
4772 if (flags & SEC_DEBUGGING)
4773 continue;
4774 if (!(flags & SEC_ALLOC))
4775 continue;
4776
4777 seginfo = seg_info (sec);
4778 if (seginfo && seginfo->frchainP)
4779 {
4780 fragS *last_fragP = seginfo->frchainP->frch_root;
4781 for (fragP = seginfo->frchainP->frch_root; fragP;
4782 fragP = fragP->fr_next)
4783 {
4784 /* cvt_frag_to_fill has changed the fr_type of org frags to
4785 rs_fill, so use the value as cached in rs_subtype here. */
4786 if (fragP->fr_subtype == RELAX_ORG)
4787 {
4788 while (last_fragP != fragP->fr_next)
4789 {
4790 last_fragP->tc_frag_data.is_no_transform = TRUE;
4791 last_fragP = last_fragP->fr_next;
4792 }
4793 }
4794 }
4795 }
4796 }
4797 }
4798
4799
4800 static void
4801 xtensa_find_unmarked_state_frags (void)
4802 {
4803 segT *seclist;
4804
4805 /* Walk over each fragment of all of the current segments. For each
4806 unmarked fragment, mark it with the same info as the previous
4807 fragment. */
4808 for (seclist = &stdoutput->sections;
4809 seclist && *seclist;
4810 seclist = &(*seclist)->next)
4811 {
4812 segT sec = *seclist;
4813 segment_info_type *seginfo;
4814 fragS *fragP;
4815 flagword flags;
4816 flags = bfd_get_section_flags (stdoutput, sec);
4817 if (flags & SEC_DEBUGGING)
4818 continue;
4819 if (!(flags & SEC_ALLOC))
4820 continue;
4821
4822 seginfo = seg_info (sec);
4823 if (seginfo && seginfo->frchainP)
4824 {
4825 fragS *last_fragP = 0;
4826 for (fragP = seginfo->frchainP->frch_root; fragP;
4827 fragP = fragP->fr_next)
4828 {
4829 if (fragP->fr_fix != 0
4830 && !fragP->tc_frag_data.is_assembly_state_set)
4831 {
4832 if (last_fragP == 0)
4833 {
4834 as_warn_where (fragP->fr_file, fragP->fr_line,
4835 _("assembly state not set for first frag in section %s"),
4836 sec->name);
4837 }
4838 else
4839 {
4840 fragP->tc_frag_data.is_assembly_state_set = TRUE;
4841 fragP->tc_frag_data.is_no_density =
4842 last_fragP->tc_frag_data.is_no_density;
4843 fragP->tc_frag_data.is_no_transform =
4844 last_fragP->tc_frag_data.is_no_transform;
4845 fragP->tc_frag_data.use_longcalls =
4846 last_fragP->tc_frag_data.use_longcalls;
4847 fragP->tc_frag_data.use_absolute_literals =
4848 last_fragP->tc_frag_data.use_absolute_literals;
4849 }
4850 }
4851 if (fragP->tc_frag_data.is_assembly_state_set)
4852 last_fragP = fragP;
4853 }
4854 }
4855 }
4856 }
4857
4858
4859 static void
4860 xtensa_find_unaligned_branch_targets (bfd *abfd ATTRIBUTE_UNUSED,
4861 asection *sec,
4862 void *unused ATTRIBUTE_UNUSED)
4863 {
4864 flagword flags = bfd_get_section_flags (abfd, sec);
4865 segment_info_type *seginfo = seg_info (sec);
4866 fragS *frag = seginfo->frchainP->frch_root;
4867
4868 if (flags & SEC_CODE)
4869 {
4870 xtensa_isa isa = xtensa_default_isa;
4871 xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa);
4872 while (frag != NULL)
4873 {
4874 if (frag->tc_frag_data.is_branch_target)
4875 {
4876 int op_size;
4877 addressT branch_align, frag_addr;
4878 xtensa_format fmt;
4879
4880 xtensa_insnbuf_from_chars
4881 (isa, insnbuf, (unsigned char *) frag->fr_literal, 0);
4882 fmt = xtensa_format_decode (isa, insnbuf);
4883 op_size = xtensa_format_length (isa, fmt);
4884 branch_align = 1 << branch_align_power (sec);
4885 frag_addr = frag->fr_address % branch_align;
4886 if (frag_addr + op_size > branch_align)
4887 as_warn_where (frag->fr_file, frag->fr_line,
4888 _("unaligned branch target: %d bytes at 0x%lx"),
4889 op_size, (long) frag->fr_address);
4890 }
4891 frag = frag->fr_next;
4892 }
4893 xtensa_insnbuf_free (isa, insnbuf);
4894 }
4895 }
4896
4897
4898 static void
4899 xtensa_find_unaligned_loops (bfd *abfd ATTRIBUTE_UNUSED,
4900 asection *sec,
4901 void *unused ATTRIBUTE_UNUSED)
4902 {
4903 flagword flags = bfd_get_section_flags (abfd, sec);
4904 segment_info_type *seginfo = seg_info (sec);
4905 fragS *frag = seginfo->frchainP->frch_root;
4906 xtensa_isa isa = xtensa_default_isa;
4907
4908 if (flags & SEC_CODE)
4909 {
4910 xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa);
4911 while (frag != NULL)
4912 {
4913 if (frag->tc_frag_data.is_first_loop_insn)
4914 {
4915 int op_size;
4916 addressT frag_addr;
4917 xtensa_format fmt;
4918
4919 xtensa_insnbuf_from_chars
4920 (isa, insnbuf, (unsigned char *) frag->fr_literal, 0);
4921 fmt = xtensa_format_decode (isa, insnbuf);
4922 op_size = xtensa_format_length (isa, fmt);
4923 frag_addr = frag->fr_address % xtensa_fetch_width;
4924
4925 if (frag_addr + op_size > xtensa_fetch_width)
4926 as_warn_where (frag->fr_file, frag->fr_line,
4927 _("unaligned loop: %d bytes at 0x%lx"),
4928 op_size, (long) frag->fr_address);
4929 }
4930 frag = frag->fr_next;
4931 }
4932 xtensa_insnbuf_free (isa, insnbuf);
4933 }
4934 }
4935
4936
4937 static int
4938 xg_apply_fix_value (fixS *fixP, valueT val)
4939 {
4940 xtensa_isa isa = xtensa_default_isa;
4941 static xtensa_insnbuf insnbuf = NULL;
4942 static xtensa_insnbuf slotbuf = NULL;
4943 xtensa_format fmt;
4944 int slot;
4945 bfd_boolean alt_reloc;
4946 xtensa_opcode opcode;
4947 char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
4948
4949 (void) decode_reloc (fixP->fx_r_type, &slot, &alt_reloc);
4950 if (alt_reloc)
4951 as_fatal (_("unexpected fix"));
4952
4953 if (!insnbuf)
4954 {
4955 insnbuf = xtensa_insnbuf_alloc (isa);
4956 slotbuf = xtensa_insnbuf_alloc (isa);
4957 }
4958
4959 xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) fixpos, 0);
4960 fmt = xtensa_format_decode (isa, insnbuf);
4961 if (fmt == XTENSA_UNDEFINED)
4962 as_fatal (_("undecodable fix"));
4963 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
4964 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
4965 if (opcode == XTENSA_UNDEFINED)
4966 as_fatal (_("undecodable fix"));
4967
4968 /* CONST16 immediates are not PC-relative, despite the fact that we
4969 reuse the normal PC-relative operand relocations for the low part
4970 of a CONST16 operand. */
4971 if (opcode == xtensa_const16_opcode)
4972 return 0;
4973
4974 xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode,
4975 get_relaxable_immed (opcode), val,
4976 fixP->fx_file, fixP->fx_line);
4977
4978 xtensa_format_set_slot (isa, fmt, slot, insnbuf, slotbuf);
4979 xtensa_insnbuf_to_chars (isa, insnbuf, (unsigned char *) fixpos, 0);
4980
4981 return 1;
4982 }
4983
4984 \f
4985 /* External Functions and Other GAS Hooks. */
4986
4987 const char *
4988 xtensa_target_format (void)
4989 {
4990 return (target_big_endian ? "elf32-xtensa-be" : "elf32-xtensa-le");
4991 }
4992
4993
4994 void
4995 xtensa_file_arch_init (bfd *abfd)
4996 {
4997 bfd_set_private_flags (abfd, 0x100 | 0x200);
4998 }
4999
5000
5001 void
5002 md_number_to_chars (char *buf, valueT val, int n)
5003 {
5004 if (target_big_endian)
5005 number_to_chars_bigendian (buf, val, n);
5006 else
5007 number_to_chars_littleendian (buf, val, n);
5008 }
5009
5010
5011 /* This function is called once, at assembler startup time. It should
5012 set up all the tables, etc. that the MD part of the assembler will
5013 need. */
5014
5015 void
5016 md_begin (void)
5017 {
5018 segT current_section = now_seg;
5019 int current_subsec = now_subseg;
5020 xtensa_isa isa;
5021
5022 xtensa_default_isa = xtensa_isa_init (0, 0);
5023 isa = xtensa_default_isa;
5024
5025 linkrelax = 1;
5026
5027 /* Set up the literal sections. */
5028 memset (&default_lit_sections, 0, sizeof (default_lit_sections));
5029
5030 subseg_set (current_section, current_subsec);
5031
5032 xg_init_vinsn (&cur_vinsn);
5033
5034 xtensa_addi_opcode = xtensa_opcode_lookup (isa, "addi");
5035 xtensa_addmi_opcode = xtensa_opcode_lookup (isa, "addmi");
5036 xtensa_call0_opcode = xtensa_opcode_lookup (isa, "call0");
5037 xtensa_call4_opcode = xtensa_opcode_lookup (isa, "call4");
5038 xtensa_call8_opcode = xtensa_opcode_lookup (isa, "call8");
5039 xtensa_call12_opcode = xtensa_opcode_lookup (isa, "call12");
5040 xtensa_callx0_opcode = xtensa_opcode_lookup (isa, "callx0");
5041 xtensa_callx4_opcode = xtensa_opcode_lookup (isa, "callx4");
5042 xtensa_callx8_opcode = xtensa_opcode_lookup (isa, "callx8");
5043 xtensa_callx12_opcode = xtensa_opcode_lookup (isa, "callx12");
5044 xtensa_const16_opcode = xtensa_opcode_lookup (isa, "const16");
5045 xtensa_entry_opcode = xtensa_opcode_lookup (isa, "entry");
5046 xtensa_extui_opcode = xtensa_opcode_lookup (isa, "extui");
5047 xtensa_movi_opcode = xtensa_opcode_lookup (isa, "movi");
5048 xtensa_movi_n_opcode = xtensa_opcode_lookup (isa, "movi.n");
5049 xtensa_isync_opcode = xtensa_opcode_lookup (isa, "isync");
5050 xtensa_jx_opcode = xtensa_opcode_lookup (isa, "jx");
5051 xtensa_l32r_opcode = xtensa_opcode_lookup (isa, "l32r");
5052 xtensa_loop_opcode = xtensa_opcode_lookup (isa, "loop");
5053 xtensa_loopnez_opcode = xtensa_opcode_lookup (isa, "loopnez");
5054 xtensa_loopgtz_opcode = xtensa_opcode_lookup (isa, "loopgtz");
5055 xtensa_nop_opcode = xtensa_opcode_lookup (isa, "nop");
5056 xtensa_nop_n_opcode = xtensa_opcode_lookup (isa, "nop.n");
5057 xtensa_or_opcode = xtensa_opcode_lookup (isa, "or");
5058 xtensa_ret_opcode = xtensa_opcode_lookup (isa, "ret");
5059 xtensa_ret_n_opcode = xtensa_opcode_lookup (isa, "ret.n");
5060 xtensa_retw_opcode = xtensa_opcode_lookup (isa, "retw");
5061 xtensa_retw_n_opcode = xtensa_opcode_lookup (isa, "retw.n");
5062 xtensa_rsr_lcount_opcode = xtensa_opcode_lookup (isa, "rsr.lcount");
5063 xtensa_waiti_opcode = xtensa_opcode_lookup (isa, "waiti");
5064
5065 init_op_placement_info_table ();
5066
5067 /* Set up the assembly state. */
5068 if (!frag_now->tc_frag_data.is_assembly_state_set)
5069 xtensa_set_frag_assembly_state (frag_now);
5070 }
5071
5072
5073 /* TC_INIT_FIX_DATA hook */
5074
5075 void
5076 xtensa_init_fix_data (fixS *x)
5077 {
5078 x->tc_fix_data.slot = 0;
5079 x->tc_fix_data.X_add_symbol = NULL;
5080 x->tc_fix_data.X_add_number = 0;
5081 }
5082
5083
5084 /* tc_frob_label hook */
5085
5086 void
5087 xtensa_frob_label (symbolS *sym)
5088 {
5089 float freq;
5090
5091 if (cur_vinsn.inside_bundle)
5092 {
5093 as_bad (_("labels are not valid inside bundles"));
5094 return;
5095 }
5096
5097 freq = get_subseg_target_freq (now_seg, now_subseg);
5098
5099 /* Since the label was already attached to a frag associated with the
5100 previous basic block, it now needs to be reset to the current frag. */
5101 symbol_set_frag (sym, frag_now);
5102 S_SET_VALUE (sym, (valueT) frag_now_fix ());
5103
5104 if (generating_literals)
5105 xtensa_add_literal_sym (sym);
5106 else
5107 xtensa_add_insn_label (sym);
5108
5109 if (symbol_get_tc (sym)->is_loop_target)
5110 {
5111 if ((get_last_insn_flags (now_seg, now_subseg)
5112 & FLAG_IS_BAD_LOOPEND) != 0)
5113 as_bad (_("invalid last instruction for a zero-overhead loop"));
5114
5115 xtensa_set_frag_assembly_state (frag_now);
5116 frag_var (rs_machine_dependent, 4, 4, RELAX_LOOP_END,
5117 frag_now->fr_symbol, frag_now->fr_offset, NULL);
5118
5119 xtensa_set_frag_assembly_state (frag_now);
5120 xtensa_move_labels (frag_now, 0);
5121 }
5122
5123 /* No target aligning in the absolute section. */
5124 if (now_seg != absolute_section
5125 && do_align_targets ()
5126 && !is_unaligned_label (sym)
5127 && !generating_literals)
5128 {
5129 xtensa_set_frag_assembly_state (frag_now);
5130
5131 frag_var (rs_machine_dependent,
5132 0, (int) freq,
5133 RELAX_DESIRE_ALIGN_IF_TARGET,
5134 frag_now->fr_symbol, frag_now->fr_offset, NULL);
5135 xtensa_set_frag_assembly_state (frag_now);
5136 xtensa_move_labels (frag_now, 0);
5137 }
5138
5139 /* We need to mark the following properties even if we aren't aligning. */
5140
5141 /* If the label is already known to be a branch target, i.e., a
5142 forward branch, mark the frag accordingly. Backward branches
5143 are handled by xg_add_branch_and_loop_targets. */
5144 if (symbol_get_tc (sym)->is_branch_target)
5145 symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
5146
5147 /* Loops only go forward, so they can be identified here. */
5148 if (symbol_get_tc (sym)->is_loop_target)
5149 symbol_get_frag (sym)->tc_frag_data.is_loop_target = TRUE;
5150
5151 dwarf2_emit_label (sym);
5152 }
5153
5154
5155 /* tc_unrecognized_line hook */
5156
5157 int
5158 xtensa_unrecognized_line (int ch)
5159 {
5160 switch (ch)
5161 {
5162 case '{' :
5163 if (cur_vinsn.inside_bundle == 0)
5164 {
5165 /* PR8110: Cannot emit line number info inside a FLIX bundle
5166 when using --gstabs. Temporarily disable debug info. */
5167 generate_lineno_debug ();
5168 if (debug_type == DEBUG_STABS)
5169 {
5170 xt_saved_debug_type = debug_type;
5171 debug_type = DEBUG_NONE;
5172 }
5173
5174 cur_vinsn.inside_bundle = 1;
5175 }
5176 else
5177 {
5178 as_bad (_("extra opening brace"));
5179 return 0;
5180 }
5181 break;
5182
5183 case '}' :
5184 if (cur_vinsn.inside_bundle)
5185 finish_vinsn (&cur_vinsn);
5186 else
5187 {
5188 as_bad (_("extra closing brace"));
5189 return 0;
5190 }
5191 break;
5192 default:
5193 as_bad (_("syntax error"));
5194 return 0;
5195 }
5196 return 1;
5197 }
5198
5199
5200 /* md_flush_pending_output hook */
5201
5202 void
5203 xtensa_flush_pending_output (void)
5204 {
5205 /* This line fixes a bug where automatically generated gstabs info
5206 separates a function label from its entry instruction, ending up
5207 with the literal position between the function label and the entry
5208 instruction and crashing code. It only happens with --gstabs and
5209 --text-section-literals, and when several other obscure relaxation
5210 conditions are met. */
5211 if (outputting_stabs_line_debug)
5212 return;
5213
5214 if (cur_vinsn.inside_bundle)
5215 as_bad (_("missing closing brace"));
5216
5217 /* If there is a non-zero instruction fragment, close it. */
5218 if (frag_now_fix () != 0 && frag_now->tc_frag_data.is_insn)
5219 {
5220 frag_wane (frag_now);
5221 frag_new (0);
5222 xtensa_set_frag_assembly_state (frag_now);
5223 }
5224 frag_now->tc_frag_data.is_insn = FALSE;
5225
5226 xtensa_clear_insn_labels ();
5227 }
5228
5229
5230 /* We had an error while parsing an instruction. The string might look
5231 like this: "insn arg1, arg2 }". If so, we need to see the closing
5232 brace and reset some fields. Otherwise, the vinsn never gets closed
5233 and the num_slots field will grow past the end of the array of slots,
5234 and bad things happen. */
5235
5236 static void
5237 error_reset_cur_vinsn (void)
5238 {
5239 if (cur_vinsn.inside_bundle)
5240 {
5241 if (*input_line_pointer == '}'
5242 || *(input_line_pointer - 1) == '}'
5243 || *(input_line_pointer - 2) == '}')
5244 xg_clear_vinsn (&cur_vinsn);
5245 }
5246 }
5247
5248
5249 void
5250 md_assemble (char *str)
5251 {
5252 xtensa_isa isa = xtensa_default_isa;
5253 char *opname;
5254 unsigned opnamelen;
5255 bfd_boolean has_underbar = FALSE;
5256 char *arg_strings[MAX_INSN_ARGS];
5257 int num_args;
5258 TInsn orig_insn; /* Original instruction from the input. */
5259
5260 tinsn_init (&orig_insn);
5261
5262 /* Split off the opcode. */
5263 opnamelen = strspn (str, "abcdefghijklmnopqrstuvwxyz_/0123456789.");
5264 opname = xmalloc (opnamelen + 1);
5265 memcpy (opname, str, opnamelen);
5266 opname[opnamelen] = '\0';
5267
5268 num_args = tokenize_arguments (arg_strings, str + opnamelen);
5269 if (num_args == -1)
5270 {
5271 as_bad (_("syntax error"));
5272 return;
5273 }
5274
5275 if (xg_translate_idioms (&opname, &num_args, arg_strings))
5276 return;
5277
5278 /* Check for an underbar prefix. */
5279 if (*opname == '_')
5280 {
5281 has_underbar = TRUE;
5282 opname += 1;
5283 }
5284
5285 orig_insn.insn_type = ITYPE_INSN;
5286 orig_insn.ntok = 0;
5287 orig_insn.is_specific_opcode = (has_underbar || !use_transform ());
5288
5289 orig_insn.opcode = xtensa_opcode_lookup (isa, opname);
5290 if (orig_insn.opcode == XTENSA_UNDEFINED)
5291 {
5292 xtensa_format fmt = xtensa_format_lookup (isa, opname);
5293 if (fmt == XTENSA_UNDEFINED)
5294 {
5295 as_bad (_("unknown opcode or format name '%s'"), opname);
5296 error_reset_cur_vinsn ();
5297 return;
5298 }
5299 if (!cur_vinsn.inside_bundle)
5300 {
5301 as_bad (_("format names only valid inside bundles"));
5302 error_reset_cur_vinsn ();
5303 return;
5304 }
5305 if (cur_vinsn.format != XTENSA_UNDEFINED)
5306 as_warn (_("multiple formats specified for one bundle; using '%s'"),
5307 opname);
5308 cur_vinsn.format = fmt;
5309 free (has_underbar ? opname - 1 : opname);
5310 error_reset_cur_vinsn ();
5311 return;
5312 }
5313
5314 /* Parse the arguments. */
5315 if (parse_arguments (&orig_insn, num_args, arg_strings))
5316 {
5317 as_bad (_("syntax error"));
5318 error_reset_cur_vinsn ();
5319 return;
5320 }
5321
5322 /* Free the opcode and argument strings, now that they've been parsed. */
5323 free (has_underbar ? opname - 1 : opname);
5324 opname = 0;
5325 while (num_args-- > 0)
5326 free (arg_strings[num_args]);
5327
5328 /* Get expressions for invisible operands. */
5329 if (get_invisible_operands (&orig_insn))
5330 {
5331 error_reset_cur_vinsn ();
5332 return;
5333 }
5334
5335 /* Check for the right number and type of arguments. */
5336 if (tinsn_check_arguments (&orig_insn))
5337 {
5338 error_reset_cur_vinsn ();
5339 return;
5340 }
5341
5342 /* Record the line number for each TInsn, because a FLIX bundle may be
5343 spread across multiple input lines and individual instructions may be
5344 moved around in some cases. */
5345 orig_insn.loc_directive_seen = dwarf2_loc_directive_seen;
5346 dwarf2_where (&orig_insn.debug_line);
5347 dwarf2_consume_line_info ();
5348
5349 xg_add_branch_and_loop_targets (&orig_insn);
5350
5351 /* Check that immediate value for ENTRY is >= 16. */
5352 if (orig_insn.opcode == xtensa_entry_opcode && orig_insn.ntok >= 3)
5353 {
5354 expressionS *exp = &orig_insn.tok[2];
5355 if (exp->X_op == O_constant && exp->X_add_number < 16)
5356 as_warn (_("entry instruction with stack decrement < 16"));
5357 }
5358
5359 /* Finish it off:
5360 assemble_tokens (opcode, tok, ntok);
5361 expand the tokens from the orig_insn into the
5362 stack of instructions that will not expand
5363 unless required at relaxation time. */
5364
5365 if (!cur_vinsn.inside_bundle)
5366 emit_single_op (&orig_insn);
5367 else /* We are inside a bundle. */
5368 {
5369 cur_vinsn.slots[cur_vinsn.num_slots] = orig_insn;
5370 cur_vinsn.num_slots++;
5371 if (*input_line_pointer == '}'
5372 || *(input_line_pointer - 1) == '}'
5373 || *(input_line_pointer - 2) == '}')
5374 finish_vinsn (&cur_vinsn);
5375 }
5376
5377 /* We've just emitted a new instruction so clear the list of labels. */
5378 xtensa_clear_insn_labels ();
5379 }
5380
5381
5382 /* HANDLE_ALIGN hook */
5383
5384 /* For a .align directive, we mark the previous block with the alignment
5385 information. This will be placed in the object file in the
5386 property section corresponding to this section. */
5387
5388 void
5389 xtensa_handle_align (fragS *fragP)
5390 {
5391 if (linkrelax
5392 && ! fragP->tc_frag_data.is_literal
5393 && (fragP->fr_type == rs_align
5394 || fragP->fr_type == rs_align_code)
5395 && fragP->fr_address + fragP->fr_fix > 0
5396 && fragP->fr_offset > 0
5397 && now_seg != bss_section)
5398 {
5399 fragP->tc_frag_data.is_align = TRUE;
5400 fragP->tc_frag_data.alignment = fragP->fr_offset;
5401 }
5402
5403 if (fragP->fr_type == rs_align_test)
5404 {
5405 int count;
5406 count = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
5407 if (count != 0)
5408 as_bad_where (fragP->fr_file, fragP->fr_line,
5409 _("unaligned entry instruction"));
5410 }
5411
5412 if (linkrelax && fragP->fr_type == rs_org)
5413 fragP->fr_subtype = RELAX_ORG;
5414 }
5415
5416
5417 /* TC_FRAG_INIT hook */
5418
5419 void
5420 xtensa_frag_init (fragS *frag)
5421 {
5422 xtensa_set_frag_assembly_state (frag);
5423 }
5424
5425
5426 symbolS *
5427 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
5428 {
5429 return NULL;
5430 }
5431
5432
5433 /* Round up a section size to the appropriate boundary. */
5434
5435 valueT
5436 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
5437 {
5438 return size; /* Byte alignment is fine. */
5439 }
5440
5441
5442 long
5443 md_pcrel_from (fixS *fixP)
5444 {
5445 char *insn_p;
5446 static xtensa_insnbuf insnbuf = NULL;
5447 static xtensa_insnbuf slotbuf = NULL;
5448 int opnum;
5449 uint32 opnd_value;
5450 xtensa_opcode opcode;
5451 xtensa_format fmt;
5452 int slot;
5453 xtensa_isa isa = xtensa_default_isa;
5454 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
5455 bfd_boolean alt_reloc;
5456
5457 if (fixP->fx_r_type == BFD_RELOC_XTENSA_ASM_EXPAND)
5458 return 0;
5459
5460 if (fixP->fx_r_type == BFD_RELOC_32_PCREL)
5461 return addr;
5462
5463 if (!insnbuf)
5464 {
5465 insnbuf = xtensa_insnbuf_alloc (isa);
5466 slotbuf = xtensa_insnbuf_alloc (isa);
5467 }
5468
5469 insn_p = &fixP->fx_frag->fr_literal[fixP->fx_where];
5470 xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) insn_p, 0);
5471 fmt = xtensa_format_decode (isa, insnbuf);
5472
5473 if (fmt == XTENSA_UNDEFINED)
5474 as_fatal (_("bad instruction format"));
5475
5476 if (decode_reloc (fixP->fx_r_type, &slot, &alt_reloc) != 0)
5477 as_fatal (_("invalid relocation"));
5478
5479 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
5480 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
5481
5482 /* Check for "alternate" relocations (operand not specified). None
5483 of the current uses for these are really PC-relative. */
5484 if (alt_reloc || opcode == xtensa_const16_opcode)
5485 {
5486 if (opcode != xtensa_l32r_opcode
5487 && opcode != xtensa_const16_opcode)
5488 as_fatal (_("invalid relocation for '%s' instruction"),
5489 xtensa_opcode_name (isa, opcode));
5490 return 0;
5491 }
5492
5493 opnum = get_relaxable_immed (opcode);
5494 opnd_value = 0;
5495 if (xtensa_operand_is_PCrelative (isa, opcode, opnum) != 1
5496 || xtensa_operand_do_reloc (isa, opcode, opnum, &opnd_value, addr))
5497 {
5498 as_bad_where (fixP->fx_file,
5499 fixP->fx_line,
5500 _("invalid relocation for operand %d of '%s'"),
5501 opnum, xtensa_opcode_name (isa, opcode));
5502 return 0;
5503 }
5504 return 0 - opnd_value;
5505 }
5506
5507
5508 /* TC_FORCE_RELOCATION hook */
5509
5510 int
5511 xtensa_force_relocation (fixS *fix)
5512 {
5513 switch (fix->fx_r_type)
5514 {
5515 case BFD_RELOC_XTENSA_ASM_EXPAND:
5516 case BFD_RELOC_XTENSA_SLOT0_ALT:
5517 case BFD_RELOC_XTENSA_SLOT1_ALT:
5518 case BFD_RELOC_XTENSA_SLOT2_ALT:
5519 case BFD_RELOC_XTENSA_SLOT3_ALT:
5520 case BFD_RELOC_XTENSA_SLOT4_ALT:
5521 case BFD_RELOC_XTENSA_SLOT5_ALT:
5522 case BFD_RELOC_XTENSA_SLOT6_ALT:
5523 case BFD_RELOC_XTENSA_SLOT7_ALT:
5524 case BFD_RELOC_XTENSA_SLOT8_ALT:
5525 case BFD_RELOC_XTENSA_SLOT9_ALT:
5526 case BFD_RELOC_XTENSA_SLOT10_ALT:
5527 case BFD_RELOC_XTENSA_SLOT11_ALT:
5528 case BFD_RELOC_XTENSA_SLOT12_ALT:
5529 case BFD_RELOC_XTENSA_SLOT13_ALT:
5530 case BFD_RELOC_XTENSA_SLOT14_ALT:
5531 return 1;
5532 default:
5533 break;
5534 }
5535
5536 if (linkrelax && fix->fx_addsy
5537 && relaxable_section (S_GET_SEGMENT (fix->fx_addsy)))
5538 return 1;
5539
5540 return generic_force_reloc (fix);
5541 }
5542
5543
5544 /* TC_VALIDATE_FIX_SUB hook */
5545
5546 int
5547 xtensa_validate_fix_sub (fixS *fix)
5548 {
5549 segT add_symbol_segment, sub_symbol_segment;
5550
5551 /* The difference of two symbols should be resolved by the assembler when
5552 linkrelax is not set. If the linker may relax the section containing
5553 the symbols, then an Xtensa DIFF relocation must be generated so that
5554 the linker knows to adjust the difference value. */
5555 if (!linkrelax || fix->fx_addsy == NULL)
5556 return 0;
5557
5558 /* Make sure both symbols are in the same segment, and that segment is
5559 "normal" and relaxable. If the segment is not "normal", then the
5560 fix is not valid. If the segment is not "relaxable", then the fix
5561 should have been handled earlier. */
5562 add_symbol_segment = S_GET_SEGMENT (fix->fx_addsy);
5563 if (! SEG_NORMAL (add_symbol_segment) ||
5564 ! relaxable_section (add_symbol_segment))
5565 return 0;
5566 sub_symbol_segment = S_GET_SEGMENT (fix->fx_subsy);
5567 return (sub_symbol_segment == add_symbol_segment);
5568 }
5569
5570
5571 /* NO_PSEUDO_DOT hook */
5572
5573 /* This function has nothing to do with pseudo dots, but this is the
5574 nearest macro to where the check needs to take place. FIXME: This
5575 seems wrong. */
5576
5577 bfd_boolean
5578 xtensa_check_inside_bundle (void)
5579 {
5580 if (cur_vinsn.inside_bundle && input_line_pointer[-1] == '.')
5581 as_bad (_("directives are not valid inside bundles"));
5582
5583 /* This function must always return FALSE because it is called via a
5584 macro that has nothing to do with bundling. */
5585 return FALSE;
5586 }
5587
5588
5589 /* md_elf_section_change_hook */
5590
5591 void
5592 xtensa_elf_section_change_hook (void)
5593 {
5594 /* Set up the assembly state. */
5595 if (!frag_now->tc_frag_data.is_assembly_state_set)
5596 xtensa_set_frag_assembly_state (frag_now);
5597 }
5598
5599
5600 /* tc_fix_adjustable hook */
5601
5602 bfd_boolean
5603 xtensa_fix_adjustable (fixS *fixP)
5604 {
5605 /* An offset is not allowed in combination with the difference of two
5606 symbols, but that cannot be easily detected after a local symbol
5607 has been adjusted to a (section+offset) form. Return 0 so that such
5608 an fix will not be adjusted. */
5609 if (fixP->fx_subsy && fixP->fx_addsy && fixP->fx_offset
5610 && relaxable_section (S_GET_SEGMENT (fixP->fx_subsy)))
5611 return 0;
5612
5613 /* We need the symbol name for the VTABLE entries. */
5614 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
5615 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
5616 return 0;
5617
5618 return 1;
5619 }
5620
5621
5622 /* tc_symbol_new_hook */
5623
5624 symbolS *expr_symbols = NULL;
5625
5626 void
5627 xtensa_symbol_new_hook (symbolS *sym)
5628 {
5629 if (S_GET_SEGMENT (sym) == expr_section)
5630 {
5631 symbol_get_tc (sym)->next_expr_symbol = expr_symbols;
5632 expr_symbols = sym;
5633 }
5634 }
5635
5636
5637 void
5638 md_apply_fix (fixS *fixP, valueT *valP, segT seg)
5639 {
5640 char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
5641 valueT val = 0;
5642
5643 /* Subtracted symbols are only allowed for a few relocation types, and
5644 unless linkrelax is enabled, they should not make it to this point. */
5645 if (fixP->fx_subsy && !(linkrelax && (fixP->fx_r_type == BFD_RELOC_32
5646 || fixP->fx_r_type == BFD_RELOC_16
5647 || fixP->fx_r_type == BFD_RELOC_8)))
5648 as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
5649
5650 switch (fixP->fx_r_type)
5651 {
5652 case BFD_RELOC_32_PCREL:
5653 case BFD_RELOC_32:
5654 case BFD_RELOC_16:
5655 case BFD_RELOC_8:
5656 if (fixP->fx_subsy)
5657 {
5658 switch (fixP->fx_r_type)
5659 {
5660 case BFD_RELOC_8:
5661 fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF8;
5662 break;
5663 case BFD_RELOC_16:
5664 fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF16;
5665 break;
5666 case BFD_RELOC_32:
5667 fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF32;
5668 break;
5669 default:
5670 break;
5671 }
5672
5673 /* An offset is only allowed when it results from adjusting a
5674 local symbol into a section-relative offset. If the offset
5675 came from the original expression, tc_fix_adjustable will have
5676 prevented the fix from being converted to a section-relative
5677 form so that we can flag the error here. */
5678 if (fixP->fx_offset != 0 && !symbol_section_p (fixP->fx_addsy))
5679 as_bad_where (fixP->fx_file, fixP->fx_line,
5680 _("cannot represent subtraction with an offset"));
5681
5682 val = (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset
5683 - S_GET_VALUE (fixP->fx_subsy));
5684
5685 /* The difference value gets written out, and the DIFF reloc
5686 identifies the address of the subtracted symbol (i.e., the one
5687 with the lowest address). */
5688 *valP = val;
5689 fixP->fx_offset -= val;
5690 fixP->fx_subsy = NULL;
5691 }
5692 else if (! fixP->fx_addsy)
5693 {
5694 val = *valP;
5695 fixP->fx_done = 1;
5696 }
5697 /* fall through */
5698
5699 case BFD_RELOC_XTENSA_PLT:
5700 md_number_to_chars (fixpos, val, fixP->fx_size);
5701 fixP->fx_no_overflow = 0; /* Use the standard overflow check. */
5702 break;
5703
5704 case BFD_RELOC_XTENSA_SLOT0_OP:
5705 case BFD_RELOC_XTENSA_SLOT1_OP:
5706 case BFD_RELOC_XTENSA_SLOT2_OP:
5707 case BFD_RELOC_XTENSA_SLOT3_OP:
5708 case BFD_RELOC_XTENSA_SLOT4_OP:
5709 case BFD_RELOC_XTENSA_SLOT5_OP:
5710 case BFD_RELOC_XTENSA_SLOT6_OP:
5711 case BFD_RELOC_XTENSA_SLOT7_OP:
5712 case BFD_RELOC_XTENSA_SLOT8_OP:
5713 case BFD_RELOC_XTENSA_SLOT9_OP:
5714 case BFD_RELOC_XTENSA_SLOT10_OP:
5715 case BFD_RELOC_XTENSA_SLOT11_OP:
5716 case BFD_RELOC_XTENSA_SLOT12_OP:
5717 case BFD_RELOC_XTENSA_SLOT13_OP:
5718 case BFD_RELOC_XTENSA_SLOT14_OP:
5719 if (linkrelax)
5720 {
5721 /* Write the tentative value of a PC-relative relocation to a
5722 local symbol into the instruction. The value will be ignored
5723 by the linker, and it makes the object file disassembly
5724 readable when all branch targets are encoded in relocations. */
5725
5726 assert (fixP->fx_addsy);
5727 if (S_GET_SEGMENT (fixP->fx_addsy) == seg
5728 && !S_FORCE_RELOC (fixP->fx_addsy, 1))
5729 {
5730 val = (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset
5731 - md_pcrel_from (fixP));
5732 (void) xg_apply_fix_value (fixP, val);
5733 }
5734 }
5735 else if (! fixP->fx_addsy)
5736 {
5737 val = *valP;
5738 if (xg_apply_fix_value (fixP, val))
5739 fixP->fx_done = 1;
5740 }
5741 break;
5742
5743 case BFD_RELOC_XTENSA_ASM_EXPAND:
5744 case BFD_RELOC_XTENSA_SLOT0_ALT:
5745 case BFD_RELOC_XTENSA_SLOT1_ALT:
5746 case BFD_RELOC_XTENSA_SLOT2_ALT:
5747 case BFD_RELOC_XTENSA_SLOT3_ALT:
5748 case BFD_RELOC_XTENSA_SLOT4_ALT:
5749 case BFD_RELOC_XTENSA_SLOT5_ALT:
5750 case BFD_RELOC_XTENSA_SLOT6_ALT:
5751 case BFD_RELOC_XTENSA_SLOT7_ALT:
5752 case BFD_RELOC_XTENSA_SLOT8_ALT:
5753 case BFD_RELOC_XTENSA_SLOT9_ALT:
5754 case BFD_RELOC_XTENSA_SLOT10_ALT:
5755 case BFD_RELOC_XTENSA_SLOT11_ALT:
5756 case BFD_RELOC_XTENSA_SLOT12_ALT:
5757 case BFD_RELOC_XTENSA_SLOT13_ALT:
5758 case BFD_RELOC_XTENSA_SLOT14_ALT:
5759 /* These all need to be resolved at link-time. Do nothing now. */
5760 break;
5761
5762 case BFD_RELOC_VTABLE_INHERIT:
5763 case BFD_RELOC_VTABLE_ENTRY:
5764 fixP->fx_done = 0;
5765 break;
5766
5767 default:
5768 as_bad (_("unhandled local relocation fix %s"),
5769 bfd_get_reloc_code_name (fixP->fx_r_type));
5770 }
5771 }
5772
5773
5774 char *
5775 md_atof (int type, char *litP, int *sizeP)
5776 {
5777 return ieee_md_atof (type, litP, sizeP, target_big_endian);
5778 }
5779
5780
5781 int
5782 md_estimate_size_before_relax (fragS *fragP, segT seg ATTRIBUTE_UNUSED)
5783 {
5784 return total_frag_text_expansion (fragP);
5785 }
5786
5787
5788 /* Translate internal representation of relocation info to BFD target
5789 format. */
5790
5791 arelent *
5792 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
5793 {
5794 arelent *reloc;
5795
5796 reloc = (arelent *) xmalloc (sizeof (arelent));
5797 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5798 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
5799 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
5800
5801 /* Make sure none of our internal relocations make it this far.
5802 They'd better have been fully resolved by this point. */
5803 assert ((int) fixp->fx_r_type > 0);
5804
5805 reloc->addend = fixp->fx_offset;
5806
5807 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
5808 if (reloc->howto == NULL)
5809 {
5810 as_bad_where (fixp->fx_file, fixp->fx_line,
5811 _("cannot represent `%s' relocation in object file"),
5812 bfd_get_reloc_code_name (fixp->fx_r_type));
5813 free (reloc->sym_ptr_ptr);
5814 free (reloc);
5815 return NULL;
5816 }
5817
5818 if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
5819 as_fatal (_("internal error; cannot generate `%s' relocation"),
5820 bfd_get_reloc_code_name (fixp->fx_r_type));
5821
5822 return reloc;
5823 }
5824
5825 \f
5826 /* Checks for resource conflicts between instructions. */
5827
5828 /* The func unit stuff could be implemented as bit-vectors rather
5829 than the iterative approach here. If it ends up being too
5830 slow, we will switch it. */
5831
5832 resource_table *
5833 new_resource_table (void *data,
5834 int cycles,
5835 int nu,
5836 unit_num_copies_func uncf,
5837 opcode_num_units_func onuf,
5838 opcode_funcUnit_use_unit_func ouuf,
5839 opcode_funcUnit_use_stage_func ousf)
5840 {
5841 int i;
5842 resource_table *rt = (resource_table *) xmalloc (sizeof (resource_table));
5843 rt->data = data;
5844 rt->cycles = cycles;
5845 rt->allocated_cycles = cycles;
5846 rt->num_units = nu;
5847 rt->unit_num_copies = uncf;
5848 rt->opcode_num_units = onuf;
5849 rt->opcode_unit_use = ouuf;
5850 rt->opcode_unit_stage = ousf;
5851
5852 rt->units = (unsigned char **) xcalloc (cycles, sizeof (unsigned char *));
5853 for (i = 0; i < cycles; i++)
5854 rt->units[i] = (unsigned char *) xcalloc (nu, sizeof (unsigned char));
5855
5856 return rt;
5857 }
5858
5859
5860 void
5861 clear_resource_table (resource_table *rt)
5862 {
5863 int i, j;
5864 for (i = 0; i < rt->allocated_cycles; i++)
5865 for (j = 0; j < rt->num_units; j++)
5866 rt->units[i][j] = 0;
5867 }
5868
5869
5870 /* We never shrink it, just fake it into thinking so. */
5871
5872 void
5873 resize_resource_table (resource_table *rt, int cycles)
5874 {
5875 int i, old_cycles;
5876
5877 rt->cycles = cycles;
5878 if (cycles <= rt->allocated_cycles)
5879 return;
5880
5881 old_cycles = rt->allocated_cycles;
5882 rt->allocated_cycles = cycles;
5883
5884 rt->units = xrealloc (rt->units,
5885 rt->allocated_cycles * sizeof (unsigned char *));
5886 for (i = 0; i < old_cycles; i++)
5887 rt->units[i] = xrealloc (rt->units[i],
5888 rt->num_units * sizeof (unsigned char));
5889 for (i = old_cycles; i < cycles; i++)
5890 rt->units[i] = xcalloc (rt->num_units, sizeof (unsigned char));
5891 }
5892
5893
5894 bfd_boolean
5895 resources_available (resource_table *rt, xtensa_opcode opcode, int cycle)
5896 {
5897 int i;
5898 int uses = (rt->opcode_num_units) (rt->data, opcode);
5899
5900 for (i = 0; i < uses; i++)
5901 {
5902 xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
5903 int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
5904 int copies_in_use = rt->units[stage + cycle][unit];
5905 int copies = (rt->unit_num_copies) (rt->data, unit);
5906 if (copies_in_use >= copies)
5907 return FALSE;
5908 }
5909 return TRUE;
5910 }
5911
5912
5913 void
5914 reserve_resources (resource_table *rt, xtensa_opcode opcode, int cycle)
5915 {
5916 int i;
5917 int uses = (rt->opcode_num_units) (rt->data, opcode);
5918
5919 for (i = 0; i < uses; i++)
5920 {
5921 xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
5922 int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
5923 /* Note that this allows resources to be oversubscribed. That's
5924 essential to the way the optional scheduler works.
5925 resources_available reports when a resource is over-subscribed,
5926 so it's easy to tell. */
5927 rt->units[stage + cycle][unit]++;
5928 }
5929 }
5930
5931
5932 void
5933 release_resources (resource_table *rt, xtensa_opcode opcode, int cycle)
5934 {
5935 int i;
5936 int uses = (rt->opcode_num_units) (rt->data, opcode);
5937
5938 for (i = 0; i < uses; i++)
5939 {
5940 xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
5941 int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
5942 assert (rt->units[stage + cycle][unit] > 0);
5943 rt->units[stage + cycle][unit]--;
5944 }
5945 }
5946
5947
5948 /* Wrapper functions make parameterized resource reservation
5949 more convenient. */
5950
5951 int
5952 opcode_funcUnit_use_unit (void *data, xtensa_opcode opcode, int idx)
5953 {
5954 xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx);
5955 return use->unit;
5956 }
5957
5958
5959 int
5960 opcode_funcUnit_use_stage (void *data, xtensa_opcode opcode, int idx)
5961 {
5962 xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx);
5963 return use->stage;
5964 }
5965
5966
5967 /* Note that this function does not check issue constraints, but
5968 solely whether the hardware is available to execute the given
5969 instructions together. It also doesn't check if the tinsns
5970 write the same state, or access the same tieports. That is
5971 checked by check_t1_t2_reads_and_writes. */
5972
5973 static bfd_boolean
5974 resources_conflict (vliw_insn *vinsn)
5975 {
5976 int i;
5977 static resource_table *rt = NULL;
5978
5979 /* This is the most common case by far. Optimize it. */
5980 if (vinsn->num_slots == 1)
5981 return FALSE;
5982
5983 if (rt == NULL)
5984 {
5985 xtensa_isa isa = xtensa_default_isa;
5986 rt = new_resource_table
5987 (isa, xtensa_isa_num_pipe_stages (isa),
5988 xtensa_isa_num_funcUnits (isa),
5989 (unit_num_copies_func) xtensa_funcUnit_num_copies,
5990 (opcode_num_units_func) xtensa_opcode_num_funcUnit_uses,
5991 opcode_funcUnit_use_unit,
5992 opcode_funcUnit_use_stage);
5993 }
5994
5995 clear_resource_table (rt);
5996
5997 for (i = 0; i < vinsn->num_slots; i++)
5998 {
5999 if (!resources_available (rt, vinsn->slots[i].opcode, 0))
6000 return TRUE;
6001 reserve_resources (rt, vinsn->slots[i].opcode, 0);
6002 }
6003
6004 return FALSE;
6005 }
6006
6007 \f
6008 /* finish_vinsn, emit_single_op and helper functions. */
6009
6010 static bfd_boolean find_vinsn_conflicts (vliw_insn *);
6011 static xtensa_format xg_find_narrowest_format (vliw_insn *);
6012 static void xg_assemble_vliw_tokens (vliw_insn *);
6013
6014
6015 /* We have reached the end of a bundle; emit into the frag. */
6016
6017 static void
6018 finish_vinsn (vliw_insn *vinsn)
6019 {
6020 IStack slotstack;
6021 int i;
6022 char *file_name;
6023 unsigned line;
6024
6025 if (find_vinsn_conflicts (vinsn))
6026 {
6027 xg_clear_vinsn (vinsn);
6028 return;
6029 }
6030
6031 /* First, find a format that works. */
6032 if (vinsn->format == XTENSA_UNDEFINED)
6033 vinsn->format = xg_find_narrowest_format (vinsn);
6034
6035 if (vinsn->format == XTENSA_UNDEFINED)
6036 {
6037 as_where (&file_name, &line);
6038 as_bad_where (file_name, line,
6039 _("couldn't find a valid instruction format"));
6040 fprintf (stderr, _(" ops were: "));
6041 for (i = 0; i < vinsn->num_slots; i++)
6042 fprintf (stderr, _(" %s;"),
6043 xtensa_opcode_name (xtensa_default_isa,
6044 vinsn->slots[i].opcode));
6045 fprintf (stderr, _("\n"));
6046 xg_clear_vinsn (vinsn);
6047 return;
6048 }
6049
6050 if (vinsn->num_slots
6051 != xtensa_format_num_slots (xtensa_default_isa, vinsn->format))
6052 {
6053 as_bad (_("format '%s' allows %d slots, but there are %d opcodes"),
6054 xtensa_format_name (xtensa_default_isa, vinsn->format),
6055 xtensa_format_num_slots (xtensa_default_isa, vinsn->format),
6056 vinsn->num_slots);
6057 xg_clear_vinsn (vinsn);
6058 return;
6059 }
6060
6061 if (resources_conflict (vinsn))
6062 {
6063 as_where (&file_name, &line);
6064 as_bad_where (file_name, line, _("illegal resource usage in bundle"));
6065 fprintf (stderr, " ops were: ");
6066 for (i = 0; i < vinsn->num_slots; i++)
6067 fprintf (stderr, " %s;",
6068 xtensa_opcode_name (xtensa_default_isa,
6069 vinsn->slots[i].opcode));
6070 fprintf (stderr, "\n");
6071 xg_clear_vinsn (vinsn);
6072 return;
6073 }
6074
6075 for (i = 0; i < vinsn->num_slots; i++)
6076 {
6077 if (vinsn->slots[i].opcode != XTENSA_UNDEFINED)
6078 {
6079 symbolS *lit_sym = NULL;
6080 int j;
6081 bfd_boolean e = FALSE;
6082 bfd_boolean saved_density = density_supported;
6083
6084 /* We don't want to narrow ops inside multi-slot bundles. */
6085 if (vinsn->num_slots > 1)
6086 density_supported = FALSE;
6087
6088 istack_init (&slotstack);
6089 if (vinsn->slots[i].opcode == xtensa_nop_opcode)
6090 {
6091 vinsn->slots[i].opcode =
6092 xtensa_format_slot_nop_opcode (xtensa_default_isa,
6093 vinsn->format, i);
6094 vinsn->slots[i].ntok = 0;
6095 }
6096
6097 if (xg_expand_assembly_insn (&slotstack, &vinsn->slots[i]))
6098 {
6099 e = TRUE;
6100 continue;
6101 }
6102
6103 density_supported = saved_density;
6104
6105 if (e)
6106 {
6107 xg_clear_vinsn (vinsn);
6108 return;
6109 }
6110
6111 for (j = 0; j < slotstack.ninsn; j++)
6112 {
6113 TInsn *insn = &slotstack.insn[j];
6114 if (insn->insn_type == ITYPE_LITERAL)
6115 {
6116 assert (lit_sym == NULL);
6117 lit_sym = xg_assemble_literal (insn);
6118 }
6119 else
6120 {
6121 assert (insn->insn_type == ITYPE_INSN);
6122 if (lit_sym)
6123 xg_resolve_literals (insn, lit_sym);
6124 if (j != slotstack.ninsn - 1)
6125 emit_single_op (insn);
6126 }
6127 }
6128
6129 if (vinsn->num_slots > 1)
6130 {
6131 if (opcode_fits_format_slot
6132 (slotstack.insn[slotstack.ninsn - 1].opcode,
6133 vinsn->format, i))
6134 {
6135 vinsn->slots[i] = slotstack.insn[slotstack.ninsn - 1];
6136 }
6137 else
6138 {
6139 emit_single_op (&slotstack.insn[slotstack.ninsn - 1]);
6140 if (vinsn->format == XTENSA_UNDEFINED)
6141 vinsn->slots[i].opcode = xtensa_nop_opcode;
6142 else
6143 vinsn->slots[i].opcode
6144 = xtensa_format_slot_nop_opcode (xtensa_default_isa,
6145 vinsn->format, i);
6146
6147 vinsn->slots[i].ntok = 0;
6148 }
6149 }
6150 else
6151 {
6152 vinsn->slots[0] = slotstack.insn[slotstack.ninsn - 1];
6153 vinsn->format = XTENSA_UNDEFINED;
6154 }
6155 }
6156 }
6157
6158 /* Now check resource conflicts on the modified bundle. */
6159 if (resources_conflict (vinsn))
6160 {
6161 as_where (&file_name, &line);
6162 as_bad_where (file_name, line, _("illegal resource usage in bundle"));
6163 fprintf (stderr, " ops were: ");
6164 for (i = 0; i < vinsn->num_slots; i++)
6165 fprintf (stderr, " %s;",
6166 xtensa_opcode_name (xtensa_default_isa,
6167 vinsn->slots[i].opcode));
6168 fprintf (stderr, "\n");
6169 xg_clear_vinsn (vinsn);
6170 return;
6171 }
6172
6173 /* First, find a format that works. */
6174 if (vinsn->format == XTENSA_UNDEFINED)
6175 vinsn->format = xg_find_narrowest_format (vinsn);
6176
6177 xg_assemble_vliw_tokens (vinsn);
6178
6179 xg_clear_vinsn (vinsn);
6180 }
6181
6182
6183 /* Given an vliw instruction, what conflicts are there in register
6184 usage and in writes to states and queues?
6185
6186 This function does two things:
6187 1. Reports an error when a vinsn contains illegal combinations
6188 of writes to registers states or queues.
6189 2. Marks individual tinsns as not relaxable if the combination
6190 contains antidependencies.
6191
6192 Job 2 handles things like swap semantics in instructions that need
6193 to be relaxed. For example,
6194
6195 addi a0, a1, 100000
6196
6197 normally would be relaxed to
6198
6199 l32r a0, some_label
6200 add a0, a1, a0
6201
6202 _but_, if the above instruction is bundled with an a0 reader, e.g.,
6203
6204 { addi a0, a1, 10000 ; add a2, a0, a4 ; }
6205
6206 then we can't relax it into
6207
6208 l32r a0, some_label
6209 { add a0, a1, a0 ; add a2, a0, a4 ; }
6210
6211 because the value of a0 is trashed before the second add can read it. */
6212
6213 static char check_t1_t2_reads_and_writes (TInsn *, TInsn *);
6214
6215 static bfd_boolean
6216 find_vinsn_conflicts (vliw_insn *vinsn)
6217 {
6218 int i, j;
6219 int branches = 0;
6220 xtensa_isa isa = xtensa_default_isa;
6221
6222 assert (!past_xtensa_end);
6223
6224 for (i = 0 ; i < vinsn->num_slots; i++)
6225 {
6226 TInsn *op1 = &vinsn->slots[i];
6227 if (op1->is_specific_opcode)
6228 op1->keep_wide = TRUE;
6229 else
6230 op1->keep_wide = FALSE;
6231 }
6232
6233 for (i = 0 ; i < vinsn->num_slots; i++)
6234 {
6235 TInsn *op1 = &vinsn->slots[i];
6236
6237 if (xtensa_opcode_is_branch (isa, op1->opcode) == 1)
6238 branches++;
6239
6240 for (j = 0; j < vinsn->num_slots; j++)
6241 {
6242 if (i != j)
6243 {
6244 TInsn *op2 = &vinsn->slots[j];
6245 char conflict_type = check_t1_t2_reads_and_writes (op1, op2);
6246 switch (conflict_type)
6247 {
6248 case 'c':
6249 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same register"),
6250 xtensa_opcode_name (isa, op1->opcode), i,
6251 xtensa_opcode_name (isa, op2->opcode), j);
6252 return TRUE;
6253 case 'd':
6254 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same state"),
6255 xtensa_opcode_name (isa, op1->opcode), i,
6256 xtensa_opcode_name (isa, op2->opcode), j);
6257 return TRUE;
6258 case 'e':
6259 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same port"),
6260 xtensa_opcode_name (isa, op1->opcode), i,
6261 xtensa_opcode_name (isa, op2->opcode), j);
6262 return TRUE;
6263 case 'f':
6264 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) both have volatile port accesses"),
6265 xtensa_opcode_name (isa, op1->opcode), i,
6266 xtensa_opcode_name (isa, op2->opcode), j);
6267 return TRUE;
6268 default:
6269 /* Everything is OK. */
6270 break;
6271 }
6272 op2->is_specific_opcode = (op2->is_specific_opcode
6273 || conflict_type == 'a');
6274 }
6275 }
6276 }
6277
6278 if (branches > 1)
6279 {
6280 as_bad (_("multiple branches or jumps in the same bundle"));
6281 return TRUE;
6282 }
6283
6284 return FALSE;
6285 }
6286
6287
6288 /* Check how the state used by t1 and t2 relate.
6289 Cases found are:
6290
6291 case A: t1 reads a register t2 writes (an antidependency within a bundle)
6292 case B: no relationship between what is read and written (both could
6293 read the same reg though)
6294 case C: t1 writes a register t2 writes (a register conflict within a
6295 bundle)
6296 case D: t1 writes a state that t2 also writes
6297 case E: t1 writes a tie queue that t2 also writes
6298 case F: two volatile queue accesses
6299 */
6300
6301 static char
6302 check_t1_t2_reads_and_writes (TInsn *t1, TInsn *t2)
6303 {
6304 xtensa_isa isa = xtensa_default_isa;
6305 xtensa_regfile t1_regfile, t2_regfile;
6306 int t1_reg, t2_reg;
6307 int t1_base_reg, t1_last_reg;
6308 int t2_base_reg, t2_last_reg;
6309 char t1_inout, t2_inout;
6310 int i, j;
6311 char conflict = 'b';
6312 int t1_states;
6313 int t2_states;
6314 int t1_interfaces;
6315 int t2_interfaces;
6316 bfd_boolean t1_volatile = FALSE;
6317 bfd_boolean t2_volatile = FALSE;
6318
6319 /* Check registers. */
6320 for (j = 0; j < t2->ntok; j++)
6321 {
6322 if (xtensa_operand_is_register (isa, t2->opcode, j) != 1)
6323 continue;
6324
6325 t2_regfile = xtensa_operand_regfile (isa, t2->opcode, j);
6326 t2_base_reg = t2->tok[j].X_add_number;
6327 t2_last_reg = t2_base_reg + xtensa_operand_num_regs (isa, t2->opcode, j);
6328
6329 for (i = 0; i < t1->ntok; i++)
6330 {
6331 if (xtensa_operand_is_register (isa, t1->opcode, i) != 1)
6332 continue;
6333
6334 t1_regfile = xtensa_operand_regfile (isa, t1->opcode, i);
6335
6336 if (t1_regfile != t2_regfile)
6337 continue;
6338
6339 t1_inout = xtensa_operand_inout (isa, t1->opcode, i);
6340 t2_inout = xtensa_operand_inout (isa, t2->opcode, j);
6341
6342 if (xtensa_operand_is_known_reg (isa, t1->opcode, i) == 0
6343 || xtensa_operand_is_known_reg (isa, t2->opcode, j) == 0)
6344 {
6345 if (t1_inout == 'm' || t1_inout == 'o'
6346 || t2_inout == 'm' || t2_inout == 'o')
6347 {
6348 conflict = 'a';
6349 continue;
6350 }
6351 }
6352
6353 t1_base_reg = t1->tok[i].X_add_number;
6354 t1_last_reg = (t1_base_reg
6355 + xtensa_operand_num_regs (isa, t1->opcode, i));
6356
6357 for (t1_reg = t1_base_reg; t1_reg < t1_last_reg; t1_reg++)
6358 {
6359 for (t2_reg = t2_base_reg; t2_reg < t2_last_reg; t2_reg++)
6360 {
6361 if (t1_reg != t2_reg)
6362 continue;
6363
6364 if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o'))
6365 {
6366 conflict = 'a';
6367 continue;
6368 }
6369
6370 if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o'))
6371 {
6372 conflict = 'a';
6373 continue;
6374 }
6375
6376 if (t1_inout != 'i' && t2_inout != 'i')
6377 return 'c';
6378 }
6379 }
6380 }
6381 }
6382
6383 /* Check states. */
6384 t1_states = xtensa_opcode_num_stateOperands (isa, t1->opcode);
6385 t2_states = xtensa_opcode_num_stateOperands (isa, t2->opcode);
6386 for (j = 0; j < t2_states; j++)
6387 {
6388 xtensa_state t2_so = xtensa_stateOperand_state (isa, t2->opcode, j);
6389 t2_inout = xtensa_stateOperand_inout (isa, t2->opcode, j);
6390 for (i = 0; i < t1_states; i++)
6391 {
6392 xtensa_state t1_so = xtensa_stateOperand_state (isa, t1->opcode, i);
6393 t1_inout = xtensa_stateOperand_inout (isa, t1->opcode, i);
6394 if (t1_so != t2_so)
6395 continue;
6396
6397 if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o'))
6398 {
6399 conflict = 'a';
6400 continue;
6401 }
6402
6403 if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o'))
6404 {
6405 conflict = 'a';
6406 continue;
6407 }
6408
6409 if (t1_inout != 'i' && t2_inout != 'i')
6410 return 'd';
6411 }
6412 }
6413
6414 /* Check tieports. */
6415 t1_interfaces = xtensa_opcode_num_interfaceOperands (isa, t1->opcode);
6416 t2_interfaces = xtensa_opcode_num_interfaceOperands (isa, t2->opcode);
6417 for (j = 0; j < t2_interfaces; j++)
6418 {
6419 xtensa_interface t2_int
6420 = xtensa_interfaceOperand_interface (isa, t2->opcode, j);
6421 int t2_class = xtensa_interface_class_id (isa, t2_int);
6422
6423 t2_inout = xtensa_interface_inout (isa, t2_int);
6424 if (xtensa_interface_has_side_effect (isa, t2_int) == 1)
6425 t2_volatile = TRUE;
6426
6427 for (i = 0; i < t1_interfaces; i++)
6428 {
6429 xtensa_interface t1_int
6430 = xtensa_interfaceOperand_interface (isa, t1->opcode, j);
6431 int t1_class = xtensa_interface_class_id (isa, t1_int);
6432
6433 t1_inout = xtensa_interface_inout (isa, t1_int);
6434 if (xtensa_interface_has_side_effect (isa, t1_int) == 1)
6435 t1_volatile = TRUE;
6436
6437 if (t1_volatile && t2_volatile && (t1_class == t2_class))
6438 return 'f';
6439
6440 if (t1_int != t2_int)
6441 continue;
6442
6443 if (t2_inout == 'i' && t1_inout == 'o')
6444 {
6445 conflict = 'a';
6446 continue;
6447 }
6448
6449 if (t1_inout == 'i' && t2_inout == 'o')
6450 {
6451 conflict = 'a';
6452 continue;
6453 }
6454
6455 if (t1_inout != 'i' && t2_inout != 'i')
6456 return 'e';
6457 }
6458 }
6459
6460 return conflict;
6461 }
6462
6463
6464 static xtensa_format
6465 xg_find_narrowest_format (vliw_insn *vinsn)
6466 {
6467 /* Right now we assume that the ops within the vinsn are properly
6468 ordered for the slots that the programmer wanted them in. In
6469 other words, we don't rearrange the ops in hopes of finding a
6470 better format. The scheduler handles that. */
6471
6472 xtensa_isa isa = xtensa_default_isa;
6473 xtensa_format format;
6474 vliw_insn v_copy = *vinsn;
6475 xtensa_opcode nop_opcode = xtensa_nop_opcode;
6476
6477 if (vinsn->num_slots == 1)
6478 return xg_get_single_format (vinsn->slots[0].opcode);
6479
6480 for (format = 0; format < xtensa_isa_num_formats (isa); format++)
6481 {
6482 v_copy = *vinsn;
6483 if (xtensa_format_num_slots (isa, format) == v_copy.num_slots)
6484 {
6485 int slot;
6486 int fit = 0;
6487 for (slot = 0; slot < v_copy.num_slots; slot++)
6488 {
6489 if (v_copy.slots[slot].opcode == nop_opcode)
6490 {
6491 v_copy.slots[slot].opcode =
6492 xtensa_format_slot_nop_opcode (isa, format, slot);
6493 v_copy.slots[slot].ntok = 0;
6494 }
6495
6496 if (opcode_fits_format_slot (v_copy.slots[slot].opcode,
6497 format, slot))
6498 fit++;
6499 else if (v_copy.num_slots > 1)
6500 {
6501 TInsn widened;
6502 /* Try the widened version. */
6503 if (!v_copy.slots[slot].keep_wide
6504 && !v_copy.slots[slot].is_specific_opcode
6505 && xg_is_single_relaxable_insn (&v_copy.slots[slot],
6506 &widened, TRUE)
6507 && opcode_fits_format_slot (widened.opcode,
6508 format, slot))
6509 {
6510 v_copy.slots[slot] = widened;
6511 fit++;
6512 }
6513 }
6514 }
6515 if (fit == v_copy.num_slots)
6516 {
6517 *vinsn = v_copy;
6518 xtensa_format_encode (isa, format, vinsn->insnbuf);
6519 vinsn->format = format;
6520 break;
6521 }
6522 }
6523 }
6524
6525 if (format == xtensa_isa_num_formats (isa))
6526 return XTENSA_UNDEFINED;
6527
6528 return format;
6529 }
6530
6531
6532 /* Return the additional space needed in a frag
6533 for possible relaxations of any ops in a VLIW insn.
6534 Also fill out the relaxations that might be required of
6535 each tinsn in the vinsn. */
6536
6537 static int
6538 relaxation_requirements (vliw_insn *vinsn, bfd_boolean *pfinish_frag)
6539 {
6540 bfd_boolean finish_frag = FALSE;
6541 int extra_space = 0;
6542 int slot;
6543
6544 for (slot = 0; slot < vinsn->num_slots; slot++)
6545 {
6546 TInsn *tinsn = &vinsn->slots[slot];
6547 if (!tinsn_has_symbolic_operands (tinsn))
6548 {
6549 /* A narrow instruction could be widened later to help
6550 alignment issues. */
6551 if (xg_is_single_relaxable_insn (tinsn, 0, TRUE)
6552 && !tinsn->is_specific_opcode
6553 && vinsn->num_slots == 1)
6554 {
6555 /* Difference in bytes between narrow and wide insns... */
6556 extra_space += 1;
6557 tinsn->subtype = RELAX_NARROW;
6558 }
6559 }
6560 else
6561 {
6562 if (workaround_b_j_loop_end
6563 && tinsn->opcode == xtensa_jx_opcode
6564 && use_transform ())
6565 {
6566 /* Add 2 of these. */
6567 extra_space += 3; /* for the nop size */
6568 tinsn->subtype = RELAX_ADD_NOP_IF_PRE_LOOP_END;
6569 }
6570
6571 /* Need to assemble it with space for the relocation. */
6572 if (xg_is_relaxable_insn (tinsn, 0)
6573 && !tinsn->is_specific_opcode)
6574 {
6575 int max_size = xg_get_max_insn_widen_size (tinsn->opcode);
6576 int max_literal_size =
6577 xg_get_max_insn_widen_literal_size (tinsn->opcode);
6578
6579 tinsn->literal_space = max_literal_size;
6580
6581 tinsn->subtype = RELAX_IMMED;
6582 extra_space += max_size;
6583 }
6584 else
6585 {
6586 /* A fix record will be added for this instruction prior
6587 to relaxation, so make it end the frag. */
6588 finish_frag = TRUE;
6589 }
6590 }
6591 }
6592 *pfinish_frag = finish_frag;
6593 return extra_space;
6594 }
6595
6596
6597 static void
6598 bundle_tinsn (TInsn *tinsn, vliw_insn *vinsn)
6599 {
6600 xtensa_isa isa = xtensa_default_isa;
6601 int slot, chosen_slot;
6602
6603 vinsn->format = xg_get_single_format (tinsn->opcode);
6604 assert (vinsn->format != XTENSA_UNDEFINED);
6605 vinsn->num_slots = xtensa_format_num_slots (isa, vinsn->format);
6606
6607 chosen_slot = xg_get_single_slot (tinsn->opcode);
6608 for (slot = 0; slot < vinsn->num_slots; slot++)
6609 {
6610 if (slot == chosen_slot)
6611 vinsn->slots[slot] = *tinsn;
6612 else
6613 {
6614 vinsn->slots[slot].opcode =
6615 xtensa_format_slot_nop_opcode (isa, vinsn->format, slot);
6616 vinsn->slots[slot].ntok = 0;
6617 vinsn->slots[slot].insn_type = ITYPE_INSN;
6618 }
6619 }
6620 }
6621
6622
6623 static bfd_boolean
6624 emit_single_op (TInsn *orig_insn)
6625 {
6626 int i;
6627 IStack istack; /* put instructions into here */
6628 symbolS *lit_sym = NULL;
6629 symbolS *label_sym = NULL;
6630
6631 istack_init (&istack);
6632
6633 /* Special-case for "movi aX, foo" which is guaranteed to need relaxing.
6634 Because the scheduling and bundling characteristics of movi and
6635 l32r or const16 are so different, we can do much better if we relax
6636 it prior to scheduling and bundling, rather than after. */
6637 if ((orig_insn->opcode == xtensa_movi_opcode
6638 || orig_insn->opcode == xtensa_movi_n_opcode)
6639 && !cur_vinsn.inside_bundle
6640 && (orig_insn->tok[1].X_op == O_symbol
6641 || orig_insn->tok[1].X_op == O_pltrel)
6642 && !orig_insn->is_specific_opcode && use_transform ())
6643 xg_assembly_relax (&istack, orig_insn, now_seg, frag_now, 0, 1, 0);
6644 else
6645 if (xg_expand_assembly_insn (&istack, orig_insn))
6646 return TRUE;
6647
6648 for (i = 0; i < istack.ninsn; i++)
6649 {
6650 TInsn *insn = &istack.insn[i];
6651 switch (insn->insn_type)
6652 {
6653 case ITYPE_LITERAL:
6654 assert (lit_sym == NULL);
6655 lit_sym = xg_assemble_literal (insn);
6656 break;
6657 case ITYPE_LABEL:
6658 {
6659 static int relaxed_sym_idx = 0;
6660 char *label = xmalloc (strlen (FAKE_LABEL_NAME) + 12);
6661 sprintf (label, "%s_rl_%x", FAKE_LABEL_NAME, relaxed_sym_idx++);
6662 colon (label);
6663 assert (label_sym == NULL);
6664 label_sym = symbol_find_or_make (label);
6665 assert (label_sym);
6666 free (label);
6667 }
6668 break;
6669 case ITYPE_INSN:
6670 {
6671 vliw_insn v;
6672 if (lit_sym)
6673 xg_resolve_literals (insn, lit_sym);
6674 if (label_sym)
6675 xg_resolve_labels (insn, label_sym);
6676 xg_init_vinsn (&v);
6677 bundle_tinsn (insn, &v);
6678 finish_vinsn (&v);
6679 xg_free_vinsn (&v);
6680 }
6681 break;
6682 default:
6683 assert (0);
6684 break;
6685 }
6686 }
6687 return FALSE;
6688 }
6689
6690
6691 static int
6692 total_frag_text_expansion (fragS *fragP)
6693 {
6694 int slot;
6695 int total_expansion = 0;
6696
6697 for (slot = 0; slot < MAX_SLOTS; slot++)
6698 total_expansion += fragP->tc_frag_data.text_expansion[slot];
6699
6700 return total_expansion;
6701 }
6702
6703
6704 /* Emit a vliw instruction to the current fragment. */
6705
6706 static void
6707 xg_assemble_vliw_tokens (vliw_insn *vinsn)
6708 {
6709 bfd_boolean finish_frag;
6710 bfd_boolean is_jump = FALSE;
6711 bfd_boolean is_branch = FALSE;
6712 xtensa_isa isa = xtensa_default_isa;
6713 int insn_size;
6714 int extra_space;
6715 char *f = NULL;
6716 int slot;
6717 struct dwarf2_line_info debug_line;
6718 bfd_boolean loc_directive_seen = FALSE;
6719 TInsn *tinsn;
6720
6721 memset (&debug_line, 0, sizeof (struct dwarf2_line_info));
6722
6723 if (generating_literals)
6724 {
6725 static int reported = 0;
6726 if (reported < 4)
6727 as_bad_where (frag_now->fr_file, frag_now->fr_line,
6728 _("cannot assemble into a literal fragment"));
6729 if (reported == 3)
6730 as_bad (_("..."));
6731 reported++;
6732 return;
6733 }
6734
6735 if (frag_now_fix () != 0
6736 && (! frag_now->tc_frag_data.is_insn
6737 || (vinsn_has_specific_opcodes (vinsn) && use_transform ())
6738 || !use_transform () != frag_now->tc_frag_data.is_no_transform
6739 || (directive_state[directive_longcalls]
6740 != frag_now->tc_frag_data.use_longcalls)
6741 || (directive_state[directive_absolute_literals]
6742 != frag_now->tc_frag_data.use_absolute_literals)))
6743 {
6744 frag_wane (frag_now);
6745 frag_new (0);
6746 xtensa_set_frag_assembly_state (frag_now);
6747 }
6748
6749 if (workaround_a0_b_retw
6750 && vinsn->num_slots == 1
6751 && (get_last_insn_flags (now_seg, now_subseg) & FLAG_IS_A0_WRITER) != 0
6752 && xtensa_opcode_is_branch (isa, vinsn->slots[0].opcode) == 1
6753 && use_transform ())
6754 {
6755 has_a0_b_retw = TRUE;
6756
6757 /* Mark this fragment with the special RELAX_ADD_NOP_IF_A0_B_RETW.
6758 After the first assembly pass we will check all of them and
6759 add a nop if needed. */
6760 frag_now->tc_frag_data.is_insn = TRUE;
6761 frag_var (rs_machine_dependent, 4, 4,
6762 RELAX_ADD_NOP_IF_A0_B_RETW,
6763 frag_now->fr_symbol,
6764 frag_now->fr_offset,
6765 NULL);
6766 xtensa_set_frag_assembly_state (frag_now);
6767 frag_now->tc_frag_data.is_insn = TRUE;
6768 frag_var (rs_machine_dependent, 4, 4,
6769 RELAX_ADD_NOP_IF_A0_B_RETW,
6770 frag_now->fr_symbol,
6771 frag_now->fr_offset,
6772 NULL);
6773 xtensa_set_frag_assembly_state (frag_now);
6774 }
6775
6776 for (slot = 0; slot < vinsn->num_slots; slot++)
6777 {
6778 tinsn = &vinsn->slots[slot];
6779
6780 /* See if the instruction implies an aligned section. */
6781 if (xtensa_opcode_is_loop (isa, tinsn->opcode) == 1)
6782 record_alignment (now_seg, 2);
6783
6784 /* Determine the best line number for debug info. */
6785 if ((tinsn->loc_directive_seen || !loc_directive_seen)
6786 && (tinsn->debug_line.filenum != debug_line.filenum
6787 || tinsn->debug_line.line < debug_line.line
6788 || tinsn->debug_line.column < debug_line.column))
6789 debug_line = tinsn->debug_line;
6790 if (tinsn->loc_directive_seen)
6791 loc_directive_seen = TRUE;
6792 }
6793
6794 /* Special cases for instructions that force an alignment... */
6795 /* None of these opcodes are bundle-able. */
6796 if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode) == 1)
6797 {
6798 int max_fill;
6799
6800 /* Remember the symbol that marks the end of the loop in the frag
6801 that marks the start of the loop. This way we can easily find
6802 the end of the loop at the beginning, without adding special code
6803 to mark the loop instructions themselves. */
6804 symbolS *target_sym = NULL;
6805 if (vinsn->slots[0].tok[1].X_op == O_symbol)
6806 target_sym = vinsn->slots[0].tok[1].X_add_symbol;
6807
6808 xtensa_set_frag_assembly_state (frag_now);
6809 frag_now->tc_frag_data.is_insn = TRUE;
6810
6811 max_fill = get_text_align_max_fill_size
6812 (get_text_align_power (xtensa_fetch_width),
6813 TRUE, frag_now->tc_frag_data.is_no_density);
6814
6815 if (use_transform ())
6816 frag_var (rs_machine_dependent, max_fill, max_fill,
6817 RELAX_ALIGN_NEXT_OPCODE, target_sym, 0, NULL);
6818 else
6819 frag_var (rs_machine_dependent, 0, 0,
6820 RELAX_CHECK_ALIGN_NEXT_OPCODE, target_sym, 0, NULL);
6821 xtensa_set_frag_assembly_state (frag_now);
6822 }
6823
6824 if (vinsn->slots[0].opcode == xtensa_entry_opcode
6825 && !vinsn->slots[0].is_specific_opcode)
6826 {
6827 xtensa_mark_literal_pool_location ();
6828 xtensa_move_labels (frag_now, 0);
6829 frag_var (rs_align_test, 1, 1, 0, NULL, 2, NULL);
6830 }
6831
6832 if (vinsn->num_slots == 1)
6833 {
6834 if (workaround_a0_b_retw && use_transform ())
6835 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_A0_WRITER,
6836 is_register_writer (&vinsn->slots[0], "a", 0));
6837
6838 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND,
6839 is_bad_loopend_opcode (&vinsn->slots[0]));
6840 }
6841 else
6842 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND, FALSE);
6843
6844 insn_size = xtensa_format_length (isa, vinsn->format);
6845
6846 extra_space = relaxation_requirements (vinsn, &finish_frag);
6847
6848 /* vinsn_to_insnbuf will produce the error. */
6849 if (vinsn->format != XTENSA_UNDEFINED)
6850 {
6851 f = frag_more (insn_size + extra_space);
6852 xtensa_set_frag_assembly_state (frag_now);
6853 frag_now->tc_frag_data.is_insn = TRUE;
6854 }
6855
6856 vinsn_to_insnbuf (vinsn, f, frag_now, FALSE);
6857 if (vinsn->format == XTENSA_UNDEFINED)
6858 return;
6859
6860 xtensa_insnbuf_to_chars (isa, vinsn->insnbuf, (unsigned char *) f, 0);
6861
6862 if (debug_type == DEBUG_DWARF2 || loc_directive_seen)
6863 dwarf2_gen_line_info (frag_now_fix () - (insn_size + extra_space),
6864 &debug_line);
6865
6866 for (slot = 0; slot < vinsn->num_slots; slot++)
6867 {
6868 tinsn = &vinsn->slots[slot];
6869 frag_now->tc_frag_data.slot_subtypes[slot] = tinsn->subtype;
6870 frag_now->tc_frag_data.slot_symbols[slot] = tinsn->symbol;
6871 frag_now->tc_frag_data.slot_offsets[slot] = tinsn->offset;
6872 frag_now->tc_frag_data.literal_frags[slot] = tinsn->literal_frag;
6873 if (tinsn->literal_space != 0)
6874 xg_assemble_literal_space (tinsn->literal_space, slot);
6875
6876 if (tinsn->subtype == RELAX_NARROW)
6877 assert (vinsn->num_slots == 1);
6878 if (xtensa_opcode_is_jump (isa, tinsn->opcode) == 1)
6879 is_jump = TRUE;
6880 if (xtensa_opcode_is_branch (isa, tinsn->opcode) == 1)
6881 is_branch = TRUE;
6882
6883 if (tinsn->subtype || tinsn->symbol || tinsn->offset
6884 || tinsn->literal_frag || is_jump || is_branch)
6885 finish_frag = TRUE;
6886 }
6887
6888 if (vinsn_has_specific_opcodes (vinsn) && use_transform ())
6889 frag_now->tc_frag_data.is_specific_opcode = TRUE;
6890
6891 if (finish_frag)
6892 {
6893 frag_variant (rs_machine_dependent,
6894 extra_space, extra_space, RELAX_SLOTS,
6895 frag_now->fr_symbol, frag_now->fr_offset, f);
6896 xtensa_set_frag_assembly_state (frag_now);
6897 }
6898
6899 /* Special cases for loops:
6900 close_loop_end should be inserted AFTER short_loop.
6901 Make sure that CLOSE loops are processed BEFORE short_loops
6902 when converting them. */
6903
6904 /* "short_loop": Add a NOP if the loop is < 4 bytes. */
6905 if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode) == 1
6906 && !vinsn->slots[0].is_specific_opcode)
6907 {
6908 if (workaround_short_loop && use_transform ())
6909 {
6910 maybe_has_short_loop = TRUE;
6911 frag_now->tc_frag_data.is_insn = TRUE;
6912 frag_var (rs_machine_dependent, 4, 4,
6913 RELAX_ADD_NOP_IF_SHORT_LOOP,
6914 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6915 frag_now->tc_frag_data.is_insn = TRUE;
6916 frag_var (rs_machine_dependent, 4, 4,
6917 RELAX_ADD_NOP_IF_SHORT_LOOP,
6918 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6919 }
6920
6921 /* "close_loop_end": Add up to 12 bytes of NOPs to keep a
6922 loop at least 12 bytes away from another loop's end. */
6923 if (workaround_close_loop_end && use_transform ())
6924 {
6925 maybe_has_close_loop_end = TRUE;
6926 frag_now->tc_frag_data.is_insn = TRUE;
6927 frag_var (rs_machine_dependent, 12, 12,
6928 RELAX_ADD_NOP_IF_CLOSE_LOOP_END,
6929 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6930 }
6931 }
6932
6933 if (use_transform ())
6934 {
6935 if (is_jump)
6936 {
6937 assert (finish_frag);
6938 frag_var (rs_machine_dependent,
6939 UNREACHABLE_MAX_WIDTH, UNREACHABLE_MAX_WIDTH,
6940 RELAX_UNREACHABLE,
6941 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6942 xtensa_set_frag_assembly_state (frag_now);
6943 }
6944 else if (is_branch && do_align_targets ())
6945 {
6946 assert (finish_frag);
6947 frag_var (rs_machine_dependent,
6948 UNREACHABLE_MAX_WIDTH, UNREACHABLE_MAX_WIDTH,
6949 RELAX_MAYBE_UNREACHABLE,
6950 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6951 xtensa_set_frag_assembly_state (frag_now);
6952 frag_var (rs_machine_dependent,
6953 0, 0,
6954 RELAX_MAYBE_DESIRE_ALIGN,
6955 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6956 xtensa_set_frag_assembly_state (frag_now);
6957 }
6958 }
6959
6960 /* Now, if the original opcode was a call... */
6961 if (do_align_targets ()
6962 && xtensa_opcode_is_call (isa, vinsn->slots[0].opcode) == 1)
6963 {
6964 float freq = get_subseg_total_freq (now_seg, now_subseg);
6965 frag_now->tc_frag_data.is_insn = TRUE;
6966 frag_var (rs_machine_dependent, 4, (int) freq, RELAX_DESIRE_ALIGN,
6967 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6968 xtensa_set_frag_assembly_state (frag_now);
6969 }
6970
6971 if (vinsn_has_specific_opcodes (vinsn) && use_transform ())
6972 {
6973 frag_wane (frag_now);
6974 frag_new (0);
6975 xtensa_set_frag_assembly_state (frag_now);
6976 }
6977 }
6978
6979 \f
6980 /* xtensa_end and helper functions. */
6981
6982 static void xtensa_cleanup_align_frags (void);
6983 static void xtensa_fix_target_frags (void);
6984 static void xtensa_mark_narrow_branches (void);
6985 static void xtensa_mark_zcl_first_insns (void);
6986 static void xtensa_mark_difference_of_two_symbols (void);
6987 static void xtensa_fix_a0_b_retw_frags (void);
6988 static void xtensa_fix_b_j_loop_end_frags (void);
6989 static void xtensa_fix_close_loop_end_frags (void);
6990 static void xtensa_fix_short_loop_frags (void);
6991 static void xtensa_sanity_check (void);
6992 static void xtensa_add_config_info (void);
6993
6994 void
6995 xtensa_end (void)
6996 {
6997 directive_balance ();
6998 xtensa_flush_pending_output ();
6999
7000 past_xtensa_end = TRUE;
7001
7002 xtensa_move_literals ();
7003
7004 xtensa_reorder_segments ();
7005 xtensa_cleanup_align_frags ();
7006 xtensa_fix_target_frags ();
7007 if (workaround_a0_b_retw && has_a0_b_retw)
7008 xtensa_fix_a0_b_retw_frags ();
7009 if (workaround_b_j_loop_end)
7010 xtensa_fix_b_j_loop_end_frags ();
7011
7012 /* "close_loop_end" should be processed BEFORE "short_loop". */
7013 if (workaround_close_loop_end && maybe_has_close_loop_end)
7014 xtensa_fix_close_loop_end_frags ();
7015
7016 if (workaround_short_loop && maybe_has_short_loop)
7017 xtensa_fix_short_loop_frags ();
7018 if (align_targets)
7019 xtensa_mark_narrow_branches ();
7020 xtensa_mark_zcl_first_insns ();
7021
7022 xtensa_sanity_check ();
7023
7024 xtensa_add_config_info ();
7025 }
7026
7027
7028 static void
7029 xtensa_cleanup_align_frags (void)
7030 {
7031 frchainS *frchP;
7032 asection *s;
7033
7034 for (s = stdoutput->sections; s; s = s->next)
7035 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7036 {
7037 fragS *fragP;
7038 /* Walk over all of the fragments in a subsection. */
7039 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7040 {
7041 if ((fragP->fr_type == rs_align
7042 || fragP->fr_type == rs_align_code
7043 || (fragP->fr_type == rs_machine_dependent
7044 && (fragP->fr_subtype == RELAX_DESIRE_ALIGN
7045 || fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)))
7046 && fragP->fr_fix == 0)
7047 {
7048 fragS *next = fragP->fr_next;
7049
7050 while (next
7051 && next->fr_fix == 0
7052 && next->fr_type == rs_machine_dependent
7053 && next->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
7054 {
7055 frag_wane (next);
7056 next = next->fr_next;
7057 }
7058 }
7059 /* If we don't widen branch targets, then they
7060 will be easier to align. */
7061 if (fragP->tc_frag_data.is_branch_target
7062 && fragP->fr_opcode == fragP->fr_literal
7063 && fragP->fr_type == rs_machine_dependent
7064 && fragP->fr_subtype == RELAX_SLOTS
7065 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
7066 frag_wane (fragP);
7067 if (fragP->fr_type == rs_machine_dependent
7068 && fragP->fr_subtype == RELAX_UNREACHABLE)
7069 fragP->tc_frag_data.is_unreachable = TRUE;
7070 }
7071 }
7072 }
7073
7074
7075 /* Re-process all of the fragments looking to convert all of the
7076 RELAX_DESIRE_ALIGN_IF_TARGET fragments. If there is a branch
7077 target in the next fragment, convert this to RELAX_DESIRE_ALIGN.
7078 Otherwise, convert to a .fill 0. */
7079
7080 static void
7081 xtensa_fix_target_frags (void)
7082 {
7083 frchainS *frchP;
7084 asection *s;
7085
7086 /* When this routine is called, all of the subsections are still intact
7087 so we walk over subsections instead of sections. */
7088 for (s = stdoutput->sections; s; s = s->next)
7089 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7090 {
7091 fragS *fragP;
7092
7093 /* Walk over all of the fragments in a subsection. */
7094 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7095 {
7096 if (fragP->fr_type == rs_machine_dependent
7097 && fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
7098 {
7099 if (next_frag_is_branch_target (fragP))
7100 fragP->fr_subtype = RELAX_DESIRE_ALIGN;
7101 else
7102 frag_wane (fragP);
7103 }
7104 }
7105 }
7106 }
7107
7108
7109 static bfd_boolean is_narrow_branch_guaranteed_in_range (fragS *, TInsn *);
7110
7111 static void
7112 xtensa_mark_narrow_branches (void)
7113 {
7114 frchainS *frchP;
7115 asection *s;
7116
7117 for (s = stdoutput->sections; s; s = s->next)
7118 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7119 {
7120 fragS *fragP;
7121 /* Walk over all of the fragments in a subsection. */
7122 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7123 {
7124 if (fragP->fr_type == rs_machine_dependent
7125 && fragP->fr_subtype == RELAX_SLOTS
7126 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED)
7127 {
7128 vliw_insn vinsn;
7129
7130 vinsn_from_chars (&vinsn, fragP->fr_opcode);
7131 tinsn_immed_from_frag (&vinsn.slots[0], fragP, 0);
7132
7133 if (vinsn.num_slots == 1
7134 && xtensa_opcode_is_branch (xtensa_default_isa,
7135 vinsn.slots[0].opcode) == 1
7136 && xg_get_single_size (vinsn.slots[0].opcode) == 2
7137 && is_narrow_branch_guaranteed_in_range (fragP,
7138 &vinsn.slots[0]))
7139 {
7140 fragP->fr_subtype = RELAX_SLOTS;
7141 fragP->tc_frag_data.slot_subtypes[0] = RELAX_NARROW;
7142 fragP->tc_frag_data.is_aligning_branch = 1;
7143 }
7144 }
7145 }
7146 }
7147 }
7148
7149
7150 /* A branch is typically widened only when its target is out of
7151 range. However, we would like to widen them to align a subsequent
7152 branch target when possible.
7153
7154 Because the branch relaxation code is so convoluted, the optimal solution
7155 (combining the two cases) is difficult to get right in all circumstances.
7156 We therefore go with an "almost as good" solution, where we only
7157 use for alignment narrow branches that definitely will not expand to a
7158 jump and a branch. These functions find and mark these cases. */
7159
7160 /* The range in bytes of BNEZ.N and BEQZ.N. The target operand is encoded
7161 as PC + 4 + imm6, where imm6 is a 6-bit immediate ranging from 0 to 63.
7162 We start counting beginning with the frag after the 2-byte branch, so the
7163 maximum offset is (4 - 2) + 63 = 65. */
7164 #define MAX_IMMED6 65
7165
7166 static offsetT unrelaxed_frag_max_size (fragS *);
7167
7168 static bfd_boolean
7169 is_narrow_branch_guaranteed_in_range (fragS *fragP, TInsn *tinsn)
7170 {
7171 const expressionS *expr = &tinsn->tok[1];
7172 symbolS *symbolP = expr->X_add_symbol;
7173 offsetT max_distance = expr->X_add_number;
7174 fragS *target_frag;
7175
7176 if (expr->X_op != O_symbol)
7177 return FALSE;
7178
7179 target_frag = symbol_get_frag (symbolP);
7180
7181 max_distance += (S_GET_VALUE (symbolP) - target_frag->fr_address);
7182 if (is_branch_jmp_to_next (tinsn, fragP))
7183 return FALSE;
7184
7185 /* The branch doesn't branch over it's own frag,
7186 but over the subsequent ones. */
7187 fragP = fragP->fr_next;
7188 while (fragP != NULL && fragP != target_frag && max_distance <= MAX_IMMED6)
7189 {
7190 max_distance += unrelaxed_frag_max_size (fragP);
7191 fragP = fragP->fr_next;
7192 }
7193 if (max_distance <= MAX_IMMED6 && fragP == target_frag)
7194 return TRUE;
7195 return FALSE;
7196 }
7197
7198
7199 static void
7200 xtensa_mark_zcl_first_insns (void)
7201 {
7202 frchainS *frchP;
7203 asection *s;
7204
7205 for (s = stdoutput->sections; s; s = s->next)
7206 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7207 {
7208 fragS *fragP;
7209 /* Walk over all of the fragments in a subsection. */
7210 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7211 {
7212 if (fragP->fr_type == rs_machine_dependent
7213 && (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE
7214 || fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE))
7215 {
7216 /* Find the loop frag. */
7217 fragS *targ_frag = next_non_empty_frag (fragP);
7218 /* Find the first insn frag. */
7219 targ_frag = next_non_empty_frag (targ_frag);
7220
7221 /* Of course, sometimes (mostly for toy test cases) a
7222 zero-cost loop instruction is the last in a section. */
7223 if (targ_frag)
7224 {
7225 targ_frag->tc_frag_data.is_first_loop_insn = TRUE;
7226 /* Do not widen a frag that is the first instruction of a
7227 zero-cost loop. It makes that loop harder to align. */
7228 if (targ_frag->fr_type == rs_machine_dependent
7229 && targ_frag->fr_subtype == RELAX_SLOTS
7230 && (targ_frag->tc_frag_data.slot_subtypes[0]
7231 == RELAX_NARROW))
7232 {
7233 if (targ_frag->tc_frag_data.is_aligning_branch)
7234 targ_frag->tc_frag_data.slot_subtypes[0] = RELAX_IMMED;
7235 else
7236 {
7237 frag_wane (targ_frag);
7238 targ_frag->tc_frag_data.slot_subtypes[0] = 0;
7239 }
7240 }
7241 }
7242 if (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)
7243 frag_wane (fragP);
7244 }
7245 }
7246 }
7247 }
7248
7249
7250 /* Some difference-of-symbols expressions make it out to the linker. Some
7251 don't. If one does, then the linker can optimize between the two labels.
7252 If it doesn't, then the linker shouldn't. */
7253
7254 static void
7255 xtensa_mark_difference_of_two_symbols (void)
7256 {
7257 symbolS *expr_sym;
7258
7259 for (expr_sym = expr_symbols; expr_sym;
7260 expr_sym = symbol_get_tc (expr_sym)->next_expr_symbol)
7261 {
7262 expressionS *expr = symbol_get_value_expression (expr_sym);
7263
7264 if (expr->X_op == O_subtract)
7265 {
7266 symbolS *left = expr->X_add_symbol;
7267 symbolS *right = expr->X_op_symbol;
7268
7269 /* Difference of two symbols not in the same section
7270 are handled with relocations in the linker. */
7271 if (S_GET_SEGMENT (left) == S_GET_SEGMENT (right))
7272 {
7273 fragS *start;
7274 fragS *end;
7275
7276 if (symbol_get_frag (left)->fr_address
7277 <= symbol_get_frag (right)->fr_address)
7278 {
7279 start = symbol_get_frag (left);
7280 end = symbol_get_frag (right);
7281 }
7282 else
7283 {
7284 start = symbol_get_frag (right);
7285 end = symbol_get_frag (left);
7286 }
7287 do
7288 {
7289 start->tc_frag_data.is_no_transform = 1;
7290 start = start->fr_next;
7291 }
7292 while (start && start->fr_address < end->fr_address);
7293 }
7294 }
7295 }
7296 }
7297
7298
7299 /* Re-process all of the fragments looking to convert all of the
7300 RELAX_ADD_NOP_IF_A0_B_RETW. If the next instruction is a
7301 conditional branch or a retw/retw.n, convert this frag to one that
7302 will generate a NOP. In any case close it off with a .fill 0. */
7303
7304 static bfd_boolean next_instrs_are_b_retw (fragS *);
7305
7306 static void
7307 xtensa_fix_a0_b_retw_frags (void)
7308 {
7309 frchainS *frchP;
7310 asection *s;
7311
7312 /* When this routine is called, all of the subsections are still intact
7313 so we walk over subsections instead of sections. */
7314 for (s = stdoutput->sections; s; s = s->next)
7315 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7316 {
7317 fragS *fragP;
7318
7319 /* Walk over all of the fragments in a subsection. */
7320 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7321 {
7322 if (fragP->fr_type == rs_machine_dependent
7323 && fragP->fr_subtype == RELAX_ADD_NOP_IF_A0_B_RETW)
7324 {
7325 if (next_instrs_are_b_retw (fragP))
7326 {
7327 if (fragP->tc_frag_data.is_no_transform)
7328 as_bad (_("instruction sequence (write a0, branch, retw) may trigger hardware errata"));
7329 else
7330 relax_frag_add_nop (fragP);
7331 }
7332 frag_wane (fragP);
7333 }
7334 }
7335 }
7336 }
7337
7338
7339 static bfd_boolean
7340 next_instrs_are_b_retw (fragS *fragP)
7341 {
7342 xtensa_opcode opcode;
7343 xtensa_format fmt;
7344 const fragS *next_fragP = next_non_empty_frag (fragP);
7345 static xtensa_insnbuf insnbuf = NULL;
7346 static xtensa_insnbuf slotbuf = NULL;
7347 xtensa_isa isa = xtensa_default_isa;
7348 int offset = 0;
7349 int slot;
7350 bfd_boolean branch_seen = FALSE;
7351
7352 if (!insnbuf)
7353 {
7354 insnbuf = xtensa_insnbuf_alloc (isa);
7355 slotbuf = xtensa_insnbuf_alloc (isa);
7356 }
7357
7358 if (next_fragP == NULL)
7359 return FALSE;
7360
7361 /* Check for the conditional branch. */
7362 xtensa_insnbuf_from_chars
7363 (isa, insnbuf, (unsigned char *) &next_fragP->fr_literal[offset], 0);
7364 fmt = xtensa_format_decode (isa, insnbuf);
7365 if (fmt == XTENSA_UNDEFINED)
7366 return FALSE;
7367
7368 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
7369 {
7370 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
7371 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
7372
7373 branch_seen = (branch_seen
7374 || xtensa_opcode_is_branch (isa, opcode) == 1);
7375 }
7376
7377 if (!branch_seen)
7378 return FALSE;
7379
7380 offset += xtensa_format_length (isa, fmt);
7381 if (offset == next_fragP->fr_fix)
7382 {
7383 next_fragP = next_non_empty_frag (next_fragP);
7384 offset = 0;
7385 }
7386
7387 if (next_fragP == NULL)
7388 return FALSE;
7389
7390 /* Check for the retw/retw.n. */
7391 xtensa_insnbuf_from_chars
7392 (isa, insnbuf, (unsigned char *) &next_fragP->fr_literal[offset], 0);
7393 fmt = xtensa_format_decode (isa, insnbuf);
7394
7395 /* Because RETW[.N] is not bundleable, a VLIW bundle here means that we
7396 have no problems. */
7397 if (fmt == XTENSA_UNDEFINED
7398 || xtensa_format_num_slots (isa, fmt) != 1)
7399 return FALSE;
7400
7401 xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf);
7402 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
7403
7404 if (opcode == xtensa_retw_opcode || opcode == xtensa_retw_n_opcode)
7405 return TRUE;
7406
7407 return FALSE;
7408 }
7409
7410
7411 /* Re-process all of the fragments looking to convert all of the
7412 RELAX_ADD_NOP_IF_PRE_LOOP_END. If there is one instruction and a
7413 loop end label, convert this frag to one that will generate a NOP.
7414 In any case close it off with a .fill 0. */
7415
7416 static bfd_boolean next_instr_is_loop_end (fragS *);
7417
7418 static void
7419 xtensa_fix_b_j_loop_end_frags (void)
7420 {
7421 frchainS *frchP;
7422 asection *s;
7423
7424 /* When this routine is called, all of the subsections are still intact
7425 so we walk over subsections instead of sections. */
7426 for (s = stdoutput->sections; s; s = s->next)
7427 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7428 {
7429 fragS *fragP;
7430
7431 /* Walk over all of the fragments in a subsection. */
7432 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7433 {
7434 if (fragP->fr_type == rs_machine_dependent
7435 && fragP->fr_subtype == RELAX_ADD_NOP_IF_PRE_LOOP_END)
7436 {
7437 if (next_instr_is_loop_end (fragP))
7438 {
7439 if (fragP->tc_frag_data.is_no_transform)
7440 as_bad (_("branching or jumping to a loop end may trigger hardware errata"));
7441 else
7442 relax_frag_add_nop (fragP);
7443 }
7444 frag_wane (fragP);
7445 }
7446 }
7447 }
7448 }
7449
7450
7451 static bfd_boolean
7452 next_instr_is_loop_end (fragS *fragP)
7453 {
7454 const fragS *next_fragP;
7455
7456 if (next_frag_is_loop_target (fragP))
7457 return FALSE;
7458
7459 next_fragP = next_non_empty_frag (fragP);
7460 if (next_fragP == NULL)
7461 return FALSE;
7462
7463 if (!next_frag_is_loop_target (next_fragP))
7464 return FALSE;
7465
7466 /* If the size is >= 3 then there is more than one instruction here.
7467 The hardware bug will not fire. */
7468 if (next_fragP->fr_fix > 3)
7469 return FALSE;
7470
7471 return TRUE;
7472 }
7473
7474
7475 /* Re-process all of the fragments looking to convert all of the
7476 RELAX_ADD_NOP_IF_CLOSE_LOOP_END. If there is an loop end that is
7477 not MY loop's loop end within 12 bytes, add enough nops here to
7478 make it at least 12 bytes away. In any case close it off with a
7479 .fill 0. */
7480
7481 static offsetT min_bytes_to_other_loop_end
7482 (fragS *, fragS *, offsetT);
7483
7484 static void
7485 xtensa_fix_close_loop_end_frags (void)
7486 {
7487 frchainS *frchP;
7488 asection *s;
7489
7490 /* When this routine is called, all of the subsections are still intact
7491 so we walk over subsections instead of sections. */
7492 for (s = stdoutput->sections; s; s = s->next)
7493 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7494 {
7495 fragS *fragP;
7496
7497 fragS *current_target = NULL;
7498
7499 /* Walk over all of the fragments in a subsection. */
7500 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7501 {
7502 if (fragP->fr_type == rs_machine_dependent
7503 && ((fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
7504 || (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)))
7505 current_target = symbol_get_frag (fragP->fr_symbol);
7506
7507 if (current_target
7508 && fragP->fr_type == rs_machine_dependent
7509 && fragP->fr_subtype == RELAX_ADD_NOP_IF_CLOSE_LOOP_END)
7510 {
7511 offsetT min_bytes;
7512 int bytes_added = 0;
7513
7514 #define REQUIRED_LOOP_DIVIDING_BYTES 12
7515 /* Max out at 12. */
7516 min_bytes = min_bytes_to_other_loop_end
7517 (fragP->fr_next, current_target, REQUIRED_LOOP_DIVIDING_BYTES);
7518
7519 if (min_bytes < REQUIRED_LOOP_DIVIDING_BYTES)
7520 {
7521 if (fragP->tc_frag_data.is_no_transform)
7522 as_bad (_("loop end too close to another loop end may trigger hardware errata"));
7523 else
7524 {
7525 while (min_bytes + bytes_added
7526 < REQUIRED_LOOP_DIVIDING_BYTES)
7527 {
7528 int length = 3;
7529
7530 if (fragP->fr_var < length)
7531 as_fatal (_("fr_var %lu < length %d"),
7532 (long) fragP->fr_var, length);
7533 else
7534 {
7535 assemble_nop (length,
7536 fragP->fr_literal + fragP->fr_fix);
7537 fragP->fr_fix += length;
7538 fragP->fr_var -= length;
7539 }
7540 bytes_added += length;
7541 }
7542 }
7543 }
7544 frag_wane (fragP);
7545 }
7546 assert (fragP->fr_type != rs_machine_dependent
7547 || fragP->fr_subtype != RELAX_ADD_NOP_IF_CLOSE_LOOP_END);
7548 }
7549 }
7550 }
7551
7552
7553 static offsetT unrelaxed_frag_min_size (fragS *);
7554
7555 static offsetT
7556 min_bytes_to_other_loop_end (fragS *fragP,
7557 fragS *current_target,
7558 offsetT max_size)
7559 {
7560 offsetT offset = 0;
7561 fragS *current_fragP;
7562
7563 for (current_fragP = fragP;
7564 current_fragP;
7565 current_fragP = current_fragP->fr_next)
7566 {
7567 if (current_fragP->tc_frag_data.is_loop_target
7568 && current_fragP != current_target)
7569 return offset;
7570
7571 offset += unrelaxed_frag_min_size (current_fragP);
7572
7573 if (offset >= max_size)
7574 return max_size;
7575 }
7576 return max_size;
7577 }
7578
7579
7580 static offsetT
7581 unrelaxed_frag_min_size (fragS *fragP)
7582 {
7583 offsetT size = fragP->fr_fix;
7584
7585 /* Add fill size. */
7586 if (fragP->fr_type == rs_fill)
7587 size += fragP->fr_offset;
7588
7589 return size;
7590 }
7591
7592
7593 static offsetT
7594 unrelaxed_frag_max_size (fragS *fragP)
7595 {
7596 offsetT size = fragP->fr_fix;
7597 switch (fragP->fr_type)
7598 {
7599 case 0:
7600 /* Empty frags created by the obstack allocation scheme
7601 end up with type 0. */
7602 break;
7603 case rs_fill:
7604 case rs_org:
7605 case rs_space:
7606 size += fragP->fr_offset;
7607 break;
7608 case rs_align:
7609 case rs_align_code:
7610 case rs_align_test:
7611 case rs_leb128:
7612 case rs_cfa:
7613 case rs_dwarf2dbg:
7614 /* No further adjustments needed. */
7615 break;
7616 case rs_machine_dependent:
7617 if (fragP->fr_subtype != RELAX_DESIRE_ALIGN)
7618 size += fragP->fr_var;
7619 break;
7620 default:
7621 /* We had darn well better know how big it is. */
7622 assert (0);
7623 break;
7624 }
7625
7626 return size;
7627 }
7628
7629
7630 /* Re-process all of the fragments looking to convert all
7631 of the RELAX_ADD_NOP_IF_SHORT_LOOP. If:
7632
7633 A)
7634 1) the instruction size count to the loop end label
7635 is too short (<= 2 instructions),
7636 2) loop has a jump or branch in it
7637
7638 or B)
7639 1) workaround_all_short_loops is TRUE
7640 2) The generating loop was a 'loopgtz' or 'loopnez'
7641 3) the instruction size count to the loop end label is too short
7642 (<= 2 instructions)
7643 then convert this frag (and maybe the next one) to generate a NOP.
7644 In any case close it off with a .fill 0. */
7645
7646 static int count_insns_to_loop_end (fragS *, bfd_boolean, int);
7647 static bfd_boolean branch_before_loop_end (fragS *);
7648
7649 static void
7650 xtensa_fix_short_loop_frags (void)
7651 {
7652 frchainS *frchP;
7653 asection *s;
7654
7655 /* When this routine is called, all of the subsections are still intact
7656 so we walk over subsections instead of sections. */
7657 for (s = stdoutput->sections; s; s = s->next)
7658 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7659 {
7660 fragS *fragP;
7661 fragS *current_target = NULL;
7662 xtensa_opcode current_opcode = XTENSA_UNDEFINED;
7663
7664 /* Walk over all of the fragments in a subsection. */
7665 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7666 {
7667 if (fragP->fr_type == rs_machine_dependent
7668 && ((fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
7669 || (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)))
7670 {
7671 TInsn t_insn;
7672 fragS *loop_frag = next_non_empty_frag (fragP);
7673 tinsn_from_chars (&t_insn, loop_frag->fr_opcode, 0);
7674 current_target = symbol_get_frag (fragP->fr_symbol);
7675 current_opcode = t_insn.opcode;
7676 assert (xtensa_opcode_is_loop (xtensa_default_isa,
7677 current_opcode) == 1);
7678 }
7679
7680 if (fragP->fr_type == rs_machine_dependent
7681 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
7682 {
7683 if (count_insns_to_loop_end (fragP->fr_next, TRUE, 3) < 3
7684 && (branch_before_loop_end (fragP->fr_next)
7685 || (workaround_all_short_loops
7686 && current_opcode != XTENSA_UNDEFINED
7687 && current_opcode != xtensa_loop_opcode)))
7688 {
7689 if (fragP->tc_frag_data.is_no_transform)
7690 as_bad (_("loop containing less than three instructions may trigger hardware errata"));
7691 else
7692 relax_frag_add_nop (fragP);
7693 }
7694 frag_wane (fragP);
7695 }
7696 }
7697 }
7698 }
7699
7700
7701 static int unrelaxed_frag_min_insn_count (fragS *);
7702
7703 static int
7704 count_insns_to_loop_end (fragS *base_fragP,
7705 bfd_boolean count_relax_add,
7706 int max_count)
7707 {
7708 fragS *fragP = NULL;
7709 int insn_count = 0;
7710
7711 fragP = base_fragP;
7712
7713 for (; fragP && !fragP->tc_frag_data.is_loop_target; fragP = fragP->fr_next)
7714 {
7715 insn_count += unrelaxed_frag_min_insn_count (fragP);
7716 if (insn_count >= max_count)
7717 return max_count;
7718
7719 if (count_relax_add)
7720 {
7721 if (fragP->fr_type == rs_machine_dependent
7722 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
7723 {
7724 /* In order to add the appropriate number of
7725 NOPs, we count an instruction for downstream
7726 occurrences. */
7727 insn_count++;
7728 if (insn_count >= max_count)
7729 return max_count;
7730 }
7731 }
7732 }
7733 return insn_count;
7734 }
7735
7736
7737 static int
7738 unrelaxed_frag_min_insn_count (fragS *fragP)
7739 {
7740 xtensa_isa isa = xtensa_default_isa;
7741 static xtensa_insnbuf insnbuf = NULL;
7742 int insn_count = 0;
7743 int offset = 0;
7744
7745 if (!fragP->tc_frag_data.is_insn)
7746 return insn_count;
7747
7748 if (!insnbuf)
7749 insnbuf = xtensa_insnbuf_alloc (isa);
7750
7751 /* Decode the fixed instructions. */
7752 while (offset < fragP->fr_fix)
7753 {
7754 xtensa_format fmt;
7755
7756 xtensa_insnbuf_from_chars
7757 (isa, insnbuf, (unsigned char *) fragP->fr_literal + offset, 0);
7758 fmt = xtensa_format_decode (isa, insnbuf);
7759
7760 if (fmt == XTENSA_UNDEFINED)
7761 {
7762 as_fatal (_("undecodable instruction in instruction frag"));
7763 return insn_count;
7764 }
7765 offset += xtensa_format_length (isa, fmt);
7766 insn_count++;
7767 }
7768
7769 return insn_count;
7770 }
7771
7772
7773 static bfd_boolean unrelaxed_frag_has_b_j (fragS *);
7774
7775 static bfd_boolean
7776 branch_before_loop_end (fragS *base_fragP)
7777 {
7778 fragS *fragP;
7779
7780 for (fragP = base_fragP;
7781 fragP && !fragP->tc_frag_data.is_loop_target;
7782 fragP = fragP->fr_next)
7783 {
7784 if (unrelaxed_frag_has_b_j (fragP))
7785 return TRUE;
7786 }
7787 return FALSE;
7788 }
7789
7790
7791 static bfd_boolean
7792 unrelaxed_frag_has_b_j (fragS *fragP)
7793 {
7794 static xtensa_insnbuf insnbuf = NULL;
7795 xtensa_isa isa = xtensa_default_isa;
7796 int offset = 0;
7797
7798 if (!fragP->tc_frag_data.is_insn)
7799 return FALSE;
7800
7801 if (!insnbuf)
7802 insnbuf = xtensa_insnbuf_alloc (isa);
7803
7804 /* Decode the fixed instructions. */
7805 while (offset < fragP->fr_fix)
7806 {
7807 xtensa_format fmt;
7808 int slot;
7809
7810 xtensa_insnbuf_from_chars
7811 (isa, insnbuf, (unsigned char *) fragP->fr_literal + offset, 0);
7812 fmt = xtensa_format_decode (isa, insnbuf);
7813 if (fmt == XTENSA_UNDEFINED)
7814 return FALSE;
7815
7816 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
7817 {
7818 xtensa_opcode opcode =
7819 get_opcode_from_buf (fragP->fr_literal + offset, slot);
7820 if (xtensa_opcode_is_branch (isa, opcode) == 1
7821 || xtensa_opcode_is_jump (isa, opcode) == 1)
7822 return TRUE;
7823 }
7824 offset += xtensa_format_length (isa, fmt);
7825 }
7826 return FALSE;
7827 }
7828
7829
7830 /* Checks to be made after initial assembly but before relaxation. */
7831
7832 static bfd_boolean is_empty_loop (const TInsn *, fragS *);
7833 static bfd_boolean is_local_forward_loop (const TInsn *, fragS *);
7834
7835 static void
7836 xtensa_sanity_check (void)
7837 {
7838 char *file_name;
7839 unsigned line;
7840 frchainS *frchP;
7841 asection *s;
7842
7843 as_where (&file_name, &line);
7844 for (s = stdoutput->sections; s; s = s->next)
7845 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7846 {
7847 fragS *fragP;
7848
7849 /* Walk over all of the fragments in a subsection. */
7850 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7851 {
7852 if (fragP->fr_type == rs_machine_dependent
7853 && fragP->fr_subtype == RELAX_SLOTS
7854 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED)
7855 {
7856 static xtensa_insnbuf insnbuf = NULL;
7857 TInsn t_insn;
7858
7859 if (fragP->fr_opcode != NULL)
7860 {
7861 if (!insnbuf)
7862 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
7863 tinsn_from_chars (&t_insn, fragP->fr_opcode, 0);
7864 tinsn_immed_from_frag (&t_insn, fragP, 0);
7865
7866 if (xtensa_opcode_is_loop (xtensa_default_isa,
7867 t_insn.opcode) == 1)
7868 {
7869 if (is_empty_loop (&t_insn, fragP))
7870 {
7871 new_logical_line (fragP->fr_file, fragP->fr_line);
7872 as_bad (_("invalid empty loop"));
7873 }
7874 if (!is_local_forward_loop (&t_insn, fragP))
7875 {
7876 new_logical_line (fragP->fr_file, fragP->fr_line);
7877 as_bad (_("loop target does not follow "
7878 "loop instruction in section"));
7879 }
7880 }
7881 }
7882 }
7883 }
7884 }
7885 new_logical_line (file_name, line);
7886 }
7887
7888
7889 #define LOOP_IMMED_OPN 1
7890
7891 /* Return TRUE if the loop target is the next non-zero fragment. */
7892
7893 static bfd_boolean
7894 is_empty_loop (const TInsn *insn, fragS *fragP)
7895 {
7896 const expressionS *expr;
7897 symbolS *symbolP;
7898 fragS *next_fragP;
7899
7900 if (insn->insn_type != ITYPE_INSN)
7901 return FALSE;
7902
7903 if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) != 1)
7904 return FALSE;
7905
7906 if (insn->ntok <= LOOP_IMMED_OPN)
7907 return FALSE;
7908
7909 expr = &insn->tok[LOOP_IMMED_OPN];
7910
7911 if (expr->X_op != O_symbol)
7912 return FALSE;
7913
7914 symbolP = expr->X_add_symbol;
7915 if (!symbolP)
7916 return FALSE;
7917
7918 if (symbol_get_frag (symbolP) == NULL)
7919 return FALSE;
7920
7921 if (S_GET_VALUE (symbolP) != 0)
7922 return FALSE;
7923
7924 /* Walk through the zero-size fragments from this one. If we find
7925 the target fragment, then this is a zero-size loop. */
7926
7927 for (next_fragP = fragP->fr_next;
7928 next_fragP != NULL;
7929 next_fragP = next_fragP->fr_next)
7930 {
7931 if (next_fragP == symbol_get_frag (symbolP))
7932 return TRUE;
7933 if (next_fragP->fr_fix != 0)
7934 return FALSE;
7935 }
7936 return FALSE;
7937 }
7938
7939
7940 static bfd_boolean
7941 is_local_forward_loop (const TInsn *insn, fragS *fragP)
7942 {
7943 const expressionS *expr;
7944 symbolS *symbolP;
7945 fragS *next_fragP;
7946
7947 if (insn->insn_type != ITYPE_INSN)
7948 return FALSE;
7949
7950 if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) != 1)
7951 return FALSE;
7952
7953 if (insn->ntok <= LOOP_IMMED_OPN)
7954 return FALSE;
7955
7956 expr = &insn->tok[LOOP_IMMED_OPN];
7957
7958 if (expr->X_op != O_symbol)
7959 return FALSE;
7960
7961 symbolP = expr->X_add_symbol;
7962 if (!symbolP)
7963 return FALSE;
7964
7965 if (symbol_get_frag (symbolP) == NULL)
7966 return FALSE;
7967
7968 /* Walk through fragments until we find the target.
7969 If we do not find the target, then this is an invalid loop. */
7970
7971 for (next_fragP = fragP->fr_next;
7972 next_fragP != NULL;
7973 next_fragP = next_fragP->fr_next)
7974 {
7975 if (next_fragP == symbol_get_frag (symbolP))
7976 return TRUE;
7977 }
7978
7979 return FALSE;
7980 }
7981
7982
7983 #define XTINFO_NAME "Xtensa_Info"
7984 #define XTINFO_NAMESZ 12
7985 #define XTINFO_TYPE 1
7986
7987 static void
7988 xtensa_add_config_info (void)
7989 {
7990 asection *info_sec;
7991 char *data, *p;
7992 int sz;
7993
7994 info_sec = subseg_new (".xtensa.info", 0);
7995 bfd_set_section_flags (stdoutput, info_sec, SEC_HAS_CONTENTS | SEC_READONLY);
7996
7997 data = xmalloc (100);
7998 sprintf (data, "USE_ABSOLUTE_LITERALS=%d\nABI=%d\n",
7999 XSHAL_USE_ABSOLUTE_LITERALS, XSHAL_ABI);
8000 sz = strlen (data) + 1;
8001
8002 /* Add enough null terminators to pad to a word boundary. */
8003 do
8004 data[sz++] = 0;
8005 while ((sz & 3) != 0);
8006
8007 /* Follow the standard note section layout:
8008 First write the length of the name string. */
8009 p = frag_more (4);
8010 md_number_to_chars (p, (valueT) XTINFO_NAMESZ, 4);
8011
8012 /* Next comes the length of the "descriptor", i.e., the actual data. */
8013 p = frag_more (4);
8014 md_number_to_chars (p, (valueT) sz, 4);
8015
8016 /* Write the note type. */
8017 p = frag_more (4);
8018 md_number_to_chars (p, (valueT) XTINFO_TYPE, 4);
8019
8020 /* Write the name field. */
8021 p = frag_more (XTINFO_NAMESZ);
8022 memcpy (p, XTINFO_NAME, XTINFO_NAMESZ);
8023
8024 /* Finally, write the descriptor. */
8025 p = frag_more (sz);
8026 memcpy (p, data, sz);
8027
8028 free (data);
8029 }
8030
8031 \f
8032 /* Alignment Functions. */
8033
8034 static int
8035 get_text_align_power (unsigned target_size)
8036 {
8037 if (target_size <= 4)
8038 return 2;
8039 assert (target_size == 8);
8040 return 3;
8041 }
8042
8043
8044 static int
8045 get_text_align_max_fill_size (int align_pow,
8046 bfd_boolean use_nops,
8047 bfd_boolean use_no_density)
8048 {
8049 if (!use_nops)
8050 return (1 << align_pow);
8051 if (use_no_density)
8052 return 3 * (1 << align_pow);
8053
8054 return 1 + (1 << align_pow);
8055 }
8056
8057
8058 /* Calculate the minimum bytes of fill needed at "address" to align a
8059 target instruction of size "target_size" so that it does not cross a
8060 power-of-two boundary specified by "align_pow". If "use_nops" is FALSE,
8061 the fill can be an arbitrary number of bytes. Otherwise, the space must
8062 be filled by NOP instructions. */
8063
8064 static int
8065 get_text_align_fill_size (addressT address,
8066 int align_pow,
8067 int target_size,
8068 bfd_boolean use_nops,
8069 bfd_boolean use_no_density)
8070 {
8071 addressT alignment, fill, fill_limit, fill_step;
8072 bfd_boolean skip_one = FALSE;
8073
8074 alignment = (1 << align_pow);
8075 assert (target_size > 0 && alignment >= (addressT) target_size);
8076
8077 if (!use_nops)
8078 {
8079 fill_limit = alignment;
8080 fill_step = 1;
8081 }
8082 else if (!use_no_density)
8083 {
8084 /* Combine 2- and 3-byte NOPs to fill anything larger than one. */
8085 fill_limit = alignment * 2;
8086 fill_step = 1;
8087 skip_one = TRUE;
8088 }
8089 else
8090 {
8091 /* Fill with 3-byte NOPs -- can only fill multiples of 3. */
8092 fill_limit = alignment * 3;
8093 fill_step = 3;
8094 }
8095
8096 /* Try all fill sizes until finding one that works. */
8097 for (fill = 0; fill < fill_limit; fill += fill_step)
8098 {
8099 if (skip_one && fill == 1)
8100 continue;
8101 if ((address + fill) >> align_pow
8102 == (address + fill + target_size - 1) >> align_pow)
8103 return fill;
8104 }
8105 assert (0);
8106 return 0;
8107 }
8108
8109
8110 static int
8111 branch_align_power (segT sec)
8112 {
8113 /* If the Xtensa processor has a fetch width of 8 bytes, and the section
8114 is aligned to at least an 8-byte boundary, then a branch target need
8115 only fit within an 8-byte aligned block of memory to avoid a stall.
8116 Otherwise, try to fit branch targets within 4-byte aligned blocks
8117 (which may be insufficient, e.g., if the section has no alignment, but
8118 it's good enough). */
8119 if (xtensa_fetch_width == 8)
8120 {
8121 if (get_recorded_alignment (sec) >= 3)
8122 return 3;
8123 }
8124 else
8125 assert (xtensa_fetch_width == 4);
8126
8127 return 2;
8128 }
8129
8130
8131 /* This will assert if it is not possible. */
8132
8133 static int
8134 get_text_align_nop_count (offsetT fill_size, bfd_boolean use_no_density)
8135 {
8136 int count = 0;
8137
8138 if (use_no_density)
8139 {
8140 assert (fill_size % 3 == 0);
8141 return (fill_size / 3);
8142 }
8143
8144 assert (fill_size != 1); /* Bad argument. */
8145
8146 while (fill_size > 1)
8147 {
8148 int insn_size = 3;
8149 if (fill_size == 2 || fill_size == 4)
8150 insn_size = 2;
8151 fill_size -= insn_size;
8152 count++;
8153 }
8154 assert (fill_size != 1); /* Bad algorithm. */
8155 return count;
8156 }
8157
8158
8159 static int
8160 get_text_align_nth_nop_size (offsetT fill_size,
8161 int n,
8162 bfd_boolean use_no_density)
8163 {
8164 int count = 0;
8165
8166 if (use_no_density)
8167 return 3;
8168
8169 assert (fill_size != 1); /* Bad argument. */
8170
8171 while (fill_size > 1)
8172 {
8173 int insn_size = 3;
8174 if (fill_size == 2 || fill_size == 4)
8175 insn_size = 2;
8176 fill_size -= insn_size;
8177 count++;
8178 if (n + 1 == count)
8179 return insn_size;
8180 }
8181 assert (0);
8182 return 0;
8183 }
8184
8185
8186 /* For the given fragment, find the appropriate address
8187 for it to begin at if we are using NOPs to align it. */
8188
8189 static addressT
8190 get_noop_aligned_address (fragS *fragP, addressT address)
8191 {
8192 /* The rule is: get next fragment's FIRST instruction. Find
8193 the smallest number of bytes that need to be added to
8194 ensure that the next fragment's FIRST instruction will fit
8195 in a single word.
8196
8197 E.G., 2 bytes : 0, 1, 2 mod 4
8198 3 bytes: 0, 1 mod 4
8199
8200 If the FIRST instruction MIGHT be relaxed,
8201 assume that it will become a 3-byte instruction.
8202
8203 Note again here that LOOP instructions are not bundleable,
8204 and this relaxation only applies to LOOP opcodes. */
8205
8206 int fill_size = 0;
8207 int first_insn_size;
8208 int loop_insn_size;
8209 addressT pre_opcode_bytes;
8210 int align_power;
8211 fragS *first_insn;
8212 xtensa_opcode opcode;
8213 bfd_boolean is_loop;
8214
8215 assert (fragP->fr_type == rs_machine_dependent);
8216 assert (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE);
8217
8218 /* Find the loop frag. */
8219 first_insn = next_non_empty_frag (fragP);
8220 /* Now find the first insn frag. */
8221 first_insn = next_non_empty_frag (first_insn);
8222
8223 is_loop = next_frag_opcode_is_loop (fragP, &opcode);
8224 assert (is_loop);
8225 loop_insn_size = xg_get_single_size (opcode);
8226
8227 pre_opcode_bytes = next_frag_pre_opcode_bytes (fragP);
8228 pre_opcode_bytes += loop_insn_size;
8229
8230 /* For loops, the alignment depends on the size of the
8231 instruction following the loop, not the LOOP instruction. */
8232
8233 if (first_insn == NULL)
8234 first_insn_size = xtensa_fetch_width;
8235 else
8236 first_insn_size = get_loop_align_size (frag_format_size (first_insn));
8237
8238 /* If it was 8, then we'll need a larger alignment for the section. */
8239 align_power = get_text_align_power (first_insn_size);
8240 record_alignment (now_seg, align_power);
8241
8242 fill_size = get_text_align_fill_size
8243 (address + pre_opcode_bytes, align_power, first_insn_size, TRUE,
8244 fragP->tc_frag_data.is_no_density);
8245
8246 return address + fill_size;
8247 }
8248
8249
8250 /* 3 mechanisms for relaxing an alignment:
8251
8252 Align to a power of 2.
8253 Align so the next fragment's instruction does not cross a word boundary.
8254 Align the current instruction so that if the next instruction
8255 were 3 bytes, it would not cross a word boundary.
8256
8257 We can align with:
8258
8259 zeros - This is easy; always insert zeros.
8260 nops - 3-byte and 2-byte instructions
8261 2 - 2-byte nop
8262 3 - 3-byte nop
8263 4 - 2 2-byte nops
8264 >=5 : 3-byte instruction + fn (n-3)
8265 widening - widen previous instructions. */
8266
8267 static offsetT
8268 get_aligned_diff (fragS *fragP, addressT address, offsetT *max_diff)
8269 {
8270 addressT target_address, loop_insn_offset;
8271 int target_size;
8272 xtensa_opcode loop_opcode;
8273 bfd_boolean is_loop;
8274 int align_power;
8275 offsetT opt_diff;
8276 offsetT branch_align;
8277 fragS *loop_frag;
8278
8279 assert (fragP->fr_type == rs_machine_dependent);
8280 switch (fragP->fr_subtype)
8281 {
8282 case RELAX_DESIRE_ALIGN:
8283 target_size = next_frag_format_size (fragP);
8284 if (target_size == XTENSA_UNDEFINED)
8285 target_size = 3;
8286 align_power = branch_align_power (now_seg);
8287 branch_align = 1 << align_power;
8288 /* Don't count on the section alignment being as large as the target. */
8289 if (target_size > branch_align)
8290 target_size = branch_align;
8291 opt_diff = get_text_align_fill_size (address, align_power,
8292 target_size, FALSE, FALSE);
8293
8294 *max_diff = (opt_diff + branch_align
8295 - (target_size + ((address + opt_diff) % branch_align)));
8296 assert (*max_diff >= opt_diff);
8297 return opt_diff;
8298
8299 case RELAX_ALIGN_NEXT_OPCODE:
8300 /* The next non-empty frag after this one holds the LOOP instruction
8301 that needs to be aligned. The required alignment depends on the
8302 size of the next non-empty frag after the loop frag, i.e., the
8303 first instruction in the loop. */
8304 loop_frag = next_non_empty_frag (fragP);
8305 target_size = get_loop_align_size (next_frag_format_size (loop_frag));
8306 loop_insn_offset = 0;
8307 is_loop = next_frag_opcode_is_loop (fragP, &loop_opcode);
8308 assert (is_loop);
8309
8310 /* If the loop has been expanded then the LOOP instruction
8311 could be at an offset from this fragment. */
8312 if (loop_frag->tc_frag_data.slot_subtypes[0] != RELAX_IMMED)
8313 loop_insn_offset = get_expanded_loop_offset (loop_opcode);
8314
8315 /* In an ideal world, which is what we are shooting for here,
8316 we wouldn't need to use any NOPs immediately prior to the
8317 LOOP instruction. If this approach fails, relax_frag_loop_align
8318 will call get_noop_aligned_address. */
8319 target_address =
8320 address + loop_insn_offset + xg_get_single_size (loop_opcode);
8321 align_power = get_text_align_power (target_size);
8322 opt_diff = get_text_align_fill_size (target_address, align_power,
8323 target_size, FALSE, FALSE);
8324
8325 *max_diff = xtensa_fetch_width
8326 - ((target_address + opt_diff) % xtensa_fetch_width)
8327 - target_size + opt_diff;
8328 assert (*max_diff >= opt_diff);
8329 return opt_diff;
8330
8331 default:
8332 break;
8333 }
8334 assert (0);
8335 return 0;
8336 }
8337
8338 \f
8339 /* md_relax_frag Hook and Helper Functions. */
8340
8341 static long relax_frag_loop_align (fragS *, long);
8342 static long relax_frag_for_align (fragS *, long);
8343 static long relax_frag_immed
8344 (segT, fragS *, long, int, xtensa_format, int, int *, bfd_boolean);
8345
8346
8347 /* Return the number of bytes added to this fragment, given that the
8348 input has been stretched already by "stretch". */
8349
8350 long
8351 xtensa_relax_frag (fragS *fragP, long stretch, int *stretched_p)
8352 {
8353 xtensa_isa isa = xtensa_default_isa;
8354 int unreported = fragP->tc_frag_data.unreported_expansion;
8355 long new_stretch = 0;
8356 char *file_name;
8357 unsigned line;
8358 int lit_size;
8359 static xtensa_insnbuf vbuf = NULL;
8360 int slot, num_slots;
8361 xtensa_format fmt;
8362
8363 as_where (&file_name, &line);
8364 new_logical_line (fragP->fr_file, fragP->fr_line);
8365
8366 fragP->tc_frag_data.unreported_expansion = 0;
8367
8368 switch (fragP->fr_subtype)
8369 {
8370 case RELAX_ALIGN_NEXT_OPCODE:
8371 /* Always convert. */
8372 if (fragP->tc_frag_data.relax_seen)
8373 new_stretch = relax_frag_loop_align (fragP, stretch);
8374 break;
8375
8376 case RELAX_LOOP_END:
8377 /* Do nothing. */
8378 break;
8379
8380 case RELAX_LOOP_END_ADD_NOP:
8381 /* Add a NOP and switch to .fill 0. */
8382 new_stretch = relax_frag_add_nop (fragP);
8383 frag_wane (fragP);
8384 break;
8385
8386 case RELAX_DESIRE_ALIGN:
8387 /* Do nothing. The narrowing before this frag will either align
8388 it or not. */
8389 break;
8390
8391 case RELAX_LITERAL:
8392 case RELAX_LITERAL_FINAL:
8393 return 0;
8394
8395 case RELAX_LITERAL_NR:
8396 lit_size = 4;
8397 fragP->fr_subtype = RELAX_LITERAL_FINAL;
8398 assert (unreported == lit_size);
8399 memset (&fragP->fr_literal[fragP->fr_fix], 0, 4);
8400 fragP->fr_var -= lit_size;
8401 fragP->fr_fix += lit_size;
8402 new_stretch = 4;
8403 break;
8404
8405 case RELAX_SLOTS:
8406 if (vbuf == NULL)
8407 vbuf = xtensa_insnbuf_alloc (isa);
8408
8409 xtensa_insnbuf_from_chars
8410 (isa, vbuf, (unsigned char *) fragP->fr_opcode, 0);
8411 fmt = xtensa_format_decode (isa, vbuf);
8412 num_slots = xtensa_format_num_slots (isa, fmt);
8413
8414 for (slot = 0; slot < num_slots; slot++)
8415 {
8416 switch (fragP->tc_frag_data.slot_subtypes[slot])
8417 {
8418 case RELAX_NARROW:
8419 if (fragP->tc_frag_data.relax_seen)
8420 new_stretch += relax_frag_for_align (fragP, stretch);
8421 break;
8422
8423 case RELAX_IMMED:
8424 case RELAX_IMMED_STEP1:
8425 case RELAX_IMMED_STEP2:
8426 case RELAX_IMMED_STEP3:
8427 /* Place the immediate. */
8428 new_stretch += relax_frag_immed
8429 (now_seg, fragP, stretch,
8430 fragP->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED,
8431 fmt, slot, stretched_p, FALSE);
8432 break;
8433
8434 default:
8435 /* This is OK; see the note in xg_assemble_vliw_tokens. */
8436 break;
8437 }
8438 }
8439 break;
8440
8441 case RELAX_LITERAL_POOL_BEGIN:
8442 case RELAX_LITERAL_POOL_END:
8443 case RELAX_MAYBE_UNREACHABLE:
8444 case RELAX_MAYBE_DESIRE_ALIGN:
8445 /* No relaxation required. */
8446 break;
8447
8448 case RELAX_FILL_NOP:
8449 case RELAX_UNREACHABLE:
8450 if (fragP->tc_frag_data.relax_seen)
8451 new_stretch += relax_frag_for_align (fragP, stretch);
8452 break;
8453
8454 default:
8455 as_bad (_("bad relaxation state"));
8456 }
8457
8458 /* Tell gas we need another relaxation pass. */
8459 if (! fragP->tc_frag_data.relax_seen)
8460 {
8461 fragP->tc_frag_data.relax_seen = TRUE;
8462 *stretched_p = 1;
8463 }
8464
8465 new_logical_line (file_name, line);
8466 return new_stretch;
8467 }
8468
8469
8470 static long
8471 relax_frag_loop_align (fragS *fragP, long stretch)
8472 {
8473 addressT old_address, old_next_address, old_size;
8474 addressT new_address, new_next_address, new_size;
8475 addressT growth;
8476
8477 /* All the frags with relax_frag_for_alignment prior to this one in the
8478 section have been done, hopefully eliminating the need for a NOP here.
8479 But, this will put it in if necessary. */
8480
8481 /* Calculate the old address of this fragment and the next fragment. */
8482 old_address = fragP->fr_address - stretch;
8483 old_next_address = (fragP->fr_address - stretch + fragP->fr_fix +
8484 fragP->tc_frag_data.text_expansion[0]);
8485 old_size = old_next_address - old_address;
8486
8487 /* Calculate the new address of this fragment and the next fragment. */
8488 new_address = fragP->fr_address;
8489 new_next_address =
8490 get_noop_aligned_address (fragP, fragP->fr_address + fragP->fr_fix);
8491 new_size = new_next_address - new_address;
8492
8493 growth = new_size - old_size;
8494
8495 /* Fix up the text_expansion field and return the new growth. */
8496 fragP->tc_frag_data.text_expansion[0] += growth;
8497 return growth;
8498 }
8499
8500
8501 /* Add a NOP instruction. */
8502
8503 static long
8504 relax_frag_add_nop (fragS *fragP)
8505 {
8506 char *nop_buf = fragP->fr_literal + fragP->fr_fix;
8507 int length = fragP->tc_frag_data.is_no_density ? 3 : 2;
8508 assemble_nop (length, nop_buf);
8509 fragP->tc_frag_data.is_insn = TRUE;
8510
8511 if (fragP->fr_var < length)
8512 {
8513 as_fatal (_("fr_var (%ld) < length (%d)"), (long) fragP->fr_var, length);
8514 return 0;
8515 }
8516
8517 fragP->fr_fix += length;
8518 fragP->fr_var -= length;
8519 return length;
8520 }
8521
8522
8523 static long future_alignment_required (fragS *, long);
8524
8525 static long
8526 relax_frag_for_align (fragS *fragP, long stretch)
8527 {
8528 /* Overview of the relaxation procedure for alignment:
8529 We can widen with NOPs or by widening instructions or by filling
8530 bytes after jump instructions. Find the opportune places and widen
8531 them if necessary. */
8532
8533 long stretch_me;
8534 long diff;
8535
8536 assert (fragP->fr_subtype == RELAX_FILL_NOP
8537 || fragP->fr_subtype == RELAX_UNREACHABLE
8538 || (fragP->fr_subtype == RELAX_SLOTS
8539 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW));
8540
8541 stretch_me = future_alignment_required (fragP, stretch);
8542 diff = stretch_me - fragP->tc_frag_data.text_expansion[0];
8543 if (diff == 0)
8544 return 0;
8545
8546 if (diff < 0)
8547 {
8548 /* We expanded on a previous pass. Can we shrink now? */
8549 long shrink = fragP->tc_frag_data.text_expansion[0] - stretch_me;
8550 if (shrink <= stretch && stretch > 0)
8551 {
8552 fragP->tc_frag_data.text_expansion[0] = stretch_me;
8553 return -shrink;
8554 }
8555 return 0;
8556 }
8557
8558 /* Below here, diff > 0. */
8559 fragP->tc_frag_data.text_expansion[0] = stretch_me;
8560
8561 return diff;
8562 }
8563
8564
8565 /* Return the address of the next frag that should be aligned.
8566
8567 By "address" we mean the address it _would_ be at if there
8568 is no action taken to align it between here and the target frag.
8569 In other words, if no narrows and no fill nops are used between
8570 here and the frag to align, _even_if_ some of the frags we use
8571 to align targets have already expanded on a previous relaxation
8572 pass.
8573
8574 Also, count each frag that may be used to help align the target.
8575
8576 Return 0 if there are no frags left in the chain that need to be
8577 aligned. */
8578
8579 static addressT
8580 find_address_of_next_align_frag (fragS **fragPP,
8581 int *wide_nops,
8582 int *narrow_nops,
8583 int *widens,
8584 bfd_boolean *paddable)
8585 {
8586 fragS *fragP = *fragPP;
8587 addressT address = fragP->fr_address;
8588
8589 /* Do not reset the counts to 0. */
8590
8591 while (fragP)
8592 {
8593 /* Limit this to a small search. */
8594 if (*widens >= (int) xtensa_fetch_width)
8595 {
8596 *fragPP = fragP;
8597 return 0;
8598 }
8599 address += fragP->fr_fix;
8600
8601 if (fragP->fr_type == rs_fill)
8602 address += fragP->fr_offset * fragP->fr_var;
8603 else if (fragP->fr_type == rs_machine_dependent)
8604 {
8605 switch (fragP->fr_subtype)
8606 {
8607 case RELAX_UNREACHABLE:
8608 *paddable = TRUE;
8609 break;
8610
8611 case RELAX_FILL_NOP:
8612 (*wide_nops)++;
8613 if (!fragP->tc_frag_data.is_no_density)
8614 (*narrow_nops)++;
8615 break;
8616
8617 case RELAX_SLOTS:
8618 if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
8619 {
8620 (*widens)++;
8621 break;
8622 }
8623 address += total_frag_text_expansion (fragP);;
8624 break;
8625
8626 case RELAX_IMMED:
8627 address += fragP->tc_frag_data.text_expansion[0];
8628 break;
8629
8630 case RELAX_ALIGN_NEXT_OPCODE:
8631 case RELAX_DESIRE_ALIGN:
8632 *fragPP = fragP;
8633 return address;
8634
8635 case RELAX_MAYBE_UNREACHABLE:
8636 case RELAX_MAYBE_DESIRE_ALIGN:
8637 /* Do nothing. */
8638 break;
8639
8640 default:
8641 /* Just punt if we don't know the type. */
8642 *fragPP = fragP;
8643 return 0;
8644 }
8645 }
8646 else
8647 {
8648 /* Just punt if we don't know the type. */
8649 *fragPP = fragP;
8650 return 0;
8651 }
8652 fragP = fragP->fr_next;
8653 }
8654
8655 *fragPP = fragP;
8656 return 0;
8657 }
8658
8659
8660 static long bytes_to_stretch (fragS *, int, int, int, int);
8661
8662 static long
8663 future_alignment_required (fragS *fragP, long stretch ATTRIBUTE_UNUSED)
8664 {
8665 fragS *this_frag = fragP;
8666 long address;
8667 int num_widens = 0;
8668 int wide_nops = 0;
8669 int narrow_nops = 0;
8670 bfd_boolean paddable = FALSE;
8671 offsetT local_opt_diff;
8672 offsetT opt_diff;
8673 offsetT max_diff;
8674 int stretch_amount = 0;
8675 int local_stretch_amount;
8676 int global_stretch_amount;
8677
8678 address = find_address_of_next_align_frag
8679 (&fragP, &wide_nops, &narrow_nops, &num_widens, &paddable);
8680
8681 if (!address)
8682 {
8683 if (this_frag->tc_frag_data.is_aligning_branch)
8684 this_frag->tc_frag_data.slot_subtypes[0] = RELAX_IMMED;
8685 else
8686 frag_wane (this_frag);
8687 }
8688 else
8689 {
8690 local_opt_diff = get_aligned_diff (fragP, address, &max_diff);
8691 opt_diff = local_opt_diff;
8692 assert (opt_diff >= 0);
8693 assert (max_diff >= opt_diff);
8694 if (max_diff == 0)
8695 return 0;
8696
8697 if (fragP)
8698 fragP = fragP->fr_next;
8699
8700 while (fragP && opt_diff < max_diff && address)
8701 {
8702 /* We only use these to determine if we can exit early
8703 because there will be plenty of ways to align future
8704 align frags. */
8705 int glob_widens = 0;
8706 int dnn = 0;
8707 int dw = 0;
8708 bfd_boolean glob_pad = 0;
8709 address = find_address_of_next_align_frag
8710 (&fragP, &glob_widens, &dnn, &dw, &glob_pad);
8711 /* If there is a padable portion, then skip. */
8712 if (glob_pad || glob_widens >= (1 << branch_align_power (now_seg)))
8713 address = 0;
8714
8715 if (address)
8716 {
8717 offsetT next_m_diff;
8718 offsetT next_o_diff;
8719
8720 /* Downrange frags haven't had stretch added to them yet. */
8721 address += stretch;
8722
8723 /* The address also includes any text expansion from this
8724 frag in a previous pass, but we don't want that. */
8725 address -= this_frag->tc_frag_data.text_expansion[0];
8726
8727 /* Assume we are going to move at least opt_diff. In
8728 reality, we might not be able to, but assuming that
8729 we will helps catch cases where moving opt_diff pushes
8730 the next target from aligned to unaligned. */
8731 address += opt_diff;
8732
8733 next_o_diff = get_aligned_diff (fragP, address, &next_m_diff);
8734
8735 /* Now cleanup for the adjustments to address. */
8736 next_o_diff += opt_diff;
8737 next_m_diff += opt_diff;
8738 if (next_o_diff <= max_diff && next_o_diff > opt_diff)
8739 opt_diff = next_o_diff;
8740 if (next_m_diff < max_diff)
8741 max_diff = next_m_diff;
8742 fragP = fragP->fr_next;
8743 }
8744 }
8745
8746 /* If there are enough wideners in between, do it. */
8747 if (paddable)
8748 {
8749 if (this_frag->fr_subtype == RELAX_UNREACHABLE)
8750 {
8751 assert (opt_diff <= UNREACHABLE_MAX_WIDTH);
8752 return opt_diff;
8753 }
8754 return 0;
8755 }
8756 local_stretch_amount
8757 = bytes_to_stretch (this_frag, wide_nops, narrow_nops,
8758 num_widens, local_opt_diff);
8759 global_stretch_amount
8760 = bytes_to_stretch (this_frag, wide_nops, narrow_nops,
8761 num_widens, opt_diff);
8762 /* If the condition below is true, then the frag couldn't
8763 stretch the correct amount for the global case, so we just
8764 optimize locally. We'll rely on the subsequent frags to get
8765 the correct alignment in the global case. */
8766 if (global_stretch_amount < local_stretch_amount)
8767 stretch_amount = local_stretch_amount;
8768 else
8769 stretch_amount = global_stretch_amount;
8770
8771 if (this_frag->fr_subtype == RELAX_SLOTS
8772 && this_frag->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
8773 assert (stretch_amount <= 1);
8774 else if (this_frag->fr_subtype == RELAX_FILL_NOP)
8775 {
8776 if (this_frag->tc_frag_data.is_no_density)
8777 assert (stretch_amount == 3 || stretch_amount == 0);
8778 else
8779 assert (stretch_amount <= 3);
8780 }
8781 }
8782 return stretch_amount;
8783 }
8784
8785
8786 /* The idea: widen everything you can to get a target or loop aligned,
8787 then start using NOPs.
8788
8789 When we must have a NOP, here is a table of how we decide
8790 (so you don't have to fight through the control flow below):
8791
8792 wide_nops = the number of wide NOPs available for aligning
8793 narrow_nops = the number of narrow NOPs available for aligning
8794 (a subset of wide_nops)
8795 widens = the number of narrow instructions that should be widened
8796
8797 Desired wide narrow
8798 Diff nop nop widens
8799 1 0 0 1
8800 2 0 1 0
8801 3a 1 0 0
8802 b 0 1 1 (case 3a makes this case unnecessary)
8803 4a 1 0 1
8804 b 0 2 0
8805 c 0 1 2 (case 4a makes this case unnecessary)
8806 5a 1 0 2
8807 b 1 1 0
8808 c 0 2 1 (case 5b makes this case unnecessary)
8809 6a 2 0 0
8810 b 1 0 3
8811 c 0 1 4 (case 6b makes this case unnecessary)
8812 d 1 1 1 (case 6a makes this case unnecessary)
8813 e 0 2 2 (case 6a makes this case unnecessary)
8814 f 0 3 0 (case 6a makes this case unnecessary)
8815 7a 1 0 4
8816 b 2 0 1
8817 c 1 1 2 (case 7b makes this case unnecessary)
8818 d 0 1 5 (case 7a makes this case unnecessary)
8819 e 0 2 3 (case 7b makes this case unnecessary)
8820 f 0 3 1 (case 7b makes this case unnecessary)
8821 g 1 2 1 (case 7b makes this case unnecessary)
8822 */
8823
8824 static long
8825 bytes_to_stretch (fragS *this_frag,
8826 int wide_nops,
8827 int narrow_nops,
8828 int num_widens,
8829 int desired_diff)
8830 {
8831 int bytes_short = desired_diff - num_widens;
8832
8833 assert (desired_diff >= 0 && desired_diff < 8);
8834 if (desired_diff == 0)
8835 return 0;
8836
8837 assert (wide_nops > 0 || num_widens > 0);
8838
8839 /* Always prefer widening to NOP-filling. */
8840 if (bytes_short < 0)
8841 {
8842 /* There are enough RELAX_NARROW frags after this one
8843 to align the target without widening this frag in any way. */
8844 return 0;
8845 }
8846
8847 if (bytes_short == 0)
8848 {
8849 /* Widen every narrow between here and the align target
8850 and the align target will be properly aligned. */
8851 if (this_frag->fr_subtype == RELAX_FILL_NOP)
8852 return 0;
8853 else
8854 return 1;
8855 }
8856
8857 /* From here we will need at least one NOP to get an alignment.
8858 However, we may not be able to align at all, in which case,
8859 don't widen. */
8860 if (this_frag->fr_subtype == RELAX_FILL_NOP)
8861 {
8862 switch (desired_diff)
8863 {
8864 case 1:
8865 return 0;
8866 case 2:
8867 if (!this_frag->tc_frag_data.is_no_density && narrow_nops == 1)
8868 return 2; /* case 2 */
8869 return 0;
8870 case 3:
8871 if (wide_nops > 1)
8872 return 0;
8873 else
8874 return 3; /* case 3a */
8875 case 4:
8876 if (num_widens >= 1 && wide_nops == 1)
8877 return 3; /* case 4a */
8878 if (!this_frag->tc_frag_data.is_no_density && narrow_nops == 2)
8879 return 2; /* case 4b */
8880 return 0;
8881 case 5:
8882 if (num_widens >= 2 && wide_nops == 1)
8883 return 3; /* case 5a */
8884 /* We will need two nops. Are there enough nops
8885 between here and the align target? */
8886 if (wide_nops < 2 || narrow_nops == 0)
8887 return 0;
8888 /* Are there other nops closer that can serve instead? */
8889 if (wide_nops > 2 && narrow_nops > 1)
8890 return 0;
8891 /* Take the density one first, because there might not be
8892 another density one available. */
8893 if (!this_frag->tc_frag_data.is_no_density)
8894 return 2; /* case 5b narrow */
8895 else
8896 return 3; /* case 5b wide */
8897 return 0;
8898 case 6:
8899 if (wide_nops == 2)
8900 return 3; /* case 6a */
8901 else if (num_widens >= 3 && wide_nops == 1)
8902 return 3; /* case 6b */
8903 return 0;
8904 case 7:
8905 if (wide_nops == 1 && num_widens >= 4)
8906 return 3; /* case 7a */
8907 else if (wide_nops == 2 && num_widens >= 1)
8908 return 3; /* case 7b */
8909 return 0;
8910 default:
8911 assert (0);
8912 }
8913 }
8914 else
8915 {
8916 /* We will need a NOP no matter what, but should we widen
8917 this instruction to help?
8918
8919 This is a RELAX_NARROW frag. */
8920 switch (desired_diff)
8921 {
8922 case 1:
8923 assert (0);
8924 return 0;
8925 case 2:
8926 case 3:
8927 return 0;
8928 case 4:
8929 if (wide_nops >= 1 && num_widens == 1)
8930 return 1; /* case 4a */
8931 return 0;
8932 case 5:
8933 if (wide_nops >= 1 && num_widens == 2)
8934 return 1; /* case 5a */
8935 return 0;
8936 case 6:
8937 if (wide_nops >= 2)
8938 return 0; /* case 6a */
8939 else if (wide_nops >= 1 && num_widens == 3)
8940 return 1; /* case 6b */
8941 return 0;
8942 case 7:
8943 if (wide_nops >= 1 && num_widens == 4)
8944 return 1; /* case 7a */
8945 else if (wide_nops >= 2 && num_widens == 1)
8946 return 1; /* case 7b */
8947 return 0;
8948 default:
8949 assert (0);
8950 return 0;
8951 }
8952 }
8953 assert (0);
8954 return 0;
8955 }
8956
8957
8958 static long
8959 relax_frag_immed (segT segP,
8960 fragS *fragP,
8961 long stretch,
8962 int min_steps,
8963 xtensa_format fmt,
8964 int slot,
8965 int *stretched_p,
8966 bfd_boolean estimate_only)
8967 {
8968 TInsn tinsn;
8969 int old_size;
8970 bfd_boolean negatable_branch = FALSE;
8971 bfd_boolean branch_jmp_to_next = FALSE;
8972 bfd_boolean from_wide_insn = FALSE;
8973 xtensa_isa isa = xtensa_default_isa;
8974 IStack istack;
8975 offsetT frag_offset;
8976 int num_steps;
8977 fragS *lit_fragP;
8978 int num_text_bytes, num_literal_bytes;
8979 int literal_diff, total_text_diff, this_text_diff, first;
8980
8981 assert (fragP->fr_opcode != NULL);
8982
8983 xg_clear_vinsn (&cur_vinsn);
8984 vinsn_from_chars (&cur_vinsn, fragP->fr_opcode);
8985 if (cur_vinsn.num_slots > 1)
8986 from_wide_insn = TRUE;
8987
8988 tinsn = cur_vinsn.slots[slot];
8989 tinsn_immed_from_frag (&tinsn, fragP, slot);
8990
8991 if (estimate_only && xtensa_opcode_is_loop (isa, tinsn.opcode) == 1)
8992 return 0;
8993
8994 if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform)
8995 branch_jmp_to_next = is_branch_jmp_to_next (&tinsn, fragP);
8996
8997 negatable_branch = (xtensa_opcode_is_branch (isa, tinsn.opcode) == 1);
8998
8999 old_size = xtensa_format_length (isa, fmt);
9000
9001 /* Special case: replace a branch to the next instruction with a NOP.
9002 This is required to work around a hardware bug in T1040.0 and also
9003 serves as an optimization. */
9004
9005 if (branch_jmp_to_next
9006 && ((old_size == 2) || (old_size == 3))
9007 && !next_frag_is_loop_target (fragP))
9008 return 0;
9009
9010 /* Here is the fun stuff: Get the immediate field from this
9011 instruction. If it fits, we are done. If not, find the next
9012 instruction sequence that fits. */
9013
9014 frag_offset = fragP->fr_opcode - fragP->fr_literal;
9015 istack_init (&istack);
9016 num_steps = xg_assembly_relax (&istack, &tinsn, segP, fragP, frag_offset,
9017 min_steps, stretch);
9018 if (num_steps < min_steps)
9019 {
9020 as_fatal (_("internal error: relaxation failed"));
9021 return 0;
9022 }
9023
9024 if (num_steps > RELAX_IMMED_MAXSTEPS)
9025 {
9026 as_fatal (_("internal error: relaxation requires too many steps"));
9027 return 0;
9028 }
9029
9030 fragP->tc_frag_data.slot_subtypes[slot] = (int) RELAX_IMMED + num_steps;
9031
9032 /* Figure out the number of bytes needed. */
9033 lit_fragP = 0;
9034 num_literal_bytes = get_num_stack_literal_bytes (&istack);
9035 literal_diff =
9036 num_literal_bytes - fragP->tc_frag_data.literal_expansion[slot];
9037 first = 0;
9038 while (istack.insn[first].opcode == XTENSA_UNDEFINED)
9039 first++;
9040
9041 num_text_bytes = get_num_stack_text_bytes (&istack);
9042
9043 if (from_wide_insn)
9044 {
9045 num_text_bytes += old_size;
9046 if (opcode_fits_format_slot (istack.insn[first].opcode, fmt, slot))
9047 num_text_bytes -= xg_get_single_size (istack.insn[first].opcode);
9048 }
9049
9050 total_text_diff = num_text_bytes - old_size;
9051 this_text_diff = total_text_diff - fragP->tc_frag_data.text_expansion[slot];
9052
9053 /* It MUST get larger. If not, we could get an infinite loop. */
9054 assert (num_text_bytes >= 0);
9055 assert (literal_diff >= 0);
9056 assert (total_text_diff >= 0);
9057
9058 fragP->tc_frag_data.text_expansion[slot] = total_text_diff;
9059 fragP->tc_frag_data.literal_expansion[slot] = num_literal_bytes;
9060 assert (fragP->tc_frag_data.text_expansion[slot] >= 0);
9061 assert (fragP->tc_frag_data.literal_expansion[slot] >= 0);
9062
9063 /* Find the associated expandable literal for this. */
9064 if (literal_diff != 0)
9065 {
9066 lit_fragP = fragP->tc_frag_data.literal_frags[slot];
9067 if (lit_fragP)
9068 {
9069 assert (literal_diff == 4);
9070 lit_fragP->tc_frag_data.unreported_expansion += literal_diff;
9071
9072 /* We expect that the literal section state has NOT been
9073 modified yet. */
9074 assert (lit_fragP->fr_type == rs_machine_dependent
9075 && lit_fragP->fr_subtype == RELAX_LITERAL);
9076 lit_fragP->fr_subtype = RELAX_LITERAL_NR;
9077
9078 /* We need to mark this section for another iteration
9079 of relaxation. */
9080 (*stretched_p)++;
9081 }
9082 }
9083
9084 if (negatable_branch && istack.ninsn > 1)
9085 update_next_frag_state (fragP);
9086
9087 return this_text_diff;
9088 }
9089
9090 \f
9091 /* md_convert_frag Hook and Helper Functions. */
9092
9093 static void convert_frag_align_next_opcode (fragS *);
9094 static void convert_frag_narrow (segT, fragS *, xtensa_format, int);
9095 static void convert_frag_fill_nop (fragS *);
9096 static void convert_frag_immed (segT, fragS *, int, xtensa_format, int);
9097
9098 void
9099 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec, fragS *fragp)
9100 {
9101 static xtensa_insnbuf vbuf = NULL;
9102 xtensa_isa isa = xtensa_default_isa;
9103 int slot;
9104 int num_slots;
9105 xtensa_format fmt;
9106 char *file_name;
9107 unsigned line;
9108
9109 as_where (&file_name, &line);
9110 new_logical_line (fragp->fr_file, fragp->fr_line);
9111
9112 switch (fragp->fr_subtype)
9113 {
9114 case RELAX_ALIGN_NEXT_OPCODE:
9115 /* Always convert. */
9116 convert_frag_align_next_opcode (fragp);
9117 break;
9118
9119 case RELAX_DESIRE_ALIGN:
9120 /* Do nothing. If not aligned already, too bad. */
9121 break;
9122
9123 case RELAX_LITERAL:
9124 case RELAX_LITERAL_FINAL:
9125 break;
9126
9127 case RELAX_SLOTS:
9128 if (vbuf == NULL)
9129 vbuf = xtensa_insnbuf_alloc (isa);
9130
9131 xtensa_insnbuf_from_chars
9132 (isa, vbuf, (unsigned char *) fragp->fr_opcode, 0);
9133 fmt = xtensa_format_decode (isa, vbuf);
9134 num_slots = xtensa_format_num_slots (isa, fmt);
9135
9136 for (slot = 0; slot < num_slots; slot++)
9137 {
9138 switch (fragp->tc_frag_data.slot_subtypes[slot])
9139 {
9140 case RELAX_NARROW:
9141 convert_frag_narrow (sec, fragp, fmt, slot);
9142 break;
9143
9144 case RELAX_IMMED:
9145 case RELAX_IMMED_STEP1:
9146 case RELAX_IMMED_STEP2:
9147 case RELAX_IMMED_STEP3:
9148 /* Place the immediate. */
9149 convert_frag_immed
9150 (sec, fragp,
9151 fragp->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED,
9152 fmt, slot);
9153 break;
9154
9155 default:
9156 /* This is OK because some slots could have
9157 relaxations and others have none. */
9158 break;
9159 }
9160 }
9161 break;
9162
9163 case RELAX_UNREACHABLE:
9164 memset (&fragp->fr_literal[fragp->fr_fix], 0, fragp->fr_var);
9165 fragp->fr_fix += fragp->tc_frag_data.text_expansion[0];
9166 fragp->fr_var -= fragp->tc_frag_data.text_expansion[0];
9167 frag_wane (fragp);
9168 break;
9169
9170 case RELAX_MAYBE_UNREACHABLE:
9171 case RELAX_MAYBE_DESIRE_ALIGN:
9172 frag_wane (fragp);
9173 break;
9174
9175 case RELAX_FILL_NOP:
9176 convert_frag_fill_nop (fragp);
9177 break;
9178
9179 case RELAX_LITERAL_NR:
9180 if (use_literal_section)
9181 {
9182 /* This should have been handled during relaxation. When
9183 relaxing a code segment, literals sometimes need to be
9184 added to the corresponding literal segment. If that
9185 literal segment has already been relaxed, then we end up
9186 in this situation. Marking the literal segments as data
9187 would make this happen less often (since GAS always relaxes
9188 code before data), but we could still get into trouble if
9189 there are instructions in a segment that is not marked as
9190 containing code. Until we can implement a better solution,
9191 cheat and adjust the addresses of all the following frags.
9192 This could break subsequent alignments, but the linker's
9193 literal coalescing will do that anyway. */
9194
9195 fragS *f;
9196 fragp->fr_subtype = RELAX_LITERAL_FINAL;
9197 assert (fragp->tc_frag_data.unreported_expansion == 4);
9198 memset (&fragp->fr_literal[fragp->fr_fix], 0, 4);
9199 fragp->fr_var -= 4;
9200 fragp->fr_fix += 4;
9201 for (f = fragp->fr_next; f; f = f->fr_next)
9202 f->fr_address += 4;
9203 }
9204 else
9205 as_bad (_("invalid relaxation fragment result"));
9206 break;
9207 }
9208
9209 fragp->fr_var = 0;
9210 new_logical_line (file_name, line);
9211 }
9212
9213
9214 static void
9215 convert_frag_align_next_opcode (fragS *fragp)
9216 {
9217 char *nop_buf; /* Location for Writing. */
9218 bfd_boolean use_no_density = fragp->tc_frag_data.is_no_density;
9219 addressT aligned_address;
9220 offsetT fill_size;
9221 int nop, nop_count;
9222
9223 aligned_address = get_noop_aligned_address (fragp, fragp->fr_address +
9224 fragp->fr_fix);
9225 fill_size = aligned_address - (fragp->fr_address + fragp->fr_fix);
9226 nop_count = get_text_align_nop_count (fill_size, use_no_density);
9227 nop_buf = fragp->fr_literal + fragp->fr_fix;
9228
9229 for (nop = 0; nop < nop_count; nop++)
9230 {
9231 int nop_size;
9232 nop_size = get_text_align_nth_nop_size (fill_size, nop, use_no_density);
9233
9234 assemble_nop (nop_size, nop_buf);
9235 nop_buf += nop_size;
9236 }
9237
9238 fragp->fr_fix += fill_size;
9239 fragp->fr_var -= fill_size;
9240 }
9241
9242
9243 static void
9244 convert_frag_narrow (segT segP, fragS *fragP, xtensa_format fmt, int slot)
9245 {
9246 TInsn tinsn, single_target;
9247 int size, old_size, diff;
9248 offsetT frag_offset;
9249
9250 assert (slot == 0);
9251 tinsn_from_chars (&tinsn, fragP->fr_opcode, 0);
9252
9253 if (fragP->tc_frag_data.is_aligning_branch == 1)
9254 {
9255 assert (fragP->tc_frag_data.text_expansion[0] == 1
9256 || fragP->tc_frag_data.text_expansion[0] == 0);
9257 convert_frag_immed (segP, fragP, fragP->tc_frag_data.text_expansion[0],
9258 fmt, slot);
9259 return;
9260 }
9261
9262 if (fragP->tc_frag_data.text_expansion[0] == 0)
9263 {
9264 /* No conversion. */
9265 fragP->fr_var = 0;
9266 return;
9267 }
9268
9269 assert (fragP->fr_opcode != NULL);
9270
9271 /* Frags in this relaxation state should only contain
9272 single instruction bundles. */
9273 tinsn_immed_from_frag (&tinsn, fragP, 0);
9274
9275 /* Just convert it to a wide form.... */
9276 size = 0;
9277 old_size = xg_get_single_size (tinsn.opcode);
9278
9279 tinsn_init (&single_target);
9280 frag_offset = fragP->fr_opcode - fragP->fr_literal;
9281
9282 if (! xg_is_single_relaxable_insn (&tinsn, &single_target, FALSE))
9283 {
9284 as_bad (_("unable to widen instruction"));
9285 return;
9286 }
9287
9288 size = xg_get_single_size (single_target.opcode);
9289 xg_emit_insn_to_buf (&single_target, fragP->fr_opcode, fragP,
9290 frag_offset, TRUE);
9291
9292 diff = size - old_size;
9293 assert (diff >= 0);
9294 assert (diff <= fragP->fr_var);
9295 fragP->fr_var -= diff;
9296 fragP->fr_fix += diff;
9297
9298 /* clean it up */
9299 fragP->fr_var = 0;
9300 }
9301
9302
9303 static void
9304 convert_frag_fill_nop (fragS *fragP)
9305 {
9306 char *loc = &fragP->fr_literal[fragP->fr_fix];
9307 int size = fragP->tc_frag_data.text_expansion[0];
9308 assert ((unsigned) size == (fragP->fr_next->fr_address
9309 - fragP->fr_address - fragP->fr_fix));
9310 if (size == 0)
9311 {
9312 /* No conversion. */
9313 fragP->fr_var = 0;
9314 return;
9315 }
9316 assemble_nop (size, loc);
9317 fragP->tc_frag_data.is_insn = TRUE;
9318 fragP->fr_var -= size;
9319 fragP->fr_fix += size;
9320 frag_wane (fragP);
9321 }
9322
9323
9324 static fixS *fix_new_exp_in_seg
9325 (segT, subsegT, fragS *, int, int, expressionS *, int,
9326 bfd_reloc_code_real_type);
9327 static void convert_frag_immed_finish_loop (segT, fragS *, TInsn *);
9328
9329 static void
9330 convert_frag_immed (segT segP,
9331 fragS *fragP,
9332 int min_steps,
9333 xtensa_format fmt,
9334 int slot)
9335 {
9336 char *immed_instr = fragP->fr_opcode;
9337 TInsn orig_tinsn;
9338 bfd_boolean expanded = FALSE;
9339 bfd_boolean branch_jmp_to_next = FALSE;
9340 char *fr_opcode = fragP->fr_opcode;
9341 xtensa_isa isa = xtensa_default_isa;
9342 bfd_boolean from_wide_insn = FALSE;
9343 int bytes;
9344 bfd_boolean is_loop;
9345
9346 assert (fr_opcode != NULL);
9347
9348 xg_clear_vinsn (&cur_vinsn);
9349
9350 vinsn_from_chars (&cur_vinsn, fr_opcode);
9351 if (cur_vinsn.num_slots > 1)
9352 from_wide_insn = TRUE;
9353
9354 orig_tinsn = cur_vinsn.slots[slot];
9355 tinsn_immed_from_frag (&orig_tinsn, fragP, slot);
9356
9357 is_loop = xtensa_opcode_is_loop (xtensa_default_isa, orig_tinsn.opcode) == 1;
9358
9359 if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform)
9360 branch_jmp_to_next = is_branch_jmp_to_next (&orig_tinsn, fragP);
9361
9362 if (branch_jmp_to_next && !next_frag_is_loop_target (fragP))
9363 {
9364 /* Conversion just inserts a NOP and marks the fix as completed. */
9365 bytes = xtensa_format_length (isa, fmt);
9366 if (bytes >= 4)
9367 {
9368 cur_vinsn.slots[slot].opcode =
9369 xtensa_format_slot_nop_opcode (isa, cur_vinsn.format, slot);
9370 cur_vinsn.slots[slot].ntok = 0;
9371 }
9372 else
9373 {
9374 bytes += fragP->tc_frag_data.text_expansion[0];
9375 assert (bytes == 2 || bytes == 3);
9376 build_nop (&cur_vinsn.slots[0], bytes);
9377 fragP->fr_fix += fragP->tc_frag_data.text_expansion[0];
9378 }
9379 vinsn_to_insnbuf (&cur_vinsn, fr_opcode, frag_now, TRUE);
9380 xtensa_insnbuf_to_chars
9381 (isa, cur_vinsn.insnbuf, (unsigned char *) fr_opcode, 0);
9382 fragP->fr_var = 0;
9383 }
9384 else
9385 {
9386 /* Here is the fun stuff: Get the immediate field from this
9387 instruction. If it fits, we're done. If not, find the next
9388 instruction sequence that fits. */
9389
9390 IStack istack;
9391 int i;
9392 symbolS *lit_sym = NULL;
9393 int total_size = 0;
9394 int target_offset = 0;
9395 int old_size;
9396 int diff;
9397 symbolS *gen_label = NULL;
9398 offsetT frag_offset;
9399 bfd_boolean first = TRUE;
9400 bfd_boolean last_is_jump;
9401
9402 /* It does not fit. Find something that does and
9403 convert immediately. */
9404 frag_offset = fr_opcode - fragP->fr_literal;
9405 istack_init (&istack);
9406 xg_assembly_relax (&istack, &orig_tinsn,
9407 segP, fragP, frag_offset, min_steps, 0);
9408
9409 old_size = xtensa_format_length (isa, fmt);
9410
9411 /* Assemble this right inline. */
9412
9413 /* First, create the mapping from a label name to the REAL label. */
9414 target_offset = 0;
9415 for (i = 0; i < istack.ninsn; i++)
9416 {
9417 TInsn *tinsn = &istack.insn[i];
9418 fragS *lit_frag;
9419
9420 switch (tinsn->insn_type)
9421 {
9422 case ITYPE_LITERAL:
9423 if (lit_sym != NULL)
9424 as_bad (_("multiple literals in expansion"));
9425 /* First find the appropriate space in the literal pool. */
9426 lit_frag = fragP->tc_frag_data.literal_frags[slot];
9427 if (lit_frag == NULL)
9428 as_bad (_("no registered fragment for literal"));
9429 if (tinsn->ntok != 1)
9430 as_bad (_("number of literal tokens != 1"));
9431
9432 /* Set the literal symbol and add a fixup. */
9433 lit_sym = lit_frag->fr_symbol;
9434 break;
9435
9436 case ITYPE_LABEL:
9437 if (align_targets && !is_loop)
9438 {
9439 fragS *unreach = fragP->fr_next;
9440 while (!(unreach->fr_type == rs_machine_dependent
9441 && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE
9442 || unreach->fr_subtype == RELAX_UNREACHABLE)))
9443 {
9444 unreach = unreach->fr_next;
9445 }
9446
9447 assert (unreach->fr_type == rs_machine_dependent
9448 && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE
9449 || unreach->fr_subtype == RELAX_UNREACHABLE));
9450
9451 target_offset += unreach->tc_frag_data.text_expansion[0];
9452 }
9453 assert (gen_label == NULL);
9454 gen_label = symbol_new (FAKE_LABEL_NAME, now_seg,
9455 fr_opcode - fragP->fr_literal
9456 + target_offset, fragP);
9457 break;
9458
9459 case ITYPE_INSN:
9460 if (first && from_wide_insn)
9461 {
9462 target_offset += xtensa_format_length (isa, fmt);
9463 first = FALSE;
9464 if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot))
9465 target_offset += xg_get_single_size (tinsn->opcode);
9466 }
9467 else
9468 target_offset += xg_get_single_size (tinsn->opcode);
9469 break;
9470 }
9471 }
9472
9473 total_size = 0;
9474 first = TRUE;
9475 last_is_jump = FALSE;
9476 for (i = 0; i < istack.ninsn; i++)
9477 {
9478 TInsn *tinsn = &istack.insn[i];
9479 fragS *lit_frag;
9480 int size;
9481 segT target_seg;
9482 bfd_reloc_code_real_type reloc_type;
9483
9484 switch (tinsn->insn_type)
9485 {
9486 case ITYPE_LITERAL:
9487 lit_frag = fragP->tc_frag_data.literal_frags[slot];
9488 /* Already checked. */
9489 assert (lit_frag != NULL);
9490 assert (lit_sym != NULL);
9491 assert (tinsn->ntok == 1);
9492 /* Add a fixup. */
9493 target_seg = S_GET_SEGMENT (lit_sym);
9494 assert (target_seg);
9495 reloc_type = map_operator_to_reloc (tinsn->tok[0].X_op);
9496 fix_new_exp_in_seg (target_seg, 0, lit_frag, 0, 4,
9497 &tinsn->tok[0], FALSE, reloc_type);
9498 break;
9499
9500 case ITYPE_LABEL:
9501 break;
9502
9503 case ITYPE_INSN:
9504 xg_resolve_labels (tinsn, gen_label);
9505 xg_resolve_literals (tinsn, lit_sym);
9506 if (from_wide_insn && first)
9507 {
9508 first = FALSE;
9509 if (opcode_fits_format_slot (tinsn->opcode, fmt, slot))
9510 {
9511 cur_vinsn.slots[slot] = *tinsn;
9512 }
9513 else
9514 {
9515 cur_vinsn.slots[slot].opcode =
9516 xtensa_format_slot_nop_opcode (isa, fmt, slot);
9517 cur_vinsn.slots[slot].ntok = 0;
9518 }
9519 vinsn_to_insnbuf (&cur_vinsn, immed_instr, fragP, TRUE);
9520 xtensa_insnbuf_to_chars (isa, cur_vinsn.insnbuf,
9521 (unsigned char *) immed_instr, 0);
9522 fragP->tc_frag_data.is_insn = TRUE;
9523 size = xtensa_format_length (isa, fmt);
9524 if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot))
9525 {
9526 xg_emit_insn_to_buf
9527 (tinsn, immed_instr + size, fragP,
9528 immed_instr - fragP->fr_literal + size, TRUE);
9529 size += xg_get_single_size (tinsn->opcode);
9530 }
9531 }
9532 else
9533 {
9534 size = xg_get_single_size (tinsn->opcode);
9535 xg_emit_insn_to_buf (tinsn, immed_instr, fragP,
9536 immed_instr - fragP->fr_literal, TRUE);
9537 }
9538 immed_instr += size;
9539 total_size += size;
9540 break;
9541 }
9542 }
9543
9544 diff = total_size - old_size;
9545 assert (diff >= 0);
9546 if (diff != 0)
9547 expanded = TRUE;
9548 assert (diff <= fragP->fr_var);
9549 fragP->fr_var -= diff;
9550 fragP->fr_fix += diff;
9551 }
9552
9553 /* Check for undefined immediates in LOOP instructions. */
9554 if (is_loop)
9555 {
9556 symbolS *sym;
9557 sym = orig_tinsn.tok[1].X_add_symbol;
9558 if (sym != NULL && !S_IS_DEFINED (sym))
9559 {
9560 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
9561 return;
9562 }
9563 sym = orig_tinsn.tok[1].X_op_symbol;
9564 if (sym != NULL && !S_IS_DEFINED (sym))
9565 {
9566 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
9567 return;
9568 }
9569 }
9570
9571 if (expanded && xtensa_opcode_is_loop (isa, orig_tinsn.opcode) == 1)
9572 convert_frag_immed_finish_loop (segP, fragP, &orig_tinsn);
9573
9574 if (expanded && is_direct_call_opcode (orig_tinsn.opcode))
9575 {
9576 /* Add an expansion note on the expanded instruction. */
9577 fix_new_exp_in_seg (now_seg, 0, fragP, fr_opcode - fragP->fr_literal, 4,
9578 &orig_tinsn.tok[0], TRUE,
9579 BFD_RELOC_XTENSA_ASM_EXPAND);
9580 }
9581 }
9582
9583
9584 /* Add a new fix expression into the desired segment. We have to
9585 switch to that segment to do this. */
9586
9587 static fixS *
9588 fix_new_exp_in_seg (segT new_seg,
9589 subsegT new_subseg,
9590 fragS *frag,
9591 int where,
9592 int size,
9593 expressionS *exp,
9594 int pcrel,
9595 bfd_reloc_code_real_type r_type)
9596 {
9597 fixS *new_fix;
9598 segT seg = now_seg;
9599 subsegT subseg = now_subseg;
9600
9601 assert (new_seg != 0);
9602 subseg_set (new_seg, new_subseg);
9603
9604 new_fix = fix_new_exp (frag, where, size, exp, pcrel, r_type);
9605 subseg_set (seg, subseg);
9606 return new_fix;
9607 }
9608
9609
9610 /* Relax a loop instruction so that it can span loop >256 bytes.
9611
9612 loop as, .L1
9613 .L0:
9614 rsr as, LEND
9615 wsr as, LBEG
9616 addi as, as, lo8 (label-.L1)
9617 addmi as, as, mid8 (label-.L1)
9618 wsr as, LEND
9619 isync
9620 rsr as, LCOUNT
9621 addi as, as, 1
9622 .L1:
9623 <<body>>
9624 label:
9625 */
9626
9627 static void
9628 convert_frag_immed_finish_loop (segT segP, fragS *fragP, TInsn *tinsn)
9629 {
9630 TInsn loop_insn;
9631 TInsn addi_insn;
9632 TInsn addmi_insn;
9633 unsigned long target;
9634 static xtensa_insnbuf insnbuf = NULL;
9635 unsigned int loop_length, loop_length_hi, loop_length_lo;
9636 xtensa_isa isa = xtensa_default_isa;
9637 addressT loop_offset;
9638 addressT addi_offset = 9;
9639 addressT addmi_offset = 12;
9640 fragS *next_fragP;
9641 int target_count;
9642
9643 if (!insnbuf)
9644 insnbuf = xtensa_insnbuf_alloc (isa);
9645
9646 /* Get the loop offset. */
9647 loop_offset = get_expanded_loop_offset (tinsn->opcode);
9648
9649 /* Validate that there really is a LOOP at the loop_offset. Because
9650 loops are not bundleable, we can assume that the instruction will be
9651 in slot 0. */
9652 tinsn_from_chars (&loop_insn, fragP->fr_opcode + loop_offset, 0);
9653 tinsn_immed_from_frag (&loop_insn, fragP, 0);
9654
9655 assert (xtensa_opcode_is_loop (isa, loop_insn.opcode) == 1);
9656 addi_offset += loop_offset;
9657 addmi_offset += loop_offset;
9658
9659 assert (tinsn->ntok == 2);
9660 if (tinsn->tok[1].X_op == O_constant)
9661 target = tinsn->tok[1].X_add_number;
9662 else if (tinsn->tok[1].X_op == O_symbol)
9663 {
9664 /* Find the fragment. */
9665 symbolS *sym = tinsn->tok[1].X_add_symbol;
9666 assert (S_GET_SEGMENT (sym) == segP
9667 || S_GET_SEGMENT (sym) == absolute_section);
9668 target = (S_GET_VALUE (sym) + tinsn->tok[1].X_add_number);
9669 }
9670 else
9671 {
9672 as_bad (_("invalid expression evaluation type %d"), tinsn->tok[1].X_op);
9673 target = 0;
9674 }
9675
9676 loop_length = target - (fragP->fr_address + fragP->fr_fix);
9677 loop_length_hi = loop_length & ~0x0ff;
9678 loop_length_lo = loop_length & 0x0ff;
9679 if (loop_length_lo >= 128)
9680 {
9681 loop_length_lo -= 256;
9682 loop_length_hi += 256;
9683 }
9684
9685 /* Because addmi sign-extends the immediate, 'loop_length_hi' can be at most
9686 32512. If the loop is larger than that, then we just fail. */
9687 if (loop_length_hi > 32512)
9688 as_bad_where (fragP->fr_file, fragP->fr_line,
9689 _("loop too long for LOOP instruction"));
9690
9691 tinsn_from_chars (&addi_insn, fragP->fr_opcode + addi_offset, 0);
9692 assert (addi_insn.opcode == xtensa_addi_opcode);
9693
9694 tinsn_from_chars (&addmi_insn, fragP->fr_opcode + addmi_offset, 0);
9695 assert (addmi_insn.opcode == xtensa_addmi_opcode);
9696
9697 set_expr_const (&addi_insn.tok[2], loop_length_lo);
9698 tinsn_to_insnbuf (&addi_insn, insnbuf);
9699
9700 fragP->tc_frag_data.is_insn = TRUE;
9701 xtensa_insnbuf_to_chars
9702 (isa, insnbuf, (unsigned char *) fragP->fr_opcode + addi_offset, 0);
9703
9704 set_expr_const (&addmi_insn.tok[2], loop_length_hi);
9705 tinsn_to_insnbuf (&addmi_insn, insnbuf);
9706 xtensa_insnbuf_to_chars
9707 (isa, insnbuf, (unsigned char *) fragP->fr_opcode + addmi_offset, 0);
9708
9709 /* Walk through all of the frags from here to the loop end
9710 and mark them as no_transform to keep them from being modified
9711 by the linker. If we ever have a relocation for the
9712 addi/addmi of the difference of two symbols we can remove this. */
9713
9714 target_count = 0;
9715 for (next_fragP = fragP; next_fragP != NULL;
9716 next_fragP = next_fragP->fr_next)
9717 {
9718 next_fragP->tc_frag_data.is_no_transform = TRUE;
9719 if (next_fragP->tc_frag_data.is_loop_target)
9720 target_count++;
9721 if (target_count == 2)
9722 break;
9723 }
9724 }
9725
9726 \f
9727 /* A map that keeps information on a per-subsegment basis. This is
9728 maintained during initial assembly, but is invalid once the
9729 subsegments are smashed together. I.E., it cannot be used during
9730 the relaxation. */
9731
9732 typedef struct subseg_map_struct
9733 {
9734 /* the key */
9735 segT seg;
9736 subsegT subseg;
9737
9738 /* the data */
9739 unsigned flags;
9740 float total_freq; /* fall-through + branch target frequency */
9741 float target_freq; /* branch target frequency alone */
9742
9743 struct subseg_map_struct *next;
9744 } subseg_map;
9745
9746
9747 static subseg_map *sseg_map = NULL;
9748
9749 static subseg_map *
9750 get_subseg_info (segT seg, subsegT subseg)
9751 {
9752 subseg_map *subseg_e;
9753
9754 for (subseg_e = sseg_map; subseg_e; subseg_e = subseg_e->next)
9755 {
9756 if (seg == subseg_e->seg && subseg == subseg_e->subseg)
9757 break;
9758 }
9759 return subseg_e;
9760 }
9761
9762
9763 static subseg_map *
9764 add_subseg_info (segT seg, subsegT subseg)
9765 {
9766 subseg_map *subseg_e = (subseg_map *) xmalloc (sizeof (subseg_map));
9767 memset (subseg_e, 0, sizeof (subseg_map));
9768 subseg_e->seg = seg;
9769 subseg_e->subseg = subseg;
9770 subseg_e->flags = 0;
9771 /* Start off considering every branch target very important. */
9772 subseg_e->target_freq = 1.0;
9773 subseg_e->total_freq = 1.0;
9774 subseg_e->next = sseg_map;
9775 sseg_map = subseg_e;
9776 return subseg_e;
9777 }
9778
9779
9780 static unsigned
9781 get_last_insn_flags (segT seg, subsegT subseg)
9782 {
9783 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9784 if (subseg_e)
9785 return subseg_e->flags;
9786 return 0;
9787 }
9788
9789
9790 static void
9791 set_last_insn_flags (segT seg,
9792 subsegT subseg,
9793 unsigned fl,
9794 bfd_boolean val)
9795 {
9796 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9797 if (! subseg_e)
9798 subseg_e = add_subseg_info (seg, subseg);
9799 if (val)
9800 subseg_e->flags |= fl;
9801 else
9802 subseg_e->flags &= ~fl;
9803 }
9804
9805
9806 static float
9807 get_subseg_total_freq (segT seg, subsegT subseg)
9808 {
9809 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9810 if (subseg_e)
9811 return subseg_e->total_freq;
9812 return 1.0;
9813 }
9814
9815
9816 static float
9817 get_subseg_target_freq (segT seg, subsegT subseg)
9818 {
9819 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9820 if (subseg_e)
9821 return subseg_e->target_freq;
9822 return 1.0;
9823 }
9824
9825
9826 static void
9827 set_subseg_freq (segT seg, subsegT subseg, float total_f, float target_f)
9828 {
9829 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9830 if (! subseg_e)
9831 subseg_e = add_subseg_info (seg, subseg);
9832 subseg_e->total_freq = total_f;
9833 subseg_e->target_freq = target_f;
9834 }
9835
9836 \f
9837 /* Segment Lists and emit_state Stuff. */
9838
9839 static void
9840 xtensa_move_seg_list_to_beginning (seg_list *head)
9841 {
9842 head = head->next;
9843 while (head)
9844 {
9845 segT literal_section = head->seg;
9846
9847 /* Move the literal section to the front of the section list. */
9848 assert (literal_section);
9849 if (literal_section != stdoutput->sections)
9850 {
9851 bfd_section_list_remove (stdoutput, literal_section);
9852 bfd_section_list_prepend (stdoutput, literal_section);
9853 }
9854 head = head->next;
9855 }
9856 }
9857
9858
9859 static void mark_literal_frags (seg_list *);
9860
9861 static void
9862 xtensa_move_literals (void)
9863 {
9864 seg_list *segment;
9865 frchainS *frchain_from, *frchain_to;
9866 fragS *search_frag, *next_frag, *last_frag, *literal_pool, *insert_after;
9867 fragS **frag_splice;
9868 emit_state state;
9869 segT dest_seg;
9870 fixS *fix, *next_fix, **fix_splice;
9871 sym_list *lit;
9872
9873 mark_literal_frags (literal_head->next);
9874
9875 if (use_literal_section)
9876 return;
9877
9878 for (segment = literal_head->next; segment; segment = segment->next)
9879 {
9880 /* Keep the literals for .init and .fini in separate sections. */
9881 if (!strcmp (segment_name (segment->seg), INIT_SECTION_NAME)
9882 || !strcmp (segment_name (segment->seg), FINI_SECTION_NAME))
9883 continue;
9884
9885 frchain_from = seg_info (segment->seg)->frchainP;
9886 search_frag = frchain_from->frch_root;
9887 literal_pool = NULL;
9888 frchain_to = NULL;
9889 frag_splice = &(frchain_from->frch_root);
9890
9891 while (!search_frag->tc_frag_data.literal_frag)
9892 {
9893 assert (search_frag->fr_fix == 0
9894 || search_frag->fr_type == rs_align);
9895 search_frag = search_frag->fr_next;
9896 }
9897
9898 assert (search_frag->tc_frag_data.literal_frag->fr_subtype
9899 == RELAX_LITERAL_POOL_BEGIN);
9900 xtensa_switch_section_emit_state (&state, segment->seg, 0);
9901
9902 /* Make sure that all the frags in this series are closed, and
9903 that there is at least one left over of zero-size. This
9904 prevents us from making a segment with an frchain without any
9905 frags in it. */
9906 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
9907 xtensa_set_frag_assembly_state (frag_now);
9908 last_frag = frag_now;
9909 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
9910 xtensa_set_frag_assembly_state (frag_now);
9911
9912 while (search_frag != frag_now)
9913 {
9914 next_frag = search_frag->fr_next;
9915
9916 /* First, move the frag out of the literal section and
9917 to the appropriate place. */
9918 if (search_frag->tc_frag_data.literal_frag)
9919 {
9920 literal_pool = search_frag->tc_frag_data.literal_frag;
9921 assert (literal_pool->fr_subtype == RELAX_LITERAL_POOL_BEGIN);
9922 frchain_to = literal_pool->tc_frag_data.lit_frchain;
9923 assert (frchain_to);
9924 }
9925 insert_after = literal_pool->tc_frag_data.literal_frag;
9926 dest_seg = insert_after->fr_next->tc_frag_data.lit_seg;
9927
9928 *frag_splice = next_frag;
9929 search_frag->fr_next = insert_after->fr_next;
9930 insert_after->fr_next = search_frag;
9931 search_frag->tc_frag_data.lit_seg = dest_seg;
9932 literal_pool->tc_frag_data.literal_frag = search_frag;
9933
9934 /* Now move any fixups associated with this frag to the
9935 right section. */
9936 fix = frchain_from->fix_root;
9937 fix_splice = &(frchain_from->fix_root);
9938 while (fix)
9939 {
9940 next_fix = fix->fx_next;
9941 if (fix->fx_frag == search_frag)
9942 {
9943 *fix_splice = next_fix;
9944 fix->fx_next = frchain_to->fix_root;
9945 frchain_to->fix_root = fix;
9946 if (frchain_to->fix_tail == NULL)
9947 frchain_to->fix_tail = fix;
9948 }
9949 else
9950 fix_splice = &(fix->fx_next);
9951 fix = next_fix;
9952 }
9953 search_frag = next_frag;
9954 }
9955
9956 if (frchain_from->fix_root != NULL)
9957 {
9958 frchain_from = seg_info (segment->seg)->frchainP;
9959 as_warn (_("fixes not all moved from %s"), segment->seg->name);
9960
9961 assert (frchain_from->fix_root == NULL);
9962 }
9963 frchain_from->fix_tail = NULL;
9964 xtensa_restore_emit_state (&state);
9965 }
9966
9967 /* Now fix up the SEGMENT value for all the literal symbols. */
9968 for (lit = literal_syms; lit; lit = lit->next)
9969 {
9970 symbolS *lit_sym = lit->sym;
9971 segT dest_seg = symbol_get_frag (lit_sym)->tc_frag_data.lit_seg;
9972 if (dest_seg)
9973 S_SET_SEGMENT (lit_sym, dest_seg);
9974 }
9975 }
9976
9977
9978 /* Walk over all the frags for segments in a list and mark them as
9979 containing literals. As clunky as this is, we can't rely on frag_var
9980 and frag_variant to get called in all situations. */
9981
9982 static void
9983 mark_literal_frags (seg_list *segment)
9984 {
9985 frchainS *frchain_from;
9986 fragS *search_frag;
9987
9988 while (segment)
9989 {
9990 frchain_from = seg_info (segment->seg)->frchainP;
9991 search_frag = frchain_from->frch_root;
9992 while (search_frag)
9993 {
9994 search_frag->tc_frag_data.is_literal = TRUE;
9995 search_frag = search_frag->fr_next;
9996 }
9997 segment = segment->next;
9998 }
9999 }
10000
10001
10002 static void
10003 xtensa_reorder_seg_list (seg_list *head, segT after)
10004 {
10005 /* Move all of the sections in the section list to come
10006 after "after" in the gnu segment list. */
10007
10008 head = head->next;
10009 while (head)
10010 {
10011 segT literal_section = head->seg;
10012
10013 /* Move the literal section after "after". */
10014 assert (literal_section);
10015 if (literal_section != after)
10016 {
10017 bfd_section_list_remove (stdoutput, literal_section);
10018 bfd_section_list_insert_after (stdoutput, after, literal_section);
10019 }
10020
10021 head = head->next;
10022 }
10023 }
10024
10025
10026 /* Push all the literal segments to the end of the gnu list. */
10027
10028 static void
10029 xtensa_reorder_segments (void)
10030 {
10031 segT sec;
10032 segT last_sec = 0;
10033 int old_count = 0;
10034 int new_count = 0;
10035
10036 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
10037 {
10038 last_sec = sec;
10039 old_count++;
10040 }
10041
10042 /* Now that we have the last section, push all the literal
10043 sections to the end. */
10044 xtensa_reorder_seg_list (literal_head, last_sec);
10045
10046 /* Now perform the final error check. */
10047 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
10048 new_count++;
10049 assert (new_count == old_count);
10050 }
10051
10052
10053 /* Change the emit state (seg, subseg, and frag related stuff) to the
10054 correct location. Return a emit_state which can be passed to
10055 xtensa_restore_emit_state to return to current fragment. */
10056
10057 static void
10058 xtensa_switch_to_literal_fragment (emit_state *result)
10059 {
10060 if (directive_state[directive_absolute_literals])
10061 {
10062 segT lit4_seg = cache_literal_section (TRUE);
10063 xtensa_switch_section_emit_state (result, lit4_seg, 0);
10064 }
10065 else
10066 xtensa_switch_to_non_abs_literal_fragment (result);
10067
10068 /* Do a 4-byte align here. */
10069 frag_align (2, 0, 0);
10070 record_alignment (now_seg, 2);
10071 }
10072
10073
10074 static void
10075 xtensa_switch_to_non_abs_literal_fragment (emit_state *result)
10076 {
10077 static bfd_boolean recursive = FALSE;
10078 fragS *pool_location = get_literal_pool_location (now_seg);
10079 segT lit_seg;
10080 bfd_boolean is_init =
10081 (now_seg && !strcmp (segment_name (now_seg), INIT_SECTION_NAME));
10082 bfd_boolean is_fini =
10083 (now_seg && !strcmp (segment_name (now_seg), FINI_SECTION_NAME));
10084
10085 if (pool_location == NULL
10086 && !use_literal_section
10087 && !recursive
10088 && !is_init && ! is_fini)
10089 {
10090 as_bad (_("literal pool location required for text-section-literals; specify with .literal_position"));
10091
10092 /* When we mark a literal pool location, we want to put a frag in
10093 the literal pool that points to it. But to do that, we want to
10094 switch_to_literal_fragment. But literal sections don't have
10095 literal pools, so their location is always null, so we would
10096 recurse forever. This is kind of hacky, but it works. */
10097
10098 recursive = TRUE;
10099 xtensa_mark_literal_pool_location ();
10100 recursive = FALSE;
10101 }
10102
10103 lit_seg = cache_literal_section (FALSE);
10104 xtensa_switch_section_emit_state (result, lit_seg, 0);
10105
10106 if (!use_literal_section
10107 && !is_init && !is_fini
10108 && get_literal_pool_location (now_seg) != pool_location)
10109 {
10110 /* Close whatever frag is there. */
10111 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
10112 xtensa_set_frag_assembly_state (frag_now);
10113 frag_now->tc_frag_data.literal_frag = pool_location;
10114 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
10115 xtensa_set_frag_assembly_state (frag_now);
10116 }
10117 }
10118
10119
10120 /* Call this function before emitting data into the literal section.
10121 This is a helper function for xtensa_switch_to_literal_fragment.
10122 This is similar to a .section new_now_seg subseg. */
10123
10124 static void
10125 xtensa_switch_section_emit_state (emit_state *state,
10126 segT new_now_seg,
10127 subsegT new_now_subseg)
10128 {
10129 state->name = now_seg->name;
10130 state->now_seg = now_seg;
10131 state->now_subseg = now_subseg;
10132 state->generating_literals = generating_literals;
10133 generating_literals++;
10134 subseg_set (new_now_seg, new_now_subseg);
10135 }
10136
10137
10138 /* Use to restore the emitting into the normal place. */
10139
10140 static void
10141 xtensa_restore_emit_state (emit_state *state)
10142 {
10143 generating_literals = state->generating_literals;
10144 subseg_set (state->now_seg, state->now_subseg);
10145 }
10146
10147
10148 /* Predicate function used to look up a section in a particular group. */
10149
10150 static bfd_boolean
10151 match_section_group (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
10152 {
10153 const char *gname = inf;
10154 const char *group_name = elf_group_name (sec);
10155
10156 return (group_name == gname
10157 || (group_name != NULL
10158 && gname != NULL
10159 && strcmp (group_name, gname) == 0));
10160 }
10161
10162
10163 /* Get the literal section to be used for the current text section.
10164 The result may be cached in the default_lit_sections structure. */
10165
10166 static segT
10167 cache_literal_section (bfd_boolean use_abs_literals)
10168 {
10169 const char *text_name, *group_name = 0;
10170 char *base_name, *name, *suffix;
10171 segT *pcached;
10172 segT seg, current_section;
10173 int current_subsec;
10174 bfd_boolean linkonce = FALSE;
10175
10176 /* Save the current section/subsection. */
10177 current_section = now_seg;
10178 current_subsec = now_subseg;
10179
10180 /* Clear the cached values if they are no longer valid. */
10181 if (now_seg != default_lit_sections.current_text_seg)
10182 {
10183 default_lit_sections.current_text_seg = now_seg;
10184 default_lit_sections.lit_seg = NULL;
10185 default_lit_sections.lit4_seg = NULL;
10186 }
10187
10188 /* Check if the literal section is already cached. */
10189 if (use_abs_literals)
10190 pcached = &default_lit_sections.lit4_seg;
10191 else
10192 pcached = &default_lit_sections.lit_seg;
10193
10194 if (*pcached)
10195 return *pcached;
10196
10197 text_name = default_lit_sections.lit_prefix;
10198 if (! text_name || ! *text_name)
10199 {
10200 text_name = segment_name (current_section);
10201 group_name = elf_group_name (current_section);
10202 linkonce = (current_section->flags & SEC_LINK_ONCE) != 0;
10203 }
10204
10205 base_name = use_abs_literals ? ".lit4" : ".literal";
10206 if (group_name)
10207 {
10208 name = xmalloc (strlen (base_name) + strlen (group_name) + 2);
10209 sprintf (name, "%s.%s", base_name, group_name);
10210 }
10211 else if (strncmp (text_name, ".gnu.linkonce.", linkonce_len) == 0)
10212 {
10213 suffix = strchr (text_name + linkonce_len, '.');
10214
10215 name = xmalloc (linkonce_len + strlen (base_name) + 1
10216 + (suffix ? strlen (suffix) : 0));
10217 strcpy (name, ".gnu.linkonce");
10218 strcat (name, base_name);
10219 if (suffix)
10220 strcat (name, suffix);
10221 linkonce = TRUE;
10222 }
10223 else
10224 {
10225 /* If the section name ends with ".text", then replace that suffix
10226 instead of appending an additional suffix. */
10227 size_t len = strlen (text_name);
10228 if (len >= 5 && strcmp (text_name + len - 5, ".text") == 0)
10229 len -= 5;
10230
10231 name = xmalloc (len + strlen (base_name) + 1);
10232 strcpy (name, text_name);
10233 strcpy (name + len, base_name);
10234 }
10235
10236 /* Canonicalize section names to allow renaming literal sections.
10237 The group name, if any, came from the current text section and
10238 has already been canonicalized. */
10239 name = tc_canonicalize_symbol_name (name);
10240
10241 seg = bfd_get_section_by_name_if (stdoutput, name, match_section_group,
10242 (void *) group_name);
10243 if (! seg)
10244 {
10245 flagword flags;
10246
10247 seg = subseg_force_new (name, 0);
10248
10249 if (! use_abs_literals)
10250 {
10251 /* Add the newly created literal segment to the list. */
10252 seg_list *n = (seg_list *) xmalloc (sizeof (seg_list));
10253 n->seg = seg;
10254 n->next = literal_head->next;
10255 literal_head->next = n;
10256 }
10257
10258 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_ALLOC | SEC_LOAD
10259 | (linkonce ? (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD) : 0)
10260 | (use_abs_literals ? SEC_DATA : SEC_CODE));
10261
10262 elf_group_name (seg) = group_name;
10263
10264 bfd_set_section_flags (stdoutput, seg, flags);
10265 bfd_set_section_alignment (stdoutput, seg, 2);
10266 }
10267
10268 *pcached = seg;
10269 subseg_set (current_section, current_subsec);
10270 return seg;
10271 }
10272
10273 \f
10274 /* Property Tables Stuff. */
10275
10276 #define XTENSA_INSN_SEC_NAME ".xt.insn"
10277 #define XTENSA_LIT_SEC_NAME ".xt.lit"
10278 #define XTENSA_PROP_SEC_NAME ".xt.prop"
10279
10280 typedef bfd_boolean (*frag_predicate) (const fragS *);
10281 typedef void (*frag_flags_fn) (const fragS *, frag_flags *);
10282
10283 static bfd_boolean get_frag_is_literal (const fragS *);
10284 static void xtensa_create_property_segments
10285 (frag_predicate, frag_predicate, const char *, xt_section_type);
10286 static void xtensa_create_xproperty_segments
10287 (frag_flags_fn, const char *, xt_section_type);
10288 static bfd_boolean section_has_property (segT, frag_predicate);
10289 static bfd_boolean section_has_xproperty (segT, frag_flags_fn);
10290 static void add_xt_block_frags
10291 (segT, xtensa_block_info **, frag_predicate, frag_predicate);
10292 static bfd_boolean xtensa_frag_flags_is_empty (const frag_flags *);
10293 static void xtensa_frag_flags_init (frag_flags *);
10294 static void get_frag_property_flags (const fragS *, frag_flags *);
10295 static bfd_vma frag_flags_to_number (const frag_flags *);
10296 static void add_xt_prop_frags (segT, xtensa_block_info **, frag_flags_fn);
10297
10298 /* Set up property tables after relaxation. */
10299
10300 void
10301 xtensa_post_relax_hook (void)
10302 {
10303 xtensa_move_seg_list_to_beginning (literal_head);
10304
10305 xtensa_find_unmarked_state_frags ();
10306 xtensa_mark_frags_for_org ();
10307 xtensa_mark_difference_of_two_symbols ();
10308
10309 xtensa_create_property_segments (get_frag_is_literal,
10310 NULL,
10311 XTENSA_LIT_SEC_NAME,
10312 xt_literal_sec);
10313 xtensa_create_xproperty_segments (get_frag_property_flags,
10314 XTENSA_PROP_SEC_NAME,
10315 xt_prop_sec);
10316
10317 if (warn_unaligned_branch_targets)
10318 bfd_map_over_sections (stdoutput, xtensa_find_unaligned_branch_targets, 0);
10319 bfd_map_over_sections (stdoutput, xtensa_find_unaligned_loops, 0);
10320 }
10321
10322
10323 /* This function is only meaningful after xtensa_move_literals. */
10324
10325 static bfd_boolean
10326 get_frag_is_literal (const fragS *fragP)
10327 {
10328 assert (fragP != NULL);
10329 return fragP->tc_frag_data.is_literal;
10330 }
10331
10332
10333 static void
10334 xtensa_create_property_segments (frag_predicate property_function,
10335 frag_predicate end_property_function,
10336 const char *section_name_base,
10337 xt_section_type sec_type)
10338 {
10339 segT *seclist;
10340
10341 /* Walk over all of the current segments.
10342 Walk over each fragment
10343 For each non-empty fragment,
10344 Build a property record (append where possible). */
10345
10346 for (seclist = &stdoutput->sections;
10347 seclist && *seclist;
10348 seclist = &(*seclist)->next)
10349 {
10350 segT sec = *seclist;
10351 flagword flags;
10352
10353 flags = bfd_get_section_flags (stdoutput, sec);
10354 if (flags & SEC_DEBUGGING)
10355 continue;
10356 if (!(flags & SEC_ALLOC))
10357 continue;
10358
10359 if (section_has_property (sec, property_function))
10360 {
10361 segment_info_type *xt_seg_info;
10362 xtensa_block_info **xt_blocks;
10363 segT prop_sec = xtensa_get_property_section (sec, section_name_base);
10364
10365 prop_sec->output_section = prop_sec;
10366 subseg_set (prop_sec, 0);
10367 xt_seg_info = seg_info (prop_sec);
10368 xt_blocks = &xt_seg_info->tc_segment_info_data.blocks[sec_type];
10369
10370 /* Walk over all of the frchains here and add new sections. */
10371 add_xt_block_frags (sec, xt_blocks, property_function,
10372 end_property_function);
10373 }
10374 }
10375
10376 /* Now we fill them out.... */
10377
10378 for (seclist = &stdoutput->sections;
10379 seclist && *seclist;
10380 seclist = &(*seclist)->next)
10381 {
10382 segment_info_type *seginfo;
10383 xtensa_block_info *block;
10384 segT sec = *seclist;
10385
10386 seginfo = seg_info (sec);
10387 block = seginfo->tc_segment_info_data.blocks[sec_type];
10388
10389 if (block)
10390 {
10391 xtensa_block_info *cur_block;
10392 int num_recs = 0;
10393 bfd_size_type rec_size;
10394
10395 for (cur_block = block; cur_block; cur_block = cur_block->next)
10396 num_recs++;
10397
10398 rec_size = num_recs * 8;
10399 bfd_set_section_size (stdoutput, sec, rec_size);
10400
10401 if (num_recs)
10402 {
10403 char *frag_data;
10404 int i;
10405
10406 subseg_set (sec, 0);
10407 frag_data = frag_more (rec_size);
10408 cur_block = block;
10409 for (i = 0; i < num_recs; i++)
10410 {
10411 fixS *fix;
10412
10413 /* Write the fixup. */
10414 assert (cur_block);
10415 fix = fix_new (frag_now, i * 8, 4,
10416 section_symbol (cur_block->sec),
10417 cur_block->offset,
10418 FALSE, BFD_RELOC_32);
10419 fix->fx_file = "<internal>";
10420 fix->fx_line = 0;
10421
10422 /* Write the length. */
10423 md_number_to_chars (&frag_data[4 + i * 8],
10424 cur_block->size, 4);
10425 cur_block = cur_block->next;
10426 }
10427 frag_wane (frag_now);
10428 frag_new (0);
10429 frag_wane (frag_now);
10430 }
10431 }
10432 }
10433 }
10434
10435
10436 static void
10437 xtensa_create_xproperty_segments (frag_flags_fn flag_fn,
10438 const char *section_name_base,
10439 xt_section_type sec_type)
10440 {
10441 segT *seclist;
10442
10443 /* Walk over all of the current segments.
10444 Walk over each fragment.
10445 For each fragment that has instructions,
10446 build an instruction record (append where possible). */
10447
10448 for (seclist = &stdoutput->sections;
10449 seclist && *seclist;
10450 seclist = &(*seclist)->next)
10451 {
10452 segT sec = *seclist;
10453 flagword flags;
10454
10455 flags = bfd_get_section_flags (stdoutput, sec);
10456 if ((flags & SEC_DEBUGGING)
10457 || !(flags & SEC_ALLOC)
10458 || (flags & SEC_MERGE))
10459 continue;
10460
10461 if (section_has_xproperty (sec, flag_fn))
10462 {
10463 segment_info_type *xt_seg_info;
10464 xtensa_block_info **xt_blocks;
10465 segT prop_sec = xtensa_get_property_section (sec, section_name_base);
10466
10467 prop_sec->output_section = prop_sec;
10468 subseg_set (prop_sec, 0);
10469 xt_seg_info = seg_info (prop_sec);
10470 xt_blocks = &xt_seg_info->tc_segment_info_data.blocks[sec_type];
10471
10472 /* Walk over all of the frchains here and add new sections. */
10473 add_xt_prop_frags (sec, xt_blocks, flag_fn);
10474 }
10475 }
10476
10477 /* Now we fill them out.... */
10478
10479 for (seclist = &stdoutput->sections;
10480 seclist && *seclist;
10481 seclist = &(*seclist)->next)
10482 {
10483 segment_info_type *seginfo;
10484 xtensa_block_info *block;
10485 segT sec = *seclist;
10486
10487 seginfo = seg_info (sec);
10488 block = seginfo->tc_segment_info_data.blocks[sec_type];
10489
10490 if (block)
10491 {
10492 xtensa_block_info *cur_block;
10493 int num_recs = 0;
10494 bfd_size_type rec_size;
10495
10496 for (cur_block = block; cur_block; cur_block = cur_block->next)
10497 num_recs++;
10498
10499 rec_size = num_recs * (8 + 4);
10500 bfd_set_section_size (stdoutput, sec, rec_size);
10501 /* elf_section_data (sec)->this_hdr.sh_entsize = 12; */
10502
10503 if (num_recs)
10504 {
10505 char *frag_data;
10506 int i;
10507
10508 subseg_set (sec, 0);
10509 frag_data = frag_more (rec_size);
10510 cur_block = block;
10511 for (i = 0; i < num_recs; i++)
10512 {
10513 fixS *fix;
10514
10515 /* Write the fixup. */
10516 assert (cur_block);
10517 fix = fix_new (frag_now, i * 12, 4,
10518 section_symbol (cur_block->sec),
10519 cur_block->offset,
10520 FALSE, BFD_RELOC_32);
10521 fix->fx_file = "<internal>";
10522 fix->fx_line = 0;
10523
10524 /* Write the length. */
10525 md_number_to_chars (&frag_data[4 + i * 12],
10526 cur_block->size, 4);
10527 md_number_to_chars (&frag_data[8 + i * 12],
10528 frag_flags_to_number (&cur_block->flags),
10529 4);
10530 cur_block = cur_block->next;
10531 }
10532 frag_wane (frag_now);
10533 frag_new (0);
10534 frag_wane (frag_now);
10535 }
10536 }
10537 }
10538 }
10539
10540
10541 static bfd_boolean
10542 section_has_property (segT sec, frag_predicate property_function)
10543 {
10544 segment_info_type *seginfo = seg_info (sec);
10545 fragS *fragP;
10546
10547 if (seginfo && seginfo->frchainP)
10548 {
10549 for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
10550 {
10551 if (property_function (fragP)
10552 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
10553 return TRUE;
10554 }
10555 }
10556 return FALSE;
10557 }
10558
10559
10560 static bfd_boolean
10561 section_has_xproperty (segT sec, frag_flags_fn property_function)
10562 {
10563 segment_info_type *seginfo = seg_info (sec);
10564 fragS *fragP;
10565
10566 if (seginfo && seginfo->frchainP)
10567 {
10568 for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
10569 {
10570 frag_flags prop_flags;
10571 property_function (fragP, &prop_flags);
10572 if (!xtensa_frag_flags_is_empty (&prop_flags))
10573 return TRUE;
10574 }
10575 }
10576 return FALSE;
10577 }
10578
10579
10580 /* Two types of block sections exist right now: literal and insns. */
10581
10582 static void
10583 add_xt_block_frags (segT sec,
10584 xtensa_block_info **xt_block,
10585 frag_predicate property_function,
10586 frag_predicate end_property_function)
10587 {
10588 bfd_vma seg_offset;
10589 fragS *fragP;
10590
10591 /* Build it if needed. */
10592 while (*xt_block != NULL)
10593 xt_block = &(*xt_block)->next;
10594 /* We are either at NULL at the beginning or at the end. */
10595
10596 /* Walk through the frags. */
10597 seg_offset = 0;
10598
10599 if (seg_info (sec)->frchainP)
10600 {
10601 for (fragP = seg_info (sec)->frchainP->frch_root;
10602 fragP;
10603 fragP = fragP->fr_next)
10604 {
10605 if (property_function (fragP)
10606 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
10607 {
10608 if (*xt_block != NULL)
10609 {
10610 if ((*xt_block)->offset + (*xt_block)->size
10611 == fragP->fr_address)
10612 (*xt_block)->size += fragP->fr_fix;
10613 else
10614 xt_block = &((*xt_block)->next);
10615 }
10616 if (*xt_block == NULL)
10617 {
10618 xtensa_block_info *new_block = (xtensa_block_info *)
10619 xmalloc (sizeof (xtensa_block_info));
10620 new_block->sec = sec;
10621 new_block->offset = fragP->fr_address;
10622 new_block->size = fragP->fr_fix;
10623 new_block->next = NULL;
10624 xtensa_frag_flags_init (&new_block->flags);
10625 *xt_block = new_block;
10626 }
10627 if (end_property_function
10628 && end_property_function (fragP))
10629 {
10630 xt_block = &((*xt_block)->next);
10631 }
10632 }
10633 }
10634 }
10635 }
10636
10637
10638 /* Break the encapsulation of add_xt_prop_frags here. */
10639
10640 static bfd_boolean
10641 xtensa_frag_flags_is_empty (const frag_flags *prop_flags)
10642 {
10643 if (prop_flags->is_literal
10644 || prop_flags->is_insn
10645 || prop_flags->is_data
10646 || prop_flags->is_unreachable)
10647 return FALSE;
10648 return TRUE;
10649 }
10650
10651
10652 static void
10653 xtensa_frag_flags_init (frag_flags *prop_flags)
10654 {
10655 memset (prop_flags, 0, sizeof (frag_flags));
10656 }
10657
10658
10659 static void
10660 get_frag_property_flags (const fragS *fragP, frag_flags *prop_flags)
10661 {
10662 xtensa_frag_flags_init (prop_flags);
10663 if (fragP->tc_frag_data.is_literal)
10664 prop_flags->is_literal = TRUE;
10665 if (fragP->tc_frag_data.is_specific_opcode
10666 || fragP->tc_frag_data.is_no_transform)
10667 prop_flags->is_no_transform = TRUE;
10668 if (fragP->tc_frag_data.is_unreachable)
10669 prop_flags->is_unreachable = TRUE;
10670 else if (fragP->tc_frag_data.is_insn)
10671 {
10672 prop_flags->is_insn = TRUE;
10673 if (fragP->tc_frag_data.is_loop_target)
10674 prop_flags->insn.is_loop_target = TRUE;
10675 if (fragP->tc_frag_data.is_branch_target)
10676 prop_flags->insn.is_branch_target = TRUE;
10677 if (fragP->tc_frag_data.is_no_density)
10678 prop_flags->insn.is_no_density = TRUE;
10679 if (fragP->tc_frag_data.use_absolute_literals)
10680 prop_flags->insn.is_abslit = TRUE;
10681 }
10682 if (fragP->tc_frag_data.is_align)
10683 {
10684 prop_flags->is_align = TRUE;
10685 prop_flags->alignment = fragP->tc_frag_data.alignment;
10686 if (xtensa_frag_flags_is_empty (prop_flags))
10687 prop_flags->is_data = TRUE;
10688 }
10689 }
10690
10691
10692 static bfd_vma
10693 frag_flags_to_number (const frag_flags *prop_flags)
10694 {
10695 bfd_vma num = 0;
10696 if (prop_flags->is_literal)
10697 num |= XTENSA_PROP_LITERAL;
10698 if (prop_flags->is_insn)
10699 num |= XTENSA_PROP_INSN;
10700 if (prop_flags->is_data)
10701 num |= XTENSA_PROP_DATA;
10702 if (prop_flags->is_unreachable)
10703 num |= XTENSA_PROP_UNREACHABLE;
10704 if (prop_flags->insn.is_loop_target)
10705 num |= XTENSA_PROP_INSN_LOOP_TARGET;
10706 if (prop_flags->insn.is_branch_target)
10707 {
10708 num |= XTENSA_PROP_INSN_BRANCH_TARGET;
10709 num = SET_XTENSA_PROP_BT_ALIGN (num, prop_flags->insn.bt_align_priority);
10710 }
10711
10712 if (prop_flags->insn.is_no_density)
10713 num |= XTENSA_PROP_INSN_NO_DENSITY;
10714 if (prop_flags->is_no_transform)
10715 num |= XTENSA_PROP_NO_TRANSFORM;
10716 if (prop_flags->insn.is_no_reorder)
10717 num |= XTENSA_PROP_INSN_NO_REORDER;
10718 if (prop_flags->insn.is_abslit)
10719 num |= XTENSA_PROP_INSN_ABSLIT;
10720
10721 if (prop_flags->is_align)
10722 {
10723 num |= XTENSA_PROP_ALIGN;
10724 num = SET_XTENSA_PROP_ALIGNMENT (num, prop_flags->alignment);
10725 }
10726
10727 return num;
10728 }
10729
10730
10731 static bfd_boolean
10732 xtensa_frag_flags_combinable (const frag_flags *prop_flags_1,
10733 const frag_flags *prop_flags_2)
10734 {
10735 /* Cannot combine with an end marker. */
10736
10737 if (prop_flags_1->is_literal != prop_flags_2->is_literal)
10738 return FALSE;
10739 if (prop_flags_1->is_insn != prop_flags_2->is_insn)
10740 return FALSE;
10741 if (prop_flags_1->is_data != prop_flags_2->is_data)
10742 return FALSE;
10743
10744 if (prop_flags_1->is_insn)
10745 {
10746 /* Properties of the beginning of the frag. */
10747 if (prop_flags_2->insn.is_loop_target)
10748 return FALSE;
10749 if (prop_flags_2->insn.is_branch_target)
10750 return FALSE;
10751 if (prop_flags_1->insn.is_no_density !=
10752 prop_flags_2->insn.is_no_density)
10753 return FALSE;
10754 if (prop_flags_1->is_no_transform !=
10755 prop_flags_2->is_no_transform)
10756 return FALSE;
10757 if (prop_flags_1->insn.is_no_reorder !=
10758 prop_flags_2->insn.is_no_reorder)
10759 return FALSE;
10760 if (prop_flags_1->insn.is_abslit !=
10761 prop_flags_2->insn.is_abslit)
10762 return FALSE;
10763 }
10764
10765 if (prop_flags_1->is_align)
10766 return FALSE;
10767
10768 return TRUE;
10769 }
10770
10771
10772 static bfd_vma
10773 xt_block_aligned_size (const xtensa_block_info *xt_block)
10774 {
10775 bfd_vma end_addr;
10776 unsigned align_bits;
10777
10778 if (!xt_block->flags.is_align)
10779 return xt_block->size;
10780
10781 end_addr = xt_block->offset + xt_block->size;
10782 align_bits = xt_block->flags.alignment;
10783 end_addr = ((end_addr + ((1 << align_bits) -1)) >> align_bits) << align_bits;
10784 return end_addr - xt_block->offset;
10785 }
10786
10787
10788 static bfd_boolean
10789 xtensa_xt_block_combine (xtensa_block_info *xt_block,
10790 const xtensa_block_info *xt_block_2)
10791 {
10792 if (xt_block->sec != xt_block_2->sec)
10793 return FALSE;
10794 if (xt_block->offset + xt_block_aligned_size (xt_block)
10795 != xt_block_2->offset)
10796 return FALSE;
10797
10798 if (xt_block_2->size == 0
10799 && (!xt_block_2->flags.is_unreachable
10800 || xt_block->flags.is_unreachable))
10801 {
10802 if (xt_block_2->flags.is_align
10803 && xt_block->flags.is_align)
10804 {
10805 /* Nothing needed. */
10806 if (xt_block->flags.alignment >= xt_block_2->flags.alignment)
10807 return TRUE;
10808 }
10809 else
10810 {
10811 if (xt_block_2->flags.is_align)
10812 {
10813 /* Push alignment to previous entry. */
10814 xt_block->flags.is_align = xt_block_2->flags.is_align;
10815 xt_block->flags.alignment = xt_block_2->flags.alignment;
10816 }
10817 return TRUE;
10818 }
10819 }
10820 if (!xtensa_frag_flags_combinable (&xt_block->flags,
10821 &xt_block_2->flags))
10822 return FALSE;
10823
10824 xt_block->size += xt_block_2->size;
10825
10826 if (xt_block_2->flags.is_align)
10827 {
10828 xt_block->flags.is_align = TRUE;
10829 xt_block->flags.alignment = xt_block_2->flags.alignment;
10830 }
10831
10832 return TRUE;
10833 }
10834
10835
10836 static void
10837 add_xt_prop_frags (segT sec,
10838 xtensa_block_info **xt_block,
10839 frag_flags_fn property_function)
10840 {
10841 bfd_vma seg_offset;
10842 fragS *fragP;
10843
10844 /* Build it if needed. */
10845 while (*xt_block != NULL)
10846 {
10847 xt_block = &(*xt_block)->next;
10848 }
10849 /* We are either at NULL at the beginning or at the end. */
10850
10851 /* Walk through the frags. */
10852 seg_offset = 0;
10853
10854 if (seg_info (sec)->frchainP)
10855 {
10856 for (fragP = seg_info (sec)->frchainP->frch_root; fragP;
10857 fragP = fragP->fr_next)
10858 {
10859 xtensa_block_info tmp_block;
10860 tmp_block.sec = sec;
10861 tmp_block.offset = fragP->fr_address;
10862 tmp_block.size = fragP->fr_fix;
10863 tmp_block.next = NULL;
10864 property_function (fragP, &tmp_block.flags);
10865
10866 if (!xtensa_frag_flags_is_empty (&tmp_block.flags))
10867 /* && fragP->fr_fix != 0) */
10868 {
10869 if ((*xt_block) == NULL
10870 || !xtensa_xt_block_combine (*xt_block, &tmp_block))
10871 {
10872 xtensa_block_info *new_block;
10873 if ((*xt_block) != NULL)
10874 xt_block = &(*xt_block)->next;
10875 new_block = (xtensa_block_info *)
10876 xmalloc (sizeof (xtensa_block_info));
10877 *new_block = tmp_block;
10878 *xt_block = new_block;
10879 }
10880 }
10881 }
10882 }
10883 }
10884
10885 \f
10886 /* op_placement_info_table */
10887
10888 /* op_placement_info makes it easier to determine which
10889 ops can go in which slots. */
10890
10891 static void
10892 init_op_placement_info_table (void)
10893 {
10894 xtensa_isa isa = xtensa_default_isa;
10895 xtensa_insnbuf ibuf = xtensa_insnbuf_alloc (isa);
10896 xtensa_opcode opcode;
10897 xtensa_format fmt;
10898 int slot;
10899 int num_opcodes = xtensa_isa_num_opcodes (isa);
10900
10901 op_placement_table = (op_placement_info_table)
10902 xmalloc (sizeof (op_placement_info) * num_opcodes);
10903 assert (xtensa_isa_num_formats (isa) < MAX_FORMATS);
10904
10905 for (opcode = 0; opcode < num_opcodes; opcode++)
10906 {
10907 op_placement_info *opi = &op_placement_table[opcode];
10908 /* FIXME: Make tinsn allocation dynamic. */
10909 if (xtensa_opcode_num_operands (isa, opcode) >= MAX_INSN_ARGS)
10910 as_fatal (_("too many operands in instruction"));
10911 opi->narrowest = XTENSA_UNDEFINED;
10912 opi->narrowest_size = 0x7F;
10913 opi->narrowest_slot = 0;
10914 opi->formats = 0;
10915 opi->num_formats = 0;
10916 opi->issuef = 0;
10917 for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
10918 {
10919 opi->slots[fmt] = 0;
10920 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
10921 {
10922 if (xtensa_opcode_encode (isa, fmt, slot, ibuf, opcode) == 0)
10923 {
10924 int fmt_length = xtensa_format_length (isa, fmt);
10925 opi->issuef++;
10926 set_bit (fmt, opi->formats);
10927 set_bit (slot, opi->slots[fmt]);
10928 if (fmt_length < opi->narrowest_size
10929 || (fmt_length == opi->narrowest_size
10930 && (xtensa_format_num_slots (isa, fmt)
10931 < xtensa_format_num_slots (isa,
10932 opi->narrowest))))
10933 {
10934 opi->narrowest = fmt;
10935 opi->narrowest_size = fmt_length;
10936 opi->narrowest_slot = slot;
10937 }
10938 }
10939 }
10940 if (opi->formats)
10941 opi->num_formats++;
10942 }
10943 }
10944 xtensa_insnbuf_free (isa, ibuf);
10945 }
10946
10947
10948 bfd_boolean
10949 opcode_fits_format_slot (xtensa_opcode opcode, xtensa_format fmt, int slot)
10950 {
10951 return bit_is_set (slot, op_placement_table[opcode].slots[fmt]);
10952 }
10953
10954
10955 /* If the opcode is available in a single slot format, return its size. */
10956
10957 static int
10958 xg_get_single_size (xtensa_opcode opcode)
10959 {
10960 return op_placement_table[opcode].narrowest_size;
10961 }
10962
10963
10964 static xtensa_format
10965 xg_get_single_format (xtensa_opcode opcode)
10966 {
10967 return op_placement_table[opcode].narrowest;
10968 }
10969
10970
10971 static int
10972 xg_get_single_slot (xtensa_opcode opcode)
10973 {
10974 return op_placement_table[opcode].narrowest_slot;
10975 }
10976
10977 \f
10978 /* Instruction Stack Functions (from "xtensa-istack.h"). */
10979
10980 void
10981 istack_init (IStack *stack)
10982 {
10983 memset (stack, 0, sizeof (IStack));
10984 stack->ninsn = 0;
10985 }
10986
10987
10988 bfd_boolean
10989 istack_empty (IStack *stack)
10990 {
10991 return (stack->ninsn == 0);
10992 }
10993
10994
10995 bfd_boolean
10996 istack_full (IStack *stack)
10997 {
10998 return (stack->ninsn == MAX_ISTACK);
10999 }
11000
11001
11002 /* Return a pointer to the top IStack entry.
11003 It is an error to call this if istack_empty () is TRUE. */
11004
11005 TInsn *
11006 istack_top (IStack *stack)
11007 {
11008 int rec = stack->ninsn - 1;
11009 assert (!istack_empty (stack));
11010 return &stack->insn[rec];
11011 }
11012
11013
11014 /* Add a new TInsn to an IStack.
11015 It is an error to call this if istack_full () is TRUE. */
11016
11017 void
11018 istack_push (IStack *stack, TInsn *insn)
11019 {
11020 int rec = stack->ninsn;
11021 assert (!istack_full (stack));
11022 stack->insn[rec] = *insn;
11023 stack->ninsn++;
11024 }
11025
11026
11027 /* Clear space for the next TInsn on the IStack and return a pointer
11028 to it. It is an error to call this if istack_full () is TRUE. */
11029
11030 TInsn *
11031 istack_push_space (IStack *stack)
11032 {
11033 int rec = stack->ninsn;
11034 TInsn *insn;
11035 assert (!istack_full (stack));
11036 insn = &stack->insn[rec];
11037 tinsn_init (insn);
11038 stack->ninsn++;
11039 return insn;
11040 }
11041
11042
11043 /* Remove the last pushed instruction. It is an error to call this if
11044 istack_empty () returns TRUE. */
11045
11046 void
11047 istack_pop (IStack *stack)
11048 {
11049 int rec = stack->ninsn - 1;
11050 assert (!istack_empty (stack));
11051 stack->ninsn--;
11052 tinsn_init (&stack->insn[rec]);
11053 }
11054
11055 \f
11056 /* TInsn functions. */
11057
11058 void
11059 tinsn_init (TInsn *dst)
11060 {
11061 memset (dst, 0, sizeof (TInsn));
11062 }
11063
11064
11065 /* Return TRUE if ANY of the operands in the insn are symbolic. */
11066
11067 static bfd_boolean
11068 tinsn_has_symbolic_operands (const TInsn *insn)
11069 {
11070 int i;
11071 int n = insn->ntok;
11072
11073 assert (insn->insn_type == ITYPE_INSN);
11074
11075 for (i = 0; i < n; ++i)
11076 {
11077 switch (insn->tok[i].X_op)
11078 {
11079 case O_register:
11080 case O_constant:
11081 break;
11082 default:
11083 return TRUE;
11084 }
11085 }
11086 return FALSE;
11087 }
11088
11089
11090 bfd_boolean
11091 tinsn_has_invalid_symbolic_operands (const TInsn *insn)
11092 {
11093 xtensa_isa isa = xtensa_default_isa;
11094 int i;
11095 int n = insn->ntok;
11096
11097 assert (insn->insn_type == ITYPE_INSN);
11098
11099 for (i = 0; i < n; ++i)
11100 {
11101 switch (insn->tok[i].X_op)
11102 {
11103 case O_register:
11104 case O_constant:
11105 break;
11106 case O_big:
11107 case O_illegal:
11108 case O_absent:
11109 /* Errors for these types are caught later. */
11110 break;
11111 case O_hi16:
11112 case O_lo16:
11113 default:
11114 /* Symbolic immediates are only allowed on the last immediate
11115 operand. At this time, CONST16 is the only opcode where we
11116 support non-PC-relative relocations. */
11117 if (i != get_relaxable_immed (insn->opcode)
11118 || (xtensa_operand_is_PCrelative (isa, insn->opcode, i) != 1
11119 && insn->opcode != xtensa_const16_opcode))
11120 {
11121 as_bad (_("invalid symbolic operand"));
11122 return TRUE;
11123 }
11124 }
11125 }
11126 return FALSE;
11127 }
11128
11129
11130 /* For assembly code with complex expressions (e.g. subtraction),
11131 we have to build them in the literal pool so that
11132 their results are calculated correctly after relaxation.
11133 The relaxation only handles expressions that
11134 boil down to SYMBOL + OFFSET. */
11135
11136 static bfd_boolean
11137 tinsn_has_complex_operands (const TInsn *insn)
11138 {
11139 int i;
11140 int n = insn->ntok;
11141 assert (insn->insn_type == ITYPE_INSN);
11142 for (i = 0; i < n; ++i)
11143 {
11144 switch (insn->tok[i].X_op)
11145 {
11146 case O_register:
11147 case O_constant:
11148 case O_symbol:
11149 case O_lo16:
11150 case O_hi16:
11151 break;
11152 default:
11153 return TRUE;
11154 }
11155 }
11156 return FALSE;
11157 }
11158
11159
11160 /* Encode a TInsn opcode and its constant operands into slotbuf.
11161 Return TRUE if there is a symbol in the immediate field. This
11162 function assumes that:
11163 1) The number of operands are correct.
11164 2) The insn_type is ITYPE_INSN.
11165 3) The opcode can be encoded in the specified format and slot.
11166 4) Operands are either O_constant or O_symbol, and all constants fit. */
11167
11168 static bfd_boolean
11169 tinsn_to_slotbuf (xtensa_format fmt,
11170 int slot,
11171 TInsn *tinsn,
11172 xtensa_insnbuf slotbuf)
11173 {
11174 xtensa_isa isa = xtensa_default_isa;
11175 xtensa_opcode opcode = tinsn->opcode;
11176 bfd_boolean has_fixup = FALSE;
11177 int noperands = xtensa_opcode_num_operands (isa, opcode);
11178 int i;
11179
11180 assert (tinsn->insn_type == ITYPE_INSN);
11181 if (noperands != tinsn->ntok)
11182 as_fatal (_("operand number mismatch"));
11183
11184 if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opcode))
11185 {
11186 as_bad (_("cannot encode opcode \"%s\" in the given format \"%s\""),
11187 xtensa_opcode_name (isa, opcode), xtensa_format_name (isa, fmt));
11188 return FALSE;
11189 }
11190
11191 for (i = 0; i < noperands; i++)
11192 {
11193 expressionS *expr = &tinsn->tok[i];
11194 int rc;
11195 unsigned line;
11196 char *file_name;
11197 uint32 opnd_value;
11198
11199 switch (expr->X_op)
11200 {
11201 case O_register:
11202 if (xtensa_operand_is_visible (isa, opcode, i) == 0)
11203 break;
11204 /* The register number has already been checked in
11205 expression_maybe_register, so we don't need to check here. */
11206 opnd_value = expr->X_add_number;
11207 (void) xtensa_operand_encode (isa, opcode, i, &opnd_value);
11208 rc = xtensa_operand_set_field (isa, opcode, i, fmt, slot, slotbuf,
11209 opnd_value);
11210 if (rc != 0)
11211 as_warn (_("xtensa-isa failure: %s"), xtensa_isa_error_msg (isa));
11212 break;
11213
11214 case O_constant:
11215 if (xtensa_operand_is_visible (isa, opcode, i) == 0)
11216 break;
11217 as_where (&file_name, &line);
11218 /* It is a constant and we called this function
11219 then we have to try to fit it. */
11220 xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode, i,
11221 expr->X_add_number, file_name, line);
11222 break;
11223
11224 default:
11225 has_fixup = TRUE;
11226 break;
11227 }
11228 }
11229
11230 return has_fixup;
11231 }
11232
11233
11234 /* Encode a single TInsn into an insnbuf. If the opcode can only be encoded
11235 into a multi-slot instruction, fill the other slots with NOPs.
11236 Return TRUE if there is a symbol in the immediate field. See also the
11237 assumptions listed for tinsn_to_slotbuf. */
11238
11239 static bfd_boolean
11240 tinsn_to_insnbuf (TInsn *tinsn, xtensa_insnbuf insnbuf)
11241 {
11242 static xtensa_insnbuf slotbuf = 0;
11243 static vliw_insn vinsn;
11244 xtensa_isa isa = xtensa_default_isa;
11245 bfd_boolean has_fixup = FALSE;
11246 int i;
11247
11248 if (!slotbuf)
11249 {
11250 slotbuf = xtensa_insnbuf_alloc (isa);
11251 xg_init_vinsn (&vinsn);
11252 }
11253
11254 xg_clear_vinsn (&vinsn);
11255
11256 bundle_tinsn (tinsn, &vinsn);
11257
11258 xtensa_format_encode (isa, vinsn.format, insnbuf);
11259
11260 for (i = 0; i < vinsn.num_slots; i++)
11261 {
11262 /* Only one slot may have a fix-up because the rest contains NOPs. */
11263 has_fixup |=
11264 tinsn_to_slotbuf (vinsn.format, i, &vinsn.slots[i], vinsn.slotbuf[i]);
11265 xtensa_format_set_slot (isa, vinsn.format, i, insnbuf, vinsn.slotbuf[i]);
11266 }
11267
11268 return has_fixup;
11269 }
11270
11271
11272 /* Check the instruction arguments. Return TRUE on failure. */
11273
11274 static bfd_boolean
11275 tinsn_check_arguments (const TInsn *insn)
11276 {
11277 xtensa_isa isa = xtensa_default_isa;
11278 xtensa_opcode opcode = insn->opcode;
11279
11280 if (opcode == XTENSA_UNDEFINED)
11281 {
11282 as_bad (_("invalid opcode"));
11283 return TRUE;
11284 }
11285
11286 if (xtensa_opcode_num_operands (isa, opcode) > insn->ntok)
11287 {
11288 as_bad (_("too few operands"));
11289 return TRUE;
11290 }
11291
11292 if (xtensa_opcode_num_operands (isa, opcode) < insn->ntok)
11293 {
11294 as_bad (_("too many operands"));
11295 return TRUE;
11296 }
11297 return FALSE;
11298 }
11299
11300
11301 /* Load an instruction from its encoded form. */
11302
11303 static void
11304 tinsn_from_chars (TInsn *tinsn, char *f, int slot)
11305 {
11306 vliw_insn vinsn;
11307
11308 xg_init_vinsn (&vinsn);
11309 vinsn_from_chars (&vinsn, f);
11310
11311 *tinsn = vinsn.slots[slot];
11312 xg_free_vinsn (&vinsn);
11313 }
11314
11315
11316 static void
11317 tinsn_from_insnbuf (TInsn *tinsn,
11318 xtensa_insnbuf slotbuf,
11319 xtensa_format fmt,
11320 int slot)
11321 {
11322 int i;
11323 xtensa_isa isa = xtensa_default_isa;
11324
11325 /* Find the immed. */
11326 tinsn_init (tinsn);
11327 tinsn->insn_type = ITYPE_INSN;
11328 tinsn->is_specific_opcode = FALSE; /* must not be specific */
11329 tinsn->opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
11330 tinsn->ntok = xtensa_opcode_num_operands (isa, tinsn->opcode);
11331 for (i = 0; i < tinsn->ntok; i++)
11332 {
11333 set_expr_const (&tinsn->tok[i],
11334 xtensa_insnbuf_get_operand (slotbuf, fmt, slot,
11335 tinsn->opcode, i));
11336 }
11337 }
11338
11339
11340 /* Read the value of the relaxable immed from the fr_symbol and fr_offset. */
11341
11342 static void
11343 tinsn_immed_from_frag (TInsn *tinsn, fragS *fragP, int slot)
11344 {
11345 xtensa_opcode opcode = tinsn->opcode;
11346 int opnum;
11347
11348 if (fragP->tc_frag_data.slot_symbols[slot])
11349 {
11350 opnum = get_relaxable_immed (opcode);
11351 assert (opnum >= 0);
11352 set_expr_symbol_offset (&tinsn->tok[opnum],
11353 fragP->tc_frag_data.slot_symbols[slot],
11354 fragP->tc_frag_data.slot_offsets[slot]);
11355 }
11356 }
11357
11358
11359 static int
11360 get_num_stack_text_bytes (IStack *istack)
11361 {
11362 int i;
11363 int text_bytes = 0;
11364
11365 for (i = 0; i < istack->ninsn; i++)
11366 {
11367 TInsn *tinsn = &istack->insn[i];
11368 if (tinsn->insn_type == ITYPE_INSN)
11369 text_bytes += xg_get_single_size (tinsn->opcode);
11370 }
11371 return text_bytes;
11372 }
11373
11374
11375 static int
11376 get_num_stack_literal_bytes (IStack *istack)
11377 {
11378 int i;
11379 int lit_bytes = 0;
11380
11381 for (i = 0; i < istack->ninsn; i++)
11382 {
11383 TInsn *tinsn = &istack->insn[i];
11384 if (tinsn->insn_type == ITYPE_LITERAL && tinsn->ntok == 1)
11385 lit_bytes += 4;
11386 }
11387 return lit_bytes;
11388 }
11389
11390 \f
11391 /* vliw_insn functions. */
11392
11393 static void
11394 xg_init_vinsn (vliw_insn *v)
11395 {
11396 int i;
11397 xtensa_isa isa = xtensa_default_isa;
11398
11399 xg_clear_vinsn (v);
11400
11401 v->insnbuf = xtensa_insnbuf_alloc (isa);
11402 if (v->insnbuf == NULL)
11403 as_fatal (_("out of memory"));
11404
11405 for (i = 0; i < MAX_SLOTS; i++)
11406 {
11407 v->slotbuf[i] = xtensa_insnbuf_alloc (isa);
11408 if (v->slotbuf[i] == NULL)
11409 as_fatal (_("out of memory"));
11410 }
11411 }
11412
11413
11414 static void
11415 xg_clear_vinsn (vliw_insn *v)
11416 {
11417 int i;
11418
11419 memset (v, 0, offsetof (vliw_insn, insnbuf));
11420
11421 v->format = XTENSA_UNDEFINED;
11422 v->num_slots = 0;
11423 v->inside_bundle = FALSE;
11424
11425 if (xt_saved_debug_type != DEBUG_NONE)
11426 debug_type = xt_saved_debug_type;
11427
11428 for (i = 0; i < MAX_SLOTS; i++)
11429 v->slots[i].opcode = XTENSA_UNDEFINED;
11430 }
11431
11432
11433 static bfd_boolean
11434 vinsn_has_specific_opcodes (vliw_insn *v)
11435 {
11436 int i;
11437
11438 for (i = 0; i < v->num_slots; i++)
11439 {
11440 if (v->slots[i].is_specific_opcode)
11441 return TRUE;
11442 }
11443 return FALSE;
11444 }
11445
11446
11447 static void
11448 xg_free_vinsn (vliw_insn *v)
11449 {
11450 int i;
11451 xtensa_insnbuf_free (xtensa_default_isa, v->insnbuf);
11452 for (i = 0; i < MAX_SLOTS; i++)
11453 xtensa_insnbuf_free (xtensa_default_isa, v->slotbuf[i]);
11454 }
11455
11456
11457 /* Encode a vliw_insn into an insnbuf. Return TRUE if there are any symbolic
11458 operands. See also the assumptions listed for tinsn_to_slotbuf. */
11459
11460 static bfd_boolean
11461 vinsn_to_insnbuf (vliw_insn *vinsn,
11462 char *frag_offset,
11463 fragS *fragP,
11464 bfd_boolean record_fixup)
11465 {
11466 xtensa_isa isa = xtensa_default_isa;
11467 xtensa_format fmt = vinsn->format;
11468 xtensa_insnbuf insnbuf = vinsn->insnbuf;
11469 int slot;
11470 bfd_boolean has_fixup = FALSE;
11471
11472 xtensa_format_encode (isa, fmt, insnbuf);
11473
11474 for (slot = 0; slot < vinsn->num_slots; slot++)
11475 {
11476 TInsn *tinsn = &vinsn->slots[slot];
11477 bfd_boolean tinsn_has_fixup =
11478 tinsn_to_slotbuf (vinsn->format, slot, tinsn,
11479 vinsn->slotbuf[slot]);
11480
11481 xtensa_format_set_slot (isa, fmt, slot,
11482 insnbuf, vinsn->slotbuf[slot]);
11483 if (tinsn_has_fixup)
11484 {
11485 int i;
11486 xtensa_opcode opcode = tinsn->opcode;
11487 int noperands = xtensa_opcode_num_operands (isa, opcode);
11488 has_fixup = TRUE;
11489
11490 for (i = 0; i < noperands; i++)
11491 {
11492 expressionS* expr = &tinsn->tok[i];
11493 switch (expr->X_op)
11494 {
11495 case O_symbol:
11496 case O_lo16:
11497 case O_hi16:
11498 if (get_relaxable_immed (opcode) == i)
11499 {
11500 /* Add a fix record for the instruction, except if this
11501 function is being called prior to relaxation, i.e.,
11502 if record_fixup is false, and the instruction might
11503 be relaxed later. */
11504 if (record_fixup
11505 || tinsn->is_specific_opcode
11506 || !xg_is_relaxable_insn (tinsn, 0))
11507 {
11508 xg_add_opcode_fix (tinsn, i, fmt, slot, expr, fragP,
11509 frag_offset - fragP->fr_literal);
11510 }
11511 else
11512 {
11513 if (expr->X_op != O_symbol)
11514 as_bad (_("invalid operand"));
11515 tinsn->symbol = expr->X_add_symbol;
11516 tinsn->offset = expr->X_add_number;
11517 }
11518 }
11519 else
11520 as_bad (_("symbolic operand not allowed"));
11521 break;
11522
11523 case O_constant:
11524 case O_register:
11525 break;
11526
11527 default:
11528 as_bad (_("expression too complex"));
11529 break;
11530 }
11531 }
11532 }
11533 }
11534
11535 return has_fixup;
11536 }
11537
11538
11539 static void
11540 vinsn_from_chars (vliw_insn *vinsn, char *f)
11541 {
11542 static xtensa_insnbuf insnbuf = NULL;
11543 static xtensa_insnbuf slotbuf = NULL;
11544 int i;
11545 xtensa_format fmt;
11546 xtensa_isa isa = xtensa_default_isa;
11547
11548 if (!insnbuf)
11549 {
11550 insnbuf = xtensa_insnbuf_alloc (isa);
11551 slotbuf = xtensa_insnbuf_alloc (isa);
11552 }
11553
11554 xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) f, 0);
11555 fmt = xtensa_format_decode (isa, insnbuf);
11556 if (fmt == XTENSA_UNDEFINED)
11557 as_fatal (_("cannot decode instruction format"));
11558 vinsn->format = fmt;
11559 vinsn->num_slots = xtensa_format_num_slots (isa, fmt);
11560
11561 for (i = 0; i < vinsn->num_slots; i++)
11562 {
11563 TInsn *tinsn = &vinsn->slots[i];
11564 xtensa_format_get_slot (isa, fmt, i, insnbuf, slotbuf);
11565 tinsn_from_insnbuf (tinsn, slotbuf, fmt, i);
11566 }
11567 }
11568
11569 \f
11570 /* Expression utilities. */
11571
11572 /* Return TRUE if the expression is an integer constant. */
11573
11574 bfd_boolean
11575 expr_is_const (const expressionS *s)
11576 {
11577 return (s->X_op == O_constant);
11578 }
11579
11580
11581 /* Get the expression constant.
11582 Calling this is illegal if expr_is_const () returns TRUE. */
11583
11584 offsetT
11585 get_expr_const (const expressionS *s)
11586 {
11587 assert (expr_is_const (s));
11588 return s->X_add_number;
11589 }
11590
11591
11592 /* Set the expression to a constant value. */
11593
11594 void
11595 set_expr_const (expressionS *s, offsetT val)
11596 {
11597 s->X_op = O_constant;
11598 s->X_add_number = val;
11599 s->X_add_symbol = NULL;
11600 s->X_op_symbol = NULL;
11601 }
11602
11603
11604 bfd_boolean
11605 expr_is_register (const expressionS *s)
11606 {
11607 return (s->X_op == O_register);
11608 }
11609
11610
11611 /* Get the expression constant.
11612 Calling this is illegal if expr_is_const () returns TRUE. */
11613
11614 offsetT
11615 get_expr_register (const expressionS *s)
11616 {
11617 assert (expr_is_register (s));
11618 return s->X_add_number;
11619 }
11620
11621
11622 /* Set the expression to a symbol + constant offset. */
11623
11624 void
11625 set_expr_symbol_offset (expressionS *s, symbolS *sym, offsetT offset)
11626 {
11627 s->X_op = O_symbol;
11628 s->X_add_symbol = sym;
11629 s->X_op_symbol = NULL; /* unused */
11630 s->X_add_number = offset;
11631 }
11632
11633
11634 /* Return TRUE if the two expressions are equal. */
11635
11636 bfd_boolean
11637 expr_is_equal (expressionS *s1, expressionS *s2)
11638 {
11639 if (s1->X_op != s2->X_op)
11640 return FALSE;
11641 if (s1->X_add_symbol != s2->X_add_symbol)
11642 return FALSE;
11643 if (s1->X_op_symbol != s2->X_op_symbol)
11644 return FALSE;
11645 if (s1->X_add_number != s2->X_add_number)
11646 return FALSE;
11647 return TRUE;
11648 }
11649
11650
11651 static void
11652 copy_expr (expressionS *dst, const expressionS *src)
11653 {
11654 memcpy (dst, src, sizeof (expressionS));
11655 }
11656
11657 \f
11658 /* Support for the "--rename-section" option. */
11659
11660 struct rename_section_struct
11661 {
11662 char *old_name;
11663 char *new_name;
11664 struct rename_section_struct *next;
11665 };
11666
11667 static struct rename_section_struct *section_rename;
11668
11669
11670 /* Parse the string "oldname=new_name(:oldname2=new_name2)*" and add
11671 entries to the section_rename list. Note: Specifying multiple
11672 renamings separated by colons is not documented and is retained only
11673 for backward compatibility. */
11674
11675 static void
11676 build_section_rename (const char *arg)
11677 {
11678 struct rename_section_struct *r;
11679 char *this_arg = NULL;
11680 char *next_arg = NULL;
11681
11682 for (this_arg = xstrdup (arg); this_arg != NULL; this_arg = next_arg)
11683 {
11684 char *old_name, *new_name;
11685
11686 if (this_arg)
11687 {
11688 next_arg = strchr (this_arg, ':');
11689 if (next_arg)
11690 {
11691 *next_arg = '\0';
11692 next_arg++;
11693 }
11694 }
11695
11696 old_name = this_arg;
11697 new_name = strchr (this_arg, '=');
11698
11699 if (*old_name == '\0')
11700 {
11701 as_warn (_("ignoring extra '-rename-section' delimiter ':'"));
11702 continue;
11703 }
11704 if (!new_name || new_name[1] == '\0')
11705 {
11706 as_warn (_("ignoring invalid '-rename-section' specification: '%s'"),
11707 old_name);
11708 continue;
11709 }
11710 *new_name = '\0';
11711 new_name++;
11712
11713 /* Check for invalid section renaming. */
11714 for (r = section_rename; r != NULL; r = r->next)
11715 {
11716 if (strcmp (r->old_name, old_name) == 0)
11717 as_bad (_("section %s renamed multiple times"), old_name);
11718 if (strcmp (r->new_name, new_name) == 0)
11719 as_bad (_("multiple sections remapped to output section %s"),
11720 new_name);
11721 }
11722
11723 /* Now add it. */
11724 r = (struct rename_section_struct *)
11725 xmalloc (sizeof (struct rename_section_struct));
11726 r->old_name = xstrdup (old_name);
11727 r->new_name = xstrdup (new_name);
11728 r->next = section_rename;
11729 section_rename = r;
11730 }
11731 }
11732
11733
11734 char *
11735 xtensa_section_rename (char *name)
11736 {
11737 struct rename_section_struct *r = section_rename;
11738
11739 for (r = section_rename; r != NULL; r = r->next)
11740 {
11741 if (strcmp (r->old_name, name) == 0)
11742 return r->new_name;
11743 }
11744
11745 return name;
11746 }
This page took 0.335998 seconds and 5 git commands to generate.