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