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