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