1 /* tc-xtensa.c -- Assemble Xtensa instructions.
2 Copyright 2003 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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)
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.
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, 59 Temple Place - Suite 330, Boston,
19 MA 02111-1307, USA. */
24 #include "safe-ctype.h"
25 #include "tc-xtensa.h"
28 #include "xtensa-relax.h"
29 #include "xtensa-istack.h"
30 #include "dwarf2dbg.h"
31 #include "struc-symbol.h"
32 #include "xtensa-config.h"
35 #define uint32 unsigned int
38 #define int32 signed int
43 There are 3 forms for instructions,
44 1) the MEMORY format -- this is the encoding 2 or 3 byte instruction
45 2) the TInsn -- handles instructions/labels and literals;
46 all operands are assumed to be expressions
47 3) the IStack -- a stack of TInsn. this allows us to
48 reason about the generated expansion instructions
50 Naming conventions (used somewhat inconsistently):
51 The xtensa_ functions are exported
52 The xg_ functions are internal
54 We also have a couple of different extensibility mechanisms.
55 1) The idiom replacement:
56 This is used when a line is first parsed to
57 replace an instruction pattern with another instruction
58 It is currently limited to replacements of instructions
59 with constant operands.
60 2) The xtensa-relax.c mechanism that has stronger instruction
61 replacement patterns. When an instruction's immediate field
62 does not fit the next instruction sequence is attempted.
63 In addition, "narrow" opcodes are supported this way. */
66 /* Define characters with special meanings to GAS. */
67 const char comment_chars[] = "#";
68 const char line_comment_chars[] = "#";
69 const char line_separator_chars[] = ";";
70 const char EXP_CHARS[] = "eE";
71 const char FLT_CHARS[] = "rRsSfFdDxXpP";
74 /* Flag to indicate whether the hardware supports the density option.
75 If not, enabling density instructions (via directives or --density flag)
79 bfd_boolean density_supported = XCHAL_HAVE_DENSITY;
81 bfd_boolean density_supported = TRUE;
84 #define XTENSA_FETCH_WIDTH 4
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
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. */
96 #define LITERAL_SECTION_NAME xtensa_section_rename (".literal")
97 #define FINI_SECTION_NAME xtensa_section_rename (".fini")
98 #define INIT_SECTION_NAME xtensa_section_rename (".init")
99 #define FINI_LITERAL_SECTION_NAME xtensa_section_rename (".fini.literal")
100 #define INIT_LITERAL_SECTION_NAME xtensa_section_rename (".init.literal")
103 /* This type is used for the directive_stack to keep track of the
104 state of the literal collection pools. */
106 typedef struct lit_state_struct
108 const char *lit_seg_name;
109 const char *init_lit_seg_name;
110 const char *fini_lit_seg_name;
116 static lit_state default_lit_sections;
119 /* We keep lists of literal segments. The seg_list type is the node
120 for such a list. The *_literal_head locals are the heads of the
121 various lists. All of these lists have a dummy node at the start. */
123 typedef struct seg_list_struct
125 struct seg_list_struct *next;
129 static seg_list literal_head_h;
130 static seg_list *literal_head = &literal_head_h;
131 static seg_list init_literal_head_h;
132 static seg_list *init_literal_head = &init_literal_head_h;
133 static seg_list fini_literal_head_h;
134 static seg_list *fini_literal_head = &fini_literal_head_h;
137 /* Lists of symbols. We keep a list of symbols that label the current
138 instruction, so that we can adjust the symbols when inserting alignment
139 for various instructions. We also keep a list of all the symbols on
140 literals, so that we can fix up those symbols when the literals are
141 later moved into the text sections. */
143 typedef struct sym_list_struct
145 struct sym_list_struct *next;
149 static sym_list *insn_labels = NULL;
150 static sym_list *free_insn_labels = NULL;
151 static sym_list *saved_insn_labels = NULL;
153 static sym_list *literal_syms;
156 /* Global flag to indicate when we are emitting literals. */
157 int generating_literals = 0;
160 /* Structure for saving the current state before emitting literals. */
161 typedef struct emit_state_struct
166 int generating_literals;
181 directive_literal_prefix
187 bfd_boolean can_be_negated;
190 const directive_infoS directive_info[] =
199 {"literal_prefix", FALSE}
202 bfd_boolean directive_state[] =
206 #if STATIC_LIBISA && !XCHAL_HAVE_DENSITY
213 FALSE, /* freeregs */
214 FALSE, /* longcalls */
215 FALSE /* literal_prefix */
219 enum xtensa_relax_statesE
221 RELAX_ALIGN_NEXT_OPCODE,
222 /* Use the first opcode of the next fragment to determine the
223 alignment requirements. This is ONLY used for LOOPS
226 RELAX_DESIRE_ALIGN_IF_TARGET,
227 /* These are placed in front of labels. They will all be converted
228 to RELAX_DESIRE_ALIGN / RELAX_LOOP_END or rs_fill of 0 before
229 relaxation begins. */
231 RELAX_ADD_NOP_IF_A0_B_RETW,
232 /* These are placed in front of conditional branches. It will be
233 turned into a NOP (using a1) if the branch is immediately
234 followed by a RETW or RETW.N. Otherwise it will be turned into
235 an rs_fill of 0 before relaxation begins. */
237 RELAX_ADD_NOP_IF_PRE_LOOP_END,
238 /* These are placed after JX instructions. It will be turned into a
239 NOP if there is one instruction before a loop end label.
240 Otherwise it will be turned into an rs_fill of 0 before
241 relaxation begins. This is used to avoid a hardware TIE
242 interlock issue prior to T1040. */
244 RELAX_ADD_NOP_IF_SHORT_LOOP,
245 /* These are placed after LOOP instructions. It will be turned into
246 a NOP when: (1) there are less than 3 instructions in the loop;
247 we place 2 of these in a row to add up to 2 NOPS in short loops;
248 or (2) The instructions in the loop do not include a branch or
249 jump. Otherwise it will be turned into an rs_fill of 0 before
250 relaxation begins. This is used to avoid hardware bug
253 RELAX_ADD_NOP_IF_CLOSE_LOOP_END,
254 /* These are placed after LOOP instructions. It will be turned into
255 a NOP if there are less than 12 bytes to the end of some other
256 loop's end. Otherwise it will be turned into an rs_fill of 0
257 before relaxation begins. This is used to avoid hardware bug
261 /* The next fragment like its first instruction to NOT cross a
265 /* This will be turned into a NOP or NOP.N if the previous
266 instruction is expanded to negate a loop. */
268 RELAX_LOOP_END_ADD_NOP,
269 /* When the code density option is available, this will generate a
270 NOP.N marked RELAX_NARROW. Otherwise, it will create an rs_fill
271 fragment with a NOP in it. */
274 /* Another fragment could generate an expansion here but has not yet. */
277 /* Expansion has been generated by an instruction that generates a
278 literal. However, the stretch has NOT been reported yet in this
282 /* Expansion has been generated by an instruction that generates a
285 RELAX_LITERAL_POOL_BEGIN,
286 RELAX_LITERAL_POOL_END,
287 /* Technically these are not relaxations at all, but mark a location
288 to store literals later. Note that fr_var stores the frchain for
289 BEGIN frags and fr_var stores now_seg for END frags. */
292 /* The last instruction in this fragment (at->fr_opcode) can be
293 freely replaced with a single wider instruction if a future
294 alignment desires or needs it. */
297 /* The last instruction in this fragment (at->fr_opcode) contains
298 the value defined by fr_symbol (fr_offset = 0). If the value
299 does not fit, use the specified expansion. This is similar to
300 "NARROW", except that these may not be expanded in order to align
304 /* The last instruction in this fragment (at->fr_opcode) contains a
305 literal. It has already been expanded at least 1 step. */
308 /* The last instruction in this fragment (at->fr_opcode) contains a
309 literal. It has already been expanded at least 2 steps. */
312 /* This is used as a stopper to bound the number of steps that
314 #define RELAX_IMMED_MAXSTEPS (RELAX_IMMED_STEP2 - RELAX_IMMED)
317 typedef bfd_boolean (*frag_predicate) (const fragS *);
320 /* Directive functions. */
322 static bfd_boolean use_generics
324 static bfd_boolean use_longcalls
326 static bfd_boolean code_density_available
328 static bfd_boolean can_relax
330 static void directive_push
331 PARAMS ((directiveE, bfd_boolean, const void *));
332 static void directive_pop
333 PARAMS ((directiveE *, bfd_boolean *, const char **,
334 unsigned int *, const void **));
335 static void directive_balance
337 static bfd_boolean inside_directive
338 PARAMS ((directiveE));
339 static void get_directive
340 PARAMS ((directiveE *, bfd_boolean *));
341 static void xtensa_begin_directive
343 static void xtensa_end_directive
345 static void xtensa_literal_prefix
346 PARAMS ((char const *, int));
347 static void xtensa_literal_position
349 static void xtensa_literal_pseudo
352 /* Parsing and Idiom Translation Functions. */
354 static const char *expression_end
355 PARAMS ((const char *));
356 static unsigned tc_get_register
357 PARAMS ((const char *));
358 static void expression_maybe_register
359 PARAMS ((xtensa_operand, expressionS *));
360 static int tokenize_arguments
361 PARAMS ((char **, char *));
362 static bfd_boolean parse_arguments
363 PARAMS ((TInsn *, int, char **));
364 static int xg_translate_idioms
365 PARAMS ((char **, int *, char **));
366 static int xg_translate_sysreg_op
367 PARAMS ((char **, int *, char **));
368 static void xg_reverse_shift_count
370 static int xg_arg_is_constant
371 PARAMS ((char *, offsetT *));
372 static void xg_replace_opname
373 PARAMS ((char **, char *));
374 static int xg_check_num_args
375 PARAMS ((int *, int, char *, char **));
377 /* Functions for dealing with the Xtensa ISA. */
379 static bfd_boolean operand_is_immed
380 PARAMS ((xtensa_operand));
381 static bfd_boolean operand_is_pcrel_label
382 PARAMS ((xtensa_operand));
383 static int get_relaxable_immed
384 PARAMS ((xtensa_opcode));
385 static xtensa_opcode get_opcode_from_buf
386 PARAMS ((const char *));
387 static bfd_boolean is_direct_call_opcode
388 PARAMS ((xtensa_opcode));
389 static bfd_boolean is_call_opcode
390 PARAMS ((xtensa_opcode));
391 static bfd_boolean is_entry_opcode
392 PARAMS ((xtensa_opcode));
393 static bfd_boolean is_loop_opcode
394 PARAMS ((xtensa_opcode));
395 static bfd_boolean is_the_loop_opcode
396 PARAMS ((xtensa_opcode));
397 static bfd_boolean is_jx_opcode
398 PARAMS ((xtensa_opcode));
399 static bfd_boolean is_windowed_return_opcode
400 PARAMS ((xtensa_opcode));
401 static bfd_boolean is_conditional_branch_opcode
402 PARAMS ((xtensa_opcode));
403 static bfd_boolean is_branch_or_jump_opcode
404 PARAMS ((xtensa_opcode));
405 static bfd_reloc_code_real_type opnum_to_reloc
407 static int reloc_to_opnum
408 PARAMS ((bfd_reloc_code_real_type));
409 static void xtensa_insnbuf_set_operand
410 PARAMS ((xtensa_insnbuf, xtensa_opcode, xtensa_operand, int32,
411 const char *, unsigned int));
412 static uint32 xtensa_insnbuf_get_operand
413 PARAMS ((xtensa_insnbuf, xtensa_opcode, int));
414 static void xtensa_insnbuf_set_immediate_field
415 PARAMS ((xtensa_opcode, xtensa_insnbuf, int32, const char *,
417 static bfd_boolean is_negatable_branch
420 /* Various Other Internal Functions. */
422 static bfd_boolean is_unique_insn_expansion
423 PARAMS ((TransitionRule *));
424 static int xg_get_insn_size
426 static int xg_get_build_instr_size
427 PARAMS ((BuildInstr *));
428 static bfd_boolean xg_is_narrow_insn
430 static bfd_boolean xg_is_single_relaxable_insn
432 static int xg_get_max_narrow_insn_size
433 PARAMS ((xtensa_opcode));
434 static int xg_get_max_insn_widen_size
435 PARAMS ((xtensa_opcode));
436 static int xg_get_max_insn_widen_literal_size
437 PARAMS ((xtensa_opcode));
438 static bfd_boolean xg_is_relaxable_insn
439 PARAMS ((TInsn *, int));
440 static symbolS *get_special_literal_symbol
442 static symbolS *get_special_label_symbol
444 static bfd_boolean xg_build_to_insn
445 PARAMS ((TInsn *, TInsn *, BuildInstr *));
446 static bfd_boolean xg_build_to_stack
447 PARAMS ((IStack *, TInsn *, BuildInstr *));
448 static bfd_boolean xg_expand_to_stack
449 PARAMS ((IStack *, TInsn *, int));
450 static bfd_boolean xg_expand_narrow
451 PARAMS ((TInsn *, TInsn *));
452 static bfd_boolean xg_immeds_fit
453 PARAMS ((const TInsn *));
454 static bfd_boolean xg_symbolic_immeds_fit
455 PARAMS ((const TInsn *, segT, fragS *, offsetT, long));
456 static bfd_boolean xg_check_operand
457 PARAMS ((int32, xtensa_operand));
458 static int is_dnrange
459 PARAMS ((fragS *, symbolS *, long));
460 static int xg_assembly_relax
461 PARAMS ((IStack *, TInsn *, segT, fragS *, offsetT, int, long));
462 static void xg_force_frag_space
464 static void xg_finish_frag
465 PARAMS ((char *, enum xtensa_relax_statesE, int, bfd_boolean));
466 static bfd_boolean is_branch_jmp_to_next
467 PARAMS ((TInsn *, fragS *));
468 static void xg_add_branch_and_loop_targets
470 static bfd_boolean xg_instruction_matches_rule
471 PARAMS ((TInsn *, TransitionRule *));
472 static TransitionRule *xg_instruction_match
474 static bfd_boolean xg_build_token_insn
475 PARAMS ((BuildInstr *, TInsn *, TInsn *));
476 static bfd_boolean xg_simplify_insn
477 PARAMS ((TInsn *, TInsn *));
478 static bfd_boolean xg_expand_assembly_insn
479 PARAMS ((IStack *, TInsn *));
480 static symbolS *xg_assemble_literal
482 static void xg_assemble_literal_space
484 static symbolS *xtensa_create_literal_symbol
485 PARAMS ((segT, fragS *));
486 static void xtensa_add_literal_sym
487 PARAMS ((symbolS *));
488 static void xtensa_add_insn_label
489 PARAMS ((symbolS *));
490 static void xtensa_clear_insn_labels
492 static bfd_boolean get_is_linkonce_section
493 PARAMS ((bfd *, segT));
494 static bfd_boolean xg_emit_insn
495 PARAMS ((TInsn *, bfd_boolean));
496 static bfd_boolean xg_emit_insn_to_buf
497 PARAMS ((TInsn *, char *, fragS *, offsetT, bfd_boolean));
498 static bfd_boolean xg_add_opcode_fix
499 PARAMS ((xtensa_opcode, int, expressionS *, fragS *, offsetT));
500 static void xg_resolve_literals
501 PARAMS ((TInsn *, symbolS *));
502 static void xg_resolve_labels
503 PARAMS ((TInsn *, symbolS *));
504 static void xg_assemble_tokens
506 static bfd_boolean is_register_writer
507 PARAMS ((const TInsn *, const char *, int));
508 static bfd_boolean is_bad_loopend_opcode
509 PARAMS ((const TInsn *));
510 static bfd_boolean is_unaligned_label
511 PARAMS ((symbolS *));
512 static fragS *next_non_empty_frag
513 PARAMS ((const fragS *));
514 static xtensa_opcode next_frag_opcode
515 PARAMS ((const fragS *));
516 static void update_next_frag_nop_state
518 static bfd_boolean next_frag_is_branch_target
519 PARAMS ((const fragS *));
520 static bfd_boolean next_frag_is_loop_target
521 PARAMS ((const fragS *));
522 static addressT next_frag_pre_opcode_bytes
523 PARAMS ((const fragS *));
524 static bfd_boolean is_next_frag_target
525 PARAMS ((const fragS *, const fragS *));
526 static void xtensa_mark_literal_pool_location
528 static void xtensa_move_labels
529 PARAMS ((fragS *, valueT, bfd_boolean));
530 static void assemble_nop
531 PARAMS ((size_t, char *));
532 static addressT get_expanded_loop_offset
533 PARAMS ((xtensa_opcode));
534 static fragS *get_literal_pool_location
536 static void set_literal_pool_location
537 PARAMS ((segT, fragS *));
539 /* Helpers for xtensa_end(). */
541 static void xtensa_cleanup_align_frags
543 static void xtensa_fix_target_frags
545 static bfd_boolean frag_can_negate_branch
547 static void xtensa_fix_a0_b_retw_frags
549 static bfd_boolean next_instrs_are_b_retw
551 static void xtensa_fix_b_j_loop_end_frags
553 static bfd_boolean next_instr_is_loop_end
555 static void xtensa_fix_close_loop_end_frags
557 static size_t min_bytes_to_other_loop_end
558 PARAMS ((fragS *, fragS *, offsetT, size_t));
559 static size_t unrelaxed_frag_min_size
561 static void xtensa_fix_short_loop_frags
563 static size_t count_insns_to_loop_end
564 PARAMS ((fragS *, bfd_boolean, size_t));
565 static size_t unrelaxed_frag_min_insn_count
567 static bfd_boolean branch_before_loop_end
569 static bfd_boolean unrelaxed_frag_has_b_j
571 static void xtensa_sanity_check
573 static bfd_boolean is_empty_loop
574 PARAMS ((const TInsn *, fragS *));
575 static bfd_boolean is_local_forward_loop
576 PARAMS ((const TInsn *, fragS *));
578 /* Alignment Functions. */
580 static size_t get_text_align_power
582 static addressT get_text_align_max_fill_size
583 PARAMS ((int, bfd_boolean, bfd_boolean));
584 static addressT get_text_align_fill_size
585 PARAMS ((addressT, int, int, bfd_boolean, bfd_boolean));
586 static size_t get_text_align_nop_count
587 PARAMS ((size_t, bfd_boolean));
588 static size_t get_text_align_nth_nop_size
589 PARAMS ((size_t, size_t, bfd_boolean));
590 static addressT get_noop_aligned_address
591 PARAMS ((fragS *, addressT));
592 static addressT get_widen_aligned_address
593 PARAMS ((fragS *, addressT));
595 /* Helpers for xtensa_relax_frag(). */
597 static long relax_frag_text_align
598 PARAMS ((fragS *, long));
599 static long relax_frag_add_nop
601 static long relax_frag_narrow
602 PARAMS ((fragS *, long));
603 static bfd_boolean future_alignment_required
604 PARAMS ((fragS *, long));
605 static long relax_frag_immed
606 PARAMS ((segT, fragS *, long, int, int *));
608 /* Helpers for md_convert_frag(). */
610 static void convert_frag_align_next_opcode
612 static void convert_frag_narrow
614 static void convert_frag_immed
615 PARAMS ((segT, fragS *, int));
616 static fixS *fix_new_exp_in_seg
617 PARAMS ((segT, subsegT, fragS *, int, int, expressionS *, int,
618 bfd_reloc_code_real_type));
619 static void convert_frag_immed_finish_loop
620 PARAMS ((segT, fragS *, TInsn *));
621 static offsetT get_expression_value
622 PARAMS ((segT, expressionS *));
624 /* Flags for the Last Instruction in Each Subsegment. */
626 static unsigned get_last_insn_flags
627 PARAMS ((segT, subsegT));
628 static void set_last_insn_flags
629 PARAMS ((segT, subsegT, unsigned, bfd_boolean));
631 /* Segment list functions. */
633 static void xtensa_remove_section
635 static void xtensa_insert_section
636 PARAMS ((segT, segT));
637 static void xtensa_move_seg_list_to_beginning
638 PARAMS ((seg_list *));
639 static void xtensa_move_literals
641 static void xtensa_reorder_seg_list
642 PARAMS ((seg_list *, segT));
643 static void xtensa_reorder_segments
645 static segT get_last_sec
647 static void xtensa_switch_to_literal_fragment
648 PARAMS ((emit_state *));
649 static void xtensa_switch_section_emit_state
650 PARAMS ((emit_state *, segT, subsegT));
651 static void xtensa_restore_emit_state
652 PARAMS ((emit_state *));
653 static void cache_literal_section
654 PARAMS ((seg_list *, const char *, segT *));
655 static segT retrieve_literal_seg
656 PARAMS ((seg_list *, const char *));
657 static segT seg_present
658 PARAMS ((const char *));
659 static void add_seg_list
660 PARAMS ((seg_list *, segT));
662 /* Property Table (e.g., ".xt.insn" and ".xt.lit") Functions. */
664 static void xtensa_create_property_segments
665 PARAMS ((frag_predicate, const char *, xt_section_type));
666 static segment_info_type *retrieve_segment_info
668 static segT retrieve_xtensa_section
670 static bfd_boolean section_has_property
671 PARAMS ((segT sec, frag_predicate));
672 static void add_xt_block_frags
673 PARAMS ((segT, segT, xtensa_block_info **, frag_predicate));
674 static bfd_boolean get_frag_is_literal
675 PARAMS ((const fragS *));
676 static bfd_boolean get_frag_is_insn
677 PARAMS ((const fragS *));
679 /* Import from elf32-xtensa.c in BFD library. */
680 extern char *xtensa_get_property_section_name
681 PARAMS ((asection *, const char *));
683 /* TInsn and IStack functions. */
684 static bfd_boolean tinsn_has_symbolic_operands
685 PARAMS ((const TInsn *));
686 static bfd_boolean tinsn_has_invalid_symbolic_operands
687 PARAMS ((const TInsn *));
688 static bfd_boolean tinsn_has_complex_operands
689 PARAMS ((const TInsn *));
690 static bfd_boolean tinsn_to_insnbuf
691 PARAMS ((TInsn *, xtensa_insnbuf));
692 static bfd_boolean tinsn_check_arguments
693 PARAMS ((const TInsn *));
694 static void tinsn_from_chars
695 PARAMS ((TInsn *, char *));
696 static void tinsn_immed_from_frag
697 PARAMS ((TInsn *, fragS *));
698 static int get_num_stack_text_bytes
700 static int get_num_stack_literal_bytes
703 /* Expression Utilities. */
704 bfd_boolean expr_is_const
705 PARAMS ((const expressionS *));
706 offsetT get_expr_const
707 PARAMS ((const expressionS *));
709 PARAMS ((expressionS *, offsetT));
710 void set_expr_symbol_offset
711 PARAMS ((expressionS *, symbolS *, offsetT));
712 bfd_boolean expr_is_equal
713 PARAMS ((expressionS *, expressionS *));
714 static void copy_expr
715 PARAMS ((expressionS *, const expressionS *));
717 #ifdef XTENSA_SECTION_RENAME
718 static void build_section_rename
719 PARAMS ((const char *));
720 static void add_section_rename
721 PARAMS ((char *, char *));
725 /* ISA imported from bfd. */
726 extern xtensa_isa xtensa_default_isa;
728 extern int target_big_endian;
730 static xtensa_opcode xtensa_addi_opcode;
731 static xtensa_opcode xtensa_addmi_opcode;
732 static xtensa_opcode xtensa_call0_opcode;
733 static xtensa_opcode xtensa_call4_opcode;
734 static xtensa_opcode xtensa_call8_opcode;
735 static xtensa_opcode xtensa_call12_opcode;
736 static xtensa_opcode xtensa_callx0_opcode;
737 static xtensa_opcode xtensa_callx4_opcode;
738 static xtensa_opcode xtensa_callx8_opcode;
739 static xtensa_opcode xtensa_callx12_opcode;
740 static xtensa_opcode xtensa_entry_opcode;
741 static xtensa_opcode xtensa_isync_opcode;
742 static xtensa_opcode xtensa_j_opcode;
743 static xtensa_opcode xtensa_jx_opcode;
744 static xtensa_opcode xtensa_loop_opcode;
745 static xtensa_opcode xtensa_loopnez_opcode;
746 static xtensa_opcode xtensa_loopgtz_opcode;
747 static xtensa_opcode xtensa_nop_n_opcode;
748 static xtensa_opcode xtensa_or_opcode;
749 static xtensa_opcode xtensa_ret_opcode;
750 static xtensa_opcode xtensa_ret_n_opcode;
751 static xtensa_opcode xtensa_retw_opcode;
752 static xtensa_opcode xtensa_retw_n_opcode;
753 static xtensa_opcode xtensa_rsr_opcode;
754 static xtensa_opcode xtensa_waiti_opcode;
757 /* Command-line Options. */
759 bfd_boolean use_literal_section = TRUE;
760 static bfd_boolean align_targets = TRUE;
761 static bfd_boolean align_only_targets = FALSE;
762 static bfd_boolean software_a0_b_retw_interlock = TRUE;
763 static bfd_boolean has_a0_b_retw = FALSE;
764 static bfd_boolean workaround_a0_b_retw = TRUE;
766 static bfd_boolean software_avoid_b_j_loop_end = TRUE;
767 static bfd_boolean workaround_b_j_loop_end = TRUE;
768 static bfd_boolean maybe_has_b_j_loop_end = FALSE;
770 static bfd_boolean software_avoid_short_loop = TRUE;
771 static bfd_boolean workaround_short_loop = TRUE;
772 static bfd_boolean maybe_has_short_loop = FALSE;
774 static bfd_boolean software_avoid_close_loop_end = TRUE;
775 static bfd_boolean workaround_close_loop_end = TRUE;
776 static bfd_boolean maybe_has_close_loop_end = FALSE;
778 /* When avoid_short_loops is true, all loops with early exits must
779 have at least 3 instructions. avoid_all_short_loops is a modifier
780 to the avoid_short_loop flag. In addition to the avoid_short_loop
781 actions, all straightline loopgtz and loopnez must have at least 3
784 static bfd_boolean software_avoid_all_short_loops = TRUE;
785 static bfd_boolean workaround_all_short_loops = TRUE;
787 /* This is on a per-instruction basis. */
788 static bfd_boolean specific_opcode = FALSE;
792 option_density = OPTION_MD_BASE,
801 option_text_section_literals,
802 option_no_text_section_literals,
804 option_align_targets,
805 option_no_align_targets,
807 option_align_only_targets,
808 option_no_align_only_targets,
813 option_workaround_a0_b_retw,
814 option_no_workaround_a0_b_retw,
816 option_workaround_b_j_loop_end,
817 option_no_workaround_b_j_loop_end,
819 option_workaround_short_loop,
820 option_no_workaround_short_loop,
822 option_workaround_all_short_loops,
823 option_no_workaround_all_short_loops,
825 option_workaround_close_loop_end,
826 option_no_workaround_close_loop_end,
828 option_no_workarounds,
830 #ifdef XTENSA_SECTION_RENAME
831 option_literal_section_name,
832 option_text_section_name,
833 option_data_section_name,
834 option_bss_section_name,
835 option_rename_section_name,
842 const char *md_shortopts = "";
844 struct option md_longopts[] =
846 {"density", no_argument, NULL, option_density},
847 {"no-density", no_argument, NULL, option_no_density},
848 /* At least as early as alameda, --[no-]relax didn't work as
849 documented, so as of albany, --[no-]relax is equivalent to
850 --[no-]generics. Both of these will be deprecated in
852 {"relax", no_argument, NULL, option_generics},
853 {"no-relax", no_argument, NULL, option_no_generics},
854 {"generics", no_argument, NULL, option_generics},
855 {"no-generics", no_argument, NULL, option_no_generics},
856 {"text-section-literals", no_argument, NULL, option_text_section_literals},
857 {"no-text-section-literals", no_argument, NULL,
858 option_no_text_section_literals},
859 /* This option was changed from -align-target to -target-align
860 because it conflicted with the "-al" option. */
861 {"target-align", no_argument, NULL, option_align_targets},
862 {"no-target-align", no_argument, NULL,
863 option_no_align_targets},
865 /* This option should do a better job aligning targets because
866 it will only attempt to align targets that are the target of a
868 { "target-align-only", no_argument, NULL, option_align_only_targets },
869 { "no-target-align-only", no_argument, NULL, option_no_align_only_targets },
871 {"longcalls", no_argument, NULL, option_longcalls},
872 {"no-longcalls", no_argument, NULL, option_no_longcalls},
874 {"no-workaround-a0-b-retw", no_argument, NULL,
875 option_no_workaround_a0_b_retw},
876 {"workaround-a0-b-retw", no_argument, NULL, option_workaround_a0_b_retw},
878 {"no-workaround-b-j-loop-end", no_argument, NULL,
879 option_no_workaround_b_j_loop_end},
880 {"workaround-b-j-loop-end", no_argument, NULL,
881 option_workaround_b_j_loop_end},
883 {"no-workaround-short-loops", no_argument, NULL,
884 option_no_workaround_short_loop},
885 {"workaround-short-loops", no_argument, NULL, option_workaround_short_loop},
887 {"no-workaround-all-short-loops", no_argument, NULL,
888 option_no_workaround_all_short_loops},
889 {"workaround-all-short-loop", no_argument, NULL,
890 option_workaround_all_short_loops},
892 {"no-workaround-close-loop-end", no_argument, NULL,
893 option_no_workaround_close_loop_end},
894 {"workaround-close-loop-end", no_argument, NULL,
895 option_workaround_close_loop_end},
897 {"no-workarounds", no_argument, NULL, option_no_workarounds},
899 #ifdef XTENSA_SECTION_RENAME
900 {"literal-section-name", required_argument, NULL,
901 option_literal_section_name},
902 {"text-section-name", required_argument, NULL,
903 option_text_section_name},
904 {"data-section-name", required_argument, NULL,
905 option_data_section_name},
906 {"rename-section", required_argument, NULL,
907 option_rename_section_name},
908 {"bss-section-name", required_argument, NULL,
909 option_bss_section_name},
910 #endif /* XTENSA_SECTION_RENAME */
912 {NULL, no_argument, NULL, 0}
915 size_t md_longopts_size = sizeof md_longopts;
919 md_parse_option (c, arg)
926 if (!density_supported)
928 as_bad (_("'--density' option not supported in this Xtensa "
932 directive_state[directive_density] = TRUE;
934 case option_no_density:
935 directive_state[directive_density] = FALSE;
937 case option_generics:
938 directive_state[directive_generics] = TRUE;
940 case option_no_generics:
941 directive_state[directive_generics] = FALSE;
943 case option_longcalls:
944 directive_state[directive_longcalls] = TRUE;
946 case option_no_longcalls:
947 directive_state[directive_longcalls] = FALSE;
949 case option_text_section_literals:
950 use_literal_section = FALSE;
952 case option_no_text_section_literals:
953 use_literal_section = TRUE;
955 case option_workaround_a0_b_retw:
956 workaround_a0_b_retw = TRUE;
957 software_a0_b_retw_interlock = TRUE;
959 case option_no_workaround_a0_b_retw:
960 workaround_a0_b_retw = FALSE;
961 software_a0_b_retw_interlock = FALSE;
963 case option_workaround_b_j_loop_end:
964 workaround_b_j_loop_end = TRUE;
965 software_avoid_b_j_loop_end = TRUE;
967 case option_no_workaround_b_j_loop_end:
968 workaround_b_j_loop_end = FALSE;
969 software_avoid_b_j_loop_end = FALSE;
972 case option_workaround_short_loop:
973 workaround_short_loop = TRUE;
974 software_avoid_short_loop = TRUE;
976 case option_no_workaround_short_loop:
977 workaround_short_loop = FALSE;
978 software_avoid_short_loop = FALSE;
981 case option_workaround_all_short_loops:
982 workaround_all_short_loops = TRUE;
983 software_avoid_all_short_loops = TRUE;
985 case option_no_workaround_all_short_loops:
986 workaround_all_short_loops = FALSE;
987 software_avoid_all_short_loops = FALSE;
990 case option_workaround_close_loop_end:
991 workaround_close_loop_end = TRUE;
992 software_avoid_close_loop_end = TRUE;
994 case option_no_workaround_close_loop_end:
995 workaround_close_loop_end = FALSE;
996 software_avoid_close_loop_end = FALSE;
999 case option_no_workarounds:
1000 workaround_a0_b_retw = FALSE;
1001 software_a0_b_retw_interlock = FALSE;
1002 workaround_b_j_loop_end = FALSE;
1003 software_avoid_b_j_loop_end = FALSE;
1004 workaround_short_loop = FALSE;
1005 software_avoid_short_loop = FALSE;
1006 workaround_all_short_loops = FALSE;
1007 software_avoid_all_short_loops = FALSE;
1008 workaround_close_loop_end = FALSE;
1009 software_avoid_close_loop_end = FALSE;
1012 case option_align_targets:
1013 align_targets = TRUE;
1015 case option_no_align_targets:
1016 align_targets = FALSE;
1019 case option_align_only_targets:
1020 align_only_targets = TRUE;
1022 case option_no_align_only_targets:
1023 align_only_targets = FALSE;
1026 #ifdef XTENSA_SECTION_RENAME
1027 case option_literal_section_name:
1028 add_section_rename (".literal", arg);
1029 as_warn (_("'--literal-section-name' is deprecated; "
1030 "use '--rename-section .literal=NEWNAME'"));
1033 case option_text_section_name:
1034 add_section_rename (".text", arg);
1035 as_warn (_("'--text-section-name' is deprecated; "
1036 "use '--rename-section .text=NEWNAME'"));
1039 case option_data_section_name:
1040 add_section_rename (".data", arg);
1041 as_warn (_("'--data-section-name' is deprecated; "
1042 "use '--rename-section .data=NEWNAME'"));
1045 case option_bss_section_name:
1046 add_section_rename (".bss", arg);
1047 as_warn (_("'--bss-section-name' is deprecated; "
1048 "use '--rename-section .bss=NEWNAME'"));
1051 case option_rename_section_name:
1052 build_section_rename (arg);
1054 #endif /* XTENSA_SECTION_RENAME */
1057 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
1058 should be emitted or not. FIXME: Not implemented. */
1068 md_show_usage (stream)
1071 fputs ("\nXtensa options:\n"
1072 "--[no-]density [Do not] emit density instructions\n"
1073 "--[no-]relax [Do not] perform branch relaxation\n"
1074 "--[no-]generics [Do not] transform instructions\n"
1075 "--[no-]longcalls [Do not] emit 32-bit call sequences\n"
1076 "--[no-]target-align [Do not] try to align branch targets\n"
1077 "--[no-]text-section-literals\n"
1078 " [Do not] put literals in the text section\n"
1079 "--no-workarounds Do not use any Xtensa workarounds\n"
1080 #ifdef XTENSA_SECTION_RENAME
1081 "--rename-section old=new(:old1=new1)*\n"
1082 " Rename section 'old' to 'new'\n"
1083 "\nThe following Xtensa options are deprecated\n"
1084 "--literal-section-name Name of literal section (default .literal)\n"
1085 "--text-section-name Name of text section (default .text)\n"
1086 "--data-section-name Name of data section (default .data)\n"
1087 "--bss-section-name Name of bss section (default .bss)\n"
1093 /* Directive data and functions. */
1095 typedef struct state_stackS_struct
1097 directiveE directive;
1098 bfd_boolean negated;
1099 bfd_boolean old_state;
1103 struct state_stackS_struct *prev;
1106 state_stackS *directive_state_stack;
1108 const pseudo_typeS md_pseudo_table[] =
1110 {"align", s_align_bytes, 0}, /* Defaulting is invalid (0) */
1111 {"literal_position", xtensa_literal_position, 0},
1112 {"frame", s_ignore, 0}, /* formerly used for STABS debugging */
1114 {"begin", xtensa_begin_directive, 0},
1115 {"end", xtensa_end_directive, 0},
1116 {"literal", xtensa_literal_pseudo, 0},
1124 return directive_state[directive_generics];
1131 return directive_state[directive_longcalls];
1136 code_density_available ()
1138 return directive_state[directive_density];
1145 return use_generics ();
1150 directive_push (directive, negated, datum)
1151 directiveE directive;
1152 bfd_boolean negated;
1157 state_stackS *stack = (state_stackS *) xmalloc (sizeof (state_stackS));
1159 as_where (&file, &line);
1161 stack->directive = directive;
1162 stack->negated = negated;
1163 stack->old_state = directive_state[directive];
1166 stack->datum = datum;
1167 stack->prev = directive_state_stack;
1168 directive_state_stack = stack;
1170 directive_state[directive] = !negated;
1174 directive_pop (directive, negated, file, line, datum)
1175 directiveE *directive;
1176 bfd_boolean *negated;
1181 state_stackS *top = directive_state_stack;
1183 if (!directive_state_stack)
1185 as_bad (_("unmatched end directive"));
1186 *directive = directive_none;
1190 directive_state[directive_state_stack->directive] = top->old_state;
1191 *directive = top->directive;
1192 *negated = top->negated;
1195 *datum = top->datum;
1196 directive_state_stack = top->prev;
1202 directive_balance ()
1204 while (directive_state_stack)
1206 directiveE directive;
1207 bfd_boolean negated;
1212 directive_pop (&directive, &negated, &file, &line, &datum);
1213 as_warn_where ((char *) file, line,
1214 _(".begin directive with no matching .end directive"));
1220 inside_directive (dir)
1223 state_stackS *top = directive_state_stack;
1225 while (top && top->directive != dir)
1228 return (top != NULL);
1233 get_directive (directive, negated)
1234 directiveE *directive;
1235 bfd_boolean *negated;
1240 if (strncmp (input_line_pointer, "no-", 3) != 0)
1245 input_line_pointer += 3;
1248 len = strspn (input_line_pointer,
1249 "abcdefghijklmnopqrstuvwxyz_/0123456789.");
1251 for (i = 0; i < sizeof (directive_info) / sizeof (*directive_info); ++i)
1253 if (strncmp (input_line_pointer, directive_info[i].name, len) == 0)
1255 input_line_pointer += len;
1256 *directive = (directiveE) i;
1257 if (*negated && !directive_info[i].can_be_negated)
1258 as_bad (_("directive %s can't be negated"),
1259 directive_info[i].name);
1264 as_bad (_("unknown directive"));
1265 *directive = (directiveE) XTENSA_UNDEFINED;
1270 xtensa_begin_directive (ignore)
1271 int ignore ATTRIBUTE_UNUSED;
1273 directiveE directive;
1274 bfd_boolean negated;
1279 md_flush_pending_output ();
1281 get_directive (&directive, &negated);
1282 if (directive == (directiveE) XTENSA_UNDEFINED)
1284 discard_rest_of_line ();
1290 case directive_literal:
1291 if (!inside_directive (directive_literal))
1293 /* Previous labels go with whatever follows this directive, not with
1294 the literal, so save them now. */
1295 saved_insn_labels = insn_labels;
1298 state = (emit_state *) xmalloc (sizeof (emit_state));
1299 xtensa_switch_to_literal_fragment (state);
1300 directive_push (directive_literal, negated, state);
1303 case directive_literal_prefix:
1304 /* Check to see if the current fragment is a literal
1305 fragment. If it is, then this operation is not allowed. */
1306 if (frag_now->tc_frag_data.is_literal)
1308 as_bad (_("cannot set literal_prefix inside literal fragment"));
1312 /* Allocate the literal state for this section and push
1313 onto the directive stack. */
1314 ls = xmalloc (sizeof (lit_state));
1317 *ls = default_lit_sections;
1319 directive_push (directive_literal_prefix, negated, ls);
1321 /* Parse the new prefix from the input_line_pointer. */
1323 len = strspn (input_line_pointer,
1324 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1325 "abcdefghijklmnopqrstuvwxyz_/0123456789.$");
1327 /* Process the new prefix. */
1328 xtensa_literal_prefix (input_line_pointer, len);
1330 /* Skip the name in the input line. */
1331 input_line_pointer += len;
1334 case directive_freeregs:
1335 /* This information is currently unused, but we'll accept the statement
1336 and just discard the rest of the line. This won't check the syntax,
1337 but it will accept every correct freeregs directive. */
1338 input_line_pointer += strcspn (input_line_pointer, "\n");
1339 directive_push (directive_freeregs, negated, 0);
1342 case directive_density:
1343 if (!density_supported && !negated)
1345 as_warn (_("Xtensa density option not supported; ignored"));
1351 directive_push (directive, negated, 0);
1355 demand_empty_rest_of_line ();
1360 xtensa_end_directive (ignore)
1361 int ignore ATTRIBUTE_UNUSED;
1363 directiveE begin_directive, end_directive;
1364 bfd_boolean begin_negated, end_negated;
1370 md_flush_pending_output ();
1372 get_directive (&end_directive, &end_negated);
1373 if (end_directive == (directiveE) XTENSA_UNDEFINED)
1375 discard_rest_of_line ();
1379 if (end_directive == directive_density && !density_supported && !end_negated)
1381 as_warn (_("Xtensa density option not supported; ignored"));
1382 demand_empty_rest_of_line ();
1386 directive_pop (&begin_directive, &begin_negated, &file, &line,
1387 (const void **) &state);
1389 if (begin_directive != directive_none)
1391 if (begin_directive != end_directive || begin_negated != end_negated)
1393 as_bad (_("does not match begin %s%s at %s:%d"),
1394 begin_negated ? "no-" : "",
1395 directive_info[begin_directive].name, file, line);
1399 switch (end_directive)
1401 case directive_literal:
1402 frag_var (rs_fill, 0, 0, 0, NULL, 0, NULL);
1403 xtensa_restore_emit_state (state);
1405 if (!inside_directive (directive_literal))
1407 /* Restore the list of current labels. */
1408 xtensa_clear_insn_labels ();
1409 insn_labels = saved_insn_labels;
1413 case directive_freeregs:
1416 case directive_literal_prefix:
1417 /* Restore the default collection sections from saved state. */
1418 s = (lit_state *) state;
1421 if (use_literal_section)
1422 default_lit_sections = *s;
1424 /* free the state storage */
1434 demand_empty_rest_of_line ();
1438 /* Place an aligned literal fragment at the current location. */
1441 xtensa_literal_position (ignore)
1442 int ignore ATTRIBUTE_UNUSED;
1444 if (inside_directive (directive_literal))
1445 as_warn (_(".literal_position inside literal directive; ignoring"));
1446 else if (!use_literal_section)
1447 xtensa_mark_literal_pool_location ();
1449 demand_empty_rest_of_line ();
1450 xtensa_clear_insn_labels ();
1454 /* Support .literal label, value@plt + offset. */
1457 xtensa_literal_pseudo (ignored)
1458 int ignored ATTRIBUTE_UNUSED;
1461 char *p, *base_name;
1466 if (inside_directive (directive_literal))
1468 as_bad (_(".literal not allowed inside .begin literal region"));
1469 ignore_rest_of_line ();
1473 /* Previous labels go with whatever follows this directive, not with
1474 the literal, so save them now. */
1475 saved_insn_labels = insn_labels;
1478 /* If we are using text-section literals, then this is the right value... */
1481 base_name = input_line_pointer;
1483 xtensa_switch_to_literal_fragment (&state);
1485 /* ...but if we aren't using text-section-literals, then we
1486 need to put them in the section we just switched to. */
1487 if (use_literal_section)
1490 /* All literals are aligned to four-byte boundaries
1491 which is handled by switch to literal fragment. */
1492 /* frag_align (2, 0, 0); */
1494 c = get_symbol_end ();
1495 /* Just after name is now '\0'. */
1496 p = input_line_pointer;
1500 if (*input_line_pointer != ',' && *input_line_pointer != ':')
1502 as_bad (_("expected comma or colon after symbol name; "
1503 "rest of line ignored"));
1504 ignore_rest_of_line ();
1505 xtensa_restore_emit_state (&state);
1514 input_line_pointer++; /* skip ',' or ':' */
1518 /* We only support 4-byte literals with .literal. */
1519 emit_expr (&expP, 4);
1521 while (*input_line_pointer == ',');
1525 demand_empty_rest_of_line ();
1527 xtensa_restore_emit_state (&state);
1529 /* Restore the list of current labels. */
1530 xtensa_clear_insn_labels ();
1531 insn_labels = saved_insn_labels;
1536 xtensa_literal_prefix (start, len)
1540 segT s_now; /* Storage for the current seg and subseg. */
1542 char *name; /* Pointer to the name itself. */
1545 if (!use_literal_section)
1548 /* Store away the current section and subsection. */
1550 ss_now = now_subseg;
1552 /* Get a null-terminated copy of the name. */
1553 name = xmalloc (len + 1);
1556 strncpy (name, start, len);
1559 /* Allocate the sections (interesting note: the memory pointing to
1560 the name is actually used for the name by the new section). */
1561 newname = xmalloc (len + strlen (".literal") + 1);
1562 strcpy (newname, name);
1563 strcpy (newname + len, ".literal");
1565 /* Note that retrieve_literal_seg does not create a segment if
1566 it already exists. */
1567 default_lit_sections.lit_seg = NULL; /* retrieved on demand */
1569 /* Canonicalizing section names allows renaming literal
1570 sections to occur correctly. */
1571 default_lit_sections.lit_seg_name =
1572 tc_canonicalize_symbol_name (newname);
1576 /* Restore the current section and subsection and set the
1577 generation into the old segment. */
1578 subseg_set (s_now, ss_now);
1582 /* Parsing and Idiom Translation. */
1585 expression_end (name)
1607 #define ERROR_REG_NUM ((unsigned) -1)
1610 tc_get_register (prefix)
1614 const char *next_expr;
1615 const char *old_line_pointer;
1618 old_line_pointer = input_line_pointer;
1620 if (*input_line_pointer == '$')
1621 ++input_line_pointer;
1623 /* Accept "sp" as a synonym for "a1". */
1624 if (input_line_pointer[0] == 's' && input_line_pointer[1] == 'p'
1625 && expression_end (input_line_pointer + 2))
1627 input_line_pointer += 2;
1628 return 1; /* AR[1] */
1631 while (*input_line_pointer++ == *prefix++)
1633 --input_line_pointer;
1638 as_bad (_("bad register name: %s"), old_line_pointer);
1639 return ERROR_REG_NUM;
1642 if (!ISDIGIT ((unsigned char) *input_line_pointer))
1644 as_bad (_("bad register number: %s"), input_line_pointer);
1645 return ERROR_REG_NUM;
1650 while (ISDIGIT ((int) *input_line_pointer))
1651 reg = reg * 10 + *input_line_pointer++ - '0';
1653 if (!(next_expr = expression_end (input_line_pointer)))
1655 as_bad (_("bad register name: %s"), old_line_pointer);
1656 return ERROR_REG_NUM;
1659 input_line_pointer = (char *) next_expr;
1665 #define PLT_SUFFIX "@PLT"
1666 #define plt_suffix "@plt"
1669 expression_maybe_register (opnd, tok)
1670 xtensa_operand opnd;
1673 char *kind = xtensa_operand_kind (opnd);
1675 if ((strlen (kind) == 1)
1676 && (*kind == 'l' || *kind == 'L' || *kind == 'i' || *kind == 'r'))
1678 segT t = expression (tok);
1679 if (t == absolute_section && operand_is_pcrel_label (opnd))
1681 assert (tok->X_op == O_constant);
1682 tok->X_op = O_symbol;
1683 tok->X_add_symbol = &abs_symbol;
1685 if (tok->X_op == O_symbol
1686 && (!strncmp (input_line_pointer, PLT_SUFFIX,
1687 strlen (PLT_SUFFIX) - 1)
1688 || !strncmp (input_line_pointer, plt_suffix,
1689 strlen (plt_suffix) - 1)))
1691 symbol_get_tc (tok->X_add_symbol)->plt = 1;
1692 input_line_pointer += strlen (plt_suffix);
1697 unsigned reg = tc_get_register (kind);
1699 if (reg != ERROR_REG_NUM) /* Already errored */
1702 if ((xtensa_operand_encode (opnd, &buf) != xtensa_encode_result_ok)
1703 || (reg != xtensa_operand_decode (opnd, buf)))
1704 as_bad (_("register number out of range"));
1707 tok->X_op = O_register;
1708 tok->X_add_symbol = 0;
1709 tok->X_add_number = reg;
1714 /* Split up the arguments for an opcode or pseudo-op. */
1717 tokenize_arguments (args, str)
1721 char *old_input_line_pointer;
1722 bfd_boolean saw_comma = FALSE;
1723 bfd_boolean saw_arg = FALSE;
1725 char *arg_end, *arg;
1728 /* Save and restore input_line_pointer around this function. */
1729 old_input_line_pointer = input_line_pointer;
1730 input_line_pointer = str;
1732 while (*input_line_pointer)
1735 switch (*input_line_pointer)
1741 input_line_pointer++;
1742 if (saw_comma || !saw_arg)
1748 if (!saw_comma && saw_arg)
1751 arg_end = input_line_pointer + 1;
1752 while (!expression_end (arg_end))
1755 arg_len = arg_end - input_line_pointer;
1756 arg = (char *) xmalloc (arg_len + 1);
1757 args[num_args] = arg;
1759 strncpy (arg, input_line_pointer, arg_len);
1760 arg[arg_len] = '\0';
1762 input_line_pointer = arg_end;
1773 input_line_pointer = old_input_line_pointer;
1777 input_line_pointer = old_input_line_pointer;
1782 /* Parse the arguments to an opcode. Return true on error. */
1785 parse_arguments (insn, num_args, arg_strings)
1790 expressionS *tok = insn->tok;
1791 xtensa_opcode opcode = insn->opcode;
1792 bfd_boolean had_error = TRUE;
1793 xtensa_isa isa = xtensa_default_isa;
1795 int opcode_operand_count;
1796 int actual_operand_count = 0;
1797 xtensa_operand opnd = NULL;
1798 char *old_input_line_pointer;
1800 if (insn->insn_type == ITYPE_LITERAL)
1801 opcode_operand_count = 1;
1803 opcode_operand_count = xtensa_num_operands (isa, opcode);
1805 memset (tok, 0, sizeof (*tok) * MAX_INSN_ARGS);
1807 /* Save and restore input_line_pointer around this function. */
1808 old_input_line_pointer = input_line_pointer;
1810 for (n = 0; n < num_args; n++)
1812 input_line_pointer = arg_strings[n];
1814 if (actual_operand_count >= opcode_operand_count)
1816 as_warn (_("too many arguments"));
1819 assert (actual_operand_count < MAX_INSN_ARGS);
1821 opnd = xtensa_get_operand (isa, opcode, actual_operand_count);
1822 expression_maybe_register (opnd, tok);
1824 if (tok->X_op == O_illegal || tok->X_op == O_absent)
1826 actual_operand_count++;
1830 insn->ntok = tok - insn->tok;
1834 input_line_pointer = old_input_line_pointer;
1840 xg_reverse_shift_count (cnt_argp)
1843 char *cnt_arg, *new_arg;
1844 cnt_arg = *cnt_argp;
1846 /* replace the argument with "31-(argument)" */
1847 new_arg = (char *) xmalloc (strlen (cnt_arg) + 6);
1848 sprintf (new_arg, "31-(%s)", cnt_arg);
1851 *cnt_argp = new_arg;
1855 /* If "arg" is a constant expression, return non-zero with the value
1859 xg_arg_is_constant (arg, valp)
1864 char *save_ptr = input_line_pointer;
1866 input_line_pointer = arg;
1868 input_line_pointer = save_ptr;
1870 if (exp.X_op == O_constant)
1872 *valp = exp.X_add_number;
1881 xg_replace_opname (popname, newop)
1886 *popname = (char *) xmalloc (strlen (newop) + 1);
1887 strcpy (*popname, newop);
1892 xg_check_num_args (pnum_args, expected_num, opname, arg_strings)
1898 int num_args = *pnum_args;
1900 if (num_args < expected_num)
1902 as_bad (_("not enough operands (%d) for '%s'; expected %d"),
1903 num_args, opname, expected_num);
1907 if (num_args > expected_num)
1909 as_warn (_("too many operands (%d) for '%s'; expected %d"),
1910 num_args, opname, expected_num);
1911 while (num_args-- > expected_num)
1913 free (arg_strings[num_args]);
1914 arg_strings[num_args] = 0;
1916 *pnum_args = expected_num;
1925 xg_translate_sysreg_op (popname, pnum_args, arg_strings)
1930 char *opname, *new_opname;
1932 bfd_boolean has_underbar = FALSE;
1937 has_underbar = TRUE;
1941 /* Opname == [rw]ur... */
1943 if (opname[3] == '\0')
1945 /* If the register is not specified as part of the opcode,
1946 then get it from the operand and move it to the opcode. */
1948 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
1951 if (!xg_arg_is_constant (arg_strings[1], &val))
1953 as_bad (_("register number for `%s' is not a constant"), opname);
1956 if ((unsigned) val > 255)
1958 as_bad (_("register number (%ld) for `%s' is out of range"),
1963 /* Remove the last argument, which is now part of the opcode. */
1964 free (arg_strings[1]);
1968 /* Translate the opcode. */
1969 new_opname = (char *) xmalloc (8);
1970 sprintf (new_opname, "%s%cur%u", (has_underbar ? "_" : ""),
1971 opname[0], (unsigned) val);
1973 *popname = new_opname;
1980 /* If the instruction is an idiom (i.e., a built-in macro), translate it.
1981 Returns non-zero if an error was found. */
1984 xg_translate_idioms (popname, pnum_args, arg_strings)
1989 char *opname = *popname;
1990 bfd_boolean has_underbar = FALSE;
1994 has_underbar = TRUE;
1998 if (strcmp (opname, "mov") == 0)
2000 if (!has_underbar && code_density_available ())
2001 xg_replace_opname (popname, "mov.n");
2004 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
2006 xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
2007 arg_strings[2] = (char *) xmalloc (strlen (arg_strings[1]) + 1);
2008 strcpy (arg_strings[2], arg_strings[1]);
2014 if (strcmp (opname, "bbsi.l") == 0)
2016 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2018 xg_replace_opname (popname, (has_underbar ? "_bbsi" : "bbsi"));
2019 if (target_big_endian)
2020 xg_reverse_shift_count (&arg_strings[1]);
2024 if (strcmp (opname, "bbci.l") == 0)
2026 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2028 xg_replace_opname (popname, (has_underbar ? "_bbci" : "bbci"));
2029 if (target_big_endian)
2030 xg_reverse_shift_count (&arg_strings[1]);
2034 if (strcmp (opname, "nop") == 0)
2036 if (!has_underbar && code_density_available ())
2037 xg_replace_opname (popname, "nop.n");
2040 if (xg_check_num_args (pnum_args, 0, opname, arg_strings))
2042 xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
2043 arg_strings[0] = (char *) xmalloc (3);
2044 arg_strings[1] = (char *) xmalloc (3);
2045 arg_strings[2] = (char *) xmalloc (3);
2046 strcpy (arg_strings[0], "a1");
2047 strcpy (arg_strings[1], "a1");
2048 strcpy (arg_strings[2], "a1");
2054 if ((opname[0] == 'r' || opname[0] == 'w')
2056 && opname[2] == 'r')
2057 return xg_translate_sysreg_op (popname, pnum_args, arg_strings);
2060 /* WIDENING DENSITY OPCODES
2062 questionable relaxations (widening) from old "tai" idioms:
2072 Note: this incomplete list was imported to match the "tai"
2073 behavior; other density opcodes are not handled.
2075 The xtensa-relax code may know how to do these but it doesn't do
2076 anything when these density opcodes appear inside a no-density
2077 region. Somehow GAS should either print an error when that happens
2078 or do the widening. The old "tai" behavior was to do the widening.
2079 For now, I'll make it widen but print a warning.
2081 FIXME: GAS needs to detect density opcodes inside no-density
2082 regions and treat them as errors. This code should be removed
2083 when that is done. */
2087 && density_supported
2088 && !code_density_available ())
2090 if (strcmp (opname, "add.n") == 0)
2091 xg_replace_opname (popname, "add");
2093 else if (strcmp (opname, "beqz.n") == 0)
2094 xg_replace_opname (popname, "beqz");
2096 else if (strcmp (opname, "ret.n") == 0)
2097 xg_replace_opname (popname, "ret");
2099 else if (strcmp (opname, "retw.n") == 0)
2100 xg_replace_opname (popname, "retw");
2102 else if (strcmp (opname, "movi.n") == 0)
2103 xg_replace_opname (popname, "movi");
2105 else if (strcmp (opname, "mov.n") == 0)
2107 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
2109 xg_replace_opname (popname, "or");
2110 arg_strings[2] = (char *) xmalloc (strlen (arg_strings[1]) + 1);
2111 strcpy (arg_strings[2], arg_strings[1]);
2115 else if (strcmp (opname, "nop.n") == 0)
2117 if (xg_check_num_args (pnum_args, 0, opname, arg_strings))
2119 xg_replace_opname (popname, "or");
2120 arg_strings[0] = (char *) xmalloc (3);
2121 arg_strings[1] = (char *) xmalloc (3);
2122 arg_strings[2] = (char *) xmalloc (3);
2123 strcpy (arg_strings[0], "a1");
2124 strcpy (arg_strings[1], "a1");
2125 strcpy (arg_strings[2], "a1");
2134 /* Functions for dealing with the Xtensa ISA. */
2136 /* Return true if the given operand is an immed or target instruction,
2137 i.e., has a reloc associated with it. Currently, this is only true
2138 if the operand kind is "i, "l" or "L". */
2141 operand_is_immed (opnd)
2142 xtensa_operand opnd;
2144 const char *opkind = xtensa_operand_kind (opnd);
2145 if (opkind[0] == '\0' || opkind[1] != '\0')
2158 /* Return true if the given operand is a pc-relative label. This is
2159 true for "l", "L", and "r" operand kinds. */
2162 operand_is_pcrel_label (opnd)
2163 xtensa_operand opnd;
2165 const char *opkind = xtensa_operand_kind (opnd);
2166 if (opkind[0] == '\0' || opkind[1] != '\0')
2179 /* Currently the assembler only allows us to use a single target per
2180 fragment. Because of this, only one operand for a given
2181 instruction may be symbolic. If there is an operand of kind "lrL",
2182 the last one is chosen. Otherwise, the result is the number of the
2183 last operand of type "i", and if there are none of those, we fail
2187 get_relaxable_immed (opcode)
2188 xtensa_opcode opcode;
2190 int last_immed = -1;
2192 xtensa_operand operand;
2194 if (opcode == XTENSA_UNDEFINED)
2197 noperands = xtensa_num_operands (xtensa_default_isa, opcode);
2198 for (opi = noperands - 1; opi >= 0; opi--)
2200 operand = xtensa_get_operand (xtensa_default_isa, opcode, opi);
2201 if (operand_is_pcrel_label (operand))
2203 if (last_immed == -1 && operand_is_immed (operand))
2211 get_opcode_from_buf (buf)
2214 static xtensa_insnbuf insnbuf = NULL;
2215 xtensa_opcode opcode;
2216 xtensa_isa isa = xtensa_default_isa;
2218 insnbuf = xtensa_insnbuf_alloc (isa);
2220 xtensa_insnbuf_from_chars (isa, insnbuf, buf);
2221 opcode = xtensa_decode_insn (isa, insnbuf);
2227 is_direct_call_opcode (opcode)
2228 xtensa_opcode opcode;
2230 if (opcode == XTENSA_UNDEFINED)
2233 return (opcode == xtensa_call0_opcode
2234 || opcode == xtensa_call4_opcode
2235 || opcode == xtensa_call8_opcode
2236 || opcode == xtensa_call12_opcode);
2241 is_call_opcode (opcode)
2242 xtensa_opcode opcode;
2244 if (is_direct_call_opcode (opcode))
2247 if (opcode == XTENSA_UNDEFINED)
2250 return (opcode == xtensa_callx0_opcode
2251 || opcode == xtensa_callx4_opcode
2252 || opcode == xtensa_callx8_opcode
2253 || opcode == xtensa_callx12_opcode);
2257 /* Return true if the opcode is an entry opcode. This is used because
2258 "entry" adds an implicit ".align 4" and also the entry instruction
2259 has an extra check for an operand value. */
2262 is_entry_opcode (opcode)
2263 xtensa_opcode opcode;
2265 if (opcode == XTENSA_UNDEFINED)
2268 return (opcode == xtensa_entry_opcode);
2272 /* Return true if it is one of the loop opcodes. Loops are special
2273 because they need automatic alignment and they have a relaxation so
2274 complex that we hard-coded it. */
2277 is_loop_opcode (opcode)
2278 xtensa_opcode opcode;
2280 if (opcode == XTENSA_UNDEFINED)
2283 return (opcode == xtensa_loop_opcode
2284 || opcode == xtensa_loopnez_opcode
2285 || opcode == xtensa_loopgtz_opcode);
2290 is_the_loop_opcode (opcode)
2291 xtensa_opcode opcode;
2293 if (opcode == XTENSA_UNDEFINED)
2296 return (opcode == xtensa_loop_opcode);
2301 is_jx_opcode (opcode)
2302 xtensa_opcode opcode;
2304 if (opcode == XTENSA_UNDEFINED)
2307 return (opcode == xtensa_jx_opcode);
2311 /* Return true if the opcode is a retw or retw.n.
2312 Needed to add nops to avoid a hardware interlock issue. */
2315 is_windowed_return_opcode (opcode)
2316 xtensa_opcode opcode;
2318 if (opcode == XTENSA_UNDEFINED)
2321 return (opcode == xtensa_retw_opcode || opcode == xtensa_retw_n_opcode);
2325 /* Return true if the opcode type is "l" and the opcode is NOT a jump. */
2328 is_conditional_branch_opcode (opcode)
2329 xtensa_opcode opcode;
2331 xtensa_isa isa = xtensa_default_isa;
2334 if (opcode == xtensa_j_opcode && opcode != XTENSA_UNDEFINED)
2337 num_ops = xtensa_num_operands (isa, opcode);
2338 for (i = 0; i < num_ops; i++)
2340 xtensa_operand operand = xtensa_get_operand (isa, opcode, i);
2341 if (strcmp (xtensa_operand_kind (operand), "l") == 0)
2348 /* Return true if the given opcode is a conditional branch
2349 instruction, i.e., currently this is true if the instruction
2350 is a jx or has an operand with 'l' type and is not a loop. */
2353 is_branch_or_jump_opcode (opcode)
2354 xtensa_opcode opcode;
2358 if (opcode == XTENSA_UNDEFINED)
2361 if (is_loop_opcode (opcode))
2364 if (is_jx_opcode (opcode))
2367 op_count = xtensa_num_operands (xtensa_default_isa, opcode);
2368 for (opn = 0; opn < op_count; opn++)
2370 xtensa_operand opnd =
2371 xtensa_get_operand (xtensa_default_isa, opcode, opn);
2372 const char *opkind = xtensa_operand_kind (opnd);
2373 if (opkind && opkind[0] == 'l' && opkind[1] == '\0')
2380 /* Convert from operand numbers to BFD relocation type code.
2381 Return BFD_RELOC_NONE on failure. */
2383 bfd_reloc_code_real_type
2384 opnum_to_reloc (opnum)
2390 return BFD_RELOC_XTENSA_OP0;
2392 return BFD_RELOC_XTENSA_OP1;
2394 return BFD_RELOC_XTENSA_OP2;
2398 return BFD_RELOC_NONE;
2402 /* Convert from BFD relocation type code to operand number.
2403 Return -1 on failure. */
2406 reloc_to_opnum (reloc)
2407 bfd_reloc_code_real_type reloc;
2411 case BFD_RELOC_XTENSA_OP0:
2413 case BFD_RELOC_XTENSA_OP1:
2415 case BFD_RELOC_XTENSA_OP2:
2425 xtensa_insnbuf_set_operand (insnbuf, opcode, operand, value, file, line)
2426 xtensa_insnbuf insnbuf;
2427 xtensa_opcode opcode;
2428 xtensa_operand operand;
2433 xtensa_encode_result encode_result;
2434 uint32 valbuf = value;
2436 encode_result = xtensa_operand_encode (operand, &valbuf);
2438 switch (encode_result)
2440 case xtensa_encode_result_ok:
2442 case xtensa_encode_result_align:
2443 as_bad_where ((char *) file, line,
2444 _("operand %d not properly aligned for '%s'"),
2445 value, xtensa_opcode_name (xtensa_default_isa, opcode));
2447 case xtensa_encode_result_not_in_table:
2448 as_bad_where ((char *) file, line,
2449 _("operand %d not in immediate table for '%s'"),
2450 value, xtensa_opcode_name (xtensa_default_isa, opcode));
2452 case xtensa_encode_result_too_high:
2453 as_bad_where ((char *) file, line,
2454 _("operand %d too large for '%s'"), value,
2455 xtensa_opcode_name (xtensa_default_isa, opcode));
2457 case xtensa_encode_result_too_low:
2458 as_bad_where ((char *) file, line,
2459 _("operand %d too small for '%s'"), value,
2460 xtensa_opcode_name (xtensa_default_isa, opcode));
2462 case xtensa_encode_result_not_ok:
2463 as_bad_where ((char *) file, line,
2464 _("operand %d is invalid for '%s'"), value,
2465 xtensa_opcode_name (xtensa_default_isa, opcode));
2471 xtensa_operand_set_field (operand, insnbuf, valbuf);
2476 xtensa_insnbuf_get_operand (insnbuf, opcode, opnum)
2477 xtensa_insnbuf insnbuf;
2478 xtensa_opcode opcode;
2481 xtensa_operand op = xtensa_get_operand (xtensa_default_isa, opcode, opnum);
2482 return xtensa_operand_decode (op, xtensa_operand_get_field (op, insnbuf));
2487 xtensa_insnbuf_set_immediate_field (opcode, insnbuf, value, file, line)
2488 xtensa_opcode opcode;
2489 xtensa_insnbuf insnbuf;
2494 xtensa_isa isa = xtensa_default_isa;
2495 int last_opnd = xtensa_num_operands (isa, opcode) - 1;
2496 xtensa_operand operand = xtensa_get_operand (isa, opcode, last_opnd);
2497 xtensa_insnbuf_set_operand (insnbuf, opcode, operand, value, file, line);
2502 is_negatable_branch (insn)
2505 xtensa_isa isa = xtensa_default_isa;
2507 int num_ops = xtensa_num_operands (isa, insn->opcode);
2509 for (i = 0; i < num_ops; i++)
2511 xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
2512 char *kind = xtensa_operand_kind (opnd);
2513 if (strlen (kind) == 1 && *kind == 'l')
2520 /* Various Other Internal Functions. */
2523 is_unique_insn_expansion (r)
2526 if (!r->to_instr || r->to_instr->next != NULL)
2528 if (r->to_instr->typ != INSTR_INSTR)
2535 xg_get_insn_size (insn)
2538 assert (insn->insn_type == ITYPE_INSN);
2539 return xtensa_insn_length (xtensa_default_isa, insn->opcode);
2544 xg_get_build_instr_size (insn)
2547 assert (insn->typ == INSTR_INSTR);
2548 return xtensa_insn_length (xtensa_default_isa, insn->opcode);
2553 xg_is_narrow_insn (insn)
2556 TransitionTable *table = xg_build_widen_table ();
2559 assert (insn->insn_type == ITYPE_INSN);
2560 assert (insn->opcode < table->num_opcodes);
2562 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
2564 TransitionRule *rule = l->rule;
2566 if (xg_instruction_matches_rule (insn, rule)
2567 && is_unique_insn_expansion (rule))
2569 /* It only generates one instruction... */
2570 assert (insn->insn_type == ITYPE_INSN);
2571 /* ...and it is a larger instruction. */
2572 if (xg_get_insn_size (insn)
2573 < xg_get_build_instr_size (rule->to_instr))
2581 return (num_match == 1);
2586 xg_is_single_relaxable_insn (insn)
2589 TransitionTable *table = xg_build_widen_table ();
2592 assert (insn->insn_type == ITYPE_INSN);
2593 assert (insn->opcode < table->num_opcodes);
2595 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
2597 TransitionRule *rule = l->rule;
2599 if (xg_instruction_matches_rule (insn, rule)
2600 && is_unique_insn_expansion (rule))
2602 assert (insn->insn_type == ITYPE_INSN);
2603 /* ... and it is a larger instruction. */
2604 if (xg_get_insn_size (insn)
2605 <= xg_get_build_instr_size (rule->to_instr))
2613 return (num_match == 1);
2617 /* Return the largest size instruction that this instruction can
2618 expand to. Currently, in all cases, this is 3 bytes. Of course we
2619 could just calculate this once and generate a table. */
2622 xg_get_max_narrow_insn_size (opcode)
2623 xtensa_opcode opcode;
2625 /* Go ahead and compute it, but it better be 3. */
2626 TransitionTable *table = xg_build_widen_table ();
2628 int old_size = xtensa_insn_length (xtensa_default_isa, opcode);
2629 assert (opcode < table->num_opcodes);
2631 /* Actually we can do better. Check to see of Only one applies. */
2632 for (l = table->table[opcode]; l != NULL; l = l->next)
2634 TransitionRule *rule = l->rule;
2636 /* If it only generates one instruction. */
2637 if (is_unique_insn_expansion (rule))
2639 int new_size = xtensa_insn_length (xtensa_default_isa,
2640 rule->to_instr->opcode);
2641 if (new_size > old_size)
2643 assert (new_size == 3);
2652 /* Return the maximum number of bytes this opcode can expand to. */
2655 xg_get_max_insn_widen_size (opcode)
2656 xtensa_opcode opcode;
2658 TransitionTable *table = xg_build_widen_table ();
2660 int max_size = xtensa_insn_length (xtensa_default_isa, opcode);
2662 assert (opcode < table->num_opcodes);
2664 for (l = table->table[opcode]; l != NULL; l = l->next)
2666 TransitionRule *rule = l->rule;
2667 BuildInstr *build_list;
2672 build_list = rule->to_instr;
2673 if (is_unique_insn_expansion (rule))
2675 assert (build_list->typ == INSTR_INSTR);
2676 this_size = xg_get_max_insn_widen_size (build_list->opcode);
2679 for (; build_list != NULL; build_list = build_list->next)
2681 switch (build_list->typ)
2684 this_size += xtensa_insn_length (xtensa_default_isa,
2685 build_list->opcode);
2688 case INSTR_LITERAL_DEF:
2689 case INSTR_LABEL_DEF:
2694 if (this_size > max_size)
2695 max_size = this_size;
2701 /* Return the maximum number of literal bytes this opcode can generate. */
2704 xg_get_max_insn_widen_literal_size (opcode)
2705 xtensa_opcode opcode;
2707 TransitionTable *table = xg_build_widen_table ();
2711 assert (opcode < table->num_opcodes);
2713 for (l = table->table[opcode]; l != NULL; l = l->next)
2715 TransitionRule *rule = l->rule;
2716 BuildInstr *build_list;
2721 build_list = rule->to_instr;
2722 if (is_unique_insn_expansion (rule))
2724 assert (build_list->typ == INSTR_INSTR);
2725 this_size = xg_get_max_insn_widen_literal_size (build_list->opcode);
2728 for (; build_list != NULL; build_list = build_list->next)
2730 switch (build_list->typ)
2732 case INSTR_LITERAL_DEF:
2733 /* hard coded 4-byte literal. */
2737 case INSTR_LABEL_DEF:
2742 if (this_size > max_size)
2743 max_size = this_size;
2750 xg_is_relaxable_insn (insn, lateral_steps)
2754 int steps_taken = 0;
2755 TransitionTable *table = xg_build_widen_table ();
2758 assert (insn->insn_type == ITYPE_INSN);
2759 assert (insn->opcode < table->num_opcodes);
2761 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
2763 TransitionRule *rule = l->rule;
2765 if (xg_instruction_matches_rule (insn, rule))
2767 if (steps_taken == lateral_steps)
2777 get_special_literal_symbol ()
2779 static symbolS *sym = NULL;
2782 sym = symbol_find_or_make ("SPECIAL_LITERAL0\001");
2788 get_special_label_symbol ()
2790 static symbolS *sym = NULL;
2793 sym = symbol_find_or_make ("SPECIAL_LABEL0\001");
2798 /* Return true on success. */
2801 xg_build_to_insn (targ, insn, bi)
2809 memset (targ, 0, sizeof (TInsn));
2814 targ->opcode = bi->opcode;
2815 targ->insn_type = ITYPE_INSN;
2816 targ->is_specific_opcode = FALSE;
2818 for (; op != NULL; op = op->next)
2820 int op_num = op->op_num;
2821 int op_data = op->op_data;
2823 assert (op->op_num < MAX_INSN_ARGS);
2825 if (targ->ntok <= op_num)
2826 targ->ntok = op_num + 1;
2831 set_expr_const (&targ->tok[op_num], op_data);
2834 assert (op_data < insn->ntok);
2835 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
2838 sym = get_special_literal_symbol ();
2839 set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
2842 sym = get_special_label_symbol ();
2843 set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
2846 /* currently handles:
2849 OP_OPERAND_F32MINUS */
2850 if (xg_has_userdef_op_fn (op->typ))
2852 assert (op_data < insn->ntok);
2853 if (expr_is_const (&insn->tok[op_data]))
2856 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
2857 val = xg_apply_userdef_op_fn (op->typ,
2860 targ->tok[op_num].X_add_number = val;
2863 return FALSE; /* We cannot use a relocation for this. */
2872 case INSTR_LITERAL_DEF:
2874 targ->opcode = XTENSA_UNDEFINED;
2875 targ->insn_type = ITYPE_LITERAL;
2876 targ->is_specific_opcode = FALSE;
2877 for (; op != NULL; op = op->next)
2879 int op_num = op->op_num;
2880 int op_data = op->op_data;
2881 assert (op->op_num < MAX_INSN_ARGS);
2883 if (targ->ntok <= op_num)
2884 targ->ntok = op_num + 1;
2889 assert (op_data < insn->ntok);
2890 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
2902 case INSTR_LABEL_DEF:
2904 targ->opcode = XTENSA_UNDEFINED;
2905 targ->insn_type = ITYPE_LABEL;
2906 targ->is_specific_opcode = FALSE;
2907 /* Literal with no ops. is a label? */
2908 assert (op == NULL);
2919 /* Return true on success. */
2922 xg_build_to_stack (istack, insn, bi)
2927 for (; bi != NULL; bi = bi->next)
2929 TInsn *next_insn = istack_push_space (istack);
2931 if (!xg_build_to_insn (next_insn, insn, bi))
2938 /* Return true on valid expansion. */
2941 xg_expand_to_stack (istack, insn, lateral_steps)
2946 int stack_size = istack->ninsn;
2947 int steps_taken = 0;
2948 TransitionTable *table = xg_build_widen_table ();
2951 assert (insn->insn_type == ITYPE_INSN);
2952 assert (insn->opcode < table->num_opcodes);
2954 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
2956 TransitionRule *rule = l->rule;
2958 if (xg_instruction_matches_rule (insn, rule))
2960 if (lateral_steps == steps_taken)
2964 /* This is it. Expand the rule to the stack. */
2965 if (!xg_build_to_stack (istack, insn, rule->to_instr))
2968 /* Check to see if it fits. */
2969 for (i = stack_size; i < istack->ninsn; i++)
2971 TInsn *insn = &istack->insn[i];
2973 if (insn->insn_type == ITYPE_INSN
2974 && !tinsn_has_symbolic_operands (insn)
2975 && !xg_immeds_fit (insn))
2977 istack->ninsn = stack_size;
2991 xg_expand_narrow (targ, insn)
2995 TransitionTable *table = xg_build_widen_table ();
2998 assert (insn->insn_type == ITYPE_INSN);
2999 assert (insn->opcode < table->num_opcodes);
3001 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3003 TransitionRule *rule = l->rule;
3004 if (xg_instruction_matches_rule (insn, rule)
3005 && is_unique_insn_expansion (rule))
3007 /* Is it a larger instruction? */
3008 if (xg_get_insn_size (insn)
3009 <= xg_get_build_instr_size (rule->to_instr))
3011 xg_build_to_insn (targ, insn, rule->to_instr);
3020 /* Assumes: All immeds are constants. Check that all constants fit
3021 into their immeds; return false if not. */
3024 xg_immeds_fit (insn)
3030 assert (insn->insn_type == ITYPE_INSN);
3031 for (i = 0; i < n; ++i)
3033 const expressionS *expr = &insn->tok[i];
3034 xtensa_operand opnd = xtensa_get_operand (xtensa_default_isa,
3036 if (!operand_is_immed (opnd))
3044 if (xg_check_operand (expr->X_add_number, opnd))
3049 /* The symbol should have a fixup associated with it. */
3058 /* This should only be called after we have an initial
3059 estimate of the addresses. */
3062 xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch)
3070 offsetT target, pc, new_offset;
3074 assert (insn->insn_type == ITYPE_INSN);
3076 for (i = 0; i < n; ++i)
3078 const expressionS *expr = &insn->tok[i];
3079 xtensa_operand opnd = xtensa_get_operand (xtensa_default_isa,
3081 if (!operand_is_immed (opnd))
3088 if (xg_check_operand (expr->X_add_number, opnd))
3093 /* We only allow symbols for pc-relative stuff.
3094 If pc_frag == 0, then we don't have frag locations yet. */
3098 /* If it is PC-relative and the symbol is in the same segment as
3100 if (!xtensa_operand_isPCRelative (opnd)
3101 || S_GET_SEGMENT (expr->X_add_symbol) != pc_seg)
3104 symbolP = expr->X_add_symbol;
3105 target = S_GET_VALUE (symbolP) + expr->X_add_number;
3106 pc = pc_frag->fr_address + pc_offset;
3108 /* If frag has yet to be reached on this pass, assume it
3109 will move by STRETCH just as we did. If this is not so,
3110 it will be because some frag between grows, and that will
3111 force another pass. Beware zero-length frags. There
3112 should be a faster way to do this. */
3114 if (stretch && is_dnrange (pc_frag, symbolP, stretch))
3117 new_offset = xtensa_operand_do_reloc (opnd, target, pc);
3118 if (xg_check_operand (new_offset, opnd))
3123 /* The symbol should have a fixup associated with it. */
3132 /* This will check to see if the value can be converted into the
3133 operand type. It will return true if it does not fit. */
3136 xg_check_operand (value, operand)
3138 xtensa_operand operand;
3140 uint32 valbuf = value;
3141 return (xtensa_operand_encode (operand, &valbuf) != xtensa_encode_result_ok);
3145 /* Check if a symbol is pointing to somewhere after
3146 the start frag, given that the segment has stretched
3147 by stretch during relaxation.
3149 This is more complicated than it might appear at first blush
3150 because of the stretching that goes on. Here is how the check
3153 If the symbol and the frag are in the same segment, then
3154 the symbol could be down range. Note that this function
3155 assumes that start_frag is in now_seg.
3157 If the symbol is pointing to a frag with an address greater than
3158 than the start_frag's address, then it _could_ be down range.
3160 The problem comes because target_frag may or may not have had
3161 stretch bytes added to its address already, depending on if it is
3162 before or after start frag. (And if we knew that, then we wouldn't
3163 need this function.) start_frag has definitely already had stretch
3164 bytes added to its address.
3166 If target_frag's address hasn't been adjusted yet, then to
3167 determine if it comes after start_frag, we need to subtract
3168 stretch from start_frag's address.
3170 If target_frag's address has been adjusted, then it might have
3171 been adjusted such that it comes after start_frag's address minus
3174 So, in that case, we scan for it down stream to within
3175 stretch bytes. We could search to the end of the fr_chain, but
3176 that ends up taking too much time (over a minute on some gnu
3180 is_dnrange (start_frag, sym, stretch)
3185 if (S_GET_SEGMENT (sym) == now_seg)
3187 fragS *cur_frag = symbol_get_frag (sym);
3189 if (cur_frag->fr_address >= start_frag->fr_address - stretch)
3191 int distance = stretch;
3193 while (cur_frag && distance >= 0)
3195 distance -= cur_frag->fr_fix;
3196 if (cur_frag == start_frag)
3198 cur_frag = cur_frag->fr_next;
3207 /* Relax the assembly instruction at least "min_steps".
3208 Return the number of steps taken. */
3211 xg_assembly_relax (istack, insn, pc_seg, pc_frag, pc_offset, min_steps,
3216 fragS *pc_frag; /* If pc_frag == 0, then no pc-relative. */
3217 offsetT pc_offset; /* Offset in fragment. */
3218 int min_steps; /* Minimum number of conversion steps. */
3219 long stretch; /* Number of bytes stretched so far. */
3221 int steps_taken = 0;
3223 /* assert (has no symbolic operands)
3224 Some of its immeds don't fit.
3225 Try to build a relaxed version.
3226 This may go through a couple of stages
3227 of single instruction transformations before
3230 TInsn single_target;
3232 int lateral_steps = 0;
3233 int istack_size = istack->ninsn;
3235 if (xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch)
3236 && steps_taken >= min_steps)
3238 istack_push (istack, insn);
3241 tinsn_copy (¤t_insn, insn);
3243 /* Walk through all of the single instruction expansions. */
3244 while (xg_is_single_relaxable_insn (¤t_insn))
3246 int error_val = xg_expand_narrow (&single_target, ¤t_insn);
3248 assert (!error_val);
3250 if (xg_symbolic_immeds_fit (&single_target, pc_seg, pc_frag, pc_offset,
3254 if (steps_taken >= min_steps)
3256 istack_push (istack, &single_target);
3260 tinsn_copy (¤t_insn, &single_target);
3263 /* Now check for a multi-instruction expansion. */
3264 while (xg_is_relaxable_insn (¤t_insn, lateral_steps))
3266 if (xg_symbolic_immeds_fit (¤t_insn, pc_seg, pc_frag, pc_offset,
3269 if (steps_taken >= min_steps)
3271 istack_push (istack, ¤t_insn);
3276 if (xg_expand_to_stack (istack, ¤t_insn, lateral_steps))
3278 if (steps_taken >= min_steps)
3282 istack->ninsn = istack_size;
3285 /* It's not going to work -- use the original. */
3286 istack_push (istack, insn);
3292 xg_force_frag_space (size)
3295 /* This may have the side effect of creating a new fragment for the
3296 space to go into. I just do not like the name of the "frag"
3303 xg_finish_frag (last_insn, state, max_growth, is_insn)
3305 enum xtensa_relax_statesE state;
3307 bfd_boolean is_insn;
3309 /* Finish off this fragment so that it has at LEAST the desired
3310 max_growth. If it doesn't fit in this fragment, close this one
3311 and start a new one. In either case, return a pointer to the
3312 beginning of the growth area. */
3315 xg_force_frag_space (max_growth);
3317 old_frag = frag_now;
3319 frag_now->fr_opcode = last_insn;
3321 frag_now->tc_frag_data.is_insn = TRUE;
3323 frag_var (rs_machine_dependent, max_growth, max_growth,
3324 state, frag_now->fr_symbol, frag_now->fr_offset, last_insn);
3326 /* Just to make sure that we did not split it up. */
3327 assert (old_frag->fr_next == frag_now);
3332 is_branch_jmp_to_next (insn, fragP)
3336 xtensa_isa isa = xtensa_default_isa;
3338 int num_ops = xtensa_num_operands (isa, insn->opcode);
3343 if (is_loop_opcode (insn->opcode))
3346 for (i = 0; i < num_ops; i++)
3348 xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
3349 char *kind = xtensa_operand_kind (opnd);
3350 if (strlen (kind) == 1 && *kind == 'l')
3356 if (target_op == -1)
3359 if (insn->ntok <= target_op)
3362 if (insn->tok[target_op].X_op != O_symbol)
3365 sym = insn->tok[target_op].X_add_symbol;
3369 if (insn->tok[target_op].X_add_number != 0)
3372 target_frag = symbol_get_frag (sym);
3373 if (target_frag == NULL)
3376 if (is_next_frag_target (fragP->fr_next, target_frag)
3377 && S_GET_VALUE (sym) == target_frag->fr_address)
3385 xg_add_branch_and_loop_targets (insn)
3388 xtensa_isa isa = xtensa_default_isa;
3389 int num_ops = xtensa_num_operands (isa, insn->opcode);
3391 if (is_loop_opcode (insn->opcode))
3394 xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
3395 char *kind = xtensa_operand_kind (opnd);
3396 if (strlen (kind) == 1 && *kind == 'l')
3397 if (insn->tok[i].X_op == O_symbol)
3398 symbol_get_tc (insn->tok[i].X_add_symbol)->is_loop_target = TRUE;
3402 /* Currently, we do not add branch targets. This is an optimization
3403 for later that tries to align only branch targets, not just any
3404 label in a text section. */
3406 if (align_only_targets)
3410 for (i = 0; i < insn->ntok && i < num_ops; i++)
3412 xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
3413 char *kind = xtensa_operand_kind (opnd);
3414 if (strlen (kind) == 1 && *kind == 'l'
3415 && insn->tok[i].X_op == O_symbol)
3417 symbolS *sym = insn->tok[i].X_add_symbol;
3418 symbol_get_tc (sym)->is_branch_target = TRUE;
3419 if (S_IS_DEFINED (sym))
3420 symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
3427 /* Return the transition rule that matches or NULL if none matches. */
3430 xg_instruction_matches_rule (insn, rule)
3432 TransitionRule *rule;
3434 PreconditionList *condition_l;
3436 if (rule->opcode != insn->opcode)
3439 for (condition_l = rule->conditions;
3440 condition_l != NULL;
3441 condition_l = condition_l->next)
3445 Precondition *cond = condition_l->precond;
3450 /* The expression must be the constant. */
3451 assert (cond->op_num < insn->ntok);
3452 exp1 = &insn->tok[cond->op_num];
3453 if (!expr_is_const (exp1))
3458 if (get_expr_const (exp1) != cond->op_data)
3462 if (get_expr_const (exp1) == cond->op_data)
3469 assert (cond->op_num < insn->ntok);
3470 assert (cond->op_data < insn->ntok);
3471 exp1 = &insn->tok[cond->op_num];
3472 exp2 = &insn->tok[cond->op_data];
3477 if (!expr_is_equal (exp1, exp2))
3481 if (expr_is_equal (exp1, exp2))
3498 xg_instruction_match (insn)
3501 TransitionTable *table = xg_build_simplify_table ();
3503 assert (insn->opcode < table->num_opcodes);
3505 /* Walk through all of the possible transitions. */
3506 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3508 TransitionRule *rule = l->rule;
3509 if (xg_instruction_matches_rule (insn, rule))
3516 /* Return false if no error. */
3519 xg_build_token_insn (instr_spec, old_insn, new_insn)
3520 BuildInstr *instr_spec;
3527 switch (instr_spec->typ)
3530 new_insn->insn_type = ITYPE_INSN;
3531 new_insn->opcode = instr_spec->opcode;
3532 new_insn->is_specific_opcode = FALSE;
3534 case INSTR_LITERAL_DEF:
3535 new_insn->insn_type = ITYPE_LITERAL;
3536 new_insn->opcode = XTENSA_UNDEFINED;
3537 new_insn->is_specific_opcode = FALSE;
3539 case INSTR_LABEL_DEF:
3540 as_bad (_("INSTR_LABEL_DEF not supported yet"));
3544 for (b_op = instr_spec->ops; b_op != NULL; b_op = b_op->next)
3547 const expressionS *src_exp;
3553 /* The expression must be the constant. */
3554 assert (b_op->op_num < MAX_INSN_ARGS);
3555 exp = &new_insn->tok[b_op->op_num];
3556 set_expr_const (exp, b_op->op_data);
3560 assert (b_op->op_num < MAX_INSN_ARGS);
3561 assert (b_op->op_data < (unsigned) old_insn->ntok);
3562 src_exp = &old_insn->tok[b_op->op_data];
3563 exp = &new_insn->tok[b_op->op_num];
3564 copy_expr (exp, src_exp);
3569 as_bad (_("can't handle generation of literal/labels yet"));
3573 as_bad (_("can't handle undefined OP TYPE"));
3578 new_insn->ntok = num_ops;
3583 /* Return true if it was simplified. */
3586 xg_simplify_insn (old_insn, new_insn)
3590 TransitionRule *rule = xg_instruction_match (old_insn);
3591 BuildInstr *insn_spec;
3595 insn_spec = rule->to_instr;
3596 /* There should only be one. */
3597 assert (insn_spec != NULL);
3598 assert (insn_spec->next == NULL);
3599 if (insn_spec->next != NULL)
3602 xg_build_token_insn (insn_spec, old_insn, new_insn);
3608 /* xg_expand_assembly_insn: (1) Simplify the instruction, i.e., l32i ->
3609 l32i.n. (2) Check the number of operands. (3) Place the instruction
3610 tokens into the stack or if we can relax it at assembly time, place
3611 multiple instructions/literals onto the stack. Return false if no
3615 xg_expand_assembly_insn (istack, orig_insn)
3621 memset (&new_insn, 0, sizeof (TInsn));
3623 /* On return, we will be using the "use_tokens" with "use_ntok".
3624 This will reduce things like addi to addi.n. */
3625 if (code_density_available () && !orig_insn->is_specific_opcode)
3627 if (xg_simplify_insn (orig_insn, &new_insn))
3628 orig_insn = &new_insn;
3631 noperands = xtensa_num_operands (xtensa_default_isa, orig_insn->opcode);
3632 if (orig_insn->ntok < noperands)
3634 as_bad (_("found %d operands for '%s': Expected %d"),
3636 xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
3640 if (orig_insn->ntok > noperands)
3641 as_warn (_("found too many (%d) operands for '%s': Expected %d"),
3643 xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
3646 /* If there are not enough operands, we will assert above. If there
3647 are too many, just cut out the extras here. */
3649 orig_insn->ntok = noperands;
3653 Instructions with all constant immeds:
3654 Assemble them and relax the instruction if possible.
3655 Give error if not possible; no fixup needed.
3657 Instructions with symbolic immeds:
3658 Assemble them with a Fix up (that may cause instruction expansion).
3659 Also close out the fragment if the fixup may cause instruction expansion.
3661 There are some other special cases where we need alignment.
3662 1) before certain instructions with required alignment (OPCODE_ALIGN)
3663 2) before labels that have jumps (LABEL_ALIGN)
3664 3) after call instructions (RETURN_ALIGN)
3665 Multiple of these may be possible on the same fragment.
3666 If so, make sure to satisfy the required alignment.
3667 Then try to get the desired alignment. */
3669 if (tinsn_has_invalid_symbolic_operands (orig_insn))
3672 if (orig_insn->is_specific_opcode || !can_relax ())
3674 istack_push (istack, orig_insn);
3678 if (tinsn_has_symbolic_operands (orig_insn))
3680 if (tinsn_has_complex_operands (orig_insn))
3681 xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0);
3683 istack_push (istack, orig_insn);
3687 if (xg_immeds_fit (orig_insn))
3688 istack_push (istack, orig_insn);
3690 xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0);
3694 for (i = 0; i < istack->ninsn; i++)
3696 if (xg_simplify_insn (&new_insn, &istack->insn[i]))
3697 istack->insn[i] = new_insn;
3705 /* Currently all literals that are generated here are 32-bit L32R targets. */
3708 xg_assemble_literal (insn)
3709 /* const */ TInsn *insn;
3712 symbolS *lit_sym = NULL;
3714 /* size = 4 for L32R. It could easily be larger when we move to
3715 larger constants. Add a parameter later. */
3716 offsetT litsize = 4;
3717 offsetT litalign = 2; /* 2^2 = 4 */
3718 expressionS saved_loc;
3719 set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ());
3721 assert (insn->insn_type == ITYPE_LITERAL);
3722 assert (insn->ntok = 1); /* must be only one token here */
3724 xtensa_switch_to_literal_fragment (&state);
3726 /* Force a 4-byte align here. Note that this opens a new frag, so all
3727 literals done with this function have a frag to themselves. That's
3728 important for the way text section literals work. */
3729 frag_align (litalign, 0, 0);
3731 emit_expr (&insn->tok[0], litsize);
3733 assert (frag_now->tc_frag_data.literal_frag == NULL);
3734 frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
3735 frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
3736 lit_sym = frag_now->fr_symbol;
3737 frag_now->tc_frag_data.is_literal = TRUE;
3740 xtensa_restore_emit_state (&state);
3746 xg_assemble_literal_space (size)
3747 /* const */ int size;
3750 /* We might have to do something about this alignment. It only
3751 takes effect if something is placed here. */
3752 offsetT litalign = 2; /* 2^2 = 4 */
3753 fragS *lit_saved_frag;
3755 expressionS saved_loc;
3757 assert (size % 4 == 0);
3758 set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ());
3760 xtensa_switch_to_literal_fragment (&state);
3762 /* Force a 4-byte align here. */
3763 frag_align (litalign, 0, 0);
3765 xg_force_frag_space (size);
3767 lit_saved_frag = frag_now;
3768 frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
3769 frag_now->tc_frag_data.is_literal = TRUE;
3770 frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
3771 xg_finish_frag (0, RELAX_LITERAL, size, FALSE);
3774 xtensa_restore_emit_state (&state);
3775 frag_now->tc_frag_data.literal_frag = lit_saved_frag;
3780 xtensa_create_literal_symbol (sec, frag)
3784 static int lit_num = 0;
3785 static char name[256];
3788 sprintf (name, ".L_lit_sym%d", lit_num);
3790 /* Create a local symbol. If it is in a linkonce section, we have to
3791 be careful to make sure that if it is used in a relocation that the
3792 symbol will be in the output file. */
3793 if (get_is_linkonce_section (stdoutput, sec))
3795 symbolP = symbol_new (name, sec, 0, frag);
3796 S_CLEAR_EXTERNAL (symbolP);
3797 /* symbolP->local = 1; */
3800 symbolP = symbol_new (name, sec, 0, frag);
3802 xtensa_add_literal_sym (symbolP);
3804 frag->tc_frag_data.is_literal = TRUE;
3811 xtensa_add_literal_sym (sym)
3816 l = (sym_list *) xmalloc (sizeof (sym_list));
3818 l->next = literal_syms;
3824 xtensa_add_insn_label (sym)
3829 if (!free_insn_labels)
3830 l = (sym_list *) xmalloc (sizeof (sym_list));
3833 l = free_insn_labels;
3834 free_insn_labels = l->next;
3838 l->next = insn_labels;
3844 xtensa_clear_insn_labels (void)
3848 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
3855 /* Return true if the section flags are marked linkonce
3856 or the name is .gnu.linkonce*. */
3859 get_is_linkonce_section (abfd, sec)
3860 bfd *abfd ATTRIBUTE_UNUSED;
3863 flagword flags, link_once_flags;
3865 flags = bfd_get_section_flags (abfd, sec);
3866 link_once_flags = (flags & SEC_LINK_ONCE);
3868 /* Flags might not be set yet. */
3869 if (!link_once_flags)
3871 static size_t len = sizeof ".gnu.linkonce.t.";
3873 if (strncmp (segment_name (sec), ".gnu.linkonce.t.", len - 1) == 0)
3874 link_once_flags = SEC_LINK_ONCE;
3876 return (link_once_flags != 0);
3880 /* Emit an instruction to the current fragment. If record_fix is true,
3881 then this instruction will not change and we can go ahead and record
3882 the fixup. If record_fix is false, then the instruction may change
3883 and we are going to close out this fragment. Go ahead and set the
3884 fr_symbol and fr_offset instead of adding a fixup. */
3887 xg_emit_insn (t_insn, record_fix)
3889 bfd_boolean record_fix;
3891 bfd_boolean ok = TRUE;
3892 xtensa_isa isa = xtensa_default_isa;
3893 xtensa_opcode opcode = t_insn->opcode;
3894 bfd_boolean has_fixup = FALSE;
3900 static xtensa_insnbuf insnbuf = NULL;
3902 /* Use a static pointer to the insn buffer so we don't have to call
3903 malloc each time through. */
3905 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
3907 has_fixup = tinsn_to_insnbuf (t_insn, insnbuf);
3909 noperands = xtensa_num_operands (isa, opcode);
3910 assert (noperands == t_insn->ntok);
3912 byte_count = xtensa_insn_length (isa, opcode);
3914 /* This should NEVER cause us to jump into a new frag;
3915 we've already reserved space. */
3916 old_size = frag_now_fix ();
3917 f = frag_more (byte_count);
3918 assert (oldfrag == frag_now);
3920 /* This needs to generate a record that lists the parts that are
3922 if (!frag_now->tc_frag_data.is_insn)
3924 /* If we are at the beginning of a fragment, switch this
3925 fragment to an instruction fragment. */
3926 if (now_seg != absolute_section && old_size != 0)
3927 as_warn (_("instruction fragment may contain data"));
3928 frag_now->tc_frag_data.is_insn = TRUE;
3931 xtensa_insnbuf_to_chars (isa, insnbuf, f);
3933 dwarf2_emit_insn (byte_count);
3935 /* Now spit out the opcode fixup.... */
3939 for (i = 0; i < noperands; ++i)
3941 expressionS *expr = &t_insn->tok[i];
3945 if (get_relaxable_immed (opcode) == i)
3949 if (!xg_add_opcode_fix (opcode, i, expr, frag_now,
3950 f - frag_now->fr_literal))
3955 /* Write it to the fr_offset, fr_symbol. */
3956 frag_now->fr_symbol = expr->X_add_symbol;
3957 frag_now->fr_offset = expr->X_add_number;
3962 as_bad (_("invalid operand %d on '%s'"),
3963 i, xtensa_opcode_name (isa, opcode));
3973 as_bad (_("invalid expression for operand %d on '%s'"),
3974 i, xtensa_opcode_name (isa, opcode));
3985 xg_emit_insn_to_buf (t_insn, buf, fragP, offset, build_fix)
3990 bfd_boolean build_fix;
3992 static xtensa_insnbuf insnbuf = NULL;
3993 bfd_boolean has_symbolic_immed = FALSE;
3994 bfd_boolean ok = TRUE;
3996 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
3998 has_symbolic_immed = tinsn_to_insnbuf (t_insn, insnbuf);
3999 if (has_symbolic_immed && build_fix)
4002 int opnum = get_relaxable_immed (t_insn->opcode);
4003 expressionS *exp = &t_insn->tok[opnum];
4005 if (!xg_add_opcode_fix (t_insn->opcode,
4006 opnum, exp, fragP, offset))
4009 fragP->tc_frag_data.is_insn = TRUE;
4010 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf);
4015 /* Put in a fixup record based on the opcode.
4016 Return true on success. */
4019 xg_add_opcode_fix (opcode, opnum, expr, fragP, offset)
4020 xtensa_opcode opcode;
4026 bfd_reloc_code_real_type reloc;
4027 reloc_howto_type *howto;
4031 reloc = opnum_to_reloc (opnum);
4032 if (reloc == BFD_RELOC_NONE)
4034 as_bad (_("invalid relocation operand %i on '%s'"),
4035 opnum, xtensa_opcode_name (xtensa_default_isa, opcode));
4039 howto = bfd_reloc_type_lookup (stdoutput, reloc);
4043 as_bad (_("undefined symbol for opcode \"%s\"."),
4044 xtensa_opcode_name (xtensa_default_isa, opcode));
4048 insn_length = xtensa_insn_length (xtensa_default_isa, opcode);
4049 the_fix = fix_new_exp (fragP, offset, insn_length, expr,
4050 howto->pc_relative, reloc);
4052 if (expr->X_add_symbol &&
4053 (S_IS_EXTERNAL (expr->X_add_symbol) || S_IS_WEAK (expr->X_add_symbol)))
4054 the_fix->fx_plt = TRUE;
4061 xg_resolve_literals (insn, lit_sym)
4065 symbolS *sym = get_special_literal_symbol ();
4069 assert (insn->insn_type == ITYPE_INSN);
4070 for (i = 0; i < insn->ntok; i++)
4071 if (insn->tok[i].X_add_symbol == sym)
4072 insn->tok[i].X_add_symbol = lit_sym;
4078 xg_resolve_labels (insn, label_sym)
4082 symbolS *sym = get_special_label_symbol ();
4084 /* assert(!insn->is_literal); */
4085 for (i = 0; i < insn->ntok; i++)
4086 if (insn->tok[i].X_add_symbol == sym)
4087 insn->tok[i].X_add_symbol = label_sym;
4093 xg_assemble_tokens (insn)
4094 /*const */ TInsn *insn;
4096 /* By the time we get here, there's not too much left to do.
4097 1) Check our assumptions.
4098 2) Check if the current instruction is "narrow".
4099 If so, then finish the frag, create another one.
4100 We could also go back to change some previous
4101 "narrow" frags into no-change ones if we have more than
4102 MAX_NARROW_ALIGNMENT of them without alignment restrictions
4106 1) It has constant operands and doesn't fit.
4107 Go ahead and assemble it so it will fail.
4108 2) It has constant operands that fit.
4109 If narrow and !is_specific_opcode,
4110 assemble it and put in a relocation
4113 3) It has a symbolic immediate operand
4114 a) Find the worst-case relaxation required
4115 b) Find the worst-case literal pool space required.
4116 Insert appropriate alignment & space in the literal.
4118 Add the relocation. */
4120 assert (insn->insn_type == ITYPE_INSN);
4122 if (!tinsn_has_symbolic_operands (insn))
4124 if (xg_is_narrow_insn (insn) && !insn->is_specific_opcode)
4126 /* assemble it but add max required space */
4127 int max_size = xg_get_max_narrow_insn_size (insn->opcode);
4128 int min_size = xg_get_insn_size (insn);
4130 assert (max_size == 3);
4131 /* make sure we have enough space to widen it */
4132 xg_force_frag_space (max_size);
4133 /* Output the instruction. It may cause an error if some
4134 operands do not fit. */
4135 last_insn = frag_more (0);
4136 if (xg_emit_insn (insn, TRUE))
4137 as_warn (_("instruction with constant operands does not fit"));
4138 xg_finish_frag (last_insn, RELAX_NARROW, max_size - min_size, TRUE);
4142 /* Assemble it. No relocation needed. */
4143 int max_size = xg_get_insn_size (insn);
4144 xg_force_frag_space (max_size);
4145 if (xg_emit_insn (insn, FALSE))
4146 as_warn (_("instruction with constant operands does not "
4147 "fit without widening"));
4148 /* frag_more (max_size); */
4150 /* Special case for jx. If the jx is the next to last
4151 instruction in a loop, we will add a NOP after it. This
4152 avoids a hardware issue that could occur if the jx jumped
4153 to the next instruction. */
4154 if (software_avoid_b_j_loop_end
4155 && is_jx_opcode (insn->opcode))
4157 maybe_has_b_j_loop_end = TRUE;
4158 /* add 2 of these */
4159 frag_now->tc_frag_data.is_insn = TRUE;
4160 frag_var (rs_machine_dependent, 4, 4,
4161 RELAX_ADD_NOP_IF_PRE_LOOP_END,
4162 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4168 /* Need to assemble it with space for the relocation. */
4169 if (!insn->is_specific_opcode)
4171 /* Assemble it but add max required space. */
4173 int min_size = xg_get_insn_size (insn);
4174 int max_size = xg_get_max_insn_widen_size (insn->opcode);
4175 int max_literal_size =
4176 xg_get_max_insn_widen_literal_size (insn->opcode);
4179 symbolS *immed_sym = xg_get_insn_immed_symbol (insn);
4180 set_frag_segment (frag_now, now_seg);
4183 /* Make sure we have enough space to widen the instruction.
4184 This may open a new fragment. */
4185 xg_force_frag_space (max_size);
4186 if (max_literal_size != 0)
4187 xg_assemble_literal_space (max_literal_size);
4189 /* Output the instruction. It may cause an error if some
4190 operands do not fit. Emit the incomplete instruction. */
4191 last_insn = frag_more (0);
4192 xg_emit_insn (insn, FALSE);
4194 xg_finish_frag (last_insn, RELAX_IMMED, max_size - min_size, TRUE);
4196 /* Special cases for loops:
4197 close_loop_end should be inserted AFTER short_loop.
4198 Make sure that CLOSE loops are processed BEFORE short_loops
4199 when converting them. */
4201 /* "short_loop": add a NOP if the loop is < 4 bytes. */
4202 if (software_avoid_short_loop
4203 && is_loop_opcode (insn->opcode))
4205 maybe_has_short_loop = TRUE;
4206 frag_now->tc_frag_data.is_insn = TRUE;
4207 frag_var (rs_machine_dependent, 4, 4,
4208 RELAX_ADD_NOP_IF_SHORT_LOOP,
4209 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4210 frag_now->tc_frag_data.is_insn = TRUE;
4211 frag_var (rs_machine_dependent, 4, 4,
4212 RELAX_ADD_NOP_IF_SHORT_LOOP,
4213 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4216 /* "close_loop_end": Add up to 12 bytes of NOPs to keep a
4217 loop at least 12 bytes away from another loop's loop
4219 if (software_avoid_close_loop_end
4220 && is_loop_opcode (insn->opcode))
4222 maybe_has_close_loop_end = TRUE;
4223 frag_now->tc_frag_data.is_insn = TRUE;
4224 frag_var (rs_machine_dependent, 12, 12,
4225 RELAX_ADD_NOP_IF_CLOSE_LOOP_END,
4226 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4231 /* Assemble it in place. No expansion will be required,
4232 but we'll still need a relocation record. */
4233 int max_size = xg_get_insn_size (insn);
4234 xg_force_frag_space (max_size);
4235 if (xg_emit_insn (insn, TRUE))
4236 as_warn (_("instruction's constant operands do not fit"));
4242 /* Return true if the instruction can write to the specified
4243 integer register. */
4246 is_register_writer (insn, regset, regnum)
4253 xtensa_isa isa = xtensa_default_isa;
4255 num_ops = xtensa_num_operands (isa, insn->opcode);
4257 for (i = 0; i < num_ops; i++)
4259 xtensa_operand operand = xtensa_get_operand (isa, insn->opcode, i);
4260 char inout = xtensa_operand_inout (operand);
4262 if (inout == '>' || inout == '=')
4264 if (strcmp (xtensa_operand_kind (operand), regset) == 0)
4266 if ((insn->tok[i].X_op == O_register)
4267 && (insn->tok[i].X_add_number == regnum))
4277 is_bad_loopend_opcode (tinsn)
4278 const TInsn * tinsn;
4280 xtensa_opcode opcode = tinsn->opcode;
4282 if (opcode == XTENSA_UNDEFINED)
4285 if (opcode == xtensa_call0_opcode
4286 || opcode == xtensa_callx0_opcode
4287 || opcode == xtensa_call4_opcode
4288 || opcode == xtensa_callx4_opcode
4289 || opcode == xtensa_call8_opcode
4290 || opcode == xtensa_callx8_opcode
4291 || opcode == xtensa_call12_opcode
4292 || opcode == xtensa_callx12_opcode
4293 || opcode == xtensa_isync_opcode
4294 || opcode == xtensa_ret_opcode
4295 || opcode == xtensa_ret_n_opcode
4296 || opcode == xtensa_retw_opcode
4297 || opcode == xtensa_retw_n_opcode
4298 || opcode == xtensa_waiti_opcode)
4301 /* An RSR of LCOUNT is illegal as the last opcode in a loop. */
4302 if (opcode == xtensa_rsr_opcode
4304 && tinsn->tok[1].X_op == O_constant
4305 && tinsn->tok[1].X_add_number == 2)
4312 /* Labels that begin with ".Ln" or ".LM" are unaligned.
4313 This allows the debugger to add unaligned labels.
4314 Also, the assembler generates stabs labels that need
4315 not be aligned: FAKE_LABEL_NAME . {"F", "L", "endfunc"}. */
4318 is_unaligned_label (sym)
4321 const char *name = S_GET_NAME (sym);
4322 static size_t fake_size = 0;
4326 && name[1] == 'L' && (name[2] == 'n' || name[2] == 'M'))
4329 /* FAKE_LABEL_NAME followed by "F", "L" or "endfunc" */
4331 fake_size = strlen (FAKE_LABEL_NAME);
4334 && strncmp (FAKE_LABEL_NAME, name, fake_size) == 0
4335 && (name[fake_size] == 'F'
4336 || name[fake_size] == 'L'
4337 || (name[fake_size] == 'e'
4338 && strncmp ("endfunc", name+fake_size, 7) == 0)))
4346 next_non_empty_frag (fragP)
4349 fragS *next_fragP = fragP->fr_next;
4351 /* Sometimes an empty will end up here due storage allocation issues.
4352 So we have to skip until we find something legit. */
4353 while (next_fragP && next_fragP->fr_fix == 0)
4354 next_fragP = next_fragP->fr_next;
4356 if (next_fragP == NULL || next_fragP->fr_fix == 0)
4364 next_frag_opcode (fragP)
4365 const fragS * fragP;
4367 const fragS *next_fragP = next_non_empty_frag (fragP);
4368 static xtensa_insnbuf insnbuf = NULL;
4369 xtensa_isa isa = xtensa_default_isa;
4372 insnbuf = xtensa_insnbuf_alloc (isa);
4374 if (next_fragP == NULL)
4375 return XTENSA_UNDEFINED;
4377 xtensa_insnbuf_from_chars (isa, insnbuf, next_fragP->fr_literal);
4378 return xtensa_decode_insn (isa, insnbuf);
4382 /* Return true if the target frag is one of the next non-empty frags. */
4385 is_next_frag_target (fragP, target)
4387 const fragS *target;
4392 for (; fragP; fragP = fragP->fr_next)
4394 if (fragP == target)
4396 if (fragP->fr_fix != 0)
4398 if (fragP->fr_type == rs_fill && fragP->fr_offset != 0)
4400 if ((fragP->fr_type == rs_align || fragP->fr_type == rs_align_code)
4401 && ((fragP->fr_address % (1 << fragP->fr_offset)) != 0))
4403 if (fragP->fr_type == rs_space)
4410 /* If the next legit fragment is an end-of-loop marker,
4411 switch its state so it will instantiate a NOP. */
4414 update_next_frag_nop_state (fragP)
4417 fragS *next_fragP = fragP->fr_next;
4419 while (next_fragP && next_fragP->fr_fix == 0)
4421 if (next_fragP->fr_type == rs_machine_dependent
4422 && next_fragP->fr_subtype == RELAX_LOOP_END)
4424 next_fragP->fr_subtype = RELAX_LOOP_END_ADD_NOP;
4427 next_fragP = next_fragP->fr_next;
4433 next_frag_is_branch_target (fragP)
4436 /* Sometimes an empty will end up here due storage allocation issues,
4437 so we have to skip until we find something legit. */
4438 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4440 if (fragP->tc_frag_data.is_branch_target)
4442 if (fragP->fr_fix != 0)
4450 next_frag_is_loop_target (fragP)
4453 /* Sometimes an empty will end up here due storage allocation issues.
4454 So we have to skip until we find something legit. */
4455 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4457 if (fragP->tc_frag_data.is_loop_target)
4459 if (fragP->fr_fix != 0)
4467 next_frag_pre_opcode_bytes (fragp)
4470 const fragS *next_fragp = fragp->fr_next;
4472 xtensa_opcode next_opcode = next_frag_opcode (fragp);
4473 if (!is_loop_opcode (next_opcode))
4476 /* Sometimes an empty will end up here due storage allocation issues.
4477 So we have to skip until we find something legit. */
4478 while (next_fragp->fr_fix == 0)
4479 next_fragp = next_fragp->fr_next;
4481 if (next_fragp->fr_type != rs_machine_dependent)
4484 /* There is some implicit knowledge encoded in here.
4485 The LOOP instructions that are NOT RELAX_IMMED have
4487 if (next_fragp->fr_subtype > RELAX_IMMED)
4488 return get_expanded_loop_offset (next_opcode);
4494 /* Mark a location where we can later insert literal frags. Update
4495 the section's literal_pool_loc, so subsequent literals can be
4496 placed nearest to their use. */
4499 xtensa_mark_literal_pool_location ()
4501 /* Any labels pointing to the current location need
4502 to be adjusted to after the literal pool. */
4504 fragS *pool_location;
4506 frag_align (2, 0, 0);
4508 /* We stash info in the fr_var of these frags
4509 so we can later move the literal's fixes into this
4510 frchain's fix list. We can use fr_var because fr_var's
4511 interpretation depends solely on the fr_type and subtype. */
4512 pool_location = frag_now;
4513 frag_variant (rs_machine_dependent, 0, (int) frchain_now,
4514 RELAX_LITERAL_POOL_BEGIN, NULL, 0, NULL);
4515 frag_variant (rs_machine_dependent, 0, (int) now_seg,
4516 RELAX_LITERAL_POOL_END, NULL, 0, NULL);
4518 /* Now put a frag into the literal pool that points to this location. */
4519 set_literal_pool_location (now_seg, pool_location);
4520 xtensa_switch_to_literal_fragment (&s);
4522 /* Close whatever frag is there. */
4523 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4524 frag_now->tc_frag_data.literal_frag = pool_location;
4525 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4526 xtensa_restore_emit_state (&s);
4530 /* The "loops_ok" argument is provided to allow ignoring labels that
4531 define loop ends. This fixes a bug where the NOPs to align a
4532 loop opcode were included in a previous zero-cost loop:
4551 This argument is used to prevent moving the NOP to before the
4552 loop-end label, which is what you want in this special case. */
4555 xtensa_move_labels (new_frag, new_offset, loops_ok)
4558 bfd_boolean loops_ok;
4562 for (lit = insn_labels; lit; lit = lit->next)
4564 symbolS *lit_sym = lit->sym;
4565 if (loops_ok || symbol_get_tc (lit_sym)->is_loop_target == 0)
4567 S_SET_VALUE (lit_sym, new_offset);
4568 symbol_set_frag (lit_sym, new_frag);
4574 /* Assemble a NOP of the requested size in the buffer. User must have
4575 allocated "buf" with at least "size" bytes. */
4578 assemble_nop (size, buf)
4582 static xtensa_insnbuf insnbuf = NULL;
4585 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4587 tinsn_init (&t_insn);
4591 t_insn.opcode = xtensa_nop_n_opcode;
4593 if (t_insn.opcode == XTENSA_UNDEFINED)
4594 as_fatal (_("opcode 'NOP.N' unavailable in this configuration"));
4595 tinsn_to_insnbuf (&t_insn, insnbuf);
4596 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf);
4600 t_insn.opcode = xtensa_or_opcode;
4601 assert (t_insn.opcode != XTENSA_UNDEFINED);
4602 if (t_insn.opcode == XTENSA_UNDEFINED)
4603 as_fatal (_("opcode 'OR' unavailable in this configuration"));
4604 set_expr_const (&t_insn.tok[0], 1);
4605 set_expr_const (&t_insn.tok[1], 1);
4606 set_expr_const (&t_insn.tok[2], 1);
4608 tinsn_to_insnbuf (&t_insn, insnbuf);
4609 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf);
4613 as_fatal (_("invalid %d-byte NOP requested"), size);
4618 /* Return the number of bytes for the offset of the expanded loop
4619 instruction. This should be incorporated into the relaxation
4620 specification but is hard-coded here. This is used to auto-align
4621 the loop instruction. It is invalid to call this function if the
4622 configuration does not have loops or if the opcode is not a loop
4626 get_expanded_loop_offset (opcode)
4627 xtensa_opcode opcode;
4629 /* This is the OFFSET of the loop instruction in the expanded loop.
4630 This MUST correspond directly to the specification of the loop
4631 expansion. It will be validated on fragment conversion. */
4632 if (opcode == XTENSA_UNDEFINED)
4633 as_fatal (_("get_expanded_loop_offset: undefined opcode"));
4634 if (opcode == xtensa_loop_opcode)
4636 if (opcode == xtensa_loopnez_opcode)
4638 if (opcode == xtensa_loopgtz_opcode)
4640 as_fatal (_("get_expanded_loop_offset: invalid opcode"));
4646 get_literal_pool_location (seg)
4649 return seg_info (seg)->tc_segment_info_data.literal_pool_loc;
4654 set_literal_pool_location (seg, literal_pool_loc)
4656 fragS *literal_pool_loc;
4658 seg_info (seg)->tc_segment_info_data.literal_pool_loc = literal_pool_loc;
4662 /* External Functions and Other GAS Hooks. */
4665 xtensa_target_format ()
4667 return (target_big_endian ? "elf32-xtensa-be" : "elf32-xtensa-le");
4672 xtensa_file_arch_init (abfd)
4675 bfd_set_private_flags (abfd, 0x100 | 0x200);
4680 md_number_to_chars (buf, val, n)
4685 if (target_big_endian)
4686 number_to_chars_bigendian (buf, val, n);
4688 number_to_chars_littleendian (buf, val, n);
4692 /* This function is called once, at assembler startup time. It should
4693 set up all the tables, etc. that the MD part of the assembler will
4699 segT current_section = now_seg;
4700 int current_subsec = now_subseg;
4704 isa = xtensa_isa_init ();
4706 /* ISA was already initialized by xtensa_init(). */
4707 isa = xtensa_default_isa;
4710 /* Set up the .literal, .fini.literal and .init.literal sections. */
4711 memset (&default_lit_sections, 0, sizeof (default_lit_sections));
4712 default_lit_sections.init_lit_seg_name = INIT_LITERAL_SECTION_NAME;
4713 default_lit_sections.fini_lit_seg_name = FINI_LITERAL_SECTION_NAME;
4714 default_lit_sections.lit_seg_name = LITERAL_SECTION_NAME;
4716 subseg_set (current_section, current_subsec);
4718 xtensa_addi_opcode = xtensa_opcode_lookup (isa, "addi");
4719 xtensa_addmi_opcode = xtensa_opcode_lookup (isa, "addmi");
4720 xtensa_call0_opcode = xtensa_opcode_lookup (isa, "call0");
4721 xtensa_call4_opcode = xtensa_opcode_lookup (isa, "call4");
4722 xtensa_call8_opcode = xtensa_opcode_lookup (isa, "call8");
4723 xtensa_call12_opcode = xtensa_opcode_lookup (isa, "call12");
4724 xtensa_callx0_opcode = xtensa_opcode_lookup (isa, "callx0");
4725 xtensa_callx4_opcode = xtensa_opcode_lookup (isa, "callx4");
4726 xtensa_callx8_opcode = xtensa_opcode_lookup (isa, "callx8");
4727 xtensa_callx12_opcode = xtensa_opcode_lookup (isa, "callx12");
4728 xtensa_entry_opcode = xtensa_opcode_lookup (isa, "entry");
4729 xtensa_isync_opcode = xtensa_opcode_lookup (isa, "isync");
4730 xtensa_j_opcode = xtensa_opcode_lookup (isa, "j");
4731 xtensa_jx_opcode = xtensa_opcode_lookup (isa, "jx");
4732 xtensa_loop_opcode = xtensa_opcode_lookup (isa, "loop");
4733 xtensa_loopnez_opcode = xtensa_opcode_lookup (isa, "loopnez");
4734 xtensa_loopgtz_opcode = xtensa_opcode_lookup (isa, "loopgtz");
4735 xtensa_nop_n_opcode = xtensa_opcode_lookup (isa, "nop.n");
4736 xtensa_or_opcode = xtensa_opcode_lookup (isa, "or");
4737 xtensa_ret_opcode = xtensa_opcode_lookup (isa, "ret");
4738 xtensa_ret_n_opcode = xtensa_opcode_lookup (isa, "ret.n");
4739 xtensa_retw_opcode = xtensa_opcode_lookup (isa, "retw");
4740 xtensa_retw_n_opcode = xtensa_opcode_lookup (isa, "retw.n");
4741 xtensa_rsr_opcode = xtensa_opcode_lookup (isa, "rsr");
4742 xtensa_waiti_opcode = xtensa_opcode_lookup (isa, "waiti");
4746 /* tc_frob_label hook */
4749 xtensa_frob_label (sym)
4752 if (generating_literals)
4753 xtensa_add_literal_sym (sym);
4755 xtensa_add_insn_label (sym);
4757 if (symbol_get_tc (sym)->is_loop_target
4758 && (get_last_insn_flags (now_seg, now_subseg)
4759 & FLAG_IS_BAD_LOOPEND) != 0)
4760 as_bad (_("invalid last instruction for a zero-overhead loop"));
4762 /* No target aligning in the absolute section. */
4763 if (now_seg != absolute_section
4765 && !is_unaligned_label (sym)
4766 && !frag_now->tc_frag_data.is_literal)
4768 /* frag_now->tc_frag_data.is_insn = TRUE; */
4769 frag_var (rs_machine_dependent, 4, 4,
4770 RELAX_DESIRE_ALIGN_IF_TARGET,
4771 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4772 xtensa_move_labels (frag_now, 0, TRUE);
4774 /* If the label is already known to be a branch target, i.e., a
4775 forward branch, mark the frag accordingly. Backward branches
4776 are handled by xg_add_branch_and_loop_targets. */
4777 if (symbol_get_tc (sym)->is_branch_target)
4778 symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
4780 /* Loops only go forward, so they can be identified here. */
4781 if (symbol_get_tc (sym)->is_loop_target)
4782 symbol_get_frag (sym)->tc_frag_data.is_loop_target = TRUE;
4787 /* md_flush_pending_output hook */
4790 xtensa_flush_pending_output ()
4792 /* If there is a non-zero instruction fragment, close it. */
4793 if (frag_now_fix () != 0 && frag_now->tc_frag_data.is_insn)
4795 frag_wane (frag_now);
4798 frag_now->tc_frag_data.is_insn = FALSE;
4800 xtensa_clear_insn_labels ();
4808 xtensa_isa isa = xtensa_default_isa;
4811 bfd_boolean has_underbar = FALSE;
4812 char *arg_strings[MAX_INSN_ARGS];
4814 IStack istack; /* Put instructions into here. */
4815 TInsn orig_insn; /* Original instruction from the input. */
4817 symbolS *lit_sym = NULL;
4819 if (frag_now->tc_frag_data.is_literal)
4821 static bfd_boolean reported = 0;
4823 as_bad (_("cannot assemble '%s' into a literal fragment"), str);
4830 istack_init (&istack);
4831 tinsn_init (&orig_insn);
4833 /* Split off the opcode. */
4834 opnamelen = strspn (str, "abcdefghijklmnopqrstuvwxyz_/0123456789.");
4835 opname = xmalloc (opnamelen + 1);
4836 memcpy (opname, str, opnamelen);
4837 opname[opnamelen] = '\0';
4839 num_args = tokenize_arguments (arg_strings, str + opnamelen);
4842 as_bad (_("syntax error"));
4846 if (xg_translate_idioms (&opname, &num_args, arg_strings))
4849 /* Check for an underbar prefix. */
4852 has_underbar = TRUE;
4856 orig_insn.insn_type = ITYPE_INSN;
4858 orig_insn.is_specific_opcode = (has_underbar || !use_generics ());
4859 specific_opcode = orig_insn.is_specific_opcode;
4861 orig_insn.opcode = xtensa_opcode_lookup (isa, opname);
4862 if (orig_insn.opcode == XTENSA_UNDEFINED)
4864 as_bad (_("unknown opcode %s"), opname);
4868 if (frag_now_fix () != 0 && !frag_now->tc_frag_data.is_insn)
4870 frag_wane (frag_now);
4874 if (software_a0_b_retw_interlock)
4876 if ((get_last_insn_flags (now_seg, now_subseg) & FLAG_IS_A0_WRITER) != 0
4877 && is_conditional_branch_opcode (orig_insn.opcode))
4879 has_a0_b_retw = TRUE;
4881 /* Mark this fragment with the special RELAX_ADD_NOP_IF_A0_B_RETW.
4882 After the first assembly pass we will check all of them and
4883 add a nop if needed. */
4884 frag_now->tc_frag_data.is_insn = TRUE;
4885 frag_var (rs_machine_dependent, 4, 4,
4886 RELAX_ADD_NOP_IF_A0_B_RETW,
4887 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4888 frag_now->tc_frag_data.is_insn = TRUE;
4889 frag_var (rs_machine_dependent, 4, 4,
4890 RELAX_ADD_NOP_IF_A0_B_RETW,
4891 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4895 /* Special case: The call instructions should be marked "specific opcode"
4896 to keep them from expanding. */
4897 if (!use_longcalls () && is_direct_call_opcode (orig_insn.opcode))
4898 orig_insn.is_specific_opcode = TRUE;
4900 /* Parse the arguments. */
4901 if (parse_arguments (&orig_insn, num_args, arg_strings))
4903 as_bad (_("syntax error"));
4907 /* Free the opcode and argument strings, now that they've been parsed. */
4908 free (has_underbar ? opname - 1 : opname);
4910 while (num_args-- > 0)
4911 free (arg_strings[num_args]);
4913 /* Check for the right number and type of arguments. */
4914 if (tinsn_check_arguments (&orig_insn))
4917 /* See if the instruction implies an aligned section. */
4918 if (is_entry_opcode (orig_insn.opcode) || is_loop_opcode (orig_insn.opcode))
4919 record_alignment (now_seg, 2);
4921 xg_add_branch_and_loop_targets (&orig_insn);
4923 /* Special cases for instructions that force an alignment... */
4924 if (!orig_insn.is_specific_opcode && is_loop_opcode (orig_insn.opcode))
4928 frag_now->tc_frag_data.is_insn = TRUE;
4929 frag_now->tc_frag_data.is_no_density = !code_density_available ();
4930 max_fill = get_text_align_max_fill_size
4931 (get_text_align_power (XTENSA_FETCH_WIDTH),
4932 TRUE, frag_now->tc_frag_data.is_no_density);
4933 frag_var (rs_machine_dependent, max_fill, max_fill,
4934 RELAX_ALIGN_NEXT_OPCODE, frag_now->fr_symbol,
4935 frag_now->fr_offset, NULL);
4937 xtensa_move_labels (frag_now, 0, FALSE);
4940 /* Special-case for "entry" instruction. */
4941 if (is_entry_opcode (orig_insn.opcode))
4943 /* Check that the second opcode (#1) is >= 16. */
4944 if (orig_insn.ntok >= 2)
4946 expressionS *exp = &orig_insn.tok[1];
4950 if (exp->X_add_number < 16)
4951 as_warn (_("entry instruction with stack decrement < 16"));
4955 as_warn (_("entry instruction with non-constant decrement"));
4959 if (!orig_insn.is_specific_opcode)
4961 xtensa_mark_literal_pool_location ();
4963 /* Automatically align ENTRY instructions. */
4964 xtensa_move_labels (frag_now, 0, TRUE);
4965 frag_align (2, 0, 0);
4969 /* Any extra alignment frags have been inserted now, and we're about to
4970 emit a new instruction so clear the list of labels. */
4971 xtensa_clear_insn_labels ();
4973 if (software_a0_b_retw_interlock)
4974 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_A0_WRITER,
4975 is_register_writer (&orig_insn, "a", 0));
4977 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND,
4978 is_bad_loopend_opcode (&orig_insn));
4981 assemble_tokens (opcode, tok, ntok);
4982 expand the tokens from the orig_insn into the
4983 stack of instructions that will not expand
4984 unless required at relaxation time. */
4985 if (xg_expand_assembly_insn (&istack, &orig_insn))
4988 for (i = 0; i < istack.ninsn; i++)
4990 TInsn *insn = &istack.insn[i];
4991 if (insn->insn_type == ITYPE_LITERAL)
4993 assert (lit_sym == NULL);
4994 lit_sym = xg_assemble_literal (insn);
4999 xg_resolve_literals (insn, lit_sym);
5000 xg_assemble_tokens (insn);
5004 /* Now, if the original opcode was a call... */
5005 if (align_targets && is_call_opcode (orig_insn.opcode))
5007 frag_now->tc_frag_data.is_insn = TRUE;
5008 frag_var (rs_machine_dependent, 4, 4,
5010 frag_now->fr_symbol,
5011 frag_now->fr_offset,
5017 /* TC_CONS_FIX_NEW hook: Check for "@PLT" suffix on symbol references.
5018 If found, use an XTENSA_PLT reloc for 4-byte values. Otherwise, this
5019 is the same as the standard code in read.c. */
5022 xtensa_cons_fix_new (frag, where, size, exp)
5028 bfd_reloc_code_real_type r;
5029 bfd_boolean plt = FALSE;
5031 if (*input_line_pointer == '@')
5033 if (!strncmp (input_line_pointer, PLT_SUFFIX, strlen (PLT_SUFFIX) - 1)
5034 && !strncmp (input_line_pointer, plt_suffix,
5035 strlen (plt_suffix) - 1))
5037 as_bad (_("undefined @ suffix '%s', expected '%s'"),
5038 input_line_pointer, plt_suffix);
5039 ignore_rest_of_line ();
5043 input_line_pointer += strlen (plt_suffix);
5056 r = plt ? BFD_RELOC_XTENSA_PLT : BFD_RELOC_32;
5062 as_bad (_("unsupported BFD relocation size %u"), size);
5066 fix_new_exp (frag, where, size, exp, 0, r);
5070 /* TC_FRAG_INIT hook */
5073 xtensa_frag_init (frag)
5076 frag->tc_frag_data.is_no_density = !code_density_available ();
5081 md_undefined_symbol (name)
5082 char *name ATTRIBUTE_UNUSED;
5088 /* Round up a section size to the appropriate boundary. */
5091 md_section_align (segment, size)
5092 segT segment ATTRIBUTE_UNUSED;
5095 return size; /* Byte alignment is fine. */
5100 md_pcrel_from (fixP)
5104 static xtensa_insnbuf insnbuf = NULL;
5106 xtensa_operand operand;
5107 xtensa_opcode opcode;
5108 xtensa_isa isa = xtensa_default_isa;
5109 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
5114 if (fixP->fx_r_type == BFD_RELOC_XTENSA_ASM_EXPAND)
5118 insnbuf = xtensa_insnbuf_alloc (isa);
5120 insn_p = &fixP->fx_frag->fr_literal[fixP->fx_where];
5121 xtensa_insnbuf_from_chars (isa, insnbuf, insn_p);
5122 opcode = xtensa_decode_insn (isa, insnbuf);
5124 opnum = reloc_to_opnum (fixP->fx_r_type);
5127 as_fatal (_("invalid operand relocation for '%s' instruction"),
5128 xtensa_opcode_name (isa, opcode));
5129 if (opnum >= xtensa_num_operands (isa, opcode))
5130 as_fatal (_("invalid relocation for operand %d in '%s' instruction"),
5131 opnum, xtensa_opcode_name (isa, opcode));
5132 operand = xtensa_get_operand (isa, opcode, opnum);
5135 as_warn_where (fixP->fx_file,
5137 _("invalid relocation type %d for %s instruction"),
5138 fixP->fx_r_type, xtensa_opcode_name (isa, opcode));
5142 if (!operand_is_pcrel_label (operand))
5144 as_bad_where (fixP->fx_file,
5146 _("invalid relocation for operand %d of '%s'"),
5147 opnum, xtensa_opcode_name (isa, opcode));
5150 if (!xtensa_operand_isPCRelative (operand))
5152 as_warn_where (fixP->fx_file,
5154 _("non-PCREL relocation operand %d for '%s': %s"),
5155 opnum, xtensa_opcode_name (isa, opcode),
5156 bfd_get_reloc_code_name (fixP->fx_r_type));
5160 return 0 - xtensa_operand_do_reloc (operand, 0, addr);
5164 /* tc_symbol_new_hook */
5167 xtensa_symbol_new_hook (symbolP)
5170 symbol_get_tc (symbolP)->plt = 0;
5174 /* tc_fix_adjustable hook */
5177 xtensa_fix_adjustable (fixP)
5180 /* We need the symbol name for the VTABLE entries. */
5181 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
5182 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
5190 md_apply_fix3 (fixP, valP, seg)
5193 segT seg ATTRIBUTE_UNUSED;
5195 if (fixP->fx_pcrel == 0 && fixP->fx_addsy == 0)
5197 /* This happens when the relocation is within the current section.
5198 It seems this implies a PCREL operation. We'll catch it and error
5201 char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
5202 static xtensa_insnbuf insnbuf = NULL;
5203 xtensa_opcode opcode;
5206 switch (fixP->fx_r_type)
5208 case BFD_RELOC_XTENSA_ASM_EXPAND:
5212 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
5213 as_bad (_("unhandled local relocation fix %s"),
5214 bfd_get_reloc_code_name (fixP->fx_r_type));
5220 /* The only one we support that isn't an instruction field. */
5221 md_number_to_chars (fixpos, *valP, fixP->fx_size);
5225 case BFD_RELOC_XTENSA_OP0:
5226 case BFD_RELOC_XTENSA_OP1:
5227 case BFD_RELOC_XTENSA_OP2:
5228 isa = xtensa_default_isa;
5230 insnbuf = xtensa_insnbuf_alloc (isa);
5232 xtensa_insnbuf_from_chars (isa, insnbuf, fixpos);
5233 opcode = xtensa_decode_insn (isa, insnbuf);
5234 if (opcode == XTENSA_UNDEFINED)
5235 as_fatal (_("undecodable FIX"));
5237 xtensa_insnbuf_set_immediate_field (opcode, insnbuf, *valP,
5238 fixP->fx_file, fixP->fx_line);
5240 fixP->fx_frag->tc_frag_data.is_insn = TRUE;
5241 xtensa_insnbuf_to_chars (isa, insnbuf, fixpos);
5245 case BFD_RELOC_VTABLE_INHERIT:
5246 case BFD_RELOC_VTABLE_ENTRY:
5251 as_bad (_("unhandled local relocation fix %s"),
5252 bfd_get_reloc_code_name (fixP->fx_r_type));
5259 md_atof (type, litP, sizeP)
5265 LITTLENUM_TYPE words[4];
5281 return "bad call to md_atof";
5284 t = atof_ieee (input_line_pointer, type, words);
5286 input_line_pointer = t;
5290 for (i = prec - 1; i >= 0; i--)
5293 if (target_big_endian)
5294 idx = (prec - 1 - i);
5296 md_number_to_chars (litP, (valueT) words[idx], 2);
5305 md_estimate_size_before_relax (fragP, seg)
5307 segT seg ATTRIBUTE_UNUSED;
5309 return fragP->tc_frag_data.text_expansion;
5313 /* Translate internal representation of relocation info to BFD target
5317 tc_gen_reloc (section, fixp)
5318 asection *section ATTRIBUTE_UNUSED;
5323 reloc = (arelent *) xmalloc (sizeof (arelent));
5324 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5325 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
5326 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
5328 /* Make sure none of our internal relocations make it this far.
5329 They'd better have been fully resolved by this point. */
5330 assert ((int) fixp->fx_r_type > 0);
5332 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
5333 if (reloc->howto == NULL)
5335 as_bad_where (fixp->fx_file, fixp->fx_line,
5336 _("cannot represent `%s' relocation in object file"),
5337 bfd_get_reloc_code_name (fixp->fx_r_type));
5341 if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
5343 as_fatal (_("internal error? cannot generate `%s' relocation"),
5344 bfd_get_reloc_code_name (fixp->fx_r_type));
5346 assert (!fixp->fx_pcrel == !reloc->howto->pc_relative);
5348 reloc->addend = fixp->fx_offset;
5350 switch (fixp->fx_r_type)
5352 case BFD_RELOC_XTENSA_OP0:
5353 case BFD_RELOC_XTENSA_OP1:
5354 case BFD_RELOC_XTENSA_OP2:
5355 case BFD_RELOC_XTENSA_ASM_EXPAND:
5357 case BFD_RELOC_XTENSA_PLT:
5358 case BFD_RELOC_VTABLE_INHERIT:
5359 case BFD_RELOC_VTABLE_ENTRY:
5362 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
5363 as_warn (_("emitting simplification relocation"));
5367 as_warn (_("emitting unknown relocation"));
5377 directive_balance ();
5378 xtensa_move_literals ();
5380 xtensa_reorder_segments ();
5381 xtensa_cleanup_align_frags ();
5382 xtensa_fix_target_frags ();
5383 if (software_a0_b_retw_interlock && has_a0_b_retw)
5384 xtensa_fix_a0_b_retw_frags ();
5385 if (software_avoid_b_j_loop_end && maybe_has_b_j_loop_end)
5386 xtensa_fix_b_j_loop_end_frags ();
5388 /* "close_loop_end" should be processed BEFORE "short_loop". */
5389 if (software_avoid_close_loop_end && maybe_has_close_loop_end)
5390 xtensa_fix_close_loop_end_frags ();
5392 if (software_avoid_short_loop && maybe_has_short_loop)
5393 xtensa_fix_short_loop_frags ();
5395 xtensa_sanity_check ();
5400 xtensa_cleanup_align_frags ()
5404 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5408 /* Walk over all of the fragments in a subsection. */
5409 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5411 if ((fragP->fr_type == rs_align
5412 || fragP->fr_type == rs_align_code
5413 || (fragP->fr_type == rs_machine_dependent
5414 && (fragP->fr_subtype == RELAX_DESIRE_ALIGN
5415 || fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)))
5416 && fragP->fr_fix == 0)
5418 fragS * next = fragP->fr_next;
5421 && next->fr_type == rs_machine_dependent
5422 && next->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
5425 next = next->fr_next;
5433 /* Re-process all of the fragments looking to convert all of the
5434 RELAX_DESIRE_ALIGN_IF_TARGET fragments. If there is a branch
5435 target in the next fragment, convert this to RELAX_DESIRE_ALIGN.
5436 If the next fragment starts with a loop target, AND the previous
5437 fragment can be expanded to negate the branch, convert this to a
5438 RELAX_LOOP_END. Otherwise, convert to a .fill 0. */
5441 xtensa_fix_target_frags ()
5445 /* When this routine is called, all of the subsections are still intact
5446 so we walk over subsections instead of sections. */
5447 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5449 bfd_boolean prev_frag_can_negate_branch = FALSE;
5452 /* Walk over all of the fragments in a subsection. */
5453 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5455 if (fragP->fr_type == rs_machine_dependent
5456 && fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
5458 if (next_frag_is_loop_target (fragP))
5460 if (prev_frag_can_negate_branch)
5461 fragP->fr_subtype = RELAX_LOOP_END;
5464 if (!align_only_targets ||
5465 next_frag_is_branch_target (fragP))
5466 fragP->fr_subtype = RELAX_DESIRE_ALIGN;
5471 else if (!align_only_targets
5472 || next_frag_is_branch_target (fragP))
5473 fragP->fr_subtype = RELAX_DESIRE_ALIGN;
5477 if (fragP->fr_fix != 0)
5478 prev_frag_can_negate_branch = FALSE;
5479 if (frag_can_negate_branch (fragP))
5480 prev_frag_can_negate_branch = TRUE;
5487 frag_can_negate_branch (fragP)
5490 if (fragP->fr_type == rs_machine_dependent
5491 && fragP->fr_subtype == RELAX_IMMED)
5494 tinsn_from_chars (&t_insn, fragP->fr_opcode);
5495 if (is_negatable_branch (&t_insn))
5502 /* Re-process all of the fragments looking to convert all of the
5503 RELAX_ADD_NOP_IF_A0_B_RETW. If the next instruction is a
5504 conditional branch or a retw/retw.n, convert this frag to one that
5505 will generate a NOP. In any case close it off with a .fill 0. */
5508 xtensa_fix_a0_b_retw_frags ()
5512 /* When this routine is called, all of the subsections are still intact
5513 so we walk over subsections instead of sections. */
5514 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5518 /* Walk over all of the fragments in a subsection. */
5519 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5521 if (fragP->fr_type == rs_machine_dependent
5522 && fragP->fr_subtype == RELAX_ADD_NOP_IF_A0_B_RETW)
5524 if (next_instrs_are_b_retw (fragP))
5525 relax_frag_add_nop (fragP);
5535 next_instrs_are_b_retw (fragP)
5538 xtensa_opcode opcode;
5539 const fragS *next_fragP = next_non_empty_frag (fragP);
5540 static xtensa_insnbuf insnbuf = NULL;
5541 xtensa_isa isa = xtensa_default_isa;
5545 insnbuf = xtensa_insnbuf_alloc (isa);
5547 if (next_fragP == NULL)
5550 /* Check for the conditional branch. */
5551 xtensa_insnbuf_from_chars (isa, insnbuf, &next_fragP->fr_literal[offset]);
5552 opcode = xtensa_decode_insn (isa, insnbuf);
5554 if (!is_conditional_branch_opcode (opcode))
5557 offset += xtensa_insn_length (isa, opcode);
5558 if (offset == next_fragP->fr_fix)
5560 next_fragP = next_non_empty_frag (next_fragP);
5563 if (next_fragP == NULL)
5566 /* Check for the retw/retw.n. */
5567 xtensa_insnbuf_from_chars (isa, insnbuf, &next_fragP->fr_literal[offset]);
5568 opcode = xtensa_decode_insn (isa, insnbuf);
5570 if (is_windowed_return_opcode (opcode))
5576 /* Re-process all of the fragments looking to convert all of the
5577 RELAX_ADD_NOP_IF_PRE_LOOP_END. If there is one instruction and a
5578 loop end label, convert this frag to one that will generate a NOP.
5579 In any case close it off with a .fill 0. */
5582 xtensa_fix_b_j_loop_end_frags ()
5586 /* When this routine is called, all of the subsections are still intact
5587 so we walk over subsections instead of sections. */
5588 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5592 /* Walk over all of the fragments in a subsection. */
5593 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5595 if (fragP->fr_type == rs_machine_dependent
5596 && fragP->fr_subtype == RELAX_ADD_NOP_IF_PRE_LOOP_END)
5598 if (next_instr_is_loop_end (fragP))
5599 relax_frag_add_nop (fragP);
5609 next_instr_is_loop_end (fragP)
5612 const fragS *next_fragP;
5614 if (next_frag_is_loop_target (fragP))
5617 next_fragP = next_non_empty_frag (fragP);
5618 if (next_fragP == NULL)
5621 if (!next_frag_is_loop_target (next_fragP))
5624 /* If the size is >= 3 then there is more than one instruction here.
5625 The hardware bug will not fire. */
5626 if (next_fragP->fr_fix > 3)
5633 /* Re-process all of the fragments looking to convert all of the
5634 RELAX_ADD_NOP_IF_CLOSE_LOOP_END. If there is an loop end that is
5635 not MY loop's loop end within 12 bytes, add enough nops here to
5636 make it at least 12 bytes away. In any case close it off with a
5640 xtensa_fix_close_loop_end_frags ()
5644 /* When this routine is called, all of the subsections are still intact
5645 so we walk over subsections instead of sections. */
5646 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5650 fragS *current_target = NULL;
5651 offsetT current_offset = 0;
5653 /* Walk over all of the fragments in a subsection. */
5654 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5656 if (fragP->fr_type == rs_machine_dependent
5657 && fragP->fr_subtype == RELAX_IMMED)
5659 /* Read it. If the instruction is a loop, get the target. */
5660 xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_opcode);
5661 if (is_loop_opcode (opcode))
5665 tinsn_from_chars (&t_insn, fragP->fr_opcode);
5666 tinsn_immed_from_frag (&t_insn, fragP);
5668 /* Get the current fragment target. */
5669 if (fragP->fr_symbol)
5671 current_target = symbol_get_frag (fragP->fr_symbol);
5672 current_offset = fragP->fr_offset;
5678 && fragP->fr_type == rs_machine_dependent
5679 && fragP->fr_subtype == RELAX_ADD_NOP_IF_CLOSE_LOOP_END)
5682 size_t bytes_added = 0;
5684 #define REQUIRED_LOOP_DIVIDING_BYTES 12
5685 /* Max out at 12. */
5686 min_bytes = min_bytes_to_other_loop_end
5687 (fragP->fr_next, current_target, current_offset,
5688 REQUIRED_LOOP_DIVIDING_BYTES);
5690 if (min_bytes < REQUIRED_LOOP_DIVIDING_BYTES)
5692 while (min_bytes + bytes_added
5693 < REQUIRED_LOOP_DIVIDING_BYTES)
5697 if (fragP->fr_var < length)
5698 as_warn (_("fr_var %lu < length %d; ignoring"),
5699 fragP->fr_var, length);
5702 assemble_nop (length,
5703 fragP->fr_literal + fragP->fr_fix);
5704 fragP->fr_fix += length;
5705 fragP->fr_var -= length;
5707 bytes_added += length;
5718 min_bytes_to_other_loop_end (fragP, current_target, current_offset, max_size)
5720 fragS *current_target;
5721 offsetT current_offset;
5725 fragS *current_fragP;
5727 for (current_fragP = fragP;
5729 current_fragP = current_fragP->fr_next)
5731 if (current_fragP->tc_frag_data.is_loop_target
5732 && current_fragP != current_target)
5733 return offset + current_offset;
5735 offset += unrelaxed_frag_min_size (current_fragP);
5737 if (offset + current_offset >= max_size)
5745 unrelaxed_frag_min_size (fragP)
5748 size_t size = fragP->fr_fix;
5751 if (fragP->fr_type == rs_fill)
5752 size += fragP->fr_offset;
5758 /* Re-process all of the fragments looking to convert all
5759 of the RELAX_ADD_NOP_IF_SHORT_LOOP. If:
5762 1) the instruction size count to the loop end label
5763 is too short (<= 2 instructions),
5764 2) loop has a jump or branch in it
5767 1) software_avoid_all_short_loops is true
5768 2) The generating loop was a 'loopgtz' or 'loopnez'
5769 3) the instruction size count to the loop end label is too short
5771 then convert this frag (and maybe the next one) to generate a NOP.
5772 In any case close it off with a .fill 0. */
5775 xtensa_fix_short_loop_frags ()
5779 /* When this routine is called, all of the subsections are still intact
5780 so we walk over subsections instead of sections. */
5781 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5784 fragS *current_target = NULL;
5785 offsetT current_offset = 0;
5786 xtensa_opcode current_opcode = XTENSA_UNDEFINED;
5788 /* Walk over all of the fragments in a subsection. */
5789 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5791 /* check on the current loop */
5792 if (fragP->fr_type == rs_machine_dependent
5793 && fragP->fr_subtype == RELAX_IMMED)
5795 /* Read it. If the instruction is a loop, get the target. */
5796 xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_opcode);
5797 if (is_loop_opcode (opcode))
5801 tinsn_from_chars (&t_insn, fragP->fr_opcode);
5802 tinsn_immed_from_frag (&t_insn, fragP);
5804 /* Get the current fragment target. */
5805 if (fragP->fr_symbol)
5807 current_target = symbol_get_frag (fragP->fr_symbol);
5808 current_offset = fragP->fr_offset;
5809 current_opcode = opcode;
5814 if (fragP->fr_type == rs_machine_dependent
5815 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
5818 count_insns_to_loop_end (fragP->fr_next, TRUE, 3);
5820 && (branch_before_loop_end (fragP->fr_next)
5821 || (software_avoid_all_short_loops
5822 && current_opcode != XTENSA_UNDEFINED
5823 && !is_the_loop_opcode (current_opcode))))
5824 relax_frag_add_nop (fragP);
5834 count_insns_to_loop_end (base_fragP, count_relax_add, max_count)
5836 bfd_boolean count_relax_add;
5839 fragS *fragP = NULL;
5840 size_t insn_count = 0;
5844 for (; fragP && !fragP->tc_frag_data.is_loop_target; fragP = fragP->fr_next)
5846 insn_count += unrelaxed_frag_min_insn_count (fragP);
5847 if (insn_count >= max_count)
5850 if (count_relax_add)
5852 if (fragP->fr_type == rs_machine_dependent
5853 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
5855 /* In order to add the appropriate number of
5856 NOPs, we count an instruction for downstream
5859 if (insn_count >= max_count)
5869 unrelaxed_frag_min_insn_count (fragP)
5872 size_t insn_count = 0;
5875 if (!fragP->tc_frag_data.is_insn)
5878 /* Decode the fixed instructions. */
5879 while (offset < fragP->fr_fix)
5881 xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_literal + offset);
5882 if (opcode == XTENSA_UNDEFINED)
5884 as_fatal (_("undecodable instruction in instruction frag"));
5887 offset += xtensa_insn_length (xtensa_default_isa, opcode);
5896 branch_before_loop_end (base_fragP)
5901 for (fragP = base_fragP;
5902 fragP && !fragP->tc_frag_data.is_loop_target;
5903 fragP = fragP->fr_next)
5905 if (unrelaxed_frag_has_b_j (fragP))
5913 unrelaxed_frag_has_b_j (fragP)
5916 size_t insn_count = 0;
5919 if (!fragP->tc_frag_data.is_insn)
5922 /* Decode the fixed instructions. */
5923 while (offset < fragP->fr_fix)
5925 xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_literal + offset);
5926 if (opcode == XTENSA_UNDEFINED)
5928 as_fatal (_("undecodable instruction in instruction frag"));
5931 if (is_branch_or_jump_opcode (opcode))
5933 offset += xtensa_insn_length (xtensa_default_isa, opcode);
5939 /* Checks to be made after initial assembly but before relaxation. */
5942 xtensa_sanity_check ()
5949 as_where (&file_name, &line);
5950 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5954 /* Walk over all of the fragments in a subsection. */
5955 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5957 /* Currently we only check for empty loops here. */
5958 if (fragP->fr_type == rs_machine_dependent
5959 && fragP->fr_subtype == RELAX_IMMED)
5961 static xtensa_insnbuf insnbuf = NULL;
5964 if (fragP->fr_opcode != NULL)
5967 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
5968 tinsn_from_chars (&t_insn, fragP->fr_opcode);
5969 tinsn_immed_from_frag (&t_insn, fragP);
5971 if (is_loop_opcode (t_insn.opcode))
5973 if (is_empty_loop (&t_insn, fragP))
5975 new_logical_line (fragP->fr_file, fragP->fr_line);
5976 as_bad (_("invalid empty loop"));
5978 if (!is_local_forward_loop (&t_insn, fragP))
5980 new_logical_line (fragP->fr_file, fragP->fr_line);
5981 as_bad (_("loop target does not follow "
5982 "loop instruction in section"));
5989 new_logical_line (file_name, line);
5993 #define LOOP_IMMED_OPN 1
5995 /* Return true if the loop target is the next non-zero fragment. */
5998 is_empty_loop (insn, fragP)
6002 const expressionS *expr;
6006 if (insn->insn_type != ITYPE_INSN)
6009 if (!is_loop_opcode (insn->opcode))
6012 if (insn->ntok <= LOOP_IMMED_OPN)
6015 expr = &insn->tok[LOOP_IMMED_OPN];
6017 if (expr->X_op != O_symbol)
6020 symbolP = expr->X_add_symbol;
6024 if (symbol_get_frag (symbolP) == NULL)
6027 if (S_GET_VALUE (symbolP) != 0)
6030 /* Walk through the zero-size fragments from this one. If we find
6031 the target fragment, then this is a zero-size loop. */
6032 for (next_fragP = fragP->fr_next;
6034 next_fragP = next_fragP->fr_next)
6036 if (next_fragP == symbol_get_frag (symbolP))
6038 if (next_fragP->fr_fix != 0)
6046 is_local_forward_loop (insn, fragP)
6050 const expressionS *expr;
6054 if (insn->insn_type != ITYPE_INSN)
6057 if (!is_loop_opcode (insn->opcode))
6060 if (insn->ntok <= LOOP_IMMED_OPN)
6063 expr = &insn->tok[LOOP_IMMED_OPN];
6065 if (expr->X_op != O_symbol)
6068 symbolP = expr->X_add_symbol;
6072 if (symbol_get_frag (symbolP) == NULL)
6075 /* Walk through fragments until we find the target.
6076 If we do not find the target, then this is an invalid loop. */
6077 for (next_fragP = fragP->fr_next;
6079 next_fragP = next_fragP->fr_next)
6080 if (next_fragP == symbol_get_frag (symbolP))
6087 /* Alignment Functions. */
6090 get_text_align_power (target_size)
6094 for (i = 0; i < sizeof (size_t); i++)
6096 if (target_size <= (1 << i))
6099 as_fatal (_("get_text_align_power: argument too large"));
6105 get_text_align_max_fill_size (align_pow, use_nops, use_no_density)
6107 bfd_boolean use_nops;
6108 bfd_boolean use_no_density;
6111 return (1 << align_pow);
6113 return 3 * (1 << align_pow);
6115 return 1 + (1 << align_pow);
6119 /* get_text_align_fill_size ()
6123 target_size = size of next instruction
6124 align_pow = get_text_align_power (target_size).
6128 address = current address + loop instruction size;
6129 target_size = 3 (for 2 or 3 byte target)
6130 = 8 (for 8 byte target)
6131 align_pow = get_text_align_power (target_size);
6133 use_no_density = set appropriately
6135 address = current address + loop instruction size;
6137 align_pow = get_text_align_power (target_size);
6139 use_no_density = 0. */
6142 get_text_align_fill_size (address, align_pow, target_size,
6143 use_nops, use_no_density)
6147 bfd_boolean use_nops;
6148 bfd_boolean use_no_density;
6152 align_pow: log2 (required alignment).
6154 target_size: alignment must allow the new_address and
6155 new_address+target_size-1.
6157 use_nops: if true, then we can only use 2 or 3 byte nops.
6159 use_no_density: if use_nops and use_no_density, we can only use
6162 Usually, for non-zero target_size, the align_pow is the power of 2
6163 that is greater than or equal to the target_size. This handles the
6164 2-byte, 3-byte and 8-byte instructions. */
6166 size_t alignment = (1 << align_pow);
6169 /* This is the easy case. */
6171 mod = address % alignment;
6173 mod = alignment - mod;
6174 assert ((address + mod) % alignment == 0);
6178 /* This is the slightly harder case. */
6179 assert ((int) alignment >= target_size);
6180 assert (target_size > 0);
6181 if (!use_no_density)
6184 for (i = 0; i < alignment * 2; i++)
6188 if ((address + i) >> align_pow ==
6189 (address + i + target_size - 1) >> align_pow)
6197 /* Can only fill multiples of 3. */
6198 for (i = 0; i <= alignment * 3; i += 3)
6200 if ((address + i) >> align_pow ==
6201 (address + i + target_size - 1) >> align_pow)
6210 /* This will assert if it is not possible. */
6213 get_text_align_nop_count (fill_size, use_no_density)
6215 bfd_boolean use_no_density;
6220 assert (fill_size % 3 == 0);
6221 return (fill_size / 3);
6224 assert (fill_size != 1); /* Bad argument. */
6226 while (fill_size > 1)
6228 size_t insn_size = 3;
6229 if (fill_size == 2 || fill_size == 4)
6231 fill_size -= insn_size;
6234 assert (fill_size != 1); /* Bad algorithm. */
6240 get_text_align_nth_nop_size (fill_size, n, use_no_density)
6243 bfd_boolean use_no_density;
6247 assert (get_text_align_nop_count (fill_size, use_no_density) > n);
6252 while (fill_size > 1)
6254 size_t insn_size = 3;
6255 if (fill_size == 2 || fill_size == 4)
6257 fill_size -= insn_size;
6267 /* For the given fragment, find the appropriate address
6268 for it to begin at if we are using NOPs to align it. */
6271 get_noop_aligned_address (fragP, address)
6275 static xtensa_insnbuf insnbuf = NULL;
6276 size_t fill_size = 0;
6279 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
6281 switch (fragP->fr_type)
6283 case rs_machine_dependent:
6284 if (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
6286 /* The rule is: get next fragment's FIRST instruction. Find
6287 the smallest number of bytes that need to be added to
6288 ensure that the next fragment's FIRST instruction will fit
6291 E.G., 2 bytes : 0, 1, 2 mod 4
6294 If the FIRST instruction MIGHT be relaxed,
6295 assume that it will become a 3 byte instruction. */
6297 int target_insn_size;
6298 xtensa_opcode opcode = next_frag_opcode (fragP);
6299 addressT pre_opcode_bytes;
6301 if (opcode == XTENSA_UNDEFINED)
6303 as_bad_where (fragP->fr_file, fragP->fr_line,
6304 _("invalid opcode for RELAX_ALIGN_NEXT_OPCODE"));
6305 as_fatal (_("cannot continue"));
6308 target_insn_size = xtensa_insn_length (xtensa_default_isa, opcode);
6310 pre_opcode_bytes = next_frag_pre_opcode_bytes (fragP);
6312 if (is_loop_opcode (opcode))
6314 /* next_fragP should be the loop. */
6315 const fragS *next_fragP = next_non_empty_frag (fragP);
6316 xtensa_opcode next_opcode = next_frag_opcode (next_fragP);
6319 pre_opcode_bytes += target_insn_size;
6321 /* For loops, the alignment depends on the size of the
6322 instruction following the loop, not the loop instruction. */
6323 if (next_opcode == XTENSA_UNDEFINED)
6324 target_insn_size = 3;
6328 xtensa_insn_length (xtensa_default_isa, next_opcode);
6330 if (target_insn_size == 2)
6331 target_insn_size = 3; /* ISA specifies this. */
6334 /* If it was 8, then we'll need a larger alignment
6336 alignment = get_text_align_power (target_insn_size);
6338 /* Is Now_seg valid */
6339 record_alignment (now_seg, alignment);
6342 as_fatal (_("expected loop opcode in relax align next target"));
6344 fill_size = get_text_align_fill_size
6345 (address + pre_opcode_bytes,
6346 get_text_align_power (target_insn_size),
6347 target_insn_size, TRUE, fragP->tc_frag_data.is_no_density);
6353 fill_size = get_text_align_fill_size
6354 (address, fragP->fr_offset, 1, TRUE,
6355 fragP->tc_frag_data.is_no_density);
6359 as_fatal (_("expected align_code or RELAX_ALIGN_NEXT_OPCODE"));
6362 return address + fill_size;
6366 /* 3 mechanisms for relaxing an alignment:
6368 Align to a power of 2.
6369 Align so the next fragment's instruction does not cross a word boundary.
6370 Align the current instruction so that if the next instruction
6371 were 3 bytes, it would not cross a word boundary.
6375 zeros - This is easy; always insert zeros.
6376 nops - 3 and 2 byte instructions
6380 >=5 : 3 byte instruction + fn(n-3)
6381 widening - widen previous instructions. */
6384 get_widen_aligned_address (fragP, address)
6388 addressT align_pow, new_address, loop_insn_offset;
6391 xtensa_opcode opcode, next_opcode;
6392 static xtensa_insnbuf insnbuf = NULL;
6395 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
6397 if (fragP->fr_type == rs_align || fragP->fr_type == rs_align_code)
6399 align_pow = fragP->fr_offset;
6400 new_address = ((address + ((1 << align_pow) - 1))
6401 << align_pow) >> align_pow;
6405 if (fragP->fr_type == rs_machine_dependent)
6407 switch (fragP->fr_subtype)
6409 case RELAX_DESIRE_ALIGN:
6411 /* The rule is: get the next fragment's FIRST instruction.
6412 Find the smallest number of bytes needed to be added
6413 in order to ensure that the next fragment is FIRST
6414 instruction will fit in a single word.
6415 i.e. 2 bytes : 0, 1, 2. mod 4
6417 If the FIRST instruction MIGHT be relaxed,
6418 assume that it will become a 3-byte instruction. */
6421 /* Check to see if it might be 2 bytes. */
6422 next_opcode = next_frag_opcode (fragP);
6423 if (next_opcode != XTENSA_UNDEFINED
6424 && xtensa_insn_length (xtensa_default_isa, next_opcode) == 2)
6427 assert (insn_size <= 4);
6428 for (new_address = address; new_address < address + 4; new_address++)
6430 if (new_address >> 2 == (new_address + insn_size - 1) >> 2)
6433 as_bad (_("internal error aligning"));
6436 case RELAX_ALIGN_NEXT_OPCODE:
6437 /* The rule is: get next fragment's FIRST instruction.
6438 Find the smallest number of bytes needed to be added
6439 in order to ensure that the next fragment's FIRST
6440 instruction will fit in a single word.
6441 i.e. 2 bytes : 0, 1, 2. mod 4
6443 If the FIRST instruction MIGHT be relaxed,
6444 assume that it will become a 3 byte instruction. */
6446 opcode = next_frag_opcode (fragP);
6447 if (opcode == XTENSA_UNDEFINED)
6449 as_bad_where (fragP->fr_file, fragP->fr_line,
6450 _("invalid opcode for RELAX_ALIGN_NEXT_OPCODE"));
6451 as_fatal (_("cannot continue"));
6453 insn_size = xtensa_insn_length (xtensa_default_isa, opcode);
6454 assert (insn_size <= 4);
6455 assert (is_loop_opcode (opcode));
6457 loop_insn_offset = 0;
6458 next_frag = next_non_empty_frag (fragP);
6460 /* If the loop has been expanded then the loop
6461 instruction could be at an offset from this fragment. */
6462 if (next_frag->fr_subtype != RELAX_IMMED)
6463 loop_insn_offset = get_expanded_loop_offset (opcode);
6465 for (new_address = address; new_address < address + 4; new_address++)
6467 if ((new_address + loop_insn_offset + insn_size) >> 2 ==
6468 (new_address + loop_insn_offset + insn_size + 2) >> 2)
6471 as_bad (_("internal error aligning"));
6475 as_bad (_("internal error aligning"));
6479 as_bad (_("internal error aligning"));
6484 /* md_relax_frag Hook and Helper Functions. */
6486 /* Return the number of bytes added to this fragment, given that the
6487 input has been stretched already by "stretch". */
6490 xtensa_relax_frag (fragP, stretch, stretched_p)
6495 int unreported = fragP->tc_frag_data.unreported_expansion;
6496 long new_stretch = 0;
6500 as_where (&file_name, &line);
6501 new_logical_line (fragP->fr_file, fragP->fr_line);
6503 fragP->tc_frag_data.unreported_expansion = 0;
6505 switch (fragP->fr_subtype)
6507 case RELAX_ALIGN_NEXT_OPCODE:
6508 /* Always convert. */
6509 new_stretch = relax_frag_text_align (fragP, stretch);
6512 case RELAX_LOOP_END:
6516 case RELAX_LOOP_END_ADD_NOP:
6517 /* Add a NOP and switch to .fill 0. */
6518 new_stretch = relax_frag_add_nop (fragP);
6521 case RELAX_DESIRE_ALIGN:
6522 /* We REALLY want to change the relaxation order here. This
6523 should do NOTHING. The narrowing before it will either align
6528 case RELAX_LITERAL_FINAL:
6531 case RELAX_LITERAL_NR:
6533 fragP->fr_subtype = RELAX_LITERAL_FINAL;
6534 assert (unreported == lit_size);
6535 memset (&fragP->fr_literal[fragP->fr_fix], 0, 4);
6536 fragP->fr_var -= lit_size;
6537 fragP->fr_fix += lit_size;
6542 new_stretch = relax_frag_narrow (fragP, stretch);
6546 case RELAX_IMMED_STEP1:
6547 case RELAX_IMMED_STEP2:
6548 /* Place the immediate. */
6549 new_stretch = relax_frag_immed (now_seg, fragP, stretch,
6550 fragP->fr_subtype - RELAX_IMMED,
6554 case RELAX_LITERAL_POOL_BEGIN:
6555 case RELAX_LITERAL_POOL_END:
6556 /* No relaxation required. */
6560 as_bad (_("bad relaxation state"));
6563 new_logical_line (file_name, line);
6569 relax_frag_text_align (fragP, stretch)
6573 addressT old_address, old_next_address, old_size;
6574 addressT new_address, new_next_address, new_size;
6577 /* Overview of the relaxation procedure for alignment
6578 inside an executable section:
6580 The old size is stored in the tc_frag_data.text_expansion field.
6582 Calculate the new address, fix up the text_expansion and
6583 return the growth. */
6585 /* Calculate the old address of this fragment and the next fragment. */
6586 old_address = fragP->fr_address - stretch;
6587 old_next_address = (fragP->fr_address - stretch + fragP->fr_fix +
6588 fragP->tc_frag_data.text_expansion);
6589 old_size = old_next_address - old_address;
6591 /* Calculate the new address of this fragment and the next fragment. */
6592 new_address = fragP->fr_address;
6594 get_noop_aligned_address (fragP, fragP->fr_address + fragP->fr_fix);
6595 new_size = new_next_address - new_address;
6597 growth = new_size - old_size;
6599 /* Fix up the text_expansion field and return the new growth. */
6600 fragP->tc_frag_data.text_expansion += growth;
6605 /* Add a NOP (i.e., "or a1, a1, a1"). Use the 3-byte one because we
6606 don't know about the availability of density yet. TODO: When the
6607 flags are stored per fragment, use NOP.N when possible. */
6610 relax_frag_add_nop (fragP)
6613 static xtensa_insnbuf insnbuf = NULL;
6615 char *nop_buf = fragP->fr_literal + fragP->fr_fix;
6618 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
6620 tinsn_init (&t_insn);
6621 t_insn.opcode = xtensa_or_opcode;
6622 assert (t_insn.opcode != XTENSA_UNDEFINED);
6625 set_expr_const (&t_insn.tok[0], 1);
6626 set_expr_const (&t_insn.tok[1], 1);
6627 set_expr_const (&t_insn.tok[2], 1);
6629 tinsn_to_insnbuf (&t_insn, insnbuf);
6630 fragP->tc_frag_data.is_insn = TRUE;
6631 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, nop_buf);
6633 length = xtensa_insn_length (xtensa_default_isa, t_insn.opcode);
6634 if (fragP->fr_var < length)
6636 as_warn (_("fr_var (%ld) < length (%d); ignoring"),
6637 fragP->fr_var, length);
6642 fragP->fr_fix += length;
6643 fragP->fr_var -= length;
6650 relax_frag_narrow (fragP, stretch)
6654 /* Overview of the relaxation procedure for alignment inside an
6655 executable section: Find the number of widenings required and the
6656 number of nop bytes required. Store the number of bytes ALREADY
6657 widened. If there are enough instructions to widen (must go back
6658 ONLY through NARROW fragments), mark each of the fragments as TO BE
6659 widened, recalculate the fragment addresses. */
6661 assert (fragP->fr_type == rs_machine_dependent
6662 && fragP->fr_subtype == RELAX_NARROW);
6664 if (!future_alignment_required (fragP, 0))
6666 /* If already expanded but no longer needed because of a prior
6667 stretch, it is SAFE to unexpand because the next fragment will
6668 NEVER start at an address > the previous time through the
6670 if (fragP->tc_frag_data.text_expansion)
6674 fragP->tc_frag_data.text_expansion = 0;
6677 /* Otherwise we have to live with this bad choice. */
6683 if (fragP->tc_frag_data.text_expansion == 0)
6685 fragP->tc_frag_data.text_expansion = 1;
6694 future_alignment_required (fragP, stretch)
6698 long address = fragP->fr_address + stretch;
6700 addressT aligned_address;
6701 offsetT desired_diff;
6705 /* Limit this to a small search. */
6708 address += fragP->fr_fix;
6710 switch (fragP->fr_type)
6713 address += fragP->fr_offset * fragP->fr_var;
6716 case rs_machine_dependent:
6717 switch (fragP->fr_subtype)
6720 /* address += fragP->fr_fix; */
6725 address += (/* fragP->fr_fix + */
6726 fragP->tc_frag_data.text_expansion);
6729 case RELAX_ALIGN_NEXT_OPCODE:
6730 case RELAX_DESIRE_ALIGN:
6731 /* address += fragP->fr_fix; */
6732 aligned_address = get_widen_aligned_address (fragP, address);
6733 desired_diff = aligned_address - address;
6734 assert (desired_diff >= 0);
6735 /* If there are enough wideners in between do it. */
6736 /* return (num_widens == desired_diff); */
6737 if (num_widens == desired_diff)
6739 if (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
6751 fragP = fragP->fr_next;
6759 relax_frag_immed (segP, fragP, stretch, min_steps, stretched_p)
6766 static xtensa_insnbuf insnbuf = NULL;
6769 bfd_boolean negatable_branch = FALSE;
6770 bfd_boolean branch_jmp_to_next = FALSE;
6772 offsetT frag_offset;
6775 int num_text_bytes, num_literal_bytes;
6776 int literal_diff, text_diff;
6778 assert (fragP->fr_opcode != NULL);
6781 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
6783 tinsn_from_chars (&t_insn, fragP->fr_opcode);
6784 tinsn_immed_from_frag (&t_insn, fragP);
6786 negatable_branch = is_negatable_branch (&t_insn);
6788 old_size = xtensa_insn_length (xtensa_default_isa, t_insn.opcode);
6790 if (software_avoid_b_j_loop_end)
6791 branch_jmp_to_next = is_branch_jmp_to_next (&t_insn, fragP);
6793 /* Special case: replace a branch to the next instruction with a NOP.
6794 This is required to work around a hardware bug in T1040.0 and also
6795 serves as an optimization. */
6797 if (branch_jmp_to_next
6798 && ((old_size == 2) || (old_size == 3))
6799 && !next_frag_is_loop_target (fragP))
6802 /* Here is the fun stuff: Get the immediate field from this
6803 instruction. If it fits, we are done. If not, find the next
6804 instruction sequence that fits. */
6806 frag_offset = fragP->fr_opcode - fragP->fr_literal;
6807 istack_init (&istack);
6808 num_steps = xg_assembly_relax (&istack, &t_insn, segP, fragP, frag_offset,
6809 min_steps, stretch);
6810 if (num_steps < min_steps)
6812 as_fatal (_("internal error: relaxation failed"));
6816 if (num_steps > RELAX_IMMED_MAXSTEPS)
6818 as_fatal (_("internal error: relaxation requires too many steps"));
6822 fragP->fr_subtype = (int) RELAX_IMMED + num_steps;
6824 /* Figure out the number of bytes needed. */
6826 num_text_bytes = get_num_stack_text_bytes (&istack) - old_size;
6827 num_literal_bytes = get_num_stack_literal_bytes (&istack);
6828 literal_diff = num_literal_bytes - fragP->tc_frag_data.literal_expansion;
6829 text_diff = num_text_bytes - fragP->tc_frag_data.text_expansion;
6831 /* It MUST get larger. If not, we could get an infinite loop. */
6832 know (num_text_bytes >= 0);
6833 know (literal_diff >= 0 && text_diff >= 0);
6835 fragP->tc_frag_data.text_expansion = num_text_bytes;
6836 fragP->tc_frag_data.literal_expansion = num_literal_bytes;
6838 /* Find the associated expandable literal for this. */
6839 if (literal_diff != 0)
6841 lit_fragP = fragP->tc_frag_data.literal_frag;
6844 assert (literal_diff == 4);
6845 lit_fragP->tc_frag_data.unreported_expansion += literal_diff;
6847 /* We expect that the literal section state has NOT been
6849 assert (lit_fragP->fr_type == rs_machine_dependent
6850 && lit_fragP->fr_subtype == RELAX_LITERAL);
6851 lit_fragP->fr_subtype = RELAX_LITERAL_NR;
6853 /* We need to mark this section for another iteration
6859 /* This implicitly uses the assumption that a branch is negated
6860 when the size of the output increases by at least 2 bytes. */
6862 if (negatable_branch && num_text_bytes >= 2)
6864 /* If next frag is a loop end, then switch it to add a NOP. */
6865 update_next_frag_nop_state (fragP);
6872 /* md_convert_frag Hook and Helper Functions. */
6875 md_convert_frag (abfd, sec, fragp)
6876 bfd *abfd ATTRIBUTE_UNUSED;
6883 as_where (&file_name, &line);
6884 new_logical_line (fragp->fr_file, fragp->fr_line);
6886 switch (fragp->fr_subtype)
6888 case RELAX_ALIGN_NEXT_OPCODE:
6889 /* Always convert. */
6890 convert_frag_align_next_opcode (fragp);
6893 case RELAX_DESIRE_ALIGN:
6894 /* Do nothing. If not aligned already, too bad. */
6898 case RELAX_LITERAL_FINAL:
6902 /* No conversion. */
6903 convert_frag_narrow (fragp);
6907 case RELAX_IMMED_STEP1:
6908 case RELAX_IMMED_STEP2:
6909 /* Place the immediate. */
6910 convert_frag_immed (sec, fragp, fragp->fr_subtype - RELAX_IMMED);
6913 case RELAX_LITERAL_NR:
6914 if (use_literal_section)
6916 /* This should have been handled during relaxation. When
6917 relaxing a code segment, literals sometimes need to be
6918 added to the corresponding literal segment. If that
6919 literal segment has already been relaxed, then we end up
6920 in this situation. Marking the literal segments as data
6921 would make this happen less often (since GAS always relaxes
6922 code before data), but we could still get into trouble if
6923 there are instructions in a segment that is not marked as
6924 containing code. Until we can implement a better solution,
6925 cheat and adjust the addresses of all the following frags.
6926 This could break subsequent alignments, but the linker's
6927 literal coalescing will do that anyway. */
6930 fragp->fr_subtype = RELAX_LITERAL_FINAL;
6931 assert (fragp->tc_frag_data.unreported_expansion == 4);
6932 memset (&fragp->fr_literal[fragp->fr_fix], 0, 4);
6935 for (f = fragp->fr_next; f; f = f->fr_next)
6939 as_bad (_("invalid relaxation fragment result"));
6944 new_logical_line (file_name, line);
6949 convert_frag_align_next_opcode (fragp)
6952 char *nop_buf; /* Location for Writing. */
6955 bfd_boolean use_no_density = fragp->tc_frag_data.is_no_density;
6956 addressT aligned_address;
6957 size_t fill_size, nop_count;
6959 aligned_address = get_noop_aligned_address (fragp, fragp->fr_address +
6961 fill_size = aligned_address - (fragp->fr_address + fragp->fr_fix);
6962 nop_count = get_text_align_nop_count (fill_size, use_no_density);
6963 nop_buf = fragp->fr_literal + fragp->fr_fix;
6965 for (i = 0; i < nop_count; i++)
6968 nop_size = get_text_align_nth_nop_size (fill_size, i, use_no_density);
6970 assemble_nop (nop_size, nop_buf);
6971 nop_buf += nop_size;
6974 fragp->fr_fix += fill_size;
6975 fragp->fr_var -= fill_size;
6980 convert_frag_narrow (fragP)
6983 static xtensa_insnbuf insnbuf = NULL;
6984 TInsn t_insn, single_target;
6985 int size, old_size, diff, error_val;
6986 offsetT frag_offset;
6988 if (fragP->tc_frag_data.text_expansion == 0)
6990 /* No conversion. */
6995 assert (fragP->fr_opcode != NULL);
6998 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
7000 tinsn_from_chars (&t_insn, fragP->fr_opcode);
7001 tinsn_immed_from_frag (&t_insn, fragP);
7003 /* Just convert it to a wide form.... */
7005 old_size = xtensa_insn_length (xtensa_default_isa, t_insn.opcode);
7007 tinsn_init (&single_target);
7008 frag_offset = fragP->fr_opcode - fragP->fr_literal;
7010 error_val = xg_expand_narrow (&single_target, &t_insn);
7012 as_bad (_("unable to widen instruction"));
7014 size = xtensa_insn_length (xtensa_default_isa, single_target.opcode);
7015 xg_emit_insn_to_buf (&single_target, fragP->fr_opcode,
7016 fragP, frag_offset, TRUE);
7018 diff = size - old_size;
7020 assert (diff <= fragP->fr_var);
7021 fragP->fr_var -= diff;
7022 fragP->fr_fix += diff;
7030 convert_frag_immed (segP, fragP, min_steps)
7035 char *immed_instr = fragP->fr_opcode;
7036 static xtensa_insnbuf insnbuf = NULL;
7038 bfd_boolean expanded = FALSE;
7039 char *fr_opcode = fragP->fr_opcode;
7040 bfd_boolean branch_jmp_to_next = FALSE;
7043 assert (fragP->fr_opcode != NULL);
7046 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
7048 tinsn_from_chars (&orig_t_insn, fragP->fr_opcode);
7049 tinsn_immed_from_frag (&orig_t_insn, fragP);
7051 /* Here is the fun stuff: Get the immediate field from this
7052 instruction. If it fits, we're done. If not, find the next
7053 instruction sequence that fits. */
7055 if (software_avoid_b_j_loop_end)
7056 branch_jmp_to_next = is_branch_jmp_to_next (&orig_t_insn, fragP);
7058 if (branch_jmp_to_next && !next_frag_is_loop_target (fragP))
7060 /* Conversion just inserts a NOP and marks the fix as completed. */
7061 size = xtensa_insn_length (xtensa_default_isa, orig_t_insn.opcode);
7062 assemble_nop (size, fragP->fr_opcode);
7069 symbolS *lit_sym = NULL;
7073 symbolS *gen_label = NULL;
7074 offsetT frag_offset;
7076 /* It does not fit. Find something that does and
7077 convert immediately. */
7078 frag_offset = fragP->fr_opcode - fragP->fr_literal;
7079 istack_init (&istack);
7080 xg_assembly_relax (&istack, &orig_t_insn,
7081 segP, fragP, frag_offset, min_steps, 0);
7083 old_size = xtensa_insn_length (xtensa_default_isa, orig_t_insn.opcode);
7085 /* Assemble this right inline. */
7087 /* First, create the mapping from a label name to the REAL label. */
7089 for (i = 0; i < istack.ninsn; i++)
7091 TInsn *t_insn = &istack.insn[i];
7095 switch (t_insn->insn_type)
7098 if (lit_sym != NULL)
7099 as_bad (_("multiple literals in expansion"));
7100 /* First find the appropriate space in the literal pool. */
7101 lit_frag = fragP->tc_frag_data.literal_frag;
7102 if (lit_frag == NULL)
7103 as_bad (_("no registered fragment for literal"));
7104 if (t_insn->ntok != 1)
7105 as_bad (_("number of literal tokens != 1"));
7107 /* Set the literal symbol and add a fixup. */
7108 lit_sym = lit_frag->fr_symbol;
7112 assert (gen_label == NULL);
7113 gen_label = symbol_new (FAKE_LABEL_NAME, now_seg,
7114 fragP->fr_opcode - fragP->fr_literal +
7119 size = xtensa_insn_length (xtensa_default_isa, t_insn->opcode);
7126 for (i = 0; i < istack.ninsn; i++)
7128 TInsn *t_insn = &istack.insn[i];
7133 switch (t_insn->insn_type)
7136 lit_frag = fragP->tc_frag_data.literal_frag;
7137 /* already checked */
7138 assert (lit_frag != NULL);
7139 assert (lit_sym != NULL);
7140 assert (t_insn->ntok == 1);
7142 target_seg = S_GET_SEGMENT (lit_sym);
7143 assert (target_seg);
7144 fix_new_exp_in_seg (target_seg, 0, lit_frag, 0, 4,
7145 &t_insn->tok[0], FALSE, BFD_RELOC_32);
7152 xg_resolve_labels (t_insn, gen_label);
7153 xg_resolve_literals (t_insn, lit_sym);
7154 size = xtensa_insn_length (xtensa_default_isa, t_insn->opcode);
7156 xg_emit_insn_to_buf (t_insn, immed_instr, fragP,
7157 immed_instr - fragP->fr_literal, TRUE);
7158 immed_instr += size;
7163 diff = total_size - old_size;
7167 assert (diff <= fragP->fr_var);
7168 fragP->fr_var -= diff;
7169 fragP->fr_fix += diff;
7175 /* Check for undefined immediates in LOOP instructions. */
7176 if (is_loop_opcode (orig_t_insn.opcode))
7179 sym = orig_t_insn.tok[1].X_add_symbol;
7180 if (sym != NULL && !S_IS_DEFINED (sym))
7182 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
7185 sym = orig_t_insn.tok[1].X_op_symbol;
7186 if (sym != NULL && !S_IS_DEFINED (sym))
7188 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
7193 if (expanded && is_loop_opcode (orig_t_insn.opcode))
7194 convert_frag_immed_finish_loop (segP, fragP, &orig_t_insn);
7196 if (expanded && is_direct_call_opcode (orig_t_insn.opcode))
7198 /* Add an expansion note on the expanded instruction. */
7199 fix_new_exp_in_seg (now_seg, 0, fragP, fr_opcode - fragP->fr_literal, 4,
7200 &orig_t_insn.tok[0], TRUE,
7201 BFD_RELOC_XTENSA_ASM_EXPAND);
7207 /* Add a new fix expression into the desired segment. We have to
7208 switch to that segment to do this. */
7211 fix_new_exp_in_seg (new_seg, new_subseg,
7212 frag, where, size, exp, pcrel, r_type)
7220 bfd_reloc_code_real_type r_type;
7224 subsegT subseg = now_subseg;
7225 assert (new_seg != 0);
7226 subseg_set (new_seg, new_subseg);
7228 if (r_type == BFD_RELOC_32
7229 && exp->X_add_symbol
7230 && symbol_get_tc (exp->X_add_symbol)->plt == 1)
7232 r_type = BFD_RELOC_XTENSA_PLT;
7235 new_fix = fix_new_exp (frag, where, size, exp, pcrel, r_type);
7236 subseg_set (seg, subseg);
7241 /* Relax a loop instruction so that it can span loop >256 bytes. */
7247 addi as, as, lo8(label-.L1)
7248 addmi as, as, mid8(label-.L1)
7258 convert_frag_immed_finish_loop (segP, fragP, t_insn)
7266 unsigned long target;
7267 static xtensa_insnbuf insnbuf = NULL;
7268 unsigned int loop_length, loop_length_hi, loop_length_lo;
7269 xtensa_isa isa = xtensa_default_isa;
7270 addressT loop_offset;
7271 addressT addi_offset = 9;
7272 addressT addmi_offset = 12;
7275 insnbuf = xtensa_insnbuf_alloc (isa);
7277 /* Get the loop offset. */
7278 loop_offset = get_expanded_loop_offset (t_insn->opcode);
7279 /* Validate that there really is a LOOP at the loop_offset. */
7280 tinsn_from_chars (&loop_insn, fragP->fr_opcode + loop_offset);
7282 if (!is_loop_opcode (loop_insn.opcode))
7284 as_bad_where (fragP->fr_file, fragP->fr_line,
7285 _("loop relaxation specification does not correspond"));
7288 addi_offset += loop_offset;
7289 addmi_offset += loop_offset;
7291 assert (t_insn->ntok == 2);
7292 target = get_expression_value (segP, &t_insn->tok[1]);
7295 know (symbolP->sy_frag);
7296 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
7297 || symbol_get_frag (symbolP) == &zero_address_frag);
7299 loop_length = target - (fragP->fr_address + fragP->fr_fix);
7300 loop_length_hi = loop_length & ~0x0ff;
7301 loop_length_lo = loop_length & 0x0ff;
7302 if (loop_length_lo >= 128)
7304 loop_length_lo -= 256;
7305 loop_length_hi += 256;
7308 /* Because addmi sign-extends the immediate, 'loop_length_hi' can be at most
7309 32512. If the loop is larger than that, then we just fail. */
7310 if (loop_length_hi > 32512)
7311 as_bad_where (fragP->fr_file, fragP->fr_line,
7312 _("loop too long for LOOP instruction"));
7314 tinsn_from_chars (&addi_insn, fragP->fr_opcode + addi_offset);
7315 assert (addi_insn.opcode == xtensa_addi_opcode);
7317 tinsn_from_chars (&addmi_insn, fragP->fr_opcode + addmi_offset);
7318 assert (addmi_insn.opcode == xtensa_addmi_opcode);
7320 set_expr_const (&addi_insn.tok[2], loop_length_lo);
7321 tinsn_to_insnbuf (&addi_insn, insnbuf);
7323 fragP->tc_frag_data.is_insn = TRUE;
7324 xtensa_insnbuf_to_chars (isa, insnbuf, fragP->fr_opcode + addi_offset);
7326 set_expr_const (&addmi_insn.tok[2], loop_length_hi);
7327 tinsn_to_insnbuf (&addmi_insn, insnbuf);
7328 xtensa_insnbuf_to_chars (isa, insnbuf, fragP->fr_opcode + addmi_offset);
7333 get_expression_value (segP, exp)
7337 if (exp->X_op == O_constant)
7338 return exp->X_add_number;
7339 if (exp->X_op == O_symbol)
7341 /* Find the fragment. */
7342 symbolS *sym = exp->X_add_symbol;
7344 assert (S_GET_SEGMENT (sym) == segP
7345 || S_GET_SEGMENT (sym) == absolute_section);
7347 return (S_GET_VALUE (sym) + exp->X_add_number);
7349 as_bad (_("invalid expression evaluation type %d"), exp->X_op);
7354 /* A map that keeps information on a per-subsegment basis. This is
7355 maintained during initial assembly, but is invalid once the
7356 subsegments are smashed together. I.E., it cannot be used during
7359 typedef struct subseg_map_struct
7368 struct subseg_map_struct *next;
7371 static subseg_map *sseg_map = NULL;
7375 get_last_insn_flags (seg, subseg)
7379 subseg_map *subseg_e;
7381 for (subseg_e = sseg_map; subseg_e != NULL; subseg_e = subseg_e->next)
7382 if (seg == subseg_e->seg && subseg == subseg_e->subseg)
7383 return subseg_e->flags;
7390 set_last_insn_flags (seg, subseg, fl, val)
7396 subseg_map *subseg_e;
7398 for (subseg_e = sseg_map; subseg_e; subseg_e = subseg_e->next)
7399 if (seg == subseg_e->seg && subseg == subseg_e->subseg)
7404 subseg_e = (subseg_map *) xmalloc (sizeof (subseg_map));
7405 memset (subseg_e, 0, sizeof (subseg_map));
7406 subseg_e->seg = seg;
7407 subseg_e->subseg = subseg;
7408 subseg_e->flags = 0;
7409 subseg_e->next = sseg_map;
7410 sseg_map = subseg_e;
7414 subseg_e->flags |= fl;
7416 subseg_e->flags &= ~fl;
7420 /* Segment Lists and emit_state Stuff. */
7422 /* Remove the segment from the global sections list. */
7425 xtensa_remove_section (sec)
7428 /* Handle brain-dead bfd_section_list_remove macro, which
7429 expect the address of the prior section's "next" field, not
7430 just the address of the section to remove. */
7432 segT *ps_next_ptr = &stdoutput->sections;
7433 while (*ps_next_ptr != sec && *ps_next_ptr != NULL)
7434 ps_next_ptr = &(*ps_next_ptr)->next;
7436 assert (*ps_next_ptr != NULL);
7438 bfd_section_list_remove (stdoutput, ps_next_ptr);
7443 xtensa_insert_section (after_sec, sec)
7447 segT *after_sec_next;
7448 if (after_sec == NULL)
7449 after_sec_next = &stdoutput->sections;
7451 after_sec_next = &after_sec->next;
7453 bfd_section_list_insert (stdoutput, after_sec_next, sec);
7458 xtensa_move_seg_list_to_beginning (head)
7464 segT literal_section = head->seg;
7466 /* Move the literal section to the front of the section list. */
7467 assert (literal_section);
7468 xtensa_remove_section (literal_section);
7469 xtensa_insert_section (NULL, literal_section);
7477 xtensa_move_literals ()
7480 frchainS *frchain_from, *frchain_to;
7481 fragS *search_frag, *next_frag, *last_frag, *literal_pool, *insert_after;
7482 fragS **frag_splice;
7485 fixS *fix, *next_fix, **fix_splice;
7488 /* As clunky as this is, we can't rely on frag_var
7489 and frag_variant to get called in all situations. */
7491 segment = literal_head->next;
7494 frchain_from = seg_info (segment->seg)->frchainP;
7495 search_frag = frchain_from->frch_root;
7498 search_frag->tc_frag_data.is_literal = TRUE;
7499 search_frag = search_frag->fr_next;
7501 segment = segment->next;
7504 if (use_literal_section)
7507 segment = literal_head->next;
7510 frchain_from = seg_info (segment->seg)->frchainP;
7511 search_frag = frchain_from->frch_root;
7512 literal_pool = NULL;
7514 frag_splice = &(frchain_from->frch_root);
7516 while (!search_frag->tc_frag_data.literal_frag)
7518 assert (search_frag->fr_fix == 0
7519 || search_frag->fr_type == rs_align);
7520 search_frag = search_frag->fr_next;
7523 assert (search_frag->tc_frag_data.literal_frag->fr_subtype
7524 == RELAX_LITERAL_POOL_BEGIN);
7525 xtensa_switch_section_emit_state (&state, segment->seg, 0);
7527 /* Make sure that all the frags in this series are closed, and
7528 that there is at least one left over of zero-size. This
7529 prevents us from making a segment with an frchain without any
7531 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
7532 last_frag = frag_now;
7533 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
7535 while (search_frag != frag_now)
7537 next_frag = search_frag->fr_next;
7539 /* First, move the frag out of the literal section and
7540 to the appropriate place. */
7541 if (search_frag->tc_frag_data.literal_frag)
7543 literal_pool = search_frag->tc_frag_data.literal_frag;
7544 assert (literal_pool->fr_subtype == RELAX_LITERAL_POOL_BEGIN);
7545 /* Note that we set this fr_var to be a fix
7546 chain when we created the literal pool location
7547 as RELAX_LITERAL_POOL_BEGIN. */
7548 frchain_to = (frchainS *) literal_pool->fr_var;
7550 insert_after = literal_pool;
7552 while (insert_after->fr_next->fr_subtype != RELAX_LITERAL_POOL_END)
7553 insert_after = insert_after->fr_next;
7555 dest_seg = (segT) insert_after->fr_next->fr_var;
7557 *frag_splice = next_frag;
7558 search_frag->fr_next = insert_after->fr_next;
7559 insert_after->fr_next = search_frag;
7560 search_frag->tc_frag_data.lit_seg = dest_seg;
7562 /* Now move any fixups associated with this frag to the
7564 fix = frchain_from->fix_root;
7565 fix_splice = &(frchain_from->fix_root);
7568 next_fix = fix->fx_next;
7569 if (fix->fx_frag == search_frag)
7571 *fix_splice = next_fix;
7572 fix->fx_next = frchain_to->fix_root;
7573 frchain_to->fix_root = fix;
7574 if (frchain_to->fix_tail == NULL)
7575 frchain_to->fix_tail = fix;
7578 fix_splice = &(fix->fx_next);
7581 search_frag = next_frag;
7584 if (frchain_from->fix_root != NULL)
7586 frchain_from = seg_info (segment->seg)->frchainP;
7587 as_warn (_("fixes not all moved from %s"), segment->seg->name);
7589 assert (frchain_from->fix_root == NULL);
7591 frchain_from->fix_tail = NULL;
7592 xtensa_restore_emit_state (&state);
7593 segment = segment->next;
7596 /* Now fix up the SEGMENT value for all the literal symbols. */
7597 for (lit = literal_syms; lit; lit = lit->next)
7599 symbolS *lit_sym = lit->sym;
7600 segT dest_seg = symbol_get_frag (lit_sym)->tc_frag_data.lit_seg;
7601 S_SET_SEGMENT (lit_sym, dest_seg);
7607 xtensa_reorder_seg_list (head, after)
7611 /* Move all of the sections in the section list to come
7612 after "after" in the gnu segment list. */
7617 segT literal_section = head->seg;
7619 /* Move the literal section after "after". */
7620 assert (literal_section);
7621 if (literal_section != after)
7623 xtensa_remove_section (literal_section);
7624 xtensa_insert_section (after, literal_section);
7632 /* Push all the literal segments to the end of the gnu list. */
7635 xtensa_reorder_segments ()
7642 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
7645 /* Now that we have the last section, push all the literal
7646 sections to the end. */
7647 last_sec = get_last_sec ();
7648 xtensa_reorder_seg_list (literal_head, last_sec);
7649 xtensa_reorder_seg_list (init_literal_head, last_sec);
7650 xtensa_reorder_seg_list (fini_literal_head, last_sec);
7652 /* Now perform the final error check. */
7653 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
7655 assert (new_count == old_count);
7662 segT last_sec = stdoutput->sections;
7663 while (last_sec->next != NULL)
7664 last_sec = last_sec->next;
7670 /* Change the emit state (seg, subseg, and frag related stuff) to the
7671 correct location. Return a emit_state which can be passed to
7672 xtensa_restore_emit_state to return to current fragment. */
7675 xtensa_switch_to_literal_fragment (result)
7678 /* When we mark a literal pool location, we want to put a frag in
7679 the literal pool that points to it. But to do that, we want to
7680 switch_to_literal_fragment. But literal sections don't have
7681 literal pools, so their location is always null, so we would
7682 recurse forever. This is kind of hacky, but it works. */
7684 static bfd_boolean recursive = FALSE;
7685 fragS *pool_location = get_literal_pool_location (now_seg);
7686 bfd_boolean is_init =
7687 (now_seg && !strcmp (segment_name (now_seg), INIT_SECTION_NAME));
7689 bfd_boolean is_fini =
7690 (now_seg && !strcmp (segment_name (now_seg), FINI_SECTION_NAME));
7693 if (pool_location == NULL
7694 && !use_literal_section
7696 && !is_init && ! is_fini)
7698 as_warn (_("inlining literal pool; "
7699 "specify location with .literal_position."));
7701 xtensa_mark_literal_pool_location ();
7705 /* Special case: If we are in the ".fini" or ".init" section, then
7706 we will ALWAYS be generating to the ".fini.literal" and
7707 ".init.literal" sections. */
7711 cache_literal_section (init_literal_head,
7712 default_lit_sections.init_lit_seg_name,
7713 &default_lit_sections.init_lit_seg);
7714 xtensa_switch_section_emit_state (result,
7715 default_lit_sections.init_lit_seg, 0);
7719 cache_literal_section (fini_literal_head,
7720 default_lit_sections.fini_lit_seg_name,
7721 &default_lit_sections.fini_lit_seg);
7722 xtensa_switch_section_emit_state (result,
7723 default_lit_sections.fini_lit_seg, 0);
7727 cache_literal_section (literal_head,
7728 default_lit_sections.lit_seg_name,
7729 &default_lit_sections.lit_seg);
7730 xtensa_switch_section_emit_state (result,
7731 default_lit_sections.lit_seg, 0);
7734 if (!use_literal_section &&
7735 !is_init && !is_fini &&
7736 get_literal_pool_location (now_seg) != pool_location)
7738 /* Close whatever frag is there. */
7739 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
7740 frag_now->tc_frag_data.literal_frag = pool_location;
7741 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
7744 /* Do a 4 byte align here. */
7745 frag_align (2, 0, 0);
7749 /* Call this function before emitting data into the literal section.
7750 This is a helper function for xtensa_switch_to_literal_fragment.
7751 This is similar to a .section new_now_seg subseg. */
7754 xtensa_switch_section_emit_state (state, new_now_seg, new_now_subseg)
7757 subsegT new_now_subseg;
7759 state->name = now_seg->name;
7760 state->now_seg = now_seg;
7761 state->now_subseg = now_subseg;
7762 state->generating_literals = generating_literals;
7763 generating_literals++;
7764 subseg_new (segment_name (new_now_seg), new_now_subseg);
7768 /* Use to restore the emitting into the normal place. */
7771 xtensa_restore_emit_state (state)
7774 generating_literals = state->generating_literals;
7775 subseg_new (state->name, state->now_subseg);
7779 /* Get a segment of a given name. If the segment is already
7780 present, return it; otherwise, create a new one. */
7783 cache_literal_section (head, name, seg)
7788 segT current_section = now_seg;
7789 int current_subsec = now_subseg;
7793 *seg = retrieve_literal_seg (head, name);
7794 subseg_set (current_section, current_subsec);
7798 /* Get a segment of a given name. If the segment is already
7799 present, return it; otherwise, create a new one. */
7802 retrieve_literal_seg (head, name)
7810 ret = seg_present (name);
7813 ret = subseg_new (name, (subsegT) 0);
7814 add_seg_list (head, ret);
7815 bfd_set_section_flags (stdoutput, ret, SEC_HAS_CONTENTS |
7816 SEC_READONLY | SEC_ALLOC | SEC_LOAD | SEC_CODE);
7817 bfd_set_section_alignment (stdoutput, ret, 2);
7824 /* Return a segment of a given name if it is present. */
7831 seg = stdoutput->sections;
7835 if (!strcmp (segment_name (seg), name))
7844 /* Add a segment to a segment list. */
7847 add_seg_list (head, seg)
7852 n = (seg_list *) xmalloc (sizeof (seg_list));
7856 n->next = head->next;
7861 /* Set up Property Tables after Relaxation. */
7863 #define XTENSA_INSN_SEC_NAME ".xt.insn"
7864 #define XTENSA_LIT_SEC_NAME ".xt.lit"
7867 xtensa_post_relax_hook ()
7869 xtensa_move_seg_list_to_beginning (literal_head);
7870 xtensa_move_seg_list_to_beginning (init_literal_head);
7871 xtensa_move_seg_list_to_beginning (fini_literal_head);
7873 xtensa_create_property_segments (get_frag_is_insn,
7874 XTENSA_INSN_SEC_NAME,
7876 if (use_literal_section)
7877 xtensa_create_property_segments (get_frag_is_literal,
7878 XTENSA_LIT_SEC_NAME,
7884 get_frag_is_literal (fragP)
7887 assert (fragP != NULL);
7888 return (fragP->tc_frag_data.is_literal);
7893 get_frag_is_insn (fragP)
7896 assert (fragP != NULL);
7897 return (fragP->tc_frag_data.is_insn);
7902 xtensa_create_property_segments (property_function, section_name_base,
7904 frag_predicate property_function;
7905 const char * section_name_base;
7906 xt_section_type sec_type;
7910 /* Walk over all of the current segments.
7911 Walk over each fragment
7912 For each fragment that has instructions
7913 Build an instruction record (append where possible). */
7915 for (seclist = &stdoutput->sections;
7916 seclist && *seclist;
7917 seclist = &(*seclist)->next)
7919 segT sec = *seclist;
7920 if (section_has_property (sec, property_function))
7922 char *property_section_name =
7923 xtensa_get_property_section_name (sec, section_name_base);
7924 segT insn_sec = retrieve_xtensa_section (property_section_name);
7925 segment_info_type *xt_seg_info = retrieve_segment_info (insn_sec);
7926 xtensa_block_info **xt_blocks =
7927 &xt_seg_info->tc_segment_info_data.blocks[sec_type];
7928 /* Walk over all of the frchains here and add new sections. */
7929 add_xt_block_frags (sec, insn_sec, xt_blocks, property_function);
7933 /* Now we fill them out.... */
7935 for (seclist = &stdoutput->sections;
7936 seclist && *seclist;
7937 seclist = &(*seclist)->next)
7939 segment_info_type *seginfo;
7940 xtensa_block_info *block;
7941 segT sec = *seclist;
7942 seginfo = seg_info (sec);
7943 block = seginfo->tc_segment_info_data.blocks[sec_type];
7947 xtensa_block_info *cur_block;
7948 /* This is a section with some data. */
7949 size_t num_recs = 0;
7952 for (cur_block = block; cur_block; cur_block = cur_block->next)
7955 rec_size = num_recs * 8;
7956 bfd_set_section_size (stdoutput, sec, rec_size);
7958 /* In order to make this work with the assembler, we have to
7959 build some frags and then build the "fixups" for it. It
7960 would be easier to just set the contents then set the
7965 /* Allocate a fragment and leak it. */
7973 frag_size = sizeof (fragS) + rec_size;
7974 fragP = (fragS *) xmalloc (frag_size);
7976 memset (fragP, 0, frag_size);
7977 fragP->fr_address = 0;
7978 fragP->fr_next = NULL;
7979 fragP->fr_fix = rec_size;
7981 fragP->fr_type = rs_fill;
7982 /* the rest are zeros */
7984 frchainP = seginfo->frchainP;
7985 frchainP->frch_root = fragP;
7986 frchainP->frch_last = fragP;
7988 fixes = (fixS *) xmalloc (sizeof (fixS) * num_recs);
7989 memset (fixes, 0, sizeof (fixS) * num_recs);
7991 seginfo->fix_root = fixes;
7992 seginfo->fix_tail = &fixes[num_recs - 1];
7994 frag_data = &fragP->fr_literal[0];
7995 for (i = 0; i < num_recs; i++)
7997 fixS *fix = &fixes[i];
8000 /* Write the fixup. */
8001 if (i != num_recs - 1)
8002 fix->fx_next = &fixes[i + 1];
8004 fix->fx_next = NULL;
8007 fix->fx_frag = fragP;
8008 fix->fx_where = i * 8;
8009 fix->fx_addsy = section_symbol (cur_block->sec);
8010 fix->fx_offset = cur_block->offset;
8011 fix->fx_r_type = BFD_RELOC_32;
8012 fix->fx_file = "Internal Assembly";
8015 /* Write the length. */
8016 md_number_to_chars (&frag_data[4 + 8 * i],
8017 cur_block->size, 4);
8018 cur_block = cur_block->next;
8027 retrieve_segment_info (seg)
8030 segment_info_type *seginfo;
8031 seginfo = (segment_info_type *) bfd_get_section_userdata (stdoutput, seg);
8036 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
8037 memset ((PTR) seginfo, 0, sizeof (*seginfo));
8038 seginfo->fix_root = NULL;
8039 seginfo->fix_tail = NULL;
8040 seginfo->bfd_section = seg;
8042 /* We will not be dealing with these, only our special ones. */
8044 if (seg == bfd_abs_section_ptr)
8045 abs_seg_info = seginfo;
8046 else if (seg == bfd_und_section_ptr)
8047 und_seg_info = seginfo;
8050 bfd_set_section_userdata (stdoutput, seg, (PTR) seginfo);
8052 seg_fix_rootP = &segment_info[seg].fix_root;
8053 seg_fix_tailP = &segment_info[seg].fix_tail;
8056 frchainP = (frchainS *) xmalloc (sizeof (frchainS));
8057 frchainP->frch_root = NULL;
8058 frchainP->frch_last = NULL;
8059 frchainP->frch_next = NULL;
8060 frchainP->frch_seg = seg;
8061 frchainP->frch_subseg = 0;
8062 frchainP->fix_root = NULL;
8063 frchainP->fix_tail = NULL;
8064 /* Do not init the objstack. */
8065 /* obstack_begin (&frchainP->frch_obstack, chunksize); */
8066 /* frchainP->frch_frag_now = fragP; */
8067 frchainP->frch_frag_now = NULL;
8069 seginfo->frchainP = frchainP;
8077 retrieve_xtensa_section (sec_name)
8080 bfd *abfd = stdoutput;
8081 flagword flags, out_flags, link_once_flags;
8084 flags = bfd_get_section_flags (abfd, now_seg);
8085 link_once_flags = (flags & SEC_LINK_ONCE);
8086 if (link_once_flags)
8087 link_once_flags |= (flags & SEC_LINK_DUPLICATES);
8088 out_flags = (SEC_RELOC | SEC_HAS_CONTENTS | SEC_READONLY | link_once_flags);
8090 s = bfd_make_section_old_way (abfd, sec_name);
8092 as_bad (_("could not create section %s"), sec_name);
8093 if (!bfd_set_section_flags (abfd, s, out_flags))
8094 as_bad (_("invalid flag combination on section %s"), sec_name);
8101 section_has_property (sec, property_function)
8103 frag_predicate property_function;
8105 segment_info_type *seginfo = seg_info (sec);
8108 if (seginfo && seginfo->frchainP)
8110 for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
8112 if (property_function (fragP)
8113 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
8121 /* Two types of block sections exist right now: literal and insns. */
8124 add_xt_block_frags (sec, xt_block_sec, xt_block, property_function)
8127 xtensa_block_info **xt_block;
8128 frag_predicate property_function;
8130 segment_info_type *seg_info;
8131 segment_info_type *xt_seg_info;
8135 xt_seg_info = retrieve_segment_info (xt_block_sec);
8136 seg_info = retrieve_segment_info (sec);
8138 /* Build it if needed. */
8139 while (*xt_block != NULL)
8140 xt_block = &(*xt_block)->next;
8141 /* We are either at NULL at the beginning or at the end. */
8143 /* Walk through the frags. */
8146 if (seg_info->frchainP)
8148 for (fragP = seg_info->frchainP->frch_root;
8150 fragP = fragP->fr_next)
8152 if (property_function (fragP)
8153 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
8155 if (*xt_block != NULL)
8157 if ((*xt_block)->offset + (*xt_block)->size
8158 == fragP->fr_address)
8159 (*xt_block)->size += fragP->fr_fix;
8161 xt_block = &((*xt_block)->next);
8163 if (*xt_block == NULL)
8165 xtensa_block_info *new_block = (xtensa_block_info *)
8166 xmalloc (sizeof (xtensa_block_info));
8167 new_block->sec = sec;
8168 new_block->offset = fragP->fr_address;
8169 new_block->size = fragP->fr_fix;
8170 new_block->next = NULL;
8171 *xt_block = new_block;
8179 /* Instruction Stack Functions (from "xtensa-istack.h"). */
8185 memset (stack, 0, sizeof (IStack));
8191 istack_empty (stack)
8194 return (stack->ninsn == 0);
8202 return (stack->ninsn == MAX_ISTACK);
8206 /* Return a pointer to the top IStack entry.
8207 It is an error to call this if istack_empty () is true. */
8213 int rec = stack->ninsn - 1;
8214 assert (!istack_empty (stack));
8215 return &stack->insn[rec];
8219 /* Add a new TInsn to an IStack.
8220 It is an error to call this if istack_full () is true. */
8223 istack_push (stack, insn)
8227 int rec = stack->ninsn;
8228 assert (!istack_full (stack));
8229 tinsn_copy (&stack->insn[rec], insn);
8234 /* Clear space for the next TInsn on the IStack and return a pointer
8235 to it. It is an error to call this if istack_full () is true. */
8238 istack_push_space (stack)
8241 int rec = stack->ninsn;
8243 assert (!istack_full (stack));
8244 insn = &stack->insn[rec];
8245 memset (insn, 0, sizeof (TInsn));
8251 /* Remove the last pushed instruction. It is an error to call this if
8252 istack_empty () returns true. */
8258 int rec = stack->ninsn - 1;
8259 assert (!istack_empty (stack));
8261 memset (&stack->insn[rec], 0, sizeof (TInsn));
8265 /* TInsn functions. */
8271 memset (dst, 0, sizeof (TInsn));
8276 tinsn_copy (dst, src)
8281 memcpy (dst, src, sizeof (TInsn));
8285 /* Get the ``num''th token of the TInsn.
8286 It is illegal to call this if num > insn->ntoks. */
8289 tinsn_get_tok (insn, num)
8293 assert (num < insn->ntok);
8294 return &insn->tok[num];
8298 /* Return true if ANY of the operands in the insn are symbolic. */
8301 tinsn_has_symbolic_operands (insn)
8307 assert (insn->insn_type == ITYPE_INSN);
8309 for (i = 0; i < n; ++i)
8311 switch (insn->tok[i].X_op)
8325 tinsn_has_invalid_symbolic_operands (insn)
8331 assert (insn->insn_type == ITYPE_INSN);
8333 for (i = 0; i < n; ++i)
8335 switch (insn->tok[i].X_op)
8341 if (i == get_relaxable_immed (insn->opcode))
8343 as_bad (_("invalid symbolic operand %d on '%s'"),
8344 i, xtensa_opcode_name (xtensa_default_isa, insn->opcode));
8352 /* For assembly code with complex expressions (e.g. subtraction),
8353 we have to build them in the literal pool so that
8354 their results are calculated correctly after relaxation.
8355 The relaxation only handles expressions that
8356 boil down to SYMBOL + OFFSET. */
8359 tinsn_has_complex_operands (insn)
8364 assert (insn->insn_type == ITYPE_INSN);
8365 for (i = 0; i < n; ++i)
8367 switch (insn->tok[i].X_op)
8381 /* Convert the constant operands in the t_insn to insnbuf.
8382 Return true if there is a symbol in the immediate field.
8384 Before this is called,
8385 1) the number of operands are correct
8386 2) the t_insn is a ITYPE_INSN
8387 3) ONLY the relaxable_ is built
8388 4) All operands are O_constant, O_symbol. All constants fit
8389 The return value tells whether there are any remaining O_symbols. */
8392 tinsn_to_insnbuf (t_insn, insnbuf)
8394 xtensa_insnbuf insnbuf;
8396 xtensa_isa isa = xtensa_default_isa;
8397 xtensa_opcode opcode = t_insn->opcode;
8398 bfd_boolean has_fixup = FALSE;
8399 int noperands = xtensa_num_operands (isa, opcode);
8405 assert (t_insn->insn_type == ITYPE_INSN);
8406 if (noperands != t_insn->ntok)
8407 as_fatal (_("operand number mismatch"));
8409 xtensa_encode_insn (isa, opcode, insnbuf);
8411 for (i = 0; i < noperands; ++i)
8413 expressionS *expr = &t_insn->tok[i];
8414 xtensa_operand operand = xtensa_get_operand (isa, opcode, i);
8418 /* The register number has already been checked in
8419 expression_maybe_register, so we don't need to check here. */
8420 opnd_value = expr->X_add_number;
8421 (void) xtensa_operand_encode (operand, &opnd_value);
8422 xtensa_operand_set_field (operand, insnbuf, opnd_value);
8426 as_where (&file_name, &line);
8427 /* It is a constant and we called this function,
8428 then we have to try to fit it. */
8429 xtensa_insnbuf_set_operand (insnbuf, opcode, operand,
8430 expr->X_add_number, file_name, line);
8443 /* Check the instruction arguments. Return true on failure. */
8446 tinsn_check_arguments (insn)
8449 xtensa_isa isa = xtensa_default_isa;
8450 xtensa_opcode opcode = insn->opcode;
8452 if (opcode == XTENSA_UNDEFINED)
8454 as_bad (_("invalid opcode"));
8458 if (xtensa_num_operands (isa, opcode) > insn->ntok)
8460 as_bad (_("too few operands"));
8464 if (xtensa_num_operands (isa, opcode) < insn->ntok)
8466 as_bad (_("too many operands"));
8473 /* Load an instruction from its encoded form. */
8476 tinsn_from_chars (t_insn, f)
8480 static xtensa_insnbuf insnbuf = NULL;
8482 xtensa_opcode opcode;
8483 xtensa_isa isa = xtensa_default_isa;
8486 insnbuf = xtensa_insnbuf_alloc (isa);
8488 xtensa_insnbuf_from_chars (isa, insnbuf, f);
8489 opcode = xtensa_decode_insn (isa, insnbuf);
8491 /* Find the immed. */
8492 tinsn_init (t_insn);
8493 t_insn->insn_type = ITYPE_INSN;
8494 t_insn->is_specific_opcode = FALSE; /* Must not be specific. */
8495 t_insn->opcode = opcode;
8496 t_insn->ntok = xtensa_num_operands (isa, opcode);
8497 for (i = 0; i < t_insn->ntok; i++)
8499 set_expr_const (&t_insn->tok[i],
8500 xtensa_insnbuf_get_operand (insnbuf, opcode, i));
8505 /* Read the value of the relaxable immed from the fr_symbol and fr_offset. */
8508 tinsn_immed_from_frag (t_insn, fragP)
8512 xtensa_opcode opcode = t_insn->opcode;
8515 if (fragP->fr_symbol)
8517 opnum = get_relaxable_immed (opcode);
8518 set_expr_symbol_offset (&t_insn->tok[opnum],
8519 fragP->fr_symbol, fragP->fr_offset);
8525 get_num_stack_text_bytes (istack)
8531 for (i = 0; i < istack->ninsn; i++)
8533 TInsn *t_insn = &istack->insn[i];
8534 if (t_insn->insn_type == ITYPE_INSN)
8535 text_bytes += xg_get_insn_size (t_insn);
8542 get_num_stack_literal_bytes (istack)
8548 for (i = 0; i < istack->ninsn; i++)
8550 TInsn *t_insn = &istack->insn[i];
8552 if (t_insn->insn_type == ITYPE_LITERAL && t_insn->ntok == 1)
8559 /* Expression utilities. */
8561 /* Return true if the expression is an integer constant. */
8565 const expressionS *s;
8567 return (s->X_op == O_constant);
8571 /* Get the expression constant.
8572 Calling this is illegal if expr_is_const () returns true. */
8576 const expressionS *s;
8578 assert (expr_is_const (s));
8579 return s->X_add_number;
8583 /* Set the expression to a constant value. */
8586 set_expr_const (s, val)
8590 s->X_op = O_constant;
8591 s->X_add_number = val;
8592 s->X_add_symbol = NULL;
8593 s->X_op_symbol = NULL;
8597 /* Set the expression to a symbol + constant offset. */
8600 set_expr_symbol_offset (s, sym, offset)
8606 s->X_add_symbol = sym;
8607 s->X_op_symbol = NULL; /* unused */
8608 s->X_add_number = offset;
8613 expr_is_equal (s1, s2)
8617 if (s1->X_op != s2->X_op)
8619 if (s1->X_add_symbol != s2->X_add_symbol)
8621 if (s1->X_op_symbol != s2->X_op_symbol)
8623 if (s1->X_add_number != s2->X_add_number)
8630 copy_expr (dst, src)
8632 const expressionS *src;
8634 memcpy (dst, src, sizeof (expressionS));
8638 /* Support for Tensilica's "--rename-section" option. */
8640 #ifdef XTENSA_SECTION_RENAME
8642 struct rename_section_struct
8646 struct rename_section_struct *next;
8649 static struct rename_section_struct *section_rename;
8652 /* Parse the string oldname=new_name:oldname2=new_name2
8653 and call add_section_rename. */
8656 build_section_rename (arg)
8659 char *this_arg = NULL;
8660 char *next_arg = NULL;
8662 for (this_arg = strdup (arg); this_arg != NULL; this_arg = next_arg)
8666 next_arg = strchr (this_arg, ':');
8674 char *old_name = this_arg;
8675 char *new_name = strchr (this_arg, '=');
8677 if (*old_name == '\0')
8679 as_warn (_("ignoring extra '-rename-section' delimiter ':'"));
8682 if (!new_name || new_name[1] == '\0')
8684 as_warn (_("ignoring invalid '-rename-section' "
8685 "specification: '%s'"), old_name);
8690 add_section_rename (old_name, new_name);
8697 add_section_rename (old_name, new_name)
8701 struct rename_section_struct *r = section_rename;
8703 /* Check for invalid section renaming. */
8704 for (r = section_rename; r != NULL; r = r->next)
8706 if (strcmp (r->old_name, old_name) == 0)
8707 as_bad (_("section %s renamed multiple times"), old_name);
8708 if (strcmp (r->new_name, new_name) == 0)
8709 as_bad (_("multiple sections remapped to output section %s"),
8714 r = (struct rename_section_struct *)
8715 xmalloc (sizeof (struct rename_section_struct));
8716 r->old_name = strdup (old_name);
8717 r->new_name = strdup (new_name);
8718 r->next = section_rename;
8724 xtensa_section_rename (name)
8727 struct rename_section_struct *r = section_rename;
8729 for (r = section_rename; r != NULL; r = r->next)
8730 if (strcmp (r->old_name, name) == 0)
8736 #endif /* XTENSA_SECTION_RENAME */