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