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