Add Xtensa port
[deliverable/binutils-gdb.git] / gas / config / tc-xtensa.c
1 /* tc-xtensa.c -- Assemble Xtensa instructions.
2 Copyright 2003 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 MA 02111-1307, USA. */
20
21 #include <string.h>
22 #include "as.h"
23 #include "sb.h"
24 #include "safe-ctype.h"
25 #include "tc-xtensa.h"
26 #include "frags.h"
27 #include "subsegs.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"
33
34 #ifndef uint32
35 #define uint32 unsigned int
36 #endif
37 #ifndef int32
38 #define int32 signed int
39 #endif
40
41 /* Notes:
42
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
49
50 Naming conventions (used somewhat inconsistently):
51 The xtensa_ functions are exported
52 The xg_ functions are internal
53
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. */
64
65
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";
72
73
74 /* Flag to indicate whether the hardware supports the density option.
75 If not, enabling density instructions (via directives or --density flag)
76 is illegal. */
77
78 #if STATIC_LIBISA
79 bfd_boolean density_supported = XCHAL_HAVE_DENSITY;
80 #else
81 bfd_boolean density_supported = TRUE;
82 #endif
83
84 #define XTENSA_FETCH_WIDTH 4
85
86 /* Flags for properties of the last instruction in a segment. */
87 #define FLAG_IS_A0_WRITER 0x1
88 #define FLAG_IS_BAD_LOOPEND 0x2
89
90
91 /* We define a special segment names ".literal" to place literals
92 into. The .fini and .init sections are special because they
93 contain code that is moved together by the linker. We give them
94 their own special .fini.literal and .init.literal sections. */
95
96 #define LITERAL_SECTION_NAME xtensa_section_rename (".literal")
97 #define 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")
101
102
103 /* This type is used for the directive_stack to keep track of the
104 state of the literal collection pools. */
105
106 typedef struct lit_state_struct
107 {
108 const char *lit_seg_name;
109 const char *init_lit_seg_name;
110 const char *fini_lit_seg_name;
111 segT lit_seg;
112 segT init_lit_seg;
113 segT fini_lit_seg;
114 } lit_state;
115
116 static lit_state default_lit_sections;
117
118
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. */
122
123 typedef struct seg_list_struct
124 {
125 struct seg_list_struct *next;
126 segT seg;
127 } seg_list;
128
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;
135
136
137 /* Global flag to indicate when we are emitting literals. */
138 int generating_literals = 0;
139
140
141 /* Structure for saving the current state before emitting literals. */
142 typedef struct emit_state_struct
143 {
144 const char *name;
145 segT now_seg;
146 subsegT now_subseg;
147 int generating_literals;
148 } emit_state;
149
150
151 /* Directives. */
152
153 typedef enum
154 {
155 directive_none = 0,
156 directive_literal,
157 directive_density,
158 directive_generics,
159 directive_relax,
160 directive_freeregs,
161 directive_longcalls,
162 directive_literal_prefix
163 } directiveE;
164
165 typedef struct
166 {
167 const char *name;
168 bfd_boolean can_be_negated;
169 } directive_infoS;
170
171 const directive_infoS directive_info[] =
172 {
173 {"none", FALSE},
174 {"literal", FALSE},
175 {"density", TRUE},
176 {"generics", TRUE},
177 {"relax", TRUE},
178 {"freeregs", FALSE},
179 {"longcalls", TRUE},
180 {"literal_prefix", FALSE}
181 };
182
183 bfd_boolean directive_state[] =
184 {
185 FALSE, /* none */
186 FALSE, /* literal */
187 #if STATIC_LIBISA && !XCHAL_HAVE_DENSITY
188 FALSE, /* density */
189 #else
190 TRUE, /* density */
191 #endif
192 TRUE, /* generics */
193 TRUE, /* relax */
194 FALSE, /* freeregs */
195 FALSE, /* longcalls */
196 FALSE /* literal_prefix */
197 };
198
199
200 enum xtensa_relax_statesE
201 {
202 RELAX_ALIGN_NEXT_OPCODE,
203 /* Use the first opcode of the next fragment to determine the
204 alignment requirements. This is ONLY used for LOOPS
205 currently. */
206
207 RELAX_DESIRE_ALIGN_IF_TARGET,
208 /* These are placed in front of labels. They will all be converted
209 to RELAX_DESIRE_ALIGN / RELAX_LOOP_END or rs_fill of 0 before
210 relaxation begins. */
211
212 RELAX_ADD_NOP_IF_A0_B_RETW,
213 /* These are placed in front of conditional branches. It will be
214 turned into a NOP (using a1) if the branch is immediately
215 followed by a RETW or RETW.N. Otherwise it will be turned into
216 an rs_fill of 0 before relaxation begins. */
217
218 RELAX_ADD_NOP_IF_PRE_LOOP_END,
219 /* These are placed after JX instructions. It will be turned into a
220 NOP if there is one instruction before a loop end label.
221 Otherwise it will be turned into an rs_fill of 0 before
222 relaxation begins. This is used to avoid a hardware TIE
223 interlock issue prior to T1040. */
224
225 RELAX_ADD_NOP_IF_SHORT_LOOP,
226 /* These are placed after LOOP instructions. It will be turned into
227 a NOP when: (1) there are less than 3 instructions in the loop;
228 we place 2 of these in a row to add up to 2 NOPS in short loops;
229 or (2) The instructions in the loop do not include a branch or
230 jump. Otherwise it will be turned into an rs_fill of 0 before
231 relaxation begins. This is used to avoid hardware bug
232 PR3830. */
233
234 RELAX_ADD_NOP_IF_CLOSE_LOOP_END,
235 /* These are placed after LOOP instructions. It will be turned into
236 a NOP if there are less than 12 bytes to the end of some other
237 loop's end. Otherwise it will be turned into an rs_fill of 0
238 before relaxation begins. This is used to avoid hardware bug
239 PR3830. */
240
241 RELAX_DESIRE_ALIGN,
242 /* The next fragment like its first instruction to NOT cross a
243 4-byte boundary. */
244
245 RELAX_LOOP_END,
246 /* This will be turned into a NOP or NOP.N if the previous
247 instruction is expanded to negate a loop. */
248
249 RELAX_LOOP_END_ADD_NOP,
250 /* When the code density option is available, this will generate a
251 NOP.N marked RELAX_NARROW. Otherwise, it will create an rs_fill
252 fragment with a NOP in it. */
253
254 RELAX_LITERAL,
255 /* Another fragment could generate an expansion here but has not yet. */
256
257 RELAX_LITERAL_NR,
258 /* Expansion has been generated by an instruction that generates a
259 literal. However, the stretch has NOT been reported yet in this
260 fragment. */
261
262 RELAX_LITERAL_FINAL,
263 /* Expansion has been generated by an instruction that generates a
264 literal. */
265
266 RELAX_LITERAL_POOL_BEGIN,
267 RELAX_LITERAL_POOL_END,
268 /* Technically these are not relaxations at all, but mark a location
269 to store literals later. Note that fr_var stores the frchain for
270 BEGIN frags and fr_var stores now_seg for END frags. */
271
272 RELAX_NARROW,
273 /* The last instruction in this fragment (at->fr_opcode) can be
274 freely replaced with a single wider instruction if a future
275 alignment desires or needs it. */
276
277 RELAX_IMMED,
278 /* The last instruction in this fragment (at->fr_opcode) contains
279 the value defined by fr_symbol (fr_offset = 0). If the value
280 does not fit, use the specified expansion. This is similar to
281 "NARROW", except that these may not be expanded in order to align
282 code. */
283
284 RELAX_IMMED_STEP1,
285 /* The last instruction in this fragment (at->fr_opcode) contains a
286 literal. It has already been expanded at least 1 step. */
287
288 RELAX_IMMED_STEP2
289 /* The last instruction in this fragment (at->fr_opcode) contains a
290 literal. It has already been expanded at least 2 steps. */
291 };
292
293 /* This is used as a stopper to bound the number of steps that
294 can be taken. */
295 #define RELAX_IMMED_MAXSTEPS (RELAX_IMMED_STEP2 - RELAX_IMMED)
296
297
298 typedef bfd_boolean (*frag_predicate) (const fragS *);
299
300
301 /* Directive functions. */
302
303 static bfd_boolean use_generics
304 PARAMS ((void));
305 static bfd_boolean use_longcalls
306 PARAMS ((void));
307 static bfd_boolean code_density_available
308 PARAMS ((void));
309 static bfd_boolean can_relax
310 PARAMS ((void));
311 static void directive_push
312 PARAMS ((directiveE, bfd_boolean, const void *));
313 static void directive_pop
314 PARAMS ((directiveE *, bfd_boolean *, const char **,
315 unsigned int *, const void **));
316 static void directive_balance
317 PARAMS ((void));
318 static bfd_boolean inside_directive
319 PARAMS ((directiveE));
320 static void get_directive
321 PARAMS ((directiveE *, bfd_boolean *));
322 static void xtensa_begin_directive
323 PARAMS ((int));
324 static void xtensa_end_directive
325 PARAMS ((int));
326 static void xtensa_literal_prefix
327 PARAMS ((char const *, int));
328 static void xtensa_literal_position
329 PARAMS ((int));
330 static void xtensa_literal_pseudo
331 PARAMS ((int));
332
333 /* Parsing and Idiom Translation Functions. */
334
335 static const char *expression_end
336 PARAMS ((const char *));
337 static unsigned tc_get_register
338 PARAMS ((const char *));
339 static void expression_maybe_register
340 PARAMS ((xtensa_operand, expressionS *));
341 static int tokenize_arguments
342 PARAMS ((char **, char *));
343 static bfd_boolean parse_arguments
344 PARAMS ((TInsn *, int, char **));
345 static int xg_translate_idioms
346 PARAMS ((char **, int *, char **));
347 static int xg_translate_sysreg_op
348 PARAMS ((char **, int *, char **));
349 static void xg_reverse_shift_count
350 PARAMS ((char **));
351 static int xg_arg_is_constant
352 PARAMS ((char *, offsetT *));
353 static void xg_replace_opname
354 PARAMS ((char **, char *));
355 static int xg_check_num_args
356 PARAMS ((int *, int, char *, char **));
357
358 /* Functions for dealing with the Xtensa ISA. */
359
360 static bfd_boolean operand_is_immed
361 PARAMS ((xtensa_operand));
362 static bfd_boolean operand_is_pcrel_label
363 PARAMS ((xtensa_operand));
364 static int get_relaxable_immed
365 PARAMS ((xtensa_opcode));
366 static xtensa_opcode get_opcode_from_buf
367 PARAMS ((const char *));
368 static bfd_boolean is_direct_call_opcode
369 PARAMS ((xtensa_opcode));
370 static bfd_boolean is_call_opcode
371 PARAMS ((xtensa_opcode));
372 static bfd_boolean is_entry_opcode
373 PARAMS ((xtensa_opcode));
374 static bfd_boolean is_loop_opcode
375 PARAMS ((xtensa_opcode));
376 static bfd_boolean is_the_loop_opcode
377 PARAMS ((xtensa_opcode));
378 static bfd_boolean is_jx_opcode
379 PARAMS ((xtensa_opcode));
380 static bfd_boolean is_windowed_return_opcode
381 PARAMS ((xtensa_opcode));
382 static bfd_boolean is_conditional_branch_opcode
383 PARAMS ((xtensa_opcode));
384 static bfd_boolean is_branch_or_jump_opcode
385 PARAMS ((xtensa_opcode));
386 static bfd_reloc_code_real_type opnum_to_reloc
387 PARAMS ((int));
388 static int reloc_to_opnum
389 PARAMS ((bfd_reloc_code_real_type));
390 static void xtensa_insnbuf_set_operand
391 PARAMS ((xtensa_insnbuf, xtensa_opcode, xtensa_operand, int32,
392 const char *, unsigned int));
393 static uint32 xtensa_insnbuf_get_operand
394 PARAMS ((xtensa_insnbuf, xtensa_opcode, int));
395 static void xtensa_insnbuf_set_immediate_field
396 PARAMS ((xtensa_opcode, xtensa_insnbuf, int32, const char *,
397 unsigned int));
398 static bfd_boolean is_negatable_branch
399 PARAMS ((TInsn *));
400
401 /* Functions for Internal Lists of Symbols. */
402 static void xtensa_define_label
403 PARAMS ((symbolS *));
404 static void add_target_symbol
405 PARAMS ((symbolS *, bfd_boolean));
406 static symbolS *xtensa_find_label
407 PARAMS ((fragS *, offsetT, bfd_boolean));
408 static void map_over_defined_symbols
409 PARAMS ((void (*fn) (symbolS *)));
410 static bfd_boolean is_loop_target_label
411 PARAMS ((symbolS *));
412 static void xtensa_mark_target_fragments
413 PARAMS ((void));
414
415 /* Various Other Internal Functions. */
416
417 static bfd_boolean is_unique_insn_expansion
418 PARAMS ((TransitionRule *));
419 static int xg_get_insn_size
420 PARAMS ((TInsn *));
421 static int xg_get_build_instr_size
422 PARAMS ((BuildInstr *));
423 static bfd_boolean xg_is_narrow_insn
424 PARAMS ((TInsn *));
425 static bfd_boolean xg_is_single_relaxable_insn
426 PARAMS ((TInsn *));
427 static int xg_get_max_narrow_insn_size
428 PARAMS ((xtensa_opcode));
429 static int xg_get_max_insn_widen_size
430 PARAMS ((xtensa_opcode));
431 static int xg_get_max_insn_widen_literal_size
432 PARAMS ((xtensa_opcode));
433 static bfd_boolean xg_is_relaxable_insn
434 PARAMS ((TInsn *, int));
435 static symbolS *get_special_literal_symbol
436 PARAMS ((void));
437 static symbolS *get_special_label_symbol
438 PARAMS ((void));
439 static bfd_boolean xg_build_to_insn
440 PARAMS ((TInsn *, TInsn *, BuildInstr *));
441 static bfd_boolean xg_build_to_stack
442 PARAMS ((IStack *, TInsn *, BuildInstr *));
443 static bfd_boolean xg_expand_to_stack
444 PARAMS ((IStack *, TInsn *, int));
445 static bfd_boolean xg_expand_narrow
446 PARAMS ((TInsn *, TInsn *));
447 static bfd_boolean xg_immeds_fit
448 PARAMS ((const TInsn *));
449 static bfd_boolean xg_symbolic_immeds_fit
450 PARAMS ((const TInsn *, segT, fragS *, offsetT, long));
451 static bfd_boolean xg_check_operand
452 PARAMS ((int32, xtensa_operand));
453 static int is_dnrange
454 PARAMS ((fragS *, symbolS *, long));
455 static int xg_assembly_relax
456 PARAMS ((IStack *, TInsn *, segT, fragS *, offsetT, int, long));
457 static void xg_force_frag_space
458 PARAMS ((int));
459 static void xg_finish_frag
460 PARAMS ((char *, enum xtensa_relax_statesE, int, bfd_boolean));
461 static bfd_boolean is_branch_jmp_to_next
462 PARAMS ((TInsn *, fragS *));
463 static void xg_add_branch_and_loop_targets
464 PARAMS ((TInsn *));
465 static bfd_boolean xg_instruction_matches_rule
466 PARAMS ((TInsn *, TransitionRule *));
467 static TransitionRule *xg_instruction_match
468 PARAMS ((TInsn *));
469 static bfd_boolean xg_build_token_insn
470 PARAMS ((BuildInstr *, TInsn *, TInsn *));
471 static bfd_boolean xg_simplify_insn
472 PARAMS ((TInsn *, TInsn *));
473 static bfd_boolean xg_expand_assembly_insn
474 PARAMS ((IStack *, TInsn *));
475 static symbolS *xg_assemble_literal
476 PARAMS ((TInsn *));
477 static void xg_assemble_literal_space
478 PARAMS ((int));
479 static symbolS *xtensa_create_literal_symbol
480 PARAMS ((segT, fragS *));
481 static symbolS *xtensa_create_local_symbol
482 PARAMS ((bfd *, const char *, segT, valueT, fragS *));
483 static bfd_boolean get_is_linkonce_section
484 PARAMS ((bfd *, segT));
485 static bfd_boolean xg_emit_insn
486 PARAMS ((TInsn *, bfd_boolean));
487 static bfd_boolean xg_emit_insn_to_buf
488 PARAMS ((TInsn *, char *, fragS *, offsetT, bfd_boolean));
489 static bfd_boolean xg_add_opcode_fix
490 PARAMS ((xtensa_opcode, int, expressionS *, fragS *, offsetT));
491 static void xg_resolve_literals
492 PARAMS ((TInsn *, symbolS *));
493 static void xg_resolve_labels
494 PARAMS ((TInsn *, symbolS *));
495 static void xg_assemble_tokens
496 PARAMS ((TInsn *));
497 static bfd_boolean is_register_writer
498 PARAMS ((const TInsn *, const char *, int));
499 static bfd_boolean is_bad_loopend_opcode
500 PARAMS ((const TInsn *));
501 static bfd_boolean is_unaligned_label
502 PARAMS ((symbolS *));
503 static fragS *next_non_empty_frag
504 PARAMS ((const fragS *));
505 static xtensa_opcode next_frag_opcode
506 PARAMS ((const fragS *));
507 static void update_next_frag_nop_state
508 PARAMS ((fragS *));
509 static bfd_boolean next_frag_is_branch_target
510 PARAMS ((const fragS *));
511 static bfd_boolean next_frag_is_loop_target
512 PARAMS ((const fragS *));
513 static addressT next_frag_pre_opcode_bytes
514 PARAMS ((const fragS *));
515 static bfd_boolean is_next_frag_target
516 PARAMS ((const fragS *, const fragS *));
517 static void xtensa_mark_literal_pool_location
518 PARAMS ((bfd_boolean));
519 static void xtensa_move_labels
520 PARAMS ((fragS *, valueT, fragS *, valueT));
521 static void assemble_nop
522 PARAMS ((size_t, char *));
523 static addressT get_expanded_loop_offset
524 PARAMS ((xtensa_opcode));
525 static fragS *get_literal_pool_location
526 PARAMS ((segT));
527 static void set_literal_pool_location
528 PARAMS ((segT, fragS *));
529
530 /* Helpers for xtensa_end(). */
531
532 static void xtensa_cleanup_align_frags
533 PARAMS ((void));
534 static void xtensa_fix_target_frags
535 PARAMS ((void));
536 static bfd_boolean frag_can_negate_branch
537 PARAMS ((fragS *));
538 static void xtensa_fix_a0_b_retw_frags
539 PARAMS ((void));
540 static bfd_boolean next_instrs_are_b_retw
541 PARAMS ((fragS *));
542 static void xtensa_fix_b_j_loop_end_frags
543 PARAMS ((void));
544 static bfd_boolean next_instr_is_loop_end
545 PARAMS ((fragS *));
546 static void xtensa_fix_close_loop_end_frags
547 PARAMS ((void));
548 static size_t min_bytes_to_other_loop_end
549 PARAMS ((fragS *, fragS *, offsetT, size_t));
550 static size_t unrelaxed_frag_min_size
551 PARAMS ((fragS *));
552 static void xtensa_fix_short_loop_frags
553 PARAMS ((void));
554 static size_t count_insns_to_loop_end
555 PARAMS ((fragS *, bfd_boolean, size_t));
556 static size_t unrelaxed_frag_min_insn_count
557 PARAMS ((fragS *));
558 static bfd_boolean branch_before_loop_end
559 PARAMS ((fragS *));
560 static bfd_boolean unrelaxed_frag_has_b_j
561 PARAMS ((fragS *));
562 static void xtensa_sanity_check
563 PARAMS ((void));
564 static bfd_boolean is_empty_loop
565 PARAMS ((const TInsn *, fragS *));
566 static bfd_boolean is_local_forward_loop
567 PARAMS ((const TInsn *, fragS *));
568
569 /* Alignment Functions. */
570
571 static size_t get_text_align_power
572 PARAMS ((int));
573 static addressT get_text_align_max_fill_size
574 PARAMS ((int, bfd_boolean, bfd_boolean));
575 static addressT get_text_align_fill_size
576 PARAMS ((addressT, int, int, bfd_boolean, bfd_boolean));
577 static size_t get_text_align_nop_count
578 PARAMS ((size_t, bfd_boolean));
579 static size_t get_text_align_nth_nop_size
580 PARAMS ((size_t, size_t, bfd_boolean));
581 static addressT get_noop_aligned_address
582 PARAMS ((fragS *, addressT));
583 static addressT get_widen_aligned_address
584 PARAMS ((fragS *, addressT));
585
586 /* Helpers for xtensa_relax_frag(). */
587
588 static long relax_frag_text_align
589 PARAMS ((fragS *, long));
590 static long relax_frag_add_nop
591 PARAMS ((fragS *));
592 static long relax_frag_narrow
593 PARAMS ((fragS *, long));
594 static bfd_boolean future_alignment_required
595 PARAMS ((fragS *, long));
596 static long relax_frag_immed
597 PARAMS ((segT, fragS *, long, int, int *));
598
599 /* Helpers for md_convert_frag(). */
600
601 static void convert_frag_align_next_opcode
602 PARAMS ((fragS *));
603 static void convert_frag_narrow
604 PARAMS ((fragS *));
605 static void convert_frag_immed
606 PARAMS ((segT, fragS *, int));
607 static fixS *fix_new_exp_in_seg
608 PARAMS ((segT, subsegT, fragS *, int, int, expressionS *, int,
609 bfd_reloc_code_real_type));
610 static void convert_frag_immed_finish_loop
611 PARAMS ((segT, fragS *, TInsn *));
612 static offsetT get_expression_value
613 PARAMS ((segT, expressionS *));
614
615 /* Flags for the Last Instruction in Each Subsegment. */
616
617 static unsigned get_last_insn_flags
618 PARAMS ((segT, subsegT));
619 static void set_last_insn_flags
620 PARAMS ((segT, subsegT, unsigned, bfd_boolean));
621
622 /* Segment list functions. */
623
624 static void xtensa_remove_section
625 PARAMS ((segT));
626 static void xtensa_insert_section
627 PARAMS ((segT, segT));
628 static void xtensa_move_seg_list_to_beginning
629 PARAMS ((seg_list *));
630 static void xtensa_move_literals
631 PARAMS ((void));
632 static void xtensa_move_frag_symbol
633 PARAMS ((symbolS *));
634 static void xtensa_move_frag_symbols
635 PARAMS ((void));
636 static void xtensa_reorder_seg_list
637 PARAMS ((seg_list *, segT));
638 static void xtensa_reorder_segments
639 PARAMS ((void));
640 static segT get_last_sec
641 PARAMS ((void));
642 static void xtensa_switch_to_literal_fragment
643 PARAMS ((emit_state *));
644 static void xtensa_switch_section_emit_state
645 PARAMS ((emit_state *, segT, subsegT));
646 static void xtensa_restore_emit_state
647 PARAMS ((emit_state *));
648 static void cache_literal_section
649 PARAMS ((seg_list *, const char *, segT *));
650 static segT retrieve_literal_seg
651 PARAMS ((seg_list *, const char *));
652 static segT seg_present
653 PARAMS ((const char *));
654 static void add_seg_list
655 PARAMS ((seg_list *, segT));
656
657 /* Property Table (e.g., ".xt.insn" and ".xt.lit") Functions. */
658
659 static void xtensa_create_property_segments
660 PARAMS ((frag_predicate, const char *, xt_section_type));
661 static segment_info_type *retrieve_segment_info
662 PARAMS ((segT));
663 static segT retrieve_xtensa_section
664 PARAMS ((char *));
665 static bfd_boolean section_has_property
666 PARAMS ((segT sec, frag_predicate));
667 static void add_xt_block_frags
668 PARAMS ((segT, segT, xtensa_block_info **, frag_predicate));
669 static bfd_boolean get_frag_is_literal
670 PARAMS ((const fragS *));
671 static bfd_boolean get_frag_is_insn
672 PARAMS ((const fragS *));
673
674 /* Import from elf32-xtensa.c in BFD library. */
675 extern char *xtensa_get_property_section_name
676 PARAMS ((bfd *, asection *, const char *));
677
678 /* TInsn and IStack functions. */
679 static bfd_boolean tinsn_has_symbolic_operands
680 PARAMS ((const TInsn *));
681 static bfd_boolean tinsn_has_invalid_symbolic_operands
682 PARAMS ((const TInsn *));
683 static bfd_boolean tinsn_has_complex_operands
684 PARAMS ((const TInsn *));
685 static bfd_boolean tinsn_to_insnbuf
686 PARAMS ((TInsn *, xtensa_insnbuf));
687 static bfd_boolean tinsn_check_arguments
688 PARAMS ((const TInsn *));
689 static void tinsn_from_chars
690 PARAMS ((TInsn *, char *));
691 static void tinsn_immed_from_frag
692 PARAMS ((TInsn *, fragS *));
693 static int get_num_stack_text_bytes
694 PARAMS ((IStack *));
695 static int get_num_stack_literal_bytes
696 PARAMS ((IStack *));
697
698 /* Expression Utilities. */
699 bfd_boolean expr_is_const
700 PARAMS ((const expressionS *));
701 offsetT get_expr_const
702 PARAMS ((const expressionS *));
703 void set_expr_const
704 PARAMS ((expressionS *, offsetT));
705 void set_expr_symbol_offset
706 PARAMS ((expressionS *, symbolS *, offsetT));
707 bfd_boolean expr_is_equal
708 PARAMS ((expressionS *, expressionS *));
709 static void copy_expr
710 PARAMS ((expressionS *, const expressionS *));
711
712 #ifdef XTENSA_SECTION_RENAME
713 static void build_section_rename
714 PARAMS ((const char *));
715 static void add_section_rename
716 PARAMS ((char *, char *));
717 #endif
718
719 #ifdef XTENSA_COMBINE_LITERALS
720 static void find_lit_sym_translation
721 PARAMS ((expressionS *));
722 static void add_lit_sym_translation
723 PARAMS ((char *, offsetT, symbolS *));
724 #endif
725
726
727 /* ISA imported from bfd. */
728 extern xtensa_isa xtensa_default_isa;
729
730 extern int target_big_endian;
731
732 static xtensa_opcode xtensa_addi_opcode;
733 static xtensa_opcode xtensa_addmi_opcode;
734 static xtensa_opcode xtensa_call0_opcode;
735 static xtensa_opcode xtensa_call4_opcode;
736 static xtensa_opcode xtensa_call8_opcode;
737 static xtensa_opcode xtensa_call12_opcode;
738 static xtensa_opcode xtensa_callx0_opcode;
739 static xtensa_opcode xtensa_callx4_opcode;
740 static xtensa_opcode xtensa_callx8_opcode;
741 static xtensa_opcode xtensa_callx12_opcode;
742 static xtensa_opcode xtensa_entry_opcode;
743 static xtensa_opcode xtensa_isync_opcode;
744 static xtensa_opcode xtensa_j_opcode;
745 static xtensa_opcode xtensa_jx_opcode;
746 static xtensa_opcode xtensa_loop_opcode;
747 static xtensa_opcode xtensa_loopnez_opcode;
748 static xtensa_opcode xtensa_loopgtz_opcode;
749 static xtensa_opcode xtensa_nop_n_opcode;
750 static xtensa_opcode xtensa_or_opcode;
751 static xtensa_opcode xtensa_ret_opcode;
752 static xtensa_opcode xtensa_ret_n_opcode;
753 static xtensa_opcode xtensa_retw_opcode;
754 static xtensa_opcode xtensa_retw_n_opcode;
755 static xtensa_opcode xtensa_rsr_opcode;
756 static xtensa_opcode xtensa_waiti_opcode;
757
758 \f
759 /* Command-line Options. */
760
761 bfd_boolean use_literal_section = TRUE;
762 static bfd_boolean align_targets = TRUE;
763 static bfd_boolean align_only_targets = FALSE;
764 static bfd_boolean software_a0_b_retw_interlock = TRUE;
765 static bfd_boolean has_a0_b_retw = FALSE;
766 static bfd_boolean workaround_a0_b_retw = TRUE;
767
768 static bfd_boolean software_avoid_b_j_loop_end = TRUE;
769 static bfd_boolean workaround_b_j_loop_end = TRUE;
770 static bfd_boolean maybe_has_b_j_loop_end = FALSE;
771
772 static bfd_boolean software_avoid_short_loop = TRUE;
773 static bfd_boolean workaround_short_loop = TRUE;
774 static bfd_boolean maybe_has_short_loop = FALSE;
775
776 static bfd_boolean software_avoid_close_loop_end = TRUE;
777 static bfd_boolean workaround_close_loop_end = TRUE;
778 static bfd_boolean maybe_has_close_loop_end = FALSE;
779
780 /* When avoid_short_loops is true, all loops with early exits must
781 have at least 3 instructions. avoid_all_short_loops is a modifier
782 to the avoid_short_loop flag. In addition to the avoid_short_loop
783 actions, all straightline loopgtz and loopnez must have at least 3
784 instructions. */
785
786 static bfd_boolean software_avoid_all_short_loops = TRUE;
787 static bfd_boolean workaround_all_short_loops = TRUE;
788
789 /* This is on a per-instruction basis. */
790 static bfd_boolean specific_opcode = FALSE;
791
792 enum
793 {
794 option_density = OPTION_MD_BASE,
795 option_no_density,
796
797 option_relax,
798 option_no_relax,
799
800 option_generics,
801 option_no_generics,
802
803 option_text_section_literals,
804 option_no_text_section_literals,
805
806 option_align_targets,
807 option_no_align_targets,
808
809 option_align_only_targets,
810 option_no_align_only_targets,
811
812 option_longcalls,
813 option_no_longcalls,
814
815 option_workaround_a0_b_retw,
816 option_no_workaround_a0_b_retw,
817
818 option_workaround_b_j_loop_end,
819 option_no_workaround_b_j_loop_end,
820
821 option_workaround_short_loop,
822 option_no_workaround_short_loop,
823
824 option_workaround_all_short_loops,
825 option_no_workaround_all_short_loops,
826
827 option_workaround_close_loop_end,
828 option_no_workaround_close_loop_end,
829
830 option_no_workarounds,
831
832 #ifdef XTENSA_SECTION_RENAME
833 option_literal_section_name,
834 option_text_section_name,
835 option_data_section_name,
836 option_bss_section_name,
837 option_rename_section_name,
838 #endif
839
840 option_eb,
841 option_el
842 };
843
844 const char *md_shortopts = "";
845
846 struct option md_longopts[] =
847 {
848 {"density", no_argument, NULL, option_density},
849 {"no-density", no_argument, NULL, option_no_density},
850 /* At least as early as alameda, --[no-]relax didn't work as
851 documented, so as of albany, --[no-]relax is equivalent to
852 --[no-]generics. Both of these will be deprecated in
853 BearValley. */
854 {"relax", no_argument, NULL, option_generics},
855 {"no-relax", no_argument, NULL, option_no_generics},
856 {"generics", no_argument, NULL, option_generics},
857 {"no-generics", no_argument, NULL, option_no_generics},
858 {"text-section-literals", no_argument, NULL, option_text_section_literals},
859 {"no-text-section-literals", no_argument, NULL,
860 option_no_text_section_literals},
861 /* This option was changed from -align-target to -target-align
862 because it conflicted with the "-al" option. */
863 {"target-align", no_argument, NULL, option_align_targets},
864 {"no-target-align", no_argument, NULL,
865 option_no_align_targets},
866 #if 0
867 /* This option should do a better job aligning targets because
868 it will only attempt to align targets that are the target of a
869 branch. */
870 { "target-align-only", no_argument, NULL, option_align_only_targets },
871 { "no-target-align-only", no_argument, NULL, option_no_align_only_targets },
872 #endif /* 0 */
873 {"longcalls", no_argument, NULL, option_longcalls},
874 {"no-longcalls", no_argument, NULL, option_no_longcalls},
875
876 {"no-workaround-a0-b-retw", no_argument, NULL,
877 option_no_workaround_a0_b_retw},
878 {"workaround-a0-b-retw", no_argument, NULL, option_workaround_a0_b_retw},
879
880 {"no-workaround-b-j-loop-end", no_argument, NULL,
881 option_no_workaround_b_j_loop_end},
882 {"workaround-b-j-loop-end", no_argument, NULL,
883 option_workaround_b_j_loop_end},
884
885 {"no-workaround-short-loops", no_argument, NULL,
886 option_no_workaround_short_loop},
887 {"workaround-short-loops", no_argument, NULL, option_workaround_short_loop},
888
889 {"no-workaround-all-short-loops", no_argument, NULL,
890 option_no_workaround_all_short_loops},
891 {"workaround-all-short-loop", no_argument, NULL,
892 option_workaround_all_short_loops},
893
894 {"no-workaround-close-loop-end", no_argument, NULL,
895 option_no_workaround_close_loop_end},
896 {"workaround-close-loop-end", no_argument, NULL,
897 option_workaround_close_loop_end},
898
899 {"no-workarounds", no_argument, NULL, option_no_workarounds},
900
901 #ifdef XTENSA_SECTION_RENAME
902 {"literal-section-name", required_argument, NULL,
903 option_literal_section_name},
904 {"text-section-name", required_argument, NULL,
905 option_text_section_name},
906 {"data-section-name", required_argument, NULL,
907 option_data_section_name},
908 {"rename-section", required_argument, NULL,
909 option_rename_section_name},
910 {"bss-section-name", required_argument, NULL,
911 option_bss_section_name},
912 #endif /* XTENSA_SECTION_RENAME */
913
914 {NULL, no_argument, NULL, 0}
915 };
916
917 size_t md_longopts_size = sizeof md_longopts;
918
919
920 int
921 md_parse_option (c, arg)
922 int c;
923 char *arg;
924 {
925 switch (c)
926 {
927 case option_density:
928 if (!density_supported)
929 {
930 as_bad (_("'--density' option not supported in this Xtensa "
931 "configuration"));
932 return 0;
933 }
934 directive_state[directive_density] = TRUE;
935 return 1;
936 case option_no_density:
937 directive_state[directive_density] = FALSE;
938 return 1;
939 case option_generics:
940 directive_state[directive_generics] = TRUE;
941 return 1;
942 case option_no_generics:
943 directive_state[directive_generics] = FALSE;
944 return 1;
945 case option_longcalls:
946 directive_state[directive_longcalls] = TRUE;
947 return 1;
948 case option_no_longcalls:
949 directive_state[directive_longcalls] = FALSE;
950 return 1;
951 case option_text_section_literals:
952 use_literal_section = FALSE;
953 return 1;
954 case option_no_text_section_literals:
955 use_literal_section = TRUE;
956 return 1;
957 case option_workaround_a0_b_retw:
958 workaround_a0_b_retw = TRUE;
959 software_a0_b_retw_interlock = TRUE;
960 return 1;
961 case option_no_workaround_a0_b_retw:
962 workaround_a0_b_retw = FALSE;
963 software_a0_b_retw_interlock = FALSE;
964 return 1;
965 case option_workaround_b_j_loop_end:
966 workaround_b_j_loop_end = TRUE;
967 software_avoid_b_j_loop_end = TRUE;
968 return 1;
969 case option_no_workaround_b_j_loop_end:
970 workaround_b_j_loop_end = FALSE;
971 software_avoid_b_j_loop_end = FALSE;
972 return 1;
973
974 case option_workaround_short_loop:
975 workaround_short_loop = TRUE;
976 software_avoid_short_loop = TRUE;
977 return 1;
978 case option_no_workaround_short_loop:
979 workaround_short_loop = FALSE;
980 software_avoid_short_loop = FALSE;
981 return 1;
982
983 case option_workaround_all_short_loops:
984 workaround_all_short_loops = TRUE;
985 software_avoid_all_short_loops = TRUE;
986 return 1;
987 case option_no_workaround_all_short_loops:
988 workaround_all_short_loops = FALSE;
989 software_avoid_all_short_loops = FALSE;
990 return 1;
991
992 case option_workaround_close_loop_end:
993 workaround_close_loop_end = TRUE;
994 software_avoid_close_loop_end = TRUE;
995 return 1;
996 case option_no_workaround_close_loop_end:
997 workaround_close_loop_end = FALSE;
998 software_avoid_close_loop_end = FALSE;
999 return 1;
1000
1001 case option_no_workarounds:
1002 workaround_a0_b_retw = FALSE;
1003 software_a0_b_retw_interlock = FALSE;
1004 workaround_b_j_loop_end = FALSE;
1005 software_avoid_b_j_loop_end = FALSE;
1006 workaround_short_loop = FALSE;
1007 software_avoid_short_loop = FALSE;
1008 workaround_all_short_loops = FALSE;
1009 software_avoid_all_short_loops = FALSE;
1010 workaround_close_loop_end = FALSE;
1011 software_avoid_close_loop_end = FALSE;
1012 return 1;
1013
1014 case option_align_targets:
1015 align_targets = TRUE;
1016 return 1;
1017 case option_no_align_targets:
1018 align_targets = FALSE;
1019 return 1;
1020
1021 case option_align_only_targets:
1022 align_only_targets = TRUE;
1023 return 1;
1024 case option_no_align_only_targets:
1025 align_only_targets = FALSE;
1026 return 1;
1027
1028 #ifdef XTENSA_SECTION_RENAME
1029 case option_literal_section_name:
1030 add_section_rename (".literal", arg);
1031 as_warn (_("'--literal-section-name' is deprecated; "
1032 "use '--rename-section .literal=NEWNAME'"));
1033 return 1;
1034
1035 case option_text_section_name:
1036 add_section_rename (".text", arg);
1037 as_warn (_("'--text-section-name' is deprecated; "
1038 "use '--rename-section .text=NEWNAME'"));
1039 return 1;
1040
1041 case option_data_section_name:
1042 add_section_rename (".data", arg);
1043 as_warn (_("'--data-section-name' is deprecated; "
1044 "use '--rename-section .data=NEWNAME'"));
1045 return 1;
1046
1047 case option_bss_section_name:
1048 add_section_rename (".bss", arg);
1049 as_warn (_("'--bss-section-name' is deprecated; "
1050 "use '--rename-section .bss=NEWNAME'"));
1051 return 1;
1052
1053 case option_rename_section_name:
1054 build_section_rename (arg);
1055 return 1;
1056 #endif /* XTENSA_SECTION_RENAME */
1057
1058 case 'Q':
1059 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
1060 should be emitted or not. FIXME: Not implemented. */
1061 return 1;
1062
1063 default:
1064 return 0;
1065 }
1066 }
1067
1068
1069 void
1070 md_show_usage (stream)
1071 FILE *stream;
1072 {
1073 fputs ("\nXtensa options:\n"
1074 "--[no-]density [Do not] emit density instructions\n"
1075 "--[no-]relax [Do not] perform branch relaxation\n"
1076 "--[no-]generics [Do not] transform instructions\n"
1077 "--[no-]longcalls [Do not] emit 32-bit call sequences\n"
1078 "--[no-]target-align [Do not] try to align branch targets\n"
1079 "--[no-]text-section-literals\n"
1080 " [Do not] put literals in the text section\n"
1081 "--no-workarounds Do not use any Xtensa workarounds\n"
1082 #ifdef XTENSA_SECTION_RENAME
1083 "--rename-section old=new(:old1=new1)*\n"
1084 " Rename section 'old' to 'new'\n"
1085 "\nThe following Xtensa options are deprecated\n"
1086 "--literal-section-name Name of literal section (default .literal)\n"
1087 "--text-section-name Name of text section (default .text)\n"
1088 "--data-section-name Name of data section (default .data)\n"
1089 "--bss-section-name Name of bss section (default .bss)\n"
1090 #endif
1091 , stream);
1092 }
1093
1094 \f
1095 /* Directive data and functions. */
1096
1097 typedef struct state_stackS_struct
1098 {
1099 directiveE directive;
1100 bfd_boolean negated;
1101 bfd_boolean old_state;
1102 const char *file;
1103 unsigned int line;
1104 const void *datum;
1105 struct state_stackS_struct *prev;
1106 } state_stackS;
1107
1108 state_stackS *directive_state_stack;
1109
1110 const pseudo_typeS md_pseudo_table[] =
1111 {
1112 {"align", s_align_bytes, 0}, /* Defaulting is invalid (0) */
1113 {"literal_position", xtensa_literal_position, 0},
1114 {"frame", s_ignore, 0}, /* formerly used for STABS debugging */
1115 {"word", cons, 4},
1116 {"begin", xtensa_begin_directive, 0},
1117 {"end", xtensa_end_directive, 0},
1118 {"file", (void (*) PARAMS ((int))) dwarf2_directive_file, 0},
1119 {"loc", dwarf2_directive_loc, 0},
1120 {"literal", xtensa_literal_pseudo, 0},
1121 {NULL, 0, 0},
1122 };
1123
1124
1125 bfd_boolean
1126 use_generics ()
1127 {
1128 return directive_state[directive_generics];
1129 }
1130
1131
1132 bfd_boolean
1133 use_longcalls ()
1134 {
1135 return directive_state[directive_longcalls];
1136 }
1137
1138
1139 bfd_boolean
1140 code_density_available ()
1141 {
1142 return directive_state[directive_density];
1143 }
1144
1145
1146 bfd_boolean
1147 can_relax ()
1148 {
1149 return use_generics ();
1150 }
1151
1152
1153 static void
1154 directive_push (directive, negated, datum)
1155 directiveE directive;
1156 bfd_boolean negated;
1157 const void *datum;
1158 {
1159 char *file;
1160 unsigned int line;
1161 state_stackS *stack = (state_stackS *) xmalloc (sizeof (state_stackS));
1162
1163 as_where (&file, &line);
1164
1165 stack->directive = directive;
1166 stack->negated = negated;
1167 stack->old_state = directive_state[directive];
1168 stack->file = file;
1169 stack->line = line;
1170 stack->datum = datum;
1171 stack->prev = directive_state_stack;
1172 directive_state_stack = stack;
1173
1174 directive_state[directive] = !negated;
1175 }
1176
1177 static void
1178 directive_pop (directive, negated, file, line, datum)
1179 directiveE *directive;
1180 bfd_boolean *negated;
1181 const char **file;
1182 unsigned int *line;
1183 const void **datum;
1184 {
1185 state_stackS *top = directive_state_stack;
1186
1187 if (!directive_state_stack)
1188 {
1189 as_bad (_("unmatched end directive"));
1190 *directive = directive_none;
1191 return;
1192 }
1193
1194 directive_state[directive_state_stack->directive] = top->old_state;
1195 *directive = top->directive;
1196 *negated = top->negated;
1197 *file = top->file;
1198 *line = top->line;
1199 *datum = top->datum;
1200 directive_state_stack = top->prev;
1201 free (top);
1202 }
1203
1204
1205 static void
1206 directive_balance ()
1207 {
1208 while (directive_state_stack)
1209 {
1210 directiveE directive;
1211 bfd_boolean negated;
1212 const char *file;
1213 unsigned int line;
1214 const void *datum;
1215
1216 directive_pop (&directive, &negated, &file, &line, &datum);
1217 as_warn_where ((char *) file, line,
1218 _(".begin directive with no matching .end directive"));
1219 }
1220 }
1221
1222
1223 static bfd_boolean
1224 inside_directive (dir)
1225 directiveE dir;
1226 {
1227 state_stackS *top = directive_state_stack;
1228
1229 while (top && top->directive != dir)
1230 top = top->prev;
1231
1232 return (top != NULL);
1233 }
1234
1235
1236 static void
1237 get_directive (directive, negated)
1238 directiveE *directive;
1239 bfd_boolean *negated;
1240 {
1241 int len;
1242 unsigned i;
1243
1244 if (strncmp (input_line_pointer, "no-", 3) != 0)
1245 *negated = FALSE;
1246 else
1247 {
1248 *negated = TRUE;
1249 input_line_pointer += 3;
1250 }
1251
1252 len = strspn (input_line_pointer,
1253 "abcdefghijklmnopqrstuvwxyz_/0123456789.");
1254
1255 for (i = 0; i < sizeof (directive_info) / sizeof (*directive_info); ++i)
1256 {
1257 if (strncmp (input_line_pointer, directive_info[i].name, len) == 0)
1258 {
1259 input_line_pointer += len;
1260 *directive = (directiveE) i;
1261 if (*negated && !directive_info[i].can_be_negated)
1262 as_bad (_("directive %s can't be negated"),
1263 directive_info[i].name);
1264 return;
1265 }
1266 }
1267
1268 as_bad (_("unknown directive"));
1269 *directive = (directiveE) XTENSA_UNDEFINED;
1270 }
1271
1272
1273 static void
1274 xtensa_begin_directive (ignore)
1275 int ignore ATTRIBUTE_UNUSED;
1276 {
1277 directiveE directive;
1278 bfd_boolean negated;
1279 emit_state *state;
1280 int len;
1281 lit_state *ls;
1282
1283 get_directive (&directive, &negated);
1284 if (directive == (directiveE) XTENSA_UNDEFINED)
1285 {
1286 discard_rest_of_line ();
1287 return;
1288 }
1289
1290 switch (directive)
1291 {
1292 case directive_literal:
1293 state = (emit_state *) xmalloc (sizeof (emit_state));
1294 xtensa_switch_to_literal_fragment (state);
1295 directive_push (directive_literal, negated, state);
1296 break;
1297
1298 case directive_literal_prefix:
1299 /* Check to see if the current fragment is a literal
1300 fragment. If it is, then this operation is not allowed. */
1301 if (frag_now->tc_frag_data.is_literal)
1302 {
1303 as_bad (_("cannot set literal_prefix inside literal fragment"));
1304 return;
1305 }
1306
1307 /* Allocate the literal state for this section and push
1308 onto the directive stack. */
1309 ls = xmalloc (sizeof (lit_state));
1310 assert (ls);
1311
1312 *ls = default_lit_sections;
1313
1314 directive_push (directive_literal_prefix, negated, ls);
1315
1316 /* Parse the new prefix from the input_line_pointer. */
1317 SKIP_WHITESPACE ();
1318 len = strspn (input_line_pointer,
1319 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1320 "abcdefghijklmnopqrstuvwxyz_/0123456789.$");
1321
1322 /* Process the new prefix. */
1323 xtensa_literal_prefix (input_line_pointer, len);
1324
1325 /* Skip the name in the input line. */
1326 input_line_pointer += len;
1327 break;
1328
1329 case directive_freeregs:
1330 /* This information is currently unused, but we'll accept the statement
1331 and just discard the rest of the line. This won't check the syntax,
1332 but it will accept every correct freeregs directive. */
1333 input_line_pointer += strcspn (input_line_pointer, "\n");
1334 directive_push (directive_freeregs, negated, 0);
1335 break;
1336
1337 case directive_density:
1338 if (!density_supported && !negated)
1339 {
1340 as_warn (_("Xtensa density option not supported; ignored"));
1341 break;
1342 }
1343 /* fall through */
1344
1345 default:
1346 directive_push (directive, negated, 0);
1347 break;
1348 }
1349
1350 demand_empty_rest_of_line ();
1351 }
1352
1353
1354 static void
1355 xtensa_end_directive (ignore)
1356 int ignore ATTRIBUTE_UNUSED;
1357 {
1358 directiveE begin_directive, end_directive;
1359 bfd_boolean begin_negated, end_negated;
1360 const char *file;
1361 unsigned int line;
1362 emit_state *state;
1363 lit_state *s;
1364
1365 get_directive (&end_directive, &end_negated);
1366 if (end_directive == (directiveE) XTENSA_UNDEFINED)
1367 {
1368 discard_rest_of_line ();
1369 return;
1370 }
1371
1372 if (end_directive == directive_density && !density_supported && !end_negated)
1373 {
1374 as_warn (_("Xtensa density option not supported; ignored"));
1375 demand_empty_rest_of_line ();
1376 return;
1377 }
1378
1379 directive_pop (&begin_directive, &begin_negated, &file, &line,
1380 (const void **) &state);
1381
1382 if (begin_directive != directive_none)
1383 {
1384 if (begin_directive != end_directive || begin_negated != end_negated)
1385 {
1386 as_bad (_("does not match begin %s%s at %s:%d"),
1387 begin_negated ? "no-" : "",
1388 directive_info[begin_directive].name, file, line);
1389 }
1390 else
1391 {
1392 switch (end_directive)
1393 {
1394 case directive_literal:
1395 frag_var (rs_fill, 0, 0, 0, NULL, 0, NULL);
1396 xtensa_restore_emit_state (state);
1397 free (state);
1398 break;
1399
1400 case directive_freeregs:
1401 break;
1402
1403 case directive_literal_prefix:
1404 /* Restore the default collection sections from saved state. */
1405 s = (lit_state *) state;
1406 assert (s);
1407
1408 if (use_literal_section)
1409 default_lit_sections = *s;
1410
1411 /* free the state storage */
1412 free (s);
1413 break;
1414
1415 default:
1416 break;
1417 }
1418 }
1419 }
1420
1421 demand_empty_rest_of_line ();
1422 }
1423
1424
1425 /* Place an aligned literal fragment at the current location. */
1426
1427 static void
1428 xtensa_literal_position (ignore)
1429 int ignore ATTRIBUTE_UNUSED;
1430 {
1431 if (inside_directive (directive_literal))
1432 as_warn (_(".literal_position inside literal directive; ignoring"));
1433 else if (!use_literal_section)
1434 xtensa_mark_literal_pool_location (FALSE);
1435
1436 demand_empty_rest_of_line ();
1437 }
1438
1439
1440 /* Support .literal label, value@plt + offset. */
1441
1442 static void
1443 xtensa_literal_pseudo (ignored)
1444 int ignored ATTRIBUTE_UNUSED;
1445 {
1446 emit_state state;
1447 char *base_name;
1448 #ifdef XTENSA_COMBINE_LITERALS
1449 char *next_name;
1450 symbolS *duplicate;
1451 bfd_boolean used_name = FALSE;
1452 int offset = 0;
1453 #endif
1454 char c;
1455 char *p;
1456 expressionS expP;
1457 segT dest_seg;
1458
1459 /* If we are using text-section literals, then this is the right value... */
1460 dest_seg = now_seg;
1461
1462 base_name = input_line_pointer;
1463
1464 xtensa_switch_to_literal_fragment (&state);
1465
1466 /* ...but if we aren't using text-section-literals, then we
1467 need to put them in the section we just switched to. */
1468 if (use_literal_section)
1469 dest_seg = now_seg;
1470
1471 /* All literals are aligned to four-byte boundaries
1472 which is handled by switch to literal fragment. */
1473 /* frag_align (2, 0, 0); */
1474
1475 c = get_symbol_end ();
1476 /* Just after name is now '\0'. */
1477 p = input_line_pointer;
1478 *p = c;
1479 SKIP_WHITESPACE ();
1480
1481 if (*input_line_pointer != ',' && *input_line_pointer != ':')
1482 {
1483 as_bad (_("expected comma or colon after symbol name; "
1484 "rest of line ignored"));
1485 ignore_rest_of_line ();
1486 xtensa_restore_emit_state (&state);
1487 return;
1488 }
1489 *p = 0;
1490
1491 #ifdef XTENSA_COMBINE_LITERALS
1492 /* We need next name to start out equal to base_name,
1493 but we modify it later to refer to a symbol and an offset. */
1494 next_name = xmalloc (strlen (base_name) + 1);
1495 strcpy (next_name, base_name);
1496
1497 /* We need a copy of base_name because we refer to it in the
1498 lit_sym_translations and the source is somewhere in the input stream. */
1499 base_name = xmalloc (strlen (base_name) + 1);
1500 strcpy (base_name, next_name);
1501
1502 #else
1503
1504 colon (base_name);
1505 #endif
1506
1507 do
1508 {
1509 input_line_pointer++; /* skip ',' or ':' */
1510
1511 expr (0, &expP);
1512
1513 #ifdef XTENSA_COMBINE_LITERALS
1514 duplicate = is_duplicate_literal (&expP, dest_seg);
1515 if (duplicate)
1516 {
1517 add_lit_sym_translation (base_name, offset, duplicate);
1518 used_name = TRUE;
1519 continue;
1520 }
1521 colon (next_name);
1522 #endif
1523
1524 /* We only support 4-byte literals with .literal. */
1525 emit_expr (&expP, 4);
1526
1527 #ifdef XTENSA_COMBINE_LITERALS
1528 cache_literal (next_name, &expP, dest_seg);
1529 free (next_name);
1530
1531 if (*input_line_pointer == ',')
1532 {
1533 offset += 4;
1534 next_name = xmalloc (strlen (base_name) +
1535 strlen (XTENSA_LIT_PLUS_OFFSET) + 10);
1536 sprintf (next_name, "%s%s%d",
1537 XTENSA_LIT_PLUS_OFFSET, base_name, offset);
1538 }
1539 #endif
1540 }
1541 while (*input_line_pointer == ',');
1542
1543 *p = c;
1544 #ifdef XTENSA_COMBINE_LITERALS
1545 if (!used_name)
1546 free (base_name);
1547 #endif
1548
1549 demand_empty_rest_of_line ();
1550
1551 xtensa_restore_emit_state (&state);
1552 }
1553
1554
1555 static void
1556 xtensa_literal_prefix (start, len)
1557 char const *start;
1558 int len;
1559 {
1560 segT s_now; /* Storage for the current seg and subseg. */
1561 subsegT ss_now;
1562 char *name; /* Pointer to the name itself. */
1563 char *newname;
1564
1565 if (!use_literal_section)
1566 return;
1567
1568 /* Store away the current section and subsection. */
1569 s_now = now_seg;
1570 ss_now = now_subseg;
1571
1572 /* Get a null-terminated copy of the name. */
1573 name = xmalloc (len + 1);
1574 assert (name);
1575
1576 strncpy (name, start, len);
1577 name[len] = 0;
1578
1579 /* Allocate the sections (interesting note: the memory pointing to
1580 the name is actually used for the name by the new section). */
1581 newname = xmalloc (len + strlen (".literal") + 1);
1582 strcpy (newname, name);
1583 strcpy (newname + len, ".literal");
1584
1585 /* Note that retrieve_literal_seg does not create a segment if
1586 it already exists. */
1587 default_lit_sections.lit_seg = NULL; /* retrieved on demand */
1588
1589 /* Canonicalizing section names allows renaming literal
1590 sections to occur correctly. */
1591 default_lit_sections.lit_seg_name =
1592 tc_canonicalize_symbol_name (newname);
1593
1594 free (name);
1595
1596 /* Restore the current section and subsection and set the
1597 generation into the old segment. */
1598 subseg_set (s_now, ss_now);
1599 }
1600
1601 \f
1602 /* Parsing and Idiom Translation. */
1603
1604 static const char *
1605 expression_end (name)
1606 const char *name;
1607 {
1608 while (1)
1609 {
1610 switch (*name)
1611 {
1612 case ';':
1613 case '\0':
1614 case ',':
1615 return name;
1616 case ' ':
1617 case '\t':
1618 ++name;
1619 continue;
1620 default:
1621 return 0;
1622 }
1623 }
1624 }
1625
1626
1627 #define ERROR_REG_NUM ((unsigned) -1)
1628
1629 static unsigned
1630 tc_get_register (prefix)
1631 const char *prefix;
1632 {
1633 unsigned reg;
1634 const char *next_expr;
1635 const char *old_line_pointer;
1636
1637 SKIP_WHITESPACE ();
1638 old_line_pointer = input_line_pointer;
1639
1640 if (*input_line_pointer == '$')
1641 ++input_line_pointer;
1642
1643 /* Accept "sp" as a synonym for "a1". */
1644 if (input_line_pointer[0] == 's' && input_line_pointer[1] == 'p'
1645 && expression_end (input_line_pointer + 2))
1646 {
1647 input_line_pointer += 2;
1648 return 1; /* AR[1] */
1649 }
1650
1651 while (*input_line_pointer++ == *prefix++)
1652 ;
1653 --input_line_pointer;
1654 --prefix;
1655
1656 if (*prefix)
1657 {
1658 as_bad (_("bad register name: %s"), old_line_pointer);
1659 return ERROR_REG_NUM;
1660 }
1661
1662 if (!ISDIGIT ((unsigned char) *input_line_pointer))
1663 {
1664 as_bad (_("bad register number: %s"), input_line_pointer);
1665 return ERROR_REG_NUM;
1666 }
1667
1668 reg = 0;
1669
1670 while (ISDIGIT ((int) *input_line_pointer))
1671 reg = reg * 10 + *input_line_pointer++ - '0';
1672
1673 if (!(next_expr = expression_end (input_line_pointer)))
1674 {
1675 as_bad (_("bad register name: %s"), old_line_pointer);
1676 return ERROR_REG_NUM;
1677 }
1678
1679 input_line_pointer = (char *) next_expr;
1680
1681 return reg;
1682 }
1683
1684
1685 #define PLT_SUFFIX "@PLT"
1686 #define plt_suffix "@plt"
1687
1688 static void
1689 expression_maybe_register (opnd, tok)
1690 xtensa_operand opnd;
1691 expressionS *tok;
1692 {
1693 char *kind = xtensa_operand_kind (opnd);
1694
1695 if ((strlen (kind) == 1)
1696 && (*kind == 'l' || *kind == 'L' || *kind == 'i' || *kind == 'r'))
1697 {
1698 segT t = expression (tok);
1699 if (t == absolute_section && operand_is_pcrel_label (opnd))
1700 {
1701 assert (tok->X_op == O_constant);
1702 tok->X_op = O_symbol;
1703 tok->X_add_symbol = &abs_symbol;
1704 }
1705 if (tok->X_op == O_symbol
1706 && (!strncmp (input_line_pointer, PLT_SUFFIX,
1707 strlen (PLT_SUFFIX) - 1)
1708 || !strncmp (input_line_pointer, plt_suffix,
1709 strlen (plt_suffix) - 1)))
1710 {
1711 tok->X_add_symbol->sy_tc.plt = 1;
1712 input_line_pointer += strlen (plt_suffix);
1713 }
1714 #ifdef XTENSA_COMBINE_LITERALS
1715 find_lit_sym_translation (tok);
1716 #endif
1717 }
1718 else
1719 {
1720 unsigned reg = tc_get_register (kind);
1721
1722 if (reg != ERROR_REG_NUM) /* Already errored */
1723 {
1724 uint32 buf = reg;
1725 if ((xtensa_operand_encode (opnd, &buf) != xtensa_encode_result_ok)
1726 || (reg != xtensa_operand_decode (opnd, buf)))
1727 as_bad (_("register number out of range"));
1728 }
1729
1730 tok->X_op = O_register;
1731 tok->X_add_symbol = 0;
1732 tok->X_add_number = reg;
1733 }
1734 }
1735
1736
1737 /* Split up the arguments for an opcode or pseudo-op. */
1738
1739 static int
1740 tokenize_arguments (args, str)
1741 char **args;
1742 char *str;
1743 {
1744 char *old_input_line_pointer;
1745 bfd_boolean saw_comma = FALSE;
1746 bfd_boolean saw_arg = FALSE;
1747 int num_args = 0;
1748 char *arg_end, *arg;
1749 int arg_len;
1750
1751 /* Save and restore input_line_pointer around this function. */
1752 old_input_line_pointer = input_line_pointer;
1753 input_line_pointer = str;
1754
1755 while (*input_line_pointer)
1756 {
1757 SKIP_WHITESPACE ();
1758 switch (*input_line_pointer)
1759 {
1760 case '\0':
1761 goto fini;
1762
1763 case ',':
1764 input_line_pointer++;
1765 if (saw_comma || !saw_arg)
1766 goto err;
1767 saw_comma = TRUE;
1768 break;
1769
1770 default:
1771 if (!saw_comma && saw_arg)
1772 goto err;
1773
1774 arg_end = input_line_pointer + 1;
1775 while (!expression_end (arg_end))
1776 arg_end += 1;
1777
1778 arg_len = arg_end - input_line_pointer;
1779 arg = (char *) xmalloc (arg_len + 1);
1780 args[num_args] = arg;
1781
1782 strncpy (arg, input_line_pointer, arg_len);
1783 arg[arg_len] = '\0';
1784
1785 input_line_pointer = arg_end;
1786 num_args += 1;
1787 saw_comma = FALSE;
1788 saw_arg = TRUE;
1789 break;
1790 }
1791 }
1792
1793 fini:
1794 if (saw_comma)
1795 goto err;
1796 input_line_pointer = old_input_line_pointer;
1797 return num_args;
1798
1799 err:
1800 input_line_pointer = old_input_line_pointer;
1801 return -1;
1802 }
1803
1804
1805 /* Parse the arguments to an opcode. Return true on error. */
1806
1807 static bfd_boolean
1808 parse_arguments (insn, num_args, arg_strings)
1809 TInsn *insn;
1810 int num_args;
1811 char **arg_strings;
1812 {
1813 expressionS *tok = insn->tok;
1814 xtensa_opcode opcode = insn->opcode;
1815 bfd_boolean had_error = TRUE;
1816 xtensa_isa isa = xtensa_default_isa;
1817 int n;
1818 int opcode_operand_count;
1819 int actual_operand_count = 0;
1820 xtensa_operand opnd = NULL;
1821 char *old_input_line_pointer;
1822
1823 if (insn->insn_type == ITYPE_LITERAL)
1824 opcode_operand_count = 1;
1825 else
1826 opcode_operand_count = xtensa_num_operands (isa, opcode);
1827
1828 memset (tok, 0, sizeof (*tok) * MAX_INSN_ARGS);
1829
1830 /* Save and restore input_line_pointer around this function. */
1831 old_input_line_pointer = input_line_pointer;
1832
1833 for (n = 0; n < num_args; n++)
1834 {
1835 input_line_pointer = arg_strings[n];
1836
1837 if (actual_operand_count >= opcode_operand_count)
1838 {
1839 as_warn (_("too many arguments"));
1840 goto err;
1841 }
1842 assert (actual_operand_count < MAX_INSN_ARGS);
1843
1844 opnd = xtensa_get_operand (isa, opcode, actual_operand_count);
1845 expression_maybe_register (opnd, tok);
1846
1847 if (tok->X_op == O_illegal || tok->X_op == O_absent)
1848 goto err;
1849 actual_operand_count++;
1850 tok++;
1851 }
1852
1853 insn->ntok = tok - insn->tok;
1854 had_error = FALSE;
1855
1856 err:
1857 input_line_pointer = old_input_line_pointer;
1858 return had_error;
1859 }
1860
1861
1862 static void
1863 xg_reverse_shift_count (cnt_argp)
1864 char **cnt_argp;
1865 {
1866 char *cnt_arg, *new_arg;
1867 cnt_arg = *cnt_argp;
1868
1869 /* replace the argument with "31-(argument)" */
1870 new_arg = (char *) xmalloc (strlen (cnt_arg) + 6);
1871 sprintf (new_arg, "31-(%s)", cnt_arg);
1872
1873 free (cnt_arg);
1874 *cnt_argp = new_arg;
1875 }
1876
1877
1878 /* If "arg" is a constant expression, return non-zero with the value
1879 in *valp. */
1880
1881 static int
1882 xg_arg_is_constant (arg, valp)
1883 char *arg;
1884 offsetT *valp;
1885 {
1886 expressionS exp;
1887 char *save_ptr = input_line_pointer;
1888
1889 input_line_pointer = arg;
1890 expression (&exp);
1891 input_line_pointer = save_ptr;
1892
1893 if (exp.X_op == O_constant)
1894 {
1895 *valp = exp.X_add_number;
1896 return 1;
1897 }
1898
1899 return 0;
1900 }
1901
1902
1903 static void
1904 xg_replace_opname (popname, newop)
1905 char **popname;
1906 char *newop;
1907 {
1908 free (*popname);
1909 *popname = (char *) xmalloc (strlen (newop) + 1);
1910 strcpy (*popname, newop);
1911 }
1912
1913
1914 static int
1915 xg_check_num_args (pnum_args, expected_num, opname, arg_strings)
1916 int *pnum_args;
1917 int expected_num;
1918 char *opname;
1919 char **arg_strings;
1920 {
1921 int num_args = *pnum_args;
1922
1923 if (num_args < expected_num)
1924 {
1925 as_bad (_("not enough operands (%d) for '%s'; expected %d"),
1926 num_args, opname, expected_num);
1927 return -1;
1928 }
1929
1930 if (num_args > expected_num)
1931 {
1932 as_warn (_("too many operands (%d) for '%s'; expected %d"),
1933 num_args, opname, expected_num);
1934 while (num_args-- > expected_num)
1935 {
1936 free (arg_strings[num_args]);
1937 arg_strings[num_args] = 0;
1938 }
1939 *pnum_args = expected_num;
1940 return -1;
1941 }
1942
1943 return 0;
1944 }
1945
1946
1947 static int
1948 xg_translate_sysreg_op (popname, pnum_args, arg_strings)
1949 char **popname;
1950 int *pnum_args;
1951 char **arg_strings;
1952 {
1953 char *opname, *new_opname;
1954 offsetT val;
1955 bfd_boolean has_underbar = FALSE;
1956
1957 opname = *popname;
1958 if (*opname == '_')
1959 {
1960 has_underbar = TRUE;
1961 opname += 1;
1962 }
1963
1964 /* Opname == [rw]ur... */
1965
1966 if (opname[3] == '\0')
1967 {
1968 /* If the register is not specified as part of the opcode,
1969 then get it from the operand and move it to the opcode. */
1970
1971 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
1972 return -1;
1973
1974 if (!xg_arg_is_constant (arg_strings[1], &val))
1975 {
1976 as_bad (_("register number for `%s' is not a constant"), opname);
1977 return -1;
1978 }
1979 if ((unsigned) val > 255)
1980 {
1981 as_bad (_("register number (%ld) for `%s' is out of range"),
1982 val, opname);
1983 return -1;
1984 }
1985
1986 /* Remove the last argument, which is now part of the opcode. */
1987 free (arg_strings[1]);
1988 arg_strings[1] = 0;
1989 *pnum_args = 1;
1990
1991 /* Translate the opcode. */
1992 new_opname = (char *) xmalloc (8);
1993 sprintf (new_opname, "%s%cur%u", (has_underbar ? "_" : ""),
1994 opname[0], (unsigned) val);
1995 free (*popname);
1996 *popname = new_opname;
1997 }
1998
1999 return 0;
2000 }
2001
2002
2003 /* If the instruction is an idiom (i.e., a built-in macro), translate it.
2004 Returns non-zero if an error was found. */
2005
2006 static int
2007 xg_translate_idioms (popname, pnum_args, arg_strings)
2008 char **popname;
2009 int *pnum_args;
2010 char **arg_strings;
2011 {
2012 char *opname = *popname;
2013 bfd_boolean has_underbar = FALSE;
2014
2015 if (*opname == '_')
2016 {
2017 has_underbar = TRUE;
2018 opname += 1;
2019 }
2020
2021 if (strcmp (opname, "mov") == 0)
2022 {
2023 if (!has_underbar && code_density_available ())
2024 xg_replace_opname (popname, "mov.n");
2025 else
2026 {
2027 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
2028 return -1;
2029 xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
2030 arg_strings[2] = (char *) xmalloc (strlen (arg_strings[1]) + 1);
2031 strcpy (arg_strings[2], arg_strings[1]);
2032 *pnum_args = 3;
2033 }
2034 return 0;
2035 }
2036
2037 if (strcmp (opname, "bbsi.l") == 0)
2038 {
2039 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2040 return -1;
2041 xg_replace_opname (popname, (has_underbar ? "_bbsi" : "bbsi"));
2042 if (target_big_endian)
2043 xg_reverse_shift_count (&arg_strings[1]);
2044 return 0;
2045 }
2046
2047 if (strcmp (opname, "bbci.l") == 0)
2048 {
2049 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2050 return -1;
2051 xg_replace_opname (popname, (has_underbar ? "_bbci" : "bbci"));
2052 if (target_big_endian)
2053 xg_reverse_shift_count (&arg_strings[1]);
2054 return 0;
2055 }
2056
2057 if (strcmp (opname, "nop") == 0)
2058 {
2059 if (!has_underbar && code_density_available ())
2060 xg_replace_opname (popname, "nop.n");
2061 else
2062 {
2063 if (xg_check_num_args (pnum_args, 0, opname, arg_strings))
2064 return -1;
2065 xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
2066 arg_strings[0] = (char *) xmalloc (3);
2067 arg_strings[1] = (char *) xmalloc (3);
2068 arg_strings[2] = (char *) xmalloc (3);
2069 strcpy (arg_strings[0], "a1");
2070 strcpy (arg_strings[1], "a1");
2071 strcpy (arg_strings[2], "a1");
2072 *pnum_args = 3;
2073 }
2074 return 0;
2075 }
2076
2077 if ((opname[0] == 'r' || opname[0] == 'w')
2078 && opname[1] == 'u'
2079 && opname[2] == 'r')
2080 return xg_translate_sysreg_op (popname, pnum_args, arg_strings);
2081
2082
2083 /* WIDENING DENSITY OPCODES
2084
2085 questionable relaxations (widening) from old "tai" idioms:
2086
2087 ADD.N --> ADD
2088 BEQZ.N --> BEQZ
2089 RET.N --> RET
2090 RETW.N --> RETW
2091 MOVI.N --> MOVI
2092 MOV.N --> MOV
2093 NOP.N --> NOP
2094
2095 Note: this incomplete list was imported to match the "tai"
2096 behavior; other density opcodes are not handled.
2097
2098 The xtensa-relax code may know how to do these but it doesn't do
2099 anything when these density opcodes appear inside a no-density
2100 region. Somehow GAS should either print an error when that happens
2101 or do the widening. The old "tai" behavior was to do the widening.
2102 For now, I'll make it widen but print a warning.
2103
2104 FIXME: GAS needs to detect density opcodes inside no-density
2105 regions and treat them as errors. This code should be removed
2106 when that is done. */
2107
2108 if (use_generics ()
2109 && !has_underbar
2110 && density_supported
2111 && !code_density_available ())
2112 {
2113 if (strcmp (opname, "add.n") == 0)
2114 xg_replace_opname (popname, "add");
2115
2116 else if (strcmp (opname, "beqz.n") == 0)
2117 xg_replace_opname (popname, "beqz");
2118
2119 else if (strcmp (opname, "ret.n") == 0)
2120 xg_replace_opname (popname, "ret");
2121
2122 else if (strcmp (opname, "retw.n") == 0)
2123 xg_replace_opname (popname, "retw");
2124
2125 else if (strcmp (opname, "movi.n") == 0)
2126 xg_replace_opname (popname, "movi");
2127
2128 else if (strcmp (opname, "mov.n") == 0)
2129 {
2130 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
2131 return -1;
2132 xg_replace_opname (popname, "or");
2133 arg_strings[2] = (char *) xmalloc (strlen (arg_strings[1]) + 1);
2134 strcpy (arg_strings[2], arg_strings[1]);
2135 *pnum_args = 3;
2136 }
2137
2138 else if (strcmp (opname, "nop.n") == 0)
2139 {
2140 if (xg_check_num_args (pnum_args, 0, opname, arg_strings))
2141 return -1;
2142 xg_replace_opname (popname, "or");
2143 arg_strings[0] = (char *) xmalloc (3);
2144 arg_strings[1] = (char *) xmalloc (3);
2145 arg_strings[2] = (char *) xmalloc (3);
2146 strcpy (arg_strings[0], "a1");
2147 strcpy (arg_strings[1], "a1");
2148 strcpy (arg_strings[2], "a1");
2149 *pnum_args = 3;
2150 }
2151 }
2152
2153 return 0;
2154 }
2155
2156 \f
2157 /* Functions for dealing with the Xtensa ISA. */
2158
2159 /* Return true if the given operand is an immed or target instruction,
2160 i.e., has a reloc associated with it. Currently, this is only true
2161 if the operand kind is "i, "l" or "L". */
2162
2163 static bfd_boolean
2164 operand_is_immed (opnd)
2165 xtensa_operand opnd;
2166 {
2167 const char *opkind = xtensa_operand_kind (opnd);
2168 if (opkind[0] == '\0' || opkind[1] != '\0')
2169 return FALSE;
2170 switch (opkind[0])
2171 {
2172 case 'i':
2173 case 'l':
2174 case 'L':
2175 return TRUE;
2176 }
2177 return FALSE;
2178 }
2179
2180
2181 /* Return true if the given operand is a pc-relative label. This is
2182 true for "l", "L", and "r" operand kinds. */
2183
2184 bfd_boolean
2185 operand_is_pcrel_label (opnd)
2186 xtensa_operand opnd;
2187 {
2188 const char *opkind = xtensa_operand_kind (opnd);
2189 if (opkind[0] == '\0' || opkind[1] != '\0')
2190 return FALSE;
2191 switch (opkind[0])
2192 {
2193 case 'r':
2194 case 'l':
2195 case 'L':
2196 return TRUE;
2197 }
2198 return FALSE;
2199 }
2200
2201
2202 /* Currently the assembler only allows us to use a single target per
2203 fragment. Because of this, only one operand for a given
2204 instruction may be symbolic. If there is an operand of kind "lrL",
2205 the last one is chosen. Otherwise, the result is the number of the
2206 last operand of type "i", and if there are none of those, we fail
2207 and return -1. */
2208
2209 int
2210 get_relaxable_immed (opcode)
2211 xtensa_opcode opcode;
2212 {
2213 int last_immed = -1;
2214 int noperands, opi;
2215 xtensa_operand operand;
2216
2217 if (opcode == XTENSA_UNDEFINED)
2218 return -1;
2219
2220 noperands = xtensa_num_operands (xtensa_default_isa, opcode);
2221 for (opi = noperands - 1; opi >= 0; opi--)
2222 {
2223 operand = xtensa_get_operand (xtensa_default_isa, opcode, opi);
2224 if (operand_is_pcrel_label (operand))
2225 return opi;
2226 if (last_immed == -1 && operand_is_immed (operand))
2227 last_immed = opi;
2228 }
2229 return last_immed;
2230 }
2231
2232
2233 xtensa_opcode
2234 get_opcode_from_buf (buf)
2235 const char *buf;
2236 {
2237 static xtensa_insnbuf insnbuf = NULL;
2238 xtensa_opcode opcode;
2239 xtensa_isa isa = xtensa_default_isa;
2240 if (!insnbuf)
2241 insnbuf = xtensa_insnbuf_alloc (isa);
2242
2243 xtensa_insnbuf_from_chars (isa, insnbuf, buf);
2244 opcode = xtensa_decode_insn (isa, insnbuf);
2245 return opcode;
2246 }
2247
2248
2249 static bfd_boolean
2250 is_direct_call_opcode (opcode)
2251 xtensa_opcode opcode;
2252 {
2253 if (opcode == XTENSA_UNDEFINED)
2254 return FALSE;
2255
2256 return (opcode == xtensa_call0_opcode
2257 || opcode == xtensa_call4_opcode
2258 || opcode == xtensa_call8_opcode
2259 || opcode == xtensa_call12_opcode);
2260 }
2261
2262
2263 static bfd_boolean
2264 is_call_opcode (opcode)
2265 xtensa_opcode opcode;
2266 {
2267 if (is_direct_call_opcode (opcode))
2268 return TRUE;
2269
2270 if (opcode == XTENSA_UNDEFINED)
2271 return FALSE;
2272
2273 return (opcode == xtensa_callx0_opcode
2274 || opcode == xtensa_callx4_opcode
2275 || opcode == xtensa_callx8_opcode
2276 || opcode == xtensa_callx12_opcode);
2277 }
2278
2279
2280 /* Return true if the opcode is an entry opcode. This is used because
2281 "entry" adds an implicit ".align 4" and also the entry instruction
2282 has an extra check for an operand value. */
2283
2284 static bfd_boolean
2285 is_entry_opcode (opcode)
2286 xtensa_opcode opcode;
2287 {
2288 if (opcode == XTENSA_UNDEFINED)
2289 return FALSE;
2290
2291 return (opcode == xtensa_entry_opcode);
2292 }
2293
2294
2295 /* Return true if it is one of the loop opcodes. Loops are special
2296 because they need automatic alignment and they have a relaxation so
2297 complex that we hard-coded it. */
2298
2299 static bfd_boolean
2300 is_loop_opcode (opcode)
2301 xtensa_opcode opcode;
2302 {
2303 if (opcode == XTENSA_UNDEFINED)
2304 return FALSE;
2305
2306 return (opcode == xtensa_loop_opcode
2307 || opcode == xtensa_loopnez_opcode
2308 || opcode == xtensa_loopgtz_opcode);
2309 }
2310
2311
2312 static bfd_boolean
2313 is_the_loop_opcode (opcode)
2314 xtensa_opcode opcode;
2315 {
2316 if (opcode == XTENSA_UNDEFINED)
2317 return FALSE;
2318
2319 return (opcode == xtensa_loop_opcode);
2320 }
2321
2322
2323 static bfd_boolean
2324 is_jx_opcode (opcode)
2325 xtensa_opcode opcode;
2326 {
2327 if (opcode == XTENSA_UNDEFINED)
2328 return FALSE;
2329
2330 return (opcode == xtensa_jx_opcode);
2331 }
2332
2333
2334 /* Return true if the opcode is a retw or retw.n.
2335 Needed to add nops to avoid a hardware interlock issue. */
2336
2337 static bfd_boolean
2338 is_windowed_return_opcode (opcode)
2339 xtensa_opcode opcode;
2340 {
2341 if (opcode == XTENSA_UNDEFINED)
2342 return FALSE;
2343
2344 return (opcode == xtensa_retw_opcode || opcode == xtensa_retw_n_opcode);
2345 }
2346
2347
2348 /* Return true if the opcode type is "l" and the opcode is NOT a jump. */
2349
2350 static bfd_boolean
2351 is_conditional_branch_opcode (opcode)
2352 xtensa_opcode opcode;
2353 {
2354 xtensa_isa isa = xtensa_default_isa;
2355 int num_ops, i;
2356
2357 if (opcode == xtensa_j_opcode && opcode != XTENSA_UNDEFINED)
2358 return FALSE;
2359
2360 num_ops = xtensa_num_operands (isa, opcode);
2361 for (i = 0; i < num_ops; i++)
2362 {
2363 xtensa_operand operand = xtensa_get_operand (isa, opcode, i);
2364 if (strcmp (xtensa_operand_kind (operand), "l") == 0)
2365 return TRUE;
2366 }
2367 return FALSE;
2368 }
2369
2370
2371 /* Return true if the given opcode is a conditional branch
2372 instruction, i.e., currently this is true if the instruction
2373 is a jx or has an operand with 'l' type and is not a loop. */
2374
2375 bfd_boolean
2376 is_branch_or_jump_opcode (opcode)
2377 xtensa_opcode opcode;
2378 {
2379 int opn, op_count;
2380
2381 if (opcode == XTENSA_UNDEFINED)
2382 return FALSE;
2383
2384 if (is_loop_opcode (opcode))
2385 return FALSE;
2386
2387 if (is_jx_opcode (opcode))
2388 return TRUE;
2389
2390 op_count = xtensa_num_operands (xtensa_default_isa, opcode);
2391 for (opn = 0; opn < op_count; opn++)
2392 {
2393 xtensa_operand opnd =
2394 xtensa_get_operand (xtensa_default_isa, opcode, opn);
2395 const char *opkind = xtensa_operand_kind (opnd);
2396 if (opkind && opkind[0] == 'l' && opkind[1] == '\0')
2397 return TRUE;
2398 }
2399 return FALSE;
2400 }
2401
2402
2403 /* Convert from operand numbers to BFD relocation type code.
2404 Return BFD_RELOC_NONE on failure. */
2405
2406 bfd_reloc_code_real_type
2407 opnum_to_reloc (opnum)
2408 int opnum;
2409 {
2410 switch (opnum)
2411 {
2412 case 0:
2413 return BFD_RELOC_XTENSA_OP0;
2414 case 1:
2415 return BFD_RELOC_XTENSA_OP1;
2416 case 2:
2417 return BFD_RELOC_XTENSA_OP2;
2418 default:
2419 break;
2420 }
2421 return BFD_RELOC_NONE;
2422 }
2423
2424
2425 /* Convert from BFD relocation type code to operand number.
2426 Return -1 on failure. */
2427
2428 int
2429 reloc_to_opnum (reloc)
2430 bfd_reloc_code_real_type reloc;
2431 {
2432 switch (reloc)
2433 {
2434 case BFD_RELOC_XTENSA_OP0:
2435 return 0;
2436 case BFD_RELOC_XTENSA_OP1:
2437 return 1;
2438 case BFD_RELOC_XTENSA_OP2:
2439 return 2;
2440 default:
2441 break;
2442 }
2443 return -1;
2444 }
2445
2446
2447 static void
2448 xtensa_insnbuf_set_operand (insnbuf, opcode, operand, value, file, line)
2449 xtensa_insnbuf insnbuf;
2450 xtensa_opcode opcode;
2451 xtensa_operand operand;
2452 int32 value;
2453 const char *file;
2454 unsigned int line;
2455 {
2456 xtensa_encode_result encode_result;
2457 uint32 valbuf = value;
2458
2459 encode_result = xtensa_operand_encode (operand, &valbuf);
2460
2461 switch (encode_result)
2462 {
2463 case xtensa_encode_result_ok:
2464 break;
2465 case xtensa_encode_result_align:
2466 as_bad_where ((char *) file, line,
2467 _("operand %d not properly aligned for '%s'"),
2468 value, xtensa_opcode_name (xtensa_default_isa, opcode));
2469 break;
2470 case xtensa_encode_result_not_in_table:
2471 as_bad_where ((char *) file, line,
2472 _("operand %d not in immediate table for '%s'"),
2473 value, xtensa_opcode_name (xtensa_default_isa, opcode));
2474 break;
2475 case xtensa_encode_result_too_high:
2476 as_bad_where ((char *) file, line,
2477 _("operand %d too large for '%s'"), value,
2478 xtensa_opcode_name (xtensa_default_isa, opcode));
2479 break;
2480 case xtensa_encode_result_too_low:
2481 as_bad_where ((char *) file, line,
2482 _("operand %d too small for '%s'"), value,
2483 xtensa_opcode_name (xtensa_default_isa, opcode));
2484 break;
2485 case xtensa_encode_result_not_ok:
2486 as_bad_where ((char *) file, line,
2487 _("operand %d is invalid for '%s'"), value,
2488 xtensa_opcode_name (xtensa_default_isa, opcode));
2489 break;
2490 default:
2491 abort ();
2492 }
2493
2494 xtensa_operand_set_field (operand, insnbuf, valbuf);
2495 }
2496
2497
2498 static uint32
2499 xtensa_insnbuf_get_operand (insnbuf, opcode, opnum)
2500 xtensa_insnbuf insnbuf;
2501 xtensa_opcode opcode;
2502 int opnum;
2503 {
2504 xtensa_operand op = xtensa_get_operand (xtensa_default_isa, opcode, opnum);
2505 return xtensa_operand_decode (op, xtensa_operand_get_field (op, insnbuf));
2506 }
2507
2508
2509 static void
2510 xtensa_insnbuf_set_immediate_field (opcode, insnbuf, value, file, line)
2511 xtensa_opcode opcode;
2512 xtensa_insnbuf insnbuf;
2513 int32 value;
2514 const char *file;
2515 unsigned int line;
2516 {
2517 xtensa_isa isa = xtensa_default_isa;
2518 int last_opnd = xtensa_num_operands (isa, opcode) - 1;
2519 xtensa_operand operand = xtensa_get_operand (isa, opcode, last_opnd);
2520 xtensa_insnbuf_set_operand (insnbuf, opcode, operand, value, file, line);
2521 }
2522
2523
2524 static bfd_boolean
2525 is_negatable_branch (insn)
2526 TInsn *insn;
2527 {
2528 xtensa_isa isa = xtensa_default_isa;
2529 int i;
2530 int num_ops = xtensa_num_operands (isa, insn->opcode);
2531
2532 for (i = 0; i < num_ops; i++)
2533 {
2534 xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
2535 char *kind = xtensa_operand_kind (opnd);
2536 if (strlen (kind) == 1 && *kind == 'l')
2537 return TRUE;
2538 }
2539 return FALSE;
2540 }
2541
2542 \f
2543 /* Lists for recording various properties of symbols. */
2544
2545 typedef struct symbol_consS_struct
2546 {
2547 symbolS *first;
2548 /* These are used for the target taken. */
2549 int is_loop_target:1;
2550 int is_branch_target:1;
2551 int is_literal:1;
2552 int is_moved:1;
2553 struct symbol_consS_struct *rest;
2554 } symbol_consS;
2555
2556 symbol_consS *defined_symbols = 0;
2557 symbol_consS *branch_targets = 0;
2558
2559
2560 static void
2561 xtensa_define_label (sym)
2562 symbolS *sym;
2563 {
2564 symbol_consS *cons = (symbol_consS *) xmalloc (sizeof (symbol_consS));
2565
2566 cons->first = sym;
2567 cons->is_branch_target = 0;
2568 cons->is_loop_target = 0;
2569 cons->is_literal = generating_literals ? 1 : 0;
2570 cons->is_moved = 0;
2571 cons->rest = defined_symbols;
2572 defined_symbols = cons;
2573 }
2574
2575
2576 void
2577 add_target_symbol (sym, is_loop)
2578 symbolS *sym;
2579 bfd_boolean is_loop;
2580 {
2581 symbol_consS *cons, *sym_e;
2582
2583 for (sym_e = branch_targets; sym_e; sym_e = sym_e->rest)
2584 {
2585 if (sym_e->first == sym)
2586 {
2587 if (is_loop)
2588 sym_e->is_loop_target = 1;
2589 else
2590 sym_e->is_branch_target = 1;
2591 return;
2592 }
2593 }
2594
2595 cons = (symbol_consS *) xmalloc (sizeof (symbol_consS));
2596 cons->first = sym;
2597 cons->is_branch_target = (is_loop ? 0 : 1);
2598 cons->is_loop_target = (is_loop ? 1 : 0);
2599 cons->rest = branch_targets;
2600 branch_targets = cons;
2601 }
2602
2603
2604 /* Find the symbol at a given position. (Note: the "loops_ok"
2605 argument is provided to allow ignoring labels that define loop
2606 ends. This fixes a bug where the NOPs to align a loop opcode were
2607 included in a previous zero-cost loop:
2608
2609 loop a0, loopend
2610 <loop1 body>
2611 loopend:
2612
2613 loop a2, loopend2
2614 <loop2 body>
2615
2616 would become:
2617
2618 loop a0, loopend
2619 <loop1 body>
2620 nop.n <===== bad!
2621 loopend:
2622
2623 loop a2, loopend2
2624 <loop2 body>
2625
2626 This argument is used to prevent moving the NOP to before the
2627 loop-end label, which is what you want in this special case.) */
2628
2629 static symbolS *
2630 xtensa_find_label (fragP, offset, loops_ok)
2631 fragS *fragP;
2632 offsetT offset;
2633 bfd_boolean loops_ok;
2634 {
2635 symbol_consS *consP;
2636
2637 for (consP = defined_symbols; consP; consP = consP->rest)
2638 {
2639 symbolS *symP = consP->first;
2640
2641 if (S_GET_SEGMENT (symP) == now_seg
2642 && symbol_get_frag (symP) == fragP
2643 && symbol_constant_p (symP)
2644 && S_GET_VALUE (symP) == fragP->fr_address + (unsigned) offset
2645 && (loops_ok || !is_loop_target_label (symP)))
2646 return symP;
2647 }
2648 return NULL;
2649 }
2650
2651
2652 static void
2653 map_over_defined_symbols (fn)
2654 void (*fn) PARAMS ((symbolS *));
2655 {
2656 symbol_consS *sym_cons;
2657
2658 for (sym_cons = defined_symbols; sym_cons; sym_cons = sym_cons->rest)
2659 fn (sym_cons->first);
2660 }
2661
2662
2663 static bfd_boolean
2664 is_loop_target_label (sym)
2665 symbolS *sym;
2666 {
2667 symbol_consS *sym_e;
2668
2669 for (sym_e = branch_targets; sym_e; sym_e = sym_e->rest)
2670 {
2671 if (sym_e->first == sym)
2672 return sym_e->is_loop_target;
2673 }
2674 return FALSE;
2675 }
2676
2677
2678 /* Walk over all of the symbols that are branch target labels and
2679 loop target labels. Mark the associated fragments for these with
2680 the appropriate flags. */
2681
2682 static void
2683 xtensa_mark_target_fragments ()
2684 {
2685 symbol_consS *sym_e;
2686
2687 for (sym_e = branch_targets; sym_e; sym_e = sym_e->rest)
2688 {
2689 symbolS *sym = sym_e->first;
2690
2691 if (symbol_get_frag (sym)
2692 && symbol_constant_p (sym)
2693 && S_GET_VALUE (sym) == 0)
2694 {
2695 if (sym_e->is_branch_target)
2696 symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
2697 if (sym_e->is_loop_target)
2698 symbol_get_frag (sym)->tc_frag_data.is_loop_target = TRUE;
2699 }
2700 }
2701 }
2702
2703 \f
2704 /* Various Other Internal Functions. */
2705
2706 static bfd_boolean
2707 is_unique_insn_expansion (r)
2708 TransitionRule *r;
2709 {
2710 if (!r->to_instr || r->to_instr->next != NULL)
2711 return FALSE;
2712 if (r->to_instr->typ != INSTR_INSTR)
2713 return FALSE;
2714 return TRUE;
2715 }
2716
2717
2718 static int
2719 xg_get_insn_size (insn)
2720 TInsn *insn;
2721 {
2722 assert (insn->insn_type == ITYPE_INSN);
2723 return xtensa_insn_length (xtensa_default_isa, insn->opcode);
2724 }
2725
2726
2727 static int
2728 xg_get_build_instr_size (insn)
2729 BuildInstr *insn;
2730 {
2731 assert (insn->typ == INSTR_INSTR);
2732 return xtensa_insn_length (xtensa_default_isa, insn->opcode);
2733 }
2734
2735
2736 bfd_boolean
2737 xg_is_narrow_insn (insn)
2738 TInsn *insn;
2739 {
2740 TransitionTable *table = xg_build_widen_table ();
2741 TransitionList *l;
2742 int num_match = 0;
2743 assert (insn->insn_type == ITYPE_INSN);
2744 assert (insn->opcode < table->num_opcodes);
2745
2746 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
2747 {
2748 TransitionRule *rule = l->rule;
2749
2750 if (xg_instruction_matches_rule (insn, rule)
2751 && is_unique_insn_expansion (rule))
2752 {
2753 /* It only generates one instruction... */
2754 assert (insn->insn_type == ITYPE_INSN);
2755 /* ...and it is a larger instruction. */
2756 if (xg_get_insn_size (insn)
2757 < xg_get_build_instr_size (rule->to_instr))
2758 {
2759 num_match++;
2760 if (num_match > 1)
2761 return FALSE;
2762 }
2763 }
2764 }
2765 return (num_match == 1);
2766 }
2767
2768
2769 bfd_boolean
2770 xg_is_single_relaxable_insn (insn)
2771 TInsn *insn;
2772 {
2773 TransitionTable *table = xg_build_widen_table ();
2774 TransitionList *l;
2775 int num_match = 0;
2776 assert (insn->insn_type == ITYPE_INSN);
2777 assert (insn->opcode < table->num_opcodes);
2778
2779 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
2780 {
2781 TransitionRule *rule = l->rule;
2782
2783 if (xg_instruction_matches_rule (insn, rule)
2784 && is_unique_insn_expansion (rule))
2785 {
2786 assert (insn->insn_type == ITYPE_INSN);
2787 /* ... and it is a larger instruction. */
2788 if (xg_get_insn_size (insn)
2789 <= xg_get_build_instr_size (rule->to_instr))
2790 {
2791 num_match++;
2792 if (num_match > 1)
2793 return FALSE;
2794 }
2795 }
2796 }
2797 return (num_match == 1);
2798 }
2799
2800
2801 /* Return the largest size instruction that this instruction can
2802 expand to. Currently, in all cases, this is 3 bytes. Of course we
2803 could just calculate this once and generate a table. */
2804
2805 int
2806 xg_get_max_narrow_insn_size (opcode)
2807 xtensa_opcode opcode;
2808 {
2809 /* Go ahead and compute it, but it better be 3. */
2810 TransitionTable *table = xg_build_widen_table ();
2811 TransitionList *l;
2812 int old_size = xtensa_insn_length (xtensa_default_isa, opcode);
2813 assert (opcode < table->num_opcodes);
2814
2815 /* Actually we can do better. Check to see of Only one applies. */
2816 for (l = table->table[opcode]; l != NULL; l = l->next)
2817 {
2818 TransitionRule *rule = l->rule;
2819
2820 /* If it only generates one instruction. */
2821 if (is_unique_insn_expansion (rule))
2822 {
2823 int new_size = xtensa_insn_length (xtensa_default_isa,
2824 rule->to_instr->opcode);
2825 if (new_size > old_size)
2826 {
2827 assert (new_size == 3);
2828 return 3;
2829 }
2830 }
2831 }
2832 return old_size;
2833 }
2834
2835
2836 /* Return the maximum number of bytes this opcode can expand to. */
2837
2838 int
2839 xg_get_max_insn_widen_size (opcode)
2840 xtensa_opcode opcode;
2841 {
2842 TransitionTable *table = xg_build_widen_table ();
2843 TransitionList *l;
2844 int max_size = xtensa_insn_length (xtensa_default_isa, opcode);
2845
2846 assert (opcode < table->num_opcodes);
2847
2848 for (l = table->table[opcode]; l != NULL; l = l->next)
2849 {
2850 TransitionRule *rule = l->rule;
2851 BuildInstr *build_list;
2852 int this_size = 0;
2853
2854 if (!rule)
2855 continue;
2856 build_list = rule->to_instr;
2857 if (is_unique_insn_expansion (rule))
2858 {
2859 assert (build_list->typ == INSTR_INSTR);
2860 this_size = xg_get_max_insn_widen_size (build_list->opcode);
2861 }
2862 else
2863 for (; build_list != NULL; build_list = build_list->next)
2864 {
2865 switch (build_list->typ)
2866 {
2867 case INSTR_INSTR:
2868 this_size += xtensa_insn_length (xtensa_default_isa,
2869 build_list->opcode);
2870
2871 break;
2872 case INSTR_LITERAL_DEF:
2873 case INSTR_LABEL_DEF:
2874 default:
2875 break;
2876 }
2877 }
2878 if (this_size > max_size)
2879 max_size = this_size;
2880 }
2881 return max_size;
2882 }
2883
2884
2885 /* Return the maximum number of literal bytes this opcode can generate. */
2886
2887 int
2888 xg_get_max_insn_widen_literal_size (opcode)
2889 xtensa_opcode opcode;
2890 {
2891 TransitionTable *table = xg_build_widen_table ();
2892 TransitionList *l;
2893 int max_size = 0;
2894
2895 assert (opcode < table->num_opcodes);
2896
2897 for (l = table->table[opcode]; l != NULL; l = l->next)
2898 {
2899 TransitionRule *rule = l->rule;
2900 BuildInstr *build_list;
2901 int this_size = 0;
2902
2903 if (!rule)
2904 continue;
2905 build_list = rule->to_instr;
2906 if (is_unique_insn_expansion (rule))
2907 {
2908 assert (build_list->typ == INSTR_INSTR);
2909 this_size = xg_get_max_insn_widen_literal_size (build_list->opcode);
2910 }
2911 else
2912 for (; build_list != NULL; build_list = build_list->next)
2913 {
2914 switch (build_list->typ)
2915 {
2916 case INSTR_LITERAL_DEF:
2917 /* hard coded 4-byte literal. */
2918 this_size += 4;
2919 break;
2920 case INSTR_INSTR:
2921 case INSTR_LABEL_DEF:
2922 default:
2923 break;
2924 }
2925 }
2926 if (this_size > max_size)
2927 max_size = this_size;
2928 }
2929 return max_size;
2930 }
2931
2932
2933 bfd_boolean
2934 xg_is_relaxable_insn (insn, lateral_steps)
2935 TInsn *insn;
2936 int lateral_steps;
2937 {
2938 int steps_taken = 0;
2939 TransitionTable *table = xg_build_widen_table ();
2940 TransitionList *l;
2941
2942 assert (insn->insn_type == ITYPE_INSN);
2943 assert (insn->opcode < table->num_opcodes);
2944
2945 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
2946 {
2947 TransitionRule *rule = l->rule;
2948
2949 if (xg_instruction_matches_rule (insn, rule))
2950 {
2951 if (steps_taken == lateral_steps)
2952 return TRUE;
2953 steps_taken++;
2954 }
2955 }
2956 return FALSE;
2957 }
2958
2959
2960 static symbolS *
2961 get_special_literal_symbol ()
2962 {
2963 static symbolS *sym = NULL;
2964
2965 if (sym == NULL)
2966 sym = symbol_find_or_make ("SPECIAL_LITERAL0\001");
2967 return sym;
2968 }
2969
2970
2971 static symbolS *
2972 get_special_label_symbol ()
2973 {
2974 static symbolS *sym = NULL;
2975
2976 if (sym == NULL)
2977 sym = symbol_find_or_make ("SPECIAL_LABEL0\001");
2978 return sym;
2979 }
2980
2981
2982 /* Return true on success. */
2983
2984 bfd_boolean
2985 xg_build_to_insn (targ, insn, bi)
2986 TInsn *targ;
2987 TInsn *insn;
2988 BuildInstr *bi;
2989 {
2990 BuildOp *op;
2991 symbolS *sym;
2992
2993 memset (targ, 0, sizeof (TInsn));
2994 switch (bi->typ)
2995 {
2996 case INSTR_INSTR:
2997 op = bi->ops;
2998 targ->opcode = bi->opcode;
2999 targ->insn_type = ITYPE_INSN;
3000 targ->is_specific_opcode = FALSE;
3001
3002 for (; op != NULL; op = op->next)
3003 {
3004 int op_num = op->op_num;
3005 int op_data = op->op_data;
3006
3007 assert (op->op_num < MAX_INSN_ARGS);
3008
3009 if (targ->ntok <= op_num)
3010 targ->ntok = op_num + 1;
3011
3012 switch (op->typ)
3013 {
3014 case OP_CONSTANT:
3015 set_expr_const (&targ->tok[op_num], op_data);
3016 break;
3017 case OP_OPERAND:
3018 assert (op_data < insn->ntok);
3019 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3020 break;
3021 case OP_LITERAL:
3022 sym = get_special_literal_symbol ();
3023 set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
3024 break;
3025 case OP_LABEL:
3026 sym = get_special_label_symbol ();
3027 set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
3028 break;
3029 default:
3030 /* currently handles:
3031 OP_OPERAND_LOW8
3032 OP_OPERAND_HI24S
3033 OP_OPERAND_F32MINUS */
3034 if (xg_has_userdef_op_fn (op->typ))
3035 {
3036 assert (op_data < insn->ntok);
3037 if (expr_is_const (&insn->tok[op_data]))
3038 {
3039 long val;
3040 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3041 val = xg_apply_userdef_op_fn (op->typ,
3042 targ->tok[op_num].
3043 X_add_number);
3044 targ->tok[op_num].X_add_number = val;
3045 }
3046 else
3047 return FALSE; /* We cannot use a relocation for this. */
3048 break;
3049 }
3050 assert (0);
3051 break;
3052 }
3053 }
3054 break;
3055
3056 case INSTR_LITERAL_DEF:
3057 op = bi->ops;
3058 targ->opcode = XTENSA_UNDEFINED;
3059 targ->insn_type = ITYPE_LITERAL;
3060 targ->is_specific_opcode = FALSE;
3061 for (; op != NULL; op = op->next)
3062 {
3063 int op_num = op->op_num;
3064 int op_data = op->op_data;
3065 assert (op->op_num < MAX_INSN_ARGS);
3066
3067 if (targ->ntok <= op_num)
3068 targ->ntok = op_num + 1;
3069
3070 switch (op->typ)
3071 {
3072 case OP_OPERAND:
3073 assert (op_data < insn->ntok);
3074 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3075 break;
3076 case OP_LITERAL:
3077 case OP_CONSTANT:
3078 case OP_LABEL:
3079 default:
3080 assert (0);
3081 break;
3082 }
3083 }
3084 break;
3085
3086 case INSTR_LABEL_DEF:
3087 op = bi->ops;
3088 targ->opcode = XTENSA_UNDEFINED;
3089 targ->insn_type = ITYPE_LABEL;
3090 targ->is_specific_opcode = FALSE;
3091 /* Literal with no ops. is a label? */
3092 assert (op == NULL);
3093 break;
3094
3095 default:
3096 assert (0);
3097 }
3098
3099 return TRUE;
3100 }
3101
3102
3103 /* Return true on success. */
3104
3105 bfd_boolean
3106 xg_build_to_stack (istack, insn, bi)
3107 IStack *istack;
3108 TInsn *insn;
3109 BuildInstr *bi;
3110 {
3111 for (; bi != NULL; bi = bi->next)
3112 {
3113 TInsn *next_insn = istack_push_space (istack);
3114
3115 if (!xg_build_to_insn (next_insn, insn, bi))
3116 return FALSE;
3117 }
3118 return TRUE;
3119 }
3120
3121
3122 /* Return true on valid expansion. */
3123
3124 bfd_boolean
3125 xg_expand_to_stack (istack, insn, lateral_steps)
3126 IStack *istack;
3127 TInsn *insn;
3128 int lateral_steps;
3129 {
3130 int stack_size = istack->ninsn;
3131 int steps_taken = 0;
3132 TransitionTable *table = xg_build_widen_table ();
3133 TransitionList *l;
3134
3135 assert (insn->insn_type == ITYPE_INSN);
3136 assert (insn->opcode < table->num_opcodes);
3137
3138 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3139 {
3140 TransitionRule *rule = l->rule;
3141
3142 if (xg_instruction_matches_rule (insn, rule))
3143 {
3144 if (lateral_steps == steps_taken)
3145 {
3146 int i;
3147
3148 /* This is it. Expand the rule to the stack. */
3149 if (!xg_build_to_stack (istack, insn, rule->to_instr))
3150 return FALSE;
3151
3152 /* Check to see if it fits. */
3153 for (i = stack_size; i < istack->ninsn; i++)
3154 {
3155 TInsn *insn = &istack->insn[i];
3156
3157 if (insn->insn_type == ITYPE_INSN
3158 && !tinsn_has_symbolic_operands (insn)
3159 && !xg_immeds_fit (insn))
3160 {
3161 istack->ninsn = stack_size;
3162 return FALSE;
3163 }
3164 }
3165 return TRUE;
3166 }
3167 steps_taken++;
3168 }
3169 }
3170 return FALSE;
3171 }
3172
3173
3174 bfd_boolean
3175 xg_expand_narrow (targ, insn)
3176 TInsn *targ;
3177 TInsn *insn;
3178 {
3179 TransitionTable *table = xg_build_widen_table ();
3180 TransitionList *l;
3181
3182 assert (insn->insn_type == ITYPE_INSN);
3183 assert (insn->opcode < table->num_opcodes);
3184
3185 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3186 {
3187 TransitionRule *rule = l->rule;
3188 if (xg_instruction_matches_rule (insn, rule)
3189 && is_unique_insn_expansion (rule))
3190 {
3191 /* Is it a larger instruction? */
3192 if (xg_get_insn_size (insn)
3193 <= xg_get_build_instr_size (rule->to_instr))
3194 {
3195 xg_build_to_insn (targ, insn, rule->to_instr);
3196 return FALSE;
3197 }
3198 }
3199 }
3200 return TRUE;
3201 }
3202
3203
3204 /* Assumes: All immeds are constants. Check that all constants fit
3205 into their immeds; return false if not. */
3206
3207 static bfd_boolean
3208 xg_immeds_fit (insn)
3209 const TInsn *insn;
3210 {
3211 int i;
3212
3213 int n = insn->ntok;
3214 assert (insn->insn_type == ITYPE_INSN);
3215 for (i = 0; i < n; ++i)
3216 {
3217 const expressionS *expr = &insn->tok[i];
3218 xtensa_operand opnd = xtensa_get_operand (xtensa_default_isa,
3219 insn->opcode, i);
3220 if (!operand_is_immed (opnd))
3221 continue;
3222
3223 switch (expr->X_op)
3224 {
3225 case O_register:
3226 case O_constant:
3227 {
3228 if (xg_check_operand (expr->X_add_number, opnd))
3229 return FALSE;
3230 }
3231 break;
3232 default:
3233 /* The symbol should have a fixup associated with it. */
3234 assert (FALSE);
3235 break;
3236 }
3237 }
3238 return TRUE;
3239 }
3240
3241
3242 /* This should only be called after we have an initial
3243 estimate of the addresses. */
3244
3245 static bfd_boolean
3246 xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch)
3247 const TInsn *insn;
3248 segT pc_seg;
3249 fragS *pc_frag;
3250 offsetT pc_offset;
3251 long stretch;
3252 {
3253 symbolS *symbolP;
3254 offsetT target, pc, new_offset;
3255 int i;
3256 int n = insn->ntok;
3257
3258 assert (insn->insn_type == ITYPE_INSN);
3259
3260 for (i = 0; i < n; ++i)
3261 {
3262 const expressionS *expr = &insn->tok[i];
3263 xtensa_operand opnd = xtensa_get_operand (xtensa_default_isa,
3264 insn->opcode, i);
3265 if (!operand_is_immed (opnd))
3266 continue;
3267
3268 switch (expr->X_op)
3269 {
3270 case O_register:
3271 case O_constant:
3272 if (xg_check_operand (expr->X_add_number, opnd))
3273 return FALSE;
3274 break;
3275
3276 case O_symbol:
3277 /* We only allow symbols for pc-relative stuff.
3278 If pc_frag == 0, then we don't have frag locations yet. */
3279 if (pc_frag == 0)
3280 return FALSE;
3281
3282 /* If it is PC-relative and the symbol is in the same segment as
3283 the PC.... */
3284 if (!xtensa_operand_isPCRelative (opnd)
3285 || S_GET_SEGMENT (expr->X_add_symbol) != pc_seg)
3286 return FALSE;
3287
3288 symbolP = expr->X_add_symbol;
3289 target = S_GET_VALUE (symbolP) + expr->X_add_number;
3290 pc = pc_frag->fr_address + pc_offset;
3291
3292 /* If frag has yet to be reached on this pass, assume it
3293 will move by STRETCH just as we did. If this is not so,
3294 it will be because some frag between grows, and that will
3295 force another pass. Beware zero-length frags. There
3296 should be a faster way to do this. */
3297
3298 if (stretch && is_dnrange (pc_frag, symbolP, stretch))
3299 target += stretch;
3300
3301 new_offset = xtensa_operand_do_reloc (opnd, target, pc);
3302 if (xg_check_operand (new_offset, opnd))
3303 return FALSE;
3304 break;
3305
3306 default:
3307 /* The symbol should have a fixup associated with it. */
3308 return FALSE;
3309 }
3310 }
3311
3312 return TRUE;
3313 }
3314
3315
3316 /* This will check to see if the value can be converted into the
3317 operand type. It will return true if it does not fit. */
3318
3319 static bfd_boolean
3320 xg_check_operand (value, operand)
3321 int32 value;
3322 xtensa_operand operand;
3323 {
3324 uint32 valbuf = value;
3325 return (xtensa_operand_encode (operand, &valbuf) != xtensa_encode_result_ok);
3326 }
3327
3328
3329 /* Check if a symbol is pointing to somewhere after
3330 the start frag, given that the segment has stretched
3331 by stretch during relaxation.
3332
3333 This is more complicated than it might appear at first blush
3334 because of the stretching that goes on. Here is how the check
3335 works:
3336
3337 If the symbol and the frag are in the same segment, then
3338 the symbol could be down range. Note that this function
3339 assumes that start_frag is in now_seg.
3340
3341 If the symbol is pointing to a frag with an address greater than
3342 than the start_frag's address, then it _could_ be down range.
3343
3344 The problem comes because target_frag may or may not have had
3345 stretch bytes added to its address already, depending on if it is
3346 before or after start frag. (And if we knew that, then we wouldn't
3347 need this function.) start_frag has definitely already had stretch
3348 bytes added to its address.
3349
3350 If target_frag's address hasn't been adjusted yet, then to
3351 determine if it comes after start_frag, we need to subtract
3352 stretch from start_frag's address.
3353
3354 If target_frag's address has been adjusted, then it might have
3355 been adjusted such that it comes after start_frag's address minus
3356 stretch bytes.
3357
3358 So, in that case, we scan for it down stream to within
3359 stretch bytes. We could search to the end of the fr_chain, but
3360 that ends up taking too much time (over a minute on some gnu
3361 tests). */
3362
3363 int
3364 is_dnrange (start_frag, sym, stretch)
3365 fragS *start_frag;
3366 symbolS *sym;
3367 long stretch;
3368 {
3369 if (S_GET_SEGMENT (sym) == now_seg)
3370 {
3371 fragS *cur_frag = symbol_get_frag (sym);
3372
3373 if (cur_frag->fr_address >= start_frag->fr_address - stretch)
3374 {
3375 int distance = stretch;
3376
3377 while (cur_frag && distance >= 0)
3378 {
3379 distance -= cur_frag->fr_fix;
3380 if (cur_frag == start_frag)
3381 return 0;
3382 cur_frag = cur_frag->fr_next;
3383 }
3384 return 1;
3385 }
3386 }
3387 return 0;
3388 }
3389
3390 \f
3391 /* Relax the assembly instruction at least "min_steps".
3392 Return the number of steps taken. */
3393
3394 int
3395 xg_assembly_relax (istack, insn, pc_seg, pc_frag, pc_offset, min_steps,
3396 stretch)
3397 IStack *istack;
3398 TInsn *insn;
3399 segT pc_seg;
3400 fragS *pc_frag; /* If pc_frag == 0, then no pc-relative. */
3401 offsetT pc_offset; /* Offset in fragment. */
3402 int min_steps; /* Minimum number of conversion steps. */
3403 long stretch; /* Number of bytes stretched so far. */
3404 {
3405 int steps_taken = 0;
3406
3407 /* assert (has no symbolic operands)
3408 Some of its immeds don't fit.
3409 Try to build a relaxed version.
3410 This may go through a couple of stages
3411 of single instruction transformations before
3412 we get there. */
3413
3414 TInsn single_target;
3415 TInsn current_insn;
3416 int lateral_steps = 0;
3417 int istack_size = istack->ninsn;
3418
3419 if (xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch)
3420 && steps_taken >= min_steps)
3421 {
3422 istack_push (istack, insn);
3423 return steps_taken;
3424 }
3425 tinsn_copy (&current_insn, insn);
3426
3427 /* Walk through all of the single instruction expansions. */
3428 while (xg_is_single_relaxable_insn (&current_insn))
3429 {
3430 int error_val = xg_expand_narrow (&single_target, &current_insn);
3431
3432 assert (!error_val);
3433
3434 if (xg_symbolic_immeds_fit (&single_target, pc_seg, pc_frag, pc_offset,
3435 stretch))
3436 {
3437 steps_taken++;
3438 if (steps_taken >= min_steps)
3439 {
3440 istack_push (istack, &single_target);
3441 return steps_taken;
3442 }
3443 }
3444 tinsn_copy (&current_insn, &single_target);
3445 }
3446
3447 /* Now check for a multi-instruction expansion. */
3448 while (xg_is_relaxable_insn (&current_insn, lateral_steps))
3449 {
3450 if (xg_symbolic_immeds_fit (&current_insn, pc_seg, pc_frag, pc_offset,
3451 stretch))
3452 {
3453 if (steps_taken >= min_steps)
3454 {
3455 istack_push (istack, &current_insn);
3456 return steps_taken;
3457 }
3458 }
3459 steps_taken++;
3460 if (xg_expand_to_stack (istack, &current_insn, lateral_steps))
3461 {
3462 if (steps_taken >= min_steps)
3463 return steps_taken;
3464 }
3465 lateral_steps++;
3466 istack->ninsn = istack_size;
3467 }
3468
3469 /* It's not going to work -- use the original. */
3470 istack_push (istack, insn);
3471 return steps_taken;
3472 }
3473
3474
3475 static void
3476 xg_force_frag_space (size)
3477 int size;
3478 {
3479 /* This may have the side effect of creating a new fragment for the
3480 space to go into. I just do not like the name of the "frag"
3481 functions. */
3482 frag_grow (size);
3483 }
3484
3485
3486 void
3487 xg_finish_frag (last_insn, state, max_growth, is_insn)
3488 char *last_insn;
3489 enum xtensa_relax_statesE state;
3490 int max_growth;
3491 bfd_boolean is_insn;
3492 {
3493 /* Finish off this fragment so that it has at LEAST the desired
3494 max_growth. If it doesn't fit in this fragment, close this one
3495 and start a new one. In either case, return a pointer to the
3496 beginning of the growth area. */
3497
3498 fragS *old_frag;
3499 xg_force_frag_space (max_growth);
3500
3501 old_frag = frag_now;
3502
3503 frag_now->fr_opcode = last_insn;
3504 if (is_insn)
3505 frag_now->tc_frag_data.is_insn = TRUE;
3506
3507 frag_var (rs_machine_dependent, max_growth, max_growth,
3508 state, frag_now->fr_symbol, frag_now->fr_offset, last_insn);
3509
3510 /* Just to make sure that we did not split it up. */
3511 assert (old_frag->fr_next == frag_now);
3512 }
3513
3514
3515 static bfd_boolean
3516 is_branch_jmp_to_next (insn, fragP)
3517 TInsn *insn;
3518 fragS *fragP;
3519 {
3520 xtensa_isa isa = xtensa_default_isa;
3521 int i;
3522 int num_ops = xtensa_num_operands (isa, insn->opcode);
3523 int target_op = -1;
3524 symbolS *sym;
3525 fragS *target_frag;
3526
3527 if (is_loop_opcode (insn->opcode))
3528 return FALSE;
3529
3530 for (i = 0; i < num_ops; i++)
3531 {
3532 xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
3533 char *kind = xtensa_operand_kind (opnd);
3534 if (strlen (kind) == 1 && *kind == 'l')
3535 {
3536 target_op = i;
3537 break;
3538 }
3539 }
3540 if (target_op == -1)
3541 return FALSE;
3542
3543 if (insn->ntok <= target_op)
3544 return FALSE;
3545
3546 if (insn->tok[target_op].X_op != O_symbol)
3547 return FALSE;
3548
3549 sym = insn->tok[target_op].X_add_symbol;
3550 if (sym == NULL)
3551 return FALSE;
3552
3553 if (insn->tok[target_op].X_add_number != 0)
3554 return FALSE;
3555
3556 target_frag = symbol_get_frag (sym);
3557 if (target_frag == NULL)
3558 return FALSE;
3559
3560 if (is_next_frag_target (fragP->fr_next, target_frag)
3561 && S_GET_VALUE (sym) == target_frag->fr_address)
3562 return TRUE;
3563
3564 return FALSE;
3565 }
3566
3567
3568 static void
3569 xg_add_branch_and_loop_targets (insn)
3570 TInsn *insn;
3571 {
3572 xtensa_isa isa = xtensa_default_isa;
3573 int num_ops = xtensa_num_operands (isa, insn->opcode);
3574
3575 if (is_loop_opcode (insn->opcode))
3576 {
3577 int i = 1;
3578 xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
3579 char *kind = xtensa_operand_kind (opnd);
3580 if (strlen (kind) == 1 && *kind == 'l')
3581 if (insn->tok[i].X_op == O_symbol)
3582 add_target_symbol (insn->tok[i].X_add_symbol, TRUE);
3583 return;
3584 }
3585
3586 /* Currently, we do not add branch targets. This is an optimization
3587 for later that tries to align only branch targets, not just any
3588 label in a text section. */
3589
3590 if (align_only_targets)
3591 {
3592 int i;
3593
3594 for (i = 0; i < insn->ntok && i < num_ops; i++)
3595 {
3596 xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
3597 char *kind = xtensa_operand_kind (opnd);
3598 if (strlen (kind) == 1 && *kind == 'l'
3599 && insn->tok[i].X_op == O_symbol)
3600 add_target_symbol (insn->tok[i].X_add_symbol, FALSE);
3601 }
3602 }
3603 }
3604
3605
3606 /* Return the transition rule that matches or NULL if none matches. */
3607
3608 bfd_boolean
3609 xg_instruction_matches_rule (insn, rule)
3610 TInsn *insn;
3611 TransitionRule *rule;
3612 {
3613 PreconditionList *condition_l;
3614
3615 if (rule->opcode != insn->opcode)
3616 return FALSE;
3617
3618 for (condition_l = rule->conditions;
3619 condition_l != NULL;
3620 condition_l = condition_l->next)
3621 {
3622 expressionS *exp1;
3623 expressionS *exp2;
3624 Precondition *cond = condition_l->precond;
3625
3626 switch (cond->typ)
3627 {
3628 case OP_CONSTANT:
3629 /* The expression must be the constant. */
3630 assert (cond->op_num < insn->ntok);
3631 exp1 = &insn->tok[cond->op_num];
3632 if (!expr_is_const (exp1))
3633 return FALSE;
3634 switch (cond->cmp)
3635 {
3636 case OP_EQUAL:
3637 if (get_expr_const (exp1) != cond->op_data)
3638 return FALSE;
3639 break;
3640 case OP_NOTEQUAL:
3641 if (get_expr_const (exp1) == cond->op_data)
3642 return FALSE;
3643 break;
3644 }
3645 break;
3646
3647 case OP_OPERAND:
3648 assert (cond->op_num < insn->ntok);
3649 assert (cond->op_data < insn->ntok);
3650 exp1 = &insn->tok[cond->op_num];
3651 exp2 = &insn->tok[cond->op_data];
3652
3653 switch (cond->cmp)
3654 {
3655 case OP_EQUAL:
3656 if (!expr_is_equal (exp1, exp2))
3657 return FALSE;
3658 break;
3659 case OP_NOTEQUAL:
3660 if (expr_is_equal (exp1, exp2))
3661 return FALSE;
3662 break;
3663 }
3664 break;
3665
3666 case OP_LITERAL:
3667 case OP_LABEL:
3668 default:
3669 return FALSE;
3670 }
3671 }
3672 return TRUE;
3673 }
3674
3675
3676 TransitionRule *
3677 xg_instruction_match (insn)
3678 TInsn *insn;
3679 {
3680 TransitionTable *table = xg_build_simplify_table ();
3681 TransitionList *l;
3682 assert (insn->opcode < table->num_opcodes);
3683
3684 /* Walk through all of the possible transitions. */
3685 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3686 {
3687 TransitionRule *rule = l->rule;
3688 if (xg_instruction_matches_rule (insn, rule))
3689 return rule;
3690 }
3691 return NULL;
3692 }
3693
3694
3695 /* Return false if no error. */
3696
3697 bfd_boolean
3698 xg_build_token_insn (instr_spec, old_insn, new_insn)
3699 BuildInstr *instr_spec;
3700 TInsn *old_insn;
3701 TInsn *new_insn;
3702 {
3703 int num_ops = 0;
3704 BuildOp *b_op;
3705
3706 switch (instr_spec->typ)
3707 {
3708 case INSTR_INSTR:
3709 new_insn->insn_type = ITYPE_INSN;
3710 new_insn->opcode = instr_spec->opcode;
3711 new_insn->is_specific_opcode = FALSE;
3712 break;
3713 case INSTR_LITERAL_DEF:
3714 new_insn->insn_type = ITYPE_LITERAL;
3715 new_insn->opcode = XTENSA_UNDEFINED;
3716 new_insn->is_specific_opcode = FALSE;
3717 break;
3718 case INSTR_LABEL_DEF:
3719 as_bad (_("INSTR_LABEL_DEF not supported yet"));
3720 break;
3721 }
3722
3723 for (b_op = instr_spec->ops; b_op != NULL; b_op = b_op->next)
3724 {
3725 expressionS *exp;
3726 const expressionS *src_exp;
3727
3728 num_ops++;
3729 switch (b_op->typ)
3730 {
3731 case OP_CONSTANT:
3732 /* The expression must be the constant. */
3733 assert (b_op->op_num < MAX_INSN_ARGS);
3734 exp = &new_insn->tok[b_op->op_num];
3735 set_expr_const (exp, b_op->op_data);
3736 break;
3737
3738 case OP_OPERAND:
3739 assert (b_op->op_num < MAX_INSN_ARGS);
3740 assert (b_op->op_data < (unsigned) old_insn->ntok);
3741 src_exp = &old_insn->tok[b_op->op_data];
3742 exp = &new_insn->tok[b_op->op_num];
3743 copy_expr (exp, src_exp);
3744 break;
3745
3746 case OP_LITERAL:
3747 case OP_LABEL:
3748 as_bad (_("can't handle generation of literal/labels yet"));
3749 assert (0);
3750
3751 default:
3752 as_bad (_("can't handle undefined OP TYPE"));
3753 assert (0);
3754 }
3755 }
3756
3757 new_insn->ntok = num_ops;
3758 return FALSE;
3759 }
3760
3761
3762 /* Return true if it was simplified. */
3763
3764 bfd_boolean
3765 xg_simplify_insn (old_insn, new_insn)
3766 TInsn *old_insn;
3767 TInsn *new_insn;
3768 {
3769 TransitionRule *rule = xg_instruction_match (old_insn);
3770 BuildInstr *insn_spec;
3771 if (rule == NULL)
3772 return FALSE;
3773
3774 insn_spec = rule->to_instr;
3775 /* There should only be one. */
3776 assert (insn_spec != NULL);
3777 assert (insn_spec->next == NULL);
3778 if (insn_spec->next != NULL)
3779 return FALSE;
3780
3781 xg_build_token_insn (insn_spec, old_insn, new_insn);
3782
3783 return TRUE;
3784 }
3785
3786
3787 /* xg_expand_assembly_insn: (1) Simplify the instruction, i.e., l32i ->
3788 l32i.n. (2) Check the number of operands. (3) Place the instruction
3789 tokens into the stack or if we can relax it at assembly time, place
3790 multiple instructions/literals onto the stack. Return false if no
3791 error. */
3792
3793 static bfd_boolean
3794 xg_expand_assembly_insn (istack, orig_insn)
3795 IStack *istack;
3796 TInsn *orig_insn;
3797 {
3798 int noperands;
3799 TInsn new_insn;
3800 memset (&new_insn, 0, sizeof (TInsn));
3801
3802 /* On return, we will be using the "use_tokens" with "use_ntok".
3803 This will reduce things like addi to addi.n. */
3804 if (code_density_available () && !orig_insn->is_specific_opcode)
3805 {
3806 if (xg_simplify_insn (orig_insn, &new_insn))
3807 orig_insn = &new_insn;
3808 }
3809
3810 noperands = xtensa_num_operands (xtensa_default_isa, orig_insn->opcode);
3811 if (orig_insn->ntok < noperands)
3812 {
3813 as_bad (_("found %d operands for '%s': Expected %d"),
3814 orig_insn->ntok,
3815 xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
3816 noperands);
3817 return TRUE;
3818 }
3819 if (orig_insn->ntok > noperands)
3820 as_warn (_("found too many (%d) operands for '%s': Expected %d"),
3821 orig_insn->ntok,
3822 xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
3823 noperands);
3824
3825 /* If there are not enough operands, we will assert above. If there
3826 are too many, just cut out the extras here. */
3827
3828 orig_insn->ntok = noperands;
3829
3830 /* Cases:
3831
3832 Instructions with all constant immeds:
3833 Assemble them and relax the instruction if possible.
3834 Give error if not possible; no fixup needed.
3835
3836 Instructions with symbolic immeds:
3837 Assemble them with a Fix up (that may cause instruction expansion).
3838 Also close out the fragment if the fixup may cause instruction expansion.
3839
3840 There are some other special cases where we need alignment.
3841 1) before certain instructions with required alignment (OPCODE_ALIGN)
3842 2) before labels that have jumps (LABEL_ALIGN)
3843 3) after call instructions (RETURN_ALIGN)
3844 Multiple of these may be possible on the same fragment.
3845 If so, make sure to satisfy the required alignment.
3846 Then try to get the desired alignment. */
3847
3848 if (tinsn_has_invalid_symbolic_operands (orig_insn))
3849 return TRUE;
3850
3851 if (orig_insn->is_specific_opcode || !can_relax ())
3852 {
3853 istack_push (istack, orig_insn);
3854 return FALSE;
3855 }
3856
3857 if (tinsn_has_symbolic_operands (orig_insn))
3858 {
3859 if (tinsn_has_complex_operands (orig_insn))
3860 xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0);
3861 else
3862 istack_push (istack, orig_insn);
3863 }
3864 else
3865 {
3866 if (xg_immeds_fit (orig_insn))
3867 istack_push (istack, orig_insn);
3868 else
3869 xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0);
3870 }
3871
3872 #if 0
3873 for (i = 0; i < istack->ninsn; i++)
3874 {
3875 if (xg_simplify_insn (&new_insn, &istack->insn[i]))
3876 istack->insn[i] = new_insn;
3877 }
3878 #endif
3879
3880 return FALSE;
3881 }
3882
3883
3884 /* Currently all literals that are generated here are 32-bit L32R targets. */
3885
3886 symbolS *
3887 xg_assemble_literal (insn)
3888 /* const */ TInsn *insn;
3889 {
3890 emit_state state;
3891 symbolS *lit_sym = NULL;
3892
3893 /* size = 4 for L32R. It could easily be larger when we move to
3894 larger constants. Add a parameter later. */
3895 offsetT litsize = 4;
3896 offsetT litalign = 2; /* 2^2 = 4 */
3897 expressionS saved_loc;
3898 set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ());
3899
3900 assert (insn->insn_type == ITYPE_LITERAL);
3901 assert (insn->ntok = 1); /* must be only one token here */
3902
3903 xtensa_switch_to_literal_fragment (&state);
3904
3905 /* Force a 4-byte align here. Note that this opens a new frag, so all
3906 literals done with this function have a frag to themselves. That's
3907 important for the way text section literals work. */
3908 frag_align (litalign, 0, 0);
3909
3910 emit_expr (&insn->tok[0], litsize);
3911
3912 assert (frag_now->tc_frag_data.literal_frag == NULL);
3913 frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
3914 frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
3915 lit_sym = frag_now->fr_symbol;
3916 frag_now->tc_frag_data.is_literal = TRUE;
3917
3918 /* Go back. */
3919 xtensa_restore_emit_state (&state);
3920 return lit_sym;
3921 }
3922
3923
3924 static void
3925 xg_assemble_literal_space (size)
3926 /* const */ int size;
3927 {
3928 emit_state state;
3929 /* We might have to do something about this alignment. It only
3930 takes effect if something is placed here. */
3931 offsetT litalign = 2; /* 2^2 = 4 */
3932 fragS *lit_saved_frag;
3933
3934 expressionS saved_loc;
3935
3936 assert (size % 4 == 0);
3937 set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ());
3938
3939 xtensa_switch_to_literal_fragment (&state);
3940
3941 /* Force a 4-byte align here. */
3942 frag_align (litalign, 0, 0);
3943
3944 xg_force_frag_space (size);
3945
3946 lit_saved_frag = frag_now;
3947 frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
3948 frag_now->tc_frag_data.is_literal = TRUE;
3949 frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
3950 xg_finish_frag (0, RELAX_LITERAL, size, FALSE);
3951
3952 /* Go back. */
3953 xtensa_restore_emit_state (&state);
3954 frag_now->tc_frag_data.literal_frag = lit_saved_frag;
3955 }
3956
3957
3958 symbolS *
3959 xtensa_create_literal_symbol (sec, frag)
3960 segT sec;
3961 fragS *frag;
3962 {
3963 static int lit_num = 0;
3964 static char name[256];
3965 symbolS *fragSym;
3966
3967 sprintf (name, ".L_lit_sym%d", lit_num);
3968 fragSym = xtensa_create_local_symbol (stdoutput, name, sec, 0, frag_now);
3969
3970 frag->tc_frag_data.is_literal = TRUE;
3971 lit_num++;
3972 return fragSym;
3973 }
3974
3975
3976 /* Create a local symbol. If it is in a linkonce section, we have to
3977 be careful to make sure that if it is used in a relocation that the
3978 symbol will be in the output file. */
3979
3980 symbolS *
3981 xtensa_create_local_symbol (abfd, name, sec, value, frag)
3982 bfd *abfd;
3983 const char *name;
3984 segT sec;
3985 valueT value;
3986 fragS *frag;
3987 {
3988 symbolS *symbolP;
3989
3990 if (get_is_linkonce_section (abfd, sec))
3991 {
3992 symbolP = symbol_new (name, sec, value, frag);
3993 S_CLEAR_EXTERNAL (symbolP);
3994 /* symbolP->local = 1; */
3995 }
3996 else
3997 symbolP = symbol_new (name, sec, value, frag);
3998
3999 return symbolP;
4000 }
4001
4002
4003 /* Return true if the section flags are marked linkonce
4004 or the name is .gnu.linkonce*. */
4005
4006 bfd_boolean
4007 get_is_linkonce_section (abfd, sec)
4008 bfd *abfd ATTRIBUTE_UNUSED;
4009 segT sec;
4010 {
4011 flagword flags, link_once_flags;
4012
4013 flags = bfd_get_section_flags (abfd, sec);
4014 link_once_flags = (flags & SEC_LINK_ONCE);
4015
4016 /* Flags might not be set yet. */
4017 if (!link_once_flags)
4018 {
4019 static size_t len = sizeof ".gnu.linkonce.t.";
4020
4021 if (strncmp (segment_name (sec), ".gnu.linkonce.t.", len - 1) == 0)
4022 link_once_flags = SEC_LINK_ONCE;
4023 }
4024 return (link_once_flags != 0);
4025 }
4026
4027
4028 /* Emit an instruction to the current fragment. If record_fix is true,
4029 then this instruction will not change and we can go ahead and record
4030 the fixup. If record_fix is false, then the instruction may change
4031 and we are going to close out this fragment. Go ahead and set the
4032 fr_symbol and fr_offset instead of adding a fixup. */
4033
4034 static bfd_boolean
4035 xg_emit_insn (t_insn, record_fix)
4036 TInsn *t_insn;
4037 bfd_boolean record_fix;
4038 {
4039 bfd_boolean ok = TRUE;
4040 xtensa_isa isa = xtensa_default_isa;
4041 xtensa_opcode opcode = t_insn->opcode;
4042 bfd_boolean has_fixup = FALSE;
4043 int noperands;
4044 int i, byte_count;
4045 fragS *oldfrag;
4046 size_t old_size;
4047 char *f;
4048 static xtensa_insnbuf insnbuf = NULL;
4049
4050 /* Use a static pointer to the insn buffer so we don't have to call
4051 malloc each time through. */
4052 if (!insnbuf)
4053 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4054
4055 has_fixup = tinsn_to_insnbuf (t_insn, insnbuf);
4056
4057 noperands = xtensa_num_operands (isa, opcode);
4058 assert (noperands == t_insn->ntok);
4059
4060 byte_count = xtensa_insn_length (isa, opcode);
4061 oldfrag = frag_now;
4062 /* This should NEVER cause us to jump into a new frag;
4063 we've already reserved space. */
4064 old_size = frag_now_fix ();
4065 f = frag_more (byte_count);
4066 assert (oldfrag == frag_now);
4067
4068 /* This needs to generate a record that lists the parts that are
4069 instructions. */
4070 if (!frag_now->tc_frag_data.is_insn)
4071 {
4072 /* If we are at the beginning of a fragment, switch this
4073 fragment to an instruction fragment. */
4074 if (now_seg != absolute_section && old_size != 0)
4075 as_warn (_("instruction fragment may contain data"));
4076 frag_now->tc_frag_data.is_insn = TRUE;
4077 }
4078
4079 xtensa_insnbuf_to_chars (isa, insnbuf, f);
4080
4081 /* dwarf2_emit_insn (byte_count); */
4082
4083 /* Now spit out the opcode fixup.... */
4084 if (!has_fixup)
4085 return !ok;
4086
4087 for (i = 0; i < noperands; ++i)
4088 {
4089 expressionS *expr = &t_insn->tok[i];
4090 switch (expr->X_op)
4091 {
4092 case O_symbol:
4093 if (get_relaxable_immed (opcode) == i)
4094 {
4095 if (record_fix)
4096 {
4097 if (!xg_add_opcode_fix (opcode, i, expr, frag_now,
4098 f - frag_now->fr_literal))
4099 ok = FALSE;
4100 }
4101 else
4102 {
4103 /* Write it to the fr_offset, fr_symbol. */
4104 frag_now->fr_symbol = expr->X_add_symbol;
4105 frag_now->fr_offset = expr->X_add_number;
4106 }
4107 }
4108 else
4109 {
4110 as_bad (_("invalid operand %d on '%s'"),
4111 i, xtensa_opcode_name (isa, opcode));
4112 ok = FALSE;
4113 }
4114 break;
4115
4116 case O_constant:
4117 case O_register:
4118 break;
4119
4120 default:
4121 as_bad (_("invalid expression for operand %d on '%s'"),
4122 i, xtensa_opcode_name (isa, opcode));
4123 ok = FALSE;
4124 break;
4125 }
4126 }
4127
4128 return !ok;
4129 }
4130
4131
4132 static bfd_boolean
4133 xg_emit_insn_to_buf (t_insn, buf, fragP, offset, build_fix)
4134 TInsn *t_insn;
4135 char *buf;
4136 fragS *fragP;
4137 offsetT offset;
4138 bfd_boolean build_fix;
4139 {
4140 static xtensa_insnbuf insnbuf = NULL;
4141 bfd_boolean has_symbolic_immed = FALSE;
4142 bfd_boolean ok = TRUE;
4143 if (!insnbuf)
4144 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4145
4146 has_symbolic_immed = tinsn_to_insnbuf (t_insn, insnbuf);
4147 if (has_symbolic_immed && build_fix)
4148 {
4149 /* Add a fixup. */
4150 int opnum = get_relaxable_immed (t_insn->opcode);
4151 expressionS *exp = &t_insn->tok[opnum];
4152
4153 if (!xg_add_opcode_fix (t_insn->opcode,
4154 opnum, exp, fragP, offset))
4155 ok = FALSE;
4156 }
4157 fragP->tc_frag_data.is_insn = TRUE;
4158 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf);
4159 return ok;
4160 }
4161
4162
4163 /* Put in a fixup record based on the opcode.
4164 Return true on success. */
4165
4166 bfd_boolean
4167 xg_add_opcode_fix (opcode, opnum, expr, fragP, offset)
4168 xtensa_opcode opcode;
4169 int opnum;
4170 expressionS *expr;
4171 fragS *fragP;
4172 offsetT offset;
4173 {
4174 bfd_reloc_code_real_type reloc;
4175 reloc_howto_type *howto;
4176 int insn_length;
4177 fixS *the_fix;
4178
4179 reloc = opnum_to_reloc (opnum);
4180 if (reloc == BFD_RELOC_NONE)
4181 {
4182 as_bad (_("invalid relocation operand %i on '%s'"),
4183 opnum, xtensa_opcode_name (xtensa_default_isa, opcode));
4184 return FALSE;
4185 }
4186
4187 howto = bfd_reloc_type_lookup (stdoutput, reloc);
4188
4189 if (!howto)
4190 {
4191 as_bad (_("undefined symbol for opcode \"%s\"."),
4192 xtensa_opcode_name (xtensa_default_isa, opcode));
4193 return FALSE;
4194 }
4195
4196 insn_length = xtensa_insn_length (xtensa_default_isa, opcode);
4197 the_fix = fix_new_exp (fragP, offset, insn_length, expr,
4198 howto->pc_relative, reloc);
4199
4200 if (expr->X_add_symbol &&
4201 (S_IS_EXTERNAL (expr->X_add_symbol) || S_IS_WEAK (expr->X_add_symbol)))
4202 the_fix->fx_plt = TRUE;
4203
4204 return TRUE;
4205 }
4206
4207
4208 void
4209 xg_resolve_literals (insn, lit_sym)
4210 TInsn *insn;
4211 symbolS *lit_sym;
4212 {
4213 symbolS *sym = get_special_literal_symbol ();
4214 int i;
4215 if (lit_sym == 0)
4216 return;
4217 assert (insn->insn_type == ITYPE_INSN);
4218 for (i = 0; i < insn->ntok; i++)
4219 if (insn->tok[i].X_add_symbol == sym)
4220 insn->tok[i].X_add_symbol = lit_sym;
4221
4222 }
4223
4224
4225 void
4226 xg_resolve_labels (insn, label_sym)
4227 TInsn *insn;
4228 symbolS *label_sym;
4229 {
4230 symbolS *sym = get_special_label_symbol ();
4231 int i;
4232 /* assert(!insn->is_literal); */
4233 for (i = 0; i < insn->ntok; i++)
4234 if (insn->tok[i].X_add_symbol == sym)
4235 insn->tok[i].X_add_symbol = label_sym;
4236
4237 }
4238
4239
4240 static void
4241 xg_assemble_tokens (insn)
4242 /*const */ TInsn *insn;
4243 {
4244 /* By the time we get here, there's not too much left to do.
4245 1) Check our assumptions.
4246 2) Check if the current instruction is "narrow".
4247 If so, then finish the frag, create another one.
4248 We could also go back to change some previous
4249 "narrow" frags into no-change ones if we have more than
4250 MAX_NARROW_ALIGNMENT of them without alignment restrictions
4251 between them.
4252
4253 Cases:
4254 1) It has constant operands and doesn't fit.
4255 Go ahead and assemble it so it will fail.
4256 2) It has constant operands that fit.
4257 If narrow and !is_specific_opcode,
4258 assemble it and put in a relocation
4259 else
4260 assemble it.
4261 3) It has a symbolic immediate operand
4262 a) Find the worst-case relaxation required
4263 b) Find the worst-case literal pool space required.
4264 Insert appropriate alignment & space in the literal.
4265 Assemble it.
4266 Add the relocation. */
4267
4268 assert (insn->insn_type == ITYPE_INSN);
4269
4270 if (!tinsn_has_symbolic_operands (insn))
4271 {
4272 if (xg_is_narrow_insn (insn) && !insn->is_specific_opcode)
4273 {
4274 /* assemble it but add max required space */
4275 int max_size = xg_get_max_narrow_insn_size (insn->opcode);
4276 int min_size = xg_get_insn_size (insn);
4277 char *last_insn;
4278 assert (max_size == 3);
4279 /* make sure we have enough space to widen it */
4280 xg_force_frag_space (max_size);
4281 /* Output the instruction. It may cause an error if some
4282 operands do not fit. */
4283 last_insn = frag_more (0);
4284 if (xg_emit_insn (insn, TRUE))
4285 as_warn (_("instruction with constant operands does not fit"));
4286 xg_finish_frag (last_insn, RELAX_NARROW, max_size - min_size, TRUE);
4287 }
4288 else
4289 {
4290 /* Assemble it. No relocation needed. */
4291 int max_size = xg_get_insn_size (insn);
4292 xg_force_frag_space (max_size);
4293 if (xg_emit_insn (insn, FALSE))
4294 as_warn (_("instruction with constant operands does not "
4295 "fit without widening"));
4296 /* frag_more (max_size); */
4297
4298 /* Special case for jx. If the jx is the next to last
4299 instruction in a loop, we will add a NOP after it. This
4300 avoids a hardware issue that could occur if the jx jumped
4301 to the next instruction. */
4302 if (software_avoid_b_j_loop_end
4303 && is_jx_opcode (insn->opcode))
4304 {
4305 maybe_has_b_j_loop_end = TRUE;
4306 /* add 2 of these */
4307 frag_now->tc_frag_data.is_insn = TRUE;
4308 frag_var (rs_machine_dependent, 4, 4,
4309 RELAX_ADD_NOP_IF_PRE_LOOP_END,
4310 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4311 }
4312 }
4313 }
4314 else
4315 {
4316 /* Need to assemble it with space for the relocation. */
4317 if (!insn->is_specific_opcode)
4318 {
4319 /* Assemble it but add max required space. */
4320 char *last_insn;
4321 int min_size = xg_get_insn_size (insn);
4322 int max_size = xg_get_max_insn_widen_size (insn->opcode);
4323 int max_literal_size =
4324 xg_get_max_insn_widen_literal_size (insn->opcode);
4325
4326 #if 0
4327 symbolS *immed_sym = xg_get_insn_immed_symbol (insn);
4328 set_frag_segment (frag_now, now_seg);
4329 #endif /* 0 */
4330
4331 /* Make sure we have enough space to widen the instruction.
4332 This may open a new fragment. */
4333 xg_force_frag_space (max_size);
4334 if (max_literal_size != 0)
4335 xg_assemble_literal_space (max_literal_size);
4336
4337 /* Output the instruction. It may cause an error if some
4338 operands do not fit. Emit the incomplete instruction. */
4339 last_insn = frag_more (0);
4340 xg_emit_insn (insn, FALSE);
4341
4342 xg_finish_frag (last_insn, RELAX_IMMED, max_size - min_size, TRUE);
4343
4344 /* Special cases for loops:
4345 close_loop_end should be inserted AFTER short_loop.
4346 Make sure that CLOSE loops are processed BEFORE short_loops
4347 when converting them. */
4348
4349 /* "short_loop": add a NOP if the loop is < 4 bytes. */
4350 if (software_avoid_short_loop
4351 && is_loop_opcode (insn->opcode))
4352 {
4353 maybe_has_short_loop = TRUE;
4354 frag_now->tc_frag_data.is_insn = TRUE;
4355 frag_var (rs_machine_dependent, 4, 4,
4356 RELAX_ADD_NOP_IF_SHORT_LOOP,
4357 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4358 frag_now->tc_frag_data.is_insn = TRUE;
4359 frag_var (rs_machine_dependent, 4, 4,
4360 RELAX_ADD_NOP_IF_SHORT_LOOP,
4361 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4362 }
4363
4364 /* "close_loop_end": Add up to 12 bytes of NOPs to keep a
4365 loop at least 12 bytes away from another loop's loop
4366 end. */
4367 if (software_avoid_close_loop_end
4368 && is_loop_opcode (insn->opcode))
4369 {
4370 maybe_has_close_loop_end = TRUE;
4371 frag_now->tc_frag_data.is_insn = TRUE;
4372 frag_var (rs_machine_dependent, 12, 12,
4373 RELAX_ADD_NOP_IF_CLOSE_LOOP_END,
4374 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4375 }
4376 }
4377 else
4378 {
4379 /* Assemble it in place. No expansion will be required,
4380 but we'll still need a relocation record. */
4381 int max_size = xg_get_insn_size (insn);
4382 xg_force_frag_space (max_size);
4383 if (xg_emit_insn (insn, TRUE))
4384 as_warn (_("instruction's constant operands do not fit"));
4385 }
4386 }
4387 }
4388
4389
4390 /* Return true if the instruction can write to the specified
4391 integer register. */
4392
4393 static bfd_boolean
4394 is_register_writer (insn, regset, regnum)
4395 const TInsn *insn;
4396 const char *regset;
4397 int regnum;
4398 {
4399 int i;
4400 int num_ops;
4401 xtensa_isa isa = xtensa_default_isa;
4402
4403 num_ops = xtensa_num_operands (isa, insn->opcode);
4404
4405 for (i = 0; i < num_ops; i++)
4406 {
4407 xtensa_operand operand = xtensa_get_operand (isa, insn->opcode, i);
4408 char inout = xtensa_operand_inout (operand);
4409
4410 if (inout == '>' || inout == '=')
4411 {
4412 if (strcmp (xtensa_operand_kind (operand), regset) == 0)
4413 {
4414 if ((insn->tok[i].X_op == O_register)
4415 && (insn->tok[i].X_add_number == regnum))
4416 return TRUE;
4417 }
4418 }
4419 }
4420 return FALSE;
4421 }
4422
4423
4424 static bfd_boolean
4425 is_bad_loopend_opcode (tinsn)
4426 const TInsn * tinsn;
4427 {
4428 xtensa_opcode opcode = tinsn->opcode;
4429
4430 if (opcode == XTENSA_UNDEFINED)
4431 return FALSE;
4432
4433 if (opcode == xtensa_call0_opcode
4434 || opcode == xtensa_callx0_opcode
4435 || opcode == xtensa_call4_opcode
4436 || opcode == xtensa_callx4_opcode
4437 || opcode == xtensa_call8_opcode
4438 || opcode == xtensa_callx8_opcode
4439 || opcode == xtensa_call12_opcode
4440 || opcode == xtensa_callx12_opcode
4441 || opcode == xtensa_isync_opcode
4442 || opcode == xtensa_ret_opcode
4443 || opcode == xtensa_ret_n_opcode
4444 || opcode == xtensa_retw_opcode
4445 || opcode == xtensa_retw_n_opcode
4446 || opcode == xtensa_waiti_opcode)
4447 return TRUE;
4448
4449 /* An RSR of LCOUNT is illegal as the last opcode in a loop. */
4450 if (opcode == xtensa_rsr_opcode
4451 && tinsn->ntok >= 2
4452 && tinsn->tok[1].X_op == O_constant
4453 && tinsn->tok[1].X_add_number == 2)
4454 return TRUE;
4455
4456 return FALSE;
4457 }
4458
4459
4460 /* Labels that begin with ".Ln" or ".LM" are unaligned.
4461 This allows the debugger to add unaligned labels.
4462 Also, the assembler generates stabs labels that need
4463 not be aligned: FAKE_LABEL_NAME . {"F", "L", "endfunc"}. */
4464
4465 bfd_boolean
4466 is_unaligned_label (sym)
4467 symbolS *sym;
4468 {
4469 const char *name = S_GET_NAME (sym);
4470 static size_t fake_size = 0;
4471
4472 if (name
4473 && name[0] == '.'
4474 && name[1] == 'L' && (name[2] == 'n' || name[2] == 'M'))
4475 return TRUE;
4476
4477 /* FAKE_LABEL_NAME followed by "F", "L" or "endfunc" */
4478 if (fake_size == 0)
4479 fake_size = strlen (FAKE_LABEL_NAME);
4480
4481 if (name
4482 && strncmp (FAKE_LABEL_NAME, name, fake_size) == 0
4483 && (name[fake_size] == 'F'
4484 || name[fake_size] == 'L'
4485 || (name[fake_size] == 'e'
4486 && strncmp ("endfunc", name+fake_size, 7) == 0)))
4487 return TRUE;
4488
4489 return FALSE;
4490 }
4491
4492
4493 fragS *
4494 next_non_empty_frag (fragP)
4495 const fragS *fragP;
4496 {
4497 fragS *next_fragP = fragP->fr_next;
4498
4499 /* Sometimes an empty will end up here due storage allocation issues.
4500 So we have to skip until we find something legit. */
4501 while (next_fragP && next_fragP->fr_fix == 0)
4502 next_fragP = next_fragP->fr_next;
4503
4504 if (next_fragP == NULL || next_fragP->fr_fix == 0)
4505 return NULL;
4506
4507 return next_fragP;
4508 }
4509
4510
4511 xtensa_opcode
4512 next_frag_opcode (fragP)
4513 const fragS * fragP;
4514 {
4515 const fragS *next_fragP = next_non_empty_frag (fragP);
4516 static xtensa_insnbuf insnbuf = NULL;
4517 xtensa_isa isa = xtensa_default_isa;
4518
4519 if (!insnbuf)
4520 insnbuf = xtensa_insnbuf_alloc (isa);
4521
4522 if (next_fragP == NULL)
4523 return XTENSA_UNDEFINED;
4524
4525 xtensa_insnbuf_from_chars (isa, insnbuf, next_fragP->fr_literal);
4526 return xtensa_decode_insn (isa, insnbuf);
4527 }
4528
4529
4530 /* Return true if the target frag is one of the next non-empty frags. */
4531
4532 bfd_boolean
4533 is_next_frag_target (fragP, target)
4534 const fragS *fragP;
4535 const fragS *target;
4536 {
4537 if (fragP == NULL)
4538 return FALSE;
4539
4540 for (; fragP; fragP = fragP->fr_next)
4541 {
4542 if (fragP == target)
4543 return TRUE;
4544 if (fragP->fr_fix != 0)
4545 return FALSE;
4546 if (fragP->fr_type == rs_fill && fragP->fr_offset != 0)
4547 return FALSE;
4548 if ((fragP->fr_type == rs_align || fragP->fr_type == rs_align_code)
4549 && ((fragP->fr_address % (1 << fragP->fr_offset)) != 0))
4550 return FALSE;
4551 if (fragP->fr_type == rs_space)
4552 return FALSE;
4553 }
4554 return FALSE;
4555 }
4556
4557
4558 /* If the next legit fragment is an end-of-loop marker,
4559 switch its state so it will instantiate a NOP. */
4560
4561 static void
4562 update_next_frag_nop_state (fragP)
4563 fragS *fragP;
4564 {
4565 fragS *next_fragP = fragP->fr_next;
4566
4567 while (next_fragP && next_fragP->fr_fix == 0)
4568 {
4569 if (next_fragP->fr_type == rs_machine_dependent
4570 && next_fragP->fr_subtype == RELAX_LOOP_END)
4571 {
4572 next_fragP->fr_subtype = RELAX_LOOP_END_ADD_NOP;
4573 return;
4574 }
4575 next_fragP = next_fragP->fr_next;
4576 }
4577 }
4578
4579
4580 static bfd_boolean
4581 next_frag_is_branch_target (fragP)
4582 const fragS *fragP;
4583 {
4584 /* Sometimes an empty will end up here due storage allocation issues,
4585 so we have to skip until we find something legit. */
4586 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4587 {
4588 if (fragP->tc_frag_data.is_branch_target)
4589 return TRUE;
4590 if (fragP->fr_fix != 0)
4591 break;
4592 }
4593 return FALSE;
4594 }
4595
4596
4597 static bfd_boolean
4598 next_frag_is_loop_target (fragP)
4599 const fragS *fragP;
4600 {
4601 /* Sometimes an empty will end up here due storage allocation issues.
4602 So we have to skip until we find something legit. */
4603 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4604 {
4605 if (fragP->tc_frag_data.is_loop_target)
4606 return TRUE;
4607 if (fragP->fr_fix != 0)
4608 break;
4609 }
4610 return FALSE;
4611 }
4612
4613
4614 static addressT
4615 next_frag_pre_opcode_bytes (fragp)
4616 const fragS *fragp;
4617 {
4618 const fragS *next_fragp = fragp->fr_next;
4619
4620 xtensa_opcode next_opcode = next_frag_opcode (fragp);
4621 if (!is_loop_opcode (next_opcode))
4622 return 0;
4623
4624 /* Sometimes an empty will end up here due storage allocation issues.
4625 So we have to skip until we find something legit. */
4626 while (next_fragp->fr_fix == 0)
4627 next_fragp = next_fragp->fr_next;
4628
4629 if (next_fragp->fr_type != rs_machine_dependent)
4630 return 0;
4631
4632 /* There is some implicit knowledge encoded in here.
4633 The LOOP instructions that are NOT RELAX_IMMED have
4634 been relaxed. */
4635 if (next_fragp->fr_subtype > RELAX_IMMED)
4636 return get_expanded_loop_offset (next_opcode);
4637
4638 return 0;
4639 }
4640
4641
4642 /* Mark a location where we can later insert literal frags. Update
4643 the section's literal_pool_loc, so subsequent literals can be
4644 placed nearest to their use. */
4645
4646 static void
4647 xtensa_mark_literal_pool_location (move_labels)
4648 bfd_boolean move_labels;
4649 {
4650 /* Any labels pointing to the current location need
4651 to be adjusted to after the literal pool. */
4652 emit_state s;
4653 fragS *label_target = frag_now;
4654 fragS *pool_location;
4655 offsetT label_offset = frag_now_fix ();
4656
4657 frag_align (2, 0, 0);
4658
4659 /* We stash info in the fr_var of these frags
4660 so we can later move the literal's fixes into this
4661 frchain's fix list. We can use fr_var because fr_var's
4662 interpretation depends solely on the fr_type and subtype. */
4663 pool_location = frag_now;
4664 frag_variant (rs_machine_dependent, 0, (int) frchain_now,
4665 RELAX_LITERAL_POOL_BEGIN, NULL, 0, NULL);
4666 frag_variant (rs_machine_dependent, 0, (int) now_seg,
4667 RELAX_LITERAL_POOL_END, NULL, 0, NULL);
4668
4669 /* Now put a frag into the literal pool that points to this location. */
4670 set_literal_pool_location (now_seg, pool_location);
4671 xtensa_switch_to_literal_fragment (&s);
4672
4673 /* Close whatever frag is there. */
4674 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4675 frag_now->tc_frag_data.literal_frag = pool_location;
4676 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4677 xtensa_restore_emit_state (&s);
4678 if (move_labels)
4679 xtensa_move_labels (label_target, label_offset, frag_now, 0);
4680 }
4681
4682
4683 static void
4684 xtensa_move_labels (old_frag, old_offset, new_frag, new_offset)
4685 fragS *old_frag;
4686 valueT old_offset;
4687 fragS *new_frag ATTRIBUTE_UNUSED;
4688 valueT new_offset;
4689 {
4690 symbolS *old_sym;
4691
4692 /* Repeat until there are no more.... */
4693 for (old_sym = xtensa_find_label (old_frag, old_offset, TRUE);
4694 old_sym;
4695 old_sym = xtensa_find_label (old_frag, old_offset, TRUE))
4696 {
4697 S_SET_VALUE (old_sym, (valueT) new_offset);
4698 symbol_set_frag (old_sym, frag_now);
4699 }
4700 }
4701
4702
4703 /* Assemble a NOP of the requested size in the buffer. User must have
4704 allocated "buf" with at least "size" bytes. */
4705
4706 void
4707 assemble_nop (size, buf)
4708 size_t size;
4709 char *buf;
4710 {
4711 static xtensa_insnbuf insnbuf = NULL;
4712 TInsn t_insn;
4713 if (!insnbuf)
4714 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4715
4716 tinsn_init (&t_insn);
4717 switch (size)
4718 {
4719 case 2:
4720 t_insn.opcode = xtensa_nop_n_opcode;
4721 t_insn.ntok = 0;
4722 if (t_insn.opcode == XTENSA_UNDEFINED)
4723 as_fatal (_("opcode 'NOP.N' unavailable in this configuration"));
4724 tinsn_to_insnbuf (&t_insn, insnbuf);
4725 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf);
4726 break;
4727
4728 case 3:
4729 t_insn.opcode = xtensa_or_opcode;
4730 assert (t_insn.opcode != XTENSA_UNDEFINED);
4731 if (t_insn.opcode == XTENSA_UNDEFINED)
4732 as_fatal (_("opcode 'OR' unavailable in this configuration"));
4733 set_expr_const (&t_insn.tok[0], 1);
4734 set_expr_const (&t_insn.tok[1], 1);
4735 set_expr_const (&t_insn.tok[2], 1);
4736 t_insn.ntok = 3;
4737 tinsn_to_insnbuf (&t_insn, insnbuf);
4738 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf);
4739 break;
4740
4741 default:
4742 as_fatal (_("invalid %d-byte NOP requested"), size);
4743 }
4744 }
4745
4746
4747 /* Return the number of bytes for the offset of the expanded loop
4748 instruction. This should be incorporated into the relaxation
4749 specification but is hard-coded here. This is used to auto-align
4750 the loop instruction. It is invalid to call this function if the
4751 configuration does not have loops or if the opcode is not a loop
4752 opcode. */
4753
4754 static addressT
4755 get_expanded_loop_offset (opcode)
4756 xtensa_opcode opcode;
4757 {
4758 /* This is the OFFSET of the loop instruction in the expanded loop.
4759 This MUST correspond directly to the specification of the loop
4760 expansion. It will be validated on fragment conversion. */
4761 if (opcode == XTENSA_UNDEFINED)
4762 as_fatal (_("get_expanded_loop_offset: undefined opcode"));
4763 if (opcode == xtensa_loop_opcode)
4764 return 0;
4765 if (opcode == xtensa_loopnez_opcode)
4766 return 3;
4767 if (opcode == xtensa_loopgtz_opcode)
4768 return 6;
4769 as_fatal (_("get_expanded_loop_offset: invalid opcode"));
4770 return 0;
4771 }
4772
4773
4774 fragS *
4775 get_literal_pool_location (seg)
4776 segT seg;
4777 {
4778 return seg_info (seg)->tc_segment_info_data.literal_pool_loc;
4779 }
4780
4781
4782 static void
4783 set_literal_pool_location (seg, literal_pool_loc)
4784 segT seg;
4785 fragS *literal_pool_loc;
4786 {
4787 seg_info (seg)->tc_segment_info_data.literal_pool_loc = literal_pool_loc;
4788 }
4789
4790 \f
4791 /* External Functions and Other GAS Hooks. */
4792
4793 const char *
4794 xtensa_target_format ()
4795 {
4796 return (target_big_endian ? "elf32-xtensa-be" : "elf32-xtensa-le");
4797 }
4798
4799
4800 void
4801 xtensa_file_arch_init (abfd)
4802 bfd *abfd;
4803 {
4804 bfd_set_private_flags (abfd, 0x100 | 0x200);
4805 }
4806
4807
4808 void
4809 md_number_to_chars (buf, val, n)
4810 char *buf;
4811 valueT val;
4812 int n;
4813 {
4814 if (target_big_endian)
4815 number_to_chars_bigendian (buf, val, n);
4816 else
4817 number_to_chars_littleendian (buf, val, n);
4818 }
4819
4820
4821 /* This function is called once, at assembler startup time. It should
4822 set up all the tables, etc. that the MD part of the assembler will
4823 need. */
4824
4825 void
4826 md_begin ()
4827 {
4828 segT current_section = now_seg;
4829 int current_subsec = now_subseg;
4830 xtensa_isa isa;
4831
4832 #if STATIC_LIBISA
4833 isa = xtensa_isa_init ();
4834 #else
4835 /* ISA was already initialized by xtensa_init(). */
4836 isa = xtensa_default_isa;
4837 #endif
4838
4839 /* Set up the .literal, .fini.literal and .init.literal sections. */
4840 memset (&default_lit_sections, 0, sizeof (default_lit_sections));
4841 default_lit_sections.init_lit_seg_name = INIT_LITERAL_SECTION_NAME;
4842 default_lit_sections.fini_lit_seg_name = FINI_LITERAL_SECTION_NAME;
4843 default_lit_sections.lit_seg_name = LITERAL_SECTION_NAME;
4844
4845 subseg_set (current_section, current_subsec);
4846
4847 xtensa_addi_opcode = xtensa_opcode_lookup (isa, "addi");
4848 xtensa_addmi_opcode = xtensa_opcode_lookup (isa, "addmi");
4849 xtensa_call0_opcode = xtensa_opcode_lookup (isa, "call0");
4850 xtensa_call4_opcode = xtensa_opcode_lookup (isa, "call4");
4851 xtensa_call8_opcode = xtensa_opcode_lookup (isa, "call8");
4852 xtensa_call12_opcode = xtensa_opcode_lookup (isa, "call12");
4853 xtensa_callx0_opcode = xtensa_opcode_lookup (isa, "callx0");
4854 xtensa_callx4_opcode = xtensa_opcode_lookup (isa, "callx4");
4855 xtensa_callx8_opcode = xtensa_opcode_lookup (isa, "callx8");
4856 xtensa_callx12_opcode = xtensa_opcode_lookup (isa, "callx12");
4857 xtensa_entry_opcode = xtensa_opcode_lookup (isa, "entry");
4858 xtensa_isync_opcode = xtensa_opcode_lookup (isa, "isync");
4859 xtensa_j_opcode = xtensa_opcode_lookup (isa, "j");
4860 xtensa_jx_opcode = xtensa_opcode_lookup (isa, "jx");
4861 xtensa_loop_opcode = xtensa_opcode_lookup (isa, "loop");
4862 xtensa_loopnez_opcode = xtensa_opcode_lookup (isa, "loopnez");
4863 xtensa_loopgtz_opcode = xtensa_opcode_lookup (isa, "loopgtz");
4864 xtensa_nop_n_opcode = xtensa_opcode_lookup (isa, "nop.n");
4865 xtensa_or_opcode = xtensa_opcode_lookup (isa, "or");
4866 xtensa_ret_opcode = xtensa_opcode_lookup (isa, "ret");
4867 xtensa_ret_n_opcode = xtensa_opcode_lookup (isa, "ret.n");
4868 xtensa_retw_opcode = xtensa_opcode_lookup (isa, "retw");
4869 xtensa_retw_n_opcode = xtensa_opcode_lookup (isa, "retw.n");
4870 xtensa_rsr_opcode = xtensa_opcode_lookup (isa, "rsr");
4871 xtensa_waiti_opcode = xtensa_opcode_lookup (isa, "waiti");
4872 }
4873
4874
4875 /* tc_frob_label hook */
4876
4877 void
4878 xtensa_frob_label (sym)
4879 symbolS *sym;
4880 {
4881 xtensa_define_label (sym);
4882 if (is_loop_target_label (sym)
4883 && (get_last_insn_flags (now_seg, now_subseg)
4884 & FLAG_IS_BAD_LOOPEND) != 0)
4885 as_bad (_("invalid last instruction for a zero-overhead loop"));
4886
4887 /* No target aligning in the absolute section. */
4888 if (now_seg != absolute_section && align_targets
4889 && !is_unaligned_label (sym))
4890 {
4891 fragS *old_frag = frag_now;
4892 offsetT old_offset = frag_now_fix ();
4893 if (frag_now->tc_frag_data.is_literal)
4894 return;
4895 /* frag_now->tc_frag_data.is_insn = TRUE; */
4896 frag_var (rs_machine_dependent, 4, 4,
4897 RELAX_DESIRE_ALIGN_IF_TARGET,
4898 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4899 xtensa_move_labels (old_frag, old_offset, frag_now, 0);
4900 /* Once we know whether or not the label is a branch target
4901 We will suppress some of these alignments. */
4902 }
4903 }
4904
4905
4906 /* md_flush_pending_output hook */
4907
4908 void
4909 xtensa_flush_pending_output ()
4910 {
4911 /* If there is a non-zero instruction fragment, close it. */
4912 if (frag_now_fix () != 0 && frag_now->tc_frag_data.is_insn)
4913 {
4914 frag_wane (frag_now);
4915 frag_new (0);
4916 }
4917 frag_now->tc_frag_data.is_insn = FALSE;
4918 }
4919
4920
4921 void
4922 md_assemble (str)
4923 char *str;
4924 {
4925 xtensa_isa isa = xtensa_default_isa;
4926 char *opname;
4927 unsigned opnamelen;
4928 bfd_boolean has_underbar = FALSE;
4929 char *arg_strings[MAX_INSN_ARGS];
4930 int num_args;
4931 IStack istack; /* Put instructions into here. */
4932 TInsn orig_insn; /* Original instruction from the input. */
4933 int i;
4934 symbolS *lit_sym = NULL;
4935
4936 if (frag_now->tc_frag_data.is_literal)
4937 {
4938 static bfd_boolean reported = 0;
4939 if (reported < 4)
4940 as_bad (_("cannot assemble '%s' into a literal fragment"), str);
4941 if (reported == 3)
4942 as_bad (_("..."));
4943 reported++;
4944 return;
4945 }
4946
4947 istack_init (&istack);
4948 tinsn_init (&orig_insn);
4949
4950 /* Split off the opcode. */
4951 opnamelen = strspn (str, "abcdefghijklmnopqrstuvwxyz_/0123456789.");
4952 opname = xmalloc (opnamelen + 1);
4953 memcpy (opname, str, opnamelen);
4954 opname[opnamelen] = '\0';
4955
4956 num_args = tokenize_arguments (arg_strings, str + opnamelen);
4957 if (num_args == -1)
4958 {
4959 as_bad (_("syntax error"));
4960 return;
4961 }
4962
4963 if (xg_translate_idioms (&opname, &num_args, arg_strings))
4964 return;
4965
4966 /* Check for an underbar prefix. */
4967 if (*opname == '_')
4968 {
4969 has_underbar = TRUE;
4970 opname += 1;
4971 }
4972
4973 orig_insn.insn_type = ITYPE_INSN;
4974 orig_insn.ntok = 0;
4975 orig_insn.is_specific_opcode = (has_underbar || !use_generics ());
4976 specific_opcode = orig_insn.is_specific_opcode;
4977
4978 orig_insn.opcode = xtensa_opcode_lookup (isa, opname);
4979 if (orig_insn.opcode == XTENSA_UNDEFINED)
4980 {
4981 as_bad (_("unknown opcode %s"), opname);
4982 return;
4983 }
4984
4985 if (frag_now_fix () != 0 && !frag_now->tc_frag_data.is_insn)
4986 {
4987 frag_wane (frag_now);
4988 frag_new (0);
4989 }
4990
4991 if (software_a0_b_retw_interlock)
4992 {
4993 if ((get_last_insn_flags (now_seg, now_subseg) & FLAG_IS_A0_WRITER) != 0
4994 && is_conditional_branch_opcode (orig_insn.opcode))
4995 {
4996 has_a0_b_retw = TRUE;
4997
4998 /* Mark this fragment with the special RELAX_ADD_NOP_IF_A0_B_RETW.
4999 After the first assembly pass we will check all of them and
5000 add a nop if needed. */
5001 frag_now->tc_frag_data.is_insn = TRUE;
5002 frag_var (rs_machine_dependent, 4, 4,
5003 RELAX_ADD_NOP_IF_A0_B_RETW,
5004 frag_now->fr_symbol, frag_now->fr_offset, NULL);
5005 frag_now->tc_frag_data.is_insn = TRUE;
5006 frag_var (rs_machine_dependent, 4, 4,
5007 RELAX_ADD_NOP_IF_A0_B_RETW,
5008 frag_now->fr_symbol, frag_now->fr_offset, NULL);
5009 }
5010 }
5011
5012 /* Special case: The call instructions should be marked "specific opcode"
5013 to keep them from expanding. */
5014 if (!use_longcalls () && is_direct_call_opcode (orig_insn.opcode))
5015 orig_insn.is_specific_opcode = TRUE;
5016
5017 /* Parse the arguments. */
5018 if (parse_arguments (&orig_insn, num_args, arg_strings))
5019 {
5020 as_bad (_("syntax error"));
5021 return;
5022 }
5023
5024 /* Free the opcode and argument strings, now that they've been parsed. */
5025 free (has_underbar ? opname - 1 : opname);
5026 opname = 0;
5027 while (num_args-- > 0)
5028 free (arg_strings[num_args]);
5029
5030 /* Check for the right number and type of arguments. */
5031 if (tinsn_check_arguments (&orig_insn))
5032 return;
5033
5034 /* See if the instruction implies an aligned section. */
5035 if (is_entry_opcode (orig_insn.opcode) || is_loop_opcode (orig_insn.opcode))
5036 record_alignment (now_seg, 2);
5037
5038 xg_add_branch_and_loop_targets (&orig_insn);
5039
5040 /* Special cases for instructions that force an alignment... */
5041 if (!orig_insn.is_specific_opcode && is_loop_opcode (orig_insn.opcode))
5042 {
5043 fragS *old_frag = frag_now;
5044 offsetT old_offset = frag_now_fix ();
5045 symbolS *old_sym = NULL;
5046 size_t max_fill;
5047
5048 frag_now->tc_frag_data.is_insn = TRUE;
5049 frag_now->tc_frag_data.is_no_density = !code_density_available ();
5050 max_fill = get_text_align_max_fill_size
5051 (get_text_align_power (XTENSA_FETCH_WIDTH),
5052 TRUE, frag_now->tc_frag_data.is_no_density);
5053 frag_var (rs_machine_dependent, max_fill, max_fill,
5054 RELAX_ALIGN_NEXT_OPCODE, frag_now->fr_symbol,
5055 frag_now->fr_offset, NULL);
5056
5057 /* Repeat until there are no more. */
5058 while ((old_sym = xtensa_find_label (old_frag, old_offset, FALSE)))
5059 {
5060 S_SET_VALUE (old_sym, (valueT) 0);
5061 symbol_set_frag (old_sym, frag_now);
5062 }
5063 }
5064
5065 /* Special count for "entry" instruction. */
5066 if (is_entry_opcode (orig_insn.opcode))
5067 {
5068 /* Check that the second opcode (#1) is >= 16. */
5069 if (orig_insn.ntok >= 2)
5070 {
5071 expressionS *exp = &orig_insn.tok[1];
5072 switch (exp->X_op)
5073 {
5074 case O_constant:
5075 if (exp->X_add_number < 16)
5076 as_warn (_("entry instruction with stack decrement < 16"));
5077 break;
5078
5079 default:
5080 as_warn (_("entry instruction with non-constant decrement"));
5081 }
5082 }
5083 }
5084
5085 if (!orig_insn.is_specific_opcode && is_entry_opcode (orig_insn.opcode))
5086 {
5087 xtensa_mark_literal_pool_location (TRUE);
5088
5089 /* Automatically align ENTRY instructions. */
5090 frag_align (2, 0, 0);
5091 }
5092
5093 if (software_a0_b_retw_interlock)
5094 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_A0_WRITER,
5095 is_register_writer (&orig_insn, "a", 0));
5096
5097 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND,
5098 is_bad_loopend_opcode (&orig_insn));
5099
5100 /* Finish it off:
5101 assemble_tokens (opcode, tok, ntok);
5102 expand the tokens from the orig_insn into the
5103 stack of instructions that will not expand
5104 unless required at relaxation time. */
5105 if (xg_expand_assembly_insn (&istack, &orig_insn))
5106 return;
5107
5108 for (i = 0; i < istack.ninsn; i++)
5109 {
5110 TInsn *insn = &istack.insn[i];
5111 if (insn->insn_type == ITYPE_LITERAL)
5112 {
5113 assert (lit_sym == NULL);
5114 lit_sym = xg_assemble_literal (insn);
5115 }
5116 else
5117 {
5118 if (lit_sym)
5119 xg_resolve_literals (insn, lit_sym);
5120 xg_assemble_tokens (insn);
5121 }
5122 }
5123
5124 /* Now, if the original opcode was a call... */
5125 if (align_targets && is_call_opcode (orig_insn.opcode))
5126 {
5127 frag_now->tc_frag_data.is_insn = TRUE;
5128 frag_var (rs_machine_dependent, 4, 4,
5129 RELAX_DESIRE_ALIGN,
5130 frag_now->fr_symbol,
5131 frag_now->fr_offset,
5132 NULL);
5133 }
5134 }
5135
5136
5137 /* TC_CONS_FIX_NEW hook: Check for "@PLT" suffix on symbol references.
5138 If found, use an XTENSA_PLT reloc for 4-byte values. Otherwise, this
5139 is the same as the standard code in read.c. */
5140
5141 void
5142 xtensa_cons_fix_new (frag, where, size, exp)
5143 fragS *frag;
5144 int where;
5145 int size;
5146 expressionS *exp;
5147 {
5148 bfd_reloc_code_real_type r;
5149 bfd_boolean plt = FALSE;
5150
5151 if (*input_line_pointer == '@')
5152 {
5153 if (!strncmp (input_line_pointer, PLT_SUFFIX, strlen (PLT_SUFFIX) - 1)
5154 && !strncmp (input_line_pointer, plt_suffix,
5155 strlen (plt_suffix) - 1))
5156 {
5157 as_bad (_("undefined @ suffix '%s', expected '%s'"),
5158 input_line_pointer, plt_suffix);
5159 ignore_rest_of_line ();
5160 return;
5161 }
5162
5163 input_line_pointer += strlen (plt_suffix);
5164 plt = TRUE;
5165 }
5166
5167 switch (size)
5168 {
5169 case 1:
5170 r = BFD_RELOC_8;
5171 break;
5172 case 2:
5173 r = BFD_RELOC_16;
5174 break;
5175 case 4:
5176 r = plt ? BFD_RELOC_XTENSA_PLT : BFD_RELOC_32;
5177 break;
5178 case 8:
5179 r = BFD_RELOC_64;
5180 break;
5181 default:
5182 as_bad (_("unsupported BFD relocation size %u"), size);
5183 r = BFD_RELOC_32;
5184 break;
5185 }
5186 fix_new_exp (frag, where, size, exp, 0, r);
5187 }
5188
5189
5190 /* TC_FRAG_INIT hook */
5191
5192 void
5193 xtensa_frag_init (frag)
5194 fragS *frag;
5195 {
5196 frag->tc_frag_data.is_no_density = !code_density_available ();
5197 }
5198
5199
5200 symbolS *
5201 md_undefined_symbol (name)
5202 char *name ATTRIBUTE_UNUSED;
5203 {
5204 return NULL;
5205 }
5206
5207
5208 /* Round up a section size to the appropriate boundary. */
5209
5210 valueT
5211 md_section_align (segment, size)
5212 segT segment ATTRIBUTE_UNUSED;
5213 valueT size;
5214 {
5215 return size; /* Byte alignment is fine. */
5216 }
5217
5218
5219 long
5220 md_pcrel_from (fixP)
5221 fixS *fixP;
5222 {
5223 char *insn_p;
5224 static xtensa_insnbuf insnbuf = NULL;
5225 int opnum;
5226 xtensa_operand operand;
5227 xtensa_opcode opcode;
5228 xtensa_isa isa = xtensa_default_isa;
5229 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
5230
5231 if (fixP->fx_done)
5232 return addr;
5233
5234 if (fixP->fx_r_type == BFD_RELOC_XTENSA_ASM_EXPAND)
5235 return addr;
5236
5237 if (!insnbuf)
5238 insnbuf = xtensa_insnbuf_alloc (isa);
5239
5240 insn_p = &fixP->fx_frag->fr_literal[fixP->fx_where];
5241 xtensa_insnbuf_from_chars (isa, insnbuf, insn_p);
5242 opcode = xtensa_decode_insn (isa, insnbuf);
5243
5244 opnum = reloc_to_opnum (fixP->fx_r_type);
5245
5246 if (opnum < 0)
5247 as_fatal (_("invalid operand relocation for '%s' instruction"),
5248 xtensa_opcode_name (isa, opcode));
5249 if (opnum >= xtensa_num_operands (isa, opcode))
5250 as_fatal (_("invalid relocation for operand %d in '%s' instruction"),
5251 opnum, xtensa_opcode_name (isa, opcode));
5252 operand = xtensa_get_operand (isa, opcode, opnum);
5253 if (!operand)
5254 {
5255 as_warn_where (fixP->fx_file,
5256 fixP->fx_line,
5257 _("invalid relocation type %d for %s instruction"),
5258 fixP->fx_r_type, xtensa_opcode_name (isa, opcode));
5259 return addr;
5260 }
5261
5262 if (!operand_is_pcrel_label (operand))
5263 {
5264 as_bad_where (fixP->fx_file,
5265 fixP->fx_line,
5266 _("invalid relocation for operand %d of '%s'"),
5267 opnum, xtensa_opcode_name (isa, opcode));
5268 return addr;
5269 }
5270 if (!xtensa_operand_isPCRelative (operand))
5271 {
5272 as_warn_where (fixP->fx_file,
5273 fixP->fx_line,
5274 _("non-PCREL relocation operand %d for '%s': %s"),
5275 opnum, xtensa_opcode_name (isa, opcode),
5276 bfd_get_reloc_code_name (fixP->fx_r_type));
5277 return addr;
5278 }
5279
5280 return 0 - xtensa_operand_do_reloc (operand, 0, addr);
5281 }
5282
5283
5284 /* tc_symbol_new_hook */
5285
5286 void
5287 xtensa_symbol_new_hook (symbolP)
5288 symbolS *symbolP;
5289 {
5290 symbolP->sy_tc.plt = 0;
5291 }
5292
5293
5294 /* tc_fix_adjustable hook */
5295
5296 bfd_boolean
5297 xtensa_fix_adjustable (fixP)
5298 fixS *fixP;
5299 {
5300 /* We need the symbol name for the VTABLE entries. */
5301 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
5302 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
5303 return 0;
5304
5305 return 1;
5306 }
5307
5308
5309 void
5310 md_apply_fix3 (fixP, valP, seg)
5311 fixS *fixP;
5312 valueT *valP;
5313 segT seg ATTRIBUTE_UNUSED;
5314 {
5315 if (fixP->fx_pcrel == 0 && fixP->fx_addsy == 0)
5316 {
5317 /* This happens when the relocation is within the current section.
5318 It seems this implies a PCREL operation. We'll catch it and error
5319 if not. */
5320
5321 char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
5322 static xtensa_insnbuf insnbuf = NULL;
5323 xtensa_opcode opcode;
5324 xtensa_isa isa;
5325
5326 switch (fixP->fx_r_type)
5327 {
5328 case BFD_RELOC_XTENSA_ASM_EXPAND:
5329 fixP->fx_done = 1;
5330 break;
5331
5332 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
5333 as_bad (_("unhandled local relocation fix %s"),
5334 bfd_get_reloc_code_name (fixP->fx_r_type));
5335 break;
5336
5337 case BFD_RELOC_32:
5338 case BFD_RELOC_16:
5339 case BFD_RELOC_8:
5340 /* The only one we support that isn't an instruction field. */
5341 md_number_to_chars (fixpos, *valP, fixP->fx_size);
5342 fixP->fx_done = 1;
5343 break;
5344
5345 case BFD_RELOC_XTENSA_OP0:
5346 case BFD_RELOC_XTENSA_OP1:
5347 case BFD_RELOC_XTENSA_OP2:
5348 isa = xtensa_default_isa;
5349 if (!insnbuf)
5350 insnbuf = xtensa_insnbuf_alloc (isa);
5351
5352 xtensa_insnbuf_from_chars (isa, insnbuf, fixpos);
5353 opcode = xtensa_decode_insn (isa, insnbuf);
5354 if (opcode == XTENSA_UNDEFINED)
5355 as_fatal (_("undecodable FIX"));
5356
5357 xtensa_insnbuf_set_immediate_field (opcode, insnbuf, *valP,
5358 fixP->fx_file, fixP->fx_line);
5359
5360 fixP->fx_frag->tc_frag_data.is_insn = TRUE;
5361 xtensa_insnbuf_to_chars (isa, insnbuf, fixpos);
5362 fixP->fx_done = 1;
5363 break;
5364
5365 case BFD_RELOC_VTABLE_INHERIT:
5366 case BFD_RELOC_VTABLE_ENTRY:
5367 fixP->fx_done = 0;
5368 break;
5369
5370 default:
5371 as_bad (_("unhandled local relocation fix %s"),
5372 bfd_get_reloc_code_name (fixP->fx_r_type));
5373 }
5374 }
5375 }
5376
5377
5378 char *
5379 md_atof (type, litP, sizeP)
5380 int type;
5381 char *litP;
5382 int *sizeP;
5383 {
5384 int prec;
5385 LITTLENUM_TYPE words[4];
5386 char *t;
5387 int i;
5388
5389 switch (type)
5390 {
5391 case 'f':
5392 prec = 2;
5393 break;
5394
5395 case 'd':
5396 prec = 4;
5397 break;
5398
5399 default:
5400 *sizeP = 0;
5401 return "bad call to md_atof";
5402 }
5403
5404 t = atof_ieee (input_line_pointer, type, words);
5405 if (t)
5406 input_line_pointer = t;
5407
5408 *sizeP = prec * 2;
5409
5410 for (i = prec - 1; i >= 0; i--)
5411 {
5412 int idx = i;
5413 if (target_big_endian)
5414 idx = (prec - 1 - i);
5415
5416 md_number_to_chars (litP, (valueT) words[idx], 2);
5417 litP += 2;
5418 }
5419
5420 return NULL;
5421 }
5422
5423
5424 int
5425 md_estimate_size_before_relax (fragP, seg)
5426 fragS *fragP;
5427 segT seg ATTRIBUTE_UNUSED;
5428 {
5429 return fragP->tc_frag_data.text_expansion;
5430 }
5431
5432
5433 /* Translate internal representation of relocation info to BFD target
5434 format. */
5435
5436 arelent *
5437 tc_gen_reloc (section, fixp)
5438 asection *section ATTRIBUTE_UNUSED;
5439 fixS *fixp;
5440 {
5441 arelent *reloc;
5442
5443 reloc = (arelent *) xmalloc (sizeof (arelent));
5444 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5445 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
5446 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
5447
5448 /* Make sure none of our internal relocations make it this far.
5449 They'd better have been fully resolved by this point. */
5450 assert ((int) fixp->fx_r_type > 0);
5451
5452 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
5453 if (reloc->howto == NULL)
5454 {
5455 as_bad_where (fixp->fx_file, fixp->fx_line,
5456 _("cannot represent `%s' relocation in object file"),
5457 bfd_get_reloc_code_name (fixp->fx_r_type));
5458 return NULL;
5459 }
5460
5461 if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
5462 {
5463 as_fatal (_("internal error? cannot generate `%s' relocation"),
5464 bfd_get_reloc_code_name (fixp->fx_r_type));
5465 }
5466 assert (!fixp->fx_pcrel == !reloc->howto->pc_relative);
5467
5468 reloc->addend = fixp->fx_offset;
5469
5470 switch (fixp->fx_r_type)
5471 {
5472 case BFD_RELOC_XTENSA_OP0:
5473 case BFD_RELOC_XTENSA_OP1:
5474 case BFD_RELOC_XTENSA_OP2:
5475 case BFD_RELOC_XTENSA_ASM_EXPAND:
5476 case BFD_RELOC_32:
5477 case BFD_RELOC_XTENSA_PLT:
5478 case BFD_RELOC_VTABLE_INHERIT:
5479 case BFD_RELOC_VTABLE_ENTRY:
5480 break;
5481
5482 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
5483 as_warn (_("emitting simplification relocation"));
5484 break;
5485
5486 default:
5487 as_warn (_("emitting unknown relocation"));
5488 }
5489
5490 return reloc;
5491 }
5492
5493 \f
5494 void
5495 xtensa_end ()
5496 {
5497 directive_balance ();
5498 xtensa_move_literals ();
5499
5500 xtensa_reorder_segments ();
5501 xtensa_mark_target_fragments ();
5502 xtensa_cleanup_align_frags ();
5503 xtensa_fix_target_frags ();
5504 if (software_a0_b_retw_interlock && has_a0_b_retw)
5505 xtensa_fix_a0_b_retw_frags ();
5506 if (software_avoid_b_j_loop_end && maybe_has_b_j_loop_end)
5507 xtensa_fix_b_j_loop_end_frags ();
5508
5509 /* "close_loop_end" should be processed BEFORE "short_loop". */
5510 if (software_avoid_close_loop_end && maybe_has_close_loop_end)
5511 xtensa_fix_close_loop_end_frags ();
5512
5513 if (software_avoid_short_loop && maybe_has_short_loop)
5514 xtensa_fix_short_loop_frags ();
5515
5516 xtensa_sanity_check ();
5517 }
5518
5519
5520 static void
5521 xtensa_cleanup_align_frags ()
5522 {
5523 frchainS *frchP;
5524
5525 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5526 {
5527 fragS *fragP;
5528
5529 /* Walk over all of the fragments in a subsection. */
5530 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5531 {
5532 if ((fragP->fr_type == rs_align
5533 || fragP->fr_type == rs_align_code
5534 || (fragP->fr_type == rs_machine_dependent
5535 && (fragP->fr_subtype == RELAX_DESIRE_ALIGN
5536 || fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)))
5537 && fragP->fr_fix == 0)
5538 {
5539 fragS * next = fragP->fr_next;
5540
5541 while (next
5542 && next->fr_type == rs_machine_dependent
5543 && next->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
5544 {
5545 frag_wane (next);
5546 next = next->fr_next;
5547 }
5548 }
5549 }
5550 }
5551 }
5552
5553
5554 /* Re-process all of the fragments looking to convert all of the
5555 RELAX_DESIRE_ALIGN_IF_TARGET fragments. If there is a branch
5556 target in the next fragment, convert this to RELAX_DESIRE_ALIGN.
5557 If the next fragment starts with a loop target, AND the previous
5558 fragment can be expanded to negate the branch, convert this to a
5559 RELAX_LOOP_END. Otherwise, convert to a .fill 0. */
5560
5561 static void
5562 xtensa_fix_target_frags ()
5563 {
5564 frchainS *frchP;
5565
5566 /* When this routine is called, all of the subsections are still intact
5567 so we walk over subsections instead of sections. */
5568 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5569 {
5570 bfd_boolean prev_frag_can_negate_branch = FALSE;
5571 fragS *fragP;
5572
5573 /* Walk over all of the fragments in a subsection. */
5574 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5575 {
5576 if (fragP->fr_type == rs_machine_dependent
5577 && fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
5578 {
5579 if (next_frag_is_loop_target (fragP))
5580 {
5581 if (prev_frag_can_negate_branch)
5582 fragP->fr_subtype = RELAX_LOOP_END;
5583 else
5584 {
5585 if (!align_only_targets ||
5586 next_frag_is_branch_target (fragP))
5587 fragP->fr_subtype = RELAX_DESIRE_ALIGN;
5588 else
5589 frag_wane (fragP);
5590 }
5591 }
5592 else if (!align_only_targets
5593 || next_frag_is_branch_target (fragP))
5594 fragP->fr_subtype = RELAX_DESIRE_ALIGN;
5595 else
5596 frag_wane (fragP);
5597 }
5598 if (fragP->fr_fix != 0)
5599 prev_frag_can_negate_branch = FALSE;
5600 if (frag_can_negate_branch (fragP))
5601 prev_frag_can_negate_branch = TRUE;
5602 }
5603 }
5604 }
5605
5606
5607 static bfd_boolean
5608 frag_can_negate_branch (fragP)
5609 fragS *fragP;
5610 {
5611 if (fragP->fr_type == rs_machine_dependent
5612 && fragP->fr_subtype == RELAX_IMMED)
5613 {
5614 TInsn t_insn;
5615 tinsn_from_chars (&t_insn, fragP->fr_opcode);
5616 if (is_negatable_branch (&t_insn))
5617 return TRUE;
5618 }
5619 return FALSE;
5620 }
5621
5622
5623 /* Re-process all of the fragments looking to convert all of the
5624 RELAX_ADD_NOP_IF_A0_B_RETW. If the next instruction is a
5625 conditional branch or a retw/retw.n, convert this frag to one that
5626 will generate a NOP. In any case close it off with a .fill 0. */
5627
5628 static void
5629 xtensa_fix_a0_b_retw_frags ()
5630 {
5631 frchainS *frchP;
5632
5633 /* When this routine is called, all of the subsections are still intact
5634 so we walk over subsections instead of sections. */
5635 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5636 {
5637 fragS *fragP;
5638
5639 /* Walk over all of the fragments in a subsection. */
5640 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5641 {
5642 if (fragP->fr_type == rs_machine_dependent
5643 && fragP->fr_subtype == RELAX_ADD_NOP_IF_A0_B_RETW)
5644 {
5645 if (next_instrs_are_b_retw (fragP))
5646 relax_frag_add_nop (fragP);
5647 else
5648 frag_wane (fragP);
5649 }
5650 }
5651 }
5652 }
5653
5654
5655 bfd_boolean
5656 next_instrs_are_b_retw (fragP)
5657 fragS * fragP;
5658 {
5659 xtensa_opcode opcode;
5660 const fragS *next_fragP = next_non_empty_frag (fragP);
5661 static xtensa_insnbuf insnbuf = NULL;
5662 xtensa_isa isa = xtensa_default_isa;
5663 int offset = 0;
5664
5665 if (!insnbuf)
5666 insnbuf = xtensa_insnbuf_alloc (isa);
5667
5668 if (next_fragP == NULL)
5669 return FALSE;
5670
5671 /* Check for the conditional branch. */
5672 xtensa_insnbuf_from_chars (isa, insnbuf, &next_fragP->fr_literal[offset]);
5673 opcode = xtensa_decode_insn (isa, insnbuf);
5674
5675 if (!is_conditional_branch_opcode (opcode))
5676 return FALSE;
5677
5678 offset += xtensa_insn_length (isa, opcode);
5679 if (offset == next_fragP->fr_fix)
5680 {
5681 next_fragP = next_non_empty_frag (next_fragP);
5682 offset = 0;
5683 }
5684 if (next_fragP == NULL)
5685 return FALSE;
5686
5687 /* Check for the retw/retw.n. */
5688 xtensa_insnbuf_from_chars (isa, insnbuf, &next_fragP->fr_literal[offset]);
5689 opcode = xtensa_decode_insn (isa, insnbuf);
5690
5691 if (is_windowed_return_opcode (opcode))
5692 return TRUE;
5693 return FALSE;
5694 }
5695
5696
5697 /* Re-process all of the fragments looking to convert all of the
5698 RELAX_ADD_NOP_IF_PRE_LOOP_END. If there is one instruction and a
5699 loop end label, convert this frag to one that will generate a NOP.
5700 In any case close it off with a .fill 0. */
5701
5702 static void
5703 xtensa_fix_b_j_loop_end_frags ()
5704 {
5705 frchainS *frchP;
5706
5707 /* When this routine is called, all of the subsections are still intact
5708 so we walk over subsections instead of sections. */
5709 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5710 {
5711 fragS *fragP;
5712
5713 /* Walk over all of the fragments in a subsection. */
5714 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5715 {
5716 if (fragP->fr_type == rs_machine_dependent
5717 && fragP->fr_subtype == RELAX_ADD_NOP_IF_PRE_LOOP_END)
5718 {
5719 if (next_instr_is_loop_end (fragP))
5720 relax_frag_add_nop (fragP);
5721 else
5722 frag_wane (fragP);
5723 }
5724 }
5725 }
5726 }
5727
5728
5729 bfd_boolean
5730 next_instr_is_loop_end (fragP)
5731 fragS * fragP;
5732 {
5733 const fragS *next_fragP;
5734
5735 if (next_frag_is_loop_target (fragP))
5736 return FALSE;
5737
5738 next_fragP = next_non_empty_frag (fragP);
5739 if (next_fragP == NULL)
5740 return FALSE;
5741
5742 if (!next_frag_is_loop_target (next_fragP))
5743 return FALSE;
5744
5745 /* If the size is >= 3 then there is more than one instruction here.
5746 The hardware bug will not fire. */
5747 if (next_fragP->fr_fix > 3)
5748 return FALSE;
5749
5750 return TRUE;
5751 }
5752
5753
5754 /* Re-process all of the fragments looking to convert all of the
5755 RELAX_ADD_NOP_IF_CLOSE_LOOP_END. If there is an loop end that is
5756 not MY loop's loop end within 12 bytes, add enough nops here to
5757 make it at least 12 bytes away. In any case close it off with a
5758 .fill 0. */
5759
5760 static void
5761 xtensa_fix_close_loop_end_frags ()
5762 {
5763 frchainS *frchP;
5764
5765 /* When this routine is called, all of the subsections are still intact
5766 so we walk over subsections instead of sections. */
5767 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5768 {
5769 fragS *fragP;
5770
5771 fragS *current_target = NULL;
5772 offsetT current_offset = 0;
5773
5774 /* Walk over all of the fragments in a subsection. */
5775 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5776 {
5777 if (fragP->fr_type == rs_machine_dependent
5778 && fragP->fr_subtype == RELAX_IMMED)
5779 {
5780 /* Read it. If the instruction is a loop, get the target. */
5781 xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_opcode);
5782 if (is_loop_opcode (opcode))
5783 {
5784 TInsn t_insn;
5785
5786 tinsn_from_chars (&t_insn, fragP->fr_opcode);
5787 tinsn_immed_from_frag (&t_insn, fragP);
5788
5789 /* Get the current fragment target. */
5790 if (fragP->fr_symbol)
5791 {
5792 current_target = symbol_get_frag (fragP->fr_symbol);
5793 current_offset = fragP->fr_offset;
5794 }
5795 }
5796 }
5797
5798 if (current_target
5799 && fragP->fr_type == rs_machine_dependent
5800 && fragP->fr_subtype == RELAX_ADD_NOP_IF_CLOSE_LOOP_END)
5801 {
5802 size_t min_bytes;
5803 size_t bytes_added = 0;
5804
5805 #define REQUIRED_LOOP_DIVIDING_BYTES 12
5806 /* Max out at 12. */
5807 min_bytes = min_bytes_to_other_loop_end
5808 (fragP->fr_next, current_target, current_offset,
5809 REQUIRED_LOOP_DIVIDING_BYTES);
5810
5811 if (min_bytes < REQUIRED_LOOP_DIVIDING_BYTES)
5812 {
5813 while (min_bytes + bytes_added
5814 < REQUIRED_LOOP_DIVIDING_BYTES)
5815 {
5816 int length = 3;
5817
5818 if (fragP->fr_var < length)
5819 as_warn (_("fr_var %lu < length %d; ignoring"),
5820 fragP->fr_var, length);
5821 else
5822 {
5823 assemble_nop (length,
5824 fragP->fr_literal + fragP->fr_fix);
5825 fragP->fr_fix += length;
5826 fragP->fr_var -= length;
5827 }
5828 bytes_added += length;
5829 }
5830 }
5831 frag_wane (fragP);
5832 }
5833 }
5834 }
5835 }
5836
5837
5838 size_t
5839 min_bytes_to_other_loop_end (fragP, current_target, current_offset, max_size)
5840 fragS *fragP;
5841 fragS *current_target;
5842 offsetT current_offset;
5843 size_t max_size;
5844 {
5845 size_t offset = 0;
5846 fragS *current_fragP;
5847
5848 for (current_fragP = fragP;
5849 current_fragP;
5850 current_fragP = current_fragP->fr_next)
5851 {
5852 if (current_fragP->tc_frag_data.is_loop_target
5853 && current_fragP != current_target)
5854 return offset + current_offset;
5855
5856 offset += unrelaxed_frag_min_size (current_fragP);
5857
5858 if (offset + current_offset >= max_size)
5859 return max_size;
5860 }
5861 return max_size;
5862 }
5863
5864
5865 size_t
5866 unrelaxed_frag_min_size (fragP)
5867 fragS * fragP;
5868 {
5869 size_t size = fragP->fr_fix;
5870
5871 /* add fill size */
5872 if (fragP->fr_type == rs_fill)
5873 size += fragP->fr_offset;
5874
5875 return size;
5876 }
5877
5878
5879 /* Re-process all of the fragments looking to convert all
5880 of the RELAX_ADD_NOP_IF_SHORT_LOOP. If:
5881
5882 A)
5883 1) the instruction size count to the loop end label
5884 is too short (<= 2 instructions),
5885 2) loop has a jump or branch in it
5886
5887 or B)
5888 1) software_avoid_all_short_loops is true
5889 2) The generating loop was a 'loopgtz' or 'loopnez'
5890 3) the instruction size count to the loop end label is too short
5891 (<= 2 instructions)
5892 then convert this frag (and maybe the next one) to generate a NOP.
5893 In any case close it off with a .fill 0. */
5894
5895 static void
5896 xtensa_fix_short_loop_frags ()
5897 {
5898 frchainS *frchP;
5899
5900 /* When this routine is called, all of the subsections are still intact
5901 so we walk over subsections instead of sections. */
5902 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5903 {
5904 fragS *fragP;
5905 fragS *current_target = NULL;
5906 offsetT current_offset = 0;
5907 xtensa_opcode current_opcode = XTENSA_UNDEFINED;
5908
5909 /* Walk over all of the fragments in a subsection. */
5910 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5911 {
5912 /* check on the current loop */
5913 if (fragP->fr_type == rs_machine_dependent
5914 && fragP->fr_subtype == RELAX_IMMED)
5915 {
5916 /* Read it. If the instruction is a loop, get the target. */
5917 xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_opcode);
5918 if (is_loop_opcode (opcode))
5919 {
5920 TInsn t_insn;
5921
5922 tinsn_from_chars (&t_insn, fragP->fr_opcode);
5923 tinsn_immed_from_frag (&t_insn, fragP);
5924
5925 /* Get the current fragment target. */
5926 if (fragP->fr_symbol)
5927 {
5928 current_target = symbol_get_frag (fragP->fr_symbol);
5929 current_offset = fragP->fr_offset;
5930 current_opcode = opcode;
5931 }
5932 }
5933 }
5934
5935 if (fragP->fr_type == rs_machine_dependent
5936 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
5937 {
5938 size_t insn_count =
5939 count_insns_to_loop_end (fragP->fr_next, TRUE, 3);
5940 if (insn_count < 3
5941 && (branch_before_loop_end (fragP->fr_next)
5942 || (software_avoid_all_short_loops
5943 && current_opcode != XTENSA_UNDEFINED
5944 && !is_the_loop_opcode (current_opcode))))
5945 relax_frag_add_nop (fragP);
5946 else
5947 frag_wane (fragP);
5948 }
5949 }
5950 }
5951 }
5952
5953
5954 size_t
5955 count_insns_to_loop_end (base_fragP, count_relax_add, max_count)
5956 fragS *base_fragP;
5957 bfd_boolean count_relax_add;
5958 size_t max_count;
5959 {
5960 fragS *fragP = NULL;
5961 size_t insn_count = 0;
5962
5963 fragP = base_fragP;
5964
5965 for (; fragP && !fragP->tc_frag_data.is_loop_target; fragP = fragP->fr_next)
5966 {
5967 insn_count += unrelaxed_frag_min_insn_count (fragP);
5968 if (insn_count >= max_count)
5969 return max_count;
5970
5971 if (count_relax_add)
5972 {
5973 if (fragP->fr_type == rs_machine_dependent
5974 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
5975 {
5976 /* In order to add the appropriate number of
5977 NOPs, we count an instruction for downstream
5978 occurrences. */
5979 insn_count++;
5980 if (insn_count >= max_count)
5981 return max_count;
5982 }
5983 }
5984 }
5985 return insn_count;
5986 }
5987
5988
5989 size_t
5990 unrelaxed_frag_min_insn_count (fragP)
5991 fragS *fragP;
5992 {
5993 size_t insn_count = 0;
5994 int offset = 0;
5995
5996 if (!fragP->tc_frag_data.is_insn)
5997 return insn_count;
5998
5999 /* Decode the fixed instructions. */
6000 while (offset < fragP->fr_fix)
6001 {
6002 xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_literal + offset);
6003 if (opcode == XTENSA_UNDEFINED)
6004 {
6005 as_fatal (_("undecodable instruction in instruction frag"));
6006 return insn_count;
6007 }
6008 offset += xtensa_insn_length (xtensa_default_isa, opcode);
6009 insn_count++;
6010 }
6011
6012 return insn_count;
6013 }
6014
6015
6016 bfd_boolean
6017 branch_before_loop_end (base_fragP)
6018 fragS *base_fragP;
6019 {
6020 fragS *fragP;
6021
6022 for (fragP = base_fragP;
6023 fragP && !fragP->tc_frag_data.is_loop_target;
6024 fragP = fragP->fr_next)
6025 {
6026 if (unrelaxed_frag_has_b_j (fragP))
6027 return TRUE;
6028 }
6029 return FALSE;
6030 }
6031
6032
6033 bfd_boolean
6034 unrelaxed_frag_has_b_j (fragP)
6035 fragS *fragP;
6036 {
6037 size_t insn_count = 0;
6038 int offset = 0;
6039
6040 if (!fragP->tc_frag_data.is_insn)
6041 return FALSE;
6042
6043 /* Decode the fixed instructions. */
6044 while (offset < fragP->fr_fix)
6045 {
6046 xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_literal + offset);
6047 if (opcode == XTENSA_UNDEFINED)
6048 {
6049 as_fatal (_("undecodable instruction in instruction frag"));
6050 return insn_count;
6051 }
6052 if (is_branch_or_jump_opcode (opcode))
6053 return TRUE;
6054 offset += xtensa_insn_length (xtensa_default_isa, opcode);
6055 }
6056 return FALSE;
6057 }
6058
6059
6060 /* Checks to be made after initial assembly but before relaxation. */
6061
6062 static void
6063 xtensa_sanity_check ()
6064 {
6065 char *file_name;
6066 int line;
6067
6068 frchainS *frchP;
6069
6070 as_where (&file_name, &line);
6071 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
6072 {
6073 fragS *fragP;
6074
6075 /* Walk over all of the fragments in a subsection. */
6076 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
6077 {
6078 /* Currently we only check for empty loops here. */
6079 if (fragP->fr_type == rs_machine_dependent
6080 && fragP->fr_subtype == RELAX_IMMED)
6081 {
6082 static xtensa_insnbuf insnbuf = NULL;
6083 TInsn t_insn;
6084
6085 if (fragP->fr_opcode != NULL)
6086 {
6087 if (!insnbuf)
6088 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
6089 tinsn_from_chars (&t_insn, fragP->fr_opcode);
6090 tinsn_immed_from_frag (&t_insn, fragP);
6091
6092 if (is_loop_opcode (t_insn.opcode))
6093 {
6094 if (is_empty_loop (&t_insn, fragP))
6095 {
6096 new_logical_line (fragP->fr_file, fragP->fr_line);
6097 as_bad (_("invalid empty loop"));
6098 }
6099 if (!is_local_forward_loop (&t_insn, fragP))
6100 {
6101 new_logical_line (fragP->fr_file, fragP->fr_line);
6102 as_bad (_("loop target does not follow "
6103 "loop instruction in section"));
6104 }
6105 }
6106 }
6107 }
6108 }
6109 }
6110 new_logical_line (file_name, line);
6111 }
6112
6113
6114 #define LOOP_IMMED_OPN 1
6115
6116 /* Return true if the loop target is the next non-zero fragment. */
6117
6118 bfd_boolean
6119 is_empty_loop (insn, fragP)
6120 const TInsn *insn;
6121 fragS *fragP;
6122 {
6123 const expressionS *expr;
6124 symbolS *symbolP;
6125 fragS *next_fragP;
6126
6127 if (insn->insn_type != ITYPE_INSN)
6128 return FALSE;
6129
6130 if (!is_loop_opcode (insn->opcode))
6131 return FALSE;
6132
6133 if (insn->ntok <= LOOP_IMMED_OPN)
6134 return FALSE;
6135
6136 expr = &insn->tok[LOOP_IMMED_OPN];
6137
6138 if (expr->X_op != O_symbol)
6139 return FALSE;
6140
6141 symbolP = expr->X_add_symbol;
6142 if (!symbolP)
6143 return FALSE;
6144
6145 if (symbol_get_frag (symbolP) == NULL)
6146 return FALSE;
6147
6148 if (S_GET_VALUE (symbolP) != 0)
6149 return FALSE;
6150
6151 /* Walk through the zero-size fragments from this one. If we find
6152 the target fragment, then this is a zero-size loop. */
6153 for (next_fragP = fragP->fr_next;
6154 next_fragP != NULL;
6155 next_fragP = next_fragP->fr_next)
6156 {
6157 if (next_fragP == symbol_get_frag (symbolP))
6158 return TRUE;
6159 if (next_fragP->fr_fix != 0)
6160 return FALSE;
6161 }
6162 return FALSE;
6163 }
6164
6165
6166 bfd_boolean
6167 is_local_forward_loop (insn, fragP)
6168 const TInsn *insn;
6169 fragS *fragP;
6170 {
6171 const expressionS *expr;
6172 symbolS *symbolP;
6173 fragS *next_fragP;
6174
6175 if (insn->insn_type != ITYPE_INSN)
6176 return FALSE;
6177
6178 if (!is_loop_opcode (insn->opcode))
6179 return FALSE;
6180
6181 if (insn->ntok <= LOOP_IMMED_OPN)
6182 return FALSE;
6183
6184 expr = &insn->tok[LOOP_IMMED_OPN];
6185
6186 if (expr->X_op != O_symbol)
6187 return FALSE;
6188
6189 symbolP = expr->X_add_symbol;
6190 if (!symbolP)
6191 return FALSE;
6192
6193 if (symbol_get_frag (symbolP) == NULL)
6194 return FALSE;
6195
6196 /* Walk through fragments until we find the target.
6197 If we do not find the target, then this is an invalid loop. */
6198 for (next_fragP = fragP->fr_next;
6199 next_fragP != NULL;
6200 next_fragP = next_fragP->fr_next)
6201 if (next_fragP == symbol_get_frag (symbolP))
6202 return TRUE;
6203
6204 return FALSE;
6205 }
6206
6207 \f
6208 /* Alignment Functions. */
6209
6210 size_t
6211 get_text_align_power (target_size)
6212 int target_size;
6213 {
6214 size_t i = 0;
6215 for (i = 0; i < sizeof (size_t); i++)
6216 {
6217 if (target_size <= (1 << i))
6218 return i;
6219 }
6220 as_fatal (_("get_text_align_power: argument too large"));
6221 return 0;
6222 }
6223
6224
6225 addressT
6226 get_text_align_max_fill_size (align_pow, use_nops, use_no_density)
6227 int align_pow;
6228 bfd_boolean use_nops;
6229 bfd_boolean use_no_density;
6230 {
6231 if (!use_nops)
6232 return (1 << align_pow);
6233 if (use_no_density)
6234 return 3 * (1 << align_pow);
6235
6236 return 1 + (1 << align_pow);
6237 }
6238
6239
6240 /* get_text_align_fill_size ()
6241
6242 Desired alignments:
6243 give the address
6244 target_size = size of next instruction
6245 align_pow = get_text_align_power (target_size).
6246 use_nops = 0
6247 use_no_density = 0;
6248 Loop alignments:
6249 address = current address + loop instruction size;
6250 target_size = 3 (for 2 or 3 byte target)
6251 = 8 (for 8 byte target)
6252 align_pow = get_text_align_power (target_size);
6253 use_nops = 1
6254 use_no_density = set appropriately
6255 Text alignments:
6256 address = current address + loop instruction size;
6257 target_size = 0
6258 align_pow = get_text_align_power (target_size);
6259 use_nops = 0
6260 use_no_density = 0. */
6261
6262 addressT
6263 get_text_align_fill_size (address, align_pow, target_size,
6264 use_nops, use_no_density)
6265 addressT address;
6266 int align_pow;
6267 int target_size;
6268 bfd_boolean use_nops;
6269 bfd_boolean use_no_density;
6270 {
6271 /* Input arguments:
6272
6273 align_pow: log2 (required alignment).
6274
6275 target_size: alignment must allow the new_address and
6276 new_address+target_size-1.
6277
6278 use_nops: if true, then we can only use 2 or 3 byte nops.
6279
6280 use_no_density: if use_nops and use_no_density, we can only use
6281 3-byte nops.
6282
6283 Usually, for non-zero target_size, the align_pow is the power of 2
6284 that is greater than or equal to the target_size. This handles the
6285 2-byte, 3-byte and 8-byte instructions. */
6286
6287 size_t alignment = (1 << align_pow);
6288 if (!use_nops)
6289 {
6290 /* This is the easy case. */
6291 size_t mod;
6292 mod = address % alignment;
6293 if (mod != 0)
6294 mod = alignment - mod;
6295 assert ((address + mod) % alignment == 0);
6296 return mod;
6297 }
6298
6299 /* This is the slightly harder case. */
6300 assert ((int) alignment >= target_size);
6301 assert (target_size > 0);
6302 if (!use_no_density)
6303 {
6304 size_t i;
6305 for (i = 0; i < alignment * 2; i++)
6306 {
6307 if (i == 1)
6308 continue;
6309 if ((address + i) >> align_pow ==
6310 (address + i + target_size - 1) >> align_pow)
6311 return i;
6312 }
6313 }
6314 else
6315 {
6316 size_t i;
6317
6318 /* Can only fill multiples of 3. */
6319 for (i = 0; i <= alignment * 3; i += 3)
6320 {
6321 if ((address + i) >> align_pow ==
6322 (address + i + target_size - 1) >> align_pow)
6323 return i;
6324 }
6325 }
6326 assert (0);
6327 return 0;
6328 }
6329
6330
6331 /* This will assert if it is not possible. */
6332
6333 size_t
6334 get_text_align_nop_count (fill_size, use_no_density)
6335 size_t fill_size;
6336 bfd_boolean use_no_density;
6337 {
6338 size_t count = 0;
6339 if (use_no_density)
6340 {
6341 assert (fill_size % 3 == 0);
6342 return (fill_size / 3);
6343 }
6344
6345 assert (fill_size != 1); /* Bad argument. */
6346
6347 while (fill_size > 1)
6348 {
6349 size_t insn_size = 3;
6350 if (fill_size == 2 || fill_size == 4)
6351 insn_size = 2;
6352 fill_size -= insn_size;
6353 count++;
6354 }
6355 assert (fill_size != 1); /* Bad algorithm. */
6356 return count;
6357 }
6358
6359
6360 size_t
6361 get_text_align_nth_nop_size (fill_size, n, use_no_density)
6362 size_t fill_size;
6363 size_t n;
6364 bfd_boolean use_no_density;
6365 {
6366 size_t count = 0;
6367
6368 assert (get_text_align_nop_count (fill_size, use_no_density) > n);
6369
6370 if (use_no_density)
6371 return 3;
6372
6373 while (fill_size > 1)
6374 {
6375 size_t insn_size = 3;
6376 if (fill_size == 2 || fill_size == 4)
6377 insn_size = 2;
6378 fill_size -= insn_size;
6379 count++;
6380 if (n + 1 == count)
6381 return insn_size;
6382 }
6383 assert (0);
6384 return 0;
6385 }
6386
6387
6388 /* For the given fragment, find the appropriate address
6389 for it to begin at if we are using NOPs to align it. */
6390
6391 static addressT
6392 get_noop_aligned_address (fragP, address)
6393 fragS *fragP;
6394 addressT address;
6395 {
6396 static xtensa_insnbuf insnbuf = NULL;
6397 size_t fill_size = 0;
6398
6399 if (!insnbuf)
6400 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
6401
6402 switch (fragP->fr_type)
6403 {
6404 case rs_machine_dependent:
6405 if (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
6406 {
6407 /* The rule is: get next fragment's FIRST instruction. Find
6408 the smallest number of bytes that need to be added to
6409 ensure that the next fragment's FIRST instruction will fit
6410 in a single word.
6411
6412 E.G., 2 bytes : 0, 1, 2 mod 4
6413 3 bytes: 0, 1 mod 4
6414
6415 If the FIRST instruction MIGHT be relaxed,
6416 assume that it will become a 3 byte instruction. */
6417
6418 int target_insn_size;
6419 xtensa_opcode opcode = next_frag_opcode (fragP);
6420 addressT pre_opcode_bytes;
6421
6422 if (opcode == XTENSA_UNDEFINED)
6423 {
6424 as_bad_where (fragP->fr_file, fragP->fr_line,
6425 _("invalid opcode for RELAX_ALIGN_NEXT_OPCODE"));
6426 as_fatal (_("cannot continue"));
6427 }
6428
6429 target_insn_size = xtensa_insn_length (xtensa_default_isa, opcode);
6430
6431 pre_opcode_bytes = next_frag_pre_opcode_bytes (fragP);
6432
6433 if (is_loop_opcode (opcode))
6434 {
6435 /* next_fragP should be the loop. */
6436 const fragS *next_fragP = next_non_empty_frag (fragP);
6437 xtensa_opcode next_opcode = next_frag_opcode (next_fragP);
6438 size_t alignment;
6439
6440 pre_opcode_bytes += target_insn_size;
6441
6442 /* For loops, the alignment depends on the size of the
6443 instruction following the loop, not the loop instruction. */
6444 if (next_opcode == XTENSA_UNDEFINED)
6445 target_insn_size = 3;
6446 else
6447 {
6448 target_insn_size =
6449 xtensa_insn_length (xtensa_default_isa, next_opcode);
6450
6451 if (target_insn_size == 2)
6452 target_insn_size = 3; /* ISA specifies this. */
6453 }
6454
6455 /* If it was 8, then we'll need a larger alignment
6456 for the section. */
6457 alignment = get_text_align_power (target_insn_size);
6458
6459 /* Is Now_seg valid */
6460 record_alignment (now_seg, alignment);
6461 }
6462 else
6463 as_fatal (_("expected loop opcode in relax align next target"));
6464
6465 fill_size = get_text_align_fill_size
6466 (address + pre_opcode_bytes,
6467 get_text_align_power (target_insn_size),
6468 target_insn_size, TRUE, fragP->tc_frag_data.is_no_density);
6469 }
6470 break;
6471 #if 0
6472 case rs_align:
6473 case rs_align_code:
6474 fill_size = get_text_align_fill_size
6475 (address, fragP->fr_offset, 1, TRUE,
6476 fragP->tc_frag_data.is_no_density);
6477 break;
6478 #endif
6479 default:
6480 as_fatal (_("expected align_code or RELAX_ALIGN_NEXT_OPCODE"));
6481 }
6482
6483 return address + fill_size;
6484 }
6485
6486
6487 /* 3 mechanisms for relaxing an alignment:
6488
6489 Align to a power of 2.
6490 Align so the next fragment's instruction does not cross a word boundary.
6491 Align the current instruction so that if the next instruction
6492 were 3 bytes, it would not cross a word boundary.
6493
6494 We can align with:
6495
6496 zeros - This is easy; always insert zeros.
6497 nops - 3 and 2 byte instructions
6498 2 - 2 byte nop
6499 3 - 3 byte nop
6500 4 - 2, 2-byte nops
6501 >=5 : 3 byte instruction + fn(n-3)
6502 widening - widen previous instructions. */
6503
6504 static addressT
6505 get_widen_aligned_address (fragP, address)
6506 fragS *fragP;
6507 addressT address;
6508 {
6509 addressT align_pow, new_address, loop_insn_offset;
6510 fragS *next_frag;
6511 int insn_size;
6512 xtensa_opcode opcode, next_opcode;
6513 static xtensa_insnbuf insnbuf = NULL;
6514
6515 if (!insnbuf)
6516 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
6517
6518 if (fragP->fr_type == rs_align || fragP->fr_type == rs_align_code)
6519 {
6520 align_pow = fragP->fr_offset;
6521 new_address = ((address + ((1 << align_pow) - 1))
6522 << align_pow) >> align_pow;
6523 return new_address;
6524 }
6525
6526 if (fragP->fr_type == rs_machine_dependent)
6527 {
6528 switch (fragP->fr_subtype)
6529 {
6530 case RELAX_DESIRE_ALIGN:
6531
6532 /* The rule is: get the next fragment's FIRST instruction.
6533 Find the smallest number of bytes needed to be added
6534 in order to ensure that the next fragment is FIRST
6535 instruction will fit in a single word.
6536 i.e. 2 bytes : 0, 1, 2. mod 4
6537 3 bytes: 0, 1 mod 4
6538 If the FIRST instruction MIGHT be relaxed,
6539 assume that it will become a 3-byte instruction. */
6540
6541 insn_size = 3;
6542 /* Check to see if it might be 2 bytes. */
6543 next_opcode = next_frag_opcode (fragP);
6544 if (next_opcode != XTENSA_UNDEFINED
6545 && xtensa_insn_length (xtensa_default_isa, next_opcode) == 2)
6546 insn_size = 2;
6547
6548 assert (insn_size <= 4);
6549 for (new_address = address; new_address < address + 4; new_address++)
6550 {
6551 if (new_address >> 2 == (new_address + insn_size - 1) >> 2)
6552 return new_address;
6553 }
6554 as_bad (_("internal error aligning"));
6555 return address;
6556
6557 case RELAX_ALIGN_NEXT_OPCODE:
6558 /* The rule is: get next fragment's FIRST instruction.
6559 Find the smallest number of bytes needed to be added
6560 in order to ensure that the next fragment's FIRST
6561 instruction will fit in a single word.
6562 i.e. 2 bytes : 0, 1, 2. mod 4
6563 3 bytes: 0, 1 mod 4
6564 If the FIRST instruction MIGHT be relaxed,
6565 assume that it will become a 3 byte instruction. */
6566
6567 opcode = next_frag_opcode (fragP);
6568 if (opcode == XTENSA_UNDEFINED)
6569 {
6570 as_bad_where (fragP->fr_file, fragP->fr_line,
6571 _("invalid opcode for RELAX_ALIGN_NEXT_OPCODE"));
6572 as_fatal (_("cannot continue"));
6573 }
6574 insn_size = xtensa_insn_length (xtensa_default_isa, opcode);
6575 assert (insn_size <= 4);
6576 assert (is_loop_opcode (opcode));
6577
6578 loop_insn_offset = 0;
6579 next_frag = next_non_empty_frag (fragP);
6580
6581 /* If the loop has been expanded then the loop
6582 instruction could be at an offset from this fragment. */
6583 if (next_frag->fr_subtype != RELAX_IMMED)
6584 loop_insn_offset = get_expanded_loop_offset (opcode);
6585
6586 for (new_address = address; new_address < address + 4; new_address++)
6587 {
6588 if ((new_address + loop_insn_offset + insn_size) >> 2 ==
6589 (new_address + loop_insn_offset + insn_size + 2) >> 2)
6590 return new_address;
6591 }
6592 as_bad (_("internal error aligning"));
6593 return address;
6594
6595 default:
6596 as_bad (_("internal error aligning"));
6597 return address;
6598 }
6599 }
6600 as_bad (_("internal error aligning"));
6601 return address;
6602 }
6603
6604 \f
6605 /* md_relax_frag Hook and Helper Functions. */
6606
6607 /* Return the number of bytes added to this fragment, given that the
6608 input has been stretched already by "stretch". */
6609
6610 long
6611 xtensa_relax_frag (fragP, stretch, stretched_p)
6612 fragS *fragP;
6613 long stretch;
6614 int *stretched_p;
6615 {
6616 int unreported = fragP->tc_frag_data.unreported_expansion;
6617 long new_stretch = 0;
6618 char *file_name;
6619 int line, lit_size;
6620
6621 as_where (&file_name, &line);
6622 new_logical_line (fragP->fr_file, fragP->fr_line);
6623
6624 fragP->tc_frag_data.unreported_expansion = 0;
6625
6626 switch (fragP->fr_subtype)
6627 {
6628 case RELAX_ALIGN_NEXT_OPCODE:
6629 /* Always convert. */
6630 new_stretch = relax_frag_text_align (fragP, stretch);
6631 break;
6632
6633 case RELAX_LOOP_END:
6634 /* Do nothing. */
6635 break;
6636
6637 case RELAX_LOOP_END_ADD_NOP:
6638 /* Add a NOP and switch to .fill 0. */
6639 new_stretch = relax_frag_add_nop (fragP);
6640 break;
6641
6642 case RELAX_DESIRE_ALIGN:
6643 /* We REALLY want to change the relaxation order here. This
6644 should do NOTHING. The narrowing before it will either align
6645 it or not. */
6646 break;
6647
6648 case RELAX_LITERAL:
6649 case RELAX_LITERAL_FINAL:
6650 return 0;
6651
6652 case RELAX_LITERAL_NR:
6653 lit_size = 4;
6654 fragP->fr_subtype = RELAX_LITERAL_FINAL;
6655 assert (unreported == lit_size);
6656 memset (&fragP->fr_literal[fragP->fr_fix], 0, 4);
6657 fragP->fr_var -= lit_size;
6658 fragP->fr_fix += lit_size;
6659 new_stretch = 4;
6660 break;
6661
6662 case RELAX_NARROW:
6663 new_stretch = relax_frag_narrow (fragP, stretch);
6664 break;
6665
6666 case RELAX_IMMED:
6667 case RELAX_IMMED_STEP1:
6668 case RELAX_IMMED_STEP2:
6669 /* Place the immediate. */
6670 new_stretch = relax_frag_immed (now_seg, fragP, stretch,
6671 fragP->fr_subtype - RELAX_IMMED,
6672 stretched_p);
6673 break;
6674
6675 case RELAX_LITERAL_POOL_BEGIN:
6676 case RELAX_LITERAL_POOL_END:
6677 /* No relaxation required. */
6678 break;
6679
6680 default:
6681 as_bad (_("bad relaxation state"));
6682 }
6683
6684 new_logical_line (file_name, line);
6685 return new_stretch;
6686 }
6687
6688
6689 static long
6690 relax_frag_text_align (fragP, stretch)
6691 fragS *fragP;
6692 long stretch;
6693 {
6694 addressT old_address, old_next_address, old_size;
6695 addressT new_address, new_next_address, new_size;
6696 addressT growth;
6697
6698 /* Overview of the relaxation procedure for alignment
6699 inside an executable section:
6700
6701 The old size is stored in the tc_frag_data.text_expansion field.
6702
6703 Calculate the new address, fix up the text_expansion and
6704 return the growth. */
6705
6706 /* Calculate the old address of this fragment and the next fragment. */
6707 old_address = fragP->fr_address - stretch;
6708 old_next_address = (fragP->fr_address - stretch + fragP->fr_fix +
6709 fragP->tc_frag_data.text_expansion);
6710 old_size = old_next_address - old_address;
6711
6712 /* Calculate the new address of this fragment and the next fragment. */
6713 new_address = fragP->fr_address;
6714 new_next_address =
6715 get_noop_aligned_address (fragP, fragP->fr_address + fragP->fr_fix);
6716 new_size = new_next_address - new_address;
6717
6718 growth = new_size - old_size;
6719
6720 /* Fix up the text_expansion field and return the new growth. */
6721 fragP->tc_frag_data.text_expansion += growth;
6722 return growth;
6723 }
6724
6725
6726 /* Add a NOP (i.e., "or a1, a1, a1"). Use the 3-byte one because we
6727 don't know about the availability of density yet. TODO: When the
6728 flags are stored per fragment, use NOP.N when possible. */
6729
6730 static long
6731 relax_frag_add_nop (fragP)
6732 fragS *fragP;
6733 {
6734 static xtensa_insnbuf insnbuf = NULL;
6735 TInsn t_insn;
6736 char *nop_buf = fragP->fr_literal + fragP->fr_fix;
6737 int length;
6738 if (!insnbuf)
6739 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
6740
6741 tinsn_init (&t_insn);
6742 t_insn.opcode = xtensa_or_opcode;
6743 assert (t_insn.opcode != XTENSA_UNDEFINED);
6744
6745 t_insn.ntok = 3;
6746 set_expr_const (&t_insn.tok[0], 1);
6747 set_expr_const (&t_insn.tok[1], 1);
6748 set_expr_const (&t_insn.tok[2], 1);
6749
6750 tinsn_to_insnbuf (&t_insn, insnbuf);
6751 fragP->tc_frag_data.is_insn = TRUE;
6752 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, nop_buf);
6753
6754 length = xtensa_insn_length (xtensa_default_isa, t_insn.opcode);
6755 if (fragP->fr_var < length)
6756 {
6757 as_warn (_("fr_var (%ld) < length (%d); ignoring"),
6758 fragP->fr_var, length);
6759 frag_wane (fragP);
6760 return 0;
6761 }
6762
6763 fragP->fr_fix += length;
6764 fragP->fr_var -= length;
6765 frag_wane (fragP);
6766 return length;
6767 }
6768
6769
6770 static long
6771 relax_frag_narrow (fragP, stretch)
6772 fragS *fragP;
6773 long stretch;
6774 {
6775 /* Overview of the relaxation procedure for alignment inside an
6776 executable section: Find the number of widenings required and the
6777 number of nop bytes required. Store the number of bytes ALREADY
6778 widened. If there are enough instructions to widen (must go back
6779 ONLY through NARROW fragments), mark each of the fragments as TO BE
6780 widened, recalculate the fragment addresses. */
6781
6782 assert (fragP->fr_type == rs_machine_dependent
6783 && fragP->fr_subtype == RELAX_NARROW);
6784
6785 if (!future_alignment_required (fragP, 0))
6786 {
6787 /* If already expanded but no longer needed because of a prior
6788 stretch, it is SAFE to unexpand because the next fragment will
6789 NEVER start at an address > the previous time through the
6790 relaxation. */
6791 if (fragP->tc_frag_data.text_expansion)
6792 {
6793 if (stretch > 0)
6794 {
6795 fragP->tc_frag_data.text_expansion = 0;
6796 return -1;
6797 }
6798 /* Otherwise we have to live with this bad choice. */
6799 return 0;
6800 }
6801 return 0;
6802 }
6803
6804 if (fragP->tc_frag_data.text_expansion == 0)
6805 {
6806 fragP->tc_frag_data.text_expansion = 1;
6807 return 1;
6808 }
6809
6810 return 0;
6811 }
6812
6813
6814 static bfd_boolean
6815 future_alignment_required (fragP, stretch)
6816 fragS *fragP;
6817 long stretch;
6818 {
6819 long address = fragP->fr_address + stretch;
6820 int num_widens = 0;
6821 addressT aligned_address;
6822 offsetT desired_diff;
6823
6824 while (fragP)
6825 {
6826 /* Limit this to a small search. */
6827 if (num_widens > 8)
6828 return FALSE;
6829 address += fragP->fr_fix;
6830
6831 switch (fragP->fr_type)
6832 {
6833 case rs_fill:
6834 address += fragP->fr_offset * fragP->fr_var;
6835 break;
6836
6837 case rs_machine_dependent:
6838 switch (fragP->fr_subtype)
6839 {
6840 case RELAX_NARROW:
6841 /* address += fragP->fr_fix; */
6842 num_widens++;
6843 break;
6844
6845 case RELAX_IMMED:
6846 address += (/* fragP->fr_fix + */
6847 fragP->tc_frag_data.text_expansion);
6848 break;
6849
6850 case RELAX_ALIGN_NEXT_OPCODE:
6851 case RELAX_DESIRE_ALIGN:
6852 /* address += fragP->fr_fix; */
6853 aligned_address = get_widen_aligned_address (fragP, address);
6854 desired_diff = aligned_address - address;
6855 assert (desired_diff >= 0);
6856 /* If there are enough wideners in between do it. */
6857 /* return (num_widens == desired_diff); */
6858 if (num_widens == desired_diff)
6859 return TRUE;
6860 if (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
6861 return FALSE;
6862 break;
6863
6864 default:
6865 return FALSE;
6866 }
6867 break;
6868
6869 default:
6870 return FALSE;
6871 }
6872 fragP = fragP->fr_next;
6873 }
6874
6875 return FALSE;
6876 }
6877
6878
6879 static long
6880 relax_frag_immed (segP, fragP, stretch, min_steps, stretched_p)
6881 segT segP;
6882 fragS *fragP;
6883 long stretch;
6884 int min_steps;
6885 int *stretched_p;
6886 {
6887 static xtensa_insnbuf insnbuf = NULL;
6888 TInsn t_insn;
6889 int old_size;
6890 bfd_boolean negatable_branch = FALSE;
6891 bfd_boolean branch_jmp_to_next = FALSE;
6892 IStack istack;
6893 offsetT frag_offset;
6894 int num_steps;
6895 fragS *lit_fragP;
6896 int num_text_bytes, num_literal_bytes;
6897 int literal_diff, text_diff;
6898
6899 assert (fragP->fr_opcode != NULL);
6900
6901 if (!insnbuf)
6902 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
6903
6904 tinsn_from_chars (&t_insn, fragP->fr_opcode);
6905 tinsn_immed_from_frag (&t_insn, fragP);
6906
6907 negatable_branch = is_negatable_branch (&t_insn);
6908
6909 old_size = xtensa_insn_length (xtensa_default_isa, t_insn.opcode);
6910
6911 if (software_avoid_b_j_loop_end)
6912 branch_jmp_to_next = is_branch_jmp_to_next (&t_insn, fragP);
6913
6914 /* Special case: replace a branch to the next instruction with a NOP.
6915 This is required to work around a hardware bug in T1040.0 and also
6916 serves as an optimization. */
6917
6918 if (branch_jmp_to_next
6919 && ((old_size == 2) || (old_size == 3))
6920 && !next_frag_is_loop_target (fragP))
6921 return 0;
6922
6923 /* Here is the fun stuff: Get the immediate field from this
6924 instruction. If it fits, we are done. If not, find the next
6925 instruction sequence that fits. */
6926
6927 frag_offset = fragP->fr_opcode - fragP->fr_literal;
6928 istack_init (&istack);
6929 num_steps = xg_assembly_relax (&istack, &t_insn, segP, fragP, frag_offset,
6930 min_steps, stretch);
6931 if (num_steps < min_steps)
6932 {
6933 as_fatal (_("internal error: relaxation failed"));
6934 return 0;
6935 }
6936
6937 if (num_steps > RELAX_IMMED_MAXSTEPS)
6938 {
6939 as_fatal (_("internal error: relaxation requires too many steps"));
6940 return 0;
6941 }
6942
6943 fragP->fr_subtype = (int) RELAX_IMMED + num_steps;
6944
6945 /* Figure out the number of bytes needed. */
6946 lit_fragP = 0;
6947 num_text_bytes = get_num_stack_text_bytes (&istack) - old_size;
6948 num_literal_bytes = get_num_stack_literal_bytes (&istack);
6949 literal_diff = num_literal_bytes - fragP->tc_frag_data.literal_expansion;
6950 text_diff = num_text_bytes - fragP->tc_frag_data.text_expansion;
6951
6952 /* It MUST get larger. If not, we could get an infinite loop. */
6953 know (num_text_bytes >= 0);
6954 know (literal_diff >= 0 && text_diff >= 0);
6955
6956 fragP->tc_frag_data.text_expansion = num_text_bytes;
6957 fragP->tc_frag_data.literal_expansion = num_literal_bytes;
6958
6959 /* Find the associated expandable literal for this. */
6960 if (literal_diff != 0)
6961 {
6962 lit_fragP = fragP->tc_frag_data.literal_frag;
6963 if (lit_fragP)
6964 {
6965 assert (literal_diff == 4);
6966 lit_fragP->tc_frag_data.unreported_expansion += literal_diff;
6967
6968 /* We expect that the literal section state has NOT been
6969 modified yet. */
6970 assert (lit_fragP->fr_type == rs_machine_dependent
6971 && lit_fragP->fr_subtype == RELAX_LITERAL);
6972 lit_fragP->fr_subtype = RELAX_LITERAL_NR;
6973
6974 /* We need to mark this section for another iteration
6975 of relaxation. */
6976 (*stretched_p)++;
6977 }
6978 }
6979
6980 /* This implicitly uses the assumption that a branch is negated
6981 when the size of the output increases by at least 2 bytes. */
6982
6983 if (negatable_branch && num_text_bytes >= 2)
6984 {
6985 /* If next frag is a loop end, then switch it to add a NOP. */
6986 update_next_frag_nop_state (fragP);
6987 }
6988
6989 return text_diff;
6990 }
6991
6992 \f
6993 /* md_convert_frag Hook and Helper Functions. */
6994
6995 void
6996 md_convert_frag (abfd, sec, fragp)
6997 bfd *abfd ATTRIBUTE_UNUSED;
6998 segT sec;
6999 fragS *fragp;
7000 {
7001 char *file_name;
7002 int line;
7003
7004 as_where (&file_name, &line);
7005 new_logical_line (fragp->fr_file, fragp->fr_line);
7006
7007 switch (fragp->fr_subtype)
7008 {
7009 case RELAX_ALIGN_NEXT_OPCODE:
7010 /* Always convert. */
7011 convert_frag_align_next_opcode (fragp);
7012 break;
7013
7014 case RELAX_DESIRE_ALIGN:
7015 /* Do nothing. If not aligned already, too bad. */
7016 break;
7017
7018 case RELAX_LITERAL:
7019 case RELAX_LITERAL_FINAL:
7020 break;
7021
7022 case RELAX_NARROW:
7023 /* No conversion. */
7024 convert_frag_narrow (fragp);
7025 break;
7026
7027 case RELAX_IMMED:
7028 case RELAX_IMMED_STEP1:
7029 case RELAX_IMMED_STEP2:
7030 /* Place the immediate. */
7031 convert_frag_immed (sec, fragp, fragp->fr_subtype - RELAX_IMMED);
7032 break;
7033
7034 case RELAX_LITERAL_NR:
7035 if (use_literal_section)
7036 {
7037 /* This should have been handled during relaxation. When
7038 relaxing a code segment, literals sometimes need to be
7039 added to the corresponding literal segment. If that
7040 literal segment has already been relaxed, then we end up
7041 in this situation. Marking the literal segments as data
7042 would make this happen less often (since GAS always relaxes
7043 code before data), but we could still get into trouble if
7044 there are instructions in a segment that is not marked as
7045 containing code. Until we can implement a better solution,
7046 cheat and adjust the addresses of all the following frags.
7047 This could break subsequent alignments, but the linker's
7048 literal coalescing will do that anyway. */
7049
7050 fragS *f;
7051 fragp->fr_subtype = RELAX_LITERAL_FINAL;
7052 assert (fragp->tc_frag_data.unreported_expansion == 4);
7053 memset (&fragp->fr_literal[fragp->fr_fix], 0, 4);
7054 fragp->fr_var -= 4;
7055 fragp->fr_fix += 4;
7056 for (f = fragp->fr_next; f; f = f->fr_next)
7057 f->fr_address += 4;
7058 }
7059 else
7060 as_bad (_("invalid relaxation fragment result"));
7061 break;
7062 }
7063
7064 fragp->fr_var = 0;
7065 new_logical_line (file_name, line);
7066 }
7067
7068
7069 void
7070 convert_frag_align_next_opcode (fragp)
7071 fragS *fragp;
7072 {
7073 char *nop_buf; /* Location for Writing. */
7074 size_t i;
7075
7076 bfd_boolean use_no_density = fragp->tc_frag_data.is_no_density;
7077 addressT aligned_address;
7078 size_t fill_size, nop_count;
7079
7080 aligned_address = get_noop_aligned_address (fragp, fragp->fr_address +
7081 fragp->fr_fix);
7082 fill_size = aligned_address - (fragp->fr_address + fragp->fr_fix);
7083 nop_count = get_text_align_nop_count (fill_size, use_no_density);
7084 nop_buf = fragp->fr_literal + fragp->fr_fix;
7085
7086 for (i = 0; i < nop_count; i++)
7087 {
7088 size_t nop_size;
7089 nop_size = get_text_align_nth_nop_size (fill_size, i, use_no_density);
7090
7091 assemble_nop (nop_size, nop_buf);
7092 nop_buf += nop_size;
7093 }
7094
7095 fragp->fr_fix += fill_size;
7096 fragp->fr_var -= fill_size;
7097 }
7098
7099
7100 static void
7101 convert_frag_narrow (fragP)
7102 fragS *fragP;
7103 {
7104 static xtensa_insnbuf insnbuf = NULL;
7105 TInsn t_insn, single_target;
7106 int size, old_size, diff, error_val;
7107 offsetT frag_offset;
7108
7109 if (fragP->tc_frag_data.text_expansion == 0)
7110 {
7111 /* No conversion. */
7112 fragP->fr_var = 0;
7113 return;
7114 }
7115
7116 assert (fragP->fr_opcode != NULL);
7117
7118 if (!insnbuf)
7119 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
7120
7121 tinsn_from_chars (&t_insn, fragP->fr_opcode);
7122 tinsn_immed_from_frag (&t_insn, fragP);
7123
7124 /* Just convert it to a wide form.... */
7125 size = 0;
7126 old_size = xtensa_insn_length (xtensa_default_isa, t_insn.opcode);
7127
7128 tinsn_init (&single_target);
7129 frag_offset = fragP->fr_opcode - fragP->fr_literal;
7130
7131 error_val = xg_expand_narrow (&single_target, &t_insn);
7132 if (error_val)
7133 as_bad (_("unable to widen instruction"));
7134
7135 size = xtensa_insn_length (xtensa_default_isa, single_target.opcode);
7136 xg_emit_insn_to_buf (&single_target, fragP->fr_opcode,
7137 fragP, frag_offset, TRUE);
7138
7139 diff = size - old_size;
7140 assert (diff >= 0);
7141 assert (diff <= fragP->fr_var);
7142 fragP->fr_var -= diff;
7143 fragP->fr_fix += diff;
7144
7145 /* clean it up */
7146 fragP->fr_var = 0;
7147 }
7148
7149
7150 static void
7151 convert_frag_immed (segP, fragP, min_steps)
7152 segT segP;
7153 fragS *fragP;
7154 int min_steps;
7155 {
7156 char *immed_instr = fragP->fr_opcode;
7157 static xtensa_insnbuf insnbuf = NULL;
7158 TInsn orig_t_insn;
7159 bfd_boolean expanded = FALSE;
7160 char *fr_opcode = fragP->fr_opcode;
7161 bfd_boolean branch_jmp_to_next = FALSE;
7162 int size;
7163
7164 assert (fragP->fr_opcode != NULL);
7165
7166 if (!insnbuf)
7167 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
7168
7169 tinsn_from_chars (&orig_t_insn, fragP->fr_opcode);
7170 tinsn_immed_from_frag (&orig_t_insn, fragP);
7171
7172 /* Here is the fun stuff: Get the immediate field from this
7173 instruction. If it fits, we're done. If not, find the next
7174 instruction sequence that fits. */
7175
7176 if (software_avoid_b_j_loop_end)
7177 branch_jmp_to_next = is_branch_jmp_to_next (&orig_t_insn, fragP);
7178
7179 if (branch_jmp_to_next && !next_frag_is_loop_target (fragP))
7180 {
7181 /* Conversion just inserts a NOP and marks the fix as completed. */
7182 size = xtensa_insn_length (xtensa_default_isa, orig_t_insn.opcode);
7183 assemble_nop (size, fragP->fr_opcode);
7184 fragP->fr_var = 0;
7185 }
7186 else
7187 {
7188 IStack istack;
7189 int i;
7190 symbolS *lit_sym = NULL;
7191 int total_size = 0;
7192 int old_size;
7193 int diff;
7194 symbolS *gen_label = NULL;
7195 offsetT frag_offset;
7196
7197 /* It does not fit. Find something that does and
7198 convert immediately. */
7199 frag_offset = fragP->fr_opcode - fragP->fr_literal;
7200 istack_init (&istack);
7201 xg_assembly_relax (&istack, &orig_t_insn,
7202 segP, fragP, frag_offset, min_steps, 0);
7203
7204 old_size = xtensa_insn_length (xtensa_default_isa, orig_t_insn.opcode);
7205
7206 /* Assemble this right inline. */
7207
7208 /* First, create the mapping from a label name to the REAL label. */
7209 total_size = 0;
7210 for (i = 0; i < istack.ninsn; i++)
7211 {
7212 TInsn *t_insn = &istack.insn[i];
7213 int size = 0;
7214 fragS *lit_frag;
7215
7216 switch (t_insn->insn_type)
7217 {
7218 case ITYPE_LITERAL:
7219 if (lit_sym != NULL)
7220 as_bad (_("multiple literals in expansion"));
7221 /* First find the appropriate space in the literal pool. */
7222 lit_frag = fragP->tc_frag_data.literal_frag;
7223 if (lit_frag == NULL)
7224 as_bad (_("no registered fragment for literal"));
7225 if (t_insn->ntok != 1)
7226 as_bad (_("number of literal tokens != 1"));
7227
7228 /* Set the literal symbol and add a fixup. */
7229 lit_sym = lit_frag->fr_symbol;
7230 break;
7231
7232 case ITYPE_LABEL:
7233 assert (gen_label == NULL);
7234 gen_label = symbol_new (FAKE_LABEL_NAME, now_seg,
7235 fragP->fr_opcode - fragP->fr_literal +
7236 total_size, fragP);
7237 break;
7238
7239 case ITYPE_INSN:
7240 size = xtensa_insn_length (xtensa_default_isa, t_insn->opcode);
7241 total_size += size;
7242 break;
7243 }
7244 }
7245
7246 total_size = 0;
7247 for (i = 0; i < istack.ninsn; i++)
7248 {
7249 TInsn *t_insn = &istack.insn[i];
7250 fragS *lit_frag;
7251 int size;
7252 segT target_seg;
7253
7254 switch (t_insn->insn_type)
7255 {
7256 case ITYPE_LITERAL:
7257 lit_frag = fragP->tc_frag_data.literal_frag;
7258 /* already checked */
7259 assert (lit_frag != NULL);
7260 assert (lit_sym != NULL);
7261 assert (t_insn->ntok == 1);
7262 /* add a fixup */
7263 target_seg = S_GET_SEGMENT (lit_sym);
7264 assert (target_seg);
7265 fix_new_exp_in_seg (target_seg, 0, lit_frag, 0, 4,
7266 &t_insn->tok[0], FALSE, BFD_RELOC_32);
7267 break;
7268
7269 case ITYPE_LABEL:
7270 break;
7271
7272 case ITYPE_INSN:
7273 xg_resolve_labels (t_insn, gen_label);
7274 xg_resolve_literals (t_insn, lit_sym);
7275 size = xtensa_insn_length (xtensa_default_isa, t_insn->opcode);
7276 total_size += size;
7277 xg_emit_insn_to_buf (t_insn, immed_instr, fragP,
7278 immed_instr - fragP->fr_literal, TRUE);
7279 immed_instr += size;
7280 break;
7281 }
7282 }
7283
7284 diff = total_size - old_size;
7285 assert (diff >= 0);
7286 if (diff != 0)
7287 expanded = TRUE;
7288 assert (diff <= fragP->fr_var);
7289 fragP->fr_var -= diff;
7290 fragP->fr_fix += diff;
7291 }
7292
7293 /* Clean it up. */
7294 fragP->fr_var = 0;
7295
7296 /* Check for undefined immediates in LOOP instructions. */
7297 if (is_loop_opcode (orig_t_insn.opcode))
7298 {
7299 symbolS *sym;
7300 sym = orig_t_insn.tok[1].X_add_symbol;
7301 if (sym != NULL && !S_IS_DEFINED (sym))
7302 {
7303 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
7304 return;
7305 }
7306 sym = orig_t_insn.tok[1].X_op_symbol;
7307 if (sym != NULL && !S_IS_DEFINED (sym))
7308 {
7309 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
7310 return;
7311 }
7312 }
7313
7314 if (expanded && is_loop_opcode (orig_t_insn.opcode))
7315 convert_frag_immed_finish_loop (segP, fragP, &orig_t_insn);
7316
7317 if (expanded && is_direct_call_opcode (orig_t_insn.opcode))
7318 {
7319 /* Add an expansion note on the expanded instruction. */
7320 fix_new_exp_in_seg (now_seg, 0, fragP, fr_opcode - fragP->fr_literal, 4,
7321 &orig_t_insn.tok[0], TRUE,
7322 BFD_RELOC_XTENSA_ASM_EXPAND);
7323
7324 }
7325 }
7326
7327
7328 /* Add a new fix expression into the desired segment. We have to
7329 switch to that segment to do this. */
7330
7331 static fixS *
7332 fix_new_exp_in_seg (new_seg, new_subseg,
7333 frag, where, size, exp, pcrel, r_type)
7334 segT new_seg;
7335 subsegT new_subseg;
7336 fragS *frag;
7337 int where;
7338 int size;
7339 expressionS *exp;
7340 int pcrel;
7341 bfd_reloc_code_real_type r_type;
7342 {
7343 fixS *new_fix;
7344 segT seg = now_seg;
7345 subsegT subseg = now_subseg;
7346 assert (new_seg != 0);
7347 subseg_set (new_seg, new_subseg);
7348
7349 if (r_type == BFD_RELOC_32
7350 && exp->X_add_symbol
7351 && exp->X_add_symbol->sy_tc.plt == 1)
7352 {
7353 r_type = BFD_RELOC_XTENSA_PLT;
7354 }
7355
7356 new_fix = fix_new_exp (frag, where, size, exp, pcrel, r_type);
7357 subseg_set (seg, subseg);
7358 return new_fix;
7359 }
7360
7361
7362 /* Relax a loop instruction so that it can span loop >256 bytes. */
7363 /*
7364 loop as, .L1
7365 .L0:
7366 rsr as, LEND
7367 wsr as, LBEG
7368 addi as, as, lo8(label-.L1)
7369 addmi as, as, mid8(label-.L1)
7370 wsr as, LEND
7371 isync
7372 rsr as, LCOUNT
7373 addi as, as, 1
7374 .L1:
7375 <<body>>
7376 label: */
7377
7378 static void
7379 convert_frag_immed_finish_loop (segP, fragP, t_insn)
7380 segT segP;
7381 fragS *fragP;
7382 TInsn *t_insn;
7383 {
7384 TInsn loop_insn;
7385 TInsn addi_insn;
7386 TInsn addmi_insn;
7387 unsigned long target;
7388 static xtensa_insnbuf insnbuf = NULL;
7389 unsigned int loop_length, loop_length_hi, loop_length_lo;
7390 xtensa_isa isa = xtensa_default_isa;
7391 addressT loop_offset;
7392 addressT addi_offset = 9;
7393 addressT addmi_offset = 12;
7394
7395 if (!insnbuf)
7396 insnbuf = xtensa_insnbuf_alloc (isa);
7397
7398 /* Get the loop offset. */
7399 loop_offset = get_expanded_loop_offset (t_insn->opcode);
7400 /* Validate that there really is a LOOP at the loop_offset. */
7401 tinsn_from_chars (&loop_insn, fragP->fr_opcode + loop_offset);
7402
7403 if (!is_loop_opcode (loop_insn.opcode))
7404 {
7405 as_bad_where (fragP->fr_file, fragP->fr_line,
7406 _("loop relaxation specification does not correspond"));
7407 assert (0);
7408 }
7409 addi_offset += loop_offset;
7410 addmi_offset += loop_offset;
7411
7412 assert (t_insn->ntok == 2);
7413 target = get_expression_value (segP, &t_insn->tok[1]);
7414
7415 know (symbolP);
7416 know (symbolP->sy_frag);
7417 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
7418 || symbol_get_frag (symbolP) == &zero_address_frag);
7419
7420 loop_length = target - (fragP->fr_address + fragP->fr_fix);
7421 loop_length_hi = loop_length & ~0x0ff;
7422 loop_length_lo = loop_length & 0x0ff;
7423 if (loop_length_lo >= 128)
7424 {
7425 loop_length_lo -= 256;
7426 loop_length_hi += 256;
7427 }
7428
7429 /* Because addmi sign-extends the immediate, 'loop_length_hi' can be at most
7430 32512. If the loop is larger than that, then we just fail. */
7431 if (loop_length_hi > 32512)
7432 as_bad_where (fragP->fr_file, fragP->fr_line,
7433 _("loop too long for LOOP instruction"));
7434
7435 tinsn_from_chars (&addi_insn, fragP->fr_opcode + addi_offset);
7436 assert (addi_insn.opcode == xtensa_addi_opcode);
7437
7438 tinsn_from_chars (&addmi_insn, fragP->fr_opcode + addmi_offset);
7439 assert (addmi_insn.opcode == xtensa_addmi_opcode);
7440
7441 set_expr_const (&addi_insn.tok[2], loop_length_lo);
7442 tinsn_to_insnbuf (&addi_insn, insnbuf);
7443
7444 fragP->tc_frag_data.is_insn = TRUE;
7445 xtensa_insnbuf_to_chars (isa, insnbuf, fragP->fr_opcode + addi_offset);
7446
7447 set_expr_const (&addmi_insn.tok[2], loop_length_hi);
7448 tinsn_to_insnbuf (&addmi_insn, insnbuf);
7449 xtensa_insnbuf_to_chars (isa, insnbuf, fragP->fr_opcode + addmi_offset);
7450 }
7451
7452
7453 static offsetT
7454 get_expression_value (segP, exp)
7455 segT segP;
7456 expressionS *exp;
7457 {
7458 if (exp->X_op == O_constant)
7459 return exp->X_add_number;
7460 if (exp->X_op == O_symbol)
7461 {
7462 /* Find the fragment. */
7463 symbolS *sym = exp->X_add_symbol;
7464
7465 assert (S_GET_SEGMENT (sym) == segP
7466 || S_GET_SEGMENT (sym) == absolute_section);
7467
7468 return (S_GET_VALUE (sym) + exp->X_add_number);
7469 }
7470 as_bad (_("invalid expression evaluation type %d"), exp->X_op);
7471 return 0;
7472 }
7473
7474 \f
7475 /* A map that keeps information on a per-subsegment basis. This is
7476 maintained during initial assembly, but is invalid once the
7477 subsegments are smashed together. I.E., it cannot be used during
7478 the relaxation. */
7479
7480 typedef struct subseg_map_struct
7481 {
7482 /* the key */
7483 segT seg;
7484 subsegT subseg;
7485
7486 /* the data */
7487 unsigned flags;
7488
7489 struct subseg_map_struct *next;
7490 } subseg_map;
7491
7492 static subseg_map *sseg_map = NULL;
7493
7494
7495 static unsigned
7496 get_last_insn_flags (seg, subseg)
7497 segT seg;
7498 subsegT subseg;
7499 {
7500 subseg_map *subseg_e;
7501
7502 for (subseg_e = sseg_map; subseg_e != NULL; subseg_e = subseg_e->next)
7503 if (seg == subseg_e->seg && subseg == subseg_e->subseg)
7504 return subseg_e->flags;
7505
7506 return 0;
7507 }
7508
7509
7510 static void
7511 set_last_insn_flags (seg, subseg, fl, val)
7512 segT seg;
7513 subsegT subseg;
7514 unsigned fl;
7515 bfd_boolean val;
7516 {
7517 subseg_map *subseg_e;
7518
7519 for (subseg_e = sseg_map; subseg_e; subseg_e = subseg_e->next)
7520 if (seg == subseg_e->seg && subseg == subseg_e->subseg)
7521 break;
7522
7523 if (!subseg_e)
7524 {
7525 subseg_e = (subseg_map *) xmalloc (sizeof (subseg_map));
7526 memset (subseg_e, 0, sizeof (subseg_map));
7527 subseg_e->seg = seg;
7528 subseg_e->subseg = subseg;
7529 subseg_e->flags = 0;
7530 subseg_e->next = sseg_map;
7531 sseg_map = subseg_e;
7532 }
7533
7534 if (val)
7535 subseg_e->flags |= fl;
7536 else
7537 subseg_e->flags &= ~fl;
7538 }
7539
7540 \f
7541 /* Segment Lists and emit_state Stuff. */
7542
7543 /* Remove the segment from the global sections list. */
7544
7545 static void
7546 xtensa_remove_section (sec)
7547 segT sec;
7548 {
7549 /* Handle brain-dead bfd_section_list_remove macro, which
7550 expect the address of the prior section's "next" field, not
7551 just the address of the section to remove. */
7552
7553 segT *ps_next_ptr = &stdoutput->sections;
7554 while (*ps_next_ptr != sec && *ps_next_ptr != NULL)
7555 ps_next_ptr = &(*ps_next_ptr)->next;
7556
7557 assert (*ps_next_ptr != NULL);
7558
7559 bfd_section_list_remove (stdoutput, ps_next_ptr);
7560 }
7561
7562
7563 static void
7564 xtensa_insert_section (after_sec, sec)
7565 segT after_sec;
7566 segT sec;
7567 {
7568 segT *after_sec_next;
7569 if (after_sec == NULL)
7570 after_sec_next = &stdoutput->sections;
7571 else
7572 after_sec_next = &after_sec->next;
7573
7574 bfd_section_list_insert (stdoutput, after_sec_next, sec);
7575 }
7576
7577
7578 static void
7579 xtensa_move_seg_list_to_beginning (head)
7580 seg_list *head;
7581 {
7582 head = head->next;
7583 while (head)
7584 {
7585 segT literal_section = head->seg;
7586
7587 /* Move the literal section to the front of the section list. */
7588 assert (literal_section);
7589 xtensa_remove_section (literal_section);
7590 xtensa_insert_section (NULL, literal_section);
7591
7592 head = head->next;
7593 }
7594 }
7595
7596
7597 void
7598 xtensa_move_literals ()
7599 {
7600 seg_list *segment;
7601 frchainS *frchain_from, *frchain_to;
7602 fragS *search_frag, *next_frag, *last_frag, *literal_pool, *insert_after;
7603 fragS **frag_splice;
7604 emit_state state;
7605 segT dest_seg;
7606 fixS *fix, *next_fix, **fix_splice;
7607
7608 /* As clunky as this is, we can't rely on frag_var
7609 and frag_variant to get called in all situations. */
7610
7611 segment = literal_head->next;
7612 while (segment)
7613 {
7614 frchain_from = seg_info (segment->seg)->frchainP;
7615 search_frag = frchain_from->frch_root;
7616 while (search_frag)
7617 {
7618 search_frag->tc_frag_data.is_literal = TRUE;
7619 search_frag = search_frag->fr_next;
7620 }
7621 segment = segment->next;
7622 }
7623
7624 if (use_literal_section)
7625 return;
7626
7627 segment = literal_head->next;
7628 while (segment)
7629 {
7630 frchain_from = seg_info (segment->seg)->frchainP;
7631 search_frag = frchain_from->frch_root;
7632 literal_pool = NULL;
7633 frchain_to = NULL;
7634 frag_splice = &(frchain_from->frch_root);
7635
7636 while (!search_frag->tc_frag_data.literal_frag)
7637 {
7638 assert (search_frag->fr_fix == 0
7639 || search_frag->fr_type == rs_align);
7640 search_frag = search_frag->fr_next;
7641 }
7642
7643 assert (search_frag->tc_frag_data.literal_frag->fr_subtype
7644 == RELAX_LITERAL_POOL_BEGIN);
7645 xtensa_switch_section_emit_state (&state, segment->seg, 0);
7646
7647 /* Make sure that all the frags in this series are closed, and
7648 that there is at least one left over of zero-size. This
7649 prevents us from making a segment with an frchain without any
7650 frags in it. */
7651 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
7652 last_frag = frag_now;
7653 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
7654
7655 while (search_frag != frag_now)
7656 {
7657 next_frag = search_frag->fr_next;
7658
7659 /* First, move the frag out of the literal section and
7660 to the appropriate place. */
7661 if (search_frag->tc_frag_data.literal_frag)
7662 {
7663 literal_pool = search_frag->tc_frag_data.literal_frag;
7664 assert (literal_pool->fr_subtype == RELAX_LITERAL_POOL_BEGIN);
7665 /* Note that we set this fr_var to be a fix
7666 chain when we created the literal pool location
7667 as RELAX_LITERAL_POOL_BEGIN. */
7668 frchain_to = (frchainS *) literal_pool->fr_var;
7669 }
7670 insert_after = literal_pool;
7671
7672 while (insert_after->fr_next->fr_subtype != RELAX_LITERAL_POOL_END)
7673 insert_after = insert_after->fr_next;
7674
7675 dest_seg = (segT) insert_after->fr_next->fr_var;
7676
7677 *frag_splice = next_frag;
7678 search_frag->fr_next = insert_after->fr_next;
7679 insert_after->fr_next = search_frag;
7680 search_frag->tc_frag_data.lit_seg = dest_seg;
7681
7682 /* Now move any fixups associated with this frag to the
7683 right section. */
7684 fix = frchain_from->fix_root;
7685 fix_splice = &(frchain_from->fix_root);
7686 while (fix)
7687 {
7688 next_fix = fix->fx_next;
7689 if (fix->fx_frag == search_frag)
7690 {
7691 *fix_splice = next_fix;
7692 fix->fx_next = frchain_to->fix_root;
7693 frchain_to->fix_root = fix;
7694 if (frchain_to->fix_tail == NULL)
7695 frchain_to->fix_tail = fix;
7696 }
7697 else
7698 fix_splice = &(fix->fx_next);
7699 fix = next_fix;
7700 }
7701 search_frag = next_frag;
7702 }
7703
7704 if (frchain_from->fix_root != NULL)
7705 {
7706 frchain_from = seg_info (segment->seg)->frchainP;
7707 as_warn (_("fixes not all moved from %s"), segment->seg->name);
7708
7709 assert (frchain_from->fix_root == NULL);
7710 }
7711 frchain_from->fix_tail = NULL;
7712 xtensa_restore_emit_state (&state);
7713 segment = segment->next;
7714 }
7715
7716 xtensa_move_frag_symbols ();
7717 }
7718
7719
7720 static void
7721 xtensa_move_frag_symbol (sym)
7722 symbolS *sym;
7723 {
7724 fragS *frag = symbol_get_frag (sym);
7725
7726 if (frag->tc_frag_data.lit_seg != (segT) 0)
7727 S_SET_SEGMENT (sym, frag->tc_frag_data.lit_seg);
7728 }
7729
7730
7731 static void
7732 xtensa_move_frag_symbols ()
7733 {
7734 symbolS *symbolP;
7735
7736 /* Although you might think that only one of these lists should be
7737 searched, it turns out that the difference of the two sets
7738 (either way) is not empty. They do overlap quite a bit,
7739 however. */
7740
7741 for (symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next)
7742 xtensa_move_frag_symbol (symbolP);
7743
7744 map_over_defined_symbols (xtensa_move_frag_symbol);
7745 }
7746
7747
7748 static void
7749 xtensa_reorder_seg_list (head, after)
7750 seg_list *head;
7751 segT after;
7752 {
7753 /* Move all of the sections in the section list to come
7754 after "after" in the gnu segment list. */
7755
7756 head = head->next;
7757 while (head)
7758 {
7759 segT literal_section = head->seg;
7760
7761 /* Move the literal section after "after". */
7762 assert (literal_section);
7763 if (literal_section != after)
7764 {
7765 xtensa_remove_section (literal_section);
7766 xtensa_insert_section (after, literal_section);
7767 }
7768
7769 head = head->next;
7770 }
7771 }
7772
7773
7774 /* Push all the literal segments to the end of the gnu list. */
7775
7776 void
7777 xtensa_reorder_segments ()
7778 {
7779 segT sec;
7780 segT last_sec;
7781 int old_count = 0;
7782 int new_count = 0;
7783
7784 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
7785 old_count++;
7786
7787 /* Now that we have the last section, push all the literal
7788 sections to the end. */
7789 last_sec = get_last_sec ();
7790 xtensa_reorder_seg_list (literal_head, last_sec);
7791 xtensa_reorder_seg_list (init_literal_head, last_sec);
7792 xtensa_reorder_seg_list (fini_literal_head, last_sec);
7793
7794 /* Now perform the final error check. */
7795 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
7796 new_count++;
7797 assert (new_count == old_count);
7798 }
7799
7800
7801 segT
7802 get_last_sec ()
7803 {
7804 segT last_sec = stdoutput->sections;
7805 while (last_sec->next != NULL)
7806 last_sec = last_sec->next;
7807
7808 return last_sec;
7809 }
7810
7811
7812 /* Change the emit state (seg, subseg, and frag related stuff) to the
7813 correct location. Return a emit_state which can be passed to
7814 xtensa_restore_emit_state to return to current fragment. */
7815
7816 void
7817 xtensa_switch_to_literal_fragment (result)
7818 emit_state *result;
7819 {
7820 /* When we mark a literal pool location, we want to put a frag in
7821 the literal pool that points to it. But to do that, we want to
7822 switch_to_literal_fragment. But literal sections don't have
7823 literal pools, so their location is always null, so we would
7824 recurse forever. This is kind of hacky, but it works. */
7825
7826 static bfd_boolean recursive = FALSE;
7827 fragS *pool_location = get_literal_pool_location (now_seg);
7828 bfd_boolean is_init =
7829 (now_seg && !strcmp (segment_name (now_seg), INIT_SECTION_NAME));
7830
7831 bfd_boolean is_fini =
7832 (now_seg && !strcmp (segment_name (now_seg), FINI_SECTION_NAME));
7833
7834
7835 if (pool_location == NULL
7836 && !use_literal_section
7837 && !recursive
7838 && !is_init && ! is_fini)
7839 {
7840 as_warn (_("inlining literal pool; "
7841 "specify location with .literal_position."));
7842 recursive = TRUE;
7843 xtensa_mark_literal_pool_location (FALSE);
7844 recursive = FALSE;
7845 }
7846
7847 /* Special case: If we are in the ".fini" or ".init" section, then
7848 we will ALWAYS be generating to the ".fini.literal" and
7849 ".init.literal" sections. */
7850
7851 if (is_init)
7852 {
7853 cache_literal_section (init_literal_head,
7854 default_lit_sections.init_lit_seg_name,
7855 &default_lit_sections.init_lit_seg);
7856 xtensa_switch_section_emit_state (result,
7857 default_lit_sections.init_lit_seg, 0);
7858 }
7859 else if (is_fini)
7860 {
7861 cache_literal_section (fini_literal_head,
7862 default_lit_sections.fini_lit_seg_name,
7863 &default_lit_sections.fini_lit_seg);
7864 xtensa_switch_section_emit_state (result,
7865 default_lit_sections.fini_lit_seg, 0);
7866 }
7867 else
7868 {
7869 cache_literal_section (literal_head,
7870 default_lit_sections.lit_seg_name,
7871 &default_lit_sections.lit_seg);
7872 xtensa_switch_section_emit_state (result,
7873 default_lit_sections.lit_seg, 0);
7874 }
7875
7876 if (!use_literal_section &&
7877 !is_init && !is_fini &&
7878 get_literal_pool_location (now_seg) != pool_location)
7879 {
7880 /* Close whatever frag is there. */
7881 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
7882 frag_now->tc_frag_data.literal_frag = pool_location;
7883 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
7884 }
7885
7886 /* Do a 4 byte align here. */
7887 frag_align (2, 0, 0);
7888 }
7889
7890
7891 /* Call this function before emitting data into the literal section.
7892 This is a helper function for xtensa_switch_to_literal_fragment.
7893 This is similar to a .section new_now_seg subseg. */
7894
7895 void
7896 xtensa_switch_section_emit_state (state, new_now_seg, new_now_subseg)
7897 emit_state *state;
7898 segT new_now_seg;
7899 subsegT new_now_subseg;
7900 {
7901 state->name = now_seg->name;
7902 state->now_seg = now_seg;
7903 state->now_subseg = now_subseg;
7904 state->generating_literals = generating_literals;
7905 generating_literals++;
7906 subseg_new (segment_name (new_now_seg), new_now_subseg);
7907 }
7908
7909
7910 /* Use to restore the emitting into the normal place. */
7911
7912 void
7913 xtensa_restore_emit_state (state)
7914 emit_state *state;
7915 {
7916 generating_literals = state->generating_literals;
7917 subseg_new (state->name, state->now_subseg);
7918 }
7919
7920
7921 /* Get a segment of a given name. If the segment is already
7922 present, return it; otherwise, create a new one. */
7923
7924 static void
7925 cache_literal_section (head, name, seg)
7926 seg_list *head;
7927 const char *name;
7928 segT *seg;
7929 {
7930 segT current_section = now_seg;
7931 int current_subsec = now_subseg;
7932
7933 if (*seg != 0)
7934 return;
7935 *seg = retrieve_literal_seg (head, name);
7936 subseg_set (current_section, current_subsec);
7937 }
7938
7939
7940 /* Get a segment of a given name. If the segment is already
7941 present, return it; otherwise, create a new one. */
7942
7943 static segT
7944 retrieve_literal_seg (head, name)
7945 seg_list *head;
7946 const char *name;
7947 {
7948 segT ret = 0;
7949
7950 assert (head);
7951
7952 ret = seg_present (name);
7953 if (!ret)
7954 {
7955 ret = subseg_new (name, (subsegT) 0);
7956 add_seg_list (head, ret);
7957 bfd_set_section_flags (stdoutput, ret, SEC_HAS_CONTENTS |
7958 SEC_READONLY | SEC_ALLOC | SEC_LOAD | SEC_CODE);
7959 bfd_set_section_alignment (stdoutput, ret, 2);
7960 }
7961
7962 return ret;
7963 }
7964
7965
7966 /* Return a segment of a given name if it is present. */
7967
7968 static segT
7969 seg_present (name)
7970 const char *name;
7971 {
7972 segT seg;
7973 seg = stdoutput->sections;
7974
7975 while (seg)
7976 {
7977 if (!strcmp (segment_name (seg), name))
7978 return seg;
7979 seg = seg->next;
7980 }
7981
7982 return 0;
7983 }
7984
7985
7986 /* Add a segment to a segment list. */
7987
7988 static void
7989 add_seg_list (head, seg)
7990 seg_list *head;
7991 segT seg;
7992 {
7993 seg_list *n;
7994 n = (seg_list *) xmalloc (sizeof (seg_list));
7995 assert (n);
7996
7997 n->seg = seg;
7998 n->next = head->next;
7999 head->next = n;
8000 }
8001
8002 \f
8003 /* Set up Property Tables after Relaxation. */
8004
8005 #define XTENSA_INSN_SEC_NAME ".xt.insn"
8006 #define XTENSA_LIT_SEC_NAME ".xt.lit"
8007
8008 void
8009 xtensa_post_relax_hook ()
8010 {
8011 xtensa_move_seg_list_to_beginning (literal_head);
8012 xtensa_move_seg_list_to_beginning (init_literal_head);
8013 xtensa_move_seg_list_to_beginning (fini_literal_head);
8014
8015 xtensa_create_property_segments (get_frag_is_insn,
8016 XTENSA_INSN_SEC_NAME,
8017 xt_literal_sec);
8018 if (use_literal_section)
8019 xtensa_create_property_segments (get_frag_is_literal,
8020 XTENSA_LIT_SEC_NAME,
8021 xt_insn_sec);
8022 }
8023
8024
8025 static bfd_boolean
8026 get_frag_is_literal (fragP)
8027 const fragS *fragP;
8028 {
8029 assert (fragP != NULL);
8030 return (fragP->tc_frag_data.is_literal);
8031 }
8032
8033
8034 static bfd_boolean
8035 get_frag_is_insn (fragP)
8036 const fragS *fragP;
8037 {
8038 assert (fragP != NULL);
8039 return (fragP->tc_frag_data.is_insn);
8040 }
8041
8042
8043 static void
8044 xtensa_create_property_segments (property_function, section_name_base,
8045 sec_type)
8046 frag_predicate property_function;
8047 const char * section_name_base;
8048 xt_section_type sec_type;
8049 {
8050 segT *seclist;
8051
8052 /* Walk over all of the current segments.
8053 Walk over each fragment
8054 For each fragment that has instructions
8055 Build an instruction record (append where possible). */
8056
8057 for (seclist = &stdoutput->sections;
8058 seclist && *seclist;
8059 seclist = &(*seclist)->next)
8060 {
8061 segT sec = *seclist;
8062 if (section_has_property (sec, property_function))
8063 {
8064 char * property_section_name =
8065 xtensa_get_property_section_name (stdoutput, sec,
8066 section_name_base);
8067 segT insn_sec = retrieve_xtensa_section (property_section_name);
8068 segment_info_type *xt_seg_info = retrieve_segment_info (insn_sec);
8069 xtensa_block_info ** xt_blocks =
8070 &xt_seg_info->tc_segment_info_data.blocks[sec_type];
8071 /* Walk over all of the frchains here and add new sections. */
8072 add_xt_block_frags (sec, insn_sec, xt_blocks, property_function);
8073 }
8074 }
8075
8076 /* Now we fill them out.... */
8077
8078 for (seclist = &stdoutput->sections;
8079 seclist && *seclist;
8080 seclist = &(*seclist)->next)
8081 {
8082 segment_info_type *seginfo;
8083 xtensa_block_info *block;
8084 segT sec = *seclist;
8085 seginfo = seg_info (sec);
8086 block = seginfo->tc_segment_info_data.blocks[sec_type];
8087
8088 if (block)
8089 {
8090 xtensa_block_info *cur_block;
8091 /* This is a section with some data. */
8092 size_t num_recs = 0;
8093 size_t rec_size;
8094
8095 for (cur_block = block; cur_block; cur_block = cur_block->next)
8096 num_recs++;
8097
8098 rec_size = num_recs * 8;
8099 bfd_set_section_size (stdoutput, sec, rec_size);
8100
8101 /* In order to make this work with the assembler, we have to
8102 build some frags and then build the "fixups" for it. It
8103 would be easier to just set the contents then set the
8104 arlents. */
8105
8106 if (num_recs)
8107 {
8108 /* Allocate a fragment and leak it. */
8109 fragS *fragP;
8110 size_t frag_size;
8111 fixS *fixes;
8112 frchainS *frchainP;
8113 size_t i;
8114 char *frag_data;
8115
8116 frag_size = sizeof (fragS) + rec_size;
8117 fragP = (fragS *) xmalloc (frag_size);
8118
8119 memset (fragP, 0, frag_size);
8120 fragP->fr_address = 0;
8121 fragP->fr_next = NULL;
8122 fragP->fr_fix = rec_size;
8123 fragP->fr_var = 0;
8124 fragP->fr_type = rs_fill;
8125 /* the rest are zeros */
8126
8127 frchainP = seginfo->frchainP;
8128 frchainP->frch_root = fragP;
8129 frchainP->frch_last = fragP;
8130
8131 fixes = (fixS *) xmalloc (sizeof (fixS) * num_recs);
8132 memset (fixes, 0, sizeof (fixS) * num_recs);
8133
8134 seginfo->fix_root = fixes;
8135 seginfo->fix_tail = &fixes[num_recs - 1];
8136 cur_block = block;
8137 frag_data = &fragP->fr_literal[0];
8138 for (i = 0; i < num_recs; i++)
8139 {
8140 fixS *fix = &fixes[i];
8141 assert (cur_block);
8142
8143 /* Write the fixup. */
8144 if (i != num_recs - 1)
8145 fix->fx_next = &fixes[i + 1];
8146 else
8147 fix->fx_next = NULL;
8148 fix->fx_size = 4;
8149 fix->fx_done = 0;
8150 fix->fx_frag = fragP;
8151 fix->fx_where = i * 8;
8152 fix->fx_addsy = section_symbol (cur_block->sec);
8153 fix->fx_offset = cur_block->offset;
8154 fix->fx_r_type = BFD_RELOC_32;
8155 fix->fx_file = "Internal Assembly";
8156 fix->fx_line = 0;
8157
8158 /* Write the length. */
8159 md_number_to_chars (&frag_data[4 + 8 * i],
8160 cur_block->size, 4);
8161 cur_block = cur_block->next;
8162 }
8163 }
8164 }
8165 }
8166 }
8167
8168
8169 segment_info_type *
8170 retrieve_segment_info (seg)
8171 segT seg;
8172 {
8173 segment_info_type *seginfo;
8174 seginfo = (segment_info_type *) bfd_get_section_userdata (stdoutput, seg);
8175 if (!seginfo)
8176 {
8177 frchainS *frchainP;
8178
8179 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
8180 memset ((PTR) seginfo, 0, sizeof (*seginfo));
8181 seginfo->fix_root = NULL;
8182 seginfo->fix_tail = NULL;
8183 seginfo->bfd_section = seg;
8184 seginfo->sym = 0;
8185 /* We will not be dealing with these, only our special ones. */
8186 #if 0
8187 if (seg == bfd_abs_section_ptr)
8188 abs_seg_info = seginfo;
8189 else if (seg == bfd_und_section_ptr)
8190 und_seg_info = seginfo;
8191 else
8192 #endif
8193 bfd_set_section_userdata (stdoutput, seg, (PTR) seginfo);
8194 #if 0
8195 seg_fix_rootP = &segment_info[seg].fix_root;
8196 seg_fix_tailP = &segment_info[seg].fix_tail;
8197 #endif
8198
8199 frchainP = (frchainS *) xmalloc (sizeof (frchainS));
8200 frchainP->frch_root = NULL;
8201 frchainP->frch_last = NULL;
8202 frchainP->frch_next = NULL;
8203 frchainP->frch_seg = seg;
8204 frchainP->frch_subseg = 0;
8205 frchainP->fix_root = NULL;
8206 frchainP->fix_tail = NULL;
8207 /* Do not init the objstack. */
8208 /* obstack_begin (&frchainP->frch_obstack, chunksize); */
8209 /* frchainP->frch_frag_now = fragP; */
8210 frchainP->frch_frag_now = NULL;
8211
8212 seginfo->frchainP = frchainP;
8213 }
8214
8215 return seginfo;
8216 }
8217
8218
8219 segT
8220 retrieve_xtensa_section (sec_name)
8221 char *sec_name;
8222 {
8223 bfd *abfd = stdoutput;
8224 flagword flags, out_flags, link_once_flags;
8225 segT s;
8226
8227 flags = bfd_get_section_flags (abfd, now_seg);
8228 link_once_flags = (flags & SEC_LINK_ONCE);
8229 if (link_once_flags)
8230 link_once_flags |= (flags & SEC_LINK_DUPLICATES);
8231 out_flags = (SEC_RELOC | SEC_HAS_CONTENTS | SEC_READONLY | link_once_flags);
8232
8233 s = bfd_make_section_old_way (abfd, sec_name);
8234 if (s == NULL)
8235 as_bad (_("could not create section %s"), sec_name);
8236 if (!bfd_set_section_flags (abfd, s, out_flags))
8237 as_bad (_("invalid flag combination on section %s"), sec_name);
8238
8239 return s;
8240 }
8241
8242
8243 bfd_boolean
8244 section_has_property (sec, property_function)
8245 segT sec;
8246 frag_predicate property_function;
8247 {
8248 segment_info_type *seginfo = seg_info (sec);
8249 fragS *fragP;
8250
8251 if (seginfo && seginfo->frchainP)
8252 {
8253 for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
8254 {
8255 if (property_function (fragP)
8256 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
8257 return TRUE;
8258 }
8259 }
8260 return FALSE;
8261 }
8262
8263
8264 /* Two types of block sections exist right now: literal and insns. */
8265
8266 void
8267 add_xt_block_frags (sec, xt_block_sec, xt_block, property_function)
8268 segT sec;
8269 segT xt_block_sec;
8270 xtensa_block_info **xt_block;
8271 frag_predicate property_function;
8272 {
8273 segment_info_type *seg_info;
8274 segment_info_type *xt_seg_info;
8275 bfd_vma seg_offset;
8276 fragS *fragP;
8277
8278 xt_seg_info = retrieve_segment_info (xt_block_sec);
8279 seg_info = retrieve_segment_info (sec);
8280
8281 /* Build it if needed. */
8282 while (*xt_block != NULL)
8283 xt_block = &(*xt_block)->next;
8284 /* We are either at NULL at the beginning or at the end. */
8285
8286 /* Walk through the frags. */
8287 seg_offset = 0;
8288
8289 if (seg_info->frchainP)
8290 {
8291 for (fragP = seg_info->frchainP->frch_root;
8292 fragP;
8293 fragP = fragP->fr_next)
8294 {
8295 if (property_function (fragP)
8296 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
8297 {
8298 if (*xt_block != NULL)
8299 {
8300 if ((*xt_block)->offset + (*xt_block)->size
8301 == fragP->fr_address)
8302 (*xt_block)->size += fragP->fr_fix;
8303 else
8304 xt_block = &((*xt_block)->next);
8305 }
8306 if (*xt_block == NULL)
8307 {
8308 xtensa_block_info *new_block = (xtensa_block_info *)
8309 xmalloc (sizeof (xtensa_block_info));
8310 new_block->sec = sec;
8311 new_block->offset = fragP->fr_address;
8312 new_block->size = fragP->fr_fix;
8313 new_block->next = NULL;
8314 *xt_block = new_block;
8315 }
8316 }
8317 }
8318 }
8319 }
8320
8321 \f
8322 /* Instruction Stack Functions (from "xtensa-istack.h"). */
8323
8324 void
8325 istack_init (stack)
8326 IStack *stack;
8327 {
8328 memset (stack, 0, sizeof (IStack));
8329 stack->ninsn = 0;
8330 }
8331
8332
8333 bfd_boolean
8334 istack_empty (stack)
8335 IStack *stack;
8336 {
8337 return (stack->ninsn == 0);
8338 }
8339
8340
8341 bfd_boolean
8342 istack_full (stack)
8343 IStack *stack;
8344 {
8345 return (stack->ninsn == MAX_ISTACK);
8346 }
8347
8348
8349 /* Return a pointer to the top IStack entry.
8350 It is an error to call this if istack_empty () is true. */
8351
8352 TInsn *
8353 istack_top (stack)
8354 IStack *stack;
8355 {
8356 int rec = stack->ninsn - 1;
8357 assert (!istack_empty (stack));
8358 return &stack->insn[rec];
8359 }
8360
8361
8362 /* Add a new TInsn to an IStack.
8363 It is an error to call this if istack_full () is true. */
8364
8365 void
8366 istack_push (stack, insn)
8367 IStack *stack;
8368 TInsn *insn;
8369 {
8370 int rec = stack->ninsn;
8371 assert (!istack_full (stack));
8372 tinsn_copy (&stack->insn[rec], insn);
8373 stack->ninsn++;
8374 }
8375
8376
8377 /* Clear space for the next TInsn on the IStack and return a pointer
8378 to it. It is an error to call this if istack_full () is true. */
8379
8380 TInsn *
8381 istack_push_space (stack)
8382 IStack *stack;
8383 {
8384 int rec = stack->ninsn;
8385 TInsn *insn;
8386 assert (!istack_full (stack));
8387 insn = &stack->insn[rec];
8388 memset (insn, 0, sizeof (TInsn));
8389 stack->ninsn++;
8390 return insn;
8391 }
8392
8393
8394 /* Remove the last pushed instruction. It is an error to call this if
8395 istack_empty () returns true. */
8396
8397 void
8398 istack_pop (stack)
8399 IStack *stack;
8400 {
8401 int rec = stack->ninsn - 1;
8402 assert (!istack_empty (stack));
8403 stack->ninsn--;
8404 memset (&stack->insn[rec], 0, sizeof (TInsn));
8405 }
8406
8407 \f
8408 /* TInsn functions. */
8409
8410 void
8411 tinsn_init (dst)
8412 TInsn *dst;
8413 {
8414 memset (dst, 0, sizeof (TInsn));
8415 }
8416
8417
8418 void
8419 tinsn_copy (dst, src)
8420 TInsn *dst;
8421 const TInsn *src;
8422 {
8423 tinsn_init (dst);
8424 memcpy (dst, src, sizeof (TInsn));
8425 }
8426
8427
8428 /* Get the ``num''th token of the TInsn.
8429 It is illegal to call this if num > insn->ntoks. */
8430
8431 expressionS *
8432 tinsn_get_tok (insn, num)
8433 TInsn *insn;
8434 int num;
8435 {
8436 assert (num < insn->ntok);
8437 return &insn->tok[num];
8438 }
8439
8440
8441 /* Return true if ANY of the operands in the insn are symbolic. */
8442
8443 static bfd_boolean
8444 tinsn_has_symbolic_operands (insn)
8445 const TInsn *insn;
8446 {
8447 int i;
8448 int n = insn->ntok;
8449
8450 assert (insn->insn_type == ITYPE_INSN);
8451
8452 for (i = 0; i < n; ++i)
8453 {
8454 switch (insn->tok[i].X_op)
8455 {
8456 case O_register:
8457 case O_constant:
8458 break;
8459 default:
8460 return TRUE;
8461 }
8462 }
8463 return FALSE;
8464 }
8465
8466
8467 bfd_boolean
8468 tinsn_has_invalid_symbolic_operands (insn)
8469 const TInsn *insn;
8470 {
8471 int i;
8472 int n = insn->ntok;
8473
8474 assert (insn->insn_type == ITYPE_INSN);
8475
8476 for (i = 0; i < n; ++i)
8477 {
8478 switch (insn->tok[i].X_op)
8479 {
8480 case O_register:
8481 case O_constant:
8482 break;
8483 default:
8484 if (i == get_relaxable_immed (insn->opcode))
8485 break;
8486 as_bad (_("invalid symbolic operand %d on '%s'"),
8487 i, xtensa_opcode_name (xtensa_default_isa, insn->opcode));
8488 return TRUE;
8489 }
8490 }
8491 return FALSE;
8492 }
8493
8494
8495 /* For assembly code with complex expressions (e.g. subtraction),
8496 we have to build them in the literal pool so that
8497 their results are calculated correctly after relaxation.
8498 The relaxation only handles expressions that
8499 boil down to SYMBOL + OFFSET. */
8500
8501 static bfd_boolean
8502 tinsn_has_complex_operands (insn)
8503 const TInsn *insn;
8504 {
8505 int i;
8506 int n = insn->ntok;
8507 assert (insn->insn_type == ITYPE_INSN);
8508 for (i = 0; i < n; ++i)
8509 {
8510 switch (insn->tok[i].X_op)
8511 {
8512 case O_register:
8513 case O_constant:
8514 case O_symbol:
8515 break;
8516 default:
8517 return TRUE;
8518 }
8519 }
8520 return FALSE;
8521 }
8522
8523
8524 /* Convert the constant operands in the t_insn to insnbuf.
8525 Return true if there is a symbol in the immediate field.
8526
8527 Before this is called,
8528 1) the number of operands are correct
8529 2) the t_insn is a ITYPE_INSN
8530 3) ONLY the relaxable_ is built
8531 4) All operands are O_constant, O_symbol. All constants fit
8532 The return value tells whether there are any remaining O_symbols. */
8533
8534 static bfd_boolean
8535 tinsn_to_insnbuf (t_insn, insnbuf)
8536 TInsn *t_insn;
8537 xtensa_insnbuf insnbuf;
8538 {
8539 xtensa_isa isa = xtensa_default_isa;
8540 xtensa_opcode opcode = t_insn->opcode;
8541 bfd_boolean has_fixup = FALSE;
8542 int noperands = xtensa_num_operands (isa, opcode);
8543 int i;
8544 uint32 opnd_value;
8545 char *file_name;
8546 int line;
8547
8548 assert (t_insn->insn_type == ITYPE_INSN);
8549 if (noperands != t_insn->ntok)
8550 as_fatal (_("operand number mismatch"));
8551
8552 xtensa_encode_insn (isa, opcode, insnbuf);
8553
8554 for (i = 0; i < noperands; ++i)
8555 {
8556 expressionS *expr = &t_insn->tok[i];
8557 xtensa_operand operand = xtensa_get_operand (isa, opcode, i);
8558 switch (expr->X_op)
8559 {
8560 case O_register:
8561 /* The register number has already been checked in
8562 expression_maybe_register, so we don't need to check here. */
8563 opnd_value = expr->X_add_number;
8564 (void) xtensa_operand_encode (operand, &opnd_value);
8565 xtensa_operand_set_field (operand, insnbuf, opnd_value);
8566 break;
8567
8568 case O_constant:
8569 as_where (&file_name, &line);
8570 /* It is a constant and we called this function,
8571 then we have to try to fit it. */
8572 xtensa_insnbuf_set_operand (insnbuf, opcode, operand,
8573 expr->X_add_number, file_name, line);
8574 break;
8575
8576 case O_symbol:
8577 default:
8578 has_fixup = TRUE;
8579 break;
8580 }
8581 }
8582 return has_fixup;
8583 }
8584
8585
8586 /* Check the instruction arguments. Return true on failure. */
8587
8588 bfd_boolean
8589 tinsn_check_arguments (insn)
8590 const TInsn *insn;
8591 {
8592 xtensa_isa isa = xtensa_default_isa;
8593 xtensa_opcode opcode = insn->opcode;
8594
8595 if (opcode == XTENSA_UNDEFINED)
8596 {
8597 as_bad (_("invalid opcode"));
8598 return TRUE;
8599 }
8600
8601 if (xtensa_num_operands (isa, opcode) > insn->ntok)
8602 {
8603 as_bad (_("too few operands"));
8604 return TRUE;
8605 }
8606
8607 if (xtensa_num_operands (isa, opcode) < insn->ntok)
8608 {
8609 as_bad (_("too many operands"));
8610 return TRUE;
8611 }
8612 return FALSE;
8613 }
8614
8615
8616 /* Load an instruction from its encoded form. */
8617
8618 static void
8619 tinsn_from_chars (t_insn, f)
8620 TInsn *t_insn;
8621 char *f;
8622 {
8623 static xtensa_insnbuf insnbuf = NULL;
8624 int i;
8625 xtensa_opcode opcode;
8626 xtensa_isa isa = xtensa_default_isa;
8627
8628 if (!insnbuf)
8629 insnbuf = xtensa_insnbuf_alloc (isa);
8630
8631 xtensa_insnbuf_from_chars (isa, insnbuf, f);
8632 opcode = xtensa_decode_insn (isa, insnbuf);
8633
8634 /* Find the immed. */
8635 tinsn_init (t_insn);
8636 t_insn->insn_type = ITYPE_INSN;
8637 t_insn->is_specific_opcode = FALSE; /* Must not be specific. */
8638 t_insn->opcode = opcode;
8639 t_insn->ntok = xtensa_num_operands (isa, opcode);
8640 for (i = 0; i < t_insn->ntok; i++)
8641 {
8642 set_expr_const (&t_insn->tok[i],
8643 xtensa_insnbuf_get_operand (insnbuf, opcode, i));
8644 }
8645 }
8646
8647
8648 /* Read the value of the relaxable immed from the fr_symbol and fr_offset. */
8649
8650 static void
8651 tinsn_immed_from_frag (t_insn, fragP)
8652 TInsn *t_insn;
8653 fragS *fragP;
8654 {
8655 xtensa_opcode opcode = t_insn->opcode;
8656 int opnum;
8657
8658 if (fragP->fr_symbol)
8659 {
8660 opnum = get_relaxable_immed (opcode);
8661 set_expr_symbol_offset (&t_insn->tok[opnum],
8662 fragP->fr_symbol, fragP->fr_offset);
8663 }
8664 }
8665
8666
8667 static int
8668 get_num_stack_text_bytes (istack)
8669 IStack *istack;
8670 {
8671 int i;
8672 int text_bytes = 0;
8673
8674 for (i = 0; i < istack->ninsn; i++)
8675 {
8676 TInsn *t_insn = &istack->insn[i];
8677 if (t_insn->insn_type == ITYPE_INSN)
8678 text_bytes += xg_get_insn_size (t_insn);
8679 }
8680 return text_bytes;
8681 }
8682
8683
8684 static int
8685 get_num_stack_literal_bytes (istack)
8686 IStack *istack;
8687 {
8688 int i;
8689 int lit_bytes = 0;
8690
8691 for (i = 0; i < istack->ninsn; i++)
8692 {
8693 TInsn *t_insn = &istack->insn[i];
8694
8695 if (t_insn->insn_type == ITYPE_LITERAL && t_insn->ntok == 1)
8696 lit_bytes += 4;
8697 }
8698 return lit_bytes;
8699 }
8700
8701 \f
8702 /* Expression utilities. */
8703
8704 /* Return true if the expression is an integer constant. */
8705
8706 bfd_boolean
8707 expr_is_const (s)
8708 const expressionS *s;
8709 {
8710 return (s->X_op == O_constant);
8711 }
8712
8713
8714 /* Get the expression constant.
8715 Calling this is illegal if expr_is_const () returns true. */
8716
8717 offsetT
8718 get_expr_const (s)
8719 const expressionS *s;
8720 {
8721 assert (expr_is_const (s));
8722 return s->X_add_number;
8723 }
8724
8725
8726 /* Set the expression to a constant value. */
8727
8728 void
8729 set_expr_const (s, val)
8730 expressionS *s;
8731 offsetT val;
8732 {
8733 s->X_op = O_constant;
8734 s->X_add_number = val;
8735 s->X_add_symbol = NULL;
8736 s->X_op_symbol = NULL;
8737 }
8738
8739
8740 /* Set the expression to a symbol + constant offset. */
8741
8742 void
8743 set_expr_symbol_offset (s, sym, offset)
8744 expressionS *s;
8745 symbolS *sym;
8746 offsetT offset;
8747 {
8748 s->X_op = O_symbol;
8749 s->X_add_symbol = sym;
8750 s->X_op_symbol = NULL; /* unused */
8751 s->X_add_number = offset;
8752 }
8753
8754
8755 bfd_boolean
8756 expr_is_equal (s1, s2)
8757 expressionS *s1;
8758 expressionS *s2;
8759 {
8760 if (s1->X_op != s2->X_op)
8761 return FALSE;
8762 if (s1->X_add_symbol != s2->X_add_symbol)
8763 return FALSE;
8764 if (s1->X_op_symbol != s2->X_op_symbol)
8765 return FALSE;
8766 if (s1->X_add_number != s2->X_add_number)
8767 return FALSE;
8768 return TRUE;
8769 }
8770
8771
8772 static void
8773 copy_expr (dst, src)
8774 expressionS *dst;
8775 const expressionS *src;
8776 {
8777 memcpy (dst, src, sizeof (expressionS));
8778 }
8779
8780 \f
8781 /* Support for Tensilica's "--rename-section" option. */
8782
8783 #ifdef XTENSA_SECTION_RENAME
8784
8785 struct rename_section_struct
8786 {
8787 char *old_name;
8788 char *new_name;
8789 struct rename_section_struct *next;
8790 };
8791
8792 static struct rename_section_struct *section_rename;
8793
8794
8795 /* Parse the string oldname=new_name:oldname2=new_name2
8796 and call add_section_rename. */
8797
8798 void
8799 build_section_rename (arg)
8800 const char *arg;
8801 {
8802 char *this_arg = NULL;
8803 char *next_arg = NULL;
8804
8805 for (this_arg = strdup (arg); this_arg != NULL; this_arg = next_arg)
8806 {
8807 if (this_arg)
8808 {
8809 next_arg = strchr (this_arg, ':');
8810 if (next_arg)
8811 {
8812 *next_arg = '\0';
8813 next_arg++;
8814 }
8815 }
8816 {
8817 char *old_name = this_arg;
8818 char *new_name = strchr (this_arg, '=');
8819
8820 if (*old_name == '\0')
8821 {
8822 as_warn (_("ignoring extra '-rename-section' delimiter ':'"));
8823 continue;
8824 }
8825 if (!new_name || new_name[1] == '\0')
8826 {
8827 as_warn (_("ignoring invalid '-rename-section' "
8828 "specification: '%s'"), old_name);
8829 continue;
8830 }
8831 *new_name = '\0';
8832 new_name++;
8833 add_section_rename (old_name, new_name);
8834 }
8835 }
8836 }
8837
8838
8839 static void
8840 add_section_rename (old_name, new_name)
8841 char *old_name;
8842 char *new_name;
8843 {
8844 struct rename_section_struct *r = section_rename;
8845
8846 /* Check for invalid section renaming. */
8847 for (r = section_rename; r != NULL; r = r->next)
8848 {
8849 if (strcmp (r->old_name, old_name) == 0)
8850 as_bad (_("section %s renamed multiple times"), old_name);
8851 if (strcmp (r->new_name, new_name) == 0)
8852 as_bad (_("multiple sections remapped to output section %s"),
8853 new_name);
8854 }
8855
8856 /* Now add it. */
8857 r = (struct rename_section_struct *)
8858 xmalloc (sizeof (struct rename_section_struct));
8859 r->old_name = strdup (old_name);
8860 r->new_name = strdup (new_name);
8861 r->next = section_rename;
8862 section_rename = r;
8863 }
8864
8865
8866 const char *
8867 xtensa_section_rename (name)
8868 const char *name;
8869 {
8870 struct rename_section_struct *r = section_rename;
8871
8872 for (r = section_rename; r != NULL; r = r->next)
8873 if (strcmp (r->old_name, name) == 0)
8874 return r->new_name;
8875
8876 return name;
8877 }
8878
8879 #endif /* XTENSA_SECTION_RENAME */
8880
8881 \f
8882 /* Combining identical literals. */
8883
8884 #ifdef XTENSA_COMBINE_LITERALS
8885
8886 /* This code records all the .literal values that are ever seen and
8887 detects duplicates so that identical values can be combined. This
8888 is currently disabled because it's only half-baked. */
8889
8890 #define XTENSA_LIT_PLUS_OFFSET ".xtensa_litsym_offset_"
8891
8892 /* TODO: make this into a more efficient data structure. */
8893 typedef struct literal_list_elem
8894 {
8895 symbolS *sym; /* The symbol that points to this literal. */
8896 expressionS expr; /* The expression. */
8897 segT seg;
8898 struct literal_list_elem *next; /* Next in the list. */
8899 } literal_list_elem;
8900
8901 literal_list_elem *lit_cache = NULL;
8902
8903 typedef struct lit_sym_translation
8904 {
8905 char *name; /* This name. */
8906 offsetT offset; /* Plus this offset. */
8907 symbolS *sym; /* Should really mean this symbol. */
8908 struct lit_sym_translation *next;
8909 } lit_sym_translation;
8910
8911 lit_sym_translation *translations = NULL;
8912
8913 static bfd_boolean is_duplicate_expression
8914 PARAMS ((expressionS *, expressionS *));
8915 static void cache_literal
8916 PARAMS ((char *sym_name, expressionS *, segT));
8917 static symbolS *is_duplicate_literal
8918 PARAMS ((expressionS *, segT));
8919
8920
8921 static bfd_boolean
8922 is_duplicate_expression (e1, e2)
8923 expressionS *e1;
8924 expressionS *e2;
8925 {
8926 if (e1->X_op != e2->X_op)
8927 return FALSE;
8928 if (e1->X_add_symbol != e2->X_add_symbol)
8929 return FALSE;
8930 if (e1->X_op_symbol != e2->X_op_symbol)
8931 return FALSE;
8932 if (e1->X_add_number != e2->X_add_number)
8933 return FALSE;
8934 if (e1->X_unsigned != e2->X_unsigned)
8935 return FALSE;
8936 if (e1->X_md != e2->X_md)
8937 return FALSE;
8938 return TRUE;
8939 }
8940
8941
8942 static void
8943 cache_literal (sym_name, expP, seg)
8944 char *sym_name;
8945 expressionS *expP;
8946 segT seg;
8947 {
8948 literal_list_elem *lit = xmalloc (sizeof (literal_list_elem));
8949
8950 lit->sym = symbol_find (sym_name);
8951 lit->expr = *expP;
8952 lit->seg = seg;
8953 lit->next = lit_cache;
8954 lit_cache = lit;
8955 }
8956
8957
8958 static symbolS *
8959 is_duplicate_literal (expr, seg)
8960 expressionS *expr;
8961 segT seg;
8962 {
8963 literal_list_elem *lit = lit_cache;
8964
8965 while (lit != NULL)
8966 {
8967 if (is_duplicate_expression (&lit->expr, expr) && seg == lit->seg)
8968 return lit->sym;
8969 lit = lit->next;
8970 }
8971
8972 return NULL;
8973 }
8974
8975
8976 static void
8977 add_lit_sym_translation (name, offset, target)
8978 char * name;
8979 offsetT offset;
8980 symbolS * target;
8981 {
8982 lit_sym_translation *lit_trans = xmalloc (sizeof (lit_sym_translation));
8983
8984 lit_trans->name = name;
8985 lit_trans->offset = offset;
8986 lit_trans->sym = target;
8987 lit_trans->next = translations;
8988 translations = lit_trans;
8989 }
8990
8991
8992 static void
8993 find_lit_sym_translation (expr)
8994 expressionS *expr;
8995 {
8996 lit_sym_translation *lit_trans = translations;
8997
8998 if (expr->X_op != O_symbol)
8999 return;
9000
9001 while (lit_trans != NULL)
9002 {
9003 if (lit_trans->offset == expr->X_add_number
9004 && strcmp (lit_trans->name, S_GET_NAME (expr->X_add_symbol)) == 0)
9005 {
9006 expr->X_add_symbol = lit_trans->sym;
9007 expr->X_add_number = 0;
9008 return;
9009 }
9010 lit_trans = lit_trans->next;
9011 }
9012 }
9013
9014 #endif /* XTENSA_COMBINE_LITERALS */
This page took 0.331392 seconds and 4 git commands to generate.