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