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