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