1 /* Altera Nios II assembler.
2 Copyright (C) 2012-2016 Free Software Foundation, Inc.
3 Contributed by Nigel Gray (ngray@altera.com).
4 Contributed by Mentor Graphics, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
24 #include "opcode/nios2.h"
25 #include "elf/nios2.h"
29 #include "dwarf2dbg.h"
31 #include "safe-ctype.h"
32 #include "dw2gencfi.h"
35 /* We are not supporting any other target so we throw a compile time error. */
39 /* We can choose our endianness at run-time, regardless of configuration. */
40 extern int target_big_endian
;
42 /* This array holds the chars that always start a comment. If the
43 pre-processor is disabled, these aren't very useful. */
44 const char comment_chars
[] = "#";
46 /* This array holds the chars that only start a comment at the beginning of
47 a line. If the line seems to have the form '# 123 filename'
48 .line and .file directives will appear in the pre-processed output. */
49 /* Note that input_file.c hand checks for '#' at the beginning of the
50 first line of the input file. This is because the compiler outputs
51 #NO_APP at the beginning of its output. */
52 /* Also note that C style comments are always supported. */
53 const char line_comment_chars
[] = "#";
55 /* This array holds machine specific line separator characters. */
56 const char line_separator_chars
[] = ";";
58 /* Chars that can be used to separate mant from exp in floating point nums. */
59 const char EXP_CHARS
[] = "eE";
61 /* Chars that mean this number is a floating point constant. */
64 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
66 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
67 changed in read.c. Ideally it shouldn't have to know about it at all,
68 but nothing is ideal around here. */
70 /* Machine-dependent command-line options. */
72 const char *md_shortopts
= "r";
74 struct option md_longopts
[] = {
75 #define OPTION_RELAX_ALL (OPTION_MD_BASE + 0)
76 {"relax-all", no_argument
, NULL
, OPTION_RELAX_ALL
},
77 #define OPTION_NORELAX (OPTION_MD_BASE + 1)
78 {"no-relax", no_argument
, NULL
, OPTION_NORELAX
},
79 #define OPTION_RELAX_SECTION (OPTION_MD_BASE + 2)
80 {"relax-section", no_argument
, NULL
, OPTION_RELAX_SECTION
},
81 #define OPTION_EB (OPTION_MD_BASE + 3)
82 {"EB", no_argument
, NULL
, OPTION_EB
},
83 #define OPTION_EL (OPTION_MD_BASE + 4)
84 {"EL", no_argument
, NULL
, OPTION_EL
},
85 #define OPTION_MARCH (OPTION_MD_BASE + 5)
86 {"march", required_argument
, NULL
, OPTION_MARCH
}
89 size_t md_longopts_size
= sizeof (md_longopts
);
91 /* The assembler supports three different relaxation modes, controlled by
92 command-line options. */
100 /* Struct contains all assembler options set with .set. */
103 /* .set noat -> noat = 1 allows assembly code to use at without warning
104 and macro expansions generate a warning.
105 .set at -> noat = 0, assembly code using at warn but macro expansions
106 do not generate warnings. */
109 /* .set nobreak -> nobreak = 1 allows assembly code to use ba,bt without
111 .set break -> nobreak = 0, assembly code using ba,bt warns. */
114 /* .cmd line option -relax-all allows all branches and calls to be replaced
115 with longer versions.
116 -no-relax inhibits branch/call conversion.
117 The default value is relax_section, which relaxes branches within
121 } nios2_as_options
= {FALSE
, FALSE
, relax_section
};
124 typedef struct nios2_insn_reloc
126 /* Any expression in the instruction is parsed into this field,
127 which is passed to fix_new_exp() to generate a fixup. */
128 expressionS reloc_expression
;
130 /* The type of the relocation to be applied. */
131 bfd_reloc_code_real_type reloc_type
;
134 unsigned int reloc_pcrel
;
136 /* The next relocation to be applied to the instruction. */
137 struct nios2_insn_reloc
*reloc_next
;
140 /* This struct is used to hold state when assembling instructions. */
141 typedef struct nios2_insn_info
143 /* Assembled instruction. */
144 unsigned long insn_code
;
146 /* Constant bits masked into insn_code for self-check mode. */
147 unsigned long constant_bits
;
149 /* Pointer to the relevant bit of the opcode table. */
150 const struct nios2_opcode
*insn_nios2_opcode
;
151 /* After parsing ptrs to the tokens in the instruction fill this array
152 it is terminated with a null pointer (hence the first +1).
153 The second +1 is because in some parts of the code the opcode
154 is not counted as a token, but still placed in this array. */
155 const char *insn_tokens
[NIOS2_MAX_INSN_TOKENS
+ 1 + 1];
157 /* This holds information used to generate fixups
158 and eventually relocations if it is not null. */
159 nios2_insn_relocS
*insn_reloc
;
163 /* This struct is used to convert Nios II pseudo-ops into the
164 corresponding real op. */
165 typedef struct nios2_ps_insn_info
167 /* Map this pseudo_op... */
168 const char *pseudo_insn
;
170 /* ...to this real instruction. */
173 /* Call this function to modify the operands.... */
174 void (*arg_modifer_func
) (char ** parsed_args
, const char *arg
, int num
,
177 /* ...with these arguments. */
178 const char *arg_modifier
;
182 /* If arg_modifier_func allocates new memory, provide this function
183 to free it afterwards. */
184 void (*arg_cleanup_func
) (char **parsed_args
, int num
, int start
);
185 } nios2_ps_insn_infoS
;
187 /* Opcode hash table. */
188 static struct hash_control
*nios2_opcode_hash
= NULL
;
189 #define nios2_opcode_lookup(NAME) \
190 ((struct nios2_opcode *) hash_find (nios2_opcode_hash, (NAME)))
192 /* Register hash table. */
193 static struct hash_control
*nios2_reg_hash
= NULL
;
194 #define nios2_reg_lookup(NAME) \
195 ((struct nios2_reg *) hash_find (nios2_reg_hash, (NAME)))
198 /* Pseudo-op hash table. */
199 static struct hash_control
*nios2_ps_hash
= NULL
;
200 #define nios2_ps_lookup(NAME) \
201 ((nios2_ps_insn_infoS *) hash_find (nios2_ps_hash, (NAME)))
203 /* The known current alignment of the current section. */
204 static int nios2_current_align
;
205 static segT nios2_current_align_seg
;
207 static int nios2_auto_align_on
= 1;
209 /* The last seen label in the current section. This is used to auto-align
210 labels preceeding instructions. */
211 static symbolS
*nios2_last_label
;
213 /* If we saw a 16-bit CDX instruction, we can align on 2-byte boundaries
214 instead of 4-bytes. Use this to keep track of the minimum power-of-2
216 static int nios2_min_align
= 2;
219 /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
223 /* The processor architecture value, EF_NIOS2_ARCH_R1 by default. */
224 static int nios2_architecture
= EF_NIOS2_ARCH_R1
;
227 /** Utility routines. */
228 /* Function md_chars_to_number takes the sequence of
229 bytes in buf and returns the corresponding value
230 in an int. n must be 1, 2 or 4. */
232 md_chars_to_number (char *buf
, int n
)
237 gas_assert (n
== 1 || n
== 2 || n
== 4);
240 if (target_big_endian
)
241 for (i
= 0; i
< n
; ++i
)
242 val
= val
| ((buf
[i
] & 0xff) << 8 * (n
- (i
+ 1)));
244 for (i
= 0; i
< n
; ++i
)
245 val
= val
| ((buf
[i
] & 0xff) << 8 * i
);
250 /* This function turns a C long int, short int or char
251 into the series of bytes that represent the number
252 on the target machine. */
254 md_number_to_chars (char *buf
, valueT val
, int n
)
256 gas_assert (n
== 1 || n
== 2 || n
== 4 || n
== 8);
257 if (target_big_endian
)
258 number_to_chars_bigendian (buf
, val
, n
);
260 number_to_chars_littleendian (buf
, val
, n
);
263 /* Turn a string in input_line_pointer into a floating point constant
264 of type TYPE, and store the appropriate bytes in *LITP. The number
265 of LITTLENUMS emitted is stored in *SIZEP. An error message is
266 returned, or NULL on OK. */
268 md_atof (int type
, char *litP
, int *sizeP
)
271 LITTLENUM_TYPE words
[4];
285 return _("bad call to md_atof");
288 t
= atof_ieee (input_line_pointer
, type
, words
);
290 input_line_pointer
= t
;
294 if (! target_big_endian
)
295 for (i
= prec
- 1; i
>= 0; i
--, litP
+= 2)
296 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
298 for (i
= 0; i
< prec
; i
++, litP
+= 2)
299 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
304 /* Return true if STR starts with PREFIX, which should be a string literal. */
305 #define strprefix(STR, PREFIX) \
306 (strncmp ((STR), PREFIX, strlen (PREFIX)) == 0)
309 /* Return true if STR is prefixed with a special relocation operator. */
311 nios2_special_relocation_p (const char *str
)
313 return (strprefix (str
, "%lo")
314 || strprefix (str
, "%hi")
315 || strprefix (str
, "%hiadj")
316 || strprefix (str
, "%gprel")
317 || strprefix (str
, "%got")
318 || strprefix (str
, "%call")
319 || strprefix (str
, "%gotoff_lo")
320 || strprefix (str
, "%gotoff_hiadj")
321 || strprefix (str
, "%tls_gd")
322 || strprefix (str
, "%tls_ldm")
323 || strprefix (str
, "%tls_ldo")
324 || strprefix (str
, "%tls_ie")
325 || strprefix (str
, "%tls_le")
326 || strprefix (str
, "%gotoff"));
330 /* nop fill patterns for text section. */
331 static char const nop_r1
[4] = { 0x3a, 0x88, 0x01, 0x00 };
332 static char const nop_r2
[4] = { 0x20, 0x00, 0x00, 0xc4 };
333 static char const nop_r2_cdx
[2] = { 0x3b, 0x00 };
334 static char const *nop32
= nop_r1
;
335 static char const *nop16
= NULL
;
337 /* Handles all machine-dependent alignment needs. */
339 nios2_align (int log_size
, const char *pfill
, symbolS
*label
)
342 long max_alignment
= 15;
344 /* The front end is prone to changing segments out from under us
345 temporarily when -g is in effect. */
346 int switched_seg_p
= (nios2_current_align_seg
!= now_seg
);
349 if (align
> max_alignment
)
351 align
= max_alignment
;
352 as_bad (_("Alignment too large: %d. assumed"), align
);
356 as_warn (_("Alignment negative: 0 assumed"));
362 if (subseg_text_p (now_seg
) && align
>= nios2_min_align
)
364 /* First, make sure we're on the minimum boundary, in case
365 someone has been putting .byte values the text section. */
366 if (nios2_current_align
< nios2_min_align
|| switched_seg_p
)
367 frag_align (nios2_min_align
, 0, 0);
369 /* If we might be on a 2-byte boundary, first align to a
370 4-byte boundary using the 2-byte nop as fill. */
371 if (nios2_min_align
== 1
372 && align
> nios2_min_align
376 frag_align_pattern (2, nop16
, 2, 0);
379 /* Now fill in the alignment pattern. */
381 frag_align_pattern (align
, pfill
, 4, 0);
383 frag_align (align
, 0, 0);
386 frag_align (align
, 0, 0);
389 nios2_current_align
= align
;
391 /* If the last label was in a different section we can't align it. */
392 if (label
!= NULL
&& !switched_seg_p
)
395 int label_seen
= FALSE
;
396 struct frag
*old_frag
;
400 gas_assert (S_GET_SEGMENT (label
) == now_seg
);
402 old_frag
= symbol_get_frag (label
);
403 old_value
= S_GET_VALUE (label
);
404 new_value
= (valueT
) frag_now_fix ();
406 /* It is possible to have more than one label at a particular
407 address, especially if debugging is enabled, so we must
408 take care to adjust all the labels at this address in this
409 fragment. To save time we search from the end of the symbol
410 list, backwards, since the symbols we are interested in are
411 almost certainly the ones that were most recently added.
412 Also to save time we stop searching once we have seen at least
413 one matching label, and we encounter a label that is no longer
414 in the target fragment. Note, this search is guaranteed to
415 find at least one match when sym == label, so no special case
416 code is necessary. */
417 for (sym
= symbol_lastP
; sym
!= NULL
; sym
= symbol_previous (sym
))
418 if (symbol_get_frag (sym
) == old_frag
419 && S_GET_VALUE (sym
) == old_value
)
422 symbol_set_frag (sym
, frag_now
);
423 S_SET_VALUE (sym
, new_value
);
425 else if (label_seen
&& symbol_get_frag (sym
) != old_frag
)
428 record_alignment (now_seg
, align
);
433 /** Support for self-check mode. */
435 /* Mode of the assembler. */
438 NIOS2_MODE_ASSEMBLE
, /* Ordinary operation. */
439 NIOS2_MODE_TEST
/* Hidden mode used for self testing. */
442 static NIOS2_MODE nios2_mode
= NIOS2_MODE_ASSEMBLE
;
444 /* This function is used to in self-checking mode
445 to check the assembled instruction
446 opcode should be the assembled opcode, and exp_opcode
447 the parsed string representing the expected opcode. */
449 nios2_check_assembly (unsigned int opcode
, const char *exp_opcode
)
451 if (nios2_mode
== NIOS2_MODE_TEST
)
453 if (exp_opcode
== NULL
)
454 as_bad (_("expecting opcode string in self test mode"));
455 else if (opcode
!= strtoul (exp_opcode
, NULL
, 16))
456 as_bad (_("assembly 0x%08x, expected %s"), opcode
, exp_opcode
);
461 /** Support for machine-dependent assembler directives. */
462 /* Handle the .align pseudo-op. This aligns to a power of two. It
463 also adjusts any current instruction label. We treat this the same
464 way the MIPS port does: .align 0 turns off auto alignment. */
466 s_nios2_align (int ignore ATTRIBUTE_UNUSED
)
470 const char *pfill
= NULL
;
471 long max_alignment
= 15;
473 align
= get_absolute_expression ();
474 if (align
> max_alignment
)
476 align
= max_alignment
;
477 as_bad (_("Alignment too large: %d. assumed"), align
);
481 as_warn (_("Alignment negative: 0 assumed"));
485 if (*input_line_pointer
== ',')
487 input_line_pointer
++;
488 fill
= get_absolute_expression ();
489 pfill
= (const char *) &fill
;
491 else if (subseg_text_p (now_seg
))
492 pfill
= (const char *) nop32
;
496 nios2_last_label
= NULL
;
501 nios2_auto_align_on
= 1;
502 nios2_align (align
, pfill
, nios2_last_label
);
503 nios2_last_label
= NULL
;
506 nios2_auto_align_on
= 0;
508 demand_empty_rest_of_line ();
511 /* Handle the .text pseudo-op. This is like the usual one, but it
512 clears the saved last label and resets known alignment. */
517 nios2_last_label
= NULL
;
518 nios2_current_align
= 0;
519 nios2_current_align_seg
= now_seg
;
522 /* Handle the .data pseudo-op. This is like the usual one, but it
523 clears the saved last label and resets known alignment. */
528 nios2_last_label
= NULL
;
529 nios2_current_align
= 0;
530 nios2_current_align_seg
= now_seg
;
533 /* Handle the .section pseudo-op. This is like the usual one, but it
534 clears the saved last label and resets known alignment. */
536 s_nios2_section (int ignore
)
538 obj_elf_section (ignore
);
539 nios2_last_label
= NULL
;
540 nios2_current_align
= 0;
541 nios2_current_align_seg
= now_seg
;
544 /* Explicitly unaligned cons. */
546 s_nios2_ucons (int nbytes
)
549 hold
= nios2_auto_align_on
;
550 nios2_auto_align_on
= 0;
552 nios2_auto_align_on
= hold
;
555 /* Handle the .sdata directive. */
557 s_nios2_sdata (int ignore ATTRIBUTE_UNUSED
)
559 get_absolute_expression (); /* Ignored. */
560 subseg_new (".sdata", 0);
561 demand_empty_rest_of_line ();
564 /* .set sets assembler options eg noat/at and is also used
565 to set symbol values (.equ, .equiv ). */
567 s_nios2_set (int equiv
)
569 char *save
= input_line_pointer
;
571 char delim
= get_symbol_name (&directive
);
572 char *endline
= input_line_pointer
;
574 (void) restore_line_pointer (delim
);
576 /* We only want to handle ".set XXX" if the
577 user has tried ".set XXX, YYY" they are not
578 trying a directive. This prevents
579 us from polluting the name space. */
581 if (is_end_of_line
[(unsigned char) *input_line_pointer
])
583 bfd_boolean done
= TRUE
;
586 if (!strcmp (directive
, "noat"))
587 nios2_as_options
.noat
= TRUE
;
588 else if (!strcmp (directive
, "at"))
589 nios2_as_options
.noat
= FALSE
;
590 else if (!strcmp (directive
, "nobreak"))
591 nios2_as_options
.nobreak
= TRUE
;
592 else if (!strcmp (directive
, "break"))
593 nios2_as_options
.nobreak
= FALSE
;
594 else if (!strcmp (directive
, "norelax"))
595 nios2_as_options
.relax
= relax_none
;
596 else if (!strcmp (directive
, "relaxsection"))
597 nios2_as_options
.relax
= relax_section
;
598 else if (!strcmp (directive
, "relaxall"))
599 nios2_as_options
.relax
= relax_all
;
606 demand_empty_rest_of_line ();
611 /* If we fall through to here, either we have ".set XXX, YYY"
612 or we have ".set XXX" where XXX is unknown or we have
614 input_line_pointer
= save
;
618 /* Machine-dependent assembler directives.
619 Format of each entry is:
620 { "directive", handler_func, param } */
621 const pseudo_typeS md_pseudo_table
[] = {
622 {"align", s_nios2_align
, 0},
623 {"text", s_nios2_text
, 0},
624 {"data", s_nios2_data
, 0},
625 {"section", s_nios2_section
, 0},
626 {"section.s", s_nios2_section
, 0},
627 {"sect", s_nios2_section
, 0},
628 {"sect.s", s_nios2_section
, 0},
629 /* .dword and .half are included for compatibility with MIPS. */
632 /* NIOS2 native word size is 4 bytes, so we override
633 the GAS default of 2. */
635 /* Explicitly unaligned directives. */
636 {"2byte", s_nios2_ucons
, 2},
637 {"4byte", s_nios2_ucons
, 4},
638 {"8byte", s_nios2_ucons
, 8},
639 {"16byte", s_nios2_ucons
, 16},
641 {"sdata", s_nios2_sdata
, 0},
643 {"set", s_nios2_set
, 0},
648 /** Relaxation support. */
650 /* We support two relaxation modes: a limited PC-relative mode with
651 -relax-section (the default), and an absolute jump mode with -relax-all.
653 Nios II PC-relative branch instructions only support 16-bit offsets.
654 And, there's no good way to add a 32-bit constant to the PC without
657 To deal with this, for the pc-relative relaxation mode we convert
659 into a series of 16-bit adds, like:
663 addi at, at, remainder
666 Similarly, conditional branches are converted from
667 b(condition) r, s, label
669 b(opposite condition) r, s, skip
673 addi at, at, remainder
677 The compiler can do a better job, either by converting the branch
678 directly into a JMP (going through the GOT for PIC) or by allocating
679 a second register for the 32-bit displacement.
681 For the -relax-all relaxation mode, the conversions are
682 movhi at, %hi(symbol+offset)
683 ori at, %lo(symbol+offset)
686 b(opposite condition), r, s, skip
687 movhi at, %hi(symbol+offset)
688 ori at, %lo(symbol+offset)
693 16-bit CDX branch instructions are relaxed first into equivalent
694 32-bit branches and then the above transformations are applied
699 /* Arbitrarily limit the number of addis we can insert; we need to be able
700 to specify the maximum growth size for each frag that contains a
701 relaxable branch. There's no point in specifying a huge number here
702 since that means the assembler needs to allocate that much extra
703 memory for every branch, and almost no real code will ever need it.
704 Plus, as already noted a better solution is to just use a jmp, or
705 allocate a second register to hold a 32-bit displacement.
706 FIXME: Rather than making this a constant, it could be controlled by
707 a command-line argument. */
708 #define RELAX_MAX_ADDI 32
710 /* The fr_subtype field represents the target-specific relocation state.
711 It has type relax_substateT (unsigned int). We use it to track the
712 number of addis necessary, plus a bit to track whether this is a
713 conditional branch and a bit for 16-bit CDX instructions.
714 Regardless of the smaller RELAX_MAX_ADDI limit, we reserve 16 bits
715 in the fr_subtype to encode the number of addis so that the whole
716 theoretically-valid range is representable.
717 For the -relax-all mode, N = 0 represents an in-range branch and N = 1
718 represents a branch that needs to be relaxed. */
719 #define UBRANCH (0 << 16)
720 #define CBRANCH (1 << 16)
721 #define CDXBRANCH (1 << 17)
722 #define IS_CBRANCH(SUBTYPE) ((SUBTYPE) & CBRANCH)
723 #define IS_UBRANCH(SUBTYPE) (!IS_CBRANCH (SUBTYPE))
724 #define IS_CDXBRANCH(SUBTYPE) ((SUBTYPE) & CDXBRANCH)
725 #define UBRANCH_SUBTYPE(N) (UBRANCH | (N))
726 #define CBRANCH_SUBTYPE(N) (CBRANCH | (N))
727 #define CDX_UBRANCH_SUBTYPE(N) (CDXBRANCH | UBRANCH | (N))
728 #define CDX_CBRANCH_SUBTYPE(N) (CDXBRANCH | CBRANCH | (N))
729 #define SUBTYPE_ADDIS(SUBTYPE) ((SUBTYPE) & 0xffff)
731 /* For the -relax-section mode, unconditional branches require 2 extra i
732 nstructions besides the addis, conditional branches require 3. */
733 #define UBRANCH_ADDIS_TO_SIZE(N) (((N) + 2) * 4)
734 #define CBRANCH_ADDIS_TO_SIZE(N) (((N) + 3) * 4)
736 /* For the -relax-all mode, unconditional branches require 3 instructions
737 and conditional branches require 4. */
738 #define UBRANCH_JUMP_SIZE 12
739 #define CBRANCH_JUMP_SIZE 16
741 /* Maximum sizes of relaxation sequences. */
742 #define UBRANCH_MAX_SIZE \
743 (nios2_as_options.relax == relax_all \
744 ? UBRANCH_JUMP_SIZE \
745 : UBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
746 #define CBRANCH_MAX_SIZE \
747 (nios2_as_options.relax == relax_all \
748 ? CBRANCH_JUMP_SIZE \
749 : CBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
751 /* Register number of AT, the assembler temporary. */
754 /* Determine how many bytes are required to represent the sequence
755 indicated by SUBTYPE. */
757 nios2_relax_subtype_size (relax_substateT subtype
)
759 int n
= SUBTYPE_ADDIS (subtype
);
761 /* Regular conditional/unconditional branch instruction. */
762 return (IS_CDXBRANCH (subtype
) ? 2 : 4);
763 else if (nios2_as_options
.relax
== relax_all
)
764 return (IS_CBRANCH (subtype
) ? CBRANCH_JUMP_SIZE
: UBRANCH_JUMP_SIZE
);
765 else if (IS_CBRANCH (subtype
))
766 return CBRANCH_ADDIS_TO_SIZE (n
);
768 return UBRANCH_ADDIS_TO_SIZE (n
);
771 /* Estimate size of fragp before relaxation.
772 This could also examine the offset in fragp and adjust
773 fragp->fr_subtype, but we will do that in nios2_relax_frag anyway. */
775 md_estimate_size_before_relax (fragS
*fragp
, segT segment ATTRIBUTE_UNUSED
)
777 return nios2_relax_subtype_size (fragp
->fr_subtype
);
780 /* Implement md_relax_frag, returning the change in size of the frag. */
782 nios2_relax_frag (segT segment
, fragS
*fragp
, long stretch
)
784 addressT target
= fragp
->fr_offset
;
785 relax_substateT subtype
= fragp
->fr_subtype
;
786 symbolS
*symbolp
= fragp
->fr_symbol
;
790 fragS
*sym_frag
= symbol_get_frag (symbolp
);
793 bfd_boolean is_cdx
= FALSE
;
795 target
+= S_GET_VALUE (symbolp
);
797 /* See comments in write.c:relax_frag about handling of stretch. */
799 && sym_frag
->relax_marker
!= fragp
->relax_marker
)
801 if (stretch
< 0 || sym_frag
->region
== fragp
->region
)
803 else if (target
< fragp
->fr_address
)
804 target
= fragp
->fr_next
->fr_address
+ stretch
;
807 /* We subtract fr_var (4 for 32-bit insns) because all pc relative
808 branches are from the next instruction. */
809 offset
= target
- fragp
->fr_address
- fragp
->fr_fix
- fragp
->fr_var
;
810 if (IS_CDXBRANCH (subtype
) && IS_UBRANCH (subtype
)
811 && offset
>= -1024 && offset
< 1024)
812 /* PC-relative CDX branch with 11-bit offset. */
814 else if (IS_CDXBRANCH (subtype
) && IS_CBRANCH (subtype
)
815 && offset
>= -128 && offset
< 128)
816 /* PC-relative CDX branch with 8-bit offset. */
818 else if (offset
>= -32768 && offset
< 32768)
819 /* Fits in PC-relative branch. */
821 else if (nios2_as_options
.relax
== relax_all
)
822 /* Convert to jump. */
824 else if (nios2_as_options
.relax
== relax_section
825 && S_GET_SEGMENT (symbolp
) == segment
826 && S_IS_DEFINED (symbolp
))
827 /* Attempt a PC-relative relaxation on a branch to a defined
828 symbol in the same segment. */
830 /* The relaxation for conditional branches is offset by 4
831 bytes because we insert the inverted branch around the
833 if (IS_CBRANCH (subtype
))
836 n
= offset
/ 32767 + 1;
838 n
= offset
/ -32768 + 1;
840 /* Bail out immediately if relaxation has failed. If we try to
841 defer the diagnostic to md_convert_frag, some pathological test
842 cases (e.g. gcc/testsuite/gcc.c-torture/compile/20001226-1.c)
843 apparently never converge. By returning 0 here we could pretend
844 to the caller that nothing has changed, but that leaves things
845 in an inconsistent state when we get to md_convert_frag. */
846 if (n
> RELAX_MAX_ADDI
)
848 as_bad_where (fragp
->fr_file
, fragp
->fr_line
,
849 _("branch offset out of range\n"));
850 as_fatal (_("branch relaxation failed\n"));
854 /* We cannot handle this case, diagnose overflow later. */
858 fragp
->fr_subtype
= subtype
;
859 else if (IS_CBRANCH (subtype
))
860 fragp
->fr_subtype
= CBRANCH_SUBTYPE (n
);
862 fragp
->fr_subtype
= UBRANCH_SUBTYPE (n
);
864 return (nios2_relax_subtype_size (fragp
->fr_subtype
)
865 - nios2_relax_subtype_size (subtype
));
868 /* If we got here, it's probably an error. */
873 /* Complete fragp using the data from the relaxation pass. */
875 md_convert_frag (bfd
*headers ATTRIBUTE_UNUSED
, segT segment ATTRIBUTE_UNUSED
,
878 char *buffer
= fragp
->fr_literal
+ fragp
->fr_fix
;
879 relax_substateT subtype
= fragp
->fr_subtype
;
880 int n
= SUBTYPE_ADDIS (subtype
);
881 addressT target
= fragp
->fr_offset
;
882 symbolS
*symbolp
= fragp
->fr_symbol
;
884 unsigned int addend_mask
, addi_mask
, op
;
885 offsetT addend
, remainder
;
887 bfd_boolean is_r2
= (bfd_get_mach (stdoutput
) == bfd_mach_nios2r2
);
889 /* If this is a CDX branch we're not relaxing, just generate the fixup. */
890 if (IS_CDXBRANCH (subtype
))
893 fix_new (fragp
, fragp
->fr_fix
, 2, fragp
->fr_symbol
,
895 (IS_UBRANCH (subtype
)
896 ? BFD_RELOC_NIOS2_R2_I10_1_PCREL
897 : BFD_RELOC_NIOS2_R2_T1I7_1_PCREL
));
902 /* If this is a CDX branch we are relaxing, turn it into an equivalent
903 32-bit branch and then fall through to the normal non-CDX cases. */
904 if (fragp
->fr_var
== 2)
906 unsigned int opcode
= md_chars_to_number (buffer
, 2);
908 if (IS_CBRANCH (subtype
))
910 unsigned int reg
= nios2_r2_reg3_mappings
[GET_IW_T1I7_A3 (opcode
)];
911 if (GET_IW_R2_OP (opcode
) == R2_OP_BNEZ_N
)
912 opcode
= MATCH_R2_BNE
| SET_IW_F2I16_A (reg
);
914 opcode
= MATCH_R2_BEQ
| SET_IW_F2I16_A (reg
);
917 opcode
= MATCH_R2_BR
;
918 md_number_to_chars (buffer
, opcode
, 4);
922 /* If we didn't or can't relax, this is a regular branch instruction.
923 We just need to generate the fixup for the symbol and offset. */
926 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
,
927 fragp
->fr_offset
, 1, BFD_RELOC_16_PCREL
);
932 /* Replace the cbranch at fr_fix with one that has the opposite condition
933 in order to jump around the block of instructions we'll be adding. */
934 if (IS_CBRANCH (subtype
))
936 unsigned int br_opcode
;
937 unsigned int old_op
, new_op
;
940 /* Account for the nextpc and jmp in the pc-relative case, or the two
941 load instructions and jump in the absolute case. */
942 if (nios2_as_options
.relax
== relax_section
)
943 nbytes
= (n
+ 2) * 4;
947 br_opcode
= md_chars_to_number (buffer
, 4);
950 old_op
= GET_IW_R2_OP (br_opcode
);
974 br_opcode
= ((br_opcode
& ~IW_R2_OP_SHIFTED_MASK
)
975 | SET_IW_R2_OP (new_op
));
976 br_opcode
= br_opcode
| SET_IW_F2I16_IMM16 (nbytes
);
980 old_op
= GET_IW_R1_OP (br_opcode
);
1004 br_opcode
= ((br_opcode
& ~IW_R1_OP_SHIFTED_MASK
)
1005 | SET_IW_R1_OP (new_op
));
1006 br_opcode
= br_opcode
| SET_IW_I_IMM16 (nbytes
);
1008 md_number_to_chars (buffer
, br_opcode
, 4);
1013 /* Load at for the PC-relative case. */
1014 if (nios2_as_options
.relax
== relax_section
)
1016 /* Insert the nextpc instruction. */
1018 op
= MATCH_R2_NEXTPC
| SET_IW_F3X6L5_C (AT_REGNUM
);
1020 op
= MATCH_R1_NEXTPC
| SET_IW_R_C (AT_REGNUM
);
1021 md_number_to_chars (buffer
, op
, 4);
1025 /* We need to know whether the offset is positive or negative. */
1026 target
+= S_GET_VALUE (symbolp
);
1027 offset
= target
- fragp
->fr_address
- fragp
->fr_fix
;
1033 addend_mask
= SET_IW_F2I16_IMM16 ((unsigned int)addend
);
1035 addend_mask
= SET_IW_I_IMM16 ((unsigned int)addend
);
1037 /* Insert n-1 addi instructions. */
1039 addi_mask
= (MATCH_R2_ADDI
1040 | SET_IW_F2I16_B (AT_REGNUM
)
1041 | SET_IW_F2I16_A (AT_REGNUM
));
1043 addi_mask
= (MATCH_R1_ADDI
1044 | SET_IW_I_B (AT_REGNUM
)
1045 | SET_IW_I_A (AT_REGNUM
));
1046 for (i
= 0; i
< n
- 1; i
++)
1048 md_number_to_chars (buffer
, addi_mask
| addend_mask
, 4);
1053 /* Insert the last addi instruction to hold the remainder. */
1054 remainder
= offset
- addend
* (n
- 1);
1055 gas_assert (remainder
>= -32768 && remainder
<= 32767);
1057 addend_mask
= SET_IW_F2I16_IMM16 ((unsigned int)remainder
);
1059 addend_mask
= SET_IW_I_IMM16 ((unsigned int)remainder
);
1060 md_number_to_chars (buffer
, addi_mask
| addend_mask
, 4);
1065 /* Load at for the absolute case. */
1069 op
= MATCH_R2_ORHI
| SET_IW_F2I16_B (AT_REGNUM
) | SET_IW_F2I16_A (0);
1071 op
= MATCH_R1_ORHI
| SET_IW_I_B (AT_REGNUM
) | SET_IW_I_A (0);
1072 md_number_to_chars (buffer
, op
, 4);
1073 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
, fragp
->fr_offset
,
1074 0, BFD_RELOC_NIOS2_HI16
);
1078 op
= (MATCH_R2_ORI
| SET_IW_F2I16_B (AT_REGNUM
)
1079 | SET_IW_F2I16_A (AT_REGNUM
));
1081 op
= (MATCH_R1_ORI
| SET_IW_I_B (AT_REGNUM
)
1082 | SET_IW_I_A (AT_REGNUM
));
1083 md_number_to_chars (buffer
, op
, 4);
1084 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
, fragp
->fr_offset
,
1085 0, BFD_RELOC_NIOS2_LO16
);
1090 /* Insert the jmp instruction. */
1092 op
= MATCH_R2_JMP
| SET_IW_F3X6L5_A (AT_REGNUM
);
1094 op
= MATCH_R1_JMP
| SET_IW_R_A (AT_REGNUM
);
1095 md_number_to_chars (buffer
, op
, 4);
1101 /** Fixups and overflow checking. */
1103 /* Check a fixup for overflow. */
1105 nios2_check_overflow (valueT fixup
, reloc_howto_type
*howto
)
1107 /* If there is a rightshift, check that the low-order bits are
1108 zero before applying it. */
1109 if (howto
->rightshift
)
1111 if ((~(~((valueT
) 0) << howto
->rightshift
) & fixup
)
1112 && howto
->complain_on_overflow
!= complain_overflow_dont
)
1114 fixup
= ((signed)fixup
) >> howto
->rightshift
;
1117 /* Check for overflow - return TRUE if overflow, FALSE if not. */
1118 switch (howto
->complain_on_overflow
)
1120 case complain_overflow_dont
:
1122 case complain_overflow_bitfield
:
1123 if ((fixup
>> howto
->bitsize
) != 0
1124 && ((signed) fixup
>> howto
->bitsize
) != -1)
1127 case complain_overflow_signed
:
1128 if ((fixup
& 0x80000000) > 0)
1130 /* Check for negative overflow. */
1131 if ((signed) fixup
< ((signed) ~0 << (howto
->bitsize
-1)))
1136 /* Check for positive overflow. */
1137 if (fixup
>= ((unsigned) 1 << (howto
->bitsize
- 1)))
1141 case complain_overflow_unsigned
:
1142 if ((fixup
>> howto
->bitsize
) != 0)
1146 as_bad (_("error checking for overflow - broken assembler"));
1152 /* Emit diagnostic for fixup overflow. */
1154 nios2_diagnose_overflow (valueT fixup
, reloc_howto_type
*howto
,
1155 fixS
*fixP
, valueT value
)
1157 if (fixP
->fx_r_type
== BFD_RELOC_8
1158 || fixP
->fx_r_type
== BFD_RELOC_16
1159 || fixP
->fx_r_type
== BFD_RELOC_32
)
1160 /* These relocs are against data, not instructions. */
1161 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1162 _("immediate value 0x%x truncated to 0x%x"),
1163 (unsigned int) fixup
,
1164 (unsigned int) (~(~(valueT
) 0 << howto
->bitsize
) & fixup
));
1167 /* What opcode is the instruction? This will determine
1168 whether we check for overflow in immediate values
1169 and what error message we get. */
1170 const struct nios2_opcode
*opcode
;
1171 enum overflow_type overflow_msg_type
;
1172 unsigned int range_min
;
1173 unsigned int range_max
;
1174 unsigned int address
;
1176 opcode
= nios2_find_opcode_hash (value
, bfd_get_mach (stdoutput
));
1177 gas_assert (opcode
);
1178 gas_assert (fixP
->fx_size
== opcode
->size
);
1179 overflow_msg_type
= opcode
->overflow_msg
;
1180 switch (overflow_msg_type
)
1182 case call_target_overflow
:
1184 = ((fixP
->fx_frag
->fr_address
+ fixP
->fx_where
) & 0xf0000000);
1185 range_max
= range_min
+ 0x0fffffff;
1186 address
= fixup
| range_min
;
1188 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1189 _("call target address 0x%08x out of range 0x%08x to 0x%08x"),
1190 address
, range_min
, range_max
);
1192 case branch_target_overflow
:
1193 if (opcode
->format
== iw_i_type
|| opcode
->format
== iw_F2I16_type
)
1194 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1195 _("branch offset %d out of range %d to %d"),
1196 (int)fixup
, -32768, 32767);
1198 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1199 _("branch offset %d out of range"),
1202 case address_offset_overflow
:
1203 if (opcode
->format
== iw_i_type
|| opcode
->format
== iw_F2I16_type
)
1204 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1205 _("%s offset %d out of range %d to %d"),
1206 opcode
->name
, (int)fixup
, -32768, 32767);
1208 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1209 _("%s offset %d out of range"),
1210 opcode
->name
, (int)fixup
);
1212 case signed_immed16_overflow
:
1213 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1214 _("immediate value %d out of range %d to %d"),
1215 (int)fixup
, -32768, 32767);
1217 case unsigned_immed16_overflow
:
1218 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1219 _("immediate value %u out of range %u to %u"),
1220 (unsigned int)fixup
, 0, 65535);
1222 case unsigned_immed5_overflow
:
1223 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1224 _("immediate value %u out of range %u to %u"),
1225 (unsigned int)fixup
, 0, 31);
1227 case signed_immed12_overflow
:
1228 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1229 _("immediate value %d out of range %d to %d"),
1230 (int)fixup
, -2048, 2047);
1232 case custom_opcode_overflow
:
1233 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1234 _("custom instruction opcode %u out of range %u to %u"),
1235 (unsigned int)fixup
, 0, 255);
1238 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1239 _("overflow in immediate argument"));
1245 /* Apply a fixup to the object file. */
1247 md_apply_fix (fixS
*fixP
, valueT
*valP
, segT seg ATTRIBUTE_UNUSED
)
1249 /* Assert that the fixup is one we can handle. */
1250 gas_assert (fixP
!= NULL
&& valP
!= NULL
1251 && (fixP
->fx_r_type
== BFD_RELOC_8
1252 || fixP
->fx_r_type
== BFD_RELOC_16
1253 || fixP
->fx_r_type
== BFD_RELOC_32
1254 || fixP
->fx_r_type
== BFD_RELOC_64
1255 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_S16
1256 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_U16
1257 || fixP
->fx_r_type
== BFD_RELOC_16_PCREL
1258 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL26
1259 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM5
1260 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CACHE_OPX
1261 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM6
1262 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM8
1263 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_HI16
1264 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_LO16
1265 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_HIADJ16
1266 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GPREL
1267 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1268 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
1269 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_UJMP
1270 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CJMP
1271 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALLR
1272 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_ALIGN
1273 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOT16
1274 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL16
1275 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_LO
1276 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_HA
1277 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_GD16
1278 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDM16
1279 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDO16
1280 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_IE16
1281 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LE16
1282 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF
1283 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPREL
1284 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL26_NOAT
1285 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOT_LO
1286 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOT_HA
1287 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL_LO
1288 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL_HA
1289 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_S12
1290 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_I10_1_PCREL
1291 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_T1I7_1_PCREL
1292 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_T1I7_2
1293 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_T2I4
1294 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_T2I4_1
1295 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_T2I4_2
1296 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_X1I7_2
1297 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_X2L5
1298 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_F1I5_2
1299 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_L5I4X1
1300 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_T1X1I6
1301 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_T1X1I6_2
1302 /* Add other relocs here as we generate them. */
1305 if (fixP
->fx_r_type
== BFD_RELOC_64
)
1307 /* We may reach here due to .8byte directives, but we never output
1308 BFD_RELOC_64; it must be resolved. */
1309 if (fixP
->fx_addsy
!= NULL
)
1310 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1311 _("cannot create 64-bit relocation"));
1314 md_number_to_chars (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
,
1321 /* The value passed in valP can be the value of a fully
1322 resolved expression, or it can be the value of a partially
1323 resolved expression. In the former case, both fixP->fx_addsy
1324 and fixP->fx_subsy are NULL, and fixP->fx_offset == *valP, and
1325 we can fix up the instruction that fixP relates to.
1326 In the latter case, one or both of fixP->fx_addsy and
1327 fixP->fx_subsy are not NULL, and fixP->fx_offset may or may not
1328 equal *valP. We don't need to check for fixP->fx_subsy being null
1329 because the generic part of the assembler generates an error if
1330 it is not an absolute symbol. */
1331 if (fixP
->fx_addsy
!= NULL
)
1332 /* Partially resolved expression. */
1334 fixP
->fx_addnumber
= fixP
->fx_offset
;
1337 switch (fixP
->fx_r_type
)
1339 case BFD_RELOC_NIOS2_TLS_GD16
:
1340 case BFD_RELOC_NIOS2_TLS_LDM16
:
1341 case BFD_RELOC_NIOS2_TLS_LDO16
:
1342 case BFD_RELOC_NIOS2_TLS_IE16
:
1343 case BFD_RELOC_NIOS2_TLS_LE16
:
1344 case BFD_RELOC_NIOS2_TLS_DTPMOD
:
1345 case BFD_RELOC_NIOS2_TLS_DTPREL
:
1346 case BFD_RELOC_NIOS2_TLS_TPREL
:
1347 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
1354 /* Fully resolved fixup. */
1356 reloc_howto_type
*howto
1357 = bfd_reloc_type_lookup (stdoutput
, fixP
->fx_r_type
);
1360 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1361 _("relocation is not supported"));
1364 valueT fixup
= *valP
;
1368 /* If this is a pc-relative relocation, we need to
1369 subtract the current offset within the object file
1370 FIXME : for some reason fixP->fx_pcrel isn't 1 when it should be
1371 so I'm using the howto structure instead to determine this. */
1372 if (howto
->pc_relative
== 1)
1374 fixup
= (fixup
- (fixP
->fx_frag
->fr_address
+ fixP
->fx_where
1379 /* Get the instruction or data to be fixed up. */
1380 buf
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
1381 value
= md_chars_to_number (buf
, fixP
->fx_size
);
1383 /* Check for overflow, emitting a diagnostic if necessary. */
1384 if (nios2_check_overflow (fixup
, howto
))
1385 nios2_diagnose_overflow (fixup
, howto
, fixP
, value
);
1387 /* Apply the right shift. */
1388 fixup
= ((signed)fixup
) >> howto
->rightshift
;
1390 /* Truncate the fixup to right size. */
1391 switch (fixP
->fx_r_type
)
1393 case BFD_RELOC_NIOS2_HI16
:
1394 fixup
= (fixup
>> 16) & 0xFFFF;
1396 case BFD_RELOC_NIOS2_LO16
:
1397 fixup
= fixup
& 0xFFFF;
1399 case BFD_RELOC_NIOS2_HIADJ16
:
1400 fixup
= ((((fixup
>> 16) & 0xFFFF) + ((fixup
>> 15) & 0x01))
1405 int n
= sizeof (fixup
) * 8 - howto
->bitsize
;
1406 fixup
= (fixup
<< n
) >> n
;
1411 /* Fix up the instruction. */
1412 value
= (value
& ~howto
->dst_mask
) | (fixup
<< howto
->bitpos
);
1413 md_number_to_chars (buf
, value
, fixP
->fx_size
);
1419 if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
)
1423 && !S_IS_DEFINED (fixP
->fx_addsy
) && !S_IS_WEAK (fixP
->fx_addsy
))
1424 S_SET_WEAK (fixP
->fx_addsy
);
1426 else if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1432 /** Instruction parsing support. */
1434 /* General internal error routine. */
1437 bad_opcode (const struct nios2_opcode
*op
)
1439 fprintf (stderr
, _("internal error: broken opcode descriptor for `%s %s'\n"),
1440 op
->name
, op
->args
);
1441 as_fatal (_("Broken assembler. No assembly attempted."));
1444 /* Special relocation directive strings. */
1446 struct nios2_special_relocS
1449 bfd_reloc_code_real_type reloc_type
;
1452 /* This table is sorted so that prefix strings are listed after the longer
1453 strings that include them -- e.g., %got after %got_hiadj, etc. */
1455 struct nios2_special_relocS nios2_special_reloc
[] = {
1456 {"%hiadj", BFD_RELOC_NIOS2_HIADJ16
},
1457 {"%hi", BFD_RELOC_NIOS2_HI16
},
1458 {"%lo", BFD_RELOC_NIOS2_LO16
},
1459 {"%gprel", BFD_RELOC_NIOS2_GPREL
},
1460 {"%call_lo", BFD_RELOC_NIOS2_CALL_LO
},
1461 {"%call_hiadj", BFD_RELOC_NIOS2_CALL_HA
},
1462 {"%call", BFD_RELOC_NIOS2_CALL16
},
1463 {"%gotoff_lo", BFD_RELOC_NIOS2_GOTOFF_LO
},
1464 {"%gotoff_hiadj", BFD_RELOC_NIOS2_GOTOFF_HA
},
1465 {"%gotoff", BFD_RELOC_NIOS2_GOTOFF
},
1466 {"%got_hiadj", BFD_RELOC_NIOS2_GOT_HA
},
1467 {"%got_lo", BFD_RELOC_NIOS2_GOT_LO
},
1468 {"%got", BFD_RELOC_NIOS2_GOT16
},
1469 {"%tls_gd", BFD_RELOC_NIOS2_TLS_GD16
},
1470 {"%tls_ldm", BFD_RELOC_NIOS2_TLS_LDM16
},
1471 {"%tls_ldo", BFD_RELOC_NIOS2_TLS_LDO16
},
1472 {"%tls_ie", BFD_RELOC_NIOS2_TLS_IE16
},
1473 {"%tls_le", BFD_RELOC_NIOS2_TLS_LE16
},
1476 #define NIOS2_NUM_SPECIAL_RELOCS \
1477 (sizeof(nios2_special_reloc)/sizeof(nios2_special_reloc[0]))
1478 const int nios2_num_special_relocs
= NIOS2_NUM_SPECIAL_RELOCS
;
1480 /* Creates a new nios2_insn_relocS and returns a pointer to it. */
1481 static nios2_insn_relocS
*
1482 nios2_insn_reloc_new (bfd_reloc_code_real_type reloc_type
, unsigned int pcrel
)
1484 nios2_insn_relocS
*retval
;
1485 retval
= (nios2_insn_relocS
*) malloc (sizeof (nios2_insn_relocS
));
1488 as_bad (_("can't create relocation"));
1492 /* Fill out the fields with default values. */
1493 retval
->reloc_next
= NULL
;
1494 retval
->reloc_type
= reloc_type
;
1495 retval
->reloc_pcrel
= pcrel
;
1499 /* Frees up memory previously allocated by nios2_insn_reloc_new(). */
1500 /* FIXME: this is never called; memory leak? */
1503 nios2_insn_reloc_destroy (nios2_insn_relocS
*reloc
)
1505 gas_assert (reloc
!= NULL
);
1510 /* Look up a register name and validate it for the given regtype.
1511 Return the register mapping or NULL on failure. */
1512 static struct nios2_reg
*
1513 nios2_parse_reg (const char *token
, unsigned long regtype
)
1515 struct nios2_reg
*reg
= nios2_reg_lookup (token
);
1519 as_bad (_("unknown register %s"), token
);
1523 /* Matched a register, but is it the wrong type? */
1524 if (!(regtype
& reg
->regtype
))
1526 if (regtype
& REG_CONTROL
)
1527 as_bad (_("expecting control register"));
1528 else if (reg
->regtype
& REG_CONTROL
)
1529 as_bad (_("illegal use of control register"));
1530 else if (reg
->regtype
& REG_COPROCESSOR
)
1531 as_bad (_("illegal use of coprocessor register"));
1533 as_bad (_("invalid register %s"), token
);
1537 /* Warn for explicit use of special registers. */
1538 if (reg
->regtype
& REG_NORMAL
)
1540 if (!nios2_as_options
.noat
&& reg
->index
== 1)
1541 as_warn (_("Register at (r1) can sometimes be corrupted by "
1542 "assembler optimizations.\n"
1543 "Use .set noat to turn off those optimizations "
1544 "(and this warning)."));
1545 if (!nios2_as_options
.nobreak
&& reg
->index
== 25)
1546 as_warn (_("The debugger will corrupt bt (r25).\n"
1547 "If you don't need to debug this "
1548 "code use .set nobreak to turn off this warning."));
1549 if (!nios2_as_options
.nobreak
&& reg
->index
== 30)
1550 as_warn (_("The debugger will corrupt sstatus/ba (r30).\n"
1551 "If you don't need to debug this "
1552 "code use .set nobreak to turn off this warning."));
1558 /* This function parses a reglist for ldwm/stwm and push.n/pop.n
1559 instructions, given as a brace-enclosed register list. The tokenizer
1560 has replaced commas in the token with spaces.
1561 The return value is a bitmask of registers in the set. It also
1562 sets nios2_reglist_mask and nios2_reglist_dir to allow error checking
1563 when parsing the base register. */
1565 static unsigned long nios2_reglist_mask
;
1566 static int nios2_reglist_dir
;
1568 static unsigned long
1569 nios2_parse_reglist (char *token
, const struct nios2_opcode
*op
)
1571 unsigned long mask
= 0;
1573 unsigned long regtype
= 0;
1575 const char *regname
;
1577 nios2_reglist_mask
= 0;
1578 nios2_reglist_dir
= 0;
1580 if (op
->match
== MATCH_R2_LDWM
|| op
->match
== MATCH_R2_STWM
)
1585 else if (op
->match
== MATCH_R2_PUSH_N
)
1590 else if (op
->match
== MATCH_R2_POP_N
)
1598 for (regname
= strtok (token
, "{ }");
1600 regname
= strtok (NULL
, "{ }"))
1603 struct nios2_reg
*reg
= nios2_parse_reg (regname
, regtype
);
1609 /* Make sure registers are listed in proper sequence. */
1614 as_bad ("duplicate register %s\n", reg
->name
);
1618 dir
= (regno
< last
? -1 : 1);
1619 else if ((dir
> 0 && regno
< last
)
1620 || (dir
< 0 && regno
> last
)
1621 || (op
->match
== MATCH_R2_PUSH_N
1622 && ! ((last
== 31 && regno
== 28)
1623 || (last
== 31 && regno
<= 23)
1624 || (last
== 28 && regno
<= 23)
1625 || (regno
< 23 && regno
== last
- 1)))
1626 || (op
->match
== MATCH_R2_POP_N
1627 && ! ((regno
== 31 && last
== 28)
1628 || (regno
== 31 && last
<= 23)
1629 || (regno
== 28 && last
<= 23)
1630 || (last
< 23 && last
== regno
- 1))))
1632 as_bad ("invalid register order");
1641 /* Check that all ldwm/stwm regs belong to the same set. */
1642 if ((op
->match
== MATCH_R2_LDWM
|| op
->match
== MATCH_R2_STWM
)
1643 && (mask
& 0x00003ffc) && (mask
& 0x90ffc000))
1645 as_bad ("invalid register set in reglist");
1649 /* Check that push.n/pop.n regs include RA. */
1650 if ((op
->match
== MATCH_R2_PUSH_N
|| op
->match
== MATCH_R2_POP_N
)
1651 && ((mask
& 0x80000000) == 0))
1653 as_bad ("reglist must include ra (r31)");
1657 /* Check that there is at least one register in the set. */
1660 as_bad ("reglist must include at least one register");
1664 /* OK, reglist passed validation. */
1665 nios2_reglist_mask
= mask
;
1666 nios2_reglist_dir
= dir
;
1670 /* This function parses the base register and options used by the ldwm/stwm
1671 instructions. Returns the base register and sets the option arguments
1672 accordingly. On failure, returns NULL. */
1673 static struct nios2_reg
*
1674 nios2_parse_base_register (char *str
, int *direction
, int *writeback
, int *ret
)
1677 struct nios2_reg
*reg
;
1684 if (strncmp (str
, "--", 2) == 0)
1690 /* Extract the base register. */
1693 as_bad ("expected '(' before base register");
1698 str
= strchr (str
, ')');
1701 as_bad ("expected ')' after base register");
1706 reg
= nios2_parse_reg (regname
, REG_NORMAL
);
1711 if (strncmp (str
, "++", 2) == 0)
1717 /* Ensure that either -- or ++ is specified, but not both. */
1718 if (*direction
== 0)
1720 as_bad ("invalid base register syntax");
1724 /* Check for options. The tokenizer has replaced commas with spaces. */
1729 if (strncmp (str
, "writeback", 9) == 0)
1734 else if (strncmp (str
, "ret", 3) == 0)
1741 as_bad ("invalid option syntax");
1750 /* The various nios2_assemble_* functions call this
1751 function to generate an expression from a string representing an expression.
1752 It then tries to evaluate the expression, and if it can, returns its value.
1753 If not, it creates a new nios2_insn_relocS and stores the expression and
1754 reloc_type for future use. */
1755 static unsigned long
1756 nios2_assemble_expression (const char *exprstr
,
1757 nios2_insn_infoS
*insn
,
1758 bfd_reloc_code_real_type orig_reloc_type
,
1761 nios2_insn_relocS
*reloc
;
1762 char *saved_line_ptr
;
1763 unsigned long value
= 0;
1765 bfd_reloc_code_real_type reloc_type
= orig_reloc_type
;
1767 gas_assert (exprstr
!= NULL
);
1768 gas_assert (insn
!= NULL
);
1770 /* Check for relocation operators.
1771 Change the relocation type and advance the ptr to the start of
1772 the expression proper. */
1773 for (i
= 0; i
< nios2_num_special_relocs
; i
++)
1774 if (strstr (exprstr
, nios2_special_reloc
[i
].string
) != NULL
)
1776 reloc_type
= nios2_special_reloc
[i
].reloc_type
;
1777 exprstr
+= strlen (nios2_special_reloc
[i
].string
) + 1;
1779 /* %lo and %hiadj have different meanings for PC-relative
1783 if (reloc_type
== BFD_RELOC_NIOS2_LO16
)
1784 reloc_type
= BFD_RELOC_NIOS2_PCREL_LO
;
1785 if (reloc_type
== BFD_RELOC_NIOS2_HIADJ16
)
1786 reloc_type
= BFD_RELOC_NIOS2_PCREL_HA
;
1792 /* No relocation allowed; we must have a constant expression. */
1793 if (orig_reloc_type
== BFD_RELOC_NONE
)
1797 /* Parse the expression string. */
1798 saved_line_ptr
= input_line_pointer
;
1799 input_line_pointer
= (char *) exprstr
;
1801 input_line_pointer
= saved_line_ptr
;
1803 /* If we don't have a constant, give an error. */
1804 if (reloc_type
!= orig_reloc_type
|| exp
.X_op
!= O_constant
)
1805 as_bad (_("expression must be constant"));
1807 value
= exp
.X_add_number
;
1808 return (unsigned long) value
;
1811 /* We potentially have a relocation. */
1812 reloc
= nios2_insn_reloc_new (reloc_type
, pcrel
);
1813 reloc
->reloc_next
= insn
->insn_reloc
;
1814 insn
->insn_reloc
= reloc
;
1816 /* Parse the expression string. */
1817 saved_line_ptr
= input_line_pointer
;
1818 input_line_pointer
= (char *) exprstr
;
1819 expression (&reloc
->reloc_expression
);
1820 input_line_pointer
= saved_line_ptr
;
1822 /* This is redundant as the fixup will put this into
1823 the instruction, but it is included here so that
1824 self-test mode (-r) works. */
1825 if (nios2_mode
== NIOS2_MODE_TEST
1826 && reloc
->reloc_expression
.X_op
== O_constant
)
1827 value
= reloc
->reloc_expression
.X_add_number
;
1829 return (unsigned long) value
;
1832 /* Encode a 3-bit register number, giving an error if this is not possible. */
1834 nios2_assemble_reg3 (const char *token
)
1836 struct nios2_reg
*reg
= nios2_parse_reg (token
, REG_3BIT
);
1842 for (j
= 0; j
< nios2_num_r2_reg3_mappings
; j
++)
1843 if (nios2_r2_reg3_mappings
[j
] == reg
->index
)
1846 /* Should never get here if we passed validation. */
1847 as_bad (_("invalid register %s"), token
);
1851 /* Argument assemble functions. */
1854 /* Control register index. */
1856 nios2_assemble_arg_c (const char *token
, nios2_insn_infoS
*insn
)
1858 struct nios2_reg
*reg
= nios2_parse_reg (token
, REG_CONTROL
);
1859 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1867 insn
->insn_code
|= SET_IW_R_IMM5 (reg
->index
);
1869 case iw_F3X6L5_type
:
1870 insn
->insn_code
|= SET_IW_F3X6L5_IMM5 (reg
->index
);
1877 /* Destination register. */
1879 nios2_assemble_arg_d (const char *token
, nios2_insn_infoS
*insn
)
1881 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1882 unsigned long regtype
= REG_NORMAL
;
1883 struct nios2_reg
*reg
;
1885 if (op
->format
== iw_custom_type
|| op
->format
== iw_F3X8_type
)
1886 regtype
|= REG_COPROCESSOR
;
1887 reg
= nios2_parse_reg (token
, regtype
);
1894 insn
->insn_code
|= SET_IW_R_C (reg
->index
);
1896 case iw_custom_type
:
1897 insn
->insn_code
|= SET_IW_CUSTOM_C (reg
->index
);
1898 if (reg
->regtype
& REG_COPROCESSOR
)
1899 insn
->insn_code
|= SET_IW_CUSTOM_READC (0);
1901 insn
->insn_code
|= SET_IW_CUSTOM_READC (1);
1903 case iw_F3X6L5_type
:
1905 insn
->insn_code
|= SET_IW_F3X6L5_C (reg
->index
);
1908 insn
->insn_code
|= SET_IW_F3X8_C (reg
->index
);
1909 if (reg
->regtype
& REG_COPROCESSOR
)
1910 insn
->insn_code
|= SET_IW_F3X8_READC (0);
1912 insn
->insn_code
|= SET_IW_F3X8_READC (1);
1915 insn
->insn_code
|= SET_IW_F2_B (reg
->index
);
1922 /* Source register 1. */
1924 nios2_assemble_arg_s (const char *token
, nios2_insn_infoS
*insn
)
1926 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1927 unsigned long regtype
= REG_NORMAL
;
1928 struct nios2_reg
*reg
;
1930 if (op
->format
== iw_custom_type
|| op
->format
== iw_F3X8_type
)
1931 regtype
|= REG_COPROCESSOR
;
1932 reg
= nios2_parse_reg (token
, regtype
);
1939 if (op
->match
== MATCH_R1_JMP
&& reg
->index
== 31)
1940 as_bad (_("r31 cannot be used with jmp; use ret instead"));
1941 insn
->insn_code
|= SET_IW_R_A (reg
->index
);
1944 insn
->insn_code
|= SET_IW_I_A (reg
->index
);
1946 case iw_custom_type
:
1947 insn
->insn_code
|= SET_IW_CUSTOM_A (reg
->index
);
1948 if (reg
->regtype
& REG_COPROCESSOR
)
1949 insn
->insn_code
|= SET_IW_CUSTOM_READA (0);
1951 insn
->insn_code
|= SET_IW_CUSTOM_READA (1);
1954 insn
->insn_code
|= SET_IW_F2I16_A (reg
->index
);
1956 case iw_F2X4I12_type
:
1957 insn
->insn_code
|= SET_IW_F2X4I12_A (reg
->index
);
1959 case iw_F1X4I12_type
:
1960 insn
->insn_code
|= SET_IW_F1X4I12_A (reg
->index
);
1962 case iw_F1X4L17_type
:
1963 insn
->insn_code
|= SET_IW_F1X4L17_A (reg
->index
);
1965 case iw_F3X6L5_type
:
1967 if (op
->match
== MATCH_R2_JMP
&& reg
->index
== 31)
1968 as_bad (_("r31 cannot be used with jmp; use ret instead"));
1969 insn
->insn_code
|= SET_IW_F3X6L5_A (reg
->index
);
1971 case iw_F2X6L10_type
:
1972 insn
->insn_code
|= SET_IW_F2X6L10_A (reg
->index
);
1975 insn
->insn_code
|= SET_IW_F3X8_A (reg
->index
);
1976 if (reg
->regtype
& REG_COPROCESSOR
)
1977 insn
->insn_code
|= SET_IW_F3X8_READA (0);
1979 insn
->insn_code
|= SET_IW_F3X8_READA (1);
1982 if (op
->match
== MATCH_R2_JMPR_N
&& reg
->index
== 31)
1983 as_bad (_("r31 cannot be used with jmpr.n; use ret.n instead"));
1984 insn
->insn_code
|= SET_IW_F1X1_A (reg
->index
);
1987 /* Implicit stack pointer reference. */
1988 if (reg
->index
!= 27)
1989 as_bad (_("invalid register %s"), token
);
1992 insn
->insn_code
|= SET_IW_F2_A (reg
->index
);
1999 /* Source register 2. */
2001 nios2_assemble_arg_t (const char *token
, nios2_insn_infoS
*insn
)
2003 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2004 unsigned long regtype
= REG_NORMAL
;
2005 struct nios2_reg
*reg
;
2007 if (op
->format
== iw_custom_type
|| op
->format
== iw_F3X8_type
)
2008 regtype
|= REG_COPROCESSOR
;
2009 reg
= nios2_parse_reg (token
, regtype
);
2016 insn
->insn_code
|= SET_IW_R_B (reg
->index
);
2019 insn
->insn_code
|= SET_IW_I_B (reg
->index
);
2021 case iw_custom_type
:
2022 insn
->insn_code
|= SET_IW_CUSTOM_B (reg
->index
);
2023 if (reg
->regtype
& REG_COPROCESSOR
)
2024 insn
->insn_code
|= SET_IW_CUSTOM_READB (0);
2026 insn
->insn_code
|= SET_IW_CUSTOM_READB (1);
2029 insn
->insn_code
|= SET_IW_F2I16_B (reg
->index
);
2031 case iw_F2X4I12_type
:
2032 insn
->insn_code
|= SET_IW_F2X4I12_B (reg
->index
);
2034 case iw_F3X6L5_type
:
2036 insn
->insn_code
|= SET_IW_F3X6L5_B (reg
->index
);
2038 case iw_F2X6L10_type
:
2039 insn
->insn_code
|= SET_IW_F2X6L10_B (reg
->index
);
2042 insn
->insn_code
|= SET_IW_F3X8_B (reg
->index
);
2043 if (reg
->regtype
& REG_COPROCESSOR
)
2044 insn
->insn_code
|= SET_IW_F3X8_READB (0);
2046 insn
->insn_code
|= SET_IW_F3X8_READB (1);
2049 insn
->insn_code
|= SET_IW_F1I5_B (reg
->index
);
2052 insn
->insn_code
|= SET_IW_F2_B (reg
->index
);
2054 case iw_T1X1I6_type
:
2055 /* Implicit zero register reference. */
2056 if (reg
->index
!= 0)
2057 as_bad (_("invalid register %s"), token
);
2065 /* Destination register w/3-bit encoding. */
2067 nios2_assemble_arg_D (const char *token
, nios2_insn_infoS
*insn
)
2069 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2070 int reg
= nios2_assemble_reg3 (token
);
2075 insn
->insn_code
|= SET_IW_T1I7_A3 (reg
);
2077 case iw_T2X1L3_type
:
2078 insn
->insn_code
|= SET_IW_T2X1L3_B3 (reg
);
2080 case iw_T2X1I3_type
:
2081 insn
->insn_code
|= SET_IW_T2X1I3_B3 (reg
);
2084 insn
->insn_code
|= SET_IW_T3X1_C3 (reg
);
2087 /* Some instructions using this encoding take 3 register arguments,
2088 requiring the destination register to be the same as the first
2090 if (op
->num_args
== 3)
2091 insn
->insn_code
|= SET_IW_T2X3_A3 (reg
);
2093 insn
->insn_code
|= SET_IW_T2X3_B3 (reg
);
2100 /* Source register w/3-bit encoding. */
2102 nios2_assemble_arg_S (const char *token
, nios2_insn_infoS
*insn
)
2104 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2105 int reg
= nios2_assemble_reg3 (token
);
2110 insn
->insn_code
|= SET_IW_T1I7_A3 (reg
);
2113 insn
->insn_code
|= SET_IW_T2I4_A3 (reg
);
2115 case iw_T2X1L3_type
:
2116 insn
->insn_code
|= SET_IW_T2X1L3_A3 (reg
);
2118 case iw_T2X1I3_type
:
2119 insn
->insn_code
|= SET_IW_T2X1I3_A3 (reg
);
2122 insn
->insn_code
|= SET_IW_T3X1_A3 (reg
);
2125 /* Some instructions using this encoding take 3 register arguments,
2126 requiring the destination register to be the same as the first
2128 if (op
->num_args
== 3)
2130 int dreg
= GET_IW_T2X3_A3 (insn
->insn_code
);
2132 as_bad ("source and destination registers must be the same");
2135 insn
->insn_code
|= SET_IW_T2X3_A3 (reg
);
2137 case iw_T1X1I6_type
:
2138 insn
->insn_code
|= SET_IW_T1X1I6_A3 (reg
);
2145 /* Source register 2 w/3-bit encoding. */
2147 nios2_assemble_arg_T (const char *token
, nios2_insn_infoS
*insn
)
2149 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2150 int reg
= nios2_assemble_reg3 (token
);
2155 insn
->insn_code
|= SET_IW_T2I4_B3 (reg
);
2158 insn
->insn_code
|= SET_IW_T3X1_B3 (reg
);
2161 insn
->insn_code
|= SET_IW_T2X3_B3 (reg
);
2168 /* 16-bit signed immediate. */
2170 nios2_assemble_arg_i (const char *token
, nios2_insn_infoS
*insn
)
2172 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2178 val
= nios2_assemble_expression (token
, insn
,
2179 BFD_RELOC_NIOS2_S16
, 0);
2180 insn
->constant_bits
|= SET_IW_I_IMM16 (val
);
2183 val
= nios2_assemble_expression (token
, insn
,
2184 BFD_RELOC_NIOS2_S16
, 0);
2185 insn
->constant_bits
|= SET_IW_F2I16_IMM16 (val
);
2192 /* 12-bit signed immediate. */
2194 nios2_assemble_arg_I (const char *token
, nios2_insn_infoS
*insn
)
2196 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2201 case iw_F2X4I12_type
:
2202 val
= nios2_assemble_expression (token
, insn
,
2203 BFD_RELOC_NIOS2_R2_S12
, 0);
2204 insn
->constant_bits
|= SET_IW_F2X4I12_IMM12 (val
);
2206 case iw_F1X4I12_type
:
2207 val
= nios2_assemble_expression (token
, insn
,
2208 BFD_RELOC_NIOS2_R2_S12
, 0);
2209 insn
->constant_bits
|= SET_IW_F2X4I12_IMM12 (val
);
2216 /* 16-bit unsigned immediate. */
2218 nios2_assemble_arg_u (const char *token
, nios2_insn_infoS
*insn
)
2220 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2226 val
= nios2_assemble_expression (token
, insn
,
2227 BFD_RELOC_NIOS2_U16
, 0);
2228 insn
->constant_bits
|= SET_IW_I_IMM16 (val
);
2231 val
= nios2_assemble_expression (token
, insn
,
2232 BFD_RELOC_NIOS2_U16
, 0);
2233 insn
->constant_bits
|= SET_IW_F2I16_IMM16 (val
);
2240 /* 7-bit unsigned immediate with 2-bit shift. */
2242 nios2_assemble_arg_U (const char *token
, nios2_insn_infoS
*insn
)
2244 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2250 val
= nios2_assemble_expression (token
, insn
,
2251 BFD_RELOC_NIOS2_R2_T1I7_2
, 0);
2252 insn
->constant_bits
|= SET_IW_T1I7_IMM7 (val
>> 2);
2255 val
= nios2_assemble_expression (token
, insn
,
2256 BFD_RELOC_NIOS2_R2_X1I7_2
, 0);
2257 insn
->constant_bits
|= SET_IW_X1I7_IMM7 (val
>> 2);
2264 /* 5-bit unsigned immediate with 2-bit shift. */
2266 nios2_assemble_arg_V (const char *token
, nios2_insn_infoS
*insn
)
2268 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2274 val
= nios2_assemble_expression (token
, insn
,
2275 BFD_RELOC_NIOS2_R2_F1I5_2
, 0);
2276 insn
->constant_bits
|= SET_IW_F1I5_IMM5 (val
>> 2);
2283 /* 4-bit unsigned immediate with 2-bit shift. */
2285 nios2_assemble_arg_W (const char *token
, nios2_insn_infoS
*insn
)
2287 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2293 val
= nios2_assemble_expression (token
, insn
,
2294 BFD_RELOC_NIOS2_R2_T2I4_2
, 0);
2295 insn
->constant_bits
|= SET_IW_T2I4_IMM4 (val
>> 2);
2297 case iw_L5I4X1_type
:
2298 /* This argument is optional for push.n/pop.n, and defaults to
2299 zero if unspecified. */
2303 val
= nios2_assemble_expression (token
, insn
,
2304 BFD_RELOC_NIOS2_R2_L5I4X1
, 0);
2305 insn
->constant_bits
|= SET_IW_L5I4X1_IMM4 (val
>> 2);
2312 /* 4-bit unsigned immediate with 1-bit shift. */
2314 nios2_assemble_arg_X (const char *token
, nios2_insn_infoS
*insn
)
2316 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2322 val
= nios2_assemble_expression (token
, insn
,
2323 BFD_RELOC_NIOS2_R2_T2I4_1
, 0);
2324 insn
->constant_bits
|= SET_IW_T2I4_IMM4 (val
>> 1);
2331 /* 4-bit unsigned immediate without shift. */
2333 nios2_assemble_arg_Y (const char *token
, nios2_insn_infoS
*insn
)
2335 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2341 val
= nios2_assemble_expression (token
, insn
,
2342 BFD_RELOC_NIOS2_R2_T2I4
, 0);
2343 insn
->constant_bits
|= SET_IW_T2I4_IMM4 (val
);
2351 /* 16-bit signed immediate address offset. */
2353 nios2_assemble_arg_o (const char *token
, nios2_insn_infoS
*insn
)
2355 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2361 val
= nios2_assemble_expression (token
, insn
,
2362 BFD_RELOC_16_PCREL
, 1);
2363 insn
->constant_bits
|= SET_IW_I_IMM16 (val
);
2366 val
= nios2_assemble_expression (token
, insn
,
2367 BFD_RELOC_16_PCREL
, 1);
2368 insn
->constant_bits
|= SET_IW_F2I16_IMM16 (val
);
2375 /* 10-bit signed address offset with 1-bit shift. */
2377 nios2_assemble_arg_O (const char *token
, nios2_insn_infoS
*insn
)
2379 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2385 val
= nios2_assemble_expression (token
, insn
,
2386 BFD_RELOC_NIOS2_R2_I10_1_PCREL
, 1);
2387 insn
->constant_bits
|= SET_IW_I10_IMM10 (val
>> 1);
2394 /* 7-bit signed address offset with 1-bit shift. */
2396 nios2_assemble_arg_P (const char *token
, nios2_insn_infoS
*insn
)
2398 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2404 val
= nios2_assemble_expression (token
, insn
,
2405 BFD_RELOC_NIOS2_R2_T1I7_1_PCREL
, 1);
2406 insn
->constant_bits
|= SET_IW_T1I7_IMM7 (val
>> 1);
2413 /* 5-bit unsigned immediate. */
2415 nios2_assemble_arg_j (const char *token
, nios2_insn_infoS
*insn
)
2417 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2423 val
= nios2_assemble_expression (token
, insn
,
2424 BFD_RELOC_NIOS2_IMM5
, 0);
2425 insn
->constant_bits
|= SET_IW_R_IMM5 (val
);
2427 case iw_F3X6L5_type
:
2428 if (op
->match
== MATCH_R2_ENI
)
2429 /* Value must be constant 0 or 1. */
2431 val
= nios2_assemble_expression (token
, insn
, BFD_RELOC_NONE
, 0);
2432 if (val
!= 0 && val
!= 1)
2433 as_bad ("invalid eni argument %u", val
);
2434 insn
->insn_code
|= SET_IW_F3X6L5_IMM5 (val
);
2438 val
= nios2_assemble_expression (token
, insn
,
2439 BFD_RELOC_NIOS2_IMM5
, 0);
2440 insn
->constant_bits
|= SET_IW_F3X6L5_IMM5 (val
);
2443 case iw_F2X6L10_type
:
2444 /* Only constant expression without relocation permitted for
2446 val
= nios2_assemble_expression (token
, insn
, BFD_RELOC_NONE
, 0);
2448 as_bad ("invalid bit position %u", val
);
2449 insn
->insn_code
|= SET_IW_F2X6L10_MSB (val
);
2452 val
= nios2_assemble_expression (token
, insn
,
2453 BFD_RELOC_NIOS2_R2_X2L5
, 0);
2454 insn
->constant_bits
|= SET_IW_X2L5_IMM5 (val
);
2461 /* Second 5-bit unsigned immediate field. */
2463 nios2_assemble_arg_k (const char *token
, nios2_insn_infoS
*insn
)
2465 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2470 case iw_F2X6L10_type
:
2471 /* Only constant expression without relocation permitted for
2473 val
= nios2_assemble_expression (token
, insn
,
2476 as_bad ("invalid bit position %u", val
);
2477 else if (GET_IW_F2X6L10_MSB (insn
->insn_code
) < val
)
2478 as_bad ("MSB must be greater than or equal to LSB");
2479 insn
->insn_code
|= SET_IW_F2X6L10_LSB (val
);
2486 /* 8-bit unsigned immediate. */
2488 nios2_assemble_arg_l (const char *token
, nios2_insn_infoS
*insn
)
2490 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2495 case iw_custom_type
:
2496 val
= nios2_assemble_expression (token
, insn
,
2497 BFD_RELOC_NIOS2_IMM8
, 0);
2498 insn
->constant_bits
|= SET_IW_CUSTOM_N (val
);
2501 val
= nios2_assemble_expression (token
, insn
,
2502 BFD_RELOC_NIOS2_IMM8
, 0);
2503 insn
->constant_bits
|= SET_IW_F3X8_N (val
);
2510 /* 26-bit unsigned immediate. */
2512 nios2_assemble_arg_m (const char *token
, nios2_insn_infoS
*insn
)
2514 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2520 val
= nios2_assemble_expression (token
, insn
,
2521 (nios2_as_options
.noat
2522 ? BFD_RELOC_NIOS2_CALL26_NOAT
2523 : BFD_RELOC_NIOS2_CALL26
),
2525 insn
->constant_bits
|= SET_IW_J_IMM26 (val
);
2528 val
= nios2_assemble_expression (token
, insn
,
2529 (nios2_as_options
.noat
2530 ? BFD_RELOC_NIOS2_CALL26_NOAT
2531 : BFD_RELOC_NIOS2_CALL26
),
2533 insn
->constant_bits
|= SET_IW_L26_IMM26 (val
);
2540 /* 6-bit unsigned immediate with no shifting. */
2542 nios2_assemble_arg_M (const char *token
, nios2_insn_infoS
*insn
)
2544 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2549 case iw_T1X1I6_type
:
2550 val
= nios2_assemble_expression (token
, insn
,
2551 BFD_RELOC_NIOS2_R2_T1X1I6
, 0);
2552 insn
->constant_bits
|= SET_IW_T1X1I6_IMM6 (val
);
2559 /* 6-bit unsigned immediate with 2-bit shift. */
2561 nios2_assemble_arg_N (const char *token
, nios2_insn_infoS
*insn
)
2563 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2568 case iw_T1X1I6_type
:
2569 val
= nios2_assemble_expression (token
, insn
,
2570 BFD_RELOC_NIOS2_R2_T1X1I6_2
, 0);
2571 insn
->constant_bits
|= SET_IW_T1X1I6_IMM6 (val
>> 2);
2579 /* Encoded enumeration for addi.n/subi.n. */
2581 nios2_assemble_arg_e (const char *token
, nios2_insn_infoS
*insn
)
2583 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2589 case iw_T2X1I3_type
:
2590 val
= nios2_assemble_expression (token
, insn
, BFD_RELOC_NONE
, 0);
2591 for (i
= 0; i
< nios2_num_r2_asi_n_mappings
; i
++)
2592 if (val
== nios2_r2_asi_n_mappings
[i
])
2594 if (i
== nios2_num_r2_asi_n_mappings
)
2596 as_bad (_("Invalid constant operand %s"), token
);
2599 insn
->insn_code
|= SET_IW_T2X1I3_IMM3 ((unsigned)i
);
2606 /* Encoded enumeration for slli.n/srli.n. */
2608 nios2_assemble_arg_f (const char *token
, nios2_insn_infoS
*insn
)
2610 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2616 case iw_T2X1L3_type
:
2617 val
= nios2_assemble_expression (token
, insn
, BFD_RELOC_NONE
, 0);
2618 for (i
= 0; i
< nios2_num_r2_shi_n_mappings
; i
++)
2619 if (val
== nios2_r2_shi_n_mappings
[i
])
2621 if (i
== nios2_num_r2_shi_n_mappings
)
2623 as_bad (_("Invalid constant operand %s"), token
);
2626 insn
->insn_code
|= SET_IW_T2X1L3_SHAMT ((unsigned)i
);
2633 /* Encoded enumeration for andi.n. */
2635 nios2_assemble_arg_g (const char *token
, nios2_insn_infoS
*insn
)
2637 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2644 val
= nios2_assemble_expression (token
, insn
, BFD_RELOC_NONE
, 0);
2645 for (i
= 0; i
< nios2_num_r2_andi_n_mappings
; i
++)
2646 if (val
== nios2_r2_andi_n_mappings
[i
])
2648 if (i
== nios2_num_r2_andi_n_mappings
)
2650 as_bad (_("Invalid constant operand %s"), token
);
2653 insn
->insn_code
|= SET_IW_T2I4_IMM4 ((unsigned)i
);
2660 /* Encoded enumeration for movi.n. */
2662 nios2_assemble_arg_h (const char *token
, nios2_insn_infoS
*insn
)
2664 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2671 val
= nios2_assemble_expression (token
, insn
, BFD_RELOC_NONE
, 0);
2673 if ((signed) i
== -1)
2679 else if (i
< 0 || i
> 125)
2681 as_bad (_("Invalid constant operand %s"), token
);
2684 insn
->insn_code
|= SET_IW_T1I7_IMM7 (val
);
2691 /* Encoded REGMASK for ldwm/stwm or push.n/pop.n. */
2693 nios2_assemble_arg_R (const char *token
, nios2_insn_infoS
*insn
)
2695 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2697 char *buf
= strdup (token
);
2698 unsigned long reglist
= nios2_parse_reglist (buf
, op
);
2706 case iw_F1X4L17_type
:
2707 /* Encoding for ldwm/stwm. */
2708 if (reglist
& 0x00003ffc)
2709 mask
= reglist
>> 2;
2712 insn
->insn_code
|= SET_IW_F1X4L17_RS (1);
2713 mask
= (reglist
& 0x00ffc000) >> 14;
2714 if (reglist
& (1 << 28))
2716 if (reglist
& (1 << 31))
2719 insn
->insn_code
|= SET_IW_F1X4L17_REGMASK (mask
);
2722 case iw_L5I4X1_type
:
2723 /* Encoding for push.n/pop.n. */
2724 if (reglist
& (1 << 28))
2725 insn
->insn_code
|= SET_IW_L5I4X1_FP (1);
2726 mask
= reglist
& 0x00ff0000;
2731 for (i
= 0; i
< nios2_num_r2_reg_range_mappings
; i
++)
2732 if (nios2_r2_reg_range_mappings
[i
] == mask
)
2734 if (i
== nios2_num_r2_reg_range_mappings
)
2736 as_bad ("invalid reglist");
2739 insn
->insn_code
|= SET_IW_L5I4X1_REGRANGE (i
);
2740 insn
->insn_code
|= SET_IW_L5I4X1_CS (1);
2749 /* Base register for ldwm/stwm. */
2751 nios2_assemble_arg_B (const char *token
, nios2_insn_infoS
*insn
)
2753 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2754 int direction
, writeback
, ret
;
2755 char *str
= strdup (token
);
2756 struct nios2_reg
*reg
2757 = nios2_parse_base_register (str
, &direction
, &writeback
, &ret
);
2765 case iw_F1X4L17_type
:
2766 /* For ldwm, check to see if the base register is already inside the
2768 if (op
->match
== MATCH_R2_LDWM
2769 && (nios2_reglist_mask
& (1 << reg
->index
)))
2771 as_bad ("invalid base register; %s is inside the reglist", reg
->name
);
2775 /* For stwm, ret option is not allowed. */
2776 if (op
->match
== MATCH_R2_STWM
&& ret
)
2778 as_bad ("invalid option syntax");
2782 /* Check that the direction matches the ordering of the reglist. */
2783 if (nios2_reglist_dir
&& direction
!= nios2_reglist_dir
)
2785 as_bad ("reglist order does not match increment/decrement mode");
2789 insn
->insn_code
|= SET_IW_F1X4L17_A (reg
->index
);
2791 insn
->insn_code
|= SET_IW_F1X4L17_ID (1);
2793 insn
->insn_code
|= SET_IW_F1X4L17_WB (1);
2795 insn
->insn_code
|= SET_IW_F1X4L17_PC (1);
2804 nios2_assemble_args (nios2_insn_infoS
*insn
)
2806 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2808 unsigned int tokidx
, ntok
;
2810 /* Make sure there are enough arguments. */
2811 ntok
= (op
->pinfo
& NIOS2_INSN_OPTARG
) ? op
->num_args
- 1 : op
->num_args
;
2812 for (tokidx
= 1; tokidx
<= ntok
; tokidx
++)
2813 if (insn
->insn_tokens
[tokidx
] == NULL
)
2815 as_bad ("missing argument");
2819 for (argptr
= op
->args
, tokidx
= 1;
2820 *argptr
&& insn
->insn_tokens
[tokidx
];
2830 nios2_assemble_arg_c (insn
->insn_tokens
[tokidx
++], insn
);
2834 nios2_assemble_arg_d (insn
->insn_tokens
[tokidx
++], insn
);
2838 nios2_assemble_arg_s (insn
->insn_tokens
[tokidx
++], insn
);
2842 nios2_assemble_arg_t (insn
->insn_tokens
[tokidx
++], insn
);
2846 nios2_assemble_arg_D (insn
->insn_tokens
[tokidx
++], insn
);
2850 nios2_assemble_arg_S (insn
->insn_tokens
[tokidx
++], insn
);
2854 nios2_assemble_arg_T (insn
->insn_tokens
[tokidx
++], insn
);
2858 nios2_assemble_arg_i (insn
->insn_tokens
[tokidx
++], insn
);
2862 nios2_assemble_arg_I (insn
->insn_tokens
[tokidx
++], insn
);
2866 nios2_assemble_arg_u (insn
->insn_tokens
[tokidx
++], insn
);
2870 nios2_assemble_arg_U (insn
->insn_tokens
[tokidx
++], insn
);
2874 nios2_assemble_arg_V (insn
->insn_tokens
[tokidx
++], insn
);
2878 nios2_assemble_arg_W (insn
->insn_tokens
[tokidx
++], insn
);
2882 nios2_assemble_arg_X (insn
->insn_tokens
[tokidx
++], insn
);
2886 nios2_assemble_arg_Y (insn
->insn_tokens
[tokidx
++], insn
);
2890 nios2_assemble_arg_o (insn
->insn_tokens
[tokidx
++], insn
);
2894 nios2_assemble_arg_O (insn
->insn_tokens
[tokidx
++], insn
);
2898 nios2_assemble_arg_P (insn
->insn_tokens
[tokidx
++], insn
);
2902 nios2_assemble_arg_j (insn
->insn_tokens
[tokidx
++], insn
);
2906 nios2_assemble_arg_k (insn
->insn_tokens
[tokidx
++], insn
);
2910 nios2_assemble_arg_l (insn
->insn_tokens
[tokidx
++], insn
);
2914 nios2_assemble_arg_m (insn
->insn_tokens
[tokidx
++], insn
);
2918 nios2_assemble_arg_M (insn
->insn_tokens
[tokidx
++], insn
);
2922 nios2_assemble_arg_N (insn
->insn_tokens
[tokidx
++], insn
);
2926 nios2_assemble_arg_e (insn
->insn_tokens
[tokidx
++], insn
);
2930 nios2_assemble_arg_f (insn
->insn_tokens
[tokidx
++], insn
);
2934 nios2_assemble_arg_g (insn
->insn_tokens
[tokidx
++], insn
);
2938 nios2_assemble_arg_h (insn
->insn_tokens
[tokidx
++], insn
);
2942 nios2_assemble_arg_R (insn
->insn_tokens
[tokidx
++], insn
);
2946 nios2_assemble_arg_B (insn
->insn_tokens
[tokidx
++], insn
);
2954 /* Perform argument checking. */
2955 nios2_check_assembly (insn
->insn_code
| insn
->constant_bits
,
2956 insn
->insn_tokens
[tokidx
]);
2960 /* The function consume_arg takes a pointer into a string
2961 of instruction tokens (args) and a pointer into a string
2962 representing the expected sequence of tokens and separators.
2963 It checks whether the first argument in argstr is of the
2964 expected type, throwing an error if it is not, and returns
2965 the pointer argstr. */
2967 nios2_consume_arg (char *argstr
, const char *parsestr
)
2986 if (nios2_special_relocation_p (argstr
))
2988 /* We zap the parentheses because we don't want them confused
2990 temp
= strchr (argstr
, '(');
2993 temp
= strchr (argstr
, ')');
2998 as_bad (_("badly formed expression near %s"), argstr
);
3020 /* We can't have %hi, %lo or %hiadj here. */
3022 as_bad (_("badly formed expression near %s"), argstr
);
3026 /* Register list for ldwm/stwm or push.n/pop.n. Replace the commas
3027 in the list with spaces so we don't confuse them with separators. */
3030 as_bad ("missing '{' in register list");
3033 for (temp
= argstr
+ 1; *temp
; temp
++)
3037 else if (*temp
== ',')
3042 as_bad ("missing '}' in register list");
3048 /* Base register and options for ldwm/stwm. This is the final argument
3049 and consumes the rest of the argument string; replace commas
3050 with spaces so that the token splitter doesn't think they are
3051 separate arguments. */
3052 for (temp
= argstr
; *temp
; temp
++)
3061 BAD_CASE (*parsestr
);
3068 /* The function consume_separator takes a pointer into a string
3069 of instruction tokens (args) and a pointer into a string representing
3070 the expected sequence of tokens and separators. It finds the first
3071 instance of the character pointed to by separator in argstr, and
3072 returns a pointer to the next element of argstr, which is the
3073 following token in the sequence. */
3075 nios2_consume_separator (char *argstr
, const char *separator
)
3079 /* If we have a opcode reg, expr(reg) type instruction, and
3080 * we are separating the expr from the (reg), we find the last
3081 * (, just in case the expression has parentheses. */
3083 if (*separator
== '(')
3084 p
= strrchr (argstr
, *separator
);
3086 p
= strchr (argstr
, *separator
);
3093 /* The principal argument parsing function which takes a string argstr
3094 representing the instruction arguments for insn, and extracts the argument
3095 tokens matching parsestr into parsed_args. */
3097 nios2_parse_args (nios2_insn_infoS
*insn
, char *argstr
,
3098 const char *parsestr
, char **parsed_args
)
3105 bfd_boolean terminate
= FALSE
;
3107 /* This rest of this function is it too fragile and it mostly works,
3108 therefore special case this one. */
3109 if (*parsestr
== 0 && argstr
!= 0)
3111 as_bad (_("too many arguments"));
3112 parsed_args
[0] = NULL
;
3116 while (p
!= NULL
&& !terminate
&& i
< NIOS2_MAX_INSN_TOKENS
)
3118 parsed_args
[i
] = nios2_consume_arg (p
, parsestr
);
3120 while (*parsestr
== '(' || *parsestr
== ')' || *parsestr
== ',')
3123 p
= nios2_consume_separator (p
, parsestr
);
3124 /* Check for missing separators. */
3125 if (!p
&& !(insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_OPTARG
))
3127 as_bad (_("expecting %c near %s"), *parsestr
, context
);
3133 if (*parsestr
== '\0')
3135 /* Check that the argument string has no trailing arguments. */
3136 end
= strpbrk (p
, ",");
3138 as_bad (_("too many arguments"));
3141 if (*parsestr
== '\0' || (p
!= NULL
&& *p
== '\0'))
3146 parsed_args
[i
] = NULL
;
3151 /** Support for pseudo-op parsing. These are macro-like opcodes that
3152 expand into real insns by suitable fiddling with the operands. */
3154 /* Append the string modifier to the string contained in the argument at
3155 parsed_args[ndx]. */
3157 nios2_modify_arg (char **parsed_args
, const char *modifier
,
3158 int unused ATTRIBUTE_UNUSED
, int ndx
)
3160 char *tmp
= parsed_args
[ndx
];
3163 = (char *) malloc (strlen (parsed_args
[ndx
]) + strlen (modifier
) + 1);
3164 strcpy (parsed_args
[ndx
], tmp
);
3165 strcat (parsed_args
[ndx
], modifier
);
3168 /* Modify parsed_args[ndx] by negating that argument. */
3170 nios2_negate_arg (char **parsed_args
, const char *modifier ATTRIBUTE_UNUSED
,
3171 int unused ATTRIBUTE_UNUSED
, int ndx
)
3173 char *tmp
= parsed_args
[ndx
];
3176 = (char *) malloc (strlen ("~(") + strlen (parsed_args
[ndx
]) +
3177 strlen (")+1") + 1);
3179 strcpy (parsed_args
[ndx
], "~(");
3180 strcat (parsed_args
[ndx
], tmp
);
3181 strcat (parsed_args
[ndx
], ")+1");
3184 /* The function nios2_swap_args swaps the pointers at indices index_1 and
3185 index_2 in the array parsed_args[] - this is used for operand swapping
3186 for comparison operations. */
3188 nios2_swap_args (char **parsed_args
, const char *unused ATTRIBUTE_UNUSED
,
3189 int index_1
, int index_2
)
3192 gas_assert (index_1
< NIOS2_MAX_INSN_TOKENS
3193 && index_2
< NIOS2_MAX_INSN_TOKENS
);
3194 tmp
= parsed_args
[index_1
];
3195 parsed_args
[index_1
] = parsed_args
[index_2
];
3196 parsed_args
[index_2
] = tmp
;
3199 /* This function appends the string appnd to the array of strings in
3200 parsed_args num times starting at index start in the array. */
3202 nios2_append_arg (char **parsed_args
, const char *appnd
, int num
,
3208 gas_assert ((start
+ num
) < NIOS2_MAX_INSN_TOKENS
);
3210 if (nios2_mode
== NIOS2_MODE_TEST
)
3211 tmp
= parsed_args
[start
];
3215 for (i
= start
, count
= num
; count
> 0; ++i
, --count
)
3216 parsed_args
[i
] = (char *) appnd
;
3218 gas_assert (i
== (start
+ num
));
3219 parsed_args
[i
] = tmp
;
3220 parsed_args
[i
+ 1] = NULL
;
3223 /* This function inserts the string insert num times in the array
3224 parsed_args, starting at the index start. */
3226 nios2_insert_arg (char **parsed_args
, const char *insert
, int num
,
3231 gas_assert ((start
+ num
) < NIOS2_MAX_INSN_TOKENS
);
3233 /* Move the existing arguments up to create space. */
3234 for (i
= NIOS2_MAX_INSN_TOKENS
; i
- num
>= start
; --i
)
3235 parsed_args
[i
] = parsed_args
[i
- num
];
3237 for (i
= start
, count
= num
; count
> 0; ++i
, --count
)
3238 parsed_args
[i
] = (char *) insert
;
3241 /* Cleanup function to free malloc'ed arg strings. */
3243 nios2_free_arg (char **parsed_args
, int num ATTRIBUTE_UNUSED
, int start
)
3245 if (parsed_args
[start
])
3247 free (parsed_args
[start
]);
3248 parsed_args
[start
] = NULL
;
3252 /* This function swaps the pseudo-op for a real op. */
3253 static nios2_ps_insn_infoS
*
3254 nios2_translate_pseudo_insn (nios2_insn_infoS
*insn
)
3257 nios2_ps_insn_infoS
*ps_insn
;
3259 /* Find which real insn the pseudo-op transates to and
3260 switch the insn_info ptr to point to it. */
3261 ps_insn
= nios2_ps_lookup (insn
->insn_nios2_opcode
->name
);
3263 if (ps_insn
!= NULL
)
3265 insn
->insn_nios2_opcode
= nios2_opcode_lookup (ps_insn
->insn
);
3266 insn
->insn_tokens
[0] = insn
->insn_nios2_opcode
->name
;
3267 /* Modify the args so they work with the real insn. */
3268 ps_insn
->arg_modifer_func ((char **) insn
->insn_tokens
,
3269 ps_insn
->arg_modifier
, ps_insn
->num
,
3273 /* we cannot recover from this. */
3274 as_fatal (_("unrecognized pseudo-instruction %s"),
3275 insn
->insn_nios2_opcode
->name
);
3279 /* Invoke the cleanup handler for pseudo-insn ps_insn on insn. */
3281 nios2_cleanup_pseudo_insn (nios2_insn_infoS
*insn
,
3282 nios2_ps_insn_infoS
*ps_insn
)
3284 if (ps_insn
->arg_cleanup_func
)
3285 (ps_insn
->arg_cleanup_func
) ((char **) insn
->insn_tokens
,
3286 ps_insn
->num
, ps_insn
->index
);
3289 const nios2_ps_insn_infoS nios2_ps_insn_info_structs
[] = {
3290 /* pseudo-op, real-op, arg, arg_modifier_func, num, index, arg_cleanup_func */
3291 {"mov", "add", nios2_append_arg
, "zero", 1, 3, NULL
},
3292 {"movi", "addi", nios2_insert_arg
, "zero", 1, 2, NULL
},
3293 {"movhi", "orhi", nios2_insert_arg
, "zero", 1, 2, NULL
},
3294 {"movui", "ori", nios2_insert_arg
, "zero", 1, 2, NULL
},
3295 {"movia", "orhi", nios2_insert_arg
, "zero", 1, 2, NULL
},
3296 {"nop", "add", nios2_append_arg
, "zero", 3, 1, NULL
},
3297 {"bgt", "blt", nios2_swap_args
, "", 1, 2, NULL
},
3298 {"bgtu", "bltu", nios2_swap_args
, "", 1, 2, NULL
},
3299 {"ble", "bge", nios2_swap_args
, "", 1, 2, NULL
},
3300 {"bleu", "bgeu", nios2_swap_args
, "", 1, 2, NULL
},
3301 {"cmpgt", "cmplt", nios2_swap_args
, "", 2, 3, NULL
},
3302 {"cmpgtu", "cmpltu", nios2_swap_args
, "", 2, 3, NULL
},
3303 {"cmple", "cmpge", nios2_swap_args
, "", 2, 3, NULL
},
3304 {"cmpleu", "cmpgeu", nios2_swap_args
, "", 2, 3, NULL
},
3305 {"cmpgti", "cmpgei", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
3306 {"cmpgtui", "cmpgeui", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
3307 {"cmplei", "cmplti", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
3308 {"cmpleui", "cmpltui", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
3309 {"subi", "addi", nios2_negate_arg
, "", 0, 3, nios2_free_arg
},
3310 {"nop.n", "mov.n", nios2_append_arg
, "zero", 2, 1, NULL
}
3311 /* Add further pseudo-ops here. */
3314 #define NIOS2_NUM_PSEUDO_INSNS \
3315 ((sizeof(nios2_ps_insn_info_structs)/ \
3316 sizeof(nios2_ps_insn_info_structs[0])))
3317 const int nios2_num_ps_insn_info_structs
= NIOS2_NUM_PSEUDO_INSNS
;
3320 /** Assembler output support. */
3322 /* Output a normal instruction. */
3324 output_insn (nios2_insn_infoS
*insn
)
3327 nios2_insn_relocS
*reloc
;
3328 f
= frag_more (insn
->insn_nios2_opcode
->size
);
3329 /* This allocates enough space for the instruction
3330 and puts it in the current frag. */
3331 md_number_to_chars (f
, insn
->insn_code
, insn
->insn_nios2_opcode
->size
);
3332 /* Emit debug info. */
3333 dwarf2_emit_insn (insn
->insn_nios2_opcode
->size
);
3334 /* Create any fixups to be acted on later. */
3336 for (reloc
= insn
->insn_reloc
; reloc
!= NULL
; reloc
= reloc
->reloc_next
)
3337 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
,
3338 insn
->insn_nios2_opcode
->size
,
3339 &reloc
->reloc_expression
, reloc
->reloc_pcrel
,
3343 /* Output an unconditional branch. */
3345 output_ubranch (nios2_insn_infoS
*insn
)
3347 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
3349 /* If the reloc is NULL, there was an error assembling the branch. */
3352 symbolS
*symp
= reloc
->reloc_expression
.X_add_symbol
;
3353 offsetT offset
= reloc
->reloc_expression
.X_add_number
;
3355 bfd_boolean is_cdx
= (insn
->insn_nios2_opcode
->size
== 2);
3357 /* Tag dwarf2 debug info to the address at the start of the insn.
3358 We must do it before frag_var() below closes off the frag. */
3359 dwarf2_emit_insn (0);
3361 /* We create a machine dependent frag which can grow
3362 to accommodate the largest possible instruction sequence
3363 this may generate. */
3364 f
= frag_var (rs_machine_dependent
,
3365 UBRANCH_MAX_SIZE
, insn
->insn_nios2_opcode
->size
,
3366 (is_cdx
? CDX_UBRANCH_SUBTYPE (0) : UBRANCH_SUBTYPE (0)),
3367 symp
, offset
, NULL
);
3369 md_number_to_chars (f
, insn
->insn_code
, insn
->insn_nios2_opcode
->size
);
3371 /* We leave fixup generation to md_convert_frag. */
3375 /* Output a conditional branch. */
3377 output_cbranch (nios2_insn_infoS
*insn
)
3379 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
3381 /* If the reloc is NULL, there was an error assembling the branch. */
3384 symbolS
*symp
= reloc
->reloc_expression
.X_add_symbol
;
3385 offsetT offset
= reloc
->reloc_expression
.X_add_number
;
3387 bfd_boolean is_cdx
= (insn
->insn_nios2_opcode
->size
== 2);
3389 /* Tag dwarf2 debug info to the address at the start of the insn.
3390 We must do it before frag_var() below closes off the frag. */
3391 dwarf2_emit_insn (0);
3393 /* We create a machine dependent frag which can grow
3394 to accommodate the largest possible instruction sequence
3395 this may generate. */
3396 f
= frag_var (rs_machine_dependent
,
3397 CBRANCH_MAX_SIZE
, insn
->insn_nios2_opcode
->size
,
3398 (is_cdx
? CDX_CBRANCH_SUBTYPE (0) : CBRANCH_SUBTYPE (0)),
3399 symp
, offset
, NULL
);
3401 md_number_to_chars (f
, insn
->insn_code
, insn
->insn_nios2_opcode
->size
);
3403 /* We leave fixup generation to md_convert_frag. */
3407 /* Output a call sequence. Since calls are not pc-relative for NIOS2,
3408 but are page-relative, we cannot tell at any stage in assembly
3409 whether a call will be out of range since a section may be linked
3410 at any address. So if we are relaxing, we convert all call instructions
3411 to long call sequences, and rely on the linker to relax them back to
3414 output_call (nios2_insn_infoS
*insn
)
3416 /* This allocates enough space for the instruction
3417 and puts it in the current frag. */
3418 char *f
= frag_more (12);
3419 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
3420 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
3425 md_number_to_chars (f
,
3426 (MATCH_R1_ORHI
| SET_IW_I_B (AT_REGNUM
)
3429 dwarf2_emit_insn (4);
3430 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
, 4,
3431 &reloc
->reloc_expression
, 0, BFD_RELOC_NIOS2_HI16
);
3432 md_number_to_chars (f
+ 4,
3433 (MATCH_R1_ORI
| SET_IW_I_B (AT_REGNUM
)
3434 | SET_IW_I_A (AT_REGNUM
)),
3436 dwarf2_emit_insn (4);
3437 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
+ 4, 4,
3438 &reloc
->reloc_expression
, 0, BFD_RELOC_NIOS2_LO16
);
3439 md_number_to_chars (f
+ 8, MATCH_R1_CALLR
| SET_IW_R_A (AT_REGNUM
), 4);
3440 dwarf2_emit_insn (4);
3443 md_number_to_chars (f
,
3444 (MATCH_R2_ORHI
| SET_IW_F2I16_B (AT_REGNUM
)
3445 | SET_IW_F2I16_A (0)),
3447 dwarf2_emit_insn (4);
3448 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
, 4,
3449 &reloc
->reloc_expression
, 0, BFD_RELOC_NIOS2_HI16
);
3450 md_number_to_chars (f
+ 4,
3451 (MATCH_R2_ORI
| SET_IW_F2I16_B (AT_REGNUM
)
3452 | SET_IW_F2I16_A (AT_REGNUM
)),
3454 dwarf2_emit_insn (4);
3455 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
+ 4, 4,
3456 &reloc
->reloc_expression
, 0, BFD_RELOC_NIOS2_LO16
);
3457 md_number_to_chars (f
+ 8, MATCH_R2_CALLR
| SET_IW_F3X6L5_A (AT_REGNUM
),
3459 dwarf2_emit_insn (4);
3466 /* Output a movhi/addi pair for the movia pseudo-op. */
3468 output_movia (nios2_insn_infoS
*insn
)
3470 /* This allocates enough space for the instruction
3471 and puts it in the current frag. */
3472 char *f
= frag_more (8);
3473 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
3474 unsigned long reg
, code
= 0;
3475 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
3477 /* If the reloc is NULL, there was an error assembling the movia. */
3483 reg
= GET_IW_I_B (insn
->insn_code
);
3484 code
= MATCH_R1_ADDI
| SET_IW_I_A (reg
) | SET_IW_I_B (reg
);
3487 reg
= GET_IW_F2I16_B (insn
->insn_code
);
3488 code
= MATCH_R2_ADDI
| SET_IW_F2I16_A (reg
) | SET_IW_F2I16_B (reg
);
3494 md_number_to_chars (f
, insn
->insn_code
, 4);
3495 dwarf2_emit_insn (4);
3496 fix_new (frag_now
, f
- frag_now
->fr_literal
, 4,
3497 reloc
->reloc_expression
.X_add_symbol
,
3498 reloc
->reloc_expression
.X_add_number
, 0,
3499 BFD_RELOC_NIOS2_HIADJ16
);
3500 md_number_to_chars (f
+ 4, code
, 4);
3501 dwarf2_emit_insn (4);
3502 fix_new (frag_now
, f
+ 4 - frag_now
->fr_literal
, 4,
3503 reloc
->reloc_expression
.X_add_symbol
,
3504 reloc
->reloc_expression
.X_add_number
, 0, BFD_RELOC_NIOS2_LO16
);
3510 /** External interfaces. */
3512 /* Update the selected architecture based on ARCH, giving an error if
3513 ARCH is an invalid value. */
3516 nios2_use_arch (const char *arch
)
3518 if (strcmp (arch
, "nios2") == 0 || strcmp (arch
, "r1") == 0)
3520 nios2_architecture
|= EF_NIOS2_ARCH_R1
;
3521 nios2_opcodes
= (struct nios2_opcode
*) nios2_r1_opcodes
;
3522 nios2_num_opcodes
= nios2_num_r1_opcodes
;
3527 else if (strcmp (arch
, "r2") == 0)
3529 nios2_architecture
|= EF_NIOS2_ARCH_R2
;
3530 nios2_opcodes
= (struct nios2_opcode
*) nios2_r2_opcodes
;
3531 nios2_num_opcodes
= nios2_num_r2_opcodes
;
3537 as_bad (_("unknown architecture '%s'"), arch
);
3540 /* The following functions are called by machine-independent parts of
3543 md_parse_option (int c
, const char *arg ATTRIBUTE_UNUSED
)
3548 /* Hidden option for self-test mode. */
3549 nios2_mode
= NIOS2_MODE_TEST
;
3551 case OPTION_RELAX_ALL
:
3552 nios2_as_options
.relax
= relax_all
;
3554 case OPTION_NORELAX
:
3555 nios2_as_options
.relax
= relax_none
;
3557 case OPTION_RELAX_SECTION
:
3558 nios2_as_options
.relax
= relax_section
;
3561 target_big_endian
= 1;
3564 target_big_endian
= 0;
3567 nios2_use_arch (arg
);
3577 /* Implement TARGET_FORMAT. We can choose to be big-endian or
3578 little-endian at runtime based on a switch. */
3580 nios2_target_format (void)
3582 return target_big_endian
? "elf32-bignios2" : "elf32-littlenios2";
3585 /* Machine-dependent usage message. */
3587 md_show_usage (FILE *stream
)
3589 fprintf (stream
, " NIOS2 options:\n"
3590 " -relax-all replace all branch and call "
3591 "instructions with jmp and callr sequences\n"
3592 " -relax-section replace identified out of range "
3593 "branches with jmp sequences (default)\n"
3594 " -no-relax do not replace any branches or calls\n"
3595 " -EB force big-endian byte ordering\n"
3596 " -EL force little-endian byte ordering\n"
3597 " -march=ARCH enable instructions from architecture ARCH\n");
3601 /* This function is called once, at assembler startup time.
3602 It should set up all the tables, etc. that the MD part of the
3603 assembler will need. */
3608 const char *inserted
;
3610 switch (nios2_architecture
)
3613 case EF_NIOS2_ARCH_R1
:
3614 bfd_default_set_arch_mach (stdoutput
, bfd_arch_nios2
, bfd_mach_nios2r1
);
3616 case EF_NIOS2_ARCH_R2
:
3617 if (target_big_endian
)
3618 as_fatal (_("Big-endian R2 is not supported."));
3619 bfd_default_set_arch_mach (stdoutput
, bfd_arch_nios2
, bfd_mach_nios2r2
);
3623 /* Create and fill a hashtable for the Nios II opcodes, registers and
3625 nios2_opcode_hash
= hash_new ();
3626 nios2_reg_hash
= hash_new ();
3627 nios2_ps_hash
= hash_new ();
3629 for (i
= 0; i
< nios2_num_opcodes
; ++i
)
3632 = hash_insert (nios2_opcode_hash
, nios2_opcodes
[i
].name
,
3633 (PTR
) & nios2_opcodes
[i
]);
3634 if (inserted
!= NULL
)
3636 fprintf (stderr
, _("internal error: can't hash `%s': %s\n"),
3637 nios2_opcodes
[i
].name
, inserted
);
3638 /* Probably a memory allocation problem? Give up now. */
3639 as_fatal (_("Broken assembler. No assembly attempted."));
3643 for (i
= 0; i
< nios2_num_regs
; ++i
)
3646 = hash_insert (nios2_reg_hash
, nios2_regs
[i
].name
,
3647 (PTR
) & nios2_regs
[i
]);
3648 if (inserted
!= NULL
)
3650 fprintf (stderr
, _("internal error: can't hash `%s': %s\n"),
3651 nios2_regs
[i
].name
, inserted
);
3652 /* Probably a memory allocation problem? Give up now. */
3653 as_fatal (_("Broken assembler. No assembly attempted."));
3658 for (i
= 0; i
< nios2_num_ps_insn_info_structs
; ++i
)
3661 = hash_insert (nios2_ps_hash
, nios2_ps_insn_info_structs
[i
].pseudo_insn
,
3662 (PTR
) & nios2_ps_insn_info_structs
[i
]);
3663 if (inserted
!= NULL
)
3665 fprintf (stderr
, _("internal error: can't hash `%s': %s\n"),
3666 nios2_ps_insn_info_structs
[i
].pseudo_insn
, inserted
);
3667 /* Probably a memory allocation problem? Give up now. */
3668 as_fatal (_("Broken assembler. No assembly attempted."));
3672 /* Assembler option defaults. */
3673 nios2_as_options
.noat
= FALSE
;
3674 nios2_as_options
.nobreak
= FALSE
;
3676 /* Debug information is incompatible with relaxation. */
3677 if (debug_type
!= DEBUG_UNSPECIFIED
)
3678 nios2_as_options
.relax
= relax_none
;
3680 /* Initialize the alignment data. */
3681 nios2_current_align_seg
= now_seg
;
3682 nios2_last_label
= NULL
;
3683 nios2_current_align
= 0;
3684 nios2_min_align
= 2;
3688 /* Assembles a single line of Nios II assembly language. */
3690 md_assemble (char *op_str
)
3693 char *op_strdup
= NULL
;
3694 unsigned long saved_pinfo
= 0;
3695 nios2_insn_infoS thisinsn
;
3696 nios2_insn_infoS
*insn
= &thisinsn
;
3698 /* Make sure we are aligned on an appropriate boundary. */
3699 if (nios2_current_align
< nios2_min_align
)
3700 nios2_align (nios2_min_align
, NULL
, nios2_last_label
);
3701 else if (nios2_current_align
> nios2_min_align
)
3702 nios2_current_align
= nios2_min_align
;
3703 nios2_last_label
= NULL
;
3705 /* We don't want to clobber to op_str
3706 because we want to be able to use it in messages. */
3707 op_strdup
= strdup (op_str
);
3708 insn
->insn_tokens
[0] = strtok (op_strdup
, " ");
3709 argstr
= strtok (NULL
, "");
3711 /* Assemble the opcode. */
3712 insn
->insn_nios2_opcode
= nios2_opcode_lookup (insn
->insn_tokens
[0]);
3713 insn
->insn_reloc
= NULL
;
3715 if (insn
->insn_nios2_opcode
!= NULL
)
3717 nios2_ps_insn_infoS
*ps_insn
= NULL
;
3719 /* Note if we've seen a 16-bit instruction. */
3720 if (insn
->insn_nios2_opcode
->size
== 2)
3721 nios2_min_align
= 1;
3723 /* Set the opcode for the instruction. */
3724 insn
->insn_code
= insn
->insn_nios2_opcode
->match
;
3725 insn
->constant_bits
= 0;
3727 /* Parse the arguments pointed to by argstr. */
3728 if (nios2_mode
== NIOS2_MODE_ASSEMBLE
)
3729 nios2_parse_args (insn
, argstr
, insn
->insn_nios2_opcode
->args
,
3730 (char **) &insn
->insn_tokens
[1]);
3732 nios2_parse_args (insn
, argstr
, insn
->insn_nios2_opcode
->args_test
,
3733 (char **) &insn
->insn_tokens
[1]);
3735 /* We need to preserve the MOVIA macro as this is clobbered by
3736 translate_pseudo_insn. */
3737 if (insn
->insn_nios2_opcode
->pinfo
== NIOS2_INSN_MACRO_MOVIA
)
3738 saved_pinfo
= NIOS2_INSN_MACRO_MOVIA
;
3739 /* If the instruction is an pseudo-instruction, we want to replace it
3740 with its real equivalent, and then continue. */
3741 if ((insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_MACRO
)
3742 == NIOS2_INSN_MACRO
)
3743 ps_insn
= nios2_translate_pseudo_insn (insn
);
3745 /* Assemble the parsed arguments into the instruction word. */
3746 nios2_assemble_args (insn
);
3748 /* Handle relaxation and other transformations. */
3749 if (nios2_as_options
.relax
!= relax_none
3750 && !nios2_as_options
.noat
3751 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_UBRANCH
)
3752 output_ubranch (insn
);
3753 else if (nios2_as_options
.relax
!= relax_none
3754 && !nios2_as_options
.noat
3755 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_CBRANCH
)
3756 output_cbranch (insn
);
3757 else if (nios2_as_options
.relax
== relax_all
3758 && !nios2_as_options
.noat
3759 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_CALL
3761 && ((insn
->insn_reloc
->reloc_type
3762 == BFD_RELOC_NIOS2_CALL26
)
3763 || (insn
->insn_reloc
->reloc_type
3764 == BFD_RELOC_NIOS2_CALL26_NOAT
)))
3766 else if (saved_pinfo
== NIOS2_INSN_MACRO_MOVIA
)
3767 output_movia (insn
);
3771 nios2_cleanup_pseudo_insn (insn
, ps_insn
);
3774 /* Unrecognised instruction - error. */
3775 as_bad (_("unrecognised instruction %s"), insn
->insn_tokens
[0]);
3777 /* Don't leak memory. */
3781 /* Round up section size. */
3783 md_section_align (asection
*seg ATTRIBUTE_UNUSED
, valueT size
)
3785 /* I think byte alignment is fine here. */
3789 /* Implement TC_FORCE_RELOCATION. */
3791 nios2_force_relocation (fixS
*fixp
)
3793 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
3794 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
3795 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_ALIGN
)
3798 return generic_force_reloc (fixp
);
3801 /* Implement tc_fix_adjustable. */
3803 nios2_fix_adjustable (fixS
*fixp
)
3805 if (fixp
->fx_addsy
== NULL
)
3809 /* Prevent all adjustments to global symbols. */
3810 if (OUTPUT_FLAVOR
== bfd_target_elf_flavour
3811 && (S_IS_EXTERNAL (fixp
->fx_addsy
) || S_IS_WEAK (fixp
->fx_addsy
)))
3814 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
3815 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
3818 /* Preserve relocations against symbols with function type. */
3819 if (symbol_get_bfdsym (fixp
->fx_addsy
)->flags
& BSF_FUNCTION
)
3822 /* Don't allow symbols to be discarded on GOT related relocs. */
3823 if (fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOT16
3824 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_CALL16
3825 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_LO
3826 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_HA
3827 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_GD16
3828 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDM16
3829 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDO16
3830 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_IE16
3831 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LE16
3832 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPMOD
3833 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPREL
3834 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_TPREL
3835 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF
3836 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOT_LO
3837 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOT_HA
3838 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_CALL_LO
3839 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_CALL_HA
3846 /* Implement tc_frob_symbol. This is called in adjust_reloc_syms;
3847 it is used to remove *ABS* references from the symbol table. */
3849 nios2_frob_symbol (symbolS
*symp
)
3851 if ((OUTPUT_FLAVOR
== bfd_target_elf_flavour
3852 && symp
== section_symbol (absolute_section
))
3853 || !S_IS_DEFINED (symp
))
3859 /* The function tc_gen_reloc creates a relocation structure for the
3860 fixup fixp, and returns a pointer to it. This structure is passed
3861 to bfd_install_relocation so that it can be written to the object
3862 file for linking. */
3864 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixp
)
3866 arelent
*reloc
= (arelent
*) xmalloc (sizeof (arelent
));
3867 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
3868 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
3870 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
3871 reloc
->addend
= fixp
->fx_offset
; /* fixp->fx_addnumber; */
3875 switch (fixp
->fx_r_type
)
3878 fixp
->fx_r_type
= BFD_RELOC_16_PCREL
;
3880 case BFD_RELOC_NIOS2_LO16
:
3881 fixp
->fx_r_type
= BFD_RELOC_NIOS2_PCREL_LO
;
3883 case BFD_RELOC_NIOS2_HIADJ16
:
3884 fixp
->fx_r_type
= BFD_RELOC_NIOS2_PCREL_HA
;
3891 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
3892 if (reloc
->howto
== NULL
)
3894 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
3895 _("can't represent relocation type %s"),
3896 bfd_get_reloc_code_name (fixp
->fx_r_type
));
3898 /* Set howto to a garbage value so that we can keep going. */
3899 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, BFD_RELOC_32
);
3900 gas_assert (reloc
->howto
!= NULL
);
3906 md_pcrel_from (fixS
*fixP ATTRIBUTE_UNUSED
)
3911 /* Called just before the assembler exits. */
3915 /* FIXME - not yet implemented */
3918 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
3919 Otherwise we have no need to default values of symbols. */
3921 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
3924 if (name
[0] == '_' && name
[1] == 'G'
3925 && strcmp (name
, GLOBAL_OFFSET_TABLE_NAME
) == 0)
3929 if (symbol_find (name
))
3930 as_bad ("GOT already in the symbol table");
3932 GOT_symbol
= symbol_new (name
, undefined_section
,
3933 (valueT
) 0, &zero_address_frag
);
3943 /* Implement tc_frob_label. */
3945 nios2_frob_label (symbolS
*lab
)
3947 /* Emit dwarf information. */
3948 dwarf2_emit_label (lab
);
3950 /* Update the label's address with the current output pointer. */
3951 symbol_set_frag (lab
, frag_now
);
3952 S_SET_VALUE (lab
, (valueT
) frag_now_fix ());
3954 /* Record this label for future adjustment after we find out what
3955 kind of data it references, and the required alignment therewith. */
3956 nios2_last_label
= lab
;
3959 /* Implement md_cons_align. */
3961 nios2_cons_align (int size
)
3964 const char *pfill
= NULL
;
3966 while ((size
>>= 1) != 0)
3969 if (subseg_text_p (now_seg
))
3970 pfill
= (const char *) nop32
;
3974 if (nios2_auto_align_on
)
3975 nios2_align (log_size
, pfill
, NULL
);
3977 nios2_last_label
= NULL
;
3980 /* Map 's' to SHF_NIOS2_GPREL. */
3981 /* This is from the Alpha code tc-alpha.c. */
3983 nios2_elf_section_letter (int letter
, char **ptr_msg
)
3986 return SHF_NIOS2_GPREL
;
3988 *ptr_msg
= _("Bad .section directive: want a,s,w,x,M,S,G,T in string");
3992 /* Map SHF_ALPHA_GPREL to SEC_SMALL_DATA. */
3993 /* This is from the Alpha code tc-alpha.c. */
3995 nios2_elf_section_flags (flagword flags
, int attr
, int type ATTRIBUTE_UNUSED
)
3997 if (attr
& SHF_NIOS2_GPREL
)
3998 flags
|= SEC_SMALL_DATA
;
4002 /* Implement TC_PARSE_CONS_EXPRESSION to handle %tls_ldo(...) */
4003 bfd_reloc_code_real_type
4004 nios2_cons (expressionS
*exp
, int size
)
4006 bfd_reloc_code_real_type nios2_tls_ldo_reloc
= BFD_RELOC_NONE
;
4009 if (input_line_pointer
[0] == '%')
4011 if (strprefix (input_line_pointer
+ 1, "tls_ldo"))
4014 as_bad (_("Illegal operands: %%tls_ldo in %d-byte data field"),
4018 input_line_pointer
+= 8;
4019 nios2_tls_ldo_reloc
= BFD_RELOC_NIOS2_TLS_DTPREL
;
4022 if (nios2_tls_ldo_reloc
!= BFD_RELOC_NONE
)
4025 if (input_line_pointer
[0] != '(')
4026 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
4030 char *end
= ++input_line_pointer
;
4033 for (c
= *end
; !is_end_of_line
[c
]; end
++, c
= *end
)
4044 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
4050 if (input_line_pointer
!= end
)
4051 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
4054 input_line_pointer
++;
4056 c
= *input_line_pointer
;
4057 if (! is_end_of_line
[c
] && c
!= ',')
4058 as_bad (_("Illegal operands: garbage after %%tls_ldo()"));
4064 if (nios2_tls_ldo_reloc
== BFD_RELOC_NONE
)
4066 return nios2_tls_ldo_reloc
;
4069 /* Implement HANDLE_ALIGN. */
4071 nios2_handle_align (fragS
*fragp
)
4073 /* If we are expecting to relax in the linker, then we must output a
4074 relocation to tell the linker we are aligning code. */
4075 if (nios2_as_options
.relax
== relax_all
4076 && (fragp
->fr_type
== rs_align
|| fragp
->fr_type
== rs_align_code
)
4077 && fragp
->fr_address
+ fragp
->fr_fix
> 0
4078 && fragp
->fr_offset
> 1
4079 && now_seg
!= bss_section
)
4080 fix_new (fragp
, fragp
->fr_fix
, 0, &abs_symbol
, fragp
->fr_offset
, 0,
4081 BFD_RELOC_NIOS2_ALIGN
);
4084 /* Implement tc_regname_to_dw2regnum, to convert REGNAME to a DWARF-2
4087 nios2_regname_to_dw2regnum (char *regname
)
4089 struct nios2_reg
*r
= nios2_reg_lookup (regname
);
4095 /* Implement tc_cfi_frame_initial_instructions, to initialize the DWARF-2
4096 unwind information for this procedure. */
4098 nios2_frame_initial_instructions (void)
4100 cfi_add_CFA_def_cfa (27, 0);
4104 /* Some special processing for a Nios II ELF file. */
4107 nios2_elf_final_processing (void)
4109 elf_elfheader (stdoutput
)->e_flags
= nios2_architecture
;