* config/tc-mips.h (mips_flush_pending_output): Delete.
[binutils.git] / gas / config / tc-xtensa.c
blob34fdc2cdf7022dfad3d8a835cc9ee33b73ec15d7
1 /* tc-xtensa.c -- Assemble Xtensa instructions.
2 Copyright 2003, 2004, 2005 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 2, or (at your option)
9 any later version.
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, 59 Temple Place - Suite 330, Boston,
19 MA 02111-1307, USA. */
21 #include <string.h>
22 #include <limits.h>
23 #include "as.h"
24 #include "sb.h"
25 #include "safe-ctype.h"
26 #include "tc-xtensa.h"
27 #include "frags.h"
28 #include "subsegs.h"
29 #include "xtensa-relax.h"
30 #include "xtensa-istack.h"
31 #include "dwarf2dbg.h"
32 #include "struc-symbol.h"
33 #include "xtensa-config.h"
35 #ifndef uint32
36 #define uint32 unsigned int
37 #endif
38 #ifndef int32
39 #define int32 signed int
40 #endif
42 /* Notes:
44 Naming conventions (used somewhat inconsistently):
45 The xtensa_ functions are exported
46 The xg_ functions are internal
48 We also have a couple of different extensibility mechanisms.
49 1) The idiom replacement:
50 This is used when a line is first parsed to
51 replace an instruction pattern with another instruction
52 It is currently limited to replacements of instructions
53 with constant operands.
54 2) The xtensa-relax.c mechanism that has stronger instruction
55 replacement patterns. When an instruction's immediate field
56 does not fit the next instruction sequence is attempted.
57 In addition, "narrow" opcodes are supported this way. */
60 /* Define characters with special meanings to GAS. */
61 const char comment_chars[] = "#";
62 const char line_comment_chars[] = "#";
63 const char line_separator_chars[] = ";";
64 const char EXP_CHARS[] = "eE";
65 const char FLT_CHARS[] = "rRsSfFdDxXpP";
68 /* Flags to indicate whether the hardware supports the density and
69 absolute literals options. */
71 bfd_boolean density_supported = XCHAL_HAVE_DENSITY;
72 bfd_boolean absolute_literals_supported = XSHAL_USE_ABSOLUTE_LITERALS;
74 /* Maximum width we would pad an unreachable frag to get alignment. */
75 #define UNREACHABLE_MAX_WIDTH 8
77 static vliw_insn cur_vinsn;
79 size_t xtensa_fetch_width = XCHAL_INST_FETCH_WIDTH;
81 static enum debug_info_type xt_saved_debug_type = DEBUG_NONE;
83 /* Some functions are only valid in the front end. This variable
84 allows us to assert that we haven't crossed over into the
85 back end. */
86 static bfd_boolean past_xtensa_end = FALSE;
88 /* Flags for properties of the last instruction in a segment. */
89 #define FLAG_IS_A0_WRITER 0x1
90 #define FLAG_IS_BAD_LOOPEND 0x2
93 /* We define a special segment names ".literal" to place literals
94 into. The .fini and .init sections are special because they
95 contain code that is moved together by the linker. We give them
96 their own special .fini.literal and .init.literal sections. */
98 #define LITERAL_SECTION_NAME xtensa_section_rename (".literal")
99 #define LIT4_SECTION_NAME xtensa_section_rename (".lit4")
100 #define FINI_SECTION_NAME xtensa_section_rename (".fini")
101 #define INIT_SECTION_NAME xtensa_section_rename (".init")
102 #define FINI_LITERAL_SECTION_NAME xtensa_section_rename (".fini.literal")
103 #define INIT_LITERAL_SECTION_NAME xtensa_section_rename (".init.literal")
106 /* This type is used for the directive_stack to keep track of the
107 state of the literal collection pools. */
109 typedef struct lit_state_struct
111 const char *lit_seg_name;
112 const char *lit4_seg_name;
113 const char *init_lit_seg_name;
114 const char *fini_lit_seg_name;
115 segT lit_seg;
116 segT lit4_seg;
117 segT init_lit_seg;
118 segT fini_lit_seg;
119 } lit_state;
121 static lit_state default_lit_sections;
124 /* We keep lists of literal segments. The seg_list type is the node
125 for such a list. The *_literal_head locals are the heads of the
126 various lists. All of these lists have a dummy node at the start. */
128 typedef struct seg_list_struct
130 struct seg_list_struct *next;
131 segT seg;
132 } seg_list;
134 static seg_list literal_head_h;
135 static seg_list *literal_head = &literal_head_h;
136 static seg_list init_literal_head_h;
137 static seg_list *init_literal_head = &init_literal_head_h;
138 static seg_list fini_literal_head_h;
139 static seg_list *fini_literal_head = &fini_literal_head_h;
142 /* Lists of symbols. We keep a list of symbols that label the current
143 instruction, so that we can adjust the symbols when inserting alignment
144 for various instructions. We also keep a list of all the symbols on
145 literals, so that we can fix up those symbols when the literals are
146 later moved into the text sections. */
148 typedef struct sym_list_struct
150 struct sym_list_struct *next;
151 symbolS *sym;
152 } sym_list;
154 static sym_list *insn_labels = NULL;
155 static sym_list *free_insn_labels = NULL;
156 static sym_list *saved_insn_labels = NULL;
158 static sym_list *literal_syms;
161 /* Flags to determine whether to prefer const16 or l32r
162 if both options are available. */
163 int prefer_const16 = 0;
164 int prefer_l32r = 0;
166 /* Global flag to indicate when we are emitting literals. */
167 int generating_literals = 0;
169 /* The following PROPERTY table definitions are copied from
170 <elf/xtensa.h> and must be kept in sync with the code there. */
172 /* Flags in the property tables to specify whether blocks of memory
173 are literals, instructions, data, or unreachable. For
174 instructions, blocks that begin loop targets and branch targets are
175 designated. Blocks that do not allow density, instruction
176 reordering or transformation are also specified. Finally, for
177 branch targets, branch target alignment priority is included.
178 Alignment of the next block is specified in the current block
179 and the size of the current block does not include any fill required
180 to align to the next block. */
182 #define XTENSA_PROP_LITERAL 0x00000001
183 #define XTENSA_PROP_INSN 0x00000002
184 #define XTENSA_PROP_DATA 0x00000004
185 #define XTENSA_PROP_UNREACHABLE 0x00000008
186 /* Instruction only properties at beginning of code. */
187 #define XTENSA_PROP_INSN_LOOP_TARGET 0x00000010
188 #define XTENSA_PROP_INSN_BRANCH_TARGET 0x00000020
189 /* Instruction only properties about code. */
190 #define XTENSA_PROP_INSN_NO_DENSITY 0x00000040
191 #define XTENSA_PROP_INSN_NO_REORDER 0x00000080
192 #define XTENSA_PROP_INSN_NO_TRANSFORM 0x00000100
194 /* Branch target alignment information. This transmits information
195 to the linker optimization about the priority of aligning a
196 particular block for branch target alignment: None, low priority,
197 high priority, or required. These only need to be checked in
198 instruction blocks marked as XTENSA_PROP_INSN_BRANCH_TARGET.
199 Common usage is
201 switch (GET_XTENSA_PROP_BT_ALIGN (flags))
202 case XTENSA_PROP_BT_ALIGN_NONE:
203 case XTENSA_PROP_BT_ALIGN_LOW:
204 case XTENSA_PROP_BT_ALIGN_HIGH:
205 case XTENSA_PROP_BT_ALIGN_REQUIRE:
207 #define XTENSA_PROP_BT_ALIGN_MASK 0x00000600
209 /* No branch target alignment. */
210 #define XTENSA_PROP_BT_ALIGN_NONE 0x0
211 /* Low priority branch target alignment. */
212 #define XTENSA_PROP_BT_ALIGN_LOW 0x1
213 /* High priority branch target alignment. */
214 #define XTENSA_PROP_BT_ALIGN_HIGH 0x2
215 /* Required branch target alignment. */
216 #define XTENSA_PROP_BT_ALIGN_REQUIRE 0x3
218 #define GET_XTENSA_PROP_BT_ALIGN(flag) \
219 (((unsigned) ((flag) & (XTENSA_PROP_BT_ALIGN_MASK))) >> 9)
220 #define SET_XTENSA_PROP_BT_ALIGN(flag, align) \
221 (((flag) & (~XTENSA_PROP_BT_ALIGN_MASK)) | \
222 (((align) << 9) & XTENSA_PROP_BT_ALIGN_MASK))
225 /* Alignment is specified in the block BEFORE the one that needs
226 alignment. Up to 5 bits. Use GET_XTENSA_PROP_ALIGNMENT(flags) to
227 get the required alignment specified as a power of 2. Use
228 SET_XTENSA_PROP_ALIGNMENT(flags, pow2) to set the required
229 alignment. Be careful of side effects since the SET will evaluate
230 flags twice. Also, note that the SIZE of a block in the property
231 table does not include the alignment size, so the alignment fill
232 must be calculated to determine if two blocks are contiguous.
233 TEXT_ALIGN is not currently implemented but is a placeholder for a
234 possible future implementation. */
236 #define XTENSA_PROP_ALIGN 0x00000800
238 #define XTENSA_PROP_ALIGNMENT_MASK 0x0001f000
240 #define GET_XTENSA_PROP_ALIGNMENT(flag) \
241 (((unsigned) ((flag) & (XTENSA_PROP_ALIGNMENT_MASK))) >> 12)
242 #define SET_XTENSA_PROP_ALIGNMENT(flag, align) \
243 (((flag) & (~XTENSA_PROP_ALIGNMENT_MASK)) | \
244 (((align) << 12) & XTENSA_PROP_ALIGNMENT_MASK))
246 #define XTENSA_PROP_INSN_ABSLIT 0x00020000
249 /* Structure for saving instruction and alignment per-fragment data
250 that will be written to the object file. This structure is
251 equivalent to the actual data that will be written out to the file
252 but is easier to use. We provide a conversion to file flags
253 in frag_flags_to_number. */
255 typedef struct frag_flags_struct frag_flags;
257 struct frag_flags_struct
259 /* is_literal should only be used after xtensa_move_literals.
260 If you need to check if you are generating a literal fragment,
261 then use the generating_literals global. */
263 unsigned is_literal : 1;
264 unsigned is_insn : 1;
265 unsigned is_data : 1;
266 unsigned is_unreachable : 1;
268 struct
270 unsigned is_loop_target : 1;
271 unsigned is_branch_target : 1; /* Branch targets have a priority. */
272 unsigned bt_align_priority : 2;
274 unsigned is_no_density : 1;
275 /* no_longcalls flag does not need to be placed in the object file. */
276 /* is_specific_opcode implies no_transform. */
277 unsigned is_no_transform : 1;
279 unsigned is_no_reorder : 1;
281 /* Uses absolute literal addressing for l32r. */
282 unsigned is_abslit : 1;
283 } insn;
284 unsigned is_align : 1;
285 unsigned alignment : 5;
289 /* Structure for saving information about a block of property data
290 for frags that have the same flags. */
291 struct xtensa_block_info_struct
293 segT sec;
294 bfd_vma offset;
295 size_t size;
296 frag_flags flags;
297 struct xtensa_block_info_struct *next;
301 /* Structure for saving the current state before emitting literals. */
302 typedef struct emit_state_struct
304 const char *name;
305 segT now_seg;
306 subsegT now_subseg;
307 int generating_literals;
308 } emit_state;
311 /* Opcode placement information */
313 typedef unsigned long long bitfield;
314 #define bit_is_set(bit, bf) ((bf) & (0x01ll << (bit)))
315 #define set_bit(bit, bf) ((bf) |= (0x01ll << (bit)))
316 #define clear_bit(bit, bf) ((bf) &= ~(0x01ll << (bit)))
318 #define MAX_FORMATS 32
320 typedef struct op_placement_info_struct
322 int num_formats;
323 /* A number describing how restrictive the issue is for this
324 opcode. For example, an opcode that fits lots of different
325 formats has a high freedom, as does an opcode that fits
326 only one format but many slots in that format. The most
327 restrictive is the opcode that fits only one slot in one
328 format. */
329 int issuef;
330 /* The single format (i.e., if the op can live in a bundle by itself),
331 narrowest format, and widest format the op can be bundled in
332 and their sizes: */
333 xtensa_format single;
334 xtensa_format narrowest;
335 xtensa_format widest;
336 char narrowest_size;
337 char widest_size;
338 char single_size;
340 /* formats is a bitfield with the Nth bit set
341 if the opcode fits in the Nth xtensa_format. */
342 bitfield formats;
344 /* slots[N]'s Mth bit is set if the op fits in the
345 Mth slot of the Nth xtensa_format. */
346 bitfield slots[MAX_FORMATS];
348 /* A count of the number of slots in a given format
349 an op can fit (i.e., the bitcount of the slot field above). */
350 char slots_in_format[MAX_FORMATS];
352 } op_placement_info, *op_placement_info_table;
354 op_placement_info_table op_placement_table;
357 /* Extra expression types. */
359 #define O_pltrel O_md1 /* like O_symbol but use a PLT reloc */
360 #define O_hi16 O_md2 /* use high 16 bits of symbolic value */
361 #define O_lo16 O_md3 /* use low 16 bits of symbolic value */
364 /* Directives. */
366 typedef enum
368 directive_none = 0,
369 directive_literal,
370 directive_density,
371 directive_transform,
372 directive_freeregs,
373 directive_longcalls,
374 directive_literal_prefix,
375 directive_schedule,
376 directive_absolute_literals,
377 directive_last_directive
378 } directiveE;
380 typedef struct
382 const char *name;
383 bfd_boolean can_be_negated;
384 } directive_infoS;
386 const directive_infoS directive_info[] =
388 { "none", FALSE },
389 { "literal", FALSE },
390 { "density", TRUE },
391 { "transform", TRUE },
392 { "freeregs", FALSE },
393 { "longcalls", TRUE },
394 { "literal_prefix", FALSE },
395 { "schedule", TRUE },
396 { "absolute-literals", TRUE }
399 bfd_boolean directive_state[] =
401 FALSE, /* none */
402 FALSE, /* literal */
403 #if !XCHAL_HAVE_DENSITY
404 FALSE, /* density */
405 #else
406 TRUE, /* density */
407 #endif
408 TRUE, /* transform */
409 FALSE, /* freeregs */
410 FALSE, /* longcalls */
411 FALSE, /* literal_prefix */
412 TRUE, /* schedule */
413 #if XSHAL_USE_ABSOLUTE_LITERALS
414 TRUE /* absolute_literals */
415 #else
416 FALSE /* absolute_literals */
417 #endif
421 /* Directive functions. */
423 static void xtensa_begin_directive (int);
424 static void xtensa_end_directive (int);
425 static void xtensa_dwarf2_directive_loc (int);
426 static void xtensa_literal_prefix (char const *, int);
427 static void xtensa_literal_position (int);
428 static void xtensa_literal_pseudo (int);
429 static void xtensa_frequency_pseudo (int);
430 static void xtensa_elf_cons (int);
432 /* Parsing and Idiom Translation. */
434 static bfd_reloc_code_real_type xtensa_elf_suffix (char **, expressionS *);
436 /* Various Other Internal Functions. */
438 static void xtensa_mark_literal_pool_location (void);
439 static addressT get_expanded_loop_offset (xtensa_opcode);
440 static fragS *get_literal_pool_location (segT);
441 static void set_literal_pool_location (segT, fragS *);
442 static void xtensa_set_frag_assembly_state (fragS *);
443 static void finish_vinsn (vliw_insn *);
444 static bfd_boolean emit_single_op (TInsn *);
445 static int total_frag_text_expansion (fragS *);
447 /* Alignment Functions. */
449 static size_t get_text_align_power (int);
450 static addressT get_text_align_max_fill_size (int, bfd_boolean, bfd_boolean);
452 /* Helpers for xtensa_relax_frag(). */
454 static long relax_frag_add_nop (fragS *);
456 /* Accessors for additional per-subsegment information. */
458 static unsigned get_last_insn_flags (segT, subsegT);
459 static void set_last_insn_flags (segT, subsegT, unsigned, bfd_boolean);
460 static float get_subseg_total_freq (segT, subsegT);
461 static float get_subseg_target_freq (segT, subsegT);
462 static void set_subseg_freq (segT, subsegT, float, float);
464 /* Segment list functions. */
466 static void xtensa_move_literals (void);
467 static void xtensa_reorder_segments (void);
468 static void xtensa_switch_to_literal_fragment (emit_state *);
469 static void xtensa_switch_to_non_abs_literal_fragment (emit_state *);
470 static void xtensa_switch_section_emit_state (emit_state *, segT, subsegT);
471 static void xtensa_restore_emit_state (emit_state *);
472 static void cache_literal_section
473 (seg_list *, const char *, segT *, bfd_boolean);
475 /* Import from elf32-xtensa.c in BFD library. */
477 extern char *xtensa_get_property_section_name (asection *, const char *);
479 /* op_placement_info functions. */
481 static void init_op_placement_info_table (void);
482 extern bfd_boolean opcode_fits_format_slot (xtensa_opcode, xtensa_format, int);
483 static int xg_get_single_size (xtensa_opcode);
484 static xtensa_format xg_get_single_format (xtensa_opcode);
486 /* TInsn and IStack functions. */
488 static bfd_boolean tinsn_has_symbolic_operands (const TInsn *);
489 static bfd_boolean tinsn_has_invalid_symbolic_operands (const TInsn *);
490 static bfd_boolean tinsn_has_complex_operands (const TInsn *);
491 static bfd_boolean tinsn_to_insnbuf (TInsn *, xtensa_insnbuf);
492 static bfd_boolean tinsn_check_arguments (const TInsn *);
493 static void tinsn_from_chars (TInsn *, char *, int);
494 static void tinsn_immed_from_frag (TInsn *, fragS *, int);
495 static int get_num_stack_text_bytes (IStack *);
496 static int get_num_stack_literal_bytes (IStack *);
498 /* vliw_insn functions. */
500 static void xg_init_vinsn (vliw_insn *);
501 static void xg_clear_vinsn (vliw_insn *);
502 static bfd_boolean vinsn_has_specific_opcodes (vliw_insn *);
503 static void xg_free_vinsn (vliw_insn *);
504 static bfd_boolean vinsn_to_insnbuf
505 (vliw_insn *, char *, fragS *, bfd_boolean);
506 static void vinsn_from_chars (vliw_insn *, char *);
508 /* Expression Utilities. */
510 bfd_boolean expr_is_const (const expressionS *);
511 offsetT get_expr_const (const expressionS *);
512 void set_expr_const (expressionS *, offsetT);
513 bfd_boolean expr_is_register (const expressionS *);
514 offsetT get_expr_register (const expressionS *);
515 void set_expr_symbol_offset (expressionS *, symbolS *, offsetT);
516 static void set_expr_symbol_offset_diff
517 (expressionS *, symbolS *, symbolS *, offsetT);
518 bfd_boolean expr_is_equal (expressionS *, expressionS *);
519 static void copy_expr (expressionS *, const expressionS *);
521 /* Section renaming. */
523 static void build_section_rename (const char *);
526 /* ISA imported from bfd. */
527 extern xtensa_isa xtensa_default_isa;
529 extern int target_big_endian;
531 static xtensa_opcode xtensa_addi_opcode;
532 static xtensa_opcode xtensa_addmi_opcode;
533 static xtensa_opcode xtensa_call0_opcode;
534 static xtensa_opcode xtensa_call4_opcode;
535 static xtensa_opcode xtensa_call8_opcode;
536 static xtensa_opcode xtensa_call12_opcode;
537 static xtensa_opcode xtensa_callx0_opcode;
538 static xtensa_opcode xtensa_callx4_opcode;
539 static xtensa_opcode xtensa_callx8_opcode;
540 static xtensa_opcode xtensa_callx12_opcode;
541 static xtensa_opcode xtensa_const16_opcode;
542 static xtensa_opcode xtensa_entry_opcode;
543 static xtensa_opcode xtensa_movi_opcode;
544 static xtensa_opcode xtensa_movi_n_opcode;
545 static xtensa_opcode xtensa_isync_opcode;
546 static xtensa_opcode xtensa_jx_opcode;
547 static xtensa_opcode xtensa_l32r_opcode;
548 static xtensa_opcode xtensa_loop_opcode;
549 static xtensa_opcode xtensa_loopnez_opcode;
550 static xtensa_opcode xtensa_loopgtz_opcode;
551 static xtensa_opcode xtensa_nop_opcode;
552 static xtensa_opcode xtensa_nop_n_opcode;
553 static xtensa_opcode xtensa_or_opcode;
554 static xtensa_opcode xtensa_ret_opcode;
555 static xtensa_opcode xtensa_ret_n_opcode;
556 static xtensa_opcode xtensa_retw_opcode;
557 static xtensa_opcode xtensa_retw_n_opcode;
558 static xtensa_opcode xtensa_rsr_lcount_opcode;
559 static xtensa_opcode xtensa_waiti_opcode;
562 /* Command-line Options. */
564 bfd_boolean use_literal_section = TRUE;
565 static bfd_boolean align_targets = TRUE;
566 static bfd_boolean warn_unaligned_branch_targets = FALSE;
567 static bfd_boolean has_a0_b_retw = FALSE;
568 static bfd_boolean workaround_a0_b_retw = FALSE;
569 static bfd_boolean workaround_b_j_loop_end = FALSE;
570 static bfd_boolean workaround_short_loop = FALSE;
571 static bfd_boolean maybe_has_short_loop = FALSE;
572 static bfd_boolean workaround_close_loop_end = FALSE;
573 static bfd_boolean maybe_has_close_loop_end = FALSE;
575 /* When workaround_short_loops is TRUE, all loops with early exits must
576 have at least 3 instructions. workaround_all_short_loops is a modifier
577 to the workaround_short_loop flag. In addition to the
578 workaround_short_loop actions, all straightline loopgtz and loopnez
579 must have at least 3 instructions. */
581 static bfd_boolean workaround_all_short_loops = FALSE;
584 static void
585 xtensa_setup_hw_workarounds (int earliest, int latest)
587 if (earliest > latest)
588 as_fatal (_("illegal range of target hardware versions"));
590 /* Enable all workarounds for pre-T1050.0 hardware. */
591 if (earliest < 105000 || latest < 105000)
593 workaround_a0_b_retw |= TRUE;
594 workaround_b_j_loop_end |= TRUE;
595 workaround_short_loop |= TRUE;
596 workaround_close_loop_end |= TRUE;
597 workaround_all_short_loops |= TRUE;
602 enum
604 option_density = OPTION_MD_BASE,
605 option_no_density,
607 option_relax,
608 option_no_relax,
610 option_link_relax,
611 option_no_link_relax,
613 option_generics,
614 option_no_generics,
616 option_transform,
617 option_no_transform,
619 option_text_section_literals,
620 option_no_text_section_literals,
622 option_absolute_literals,
623 option_no_absolute_literals,
625 option_align_targets,
626 option_no_align_targets,
628 option_warn_unaligned_targets,
630 option_longcalls,
631 option_no_longcalls,
633 option_workaround_a0_b_retw,
634 option_no_workaround_a0_b_retw,
636 option_workaround_b_j_loop_end,
637 option_no_workaround_b_j_loop_end,
639 option_workaround_short_loop,
640 option_no_workaround_short_loop,
642 option_workaround_all_short_loops,
643 option_no_workaround_all_short_loops,
645 option_workaround_close_loop_end,
646 option_no_workaround_close_loop_end,
648 option_no_workarounds,
650 option_rename_section_name,
652 option_prefer_l32r,
653 option_prefer_const16,
655 option_target_hardware
658 const char *md_shortopts = "";
660 struct option md_longopts[] =
662 { "density", no_argument, NULL, option_density },
663 { "no-density", no_argument, NULL, option_no_density },
665 /* Both "relax" and "generics" are deprecated and treated as equivalent
666 to the "transform" option. */
667 { "relax", no_argument, NULL, option_relax },
668 { "no-relax", no_argument, NULL, option_no_relax },
669 { "generics", no_argument, NULL, option_generics },
670 { "no-generics", no_argument, NULL, option_no_generics },
672 { "transform", no_argument, NULL, option_transform },
673 { "no-transform", no_argument, NULL, option_no_transform },
674 { "text-section-literals", no_argument, NULL, option_text_section_literals },
675 { "no-text-section-literals", no_argument, NULL,
676 option_no_text_section_literals },
677 { "absolute-literals", no_argument, NULL, option_absolute_literals },
678 { "no-absolute-literals", no_argument, NULL, option_no_absolute_literals },
679 /* This option was changed from -align-target to -target-align
680 because it conflicted with the "-al" option. */
681 { "target-align", no_argument, NULL, option_align_targets },
682 { "no-target-align", no_argument, NULL, option_no_align_targets },
683 { "warn-unaligned-targets", no_argument, NULL,
684 option_warn_unaligned_targets },
685 { "longcalls", no_argument, NULL, option_longcalls },
686 { "no-longcalls", no_argument, NULL, option_no_longcalls },
688 { "no-workaround-a0-b-retw", no_argument, NULL,
689 option_no_workaround_a0_b_retw },
690 { "workaround-a0-b-retw", no_argument, NULL, option_workaround_a0_b_retw },
692 { "no-workaround-b-j-loop-end", no_argument, NULL,
693 option_no_workaround_b_j_loop_end },
694 { "workaround-b-j-loop-end", no_argument, NULL,
695 option_workaround_b_j_loop_end },
697 { "no-workaround-short-loops", no_argument, NULL,
698 option_no_workaround_short_loop },
699 { "workaround-short-loops", no_argument, NULL,
700 option_workaround_short_loop },
702 { "no-workaround-all-short-loops", no_argument, NULL,
703 option_no_workaround_all_short_loops },
704 { "workaround-all-short-loop", no_argument, NULL,
705 option_workaround_all_short_loops },
707 { "prefer-l32r", no_argument, NULL, option_prefer_l32r },
708 { "prefer-const16", no_argument, NULL, option_prefer_const16 },
710 { "no-workarounds", no_argument, NULL, option_no_workarounds },
712 { "no-workaround-close-loop-end", no_argument, NULL,
713 option_no_workaround_close_loop_end },
714 { "workaround-close-loop-end", no_argument, NULL,
715 option_workaround_close_loop_end },
717 { "rename-section", required_argument, NULL, option_rename_section_name },
719 { "link-relax", no_argument, NULL, option_link_relax },
720 { "no-link-relax", no_argument, NULL, option_no_link_relax },
722 { "target-hardware", required_argument, NULL, option_target_hardware },
724 { NULL, no_argument, NULL, 0 }
727 size_t md_longopts_size = sizeof md_longopts;
731 md_parse_option (int c, char *arg)
733 switch (c)
735 case option_density:
736 as_warn (_("--density option is ignored"));
737 return 1;
738 case option_no_density:
739 as_warn (_("--no-density option is ignored"));
740 return 1;
741 case option_link_relax:
742 linkrelax = 1;
743 return 1;
744 case option_no_link_relax:
745 linkrelax = 0;
746 return 1;
747 case option_generics:
748 as_warn (_("--generics is deprecated; use --transform instead"));
749 return md_parse_option (option_transform, arg);
750 case option_no_generics:
751 as_warn (_("--no-generics is deprecated; use --no-transform instead"));
752 return md_parse_option (option_no_transform, arg);
753 case option_relax:
754 as_warn (_("--relax is deprecated; use --transform instead"));
755 return md_parse_option (option_transform, arg);
756 case option_no_relax:
757 as_warn (_("--no-relax is deprecated; use --no-transform instead"));
758 return md_parse_option (option_no_transform, arg);
759 case option_longcalls:
760 directive_state[directive_longcalls] = TRUE;
761 return 1;
762 case option_no_longcalls:
763 directive_state[directive_longcalls] = FALSE;
764 return 1;
765 case option_text_section_literals:
766 use_literal_section = FALSE;
767 return 1;
768 case option_no_text_section_literals:
769 use_literal_section = TRUE;
770 return 1;
771 case option_absolute_literals:
772 if (!absolute_literals_supported)
774 as_fatal (_("--absolute-literals option not supported in this Xtensa configuration"));
775 return 0;
777 directive_state[directive_absolute_literals] = TRUE;
778 return 1;
779 case option_no_absolute_literals:
780 directive_state[directive_absolute_literals] = FALSE;
781 return 1;
783 case option_workaround_a0_b_retw:
784 workaround_a0_b_retw = TRUE;
785 return 1;
786 case option_no_workaround_a0_b_retw:
787 workaround_a0_b_retw = FALSE;
788 return 1;
789 case option_workaround_b_j_loop_end:
790 workaround_b_j_loop_end = TRUE;
791 return 1;
792 case option_no_workaround_b_j_loop_end:
793 workaround_b_j_loop_end = FALSE;
794 return 1;
796 case option_workaround_short_loop:
797 workaround_short_loop = TRUE;
798 return 1;
799 case option_no_workaround_short_loop:
800 workaround_short_loop = FALSE;
801 return 1;
803 case option_workaround_all_short_loops:
804 workaround_all_short_loops = TRUE;
805 return 1;
806 case option_no_workaround_all_short_loops:
807 workaround_all_short_loops = FALSE;
808 return 1;
810 case option_workaround_close_loop_end:
811 workaround_close_loop_end = TRUE;
812 return 1;
813 case option_no_workaround_close_loop_end:
814 workaround_close_loop_end = FALSE;
815 return 1;
817 case option_no_workarounds:
818 workaround_a0_b_retw = FALSE;
819 workaround_b_j_loop_end = FALSE;
820 workaround_short_loop = FALSE;
821 workaround_all_short_loops = FALSE;
822 workaround_close_loop_end = FALSE;
823 return 1;
825 case option_align_targets:
826 align_targets = TRUE;
827 return 1;
828 case option_no_align_targets:
829 align_targets = FALSE;
830 return 1;
832 case option_warn_unaligned_targets:
833 warn_unaligned_branch_targets = TRUE;
834 return 1;
836 case option_rename_section_name:
837 build_section_rename (arg);
838 return 1;
840 case 'Q':
841 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
842 should be emitted or not. FIXME: Not implemented. */
843 return 1;
845 case option_prefer_l32r:
846 if (prefer_const16)
847 as_fatal (_("prefer-l32r conflicts with prefer-const16"));
848 prefer_l32r = 1;
849 return 1;
851 case option_prefer_const16:
852 if (prefer_l32r)
853 as_fatal (_("prefer-const16 conflicts with prefer-l32r"));
854 prefer_const16 = 1;
855 return 1;
857 case option_target_hardware:
859 int earliest, latest = 0;
860 if (*arg == 0 || *arg == '-')
861 as_fatal (_("invalid target hardware version"));
863 earliest = strtol (arg, &arg, 0);
865 if (*arg == 0)
866 latest = earliest;
867 else if (*arg == '-')
869 if (*++arg == 0)
870 as_fatal (_("invalid target hardware version"));
871 latest = strtol (arg, &arg, 0);
873 if (*arg != 0)
874 as_fatal (_("invalid target hardware version"));
876 xtensa_setup_hw_workarounds (earliest, latest);
877 return 1;
880 case option_transform:
881 /* This option has no affect other than to use the defaults,
882 which are already set. */
883 return 1;
885 case option_no_transform:
886 /* This option turns off all transformations of any kind.
887 However, because we want to preserve the state of other
888 directives, we only change its own field. Thus, before
889 you perform any transformation, always check if transform
890 is available. If you use the functions we provide for this
891 purpose, you will be ok. */
892 directive_state[directive_transform] = FALSE;
893 return 1;
895 default:
896 return 0;
901 void
902 md_show_usage (FILE *stream)
904 fputs ("\n\
905 Xtensa options:\n\
906 --[no-]text-section-literals\n\
907 [Do not] put literals in the text section\n\
908 --[no-]absolute-literals\n\
909 [Do not] default to use non-PC-relative literals\n\
910 --[no-]target-align [Do not] try to align branch targets\n\
911 --[no-]longcalls [Do not] emit 32-bit call sequences\n\
912 --[no-]transform [Do not] transform instructions\n\
913 --rename-section old=new Rename section 'old' to 'new'\n", stream);
917 /* Functions related to the list of current label symbols. */
919 static void
920 xtensa_add_insn_label (symbolS *sym)
922 sym_list *l;
924 if (!free_insn_labels)
925 l = (sym_list *) xmalloc (sizeof (sym_list));
926 else
928 l = free_insn_labels;
929 free_insn_labels = l->next;
932 l->sym = sym;
933 l->next = insn_labels;
934 insn_labels = l;
938 static void
939 xtensa_clear_insn_labels (void)
941 sym_list **pl;
943 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
945 *pl = insn_labels;
946 insn_labels = NULL;
950 /* The "loops_ok" argument is provided to allow ignoring labels that
951 define loop ends. This fixes a bug where the NOPs to align a
952 loop opcode were included in a previous zero-cost loop:
954 loop a0, loopend
955 <loop1 body>
956 loopend:
958 loop a2, loopend2
959 <loop2 body>
961 would become:
963 loop a0, loopend
964 <loop1 body>
965 nop.n <===== bad!
966 loopend:
968 loop a2, loopend2
969 <loop2 body>
971 This argument is used to prevent moving the NOP to before the
972 loop-end label, which is what you want in this special case. */
974 static void
975 xtensa_move_labels (fragS *new_frag, valueT new_offset, bfd_boolean loops_ok)
977 sym_list *lit;
979 for (lit = insn_labels; lit; lit = lit->next)
981 symbolS *lit_sym = lit->sym;
982 if (loops_ok || ! symbol_get_tc (lit_sym)->is_loop_target)
984 S_SET_VALUE (lit_sym, new_offset);
985 symbol_set_frag (lit_sym, new_frag);
991 /* Directive data and functions. */
993 typedef struct state_stackS_struct
995 directiveE directive;
996 bfd_boolean negated;
997 bfd_boolean old_state;
998 const char *file;
999 unsigned int line;
1000 const void *datum;
1001 struct state_stackS_struct *prev;
1002 } state_stackS;
1004 state_stackS *directive_state_stack;
1006 const pseudo_typeS md_pseudo_table[] =
1008 { "align", s_align_bytes, 0 }, /* Defaulting is invalid (0). */
1009 { "literal_position", xtensa_literal_position, 0 },
1010 { "frame", s_ignore, 0 }, /* Formerly used for STABS debugging. */
1011 { "long", xtensa_elf_cons, 4 },
1012 { "word", xtensa_elf_cons, 4 },
1013 { "short", xtensa_elf_cons, 2 },
1014 { "begin", xtensa_begin_directive, 0 },
1015 { "end", xtensa_end_directive, 0 },
1016 { "loc", xtensa_dwarf2_directive_loc, 0 },
1017 { "literal", xtensa_literal_pseudo, 0 },
1018 { "frequency", xtensa_frequency_pseudo, 0 },
1019 { NULL, 0, 0 },
1023 static bfd_boolean
1024 use_transform (void)
1026 /* After md_end, you should be checking frag by frag, rather
1027 than state directives. */
1028 assert (!past_xtensa_end);
1029 return directive_state[directive_transform];
1033 static bfd_boolean
1034 use_longcalls (void)
1036 /* After md_end, you should be checking frag by frag, rather
1037 than state directives. */
1038 assert (!past_xtensa_end);
1039 return directive_state[directive_longcalls] && use_transform ();
1043 static bfd_boolean
1044 do_align_targets (void)
1046 /* After md_end, you should be checking frag by frag, rather
1047 than state directives. */
1048 assert (!past_xtensa_end);
1049 return align_targets && use_transform ();
1053 static void
1054 directive_push (directiveE directive, bfd_boolean negated, const void *datum)
1056 char *file;
1057 unsigned int line;
1058 state_stackS *stack = (state_stackS *) xmalloc (sizeof (state_stackS));
1060 as_where (&file, &line);
1062 stack->directive = directive;
1063 stack->negated = negated;
1064 stack->old_state = directive_state[directive];
1065 stack->file = file;
1066 stack->line = line;
1067 stack->datum = datum;
1068 stack->prev = directive_state_stack;
1069 directive_state_stack = stack;
1071 directive_state[directive] = !negated;
1075 static void
1076 directive_pop (directiveE *directive,
1077 bfd_boolean *negated,
1078 const char **file,
1079 unsigned int *line,
1080 const void **datum)
1082 state_stackS *top = directive_state_stack;
1084 if (!directive_state_stack)
1086 as_bad (_("unmatched end directive"));
1087 *directive = directive_none;
1088 return;
1091 directive_state[directive_state_stack->directive] = top->old_state;
1092 *directive = top->directive;
1093 *negated = top->negated;
1094 *file = top->file;
1095 *line = top->line;
1096 *datum = top->datum;
1097 directive_state_stack = top->prev;
1098 free (top);
1102 static void
1103 directive_balance (void)
1105 while (directive_state_stack)
1107 directiveE directive;
1108 bfd_boolean negated;
1109 const char *file;
1110 unsigned int line;
1111 const void *datum;
1113 directive_pop (&directive, &negated, &file, &line, &datum);
1114 as_warn_where ((char *) file, line,
1115 _(".begin directive with no matching .end directive"));
1120 static bfd_boolean
1121 inside_directive (directiveE dir)
1123 state_stackS *top = directive_state_stack;
1125 while (top && top->directive != dir)
1126 top = top->prev;
1128 return (top != NULL);
1132 static void
1133 get_directive (directiveE *directive, bfd_boolean *negated)
1135 int len;
1136 unsigned i;
1137 char *directive_string;
1139 if (strncmp (input_line_pointer, "no-", 3) != 0)
1140 *negated = FALSE;
1141 else
1143 *negated = TRUE;
1144 input_line_pointer += 3;
1147 len = strspn (input_line_pointer,
1148 "abcdefghijklmnopqrstuvwxyz_-/0123456789.");
1150 /* This code is a hack to make .begin [no-][generics|relax] exactly
1151 equivalent to .begin [no-]transform. We should remove it when
1152 we stop accepting those options. */
1154 if (strncmp (input_line_pointer, "generics", strlen ("generics")) == 0)
1156 as_warn (_("[no-]generics is deprecated; use [no-]transform instead"));
1157 directive_string = "transform";
1159 else if (strncmp (input_line_pointer, "relax", strlen ("relax")) == 0)
1161 as_warn (_("[no-]relax is deprecated; use [no-]transform instead"));
1162 directive_string = "transform";
1164 else
1165 directive_string = input_line_pointer;
1167 for (i = 0; i < sizeof (directive_info) / sizeof (*directive_info); ++i)
1169 if (strncmp (directive_string, directive_info[i].name, len) == 0)
1171 input_line_pointer += len;
1172 *directive = (directiveE) i;
1173 if (*negated && !directive_info[i].can_be_negated)
1174 as_bad (_("directive %s cannot be negated"),
1175 directive_info[i].name);
1176 return;
1180 as_bad (_("unknown directive"));
1181 *directive = (directiveE) XTENSA_UNDEFINED;
1185 static void
1186 xtensa_begin_directive (int ignore ATTRIBUTE_UNUSED)
1188 directiveE directive;
1189 bfd_boolean negated;
1190 emit_state *state;
1191 int len;
1192 lit_state *ls;
1194 get_directive (&directive, &negated);
1195 if (directive == (directiveE) XTENSA_UNDEFINED)
1197 discard_rest_of_line ();
1198 return;
1201 if (cur_vinsn.inside_bundle)
1202 as_bad (_("directives are not valid inside bundles"));
1204 switch (directive)
1206 case directive_literal:
1207 if (!inside_directive (directive_literal))
1209 /* Previous labels go with whatever follows this directive, not with
1210 the literal, so save them now. */
1211 saved_insn_labels = insn_labels;
1212 insn_labels = NULL;
1214 as_warn (_(".begin literal is deprecated; use .literal instead"));
1215 state = (emit_state *) xmalloc (sizeof (emit_state));
1216 xtensa_switch_to_literal_fragment (state);
1217 directive_push (directive_literal, negated, state);
1218 break;
1220 case directive_literal_prefix:
1221 /* Have to flush pending output because a movi relaxed to an l32r
1222 might produce a literal. */
1223 md_flush_pending_output ();
1224 /* Check to see if the current fragment is a literal
1225 fragment. If it is, then this operation is not allowed. */
1226 if (generating_literals)
1228 as_bad (_("cannot set literal_prefix inside literal fragment"));
1229 return;
1232 /* Allocate the literal state for this section and push
1233 onto the directive stack. */
1234 ls = xmalloc (sizeof (lit_state));
1235 assert (ls);
1237 *ls = default_lit_sections;
1239 directive_push (directive_literal_prefix, negated, ls);
1241 /* Parse the new prefix from the input_line_pointer. */
1242 SKIP_WHITESPACE ();
1243 len = strspn (input_line_pointer,
1244 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1245 "abcdefghijklmnopqrstuvwxyz_/0123456789.$");
1247 /* Process the new prefix. */
1248 xtensa_literal_prefix (input_line_pointer, len);
1250 /* Skip the name in the input line. */
1251 input_line_pointer += len;
1252 break;
1254 case directive_freeregs:
1255 /* This information is currently unused, but we'll accept the statement
1256 and just discard the rest of the line. This won't check the syntax,
1257 but it will accept every correct freeregs directive. */
1258 input_line_pointer += strcspn (input_line_pointer, "\n");
1259 directive_push (directive_freeregs, negated, 0);
1260 break;
1262 case directive_schedule:
1263 md_flush_pending_output ();
1264 frag_var (rs_fill, 0, 0, frag_now->fr_subtype,
1265 frag_now->fr_symbol, frag_now->fr_offset, NULL);
1266 directive_push (directive_schedule, negated, 0);
1267 xtensa_set_frag_assembly_state (frag_now);
1268 break;
1270 case directive_density:
1271 as_warn (_(".begin [no-]density is ignored"));
1272 break;
1274 case directive_absolute_literals:
1275 md_flush_pending_output ();
1276 if (!absolute_literals_supported && !negated)
1278 as_warn (_("Xtensa absolute literals option not supported; ignored"));
1279 break;
1281 xtensa_set_frag_assembly_state (frag_now);
1282 directive_push (directive, negated, 0);
1283 break;
1285 default:
1286 md_flush_pending_output ();
1287 xtensa_set_frag_assembly_state (frag_now);
1288 directive_push (directive, negated, 0);
1289 break;
1292 demand_empty_rest_of_line ();
1296 static void
1297 xtensa_end_directive (int ignore ATTRIBUTE_UNUSED)
1299 directiveE begin_directive, end_directive;
1300 bfd_boolean begin_negated, end_negated;
1301 const char *file;
1302 unsigned int line;
1303 emit_state *state;
1304 emit_state **state_ptr;
1305 lit_state *s;
1307 if (cur_vinsn.inside_bundle)
1308 as_bad (_("directives are not valid inside bundles"));
1310 get_directive (&end_directive, &end_negated);
1312 md_flush_pending_output ();
1314 switch (end_directive)
1316 case (directiveE) XTENSA_UNDEFINED:
1317 discard_rest_of_line ();
1318 return;
1320 case directive_density:
1321 as_warn (_(".end [no-]density is ignored"));
1322 demand_empty_rest_of_line ();
1323 break;
1325 case directive_absolute_literals:
1326 if (!absolute_literals_supported && !end_negated)
1328 as_warn (_("Xtensa absolute literals option not supported; ignored"));
1329 demand_empty_rest_of_line ();
1330 return;
1332 break;
1334 default:
1335 break;
1338 state_ptr = &state; /* use state_ptr to avoid type-punning warning */
1339 directive_pop (&begin_directive, &begin_negated, &file, &line,
1340 (const void **) state_ptr);
1342 if (begin_directive != directive_none)
1344 if (begin_directive != end_directive || begin_negated != end_negated)
1346 as_bad (_("does not match begin %s%s at %s:%d"),
1347 begin_negated ? "no-" : "",
1348 directive_info[begin_directive].name, file, line);
1350 else
1352 switch (end_directive)
1354 case directive_literal:
1355 frag_var (rs_fill, 0, 0, 0, NULL, 0, NULL);
1356 xtensa_restore_emit_state (state);
1357 xtensa_set_frag_assembly_state (frag_now);
1358 free (state);
1359 if (!inside_directive (directive_literal))
1361 /* Restore the list of current labels. */
1362 xtensa_clear_insn_labels ();
1363 insn_labels = saved_insn_labels;
1365 break;
1367 case directive_literal_prefix:
1368 /* Restore the default collection sections from saved state. */
1369 s = (lit_state *) state;
1370 assert (s);
1372 if (use_literal_section)
1373 default_lit_sections = *s;
1375 /* free the state storage */
1376 free (s);
1377 break;
1379 case directive_schedule:
1380 case directive_freeregs:
1381 break;
1383 default:
1384 xtensa_set_frag_assembly_state (frag_now);
1385 break;
1390 demand_empty_rest_of_line ();
1394 /* Wrap dwarf2 functions so that we correctly support the .loc directive. */
1396 static bfd_boolean xtensa_loc_directive_seen = FALSE;
1398 static void
1399 xtensa_dwarf2_directive_loc (int x)
1401 xtensa_loc_directive_seen = TRUE;
1402 dwarf2_directive_loc (x);
1406 static void
1407 xtensa_dwarf2_emit_insn (int size, struct dwarf2_line_info *loc)
1409 if (debug_type != DEBUG_DWARF2 && ! xtensa_loc_directive_seen)
1410 return;
1411 xtensa_loc_directive_seen = FALSE;
1412 dwarf2_gen_line_info (frag_now_fix () - size, loc);
1416 /* Place an aligned literal fragment at the current location. */
1418 static void
1419 xtensa_literal_position (int ignore ATTRIBUTE_UNUSED)
1421 md_flush_pending_output ();
1423 if (inside_directive (directive_literal))
1424 as_warn (_(".literal_position inside literal directive; ignoring"));
1425 xtensa_mark_literal_pool_location ();
1427 demand_empty_rest_of_line ();
1428 xtensa_clear_insn_labels ();
1432 /* Support .literal label, expr, ... */
1434 static void
1435 xtensa_literal_pseudo (int ignored ATTRIBUTE_UNUSED)
1437 emit_state state;
1438 char *p, *base_name;
1439 char c;
1440 segT dest_seg;
1442 if (inside_directive (directive_literal))
1444 as_bad (_(".literal not allowed inside .begin literal region"));
1445 ignore_rest_of_line ();
1446 return;
1449 md_flush_pending_output ();
1451 /* Previous labels go with whatever follows this directive, not with
1452 the literal, so save them now. */
1453 saved_insn_labels = insn_labels;
1454 insn_labels = NULL;
1456 /* If we are using text-section literals, then this is the right value... */
1457 dest_seg = now_seg;
1459 base_name = input_line_pointer;
1461 xtensa_switch_to_literal_fragment (&state);
1463 /* ...but if we aren't using text-section-literals, then we
1464 need to put them in the section we just switched to. */
1465 if (use_literal_section || directive_state[directive_absolute_literals])
1466 dest_seg = now_seg;
1468 /* All literals are aligned to four-byte boundaries. */
1469 frag_align (2, 0, 0);
1470 record_alignment (now_seg, 2);
1472 c = get_symbol_end ();
1473 /* Just after name is now '\0'. */
1474 p = input_line_pointer;
1475 *p = c;
1476 SKIP_WHITESPACE ();
1478 if (*input_line_pointer != ',' && *input_line_pointer != ':')
1480 as_bad (_("expected comma or colon after symbol name; "
1481 "rest of line ignored"));
1482 ignore_rest_of_line ();
1483 xtensa_restore_emit_state (&state);
1484 return;
1486 *p = 0;
1488 colon (base_name);
1490 *p = c;
1491 input_line_pointer++; /* skip ',' or ':' */
1493 xtensa_elf_cons (4);
1495 xtensa_restore_emit_state (&state);
1497 /* Restore the list of current labels. */
1498 xtensa_clear_insn_labels ();
1499 insn_labels = saved_insn_labels;
1503 static void
1504 xtensa_literal_prefix (char const *start, int len)
1506 char *name, *linkonce_suffix;
1507 char *newname, *newname4;
1508 size_t linkonce_len;
1510 /* Get a null-terminated copy of the name. */
1511 name = xmalloc (len + 1);
1512 assert (name);
1514 strncpy (name, start, len);
1515 name[len] = 0;
1517 /* Allocate the sections (interesting note: the memory pointing to
1518 the name is actually used for the name by the new section). */
1520 newname = xmalloc (len + strlen (".literal") + 1);
1521 newname4 = xmalloc (len + strlen (".lit4") + 1);
1523 linkonce_len = sizeof (".gnu.linkonce.") - 1;
1524 if (strncmp (name, ".gnu.linkonce.", linkonce_len) == 0
1525 && (linkonce_suffix = strchr (name + linkonce_len, '.')) != 0)
1527 strcpy (newname, ".gnu.linkonce.literal");
1528 strcpy (newname4, ".gnu.linkonce.lit4");
1530 strcat (newname, linkonce_suffix);
1531 strcat (newname4, linkonce_suffix);
1533 else
1535 int suffix_pos = len;
1537 /* If the section name ends with ".text", then replace that suffix
1538 instead of appending an additional suffix. */
1539 if (len >= 5 && strcmp (name + len - 5, ".text") == 0)
1540 suffix_pos -= 5;
1542 strcpy (newname, name);
1543 strcpy (newname4, name);
1545 strcpy (newname + suffix_pos, ".literal");
1546 strcpy (newname4 + suffix_pos, ".lit4");
1549 /* Note that cache_literal_section does not create a segment if
1550 it already exists. */
1551 default_lit_sections.lit_seg = NULL;
1552 default_lit_sections.lit4_seg = NULL;
1554 /* Canonicalizing section names allows renaming literal
1555 sections to occur correctly. */
1556 default_lit_sections.lit_seg_name = tc_canonicalize_symbol_name (newname);
1557 default_lit_sections.lit4_seg_name = tc_canonicalize_symbol_name (newname4);
1559 free (name);
1563 /* Support ".frequency branch_target_frequency fall_through_frequency". */
1565 static void
1566 xtensa_frequency_pseudo (int ignored ATTRIBUTE_UNUSED)
1568 float fall_through_f, target_f;
1570 fall_through_f = (float) strtod (input_line_pointer, &input_line_pointer);
1571 if (fall_through_f < 0)
1573 as_bad (_("fall through frequency must be greater than 0"));
1574 ignore_rest_of_line ();
1575 return;
1578 target_f = (float) strtod (input_line_pointer, &input_line_pointer);
1579 if (target_f < 0)
1581 as_bad (_("branch target frequency must be greater than 0"));
1582 ignore_rest_of_line ();
1583 return;
1586 set_subseg_freq (now_seg, now_subseg, target_f + fall_through_f, target_f);
1588 demand_empty_rest_of_line ();
1592 /* Like normal .long/.short/.word, except support @plt, etc.
1593 Clobbers input_line_pointer, checks end-of-line. */
1595 static void
1596 xtensa_elf_cons (int nbytes)
1598 expressionS exp;
1599 bfd_reloc_code_real_type reloc;
1601 md_flush_pending_output ();
1603 if (cur_vinsn.inside_bundle)
1604 as_bad (_("directives are not valid inside bundles"));
1606 if (is_it_end_of_statement ())
1608 demand_empty_rest_of_line ();
1609 return;
1614 expression (&exp);
1615 if (exp.X_op == O_symbol
1616 && *input_line_pointer == '@'
1617 && ((reloc = xtensa_elf_suffix (&input_line_pointer, &exp))
1618 != BFD_RELOC_NONE))
1620 reloc_howto_type *reloc_howto =
1621 bfd_reloc_type_lookup (stdoutput, reloc);
1623 if (reloc == BFD_RELOC_UNUSED || !reloc_howto)
1624 as_bad (_("unsupported relocation"));
1625 else if ((reloc >= BFD_RELOC_XTENSA_SLOT0_OP
1626 && reloc <= BFD_RELOC_XTENSA_SLOT14_OP)
1627 || (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT
1628 && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT))
1629 as_bad (_("opcode-specific %s relocation used outside "
1630 "an instruction"), reloc_howto->name);
1631 else if (nbytes != (int) bfd_get_reloc_size (reloc_howto))
1632 as_bad (_("%s relocations do not fit in %d bytes"),
1633 reloc_howto->name, nbytes);
1634 else
1636 char *p = frag_more ((int) nbytes);
1637 xtensa_set_frag_assembly_state (frag_now);
1638 fix_new_exp (frag_now, p - frag_now->fr_literal,
1639 nbytes, &exp, 0, reloc);
1642 else
1643 emit_expr (&exp, (unsigned int) nbytes);
1645 while (*input_line_pointer++ == ',');
1647 input_line_pointer--; /* Put terminator back into stream. */
1648 demand_empty_rest_of_line ();
1652 /* Parsing and Idiom Translation. */
1654 /* Parse @plt, etc. and return the desired relocation. */
1655 static bfd_reloc_code_real_type
1656 xtensa_elf_suffix (char **str_p, expressionS *exp_p)
1658 struct map_bfd
1660 char *string;
1661 int length;
1662 bfd_reloc_code_real_type reloc;
1665 char ident[20];
1666 char *str = *str_p;
1667 char *str2;
1668 int ch;
1669 int len;
1670 struct map_bfd *ptr;
1672 #define MAP(str,reloc) { str, sizeof (str) - 1, reloc }
1674 static struct map_bfd mapping[] =
1676 MAP ("l", BFD_RELOC_LO16),
1677 MAP ("h", BFD_RELOC_HI16),
1678 MAP ("plt", BFD_RELOC_XTENSA_PLT),
1679 { (char *) 0, 0, BFD_RELOC_UNUSED }
1682 if (*str++ != '@')
1683 return BFD_RELOC_NONE;
1685 for (ch = *str, str2 = ident;
1686 (str2 < ident + sizeof (ident) - 1
1687 && (ISALNUM (ch) || ch == '@'));
1688 ch = *++str)
1690 *str2++ = (ISLOWER (ch)) ? ch : TOLOWER (ch);
1693 *str2 = '\0';
1694 len = str2 - ident;
1696 ch = ident[0];
1697 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
1698 if (ch == ptr->string[0]
1699 && len == ptr->length
1700 && memcmp (ident, ptr->string, ptr->length) == 0)
1702 /* Now check for "identifier@suffix+constant". */
1703 if (*str == '-' || *str == '+')
1705 char *orig_line = input_line_pointer;
1706 expressionS new_exp;
1708 input_line_pointer = str;
1709 expression (&new_exp);
1710 if (new_exp.X_op == O_constant)
1712 exp_p->X_add_number += new_exp.X_add_number;
1713 str = input_line_pointer;
1716 if (&input_line_pointer != str_p)
1717 input_line_pointer = orig_line;
1720 *str_p = str;
1721 return ptr->reloc;
1724 return BFD_RELOC_UNUSED;
1728 static const char *
1729 expression_end (const char *name)
1731 while (1)
1733 switch (*name)
1735 case '}':
1736 case ';':
1737 case '\0':
1738 case ',':
1739 case ':':
1740 return name;
1741 case ' ':
1742 case '\t':
1743 ++name;
1744 continue;
1745 default:
1746 return 0;
1752 #define ERROR_REG_NUM ((unsigned) -1)
1754 static unsigned
1755 tc_get_register (const char *prefix)
1757 unsigned reg;
1758 const char *next_expr;
1759 const char *old_line_pointer;
1761 SKIP_WHITESPACE ();
1762 old_line_pointer = input_line_pointer;
1764 if (*input_line_pointer == '$')
1765 ++input_line_pointer;
1767 /* Accept "sp" as a synonym for "a1". */
1768 if (input_line_pointer[0] == 's' && input_line_pointer[1] == 'p'
1769 && expression_end (input_line_pointer + 2))
1771 input_line_pointer += 2;
1772 return 1; /* AR[1] */
1775 while (*input_line_pointer++ == *prefix++)
1777 --input_line_pointer;
1778 --prefix;
1780 if (*prefix)
1782 as_bad (_("bad register name: %s"), old_line_pointer);
1783 return ERROR_REG_NUM;
1786 if (!ISDIGIT ((unsigned char) *input_line_pointer))
1788 as_bad (_("bad register number: %s"), input_line_pointer);
1789 return ERROR_REG_NUM;
1792 reg = 0;
1794 while (ISDIGIT ((int) *input_line_pointer))
1795 reg = reg * 10 + *input_line_pointer++ - '0';
1797 if (!(next_expr = expression_end (input_line_pointer)))
1799 as_bad (_("bad register name: %s"), old_line_pointer);
1800 return ERROR_REG_NUM;
1803 input_line_pointer = (char *) next_expr;
1805 return reg;
1809 static void
1810 expression_maybe_register (xtensa_opcode opc, int opnd, expressionS *tok)
1812 xtensa_isa isa = xtensa_default_isa;
1814 /* Check if this is an immediate operand. */
1815 if (xtensa_operand_is_register (isa, opc, opnd) == 0)
1817 bfd_reloc_code_real_type reloc;
1818 segT t = expression (tok);
1819 if (t == absolute_section
1820 && xtensa_operand_is_PCrelative (isa, opc, opnd) == 1)
1822 assert (tok->X_op == O_constant);
1823 tok->X_op = O_symbol;
1824 tok->X_add_symbol = &abs_symbol;
1827 if ((tok->X_op == O_constant || tok->X_op == O_symbol)
1828 && (reloc = xtensa_elf_suffix (&input_line_pointer, tok))
1829 && (reloc != BFD_RELOC_NONE))
1831 switch (reloc)
1833 default:
1834 case BFD_RELOC_UNUSED:
1835 as_bad (_("unsupported relocation"));
1836 break;
1838 case BFD_RELOC_XTENSA_PLT:
1839 tok->X_op = O_pltrel;
1840 break;
1842 case BFD_RELOC_LO16:
1843 if (tok->X_op == O_constant)
1844 tok->X_add_number &= 0xffff;
1845 else
1846 tok->X_op = O_lo16;
1847 break;
1849 case BFD_RELOC_HI16:
1850 if (tok->X_op == O_constant)
1851 tok->X_add_number = ((unsigned) tok->X_add_number) >> 16;
1852 else
1853 tok->X_op = O_hi16;
1854 break;
1858 else
1860 xtensa_regfile opnd_rf = xtensa_operand_regfile (isa, opc, opnd);
1861 unsigned reg = tc_get_register (xtensa_regfile_shortname (isa, opnd_rf));
1863 if (reg != ERROR_REG_NUM) /* Already errored */
1865 uint32 buf = reg;
1866 if (xtensa_operand_encode (isa, opc, opnd, &buf))
1867 as_bad (_("register number out of range"));
1870 tok->X_op = O_register;
1871 tok->X_add_symbol = 0;
1872 tok->X_add_number = reg;
1877 /* Split up the arguments for an opcode or pseudo-op. */
1879 static int
1880 tokenize_arguments (char **args, char *str)
1882 char *old_input_line_pointer;
1883 bfd_boolean saw_comma = FALSE;
1884 bfd_boolean saw_arg = FALSE;
1885 bfd_boolean saw_colon = FALSE;
1886 int num_args = 0;
1887 char *arg_end, *arg;
1888 int arg_len;
1890 /* Save and restore input_line_pointer around this function. */
1891 old_input_line_pointer = input_line_pointer;
1892 input_line_pointer = str;
1894 while (*input_line_pointer)
1896 SKIP_WHITESPACE ();
1897 switch (*input_line_pointer)
1899 case '\0':
1900 case '}':
1901 goto fini;
1903 case ':':
1904 input_line_pointer++;
1905 if (saw_comma || saw_colon || !saw_arg)
1906 goto err;
1907 saw_colon = TRUE;
1908 break;
1910 case ',':
1911 input_line_pointer++;
1912 if (saw_comma || saw_colon || !saw_arg)
1913 goto err;
1914 saw_comma = TRUE;
1915 break;
1917 default:
1918 if (!saw_comma && !saw_colon && saw_arg)
1919 goto err;
1921 arg_end = input_line_pointer + 1;
1922 while (!expression_end (arg_end))
1923 arg_end += 1;
1925 arg_len = arg_end - input_line_pointer;
1926 arg = (char *) xmalloc ((saw_colon ? 1 : 0) + arg_len + 1);
1927 args[num_args] = arg;
1929 if (saw_colon)
1930 *arg++ = ':';
1931 strncpy (arg, input_line_pointer, arg_len);
1932 arg[arg_len] = '\0';
1934 input_line_pointer = arg_end;
1935 num_args += 1;
1936 saw_comma = FALSE;
1937 saw_colon = FALSE;
1938 saw_arg = TRUE;
1939 break;
1943 fini:
1944 if (saw_comma || saw_colon)
1945 goto err;
1946 input_line_pointer = old_input_line_pointer;
1947 return num_args;
1949 err:
1950 if (saw_comma)
1951 as_bad (_("extra comma"));
1952 else if (saw_colon)
1953 as_bad (_("extra colon"));
1954 else if (!saw_arg)
1955 as_bad (_("missing argument"));
1956 else
1957 as_bad (_("missing comma or colon"));
1958 input_line_pointer = old_input_line_pointer;
1959 return -1;
1963 /* Parse the arguments to an opcode. Return TRUE on error. */
1965 static bfd_boolean
1966 parse_arguments (TInsn *insn, int num_args, char **arg_strings)
1968 expressionS *tok, *last_tok;
1969 xtensa_opcode opcode = insn->opcode;
1970 bfd_boolean had_error = TRUE;
1971 xtensa_isa isa = xtensa_default_isa;
1972 int n, num_regs = 0;
1973 int opcode_operand_count;
1974 int opnd_cnt, last_opnd_cnt;
1975 unsigned int next_reg = 0;
1976 char *old_input_line_pointer;
1978 if (insn->insn_type == ITYPE_LITERAL)
1979 opcode_operand_count = 1;
1980 else
1981 opcode_operand_count = xtensa_opcode_num_operands (isa, opcode);
1983 tok = insn->tok;
1984 memset (tok, 0, sizeof (*tok) * MAX_INSN_ARGS);
1986 /* Save and restore input_line_pointer around this function. */
1987 old_input_line_pointer = input_line_pointer;
1989 last_tok = 0;
1990 last_opnd_cnt = -1;
1991 opnd_cnt = 0;
1993 /* Skip invisible operands. */
1994 while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0)
1996 opnd_cnt += 1;
1997 tok++;
2000 for (n = 0; n < num_args; n++)
2002 input_line_pointer = arg_strings[n];
2003 if (*input_line_pointer == ':')
2005 xtensa_regfile opnd_rf;
2006 input_line_pointer++;
2007 if (num_regs == 0)
2008 goto err;
2009 assert (opnd_cnt > 0);
2010 num_regs--;
2011 opnd_rf = xtensa_operand_regfile (isa, opcode, last_opnd_cnt);
2012 if (next_reg
2013 != tc_get_register (xtensa_regfile_shortname (isa, opnd_rf)))
2014 as_warn (_("incorrect register number, ignoring"));
2015 next_reg++;
2017 else
2019 if (opnd_cnt >= opcode_operand_count)
2021 as_warn (_("too many arguments"));
2022 goto err;
2024 assert (opnd_cnt < MAX_INSN_ARGS);
2026 expression_maybe_register (opcode, opnd_cnt, tok);
2027 next_reg = tok->X_add_number + 1;
2029 if (tok->X_op == O_illegal || tok->X_op == O_absent)
2030 goto err;
2031 if (xtensa_operand_is_register (isa, opcode, opnd_cnt) == 1)
2033 num_regs = xtensa_operand_num_regs (isa, opcode, opnd_cnt) - 1;
2034 /* minus 1 because we are seeing one right now */
2036 else
2037 num_regs = 0;
2039 last_tok = tok;
2040 last_opnd_cnt = opnd_cnt;
2044 opnd_cnt += 1;
2045 tok++;
2047 while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0);
2051 if (num_regs > 0 && ((int) next_reg != last_tok->X_add_number + 1))
2052 goto err;
2054 insn->ntok = tok - insn->tok;
2055 had_error = FALSE;
2057 err:
2058 input_line_pointer = old_input_line_pointer;
2059 return had_error;
2063 static int
2064 get_invisible_operands (TInsn *insn)
2066 xtensa_isa isa = xtensa_default_isa;
2067 static xtensa_insnbuf slotbuf = NULL;
2068 xtensa_format fmt;
2069 xtensa_opcode opc = insn->opcode;
2070 int slot, opnd, fmt_found;
2071 unsigned val;
2073 if (!slotbuf)
2074 slotbuf = xtensa_insnbuf_alloc (isa);
2076 /* Find format/slot where this can be encoded. */
2077 fmt_found = 0;
2078 slot = 0;
2079 for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
2081 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
2083 if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opc) == 0)
2085 fmt_found = 1;
2086 break;
2089 if (fmt_found) break;
2092 if (!fmt_found)
2094 as_bad (_("cannot encode opcode \"%s\""), xtensa_opcode_name (isa, opc));
2095 return -1;
2098 /* First encode all the visible operands
2099 (to deal with shared field operands). */
2100 for (opnd = 0; opnd < insn->ntok; opnd++)
2102 if (xtensa_operand_is_visible (isa, opc, opnd) == 1
2103 && (insn->tok[opnd].X_op == O_register
2104 || insn->tok[opnd].X_op == O_constant))
2106 val = insn->tok[opnd].X_add_number;
2107 xtensa_operand_encode (isa, opc, opnd, &val);
2108 xtensa_operand_set_field (isa, opc, opnd, fmt, slot, slotbuf, val);
2112 /* Then pull out the values for the invisible ones. */
2113 for (opnd = 0; opnd < insn->ntok; opnd++)
2115 if (xtensa_operand_is_visible (isa, opc, opnd) == 0)
2117 xtensa_operand_get_field (isa, opc, opnd, fmt, slot, slotbuf, &val);
2118 xtensa_operand_decode (isa, opc, opnd, &val);
2119 insn->tok[opnd].X_add_number = val;
2120 if (xtensa_operand_is_register (isa, opc, opnd) == 1)
2121 insn->tok[opnd].X_op = O_register;
2122 else
2123 insn->tok[opnd].X_op = O_constant;
2127 return 0;
2131 static void
2132 xg_reverse_shift_count (char **cnt_argp)
2134 char *cnt_arg, *new_arg;
2135 cnt_arg = *cnt_argp;
2137 /* replace the argument with "31-(argument)" */
2138 new_arg = (char *) xmalloc (strlen (cnt_arg) + 6);
2139 sprintf (new_arg, "31-(%s)", cnt_arg);
2141 free (cnt_arg);
2142 *cnt_argp = new_arg;
2146 /* If "arg" is a constant expression, return non-zero with the value
2147 in *valp. */
2149 static int
2150 xg_arg_is_constant (char *arg, offsetT *valp)
2152 expressionS exp;
2153 char *save_ptr = input_line_pointer;
2155 input_line_pointer = arg;
2156 expression (&exp);
2157 input_line_pointer = save_ptr;
2159 if (exp.X_op == O_constant)
2161 *valp = exp.X_add_number;
2162 return 1;
2165 return 0;
2169 static void
2170 xg_replace_opname (char **popname, char *newop)
2172 free (*popname);
2173 *popname = (char *) xmalloc (strlen (newop) + 1);
2174 strcpy (*popname, newop);
2178 static int
2179 xg_check_num_args (int *pnum_args,
2180 int expected_num,
2181 char *opname,
2182 char **arg_strings)
2184 int num_args = *pnum_args;
2186 if (num_args < expected_num)
2188 as_bad (_("not enough operands (%d) for '%s'; expected %d"),
2189 num_args, opname, expected_num);
2190 return -1;
2193 if (num_args > expected_num)
2195 as_warn (_("too many operands (%d) for '%s'; expected %d"),
2196 num_args, opname, expected_num);
2197 while (num_args-- > expected_num)
2199 free (arg_strings[num_args]);
2200 arg_strings[num_args] = 0;
2202 *pnum_args = expected_num;
2203 return -1;
2206 return 0;
2210 /* If the register is not specified as part of the opcode,
2211 then get it from the operand and move it to the opcode. */
2213 static int
2214 xg_translate_sysreg_op (char **popname, int *pnum_args, char **arg_strings)
2216 xtensa_isa isa = xtensa_default_isa;
2217 xtensa_sysreg sr;
2218 char *opname, *new_opname;
2219 const char *sr_name;
2220 int is_user, is_write;
2221 bfd_boolean has_underbar = FALSE;
2223 opname = *popname;
2224 if (*opname == '_')
2226 has_underbar = TRUE;
2227 opname += 1;
2229 is_user = (opname[1] == 'u');
2230 is_write = (opname[0] == 'w');
2232 /* Opname == [rw]ur or [rwx]sr... */
2234 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
2235 return -1;
2237 /* Check if the argument is a symbolic register name. */
2238 sr = xtensa_sysreg_lookup_name (isa, arg_strings[1]);
2239 /* Handle WSR to "INTSET" as a special case. */
2240 if (sr == XTENSA_UNDEFINED && is_write && !is_user
2241 && !strcasecmp (arg_strings[1], "intset"))
2242 sr = xtensa_sysreg_lookup_name (isa, "interrupt");
2243 if (sr == XTENSA_UNDEFINED
2244 || (xtensa_sysreg_is_user (isa, sr) == 1) != is_user)
2246 /* Maybe it's a register number.... */
2247 offsetT val;
2248 if (!xg_arg_is_constant (arg_strings[1], &val))
2250 as_bad (_("invalid register '%s' for '%s' instruction"),
2251 arg_strings[1], opname);
2252 return -1;
2254 sr = xtensa_sysreg_lookup (isa, val, is_user);
2255 if (sr == XTENSA_UNDEFINED)
2257 as_bad (_("invalid register number (%ld) for '%s' instruction"),
2258 val, opname);
2259 return -1;
2263 /* Remove the last argument, which is now part of the opcode. */
2264 free (arg_strings[1]);
2265 arg_strings[1] = 0;
2266 *pnum_args = 1;
2268 /* Translate the opcode. */
2269 sr_name = xtensa_sysreg_name (isa, sr);
2270 /* Another special case for "WSR.INTSET".... */
2271 if (is_write && !is_user && !strcasecmp ("interrupt", sr_name))
2272 sr_name = "intset";
2273 new_opname = (char *) xmalloc (strlen (sr_name) + 6);
2274 sprintf (new_opname, "%s%s.%s", (has_underbar ? "_" : ""),
2275 *popname, sr_name);
2276 free (*popname);
2277 *popname = new_opname;
2279 return 0;
2283 static int
2284 xtensa_translate_old_userreg_ops (char **popname)
2286 xtensa_isa isa = xtensa_default_isa;
2287 xtensa_sysreg sr;
2288 char *opname, *new_opname;
2289 const char *sr_name;
2290 bfd_boolean has_underbar = FALSE;
2292 opname = *popname;
2293 if (opname[0] == '_')
2295 has_underbar = TRUE;
2296 opname += 1;
2299 sr = xtensa_sysreg_lookup_name (isa, opname + 1);
2300 if (sr != XTENSA_UNDEFINED)
2302 /* The new default name ("nnn") is different from the old default
2303 name ("URnnn"). The old default is handled below, and we don't
2304 want to recognize [RW]nnn, so do nothing if the name is the (new)
2305 default. */
2306 static char namebuf[10];
2307 sprintf (namebuf, "%d", xtensa_sysreg_number (isa, sr));
2308 if (strcmp (namebuf, opname + 1) == 0)
2309 return 0;
2311 else
2313 offsetT val;
2314 char *end;
2316 /* Only continue if the reg name is "URnnn". */
2317 if (opname[1] != 'u' || opname[2] != 'r')
2318 return 0;
2319 val = strtoul (opname + 3, &end, 10);
2320 if (*end != '\0')
2321 return 0;
2323 sr = xtensa_sysreg_lookup (isa, val, 1);
2324 if (sr == XTENSA_UNDEFINED)
2326 as_bad (_("invalid register number (%ld) for '%s'"),
2327 val, opname);
2328 return -1;
2332 /* Translate the opcode. */
2333 sr_name = xtensa_sysreg_name (isa, sr);
2334 new_opname = (char *) xmalloc (strlen (sr_name) + 6);
2335 sprintf (new_opname, "%s%cur.%s", (has_underbar ? "_" : ""),
2336 opname[0], sr_name);
2337 free (*popname);
2338 *popname = new_opname;
2340 return 0;
2344 static int
2345 xtensa_translate_zero_immed (char *old_op,
2346 char *new_op,
2347 char **popname,
2348 int *pnum_args,
2349 char **arg_strings)
2351 char *opname;
2352 offsetT val;
2354 opname = *popname;
2355 assert (opname[0] != '_');
2357 if (strcmp (opname, old_op) != 0)
2358 return 0;
2360 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2361 return -1;
2362 if (xg_arg_is_constant (arg_strings[1], &val) && val == 0)
2364 xg_replace_opname (popname, new_op);
2365 free (arg_strings[1]);
2366 arg_strings[1] = arg_strings[2];
2367 arg_strings[2] = 0;
2368 *pnum_args = 2;
2371 return 0;
2375 /* If the instruction is an idiom (i.e., a built-in macro), translate it.
2376 Returns non-zero if an error was found. */
2378 static int
2379 xg_translate_idioms (char **popname, int *pnum_args, char **arg_strings)
2381 char *opname = *popname;
2382 bfd_boolean has_underbar = FALSE;
2384 if (cur_vinsn.inside_bundle)
2385 return 0;
2387 if (*opname == '_')
2389 has_underbar = TRUE;
2390 opname += 1;
2393 if (strcmp (opname, "mov") == 0)
2395 if (use_transform () && !has_underbar && density_supported)
2396 xg_replace_opname (popname, "mov.n");
2397 else
2399 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
2400 return -1;
2401 xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
2402 arg_strings[2] = (char *) xmalloc (strlen (arg_strings[1]) + 1);
2403 strcpy (arg_strings[2], arg_strings[1]);
2404 *pnum_args = 3;
2406 return 0;
2409 if (strcmp (opname, "bbsi.l") == 0)
2411 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2412 return -1;
2413 xg_replace_opname (popname, (has_underbar ? "_bbsi" : "bbsi"));
2414 if (target_big_endian)
2415 xg_reverse_shift_count (&arg_strings[1]);
2416 return 0;
2419 if (strcmp (opname, "bbci.l") == 0)
2421 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2422 return -1;
2423 xg_replace_opname (popname, (has_underbar ? "_bbci" : "bbci"));
2424 if (target_big_endian)
2425 xg_reverse_shift_count (&arg_strings[1]);
2426 return 0;
2429 if (xtensa_nop_opcode == XTENSA_UNDEFINED
2430 && strcmp (opname, "nop") == 0)
2432 if (use_transform () && !has_underbar && density_supported)
2433 xg_replace_opname (popname, "nop.n");
2434 else
2436 if (xg_check_num_args (pnum_args, 0, opname, arg_strings))
2437 return -1;
2438 xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
2439 arg_strings[0] = (char *) xmalloc (3);
2440 arg_strings[1] = (char *) xmalloc (3);
2441 arg_strings[2] = (char *) xmalloc (3);
2442 strcpy (arg_strings[0], "a1");
2443 strcpy (arg_strings[1], "a1");
2444 strcpy (arg_strings[2], "a1");
2445 *pnum_args = 3;
2447 return 0;
2450 /* Recognize [RW]UR and [RWX]SR. */
2451 if ((((opname[0] == 'r' || opname[0] == 'w')
2452 && (opname[1] == 'u' || opname[1] == 's'))
2453 || (opname[0] == 'x' && opname[1] == 's'))
2454 && opname[2] == 'r'
2455 && opname[3] == '\0')
2456 return xg_translate_sysreg_op (popname, pnum_args, arg_strings);
2458 /* Backward compatibility for RUR and WUR: Recognize [RW]UR<nnn> and
2459 [RW]<name> if <name> is the non-default name of a user register. */
2460 if ((opname[0] == 'r' || opname[0] == 'w')
2461 && xtensa_opcode_lookup (xtensa_default_isa, opname) == XTENSA_UNDEFINED)
2462 return xtensa_translate_old_userreg_ops (popname);
2464 /* Relax branches that don't allow comparisons against an immediate value
2465 of zero to the corresponding branches with implicit zero immediates. */
2466 if (!has_underbar && use_transform ())
2468 if (xtensa_translate_zero_immed ("bnei", "bnez", popname,
2469 pnum_args, arg_strings))
2470 return -1;
2472 if (xtensa_translate_zero_immed ("beqi", "beqz", popname,
2473 pnum_args, arg_strings))
2474 return -1;
2476 if (xtensa_translate_zero_immed ("bgei", "bgez", popname,
2477 pnum_args, arg_strings))
2478 return -1;
2480 if (xtensa_translate_zero_immed ("blti", "bltz", popname,
2481 pnum_args, arg_strings))
2482 return -1;
2485 return 0;
2489 /* Functions for dealing with the Xtensa ISA. */
2491 /* Currently the assembler only allows us to use a single target per
2492 fragment. Because of this, only one operand for a given
2493 instruction may be symbolic. If there is a PC-relative operand,
2494 the last one is chosen. Otherwise, the result is the number of the
2495 last immediate operand, and if there are none of those, we fail and
2496 return -1. */
2498 static int
2499 get_relaxable_immed (xtensa_opcode opcode)
2501 int last_immed = -1;
2502 int noperands, opi;
2504 if (opcode == XTENSA_UNDEFINED)
2505 return -1;
2507 noperands = xtensa_opcode_num_operands (xtensa_default_isa, opcode);
2508 for (opi = noperands - 1; opi >= 0; opi--)
2510 if (xtensa_operand_is_visible (xtensa_default_isa, opcode, opi) == 0)
2511 continue;
2512 if (xtensa_operand_is_PCrelative (xtensa_default_isa, opcode, opi) == 1)
2513 return opi;
2514 if (last_immed == -1
2515 && xtensa_operand_is_register (xtensa_default_isa, opcode, opi) == 0)
2516 last_immed = opi;
2518 return last_immed;
2522 static xtensa_opcode
2523 get_opcode_from_buf (const char *buf, int slot)
2525 static xtensa_insnbuf insnbuf = NULL;
2526 static xtensa_insnbuf slotbuf = NULL;
2527 xtensa_isa isa = xtensa_default_isa;
2528 xtensa_format fmt;
2530 if (!insnbuf)
2532 insnbuf = xtensa_insnbuf_alloc (isa);
2533 slotbuf = xtensa_insnbuf_alloc (isa);
2536 xtensa_insnbuf_from_chars (isa, insnbuf, buf, 0);
2537 fmt = xtensa_format_decode (isa, insnbuf);
2538 if (fmt == XTENSA_UNDEFINED)
2539 return XTENSA_UNDEFINED;
2541 if (slot >= xtensa_format_num_slots (isa, fmt))
2542 return XTENSA_UNDEFINED;
2544 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
2545 return xtensa_opcode_decode (isa, fmt, slot, slotbuf);
2549 #ifdef TENSILICA_DEBUG
2551 /* For debugging, print out the mapping of opcode numbers to opcodes. */
2553 static void
2554 xtensa_print_insn_table (void)
2556 int num_opcodes, num_operands;
2557 xtensa_opcode opcode;
2558 xtensa_isa isa = xtensa_default_isa;
2560 num_opcodes = xtensa_isa_num_opcodes (xtensa_default_isa);
2561 for (opcode = 0; opcode < num_opcodes; opcode++)
2563 int opn;
2564 fprintf (stderr, "%d: %s: ", opcode, xtensa_opcode_name (isa, opcode));
2565 num_operands = xtensa_opcode_num_operands (isa, opcode);
2566 for (opn = 0; opn < num_operands; opn++)
2568 if (xtensa_operand_is_visible (isa, opcode, opn) == 0)
2569 continue;
2570 if (xtensa_operand_is_register (isa, opcode, opn) == 1)
2572 xtensa_regfile opnd_rf =
2573 xtensa_operand_regfile (isa, opcode, opn);
2574 fprintf (stderr, "%s ", xtensa_regfile_shortname (isa, opnd_rf));
2576 else if (xtensa_operand_is_PCrelative (isa, opcode, opn) == 1)
2577 fputs ("[lLr] ", stderr);
2578 else
2579 fputs ("i ", stderr);
2581 fprintf (stderr, "\n");
2586 static void
2587 print_vliw_insn (xtensa_insnbuf vbuf)
2589 xtensa_isa isa = xtensa_default_isa;
2590 xtensa_format f = xtensa_format_decode (isa, vbuf);
2591 xtensa_insnbuf sbuf = xtensa_insnbuf_alloc (isa);
2592 int op;
2594 fprintf (stderr, "format = %d\n", f);
2596 for (op = 0; op < xtensa_format_num_slots (isa, f); op++)
2598 xtensa_opcode opcode;
2599 const char *opname;
2600 int operands;
2602 xtensa_format_get_slot (isa, f, op, vbuf, sbuf);
2603 opcode = xtensa_opcode_decode (isa, f, op, sbuf);
2604 opname = xtensa_opcode_name (isa, opcode);
2606 fprintf (stderr, "op in slot %i is %s;\n", op, opname);
2607 fprintf (stderr, " operands = ");
2608 for (operands = 0;
2609 operands < xtensa_opcode_num_operands (isa, opcode);
2610 operands++)
2612 unsigned int val;
2613 if (xtensa_operand_is_visible (isa, opcode, operands) == 0)
2614 continue;
2615 xtensa_operand_get_field (isa, opcode, operands, f, op, sbuf, &val);
2616 xtensa_operand_decode (isa, opcode, operands, &val);
2617 fprintf (stderr, "%d ", val);
2619 fprintf (stderr, "\n");
2621 xtensa_insnbuf_free (isa, sbuf);
2624 #endif /* TENSILICA_DEBUG */
2627 static bfd_boolean
2628 is_direct_call_opcode (xtensa_opcode opcode)
2630 xtensa_isa isa = xtensa_default_isa;
2631 int n, num_operands;
2633 if (xtensa_opcode_is_call (isa, opcode) == 0)
2634 return FALSE;
2636 num_operands = xtensa_opcode_num_operands (isa, opcode);
2637 for (n = 0; n < num_operands; n++)
2639 if (xtensa_operand_is_register (isa, opcode, n) == 0
2640 && xtensa_operand_is_PCrelative (isa, opcode, n) == 1)
2641 return TRUE;
2643 return FALSE;
2647 /* Convert from BFD relocation type code to slot and operand number.
2648 Returns non-zero on failure. */
2650 static int
2651 decode_reloc (bfd_reloc_code_real_type reloc, int *slot, bfd_boolean *is_alt)
2653 if (reloc >= BFD_RELOC_XTENSA_SLOT0_OP
2654 && reloc <= BFD_RELOC_XTENSA_SLOT14_OP)
2656 *slot = reloc - BFD_RELOC_XTENSA_SLOT0_OP;
2657 *is_alt = FALSE;
2659 else if (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT
2660 && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT)
2662 *slot = reloc - BFD_RELOC_XTENSA_SLOT0_ALT;
2663 *is_alt = TRUE;
2665 else
2666 return -1;
2668 return 0;
2672 /* Convert from slot number to BFD relocation type code for the
2673 standard PC-relative relocations. Return BFD_RELOC_NONE on
2674 failure. */
2676 static bfd_reloc_code_real_type
2677 encode_reloc (int slot)
2679 if (slot < 0 || slot > 14)
2680 return BFD_RELOC_NONE;
2682 return BFD_RELOC_XTENSA_SLOT0_OP + slot;
2686 /* Convert from slot numbers to BFD relocation type code for the
2687 "alternate" relocations. Return BFD_RELOC_NONE on failure. */
2689 static bfd_reloc_code_real_type
2690 encode_alt_reloc (int slot)
2692 if (slot < 0 || slot > 14)
2693 return BFD_RELOC_NONE;
2695 return BFD_RELOC_XTENSA_SLOT0_ALT + slot;
2699 static void
2700 xtensa_insnbuf_set_operand (xtensa_insnbuf slotbuf,
2701 xtensa_format fmt,
2702 int slot,
2703 xtensa_opcode opcode,
2704 int operand,
2705 uint32 value,
2706 const char *file,
2707 unsigned int line)
2709 uint32 valbuf = value;
2711 if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf))
2713 if (xtensa_operand_is_PCrelative (xtensa_default_isa, opcode, operand)
2714 == 1)
2715 as_bad_where ((char *) file, line,
2716 _("operand %u is out of range for '%s'"), value,
2717 xtensa_opcode_name (xtensa_default_isa, opcode));
2718 else
2719 as_bad_where ((char *) file, line,
2720 _("operand %u is invalid for '%s'"), value,
2721 xtensa_opcode_name (xtensa_default_isa, opcode));
2722 return;
2725 xtensa_operand_set_field (xtensa_default_isa, opcode, operand, fmt, slot,
2726 slotbuf, valbuf);
2730 static uint32
2731 xtensa_insnbuf_get_operand (xtensa_insnbuf slotbuf,
2732 xtensa_format fmt,
2733 int slot,
2734 xtensa_opcode opcode,
2735 int opnum)
2737 uint32 val = 0;
2738 (void) xtensa_operand_get_field (xtensa_default_isa, opcode, opnum,
2739 fmt, slot, slotbuf, &val);
2740 (void) xtensa_operand_decode (xtensa_default_isa, opcode, opnum, &val);
2741 return val;
2745 /* Checks for rules from xtensa-relax tables. */
2747 /* The routine xg_instruction_matches_option_term must return TRUE
2748 when a given option term is true. The meaning of all of the option
2749 terms is given interpretation by this function. This is needed when
2750 an option depends on the state of a directive, but there are no such
2751 options in use right now. */
2753 static bfd_boolean
2754 xg_instruction_matches_option_term (TInsn *insn ATTRIBUTE_UNUSED,
2755 const ReqOrOption *option)
2757 if (strcmp (option->option_name, "realnop") == 0
2758 || strncmp (option->option_name, "IsaUse", 6) == 0)
2760 /* These conditions were evaluated statically when building the
2761 relaxation table. There's no need to reevaluate them now. */
2762 return TRUE;
2764 else
2766 as_fatal (_("internal error: unknown option name '%s'"),
2767 option->option_name);
2772 static bfd_boolean
2773 xg_instruction_matches_or_options (TInsn *insn,
2774 const ReqOrOptionList *or_option)
2776 const ReqOrOption *option;
2777 /* Must match each of the AND terms. */
2778 for (option = or_option; option != NULL; option = option->next)
2780 if (xg_instruction_matches_option_term (insn, option))
2781 return TRUE;
2783 return FALSE;
2787 static bfd_boolean
2788 xg_instruction_matches_options (TInsn *insn, const ReqOptionList *options)
2790 const ReqOption *req_options;
2791 /* Must match each of the AND terms. */
2792 for (req_options = options;
2793 req_options != NULL;
2794 req_options = req_options->next)
2796 /* Must match one of the OR clauses. */
2797 if (!xg_instruction_matches_or_options (insn,
2798 req_options->or_option_terms))
2799 return FALSE;
2801 return TRUE;
2805 /* Return the transition rule that matches or NULL if none matches. */
2807 static bfd_boolean
2808 xg_instruction_matches_rule (TInsn *insn, TransitionRule *rule)
2810 PreconditionList *condition_l;
2812 if (rule->opcode != insn->opcode)
2813 return FALSE;
2815 for (condition_l = rule->conditions;
2816 condition_l != NULL;
2817 condition_l = condition_l->next)
2819 expressionS *exp1;
2820 expressionS *exp2;
2821 Precondition *cond = condition_l->precond;
2823 switch (cond->typ)
2825 case OP_CONSTANT:
2826 /* The expression must be the constant. */
2827 assert (cond->op_num < insn->ntok);
2828 exp1 = &insn->tok[cond->op_num];
2829 if (expr_is_const (exp1))
2831 switch (cond->cmp)
2833 case OP_EQUAL:
2834 if (get_expr_const (exp1) != cond->op_data)
2835 return FALSE;
2836 break;
2837 case OP_NOTEQUAL:
2838 if (get_expr_const (exp1) == cond->op_data)
2839 return FALSE;
2840 break;
2841 default:
2842 return FALSE;
2845 else if (expr_is_register (exp1))
2847 switch (cond->cmp)
2849 case OP_EQUAL:
2850 if (get_expr_register (exp1) != cond->op_data)
2851 return FALSE;
2852 break;
2853 case OP_NOTEQUAL:
2854 if (get_expr_register (exp1) == cond->op_data)
2855 return FALSE;
2856 break;
2857 default:
2858 return FALSE;
2861 else
2862 return FALSE;
2863 break;
2865 case OP_OPERAND:
2866 assert (cond->op_num < insn->ntok);
2867 assert (cond->op_data < insn->ntok);
2868 exp1 = &insn->tok[cond->op_num];
2869 exp2 = &insn->tok[cond->op_data];
2871 switch (cond->cmp)
2873 case OP_EQUAL:
2874 if (!expr_is_equal (exp1, exp2))
2875 return FALSE;
2876 break;
2877 case OP_NOTEQUAL:
2878 if (expr_is_equal (exp1, exp2))
2879 return FALSE;
2880 break;
2882 break;
2884 case OP_LITERAL:
2885 case OP_LABEL:
2886 default:
2887 return FALSE;
2890 if (!xg_instruction_matches_options (insn, rule->options))
2891 return FALSE;
2893 return TRUE;
2897 static int
2898 transition_rule_cmp (const TransitionRule *a, const TransitionRule *b)
2900 bfd_boolean a_greater = FALSE;
2901 bfd_boolean b_greater = FALSE;
2903 ReqOptionList *l_a = a->options;
2904 ReqOptionList *l_b = b->options;
2906 /* We only care if they both are the same except for
2907 a const16 vs. an l32r. */
2909 while (l_a && l_b && ((l_a->next == NULL) == (l_b->next == NULL)))
2911 ReqOrOptionList *l_or_a = l_a->or_option_terms;
2912 ReqOrOptionList *l_or_b = l_b->or_option_terms;
2913 while (l_or_a && l_or_b && ((l_a->next == NULL) == (l_b->next == NULL)))
2915 if (l_or_a->is_true != l_or_b->is_true)
2916 return 0;
2917 if (strcmp (l_or_a->option_name, l_or_b->option_name) != 0)
2919 /* This is the case we care about. */
2920 if (strcmp (l_or_a->option_name, "IsaUseConst16") == 0
2921 && strcmp (l_or_b->option_name, "IsaUseL32R") == 0)
2923 if (prefer_const16)
2924 a_greater = TRUE;
2925 else
2926 b_greater = TRUE;
2928 else if (strcmp (l_or_a->option_name, "IsaUseL32R") == 0
2929 && strcmp (l_or_b->option_name, "IsaUseConst16") == 0)
2931 if (prefer_const16)
2932 b_greater = TRUE;
2933 else
2934 a_greater = TRUE;
2936 else
2937 return 0;
2939 l_or_a = l_or_a->next;
2940 l_or_b = l_or_b->next;
2942 if (l_or_a || l_or_b)
2943 return 0;
2945 l_a = l_a->next;
2946 l_b = l_b->next;
2948 if (l_a || l_b)
2949 return 0;
2951 /* Incomparable if the substitution was used differently in two cases. */
2952 if (a_greater && b_greater)
2953 return 0;
2955 if (b_greater)
2956 return 1;
2957 if (a_greater)
2958 return -1;
2960 return 0;
2964 static TransitionRule *
2965 xg_instruction_match (TInsn *insn)
2967 TransitionTable *table = xg_build_simplify_table (&transition_rule_cmp);
2968 TransitionList *l;
2969 assert (insn->opcode < table->num_opcodes);
2971 /* Walk through all of the possible transitions. */
2972 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
2974 TransitionRule *rule = l->rule;
2975 if (xg_instruction_matches_rule (insn, rule))
2976 return rule;
2978 return NULL;
2982 /* Various Other Internal Functions. */
2984 static bfd_boolean
2985 is_unique_insn_expansion (TransitionRule *r)
2987 if (!r->to_instr || r->to_instr->next != NULL)
2988 return FALSE;
2989 if (r->to_instr->typ != INSTR_INSTR)
2990 return FALSE;
2991 return TRUE;
2995 static int
2996 xg_get_build_instr_size (BuildInstr *insn)
2998 assert (insn->typ == INSTR_INSTR);
2999 return xg_get_single_size (insn->opcode);
3003 static bfd_boolean
3004 xg_is_narrow_insn (TInsn *insn)
3006 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3007 TransitionList *l;
3008 int num_match = 0;
3009 assert (insn->insn_type == ITYPE_INSN);
3010 assert (insn->opcode < table->num_opcodes);
3012 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3014 TransitionRule *rule = l->rule;
3016 if (xg_instruction_matches_rule (insn, rule)
3017 && is_unique_insn_expansion (rule))
3019 /* It only generates one instruction... */
3020 assert (insn->insn_type == ITYPE_INSN);
3021 /* ...and it is a larger instruction. */
3022 if (xg_get_single_size (insn->opcode)
3023 < xg_get_build_instr_size (rule->to_instr))
3025 num_match++;
3026 if (num_match > 1)
3027 return FALSE;
3031 return (num_match == 1);
3035 static bfd_boolean
3036 xg_is_single_relaxable_insn (TInsn *insn)
3038 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3039 TransitionList *l;
3040 int num_match = 0;
3041 assert (insn->insn_type == ITYPE_INSN);
3042 assert (insn->opcode < table->num_opcodes);
3044 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3046 TransitionRule *rule = l->rule;
3048 if (xg_instruction_matches_rule (insn, rule)
3049 && is_unique_insn_expansion (rule))
3051 /* It only generates one instruction... */
3052 assert (insn->insn_type == ITYPE_INSN);
3053 /* ... and it is a larger instruction. */
3054 if (xg_get_single_size (insn->opcode)
3055 <= xg_get_build_instr_size (rule->to_instr))
3057 num_match++;
3058 if (num_match > 1)
3059 return FALSE;
3063 return (num_match == 1);
3067 /* Return the maximum number of bytes this opcode can expand to. */
3069 static int
3070 xg_get_max_insn_widen_size (xtensa_opcode opcode)
3072 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3073 TransitionList *l;
3074 int max_size = xg_get_single_size (opcode);
3076 assert (opcode < table->num_opcodes);
3078 for (l = table->table[opcode]; l != NULL; l = l->next)
3080 TransitionRule *rule = l->rule;
3081 BuildInstr *build_list;
3082 int this_size = 0;
3084 if (!rule)
3085 continue;
3086 build_list = rule->to_instr;
3087 if (is_unique_insn_expansion (rule))
3089 assert (build_list->typ == INSTR_INSTR);
3090 this_size = xg_get_max_insn_widen_size (build_list->opcode);
3092 else
3093 for (; build_list != NULL; build_list = build_list->next)
3095 switch (build_list->typ)
3097 case INSTR_INSTR:
3098 this_size += xg_get_single_size (build_list->opcode);
3099 break;
3100 case INSTR_LITERAL_DEF:
3101 case INSTR_LABEL_DEF:
3102 default:
3103 break;
3106 if (this_size > max_size)
3107 max_size = this_size;
3109 return max_size;
3113 /* Return the maximum number of literal bytes this opcode can generate. */
3115 static int
3116 xg_get_max_insn_widen_literal_size (xtensa_opcode opcode)
3118 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3119 TransitionList *l;
3120 int max_size = 0;
3122 assert (opcode < table->num_opcodes);
3124 for (l = table->table[opcode]; l != NULL; l = l->next)
3126 TransitionRule *rule = l->rule;
3127 BuildInstr *build_list;
3128 int this_size = 0;
3130 if (!rule)
3131 continue;
3132 build_list = rule->to_instr;
3133 if (is_unique_insn_expansion (rule))
3135 assert (build_list->typ == INSTR_INSTR);
3136 this_size = xg_get_max_insn_widen_literal_size (build_list->opcode);
3138 else
3139 for (; build_list != NULL; build_list = build_list->next)
3141 switch (build_list->typ)
3143 case INSTR_LITERAL_DEF:
3144 /* Hard-coded 4-byte literal. */
3145 this_size += 4;
3146 break;
3147 case INSTR_INSTR:
3148 case INSTR_LABEL_DEF:
3149 default:
3150 break;
3153 if (this_size > max_size)
3154 max_size = this_size;
3156 return max_size;
3160 static bfd_boolean
3161 xg_is_relaxable_insn (TInsn *insn, int lateral_steps)
3163 int steps_taken = 0;
3164 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3165 TransitionList *l;
3167 assert (insn->insn_type == ITYPE_INSN);
3168 assert (insn->opcode < table->num_opcodes);
3170 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3172 TransitionRule *rule = l->rule;
3174 if (xg_instruction_matches_rule (insn, rule))
3176 if (steps_taken == lateral_steps)
3177 return TRUE;
3178 steps_taken++;
3181 return FALSE;
3185 static symbolS *
3186 get_special_literal_symbol (void)
3188 static symbolS *sym = NULL;
3190 if (sym == NULL)
3191 sym = symbol_find_or_make ("SPECIAL_LITERAL0\001");
3192 return sym;
3196 static symbolS *
3197 get_special_label_symbol (void)
3199 static symbolS *sym = NULL;
3201 if (sym == NULL)
3202 sym = symbol_find_or_make ("SPECIAL_LABEL0\001");
3203 return sym;
3207 static bfd_boolean
3208 xg_valid_literal_expression (const expressionS *exp)
3210 switch (exp->X_op)
3212 case O_constant:
3213 case O_symbol:
3214 case O_big:
3215 case O_uminus:
3216 case O_subtract:
3217 case O_pltrel:
3218 return TRUE;
3219 default:
3220 return FALSE;
3225 /* This will check to see if the value can be converted into the
3226 operand type. It will return TRUE if it does not fit. */
3228 static bfd_boolean
3229 xg_check_operand (int32 value, xtensa_opcode opcode, int operand)
3231 uint32 valbuf = value;
3232 if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf))
3233 return TRUE;
3234 return FALSE;
3238 /* Assumes: All immeds are constants. Check that all constants fit
3239 into their immeds; return FALSE if not. */
3241 static bfd_boolean
3242 xg_immeds_fit (const TInsn *insn)
3244 xtensa_isa isa = xtensa_default_isa;
3245 int i;
3247 int n = insn->ntok;
3248 assert (insn->insn_type == ITYPE_INSN);
3249 for (i = 0; i < n; ++i)
3251 const expressionS *expr = &insn->tok[i];
3252 if (xtensa_operand_is_register (isa, insn->opcode, i) == 1)
3253 continue;
3255 switch (expr->X_op)
3257 case O_register:
3258 case O_constant:
3259 if (xg_check_operand (expr->X_add_number, insn->opcode, i))
3260 return FALSE;
3261 break;
3263 default:
3264 /* The symbol should have a fixup associated with it. */
3265 assert (FALSE);
3266 break;
3269 return TRUE;
3273 /* This should only be called after we have an initial
3274 estimate of the addresses. */
3276 static bfd_boolean
3277 xg_symbolic_immeds_fit (const TInsn *insn,
3278 segT pc_seg,
3279 fragS *pc_frag,
3280 offsetT pc_offset,
3281 long stretch)
3283 xtensa_isa isa = xtensa_default_isa;
3284 symbolS *symbolP;
3285 fragS *sym_frag;
3286 offsetT target, pc;
3287 uint32 new_offset;
3288 int i;
3289 int n = insn->ntok;
3291 assert (insn->insn_type == ITYPE_INSN);
3293 for (i = 0; i < n; ++i)
3295 const expressionS *expr = &insn->tok[i];
3296 if (xtensa_operand_is_register (isa, insn->opcode, i) == 1)
3297 continue;
3299 switch (expr->X_op)
3301 case O_register:
3302 case O_constant:
3303 if (xg_check_operand (expr->X_add_number, insn->opcode, i))
3304 return FALSE;
3305 break;
3307 case O_lo16:
3308 case O_hi16:
3309 /* Check for the worst case. */
3310 if (xg_check_operand (0xffff, insn->opcode, i))
3311 return FALSE;
3312 break;
3314 case O_symbol:
3315 /* We only allow symbols for pc-relative stuff.
3316 If pc_frag == 0, then we don't have frag locations yet. */
3317 if (pc_frag == 0)
3318 return FALSE;
3320 /* If it is PC-relative and the symbol is not in the same
3321 segment as the PC.... */
3322 if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 0
3323 || S_GET_SEGMENT (expr->X_add_symbol) != pc_seg)
3324 return FALSE;
3326 /* If it is a weak symbol, then assume it won't reach. This will
3327 only affect calls when longcalls are enabled, because if
3328 longcalls are disabled, then the call is marked as a specific
3329 opcode. */
3330 if (S_IS_WEAK (expr->X_add_symbol))
3331 return FALSE;
3333 symbolP = expr->X_add_symbol;
3334 sym_frag = symbol_get_frag (symbolP);
3335 target = S_GET_VALUE (symbolP) + expr->X_add_number;
3336 pc = pc_frag->fr_address + pc_offset;
3338 /* If frag has yet to be reached on this pass, assume it
3339 will move by STRETCH just as we did. If this is not so,
3340 it will be because some frag between grows, and that will
3341 force another pass. Beware zero-length frags. There
3342 should be a faster way to do this. */
3344 if (stretch != 0
3345 && sym_frag->relax_marker != pc_frag->relax_marker
3346 && S_GET_SEGMENT (symbolP) == pc_seg)
3348 target += stretch;
3351 new_offset = target;
3352 xtensa_operand_do_reloc (isa, insn->opcode, i, &new_offset, pc);
3353 if (xg_check_operand (new_offset, insn->opcode, i))
3354 return FALSE;
3355 break;
3357 default:
3358 /* The symbol should have a fixup associated with it. */
3359 return FALSE;
3363 return TRUE;
3367 /* Return TRUE on success. */
3369 static bfd_boolean
3370 xg_build_to_insn (TInsn *targ, TInsn *insn, BuildInstr *bi)
3372 BuildOp *op;
3373 symbolS *sym;
3375 memset (targ, 0, sizeof (TInsn));
3376 targ->loc = insn->loc;
3377 switch (bi->typ)
3379 case INSTR_INSTR:
3380 op = bi->ops;
3381 targ->opcode = bi->opcode;
3382 targ->insn_type = ITYPE_INSN;
3383 targ->is_specific_opcode = FALSE;
3385 for (; op != NULL; op = op->next)
3387 int op_num = op->op_num;
3388 int op_data = op->op_data;
3390 assert (op->op_num < MAX_INSN_ARGS);
3392 if (targ->ntok <= op_num)
3393 targ->ntok = op_num + 1;
3395 switch (op->typ)
3397 case OP_CONSTANT:
3398 set_expr_const (&targ->tok[op_num], op_data);
3399 break;
3400 case OP_OPERAND:
3401 assert (op_data < insn->ntok);
3402 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3403 break;
3404 case OP_LITERAL:
3405 sym = get_special_literal_symbol ();
3406 set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
3407 break;
3408 case OP_LABEL:
3409 sym = get_special_label_symbol ();
3410 set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
3411 break;
3412 case OP_OPERAND_HI16U:
3413 case OP_OPERAND_LOW16U:
3414 assert (op_data < insn->ntok);
3415 if (expr_is_const (&insn->tok[op_data]))
3417 long val;
3418 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3419 val = xg_apply_userdef_op_fn (op->typ,
3420 targ->tok[op_num].
3421 X_add_number);
3422 targ->tok[op_num].X_add_number = val;
3424 else
3426 /* For const16 we can create relocations for these. */
3427 if (targ->opcode == XTENSA_UNDEFINED
3428 || (targ->opcode != xtensa_const16_opcode))
3429 return FALSE;
3430 assert (op_data < insn->ntok);
3431 /* Need to build a O_lo16 or O_hi16. */
3432 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3433 if (targ->tok[op_num].X_op == O_symbol)
3435 if (op->typ == OP_OPERAND_HI16U)
3436 targ->tok[op_num].X_op = O_hi16;
3437 else if (op->typ == OP_OPERAND_LOW16U)
3438 targ->tok[op_num].X_op = O_lo16;
3439 else
3440 return FALSE;
3443 break;
3444 default:
3445 /* currently handles:
3446 OP_OPERAND_LOW8
3447 OP_OPERAND_HI24S
3448 OP_OPERAND_F32MINUS */
3449 if (xg_has_userdef_op_fn (op->typ))
3451 assert (op_data < insn->ntok);
3452 if (expr_is_const (&insn->tok[op_data]))
3454 long val;
3455 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3456 val = xg_apply_userdef_op_fn (op->typ,
3457 targ->tok[op_num].
3458 X_add_number);
3459 targ->tok[op_num].X_add_number = val;
3461 else
3462 return FALSE; /* We cannot use a relocation for this. */
3463 break;
3465 assert (0);
3466 break;
3469 break;
3471 case INSTR_LITERAL_DEF:
3472 op = bi->ops;
3473 targ->opcode = XTENSA_UNDEFINED;
3474 targ->insn_type = ITYPE_LITERAL;
3475 targ->is_specific_opcode = FALSE;
3476 for (; op != NULL; op = op->next)
3478 int op_num = op->op_num;
3479 int op_data = op->op_data;
3480 assert (op->op_num < MAX_INSN_ARGS);
3482 if (targ->ntok <= op_num)
3483 targ->ntok = op_num + 1;
3485 switch (op->typ)
3487 case OP_OPERAND:
3488 assert (op_data < insn->ntok);
3489 /* We can only pass resolvable literals through. */
3490 if (!xg_valid_literal_expression (&insn->tok[op_data]))
3491 return FALSE;
3492 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3493 break;
3494 case OP_LITERAL:
3495 case OP_CONSTANT:
3496 case OP_LABEL:
3497 default:
3498 assert (0);
3499 break;
3502 break;
3504 case INSTR_LABEL_DEF:
3505 op = bi->ops;
3506 targ->opcode = XTENSA_UNDEFINED;
3507 targ->insn_type = ITYPE_LABEL;
3508 targ->is_specific_opcode = FALSE;
3509 /* Literal with no ops is a label? */
3510 assert (op == NULL);
3511 break;
3513 default:
3514 assert (0);
3517 return TRUE;
3521 /* Return TRUE on success. */
3523 static bfd_boolean
3524 xg_build_to_stack (IStack *istack, TInsn *insn, BuildInstr *bi)
3526 for (; bi != NULL; bi = bi->next)
3528 TInsn *next_insn = istack_push_space (istack);
3530 if (!xg_build_to_insn (next_insn, insn, bi))
3531 return FALSE;
3533 return TRUE;
3537 /* Return TRUE on valid expansion. */
3539 static bfd_boolean
3540 xg_expand_to_stack (IStack *istack, TInsn *insn, int lateral_steps)
3542 int stack_size = istack->ninsn;
3543 int steps_taken = 0;
3544 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3545 TransitionList *l;
3547 assert (insn->insn_type == ITYPE_INSN);
3548 assert (insn->opcode < table->num_opcodes);
3550 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3552 TransitionRule *rule = l->rule;
3554 if (xg_instruction_matches_rule (insn, rule))
3556 if (lateral_steps == steps_taken)
3558 int i;
3560 /* This is it. Expand the rule to the stack. */
3561 if (!xg_build_to_stack (istack, insn, rule->to_instr))
3562 return FALSE;
3564 /* Check to see if it fits. */
3565 for (i = stack_size; i < istack->ninsn; i++)
3567 TInsn *insn = &istack->insn[i];
3569 if (insn->insn_type == ITYPE_INSN
3570 && !tinsn_has_symbolic_operands (insn)
3571 && !xg_immeds_fit (insn))
3573 istack->ninsn = stack_size;
3574 return FALSE;
3577 return TRUE;
3579 steps_taken++;
3582 return FALSE;
3586 static bfd_boolean
3587 xg_expand_narrow (TInsn *targ, TInsn *insn)
3589 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3590 TransitionList *l;
3592 assert (insn->insn_type == ITYPE_INSN);
3593 assert (insn->opcode < table->num_opcodes);
3595 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3597 TransitionRule *rule = l->rule;
3598 if (xg_instruction_matches_rule (insn, rule)
3599 && is_unique_insn_expansion (rule))
3601 /* Is it a larger instruction? */
3602 if (xg_get_single_size (insn->opcode)
3603 <= xg_get_build_instr_size (rule->to_instr))
3605 xg_build_to_insn (targ, insn, rule->to_instr);
3606 return FALSE;
3610 return TRUE;
3614 /* Relax the assembly instruction at least "min_steps".
3615 Return the number of steps taken. */
3617 static int
3618 xg_assembly_relax (IStack *istack,
3619 TInsn *insn,
3620 segT pc_seg,
3621 fragS *pc_frag, /* if pc_frag == 0, not pc-relative */
3622 offsetT pc_offset, /* offset in fragment */
3623 int min_steps, /* minimum conversion steps */
3624 long stretch) /* number of bytes stretched so far */
3626 int steps_taken = 0;
3628 /* assert (has no symbolic operands)
3629 Some of its immeds don't fit.
3630 Try to build a relaxed version.
3631 This may go through a couple of stages
3632 of single instruction transformations before
3633 we get there. */
3635 TInsn single_target;
3636 TInsn current_insn;
3637 int lateral_steps = 0;
3638 int istack_size = istack->ninsn;
3640 if (xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch)
3641 && steps_taken >= min_steps)
3643 istack_push (istack, insn);
3644 return steps_taken;
3646 current_insn = *insn;
3648 /* Walk through all of the single instruction expansions. */
3649 while (xg_is_single_relaxable_insn (&current_insn))
3651 int error_val = xg_expand_narrow (&single_target, &current_insn);
3653 assert (!error_val);
3655 if (xg_symbolic_immeds_fit (&single_target, pc_seg, pc_frag, pc_offset,
3656 stretch))
3658 steps_taken++;
3659 if (steps_taken >= min_steps)
3661 istack_push (istack, &single_target);
3662 return steps_taken;
3665 current_insn = single_target;
3668 /* Now check for a multi-instruction expansion. */
3669 while (xg_is_relaxable_insn (&current_insn, lateral_steps))
3671 if (xg_symbolic_immeds_fit (&current_insn, pc_seg, pc_frag, pc_offset,
3672 stretch))
3674 if (steps_taken >= min_steps)
3676 istack_push (istack, &current_insn);
3677 return steps_taken;
3680 steps_taken++;
3681 if (xg_expand_to_stack (istack, &current_insn, lateral_steps))
3683 if (steps_taken >= min_steps)
3684 return steps_taken;
3686 lateral_steps++;
3687 istack->ninsn = istack_size;
3690 /* It's not going to work -- use the original. */
3691 istack_push (istack, insn);
3692 return steps_taken;
3696 static void
3697 xg_force_frag_space (int size)
3699 /* This may have the side effect of creating a new fragment for the
3700 space to go into. I just do not like the name of the "frag"
3701 functions. */
3702 frag_grow (size);
3706 static void
3707 xg_finish_frag (char *last_insn,
3708 enum xtensa_relax_statesE frag_state,
3709 enum xtensa_relax_statesE slot0_state,
3710 int max_growth,
3711 bfd_boolean is_insn)
3713 /* Finish off this fragment so that it has at LEAST the desired
3714 max_growth. If it doesn't fit in this fragment, close this one
3715 and start a new one. In either case, return a pointer to the
3716 beginning of the growth area. */
3718 fragS *old_frag;
3720 xg_force_frag_space (max_growth);
3722 old_frag = frag_now;
3724 frag_now->fr_opcode = last_insn;
3725 if (is_insn)
3726 frag_now->tc_frag_data.is_insn = TRUE;
3728 frag_var (rs_machine_dependent, max_growth, max_growth,
3729 frag_state, frag_now->fr_symbol, frag_now->fr_offset, last_insn);
3731 old_frag->tc_frag_data.slot_subtypes[0] = slot0_state;
3732 xtensa_set_frag_assembly_state (frag_now);
3734 /* Just to make sure that we did not split it up. */
3735 assert (old_frag->fr_next == frag_now);
3739 /* Return TRUE if the target frag is one of the next non-empty frags. */
3741 static bfd_boolean
3742 is_next_frag_target (const fragS *fragP, const fragS *target)
3744 if (fragP == NULL)
3745 return FALSE;
3747 for (; fragP; fragP = fragP->fr_next)
3749 if (fragP == target)
3750 return TRUE;
3751 if (fragP->fr_fix != 0)
3752 return FALSE;
3753 if (fragP->fr_type == rs_fill && fragP->fr_offset != 0)
3754 return FALSE;
3755 if ((fragP->fr_type == rs_align || fragP->fr_type == rs_align_code)
3756 && ((fragP->fr_address % (1 << fragP->fr_offset)) != 0))
3757 return FALSE;
3758 if (fragP->fr_type == rs_space)
3759 return FALSE;
3761 return FALSE;
3765 static bfd_boolean
3766 is_branch_jmp_to_next (TInsn *insn, fragS *fragP)
3768 xtensa_isa isa = xtensa_default_isa;
3769 int i;
3770 int num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
3771 int target_op = -1;
3772 symbolS *sym;
3773 fragS *target_frag;
3775 if (xtensa_opcode_is_branch (isa, insn->opcode) == 0
3776 && xtensa_opcode_is_jump (isa, insn->opcode) == 0)
3777 return FALSE;
3779 for (i = 0; i < num_ops; i++)
3781 if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1)
3783 target_op = i;
3784 break;
3787 if (target_op == -1)
3788 return FALSE;
3790 if (insn->ntok <= target_op)
3791 return FALSE;
3793 if (insn->tok[target_op].X_op != O_symbol)
3794 return FALSE;
3796 sym = insn->tok[target_op].X_add_symbol;
3797 if (sym == NULL)
3798 return FALSE;
3800 if (insn->tok[target_op].X_add_number != 0)
3801 return FALSE;
3803 target_frag = symbol_get_frag (sym);
3804 if (target_frag == NULL)
3805 return FALSE;
3807 if (is_next_frag_target (fragP->fr_next, target_frag)
3808 && S_GET_VALUE (sym) == target_frag->fr_address)
3809 return TRUE;
3811 return FALSE;
3815 static void
3816 xg_add_branch_and_loop_targets (TInsn *insn)
3818 xtensa_isa isa = xtensa_default_isa;
3819 int num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
3821 if (xtensa_opcode_is_loop (isa, insn->opcode) == 1)
3823 int i = 1;
3824 if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1
3825 && insn->tok[i].X_op == O_symbol)
3826 symbol_get_tc (insn->tok[i].X_add_symbol)->is_loop_target = TRUE;
3827 return;
3830 if (xtensa_opcode_is_branch (isa, insn->opcode) == 1
3831 || xtensa_opcode_is_loop (isa, insn->opcode) == 1)
3833 int i;
3835 for (i = 0; i < insn->ntok && i < num_ops; i++)
3837 if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1
3838 && insn->tok[i].X_op == O_symbol)
3840 symbolS *sym = insn->tok[i].X_add_symbol;
3841 symbol_get_tc (sym)->is_branch_target = TRUE;
3842 if (S_IS_DEFINED (sym))
3843 symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
3850 /* Return FALSE if no error. */
3852 static bfd_boolean
3853 xg_build_token_insn (BuildInstr *instr_spec, TInsn *old_insn, TInsn *new_insn)
3855 int num_ops = 0;
3856 BuildOp *b_op;
3858 switch (instr_spec->typ)
3860 case INSTR_INSTR:
3861 new_insn->insn_type = ITYPE_INSN;
3862 new_insn->opcode = instr_spec->opcode;
3863 new_insn->is_specific_opcode = FALSE;
3864 new_insn->loc = old_insn->loc;
3865 break;
3866 case INSTR_LITERAL_DEF:
3867 new_insn->insn_type = ITYPE_LITERAL;
3868 new_insn->opcode = XTENSA_UNDEFINED;
3869 new_insn->is_specific_opcode = FALSE;
3870 new_insn->loc = old_insn->loc;
3871 break;
3872 case INSTR_LABEL_DEF:
3873 as_bad (_("INSTR_LABEL_DEF not supported yet"));
3874 break;
3877 for (b_op = instr_spec->ops; b_op != NULL; b_op = b_op->next)
3879 expressionS *exp;
3880 const expressionS *src_exp;
3882 num_ops++;
3883 switch (b_op->typ)
3885 case OP_CONSTANT:
3886 /* The expression must be the constant. */
3887 assert (b_op->op_num < MAX_INSN_ARGS);
3888 exp = &new_insn->tok[b_op->op_num];
3889 set_expr_const (exp, b_op->op_data);
3890 break;
3892 case OP_OPERAND:
3893 assert (b_op->op_num < MAX_INSN_ARGS);
3894 assert (b_op->op_data < (unsigned) old_insn->ntok);
3895 src_exp = &old_insn->tok[b_op->op_data];
3896 exp = &new_insn->tok[b_op->op_num];
3897 copy_expr (exp, src_exp);
3898 break;
3900 case OP_LITERAL:
3901 case OP_LABEL:
3902 as_bad (_("can't handle generation of literal/labels yet"));
3903 assert (0);
3905 default:
3906 as_bad (_("can't handle undefined OP TYPE"));
3907 assert (0);
3911 new_insn->ntok = num_ops;
3912 return FALSE;
3916 /* Return TRUE if it was simplified. */
3918 static bfd_boolean
3919 xg_simplify_insn (TInsn *old_insn, TInsn *new_insn)
3921 TransitionRule *rule;
3922 BuildInstr *insn_spec;
3924 if (old_insn->is_specific_opcode || !density_supported)
3925 return FALSE;
3927 rule = xg_instruction_match (old_insn);
3928 if (rule == NULL)
3929 return FALSE;
3931 insn_spec = rule->to_instr;
3932 /* There should only be one. */
3933 assert (insn_spec != NULL);
3934 assert (insn_spec->next == NULL);
3935 if (insn_spec->next != NULL)
3936 return FALSE;
3938 xg_build_token_insn (insn_spec, old_insn, new_insn);
3940 return TRUE;
3944 /* xg_expand_assembly_insn: (1) Simplify the instruction, i.e., l32i ->
3945 l32i.n. (2) Check the number of operands. (3) Place the instruction
3946 tokens into the stack or if we can relax it at assembly time, place
3947 multiple instructions/literals onto the stack. Return FALSE if no
3948 error. */
3950 static bfd_boolean
3951 xg_expand_assembly_insn (IStack *istack, TInsn *orig_insn)
3953 int noperands;
3954 TInsn new_insn;
3955 memset (&new_insn, 0, sizeof (TInsn));
3957 /* Narrow it if we can. xg_simplify_insn now does all the
3958 appropriate checking (e.g., for the density option). */
3959 if (xg_simplify_insn (orig_insn, &new_insn))
3960 orig_insn = &new_insn;
3962 noperands = xtensa_opcode_num_operands (xtensa_default_isa,
3963 orig_insn->opcode);
3964 if (orig_insn->ntok < noperands)
3966 as_bad (_("found %d operands for '%s': Expected %d"),
3967 orig_insn->ntok,
3968 xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
3969 noperands);
3970 return TRUE;
3972 if (orig_insn->ntok > noperands)
3973 as_warn (_("found too many (%d) operands for '%s': Expected %d"),
3974 orig_insn->ntok,
3975 xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
3976 noperands);
3978 /* If there are not enough operands, we will assert above. If there
3979 are too many, just cut out the extras here. */
3981 orig_insn->ntok = noperands;
3983 /* Cases:
3985 Instructions with all constant immeds:
3986 Assemble them and relax the instruction if possible.
3987 Give error if not possible; no fixup needed.
3989 Instructions with symbolic immeds:
3990 Assemble them with a Fix up (that may cause instruction expansion).
3991 Also close out the fragment if the fixup may cause instruction expansion.
3993 There are some other special cases where we need alignment.
3994 1) before certain instructions with required alignment (OPCODE_ALIGN)
3995 2) before labels that have jumps (LABEL_ALIGN)
3996 3) after call instructions (RETURN_ALIGN)
3997 Multiple of these may be possible on the same fragment.
3998 If so, make sure to satisfy the required alignment.
3999 Then try to get the desired alignment. */
4001 if (tinsn_has_invalid_symbolic_operands (orig_insn))
4002 return TRUE;
4004 if (orig_insn->is_specific_opcode || !use_transform ())
4006 istack_push (istack, orig_insn);
4007 return FALSE;
4010 if (tinsn_has_symbolic_operands (orig_insn))
4012 if (tinsn_has_complex_operands (orig_insn))
4013 xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0);
4014 else
4015 istack_push (istack, orig_insn);
4017 else
4019 if (xg_immeds_fit (orig_insn))
4020 istack_push (istack, orig_insn);
4021 else
4022 xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0);
4025 return FALSE;
4029 /* Return TRUE if the section flags are marked linkonce
4030 or the name is .gnu.linkonce*. */
4032 static bfd_boolean
4033 get_is_linkonce_section (bfd *abfd ATTRIBUTE_UNUSED, segT sec)
4035 flagword flags, link_once_flags;
4037 flags = bfd_get_section_flags (abfd, sec);
4038 link_once_flags = (flags & SEC_LINK_ONCE);
4040 /* Flags might not be set yet. */
4041 if (!link_once_flags)
4043 static size_t len = sizeof ".gnu.linkonce.t.";
4045 if (strncmp (segment_name (sec), ".gnu.linkonce.t.", len - 1) == 0)
4046 link_once_flags = SEC_LINK_ONCE;
4048 return (link_once_flags != 0);
4052 static void
4053 xtensa_add_literal_sym (symbolS *sym)
4055 sym_list *l;
4057 l = (sym_list *) xmalloc (sizeof (sym_list));
4058 l->sym = sym;
4059 l->next = literal_syms;
4060 literal_syms = l;
4064 static symbolS *
4065 xtensa_create_literal_symbol (segT sec, fragS *frag)
4067 static int lit_num = 0;
4068 static char name[256];
4069 symbolS *symbolP;
4071 sprintf (name, ".L_lit_sym%d", lit_num);
4073 /* Create a local symbol. If it is in a linkonce section, we have to
4074 be careful to make sure that if it is used in a relocation that the
4075 symbol will be in the output file. */
4076 if (get_is_linkonce_section (stdoutput, sec))
4078 symbolP = symbol_new (name, sec, 0, frag);
4079 S_CLEAR_EXTERNAL (symbolP);
4080 /* symbolP->local = 1; */
4082 else
4083 symbolP = symbol_new (name, sec, 0, frag);
4085 xtensa_add_literal_sym (symbolP);
4087 frag->tc_frag_data.is_literal = TRUE;
4088 lit_num++;
4089 return symbolP;
4093 /* Currently all literals that are generated here are 32-bit L32R targets. */
4095 static symbolS *
4096 xg_assemble_literal (/* const */ TInsn *insn)
4098 emit_state state;
4099 symbolS *lit_sym = NULL;
4101 /* size = 4 for L32R. It could easily be larger when we move to
4102 larger constants. Add a parameter later. */
4103 offsetT litsize = 4;
4104 offsetT litalign = 2; /* 2^2 = 4 */
4105 expressionS saved_loc;
4106 expressionS * emit_val;
4108 set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ());
4110 assert (insn->insn_type == ITYPE_LITERAL);
4111 assert (insn->ntok == 1); /* must be only one token here */
4113 xtensa_switch_to_literal_fragment (&state);
4115 emit_val = &insn->tok[0];
4116 if (emit_val->X_op == O_big)
4118 int size = emit_val->X_add_number * CHARS_PER_LITTLENUM;
4119 if (size > litsize)
4121 /* This happens when someone writes a "movi a2, big_number". */
4122 as_bad_where (frag_now->fr_file, frag_now->fr_line,
4123 _("invalid immediate"));
4124 xtensa_restore_emit_state (&state);
4125 return NULL;
4129 /* Force a 4-byte align here. Note that this opens a new frag, so all
4130 literals done with this function have a frag to themselves. That's
4131 important for the way text section literals work. */
4132 frag_align (litalign, 0, 0);
4133 record_alignment (now_seg, litalign);
4135 if (emit_val->X_op == O_pltrel)
4137 char *p = frag_more (litsize);
4138 xtensa_set_frag_assembly_state (frag_now);
4139 if (emit_val->X_add_symbol)
4140 emit_val->X_op = O_symbol;
4141 else
4142 emit_val->X_op = O_constant;
4143 fix_new_exp (frag_now, p - frag_now->fr_literal,
4144 litsize, emit_val, 0, BFD_RELOC_XTENSA_PLT);
4146 else
4147 emit_expr (emit_val, litsize);
4149 assert (frag_now->tc_frag_data.literal_frag == NULL);
4150 frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
4151 frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
4152 lit_sym = frag_now->fr_symbol;
4153 frag_now->tc_frag_data.is_literal = TRUE;
4155 /* Go back. */
4156 xtensa_restore_emit_state (&state);
4157 return lit_sym;
4161 static void
4162 xg_assemble_literal_space (/* const */ int size, int slot)
4164 emit_state state;
4165 /* We might have to do something about this alignment. It only
4166 takes effect if something is placed here. */
4167 offsetT litalign = 2; /* 2^2 = 4 */
4168 fragS *lit_saved_frag;
4170 assert (size % 4 == 0);
4172 xtensa_switch_to_literal_fragment (&state);
4174 /* Force a 4-byte align here. */
4175 frag_align (litalign, 0, 0);
4176 record_alignment (now_seg, litalign);
4178 xg_force_frag_space (size);
4180 lit_saved_frag = frag_now;
4181 frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
4182 frag_now->tc_frag_data.is_literal = TRUE;
4183 frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
4184 xg_finish_frag (0, RELAX_LITERAL, 0, size, FALSE);
4186 /* Go back. */
4187 xtensa_restore_emit_state (&state);
4188 frag_now->tc_frag_data.literal_frags[slot] = lit_saved_frag;
4192 /* Put in a fixup record based on the opcode.
4193 Return TRUE on success. */
4195 static bfd_boolean
4196 xg_add_opcode_fix (TInsn *tinsn,
4197 int opnum,
4198 xtensa_format fmt,
4199 int slot,
4200 expressionS *expr,
4201 fragS *fragP,
4202 offsetT offset)
4204 xtensa_opcode opcode = tinsn->opcode;
4205 bfd_reloc_code_real_type reloc;
4206 reloc_howto_type *howto;
4207 int fmt_length;
4208 fixS *the_fix;
4210 reloc = BFD_RELOC_NONE;
4212 /* First try the special cases for "alternate" relocs. */
4213 if (opcode == xtensa_l32r_opcode)
4215 if (fragP->tc_frag_data.use_absolute_literals)
4216 reloc = encode_alt_reloc (slot);
4218 else if (opcode == xtensa_const16_opcode)
4220 if (expr->X_op == O_lo16)
4222 reloc = encode_reloc (slot);
4223 expr->X_op = O_symbol;
4225 else if (expr->X_op == O_hi16)
4227 reloc = encode_alt_reloc (slot);
4228 expr->X_op = O_symbol;
4232 if (opnum != get_relaxable_immed (opcode))
4234 as_bad (_("invalid relocation for operand %i of '%s'"),
4235 opnum, xtensa_opcode_name (xtensa_default_isa, opcode));
4236 return FALSE;
4239 /* Handle erroneous "@h" and "@l" expressions here before they propagate
4240 into the symbol table where the generic portions of the assembler
4241 won't know what to do with them. */
4242 if (expr->X_op == O_lo16 || expr->X_op == O_hi16)
4244 as_bad (_("invalid expression for operand %i of '%s'"),
4245 opnum, xtensa_opcode_name (xtensa_default_isa, opcode));
4246 return FALSE;
4249 /* Next try the generic relocs. */
4250 if (reloc == BFD_RELOC_NONE)
4251 reloc = encode_reloc (slot);
4252 if (reloc == BFD_RELOC_NONE)
4254 as_bad (_("invalid relocation in instruction slot %i"), slot);
4255 return FALSE;
4258 howto = bfd_reloc_type_lookup (stdoutput, reloc);
4259 if (!howto)
4261 as_bad (_("undefined symbol for opcode \"%s\""),
4262 xtensa_opcode_name (xtensa_default_isa, opcode));
4263 return FALSE;
4266 fmt_length = xtensa_format_length (xtensa_default_isa, fmt);
4267 the_fix = fix_new_exp (fragP, offset, fmt_length, expr,
4268 howto->pc_relative, reloc);
4269 the_fix->fx_no_overflow = 1;
4271 if (expr->X_add_symbol
4272 && (S_IS_EXTERNAL (expr->X_add_symbol)
4273 || S_IS_WEAK (expr->X_add_symbol)))
4274 the_fix->fx_plt = TRUE;
4276 the_fix->tc_fix_data.X_add_symbol = expr->X_add_symbol;
4277 the_fix->tc_fix_data.X_add_number = expr->X_add_number;
4278 the_fix->tc_fix_data.slot = slot;
4280 return TRUE;
4284 static bfd_boolean
4285 xg_emit_insn_to_buf (TInsn *tinsn,
4286 xtensa_format fmt,
4287 char *buf,
4288 fragS *fragP,
4289 offsetT offset,
4290 bfd_boolean build_fix)
4292 static xtensa_insnbuf insnbuf = NULL;
4293 bfd_boolean has_symbolic_immed = FALSE;
4294 bfd_boolean ok = TRUE;
4295 if (!insnbuf)
4296 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4298 has_symbolic_immed = tinsn_to_insnbuf (tinsn, insnbuf);
4299 if (has_symbolic_immed && build_fix)
4301 /* Add a fixup. */
4302 int opnum = get_relaxable_immed (tinsn->opcode);
4303 expressionS *exp = &tinsn->tok[opnum];
4305 if (!xg_add_opcode_fix (tinsn, opnum, fmt, 0, exp, fragP, offset))
4306 ok = FALSE;
4308 fragP->tc_frag_data.is_insn = TRUE;
4309 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf, 0);
4310 return ok;
4314 static void
4315 xg_resolve_literals (TInsn *insn, symbolS *lit_sym)
4317 symbolS *sym = get_special_literal_symbol ();
4318 int i;
4319 if (lit_sym == 0)
4320 return;
4321 assert (insn->insn_type == ITYPE_INSN);
4322 for (i = 0; i < insn->ntok; i++)
4323 if (insn->tok[i].X_add_symbol == sym)
4324 insn->tok[i].X_add_symbol = lit_sym;
4329 static void
4330 xg_resolve_labels (TInsn *insn, symbolS *label_sym)
4332 symbolS *sym = get_special_label_symbol ();
4333 int i;
4334 /* assert (!insn->is_literal); */
4335 for (i = 0; i < insn->ntok; i++)
4336 if (insn->tok[i].X_add_symbol == sym)
4337 insn->tok[i].X_add_symbol = label_sym;
4342 /* Return TRUE if the instruction can write to the specified
4343 integer register. */
4345 static bfd_boolean
4346 is_register_writer (const TInsn *insn, const char *regset, int regnum)
4348 int i;
4349 int num_ops;
4350 xtensa_isa isa = xtensa_default_isa;
4352 num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
4354 for (i = 0; i < num_ops; i++)
4356 char inout;
4357 inout = xtensa_operand_inout (isa, insn->opcode, i);
4358 if ((inout == 'o' || inout == 'm')
4359 && xtensa_operand_is_register (isa, insn->opcode, i) == 1)
4361 xtensa_regfile opnd_rf =
4362 xtensa_operand_regfile (isa, insn->opcode, i);
4363 if (!strcmp (xtensa_regfile_shortname (isa, opnd_rf), regset))
4365 if ((insn->tok[i].X_op == O_register)
4366 && (insn->tok[i].X_add_number == regnum))
4367 return TRUE;
4371 return FALSE;
4375 static bfd_boolean
4376 is_bad_loopend_opcode (const TInsn *tinsn)
4378 xtensa_opcode opcode = tinsn->opcode;
4380 if (opcode == XTENSA_UNDEFINED)
4381 return FALSE;
4383 if (opcode == xtensa_call0_opcode
4384 || opcode == xtensa_callx0_opcode
4385 || opcode == xtensa_call4_opcode
4386 || opcode == xtensa_callx4_opcode
4387 || opcode == xtensa_call8_opcode
4388 || opcode == xtensa_callx8_opcode
4389 || opcode == xtensa_call12_opcode
4390 || opcode == xtensa_callx12_opcode
4391 || opcode == xtensa_isync_opcode
4392 || opcode == xtensa_ret_opcode
4393 || opcode == xtensa_ret_n_opcode
4394 || opcode == xtensa_retw_opcode
4395 || opcode == xtensa_retw_n_opcode
4396 || opcode == xtensa_waiti_opcode
4397 || opcode == xtensa_rsr_lcount_opcode)
4398 return TRUE;
4400 return FALSE;
4404 /* Labels that begin with ".Ln" or ".LM" are unaligned.
4405 This allows the debugger to add unaligned labels.
4406 Also, the assembler generates stabs labels that need
4407 not be aligned: FAKE_LABEL_NAME . {"F", "L", "endfunc"}. */
4409 static bfd_boolean
4410 is_unaligned_label (symbolS *sym)
4412 const char *name = S_GET_NAME (sym);
4413 static size_t fake_size = 0;
4415 if (name
4416 && name[0] == '.'
4417 && name[1] == 'L' && (name[2] == 'n' || name[2] == 'M'))
4418 return TRUE;
4420 /* FAKE_LABEL_NAME followed by "F", "L" or "endfunc" */
4421 if (fake_size == 0)
4422 fake_size = strlen (FAKE_LABEL_NAME);
4424 if (name
4425 && strncmp (FAKE_LABEL_NAME, name, fake_size) == 0
4426 && (name[fake_size] == 'F'
4427 || name[fake_size] == 'L'
4428 || (name[fake_size] == 'e'
4429 && strncmp ("endfunc", name+fake_size, 7) == 0)))
4430 return TRUE;
4432 return FALSE;
4436 static fragS *
4437 next_non_empty_frag (const fragS *fragP)
4439 fragS *next_fragP = fragP->fr_next;
4441 /* Sometimes an empty will end up here due storage allocation issues.
4442 So we have to skip until we find something legit. */
4443 while (next_fragP && next_fragP->fr_fix == 0)
4444 next_fragP = next_fragP->fr_next;
4446 if (next_fragP == NULL || next_fragP->fr_fix == 0)
4447 return NULL;
4449 return next_fragP;
4453 static bfd_boolean
4454 next_frag_opcode_is_loop (const fragS *fragP, xtensa_opcode *opcode)
4456 xtensa_opcode out_opcode;
4457 const fragS *next_fragP = next_non_empty_frag (fragP);
4459 if (next_fragP == NULL)
4460 return FALSE;
4462 out_opcode = get_opcode_from_buf (next_fragP->fr_literal, 0);
4463 if (xtensa_opcode_is_loop (xtensa_default_isa, out_opcode) == 1)
4465 *opcode = out_opcode;
4466 return TRUE;
4468 return FALSE;
4472 static int
4473 frag_format_size (const fragS *fragP)
4475 static xtensa_insnbuf insnbuf = NULL;
4476 xtensa_isa isa = xtensa_default_isa;
4477 xtensa_format fmt;
4478 int fmt_size;
4480 if (!insnbuf)
4481 insnbuf = xtensa_insnbuf_alloc (isa);
4483 if (fragP == NULL)
4484 return XTENSA_UNDEFINED;
4486 xtensa_insnbuf_from_chars (isa, insnbuf, fragP->fr_literal, 0);
4488 fmt = xtensa_format_decode (isa, insnbuf);
4489 if (fmt == XTENSA_UNDEFINED)
4490 return XTENSA_UNDEFINED;
4491 fmt_size = xtensa_format_length (isa, fmt);
4493 /* If the next format won't be changing due to relaxation, just
4494 return the length of the first format. */
4495 if (fragP->fr_opcode != fragP->fr_literal)
4496 return fmt_size;
4498 /* If during relaxation we have to pull an instruction out of a
4499 multi-slot instruction, we will return the more conservative
4500 number. This works because alignment on bigger instructions
4501 is more restrictive than alignment on smaller instructions.
4502 This is more conservative than we would like, but it happens
4503 infrequently. */
4505 if (xtensa_format_num_slots (xtensa_default_isa, fmt) > 1)
4506 return fmt_size;
4508 /* If we aren't doing one of our own relaxations or it isn't
4509 slot-based, then the insn size won't change. */
4510 if (fragP->fr_type != rs_machine_dependent)
4511 return fmt_size;
4512 if (fragP->fr_subtype != RELAX_SLOTS)
4513 return fmt_size;
4515 /* If an instruction is about to grow, return the longer size. */
4516 if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP1
4517 || fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP2)
4518 return 3;
4520 if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
4521 return 2 + fragP->tc_frag_data.text_expansion[0];
4523 return fmt_size;
4527 static int
4528 next_frag_format_size (const fragS *fragP)
4530 const fragS *next_fragP = next_non_empty_frag (fragP);
4531 return frag_format_size (next_fragP);
4535 /* If the next legit fragment is an end-of-loop marker,
4536 switch its state so it will instantiate a NOP. */
4538 static void
4539 update_next_frag_state (fragS *fragP)
4541 fragS *next_fragP = fragP->fr_next;
4542 fragS *new_target = NULL;
4544 if (align_targets)
4546 /* We are guaranteed there will be one of these... */
4547 while (!(next_fragP->fr_type == rs_machine_dependent
4548 && (next_fragP->fr_subtype == RELAX_MAYBE_UNREACHABLE
4549 || next_fragP->fr_subtype == RELAX_UNREACHABLE)))
4550 next_fragP = next_fragP->fr_next;
4552 assert (next_fragP->fr_type == rs_machine_dependent
4553 && (next_fragP->fr_subtype == RELAX_MAYBE_UNREACHABLE
4554 || next_fragP->fr_subtype == RELAX_UNREACHABLE));
4556 /* ...and one of these. */
4557 new_target = next_fragP->fr_next;
4558 while (!(new_target->fr_type == rs_machine_dependent
4559 && (new_target->fr_subtype == RELAX_MAYBE_DESIRE_ALIGN
4560 || new_target->fr_subtype == RELAX_DESIRE_ALIGN)))
4561 new_target = new_target->fr_next;
4563 assert (new_target->fr_type == rs_machine_dependent
4564 && (new_target->fr_subtype == RELAX_MAYBE_DESIRE_ALIGN
4565 || new_target->fr_subtype == RELAX_DESIRE_ALIGN));
4568 while (next_fragP && next_fragP->fr_fix == 0)
4570 if (next_fragP->fr_type == rs_machine_dependent
4571 && next_fragP->fr_subtype == RELAX_LOOP_END)
4573 next_fragP->fr_subtype = RELAX_LOOP_END_ADD_NOP;
4574 return;
4577 next_fragP = next_fragP->fr_next;
4582 static bfd_boolean
4583 next_frag_is_branch_target (const fragS *fragP)
4585 /* Sometimes an empty will end up here due to storage allocation issues,
4586 so we have to skip until we find something legit. */
4587 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4589 if (fragP->tc_frag_data.is_branch_target)
4590 return TRUE;
4591 if (fragP->fr_fix != 0)
4592 break;
4594 return FALSE;
4598 static bfd_boolean
4599 next_frag_is_loop_target (const fragS *fragP)
4601 /* Sometimes an empty will end up here due storage allocation issues.
4602 So we have to skip until we find something legit. */
4603 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4605 if (fragP->tc_frag_data.is_loop_target)
4606 return TRUE;
4607 if (fragP->fr_fix != 0)
4608 break;
4610 return FALSE;
4614 static addressT
4615 next_frag_pre_opcode_bytes (const fragS *fragp)
4617 const fragS *next_fragp = fragp->fr_next;
4618 xtensa_opcode next_opcode;
4620 if (!next_frag_opcode_is_loop (fragp, &next_opcode))
4621 return 0;
4623 /* Sometimes an empty will end up here due to storage allocation issues,
4624 so we have to skip until we find something legit. */
4625 while (next_fragp->fr_fix == 0)
4626 next_fragp = next_fragp->fr_next;
4628 if (next_fragp->fr_type != rs_machine_dependent)
4629 return 0;
4631 /* There is some implicit knowledge encoded in here.
4632 The LOOP instructions that are NOT RELAX_IMMED have
4633 been relaxed. Note that we can assume that the LOOP
4634 instruction is in slot 0 because loops aren't bundleable. */
4635 if (next_fragp->tc_frag_data.slot_subtypes[0] > RELAX_IMMED)
4636 return get_expanded_loop_offset (next_opcode);
4638 return 0;
4642 /* Mark a location where we can later insert literal frags. Update
4643 the section's literal_pool_loc, so subsequent literals can be
4644 placed nearest to their use. */
4646 static void
4647 xtensa_mark_literal_pool_location (void)
4649 /* Any labels pointing to the current location need
4650 to be adjusted to after the literal pool. */
4651 emit_state s;
4652 fragS *pool_location;
4654 if (use_literal_section && !directive_state[directive_absolute_literals])
4655 return;
4657 frag_align (2, 0, 0);
4658 record_alignment (now_seg, 2);
4660 /* We stash info in the fr_var of these frags
4661 so we can later move the literal's fixes into this
4662 frchain's fix list. We can use fr_var because fr_var's
4663 interpretation depends solely on the fr_type and subtype. */
4664 pool_location = frag_now;
4665 frag_variant (rs_machine_dependent, 0, (int) frchain_now,
4666 RELAX_LITERAL_POOL_BEGIN, NULL, 0, NULL);
4667 xtensa_set_frag_assembly_state (frag_now);
4668 frag_variant (rs_machine_dependent, 0, (int) now_seg,
4669 RELAX_LITERAL_POOL_END, NULL, 0, NULL);
4670 xtensa_set_frag_assembly_state (frag_now);
4672 /* Now put a frag into the literal pool that points to this location. */
4673 set_literal_pool_location (now_seg, pool_location);
4674 xtensa_switch_to_non_abs_literal_fragment (&s);
4675 frag_align (2, 0, 0);
4676 record_alignment (now_seg, 2);
4678 /* Close whatever frag is there. */
4679 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4680 xtensa_set_frag_assembly_state (frag_now);
4681 frag_now->tc_frag_data.literal_frag = pool_location;
4682 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4683 xtensa_restore_emit_state (&s);
4684 xtensa_set_frag_assembly_state (frag_now);
4688 /* Build a nop of the correct size into tinsn. */
4690 static void
4691 build_nop (TInsn *tinsn, int size)
4693 tinsn_init (tinsn);
4694 switch (size)
4696 case 2:
4697 tinsn->opcode = xtensa_nop_n_opcode;
4698 tinsn->ntok = 0;
4699 if (tinsn->opcode == XTENSA_UNDEFINED)
4700 as_fatal (_("opcode 'NOP.N' unavailable in this configuration"));
4701 break;
4703 case 3:
4704 if (xtensa_nop_opcode == XTENSA_UNDEFINED)
4706 tinsn->opcode = xtensa_or_opcode;
4707 set_expr_const (&tinsn->tok[0], 1);
4708 set_expr_const (&tinsn->tok[1], 1);
4709 set_expr_const (&tinsn->tok[2], 1);
4710 tinsn->ntok = 3;
4712 else
4713 tinsn->opcode = xtensa_nop_opcode;
4715 assert (tinsn->opcode != XTENSA_UNDEFINED);
4720 /* Assemble a NOP of the requested size in the buffer. User must have
4721 allocated "buf" with at least "size" bytes. */
4723 static void
4724 assemble_nop (size_t size, char *buf)
4726 static xtensa_insnbuf insnbuf = NULL;
4727 TInsn tinsn;
4729 build_nop (&tinsn, size);
4731 if (!insnbuf)
4732 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4734 tinsn_to_insnbuf (&tinsn, insnbuf);
4735 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf, 0);
4739 /* Return the number of bytes for the offset of the expanded loop
4740 instruction. This should be incorporated into the relaxation
4741 specification but is hard-coded here. This is used to auto-align
4742 the loop instruction. It is invalid to call this function if the
4743 configuration does not have loops or if the opcode is not a loop
4744 opcode. */
4746 static addressT
4747 get_expanded_loop_offset (xtensa_opcode opcode)
4749 /* This is the OFFSET of the loop instruction in the expanded loop.
4750 This MUST correspond directly to the specification of the loop
4751 expansion. It will be validated on fragment conversion. */
4752 assert (opcode != XTENSA_UNDEFINED);
4753 if (opcode == xtensa_loop_opcode)
4754 return 0;
4755 if (opcode == xtensa_loopnez_opcode)
4756 return 3;
4757 if (opcode == xtensa_loopgtz_opcode)
4758 return 6;
4759 as_fatal (_("get_expanded_loop_offset: invalid opcode"));
4760 return 0;
4764 static fragS *
4765 get_literal_pool_location (segT seg)
4767 return seg_info (seg)->tc_segment_info_data.literal_pool_loc;
4771 static void
4772 set_literal_pool_location (segT seg, fragS *literal_pool_loc)
4774 seg_info (seg)->tc_segment_info_data.literal_pool_loc = literal_pool_loc;
4778 /* Set frag assembly state should be called when a new frag is
4779 opened and after a frag has been closed. */
4781 static void
4782 xtensa_set_frag_assembly_state (fragS *fragP)
4784 if (!density_supported)
4785 fragP->tc_frag_data.is_no_density = TRUE;
4787 /* This function is called from subsegs_finish, which is called
4788 after xtensa_end, so we can't use "use_transform" or
4789 "use_schedule" here. */
4790 if (!directive_state[directive_transform])
4791 fragP->tc_frag_data.is_no_transform = TRUE;
4792 fragP->tc_frag_data.use_absolute_literals =
4793 directive_state[directive_absolute_literals];
4794 fragP->tc_frag_data.is_assembly_state_set = TRUE;
4798 static bfd_boolean
4799 relaxable_section (asection *sec)
4801 return (sec->flags & SEC_DEBUGGING) == 0;
4805 static void
4806 xtensa_find_unmarked_state_frags (void)
4808 segT *seclist;
4810 /* Walk over each fragment of all of the current segments. For each
4811 unmarked fragment, mark it with the same info as the previous
4812 fragment. */
4813 for (seclist = &stdoutput->sections;
4814 seclist && *seclist;
4815 seclist = &(*seclist)->next)
4817 segT sec = *seclist;
4818 segment_info_type *seginfo;
4819 fragS *fragP;
4820 flagword flags;
4821 flags = bfd_get_section_flags (stdoutput, sec);
4822 if (flags & SEC_DEBUGGING)
4823 continue;
4824 if (!(flags & SEC_ALLOC))
4825 continue;
4827 seginfo = seg_info (sec);
4828 if (seginfo && seginfo->frchainP)
4830 fragS *last_fragP = 0;
4831 for (fragP = seginfo->frchainP->frch_root; fragP;
4832 fragP = fragP->fr_next)
4834 if (fragP->fr_fix != 0
4835 && !fragP->tc_frag_data.is_assembly_state_set)
4837 if (last_fragP == 0)
4839 as_warn_where (fragP->fr_file, fragP->fr_line,
4840 _("assembly state not set for first frag in section %s"),
4841 sec->name);
4843 else
4845 fragP->tc_frag_data.is_assembly_state_set = TRUE;
4846 fragP->tc_frag_data.is_no_density =
4847 last_fragP->tc_frag_data.is_no_density;
4848 fragP->tc_frag_data.is_no_transform =
4849 last_fragP->tc_frag_data.is_no_transform;
4850 fragP->tc_frag_data.use_absolute_literals =
4851 last_fragP->tc_frag_data.use_absolute_literals;
4854 if (fragP->tc_frag_data.is_assembly_state_set)
4855 last_fragP = fragP;
4862 static void
4863 xtensa_find_unaligned_branch_targets (bfd *abfd ATTRIBUTE_UNUSED,
4864 asection *sec,
4865 void *unused ATTRIBUTE_UNUSED)
4867 flagword flags = bfd_get_section_flags (abfd, sec);
4868 segment_info_type *seginfo = seg_info (sec);
4869 fragS *frag = seginfo->frchainP->frch_root;
4871 if (flags & SEC_CODE)
4873 xtensa_isa isa = xtensa_default_isa;
4874 xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa);
4875 while (frag != NULL)
4877 if (frag->tc_frag_data.is_branch_target)
4879 int op_size;
4880 int frag_addr;
4881 xtensa_format fmt;
4883 xtensa_insnbuf_from_chars (isa, insnbuf, frag->fr_literal, 0);
4884 fmt = xtensa_format_decode (isa, insnbuf);
4885 op_size = xtensa_format_length (isa, fmt);
4886 frag_addr = frag->fr_address % xtensa_fetch_width;
4887 if (frag_addr + op_size > (int) xtensa_fetch_width)
4888 as_warn_where (frag->fr_file, frag->fr_line,
4889 _("unaligned branch target: %d bytes at 0x%lx"),
4890 op_size, frag->fr_address);
4892 frag = frag->fr_next;
4894 xtensa_insnbuf_free (isa, insnbuf);
4899 static void
4900 xtensa_find_unaligned_loops (bfd *abfd ATTRIBUTE_UNUSED,
4901 asection *sec,
4902 void *unused ATTRIBUTE_UNUSED)
4904 flagword flags = bfd_get_section_flags (abfd, sec);
4905 segment_info_type *seginfo = seg_info (sec);
4906 fragS *frag = seginfo->frchainP->frch_root;
4907 xtensa_isa isa = xtensa_default_isa;
4909 if (flags & SEC_CODE)
4911 xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa);
4912 while (frag != NULL)
4914 if (frag->tc_frag_data.is_first_loop_insn)
4916 int op_size;
4917 int frag_addr;
4918 xtensa_format fmt;
4920 xtensa_insnbuf_from_chars (isa, insnbuf, frag->fr_literal, 0);
4921 fmt = xtensa_format_decode (isa, insnbuf);
4922 op_size = xtensa_format_length (isa, fmt);
4923 frag_addr = frag->fr_address % xtensa_fetch_width;
4925 if (frag_addr + op_size > (signed) xtensa_fetch_width)
4926 as_warn_where (frag->fr_file, frag->fr_line,
4927 _("unaligned loop: %d bytes at 0x%lx"),
4928 op_size, frag->fr_address);
4930 frag = frag->fr_next;
4932 xtensa_insnbuf_free (isa, insnbuf);
4937 static void
4938 xg_apply_tentative_value (fixS *fixP, valueT val)
4940 xtensa_isa isa = xtensa_default_isa;
4941 static xtensa_insnbuf insnbuf = NULL;
4942 static xtensa_insnbuf slotbuf = NULL;
4943 xtensa_format fmt;
4944 int slot;
4945 bfd_boolean alt_reloc;
4946 xtensa_opcode opcode;
4947 char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
4949 (void) decode_reloc (fixP->fx_r_type, &slot, &alt_reloc);
4950 if (alt_reloc)
4951 as_fatal (_("unexpected fix"));
4953 if (!insnbuf)
4955 insnbuf = xtensa_insnbuf_alloc (isa);
4956 slotbuf = xtensa_insnbuf_alloc (isa);
4959 xtensa_insnbuf_from_chars (isa, insnbuf, fixpos, 0);
4960 fmt = xtensa_format_decode (isa, insnbuf);
4961 if (fmt == XTENSA_UNDEFINED)
4962 as_fatal (_("undecodable fix"));
4963 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
4964 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
4965 if (opcode == XTENSA_UNDEFINED)
4966 as_fatal (_("undecodable fix"));
4968 /* CONST16 immediates are not PC-relative, despite the fact that we
4969 reuse the normal PC-relative operand relocations for the low part
4970 of a CONST16 operand. The code in tc_gen_reloc does not decode
4971 the opcodes so it is more convenient to detect this special case
4972 here. */
4973 if (opcode == xtensa_const16_opcode)
4974 return;
4976 xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode,
4977 get_relaxable_immed (opcode), val,
4978 fixP->fx_file, fixP->fx_line);
4980 xtensa_format_set_slot (isa, fmt, slot, insnbuf, slotbuf);
4981 xtensa_insnbuf_to_chars (isa, insnbuf, fixpos, 0);
4985 /* External Functions and Other GAS Hooks. */
4987 const char *
4988 xtensa_target_format (void)
4990 return (target_big_endian ? "elf32-xtensa-be" : "elf32-xtensa-le");
4994 void
4995 xtensa_file_arch_init (bfd *abfd)
4997 bfd_set_private_flags (abfd, 0x100 | 0x200);
5001 void
5002 md_number_to_chars (char *buf, valueT val, int n)
5004 if (target_big_endian)
5005 number_to_chars_bigendian (buf, val, n);
5006 else
5007 number_to_chars_littleendian (buf, val, n);
5011 /* This function is called once, at assembler startup time. It should
5012 set up all the tables, etc. that the MD part of the assembler will
5013 need. */
5015 void
5016 md_begin (void)
5018 segT current_section = now_seg;
5019 int current_subsec = now_subseg;
5020 xtensa_isa isa;
5022 xtensa_default_isa = xtensa_isa_init (0, 0);
5023 isa = xtensa_default_isa;
5025 linkrelax = 1;
5027 /* Set up the .literal, .fini.literal and .init.literal sections. */
5028 memset (&default_lit_sections, 0, sizeof (default_lit_sections));
5029 default_lit_sections.init_lit_seg_name = INIT_LITERAL_SECTION_NAME;
5030 default_lit_sections.fini_lit_seg_name = FINI_LITERAL_SECTION_NAME;
5031 default_lit_sections.lit_seg_name = LITERAL_SECTION_NAME;
5032 default_lit_sections.lit4_seg_name = LIT4_SECTION_NAME;
5034 subseg_set (current_section, current_subsec);
5036 xg_init_vinsn (&cur_vinsn);
5038 xtensa_addi_opcode = xtensa_opcode_lookup (isa, "addi");
5039 xtensa_addmi_opcode = xtensa_opcode_lookup (isa, "addmi");
5040 xtensa_call0_opcode = xtensa_opcode_lookup (isa, "call0");
5041 xtensa_call4_opcode = xtensa_opcode_lookup (isa, "call4");
5042 xtensa_call8_opcode = xtensa_opcode_lookup (isa, "call8");
5043 xtensa_call12_opcode = xtensa_opcode_lookup (isa, "call12");
5044 xtensa_callx0_opcode = xtensa_opcode_lookup (isa, "callx0");
5045 xtensa_callx4_opcode = xtensa_opcode_lookup (isa, "callx4");
5046 xtensa_callx8_opcode = xtensa_opcode_lookup (isa, "callx8");
5047 xtensa_callx12_opcode = xtensa_opcode_lookup (isa, "callx12");
5048 xtensa_const16_opcode = xtensa_opcode_lookup (isa, "const16");
5049 xtensa_entry_opcode = xtensa_opcode_lookup (isa, "entry");
5050 xtensa_movi_opcode = xtensa_opcode_lookup (isa, "movi");
5051 xtensa_movi_n_opcode = xtensa_opcode_lookup (isa, "movi.n");
5052 xtensa_isync_opcode = xtensa_opcode_lookup (isa, "isync");
5053 xtensa_jx_opcode = xtensa_opcode_lookup (isa, "jx");
5054 xtensa_l32r_opcode = xtensa_opcode_lookup (isa, "l32r");
5055 xtensa_loop_opcode = xtensa_opcode_lookup (isa, "loop");
5056 xtensa_loopnez_opcode = xtensa_opcode_lookup (isa, "loopnez");
5057 xtensa_loopgtz_opcode = xtensa_opcode_lookup (isa, "loopgtz");
5058 xtensa_nop_opcode = xtensa_opcode_lookup (isa, "nop");
5059 xtensa_nop_n_opcode = xtensa_opcode_lookup (isa, "nop.n");
5060 xtensa_or_opcode = xtensa_opcode_lookup (isa, "or");
5061 xtensa_ret_opcode = xtensa_opcode_lookup (isa, "ret");
5062 xtensa_ret_n_opcode = xtensa_opcode_lookup (isa, "ret.n");
5063 xtensa_retw_opcode = xtensa_opcode_lookup (isa, "retw");
5064 xtensa_retw_n_opcode = xtensa_opcode_lookup (isa, "retw.n");
5065 xtensa_rsr_lcount_opcode = xtensa_opcode_lookup (isa, "rsr.lcount");
5066 xtensa_waiti_opcode = xtensa_opcode_lookup (isa, "waiti");
5068 init_op_placement_info_table ();
5070 /* Set up the assembly state. */
5071 if (!frag_now->tc_frag_data.is_assembly_state_set)
5072 xtensa_set_frag_assembly_state (frag_now);
5076 /* TC_INIT_FIX_DATA hook */
5078 void
5079 xtensa_init_fix_data (fixS *x)
5081 x->tc_fix_data.slot = 0;
5082 x->tc_fix_data.X_add_symbol = NULL;
5083 x->tc_fix_data.X_add_number = 0;
5087 /* tc_frob_label hook */
5089 void
5090 xtensa_frob_label (symbolS *sym)
5092 /* Since the label was already attached to a frag associated with the
5093 previous basic block, it now needs to be reset to the current frag. */
5094 symbol_set_frag (sym, frag_now);
5095 S_SET_VALUE (sym, (valueT) frag_now_fix ());
5097 if (generating_literals)
5098 xtensa_add_literal_sym (sym);
5099 else
5100 xtensa_add_insn_label (sym);
5102 if (symbol_get_tc (sym)->is_loop_target
5103 && (get_last_insn_flags (now_seg, now_subseg)
5104 & FLAG_IS_BAD_LOOPEND) != 0)
5105 as_bad (_("invalid last instruction for a zero-overhead loop"));
5107 /* No target aligning in the absolute section. */
5108 if (now_seg != absolute_section
5109 && do_align_targets ()
5110 && !is_unaligned_label (sym)
5111 && !generating_literals)
5113 float freq = get_subseg_target_freq (now_seg, now_subseg);
5114 xtensa_set_frag_assembly_state (frag_now);
5116 /* The only time this type of frag grows is when there is a
5117 negatable branch that needs to be relaxed as the last
5118 instruction in a zero-overhead loop. Because alignment frags
5119 are so common, marking them all as possibly growing four
5120 bytes makes any worst-case analysis appear much worse than it
5121 is. So, we make fr_var not actually reflect the amount of
5122 memory allocated at the end of this frag, but rather the
5123 amount of memory this frag might grow. The "4, 0" below
5124 allocates four bytes at the end of the frag for room to grow
5125 if we need to relax a loop end with a NOP. Frags prior to
5126 this one might grow to align this one, but the frag itself
5127 won't grow unless it meets the condition above. */
5129 #define RELAX_LOOP_END_BYTES 4
5131 frag_var (rs_machine_dependent,
5132 RELAX_LOOP_END_BYTES, (int) freq,
5133 RELAX_DESIRE_ALIGN_IF_TARGET,
5134 frag_now->fr_symbol, frag_now->fr_offset, NULL);
5135 xtensa_set_frag_assembly_state (frag_now);
5136 xtensa_move_labels (frag_now, 0, TRUE);
5139 /* We need to mark the following properties even if we aren't aligning. */
5141 /* If the label is already known to be a branch target, i.e., a
5142 forward branch, mark the frag accordingly. Backward branches
5143 are handled by xg_add_branch_and_loop_targets. */
5144 if (symbol_get_tc (sym)->is_branch_target)
5145 symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
5147 /* Loops only go forward, so they can be identified here. */
5148 if (symbol_get_tc (sym)->is_loop_target)
5149 symbol_get_frag (sym)->tc_frag_data.is_loop_target = TRUE;
5153 /* tc_unrecognized_line hook */
5156 xtensa_unrecognized_line (int ch)
5158 switch (ch)
5160 case '{' :
5161 if (cur_vinsn.inside_bundle == 0)
5163 /* PR8110: Cannot emit line number info inside a FLIX bundle
5164 when using --gstabs. Temporarily disable debug info. */
5165 generate_lineno_debug ();
5166 if (debug_type == DEBUG_STABS)
5168 xt_saved_debug_type = debug_type;
5169 debug_type = DEBUG_NONE;
5172 cur_vinsn.inside_bundle = 1;
5174 else
5176 as_bad (_("extra opening brace"));
5177 return 0;
5179 break;
5181 case '}' :
5182 if (cur_vinsn.inside_bundle)
5183 finish_vinsn (&cur_vinsn);
5184 else
5186 as_bad (_("extra closing brace"));
5187 return 0;
5189 break;
5190 default:
5191 as_bad (_("syntax error"));
5192 return 0;
5194 return 1;
5198 /* md_flush_pending_output hook */
5200 void
5201 xtensa_flush_pending_output (void)
5203 if (cur_vinsn.inside_bundle)
5204 as_bad (_("missing closing brace"));
5206 /* If there is a non-zero instruction fragment, close it. */
5207 if (frag_now_fix () != 0 && frag_now->tc_frag_data.is_insn)
5209 frag_wane (frag_now);
5210 frag_new (0);
5211 xtensa_set_frag_assembly_state (frag_now);
5213 frag_now->tc_frag_data.is_insn = FALSE;
5215 xtensa_clear_insn_labels ();
5219 /* We had an error while parsing an instruction. The string might look
5220 like this: "insn arg1, arg2 }". If so, we need to see the closing
5221 brace and reset some fields. Otherwise, the vinsn never gets closed
5222 and the num_slots field will grow past the end of the array of slots,
5223 and bad things happen. */
5225 static void
5226 error_reset_cur_vinsn (void)
5228 if (cur_vinsn.inside_bundle)
5230 if (*input_line_pointer == '}'
5231 || *(input_line_pointer - 1) == '}'
5232 || *(input_line_pointer - 2) == '}')
5233 xg_clear_vinsn (&cur_vinsn);
5238 void
5239 md_assemble (char *str)
5241 xtensa_isa isa = xtensa_default_isa;
5242 char *opname;
5243 unsigned opnamelen;
5244 bfd_boolean has_underbar = FALSE;
5245 char *arg_strings[MAX_INSN_ARGS];
5246 int num_args;
5247 TInsn orig_insn; /* Original instruction from the input. */
5249 tinsn_init (&orig_insn);
5251 /* Split off the opcode. */
5252 opnamelen = strspn (str, "abcdefghijklmnopqrstuvwxyz_/0123456789.");
5253 opname = xmalloc (opnamelen + 1);
5254 memcpy (opname, str, opnamelen);
5255 opname[opnamelen] = '\0';
5257 num_args = tokenize_arguments (arg_strings, str + opnamelen);
5258 if (num_args == -1)
5260 as_bad (_("syntax error"));
5261 return;
5264 if (xg_translate_idioms (&opname, &num_args, arg_strings))
5265 return;
5267 /* Check for an underbar prefix. */
5268 if (*opname == '_')
5270 has_underbar = TRUE;
5271 opname += 1;
5274 orig_insn.insn_type = ITYPE_INSN;
5275 orig_insn.ntok = 0;
5276 orig_insn.is_specific_opcode = (has_underbar || !use_transform ());
5278 orig_insn.opcode = xtensa_opcode_lookup (isa, opname);
5279 if (orig_insn.opcode == XTENSA_UNDEFINED)
5281 xtensa_format fmt = xtensa_format_lookup (isa, opname);
5282 if (fmt == XTENSA_UNDEFINED)
5284 as_bad (_("unknown opcode or format name '%s'"), opname);
5285 error_reset_cur_vinsn ();
5286 return;
5288 if (!cur_vinsn.inside_bundle)
5290 as_bad (_("format names only valid inside bundles"));
5291 error_reset_cur_vinsn ();
5292 return;
5294 if (cur_vinsn.format != XTENSA_UNDEFINED)
5295 as_warn (_("multiple formats specified for one bundle; using '%s'"),
5296 opname);
5297 cur_vinsn.format = fmt;
5298 free (has_underbar ? opname - 1 : opname);
5299 error_reset_cur_vinsn ();
5300 return;
5303 /* Special case: The call instructions should be marked "specific opcode"
5304 to keep them from expanding. */
5305 if (!use_longcalls () && is_direct_call_opcode (orig_insn.opcode))
5306 orig_insn.is_specific_opcode = TRUE;
5308 /* Parse the arguments. */
5309 if (parse_arguments (&orig_insn, num_args, arg_strings))
5311 as_bad (_("syntax error"));
5312 error_reset_cur_vinsn ();
5313 return;
5316 /* Free the opcode and argument strings, now that they've been parsed. */
5317 free (has_underbar ? opname - 1 : opname);
5318 opname = 0;
5319 while (num_args-- > 0)
5320 free (arg_strings[num_args]);
5322 /* Get expressions for invisible operands. */
5323 if (get_invisible_operands (&orig_insn))
5325 error_reset_cur_vinsn ();
5326 return;
5329 /* Check for the right number and type of arguments. */
5330 if (tinsn_check_arguments (&orig_insn))
5332 error_reset_cur_vinsn ();
5333 return;
5336 dwarf2_where (&orig_insn.loc);
5338 xg_add_branch_and_loop_targets (&orig_insn);
5340 /* Special-case for "entry" instruction. */
5341 if (orig_insn.opcode == xtensa_entry_opcode)
5343 /* Check that the third opcode (#2) is >= 16. */
5344 if (orig_insn.ntok >= 3)
5346 expressionS *exp = &orig_insn.tok[2];
5347 switch (exp->X_op)
5349 case O_constant:
5350 if (exp->X_add_number < 16)
5351 as_warn (_("entry instruction with stack decrement < 16"));
5352 break;
5354 default:
5355 as_warn (_("entry instruction with non-constant decrement"));
5360 /* Finish it off:
5361 assemble_tokens (opcode, tok, ntok);
5362 expand the tokens from the orig_insn into the
5363 stack of instructions that will not expand
5364 unless required at relaxation time. */
5366 if (!cur_vinsn.inside_bundle)
5367 emit_single_op (&orig_insn);
5368 else /* We are inside a bundle. */
5370 cur_vinsn.slots[cur_vinsn.num_slots] = orig_insn;
5371 cur_vinsn.num_slots++;
5372 if (*input_line_pointer == '}'
5373 || *(input_line_pointer - 1) == '}'
5374 || *(input_line_pointer - 2) == '}')
5375 finish_vinsn (&cur_vinsn);
5378 /* We've just emitted a new instruction so clear the list of labels. */
5379 xtensa_clear_insn_labels ();
5383 /* HANDLE_ALIGN hook */
5385 /* For a .align directive, we mark the previous block with the alignment
5386 information. This will be placed in the object file in the
5387 property section corresponding to this section. */
5389 void
5390 xtensa_handle_align (fragS *fragP)
5392 if (linkrelax
5393 && ! fragP->tc_frag_data.is_literal
5394 && (fragP->fr_type == rs_align
5395 || fragP->fr_type == rs_align_code)
5396 && fragP->fr_address + fragP->fr_fix > 0
5397 && fragP->fr_offset > 0
5398 && now_seg != bss_section)
5400 fragP->tc_frag_data.is_align = TRUE;
5401 fragP->tc_frag_data.alignment = fragP->fr_offset;
5404 if (fragP->fr_type == rs_align_test)
5406 int count;
5407 count = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
5408 if (count != 0)
5409 as_bad_where (fragP->fr_file, fragP->fr_line,
5410 _("unaligned entry instruction"));
5415 /* TC_FRAG_INIT hook */
5417 void
5418 xtensa_frag_init (fragS *frag)
5420 xtensa_set_frag_assembly_state (frag);
5424 symbolS *
5425 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
5427 return NULL;
5431 /* Round up a section size to the appropriate boundary. */
5433 valueT
5434 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
5436 return size; /* Byte alignment is fine. */
5440 long
5441 md_pcrel_from (fixS *fixP)
5443 char *insn_p;
5444 static xtensa_insnbuf insnbuf = NULL;
5445 static xtensa_insnbuf slotbuf = NULL;
5446 int opnum;
5447 uint32 opnd_value;
5448 xtensa_opcode opcode;
5449 xtensa_format fmt;
5450 int slot;
5451 xtensa_isa isa = xtensa_default_isa;
5452 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
5453 bfd_boolean alt_reloc;
5455 if (fixP->fx_done)
5456 return addr;
5458 if (fixP->fx_r_type == BFD_RELOC_XTENSA_ASM_EXPAND)
5459 return addr;
5461 if (!insnbuf)
5463 insnbuf = xtensa_insnbuf_alloc (isa);
5464 slotbuf = xtensa_insnbuf_alloc (isa);
5467 insn_p = &fixP->fx_frag->fr_literal[fixP->fx_where];
5468 xtensa_insnbuf_from_chars (isa, insnbuf, insn_p, 0);
5469 fmt = xtensa_format_decode (isa, insnbuf);
5471 if (fmt == XTENSA_UNDEFINED)
5472 as_fatal (_("bad instruction format"));
5474 if (decode_reloc (fixP->fx_r_type, &slot, &alt_reloc) != 0)
5475 as_fatal (_("invalid relocation"));
5477 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
5478 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
5480 /* Check for "alternate" relocation (operand not specified). */
5481 if (alt_reloc || opcode == xtensa_const16_opcode)
5483 if (opcode != xtensa_l32r_opcode
5484 && opcode != xtensa_const16_opcode)
5485 as_fatal (_("invalid relocation for '%s' instruction"),
5486 xtensa_opcode_name (isa, opcode));
5487 return addr;
5490 opnum = get_relaxable_immed (opcode);
5491 opnd_value = 0;
5492 if (xtensa_operand_is_PCrelative (isa, opcode, opnum) != 1
5493 || xtensa_operand_do_reloc (isa, opcode, opnum, &opnd_value, addr))
5495 as_bad_where (fixP->fx_file,
5496 fixP->fx_line,
5497 _("invalid relocation for operand %d of '%s'"),
5498 opnum, xtensa_opcode_name (isa, opcode));
5499 return addr;
5501 return 0 - opnd_value;
5505 /* TC_FORCE_RELOCATION hook */
5508 xtensa_force_relocation (fixS *fix)
5510 switch (fix->fx_r_type)
5512 case BFD_RELOC_XTENSA_SLOT0_ALT:
5513 case BFD_RELOC_XTENSA_SLOT1_ALT:
5514 case BFD_RELOC_XTENSA_SLOT2_ALT:
5515 case BFD_RELOC_XTENSA_SLOT3_ALT:
5516 case BFD_RELOC_XTENSA_SLOT4_ALT:
5517 case BFD_RELOC_XTENSA_SLOT5_ALT:
5518 case BFD_RELOC_XTENSA_SLOT6_ALT:
5519 case BFD_RELOC_XTENSA_SLOT7_ALT:
5520 case BFD_RELOC_XTENSA_SLOT8_ALT:
5521 case BFD_RELOC_XTENSA_SLOT9_ALT:
5522 case BFD_RELOC_XTENSA_SLOT10_ALT:
5523 case BFD_RELOC_XTENSA_SLOT11_ALT:
5524 case BFD_RELOC_XTENSA_SLOT12_ALT:
5525 case BFD_RELOC_XTENSA_SLOT13_ALT:
5526 case BFD_RELOC_XTENSA_SLOT14_ALT:
5527 case BFD_RELOC_VTABLE_INHERIT:
5528 case BFD_RELOC_VTABLE_ENTRY:
5529 return 1;
5530 default:
5531 break;
5534 if (linkrelax && fix->fx_addsy
5535 && relaxable_section (S_GET_SEGMENT (fix->fx_addsy)))
5536 return 1;
5538 return generic_force_reloc (fix);
5542 /* NO_PSEUDO_DOT hook */
5544 /* This function has nothing to do with pseudo dots, but this is the
5545 nearest macro to where the check needs to take place. FIXME: This
5546 seems wrong. */
5548 bfd_boolean
5549 xtensa_check_inside_bundle (void)
5551 if (cur_vinsn.inside_bundle && input_line_pointer[-1] == '.')
5552 as_bad (_("directives are not valid inside bundles"));
5554 /* This function must always return FALSE because it is called via a
5555 macro that has nothing to do with bundling. */
5556 return FALSE;
5560 /* md_elf_section_change_hook */
5562 void
5563 xtensa_elf_section_change_hook (void)
5565 /* Set up the assembly state. */
5566 if (!frag_now->tc_frag_data.is_assembly_state_set)
5567 xtensa_set_frag_assembly_state (frag_now);
5571 /* tc_fix_adjustable hook */
5573 bfd_boolean
5574 xtensa_fix_adjustable (fixS *fixP)
5576 /* An offset is not allowed in combination with the difference of two
5577 symbols, but that cannot be easily detected after a local symbol
5578 has been adjusted to a (section+offset) form. Return 0 so that such
5579 an fix will not be adjusted. */
5580 if (fixP->fx_subsy && fixP->fx_addsy && fixP->fx_offset
5581 && relaxable_section (S_GET_SEGMENT (fixP->fx_subsy)))
5582 return 0;
5584 /* We need the symbol name for the VTABLE entries. */
5585 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
5586 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
5587 return 0;
5589 if (fixP->fx_addsy
5590 && (S_IS_EXTERNAL (fixP->fx_addsy) || S_IS_WEAK (fixP->fx_addsy)))
5591 return 0;
5593 return 1;
5597 void
5598 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
5600 if (fixP->fx_pcrel == 0 && fixP->fx_addsy == 0)
5602 char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
5604 switch (fixP->fx_r_type)
5606 case BFD_RELOC_XTENSA_ASM_EXPAND:
5607 fixP->fx_done = 1;
5608 break;
5610 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
5611 as_bad (_("unhandled local relocation fix %s"),
5612 bfd_get_reloc_code_name (fixP->fx_r_type));
5613 break;
5615 case BFD_RELOC_32:
5616 case BFD_RELOC_16:
5617 case BFD_RELOC_8:
5618 /* The only one we support that isn't an instruction field. */
5619 md_number_to_chars (fixpos, *valP, fixP->fx_size);
5620 fixP->fx_done = 1;
5621 break;
5623 case BFD_RELOC_VTABLE_INHERIT:
5624 case BFD_RELOC_VTABLE_ENTRY:
5625 fixP->fx_done = 0;
5626 break;
5628 default:
5629 as_bad (_("unhandled local relocation fix %s"),
5630 bfd_get_reloc_code_name (fixP->fx_r_type));
5636 char *
5637 md_atof (int type, char *litP, int *sizeP)
5639 int prec;
5640 LITTLENUM_TYPE words[4];
5641 char *t;
5642 int i;
5644 switch (type)
5646 case 'f':
5647 prec = 2;
5648 break;
5650 case 'd':
5651 prec = 4;
5652 break;
5654 default:
5655 *sizeP = 0;
5656 return "bad call to md_atof";
5659 t = atof_ieee (input_line_pointer, type, words);
5660 if (t)
5661 input_line_pointer = t;
5663 *sizeP = prec * 2;
5665 for (i = prec - 1; i >= 0; i--)
5667 int idx = i;
5668 if (target_big_endian)
5669 idx = (prec - 1 - i);
5671 md_number_to_chars (litP, (valueT) words[idx], 2);
5672 litP += 2;
5675 return NULL;
5680 md_estimate_size_before_relax (fragS *fragP, segT seg ATTRIBUTE_UNUSED)
5682 return total_frag_text_expansion (fragP);
5686 /* Translate internal representation of relocation info to BFD target
5687 format. */
5689 arelent *
5690 tc_gen_reloc (asection *section, fixS *fixp)
5692 arelent *reloc;
5693 bfd_boolean apply_tentative_value = FALSE;
5695 reloc = (arelent *) xmalloc (sizeof (arelent));
5696 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5697 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
5698 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
5700 /* Make sure none of our internal relocations make it this far.
5701 They'd better have been fully resolved by this point. */
5702 assert ((int) fixp->fx_r_type > 0);
5704 if (linkrelax && fixp->fx_subsy
5705 && (fixp->fx_r_type == BFD_RELOC_8
5706 || fixp->fx_r_type == BFD_RELOC_16
5707 || fixp->fx_r_type == BFD_RELOC_32))
5709 int diff_size = 0;
5710 bfd_vma diff_value, diff_mask = 0;
5712 switch (fixp->fx_r_type)
5714 case BFD_RELOC_8:
5715 fixp->fx_r_type = BFD_RELOC_XTENSA_DIFF8;
5716 diff_size = 1;
5717 diff_mask = 0xff;
5718 break;
5719 case BFD_RELOC_16:
5720 fixp->fx_r_type = BFD_RELOC_XTENSA_DIFF16;
5721 diff_size = 2;
5722 diff_mask = 0xffff;
5723 break;
5724 case BFD_RELOC_32:
5725 fixp->fx_r_type = BFD_RELOC_XTENSA_DIFF32;
5726 diff_size = 4;
5727 diff_mask = 0xffffffff;
5728 break;
5729 default:
5730 break;
5733 /* An offset is only allowed when it results from adjusting a local
5734 symbol into a section-relative offset. If the offset came from the
5735 original expression, tc_fix_adjustable will have prevented the fix
5736 from being converted to a section-relative form so that we can flag
5737 the error here. */
5738 if (fixp->fx_offset != 0 && !symbol_section_p (fixp->fx_addsy))
5740 as_bad_where (fixp->fx_file, fixp->fx_line,
5741 _("cannot represent subtraction with an offset"));
5742 free (reloc->sym_ptr_ptr);
5743 free (reloc);
5744 return NULL;
5747 assert (S_GET_SEGMENT (fixp->fx_addsy)
5748 == S_GET_SEGMENT (fixp->fx_subsy));
5750 diff_value = (S_GET_VALUE (fixp->fx_addsy) + fixp->fx_offset
5751 - S_GET_VALUE (fixp->fx_subsy));
5753 /* Check for overflow. */
5754 if ((diff_value & ~diff_mask) != 0)
5756 as_bad_where (fixp->fx_file, fixp->fx_line,
5757 _("value of %ld too large"), diff_value);
5758 free (reloc->sym_ptr_ptr);
5759 free (reloc);
5760 return NULL;
5763 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
5764 diff_value, diff_size);
5765 reloc->addend = fixp->fx_offset - diff_value;
5767 else
5769 reloc->addend = fixp->fx_offset;
5771 switch (fixp->fx_r_type)
5773 case BFD_RELOC_XTENSA_SLOT0_OP:
5774 case BFD_RELOC_XTENSA_SLOT1_OP:
5775 case BFD_RELOC_XTENSA_SLOT2_OP:
5776 case BFD_RELOC_XTENSA_SLOT3_OP:
5777 case BFD_RELOC_XTENSA_SLOT4_OP:
5778 case BFD_RELOC_XTENSA_SLOT5_OP:
5779 case BFD_RELOC_XTENSA_SLOT6_OP:
5780 case BFD_RELOC_XTENSA_SLOT7_OP:
5781 case BFD_RELOC_XTENSA_SLOT8_OP:
5782 case BFD_RELOC_XTENSA_SLOT9_OP:
5783 case BFD_RELOC_XTENSA_SLOT10_OP:
5784 case BFD_RELOC_XTENSA_SLOT11_OP:
5785 case BFD_RELOC_XTENSA_SLOT12_OP:
5786 case BFD_RELOC_XTENSA_SLOT13_OP:
5787 case BFD_RELOC_XTENSA_SLOT14_OP:
5788 /* As a special case, the immediate value for a CONST16 opcode
5789 should not be applied, since this kind of relocation is
5790 handled specially for CONST16 and is not really PC-relative.
5791 Rather than decode the opcode here, just wait and handle it
5792 in xg_apply_tentative_value. */
5793 apply_tentative_value = TRUE;
5794 break;
5796 case BFD_RELOC_XTENSA_SLOT0_ALT:
5797 case BFD_RELOC_XTENSA_SLOT1_ALT:
5798 case BFD_RELOC_XTENSA_SLOT2_ALT:
5799 case BFD_RELOC_XTENSA_SLOT3_ALT:
5800 case BFD_RELOC_XTENSA_SLOT4_ALT:
5801 case BFD_RELOC_XTENSA_SLOT5_ALT:
5802 case BFD_RELOC_XTENSA_SLOT6_ALT:
5803 case BFD_RELOC_XTENSA_SLOT7_ALT:
5804 case BFD_RELOC_XTENSA_SLOT8_ALT:
5805 case BFD_RELOC_XTENSA_SLOT9_ALT:
5806 case BFD_RELOC_XTENSA_SLOT10_ALT:
5807 case BFD_RELOC_XTENSA_SLOT11_ALT:
5808 case BFD_RELOC_XTENSA_SLOT12_ALT:
5809 case BFD_RELOC_XTENSA_SLOT13_ALT:
5810 case BFD_RELOC_XTENSA_SLOT14_ALT:
5811 case BFD_RELOC_XTENSA_ASM_EXPAND:
5812 case BFD_RELOC_32:
5813 case BFD_RELOC_XTENSA_PLT:
5814 case BFD_RELOC_VTABLE_INHERIT:
5815 case BFD_RELOC_VTABLE_ENTRY:
5816 break;
5818 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
5819 as_warn (_("emitting simplification relocation"));
5820 break;
5822 default:
5823 as_warn (_("emitting unknown relocation"));
5827 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
5828 if (reloc->howto == NULL)
5830 as_bad_where (fixp->fx_file, fixp->fx_line,
5831 _("cannot represent `%s' relocation in object file"),
5832 bfd_get_reloc_code_name (fixp->fx_r_type));
5833 free (reloc->sym_ptr_ptr);
5834 free (reloc);
5835 return NULL;
5838 if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
5839 as_fatal (_("internal error? cannot generate `%s' relocation"),
5840 bfd_get_reloc_code_name (fixp->fx_r_type));
5842 /* Write the tentative value of a PC-relative relocation to a local symbol
5843 into the instruction. The value will be ignored by the linker, and it
5844 makes the object file disassembly readable when the linkrelax flag is
5845 set and all branch targets are encoded in relocations. */
5847 if (linkrelax && apply_tentative_value && fixp->fx_pcrel)
5849 valueT val;
5850 assert (fixp->fx_addsy);
5851 if (S_GET_SEGMENT (fixp->fx_addsy) == section && !fixp->fx_plt
5852 && !S_FORCE_RELOC (fixp->fx_addsy, 1))
5854 val = (S_GET_VALUE (fixp->fx_addsy) + fixp->fx_offset
5855 - md_pcrel_from (fixp));
5856 xg_apply_tentative_value (fixp, val);
5860 return reloc;
5864 /* Checks for resource conflicts between instructions. */
5866 /* The func unit stuff could be implemented as bit-vectors rather
5867 than the iterative approach here. If it ends up being too
5868 slow, we will switch it. */
5870 resource_table *
5871 new_resource_table (void *data,
5872 int cycles,
5873 int nu,
5874 unit_num_copies_func uncf,
5875 opcode_num_units_func onuf,
5876 opcode_funcUnit_use_unit_func ouuf,
5877 opcode_funcUnit_use_stage_func ousf)
5879 int i;
5880 resource_table *rt = (resource_table *) xmalloc (sizeof (resource_table));
5881 rt->data = data;
5882 rt->cycles = cycles;
5883 rt->allocated_cycles = cycles;
5884 rt->num_units = nu;
5885 rt->unit_num_copies = uncf;
5886 rt->opcode_num_units = onuf;
5887 rt->opcode_unit_use = ouuf;
5888 rt->opcode_unit_stage = ousf;
5890 rt->units = (char **) xcalloc (cycles, sizeof (char *));
5891 for (i = 0; i < cycles; i++)
5892 rt->units[i] = (char *) xcalloc (nu, sizeof (char));
5894 return rt;
5898 void
5899 clear_resource_table (resource_table *rt)
5901 int i, j;
5902 for (i = 0; i < rt->allocated_cycles; i++)
5903 for (j = 0; j < rt->num_units; j++)
5904 rt->units[i][j] = 0;
5908 /* We never shrink it, just fake it into thinking so. */
5910 void
5911 resize_resource_table (resource_table *rt, int cycles)
5913 int i, old_cycles;
5915 rt->cycles = cycles;
5916 if (cycles <= rt->allocated_cycles)
5917 return;
5919 old_cycles = rt->allocated_cycles;
5920 rt->allocated_cycles = cycles;
5922 rt->units = xrealloc (rt->units, sizeof (char *) * rt->allocated_cycles);
5923 for (i = 0; i < old_cycles; i++)
5924 rt->units[i] = xrealloc (rt->units[i], sizeof (char) * rt->num_units);
5925 for (i = old_cycles; i < cycles; i++)
5926 rt->units[i] = xcalloc (rt->num_units, sizeof (char));
5930 bfd_boolean
5931 resources_available (resource_table *rt, xtensa_opcode opcode, int cycle)
5933 int i;
5934 int uses = (rt->opcode_num_units) (rt->data, opcode);
5936 for (i = 0; i < uses; i++)
5938 xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
5939 int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
5940 int copies_in_use = rt->units[stage + cycle][unit];
5941 int copies = (rt->unit_num_copies) (rt->data, unit);
5942 if (copies_in_use >= copies)
5943 return FALSE;
5945 return TRUE;
5949 void
5950 reserve_resources (resource_table *rt, xtensa_opcode opcode, int cycle)
5952 int i;
5953 int uses = (rt->opcode_num_units) (rt->data, opcode);
5955 for (i = 0; i < uses; i++)
5957 xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
5958 int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
5959 /* Note that this allows resources to be oversubscribed. That's
5960 essential to the way the optional scheduler works.
5961 resources_available reports when a resource is over-subscribed,
5962 so it's easy to tell. */
5963 rt->units[stage + cycle][unit]++;
5968 void
5969 release_resources (resource_table *rt, xtensa_opcode opcode, int cycle)
5971 int i;
5972 int uses = (rt->opcode_num_units) (rt->data, opcode);
5974 for (i = 0; i < uses; i++)
5976 xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
5977 int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
5978 rt->units[stage + cycle][unit]--;
5979 assert (rt->units[stage + cycle][unit] >= 0);
5984 /* Wrapper functions make parameterized resource reservation
5985 more convenient. */
5987 int
5988 opcode_funcUnit_use_unit (void *data, xtensa_opcode opcode, int idx)
5990 xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx);
5991 return use->unit;
5995 int
5996 opcode_funcUnit_use_stage (void *data, xtensa_opcode opcode, int idx)
5998 xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx);
5999 return use->stage;
6003 /* Note that this function does not check issue constraints, but
6004 solely whether the hardware is available to execute the given
6005 instructions together. It also doesn't check if the tinsns
6006 write the same state, or access the same tieports. That is
6007 checked by check_t1_t2_reads_and_writes. */
6009 static bfd_boolean
6010 resources_conflict (vliw_insn *vinsn)
6012 int i;
6013 static resource_table *rt = NULL;
6015 /* This is the most common case by far. Optimize it. */
6016 if (vinsn->num_slots == 1)
6017 return FALSE;
6019 if (rt == NULL)
6021 xtensa_isa isa = xtensa_default_isa;
6022 rt = new_resource_table
6023 (isa, xtensa_isa_num_pipe_stages (isa),
6024 xtensa_isa_num_funcUnits (isa),
6025 (unit_num_copies_func) xtensa_funcUnit_num_copies,
6026 (opcode_num_units_func) xtensa_opcode_num_funcUnit_uses,
6027 opcode_funcUnit_use_unit,
6028 opcode_funcUnit_use_stage);
6031 clear_resource_table (rt);
6033 for (i = 0; i < vinsn->num_slots; i++)
6035 if (!resources_available (rt, vinsn->slots[i].opcode, 0))
6036 return TRUE;
6037 reserve_resources (rt, vinsn->slots[i].opcode, 0);
6040 return FALSE;
6044 /* finish_vinsn, emit_single_op and helper functions. */
6046 static bfd_boolean find_vinsn_conflicts (vliw_insn *);
6047 static xtensa_format xg_find_narrowest_format (vliw_insn *);
6048 static void bundle_single_op (TInsn *);
6049 static void xg_assemble_vliw_tokens (vliw_insn *);
6052 /* We have reached the end of a bundle; emit into the frag. */
6054 static void
6055 finish_vinsn (vliw_insn *vinsn)
6057 IStack slotstack;
6058 int i;
6059 char *file_name;
6060 int line;
6062 if (find_vinsn_conflicts (vinsn))
6064 xg_clear_vinsn (vinsn);
6065 return;
6068 /* First, find a format that works. */
6069 if (vinsn->format == XTENSA_UNDEFINED)
6070 vinsn->format = xg_find_narrowest_format (vinsn);
6072 if (vinsn->format == XTENSA_UNDEFINED)
6074 as_where (&file_name, &line);
6075 as_bad_where (file_name, line,
6076 _("couldn't find a valid instruction format"));
6077 fprintf (stderr, _(" ops were: "));
6078 for (i = 0; i < vinsn->num_slots; i++)
6079 fprintf (stderr, _(" %s;"),
6080 xtensa_opcode_name (xtensa_default_isa,
6081 vinsn->slots[i].opcode));
6082 fprintf (stderr, _("\n"));
6083 xg_clear_vinsn (vinsn);
6084 return;
6087 if (vinsn->num_slots
6088 != xtensa_format_num_slots (xtensa_default_isa, vinsn->format))
6090 as_bad (_("format '%s' allows %d slots, but there are %d opcodes"),
6091 xtensa_format_name (xtensa_default_isa, vinsn->format),
6092 xtensa_format_num_slots (xtensa_default_isa, vinsn->format),
6093 vinsn->num_slots);
6094 xg_clear_vinsn (vinsn);
6095 return;
6098 if (resources_conflict (vinsn))
6100 as_where (&file_name, &line);
6101 as_bad_where (file_name, line, _("illegal resource usage in bundle"));
6102 fprintf (stderr, " ops were: ");
6103 for (i = 0; i < vinsn->num_slots; i++)
6104 fprintf (stderr, " %s;",
6105 xtensa_opcode_name (xtensa_default_isa,
6106 vinsn->slots[i].opcode));
6107 fprintf (stderr, "\n");
6108 xg_clear_vinsn (vinsn);
6109 return;
6112 for (i = 0; i < vinsn->num_slots; i++)
6114 if (vinsn->slots[i].opcode != XTENSA_UNDEFINED)
6116 symbolS *lit_sym = NULL;
6117 int j;
6118 bfd_boolean e = FALSE;
6119 bfd_boolean saved_density = density_supported;
6121 /* We don't want to narrow ops inside multi-slot bundles. */
6122 if (vinsn->num_slots > 1)
6123 density_supported = FALSE;
6125 istack_init (&slotstack);
6126 if (vinsn->slots[i].opcode == xtensa_nop_opcode)
6128 vinsn->slots[i].opcode =
6129 xtensa_format_slot_nop_opcode (xtensa_default_isa,
6130 vinsn->format, i);
6131 vinsn->slots[i].ntok = 0;
6134 if (xg_expand_assembly_insn (&slotstack, &vinsn->slots[i]))
6136 e = TRUE;
6137 continue;
6140 density_supported = saved_density;
6142 if (e)
6144 xg_clear_vinsn (vinsn);
6145 return;
6148 for (j = 0; j < slotstack.ninsn - 1; j++)
6150 TInsn *insn = &slotstack.insn[j];
6151 if (insn->insn_type == ITYPE_LITERAL)
6153 assert (lit_sym == NULL);
6154 lit_sym = xg_assemble_literal (insn);
6156 else
6158 if (lit_sym)
6159 xg_resolve_literals (insn, lit_sym);
6160 emit_single_op (insn);
6164 if (vinsn->num_slots > 1)
6166 if (opcode_fits_format_slot
6167 (slotstack.insn[slotstack.ninsn - 1].opcode,
6168 vinsn->format, i))
6170 vinsn->slots[i] = slotstack.insn[slotstack.ninsn - 1];
6172 else
6174 bundle_single_op (&slotstack.insn[slotstack.ninsn - 1]);
6175 if (vinsn->format == XTENSA_UNDEFINED)
6176 vinsn->slots[i].opcode = xtensa_nop_opcode;
6177 else
6178 vinsn->slots[i].opcode
6179 = xtensa_format_slot_nop_opcode (xtensa_default_isa,
6180 vinsn->format, i);
6182 vinsn->slots[i].ntok = 0;
6185 else
6187 vinsn->slots[0] = slotstack.insn[slotstack.ninsn - 1];
6188 vinsn->format = XTENSA_UNDEFINED;
6193 /* Now check resource conflicts on the modified bundle. */
6194 if (resources_conflict (vinsn))
6196 as_where (&file_name, &line);
6197 as_bad_where (file_name, line, _("illegal resource usage in bundle"));
6198 fprintf (stderr, " ops were: ");
6199 for (i = 0; i < vinsn->num_slots; i++)
6200 fprintf (stderr, " %s;",
6201 xtensa_opcode_name (xtensa_default_isa,
6202 vinsn->slots[i].opcode));
6203 fprintf (stderr, "\n");
6204 xg_clear_vinsn (vinsn);
6205 return;
6208 /* First, find a format that works. */
6209 if (vinsn->format == XTENSA_UNDEFINED)
6210 vinsn->format = xg_find_narrowest_format (vinsn);
6212 xg_assemble_vliw_tokens (vinsn);
6214 xg_clear_vinsn (vinsn);
6218 /* Given an vliw instruction, what conflicts are there in register
6219 usage and in writes to states and queues?
6221 This function does two things:
6222 1. Reports an error when a vinsn contains illegal combinations
6223 of writes to registers states or queues.
6224 2. Marks individual tinsns as not relaxable if the combination
6225 contains antidependencies.
6227 Job 2 handles things like swap semantics in instructions that need
6228 to be relaxed. For example,
6230 addi a0, a1, 100000
6232 normally would be relaxed to
6234 l32r a0, some_label
6235 add a0, a1, a0
6237 _but_, if the above instruction is bundled with an a0 reader, e.g.,
6239 { addi a0, a1, 10000 ; add a2, a0, a4 ; }
6241 then we can't relax it into
6243 l32r a0, some_label
6244 { add a0, a1, a0 ; add a2, a0, a4 ; }
6246 because the value of a0 is trashed before the second add can read it. */
6248 static char check_t1_t2_reads_and_writes (TInsn *, TInsn *);
6250 static bfd_boolean
6251 find_vinsn_conflicts (vliw_insn *vinsn)
6253 int i, j;
6254 int branches = 0;
6255 xtensa_isa isa = xtensa_default_isa;
6257 assert (!past_xtensa_end);
6259 for (i = 0 ; i < vinsn->num_slots; i++)
6261 TInsn *op1 = &vinsn->slots[i];
6262 if (op1->is_specific_opcode)
6263 op1->keep_wide = TRUE;
6264 else
6265 op1->keep_wide = FALSE;
6268 for (i = 0 ; i < vinsn->num_slots; i++)
6270 TInsn *op1 = &vinsn->slots[i];
6272 if (xtensa_opcode_is_branch (isa, op1->opcode) == 1)
6273 branches++;
6275 for (j = 0; j < vinsn->num_slots; j++)
6277 if (i != j)
6279 TInsn *op2 = &vinsn->slots[j];
6280 char conflict_type = check_t1_t2_reads_and_writes (op1, op2);
6281 switch (conflict_type)
6283 case 'c':
6284 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same register"),
6285 xtensa_opcode_name (isa, op1->opcode), i,
6286 xtensa_opcode_name (isa, op2->opcode), j);
6287 return TRUE;
6288 case 'd':
6289 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same state"),
6290 xtensa_opcode_name (isa, op1->opcode), i,
6291 xtensa_opcode_name (isa, op2->opcode), j);
6292 return TRUE;
6293 case 'e':
6294 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same queue"),
6295 xtensa_opcode_name (isa, op1->opcode), i,
6296 xtensa_opcode_name (isa, op2->opcode), j);
6297 return TRUE;
6298 case 'f':
6299 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) both have volatile queue accesses"),
6300 xtensa_opcode_name (isa, op1->opcode), i,
6301 xtensa_opcode_name (isa, op2->opcode), j);
6302 return TRUE;
6303 default:
6304 /* Everything is OK. */
6305 break;
6307 op2->is_specific_opcode = (op2->is_specific_opcode
6308 || conflict_type == 'a');
6313 if (branches > 1)
6315 as_bad (_("multiple branches or jumps in the same bundle"));
6316 return TRUE;
6319 return FALSE;
6323 /* Check how the state used by t1 and t2 relate.
6324 Cases found are:
6326 case A: t1 reads a register t2 writes (an antidependency within a bundle)
6327 case B: no relationship between what is read and written (both could
6328 read the same reg though)
6329 case C: t1 writes a register t2 writes (a register conflict within a
6330 bundle)
6331 case D: t1 writes a state that t2 also writes
6332 case E: t1 writes a tie queue that t2 also writes
6333 case F: two volatile queue accesses
6336 static char
6337 check_t1_t2_reads_and_writes (TInsn *t1, TInsn *t2)
6339 xtensa_isa isa = xtensa_default_isa;
6340 xtensa_regfile t1_regfile, t2_regfile;
6341 int t1_reg, t2_reg;
6342 int t1_base_reg, t1_last_reg;
6343 int t2_base_reg, t2_last_reg;
6344 char t1_inout, t2_inout;
6345 int i, j;
6346 char conflict = 'b';
6347 int t1_states;
6348 int t2_states;
6349 int t1_interfaces;
6350 int t2_interfaces;
6351 bfd_boolean t1_volatile = FALSE;
6352 bfd_boolean t2_volatile = FALSE;
6354 /* Check registers. */
6355 for (j = 0; j < t2->ntok; j++)
6357 if (xtensa_operand_is_register (isa, t2->opcode, j) != 1)
6358 continue;
6360 t2_regfile = xtensa_operand_regfile (isa, t2->opcode, j);
6361 t2_base_reg = t2->tok[j].X_add_number;
6362 t2_last_reg = t2_base_reg + xtensa_operand_num_regs (isa, t2->opcode, j);
6364 for (i = 0; i < t1->ntok; i++)
6366 if (xtensa_operand_is_register (isa, t1->opcode, i) != 1)
6367 continue;
6369 t1_regfile = xtensa_operand_regfile (isa, t1->opcode, i);
6371 if (t1_regfile != t2_regfile)
6372 continue;
6374 t1_inout = xtensa_operand_inout (isa, t1->opcode, i);
6375 t2_inout = xtensa_operand_inout (isa, t2->opcode, j);
6377 if (xtensa_operand_is_known_reg (isa, t1->opcode, i) == 0
6378 || xtensa_operand_is_known_reg (isa, t2->opcode, j) == 0)
6380 if (t1_inout == 'm' || t1_inout == 'o'
6381 || t2_inout == 'm' || t2_inout == 'o')
6383 conflict = 'a';
6384 continue;
6388 t1_base_reg = t1->tok[i].X_add_number;
6389 t1_last_reg = (t1_base_reg
6390 + xtensa_operand_num_regs (isa, t1->opcode, i));
6392 for (t1_reg = t1_base_reg; t1_reg < t1_last_reg; t1_reg++)
6394 for (t2_reg = t2_base_reg; t2_reg < t2_last_reg; t2_reg++)
6396 if (t1_reg != t2_reg)
6397 continue;
6399 if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o'))
6401 conflict = 'a';
6402 continue;
6405 if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o'))
6407 conflict = 'a';
6408 continue;
6411 if (t1_inout != 'i' && t2_inout != 'i')
6412 return 'c';
6418 /* Check states. */
6419 t1_states = xtensa_opcode_num_stateOperands (isa, t1->opcode);
6420 t2_states = xtensa_opcode_num_stateOperands (isa, t2->opcode);
6421 for (j = 0; j < t2_states; j++)
6423 xtensa_state t2_so = xtensa_stateOperand_state (isa, t2->opcode, j);
6424 t2_inout = xtensa_stateOperand_inout (isa, t2->opcode, j);
6425 for (i = 0; i < t1_states; i++)
6427 xtensa_state t1_so = xtensa_stateOperand_state (isa, t1->opcode, i);
6428 t1_inout = xtensa_stateOperand_inout (isa, t1->opcode, i);
6429 if (t1_so != t2_so)
6430 continue;
6432 if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o'))
6434 conflict = 'a';
6435 continue;
6438 if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o'))
6440 conflict = 'a';
6441 continue;
6444 if (t1_inout != 'i' && t2_inout != 'i')
6445 return 'd';
6449 /* Check tieports. */
6450 t1_interfaces = xtensa_opcode_num_interfaceOperands (isa, t1->opcode);
6451 t2_interfaces = xtensa_opcode_num_interfaceOperands (isa, t2->opcode);
6452 for (j = 0; j < t2_interfaces; j++)
6454 xtensa_interface t2_int
6455 = xtensa_interfaceOperand_interface (isa, t2->opcode, j);
6456 int t2_class = xtensa_interface_class_id (isa, t2_int);
6458 t2_inout = xtensa_interface_inout (isa, j);
6459 if (xtensa_interface_has_side_effect (isa, t2_int) == 1)
6460 t2_volatile = TRUE;
6462 for (i = 0; i < t1_interfaces; i++)
6464 xtensa_interface t1_int
6465 = xtensa_interfaceOperand_interface (isa, t1->opcode, j);
6466 int t1_class = xtensa_interface_class_id (isa, t2_int);
6468 t1_inout = xtensa_interface_inout (isa, i);
6469 if (xtensa_interface_has_side_effect (isa, t1_int) == 1)
6470 t1_volatile = TRUE;
6472 if (t1_volatile && t2_volatile && (t1_class == t2_class))
6473 return 'f';
6475 if (t1_int != t2_int)
6476 continue;
6478 if (t2_inout == 'i' && t1_inout == 'o')
6480 conflict = 'a';
6481 continue;
6484 if (t1_inout == 'i' && t2_inout == 'o')
6486 conflict = 'a';
6487 continue;
6490 if (t1_inout != 'i' && t2_inout != 'i')
6491 return 'e';
6495 return conflict;
6499 static xtensa_format
6500 xg_find_narrowest_format (vliw_insn *vinsn)
6502 /* Right now we assume that the ops within the vinsn are properly
6503 ordered for the slots that the programmer wanted them in. In
6504 other words, we don't rearrange the ops in hopes of finding a
6505 better format. The scheduler handles that. */
6507 xtensa_isa isa = xtensa_default_isa;
6508 xtensa_format format;
6509 vliw_insn v_copy = *vinsn;
6510 xtensa_opcode nop_opcode = xtensa_nop_opcode;
6512 for (format = 0; format < xtensa_isa_num_formats (isa); format++)
6514 v_copy = *vinsn;
6515 if (xtensa_format_num_slots (isa, format) == v_copy.num_slots)
6517 int slot;
6518 int fit = 0;
6519 for (slot = 0; slot < v_copy.num_slots; slot++)
6521 if (v_copy.slots[slot].opcode == nop_opcode)
6523 v_copy.slots[slot].opcode =
6524 xtensa_format_slot_nop_opcode (isa, format, slot);
6525 v_copy.slots[slot].ntok = 0;
6528 if (opcode_fits_format_slot (v_copy.slots[slot].opcode,
6529 format, slot))
6530 fit++;
6531 else if (v_copy.num_slots > 1)
6533 TInsn widened;
6534 /* Try the widened version. */
6535 if (!v_copy.slots[slot].keep_wide
6536 && !v_copy.slots[slot].is_specific_opcode
6537 && xg_is_narrow_insn (&v_copy.slots[slot])
6538 && !xg_expand_narrow (&widened, &v_copy.slots[slot])
6539 && opcode_fits_format_slot (widened.opcode,
6540 format, slot))
6542 /* The xg_is_narrow clause requires some explanation:
6544 addi can be "widened" to an addmi, which is then
6545 expanded to an addmi/addi pair if the immediate
6546 requires it, but here we must have a single widen
6547 only.
6549 xg_is_narrow tells us that addi isn't really
6550 narrow. The widen_spec_list says that there are
6551 other cases. */
6553 v_copy.slots[slot] = widened;
6554 fit++;
6558 if (fit == v_copy.num_slots)
6560 *vinsn = v_copy;
6561 xtensa_format_encode (isa, format, vinsn->insnbuf);
6562 vinsn->format = format;
6563 break;
6568 if (format == xtensa_isa_num_formats (isa))
6569 return XTENSA_UNDEFINED;
6571 return format;
6575 /* Return the additional space needed in a frag
6576 for possible relaxations of any ops in a VLIW insn.
6577 Also fill out the relaxations that might be required of
6578 each tinsn in the vinsn. */
6580 static int
6581 relaxation_requirements (vliw_insn *vinsn)
6583 int extra_space = 0;
6584 int slot;
6586 for (slot = 0; slot < vinsn->num_slots; slot++)
6588 TInsn *tinsn = &vinsn->slots[slot];
6589 if (!tinsn_has_symbolic_operands (tinsn))
6591 /* A narrow instruction could be widened later to help
6592 alignment issues. */
6593 if (xg_is_narrow_insn (tinsn)
6594 && !tinsn->is_specific_opcode
6595 && vinsn->num_slots == 1)
6597 /* Difference in bytes between narrow and wide insns... */
6598 extra_space += 1;
6599 tinsn->subtype = RELAX_NARROW;
6600 tinsn->record_fix = TRUE;
6601 break;
6603 else
6605 tinsn->record_fix = FALSE;
6606 /* No extra_space needed. */
6609 else
6611 if (workaround_b_j_loop_end
6612 && tinsn->opcode == xtensa_jx_opcode
6613 && use_transform ())
6615 /* Add 2 of these. */
6616 extra_space += 3; /* for the nop size */
6617 tinsn->subtype = RELAX_ADD_NOP_IF_PRE_LOOP_END;
6620 /* Need to assemble it with space for the relocation. */
6621 if (xg_is_relaxable_insn (tinsn, 0)
6622 && !tinsn->is_specific_opcode)
6624 int max_size = xg_get_max_insn_widen_size (tinsn->opcode);
6625 int max_literal_size =
6626 xg_get_max_insn_widen_literal_size (tinsn->opcode);
6628 tinsn->literal_space = max_literal_size;
6630 tinsn->subtype = RELAX_IMMED;
6631 tinsn->record_fix = FALSE;
6632 extra_space += max_size;
6634 else
6636 tinsn->record_fix = TRUE;
6637 /* No extra space needed. */
6641 return extra_space;
6645 static void
6646 bundle_single_op (TInsn *orig_insn)
6648 xtensa_isa isa = xtensa_default_isa;
6649 vliw_insn v;
6650 int slot;
6652 xg_init_vinsn (&v);
6653 v.format = op_placement_table[orig_insn->opcode].narrowest;
6654 assert (v.format != XTENSA_UNDEFINED);
6655 v.num_slots = xtensa_format_num_slots (isa, v.format);
6657 for (slot = 0;
6658 !opcode_fits_format_slot (orig_insn->opcode, v.format, slot);
6659 slot++)
6661 v.slots[slot].opcode =
6662 xtensa_format_slot_nop_opcode (isa, v.format, slot);
6663 v.slots[slot].ntok = 0;
6664 v.slots[slot].insn_type = ITYPE_INSN;
6667 v.slots[slot] = *orig_insn;
6668 slot++;
6670 for ( ; slot < v.num_slots; slot++)
6672 v.slots[slot].opcode =
6673 xtensa_format_slot_nop_opcode (isa, v.format, slot);
6674 v.slots[slot].ntok = 0;
6675 v.slots[slot].insn_type = ITYPE_INSN;
6678 finish_vinsn (&v);
6679 xg_free_vinsn (&v);
6683 static bfd_boolean
6684 emit_single_op (TInsn *orig_insn)
6686 int i;
6687 IStack istack; /* put instructions into here */
6688 symbolS *lit_sym = NULL;
6689 symbolS *label_sym = NULL;
6691 istack_init (&istack);
6693 /* Special-case for "movi aX, foo" which is guaranteed to need relaxing.
6694 Because the scheduling and bundling characteristics of movi and
6695 l32r or const16 are so different, we can do much better if we relax
6696 it prior to scheduling and bundling, rather than after. */
6697 if ((orig_insn->opcode == xtensa_movi_opcode
6698 || orig_insn->opcode == xtensa_movi_n_opcode)
6699 && !cur_vinsn.inside_bundle
6700 && (orig_insn->tok[1].X_op == O_symbol
6701 || orig_insn->tok[1].X_op == O_pltrel))
6702 xg_assembly_relax (&istack, orig_insn, now_seg, frag_now, 0, 1, 0);
6703 else
6704 if (xg_expand_assembly_insn (&istack, orig_insn))
6705 return TRUE;
6707 for (i = 0; i < istack.ninsn; i++)
6709 TInsn *insn = &istack.insn[i];
6710 switch (insn->insn_type)
6712 case ITYPE_LITERAL:
6713 assert (lit_sym == NULL);
6714 lit_sym = xg_assemble_literal (insn);
6715 break;
6716 case ITYPE_LABEL:
6718 static int relaxed_sym_idx = 0;
6719 char *label = xmalloc (strlen (FAKE_LABEL_NAME) + 12);
6720 sprintf (label, "%s_rl_%x", FAKE_LABEL_NAME, relaxed_sym_idx++);
6721 colon (label);
6722 assert (label_sym == NULL);
6723 label_sym = symbol_find_or_make (label);
6724 assert (label_sym);
6725 free (label);
6727 break;
6728 case ITYPE_INSN:
6729 if (lit_sym)
6730 xg_resolve_literals (insn, lit_sym);
6731 if (label_sym)
6732 xg_resolve_labels (insn, label_sym);
6733 bundle_single_op (insn);
6734 break;
6735 default:
6736 assert (0);
6737 break;
6740 return FALSE;
6744 static int
6745 total_frag_text_expansion (fragS *fragP)
6747 int slot;
6748 int total_expansion = 0;
6750 for (slot = 0; slot < MAX_SLOTS; slot++)
6751 total_expansion += fragP->tc_frag_data.text_expansion[slot];
6753 return total_expansion;
6757 /* Emit a vliw instruction to the current fragment. */
6759 static void
6760 xg_assemble_vliw_tokens (vliw_insn *vinsn)
6762 bfd_boolean finish_frag = FALSE;
6763 bfd_boolean is_jump = FALSE;
6764 bfd_boolean is_branch = FALSE;
6765 xtensa_isa isa = xtensa_default_isa;
6766 int i;
6767 int insn_size;
6768 int extra_space;
6769 char *f = NULL;
6770 int slot;
6771 struct dwarf2_line_info best_loc;
6773 best_loc.line = INT_MAX;
6775 if (generating_literals)
6777 static int reported = 0;
6778 if (reported < 4)
6779 as_bad_where (frag_now->fr_file, frag_now->fr_line,
6780 _("cannot assemble into a literal fragment"));
6781 if (reported == 3)
6782 as_bad (_("..."));
6783 reported++;
6784 return;
6787 if (frag_now_fix () != 0
6788 && (! frag_now->tc_frag_data.is_insn
6789 || (vinsn_has_specific_opcodes (vinsn) && use_transform ())
6790 || !use_transform () != frag_now->tc_frag_data.is_no_transform
6791 || (directive_state[directive_absolute_literals]
6792 != frag_now->tc_frag_data.use_absolute_literals)))
6794 frag_wane (frag_now);
6795 frag_new (0);
6796 xtensa_set_frag_assembly_state (frag_now);
6799 if (workaround_a0_b_retw
6800 && vinsn->num_slots == 1
6801 && (get_last_insn_flags (now_seg, now_subseg) & FLAG_IS_A0_WRITER) != 0
6802 && xtensa_opcode_is_branch (isa, vinsn->slots[0].opcode) == 1
6803 && use_transform ())
6805 has_a0_b_retw = TRUE;
6807 /* Mark this fragment with the special RELAX_ADD_NOP_IF_A0_B_RETW.
6808 After the first assembly pass we will check all of them and
6809 add a nop if needed. */
6810 frag_now->tc_frag_data.is_insn = TRUE;
6811 frag_var (rs_machine_dependent, 4, 4,
6812 RELAX_ADD_NOP_IF_A0_B_RETW,
6813 frag_now->fr_symbol,
6814 frag_now->fr_offset,
6815 NULL);
6816 xtensa_set_frag_assembly_state (frag_now);
6817 frag_now->tc_frag_data.is_insn = TRUE;
6818 frag_var (rs_machine_dependent, 4, 4,
6819 RELAX_ADD_NOP_IF_A0_B_RETW,
6820 frag_now->fr_symbol,
6821 frag_now->fr_offset,
6822 NULL);
6823 xtensa_set_frag_assembly_state (frag_now);
6826 for (i = 0; i < vinsn->num_slots; i++)
6828 /* See if the instruction implies an aligned section. */
6829 if (xtensa_opcode_is_loop (isa, vinsn->slots[i].opcode) == 1)
6830 record_alignment (now_seg, 2);
6832 /* Also determine the best line number for debug info. */
6833 best_loc = vinsn->slots[i].loc.line < best_loc.line
6834 ? vinsn->slots[i].loc : best_loc;
6837 /* Special cases for instructions that force an alignment... */
6838 /* None of these opcodes are bundle-able. */
6839 if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode) == 1)
6841 size_t max_fill;
6843 xtensa_set_frag_assembly_state (frag_now);
6844 frag_now->tc_frag_data.is_insn = TRUE;
6846 max_fill = get_text_align_max_fill_size
6847 (get_text_align_power (xtensa_fetch_width),
6848 TRUE, frag_now->tc_frag_data.is_no_density);
6850 if (use_transform ())
6851 frag_var (rs_machine_dependent, max_fill, max_fill,
6852 RELAX_ALIGN_NEXT_OPCODE,
6853 frag_now->fr_symbol,
6854 frag_now->fr_offset,
6855 NULL);
6856 else
6857 frag_var (rs_machine_dependent, 0, 0,
6858 RELAX_CHECK_ALIGN_NEXT_OPCODE, 0, 0, NULL);
6859 xtensa_set_frag_assembly_state (frag_now);
6861 xtensa_move_labels (frag_now, 0, FALSE);
6864 if (vinsn->slots[0].opcode == xtensa_entry_opcode
6865 && !vinsn->slots[0].is_specific_opcode)
6867 xtensa_mark_literal_pool_location ();
6868 xtensa_move_labels (frag_now, 0, TRUE);
6869 frag_var (rs_align_test, 1, 1, 0, NULL, 2, NULL);
6872 if (vinsn->num_slots == 1)
6874 if (workaround_a0_b_retw && use_transform ())
6875 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_A0_WRITER,
6876 is_register_writer (&vinsn->slots[0], "a", 0));
6878 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND,
6879 is_bad_loopend_opcode (&vinsn->slots[0]));
6881 else
6882 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND, FALSE);
6884 insn_size = xtensa_format_length (isa, vinsn->format);
6886 extra_space = relaxation_requirements (vinsn);
6888 /* vinsn_to_insnbuf will produce the error. */
6889 if (vinsn->format != XTENSA_UNDEFINED)
6891 f = (char *) frag_more (insn_size + extra_space);
6892 xtensa_set_frag_assembly_state (frag_now);
6893 frag_now->tc_frag_data.is_insn = TRUE;
6896 vinsn_to_insnbuf (vinsn, f, frag_now, TRUE);
6897 if (vinsn->format == XTENSA_UNDEFINED)
6898 return;
6900 xtensa_insnbuf_to_chars (isa, vinsn->insnbuf, f, 0);
6902 xtensa_dwarf2_emit_insn (insn_size - extra_space, &best_loc);
6904 for (slot = 0; slot < vinsn->num_slots; slot++)
6906 TInsn *tinsn = &vinsn->slots[slot];
6907 frag_now->tc_frag_data.slot_subtypes[slot] = tinsn->subtype;
6908 frag_now->tc_frag_data.slot_symbols[slot] = tinsn->symbol;
6909 frag_now->tc_frag_data.slot_sub_symbols[slot] = tinsn->sub_symbol;
6910 frag_now->tc_frag_data.slot_offsets[slot] = tinsn->offset;
6911 frag_now->tc_frag_data.literal_frags[slot] = tinsn->literal_frag;
6912 if (tinsn->literal_space != 0)
6913 xg_assemble_literal_space (tinsn->literal_space, slot);
6915 if (tinsn->subtype == RELAX_NARROW)
6916 assert (vinsn->num_slots == 1);
6917 if (xtensa_opcode_is_jump (isa, tinsn->opcode) == 1)
6918 is_jump = TRUE;
6919 if (xtensa_opcode_is_branch (isa, tinsn->opcode) == 1)
6920 is_branch = TRUE;
6922 if (tinsn->subtype || tinsn->symbol || tinsn->record_fix
6923 || tinsn->offset || tinsn->literal_frag || is_jump || is_branch)
6924 finish_frag = TRUE;
6927 if (vinsn_has_specific_opcodes (vinsn) && use_transform ())
6928 frag_now->tc_frag_data.is_specific_opcode = TRUE;
6930 if (finish_frag)
6932 frag_variant (rs_machine_dependent,
6933 extra_space, extra_space, RELAX_SLOTS,
6934 frag_now->fr_symbol, frag_now->fr_offset, f);
6935 xtensa_set_frag_assembly_state (frag_now);
6938 /* Special cases for loops:
6939 close_loop_end should be inserted AFTER short_loop.
6940 Make sure that CLOSE loops are processed BEFORE short_loops
6941 when converting them. */
6943 /* "short_loop": Add a NOP if the loop is < 4 bytes. */
6944 if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode)
6945 && !vinsn->slots[0].is_specific_opcode)
6947 if (workaround_short_loop && use_transform ())
6949 maybe_has_short_loop = TRUE;
6950 frag_now->tc_frag_data.is_insn = TRUE;
6951 frag_var (rs_machine_dependent, 4, 4,
6952 RELAX_ADD_NOP_IF_SHORT_LOOP,
6953 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6954 frag_now->tc_frag_data.is_insn = TRUE;
6955 frag_var (rs_machine_dependent, 4, 4,
6956 RELAX_ADD_NOP_IF_SHORT_LOOP,
6957 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6960 /* "close_loop_end": Add up to 12 bytes of NOPs to keep a
6961 loop at least 12 bytes away from another loop's end. */
6962 if (workaround_close_loop_end && use_transform ())
6964 maybe_has_close_loop_end = TRUE;
6965 frag_now->tc_frag_data.is_insn = TRUE;
6966 frag_var (rs_machine_dependent, 12, 12,
6967 RELAX_ADD_NOP_IF_CLOSE_LOOP_END,
6968 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6972 if (use_transform ())
6974 if (is_jump)
6976 assert (finish_frag);
6977 frag_var (rs_machine_dependent,
6978 UNREACHABLE_MAX_WIDTH, UNREACHABLE_MAX_WIDTH,
6979 RELAX_UNREACHABLE,
6980 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6981 xtensa_set_frag_assembly_state (frag_now);
6983 else if (is_branch && align_targets)
6985 assert (finish_frag);
6986 frag_var (rs_machine_dependent,
6987 UNREACHABLE_MAX_WIDTH, UNREACHABLE_MAX_WIDTH,
6988 RELAX_MAYBE_UNREACHABLE,
6989 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6990 xtensa_set_frag_assembly_state (frag_now);
6991 frag_var (rs_machine_dependent,
6992 0, 0,
6993 RELAX_MAYBE_DESIRE_ALIGN,
6994 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6995 xtensa_set_frag_assembly_state (frag_now);
6999 /* Now, if the original opcode was a call... */
7000 if (do_align_targets ()
7001 && xtensa_opcode_is_call (isa, vinsn->slots[0].opcode) == 1)
7003 float freq = get_subseg_total_freq (now_seg, now_subseg);
7004 frag_now->tc_frag_data.is_insn = TRUE;
7005 frag_var (rs_machine_dependent, 4, (int) freq, RELAX_DESIRE_ALIGN,
7006 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7007 xtensa_set_frag_assembly_state (frag_now);
7010 if (vinsn_has_specific_opcodes (vinsn) && use_transform ())
7012 frag_wane (frag_now);
7013 frag_new (0);
7014 xtensa_set_frag_assembly_state (frag_now);
7019 /* xtensa_end and helper functions. */
7021 static void xtensa_cleanup_align_frags (void);
7022 static void xtensa_fix_target_frags (void);
7023 static void xtensa_mark_narrow_branches (void);
7024 static void xtensa_mark_zcl_first_insns (void);
7025 static void xtensa_fix_a0_b_retw_frags (void);
7026 static void xtensa_fix_b_j_loop_end_frags (void);
7027 static void xtensa_fix_close_loop_end_frags (void);
7028 static void xtensa_fix_short_loop_frags (void);
7029 static void xtensa_sanity_check (void);
7031 void
7032 xtensa_end (void)
7034 directive_balance ();
7035 xtensa_flush_pending_output ();
7037 past_xtensa_end = TRUE;
7039 xtensa_move_literals ();
7041 xtensa_reorder_segments ();
7042 xtensa_cleanup_align_frags ();
7043 xtensa_fix_target_frags ();
7044 if (workaround_a0_b_retw && has_a0_b_retw)
7045 xtensa_fix_a0_b_retw_frags ();
7046 if (workaround_b_j_loop_end)
7047 xtensa_fix_b_j_loop_end_frags ();
7049 /* "close_loop_end" should be processed BEFORE "short_loop". */
7050 if (workaround_close_loop_end && maybe_has_close_loop_end)
7051 xtensa_fix_close_loop_end_frags ();
7053 if (workaround_short_loop && maybe_has_short_loop)
7054 xtensa_fix_short_loop_frags ();
7055 xtensa_mark_narrow_branches ();
7056 xtensa_mark_zcl_first_insns ();
7058 xtensa_sanity_check ();
7062 static void
7063 xtensa_cleanup_align_frags (void)
7065 frchainS *frchP;
7067 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7069 fragS *fragP;
7070 /* Walk over all of the fragments in a subsection. */
7071 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7073 if ((fragP->fr_type == rs_align
7074 || fragP->fr_type == rs_align_code
7075 || (fragP->fr_type == rs_machine_dependent
7076 && (fragP->fr_subtype == RELAX_DESIRE_ALIGN
7077 || fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)))
7078 && fragP->fr_fix == 0)
7080 fragS *next = fragP->fr_next;
7082 while (next
7083 && next->fr_fix == 0
7084 && next->fr_type == rs_machine_dependent
7085 && next->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
7087 frag_wane (next);
7088 next = next->fr_next;
7091 /* If we don't widen branch targets, then they
7092 will be easier to align. */
7093 if (fragP->tc_frag_data.is_branch_target
7094 && fragP->fr_opcode == fragP->fr_literal
7095 && fragP->fr_type == rs_machine_dependent
7096 && fragP->fr_subtype == RELAX_SLOTS
7097 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
7098 frag_wane (fragP);
7099 if (fragP->fr_type == rs_machine_dependent
7100 && fragP->fr_subtype == RELAX_UNREACHABLE)
7101 fragP->tc_frag_data.is_unreachable = TRUE;
7107 /* Re-process all of the fragments looking to convert all of the
7108 RELAX_DESIRE_ALIGN_IF_TARGET fragments. If there is a branch
7109 target in the next fragment, convert this to RELAX_DESIRE_ALIGN.
7110 If the next fragment starts with a loop target, AND the previous
7111 fragment can be expanded to negate the branch, convert this to a
7112 RELAX_LOOP_END. Otherwise, convert to a .fill 0. */
7114 static bfd_boolean frag_can_negate_branch (fragS *);
7116 static void
7117 xtensa_fix_target_frags (void)
7119 frchainS *frchP;
7121 /* When this routine is called, all of the subsections are still intact
7122 so we walk over subsections instead of sections. */
7123 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7125 bfd_boolean prev_frag_can_negate_branch = FALSE;
7126 fragS *fragP;
7128 /* Walk over all of the fragments in a subsection. */
7129 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7131 if (fragP->fr_type == rs_machine_dependent
7132 && fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
7134 if (next_frag_is_loop_target (fragP))
7136 if (prev_frag_can_negate_branch)
7138 fragP->fr_subtype = RELAX_LOOP_END;
7139 /* See the comment near the frag_var with a
7140 RELAX_DESIRE_ALIGN to see why we do this. */
7141 fragP->fr_var = RELAX_LOOP_END_BYTES;
7143 else
7145 if (next_frag_is_branch_target (fragP))
7146 fragP->fr_subtype = RELAX_DESIRE_ALIGN;
7147 else
7148 frag_wane (fragP);
7151 else if (next_frag_is_branch_target (fragP))
7152 fragP->fr_subtype = RELAX_DESIRE_ALIGN;
7153 else
7154 frag_wane (fragP);
7156 if (fragP->fr_fix != 0)
7157 prev_frag_can_negate_branch = FALSE;
7158 if (frag_can_negate_branch (fragP))
7159 prev_frag_can_negate_branch = TRUE;
7165 static bfd_boolean
7166 frag_can_negate_branch (fragS *fragP)
7168 xtensa_isa isa = xtensa_default_isa;
7169 vliw_insn vinsn;
7170 int slot;
7172 if (fragP->fr_type != rs_machine_dependent
7173 || fragP->fr_subtype != RELAX_SLOTS)
7174 return FALSE;
7176 vinsn_from_chars (&vinsn, fragP->fr_opcode);
7178 for (slot = 0; slot < xtensa_format_num_slots (isa, vinsn.format); slot++)
7180 if ((fragP->tc_frag_data.slot_subtypes[slot] == RELAX_IMMED)
7181 && xtensa_opcode_is_branch (isa, vinsn.slots[slot].opcode) == 1)
7182 return TRUE;
7185 return FALSE;
7189 static bfd_boolean is_narrow_branch_guaranteed_in_range (fragS *, TInsn *);
7191 static void
7192 xtensa_mark_narrow_branches (void)
7194 frchainS *frchP;
7196 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7198 fragS *fragP;
7199 /* Walk over all of the fragments in a subsection. */
7200 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7202 if (fragP->fr_type == rs_machine_dependent
7203 && fragP->fr_subtype == RELAX_SLOTS
7204 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED)
7206 vliw_insn vinsn;
7207 const expressionS *expr;
7208 symbolS *symbolP;
7210 vinsn_from_chars (&vinsn, fragP->fr_opcode);
7211 tinsn_immed_from_frag (&vinsn.slots[0], fragP, 0);
7213 expr = &vinsn.slots[0].tok[1];
7214 symbolP = expr->X_add_symbol;
7216 if (vinsn.num_slots == 1
7217 && xtensa_opcode_is_branch (xtensa_default_isa,
7218 vinsn.slots[0].opcode)
7219 && xg_get_single_size (vinsn.slots[0].opcode) == 2
7220 && is_narrow_branch_guaranteed_in_range (fragP,
7221 &vinsn.slots[0]))
7223 fragP->fr_subtype = RELAX_SLOTS;
7224 fragP->tc_frag_data.slot_subtypes[0] = RELAX_NARROW;
7232 /* A branch is typically widened only when its target is out of
7233 range. However, we would like to widen them to align a subsequent
7234 branch target when possible.
7236 Because the branch relaxation code is so convoluted, the optimal solution
7237 (combining the two cases) is difficult to get right in all circumstances.
7238 We therefore go with an "almost as good" solution, where we only
7239 use for alignment narrow branches that definitely will not expand to a
7240 jump and a branch. These functions find and mark these cases. */
7242 /* The range in bytes of BNEZ.N and BEQZ.N. The target operand is encoded
7243 as PC + 4 + imm6, where imm6 is a 6-bit immediate ranging from 0 to 63.
7244 We start counting beginning with the frag after the 2-byte branch, so the
7245 maximum offset is (4 - 2) + 63 = 65. */
7246 #define MAX_IMMED6 65
7248 static size_t unrelaxed_frag_max_size (fragS *);
7250 static bfd_boolean
7251 is_narrow_branch_guaranteed_in_range (fragS *fragP, TInsn *tinsn)
7253 const expressionS *expr = &tinsn->tok[1];
7254 symbolS *symbolP = expr->X_add_symbol;
7255 fragS *target_frag = symbol_get_frag (symbolP);
7256 size_t max_distance = expr->X_add_number;
7257 max_distance += (S_GET_VALUE (symbolP) - target_frag->fr_address);
7258 if (is_branch_jmp_to_next (tinsn, fragP))
7259 return FALSE;
7261 /* The branch doesn't branch over it's own frag,
7262 but over the subsequent ones. */
7263 fragP = fragP->fr_next;
7264 while (fragP != NULL && fragP != target_frag && max_distance <= MAX_IMMED6)
7266 max_distance += unrelaxed_frag_max_size (fragP);
7267 fragP = fragP->fr_next;
7269 if (max_distance <= MAX_IMMED6 && fragP == target_frag)
7270 return TRUE;
7271 return FALSE;
7275 static void
7276 xtensa_mark_zcl_first_insns (void)
7278 frchainS *frchP;
7280 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7282 fragS *fragP;
7283 /* Walk over all of the fragments in a subsection. */
7284 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7286 if (fragP->fr_type == rs_machine_dependent
7287 && (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE
7288 || fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE))
7290 /* Find the loop frag. */
7291 fragS *targ_frag = next_non_empty_frag (fragP);
7292 /* Find the first insn frag. */
7293 targ_frag = next_non_empty_frag (targ_frag);
7295 /* Of course, sometimes (mostly for toy test cases) a
7296 zero-cost loop instruction is the last in a section. */
7297 if (targ_frag)
7299 targ_frag->tc_frag_data.is_first_loop_insn = TRUE;
7300 if (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)
7301 frag_wane (fragP);
7309 /* Re-process all of the fragments looking to convert all of the
7310 RELAX_ADD_NOP_IF_A0_B_RETW. If the next instruction is a
7311 conditional branch or a retw/retw.n, convert this frag to one that
7312 will generate a NOP. In any case close it off with a .fill 0. */
7314 static bfd_boolean next_instrs_are_b_retw (fragS *);
7316 static void
7317 xtensa_fix_a0_b_retw_frags (void)
7319 frchainS *frchP;
7321 /* When this routine is called, all of the subsections are still intact
7322 so we walk over subsections instead of sections. */
7323 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7325 fragS *fragP;
7327 /* Walk over all of the fragments in a subsection. */
7328 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7330 if (fragP->fr_type == rs_machine_dependent
7331 && fragP->fr_subtype == RELAX_ADD_NOP_IF_A0_B_RETW)
7333 if (next_instrs_are_b_retw (fragP))
7335 if (fragP->tc_frag_data.is_no_transform)
7336 as_bad (_("instruction sequence (write a0, branch, retw) may trigger hardware errata"));
7337 else
7338 relax_frag_add_nop (fragP);
7340 frag_wane (fragP);
7347 static bfd_boolean
7348 next_instrs_are_b_retw (fragS *fragP)
7350 xtensa_opcode opcode;
7351 xtensa_format fmt;
7352 const fragS *next_fragP = next_non_empty_frag (fragP);
7353 static xtensa_insnbuf insnbuf = NULL;
7354 static xtensa_insnbuf slotbuf = NULL;
7355 xtensa_isa isa = xtensa_default_isa;
7356 int offset = 0;
7357 int slot;
7358 bfd_boolean branch_seen = FALSE;
7360 if (!insnbuf)
7362 insnbuf = xtensa_insnbuf_alloc (isa);
7363 slotbuf = xtensa_insnbuf_alloc (isa);
7366 if (next_fragP == NULL)
7367 return FALSE;
7369 /* Check for the conditional branch. */
7370 xtensa_insnbuf_from_chars (isa, insnbuf, &next_fragP->fr_literal[offset], 0);
7371 fmt = xtensa_format_decode (isa, insnbuf);
7372 if (fmt == XTENSA_UNDEFINED)
7373 return FALSE;
7375 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
7377 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
7378 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
7380 branch_seen = (branch_seen
7381 || xtensa_opcode_is_branch (isa, opcode) == 1);
7384 if (!branch_seen)
7385 return FALSE;
7387 offset += xtensa_format_length (isa, fmt);
7388 if (offset == next_fragP->fr_fix)
7390 next_fragP = next_non_empty_frag (next_fragP);
7391 offset = 0;
7394 if (next_fragP == NULL)
7395 return FALSE;
7397 /* Check for the retw/retw.n. */
7398 xtensa_insnbuf_from_chars (isa, insnbuf, &next_fragP->fr_literal[offset], 0);
7399 fmt = xtensa_format_decode (isa, insnbuf);
7401 /* Because RETW[.N] is not bundleable, a VLIW bundle here means that we
7402 have no problems. */
7403 if (fmt == XTENSA_UNDEFINED
7404 || xtensa_format_num_slots (isa, fmt) != 1)
7405 return FALSE;
7407 xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf);
7408 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
7410 if (opcode == xtensa_retw_opcode || opcode == xtensa_retw_n_opcode)
7411 return TRUE;
7413 return FALSE;
7417 /* Re-process all of the fragments looking to convert all of the
7418 RELAX_ADD_NOP_IF_PRE_LOOP_END. If there is one instruction and a
7419 loop end label, convert this frag to one that will generate a NOP.
7420 In any case close it off with a .fill 0. */
7422 static bfd_boolean next_instr_is_loop_end (fragS *);
7424 static void
7425 xtensa_fix_b_j_loop_end_frags (void)
7427 frchainS *frchP;
7429 /* When this routine is called, all of the subsections are still intact
7430 so we walk over subsections instead of sections. */
7431 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7433 fragS *fragP;
7435 /* Walk over all of the fragments in a subsection. */
7436 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7438 if (fragP->fr_type == rs_machine_dependent
7439 && fragP->fr_subtype == RELAX_ADD_NOP_IF_PRE_LOOP_END)
7441 if (next_instr_is_loop_end (fragP))
7443 if (fragP->tc_frag_data.is_no_transform)
7444 as_bad (_("branching or jumping to a loop end may trigger hardware errata"));
7445 else
7446 relax_frag_add_nop (fragP);
7448 frag_wane (fragP);
7455 static bfd_boolean
7456 next_instr_is_loop_end (fragS *fragP)
7458 const fragS *next_fragP;
7460 if (next_frag_is_loop_target (fragP))
7461 return FALSE;
7463 next_fragP = next_non_empty_frag (fragP);
7464 if (next_fragP == NULL)
7465 return FALSE;
7467 if (!next_frag_is_loop_target (next_fragP))
7468 return FALSE;
7470 /* If the size is >= 3 then there is more than one instruction here.
7471 The hardware bug will not fire. */
7472 if (next_fragP->fr_fix > 3)
7473 return FALSE;
7475 return TRUE;
7479 /* Re-process all of the fragments looking to convert all of the
7480 RELAX_ADD_NOP_IF_CLOSE_LOOP_END. If there is an loop end that is
7481 not MY loop's loop end within 12 bytes, add enough nops here to
7482 make it at least 12 bytes away. In any case close it off with a
7483 .fill 0. */
7485 static size_t min_bytes_to_other_loop_end (fragS *, fragS *, offsetT, size_t);
7487 static void
7488 xtensa_fix_close_loop_end_frags (void)
7490 frchainS *frchP;
7492 /* When this routine is called, all of the subsections are still intact
7493 so we walk over subsections instead of sections. */
7494 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7496 fragS *fragP;
7498 fragS *current_target = NULL;
7499 offsetT current_offset = 0;
7501 /* Walk over all of the fragments in a subsection. */
7502 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7504 if (fragP->fr_type == rs_machine_dependent
7505 && ((fragP->fr_subtype == RELAX_IMMED)
7506 || ((fragP->fr_subtype == RELAX_SLOTS)
7507 && (fragP->tc_frag_data.slot_subtypes[0]
7508 == RELAX_IMMED))))
7510 /* Read it. If the instruction is a loop, get the target. */
7511 TInsn t_insn;
7512 tinsn_from_chars (&t_insn, fragP->fr_opcode, 0);
7513 if (xtensa_opcode_is_loop (xtensa_default_isa,
7514 t_insn.opcode) == 1)
7516 /* Get the current fragment target. */
7517 if (fragP->tc_frag_data.slot_symbols[0])
7519 symbolS *sym = fragP->tc_frag_data.slot_symbols[0];
7520 current_target = symbol_get_frag (sym);
7521 current_offset = fragP->fr_offset;
7526 if (current_target
7527 && fragP->fr_type == rs_machine_dependent
7528 && fragP->fr_subtype == RELAX_ADD_NOP_IF_CLOSE_LOOP_END)
7530 size_t min_bytes;
7531 size_t bytes_added = 0;
7533 #define REQUIRED_LOOP_DIVIDING_BYTES 12
7534 /* Max out at 12. */
7535 min_bytes = min_bytes_to_other_loop_end
7536 (fragP->fr_next, current_target, current_offset,
7537 REQUIRED_LOOP_DIVIDING_BYTES);
7539 if (min_bytes < REQUIRED_LOOP_DIVIDING_BYTES)
7541 if (fragP->tc_frag_data.is_no_transform)
7542 as_bad (_("loop end too close to another loop end may trigger hardware errata"));
7543 else
7545 while (min_bytes + bytes_added
7546 < REQUIRED_LOOP_DIVIDING_BYTES)
7548 int length = 3;
7550 if (fragP->fr_var < length)
7551 as_fatal (_("fr_var %lu < length %d"),
7552 fragP->fr_var, length);
7553 else
7555 assemble_nop (length,
7556 fragP->fr_literal + fragP->fr_fix);
7557 fragP->fr_fix += length;
7558 fragP->fr_var -= length;
7560 bytes_added += length;
7564 frag_wane (fragP);
7566 assert (fragP->fr_type != rs_machine_dependent
7567 || fragP->fr_subtype != RELAX_ADD_NOP_IF_CLOSE_LOOP_END);
7573 static size_t unrelaxed_frag_min_size (fragS *);
7575 static size_t
7576 min_bytes_to_other_loop_end (fragS *fragP,
7577 fragS *current_target,
7578 offsetT current_offset,
7579 size_t max_size)
7581 size_t offset = 0;
7582 fragS *current_fragP;
7584 for (current_fragP = fragP;
7585 current_fragP;
7586 current_fragP = current_fragP->fr_next)
7588 if (current_fragP->tc_frag_data.is_loop_target
7589 && current_fragP != current_target)
7590 return offset + current_offset;
7592 offset += unrelaxed_frag_min_size (current_fragP);
7594 if (offset + current_offset >= max_size)
7595 return max_size;
7597 return max_size;
7601 static size_t
7602 unrelaxed_frag_min_size (fragS *fragP)
7604 size_t size = fragP->fr_fix;
7606 /* add fill size */
7607 if (fragP->fr_type == rs_fill)
7608 size += fragP->fr_offset;
7610 return size;
7614 static size_t
7615 unrelaxed_frag_max_size (fragS *fragP)
7617 size_t size = fragP->fr_fix;
7618 switch (fragP->fr_type)
7620 case 0:
7621 /* Empty frags created by the obstack allocation scheme
7622 end up with type 0. */
7623 break;
7624 case rs_fill:
7625 case rs_org:
7626 case rs_space:
7627 size += fragP->fr_offset;
7628 break;
7629 case rs_align:
7630 case rs_align_code:
7631 case rs_align_test:
7632 case rs_leb128:
7633 case rs_cfa:
7634 case rs_dwarf2dbg:
7635 /* No further adjustments needed. */
7636 break;
7637 case rs_machine_dependent:
7638 if (fragP->fr_subtype != RELAX_DESIRE_ALIGN)
7639 size += fragP->fr_var;
7640 break;
7641 default:
7642 /* We had darn well better know how big it is. */
7643 assert (0);
7644 break;
7647 return size;
7651 /* Re-process all of the fragments looking to convert all
7652 of the RELAX_ADD_NOP_IF_SHORT_LOOP. If:
7655 1) the instruction size count to the loop end label
7656 is too short (<= 2 instructions),
7657 2) loop has a jump or branch in it
7659 or B)
7660 1) workaround_all_short_loops is TRUE
7661 2) The generating loop was a 'loopgtz' or 'loopnez'
7662 3) the instruction size count to the loop end label is too short
7663 (<= 2 instructions)
7664 then convert this frag (and maybe the next one) to generate a NOP.
7665 In any case close it off with a .fill 0. */
7667 static size_t count_insns_to_loop_end (fragS *, bfd_boolean, size_t);
7668 static bfd_boolean branch_before_loop_end (fragS *);
7670 static void
7671 xtensa_fix_short_loop_frags (void)
7673 frchainS *frchP;
7675 /* When this routine is called, all of the subsections are still intact
7676 so we walk over subsections instead of sections. */
7677 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7679 fragS *fragP;
7680 fragS *current_target = NULL;
7681 offsetT current_offset = 0;
7682 xtensa_opcode current_opcode = XTENSA_UNDEFINED;
7684 /* Walk over all of the fragments in a subsection. */
7685 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7687 /* Check on the current loop. */
7688 if (fragP->fr_type == rs_machine_dependent
7689 && ((fragP->fr_subtype == RELAX_IMMED)
7690 || ((fragP->fr_subtype == RELAX_SLOTS)
7691 && (fragP->tc_frag_data.slot_subtypes[0]
7692 == RELAX_IMMED))))
7694 TInsn t_insn;
7696 /* Read it. If the instruction is a loop, get the target. */
7697 tinsn_from_chars (&t_insn, fragP->fr_opcode, 0);
7698 if (xtensa_opcode_is_loop (xtensa_default_isa,
7699 t_insn.opcode) == 1)
7701 /* Get the current fragment target. */
7702 if (fragP->tc_frag_data.slot_symbols[0])
7704 symbolS *sym = fragP->tc_frag_data.slot_symbols[0];
7705 current_target = symbol_get_frag (sym);
7706 current_offset = fragP->fr_offset;
7707 current_opcode = t_insn.opcode;
7712 if (fragP->fr_type == rs_machine_dependent
7713 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
7715 size_t insn_count =
7716 count_insns_to_loop_end (fragP->fr_next, TRUE, 3);
7717 if (insn_count < 3
7718 && (branch_before_loop_end (fragP->fr_next)
7719 || (workaround_all_short_loops
7720 && current_opcode != XTENSA_UNDEFINED
7721 && current_opcode != xtensa_loop_opcode)))
7723 if (fragP->tc_frag_data.is_no_transform)
7724 as_bad (_("loop containing less than three instructions may trigger hardware errata"));
7725 else
7726 relax_frag_add_nop (fragP);
7728 frag_wane (fragP);
7735 static size_t unrelaxed_frag_min_insn_count (fragS *);
7737 static size_t
7738 count_insns_to_loop_end (fragS *base_fragP,
7739 bfd_boolean count_relax_add,
7740 size_t max_count)
7742 fragS *fragP = NULL;
7743 size_t insn_count = 0;
7745 fragP = base_fragP;
7747 for (; fragP && !fragP->tc_frag_data.is_loop_target; fragP = fragP->fr_next)
7749 insn_count += unrelaxed_frag_min_insn_count (fragP);
7750 if (insn_count >= max_count)
7751 return max_count;
7753 if (count_relax_add)
7755 if (fragP->fr_type == rs_machine_dependent
7756 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
7758 /* In order to add the appropriate number of
7759 NOPs, we count an instruction for downstream
7760 occurrences. */
7761 insn_count++;
7762 if (insn_count >= max_count)
7763 return max_count;
7767 return insn_count;
7771 static size_t
7772 unrelaxed_frag_min_insn_count (fragS *fragP)
7774 xtensa_isa isa = xtensa_default_isa;
7775 static xtensa_insnbuf insnbuf = NULL;
7776 size_t insn_count = 0;
7777 int offset = 0;
7779 if (!fragP->tc_frag_data.is_insn)
7780 return insn_count;
7782 if (!insnbuf)
7783 insnbuf = xtensa_insnbuf_alloc (isa);
7785 /* Decode the fixed instructions. */
7786 while (offset < fragP->fr_fix)
7788 xtensa_format fmt;
7790 xtensa_insnbuf_from_chars (isa, insnbuf, fragP->fr_literal + offset, 0);
7791 fmt = xtensa_format_decode (isa, insnbuf);
7793 if (fmt == XTENSA_UNDEFINED)
7795 as_fatal (_("undecodable instruction in instruction frag"));
7796 return insn_count;
7798 offset += xtensa_format_length (isa, fmt);
7799 insn_count++;
7802 return insn_count;
7806 static bfd_boolean unrelaxed_frag_has_b_j (fragS *);
7808 static bfd_boolean
7809 branch_before_loop_end (fragS *base_fragP)
7811 fragS *fragP;
7813 for (fragP = base_fragP;
7814 fragP && !fragP->tc_frag_data.is_loop_target;
7815 fragP = fragP->fr_next)
7817 if (unrelaxed_frag_has_b_j (fragP))
7818 return TRUE;
7820 return FALSE;
7824 static bfd_boolean
7825 unrelaxed_frag_has_b_j (fragS *fragP)
7827 static xtensa_insnbuf insnbuf = NULL;
7828 xtensa_isa isa = xtensa_default_isa;
7829 int offset = 0;
7831 if (!fragP->tc_frag_data.is_insn)
7832 return FALSE;
7834 if (!insnbuf)
7835 insnbuf = xtensa_insnbuf_alloc (isa);
7837 /* Decode the fixed instructions. */
7838 while (offset < fragP->fr_fix)
7840 xtensa_format fmt;
7841 int slot;
7843 xtensa_insnbuf_from_chars (isa, insnbuf, fragP->fr_literal + offset, 0);
7844 fmt = xtensa_format_decode (isa, insnbuf);
7845 if (fmt == XTENSA_UNDEFINED)
7846 return FALSE;
7848 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
7850 xtensa_opcode opcode =
7851 get_opcode_from_buf (fragP->fr_literal + offset, slot);
7852 if (xtensa_opcode_is_branch (isa, opcode) == 1
7853 || xtensa_opcode_is_jump (isa, opcode) == 1)
7854 return TRUE;
7856 offset += xtensa_format_length (isa, fmt);
7858 return FALSE;
7862 /* Checks to be made after initial assembly but before relaxation. */
7864 static bfd_boolean is_empty_loop (const TInsn *, fragS *);
7865 static bfd_boolean is_local_forward_loop (const TInsn *, fragS *);
7867 static void
7868 xtensa_sanity_check (void)
7870 char *file_name;
7871 int line;
7873 frchainS *frchP;
7875 as_where (&file_name, &line);
7876 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7878 fragS *fragP;
7880 /* Walk over all of the fragments in a subsection. */
7881 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7883 /* Currently we only check for empty loops here. */
7884 if (fragP->fr_type == rs_machine_dependent
7885 && fragP->fr_subtype == RELAX_IMMED)
7887 static xtensa_insnbuf insnbuf = NULL;
7888 TInsn t_insn;
7890 if (fragP->fr_opcode != NULL)
7892 if (!insnbuf)
7893 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
7894 tinsn_from_chars (&t_insn, fragP->fr_opcode, 0);
7895 tinsn_immed_from_frag (&t_insn, fragP, 0);
7897 if (xtensa_opcode_is_loop (xtensa_default_isa,
7898 t_insn.opcode) == 1)
7900 if (is_empty_loop (&t_insn, fragP))
7902 new_logical_line (fragP->fr_file, fragP->fr_line);
7903 as_bad (_("invalid empty loop"));
7905 if (!is_local_forward_loop (&t_insn, fragP))
7907 new_logical_line (fragP->fr_file, fragP->fr_line);
7908 as_bad (_("loop target does not follow "
7909 "loop instruction in section"));
7916 new_logical_line (file_name, line);
7920 #define LOOP_IMMED_OPN 1
7922 /* Return TRUE if the loop target is the next non-zero fragment. */
7924 static bfd_boolean
7925 is_empty_loop (const TInsn *insn, fragS *fragP)
7927 const expressionS *expr;
7928 symbolS *symbolP;
7929 fragS *next_fragP;
7931 if (insn->insn_type != ITYPE_INSN)
7932 return FALSE;
7934 if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) != 1)
7935 return FALSE;
7937 if (insn->ntok <= LOOP_IMMED_OPN)
7938 return FALSE;
7940 expr = &insn->tok[LOOP_IMMED_OPN];
7942 if (expr->X_op != O_symbol)
7943 return FALSE;
7945 symbolP = expr->X_add_symbol;
7946 if (!symbolP)
7947 return FALSE;
7949 if (symbol_get_frag (symbolP) == NULL)
7950 return FALSE;
7952 if (S_GET_VALUE (symbolP) != 0)
7953 return FALSE;
7955 /* Walk through the zero-size fragments from this one. If we find
7956 the target fragment, then this is a zero-size loop. */
7958 for (next_fragP = fragP->fr_next;
7959 next_fragP != NULL;
7960 next_fragP = next_fragP->fr_next)
7962 if (next_fragP == symbol_get_frag (symbolP))
7963 return TRUE;
7964 if (next_fragP->fr_fix != 0)
7965 return FALSE;
7967 return FALSE;
7971 static bfd_boolean
7972 is_local_forward_loop (const TInsn *insn, fragS *fragP)
7974 const expressionS *expr;
7975 symbolS *symbolP;
7976 fragS *next_fragP;
7978 if (insn->insn_type != ITYPE_INSN)
7979 return FALSE;
7981 if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) == 0)
7982 return FALSE;
7984 if (insn->ntok <= LOOP_IMMED_OPN)
7985 return FALSE;
7987 expr = &insn->tok[LOOP_IMMED_OPN];
7989 if (expr->X_op != O_symbol)
7990 return FALSE;
7992 symbolP = expr->X_add_symbol;
7993 if (!symbolP)
7994 return FALSE;
7996 if (symbol_get_frag (symbolP) == NULL)
7997 return FALSE;
7999 /* Walk through fragments until we find the target.
8000 If we do not find the target, then this is an invalid loop. */
8002 for (next_fragP = fragP->fr_next;
8003 next_fragP != NULL;
8004 next_fragP = next_fragP->fr_next)
8006 if (next_fragP == symbol_get_frag (symbolP))
8007 return TRUE;
8010 return FALSE;
8014 /* Alignment Functions. */
8016 static size_t
8017 get_text_align_power (int target_size)
8019 size_t i = 0;
8020 for (i = 0; i < sizeof (size_t); i++)
8022 if (target_size <= (1 << i))
8023 return i;
8025 assert (0);
8026 return 0;
8030 static addressT
8031 get_text_align_max_fill_size (int align_pow,
8032 bfd_boolean use_nops,
8033 bfd_boolean use_no_density)
8035 if (!use_nops)
8036 return (1 << align_pow);
8037 if (use_no_density)
8038 return 3 * (1 << align_pow);
8040 return 1 + (1 << align_pow);
8044 /* get_text_align_fill_size ()
8046 Desired alignments:
8047 give the address
8048 target_size = size of next instruction
8049 align_pow = get_text_align_power (target_size).
8050 use_nops = 0
8051 use_no_density = 0;
8052 Loop alignments:
8053 address = current address + loop instruction size;
8054 target_size = 3 (for 2 or 3 byte target)
8055 = 4 (for 4 byte target)
8056 = 8 (for 8 byte target)
8057 align_pow = get_text_align_power (target_size);
8058 use_nops = 1
8059 use_no_density = set appropriately
8060 Text alignments:
8061 address = current address + loop instruction size;
8062 target_size = 0
8063 align_pow = get_text_align_power (target_size);
8064 use_nops = 0
8065 use_no_density = 0. */
8067 static addressT
8068 get_text_align_fill_size (addressT address,
8069 int align_pow,
8070 int target_size,
8071 bfd_boolean use_nops,
8072 bfd_boolean use_no_density)
8074 /* Input arguments:
8076 align_pow: log2 (required alignment).
8078 target_size: alignment must allow the new_address and
8079 new_address+target_size-1.
8081 use_nops: if TRUE, then we can only use 2- or 3-byte nops.
8083 use_no_density: if use_nops and use_no_density, we can only use
8084 3-byte nops.
8086 Usually the align_pow is the power of 2 that is greater than
8087 or equal to the target_size. This handles the 2-byte, 3-byte
8088 and 8-byte instructions.
8090 Two cases:
8092 (1) aligning an instruction properly, but without using NOPs.
8093 E.G.: a 3-byte instruction can go on any address where address mod 4
8094 is zero or one. The aligner uses this case to find the optimal
8095 number of fill bytes for relax_frag_for_align.
8097 (2) aligning an instruction properly, but where we might need to use
8098 extra NOPs. E.G.: when the aligner couldn't find enough widenings
8099 or similar to get the optimal location. */
8101 size_t alignment = (1 << align_pow);
8103 assert (target_size != 0);
8105 if (!use_nops)
8107 unsigned fill_bytes;
8108 for (fill_bytes = 0; fill_bytes < alignment; fill_bytes++)
8110 addressT end_address = address + target_size - 1 + fill_bytes;
8111 addressT start_address = address + fill_bytes;
8112 if ((end_address >> align_pow) == (start_address >> align_pow))
8113 return fill_bytes;
8115 assert (0);
8118 /* This is the slightly harder case. */
8119 assert ((int) alignment >= target_size);
8120 assert (target_size > 0);
8121 if (!use_no_density)
8123 size_t i;
8124 for (i = 0; i < alignment * 2; i++)
8126 if (i == 1)
8127 continue;
8128 if ((address + i) >> align_pow
8129 == (address + i + target_size - 1) >> align_pow)
8130 return i;
8133 else
8135 size_t i;
8137 /* Can only fill multiples of 3. */
8138 for (i = 0; i <= alignment * 3; i += 3)
8140 if ((address + i) >> align_pow
8141 == (address + i + target_size - 1) >> align_pow)
8142 return i;
8145 assert (0);
8146 return 0;
8150 /* This will assert if it is not possible. */
8152 static size_t
8153 get_text_align_nop_count (size_t fill_size, bfd_boolean use_no_density)
8155 size_t count = 0;
8156 if (use_no_density)
8158 assert (fill_size % 3 == 0);
8159 return (fill_size / 3);
8162 assert (fill_size != 1); /* Bad argument. */
8164 while (fill_size > 1)
8166 size_t insn_size = 3;
8167 if (fill_size == 2 || fill_size == 4)
8168 insn_size = 2;
8169 fill_size -= insn_size;
8170 count++;
8172 assert (fill_size != 1); /* Bad algorithm. */
8173 return count;
8177 static size_t
8178 get_text_align_nth_nop_size (size_t fill_size,
8179 size_t n,
8180 bfd_boolean use_no_density)
8182 size_t count = 0;
8184 assert (get_text_align_nop_count (fill_size, use_no_density) > n);
8186 if (use_no_density)
8187 return 3;
8189 while (fill_size > 1)
8191 size_t insn_size = 3;
8192 if (fill_size == 2 || fill_size == 4)
8193 insn_size = 2;
8194 fill_size -= insn_size;
8195 count++;
8196 if (n + 1 == count)
8197 return insn_size;
8199 assert (0);
8200 return 0;
8204 /* For the given fragment, find the appropriate address
8205 for it to begin at if we are using NOPs to align it. */
8207 static addressT
8208 get_noop_aligned_address (fragS *fragP, addressT address)
8210 /* The rule is: get next fragment's FIRST instruction. Find
8211 the smallest number of bytes that need to be added to
8212 ensure that the next fragment's FIRST instruction will fit
8213 in a single word.
8215 E.G., 2 bytes : 0, 1, 2 mod 4
8216 3 bytes: 0, 1 mod 4
8218 If the FIRST instruction MIGHT be relaxed,
8219 assume that it will become a 3-byte instruction.
8221 Note again here that LOOP instructions are not bundleable,
8222 and this relaxation only applies to LOOP opcodes. */
8224 size_t fill_size = 0;
8225 int first_insn_size;
8226 int loop_insn_size;
8227 addressT pre_opcode_bytes;
8228 size_t alignment;
8229 fragS *first_insn;
8230 xtensa_opcode opcode;
8231 bfd_boolean is_loop;
8233 assert (fragP->fr_type == rs_machine_dependent);
8234 assert (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE);
8236 /* Find the loop frag. */
8237 first_insn = next_non_empty_frag (fragP);
8238 /* Now find the first insn frag. */
8239 first_insn = next_non_empty_frag (first_insn);
8241 is_loop = next_frag_opcode_is_loop (fragP, &opcode);
8242 assert (is_loop);
8243 loop_insn_size = xg_get_single_size (opcode);
8245 pre_opcode_bytes = next_frag_pre_opcode_bytes (fragP);
8246 pre_opcode_bytes += loop_insn_size;
8248 /* For loops, the alignment depends on the size of the
8249 instruction following the loop, not the LOOP instruction. */
8251 if (first_insn == NULL)
8252 return address;
8254 assert (first_insn->tc_frag_data.is_first_loop_insn);
8256 first_insn_size = frag_format_size (first_insn);
8258 if (first_insn_size == 2 || first_insn_size == XTENSA_UNDEFINED)
8259 first_insn_size = 3; /* ISA specifies this */
8261 /* If it was 8, then we'll need a larger alignment for the section. */
8262 alignment = get_text_align_power (first_insn_size);
8264 /* Is now_seg valid? */
8265 record_alignment (now_seg, alignment);
8267 fill_size = get_text_align_fill_size
8268 (address + pre_opcode_bytes,
8269 get_text_align_power (first_insn_size),
8270 first_insn_size, TRUE, 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.
8283 We can align with:
8285 zeros - This is easy; always insert zeros.
8286 nops - 3-byte and 2-byte instructions
8287 2 - 2-byte nop
8288 3 - 3-byte nop
8289 4 - 2 2-byte nops
8290 >=5 : 3-byte instruction + fn (n-3)
8291 widening - widen previous instructions. */
8293 static addressT
8294 get_aligned_diff (fragS *fragP, addressT address, addressT *max_diff)
8296 addressT target_address, loop_insn_offset;
8297 int target_size;
8298 xtensa_opcode loop_opcode;
8299 bfd_boolean is_loop;
8300 int text_align_power;
8301 addressT opt_diff;
8303 assert (fragP->fr_type == rs_machine_dependent);
8304 switch (fragP->fr_subtype)
8306 case RELAX_DESIRE_ALIGN:
8307 target_size = next_frag_format_size (fragP);
8308 if (target_size == XTENSA_UNDEFINED)
8309 target_size = 3;
8310 text_align_power = get_text_align_power (xtensa_fetch_width);
8311 opt_diff = get_text_align_fill_size (address, text_align_power,
8312 target_size, FALSE, FALSE);
8314 *max_diff = opt_diff + xtensa_fetch_width
8315 - (target_size + ((address + opt_diff) % xtensa_fetch_width));
8316 assert (*max_diff >= opt_diff);
8317 return opt_diff;
8319 case RELAX_ALIGN_NEXT_OPCODE:
8320 target_size = next_frag_format_size (fragP);
8321 loop_insn_offset = 0;
8322 is_loop = next_frag_opcode_is_loop (fragP, &loop_opcode);
8323 assert (is_loop);
8325 /* If the loop has been expanded then the LOOP instruction
8326 could be at an offset from this fragment. */
8327 if (next_non_empty_frag(fragP)->tc_frag_data.slot_subtypes[0]
8328 != RELAX_IMMED)
8329 loop_insn_offset = get_expanded_loop_offset (loop_opcode);
8331 if (target_size == 2)
8332 target_size = 3; /* ISA specifies this */
8334 /* In an ideal world, which is what we are shooting for here,
8335 we wouldn't need to use any NOPs immediately prior to the
8336 LOOP instruction. If this approach fails, relax_frag_loop_align
8337 will call get_noop_aligned_address. */
8338 target_address =
8339 address + loop_insn_offset + xg_get_single_size (loop_opcode);
8340 text_align_power = get_text_align_power (target_size),
8341 opt_diff = get_text_align_fill_size (target_address, text_align_power,
8342 target_size, FALSE, FALSE);
8344 *max_diff = xtensa_fetch_width
8345 - ((target_address + opt_diff) % xtensa_fetch_width)
8346 - target_size + opt_diff;
8347 assert (*max_diff >= opt_diff);
8348 return opt_diff;
8350 default:
8351 break;
8353 assert (0);
8354 return 0;
8358 /* md_relax_frag Hook and Helper Functions. */
8360 static long relax_frag_loop_align (fragS *, long);
8361 static long relax_frag_for_align (fragS *, long);
8362 static long relax_frag_immed
8363 (segT, fragS *, long, int, xtensa_format, int, int *, bfd_boolean);
8366 /* Return the number of bytes added to this fragment, given that the
8367 input has been stretched already by "stretch". */
8369 long
8370 xtensa_relax_frag (fragS *fragP, long stretch, int *stretched_p)
8372 xtensa_isa isa = xtensa_default_isa;
8373 int unreported = fragP->tc_frag_data.unreported_expansion;
8374 long new_stretch = 0;
8375 char *file_name;
8376 int line, lit_size;
8377 static xtensa_insnbuf vbuf = NULL;
8378 int slot, num_slots;
8379 xtensa_format fmt;
8381 as_where (&file_name, &line);
8382 new_logical_line (fragP->fr_file, fragP->fr_line);
8384 fragP->tc_frag_data.unreported_expansion = 0;
8386 switch (fragP->fr_subtype)
8388 case RELAX_ALIGN_NEXT_OPCODE:
8389 /* Always convert. */
8390 if (fragP->tc_frag_data.relax_seen)
8391 new_stretch = relax_frag_loop_align (fragP, stretch);
8392 break;
8394 case RELAX_LOOP_END:
8395 /* Do nothing. */
8396 break;
8398 case RELAX_LOOP_END_ADD_NOP:
8399 /* Add a NOP and switch to .fill 0. */
8400 new_stretch = relax_frag_add_nop (fragP);
8401 frag_wane (fragP);
8402 break;
8404 case RELAX_DESIRE_ALIGN:
8405 /* Do nothing. The narrowing before this frag will either align
8406 it or not. */
8407 break;
8409 case RELAX_LITERAL:
8410 case RELAX_LITERAL_FINAL:
8411 return 0;
8413 case RELAX_LITERAL_NR:
8414 lit_size = 4;
8415 fragP->fr_subtype = RELAX_LITERAL_FINAL;
8416 assert (unreported == lit_size);
8417 memset (&fragP->fr_literal[fragP->fr_fix], 0, 4);
8418 fragP->fr_var -= lit_size;
8419 fragP->fr_fix += lit_size;
8420 new_stretch = 4;
8421 break;
8423 case RELAX_SLOTS:
8424 if (vbuf == NULL)
8425 vbuf = xtensa_insnbuf_alloc (isa);
8427 xtensa_insnbuf_from_chars (isa, vbuf, fragP->fr_opcode, 0);
8428 fmt = xtensa_format_decode (isa, vbuf);
8429 num_slots = xtensa_format_num_slots (isa, fmt);
8431 for (slot = 0; slot < num_slots; slot++)
8433 switch (fragP->tc_frag_data.slot_subtypes[slot])
8435 case RELAX_NARROW:
8436 if (fragP->tc_frag_data.relax_seen)
8437 new_stretch += relax_frag_for_align (fragP, stretch);
8438 break;
8440 case RELAX_IMMED:
8441 case RELAX_IMMED_STEP1:
8442 case RELAX_IMMED_STEP2:
8443 /* Place the immediate. */
8444 new_stretch += relax_frag_immed
8445 (now_seg, fragP, stretch,
8446 fragP->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED,
8447 fmt, slot, stretched_p, FALSE);
8448 break;
8450 default:
8451 /* This is OK; see the note in xg_assemble_vliw_tokens. */
8452 break;
8455 break;
8457 case RELAX_LITERAL_POOL_BEGIN:
8458 case RELAX_LITERAL_POOL_END:
8459 case RELAX_MAYBE_UNREACHABLE:
8460 case RELAX_MAYBE_DESIRE_ALIGN:
8461 /* No relaxation required. */
8462 break;
8464 case RELAX_FILL_NOP:
8465 case RELAX_UNREACHABLE:
8466 if (fragP->tc_frag_data.relax_seen)
8467 new_stretch += relax_frag_for_align (fragP, stretch);
8468 break;
8470 default:
8471 as_bad (_("bad relaxation state"));
8474 /* Tell gas we need another relaxation pass. */
8475 if (! fragP->tc_frag_data.relax_seen)
8477 fragP->tc_frag_data.relax_seen = TRUE;
8478 *stretched_p = 1;
8481 new_logical_line (file_name, line);
8482 return new_stretch;
8486 static long
8487 relax_frag_loop_align (fragS *fragP, long stretch)
8489 addressT old_address, old_next_address, old_size;
8490 addressT new_address, new_next_address, new_size;
8491 addressT growth;
8493 /* All the frags with relax_frag_for_alignment prior to this one in the
8494 section have been done, hopefully eliminating the need for a NOP here.
8495 But, this will put it in if necessary. */
8497 /* Calculate the old address of this fragment and the next fragment. */
8498 old_address = fragP->fr_address - stretch;
8499 old_next_address = (fragP->fr_address - stretch + fragP->fr_fix +
8500 fragP->tc_frag_data.text_expansion[0]);
8501 old_size = old_next_address - old_address;
8503 /* Calculate the new address of this fragment and the next fragment. */
8504 new_address = fragP->fr_address;
8505 new_next_address =
8506 get_noop_aligned_address (fragP, fragP->fr_address + fragP->fr_fix);
8507 new_size = new_next_address - new_address;
8509 growth = new_size - old_size;
8511 /* Fix up the text_expansion field and return the new growth. */
8512 fragP->tc_frag_data.text_expansion[0] += growth;
8513 return growth;
8517 /* Add a NOP instruction. */
8519 static long
8520 relax_frag_add_nop (fragS *fragP)
8522 char *nop_buf = fragP->fr_literal + fragP->fr_fix;
8523 int length = fragP->tc_frag_data.is_no_density ? 3 : 2;
8524 assemble_nop (length, nop_buf);
8525 fragP->tc_frag_data.is_insn = TRUE;
8527 if (fragP->fr_var < length)
8529 as_fatal (_("fr_var (%ld) < length (%d)"), fragP->fr_var, length);
8530 return 0;
8533 fragP->fr_fix += length;
8534 fragP->fr_var -= length;
8535 return length;
8539 static long future_alignment_required (fragS *, long);
8541 static long
8542 relax_frag_for_align (fragS *fragP, long stretch)
8544 /* Overview of the relaxation procedure for alignment:
8545 We can widen with NOPs or by widening instructions or by filling
8546 bytes after jump instructions. Find the opportune places and widen
8547 them if necessary. */
8549 long stretch_me;
8550 long diff;
8552 assert (fragP->fr_subtype == RELAX_FILL_NOP
8553 || fragP->fr_subtype == RELAX_UNREACHABLE
8554 || (fragP->fr_subtype == RELAX_SLOTS
8555 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW));
8557 stretch_me = future_alignment_required (fragP, stretch);
8558 diff = stretch_me - fragP->tc_frag_data.text_expansion[0];
8559 if (diff == 0)
8560 return 0;
8562 if (diff < 0)
8564 /* We expanded on a previous pass. Can we shrink now? */
8565 long shrink = fragP->tc_frag_data.text_expansion[0] - stretch_me;
8566 if (shrink <= stretch && stretch > 0)
8568 fragP->tc_frag_data.text_expansion[0] = stretch_me;
8569 return -shrink;
8571 return 0;
8574 /* Below here, diff > 0. */
8575 fragP->tc_frag_data.text_expansion[0] = stretch_me;
8577 return diff;
8581 /* Return the address of the next frag that should be aligned.
8583 By "address" we mean the address it _would_ be at if there
8584 is no action taken to align it between here and the target frag.
8585 In other words, if no narrows and no fill nops are used between
8586 here and the frag to align, _even_if_ some of the frags we use
8587 to align targets have already expanded on a previous relaxation
8588 pass.
8590 Also, count each frag that may be used to help align the target.
8592 Return 0 if there are no frags left in the chain that need to be
8593 aligned. */
8595 static addressT
8596 find_address_of_next_align_frag (fragS **fragPP,
8597 int *wide_nops,
8598 int *narrow_nops,
8599 int *widens,
8600 bfd_boolean *paddable)
8602 fragS *fragP = *fragPP;
8603 addressT address = fragP->fr_address;
8605 /* Do not reset the counts to 0. */
8607 while (fragP)
8609 /* Limit this to a small search. */
8610 if (*widens > 8)
8612 *fragPP = fragP;
8613 return 0;
8615 address += fragP->fr_fix;
8617 if (fragP->fr_type == rs_fill)
8618 address += fragP->fr_offset * fragP->fr_var;
8619 else if (fragP->fr_type == rs_machine_dependent)
8621 switch (fragP->fr_subtype)
8623 case RELAX_UNREACHABLE:
8624 *paddable = TRUE;
8625 break;
8627 case RELAX_FILL_NOP:
8628 (*wide_nops)++;
8629 if (!fragP->tc_frag_data.is_no_density)
8630 (*narrow_nops)++;
8631 break;
8633 case RELAX_SLOTS:
8634 if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
8636 (*widens)++;
8637 break;
8639 address += total_frag_text_expansion (fragP);;
8640 break;
8642 case RELAX_IMMED:
8643 address += fragP->tc_frag_data.text_expansion[0];
8644 break;
8646 case RELAX_ALIGN_NEXT_OPCODE:
8647 case RELAX_DESIRE_ALIGN:
8648 *fragPP = fragP;
8649 return address;
8651 case RELAX_MAYBE_UNREACHABLE:
8652 case RELAX_MAYBE_DESIRE_ALIGN:
8653 /* Do nothing. */
8654 break;
8656 default:
8657 /* Just punt if we don't know the type. */
8658 *fragPP = fragP;
8659 return 0;
8662 else
8664 /* Just punt if we don't know the type. */
8665 *fragPP = fragP;
8666 return 0;
8668 fragP = fragP->fr_next;
8671 *fragPP = fragP;
8672 return 0;
8676 static long bytes_to_stretch (fragS *, int, int, int, int);
8678 /* Undefine LOOKAHEAD_ALIGNER to get the older behavior.
8679 I'll leave this in until I am more confident this works. */
8681 #define LOOKAHEAD_ALIGNER 1
8683 static long
8684 future_alignment_required (fragS *fragP, long stretch ATTRIBUTE_UNUSED)
8686 fragS *this_frag = fragP;
8687 long address;
8688 int num_widens = 0;
8689 int wide_nops = 0;
8690 int narrow_nops = 0;
8691 bfd_boolean paddable = FALSE;
8692 offsetT local_opt_diff;
8693 offsetT opt_diff;
8694 offsetT max_diff;
8695 int stretch_amount = 0;
8696 int local_stretch_amount;
8697 int global_stretch_amount;
8699 address = find_address_of_next_align_frag
8700 (&fragP, &wide_nops, &narrow_nops, &num_widens, &paddable);
8702 if (address)
8704 local_opt_diff = get_aligned_diff (fragP, address, &max_diff);
8705 opt_diff = local_opt_diff;
8706 assert (opt_diff >= 0);
8707 assert (max_diff >= opt_diff);
8708 if (max_diff == 0)
8709 return 0;
8710 #ifdef LOOKAHEAD_ALIGNER
8711 if (fragP)
8712 fragP = fragP->fr_next;
8714 while (fragP && opt_diff < max_diff && address)
8716 /* We only use these to determine if we can exit early
8717 because there will be plenty of ways to align future
8718 align frags. */
8719 unsigned int glob_widens = 0;
8720 int dnn = 0;
8721 int dw = 0;
8722 bfd_boolean glob_pad = 0;
8723 address = find_address_of_next_align_frag
8724 (&fragP, &glob_widens, &dnn, &dw, &glob_pad);
8725 /* If there is a padable portion, then skip. */
8726 if (glob_pad || (glob_widens >= xtensa_fetch_width))
8727 break;
8729 if (address)
8731 offsetT next_m_diff;
8732 offsetT next_o_diff;
8734 /* Downrange frags haven't had stretch added to them yet. */
8735 address += stretch;
8737 /* The address also includes any text expansion from this
8738 frag in a previous pass, but we don't want that. */
8739 address -= this_frag->tc_frag_data.text_expansion[0];
8741 /* Assume we are going to move at least opt_diff. In
8742 reality, we might not be able to, but assuming that
8743 we will helps catch cases where moving opt_diff pushes
8744 the next target from aligned to unaligned. */
8745 address += opt_diff;
8747 next_o_diff = get_aligned_diff (fragP, address, &next_m_diff);
8749 /* Now cleanup for the adjustments to address. */
8750 next_o_diff += opt_diff;
8751 next_m_diff += opt_diff;
8752 if (next_o_diff <= max_diff && next_o_diff > opt_diff)
8753 opt_diff = next_o_diff;
8754 if (next_m_diff < max_diff)
8755 max_diff = next_m_diff;
8756 fragP = fragP->fr_next;
8759 #endif /* LOOKAHEAD_ALIGNER */
8760 /* If there are enough wideners in between, do it. */
8761 if (paddable)
8763 if (this_frag->fr_subtype == RELAX_UNREACHABLE)
8765 assert (opt_diff <= UNREACHABLE_MAX_WIDTH);
8766 return opt_diff;
8768 return 0;
8770 local_stretch_amount
8771 = bytes_to_stretch (this_frag, wide_nops, narrow_nops,
8772 num_widens, local_opt_diff);
8773 #ifdef LOOKAHEAD_ALIGNER
8774 global_stretch_amount
8775 = bytes_to_stretch (this_frag, wide_nops, narrow_nops,
8776 num_widens, opt_diff);
8777 /* If the condition below is true, then the frag couldn't
8778 stretch the correct amount for the global case, so we just
8779 optimize locally. We'll rely on the subsequent frags to get
8780 the correct alignment in the global case. */
8781 if (global_stretch_amount < local_stretch_amount)
8782 stretch_amount = local_stretch_amount;
8783 else
8784 stretch_amount = global_stretch_amount;
8785 #else /* ! LOOKAHEAD_ALIGNER */
8786 stretch_amount = local_stretch_amount;
8787 #endif /* ! LOOKAHEAD_ALIGNER */
8788 if (this_frag->fr_subtype == RELAX_SLOTS
8789 && this_frag->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
8790 assert (stretch_amount <= 1);
8791 else if (this_frag->fr_subtype == RELAX_FILL_NOP)
8793 if (this_frag->tc_frag_data.is_no_density)
8794 assert (stretch_amount == 3 || stretch_amount == 0);
8795 else
8796 assert (stretch_amount <= 3);
8799 return stretch_amount;
8803 /* The idea: widen everything you can to get a target or loop aligned,
8804 then start using NOPs.
8806 When we must have a NOP, here is a table of how we decide
8807 (so you don't have to fight through the control flow below):
8809 wide_nops = the number of wide NOPs available for aligning
8810 narrow_nops = the number of narrow NOPs available for aligning
8811 (a subset of wide_nops)
8812 widens = the number of narrow instructions that should be widened
8814 Desired wide narrow
8815 Diff nop nop widens
8816 1 0 0 1
8817 2 0 1 0
8818 3a 1 0 0
8819 b 0 1 1 (case 3a makes this case unnecessary)
8820 4a 1 0 1
8821 b 0 2 0
8822 c 0 1 2 (case 4a makes this case unnecessary)
8823 5a 1 0 2
8824 b 1 1 0
8825 c 0 2 1 (case 5b makes this case unnecessary)
8826 6a 2 0 0
8827 b 1 0 3
8828 c 0 1 4 (case 6b makes this case unneccesary)
8829 d 1 1 1 (case 6a makes this case unnecessary)
8830 e 0 2 2 (case 6a makes this case unnecessary)
8831 f 0 3 0 (case 6a makes this case unnecessary)
8832 7a 1 0 4
8833 b 2 0 1
8834 c 1 1 2 (case 7b makes this case unnecessary)
8835 d 0 1 5 (case 7a makes this case unnecessary)
8836 e 0 2 3 (case 7b makes this case unnecessary)
8837 f 0 3 1 (case 7b makes this case unnecessary)
8838 g 1 2 1 (case 7b makes this case unnecessary)
8841 static long
8842 bytes_to_stretch (fragS *this_frag,
8843 int wide_nops,
8844 int narrow_nops,
8845 int num_widens,
8846 int desired_diff)
8848 int bytes_short = desired_diff - num_widens;
8850 assert (desired_diff >= 0 && desired_diff < 8);
8851 if (desired_diff == 0)
8852 return 0;
8854 assert (wide_nops > 0 || num_widens > 0);
8856 /* Always prefer widening to NOP-filling. */
8857 if (bytes_short < 0)
8859 /* There are enough RELAX_NARROW frags after this one
8860 to align the target without widening this frag in any way. */
8861 return 0;
8864 if (bytes_short == 0)
8866 /* Widen every narrow between here and the align target
8867 and the align target will be properly aligned. */
8868 if (this_frag->fr_subtype == RELAX_FILL_NOP)
8869 return 0;
8870 else
8871 return 1;
8874 /* From here we will need at least one NOP to get an alignment.
8875 However, we may not be able to align at all, in which case,
8876 don't widen. */
8877 if (this_frag->fr_subtype == RELAX_FILL_NOP)
8879 switch (desired_diff)
8881 case 1:
8882 return 0;
8883 case 2:
8884 if (!this_frag->tc_frag_data.is_no_density && narrow_nops == 1)
8885 return 2; /* case 2 */
8886 return 0;
8887 case 3:
8888 if (wide_nops > 1)
8889 return 0;
8890 else
8891 return 3; /* case 3a */
8892 case 4:
8893 if (num_widens >= 1 && wide_nops == 1)
8894 return 3; /* case 4a */
8895 if (!this_frag->tc_frag_data.is_no_density && narrow_nops == 2)
8896 return 2; /* case 4b */
8897 return 0;
8898 case 5:
8899 if (num_widens >= 2 && wide_nops == 1)
8900 return 3; /* case 5a */
8901 /* We will need two nops. Are there enough nops
8902 between here and the align target? */
8903 if (wide_nops < 2 || narrow_nops == 0)
8904 return 0;
8905 /* Are there other nops closer that can serve instead? */
8906 if (wide_nops > 2 && narrow_nops > 1)
8907 return 0;
8908 /* Take the density one first, because there might not be
8909 another density one available. */
8910 if (!this_frag->tc_frag_data.is_no_density)
8911 return 2; /* case 5b narrow */
8912 else
8913 return 3; /* case 5b wide */
8914 return 0;
8915 case 6:
8916 if (wide_nops == 2)
8917 return 3; /* case 6a */
8918 else if (num_widens >= 3 && wide_nops == 1)
8919 return 3; /* case 6b */
8920 return 0;
8921 case 7:
8922 if (wide_nops == 1 && num_widens >= 4)
8923 return 3; /* case 7a */
8924 else if (wide_nops == 2 && num_widens >= 1)
8925 return 3; /* case 7b */
8926 return 0;
8927 default:
8928 assert (0);
8931 else
8933 /* We will need a NOP no matter what, but should we widen
8934 this instruction to help?
8936 This is a RELAX_FRAG_NARROW frag. */
8937 switch (desired_diff)
8939 case 1:
8940 assert (0);
8941 return 0;
8942 case 2:
8943 case 3:
8944 return 0;
8945 case 4:
8946 if (wide_nops >= 1 && num_widens == 1)
8947 return 1; /* case 4a */
8948 return 0;
8949 case 5:
8950 if (wide_nops >= 1 && num_widens == 2)
8951 return 1; /* case 5a */
8952 return 0;
8953 case 6:
8954 if (wide_nops >= 2)
8955 return 0; /* case 6a */
8956 else if (wide_nops >= 1 && num_widens == 3)
8957 return 1; /* case 6b */
8958 return 0;
8959 case 7:
8960 if (wide_nops >= 1 && num_widens == 4)
8961 return 1; /* case 7a */
8962 else if (wide_nops >= 2 && num_widens == 1)
8963 return 1; /* case 7b */
8964 return 0;
8965 default:
8966 assert (0);
8967 return 0;
8970 assert (0);
8971 return 0;
8975 static long
8976 relax_frag_immed (segT segP,
8977 fragS *fragP,
8978 long stretch,
8979 int min_steps,
8980 xtensa_format fmt,
8981 int slot,
8982 int *stretched_p,
8983 bfd_boolean estimate_only)
8985 TInsn tinsn;
8986 vliw_insn orig_vinsn;
8987 int old_size;
8988 bfd_boolean negatable_branch = FALSE;
8989 bfd_boolean branch_jmp_to_next = FALSE;
8990 bfd_boolean wide_insn = FALSE;
8991 xtensa_isa isa = xtensa_default_isa;
8992 IStack istack;
8993 offsetT frag_offset;
8994 int num_steps;
8995 fragS *lit_fragP;
8996 int num_text_bytes, num_literal_bytes;
8997 int literal_diff, total_text_diff, this_text_diff, first;
8999 assert (fragP->fr_opcode != NULL);
9001 xg_init_vinsn (&orig_vinsn);
9002 vinsn_from_chars (&orig_vinsn, fragP->fr_opcode);
9003 if (xtensa_format_num_slots (isa, fmt) > 1)
9004 wide_insn = TRUE;
9006 tinsn = orig_vinsn.slots[slot];
9007 tinsn_immed_from_frag (&tinsn, fragP, slot);
9009 if (estimate_only && xtensa_opcode_is_loop (isa, tinsn.opcode))
9010 return 0;
9012 if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform)
9013 branch_jmp_to_next = is_branch_jmp_to_next (&tinsn, fragP);
9015 negatable_branch = (xtensa_opcode_is_branch (isa, tinsn.opcode) == 1);
9017 old_size = xtensa_format_length (isa, fmt);
9019 /* Special case: replace a branch to the next instruction with a NOP.
9020 This is required to work around a hardware bug in T1040.0 and also
9021 serves as an optimization. */
9023 if (branch_jmp_to_next
9024 && ((old_size == 2) || (old_size == 3))
9025 && !next_frag_is_loop_target (fragP))
9026 return 0;
9028 /* Here is the fun stuff: Get the immediate field from this
9029 instruction. If it fits, we are done. If not, find the next
9030 instruction sequence that fits. */
9032 frag_offset = fragP->fr_opcode - fragP->fr_literal;
9033 istack_init (&istack);
9034 num_steps = xg_assembly_relax (&istack, &tinsn, segP, fragP, frag_offset,
9035 min_steps, stretch);
9036 if (num_steps < min_steps)
9038 as_fatal (_("internal error: relaxation failed"));
9039 return 0;
9042 if (num_steps > RELAX_IMMED_MAXSTEPS)
9044 as_fatal (_("internal error: relaxation requires too many steps"));
9045 return 0;
9048 fragP->tc_frag_data.slot_subtypes[slot] = (int) RELAX_IMMED + num_steps;
9050 /* Figure out the number of bytes needed. */
9051 lit_fragP = 0;
9052 num_literal_bytes = get_num_stack_literal_bytes (&istack);
9053 literal_diff =
9054 num_literal_bytes - fragP->tc_frag_data.literal_expansion[slot];
9055 first = 0;
9056 while (istack.insn[first].opcode == XTENSA_UNDEFINED)
9057 first++;
9058 num_text_bytes = get_num_stack_text_bytes (&istack);
9059 if (wide_insn)
9061 num_text_bytes += old_size;
9062 if (opcode_fits_format_slot (istack.insn[first].opcode, fmt, slot))
9063 num_text_bytes -= xg_get_single_size (istack.insn[first].opcode);
9065 total_text_diff = num_text_bytes - old_size;
9066 this_text_diff = total_text_diff - fragP->tc_frag_data.text_expansion[slot];
9068 /* It MUST get larger. If not, we could get an infinite loop. */
9069 assert (num_text_bytes >= 0);
9070 assert (literal_diff >= 0);
9071 assert (total_text_diff >= 0);
9073 fragP->tc_frag_data.text_expansion[slot] = total_text_diff;
9074 fragP->tc_frag_data.literal_expansion[slot] = num_literal_bytes;
9075 assert (fragP->tc_frag_data.text_expansion[slot] >= 0);
9076 assert (fragP->tc_frag_data.literal_expansion[slot] >= 0);
9078 /* Find the associated expandable literal for this. */
9079 if (literal_diff != 0)
9081 lit_fragP = fragP->tc_frag_data.literal_frags[slot];
9082 if (lit_fragP)
9084 assert (literal_diff == 4);
9085 lit_fragP->tc_frag_data.unreported_expansion += literal_diff;
9087 /* We expect that the literal section state has NOT been
9088 modified yet. */
9089 assert (lit_fragP->fr_type == rs_machine_dependent
9090 && lit_fragP->fr_subtype == RELAX_LITERAL);
9091 lit_fragP->fr_subtype = RELAX_LITERAL_NR;
9093 /* We need to mark this section for another iteration
9094 of relaxation. */
9095 (*stretched_p)++;
9099 if (negatable_branch && istack.ninsn > 1)
9100 update_next_frag_state (fragP);
9102 return this_text_diff;
9106 /* md_convert_frag Hook and Helper Functions. */
9108 static void convert_frag_align_next_opcode (fragS *);
9109 static void convert_frag_narrow (segT, fragS *, xtensa_format, int);
9110 static void convert_frag_fill_nop (fragS *);
9111 static void convert_frag_immed (segT, fragS *, int, xtensa_format, int);
9113 void
9114 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec, fragS *fragp)
9116 static xtensa_insnbuf vbuf = NULL;
9117 xtensa_isa isa = xtensa_default_isa;
9118 int slot;
9119 int num_slots;
9120 xtensa_format fmt;
9121 char *file_name;
9122 int line;
9124 as_where (&file_name, &line);
9125 new_logical_line (fragp->fr_file, fragp->fr_line);
9127 switch (fragp->fr_subtype)
9129 case RELAX_ALIGN_NEXT_OPCODE:
9130 /* Always convert. */
9131 convert_frag_align_next_opcode (fragp);
9132 break;
9134 case RELAX_DESIRE_ALIGN:
9135 /* Do nothing. If not aligned already, too bad. */
9136 break;
9138 case RELAX_LITERAL:
9139 case RELAX_LITERAL_FINAL:
9140 break;
9142 case RELAX_SLOTS:
9143 if (vbuf == NULL)
9144 vbuf = xtensa_insnbuf_alloc (isa);
9146 xtensa_insnbuf_from_chars (isa, vbuf, fragp->fr_opcode, 0);
9147 fmt = xtensa_format_decode (isa, vbuf);
9148 num_slots = xtensa_format_num_slots (isa, fmt);
9150 for (slot = 0; slot < num_slots; slot++)
9152 switch (fragp->tc_frag_data.slot_subtypes[slot])
9154 case RELAX_NARROW:
9155 convert_frag_narrow (sec, fragp, fmt, slot);
9156 break;
9158 case RELAX_IMMED:
9159 case RELAX_IMMED_STEP1:
9160 case RELAX_IMMED_STEP2:
9161 /* Place the immediate. */
9162 convert_frag_immed
9163 (sec, fragp,
9164 fragp->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED,
9165 fmt, slot);
9166 break;
9168 default:
9169 /* This is OK because some slots could have
9170 relaxations and others have none. */
9171 break;
9174 break;
9176 case RELAX_UNREACHABLE:
9177 memset (&fragp->fr_literal[fragp->fr_fix], 0, fragp->fr_var);
9178 fragp->fr_fix += fragp->tc_frag_data.text_expansion[0];
9179 fragp->fr_var -= fragp->tc_frag_data.text_expansion[0];
9180 frag_wane (fragp);
9181 break;
9183 case RELAX_MAYBE_UNREACHABLE:
9184 case RELAX_MAYBE_DESIRE_ALIGN:
9185 frag_wane (fragp);
9186 break;
9188 case RELAX_FILL_NOP:
9189 convert_frag_fill_nop (fragp);
9190 break;
9192 case RELAX_LITERAL_NR:
9193 if (use_literal_section)
9195 /* This should have been handled during relaxation. When
9196 relaxing a code segment, literals sometimes need to be
9197 added to the corresponding literal segment. If that
9198 literal segment has already been relaxed, then we end up
9199 in this situation. Marking the literal segments as data
9200 would make this happen less often (since GAS always relaxes
9201 code before data), but we could still get into trouble if
9202 there are instructions in a segment that is not marked as
9203 containing code. Until we can implement a better solution,
9204 cheat and adjust the addresses of all the following frags.
9205 This could break subsequent alignments, but the linker's
9206 literal coalescing will do that anyway. */
9208 fragS *f;
9209 fragp->fr_subtype = RELAX_LITERAL_FINAL;
9210 assert (fragp->tc_frag_data.unreported_expansion == 4);
9211 memset (&fragp->fr_literal[fragp->fr_fix], 0, 4);
9212 fragp->fr_var -= 4;
9213 fragp->fr_fix += 4;
9214 for (f = fragp->fr_next; f; f = f->fr_next)
9215 f->fr_address += 4;
9217 else
9218 as_bad (_("invalid relaxation fragment result"));
9219 break;
9222 fragp->fr_var = 0;
9223 new_logical_line (file_name, line);
9227 static void
9228 convert_frag_align_next_opcode (fragS *fragp)
9230 char *nop_buf; /* Location for Writing. */
9231 size_t i;
9233 bfd_boolean use_no_density = fragp->tc_frag_data.is_no_density;
9234 addressT aligned_address;
9235 size_t fill_size, nop_count;
9237 aligned_address = get_noop_aligned_address (fragp, fragp->fr_address +
9238 fragp->fr_fix);
9239 fill_size = aligned_address - (fragp->fr_address + fragp->fr_fix);
9240 nop_count = get_text_align_nop_count (fill_size, use_no_density);
9241 nop_buf = fragp->fr_literal + fragp->fr_fix;
9243 for (i = 0; i < nop_count; i++)
9245 size_t nop_size;
9246 nop_size = get_text_align_nth_nop_size (fill_size, i, use_no_density);
9248 assemble_nop (nop_size, nop_buf);
9249 nop_buf += nop_size;
9252 fragp->fr_fix += fill_size;
9253 fragp->fr_var -= fill_size;
9257 static void
9258 convert_frag_narrow (segT segP, fragS *fragP, xtensa_format fmt, int slot)
9260 TInsn tinsn, single_target;
9261 xtensa_format single_fmt;
9262 int size, old_size, diff, error_val;
9263 offsetT frag_offset;
9265 assert (slot == 0);
9266 tinsn_from_chars (&tinsn, fragP->fr_opcode, 0);
9268 if (xtensa_opcode_is_branch (xtensa_default_isa, tinsn.opcode) == 1)
9270 assert (fragP->tc_frag_data.text_expansion[0] == 1
9271 || fragP->tc_frag_data.text_expansion[0] == 0);
9272 convert_frag_immed (segP, fragP, fragP->tc_frag_data.text_expansion[0],
9273 fmt, slot);
9274 return;
9277 if (fragP->tc_frag_data.text_expansion[0] == 0)
9279 /* No conversion. */
9280 fragP->fr_var = 0;
9281 return;
9284 assert (fragP->fr_opcode != NULL);
9286 /* Frags in this relaxation state should only contain
9287 single instruction bundles. */
9288 tinsn_immed_from_frag (&tinsn, fragP, 0);
9290 /* Just convert it to a wide form.... */
9291 size = 0;
9292 old_size = xg_get_single_size (tinsn.opcode);
9294 tinsn_init (&single_target);
9295 frag_offset = fragP->fr_opcode - fragP->fr_literal;
9297 error_val = xg_expand_narrow (&single_target, &tinsn);
9298 if (error_val)
9300 as_bad (_("unable to widen instruction"));
9301 return;
9304 size = xg_get_single_size (single_target.opcode);
9305 single_fmt = xg_get_single_format (single_target.opcode);
9307 xg_emit_insn_to_buf (&single_target, single_fmt, fragP->fr_opcode,
9308 fragP, frag_offset, TRUE);
9310 diff = size - old_size;
9311 assert (diff >= 0);
9312 assert (diff <= fragP->fr_var);
9313 fragP->fr_var -= diff;
9314 fragP->fr_fix += diff;
9316 /* clean it up */
9317 fragP->fr_var = 0;
9321 static void
9322 convert_frag_fill_nop (fragS *fragP)
9324 char *loc = &fragP->fr_literal[fragP->fr_fix];
9325 int size = fragP->tc_frag_data.text_expansion[0];
9326 assert ((unsigned) size == (fragP->fr_next->fr_address
9327 - fragP->fr_address - fragP->fr_fix));
9328 if (size == 0)
9330 /* No conversion. */
9331 fragP->fr_var = 0;
9332 return;
9334 assemble_nop (size, loc);
9335 fragP->tc_frag_data.is_insn = TRUE;
9336 fragP->fr_var -= size;
9337 fragP->fr_fix += size;
9338 frag_wane (fragP);
9342 static fixS *fix_new_exp_in_seg
9343 (segT, subsegT, fragS *, int, int, expressionS *, int,
9344 bfd_reloc_code_real_type);
9345 static void convert_frag_immed_finish_loop (segT, fragS *, TInsn *);
9347 static void
9348 convert_frag_immed (segT segP,
9349 fragS *fragP,
9350 int min_steps,
9351 xtensa_format fmt,
9352 int slot)
9354 char *immed_instr = fragP->fr_opcode;
9355 TInsn orig_tinsn;
9356 bfd_boolean expanded = FALSE;
9357 bfd_boolean branch_jmp_to_next = FALSE;
9358 char *fr_opcode = fragP->fr_opcode;
9359 vliw_insn orig_vinsn;
9360 xtensa_isa isa = xtensa_default_isa;
9361 bfd_boolean wide_insn = FALSE;
9362 int bytes;
9363 bfd_boolean is_loop;
9365 assert (fr_opcode != NULL);
9367 xg_init_vinsn (&orig_vinsn);
9369 vinsn_from_chars (&orig_vinsn, fr_opcode);
9370 if (xtensa_format_num_slots (isa, fmt) > 1)
9371 wide_insn = TRUE;
9373 orig_tinsn = orig_vinsn.slots[slot];
9374 tinsn_immed_from_frag (&orig_tinsn, fragP, slot);
9376 is_loop = xtensa_opcode_is_loop (xtensa_default_isa, orig_tinsn.opcode) == 1;
9378 if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform)
9379 branch_jmp_to_next = is_branch_jmp_to_next (&orig_tinsn, fragP);
9381 if (branch_jmp_to_next && !next_frag_is_loop_target (fragP))
9383 /* Conversion just inserts a NOP and marks the fix as completed. */
9384 bytes = xtensa_format_length (isa, fmt);
9385 if (bytes >= 4)
9387 orig_vinsn.slots[slot].opcode =
9388 xtensa_format_slot_nop_opcode (isa, orig_vinsn.format, slot);
9389 orig_vinsn.slots[slot].ntok = 0;
9391 else
9393 bytes += fragP->tc_frag_data.text_expansion[0];
9394 assert (bytes == 2 || bytes == 3);
9395 build_nop (&orig_vinsn.slots[0], bytes);
9396 fragP->fr_fix += fragP->tc_frag_data.text_expansion[0];
9398 vinsn_to_insnbuf (&orig_vinsn, fr_opcode, frag_now, FALSE);
9399 xtensa_insnbuf_to_chars (isa, orig_vinsn.insnbuf, fr_opcode, 0);
9400 fragP->fr_var = 0;
9402 else if (!orig_tinsn.is_specific_opcode)
9404 /* Here is the fun stuff: Get the immediate field from this
9405 instruction. If it fits, we're done. If not, find the next
9406 instruction sequence that fits. */
9408 IStack istack;
9409 int i;
9410 symbolS *lit_sym = NULL;
9411 int total_size = 0;
9412 int target_offset = 0;
9413 int old_size;
9414 int diff;
9415 symbolS *gen_label = NULL;
9416 offsetT frag_offset;
9417 bfd_boolean first = TRUE;
9418 bfd_boolean last_is_jump;
9420 /* It does not fit. Find something that does and
9421 convert immediately. */
9422 frag_offset = fr_opcode - fragP->fr_literal;
9423 istack_init (&istack);
9424 xg_assembly_relax (&istack, &orig_tinsn,
9425 segP, fragP, frag_offset, min_steps, 0);
9427 old_size = xtensa_format_length (isa, fmt);
9429 /* Assemble this right inline. */
9431 /* First, create the mapping from a label name to the REAL label. */
9432 target_offset = 0;
9433 for (i = 0; i < istack.ninsn; i++)
9435 TInsn *tinsn = &istack.insn[i];
9436 fragS *lit_frag;
9438 switch (tinsn->insn_type)
9440 case ITYPE_LITERAL:
9441 if (lit_sym != NULL)
9442 as_bad (_("multiple literals in expansion"));
9443 /* First find the appropriate space in the literal pool. */
9444 lit_frag = fragP->tc_frag_data.literal_frags[slot];
9445 if (lit_frag == NULL)
9446 as_bad (_("no registered fragment for literal"));
9447 if (tinsn->ntok != 1)
9448 as_bad (_("number of literal tokens != 1"));
9450 /* Set the literal symbol and add a fixup. */
9451 lit_sym = lit_frag->fr_symbol;
9452 break;
9454 case ITYPE_LABEL:
9455 if (align_targets && !is_loop)
9457 fragS *unreach = fragP->fr_next;
9458 while (!(unreach->fr_type == rs_machine_dependent
9459 && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE
9460 || unreach->fr_subtype == RELAX_UNREACHABLE)))
9462 unreach = unreach->fr_next;
9465 assert (unreach->fr_type == rs_machine_dependent
9466 && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE
9467 || unreach->fr_subtype == RELAX_UNREACHABLE));
9469 target_offset += unreach->tc_frag_data.text_expansion[0];
9471 assert (gen_label == NULL);
9472 gen_label = symbol_new (FAKE_LABEL_NAME, now_seg,
9473 fr_opcode - fragP->fr_literal
9474 + target_offset, fragP);
9475 break;
9477 case ITYPE_INSN:
9478 if (first && wide_insn)
9480 target_offset += xtensa_format_length (isa, fmt);
9481 first = FALSE;
9482 if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot))
9483 target_offset += xg_get_single_size (tinsn->opcode);
9485 else
9486 target_offset += xg_get_single_size (tinsn->opcode);
9487 break;
9491 total_size = 0;
9492 first = TRUE;
9493 last_is_jump = FALSE;
9494 for (i = 0; i < istack.ninsn; i++)
9496 TInsn *tinsn = &istack.insn[i];
9497 fragS *lit_frag;
9498 int size;
9499 segT target_seg;
9500 bfd_reloc_code_real_type reloc_type;
9502 switch (tinsn->insn_type)
9504 case ITYPE_LITERAL:
9505 lit_frag = fragP->tc_frag_data.literal_frags[slot];
9506 /* Already checked. */
9507 assert (lit_frag != NULL);
9508 assert (lit_sym != NULL);
9509 assert (tinsn->ntok == 1);
9510 /* Add a fixup. */
9511 target_seg = S_GET_SEGMENT (lit_sym);
9512 assert (target_seg);
9513 if (tinsn->tok[0].X_op == O_pltrel)
9514 reloc_type = BFD_RELOC_XTENSA_PLT;
9515 else
9516 reloc_type = BFD_RELOC_32;
9517 fix_new_exp_in_seg (target_seg, 0, lit_frag, 0, 4,
9518 &tinsn->tok[0], FALSE, reloc_type);
9519 break;
9521 case ITYPE_LABEL:
9522 break;
9524 case ITYPE_INSN:
9525 xg_resolve_labels (tinsn, gen_label);
9526 xg_resolve_literals (tinsn, lit_sym);
9527 if (wide_insn && first)
9529 first = FALSE;
9530 if (opcode_fits_format_slot (tinsn->opcode, fmt, slot))
9532 tinsn->record_fix = TRUE;
9533 orig_vinsn.slots[slot] = *tinsn;
9535 else
9537 orig_vinsn.slots[slot].opcode =
9538 xtensa_format_slot_nop_opcode (isa, fmt, slot);
9539 orig_vinsn.slots[slot].ntok = 0;
9540 orig_vinsn.slots[slot].record_fix = FALSE;
9542 vinsn_to_insnbuf (&orig_vinsn, immed_instr, fragP, TRUE);
9543 xtensa_insnbuf_to_chars (isa, orig_vinsn.insnbuf,
9544 immed_instr, 0);
9545 fragP->tc_frag_data.is_insn = TRUE;
9546 size = xtensa_format_length (isa, fmt);
9547 if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot))
9549 xtensa_format single_fmt =
9550 xg_get_single_format (tinsn->opcode);
9552 xg_emit_insn_to_buf
9553 (tinsn, single_fmt, immed_instr + size, fragP,
9554 immed_instr - fragP->fr_literal + size, TRUE);
9555 size += xg_get_single_size (tinsn->opcode);
9558 else
9560 xtensa_format single_format;
9561 size = xg_get_single_size (tinsn->opcode);
9562 single_format = xg_get_single_format (tinsn->opcode);
9563 xg_emit_insn_to_buf (tinsn, single_format, immed_instr,
9564 fragP,
9565 immed_instr - fragP->fr_literal, TRUE);
9567 immed_instr += size;
9568 total_size += size;
9569 break;
9573 diff = total_size - old_size;
9574 assert (diff >= 0);
9575 if (diff != 0)
9576 expanded = TRUE;
9577 assert (diff <= fragP->fr_var);
9578 fragP->fr_var -= diff;
9579 fragP->fr_fix += diff;
9582 /* Clean it up. */
9583 xg_free_vinsn (&orig_vinsn);
9585 /* Check for undefined immediates in LOOP instructions. */
9586 if (is_loop)
9588 symbolS *sym;
9589 sym = orig_tinsn.tok[1].X_add_symbol;
9590 if (sym != NULL && !S_IS_DEFINED (sym))
9592 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
9593 return;
9595 sym = orig_tinsn.tok[1].X_op_symbol;
9596 if (sym != NULL && !S_IS_DEFINED (sym))
9598 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
9599 return;
9603 if (expanded && xtensa_opcode_is_loop (isa, orig_tinsn.opcode) == 1)
9604 convert_frag_immed_finish_loop (segP, fragP, &orig_tinsn);
9606 if (expanded && is_direct_call_opcode (orig_tinsn.opcode))
9608 /* Add an expansion note on the expanded instruction. */
9609 fix_new_exp_in_seg (now_seg, 0, fragP, fr_opcode - fragP->fr_literal, 4,
9610 &orig_tinsn.tok[0], TRUE,
9611 BFD_RELOC_XTENSA_ASM_EXPAND);
9616 /* Add a new fix expression into the desired segment. We have to
9617 switch to that segment to do this. */
9619 static fixS *
9620 fix_new_exp_in_seg (segT new_seg,
9621 subsegT new_subseg,
9622 fragS *frag,
9623 int where,
9624 int size,
9625 expressionS *exp,
9626 int pcrel,
9627 bfd_reloc_code_real_type r_type)
9629 fixS *new_fix;
9630 segT seg = now_seg;
9631 subsegT subseg = now_subseg;
9633 assert (new_seg != 0);
9634 subseg_set (new_seg, new_subseg);
9636 new_fix = fix_new_exp (frag, where, size, exp, pcrel, r_type);
9637 subseg_set (seg, subseg);
9638 return new_fix;
9642 /* Relax a loop instruction so that it can span loop >256 bytes.
9644 loop as, .L1
9645 .L0:
9646 rsr as, LEND
9647 wsr as, LBEG
9648 addi as, as, lo8 (label-.L1)
9649 addmi as, as, mid8 (label-.L1)
9650 wsr as, LEND
9651 isync
9652 rsr as, LCOUNT
9653 addi as, as, 1
9654 .L1:
9655 <<body>>
9656 label:
9659 static void
9660 convert_frag_immed_finish_loop (segT segP, fragS *fragP, TInsn *tinsn)
9662 TInsn loop_insn;
9663 TInsn addi_insn;
9664 TInsn addmi_insn;
9665 unsigned long target;
9666 static xtensa_insnbuf insnbuf = NULL;
9667 unsigned int loop_length, loop_length_hi, loop_length_lo;
9668 xtensa_isa isa = xtensa_default_isa;
9669 addressT loop_offset;
9670 addressT addi_offset = 9;
9671 addressT addmi_offset = 12;
9672 fragS *next_fragP;
9673 size_t target_count;
9675 if (!insnbuf)
9676 insnbuf = xtensa_insnbuf_alloc (isa);
9678 /* Get the loop offset. */
9679 loop_offset = get_expanded_loop_offset (tinsn->opcode);
9681 /* Validate that there really is a LOOP at the loop_offset. Because
9682 loops are not bundleable, we can assume that the instruction will be
9683 in slot 0. */
9684 tinsn_from_chars (&loop_insn, fragP->fr_opcode + loop_offset, 0);
9685 tinsn_immed_from_frag (&loop_insn, fragP, 0);
9687 assert (xtensa_opcode_is_loop (isa, loop_insn.opcode) == 1);
9688 addi_offset += loop_offset;
9689 addmi_offset += loop_offset;
9691 assert (tinsn->ntok == 2);
9692 if (tinsn->tok[1].X_op == O_constant)
9693 target = tinsn->tok[1].X_add_number;
9694 else if (tinsn->tok[1].X_op == O_symbol)
9696 /* Find the fragment. */
9697 symbolS *sym = tinsn->tok[1].X_add_symbol;
9698 assert (S_GET_SEGMENT (sym) == segP
9699 || S_GET_SEGMENT (sym) == absolute_section);
9700 target = (S_GET_VALUE (sym) + tinsn->tok[1].X_add_number);
9702 else
9704 as_bad (_("invalid expression evaluation type %d"), tinsn->tok[1].X_op);
9705 target = 0;
9708 know (symbolP);
9709 know (symbolP->sy_frag);
9710 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
9711 || symbol_get_frag (symbolP) == &zero_address_frag);
9713 loop_length = target - (fragP->fr_address + fragP->fr_fix);
9714 loop_length_hi = loop_length & ~0x0ff;
9715 loop_length_lo = loop_length & 0x0ff;
9716 if (loop_length_lo >= 128)
9718 loop_length_lo -= 256;
9719 loop_length_hi += 256;
9722 /* Because addmi sign-extends the immediate, 'loop_length_hi' can be at most
9723 32512. If the loop is larger than that, then we just fail. */
9724 if (loop_length_hi > 32512)
9725 as_bad_where (fragP->fr_file, fragP->fr_line,
9726 _("loop too long for LOOP instruction"));
9728 tinsn_from_chars (&addi_insn, fragP->fr_opcode + addi_offset, 0);
9729 assert (addi_insn.opcode == xtensa_addi_opcode);
9731 tinsn_from_chars (&addmi_insn, fragP->fr_opcode + addmi_offset, 0);
9732 assert (addmi_insn.opcode == xtensa_addmi_opcode);
9734 set_expr_const (&addi_insn.tok[2], loop_length_lo);
9735 tinsn_to_insnbuf (&addi_insn, insnbuf);
9737 fragP->tc_frag_data.is_insn = TRUE;
9738 xtensa_insnbuf_to_chars (isa, insnbuf, fragP->fr_opcode + addi_offset, 0);
9740 set_expr_const (&addmi_insn.tok[2], loop_length_hi);
9741 tinsn_to_insnbuf (&addmi_insn, insnbuf);
9742 xtensa_insnbuf_to_chars (isa, insnbuf, 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. */
9749 target_count = 0;
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)
9755 target_count++;
9756 if (target_count == 2)
9757 break;
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
9765 the relaxation. */
9767 typedef struct subseg_map_struct
9769 /* the key */
9770 segT seg;
9771 subsegT subseg;
9773 /* the data */
9774 unsigned flags;
9775 float total_freq; /* fall-through + branch target frequency */
9776 float target_freq; /* branch target frequency alone */
9778 struct subseg_map_struct *next;
9779 } subseg_map;
9782 static subseg_map *sseg_map = NULL;
9784 static subseg_map *
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)
9792 break;
9794 return subseg_e;
9798 static subseg_map *
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;
9811 return subseg_e;
9815 static unsigned
9816 get_last_insn_flags (segT seg, subsegT subseg)
9818 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9819 if (subseg_e)
9820 return subseg_e->flags;
9821 return 0;
9825 static void
9826 set_last_insn_flags (segT seg,
9827 subsegT subseg,
9828 unsigned fl,
9829 bfd_boolean val)
9831 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9832 if (! subseg_e)
9833 subseg_e = add_subseg_info (seg, subseg);
9834 if (val)
9835 subseg_e->flags |= fl;
9836 else
9837 subseg_e->flags &= ~fl;
9841 static float
9842 get_subseg_total_freq (segT seg, subsegT subseg)
9844 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9845 if (subseg_e)
9846 return subseg_e->total_freq;
9847 return 1.0;
9851 static float
9852 get_subseg_target_freq (segT seg, subsegT subseg)
9854 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9855 if (subseg_e)
9856 return subseg_e->target_freq;
9857 return 1.0;
9861 static void
9862 set_subseg_freq (segT seg, subsegT subseg, float total_f, float target_f)
9864 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9865 if (! subseg_e)
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. */
9874 /* Remove the segment from the global sections list. */
9876 static void
9877 xtensa_remove_section (segT sec)
9879 /* Handle brain-dead bfd_section_list_remove macro, which
9880 expect the address of the prior section's "next" field, not
9881 just the address of the section to remove. */
9883 segT *ps_next_ptr = &stdoutput->sections;
9884 while (*ps_next_ptr != sec && *ps_next_ptr != NULL)
9885 ps_next_ptr = &(*ps_next_ptr)->next;
9887 assert (*ps_next_ptr != NULL);
9889 bfd_section_list_remove (stdoutput, ps_next_ptr);
9893 static void
9894 xtensa_insert_section (segT after_sec, segT sec)
9896 segT *after_sec_next;
9897 if (after_sec == NULL)
9898 after_sec_next = &stdoutput->sections;
9899 else
9900 after_sec_next = &after_sec->next;
9902 bfd_section_list_insert (stdoutput, after_sec_next, sec);
9906 static void
9907 xtensa_move_seg_list_to_beginning (seg_list *head)
9909 head = head->next;
9910 while (head)
9912 segT literal_section = head->seg;
9914 /* Move the literal section to the front of the section list. */
9915 assert (literal_section);
9916 xtensa_remove_section (literal_section);
9917 xtensa_insert_section (NULL, literal_section);
9919 head = head->next;
9924 static void mark_literal_frags (seg_list *);
9926 static void
9927 xtensa_move_literals (void)
9929 seg_list *segment;
9930 frchainS *frchain_from, *frchain_to;
9931 fragS *search_frag, *next_frag, *last_frag, *literal_pool, *insert_after;
9932 fragS **frag_splice;
9933 emit_state state;
9934 segT dest_seg;
9935 fixS *fix, *next_fix, **fix_splice;
9936 sym_list *lit;
9938 mark_literal_frags (literal_head->next);
9939 mark_literal_frags (init_literal_head->next);
9940 mark_literal_frags (fini_literal_head->next);
9942 if (use_literal_section)
9943 return;
9945 segment = literal_head->next;
9946 while (segment)
9948 frchain_from = seg_info (segment->seg)->frchainP;
9949 search_frag = frchain_from->frch_root;
9950 literal_pool = NULL;
9951 frchain_to = NULL;
9952 frag_splice = &(frchain_from->frch_root);
9954 while (!search_frag->tc_frag_data.literal_frag)
9956 assert (search_frag->fr_fix == 0
9957 || search_frag->fr_type == rs_align);
9958 search_frag = search_frag->fr_next;
9961 assert (search_frag->tc_frag_data.literal_frag->fr_subtype
9962 == RELAX_LITERAL_POOL_BEGIN);
9963 xtensa_switch_section_emit_state (&state, segment->seg, 0);
9965 /* Make sure that all the frags in this series are closed, and
9966 that there is at least one left over of zero-size. This
9967 prevents us from making a segment with an frchain without any
9968 frags in it. */
9969 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
9970 xtensa_set_frag_assembly_state (frag_now);
9971 last_frag = frag_now;
9972 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
9973 xtensa_set_frag_assembly_state (frag_now);
9975 while (search_frag != frag_now)
9977 next_frag = search_frag->fr_next;
9979 /* First, move the frag out of the literal section and
9980 to the appropriate place. */
9981 if (search_frag->tc_frag_data.literal_frag)
9983 literal_pool = search_frag->tc_frag_data.literal_frag;
9984 assert (literal_pool->fr_subtype == RELAX_LITERAL_POOL_BEGIN);
9985 /* Note that we set this fr_var to be a fix
9986 chain when we created the literal pool location
9987 as RELAX_LITERAL_POOL_BEGIN. */
9988 frchain_to = (frchainS *) literal_pool->fr_var;
9990 insert_after = literal_pool;
9992 while (insert_after->fr_next->fr_subtype != RELAX_LITERAL_POOL_END)
9993 insert_after = insert_after->fr_next;
9995 dest_seg = (segT) insert_after->fr_next->fr_var;
9997 *frag_splice = next_frag;
9998 search_frag->fr_next = insert_after->fr_next;
9999 insert_after->fr_next = search_frag;
10000 search_frag->tc_frag_data.lit_seg = dest_seg;
10002 /* Now move any fixups associated with this frag to the
10003 right section. */
10004 fix = frchain_from->fix_root;
10005 fix_splice = &(frchain_from->fix_root);
10006 while (fix)
10008 next_fix = fix->fx_next;
10009 if (fix->fx_frag == search_frag)
10011 *fix_splice = next_fix;
10012 fix->fx_next = frchain_to->fix_root;
10013 frchain_to->fix_root = fix;
10014 if (frchain_to->fix_tail == NULL)
10015 frchain_to->fix_tail = fix;
10017 else
10018 fix_splice = &(fix->fx_next);
10019 fix = next_fix;
10021 search_frag = next_frag;
10024 if (frchain_from->fix_root != NULL)
10026 frchain_from = seg_info (segment->seg)->frchainP;
10027 as_warn (_("fixes not all moved from %s"), segment->seg->name);
10029 assert (frchain_from->fix_root == NULL);
10031 frchain_from->fix_tail = NULL;
10032 xtensa_restore_emit_state (&state);
10033 segment = segment->next;
10036 /* Now fix up the SEGMENT value for all the literal symbols. */
10037 for (lit = literal_syms; lit; lit = lit->next)
10039 symbolS *lit_sym = lit->sym;
10040 segT dest_seg = symbol_get_frag (lit_sym)->tc_frag_data.lit_seg;
10041 if (dest_seg)
10042 S_SET_SEGMENT (lit_sym, dest_seg);
10047 /* Walk over all the frags for segments in a list and mark them as
10048 containing literals. As clunky as this is, we can't rely on frag_var
10049 and frag_variant to get called in all situations. */
10051 static void
10052 mark_literal_frags (seg_list *segment)
10054 frchainS *frchain_from;
10055 fragS *search_frag;
10057 while (segment)
10059 frchain_from = seg_info (segment->seg)->frchainP;
10060 search_frag = frchain_from->frch_root;
10061 while (search_frag)
10063 search_frag->tc_frag_data.is_literal = TRUE;
10064 search_frag = search_frag->fr_next;
10066 segment = segment->next;
10071 static void
10072 xtensa_reorder_seg_list (seg_list *head, segT after)
10074 /* Move all of the sections in the section list to come
10075 after "after" in the gnu segment list. */
10077 head = head->next;
10078 while (head)
10080 segT literal_section = head->seg;
10082 /* Move the literal section after "after". */
10083 assert (literal_section);
10084 if (literal_section != after)
10086 xtensa_remove_section (literal_section);
10087 xtensa_insert_section (after, literal_section);
10090 head = head->next;
10095 /* Push all the literal segments to the end of the gnu list. */
10097 static void
10098 xtensa_reorder_segments (void)
10100 segT sec;
10101 segT last_sec = 0;
10102 int old_count = 0;
10103 int new_count = 0;
10105 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
10107 last_sec = sec;
10108 old_count++;
10111 /* Now that we have the last section, push all the literal
10112 sections to the end. */
10113 xtensa_reorder_seg_list (literal_head, last_sec);
10114 xtensa_reorder_seg_list (init_literal_head, last_sec);
10115 xtensa_reorder_seg_list (fini_literal_head, last_sec);
10117 /* Now perform the final error check. */
10118 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
10119 new_count++;
10120 assert (new_count == old_count);
10124 /* Change the emit state (seg, subseg, and frag related stuff) to the
10125 correct location. Return a emit_state which can be passed to
10126 xtensa_restore_emit_state to return to current fragment. */
10128 static void
10129 xtensa_switch_to_literal_fragment (emit_state *result)
10131 if (directive_state[directive_absolute_literals])
10133 cache_literal_section (0, default_lit_sections.lit4_seg_name,
10134 &default_lit_sections.lit4_seg, FALSE);
10135 xtensa_switch_section_emit_state (result,
10136 default_lit_sections.lit4_seg, 0);
10138 else
10139 xtensa_switch_to_non_abs_literal_fragment (result);
10141 /* Do a 4-byte align here. */
10142 frag_align (2, 0, 0);
10143 record_alignment (now_seg, 2);
10147 static void
10148 xtensa_switch_to_non_abs_literal_fragment (emit_state *result)
10150 /* When we mark a literal pool location, we want to put a frag in
10151 the literal pool that points to it. But to do that, we want to
10152 switch_to_literal_fragment. But literal sections don't have
10153 literal pools, so their location is always null, so we would
10154 recurse forever. This is kind of hacky, but it works. */
10156 static bfd_boolean recursive = FALSE;
10157 fragS *pool_location = get_literal_pool_location (now_seg);
10158 bfd_boolean is_init =
10159 (now_seg && !strcmp (segment_name (now_seg), INIT_SECTION_NAME));
10161 bfd_boolean is_fini =
10162 (now_seg && !strcmp (segment_name (now_seg), FINI_SECTION_NAME));
10164 if (pool_location == NULL
10165 && !use_literal_section
10166 && !recursive
10167 && !is_init && ! is_fini)
10169 as_bad (_("literal pool location required for text-section-literals; specify with .literal_position"));
10170 recursive = TRUE;
10171 xtensa_mark_literal_pool_location ();
10172 recursive = FALSE;
10175 /* Special case: If we are in the ".fini" or ".init" section, then
10176 we will ALWAYS be generating to the ".fini.literal" and
10177 ".init.literal" sections. */
10179 if (is_init)
10181 cache_literal_section (init_literal_head,
10182 default_lit_sections.init_lit_seg_name,
10183 &default_lit_sections.init_lit_seg, TRUE);
10184 xtensa_switch_section_emit_state (result,
10185 default_lit_sections.init_lit_seg, 0);
10187 else if (is_fini)
10189 cache_literal_section (fini_literal_head,
10190 default_lit_sections.fini_lit_seg_name,
10191 &default_lit_sections.fini_lit_seg, TRUE);
10192 xtensa_switch_section_emit_state (result,
10193 default_lit_sections.fini_lit_seg, 0);
10195 else
10197 cache_literal_section (literal_head,
10198 default_lit_sections.lit_seg_name,
10199 &default_lit_sections.lit_seg, TRUE);
10200 xtensa_switch_section_emit_state (result,
10201 default_lit_sections.lit_seg, 0);
10204 if (!use_literal_section
10205 && !is_init && !is_fini
10206 && get_literal_pool_location (now_seg) != pool_location)
10208 /* Close whatever frag is there. */
10209 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
10210 xtensa_set_frag_assembly_state (frag_now);
10211 frag_now->tc_frag_data.literal_frag = pool_location;
10212 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
10213 xtensa_set_frag_assembly_state (frag_now);
10218 /* Call this function before emitting data into the literal section.
10219 This is a helper function for xtensa_switch_to_literal_fragment.
10220 This is similar to a .section new_now_seg subseg. */
10222 static void
10223 xtensa_switch_section_emit_state (emit_state *state,
10224 segT new_now_seg,
10225 subsegT new_now_subseg)
10227 state->name = now_seg->name;
10228 state->now_seg = now_seg;
10229 state->now_subseg = now_subseg;
10230 state->generating_literals = generating_literals;
10231 generating_literals++;
10232 subseg_set (new_now_seg, new_now_subseg);
10236 /* Use to restore the emitting into the normal place. */
10238 static void
10239 xtensa_restore_emit_state (emit_state *state)
10241 generating_literals = state->generating_literals;
10242 subseg_set (state->now_seg, state->now_subseg);
10246 /* Get a segment of a given name. If the segment is already
10247 present, return it; otherwise, create a new one. */
10249 static void
10250 cache_literal_section (seg_list *head,
10251 const char *name,
10252 segT *pseg,
10253 bfd_boolean is_code)
10255 segT current_section = now_seg;
10256 int current_subsec = now_subseg;
10257 segT seg;
10259 if (*pseg != 0)
10260 return;
10262 /* Check if the named section exists. */
10263 for (seg = stdoutput->sections; seg; seg = seg->next)
10265 if (!strcmp (segment_name (seg), name))
10266 break;
10269 if (!seg)
10271 /* Create a new literal section. */
10272 seg = subseg_new (name, (subsegT) 0);
10273 if (head)
10275 /* Add the newly created literal segment to the specified list. */
10276 seg_list *n = (seg_list *) xmalloc (sizeof (seg_list));
10277 n->seg = seg;
10278 n->next = head->next;
10279 head->next = n;
10281 bfd_set_section_flags (stdoutput, seg, SEC_HAS_CONTENTS |
10282 SEC_READONLY | SEC_ALLOC | SEC_LOAD
10283 | (is_code ? SEC_CODE : SEC_DATA));
10284 bfd_set_section_alignment (stdoutput, seg, 2);
10287 *pseg = seg;
10288 subseg_set (current_section, current_subsec);
10292 /* Property Tables Stuff. */
10294 #define XTENSA_INSN_SEC_NAME ".xt.insn"
10295 #define XTENSA_LIT_SEC_NAME ".xt.lit"
10296 #define XTENSA_PROP_SEC_NAME ".xt.prop"
10298 typedef bfd_boolean (*frag_predicate) (const fragS *);
10299 typedef void (*frag_flags_fn) (const fragS *, frag_flags *);
10301 static bfd_boolean get_frag_is_literal (const fragS *);
10302 static void xtensa_create_property_segments
10303 (frag_predicate, frag_predicate, const char *, xt_section_type);
10304 static void xtensa_create_xproperty_segments
10305 (frag_flags_fn, const char *, xt_section_type);
10306 static segment_info_type *retrieve_segment_info (segT);
10307 static segT retrieve_xtensa_section (char *);
10308 static bfd_boolean section_has_property (segT, frag_predicate);
10309 static bfd_boolean section_has_xproperty (segT, frag_flags_fn);
10310 static void add_xt_block_frags
10311 (segT, segT, xtensa_block_info **, frag_predicate, frag_predicate);
10312 static bfd_boolean xtensa_frag_flags_is_empty (const frag_flags *);
10313 static void xtensa_frag_flags_init (frag_flags *);
10314 static void get_frag_property_flags (const fragS *, frag_flags *);
10315 static bfd_vma frag_flags_to_number (const frag_flags *);
10316 static void add_xt_prop_frags
10317 (segT, segT, xtensa_block_info **, frag_flags_fn);
10319 /* Set up property tables after relaxation. */
10321 void
10322 xtensa_post_relax_hook (void)
10324 xtensa_move_seg_list_to_beginning (literal_head);
10325 xtensa_move_seg_list_to_beginning (init_literal_head);
10326 xtensa_move_seg_list_to_beginning (fini_literal_head);
10328 xtensa_find_unmarked_state_frags ();
10330 if (use_literal_section)
10331 xtensa_create_property_segments (get_frag_is_literal,
10332 NULL,
10333 XTENSA_LIT_SEC_NAME,
10334 xt_literal_sec);
10335 xtensa_create_xproperty_segments (get_frag_property_flags,
10336 XTENSA_PROP_SEC_NAME,
10337 xt_prop_sec);
10339 if (warn_unaligned_branch_targets)
10340 bfd_map_over_sections (stdoutput, xtensa_find_unaligned_branch_targets, 0);
10341 bfd_map_over_sections (stdoutput, xtensa_find_unaligned_loops, 0);
10345 /* This function is only meaningful after xtensa_move_literals. */
10347 static bfd_boolean
10348 get_frag_is_literal (const fragS *fragP)
10350 assert (fragP != NULL);
10351 return fragP->tc_frag_data.is_literal;
10355 static void
10356 xtensa_create_property_segments (frag_predicate property_function,
10357 frag_predicate end_property_function,
10358 const char *section_name_base,
10359 xt_section_type sec_type)
10361 segT *seclist;
10363 /* Walk over all of the current segments.
10364 Walk over each fragment
10365 For each non-empty fragment,
10366 Build a property record (append where possible). */
10368 for (seclist = &stdoutput->sections;
10369 seclist && *seclist;
10370 seclist = &(*seclist)->next)
10372 segT sec = *seclist;
10373 flagword flags;
10375 flags = bfd_get_section_flags (stdoutput, sec);
10376 if (flags & SEC_DEBUGGING)
10377 continue;
10378 if (!(flags & SEC_ALLOC))
10379 continue;
10381 if (section_has_property (sec, property_function))
10383 char *property_section_name =
10384 xtensa_get_property_section_name (sec, section_name_base);
10385 segT insn_sec = retrieve_xtensa_section (property_section_name);
10386 segment_info_type *xt_seg_info = retrieve_segment_info (insn_sec);
10387 xtensa_block_info **xt_blocks =
10388 &xt_seg_info->tc_segment_info_data.blocks[sec_type];
10389 /* Walk over all of the frchains here and add new sections. */
10390 add_xt_block_frags (sec, insn_sec, xt_blocks, property_function,
10391 end_property_function);
10395 /* Now we fill them out.... */
10397 for (seclist = &stdoutput->sections;
10398 seclist && *seclist;
10399 seclist = &(*seclist)->next)
10401 segment_info_type *seginfo;
10402 xtensa_block_info *block;
10403 segT sec = *seclist;
10405 seginfo = seg_info (sec);
10406 block = seginfo->tc_segment_info_data.blocks[sec_type];
10408 if (block)
10410 xtensa_block_info *cur_block;
10411 /* This is a section with some data. */
10412 int num_recs = 0;
10413 size_t rec_size;
10415 for (cur_block = block; cur_block; cur_block = cur_block->next)
10416 num_recs++;
10418 rec_size = num_recs * 8;
10419 bfd_set_section_size (stdoutput, sec, rec_size);
10421 /* In order to make this work with the assembler, we have to
10422 build some frags and then build the "fixups" for it. It
10423 would be easier to just set the contents then set the
10424 arlents. */
10426 if (num_recs)
10428 /* Allocate a fragment and leak it. */
10429 fragS *fragP;
10430 size_t frag_size;
10431 fixS *fixes;
10432 frchainS *frchainP;
10433 int i;
10434 char *frag_data;
10436 frag_size = sizeof (fragS) + rec_size;
10437 fragP = (fragS *) xmalloc (frag_size);
10439 memset (fragP, 0, frag_size);
10440 fragP->fr_address = 0;
10441 fragP->fr_next = NULL;
10442 fragP->fr_fix = rec_size;
10443 fragP->fr_var = 0;
10444 fragP->fr_type = rs_fill;
10445 /* The rest are zeros. */
10447 frchainP = seginfo->frchainP;
10448 frchainP->frch_root = fragP;
10449 frchainP->frch_last = fragP;
10451 fixes = (fixS *) xmalloc (sizeof (fixS) * num_recs);
10452 memset (fixes, 0, sizeof (fixS) * num_recs);
10454 seginfo->fix_root = fixes;
10455 seginfo->fix_tail = &fixes[num_recs - 1];
10456 cur_block = block;
10457 frag_data = &fragP->fr_literal[0];
10458 for (i = 0; i < num_recs; i++)
10460 fixS *fix = &fixes[i];
10461 assert (cur_block);
10463 /* Write the fixup. */
10464 if (i != num_recs - 1)
10465 fix->fx_next = &fixes[i + 1];
10466 else
10467 fix->fx_next = NULL;
10468 fix->fx_size = 4;
10469 fix->fx_done = 0;
10470 fix->fx_frag = fragP;
10471 fix->fx_where = i * 8;
10472 fix->fx_addsy = section_symbol (cur_block->sec);
10473 fix->fx_offset = cur_block->offset;
10474 fix->fx_r_type = BFD_RELOC_32;
10475 fix->fx_file = "Internal Assembly";
10476 fix->fx_line = 0;
10478 /* Write the length. */
10479 md_number_to_chars (&frag_data[4 + 8 * i],
10480 cur_block->size, 4);
10481 cur_block = cur_block->next;
10489 static void
10490 xtensa_create_xproperty_segments (frag_flags_fn flag_fn,
10491 const char *section_name_base,
10492 xt_section_type sec_type)
10494 segT *seclist;
10496 /* Walk over all of the current segments.
10497 Walk over each fragment.
10498 For each fragment that has instructions,
10499 build an instruction record (append where possible). */
10501 for (seclist = &stdoutput->sections;
10502 seclist && *seclist;
10503 seclist = &(*seclist)->next)
10505 segT sec = *seclist;
10506 flagword flags;
10508 flags = bfd_get_section_flags (stdoutput, sec);
10509 if (flags & SEC_DEBUGGING)
10510 continue;
10511 if (!(flags & SEC_ALLOC))
10512 continue;
10514 if (section_has_xproperty (sec, flag_fn))
10516 char *property_section_name =
10517 xtensa_get_property_section_name (sec, section_name_base);
10518 segT insn_sec = retrieve_xtensa_section (property_section_name);
10519 segment_info_type *xt_seg_info = retrieve_segment_info (insn_sec);
10520 xtensa_block_info **xt_blocks =
10521 &xt_seg_info->tc_segment_info_data.blocks[sec_type];
10522 /* Walk over all of the frchains here and add new sections. */
10523 add_xt_prop_frags (sec, insn_sec, xt_blocks, flag_fn);
10527 /* Now we fill them out.... */
10529 for (seclist = &stdoutput->sections;
10530 seclist && *seclist;
10531 seclist = &(*seclist)->next)
10533 segment_info_type *seginfo;
10534 xtensa_block_info *block;
10535 segT sec = *seclist;
10537 seginfo = seg_info (sec);
10538 block = seginfo->tc_segment_info_data.blocks[sec_type];
10540 if (block)
10542 xtensa_block_info *cur_block;
10543 /* This is a section with some data. */
10544 int num_recs = 0;
10545 size_t rec_size;
10547 for (cur_block = block; cur_block; cur_block = cur_block->next)
10548 num_recs++;
10550 rec_size = num_recs * (8 + 4);
10551 bfd_set_section_size (stdoutput, sec, rec_size);
10553 /* elf_section_data (sec)->this_hdr.sh_entsize = 12; */
10555 /* In order to make this work with the assembler, we have to build
10556 some frags then build the "fixups" for it. It would be easier to
10557 just set the contents then set the arlents. */
10559 if (num_recs)
10561 /* Allocate a fragment and (unfortunately) leak it. */
10562 fragS *fragP;
10563 size_t frag_size;
10564 fixS *fixes;
10565 frchainS *frchainP;
10566 int i;
10567 char *frag_data;
10569 frag_size = sizeof (fragS) + rec_size;
10570 fragP = (fragS *) xmalloc (frag_size);
10572 memset (fragP, 0, frag_size);
10573 fragP->fr_address = 0;
10574 fragP->fr_next = NULL;
10575 fragP->fr_fix = rec_size;
10576 fragP->fr_var = 0;
10577 fragP->fr_type = rs_fill;
10578 /* The rest are zeros. */
10580 frchainP = seginfo->frchainP;
10581 frchainP->frch_root = fragP;
10582 frchainP->frch_last = fragP;
10584 fixes = (fixS *) xmalloc (sizeof (fixS) * num_recs);
10585 memset (fixes, 0, sizeof (fixS) * num_recs);
10587 seginfo->fix_root = fixes;
10588 seginfo->fix_tail = &fixes[num_recs - 1];
10589 cur_block = block;
10590 frag_data = &fragP->fr_literal[0];
10591 for (i = 0; i < num_recs; i++)
10593 fixS *fix = &fixes[i];
10594 assert (cur_block);
10596 /* Write the fixup. */
10597 if (i != num_recs - 1)
10598 fix->fx_next = &fixes[i + 1];
10599 else
10600 fix->fx_next = NULL;
10601 fix->fx_size = 4;
10602 fix->fx_done = 0;
10603 fix->fx_frag = fragP;
10604 fix->fx_where = i * (8 + 4);
10605 fix->fx_addsy = section_symbol (cur_block->sec);
10606 fix->fx_offset = cur_block->offset;
10607 fix->fx_r_type = BFD_RELOC_32;
10608 fix->fx_file = "Internal Assembly";
10609 fix->fx_line = 0;
10611 /* Write the length. */
10612 md_number_to_chars (&frag_data[4 + (8+4) * i],
10613 cur_block->size, 4);
10614 md_number_to_chars (&frag_data[8 + (8+4) * i],
10615 frag_flags_to_number (&cur_block->flags),
10617 cur_block = cur_block->next;
10625 static segment_info_type *
10626 retrieve_segment_info (segT seg)
10628 segment_info_type *seginfo;
10629 seginfo = (segment_info_type *) bfd_get_section_userdata (stdoutput, seg);
10630 if (!seginfo)
10632 frchainS *frchainP;
10634 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
10635 memset ((void *) seginfo, 0, sizeof (*seginfo));
10636 seginfo->fix_root = NULL;
10637 seginfo->fix_tail = NULL;
10638 seginfo->bfd_section = seg;
10639 seginfo->sym = 0;
10640 /* We will not be dealing with these, only our special ones. */
10641 bfd_set_section_userdata (stdoutput, seg, (void *) seginfo);
10643 frchainP = (frchainS *) xmalloc (sizeof (frchainS));
10644 frchainP->frch_root = NULL;
10645 frchainP->frch_last = NULL;
10646 frchainP->frch_next = NULL;
10647 frchainP->frch_seg = seg;
10648 frchainP->frch_subseg = 0;
10649 frchainP->fix_root = NULL;
10650 frchainP->fix_tail = NULL;
10651 /* Do not init the objstack. */
10652 /* obstack_begin (&frchainP->frch_obstack, chunksize); */
10653 /* frchainP->frch_frag_now = fragP; */
10654 frchainP->frch_frag_now = NULL;
10656 seginfo->frchainP = frchainP;
10659 return seginfo;
10663 static segT
10664 retrieve_xtensa_section (char *sec_name)
10666 bfd *abfd = stdoutput;
10667 flagword flags, out_flags, link_once_flags;
10668 segT s;
10670 flags = bfd_get_section_flags (abfd, now_seg);
10671 link_once_flags = (flags & SEC_LINK_ONCE);
10672 if (link_once_flags)
10673 link_once_flags |= (flags & SEC_LINK_DUPLICATES);
10674 out_flags = (SEC_RELOC | SEC_HAS_CONTENTS | SEC_READONLY | link_once_flags);
10676 s = bfd_make_section_old_way (abfd, sec_name);
10677 if (s == NULL)
10678 as_bad (_("could not create section %s"), sec_name);
10679 if (!bfd_set_section_flags (abfd, s, out_flags))
10680 as_bad (_("invalid flag combination on section %s"), sec_name);
10682 return s;
10686 static bfd_boolean
10687 section_has_property (segT sec, frag_predicate property_function)
10689 segment_info_type *seginfo = seg_info (sec);
10690 fragS *fragP;
10692 if (seginfo && seginfo->frchainP)
10694 for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
10696 if (property_function (fragP)
10697 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
10698 return TRUE;
10701 return FALSE;
10705 static bfd_boolean
10706 section_has_xproperty (segT sec, frag_flags_fn property_function)
10708 segment_info_type *seginfo = seg_info (sec);
10709 fragS *fragP;
10711 if (seginfo && seginfo->frchainP)
10713 for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
10715 frag_flags prop_flags;
10716 property_function (fragP, &prop_flags);
10717 if (!xtensa_frag_flags_is_empty (&prop_flags))
10718 return TRUE;
10721 return FALSE;
10725 /* Two types of block sections exist right now: literal and insns. */
10727 static void
10728 add_xt_block_frags (segT sec,
10729 segT xt_block_sec,
10730 xtensa_block_info **xt_block,
10731 frag_predicate property_function,
10732 frag_predicate end_property_function)
10734 segment_info_type *seg_info;
10735 segment_info_type *xt_seg_info;
10736 bfd_vma seg_offset;
10737 fragS *fragP;
10739 xt_seg_info = retrieve_segment_info (xt_block_sec);
10740 seg_info = retrieve_segment_info (sec);
10742 /* Build it if needed. */
10743 while (*xt_block != NULL)
10744 xt_block = &(*xt_block)->next;
10745 /* We are either at NULL at the beginning or at the end. */
10747 /* Walk through the frags. */
10748 seg_offset = 0;
10750 if (seg_info->frchainP)
10752 for (fragP = seg_info->frchainP->frch_root;
10753 fragP;
10754 fragP = fragP->fr_next)
10756 if (property_function (fragP)
10757 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
10759 if (*xt_block != NULL)
10761 if ((*xt_block)->offset + (*xt_block)->size
10762 == fragP->fr_address)
10763 (*xt_block)->size += fragP->fr_fix;
10764 else
10765 xt_block = &((*xt_block)->next);
10767 if (*xt_block == NULL)
10769 xtensa_block_info *new_block = (xtensa_block_info *)
10770 xmalloc (sizeof (xtensa_block_info));
10771 new_block->sec = sec;
10772 new_block->offset = fragP->fr_address;
10773 new_block->size = fragP->fr_fix;
10774 new_block->next = NULL;
10775 xtensa_frag_flags_init (&new_block->flags);
10776 *xt_block = new_block;
10778 if (end_property_function
10779 && end_property_function (fragP))
10781 xt_block = &((*xt_block)->next);
10789 /* Break the encapsulation of add_xt_prop_frags here. */
10791 static bfd_boolean
10792 xtensa_frag_flags_is_empty (const frag_flags *prop_flags)
10794 if (prop_flags->is_literal
10795 || prop_flags->is_insn
10796 || prop_flags->is_data
10797 || prop_flags->is_unreachable)
10798 return FALSE;
10799 return TRUE;
10803 static void
10804 xtensa_frag_flags_init (frag_flags *prop_flags)
10806 memset (prop_flags, 0, sizeof (frag_flags));
10810 static void
10811 get_frag_property_flags (const fragS *fragP, frag_flags *prop_flags)
10813 xtensa_frag_flags_init (prop_flags);
10814 if (fragP->tc_frag_data.is_literal)
10815 prop_flags->is_literal = TRUE;
10816 if (fragP->tc_frag_data.is_unreachable)
10817 prop_flags->is_unreachable = TRUE;
10818 else if (fragP->tc_frag_data.is_insn)
10820 prop_flags->is_insn = TRUE;
10821 if (fragP->tc_frag_data.is_loop_target)
10822 prop_flags->insn.is_loop_target = TRUE;
10823 if (fragP->tc_frag_data.is_branch_target)
10824 prop_flags->insn.is_branch_target = TRUE;
10825 if (fragP->tc_frag_data.is_specific_opcode
10826 || fragP->tc_frag_data.is_no_transform)
10827 prop_flags->insn.is_no_transform = TRUE;
10828 if (fragP->tc_frag_data.is_no_density)
10829 prop_flags->insn.is_no_density = TRUE;
10830 if (fragP->tc_frag_data.use_absolute_literals)
10831 prop_flags->insn.is_abslit = TRUE;
10833 if (fragP->tc_frag_data.is_align)
10835 prop_flags->is_align = TRUE;
10836 prop_flags->alignment = fragP->tc_frag_data.alignment;
10837 if (xtensa_frag_flags_is_empty (prop_flags))
10838 prop_flags->is_data = TRUE;
10843 static bfd_vma
10844 frag_flags_to_number (const frag_flags *prop_flags)
10846 bfd_vma num = 0;
10847 if (prop_flags->is_literal)
10848 num |= XTENSA_PROP_LITERAL;
10849 if (prop_flags->is_insn)
10850 num |= XTENSA_PROP_INSN;
10851 if (prop_flags->is_data)
10852 num |= XTENSA_PROP_DATA;
10853 if (prop_flags->is_unreachable)
10854 num |= XTENSA_PROP_UNREACHABLE;
10855 if (prop_flags->insn.is_loop_target)
10856 num |= XTENSA_PROP_INSN_LOOP_TARGET;
10857 if (prop_flags->insn.is_branch_target)
10859 num |= XTENSA_PROP_INSN_BRANCH_TARGET;
10860 num = SET_XTENSA_PROP_BT_ALIGN (num, prop_flags->insn.bt_align_priority);
10863 if (prop_flags->insn.is_no_density)
10864 num |= XTENSA_PROP_INSN_NO_DENSITY;
10865 if (prop_flags->insn.is_no_transform)
10866 num |= XTENSA_PROP_INSN_NO_TRANSFORM;
10867 if (prop_flags->insn.is_no_reorder)
10868 num |= XTENSA_PROP_INSN_NO_REORDER;
10869 if (prop_flags->insn.is_abslit)
10870 num |= XTENSA_PROP_INSN_ABSLIT;
10872 if (prop_flags->is_align)
10874 num |= XTENSA_PROP_ALIGN;
10875 num = SET_XTENSA_PROP_ALIGNMENT (num, prop_flags->alignment);
10878 return num;
10882 static bfd_boolean
10883 xtensa_frag_flags_combinable (const frag_flags *prop_flags_1,
10884 const frag_flags *prop_flags_2)
10886 /* Cannot combine with an end marker. */
10888 if (prop_flags_1->is_literal != prop_flags_2->is_literal)
10889 return FALSE;
10890 if (prop_flags_1->is_insn != prop_flags_2->is_insn)
10891 return FALSE;
10892 if (prop_flags_1->is_data != prop_flags_2->is_data)
10893 return FALSE;
10895 if (prop_flags_1->is_insn)
10897 /* Properties of the beginning of the frag. */
10898 if (prop_flags_2->insn.is_loop_target)
10899 return FALSE;
10900 if (prop_flags_2->insn.is_branch_target)
10901 return FALSE;
10902 if (prop_flags_1->insn.is_no_density !=
10903 prop_flags_2->insn.is_no_density)
10904 return FALSE;
10905 if (prop_flags_1->insn.is_no_transform !=
10906 prop_flags_2->insn.is_no_transform)
10907 return FALSE;
10908 if (prop_flags_1->insn.is_no_reorder !=
10909 prop_flags_2->insn.is_no_reorder)
10910 return FALSE;
10911 if (prop_flags_1->insn.is_abslit !=
10912 prop_flags_2->insn.is_abslit)
10913 return FALSE;
10916 if (prop_flags_1->is_align)
10917 return FALSE;
10919 return TRUE;
10923 static bfd_vma
10924 xt_block_aligned_size (const xtensa_block_info *xt_block)
10926 bfd_vma end_addr;
10927 size_t align_bits;
10929 if (!xt_block->flags.is_align)
10930 return xt_block->size;
10932 end_addr = xt_block->offset + xt_block->size;
10933 align_bits = xt_block->flags.alignment;
10934 end_addr = ((end_addr + ((1 << align_bits) -1)) >> align_bits) << align_bits;
10935 return end_addr - xt_block->offset;
10939 static bfd_boolean
10940 xtensa_xt_block_combine (xtensa_block_info *xt_block,
10941 const xtensa_block_info *xt_block_2)
10943 if (xt_block->sec != xt_block_2->sec)
10944 return FALSE;
10945 if (xt_block->offset + xt_block_aligned_size (xt_block)
10946 != xt_block_2->offset)
10947 return FALSE;
10949 if (xt_block_2->size == 0
10950 && (!xt_block_2->flags.is_unreachable
10951 || xt_block->flags.is_unreachable))
10953 if (xt_block_2->flags.is_align
10954 && xt_block->flags.is_align)
10956 /* Nothing needed. */
10957 if (xt_block->flags.alignment >= xt_block_2->flags.alignment)
10958 return TRUE;
10960 else
10962 if (xt_block_2->flags.is_align)
10964 /* Push alignment to previous entry. */
10965 xt_block->flags.is_align = xt_block_2->flags.is_align;
10966 xt_block->flags.alignment = xt_block_2->flags.alignment;
10968 return TRUE;
10971 if (!xtensa_frag_flags_combinable (&xt_block->flags,
10972 &xt_block_2->flags))
10973 return FALSE;
10975 xt_block->size += xt_block_2->size;
10977 if (xt_block_2->flags.is_align)
10979 xt_block->flags.is_align = TRUE;
10980 xt_block->flags.alignment = xt_block_2->flags.alignment;
10983 return TRUE;
10987 static void
10988 add_xt_prop_frags (segT sec,
10989 segT xt_block_sec,
10990 xtensa_block_info **xt_block,
10991 frag_flags_fn property_function)
10993 segment_info_type *seg_info;
10994 segment_info_type *xt_seg_info;
10995 bfd_vma seg_offset;
10996 fragS *fragP;
10998 xt_seg_info = retrieve_segment_info (xt_block_sec);
10999 seg_info = retrieve_segment_info (sec);
11000 /* Build it if needed. */
11001 while (*xt_block != NULL)
11003 xt_block = &(*xt_block)->next;
11005 /* We are either at NULL at the beginning or at the end. */
11007 /* Walk through the frags. */
11008 seg_offset = 0;
11010 if (seg_info->frchainP)
11012 for (fragP = seg_info->frchainP->frch_root; fragP;
11013 fragP = fragP->fr_next)
11015 xtensa_block_info tmp_block;
11016 tmp_block.sec = sec;
11017 tmp_block.offset = fragP->fr_address;
11018 tmp_block.size = fragP->fr_fix;
11019 tmp_block.next = NULL;
11020 property_function (fragP, &tmp_block.flags);
11022 if (!xtensa_frag_flags_is_empty (&tmp_block.flags))
11023 /* && fragP->fr_fix != 0) */
11025 if ((*xt_block) == NULL
11026 || !xtensa_xt_block_combine (*xt_block, &tmp_block))
11028 xtensa_block_info *new_block;
11029 if ((*xt_block) != NULL)
11030 xt_block = &(*xt_block)->next;
11031 new_block = (xtensa_block_info *)
11032 xmalloc (sizeof (xtensa_block_info));
11033 *new_block = tmp_block;
11034 *xt_block = new_block;
11042 /* op_placement_info_table */
11044 /* op_placement_info makes it easier to determine which
11045 ops can go in which slots. */
11047 static void
11048 init_op_placement_info_table (void)
11050 xtensa_isa isa = xtensa_default_isa;
11051 xtensa_insnbuf ibuf = xtensa_insnbuf_alloc (isa);
11052 xtensa_opcode opcode;
11053 xtensa_format fmt;
11054 int slot;
11055 int num_opcodes = xtensa_isa_num_opcodes (isa);
11057 op_placement_table = (op_placement_info_table)
11058 xmalloc (sizeof (op_placement_info) * num_opcodes);
11059 assert (xtensa_isa_num_formats (isa) < MAX_FORMATS);
11061 for (opcode = 0; opcode < num_opcodes; opcode++)
11063 op_placement_info *opi = &op_placement_table[opcode];
11064 /* FIXME: Make tinsn allocation dynamic. */
11065 if (xtensa_opcode_num_operands (isa, opcode) >= MAX_INSN_ARGS)
11066 as_fatal (_("too many operands in instruction"));
11067 opi->single = XTENSA_UNDEFINED;
11068 opi->single_size = 0;
11069 opi->widest = XTENSA_UNDEFINED;
11070 opi->widest_size = 0;
11071 opi->narrowest = XTENSA_UNDEFINED;
11072 opi->narrowest_size = 0x7F;
11073 opi->formats = 0;
11074 opi->num_formats = 0;
11075 opi->issuef = 0;
11076 for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
11078 opi->slots[fmt] = 0;
11079 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
11081 if (xtensa_opcode_encode (isa, fmt, slot, ibuf, opcode) == 0)
11083 int fmt_length = xtensa_format_length (isa, fmt);
11084 opi->issuef++;
11085 set_bit (fmt, opi->formats);
11086 set_bit (slot, opi->slots[fmt]);
11087 /* opi->slot_count[fmt]++; */
11088 if (fmt_length < opi->narrowest_size)
11090 opi->narrowest = fmt;
11091 opi->narrowest_size = fmt_length;
11093 if (fmt_length > opi->widest_size)
11095 opi->widest = fmt;
11096 opi->widest_size = fmt_length;
11098 if (xtensa_format_num_slots (isa, fmt) == 1)
11100 if (opi->single_size == 0
11101 || fmt_length < opi->single_size)
11103 opi->single = fmt;
11104 opi->single_size = fmt_length;
11109 if (opi->formats)
11110 opi->num_formats++;
11113 xtensa_insnbuf_free (isa, ibuf);
11117 bfd_boolean
11118 opcode_fits_format_slot (xtensa_opcode opcode, xtensa_format fmt, int slot)
11120 return bit_is_set (slot, op_placement_table[opcode].slots[fmt]);
11124 /* If the opcode is available in a single slot format, return its size. */
11126 static int
11127 xg_get_single_size (xtensa_opcode opcode)
11129 assert (op_placement_table[opcode].single != XTENSA_UNDEFINED);
11130 return op_placement_table[opcode].single_size;
11134 static xtensa_format
11135 xg_get_single_format (xtensa_opcode opcode)
11137 return op_placement_table[opcode].single;
11141 /* Instruction Stack Functions (from "xtensa-istack.h"). */
11143 void
11144 istack_init (IStack *stack)
11146 memset (stack, 0, sizeof (IStack));
11147 stack->ninsn = 0;
11151 bfd_boolean
11152 istack_empty (IStack *stack)
11154 return (stack->ninsn == 0);
11158 bfd_boolean
11159 istack_full (IStack *stack)
11161 return (stack->ninsn == MAX_ISTACK);
11165 /* Return a pointer to the top IStack entry.
11166 It is an error to call this if istack_empty () is TRUE. */
11168 TInsn *
11169 istack_top (IStack *stack)
11171 int rec = stack->ninsn - 1;
11172 assert (!istack_empty (stack));
11173 return &stack->insn[rec];
11177 /* Add a new TInsn to an IStack.
11178 It is an error to call this if istack_full () is TRUE. */
11180 void
11181 istack_push (IStack *stack, TInsn *insn)
11183 int rec = stack->ninsn;
11184 assert (!istack_full (stack));
11185 stack->insn[rec] = *insn;
11186 stack->ninsn++;
11190 /* Clear space for the next TInsn on the IStack and return a pointer
11191 to it. It is an error to call this if istack_full () is TRUE. */
11193 TInsn *
11194 istack_push_space (IStack *stack)
11196 int rec = stack->ninsn;
11197 TInsn *insn;
11198 assert (!istack_full (stack));
11199 insn = &stack->insn[rec];
11200 memset (insn, 0, sizeof (TInsn));
11201 stack->ninsn++;
11202 return insn;
11206 /* Remove the last pushed instruction. It is an error to call this if
11207 istack_empty () returns TRUE. */
11209 void
11210 istack_pop (IStack *stack)
11212 int rec = stack->ninsn - 1;
11213 assert (!istack_empty (stack));
11214 stack->ninsn--;
11215 memset (&stack->insn[rec], 0, sizeof (TInsn));
11219 /* TInsn functions. */
11221 void
11222 tinsn_init (TInsn *dst)
11224 memset (dst, 0, sizeof (TInsn));
11228 /* Get the ``num''th token of the TInsn.
11229 It is illegal to call this if num > insn->ntoks. */
11231 expressionS *
11232 tinsn_get_tok (TInsn *insn, int num)
11234 assert (num < insn->ntok);
11235 return &insn->tok[num];
11239 /* Return TRUE if ANY of the operands in the insn are symbolic. */
11241 static bfd_boolean
11242 tinsn_has_symbolic_operands (const TInsn *insn)
11244 int i;
11245 int n = insn->ntok;
11247 assert (insn->insn_type == ITYPE_INSN);
11249 for (i = 0; i < n; ++i)
11251 switch (insn->tok[i].X_op)
11253 case O_register:
11254 case O_constant:
11255 break;
11256 default:
11257 return TRUE;
11260 return FALSE;
11264 bfd_boolean
11265 tinsn_has_invalid_symbolic_operands (const TInsn *insn)
11267 xtensa_isa isa = xtensa_default_isa;
11268 int i;
11269 int n = insn->ntok;
11271 assert (insn->insn_type == ITYPE_INSN);
11273 for (i = 0; i < n; ++i)
11275 switch (insn->tok[i].X_op)
11277 case O_register:
11278 case O_constant:
11279 break;
11280 case O_big:
11281 case O_illegal:
11282 case O_absent:
11283 /* Errors for these types are caught later. */
11284 break;
11285 case O_hi16:
11286 case O_lo16:
11287 default:
11288 /* Symbolic immediates are only allowed on the last immediate
11289 operand. At this time, CONST16 is the only opcode where we
11290 support non-PC-relative relocations. (It isn't necessary
11291 to complain about non-PC-relative relocations here, but
11292 otherwise, no error is reported until the relocations are
11293 generated, and the assembler won't get that far if there
11294 are any other errors. It's nice to see all the problems
11295 at once.) */
11296 if (i != get_relaxable_immed (insn->opcode)
11297 || (xtensa_operand_is_PCrelative (isa, insn->opcode, i) != 1
11298 && insn->opcode != xtensa_const16_opcode))
11300 as_bad (_("invalid symbolic operand %d on '%s'"),
11301 i, xtensa_opcode_name (isa, insn->opcode));
11302 return TRUE;
11306 return FALSE;
11310 /* For assembly code with complex expressions (e.g. subtraction),
11311 we have to build them in the literal pool so that
11312 their results are calculated correctly after relaxation.
11313 The relaxation only handles expressions that
11314 boil down to SYMBOL + OFFSET. */
11316 static bfd_boolean
11317 tinsn_has_complex_operands (const TInsn *insn)
11319 int i;
11320 int n = insn->ntok;
11321 assert (insn->insn_type == ITYPE_INSN);
11322 for (i = 0; i < n; ++i)
11324 switch (insn->tok[i].X_op)
11326 case O_register:
11327 case O_constant:
11328 case O_symbol:
11329 case O_lo16:
11330 case O_hi16:
11331 break;
11332 default:
11333 return TRUE;
11336 return FALSE;
11340 /* Convert the constant operands in the tinsn to insnbuf.
11341 Return TRUE if there is a symbol in the immediate field.
11343 Before this is called,
11344 1) the number of operands are correct
11345 2) the tinsn is a ITYPE_INSN
11346 3) ONLY the relaxable_ is built
11347 4) All operands are O_constant, O_symbol. All constants fit
11348 The return value tells whether there are any remaining O_symbols. */
11350 static bfd_boolean
11351 tinsn_to_insnbuf (TInsn *tinsn, xtensa_insnbuf insnbuf)
11353 static xtensa_insnbuf slotbuf = 0;
11354 xtensa_isa isa = xtensa_default_isa;
11355 xtensa_opcode opcode = tinsn->opcode;
11356 xtensa_format fmt = xg_get_single_format (opcode);
11357 bfd_boolean has_fixup = FALSE;
11358 int noperands = xtensa_opcode_num_operands (isa, opcode);
11359 int i;
11360 uint32 opnd_value;
11361 char *file_name;
11362 int line;
11364 if (!slotbuf)
11365 slotbuf = xtensa_insnbuf_alloc (isa);
11367 assert (tinsn->insn_type == ITYPE_INSN);
11368 if (noperands != tinsn->ntok)
11369 as_fatal (_("operand number mismatch"));
11371 if (xtensa_opcode_encode (isa, fmt, 0, slotbuf, opcode))
11372 as_fatal (_("cannot encode opcode"));
11374 for (i = 0; i < noperands; ++i)
11376 expressionS *expr = &tinsn->tok[i];
11377 switch (expr->X_op)
11379 case O_register:
11380 if (xtensa_operand_is_visible (isa, opcode, i) == 0)
11381 break;
11382 /* The register number has already been checked in
11383 expression_maybe_register, so we don't need to check here. */
11384 opnd_value = expr->X_add_number;
11385 (void) xtensa_operand_encode (isa, opcode, i, &opnd_value);
11386 xtensa_operand_set_field (isa, opcode, i, fmt, 0,
11387 slotbuf, opnd_value);
11388 break;
11390 case O_constant:
11391 if (xtensa_operand_is_visible (isa, opcode, i) == 0)
11392 break;
11393 as_where (&file_name, &line);
11394 /* It is a constant and we called this function,
11395 then we have to try to fit it. */
11396 xtensa_insnbuf_set_operand (slotbuf, fmt, 0, opcode, i,
11397 expr->X_add_number, file_name, line);
11398 break;
11400 default:
11401 has_fixup = TRUE;
11402 break;
11406 xtensa_format_encode (isa, fmt, insnbuf);
11407 xtensa_format_set_slot (isa, fmt, 0, insnbuf, slotbuf);
11409 return has_fixup;
11413 /* Convert the constant operands in the tinsn to slotbuf.
11414 Return TRUE if there is a symbol in the immediate field.
11415 (Eventually this should replace tinsn_to_insnbuf.) */
11417 /* Before this is called,
11418 1) the number of operands are correct
11419 2) the tinsn is a ITYPE_INSN
11420 3) ONLY the relaxable_ is built
11421 4) All operands are
11422 O_constant, O_symbol
11423 All constants fit
11425 The return value tells whether there are any remaining O_symbols. */
11427 static bfd_boolean
11428 tinsn_to_slotbuf (xtensa_format fmt,
11429 int slot,
11430 TInsn *tinsn,
11431 xtensa_insnbuf slotbuf)
11433 xtensa_isa isa = xtensa_default_isa;
11434 xtensa_opcode opcode = tinsn->opcode;
11435 bfd_boolean has_fixup = FALSE;
11436 int noperands = xtensa_opcode_num_operands (isa, opcode);
11437 int i;
11439 *((int *) &slotbuf[0]) = 0;
11440 *((int *) &slotbuf[1]) = 0;
11441 assert (tinsn->insn_type == ITYPE_INSN);
11442 if (noperands != tinsn->ntok)
11443 as_fatal (_("operand number mismatch"));
11445 if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opcode))
11447 as_bad (_("cannot encode opcode \"%s\" in the given format \"%s\""),
11448 xtensa_opcode_name (isa, opcode), xtensa_format_name (isa, fmt));
11449 return FALSE;
11452 for (i = 0; i < noperands; i++)
11454 expressionS *expr = &tinsn->tok[i];
11455 int rc, line;
11456 char *file_name;
11457 uint32 opnd_value;
11459 switch (expr->X_op)
11461 case O_register:
11462 if (xtensa_operand_is_visible (isa, opcode, i) == 0)
11463 break;
11464 /* The register number has already been checked in
11465 expression_maybe_register, so we don't need to check here. */
11466 opnd_value = expr->X_add_number;
11467 (void) xtensa_operand_encode (isa, opcode, i, &opnd_value);
11468 rc = xtensa_operand_set_field (isa, opcode, i, fmt, slot, slotbuf,
11469 opnd_value);
11470 if (rc != 0)
11471 as_warn (_("xtensa-isa failure: %s"), xtensa_isa_error_msg (isa));
11472 break;
11474 case O_constant:
11475 if (xtensa_operand_is_visible (isa, opcode, i) == 0)
11476 break;
11477 as_where (&file_name, &line);
11478 /* It is a constant and we called this function
11479 then we have to try to fit it. */
11480 xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode, i,
11481 expr->X_add_number, file_name, line);
11482 break;
11484 default:
11485 has_fixup = TRUE;
11486 break;
11490 return has_fixup;
11494 /* Check the instruction arguments. Return TRUE on failure. */
11496 static bfd_boolean
11497 tinsn_check_arguments (const TInsn *insn)
11499 xtensa_isa isa = xtensa_default_isa;
11500 xtensa_opcode opcode = insn->opcode;
11502 if (opcode == XTENSA_UNDEFINED)
11504 as_bad (_("invalid opcode"));
11505 return TRUE;
11508 if (xtensa_opcode_num_operands (isa, opcode) > insn->ntok)
11510 as_bad (_("too few operands"));
11511 return TRUE;
11514 if (xtensa_opcode_num_operands (isa, opcode) < insn->ntok)
11516 as_bad (_("too many operands"));
11517 return TRUE;
11519 return FALSE;
11523 /* Load an instruction from its encoded form. */
11525 static void
11526 tinsn_from_chars (TInsn *tinsn, char *f, int slot)
11528 vliw_insn vinsn;
11530 xg_init_vinsn (&vinsn);
11531 vinsn_from_chars (&vinsn, f);
11533 *tinsn = vinsn.slots[slot];
11534 xg_free_vinsn (&vinsn);
11538 static void
11539 tinsn_from_insnbuf (TInsn *tinsn,
11540 xtensa_insnbuf slotbuf,
11541 xtensa_format fmt,
11542 int slot)
11544 int i;
11545 xtensa_isa isa = xtensa_default_isa;
11547 /* Find the immed. */
11548 tinsn_init (tinsn);
11549 tinsn->insn_type = ITYPE_INSN;
11550 tinsn->is_specific_opcode = FALSE; /* must not be specific */
11551 tinsn->opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
11552 tinsn->ntok = xtensa_opcode_num_operands (isa, tinsn->opcode);
11553 for (i = 0; i < tinsn->ntok; i++)
11555 set_expr_const (&tinsn->tok[i],
11556 xtensa_insnbuf_get_operand (slotbuf, fmt, slot,
11557 tinsn->opcode, i));
11562 /* Read the value of the relaxable immed from the fr_symbol and fr_offset. */
11564 static void
11565 tinsn_immed_from_frag (TInsn *tinsn, fragS *fragP, int slot)
11567 xtensa_opcode opcode = tinsn->opcode;
11568 int opnum;
11570 if (fragP->tc_frag_data.slot_symbols[slot])
11572 opnum = get_relaxable_immed (opcode);
11573 assert (opnum >= 0);
11574 if (fragP->tc_frag_data.slot_sub_symbols[slot])
11576 set_expr_symbol_offset_diff
11577 (&tinsn->tok[opnum],
11578 fragP->tc_frag_data.slot_symbols[slot],
11579 fragP->tc_frag_data.slot_sub_symbols[slot],
11580 fragP->tc_frag_data.slot_offsets[slot]);
11582 else
11584 set_expr_symbol_offset
11585 (&tinsn->tok[opnum],
11586 fragP->tc_frag_data.slot_symbols[slot],
11587 fragP->tc_frag_data.slot_offsets[slot]);
11593 static int
11594 get_num_stack_text_bytes (IStack *istack)
11596 int i;
11597 int text_bytes = 0;
11599 for (i = 0; i < istack->ninsn; i++)
11601 TInsn *tinsn = &istack->insn[i];
11602 if (tinsn->insn_type == ITYPE_INSN)
11603 text_bytes += xg_get_single_size (tinsn->opcode);
11605 return text_bytes;
11609 static int
11610 get_num_stack_literal_bytes (IStack *istack)
11612 int i;
11613 int lit_bytes = 0;
11615 for (i = 0; i < istack->ninsn; i++)
11617 TInsn *tinsn = &istack->insn[i];
11618 if (tinsn->insn_type == ITYPE_LITERAL && tinsn->ntok == 1)
11619 lit_bytes += 4;
11621 return lit_bytes;
11625 /* vliw_insn functions. */
11627 static void
11628 xg_init_vinsn (vliw_insn *v)
11630 int i;
11631 xtensa_isa isa = xtensa_default_isa;
11633 xg_clear_vinsn (v);
11635 v->insnbuf = xtensa_insnbuf_alloc (isa);
11636 if (v->insnbuf == NULL)
11637 as_fatal (_("out of memory"));
11639 for (i = 0; i < MAX_SLOTS; i++)
11641 tinsn_init (&v->slots[i]);
11642 v->slots[i].opcode = XTENSA_UNDEFINED;
11643 v->slotbuf[i] = xtensa_insnbuf_alloc (isa);
11644 if (v->slotbuf[i] == NULL)
11645 as_fatal (_("out of memory"));
11650 static void
11651 xg_clear_vinsn (vliw_insn *v)
11653 int i;
11654 v->format = XTENSA_UNDEFINED;
11655 v->num_slots = 0;
11656 v->inside_bundle = FALSE;
11658 if (xt_saved_debug_type != DEBUG_NONE)
11659 debug_type = xt_saved_debug_type;
11661 for (i = 0; i < MAX_SLOTS; i++)
11663 memset (&v->slots[i], 0, sizeof (TInsn));
11664 v->slots[i].opcode = XTENSA_UNDEFINED;
11669 static bfd_boolean
11670 vinsn_has_specific_opcodes (vliw_insn *v)
11672 int i;
11674 for (i = 0; i < v->num_slots; i++)
11676 if (v->slots[i].is_specific_opcode)
11677 return TRUE;
11679 return FALSE;
11683 static void
11684 xg_free_vinsn (vliw_insn *v)
11686 int i;
11687 xtensa_insnbuf_free (xtensa_default_isa, v->insnbuf);
11688 for (i = 0; i < MAX_SLOTS; i++)
11689 xtensa_insnbuf_free (xtensa_default_isa, v->slotbuf[i]);
11693 /* Before this is called, we should have
11694 filled out the following fields:
11696 1) the number of operands for each opcode are correct
11697 2) the tinsn in the slots are ITYPE_INSN
11698 3) ONLY the relaxable_ is built
11699 4) All operands are
11700 O_constant, O_symbol
11701 All constants fit
11703 The return value tells whether there are any remaining O_symbols. */
11705 static bfd_boolean
11706 vinsn_to_insnbuf (vliw_insn *vinsn,
11707 char *frag_offset,
11708 fragS *fragP,
11709 bfd_boolean record_fixup)
11711 xtensa_isa isa = xtensa_default_isa;
11712 xtensa_format fmt = vinsn->format;
11713 xtensa_insnbuf insnbuf = vinsn->insnbuf;
11714 int slot;
11715 bfd_boolean has_fixup = FALSE;
11717 xtensa_format_encode (isa, fmt, insnbuf);
11719 for (slot = 0; slot < vinsn->num_slots; slot++)
11721 TInsn *tinsn = &vinsn->slots[slot];
11722 bfd_boolean tinsn_has_fixup =
11723 tinsn_to_slotbuf (vinsn->format, slot, tinsn,
11724 vinsn->slotbuf[slot]);
11726 xtensa_format_set_slot (isa, fmt, slot,
11727 insnbuf, vinsn->slotbuf[slot]);
11728 /* tinsn_has_fixup tracks if there is a fixup at all.
11729 record_fixup controls globally. I.E., we use this
11730 function from several places, some of which are after
11731 fixups have already been recorded. Finally,
11732 tinsn->record_fixup controls based on the individual ops,
11733 which may or may not need it based on the relaxation
11734 requirements. */
11735 if (tinsn_has_fixup && record_fixup)
11737 int i;
11738 xtensa_opcode opcode = tinsn->opcode;
11739 int noperands = xtensa_opcode_num_operands (isa, opcode);
11740 has_fixup = TRUE;
11742 for (i = 0; i < noperands; i++)
11744 expressionS* expr = &tinsn->tok[i];
11745 switch (expr->X_op)
11747 case O_symbol:
11748 case O_lo16:
11749 case O_hi16:
11750 if (get_relaxable_immed (opcode) == i)
11752 if (tinsn->record_fix || expr->X_op != O_symbol)
11754 if (!xg_add_opcode_fix
11755 (tinsn, i, fmt, slot, expr, fragP,
11756 frag_offset - fragP->fr_literal))
11757 as_bad (_("instruction with constant operands does not fit"));
11759 else
11761 tinsn->symbol = expr->X_add_symbol;
11762 tinsn->offset = expr->X_add_number;
11765 else
11766 as_bad (_("invalid operand %d on '%s'"),
11767 i, xtensa_opcode_name (isa, opcode));
11768 break;
11770 case O_constant:
11771 case O_register:
11772 break;
11774 case O_subtract:
11775 if (get_relaxable_immed (opcode) == i)
11777 if (tinsn->record_fix)
11778 as_bad (_("invalid subtract operand"));
11779 else
11781 tinsn->symbol = expr->X_add_symbol;
11782 tinsn->sub_symbol = expr->X_op_symbol;
11783 tinsn->offset = expr->X_add_number;
11786 else
11787 as_bad (_("invalid operand %d on '%s'"),
11788 i, xtensa_opcode_name (isa, opcode));
11789 break;
11791 default:
11792 as_bad (_("invalid expression for operand %d on '%s'"),
11793 i, xtensa_opcode_name (isa, opcode));
11794 break;
11800 return has_fixup;
11804 static void
11805 vinsn_from_chars (vliw_insn *vinsn, char *f)
11807 static xtensa_insnbuf insnbuf = NULL;
11808 static xtensa_insnbuf slotbuf = NULL;
11809 int i;
11810 xtensa_format fmt;
11811 xtensa_isa isa = xtensa_default_isa;
11813 if (!insnbuf)
11815 insnbuf = xtensa_insnbuf_alloc (isa);
11816 slotbuf = xtensa_insnbuf_alloc (isa);
11819 xtensa_insnbuf_from_chars (isa, insnbuf, f, 0);
11820 fmt = xtensa_format_decode (isa, insnbuf);
11821 if (fmt == XTENSA_UNDEFINED)
11822 as_fatal (_("cannot decode instruction format"));
11823 vinsn->format = fmt;
11824 vinsn->num_slots = xtensa_format_num_slots (isa, fmt);
11826 for (i = 0; i < vinsn->num_slots; i++)
11828 TInsn *tinsn = &vinsn->slots[i];
11829 xtensa_format_get_slot (isa, fmt, i, insnbuf, slotbuf);
11830 tinsn_from_insnbuf (tinsn, slotbuf, fmt, i);
11835 /* Expression utilities. */
11837 /* Return TRUE if the expression is an integer constant. */
11839 bfd_boolean
11840 expr_is_const (const expressionS *s)
11842 return (s->X_op == O_constant);
11846 /* Get the expression constant.
11847 Calling this is illegal if expr_is_const () returns TRUE. */
11849 offsetT
11850 get_expr_const (const expressionS *s)
11852 assert (expr_is_const (s));
11853 return s->X_add_number;
11857 /* Set the expression to a constant value. */
11859 void
11860 set_expr_const (expressionS *s, offsetT val)
11862 s->X_op = O_constant;
11863 s->X_add_number = val;
11864 s->X_add_symbol = NULL;
11865 s->X_op_symbol = NULL;
11869 bfd_boolean
11870 expr_is_register (const expressionS *s)
11872 return (s->X_op == O_register);
11876 /* Get the expression constant.
11877 Calling this is illegal if expr_is_const () returns TRUE. */
11879 offsetT
11880 get_expr_register (const expressionS *s)
11882 assert (expr_is_register (s));
11883 return s->X_add_number;
11887 /* Set the expression to a symbol + constant offset. */
11889 void
11890 set_expr_symbol_offset (expressionS *s, symbolS *sym, offsetT offset)
11892 s->X_op = O_symbol;
11893 s->X_add_symbol = sym;
11894 s->X_op_symbol = NULL; /* unused */
11895 s->X_add_number = offset;
11899 /* Set the expression to symbol - minus_sym + offset. */
11901 static void
11902 set_expr_symbol_offset_diff (expressionS *s,
11903 symbolS *sym,
11904 symbolS *minus_sym,
11905 offsetT offset)
11907 s->X_op = O_subtract;
11908 s->X_add_symbol = sym;
11909 s->X_op_symbol = minus_sym; /* unused */
11910 s->X_add_number = offset;
11914 /* Return TRUE if the two expressions are equal. */
11916 bfd_boolean
11917 expr_is_equal (expressionS *s1, expressionS *s2)
11919 if (s1->X_op != s2->X_op)
11920 return FALSE;
11921 if (s1->X_add_symbol != s2->X_add_symbol)
11922 return FALSE;
11923 if (s1->X_op_symbol != s2->X_op_symbol)
11924 return FALSE;
11925 if (s1->X_add_number != s2->X_add_number)
11926 return FALSE;
11927 return TRUE;
11931 static void
11932 copy_expr (expressionS *dst, const expressionS *src)
11934 memcpy (dst, src, sizeof (expressionS));
11938 /* Support for the "--rename-section" option. */
11940 struct rename_section_struct
11942 char *old_name;
11943 char *new_name;
11944 struct rename_section_struct *next;
11947 static struct rename_section_struct *section_rename;
11950 /* Parse the string "oldname=new_name(:oldname2=new_name2)*" and add
11951 entries to the section_rename list. Note: Specifying multiple
11952 renamings separated by colons is not documented and is retained only
11953 for backward compatibility. */
11955 static void
11956 build_section_rename (const char *arg)
11958 struct rename_section_struct *r;
11959 char *this_arg = NULL;
11960 char *next_arg = NULL;
11962 for (this_arg = xstrdup (arg); this_arg != NULL; this_arg = next_arg)
11964 char *old_name, *new_name;
11966 if (this_arg)
11968 next_arg = strchr (this_arg, ':');
11969 if (next_arg)
11971 *next_arg = '\0';
11972 next_arg++;
11976 old_name = this_arg;
11977 new_name = strchr (this_arg, '=');
11979 if (*old_name == '\0')
11981 as_warn (_("ignoring extra '-rename-section' delimiter ':'"));
11982 continue;
11984 if (!new_name || new_name[1] == '\0')
11986 as_warn (_("ignoring invalid '-rename-section' specification: '%s'"),
11987 old_name);
11988 continue;
11990 *new_name = '\0';
11991 new_name++;
11993 /* Check for invalid section renaming. */
11994 for (r = section_rename; r != NULL; r = r->next)
11996 if (strcmp (r->old_name, old_name) == 0)
11997 as_bad (_("section %s renamed multiple times"), old_name);
11998 if (strcmp (r->new_name, new_name) == 0)
11999 as_bad (_("multiple sections remapped to output section %s"),
12000 new_name);
12003 /* Now add it. */
12004 r = (struct rename_section_struct *)
12005 xmalloc (sizeof (struct rename_section_struct));
12006 r->old_name = xstrdup (old_name);
12007 r->new_name = xstrdup (new_name);
12008 r->next = section_rename;
12009 section_rename = r;
12014 char *
12015 xtensa_section_rename (char *name)
12017 struct rename_section_struct *r = section_rename;
12019 for (r = section_rename; r != NULL; r = r->next)
12021 if (strcmp (r->old_name, name) == 0)
12022 return r->new_name;
12025 return name;