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