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