PR 11225
[binutils/dougsmingw.git] / gas / config / tc-xtensa.c
blobefb527b037b4a4a6db282ddcf27eb5d26e8a575f
1 /* tc-xtensa.c -- Assemble Xtensa instructions.
2 Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include <limits.h>
23 #include "as.h"
24 #include "sb.h"
25 #include "safe-ctype.h"
26 #include "tc-xtensa.h"
27 #include "subsegs.h"
28 #include "xtensa-relax.h"
29 #include "dwarf2dbg.h"
30 #include "xtensa-istack.h"
31 #include "struc-symbol.h"
32 #include "xtensa-config.h"
34 /* Provide default values for new configuration settings. */
35 #ifndef XSHAL_ABI
36 #define XSHAL_ABI 0
37 #endif
39 #ifndef uint32
40 #define uint32 unsigned int
41 #endif
42 #ifndef int32
43 #define int32 signed int
44 #endif
46 /* Notes:
48 Naming conventions (used somewhat inconsistently):
49 The xtensa_ functions are exported
50 The xg_ functions are internal
52 We also have a couple of different extensibility mechanisms.
53 1) The idiom replacement:
54 This is used when a line is first parsed to
55 replace an instruction pattern with another instruction
56 It is currently limited to replacements of instructions
57 with constant operands.
58 2) The xtensa-relax.c mechanism that has stronger instruction
59 replacement patterns. When an instruction's immediate field
60 does not fit the next instruction sequence is attempted.
61 In addition, "narrow" opcodes are supported this way. */
64 /* Define characters with special meanings to GAS. */
65 const char comment_chars[] = "#";
66 const char line_comment_chars[] = "#";
67 const char line_separator_chars[] = ";";
68 const char EXP_CHARS[] = "eE";
69 const char FLT_CHARS[] = "rRsSfFdDxXpP";
72 /* Flags to indicate whether the hardware supports the density and
73 absolute literals options. */
75 bfd_boolean density_supported = XCHAL_HAVE_DENSITY;
76 bfd_boolean absolute_literals_supported = XSHAL_USE_ABSOLUTE_LITERALS;
78 /* Maximum width we would pad an unreachable frag to get alignment. */
79 #define UNREACHABLE_MAX_WIDTH 8
81 static vliw_insn cur_vinsn;
83 unsigned xtensa_num_pipe_stages;
84 unsigned xtensa_fetch_width = XCHAL_INST_FETCH_WIDTH;
86 static enum debug_info_type xt_saved_debug_type = DEBUG_NONE;
88 /* Some functions are only valid in the front end. This variable
89 allows us to assert that we haven't crossed over into the
90 back end. */
91 static bfd_boolean past_xtensa_end = FALSE;
93 /* Flags for properties of the last instruction in a segment. */
94 #define FLAG_IS_A0_WRITER 0x1
95 #define FLAG_IS_BAD_LOOPEND 0x2
98 /* We define a special segment names ".literal" to place literals
99 into. The .fini and .init sections are special because they
100 contain code that is moved together by the linker. We give them
101 their own special .fini.literal and .init.literal sections. */
103 #define LITERAL_SECTION_NAME xtensa_section_rename (".literal")
104 #define LIT4_SECTION_NAME xtensa_section_rename (".lit4")
105 #define INIT_SECTION_NAME xtensa_section_rename (".init")
106 #define FINI_SECTION_NAME xtensa_section_rename (".fini")
109 /* This type is used for the directive_stack to keep track of the
110 state of the literal collection pools. If lit_prefix is set, it is
111 used to determine the literal section names; otherwise, the literal
112 sections are determined based on the current text section. The
113 lit_seg and lit4_seg fields cache these literal sections, with the
114 current_text_seg field used a tag to indicate whether the cached
115 values are valid. */
117 typedef struct lit_state_struct
119 char *lit_prefix;
120 segT current_text_seg;
121 segT lit_seg;
122 segT lit4_seg;
123 } lit_state;
125 static lit_state default_lit_sections;
128 /* We keep a list of literal segments. The seg_list type is the node
129 for this list. The literal_head pointer is the head of the list,
130 with the literal_head_h dummy node at the start. */
132 typedef struct seg_list_struct
134 struct seg_list_struct *next;
135 segT seg;
136 } seg_list;
138 static seg_list literal_head_h;
139 static seg_list *literal_head = &literal_head_h;
142 /* Lists of symbols. We keep a list of symbols that label the current
143 instruction, so that we can adjust the symbols when inserting alignment
144 for various instructions. We also keep a list of all the symbols on
145 literals, so that we can fix up those symbols when the literals are
146 later moved into the text sections. */
148 typedef struct sym_list_struct
150 struct sym_list_struct *next;
151 symbolS *sym;
152 } sym_list;
154 static sym_list *insn_labels = NULL;
155 static sym_list *free_insn_labels = NULL;
156 static sym_list *saved_insn_labels = NULL;
158 static sym_list *literal_syms;
161 /* Flags to determine whether to prefer const16 or l32r
162 if both options are available. */
163 int prefer_const16 = 0;
164 int prefer_l32r = 0;
166 /* Global flag to indicate when we are emitting literals. */
167 int generating_literals = 0;
169 /* The following PROPERTY table definitions are copied from
170 <elf/xtensa.h> and must be kept in sync with the code there. */
172 /* Flags in the property tables to specify whether blocks of memory
173 are literals, instructions, data, or unreachable. For
174 instructions, blocks that begin loop targets and branch targets are
175 designated. Blocks that do not allow density, instruction
176 reordering or transformation are also specified. Finally, for
177 branch targets, branch target alignment priority is included.
178 Alignment of the next block is specified in the current block
179 and the size of the current block does not include any fill required
180 to align to the next block. */
182 #define XTENSA_PROP_LITERAL 0x00000001
183 #define XTENSA_PROP_INSN 0x00000002
184 #define XTENSA_PROP_DATA 0x00000004
185 #define XTENSA_PROP_UNREACHABLE 0x00000008
186 /* Instruction only properties at beginning of code. */
187 #define XTENSA_PROP_INSN_LOOP_TARGET 0x00000010
188 #define XTENSA_PROP_INSN_BRANCH_TARGET 0x00000020
189 /* Instruction only properties about code. */
190 #define XTENSA_PROP_INSN_NO_DENSITY 0x00000040
191 #define XTENSA_PROP_INSN_NO_REORDER 0x00000080
192 /* Historically, NO_TRANSFORM was a property of instructions,
193 but it should apply to literals under certain circumstances. */
194 #define XTENSA_PROP_NO_TRANSFORM 0x00000100
196 /* Branch target alignment information. This transmits information
197 to the linker optimization about the priority of aligning a
198 particular block for branch target alignment: None, low priority,
199 high priority, or required. These only need to be checked in
200 instruction blocks marked as XTENSA_PROP_INSN_BRANCH_TARGET.
201 Common usage is
203 switch (GET_XTENSA_PROP_BT_ALIGN (flags))
204 case XTENSA_PROP_BT_ALIGN_NONE:
205 case XTENSA_PROP_BT_ALIGN_LOW:
206 case XTENSA_PROP_BT_ALIGN_HIGH:
207 case XTENSA_PROP_BT_ALIGN_REQUIRE:
209 #define XTENSA_PROP_BT_ALIGN_MASK 0x00000600
211 /* No branch target alignment. */
212 #define XTENSA_PROP_BT_ALIGN_NONE 0x0
213 /* Low priority branch target alignment. */
214 #define XTENSA_PROP_BT_ALIGN_LOW 0x1
215 /* High priority branch target alignment. */
216 #define XTENSA_PROP_BT_ALIGN_HIGH 0x2
217 /* Required branch target alignment. */
218 #define XTENSA_PROP_BT_ALIGN_REQUIRE 0x3
220 #define GET_XTENSA_PROP_BT_ALIGN(flag) \
221 (((unsigned) ((flag) & (XTENSA_PROP_BT_ALIGN_MASK))) >> 9)
222 #define SET_XTENSA_PROP_BT_ALIGN(flag, align) \
223 (((flag) & (~XTENSA_PROP_BT_ALIGN_MASK)) | \
224 (((align) << 9) & XTENSA_PROP_BT_ALIGN_MASK))
227 /* Alignment is specified in the block BEFORE the one that needs
228 alignment. Up to 5 bits. Use GET_XTENSA_PROP_ALIGNMENT(flags) to
229 get the required alignment specified as a power of 2. Use
230 SET_XTENSA_PROP_ALIGNMENT(flags, pow2) to set the required
231 alignment. Be careful of side effects since the SET will evaluate
232 flags twice. Also, note that the SIZE of a block in the property
233 table does not include the alignment size, so the alignment fill
234 must be calculated to determine if two blocks are contiguous.
235 TEXT_ALIGN is not currently implemented but is a placeholder for a
236 possible future implementation. */
238 #define XTENSA_PROP_ALIGN 0x00000800
240 #define XTENSA_PROP_ALIGNMENT_MASK 0x0001f000
242 #define GET_XTENSA_PROP_ALIGNMENT(flag) \
243 (((unsigned) ((flag) & (XTENSA_PROP_ALIGNMENT_MASK))) >> 12)
244 #define SET_XTENSA_PROP_ALIGNMENT(flag, align) \
245 (((flag) & (~XTENSA_PROP_ALIGNMENT_MASK)) | \
246 (((align) << 12) & XTENSA_PROP_ALIGNMENT_MASK))
248 #define XTENSA_PROP_INSN_ABSLIT 0x00020000
251 /* Structure for saving instruction and alignment per-fragment data
252 that will be written to the object file. This structure is
253 equivalent to the actual data that will be written out to the file
254 but is easier to use. We provide a conversion to file flags
255 in frag_flags_to_number. */
257 typedef struct frag_flags_struct frag_flags;
259 struct frag_flags_struct
261 /* is_literal should only be used after xtensa_move_literals.
262 If you need to check if you are generating a literal fragment,
263 then use the generating_literals global. */
265 unsigned is_literal : 1;
266 unsigned is_insn : 1;
267 unsigned is_data : 1;
268 unsigned is_unreachable : 1;
270 /* is_specific_opcode implies no_transform. */
271 unsigned is_no_transform : 1;
273 struct
275 unsigned is_loop_target : 1;
276 unsigned is_branch_target : 1; /* Branch targets have a priority. */
277 unsigned bt_align_priority : 2;
279 unsigned is_no_density : 1;
280 /* no_longcalls flag does not need to be placed in the object file. */
282 unsigned is_no_reorder : 1;
284 /* Uses absolute literal addressing for l32r. */
285 unsigned is_abslit : 1;
286 } insn;
287 unsigned is_align : 1;
288 unsigned alignment : 5;
292 /* Structure for saving information about a block of property data
293 for frags that have the same flags. */
294 struct xtensa_block_info_struct
296 segT sec;
297 bfd_vma offset;
298 size_t size;
299 frag_flags flags;
300 struct xtensa_block_info_struct *next;
304 /* Structure for saving the current state before emitting literals. */
305 typedef struct emit_state_struct
307 const char *name;
308 segT now_seg;
309 subsegT now_subseg;
310 int generating_literals;
311 } emit_state;
314 /* Opcode placement information */
316 typedef unsigned long long bitfield;
317 #define bit_is_set(bit, bf) ((bf) & (0x01ll << (bit)))
318 #define set_bit(bit, bf) ((bf) |= (0x01ll << (bit)))
319 #define clear_bit(bit, bf) ((bf) &= ~(0x01ll << (bit)))
321 #define MAX_FORMATS 32
323 typedef struct op_placement_info_struct
325 int num_formats;
326 /* A number describing how restrictive the issue is for this
327 opcode. For example, an opcode that fits lots of different
328 formats has a high freedom, as does an opcode that fits
329 only one format but many slots in that format. The most
330 restrictive is the opcode that fits only one slot in one
331 format. */
332 int issuef;
333 xtensa_format narrowest;
334 char narrowest_size;
335 char narrowest_slot;
337 /* formats is a bitfield with the Nth bit set
338 if the opcode fits in the Nth xtensa_format. */
339 bitfield formats;
341 /* slots[N]'s Mth bit is set if the op fits in the
342 Mth slot of the Nth xtensa_format. */
343 bitfield slots[MAX_FORMATS];
345 /* A count of the number of slots in a given format
346 an op can fit (i.e., the bitcount of the slot field above). */
347 char slots_in_format[MAX_FORMATS];
349 } op_placement_info, *op_placement_info_table;
351 op_placement_info_table op_placement_table;
354 /* Extra expression types. */
356 #define O_pltrel O_md1 /* like O_symbol but use a PLT reloc */
357 #define O_hi16 O_md2 /* use high 16 bits of symbolic value */
358 #define O_lo16 O_md3 /* use low 16 bits of symbolic value */
359 #define O_pcrel O_md4 /* value is a PC-relative offset */
360 #define O_tlsfunc O_md5 /* TLS_FUNC/TLSDESC_FN relocation */
361 #define O_tlsarg O_md6 /* TLS_ARG/TLSDESC_ARG relocation */
362 #define O_tlscall O_md7 /* TLS_CALL relocation */
363 #define O_tpoff O_md8 /* TPOFF relocation */
364 #define O_dtpoff O_md9 /* DTPOFF relocation */
366 struct suffix_reloc_map
368 char *suffix;
369 int length;
370 bfd_reloc_code_real_type reloc;
371 unsigned char operator;
374 #define SUFFIX_MAP(str, reloc, op) { str, sizeof (str) - 1, reloc, op }
376 static struct suffix_reloc_map suffix_relocs[] =
378 SUFFIX_MAP ("l", BFD_RELOC_LO16, O_lo16),
379 SUFFIX_MAP ("h", BFD_RELOC_HI16, O_hi16),
380 SUFFIX_MAP ("plt", BFD_RELOC_XTENSA_PLT, O_pltrel),
381 SUFFIX_MAP ("pcrel", BFD_RELOC_32_PCREL, O_pcrel),
382 SUFFIX_MAP ("tlsfunc", BFD_RELOC_XTENSA_TLS_FUNC, O_tlsfunc),
383 SUFFIX_MAP ("tlsarg", BFD_RELOC_XTENSA_TLS_ARG, O_tlsarg),
384 SUFFIX_MAP ("tlscall", BFD_RELOC_XTENSA_TLS_CALL, O_tlscall),
385 SUFFIX_MAP ("tpoff", BFD_RELOC_XTENSA_TLS_TPOFF, O_tpoff),
386 SUFFIX_MAP ("dtpoff", BFD_RELOC_XTENSA_TLS_DTPOFF, O_dtpoff),
387 { (char *) 0, 0, BFD_RELOC_UNUSED, 0 }
391 /* Directives. */
393 typedef enum
395 directive_none = 0,
396 directive_literal,
397 directive_density,
398 directive_transform,
399 directive_freeregs,
400 directive_longcalls,
401 directive_literal_prefix,
402 directive_schedule,
403 directive_absolute_literals,
404 directive_last_directive
405 } directiveE;
407 typedef struct
409 const char *name;
410 bfd_boolean can_be_negated;
411 } directive_infoS;
413 const directive_infoS directive_info[] =
415 { "none", FALSE },
416 { "literal", FALSE },
417 { "density", TRUE },
418 { "transform", TRUE },
419 { "freeregs", FALSE },
420 { "longcalls", TRUE },
421 { "literal_prefix", FALSE },
422 { "schedule", TRUE },
423 { "absolute-literals", TRUE }
426 bfd_boolean directive_state[] =
428 FALSE, /* none */
429 FALSE, /* literal */
430 #if !XCHAL_HAVE_DENSITY
431 FALSE, /* density */
432 #else
433 TRUE, /* density */
434 #endif
435 TRUE, /* transform */
436 FALSE, /* freeregs */
437 FALSE, /* longcalls */
438 FALSE, /* literal_prefix */
439 FALSE, /* schedule */
440 #if XSHAL_USE_ABSOLUTE_LITERALS
441 TRUE /* absolute_literals */
442 #else
443 FALSE /* absolute_literals */
444 #endif
448 /* Directive functions. */
450 static void xtensa_begin_directive (int);
451 static void xtensa_end_directive (int);
452 static void xtensa_literal_prefix (void);
453 static void xtensa_literal_position (int);
454 static void xtensa_literal_pseudo (int);
455 static void xtensa_frequency_pseudo (int);
456 static void xtensa_elf_cons (int);
457 static void xtensa_leb128 (int);
459 /* Parsing and Idiom Translation. */
461 static bfd_reloc_code_real_type xtensa_elf_suffix (char **, expressionS *);
463 /* Various Other Internal Functions. */
465 extern bfd_boolean xg_is_single_relaxable_insn (TInsn *, TInsn *, bfd_boolean);
466 static bfd_boolean xg_build_to_insn (TInsn *, TInsn *, BuildInstr *);
467 static void xtensa_mark_literal_pool_location (void);
468 static addressT get_expanded_loop_offset (xtensa_opcode);
469 static fragS *get_literal_pool_location (segT);
470 static void set_literal_pool_location (segT, fragS *);
471 static void xtensa_set_frag_assembly_state (fragS *);
472 static void finish_vinsn (vliw_insn *);
473 static bfd_boolean emit_single_op (TInsn *);
474 static int total_frag_text_expansion (fragS *);
476 /* Alignment Functions. */
478 static int get_text_align_power (unsigned);
479 static int get_text_align_max_fill_size (int, bfd_boolean, bfd_boolean);
480 static int branch_align_power (segT);
482 /* Helpers for xtensa_relax_frag(). */
484 static long relax_frag_add_nop (fragS *);
486 /* Accessors for additional per-subsegment information. */
488 static unsigned get_last_insn_flags (segT, subsegT);
489 static void set_last_insn_flags (segT, subsegT, unsigned, bfd_boolean);
490 static float get_subseg_total_freq (segT, subsegT);
491 static float get_subseg_target_freq (segT, subsegT);
492 static void set_subseg_freq (segT, subsegT, float, float);
494 /* Segment list functions. */
496 static void xtensa_move_literals (void);
497 static void xtensa_reorder_segments (void);
498 static void xtensa_switch_to_literal_fragment (emit_state *);
499 static void xtensa_switch_to_non_abs_literal_fragment (emit_state *);
500 static void xtensa_switch_section_emit_state (emit_state *, segT, subsegT);
501 static void xtensa_restore_emit_state (emit_state *);
502 static segT cache_literal_section (bfd_boolean);
504 /* Import from elf32-xtensa.c in BFD library. */
506 extern asection *xtensa_make_property_section (asection *, const char *);
508 /* op_placement_info functions. */
510 static void init_op_placement_info_table (void);
511 extern bfd_boolean opcode_fits_format_slot (xtensa_opcode, xtensa_format, int);
512 static int xg_get_single_size (xtensa_opcode);
513 static xtensa_format xg_get_single_format (xtensa_opcode);
514 static int xg_get_single_slot (xtensa_opcode);
516 /* TInsn and IStack functions. */
518 static bfd_boolean tinsn_has_symbolic_operands (const TInsn *);
519 static bfd_boolean tinsn_has_invalid_symbolic_operands (const TInsn *);
520 static bfd_boolean tinsn_has_complex_operands (const TInsn *);
521 static bfd_boolean tinsn_to_insnbuf (TInsn *, xtensa_insnbuf);
522 static bfd_boolean tinsn_check_arguments (const TInsn *);
523 static void tinsn_from_chars (TInsn *, char *, int);
524 static void tinsn_immed_from_frag (TInsn *, fragS *, int);
525 static int get_num_stack_text_bytes (IStack *);
526 static int get_num_stack_literal_bytes (IStack *);
528 /* vliw_insn functions. */
530 static void xg_init_vinsn (vliw_insn *);
531 static void xg_copy_vinsn (vliw_insn *, vliw_insn *);
532 static void xg_clear_vinsn (vliw_insn *);
533 static bfd_boolean vinsn_has_specific_opcodes (vliw_insn *);
534 static void xg_free_vinsn (vliw_insn *);
535 static bfd_boolean vinsn_to_insnbuf
536 (vliw_insn *, char *, fragS *, bfd_boolean);
537 static void vinsn_from_chars (vliw_insn *, char *);
539 /* Expression Utilities. */
541 bfd_boolean expr_is_const (const expressionS *);
542 offsetT get_expr_const (const expressionS *);
543 void set_expr_const (expressionS *, offsetT);
544 bfd_boolean expr_is_register (const expressionS *);
545 offsetT get_expr_register (const expressionS *);
546 void set_expr_symbol_offset (expressionS *, symbolS *, offsetT);
547 bfd_boolean expr_is_equal (expressionS *, expressionS *);
548 static void copy_expr (expressionS *, const expressionS *);
550 /* Section renaming. */
552 static void build_section_rename (const char *);
555 /* ISA imported from bfd. */
556 extern xtensa_isa xtensa_default_isa;
558 extern int target_big_endian;
560 static xtensa_opcode xtensa_addi_opcode;
561 static xtensa_opcode xtensa_addmi_opcode;
562 static xtensa_opcode xtensa_call0_opcode;
563 static xtensa_opcode xtensa_call4_opcode;
564 static xtensa_opcode xtensa_call8_opcode;
565 static xtensa_opcode xtensa_call12_opcode;
566 static xtensa_opcode xtensa_callx0_opcode;
567 static xtensa_opcode xtensa_callx4_opcode;
568 static xtensa_opcode xtensa_callx8_opcode;
569 static xtensa_opcode xtensa_callx12_opcode;
570 static xtensa_opcode xtensa_const16_opcode;
571 static xtensa_opcode xtensa_entry_opcode;
572 static xtensa_opcode xtensa_extui_opcode;
573 static xtensa_opcode xtensa_movi_opcode;
574 static xtensa_opcode xtensa_movi_n_opcode;
575 static xtensa_opcode xtensa_isync_opcode;
576 static xtensa_opcode xtensa_j_opcode;
577 static xtensa_opcode xtensa_jx_opcode;
578 static xtensa_opcode xtensa_l32r_opcode;
579 static xtensa_opcode xtensa_loop_opcode;
580 static xtensa_opcode xtensa_loopnez_opcode;
581 static xtensa_opcode xtensa_loopgtz_opcode;
582 static xtensa_opcode xtensa_nop_opcode;
583 static xtensa_opcode xtensa_nop_n_opcode;
584 static xtensa_opcode xtensa_or_opcode;
585 static xtensa_opcode xtensa_ret_opcode;
586 static xtensa_opcode xtensa_ret_n_opcode;
587 static xtensa_opcode xtensa_retw_opcode;
588 static xtensa_opcode xtensa_retw_n_opcode;
589 static xtensa_opcode xtensa_rsr_lcount_opcode;
590 static xtensa_opcode xtensa_waiti_opcode;
591 static int config_max_slots = 0;
594 /* Command-line Options. */
596 bfd_boolean use_literal_section = TRUE;
597 enum flix_level produce_flix = FLIX_ALL;
598 static bfd_boolean align_targets = TRUE;
599 static bfd_boolean warn_unaligned_branch_targets = FALSE;
600 static bfd_boolean has_a0_b_retw = FALSE;
601 static bfd_boolean workaround_a0_b_retw = FALSE;
602 static bfd_boolean workaround_b_j_loop_end = FALSE;
603 static bfd_boolean workaround_short_loop = FALSE;
604 static bfd_boolean maybe_has_short_loop = FALSE;
605 static bfd_boolean workaround_close_loop_end = FALSE;
606 static bfd_boolean maybe_has_close_loop_end = FALSE;
607 static bfd_boolean enforce_three_byte_loop_align = FALSE;
609 /* When workaround_short_loops is TRUE, all loops with early exits must
610 have at least 3 instructions. workaround_all_short_loops is a modifier
611 to the workaround_short_loop flag. In addition to the
612 workaround_short_loop actions, all straightline loopgtz and loopnez
613 must have at least 3 instructions. */
615 static bfd_boolean workaround_all_short_loops = FALSE;
618 static void
619 xtensa_setup_hw_workarounds (int earliest, int latest)
621 if (earliest > latest)
622 as_fatal (_("illegal range of target hardware versions"));
624 /* Enable all workarounds for pre-T1050.0 hardware. */
625 if (earliest < 105000 || latest < 105000)
627 workaround_a0_b_retw |= TRUE;
628 workaround_b_j_loop_end |= TRUE;
629 workaround_short_loop |= TRUE;
630 workaround_close_loop_end |= TRUE;
631 workaround_all_short_loops |= TRUE;
632 enforce_three_byte_loop_align = TRUE;
637 enum
639 option_density = OPTION_MD_BASE,
640 option_no_density,
642 option_flix,
643 option_no_generate_flix,
644 option_no_flix,
646 option_relax,
647 option_no_relax,
649 option_link_relax,
650 option_no_link_relax,
652 option_generics,
653 option_no_generics,
655 option_transform,
656 option_no_transform,
658 option_text_section_literals,
659 option_no_text_section_literals,
661 option_absolute_literals,
662 option_no_absolute_literals,
664 option_align_targets,
665 option_no_align_targets,
667 option_warn_unaligned_targets,
669 option_longcalls,
670 option_no_longcalls,
672 option_workaround_a0_b_retw,
673 option_no_workaround_a0_b_retw,
675 option_workaround_b_j_loop_end,
676 option_no_workaround_b_j_loop_end,
678 option_workaround_short_loop,
679 option_no_workaround_short_loop,
681 option_workaround_all_short_loops,
682 option_no_workaround_all_short_loops,
684 option_workaround_close_loop_end,
685 option_no_workaround_close_loop_end,
687 option_no_workarounds,
689 option_rename_section_name,
691 option_prefer_l32r,
692 option_prefer_const16,
694 option_target_hardware
697 const char *md_shortopts = "";
699 struct option md_longopts[] =
701 { "density", no_argument, NULL, option_density },
702 { "no-density", no_argument, NULL, option_no_density },
704 { "flix", no_argument, NULL, option_flix },
705 { "no-generate-flix", no_argument, NULL, option_no_generate_flix },
706 { "no-allow-flix", no_argument, NULL, option_no_flix },
708 /* Both "relax" and "generics" are deprecated and treated as equivalent
709 to the "transform" option. */
710 { "relax", no_argument, NULL, option_relax },
711 { "no-relax", no_argument, NULL, option_no_relax },
712 { "generics", no_argument, NULL, option_generics },
713 { "no-generics", no_argument, NULL, option_no_generics },
715 { "transform", no_argument, NULL, option_transform },
716 { "no-transform", no_argument, NULL, option_no_transform },
717 { "text-section-literals", no_argument, NULL, option_text_section_literals },
718 { "no-text-section-literals", no_argument, NULL,
719 option_no_text_section_literals },
720 { "absolute-literals", no_argument, NULL, option_absolute_literals },
721 { "no-absolute-literals", no_argument, NULL, option_no_absolute_literals },
722 /* This option was changed from -align-target to -target-align
723 because it conflicted with the "-al" option. */
724 { "target-align", no_argument, NULL, option_align_targets },
725 { "no-target-align", no_argument, NULL, option_no_align_targets },
726 { "warn-unaligned-targets", no_argument, NULL,
727 option_warn_unaligned_targets },
728 { "longcalls", no_argument, NULL, option_longcalls },
729 { "no-longcalls", no_argument, NULL, option_no_longcalls },
731 { "no-workaround-a0-b-retw", no_argument, NULL,
732 option_no_workaround_a0_b_retw },
733 { "workaround-a0-b-retw", no_argument, NULL, option_workaround_a0_b_retw },
735 { "no-workaround-b-j-loop-end", no_argument, NULL,
736 option_no_workaround_b_j_loop_end },
737 { "workaround-b-j-loop-end", no_argument, NULL,
738 option_workaround_b_j_loop_end },
740 { "no-workaround-short-loops", no_argument, NULL,
741 option_no_workaround_short_loop },
742 { "workaround-short-loops", no_argument, NULL,
743 option_workaround_short_loop },
745 { "no-workaround-all-short-loops", no_argument, NULL,
746 option_no_workaround_all_short_loops },
747 { "workaround-all-short-loop", no_argument, NULL,
748 option_workaround_all_short_loops },
750 { "prefer-l32r", no_argument, NULL, option_prefer_l32r },
751 { "prefer-const16", no_argument, NULL, option_prefer_const16 },
753 { "no-workarounds", no_argument, NULL, option_no_workarounds },
755 { "no-workaround-close-loop-end", no_argument, NULL,
756 option_no_workaround_close_loop_end },
757 { "workaround-close-loop-end", no_argument, NULL,
758 option_workaround_close_loop_end },
760 { "rename-section", required_argument, NULL, option_rename_section_name },
762 { "link-relax", no_argument, NULL, option_link_relax },
763 { "no-link-relax", no_argument, NULL, option_no_link_relax },
765 { "target-hardware", required_argument, NULL, option_target_hardware },
767 { NULL, no_argument, NULL, 0 }
770 size_t md_longopts_size = sizeof md_longopts;
774 md_parse_option (int c, char *arg)
776 switch (c)
778 case option_density:
779 as_warn (_("--density option is ignored"));
780 return 1;
781 case option_no_density:
782 as_warn (_("--no-density option is ignored"));
783 return 1;
784 case option_link_relax:
785 linkrelax = 1;
786 return 1;
787 case option_no_link_relax:
788 linkrelax = 0;
789 return 1;
790 case option_flix:
791 produce_flix = FLIX_ALL;
792 return 1;
793 case option_no_generate_flix:
794 produce_flix = FLIX_NO_GENERATE;
795 return 1;
796 case option_no_flix:
797 produce_flix = FLIX_NONE;
798 return 1;
799 case option_generics:
800 as_warn (_("--generics is deprecated; use --transform instead"));
801 return md_parse_option (option_transform, arg);
802 case option_no_generics:
803 as_warn (_("--no-generics is deprecated; use --no-transform instead"));
804 return md_parse_option (option_no_transform, arg);
805 case option_relax:
806 as_warn (_("--relax is deprecated; use --transform instead"));
807 return md_parse_option (option_transform, arg);
808 case option_no_relax:
809 as_warn (_("--no-relax is deprecated; use --no-transform instead"));
810 return md_parse_option (option_no_transform, arg);
811 case option_longcalls:
812 directive_state[directive_longcalls] = TRUE;
813 return 1;
814 case option_no_longcalls:
815 directive_state[directive_longcalls] = FALSE;
816 return 1;
817 case option_text_section_literals:
818 use_literal_section = FALSE;
819 return 1;
820 case option_no_text_section_literals:
821 use_literal_section = TRUE;
822 return 1;
823 case option_absolute_literals:
824 if (!absolute_literals_supported)
826 as_fatal (_("--absolute-literals option not supported in this Xtensa configuration"));
827 return 0;
829 directive_state[directive_absolute_literals] = TRUE;
830 return 1;
831 case option_no_absolute_literals:
832 directive_state[directive_absolute_literals] = FALSE;
833 return 1;
835 case option_workaround_a0_b_retw:
836 workaround_a0_b_retw = TRUE;
837 return 1;
838 case option_no_workaround_a0_b_retw:
839 workaround_a0_b_retw = FALSE;
840 return 1;
841 case option_workaround_b_j_loop_end:
842 workaround_b_j_loop_end = TRUE;
843 return 1;
844 case option_no_workaround_b_j_loop_end:
845 workaround_b_j_loop_end = FALSE;
846 return 1;
848 case option_workaround_short_loop:
849 workaround_short_loop = TRUE;
850 return 1;
851 case option_no_workaround_short_loop:
852 workaround_short_loop = FALSE;
853 return 1;
855 case option_workaround_all_short_loops:
856 workaround_all_short_loops = TRUE;
857 return 1;
858 case option_no_workaround_all_short_loops:
859 workaround_all_short_loops = FALSE;
860 return 1;
862 case option_workaround_close_loop_end:
863 workaround_close_loop_end = TRUE;
864 return 1;
865 case option_no_workaround_close_loop_end:
866 workaround_close_loop_end = FALSE;
867 return 1;
869 case option_no_workarounds:
870 workaround_a0_b_retw = FALSE;
871 workaround_b_j_loop_end = FALSE;
872 workaround_short_loop = FALSE;
873 workaround_all_short_loops = FALSE;
874 workaround_close_loop_end = FALSE;
875 return 1;
877 case option_align_targets:
878 align_targets = TRUE;
879 return 1;
880 case option_no_align_targets:
881 align_targets = FALSE;
882 return 1;
884 case option_warn_unaligned_targets:
885 warn_unaligned_branch_targets = TRUE;
886 return 1;
888 case option_rename_section_name:
889 build_section_rename (arg);
890 return 1;
892 case 'Q':
893 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
894 should be emitted or not. FIXME: Not implemented. */
895 return 1;
897 case option_prefer_l32r:
898 if (prefer_const16)
899 as_fatal (_("prefer-l32r conflicts with prefer-const16"));
900 prefer_l32r = 1;
901 return 1;
903 case option_prefer_const16:
904 if (prefer_l32r)
905 as_fatal (_("prefer-const16 conflicts with prefer-l32r"));
906 prefer_const16 = 1;
907 return 1;
909 case option_target_hardware:
911 int earliest, latest = 0;
912 if (*arg == 0 || *arg == '-')
913 as_fatal (_("invalid target hardware version"));
915 earliest = strtol (arg, &arg, 0);
917 if (*arg == 0)
918 latest = earliest;
919 else if (*arg == '-')
921 if (*++arg == 0)
922 as_fatal (_("invalid target hardware version"));
923 latest = strtol (arg, &arg, 0);
925 if (*arg != 0)
926 as_fatal (_("invalid target hardware version"));
928 xtensa_setup_hw_workarounds (earliest, latest);
929 return 1;
932 case option_transform:
933 /* This option has no affect other than to use the defaults,
934 which are already set. */
935 return 1;
937 case option_no_transform:
938 /* This option turns off all transformations of any kind.
939 However, because we want to preserve the state of other
940 directives, we only change its own field. Thus, before
941 you perform any transformation, always check if transform
942 is available. If you use the functions we provide for this
943 purpose, you will be ok. */
944 directive_state[directive_transform] = FALSE;
945 return 1;
947 default:
948 return 0;
953 void
954 md_show_usage (FILE *stream)
956 fputs ("\n\
957 Xtensa options:\n\
958 --[no-]text-section-literals\n\
959 [Do not] put literals in the text section\n\
960 --[no-]absolute-literals\n\
961 [Do not] default to use non-PC-relative literals\n\
962 --[no-]target-align [Do not] try to align branch targets\n\
963 --[no-]longcalls [Do not] emit 32-bit call sequences\n\
964 --[no-]transform [Do not] transform instructions\n\
965 --flix both allow hand-written and generate flix bundles\n\
966 --no-generate-flix allow hand-written but do not generate\n\
967 flix bundles\n\
968 --no-allow-flix neither allow hand-written nor generate\n\
969 flix bundles\n\
970 --rename-section old=new Rename section 'old' to 'new'\n", stream);
974 /* Functions related to the list of current label symbols. */
976 static void
977 xtensa_add_insn_label (symbolS *sym)
979 sym_list *l;
981 if (!free_insn_labels)
982 l = (sym_list *) xmalloc (sizeof (sym_list));
983 else
985 l = free_insn_labels;
986 free_insn_labels = l->next;
989 l->sym = sym;
990 l->next = insn_labels;
991 insn_labels = l;
995 static void
996 xtensa_clear_insn_labels (void)
998 sym_list **pl;
1000 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1002 *pl = insn_labels;
1003 insn_labels = NULL;
1007 static void
1008 xtensa_move_labels (fragS *new_frag, valueT new_offset)
1010 sym_list *lit;
1012 for (lit = insn_labels; lit; lit = lit->next)
1014 symbolS *lit_sym = lit->sym;
1015 S_SET_VALUE (lit_sym, new_offset);
1016 symbol_set_frag (lit_sym, new_frag);
1021 /* Directive data and functions. */
1023 typedef struct state_stackS_struct
1025 directiveE directive;
1026 bfd_boolean negated;
1027 bfd_boolean old_state;
1028 const char *file;
1029 unsigned int line;
1030 const void *datum;
1031 struct state_stackS_struct *prev;
1032 } state_stackS;
1034 state_stackS *directive_state_stack;
1036 const pseudo_typeS md_pseudo_table[] =
1038 { "align", s_align_bytes, 0 }, /* Defaulting is invalid (0). */
1039 { "literal_position", xtensa_literal_position, 0 },
1040 { "frame", s_ignore, 0 }, /* Formerly used for STABS debugging. */
1041 { "long", xtensa_elf_cons, 4 },
1042 { "word", xtensa_elf_cons, 4 },
1043 { "4byte", xtensa_elf_cons, 4 },
1044 { "short", xtensa_elf_cons, 2 },
1045 { "2byte", xtensa_elf_cons, 2 },
1046 { "sleb128", xtensa_leb128, 1},
1047 { "uleb128", xtensa_leb128, 0},
1048 { "begin", xtensa_begin_directive, 0 },
1049 { "end", xtensa_end_directive, 0 },
1050 { "literal", xtensa_literal_pseudo, 0 },
1051 { "frequency", xtensa_frequency_pseudo, 0 },
1052 { NULL, 0, 0 },
1056 static bfd_boolean
1057 use_transform (void)
1059 /* After md_end, you should be checking frag by frag, rather
1060 than state directives. */
1061 gas_assert (!past_xtensa_end);
1062 return directive_state[directive_transform];
1066 static bfd_boolean
1067 do_align_targets (void)
1069 /* Do not use this function after md_end; just look at align_targets
1070 instead. There is no target-align directive, so alignment is either
1071 enabled for all frags or not done at all. */
1072 gas_assert (!past_xtensa_end);
1073 return align_targets && use_transform ();
1077 static void
1078 directive_push (directiveE directive, bfd_boolean negated, const void *datum)
1080 char *file;
1081 unsigned int line;
1082 state_stackS *stack = (state_stackS *) xmalloc (sizeof (state_stackS));
1084 as_where (&file, &line);
1086 stack->directive = directive;
1087 stack->negated = negated;
1088 stack->old_state = directive_state[directive];
1089 stack->file = file;
1090 stack->line = line;
1091 stack->datum = datum;
1092 stack->prev = directive_state_stack;
1093 directive_state_stack = stack;
1095 directive_state[directive] = !negated;
1099 static void
1100 directive_pop (directiveE *directive,
1101 bfd_boolean *negated,
1102 const char **file,
1103 unsigned int *line,
1104 const void **datum)
1106 state_stackS *top = directive_state_stack;
1108 if (!directive_state_stack)
1110 as_bad (_("unmatched end directive"));
1111 *directive = directive_none;
1112 return;
1115 directive_state[directive_state_stack->directive] = top->old_state;
1116 *directive = top->directive;
1117 *negated = top->negated;
1118 *file = top->file;
1119 *line = top->line;
1120 *datum = top->datum;
1121 directive_state_stack = top->prev;
1122 free (top);
1126 static void
1127 directive_balance (void)
1129 while (directive_state_stack)
1131 directiveE directive;
1132 bfd_boolean negated;
1133 const char *file;
1134 unsigned int line;
1135 const void *datum;
1137 directive_pop (&directive, &negated, &file, &line, &datum);
1138 as_warn_where ((char *) file, line,
1139 _(".begin directive with no matching .end directive"));
1144 static bfd_boolean
1145 inside_directive (directiveE dir)
1147 state_stackS *top = directive_state_stack;
1149 while (top && top->directive != dir)
1150 top = top->prev;
1152 return (top != NULL);
1156 static void
1157 get_directive (directiveE *directive, bfd_boolean *negated)
1159 int len;
1160 unsigned i;
1161 char *directive_string;
1163 if (strncmp (input_line_pointer, "no-", 3) != 0)
1164 *negated = FALSE;
1165 else
1167 *negated = TRUE;
1168 input_line_pointer += 3;
1171 len = strspn (input_line_pointer,
1172 "abcdefghijklmnopqrstuvwxyz_-/0123456789.");
1174 /* This code is a hack to make .begin [no-][generics|relax] exactly
1175 equivalent to .begin [no-]transform. We should remove it when
1176 we stop accepting those options. */
1178 if (strncmp (input_line_pointer, "generics", strlen ("generics")) == 0)
1180 as_warn (_("[no-]generics is deprecated; use [no-]transform instead"));
1181 directive_string = "transform";
1183 else if (strncmp (input_line_pointer, "relax", strlen ("relax")) == 0)
1185 as_warn (_("[no-]relax is deprecated; use [no-]transform instead"));
1186 directive_string = "transform";
1188 else
1189 directive_string = input_line_pointer;
1191 for (i = 0; i < sizeof (directive_info) / sizeof (*directive_info); ++i)
1193 if (strncmp (directive_string, directive_info[i].name, len) == 0)
1195 input_line_pointer += len;
1196 *directive = (directiveE) i;
1197 if (*negated && !directive_info[i].can_be_negated)
1198 as_bad (_("directive %s cannot be negated"),
1199 directive_info[i].name);
1200 return;
1204 as_bad (_("unknown directive"));
1205 *directive = (directiveE) XTENSA_UNDEFINED;
1209 static void
1210 xtensa_begin_directive (int ignore ATTRIBUTE_UNUSED)
1212 directiveE directive;
1213 bfd_boolean negated;
1214 emit_state *state;
1215 lit_state *ls;
1217 get_directive (&directive, &negated);
1218 if (directive == (directiveE) XTENSA_UNDEFINED)
1220 discard_rest_of_line ();
1221 return;
1224 if (cur_vinsn.inside_bundle)
1225 as_bad (_("directives are not valid inside bundles"));
1227 switch (directive)
1229 case directive_literal:
1230 if (!inside_directive (directive_literal))
1232 /* Previous labels go with whatever follows this directive, not with
1233 the literal, so save them now. */
1234 saved_insn_labels = insn_labels;
1235 insn_labels = NULL;
1237 as_warn (_(".begin literal is deprecated; use .literal instead"));
1238 state = (emit_state *) xmalloc (sizeof (emit_state));
1239 xtensa_switch_to_literal_fragment (state);
1240 directive_push (directive_literal, negated, state);
1241 break;
1243 case directive_literal_prefix:
1244 /* Have to flush pending output because a movi relaxed to an l32r
1245 might produce a literal. */
1246 md_flush_pending_output ();
1247 /* Check to see if the current fragment is a literal
1248 fragment. If it is, then this operation is not allowed. */
1249 if (generating_literals)
1251 as_bad (_("cannot set literal_prefix inside literal fragment"));
1252 return;
1255 /* Allocate the literal state for this section and push
1256 onto the directive stack. */
1257 ls = xmalloc (sizeof (lit_state));
1258 gas_assert (ls);
1260 *ls = default_lit_sections;
1261 directive_push (directive_literal_prefix, negated, ls);
1263 /* Process the new prefix. */
1264 xtensa_literal_prefix ();
1265 break;
1267 case directive_freeregs:
1268 /* This information is currently unused, but we'll accept the statement
1269 and just discard the rest of the line. This won't check the syntax,
1270 but it will accept every correct freeregs directive. */
1271 input_line_pointer += strcspn (input_line_pointer, "\n");
1272 directive_push (directive_freeregs, negated, 0);
1273 break;
1275 case directive_schedule:
1276 md_flush_pending_output ();
1277 frag_var (rs_fill, 0, 0, frag_now->fr_subtype,
1278 frag_now->fr_symbol, frag_now->fr_offset, NULL);
1279 directive_push (directive_schedule, negated, 0);
1280 xtensa_set_frag_assembly_state (frag_now);
1281 break;
1283 case directive_density:
1284 as_warn (_(".begin [no-]density is ignored"));
1285 break;
1287 case directive_absolute_literals:
1288 md_flush_pending_output ();
1289 if (!absolute_literals_supported && !negated)
1291 as_warn (_("Xtensa absolute literals option not supported; ignored"));
1292 break;
1294 xtensa_set_frag_assembly_state (frag_now);
1295 directive_push (directive, negated, 0);
1296 break;
1298 default:
1299 md_flush_pending_output ();
1300 xtensa_set_frag_assembly_state (frag_now);
1301 directive_push (directive, negated, 0);
1302 break;
1305 demand_empty_rest_of_line ();
1309 static void
1310 xtensa_end_directive (int ignore ATTRIBUTE_UNUSED)
1312 directiveE begin_directive, end_directive;
1313 bfd_boolean begin_negated, end_negated;
1314 const char *file;
1315 unsigned int line;
1316 emit_state *state;
1317 emit_state **state_ptr;
1318 lit_state *s;
1320 if (cur_vinsn.inside_bundle)
1321 as_bad (_("directives are not valid inside bundles"));
1323 get_directive (&end_directive, &end_negated);
1325 md_flush_pending_output ();
1327 switch (end_directive)
1329 case (directiveE) XTENSA_UNDEFINED:
1330 discard_rest_of_line ();
1331 return;
1333 case directive_density:
1334 as_warn (_(".end [no-]density is ignored"));
1335 demand_empty_rest_of_line ();
1336 break;
1338 case directive_absolute_literals:
1339 if (!absolute_literals_supported && !end_negated)
1341 as_warn (_("Xtensa absolute literals option not supported; ignored"));
1342 demand_empty_rest_of_line ();
1343 return;
1345 break;
1347 default:
1348 break;
1351 state_ptr = &state; /* use state_ptr to avoid type-punning warning */
1352 directive_pop (&begin_directive, &begin_negated, &file, &line,
1353 (const void **) state_ptr);
1355 if (begin_directive != directive_none)
1357 if (begin_directive != end_directive || begin_negated != end_negated)
1359 as_bad (_("does not match begin %s%s at %s:%d"),
1360 begin_negated ? "no-" : "",
1361 directive_info[begin_directive].name, file, line);
1363 else
1365 switch (end_directive)
1367 case directive_literal:
1368 frag_var (rs_fill, 0, 0, 0, NULL, 0, NULL);
1369 xtensa_restore_emit_state (state);
1370 xtensa_set_frag_assembly_state (frag_now);
1371 free (state);
1372 if (!inside_directive (directive_literal))
1374 /* Restore the list of current labels. */
1375 xtensa_clear_insn_labels ();
1376 insn_labels = saved_insn_labels;
1378 break;
1380 case directive_literal_prefix:
1381 /* Restore the default collection sections from saved state. */
1382 s = (lit_state *) state;
1383 gas_assert (s);
1384 default_lit_sections = *s;
1386 /* Free the state storage. */
1387 free (s->lit_prefix);
1388 free (s);
1389 break;
1391 case directive_schedule:
1392 case directive_freeregs:
1393 break;
1395 default:
1396 xtensa_set_frag_assembly_state (frag_now);
1397 break;
1402 demand_empty_rest_of_line ();
1406 /* Place an aligned literal fragment at the current location. */
1408 static void
1409 xtensa_literal_position (int ignore ATTRIBUTE_UNUSED)
1411 md_flush_pending_output ();
1413 if (inside_directive (directive_literal))
1414 as_warn (_(".literal_position inside literal directive; ignoring"));
1415 xtensa_mark_literal_pool_location ();
1417 demand_empty_rest_of_line ();
1418 xtensa_clear_insn_labels ();
1422 /* Support .literal label, expr, ... */
1424 static void
1425 xtensa_literal_pseudo (int ignored ATTRIBUTE_UNUSED)
1427 emit_state state;
1428 char *p, *base_name;
1429 char c;
1430 segT dest_seg;
1432 if (inside_directive (directive_literal))
1434 as_bad (_(".literal not allowed inside .begin literal region"));
1435 ignore_rest_of_line ();
1436 return;
1439 md_flush_pending_output ();
1441 /* Previous labels go with whatever follows this directive, not with
1442 the literal, so save them now. */
1443 saved_insn_labels = insn_labels;
1444 insn_labels = NULL;
1446 /* If we are using text-section literals, then this is the right value... */
1447 dest_seg = now_seg;
1449 base_name = input_line_pointer;
1451 xtensa_switch_to_literal_fragment (&state);
1453 /* ...but if we aren't using text-section-literals, then we
1454 need to put them in the section we just switched to. */
1455 if (use_literal_section || directive_state[directive_absolute_literals])
1456 dest_seg = now_seg;
1458 /* All literals are aligned to four-byte boundaries. */
1459 frag_align (2, 0, 0);
1460 record_alignment (now_seg, 2);
1462 c = get_symbol_end ();
1463 /* Just after name is now '\0'. */
1464 p = input_line_pointer;
1465 *p = c;
1466 SKIP_WHITESPACE ();
1468 if (*input_line_pointer != ',' && *input_line_pointer != ':')
1470 as_bad (_("expected comma or colon after symbol name; "
1471 "rest of line ignored"));
1472 ignore_rest_of_line ();
1473 xtensa_restore_emit_state (&state);
1474 return;
1476 *p = 0;
1478 colon (base_name);
1480 *p = c;
1481 input_line_pointer++; /* skip ',' or ':' */
1483 xtensa_elf_cons (4);
1485 xtensa_restore_emit_state (&state);
1487 /* Restore the list of current labels. */
1488 xtensa_clear_insn_labels ();
1489 insn_labels = saved_insn_labels;
1493 static void
1494 xtensa_literal_prefix (void)
1496 char *name;
1497 int len;
1499 /* Parse the new prefix from the input_line_pointer. */
1500 SKIP_WHITESPACE ();
1501 len = strspn (input_line_pointer,
1502 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1503 "abcdefghijklmnopqrstuvwxyz_/0123456789.$");
1505 /* Get a null-terminated copy of the name. */
1506 name = xmalloc (len + 1);
1507 gas_assert (name);
1508 strncpy (name, input_line_pointer, len);
1509 name[len] = 0;
1511 /* Skip the name in the input line. */
1512 input_line_pointer += len;
1514 default_lit_sections.lit_prefix = name;
1516 /* Clear cached literal sections, since the prefix has changed. */
1517 default_lit_sections.lit_seg = NULL;
1518 default_lit_sections.lit4_seg = NULL;
1522 /* Support ".frequency branch_target_frequency fall_through_frequency". */
1524 static void
1525 xtensa_frequency_pseudo (int ignored ATTRIBUTE_UNUSED)
1527 float fall_through_f, target_f;
1529 fall_through_f = (float) strtod (input_line_pointer, &input_line_pointer);
1530 if (fall_through_f < 0)
1532 as_bad (_("fall through frequency must be greater than 0"));
1533 ignore_rest_of_line ();
1534 return;
1537 target_f = (float) strtod (input_line_pointer, &input_line_pointer);
1538 if (target_f < 0)
1540 as_bad (_("branch target frequency must be greater than 0"));
1541 ignore_rest_of_line ();
1542 return;
1545 set_subseg_freq (now_seg, now_subseg, target_f + fall_through_f, target_f);
1547 demand_empty_rest_of_line ();
1551 /* Like normal .long/.short/.word, except support @plt, etc.
1552 Clobbers input_line_pointer, checks end-of-line. */
1554 static void
1555 xtensa_elf_cons (int nbytes)
1557 expressionS exp;
1558 bfd_reloc_code_real_type reloc;
1560 md_flush_pending_output ();
1562 if (cur_vinsn.inside_bundle)
1563 as_bad (_("directives are not valid inside bundles"));
1565 if (is_it_end_of_statement ())
1567 demand_empty_rest_of_line ();
1568 return;
1573 expression (&exp);
1574 if (exp.X_op == O_symbol
1575 && *input_line_pointer == '@'
1576 && ((reloc = xtensa_elf_suffix (&input_line_pointer, &exp))
1577 != BFD_RELOC_NONE))
1579 reloc_howto_type *reloc_howto =
1580 bfd_reloc_type_lookup (stdoutput, reloc);
1582 if (reloc == BFD_RELOC_UNUSED || !reloc_howto)
1583 as_bad (_("unsupported relocation"));
1584 else if ((reloc >= BFD_RELOC_XTENSA_SLOT0_OP
1585 && reloc <= BFD_RELOC_XTENSA_SLOT14_OP)
1586 || (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT
1587 && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT))
1588 as_bad (_("opcode-specific %s relocation used outside "
1589 "an instruction"), reloc_howto->name);
1590 else if (nbytes != (int) bfd_get_reloc_size (reloc_howto))
1591 as_bad (_("%s relocations do not fit in %d bytes"),
1592 reloc_howto->name, nbytes);
1593 else if (reloc == BFD_RELOC_XTENSA_TLS_FUNC
1594 || reloc == BFD_RELOC_XTENSA_TLS_ARG
1595 || reloc == BFD_RELOC_XTENSA_TLS_CALL)
1596 as_bad (_("invalid use of %s relocation"), reloc_howto->name);
1597 else
1599 char *p = frag_more ((int) nbytes);
1600 xtensa_set_frag_assembly_state (frag_now);
1601 fix_new_exp (frag_now, p - frag_now->fr_literal,
1602 nbytes, &exp, reloc_howto->pc_relative, reloc);
1605 else
1607 xtensa_set_frag_assembly_state (frag_now);
1608 emit_expr (&exp, (unsigned int) nbytes);
1611 while (*input_line_pointer++ == ',');
1613 input_line_pointer--; /* Put terminator back into stream. */
1614 demand_empty_rest_of_line ();
1617 static bfd_boolean is_leb128_expr;
1619 static void
1620 xtensa_leb128 (int sign)
1622 is_leb128_expr = TRUE;
1623 s_leb128 (sign);
1624 is_leb128_expr = FALSE;
1628 /* Parsing and Idiom Translation. */
1630 /* Parse @plt, etc. and return the desired relocation. */
1631 static bfd_reloc_code_real_type
1632 xtensa_elf_suffix (char **str_p, expressionS *exp_p)
1634 char ident[20];
1635 char *str = *str_p;
1636 char *str2;
1637 int ch;
1638 int len;
1639 struct suffix_reloc_map *ptr;
1641 if (*str++ != '@')
1642 return BFD_RELOC_NONE;
1644 for (ch = *str, str2 = ident;
1645 (str2 < ident + sizeof (ident) - 1
1646 && (ISALNUM (ch) || ch == '@'));
1647 ch = *++str)
1649 *str2++ = (ISLOWER (ch)) ? ch : TOLOWER (ch);
1652 *str2 = '\0';
1653 len = str2 - ident;
1655 ch = ident[0];
1656 for (ptr = &suffix_relocs[0]; ptr->length > 0; ptr++)
1657 if (ch == ptr->suffix[0]
1658 && len == ptr->length
1659 && memcmp (ident, ptr->suffix, ptr->length) == 0)
1661 /* Now check for "identifier@suffix+constant". */
1662 if (*str == '-' || *str == '+')
1664 char *orig_line = input_line_pointer;
1665 expressionS new_exp;
1667 input_line_pointer = str;
1668 expression (&new_exp);
1669 if (new_exp.X_op == O_constant)
1671 exp_p->X_add_number += new_exp.X_add_number;
1672 str = input_line_pointer;
1675 if (&input_line_pointer != str_p)
1676 input_line_pointer = orig_line;
1679 *str_p = str;
1680 return ptr->reloc;
1683 return BFD_RELOC_UNUSED;
1687 /* Find the matching operator type. */
1688 static unsigned char
1689 map_suffix_reloc_to_operator (bfd_reloc_code_real_type reloc)
1691 struct suffix_reloc_map *sfx;
1692 unsigned char operator = (unsigned char) -1;
1694 for (sfx = &suffix_relocs[0]; sfx->suffix; sfx++)
1696 if (sfx->reloc == reloc)
1698 operator = sfx->operator;
1699 break;
1702 gas_assert (operator != (unsigned char) -1);
1703 return operator;
1707 /* Find the matching reloc type. */
1708 static bfd_reloc_code_real_type
1709 map_operator_to_reloc (unsigned char operator, bfd_boolean is_literal)
1711 struct suffix_reloc_map *sfx;
1712 bfd_reloc_code_real_type reloc = BFD_RELOC_UNUSED;
1714 for (sfx = &suffix_relocs[0]; sfx->suffix; sfx++)
1716 if (sfx->operator == operator)
1718 reloc = sfx->reloc;
1719 break;
1723 if (is_literal)
1725 if (reloc == BFD_RELOC_XTENSA_TLS_FUNC)
1726 return BFD_RELOC_XTENSA_TLSDESC_FN;
1727 else if (reloc == BFD_RELOC_XTENSA_TLS_ARG)
1728 return BFD_RELOC_XTENSA_TLSDESC_ARG;
1731 if (reloc == BFD_RELOC_UNUSED)
1732 return BFD_RELOC_32;
1734 return reloc;
1738 static const char *
1739 expression_end (const char *name)
1741 while (1)
1743 switch (*name)
1745 case '}':
1746 case ';':
1747 case '\0':
1748 case ',':
1749 case ':':
1750 return name;
1751 case ' ':
1752 case '\t':
1753 ++name;
1754 continue;
1755 default:
1756 return 0;
1762 #define ERROR_REG_NUM ((unsigned) -1)
1764 static unsigned
1765 tc_get_register (const char *prefix)
1767 unsigned reg;
1768 const char *next_expr;
1769 const char *old_line_pointer;
1771 SKIP_WHITESPACE ();
1772 old_line_pointer = input_line_pointer;
1774 if (*input_line_pointer == '$')
1775 ++input_line_pointer;
1777 /* Accept "sp" as a synonym for "a1". */
1778 if (input_line_pointer[0] == 's' && input_line_pointer[1] == 'p'
1779 && expression_end (input_line_pointer + 2))
1781 input_line_pointer += 2;
1782 return 1; /* AR[1] */
1785 while (*input_line_pointer++ == *prefix++)
1787 --input_line_pointer;
1788 --prefix;
1790 if (*prefix)
1792 as_bad (_("bad register name: %s"), old_line_pointer);
1793 return ERROR_REG_NUM;
1796 if (!ISDIGIT ((unsigned char) *input_line_pointer))
1798 as_bad (_("bad register number: %s"), input_line_pointer);
1799 return ERROR_REG_NUM;
1802 reg = 0;
1804 while (ISDIGIT ((int) *input_line_pointer))
1805 reg = reg * 10 + *input_line_pointer++ - '0';
1807 if (!(next_expr = expression_end (input_line_pointer)))
1809 as_bad (_("bad register name: %s"), old_line_pointer);
1810 return ERROR_REG_NUM;
1813 input_line_pointer = (char *) next_expr;
1815 return reg;
1819 static void
1820 expression_maybe_register (xtensa_opcode opc, int opnd, expressionS *tok)
1822 xtensa_isa isa = xtensa_default_isa;
1824 /* Check if this is an immediate operand. */
1825 if (xtensa_operand_is_register (isa, opc, opnd) == 0)
1827 bfd_reloc_code_real_type reloc;
1828 segT t = expression (tok);
1830 if (t == absolute_section
1831 && xtensa_operand_is_PCrelative (isa, opc, opnd) == 1)
1833 gas_assert (tok->X_op == O_constant);
1834 tok->X_op = O_symbol;
1835 tok->X_add_symbol = &abs_symbol;
1838 if ((tok->X_op == O_constant || tok->X_op == O_symbol)
1839 && ((reloc = xtensa_elf_suffix (&input_line_pointer, tok))
1840 != BFD_RELOC_NONE))
1842 switch (reloc)
1844 case BFD_RELOC_LO16:
1845 if (tok->X_op == O_constant)
1847 tok->X_add_number &= 0xffff;
1848 return;
1850 break;
1851 case BFD_RELOC_HI16:
1852 if (tok->X_op == O_constant)
1854 tok->X_add_number = ((unsigned) tok->X_add_number) >> 16;
1855 return;
1857 break;
1858 case BFD_RELOC_UNUSED:
1859 as_bad (_("unsupported relocation"));
1860 return;
1861 case BFD_RELOC_32_PCREL:
1862 as_bad (_("pcrel relocation not allowed in an instruction"));
1863 return;
1864 default:
1865 break;
1867 tok->X_op = map_suffix_reloc_to_operator (reloc);
1870 else
1872 xtensa_regfile opnd_rf = xtensa_operand_regfile (isa, opc, opnd);
1873 unsigned reg = tc_get_register (xtensa_regfile_shortname (isa, opnd_rf));
1875 if (reg != ERROR_REG_NUM) /* Already errored */
1877 uint32 buf = reg;
1878 if (xtensa_operand_encode (isa, opc, opnd, &buf))
1879 as_bad (_("register number out of range"));
1882 tok->X_op = O_register;
1883 tok->X_add_symbol = 0;
1884 tok->X_add_number = reg;
1889 /* Split up the arguments for an opcode or pseudo-op. */
1891 static int
1892 tokenize_arguments (char **args, char *str)
1894 char *old_input_line_pointer;
1895 bfd_boolean saw_comma = FALSE;
1896 bfd_boolean saw_arg = FALSE;
1897 bfd_boolean saw_colon = FALSE;
1898 int num_args = 0;
1899 char *arg_end, *arg;
1900 int arg_len;
1902 /* Save and restore input_line_pointer around this function. */
1903 old_input_line_pointer = input_line_pointer;
1904 input_line_pointer = str;
1906 while (*input_line_pointer)
1908 SKIP_WHITESPACE ();
1909 switch (*input_line_pointer)
1911 case '\0':
1912 case '}':
1913 goto fini;
1915 case ':':
1916 input_line_pointer++;
1917 if (saw_comma || saw_colon || !saw_arg)
1918 goto err;
1919 saw_colon = TRUE;
1920 break;
1922 case ',':
1923 input_line_pointer++;
1924 if (saw_comma || saw_colon || !saw_arg)
1925 goto err;
1926 saw_comma = TRUE;
1927 break;
1929 default:
1930 if (!saw_comma && !saw_colon && saw_arg)
1931 goto err;
1933 arg_end = input_line_pointer + 1;
1934 while (!expression_end (arg_end))
1935 arg_end += 1;
1937 arg_len = arg_end - input_line_pointer;
1938 arg = (char *) xmalloc ((saw_colon ? 1 : 0) + arg_len + 1);
1939 args[num_args] = arg;
1941 if (saw_colon)
1942 *arg++ = ':';
1943 strncpy (arg, input_line_pointer, arg_len);
1944 arg[arg_len] = '\0';
1946 input_line_pointer = arg_end;
1947 num_args += 1;
1948 saw_comma = FALSE;
1949 saw_colon = FALSE;
1950 saw_arg = TRUE;
1951 break;
1955 fini:
1956 if (saw_comma || saw_colon)
1957 goto err;
1958 input_line_pointer = old_input_line_pointer;
1959 return num_args;
1961 err:
1962 if (saw_comma)
1963 as_bad (_("extra comma"));
1964 else if (saw_colon)
1965 as_bad (_("extra colon"));
1966 else if (!saw_arg)
1967 as_bad (_("missing argument"));
1968 else
1969 as_bad (_("missing comma or colon"));
1970 input_line_pointer = old_input_line_pointer;
1971 return -1;
1975 /* Parse the arguments to an opcode. Return TRUE on error. */
1977 static bfd_boolean
1978 parse_arguments (TInsn *insn, int num_args, char **arg_strings)
1980 expressionS *tok, *last_tok;
1981 xtensa_opcode opcode = insn->opcode;
1982 bfd_boolean had_error = TRUE;
1983 xtensa_isa isa = xtensa_default_isa;
1984 int n, num_regs = 0;
1985 int opcode_operand_count;
1986 int opnd_cnt, last_opnd_cnt;
1987 unsigned int next_reg = 0;
1988 char *old_input_line_pointer;
1990 if (insn->insn_type == ITYPE_LITERAL)
1991 opcode_operand_count = 1;
1992 else
1993 opcode_operand_count = xtensa_opcode_num_operands (isa, opcode);
1995 tok = insn->tok;
1996 memset (tok, 0, sizeof (*tok) * MAX_INSN_ARGS);
1998 /* Save and restore input_line_pointer around this function. */
1999 old_input_line_pointer = input_line_pointer;
2001 last_tok = 0;
2002 last_opnd_cnt = -1;
2003 opnd_cnt = 0;
2005 /* Skip invisible operands. */
2006 while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0)
2008 opnd_cnt += 1;
2009 tok++;
2012 for (n = 0; n < num_args; n++)
2014 input_line_pointer = arg_strings[n];
2015 if (*input_line_pointer == ':')
2017 xtensa_regfile opnd_rf;
2018 input_line_pointer++;
2019 if (num_regs == 0)
2020 goto err;
2021 gas_assert (opnd_cnt > 0);
2022 num_regs--;
2023 opnd_rf = xtensa_operand_regfile (isa, opcode, last_opnd_cnt);
2024 if (next_reg
2025 != tc_get_register (xtensa_regfile_shortname (isa, opnd_rf)))
2026 as_warn (_("incorrect register number, ignoring"));
2027 next_reg++;
2029 else
2031 if (opnd_cnt >= opcode_operand_count)
2033 as_warn (_("too many arguments"));
2034 goto err;
2036 gas_assert (opnd_cnt < MAX_INSN_ARGS);
2038 expression_maybe_register (opcode, opnd_cnt, tok);
2039 next_reg = tok->X_add_number + 1;
2041 if (tok->X_op == O_illegal || tok->X_op == O_absent)
2042 goto err;
2043 if (xtensa_operand_is_register (isa, opcode, opnd_cnt) == 1)
2045 num_regs = xtensa_operand_num_regs (isa, opcode, opnd_cnt) - 1;
2046 /* minus 1 because we are seeing one right now */
2048 else
2049 num_regs = 0;
2051 last_tok = tok;
2052 last_opnd_cnt = opnd_cnt;
2053 demand_empty_rest_of_line ();
2057 opnd_cnt += 1;
2058 tok++;
2060 while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0);
2064 if (num_regs > 0 && ((int) next_reg != last_tok->X_add_number + 1))
2065 goto err;
2067 insn->ntok = tok - insn->tok;
2068 had_error = FALSE;
2070 err:
2071 input_line_pointer = old_input_line_pointer;
2072 return had_error;
2076 static int
2077 get_invisible_operands (TInsn *insn)
2079 xtensa_isa isa = xtensa_default_isa;
2080 static xtensa_insnbuf slotbuf = NULL;
2081 xtensa_format fmt;
2082 xtensa_opcode opc = insn->opcode;
2083 int slot, opnd, fmt_found;
2084 unsigned val;
2086 if (!slotbuf)
2087 slotbuf = xtensa_insnbuf_alloc (isa);
2089 /* Find format/slot where this can be encoded. */
2090 fmt_found = 0;
2091 slot = 0;
2092 for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
2094 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
2096 if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opc) == 0)
2098 fmt_found = 1;
2099 break;
2102 if (fmt_found) break;
2105 if (!fmt_found)
2107 as_bad (_("cannot encode opcode \"%s\""), xtensa_opcode_name (isa, opc));
2108 return -1;
2111 /* First encode all the visible operands
2112 (to deal with shared field operands). */
2113 for (opnd = 0; opnd < insn->ntok; opnd++)
2115 if (xtensa_operand_is_visible (isa, opc, opnd) == 1
2116 && (insn->tok[opnd].X_op == O_register
2117 || insn->tok[opnd].X_op == O_constant))
2119 val = insn->tok[opnd].X_add_number;
2120 xtensa_operand_encode (isa, opc, opnd, &val);
2121 xtensa_operand_set_field (isa, opc, opnd, fmt, slot, slotbuf, val);
2125 /* Then pull out the values for the invisible ones. */
2126 for (opnd = 0; opnd < insn->ntok; opnd++)
2128 if (xtensa_operand_is_visible (isa, opc, opnd) == 0)
2130 xtensa_operand_get_field (isa, opc, opnd, fmt, slot, slotbuf, &val);
2131 xtensa_operand_decode (isa, opc, opnd, &val);
2132 insn->tok[opnd].X_add_number = val;
2133 if (xtensa_operand_is_register (isa, opc, opnd) == 1)
2134 insn->tok[opnd].X_op = O_register;
2135 else
2136 insn->tok[opnd].X_op = O_constant;
2140 return 0;
2144 static void
2145 xg_reverse_shift_count (char **cnt_argp)
2147 char *cnt_arg, *new_arg;
2148 cnt_arg = *cnt_argp;
2150 /* replace the argument with "31-(argument)" */
2151 new_arg = (char *) xmalloc (strlen (cnt_arg) + 6);
2152 sprintf (new_arg, "31-(%s)", cnt_arg);
2154 free (cnt_arg);
2155 *cnt_argp = new_arg;
2159 /* If "arg" is a constant expression, return non-zero with the value
2160 in *valp. */
2162 static int
2163 xg_arg_is_constant (char *arg, offsetT *valp)
2165 expressionS exp;
2166 char *save_ptr = input_line_pointer;
2168 input_line_pointer = arg;
2169 expression (&exp);
2170 input_line_pointer = save_ptr;
2172 if (exp.X_op == O_constant)
2174 *valp = exp.X_add_number;
2175 return 1;
2178 return 0;
2182 static void
2183 xg_replace_opname (char **popname, char *newop)
2185 free (*popname);
2186 *popname = (char *) xmalloc (strlen (newop) + 1);
2187 strcpy (*popname, newop);
2191 static int
2192 xg_check_num_args (int *pnum_args,
2193 int expected_num,
2194 char *opname,
2195 char **arg_strings)
2197 int num_args = *pnum_args;
2199 if (num_args < expected_num)
2201 as_bad (_("not enough operands (%d) for '%s'; expected %d"),
2202 num_args, opname, expected_num);
2203 return -1;
2206 if (num_args > expected_num)
2208 as_warn (_("too many operands (%d) for '%s'; expected %d"),
2209 num_args, opname, expected_num);
2210 while (num_args-- > expected_num)
2212 free (arg_strings[num_args]);
2213 arg_strings[num_args] = 0;
2215 *pnum_args = expected_num;
2216 return -1;
2219 return 0;
2223 /* If the register is not specified as part of the opcode,
2224 then get it from the operand and move it to the opcode. */
2226 static int
2227 xg_translate_sysreg_op (char **popname, int *pnum_args, char **arg_strings)
2229 xtensa_isa isa = xtensa_default_isa;
2230 xtensa_sysreg sr;
2231 char *opname, *new_opname;
2232 const char *sr_name;
2233 int is_user, is_write;
2235 opname = *popname;
2236 if (*opname == '_')
2237 opname += 1;
2238 is_user = (opname[1] == 'u');
2239 is_write = (opname[0] == 'w');
2241 /* Opname == [rw]ur or [rwx]sr... */
2243 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
2244 return -1;
2246 /* Check if the argument is a symbolic register name. */
2247 sr = xtensa_sysreg_lookup_name (isa, arg_strings[1]);
2248 /* Handle WSR to "INTSET" as a special case. */
2249 if (sr == XTENSA_UNDEFINED && is_write && !is_user
2250 && !strcasecmp (arg_strings[1], "intset"))
2251 sr = xtensa_sysreg_lookup_name (isa, "interrupt");
2252 if (sr == XTENSA_UNDEFINED
2253 || (xtensa_sysreg_is_user (isa, sr) == 1) != is_user)
2255 /* Maybe it's a register number.... */
2256 offsetT val;
2257 if (!xg_arg_is_constant (arg_strings[1], &val))
2259 as_bad (_("invalid register '%s' for '%s' instruction"),
2260 arg_strings[1], opname);
2261 return -1;
2263 sr = xtensa_sysreg_lookup (isa, val, is_user);
2264 if (sr == XTENSA_UNDEFINED)
2266 as_bad (_("invalid register number (%ld) for '%s' instruction"),
2267 (long) val, opname);
2268 return -1;
2272 /* Remove the last argument, which is now part of the opcode. */
2273 free (arg_strings[1]);
2274 arg_strings[1] = 0;
2275 *pnum_args = 1;
2277 /* Translate the opcode. */
2278 sr_name = xtensa_sysreg_name (isa, sr);
2279 /* Another special case for "WSR.INTSET".... */
2280 if (is_write && !is_user && !strcasecmp ("interrupt", sr_name))
2281 sr_name = "intset";
2282 new_opname = (char *) xmalloc (strlen (sr_name) + 6);
2283 sprintf (new_opname, "%s.%s", *popname, sr_name);
2284 free (*popname);
2285 *popname = new_opname;
2287 return 0;
2291 static int
2292 xtensa_translate_old_userreg_ops (char **popname)
2294 xtensa_isa isa = xtensa_default_isa;
2295 xtensa_sysreg sr;
2296 char *opname, *new_opname;
2297 const char *sr_name;
2298 bfd_boolean has_underbar = FALSE;
2300 opname = *popname;
2301 if (opname[0] == '_')
2303 has_underbar = TRUE;
2304 opname += 1;
2307 sr = xtensa_sysreg_lookup_name (isa, opname + 1);
2308 if (sr != XTENSA_UNDEFINED)
2310 /* The new default name ("nnn") is different from the old default
2311 name ("URnnn"). The old default is handled below, and we don't
2312 want to recognize [RW]nnn, so do nothing if the name is the (new)
2313 default. */
2314 static char namebuf[10];
2315 sprintf (namebuf, "%d", xtensa_sysreg_number (isa, sr));
2316 if (strcmp (namebuf, opname + 1) == 0)
2317 return 0;
2319 else
2321 offsetT val;
2322 char *end;
2324 /* Only continue if the reg name is "URnnn". */
2325 if (opname[1] != 'u' || opname[2] != 'r')
2326 return 0;
2327 val = strtoul (opname + 3, &end, 10);
2328 if (*end != '\0')
2329 return 0;
2331 sr = xtensa_sysreg_lookup (isa, val, 1);
2332 if (sr == XTENSA_UNDEFINED)
2334 as_bad (_("invalid register number (%ld) for '%s'"),
2335 (long) val, opname);
2336 return -1;
2340 /* Translate the opcode. */
2341 sr_name = xtensa_sysreg_name (isa, sr);
2342 new_opname = (char *) xmalloc (strlen (sr_name) + 6);
2343 sprintf (new_opname, "%s%cur.%s", (has_underbar ? "_" : ""),
2344 opname[0], sr_name);
2345 free (*popname);
2346 *popname = new_opname;
2348 return 0;
2352 static int
2353 xtensa_translate_zero_immed (char *old_op,
2354 char *new_op,
2355 char **popname,
2356 int *pnum_args,
2357 char **arg_strings)
2359 char *opname;
2360 offsetT val;
2362 opname = *popname;
2363 gas_assert (opname[0] != '_');
2365 if (strcmp (opname, old_op) != 0)
2366 return 0;
2368 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2369 return -1;
2370 if (xg_arg_is_constant (arg_strings[1], &val) && val == 0)
2372 xg_replace_opname (popname, new_op);
2373 free (arg_strings[1]);
2374 arg_strings[1] = arg_strings[2];
2375 arg_strings[2] = 0;
2376 *pnum_args = 2;
2379 return 0;
2383 /* If the instruction is an idiom (i.e., a built-in macro), translate it.
2384 Returns non-zero if an error was found. */
2386 static int
2387 xg_translate_idioms (char **popname, int *pnum_args, char **arg_strings)
2389 char *opname = *popname;
2390 bfd_boolean has_underbar = FALSE;
2392 if (*opname == '_')
2394 has_underbar = TRUE;
2395 opname += 1;
2398 if (strcmp (opname, "mov") == 0)
2400 if (use_transform () && !has_underbar && density_supported)
2401 xg_replace_opname (popname, "mov.n");
2402 else
2404 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
2405 return -1;
2406 xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
2407 arg_strings[2] = (char *) xmalloc (strlen (arg_strings[1]) + 1);
2408 strcpy (arg_strings[2], arg_strings[1]);
2409 *pnum_args = 3;
2411 return 0;
2414 if (strcmp (opname, "bbsi.l") == 0)
2416 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2417 return -1;
2418 xg_replace_opname (popname, (has_underbar ? "_bbsi" : "bbsi"));
2419 if (target_big_endian)
2420 xg_reverse_shift_count (&arg_strings[1]);
2421 return 0;
2424 if (strcmp (opname, "bbci.l") == 0)
2426 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2427 return -1;
2428 xg_replace_opname (popname, (has_underbar ? "_bbci" : "bbci"));
2429 if (target_big_endian)
2430 xg_reverse_shift_count (&arg_strings[1]);
2431 return 0;
2434 /* Don't do anything special with NOPs inside FLIX instructions. They
2435 are handled elsewhere. Real NOP instructions are always available
2436 in configurations with FLIX, so this should never be an issue but
2437 check for it anyway. */
2438 if (!cur_vinsn.inside_bundle && xtensa_nop_opcode == XTENSA_UNDEFINED
2439 && strcmp (opname, "nop") == 0)
2441 if (use_transform () && !has_underbar && density_supported)
2442 xg_replace_opname (popname, "nop.n");
2443 else
2445 if (xg_check_num_args (pnum_args, 0, opname, arg_strings))
2446 return -1;
2447 xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
2448 arg_strings[0] = (char *) xmalloc (3);
2449 arg_strings[1] = (char *) xmalloc (3);
2450 arg_strings[2] = (char *) xmalloc (3);
2451 strcpy (arg_strings[0], "a1");
2452 strcpy (arg_strings[1], "a1");
2453 strcpy (arg_strings[2], "a1");
2454 *pnum_args = 3;
2456 return 0;
2459 /* Recognize [RW]UR and [RWX]SR. */
2460 if ((((opname[0] == 'r' || opname[0] == 'w')
2461 && (opname[1] == 'u' || opname[1] == 's'))
2462 || (opname[0] == 'x' && opname[1] == 's'))
2463 && opname[2] == 'r'
2464 && opname[3] == '\0')
2465 return xg_translate_sysreg_op (popname, pnum_args, arg_strings);
2467 /* Backward compatibility for RUR and WUR: Recognize [RW]UR<nnn> and
2468 [RW]<name> if <name> is the non-default name of a user register. */
2469 if ((opname[0] == 'r' || opname[0] == 'w')
2470 && xtensa_opcode_lookup (xtensa_default_isa, opname) == XTENSA_UNDEFINED)
2471 return xtensa_translate_old_userreg_ops (popname);
2473 /* Relax branches that don't allow comparisons against an immediate value
2474 of zero to the corresponding branches with implicit zero immediates. */
2475 if (!has_underbar && use_transform ())
2477 if (xtensa_translate_zero_immed ("bnei", "bnez", popname,
2478 pnum_args, arg_strings))
2479 return -1;
2481 if (xtensa_translate_zero_immed ("beqi", "beqz", popname,
2482 pnum_args, arg_strings))
2483 return -1;
2485 if (xtensa_translate_zero_immed ("bgei", "bgez", popname,
2486 pnum_args, arg_strings))
2487 return -1;
2489 if (xtensa_translate_zero_immed ("blti", "bltz", popname,
2490 pnum_args, arg_strings))
2491 return -1;
2494 return 0;
2498 /* Functions for dealing with the Xtensa ISA. */
2500 /* Currently the assembler only allows us to use a single target per
2501 fragment. Because of this, only one operand for a given
2502 instruction may be symbolic. If there is a PC-relative operand,
2503 the last one is chosen. Otherwise, the result is the number of the
2504 last immediate operand, and if there are none of those, we fail and
2505 return -1. */
2507 static int
2508 get_relaxable_immed (xtensa_opcode opcode)
2510 int last_immed = -1;
2511 int noperands, opi;
2513 if (opcode == XTENSA_UNDEFINED)
2514 return -1;
2516 noperands = xtensa_opcode_num_operands (xtensa_default_isa, opcode);
2517 for (opi = noperands - 1; opi >= 0; opi--)
2519 if (xtensa_operand_is_visible (xtensa_default_isa, opcode, opi) == 0)
2520 continue;
2521 if (xtensa_operand_is_PCrelative (xtensa_default_isa, opcode, opi) == 1)
2522 return opi;
2523 if (last_immed == -1
2524 && xtensa_operand_is_register (xtensa_default_isa, opcode, opi) == 0)
2525 last_immed = opi;
2527 return last_immed;
2531 static xtensa_opcode
2532 get_opcode_from_buf (const char *buf, int slot)
2534 static xtensa_insnbuf insnbuf = NULL;
2535 static xtensa_insnbuf slotbuf = NULL;
2536 xtensa_isa isa = xtensa_default_isa;
2537 xtensa_format fmt;
2539 if (!insnbuf)
2541 insnbuf = xtensa_insnbuf_alloc (isa);
2542 slotbuf = xtensa_insnbuf_alloc (isa);
2545 xtensa_insnbuf_from_chars (isa, insnbuf, (const unsigned char *) buf, 0);
2546 fmt = xtensa_format_decode (isa, insnbuf);
2547 if (fmt == XTENSA_UNDEFINED)
2548 return XTENSA_UNDEFINED;
2550 if (slot >= xtensa_format_num_slots (isa, fmt))
2551 return XTENSA_UNDEFINED;
2553 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
2554 return xtensa_opcode_decode (isa, fmt, slot, slotbuf);
2558 #ifdef TENSILICA_DEBUG
2560 /* For debugging, print out the mapping of opcode numbers to opcodes. */
2562 static void
2563 xtensa_print_insn_table (void)
2565 int num_opcodes, num_operands;
2566 xtensa_opcode opcode;
2567 xtensa_isa isa = xtensa_default_isa;
2569 num_opcodes = xtensa_isa_num_opcodes (xtensa_default_isa);
2570 for (opcode = 0; opcode < num_opcodes; opcode++)
2572 int opn;
2573 fprintf (stderr, "%d: %s: ", opcode, xtensa_opcode_name (isa, opcode));
2574 num_operands = xtensa_opcode_num_operands (isa, opcode);
2575 for (opn = 0; opn < num_operands; opn++)
2577 if (xtensa_operand_is_visible (isa, opcode, opn) == 0)
2578 continue;
2579 if (xtensa_operand_is_register (isa, opcode, opn) == 1)
2581 xtensa_regfile opnd_rf =
2582 xtensa_operand_regfile (isa, opcode, opn);
2583 fprintf (stderr, "%s ", xtensa_regfile_shortname (isa, opnd_rf));
2585 else if (xtensa_operand_is_PCrelative (isa, opcode, opn) == 1)
2586 fputs ("[lLr] ", stderr);
2587 else
2588 fputs ("i ", stderr);
2590 fprintf (stderr, "\n");
2595 static void
2596 print_vliw_insn (xtensa_insnbuf vbuf)
2598 xtensa_isa isa = xtensa_default_isa;
2599 xtensa_format f = xtensa_format_decode (isa, vbuf);
2600 xtensa_insnbuf sbuf = xtensa_insnbuf_alloc (isa);
2601 int op;
2603 fprintf (stderr, "format = %d\n", f);
2605 for (op = 0; op < xtensa_format_num_slots (isa, f); op++)
2607 xtensa_opcode opcode;
2608 const char *opname;
2609 int operands;
2611 xtensa_format_get_slot (isa, f, op, vbuf, sbuf);
2612 opcode = xtensa_opcode_decode (isa, f, op, sbuf);
2613 opname = xtensa_opcode_name (isa, opcode);
2615 fprintf (stderr, "op in slot %i is %s;\n", op, opname);
2616 fprintf (stderr, " operands = ");
2617 for (operands = 0;
2618 operands < xtensa_opcode_num_operands (isa, opcode);
2619 operands++)
2621 unsigned int val;
2622 if (xtensa_operand_is_visible (isa, opcode, operands) == 0)
2623 continue;
2624 xtensa_operand_get_field (isa, opcode, operands, f, op, sbuf, &val);
2625 xtensa_operand_decode (isa, opcode, operands, &val);
2626 fprintf (stderr, "%d ", val);
2628 fprintf (stderr, "\n");
2630 xtensa_insnbuf_free (isa, sbuf);
2633 #endif /* TENSILICA_DEBUG */
2636 static bfd_boolean
2637 is_direct_call_opcode (xtensa_opcode opcode)
2639 xtensa_isa isa = xtensa_default_isa;
2640 int n, num_operands;
2642 if (xtensa_opcode_is_call (isa, opcode) != 1)
2643 return FALSE;
2645 num_operands = xtensa_opcode_num_operands (isa, opcode);
2646 for (n = 0; n < num_operands; n++)
2648 if (xtensa_operand_is_register (isa, opcode, n) == 0
2649 && xtensa_operand_is_PCrelative (isa, opcode, n) == 1)
2650 return TRUE;
2652 return FALSE;
2656 /* Convert from BFD relocation type code to slot and operand number.
2657 Returns non-zero on failure. */
2659 static int
2660 decode_reloc (bfd_reloc_code_real_type reloc, int *slot, bfd_boolean *is_alt)
2662 if (reloc >= BFD_RELOC_XTENSA_SLOT0_OP
2663 && reloc <= BFD_RELOC_XTENSA_SLOT14_OP)
2665 *slot = reloc - BFD_RELOC_XTENSA_SLOT0_OP;
2666 *is_alt = FALSE;
2668 else if (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT
2669 && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT)
2671 *slot = reloc - BFD_RELOC_XTENSA_SLOT0_ALT;
2672 *is_alt = TRUE;
2674 else
2675 return -1;
2677 return 0;
2681 /* Convert from slot number to BFD relocation type code for the
2682 standard PC-relative relocations. Return BFD_RELOC_NONE on
2683 failure. */
2685 static bfd_reloc_code_real_type
2686 encode_reloc (int slot)
2688 if (slot < 0 || slot > 14)
2689 return BFD_RELOC_NONE;
2691 return BFD_RELOC_XTENSA_SLOT0_OP + slot;
2695 /* Convert from slot numbers to BFD relocation type code for the
2696 "alternate" relocations. Return BFD_RELOC_NONE on failure. */
2698 static bfd_reloc_code_real_type
2699 encode_alt_reloc (int slot)
2701 if (slot < 0 || slot > 14)
2702 return BFD_RELOC_NONE;
2704 return BFD_RELOC_XTENSA_SLOT0_ALT + slot;
2708 static void
2709 xtensa_insnbuf_set_operand (xtensa_insnbuf slotbuf,
2710 xtensa_format fmt,
2711 int slot,
2712 xtensa_opcode opcode,
2713 int operand,
2714 uint32 value,
2715 const char *file,
2716 unsigned int line)
2718 uint32 valbuf = value;
2720 if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf))
2722 if (xtensa_operand_is_PCrelative (xtensa_default_isa, opcode, operand)
2723 == 1)
2724 as_bad_where ((char *) file, line,
2725 _("operand %d of '%s' has out of range value '%u'"),
2726 operand + 1,
2727 xtensa_opcode_name (xtensa_default_isa, opcode),
2728 value);
2729 else
2730 as_bad_where ((char *) file, line,
2731 _("operand %d of '%s' has invalid value '%u'"),
2732 operand + 1,
2733 xtensa_opcode_name (xtensa_default_isa, opcode),
2734 value);
2735 return;
2738 xtensa_operand_set_field (xtensa_default_isa, opcode, operand, fmt, slot,
2739 slotbuf, valbuf);
2743 static uint32
2744 xtensa_insnbuf_get_operand (xtensa_insnbuf slotbuf,
2745 xtensa_format fmt,
2746 int slot,
2747 xtensa_opcode opcode,
2748 int opnum)
2750 uint32 val = 0;
2751 (void) xtensa_operand_get_field (xtensa_default_isa, opcode, opnum,
2752 fmt, slot, slotbuf, &val);
2753 (void) xtensa_operand_decode (xtensa_default_isa, opcode, opnum, &val);
2754 return val;
2758 /* Checks for rules from xtensa-relax tables. */
2760 /* The routine xg_instruction_matches_option_term must return TRUE
2761 when a given option term is true. The meaning of all of the option
2762 terms is given interpretation by this function. */
2764 static bfd_boolean
2765 xg_instruction_matches_option_term (TInsn *insn, const ReqOrOption *option)
2767 if (strcmp (option->option_name, "realnop") == 0
2768 || strncmp (option->option_name, "IsaUse", 6) == 0)
2770 /* These conditions were evaluated statically when building the
2771 relaxation table. There's no need to reevaluate them now. */
2772 return TRUE;
2774 else if (strcmp (option->option_name, "FREEREG") == 0)
2775 return insn->extra_arg.X_op == O_register;
2776 else
2778 as_fatal (_("internal error: unknown option name '%s'"),
2779 option->option_name);
2784 static bfd_boolean
2785 xg_instruction_matches_or_options (TInsn *insn,
2786 const ReqOrOptionList *or_option)
2788 const ReqOrOption *option;
2789 /* Must match each of the AND terms. */
2790 for (option = or_option; option != NULL; option = option->next)
2792 if (xg_instruction_matches_option_term (insn, option))
2793 return TRUE;
2795 return FALSE;
2799 static bfd_boolean
2800 xg_instruction_matches_options (TInsn *insn, const ReqOptionList *options)
2802 const ReqOption *req_options;
2803 /* Must match each of the AND terms. */
2804 for (req_options = options;
2805 req_options != NULL;
2806 req_options = req_options->next)
2808 /* Must match one of the OR clauses. */
2809 if (!xg_instruction_matches_or_options (insn,
2810 req_options->or_option_terms))
2811 return FALSE;
2813 return TRUE;
2817 /* Return the transition rule that matches or NULL if none matches. */
2819 static bfd_boolean
2820 xg_instruction_matches_rule (TInsn *insn, TransitionRule *rule)
2822 PreconditionList *condition_l;
2824 if (rule->opcode != insn->opcode)
2825 return FALSE;
2827 for (condition_l = rule->conditions;
2828 condition_l != NULL;
2829 condition_l = condition_l->next)
2831 expressionS *exp1;
2832 expressionS *exp2;
2833 Precondition *cond = condition_l->precond;
2835 switch (cond->typ)
2837 case OP_CONSTANT:
2838 /* The expression must be the constant. */
2839 gas_assert (cond->op_num < insn->ntok);
2840 exp1 = &insn->tok[cond->op_num];
2841 if (expr_is_const (exp1))
2843 switch (cond->cmp)
2845 case OP_EQUAL:
2846 if (get_expr_const (exp1) != cond->op_data)
2847 return FALSE;
2848 break;
2849 case OP_NOTEQUAL:
2850 if (get_expr_const (exp1) == cond->op_data)
2851 return FALSE;
2852 break;
2853 default:
2854 return FALSE;
2857 else if (expr_is_register (exp1))
2859 switch (cond->cmp)
2861 case OP_EQUAL:
2862 if (get_expr_register (exp1) != cond->op_data)
2863 return FALSE;
2864 break;
2865 case OP_NOTEQUAL:
2866 if (get_expr_register (exp1) == cond->op_data)
2867 return FALSE;
2868 break;
2869 default:
2870 return FALSE;
2873 else
2874 return FALSE;
2875 break;
2877 case OP_OPERAND:
2878 gas_assert (cond->op_num < insn->ntok);
2879 gas_assert (cond->op_data < insn->ntok);
2880 exp1 = &insn->tok[cond->op_num];
2881 exp2 = &insn->tok[cond->op_data];
2883 switch (cond->cmp)
2885 case OP_EQUAL:
2886 if (!expr_is_equal (exp1, exp2))
2887 return FALSE;
2888 break;
2889 case OP_NOTEQUAL:
2890 if (expr_is_equal (exp1, exp2))
2891 return FALSE;
2892 break;
2894 break;
2896 case OP_LITERAL:
2897 case OP_LABEL:
2898 default:
2899 return FALSE;
2902 if (!xg_instruction_matches_options (insn, rule->options))
2903 return FALSE;
2905 return TRUE;
2909 static int
2910 transition_rule_cmp (const TransitionRule *a, const TransitionRule *b)
2912 bfd_boolean a_greater = FALSE;
2913 bfd_boolean b_greater = FALSE;
2915 ReqOptionList *l_a = a->options;
2916 ReqOptionList *l_b = b->options;
2918 /* We only care if they both are the same except for
2919 a const16 vs. an l32r. */
2921 while (l_a && l_b && ((l_a->next == NULL) == (l_b->next == NULL)))
2923 ReqOrOptionList *l_or_a = l_a->or_option_terms;
2924 ReqOrOptionList *l_or_b = l_b->or_option_terms;
2925 while (l_or_a && l_or_b && ((l_a->next == NULL) == (l_b->next == NULL)))
2927 if (l_or_a->is_true != l_or_b->is_true)
2928 return 0;
2929 if (strcmp (l_or_a->option_name, l_or_b->option_name) != 0)
2931 /* This is the case we care about. */
2932 if (strcmp (l_or_a->option_name, "IsaUseConst16") == 0
2933 && strcmp (l_or_b->option_name, "IsaUseL32R") == 0)
2935 if (prefer_const16)
2936 a_greater = TRUE;
2937 else
2938 b_greater = TRUE;
2940 else if (strcmp (l_or_a->option_name, "IsaUseL32R") == 0
2941 && strcmp (l_or_b->option_name, "IsaUseConst16") == 0)
2943 if (prefer_const16)
2944 b_greater = TRUE;
2945 else
2946 a_greater = TRUE;
2948 else
2949 return 0;
2951 l_or_a = l_or_a->next;
2952 l_or_b = l_or_b->next;
2954 if (l_or_a || l_or_b)
2955 return 0;
2957 l_a = l_a->next;
2958 l_b = l_b->next;
2960 if (l_a || l_b)
2961 return 0;
2963 /* Incomparable if the substitution was used differently in two cases. */
2964 if (a_greater && b_greater)
2965 return 0;
2967 if (b_greater)
2968 return 1;
2969 if (a_greater)
2970 return -1;
2972 return 0;
2976 static TransitionRule *
2977 xg_instruction_match (TInsn *insn)
2979 TransitionTable *table = xg_build_simplify_table (&transition_rule_cmp);
2980 TransitionList *l;
2981 gas_assert (insn->opcode < table->num_opcodes);
2983 /* Walk through all of the possible transitions. */
2984 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
2986 TransitionRule *rule = l->rule;
2987 if (xg_instruction_matches_rule (insn, rule))
2988 return rule;
2990 return NULL;
2994 /* Various Other Internal Functions. */
2996 static bfd_boolean
2997 is_unique_insn_expansion (TransitionRule *r)
2999 if (!r->to_instr || r->to_instr->next != NULL)
3000 return FALSE;
3001 if (r->to_instr->typ != INSTR_INSTR)
3002 return FALSE;
3003 return TRUE;
3007 /* Check if there is exactly one relaxation for INSN that converts it to
3008 another instruction of equal or larger size. If so, and if TARG is
3009 non-null, go ahead and generate the relaxed instruction into TARG. If
3010 NARROW_ONLY is true, then only consider relaxations that widen a narrow
3011 instruction, i.e., ignore relaxations that convert to an instruction of
3012 equal size. In some contexts where this function is used, only
3013 a single widening is allowed and the NARROW_ONLY argument is used to
3014 exclude cases like ADDI being "widened" to an ADDMI, which may
3015 later be relaxed to an ADDMI/ADDI pair. */
3017 bfd_boolean
3018 xg_is_single_relaxable_insn (TInsn *insn, TInsn *targ, bfd_boolean narrow_only)
3020 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3021 TransitionList *l;
3022 TransitionRule *match = 0;
3024 gas_assert (insn->insn_type == ITYPE_INSN);
3025 gas_assert (insn->opcode < table->num_opcodes);
3027 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3029 TransitionRule *rule = l->rule;
3031 if (xg_instruction_matches_rule (insn, rule)
3032 && is_unique_insn_expansion (rule)
3033 && (xg_get_single_size (insn->opcode) + (narrow_only ? 1 : 0)
3034 <= xg_get_single_size (rule->to_instr->opcode)))
3036 if (match)
3037 return FALSE;
3038 match = rule;
3041 if (!match)
3042 return FALSE;
3044 if (targ)
3045 xg_build_to_insn (targ, insn, match->to_instr);
3046 return TRUE;
3050 /* Return the maximum number of bytes this opcode can expand to. */
3052 static int
3053 xg_get_max_insn_widen_size (xtensa_opcode opcode)
3055 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3056 TransitionList *l;
3057 int max_size = xg_get_single_size (opcode);
3059 gas_assert (opcode < table->num_opcodes);
3061 for (l = table->table[opcode]; l != NULL; l = l->next)
3063 TransitionRule *rule = l->rule;
3064 BuildInstr *build_list;
3065 int this_size = 0;
3067 if (!rule)
3068 continue;
3069 build_list = rule->to_instr;
3070 if (is_unique_insn_expansion (rule))
3072 gas_assert (build_list->typ == INSTR_INSTR);
3073 this_size = xg_get_max_insn_widen_size (build_list->opcode);
3075 else
3076 for (; build_list != NULL; build_list = build_list->next)
3078 switch (build_list->typ)
3080 case INSTR_INSTR:
3081 this_size += xg_get_single_size (build_list->opcode);
3082 break;
3083 case INSTR_LITERAL_DEF:
3084 case INSTR_LABEL_DEF:
3085 default:
3086 break;
3089 if (this_size > max_size)
3090 max_size = this_size;
3092 return max_size;
3096 /* Return the maximum number of literal bytes this opcode can generate. */
3098 static int
3099 xg_get_max_insn_widen_literal_size (xtensa_opcode opcode)
3101 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3102 TransitionList *l;
3103 int max_size = 0;
3105 gas_assert (opcode < table->num_opcodes);
3107 for (l = table->table[opcode]; l != NULL; l = l->next)
3109 TransitionRule *rule = l->rule;
3110 BuildInstr *build_list;
3111 int this_size = 0;
3113 if (!rule)
3114 continue;
3115 build_list = rule->to_instr;
3116 if (is_unique_insn_expansion (rule))
3118 gas_assert (build_list->typ == INSTR_INSTR);
3119 this_size = xg_get_max_insn_widen_literal_size (build_list->opcode);
3121 else
3122 for (; build_list != NULL; build_list = build_list->next)
3124 switch (build_list->typ)
3126 case INSTR_LITERAL_DEF:
3127 /* Hard-coded 4-byte literal. */
3128 this_size += 4;
3129 break;
3130 case INSTR_INSTR:
3131 case INSTR_LABEL_DEF:
3132 default:
3133 break;
3136 if (this_size > max_size)
3137 max_size = this_size;
3139 return max_size;
3143 static bfd_boolean
3144 xg_is_relaxable_insn (TInsn *insn, int lateral_steps)
3146 int steps_taken = 0;
3147 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3148 TransitionList *l;
3150 gas_assert (insn->insn_type == ITYPE_INSN);
3151 gas_assert (insn->opcode < table->num_opcodes);
3153 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3155 TransitionRule *rule = l->rule;
3157 if (xg_instruction_matches_rule (insn, rule))
3159 if (steps_taken == lateral_steps)
3160 return TRUE;
3161 steps_taken++;
3164 return FALSE;
3168 static symbolS *
3169 get_special_literal_symbol (void)
3171 static symbolS *sym = NULL;
3173 if (sym == NULL)
3174 sym = symbol_find_or_make ("SPECIAL_LITERAL0\001");
3175 return sym;
3179 static symbolS *
3180 get_special_label_symbol (void)
3182 static symbolS *sym = NULL;
3184 if (sym == NULL)
3185 sym = symbol_find_or_make ("SPECIAL_LABEL0\001");
3186 return sym;
3190 static bfd_boolean
3191 xg_valid_literal_expression (const expressionS *exp)
3193 switch (exp->X_op)
3195 case O_constant:
3196 case O_symbol:
3197 case O_big:
3198 case O_uminus:
3199 case O_subtract:
3200 case O_pltrel:
3201 case O_pcrel:
3202 case O_tlsfunc:
3203 case O_tlsarg:
3204 case O_tpoff:
3205 case O_dtpoff:
3206 return TRUE;
3207 default:
3208 return FALSE;
3213 /* This will check to see if the value can be converted into the
3214 operand type. It will return TRUE if it does not fit. */
3216 static bfd_boolean
3217 xg_check_operand (int32 value, xtensa_opcode opcode, int operand)
3219 uint32 valbuf = value;
3220 if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf))
3221 return TRUE;
3222 return FALSE;
3226 /* Assumes: All immeds are constants. Check that all constants fit
3227 into their immeds; return FALSE if not. */
3229 static bfd_boolean
3230 xg_immeds_fit (const TInsn *insn)
3232 xtensa_isa isa = xtensa_default_isa;
3233 int i;
3235 int n = insn->ntok;
3236 gas_assert (insn->insn_type == ITYPE_INSN);
3237 for (i = 0; i < n; ++i)
3239 const expressionS *exp = &insn->tok[i];
3241 if (xtensa_operand_is_register (isa, insn->opcode, i) == 1)
3242 continue;
3244 switch (exp->X_op)
3246 case O_register:
3247 case O_constant:
3248 if (xg_check_operand (exp->X_add_number, insn->opcode, i))
3249 return FALSE;
3250 break;
3252 default:
3253 /* The symbol should have a fixup associated with it. */
3254 gas_assert (FALSE);
3255 break;
3258 return TRUE;
3262 /* This should only be called after we have an initial
3263 estimate of the addresses. */
3265 static bfd_boolean
3266 xg_symbolic_immeds_fit (const TInsn *insn,
3267 segT pc_seg,
3268 fragS *pc_frag,
3269 offsetT pc_offset,
3270 long stretch)
3272 xtensa_isa isa = xtensa_default_isa;
3273 symbolS *symbolP;
3274 fragS *sym_frag;
3275 offsetT target, pc;
3276 uint32 new_offset;
3277 int i;
3278 int n = insn->ntok;
3280 gas_assert (insn->insn_type == ITYPE_INSN);
3282 for (i = 0; i < n; ++i)
3284 const expressionS *exp = &insn->tok[i];
3286 if (xtensa_operand_is_register (isa, insn->opcode, i) == 1)
3287 continue;
3289 switch (exp->X_op)
3291 case O_register:
3292 case O_constant:
3293 if (xg_check_operand (exp->X_add_number, insn->opcode, i))
3294 return FALSE;
3295 break;
3297 case O_lo16:
3298 case O_hi16:
3299 /* Check for the worst case. */
3300 if (xg_check_operand (0xffff, insn->opcode, i))
3301 return FALSE;
3302 break;
3304 case O_symbol:
3305 /* We only allow symbols for PC-relative references.
3306 If pc_frag == 0, then we don't have frag locations yet. */
3307 if (pc_frag == 0
3308 || xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 0)
3309 return FALSE;
3311 /* If it is a weak symbol or a symbol in a different section,
3312 it cannot be known to fit at assembly time. */
3313 if (S_IS_WEAK (exp->X_add_symbol)
3314 || S_GET_SEGMENT (exp->X_add_symbol) != pc_seg)
3316 /* For a direct call with --no-longcalls, be optimistic and
3317 assume it will be in range. If the symbol is weak and
3318 undefined, it may remain undefined at link-time, in which
3319 case it will have a zero value and almost certainly be out
3320 of range for a direct call; thus, relax for undefined weak
3321 symbols even if longcalls is not enabled. */
3322 if (is_direct_call_opcode (insn->opcode)
3323 && ! pc_frag->tc_frag_data.use_longcalls
3324 && (! S_IS_WEAK (exp->X_add_symbol)
3325 || S_IS_DEFINED (exp->X_add_symbol)))
3326 return TRUE;
3328 return FALSE;
3331 symbolP = exp->X_add_symbol;
3332 sym_frag = symbol_get_frag (symbolP);
3333 target = S_GET_VALUE (symbolP) + exp->X_add_number;
3334 pc = pc_frag->fr_address + pc_offset;
3336 /* If frag has yet to be reached on this pass, assume it
3337 will move by STRETCH just as we did. If this is not so,
3338 it will be because some frag between grows, and that will
3339 force another pass. Beware zero-length frags. There
3340 should be a faster way to do this. */
3342 if (stretch != 0
3343 && sym_frag->relax_marker != pc_frag->relax_marker
3344 && S_GET_SEGMENT (symbolP) == pc_seg)
3346 target += stretch;
3349 new_offset = target;
3350 xtensa_operand_do_reloc (isa, insn->opcode, i, &new_offset, pc);
3351 if (xg_check_operand (new_offset, insn->opcode, i))
3352 return FALSE;
3353 break;
3355 default:
3356 /* The symbol should have a fixup associated with it. */
3357 return FALSE;
3361 return TRUE;
3365 /* Return TRUE on success. */
3367 static bfd_boolean
3368 xg_build_to_insn (TInsn *targ, TInsn *insn, BuildInstr *bi)
3370 BuildOp *op;
3371 symbolS *sym;
3373 tinsn_init (targ);
3374 targ->debug_line = insn->debug_line;
3375 targ->loc_directive_seen = insn->loc_directive_seen;
3376 switch (bi->typ)
3378 case INSTR_INSTR:
3379 op = bi->ops;
3380 targ->opcode = bi->opcode;
3381 targ->insn_type = ITYPE_INSN;
3382 targ->is_specific_opcode = FALSE;
3384 for (; op != NULL; op = op->next)
3386 int op_num = op->op_num;
3387 int op_data = op->op_data;
3389 gas_assert (op->op_num < MAX_INSN_ARGS);
3391 if (targ->ntok <= op_num)
3392 targ->ntok = op_num + 1;
3394 switch (op->typ)
3396 case OP_CONSTANT:
3397 set_expr_const (&targ->tok[op_num], op_data);
3398 break;
3399 case OP_OPERAND:
3400 gas_assert (op_data < insn->ntok);
3401 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3402 break;
3403 case OP_FREEREG:
3404 if (insn->extra_arg.X_op != O_register)
3405 return FALSE;
3406 copy_expr (&targ->tok[op_num], &insn->extra_arg);
3407 break;
3408 case OP_LITERAL:
3409 sym = get_special_literal_symbol ();
3410 set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
3411 if (insn->tok[op_data].X_op == O_tlsfunc
3412 || insn->tok[op_data].X_op == O_tlsarg)
3413 copy_expr (&targ->extra_arg, &insn->tok[op_data]);
3414 break;
3415 case OP_LABEL:
3416 sym = get_special_label_symbol ();
3417 set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
3418 break;
3419 case OP_OPERAND_HI16U:
3420 case OP_OPERAND_LOW16U:
3421 gas_assert (op_data < insn->ntok);
3422 if (expr_is_const (&insn->tok[op_data]))
3424 long val;
3425 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3426 val = xg_apply_userdef_op_fn (op->typ,
3427 targ->tok[op_num].
3428 X_add_number);
3429 targ->tok[op_num].X_add_number = val;
3431 else
3433 /* For const16 we can create relocations for these. */
3434 if (targ->opcode == XTENSA_UNDEFINED
3435 || (targ->opcode != xtensa_const16_opcode))
3436 return FALSE;
3437 gas_assert (op_data < insn->ntok);
3438 /* Need to build a O_lo16 or O_hi16. */
3439 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3440 if (targ->tok[op_num].X_op == O_symbol)
3442 if (op->typ == OP_OPERAND_HI16U)
3443 targ->tok[op_num].X_op = O_hi16;
3444 else if (op->typ == OP_OPERAND_LOW16U)
3445 targ->tok[op_num].X_op = O_lo16;
3446 else
3447 return FALSE;
3450 break;
3451 default:
3452 /* currently handles:
3453 OP_OPERAND_LOW8
3454 OP_OPERAND_HI24S
3455 OP_OPERAND_F32MINUS */
3456 if (xg_has_userdef_op_fn (op->typ))
3458 gas_assert (op_data < insn->ntok);
3459 if (expr_is_const (&insn->tok[op_data]))
3461 long val;
3462 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3463 val = xg_apply_userdef_op_fn (op->typ,
3464 targ->tok[op_num].
3465 X_add_number);
3466 targ->tok[op_num].X_add_number = val;
3468 else
3469 return FALSE; /* We cannot use a relocation for this. */
3470 break;
3472 gas_assert (0);
3473 break;
3476 break;
3478 case INSTR_LITERAL_DEF:
3479 op = bi->ops;
3480 targ->opcode = XTENSA_UNDEFINED;
3481 targ->insn_type = ITYPE_LITERAL;
3482 targ->is_specific_opcode = FALSE;
3483 for (; op != NULL; op = op->next)
3485 int op_num = op->op_num;
3486 int op_data = op->op_data;
3487 gas_assert (op->op_num < MAX_INSN_ARGS);
3489 if (targ->ntok <= op_num)
3490 targ->ntok = op_num + 1;
3492 switch (op->typ)
3494 case OP_OPERAND:
3495 gas_assert (op_data < insn->ntok);
3496 /* We can only pass resolvable literals through. */
3497 if (!xg_valid_literal_expression (&insn->tok[op_data]))
3498 return FALSE;
3499 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3500 break;
3501 case OP_LITERAL:
3502 case OP_CONSTANT:
3503 case OP_LABEL:
3504 default:
3505 gas_assert (0);
3506 break;
3509 break;
3511 case INSTR_LABEL_DEF:
3512 op = bi->ops;
3513 targ->opcode = XTENSA_UNDEFINED;
3514 targ->insn_type = ITYPE_LABEL;
3515 targ->is_specific_opcode = FALSE;
3516 /* Literal with no ops is a label? */
3517 gas_assert (op == NULL);
3518 break;
3520 default:
3521 gas_assert (0);
3524 return TRUE;
3528 /* Return TRUE on success. */
3530 static bfd_boolean
3531 xg_build_to_stack (IStack *istack, TInsn *insn, BuildInstr *bi)
3533 for (; bi != NULL; bi = bi->next)
3535 TInsn *next_insn = istack_push_space (istack);
3537 if (!xg_build_to_insn (next_insn, insn, bi))
3538 return FALSE;
3540 return TRUE;
3544 /* Return TRUE on valid expansion. */
3546 static bfd_boolean
3547 xg_expand_to_stack (IStack *istack, TInsn *insn, int lateral_steps)
3549 int stack_size = istack->ninsn;
3550 int steps_taken = 0;
3551 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3552 TransitionList *l;
3554 gas_assert (insn->insn_type == ITYPE_INSN);
3555 gas_assert (insn->opcode < table->num_opcodes);
3557 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3559 TransitionRule *rule = l->rule;
3561 if (xg_instruction_matches_rule (insn, rule))
3563 if (lateral_steps == steps_taken)
3565 int i;
3567 /* This is it. Expand the rule to the stack. */
3568 if (!xg_build_to_stack (istack, insn, rule->to_instr))
3569 return FALSE;
3571 /* Check to see if it fits. */
3572 for (i = stack_size; i < istack->ninsn; i++)
3574 TInsn *tinsn = &istack->insn[i];
3576 if (tinsn->insn_type == ITYPE_INSN
3577 && !tinsn_has_symbolic_operands (tinsn)
3578 && !xg_immeds_fit (tinsn))
3580 istack->ninsn = stack_size;
3581 return FALSE;
3584 return TRUE;
3586 steps_taken++;
3589 return FALSE;
3593 /* Relax the assembly instruction at least "min_steps".
3594 Return the number of steps taken.
3596 For relaxation to correctly terminate, every relaxation chain must
3597 terminate in one of two ways:
3599 1. If the chain from one instruction to the next consists entirely of
3600 single instructions, then the chain *must* handle all possible
3601 immediates without failing. It must not ever fail because an
3602 immediate is out of range. The MOVI.N -> MOVI -> L32R relaxation
3603 chain is one example. L32R loads 32 bits, and there cannot be an
3604 immediate larger than 32 bits, so it satisfies this condition.
3605 Single instruction relaxation chains are as defined by
3606 xg_is_single_relaxable_instruction.
3608 2. Otherwise, the chain must end in a multi-instruction expansion: e.g.,
3609 BNEZ.N -> BNEZ -> BNEZ.W15 -> BENZ.N/J
3611 Strictly speaking, in most cases you can violate condition 1 and be OK
3612 -- in particular when the last two instructions have the same single
3613 size. But nevertheless, you should guarantee the above two conditions.
3615 We could fix this so that single-instruction expansions correctly
3616 terminate when they can't handle the range, but the error messages are
3617 worse, and it actually turns out that in every case but one (18-bit wide
3618 branches), you need a multi-instruction expansion to get the full range
3619 anyway. And because 18-bit branches are handled identically to 15-bit
3620 branches, there isn't any point in changing it. */
3622 static int
3623 xg_assembly_relax (IStack *istack,
3624 TInsn *insn,
3625 segT pc_seg,
3626 fragS *pc_frag, /* if pc_frag == 0, not pc-relative */
3627 offsetT pc_offset, /* offset in fragment */
3628 int min_steps, /* minimum conversion steps */
3629 long stretch) /* number of bytes stretched so far */
3631 int steps_taken = 0;
3633 /* Some of its immeds don't fit. Try to build a relaxed version.
3634 This may go through a couple of stages of single instruction
3635 transformations before we get there. */
3637 TInsn single_target;
3638 TInsn current_insn;
3639 int lateral_steps = 0;
3640 int istack_size = istack->ninsn;
3642 if (xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch)
3643 && steps_taken >= min_steps)
3645 istack_push (istack, insn);
3646 return steps_taken;
3648 current_insn = *insn;
3650 /* Walk through all of the single instruction expansions. */
3651 while (xg_is_single_relaxable_insn (&current_insn, &single_target, FALSE))
3653 steps_taken++;
3654 if (xg_symbolic_immeds_fit (&single_target, pc_seg, pc_frag, pc_offset,
3655 stretch))
3657 if (steps_taken >= min_steps)
3659 istack_push (istack, &single_target);
3660 return steps_taken;
3663 current_insn = single_target;
3666 /* Now check for a multi-instruction expansion. */
3667 while (xg_is_relaxable_insn (&current_insn, lateral_steps))
3669 if (xg_symbolic_immeds_fit (&current_insn, pc_seg, pc_frag, pc_offset,
3670 stretch))
3672 if (steps_taken >= min_steps)
3674 istack_push (istack, &current_insn);
3675 return steps_taken;
3678 steps_taken++;
3679 if (xg_expand_to_stack (istack, &current_insn, lateral_steps))
3681 if (steps_taken >= min_steps)
3682 return steps_taken;
3684 lateral_steps++;
3685 istack->ninsn = istack_size;
3688 /* It's not going to work -- use the original. */
3689 istack_push (istack, insn);
3690 return steps_taken;
3694 static void
3695 xg_finish_frag (char *last_insn,
3696 enum xtensa_relax_statesE frag_state,
3697 enum xtensa_relax_statesE slot0_state,
3698 int max_growth,
3699 bfd_boolean is_insn)
3701 /* Finish off this fragment so that it has at LEAST the desired
3702 max_growth. If it doesn't fit in this fragment, close this one
3703 and start a new one. In either case, return a pointer to the
3704 beginning of the growth area. */
3706 fragS *old_frag;
3708 frag_grow (max_growth);
3709 old_frag = frag_now;
3711 frag_now->fr_opcode = last_insn;
3712 if (is_insn)
3713 frag_now->tc_frag_data.is_insn = TRUE;
3715 frag_var (rs_machine_dependent, max_growth, max_growth,
3716 frag_state, frag_now->fr_symbol, frag_now->fr_offset, last_insn);
3718 old_frag->tc_frag_data.slot_subtypes[0] = slot0_state;
3719 xtensa_set_frag_assembly_state (frag_now);
3721 /* Just to make sure that we did not split it up. */
3722 gas_assert (old_frag->fr_next == frag_now);
3726 /* Return TRUE if the target frag is one of the next non-empty frags. */
3728 static bfd_boolean
3729 is_next_frag_target (const fragS *fragP, const fragS *target)
3731 if (fragP == NULL)
3732 return FALSE;
3734 for (; fragP; fragP = fragP->fr_next)
3736 if (fragP == target)
3737 return TRUE;
3738 if (fragP->fr_fix != 0)
3739 return FALSE;
3740 if (fragP->fr_type == rs_fill && fragP->fr_offset != 0)
3741 return FALSE;
3742 if ((fragP->fr_type == rs_align || fragP->fr_type == rs_align_code)
3743 && ((fragP->fr_address % (1 << fragP->fr_offset)) != 0))
3744 return FALSE;
3745 if (fragP->fr_type == rs_space)
3746 return FALSE;
3748 return FALSE;
3752 static bfd_boolean
3753 is_branch_jmp_to_next (TInsn *insn, fragS *fragP)
3755 xtensa_isa isa = xtensa_default_isa;
3756 int i;
3757 int num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
3758 int target_op = -1;
3759 symbolS *sym;
3760 fragS *target_frag;
3762 if (xtensa_opcode_is_branch (isa, insn->opcode) != 1
3763 && xtensa_opcode_is_jump (isa, insn->opcode) != 1)
3764 return FALSE;
3766 for (i = 0; i < num_ops; i++)
3768 if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1)
3770 target_op = i;
3771 break;
3774 if (target_op == -1)
3775 return FALSE;
3777 if (insn->ntok <= target_op)
3778 return FALSE;
3780 if (insn->tok[target_op].X_op != O_symbol)
3781 return FALSE;
3783 sym = insn->tok[target_op].X_add_symbol;
3784 if (sym == NULL)
3785 return FALSE;
3787 if (insn->tok[target_op].X_add_number != 0)
3788 return FALSE;
3790 target_frag = symbol_get_frag (sym);
3791 if (target_frag == NULL)
3792 return FALSE;
3794 if (is_next_frag_target (fragP->fr_next, target_frag)
3795 && S_GET_VALUE (sym) == target_frag->fr_address)
3796 return TRUE;
3798 return FALSE;
3802 static void
3803 xg_add_branch_and_loop_targets (TInsn *insn)
3805 xtensa_isa isa = xtensa_default_isa;
3806 int num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
3808 if (xtensa_opcode_is_loop (isa, insn->opcode) == 1)
3810 int i = 1;
3811 if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1
3812 && insn->tok[i].X_op == O_symbol)
3813 symbol_get_tc (insn->tok[i].X_add_symbol)->is_loop_target = TRUE;
3814 return;
3817 if (xtensa_opcode_is_branch (isa, insn->opcode) == 1
3818 || xtensa_opcode_is_loop (isa, insn->opcode) == 1)
3820 int i;
3822 for (i = 0; i < insn->ntok && i < num_ops; i++)
3824 if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1
3825 && insn->tok[i].X_op == O_symbol)
3827 symbolS *sym = insn->tok[i].X_add_symbol;
3828 symbol_get_tc (sym)->is_branch_target = TRUE;
3829 if (S_IS_DEFINED (sym))
3830 symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
3837 /* Return FALSE if no error. */
3839 static bfd_boolean
3840 xg_build_token_insn (BuildInstr *instr_spec, TInsn *old_insn, TInsn *new_insn)
3842 int num_ops = 0;
3843 BuildOp *b_op;
3845 switch (instr_spec->typ)
3847 case INSTR_INSTR:
3848 new_insn->insn_type = ITYPE_INSN;
3849 new_insn->opcode = instr_spec->opcode;
3850 break;
3851 case INSTR_LITERAL_DEF:
3852 new_insn->insn_type = ITYPE_LITERAL;
3853 new_insn->opcode = XTENSA_UNDEFINED;
3854 break;
3855 case INSTR_LABEL_DEF:
3856 abort ();
3858 new_insn->is_specific_opcode = FALSE;
3859 new_insn->debug_line = old_insn->debug_line;
3860 new_insn->loc_directive_seen = old_insn->loc_directive_seen;
3862 for (b_op = instr_spec->ops; b_op != NULL; b_op = b_op->next)
3864 expressionS *exp;
3865 const expressionS *src_exp;
3867 num_ops++;
3868 switch (b_op->typ)
3870 case OP_CONSTANT:
3871 /* The expression must be the constant. */
3872 gas_assert (b_op->op_num < MAX_INSN_ARGS);
3873 exp = &new_insn->tok[b_op->op_num];
3874 set_expr_const (exp, b_op->op_data);
3875 break;
3877 case OP_OPERAND:
3878 gas_assert (b_op->op_num < MAX_INSN_ARGS);
3879 gas_assert (b_op->op_data < (unsigned) old_insn->ntok);
3880 src_exp = &old_insn->tok[b_op->op_data];
3881 exp = &new_insn->tok[b_op->op_num];
3882 copy_expr (exp, src_exp);
3883 break;
3885 case OP_LITERAL:
3886 case OP_LABEL:
3887 as_bad (_("can't handle generation of literal/labels yet"));
3888 gas_assert (0);
3890 default:
3891 as_bad (_("can't handle undefined OP TYPE"));
3892 gas_assert (0);
3896 new_insn->ntok = num_ops;
3897 return FALSE;
3901 /* Return TRUE if it was simplified. */
3903 static bfd_boolean
3904 xg_simplify_insn (TInsn *old_insn, TInsn *new_insn)
3906 TransitionRule *rule;
3907 BuildInstr *insn_spec;
3909 if (old_insn->is_specific_opcode || !density_supported)
3910 return FALSE;
3912 rule = xg_instruction_match (old_insn);
3913 if (rule == NULL)
3914 return FALSE;
3916 insn_spec = rule->to_instr;
3917 /* There should only be one. */
3918 gas_assert (insn_spec != NULL);
3919 gas_assert (insn_spec->next == NULL);
3920 if (insn_spec->next != NULL)
3921 return FALSE;
3923 xg_build_token_insn (insn_spec, old_insn, new_insn);
3925 return TRUE;
3929 /* xg_expand_assembly_insn: (1) Simplify the instruction, i.e., l32i ->
3930 l32i.n. (2) Check the number of operands. (3) Place the instruction
3931 tokens into the stack or relax it and place multiple
3932 instructions/literals onto the stack. Return FALSE if no error. */
3934 static bfd_boolean
3935 xg_expand_assembly_insn (IStack *istack, TInsn *orig_insn)
3937 int noperands;
3938 TInsn new_insn;
3939 bfd_boolean do_expand;
3941 tinsn_init (&new_insn);
3943 /* Narrow it if we can. xg_simplify_insn now does all the
3944 appropriate checking (e.g., for the density option). */
3945 if (xg_simplify_insn (orig_insn, &new_insn))
3946 orig_insn = &new_insn;
3948 noperands = xtensa_opcode_num_operands (xtensa_default_isa,
3949 orig_insn->opcode);
3950 if (orig_insn->ntok < noperands)
3952 as_bad (_("found %d operands for '%s': Expected %d"),
3953 orig_insn->ntok,
3954 xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
3955 noperands);
3956 return TRUE;
3958 if (orig_insn->ntok > noperands)
3959 as_warn (_("found too many (%d) operands for '%s': Expected %d"),
3960 orig_insn->ntok,
3961 xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
3962 noperands);
3964 /* If there are not enough operands, we will assert above. If there
3965 are too many, just cut out the extras here. */
3966 orig_insn->ntok = noperands;
3968 if (tinsn_has_invalid_symbolic_operands (orig_insn))
3969 return TRUE;
3971 /* Special case for extui opcode which has constraints not handled
3972 by the ordinary operand encoding checks. The number of operands
3973 and related syntax issues have already been checked. */
3974 if (orig_insn->opcode == xtensa_extui_opcode)
3976 int shiftimm = orig_insn->tok[2].X_add_number;
3977 int maskimm = orig_insn->tok[3].X_add_number;
3978 if (shiftimm + maskimm > 32)
3980 as_bad (_("immediate operands sum to greater than 32"));
3981 return TRUE;
3985 /* If the instruction will definitely need to be relaxed, it is better
3986 to expand it now for better scheduling. Decide whether to expand
3987 now.... */
3988 do_expand = (!orig_insn->is_specific_opcode && use_transform ());
3990 /* Calls should be expanded to longcalls only in the backend relaxation
3991 so that the assembly scheduler will keep the L32R/CALLX instructions
3992 adjacent. */
3993 if (is_direct_call_opcode (orig_insn->opcode))
3994 do_expand = FALSE;
3996 if (tinsn_has_symbolic_operands (orig_insn))
3998 /* The values of symbolic operands are not known yet, so only expand
3999 now if an operand is "complex" (e.g., difference of symbols) and
4000 will have to be stored as a literal regardless of the value. */
4001 if (!tinsn_has_complex_operands (orig_insn))
4002 do_expand = FALSE;
4004 else if (xg_immeds_fit (orig_insn))
4005 do_expand = FALSE;
4007 if (do_expand)
4008 xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0);
4009 else
4010 istack_push (istack, orig_insn);
4012 return FALSE;
4016 /* Return TRUE if the section flags are marked linkonce
4017 or the name is .gnu.linkonce.*. */
4019 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
4021 static bfd_boolean
4022 get_is_linkonce_section (bfd *abfd ATTRIBUTE_UNUSED, segT sec)
4024 flagword flags, link_once_flags;
4026 flags = bfd_get_section_flags (abfd, sec);
4027 link_once_flags = (flags & SEC_LINK_ONCE);
4029 /* Flags might not be set yet. */
4030 if (!link_once_flags
4031 && strncmp (segment_name (sec), ".gnu.linkonce.", linkonce_len) == 0)
4032 link_once_flags = SEC_LINK_ONCE;
4034 return (link_once_flags != 0);
4038 static void
4039 xtensa_add_literal_sym (symbolS *sym)
4041 sym_list *l;
4043 l = (sym_list *) xmalloc (sizeof (sym_list));
4044 l->sym = sym;
4045 l->next = literal_syms;
4046 literal_syms = l;
4050 static symbolS *
4051 xtensa_create_literal_symbol (segT sec, fragS *frag)
4053 static int lit_num = 0;
4054 static char name[256];
4055 symbolS *symbolP;
4057 sprintf (name, ".L_lit_sym%d", lit_num);
4059 /* Create a local symbol. If it is in a linkonce section, we have to
4060 be careful to make sure that if it is used in a relocation that the
4061 symbol will be in the output file. */
4062 if (get_is_linkonce_section (stdoutput, sec))
4064 symbolP = symbol_new (name, sec, 0, frag);
4065 S_CLEAR_EXTERNAL (symbolP);
4066 /* symbolP->local = 1; */
4068 else
4069 symbolP = symbol_new (name, sec, 0, frag);
4071 xtensa_add_literal_sym (symbolP);
4073 lit_num++;
4074 return symbolP;
4078 /* Currently all literals that are generated here are 32-bit L32R targets. */
4080 static symbolS *
4081 xg_assemble_literal (/* const */ TInsn *insn)
4083 emit_state state;
4084 symbolS *lit_sym = NULL;
4085 bfd_reloc_code_real_type reloc;
4086 bfd_boolean pcrel = FALSE;
4087 char *p;
4089 /* size = 4 for L32R. It could easily be larger when we move to
4090 larger constants. Add a parameter later. */
4091 offsetT litsize = 4;
4092 offsetT litalign = 2; /* 2^2 = 4 */
4093 expressionS saved_loc;
4094 expressionS * emit_val;
4096 set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ());
4098 gas_assert (insn->insn_type == ITYPE_LITERAL);
4099 gas_assert (insn->ntok == 1); /* must be only one token here */
4101 xtensa_switch_to_literal_fragment (&state);
4103 emit_val = &insn->tok[0];
4104 if (emit_val->X_op == O_big)
4106 int size = emit_val->X_add_number * CHARS_PER_LITTLENUM;
4107 if (size > litsize)
4109 /* This happens when someone writes a "movi a2, big_number". */
4110 as_bad_where (frag_now->fr_file, frag_now->fr_line,
4111 _("invalid immediate"));
4112 xtensa_restore_emit_state (&state);
4113 return NULL;
4117 /* Force a 4-byte align here. Note that this opens a new frag, so all
4118 literals done with this function have a frag to themselves. That's
4119 important for the way text section literals work. */
4120 frag_align (litalign, 0, 0);
4121 record_alignment (now_seg, litalign);
4123 switch (emit_val->X_op)
4125 case O_pcrel:
4126 pcrel = TRUE;
4127 /* fall through */
4128 case O_pltrel:
4129 case O_tlsfunc:
4130 case O_tlsarg:
4131 case O_tpoff:
4132 case O_dtpoff:
4133 p = frag_more (litsize);
4134 xtensa_set_frag_assembly_state (frag_now);
4135 reloc = map_operator_to_reloc (emit_val->X_op, TRUE);
4136 if (emit_val->X_add_symbol)
4137 emit_val->X_op = O_symbol;
4138 else
4139 emit_val->X_op = O_constant;
4140 fix_new_exp (frag_now, p - frag_now->fr_literal,
4141 litsize, emit_val, pcrel, reloc);
4142 break;
4144 default:
4145 emit_expr (emit_val, litsize);
4146 break;
4149 gas_assert (frag_now->tc_frag_data.literal_frag == NULL);
4150 frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
4151 frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
4152 lit_sym = frag_now->fr_symbol;
4154 /* Go back. */
4155 xtensa_restore_emit_state (&state);
4156 return lit_sym;
4160 static void
4161 xg_assemble_literal_space (/* const */ int size, int slot)
4163 emit_state state;
4164 /* We might have to do something about this alignment. It only
4165 takes effect if something is placed here. */
4166 offsetT litalign = 2; /* 2^2 = 4 */
4167 fragS *lit_saved_frag;
4169 gas_assert (size % 4 == 0);
4171 xtensa_switch_to_literal_fragment (&state);
4173 /* Force a 4-byte align here. */
4174 frag_align (litalign, 0, 0);
4175 record_alignment (now_seg, litalign);
4177 frag_grow (size);
4179 lit_saved_frag = frag_now;
4180 frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
4181 frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
4182 xg_finish_frag (0, RELAX_LITERAL, 0, size, FALSE);
4184 /* Go back. */
4185 xtensa_restore_emit_state (&state);
4186 frag_now->tc_frag_data.literal_frags[slot] = lit_saved_frag;
4190 /* Put in a fixup record based on the opcode.
4191 Return TRUE on success. */
4193 static bfd_boolean
4194 xg_add_opcode_fix (TInsn *tinsn,
4195 int opnum,
4196 xtensa_format fmt,
4197 int slot,
4198 expressionS *exp,
4199 fragS *fragP,
4200 offsetT offset)
4202 xtensa_opcode opcode = tinsn->opcode;
4203 bfd_reloc_code_real_type reloc;
4204 reloc_howto_type *howto;
4205 int fmt_length;
4206 fixS *the_fix;
4208 reloc = BFD_RELOC_NONE;
4210 /* First try the special cases for "alternate" relocs. */
4211 if (opcode == xtensa_l32r_opcode)
4213 if (fragP->tc_frag_data.use_absolute_literals)
4214 reloc = encode_alt_reloc (slot);
4216 else if (opcode == xtensa_const16_opcode)
4218 if (exp->X_op == O_lo16)
4220 reloc = encode_reloc (slot);
4221 exp->X_op = O_symbol;
4223 else if (exp->X_op == O_hi16)
4225 reloc = encode_alt_reloc (slot);
4226 exp->X_op = O_symbol;
4230 if (opnum != get_relaxable_immed (opcode))
4232 as_bad (_("invalid relocation for operand %i of '%s'"),
4233 opnum + 1, xtensa_opcode_name (xtensa_default_isa, opcode));
4234 return FALSE;
4237 /* Handle erroneous "@h" and "@l" expressions here before they propagate
4238 into the symbol table where the generic portions of the assembler
4239 won't know what to do with them. */
4240 if (exp->X_op == O_lo16 || exp->X_op == O_hi16)
4242 as_bad (_("invalid expression for operand %i of '%s'"),
4243 opnum + 1, xtensa_opcode_name (xtensa_default_isa, opcode));
4244 return FALSE;
4247 /* Next try the generic relocs. */
4248 if (reloc == BFD_RELOC_NONE)
4249 reloc = encode_reloc (slot);
4250 if (reloc == BFD_RELOC_NONE)
4252 as_bad (_("invalid relocation in instruction slot %i"), slot);
4253 return FALSE;
4256 howto = bfd_reloc_type_lookup (stdoutput, reloc);
4257 if (!howto)
4259 as_bad (_("undefined symbol for opcode \"%s\""),
4260 xtensa_opcode_name (xtensa_default_isa, opcode));
4261 return FALSE;
4264 fmt_length = xtensa_format_length (xtensa_default_isa, fmt);
4265 the_fix = fix_new_exp (fragP, offset, fmt_length, exp,
4266 howto->pc_relative, reloc);
4267 the_fix->fx_no_overflow = 1;
4268 the_fix->tc_fix_data.X_add_symbol = exp->X_add_symbol;
4269 the_fix->tc_fix_data.X_add_number = exp->X_add_number;
4270 the_fix->tc_fix_data.slot = slot;
4272 return TRUE;
4276 static bfd_boolean
4277 xg_emit_insn_to_buf (TInsn *tinsn,
4278 char *buf,
4279 fragS *fragP,
4280 offsetT offset,
4281 bfd_boolean build_fix)
4283 static xtensa_insnbuf insnbuf = NULL;
4284 bfd_boolean has_symbolic_immed = FALSE;
4285 bfd_boolean ok = TRUE;
4287 if (!insnbuf)
4288 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4290 has_symbolic_immed = tinsn_to_insnbuf (tinsn, insnbuf);
4291 if (has_symbolic_immed && build_fix)
4293 /* Add a fixup. */
4294 xtensa_format fmt = xg_get_single_format (tinsn->opcode);
4295 int slot = xg_get_single_slot (tinsn->opcode);
4296 int opnum = get_relaxable_immed (tinsn->opcode);
4297 expressionS *exp = &tinsn->tok[opnum];
4299 if (!xg_add_opcode_fix (tinsn, opnum, fmt, slot, exp, fragP, offset))
4300 ok = FALSE;
4302 fragP->tc_frag_data.is_insn = TRUE;
4303 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf,
4304 (unsigned char *) buf, 0);
4305 return ok;
4309 static void
4310 xg_resolve_literals (TInsn *insn, symbolS *lit_sym)
4312 symbolS *sym = get_special_literal_symbol ();
4313 int i;
4314 if (lit_sym == 0)
4315 return;
4316 gas_assert (insn->insn_type == ITYPE_INSN);
4317 for (i = 0; i < insn->ntok; i++)
4318 if (insn->tok[i].X_add_symbol == sym)
4319 insn->tok[i].X_add_symbol = lit_sym;
4324 static void
4325 xg_resolve_labels (TInsn *insn, symbolS *label_sym)
4327 symbolS *sym = get_special_label_symbol ();
4328 int i;
4329 for (i = 0; i < insn->ntok; i++)
4330 if (insn->tok[i].X_add_symbol == sym)
4331 insn->tok[i].X_add_symbol = label_sym;
4336 /* Return TRUE if the instruction can write to the specified
4337 integer register. */
4339 static bfd_boolean
4340 is_register_writer (const TInsn *insn, const char *regset, int regnum)
4342 int i;
4343 int num_ops;
4344 xtensa_isa isa = xtensa_default_isa;
4346 num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
4348 for (i = 0; i < num_ops; i++)
4350 char inout;
4351 inout = xtensa_operand_inout (isa, insn->opcode, i);
4352 if ((inout == 'o' || inout == 'm')
4353 && xtensa_operand_is_register (isa, insn->opcode, i) == 1)
4355 xtensa_regfile opnd_rf =
4356 xtensa_operand_regfile (isa, insn->opcode, i);
4357 if (!strcmp (xtensa_regfile_shortname (isa, opnd_rf), regset))
4359 if ((insn->tok[i].X_op == O_register)
4360 && (insn->tok[i].X_add_number == regnum))
4361 return TRUE;
4365 return FALSE;
4369 static bfd_boolean
4370 is_bad_loopend_opcode (const TInsn *tinsn)
4372 xtensa_opcode opcode = tinsn->opcode;
4374 if (opcode == XTENSA_UNDEFINED)
4375 return FALSE;
4377 if (opcode == xtensa_call0_opcode
4378 || opcode == xtensa_callx0_opcode
4379 || opcode == xtensa_call4_opcode
4380 || opcode == xtensa_callx4_opcode
4381 || opcode == xtensa_call8_opcode
4382 || opcode == xtensa_callx8_opcode
4383 || opcode == xtensa_call12_opcode
4384 || opcode == xtensa_callx12_opcode
4385 || opcode == xtensa_isync_opcode
4386 || opcode == xtensa_ret_opcode
4387 || opcode == xtensa_ret_n_opcode
4388 || opcode == xtensa_retw_opcode
4389 || opcode == xtensa_retw_n_opcode
4390 || opcode == xtensa_waiti_opcode
4391 || opcode == xtensa_rsr_lcount_opcode)
4392 return TRUE;
4394 return FALSE;
4398 /* Labels that begin with ".Ln" or ".LM" are unaligned.
4399 This allows the debugger to add unaligned labels.
4400 Also, the assembler generates stabs labels that need
4401 not be aligned: FAKE_LABEL_NAME . {"F", "L", "endfunc"}. */
4403 static bfd_boolean
4404 is_unaligned_label (symbolS *sym)
4406 const char *name = S_GET_NAME (sym);
4407 static size_t fake_size = 0;
4409 if (name
4410 && name[0] == '.'
4411 && name[1] == 'L' && (name[2] == 'n' || name[2] == 'M'))
4412 return TRUE;
4414 /* FAKE_LABEL_NAME followed by "F", "L" or "endfunc" */
4415 if (fake_size == 0)
4416 fake_size = strlen (FAKE_LABEL_NAME);
4418 if (name
4419 && strncmp (FAKE_LABEL_NAME, name, fake_size) == 0
4420 && (name[fake_size] == 'F'
4421 || name[fake_size] == 'L'
4422 || (name[fake_size] == 'e'
4423 && strncmp ("endfunc", name+fake_size, 7) == 0)))
4424 return TRUE;
4426 return FALSE;
4430 static fragS *
4431 next_non_empty_frag (const fragS *fragP)
4433 fragS *next_fragP = fragP->fr_next;
4435 /* Sometimes an empty will end up here due storage allocation issues.
4436 So we have to skip until we find something legit. */
4437 while (next_fragP && next_fragP->fr_fix == 0)
4438 next_fragP = next_fragP->fr_next;
4440 if (next_fragP == NULL || next_fragP->fr_fix == 0)
4441 return NULL;
4443 return next_fragP;
4447 static bfd_boolean
4448 next_frag_opcode_is_loop (const fragS *fragP, xtensa_opcode *opcode)
4450 xtensa_opcode out_opcode;
4451 const fragS *next_fragP = next_non_empty_frag (fragP);
4453 if (next_fragP == NULL)
4454 return FALSE;
4456 out_opcode = get_opcode_from_buf (next_fragP->fr_literal, 0);
4457 if (xtensa_opcode_is_loop (xtensa_default_isa, out_opcode) == 1)
4459 *opcode = out_opcode;
4460 return TRUE;
4462 return FALSE;
4466 static int
4467 frag_format_size (const fragS *fragP)
4469 static xtensa_insnbuf insnbuf = NULL;
4470 xtensa_isa isa = xtensa_default_isa;
4471 xtensa_format fmt;
4472 int fmt_size;
4474 if (!insnbuf)
4475 insnbuf = xtensa_insnbuf_alloc (isa);
4477 if (fragP == NULL)
4478 return XTENSA_UNDEFINED;
4480 xtensa_insnbuf_from_chars (isa, insnbuf,
4481 (unsigned char *) fragP->fr_literal, 0);
4483 fmt = xtensa_format_decode (isa, insnbuf);
4484 if (fmt == XTENSA_UNDEFINED)
4485 return XTENSA_UNDEFINED;
4486 fmt_size = xtensa_format_length (isa, fmt);
4488 /* If the next format won't be changing due to relaxation, just
4489 return the length of the first format. */
4490 if (fragP->fr_opcode != fragP->fr_literal)
4491 return fmt_size;
4493 /* If during relaxation we have to pull an instruction out of a
4494 multi-slot instruction, we will return the more conservative
4495 number. This works because alignment on bigger instructions
4496 is more restrictive than alignment on smaller instructions.
4497 This is more conservative than we would like, but it happens
4498 infrequently. */
4500 if (xtensa_format_num_slots (xtensa_default_isa, fmt) > 1)
4501 return fmt_size;
4503 /* If we aren't doing one of our own relaxations or it isn't
4504 slot-based, then the insn size won't change. */
4505 if (fragP->fr_type != rs_machine_dependent)
4506 return fmt_size;
4507 if (fragP->fr_subtype != RELAX_SLOTS)
4508 return fmt_size;
4510 /* If an instruction is about to grow, return the longer size. */
4511 if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP1
4512 || fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP2
4513 || fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP3)
4515 /* For most frags at RELAX_IMMED_STEPX, with X > 0, the first
4516 instruction in the relaxed version is of length 3. (The case
4517 where we have to pull the instruction out of a FLIX bundle
4518 is handled conservatively above.) However, frags with opcodes
4519 that are expanding to wide branches end up having formats that
4520 are not determinable by the RELAX_IMMED_STEPX enumeration, and
4521 we can't tell directly what format the relaxer picked. This
4522 is a wart in the design of the relaxer that should someday be
4523 fixed, but would require major changes, or at least should
4524 be accompanied by major changes to make use of that data.
4526 In any event, we can tell that we are expanding from a single-slot
4527 three-byte format to a wider one with the logic below. */
4529 if (fmt_size <= 3 && fragP->tc_frag_data.text_expansion[0] != 3)
4530 return 3 + fragP->tc_frag_data.text_expansion[0];
4531 else
4532 return 3;
4535 if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
4536 return 2 + fragP->tc_frag_data.text_expansion[0];
4538 return fmt_size;
4542 static int
4543 next_frag_format_size (const fragS *fragP)
4545 const fragS *next_fragP = next_non_empty_frag (fragP);
4546 return frag_format_size (next_fragP);
4550 /* In early Xtensa Processors, for reasons that are unclear, the ISA
4551 required two-byte instructions to be treated as three-byte instructions
4552 for loop instruction alignment. This restriction was removed beginning
4553 with Xtensa LX. Now the only requirement on loop instruction alignment
4554 is that the first instruction of the loop must appear at an address that
4555 does not cross a fetch boundary. */
4557 static int
4558 get_loop_align_size (int insn_size)
4560 if (insn_size == XTENSA_UNDEFINED)
4561 return xtensa_fetch_width;
4563 if (enforce_three_byte_loop_align && insn_size == 2)
4564 return 3;
4566 return insn_size;
4570 /* If the next legit fragment is an end-of-loop marker,
4571 switch its state so it will instantiate a NOP. */
4573 static void
4574 update_next_frag_state (fragS *fragP)
4576 fragS *next_fragP = fragP->fr_next;
4577 fragS *new_target = NULL;
4579 if (align_targets)
4581 /* We are guaranteed there will be one of these... */
4582 while (!(next_fragP->fr_type == rs_machine_dependent
4583 && (next_fragP->fr_subtype == RELAX_MAYBE_UNREACHABLE
4584 || next_fragP->fr_subtype == RELAX_UNREACHABLE)))
4585 next_fragP = next_fragP->fr_next;
4587 gas_assert (next_fragP->fr_type == rs_machine_dependent
4588 && (next_fragP->fr_subtype == RELAX_MAYBE_UNREACHABLE
4589 || next_fragP->fr_subtype == RELAX_UNREACHABLE));
4591 /* ...and one of these. */
4592 new_target = next_fragP->fr_next;
4593 while (!(new_target->fr_type == rs_machine_dependent
4594 && (new_target->fr_subtype == RELAX_MAYBE_DESIRE_ALIGN
4595 || new_target->fr_subtype == RELAX_DESIRE_ALIGN)))
4596 new_target = new_target->fr_next;
4598 gas_assert (new_target->fr_type == rs_machine_dependent
4599 && (new_target->fr_subtype == RELAX_MAYBE_DESIRE_ALIGN
4600 || new_target->fr_subtype == RELAX_DESIRE_ALIGN));
4603 while (next_fragP && next_fragP->fr_fix == 0)
4605 if (next_fragP->fr_type == rs_machine_dependent
4606 && next_fragP->fr_subtype == RELAX_LOOP_END)
4608 next_fragP->fr_subtype = RELAX_LOOP_END_ADD_NOP;
4609 return;
4612 next_fragP = next_fragP->fr_next;
4617 static bfd_boolean
4618 next_frag_is_branch_target (const fragS *fragP)
4620 /* Sometimes an empty will end up here due to storage allocation issues,
4621 so we have to skip until we find something legit. */
4622 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4624 if (fragP->tc_frag_data.is_branch_target)
4625 return TRUE;
4626 if (fragP->fr_fix != 0)
4627 break;
4629 return FALSE;
4633 static bfd_boolean
4634 next_frag_is_loop_target (const fragS *fragP)
4636 /* Sometimes an empty will end up here due storage allocation issues.
4637 So we have to skip until we find something legit. */
4638 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4640 if (fragP->tc_frag_data.is_loop_target)
4641 return TRUE;
4642 if (fragP->fr_fix != 0)
4643 break;
4645 return FALSE;
4649 static addressT
4650 next_frag_pre_opcode_bytes (const fragS *fragp)
4652 const fragS *next_fragp = fragp->fr_next;
4653 xtensa_opcode next_opcode;
4655 if (!next_frag_opcode_is_loop (fragp, &next_opcode))
4656 return 0;
4658 /* Sometimes an empty will end up here due to storage allocation issues,
4659 so we have to skip until we find something legit. */
4660 while (next_fragp->fr_fix == 0)
4661 next_fragp = next_fragp->fr_next;
4663 if (next_fragp->fr_type != rs_machine_dependent)
4664 return 0;
4666 /* There is some implicit knowledge encoded in here.
4667 The LOOP instructions that are NOT RELAX_IMMED have
4668 been relaxed. Note that we can assume that the LOOP
4669 instruction is in slot 0 because loops aren't bundleable. */
4670 if (next_fragp->tc_frag_data.slot_subtypes[0] > RELAX_IMMED)
4671 return get_expanded_loop_offset (next_opcode);
4673 return 0;
4677 /* Mark a location where we can later insert literal frags. Update
4678 the section's literal_pool_loc, so subsequent literals can be
4679 placed nearest to their use. */
4681 static void
4682 xtensa_mark_literal_pool_location (void)
4684 /* Any labels pointing to the current location need
4685 to be adjusted to after the literal pool. */
4686 emit_state s;
4687 fragS *pool_location;
4689 if (use_literal_section)
4690 return;
4692 /* We stash info in these frags so we can later move the literal's
4693 fixes into this frchain's fix list. */
4694 pool_location = frag_now;
4695 frag_now->tc_frag_data.lit_frchain = frchain_now;
4696 frag_now->tc_frag_data.literal_frag = frag_now;
4697 frag_variant (rs_machine_dependent, 0, 0,
4698 RELAX_LITERAL_POOL_BEGIN, NULL, 0, NULL);
4699 xtensa_set_frag_assembly_state (frag_now);
4700 frag_now->tc_frag_data.lit_seg = now_seg;
4701 frag_variant (rs_machine_dependent, 0, 0,
4702 RELAX_LITERAL_POOL_END, NULL, 0, NULL);
4703 xtensa_set_frag_assembly_state (frag_now);
4705 /* Now put a frag into the literal pool that points to this location. */
4706 set_literal_pool_location (now_seg, pool_location);
4707 xtensa_switch_to_non_abs_literal_fragment (&s);
4708 frag_align (2, 0, 0);
4709 record_alignment (now_seg, 2);
4711 /* Close whatever frag is there. */
4712 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4713 xtensa_set_frag_assembly_state (frag_now);
4714 frag_now->tc_frag_data.literal_frag = pool_location;
4715 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4716 xtensa_restore_emit_state (&s);
4717 xtensa_set_frag_assembly_state (frag_now);
4721 /* Build a nop of the correct size into tinsn. */
4723 static void
4724 build_nop (TInsn *tinsn, int size)
4726 tinsn_init (tinsn);
4727 switch (size)
4729 case 2:
4730 tinsn->opcode = xtensa_nop_n_opcode;
4731 tinsn->ntok = 0;
4732 if (tinsn->opcode == XTENSA_UNDEFINED)
4733 as_fatal (_("opcode 'NOP.N' unavailable in this configuration"));
4734 break;
4736 case 3:
4737 if (xtensa_nop_opcode == XTENSA_UNDEFINED)
4739 tinsn->opcode = xtensa_or_opcode;
4740 set_expr_const (&tinsn->tok[0], 1);
4741 set_expr_const (&tinsn->tok[1], 1);
4742 set_expr_const (&tinsn->tok[2], 1);
4743 tinsn->ntok = 3;
4745 else
4746 tinsn->opcode = xtensa_nop_opcode;
4748 gas_assert (tinsn->opcode != XTENSA_UNDEFINED);
4753 /* Assemble a NOP of the requested size in the buffer. User must have
4754 allocated "buf" with at least "size" bytes. */
4756 static void
4757 assemble_nop (int size, char *buf)
4759 static xtensa_insnbuf insnbuf = NULL;
4760 TInsn tinsn;
4762 build_nop (&tinsn, size);
4764 if (!insnbuf)
4765 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4767 tinsn_to_insnbuf (&tinsn, insnbuf);
4768 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf,
4769 (unsigned char *) buf, 0);
4773 /* Return the number of bytes for the offset of the expanded loop
4774 instruction. This should be incorporated into the relaxation
4775 specification but is hard-coded here. This is used to auto-align
4776 the loop instruction. It is invalid to call this function if the
4777 configuration does not have loops or if the opcode is not a loop
4778 opcode. */
4780 static addressT
4781 get_expanded_loop_offset (xtensa_opcode opcode)
4783 /* This is the OFFSET of the loop instruction in the expanded loop.
4784 This MUST correspond directly to the specification of the loop
4785 expansion. It will be validated on fragment conversion. */
4786 gas_assert (opcode != XTENSA_UNDEFINED);
4787 if (opcode == xtensa_loop_opcode)
4788 return 0;
4789 if (opcode == xtensa_loopnez_opcode)
4790 return 3;
4791 if (opcode == xtensa_loopgtz_opcode)
4792 return 6;
4793 as_fatal (_("get_expanded_loop_offset: invalid opcode"));
4794 return 0;
4798 static fragS *
4799 get_literal_pool_location (segT seg)
4801 return seg_info (seg)->tc_segment_info_data.literal_pool_loc;
4805 static void
4806 set_literal_pool_location (segT seg, fragS *literal_pool_loc)
4808 seg_info (seg)->tc_segment_info_data.literal_pool_loc = literal_pool_loc;
4812 /* Set frag assembly state should be called when a new frag is
4813 opened and after a frag has been closed. */
4815 static void
4816 xtensa_set_frag_assembly_state (fragS *fragP)
4818 if (!density_supported)
4819 fragP->tc_frag_data.is_no_density = TRUE;
4821 /* This function is called from subsegs_finish, which is called
4822 after xtensa_end, so we can't use "use_transform" or
4823 "use_schedule" here. */
4824 if (!directive_state[directive_transform])
4825 fragP->tc_frag_data.is_no_transform = TRUE;
4826 if (directive_state[directive_longcalls])
4827 fragP->tc_frag_data.use_longcalls = TRUE;
4828 fragP->tc_frag_data.use_absolute_literals =
4829 directive_state[directive_absolute_literals];
4830 fragP->tc_frag_data.is_assembly_state_set = TRUE;
4834 static bfd_boolean
4835 relaxable_section (asection *sec)
4837 return ((sec->flags & SEC_DEBUGGING) == 0
4838 && strcmp (sec->name, ".eh_frame") != 0);
4842 static void
4843 xtensa_mark_frags_for_org (void)
4845 segT *seclist;
4847 /* Walk over each fragment of all of the current segments. If we find
4848 a .org frag in any of the segments, mark all frags prior to it as
4849 "no transform", which will prevent linker optimizations from messing
4850 up the .org distance. This should be done after
4851 xtensa_find_unmarked_state_frags, because we don't want to worry here
4852 about that function trashing the data we save here. */
4854 for (seclist = &stdoutput->sections;
4855 seclist && *seclist;
4856 seclist = &(*seclist)->next)
4858 segT sec = *seclist;
4859 segment_info_type *seginfo;
4860 fragS *fragP;
4861 flagword flags;
4862 flags = bfd_get_section_flags (stdoutput, sec);
4863 if (flags & SEC_DEBUGGING)
4864 continue;
4865 if (!(flags & SEC_ALLOC))
4866 continue;
4868 seginfo = seg_info (sec);
4869 if (seginfo && seginfo->frchainP)
4871 fragS *last_fragP = seginfo->frchainP->frch_root;
4872 for (fragP = seginfo->frchainP->frch_root; fragP;
4873 fragP = fragP->fr_next)
4875 /* cvt_frag_to_fill has changed the fr_type of org frags to
4876 rs_fill, so use the value as cached in rs_subtype here. */
4877 if (fragP->fr_subtype == RELAX_ORG)
4879 while (last_fragP != fragP->fr_next)
4881 last_fragP->tc_frag_data.is_no_transform = TRUE;
4882 last_fragP = last_fragP->fr_next;
4891 static void
4892 xtensa_find_unmarked_state_frags (void)
4894 segT *seclist;
4896 /* Walk over each fragment of all of the current segments. For each
4897 unmarked fragment, mark it with the same info as the previous
4898 fragment. */
4899 for (seclist = &stdoutput->sections;
4900 seclist && *seclist;
4901 seclist = &(*seclist)->next)
4903 segT sec = *seclist;
4904 segment_info_type *seginfo;
4905 fragS *fragP;
4906 flagword flags;
4907 flags = bfd_get_section_flags (stdoutput, sec);
4908 if (flags & SEC_DEBUGGING)
4909 continue;
4910 if (!(flags & SEC_ALLOC))
4911 continue;
4913 seginfo = seg_info (sec);
4914 if (seginfo && seginfo->frchainP)
4916 fragS *last_fragP = 0;
4917 for (fragP = seginfo->frchainP->frch_root; fragP;
4918 fragP = fragP->fr_next)
4920 if (fragP->fr_fix != 0
4921 && !fragP->tc_frag_data.is_assembly_state_set)
4923 if (last_fragP == 0)
4925 as_warn_where (fragP->fr_file, fragP->fr_line,
4926 _("assembly state not set for first frag in section %s"),
4927 sec->name);
4929 else
4931 fragP->tc_frag_data.is_assembly_state_set = TRUE;
4932 fragP->tc_frag_data.is_no_density =
4933 last_fragP->tc_frag_data.is_no_density;
4934 fragP->tc_frag_data.is_no_transform =
4935 last_fragP->tc_frag_data.is_no_transform;
4936 fragP->tc_frag_data.use_longcalls =
4937 last_fragP->tc_frag_data.use_longcalls;
4938 fragP->tc_frag_data.use_absolute_literals =
4939 last_fragP->tc_frag_data.use_absolute_literals;
4942 if (fragP->tc_frag_data.is_assembly_state_set)
4943 last_fragP = fragP;
4950 static void
4951 xtensa_find_unaligned_branch_targets (bfd *abfd ATTRIBUTE_UNUSED,
4952 asection *sec,
4953 void *unused ATTRIBUTE_UNUSED)
4955 flagword flags = bfd_get_section_flags (abfd, sec);
4956 segment_info_type *seginfo = seg_info (sec);
4957 fragS *frag = seginfo->frchainP->frch_root;
4959 if (flags & SEC_CODE)
4961 xtensa_isa isa = xtensa_default_isa;
4962 xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa);
4963 while (frag != NULL)
4965 if (frag->tc_frag_data.is_branch_target)
4967 int op_size;
4968 addressT branch_align, frag_addr;
4969 xtensa_format fmt;
4971 xtensa_insnbuf_from_chars
4972 (isa, insnbuf, (unsigned char *) frag->fr_literal, 0);
4973 fmt = xtensa_format_decode (isa, insnbuf);
4974 op_size = xtensa_format_length (isa, fmt);
4975 branch_align = 1 << branch_align_power (sec);
4976 frag_addr = frag->fr_address % branch_align;
4977 if (frag_addr + op_size > branch_align)
4978 as_warn_where (frag->fr_file, frag->fr_line,
4979 _("unaligned branch target: %d bytes at 0x%lx"),
4980 op_size, (long) frag->fr_address);
4982 frag = frag->fr_next;
4984 xtensa_insnbuf_free (isa, insnbuf);
4989 static void
4990 xtensa_find_unaligned_loops (bfd *abfd ATTRIBUTE_UNUSED,
4991 asection *sec,
4992 void *unused ATTRIBUTE_UNUSED)
4994 flagword flags = bfd_get_section_flags (abfd, sec);
4995 segment_info_type *seginfo = seg_info (sec);
4996 fragS *frag = seginfo->frchainP->frch_root;
4997 xtensa_isa isa = xtensa_default_isa;
4999 if (flags & SEC_CODE)
5001 xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa);
5002 while (frag != NULL)
5004 if (frag->tc_frag_data.is_first_loop_insn)
5006 int op_size;
5007 addressT frag_addr;
5008 xtensa_format fmt;
5010 xtensa_insnbuf_from_chars
5011 (isa, insnbuf, (unsigned char *) frag->fr_literal, 0);
5012 fmt = xtensa_format_decode (isa, insnbuf);
5013 op_size = xtensa_format_length (isa, fmt);
5014 frag_addr = frag->fr_address % xtensa_fetch_width;
5016 if (frag_addr + op_size > xtensa_fetch_width)
5017 as_warn_where (frag->fr_file, frag->fr_line,
5018 _("unaligned loop: %d bytes at 0x%lx"),
5019 op_size, (long) frag->fr_address);
5021 frag = frag->fr_next;
5023 xtensa_insnbuf_free (isa, insnbuf);
5028 static int
5029 xg_apply_fix_value (fixS *fixP, valueT val)
5031 xtensa_isa isa = xtensa_default_isa;
5032 static xtensa_insnbuf insnbuf = NULL;
5033 static xtensa_insnbuf slotbuf = NULL;
5034 xtensa_format fmt;
5035 int slot;
5036 bfd_boolean alt_reloc;
5037 xtensa_opcode opcode;
5038 char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
5040 if (decode_reloc (fixP->fx_r_type, &slot, &alt_reloc)
5041 || alt_reloc)
5042 as_fatal (_("unexpected fix"));
5044 if (!insnbuf)
5046 insnbuf = xtensa_insnbuf_alloc (isa);
5047 slotbuf = xtensa_insnbuf_alloc (isa);
5050 xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) fixpos, 0);
5051 fmt = xtensa_format_decode (isa, insnbuf);
5052 if (fmt == XTENSA_UNDEFINED)
5053 as_fatal (_("undecodable fix"));
5054 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
5055 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
5056 if (opcode == XTENSA_UNDEFINED)
5057 as_fatal (_("undecodable fix"));
5059 /* CONST16 immediates are not PC-relative, despite the fact that we
5060 reuse the normal PC-relative operand relocations for the low part
5061 of a CONST16 operand. */
5062 if (opcode == xtensa_const16_opcode)
5063 return 0;
5065 xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode,
5066 get_relaxable_immed (opcode), val,
5067 fixP->fx_file, fixP->fx_line);
5069 xtensa_format_set_slot (isa, fmt, slot, insnbuf, slotbuf);
5070 xtensa_insnbuf_to_chars (isa, insnbuf, (unsigned char *) fixpos, 0);
5072 return 1;
5076 /* External Functions and Other GAS Hooks. */
5078 const char *
5079 xtensa_target_format (void)
5081 return (target_big_endian ? "elf32-xtensa-be" : "elf32-xtensa-le");
5085 void
5086 xtensa_file_arch_init (bfd *abfd)
5088 bfd_set_private_flags (abfd, 0x100 | 0x200);
5092 void
5093 md_number_to_chars (char *buf, valueT val, int n)
5095 if (target_big_endian)
5096 number_to_chars_bigendian (buf, val, n);
5097 else
5098 number_to_chars_littleendian (buf, val, n);
5102 /* This function is called once, at assembler startup time. It should
5103 set up all the tables, etc. that the MD part of the assembler will
5104 need. */
5106 void
5107 md_begin (void)
5109 segT current_section = now_seg;
5110 int current_subsec = now_subseg;
5111 xtensa_isa isa;
5112 int i;
5114 xtensa_default_isa = xtensa_isa_init (0, 0);
5115 isa = xtensa_default_isa;
5117 linkrelax = 1;
5119 /* Set up the literal sections. */
5120 memset (&default_lit_sections, 0, sizeof (default_lit_sections));
5122 subseg_set (current_section, current_subsec);
5124 xtensa_addi_opcode = xtensa_opcode_lookup (isa, "addi");
5125 xtensa_addmi_opcode = xtensa_opcode_lookup (isa, "addmi");
5126 xtensa_call0_opcode = xtensa_opcode_lookup (isa, "call0");
5127 xtensa_call4_opcode = xtensa_opcode_lookup (isa, "call4");
5128 xtensa_call8_opcode = xtensa_opcode_lookup (isa, "call8");
5129 xtensa_call12_opcode = xtensa_opcode_lookup (isa, "call12");
5130 xtensa_callx0_opcode = xtensa_opcode_lookup (isa, "callx0");
5131 xtensa_callx4_opcode = xtensa_opcode_lookup (isa, "callx4");
5132 xtensa_callx8_opcode = xtensa_opcode_lookup (isa, "callx8");
5133 xtensa_callx12_opcode = xtensa_opcode_lookup (isa, "callx12");
5134 xtensa_const16_opcode = xtensa_opcode_lookup (isa, "const16");
5135 xtensa_entry_opcode = xtensa_opcode_lookup (isa, "entry");
5136 xtensa_extui_opcode = xtensa_opcode_lookup (isa, "extui");
5137 xtensa_movi_opcode = xtensa_opcode_lookup (isa, "movi");
5138 xtensa_movi_n_opcode = xtensa_opcode_lookup (isa, "movi.n");
5139 xtensa_isync_opcode = xtensa_opcode_lookup (isa, "isync");
5140 xtensa_j_opcode = xtensa_opcode_lookup (isa, "j");
5141 xtensa_jx_opcode = xtensa_opcode_lookup (isa, "jx");
5142 xtensa_l32r_opcode = xtensa_opcode_lookup (isa, "l32r");
5143 xtensa_loop_opcode = xtensa_opcode_lookup (isa, "loop");
5144 xtensa_loopnez_opcode = xtensa_opcode_lookup (isa, "loopnez");
5145 xtensa_loopgtz_opcode = xtensa_opcode_lookup (isa, "loopgtz");
5146 xtensa_nop_opcode = xtensa_opcode_lookup (isa, "nop");
5147 xtensa_nop_n_opcode = xtensa_opcode_lookup (isa, "nop.n");
5148 xtensa_or_opcode = xtensa_opcode_lookup (isa, "or");
5149 xtensa_ret_opcode = xtensa_opcode_lookup (isa, "ret");
5150 xtensa_ret_n_opcode = xtensa_opcode_lookup (isa, "ret.n");
5151 xtensa_retw_opcode = xtensa_opcode_lookup (isa, "retw");
5152 xtensa_retw_n_opcode = xtensa_opcode_lookup (isa, "retw.n");
5153 xtensa_rsr_lcount_opcode = xtensa_opcode_lookup (isa, "rsr.lcount");
5154 xtensa_waiti_opcode = xtensa_opcode_lookup (isa, "waiti");
5156 for (i = 0; i < xtensa_isa_num_formats (isa); i++)
5158 int format_slots = xtensa_format_num_slots (isa, i);
5159 if (format_slots > config_max_slots)
5160 config_max_slots = format_slots;
5163 xg_init_vinsn (&cur_vinsn);
5165 xtensa_num_pipe_stages = xtensa_isa_num_pipe_stages (isa);
5167 init_op_placement_info_table ();
5169 /* Set up the assembly state. */
5170 if (!frag_now->tc_frag_data.is_assembly_state_set)
5171 xtensa_set_frag_assembly_state (frag_now);
5175 /* TC_INIT_FIX_DATA hook */
5177 void
5178 xtensa_init_fix_data (fixS *x)
5180 x->tc_fix_data.slot = 0;
5181 x->tc_fix_data.X_add_symbol = NULL;
5182 x->tc_fix_data.X_add_number = 0;
5186 /* tc_frob_label hook */
5188 void
5189 xtensa_frob_label (symbolS *sym)
5191 float freq;
5193 if (cur_vinsn.inside_bundle)
5195 as_bad (_("labels are not valid inside bundles"));
5196 return;
5199 freq = get_subseg_target_freq (now_seg, now_subseg);
5201 /* Since the label was already attached to a frag associated with the
5202 previous basic block, it now needs to be reset to the current frag. */
5203 symbol_set_frag (sym, frag_now);
5204 S_SET_VALUE (sym, (valueT) frag_now_fix ());
5206 if (generating_literals)
5207 xtensa_add_literal_sym (sym);
5208 else
5209 xtensa_add_insn_label (sym);
5211 if (symbol_get_tc (sym)->is_loop_target)
5213 if ((get_last_insn_flags (now_seg, now_subseg)
5214 & FLAG_IS_BAD_LOOPEND) != 0)
5215 as_bad (_("invalid last instruction for a zero-overhead loop"));
5217 xtensa_set_frag_assembly_state (frag_now);
5218 frag_var (rs_machine_dependent, 4, 4, RELAX_LOOP_END,
5219 frag_now->fr_symbol, frag_now->fr_offset, NULL);
5221 xtensa_set_frag_assembly_state (frag_now);
5222 xtensa_move_labels (frag_now, 0);
5225 /* No target aligning in the absolute section. */
5226 if (now_seg != absolute_section
5227 && !is_unaligned_label (sym)
5228 && !generating_literals)
5230 xtensa_set_frag_assembly_state (frag_now);
5232 if (do_align_targets ())
5233 frag_var (rs_machine_dependent, 0, (int) freq,
5234 RELAX_DESIRE_ALIGN_IF_TARGET, frag_now->fr_symbol,
5235 frag_now->fr_offset, NULL);
5236 else
5237 frag_var (rs_fill, 0, 0, frag_now->fr_subtype,
5238 frag_now->fr_symbol, frag_now->fr_offset, NULL);
5239 xtensa_set_frag_assembly_state (frag_now);
5240 xtensa_move_labels (frag_now, 0);
5243 /* We need to mark the following properties even if we aren't aligning. */
5245 /* If the label is already known to be a branch target, i.e., a
5246 forward branch, mark the frag accordingly. Backward branches
5247 are handled by xg_add_branch_and_loop_targets. */
5248 if (symbol_get_tc (sym)->is_branch_target)
5249 symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
5251 /* Loops only go forward, so they can be identified here. */
5252 if (symbol_get_tc (sym)->is_loop_target)
5253 symbol_get_frag (sym)->tc_frag_data.is_loop_target = TRUE;
5255 dwarf2_emit_label (sym);
5259 /* tc_unrecognized_line hook */
5262 xtensa_unrecognized_line (int ch)
5264 switch (ch)
5266 case '{' :
5267 if (cur_vinsn.inside_bundle == 0)
5269 /* PR8110: Cannot emit line number info inside a FLIX bundle
5270 when using --gstabs. Temporarily disable debug info. */
5271 generate_lineno_debug ();
5272 if (debug_type == DEBUG_STABS)
5274 xt_saved_debug_type = debug_type;
5275 debug_type = DEBUG_NONE;
5278 cur_vinsn.inside_bundle = 1;
5280 else
5282 as_bad (_("extra opening brace"));
5283 return 0;
5285 break;
5287 case '}' :
5288 if (cur_vinsn.inside_bundle)
5289 finish_vinsn (&cur_vinsn);
5290 else
5292 as_bad (_("extra closing brace"));
5293 return 0;
5295 break;
5296 default:
5297 as_bad (_("syntax error"));
5298 return 0;
5300 return 1;
5304 /* md_flush_pending_output hook */
5306 void
5307 xtensa_flush_pending_output (void)
5309 /* This line fixes a bug where automatically generated gstabs info
5310 separates a function label from its entry instruction, ending up
5311 with the literal position between the function label and the entry
5312 instruction and crashing code. It only happens with --gstabs and
5313 --text-section-literals, and when several other obscure relaxation
5314 conditions are met. */
5315 if (outputting_stabs_line_debug)
5316 return;
5318 if (cur_vinsn.inside_bundle)
5319 as_bad (_("missing closing brace"));
5321 /* If there is a non-zero instruction fragment, close it. */
5322 if (frag_now_fix () != 0 && frag_now->tc_frag_data.is_insn)
5324 frag_wane (frag_now);
5325 frag_new (0);
5326 xtensa_set_frag_assembly_state (frag_now);
5328 frag_now->tc_frag_data.is_insn = FALSE;
5330 xtensa_clear_insn_labels ();
5334 /* We had an error while parsing an instruction. The string might look
5335 like this: "insn arg1, arg2 }". If so, we need to see the closing
5336 brace and reset some fields. Otherwise, the vinsn never gets closed
5337 and the num_slots field will grow past the end of the array of slots,
5338 and bad things happen. */
5340 static void
5341 error_reset_cur_vinsn (void)
5343 if (cur_vinsn.inside_bundle)
5345 if (*input_line_pointer == '}'
5346 || *(input_line_pointer - 1) == '}'
5347 || *(input_line_pointer - 2) == '}')
5348 xg_clear_vinsn (&cur_vinsn);
5353 void
5354 md_assemble (char *str)
5356 xtensa_isa isa = xtensa_default_isa;
5357 char *opname;
5358 unsigned opnamelen;
5359 bfd_boolean has_underbar = FALSE;
5360 char *arg_strings[MAX_INSN_ARGS];
5361 int num_args;
5362 TInsn orig_insn; /* Original instruction from the input. */
5364 tinsn_init (&orig_insn);
5366 /* Split off the opcode. */
5367 opnamelen = strspn (str, "abcdefghijklmnopqrstuvwxyz_/0123456789.");
5368 opname = xmalloc (opnamelen + 1);
5369 memcpy (opname, str, opnamelen);
5370 opname[opnamelen] = '\0';
5372 num_args = tokenize_arguments (arg_strings, str + opnamelen);
5373 if (num_args == -1)
5375 as_bad (_("syntax error"));
5376 return;
5379 if (xg_translate_idioms (&opname, &num_args, arg_strings))
5380 return;
5382 /* Check for an underbar prefix. */
5383 if (*opname == '_')
5385 has_underbar = TRUE;
5386 opname += 1;
5389 orig_insn.insn_type = ITYPE_INSN;
5390 orig_insn.ntok = 0;
5391 orig_insn.is_specific_opcode = (has_underbar || !use_transform ());
5392 orig_insn.opcode = xtensa_opcode_lookup (isa, opname);
5394 /* Special case: Check for "CALLXn.TLS" psuedo op. If found, grab its
5395 extra argument and set the opcode to "CALLXn". */
5396 if (orig_insn.opcode == XTENSA_UNDEFINED
5397 && strncasecmp (opname, "callx", 5) == 0)
5399 unsigned long window_size;
5400 char *suffix;
5402 window_size = strtoul (opname + 5, &suffix, 10);
5403 if (suffix != opname + 5
5404 && (window_size == 0
5405 || window_size == 4
5406 || window_size == 8
5407 || window_size == 12)
5408 && strcasecmp (suffix, ".tls") == 0)
5410 switch (window_size)
5412 case 0: orig_insn.opcode = xtensa_callx0_opcode; break;
5413 case 4: orig_insn.opcode = xtensa_callx4_opcode; break;
5414 case 8: orig_insn.opcode = xtensa_callx8_opcode; break;
5415 case 12: orig_insn.opcode = xtensa_callx12_opcode; break;
5418 if (num_args != 2)
5419 as_bad (_("wrong number of operands for '%s'"), opname);
5420 else
5422 bfd_reloc_code_real_type reloc;
5423 char *old_input_line_pointer;
5424 expressionS *tok = &orig_insn.extra_arg;
5425 segT t;
5427 old_input_line_pointer = input_line_pointer;
5428 input_line_pointer = arg_strings[num_args - 1];
5430 t = expression (tok);
5431 if (tok->X_op == O_symbol
5432 && ((reloc = xtensa_elf_suffix (&input_line_pointer, tok))
5433 == BFD_RELOC_XTENSA_TLS_CALL))
5434 tok->X_op = map_suffix_reloc_to_operator (reloc);
5435 else
5436 as_bad (_("bad relocation expression for '%s'"), opname);
5438 input_line_pointer = old_input_line_pointer;
5439 num_args -= 1;
5444 /* Special case: Check for "j.l" psuedo op. */
5445 if (orig_insn.opcode == XTENSA_UNDEFINED
5446 && strncasecmp (opname, "j.l", 3) == 0)
5448 if (num_args != 2)
5449 as_bad (_("wrong number of operands for '%s'"), opname);
5450 else
5452 char *old_input_line_pointer;
5453 expressionS *tok = &orig_insn.extra_arg;
5455 old_input_line_pointer = input_line_pointer;
5456 input_line_pointer = arg_strings[num_args - 1];
5458 expression_maybe_register (xtensa_jx_opcode, 0, tok);
5459 input_line_pointer = old_input_line_pointer;
5461 num_args -= 1;
5462 orig_insn.opcode = xtensa_j_opcode;
5466 if (orig_insn.opcode == XTENSA_UNDEFINED)
5468 xtensa_format fmt = xtensa_format_lookup (isa, opname);
5469 if (fmt == XTENSA_UNDEFINED)
5471 as_bad (_("unknown opcode or format name '%s'"), opname);
5472 error_reset_cur_vinsn ();
5473 return;
5475 if (!cur_vinsn.inside_bundle)
5477 as_bad (_("format names only valid inside bundles"));
5478 error_reset_cur_vinsn ();
5479 return;
5481 if (cur_vinsn.format != XTENSA_UNDEFINED)
5482 as_warn (_("multiple formats specified for one bundle; using '%s'"),
5483 opname);
5484 cur_vinsn.format = fmt;
5485 free (has_underbar ? opname - 1 : opname);
5486 error_reset_cur_vinsn ();
5487 return;
5490 /* Parse the arguments. */
5491 if (parse_arguments (&orig_insn, num_args, arg_strings))
5493 as_bad (_("syntax error"));
5494 error_reset_cur_vinsn ();
5495 return;
5498 /* Free the opcode and argument strings, now that they've been parsed. */
5499 free (has_underbar ? opname - 1 : opname);
5500 opname = 0;
5501 while (num_args-- > 0)
5502 free (arg_strings[num_args]);
5504 /* Get expressions for invisible operands. */
5505 if (get_invisible_operands (&orig_insn))
5507 error_reset_cur_vinsn ();
5508 return;
5511 /* Check for the right number and type of arguments. */
5512 if (tinsn_check_arguments (&orig_insn))
5514 error_reset_cur_vinsn ();
5515 return;
5518 /* Record the line number for each TInsn, because a FLIX bundle may be
5519 spread across multiple input lines and individual instructions may be
5520 moved around in some cases. */
5521 orig_insn.loc_directive_seen = dwarf2_loc_directive_seen;
5522 dwarf2_where (&orig_insn.debug_line);
5523 dwarf2_consume_line_info ();
5525 xg_add_branch_and_loop_targets (&orig_insn);
5527 /* Check that immediate value for ENTRY is >= 16. */
5528 if (orig_insn.opcode == xtensa_entry_opcode && orig_insn.ntok >= 3)
5530 expressionS *exp = &orig_insn.tok[2];
5531 if (exp->X_op == O_constant && exp->X_add_number < 16)
5532 as_warn (_("entry instruction with stack decrement < 16"));
5535 /* Finish it off:
5536 assemble_tokens (opcode, tok, ntok);
5537 expand the tokens from the orig_insn into the
5538 stack of instructions that will not expand
5539 unless required at relaxation time. */
5541 if (!cur_vinsn.inside_bundle)
5542 emit_single_op (&orig_insn);
5543 else /* We are inside a bundle. */
5545 cur_vinsn.slots[cur_vinsn.num_slots] = orig_insn;
5546 cur_vinsn.num_slots++;
5547 if (*input_line_pointer == '}'
5548 || *(input_line_pointer - 1) == '}'
5549 || *(input_line_pointer - 2) == '}')
5550 finish_vinsn (&cur_vinsn);
5553 /* We've just emitted a new instruction so clear the list of labels. */
5554 xtensa_clear_insn_labels ();
5558 /* HANDLE_ALIGN hook */
5560 /* For a .align directive, we mark the previous block with the alignment
5561 information. This will be placed in the object file in the
5562 property section corresponding to this section. */
5564 void
5565 xtensa_handle_align (fragS *fragP)
5567 if (linkrelax
5568 && ! fragP->tc_frag_data.is_literal
5569 && (fragP->fr_type == rs_align
5570 || fragP->fr_type == rs_align_code)
5571 && fragP->fr_address + fragP->fr_fix > 0
5572 && fragP->fr_offset > 0
5573 && now_seg != bss_section)
5575 fragP->tc_frag_data.is_align = TRUE;
5576 fragP->tc_frag_data.alignment = fragP->fr_offset;
5579 if (fragP->fr_type == rs_align_test)
5581 int count;
5582 count = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
5583 if (count != 0)
5584 as_bad_where (fragP->fr_file, fragP->fr_line,
5585 _("unaligned entry instruction"));
5588 if (linkrelax && fragP->fr_type == rs_org)
5589 fragP->fr_subtype = RELAX_ORG;
5593 /* TC_FRAG_INIT hook */
5595 void
5596 xtensa_frag_init (fragS *frag)
5598 xtensa_set_frag_assembly_state (frag);
5602 symbolS *
5603 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
5605 return NULL;
5609 /* Round up a section size to the appropriate boundary. */
5611 valueT
5612 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
5614 return size; /* Byte alignment is fine. */
5618 long
5619 md_pcrel_from (fixS *fixP)
5621 char *insn_p;
5622 static xtensa_insnbuf insnbuf = NULL;
5623 static xtensa_insnbuf slotbuf = NULL;
5624 int opnum;
5625 uint32 opnd_value;
5626 xtensa_opcode opcode;
5627 xtensa_format fmt;
5628 int slot;
5629 xtensa_isa isa = xtensa_default_isa;
5630 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
5631 bfd_boolean alt_reloc;
5633 if (fixP->fx_r_type == BFD_RELOC_XTENSA_ASM_EXPAND)
5634 return 0;
5636 if (fixP->fx_r_type == BFD_RELOC_32_PCREL)
5637 return addr;
5639 if (!insnbuf)
5641 insnbuf = xtensa_insnbuf_alloc (isa);
5642 slotbuf = xtensa_insnbuf_alloc (isa);
5645 insn_p = &fixP->fx_frag->fr_literal[fixP->fx_where];
5646 xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) insn_p, 0);
5647 fmt = xtensa_format_decode (isa, insnbuf);
5649 if (fmt == XTENSA_UNDEFINED)
5650 as_fatal (_("bad instruction format"));
5652 if (decode_reloc (fixP->fx_r_type, &slot, &alt_reloc) != 0)
5653 as_fatal (_("invalid relocation"));
5655 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
5656 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
5658 /* Check for "alternate" relocations (operand not specified). None
5659 of the current uses for these are really PC-relative. */
5660 if (alt_reloc || opcode == xtensa_const16_opcode)
5662 if (opcode != xtensa_l32r_opcode
5663 && opcode != xtensa_const16_opcode)
5664 as_fatal (_("invalid relocation for '%s' instruction"),
5665 xtensa_opcode_name (isa, opcode));
5666 return 0;
5669 opnum = get_relaxable_immed (opcode);
5670 opnd_value = 0;
5671 if (xtensa_operand_is_PCrelative (isa, opcode, opnum) != 1
5672 || xtensa_operand_do_reloc (isa, opcode, opnum, &opnd_value, addr))
5674 as_bad_where (fixP->fx_file,
5675 fixP->fx_line,
5676 _("invalid relocation for operand %d of '%s'"),
5677 opnum, xtensa_opcode_name (isa, opcode));
5678 return 0;
5680 return 0 - opnd_value;
5684 /* TC_FORCE_RELOCATION hook */
5687 xtensa_force_relocation (fixS *fix)
5689 switch (fix->fx_r_type)
5691 case BFD_RELOC_XTENSA_ASM_EXPAND:
5692 case BFD_RELOC_XTENSA_SLOT0_ALT:
5693 case BFD_RELOC_XTENSA_SLOT1_ALT:
5694 case BFD_RELOC_XTENSA_SLOT2_ALT:
5695 case BFD_RELOC_XTENSA_SLOT3_ALT:
5696 case BFD_RELOC_XTENSA_SLOT4_ALT:
5697 case BFD_RELOC_XTENSA_SLOT5_ALT:
5698 case BFD_RELOC_XTENSA_SLOT6_ALT:
5699 case BFD_RELOC_XTENSA_SLOT7_ALT:
5700 case BFD_RELOC_XTENSA_SLOT8_ALT:
5701 case BFD_RELOC_XTENSA_SLOT9_ALT:
5702 case BFD_RELOC_XTENSA_SLOT10_ALT:
5703 case BFD_RELOC_XTENSA_SLOT11_ALT:
5704 case BFD_RELOC_XTENSA_SLOT12_ALT:
5705 case BFD_RELOC_XTENSA_SLOT13_ALT:
5706 case BFD_RELOC_XTENSA_SLOT14_ALT:
5707 return 1;
5708 default:
5709 break;
5712 if (linkrelax && fix->fx_addsy
5713 && relaxable_section (S_GET_SEGMENT (fix->fx_addsy)))
5714 return 1;
5716 return generic_force_reloc (fix);
5720 /* TC_VALIDATE_FIX_SUB hook */
5723 xtensa_validate_fix_sub (fixS *fix)
5725 segT add_symbol_segment, sub_symbol_segment;
5727 /* The difference of two symbols should be resolved by the assembler when
5728 linkrelax is not set. If the linker may relax the section containing
5729 the symbols, then an Xtensa DIFF relocation must be generated so that
5730 the linker knows to adjust the difference value. */
5731 if (!linkrelax || fix->fx_addsy == NULL)
5732 return 0;
5734 /* Make sure both symbols are in the same segment, and that segment is
5735 "normal" and relaxable. If the segment is not "normal", then the
5736 fix is not valid. If the segment is not "relaxable", then the fix
5737 should have been handled earlier. */
5738 add_symbol_segment = S_GET_SEGMENT (fix->fx_addsy);
5739 if (! SEG_NORMAL (add_symbol_segment) ||
5740 ! relaxable_section (add_symbol_segment))
5741 return 0;
5742 sub_symbol_segment = S_GET_SEGMENT (fix->fx_subsy);
5743 return (sub_symbol_segment == add_symbol_segment);
5747 /* NO_PSEUDO_DOT hook */
5749 /* This function has nothing to do with pseudo dots, but this is the
5750 nearest macro to where the check needs to take place. FIXME: This
5751 seems wrong. */
5753 bfd_boolean
5754 xtensa_check_inside_bundle (void)
5756 if (cur_vinsn.inside_bundle && input_line_pointer[-1] == '.')
5757 as_bad (_("directives are not valid inside bundles"));
5759 /* This function must always return FALSE because it is called via a
5760 macro that has nothing to do with bundling. */
5761 return FALSE;
5765 /* md_elf_section_change_hook */
5767 void
5768 xtensa_elf_section_change_hook (void)
5770 /* Set up the assembly state. */
5771 if (!frag_now->tc_frag_data.is_assembly_state_set)
5772 xtensa_set_frag_assembly_state (frag_now);
5776 /* tc_fix_adjustable hook */
5778 bfd_boolean
5779 xtensa_fix_adjustable (fixS *fixP)
5781 /* We need the symbol name for the VTABLE entries. */
5782 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
5783 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
5784 return 0;
5786 return 1;
5790 /* tc_symbol_new_hook */
5792 symbolS *expr_symbols = NULL;
5794 void
5795 xtensa_symbol_new_hook (symbolS *sym)
5797 if (is_leb128_expr && S_GET_SEGMENT (sym) == expr_section)
5799 symbol_get_tc (sym)->next_expr_symbol = expr_symbols;
5800 expr_symbols = sym;
5805 void
5806 md_apply_fix (fixS *fixP, valueT *valP, segT seg)
5808 char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
5809 valueT val = 0;
5811 /* Subtracted symbols are only allowed for a few relocation types, and
5812 unless linkrelax is enabled, they should not make it to this point. */
5813 if (fixP->fx_subsy && !(linkrelax && (fixP->fx_r_type == BFD_RELOC_32
5814 || fixP->fx_r_type == BFD_RELOC_16
5815 || fixP->fx_r_type == BFD_RELOC_8)))
5816 as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
5818 switch (fixP->fx_r_type)
5820 case BFD_RELOC_32_PCREL:
5821 case BFD_RELOC_32:
5822 case BFD_RELOC_16:
5823 case BFD_RELOC_8:
5824 if (fixP->fx_subsy)
5826 switch (fixP->fx_r_type)
5828 case BFD_RELOC_8:
5829 fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF8;
5830 break;
5831 case BFD_RELOC_16:
5832 fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF16;
5833 break;
5834 case BFD_RELOC_32:
5835 fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF32;
5836 break;
5837 default:
5838 break;
5841 val = (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset
5842 - S_GET_VALUE (fixP->fx_subsy));
5844 /* The difference value gets written out, and the DIFF reloc
5845 identifies the address of the subtracted symbol (i.e., the one
5846 with the lowest address). */
5847 *valP = val;
5848 fixP->fx_offset -= val;
5849 fixP->fx_subsy = NULL;
5851 else if (! fixP->fx_addsy)
5853 val = *valP;
5854 fixP->fx_done = 1;
5856 /* fall through */
5858 case BFD_RELOC_XTENSA_PLT:
5859 md_number_to_chars (fixpos, val, fixP->fx_size);
5860 fixP->fx_no_overflow = 0; /* Use the standard overflow check. */
5861 break;
5863 case BFD_RELOC_XTENSA_TLSDESC_FN:
5864 case BFD_RELOC_XTENSA_TLSDESC_ARG:
5865 case BFD_RELOC_XTENSA_TLS_TPOFF:
5866 case BFD_RELOC_XTENSA_TLS_DTPOFF:
5867 S_SET_THREAD_LOCAL (fixP->fx_addsy);
5868 md_number_to_chars (fixpos, 0, fixP->fx_size);
5869 fixP->fx_no_overflow = 0; /* Use the standard overflow check. */
5870 break;
5872 case BFD_RELOC_XTENSA_SLOT0_OP:
5873 case BFD_RELOC_XTENSA_SLOT1_OP:
5874 case BFD_RELOC_XTENSA_SLOT2_OP:
5875 case BFD_RELOC_XTENSA_SLOT3_OP:
5876 case BFD_RELOC_XTENSA_SLOT4_OP:
5877 case BFD_RELOC_XTENSA_SLOT5_OP:
5878 case BFD_RELOC_XTENSA_SLOT6_OP:
5879 case BFD_RELOC_XTENSA_SLOT7_OP:
5880 case BFD_RELOC_XTENSA_SLOT8_OP:
5881 case BFD_RELOC_XTENSA_SLOT9_OP:
5882 case BFD_RELOC_XTENSA_SLOT10_OP:
5883 case BFD_RELOC_XTENSA_SLOT11_OP:
5884 case BFD_RELOC_XTENSA_SLOT12_OP:
5885 case BFD_RELOC_XTENSA_SLOT13_OP:
5886 case BFD_RELOC_XTENSA_SLOT14_OP:
5887 if (linkrelax)
5889 /* Write the tentative value of a PC-relative relocation to a
5890 local symbol into the instruction. The value will be ignored
5891 by the linker, and it makes the object file disassembly
5892 readable when all branch targets are encoded in relocations. */
5894 gas_assert (fixP->fx_addsy);
5895 if (S_GET_SEGMENT (fixP->fx_addsy) == seg
5896 && !S_FORCE_RELOC (fixP->fx_addsy, 1))
5898 val = (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset
5899 - md_pcrel_from (fixP));
5900 (void) xg_apply_fix_value (fixP, val);
5903 else if (! fixP->fx_addsy)
5905 val = *valP;
5906 if (xg_apply_fix_value (fixP, val))
5907 fixP->fx_done = 1;
5909 break;
5911 case BFD_RELOC_XTENSA_ASM_EXPAND:
5912 case BFD_RELOC_XTENSA_TLS_FUNC:
5913 case BFD_RELOC_XTENSA_TLS_ARG:
5914 case BFD_RELOC_XTENSA_TLS_CALL:
5915 case BFD_RELOC_XTENSA_SLOT0_ALT:
5916 case BFD_RELOC_XTENSA_SLOT1_ALT:
5917 case BFD_RELOC_XTENSA_SLOT2_ALT:
5918 case BFD_RELOC_XTENSA_SLOT3_ALT:
5919 case BFD_RELOC_XTENSA_SLOT4_ALT:
5920 case BFD_RELOC_XTENSA_SLOT5_ALT:
5921 case BFD_RELOC_XTENSA_SLOT6_ALT:
5922 case BFD_RELOC_XTENSA_SLOT7_ALT:
5923 case BFD_RELOC_XTENSA_SLOT8_ALT:
5924 case BFD_RELOC_XTENSA_SLOT9_ALT:
5925 case BFD_RELOC_XTENSA_SLOT10_ALT:
5926 case BFD_RELOC_XTENSA_SLOT11_ALT:
5927 case BFD_RELOC_XTENSA_SLOT12_ALT:
5928 case BFD_RELOC_XTENSA_SLOT13_ALT:
5929 case BFD_RELOC_XTENSA_SLOT14_ALT:
5930 /* These all need to be resolved at link-time. Do nothing now. */
5931 break;
5933 case BFD_RELOC_VTABLE_INHERIT:
5934 case BFD_RELOC_VTABLE_ENTRY:
5935 fixP->fx_done = 0;
5936 break;
5938 default:
5939 as_bad (_("unhandled local relocation fix %s"),
5940 bfd_get_reloc_code_name (fixP->fx_r_type));
5945 char *
5946 md_atof (int type, char *litP, int *sizeP)
5948 return ieee_md_atof (type, litP, sizeP, target_big_endian);
5953 md_estimate_size_before_relax (fragS *fragP, segT seg ATTRIBUTE_UNUSED)
5955 return total_frag_text_expansion (fragP);
5959 /* Translate internal representation of relocation info to BFD target
5960 format. */
5962 arelent *
5963 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
5965 arelent *reloc;
5967 reloc = (arelent *) xmalloc (sizeof (arelent));
5968 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5969 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
5970 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
5972 /* Make sure none of our internal relocations make it this far.
5973 They'd better have been fully resolved by this point. */
5974 gas_assert ((int) fixp->fx_r_type > 0);
5976 reloc->addend = fixp->fx_offset;
5978 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
5979 if (reloc->howto == NULL)
5981 as_bad_where (fixp->fx_file, fixp->fx_line,
5982 _("cannot represent `%s' relocation in object file"),
5983 bfd_get_reloc_code_name (fixp->fx_r_type));
5984 free (reloc->sym_ptr_ptr);
5985 free (reloc);
5986 return NULL;
5989 if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
5990 as_fatal (_("internal error; cannot generate `%s' relocation"),
5991 bfd_get_reloc_code_name (fixp->fx_r_type));
5993 return reloc;
5997 /* Checks for resource conflicts between instructions. */
5999 /* The func unit stuff could be implemented as bit-vectors rather
6000 than the iterative approach here. If it ends up being too
6001 slow, we will switch it. */
6003 resource_table *
6004 new_resource_table (void *data,
6005 int cycles,
6006 int nu,
6007 unit_num_copies_func uncf,
6008 opcode_num_units_func onuf,
6009 opcode_funcUnit_use_unit_func ouuf,
6010 opcode_funcUnit_use_stage_func ousf)
6012 int i;
6013 resource_table *rt = (resource_table *) xmalloc (sizeof (resource_table));
6014 rt->data = data;
6015 rt->cycles = cycles;
6016 rt->allocated_cycles = cycles;
6017 rt->num_units = nu;
6018 rt->unit_num_copies = uncf;
6019 rt->opcode_num_units = onuf;
6020 rt->opcode_unit_use = ouuf;
6021 rt->opcode_unit_stage = ousf;
6023 rt->units = (unsigned char **) xcalloc (cycles, sizeof (unsigned char *));
6024 for (i = 0; i < cycles; i++)
6025 rt->units[i] = (unsigned char *) xcalloc (nu, sizeof (unsigned char));
6027 return rt;
6031 void
6032 clear_resource_table (resource_table *rt)
6034 int i, j;
6035 for (i = 0; i < rt->allocated_cycles; i++)
6036 for (j = 0; j < rt->num_units; j++)
6037 rt->units[i][j] = 0;
6041 /* We never shrink it, just fake it into thinking so. */
6043 void
6044 resize_resource_table (resource_table *rt, int cycles)
6046 int i, old_cycles;
6048 rt->cycles = cycles;
6049 if (cycles <= rt->allocated_cycles)
6050 return;
6052 old_cycles = rt->allocated_cycles;
6053 rt->allocated_cycles = cycles;
6055 rt->units = xrealloc (rt->units,
6056 rt->allocated_cycles * sizeof (unsigned char *));
6057 for (i = 0; i < old_cycles; i++)
6058 rt->units[i] = xrealloc (rt->units[i],
6059 rt->num_units * sizeof (unsigned char));
6060 for (i = old_cycles; i < cycles; i++)
6061 rt->units[i] = xcalloc (rt->num_units, sizeof (unsigned char));
6065 bfd_boolean
6066 resources_available (resource_table *rt, xtensa_opcode opcode, int cycle)
6068 int i;
6069 int uses = (rt->opcode_num_units) (rt->data, opcode);
6071 for (i = 0; i < uses; i++)
6073 xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
6074 int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
6075 int copies_in_use = rt->units[stage + cycle][unit];
6076 int copies = (rt->unit_num_copies) (rt->data, unit);
6077 if (copies_in_use >= copies)
6078 return FALSE;
6080 return TRUE;
6084 void
6085 reserve_resources (resource_table *rt, xtensa_opcode opcode, int cycle)
6087 int i;
6088 int uses = (rt->opcode_num_units) (rt->data, opcode);
6090 for (i = 0; i < uses; i++)
6092 xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
6093 int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
6094 /* Note that this allows resources to be oversubscribed. That's
6095 essential to the way the optional scheduler works.
6096 resources_available reports when a resource is over-subscribed,
6097 so it's easy to tell. */
6098 rt->units[stage + cycle][unit]++;
6103 void
6104 release_resources (resource_table *rt, xtensa_opcode opcode, int cycle)
6106 int i;
6107 int uses = (rt->opcode_num_units) (rt->data, opcode);
6109 for (i = 0; i < uses; i++)
6111 xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
6112 int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
6113 gas_assert (rt->units[stage + cycle][unit] > 0);
6114 rt->units[stage + cycle][unit]--;
6119 /* Wrapper functions make parameterized resource reservation
6120 more convenient. */
6123 opcode_funcUnit_use_unit (void *data, xtensa_opcode opcode, int idx)
6125 xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx);
6126 return use->unit;
6131 opcode_funcUnit_use_stage (void *data, xtensa_opcode opcode, int idx)
6133 xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx);
6134 return use->stage;
6138 /* Note that this function does not check issue constraints, but
6139 solely whether the hardware is available to execute the given
6140 instructions together. It also doesn't check if the tinsns
6141 write the same state, or access the same tieports. That is
6142 checked by check_t1_t2_reads_and_writes. */
6144 static bfd_boolean
6145 resources_conflict (vliw_insn *vinsn)
6147 int i;
6148 static resource_table *rt = NULL;
6150 /* This is the most common case by far. Optimize it. */
6151 if (vinsn->num_slots == 1)
6152 return FALSE;
6154 if (rt == NULL)
6156 xtensa_isa isa = xtensa_default_isa;
6157 rt = new_resource_table
6158 (isa, xtensa_num_pipe_stages,
6159 xtensa_isa_num_funcUnits (isa),
6160 (unit_num_copies_func) xtensa_funcUnit_num_copies,
6161 (opcode_num_units_func) xtensa_opcode_num_funcUnit_uses,
6162 opcode_funcUnit_use_unit,
6163 opcode_funcUnit_use_stage);
6166 clear_resource_table (rt);
6168 for (i = 0; i < vinsn->num_slots; i++)
6170 if (!resources_available (rt, vinsn->slots[i].opcode, 0))
6171 return TRUE;
6172 reserve_resources (rt, vinsn->slots[i].opcode, 0);
6175 return FALSE;
6179 /* finish_vinsn, emit_single_op and helper functions. */
6181 static bfd_boolean find_vinsn_conflicts (vliw_insn *);
6182 static xtensa_format xg_find_narrowest_format (vliw_insn *);
6183 static void xg_assemble_vliw_tokens (vliw_insn *);
6186 /* We have reached the end of a bundle; emit into the frag. */
6188 static void
6189 finish_vinsn (vliw_insn *vinsn)
6191 IStack slotstack;
6192 int i;
6193 char *file_name;
6194 unsigned line;
6196 if (find_vinsn_conflicts (vinsn))
6198 xg_clear_vinsn (vinsn);
6199 return;
6202 /* First, find a format that works. */
6203 if (vinsn->format == XTENSA_UNDEFINED)
6204 vinsn->format = xg_find_narrowest_format (vinsn);
6206 if (xtensa_format_num_slots (xtensa_default_isa, vinsn->format) > 1
6207 && produce_flix == FLIX_NONE)
6209 as_bad (_("The option \"--no-allow-flix\" prohibits multi-slot flix."));
6210 xg_clear_vinsn (vinsn);
6211 return;
6214 if (vinsn->format == XTENSA_UNDEFINED)
6216 as_where (&file_name, &line);
6217 as_bad_where (file_name, line,
6218 _("couldn't find a valid instruction format"));
6219 fprintf (stderr, _(" ops were: "));
6220 for (i = 0; i < vinsn->num_slots; i++)
6221 fprintf (stderr, _(" %s;"),
6222 xtensa_opcode_name (xtensa_default_isa,
6223 vinsn->slots[i].opcode));
6224 fprintf (stderr, _("\n"));
6225 xg_clear_vinsn (vinsn);
6226 return;
6229 if (vinsn->num_slots
6230 != xtensa_format_num_slots (xtensa_default_isa, vinsn->format))
6232 as_bad (_("format '%s' allows %d slots, but there are %d opcodes"),
6233 xtensa_format_name (xtensa_default_isa, vinsn->format),
6234 xtensa_format_num_slots (xtensa_default_isa, vinsn->format),
6235 vinsn->num_slots);
6236 xg_clear_vinsn (vinsn);
6237 return;
6240 if (resources_conflict (vinsn))
6242 as_where (&file_name, &line);
6243 as_bad_where (file_name, line, _("illegal resource usage in bundle"));
6244 fprintf (stderr, " ops were: ");
6245 for (i = 0; i < vinsn->num_slots; i++)
6246 fprintf (stderr, " %s;",
6247 xtensa_opcode_name (xtensa_default_isa,
6248 vinsn->slots[i].opcode));
6249 fprintf (stderr, "\n");
6250 xg_clear_vinsn (vinsn);
6251 return;
6254 for (i = 0; i < vinsn->num_slots; i++)
6256 if (vinsn->slots[i].opcode != XTENSA_UNDEFINED)
6258 symbolS *lit_sym = NULL;
6259 int j;
6260 bfd_boolean e = FALSE;
6261 bfd_boolean saved_density = density_supported;
6263 /* We don't want to narrow ops inside multi-slot bundles. */
6264 if (vinsn->num_slots > 1)
6265 density_supported = FALSE;
6267 istack_init (&slotstack);
6268 if (vinsn->slots[i].opcode == xtensa_nop_opcode)
6270 vinsn->slots[i].opcode =
6271 xtensa_format_slot_nop_opcode (xtensa_default_isa,
6272 vinsn->format, i);
6273 vinsn->slots[i].ntok = 0;
6276 if (xg_expand_assembly_insn (&slotstack, &vinsn->slots[i]))
6278 e = TRUE;
6279 continue;
6282 density_supported = saved_density;
6284 if (e)
6286 xg_clear_vinsn (vinsn);
6287 return;
6290 for (j = 0; j < slotstack.ninsn; j++)
6292 TInsn *insn = &slotstack.insn[j];
6293 if (insn->insn_type == ITYPE_LITERAL)
6295 gas_assert (lit_sym == NULL);
6296 lit_sym = xg_assemble_literal (insn);
6298 else
6300 gas_assert (insn->insn_type == ITYPE_INSN);
6301 if (lit_sym)
6302 xg_resolve_literals (insn, lit_sym);
6303 if (j != slotstack.ninsn - 1)
6304 emit_single_op (insn);
6308 if (vinsn->num_slots > 1)
6310 if (opcode_fits_format_slot
6311 (slotstack.insn[slotstack.ninsn - 1].opcode,
6312 vinsn->format, i))
6314 vinsn->slots[i] = slotstack.insn[slotstack.ninsn - 1];
6316 else
6318 emit_single_op (&slotstack.insn[slotstack.ninsn - 1]);
6319 if (vinsn->format == XTENSA_UNDEFINED)
6320 vinsn->slots[i].opcode = xtensa_nop_opcode;
6321 else
6322 vinsn->slots[i].opcode
6323 = xtensa_format_slot_nop_opcode (xtensa_default_isa,
6324 vinsn->format, i);
6326 vinsn->slots[i].ntok = 0;
6329 else
6331 vinsn->slots[0] = slotstack.insn[slotstack.ninsn - 1];
6332 vinsn->format = XTENSA_UNDEFINED;
6337 /* Now check resource conflicts on the modified bundle. */
6338 if (resources_conflict (vinsn))
6340 as_where (&file_name, &line);
6341 as_bad_where (file_name, line, _("illegal resource usage in bundle"));
6342 fprintf (stderr, " ops were: ");
6343 for (i = 0; i < vinsn->num_slots; i++)
6344 fprintf (stderr, " %s;",
6345 xtensa_opcode_name (xtensa_default_isa,
6346 vinsn->slots[i].opcode));
6347 fprintf (stderr, "\n");
6348 xg_clear_vinsn (vinsn);
6349 return;
6352 /* First, find a format that works. */
6353 if (vinsn->format == XTENSA_UNDEFINED)
6354 vinsn->format = xg_find_narrowest_format (vinsn);
6356 xg_assemble_vliw_tokens (vinsn);
6358 xg_clear_vinsn (vinsn);
6362 /* Given an vliw instruction, what conflicts are there in register
6363 usage and in writes to states and queues?
6365 This function does two things:
6366 1. Reports an error when a vinsn contains illegal combinations
6367 of writes to registers states or queues.
6368 2. Marks individual tinsns as not relaxable if the combination
6369 contains antidependencies.
6371 Job 2 handles things like swap semantics in instructions that need
6372 to be relaxed. For example,
6374 addi a0, a1, 100000
6376 normally would be relaxed to
6378 l32r a0, some_label
6379 add a0, a1, a0
6381 _but_, if the above instruction is bundled with an a0 reader, e.g.,
6383 { addi a0, a1, 10000 ; add a2, a0, a4 ; }
6385 then we can't relax it into
6387 l32r a0, some_label
6388 { add a0, a1, a0 ; add a2, a0, a4 ; }
6390 because the value of a0 is trashed before the second add can read it. */
6392 static char check_t1_t2_reads_and_writes (TInsn *, TInsn *);
6394 static bfd_boolean
6395 find_vinsn_conflicts (vliw_insn *vinsn)
6397 int i, j;
6398 int branches = 0;
6399 xtensa_isa isa = xtensa_default_isa;
6401 gas_assert (!past_xtensa_end);
6403 for (i = 0 ; i < vinsn->num_slots; i++)
6405 TInsn *op1 = &vinsn->slots[i];
6406 if (op1->is_specific_opcode)
6407 op1->keep_wide = TRUE;
6408 else
6409 op1->keep_wide = FALSE;
6412 for (i = 0 ; i < vinsn->num_slots; i++)
6414 TInsn *op1 = &vinsn->slots[i];
6416 if (xtensa_opcode_is_branch (isa, op1->opcode) == 1)
6417 branches++;
6419 for (j = 0; j < vinsn->num_slots; j++)
6421 if (i != j)
6423 TInsn *op2 = &vinsn->slots[j];
6424 char conflict_type = check_t1_t2_reads_and_writes (op1, op2);
6425 switch (conflict_type)
6427 case 'c':
6428 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same register"),
6429 xtensa_opcode_name (isa, op1->opcode), i,
6430 xtensa_opcode_name (isa, op2->opcode), j);
6431 return TRUE;
6432 case 'd':
6433 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same state"),
6434 xtensa_opcode_name (isa, op1->opcode), i,
6435 xtensa_opcode_name (isa, op2->opcode), j);
6436 return TRUE;
6437 case 'e':
6438 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same port"),
6439 xtensa_opcode_name (isa, op1->opcode), i,
6440 xtensa_opcode_name (isa, op2->opcode), j);
6441 return TRUE;
6442 case 'f':
6443 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) both have volatile port accesses"),
6444 xtensa_opcode_name (isa, op1->opcode), i,
6445 xtensa_opcode_name (isa, op2->opcode), j);
6446 return TRUE;
6447 default:
6448 /* Everything is OK. */
6449 break;
6451 op2->is_specific_opcode = (op2->is_specific_opcode
6452 || conflict_type == 'a');
6457 if (branches > 1)
6459 as_bad (_("multiple branches or jumps in the same bundle"));
6460 return TRUE;
6463 return FALSE;
6467 /* Check how the state used by t1 and t2 relate.
6468 Cases found are:
6470 case A: t1 reads a register t2 writes (an antidependency within a bundle)
6471 case B: no relationship between what is read and written (both could
6472 read the same reg though)
6473 case C: t1 writes a register t2 writes (a register conflict within a
6474 bundle)
6475 case D: t1 writes a state that t2 also writes
6476 case E: t1 writes a tie queue that t2 also writes
6477 case F: two volatile queue accesses
6480 static char
6481 check_t1_t2_reads_and_writes (TInsn *t1, TInsn *t2)
6483 xtensa_isa isa = xtensa_default_isa;
6484 xtensa_regfile t1_regfile, t2_regfile;
6485 int t1_reg, t2_reg;
6486 int t1_base_reg, t1_last_reg;
6487 int t2_base_reg, t2_last_reg;
6488 char t1_inout, t2_inout;
6489 int i, j;
6490 char conflict = 'b';
6491 int t1_states;
6492 int t2_states;
6493 int t1_interfaces;
6494 int t2_interfaces;
6495 bfd_boolean t1_volatile = FALSE;
6496 bfd_boolean t2_volatile = FALSE;
6498 /* Check registers. */
6499 for (j = 0; j < t2->ntok; j++)
6501 if (xtensa_operand_is_register (isa, t2->opcode, j) != 1)
6502 continue;
6504 t2_regfile = xtensa_operand_regfile (isa, t2->opcode, j);
6505 t2_base_reg = t2->tok[j].X_add_number;
6506 t2_last_reg = t2_base_reg + xtensa_operand_num_regs (isa, t2->opcode, j);
6508 for (i = 0; i < t1->ntok; i++)
6510 if (xtensa_operand_is_register (isa, t1->opcode, i) != 1)
6511 continue;
6513 t1_regfile = xtensa_operand_regfile (isa, t1->opcode, i);
6515 if (t1_regfile != t2_regfile)
6516 continue;
6518 t1_inout = xtensa_operand_inout (isa, t1->opcode, i);
6519 t2_inout = xtensa_operand_inout (isa, t2->opcode, j);
6521 if (xtensa_operand_is_known_reg (isa, t1->opcode, i) == 0
6522 || xtensa_operand_is_known_reg (isa, t2->opcode, j) == 0)
6524 if (t1_inout == 'm' || t1_inout == 'o'
6525 || t2_inout == 'm' || t2_inout == 'o')
6527 conflict = 'a';
6528 continue;
6532 t1_base_reg = t1->tok[i].X_add_number;
6533 t1_last_reg = (t1_base_reg
6534 + xtensa_operand_num_regs (isa, t1->opcode, i));
6536 for (t1_reg = t1_base_reg; t1_reg < t1_last_reg; t1_reg++)
6538 for (t2_reg = t2_base_reg; t2_reg < t2_last_reg; t2_reg++)
6540 if (t1_reg != t2_reg)
6541 continue;
6543 if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o'))
6545 conflict = 'a';
6546 continue;
6549 if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o'))
6551 conflict = 'a';
6552 continue;
6555 if (t1_inout != 'i' && t2_inout != 'i')
6556 return 'c';
6562 /* Check states. */
6563 t1_states = xtensa_opcode_num_stateOperands (isa, t1->opcode);
6564 t2_states = xtensa_opcode_num_stateOperands (isa, t2->opcode);
6565 for (j = 0; j < t2_states; j++)
6567 xtensa_state t2_so = xtensa_stateOperand_state (isa, t2->opcode, j);
6568 t2_inout = xtensa_stateOperand_inout (isa, t2->opcode, j);
6569 for (i = 0; i < t1_states; i++)
6571 xtensa_state t1_so = xtensa_stateOperand_state (isa, t1->opcode, i);
6572 t1_inout = xtensa_stateOperand_inout (isa, t1->opcode, i);
6573 if (t1_so != t2_so || xtensa_state_is_shared_or (isa, t1_so) == 1)
6574 continue;
6576 if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o'))
6578 conflict = 'a';
6579 continue;
6582 if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o'))
6584 conflict = 'a';
6585 continue;
6588 if (t1_inout != 'i' && t2_inout != 'i')
6589 return 'd';
6593 /* Check tieports. */
6594 t1_interfaces = xtensa_opcode_num_interfaceOperands (isa, t1->opcode);
6595 t2_interfaces = xtensa_opcode_num_interfaceOperands (isa, t2->opcode);
6596 for (j = 0; j < t2_interfaces; j++)
6598 xtensa_interface t2_int
6599 = xtensa_interfaceOperand_interface (isa, t2->opcode, j);
6600 int t2_class = xtensa_interface_class_id (isa, t2_int);
6602 t2_inout = xtensa_interface_inout (isa, t2_int);
6603 if (xtensa_interface_has_side_effect (isa, t2_int) == 1)
6604 t2_volatile = TRUE;
6606 for (i = 0; i < t1_interfaces; i++)
6608 xtensa_interface t1_int
6609 = xtensa_interfaceOperand_interface (isa, t1->opcode, j);
6610 int t1_class = xtensa_interface_class_id (isa, t1_int);
6612 t1_inout = xtensa_interface_inout (isa, t1_int);
6613 if (xtensa_interface_has_side_effect (isa, t1_int) == 1)
6614 t1_volatile = TRUE;
6616 if (t1_volatile && t2_volatile && (t1_class == t2_class))
6617 return 'f';
6619 if (t1_int != t2_int)
6620 continue;
6622 if (t2_inout == 'i' && t1_inout == 'o')
6624 conflict = 'a';
6625 continue;
6628 if (t1_inout == 'i' && t2_inout == 'o')
6630 conflict = 'a';
6631 continue;
6634 if (t1_inout != 'i' && t2_inout != 'i')
6635 return 'e';
6639 return conflict;
6643 static xtensa_format
6644 xg_find_narrowest_format (vliw_insn *vinsn)
6646 /* Right now we assume that the ops within the vinsn are properly
6647 ordered for the slots that the programmer wanted them in. In
6648 other words, we don't rearrange the ops in hopes of finding a
6649 better format. The scheduler handles that. */
6651 xtensa_isa isa = xtensa_default_isa;
6652 xtensa_format format;
6653 xtensa_opcode nop_opcode = xtensa_nop_opcode;
6655 if (vinsn->num_slots == 1)
6656 return xg_get_single_format (vinsn->slots[0].opcode);
6658 for (format = 0; format < xtensa_isa_num_formats (isa); format++)
6660 vliw_insn v_copy;
6661 xg_copy_vinsn (&v_copy, vinsn);
6662 if (xtensa_format_num_slots (isa, format) == v_copy.num_slots)
6664 int slot;
6665 int fit = 0;
6666 for (slot = 0; slot < v_copy.num_slots; slot++)
6668 if (v_copy.slots[slot].opcode == nop_opcode)
6670 v_copy.slots[slot].opcode =
6671 xtensa_format_slot_nop_opcode (isa, format, slot);
6672 v_copy.slots[slot].ntok = 0;
6675 if (opcode_fits_format_slot (v_copy.slots[slot].opcode,
6676 format, slot))
6677 fit++;
6678 else if (v_copy.num_slots > 1)
6680 TInsn widened;
6681 /* Try the widened version. */
6682 if (!v_copy.slots[slot].keep_wide
6683 && !v_copy.slots[slot].is_specific_opcode
6684 && xg_is_single_relaxable_insn (&v_copy.slots[slot],
6685 &widened, TRUE)
6686 && opcode_fits_format_slot (widened.opcode,
6687 format, slot))
6689 v_copy.slots[slot] = widened;
6690 fit++;
6694 if (fit == v_copy.num_slots)
6696 xg_copy_vinsn (vinsn, &v_copy);
6697 xtensa_format_encode (isa, format, vinsn->insnbuf);
6698 vinsn->format = format;
6699 break;
6704 if (format == xtensa_isa_num_formats (isa))
6705 return XTENSA_UNDEFINED;
6707 return format;
6711 /* Return the additional space needed in a frag
6712 for possible relaxations of any ops in a VLIW insn.
6713 Also fill out the relaxations that might be required of
6714 each tinsn in the vinsn. */
6716 static int
6717 relaxation_requirements (vliw_insn *vinsn, bfd_boolean *pfinish_frag)
6719 bfd_boolean finish_frag = FALSE;
6720 int extra_space = 0;
6721 int slot;
6723 for (slot = 0; slot < vinsn->num_slots; slot++)
6725 TInsn *tinsn = &vinsn->slots[slot];
6726 if (!tinsn_has_symbolic_operands (tinsn))
6728 /* A narrow instruction could be widened later to help
6729 alignment issues. */
6730 if (xg_is_single_relaxable_insn (tinsn, 0, TRUE)
6731 && !tinsn->is_specific_opcode
6732 && vinsn->num_slots == 1)
6734 /* Difference in bytes between narrow and wide insns... */
6735 extra_space += 1;
6736 tinsn->subtype = RELAX_NARROW;
6739 else
6741 if (workaround_b_j_loop_end
6742 && tinsn->opcode == xtensa_jx_opcode
6743 && use_transform ())
6745 /* Add 2 of these. */
6746 extra_space += 3; /* for the nop size */
6747 tinsn->subtype = RELAX_ADD_NOP_IF_PRE_LOOP_END;
6750 /* Need to assemble it with space for the relocation. */
6751 if (xg_is_relaxable_insn (tinsn, 0)
6752 && !tinsn->is_specific_opcode)
6754 int max_size = xg_get_max_insn_widen_size (tinsn->opcode);
6755 int max_literal_size =
6756 xg_get_max_insn_widen_literal_size (tinsn->opcode);
6758 tinsn->literal_space = max_literal_size;
6760 tinsn->subtype = RELAX_IMMED;
6761 extra_space += max_size;
6763 else
6765 /* A fix record will be added for this instruction prior
6766 to relaxation, so make it end the frag. */
6767 finish_frag = TRUE;
6771 *pfinish_frag = finish_frag;
6772 return extra_space;
6776 static void
6777 bundle_tinsn (TInsn *tinsn, vliw_insn *vinsn)
6779 xtensa_isa isa = xtensa_default_isa;
6780 int slot, chosen_slot;
6782 vinsn->format = xg_get_single_format (tinsn->opcode);
6783 gas_assert (vinsn->format != XTENSA_UNDEFINED);
6784 vinsn->num_slots = xtensa_format_num_slots (isa, vinsn->format);
6786 chosen_slot = xg_get_single_slot (tinsn->opcode);
6787 for (slot = 0; slot < vinsn->num_slots; slot++)
6789 if (slot == chosen_slot)
6790 vinsn->slots[slot] = *tinsn;
6791 else
6793 vinsn->slots[slot].opcode =
6794 xtensa_format_slot_nop_opcode (isa, vinsn->format, slot);
6795 vinsn->slots[slot].ntok = 0;
6796 vinsn->slots[slot].insn_type = ITYPE_INSN;
6802 static bfd_boolean
6803 emit_single_op (TInsn *orig_insn)
6805 int i;
6806 IStack istack; /* put instructions into here */
6807 symbolS *lit_sym = NULL;
6808 symbolS *label_sym = NULL;
6810 istack_init (&istack);
6812 /* Special-case for "movi aX, foo" which is guaranteed to need relaxing.
6813 Because the scheduling and bundling characteristics of movi and
6814 l32r or const16 are so different, we can do much better if we relax
6815 it prior to scheduling and bundling, rather than after. */
6816 if ((orig_insn->opcode == xtensa_movi_opcode
6817 || orig_insn->opcode == xtensa_movi_n_opcode)
6818 && !cur_vinsn.inside_bundle
6819 && (orig_insn->tok[1].X_op == O_symbol
6820 || orig_insn->tok[1].X_op == O_pltrel
6821 || orig_insn->tok[1].X_op == O_tlsfunc
6822 || orig_insn->tok[1].X_op == O_tlsarg
6823 || orig_insn->tok[1].X_op == O_tpoff
6824 || orig_insn->tok[1].X_op == O_dtpoff)
6825 && !orig_insn->is_specific_opcode && use_transform ())
6826 xg_assembly_relax (&istack, orig_insn, now_seg, frag_now, 0, 1, 0);
6827 else
6828 if (xg_expand_assembly_insn (&istack, orig_insn))
6829 return TRUE;
6831 for (i = 0; i < istack.ninsn; i++)
6833 TInsn *insn = &istack.insn[i];
6834 switch (insn->insn_type)
6836 case ITYPE_LITERAL:
6837 gas_assert (lit_sym == NULL);
6838 lit_sym = xg_assemble_literal (insn);
6839 break;
6840 case ITYPE_LABEL:
6842 static int relaxed_sym_idx = 0;
6843 char *label = xmalloc (strlen (FAKE_LABEL_NAME) + 12);
6844 sprintf (label, "%s_rl_%x", FAKE_LABEL_NAME, relaxed_sym_idx++);
6845 colon (label);
6846 gas_assert (label_sym == NULL);
6847 label_sym = symbol_find_or_make (label);
6848 gas_assert (label_sym);
6849 free (label);
6851 break;
6852 case ITYPE_INSN:
6854 vliw_insn v;
6855 if (lit_sym)
6856 xg_resolve_literals (insn, lit_sym);
6857 if (label_sym)
6858 xg_resolve_labels (insn, label_sym);
6859 xg_init_vinsn (&v);
6860 bundle_tinsn (insn, &v);
6861 finish_vinsn (&v);
6862 xg_free_vinsn (&v);
6864 break;
6865 default:
6866 gas_assert (0);
6867 break;
6870 return FALSE;
6874 static int
6875 total_frag_text_expansion (fragS *fragP)
6877 int slot;
6878 int total_expansion = 0;
6880 for (slot = 0; slot < config_max_slots; slot++)
6881 total_expansion += fragP->tc_frag_data.text_expansion[slot];
6883 return total_expansion;
6887 /* Emit a vliw instruction to the current fragment. */
6889 static void
6890 xg_assemble_vliw_tokens (vliw_insn *vinsn)
6892 bfd_boolean finish_frag;
6893 bfd_boolean is_jump = FALSE;
6894 bfd_boolean is_branch = FALSE;
6895 xtensa_isa isa = xtensa_default_isa;
6896 int insn_size;
6897 int extra_space;
6898 char *f = NULL;
6899 int slot;
6900 struct dwarf2_line_info debug_line;
6901 bfd_boolean loc_directive_seen = FALSE;
6902 TInsn *tinsn;
6904 memset (&debug_line, 0, sizeof (struct dwarf2_line_info));
6906 if (generating_literals)
6908 static int reported = 0;
6909 if (reported < 4)
6910 as_bad_where (frag_now->fr_file, frag_now->fr_line,
6911 _("cannot assemble into a literal fragment"));
6912 if (reported == 3)
6913 as_bad (_("..."));
6914 reported++;
6915 return;
6918 if (frag_now_fix () != 0
6919 && (! frag_now->tc_frag_data.is_insn
6920 || (vinsn_has_specific_opcodes (vinsn) && use_transform ())
6921 || !use_transform () != frag_now->tc_frag_data.is_no_transform
6922 || (directive_state[directive_longcalls]
6923 != frag_now->tc_frag_data.use_longcalls)
6924 || (directive_state[directive_absolute_literals]
6925 != frag_now->tc_frag_data.use_absolute_literals)))
6927 frag_wane (frag_now);
6928 frag_new (0);
6929 xtensa_set_frag_assembly_state (frag_now);
6932 if (workaround_a0_b_retw
6933 && vinsn->num_slots == 1
6934 && (get_last_insn_flags (now_seg, now_subseg) & FLAG_IS_A0_WRITER) != 0
6935 && xtensa_opcode_is_branch (isa, vinsn->slots[0].opcode) == 1
6936 && use_transform ())
6938 has_a0_b_retw = TRUE;
6940 /* Mark this fragment with the special RELAX_ADD_NOP_IF_A0_B_RETW.
6941 After the first assembly pass we will check all of them and
6942 add a nop if needed. */
6943 frag_now->tc_frag_data.is_insn = TRUE;
6944 frag_var (rs_machine_dependent, 4, 4,
6945 RELAX_ADD_NOP_IF_A0_B_RETW,
6946 frag_now->fr_symbol,
6947 frag_now->fr_offset,
6948 NULL);
6949 xtensa_set_frag_assembly_state (frag_now);
6950 frag_now->tc_frag_data.is_insn = TRUE;
6951 frag_var (rs_machine_dependent, 4, 4,
6952 RELAX_ADD_NOP_IF_A0_B_RETW,
6953 frag_now->fr_symbol,
6954 frag_now->fr_offset,
6955 NULL);
6956 xtensa_set_frag_assembly_state (frag_now);
6959 for (slot = 0; slot < vinsn->num_slots; slot++)
6961 tinsn = &vinsn->slots[slot];
6963 /* See if the instruction implies an aligned section. */
6964 if (xtensa_opcode_is_loop (isa, tinsn->opcode) == 1)
6965 record_alignment (now_seg, 2);
6967 /* Determine the best line number for debug info. */
6968 if ((tinsn->loc_directive_seen || !loc_directive_seen)
6969 && (tinsn->debug_line.filenum != debug_line.filenum
6970 || tinsn->debug_line.line < debug_line.line
6971 || tinsn->debug_line.column < debug_line.column))
6972 debug_line = tinsn->debug_line;
6973 if (tinsn->loc_directive_seen)
6974 loc_directive_seen = TRUE;
6977 /* Special cases for instructions that force an alignment... */
6978 /* None of these opcodes are bundle-able. */
6979 if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode) == 1)
6981 int max_fill;
6983 /* Remember the symbol that marks the end of the loop in the frag
6984 that marks the start of the loop. This way we can easily find
6985 the end of the loop at the beginning, without adding special code
6986 to mark the loop instructions themselves. */
6987 symbolS *target_sym = NULL;
6988 if (vinsn->slots[0].tok[1].X_op == O_symbol)
6989 target_sym = vinsn->slots[0].tok[1].X_add_symbol;
6991 xtensa_set_frag_assembly_state (frag_now);
6992 frag_now->tc_frag_data.is_insn = TRUE;
6994 max_fill = get_text_align_max_fill_size
6995 (get_text_align_power (xtensa_fetch_width),
6996 TRUE, frag_now->tc_frag_data.is_no_density);
6998 if (use_transform ())
6999 frag_var (rs_machine_dependent, max_fill, max_fill,
7000 RELAX_ALIGN_NEXT_OPCODE, target_sym, 0, NULL);
7001 else
7002 frag_var (rs_machine_dependent, 0, 0,
7003 RELAX_CHECK_ALIGN_NEXT_OPCODE, target_sym, 0, NULL);
7004 xtensa_set_frag_assembly_state (frag_now);
7007 if (vinsn->slots[0].opcode == xtensa_entry_opcode
7008 && !vinsn->slots[0].is_specific_opcode)
7010 xtensa_mark_literal_pool_location ();
7011 xtensa_move_labels (frag_now, 0);
7012 frag_var (rs_align_test, 1, 1, 0, NULL, 2, NULL);
7015 if (vinsn->num_slots == 1)
7017 if (workaround_a0_b_retw && use_transform ())
7018 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_A0_WRITER,
7019 is_register_writer (&vinsn->slots[0], "a", 0));
7021 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND,
7022 is_bad_loopend_opcode (&vinsn->slots[0]));
7024 else
7025 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND, FALSE);
7027 insn_size = xtensa_format_length (isa, vinsn->format);
7029 extra_space = relaxation_requirements (vinsn, &finish_frag);
7031 /* vinsn_to_insnbuf will produce the error. */
7032 if (vinsn->format != XTENSA_UNDEFINED)
7034 f = frag_more (insn_size + extra_space);
7035 xtensa_set_frag_assembly_state (frag_now);
7036 frag_now->tc_frag_data.is_insn = TRUE;
7039 vinsn_to_insnbuf (vinsn, f, frag_now, FALSE);
7040 if (vinsn->format == XTENSA_UNDEFINED)
7041 return;
7043 xtensa_insnbuf_to_chars (isa, vinsn->insnbuf, (unsigned char *) f, 0);
7045 if (debug_type == DEBUG_DWARF2 || loc_directive_seen)
7046 dwarf2_gen_line_info (frag_now_fix () - (insn_size + extra_space),
7047 &debug_line);
7049 for (slot = 0; slot < vinsn->num_slots; slot++)
7051 tinsn = &vinsn->slots[slot];
7052 frag_now->tc_frag_data.slot_subtypes[slot] = tinsn->subtype;
7053 frag_now->tc_frag_data.slot_symbols[slot] = tinsn->symbol;
7054 frag_now->tc_frag_data.slot_offsets[slot] = tinsn->offset;
7055 frag_now->tc_frag_data.literal_frags[slot] = tinsn->literal_frag;
7056 if (tinsn->literal_space != 0)
7057 xg_assemble_literal_space (tinsn->literal_space, slot);
7058 frag_now->tc_frag_data.free_reg[slot] = tinsn->extra_arg;
7060 if (tinsn->subtype == RELAX_NARROW)
7061 gas_assert (vinsn->num_slots == 1);
7062 if (xtensa_opcode_is_jump (isa, tinsn->opcode) == 1)
7063 is_jump = TRUE;
7064 if (xtensa_opcode_is_branch (isa, tinsn->opcode) == 1)
7065 is_branch = TRUE;
7067 if (tinsn->subtype || tinsn->symbol || tinsn->offset
7068 || tinsn->literal_frag || is_jump || is_branch)
7069 finish_frag = TRUE;
7072 if (vinsn_has_specific_opcodes (vinsn) && use_transform ())
7073 frag_now->tc_frag_data.is_specific_opcode = TRUE;
7075 if (finish_frag)
7077 frag_variant (rs_machine_dependent,
7078 extra_space, extra_space, RELAX_SLOTS,
7079 frag_now->fr_symbol, frag_now->fr_offset, f);
7080 xtensa_set_frag_assembly_state (frag_now);
7083 /* Special cases for loops:
7084 close_loop_end should be inserted AFTER short_loop.
7085 Make sure that CLOSE loops are processed BEFORE short_loops
7086 when converting them. */
7088 /* "short_loop": Add a NOP if the loop is < 4 bytes. */
7089 if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode) == 1
7090 && !vinsn->slots[0].is_specific_opcode)
7092 if (workaround_short_loop && use_transform ())
7094 maybe_has_short_loop = TRUE;
7095 frag_now->tc_frag_data.is_insn = TRUE;
7096 frag_var (rs_machine_dependent, 4, 4,
7097 RELAX_ADD_NOP_IF_SHORT_LOOP,
7098 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7099 frag_now->tc_frag_data.is_insn = TRUE;
7100 frag_var (rs_machine_dependent, 4, 4,
7101 RELAX_ADD_NOP_IF_SHORT_LOOP,
7102 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7105 /* "close_loop_end": Add up to 12 bytes of NOPs to keep a
7106 loop at least 12 bytes away from another loop's end. */
7107 if (workaround_close_loop_end && use_transform ())
7109 maybe_has_close_loop_end = TRUE;
7110 frag_now->tc_frag_data.is_insn = TRUE;
7111 frag_var (rs_machine_dependent, 12, 12,
7112 RELAX_ADD_NOP_IF_CLOSE_LOOP_END,
7113 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7117 if (use_transform ())
7119 if (is_jump)
7121 gas_assert (finish_frag);
7122 frag_var (rs_machine_dependent,
7123 UNREACHABLE_MAX_WIDTH, UNREACHABLE_MAX_WIDTH,
7124 RELAX_UNREACHABLE,
7125 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7126 xtensa_set_frag_assembly_state (frag_now);
7128 else if (is_branch && do_align_targets ())
7130 gas_assert (finish_frag);
7131 frag_var (rs_machine_dependent,
7132 UNREACHABLE_MAX_WIDTH, UNREACHABLE_MAX_WIDTH,
7133 RELAX_MAYBE_UNREACHABLE,
7134 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7135 xtensa_set_frag_assembly_state (frag_now);
7136 frag_var (rs_machine_dependent,
7137 0, 0,
7138 RELAX_MAYBE_DESIRE_ALIGN,
7139 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7140 xtensa_set_frag_assembly_state (frag_now);
7144 /* Now, if the original opcode was a call... */
7145 if (do_align_targets ()
7146 && xtensa_opcode_is_call (isa, vinsn->slots[0].opcode) == 1)
7148 float freq = get_subseg_total_freq (now_seg, now_subseg);
7149 frag_now->tc_frag_data.is_insn = TRUE;
7150 frag_var (rs_machine_dependent, 4, (int) freq, RELAX_DESIRE_ALIGN,
7151 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7152 xtensa_set_frag_assembly_state (frag_now);
7155 if (vinsn_has_specific_opcodes (vinsn) && use_transform ())
7157 frag_wane (frag_now);
7158 frag_new (0);
7159 xtensa_set_frag_assembly_state (frag_now);
7164 /* xtensa_end and helper functions. */
7166 static void xtensa_cleanup_align_frags (void);
7167 static void xtensa_fix_target_frags (void);
7168 static void xtensa_mark_narrow_branches (void);
7169 static void xtensa_mark_zcl_first_insns (void);
7170 static void xtensa_mark_difference_of_two_symbols (void);
7171 static void xtensa_fix_a0_b_retw_frags (void);
7172 static void xtensa_fix_b_j_loop_end_frags (void);
7173 static void xtensa_fix_close_loop_end_frags (void);
7174 static void xtensa_fix_short_loop_frags (void);
7175 static void xtensa_sanity_check (void);
7176 static void xtensa_add_config_info (void);
7178 void
7179 xtensa_end (void)
7181 directive_balance ();
7182 xtensa_flush_pending_output ();
7184 past_xtensa_end = TRUE;
7186 xtensa_move_literals ();
7188 xtensa_reorder_segments ();
7189 xtensa_cleanup_align_frags ();
7190 xtensa_fix_target_frags ();
7191 if (workaround_a0_b_retw && has_a0_b_retw)
7192 xtensa_fix_a0_b_retw_frags ();
7193 if (workaround_b_j_loop_end)
7194 xtensa_fix_b_j_loop_end_frags ();
7196 /* "close_loop_end" should be processed BEFORE "short_loop". */
7197 if (workaround_close_loop_end && maybe_has_close_loop_end)
7198 xtensa_fix_close_loop_end_frags ();
7200 if (workaround_short_loop && maybe_has_short_loop)
7201 xtensa_fix_short_loop_frags ();
7202 if (align_targets)
7203 xtensa_mark_narrow_branches ();
7204 xtensa_mark_zcl_first_insns ();
7206 xtensa_sanity_check ();
7208 xtensa_add_config_info ();
7212 static void
7213 xtensa_cleanup_align_frags (void)
7215 frchainS *frchP;
7216 asection *s;
7218 for (s = stdoutput->sections; s; s = s->next)
7219 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7221 fragS *fragP;
7222 /* Walk over all of the fragments in a subsection. */
7223 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7225 if ((fragP->fr_type == rs_align
7226 || fragP->fr_type == rs_align_code
7227 || (fragP->fr_type == rs_machine_dependent
7228 && (fragP->fr_subtype == RELAX_DESIRE_ALIGN
7229 || fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)))
7230 && fragP->fr_fix == 0)
7232 fragS *next = fragP->fr_next;
7234 while (next
7235 && next->fr_fix == 0
7236 && next->fr_type == rs_machine_dependent
7237 && next->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
7239 frag_wane (next);
7240 next = next->fr_next;
7243 /* If we don't widen branch targets, then they
7244 will be easier to align. */
7245 if (fragP->tc_frag_data.is_branch_target
7246 && fragP->fr_opcode == fragP->fr_literal
7247 && fragP->fr_type == rs_machine_dependent
7248 && fragP->fr_subtype == RELAX_SLOTS
7249 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
7250 frag_wane (fragP);
7251 if (fragP->fr_type == rs_machine_dependent
7252 && fragP->fr_subtype == RELAX_UNREACHABLE)
7253 fragP->tc_frag_data.is_unreachable = TRUE;
7259 /* Re-process all of the fragments looking to convert all of the
7260 RELAX_DESIRE_ALIGN_IF_TARGET fragments. If there is a branch
7261 target in the next fragment, convert this to RELAX_DESIRE_ALIGN.
7262 Otherwise, convert to a .fill 0. */
7264 static void
7265 xtensa_fix_target_frags (void)
7267 frchainS *frchP;
7268 asection *s;
7270 /* When this routine is called, all of the subsections are still intact
7271 so we walk over subsections instead of sections. */
7272 for (s = stdoutput->sections; s; s = s->next)
7273 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7275 fragS *fragP;
7277 /* Walk over all of the fragments in a subsection. */
7278 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7280 if (fragP->fr_type == rs_machine_dependent
7281 && fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
7283 if (next_frag_is_branch_target (fragP))
7284 fragP->fr_subtype = RELAX_DESIRE_ALIGN;
7285 else
7286 frag_wane (fragP);
7293 static bfd_boolean is_narrow_branch_guaranteed_in_range (fragS *, TInsn *);
7295 static void
7296 xtensa_mark_narrow_branches (void)
7298 frchainS *frchP;
7299 asection *s;
7301 for (s = stdoutput->sections; s; s = s->next)
7302 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7304 fragS *fragP;
7305 /* Walk over all of the fragments in a subsection. */
7306 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7308 if (fragP->fr_type == rs_machine_dependent
7309 && fragP->fr_subtype == RELAX_SLOTS
7310 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED)
7312 vliw_insn vinsn;
7314 vinsn_from_chars (&vinsn, fragP->fr_opcode);
7315 tinsn_immed_from_frag (&vinsn.slots[0], fragP, 0);
7317 if (vinsn.num_slots == 1
7318 && xtensa_opcode_is_branch (xtensa_default_isa,
7319 vinsn.slots[0].opcode) == 1
7320 && xg_get_single_size (vinsn.slots[0].opcode) == 2
7321 && is_narrow_branch_guaranteed_in_range (fragP,
7322 &vinsn.slots[0]))
7324 fragP->fr_subtype = RELAX_SLOTS;
7325 fragP->tc_frag_data.slot_subtypes[0] = RELAX_NARROW;
7326 fragP->tc_frag_data.is_aligning_branch = 1;
7334 /* A branch is typically widened only when its target is out of
7335 range. However, we would like to widen them to align a subsequent
7336 branch target when possible.
7338 Because the branch relaxation code is so convoluted, the optimal solution
7339 (combining the two cases) is difficult to get right in all circumstances.
7340 We therefore go with an "almost as good" solution, where we only
7341 use for alignment narrow branches that definitely will not expand to a
7342 jump and a branch. These functions find and mark these cases. */
7344 /* The range in bytes of BNEZ.N and BEQZ.N. The target operand is encoded
7345 as PC + 4 + imm6, where imm6 is a 6-bit immediate ranging from 0 to 63.
7346 We start counting beginning with the frag after the 2-byte branch, so the
7347 maximum offset is (4 - 2) + 63 = 65. */
7348 #define MAX_IMMED6 65
7350 static offsetT unrelaxed_frag_max_size (fragS *);
7352 static bfd_boolean
7353 is_narrow_branch_guaranteed_in_range (fragS *fragP, TInsn *tinsn)
7355 const expressionS *exp = &tinsn->tok[1];
7356 symbolS *symbolP = exp->X_add_symbol;
7357 offsetT max_distance = exp->X_add_number;
7358 fragS *target_frag;
7360 if (exp->X_op != O_symbol)
7361 return FALSE;
7363 target_frag = symbol_get_frag (symbolP);
7365 max_distance += (S_GET_VALUE (symbolP) - target_frag->fr_address);
7366 if (is_branch_jmp_to_next (tinsn, fragP))
7367 return FALSE;
7369 /* The branch doesn't branch over it's own frag,
7370 but over the subsequent ones. */
7371 fragP = fragP->fr_next;
7372 while (fragP != NULL && fragP != target_frag && max_distance <= MAX_IMMED6)
7374 max_distance += unrelaxed_frag_max_size (fragP);
7375 fragP = fragP->fr_next;
7377 if (max_distance <= MAX_IMMED6 && fragP == target_frag)
7378 return TRUE;
7379 return FALSE;
7383 static void
7384 xtensa_mark_zcl_first_insns (void)
7386 frchainS *frchP;
7387 asection *s;
7389 for (s = stdoutput->sections; s; s = s->next)
7390 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7392 fragS *fragP;
7393 /* Walk over all of the fragments in a subsection. */
7394 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7396 if (fragP->fr_type == rs_machine_dependent
7397 && (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE
7398 || fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE))
7400 /* Find the loop frag. */
7401 fragS *targ_frag = next_non_empty_frag (fragP);
7402 /* Find the first insn frag. */
7403 targ_frag = next_non_empty_frag (targ_frag);
7405 /* Of course, sometimes (mostly for toy test cases) a
7406 zero-cost loop instruction is the last in a section. */
7407 if (targ_frag)
7409 targ_frag->tc_frag_data.is_first_loop_insn = TRUE;
7410 /* Do not widen a frag that is the first instruction of a
7411 zero-cost loop. It makes that loop harder to align. */
7412 if (targ_frag->fr_type == rs_machine_dependent
7413 && targ_frag->fr_subtype == RELAX_SLOTS
7414 && (targ_frag->tc_frag_data.slot_subtypes[0]
7415 == RELAX_NARROW))
7417 if (targ_frag->tc_frag_data.is_aligning_branch)
7418 targ_frag->tc_frag_data.slot_subtypes[0] = RELAX_IMMED;
7419 else
7421 frag_wane (targ_frag);
7422 targ_frag->tc_frag_data.slot_subtypes[0] = 0;
7426 if (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)
7427 frag_wane (fragP);
7434 /* When a difference-of-symbols expression is encoded as a uleb128 or
7435 sleb128 value, the linker is unable to adjust that value to account for
7436 link-time relaxation. Mark all the code between such symbols so that
7437 its size cannot be changed by linker relaxation. */
7439 static void
7440 xtensa_mark_difference_of_two_symbols (void)
7442 symbolS *expr_sym;
7444 for (expr_sym = expr_symbols; expr_sym;
7445 expr_sym = symbol_get_tc (expr_sym)->next_expr_symbol)
7447 expressionS *exp = symbol_get_value_expression (expr_sym);
7449 if (exp->X_op == O_subtract)
7451 symbolS *left = exp->X_add_symbol;
7452 symbolS *right = exp->X_op_symbol;
7454 /* Difference of two symbols not in the same section
7455 are handled with relocations in the linker. */
7456 if (S_GET_SEGMENT (left) == S_GET_SEGMENT (right))
7458 fragS *start;
7459 fragS *end;
7460 fragS *walk;
7462 if (symbol_get_frag (left)->fr_address
7463 <= symbol_get_frag (right)->fr_address)
7465 start = symbol_get_frag (left);
7466 end = symbol_get_frag (right);
7468 else
7470 start = symbol_get_frag (right);
7471 end = symbol_get_frag (left);
7474 if (start->tc_frag_data.no_transform_end != NULL)
7475 walk = start->tc_frag_data.no_transform_end;
7476 else
7477 walk = start;
7480 walk->tc_frag_data.is_no_transform = 1;
7481 walk = walk->fr_next;
7483 while (walk && walk->fr_address < end->fr_address);
7485 start->tc_frag_data.no_transform_end = walk;
7492 /* Re-process all of the fragments looking to convert all of the
7493 RELAX_ADD_NOP_IF_A0_B_RETW. If the next instruction is a
7494 conditional branch or a retw/retw.n, convert this frag to one that
7495 will generate a NOP. In any case close it off with a .fill 0. */
7497 static bfd_boolean next_instrs_are_b_retw (fragS *);
7499 static void
7500 xtensa_fix_a0_b_retw_frags (void)
7502 frchainS *frchP;
7503 asection *s;
7505 /* When this routine is called, all of the subsections are still intact
7506 so we walk over subsections instead of sections. */
7507 for (s = stdoutput->sections; s; s = s->next)
7508 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7510 fragS *fragP;
7512 /* Walk over all of the fragments in a subsection. */
7513 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7515 if (fragP->fr_type == rs_machine_dependent
7516 && fragP->fr_subtype == RELAX_ADD_NOP_IF_A0_B_RETW)
7518 if (next_instrs_are_b_retw (fragP))
7520 if (fragP->tc_frag_data.is_no_transform)
7521 as_bad (_("instruction sequence (write a0, branch, retw) may trigger hardware errata"));
7522 else
7523 relax_frag_add_nop (fragP);
7525 frag_wane (fragP);
7532 static bfd_boolean
7533 next_instrs_are_b_retw (fragS *fragP)
7535 xtensa_opcode opcode;
7536 xtensa_format fmt;
7537 const fragS *next_fragP = next_non_empty_frag (fragP);
7538 static xtensa_insnbuf insnbuf = NULL;
7539 static xtensa_insnbuf slotbuf = NULL;
7540 xtensa_isa isa = xtensa_default_isa;
7541 int offset = 0;
7542 int slot;
7543 bfd_boolean branch_seen = FALSE;
7545 if (!insnbuf)
7547 insnbuf = xtensa_insnbuf_alloc (isa);
7548 slotbuf = xtensa_insnbuf_alloc (isa);
7551 if (next_fragP == NULL)
7552 return FALSE;
7554 /* Check for the conditional branch. */
7555 xtensa_insnbuf_from_chars
7556 (isa, insnbuf, (unsigned char *) &next_fragP->fr_literal[offset], 0);
7557 fmt = xtensa_format_decode (isa, insnbuf);
7558 if (fmt == XTENSA_UNDEFINED)
7559 return FALSE;
7561 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
7563 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
7564 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
7566 branch_seen = (branch_seen
7567 || xtensa_opcode_is_branch (isa, opcode) == 1);
7570 if (!branch_seen)
7571 return FALSE;
7573 offset += xtensa_format_length (isa, fmt);
7574 if (offset == next_fragP->fr_fix)
7576 next_fragP = next_non_empty_frag (next_fragP);
7577 offset = 0;
7580 if (next_fragP == NULL)
7581 return FALSE;
7583 /* Check for the retw/retw.n. */
7584 xtensa_insnbuf_from_chars
7585 (isa, insnbuf, (unsigned char *) &next_fragP->fr_literal[offset], 0);
7586 fmt = xtensa_format_decode (isa, insnbuf);
7588 /* Because RETW[.N] is not bundleable, a VLIW bundle here means that we
7589 have no problems. */
7590 if (fmt == XTENSA_UNDEFINED
7591 || xtensa_format_num_slots (isa, fmt) != 1)
7592 return FALSE;
7594 xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf);
7595 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
7597 if (opcode == xtensa_retw_opcode || opcode == xtensa_retw_n_opcode)
7598 return TRUE;
7600 return FALSE;
7604 /* Re-process all of the fragments looking to convert all of the
7605 RELAX_ADD_NOP_IF_PRE_LOOP_END. If there is one instruction and a
7606 loop end label, convert this frag to one that will generate a NOP.
7607 In any case close it off with a .fill 0. */
7609 static bfd_boolean next_instr_is_loop_end (fragS *);
7611 static void
7612 xtensa_fix_b_j_loop_end_frags (void)
7614 frchainS *frchP;
7615 asection *s;
7617 /* When this routine is called, all of the subsections are still intact
7618 so we walk over subsections instead of sections. */
7619 for (s = stdoutput->sections; s; s = s->next)
7620 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7622 fragS *fragP;
7624 /* Walk over all of the fragments in a subsection. */
7625 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7627 if (fragP->fr_type == rs_machine_dependent
7628 && fragP->fr_subtype == RELAX_ADD_NOP_IF_PRE_LOOP_END)
7630 if (next_instr_is_loop_end (fragP))
7632 if (fragP->tc_frag_data.is_no_transform)
7633 as_bad (_("branching or jumping to a loop end may trigger hardware errata"));
7634 else
7635 relax_frag_add_nop (fragP);
7637 frag_wane (fragP);
7644 static bfd_boolean
7645 next_instr_is_loop_end (fragS *fragP)
7647 const fragS *next_fragP;
7649 if (next_frag_is_loop_target (fragP))
7650 return FALSE;
7652 next_fragP = next_non_empty_frag (fragP);
7653 if (next_fragP == NULL)
7654 return FALSE;
7656 if (!next_frag_is_loop_target (next_fragP))
7657 return FALSE;
7659 /* If the size is >= 3 then there is more than one instruction here.
7660 The hardware bug will not fire. */
7661 if (next_fragP->fr_fix > 3)
7662 return FALSE;
7664 return TRUE;
7668 /* Re-process all of the fragments looking to convert all of the
7669 RELAX_ADD_NOP_IF_CLOSE_LOOP_END. If there is an loop end that is
7670 not MY loop's loop end within 12 bytes, add enough nops here to
7671 make it at least 12 bytes away. In any case close it off with a
7672 .fill 0. */
7674 static offsetT min_bytes_to_other_loop_end
7675 (fragS *, fragS *, offsetT);
7677 static void
7678 xtensa_fix_close_loop_end_frags (void)
7680 frchainS *frchP;
7681 asection *s;
7683 /* When this routine is called, all of the subsections are still intact
7684 so we walk over subsections instead of sections. */
7685 for (s = stdoutput->sections; s; s = s->next)
7686 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7688 fragS *fragP;
7690 fragS *current_target = NULL;
7692 /* Walk over all of the fragments in a subsection. */
7693 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7695 if (fragP->fr_type == rs_machine_dependent
7696 && ((fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
7697 || (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)))
7698 current_target = symbol_get_frag (fragP->fr_symbol);
7700 if (current_target
7701 && fragP->fr_type == rs_machine_dependent
7702 && fragP->fr_subtype == RELAX_ADD_NOP_IF_CLOSE_LOOP_END)
7704 offsetT min_bytes;
7705 int bytes_added = 0;
7707 #define REQUIRED_LOOP_DIVIDING_BYTES 12
7708 /* Max out at 12. */
7709 min_bytes = min_bytes_to_other_loop_end
7710 (fragP->fr_next, current_target, REQUIRED_LOOP_DIVIDING_BYTES);
7712 if (min_bytes < REQUIRED_LOOP_DIVIDING_BYTES)
7714 if (fragP->tc_frag_data.is_no_transform)
7715 as_bad (_("loop end too close to another loop end may trigger hardware errata"));
7716 else
7718 while (min_bytes + bytes_added
7719 < REQUIRED_LOOP_DIVIDING_BYTES)
7721 int length = 3;
7723 if (fragP->fr_var < length)
7724 as_fatal (_("fr_var %lu < length %d"),
7725 (long) fragP->fr_var, length);
7726 else
7728 assemble_nop (length,
7729 fragP->fr_literal + fragP->fr_fix);
7730 fragP->fr_fix += length;
7731 fragP->fr_var -= length;
7733 bytes_added += length;
7737 frag_wane (fragP);
7739 gas_assert (fragP->fr_type != rs_machine_dependent
7740 || fragP->fr_subtype != RELAX_ADD_NOP_IF_CLOSE_LOOP_END);
7746 static offsetT unrelaxed_frag_min_size (fragS *);
7748 static offsetT
7749 min_bytes_to_other_loop_end (fragS *fragP,
7750 fragS *current_target,
7751 offsetT max_size)
7753 offsetT offset = 0;
7754 fragS *current_fragP;
7756 for (current_fragP = fragP;
7757 current_fragP;
7758 current_fragP = current_fragP->fr_next)
7760 if (current_fragP->tc_frag_data.is_loop_target
7761 && current_fragP != current_target)
7762 return offset;
7764 offset += unrelaxed_frag_min_size (current_fragP);
7766 if (offset >= max_size)
7767 return max_size;
7769 return max_size;
7773 static offsetT
7774 unrelaxed_frag_min_size (fragS *fragP)
7776 offsetT size = fragP->fr_fix;
7778 /* Add fill size. */
7779 if (fragP->fr_type == rs_fill)
7780 size += fragP->fr_offset;
7782 return size;
7786 static offsetT
7787 unrelaxed_frag_max_size (fragS *fragP)
7789 offsetT size = fragP->fr_fix;
7790 switch (fragP->fr_type)
7792 case 0:
7793 /* Empty frags created by the obstack allocation scheme
7794 end up with type 0. */
7795 break;
7796 case rs_fill:
7797 case rs_org:
7798 case rs_space:
7799 size += fragP->fr_offset;
7800 break;
7801 case rs_align:
7802 case rs_align_code:
7803 case rs_align_test:
7804 case rs_leb128:
7805 case rs_cfa:
7806 case rs_dwarf2dbg:
7807 /* No further adjustments needed. */
7808 break;
7809 case rs_machine_dependent:
7810 if (fragP->fr_subtype != RELAX_DESIRE_ALIGN)
7811 size += fragP->fr_var;
7812 break;
7813 default:
7814 /* We had darn well better know how big it is. */
7815 gas_assert (0);
7816 break;
7819 return size;
7823 /* Re-process all of the fragments looking to convert all
7824 of the RELAX_ADD_NOP_IF_SHORT_LOOP. If:
7827 1) the instruction size count to the loop end label
7828 is too short (<= 2 instructions),
7829 2) loop has a jump or branch in it
7831 or B)
7832 1) workaround_all_short_loops is TRUE
7833 2) The generating loop was a 'loopgtz' or 'loopnez'
7834 3) the instruction size count to the loop end label is too short
7835 (<= 2 instructions)
7836 then convert this frag (and maybe the next one) to generate a NOP.
7837 In any case close it off with a .fill 0. */
7839 static int count_insns_to_loop_end (fragS *, bfd_boolean, int);
7840 static bfd_boolean branch_before_loop_end (fragS *);
7842 static void
7843 xtensa_fix_short_loop_frags (void)
7845 frchainS *frchP;
7846 asection *s;
7848 /* When this routine is called, all of the subsections are still intact
7849 so we walk over subsections instead of sections. */
7850 for (s = stdoutput->sections; s; s = s->next)
7851 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7853 fragS *fragP;
7854 fragS *current_target = NULL;
7855 xtensa_opcode current_opcode = XTENSA_UNDEFINED;
7857 /* Walk over all of the fragments in a subsection. */
7858 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7860 if (fragP->fr_type == rs_machine_dependent
7861 && ((fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
7862 || (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)))
7864 TInsn t_insn;
7865 fragS *loop_frag = next_non_empty_frag (fragP);
7866 tinsn_from_chars (&t_insn, loop_frag->fr_opcode, 0);
7867 current_target = symbol_get_frag (fragP->fr_symbol);
7868 current_opcode = t_insn.opcode;
7869 gas_assert (xtensa_opcode_is_loop (xtensa_default_isa,
7870 current_opcode) == 1);
7873 if (fragP->fr_type == rs_machine_dependent
7874 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
7876 if (count_insns_to_loop_end (fragP->fr_next, TRUE, 3) < 3
7877 && (branch_before_loop_end (fragP->fr_next)
7878 || (workaround_all_short_loops
7879 && current_opcode != XTENSA_UNDEFINED
7880 && current_opcode != xtensa_loop_opcode)))
7882 if (fragP->tc_frag_data.is_no_transform)
7883 as_bad (_("loop containing less than three instructions may trigger hardware errata"));
7884 else
7885 relax_frag_add_nop (fragP);
7887 frag_wane (fragP);
7894 static int unrelaxed_frag_min_insn_count (fragS *);
7896 static int
7897 count_insns_to_loop_end (fragS *base_fragP,
7898 bfd_boolean count_relax_add,
7899 int max_count)
7901 fragS *fragP = NULL;
7902 int insn_count = 0;
7904 fragP = base_fragP;
7906 for (; fragP && !fragP->tc_frag_data.is_loop_target; fragP = fragP->fr_next)
7908 insn_count += unrelaxed_frag_min_insn_count (fragP);
7909 if (insn_count >= max_count)
7910 return max_count;
7912 if (count_relax_add)
7914 if (fragP->fr_type == rs_machine_dependent
7915 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
7917 /* In order to add the appropriate number of
7918 NOPs, we count an instruction for downstream
7919 occurrences. */
7920 insn_count++;
7921 if (insn_count >= max_count)
7922 return max_count;
7926 return insn_count;
7930 static int
7931 unrelaxed_frag_min_insn_count (fragS *fragP)
7933 xtensa_isa isa = xtensa_default_isa;
7934 static xtensa_insnbuf insnbuf = NULL;
7935 int insn_count = 0;
7936 int offset = 0;
7938 if (!fragP->tc_frag_data.is_insn)
7939 return insn_count;
7941 if (!insnbuf)
7942 insnbuf = xtensa_insnbuf_alloc (isa);
7944 /* Decode the fixed instructions. */
7945 while (offset < fragP->fr_fix)
7947 xtensa_format fmt;
7949 xtensa_insnbuf_from_chars
7950 (isa, insnbuf, (unsigned char *) fragP->fr_literal + offset, 0);
7951 fmt = xtensa_format_decode (isa, insnbuf);
7953 if (fmt == XTENSA_UNDEFINED)
7955 as_fatal (_("undecodable instruction in instruction frag"));
7956 return insn_count;
7958 offset += xtensa_format_length (isa, fmt);
7959 insn_count++;
7962 return insn_count;
7966 static bfd_boolean unrelaxed_frag_has_b_j (fragS *);
7968 static bfd_boolean
7969 branch_before_loop_end (fragS *base_fragP)
7971 fragS *fragP;
7973 for (fragP = base_fragP;
7974 fragP && !fragP->tc_frag_data.is_loop_target;
7975 fragP = fragP->fr_next)
7977 if (unrelaxed_frag_has_b_j (fragP))
7978 return TRUE;
7980 return FALSE;
7984 static bfd_boolean
7985 unrelaxed_frag_has_b_j (fragS *fragP)
7987 static xtensa_insnbuf insnbuf = NULL;
7988 xtensa_isa isa = xtensa_default_isa;
7989 int offset = 0;
7991 if (!fragP->tc_frag_data.is_insn)
7992 return FALSE;
7994 if (!insnbuf)
7995 insnbuf = xtensa_insnbuf_alloc (isa);
7997 /* Decode the fixed instructions. */
7998 while (offset < fragP->fr_fix)
8000 xtensa_format fmt;
8001 int slot;
8003 xtensa_insnbuf_from_chars
8004 (isa, insnbuf, (unsigned char *) fragP->fr_literal + offset, 0);
8005 fmt = xtensa_format_decode (isa, insnbuf);
8006 if (fmt == XTENSA_UNDEFINED)
8007 return FALSE;
8009 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
8011 xtensa_opcode opcode =
8012 get_opcode_from_buf (fragP->fr_literal + offset, slot);
8013 if (xtensa_opcode_is_branch (isa, opcode) == 1
8014 || xtensa_opcode_is_jump (isa, opcode) == 1)
8015 return TRUE;
8017 offset += xtensa_format_length (isa, fmt);
8019 return FALSE;
8023 /* Checks to be made after initial assembly but before relaxation. */
8025 static bfd_boolean is_empty_loop (const TInsn *, fragS *);
8026 static bfd_boolean is_local_forward_loop (const TInsn *, fragS *);
8028 static void
8029 xtensa_sanity_check (void)
8031 char *file_name;
8032 unsigned line;
8033 frchainS *frchP;
8034 asection *s;
8036 as_where (&file_name, &line);
8037 for (s = stdoutput->sections; s; s = s->next)
8038 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
8040 fragS *fragP;
8042 /* Walk over all of the fragments in a subsection. */
8043 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
8045 if (fragP->fr_type == rs_machine_dependent
8046 && fragP->fr_subtype == RELAX_SLOTS
8047 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED)
8049 static xtensa_insnbuf insnbuf = NULL;
8050 TInsn t_insn;
8052 if (fragP->fr_opcode != NULL)
8054 if (!insnbuf)
8055 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
8056 tinsn_from_chars (&t_insn, fragP->fr_opcode, 0);
8057 tinsn_immed_from_frag (&t_insn, fragP, 0);
8059 if (xtensa_opcode_is_loop (xtensa_default_isa,
8060 t_insn.opcode) == 1)
8062 if (is_empty_loop (&t_insn, fragP))
8064 new_logical_line (fragP->fr_file, fragP->fr_line);
8065 as_bad (_("invalid empty loop"));
8067 if (!is_local_forward_loop (&t_insn, fragP))
8069 new_logical_line (fragP->fr_file, fragP->fr_line);
8070 as_bad (_("loop target does not follow "
8071 "loop instruction in section"));
8078 new_logical_line (file_name, line);
8082 #define LOOP_IMMED_OPN 1
8084 /* Return TRUE if the loop target is the next non-zero fragment. */
8086 static bfd_boolean
8087 is_empty_loop (const TInsn *insn, fragS *fragP)
8089 const expressionS *exp;
8090 symbolS *symbolP;
8091 fragS *next_fragP;
8093 if (insn->insn_type != ITYPE_INSN)
8094 return FALSE;
8096 if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) != 1)
8097 return FALSE;
8099 if (insn->ntok <= LOOP_IMMED_OPN)
8100 return FALSE;
8102 exp = &insn->tok[LOOP_IMMED_OPN];
8104 if (exp->X_op != O_symbol)
8105 return FALSE;
8107 symbolP = exp->X_add_symbol;
8108 if (!symbolP)
8109 return FALSE;
8111 if (symbol_get_frag (symbolP) == NULL)
8112 return FALSE;
8114 if (S_GET_VALUE (symbolP) != 0)
8115 return FALSE;
8117 /* Walk through the zero-size fragments from this one. If we find
8118 the target fragment, then this is a zero-size loop. */
8120 for (next_fragP = fragP->fr_next;
8121 next_fragP != NULL;
8122 next_fragP = next_fragP->fr_next)
8124 if (next_fragP == symbol_get_frag (symbolP))
8125 return TRUE;
8126 if (next_fragP->fr_fix != 0)
8127 return FALSE;
8129 return FALSE;
8133 static bfd_boolean
8134 is_local_forward_loop (const TInsn *insn, fragS *fragP)
8136 const expressionS *exp;
8137 symbolS *symbolP;
8138 fragS *next_fragP;
8140 if (insn->insn_type != ITYPE_INSN)
8141 return FALSE;
8143 if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) != 1)
8144 return FALSE;
8146 if (insn->ntok <= LOOP_IMMED_OPN)
8147 return FALSE;
8149 exp = &insn->tok[LOOP_IMMED_OPN];
8151 if (exp->X_op != O_symbol)
8152 return FALSE;
8154 symbolP = exp->X_add_symbol;
8155 if (!symbolP)
8156 return FALSE;
8158 if (symbol_get_frag (symbolP) == NULL)
8159 return FALSE;
8161 /* Walk through fragments until we find the target.
8162 If we do not find the target, then this is an invalid loop. */
8164 for (next_fragP = fragP->fr_next;
8165 next_fragP != NULL;
8166 next_fragP = next_fragP->fr_next)
8168 if (next_fragP == symbol_get_frag (symbolP))
8169 return TRUE;
8172 return FALSE;
8176 #define XTINFO_NAME "Xtensa_Info"
8177 #define XTINFO_NAMESZ 12
8178 #define XTINFO_TYPE 1
8180 static void
8181 xtensa_add_config_info (void)
8183 asection *info_sec;
8184 char *data, *p;
8185 int sz;
8187 info_sec = subseg_new (".xtensa.info", 0);
8188 bfd_set_section_flags (stdoutput, info_sec, SEC_HAS_CONTENTS | SEC_READONLY);
8190 data = xmalloc (100);
8191 sprintf (data, "USE_ABSOLUTE_LITERALS=%d\nABI=%d\n",
8192 XSHAL_USE_ABSOLUTE_LITERALS, XSHAL_ABI);
8193 sz = strlen (data) + 1;
8195 /* Add enough null terminators to pad to a word boundary. */
8197 data[sz++] = 0;
8198 while ((sz & 3) != 0);
8200 /* Follow the standard note section layout:
8201 First write the length of the name string. */
8202 p = frag_more (4);
8203 md_number_to_chars (p, (valueT) XTINFO_NAMESZ, 4);
8205 /* Next comes the length of the "descriptor", i.e., the actual data. */
8206 p = frag_more (4);
8207 md_number_to_chars (p, (valueT) sz, 4);
8209 /* Write the note type. */
8210 p = frag_more (4);
8211 md_number_to_chars (p, (valueT) XTINFO_TYPE, 4);
8213 /* Write the name field. */
8214 p = frag_more (XTINFO_NAMESZ);
8215 memcpy (p, XTINFO_NAME, XTINFO_NAMESZ);
8217 /* Finally, write the descriptor. */
8218 p = frag_more (sz);
8219 memcpy (p, data, sz);
8221 free (data);
8225 /* Alignment Functions. */
8227 static int
8228 get_text_align_power (unsigned target_size)
8230 if (target_size <= 4)
8231 return 2;
8232 gas_assert (target_size == 8);
8233 return 3;
8237 static int
8238 get_text_align_max_fill_size (int align_pow,
8239 bfd_boolean use_nops,
8240 bfd_boolean use_no_density)
8242 if (!use_nops)
8243 return (1 << align_pow);
8244 if (use_no_density)
8245 return 3 * (1 << align_pow);
8247 return 1 + (1 << align_pow);
8251 /* Calculate the minimum bytes of fill needed at "address" to align a
8252 target instruction of size "target_size" so that it does not cross a
8253 power-of-two boundary specified by "align_pow". If "use_nops" is FALSE,
8254 the fill can be an arbitrary number of bytes. Otherwise, the space must
8255 be filled by NOP instructions. */
8257 static int
8258 get_text_align_fill_size (addressT address,
8259 int align_pow,
8260 int target_size,
8261 bfd_boolean use_nops,
8262 bfd_boolean use_no_density)
8264 addressT alignment, fill, fill_limit, fill_step;
8265 bfd_boolean skip_one = FALSE;
8267 alignment = (1 << align_pow);
8268 gas_assert (target_size > 0 && alignment >= (addressT) target_size);
8270 if (!use_nops)
8272 fill_limit = alignment;
8273 fill_step = 1;
8275 else if (!use_no_density)
8277 /* Combine 2- and 3-byte NOPs to fill anything larger than one. */
8278 fill_limit = alignment * 2;
8279 fill_step = 1;
8280 skip_one = TRUE;
8282 else
8284 /* Fill with 3-byte NOPs -- can only fill multiples of 3. */
8285 fill_limit = alignment * 3;
8286 fill_step = 3;
8289 /* Try all fill sizes until finding one that works. */
8290 for (fill = 0; fill < fill_limit; fill += fill_step)
8292 if (skip_one && fill == 1)
8293 continue;
8294 if ((address + fill) >> align_pow
8295 == (address + fill + target_size - 1) >> align_pow)
8296 return fill;
8298 gas_assert (0);
8299 return 0;
8303 static int
8304 branch_align_power (segT sec)
8306 /* If the Xtensa processor has a fetch width of 8 bytes, and the section
8307 is aligned to at least an 8-byte boundary, then a branch target need
8308 only fit within an 8-byte aligned block of memory to avoid a stall.
8309 Otherwise, try to fit branch targets within 4-byte aligned blocks
8310 (which may be insufficient, e.g., if the section has no alignment, but
8311 it's good enough). */
8312 if (xtensa_fetch_width == 8)
8314 if (get_recorded_alignment (sec) >= 3)
8315 return 3;
8317 else
8318 gas_assert (xtensa_fetch_width == 4);
8320 return 2;
8324 /* This will assert if it is not possible. */
8326 static int
8327 get_text_align_nop_count (offsetT fill_size, bfd_boolean use_no_density)
8329 int count = 0;
8331 if (use_no_density)
8333 gas_assert (fill_size % 3 == 0);
8334 return (fill_size / 3);
8337 gas_assert (fill_size != 1); /* Bad argument. */
8339 while (fill_size > 1)
8341 int insn_size = 3;
8342 if (fill_size == 2 || fill_size == 4)
8343 insn_size = 2;
8344 fill_size -= insn_size;
8345 count++;
8347 gas_assert (fill_size != 1); /* Bad algorithm. */
8348 return count;
8352 static int
8353 get_text_align_nth_nop_size (offsetT fill_size,
8354 int n,
8355 bfd_boolean use_no_density)
8357 int count = 0;
8359 if (use_no_density)
8360 return 3;
8362 gas_assert (fill_size != 1); /* Bad argument. */
8364 while (fill_size > 1)
8366 int insn_size = 3;
8367 if (fill_size == 2 || fill_size == 4)
8368 insn_size = 2;
8369 fill_size -= insn_size;
8370 count++;
8371 if (n + 1 == count)
8372 return insn_size;
8374 gas_assert (0);
8375 return 0;
8379 /* For the given fragment, find the appropriate address
8380 for it to begin at if we are using NOPs to align it. */
8382 static addressT
8383 get_noop_aligned_address (fragS *fragP, addressT address)
8385 /* The rule is: get next fragment's FIRST instruction. Find
8386 the smallest number of bytes that need to be added to
8387 ensure that the next fragment's FIRST instruction will fit
8388 in a single word.
8390 E.G., 2 bytes : 0, 1, 2 mod 4
8391 3 bytes: 0, 1 mod 4
8393 If the FIRST instruction MIGHT be relaxed,
8394 assume that it will become a 3-byte instruction.
8396 Note again here that LOOP instructions are not bundleable,
8397 and this relaxation only applies to LOOP opcodes. */
8399 int fill_size = 0;
8400 int first_insn_size;
8401 int loop_insn_size;
8402 addressT pre_opcode_bytes;
8403 int align_power;
8404 fragS *first_insn;
8405 xtensa_opcode opcode;
8406 bfd_boolean is_loop;
8408 gas_assert (fragP->fr_type == rs_machine_dependent);
8409 gas_assert (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE);
8411 /* Find the loop frag. */
8412 first_insn = next_non_empty_frag (fragP);
8413 /* Now find the first insn frag. */
8414 first_insn = next_non_empty_frag (first_insn);
8416 is_loop = next_frag_opcode_is_loop (fragP, &opcode);
8417 gas_assert (is_loop);
8418 loop_insn_size = xg_get_single_size (opcode);
8420 pre_opcode_bytes = next_frag_pre_opcode_bytes (fragP);
8421 pre_opcode_bytes += loop_insn_size;
8423 /* For loops, the alignment depends on the size of the
8424 instruction following the loop, not the LOOP instruction. */
8426 if (first_insn == NULL)
8427 first_insn_size = xtensa_fetch_width;
8428 else
8429 first_insn_size = get_loop_align_size (frag_format_size (first_insn));
8431 /* If it was 8, then we'll need a larger alignment for the section. */
8432 align_power = get_text_align_power (first_insn_size);
8433 record_alignment (now_seg, align_power);
8435 fill_size = get_text_align_fill_size
8436 (address + pre_opcode_bytes, align_power, first_insn_size, TRUE,
8437 fragP->tc_frag_data.is_no_density);
8439 return address + fill_size;
8443 /* 3 mechanisms for relaxing an alignment:
8445 Align to a power of 2.
8446 Align so the next fragment's instruction does not cross a word boundary.
8447 Align the current instruction so that if the next instruction
8448 were 3 bytes, it would not cross a word boundary.
8450 We can align with:
8452 zeros - This is easy; always insert zeros.
8453 nops - 3-byte and 2-byte instructions
8454 2 - 2-byte nop
8455 3 - 3-byte nop
8456 4 - 2 2-byte nops
8457 >=5 : 3-byte instruction + fn (n-3)
8458 widening - widen previous instructions. */
8460 static offsetT
8461 get_aligned_diff (fragS *fragP, addressT address, offsetT *max_diff)
8463 addressT target_address, loop_insn_offset;
8464 int target_size;
8465 xtensa_opcode loop_opcode;
8466 bfd_boolean is_loop;
8467 int align_power;
8468 offsetT opt_diff;
8469 offsetT branch_align;
8470 fragS *loop_frag;
8472 gas_assert (fragP->fr_type == rs_machine_dependent);
8473 switch (fragP->fr_subtype)
8475 case RELAX_DESIRE_ALIGN:
8476 target_size = next_frag_format_size (fragP);
8477 if (target_size == XTENSA_UNDEFINED)
8478 target_size = 3;
8479 align_power = branch_align_power (now_seg);
8480 branch_align = 1 << align_power;
8481 /* Don't count on the section alignment being as large as the target. */
8482 if (target_size > branch_align)
8483 target_size = branch_align;
8484 opt_diff = get_text_align_fill_size (address, align_power,
8485 target_size, FALSE, FALSE);
8487 *max_diff = (opt_diff + branch_align
8488 - (target_size + ((address + opt_diff) % branch_align)));
8489 gas_assert (*max_diff >= opt_diff);
8490 return opt_diff;
8492 case RELAX_ALIGN_NEXT_OPCODE:
8493 /* The next non-empty frag after this one holds the LOOP instruction
8494 that needs to be aligned. The required alignment depends on the
8495 size of the next non-empty frag after the loop frag, i.e., the
8496 first instruction in the loop. */
8497 loop_frag = next_non_empty_frag (fragP);
8498 target_size = get_loop_align_size (next_frag_format_size (loop_frag));
8499 loop_insn_offset = 0;
8500 is_loop = next_frag_opcode_is_loop (fragP, &loop_opcode);
8501 gas_assert (is_loop);
8503 /* If the loop has been expanded then the LOOP instruction
8504 could be at an offset from this fragment. */
8505 if (loop_frag->tc_frag_data.slot_subtypes[0] != RELAX_IMMED)
8506 loop_insn_offset = get_expanded_loop_offset (loop_opcode);
8508 /* In an ideal world, which is what we are shooting for here,
8509 we wouldn't need to use any NOPs immediately prior to the
8510 LOOP instruction. If this approach fails, relax_frag_loop_align
8511 will call get_noop_aligned_address. */
8512 target_address =
8513 address + loop_insn_offset + xg_get_single_size (loop_opcode);
8514 align_power = get_text_align_power (target_size);
8515 opt_diff = get_text_align_fill_size (target_address, align_power,
8516 target_size, FALSE, FALSE);
8518 *max_diff = xtensa_fetch_width
8519 - ((target_address + opt_diff) % xtensa_fetch_width)
8520 - target_size + opt_diff;
8521 gas_assert (*max_diff >= opt_diff);
8522 return opt_diff;
8524 default:
8525 break;
8527 gas_assert (0);
8528 return 0;
8532 /* md_relax_frag Hook and Helper Functions. */
8534 static long relax_frag_loop_align (fragS *, long);
8535 static long relax_frag_for_align (fragS *, long);
8536 static long relax_frag_immed
8537 (segT, fragS *, long, int, xtensa_format, int, int *, bfd_boolean);
8540 /* Return the number of bytes added to this fragment, given that the
8541 input has been stretched already by "stretch". */
8543 long
8544 xtensa_relax_frag (fragS *fragP, long stretch, int *stretched_p)
8546 xtensa_isa isa = xtensa_default_isa;
8547 int unreported = fragP->tc_frag_data.unreported_expansion;
8548 long new_stretch = 0;
8549 char *file_name;
8550 unsigned line;
8551 int lit_size;
8552 static xtensa_insnbuf vbuf = NULL;
8553 int slot, num_slots;
8554 xtensa_format fmt;
8556 as_where (&file_name, &line);
8557 new_logical_line (fragP->fr_file, fragP->fr_line);
8559 fragP->tc_frag_data.unreported_expansion = 0;
8561 switch (fragP->fr_subtype)
8563 case RELAX_ALIGN_NEXT_OPCODE:
8564 /* Always convert. */
8565 if (fragP->tc_frag_data.relax_seen)
8566 new_stretch = relax_frag_loop_align (fragP, stretch);
8567 break;
8569 case RELAX_LOOP_END:
8570 /* Do nothing. */
8571 break;
8573 case RELAX_LOOP_END_ADD_NOP:
8574 /* Add a NOP and switch to .fill 0. */
8575 new_stretch = relax_frag_add_nop (fragP);
8576 frag_wane (fragP);
8577 break;
8579 case RELAX_DESIRE_ALIGN:
8580 /* Do nothing. The narrowing before this frag will either align
8581 it or not. */
8582 break;
8584 case RELAX_LITERAL:
8585 case RELAX_LITERAL_FINAL:
8586 return 0;
8588 case RELAX_LITERAL_NR:
8589 lit_size = 4;
8590 fragP->fr_subtype = RELAX_LITERAL_FINAL;
8591 gas_assert (unreported == lit_size);
8592 memset (&fragP->fr_literal[fragP->fr_fix], 0, 4);
8593 fragP->fr_var -= lit_size;
8594 fragP->fr_fix += lit_size;
8595 new_stretch = 4;
8596 break;
8598 case RELAX_SLOTS:
8599 if (vbuf == NULL)
8600 vbuf = xtensa_insnbuf_alloc (isa);
8602 xtensa_insnbuf_from_chars
8603 (isa, vbuf, (unsigned char *) fragP->fr_opcode, 0);
8604 fmt = xtensa_format_decode (isa, vbuf);
8605 num_slots = xtensa_format_num_slots (isa, fmt);
8607 for (slot = 0; slot < num_slots; slot++)
8609 switch (fragP->tc_frag_data.slot_subtypes[slot])
8611 case RELAX_NARROW:
8612 if (fragP->tc_frag_data.relax_seen)
8613 new_stretch += relax_frag_for_align (fragP, stretch);
8614 break;
8616 case RELAX_IMMED:
8617 case RELAX_IMMED_STEP1:
8618 case RELAX_IMMED_STEP2:
8619 case RELAX_IMMED_STEP3:
8620 /* Place the immediate. */
8621 new_stretch += relax_frag_immed
8622 (now_seg, fragP, stretch,
8623 fragP->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED,
8624 fmt, slot, stretched_p, FALSE);
8625 break;
8627 default:
8628 /* This is OK; see the note in xg_assemble_vliw_tokens. */
8629 break;
8632 break;
8634 case RELAX_LITERAL_POOL_BEGIN:
8635 case RELAX_LITERAL_POOL_END:
8636 case RELAX_MAYBE_UNREACHABLE:
8637 case RELAX_MAYBE_DESIRE_ALIGN:
8638 /* No relaxation required. */
8639 break;
8641 case RELAX_FILL_NOP:
8642 case RELAX_UNREACHABLE:
8643 if (fragP->tc_frag_data.relax_seen)
8644 new_stretch += relax_frag_for_align (fragP, stretch);
8645 break;
8647 default:
8648 as_bad (_("bad relaxation state"));
8651 /* Tell gas we need another relaxation pass. */
8652 if (! fragP->tc_frag_data.relax_seen)
8654 fragP->tc_frag_data.relax_seen = TRUE;
8655 *stretched_p = 1;
8658 new_logical_line (file_name, line);
8659 return new_stretch;
8663 static long
8664 relax_frag_loop_align (fragS *fragP, long stretch)
8666 addressT old_address, old_next_address, old_size;
8667 addressT new_address, new_next_address, new_size;
8668 addressT growth;
8670 /* All the frags with relax_frag_for_alignment prior to this one in the
8671 section have been done, hopefully eliminating the need for a NOP here.
8672 But, this will put it in if necessary. */
8674 /* Calculate the old address of this fragment and the next fragment. */
8675 old_address = fragP->fr_address - stretch;
8676 old_next_address = (fragP->fr_address - stretch + fragP->fr_fix +
8677 fragP->tc_frag_data.text_expansion[0]);
8678 old_size = old_next_address - old_address;
8680 /* Calculate the new address of this fragment and the next fragment. */
8681 new_address = fragP->fr_address;
8682 new_next_address =
8683 get_noop_aligned_address (fragP, fragP->fr_address + fragP->fr_fix);
8684 new_size = new_next_address - new_address;
8686 growth = new_size - old_size;
8688 /* Fix up the text_expansion field and return the new growth. */
8689 fragP->tc_frag_data.text_expansion[0] += growth;
8690 return growth;
8694 /* Add a NOP instruction. */
8696 static long
8697 relax_frag_add_nop (fragS *fragP)
8699 char *nop_buf = fragP->fr_literal + fragP->fr_fix;
8700 int length = fragP->tc_frag_data.is_no_density ? 3 : 2;
8701 assemble_nop (length, nop_buf);
8702 fragP->tc_frag_data.is_insn = TRUE;
8704 if (fragP->fr_var < length)
8706 as_fatal (_("fr_var (%ld) < length (%d)"), (long) fragP->fr_var, length);
8707 return 0;
8710 fragP->fr_fix += length;
8711 fragP->fr_var -= length;
8712 return length;
8716 static long future_alignment_required (fragS *, long);
8718 static long
8719 relax_frag_for_align (fragS *fragP, long stretch)
8721 /* Overview of the relaxation procedure for alignment:
8722 We can widen with NOPs or by widening instructions or by filling
8723 bytes after jump instructions. Find the opportune places and widen
8724 them if necessary. */
8726 long stretch_me;
8727 long diff;
8729 gas_assert (fragP->fr_subtype == RELAX_FILL_NOP
8730 || fragP->fr_subtype == RELAX_UNREACHABLE
8731 || (fragP->fr_subtype == RELAX_SLOTS
8732 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW));
8734 stretch_me = future_alignment_required (fragP, stretch);
8735 diff = stretch_me - fragP->tc_frag_data.text_expansion[0];
8736 if (diff == 0)
8737 return 0;
8739 if (diff < 0)
8741 /* We expanded on a previous pass. Can we shrink now? */
8742 long shrink = fragP->tc_frag_data.text_expansion[0] - stretch_me;
8743 if (shrink <= stretch && stretch > 0)
8745 fragP->tc_frag_data.text_expansion[0] = stretch_me;
8746 return -shrink;
8748 return 0;
8751 /* Below here, diff > 0. */
8752 fragP->tc_frag_data.text_expansion[0] = stretch_me;
8754 return diff;
8758 /* Return the address of the next frag that should be aligned.
8760 By "address" we mean the address it _would_ be at if there
8761 is no action taken to align it between here and the target frag.
8762 In other words, if no narrows and no fill nops are used between
8763 here and the frag to align, _even_if_ some of the frags we use
8764 to align targets have already expanded on a previous relaxation
8765 pass.
8767 Also, count each frag that may be used to help align the target.
8769 Return 0 if there are no frags left in the chain that need to be
8770 aligned. */
8772 static addressT
8773 find_address_of_next_align_frag (fragS **fragPP,
8774 int *wide_nops,
8775 int *narrow_nops,
8776 int *widens,
8777 bfd_boolean *paddable)
8779 fragS *fragP = *fragPP;
8780 addressT address = fragP->fr_address;
8782 /* Do not reset the counts to 0. */
8784 while (fragP)
8786 /* Limit this to a small search. */
8787 if (*widens >= (int) xtensa_fetch_width)
8789 *fragPP = fragP;
8790 return 0;
8792 address += fragP->fr_fix;
8794 if (fragP->fr_type == rs_fill)
8795 address += fragP->fr_offset * fragP->fr_var;
8796 else if (fragP->fr_type == rs_machine_dependent)
8798 switch (fragP->fr_subtype)
8800 case RELAX_UNREACHABLE:
8801 *paddable = TRUE;
8802 break;
8804 case RELAX_FILL_NOP:
8805 (*wide_nops)++;
8806 if (!fragP->tc_frag_data.is_no_density)
8807 (*narrow_nops)++;
8808 break;
8810 case RELAX_SLOTS:
8811 if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
8813 (*widens)++;
8814 break;
8816 address += total_frag_text_expansion (fragP);;
8817 break;
8819 case RELAX_IMMED:
8820 address += fragP->tc_frag_data.text_expansion[0];
8821 break;
8823 case RELAX_ALIGN_NEXT_OPCODE:
8824 case RELAX_DESIRE_ALIGN:
8825 *fragPP = fragP;
8826 return address;
8828 case RELAX_MAYBE_UNREACHABLE:
8829 case RELAX_MAYBE_DESIRE_ALIGN:
8830 /* Do nothing. */
8831 break;
8833 default:
8834 /* Just punt if we don't know the type. */
8835 *fragPP = fragP;
8836 return 0;
8839 else
8841 /* Just punt if we don't know the type. */
8842 *fragPP = fragP;
8843 return 0;
8845 fragP = fragP->fr_next;
8848 *fragPP = fragP;
8849 return 0;
8853 static long bytes_to_stretch (fragS *, int, int, int, int);
8855 static long
8856 future_alignment_required (fragS *fragP, long stretch ATTRIBUTE_UNUSED)
8858 fragS *this_frag = fragP;
8859 long address;
8860 int num_widens = 0;
8861 int wide_nops = 0;
8862 int narrow_nops = 0;
8863 bfd_boolean paddable = FALSE;
8864 offsetT local_opt_diff;
8865 offsetT opt_diff;
8866 offsetT max_diff;
8867 int stretch_amount = 0;
8868 int local_stretch_amount;
8869 int global_stretch_amount;
8871 address = find_address_of_next_align_frag
8872 (&fragP, &wide_nops, &narrow_nops, &num_widens, &paddable);
8874 if (!address)
8876 if (this_frag->tc_frag_data.is_aligning_branch)
8877 this_frag->tc_frag_data.slot_subtypes[0] = RELAX_IMMED;
8878 else
8879 frag_wane (this_frag);
8881 else
8883 local_opt_diff = get_aligned_diff (fragP, address, &max_diff);
8884 opt_diff = local_opt_diff;
8885 gas_assert (opt_diff >= 0);
8886 gas_assert (max_diff >= opt_diff);
8887 if (max_diff == 0)
8888 return 0;
8890 if (fragP)
8891 fragP = fragP->fr_next;
8893 while (fragP && opt_diff < max_diff && address)
8895 /* We only use these to determine if we can exit early
8896 because there will be plenty of ways to align future
8897 align frags. */
8898 int glob_widens = 0;
8899 int dnn = 0;
8900 int dw = 0;
8901 bfd_boolean glob_pad = 0;
8902 address = find_address_of_next_align_frag
8903 (&fragP, &glob_widens, &dnn, &dw, &glob_pad);
8904 /* If there is a padable portion, then skip. */
8905 if (glob_pad || glob_widens >= (1 << branch_align_power (now_seg)))
8906 address = 0;
8908 if (address)
8910 offsetT next_m_diff;
8911 offsetT next_o_diff;
8913 /* Downrange frags haven't had stretch added to them yet. */
8914 address += stretch;
8916 /* The address also includes any text expansion from this
8917 frag in a previous pass, but we don't want that. */
8918 address -= this_frag->tc_frag_data.text_expansion[0];
8920 /* Assume we are going to move at least opt_diff. In
8921 reality, we might not be able to, but assuming that
8922 we will helps catch cases where moving opt_diff pushes
8923 the next target from aligned to unaligned. */
8924 address += opt_diff;
8926 next_o_diff = get_aligned_diff (fragP, address, &next_m_diff);
8928 /* Now cleanup for the adjustments to address. */
8929 next_o_diff += opt_diff;
8930 next_m_diff += opt_diff;
8931 if (next_o_diff <= max_diff && next_o_diff > opt_diff)
8932 opt_diff = next_o_diff;
8933 if (next_m_diff < max_diff)
8934 max_diff = next_m_diff;
8935 fragP = fragP->fr_next;
8939 /* If there are enough wideners in between, do it. */
8940 if (paddable)
8942 if (this_frag->fr_subtype == RELAX_UNREACHABLE)
8944 gas_assert (opt_diff <= UNREACHABLE_MAX_WIDTH);
8945 return opt_diff;
8947 return 0;
8949 local_stretch_amount
8950 = bytes_to_stretch (this_frag, wide_nops, narrow_nops,
8951 num_widens, local_opt_diff);
8952 global_stretch_amount
8953 = bytes_to_stretch (this_frag, wide_nops, narrow_nops,
8954 num_widens, opt_diff);
8955 /* If the condition below is true, then the frag couldn't
8956 stretch the correct amount for the global case, so we just
8957 optimize locally. We'll rely on the subsequent frags to get
8958 the correct alignment in the global case. */
8959 if (global_stretch_amount < local_stretch_amount)
8960 stretch_amount = local_stretch_amount;
8961 else
8962 stretch_amount = global_stretch_amount;
8964 if (this_frag->fr_subtype == RELAX_SLOTS
8965 && this_frag->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
8966 gas_assert (stretch_amount <= 1);
8967 else if (this_frag->fr_subtype == RELAX_FILL_NOP)
8969 if (this_frag->tc_frag_data.is_no_density)
8970 gas_assert (stretch_amount == 3 || stretch_amount == 0);
8971 else
8972 gas_assert (stretch_amount <= 3);
8975 return stretch_amount;
8979 /* The idea: widen everything you can to get a target or loop aligned,
8980 then start using NOPs.
8982 When we must have a NOP, here is a table of how we decide
8983 (so you don't have to fight through the control flow below):
8985 wide_nops = the number of wide NOPs available for aligning
8986 narrow_nops = the number of narrow NOPs available for aligning
8987 (a subset of wide_nops)
8988 widens = the number of narrow instructions that should be widened
8990 Desired wide narrow
8991 Diff nop nop widens
8992 1 0 0 1
8993 2 0 1 0
8994 3a 1 0 0
8995 b 0 1 1 (case 3a makes this case unnecessary)
8996 4a 1 0 1
8997 b 0 2 0
8998 c 0 1 2 (case 4a makes this case unnecessary)
8999 5a 1 0 2
9000 b 1 1 0
9001 c 0 2 1 (case 5b makes this case unnecessary)
9002 6a 2 0 0
9003 b 1 0 3
9004 c 0 1 4 (case 6b makes this case unnecessary)
9005 d 1 1 1 (case 6a makes this case unnecessary)
9006 e 0 2 2 (case 6a makes this case unnecessary)
9007 f 0 3 0 (case 6a makes this case unnecessary)
9008 7a 1 0 4
9009 b 2 0 1
9010 c 1 1 2 (case 7b makes this case unnecessary)
9011 d 0 1 5 (case 7a makes this case unnecessary)
9012 e 0 2 3 (case 7b makes this case unnecessary)
9013 f 0 3 1 (case 7b makes this case unnecessary)
9014 g 1 2 1 (case 7b makes this case unnecessary)
9017 static long
9018 bytes_to_stretch (fragS *this_frag,
9019 int wide_nops,
9020 int narrow_nops,
9021 int num_widens,
9022 int desired_diff)
9024 int bytes_short = desired_diff - num_widens;
9026 gas_assert (desired_diff >= 0 && desired_diff < 8);
9027 if (desired_diff == 0)
9028 return 0;
9030 gas_assert (wide_nops > 0 || num_widens > 0);
9032 /* Always prefer widening to NOP-filling. */
9033 if (bytes_short < 0)
9035 /* There are enough RELAX_NARROW frags after this one
9036 to align the target without widening this frag in any way. */
9037 return 0;
9040 if (bytes_short == 0)
9042 /* Widen every narrow between here and the align target
9043 and the align target will be properly aligned. */
9044 if (this_frag->fr_subtype == RELAX_FILL_NOP)
9045 return 0;
9046 else
9047 return 1;
9050 /* From here we will need at least one NOP to get an alignment.
9051 However, we may not be able to align at all, in which case,
9052 don't widen. */
9053 if (this_frag->fr_subtype == RELAX_FILL_NOP)
9055 switch (desired_diff)
9057 case 1:
9058 return 0;
9059 case 2:
9060 if (!this_frag->tc_frag_data.is_no_density && narrow_nops == 1)
9061 return 2; /* case 2 */
9062 return 0;
9063 case 3:
9064 if (wide_nops > 1)
9065 return 0;
9066 else
9067 return 3; /* case 3a */
9068 case 4:
9069 if (num_widens >= 1 && wide_nops == 1)
9070 return 3; /* case 4a */
9071 if (!this_frag->tc_frag_data.is_no_density && narrow_nops == 2)
9072 return 2; /* case 4b */
9073 return 0;
9074 case 5:
9075 if (num_widens >= 2 && wide_nops == 1)
9076 return 3; /* case 5a */
9077 /* We will need two nops. Are there enough nops
9078 between here and the align target? */
9079 if (wide_nops < 2 || narrow_nops == 0)
9080 return 0;
9081 /* Are there other nops closer that can serve instead? */
9082 if (wide_nops > 2 && narrow_nops > 1)
9083 return 0;
9084 /* Take the density one first, because there might not be
9085 another density one available. */
9086 if (!this_frag->tc_frag_data.is_no_density)
9087 return 2; /* case 5b narrow */
9088 else
9089 return 3; /* case 5b wide */
9090 return 0;
9091 case 6:
9092 if (wide_nops == 2)
9093 return 3; /* case 6a */
9094 else if (num_widens >= 3 && wide_nops == 1)
9095 return 3; /* case 6b */
9096 return 0;
9097 case 7:
9098 if (wide_nops == 1 && num_widens >= 4)
9099 return 3; /* case 7a */
9100 else if (wide_nops == 2 && num_widens >= 1)
9101 return 3; /* case 7b */
9102 return 0;
9103 default:
9104 gas_assert (0);
9107 else
9109 /* We will need a NOP no matter what, but should we widen
9110 this instruction to help?
9112 This is a RELAX_NARROW frag. */
9113 switch (desired_diff)
9115 case 1:
9116 gas_assert (0);
9117 return 0;
9118 case 2:
9119 case 3:
9120 return 0;
9121 case 4:
9122 if (wide_nops >= 1 && num_widens == 1)
9123 return 1; /* case 4a */
9124 return 0;
9125 case 5:
9126 if (wide_nops >= 1 && num_widens == 2)
9127 return 1; /* case 5a */
9128 return 0;
9129 case 6:
9130 if (wide_nops >= 2)
9131 return 0; /* case 6a */
9132 else if (wide_nops >= 1 && num_widens == 3)
9133 return 1; /* case 6b */
9134 return 0;
9135 case 7:
9136 if (wide_nops >= 1 && num_widens == 4)
9137 return 1; /* case 7a */
9138 else if (wide_nops >= 2 && num_widens == 1)
9139 return 1; /* case 7b */
9140 return 0;
9141 default:
9142 gas_assert (0);
9143 return 0;
9146 gas_assert (0);
9147 return 0;
9151 static long
9152 relax_frag_immed (segT segP,
9153 fragS *fragP,
9154 long stretch,
9155 int min_steps,
9156 xtensa_format fmt,
9157 int slot,
9158 int *stretched_p,
9159 bfd_boolean estimate_only)
9161 TInsn tinsn;
9162 int old_size;
9163 bfd_boolean negatable_branch = FALSE;
9164 bfd_boolean branch_jmp_to_next = FALSE;
9165 bfd_boolean from_wide_insn = FALSE;
9166 xtensa_isa isa = xtensa_default_isa;
9167 IStack istack;
9168 offsetT frag_offset;
9169 int num_steps;
9170 int num_text_bytes, num_literal_bytes;
9171 int literal_diff, total_text_diff, this_text_diff;
9173 gas_assert (fragP->fr_opcode != NULL);
9175 xg_clear_vinsn (&cur_vinsn);
9176 vinsn_from_chars (&cur_vinsn, fragP->fr_opcode);
9177 if (cur_vinsn.num_slots > 1)
9178 from_wide_insn = TRUE;
9180 tinsn = cur_vinsn.slots[slot];
9181 tinsn_immed_from_frag (&tinsn, fragP, slot);
9183 if (estimate_only && xtensa_opcode_is_loop (isa, tinsn.opcode) == 1)
9184 return 0;
9186 if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform)
9187 branch_jmp_to_next = is_branch_jmp_to_next (&tinsn, fragP);
9189 negatable_branch = (xtensa_opcode_is_branch (isa, tinsn.opcode) == 1);
9191 old_size = xtensa_format_length (isa, fmt);
9193 /* Special case: replace a branch to the next instruction with a NOP.
9194 This is required to work around a hardware bug in T1040.0 and also
9195 serves as an optimization. */
9197 if (branch_jmp_to_next
9198 && ((old_size == 2) || (old_size == 3))
9199 && !next_frag_is_loop_target (fragP))
9200 return 0;
9202 /* Here is the fun stuff: Get the immediate field from this
9203 instruction. If it fits, we are done. If not, find the next
9204 instruction sequence that fits. */
9206 frag_offset = fragP->fr_opcode - fragP->fr_literal;
9207 istack_init (&istack);
9208 num_steps = xg_assembly_relax (&istack, &tinsn, segP, fragP, frag_offset,
9209 min_steps, stretch);
9210 gas_assert (num_steps >= min_steps && num_steps <= RELAX_IMMED_MAXSTEPS);
9212 fragP->tc_frag_data.slot_subtypes[slot] = (int) RELAX_IMMED + num_steps;
9214 /* Figure out the number of bytes needed. */
9215 num_literal_bytes = get_num_stack_literal_bytes (&istack);
9216 literal_diff
9217 = num_literal_bytes - fragP->tc_frag_data.literal_expansion[slot];
9218 num_text_bytes = get_num_stack_text_bytes (&istack);
9220 if (from_wide_insn)
9222 int first = 0;
9223 while (istack.insn[first].opcode == XTENSA_UNDEFINED)
9224 first++;
9226 num_text_bytes += old_size;
9227 if (opcode_fits_format_slot (istack.insn[first].opcode, fmt, slot))
9228 num_text_bytes -= xg_get_single_size (istack.insn[first].opcode);
9229 else
9231 /* The first instruction in the relaxed sequence will go after
9232 the current wide instruction, and thus its symbolic immediates
9233 might not fit. */
9235 istack_init (&istack);
9236 num_steps = xg_assembly_relax (&istack, &tinsn, segP, fragP,
9237 frag_offset + old_size,
9238 min_steps, stretch + old_size);
9239 gas_assert (num_steps >= min_steps && num_steps <= RELAX_IMMED_MAXSTEPS);
9241 fragP->tc_frag_data.slot_subtypes[slot]
9242 = (int) RELAX_IMMED + num_steps;
9244 num_literal_bytes = get_num_stack_literal_bytes (&istack);
9245 literal_diff
9246 = num_literal_bytes - fragP->tc_frag_data.literal_expansion[slot];
9248 num_text_bytes = get_num_stack_text_bytes (&istack) + old_size;
9252 total_text_diff = num_text_bytes - old_size;
9253 this_text_diff = total_text_diff - fragP->tc_frag_data.text_expansion[slot];
9255 /* It MUST get larger. If not, we could get an infinite loop. */
9256 gas_assert (num_text_bytes >= 0);
9257 gas_assert (literal_diff >= 0);
9258 gas_assert (total_text_diff >= 0);
9260 fragP->tc_frag_data.text_expansion[slot] = total_text_diff;
9261 fragP->tc_frag_data.literal_expansion[slot] = num_literal_bytes;
9262 gas_assert (fragP->tc_frag_data.text_expansion[slot] >= 0);
9263 gas_assert (fragP->tc_frag_data.literal_expansion[slot] >= 0);
9265 /* Find the associated expandable literal for this. */
9266 if (literal_diff != 0)
9268 fragS *lit_fragP = fragP->tc_frag_data.literal_frags[slot];
9269 if (lit_fragP)
9271 gas_assert (literal_diff == 4);
9272 lit_fragP->tc_frag_data.unreported_expansion += literal_diff;
9274 /* We expect that the literal section state has NOT been
9275 modified yet. */
9276 gas_assert (lit_fragP->fr_type == rs_machine_dependent
9277 && lit_fragP->fr_subtype == RELAX_LITERAL);
9278 lit_fragP->fr_subtype = RELAX_LITERAL_NR;
9280 /* We need to mark this section for another iteration
9281 of relaxation. */
9282 (*stretched_p)++;
9286 if (negatable_branch && istack.ninsn > 1)
9287 update_next_frag_state (fragP);
9289 return this_text_diff;
9293 /* md_convert_frag Hook and Helper Functions. */
9295 static void convert_frag_align_next_opcode (fragS *);
9296 static void convert_frag_narrow (segT, fragS *, xtensa_format, int);
9297 static void convert_frag_fill_nop (fragS *);
9298 static void convert_frag_immed (segT, fragS *, int, xtensa_format, int);
9300 void
9301 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec, fragS *fragp)
9303 static xtensa_insnbuf vbuf = NULL;
9304 xtensa_isa isa = xtensa_default_isa;
9305 int slot;
9306 int num_slots;
9307 xtensa_format fmt;
9308 char *file_name;
9309 unsigned line;
9311 as_where (&file_name, &line);
9312 new_logical_line (fragp->fr_file, fragp->fr_line);
9314 switch (fragp->fr_subtype)
9316 case RELAX_ALIGN_NEXT_OPCODE:
9317 /* Always convert. */
9318 convert_frag_align_next_opcode (fragp);
9319 break;
9321 case RELAX_DESIRE_ALIGN:
9322 /* Do nothing. If not aligned already, too bad. */
9323 break;
9325 case RELAX_LITERAL:
9326 case RELAX_LITERAL_FINAL:
9327 break;
9329 case RELAX_SLOTS:
9330 if (vbuf == NULL)
9331 vbuf = xtensa_insnbuf_alloc (isa);
9333 xtensa_insnbuf_from_chars
9334 (isa, vbuf, (unsigned char *) fragp->fr_opcode, 0);
9335 fmt = xtensa_format_decode (isa, vbuf);
9336 num_slots = xtensa_format_num_slots (isa, fmt);
9338 for (slot = 0; slot < num_slots; slot++)
9340 switch (fragp->tc_frag_data.slot_subtypes[slot])
9342 case RELAX_NARROW:
9343 convert_frag_narrow (sec, fragp, fmt, slot);
9344 break;
9346 case RELAX_IMMED:
9347 case RELAX_IMMED_STEP1:
9348 case RELAX_IMMED_STEP2:
9349 case RELAX_IMMED_STEP3:
9350 /* Place the immediate. */
9351 convert_frag_immed
9352 (sec, fragp,
9353 fragp->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED,
9354 fmt, slot);
9355 break;
9357 default:
9358 /* This is OK because some slots could have
9359 relaxations and others have none. */
9360 break;
9363 break;
9365 case RELAX_UNREACHABLE:
9366 memset (&fragp->fr_literal[fragp->fr_fix], 0, fragp->fr_var);
9367 fragp->fr_fix += fragp->tc_frag_data.text_expansion[0];
9368 fragp->fr_var -= fragp->tc_frag_data.text_expansion[0];
9369 frag_wane (fragp);
9370 break;
9372 case RELAX_MAYBE_UNREACHABLE:
9373 case RELAX_MAYBE_DESIRE_ALIGN:
9374 frag_wane (fragp);
9375 break;
9377 case RELAX_FILL_NOP:
9378 convert_frag_fill_nop (fragp);
9379 break;
9381 case RELAX_LITERAL_NR:
9382 if (use_literal_section)
9384 /* This should have been handled during relaxation. When
9385 relaxing a code segment, literals sometimes need to be
9386 added to the corresponding literal segment. If that
9387 literal segment has already been relaxed, then we end up
9388 in this situation. Marking the literal segments as data
9389 would make this happen less often (since GAS always relaxes
9390 code before data), but we could still get into trouble if
9391 there are instructions in a segment that is not marked as
9392 containing code. Until we can implement a better solution,
9393 cheat and adjust the addresses of all the following frags.
9394 This could break subsequent alignments, but the linker's
9395 literal coalescing will do that anyway. */
9397 fragS *f;
9398 fragp->fr_subtype = RELAX_LITERAL_FINAL;
9399 gas_assert (fragp->tc_frag_data.unreported_expansion == 4);
9400 memset (&fragp->fr_literal[fragp->fr_fix], 0, 4);
9401 fragp->fr_var -= 4;
9402 fragp->fr_fix += 4;
9403 for (f = fragp->fr_next; f; f = f->fr_next)
9404 f->fr_address += 4;
9406 else
9407 as_bad (_("invalid relaxation fragment result"));
9408 break;
9411 fragp->fr_var = 0;
9412 new_logical_line (file_name, line);
9416 static void
9417 convert_frag_align_next_opcode (fragS *fragp)
9419 char *nop_buf; /* Location for Writing. */
9420 bfd_boolean use_no_density = fragp->tc_frag_data.is_no_density;
9421 addressT aligned_address;
9422 offsetT fill_size;
9423 int nop, nop_count;
9425 aligned_address = get_noop_aligned_address (fragp, fragp->fr_address +
9426 fragp->fr_fix);
9427 fill_size = aligned_address - (fragp->fr_address + fragp->fr_fix);
9428 nop_count = get_text_align_nop_count (fill_size, use_no_density);
9429 nop_buf = fragp->fr_literal + fragp->fr_fix;
9431 for (nop = 0; nop < nop_count; nop++)
9433 int nop_size;
9434 nop_size = get_text_align_nth_nop_size (fill_size, nop, use_no_density);
9436 assemble_nop (nop_size, nop_buf);
9437 nop_buf += nop_size;
9440 fragp->fr_fix += fill_size;
9441 fragp->fr_var -= fill_size;
9445 static void
9446 convert_frag_narrow (segT segP, fragS *fragP, xtensa_format fmt, int slot)
9448 TInsn tinsn, single_target;
9449 int size, old_size, diff;
9450 offsetT frag_offset;
9452 gas_assert (slot == 0);
9453 tinsn_from_chars (&tinsn, fragP->fr_opcode, 0);
9455 if (fragP->tc_frag_data.is_aligning_branch == 1)
9457 gas_assert (fragP->tc_frag_data.text_expansion[0] == 1
9458 || fragP->tc_frag_data.text_expansion[0] == 0);
9459 convert_frag_immed (segP, fragP, fragP->tc_frag_data.text_expansion[0],
9460 fmt, slot);
9461 return;
9464 if (fragP->tc_frag_data.text_expansion[0] == 0)
9466 /* No conversion. */
9467 fragP->fr_var = 0;
9468 return;
9471 gas_assert (fragP->fr_opcode != NULL);
9473 /* Frags in this relaxation state should only contain
9474 single instruction bundles. */
9475 tinsn_immed_from_frag (&tinsn, fragP, 0);
9477 /* Just convert it to a wide form.... */
9478 size = 0;
9479 old_size = xg_get_single_size (tinsn.opcode);
9481 tinsn_init (&single_target);
9482 frag_offset = fragP->fr_opcode - fragP->fr_literal;
9484 if (! xg_is_single_relaxable_insn (&tinsn, &single_target, FALSE))
9486 as_bad (_("unable to widen instruction"));
9487 return;
9490 size = xg_get_single_size (single_target.opcode);
9491 xg_emit_insn_to_buf (&single_target, fragP->fr_opcode, fragP,
9492 frag_offset, TRUE);
9494 diff = size - old_size;
9495 gas_assert (diff >= 0);
9496 gas_assert (diff <= fragP->fr_var);
9497 fragP->fr_var -= diff;
9498 fragP->fr_fix += diff;
9500 /* clean it up */
9501 fragP->fr_var = 0;
9505 static void
9506 convert_frag_fill_nop (fragS *fragP)
9508 char *loc = &fragP->fr_literal[fragP->fr_fix];
9509 int size = fragP->tc_frag_data.text_expansion[0];
9510 gas_assert ((unsigned) size == (fragP->fr_next->fr_address
9511 - fragP->fr_address - fragP->fr_fix));
9512 if (size == 0)
9514 /* No conversion. */
9515 fragP->fr_var = 0;
9516 return;
9518 assemble_nop (size, loc);
9519 fragP->tc_frag_data.is_insn = TRUE;
9520 fragP->fr_var -= size;
9521 fragP->fr_fix += size;
9522 frag_wane (fragP);
9526 static fixS *fix_new_exp_in_seg
9527 (segT, subsegT, fragS *, int, int, expressionS *, int,
9528 bfd_reloc_code_real_type);
9529 static void convert_frag_immed_finish_loop (segT, fragS *, TInsn *);
9531 static void
9532 convert_frag_immed (segT segP,
9533 fragS *fragP,
9534 int min_steps,
9535 xtensa_format fmt,
9536 int slot)
9538 char *immed_instr = fragP->fr_opcode;
9539 TInsn orig_tinsn;
9540 bfd_boolean expanded = FALSE;
9541 bfd_boolean branch_jmp_to_next = FALSE;
9542 char *fr_opcode = fragP->fr_opcode;
9543 xtensa_isa isa = xtensa_default_isa;
9544 bfd_boolean from_wide_insn = FALSE;
9545 int bytes;
9546 bfd_boolean is_loop;
9548 gas_assert (fr_opcode != NULL);
9550 xg_clear_vinsn (&cur_vinsn);
9552 vinsn_from_chars (&cur_vinsn, fr_opcode);
9553 if (cur_vinsn.num_slots > 1)
9554 from_wide_insn = TRUE;
9556 orig_tinsn = cur_vinsn.slots[slot];
9557 tinsn_immed_from_frag (&orig_tinsn, fragP, slot);
9559 is_loop = xtensa_opcode_is_loop (xtensa_default_isa, orig_tinsn.opcode) == 1;
9561 if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform)
9562 branch_jmp_to_next = is_branch_jmp_to_next (&orig_tinsn, fragP);
9564 if (branch_jmp_to_next && !next_frag_is_loop_target (fragP))
9566 /* Conversion just inserts a NOP and marks the fix as completed. */
9567 bytes = xtensa_format_length (isa, fmt);
9568 if (bytes >= 4)
9570 cur_vinsn.slots[slot].opcode =
9571 xtensa_format_slot_nop_opcode (isa, cur_vinsn.format, slot);
9572 cur_vinsn.slots[slot].ntok = 0;
9574 else
9576 bytes += fragP->tc_frag_data.text_expansion[0];
9577 gas_assert (bytes == 2 || bytes == 3);
9578 build_nop (&cur_vinsn.slots[0], bytes);
9579 fragP->fr_fix += fragP->tc_frag_data.text_expansion[0];
9581 vinsn_to_insnbuf (&cur_vinsn, fr_opcode, frag_now, TRUE);
9582 xtensa_insnbuf_to_chars
9583 (isa, cur_vinsn.insnbuf, (unsigned char *) fr_opcode, 0);
9584 fragP->fr_var = 0;
9586 else
9588 /* Here is the fun stuff: Get the immediate field from this
9589 instruction. If it fits, we're done. If not, find the next
9590 instruction sequence that fits. */
9592 IStack istack;
9593 int i;
9594 symbolS *lit_sym = NULL;
9595 int total_size = 0;
9596 int target_offset = 0;
9597 int old_size;
9598 int diff;
9599 symbolS *gen_label = NULL;
9600 offsetT frag_offset;
9601 bfd_boolean first = TRUE;
9602 bfd_boolean last_is_jump;
9604 /* It does not fit. Find something that does and
9605 convert immediately. */
9606 frag_offset = fr_opcode - fragP->fr_literal;
9607 istack_init (&istack);
9608 xg_assembly_relax (&istack, &orig_tinsn,
9609 segP, fragP, frag_offset, min_steps, 0);
9611 old_size = xtensa_format_length (isa, fmt);
9613 /* Assemble this right inline. */
9615 /* First, create the mapping from a label name to the REAL label. */
9616 target_offset = 0;
9617 for (i = 0; i < istack.ninsn; i++)
9619 TInsn *tinsn = &istack.insn[i];
9620 fragS *lit_frag;
9622 switch (tinsn->insn_type)
9624 case ITYPE_LITERAL:
9625 if (lit_sym != NULL)
9626 as_bad (_("multiple literals in expansion"));
9627 /* First find the appropriate space in the literal pool. */
9628 lit_frag = fragP->tc_frag_data.literal_frags[slot];
9629 if (lit_frag == NULL)
9630 as_bad (_("no registered fragment for literal"));
9631 if (tinsn->ntok != 1)
9632 as_bad (_("number of literal tokens != 1"));
9634 /* Set the literal symbol and add a fixup. */
9635 lit_sym = lit_frag->fr_symbol;
9636 break;
9638 case ITYPE_LABEL:
9639 if (align_targets && !is_loop)
9641 fragS *unreach = fragP->fr_next;
9642 while (!(unreach->fr_type == rs_machine_dependent
9643 && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE
9644 || unreach->fr_subtype == RELAX_UNREACHABLE)))
9646 unreach = unreach->fr_next;
9649 gas_assert (unreach->fr_type == rs_machine_dependent
9650 && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE
9651 || unreach->fr_subtype == RELAX_UNREACHABLE));
9653 target_offset += unreach->tc_frag_data.text_expansion[0];
9655 gas_assert (gen_label == NULL);
9656 gen_label = symbol_new (FAKE_LABEL_NAME, now_seg,
9657 fr_opcode - fragP->fr_literal
9658 + target_offset, fragP);
9659 break;
9661 case ITYPE_INSN:
9662 if (first && from_wide_insn)
9664 target_offset += xtensa_format_length (isa, fmt);
9665 first = FALSE;
9666 if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot))
9667 target_offset += xg_get_single_size (tinsn->opcode);
9669 else
9670 target_offset += xg_get_single_size (tinsn->opcode);
9671 break;
9675 total_size = 0;
9676 first = TRUE;
9677 last_is_jump = FALSE;
9678 for (i = 0; i < istack.ninsn; i++)
9680 TInsn *tinsn = &istack.insn[i];
9681 fragS *lit_frag;
9682 int size;
9683 segT target_seg;
9684 bfd_reloc_code_real_type reloc_type;
9686 switch (tinsn->insn_type)
9688 case ITYPE_LITERAL:
9689 lit_frag = fragP->tc_frag_data.literal_frags[slot];
9690 /* Already checked. */
9691 gas_assert (lit_frag != NULL);
9692 gas_assert (lit_sym != NULL);
9693 gas_assert (tinsn->ntok == 1);
9694 /* Add a fixup. */
9695 target_seg = S_GET_SEGMENT (lit_sym);
9696 gas_assert (target_seg);
9697 reloc_type = map_operator_to_reloc (tinsn->tok[0].X_op, TRUE);
9698 fix_new_exp_in_seg (target_seg, 0, lit_frag, 0, 4,
9699 &tinsn->tok[0], FALSE, reloc_type);
9700 break;
9702 case ITYPE_LABEL:
9703 break;
9705 case ITYPE_INSN:
9706 xg_resolve_labels (tinsn, gen_label);
9707 xg_resolve_literals (tinsn, lit_sym);
9708 if (from_wide_insn && first)
9710 first = FALSE;
9711 if (opcode_fits_format_slot (tinsn->opcode, fmt, slot))
9713 cur_vinsn.slots[slot] = *tinsn;
9715 else
9717 cur_vinsn.slots[slot].opcode =
9718 xtensa_format_slot_nop_opcode (isa, fmt, slot);
9719 cur_vinsn.slots[slot].ntok = 0;
9721 vinsn_to_insnbuf (&cur_vinsn, immed_instr, fragP, TRUE);
9722 xtensa_insnbuf_to_chars (isa, cur_vinsn.insnbuf,
9723 (unsigned char *) immed_instr, 0);
9724 fragP->tc_frag_data.is_insn = TRUE;
9725 size = xtensa_format_length (isa, fmt);
9726 if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot))
9728 xg_emit_insn_to_buf
9729 (tinsn, immed_instr + size, fragP,
9730 immed_instr - fragP->fr_literal + size, TRUE);
9731 size += xg_get_single_size (tinsn->opcode);
9734 else
9736 size = xg_get_single_size (tinsn->opcode);
9737 xg_emit_insn_to_buf (tinsn, immed_instr, fragP,
9738 immed_instr - fragP->fr_literal, TRUE);
9740 immed_instr += size;
9741 total_size += size;
9742 break;
9746 diff = total_size - old_size;
9747 gas_assert (diff >= 0);
9748 if (diff != 0)
9749 expanded = TRUE;
9750 gas_assert (diff <= fragP->fr_var);
9751 fragP->fr_var -= diff;
9752 fragP->fr_fix += diff;
9755 /* Check for undefined immediates in LOOP instructions. */
9756 if (is_loop)
9758 symbolS *sym;
9759 sym = orig_tinsn.tok[1].X_add_symbol;
9760 if (sym != NULL && !S_IS_DEFINED (sym))
9762 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
9763 return;
9765 sym = orig_tinsn.tok[1].X_op_symbol;
9766 if (sym != NULL && !S_IS_DEFINED (sym))
9768 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
9769 return;
9773 if (expanded && xtensa_opcode_is_loop (isa, orig_tinsn.opcode) == 1)
9774 convert_frag_immed_finish_loop (segP, fragP, &orig_tinsn);
9776 if (expanded && is_direct_call_opcode (orig_tinsn.opcode))
9778 /* Add an expansion note on the expanded instruction. */
9779 fix_new_exp_in_seg (now_seg, 0, fragP, fr_opcode - fragP->fr_literal, 4,
9780 &orig_tinsn.tok[0], TRUE,
9781 BFD_RELOC_XTENSA_ASM_EXPAND);
9786 /* Add a new fix expression into the desired segment. We have to
9787 switch to that segment to do this. */
9789 static fixS *
9790 fix_new_exp_in_seg (segT new_seg,
9791 subsegT new_subseg,
9792 fragS *frag,
9793 int where,
9794 int size,
9795 expressionS *exp,
9796 int pcrel,
9797 bfd_reloc_code_real_type r_type)
9799 fixS *new_fix;
9800 segT seg = now_seg;
9801 subsegT subseg = now_subseg;
9803 gas_assert (new_seg != 0);
9804 subseg_set (new_seg, new_subseg);
9806 new_fix = fix_new_exp (frag, where, size, exp, pcrel, r_type);
9807 subseg_set (seg, subseg);
9808 return new_fix;
9812 /* Relax a loop instruction so that it can span loop >256 bytes.
9814 loop as, .L1
9815 .L0:
9816 rsr as, LEND
9817 wsr as, LBEG
9818 addi as, as, lo8 (label-.L1)
9819 addmi as, as, mid8 (label-.L1)
9820 wsr as, LEND
9821 isync
9822 rsr as, LCOUNT
9823 addi as, as, 1
9824 .L1:
9825 <<body>>
9826 label:
9829 static void
9830 convert_frag_immed_finish_loop (segT segP, fragS *fragP, TInsn *tinsn)
9832 TInsn loop_insn;
9833 TInsn addi_insn;
9834 TInsn addmi_insn;
9835 unsigned long target;
9836 static xtensa_insnbuf insnbuf = NULL;
9837 unsigned int loop_length, loop_length_hi, loop_length_lo;
9838 xtensa_isa isa = xtensa_default_isa;
9839 addressT loop_offset;
9840 addressT addi_offset = 9;
9841 addressT addmi_offset = 12;
9842 fragS *next_fragP;
9843 int target_count;
9845 if (!insnbuf)
9846 insnbuf = xtensa_insnbuf_alloc (isa);
9848 /* Get the loop offset. */
9849 loop_offset = get_expanded_loop_offset (tinsn->opcode);
9851 /* Validate that there really is a LOOP at the loop_offset. Because
9852 loops are not bundleable, we can assume that the instruction will be
9853 in slot 0. */
9854 tinsn_from_chars (&loop_insn, fragP->fr_opcode + loop_offset, 0);
9855 tinsn_immed_from_frag (&loop_insn, fragP, 0);
9857 gas_assert (xtensa_opcode_is_loop (isa, loop_insn.opcode) == 1);
9858 addi_offset += loop_offset;
9859 addmi_offset += loop_offset;
9861 gas_assert (tinsn->ntok == 2);
9862 if (tinsn->tok[1].X_op == O_constant)
9863 target = tinsn->tok[1].X_add_number;
9864 else if (tinsn->tok[1].X_op == O_symbol)
9866 /* Find the fragment. */
9867 symbolS *sym = tinsn->tok[1].X_add_symbol;
9868 gas_assert (S_GET_SEGMENT (sym) == segP
9869 || S_GET_SEGMENT (sym) == absolute_section);
9870 target = (S_GET_VALUE (sym) + tinsn->tok[1].X_add_number);
9872 else
9874 as_bad (_("invalid expression evaluation type %d"), tinsn->tok[1].X_op);
9875 target = 0;
9878 loop_length = target - (fragP->fr_address + fragP->fr_fix);
9879 loop_length_hi = loop_length & ~0x0ff;
9880 loop_length_lo = loop_length & 0x0ff;
9881 if (loop_length_lo >= 128)
9883 loop_length_lo -= 256;
9884 loop_length_hi += 256;
9887 /* Because addmi sign-extends the immediate, 'loop_length_hi' can be at most
9888 32512. If the loop is larger than that, then we just fail. */
9889 if (loop_length_hi > 32512)
9890 as_bad_where (fragP->fr_file, fragP->fr_line,
9891 _("loop too long for LOOP instruction"));
9893 tinsn_from_chars (&addi_insn, fragP->fr_opcode + addi_offset, 0);
9894 gas_assert (addi_insn.opcode == xtensa_addi_opcode);
9896 tinsn_from_chars (&addmi_insn, fragP->fr_opcode + addmi_offset, 0);
9897 gas_assert (addmi_insn.opcode == xtensa_addmi_opcode);
9899 set_expr_const (&addi_insn.tok[2], loop_length_lo);
9900 tinsn_to_insnbuf (&addi_insn, insnbuf);
9902 fragP->tc_frag_data.is_insn = TRUE;
9903 xtensa_insnbuf_to_chars
9904 (isa, insnbuf, (unsigned char *) fragP->fr_opcode + addi_offset, 0);
9906 set_expr_const (&addmi_insn.tok[2], loop_length_hi);
9907 tinsn_to_insnbuf (&addmi_insn, insnbuf);
9908 xtensa_insnbuf_to_chars
9909 (isa, insnbuf, (unsigned char *) fragP->fr_opcode + addmi_offset, 0);
9911 /* Walk through all of the frags from here to the loop end
9912 and mark them as no_transform to keep them from being modified
9913 by the linker. If we ever have a relocation for the
9914 addi/addmi of the difference of two symbols we can remove this. */
9916 target_count = 0;
9917 for (next_fragP = fragP; next_fragP != NULL;
9918 next_fragP = next_fragP->fr_next)
9920 next_fragP->tc_frag_data.is_no_transform = TRUE;
9921 if (next_fragP->tc_frag_data.is_loop_target)
9922 target_count++;
9923 if (target_count == 2)
9924 break;
9929 /* A map that keeps information on a per-subsegment basis. This is
9930 maintained during initial assembly, but is invalid once the
9931 subsegments are smashed together. I.E., it cannot be used during
9932 the relaxation. */
9934 typedef struct subseg_map_struct
9936 /* the key */
9937 segT seg;
9938 subsegT subseg;
9940 /* the data */
9941 unsigned flags;
9942 float total_freq; /* fall-through + branch target frequency */
9943 float target_freq; /* branch target frequency alone */
9945 struct subseg_map_struct *next;
9946 } subseg_map;
9949 static subseg_map *sseg_map = NULL;
9951 static subseg_map *
9952 get_subseg_info (segT seg, subsegT subseg)
9954 subseg_map *subseg_e;
9956 for (subseg_e = sseg_map; subseg_e; subseg_e = subseg_e->next)
9958 if (seg == subseg_e->seg && subseg == subseg_e->subseg)
9959 break;
9961 return subseg_e;
9965 static subseg_map *
9966 add_subseg_info (segT seg, subsegT subseg)
9968 subseg_map *subseg_e = (subseg_map *) xmalloc (sizeof (subseg_map));
9969 memset (subseg_e, 0, sizeof (subseg_map));
9970 subseg_e->seg = seg;
9971 subseg_e->subseg = subseg;
9972 subseg_e->flags = 0;
9973 /* Start off considering every branch target very important. */
9974 subseg_e->target_freq = 1.0;
9975 subseg_e->total_freq = 1.0;
9976 subseg_e->next = sseg_map;
9977 sseg_map = subseg_e;
9978 return subseg_e;
9982 static unsigned
9983 get_last_insn_flags (segT seg, subsegT subseg)
9985 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9986 if (subseg_e)
9987 return subseg_e->flags;
9988 return 0;
9992 static void
9993 set_last_insn_flags (segT seg,
9994 subsegT subseg,
9995 unsigned fl,
9996 bfd_boolean val)
9998 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9999 if (! subseg_e)
10000 subseg_e = add_subseg_info (seg, subseg);
10001 if (val)
10002 subseg_e->flags |= fl;
10003 else
10004 subseg_e->flags &= ~fl;
10008 static float
10009 get_subseg_total_freq (segT seg, subsegT subseg)
10011 subseg_map *subseg_e = get_subseg_info (seg, subseg);
10012 if (subseg_e)
10013 return subseg_e->total_freq;
10014 return 1.0;
10018 static float
10019 get_subseg_target_freq (segT seg, subsegT subseg)
10021 subseg_map *subseg_e = get_subseg_info (seg, subseg);
10022 if (subseg_e)
10023 return subseg_e->target_freq;
10024 return 1.0;
10028 static void
10029 set_subseg_freq (segT seg, subsegT subseg, float total_f, float target_f)
10031 subseg_map *subseg_e = get_subseg_info (seg, subseg);
10032 if (! subseg_e)
10033 subseg_e = add_subseg_info (seg, subseg);
10034 subseg_e->total_freq = total_f;
10035 subseg_e->target_freq = target_f;
10039 /* Segment Lists and emit_state Stuff. */
10041 static void
10042 xtensa_move_seg_list_to_beginning (seg_list *head)
10044 head = head->next;
10045 while (head)
10047 segT literal_section = head->seg;
10049 /* Move the literal section to the front of the section list. */
10050 gas_assert (literal_section);
10051 if (literal_section != stdoutput->sections)
10053 bfd_section_list_remove (stdoutput, literal_section);
10054 bfd_section_list_prepend (stdoutput, literal_section);
10056 head = head->next;
10061 static void mark_literal_frags (seg_list *);
10063 static void
10064 xtensa_move_literals (void)
10066 seg_list *segment;
10067 frchainS *frchain_from, *frchain_to;
10068 fragS *search_frag, *next_frag, *last_frag, *literal_pool, *insert_after;
10069 fragS **frag_splice;
10070 emit_state state;
10071 segT dest_seg;
10072 fixS *fix, *next_fix, **fix_splice;
10073 sym_list *lit;
10075 mark_literal_frags (literal_head->next);
10077 if (use_literal_section)
10078 return;
10080 for (segment = literal_head->next; segment; segment = segment->next)
10082 /* Keep the literals for .init and .fini in separate sections. */
10083 if (!strcmp (segment_name (segment->seg), INIT_SECTION_NAME)
10084 || !strcmp (segment_name (segment->seg), FINI_SECTION_NAME))
10085 continue;
10087 frchain_from = seg_info (segment->seg)->frchainP;
10088 search_frag = frchain_from->frch_root;
10089 literal_pool = NULL;
10090 frchain_to = NULL;
10091 frag_splice = &(frchain_from->frch_root);
10093 while (!search_frag->tc_frag_data.literal_frag)
10095 gas_assert (search_frag->fr_fix == 0
10096 || search_frag->fr_type == rs_align);
10097 search_frag = search_frag->fr_next;
10100 gas_assert (search_frag->tc_frag_data.literal_frag->fr_subtype
10101 == RELAX_LITERAL_POOL_BEGIN);
10102 xtensa_switch_section_emit_state (&state, segment->seg, 0);
10104 /* Make sure that all the frags in this series are closed, and
10105 that there is at least one left over of zero-size. This
10106 prevents us from making a segment with an frchain without any
10107 frags in it. */
10108 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
10109 xtensa_set_frag_assembly_state (frag_now);
10110 last_frag = frag_now;
10111 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
10112 xtensa_set_frag_assembly_state (frag_now);
10114 while (search_frag != frag_now)
10116 next_frag = search_frag->fr_next;
10118 /* First, move the frag out of the literal section and
10119 to the appropriate place. */
10120 if (search_frag->tc_frag_data.literal_frag)
10122 literal_pool = search_frag->tc_frag_data.literal_frag;
10123 gas_assert (literal_pool->fr_subtype == RELAX_LITERAL_POOL_BEGIN);
10124 frchain_to = literal_pool->tc_frag_data.lit_frchain;
10125 gas_assert (frchain_to);
10127 insert_after = literal_pool->tc_frag_data.literal_frag;
10128 dest_seg = insert_after->fr_next->tc_frag_data.lit_seg;
10130 *frag_splice = next_frag;
10131 search_frag->fr_next = insert_after->fr_next;
10132 insert_after->fr_next = search_frag;
10133 search_frag->tc_frag_data.lit_seg = dest_seg;
10134 literal_pool->tc_frag_data.literal_frag = search_frag;
10136 /* Now move any fixups associated with this frag to the
10137 right section. */
10138 fix = frchain_from->fix_root;
10139 fix_splice = &(frchain_from->fix_root);
10140 while (fix)
10142 next_fix = fix->fx_next;
10143 if (fix->fx_frag == search_frag)
10145 *fix_splice = next_fix;
10146 fix->fx_next = frchain_to->fix_root;
10147 frchain_to->fix_root = fix;
10148 if (frchain_to->fix_tail == NULL)
10149 frchain_to->fix_tail = fix;
10151 else
10152 fix_splice = &(fix->fx_next);
10153 fix = next_fix;
10155 search_frag = next_frag;
10158 if (frchain_from->fix_root != NULL)
10160 frchain_from = seg_info (segment->seg)->frchainP;
10161 as_warn (_("fixes not all moved from %s"), segment->seg->name);
10163 gas_assert (frchain_from->fix_root == NULL);
10165 frchain_from->fix_tail = NULL;
10166 xtensa_restore_emit_state (&state);
10169 /* Now fix up the SEGMENT value for all the literal symbols. */
10170 for (lit = literal_syms; lit; lit = lit->next)
10172 symbolS *lit_sym = lit->sym;
10173 segT dseg = symbol_get_frag (lit_sym)->tc_frag_data.lit_seg;
10174 if (dseg)
10175 S_SET_SEGMENT (lit_sym, dseg);
10180 /* Walk over all the frags for segments in a list and mark them as
10181 containing literals. As clunky as this is, we can't rely on frag_var
10182 and frag_variant to get called in all situations. */
10184 static void
10185 mark_literal_frags (seg_list *segment)
10187 frchainS *frchain_from;
10188 fragS *search_frag;
10190 while (segment)
10192 frchain_from = seg_info (segment->seg)->frchainP;
10193 search_frag = frchain_from->frch_root;
10194 while (search_frag)
10196 search_frag->tc_frag_data.is_literal = TRUE;
10197 search_frag = search_frag->fr_next;
10199 segment = segment->next;
10204 static void
10205 xtensa_reorder_seg_list (seg_list *head, segT after)
10207 /* Move all of the sections in the section list to come
10208 after "after" in the gnu segment list. */
10210 head = head->next;
10211 while (head)
10213 segT literal_section = head->seg;
10215 /* Move the literal section after "after". */
10216 gas_assert (literal_section);
10217 if (literal_section != after)
10219 bfd_section_list_remove (stdoutput, literal_section);
10220 bfd_section_list_insert_after (stdoutput, after, literal_section);
10223 head = head->next;
10228 /* Push all the literal segments to the end of the gnu list. */
10230 static void
10231 xtensa_reorder_segments (void)
10233 segT sec;
10234 segT last_sec = 0;
10235 int old_count = 0;
10236 int new_count = 0;
10238 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
10240 last_sec = sec;
10241 old_count++;
10244 /* Now that we have the last section, push all the literal
10245 sections to the end. */
10246 xtensa_reorder_seg_list (literal_head, last_sec);
10248 /* Now perform the final error check. */
10249 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
10250 new_count++;
10251 gas_assert (new_count == old_count);
10255 /* Change the emit state (seg, subseg, and frag related stuff) to the
10256 correct location. Return a emit_state which can be passed to
10257 xtensa_restore_emit_state to return to current fragment. */
10259 static void
10260 xtensa_switch_to_literal_fragment (emit_state *result)
10262 if (directive_state[directive_absolute_literals])
10264 segT lit4_seg = cache_literal_section (TRUE);
10265 xtensa_switch_section_emit_state (result, lit4_seg, 0);
10267 else
10268 xtensa_switch_to_non_abs_literal_fragment (result);
10270 /* Do a 4-byte align here. */
10271 frag_align (2, 0, 0);
10272 record_alignment (now_seg, 2);
10276 static void
10277 xtensa_switch_to_non_abs_literal_fragment (emit_state *result)
10279 static bfd_boolean recursive = FALSE;
10280 fragS *pool_location = get_literal_pool_location (now_seg);
10281 segT lit_seg;
10282 bfd_boolean is_init =
10283 (now_seg && !strcmp (segment_name (now_seg), INIT_SECTION_NAME));
10284 bfd_boolean is_fini =
10285 (now_seg && !strcmp (segment_name (now_seg), FINI_SECTION_NAME));
10287 if (pool_location == NULL
10288 && !use_literal_section
10289 && !recursive
10290 && !is_init && ! is_fini)
10292 as_bad (_("literal pool location required for text-section-literals; specify with .literal_position"));
10294 /* When we mark a literal pool location, we want to put a frag in
10295 the literal pool that points to it. But to do that, we want to
10296 switch_to_literal_fragment. But literal sections don't have
10297 literal pools, so their location is always null, so we would
10298 recurse forever. This is kind of hacky, but it works. */
10300 recursive = TRUE;
10301 xtensa_mark_literal_pool_location ();
10302 recursive = FALSE;
10305 lit_seg = cache_literal_section (FALSE);
10306 xtensa_switch_section_emit_state (result, lit_seg, 0);
10308 if (!use_literal_section
10309 && !is_init && !is_fini
10310 && get_literal_pool_location (now_seg) != pool_location)
10312 /* Close whatever frag is there. */
10313 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
10314 xtensa_set_frag_assembly_state (frag_now);
10315 frag_now->tc_frag_data.literal_frag = pool_location;
10316 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
10317 xtensa_set_frag_assembly_state (frag_now);
10322 /* Call this function before emitting data into the literal section.
10323 This is a helper function for xtensa_switch_to_literal_fragment.
10324 This is similar to a .section new_now_seg subseg. */
10326 static void
10327 xtensa_switch_section_emit_state (emit_state *state,
10328 segT new_now_seg,
10329 subsegT new_now_subseg)
10331 state->name = now_seg->name;
10332 state->now_seg = now_seg;
10333 state->now_subseg = now_subseg;
10334 state->generating_literals = generating_literals;
10335 generating_literals++;
10336 subseg_set (new_now_seg, new_now_subseg);
10340 /* Use to restore the emitting into the normal place. */
10342 static void
10343 xtensa_restore_emit_state (emit_state *state)
10345 generating_literals = state->generating_literals;
10346 subseg_set (state->now_seg, state->now_subseg);
10350 /* Predicate function used to look up a section in a particular group. */
10352 static bfd_boolean
10353 match_section_group (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
10355 const char *gname = inf;
10356 const char *group_name = elf_group_name (sec);
10358 return (group_name == gname
10359 || (group_name != NULL
10360 && gname != NULL
10361 && strcmp (group_name, gname) == 0));
10365 /* Get the literal section to be used for the current text section.
10366 The result may be cached in the default_lit_sections structure. */
10368 static segT
10369 cache_literal_section (bfd_boolean use_abs_literals)
10371 const char *text_name, *group_name = 0;
10372 char *base_name, *name, *suffix;
10373 segT *pcached;
10374 segT seg, current_section;
10375 int current_subsec;
10376 bfd_boolean linkonce = FALSE;
10378 /* Save the current section/subsection. */
10379 current_section = now_seg;
10380 current_subsec = now_subseg;
10382 /* Clear the cached values if they are no longer valid. */
10383 if (now_seg != default_lit_sections.current_text_seg)
10385 default_lit_sections.current_text_seg = now_seg;
10386 default_lit_sections.lit_seg = NULL;
10387 default_lit_sections.lit4_seg = NULL;
10390 /* Check if the literal section is already cached. */
10391 if (use_abs_literals)
10392 pcached = &default_lit_sections.lit4_seg;
10393 else
10394 pcached = &default_lit_sections.lit_seg;
10396 if (*pcached)
10397 return *pcached;
10399 text_name = default_lit_sections.lit_prefix;
10400 if (! text_name || ! *text_name)
10402 text_name = segment_name (current_section);
10403 group_name = elf_group_name (current_section);
10404 linkonce = (current_section->flags & SEC_LINK_ONCE) != 0;
10407 base_name = use_abs_literals ? ".lit4" : ".literal";
10408 if (group_name)
10410 name = xmalloc (strlen (base_name) + strlen (group_name) + 2);
10411 sprintf (name, "%s.%s", base_name, group_name);
10413 else if (strncmp (text_name, ".gnu.linkonce.", linkonce_len) == 0)
10415 suffix = strchr (text_name + linkonce_len, '.');
10417 name = xmalloc (linkonce_len + strlen (base_name) + 1
10418 + (suffix ? strlen (suffix) : 0));
10419 strcpy (name, ".gnu.linkonce");
10420 strcat (name, base_name);
10421 if (suffix)
10422 strcat (name, suffix);
10423 linkonce = TRUE;
10425 else
10427 /* If the section name ends with ".text", then replace that suffix
10428 instead of appending an additional suffix. */
10429 size_t len = strlen (text_name);
10430 if (len >= 5 && strcmp (text_name + len - 5, ".text") == 0)
10431 len -= 5;
10433 name = xmalloc (len + strlen (base_name) + 1);
10434 strcpy (name, text_name);
10435 strcpy (name + len, base_name);
10438 /* Canonicalize section names to allow renaming literal sections.
10439 The group name, if any, came from the current text section and
10440 has already been canonicalized. */
10441 name = tc_canonicalize_symbol_name (name);
10443 seg = bfd_get_section_by_name_if (stdoutput, name, match_section_group,
10444 (void *) group_name);
10445 if (! seg)
10447 flagword flags;
10449 seg = subseg_force_new (name, 0);
10451 if (! use_abs_literals)
10453 /* Add the newly created literal segment to the list. */
10454 seg_list *n = (seg_list *) xmalloc (sizeof (seg_list));
10455 n->seg = seg;
10456 n->next = literal_head->next;
10457 literal_head->next = n;
10460 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_ALLOC | SEC_LOAD
10461 | (linkonce ? (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD) : 0)
10462 | (use_abs_literals ? SEC_DATA : SEC_CODE));
10464 elf_group_name (seg) = group_name;
10466 bfd_set_section_flags (stdoutput, seg, flags);
10467 bfd_set_section_alignment (stdoutput, seg, 2);
10470 *pcached = seg;
10471 subseg_set (current_section, current_subsec);
10472 return seg;
10476 /* Property Tables Stuff. */
10478 #define XTENSA_INSN_SEC_NAME ".xt.insn"
10479 #define XTENSA_LIT_SEC_NAME ".xt.lit"
10480 #define XTENSA_PROP_SEC_NAME ".xt.prop"
10482 typedef bfd_boolean (*frag_predicate) (const fragS *);
10483 typedef void (*frag_flags_fn) (const fragS *, frag_flags *);
10485 static bfd_boolean get_frag_is_literal (const fragS *);
10486 static void xtensa_create_property_segments
10487 (frag_predicate, frag_predicate, const char *, xt_section_type);
10488 static void xtensa_create_xproperty_segments
10489 (frag_flags_fn, const char *, xt_section_type);
10490 static bfd_boolean exclude_section_from_property_tables (segT);
10491 static bfd_boolean section_has_property (segT, frag_predicate);
10492 static bfd_boolean section_has_xproperty (segT, frag_flags_fn);
10493 static void add_xt_block_frags
10494 (segT, xtensa_block_info **, frag_predicate, frag_predicate);
10495 static bfd_boolean xtensa_frag_flags_is_empty (const frag_flags *);
10496 static void xtensa_frag_flags_init (frag_flags *);
10497 static void get_frag_property_flags (const fragS *, frag_flags *);
10498 static flagword frag_flags_to_number (const frag_flags *);
10499 static void add_xt_prop_frags (segT, xtensa_block_info **, frag_flags_fn);
10501 /* Set up property tables after relaxation. */
10503 void
10504 xtensa_post_relax_hook (void)
10506 xtensa_move_seg_list_to_beginning (literal_head);
10508 xtensa_find_unmarked_state_frags ();
10509 xtensa_mark_frags_for_org ();
10510 xtensa_mark_difference_of_two_symbols ();
10512 xtensa_create_property_segments (get_frag_is_literal,
10513 NULL,
10514 XTENSA_LIT_SEC_NAME,
10515 xt_literal_sec);
10516 xtensa_create_xproperty_segments (get_frag_property_flags,
10517 XTENSA_PROP_SEC_NAME,
10518 xt_prop_sec);
10520 if (warn_unaligned_branch_targets)
10521 bfd_map_over_sections (stdoutput, xtensa_find_unaligned_branch_targets, 0);
10522 bfd_map_over_sections (stdoutput, xtensa_find_unaligned_loops, 0);
10526 /* This function is only meaningful after xtensa_move_literals. */
10528 static bfd_boolean
10529 get_frag_is_literal (const fragS *fragP)
10531 gas_assert (fragP != NULL);
10532 return fragP->tc_frag_data.is_literal;
10536 static void
10537 xtensa_create_property_segments (frag_predicate property_function,
10538 frag_predicate end_property_function,
10539 const char *section_name_base,
10540 xt_section_type sec_type)
10542 segT *seclist;
10544 /* Walk over all of the current segments.
10545 Walk over each fragment
10546 For each non-empty fragment,
10547 Build a property record (append where possible). */
10549 for (seclist = &stdoutput->sections;
10550 seclist && *seclist;
10551 seclist = &(*seclist)->next)
10553 segT sec = *seclist;
10555 if (exclude_section_from_property_tables (sec))
10556 continue;
10558 if (section_has_property (sec, property_function))
10560 segment_info_type *xt_seg_info;
10561 xtensa_block_info **xt_blocks;
10562 segT prop_sec = xtensa_make_property_section (sec, section_name_base);
10564 prop_sec->output_section = prop_sec;
10565 subseg_set (prop_sec, 0);
10566 xt_seg_info = seg_info (prop_sec);
10567 xt_blocks = &xt_seg_info->tc_segment_info_data.blocks[sec_type];
10569 /* Walk over all of the frchains here and add new sections. */
10570 add_xt_block_frags (sec, xt_blocks, property_function,
10571 end_property_function);
10575 /* Now we fill them out.... */
10577 for (seclist = &stdoutput->sections;
10578 seclist && *seclist;
10579 seclist = &(*seclist)->next)
10581 segment_info_type *seginfo;
10582 xtensa_block_info *block;
10583 segT sec = *seclist;
10585 seginfo = seg_info (sec);
10586 block = seginfo->tc_segment_info_data.blocks[sec_type];
10588 if (block)
10590 xtensa_block_info *cur_block;
10591 int num_recs = 0;
10592 bfd_size_type rec_size;
10594 for (cur_block = block; cur_block; cur_block = cur_block->next)
10595 num_recs++;
10597 rec_size = num_recs * 8;
10598 bfd_set_section_size (stdoutput, sec, rec_size);
10600 if (num_recs)
10602 char *frag_data;
10603 int i;
10605 subseg_set (sec, 0);
10606 frag_data = frag_more (rec_size);
10607 cur_block = block;
10608 for (i = 0; i < num_recs; i++)
10610 fixS *fix;
10612 /* Write the fixup. */
10613 gas_assert (cur_block);
10614 fix = fix_new (frag_now, i * 8, 4,
10615 section_symbol (cur_block->sec),
10616 cur_block->offset,
10617 FALSE, BFD_RELOC_32);
10618 fix->fx_file = "<internal>";
10619 fix->fx_line = 0;
10621 /* Write the length. */
10622 md_number_to_chars (&frag_data[4 + i * 8],
10623 cur_block->size, 4);
10624 cur_block = cur_block->next;
10626 frag_wane (frag_now);
10627 frag_new (0);
10628 frag_wane (frag_now);
10635 static void
10636 xtensa_create_xproperty_segments (frag_flags_fn flag_fn,
10637 const char *section_name_base,
10638 xt_section_type sec_type)
10640 segT *seclist;
10642 /* Walk over all of the current segments.
10643 Walk over each fragment.
10644 For each fragment that has instructions,
10645 build an instruction record (append where possible). */
10647 for (seclist = &stdoutput->sections;
10648 seclist && *seclist;
10649 seclist = &(*seclist)->next)
10651 segT sec = *seclist;
10653 if (exclude_section_from_property_tables (sec))
10654 continue;
10656 if (section_has_xproperty (sec, flag_fn))
10658 segment_info_type *xt_seg_info;
10659 xtensa_block_info **xt_blocks;
10660 segT prop_sec = xtensa_make_property_section (sec, section_name_base);
10662 prop_sec->output_section = prop_sec;
10663 subseg_set (prop_sec, 0);
10664 xt_seg_info = seg_info (prop_sec);
10665 xt_blocks = &xt_seg_info->tc_segment_info_data.blocks[sec_type];
10667 /* Walk over all of the frchains here and add new sections. */
10668 add_xt_prop_frags (sec, xt_blocks, flag_fn);
10672 /* Now we fill them out.... */
10674 for (seclist = &stdoutput->sections;
10675 seclist && *seclist;
10676 seclist = &(*seclist)->next)
10678 segment_info_type *seginfo;
10679 xtensa_block_info *block;
10680 segT sec = *seclist;
10682 seginfo = seg_info (sec);
10683 block = seginfo->tc_segment_info_data.blocks[sec_type];
10685 if (block)
10687 xtensa_block_info *cur_block;
10688 int num_recs = 0;
10689 bfd_size_type rec_size;
10691 for (cur_block = block; cur_block; cur_block = cur_block->next)
10692 num_recs++;
10694 rec_size = num_recs * (8 + 4);
10695 bfd_set_section_size (stdoutput, sec, rec_size);
10696 /* elf_section_data (sec)->this_hdr.sh_entsize = 12; */
10698 if (num_recs)
10700 char *frag_data;
10701 int i;
10703 subseg_set (sec, 0);
10704 frag_data = frag_more (rec_size);
10705 cur_block = block;
10706 for (i = 0; i < num_recs; i++)
10708 fixS *fix;
10710 /* Write the fixup. */
10711 gas_assert (cur_block);
10712 fix = fix_new (frag_now, i * 12, 4,
10713 section_symbol (cur_block->sec),
10714 cur_block->offset,
10715 FALSE, BFD_RELOC_32);
10716 fix->fx_file = "<internal>";
10717 fix->fx_line = 0;
10719 /* Write the length. */
10720 md_number_to_chars (&frag_data[4 + i * 12],
10721 cur_block->size, 4);
10722 md_number_to_chars (&frag_data[8 + i * 12],
10723 frag_flags_to_number (&cur_block->flags),
10724 sizeof (flagword));
10725 cur_block = cur_block->next;
10727 frag_wane (frag_now);
10728 frag_new (0);
10729 frag_wane (frag_now);
10736 static bfd_boolean
10737 exclude_section_from_property_tables (segT sec)
10739 flagword flags = bfd_get_section_flags (stdoutput, sec);
10741 /* Sections that don't contribute to the memory footprint are excluded. */
10742 if ((flags & SEC_DEBUGGING)
10743 || !(flags & SEC_ALLOC)
10744 || (flags & SEC_MERGE))
10745 return TRUE;
10747 /* Linker cie and fde optimizations mess up property entries for
10748 eh_frame sections, but there is nothing inside them relevant to
10749 property tables anyway. */
10750 if (strcmp (sec->name, ".eh_frame") == 0)
10751 return TRUE;
10753 return FALSE;
10757 static bfd_boolean
10758 section_has_property (segT sec, frag_predicate property_function)
10760 segment_info_type *seginfo = seg_info (sec);
10761 fragS *fragP;
10763 if (seginfo && seginfo->frchainP)
10765 for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
10767 if (property_function (fragP)
10768 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
10769 return TRUE;
10772 return FALSE;
10776 static bfd_boolean
10777 section_has_xproperty (segT sec, frag_flags_fn property_function)
10779 segment_info_type *seginfo = seg_info (sec);
10780 fragS *fragP;
10782 if (seginfo && seginfo->frchainP)
10784 for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
10786 frag_flags prop_flags;
10787 property_function (fragP, &prop_flags);
10788 if (!xtensa_frag_flags_is_empty (&prop_flags))
10789 return TRUE;
10792 return FALSE;
10796 /* Two types of block sections exist right now: literal and insns. */
10798 static void
10799 add_xt_block_frags (segT sec,
10800 xtensa_block_info **xt_block,
10801 frag_predicate property_function,
10802 frag_predicate end_property_function)
10804 fragS *fragP;
10806 /* Build it if needed. */
10807 while (*xt_block != NULL)
10808 xt_block = &(*xt_block)->next;
10809 /* We are either at NULL at the beginning or at the end. */
10811 /* Walk through the frags. */
10812 if (seg_info (sec)->frchainP)
10814 for (fragP = seg_info (sec)->frchainP->frch_root;
10815 fragP;
10816 fragP = fragP->fr_next)
10818 if (property_function (fragP)
10819 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
10821 if (*xt_block != NULL)
10823 if ((*xt_block)->offset + (*xt_block)->size
10824 == fragP->fr_address)
10825 (*xt_block)->size += fragP->fr_fix;
10826 else
10827 xt_block = &((*xt_block)->next);
10829 if (*xt_block == NULL)
10831 xtensa_block_info *new_block = (xtensa_block_info *)
10832 xmalloc (sizeof (xtensa_block_info));
10833 new_block->sec = sec;
10834 new_block->offset = fragP->fr_address;
10835 new_block->size = fragP->fr_fix;
10836 new_block->next = NULL;
10837 xtensa_frag_flags_init (&new_block->flags);
10838 *xt_block = new_block;
10840 if (end_property_function
10841 && end_property_function (fragP))
10843 xt_block = &((*xt_block)->next);
10851 /* Break the encapsulation of add_xt_prop_frags here. */
10853 static bfd_boolean
10854 xtensa_frag_flags_is_empty (const frag_flags *prop_flags)
10856 if (prop_flags->is_literal
10857 || prop_flags->is_insn
10858 || prop_flags->is_data
10859 || prop_flags->is_unreachable)
10860 return FALSE;
10861 return TRUE;
10865 static void
10866 xtensa_frag_flags_init (frag_flags *prop_flags)
10868 memset (prop_flags, 0, sizeof (frag_flags));
10872 static void
10873 get_frag_property_flags (const fragS *fragP, frag_flags *prop_flags)
10875 xtensa_frag_flags_init (prop_flags);
10876 if (fragP->tc_frag_data.is_literal)
10877 prop_flags->is_literal = TRUE;
10878 if (fragP->tc_frag_data.is_specific_opcode
10879 || fragP->tc_frag_data.is_no_transform)
10881 prop_flags->is_no_transform = TRUE;
10882 if (xtensa_frag_flags_is_empty (prop_flags))
10883 prop_flags->is_data = TRUE;
10885 if (fragP->tc_frag_data.is_unreachable)
10886 prop_flags->is_unreachable = TRUE;
10887 else if (fragP->tc_frag_data.is_insn)
10889 prop_flags->is_insn = TRUE;
10890 if (fragP->tc_frag_data.is_loop_target)
10891 prop_flags->insn.is_loop_target = TRUE;
10892 if (fragP->tc_frag_data.is_branch_target)
10893 prop_flags->insn.is_branch_target = TRUE;
10894 if (fragP->tc_frag_data.is_no_density)
10895 prop_flags->insn.is_no_density = TRUE;
10896 if (fragP->tc_frag_data.use_absolute_literals)
10897 prop_flags->insn.is_abslit = TRUE;
10899 if (fragP->tc_frag_data.is_align)
10901 prop_flags->is_align = TRUE;
10902 prop_flags->alignment = fragP->tc_frag_data.alignment;
10903 if (xtensa_frag_flags_is_empty (prop_flags))
10904 prop_flags->is_data = TRUE;
10909 static flagword
10910 frag_flags_to_number (const frag_flags *prop_flags)
10912 flagword num = 0;
10913 if (prop_flags->is_literal)
10914 num |= XTENSA_PROP_LITERAL;
10915 if (prop_flags->is_insn)
10916 num |= XTENSA_PROP_INSN;
10917 if (prop_flags->is_data)
10918 num |= XTENSA_PROP_DATA;
10919 if (prop_flags->is_unreachable)
10920 num |= XTENSA_PROP_UNREACHABLE;
10921 if (prop_flags->insn.is_loop_target)
10922 num |= XTENSA_PROP_INSN_LOOP_TARGET;
10923 if (prop_flags->insn.is_branch_target)
10925 num |= XTENSA_PROP_INSN_BRANCH_TARGET;
10926 num = SET_XTENSA_PROP_BT_ALIGN (num, prop_flags->insn.bt_align_priority);
10929 if (prop_flags->insn.is_no_density)
10930 num |= XTENSA_PROP_INSN_NO_DENSITY;
10931 if (prop_flags->is_no_transform)
10932 num |= XTENSA_PROP_NO_TRANSFORM;
10933 if (prop_flags->insn.is_no_reorder)
10934 num |= XTENSA_PROP_INSN_NO_REORDER;
10935 if (prop_flags->insn.is_abslit)
10936 num |= XTENSA_PROP_INSN_ABSLIT;
10938 if (prop_flags->is_align)
10940 num |= XTENSA_PROP_ALIGN;
10941 num = SET_XTENSA_PROP_ALIGNMENT (num, prop_flags->alignment);
10944 return num;
10948 static bfd_boolean
10949 xtensa_frag_flags_combinable (const frag_flags *prop_flags_1,
10950 const frag_flags *prop_flags_2)
10952 /* Cannot combine with an end marker. */
10954 if (prop_flags_1->is_literal != prop_flags_2->is_literal)
10955 return FALSE;
10956 if (prop_flags_1->is_insn != prop_flags_2->is_insn)
10957 return FALSE;
10958 if (prop_flags_1->is_data != prop_flags_2->is_data)
10959 return FALSE;
10961 if (prop_flags_1->is_insn)
10963 /* Properties of the beginning of the frag. */
10964 if (prop_flags_2->insn.is_loop_target)
10965 return FALSE;
10966 if (prop_flags_2->insn.is_branch_target)
10967 return FALSE;
10968 if (prop_flags_1->insn.is_no_density !=
10969 prop_flags_2->insn.is_no_density)
10970 return FALSE;
10971 if (prop_flags_1->is_no_transform !=
10972 prop_flags_2->is_no_transform)
10973 return FALSE;
10974 if (prop_flags_1->insn.is_no_reorder !=
10975 prop_flags_2->insn.is_no_reorder)
10976 return FALSE;
10977 if (prop_flags_1->insn.is_abslit !=
10978 prop_flags_2->insn.is_abslit)
10979 return FALSE;
10982 if (prop_flags_1->is_align)
10983 return FALSE;
10985 return TRUE;
10989 static bfd_vma
10990 xt_block_aligned_size (const xtensa_block_info *xt_block)
10992 bfd_vma end_addr;
10993 unsigned align_bits;
10995 if (!xt_block->flags.is_align)
10996 return xt_block->size;
10998 end_addr = xt_block->offset + xt_block->size;
10999 align_bits = xt_block->flags.alignment;
11000 end_addr = ((end_addr + ((1 << align_bits) -1)) >> align_bits) << align_bits;
11001 return end_addr - xt_block->offset;
11005 static bfd_boolean
11006 xtensa_xt_block_combine (xtensa_block_info *xt_block,
11007 const xtensa_block_info *xt_block_2)
11009 if (xt_block->sec != xt_block_2->sec)
11010 return FALSE;
11011 if (xt_block->offset + xt_block_aligned_size (xt_block)
11012 != xt_block_2->offset)
11013 return FALSE;
11015 if (xt_block_2->size == 0
11016 && (!xt_block_2->flags.is_unreachable
11017 || xt_block->flags.is_unreachable))
11019 if (xt_block_2->flags.is_align
11020 && xt_block->flags.is_align)
11022 /* Nothing needed. */
11023 if (xt_block->flags.alignment >= xt_block_2->flags.alignment)
11024 return TRUE;
11026 else
11028 if (xt_block_2->flags.is_align)
11030 /* Push alignment to previous entry. */
11031 xt_block->flags.is_align = xt_block_2->flags.is_align;
11032 xt_block->flags.alignment = xt_block_2->flags.alignment;
11034 return TRUE;
11037 if (!xtensa_frag_flags_combinable (&xt_block->flags,
11038 &xt_block_2->flags))
11039 return FALSE;
11041 xt_block->size += xt_block_2->size;
11043 if (xt_block_2->flags.is_align)
11045 xt_block->flags.is_align = TRUE;
11046 xt_block->flags.alignment = xt_block_2->flags.alignment;
11049 return TRUE;
11053 static void
11054 add_xt_prop_frags (segT sec,
11055 xtensa_block_info **xt_block,
11056 frag_flags_fn property_function)
11058 fragS *fragP;
11060 /* Build it if needed. */
11061 while (*xt_block != NULL)
11063 xt_block = &(*xt_block)->next;
11065 /* We are either at NULL at the beginning or at the end. */
11067 /* Walk through the frags. */
11068 if (seg_info (sec)->frchainP)
11070 for (fragP = seg_info (sec)->frchainP->frch_root; fragP;
11071 fragP = fragP->fr_next)
11073 xtensa_block_info tmp_block;
11074 tmp_block.sec = sec;
11075 tmp_block.offset = fragP->fr_address;
11076 tmp_block.size = fragP->fr_fix;
11077 tmp_block.next = NULL;
11078 property_function (fragP, &tmp_block.flags);
11080 if (!xtensa_frag_flags_is_empty (&tmp_block.flags))
11081 /* && fragP->fr_fix != 0) */
11083 if ((*xt_block) == NULL
11084 || !xtensa_xt_block_combine (*xt_block, &tmp_block))
11086 xtensa_block_info *new_block;
11087 if ((*xt_block) != NULL)
11088 xt_block = &(*xt_block)->next;
11089 new_block = (xtensa_block_info *)
11090 xmalloc (sizeof (xtensa_block_info));
11091 *new_block = tmp_block;
11092 *xt_block = new_block;
11100 /* op_placement_info_table */
11102 /* op_placement_info makes it easier to determine which
11103 ops can go in which slots. */
11105 static void
11106 init_op_placement_info_table (void)
11108 xtensa_isa isa = xtensa_default_isa;
11109 xtensa_insnbuf ibuf = xtensa_insnbuf_alloc (isa);
11110 xtensa_opcode opcode;
11111 xtensa_format fmt;
11112 int slot;
11113 int num_opcodes = xtensa_isa_num_opcodes (isa);
11115 op_placement_table = (op_placement_info_table)
11116 xmalloc (sizeof (op_placement_info) * num_opcodes);
11117 gas_assert (xtensa_isa_num_formats (isa) < MAX_FORMATS);
11119 for (opcode = 0; opcode < num_opcodes; opcode++)
11121 op_placement_info *opi = &op_placement_table[opcode];
11122 /* FIXME: Make tinsn allocation dynamic. */
11123 if (xtensa_opcode_num_operands (isa, opcode) > MAX_INSN_ARGS)
11124 as_fatal (_("too many operands in instruction"));
11125 opi->narrowest = XTENSA_UNDEFINED;
11126 opi->narrowest_size = 0x7F;
11127 opi->narrowest_slot = 0;
11128 opi->formats = 0;
11129 opi->num_formats = 0;
11130 opi->issuef = 0;
11131 for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
11133 opi->slots[fmt] = 0;
11134 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
11136 if (xtensa_opcode_encode (isa, fmt, slot, ibuf, opcode) == 0)
11138 int fmt_length = xtensa_format_length (isa, fmt);
11139 opi->issuef++;
11140 set_bit (fmt, opi->formats);
11141 set_bit (slot, opi->slots[fmt]);
11142 if (fmt_length < opi->narrowest_size
11143 || (fmt_length == opi->narrowest_size
11144 && (xtensa_format_num_slots (isa, fmt)
11145 < xtensa_format_num_slots (isa,
11146 opi->narrowest))))
11148 opi->narrowest = fmt;
11149 opi->narrowest_size = fmt_length;
11150 opi->narrowest_slot = slot;
11154 if (opi->formats)
11155 opi->num_formats++;
11158 xtensa_insnbuf_free (isa, ibuf);
11162 bfd_boolean
11163 opcode_fits_format_slot (xtensa_opcode opcode, xtensa_format fmt, int slot)
11165 return bit_is_set (slot, op_placement_table[opcode].slots[fmt]);
11169 /* If the opcode is available in a single slot format, return its size. */
11171 static int
11172 xg_get_single_size (xtensa_opcode opcode)
11174 return op_placement_table[opcode].narrowest_size;
11178 static xtensa_format
11179 xg_get_single_format (xtensa_opcode opcode)
11181 return op_placement_table[opcode].narrowest;
11185 static int
11186 xg_get_single_slot (xtensa_opcode opcode)
11188 return op_placement_table[opcode].narrowest_slot;
11192 /* Instruction Stack Functions (from "xtensa-istack.h"). */
11194 void
11195 istack_init (IStack *stack)
11197 memset (stack, 0, sizeof (IStack));
11198 stack->ninsn = 0;
11202 bfd_boolean
11203 istack_empty (IStack *stack)
11205 return (stack->ninsn == 0);
11209 bfd_boolean
11210 istack_full (IStack *stack)
11212 return (stack->ninsn == MAX_ISTACK);
11216 /* Return a pointer to the top IStack entry.
11217 It is an error to call this if istack_empty () is TRUE. */
11219 TInsn *
11220 istack_top (IStack *stack)
11222 int rec = stack->ninsn - 1;
11223 gas_assert (!istack_empty (stack));
11224 return &stack->insn[rec];
11228 /* Add a new TInsn to an IStack.
11229 It is an error to call this if istack_full () is TRUE. */
11231 void
11232 istack_push (IStack *stack, TInsn *insn)
11234 int rec = stack->ninsn;
11235 gas_assert (!istack_full (stack));
11236 stack->insn[rec] = *insn;
11237 stack->ninsn++;
11241 /* Clear space for the next TInsn on the IStack and return a pointer
11242 to it. It is an error to call this if istack_full () is TRUE. */
11244 TInsn *
11245 istack_push_space (IStack *stack)
11247 int rec = stack->ninsn;
11248 TInsn *insn;
11249 gas_assert (!istack_full (stack));
11250 insn = &stack->insn[rec];
11251 tinsn_init (insn);
11252 stack->ninsn++;
11253 return insn;
11257 /* Remove the last pushed instruction. It is an error to call this if
11258 istack_empty () returns TRUE. */
11260 void
11261 istack_pop (IStack *stack)
11263 int rec = stack->ninsn - 1;
11264 gas_assert (!istack_empty (stack));
11265 stack->ninsn--;
11266 tinsn_init (&stack->insn[rec]);
11270 /* TInsn functions. */
11272 void
11273 tinsn_init (TInsn *dst)
11275 memset (dst, 0, sizeof (TInsn));
11279 /* Return TRUE if ANY of the operands in the insn are symbolic. */
11281 static bfd_boolean
11282 tinsn_has_symbolic_operands (const TInsn *insn)
11284 int i;
11285 int n = insn->ntok;
11287 gas_assert (insn->insn_type == ITYPE_INSN);
11289 for (i = 0; i < n; ++i)
11291 switch (insn->tok[i].X_op)
11293 case O_register:
11294 case O_constant:
11295 break;
11296 default:
11297 return TRUE;
11300 return FALSE;
11304 bfd_boolean
11305 tinsn_has_invalid_symbolic_operands (const TInsn *insn)
11307 xtensa_isa isa = xtensa_default_isa;
11308 int i;
11309 int n = insn->ntok;
11311 gas_assert (insn->insn_type == ITYPE_INSN);
11313 for (i = 0; i < n; ++i)
11315 switch (insn->tok[i].X_op)
11317 case O_register:
11318 case O_constant:
11319 break;
11320 case O_big:
11321 case O_illegal:
11322 case O_absent:
11323 /* Errors for these types are caught later. */
11324 break;
11325 case O_hi16:
11326 case O_lo16:
11327 default:
11328 /* Symbolic immediates are only allowed on the last immediate
11329 operand. At this time, CONST16 is the only opcode where we
11330 support non-PC-relative relocations. */
11331 if (i != get_relaxable_immed (insn->opcode)
11332 || (xtensa_operand_is_PCrelative (isa, insn->opcode, i) != 1
11333 && insn->opcode != xtensa_const16_opcode))
11335 as_bad (_("invalid symbolic operand"));
11336 return TRUE;
11340 return FALSE;
11344 /* For assembly code with complex expressions (e.g. subtraction),
11345 we have to build them in the literal pool so that
11346 their results are calculated correctly after relaxation.
11347 The relaxation only handles expressions that
11348 boil down to SYMBOL + OFFSET. */
11350 static bfd_boolean
11351 tinsn_has_complex_operands (const TInsn *insn)
11353 int i;
11354 int n = insn->ntok;
11355 gas_assert (insn->insn_type == ITYPE_INSN);
11356 for (i = 0; i < n; ++i)
11358 switch (insn->tok[i].X_op)
11360 case O_register:
11361 case O_constant:
11362 case O_symbol:
11363 case O_lo16:
11364 case O_hi16:
11365 break;
11366 default:
11367 return TRUE;
11370 return FALSE;
11374 /* Encode a TInsn opcode and its constant operands into slotbuf.
11375 Return TRUE if there is a symbol in the immediate field. This
11376 function assumes that:
11377 1) The number of operands are correct.
11378 2) The insn_type is ITYPE_INSN.
11379 3) The opcode can be encoded in the specified format and slot.
11380 4) Operands are either O_constant or O_symbol, and all constants fit. */
11382 static bfd_boolean
11383 tinsn_to_slotbuf (xtensa_format fmt,
11384 int slot,
11385 TInsn *tinsn,
11386 xtensa_insnbuf slotbuf)
11388 xtensa_isa isa = xtensa_default_isa;
11389 xtensa_opcode opcode = tinsn->opcode;
11390 bfd_boolean has_fixup = FALSE;
11391 int noperands = xtensa_opcode_num_operands (isa, opcode);
11392 int i;
11394 gas_assert (tinsn->insn_type == ITYPE_INSN);
11395 if (noperands != tinsn->ntok)
11396 as_fatal (_("operand number mismatch"));
11398 if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opcode))
11400 as_bad (_("cannot encode opcode \"%s\" in the given format \"%s\""),
11401 xtensa_opcode_name (isa, opcode), xtensa_format_name (isa, fmt));
11402 return FALSE;
11405 for (i = 0; i < noperands; i++)
11407 expressionS *exp = &tinsn->tok[i];
11408 int rc;
11409 unsigned line;
11410 char *file_name;
11411 uint32 opnd_value;
11413 switch (exp->X_op)
11415 case O_register:
11416 if (xtensa_operand_is_visible (isa, opcode, i) == 0)
11417 break;
11418 /* The register number has already been checked in
11419 expression_maybe_register, so we don't need to check here. */
11420 opnd_value = exp->X_add_number;
11421 (void) xtensa_operand_encode (isa, opcode, i, &opnd_value);
11422 rc = xtensa_operand_set_field (isa, opcode, i, fmt, slot, slotbuf,
11423 opnd_value);
11424 if (rc != 0)
11425 as_warn (_("xtensa-isa failure: %s"), xtensa_isa_error_msg (isa));
11426 break;
11428 case O_constant:
11429 if (xtensa_operand_is_visible (isa, opcode, i) == 0)
11430 break;
11431 as_where (&file_name, &line);
11432 /* It is a constant and we called this function
11433 then we have to try to fit it. */
11434 xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode, i,
11435 exp->X_add_number, file_name, line);
11436 break;
11438 default:
11439 has_fixup = TRUE;
11440 break;
11444 return has_fixup;
11448 /* Encode a single TInsn into an insnbuf. If the opcode can only be encoded
11449 into a multi-slot instruction, fill the other slots with NOPs.
11450 Return TRUE if there is a symbol in the immediate field. See also the
11451 assumptions listed for tinsn_to_slotbuf. */
11453 static bfd_boolean
11454 tinsn_to_insnbuf (TInsn *tinsn, xtensa_insnbuf insnbuf)
11456 static xtensa_insnbuf slotbuf = 0;
11457 static vliw_insn vinsn;
11458 xtensa_isa isa = xtensa_default_isa;
11459 bfd_boolean has_fixup = FALSE;
11460 int i;
11462 if (!slotbuf)
11464 slotbuf = xtensa_insnbuf_alloc (isa);
11465 xg_init_vinsn (&vinsn);
11468 xg_clear_vinsn (&vinsn);
11470 bundle_tinsn (tinsn, &vinsn);
11472 xtensa_format_encode (isa, vinsn.format, insnbuf);
11474 for (i = 0; i < vinsn.num_slots; i++)
11476 /* Only one slot may have a fix-up because the rest contains NOPs. */
11477 has_fixup |=
11478 tinsn_to_slotbuf (vinsn.format, i, &vinsn.slots[i], vinsn.slotbuf[i]);
11479 xtensa_format_set_slot (isa, vinsn.format, i, insnbuf, vinsn.slotbuf[i]);
11482 return has_fixup;
11486 /* Check the instruction arguments. Return TRUE on failure. */
11488 static bfd_boolean
11489 tinsn_check_arguments (const TInsn *insn)
11491 xtensa_isa isa = xtensa_default_isa;
11492 xtensa_opcode opcode = insn->opcode;
11493 xtensa_regfile t1_regfile, t2_regfile;
11494 int t1_reg, t2_reg;
11495 int t1_base_reg, t1_last_reg;
11496 int t2_base_reg, t2_last_reg;
11497 char t1_inout, t2_inout;
11498 int i, j;
11500 if (opcode == XTENSA_UNDEFINED)
11502 as_bad (_("invalid opcode"));
11503 return TRUE;
11506 if (xtensa_opcode_num_operands (isa, opcode) > insn->ntok)
11508 as_bad (_("too few operands"));
11509 return TRUE;
11512 if (xtensa_opcode_num_operands (isa, opcode) < insn->ntok)
11514 as_bad (_("too many operands"));
11515 return TRUE;
11518 /* Check registers. */
11519 for (j = 0; j < insn->ntok; j++)
11521 if (xtensa_operand_is_register (isa, insn->opcode, j) != 1)
11522 continue;
11524 t2_regfile = xtensa_operand_regfile (isa, insn->opcode, j);
11525 t2_base_reg = insn->tok[j].X_add_number;
11526 t2_last_reg
11527 = t2_base_reg + xtensa_operand_num_regs (isa, insn->opcode, j);
11529 for (i = 0; i < insn->ntok; i++)
11531 if (i == j)
11532 continue;
11534 if (xtensa_operand_is_register (isa, insn->opcode, i) != 1)
11535 continue;
11537 t1_regfile = xtensa_operand_regfile (isa, insn->opcode, i);
11539 if (t1_regfile != t2_regfile)
11540 continue;
11542 t1_inout = xtensa_operand_inout (isa, insn->opcode, i);
11543 t2_inout = xtensa_operand_inout (isa, insn->opcode, j);
11545 t1_base_reg = insn->tok[i].X_add_number;
11546 t1_last_reg = (t1_base_reg
11547 + xtensa_operand_num_regs (isa, insn->opcode, i));
11549 for (t1_reg = t1_base_reg; t1_reg < t1_last_reg; t1_reg++)
11551 for (t2_reg = t2_base_reg; t2_reg < t2_last_reg; t2_reg++)
11553 if (t1_reg != t2_reg)
11554 continue;
11556 if (t1_inout != 'i' && t2_inout != 'i')
11558 as_bad (_("multiple writes to the same register"));
11559 return TRUE;
11565 return FALSE;
11569 /* Load an instruction from its encoded form. */
11571 static void
11572 tinsn_from_chars (TInsn *tinsn, char *f, int slot)
11574 vliw_insn vinsn;
11576 xg_init_vinsn (&vinsn);
11577 vinsn_from_chars (&vinsn, f);
11579 *tinsn = vinsn.slots[slot];
11580 xg_free_vinsn (&vinsn);
11584 static void
11585 tinsn_from_insnbuf (TInsn *tinsn,
11586 xtensa_insnbuf slotbuf,
11587 xtensa_format fmt,
11588 int slot)
11590 int i;
11591 xtensa_isa isa = xtensa_default_isa;
11593 /* Find the immed. */
11594 tinsn_init (tinsn);
11595 tinsn->insn_type = ITYPE_INSN;
11596 tinsn->is_specific_opcode = FALSE; /* must not be specific */
11597 tinsn->opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
11598 tinsn->ntok = xtensa_opcode_num_operands (isa, tinsn->opcode);
11599 for (i = 0; i < tinsn->ntok; i++)
11601 set_expr_const (&tinsn->tok[i],
11602 xtensa_insnbuf_get_operand (slotbuf, fmt, slot,
11603 tinsn->opcode, i));
11608 /* Read the value of the relaxable immed from the fr_symbol and fr_offset. */
11610 static void
11611 tinsn_immed_from_frag (TInsn *tinsn, fragS *fragP, int slot)
11613 xtensa_opcode opcode = tinsn->opcode;
11614 int opnum;
11616 if (fragP->tc_frag_data.slot_symbols[slot])
11618 opnum = get_relaxable_immed (opcode);
11619 gas_assert (opnum >= 0);
11620 set_expr_symbol_offset (&tinsn->tok[opnum],
11621 fragP->tc_frag_data.slot_symbols[slot],
11622 fragP->tc_frag_data.slot_offsets[slot]);
11624 tinsn->extra_arg = fragP->tc_frag_data.free_reg[slot];
11628 static int
11629 get_num_stack_text_bytes (IStack *istack)
11631 int i;
11632 int text_bytes = 0;
11634 for (i = 0; i < istack->ninsn; i++)
11636 TInsn *tinsn = &istack->insn[i];
11637 if (tinsn->insn_type == ITYPE_INSN)
11638 text_bytes += xg_get_single_size (tinsn->opcode);
11640 return text_bytes;
11644 static int
11645 get_num_stack_literal_bytes (IStack *istack)
11647 int i;
11648 int lit_bytes = 0;
11650 for (i = 0; i < istack->ninsn; i++)
11652 TInsn *tinsn = &istack->insn[i];
11653 if (tinsn->insn_type == ITYPE_LITERAL && tinsn->ntok == 1)
11654 lit_bytes += 4;
11656 return lit_bytes;
11660 /* vliw_insn functions. */
11662 static void
11663 xg_init_vinsn (vliw_insn *v)
11665 int i;
11666 xtensa_isa isa = xtensa_default_isa;
11668 xg_clear_vinsn (v);
11670 v->insnbuf = xtensa_insnbuf_alloc (isa);
11671 if (v->insnbuf == NULL)
11672 as_fatal (_("out of memory"));
11674 for (i = 0; i < config_max_slots; i++)
11676 v->slotbuf[i] = xtensa_insnbuf_alloc (isa);
11677 if (v->slotbuf[i] == NULL)
11678 as_fatal (_("out of memory"));
11683 static void
11684 xg_clear_vinsn (vliw_insn *v)
11686 int i;
11688 memset (v, 0, offsetof (vliw_insn, slots)
11689 + sizeof(TInsn) * config_max_slots);
11691 v->format = XTENSA_UNDEFINED;
11692 v->num_slots = 0;
11693 v->inside_bundle = FALSE;
11695 if (xt_saved_debug_type != DEBUG_NONE)
11696 debug_type = xt_saved_debug_type;
11698 for (i = 0; i < config_max_slots; i++)
11699 v->slots[i].opcode = XTENSA_UNDEFINED;
11703 static void
11704 xg_copy_vinsn (vliw_insn *dst, vliw_insn *src)
11706 memcpy (dst, src,
11707 offsetof(vliw_insn, slots) + src->num_slots * sizeof(TInsn));
11708 dst->insnbuf = src->insnbuf;
11709 memcpy (dst->slotbuf, src->slotbuf, src->num_slots * sizeof(xtensa_insnbuf));
11713 static bfd_boolean
11714 vinsn_has_specific_opcodes (vliw_insn *v)
11716 int i;
11718 for (i = 0; i < v->num_slots; i++)
11720 if (v->slots[i].is_specific_opcode)
11721 return TRUE;
11723 return FALSE;
11727 static void
11728 xg_free_vinsn (vliw_insn *v)
11730 int i;
11731 xtensa_insnbuf_free (xtensa_default_isa, v->insnbuf);
11732 for (i = 0; i < config_max_slots; i++)
11733 xtensa_insnbuf_free (xtensa_default_isa, v->slotbuf[i]);
11737 /* Encode a vliw_insn into an insnbuf. Return TRUE if there are any symbolic
11738 operands. See also the assumptions listed for tinsn_to_slotbuf. */
11740 static bfd_boolean
11741 vinsn_to_insnbuf (vliw_insn *vinsn,
11742 char *frag_offset,
11743 fragS *fragP,
11744 bfd_boolean record_fixup)
11746 xtensa_isa isa = xtensa_default_isa;
11747 xtensa_format fmt = vinsn->format;
11748 xtensa_insnbuf insnbuf = vinsn->insnbuf;
11749 int slot;
11750 bfd_boolean has_fixup = FALSE;
11752 xtensa_format_encode (isa, fmt, insnbuf);
11754 for (slot = 0; slot < vinsn->num_slots; slot++)
11756 TInsn *tinsn = &vinsn->slots[slot];
11757 expressionS *extra_arg = &tinsn->extra_arg;
11758 bfd_boolean tinsn_has_fixup =
11759 tinsn_to_slotbuf (vinsn->format, slot, tinsn,
11760 vinsn->slotbuf[slot]);
11762 xtensa_format_set_slot (isa, fmt, slot,
11763 insnbuf, vinsn->slotbuf[slot]);
11764 if (extra_arg->X_op != O_illegal && extra_arg->X_op != O_register)
11766 if (vinsn->num_slots != 1)
11767 as_bad (_("TLS relocation not allowed in FLIX bundle"));
11768 else if (record_fixup)
11769 /* Instructions that generate TLS relocations should always be
11770 relaxed in the front-end. If "record_fixup" is set, then this
11771 function is being called during back-end relaxation, so flag
11772 the unexpected behavior as an error. */
11773 as_bad (_("unexpected TLS relocation"));
11774 else
11775 fix_new (fragP, frag_offset - fragP->fr_literal,
11776 xtensa_format_length (isa, fmt),
11777 extra_arg->X_add_symbol, extra_arg->X_add_number,
11778 FALSE, map_operator_to_reloc (extra_arg->X_op, FALSE));
11780 if (tinsn_has_fixup)
11782 int i;
11783 xtensa_opcode opcode = tinsn->opcode;
11784 int noperands = xtensa_opcode_num_operands (isa, opcode);
11785 has_fixup = TRUE;
11787 for (i = 0; i < noperands; i++)
11789 expressionS* exp = &tinsn->tok[i];
11790 switch (exp->X_op)
11792 case O_symbol:
11793 case O_lo16:
11794 case O_hi16:
11795 if (get_relaxable_immed (opcode) == i)
11797 /* Add a fix record for the instruction, except if this
11798 function is being called prior to relaxation, i.e.,
11799 if record_fixup is false, and the instruction might
11800 be relaxed later. */
11801 if (record_fixup
11802 || tinsn->is_specific_opcode
11803 || !xg_is_relaxable_insn (tinsn, 0))
11805 xg_add_opcode_fix (tinsn, i, fmt, slot, exp, fragP,
11806 frag_offset - fragP->fr_literal);
11808 else
11810 if (exp->X_op != O_symbol)
11811 as_bad (_("invalid operand"));
11812 tinsn->symbol = exp->X_add_symbol;
11813 tinsn->offset = exp->X_add_number;
11816 else
11817 as_bad (_("symbolic operand not allowed"));
11818 break;
11820 case O_constant:
11821 case O_register:
11822 break;
11824 default:
11825 as_bad (_("expression too complex"));
11826 break;
11832 return has_fixup;
11836 static void
11837 vinsn_from_chars (vliw_insn *vinsn, char *f)
11839 static xtensa_insnbuf insnbuf = NULL;
11840 static xtensa_insnbuf slotbuf = NULL;
11841 int i;
11842 xtensa_format fmt;
11843 xtensa_isa isa = xtensa_default_isa;
11845 if (!insnbuf)
11847 insnbuf = xtensa_insnbuf_alloc (isa);
11848 slotbuf = xtensa_insnbuf_alloc (isa);
11851 xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) f, 0);
11852 fmt = xtensa_format_decode (isa, insnbuf);
11853 if (fmt == XTENSA_UNDEFINED)
11854 as_fatal (_("cannot decode instruction format"));
11855 vinsn->format = fmt;
11856 vinsn->num_slots = xtensa_format_num_slots (isa, fmt);
11858 for (i = 0; i < vinsn->num_slots; i++)
11860 TInsn *tinsn = &vinsn->slots[i];
11861 xtensa_format_get_slot (isa, fmt, i, insnbuf, slotbuf);
11862 tinsn_from_insnbuf (tinsn, slotbuf, fmt, i);
11867 /* Expression utilities. */
11869 /* Return TRUE if the expression is an integer constant. */
11871 bfd_boolean
11872 expr_is_const (const expressionS *s)
11874 return (s->X_op == O_constant);
11878 /* Get the expression constant.
11879 Calling this is illegal if expr_is_const () returns TRUE. */
11881 offsetT
11882 get_expr_const (const expressionS *s)
11884 gas_assert (expr_is_const (s));
11885 return s->X_add_number;
11889 /* Set the expression to a constant value. */
11891 void
11892 set_expr_const (expressionS *s, offsetT val)
11894 s->X_op = O_constant;
11895 s->X_add_number = val;
11896 s->X_add_symbol = NULL;
11897 s->X_op_symbol = NULL;
11901 bfd_boolean
11902 expr_is_register (const expressionS *s)
11904 return (s->X_op == O_register);
11908 /* Get the expression constant.
11909 Calling this is illegal if expr_is_const () returns TRUE. */
11911 offsetT
11912 get_expr_register (const expressionS *s)
11914 gas_assert (expr_is_register (s));
11915 return s->X_add_number;
11919 /* Set the expression to a symbol + constant offset. */
11921 void
11922 set_expr_symbol_offset (expressionS *s, symbolS *sym, offsetT offset)
11924 s->X_op = O_symbol;
11925 s->X_add_symbol = sym;
11926 s->X_op_symbol = NULL; /* unused */
11927 s->X_add_number = offset;
11931 /* Return TRUE if the two expressions are equal. */
11933 bfd_boolean
11934 expr_is_equal (expressionS *s1, expressionS *s2)
11936 if (s1->X_op != s2->X_op)
11937 return FALSE;
11938 if (s1->X_add_symbol != s2->X_add_symbol)
11939 return FALSE;
11940 if (s1->X_op_symbol != s2->X_op_symbol)
11941 return FALSE;
11942 if (s1->X_add_number != s2->X_add_number)
11943 return FALSE;
11944 return TRUE;
11948 static void
11949 copy_expr (expressionS *dst, const expressionS *src)
11951 memcpy (dst, src, sizeof (expressionS));
11955 /* Support for the "--rename-section" option. */
11957 struct rename_section_struct
11959 char *old_name;
11960 char *new_name;
11961 struct rename_section_struct *next;
11964 static struct rename_section_struct *section_rename;
11967 /* Parse the string "oldname=new_name(:oldname2=new_name2)*" and add
11968 entries to the section_rename list. Note: Specifying multiple
11969 renamings separated by colons is not documented and is retained only
11970 for backward compatibility. */
11972 static void
11973 build_section_rename (const char *arg)
11975 struct rename_section_struct *r;
11976 char *this_arg = NULL;
11977 char *next_arg = NULL;
11979 for (this_arg = xstrdup (arg); this_arg != NULL; this_arg = next_arg)
11981 char *old_name, *new_name;
11983 if (this_arg)
11985 next_arg = strchr (this_arg, ':');
11986 if (next_arg)
11988 *next_arg = '\0';
11989 next_arg++;
11993 old_name = this_arg;
11994 new_name = strchr (this_arg, '=');
11996 if (*old_name == '\0')
11998 as_warn (_("ignoring extra '-rename-section' delimiter ':'"));
11999 continue;
12001 if (!new_name || new_name[1] == '\0')
12003 as_warn (_("ignoring invalid '-rename-section' specification: '%s'"),
12004 old_name);
12005 continue;
12007 *new_name = '\0';
12008 new_name++;
12010 /* Check for invalid section renaming. */
12011 for (r = section_rename; r != NULL; r = r->next)
12013 if (strcmp (r->old_name, old_name) == 0)
12014 as_bad (_("section %s renamed multiple times"), old_name);
12015 if (strcmp (r->new_name, new_name) == 0)
12016 as_bad (_("multiple sections remapped to output section %s"),
12017 new_name);
12020 /* Now add it. */
12021 r = (struct rename_section_struct *)
12022 xmalloc (sizeof (struct rename_section_struct));
12023 r->old_name = xstrdup (old_name);
12024 r->new_name = xstrdup (new_name);
12025 r->next = section_rename;
12026 section_rename = r;
12031 char *
12032 xtensa_section_rename (char *name)
12034 struct rename_section_struct *r = section_rename;
12036 for (r = section_rename; r != NULL; r = r->next)
12038 if (strcmp (r->old_name, name) == 0)
12039 return r->new_name;
12042 return name;