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