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