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