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