1 /* tc-xtensa.c -- Assemble Xtensa instructions.
2 Copyright 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
24 #include "safe-ctype.h"
25 #include "tc-xtensa.h"
27 #include "xtensa-relax.h"
28 #include "dwarf2dbg.h"
29 #include "xtensa-istack.h"
30 #include "struc-symbol.h"
31 #include "xtensa-config.h"
33 /* Provide default values for new configuration settings. */
39 #define uint32 unsigned int
42 #define int32 signed int
47 Naming conventions (used somewhat inconsistently):
48 The xtensa_ functions are exported
49 The xg_ functions are internal
51 We also have a couple of different extensibility mechanisms.
52 1) The idiom replacement:
53 This is used when a line is first parsed to
54 replace an instruction pattern with another instruction
55 It is currently limited to replacements of instructions
56 with constant operands.
57 2) The xtensa-relax.c mechanism that has stronger instruction
58 replacement patterns. When an instruction's immediate field
59 does not fit the next instruction sequence is attempted.
60 In addition, "narrow" opcodes are supported this way. */
63 /* Define characters with special meanings to GAS. */
64 const char comment_chars
[] = "#";
65 const char line_comment_chars
[] = "#";
66 const char line_separator_chars
[] = ";";
67 const char EXP_CHARS
[] = "eE";
68 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
71 /* Flags to indicate whether the hardware supports the density and
72 absolute literals options. */
74 bfd_boolean density_supported
= XCHAL_HAVE_DENSITY
;
75 bfd_boolean absolute_literals_supported
= XSHAL_USE_ABSOLUTE_LITERALS
;
77 /* Maximum width we would pad an unreachable frag to get alignment. */
78 #define UNREACHABLE_MAX_WIDTH 8
80 static vliw_insn cur_vinsn
;
82 unsigned xtensa_num_pipe_stages
;
83 unsigned xtensa_fetch_width
= XCHAL_INST_FETCH_WIDTH
;
85 static enum debug_info_type xt_saved_debug_type
= DEBUG_NONE
;
87 /* Some functions are only valid in the front end. This variable
88 allows us to assert that we haven't crossed over into the
90 static bfd_boolean past_xtensa_end
= FALSE
;
92 /* Flags for properties of the last instruction in a segment. */
93 #define FLAG_IS_A0_WRITER 0x1
94 #define FLAG_IS_BAD_LOOPEND 0x2
97 /* We define a special segment names ".literal" to place literals
98 into. The .fini and .init sections are special because they
99 contain code that is moved together by the linker. We give them
100 their own special .fini.literal and .init.literal sections. */
102 #define LITERAL_SECTION_NAME xtensa_section_rename (".literal")
103 #define LIT4_SECTION_NAME xtensa_section_rename (".lit4")
104 #define INIT_SECTION_NAME xtensa_section_rename (".init")
105 #define FINI_SECTION_NAME xtensa_section_rename (".fini")
108 /* This type is used for the directive_stack to keep track of the
109 state of the literal collection pools. If lit_prefix is set, it is
110 used to determine the literal section names; otherwise, the literal
111 sections are determined based on the current text section. The
112 lit_seg and lit4_seg fields cache these literal sections, with the
113 current_text_seg field used a tag to indicate whether the cached
116 typedef struct lit_state_struct
119 segT current_text_seg
;
124 static lit_state default_lit_sections
;
127 /* We keep a list of literal segments. The seg_list type is the node
128 for this list. The literal_head pointer is the head of the list,
129 with the literal_head_h dummy node at the start. */
131 typedef struct seg_list_struct
133 struct seg_list_struct
*next
;
137 static seg_list literal_head_h
;
138 static seg_list
*literal_head
= &literal_head_h
;
141 /* Lists of symbols. We keep a list of symbols that label the current
142 instruction, so that we can adjust the symbols when inserting alignment
143 for various instructions. We also keep a list of all the symbols on
144 literals, so that we can fix up those symbols when the literals are
145 later moved into the text sections. */
147 typedef struct sym_list_struct
149 struct sym_list_struct
*next
;
153 static sym_list
*insn_labels
= NULL
;
154 static sym_list
*free_insn_labels
= NULL
;
155 static sym_list
*saved_insn_labels
= NULL
;
157 static sym_list
*literal_syms
;
160 /* Flags to determine whether to prefer const16 or l32r
161 if both options are available. */
162 int prefer_const16
= 0;
165 /* Global flag to indicate when we are emitting literals. */
166 int generating_literals
= 0;
168 /* The following PROPERTY table definitions are copied from
169 <elf/xtensa.h> and must be kept in sync with the code there. */
171 /* Flags in the property tables to specify whether blocks of memory
172 are literals, instructions, data, or unreachable. For
173 instructions, blocks that begin loop targets and branch targets are
174 designated. Blocks that do not allow density, instruction
175 reordering or transformation are also specified. Finally, for
176 branch targets, branch target alignment priority is included.
177 Alignment of the next block is specified in the current block
178 and the size of the current block does not include any fill required
179 to align to the next block. */
181 #define XTENSA_PROP_LITERAL 0x00000001
182 #define XTENSA_PROP_INSN 0x00000002
183 #define XTENSA_PROP_DATA 0x00000004
184 #define XTENSA_PROP_UNREACHABLE 0x00000008
185 /* Instruction only properties at beginning of code. */
186 #define XTENSA_PROP_INSN_LOOP_TARGET 0x00000010
187 #define XTENSA_PROP_INSN_BRANCH_TARGET 0x00000020
188 /* Instruction only properties about code. */
189 #define XTENSA_PROP_INSN_NO_DENSITY 0x00000040
190 #define XTENSA_PROP_INSN_NO_REORDER 0x00000080
191 /* Historically, NO_TRANSFORM was a property of instructions,
192 but it should apply to literals under certain circumstances. */
193 #define XTENSA_PROP_NO_TRANSFORM 0x00000100
195 /* Branch target alignment information. This transmits information
196 to the linker optimization about the priority of aligning a
197 particular block for branch target alignment: None, low priority,
198 high priority, or required. These only need to be checked in
199 instruction blocks marked as XTENSA_PROP_INSN_BRANCH_TARGET.
202 switch (GET_XTENSA_PROP_BT_ALIGN (flags))
203 case XTENSA_PROP_BT_ALIGN_NONE:
204 case XTENSA_PROP_BT_ALIGN_LOW:
205 case XTENSA_PROP_BT_ALIGN_HIGH:
206 case XTENSA_PROP_BT_ALIGN_REQUIRE:
208 #define XTENSA_PROP_BT_ALIGN_MASK 0x00000600
210 /* No branch target alignment. */
211 #define XTENSA_PROP_BT_ALIGN_NONE 0x0
212 /* Low priority branch target alignment. */
213 #define XTENSA_PROP_BT_ALIGN_LOW 0x1
214 /* High priority branch target alignment. */
215 #define XTENSA_PROP_BT_ALIGN_HIGH 0x2
216 /* Required branch target alignment. */
217 #define XTENSA_PROP_BT_ALIGN_REQUIRE 0x3
219 #define GET_XTENSA_PROP_BT_ALIGN(flag) \
220 (((unsigned) ((flag) & (XTENSA_PROP_BT_ALIGN_MASK))) >> 9)
221 #define SET_XTENSA_PROP_BT_ALIGN(flag, align) \
222 (((flag) & (~XTENSA_PROP_BT_ALIGN_MASK)) | \
223 (((align) << 9) & XTENSA_PROP_BT_ALIGN_MASK))
226 /* Alignment is specified in the block BEFORE the one that needs
227 alignment. Up to 5 bits. Use GET_XTENSA_PROP_ALIGNMENT(flags) to
228 get the required alignment specified as a power of 2. Use
229 SET_XTENSA_PROP_ALIGNMENT(flags, pow2) to set the required
230 alignment. Be careful of side effects since the SET will evaluate
231 flags twice. Also, note that the SIZE of a block in the property
232 table does not include the alignment size, so the alignment fill
233 must be calculated to determine if two blocks are contiguous.
234 TEXT_ALIGN is not currently implemented but is a placeholder for a
235 possible future implementation. */
237 #define XTENSA_PROP_ALIGN 0x00000800
239 #define XTENSA_PROP_ALIGNMENT_MASK 0x0001f000
241 #define GET_XTENSA_PROP_ALIGNMENT(flag) \
242 (((unsigned) ((flag) & (XTENSA_PROP_ALIGNMENT_MASK))) >> 12)
243 #define SET_XTENSA_PROP_ALIGNMENT(flag, align) \
244 (((flag) & (~XTENSA_PROP_ALIGNMENT_MASK)) | \
245 (((align) << 12) & XTENSA_PROP_ALIGNMENT_MASK))
247 #define XTENSA_PROP_INSN_ABSLIT 0x00020000
250 /* Structure for saving instruction and alignment per-fragment data
251 that will be written to the object file. This structure is
252 equivalent to the actual data that will be written out to the file
253 but is easier to use. We provide a conversion to file flags
254 in frag_flags_to_number. */
256 typedef struct frag_flags_struct frag_flags
;
258 struct frag_flags_struct
260 /* is_literal should only be used after xtensa_move_literals.
261 If you need to check if you are generating a literal fragment,
262 then use the generating_literals global. */
264 unsigned is_literal
: 1;
265 unsigned is_insn
: 1;
266 unsigned is_data
: 1;
267 unsigned is_unreachable
: 1;
269 /* is_specific_opcode implies no_transform. */
270 unsigned is_no_transform
: 1;
274 unsigned is_loop_target
: 1;
275 unsigned is_branch_target
: 1; /* Branch targets have a priority. */
276 unsigned bt_align_priority
: 2;
278 unsigned is_no_density
: 1;
279 /* no_longcalls flag does not need to be placed in the object file. */
281 unsigned is_no_reorder
: 1;
283 /* Uses absolute literal addressing for l32r. */
284 unsigned is_abslit
: 1;
286 unsigned is_align
: 1;
287 unsigned alignment
: 5;
291 /* Structure for saving information about a block of property data
292 for frags that have the same flags. */
293 struct xtensa_block_info_struct
299 struct xtensa_block_info_struct
*next
;
303 /* Structure for saving the current state before emitting literals. */
304 typedef struct emit_state_struct
309 int generating_literals
;
313 /* Opcode placement information */
315 typedef unsigned long long bitfield
;
316 #define bit_is_set(bit, bf) ((bf) & (0x01ll << (bit)))
317 #define set_bit(bit, bf) ((bf) |= (0x01ll << (bit)))
318 #define clear_bit(bit, bf) ((bf) &= ~(0x01ll << (bit)))
320 #define MAX_FORMATS 32
322 typedef struct op_placement_info_struct
325 /* A number describing how restrictive the issue is for this
326 opcode. For example, an opcode that fits lots of different
327 formats has a high freedom, as does an opcode that fits
328 only one format but many slots in that format. The most
329 restrictive is the opcode that fits only one slot in one
332 xtensa_format narrowest
;
336 /* formats is a bitfield with the Nth bit set
337 if the opcode fits in the Nth xtensa_format. */
340 /* slots[N]'s Mth bit is set if the op fits in the
341 Mth slot of the Nth xtensa_format. */
342 bitfield slots
[MAX_FORMATS
];
344 /* A count of the number of slots in a given format
345 an op can fit (i.e., the bitcount of the slot field above). */
346 char slots_in_format
[MAX_FORMATS
];
348 } op_placement_info
, *op_placement_info_table
;
350 op_placement_info_table op_placement_table
;
353 /* Extra expression types. */
355 #define O_pltrel O_md1 /* like O_symbol but use a PLT reloc */
356 #define O_hi16 O_md2 /* use high 16 bits of symbolic value */
357 #define O_lo16 O_md3 /* use low 16 bits of symbolic value */
358 #define O_pcrel O_md4 /* value is a PC-relative offset */
360 struct suffix_reloc_map
364 bfd_reloc_code_real_type reloc
;
365 unsigned char operator;
368 #define SUFFIX_MAP(str, reloc, op) { str, sizeof (str) - 1, reloc, op }
370 static struct suffix_reloc_map suffix_relocs
[] =
372 SUFFIX_MAP ("l", BFD_RELOC_LO16
, O_lo16
),
373 SUFFIX_MAP ("h", BFD_RELOC_HI16
, O_hi16
),
374 SUFFIX_MAP ("plt", BFD_RELOC_XTENSA_PLT
, O_pltrel
),
375 SUFFIX_MAP ("pcrel", BFD_RELOC_32_PCREL
, O_pcrel
),
376 { (char *) 0, 0, BFD_RELOC_UNUSED
, 0 }
390 directive_literal_prefix
,
392 directive_absolute_literals
,
393 directive_last_directive
399 bfd_boolean can_be_negated
;
402 const directive_infoS directive_info
[] =
405 { "literal", FALSE
},
407 { "transform", TRUE
},
408 { "freeregs", FALSE
},
409 { "longcalls", TRUE
},
410 { "literal_prefix", FALSE
},
411 { "schedule", TRUE
},
412 { "absolute-literals", TRUE
}
415 bfd_boolean directive_state
[] =
419 #if !XCHAL_HAVE_DENSITY
424 TRUE
, /* transform */
425 FALSE
, /* freeregs */
426 FALSE
, /* longcalls */
427 FALSE
, /* literal_prefix */
428 FALSE
, /* schedule */
429 #if XSHAL_USE_ABSOLUTE_LITERALS
430 TRUE
/* absolute_literals */
432 FALSE
/* absolute_literals */
437 /* Directive functions. */
439 static void xtensa_begin_directive (int);
440 static void xtensa_end_directive (int);
441 static void xtensa_literal_prefix (void);
442 static void xtensa_literal_position (int);
443 static void xtensa_literal_pseudo (int);
444 static void xtensa_frequency_pseudo (int);
445 static void xtensa_elf_cons (int);
446 static void xtensa_leb128 (int);
448 /* Parsing and Idiom Translation. */
450 static bfd_reloc_code_real_type
xtensa_elf_suffix (char **, expressionS
*);
452 /* Various Other Internal Functions. */
454 extern bfd_boolean
xg_is_single_relaxable_insn (TInsn
*, TInsn
*, bfd_boolean
);
455 static bfd_boolean
xg_build_to_insn (TInsn
*, TInsn
*, BuildInstr
*);
456 static void xtensa_mark_literal_pool_location (void);
457 static addressT
get_expanded_loop_offset (xtensa_opcode
);
458 static fragS
*get_literal_pool_location (segT
);
459 static void set_literal_pool_location (segT
, fragS
*);
460 static void xtensa_set_frag_assembly_state (fragS
*);
461 static void finish_vinsn (vliw_insn
*);
462 static bfd_boolean
emit_single_op (TInsn
*);
463 static int total_frag_text_expansion (fragS
*);
465 /* Alignment Functions. */
467 static int get_text_align_power (unsigned);
468 static int get_text_align_max_fill_size (int, bfd_boolean
, bfd_boolean
);
469 static int branch_align_power (segT
);
471 /* Helpers for xtensa_relax_frag(). */
473 static long relax_frag_add_nop (fragS
*);
475 /* Accessors for additional per-subsegment information. */
477 static unsigned get_last_insn_flags (segT
, subsegT
);
478 static void set_last_insn_flags (segT
, subsegT
, unsigned, bfd_boolean
);
479 static float get_subseg_total_freq (segT
, subsegT
);
480 static float get_subseg_target_freq (segT
, subsegT
);
481 static void set_subseg_freq (segT
, subsegT
, float, float);
483 /* Segment list functions. */
485 static void xtensa_move_literals (void);
486 static void xtensa_reorder_segments (void);
487 static void xtensa_switch_to_literal_fragment (emit_state
*);
488 static void xtensa_switch_to_non_abs_literal_fragment (emit_state
*);
489 static void xtensa_switch_section_emit_state (emit_state
*, segT
, subsegT
);
490 static void xtensa_restore_emit_state (emit_state
*);
491 static segT
cache_literal_section (bfd_boolean
);
493 /* Import from elf32-xtensa.c in BFD library. */
495 extern asection
*xtensa_get_property_section (asection
*, const char *);
497 /* op_placement_info functions. */
499 static void init_op_placement_info_table (void);
500 extern bfd_boolean
opcode_fits_format_slot (xtensa_opcode
, xtensa_format
, int);
501 static int xg_get_single_size (xtensa_opcode
);
502 static xtensa_format
xg_get_single_format (xtensa_opcode
);
503 static int xg_get_single_slot (xtensa_opcode
);
505 /* TInsn and IStack functions. */
507 static bfd_boolean
tinsn_has_symbolic_operands (const TInsn
*);
508 static bfd_boolean
tinsn_has_invalid_symbolic_operands (const TInsn
*);
509 static bfd_boolean
tinsn_has_complex_operands (const TInsn
*);
510 static bfd_boolean
tinsn_to_insnbuf (TInsn
*, xtensa_insnbuf
);
511 static bfd_boolean
tinsn_check_arguments (const TInsn
*);
512 static void tinsn_from_chars (TInsn
*, char *, int);
513 static void tinsn_immed_from_frag (TInsn
*, fragS
*, int);
514 static int get_num_stack_text_bytes (IStack
*);
515 static int get_num_stack_literal_bytes (IStack
*);
517 /* vliw_insn functions. */
519 static void xg_init_vinsn (vliw_insn
*);
520 static void xg_clear_vinsn (vliw_insn
*);
521 static bfd_boolean
vinsn_has_specific_opcodes (vliw_insn
*);
522 static void xg_free_vinsn (vliw_insn
*);
523 static bfd_boolean vinsn_to_insnbuf
524 (vliw_insn
*, char *, fragS
*, bfd_boolean
);
525 static void vinsn_from_chars (vliw_insn
*, char *);
527 /* Expression Utilities. */
529 bfd_boolean
expr_is_const (const expressionS
*);
530 offsetT
get_expr_const (const expressionS
*);
531 void set_expr_const (expressionS
*, offsetT
);
532 bfd_boolean
expr_is_register (const expressionS
*);
533 offsetT
get_expr_register (const expressionS
*);
534 void set_expr_symbol_offset (expressionS
*, symbolS
*, offsetT
);
535 bfd_boolean
expr_is_equal (expressionS
*, expressionS
*);
536 static void copy_expr (expressionS
*, const expressionS
*);
538 /* Section renaming. */
540 static void build_section_rename (const char *);
543 /* ISA imported from bfd. */
544 extern xtensa_isa xtensa_default_isa
;
546 extern int target_big_endian
;
548 static xtensa_opcode xtensa_addi_opcode
;
549 static xtensa_opcode xtensa_addmi_opcode
;
550 static xtensa_opcode xtensa_call0_opcode
;
551 static xtensa_opcode xtensa_call4_opcode
;
552 static xtensa_opcode xtensa_call8_opcode
;
553 static xtensa_opcode xtensa_call12_opcode
;
554 static xtensa_opcode xtensa_callx0_opcode
;
555 static xtensa_opcode xtensa_callx4_opcode
;
556 static xtensa_opcode xtensa_callx8_opcode
;
557 static xtensa_opcode xtensa_callx12_opcode
;
558 static xtensa_opcode xtensa_const16_opcode
;
559 static xtensa_opcode xtensa_entry_opcode
;
560 static xtensa_opcode xtensa_extui_opcode
;
561 static xtensa_opcode xtensa_movi_opcode
;
562 static xtensa_opcode xtensa_movi_n_opcode
;
563 static xtensa_opcode xtensa_isync_opcode
;
564 static xtensa_opcode xtensa_jx_opcode
;
565 static xtensa_opcode xtensa_l32r_opcode
;
566 static xtensa_opcode xtensa_loop_opcode
;
567 static xtensa_opcode xtensa_loopnez_opcode
;
568 static xtensa_opcode xtensa_loopgtz_opcode
;
569 static xtensa_opcode xtensa_nop_opcode
;
570 static xtensa_opcode xtensa_nop_n_opcode
;
571 static xtensa_opcode xtensa_or_opcode
;
572 static xtensa_opcode xtensa_ret_opcode
;
573 static xtensa_opcode xtensa_ret_n_opcode
;
574 static xtensa_opcode xtensa_retw_opcode
;
575 static xtensa_opcode xtensa_retw_n_opcode
;
576 static xtensa_opcode xtensa_rsr_lcount_opcode
;
577 static xtensa_opcode xtensa_waiti_opcode
;
580 /* Command-line Options. */
582 bfd_boolean use_literal_section
= TRUE
;
583 static bfd_boolean align_targets
= TRUE
;
584 static bfd_boolean warn_unaligned_branch_targets
= FALSE
;
585 static bfd_boolean has_a0_b_retw
= FALSE
;
586 static bfd_boolean workaround_a0_b_retw
= FALSE
;
587 static bfd_boolean workaround_b_j_loop_end
= FALSE
;
588 static bfd_boolean workaround_short_loop
= FALSE
;
589 static bfd_boolean maybe_has_short_loop
= FALSE
;
590 static bfd_boolean workaround_close_loop_end
= FALSE
;
591 static bfd_boolean maybe_has_close_loop_end
= FALSE
;
592 static bfd_boolean enforce_three_byte_loop_align
= FALSE
;
594 /* When workaround_short_loops is TRUE, all loops with early exits must
595 have at least 3 instructions. workaround_all_short_loops is a modifier
596 to the workaround_short_loop flag. In addition to the
597 workaround_short_loop actions, all straightline loopgtz and loopnez
598 must have at least 3 instructions. */
600 static bfd_boolean workaround_all_short_loops
= FALSE
;
604 xtensa_setup_hw_workarounds (int earliest
, int latest
)
606 if (earliest
> latest
)
607 as_fatal (_("illegal range of target hardware versions"));
609 /* Enable all workarounds for pre-T1050.0 hardware. */
610 if (earliest
< 105000 || latest
< 105000)
612 workaround_a0_b_retw
|= TRUE
;
613 workaround_b_j_loop_end
|= TRUE
;
614 workaround_short_loop
|= TRUE
;
615 workaround_close_loop_end
|= TRUE
;
616 workaround_all_short_loops
|= TRUE
;
617 enforce_three_byte_loop_align
= TRUE
;
624 option_density
= OPTION_MD_BASE
,
631 option_no_link_relax
,
639 option_text_section_literals
,
640 option_no_text_section_literals
,
642 option_absolute_literals
,
643 option_no_absolute_literals
,
645 option_align_targets
,
646 option_no_align_targets
,
648 option_warn_unaligned_targets
,
653 option_workaround_a0_b_retw
,
654 option_no_workaround_a0_b_retw
,
656 option_workaround_b_j_loop_end
,
657 option_no_workaround_b_j_loop_end
,
659 option_workaround_short_loop
,
660 option_no_workaround_short_loop
,
662 option_workaround_all_short_loops
,
663 option_no_workaround_all_short_loops
,
665 option_workaround_close_loop_end
,
666 option_no_workaround_close_loop_end
,
668 option_no_workarounds
,
670 option_rename_section_name
,
673 option_prefer_const16
,
675 option_target_hardware
678 const char *md_shortopts
= "";
680 struct option md_longopts
[] =
682 { "density", no_argument
, NULL
, option_density
},
683 { "no-density", no_argument
, NULL
, option_no_density
},
685 /* Both "relax" and "generics" are deprecated and treated as equivalent
686 to the "transform" option. */
687 { "relax", no_argument
, NULL
, option_relax
},
688 { "no-relax", no_argument
, NULL
, option_no_relax
},
689 { "generics", no_argument
, NULL
, option_generics
},
690 { "no-generics", no_argument
, NULL
, option_no_generics
},
692 { "transform", no_argument
, NULL
, option_transform
},
693 { "no-transform", no_argument
, NULL
, option_no_transform
},
694 { "text-section-literals", no_argument
, NULL
, option_text_section_literals
},
695 { "no-text-section-literals", no_argument
, NULL
,
696 option_no_text_section_literals
},
697 { "absolute-literals", no_argument
, NULL
, option_absolute_literals
},
698 { "no-absolute-literals", no_argument
, NULL
, option_no_absolute_literals
},
699 /* This option was changed from -align-target to -target-align
700 because it conflicted with the "-al" option. */
701 { "target-align", no_argument
, NULL
, option_align_targets
},
702 { "no-target-align", no_argument
, NULL
, option_no_align_targets
},
703 { "warn-unaligned-targets", no_argument
, NULL
,
704 option_warn_unaligned_targets
},
705 { "longcalls", no_argument
, NULL
, option_longcalls
},
706 { "no-longcalls", no_argument
, NULL
, option_no_longcalls
},
708 { "no-workaround-a0-b-retw", no_argument
, NULL
,
709 option_no_workaround_a0_b_retw
},
710 { "workaround-a0-b-retw", no_argument
, NULL
, option_workaround_a0_b_retw
},
712 { "no-workaround-b-j-loop-end", no_argument
, NULL
,
713 option_no_workaround_b_j_loop_end
},
714 { "workaround-b-j-loop-end", no_argument
, NULL
,
715 option_workaround_b_j_loop_end
},
717 { "no-workaround-short-loops", no_argument
, NULL
,
718 option_no_workaround_short_loop
},
719 { "workaround-short-loops", no_argument
, NULL
,
720 option_workaround_short_loop
},
722 { "no-workaround-all-short-loops", no_argument
, NULL
,
723 option_no_workaround_all_short_loops
},
724 { "workaround-all-short-loop", no_argument
, NULL
,
725 option_workaround_all_short_loops
},
727 { "prefer-l32r", no_argument
, NULL
, option_prefer_l32r
},
728 { "prefer-const16", no_argument
, NULL
, option_prefer_const16
},
730 { "no-workarounds", no_argument
, NULL
, option_no_workarounds
},
732 { "no-workaround-close-loop-end", no_argument
, NULL
,
733 option_no_workaround_close_loop_end
},
734 { "workaround-close-loop-end", no_argument
, NULL
,
735 option_workaround_close_loop_end
},
737 { "rename-section", required_argument
, NULL
, option_rename_section_name
},
739 { "link-relax", no_argument
, NULL
, option_link_relax
},
740 { "no-link-relax", no_argument
, NULL
, option_no_link_relax
},
742 { "target-hardware", required_argument
, NULL
, option_target_hardware
},
744 { NULL
, no_argument
, NULL
, 0 }
747 size_t md_longopts_size
= sizeof md_longopts
;
751 md_parse_option (int c
, char *arg
)
756 as_warn (_("--density option is ignored"));
758 case option_no_density
:
759 as_warn (_("--no-density option is ignored"));
761 case option_link_relax
:
764 case option_no_link_relax
:
767 case option_generics
:
768 as_warn (_("--generics is deprecated; use --transform instead"));
769 return md_parse_option (option_transform
, arg
);
770 case option_no_generics
:
771 as_warn (_("--no-generics is deprecated; use --no-transform instead"));
772 return md_parse_option (option_no_transform
, arg
);
774 as_warn (_("--relax is deprecated; use --transform instead"));
775 return md_parse_option (option_transform
, arg
);
776 case option_no_relax
:
777 as_warn (_("--no-relax is deprecated; use --no-transform instead"));
778 return md_parse_option (option_no_transform
, arg
);
779 case option_longcalls
:
780 directive_state
[directive_longcalls
] = TRUE
;
782 case option_no_longcalls
:
783 directive_state
[directive_longcalls
] = FALSE
;
785 case option_text_section_literals
:
786 use_literal_section
= FALSE
;
788 case option_no_text_section_literals
:
789 use_literal_section
= TRUE
;
791 case option_absolute_literals
:
792 if (!absolute_literals_supported
)
794 as_fatal (_("--absolute-literals option not supported in this Xtensa configuration"));
797 directive_state
[directive_absolute_literals
] = TRUE
;
799 case option_no_absolute_literals
:
800 directive_state
[directive_absolute_literals
] = FALSE
;
803 case option_workaround_a0_b_retw
:
804 workaround_a0_b_retw
= TRUE
;
806 case option_no_workaround_a0_b_retw
:
807 workaround_a0_b_retw
= FALSE
;
809 case option_workaround_b_j_loop_end
:
810 workaround_b_j_loop_end
= TRUE
;
812 case option_no_workaround_b_j_loop_end
:
813 workaround_b_j_loop_end
= FALSE
;
816 case option_workaround_short_loop
:
817 workaround_short_loop
= TRUE
;
819 case option_no_workaround_short_loop
:
820 workaround_short_loop
= FALSE
;
823 case option_workaround_all_short_loops
:
824 workaround_all_short_loops
= TRUE
;
826 case option_no_workaround_all_short_loops
:
827 workaround_all_short_loops
= FALSE
;
830 case option_workaround_close_loop_end
:
831 workaround_close_loop_end
= TRUE
;
833 case option_no_workaround_close_loop_end
:
834 workaround_close_loop_end
= FALSE
;
837 case option_no_workarounds
:
838 workaround_a0_b_retw
= FALSE
;
839 workaround_b_j_loop_end
= FALSE
;
840 workaround_short_loop
= FALSE
;
841 workaround_all_short_loops
= FALSE
;
842 workaround_close_loop_end
= FALSE
;
845 case option_align_targets
:
846 align_targets
= TRUE
;
848 case option_no_align_targets
:
849 align_targets
= FALSE
;
852 case option_warn_unaligned_targets
:
853 warn_unaligned_branch_targets
= TRUE
;
856 case option_rename_section_name
:
857 build_section_rename (arg
);
861 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
862 should be emitted or not. FIXME: Not implemented. */
865 case option_prefer_l32r
:
867 as_fatal (_("prefer-l32r conflicts with prefer-const16"));
871 case option_prefer_const16
:
873 as_fatal (_("prefer-const16 conflicts with prefer-l32r"));
877 case option_target_hardware
:
879 int earliest
, latest
= 0;
880 if (*arg
== 0 || *arg
== '-')
881 as_fatal (_("invalid target hardware version"));
883 earliest
= strtol (arg
, &arg
, 0);
887 else if (*arg
== '-')
890 as_fatal (_("invalid target hardware version"));
891 latest
= strtol (arg
, &arg
, 0);
894 as_fatal (_("invalid target hardware version"));
896 xtensa_setup_hw_workarounds (earliest
, latest
);
900 case option_transform
:
901 /* This option has no affect other than to use the defaults,
902 which are already set. */
905 case option_no_transform
:
906 /* This option turns off all transformations of any kind.
907 However, because we want to preserve the state of other
908 directives, we only change its own field. Thus, before
909 you perform any transformation, always check if transform
910 is available. If you use the functions we provide for this
911 purpose, you will be ok. */
912 directive_state
[directive_transform
] = FALSE
;
922 md_show_usage (FILE *stream
)
926 --[no-]text-section-literals\n\
927 [Do not] put literals in the text section\n\
928 --[no-]absolute-literals\n\
929 [Do not] default to use non-PC-relative literals\n\
930 --[no-]target-align [Do not] try to align branch targets\n\
931 --[no-]longcalls [Do not] emit 32-bit call sequences\n\
932 --[no-]transform [Do not] transform instructions\n\
933 --rename-section old=new Rename section 'old' to 'new'\n", stream
);
937 /* Functions related to the list of current label symbols. */
940 xtensa_add_insn_label (symbolS
*sym
)
944 if (!free_insn_labels
)
945 l
= (sym_list
*) xmalloc (sizeof (sym_list
));
948 l
= free_insn_labels
;
949 free_insn_labels
= l
->next
;
953 l
->next
= insn_labels
;
959 xtensa_clear_insn_labels (void)
963 for (pl
= &free_insn_labels
; *pl
!= NULL
; pl
= &(*pl
)->next
)
971 xtensa_move_labels (fragS
*new_frag
, valueT new_offset
)
975 for (lit
= insn_labels
; lit
; lit
= lit
->next
)
977 symbolS
*lit_sym
= lit
->sym
;
978 S_SET_VALUE (lit_sym
, new_offset
);
979 symbol_set_frag (lit_sym
, new_frag
);
984 /* Directive data and functions. */
986 typedef struct state_stackS_struct
988 directiveE directive
;
990 bfd_boolean old_state
;
994 struct state_stackS_struct
*prev
;
997 state_stackS
*directive_state_stack
;
999 const pseudo_typeS md_pseudo_table
[] =
1001 { "align", s_align_bytes
, 0 }, /* Defaulting is invalid (0). */
1002 { "literal_position", xtensa_literal_position
, 0 },
1003 { "frame", s_ignore
, 0 }, /* Formerly used for STABS debugging. */
1004 { "long", xtensa_elf_cons
, 4 },
1005 { "word", xtensa_elf_cons
, 4 },
1006 { "4byte", xtensa_elf_cons
, 4 },
1007 { "short", xtensa_elf_cons
, 2 },
1008 { "2byte", xtensa_elf_cons
, 2 },
1009 { "sleb128", xtensa_leb128
, 1},
1010 { "uleb128", xtensa_leb128
, 0},
1011 { "begin", xtensa_begin_directive
, 0 },
1012 { "end", xtensa_end_directive
, 0 },
1013 { "literal", xtensa_literal_pseudo
, 0 },
1014 { "frequency", xtensa_frequency_pseudo
, 0 },
1020 use_transform (void)
1022 /* After md_end, you should be checking frag by frag, rather
1023 than state directives. */
1024 assert (!past_xtensa_end
);
1025 return directive_state
[directive_transform
];
1030 do_align_targets (void)
1032 /* Do not use this function after md_end; just look at align_targets
1033 instead. There is no target-align directive, so alignment is either
1034 enabled for all frags or not done at all. */
1035 assert (!past_xtensa_end
);
1036 return align_targets
&& use_transform ();
1041 directive_push (directiveE directive
, bfd_boolean negated
, const void *datum
)
1045 state_stackS
*stack
= (state_stackS
*) xmalloc (sizeof (state_stackS
));
1047 as_where (&file
, &line
);
1049 stack
->directive
= directive
;
1050 stack
->negated
= negated
;
1051 stack
->old_state
= directive_state
[directive
];
1054 stack
->datum
= datum
;
1055 stack
->prev
= directive_state_stack
;
1056 directive_state_stack
= stack
;
1058 directive_state
[directive
] = !negated
;
1063 directive_pop (directiveE
*directive
,
1064 bfd_boolean
*negated
,
1069 state_stackS
*top
= directive_state_stack
;
1071 if (!directive_state_stack
)
1073 as_bad (_("unmatched end directive"));
1074 *directive
= directive_none
;
1078 directive_state
[directive_state_stack
->directive
] = top
->old_state
;
1079 *directive
= top
->directive
;
1080 *negated
= top
->negated
;
1083 *datum
= top
->datum
;
1084 directive_state_stack
= top
->prev
;
1090 directive_balance (void)
1092 while (directive_state_stack
)
1094 directiveE directive
;
1095 bfd_boolean negated
;
1100 directive_pop (&directive
, &negated
, &file
, &line
, &datum
);
1101 as_warn_where ((char *) file
, line
,
1102 _(".begin directive with no matching .end directive"));
1108 inside_directive (directiveE dir
)
1110 state_stackS
*top
= directive_state_stack
;
1112 while (top
&& top
->directive
!= dir
)
1115 return (top
!= NULL
);
1120 get_directive (directiveE
*directive
, bfd_boolean
*negated
)
1124 char *directive_string
;
1126 if (strncmp (input_line_pointer
, "no-", 3) != 0)
1131 input_line_pointer
+= 3;
1134 len
= strspn (input_line_pointer
,
1135 "abcdefghijklmnopqrstuvwxyz_-/0123456789.");
1137 /* This code is a hack to make .begin [no-][generics|relax] exactly
1138 equivalent to .begin [no-]transform. We should remove it when
1139 we stop accepting those options. */
1141 if (strncmp (input_line_pointer
, "generics", strlen ("generics")) == 0)
1143 as_warn (_("[no-]generics is deprecated; use [no-]transform instead"));
1144 directive_string
= "transform";
1146 else if (strncmp (input_line_pointer
, "relax", strlen ("relax")) == 0)
1148 as_warn (_("[no-]relax is deprecated; use [no-]transform instead"));
1149 directive_string
= "transform";
1152 directive_string
= input_line_pointer
;
1154 for (i
= 0; i
< sizeof (directive_info
) / sizeof (*directive_info
); ++i
)
1156 if (strncmp (directive_string
, directive_info
[i
].name
, len
) == 0)
1158 input_line_pointer
+= len
;
1159 *directive
= (directiveE
) i
;
1160 if (*negated
&& !directive_info
[i
].can_be_negated
)
1161 as_bad (_("directive %s cannot be negated"),
1162 directive_info
[i
].name
);
1167 as_bad (_("unknown directive"));
1168 *directive
= (directiveE
) XTENSA_UNDEFINED
;
1173 xtensa_begin_directive (int ignore ATTRIBUTE_UNUSED
)
1175 directiveE directive
;
1176 bfd_boolean negated
;
1180 get_directive (&directive
, &negated
);
1181 if (directive
== (directiveE
) XTENSA_UNDEFINED
)
1183 discard_rest_of_line ();
1187 if (cur_vinsn
.inside_bundle
)
1188 as_bad (_("directives are not valid inside bundles"));
1192 case directive_literal
:
1193 if (!inside_directive (directive_literal
))
1195 /* Previous labels go with whatever follows this directive, not with
1196 the literal, so save them now. */
1197 saved_insn_labels
= insn_labels
;
1200 as_warn (_(".begin literal is deprecated; use .literal instead"));
1201 state
= (emit_state
*) xmalloc (sizeof (emit_state
));
1202 xtensa_switch_to_literal_fragment (state
);
1203 directive_push (directive_literal
, negated
, state
);
1206 case directive_literal_prefix
:
1207 /* Have to flush pending output because a movi relaxed to an l32r
1208 might produce a literal. */
1209 md_flush_pending_output ();
1210 /* Check to see if the current fragment is a literal
1211 fragment. If it is, then this operation is not allowed. */
1212 if (generating_literals
)
1214 as_bad (_("cannot set literal_prefix inside literal fragment"));
1218 /* Allocate the literal state for this section and push
1219 onto the directive stack. */
1220 ls
= xmalloc (sizeof (lit_state
));
1223 *ls
= default_lit_sections
;
1224 directive_push (directive_literal_prefix
, negated
, ls
);
1226 /* Process the new prefix. */
1227 xtensa_literal_prefix ();
1230 case directive_freeregs
:
1231 /* This information is currently unused, but we'll accept the statement
1232 and just discard the rest of the line. This won't check the syntax,
1233 but it will accept every correct freeregs directive. */
1234 input_line_pointer
+= strcspn (input_line_pointer
, "\n");
1235 directive_push (directive_freeregs
, negated
, 0);
1238 case directive_schedule
:
1239 md_flush_pending_output ();
1240 frag_var (rs_fill
, 0, 0, frag_now
->fr_subtype
,
1241 frag_now
->fr_symbol
, frag_now
->fr_offset
, NULL
);
1242 directive_push (directive_schedule
, negated
, 0);
1243 xtensa_set_frag_assembly_state (frag_now
);
1246 case directive_density
:
1247 as_warn (_(".begin [no-]density is ignored"));
1250 case directive_absolute_literals
:
1251 md_flush_pending_output ();
1252 if (!absolute_literals_supported
&& !negated
)
1254 as_warn (_("Xtensa absolute literals option not supported; ignored"));
1257 xtensa_set_frag_assembly_state (frag_now
);
1258 directive_push (directive
, negated
, 0);
1262 md_flush_pending_output ();
1263 xtensa_set_frag_assembly_state (frag_now
);
1264 directive_push (directive
, negated
, 0);
1268 demand_empty_rest_of_line ();
1273 xtensa_end_directive (int ignore ATTRIBUTE_UNUSED
)
1275 directiveE begin_directive
, end_directive
;
1276 bfd_boolean begin_negated
, end_negated
;
1280 emit_state
**state_ptr
;
1283 if (cur_vinsn
.inside_bundle
)
1284 as_bad (_("directives are not valid inside bundles"));
1286 get_directive (&end_directive
, &end_negated
);
1288 md_flush_pending_output ();
1290 switch (end_directive
)
1292 case (directiveE
) XTENSA_UNDEFINED
:
1293 discard_rest_of_line ();
1296 case directive_density
:
1297 as_warn (_(".end [no-]density is ignored"));
1298 demand_empty_rest_of_line ();
1301 case directive_absolute_literals
:
1302 if (!absolute_literals_supported
&& !end_negated
)
1304 as_warn (_("Xtensa absolute literals option not supported; ignored"));
1305 demand_empty_rest_of_line ();
1314 state_ptr
= &state
; /* use state_ptr to avoid type-punning warning */
1315 directive_pop (&begin_directive
, &begin_negated
, &file
, &line
,
1316 (const void **) state_ptr
);
1318 if (begin_directive
!= directive_none
)
1320 if (begin_directive
!= end_directive
|| begin_negated
!= end_negated
)
1322 as_bad (_("does not match begin %s%s at %s:%d"),
1323 begin_negated
? "no-" : "",
1324 directive_info
[begin_directive
].name
, file
, line
);
1328 switch (end_directive
)
1330 case directive_literal
:
1331 frag_var (rs_fill
, 0, 0, 0, NULL
, 0, NULL
);
1332 xtensa_restore_emit_state (state
);
1333 xtensa_set_frag_assembly_state (frag_now
);
1335 if (!inside_directive (directive_literal
))
1337 /* Restore the list of current labels. */
1338 xtensa_clear_insn_labels ();
1339 insn_labels
= saved_insn_labels
;
1343 case directive_literal_prefix
:
1344 /* Restore the default collection sections from saved state. */
1345 s
= (lit_state
*) state
;
1347 default_lit_sections
= *s
;
1349 /* Free the state storage. */
1350 free (s
->lit_prefix
);
1354 case directive_schedule
:
1355 case directive_freeregs
:
1359 xtensa_set_frag_assembly_state (frag_now
);
1365 demand_empty_rest_of_line ();
1369 /* Place an aligned literal fragment at the current location. */
1372 xtensa_literal_position (int ignore ATTRIBUTE_UNUSED
)
1374 md_flush_pending_output ();
1376 if (inside_directive (directive_literal
))
1377 as_warn (_(".literal_position inside literal directive; ignoring"));
1378 xtensa_mark_literal_pool_location ();
1380 demand_empty_rest_of_line ();
1381 xtensa_clear_insn_labels ();
1385 /* Support .literal label, expr, ... */
1388 xtensa_literal_pseudo (int ignored ATTRIBUTE_UNUSED
)
1391 char *p
, *base_name
;
1395 if (inside_directive (directive_literal
))
1397 as_bad (_(".literal not allowed inside .begin literal region"));
1398 ignore_rest_of_line ();
1402 md_flush_pending_output ();
1404 /* Previous labels go with whatever follows this directive, not with
1405 the literal, so save them now. */
1406 saved_insn_labels
= insn_labels
;
1409 /* If we are using text-section literals, then this is the right value... */
1412 base_name
= input_line_pointer
;
1414 xtensa_switch_to_literal_fragment (&state
);
1416 /* ...but if we aren't using text-section-literals, then we
1417 need to put them in the section we just switched to. */
1418 if (use_literal_section
|| directive_state
[directive_absolute_literals
])
1421 /* All literals are aligned to four-byte boundaries. */
1422 frag_align (2, 0, 0);
1423 record_alignment (now_seg
, 2);
1425 c
= get_symbol_end ();
1426 /* Just after name is now '\0'. */
1427 p
= input_line_pointer
;
1431 if (*input_line_pointer
!= ',' && *input_line_pointer
!= ':')
1433 as_bad (_("expected comma or colon after symbol name; "
1434 "rest of line ignored"));
1435 ignore_rest_of_line ();
1436 xtensa_restore_emit_state (&state
);
1444 input_line_pointer
++; /* skip ',' or ':' */
1446 xtensa_elf_cons (4);
1448 xtensa_restore_emit_state (&state
);
1450 /* Restore the list of current labels. */
1451 xtensa_clear_insn_labels ();
1452 insn_labels
= saved_insn_labels
;
1457 xtensa_literal_prefix (void)
1462 /* Parse the new prefix from the input_line_pointer. */
1464 len
= strspn (input_line_pointer
,
1465 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1466 "abcdefghijklmnopqrstuvwxyz_/0123456789.$");
1468 /* Get a null-terminated copy of the name. */
1469 name
= xmalloc (len
+ 1);
1471 strncpy (name
, input_line_pointer
, len
);
1474 /* Skip the name in the input line. */
1475 input_line_pointer
+= len
;
1477 default_lit_sections
.lit_prefix
= name
;
1479 /* Clear cached literal sections, since the prefix has changed. */
1480 default_lit_sections
.lit_seg
= NULL
;
1481 default_lit_sections
.lit4_seg
= NULL
;
1485 /* Support ".frequency branch_target_frequency fall_through_frequency". */
1488 xtensa_frequency_pseudo (int ignored ATTRIBUTE_UNUSED
)
1490 float fall_through_f
, target_f
;
1492 fall_through_f
= (float) strtod (input_line_pointer
, &input_line_pointer
);
1493 if (fall_through_f
< 0)
1495 as_bad (_("fall through frequency must be greater than 0"));
1496 ignore_rest_of_line ();
1500 target_f
= (float) strtod (input_line_pointer
, &input_line_pointer
);
1503 as_bad (_("branch target frequency must be greater than 0"));
1504 ignore_rest_of_line ();
1508 set_subseg_freq (now_seg
, now_subseg
, target_f
+ fall_through_f
, target_f
);
1510 demand_empty_rest_of_line ();
1514 /* Like normal .long/.short/.word, except support @plt, etc.
1515 Clobbers input_line_pointer, checks end-of-line. */
1518 xtensa_elf_cons (int nbytes
)
1521 bfd_reloc_code_real_type reloc
;
1523 md_flush_pending_output ();
1525 if (cur_vinsn
.inside_bundle
)
1526 as_bad (_("directives are not valid inside bundles"));
1528 if (is_it_end_of_statement ())
1530 demand_empty_rest_of_line ();
1537 if (exp
.X_op
== O_symbol
1538 && *input_line_pointer
== '@'
1539 && ((reloc
= xtensa_elf_suffix (&input_line_pointer
, &exp
))
1542 reloc_howto_type
*reloc_howto
=
1543 bfd_reloc_type_lookup (stdoutput
, reloc
);
1545 if (reloc
== BFD_RELOC_UNUSED
|| !reloc_howto
)
1546 as_bad (_("unsupported relocation"));
1547 else if ((reloc
>= BFD_RELOC_XTENSA_SLOT0_OP
1548 && reloc
<= BFD_RELOC_XTENSA_SLOT14_OP
)
1549 || (reloc
>= BFD_RELOC_XTENSA_SLOT0_ALT
1550 && reloc
<= BFD_RELOC_XTENSA_SLOT14_ALT
))
1551 as_bad (_("opcode-specific %s relocation used outside "
1552 "an instruction"), reloc_howto
->name
);
1553 else if (nbytes
!= (int) bfd_get_reloc_size (reloc_howto
))
1554 as_bad (_("%s relocations do not fit in %d bytes"),
1555 reloc_howto
->name
, nbytes
);
1558 char *p
= frag_more ((int) nbytes
);
1559 xtensa_set_frag_assembly_state (frag_now
);
1560 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
,
1561 nbytes
, &exp
, reloc_howto
->pc_relative
, reloc
);
1566 xtensa_set_frag_assembly_state (frag_now
);
1567 emit_expr (&exp
, (unsigned int) nbytes
);
1570 while (*input_line_pointer
++ == ',');
1572 input_line_pointer
--; /* Put terminator back into stream. */
1573 demand_empty_rest_of_line ();
1576 static bfd_boolean is_leb128_expr
;
1579 xtensa_leb128 (int sign
)
1581 is_leb128_expr
= TRUE
;
1583 is_leb128_expr
= FALSE
;
1587 /* Parsing and Idiom Translation. */
1589 /* Parse @plt, etc. and return the desired relocation. */
1590 static bfd_reloc_code_real_type
1591 xtensa_elf_suffix (char **str_p
, expressionS
*exp_p
)
1598 struct suffix_reloc_map
*ptr
;
1601 return BFD_RELOC_NONE
;
1603 for (ch
= *str
, str2
= ident
;
1604 (str2
< ident
+ sizeof (ident
) - 1
1605 && (ISALNUM (ch
) || ch
== '@'));
1608 *str2
++ = (ISLOWER (ch
)) ? ch
: TOLOWER (ch
);
1615 for (ptr
= &suffix_relocs
[0]; ptr
->length
> 0; ptr
++)
1616 if (ch
== ptr
->suffix
[0]
1617 && len
== ptr
->length
1618 && memcmp (ident
, ptr
->suffix
, ptr
->length
) == 0)
1620 /* Now check for "identifier@suffix+constant". */
1621 if (*str
== '-' || *str
== '+')
1623 char *orig_line
= input_line_pointer
;
1624 expressionS new_exp
;
1626 input_line_pointer
= str
;
1627 expression (&new_exp
);
1628 if (new_exp
.X_op
== O_constant
)
1630 exp_p
->X_add_number
+= new_exp
.X_add_number
;
1631 str
= input_line_pointer
;
1634 if (&input_line_pointer
!= str_p
)
1635 input_line_pointer
= orig_line
;
1642 return BFD_RELOC_UNUSED
;
1646 /* Find the matching operator type. */
1647 static unsigned char
1648 map_suffix_reloc_to_operator (bfd_reloc_code_real_type reloc
)
1650 struct suffix_reloc_map
*sfx
;
1651 unsigned char operator = (unsigned char) -1;
1653 for (sfx
= &suffix_relocs
[0]; sfx
->suffix
; sfx
++)
1655 if (sfx
->reloc
== reloc
)
1657 operator = sfx
->operator;
1661 assert (operator != (unsigned char) -1);
1666 /* Find the matching reloc type. */
1667 static bfd_reloc_code_real_type
1668 map_operator_to_reloc (unsigned char operator)
1670 struct suffix_reloc_map
*sfx
;
1671 bfd_reloc_code_real_type reloc
= BFD_RELOC_UNUSED
;
1673 for (sfx
= &suffix_relocs
[0]; sfx
->suffix
; sfx
++)
1675 if (sfx
->operator == operator)
1682 if (reloc
== BFD_RELOC_UNUSED
)
1683 return BFD_RELOC_32
;
1690 expression_end (const char *name
)
1713 #define ERROR_REG_NUM ((unsigned) -1)
1716 tc_get_register (const char *prefix
)
1719 const char *next_expr
;
1720 const char *old_line_pointer
;
1723 old_line_pointer
= input_line_pointer
;
1725 if (*input_line_pointer
== '$')
1726 ++input_line_pointer
;
1728 /* Accept "sp" as a synonym for "a1". */
1729 if (input_line_pointer
[0] == 's' && input_line_pointer
[1] == 'p'
1730 && expression_end (input_line_pointer
+ 2))
1732 input_line_pointer
+= 2;
1733 return 1; /* AR[1] */
1736 while (*input_line_pointer
++ == *prefix
++)
1738 --input_line_pointer
;
1743 as_bad (_("bad register name: %s"), old_line_pointer
);
1744 return ERROR_REG_NUM
;
1747 if (!ISDIGIT ((unsigned char) *input_line_pointer
))
1749 as_bad (_("bad register number: %s"), input_line_pointer
);
1750 return ERROR_REG_NUM
;
1755 while (ISDIGIT ((int) *input_line_pointer
))
1756 reg
= reg
* 10 + *input_line_pointer
++ - '0';
1758 if (!(next_expr
= expression_end (input_line_pointer
)))
1760 as_bad (_("bad register name: %s"), old_line_pointer
);
1761 return ERROR_REG_NUM
;
1764 input_line_pointer
= (char *) next_expr
;
1771 expression_maybe_register (xtensa_opcode opc
, int opnd
, expressionS
*tok
)
1773 xtensa_isa isa
= xtensa_default_isa
;
1775 /* Check if this is an immediate operand. */
1776 if (xtensa_operand_is_register (isa
, opc
, opnd
) == 0)
1778 bfd_reloc_code_real_type reloc
;
1779 segT t
= expression (tok
);
1780 if (t
== absolute_section
1781 && xtensa_operand_is_PCrelative (isa
, opc
, opnd
) == 1)
1783 assert (tok
->X_op
== O_constant
);
1784 tok
->X_op
= O_symbol
;
1785 tok
->X_add_symbol
= &abs_symbol
;
1788 if ((tok
->X_op
== O_constant
|| tok
->X_op
== O_symbol
)
1789 && ((reloc
= xtensa_elf_suffix (&input_line_pointer
, tok
))
1794 case BFD_RELOC_LO16
:
1795 if (tok
->X_op
== O_constant
)
1797 tok
->X_add_number
&= 0xffff;
1801 case BFD_RELOC_HI16
:
1802 if (tok
->X_op
== O_constant
)
1804 tok
->X_add_number
= ((unsigned) tok
->X_add_number
) >> 16;
1808 case BFD_RELOC_UNUSED
:
1809 as_bad (_("unsupported relocation"));
1811 case BFD_RELOC_32_PCREL
:
1812 as_bad (_("pcrel relocation not allowed in an instruction"));
1817 tok
->X_op
= map_suffix_reloc_to_operator (reloc
);
1822 xtensa_regfile opnd_rf
= xtensa_operand_regfile (isa
, opc
, opnd
);
1823 unsigned reg
= tc_get_register (xtensa_regfile_shortname (isa
, opnd_rf
));
1825 if (reg
!= ERROR_REG_NUM
) /* Already errored */
1828 if (xtensa_operand_encode (isa
, opc
, opnd
, &buf
))
1829 as_bad (_("register number out of range"));
1832 tok
->X_op
= O_register
;
1833 tok
->X_add_symbol
= 0;
1834 tok
->X_add_number
= reg
;
1839 /* Split up the arguments for an opcode or pseudo-op. */
1842 tokenize_arguments (char **args
, char *str
)
1844 char *old_input_line_pointer
;
1845 bfd_boolean saw_comma
= FALSE
;
1846 bfd_boolean saw_arg
= FALSE
;
1847 bfd_boolean saw_colon
= FALSE
;
1849 char *arg_end
, *arg
;
1852 /* Save and restore input_line_pointer around this function. */
1853 old_input_line_pointer
= input_line_pointer
;
1854 input_line_pointer
= str
;
1856 while (*input_line_pointer
)
1859 switch (*input_line_pointer
)
1866 input_line_pointer
++;
1867 if (saw_comma
|| saw_colon
|| !saw_arg
)
1873 input_line_pointer
++;
1874 if (saw_comma
|| saw_colon
|| !saw_arg
)
1880 if (!saw_comma
&& !saw_colon
&& saw_arg
)
1883 arg_end
= input_line_pointer
+ 1;
1884 while (!expression_end (arg_end
))
1887 arg_len
= arg_end
- input_line_pointer
;
1888 arg
= (char *) xmalloc ((saw_colon
? 1 : 0) + arg_len
+ 1);
1889 args
[num_args
] = arg
;
1893 strncpy (arg
, input_line_pointer
, arg_len
);
1894 arg
[arg_len
] = '\0';
1896 input_line_pointer
= arg_end
;
1906 if (saw_comma
|| saw_colon
)
1908 input_line_pointer
= old_input_line_pointer
;
1913 as_bad (_("extra comma"));
1915 as_bad (_("extra colon"));
1917 as_bad (_("missing argument"));
1919 as_bad (_("missing comma or colon"));
1920 input_line_pointer
= old_input_line_pointer
;
1925 /* Parse the arguments to an opcode. Return TRUE on error. */
1928 parse_arguments (TInsn
*insn
, int num_args
, char **arg_strings
)
1930 expressionS
*tok
, *last_tok
;
1931 xtensa_opcode opcode
= insn
->opcode
;
1932 bfd_boolean had_error
= TRUE
;
1933 xtensa_isa isa
= xtensa_default_isa
;
1934 int n
, num_regs
= 0;
1935 int opcode_operand_count
;
1936 int opnd_cnt
, last_opnd_cnt
;
1937 unsigned int next_reg
= 0;
1938 char *old_input_line_pointer
;
1940 if (insn
->insn_type
== ITYPE_LITERAL
)
1941 opcode_operand_count
= 1;
1943 opcode_operand_count
= xtensa_opcode_num_operands (isa
, opcode
);
1946 memset (tok
, 0, sizeof (*tok
) * MAX_INSN_ARGS
);
1948 /* Save and restore input_line_pointer around this function. */
1949 old_input_line_pointer
= input_line_pointer
;
1955 /* Skip invisible operands. */
1956 while (xtensa_operand_is_visible (isa
, opcode
, opnd_cnt
) == 0)
1962 for (n
= 0; n
< num_args
; n
++)
1964 input_line_pointer
= arg_strings
[n
];
1965 if (*input_line_pointer
== ':')
1967 xtensa_regfile opnd_rf
;
1968 input_line_pointer
++;
1971 assert (opnd_cnt
> 0);
1973 opnd_rf
= xtensa_operand_regfile (isa
, opcode
, last_opnd_cnt
);
1975 != tc_get_register (xtensa_regfile_shortname (isa
, opnd_rf
)))
1976 as_warn (_("incorrect register number, ignoring"));
1981 if (opnd_cnt
>= opcode_operand_count
)
1983 as_warn (_("too many arguments"));
1986 assert (opnd_cnt
< MAX_INSN_ARGS
);
1988 expression_maybe_register (opcode
, opnd_cnt
, tok
);
1989 next_reg
= tok
->X_add_number
+ 1;
1991 if (tok
->X_op
== O_illegal
|| tok
->X_op
== O_absent
)
1993 if (xtensa_operand_is_register (isa
, opcode
, opnd_cnt
) == 1)
1995 num_regs
= xtensa_operand_num_regs (isa
, opcode
, opnd_cnt
) - 1;
1996 /* minus 1 because we are seeing one right now */
2002 last_opnd_cnt
= opnd_cnt
;
2009 while (xtensa_operand_is_visible (isa
, opcode
, opnd_cnt
) == 0);
2013 if (num_regs
> 0 && ((int) next_reg
!= last_tok
->X_add_number
+ 1))
2016 insn
->ntok
= tok
- insn
->tok
;
2020 input_line_pointer
= old_input_line_pointer
;
2026 get_invisible_operands (TInsn
*insn
)
2028 xtensa_isa isa
= xtensa_default_isa
;
2029 static xtensa_insnbuf slotbuf
= NULL
;
2031 xtensa_opcode opc
= insn
->opcode
;
2032 int slot
, opnd
, fmt_found
;
2036 slotbuf
= xtensa_insnbuf_alloc (isa
);
2038 /* Find format/slot where this can be encoded. */
2041 for (fmt
= 0; fmt
< xtensa_isa_num_formats (isa
); fmt
++)
2043 for (slot
= 0; slot
< xtensa_format_num_slots (isa
, fmt
); slot
++)
2045 if (xtensa_opcode_encode (isa
, fmt
, slot
, slotbuf
, opc
) == 0)
2051 if (fmt_found
) break;
2056 as_bad (_("cannot encode opcode \"%s\""), xtensa_opcode_name (isa
, opc
));
2060 /* First encode all the visible operands
2061 (to deal with shared field operands). */
2062 for (opnd
= 0; opnd
< insn
->ntok
; opnd
++)
2064 if (xtensa_operand_is_visible (isa
, opc
, opnd
) == 1
2065 && (insn
->tok
[opnd
].X_op
== O_register
2066 || insn
->tok
[opnd
].X_op
== O_constant
))
2068 val
= insn
->tok
[opnd
].X_add_number
;
2069 xtensa_operand_encode (isa
, opc
, opnd
, &val
);
2070 xtensa_operand_set_field (isa
, opc
, opnd
, fmt
, slot
, slotbuf
, val
);
2074 /* Then pull out the values for the invisible ones. */
2075 for (opnd
= 0; opnd
< insn
->ntok
; opnd
++)
2077 if (xtensa_operand_is_visible (isa
, opc
, opnd
) == 0)
2079 xtensa_operand_get_field (isa
, opc
, opnd
, fmt
, slot
, slotbuf
, &val
);
2080 xtensa_operand_decode (isa
, opc
, opnd
, &val
);
2081 insn
->tok
[opnd
].X_add_number
= val
;
2082 if (xtensa_operand_is_register (isa
, opc
, opnd
) == 1)
2083 insn
->tok
[opnd
].X_op
= O_register
;
2085 insn
->tok
[opnd
].X_op
= O_constant
;
2094 xg_reverse_shift_count (char **cnt_argp
)
2096 char *cnt_arg
, *new_arg
;
2097 cnt_arg
= *cnt_argp
;
2099 /* replace the argument with "31-(argument)" */
2100 new_arg
= (char *) xmalloc (strlen (cnt_arg
) + 6);
2101 sprintf (new_arg
, "31-(%s)", cnt_arg
);
2104 *cnt_argp
= new_arg
;
2108 /* If "arg" is a constant expression, return non-zero with the value
2112 xg_arg_is_constant (char *arg
, offsetT
*valp
)
2115 char *save_ptr
= input_line_pointer
;
2117 input_line_pointer
= arg
;
2119 input_line_pointer
= save_ptr
;
2121 if (exp
.X_op
== O_constant
)
2123 *valp
= exp
.X_add_number
;
2132 xg_replace_opname (char **popname
, char *newop
)
2135 *popname
= (char *) xmalloc (strlen (newop
) + 1);
2136 strcpy (*popname
, newop
);
2141 xg_check_num_args (int *pnum_args
,
2146 int num_args
= *pnum_args
;
2148 if (num_args
< expected_num
)
2150 as_bad (_("not enough operands (%d) for '%s'; expected %d"),
2151 num_args
, opname
, expected_num
);
2155 if (num_args
> expected_num
)
2157 as_warn (_("too many operands (%d) for '%s'; expected %d"),
2158 num_args
, opname
, expected_num
);
2159 while (num_args
-- > expected_num
)
2161 free (arg_strings
[num_args
]);
2162 arg_strings
[num_args
] = 0;
2164 *pnum_args
= expected_num
;
2172 /* If the register is not specified as part of the opcode,
2173 then get it from the operand and move it to the opcode. */
2176 xg_translate_sysreg_op (char **popname
, int *pnum_args
, char **arg_strings
)
2178 xtensa_isa isa
= xtensa_default_isa
;
2180 char *opname
, *new_opname
;
2181 const char *sr_name
;
2182 int is_user
, is_write
;
2187 is_user
= (opname
[1] == 'u');
2188 is_write
= (opname
[0] == 'w');
2190 /* Opname == [rw]ur or [rwx]sr... */
2192 if (xg_check_num_args (pnum_args
, 2, opname
, arg_strings
))
2195 /* Check if the argument is a symbolic register name. */
2196 sr
= xtensa_sysreg_lookup_name (isa
, arg_strings
[1]);
2197 /* Handle WSR to "INTSET" as a special case. */
2198 if (sr
== XTENSA_UNDEFINED
&& is_write
&& !is_user
2199 && !strcasecmp (arg_strings
[1], "intset"))
2200 sr
= xtensa_sysreg_lookup_name (isa
, "interrupt");
2201 if (sr
== XTENSA_UNDEFINED
2202 || (xtensa_sysreg_is_user (isa
, sr
) == 1) != is_user
)
2204 /* Maybe it's a register number.... */
2206 if (!xg_arg_is_constant (arg_strings
[1], &val
))
2208 as_bad (_("invalid register '%s' for '%s' instruction"),
2209 arg_strings
[1], opname
);
2212 sr
= xtensa_sysreg_lookup (isa
, val
, is_user
);
2213 if (sr
== XTENSA_UNDEFINED
)
2215 as_bad (_("invalid register number (%ld) for '%s' instruction"),
2216 (long) val
, opname
);
2221 /* Remove the last argument, which is now part of the opcode. */
2222 free (arg_strings
[1]);
2226 /* Translate the opcode. */
2227 sr_name
= xtensa_sysreg_name (isa
, sr
);
2228 /* Another special case for "WSR.INTSET".... */
2229 if (is_write
&& !is_user
&& !strcasecmp ("interrupt", sr_name
))
2231 new_opname
= (char *) xmalloc (strlen (sr_name
) + 6);
2232 sprintf (new_opname
, "%s.%s", *popname
, sr_name
);
2234 *popname
= new_opname
;
2241 xtensa_translate_old_userreg_ops (char **popname
)
2243 xtensa_isa isa
= xtensa_default_isa
;
2245 char *opname
, *new_opname
;
2246 const char *sr_name
;
2247 bfd_boolean has_underbar
= FALSE
;
2250 if (opname
[0] == '_')
2252 has_underbar
= TRUE
;
2256 sr
= xtensa_sysreg_lookup_name (isa
, opname
+ 1);
2257 if (sr
!= XTENSA_UNDEFINED
)
2259 /* The new default name ("nnn") is different from the old default
2260 name ("URnnn"). The old default is handled below, and we don't
2261 want to recognize [RW]nnn, so do nothing if the name is the (new)
2263 static char namebuf
[10];
2264 sprintf (namebuf
, "%d", xtensa_sysreg_number (isa
, sr
));
2265 if (strcmp (namebuf
, opname
+ 1) == 0)
2273 /* Only continue if the reg name is "URnnn". */
2274 if (opname
[1] != 'u' || opname
[2] != 'r')
2276 val
= strtoul (opname
+ 3, &end
, 10);
2280 sr
= xtensa_sysreg_lookup (isa
, val
, 1);
2281 if (sr
== XTENSA_UNDEFINED
)
2283 as_bad (_("invalid register number (%ld) for '%s'"),
2284 (long) val
, opname
);
2289 /* Translate the opcode. */
2290 sr_name
= xtensa_sysreg_name (isa
, sr
);
2291 new_opname
= (char *) xmalloc (strlen (sr_name
) + 6);
2292 sprintf (new_opname
, "%s%cur.%s", (has_underbar
? "_" : ""),
2293 opname
[0], sr_name
);
2295 *popname
= new_opname
;
2302 xtensa_translate_zero_immed (char *old_op
,
2312 assert (opname
[0] != '_');
2314 if (strcmp (opname
, old_op
) != 0)
2317 if (xg_check_num_args (pnum_args
, 3, opname
, arg_strings
))
2319 if (xg_arg_is_constant (arg_strings
[1], &val
) && val
== 0)
2321 xg_replace_opname (popname
, new_op
);
2322 free (arg_strings
[1]);
2323 arg_strings
[1] = arg_strings
[2];
2332 /* If the instruction is an idiom (i.e., a built-in macro), translate it.
2333 Returns non-zero if an error was found. */
2336 xg_translate_idioms (char **popname
, int *pnum_args
, char **arg_strings
)
2338 char *opname
= *popname
;
2339 bfd_boolean has_underbar
= FALSE
;
2343 has_underbar
= TRUE
;
2347 if (strcmp (opname
, "mov") == 0)
2349 if (use_transform () && !has_underbar
&& density_supported
)
2350 xg_replace_opname (popname
, "mov.n");
2353 if (xg_check_num_args (pnum_args
, 2, opname
, arg_strings
))
2355 xg_replace_opname (popname
, (has_underbar
? "_or" : "or"));
2356 arg_strings
[2] = (char *) xmalloc (strlen (arg_strings
[1]) + 1);
2357 strcpy (arg_strings
[2], arg_strings
[1]);
2363 if (strcmp (opname
, "bbsi.l") == 0)
2365 if (xg_check_num_args (pnum_args
, 3, opname
, arg_strings
))
2367 xg_replace_opname (popname
, (has_underbar
? "_bbsi" : "bbsi"));
2368 if (target_big_endian
)
2369 xg_reverse_shift_count (&arg_strings
[1]);
2373 if (strcmp (opname
, "bbci.l") == 0)
2375 if (xg_check_num_args (pnum_args
, 3, opname
, arg_strings
))
2377 xg_replace_opname (popname
, (has_underbar
? "_bbci" : "bbci"));
2378 if (target_big_endian
)
2379 xg_reverse_shift_count (&arg_strings
[1]);
2383 /* Don't do anything special with NOPs inside FLIX instructions. They
2384 are handled elsewhere. Real NOP instructions are always available
2385 in configurations with FLIX, so this should never be an issue but
2386 check for it anyway. */
2387 if (!cur_vinsn
.inside_bundle
&& xtensa_nop_opcode
== XTENSA_UNDEFINED
2388 && strcmp (opname
, "nop") == 0)
2390 if (use_transform () && !has_underbar
&& density_supported
)
2391 xg_replace_opname (popname
, "nop.n");
2394 if (xg_check_num_args (pnum_args
, 0, opname
, arg_strings
))
2396 xg_replace_opname (popname
, (has_underbar
? "_or" : "or"));
2397 arg_strings
[0] = (char *) xmalloc (3);
2398 arg_strings
[1] = (char *) xmalloc (3);
2399 arg_strings
[2] = (char *) xmalloc (3);
2400 strcpy (arg_strings
[0], "a1");
2401 strcpy (arg_strings
[1], "a1");
2402 strcpy (arg_strings
[2], "a1");
2408 /* Recognize [RW]UR and [RWX]SR. */
2409 if ((((opname
[0] == 'r' || opname
[0] == 'w')
2410 && (opname
[1] == 'u' || opname
[1] == 's'))
2411 || (opname
[0] == 'x' && opname
[1] == 's'))
2413 && opname
[3] == '\0')
2414 return xg_translate_sysreg_op (popname
, pnum_args
, arg_strings
);
2416 /* Backward compatibility for RUR and WUR: Recognize [RW]UR<nnn> and
2417 [RW]<name> if <name> is the non-default name of a user register. */
2418 if ((opname
[0] == 'r' || opname
[0] == 'w')
2419 && xtensa_opcode_lookup (xtensa_default_isa
, opname
) == XTENSA_UNDEFINED
)
2420 return xtensa_translate_old_userreg_ops (popname
);
2422 /* Relax branches that don't allow comparisons against an immediate value
2423 of zero to the corresponding branches with implicit zero immediates. */
2424 if (!has_underbar
&& use_transform ())
2426 if (xtensa_translate_zero_immed ("bnei", "bnez", popname
,
2427 pnum_args
, arg_strings
))
2430 if (xtensa_translate_zero_immed ("beqi", "beqz", popname
,
2431 pnum_args
, arg_strings
))
2434 if (xtensa_translate_zero_immed ("bgei", "bgez", popname
,
2435 pnum_args
, arg_strings
))
2438 if (xtensa_translate_zero_immed ("blti", "bltz", popname
,
2439 pnum_args
, arg_strings
))
2447 /* Functions for dealing with the Xtensa ISA. */
2449 /* Currently the assembler only allows us to use a single target per
2450 fragment. Because of this, only one operand for a given
2451 instruction may be symbolic. If there is a PC-relative operand,
2452 the last one is chosen. Otherwise, the result is the number of the
2453 last immediate operand, and if there are none of those, we fail and
2457 get_relaxable_immed (xtensa_opcode opcode
)
2459 int last_immed
= -1;
2462 if (opcode
== XTENSA_UNDEFINED
)
2465 noperands
= xtensa_opcode_num_operands (xtensa_default_isa
, opcode
);
2466 for (opi
= noperands
- 1; opi
>= 0; opi
--)
2468 if (xtensa_operand_is_visible (xtensa_default_isa
, opcode
, opi
) == 0)
2470 if (xtensa_operand_is_PCrelative (xtensa_default_isa
, opcode
, opi
) == 1)
2472 if (last_immed
== -1
2473 && xtensa_operand_is_register (xtensa_default_isa
, opcode
, opi
) == 0)
2480 static xtensa_opcode
2481 get_opcode_from_buf (const char *buf
, int slot
)
2483 static xtensa_insnbuf insnbuf
= NULL
;
2484 static xtensa_insnbuf slotbuf
= NULL
;
2485 xtensa_isa isa
= xtensa_default_isa
;
2490 insnbuf
= xtensa_insnbuf_alloc (isa
);
2491 slotbuf
= xtensa_insnbuf_alloc (isa
);
2494 xtensa_insnbuf_from_chars (isa
, insnbuf
, (const unsigned char *) buf
, 0);
2495 fmt
= xtensa_format_decode (isa
, insnbuf
);
2496 if (fmt
== XTENSA_UNDEFINED
)
2497 return XTENSA_UNDEFINED
;
2499 if (slot
>= xtensa_format_num_slots (isa
, fmt
))
2500 return XTENSA_UNDEFINED
;
2502 xtensa_format_get_slot (isa
, fmt
, slot
, insnbuf
, slotbuf
);
2503 return xtensa_opcode_decode (isa
, fmt
, slot
, slotbuf
);
2507 #ifdef TENSILICA_DEBUG
2509 /* For debugging, print out the mapping of opcode numbers to opcodes. */
2512 xtensa_print_insn_table (void)
2514 int num_opcodes
, num_operands
;
2515 xtensa_opcode opcode
;
2516 xtensa_isa isa
= xtensa_default_isa
;
2518 num_opcodes
= xtensa_isa_num_opcodes (xtensa_default_isa
);
2519 for (opcode
= 0; opcode
< num_opcodes
; opcode
++)
2522 fprintf (stderr
, "%d: %s: ", opcode
, xtensa_opcode_name (isa
, opcode
));
2523 num_operands
= xtensa_opcode_num_operands (isa
, opcode
);
2524 for (opn
= 0; opn
< num_operands
; opn
++)
2526 if (xtensa_operand_is_visible (isa
, opcode
, opn
) == 0)
2528 if (xtensa_operand_is_register (isa
, opcode
, opn
) == 1)
2530 xtensa_regfile opnd_rf
=
2531 xtensa_operand_regfile (isa
, opcode
, opn
);
2532 fprintf (stderr
, "%s ", xtensa_regfile_shortname (isa
, opnd_rf
));
2534 else if (xtensa_operand_is_PCrelative (isa
, opcode
, opn
) == 1)
2535 fputs ("[lLr] ", stderr
);
2537 fputs ("i ", stderr
);
2539 fprintf (stderr
, "\n");
2545 print_vliw_insn (xtensa_insnbuf vbuf
)
2547 xtensa_isa isa
= xtensa_default_isa
;
2548 xtensa_format f
= xtensa_format_decode (isa
, vbuf
);
2549 xtensa_insnbuf sbuf
= xtensa_insnbuf_alloc (isa
);
2552 fprintf (stderr
, "format = %d\n", f
);
2554 for (op
= 0; op
< xtensa_format_num_slots (isa
, f
); op
++)
2556 xtensa_opcode opcode
;
2560 xtensa_format_get_slot (isa
, f
, op
, vbuf
, sbuf
);
2561 opcode
= xtensa_opcode_decode (isa
, f
, op
, sbuf
);
2562 opname
= xtensa_opcode_name (isa
, opcode
);
2564 fprintf (stderr
, "op in slot %i is %s;\n", op
, opname
);
2565 fprintf (stderr
, " operands = ");
2567 operands
< xtensa_opcode_num_operands (isa
, opcode
);
2571 if (xtensa_operand_is_visible (isa
, opcode
, operands
) == 0)
2573 xtensa_operand_get_field (isa
, opcode
, operands
, f
, op
, sbuf
, &val
);
2574 xtensa_operand_decode (isa
, opcode
, operands
, &val
);
2575 fprintf (stderr
, "%d ", val
);
2577 fprintf (stderr
, "\n");
2579 xtensa_insnbuf_free (isa
, sbuf
);
2582 #endif /* TENSILICA_DEBUG */
2586 is_direct_call_opcode (xtensa_opcode opcode
)
2588 xtensa_isa isa
= xtensa_default_isa
;
2589 int n
, num_operands
;
2591 if (xtensa_opcode_is_call (isa
, opcode
) != 1)
2594 num_operands
= xtensa_opcode_num_operands (isa
, opcode
);
2595 for (n
= 0; n
< num_operands
; n
++)
2597 if (xtensa_operand_is_register (isa
, opcode
, n
) == 0
2598 && xtensa_operand_is_PCrelative (isa
, opcode
, n
) == 1)
2605 /* Convert from BFD relocation type code to slot and operand number.
2606 Returns non-zero on failure. */
2609 decode_reloc (bfd_reloc_code_real_type reloc
, int *slot
, bfd_boolean
*is_alt
)
2611 if (reloc
>= BFD_RELOC_XTENSA_SLOT0_OP
2612 && reloc
<= BFD_RELOC_XTENSA_SLOT14_OP
)
2614 *slot
= reloc
- BFD_RELOC_XTENSA_SLOT0_OP
;
2617 else if (reloc
>= BFD_RELOC_XTENSA_SLOT0_ALT
2618 && reloc
<= BFD_RELOC_XTENSA_SLOT14_ALT
)
2620 *slot
= reloc
- BFD_RELOC_XTENSA_SLOT0_ALT
;
2630 /* Convert from slot number to BFD relocation type code for the
2631 standard PC-relative relocations. Return BFD_RELOC_NONE on
2634 static bfd_reloc_code_real_type
2635 encode_reloc (int slot
)
2637 if (slot
< 0 || slot
> 14)
2638 return BFD_RELOC_NONE
;
2640 return BFD_RELOC_XTENSA_SLOT0_OP
+ slot
;
2644 /* Convert from slot numbers to BFD relocation type code for the
2645 "alternate" relocations. Return BFD_RELOC_NONE on failure. */
2647 static bfd_reloc_code_real_type
2648 encode_alt_reloc (int slot
)
2650 if (slot
< 0 || slot
> 14)
2651 return BFD_RELOC_NONE
;
2653 return BFD_RELOC_XTENSA_SLOT0_ALT
+ slot
;
2658 xtensa_insnbuf_set_operand (xtensa_insnbuf slotbuf
,
2661 xtensa_opcode opcode
,
2667 uint32 valbuf
= value
;
2669 if (xtensa_operand_encode (xtensa_default_isa
, opcode
, operand
, &valbuf
))
2671 if (xtensa_operand_is_PCrelative (xtensa_default_isa
, opcode
, operand
)
2673 as_bad_where ((char *) file
, line
,
2674 _("operand %d of '%s' has out of range value '%u'"),
2676 xtensa_opcode_name (xtensa_default_isa
, opcode
),
2679 as_bad_where ((char *) file
, line
,
2680 _("operand %d of '%s' has invalid value '%u'"),
2682 xtensa_opcode_name (xtensa_default_isa
, opcode
),
2687 xtensa_operand_set_field (xtensa_default_isa
, opcode
, operand
, fmt
, slot
,
2693 xtensa_insnbuf_get_operand (xtensa_insnbuf slotbuf
,
2696 xtensa_opcode opcode
,
2700 (void) xtensa_operand_get_field (xtensa_default_isa
, opcode
, opnum
,
2701 fmt
, slot
, slotbuf
, &val
);
2702 (void) xtensa_operand_decode (xtensa_default_isa
, opcode
, opnum
, &val
);
2707 /* Checks for rules from xtensa-relax tables. */
2709 /* The routine xg_instruction_matches_option_term must return TRUE
2710 when a given option term is true. The meaning of all of the option
2711 terms is given interpretation by this function. This is needed when
2712 an option depends on the state of a directive, but there are no such
2713 options in use right now. */
2716 xg_instruction_matches_option_term (TInsn
*insn ATTRIBUTE_UNUSED
,
2717 const ReqOrOption
*option
)
2719 if (strcmp (option
->option_name
, "realnop") == 0
2720 || strncmp (option
->option_name
, "IsaUse", 6) == 0)
2722 /* These conditions were evaluated statically when building the
2723 relaxation table. There's no need to reevaluate them now. */
2728 as_fatal (_("internal error: unknown option name '%s'"),
2729 option
->option_name
);
2735 xg_instruction_matches_or_options (TInsn
*insn
,
2736 const ReqOrOptionList
*or_option
)
2738 const ReqOrOption
*option
;
2739 /* Must match each of the AND terms. */
2740 for (option
= or_option
; option
!= NULL
; option
= option
->next
)
2742 if (xg_instruction_matches_option_term (insn
, option
))
2750 xg_instruction_matches_options (TInsn
*insn
, const ReqOptionList
*options
)
2752 const ReqOption
*req_options
;
2753 /* Must match each of the AND terms. */
2754 for (req_options
= options
;
2755 req_options
!= NULL
;
2756 req_options
= req_options
->next
)
2758 /* Must match one of the OR clauses. */
2759 if (!xg_instruction_matches_or_options (insn
,
2760 req_options
->or_option_terms
))
2767 /* Return the transition rule that matches or NULL if none matches. */
2770 xg_instruction_matches_rule (TInsn
*insn
, TransitionRule
*rule
)
2772 PreconditionList
*condition_l
;
2774 if (rule
->opcode
!= insn
->opcode
)
2777 for (condition_l
= rule
->conditions
;
2778 condition_l
!= NULL
;
2779 condition_l
= condition_l
->next
)
2783 Precondition
*cond
= condition_l
->precond
;
2788 /* The expression must be the constant. */
2789 assert (cond
->op_num
< insn
->ntok
);
2790 exp1
= &insn
->tok
[cond
->op_num
];
2791 if (expr_is_const (exp1
))
2796 if (get_expr_const (exp1
) != cond
->op_data
)
2800 if (get_expr_const (exp1
) == cond
->op_data
)
2807 else if (expr_is_register (exp1
))
2812 if (get_expr_register (exp1
) != cond
->op_data
)
2816 if (get_expr_register (exp1
) == cond
->op_data
)
2828 assert (cond
->op_num
< insn
->ntok
);
2829 assert (cond
->op_data
< insn
->ntok
);
2830 exp1
= &insn
->tok
[cond
->op_num
];
2831 exp2
= &insn
->tok
[cond
->op_data
];
2836 if (!expr_is_equal (exp1
, exp2
))
2840 if (expr_is_equal (exp1
, exp2
))
2852 if (!xg_instruction_matches_options (insn
, rule
->options
))
2860 transition_rule_cmp (const TransitionRule
*a
, const TransitionRule
*b
)
2862 bfd_boolean a_greater
= FALSE
;
2863 bfd_boolean b_greater
= FALSE
;
2865 ReqOptionList
*l_a
= a
->options
;
2866 ReqOptionList
*l_b
= b
->options
;
2868 /* We only care if they both are the same except for
2869 a const16 vs. an l32r. */
2871 while (l_a
&& l_b
&& ((l_a
->next
== NULL
) == (l_b
->next
== NULL
)))
2873 ReqOrOptionList
*l_or_a
= l_a
->or_option_terms
;
2874 ReqOrOptionList
*l_or_b
= l_b
->or_option_terms
;
2875 while (l_or_a
&& l_or_b
&& ((l_a
->next
== NULL
) == (l_b
->next
== NULL
)))
2877 if (l_or_a
->is_true
!= l_or_b
->is_true
)
2879 if (strcmp (l_or_a
->option_name
, l_or_b
->option_name
) != 0)
2881 /* This is the case we care about. */
2882 if (strcmp (l_or_a
->option_name
, "IsaUseConst16") == 0
2883 && strcmp (l_or_b
->option_name
, "IsaUseL32R") == 0)
2890 else if (strcmp (l_or_a
->option_name
, "IsaUseL32R") == 0
2891 && strcmp (l_or_b
->option_name
, "IsaUseConst16") == 0)
2901 l_or_a
= l_or_a
->next
;
2902 l_or_b
= l_or_b
->next
;
2904 if (l_or_a
|| l_or_b
)
2913 /* Incomparable if the substitution was used differently in two cases. */
2914 if (a_greater
&& b_greater
)
2926 static TransitionRule
*
2927 xg_instruction_match (TInsn
*insn
)
2929 TransitionTable
*table
= xg_build_simplify_table (&transition_rule_cmp
);
2931 assert (insn
->opcode
< table
->num_opcodes
);
2933 /* Walk through all of the possible transitions. */
2934 for (l
= table
->table
[insn
->opcode
]; l
!= NULL
; l
= l
->next
)
2936 TransitionRule
*rule
= l
->rule
;
2937 if (xg_instruction_matches_rule (insn
, rule
))
2944 /* Various Other Internal Functions. */
2947 is_unique_insn_expansion (TransitionRule
*r
)
2949 if (!r
->to_instr
|| r
->to_instr
->next
!= NULL
)
2951 if (r
->to_instr
->typ
!= INSTR_INSTR
)
2957 /* Check if there is exactly one relaxation for INSN that converts it to
2958 another instruction of equal or larger size. If so, and if TARG is
2959 non-null, go ahead and generate the relaxed instruction into TARG. If
2960 NARROW_ONLY is true, then only consider relaxations that widen a narrow
2961 instruction, i.e., ignore relaxations that convert to an instruction of
2962 equal size. In some contexts where this function is used, only
2963 a single widening is allowed and the NARROW_ONLY argument is used to
2964 exclude cases like ADDI being "widened" to an ADDMI, which may
2965 later be relaxed to an ADDMI/ADDI pair. */
2968 xg_is_single_relaxable_insn (TInsn
*insn
, TInsn
*targ
, bfd_boolean narrow_only
)
2970 TransitionTable
*table
= xg_build_widen_table (&transition_rule_cmp
);
2972 TransitionRule
*match
= 0;
2974 assert (insn
->insn_type
== ITYPE_INSN
);
2975 assert (insn
->opcode
< table
->num_opcodes
);
2977 for (l
= table
->table
[insn
->opcode
]; l
!= NULL
; l
= l
->next
)
2979 TransitionRule
*rule
= l
->rule
;
2981 if (xg_instruction_matches_rule (insn
, rule
)
2982 && is_unique_insn_expansion (rule
)
2983 && (xg_get_single_size (insn
->opcode
) + (narrow_only
? 1 : 0)
2984 <= xg_get_single_size (rule
->to_instr
->opcode
)))
2995 xg_build_to_insn (targ
, insn
, match
->to_instr
);
3000 /* Return the maximum number of bytes this opcode can expand to. */
3003 xg_get_max_insn_widen_size (xtensa_opcode opcode
)
3005 TransitionTable
*table
= xg_build_widen_table (&transition_rule_cmp
);
3007 int max_size
= xg_get_single_size (opcode
);
3009 assert (opcode
< table
->num_opcodes
);
3011 for (l
= table
->table
[opcode
]; l
!= NULL
; l
= l
->next
)
3013 TransitionRule
*rule
= l
->rule
;
3014 BuildInstr
*build_list
;
3019 build_list
= rule
->to_instr
;
3020 if (is_unique_insn_expansion (rule
))
3022 assert (build_list
->typ
== INSTR_INSTR
);
3023 this_size
= xg_get_max_insn_widen_size (build_list
->opcode
);
3026 for (; build_list
!= NULL
; build_list
= build_list
->next
)
3028 switch (build_list
->typ
)
3031 this_size
+= xg_get_single_size (build_list
->opcode
);
3033 case INSTR_LITERAL_DEF
:
3034 case INSTR_LABEL_DEF
:
3039 if (this_size
> max_size
)
3040 max_size
= this_size
;
3046 /* Return the maximum number of literal bytes this opcode can generate. */
3049 xg_get_max_insn_widen_literal_size (xtensa_opcode opcode
)
3051 TransitionTable
*table
= xg_build_widen_table (&transition_rule_cmp
);
3055 assert (opcode
< table
->num_opcodes
);
3057 for (l
= table
->table
[opcode
]; l
!= NULL
; l
= l
->next
)
3059 TransitionRule
*rule
= l
->rule
;
3060 BuildInstr
*build_list
;
3065 build_list
= rule
->to_instr
;
3066 if (is_unique_insn_expansion (rule
))
3068 assert (build_list
->typ
== INSTR_INSTR
);
3069 this_size
= xg_get_max_insn_widen_literal_size (build_list
->opcode
);
3072 for (; build_list
!= NULL
; build_list
= build_list
->next
)
3074 switch (build_list
->typ
)
3076 case INSTR_LITERAL_DEF
:
3077 /* Hard-coded 4-byte literal. */
3081 case INSTR_LABEL_DEF
:
3086 if (this_size
> max_size
)
3087 max_size
= this_size
;
3094 xg_is_relaxable_insn (TInsn
*insn
, int lateral_steps
)
3096 int steps_taken
= 0;
3097 TransitionTable
*table
= xg_build_widen_table (&transition_rule_cmp
);
3100 assert (insn
->insn_type
== ITYPE_INSN
);
3101 assert (insn
->opcode
< table
->num_opcodes
);
3103 for (l
= table
->table
[insn
->opcode
]; l
!= NULL
; l
= l
->next
)
3105 TransitionRule
*rule
= l
->rule
;
3107 if (xg_instruction_matches_rule (insn
, rule
))
3109 if (steps_taken
== lateral_steps
)
3119 get_special_literal_symbol (void)
3121 static symbolS
*sym
= NULL
;
3124 sym
= symbol_find_or_make ("SPECIAL_LITERAL0\001");
3130 get_special_label_symbol (void)
3132 static symbolS
*sym
= NULL
;
3135 sym
= symbol_find_or_make ("SPECIAL_LABEL0\001");
3141 xg_valid_literal_expression (const expressionS
*exp
)
3159 /* This will check to see if the value can be converted into the
3160 operand type. It will return TRUE if it does not fit. */
3163 xg_check_operand (int32 value
, xtensa_opcode opcode
, int operand
)
3165 uint32 valbuf
= value
;
3166 if (xtensa_operand_encode (xtensa_default_isa
, opcode
, operand
, &valbuf
))
3172 /* Assumes: All immeds are constants. Check that all constants fit
3173 into their immeds; return FALSE if not. */
3176 xg_immeds_fit (const TInsn
*insn
)
3178 xtensa_isa isa
= xtensa_default_isa
;
3182 assert (insn
->insn_type
== ITYPE_INSN
);
3183 for (i
= 0; i
< n
; ++i
)
3185 const expressionS
*expr
= &insn
->tok
[i
];
3186 if (xtensa_operand_is_register (isa
, insn
->opcode
, i
) == 1)
3193 if (xg_check_operand (expr
->X_add_number
, insn
->opcode
, i
))
3198 /* The symbol should have a fixup associated with it. */
3207 /* This should only be called after we have an initial
3208 estimate of the addresses. */
3211 xg_symbolic_immeds_fit (const TInsn
*insn
,
3217 xtensa_isa isa
= xtensa_default_isa
;
3225 assert (insn
->insn_type
== ITYPE_INSN
);
3227 for (i
= 0; i
< n
; ++i
)
3229 const expressionS
*expr
= &insn
->tok
[i
];
3230 if (xtensa_operand_is_register (isa
, insn
->opcode
, i
) == 1)
3237 if (xg_check_operand (expr
->X_add_number
, insn
->opcode
, i
))
3243 /* Check for the worst case. */
3244 if (xg_check_operand (0xffff, insn
->opcode
, i
))
3249 /* We only allow symbols for PC-relative references.
3250 If pc_frag == 0, then we don't have frag locations yet. */
3252 || xtensa_operand_is_PCrelative (isa
, insn
->opcode
, i
) == 0)
3255 /* If it is a weak symbol or a symbol in a different section,
3256 it cannot be known to fit at assembly time. */
3257 if (S_IS_WEAK (expr
->X_add_symbol
)
3258 || S_GET_SEGMENT (expr
->X_add_symbol
) != pc_seg
)
3260 /* For a direct call with --no-longcalls, be optimistic and
3261 assume it will be in range. If the symbol is weak and
3262 undefined, it may remain undefined at link-time, in which
3263 case it will have a zero value and almost certainly be out
3264 of range for a direct call; thus, relax for undefined weak
3265 symbols even if longcalls is not enabled. */
3266 if (is_direct_call_opcode (insn
->opcode
)
3267 && ! pc_frag
->tc_frag_data
.use_longcalls
3268 && (! S_IS_WEAK (expr
->X_add_symbol
)
3269 || S_IS_DEFINED (expr
->X_add_symbol
)))
3275 symbolP
= expr
->X_add_symbol
;
3276 sym_frag
= symbol_get_frag (symbolP
);
3277 target
= S_GET_VALUE (symbolP
) + expr
->X_add_number
;
3278 pc
= pc_frag
->fr_address
+ pc_offset
;
3280 /* If frag has yet to be reached on this pass, assume it
3281 will move by STRETCH just as we did. If this is not so,
3282 it will be because some frag between grows, and that will
3283 force another pass. Beware zero-length frags. There
3284 should be a faster way to do this. */
3287 && sym_frag
->relax_marker
!= pc_frag
->relax_marker
3288 && S_GET_SEGMENT (symbolP
) == pc_seg
)
3293 new_offset
= target
;
3294 xtensa_operand_do_reloc (isa
, insn
->opcode
, i
, &new_offset
, pc
);
3295 if (xg_check_operand (new_offset
, insn
->opcode
, i
))
3300 /* The symbol should have a fixup associated with it. */
3309 /* Return TRUE on success. */
3312 xg_build_to_insn (TInsn
*targ
, TInsn
*insn
, BuildInstr
*bi
)
3318 targ
->debug_line
= insn
->debug_line
;
3319 targ
->loc_directive_seen
= insn
->loc_directive_seen
;
3324 targ
->opcode
= bi
->opcode
;
3325 targ
->insn_type
= ITYPE_INSN
;
3326 targ
->is_specific_opcode
= FALSE
;
3328 for (; op
!= NULL
; op
= op
->next
)
3330 int op_num
= op
->op_num
;
3331 int op_data
= op
->op_data
;
3333 assert (op
->op_num
< MAX_INSN_ARGS
);
3335 if (targ
->ntok
<= op_num
)
3336 targ
->ntok
= op_num
+ 1;
3341 set_expr_const (&targ
->tok
[op_num
], op_data
);
3344 assert (op_data
< insn
->ntok
);
3345 copy_expr (&targ
->tok
[op_num
], &insn
->tok
[op_data
]);
3348 sym
= get_special_literal_symbol ();
3349 set_expr_symbol_offset (&targ
->tok
[op_num
], sym
, 0);
3352 sym
= get_special_label_symbol ();
3353 set_expr_symbol_offset (&targ
->tok
[op_num
], sym
, 0);
3355 case OP_OPERAND_HI16U
:
3356 case OP_OPERAND_LOW16U
:
3357 assert (op_data
< insn
->ntok
);
3358 if (expr_is_const (&insn
->tok
[op_data
]))
3361 copy_expr (&targ
->tok
[op_num
], &insn
->tok
[op_data
]);
3362 val
= xg_apply_userdef_op_fn (op
->typ
,
3365 targ
->tok
[op_num
].X_add_number
= val
;
3369 /* For const16 we can create relocations for these. */
3370 if (targ
->opcode
== XTENSA_UNDEFINED
3371 || (targ
->opcode
!= xtensa_const16_opcode
))
3373 assert (op_data
< insn
->ntok
);
3374 /* Need to build a O_lo16 or O_hi16. */
3375 copy_expr (&targ
->tok
[op_num
], &insn
->tok
[op_data
]);
3376 if (targ
->tok
[op_num
].X_op
== O_symbol
)
3378 if (op
->typ
== OP_OPERAND_HI16U
)
3379 targ
->tok
[op_num
].X_op
= O_hi16
;
3380 else if (op
->typ
== OP_OPERAND_LOW16U
)
3381 targ
->tok
[op_num
].X_op
= O_lo16
;
3388 /* currently handles:
3391 OP_OPERAND_F32MINUS */
3392 if (xg_has_userdef_op_fn (op
->typ
))
3394 assert (op_data
< insn
->ntok
);
3395 if (expr_is_const (&insn
->tok
[op_data
]))
3398 copy_expr (&targ
->tok
[op_num
], &insn
->tok
[op_data
]);
3399 val
= xg_apply_userdef_op_fn (op
->typ
,
3402 targ
->tok
[op_num
].X_add_number
= val
;
3405 return FALSE
; /* We cannot use a relocation for this. */
3414 case INSTR_LITERAL_DEF
:
3416 targ
->opcode
= XTENSA_UNDEFINED
;
3417 targ
->insn_type
= ITYPE_LITERAL
;
3418 targ
->is_specific_opcode
= FALSE
;
3419 for (; op
!= NULL
; op
= op
->next
)
3421 int op_num
= op
->op_num
;
3422 int op_data
= op
->op_data
;
3423 assert (op
->op_num
< MAX_INSN_ARGS
);
3425 if (targ
->ntok
<= op_num
)
3426 targ
->ntok
= op_num
+ 1;
3431 assert (op_data
< insn
->ntok
);
3432 /* We can only pass resolvable literals through. */
3433 if (!xg_valid_literal_expression (&insn
->tok
[op_data
]))
3435 copy_expr (&targ
->tok
[op_num
], &insn
->tok
[op_data
]);
3447 case INSTR_LABEL_DEF
:
3449 targ
->opcode
= XTENSA_UNDEFINED
;
3450 targ
->insn_type
= ITYPE_LABEL
;
3451 targ
->is_specific_opcode
= FALSE
;
3452 /* Literal with no ops is a label? */
3453 assert (op
== NULL
);
3464 /* Return TRUE on success. */
3467 xg_build_to_stack (IStack
*istack
, TInsn
*insn
, BuildInstr
*bi
)
3469 for (; bi
!= NULL
; bi
= bi
->next
)
3471 TInsn
*next_insn
= istack_push_space (istack
);
3473 if (!xg_build_to_insn (next_insn
, insn
, bi
))
3480 /* Return TRUE on valid expansion. */
3483 xg_expand_to_stack (IStack
*istack
, TInsn
*insn
, int lateral_steps
)
3485 int stack_size
= istack
->ninsn
;
3486 int steps_taken
= 0;
3487 TransitionTable
*table
= xg_build_widen_table (&transition_rule_cmp
);
3490 assert (insn
->insn_type
== ITYPE_INSN
);
3491 assert (insn
->opcode
< table
->num_opcodes
);
3493 for (l
= table
->table
[insn
->opcode
]; l
!= NULL
; l
= l
->next
)
3495 TransitionRule
*rule
= l
->rule
;
3497 if (xg_instruction_matches_rule (insn
, rule
))
3499 if (lateral_steps
== steps_taken
)
3503 /* This is it. Expand the rule to the stack. */
3504 if (!xg_build_to_stack (istack
, insn
, rule
->to_instr
))
3507 /* Check to see if it fits. */
3508 for (i
= stack_size
; i
< istack
->ninsn
; i
++)
3510 TInsn
*insn
= &istack
->insn
[i
];
3512 if (insn
->insn_type
== ITYPE_INSN
3513 && !tinsn_has_symbolic_operands (insn
)
3514 && !xg_immeds_fit (insn
))
3516 istack
->ninsn
= stack_size
;
3529 /* Relax the assembly instruction at least "min_steps".
3530 Return the number of steps taken.
3532 For relaxation to correctly terminate, every relaxation chain must
3533 terminate in one of two ways:
3535 1. If the chain from one instruction to the next consists entirely of
3536 single instructions, then the chain *must* handle all possible
3537 immediates without failing. It must not ever fail because an
3538 immediate is out of range. The MOVI.N -> MOVI -> L32R relaxation
3539 chain is one example. L32R loads 32 bits, and there cannot be an
3540 immediate larger than 32 bits, so it satisfies this condition.
3541 Single instruction relaxation chains are as defined by
3542 xg_is_single_relaxable_instruction.
3544 2. Otherwise, the chain must end in a multi-instruction expansion: e.g.,
3545 BNEZ.N -> BNEZ -> BNEZ.W15 -> BENZ.N/J
3547 Strictly speaking, in most cases you can violate condition 1 and be OK
3548 -- in particular when the last two instructions have the same single
3549 size. But nevertheless, you should guarantee the above two conditions.
3551 We could fix this so that single-instruction expansions correctly
3552 terminate when they can't handle the range, but the error messages are
3553 worse, and it actually turns out that in every case but one (18-bit wide
3554 branches), you need a multi-instruction expansion to get the full range
3555 anyway. And because 18-bit branches are handled identically to 15-bit
3556 branches, there isn't any point in changing it. */
3559 xg_assembly_relax (IStack
*istack
,
3562 fragS
*pc_frag
, /* if pc_frag == 0, not pc-relative */
3563 offsetT pc_offset
, /* offset in fragment */
3564 int min_steps
, /* minimum conversion steps */
3565 long stretch
) /* number of bytes stretched so far */
3567 int steps_taken
= 0;
3569 /* Some of its immeds don't fit. Try to build a relaxed version.
3570 This may go through a couple of stages of single instruction
3571 transformations before we get there. */
3573 TInsn single_target
;
3575 int lateral_steps
= 0;
3576 int istack_size
= istack
->ninsn
;
3578 if (xg_symbolic_immeds_fit (insn
, pc_seg
, pc_frag
, pc_offset
, stretch
)
3579 && steps_taken
>= min_steps
)
3581 istack_push (istack
, insn
);
3584 current_insn
= *insn
;
3586 /* Walk through all of the single instruction expansions. */
3587 while (xg_is_single_relaxable_insn (¤t_insn
, &single_target
, FALSE
))
3590 if (xg_symbolic_immeds_fit (&single_target
, pc_seg
, pc_frag
, pc_offset
,
3593 if (steps_taken
>= min_steps
)
3595 istack_push (istack
, &single_target
);
3599 current_insn
= single_target
;
3602 /* Now check for a multi-instruction expansion. */
3603 while (xg_is_relaxable_insn (¤t_insn
, lateral_steps
))
3605 if (xg_symbolic_immeds_fit (¤t_insn
, pc_seg
, pc_frag
, pc_offset
,
3608 if (steps_taken
>= min_steps
)
3610 istack_push (istack
, ¤t_insn
);
3615 if (xg_expand_to_stack (istack
, ¤t_insn
, lateral_steps
))
3617 if (steps_taken
>= min_steps
)
3621 istack
->ninsn
= istack_size
;
3624 /* It's not going to work -- use the original. */
3625 istack_push (istack
, insn
);
3631 xg_finish_frag (char *last_insn
,
3632 enum xtensa_relax_statesE frag_state
,
3633 enum xtensa_relax_statesE slot0_state
,
3635 bfd_boolean is_insn
)
3637 /* Finish off this fragment so that it has at LEAST the desired
3638 max_growth. If it doesn't fit in this fragment, close this one
3639 and start a new one. In either case, return a pointer to the
3640 beginning of the growth area. */
3644 frag_grow (max_growth
);
3645 old_frag
= frag_now
;
3647 frag_now
->fr_opcode
= last_insn
;
3649 frag_now
->tc_frag_data
.is_insn
= TRUE
;
3651 frag_var (rs_machine_dependent
, max_growth
, max_growth
,
3652 frag_state
, frag_now
->fr_symbol
, frag_now
->fr_offset
, last_insn
);
3654 old_frag
->tc_frag_data
.slot_subtypes
[0] = slot0_state
;
3655 xtensa_set_frag_assembly_state (frag_now
);
3657 /* Just to make sure that we did not split it up. */
3658 assert (old_frag
->fr_next
== frag_now
);
3662 /* Return TRUE if the target frag is one of the next non-empty frags. */
3665 is_next_frag_target (const fragS
*fragP
, const fragS
*target
)
3670 for (; fragP
; fragP
= fragP
->fr_next
)
3672 if (fragP
== target
)
3674 if (fragP
->fr_fix
!= 0)
3676 if (fragP
->fr_type
== rs_fill
&& fragP
->fr_offset
!= 0)
3678 if ((fragP
->fr_type
== rs_align
|| fragP
->fr_type
== rs_align_code
)
3679 && ((fragP
->fr_address
% (1 << fragP
->fr_offset
)) != 0))
3681 if (fragP
->fr_type
== rs_space
)
3689 is_branch_jmp_to_next (TInsn
*insn
, fragS
*fragP
)
3691 xtensa_isa isa
= xtensa_default_isa
;
3693 int num_ops
= xtensa_opcode_num_operands (isa
, insn
->opcode
);
3698 if (xtensa_opcode_is_branch (isa
, insn
->opcode
) != 1
3699 && xtensa_opcode_is_jump (isa
, insn
->opcode
) != 1)
3702 for (i
= 0; i
< num_ops
; i
++)
3704 if (xtensa_operand_is_PCrelative (isa
, insn
->opcode
, i
) == 1)
3710 if (target_op
== -1)
3713 if (insn
->ntok
<= target_op
)
3716 if (insn
->tok
[target_op
].X_op
!= O_symbol
)
3719 sym
= insn
->tok
[target_op
].X_add_symbol
;
3723 if (insn
->tok
[target_op
].X_add_number
!= 0)
3726 target_frag
= symbol_get_frag (sym
);
3727 if (target_frag
== NULL
)
3730 if (is_next_frag_target (fragP
->fr_next
, target_frag
)
3731 && S_GET_VALUE (sym
) == target_frag
->fr_address
)
3739 xg_add_branch_and_loop_targets (TInsn
*insn
)
3741 xtensa_isa isa
= xtensa_default_isa
;
3742 int num_ops
= xtensa_opcode_num_operands (isa
, insn
->opcode
);
3744 if (xtensa_opcode_is_loop (isa
, insn
->opcode
) == 1)
3747 if (xtensa_operand_is_PCrelative (isa
, insn
->opcode
, i
) == 1
3748 && insn
->tok
[i
].X_op
== O_symbol
)
3749 symbol_get_tc (insn
->tok
[i
].X_add_symbol
)->is_loop_target
= TRUE
;
3753 if (xtensa_opcode_is_branch (isa
, insn
->opcode
) == 1
3754 || xtensa_opcode_is_loop (isa
, insn
->opcode
) == 1)
3758 for (i
= 0; i
< insn
->ntok
&& i
< num_ops
; i
++)
3760 if (xtensa_operand_is_PCrelative (isa
, insn
->opcode
, i
) == 1
3761 && insn
->tok
[i
].X_op
== O_symbol
)
3763 symbolS
*sym
= insn
->tok
[i
].X_add_symbol
;
3764 symbol_get_tc (sym
)->is_branch_target
= TRUE
;
3765 if (S_IS_DEFINED (sym
))
3766 symbol_get_frag (sym
)->tc_frag_data
.is_branch_target
= TRUE
;
3773 /* Return FALSE if no error. */
3776 xg_build_token_insn (BuildInstr
*instr_spec
, TInsn
*old_insn
, TInsn
*new_insn
)
3781 switch (instr_spec
->typ
)
3784 new_insn
->insn_type
= ITYPE_INSN
;
3785 new_insn
->opcode
= instr_spec
->opcode
;
3787 case INSTR_LITERAL_DEF
:
3788 new_insn
->insn_type
= ITYPE_LITERAL
;
3789 new_insn
->opcode
= XTENSA_UNDEFINED
;
3791 case INSTR_LABEL_DEF
:
3794 new_insn
->is_specific_opcode
= FALSE
;
3795 new_insn
->debug_line
= old_insn
->debug_line
;
3796 new_insn
->loc_directive_seen
= old_insn
->loc_directive_seen
;
3798 for (b_op
= instr_spec
->ops
; b_op
!= NULL
; b_op
= b_op
->next
)
3801 const expressionS
*src_exp
;
3807 /* The expression must be the constant. */
3808 assert (b_op
->op_num
< MAX_INSN_ARGS
);
3809 exp
= &new_insn
->tok
[b_op
->op_num
];
3810 set_expr_const (exp
, b_op
->op_data
);
3814 assert (b_op
->op_num
< MAX_INSN_ARGS
);
3815 assert (b_op
->op_data
< (unsigned) old_insn
->ntok
);
3816 src_exp
= &old_insn
->tok
[b_op
->op_data
];
3817 exp
= &new_insn
->tok
[b_op
->op_num
];
3818 copy_expr (exp
, src_exp
);
3823 as_bad (_("can't handle generation of literal/labels yet"));
3827 as_bad (_("can't handle undefined OP TYPE"));
3832 new_insn
->ntok
= num_ops
;
3837 /* Return TRUE if it was simplified. */
3840 xg_simplify_insn (TInsn
*old_insn
, TInsn
*new_insn
)
3842 TransitionRule
*rule
;
3843 BuildInstr
*insn_spec
;
3845 if (old_insn
->is_specific_opcode
|| !density_supported
)
3848 rule
= xg_instruction_match (old_insn
);
3852 insn_spec
= rule
->to_instr
;
3853 /* There should only be one. */
3854 assert (insn_spec
!= NULL
);
3855 assert (insn_spec
->next
== NULL
);
3856 if (insn_spec
->next
!= NULL
)
3859 xg_build_token_insn (insn_spec
, old_insn
, new_insn
);
3865 /* xg_expand_assembly_insn: (1) Simplify the instruction, i.e., l32i ->
3866 l32i.n. (2) Check the number of operands. (3) Place the instruction
3867 tokens into the stack or relax it and place multiple
3868 instructions/literals onto the stack. Return FALSE if no error. */
3871 xg_expand_assembly_insn (IStack
*istack
, TInsn
*orig_insn
)
3875 bfd_boolean do_expand
;
3877 tinsn_init (&new_insn
);
3879 /* Narrow it if we can. xg_simplify_insn now does all the
3880 appropriate checking (e.g., for the density option). */
3881 if (xg_simplify_insn (orig_insn
, &new_insn
))
3882 orig_insn
= &new_insn
;
3884 noperands
= xtensa_opcode_num_operands (xtensa_default_isa
,
3886 if (orig_insn
->ntok
< noperands
)
3888 as_bad (_("found %d operands for '%s': Expected %d"),
3890 xtensa_opcode_name (xtensa_default_isa
, orig_insn
->opcode
),
3894 if (orig_insn
->ntok
> noperands
)
3895 as_warn (_("found too many (%d) operands for '%s': Expected %d"),
3897 xtensa_opcode_name (xtensa_default_isa
, orig_insn
->opcode
),
3900 /* If there are not enough operands, we will assert above. If there
3901 are too many, just cut out the extras here. */
3902 orig_insn
->ntok
= noperands
;
3904 if (tinsn_has_invalid_symbolic_operands (orig_insn
))
3907 /* Special case for extui opcode which has constraints not handled
3908 by the ordinary operand encoding checks. The number of operands
3909 and related syntax issues have already been checked. */
3910 if (orig_insn
->opcode
== xtensa_extui_opcode
)
3912 int shiftimm
= orig_insn
->tok
[2].X_add_number
;
3913 int maskimm
= orig_insn
->tok
[3].X_add_number
;
3914 if (shiftimm
+ maskimm
> 32)
3916 as_bad (_("immediate operands sum to greater than 32"));
3921 /* If the instruction will definitely need to be relaxed, it is better
3922 to expand it now for better scheduling. Decide whether to expand
3924 do_expand
= (!orig_insn
->is_specific_opcode
&& use_transform ());
3926 /* Calls should be expanded to longcalls only in the backend relaxation
3927 so that the assembly scheduler will keep the L32R/CALLX instructions
3929 if (is_direct_call_opcode (orig_insn
->opcode
))
3932 if (tinsn_has_symbolic_operands (orig_insn
))
3934 /* The values of symbolic operands are not known yet, so only expand
3935 now if an operand is "complex" (e.g., difference of symbols) and
3936 will have to be stored as a literal regardless of the value. */
3937 if (!tinsn_has_complex_operands (orig_insn
))
3940 else if (xg_immeds_fit (orig_insn
))
3944 xg_assembly_relax (istack
, orig_insn
, 0, 0, 0, 0, 0);
3946 istack_push (istack
, orig_insn
);
3952 /* Return TRUE if the section flags are marked linkonce
3953 or the name is .gnu.linkonce.*. */
3955 static int linkonce_len
= sizeof (".gnu.linkonce.") - 1;
3958 get_is_linkonce_section (bfd
*abfd ATTRIBUTE_UNUSED
, segT sec
)
3960 flagword flags
, link_once_flags
;
3962 flags
= bfd_get_section_flags (abfd
, sec
);
3963 link_once_flags
= (flags
& SEC_LINK_ONCE
);
3965 /* Flags might not be set yet. */
3966 if (!link_once_flags
3967 && strncmp (segment_name (sec
), ".gnu.linkonce.", linkonce_len
) == 0)
3968 link_once_flags
= SEC_LINK_ONCE
;
3970 return (link_once_flags
!= 0);
3975 xtensa_add_literal_sym (symbolS
*sym
)
3979 l
= (sym_list
*) xmalloc (sizeof (sym_list
));
3981 l
->next
= literal_syms
;
3987 xtensa_create_literal_symbol (segT sec
, fragS
*frag
)
3989 static int lit_num
= 0;
3990 static char name
[256];
3993 sprintf (name
, ".L_lit_sym%d", lit_num
);
3995 /* Create a local symbol. If it is in a linkonce section, we have to
3996 be careful to make sure that if it is used in a relocation that the
3997 symbol will be in the output file. */
3998 if (get_is_linkonce_section (stdoutput
, sec
))
4000 symbolP
= symbol_new (name
, sec
, 0, frag
);
4001 S_CLEAR_EXTERNAL (symbolP
);
4002 /* symbolP->local = 1; */
4005 symbolP
= symbol_new (name
, sec
, 0, frag
);
4007 xtensa_add_literal_sym (symbolP
);
4014 /* Currently all literals that are generated here are 32-bit L32R targets. */
4017 xg_assemble_literal (/* const */ TInsn
*insn
)
4020 symbolS
*lit_sym
= NULL
;
4021 bfd_reloc_code_real_type reloc
;
4022 bfd_boolean pcrel
= FALSE
;
4025 /* size = 4 for L32R. It could easily be larger when we move to
4026 larger constants. Add a parameter later. */
4027 offsetT litsize
= 4;
4028 offsetT litalign
= 2; /* 2^2 = 4 */
4029 expressionS saved_loc
;
4030 expressionS
* emit_val
;
4032 set_expr_symbol_offset (&saved_loc
, frag_now
->fr_symbol
, frag_now_fix ());
4034 assert (insn
->insn_type
== ITYPE_LITERAL
);
4035 assert (insn
->ntok
== 1); /* must be only one token here */
4037 xtensa_switch_to_literal_fragment (&state
);
4039 emit_val
= &insn
->tok
[0];
4040 if (emit_val
->X_op
== O_big
)
4042 int size
= emit_val
->X_add_number
* CHARS_PER_LITTLENUM
;
4045 /* This happens when someone writes a "movi a2, big_number". */
4046 as_bad_where (frag_now
->fr_file
, frag_now
->fr_line
,
4047 _("invalid immediate"));
4048 xtensa_restore_emit_state (&state
);
4053 /* Force a 4-byte align here. Note that this opens a new frag, so all
4054 literals done with this function have a frag to themselves. That's
4055 important for the way text section literals work. */
4056 frag_align (litalign
, 0, 0);
4057 record_alignment (now_seg
, litalign
);
4059 switch (emit_val
->X_op
)
4065 p
= frag_more (litsize
);
4066 xtensa_set_frag_assembly_state (frag_now
);
4067 reloc
= map_operator_to_reloc (emit_val
->X_op
);
4068 if (emit_val
->X_add_symbol
)
4069 emit_val
->X_op
= O_symbol
;
4071 emit_val
->X_op
= O_constant
;
4072 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
,
4073 litsize
, emit_val
, pcrel
, reloc
);
4077 emit_expr (emit_val
, litsize
);
4081 assert (frag_now
->tc_frag_data
.literal_frag
== NULL
);
4082 frag_now
->tc_frag_data
.literal_frag
= get_literal_pool_location (now_seg
);
4083 frag_now
->fr_symbol
= xtensa_create_literal_symbol (now_seg
, frag_now
);
4084 lit_sym
= frag_now
->fr_symbol
;
4087 xtensa_restore_emit_state (&state
);
4093 xg_assemble_literal_space (/* const */ int size
, int slot
)
4096 /* We might have to do something about this alignment. It only
4097 takes effect if something is placed here. */
4098 offsetT litalign
= 2; /* 2^2 = 4 */
4099 fragS
*lit_saved_frag
;
4101 assert (size
% 4 == 0);
4103 xtensa_switch_to_literal_fragment (&state
);
4105 /* Force a 4-byte align here. */
4106 frag_align (litalign
, 0, 0);
4107 record_alignment (now_seg
, litalign
);
4111 lit_saved_frag
= frag_now
;
4112 frag_now
->tc_frag_data
.literal_frag
= get_literal_pool_location (now_seg
);
4113 frag_now
->fr_symbol
= xtensa_create_literal_symbol (now_seg
, frag_now
);
4114 xg_finish_frag (0, RELAX_LITERAL
, 0, size
, FALSE
);
4117 xtensa_restore_emit_state (&state
);
4118 frag_now
->tc_frag_data
.literal_frags
[slot
] = lit_saved_frag
;
4122 /* Put in a fixup record based on the opcode.
4123 Return TRUE on success. */
4126 xg_add_opcode_fix (TInsn
*tinsn
,
4134 xtensa_opcode opcode
= tinsn
->opcode
;
4135 bfd_reloc_code_real_type reloc
;
4136 reloc_howto_type
*howto
;
4140 reloc
= BFD_RELOC_NONE
;
4142 /* First try the special cases for "alternate" relocs. */
4143 if (opcode
== xtensa_l32r_opcode
)
4145 if (fragP
->tc_frag_data
.use_absolute_literals
)
4146 reloc
= encode_alt_reloc (slot
);
4148 else if (opcode
== xtensa_const16_opcode
)
4150 if (expr
->X_op
== O_lo16
)
4152 reloc
= encode_reloc (slot
);
4153 expr
->X_op
= O_symbol
;
4155 else if (expr
->X_op
== O_hi16
)
4157 reloc
= encode_alt_reloc (slot
);
4158 expr
->X_op
= O_symbol
;
4162 if (opnum
!= get_relaxable_immed (opcode
))
4164 as_bad (_("invalid relocation for operand %i of '%s'"),
4165 opnum
+ 1, xtensa_opcode_name (xtensa_default_isa
, opcode
));
4169 /* Handle erroneous "@h" and "@l" expressions here before they propagate
4170 into the symbol table where the generic portions of the assembler
4171 won't know what to do with them. */
4172 if (expr
->X_op
== O_lo16
|| expr
->X_op
== O_hi16
)
4174 as_bad (_("invalid expression for operand %i of '%s'"),
4175 opnum
+ 1, xtensa_opcode_name (xtensa_default_isa
, opcode
));
4179 /* Next try the generic relocs. */
4180 if (reloc
== BFD_RELOC_NONE
)
4181 reloc
= encode_reloc (slot
);
4182 if (reloc
== BFD_RELOC_NONE
)
4184 as_bad (_("invalid relocation in instruction slot %i"), slot
);
4188 howto
= bfd_reloc_type_lookup (stdoutput
, reloc
);
4191 as_bad (_("undefined symbol for opcode \"%s\""),
4192 xtensa_opcode_name (xtensa_default_isa
, opcode
));
4196 fmt_length
= xtensa_format_length (xtensa_default_isa
, fmt
);
4197 the_fix
= fix_new_exp (fragP
, offset
, fmt_length
, expr
,
4198 howto
->pc_relative
, reloc
);
4199 the_fix
->fx_no_overflow
= 1;
4200 the_fix
->tc_fix_data
.X_add_symbol
= expr
->X_add_symbol
;
4201 the_fix
->tc_fix_data
.X_add_number
= expr
->X_add_number
;
4202 the_fix
->tc_fix_data
.slot
= slot
;
4209 xg_emit_insn_to_buf (TInsn
*tinsn
,
4213 bfd_boolean build_fix
)
4215 static xtensa_insnbuf insnbuf
= NULL
;
4216 bfd_boolean has_symbolic_immed
= FALSE
;
4217 bfd_boolean ok
= TRUE
;
4220 insnbuf
= xtensa_insnbuf_alloc (xtensa_default_isa
);
4222 has_symbolic_immed
= tinsn_to_insnbuf (tinsn
, insnbuf
);
4223 if (has_symbolic_immed
&& build_fix
)
4226 xtensa_format fmt
= xg_get_single_format (tinsn
->opcode
);
4227 int slot
= xg_get_single_slot (tinsn
->opcode
);
4228 int opnum
= get_relaxable_immed (tinsn
->opcode
);
4229 expressionS
*exp
= &tinsn
->tok
[opnum
];
4231 if (!xg_add_opcode_fix (tinsn
, opnum
, fmt
, slot
, exp
, fragP
, offset
))
4234 fragP
->tc_frag_data
.is_insn
= TRUE
;
4235 xtensa_insnbuf_to_chars (xtensa_default_isa
, insnbuf
,
4236 (unsigned char *) buf
, 0);
4242 xg_resolve_literals (TInsn
*insn
, symbolS
*lit_sym
)
4244 symbolS
*sym
= get_special_literal_symbol ();
4248 assert (insn
->insn_type
== ITYPE_INSN
);
4249 for (i
= 0; i
< insn
->ntok
; i
++)
4250 if (insn
->tok
[i
].X_add_symbol
== sym
)
4251 insn
->tok
[i
].X_add_symbol
= lit_sym
;
4257 xg_resolve_labels (TInsn
*insn
, symbolS
*label_sym
)
4259 symbolS
*sym
= get_special_label_symbol ();
4261 for (i
= 0; i
< insn
->ntok
; i
++)
4262 if (insn
->tok
[i
].X_add_symbol
== sym
)
4263 insn
->tok
[i
].X_add_symbol
= label_sym
;
4268 /* Return TRUE if the instruction can write to the specified
4269 integer register. */
4272 is_register_writer (const TInsn
*insn
, const char *regset
, int regnum
)
4276 xtensa_isa isa
= xtensa_default_isa
;
4278 num_ops
= xtensa_opcode_num_operands (isa
, insn
->opcode
);
4280 for (i
= 0; i
< num_ops
; i
++)
4283 inout
= xtensa_operand_inout (isa
, insn
->opcode
, i
);
4284 if ((inout
== 'o' || inout
== 'm')
4285 && xtensa_operand_is_register (isa
, insn
->opcode
, i
) == 1)
4287 xtensa_regfile opnd_rf
=
4288 xtensa_operand_regfile (isa
, insn
->opcode
, i
);
4289 if (!strcmp (xtensa_regfile_shortname (isa
, opnd_rf
), regset
))
4291 if ((insn
->tok
[i
].X_op
== O_register
)
4292 && (insn
->tok
[i
].X_add_number
== regnum
))
4302 is_bad_loopend_opcode (const TInsn
*tinsn
)
4304 xtensa_opcode opcode
= tinsn
->opcode
;
4306 if (opcode
== XTENSA_UNDEFINED
)
4309 if (opcode
== xtensa_call0_opcode
4310 || opcode
== xtensa_callx0_opcode
4311 || opcode
== xtensa_call4_opcode
4312 || opcode
== xtensa_callx4_opcode
4313 || opcode
== xtensa_call8_opcode
4314 || opcode
== xtensa_callx8_opcode
4315 || opcode
== xtensa_call12_opcode
4316 || opcode
== xtensa_callx12_opcode
4317 || opcode
== xtensa_isync_opcode
4318 || opcode
== xtensa_ret_opcode
4319 || opcode
== xtensa_ret_n_opcode
4320 || opcode
== xtensa_retw_opcode
4321 || opcode
== xtensa_retw_n_opcode
4322 || opcode
== xtensa_waiti_opcode
4323 || opcode
== xtensa_rsr_lcount_opcode
)
4330 /* Labels that begin with ".Ln" or ".LM" are unaligned.
4331 This allows the debugger to add unaligned labels.
4332 Also, the assembler generates stabs labels that need
4333 not be aligned: FAKE_LABEL_NAME . {"F", "L", "endfunc"}. */
4336 is_unaligned_label (symbolS
*sym
)
4338 const char *name
= S_GET_NAME (sym
);
4339 static size_t fake_size
= 0;
4343 && name
[1] == 'L' && (name
[2] == 'n' || name
[2] == 'M'))
4346 /* FAKE_LABEL_NAME followed by "F", "L" or "endfunc" */
4348 fake_size
= strlen (FAKE_LABEL_NAME
);
4351 && strncmp (FAKE_LABEL_NAME
, name
, fake_size
) == 0
4352 && (name
[fake_size
] == 'F'
4353 || name
[fake_size
] == 'L'
4354 || (name
[fake_size
] == 'e'
4355 && strncmp ("endfunc", name
+fake_size
, 7) == 0)))
4363 next_non_empty_frag (const fragS
*fragP
)
4365 fragS
*next_fragP
= fragP
->fr_next
;
4367 /* Sometimes an empty will end up here due storage allocation issues.
4368 So we have to skip until we find something legit. */
4369 while (next_fragP
&& next_fragP
->fr_fix
== 0)
4370 next_fragP
= next_fragP
->fr_next
;
4372 if (next_fragP
== NULL
|| next_fragP
->fr_fix
== 0)
4380 next_frag_opcode_is_loop (const fragS
*fragP
, xtensa_opcode
*opcode
)
4382 xtensa_opcode out_opcode
;
4383 const fragS
*next_fragP
= next_non_empty_frag (fragP
);
4385 if (next_fragP
== NULL
)
4388 out_opcode
= get_opcode_from_buf (next_fragP
->fr_literal
, 0);
4389 if (xtensa_opcode_is_loop (xtensa_default_isa
, out_opcode
) == 1)
4391 *opcode
= out_opcode
;
4399 frag_format_size (const fragS
*fragP
)
4401 static xtensa_insnbuf insnbuf
= NULL
;
4402 xtensa_isa isa
= xtensa_default_isa
;
4407 insnbuf
= xtensa_insnbuf_alloc (isa
);
4410 return XTENSA_UNDEFINED
;
4412 xtensa_insnbuf_from_chars (isa
, insnbuf
,
4413 (unsigned char *) fragP
->fr_literal
, 0);
4415 fmt
= xtensa_format_decode (isa
, insnbuf
);
4416 if (fmt
== XTENSA_UNDEFINED
)
4417 return XTENSA_UNDEFINED
;
4418 fmt_size
= xtensa_format_length (isa
, fmt
);
4420 /* If the next format won't be changing due to relaxation, just
4421 return the length of the first format. */
4422 if (fragP
->fr_opcode
!= fragP
->fr_literal
)
4425 /* If during relaxation we have to pull an instruction out of a
4426 multi-slot instruction, we will return the more conservative
4427 number. This works because alignment on bigger instructions
4428 is more restrictive than alignment on smaller instructions.
4429 This is more conservative than we would like, but it happens
4432 if (xtensa_format_num_slots (xtensa_default_isa
, fmt
) > 1)
4435 /* If we aren't doing one of our own relaxations or it isn't
4436 slot-based, then the insn size won't change. */
4437 if (fragP
->fr_type
!= rs_machine_dependent
)
4439 if (fragP
->fr_subtype
!= RELAX_SLOTS
)
4442 /* If an instruction is about to grow, return the longer size. */
4443 if (fragP
->tc_frag_data
.slot_subtypes
[0] == RELAX_IMMED_STEP1
4444 || fragP
->tc_frag_data
.slot_subtypes
[0] == RELAX_IMMED_STEP2
4445 || fragP
->tc_frag_data
.slot_subtypes
[0] == RELAX_IMMED_STEP3
)
4447 /* For most frags at RELAX_IMMED_STEPX, with X > 0, the first
4448 instruction in the relaxed version is of length 3. (The case
4449 where we have to pull the instruction out of a FLIX bundle
4450 is handled conservatively above.) However, frags with opcodes
4451 that are expanding to wide branches end up having formats that
4452 are not determinable by the RELAX_IMMED_STEPX enumeration, and
4453 we can't tell directly what format the relaxer picked. This
4454 is a wart in the design of the relaxer that should someday be
4455 fixed, but would require major changes, or at least should
4456 be accompanied by major changes to make use of that data.
4458 In any event, we can tell that we are expanding from a single-slot
4459 three-byte format to a wider one with the logic below. */
4461 if (fmt_size
<= 3 && fragP
->tc_frag_data
.text_expansion
[0] != 3)
4462 return 3 + fragP
->tc_frag_data
.text_expansion
[0];
4467 if (fragP
->tc_frag_data
.slot_subtypes
[0] == RELAX_NARROW
)
4468 return 2 + fragP
->tc_frag_data
.text_expansion
[0];
4475 next_frag_format_size (const fragS
*fragP
)
4477 const fragS
*next_fragP
= next_non_empty_frag (fragP
);
4478 return frag_format_size (next_fragP
);
4482 /* In early Xtensa Processors, for reasons that are unclear, the ISA
4483 required two-byte instructions to be treated as three-byte instructions
4484 for loop instruction alignment. This restriction was removed beginning
4485 with Xtensa LX. Now the only requirement on loop instruction alignment
4486 is that the first instruction of the loop must appear at an address that
4487 does not cross a fetch boundary. */
4490 get_loop_align_size (int insn_size
)
4492 if (insn_size
== XTENSA_UNDEFINED
)
4493 return xtensa_fetch_width
;
4495 if (enforce_three_byte_loop_align
&& insn_size
== 2)
4502 /* If the next legit fragment is an end-of-loop marker,
4503 switch its state so it will instantiate a NOP. */
4506 update_next_frag_state (fragS
*fragP
)
4508 fragS
*next_fragP
= fragP
->fr_next
;
4509 fragS
*new_target
= NULL
;
4513 /* We are guaranteed there will be one of these... */
4514 while (!(next_fragP
->fr_type
== rs_machine_dependent
4515 && (next_fragP
->fr_subtype
== RELAX_MAYBE_UNREACHABLE
4516 || next_fragP
->fr_subtype
== RELAX_UNREACHABLE
)))
4517 next_fragP
= next_fragP
->fr_next
;
4519 assert (next_fragP
->fr_type
== rs_machine_dependent
4520 && (next_fragP
->fr_subtype
== RELAX_MAYBE_UNREACHABLE
4521 || next_fragP
->fr_subtype
== RELAX_UNREACHABLE
));
4523 /* ...and one of these. */
4524 new_target
= next_fragP
->fr_next
;
4525 while (!(new_target
->fr_type
== rs_machine_dependent
4526 && (new_target
->fr_subtype
== RELAX_MAYBE_DESIRE_ALIGN
4527 || new_target
->fr_subtype
== RELAX_DESIRE_ALIGN
)))
4528 new_target
= new_target
->fr_next
;
4530 assert (new_target
->fr_type
== rs_machine_dependent
4531 && (new_target
->fr_subtype
== RELAX_MAYBE_DESIRE_ALIGN
4532 || new_target
->fr_subtype
== RELAX_DESIRE_ALIGN
));
4535 while (next_fragP
&& next_fragP
->fr_fix
== 0)
4537 if (next_fragP
->fr_type
== rs_machine_dependent
4538 && next_fragP
->fr_subtype
== RELAX_LOOP_END
)
4540 next_fragP
->fr_subtype
= RELAX_LOOP_END_ADD_NOP
;
4544 next_fragP
= next_fragP
->fr_next
;
4550 next_frag_is_branch_target (const fragS
*fragP
)
4552 /* Sometimes an empty will end up here due to storage allocation issues,
4553 so we have to skip until we find something legit. */
4554 for (fragP
= fragP
->fr_next
; fragP
; fragP
= fragP
->fr_next
)
4556 if (fragP
->tc_frag_data
.is_branch_target
)
4558 if (fragP
->fr_fix
!= 0)
4566 next_frag_is_loop_target (const fragS
*fragP
)
4568 /* Sometimes an empty will end up here due storage allocation issues.
4569 So we have to skip until we find something legit. */
4570 for (fragP
= fragP
->fr_next
; fragP
; fragP
= fragP
->fr_next
)
4572 if (fragP
->tc_frag_data
.is_loop_target
)
4574 if (fragP
->fr_fix
!= 0)
4582 next_frag_pre_opcode_bytes (const fragS
*fragp
)
4584 const fragS
*next_fragp
= fragp
->fr_next
;
4585 xtensa_opcode next_opcode
;
4587 if (!next_frag_opcode_is_loop (fragp
, &next_opcode
))
4590 /* Sometimes an empty will end up here due to storage allocation issues,
4591 so we have to skip until we find something legit. */
4592 while (next_fragp
->fr_fix
== 0)
4593 next_fragp
= next_fragp
->fr_next
;
4595 if (next_fragp
->fr_type
!= rs_machine_dependent
)
4598 /* There is some implicit knowledge encoded in here.
4599 The LOOP instructions that are NOT RELAX_IMMED have
4600 been relaxed. Note that we can assume that the LOOP
4601 instruction is in slot 0 because loops aren't bundleable. */
4602 if (next_fragp
->tc_frag_data
.slot_subtypes
[0] > RELAX_IMMED
)
4603 return get_expanded_loop_offset (next_opcode
);
4609 /* Mark a location where we can later insert literal frags. Update
4610 the section's literal_pool_loc, so subsequent literals can be
4611 placed nearest to their use. */
4614 xtensa_mark_literal_pool_location (void)
4616 /* Any labels pointing to the current location need
4617 to be adjusted to after the literal pool. */
4619 fragS
*pool_location
;
4621 if (use_literal_section
)
4624 /* We stash info in these frags so we can later move the literal's
4625 fixes into this frchain's fix list. */
4626 pool_location
= frag_now
;
4627 frag_now
->tc_frag_data
.lit_frchain
= frchain_now
;
4628 frag_now
->tc_frag_data
.literal_frag
= frag_now
;
4629 frag_variant (rs_machine_dependent
, 0, 0,
4630 RELAX_LITERAL_POOL_BEGIN
, NULL
, 0, NULL
);
4631 xtensa_set_frag_assembly_state (frag_now
);
4632 frag_now
->tc_frag_data
.lit_seg
= now_seg
;
4633 frag_variant (rs_machine_dependent
, 0, 0,
4634 RELAX_LITERAL_POOL_END
, NULL
, 0, NULL
);
4635 xtensa_set_frag_assembly_state (frag_now
);
4637 /* Now put a frag into the literal pool that points to this location. */
4638 set_literal_pool_location (now_seg
, pool_location
);
4639 xtensa_switch_to_non_abs_literal_fragment (&s
);
4640 frag_align (2, 0, 0);
4641 record_alignment (now_seg
, 2);
4643 /* Close whatever frag is there. */
4644 frag_variant (rs_fill
, 0, 0, 0, NULL
, 0, NULL
);
4645 xtensa_set_frag_assembly_state (frag_now
);
4646 frag_now
->tc_frag_data
.literal_frag
= pool_location
;
4647 frag_variant (rs_fill
, 0, 0, 0, NULL
, 0, NULL
);
4648 xtensa_restore_emit_state (&s
);
4649 xtensa_set_frag_assembly_state (frag_now
);
4653 /* Build a nop of the correct size into tinsn. */
4656 build_nop (TInsn
*tinsn
, int size
)
4662 tinsn
->opcode
= xtensa_nop_n_opcode
;
4664 if (tinsn
->opcode
== XTENSA_UNDEFINED
)
4665 as_fatal (_("opcode 'NOP.N' unavailable in this configuration"));
4669 if (xtensa_nop_opcode
== XTENSA_UNDEFINED
)
4671 tinsn
->opcode
= xtensa_or_opcode
;
4672 set_expr_const (&tinsn
->tok
[0], 1);
4673 set_expr_const (&tinsn
->tok
[1], 1);
4674 set_expr_const (&tinsn
->tok
[2], 1);
4678 tinsn
->opcode
= xtensa_nop_opcode
;
4680 assert (tinsn
->opcode
!= XTENSA_UNDEFINED
);
4685 /* Assemble a NOP of the requested size in the buffer. User must have
4686 allocated "buf" with at least "size" bytes. */
4689 assemble_nop (int size
, char *buf
)
4691 static xtensa_insnbuf insnbuf
= NULL
;
4694 build_nop (&tinsn
, size
);
4697 insnbuf
= xtensa_insnbuf_alloc (xtensa_default_isa
);
4699 tinsn_to_insnbuf (&tinsn
, insnbuf
);
4700 xtensa_insnbuf_to_chars (xtensa_default_isa
, insnbuf
,
4701 (unsigned char *) buf
, 0);
4705 /* Return the number of bytes for the offset of the expanded loop
4706 instruction. This should be incorporated into the relaxation
4707 specification but is hard-coded here. This is used to auto-align
4708 the loop instruction. It is invalid to call this function if the
4709 configuration does not have loops or if the opcode is not a loop
4713 get_expanded_loop_offset (xtensa_opcode opcode
)
4715 /* This is the OFFSET of the loop instruction in the expanded loop.
4716 This MUST correspond directly to the specification of the loop
4717 expansion. It will be validated on fragment conversion. */
4718 assert (opcode
!= XTENSA_UNDEFINED
);
4719 if (opcode
== xtensa_loop_opcode
)
4721 if (opcode
== xtensa_loopnez_opcode
)
4723 if (opcode
== xtensa_loopgtz_opcode
)
4725 as_fatal (_("get_expanded_loop_offset: invalid opcode"));
4731 get_literal_pool_location (segT seg
)
4733 return seg_info (seg
)->tc_segment_info_data
.literal_pool_loc
;
4738 set_literal_pool_location (segT seg
, fragS
*literal_pool_loc
)
4740 seg_info (seg
)->tc_segment_info_data
.literal_pool_loc
= literal_pool_loc
;
4744 /* Set frag assembly state should be called when a new frag is
4745 opened and after a frag has been closed. */
4748 xtensa_set_frag_assembly_state (fragS
*fragP
)
4750 if (!density_supported
)
4751 fragP
->tc_frag_data
.is_no_density
= TRUE
;
4753 /* This function is called from subsegs_finish, which is called
4754 after xtensa_end, so we can't use "use_transform" or
4755 "use_schedule" here. */
4756 if (!directive_state
[directive_transform
])
4757 fragP
->tc_frag_data
.is_no_transform
= TRUE
;
4758 if (directive_state
[directive_longcalls
])
4759 fragP
->tc_frag_data
.use_longcalls
= TRUE
;
4760 fragP
->tc_frag_data
.use_absolute_literals
=
4761 directive_state
[directive_absolute_literals
];
4762 fragP
->tc_frag_data
.is_assembly_state_set
= TRUE
;
4767 relaxable_section (asection
*sec
)
4769 return ((sec
->flags
& SEC_DEBUGGING
) == 0
4770 && strcmp (sec
->name
, ".eh_frame") != 0);
4775 xtensa_mark_frags_for_org (void)
4779 /* Walk over each fragment of all of the current segments. If we find
4780 a .org frag in any of the segments, mark all frags prior to it as
4781 "no transform", which will prevent linker optimizations from messing
4782 up the .org distance. This should be done after
4783 xtensa_find_unmarked_state_frags, because we don't want to worry here
4784 about that function trashing the data we save here. */
4786 for (seclist
= &stdoutput
->sections
;
4787 seclist
&& *seclist
;
4788 seclist
= &(*seclist
)->next
)
4790 segT sec
= *seclist
;
4791 segment_info_type
*seginfo
;
4794 flags
= bfd_get_section_flags (stdoutput
, sec
);
4795 if (flags
& SEC_DEBUGGING
)
4797 if (!(flags
& SEC_ALLOC
))
4800 seginfo
= seg_info (sec
);
4801 if (seginfo
&& seginfo
->frchainP
)
4803 fragS
*last_fragP
= seginfo
->frchainP
->frch_root
;
4804 for (fragP
= seginfo
->frchainP
->frch_root
; fragP
;
4805 fragP
= fragP
->fr_next
)
4807 /* cvt_frag_to_fill has changed the fr_type of org frags to
4808 rs_fill, so use the value as cached in rs_subtype here. */
4809 if (fragP
->fr_subtype
== RELAX_ORG
)
4811 while (last_fragP
!= fragP
->fr_next
)
4813 last_fragP
->tc_frag_data
.is_no_transform
= TRUE
;
4814 last_fragP
= last_fragP
->fr_next
;
4824 xtensa_find_unmarked_state_frags (void)
4828 /* Walk over each fragment of all of the current segments. For each
4829 unmarked fragment, mark it with the same info as the previous
4831 for (seclist
= &stdoutput
->sections
;
4832 seclist
&& *seclist
;
4833 seclist
= &(*seclist
)->next
)
4835 segT sec
= *seclist
;
4836 segment_info_type
*seginfo
;
4839 flags
= bfd_get_section_flags (stdoutput
, sec
);
4840 if (flags
& SEC_DEBUGGING
)
4842 if (!(flags
& SEC_ALLOC
))
4845 seginfo
= seg_info (sec
);
4846 if (seginfo
&& seginfo
->frchainP
)
4848 fragS
*last_fragP
= 0;
4849 for (fragP
= seginfo
->frchainP
->frch_root
; fragP
;
4850 fragP
= fragP
->fr_next
)
4852 if (fragP
->fr_fix
!= 0
4853 && !fragP
->tc_frag_data
.is_assembly_state_set
)
4855 if (last_fragP
== 0)
4857 as_warn_where (fragP
->fr_file
, fragP
->fr_line
,
4858 _("assembly state not set for first frag in section %s"),
4863 fragP
->tc_frag_data
.is_assembly_state_set
= TRUE
;
4864 fragP
->tc_frag_data
.is_no_density
=
4865 last_fragP
->tc_frag_data
.is_no_density
;
4866 fragP
->tc_frag_data
.is_no_transform
=
4867 last_fragP
->tc_frag_data
.is_no_transform
;
4868 fragP
->tc_frag_data
.use_longcalls
=
4869 last_fragP
->tc_frag_data
.use_longcalls
;
4870 fragP
->tc_frag_data
.use_absolute_literals
=
4871 last_fragP
->tc_frag_data
.use_absolute_literals
;
4874 if (fragP
->tc_frag_data
.is_assembly_state_set
)
4883 xtensa_find_unaligned_branch_targets (bfd
*abfd ATTRIBUTE_UNUSED
,
4885 void *unused ATTRIBUTE_UNUSED
)
4887 flagword flags
= bfd_get_section_flags (abfd
, sec
);
4888 segment_info_type
*seginfo
= seg_info (sec
);
4889 fragS
*frag
= seginfo
->frchainP
->frch_root
;
4891 if (flags
& SEC_CODE
)
4893 xtensa_isa isa
= xtensa_default_isa
;
4894 xtensa_insnbuf insnbuf
= xtensa_insnbuf_alloc (isa
);
4895 while (frag
!= NULL
)
4897 if (frag
->tc_frag_data
.is_branch_target
)
4900 addressT branch_align
, frag_addr
;
4903 xtensa_insnbuf_from_chars
4904 (isa
, insnbuf
, (unsigned char *) frag
->fr_literal
, 0);
4905 fmt
= xtensa_format_decode (isa
, insnbuf
);
4906 op_size
= xtensa_format_length (isa
, fmt
);
4907 branch_align
= 1 << branch_align_power (sec
);
4908 frag_addr
= frag
->fr_address
% branch_align
;
4909 if (frag_addr
+ op_size
> branch_align
)
4910 as_warn_where (frag
->fr_file
, frag
->fr_line
,
4911 _("unaligned branch target: %d bytes at 0x%lx"),
4912 op_size
, (long) frag
->fr_address
);
4914 frag
= frag
->fr_next
;
4916 xtensa_insnbuf_free (isa
, insnbuf
);
4922 xtensa_find_unaligned_loops (bfd
*abfd ATTRIBUTE_UNUSED
,
4924 void *unused ATTRIBUTE_UNUSED
)
4926 flagword flags
= bfd_get_section_flags (abfd
, sec
);
4927 segment_info_type
*seginfo
= seg_info (sec
);
4928 fragS
*frag
= seginfo
->frchainP
->frch_root
;
4929 xtensa_isa isa
= xtensa_default_isa
;
4931 if (flags
& SEC_CODE
)
4933 xtensa_insnbuf insnbuf
= xtensa_insnbuf_alloc (isa
);
4934 while (frag
!= NULL
)
4936 if (frag
->tc_frag_data
.is_first_loop_insn
)
4942 xtensa_insnbuf_from_chars
4943 (isa
, insnbuf
, (unsigned char *) frag
->fr_literal
, 0);
4944 fmt
= xtensa_format_decode (isa
, insnbuf
);
4945 op_size
= xtensa_format_length (isa
, fmt
);
4946 frag_addr
= frag
->fr_address
% xtensa_fetch_width
;
4948 if (frag_addr
+ op_size
> xtensa_fetch_width
)
4949 as_warn_where (frag
->fr_file
, frag
->fr_line
,
4950 _("unaligned loop: %d bytes at 0x%lx"),
4951 op_size
, (long) frag
->fr_address
);
4953 frag
= frag
->fr_next
;
4955 xtensa_insnbuf_free (isa
, insnbuf
);
4961 xg_apply_fix_value (fixS
*fixP
, valueT val
)
4963 xtensa_isa isa
= xtensa_default_isa
;
4964 static xtensa_insnbuf insnbuf
= NULL
;
4965 static xtensa_insnbuf slotbuf
= NULL
;
4968 bfd_boolean alt_reloc
;
4969 xtensa_opcode opcode
;
4970 char *const fixpos
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
4972 (void) decode_reloc (fixP
->fx_r_type
, &slot
, &alt_reloc
);
4974 as_fatal (_("unexpected fix"));
4978 insnbuf
= xtensa_insnbuf_alloc (isa
);
4979 slotbuf
= xtensa_insnbuf_alloc (isa
);
4982 xtensa_insnbuf_from_chars (isa
, insnbuf
, (unsigned char *) fixpos
, 0);
4983 fmt
= xtensa_format_decode (isa
, insnbuf
);
4984 if (fmt
== XTENSA_UNDEFINED
)
4985 as_fatal (_("undecodable fix"));
4986 xtensa_format_get_slot (isa
, fmt
, slot
, insnbuf
, slotbuf
);
4987 opcode
= xtensa_opcode_decode (isa
, fmt
, slot
, slotbuf
);
4988 if (opcode
== XTENSA_UNDEFINED
)
4989 as_fatal (_("undecodable fix"));
4991 /* CONST16 immediates are not PC-relative, despite the fact that we
4992 reuse the normal PC-relative operand relocations for the low part
4993 of a CONST16 operand. */
4994 if (opcode
== xtensa_const16_opcode
)
4997 xtensa_insnbuf_set_operand (slotbuf
, fmt
, slot
, opcode
,
4998 get_relaxable_immed (opcode
), val
,
4999 fixP
->fx_file
, fixP
->fx_line
);
5001 xtensa_format_set_slot (isa
, fmt
, slot
, insnbuf
, slotbuf
);
5002 xtensa_insnbuf_to_chars (isa
, insnbuf
, (unsigned char *) fixpos
, 0);
5008 /* External Functions and Other GAS Hooks. */
5011 xtensa_target_format (void)
5013 return (target_big_endian
? "elf32-xtensa-be" : "elf32-xtensa-le");
5018 xtensa_file_arch_init (bfd
*abfd
)
5020 bfd_set_private_flags (abfd
, 0x100 | 0x200);
5025 md_number_to_chars (char *buf
, valueT val
, int n
)
5027 if (target_big_endian
)
5028 number_to_chars_bigendian (buf
, val
, n
);
5030 number_to_chars_littleendian (buf
, val
, n
);
5034 /* This function is called once, at assembler startup time. It should
5035 set up all the tables, etc. that the MD part of the assembler will
5041 segT current_section
= now_seg
;
5042 int current_subsec
= now_subseg
;
5045 xtensa_default_isa
= xtensa_isa_init (0, 0);
5046 isa
= xtensa_default_isa
;
5050 /* Set up the literal sections. */
5051 memset (&default_lit_sections
, 0, sizeof (default_lit_sections
));
5053 subseg_set (current_section
, current_subsec
);
5055 xg_init_vinsn (&cur_vinsn
);
5057 xtensa_addi_opcode
= xtensa_opcode_lookup (isa
, "addi");
5058 xtensa_addmi_opcode
= xtensa_opcode_lookup (isa
, "addmi");
5059 xtensa_call0_opcode
= xtensa_opcode_lookup (isa
, "call0");
5060 xtensa_call4_opcode
= xtensa_opcode_lookup (isa
, "call4");
5061 xtensa_call8_opcode
= xtensa_opcode_lookup (isa
, "call8");
5062 xtensa_call12_opcode
= xtensa_opcode_lookup (isa
, "call12");
5063 xtensa_callx0_opcode
= xtensa_opcode_lookup (isa
, "callx0");
5064 xtensa_callx4_opcode
= xtensa_opcode_lookup (isa
, "callx4");
5065 xtensa_callx8_opcode
= xtensa_opcode_lookup (isa
, "callx8");
5066 xtensa_callx12_opcode
= xtensa_opcode_lookup (isa
, "callx12");
5067 xtensa_const16_opcode
= xtensa_opcode_lookup (isa
, "const16");
5068 xtensa_entry_opcode
= xtensa_opcode_lookup (isa
, "entry");
5069 xtensa_extui_opcode
= xtensa_opcode_lookup (isa
, "extui");
5070 xtensa_movi_opcode
= xtensa_opcode_lookup (isa
, "movi");
5071 xtensa_movi_n_opcode
= xtensa_opcode_lookup (isa
, "movi.n");
5072 xtensa_isync_opcode
= xtensa_opcode_lookup (isa
, "isync");
5073 xtensa_jx_opcode
= xtensa_opcode_lookup (isa
, "jx");
5074 xtensa_l32r_opcode
= xtensa_opcode_lookup (isa
, "l32r");
5075 xtensa_loop_opcode
= xtensa_opcode_lookup (isa
, "loop");
5076 xtensa_loopnez_opcode
= xtensa_opcode_lookup (isa
, "loopnez");
5077 xtensa_loopgtz_opcode
= xtensa_opcode_lookup (isa
, "loopgtz");
5078 xtensa_nop_opcode
= xtensa_opcode_lookup (isa
, "nop");
5079 xtensa_nop_n_opcode
= xtensa_opcode_lookup (isa
, "nop.n");
5080 xtensa_or_opcode
= xtensa_opcode_lookup (isa
, "or");
5081 xtensa_ret_opcode
= xtensa_opcode_lookup (isa
, "ret");
5082 xtensa_ret_n_opcode
= xtensa_opcode_lookup (isa
, "ret.n");
5083 xtensa_retw_opcode
= xtensa_opcode_lookup (isa
, "retw");
5084 xtensa_retw_n_opcode
= xtensa_opcode_lookup (isa
, "retw.n");
5085 xtensa_rsr_lcount_opcode
= xtensa_opcode_lookup (isa
, "rsr.lcount");
5086 xtensa_waiti_opcode
= xtensa_opcode_lookup (isa
, "waiti");
5088 xtensa_num_pipe_stages
= xtensa_isa_num_pipe_stages (isa
);
5090 init_op_placement_info_table ();
5092 /* Set up the assembly state. */
5093 if (!frag_now
->tc_frag_data
.is_assembly_state_set
)
5094 xtensa_set_frag_assembly_state (frag_now
);
5098 /* TC_INIT_FIX_DATA hook */
5101 xtensa_init_fix_data (fixS
*x
)
5103 x
->tc_fix_data
.slot
= 0;
5104 x
->tc_fix_data
.X_add_symbol
= NULL
;
5105 x
->tc_fix_data
.X_add_number
= 0;
5109 /* tc_frob_label hook */
5112 xtensa_frob_label (symbolS
*sym
)
5116 if (cur_vinsn
.inside_bundle
)
5118 as_bad (_("labels are not valid inside bundles"));
5122 freq
= get_subseg_target_freq (now_seg
, now_subseg
);
5124 /* Since the label was already attached to a frag associated with the
5125 previous basic block, it now needs to be reset to the current frag. */
5126 symbol_set_frag (sym
, frag_now
);
5127 S_SET_VALUE (sym
, (valueT
) frag_now_fix ());
5129 if (generating_literals
)
5130 xtensa_add_literal_sym (sym
);
5132 xtensa_add_insn_label (sym
);
5134 if (symbol_get_tc (sym
)->is_loop_target
)
5136 if ((get_last_insn_flags (now_seg
, now_subseg
)
5137 & FLAG_IS_BAD_LOOPEND
) != 0)
5138 as_bad (_("invalid last instruction for a zero-overhead loop"));
5140 xtensa_set_frag_assembly_state (frag_now
);
5141 frag_var (rs_machine_dependent
, 4, 4, RELAX_LOOP_END
,
5142 frag_now
->fr_symbol
, frag_now
->fr_offset
, NULL
);
5144 xtensa_set_frag_assembly_state (frag_now
);
5145 xtensa_move_labels (frag_now
, 0);
5148 /* No target aligning in the absolute section. */
5149 if (now_seg
!= absolute_section
5150 && do_align_targets ()
5151 && !is_unaligned_label (sym
)
5152 && !generating_literals
)
5154 xtensa_set_frag_assembly_state (frag_now
);
5156 frag_var (rs_machine_dependent
,
5158 RELAX_DESIRE_ALIGN_IF_TARGET
,
5159 frag_now
->fr_symbol
, frag_now
->fr_offset
, NULL
);
5160 xtensa_set_frag_assembly_state (frag_now
);
5161 xtensa_move_labels (frag_now
, 0);
5164 /* We need to mark the following properties even if we aren't aligning. */
5166 /* If the label is already known to be a branch target, i.e., a
5167 forward branch, mark the frag accordingly. Backward branches
5168 are handled by xg_add_branch_and_loop_targets. */
5169 if (symbol_get_tc (sym
)->is_branch_target
)
5170 symbol_get_frag (sym
)->tc_frag_data
.is_branch_target
= TRUE
;
5172 /* Loops only go forward, so they can be identified here. */
5173 if (symbol_get_tc (sym
)->is_loop_target
)
5174 symbol_get_frag (sym
)->tc_frag_data
.is_loop_target
= TRUE
;
5176 dwarf2_emit_label (sym
);
5180 /* tc_unrecognized_line hook */
5183 xtensa_unrecognized_line (int ch
)
5188 if (cur_vinsn
.inside_bundle
== 0)
5190 /* PR8110: Cannot emit line number info inside a FLIX bundle
5191 when using --gstabs. Temporarily disable debug info. */
5192 generate_lineno_debug ();
5193 if (debug_type
== DEBUG_STABS
)
5195 xt_saved_debug_type
= debug_type
;
5196 debug_type
= DEBUG_NONE
;
5199 cur_vinsn
.inside_bundle
= 1;
5203 as_bad (_("extra opening brace"));
5209 if (cur_vinsn
.inside_bundle
)
5210 finish_vinsn (&cur_vinsn
);
5213 as_bad (_("extra closing brace"));
5218 as_bad (_("syntax error"));
5225 /* md_flush_pending_output hook */
5228 xtensa_flush_pending_output (void)
5230 /* This line fixes a bug where automatically generated gstabs info
5231 separates a function label from its entry instruction, ending up
5232 with the literal position between the function label and the entry
5233 instruction and crashing code. It only happens with --gstabs and
5234 --text-section-literals, and when several other obscure relaxation
5235 conditions are met. */
5236 if (outputting_stabs_line_debug
)
5239 if (cur_vinsn
.inside_bundle
)
5240 as_bad (_("missing closing brace"));
5242 /* If there is a non-zero instruction fragment, close it. */
5243 if (frag_now_fix () != 0 && frag_now
->tc_frag_data
.is_insn
)
5245 frag_wane (frag_now
);
5247 xtensa_set_frag_assembly_state (frag_now
);
5249 frag_now
->tc_frag_data
.is_insn
= FALSE
;
5251 xtensa_clear_insn_labels ();
5255 /* We had an error while parsing an instruction. The string might look
5256 like this: "insn arg1, arg2 }". If so, we need to see the closing
5257 brace and reset some fields. Otherwise, the vinsn never gets closed
5258 and the num_slots field will grow past the end of the array of slots,
5259 and bad things happen. */
5262 error_reset_cur_vinsn (void)
5264 if (cur_vinsn
.inside_bundle
)
5266 if (*input_line_pointer
== '}'
5267 || *(input_line_pointer
- 1) == '}'
5268 || *(input_line_pointer
- 2) == '}')
5269 xg_clear_vinsn (&cur_vinsn
);
5275 md_assemble (char *str
)
5277 xtensa_isa isa
= xtensa_default_isa
;
5280 bfd_boolean has_underbar
= FALSE
;
5281 char *arg_strings
[MAX_INSN_ARGS
];
5283 TInsn orig_insn
; /* Original instruction from the input. */
5285 tinsn_init (&orig_insn
);
5287 /* Split off the opcode. */
5288 opnamelen
= strspn (str
, "abcdefghijklmnopqrstuvwxyz_/0123456789.");
5289 opname
= xmalloc (opnamelen
+ 1);
5290 memcpy (opname
, str
, opnamelen
);
5291 opname
[opnamelen
] = '\0';
5293 num_args
= tokenize_arguments (arg_strings
, str
+ opnamelen
);
5296 as_bad (_("syntax error"));
5300 if (xg_translate_idioms (&opname
, &num_args
, arg_strings
))
5303 /* Check for an underbar prefix. */
5306 has_underbar
= TRUE
;
5310 orig_insn
.insn_type
= ITYPE_INSN
;
5312 orig_insn
.is_specific_opcode
= (has_underbar
|| !use_transform ());
5314 orig_insn
.opcode
= xtensa_opcode_lookup (isa
, opname
);
5315 if (orig_insn
.opcode
== XTENSA_UNDEFINED
)
5317 xtensa_format fmt
= xtensa_format_lookup (isa
, opname
);
5318 if (fmt
== XTENSA_UNDEFINED
)
5320 as_bad (_("unknown opcode or format name '%s'"), opname
);
5321 error_reset_cur_vinsn ();
5324 if (!cur_vinsn
.inside_bundle
)
5326 as_bad (_("format names only valid inside bundles"));
5327 error_reset_cur_vinsn ();
5330 if (cur_vinsn
.format
!= XTENSA_UNDEFINED
)
5331 as_warn (_("multiple formats specified for one bundle; using '%s'"),
5333 cur_vinsn
.format
= fmt
;
5334 free (has_underbar
? opname
- 1 : opname
);
5335 error_reset_cur_vinsn ();
5339 /* Parse the arguments. */
5340 if (parse_arguments (&orig_insn
, num_args
, arg_strings
))
5342 as_bad (_("syntax error"));
5343 error_reset_cur_vinsn ();
5347 /* Free the opcode and argument strings, now that they've been parsed. */
5348 free (has_underbar
? opname
- 1 : opname
);
5350 while (num_args
-- > 0)
5351 free (arg_strings
[num_args
]);
5353 /* Get expressions for invisible operands. */
5354 if (get_invisible_operands (&orig_insn
))
5356 error_reset_cur_vinsn ();
5360 /* Check for the right number and type of arguments. */
5361 if (tinsn_check_arguments (&orig_insn
))
5363 error_reset_cur_vinsn ();
5367 /* Record the line number for each TInsn, because a FLIX bundle may be
5368 spread across multiple input lines and individual instructions may be
5369 moved around in some cases. */
5370 orig_insn
.loc_directive_seen
= dwarf2_loc_directive_seen
;
5371 dwarf2_where (&orig_insn
.debug_line
);
5372 dwarf2_consume_line_info ();
5374 xg_add_branch_and_loop_targets (&orig_insn
);
5376 /* Check that immediate value for ENTRY is >= 16. */
5377 if (orig_insn
.opcode
== xtensa_entry_opcode
&& orig_insn
.ntok
>= 3)
5379 expressionS
*exp
= &orig_insn
.tok
[2];
5380 if (exp
->X_op
== O_constant
&& exp
->X_add_number
< 16)
5381 as_warn (_("entry instruction with stack decrement < 16"));
5385 assemble_tokens (opcode, tok, ntok);
5386 expand the tokens from the orig_insn into the
5387 stack of instructions that will not expand
5388 unless required at relaxation time. */
5390 if (!cur_vinsn
.inside_bundle
)
5391 emit_single_op (&orig_insn
);
5392 else /* We are inside a bundle. */
5394 cur_vinsn
.slots
[cur_vinsn
.num_slots
] = orig_insn
;
5395 cur_vinsn
.num_slots
++;
5396 if (*input_line_pointer
== '}'
5397 || *(input_line_pointer
- 1) == '}'
5398 || *(input_line_pointer
- 2) == '}')
5399 finish_vinsn (&cur_vinsn
);
5402 /* We've just emitted a new instruction so clear the list of labels. */
5403 xtensa_clear_insn_labels ();
5407 /* HANDLE_ALIGN hook */
5409 /* For a .align directive, we mark the previous block with the alignment
5410 information. This will be placed in the object file in the
5411 property section corresponding to this section. */
5414 xtensa_handle_align (fragS
*fragP
)
5417 && ! fragP
->tc_frag_data
.is_literal
5418 && (fragP
->fr_type
== rs_align
5419 || fragP
->fr_type
== rs_align_code
)
5420 && fragP
->fr_address
+ fragP
->fr_fix
> 0
5421 && fragP
->fr_offset
> 0
5422 && now_seg
!= bss_section
)
5424 fragP
->tc_frag_data
.is_align
= TRUE
;
5425 fragP
->tc_frag_data
.alignment
= fragP
->fr_offset
;
5428 if (fragP
->fr_type
== rs_align_test
)
5431 count
= fragP
->fr_next
->fr_address
- fragP
->fr_address
- fragP
->fr_fix
;
5433 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
5434 _("unaligned entry instruction"));
5437 if (linkrelax
&& fragP
->fr_type
== rs_org
)
5438 fragP
->fr_subtype
= RELAX_ORG
;
5442 /* TC_FRAG_INIT hook */
5445 xtensa_frag_init (fragS
*frag
)
5447 xtensa_set_frag_assembly_state (frag
);
5452 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
5458 /* Round up a section size to the appropriate boundary. */
5461 md_section_align (segT segment ATTRIBUTE_UNUSED
, valueT size
)
5463 return size
; /* Byte alignment is fine. */
5468 md_pcrel_from (fixS
*fixP
)
5471 static xtensa_insnbuf insnbuf
= NULL
;
5472 static xtensa_insnbuf slotbuf
= NULL
;
5475 xtensa_opcode opcode
;
5478 xtensa_isa isa
= xtensa_default_isa
;
5479 valueT addr
= fixP
->fx_where
+ fixP
->fx_frag
->fr_address
;
5480 bfd_boolean alt_reloc
;
5482 if (fixP
->fx_r_type
== BFD_RELOC_XTENSA_ASM_EXPAND
)
5485 if (fixP
->fx_r_type
== BFD_RELOC_32_PCREL
)
5490 insnbuf
= xtensa_insnbuf_alloc (isa
);
5491 slotbuf
= xtensa_insnbuf_alloc (isa
);
5494 insn_p
= &fixP
->fx_frag
->fr_literal
[fixP
->fx_where
];
5495 xtensa_insnbuf_from_chars (isa
, insnbuf
, (unsigned char *) insn_p
, 0);
5496 fmt
= xtensa_format_decode (isa
, insnbuf
);
5498 if (fmt
== XTENSA_UNDEFINED
)
5499 as_fatal (_("bad instruction format"));
5501 if (decode_reloc (fixP
->fx_r_type
, &slot
, &alt_reloc
) != 0)
5502 as_fatal (_("invalid relocation"));
5504 xtensa_format_get_slot (isa
, fmt
, slot
, insnbuf
, slotbuf
);
5505 opcode
= xtensa_opcode_decode (isa
, fmt
, slot
, slotbuf
);
5507 /* Check for "alternate" relocations (operand not specified). None
5508 of the current uses for these are really PC-relative. */
5509 if (alt_reloc
|| opcode
== xtensa_const16_opcode
)
5511 if (opcode
!= xtensa_l32r_opcode
5512 && opcode
!= xtensa_const16_opcode
)
5513 as_fatal (_("invalid relocation for '%s' instruction"),
5514 xtensa_opcode_name (isa
, opcode
));
5518 opnum
= get_relaxable_immed (opcode
);
5520 if (xtensa_operand_is_PCrelative (isa
, opcode
, opnum
) != 1
5521 || xtensa_operand_do_reloc (isa
, opcode
, opnum
, &opnd_value
, addr
))
5523 as_bad_where (fixP
->fx_file
,
5525 _("invalid relocation for operand %d of '%s'"),
5526 opnum
, xtensa_opcode_name (isa
, opcode
));
5529 return 0 - opnd_value
;
5533 /* TC_FORCE_RELOCATION hook */
5536 xtensa_force_relocation (fixS
*fix
)
5538 switch (fix
->fx_r_type
)
5540 case BFD_RELOC_XTENSA_ASM_EXPAND
:
5541 case BFD_RELOC_XTENSA_SLOT0_ALT
:
5542 case BFD_RELOC_XTENSA_SLOT1_ALT
:
5543 case BFD_RELOC_XTENSA_SLOT2_ALT
:
5544 case BFD_RELOC_XTENSA_SLOT3_ALT
:
5545 case BFD_RELOC_XTENSA_SLOT4_ALT
:
5546 case BFD_RELOC_XTENSA_SLOT5_ALT
:
5547 case BFD_RELOC_XTENSA_SLOT6_ALT
:
5548 case BFD_RELOC_XTENSA_SLOT7_ALT
:
5549 case BFD_RELOC_XTENSA_SLOT8_ALT
:
5550 case BFD_RELOC_XTENSA_SLOT9_ALT
:
5551 case BFD_RELOC_XTENSA_SLOT10_ALT
:
5552 case BFD_RELOC_XTENSA_SLOT11_ALT
:
5553 case BFD_RELOC_XTENSA_SLOT12_ALT
:
5554 case BFD_RELOC_XTENSA_SLOT13_ALT
:
5555 case BFD_RELOC_XTENSA_SLOT14_ALT
:
5561 if (linkrelax
&& fix
->fx_addsy
5562 && relaxable_section (S_GET_SEGMENT (fix
->fx_addsy
)))
5565 return generic_force_reloc (fix
);
5569 /* TC_VALIDATE_FIX_SUB hook */
5572 xtensa_validate_fix_sub (fixS
*fix
)
5574 segT add_symbol_segment
, sub_symbol_segment
;
5576 /* The difference of two symbols should be resolved by the assembler when
5577 linkrelax is not set. If the linker may relax the section containing
5578 the symbols, then an Xtensa DIFF relocation must be generated so that
5579 the linker knows to adjust the difference value. */
5580 if (!linkrelax
|| fix
->fx_addsy
== NULL
)
5583 /* Make sure both symbols are in the same segment, and that segment is
5584 "normal" and relaxable. If the segment is not "normal", then the
5585 fix is not valid. If the segment is not "relaxable", then the fix
5586 should have been handled earlier. */
5587 add_symbol_segment
= S_GET_SEGMENT (fix
->fx_addsy
);
5588 if (! SEG_NORMAL (add_symbol_segment
) ||
5589 ! relaxable_section (add_symbol_segment
))
5591 sub_symbol_segment
= S_GET_SEGMENT (fix
->fx_subsy
);
5592 return (sub_symbol_segment
== add_symbol_segment
);
5596 /* NO_PSEUDO_DOT hook */
5598 /* This function has nothing to do with pseudo dots, but this is the
5599 nearest macro to where the check needs to take place. FIXME: This
5603 xtensa_check_inside_bundle (void)
5605 if (cur_vinsn
.inside_bundle
&& input_line_pointer
[-1] == '.')
5606 as_bad (_("directives are not valid inside bundles"));
5608 /* This function must always return FALSE because it is called via a
5609 macro that has nothing to do with bundling. */
5614 /* md_elf_section_change_hook */
5617 xtensa_elf_section_change_hook (void)
5619 /* Set up the assembly state. */
5620 if (!frag_now
->tc_frag_data
.is_assembly_state_set
)
5621 xtensa_set_frag_assembly_state (frag_now
);
5625 /* tc_fix_adjustable hook */
5628 xtensa_fix_adjustable (fixS
*fixP
)
5630 /* An offset is not allowed in combination with the difference of two
5631 symbols, but that cannot be easily detected after a local symbol
5632 has been adjusted to a (section+offset) form. Return 0 so that such
5633 an fix will not be adjusted. */
5634 if (fixP
->fx_subsy
&& fixP
->fx_addsy
&& fixP
->fx_offset
5635 && relaxable_section (S_GET_SEGMENT (fixP
->fx_subsy
)))
5638 /* We need the symbol name for the VTABLE entries. */
5639 if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
5640 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
5647 /* tc_symbol_new_hook */
5649 symbolS
*expr_symbols
= NULL
;
5652 xtensa_symbol_new_hook (symbolS
*sym
)
5654 if (is_leb128_expr
&& S_GET_SEGMENT (sym
) == expr_section
)
5656 symbol_get_tc (sym
)->next_expr_symbol
= expr_symbols
;
5663 md_apply_fix (fixS
*fixP
, valueT
*valP
, segT seg
)
5665 char *const fixpos
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
5668 /* Subtracted symbols are only allowed for a few relocation types, and
5669 unless linkrelax is enabled, they should not make it to this point. */
5670 if (fixP
->fx_subsy
&& !(linkrelax
&& (fixP
->fx_r_type
== BFD_RELOC_32
5671 || fixP
->fx_r_type
== BFD_RELOC_16
5672 || fixP
->fx_r_type
== BFD_RELOC_8
)))
5673 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("expression too complex"));
5675 switch (fixP
->fx_r_type
)
5677 case BFD_RELOC_32_PCREL
:
5683 switch (fixP
->fx_r_type
)
5686 fixP
->fx_r_type
= BFD_RELOC_XTENSA_DIFF8
;
5689 fixP
->fx_r_type
= BFD_RELOC_XTENSA_DIFF16
;
5692 fixP
->fx_r_type
= BFD_RELOC_XTENSA_DIFF32
;
5698 /* An offset is only allowed when it results from adjusting a
5699 local symbol into a section-relative offset. If the offset
5700 came from the original expression, tc_fix_adjustable will have
5701 prevented the fix from being converted to a section-relative
5702 form so that we can flag the error here. */
5703 if (fixP
->fx_offset
!= 0 && !symbol_section_p (fixP
->fx_addsy
))
5704 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
5705 _("cannot represent subtraction with an offset"));
5707 val
= (S_GET_VALUE (fixP
->fx_addsy
) + fixP
->fx_offset
5708 - S_GET_VALUE (fixP
->fx_subsy
));
5710 /* The difference value gets written out, and the DIFF reloc
5711 identifies the address of the subtracted symbol (i.e., the one
5712 with the lowest address). */
5714 fixP
->fx_offset
-= val
;
5715 fixP
->fx_subsy
= NULL
;
5717 else if (! fixP
->fx_addsy
)
5724 case BFD_RELOC_XTENSA_PLT
:
5725 md_number_to_chars (fixpos
, val
, fixP
->fx_size
);
5726 fixP
->fx_no_overflow
= 0; /* Use the standard overflow check. */
5729 case BFD_RELOC_XTENSA_SLOT0_OP
:
5730 case BFD_RELOC_XTENSA_SLOT1_OP
:
5731 case BFD_RELOC_XTENSA_SLOT2_OP
:
5732 case BFD_RELOC_XTENSA_SLOT3_OP
:
5733 case BFD_RELOC_XTENSA_SLOT4_OP
:
5734 case BFD_RELOC_XTENSA_SLOT5_OP
:
5735 case BFD_RELOC_XTENSA_SLOT6_OP
:
5736 case BFD_RELOC_XTENSA_SLOT7_OP
:
5737 case BFD_RELOC_XTENSA_SLOT8_OP
:
5738 case BFD_RELOC_XTENSA_SLOT9_OP
:
5739 case BFD_RELOC_XTENSA_SLOT10_OP
:
5740 case BFD_RELOC_XTENSA_SLOT11_OP
:
5741 case BFD_RELOC_XTENSA_SLOT12_OP
:
5742 case BFD_RELOC_XTENSA_SLOT13_OP
:
5743 case BFD_RELOC_XTENSA_SLOT14_OP
:
5746 /* Write the tentative value of a PC-relative relocation to a
5747 local symbol into the instruction. The value will be ignored
5748 by the linker, and it makes the object file disassembly
5749 readable when all branch targets are encoded in relocations. */
5751 assert (fixP
->fx_addsy
);
5752 if (S_GET_SEGMENT (fixP
->fx_addsy
) == seg
5753 && !S_FORCE_RELOC (fixP
->fx_addsy
, 1))
5755 val
= (S_GET_VALUE (fixP
->fx_addsy
) + fixP
->fx_offset
5756 - md_pcrel_from (fixP
));
5757 (void) xg_apply_fix_value (fixP
, val
);
5760 else if (! fixP
->fx_addsy
)
5763 if (xg_apply_fix_value (fixP
, val
))
5768 case BFD_RELOC_XTENSA_ASM_EXPAND
:
5769 case BFD_RELOC_XTENSA_SLOT0_ALT
:
5770 case BFD_RELOC_XTENSA_SLOT1_ALT
:
5771 case BFD_RELOC_XTENSA_SLOT2_ALT
:
5772 case BFD_RELOC_XTENSA_SLOT3_ALT
:
5773 case BFD_RELOC_XTENSA_SLOT4_ALT
:
5774 case BFD_RELOC_XTENSA_SLOT5_ALT
:
5775 case BFD_RELOC_XTENSA_SLOT6_ALT
:
5776 case BFD_RELOC_XTENSA_SLOT7_ALT
:
5777 case BFD_RELOC_XTENSA_SLOT8_ALT
:
5778 case BFD_RELOC_XTENSA_SLOT9_ALT
:
5779 case BFD_RELOC_XTENSA_SLOT10_ALT
:
5780 case BFD_RELOC_XTENSA_SLOT11_ALT
:
5781 case BFD_RELOC_XTENSA_SLOT12_ALT
:
5782 case BFD_RELOC_XTENSA_SLOT13_ALT
:
5783 case BFD_RELOC_XTENSA_SLOT14_ALT
:
5784 /* These all need to be resolved at link-time. Do nothing now. */
5787 case BFD_RELOC_VTABLE_INHERIT
:
5788 case BFD_RELOC_VTABLE_ENTRY
:
5793 as_bad (_("unhandled local relocation fix %s"),
5794 bfd_get_reloc_code_name (fixP
->fx_r_type
));
5800 md_atof (int type
, char *litP
, int *sizeP
)
5802 return ieee_md_atof (type
, litP
, sizeP
, target_big_endian
);
5807 md_estimate_size_before_relax (fragS
*fragP
, segT seg ATTRIBUTE_UNUSED
)
5809 return total_frag_text_expansion (fragP
);
5813 /* Translate internal representation of relocation info to BFD target
5817 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixp
)
5821 reloc
= (arelent
*) xmalloc (sizeof (arelent
));
5822 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
5823 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
5824 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
5826 /* Make sure none of our internal relocations make it this far.
5827 They'd better have been fully resolved by this point. */
5828 assert ((int) fixp
->fx_r_type
> 0);
5830 reloc
->addend
= fixp
->fx_offset
;
5832 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
5833 if (reloc
->howto
== NULL
)
5835 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
5836 _("cannot represent `%s' relocation in object file"),
5837 bfd_get_reloc_code_name (fixp
->fx_r_type
));
5838 free (reloc
->sym_ptr_ptr
);
5843 if (!fixp
->fx_pcrel
!= !reloc
->howto
->pc_relative
)
5844 as_fatal (_("internal error; cannot generate `%s' relocation"),
5845 bfd_get_reloc_code_name (fixp
->fx_r_type
));
5851 /* Checks for resource conflicts between instructions. */
5853 /* The func unit stuff could be implemented as bit-vectors rather
5854 than the iterative approach here. If it ends up being too
5855 slow, we will switch it. */
5858 new_resource_table (void *data
,
5861 unit_num_copies_func uncf
,
5862 opcode_num_units_func onuf
,
5863 opcode_funcUnit_use_unit_func ouuf
,
5864 opcode_funcUnit_use_stage_func ousf
)
5867 resource_table
*rt
= (resource_table
*) xmalloc (sizeof (resource_table
));
5869 rt
->cycles
= cycles
;
5870 rt
->allocated_cycles
= cycles
;
5872 rt
->unit_num_copies
= uncf
;
5873 rt
->opcode_num_units
= onuf
;
5874 rt
->opcode_unit_use
= ouuf
;
5875 rt
->opcode_unit_stage
= ousf
;
5877 rt
->units
= (unsigned char **) xcalloc (cycles
, sizeof (unsigned char *));
5878 for (i
= 0; i
< cycles
; i
++)
5879 rt
->units
[i
] = (unsigned char *) xcalloc (nu
, sizeof (unsigned char));
5886 clear_resource_table (resource_table
*rt
)
5889 for (i
= 0; i
< rt
->allocated_cycles
; i
++)
5890 for (j
= 0; j
< rt
->num_units
; j
++)
5891 rt
->units
[i
][j
] = 0;
5895 /* We never shrink it, just fake it into thinking so. */
5898 resize_resource_table (resource_table
*rt
, int cycles
)
5902 rt
->cycles
= cycles
;
5903 if (cycles
<= rt
->allocated_cycles
)
5906 old_cycles
= rt
->allocated_cycles
;
5907 rt
->allocated_cycles
= cycles
;
5909 rt
->units
= xrealloc (rt
->units
,
5910 rt
->allocated_cycles
* sizeof (unsigned char *));
5911 for (i
= 0; i
< old_cycles
; i
++)
5912 rt
->units
[i
] = xrealloc (rt
->units
[i
],
5913 rt
->num_units
* sizeof (unsigned char));
5914 for (i
= old_cycles
; i
< cycles
; i
++)
5915 rt
->units
[i
] = xcalloc (rt
->num_units
, sizeof (unsigned char));
5920 resources_available (resource_table
*rt
, xtensa_opcode opcode
, int cycle
)
5923 int uses
= (rt
->opcode_num_units
) (rt
->data
, opcode
);
5925 for (i
= 0; i
< uses
; i
++)
5927 xtensa_funcUnit unit
= (rt
->opcode_unit_use
) (rt
->data
, opcode
, i
);
5928 int stage
= (rt
->opcode_unit_stage
) (rt
->data
, opcode
, i
);
5929 int copies_in_use
= rt
->units
[stage
+ cycle
][unit
];
5930 int copies
= (rt
->unit_num_copies
) (rt
->data
, unit
);
5931 if (copies_in_use
>= copies
)
5939 reserve_resources (resource_table
*rt
, xtensa_opcode opcode
, int cycle
)
5942 int uses
= (rt
->opcode_num_units
) (rt
->data
, opcode
);
5944 for (i
= 0; i
< uses
; i
++)
5946 xtensa_funcUnit unit
= (rt
->opcode_unit_use
) (rt
->data
, opcode
, i
);
5947 int stage
= (rt
->opcode_unit_stage
) (rt
->data
, opcode
, i
);
5948 /* Note that this allows resources to be oversubscribed. That's
5949 essential to the way the optional scheduler works.
5950 resources_available reports when a resource is over-subscribed,
5951 so it's easy to tell. */
5952 rt
->units
[stage
+ cycle
][unit
]++;
5958 release_resources (resource_table
*rt
, xtensa_opcode opcode
, int cycle
)
5961 int uses
= (rt
->opcode_num_units
) (rt
->data
, opcode
);
5963 for (i
= 0; i
< uses
; i
++)
5965 xtensa_funcUnit unit
= (rt
->opcode_unit_use
) (rt
->data
, opcode
, i
);
5966 int stage
= (rt
->opcode_unit_stage
) (rt
->data
, opcode
, i
);
5967 assert (rt
->units
[stage
+ cycle
][unit
] > 0);
5968 rt
->units
[stage
+ cycle
][unit
]--;
5973 /* Wrapper functions make parameterized resource reservation
5977 opcode_funcUnit_use_unit (void *data
, xtensa_opcode opcode
, int idx
)
5979 xtensa_funcUnit_use
*use
= xtensa_opcode_funcUnit_use (data
, opcode
, idx
);
5985 opcode_funcUnit_use_stage (void *data
, xtensa_opcode opcode
, int idx
)
5987 xtensa_funcUnit_use
*use
= xtensa_opcode_funcUnit_use (data
, opcode
, idx
);
5992 /* Note that this function does not check issue constraints, but
5993 solely whether the hardware is available to execute the given
5994 instructions together. It also doesn't check if the tinsns
5995 write the same state, or access the same tieports. That is
5996 checked by check_t1_t2_reads_and_writes. */
5999 resources_conflict (vliw_insn
*vinsn
)
6002 static resource_table
*rt
= NULL
;
6004 /* This is the most common case by far. Optimize it. */
6005 if (vinsn
->num_slots
== 1)
6010 xtensa_isa isa
= xtensa_default_isa
;
6011 rt
= new_resource_table
6012 (isa
, xtensa_num_pipe_stages
,
6013 xtensa_isa_num_funcUnits (isa
),
6014 (unit_num_copies_func
) xtensa_funcUnit_num_copies
,
6015 (opcode_num_units_func
) xtensa_opcode_num_funcUnit_uses
,
6016 opcode_funcUnit_use_unit
,
6017 opcode_funcUnit_use_stage
);
6020 clear_resource_table (rt
);
6022 for (i
= 0; i
< vinsn
->num_slots
; i
++)
6024 if (!resources_available (rt
, vinsn
->slots
[i
].opcode
, 0))
6026 reserve_resources (rt
, vinsn
->slots
[i
].opcode
, 0);
6033 /* finish_vinsn, emit_single_op and helper functions. */
6035 static bfd_boolean
find_vinsn_conflicts (vliw_insn
*);
6036 static xtensa_format
xg_find_narrowest_format (vliw_insn
*);
6037 static void xg_assemble_vliw_tokens (vliw_insn
*);
6040 /* We have reached the end of a bundle; emit into the frag. */
6043 finish_vinsn (vliw_insn
*vinsn
)
6050 if (find_vinsn_conflicts (vinsn
))
6052 xg_clear_vinsn (vinsn
);
6056 /* First, find a format that works. */
6057 if (vinsn
->format
== XTENSA_UNDEFINED
)
6058 vinsn
->format
= xg_find_narrowest_format (vinsn
);
6060 if (vinsn
->format
== XTENSA_UNDEFINED
)
6062 as_where (&file_name
, &line
);
6063 as_bad_where (file_name
, line
,
6064 _("couldn't find a valid instruction format"));
6065 fprintf (stderr
, _(" ops were: "));
6066 for (i
= 0; i
< vinsn
->num_slots
; i
++)
6067 fprintf (stderr
, _(" %s;"),
6068 xtensa_opcode_name (xtensa_default_isa
,
6069 vinsn
->slots
[i
].opcode
));
6070 fprintf (stderr
, _("\n"));
6071 xg_clear_vinsn (vinsn
);
6075 if (vinsn
->num_slots
6076 != xtensa_format_num_slots (xtensa_default_isa
, vinsn
->format
))
6078 as_bad (_("format '%s' allows %d slots, but there are %d opcodes"),
6079 xtensa_format_name (xtensa_default_isa
, vinsn
->format
),
6080 xtensa_format_num_slots (xtensa_default_isa
, vinsn
->format
),
6082 xg_clear_vinsn (vinsn
);
6086 if (resources_conflict (vinsn
))
6088 as_where (&file_name
, &line
);
6089 as_bad_where (file_name
, line
, _("illegal resource usage in bundle"));
6090 fprintf (stderr
, " ops were: ");
6091 for (i
= 0; i
< vinsn
->num_slots
; i
++)
6092 fprintf (stderr
, " %s;",
6093 xtensa_opcode_name (xtensa_default_isa
,
6094 vinsn
->slots
[i
].opcode
));
6095 fprintf (stderr
, "\n");
6096 xg_clear_vinsn (vinsn
);
6100 for (i
= 0; i
< vinsn
->num_slots
; i
++)
6102 if (vinsn
->slots
[i
].opcode
!= XTENSA_UNDEFINED
)
6104 symbolS
*lit_sym
= NULL
;
6106 bfd_boolean e
= FALSE
;
6107 bfd_boolean saved_density
= density_supported
;
6109 /* We don't want to narrow ops inside multi-slot bundles. */
6110 if (vinsn
->num_slots
> 1)
6111 density_supported
= FALSE
;
6113 istack_init (&slotstack
);
6114 if (vinsn
->slots
[i
].opcode
== xtensa_nop_opcode
)
6116 vinsn
->slots
[i
].opcode
=
6117 xtensa_format_slot_nop_opcode (xtensa_default_isa
,
6119 vinsn
->slots
[i
].ntok
= 0;
6122 if (xg_expand_assembly_insn (&slotstack
, &vinsn
->slots
[i
]))
6128 density_supported
= saved_density
;
6132 xg_clear_vinsn (vinsn
);
6136 for (j
= 0; j
< slotstack
.ninsn
; j
++)
6138 TInsn
*insn
= &slotstack
.insn
[j
];
6139 if (insn
->insn_type
== ITYPE_LITERAL
)
6141 assert (lit_sym
== NULL
);
6142 lit_sym
= xg_assemble_literal (insn
);
6146 assert (insn
->insn_type
== ITYPE_INSN
);
6148 xg_resolve_literals (insn
, lit_sym
);
6149 if (j
!= slotstack
.ninsn
- 1)
6150 emit_single_op (insn
);
6154 if (vinsn
->num_slots
> 1)
6156 if (opcode_fits_format_slot
6157 (slotstack
.insn
[slotstack
.ninsn
- 1].opcode
,
6160 vinsn
->slots
[i
] = slotstack
.insn
[slotstack
.ninsn
- 1];
6164 emit_single_op (&slotstack
.insn
[slotstack
.ninsn
- 1]);
6165 if (vinsn
->format
== XTENSA_UNDEFINED
)
6166 vinsn
->slots
[i
].opcode
= xtensa_nop_opcode
;
6168 vinsn
->slots
[i
].opcode
6169 = xtensa_format_slot_nop_opcode (xtensa_default_isa
,
6172 vinsn
->slots
[i
].ntok
= 0;
6177 vinsn
->slots
[0] = slotstack
.insn
[slotstack
.ninsn
- 1];
6178 vinsn
->format
= XTENSA_UNDEFINED
;
6183 /* Now check resource conflicts on the modified bundle. */
6184 if (resources_conflict (vinsn
))
6186 as_where (&file_name
, &line
);
6187 as_bad_where (file_name
, line
, _("illegal resource usage in bundle"));
6188 fprintf (stderr
, " ops were: ");
6189 for (i
= 0; i
< vinsn
->num_slots
; i
++)
6190 fprintf (stderr
, " %s;",
6191 xtensa_opcode_name (xtensa_default_isa
,
6192 vinsn
->slots
[i
].opcode
));
6193 fprintf (stderr
, "\n");
6194 xg_clear_vinsn (vinsn
);
6198 /* First, find a format that works. */
6199 if (vinsn
->format
== XTENSA_UNDEFINED
)
6200 vinsn
->format
= xg_find_narrowest_format (vinsn
);
6202 xg_assemble_vliw_tokens (vinsn
);
6204 xg_clear_vinsn (vinsn
);
6208 /* Given an vliw instruction, what conflicts are there in register
6209 usage and in writes to states and queues?
6211 This function does two things:
6212 1. Reports an error when a vinsn contains illegal combinations
6213 of writes to registers states or queues.
6214 2. Marks individual tinsns as not relaxable if the combination
6215 contains antidependencies.
6217 Job 2 handles things like swap semantics in instructions that need
6218 to be relaxed. For example,
6222 normally would be relaxed to
6227 _but_, if the above instruction is bundled with an a0 reader, e.g.,
6229 { addi a0, a1, 10000 ; add a2, a0, a4 ; }
6231 then we can't relax it into
6234 { add a0, a1, a0 ; add a2, a0, a4 ; }
6236 because the value of a0 is trashed before the second add can read it. */
6238 static char check_t1_t2_reads_and_writes (TInsn
*, TInsn
*);
6241 find_vinsn_conflicts (vliw_insn
*vinsn
)
6245 xtensa_isa isa
= xtensa_default_isa
;
6247 assert (!past_xtensa_end
);
6249 for (i
= 0 ; i
< vinsn
->num_slots
; i
++)
6251 TInsn
*op1
= &vinsn
->slots
[i
];
6252 if (op1
->is_specific_opcode
)
6253 op1
->keep_wide
= TRUE
;
6255 op1
->keep_wide
= FALSE
;
6258 for (i
= 0 ; i
< vinsn
->num_slots
; i
++)
6260 TInsn
*op1
= &vinsn
->slots
[i
];
6262 if (xtensa_opcode_is_branch (isa
, op1
->opcode
) == 1)
6265 for (j
= 0; j
< vinsn
->num_slots
; j
++)
6269 TInsn
*op2
= &vinsn
->slots
[j
];
6270 char conflict_type
= check_t1_t2_reads_and_writes (op1
, op2
);
6271 switch (conflict_type
)
6274 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same register"),
6275 xtensa_opcode_name (isa
, op1
->opcode
), i
,
6276 xtensa_opcode_name (isa
, op2
->opcode
), j
);
6279 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same state"),
6280 xtensa_opcode_name (isa
, op1
->opcode
), i
,
6281 xtensa_opcode_name (isa
, op2
->opcode
), j
);
6284 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same port"),
6285 xtensa_opcode_name (isa
, op1
->opcode
), i
,
6286 xtensa_opcode_name (isa
, op2
->opcode
), j
);
6289 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) both have volatile port accesses"),
6290 xtensa_opcode_name (isa
, op1
->opcode
), i
,
6291 xtensa_opcode_name (isa
, op2
->opcode
), j
);
6294 /* Everything is OK. */
6297 op2
->is_specific_opcode
= (op2
->is_specific_opcode
6298 || conflict_type
== 'a');
6305 as_bad (_("multiple branches or jumps in the same bundle"));
6313 /* Check how the state used by t1 and t2 relate.
6316 case A: t1 reads a register t2 writes (an antidependency within a bundle)
6317 case B: no relationship between what is read and written (both could
6318 read the same reg though)
6319 case C: t1 writes a register t2 writes (a register conflict within a
6321 case D: t1 writes a state that t2 also writes
6322 case E: t1 writes a tie queue that t2 also writes
6323 case F: two volatile queue accesses
6327 check_t1_t2_reads_and_writes (TInsn
*t1
, TInsn
*t2
)
6329 xtensa_isa isa
= xtensa_default_isa
;
6330 xtensa_regfile t1_regfile
, t2_regfile
;
6332 int t1_base_reg
, t1_last_reg
;
6333 int t2_base_reg
, t2_last_reg
;
6334 char t1_inout
, t2_inout
;
6336 char conflict
= 'b';
6341 bfd_boolean t1_volatile
= FALSE
;
6342 bfd_boolean t2_volatile
= FALSE
;
6344 /* Check registers. */
6345 for (j
= 0; j
< t2
->ntok
; j
++)
6347 if (xtensa_operand_is_register (isa
, t2
->opcode
, j
) != 1)
6350 t2_regfile
= xtensa_operand_regfile (isa
, t2
->opcode
, j
);
6351 t2_base_reg
= t2
->tok
[j
].X_add_number
;
6352 t2_last_reg
= t2_base_reg
+ xtensa_operand_num_regs (isa
, t2
->opcode
, j
);
6354 for (i
= 0; i
< t1
->ntok
; i
++)
6356 if (xtensa_operand_is_register (isa
, t1
->opcode
, i
) != 1)
6359 t1_regfile
= xtensa_operand_regfile (isa
, t1
->opcode
, i
);
6361 if (t1_regfile
!= t2_regfile
)
6364 t1_inout
= xtensa_operand_inout (isa
, t1
->opcode
, i
);
6365 t2_inout
= xtensa_operand_inout (isa
, t2
->opcode
, j
);
6367 if (xtensa_operand_is_known_reg (isa
, t1
->opcode
, i
) == 0
6368 || xtensa_operand_is_known_reg (isa
, t2
->opcode
, j
) == 0)
6370 if (t1_inout
== 'm' || t1_inout
== 'o'
6371 || t2_inout
== 'm' || t2_inout
== 'o')
6378 t1_base_reg
= t1
->tok
[i
].X_add_number
;
6379 t1_last_reg
= (t1_base_reg
6380 + xtensa_operand_num_regs (isa
, t1
->opcode
, i
));
6382 for (t1_reg
= t1_base_reg
; t1_reg
< t1_last_reg
; t1_reg
++)
6384 for (t2_reg
= t2_base_reg
; t2_reg
< t2_last_reg
; t2_reg
++)
6386 if (t1_reg
!= t2_reg
)
6389 if (t2_inout
== 'i' && (t1_inout
== 'm' || t1_inout
== 'o'))
6395 if (t1_inout
== 'i' && (t2_inout
== 'm' || t2_inout
== 'o'))
6401 if (t1_inout
!= 'i' && t2_inout
!= 'i')
6409 t1_states
= xtensa_opcode_num_stateOperands (isa
, t1
->opcode
);
6410 t2_states
= xtensa_opcode_num_stateOperands (isa
, t2
->opcode
);
6411 for (j
= 0; j
< t2_states
; j
++)
6413 xtensa_state t2_so
= xtensa_stateOperand_state (isa
, t2
->opcode
, j
);
6414 t2_inout
= xtensa_stateOperand_inout (isa
, t2
->opcode
, j
);
6415 for (i
= 0; i
< t1_states
; i
++)
6417 xtensa_state t1_so
= xtensa_stateOperand_state (isa
, t1
->opcode
, i
);
6418 t1_inout
= xtensa_stateOperand_inout (isa
, t1
->opcode
, i
);
6422 if (t2_inout
== 'i' && (t1_inout
== 'm' || t1_inout
== 'o'))
6428 if (t1_inout
== 'i' && (t2_inout
== 'm' || t2_inout
== 'o'))
6434 if (t1_inout
!= 'i' && t2_inout
!= 'i')
6439 /* Check tieports. */
6440 t1_interfaces
= xtensa_opcode_num_interfaceOperands (isa
, t1
->opcode
);
6441 t2_interfaces
= xtensa_opcode_num_interfaceOperands (isa
, t2
->opcode
);
6442 for (j
= 0; j
< t2_interfaces
; j
++)
6444 xtensa_interface t2_int
6445 = xtensa_interfaceOperand_interface (isa
, t2
->opcode
, j
);
6446 int t2_class
= xtensa_interface_class_id (isa
, t2_int
);
6448 t2_inout
= xtensa_interface_inout (isa
, t2_int
);
6449 if (xtensa_interface_has_side_effect (isa
, t2_int
) == 1)
6452 for (i
= 0; i
< t1_interfaces
; i
++)
6454 xtensa_interface t1_int
6455 = xtensa_interfaceOperand_interface (isa
, t1
->opcode
, j
);
6456 int t1_class
= xtensa_interface_class_id (isa
, t1_int
);
6458 t1_inout
= xtensa_interface_inout (isa
, t1_int
);
6459 if (xtensa_interface_has_side_effect (isa
, t1_int
) == 1)
6462 if (t1_volatile
&& t2_volatile
&& (t1_class
== t2_class
))
6465 if (t1_int
!= t2_int
)
6468 if (t2_inout
== 'i' && t1_inout
== 'o')
6474 if (t1_inout
== 'i' && t2_inout
== 'o')
6480 if (t1_inout
!= 'i' && t2_inout
!= 'i')
6489 static xtensa_format
6490 xg_find_narrowest_format (vliw_insn
*vinsn
)
6492 /* Right now we assume that the ops within the vinsn are properly
6493 ordered for the slots that the programmer wanted them in. In
6494 other words, we don't rearrange the ops in hopes of finding a
6495 better format. The scheduler handles that. */
6497 xtensa_isa isa
= xtensa_default_isa
;
6498 xtensa_format format
;
6499 vliw_insn v_copy
= *vinsn
;
6500 xtensa_opcode nop_opcode
= xtensa_nop_opcode
;
6502 if (vinsn
->num_slots
== 1)
6503 return xg_get_single_format (vinsn
->slots
[0].opcode
);
6505 for (format
= 0; format
< xtensa_isa_num_formats (isa
); format
++)
6508 if (xtensa_format_num_slots (isa
, format
) == v_copy
.num_slots
)
6512 for (slot
= 0; slot
< v_copy
.num_slots
; slot
++)
6514 if (v_copy
.slots
[slot
].opcode
== nop_opcode
)
6516 v_copy
.slots
[slot
].opcode
=
6517 xtensa_format_slot_nop_opcode (isa
, format
, slot
);
6518 v_copy
.slots
[slot
].ntok
= 0;
6521 if (opcode_fits_format_slot (v_copy
.slots
[slot
].opcode
,
6524 else if (v_copy
.num_slots
> 1)
6527 /* Try the widened version. */
6528 if (!v_copy
.slots
[slot
].keep_wide
6529 && !v_copy
.slots
[slot
].is_specific_opcode
6530 && xg_is_single_relaxable_insn (&v_copy
.slots
[slot
],
6532 && opcode_fits_format_slot (widened
.opcode
,
6535 v_copy
.slots
[slot
] = widened
;
6540 if (fit
== v_copy
.num_slots
)
6543 xtensa_format_encode (isa
, format
, vinsn
->insnbuf
);
6544 vinsn
->format
= format
;
6550 if (format
== xtensa_isa_num_formats (isa
))
6551 return XTENSA_UNDEFINED
;
6557 /* Return the additional space needed in a frag
6558 for possible relaxations of any ops in a VLIW insn.
6559 Also fill out the relaxations that might be required of
6560 each tinsn in the vinsn. */
6563 relaxation_requirements (vliw_insn
*vinsn
, bfd_boolean
*pfinish_frag
)
6565 bfd_boolean finish_frag
= FALSE
;
6566 int extra_space
= 0;
6569 for (slot
= 0; slot
< vinsn
->num_slots
; slot
++)
6571 TInsn
*tinsn
= &vinsn
->slots
[slot
];
6572 if (!tinsn_has_symbolic_operands (tinsn
))
6574 /* A narrow instruction could be widened later to help
6575 alignment issues. */
6576 if (xg_is_single_relaxable_insn (tinsn
, 0, TRUE
)
6577 && !tinsn
->is_specific_opcode
6578 && vinsn
->num_slots
== 1)
6580 /* Difference in bytes between narrow and wide insns... */
6582 tinsn
->subtype
= RELAX_NARROW
;
6587 if (workaround_b_j_loop_end
6588 && tinsn
->opcode
== xtensa_jx_opcode
6589 && use_transform ())
6591 /* Add 2 of these. */
6592 extra_space
+= 3; /* for the nop size */
6593 tinsn
->subtype
= RELAX_ADD_NOP_IF_PRE_LOOP_END
;
6596 /* Need to assemble it with space for the relocation. */
6597 if (xg_is_relaxable_insn (tinsn
, 0)
6598 && !tinsn
->is_specific_opcode
)
6600 int max_size
= xg_get_max_insn_widen_size (tinsn
->opcode
);
6601 int max_literal_size
=
6602 xg_get_max_insn_widen_literal_size (tinsn
->opcode
);
6604 tinsn
->literal_space
= max_literal_size
;
6606 tinsn
->subtype
= RELAX_IMMED
;
6607 extra_space
+= max_size
;
6611 /* A fix record will be added for this instruction prior
6612 to relaxation, so make it end the frag. */
6617 *pfinish_frag
= finish_frag
;
6623 bundle_tinsn (TInsn
*tinsn
, vliw_insn
*vinsn
)
6625 xtensa_isa isa
= xtensa_default_isa
;
6626 int slot
, chosen_slot
;
6628 vinsn
->format
= xg_get_single_format (tinsn
->opcode
);
6629 assert (vinsn
->format
!= XTENSA_UNDEFINED
);
6630 vinsn
->num_slots
= xtensa_format_num_slots (isa
, vinsn
->format
);
6632 chosen_slot
= xg_get_single_slot (tinsn
->opcode
);
6633 for (slot
= 0; slot
< vinsn
->num_slots
; slot
++)
6635 if (slot
== chosen_slot
)
6636 vinsn
->slots
[slot
] = *tinsn
;
6639 vinsn
->slots
[slot
].opcode
=
6640 xtensa_format_slot_nop_opcode (isa
, vinsn
->format
, slot
);
6641 vinsn
->slots
[slot
].ntok
= 0;
6642 vinsn
->slots
[slot
].insn_type
= ITYPE_INSN
;
6649 emit_single_op (TInsn
*orig_insn
)
6652 IStack istack
; /* put instructions into here */
6653 symbolS
*lit_sym
= NULL
;
6654 symbolS
*label_sym
= NULL
;
6656 istack_init (&istack
);
6658 /* Special-case for "movi aX, foo" which is guaranteed to need relaxing.
6659 Because the scheduling and bundling characteristics of movi and
6660 l32r or const16 are so different, we can do much better if we relax
6661 it prior to scheduling and bundling, rather than after. */
6662 if ((orig_insn
->opcode
== xtensa_movi_opcode
6663 || orig_insn
->opcode
== xtensa_movi_n_opcode
)
6664 && !cur_vinsn
.inside_bundle
6665 && (orig_insn
->tok
[1].X_op
== O_symbol
6666 || orig_insn
->tok
[1].X_op
== O_pltrel
)
6667 && !orig_insn
->is_specific_opcode
&& use_transform ())
6668 xg_assembly_relax (&istack
, orig_insn
, now_seg
, frag_now
, 0, 1, 0);
6670 if (xg_expand_assembly_insn (&istack
, orig_insn
))
6673 for (i
= 0; i
< istack
.ninsn
; i
++)
6675 TInsn
*insn
= &istack
.insn
[i
];
6676 switch (insn
->insn_type
)
6679 assert (lit_sym
== NULL
);
6680 lit_sym
= xg_assemble_literal (insn
);
6684 static int relaxed_sym_idx
= 0;
6685 char *label
= xmalloc (strlen (FAKE_LABEL_NAME
) + 12);
6686 sprintf (label
, "%s_rl_%x", FAKE_LABEL_NAME
, relaxed_sym_idx
++);
6688 assert (label_sym
== NULL
);
6689 label_sym
= symbol_find_or_make (label
);
6698 xg_resolve_literals (insn
, lit_sym
);
6700 xg_resolve_labels (insn
, label_sym
);
6702 bundle_tinsn (insn
, &v
);
6717 total_frag_text_expansion (fragS
*fragP
)
6720 int total_expansion
= 0;
6722 for (slot
= 0; slot
< MAX_SLOTS
; slot
++)
6723 total_expansion
+= fragP
->tc_frag_data
.text_expansion
[slot
];
6725 return total_expansion
;
6729 /* Emit a vliw instruction to the current fragment. */
6732 xg_assemble_vliw_tokens (vliw_insn
*vinsn
)
6734 bfd_boolean finish_frag
;
6735 bfd_boolean is_jump
= FALSE
;
6736 bfd_boolean is_branch
= FALSE
;
6737 xtensa_isa isa
= xtensa_default_isa
;
6742 struct dwarf2_line_info debug_line
;
6743 bfd_boolean loc_directive_seen
= FALSE
;
6746 memset (&debug_line
, 0, sizeof (struct dwarf2_line_info
));
6748 if (generating_literals
)
6750 static int reported
= 0;
6752 as_bad_where (frag_now
->fr_file
, frag_now
->fr_line
,
6753 _("cannot assemble into a literal fragment"));
6760 if (frag_now_fix () != 0
6761 && (! frag_now
->tc_frag_data
.is_insn
6762 || (vinsn_has_specific_opcodes (vinsn
) && use_transform ())
6763 || !use_transform () != frag_now
->tc_frag_data
.is_no_transform
6764 || (directive_state
[directive_longcalls
]
6765 != frag_now
->tc_frag_data
.use_longcalls
)
6766 || (directive_state
[directive_absolute_literals
]
6767 != frag_now
->tc_frag_data
.use_absolute_literals
)))
6769 frag_wane (frag_now
);
6771 xtensa_set_frag_assembly_state (frag_now
);
6774 if (workaround_a0_b_retw
6775 && vinsn
->num_slots
== 1
6776 && (get_last_insn_flags (now_seg
, now_subseg
) & FLAG_IS_A0_WRITER
) != 0
6777 && xtensa_opcode_is_branch (isa
, vinsn
->slots
[0].opcode
) == 1
6778 && use_transform ())
6780 has_a0_b_retw
= TRUE
;
6782 /* Mark this fragment with the special RELAX_ADD_NOP_IF_A0_B_RETW.
6783 After the first assembly pass we will check all of them and
6784 add a nop if needed. */
6785 frag_now
->tc_frag_data
.is_insn
= TRUE
;
6786 frag_var (rs_machine_dependent
, 4, 4,
6787 RELAX_ADD_NOP_IF_A0_B_RETW
,
6788 frag_now
->fr_symbol
,
6789 frag_now
->fr_offset
,
6791 xtensa_set_frag_assembly_state (frag_now
);
6792 frag_now
->tc_frag_data
.is_insn
= TRUE
;
6793 frag_var (rs_machine_dependent
, 4, 4,
6794 RELAX_ADD_NOP_IF_A0_B_RETW
,
6795 frag_now
->fr_symbol
,
6796 frag_now
->fr_offset
,
6798 xtensa_set_frag_assembly_state (frag_now
);
6801 for (slot
= 0; slot
< vinsn
->num_slots
; slot
++)
6803 tinsn
= &vinsn
->slots
[slot
];
6805 /* See if the instruction implies an aligned section. */
6806 if (xtensa_opcode_is_loop (isa
, tinsn
->opcode
) == 1)
6807 record_alignment (now_seg
, 2);
6809 /* Determine the best line number for debug info. */
6810 if ((tinsn
->loc_directive_seen
|| !loc_directive_seen
)
6811 && (tinsn
->debug_line
.filenum
!= debug_line
.filenum
6812 || tinsn
->debug_line
.line
< debug_line
.line
6813 || tinsn
->debug_line
.column
< debug_line
.column
))
6814 debug_line
= tinsn
->debug_line
;
6815 if (tinsn
->loc_directive_seen
)
6816 loc_directive_seen
= TRUE
;
6819 /* Special cases for instructions that force an alignment... */
6820 /* None of these opcodes are bundle-able. */
6821 if (xtensa_opcode_is_loop (isa
, vinsn
->slots
[0].opcode
) == 1)
6825 /* Remember the symbol that marks the end of the loop in the frag
6826 that marks the start of the loop. This way we can easily find
6827 the end of the loop at the beginning, without adding special code
6828 to mark the loop instructions themselves. */
6829 symbolS
*target_sym
= NULL
;
6830 if (vinsn
->slots
[0].tok
[1].X_op
== O_symbol
)
6831 target_sym
= vinsn
->slots
[0].tok
[1].X_add_symbol
;
6833 xtensa_set_frag_assembly_state (frag_now
);
6834 frag_now
->tc_frag_data
.is_insn
= TRUE
;
6836 max_fill
= get_text_align_max_fill_size
6837 (get_text_align_power (xtensa_fetch_width
),
6838 TRUE
, frag_now
->tc_frag_data
.is_no_density
);
6840 if (use_transform ())
6841 frag_var (rs_machine_dependent
, max_fill
, max_fill
,
6842 RELAX_ALIGN_NEXT_OPCODE
, target_sym
, 0, NULL
);
6844 frag_var (rs_machine_dependent
, 0, 0,
6845 RELAX_CHECK_ALIGN_NEXT_OPCODE
, target_sym
, 0, NULL
);
6846 xtensa_set_frag_assembly_state (frag_now
);
6849 if (vinsn
->slots
[0].opcode
== xtensa_entry_opcode
6850 && !vinsn
->slots
[0].is_specific_opcode
)
6852 xtensa_mark_literal_pool_location ();
6853 xtensa_move_labels (frag_now
, 0);
6854 frag_var (rs_align_test
, 1, 1, 0, NULL
, 2, NULL
);
6857 if (vinsn
->num_slots
== 1)
6859 if (workaround_a0_b_retw
&& use_transform ())
6860 set_last_insn_flags (now_seg
, now_subseg
, FLAG_IS_A0_WRITER
,
6861 is_register_writer (&vinsn
->slots
[0], "a", 0));
6863 set_last_insn_flags (now_seg
, now_subseg
, FLAG_IS_BAD_LOOPEND
,
6864 is_bad_loopend_opcode (&vinsn
->slots
[0]));
6867 set_last_insn_flags (now_seg
, now_subseg
, FLAG_IS_BAD_LOOPEND
, FALSE
);
6869 insn_size
= xtensa_format_length (isa
, vinsn
->format
);
6871 extra_space
= relaxation_requirements (vinsn
, &finish_frag
);
6873 /* vinsn_to_insnbuf will produce the error. */
6874 if (vinsn
->format
!= XTENSA_UNDEFINED
)
6876 f
= frag_more (insn_size
+ extra_space
);
6877 xtensa_set_frag_assembly_state (frag_now
);
6878 frag_now
->tc_frag_data
.is_insn
= TRUE
;
6881 vinsn_to_insnbuf (vinsn
, f
, frag_now
, FALSE
);
6882 if (vinsn
->format
== XTENSA_UNDEFINED
)
6885 xtensa_insnbuf_to_chars (isa
, vinsn
->insnbuf
, (unsigned char *) f
, 0);
6887 if (debug_type
== DEBUG_DWARF2
|| loc_directive_seen
)
6888 dwarf2_gen_line_info (frag_now_fix () - (insn_size
+ extra_space
),
6891 for (slot
= 0; slot
< vinsn
->num_slots
; slot
++)
6893 tinsn
= &vinsn
->slots
[slot
];
6894 frag_now
->tc_frag_data
.slot_subtypes
[slot
] = tinsn
->subtype
;
6895 frag_now
->tc_frag_data
.slot_symbols
[slot
] = tinsn
->symbol
;
6896 frag_now
->tc_frag_data
.slot_offsets
[slot
] = tinsn
->offset
;
6897 frag_now
->tc_frag_data
.literal_frags
[slot
] = tinsn
->literal_frag
;
6898 if (tinsn
->literal_space
!= 0)
6899 xg_assemble_literal_space (tinsn
->literal_space
, slot
);
6901 if (tinsn
->subtype
== RELAX_NARROW
)
6902 assert (vinsn
->num_slots
== 1);
6903 if (xtensa_opcode_is_jump (isa
, tinsn
->opcode
) == 1)
6905 if (xtensa_opcode_is_branch (isa
, tinsn
->opcode
) == 1)
6908 if (tinsn
->subtype
|| tinsn
->symbol
|| tinsn
->offset
6909 || tinsn
->literal_frag
|| is_jump
|| is_branch
)
6913 if (vinsn_has_specific_opcodes (vinsn
) && use_transform ())
6914 frag_now
->tc_frag_data
.is_specific_opcode
= TRUE
;
6918 frag_variant (rs_machine_dependent
,
6919 extra_space
, extra_space
, RELAX_SLOTS
,
6920 frag_now
->fr_symbol
, frag_now
->fr_offset
, f
);
6921 xtensa_set_frag_assembly_state (frag_now
);
6924 /* Special cases for loops:
6925 close_loop_end should be inserted AFTER short_loop.
6926 Make sure that CLOSE loops are processed BEFORE short_loops
6927 when converting them. */
6929 /* "short_loop": Add a NOP if the loop is < 4 bytes. */
6930 if (xtensa_opcode_is_loop (isa
, vinsn
->slots
[0].opcode
) == 1
6931 && !vinsn
->slots
[0].is_specific_opcode
)
6933 if (workaround_short_loop
&& use_transform ())
6935 maybe_has_short_loop
= TRUE
;
6936 frag_now
->tc_frag_data
.is_insn
= TRUE
;
6937 frag_var (rs_machine_dependent
, 4, 4,
6938 RELAX_ADD_NOP_IF_SHORT_LOOP
,
6939 frag_now
->fr_symbol
, frag_now
->fr_offset
, NULL
);
6940 frag_now
->tc_frag_data
.is_insn
= TRUE
;
6941 frag_var (rs_machine_dependent
, 4, 4,
6942 RELAX_ADD_NOP_IF_SHORT_LOOP
,
6943 frag_now
->fr_symbol
, frag_now
->fr_offset
, NULL
);
6946 /* "close_loop_end": Add up to 12 bytes of NOPs to keep a
6947 loop at least 12 bytes away from another loop's end. */
6948 if (workaround_close_loop_end
&& use_transform ())
6950 maybe_has_close_loop_end
= TRUE
;
6951 frag_now
->tc_frag_data
.is_insn
= TRUE
;
6952 frag_var (rs_machine_dependent
, 12, 12,
6953 RELAX_ADD_NOP_IF_CLOSE_LOOP_END
,
6954 frag_now
->fr_symbol
, frag_now
->fr_offset
, NULL
);
6958 if (use_transform ())
6962 assert (finish_frag
);
6963 frag_var (rs_machine_dependent
,
6964 UNREACHABLE_MAX_WIDTH
, UNREACHABLE_MAX_WIDTH
,
6966 frag_now
->fr_symbol
, frag_now
->fr_offset
, NULL
);
6967 xtensa_set_frag_assembly_state (frag_now
);
6969 else if (is_branch
&& do_align_targets ())
6971 assert (finish_frag
);
6972 frag_var (rs_machine_dependent
,
6973 UNREACHABLE_MAX_WIDTH
, UNREACHABLE_MAX_WIDTH
,
6974 RELAX_MAYBE_UNREACHABLE
,
6975 frag_now
->fr_symbol
, frag_now
->fr_offset
, NULL
);
6976 xtensa_set_frag_assembly_state (frag_now
);
6977 frag_var (rs_machine_dependent
,
6979 RELAX_MAYBE_DESIRE_ALIGN
,
6980 frag_now
->fr_symbol
, frag_now
->fr_offset
, NULL
);
6981 xtensa_set_frag_assembly_state (frag_now
);
6985 /* Now, if the original opcode was a call... */
6986 if (do_align_targets ()
6987 && xtensa_opcode_is_call (isa
, vinsn
->slots
[0].opcode
) == 1)
6989 float freq
= get_subseg_total_freq (now_seg
, now_subseg
);
6990 frag_now
->tc_frag_data
.is_insn
= TRUE
;
6991 frag_var (rs_machine_dependent
, 4, (int) freq
, RELAX_DESIRE_ALIGN
,
6992 frag_now
->fr_symbol
, frag_now
->fr_offset
, NULL
);
6993 xtensa_set_frag_assembly_state (frag_now
);
6996 if (vinsn_has_specific_opcodes (vinsn
) && use_transform ())
6998 frag_wane (frag_now
);
7000 xtensa_set_frag_assembly_state (frag_now
);
7005 /* xtensa_end and helper functions. */
7007 static void xtensa_cleanup_align_frags (void);
7008 static void xtensa_fix_target_frags (void);
7009 static void xtensa_mark_narrow_branches (void);
7010 static void xtensa_mark_zcl_first_insns (void);
7011 static void xtensa_mark_difference_of_two_symbols (void);
7012 static void xtensa_fix_a0_b_retw_frags (void);
7013 static void xtensa_fix_b_j_loop_end_frags (void);
7014 static void xtensa_fix_close_loop_end_frags (void);
7015 static void xtensa_fix_short_loop_frags (void);
7016 static void xtensa_sanity_check (void);
7017 static void xtensa_add_config_info (void);
7022 directive_balance ();
7023 xtensa_flush_pending_output ();
7025 past_xtensa_end
= TRUE
;
7027 xtensa_move_literals ();
7029 xtensa_reorder_segments ();
7030 xtensa_cleanup_align_frags ();
7031 xtensa_fix_target_frags ();
7032 if (workaround_a0_b_retw
&& has_a0_b_retw
)
7033 xtensa_fix_a0_b_retw_frags ();
7034 if (workaround_b_j_loop_end
)
7035 xtensa_fix_b_j_loop_end_frags ();
7037 /* "close_loop_end" should be processed BEFORE "short_loop". */
7038 if (workaround_close_loop_end
&& maybe_has_close_loop_end
)
7039 xtensa_fix_close_loop_end_frags ();
7041 if (workaround_short_loop
&& maybe_has_short_loop
)
7042 xtensa_fix_short_loop_frags ();
7044 xtensa_mark_narrow_branches ();
7045 xtensa_mark_zcl_first_insns ();
7047 xtensa_sanity_check ();
7049 xtensa_add_config_info ();
7054 xtensa_cleanup_align_frags (void)
7059 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
7060 for (frchP
= seg_info (s
)->frchainP
; frchP
; frchP
= frchP
->frch_next
)
7063 /* Walk over all of the fragments in a subsection. */
7064 for (fragP
= frchP
->frch_root
; fragP
; fragP
= fragP
->fr_next
)
7066 if ((fragP
->fr_type
== rs_align
7067 || fragP
->fr_type
== rs_align_code
7068 || (fragP
->fr_type
== rs_machine_dependent
7069 && (fragP
->fr_subtype
== RELAX_DESIRE_ALIGN
7070 || fragP
->fr_subtype
== RELAX_DESIRE_ALIGN_IF_TARGET
)))
7071 && fragP
->fr_fix
== 0)
7073 fragS
*next
= fragP
->fr_next
;
7076 && next
->fr_fix
== 0
7077 && next
->fr_type
== rs_machine_dependent
7078 && next
->fr_subtype
== RELAX_DESIRE_ALIGN_IF_TARGET
)
7081 next
= next
->fr_next
;
7084 /* If we don't widen branch targets, then they
7085 will be easier to align. */
7086 if (fragP
->tc_frag_data
.is_branch_target
7087 && fragP
->fr_opcode
== fragP
->fr_literal
7088 && fragP
->fr_type
== rs_machine_dependent
7089 && fragP
->fr_subtype
== RELAX_SLOTS
7090 && fragP
->tc_frag_data
.slot_subtypes
[0] == RELAX_NARROW
)
7092 if (fragP
->fr_type
== rs_machine_dependent
7093 && fragP
->fr_subtype
== RELAX_UNREACHABLE
)
7094 fragP
->tc_frag_data
.is_unreachable
= TRUE
;
7100 /* Re-process all of the fragments looking to convert all of the
7101 RELAX_DESIRE_ALIGN_IF_TARGET fragments. If there is a branch
7102 target in the next fragment, convert this to RELAX_DESIRE_ALIGN.
7103 Otherwise, convert to a .fill 0. */
7106 xtensa_fix_target_frags (void)
7111 /* When this routine is called, all of the subsections are still intact
7112 so we walk over subsections instead of sections. */
7113 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
7114 for (frchP
= seg_info (s
)->frchainP
; frchP
; frchP
= frchP
->frch_next
)
7118 /* Walk over all of the fragments in a subsection. */
7119 for (fragP
= frchP
->frch_root
; fragP
; fragP
= fragP
->fr_next
)
7121 if (fragP
->fr_type
== rs_machine_dependent
7122 && fragP
->fr_subtype
== RELAX_DESIRE_ALIGN_IF_TARGET
)
7124 if (next_frag_is_branch_target (fragP
))
7125 fragP
->fr_subtype
= RELAX_DESIRE_ALIGN
;
7134 static bfd_boolean
is_narrow_branch_guaranteed_in_range (fragS
*, TInsn
*);
7137 xtensa_mark_narrow_branches (void)
7142 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
7143 for (frchP
= seg_info (s
)->frchainP
; frchP
; frchP
= frchP
->frch_next
)
7146 /* Walk over all of the fragments in a subsection. */
7147 for (fragP
= frchP
->frch_root
; fragP
; fragP
= fragP
->fr_next
)
7149 if (fragP
->fr_type
== rs_machine_dependent
7150 && fragP
->fr_subtype
== RELAX_SLOTS
7151 && fragP
->tc_frag_data
.slot_subtypes
[0] == RELAX_IMMED
)
7155 vinsn_from_chars (&vinsn
, fragP
->fr_opcode
);
7156 tinsn_immed_from_frag (&vinsn
.slots
[0], fragP
, 0);
7158 if (vinsn
.num_slots
== 1
7159 && xtensa_opcode_is_branch (xtensa_default_isa
,
7160 vinsn
.slots
[0].opcode
) == 1
7161 && xg_get_single_size (vinsn
.slots
[0].opcode
) == 2
7162 && is_narrow_branch_guaranteed_in_range (fragP
,
7165 fragP
->fr_subtype
= RELAX_SLOTS
;
7166 fragP
->tc_frag_data
.slot_subtypes
[0] = RELAX_NARROW
;
7167 fragP
->tc_frag_data
.is_aligning_branch
= 1;
7175 /* A branch is typically widened only when its target is out of
7176 range. However, we would like to widen them to align a subsequent
7177 branch target when possible.
7179 Because the branch relaxation code is so convoluted, the optimal solution
7180 (combining the two cases) is difficult to get right in all circumstances.
7181 We therefore go with an "almost as good" solution, where we only
7182 use for alignment narrow branches that definitely will not expand to a
7183 jump and a branch. These functions find and mark these cases. */
7185 /* The range in bytes of BNEZ.N and BEQZ.N. The target operand is encoded
7186 as PC + 4 + imm6, where imm6 is a 6-bit immediate ranging from 0 to 63.
7187 We start counting beginning with the frag after the 2-byte branch, so the
7188 maximum offset is (4 - 2) + 63 = 65. */
7189 #define MAX_IMMED6 65
7191 static offsetT
unrelaxed_frag_max_size (fragS
*);
7194 is_narrow_branch_guaranteed_in_range (fragS
*fragP
, TInsn
*tinsn
)
7196 const expressionS
*expr
= &tinsn
->tok
[1];
7197 symbolS
*symbolP
= expr
->X_add_symbol
;
7198 offsetT max_distance
= expr
->X_add_number
;
7201 if (expr
->X_op
!= O_symbol
)
7204 target_frag
= symbol_get_frag (symbolP
);
7206 max_distance
+= (S_GET_VALUE (symbolP
) - target_frag
->fr_address
);
7207 if (is_branch_jmp_to_next (tinsn
, fragP
))
7210 /* The branch doesn't branch over it's own frag,
7211 but over the subsequent ones. */
7212 fragP
= fragP
->fr_next
;
7213 while (fragP
!= NULL
&& fragP
!= target_frag
&& max_distance
<= MAX_IMMED6
)
7215 max_distance
+= unrelaxed_frag_max_size (fragP
);
7216 fragP
= fragP
->fr_next
;
7218 if (max_distance
<= MAX_IMMED6
&& fragP
== target_frag
)
7225 xtensa_mark_zcl_first_insns (void)
7230 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
7231 for (frchP
= seg_info (s
)->frchainP
; frchP
; frchP
= frchP
->frch_next
)
7234 /* Walk over all of the fragments in a subsection. */
7235 for (fragP
= frchP
->frch_root
; fragP
; fragP
= fragP
->fr_next
)
7237 if (fragP
->fr_type
== rs_machine_dependent
7238 && (fragP
->fr_subtype
== RELAX_ALIGN_NEXT_OPCODE
7239 || fragP
->fr_subtype
== RELAX_CHECK_ALIGN_NEXT_OPCODE
))
7241 /* Find the loop frag. */
7242 fragS
*targ_frag
= next_non_empty_frag (fragP
);
7243 /* Find the first insn frag. */
7244 targ_frag
= next_non_empty_frag (targ_frag
);
7246 /* Of course, sometimes (mostly for toy test cases) a
7247 zero-cost loop instruction is the last in a section. */
7250 targ_frag
->tc_frag_data
.is_first_loop_insn
= TRUE
;
7251 /* Do not widen a frag that is the first instruction of a
7252 zero-cost loop. It makes that loop harder to align. */
7253 if (targ_frag
->fr_type
== rs_machine_dependent
7254 && targ_frag
->fr_subtype
== RELAX_SLOTS
7255 && (targ_frag
->tc_frag_data
.slot_subtypes
[0]
7258 if (targ_frag
->tc_frag_data
.is_aligning_branch
)
7259 targ_frag
->tc_frag_data
.slot_subtypes
[0] = RELAX_IMMED
;
7262 frag_wane (targ_frag
);
7263 targ_frag
->tc_frag_data
.slot_subtypes
[0] = 0;
7267 if (fragP
->fr_subtype
== RELAX_CHECK_ALIGN_NEXT_OPCODE
)
7275 /* When a difference-of-symbols expression is encoded as a uleb128 or
7276 sleb128 value, the linker is unable to adjust that value to account for
7277 link-time relaxation. Mark all the code between such symbols so that
7278 its size cannot be changed by linker relaxation. */
7281 xtensa_mark_difference_of_two_symbols (void)
7285 for (expr_sym
= expr_symbols
; expr_sym
;
7286 expr_sym
= symbol_get_tc (expr_sym
)->next_expr_symbol
)
7288 expressionS
*expr
= symbol_get_value_expression (expr_sym
);
7290 if (expr
->X_op
== O_subtract
)
7292 symbolS
*left
= expr
->X_add_symbol
;
7293 symbolS
*right
= expr
->X_op_symbol
;
7295 /* Difference of two symbols not in the same section
7296 are handled with relocations in the linker. */
7297 if (S_GET_SEGMENT (left
) == S_GET_SEGMENT (right
))
7302 if (symbol_get_frag (left
)->fr_address
7303 <= symbol_get_frag (right
)->fr_address
)
7305 start
= symbol_get_frag (left
);
7306 end
= symbol_get_frag (right
);
7310 start
= symbol_get_frag (right
);
7311 end
= symbol_get_frag (left
);
7315 start
->tc_frag_data
.is_no_transform
= 1;
7316 start
= start
->fr_next
;
7318 while (start
&& start
->fr_address
< end
->fr_address
);
7325 /* Re-process all of the fragments looking to convert all of the
7326 RELAX_ADD_NOP_IF_A0_B_RETW. If the next instruction is a
7327 conditional branch or a retw/retw.n, convert this frag to one that
7328 will generate a NOP. In any case close it off with a .fill 0. */
7330 static bfd_boolean
next_instrs_are_b_retw (fragS
*);
7333 xtensa_fix_a0_b_retw_frags (void)
7338 /* When this routine is called, all of the subsections are still intact
7339 so we walk over subsections instead of sections. */
7340 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
7341 for (frchP
= seg_info (s
)->frchainP
; frchP
; frchP
= frchP
->frch_next
)
7345 /* Walk over all of the fragments in a subsection. */
7346 for (fragP
= frchP
->frch_root
; fragP
; fragP
= fragP
->fr_next
)
7348 if (fragP
->fr_type
== rs_machine_dependent
7349 && fragP
->fr_subtype
== RELAX_ADD_NOP_IF_A0_B_RETW
)
7351 if (next_instrs_are_b_retw (fragP
))
7353 if (fragP
->tc_frag_data
.is_no_transform
)
7354 as_bad (_("instruction sequence (write a0, branch, retw) may trigger hardware errata"));
7356 relax_frag_add_nop (fragP
);
7366 next_instrs_are_b_retw (fragS
*fragP
)
7368 xtensa_opcode opcode
;
7370 const fragS
*next_fragP
= next_non_empty_frag (fragP
);
7371 static xtensa_insnbuf insnbuf
= NULL
;
7372 static xtensa_insnbuf slotbuf
= NULL
;
7373 xtensa_isa isa
= xtensa_default_isa
;
7376 bfd_boolean branch_seen
= FALSE
;
7380 insnbuf
= xtensa_insnbuf_alloc (isa
);
7381 slotbuf
= xtensa_insnbuf_alloc (isa
);
7384 if (next_fragP
== NULL
)
7387 /* Check for the conditional branch. */
7388 xtensa_insnbuf_from_chars
7389 (isa
, insnbuf
, (unsigned char *) &next_fragP
->fr_literal
[offset
], 0);
7390 fmt
= xtensa_format_decode (isa
, insnbuf
);
7391 if (fmt
== XTENSA_UNDEFINED
)
7394 for (slot
= 0; slot
< xtensa_format_num_slots (isa
, fmt
); slot
++)
7396 xtensa_format_get_slot (isa
, fmt
, slot
, insnbuf
, slotbuf
);
7397 opcode
= xtensa_opcode_decode (isa
, fmt
, slot
, slotbuf
);
7399 branch_seen
= (branch_seen
7400 || xtensa_opcode_is_branch (isa
, opcode
) == 1);
7406 offset
+= xtensa_format_length (isa
, fmt
);
7407 if (offset
== next_fragP
->fr_fix
)
7409 next_fragP
= next_non_empty_frag (next_fragP
);
7413 if (next_fragP
== NULL
)
7416 /* Check for the retw/retw.n. */
7417 xtensa_insnbuf_from_chars
7418 (isa
, insnbuf
, (unsigned char *) &next_fragP
->fr_literal
[offset
], 0);
7419 fmt
= xtensa_format_decode (isa
, insnbuf
);
7421 /* Because RETW[.N] is not bundleable, a VLIW bundle here means that we
7422 have no problems. */
7423 if (fmt
== XTENSA_UNDEFINED
7424 || xtensa_format_num_slots (isa
, fmt
) != 1)
7427 xtensa_format_get_slot (isa
, fmt
, 0, insnbuf
, slotbuf
);
7428 opcode
= xtensa_opcode_decode (isa
, fmt
, 0, slotbuf
);
7430 if (opcode
== xtensa_retw_opcode
|| opcode
== xtensa_retw_n_opcode
)
7437 /* Re-process all of the fragments looking to convert all of the
7438 RELAX_ADD_NOP_IF_PRE_LOOP_END. If there is one instruction and a
7439 loop end label, convert this frag to one that will generate a NOP.
7440 In any case close it off with a .fill 0. */
7442 static bfd_boolean
next_instr_is_loop_end (fragS
*);
7445 xtensa_fix_b_j_loop_end_frags (void)
7450 /* When this routine is called, all of the subsections are still intact
7451 so we walk over subsections instead of sections. */
7452 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
7453 for (frchP
= seg_info (s
)->frchainP
; frchP
; frchP
= frchP
->frch_next
)
7457 /* Walk over all of the fragments in a subsection. */
7458 for (fragP
= frchP
->frch_root
; fragP
; fragP
= fragP
->fr_next
)
7460 if (fragP
->fr_type
== rs_machine_dependent
7461 && fragP
->fr_subtype
== RELAX_ADD_NOP_IF_PRE_LOOP_END
)
7463 if (next_instr_is_loop_end (fragP
))
7465 if (fragP
->tc_frag_data
.is_no_transform
)
7466 as_bad (_("branching or jumping to a loop end may trigger hardware errata"));
7468 relax_frag_add_nop (fragP
);
7478 next_instr_is_loop_end (fragS
*fragP
)
7480 const fragS
*next_fragP
;
7482 if (next_frag_is_loop_target (fragP
))
7485 next_fragP
= next_non_empty_frag (fragP
);
7486 if (next_fragP
== NULL
)
7489 if (!next_frag_is_loop_target (next_fragP
))
7492 /* If the size is >= 3 then there is more than one instruction here.
7493 The hardware bug will not fire. */
7494 if (next_fragP
->fr_fix
> 3)
7501 /* Re-process all of the fragments looking to convert all of the
7502 RELAX_ADD_NOP_IF_CLOSE_LOOP_END. If there is an loop end that is
7503 not MY loop's loop end within 12 bytes, add enough nops here to
7504 make it at least 12 bytes away. In any case close it off with a
7507 static offsetT min_bytes_to_other_loop_end
7508 (fragS
*, fragS
*, offsetT
);
7511 xtensa_fix_close_loop_end_frags (void)
7516 /* When this routine is called, all of the subsections are still intact
7517 so we walk over subsections instead of sections. */
7518 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
7519 for (frchP
= seg_info (s
)->frchainP
; frchP
; frchP
= frchP
->frch_next
)
7523 fragS
*current_target
= NULL
;
7525 /* Walk over all of the fragments in a subsection. */
7526 for (fragP
= frchP
->frch_root
; fragP
; fragP
= fragP
->fr_next
)
7528 if (fragP
->fr_type
== rs_machine_dependent
7529 && ((fragP
->fr_subtype
== RELAX_ALIGN_NEXT_OPCODE
)
7530 || (fragP
->fr_subtype
== RELAX_CHECK_ALIGN_NEXT_OPCODE
)))
7531 current_target
= symbol_get_frag (fragP
->fr_symbol
);
7534 && fragP
->fr_type
== rs_machine_dependent
7535 && fragP
->fr_subtype
== RELAX_ADD_NOP_IF_CLOSE_LOOP_END
)
7538 int bytes_added
= 0;
7540 #define REQUIRED_LOOP_DIVIDING_BYTES 12
7541 /* Max out at 12. */
7542 min_bytes
= min_bytes_to_other_loop_end
7543 (fragP
->fr_next
, current_target
, REQUIRED_LOOP_DIVIDING_BYTES
);
7545 if (min_bytes
< REQUIRED_LOOP_DIVIDING_BYTES
)
7547 if (fragP
->tc_frag_data
.is_no_transform
)
7548 as_bad (_("loop end too close to another loop end may trigger hardware errata"));
7551 while (min_bytes
+ bytes_added
7552 < REQUIRED_LOOP_DIVIDING_BYTES
)
7556 if (fragP
->fr_var
< length
)
7557 as_fatal (_("fr_var %lu < length %d"),
7558 (long) fragP
->fr_var
, length
);
7561 assemble_nop (length
,
7562 fragP
->fr_literal
+ fragP
->fr_fix
);
7563 fragP
->fr_fix
+= length
;
7564 fragP
->fr_var
-= length
;
7566 bytes_added
+= length
;
7572 assert (fragP
->fr_type
!= rs_machine_dependent
7573 || fragP
->fr_subtype
!= RELAX_ADD_NOP_IF_CLOSE_LOOP_END
);
7579 static offsetT
unrelaxed_frag_min_size (fragS
*);
7582 min_bytes_to_other_loop_end (fragS
*fragP
,
7583 fragS
*current_target
,
7587 fragS
*current_fragP
;
7589 for (current_fragP
= fragP
;
7591 current_fragP
= current_fragP
->fr_next
)
7593 if (current_fragP
->tc_frag_data
.is_loop_target
7594 && current_fragP
!= current_target
)
7597 offset
+= unrelaxed_frag_min_size (current_fragP
);
7599 if (offset
>= max_size
)
7607 unrelaxed_frag_min_size (fragS
*fragP
)
7609 offsetT size
= fragP
->fr_fix
;
7611 /* Add fill size. */
7612 if (fragP
->fr_type
== rs_fill
)
7613 size
+= fragP
->fr_offset
;
7620 unrelaxed_frag_max_size (fragS
*fragP
)
7622 offsetT size
= fragP
->fr_fix
;
7623 switch (fragP
->fr_type
)
7626 /* Empty frags created by the obstack allocation scheme
7627 end up with type 0. */
7632 size
+= fragP
->fr_offset
;
7640 /* No further adjustments needed. */
7642 case rs_machine_dependent
:
7643 if (fragP
->fr_subtype
!= RELAX_DESIRE_ALIGN
)
7644 size
+= fragP
->fr_var
;
7647 /* We had darn well better know how big it is. */
7656 /* Re-process all of the fragments looking to convert all
7657 of the RELAX_ADD_NOP_IF_SHORT_LOOP. If:
7660 1) the instruction size count to the loop end label
7661 is too short (<= 2 instructions),
7662 2) loop has a jump or branch in it
7665 1) workaround_all_short_loops is TRUE
7666 2) The generating loop was a 'loopgtz' or 'loopnez'
7667 3) the instruction size count to the loop end label is too short
7669 then convert this frag (and maybe the next one) to generate a NOP.
7670 In any case close it off with a .fill 0. */
7672 static int count_insns_to_loop_end (fragS
*, bfd_boolean
, int);
7673 static bfd_boolean
branch_before_loop_end (fragS
*);
7676 xtensa_fix_short_loop_frags (void)
7681 /* When this routine is called, all of the subsections are still intact
7682 so we walk over subsections instead of sections. */
7683 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
7684 for (frchP
= seg_info (s
)->frchainP
; frchP
; frchP
= frchP
->frch_next
)
7687 fragS
*current_target
= NULL
;
7688 xtensa_opcode current_opcode
= XTENSA_UNDEFINED
;
7690 /* Walk over all of the fragments in a subsection. */
7691 for (fragP
= frchP
->frch_root
; fragP
; fragP
= fragP
->fr_next
)
7693 if (fragP
->fr_type
== rs_machine_dependent
7694 && ((fragP
->fr_subtype
== RELAX_ALIGN_NEXT_OPCODE
)
7695 || (fragP
->fr_subtype
== RELAX_CHECK_ALIGN_NEXT_OPCODE
)))
7698 fragS
*loop_frag
= next_non_empty_frag (fragP
);
7699 tinsn_from_chars (&t_insn
, loop_frag
->fr_opcode
, 0);
7700 current_target
= symbol_get_frag (fragP
->fr_symbol
);
7701 current_opcode
= t_insn
.opcode
;
7702 assert (xtensa_opcode_is_loop (xtensa_default_isa
,
7703 current_opcode
) == 1);
7706 if (fragP
->fr_type
== rs_machine_dependent
7707 && fragP
->fr_subtype
== RELAX_ADD_NOP_IF_SHORT_LOOP
)
7709 if (count_insns_to_loop_end (fragP
->fr_next
, TRUE
, 3) < 3
7710 && (branch_before_loop_end (fragP
->fr_next
)
7711 || (workaround_all_short_loops
7712 && current_opcode
!= XTENSA_UNDEFINED
7713 && current_opcode
!= xtensa_loop_opcode
)))
7715 if (fragP
->tc_frag_data
.is_no_transform
)
7716 as_bad (_("loop containing less than three instructions may trigger hardware errata"));
7718 relax_frag_add_nop (fragP
);
7727 static int unrelaxed_frag_min_insn_count (fragS
*);
7730 count_insns_to_loop_end (fragS
*base_fragP
,
7731 bfd_boolean count_relax_add
,
7734 fragS
*fragP
= NULL
;
7739 for (; fragP
&& !fragP
->tc_frag_data
.is_loop_target
; fragP
= fragP
->fr_next
)
7741 insn_count
+= unrelaxed_frag_min_insn_count (fragP
);
7742 if (insn_count
>= max_count
)
7745 if (count_relax_add
)
7747 if (fragP
->fr_type
== rs_machine_dependent
7748 && fragP
->fr_subtype
== RELAX_ADD_NOP_IF_SHORT_LOOP
)
7750 /* In order to add the appropriate number of
7751 NOPs, we count an instruction for downstream
7754 if (insn_count
>= max_count
)
7764 unrelaxed_frag_min_insn_count (fragS
*fragP
)
7766 xtensa_isa isa
= xtensa_default_isa
;
7767 static xtensa_insnbuf insnbuf
= NULL
;
7771 if (!fragP
->tc_frag_data
.is_insn
)
7775 insnbuf
= xtensa_insnbuf_alloc (isa
);
7777 /* Decode the fixed instructions. */
7778 while (offset
< fragP
->fr_fix
)
7782 xtensa_insnbuf_from_chars
7783 (isa
, insnbuf
, (unsigned char *) fragP
->fr_literal
+ offset
, 0);
7784 fmt
= xtensa_format_decode (isa
, insnbuf
);
7786 if (fmt
== XTENSA_UNDEFINED
)
7788 as_fatal (_("undecodable instruction in instruction frag"));
7791 offset
+= xtensa_format_length (isa
, fmt
);
7799 static bfd_boolean
unrelaxed_frag_has_b_j (fragS
*);
7802 branch_before_loop_end (fragS
*base_fragP
)
7806 for (fragP
= base_fragP
;
7807 fragP
&& !fragP
->tc_frag_data
.is_loop_target
;
7808 fragP
= fragP
->fr_next
)
7810 if (unrelaxed_frag_has_b_j (fragP
))
7818 unrelaxed_frag_has_b_j (fragS
*fragP
)
7820 static xtensa_insnbuf insnbuf
= NULL
;
7821 xtensa_isa isa
= xtensa_default_isa
;
7824 if (!fragP
->tc_frag_data
.is_insn
)
7828 insnbuf
= xtensa_insnbuf_alloc (isa
);
7830 /* Decode the fixed instructions. */
7831 while (offset
< fragP
->fr_fix
)
7836 xtensa_insnbuf_from_chars
7837 (isa
, insnbuf
, (unsigned char *) fragP
->fr_literal
+ offset
, 0);
7838 fmt
= xtensa_format_decode (isa
, insnbuf
);
7839 if (fmt
== XTENSA_UNDEFINED
)
7842 for (slot
= 0; slot
< xtensa_format_num_slots (isa
, fmt
); slot
++)
7844 xtensa_opcode opcode
=
7845 get_opcode_from_buf (fragP
->fr_literal
+ offset
, slot
);
7846 if (xtensa_opcode_is_branch (isa
, opcode
) == 1
7847 || xtensa_opcode_is_jump (isa
, opcode
) == 1)
7850 offset
+= xtensa_format_length (isa
, fmt
);
7856 /* Checks to be made after initial assembly but before relaxation. */
7858 static bfd_boolean
is_empty_loop (const TInsn
*, fragS
*);
7859 static bfd_boolean
is_local_forward_loop (const TInsn
*, fragS
*);
7862 xtensa_sanity_check (void)
7869 as_where (&file_name
, &line
);
7870 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
7871 for (frchP
= seg_info (s
)->frchainP
; frchP
; frchP
= frchP
->frch_next
)
7875 /* Walk over all of the fragments in a subsection. */
7876 for (fragP
= frchP
->frch_root
; fragP
; fragP
= fragP
->fr_next
)
7878 if (fragP
->fr_type
== rs_machine_dependent
7879 && fragP
->fr_subtype
== RELAX_SLOTS
7880 && fragP
->tc_frag_data
.slot_subtypes
[0] == RELAX_IMMED
)
7882 static xtensa_insnbuf insnbuf
= NULL
;
7885 if (fragP
->fr_opcode
!= NULL
)
7888 insnbuf
= xtensa_insnbuf_alloc (xtensa_default_isa
);
7889 tinsn_from_chars (&t_insn
, fragP
->fr_opcode
, 0);
7890 tinsn_immed_from_frag (&t_insn
, fragP
, 0);
7892 if (xtensa_opcode_is_loop (xtensa_default_isa
,
7893 t_insn
.opcode
) == 1)
7895 if (is_empty_loop (&t_insn
, fragP
))
7897 new_logical_line (fragP
->fr_file
, fragP
->fr_line
);
7898 as_bad (_("invalid empty loop"));
7900 if (!is_local_forward_loop (&t_insn
, fragP
))
7902 new_logical_line (fragP
->fr_file
, fragP
->fr_line
);
7903 as_bad (_("loop target does not follow "
7904 "loop instruction in section"));
7911 new_logical_line (file_name
, line
);
7915 #define LOOP_IMMED_OPN 1
7917 /* Return TRUE if the loop target is the next non-zero fragment. */
7920 is_empty_loop (const TInsn
*insn
, fragS
*fragP
)
7922 const expressionS
*expr
;
7926 if (insn
->insn_type
!= ITYPE_INSN
)
7929 if (xtensa_opcode_is_loop (xtensa_default_isa
, insn
->opcode
) != 1)
7932 if (insn
->ntok
<= LOOP_IMMED_OPN
)
7935 expr
= &insn
->tok
[LOOP_IMMED_OPN
];
7937 if (expr
->X_op
!= O_symbol
)
7940 symbolP
= expr
->X_add_symbol
;
7944 if (symbol_get_frag (symbolP
) == NULL
)
7947 if (S_GET_VALUE (symbolP
) != 0)
7950 /* Walk through the zero-size fragments from this one. If we find
7951 the target fragment, then this is a zero-size loop. */
7953 for (next_fragP
= fragP
->fr_next
;
7955 next_fragP
= next_fragP
->fr_next
)
7957 if (next_fragP
== symbol_get_frag (symbolP
))
7959 if (next_fragP
->fr_fix
!= 0)
7967 is_local_forward_loop (const TInsn
*insn
, fragS
*fragP
)
7969 const expressionS
*expr
;
7973 if (insn
->insn_type
!= ITYPE_INSN
)
7976 if (xtensa_opcode_is_loop (xtensa_default_isa
, insn
->opcode
) != 1)
7979 if (insn
->ntok
<= LOOP_IMMED_OPN
)
7982 expr
= &insn
->tok
[LOOP_IMMED_OPN
];
7984 if (expr
->X_op
!= O_symbol
)
7987 symbolP
= expr
->X_add_symbol
;
7991 if (symbol_get_frag (symbolP
) == NULL
)
7994 /* Walk through fragments until we find the target.
7995 If we do not find the target, then this is an invalid loop. */
7997 for (next_fragP
= fragP
->fr_next
;
7999 next_fragP
= next_fragP
->fr_next
)
8001 if (next_fragP
== symbol_get_frag (symbolP
))
8009 #define XTINFO_NAME "Xtensa_Info"
8010 #define XTINFO_NAMESZ 12
8011 #define XTINFO_TYPE 1
8014 xtensa_add_config_info (void)
8020 info_sec
= subseg_new (".xtensa.info", 0);
8021 bfd_set_section_flags (stdoutput
, info_sec
, SEC_HAS_CONTENTS
| SEC_READONLY
);
8023 data
= xmalloc (100);
8024 sprintf (data
, "USE_ABSOLUTE_LITERALS=%d\nABI=%d\n",
8025 XSHAL_USE_ABSOLUTE_LITERALS
, XSHAL_ABI
);
8026 sz
= strlen (data
) + 1;
8028 /* Add enough null terminators to pad to a word boundary. */
8031 while ((sz
& 3) != 0);
8033 /* Follow the standard note section layout:
8034 First write the length of the name string. */
8036 md_number_to_chars (p
, (valueT
) XTINFO_NAMESZ
, 4);
8038 /* Next comes the length of the "descriptor", i.e., the actual data. */
8040 md_number_to_chars (p
, (valueT
) sz
, 4);
8042 /* Write the note type. */
8044 md_number_to_chars (p
, (valueT
) XTINFO_TYPE
, 4);
8046 /* Write the name field. */
8047 p
= frag_more (XTINFO_NAMESZ
);
8048 memcpy (p
, XTINFO_NAME
, XTINFO_NAMESZ
);
8050 /* Finally, write the descriptor. */
8052 memcpy (p
, data
, sz
);
8058 /* Alignment Functions. */
8061 get_text_align_power (unsigned target_size
)
8063 if (target_size
<= 4)
8065 assert (target_size
== 8);
8071 get_text_align_max_fill_size (int align_pow
,
8072 bfd_boolean use_nops
,
8073 bfd_boolean use_no_density
)
8076 return (1 << align_pow
);
8078 return 3 * (1 << align_pow
);
8080 return 1 + (1 << align_pow
);
8084 /* Calculate the minimum bytes of fill needed at "address" to align a
8085 target instruction of size "target_size" so that it does not cross a
8086 power-of-two boundary specified by "align_pow". If "use_nops" is FALSE,
8087 the fill can be an arbitrary number of bytes. Otherwise, the space must
8088 be filled by NOP instructions. */
8091 get_text_align_fill_size (addressT address
,
8094 bfd_boolean use_nops
,
8095 bfd_boolean use_no_density
)
8097 addressT alignment
, fill
, fill_limit
, fill_step
;
8098 bfd_boolean skip_one
= FALSE
;
8100 alignment
= (1 << align_pow
);
8101 assert (target_size
> 0 && alignment
>= (addressT
) target_size
);
8105 fill_limit
= alignment
;
8108 else if (!use_no_density
)
8110 /* Combine 2- and 3-byte NOPs to fill anything larger than one. */
8111 fill_limit
= alignment
* 2;
8117 /* Fill with 3-byte NOPs -- can only fill multiples of 3. */
8118 fill_limit
= alignment
* 3;
8122 /* Try all fill sizes until finding one that works. */
8123 for (fill
= 0; fill
< fill_limit
; fill
+= fill_step
)
8125 if (skip_one
&& fill
== 1)
8127 if ((address
+ fill
) >> align_pow
8128 == (address
+ fill
+ target_size
- 1) >> align_pow
)
8137 branch_align_power (segT sec
)
8139 /* If the Xtensa processor has a fetch width of 8 bytes, and the section
8140 is aligned to at least an 8-byte boundary, then a branch target need
8141 only fit within an 8-byte aligned block of memory to avoid a stall.
8142 Otherwise, try to fit branch targets within 4-byte aligned blocks
8143 (which may be insufficient, e.g., if the section has no alignment, but
8144 it's good enough). */
8145 if (xtensa_fetch_width
== 8)
8147 if (get_recorded_alignment (sec
) >= 3)
8151 assert (xtensa_fetch_width
== 4);
8157 /* This will assert if it is not possible. */
8160 get_text_align_nop_count (offsetT fill_size
, bfd_boolean use_no_density
)
8166 assert (fill_size
% 3 == 0);
8167 return (fill_size
/ 3);
8170 assert (fill_size
!= 1); /* Bad argument. */
8172 while (fill_size
> 1)
8175 if (fill_size
== 2 || fill_size
== 4)
8177 fill_size
-= insn_size
;
8180 assert (fill_size
!= 1); /* Bad algorithm. */
8186 get_text_align_nth_nop_size (offsetT fill_size
,
8188 bfd_boolean use_no_density
)
8195 assert (fill_size
!= 1); /* Bad argument. */
8197 while (fill_size
> 1)
8200 if (fill_size
== 2 || fill_size
== 4)
8202 fill_size
-= insn_size
;
8212 /* For the given fragment, find the appropriate address
8213 for it to begin at if we are using NOPs to align it. */
8216 get_noop_aligned_address (fragS
*fragP
, addressT address
)
8218 /* The rule is: get next fragment's FIRST instruction. Find
8219 the smallest number of bytes that need to be added to
8220 ensure that the next fragment's FIRST instruction will fit
8223 E.G., 2 bytes : 0, 1, 2 mod 4
8226 If the FIRST instruction MIGHT be relaxed,
8227 assume that it will become a 3-byte instruction.
8229 Note again here that LOOP instructions are not bundleable,
8230 and this relaxation only applies to LOOP opcodes. */
8233 int first_insn_size
;
8235 addressT pre_opcode_bytes
;
8238 xtensa_opcode opcode
;
8239 bfd_boolean is_loop
;
8241 assert (fragP
->fr_type
== rs_machine_dependent
);
8242 assert (fragP
->fr_subtype
== RELAX_ALIGN_NEXT_OPCODE
);
8244 /* Find the loop frag. */
8245 first_insn
= next_non_empty_frag (fragP
);
8246 /* Now find the first insn frag. */
8247 first_insn
= next_non_empty_frag (first_insn
);
8249 is_loop
= next_frag_opcode_is_loop (fragP
, &opcode
);
8251 loop_insn_size
= xg_get_single_size (opcode
);
8253 pre_opcode_bytes
= next_frag_pre_opcode_bytes (fragP
);
8254 pre_opcode_bytes
+= loop_insn_size
;
8256 /* For loops, the alignment depends on the size of the
8257 instruction following the loop, not the LOOP instruction. */
8259 if (first_insn
== NULL
)
8260 first_insn_size
= xtensa_fetch_width
;
8262 first_insn_size
= get_loop_align_size (frag_format_size (first_insn
));
8264 /* If it was 8, then we'll need a larger alignment for the section. */
8265 align_power
= get_text_align_power (first_insn_size
);
8266 record_alignment (now_seg
, align_power
);
8268 fill_size
= get_text_align_fill_size
8269 (address
+ pre_opcode_bytes
, align_power
, first_insn_size
, TRUE
,
8270 fragP
->tc_frag_data
.is_no_density
);
8272 return address
+ fill_size
;
8276 /* 3 mechanisms for relaxing an alignment:
8278 Align to a power of 2.
8279 Align so the next fragment's instruction does not cross a word boundary.
8280 Align the current instruction so that if the next instruction
8281 were 3 bytes, it would not cross a word boundary.
8285 zeros - This is easy; always insert zeros.
8286 nops - 3-byte and 2-byte instructions
8290 >=5 : 3-byte instruction + fn (n-3)
8291 widening - widen previous instructions. */
8294 get_aligned_diff (fragS
*fragP
, addressT address
, offsetT
*max_diff
)
8296 addressT target_address
, loop_insn_offset
;
8298 xtensa_opcode loop_opcode
;
8299 bfd_boolean is_loop
;
8302 offsetT branch_align
;
8305 assert (fragP
->fr_type
== rs_machine_dependent
);
8306 switch (fragP
->fr_subtype
)
8308 case RELAX_DESIRE_ALIGN
:
8309 target_size
= next_frag_format_size (fragP
);
8310 if (target_size
== XTENSA_UNDEFINED
)
8312 align_power
= branch_align_power (now_seg
);
8313 branch_align
= 1 << align_power
;
8314 /* Don't count on the section alignment being as large as the target. */
8315 if (target_size
> branch_align
)
8316 target_size
= branch_align
;
8317 opt_diff
= get_text_align_fill_size (address
, align_power
,
8318 target_size
, FALSE
, FALSE
);
8320 *max_diff
= (opt_diff
+ branch_align
8321 - (target_size
+ ((address
+ opt_diff
) % branch_align
)));
8322 assert (*max_diff
>= opt_diff
);
8325 case RELAX_ALIGN_NEXT_OPCODE
:
8326 /* The next non-empty frag after this one holds the LOOP instruction
8327 that needs to be aligned. The required alignment depends on the
8328 size of the next non-empty frag after the loop frag, i.e., the
8329 first instruction in the loop. */
8330 loop_frag
= next_non_empty_frag (fragP
);
8331 target_size
= get_loop_align_size (next_frag_format_size (loop_frag
));
8332 loop_insn_offset
= 0;
8333 is_loop
= next_frag_opcode_is_loop (fragP
, &loop_opcode
);
8336 /* If the loop has been expanded then the LOOP instruction
8337 could be at an offset from this fragment. */
8338 if (loop_frag
->tc_frag_data
.slot_subtypes
[0] != RELAX_IMMED
)
8339 loop_insn_offset
= get_expanded_loop_offset (loop_opcode
);
8341 /* In an ideal world, which is what we are shooting for here,
8342 we wouldn't need to use any NOPs immediately prior to the
8343 LOOP instruction. If this approach fails, relax_frag_loop_align
8344 will call get_noop_aligned_address. */
8346 address
+ loop_insn_offset
+ xg_get_single_size (loop_opcode
);
8347 align_power
= get_text_align_power (target_size
);
8348 opt_diff
= get_text_align_fill_size (target_address
, align_power
,
8349 target_size
, FALSE
, FALSE
);
8351 *max_diff
= xtensa_fetch_width
8352 - ((target_address
+ opt_diff
) % xtensa_fetch_width
)
8353 - target_size
+ opt_diff
;
8354 assert (*max_diff
>= opt_diff
);
8365 /* md_relax_frag Hook and Helper Functions. */
8367 static long relax_frag_loop_align (fragS
*, long);
8368 static long relax_frag_for_align (fragS
*, long);
8369 static long relax_frag_immed
8370 (segT
, fragS
*, long, int, xtensa_format
, int, int *, bfd_boolean
);
8373 /* Return the number of bytes added to this fragment, given that the
8374 input has been stretched already by "stretch". */
8377 xtensa_relax_frag (fragS
*fragP
, long stretch
, int *stretched_p
)
8379 xtensa_isa isa
= xtensa_default_isa
;
8380 int unreported
= fragP
->tc_frag_data
.unreported_expansion
;
8381 long new_stretch
= 0;
8385 static xtensa_insnbuf vbuf
= NULL
;
8386 int slot
, num_slots
;
8389 as_where (&file_name
, &line
);
8390 new_logical_line (fragP
->fr_file
, fragP
->fr_line
);
8392 fragP
->tc_frag_data
.unreported_expansion
= 0;
8394 switch (fragP
->fr_subtype
)
8396 case RELAX_ALIGN_NEXT_OPCODE
:
8397 /* Always convert. */
8398 if (fragP
->tc_frag_data
.relax_seen
)
8399 new_stretch
= relax_frag_loop_align (fragP
, stretch
);
8402 case RELAX_LOOP_END
:
8406 case RELAX_LOOP_END_ADD_NOP
:
8407 /* Add a NOP and switch to .fill 0. */
8408 new_stretch
= relax_frag_add_nop (fragP
);
8412 case RELAX_DESIRE_ALIGN
:
8413 /* Do nothing. The narrowing before this frag will either align
8418 case RELAX_LITERAL_FINAL
:
8421 case RELAX_LITERAL_NR
:
8423 fragP
->fr_subtype
= RELAX_LITERAL_FINAL
;
8424 assert (unreported
== lit_size
);
8425 memset (&fragP
->fr_literal
[fragP
->fr_fix
], 0, 4);
8426 fragP
->fr_var
-= lit_size
;
8427 fragP
->fr_fix
+= lit_size
;
8433 vbuf
= xtensa_insnbuf_alloc (isa
);
8435 xtensa_insnbuf_from_chars
8436 (isa
, vbuf
, (unsigned char *) fragP
->fr_opcode
, 0);
8437 fmt
= xtensa_format_decode (isa
, vbuf
);
8438 num_slots
= xtensa_format_num_slots (isa
, fmt
);
8440 for (slot
= 0; slot
< num_slots
; slot
++)
8442 switch (fragP
->tc_frag_data
.slot_subtypes
[slot
])
8445 if (fragP
->tc_frag_data
.relax_seen
)
8446 new_stretch
+= relax_frag_for_align (fragP
, stretch
);
8450 case RELAX_IMMED_STEP1
:
8451 case RELAX_IMMED_STEP2
:
8452 case RELAX_IMMED_STEP3
:
8453 /* Place the immediate. */
8454 new_stretch
+= relax_frag_immed
8455 (now_seg
, fragP
, stretch
,
8456 fragP
->tc_frag_data
.slot_subtypes
[slot
] - RELAX_IMMED
,
8457 fmt
, slot
, stretched_p
, FALSE
);
8461 /* This is OK; see the note in xg_assemble_vliw_tokens. */
8467 case RELAX_LITERAL_POOL_BEGIN
:
8468 case RELAX_LITERAL_POOL_END
:
8469 case RELAX_MAYBE_UNREACHABLE
:
8470 case RELAX_MAYBE_DESIRE_ALIGN
:
8471 /* No relaxation required. */
8474 case RELAX_FILL_NOP
:
8475 case RELAX_UNREACHABLE
:
8476 if (fragP
->tc_frag_data
.relax_seen
)
8477 new_stretch
+= relax_frag_for_align (fragP
, stretch
);
8481 as_bad (_("bad relaxation state"));
8484 /* Tell gas we need another relaxation pass. */
8485 if (! fragP
->tc_frag_data
.relax_seen
)
8487 fragP
->tc_frag_data
.relax_seen
= TRUE
;
8491 new_logical_line (file_name
, line
);
8497 relax_frag_loop_align (fragS
*fragP
, long stretch
)
8499 addressT old_address
, old_next_address
, old_size
;
8500 addressT new_address
, new_next_address
, new_size
;
8503 /* All the frags with relax_frag_for_alignment prior to this one in the
8504 section have been done, hopefully eliminating the need for a NOP here.
8505 But, this will put it in if necessary. */
8507 /* Calculate the old address of this fragment and the next fragment. */
8508 old_address
= fragP
->fr_address
- stretch
;
8509 old_next_address
= (fragP
->fr_address
- stretch
+ fragP
->fr_fix
+
8510 fragP
->tc_frag_data
.text_expansion
[0]);
8511 old_size
= old_next_address
- old_address
;
8513 /* Calculate the new address of this fragment and the next fragment. */
8514 new_address
= fragP
->fr_address
;
8516 get_noop_aligned_address (fragP
, fragP
->fr_address
+ fragP
->fr_fix
);
8517 new_size
= new_next_address
- new_address
;
8519 growth
= new_size
- old_size
;
8521 /* Fix up the text_expansion field and return the new growth. */
8522 fragP
->tc_frag_data
.text_expansion
[0] += growth
;
8527 /* Add a NOP instruction. */
8530 relax_frag_add_nop (fragS
*fragP
)
8532 char *nop_buf
= fragP
->fr_literal
+ fragP
->fr_fix
;
8533 int length
= fragP
->tc_frag_data
.is_no_density
? 3 : 2;
8534 assemble_nop (length
, nop_buf
);
8535 fragP
->tc_frag_data
.is_insn
= TRUE
;
8537 if (fragP
->fr_var
< length
)
8539 as_fatal (_("fr_var (%ld) < length (%d)"), (long) fragP
->fr_var
, length
);
8543 fragP
->fr_fix
+= length
;
8544 fragP
->fr_var
-= length
;
8549 static long future_alignment_required (fragS
*, long);
8552 relax_frag_for_align (fragS
*fragP
, long stretch
)
8554 /* Overview of the relaxation procedure for alignment:
8555 We can widen with NOPs or by widening instructions or by filling
8556 bytes after jump instructions. Find the opportune places and widen
8557 them if necessary. */
8562 assert (fragP
->fr_subtype
== RELAX_FILL_NOP
8563 || fragP
->fr_subtype
== RELAX_UNREACHABLE
8564 || (fragP
->fr_subtype
== RELAX_SLOTS
8565 && fragP
->tc_frag_data
.slot_subtypes
[0] == RELAX_NARROW
));
8567 stretch_me
= future_alignment_required (fragP
, stretch
);
8568 diff
= stretch_me
- fragP
->tc_frag_data
.text_expansion
[0];
8574 /* We expanded on a previous pass. Can we shrink now? */
8575 long shrink
= fragP
->tc_frag_data
.text_expansion
[0] - stretch_me
;
8576 if (shrink
<= stretch
&& stretch
> 0)
8578 fragP
->tc_frag_data
.text_expansion
[0] = stretch_me
;
8584 /* Below here, diff > 0. */
8585 fragP
->tc_frag_data
.text_expansion
[0] = stretch_me
;
8591 /* Return the address of the next frag that should be aligned.
8593 By "address" we mean the address it _would_ be at if there
8594 is no action taken to align it between here and the target frag.
8595 In other words, if no narrows and no fill nops are used between
8596 here and the frag to align, _even_if_ some of the frags we use
8597 to align targets have already expanded on a previous relaxation
8600 Also, count each frag that may be used to help align the target.
8602 Return 0 if there are no frags left in the chain that need to be
8606 find_address_of_next_align_frag (fragS
**fragPP
,
8610 bfd_boolean
*paddable
)
8612 fragS
*fragP
= *fragPP
;
8613 addressT address
= fragP
->fr_address
;
8615 /* Do not reset the counts to 0. */
8619 /* Limit this to a small search. */
8620 if (*widens
>= (int) xtensa_fetch_width
)
8625 address
+= fragP
->fr_fix
;
8627 if (fragP
->fr_type
== rs_fill
)
8628 address
+= fragP
->fr_offset
* fragP
->fr_var
;
8629 else if (fragP
->fr_type
== rs_machine_dependent
)
8631 switch (fragP
->fr_subtype
)
8633 case RELAX_UNREACHABLE
:
8637 case RELAX_FILL_NOP
:
8639 if (!fragP
->tc_frag_data
.is_no_density
)
8644 if (fragP
->tc_frag_data
.slot_subtypes
[0] == RELAX_NARROW
)
8649 address
+= total_frag_text_expansion (fragP
);;
8653 address
+= fragP
->tc_frag_data
.text_expansion
[0];
8656 case RELAX_ALIGN_NEXT_OPCODE
:
8657 case RELAX_DESIRE_ALIGN
:
8661 case RELAX_MAYBE_UNREACHABLE
:
8662 case RELAX_MAYBE_DESIRE_ALIGN
:
8667 /* Just punt if we don't know the type. */
8674 /* Just punt if we don't know the type. */
8678 fragP
= fragP
->fr_next
;
8686 static long bytes_to_stretch (fragS
*, int, int, int, int);
8689 future_alignment_required (fragS
*fragP
, long stretch ATTRIBUTE_UNUSED
)
8691 fragS
*this_frag
= fragP
;
8695 int narrow_nops
= 0;
8696 bfd_boolean paddable
= FALSE
;
8697 offsetT local_opt_diff
;
8700 int stretch_amount
= 0;
8701 int local_stretch_amount
;
8702 int global_stretch_amount
;
8704 address
= find_address_of_next_align_frag
8705 (&fragP
, &wide_nops
, &narrow_nops
, &num_widens
, &paddable
);
8709 if (this_frag
->tc_frag_data
.is_aligning_branch
)
8710 this_frag
->tc_frag_data
.slot_subtypes
[0] = RELAX_IMMED
;
8712 frag_wane (this_frag
);
8716 local_opt_diff
= get_aligned_diff (fragP
, address
, &max_diff
);
8717 opt_diff
= local_opt_diff
;
8718 assert (opt_diff
>= 0);
8719 assert (max_diff
>= opt_diff
);
8724 fragP
= fragP
->fr_next
;
8726 while (fragP
&& opt_diff
< max_diff
&& address
)
8728 /* We only use these to determine if we can exit early
8729 because there will be plenty of ways to align future
8731 int glob_widens
= 0;
8734 bfd_boolean glob_pad
= 0;
8735 address
= find_address_of_next_align_frag
8736 (&fragP
, &glob_widens
, &dnn
, &dw
, &glob_pad
);
8737 /* If there is a padable portion, then skip. */
8738 if (glob_pad
|| glob_widens
>= (1 << branch_align_power (now_seg
)))
8743 offsetT next_m_diff
;
8744 offsetT next_o_diff
;
8746 /* Downrange frags haven't had stretch added to them yet. */
8749 /* The address also includes any text expansion from this
8750 frag in a previous pass, but we don't want that. */
8751 address
-= this_frag
->tc_frag_data
.text_expansion
[0];
8753 /* Assume we are going to move at least opt_diff. In
8754 reality, we might not be able to, but assuming that
8755 we will helps catch cases where moving opt_diff pushes
8756 the next target from aligned to unaligned. */
8757 address
+= opt_diff
;
8759 next_o_diff
= get_aligned_diff (fragP
, address
, &next_m_diff
);
8761 /* Now cleanup for the adjustments to address. */
8762 next_o_diff
+= opt_diff
;
8763 next_m_diff
+= opt_diff
;
8764 if (next_o_diff
<= max_diff
&& next_o_diff
> opt_diff
)
8765 opt_diff
= next_o_diff
;
8766 if (next_m_diff
< max_diff
)
8767 max_diff
= next_m_diff
;
8768 fragP
= fragP
->fr_next
;
8772 /* If there are enough wideners in between, do it. */
8775 if (this_frag
->fr_subtype
== RELAX_UNREACHABLE
)
8777 assert (opt_diff
<= UNREACHABLE_MAX_WIDTH
);
8782 local_stretch_amount
8783 = bytes_to_stretch (this_frag
, wide_nops
, narrow_nops
,
8784 num_widens
, local_opt_diff
);
8785 global_stretch_amount
8786 = bytes_to_stretch (this_frag
, wide_nops
, narrow_nops
,
8787 num_widens
, opt_diff
);
8788 /* If the condition below is true, then the frag couldn't
8789 stretch the correct amount for the global case, so we just
8790 optimize locally. We'll rely on the subsequent frags to get
8791 the correct alignment in the global case. */
8792 if (global_stretch_amount
< local_stretch_amount
)
8793 stretch_amount
= local_stretch_amount
;
8795 stretch_amount
= global_stretch_amount
;
8797 if (this_frag
->fr_subtype
== RELAX_SLOTS
8798 && this_frag
->tc_frag_data
.slot_subtypes
[0] == RELAX_NARROW
)
8799 assert (stretch_amount
<= 1);
8800 else if (this_frag
->fr_subtype
== RELAX_FILL_NOP
)
8802 if (this_frag
->tc_frag_data
.is_no_density
)
8803 assert (stretch_amount
== 3 || stretch_amount
== 0);
8805 assert (stretch_amount
<= 3);
8808 return stretch_amount
;
8812 /* The idea: widen everything you can to get a target or loop aligned,
8813 then start using NOPs.
8815 When we must have a NOP, here is a table of how we decide
8816 (so you don't have to fight through the control flow below):
8818 wide_nops = the number of wide NOPs available for aligning
8819 narrow_nops = the number of narrow NOPs available for aligning
8820 (a subset of wide_nops)
8821 widens = the number of narrow instructions that should be widened
8828 b 0 1 1 (case 3a makes this case unnecessary)
8831 c 0 1 2 (case 4a makes this case unnecessary)
8834 c 0 2 1 (case 5b makes this case unnecessary)
8837 c 0 1 4 (case 6b makes this case unnecessary)
8838 d 1 1 1 (case 6a makes this case unnecessary)
8839 e 0 2 2 (case 6a makes this case unnecessary)
8840 f 0 3 0 (case 6a makes this case unnecessary)
8843 c 1 1 2 (case 7b makes this case unnecessary)
8844 d 0 1 5 (case 7a makes this case unnecessary)
8845 e 0 2 3 (case 7b makes this case unnecessary)
8846 f 0 3 1 (case 7b makes this case unnecessary)
8847 g 1 2 1 (case 7b makes this case unnecessary)
8851 bytes_to_stretch (fragS
*this_frag
,
8857 int bytes_short
= desired_diff
- num_widens
;
8859 assert (desired_diff
>= 0 && desired_diff
< 8);
8860 if (desired_diff
== 0)
8863 assert (wide_nops
> 0 || num_widens
> 0);
8865 /* Always prefer widening to NOP-filling. */
8866 if (bytes_short
< 0)
8868 /* There are enough RELAX_NARROW frags after this one
8869 to align the target without widening this frag in any way. */
8873 if (bytes_short
== 0)
8875 /* Widen every narrow between here and the align target
8876 and the align target will be properly aligned. */
8877 if (this_frag
->fr_subtype
== RELAX_FILL_NOP
)
8883 /* From here we will need at least one NOP to get an alignment.
8884 However, we may not be able to align at all, in which case,
8886 if (this_frag
->fr_subtype
== RELAX_FILL_NOP
)
8888 switch (desired_diff
)
8893 if (!this_frag
->tc_frag_data
.is_no_density
&& narrow_nops
== 1)
8894 return 2; /* case 2 */
8900 return 3; /* case 3a */
8902 if (num_widens
>= 1 && wide_nops
== 1)
8903 return 3; /* case 4a */
8904 if (!this_frag
->tc_frag_data
.is_no_density
&& narrow_nops
== 2)
8905 return 2; /* case 4b */
8908 if (num_widens
>= 2 && wide_nops
== 1)
8909 return 3; /* case 5a */
8910 /* We will need two nops. Are there enough nops
8911 between here and the align target? */
8912 if (wide_nops
< 2 || narrow_nops
== 0)
8914 /* Are there other nops closer that can serve instead? */
8915 if (wide_nops
> 2 && narrow_nops
> 1)
8917 /* Take the density one first, because there might not be
8918 another density one available. */
8919 if (!this_frag
->tc_frag_data
.is_no_density
)
8920 return 2; /* case 5b narrow */
8922 return 3; /* case 5b wide */
8926 return 3; /* case 6a */
8927 else if (num_widens
>= 3 && wide_nops
== 1)
8928 return 3; /* case 6b */
8931 if (wide_nops
== 1 && num_widens
>= 4)
8932 return 3; /* case 7a */
8933 else if (wide_nops
== 2 && num_widens
>= 1)
8934 return 3; /* case 7b */
8942 /* We will need a NOP no matter what, but should we widen
8943 this instruction to help?
8945 This is a RELAX_NARROW frag. */
8946 switch (desired_diff
)
8955 if (wide_nops
>= 1 && num_widens
== 1)
8956 return 1; /* case 4a */
8959 if (wide_nops
>= 1 && num_widens
== 2)
8960 return 1; /* case 5a */
8964 return 0; /* case 6a */
8965 else if (wide_nops
>= 1 && num_widens
== 3)
8966 return 1; /* case 6b */
8969 if (wide_nops
>= 1 && num_widens
== 4)
8970 return 1; /* case 7a */
8971 else if (wide_nops
>= 2 && num_widens
== 1)
8972 return 1; /* case 7b */
8985 relax_frag_immed (segT segP
,
8992 bfd_boolean estimate_only
)
8996 bfd_boolean negatable_branch
= FALSE
;
8997 bfd_boolean branch_jmp_to_next
= FALSE
;
8998 bfd_boolean from_wide_insn
= FALSE
;
8999 xtensa_isa isa
= xtensa_default_isa
;
9001 offsetT frag_offset
;
9003 int num_text_bytes
, num_literal_bytes
;
9004 int literal_diff
, total_text_diff
, this_text_diff
;
9006 assert (fragP
->fr_opcode
!= NULL
);
9008 xg_clear_vinsn (&cur_vinsn
);
9009 vinsn_from_chars (&cur_vinsn
, fragP
->fr_opcode
);
9010 if (cur_vinsn
.num_slots
> 1)
9011 from_wide_insn
= TRUE
;
9013 tinsn
= cur_vinsn
.slots
[slot
];
9014 tinsn_immed_from_frag (&tinsn
, fragP
, slot
);
9016 if (estimate_only
&& xtensa_opcode_is_loop (isa
, tinsn
.opcode
) == 1)
9019 if (workaround_b_j_loop_end
&& ! fragP
->tc_frag_data
.is_no_transform
)
9020 branch_jmp_to_next
= is_branch_jmp_to_next (&tinsn
, fragP
);
9022 negatable_branch
= (xtensa_opcode_is_branch (isa
, tinsn
.opcode
) == 1);
9024 old_size
= xtensa_format_length (isa
, fmt
);
9026 /* Special case: replace a branch to the next instruction with a NOP.
9027 This is required to work around a hardware bug in T1040.0 and also
9028 serves as an optimization. */
9030 if (branch_jmp_to_next
9031 && ((old_size
== 2) || (old_size
== 3))
9032 && !next_frag_is_loop_target (fragP
))
9035 /* Here is the fun stuff: Get the immediate field from this
9036 instruction. If it fits, we are done. If not, find the next
9037 instruction sequence that fits. */
9039 frag_offset
= fragP
->fr_opcode
- fragP
->fr_literal
;
9040 istack_init (&istack
);
9041 num_steps
= xg_assembly_relax (&istack
, &tinsn
, segP
, fragP
, frag_offset
,
9042 min_steps
, stretch
);
9043 assert (num_steps
>= min_steps
&& num_steps
<= RELAX_IMMED_MAXSTEPS
);
9045 fragP
->tc_frag_data
.slot_subtypes
[slot
] = (int) RELAX_IMMED
+ num_steps
;
9047 /* Figure out the number of bytes needed. */
9048 num_literal_bytes
= get_num_stack_literal_bytes (&istack
);
9050 = num_literal_bytes
- fragP
->tc_frag_data
.literal_expansion
[slot
];
9051 num_text_bytes
= get_num_stack_text_bytes (&istack
);
9056 while (istack
.insn
[first
].opcode
== XTENSA_UNDEFINED
)
9059 num_text_bytes
+= old_size
;
9060 if (opcode_fits_format_slot (istack
.insn
[first
].opcode
, fmt
, slot
))
9061 num_text_bytes
-= xg_get_single_size (istack
.insn
[first
].opcode
);
9064 /* The first instruction in the relaxed sequence will go after
9065 the current wide instruction, and thus its symbolic immediates
9068 istack_init (&istack
);
9069 num_steps
= xg_assembly_relax (&istack
, &tinsn
, segP
, fragP
,
9070 frag_offset
+ old_size
,
9071 min_steps
, stretch
+ old_size
);
9072 assert (num_steps
>= min_steps
&& num_steps
<= RELAX_IMMED_MAXSTEPS
);
9074 fragP
->tc_frag_data
.slot_subtypes
[slot
]
9075 = (int) RELAX_IMMED
+ num_steps
;
9077 num_literal_bytes
= get_num_stack_literal_bytes (&istack
);
9079 = num_literal_bytes
- fragP
->tc_frag_data
.literal_expansion
[slot
];
9081 num_text_bytes
= get_num_stack_text_bytes (&istack
) + old_size
;
9085 total_text_diff
= num_text_bytes
- old_size
;
9086 this_text_diff
= total_text_diff
- fragP
->tc_frag_data
.text_expansion
[slot
];
9088 /* It MUST get larger. If not, we could get an infinite loop. */
9089 assert (num_text_bytes
>= 0);
9090 assert (literal_diff
>= 0);
9091 assert (total_text_diff
>= 0);
9093 fragP
->tc_frag_data
.text_expansion
[slot
] = total_text_diff
;
9094 fragP
->tc_frag_data
.literal_expansion
[slot
] = num_literal_bytes
;
9095 assert (fragP
->tc_frag_data
.text_expansion
[slot
] >= 0);
9096 assert (fragP
->tc_frag_data
.literal_expansion
[slot
] >= 0);
9098 /* Find the associated expandable literal for this. */
9099 if (literal_diff
!= 0)
9101 fragS
*lit_fragP
= fragP
->tc_frag_data
.literal_frags
[slot
];
9104 assert (literal_diff
== 4);
9105 lit_fragP
->tc_frag_data
.unreported_expansion
+= literal_diff
;
9107 /* We expect that the literal section state has NOT been
9109 assert (lit_fragP
->fr_type
== rs_machine_dependent
9110 && lit_fragP
->fr_subtype
== RELAX_LITERAL
);
9111 lit_fragP
->fr_subtype
= RELAX_LITERAL_NR
;
9113 /* We need to mark this section for another iteration
9119 if (negatable_branch
&& istack
.ninsn
> 1)
9120 update_next_frag_state (fragP
);
9122 return this_text_diff
;
9126 /* md_convert_frag Hook and Helper Functions. */
9128 static void convert_frag_align_next_opcode (fragS
*);
9129 static void convert_frag_narrow (segT
, fragS
*, xtensa_format
, int);
9130 static void convert_frag_fill_nop (fragS
*);
9131 static void convert_frag_immed (segT
, fragS
*, int, xtensa_format
, int);
9134 md_convert_frag (bfd
*abfd ATTRIBUTE_UNUSED
, segT sec
, fragS
*fragp
)
9136 static xtensa_insnbuf vbuf
= NULL
;
9137 xtensa_isa isa
= xtensa_default_isa
;
9144 as_where (&file_name
, &line
);
9145 new_logical_line (fragp
->fr_file
, fragp
->fr_line
);
9147 switch (fragp
->fr_subtype
)
9149 case RELAX_ALIGN_NEXT_OPCODE
:
9150 /* Always convert. */
9151 convert_frag_align_next_opcode (fragp
);
9154 case RELAX_DESIRE_ALIGN
:
9155 /* Do nothing. If not aligned already, too bad. */
9159 case RELAX_LITERAL_FINAL
:
9164 vbuf
= xtensa_insnbuf_alloc (isa
);
9166 xtensa_insnbuf_from_chars
9167 (isa
, vbuf
, (unsigned char *) fragp
->fr_opcode
, 0);
9168 fmt
= xtensa_format_decode (isa
, vbuf
);
9169 num_slots
= xtensa_format_num_slots (isa
, fmt
);
9171 for (slot
= 0; slot
< num_slots
; slot
++)
9173 switch (fragp
->tc_frag_data
.slot_subtypes
[slot
])
9176 convert_frag_narrow (sec
, fragp
, fmt
, slot
);
9180 case RELAX_IMMED_STEP1
:
9181 case RELAX_IMMED_STEP2
:
9182 case RELAX_IMMED_STEP3
:
9183 /* Place the immediate. */
9186 fragp
->tc_frag_data
.slot_subtypes
[slot
] - RELAX_IMMED
,
9191 /* This is OK because some slots could have
9192 relaxations and others have none. */
9198 case RELAX_UNREACHABLE
:
9199 memset (&fragp
->fr_literal
[fragp
->fr_fix
], 0, fragp
->fr_var
);
9200 fragp
->fr_fix
+= fragp
->tc_frag_data
.text_expansion
[0];
9201 fragp
->fr_var
-= fragp
->tc_frag_data
.text_expansion
[0];
9205 case RELAX_MAYBE_UNREACHABLE
:
9206 case RELAX_MAYBE_DESIRE_ALIGN
:
9210 case RELAX_FILL_NOP
:
9211 convert_frag_fill_nop (fragp
);
9214 case RELAX_LITERAL_NR
:
9215 if (use_literal_section
)
9217 /* This should have been handled during relaxation. When
9218 relaxing a code segment, literals sometimes need to be
9219 added to the corresponding literal segment. If that
9220 literal segment has already been relaxed, then we end up
9221 in this situation. Marking the literal segments as data
9222 would make this happen less often (since GAS always relaxes
9223 code before data), but we could still get into trouble if
9224 there are instructions in a segment that is not marked as
9225 containing code. Until we can implement a better solution,
9226 cheat and adjust the addresses of all the following frags.
9227 This could break subsequent alignments, but the linker's
9228 literal coalescing will do that anyway. */
9231 fragp
->fr_subtype
= RELAX_LITERAL_FINAL
;
9232 assert (fragp
->tc_frag_data
.unreported_expansion
== 4);
9233 memset (&fragp
->fr_literal
[fragp
->fr_fix
], 0, 4);
9236 for (f
= fragp
->fr_next
; f
; f
= f
->fr_next
)
9240 as_bad (_("invalid relaxation fragment result"));
9245 new_logical_line (file_name
, line
);
9250 convert_frag_align_next_opcode (fragS
*fragp
)
9252 char *nop_buf
; /* Location for Writing. */
9253 bfd_boolean use_no_density
= fragp
->tc_frag_data
.is_no_density
;
9254 addressT aligned_address
;
9258 aligned_address
= get_noop_aligned_address (fragp
, fragp
->fr_address
+
9260 fill_size
= aligned_address
- (fragp
->fr_address
+ fragp
->fr_fix
);
9261 nop_count
= get_text_align_nop_count (fill_size
, use_no_density
);
9262 nop_buf
= fragp
->fr_literal
+ fragp
->fr_fix
;
9264 for (nop
= 0; nop
< nop_count
; nop
++)
9267 nop_size
= get_text_align_nth_nop_size (fill_size
, nop
, use_no_density
);
9269 assemble_nop (nop_size
, nop_buf
);
9270 nop_buf
+= nop_size
;
9273 fragp
->fr_fix
+= fill_size
;
9274 fragp
->fr_var
-= fill_size
;
9279 convert_frag_narrow (segT segP
, fragS
*fragP
, xtensa_format fmt
, int slot
)
9281 TInsn tinsn
, single_target
;
9282 int size
, old_size
, diff
;
9283 offsetT frag_offset
;
9286 tinsn_from_chars (&tinsn
, fragP
->fr_opcode
, 0);
9288 if (fragP
->tc_frag_data
.is_aligning_branch
== 1)
9290 assert (fragP
->tc_frag_data
.text_expansion
[0] == 1
9291 || fragP
->tc_frag_data
.text_expansion
[0] == 0);
9292 convert_frag_immed (segP
, fragP
, fragP
->tc_frag_data
.text_expansion
[0],
9297 if (fragP
->tc_frag_data
.text_expansion
[0] == 0)
9299 /* No conversion. */
9304 assert (fragP
->fr_opcode
!= NULL
);
9306 /* Frags in this relaxation state should only contain
9307 single instruction bundles. */
9308 tinsn_immed_from_frag (&tinsn
, fragP
, 0);
9310 /* Just convert it to a wide form.... */
9312 old_size
= xg_get_single_size (tinsn
.opcode
);
9314 tinsn_init (&single_target
);
9315 frag_offset
= fragP
->fr_opcode
- fragP
->fr_literal
;
9317 if (! xg_is_single_relaxable_insn (&tinsn
, &single_target
, FALSE
))
9319 as_bad (_("unable to widen instruction"));
9323 size
= xg_get_single_size (single_target
.opcode
);
9324 xg_emit_insn_to_buf (&single_target
, fragP
->fr_opcode
, fragP
,
9327 diff
= size
- old_size
;
9329 assert (diff
<= fragP
->fr_var
);
9330 fragP
->fr_var
-= diff
;
9331 fragP
->fr_fix
+= diff
;
9339 convert_frag_fill_nop (fragS
*fragP
)
9341 char *loc
= &fragP
->fr_literal
[fragP
->fr_fix
];
9342 int size
= fragP
->tc_frag_data
.text_expansion
[0];
9343 assert ((unsigned) size
== (fragP
->fr_next
->fr_address
9344 - fragP
->fr_address
- fragP
->fr_fix
));
9347 /* No conversion. */
9351 assemble_nop (size
, loc
);
9352 fragP
->tc_frag_data
.is_insn
= TRUE
;
9353 fragP
->fr_var
-= size
;
9354 fragP
->fr_fix
+= size
;
9359 static fixS
*fix_new_exp_in_seg
9360 (segT
, subsegT
, fragS
*, int, int, expressionS
*, int,
9361 bfd_reloc_code_real_type
);
9362 static void convert_frag_immed_finish_loop (segT
, fragS
*, TInsn
*);
9365 convert_frag_immed (segT segP
,
9371 char *immed_instr
= fragP
->fr_opcode
;
9373 bfd_boolean expanded
= FALSE
;
9374 bfd_boolean branch_jmp_to_next
= FALSE
;
9375 char *fr_opcode
= fragP
->fr_opcode
;
9376 xtensa_isa isa
= xtensa_default_isa
;
9377 bfd_boolean from_wide_insn
= FALSE
;
9379 bfd_boolean is_loop
;
9381 assert (fr_opcode
!= NULL
);
9383 xg_clear_vinsn (&cur_vinsn
);
9385 vinsn_from_chars (&cur_vinsn
, fr_opcode
);
9386 if (cur_vinsn
.num_slots
> 1)
9387 from_wide_insn
= TRUE
;
9389 orig_tinsn
= cur_vinsn
.slots
[slot
];
9390 tinsn_immed_from_frag (&orig_tinsn
, fragP
, slot
);
9392 is_loop
= xtensa_opcode_is_loop (xtensa_default_isa
, orig_tinsn
.opcode
) == 1;
9394 if (workaround_b_j_loop_end
&& ! fragP
->tc_frag_data
.is_no_transform
)
9395 branch_jmp_to_next
= is_branch_jmp_to_next (&orig_tinsn
, fragP
);
9397 if (branch_jmp_to_next
&& !next_frag_is_loop_target (fragP
))
9399 /* Conversion just inserts a NOP and marks the fix as completed. */
9400 bytes
= xtensa_format_length (isa
, fmt
);
9403 cur_vinsn
.slots
[slot
].opcode
=
9404 xtensa_format_slot_nop_opcode (isa
, cur_vinsn
.format
, slot
);
9405 cur_vinsn
.slots
[slot
].ntok
= 0;
9409 bytes
+= fragP
->tc_frag_data
.text_expansion
[0];
9410 assert (bytes
== 2 || bytes
== 3);
9411 build_nop (&cur_vinsn
.slots
[0], bytes
);
9412 fragP
->fr_fix
+= fragP
->tc_frag_data
.text_expansion
[0];
9414 vinsn_to_insnbuf (&cur_vinsn
, fr_opcode
, frag_now
, TRUE
);
9415 xtensa_insnbuf_to_chars
9416 (isa
, cur_vinsn
.insnbuf
, (unsigned char *) fr_opcode
, 0);
9421 /* Here is the fun stuff: Get the immediate field from this
9422 instruction. If it fits, we're done. If not, find the next
9423 instruction sequence that fits. */
9427 symbolS
*lit_sym
= NULL
;
9429 int target_offset
= 0;
9432 symbolS
*gen_label
= NULL
;
9433 offsetT frag_offset
;
9434 bfd_boolean first
= TRUE
;
9435 bfd_boolean last_is_jump
;
9437 /* It does not fit. Find something that does and
9438 convert immediately. */
9439 frag_offset
= fr_opcode
- fragP
->fr_literal
;
9440 istack_init (&istack
);
9441 xg_assembly_relax (&istack
, &orig_tinsn
,
9442 segP
, fragP
, frag_offset
, min_steps
, 0);
9444 old_size
= xtensa_format_length (isa
, fmt
);
9446 /* Assemble this right inline. */
9448 /* First, create the mapping from a label name to the REAL label. */
9450 for (i
= 0; i
< istack
.ninsn
; i
++)
9452 TInsn
*tinsn
= &istack
.insn
[i
];
9455 switch (tinsn
->insn_type
)
9458 if (lit_sym
!= NULL
)
9459 as_bad (_("multiple literals in expansion"));
9460 /* First find the appropriate space in the literal pool. */
9461 lit_frag
= fragP
->tc_frag_data
.literal_frags
[slot
];
9462 if (lit_frag
== NULL
)
9463 as_bad (_("no registered fragment for literal"));
9464 if (tinsn
->ntok
!= 1)
9465 as_bad (_("number of literal tokens != 1"));
9467 /* Set the literal symbol and add a fixup. */
9468 lit_sym
= lit_frag
->fr_symbol
;
9472 if (align_targets
&& !is_loop
)
9474 fragS
*unreach
= fragP
->fr_next
;
9475 while (!(unreach
->fr_type
== rs_machine_dependent
9476 && (unreach
->fr_subtype
== RELAX_MAYBE_UNREACHABLE
9477 || unreach
->fr_subtype
== RELAX_UNREACHABLE
)))
9479 unreach
= unreach
->fr_next
;
9482 assert (unreach
->fr_type
== rs_machine_dependent
9483 && (unreach
->fr_subtype
== RELAX_MAYBE_UNREACHABLE
9484 || unreach
->fr_subtype
== RELAX_UNREACHABLE
));
9486 target_offset
+= unreach
->tc_frag_data
.text_expansion
[0];
9488 assert (gen_label
== NULL
);
9489 gen_label
= symbol_new (FAKE_LABEL_NAME
, now_seg
,
9490 fr_opcode
- fragP
->fr_literal
9491 + target_offset
, fragP
);
9495 if (first
&& from_wide_insn
)
9497 target_offset
+= xtensa_format_length (isa
, fmt
);
9499 if (!opcode_fits_format_slot (tinsn
->opcode
, fmt
, slot
))
9500 target_offset
+= xg_get_single_size (tinsn
->opcode
);
9503 target_offset
+= xg_get_single_size (tinsn
->opcode
);
9510 last_is_jump
= FALSE
;
9511 for (i
= 0; i
< istack
.ninsn
; i
++)
9513 TInsn
*tinsn
= &istack
.insn
[i
];
9517 bfd_reloc_code_real_type reloc_type
;
9519 switch (tinsn
->insn_type
)
9522 lit_frag
= fragP
->tc_frag_data
.literal_frags
[slot
];
9523 /* Already checked. */
9524 assert (lit_frag
!= NULL
);
9525 assert (lit_sym
!= NULL
);
9526 assert (tinsn
->ntok
== 1);
9528 target_seg
= S_GET_SEGMENT (lit_sym
);
9529 assert (target_seg
);
9530 reloc_type
= map_operator_to_reloc (tinsn
->tok
[0].X_op
);
9531 fix_new_exp_in_seg (target_seg
, 0, lit_frag
, 0, 4,
9532 &tinsn
->tok
[0], FALSE
, reloc_type
);
9539 xg_resolve_labels (tinsn
, gen_label
);
9540 xg_resolve_literals (tinsn
, lit_sym
);
9541 if (from_wide_insn
&& first
)
9544 if (opcode_fits_format_slot (tinsn
->opcode
, fmt
, slot
))
9546 cur_vinsn
.slots
[slot
] = *tinsn
;
9550 cur_vinsn
.slots
[slot
].opcode
=
9551 xtensa_format_slot_nop_opcode (isa
, fmt
, slot
);
9552 cur_vinsn
.slots
[slot
].ntok
= 0;
9554 vinsn_to_insnbuf (&cur_vinsn
, immed_instr
, fragP
, TRUE
);
9555 xtensa_insnbuf_to_chars (isa
, cur_vinsn
.insnbuf
,
9556 (unsigned char *) immed_instr
, 0);
9557 fragP
->tc_frag_data
.is_insn
= TRUE
;
9558 size
= xtensa_format_length (isa
, fmt
);
9559 if (!opcode_fits_format_slot (tinsn
->opcode
, fmt
, slot
))
9562 (tinsn
, immed_instr
+ size
, fragP
,
9563 immed_instr
- fragP
->fr_literal
+ size
, TRUE
);
9564 size
+= xg_get_single_size (tinsn
->opcode
);
9569 size
= xg_get_single_size (tinsn
->opcode
);
9570 xg_emit_insn_to_buf (tinsn
, immed_instr
, fragP
,
9571 immed_instr
- fragP
->fr_literal
, TRUE
);
9573 immed_instr
+= size
;
9579 diff
= total_size
- old_size
;
9583 assert (diff
<= fragP
->fr_var
);
9584 fragP
->fr_var
-= diff
;
9585 fragP
->fr_fix
+= diff
;
9588 /* Check for undefined immediates in LOOP instructions. */
9592 sym
= orig_tinsn
.tok
[1].X_add_symbol
;
9593 if (sym
!= NULL
&& !S_IS_DEFINED (sym
))
9595 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym
));
9598 sym
= orig_tinsn
.tok
[1].X_op_symbol
;
9599 if (sym
!= NULL
&& !S_IS_DEFINED (sym
))
9601 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym
));
9606 if (expanded
&& xtensa_opcode_is_loop (isa
, orig_tinsn
.opcode
) == 1)
9607 convert_frag_immed_finish_loop (segP
, fragP
, &orig_tinsn
);
9609 if (expanded
&& is_direct_call_opcode (orig_tinsn
.opcode
))
9611 /* Add an expansion note on the expanded instruction. */
9612 fix_new_exp_in_seg (now_seg
, 0, fragP
, fr_opcode
- fragP
->fr_literal
, 4,
9613 &orig_tinsn
.tok
[0], TRUE
,
9614 BFD_RELOC_XTENSA_ASM_EXPAND
);
9619 /* Add a new fix expression into the desired segment. We have to
9620 switch to that segment to do this. */
9623 fix_new_exp_in_seg (segT new_seg
,
9630 bfd_reloc_code_real_type r_type
)
9634 subsegT subseg
= now_subseg
;
9636 assert (new_seg
!= 0);
9637 subseg_set (new_seg
, new_subseg
);
9639 new_fix
= fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
);
9640 subseg_set (seg
, subseg
);
9645 /* Relax a loop instruction so that it can span loop >256 bytes.
9651 addi as, as, lo8 (label-.L1)
9652 addmi as, as, mid8 (label-.L1)
9663 convert_frag_immed_finish_loop (segT segP
, fragS
*fragP
, TInsn
*tinsn
)
9668 unsigned long target
;
9669 static xtensa_insnbuf insnbuf
= NULL
;
9670 unsigned int loop_length
, loop_length_hi
, loop_length_lo
;
9671 xtensa_isa isa
= xtensa_default_isa
;
9672 addressT loop_offset
;
9673 addressT addi_offset
= 9;
9674 addressT addmi_offset
= 12;
9679 insnbuf
= xtensa_insnbuf_alloc (isa
);
9681 /* Get the loop offset. */
9682 loop_offset
= get_expanded_loop_offset (tinsn
->opcode
);
9684 /* Validate that there really is a LOOP at the loop_offset. Because
9685 loops are not bundleable, we can assume that the instruction will be
9687 tinsn_from_chars (&loop_insn
, fragP
->fr_opcode
+ loop_offset
, 0);
9688 tinsn_immed_from_frag (&loop_insn
, fragP
, 0);
9690 assert (xtensa_opcode_is_loop (isa
, loop_insn
.opcode
) == 1);
9691 addi_offset
+= loop_offset
;
9692 addmi_offset
+= loop_offset
;
9694 assert (tinsn
->ntok
== 2);
9695 if (tinsn
->tok
[1].X_op
== O_constant
)
9696 target
= tinsn
->tok
[1].X_add_number
;
9697 else if (tinsn
->tok
[1].X_op
== O_symbol
)
9699 /* Find the fragment. */
9700 symbolS
*sym
= tinsn
->tok
[1].X_add_symbol
;
9701 assert (S_GET_SEGMENT (sym
) == segP
9702 || S_GET_SEGMENT (sym
) == absolute_section
);
9703 target
= (S_GET_VALUE (sym
) + tinsn
->tok
[1].X_add_number
);
9707 as_bad (_("invalid expression evaluation type %d"), tinsn
->tok
[1].X_op
);
9711 loop_length
= target
- (fragP
->fr_address
+ fragP
->fr_fix
);
9712 loop_length_hi
= loop_length
& ~0x0ff;
9713 loop_length_lo
= loop_length
& 0x0ff;
9714 if (loop_length_lo
>= 128)
9716 loop_length_lo
-= 256;
9717 loop_length_hi
+= 256;
9720 /* Because addmi sign-extends the immediate, 'loop_length_hi' can be at most
9721 32512. If the loop is larger than that, then we just fail. */
9722 if (loop_length_hi
> 32512)
9723 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
9724 _("loop too long for LOOP instruction"));
9726 tinsn_from_chars (&addi_insn
, fragP
->fr_opcode
+ addi_offset
, 0);
9727 assert (addi_insn
.opcode
== xtensa_addi_opcode
);
9729 tinsn_from_chars (&addmi_insn
, fragP
->fr_opcode
+ addmi_offset
, 0);
9730 assert (addmi_insn
.opcode
== xtensa_addmi_opcode
);
9732 set_expr_const (&addi_insn
.tok
[2], loop_length_lo
);
9733 tinsn_to_insnbuf (&addi_insn
, insnbuf
);
9735 fragP
->tc_frag_data
.is_insn
= TRUE
;
9736 xtensa_insnbuf_to_chars
9737 (isa
, insnbuf
, (unsigned char *) fragP
->fr_opcode
+ addi_offset
, 0);
9739 set_expr_const (&addmi_insn
.tok
[2], loop_length_hi
);
9740 tinsn_to_insnbuf (&addmi_insn
, insnbuf
);
9741 xtensa_insnbuf_to_chars
9742 (isa
, insnbuf
, (unsigned char *) fragP
->fr_opcode
+ addmi_offset
, 0);
9744 /* Walk through all of the frags from here to the loop end
9745 and mark them as no_transform to keep them from being modified
9746 by the linker. If we ever have a relocation for the
9747 addi/addmi of the difference of two symbols we can remove this. */
9750 for (next_fragP
= fragP
; next_fragP
!= NULL
;
9751 next_fragP
= next_fragP
->fr_next
)
9753 next_fragP
->tc_frag_data
.is_no_transform
= TRUE
;
9754 if (next_fragP
->tc_frag_data
.is_loop_target
)
9756 if (target_count
== 2)
9762 /* A map that keeps information on a per-subsegment basis. This is
9763 maintained during initial assembly, but is invalid once the
9764 subsegments are smashed together. I.E., it cannot be used during
9767 typedef struct subseg_map_struct
9775 float total_freq
; /* fall-through + branch target frequency */
9776 float target_freq
; /* branch target frequency alone */
9778 struct subseg_map_struct
*next
;
9782 static subseg_map
*sseg_map
= NULL
;
9785 get_subseg_info (segT seg
, subsegT subseg
)
9787 subseg_map
*subseg_e
;
9789 for (subseg_e
= sseg_map
; subseg_e
; subseg_e
= subseg_e
->next
)
9791 if (seg
== subseg_e
->seg
&& subseg
== subseg_e
->subseg
)
9799 add_subseg_info (segT seg
, subsegT subseg
)
9801 subseg_map
*subseg_e
= (subseg_map
*) xmalloc (sizeof (subseg_map
));
9802 memset (subseg_e
, 0, sizeof (subseg_map
));
9803 subseg_e
->seg
= seg
;
9804 subseg_e
->subseg
= subseg
;
9805 subseg_e
->flags
= 0;
9806 /* Start off considering every branch target very important. */
9807 subseg_e
->target_freq
= 1.0;
9808 subseg_e
->total_freq
= 1.0;
9809 subseg_e
->next
= sseg_map
;
9810 sseg_map
= subseg_e
;
9816 get_last_insn_flags (segT seg
, subsegT subseg
)
9818 subseg_map
*subseg_e
= get_subseg_info (seg
, subseg
);
9820 return subseg_e
->flags
;
9826 set_last_insn_flags (segT seg
,
9831 subseg_map
*subseg_e
= get_subseg_info (seg
, subseg
);
9833 subseg_e
= add_subseg_info (seg
, subseg
);
9835 subseg_e
->flags
|= fl
;
9837 subseg_e
->flags
&= ~fl
;
9842 get_subseg_total_freq (segT seg
, subsegT subseg
)
9844 subseg_map
*subseg_e
= get_subseg_info (seg
, subseg
);
9846 return subseg_e
->total_freq
;
9852 get_subseg_target_freq (segT seg
, subsegT subseg
)
9854 subseg_map
*subseg_e
= get_subseg_info (seg
, subseg
);
9856 return subseg_e
->target_freq
;
9862 set_subseg_freq (segT seg
, subsegT subseg
, float total_f
, float target_f
)
9864 subseg_map
*subseg_e
= get_subseg_info (seg
, subseg
);
9866 subseg_e
= add_subseg_info (seg
, subseg
);
9867 subseg_e
->total_freq
= total_f
;
9868 subseg_e
->target_freq
= target_f
;
9872 /* Segment Lists and emit_state Stuff. */
9875 xtensa_move_seg_list_to_beginning (seg_list
*head
)
9880 segT literal_section
= head
->seg
;
9882 /* Move the literal section to the front of the section list. */
9883 assert (literal_section
);
9884 if (literal_section
!= stdoutput
->sections
)
9886 bfd_section_list_remove (stdoutput
, literal_section
);
9887 bfd_section_list_prepend (stdoutput
, literal_section
);
9894 static void mark_literal_frags (seg_list
*);
9897 xtensa_move_literals (void)
9900 frchainS
*frchain_from
, *frchain_to
;
9901 fragS
*search_frag
, *next_frag
, *last_frag
, *literal_pool
, *insert_after
;
9902 fragS
**frag_splice
;
9905 fixS
*fix
, *next_fix
, **fix_splice
;
9908 mark_literal_frags (literal_head
->next
);
9910 if (use_literal_section
)
9913 for (segment
= literal_head
->next
; segment
; segment
= segment
->next
)
9915 /* Keep the literals for .init and .fini in separate sections. */
9916 if (!strcmp (segment_name (segment
->seg
), INIT_SECTION_NAME
)
9917 || !strcmp (segment_name (segment
->seg
), FINI_SECTION_NAME
))
9920 frchain_from
= seg_info (segment
->seg
)->frchainP
;
9921 search_frag
= frchain_from
->frch_root
;
9922 literal_pool
= NULL
;
9924 frag_splice
= &(frchain_from
->frch_root
);
9926 while (!search_frag
->tc_frag_data
.literal_frag
)
9928 assert (search_frag
->fr_fix
== 0
9929 || search_frag
->fr_type
== rs_align
);
9930 search_frag
= search_frag
->fr_next
;
9933 assert (search_frag
->tc_frag_data
.literal_frag
->fr_subtype
9934 == RELAX_LITERAL_POOL_BEGIN
);
9935 xtensa_switch_section_emit_state (&state
, segment
->seg
, 0);
9937 /* Make sure that all the frags in this series are closed, and
9938 that there is at least one left over of zero-size. This
9939 prevents us from making a segment with an frchain without any
9941 frag_variant (rs_fill
, 0, 0, 0, NULL
, 0, NULL
);
9942 xtensa_set_frag_assembly_state (frag_now
);
9943 last_frag
= frag_now
;
9944 frag_variant (rs_fill
, 0, 0, 0, NULL
, 0, NULL
);
9945 xtensa_set_frag_assembly_state (frag_now
);
9947 while (search_frag
!= frag_now
)
9949 next_frag
= search_frag
->fr_next
;
9951 /* First, move the frag out of the literal section and
9952 to the appropriate place. */
9953 if (search_frag
->tc_frag_data
.literal_frag
)
9955 literal_pool
= search_frag
->tc_frag_data
.literal_frag
;
9956 assert (literal_pool
->fr_subtype
== RELAX_LITERAL_POOL_BEGIN
);
9957 frchain_to
= literal_pool
->tc_frag_data
.lit_frchain
;
9958 assert (frchain_to
);
9960 insert_after
= literal_pool
->tc_frag_data
.literal_frag
;
9961 dest_seg
= insert_after
->fr_next
->tc_frag_data
.lit_seg
;
9963 *frag_splice
= next_frag
;
9964 search_frag
->fr_next
= insert_after
->fr_next
;
9965 insert_after
->fr_next
= search_frag
;
9966 search_frag
->tc_frag_data
.lit_seg
= dest_seg
;
9967 literal_pool
->tc_frag_data
.literal_frag
= search_frag
;
9969 /* Now move any fixups associated with this frag to the
9971 fix
= frchain_from
->fix_root
;
9972 fix_splice
= &(frchain_from
->fix_root
);
9975 next_fix
= fix
->fx_next
;
9976 if (fix
->fx_frag
== search_frag
)
9978 *fix_splice
= next_fix
;
9979 fix
->fx_next
= frchain_to
->fix_root
;
9980 frchain_to
->fix_root
= fix
;
9981 if (frchain_to
->fix_tail
== NULL
)
9982 frchain_to
->fix_tail
= fix
;
9985 fix_splice
= &(fix
->fx_next
);
9988 search_frag
= next_frag
;
9991 if (frchain_from
->fix_root
!= NULL
)
9993 frchain_from
= seg_info (segment
->seg
)->frchainP
;
9994 as_warn (_("fixes not all moved from %s"), segment
->seg
->name
);
9996 assert (frchain_from
->fix_root
== NULL
);
9998 frchain_from
->fix_tail
= NULL
;
9999 xtensa_restore_emit_state (&state
);
10002 /* Now fix up the SEGMENT value for all the literal symbols. */
10003 for (lit
= literal_syms
; lit
; lit
= lit
->next
)
10005 symbolS
*lit_sym
= lit
->sym
;
10006 segT dest_seg
= symbol_get_frag (lit_sym
)->tc_frag_data
.lit_seg
;
10008 S_SET_SEGMENT (lit_sym
, dest_seg
);
10013 /* Walk over all the frags for segments in a list and mark them as
10014 containing literals. As clunky as this is, we can't rely on frag_var
10015 and frag_variant to get called in all situations. */
10018 mark_literal_frags (seg_list
*segment
)
10020 frchainS
*frchain_from
;
10021 fragS
*search_frag
;
10025 frchain_from
= seg_info (segment
->seg
)->frchainP
;
10026 search_frag
= frchain_from
->frch_root
;
10027 while (search_frag
)
10029 search_frag
->tc_frag_data
.is_literal
= TRUE
;
10030 search_frag
= search_frag
->fr_next
;
10032 segment
= segment
->next
;
10038 xtensa_reorder_seg_list (seg_list
*head
, segT after
)
10040 /* Move all of the sections in the section list to come
10041 after "after" in the gnu segment list. */
10046 segT literal_section
= head
->seg
;
10048 /* Move the literal section after "after". */
10049 assert (literal_section
);
10050 if (literal_section
!= after
)
10052 bfd_section_list_remove (stdoutput
, literal_section
);
10053 bfd_section_list_insert_after (stdoutput
, after
, literal_section
);
10061 /* Push all the literal segments to the end of the gnu list. */
10064 xtensa_reorder_segments (void)
10071 for (sec
= stdoutput
->sections
; sec
!= NULL
; sec
= sec
->next
)
10077 /* Now that we have the last section, push all the literal
10078 sections to the end. */
10079 xtensa_reorder_seg_list (literal_head
, last_sec
);
10081 /* Now perform the final error check. */
10082 for (sec
= stdoutput
->sections
; sec
!= NULL
; sec
= sec
->next
)
10084 assert (new_count
== old_count
);
10088 /* Change the emit state (seg, subseg, and frag related stuff) to the
10089 correct location. Return a emit_state which can be passed to
10090 xtensa_restore_emit_state to return to current fragment. */
10093 xtensa_switch_to_literal_fragment (emit_state
*result
)
10095 if (directive_state
[directive_absolute_literals
])
10097 segT lit4_seg
= cache_literal_section (TRUE
);
10098 xtensa_switch_section_emit_state (result
, lit4_seg
, 0);
10101 xtensa_switch_to_non_abs_literal_fragment (result
);
10103 /* Do a 4-byte align here. */
10104 frag_align (2, 0, 0);
10105 record_alignment (now_seg
, 2);
10110 xtensa_switch_to_non_abs_literal_fragment (emit_state
*result
)
10112 static bfd_boolean recursive
= FALSE
;
10113 fragS
*pool_location
= get_literal_pool_location (now_seg
);
10115 bfd_boolean is_init
=
10116 (now_seg
&& !strcmp (segment_name (now_seg
), INIT_SECTION_NAME
));
10117 bfd_boolean is_fini
=
10118 (now_seg
&& !strcmp (segment_name (now_seg
), FINI_SECTION_NAME
));
10120 if (pool_location
== NULL
10121 && !use_literal_section
10123 && !is_init
&& ! is_fini
)
10125 as_bad (_("literal pool location required for text-section-literals; specify with .literal_position"));
10127 /* When we mark a literal pool location, we want to put a frag in
10128 the literal pool that points to it. But to do that, we want to
10129 switch_to_literal_fragment. But literal sections don't have
10130 literal pools, so their location is always null, so we would
10131 recurse forever. This is kind of hacky, but it works. */
10134 xtensa_mark_literal_pool_location ();
10138 lit_seg
= cache_literal_section (FALSE
);
10139 xtensa_switch_section_emit_state (result
, lit_seg
, 0);
10141 if (!use_literal_section
10142 && !is_init
&& !is_fini
10143 && get_literal_pool_location (now_seg
) != pool_location
)
10145 /* Close whatever frag is there. */
10146 frag_variant (rs_fill
, 0, 0, 0, NULL
, 0, NULL
);
10147 xtensa_set_frag_assembly_state (frag_now
);
10148 frag_now
->tc_frag_data
.literal_frag
= pool_location
;
10149 frag_variant (rs_fill
, 0, 0, 0, NULL
, 0, NULL
);
10150 xtensa_set_frag_assembly_state (frag_now
);
10155 /* Call this function before emitting data into the literal section.
10156 This is a helper function for xtensa_switch_to_literal_fragment.
10157 This is similar to a .section new_now_seg subseg. */
10160 xtensa_switch_section_emit_state (emit_state
*state
,
10162 subsegT new_now_subseg
)
10164 state
->name
= now_seg
->name
;
10165 state
->now_seg
= now_seg
;
10166 state
->now_subseg
= now_subseg
;
10167 state
->generating_literals
= generating_literals
;
10168 generating_literals
++;
10169 subseg_set (new_now_seg
, new_now_subseg
);
10173 /* Use to restore the emitting into the normal place. */
10176 xtensa_restore_emit_state (emit_state
*state
)
10178 generating_literals
= state
->generating_literals
;
10179 subseg_set (state
->now_seg
, state
->now_subseg
);
10183 /* Predicate function used to look up a section in a particular group. */
10186 match_section_group (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *inf
)
10188 const char *gname
= inf
;
10189 const char *group_name
= elf_group_name (sec
);
10191 return (group_name
== gname
10192 || (group_name
!= NULL
10194 && strcmp (group_name
, gname
) == 0));
10198 /* Get the literal section to be used for the current text section.
10199 The result may be cached in the default_lit_sections structure. */
10202 cache_literal_section (bfd_boolean use_abs_literals
)
10204 const char *text_name
, *group_name
= 0;
10205 char *base_name
, *name
, *suffix
;
10207 segT seg
, current_section
;
10208 int current_subsec
;
10209 bfd_boolean linkonce
= FALSE
;
10211 /* Save the current section/subsection. */
10212 current_section
= now_seg
;
10213 current_subsec
= now_subseg
;
10215 /* Clear the cached values if they are no longer valid. */
10216 if (now_seg
!= default_lit_sections
.current_text_seg
)
10218 default_lit_sections
.current_text_seg
= now_seg
;
10219 default_lit_sections
.lit_seg
= NULL
;
10220 default_lit_sections
.lit4_seg
= NULL
;
10223 /* Check if the literal section is already cached. */
10224 if (use_abs_literals
)
10225 pcached
= &default_lit_sections
.lit4_seg
;
10227 pcached
= &default_lit_sections
.lit_seg
;
10232 text_name
= default_lit_sections
.lit_prefix
;
10233 if (! text_name
|| ! *text_name
)
10235 text_name
= segment_name (current_section
);
10236 group_name
= elf_group_name (current_section
);
10237 linkonce
= (current_section
->flags
& SEC_LINK_ONCE
) != 0;
10240 base_name
= use_abs_literals
? ".lit4" : ".literal";
10243 name
= xmalloc (strlen (base_name
) + strlen (group_name
) + 2);
10244 sprintf (name
, "%s.%s", base_name
, group_name
);
10246 else if (strncmp (text_name
, ".gnu.linkonce.", linkonce_len
) == 0)
10248 suffix
= strchr (text_name
+ linkonce_len
, '.');
10250 name
= xmalloc (linkonce_len
+ strlen (base_name
) + 1
10251 + (suffix
? strlen (suffix
) : 0));
10252 strcpy (name
, ".gnu.linkonce");
10253 strcat (name
, base_name
);
10255 strcat (name
, suffix
);
10260 /* If the section name ends with ".text", then replace that suffix
10261 instead of appending an additional suffix. */
10262 size_t len
= strlen (text_name
);
10263 if (len
>= 5 && strcmp (text_name
+ len
- 5, ".text") == 0)
10266 name
= xmalloc (len
+ strlen (base_name
) + 1);
10267 strcpy (name
, text_name
);
10268 strcpy (name
+ len
, base_name
);
10271 /* Canonicalize section names to allow renaming literal sections.
10272 The group name, if any, came from the current text section and
10273 has already been canonicalized. */
10274 name
= tc_canonicalize_symbol_name (name
);
10276 seg
= bfd_get_section_by_name_if (stdoutput
, name
, match_section_group
,
10277 (void *) group_name
);
10282 seg
= subseg_force_new (name
, 0);
10284 if (! use_abs_literals
)
10286 /* Add the newly created literal segment to the list. */
10287 seg_list
*n
= (seg_list
*) xmalloc (sizeof (seg_list
));
10289 n
->next
= literal_head
->next
;
10290 literal_head
->next
= n
;
10293 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_ALLOC
| SEC_LOAD
10294 | (linkonce
? (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
) : 0)
10295 | (use_abs_literals
? SEC_DATA
: SEC_CODE
));
10297 elf_group_name (seg
) = group_name
;
10299 bfd_set_section_flags (stdoutput
, seg
, flags
);
10300 bfd_set_section_alignment (stdoutput
, seg
, 2);
10304 subseg_set (current_section
, current_subsec
);
10309 /* Property Tables Stuff. */
10311 #define XTENSA_INSN_SEC_NAME ".xt.insn"
10312 #define XTENSA_LIT_SEC_NAME ".xt.lit"
10313 #define XTENSA_PROP_SEC_NAME ".xt.prop"
10315 typedef bfd_boolean (*frag_predicate
) (const fragS
*);
10316 typedef void (*frag_flags_fn
) (const fragS
*, frag_flags
*);
10318 static bfd_boolean
get_frag_is_literal (const fragS
*);
10319 static void xtensa_create_property_segments
10320 (frag_predicate
, frag_predicate
, const char *, xt_section_type
);
10321 static void xtensa_create_xproperty_segments
10322 (frag_flags_fn
, const char *, xt_section_type
);
10323 static bfd_boolean
section_has_property (segT
, frag_predicate
);
10324 static bfd_boolean
section_has_xproperty (segT
, frag_flags_fn
);
10325 static void add_xt_block_frags
10326 (segT
, xtensa_block_info
**, frag_predicate
, frag_predicate
);
10327 static bfd_boolean
xtensa_frag_flags_is_empty (const frag_flags
*);
10328 static void xtensa_frag_flags_init (frag_flags
*);
10329 static void get_frag_property_flags (const fragS
*, frag_flags
*);
10330 static bfd_vma
frag_flags_to_number (const frag_flags
*);
10331 static void add_xt_prop_frags (segT
, xtensa_block_info
**, frag_flags_fn
);
10333 /* Set up property tables after relaxation. */
10336 xtensa_post_relax_hook (void)
10338 xtensa_move_seg_list_to_beginning (literal_head
);
10340 xtensa_find_unmarked_state_frags ();
10341 xtensa_mark_frags_for_org ();
10342 xtensa_mark_difference_of_two_symbols ();
10344 xtensa_create_property_segments (get_frag_is_literal
,
10346 XTENSA_LIT_SEC_NAME
,
10348 xtensa_create_xproperty_segments (get_frag_property_flags
,
10349 XTENSA_PROP_SEC_NAME
,
10352 if (warn_unaligned_branch_targets
)
10353 bfd_map_over_sections (stdoutput
, xtensa_find_unaligned_branch_targets
, 0);
10354 bfd_map_over_sections (stdoutput
, xtensa_find_unaligned_loops
, 0);
10358 /* This function is only meaningful after xtensa_move_literals. */
10361 get_frag_is_literal (const fragS
*fragP
)
10363 assert (fragP
!= NULL
);
10364 return fragP
->tc_frag_data
.is_literal
;
10369 xtensa_create_property_segments (frag_predicate property_function
,
10370 frag_predicate end_property_function
,
10371 const char *section_name_base
,
10372 xt_section_type sec_type
)
10376 /* Walk over all of the current segments.
10377 Walk over each fragment
10378 For each non-empty fragment,
10379 Build a property record (append where possible). */
10381 for (seclist
= &stdoutput
->sections
;
10382 seclist
&& *seclist
;
10383 seclist
= &(*seclist
)->next
)
10385 segT sec
= *seclist
;
10388 flags
= bfd_get_section_flags (stdoutput
, sec
);
10389 if (flags
& SEC_DEBUGGING
)
10391 if (!(flags
& SEC_ALLOC
))
10394 if (section_has_property (sec
, property_function
))
10396 segment_info_type
*xt_seg_info
;
10397 xtensa_block_info
**xt_blocks
;
10398 segT prop_sec
= xtensa_get_property_section (sec
, section_name_base
);
10400 prop_sec
->output_section
= prop_sec
;
10401 subseg_set (prop_sec
, 0);
10402 xt_seg_info
= seg_info (prop_sec
);
10403 xt_blocks
= &xt_seg_info
->tc_segment_info_data
.blocks
[sec_type
];
10405 /* Walk over all of the frchains here and add new sections. */
10406 add_xt_block_frags (sec
, xt_blocks
, property_function
,
10407 end_property_function
);
10411 /* Now we fill them out.... */
10413 for (seclist
= &stdoutput
->sections
;
10414 seclist
&& *seclist
;
10415 seclist
= &(*seclist
)->next
)
10417 segment_info_type
*seginfo
;
10418 xtensa_block_info
*block
;
10419 segT sec
= *seclist
;
10421 seginfo
= seg_info (sec
);
10422 block
= seginfo
->tc_segment_info_data
.blocks
[sec_type
];
10426 xtensa_block_info
*cur_block
;
10428 bfd_size_type rec_size
;
10430 for (cur_block
= block
; cur_block
; cur_block
= cur_block
->next
)
10433 rec_size
= num_recs
* 8;
10434 bfd_set_section_size (stdoutput
, sec
, rec_size
);
10441 subseg_set (sec
, 0);
10442 frag_data
= frag_more (rec_size
);
10444 for (i
= 0; i
< num_recs
; i
++)
10448 /* Write the fixup. */
10449 assert (cur_block
);
10450 fix
= fix_new (frag_now
, i
* 8, 4,
10451 section_symbol (cur_block
->sec
),
10453 FALSE
, BFD_RELOC_32
);
10454 fix
->fx_file
= "<internal>";
10457 /* Write the length. */
10458 md_number_to_chars (&frag_data
[4 + i
* 8],
10459 cur_block
->size
, 4);
10460 cur_block
= cur_block
->next
;
10462 frag_wane (frag_now
);
10464 frag_wane (frag_now
);
10472 xtensa_create_xproperty_segments (frag_flags_fn flag_fn
,
10473 const char *section_name_base
,
10474 xt_section_type sec_type
)
10478 /* Walk over all of the current segments.
10479 Walk over each fragment.
10480 For each fragment that has instructions,
10481 build an instruction record (append where possible). */
10483 for (seclist
= &stdoutput
->sections
;
10484 seclist
&& *seclist
;
10485 seclist
= &(*seclist
)->next
)
10487 segT sec
= *seclist
;
10490 flags
= bfd_get_section_flags (stdoutput
, sec
);
10491 if ((flags
& SEC_DEBUGGING
)
10492 || !(flags
& SEC_ALLOC
)
10493 || (flags
& SEC_MERGE
))
10496 if (section_has_xproperty (sec
, flag_fn
))
10498 segment_info_type
*xt_seg_info
;
10499 xtensa_block_info
**xt_blocks
;
10500 segT prop_sec
= xtensa_get_property_section (sec
, section_name_base
);
10502 prop_sec
->output_section
= prop_sec
;
10503 subseg_set (prop_sec
, 0);
10504 xt_seg_info
= seg_info (prop_sec
);
10505 xt_blocks
= &xt_seg_info
->tc_segment_info_data
.blocks
[sec_type
];
10507 /* Walk over all of the frchains here and add new sections. */
10508 add_xt_prop_frags (sec
, xt_blocks
, flag_fn
);
10512 /* Now we fill them out.... */
10514 for (seclist
= &stdoutput
->sections
;
10515 seclist
&& *seclist
;
10516 seclist
= &(*seclist
)->next
)
10518 segment_info_type
*seginfo
;
10519 xtensa_block_info
*block
;
10520 segT sec
= *seclist
;
10522 seginfo
= seg_info (sec
);
10523 block
= seginfo
->tc_segment_info_data
.blocks
[sec_type
];
10527 xtensa_block_info
*cur_block
;
10529 bfd_size_type rec_size
;
10531 for (cur_block
= block
; cur_block
; cur_block
= cur_block
->next
)
10534 rec_size
= num_recs
* (8 + 4);
10535 bfd_set_section_size (stdoutput
, sec
, rec_size
);
10536 /* elf_section_data (sec)->this_hdr.sh_entsize = 12; */
10543 subseg_set (sec
, 0);
10544 frag_data
= frag_more (rec_size
);
10546 for (i
= 0; i
< num_recs
; i
++)
10550 /* Write the fixup. */
10551 assert (cur_block
);
10552 fix
= fix_new (frag_now
, i
* 12, 4,
10553 section_symbol (cur_block
->sec
),
10555 FALSE
, BFD_RELOC_32
);
10556 fix
->fx_file
= "<internal>";
10559 /* Write the length. */
10560 md_number_to_chars (&frag_data
[4 + i
* 12],
10561 cur_block
->size
, 4);
10562 md_number_to_chars (&frag_data
[8 + i
* 12],
10563 frag_flags_to_number (&cur_block
->flags
),
10565 cur_block
= cur_block
->next
;
10567 frag_wane (frag_now
);
10569 frag_wane (frag_now
);
10577 section_has_property (segT sec
, frag_predicate property_function
)
10579 segment_info_type
*seginfo
= seg_info (sec
);
10582 if (seginfo
&& seginfo
->frchainP
)
10584 for (fragP
= seginfo
->frchainP
->frch_root
; fragP
; fragP
= fragP
->fr_next
)
10586 if (property_function (fragP
)
10587 && (fragP
->fr_type
!= rs_fill
|| fragP
->fr_fix
!= 0))
10596 section_has_xproperty (segT sec
, frag_flags_fn property_function
)
10598 segment_info_type
*seginfo
= seg_info (sec
);
10601 if (seginfo
&& seginfo
->frchainP
)
10603 for (fragP
= seginfo
->frchainP
->frch_root
; fragP
; fragP
= fragP
->fr_next
)
10605 frag_flags prop_flags
;
10606 property_function (fragP
, &prop_flags
);
10607 if (!xtensa_frag_flags_is_empty (&prop_flags
))
10615 /* Two types of block sections exist right now: literal and insns. */
10618 add_xt_block_frags (segT sec
,
10619 xtensa_block_info
**xt_block
,
10620 frag_predicate property_function
,
10621 frag_predicate end_property_function
)
10623 bfd_vma seg_offset
;
10626 /* Build it if needed. */
10627 while (*xt_block
!= NULL
)
10628 xt_block
= &(*xt_block
)->next
;
10629 /* We are either at NULL at the beginning or at the end. */
10631 /* Walk through the frags. */
10634 if (seg_info (sec
)->frchainP
)
10636 for (fragP
= seg_info (sec
)->frchainP
->frch_root
;
10638 fragP
= fragP
->fr_next
)
10640 if (property_function (fragP
)
10641 && (fragP
->fr_type
!= rs_fill
|| fragP
->fr_fix
!= 0))
10643 if (*xt_block
!= NULL
)
10645 if ((*xt_block
)->offset
+ (*xt_block
)->size
10646 == fragP
->fr_address
)
10647 (*xt_block
)->size
+= fragP
->fr_fix
;
10649 xt_block
= &((*xt_block
)->next
);
10651 if (*xt_block
== NULL
)
10653 xtensa_block_info
*new_block
= (xtensa_block_info
*)
10654 xmalloc (sizeof (xtensa_block_info
));
10655 new_block
->sec
= sec
;
10656 new_block
->offset
= fragP
->fr_address
;
10657 new_block
->size
= fragP
->fr_fix
;
10658 new_block
->next
= NULL
;
10659 xtensa_frag_flags_init (&new_block
->flags
);
10660 *xt_block
= new_block
;
10662 if (end_property_function
10663 && end_property_function (fragP
))
10665 xt_block
= &((*xt_block
)->next
);
10673 /* Break the encapsulation of add_xt_prop_frags here. */
10676 xtensa_frag_flags_is_empty (const frag_flags
*prop_flags
)
10678 if (prop_flags
->is_literal
10679 || prop_flags
->is_insn
10680 || prop_flags
->is_data
10681 || prop_flags
->is_unreachable
)
10688 xtensa_frag_flags_init (frag_flags
*prop_flags
)
10690 memset (prop_flags
, 0, sizeof (frag_flags
));
10695 get_frag_property_flags (const fragS
*fragP
, frag_flags
*prop_flags
)
10697 xtensa_frag_flags_init (prop_flags
);
10698 if (fragP
->tc_frag_data
.is_literal
)
10699 prop_flags
->is_literal
= TRUE
;
10700 if (fragP
->tc_frag_data
.is_specific_opcode
10701 || fragP
->tc_frag_data
.is_no_transform
)
10703 prop_flags
->is_no_transform
= TRUE
;
10704 if (xtensa_frag_flags_is_empty (prop_flags
))
10705 prop_flags
->is_data
= TRUE
;
10707 if (fragP
->tc_frag_data
.is_unreachable
)
10708 prop_flags
->is_unreachable
= TRUE
;
10709 else if (fragP
->tc_frag_data
.is_insn
)
10711 prop_flags
->is_insn
= TRUE
;
10712 if (fragP
->tc_frag_data
.is_loop_target
)
10713 prop_flags
->insn
.is_loop_target
= TRUE
;
10714 if (fragP
->tc_frag_data
.is_branch_target
)
10715 prop_flags
->insn
.is_branch_target
= TRUE
;
10716 if (fragP
->tc_frag_data
.is_no_density
)
10717 prop_flags
->insn
.is_no_density
= TRUE
;
10718 if (fragP
->tc_frag_data
.use_absolute_literals
)
10719 prop_flags
->insn
.is_abslit
= TRUE
;
10721 if (fragP
->tc_frag_data
.is_align
)
10723 prop_flags
->is_align
= TRUE
;
10724 prop_flags
->alignment
= fragP
->tc_frag_data
.alignment
;
10725 if (xtensa_frag_flags_is_empty (prop_flags
))
10726 prop_flags
->is_data
= TRUE
;
10732 frag_flags_to_number (const frag_flags
*prop_flags
)
10735 if (prop_flags
->is_literal
)
10736 num
|= XTENSA_PROP_LITERAL
;
10737 if (prop_flags
->is_insn
)
10738 num
|= XTENSA_PROP_INSN
;
10739 if (prop_flags
->is_data
)
10740 num
|= XTENSA_PROP_DATA
;
10741 if (prop_flags
->is_unreachable
)
10742 num
|= XTENSA_PROP_UNREACHABLE
;
10743 if (prop_flags
->insn
.is_loop_target
)
10744 num
|= XTENSA_PROP_INSN_LOOP_TARGET
;
10745 if (prop_flags
->insn
.is_branch_target
)
10747 num
|= XTENSA_PROP_INSN_BRANCH_TARGET
;
10748 num
= SET_XTENSA_PROP_BT_ALIGN (num
, prop_flags
->insn
.bt_align_priority
);
10751 if (prop_flags
->insn
.is_no_density
)
10752 num
|= XTENSA_PROP_INSN_NO_DENSITY
;
10753 if (prop_flags
->is_no_transform
)
10754 num
|= XTENSA_PROP_NO_TRANSFORM
;
10755 if (prop_flags
->insn
.is_no_reorder
)
10756 num
|= XTENSA_PROP_INSN_NO_REORDER
;
10757 if (prop_flags
->insn
.is_abslit
)
10758 num
|= XTENSA_PROP_INSN_ABSLIT
;
10760 if (prop_flags
->is_align
)
10762 num
|= XTENSA_PROP_ALIGN
;
10763 num
= SET_XTENSA_PROP_ALIGNMENT (num
, prop_flags
->alignment
);
10771 xtensa_frag_flags_combinable (const frag_flags
*prop_flags_1
,
10772 const frag_flags
*prop_flags_2
)
10774 /* Cannot combine with an end marker. */
10776 if (prop_flags_1
->is_literal
!= prop_flags_2
->is_literal
)
10778 if (prop_flags_1
->is_insn
!= prop_flags_2
->is_insn
)
10780 if (prop_flags_1
->is_data
!= prop_flags_2
->is_data
)
10783 if (prop_flags_1
->is_insn
)
10785 /* Properties of the beginning of the frag. */
10786 if (prop_flags_2
->insn
.is_loop_target
)
10788 if (prop_flags_2
->insn
.is_branch_target
)
10790 if (prop_flags_1
->insn
.is_no_density
!=
10791 prop_flags_2
->insn
.is_no_density
)
10793 if (prop_flags_1
->is_no_transform
!=
10794 prop_flags_2
->is_no_transform
)
10796 if (prop_flags_1
->insn
.is_no_reorder
!=
10797 prop_flags_2
->insn
.is_no_reorder
)
10799 if (prop_flags_1
->insn
.is_abslit
!=
10800 prop_flags_2
->insn
.is_abslit
)
10804 if (prop_flags_1
->is_align
)
10812 xt_block_aligned_size (const xtensa_block_info
*xt_block
)
10815 unsigned align_bits
;
10817 if (!xt_block
->flags
.is_align
)
10818 return xt_block
->size
;
10820 end_addr
= xt_block
->offset
+ xt_block
->size
;
10821 align_bits
= xt_block
->flags
.alignment
;
10822 end_addr
= ((end_addr
+ ((1 << align_bits
) -1)) >> align_bits
) << align_bits
;
10823 return end_addr
- xt_block
->offset
;
10828 xtensa_xt_block_combine (xtensa_block_info
*xt_block
,
10829 const xtensa_block_info
*xt_block_2
)
10831 if (xt_block
->sec
!= xt_block_2
->sec
)
10833 if (xt_block
->offset
+ xt_block_aligned_size (xt_block
)
10834 != xt_block_2
->offset
)
10837 if (xt_block_2
->size
== 0
10838 && (!xt_block_2
->flags
.is_unreachable
10839 || xt_block
->flags
.is_unreachable
))
10841 if (xt_block_2
->flags
.is_align
10842 && xt_block
->flags
.is_align
)
10844 /* Nothing needed. */
10845 if (xt_block
->flags
.alignment
>= xt_block_2
->flags
.alignment
)
10850 if (xt_block_2
->flags
.is_align
)
10852 /* Push alignment to previous entry. */
10853 xt_block
->flags
.is_align
= xt_block_2
->flags
.is_align
;
10854 xt_block
->flags
.alignment
= xt_block_2
->flags
.alignment
;
10859 if (!xtensa_frag_flags_combinable (&xt_block
->flags
,
10860 &xt_block_2
->flags
))
10863 xt_block
->size
+= xt_block_2
->size
;
10865 if (xt_block_2
->flags
.is_align
)
10867 xt_block
->flags
.is_align
= TRUE
;
10868 xt_block
->flags
.alignment
= xt_block_2
->flags
.alignment
;
10876 add_xt_prop_frags (segT sec
,
10877 xtensa_block_info
**xt_block
,
10878 frag_flags_fn property_function
)
10880 bfd_vma seg_offset
;
10883 /* Build it if needed. */
10884 while (*xt_block
!= NULL
)
10886 xt_block
= &(*xt_block
)->next
;
10888 /* We are either at NULL at the beginning or at the end. */
10890 /* Walk through the frags. */
10893 if (seg_info (sec
)->frchainP
)
10895 for (fragP
= seg_info (sec
)->frchainP
->frch_root
; fragP
;
10896 fragP
= fragP
->fr_next
)
10898 xtensa_block_info tmp_block
;
10899 tmp_block
.sec
= sec
;
10900 tmp_block
.offset
= fragP
->fr_address
;
10901 tmp_block
.size
= fragP
->fr_fix
;
10902 tmp_block
.next
= NULL
;
10903 property_function (fragP
, &tmp_block
.flags
);
10905 if (!xtensa_frag_flags_is_empty (&tmp_block
.flags
))
10906 /* && fragP->fr_fix != 0) */
10908 if ((*xt_block
) == NULL
10909 || !xtensa_xt_block_combine (*xt_block
, &tmp_block
))
10911 xtensa_block_info
*new_block
;
10912 if ((*xt_block
) != NULL
)
10913 xt_block
= &(*xt_block
)->next
;
10914 new_block
= (xtensa_block_info
*)
10915 xmalloc (sizeof (xtensa_block_info
));
10916 *new_block
= tmp_block
;
10917 *xt_block
= new_block
;
10925 /* op_placement_info_table */
10927 /* op_placement_info makes it easier to determine which
10928 ops can go in which slots. */
10931 init_op_placement_info_table (void)
10933 xtensa_isa isa
= xtensa_default_isa
;
10934 xtensa_insnbuf ibuf
= xtensa_insnbuf_alloc (isa
);
10935 xtensa_opcode opcode
;
10938 int num_opcodes
= xtensa_isa_num_opcodes (isa
);
10940 op_placement_table
= (op_placement_info_table
)
10941 xmalloc (sizeof (op_placement_info
) * num_opcodes
);
10942 assert (xtensa_isa_num_formats (isa
) < MAX_FORMATS
);
10944 for (opcode
= 0; opcode
< num_opcodes
; opcode
++)
10946 op_placement_info
*opi
= &op_placement_table
[opcode
];
10947 /* FIXME: Make tinsn allocation dynamic. */
10948 if (xtensa_opcode_num_operands (isa
, opcode
) >= MAX_INSN_ARGS
)
10949 as_fatal (_("too many operands in instruction"));
10950 opi
->narrowest
= XTENSA_UNDEFINED
;
10951 opi
->narrowest_size
= 0x7F;
10952 opi
->narrowest_slot
= 0;
10954 opi
->num_formats
= 0;
10956 for (fmt
= 0; fmt
< xtensa_isa_num_formats (isa
); fmt
++)
10958 opi
->slots
[fmt
] = 0;
10959 for (slot
= 0; slot
< xtensa_format_num_slots (isa
, fmt
); slot
++)
10961 if (xtensa_opcode_encode (isa
, fmt
, slot
, ibuf
, opcode
) == 0)
10963 int fmt_length
= xtensa_format_length (isa
, fmt
);
10965 set_bit (fmt
, opi
->formats
);
10966 set_bit (slot
, opi
->slots
[fmt
]);
10967 if (fmt_length
< opi
->narrowest_size
10968 || (fmt_length
== opi
->narrowest_size
10969 && (xtensa_format_num_slots (isa
, fmt
)
10970 < xtensa_format_num_slots (isa
,
10973 opi
->narrowest
= fmt
;
10974 opi
->narrowest_size
= fmt_length
;
10975 opi
->narrowest_slot
= slot
;
10980 opi
->num_formats
++;
10983 xtensa_insnbuf_free (isa
, ibuf
);
10988 opcode_fits_format_slot (xtensa_opcode opcode
, xtensa_format fmt
, int slot
)
10990 return bit_is_set (slot
, op_placement_table
[opcode
].slots
[fmt
]);
10994 /* If the opcode is available in a single slot format, return its size. */
10997 xg_get_single_size (xtensa_opcode opcode
)
10999 return op_placement_table
[opcode
].narrowest_size
;
11003 static xtensa_format
11004 xg_get_single_format (xtensa_opcode opcode
)
11006 return op_placement_table
[opcode
].narrowest
;
11011 xg_get_single_slot (xtensa_opcode opcode
)
11013 return op_placement_table
[opcode
].narrowest_slot
;
11017 /* Instruction Stack Functions (from "xtensa-istack.h"). */
11020 istack_init (IStack
*stack
)
11022 memset (stack
, 0, sizeof (IStack
));
11028 istack_empty (IStack
*stack
)
11030 return (stack
->ninsn
== 0);
11035 istack_full (IStack
*stack
)
11037 return (stack
->ninsn
== MAX_ISTACK
);
11041 /* Return a pointer to the top IStack entry.
11042 It is an error to call this if istack_empty () is TRUE. */
11045 istack_top (IStack
*stack
)
11047 int rec
= stack
->ninsn
- 1;
11048 assert (!istack_empty (stack
));
11049 return &stack
->insn
[rec
];
11053 /* Add a new TInsn to an IStack.
11054 It is an error to call this if istack_full () is TRUE. */
11057 istack_push (IStack
*stack
, TInsn
*insn
)
11059 int rec
= stack
->ninsn
;
11060 assert (!istack_full (stack
));
11061 stack
->insn
[rec
] = *insn
;
11066 /* Clear space for the next TInsn on the IStack and return a pointer
11067 to it. It is an error to call this if istack_full () is TRUE. */
11070 istack_push_space (IStack
*stack
)
11072 int rec
= stack
->ninsn
;
11074 assert (!istack_full (stack
));
11075 insn
= &stack
->insn
[rec
];
11082 /* Remove the last pushed instruction. It is an error to call this if
11083 istack_empty () returns TRUE. */
11086 istack_pop (IStack
*stack
)
11088 int rec
= stack
->ninsn
- 1;
11089 assert (!istack_empty (stack
));
11091 tinsn_init (&stack
->insn
[rec
]);
11095 /* TInsn functions. */
11098 tinsn_init (TInsn
*dst
)
11100 memset (dst
, 0, sizeof (TInsn
));
11104 /* Return TRUE if ANY of the operands in the insn are symbolic. */
11107 tinsn_has_symbolic_operands (const TInsn
*insn
)
11110 int n
= insn
->ntok
;
11112 assert (insn
->insn_type
== ITYPE_INSN
);
11114 for (i
= 0; i
< n
; ++i
)
11116 switch (insn
->tok
[i
].X_op
)
11130 tinsn_has_invalid_symbolic_operands (const TInsn
*insn
)
11132 xtensa_isa isa
= xtensa_default_isa
;
11134 int n
= insn
->ntok
;
11136 assert (insn
->insn_type
== ITYPE_INSN
);
11138 for (i
= 0; i
< n
; ++i
)
11140 switch (insn
->tok
[i
].X_op
)
11148 /* Errors for these types are caught later. */
11153 /* Symbolic immediates are only allowed on the last immediate
11154 operand. At this time, CONST16 is the only opcode where we
11155 support non-PC-relative relocations. */
11156 if (i
!= get_relaxable_immed (insn
->opcode
)
11157 || (xtensa_operand_is_PCrelative (isa
, insn
->opcode
, i
) != 1
11158 && insn
->opcode
!= xtensa_const16_opcode
))
11160 as_bad (_("invalid symbolic operand"));
11169 /* For assembly code with complex expressions (e.g. subtraction),
11170 we have to build them in the literal pool so that
11171 their results are calculated correctly after relaxation.
11172 The relaxation only handles expressions that
11173 boil down to SYMBOL + OFFSET. */
11176 tinsn_has_complex_operands (const TInsn
*insn
)
11179 int n
= insn
->ntok
;
11180 assert (insn
->insn_type
== ITYPE_INSN
);
11181 for (i
= 0; i
< n
; ++i
)
11183 switch (insn
->tok
[i
].X_op
)
11199 /* Encode a TInsn opcode and its constant operands into slotbuf.
11200 Return TRUE if there is a symbol in the immediate field. This
11201 function assumes that:
11202 1) The number of operands are correct.
11203 2) The insn_type is ITYPE_INSN.
11204 3) The opcode can be encoded in the specified format and slot.
11205 4) Operands are either O_constant or O_symbol, and all constants fit. */
11208 tinsn_to_slotbuf (xtensa_format fmt
,
11211 xtensa_insnbuf slotbuf
)
11213 xtensa_isa isa
= xtensa_default_isa
;
11214 xtensa_opcode opcode
= tinsn
->opcode
;
11215 bfd_boolean has_fixup
= FALSE
;
11216 int noperands
= xtensa_opcode_num_operands (isa
, opcode
);
11219 assert (tinsn
->insn_type
== ITYPE_INSN
);
11220 if (noperands
!= tinsn
->ntok
)
11221 as_fatal (_("operand number mismatch"));
11223 if (xtensa_opcode_encode (isa
, fmt
, slot
, slotbuf
, opcode
))
11225 as_bad (_("cannot encode opcode \"%s\" in the given format \"%s\""),
11226 xtensa_opcode_name (isa
, opcode
), xtensa_format_name (isa
, fmt
));
11230 for (i
= 0; i
< noperands
; i
++)
11232 expressionS
*expr
= &tinsn
->tok
[i
];
11238 switch (expr
->X_op
)
11241 if (xtensa_operand_is_visible (isa
, opcode
, i
) == 0)
11243 /* The register number has already been checked in
11244 expression_maybe_register, so we don't need to check here. */
11245 opnd_value
= expr
->X_add_number
;
11246 (void) xtensa_operand_encode (isa
, opcode
, i
, &opnd_value
);
11247 rc
= xtensa_operand_set_field (isa
, opcode
, i
, fmt
, slot
, slotbuf
,
11250 as_warn (_("xtensa-isa failure: %s"), xtensa_isa_error_msg (isa
));
11254 if (xtensa_operand_is_visible (isa
, opcode
, i
) == 0)
11256 as_where (&file_name
, &line
);
11257 /* It is a constant and we called this function
11258 then we have to try to fit it. */
11259 xtensa_insnbuf_set_operand (slotbuf
, fmt
, slot
, opcode
, i
,
11260 expr
->X_add_number
, file_name
, line
);
11273 /* Encode a single TInsn into an insnbuf. If the opcode can only be encoded
11274 into a multi-slot instruction, fill the other slots with NOPs.
11275 Return TRUE if there is a symbol in the immediate field. See also the
11276 assumptions listed for tinsn_to_slotbuf. */
11279 tinsn_to_insnbuf (TInsn
*tinsn
, xtensa_insnbuf insnbuf
)
11281 static xtensa_insnbuf slotbuf
= 0;
11282 static vliw_insn vinsn
;
11283 xtensa_isa isa
= xtensa_default_isa
;
11284 bfd_boolean has_fixup
= FALSE
;
11289 slotbuf
= xtensa_insnbuf_alloc (isa
);
11290 xg_init_vinsn (&vinsn
);
11293 xg_clear_vinsn (&vinsn
);
11295 bundle_tinsn (tinsn
, &vinsn
);
11297 xtensa_format_encode (isa
, vinsn
.format
, insnbuf
);
11299 for (i
= 0; i
< vinsn
.num_slots
; i
++)
11301 /* Only one slot may have a fix-up because the rest contains NOPs. */
11303 tinsn_to_slotbuf (vinsn
.format
, i
, &vinsn
.slots
[i
], vinsn
.slotbuf
[i
]);
11304 xtensa_format_set_slot (isa
, vinsn
.format
, i
, insnbuf
, vinsn
.slotbuf
[i
]);
11311 /* Check the instruction arguments. Return TRUE on failure. */
11314 tinsn_check_arguments (const TInsn
*insn
)
11316 xtensa_isa isa
= xtensa_default_isa
;
11317 xtensa_opcode opcode
= insn
->opcode
;
11319 if (opcode
== XTENSA_UNDEFINED
)
11321 as_bad (_("invalid opcode"));
11325 if (xtensa_opcode_num_operands (isa
, opcode
) > insn
->ntok
)
11327 as_bad (_("too few operands"));
11331 if (xtensa_opcode_num_operands (isa
, opcode
) < insn
->ntok
)
11333 as_bad (_("too many operands"));
11340 /* Load an instruction from its encoded form. */
11343 tinsn_from_chars (TInsn
*tinsn
, char *f
, int slot
)
11347 xg_init_vinsn (&vinsn
);
11348 vinsn_from_chars (&vinsn
, f
);
11350 *tinsn
= vinsn
.slots
[slot
];
11351 xg_free_vinsn (&vinsn
);
11356 tinsn_from_insnbuf (TInsn
*tinsn
,
11357 xtensa_insnbuf slotbuf
,
11362 xtensa_isa isa
= xtensa_default_isa
;
11364 /* Find the immed. */
11365 tinsn_init (tinsn
);
11366 tinsn
->insn_type
= ITYPE_INSN
;
11367 tinsn
->is_specific_opcode
= FALSE
; /* must not be specific */
11368 tinsn
->opcode
= xtensa_opcode_decode (isa
, fmt
, slot
, slotbuf
);
11369 tinsn
->ntok
= xtensa_opcode_num_operands (isa
, tinsn
->opcode
);
11370 for (i
= 0; i
< tinsn
->ntok
; i
++)
11372 set_expr_const (&tinsn
->tok
[i
],
11373 xtensa_insnbuf_get_operand (slotbuf
, fmt
, slot
,
11374 tinsn
->opcode
, i
));
11379 /* Read the value of the relaxable immed from the fr_symbol and fr_offset. */
11382 tinsn_immed_from_frag (TInsn
*tinsn
, fragS
*fragP
, int slot
)
11384 xtensa_opcode opcode
= tinsn
->opcode
;
11387 if (fragP
->tc_frag_data
.slot_symbols
[slot
])
11389 opnum
= get_relaxable_immed (opcode
);
11390 assert (opnum
>= 0);
11391 set_expr_symbol_offset (&tinsn
->tok
[opnum
],
11392 fragP
->tc_frag_data
.slot_symbols
[slot
],
11393 fragP
->tc_frag_data
.slot_offsets
[slot
]);
11399 get_num_stack_text_bytes (IStack
*istack
)
11402 int text_bytes
= 0;
11404 for (i
= 0; i
< istack
->ninsn
; i
++)
11406 TInsn
*tinsn
= &istack
->insn
[i
];
11407 if (tinsn
->insn_type
== ITYPE_INSN
)
11408 text_bytes
+= xg_get_single_size (tinsn
->opcode
);
11415 get_num_stack_literal_bytes (IStack
*istack
)
11420 for (i
= 0; i
< istack
->ninsn
; i
++)
11422 TInsn
*tinsn
= &istack
->insn
[i
];
11423 if (tinsn
->insn_type
== ITYPE_LITERAL
&& tinsn
->ntok
== 1)
11430 /* vliw_insn functions. */
11433 xg_init_vinsn (vliw_insn
*v
)
11436 xtensa_isa isa
= xtensa_default_isa
;
11438 xg_clear_vinsn (v
);
11440 v
->insnbuf
= xtensa_insnbuf_alloc (isa
);
11441 if (v
->insnbuf
== NULL
)
11442 as_fatal (_("out of memory"));
11444 for (i
= 0; i
< MAX_SLOTS
; i
++)
11446 v
->slotbuf
[i
] = xtensa_insnbuf_alloc (isa
);
11447 if (v
->slotbuf
[i
] == NULL
)
11448 as_fatal (_("out of memory"));
11454 xg_clear_vinsn (vliw_insn
*v
)
11458 memset (v
, 0, offsetof (vliw_insn
, insnbuf
));
11460 v
->format
= XTENSA_UNDEFINED
;
11462 v
->inside_bundle
= FALSE
;
11464 if (xt_saved_debug_type
!= DEBUG_NONE
)
11465 debug_type
= xt_saved_debug_type
;
11467 for (i
= 0; i
< MAX_SLOTS
; i
++)
11468 v
->slots
[i
].opcode
= XTENSA_UNDEFINED
;
11473 vinsn_has_specific_opcodes (vliw_insn
*v
)
11477 for (i
= 0; i
< v
->num_slots
; i
++)
11479 if (v
->slots
[i
].is_specific_opcode
)
11487 xg_free_vinsn (vliw_insn
*v
)
11490 xtensa_insnbuf_free (xtensa_default_isa
, v
->insnbuf
);
11491 for (i
= 0; i
< MAX_SLOTS
; i
++)
11492 xtensa_insnbuf_free (xtensa_default_isa
, v
->slotbuf
[i
]);
11496 /* Encode a vliw_insn into an insnbuf. Return TRUE if there are any symbolic
11497 operands. See also the assumptions listed for tinsn_to_slotbuf. */
11500 vinsn_to_insnbuf (vliw_insn
*vinsn
,
11503 bfd_boolean record_fixup
)
11505 xtensa_isa isa
= xtensa_default_isa
;
11506 xtensa_format fmt
= vinsn
->format
;
11507 xtensa_insnbuf insnbuf
= vinsn
->insnbuf
;
11509 bfd_boolean has_fixup
= FALSE
;
11511 xtensa_format_encode (isa
, fmt
, insnbuf
);
11513 for (slot
= 0; slot
< vinsn
->num_slots
; slot
++)
11515 TInsn
*tinsn
= &vinsn
->slots
[slot
];
11516 bfd_boolean tinsn_has_fixup
=
11517 tinsn_to_slotbuf (vinsn
->format
, slot
, tinsn
,
11518 vinsn
->slotbuf
[slot
]);
11520 xtensa_format_set_slot (isa
, fmt
, slot
,
11521 insnbuf
, vinsn
->slotbuf
[slot
]);
11522 if (tinsn_has_fixup
)
11525 xtensa_opcode opcode
= tinsn
->opcode
;
11526 int noperands
= xtensa_opcode_num_operands (isa
, opcode
);
11529 for (i
= 0; i
< noperands
; i
++)
11531 expressionS
* expr
= &tinsn
->tok
[i
];
11532 switch (expr
->X_op
)
11537 if (get_relaxable_immed (opcode
) == i
)
11539 /* Add a fix record for the instruction, except if this
11540 function is being called prior to relaxation, i.e.,
11541 if record_fixup is false, and the instruction might
11542 be relaxed later. */
11544 || tinsn
->is_specific_opcode
11545 || !xg_is_relaxable_insn (tinsn
, 0))
11547 xg_add_opcode_fix (tinsn
, i
, fmt
, slot
, expr
, fragP
,
11548 frag_offset
- fragP
->fr_literal
);
11552 if (expr
->X_op
!= O_symbol
)
11553 as_bad (_("invalid operand"));
11554 tinsn
->symbol
= expr
->X_add_symbol
;
11555 tinsn
->offset
= expr
->X_add_number
;
11559 as_bad (_("symbolic operand not allowed"));
11567 as_bad (_("expression too complex"));
11579 vinsn_from_chars (vliw_insn
*vinsn
, char *f
)
11581 static xtensa_insnbuf insnbuf
= NULL
;
11582 static xtensa_insnbuf slotbuf
= NULL
;
11585 xtensa_isa isa
= xtensa_default_isa
;
11589 insnbuf
= xtensa_insnbuf_alloc (isa
);
11590 slotbuf
= xtensa_insnbuf_alloc (isa
);
11593 xtensa_insnbuf_from_chars (isa
, insnbuf
, (unsigned char *) f
, 0);
11594 fmt
= xtensa_format_decode (isa
, insnbuf
);
11595 if (fmt
== XTENSA_UNDEFINED
)
11596 as_fatal (_("cannot decode instruction format"));
11597 vinsn
->format
= fmt
;
11598 vinsn
->num_slots
= xtensa_format_num_slots (isa
, fmt
);
11600 for (i
= 0; i
< vinsn
->num_slots
; i
++)
11602 TInsn
*tinsn
= &vinsn
->slots
[i
];
11603 xtensa_format_get_slot (isa
, fmt
, i
, insnbuf
, slotbuf
);
11604 tinsn_from_insnbuf (tinsn
, slotbuf
, fmt
, i
);
11609 /* Expression utilities. */
11611 /* Return TRUE if the expression is an integer constant. */
11614 expr_is_const (const expressionS
*s
)
11616 return (s
->X_op
== O_constant
);
11620 /* Get the expression constant.
11621 Calling this is illegal if expr_is_const () returns TRUE. */
11624 get_expr_const (const expressionS
*s
)
11626 assert (expr_is_const (s
));
11627 return s
->X_add_number
;
11631 /* Set the expression to a constant value. */
11634 set_expr_const (expressionS
*s
, offsetT val
)
11636 s
->X_op
= O_constant
;
11637 s
->X_add_number
= val
;
11638 s
->X_add_symbol
= NULL
;
11639 s
->X_op_symbol
= NULL
;
11644 expr_is_register (const expressionS
*s
)
11646 return (s
->X_op
== O_register
);
11650 /* Get the expression constant.
11651 Calling this is illegal if expr_is_const () returns TRUE. */
11654 get_expr_register (const expressionS
*s
)
11656 assert (expr_is_register (s
));
11657 return s
->X_add_number
;
11661 /* Set the expression to a symbol + constant offset. */
11664 set_expr_symbol_offset (expressionS
*s
, symbolS
*sym
, offsetT offset
)
11666 s
->X_op
= O_symbol
;
11667 s
->X_add_symbol
= sym
;
11668 s
->X_op_symbol
= NULL
; /* unused */
11669 s
->X_add_number
= offset
;
11673 /* Return TRUE if the two expressions are equal. */
11676 expr_is_equal (expressionS
*s1
, expressionS
*s2
)
11678 if (s1
->X_op
!= s2
->X_op
)
11680 if (s1
->X_add_symbol
!= s2
->X_add_symbol
)
11682 if (s1
->X_op_symbol
!= s2
->X_op_symbol
)
11684 if (s1
->X_add_number
!= s2
->X_add_number
)
11691 copy_expr (expressionS
*dst
, const expressionS
*src
)
11693 memcpy (dst
, src
, sizeof (expressionS
));
11697 /* Support for the "--rename-section" option. */
11699 struct rename_section_struct
11703 struct rename_section_struct
*next
;
11706 static struct rename_section_struct
*section_rename
;
11709 /* Parse the string "oldname=new_name(:oldname2=new_name2)*" and add
11710 entries to the section_rename list. Note: Specifying multiple
11711 renamings separated by colons is not documented and is retained only
11712 for backward compatibility. */
11715 build_section_rename (const char *arg
)
11717 struct rename_section_struct
*r
;
11718 char *this_arg
= NULL
;
11719 char *next_arg
= NULL
;
11721 for (this_arg
= xstrdup (arg
); this_arg
!= NULL
; this_arg
= next_arg
)
11723 char *old_name
, *new_name
;
11727 next_arg
= strchr (this_arg
, ':');
11735 old_name
= this_arg
;
11736 new_name
= strchr (this_arg
, '=');
11738 if (*old_name
== '\0')
11740 as_warn (_("ignoring extra '-rename-section' delimiter ':'"));
11743 if (!new_name
|| new_name
[1] == '\0')
11745 as_warn (_("ignoring invalid '-rename-section' specification: '%s'"),
11752 /* Check for invalid section renaming. */
11753 for (r
= section_rename
; r
!= NULL
; r
= r
->next
)
11755 if (strcmp (r
->old_name
, old_name
) == 0)
11756 as_bad (_("section %s renamed multiple times"), old_name
);
11757 if (strcmp (r
->new_name
, new_name
) == 0)
11758 as_bad (_("multiple sections remapped to output section %s"),
11763 r
= (struct rename_section_struct
*)
11764 xmalloc (sizeof (struct rename_section_struct
));
11765 r
->old_name
= xstrdup (old_name
);
11766 r
->new_name
= xstrdup (new_name
);
11767 r
->next
= section_rename
;
11768 section_rename
= r
;
11774 xtensa_section_rename (char *name
)
11776 struct rename_section_struct
*r
= section_rename
;
11778 for (r
= section_rename
; r
!= NULL
; r
= r
->next
)
11780 if (strcmp (r
->old_name
, name
) == 0)
11781 return r
->new_name
;