1 /* Altera Nios II assembler.
2 Copyright (C) 2012-2015 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
;
214 /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
218 /* The processor architecture value, EF_NIOS2_ARCH_R1 by default. */
219 static int nios2_architecture
= EF_NIOS2_ARCH_R1
;
222 /** Utility routines. */
223 /* Function md_chars_to_number takes the sequence of
224 bytes in buf and returns the corresponding value
225 in an int. n must be 1, 2 or 4. */
227 md_chars_to_number (char *buf
, int n
)
232 gas_assert (n
== 1 || n
== 2 || n
== 4);
235 if (target_big_endian
)
236 for (i
= 0; i
< n
; ++i
)
237 val
= val
| ((buf
[i
] & 0xff) << 8 * (n
- (i
+ 1)));
239 for (i
= 0; i
< n
; ++i
)
240 val
= val
| ((buf
[i
] & 0xff) << 8 * i
);
245 /* This function turns a C long int, short int or char
246 into the series of bytes that represent the number
247 on the target machine. */
249 md_number_to_chars (char *buf
, valueT val
, int n
)
251 gas_assert (n
== 1 || n
== 2 || n
== 4 || n
== 8);
252 if (target_big_endian
)
253 number_to_chars_bigendian (buf
, val
, n
);
255 number_to_chars_littleendian (buf
, val
, n
);
258 /* Turn a string in input_line_pointer into a floating point constant
259 of type TYPE, and store the appropriate bytes in *LITP. The number
260 of LITTLENUMS emitted is stored in *SIZEP. An error message is
261 returned, or NULL on OK. */
263 md_atof (int type
, char *litP
, int *sizeP
)
266 LITTLENUM_TYPE words
[4];
280 return _("bad call to md_atof");
283 t
= atof_ieee (input_line_pointer
, type
, words
);
285 input_line_pointer
= t
;
289 if (! target_big_endian
)
290 for (i
= prec
- 1; i
>= 0; i
--, litP
+= 2)
291 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
293 for (i
= 0; i
< prec
; i
++, litP
+= 2)
294 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
299 /* Return true if STR starts with PREFIX, which should be a string literal. */
300 #define strprefix(STR, PREFIX) \
301 (strncmp ((STR), PREFIX, strlen (PREFIX)) == 0)
304 /* Return true if STR is prefixed with a special relocation operator. */
306 nios2_special_relocation_p (const char *str
)
308 return (strprefix (str
, "%lo")
309 || strprefix (str
, "%hi")
310 || strprefix (str
, "%hiadj")
311 || strprefix (str
, "%gprel")
312 || strprefix (str
, "%got")
313 || strprefix (str
, "%call")
314 || strprefix (str
, "%gotoff_lo")
315 || strprefix (str
, "%gotoff_hiadj")
316 || strprefix (str
, "%tls_gd")
317 || strprefix (str
, "%tls_ldm")
318 || strprefix (str
, "%tls_ldo")
319 || strprefix (str
, "%tls_ie")
320 || strprefix (str
, "%tls_le")
321 || strprefix (str
, "%gotoff"));
325 /* nop fill pattern for text section. */
326 static char const nop
[4] = { 0x3a, 0x88, 0x01, 0x00 };
328 /* Handles all machine-dependent alignment needs. */
330 nios2_align (int log_size
, const char *pfill
, symbolS
*label
)
333 long max_alignment
= 15;
335 /* The front end is prone to changing segments out from under us
336 temporarily when -g is in effect. */
337 int switched_seg_p
= (nios2_current_align_seg
!= now_seg
);
340 if (align
> max_alignment
)
342 align
= max_alignment
;
343 as_bad (_("Alignment too large: %d. assumed"), align
);
347 as_warn (_("Alignment negative: 0 assumed"));
353 if (subseg_text_p (now_seg
) && align
>= 2)
355 /* First, make sure we're on a four-byte boundary, in case
356 someone has been putting .byte values the text section. */
357 if (nios2_current_align
< 2 || switched_seg_p
)
358 frag_align (2, 0, 0);
360 /* Now fill in the alignment pattern. */
362 frag_align_pattern (align
, pfill
, sizeof nop
, 0);
364 frag_align (align
, 0, 0);
367 frag_align (align
, 0, 0);
370 nios2_current_align
= align
;
372 /* If the last label was in a different section we can't align it. */
373 if (label
!= NULL
&& !switched_seg_p
)
376 int label_seen
= FALSE
;
377 struct frag
*old_frag
;
381 gas_assert (S_GET_SEGMENT (label
) == now_seg
);
383 old_frag
= symbol_get_frag (label
);
384 old_value
= S_GET_VALUE (label
);
385 new_value
= (valueT
) frag_now_fix ();
387 /* It is possible to have more than one label at a particular
388 address, especially if debugging is enabled, so we must
389 take care to adjust all the labels at this address in this
390 fragment. To save time we search from the end of the symbol
391 list, backwards, since the symbols we are interested in are
392 almost certainly the ones that were most recently added.
393 Also to save time we stop searching once we have seen at least
394 one matching label, and we encounter a label that is no longer
395 in the target fragment. Note, this search is guaranteed to
396 find at least one match when sym == label, so no special case
397 code is necessary. */
398 for (sym
= symbol_lastP
; sym
!= NULL
; sym
= symbol_previous (sym
))
399 if (symbol_get_frag (sym
) == old_frag
400 && S_GET_VALUE (sym
) == old_value
)
403 symbol_set_frag (sym
, frag_now
);
404 S_SET_VALUE (sym
, new_value
);
406 else if (label_seen
&& symbol_get_frag (sym
) != old_frag
)
409 record_alignment (now_seg
, align
);
414 /** Support for self-check mode. */
416 /* Mode of the assembler. */
419 NIOS2_MODE_ASSEMBLE
, /* Ordinary operation. */
420 NIOS2_MODE_TEST
/* Hidden mode used for self testing. */
423 static NIOS2_MODE nios2_mode
= NIOS2_MODE_ASSEMBLE
;
425 /* This function is used to in self-checking mode
426 to check the assembled instruction
427 opcode should be the assembled opcode, and exp_opcode
428 the parsed string representing the expected opcode. */
430 nios2_check_assembly (unsigned int opcode
, const char *exp_opcode
)
432 if (nios2_mode
== NIOS2_MODE_TEST
)
434 if (exp_opcode
== NULL
)
435 as_bad (_("expecting opcode string in self test mode"));
436 else if (opcode
!= strtoul (exp_opcode
, NULL
, 16))
437 as_bad (_("assembly 0x%08x, expected %s"), opcode
, exp_opcode
);
442 /** Support for machine-dependent assembler directives. */
443 /* Handle the .align pseudo-op. This aligns to a power of two. It
444 also adjusts any current instruction label. We treat this the same
445 way the MIPS port does: .align 0 turns off auto alignment. */
447 s_nios2_align (int ignore ATTRIBUTE_UNUSED
)
451 const char *pfill
= NULL
;
452 long max_alignment
= 15;
454 align
= get_absolute_expression ();
455 if (align
> max_alignment
)
457 align
= max_alignment
;
458 as_bad (_("Alignment too large: %d. assumed"), align
);
462 as_warn (_("Alignment negative: 0 assumed"));
466 if (*input_line_pointer
== ',')
468 input_line_pointer
++;
469 fill
= get_absolute_expression ();
470 pfill
= (const char *) &fill
;
472 else if (subseg_text_p (now_seg
))
473 pfill
= (const char *) &nop
;
477 nios2_last_label
= NULL
;
482 nios2_auto_align_on
= 1;
483 nios2_align (align
, pfill
, nios2_last_label
);
484 nios2_last_label
= NULL
;
487 nios2_auto_align_on
= 0;
489 demand_empty_rest_of_line ();
492 /* Handle the .text pseudo-op. This is like the usual one, but it
493 clears the saved last label and resets known alignment. */
498 nios2_last_label
= NULL
;
499 nios2_current_align
= 0;
500 nios2_current_align_seg
= now_seg
;
503 /* Handle the .data pseudo-op. This is like the usual one, but it
504 clears the saved last label and resets known alignment. */
509 nios2_last_label
= NULL
;
510 nios2_current_align
= 0;
511 nios2_current_align_seg
= now_seg
;
514 /* Handle the .section pseudo-op. This is like the usual one, but it
515 clears the saved last label and resets known alignment. */
517 s_nios2_section (int ignore
)
519 obj_elf_section (ignore
);
520 nios2_last_label
= NULL
;
521 nios2_current_align
= 0;
522 nios2_current_align_seg
= now_seg
;
525 /* Explicitly unaligned cons. */
527 s_nios2_ucons (int nbytes
)
530 hold
= nios2_auto_align_on
;
531 nios2_auto_align_on
= 0;
533 nios2_auto_align_on
= hold
;
536 /* Handle the .sdata directive. */
538 s_nios2_sdata (int ignore ATTRIBUTE_UNUSED
)
540 get_absolute_expression (); /* Ignored. */
541 subseg_new (".sdata", 0);
542 demand_empty_rest_of_line ();
545 /* .set sets assembler options eg noat/at and is also used
546 to set symbol values (.equ, .equiv ). */
548 s_nios2_set (int equiv
)
550 char *directive
= input_line_pointer
;
551 char delim
= get_symbol_end ();
552 char *endline
= input_line_pointer
;
555 /* We only want to handle ".set XXX" if the
556 user has tried ".set XXX, YYY" they are not
557 trying a directive. This prevents
558 us from polluting the name space. */
560 if (is_end_of_line
[(unsigned char) *input_line_pointer
])
562 bfd_boolean done
= TRUE
;
565 if (!strcmp (directive
, "noat"))
566 nios2_as_options
.noat
= TRUE
;
567 else if (!strcmp (directive
, "at"))
568 nios2_as_options
.noat
= FALSE
;
569 else if (!strcmp (directive
, "nobreak"))
570 nios2_as_options
.nobreak
= TRUE
;
571 else if (!strcmp (directive
, "break"))
572 nios2_as_options
.nobreak
= FALSE
;
573 else if (!strcmp (directive
, "norelax"))
574 nios2_as_options
.relax
= relax_none
;
575 else if (!strcmp (directive
, "relaxsection"))
576 nios2_as_options
.relax
= relax_section
;
577 else if (!strcmp (directive
, "relaxall"))
578 nios2_as_options
.relax
= relax_all
;
585 demand_empty_rest_of_line ();
590 /* If we fall through to here, either we have ".set XXX, YYY"
591 or we have ".set XXX" where XXX is unknown or we have
593 input_line_pointer
= directive
;
598 /* Machine-dependent assembler directives.
599 Format of each entry is:
600 { "directive", handler_func, param } */
601 const pseudo_typeS md_pseudo_table
[] = {
602 {"align", s_nios2_align
, 0},
603 {"text", s_nios2_text
, 0},
604 {"data", s_nios2_data
, 0},
605 {"section", s_nios2_section
, 0},
606 {"section.s", s_nios2_section
, 0},
607 {"sect", s_nios2_section
, 0},
608 {"sect.s", s_nios2_section
, 0},
609 /* .dword and .half are included for compatibility with MIPS. */
612 /* NIOS2 native word size is 4 bytes, so we override
613 the GAS default of 2. */
615 /* Explicitly unaligned directives. */
616 {"2byte", s_nios2_ucons
, 2},
617 {"4byte", s_nios2_ucons
, 4},
618 {"8byte", s_nios2_ucons
, 8},
619 {"16byte", s_nios2_ucons
, 16},
621 {"sdata", s_nios2_sdata
, 0},
623 {"set", s_nios2_set
, 0},
628 /** Relaxation support. */
630 /* We support two relaxation modes: a limited PC-relative mode with
631 -relax-section (the default), and an absolute jump mode with -relax-all.
633 Nios II PC-relative branch instructions only support 16-bit offsets.
634 And, there's no good way to add a 32-bit constant to the PC without
637 To deal with this, for the pc-relative relaxation mode we convert
639 into a series of 16-bit adds, like:
643 addi at, at, remainder
646 Similarly, conditional branches are converted from
647 b(condition) r, s, label
649 b(opposite condition) r, s, skip
653 addi at, at, remainder
657 The compiler can do a better job, either by converting the branch
658 directly into a JMP (going through the GOT for PIC) or by allocating
659 a second register for the 32-bit displacement.
661 For the -relax-all relaxation mode, the conversions are
662 movhi at, %hi(symbol+offset)
663 ori at, %lo(symbol+offset)
666 b(opposite condition), r, s, skip
667 movhi at, %hi(symbol+offset)
668 ori at, %lo(symbol+offset)
674 /* Arbitrarily limit the number of addis we can insert; we need to be able
675 to specify the maximum growth size for each frag that contains a
676 relaxable branch. There's no point in specifying a huge number here
677 since that means the assembler needs to allocate that much extra
678 memory for every branch, and almost no real code will ever need it.
679 Plus, as already noted a better solution is to just use a jmp, or
680 allocate a second register to hold a 32-bit displacement.
681 FIXME: Rather than making this a constant, it could be controlled by
682 a command-line argument. */
683 #define RELAX_MAX_ADDI 32
685 /* The fr_subtype field represents the target-specific relocation state.
686 It has type relax_substateT (unsigned int). We use it to track the
687 number of addis necessary, plus a bit to track whether this is a
689 Regardless of the smaller RELAX_MAX_ADDI limit, we reserve 16 bits
690 in the fr_subtype to encode the number of addis so that the whole
691 theoretically-valid range is representable.
692 For the -relax-all mode, N = 0 represents an in-range branch and N = 1
693 represents a branch that needs to be relaxed. */
694 #define UBRANCH (0 << 16)
695 #define CBRANCH (1 << 16)
696 #define IS_CBRANCH(SUBTYPE) ((SUBTYPE) & CBRANCH)
697 #define IS_UBRANCH(SUBTYPE) (!IS_CBRANCH (SUBTYPE))
698 #define UBRANCH_SUBTYPE(N) (UBRANCH | (N))
699 #define CBRANCH_SUBTYPE(N) (CBRANCH | (N))
700 #define SUBTYPE_ADDIS(SUBTYPE) ((SUBTYPE) & 0xffff)
702 /* For the -relax-section mode, unconditional branches require 2 extra i
703 nstructions besides the addis, conditional branches require 3. */
704 #define UBRANCH_ADDIS_TO_SIZE(N) (((N) + 2) * 4)
705 #define CBRANCH_ADDIS_TO_SIZE(N) (((N) + 3) * 4)
707 /* For the -relax-all mode, unconditional branches require 3 instructions
708 and conditional branches require 4. */
709 #define UBRANCH_JUMP_SIZE 12
710 #define CBRANCH_JUMP_SIZE 16
712 /* Maximum sizes of relaxation sequences. */
713 #define UBRANCH_MAX_SIZE \
714 (nios2_as_options.relax == relax_all \
715 ? UBRANCH_JUMP_SIZE \
716 : UBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
717 #define CBRANCH_MAX_SIZE \
718 (nios2_as_options.relax == relax_all \
719 ? CBRANCH_JUMP_SIZE \
720 : CBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
722 /* Register number of AT, the assembler temporary. */
725 /* Determine how many bytes are required to represent the sequence
726 indicated by SUBTYPE. */
728 nios2_relax_subtype_size (relax_substateT subtype
)
730 int n
= SUBTYPE_ADDIS (subtype
);
732 /* Regular conditional/unconditional branch instruction. */
734 else if (nios2_as_options
.relax
== relax_all
)
735 return (IS_CBRANCH (subtype
) ? CBRANCH_JUMP_SIZE
: UBRANCH_JUMP_SIZE
);
736 else if (IS_CBRANCH (subtype
))
737 return CBRANCH_ADDIS_TO_SIZE (n
);
739 return UBRANCH_ADDIS_TO_SIZE (n
);
742 /* Estimate size of fragp before relaxation.
743 This could also examine the offset in fragp and adjust
744 fragp->fr_subtype, but we will do that in nios2_relax_frag anyway. */
746 md_estimate_size_before_relax (fragS
*fragp
, segT segment ATTRIBUTE_UNUSED
)
748 return nios2_relax_subtype_size (fragp
->fr_subtype
);
751 /* Implement md_relax_frag, returning the change in size of the frag. */
753 nios2_relax_frag (segT segment
, fragS
*fragp
, long stretch
)
755 addressT target
= fragp
->fr_offset
;
756 relax_substateT subtype
= fragp
->fr_subtype
;
757 symbolS
*symbolp
= fragp
->fr_symbol
;
761 fragS
*sym_frag
= symbol_get_frag (symbolp
);
765 target
+= S_GET_VALUE (symbolp
);
767 /* See comments in write.c:relax_frag about handling of stretch. */
769 && sym_frag
->relax_marker
!= fragp
->relax_marker
)
771 if (stretch
< 0 || sym_frag
->region
== fragp
->region
)
773 else if (target
< fragp
->fr_address
)
774 target
= fragp
->fr_next
->fr_address
+ stretch
;
777 /* We subtract fr_var (4 for 32-bit insns) because all pc relative
778 branches are from the next instruction. */
779 offset
= target
- fragp
->fr_address
- fragp
->fr_fix
- fragp
->fr_var
;
780 if (offset
>= -32768 && offset
<= 32764)
781 /* Fits in PC-relative branch. */
783 else if (nios2_as_options
.relax
== relax_all
)
784 /* Convert to jump. */
786 else if (nios2_as_options
.relax
== relax_section
787 && S_GET_SEGMENT (symbolp
) == segment
788 && S_IS_DEFINED (symbolp
))
789 /* Attempt a PC-relative relaxation on a branch to a defined
790 symbol in the same segment. */
792 /* The relaxation for conditional branches is offset by 4
793 bytes because we insert the inverted branch around the
795 if (IS_CBRANCH (subtype
))
798 n
= offset
/ 32767 + 1;
800 n
= offset
/ -32768 + 1;
802 /* Bail out immediately if relaxation has failed. If we try to
803 defer the diagnostic to md_convert_frag, some pathological test
804 cases (e.g. gcc/testsuite/gcc.c-torture/compile/20001226-1.c)
805 apparently never converge. By returning 0 here we could pretend
806 to the caller that nothing has changed, but that leaves things
807 in an inconsistent state when we get to md_convert_frag. */
808 if (n
> RELAX_MAX_ADDI
)
810 as_bad_where (fragp
->fr_file
, fragp
->fr_line
,
811 _("branch offset out of range\n"));
812 as_fatal (_("branch relaxation failed\n"));
816 /* We cannot handle this case, diagnose overflow later. */
819 if (IS_CBRANCH (subtype
))
820 fragp
->fr_subtype
= CBRANCH_SUBTYPE (n
);
822 fragp
->fr_subtype
= UBRANCH_SUBTYPE (n
);
824 return (nios2_relax_subtype_size (fragp
->fr_subtype
)
825 - nios2_relax_subtype_size (subtype
));
828 /* If we got here, it's probably an error. */
833 /* Complete fragp using the data from the relaxation pass. */
835 md_convert_frag (bfd
*headers ATTRIBUTE_UNUSED
, segT segment ATTRIBUTE_UNUSED
,
838 char *buffer
= fragp
->fr_literal
+ fragp
->fr_fix
;
839 relax_substateT subtype
= fragp
->fr_subtype
;
840 int n
= SUBTYPE_ADDIS (subtype
);
841 addressT target
= fragp
->fr_offset
;
842 symbolS
*symbolp
= fragp
->fr_symbol
;
844 unsigned int addend_mask
, addi_mask
;
845 offsetT addend
, remainder
;
848 /* If we didn't or can't relax, this is a regular branch instruction.
849 We just need to generate the fixup for the symbol and offset. */
852 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
, fragp
->fr_offset
, 1,
858 /* Replace the cbranch at fr_fix with one that has the opposite condition
859 in order to jump around the block of instructions we'll be adding. */
860 if (IS_CBRANCH (subtype
))
862 unsigned int br_opcode
;
863 unsigned int old_op
, new_op
;
866 /* Account for the nextpc and jmp in the pc-relative case, or the two
867 load instructions and jump in the absolute case. */
868 if (nios2_as_options
.relax
== relax_section
)
869 nbytes
= (n
+ 2) * 4;
873 br_opcode
= md_chars_to_number (buffer
, 4);
874 old_op
= GET_IW_R1_OP (br_opcode
);
896 as_bad_where (fragp
->fr_file
, fragp
->fr_line
,
897 _("expecting conditional branch for relaxation\n"));
901 br_opcode
= (br_opcode
& ~IW_R1_OP_SHIFTED_MASK
) | SET_IW_R1_OP (new_op
);
902 br_opcode
= br_opcode
| SET_IW_I_IMM16 (nbytes
);
903 md_number_to_chars (buffer
, br_opcode
, 4);
908 /* Load at for the PC-relative case. */
909 if (nios2_as_options
.relax
== relax_section
)
911 /* Insert the nextpc instruction. */
912 md_number_to_chars (buffer
,
913 MATCH_R1_NEXTPC
| SET_IW_R_C (AT_REGNUM
), 4);
917 /* We need to know whether the offset is positive or negative. */
918 target
+= S_GET_VALUE (symbolp
);
919 offset
= target
- fragp
->fr_address
- fragp
->fr_fix
;
924 addend_mask
= SET_IW_I_IMM16 ((unsigned int)addend
);
926 /* Insert n-1 addi instructions. */
927 addi_mask
= (MATCH_R1_ADDI
928 | SET_IW_I_B (AT_REGNUM
)
929 | SET_IW_I_A (AT_REGNUM
));
930 for (i
= 0; i
< n
- 1; i
++)
932 md_number_to_chars (buffer
, addi_mask
| addend_mask
, 4);
937 /* Insert the last addi instruction to hold the remainder. */
938 remainder
= offset
- addend
* (n
- 1);
939 gas_assert (remainder
>= -32768 && remainder
<= 32767);
940 addend_mask
= SET_IW_I_IMM16 ((unsigned int)remainder
);
941 md_number_to_chars (buffer
, addi_mask
| addend_mask
, 4);
946 /* Load at for the absolute case. */
949 md_number_to_chars (buffer
,
950 (MATCH_R1_ORHI
| SET_IW_I_B (AT_REGNUM
)
953 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
, fragp
->fr_offset
,
954 0, BFD_RELOC_NIOS2_HI16
);
957 md_number_to_chars (buffer
,
958 (MATCH_R1_ORI
| SET_IW_I_B (AT_REGNUM
)
959 | SET_IW_I_A (AT_REGNUM
)),
961 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
, fragp
->fr_offset
,
962 0, BFD_RELOC_NIOS2_LO16
);
967 /* Insert the jmp instruction. */
968 md_number_to_chars (buffer
, MATCH_R1_JMP
| SET_IW_R_A (AT_REGNUM
), 4);
974 /** Fixups and overflow checking. */
976 /* Check a fixup for overflow. */
978 nios2_check_overflow (valueT fixup
, reloc_howto_type
*howto
)
980 /* Apply the rightshift before checking for overflow. */
981 fixup
= ((signed)fixup
) >> howto
->rightshift
;
983 /* Check for overflow - return TRUE if overflow, FALSE if not. */
984 switch (howto
->complain_on_overflow
)
986 case complain_overflow_dont
:
988 case complain_overflow_bitfield
:
989 if ((fixup
>> howto
->bitsize
) != 0
990 && ((signed) fixup
>> howto
->bitsize
) != -1)
993 case complain_overflow_signed
:
994 if ((fixup
& 0x80000000) > 0)
996 /* Check for negative overflow. */
997 if ((signed) fixup
< ((signed) 0x80000000 >> howto
->bitsize
))
1002 /* Check for positive overflow. */
1003 if (fixup
>= ((unsigned) 1 << (howto
->bitsize
- 1)))
1007 case complain_overflow_unsigned
:
1008 if ((fixup
>> howto
->bitsize
) != 0)
1012 as_bad (_("error checking for overflow - broken assembler"));
1018 /* Emit diagnostic for fixup overflow. */
1020 nios2_diagnose_overflow (valueT fixup
, reloc_howto_type
*howto
,
1021 fixS
*fixP
, valueT value
)
1023 if (fixP
->fx_r_type
== BFD_RELOC_8
1024 || fixP
->fx_r_type
== BFD_RELOC_16
1025 || fixP
->fx_r_type
== BFD_RELOC_32
)
1026 /* These relocs are against data, not instructions. */
1027 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1028 _("immediate value 0x%x truncated to 0x%x"),
1029 (unsigned int) fixup
,
1030 (unsigned int) (~(~(valueT
) 0 << howto
->bitsize
) & fixup
));
1033 /* What opcode is the instruction? This will determine
1034 whether we check for overflow in immediate values
1035 and what error message we get. */
1036 const struct nios2_opcode
*opcode
;
1037 enum overflow_type overflow_msg_type
;
1038 unsigned int range_min
;
1039 unsigned int range_max
;
1040 unsigned int address
;
1042 opcode
= nios2_find_opcode_hash (value
, bfd_get_mach (stdoutput
));
1043 gas_assert (opcode
);
1044 gas_assert (fixP
->fx_size
== opcode
->size
);
1045 overflow_msg_type
= opcode
->overflow_msg
;
1046 switch (overflow_msg_type
)
1048 case call_target_overflow
:
1050 = ((fixP
->fx_frag
->fr_address
+ fixP
->fx_where
) & 0xf0000000);
1051 range_max
= range_min
+ 0x0fffffff;
1052 address
= fixup
| range_min
;
1054 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1055 _("call target address 0x%08x out of range 0x%08x to 0x%08x"),
1056 address
, range_min
, range_max
);
1058 case branch_target_overflow
:
1059 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1060 _("branch offset %d out of range %d to %d"),
1061 (int)fixup
, -32768, 32767);
1063 case address_offset_overflow
:
1064 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1065 _("%s offset %d out of range %d to %d"),
1066 opcode
->name
, (int)fixup
, -32768, 32767);
1068 case signed_immed16_overflow
:
1069 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1070 _("immediate value %d out of range %d to %d"),
1071 (int)fixup
, -32768, 32767);
1073 case unsigned_immed16_overflow
:
1074 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1075 _("immediate value %u out of range %u to %u"),
1076 (unsigned int)fixup
, 0, 65535);
1078 case unsigned_immed5_overflow
:
1079 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1080 _("immediate value %u out of range %u to %u"),
1081 (unsigned int)fixup
, 0, 31);
1083 case custom_opcode_overflow
:
1084 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1085 _("custom instruction opcode %u out of range %u to %u"),
1086 (unsigned int)fixup
, 0, 255);
1089 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1090 _("overflow in immediate argument"));
1096 /* Apply a fixup to the object file. */
1098 md_apply_fix (fixS
*fixP
, valueT
*valP
, segT seg ATTRIBUTE_UNUSED
)
1100 /* Assert that the fixup is one we can handle. */
1101 gas_assert (fixP
!= NULL
&& valP
!= NULL
1102 && (fixP
->fx_r_type
== BFD_RELOC_8
1103 || fixP
->fx_r_type
== BFD_RELOC_16
1104 || fixP
->fx_r_type
== BFD_RELOC_32
1105 || fixP
->fx_r_type
== BFD_RELOC_64
1106 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_S16
1107 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_U16
1108 || fixP
->fx_r_type
== BFD_RELOC_16_PCREL
1109 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL26
1110 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM5
1111 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CACHE_OPX
1112 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM6
1113 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM8
1114 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_HI16
1115 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_LO16
1116 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_HIADJ16
1117 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GPREL
1118 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1119 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
1120 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_UJMP
1121 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CJMP
1122 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALLR
1123 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_ALIGN
1124 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOT16
1125 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL16
1126 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_LO
1127 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_HA
1128 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_GD16
1129 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDM16
1130 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDO16
1131 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_IE16
1132 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LE16
1133 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF
1134 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPREL
1135 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL26_NOAT
1136 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOT_LO
1137 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOT_HA
1138 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL_LO
1139 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL_HA
1140 /* Add other relocs here as we generate them. */
1143 if (fixP
->fx_r_type
== BFD_RELOC_64
)
1145 /* We may reach here due to .8byte directives, but we never output
1146 BFD_RELOC_64; it must be resolved. */
1147 if (fixP
->fx_addsy
!= NULL
)
1148 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1149 _("cannot create 64-bit relocation"));
1152 md_number_to_chars (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
,
1159 /* The value passed in valP can be the value of a fully
1160 resolved expression, or it can be the value of a partially
1161 resolved expression. In the former case, both fixP->fx_addsy
1162 and fixP->fx_subsy are NULL, and fixP->fx_offset == *valP, and
1163 we can fix up the instruction that fixP relates to.
1164 In the latter case, one or both of fixP->fx_addsy and
1165 fixP->fx_subsy are not NULL, and fixP->fx_offset may or may not
1166 equal *valP. We don't need to check for fixP->fx_subsy being null
1167 because the generic part of the assembler generates an error if
1168 it is not an absolute symbol. */
1169 if (fixP
->fx_addsy
!= NULL
)
1170 /* Partially resolved expression. */
1172 fixP
->fx_addnumber
= fixP
->fx_offset
;
1175 switch (fixP
->fx_r_type
)
1177 case BFD_RELOC_NIOS2_TLS_GD16
:
1178 case BFD_RELOC_NIOS2_TLS_LDM16
:
1179 case BFD_RELOC_NIOS2_TLS_LDO16
:
1180 case BFD_RELOC_NIOS2_TLS_IE16
:
1181 case BFD_RELOC_NIOS2_TLS_LE16
:
1182 case BFD_RELOC_NIOS2_TLS_DTPMOD
:
1183 case BFD_RELOC_NIOS2_TLS_DTPREL
:
1184 case BFD_RELOC_NIOS2_TLS_TPREL
:
1185 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
1192 /* Fully resolved fixup. */
1194 reloc_howto_type
*howto
1195 = bfd_reloc_type_lookup (stdoutput
, fixP
->fx_r_type
);
1198 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1199 _("relocation is not supported"));
1202 valueT fixup
= *valP
;
1206 /* If this is a pc-relative relocation, we need to
1207 subtract the current offset within the object file
1208 FIXME : for some reason fixP->fx_pcrel isn't 1 when it should be
1209 so I'm using the howto structure instead to determine this. */
1210 if (howto
->pc_relative
== 1)
1211 fixup
= fixup
- (fixP
->fx_frag
->fr_address
+ fixP
->fx_where
1214 /* Get the instruction or data to be fixed up. */
1215 buf
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
1216 value
= md_chars_to_number (buf
, fixP
->fx_size
);
1218 /* Check for overflow, emitting a diagnostic if necessary. */
1219 if (nios2_check_overflow (fixup
, howto
))
1220 nios2_diagnose_overflow (fixup
, howto
, fixP
, value
);
1222 /* Apply the right shift. */
1223 fixup
= ((signed)fixup
) >> howto
->rightshift
;
1225 /* Truncate the fixup to right size. */
1226 switch (fixP
->fx_r_type
)
1228 case BFD_RELOC_NIOS2_HI16
:
1229 fixup
= (fixup
>> 16) & 0xFFFF;
1231 case BFD_RELOC_NIOS2_LO16
:
1232 fixup
= fixup
& 0xFFFF;
1234 case BFD_RELOC_NIOS2_HIADJ16
:
1235 fixup
= ((((fixup
>> 16) & 0xFFFF) + ((fixup
>> 15) & 0x01))
1240 int n
= sizeof (fixup
) * 8 - howto
->bitsize
;
1241 fixup
= (fixup
<< n
) >> n
;
1246 /* Fix up the instruction. */
1247 value
= (value
& ~howto
->dst_mask
) | (fixup
<< howto
->bitpos
);
1248 md_number_to_chars (buf
, value
, fixP
->fx_size
);
1254 if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
)
1258 && !S_IS_DEFINED (fixP
->fx_addsy
) && !S_IS_WEAK (fixP
->fx_addsy
))
1259 S_SET_WEAK (fixP
->fx_addsy
);
1261 else if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1267 /** Instruction parsing support. */
1269 /* General internal error routine. */
1272 bad_opcode (const struct nios2_opcode
*op
)
1274 fprintf (stderr
, _("internal error: broken opcode descriptor for `%s %s'\n"),
1275 op
->name
, op
->args
);
1276 as_fatal (_("Broken assembler. No assembly attempted."));
1279 /* Special relocation directive strings. */
1281 struct nios2_special_relocS
1284 bfd_reloc_code_real_type reloc_type
;
1287 /* This table is sorted so that prefix strings are listed after the longer
1288 strings that include them -- e.g., %got after %got_hiadj, etc. */
1290 struct nios2_special_relocS nios2_special_reloc
[] = {
1291 {"%hiadj", BFD_RELOC_NIOS2_HIADJ16
},
1292 {"%hi", BFD_RELOC_NIOS2_HI16
},
1293 {"%lo", BFD_RELOC_NIOS2_LO16
},
1294 {"%gprel", BFD_RELOC_NIOS2_GPREL
},
1295 {"%call_lo", BFD_RELOC_NIOS2_CALL_LO
},
1296 {"%call_hiadj", BFD_RELOC_NIOS2_CALL_HA
},
1297 {"%call", BFD_RELOC_NIOS2_CALL16
},
1298 {"%gotoff_lo", BFD_RELOC_NIOS2_GOTOFF_LO
},
1299 {"%gotoff_hiadj", BFD_RELOC_NIOS2_GOTOFF_HA
},
1300 {"%gotoff", BFD_RELOC_NIOS2_GOTOFF
},
1301 {"%got_hiadj", BFD_RELOC_NIOS2_GOT_HA
},
1302 {"%got_lo", BFD_RELOC_NIOS2_GOT_LO
},
1303 {"%got", BFD_RELOC_NIOS2_GOT16
},
1304 {"%tls_gd", BFD_RELOC_NIOS2_TLS_GD16
},
1305 {"%tls_ldm", BFD_RELOC_NIOS2_TLS_LDM16
},
1306 {"%tls_ldo", BFD_RELOC_NIOS2_TLS_LDO16
},
1307 {"%tls_ie", BFD_RELOC_NIOS2_TLS_IE16
},
1308 {"%tls_le", BFD_RELOC_NIOS2_TLS_LE16
},
1311 #define NIOS2_NUM_SPECIAL_RELOCS \
1312 (sizeof(nios2_special_reloc)/sizeof(nios2_special_reloc[0]))
1313 const int nios2_num_special_relocs
= NIOS2_NUM_SPECIAL_RELOCS
;
1315 /* Creates a new nios2_insn_relocS and returns a pointer to it. */
1316 static nios2_insn_relocS
*
1317 nios2_insn_reloc_new (bfd_reloc_code_real_type reloc_type
, unsigned int pcrel
)
1319 nios2_insn_relocS
*retval
;
1320 retval
= (nios2_insn_relocS
*) malloc (sizeof (nios2_insn_relocS
));
1323 as_bad (_("can't create relocation"));
1327 /* Fill out the fields with default values. */
1328 retval
->reloc_next
= NULL
;
1329 retval
->reloc_type
= reloc_type
;
1330 retval
->reloc_pcrel
= pcrel
;
1334 /* Frees up memory previously allocated by nios2_insn_reloc_new(). */
1335 /* FIXME: this is never called; memory leak? */
1338 nios2_insn_reloc_destroy (nios2_insn_relocS
*reloc
)
1340 gas_assert (reloc
!= NULL
);
1345 /* Look up a register name and validate it for the given regtype.
1346 Return the register mapping or NULL on failure. */
1347 static struct nios2_reg
*
1348 nios2_parse_reg (const char *token
, unsigned long regtype
)
1350 struct nios2_reg
*reg
= nios2_reg_lookup (token
);
1354 as_bad (_("unknown register %s"), token
);
1358 /* Matched a register, but is it the wrong type? */
1359 if (!(regtype
& reg
->regtype
))
1361 if (regtype
& REG_CONTROL
)
1362 as_bad (_("expecting control register"));
1363 else if (reg
->regtype
& REG_CONTROL
)
1364 as_bad (_("illegal use of control register"));
1365 else if (reg
->regtype
& REG_COPROCESSOR
)
1366 as_bad (_("illegal use of coprocessor register"));
1368 as_bad (_("invalid register %s"), token
);
1372 /* Warn for explicit use of special registers. */
1373 if (reg
->regtype
& REG_NORMAL
)
1375 if (!nios2_as_options
.noat
&& reg
->index
== 1)
1376 as_warn (_("Register at (r1) can sometimes be corrupted by "
1377 "assembler optimizations.\n"
1378 "Use .set noat to turn off those optimizations "
1379 "(and this warning)."));
1380 if (!nios2_as_options
.nobreak
&& reg
->index
== 25)
1381 as_warn (_("The debugger will corrupt bt (r25).\n"
1382 "If you don't need to debug this "
1383 "code use .set nobreak to turn off this warning."));
1384 if (!nios2_as_options
.nobreak
&& reg
->index
== 30)
1385 as_warn (_("The debugger will corrupt sstatus/ba (r30).\n"
1386 "If you don't need to debug this "
1387 "code use .set nobreak to turn off this warning."));
1393 /* The various nios2_assemble_* functions call this
1394 function to generate an expression from a string representing an expression.
1395 It then tries to evaluate the expression, and if it can, returns its value.
1396 If not, it creates a new nios2_insn_relocS and stores the expression and
1397 reloc_type for future use. */
1398 static unsigned long
1399 nios2_assemble_expression (const char *exprstr
,
1400 nios2_insn_infoS
*insn
,
1401 bfd_reloc_code_real_type reloc_type
,
1404 nios2_insn_relocS
*reloc
;
1405 char *saved_line_ptr
;
1406 unsigned short value
;
1409 gas_assert (exprstr
!= NULL
);
1410 gas_assert (insn
!= NULL
);
1412 /* Check for relocation operators.
1413 Change the relocation type and advance the ptr to the start of
1414 the expression proper. */
1415 for (i
= 0; i
< nios2_num_special_relocs
; i
++)
1416 if (strstr (exprstr
, nios2_special_reloc
[i
].string
) != NULL
)
1418 reloc_type
= nios2_special_reloc
[i
].reloc_type
;
1419 exprstr
+= strlen (nios2_special_reloc
[i
].string
) + 1;
1421 /* %lo and %hiadj have different meanings for PC-relative
1425 if (reloc_type
== BFD_RELOC_NIOS2_LO16
)
1426 reloc_type
= BFD_RELOC_NIOS2_PCREL_LO
;
1427 if (reloc_type
== BFD_RELOC_NIOS2_HIADJ16
)
1428 reloc_type
= BFD_RELOC_NIOS2_PCREL_HA
;
1434 /* We potentially have a relocation. */
1435 reloc
= nios2_insn_reloc_new (reloc_type
, pcrel
);
1436 reloc
->reloc_next
= insn
->insn_reloc
;
1437 insn
->insn_reloc
= reloc
;
1439 /* Parse the expression string. */
1440 saved_line_ptr
= input_line_pointer
;
1441 input_line_pointer
= (char *) exprstr
;
1442 expression (&reloc
->reloc_expression
);
1443 input_line_pointer
= saved_line_ptr
;
1445 /* This is redundant as the fixup will put this into
1446 the instruction, but it is included here so that
1447 self-test mode (-r) works. */
1449 if (nios2_mode
== NIOS2_MODE_TEST
1450 && reloc
->reloc_expression
.X_op
== O_constant
)
1451 value
= reloc
->reloc_expression
.X_add_number
;
1453 return (unsigned long) value
;
1457 /* Argument assemble functions. */
1459 nios2_assemble_arg_c (const char *token
, nios2_insn_infoS
*insn
)
1461 struct nios2_reg
*reg
= nios2_parse_reg (token
, REG_CONTROL
);
1462 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1470 insn
->insn_code
|= SET_IW_R_IMM5 (reg
->index
);
1478 nios2_assemble_arg_d (const char *token
, nios2_insn_infoS
*insn
)
1480 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1481 unsigned long regtype
= REG_NORMAL
;
1482 struct nios2_reg
*reg
;
1484 if (op
->format
== iw_custom_type
)
1485 regtype
|= REG_COPROCESSOR
;
1486 reg
= nios2_parse_reg (token
, regtype
);
1493 insn
->insn_code
|= SET_IW_R_C (reg
->index
);
1495 case iw_custom_type
:
1496 insn
->insn_code
|= SET_IW_CUSTOM_C (reg
->index
);
1497 if (reg
->regtype
& REG_COPROCESSOR
)
1498 insn
->insn_code
|= SET_IW_CUSTOM_READC (0);
1500 insn
->insn_code
|= SET_IW_CUSTOM_READC (1);
1508 nios2_assemble_arg_s (const char *token
, nios2_insn_infoS
*insn
)
1510 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1511 unsigned long regtype
= REG_NORMAL
;
1512 struct nios2_reg
*reg
;
1514 if (op
->format
== iw_custom_type
)
1515 regtype
|= REG_COPROCESSOR
;
1516 reg
= nios2_parse_reg (token
, regtype
);
1523 insn
->insn_code
|= SET_IW_R_A (reg
->index
);
1526 insn
->insn_code
|= SET_IW_I_A (reg
->index
);
1528 case iw_custom_type
:
1529 insn
->insn_code
|= SET_IW_CUSTOM_A (reg
->index
);
1530 if (reg
->regtype
& REG_COPROCESSOR
)
1531 insn
->insn_code
|= SET_IW_CUSTOM_READA (0);
1533 insn
->insn_code
|= SET_IW_CUSTOM_READA (1);
1541 nios2_assemble_arg_t (const char *token
, nios2_insn_infoS
*insn
)
1543 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1544 unsigned long regtype
= REG_NORMAL
;
1545 struct nios2_reg
*reg
;
1547 if (op
->format
== iw_custom_type
)
1548 regtype
|= REG_COPROCESSOR
;
1549 reg
= nios2_parse_reg (token
, regtype
);
1556 insn
->insn_code
|= SET_IW_R_B (reg
->index
);
1559 insn
->insn_code
|= SET_IW_I_B (reg
->index
);
1561 case iw_custom_type
:
1562 insn
->insn_code
|= SET_IW_CUSTOM_B (reg
->index
);
1563 if (reg
->regtype
& REG_COPROCESSOR
)
1564 insn
->insn_code
|= SET_IW_CUSTOM_READB (0);
1566 insn
->insn_code
|= SET_IW_CUSTOM_READB (1);
1574 nios2_assemble_arg_i (const char *token
, nios2_insn_infoS
*insn
)
1576 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1582 val
= nios2_assemble_expression (token
, insn
,
1583 BFD_RELOC_NIOS2_S16
, 0);
1584 insn
->constant_bits
|= SET_IW_I_IMM16 (val
);
1592 nios2_assemble_arg_u (const char *token
, nios2_insn_infoS
*insn
)
1594 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1600 val
= nios2_assemble_expression (token
, insn
,
1601 BFD_RELOC_NIOS2_U16
, 0);
1602 insn
->constant_bits
|= SET_IW_I_IMM16 (val
);
1610 nios2_assemble_arg_o (const char *token
, nios2_insn_infoS
*insn
)
1612 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1618 val
= nios2_assemble_expression (token
, insn
,
1619 BFD_RELOC_16_PCREL
, 1);
1620 insn
->constant_bits
|= SET_IW_I_IMM16 (val
);
1628 nios2_assemble_arg_j (const char *token
, nios2_insn_infoS
*insn
)
1630 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1636 val
= nios2_assemble_expression (token
, insn
,
1637 BFD_RELOC_NIOS2_IMM5
, 0);
1638 insn
->constant_bits
|= SET_IW_R_IMM5 (val
);
1646 nios2_assemble_arg_l (const char *token
, nios2_insn_infoS
*insn
)
1648 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1653 case iw_custom_type
:
1654 val
= nios2_assemble_expression (token
, insn
,
1655 BFD_RELOC_NIOS2_IMM8
, 0);
1656 insn
->constant_bits
|= SET_IW_CUSTOM_N (val
);
1664 nios2_assemble_arg_m (const char *token
, nios2_insn_infoS
*insn
)
1666 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1672 val
= nios2_assemble_expression (token
, insn
,
1673 (nios2_as_options
.noat
1674 ? BFD_RELOC_NIOS2_CALL26_NOAT
1675 : BFD_RELOC_NIOS2_CALL26
),
1677 insn
->constant_bits
|= SET_IW_J_IMM26 (val
);
1685 nios2_assemble_args (nios2_insn_infoS
*insn
)
1687 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1689 unsigned int tokidx
, ntok
;
1691 /* Make sure there are enough arguments. */
1692 ntok
= (op
->pinfo
& NIOS2_INSN_OPTARG
) ? op
->num_args
- 1 : op
->num_args
;
1693 for (tokidx
= 1; tokidx
<= ntok
; tokidx
++)
1694 if (insn
->insn_tokens
[tokidx
] == NULL
)
1696 as_bad ("missing argument");
1700 for (argptr
= op
->args
, tokidx
= 1;
1701 *argptr
&& insn
->insn_tokens
[tokidx
];
1711 nios2_assemble_arg_c (insn
->insn_tokens
[tokidx
++], insn
);
1715 nios2_assemble_arg_d (insn
->insn_tokens
[tokidx
++], insn
);
1719 nios2_assemble_arg_s (insn
->insn_tokens
[tokidx
++], insn
);
1723 nios2_assemble_arg_t (insn
->insn_tokens
[tokidx
++], insn
);
1727 nios2_assemble_arg_i (insn
->insn_tokens
[tokidx
++], insn
);
1731 nios2_assemble_arg_u (insn
->insn_tokens
[tokidx
++], insn
);
1735 nios2_assemble_arg_o (insn
->insn_tokens
[tokidx
++], insn
);
1739 nios2_assemble_arg_j (insn
->insn_tokens
[tokidx
++], insn
);
1743 nios2_assemble_arg_l (insn
->insn_tokens
[tokidx
++], insn
);
1747 nios2_assemble_arg_m (insn
->insn_tokens
[tokidx
++], insn
);
1755 /* Perform argument checking. */
1756 nios2_check_assembly (insn
->insn_code
| insn
->constant_bits
,
1757 insn
->insn_tokens
[tokidx
]);
1761 /* The function consume_arg takes a pointer into a string
1762 of instruction tokens (args) and a pointer into a string
1763 representing the expected sequence of tokens and separators.
1764 It checks whether the first argument in argstr is of the
1765 expected type, throwing an error if it is not, and returns
1766 the pointer argstr. */
1768 nios2_consume_arg (char *argstr
, const char *parsestr
)
1784 if (nios2_special_relocation_p (argstr
))
1786 /* We zap the parentheses because we don't want them confused
1788 temp
= strchr (argstr
, '(');
1791 temp
= strchr (argstr
, ')');
1796 as_bad (_("badly formed expression near %s"), argstr
);
1802 /* We can't have %hi, %lo or %hiadj here. */
1804 as_bad (_("badly formed expression near %s"), argstr
);
1810 BAD_CASE (*parsestr
);
1817 /* The function consume_separator takes a pointer into a string
1818 of instruction tokens (args) and a pointer into a string representing
1819 the expected sequence of tokens and separators. It finds the first
1820 instance of the character pointed to by separator in argstr, and
1821 returns a pointer to the next element of argstr, which is the
1822 following token in the sequence. */
1824 nios2_consume_separator (char *argstr
, const char *separator
)
1828 /* If we have a opcode reg, expr(reg) type instruction, and
1829 * we are separating the expr from the (reg), we find the last
1830 * (, just in case the expression has parentheses. */
1832 if (*separator
== '(')
1833 p
= strrchr (argstr
, *separator
);
1835 p
= strchr (argstr
, *separator
);
1843 /* The principal argument parsing function which takes a string argstr
1844 representing the instruction arguments for insn, and extracts the argument
1845 tokens matching parsestr into parsed_args. */
1847 nios2_parse_args (nios2_insn_infoS
*insn
, char *argstr
,
1848 const char *parsestr
, char **parsed_args
)
1855 bfd_boolean terminate
= FALSE
;
1857 /* This rest of this function is it too fragile and it mostly works,
1858 therefore special case this one. */
1859 if (*parsestr
== 0 && argstr
!= 0)
1861 as_bad (_("too many arguments"));
1862 parsed_args
[0] = NULL
;
1866 while (p
!= NULL
&& !terminate
&& i
< NIOS2_MAX_INSN_TOKENS
)
1868 parsed_args
[i
] = nios2_consume_arg (p
, parsestr
);
1870 while (*parsestr
== '(' || *parsestr
== ')' || *parsestr
== ',')
1873 p
= nios2_consume_separator (p
, parsestr
);
1874 /* Check for missing separators. */
1875 if (!p
&& !(insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_OPTARG
))
1877 as_bad (_("expecting %c near %s"), *parsestr
, context
);
1883 if (*parsestr
== '\0')
1885 /* Check that the argument string has no trailing arguments. */
1886 end
= strpbrk (p
, ",");
1888 as_bad (_("too many arguments"));
1891 if (*parsestr
== '\0' || (p
!= NULL
&& *p
== '\0'))
1896 parsed_args
[i
] = NULL
;
1901 /** Support for pseudo-op parsing. These are macro-like opcodes that
1902 expand into real insns by suitable fiddling with the operands. */
1904 /* Append the string modifier to the string contained in the argument at
1905 parsed_args[ndx]. */
1907 nios2_modify_arg (char **parsed_args
, const char *modifier
,
1908 int unused ATTRIBUTE_UNUSED
, int ndx
)
1910 char *tmp
= parsed_args
[ndx
];
1913 = (char *) malloc (strlen (parsed_args
[ndx
]) + strlen (modifier
) + 1);
1914 strcpy (parsed_args
[ndx
], tmp
);
1915 strcat (parsed_args
[ndx
], modifier
);
1918 /* Modify parsed_args[ndx] by negating that argument. */
1920 nios2_negate_arg (char **parsed_args
, const char *modifier ATTRIBUTE_UNUSED
,
1921 int unused ATTRIBUTE_UNUSED
, int ndx
)
1923 char *tmp
= parsed_args
[ndx
];
1926 = (char *) malloc (strlen ("~(") + strlen (parsed_args
[ndx
]) +
1927 strlen (")+1") + 1);
1929 strcpy (parsed_args
[ndx
], "~(");
1930 strcat (parsed_args
[ndx
], tmp
);
1931 strcat (parsed_args
[ndx
], ")+1");
1934 /* The function nios2_swap_args swaps the pointers at indices index_1 and
1935 index_2 in the array parsed_args[] - this is used for operand swapping
1936 for comparison operations. */
1938 nios2_swap_args (char **parsed_args
, const char *unused ATTRIBUTE_UNUSED
,
1939 int index_1
, int index_2
)
1942 gas_assert (index_1
< NIOS2_MAX_INSN_TOKENS
1943 && index_2
< NIOS2_MAX_INSN_TOKENS
);
1944 tmp
= parsed_args
[index_1
];
1945 parsed_args
[index_1
] = parsed_args
[index_2
];
1946 parsed_args
[index_2
] = tmp
;
1949 /* This function appends the string appnd to the array of strings in
1950 parsed_args num times starting at index start in the array. */
1952 nios2_append_arg (char **parsed_args
, const char *appnd
, int num
,
1958 gas_assert ((start
+ num
) < NIOS2_MAX_INSN_TOKENS
);
1960 if (nios2_mode
== NIOS2_MODE_TEST
)
1961 tmp
= parsed_args
[start
];
1965 for (i
= start
, count
= num
; count
> 0; ++i
, --count
)
1966 parsed_args
[i
] = (char *) appnd
;
1968 gas_assert (i
== (start
+ num
));
1969 parsed_args
[i
] = tmp
;
1970 parsed_args
[i
+ 1] = NULL
;
1973 /* This function inserts the string insert num times in the array
1974 parsed_args, starting at the index start. */
1976 nios2_insert_arg (char **parsed_args
, const char *insert
, int num
,
1981 gas_assert ((start
+ num
) < NIOS2_MAX_INSN_TOKENS
);
1983 /* Move the existing arguments up to create space. */
1984 for (i
= NIOS2_MAX_INSN_TOKENS
; i
- num
>= start
; --i
)
1985 parsed_args
[i
] = parsed_args
[i
- num
];
1987 for (i
= start
, count
= num
; count
> 0; ++i
, --count
)
1988 parsed_args
[i
] = (char *) insert
;
1991 /* Cleanup function to free malloc'ed arg strings. */
1993 nios2_free_arg (char **parsed_args
, int num ATTRIBUTE_UNUSED
, int start
)
1995 if (parsed_args
[start
])
1997 free (parsed_args
[start
]);
1998 parsed_args
[start
] = NULL
;
2002 /* This function swaps the pseudo-op for a real op. */
2003 static nios2_ps_insn_infoS
*
2004 nios2_translate_pseudo_insn (nios2_insn_infoS
*insn
)
2007 nios2_ps_insn_infoS
*ps_insn
;
2009 /* Find which real insn the pseudo-op transates to and
2010 switch the insn_info ptr to point to it. */
2011 ps_insn
= nios2_ps_lookup (insn
->insn_nios2_opcode
->name
);
2013 if (ps_insn
!= NULL
)
2015 insn
->insn_nios2_opcode
= nios2_opcode_lookup (ps_insn
->insn
);
2016 insn
->insn_tokens
[0] = insn
->insn_nios2_opcode
->name
;
2017 /* Modify the args so they work with the real insn. */
2018 ps_insn
->arg_modifer_func ((char **) insn
->insn_tokens
,
2019 ps_insn
->arg_modifier
, ps_insn
->num
,
2023 /* we cannot recover from this. */
2024 as_fatal (_("unrecognized pseudo-instruction %s"),
2025 ps_insn
->pseudo_insn
);
2029 /* Invoke the cleanup handler for pseudo-insn ps_insn on insn. */
2031 nios2_cleanup_pseudo_insn (nios2_insn_infoS
*insn
,
2032 nios2_ps_insn_infoS
*ps_insn
)
2034 if (ps_insn
->arg_cleanup_func
)
2035 (ps_insn
->arg_cleanup_func
) ((char **) insn
->insn_tokens
,
2036 ps_insn
->num
, ps_insn
->index
);
2039 const nios2_ps_insn_infoS nios2_ps_insn_info_structs
[] = {
2040 /* pseudo-op, real-op, arg, arg_modifier_func, num, index, arg_cleanup_func */
2041 {"mov", "add", nios2_append_arg
, "zero", 1, 3, NULL
},
2042 {"movi", "addi", nios2_insert_arg
, "zero", 1, 2, NULL
},
2043 {"movhi", "orhi", nios2_insert_arg
, "zero", 1, 2, NULL
},
2044 {"movui", "ori", nios2_insert_arg
, "zero", 1, 2, NULL
},
2045 {"movia", "orhi", nios2_insert_arg
, "zero", 1, 2, NULL
},
2046 {"nop", "add", nios2_append_arg
, "zero", 3, 1, NULL
},
2047 {"bgt", "blt", nios2_swap_args
, "", 1, 2, NULL
},
2048 {"bgtu", "bltu", nios2_swap_args
, "", 1, 2, NULL
},
2049 {"ble", "bge", nios2_swap_args
, "", 1, 2, NULL
},
2050 {"bleu", "bgeu", nios2_swap_args
, "", 1, 2, NULL
},
2051 {"cmpgt", "cmplt", nios2_swap_args
, "", 2, 3, NULL
},
2052 {"cmpgtu", "cmpltu", nios2_swap_args
, "", 2, 3, NULL
},
2053 {"cmple", "cmpge", nios2_swap_args
, "", 2, 3, NULL
},
2054 {"cmpleu", "cmpgeu", nios2_swap_args
, "", 2, 3, NULL
},
2055 {"cmpgti", "cmpgei", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
2056 {"cmpgtui", "cmpgeui", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
2057 {"cmplei", "cmplti", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
2058 {"cmpleui", "cmpltui", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
2059 {"subi", "addi", nios2_negate_arg
, "", 0, 3, nios2_free_arg
}
2060 /* Add further pseudo-ops here. */
2063 #define NIOS2_NUM_PSEUDO_INSNS \
2064 ((sizeof(nios2_ps_insn_info_structs)/ \
2065 sizeof(nios2_ps_insn_info_structs[0])))
2066 const int nios2_num_ps_insn_info_structs
= NIOS2_NUM_PSEUDO_INSNS
;
2069 /** Assembler output support. */
2071 /* Output a normal instruction. */
2073 output_insn (nios2_insn_infoS
*insn
)
2076 nios2_insn_relocS
*reloc
;
2077 f
= frag_more (insn
->insn_nios2_opcode
->size
);
2078 /* This allocates enough space for the instruction
2079 and puts it in the current frag. */
2080 md_number_to_chars (f
, insn
->insn_code
, insn
->insn_nios2_opcode
->size
);
2081 /* Emit debug info. */
2082 dwarf2_emit_insn (insn
->insn_nios2_opcode
->size
);
2083 /* Create any fixups to be acted on later. */
2085 for (reloc
= insn
->insn_reloc
; reloc
!= NULL
; reloc
= reloc
->reloc_next
)
2086 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
,
2087 insn
->insn_nios2_opcode
->size
,
2088 &reloc
->reloc_expression
, reloc
->reloc_pcrel
,
2092 /* Output an unconditional branch. */
2094 output_ubranch (nios2_insn_infoS
*insn
)
2096 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
2098 /* If the reloc is NULL, there was an error assembling the branch. */
2101 symbolS
*symp
= reloc
->reloc_expression
.X_add_symbol
;
2102 offsetT offset
= reloc
->reloc_expression
.X_add_number
;
2105 /* Tag dwarf2 debug info to the address at the start of the insn.
2106 We must do it before frag_var() below closes off the frag. */
2107 dwarf2_emit_insn (0);
2109 /* We create a machine dependent frag which can grow
2110 to accommodate the largest possible instruction sequence
2111 this may generate. */
2112 f
= frag_var (rs_machine_dependent
,
2113 UBRANCH_MAX_SIZE
, insn
->insn_nios2_opcode
->size
,
2114 UBRANCH_SUBTYPE (0), symp
, offset
, NULL
);
2116 md_number_to_chars (f
, insn
->insn_code
, insn
->insn_nios2_opcode
->size
);
2118 /* We leave fixup generation to md_convert_frag. */
2122 /* Output a conditional branch. */
2124 output_cbranch (nios2_insn_infoS
*insn
)
2126 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
2128 /* If the reloc is NULL, there was an error assembling the branch. */
2131 symbolS
*symp
= reloc
->reloc_expression
.X_add_symbol
;
2132 offsetT offset
= reloc
->reloc_expression
.X_add_number
;
2135 /* Tag dwarf2 debug info to the address at the start of the insn.
2136 We must do it before frag_var() below closes off the frag. */
2137 dwarf2_emit_insn (0);
2139 /* We create a machine dependent frag which can grow
2140 to accommodate the largest possible instruction sequence
2141 this may generate. */
2142 f
= frag_var (rs_machine_dependent
,
2143 CBRANCH_MAX_SIZE
, insn
->insn_nios2_opcode
->size
,
2144 CBRANCH_SUBTYPE (0), symp
, offset
, NULL
);
2146 md_number_to_chars (f
, insn
->insn_code
, insn
->insn_nios2_opcode
->size
);
2148 /* We leave fixup generation to md_convert_frag. */
2152 /* Output a call sequence. Since calls are not pc-relative for NIOS2,
2153 but are page-relative, we cannot tell at any stage in assembly
2154 whether a call will be out of range since a section may be linked
2155 at any address. So if we are relaxing, we convert all call instructions
2156 to long call sequences, and rely on the linker to relax them back to
2159 output_call (nios2_insn_infoS
*insn
)
2161 /* This allocates enough space for the instruction
2162 and puts it in the current frag. */
2163 char *f
= frag_more (12);
2164 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
2166 md_number_to_chars (f
,
2167 (MATCH_R1_ORHI
| SET_IW_I_B (AT_REGNUM
)
2170 dwarf2_emit_insn (4);
2171 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
, 4,
2172 &reloc
->reloc_expression
, 0, BFD_RELOC_NIOS2_HI16
);
2173 md_number_to_chars (f
+ 4,
2174 (MATCH_R1_ORI
| SET_IW_I_B (AT_REGNUM
)
2175 | SET_IW_I_A (AT_REGNUM
)),
2177 dwarf2_emit_insn (4);
2178 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
+ 4, 4,
2179 &reloc
->reloc_expression
, 0, BFD_RELOC_NIOS2_LO16
);
2180 md_number_to_chars (f
+ 8, MATCH_R1_CALLR
| SET_IW_R_A (AT_REGNUM
), 4);
2181 dwarf2_emit_insn (4);
2184 /* Output a movhi/addi pair for the movia pseudo-op. */
2186 output_movia (nios2_insn_infoS
*insn
)
2188 /* This allocates enough space for the instruction
2189 and puts it in the current frag. */
2190 char *f
= frag_more (8);
2191 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
2192 unsigned long reg_index
= GET_IW_I_B (insn
->insn_code
);
2194 /* If the reloc is NULL, there was an error assembling the movia. */
2197 md_number_to_chars (f
, insn
->insn_code
, 4);
2198 dwarf2_emit_insn (4);
2199 fix_new (frag_now
, f
- frag_now
->fr_literal
, 4,
2200 reloc
->reloc_expression
.X_add_symbol
,
2201 reloc
->reloc_expression
.X_add_number
, 0,
2202 BFD_RELOC_NIOS2_HIADJ16
);
2203 md_number_to_chars (f
+ 4,
2204 (MATCH_R1_ADDI
| SET_IW_I_A (reg_index
)
2205 | SET_IW_I_B (reg_index
)),
2207 dwarf2_emit_insn (4);
2208 fix_new (frag_now
, f
+ 4 - frag_now
->fr_literal
, 4,
2209 reloc
->reloc_expression
.X_add_symbol
,
2210 reloc
->reloc_expression
.X_add_number
, 0, BFD_RELOC_NIOS2_LO16
);
2216 /** External interfaces. */
2218 /* Update the selected architecture based on ARCH, giving an error if
2219 ARCH is an invalid value. */
2222 nios2_use_arch (const char *arch
)
2224 if (strcmp (arch
, "nios2") == 0 || strcmp (arch
, "r1") == 0)
2226 nios2_architecture
|= EF_NIOS2_ARCH_R1
;
2227 nios2_opcodes
= (struct nios2_opcode
*) nios2_r1_opcodes
;
2228 nios2_num_opcodes
= nios2_num_r1_opcodes
;
2233 else if (strcmp (arch
, "r2") == 0)
2235 nios2_architecture
|= EF_NIOS2_ARCH_R2
;
2236 nios2_opcodes
= (struct nios2_opcode
*) nios2_r2_opcodes
;
2237 nios2_num_opcodes
= nios2_num_r2_opcodes
;
2243 as_bad (_("unknown architecture '%s'"), arch
);
2246 /* The following functions are called by machine-independent parts of
2249 md_parse_option (int c
, char *arg ATTRIBUTE_UNUSED
)
2254 /* Hidden option for self-test mode. */
2255 nios2_mode
= NIOS2_MODE_TEST
;
2257 case OPTION_RELAX_ALL
:
2258 nios2_as_options
.relax
= relax_all
;
2260 case OPTION_NORELAX
:
2261 nios2_as_options
.relax
= relax_none
;
2263 case OPTION_RELAX_SECTION
:
2264 nios2_as_options
.relax
= relax_section
;
2267 target_big_endian
= 1;
2270 target_big_endian
= 0;
2273 nios2_use_arch (arg
);
2283 /* Implement TARGET_FORMAT. We can choose to be big-endian or
2284 little-endian at runtime based on a switch. */
2286 nios2_target_format (void)
2288 return target_big_endian
? "elf32-bignios2" : "elf32-littlenios2";
2291 /* Machine-dependent usage message. */
2293 md_show_usage (FILE *stream
)
2295 fprintf (stream
, " NIOS2 options:\n"
2296 " -relax-all replace all branch and call "
2297 "instructions with jmp and callr sequences\n"
2298 " -relax-section replace identified out of range "
2299 "branches with jmp sequences (default)\n"
2300 " -no-relax do not replace any branches or calls\n"
2301 " -EB force big-endian byte ordering\n"
2302 " -EL force little-endian byte ordering\n"
2303 " -march=ARCH enable instructions from architecture ARCH\n");
2307 /* This function is called once, at assembler startup time.
2308 It should set up all the tables, etc. that the MD part of the
2309 assembler will need. */
2314 const char *inserted
;
2316 switch (nios2_architecture
)
2319 case EF_NIOS2_ARCH_R1
:
2320 bfd_default_set_arch_mach (stdoutput
, bfd_arch_nios2
, bfd_mach_nios2r1
);
2322 case EF_NIOS2_ARCH_R2
:
2323 if (target_big_endian
)
2324 as_fatal (_("Big-endian R2 is not supported."));
2325 bfd_default_set_arch_mach (stdoutput
, bfd_arch_nios2
, bfd_mach_nios2r2
);
2329 /* Create and fill a hashtable for the Nios II opcodes, registers and
2331 nios2_opcode_hash
= hash_new ();
2332 nios2_reg_hash
= hash_new ();
2333 nios2_ps_hash
= hash_new ();
2335 for (i
= 0; i
< nios2_num_opcodes
; ++i
)
2338 = hash_insert (nios2_opcode_hash
, nios2_opcodes
[i
].name
,
2339 (PTR
) & nios2_opcodes
[i
]);
2340 if (inserted
!= NULL
)
2342 fprintf (stderr
, _("internal error: can't hash `%s': %s\n"),
2343 nios2_opcodes
[i
].name
, inserted
);
2344 /* Probably a memory allocation problem? Give up now. */
2345 as_fatal (_("Broken assembler. No assembly attempted."));
2349 for (i
= 0; i
< nios2_num_regs
; ++i
)
2352 = hash_insert (nios2_reg_hash
, nios2_regs
[i
].name
,
2353 (PTR
) & nios2_regs
[i
]);
2354 if (inserted
!= NULL
)
2356 fprintf (stderr
, _("internal error: can't hash `%s': %s\n"),
2357 nios2_regs
[i
].name
, inserted
);
2358 /* Probably a memory allocation problem? Give up now. */
2359 as_fatal (_("Broken assembler. No assembly attempted."));
2364 for (i
= 0; i
< nios2_num_ps_insn_info_structs
; ++i
)
2367 = hash_insert (nios2_ps_hash
, nios2_ps_insn_info_structs
[i
].pseudo_insn
,
2368 (PTR
) & nios2_ps_insn_info_structs
[i
]);
2369 if (inserted
!= NULL
)
2371 fprintf (stderr
, _("internal error: can't hash `%s': %s\n"),
2372 nios2_ps_insn_info_structs
[i
].pseudo_insn
, inserted
);
2373 /* Probably a memory allocation problem? Give up now. */
2374 as_fatal (_("Broken assembler. No assembly attempted."));
2378 /* Assembler option defaults. */
2379 nios2_as_options
.noat
= FALSE
;
2380 nios2_as_options
.nobreak
= FALSE
;
2382 /* Debug information is incompatible with relaxation. */
2383 if (debug_type
!= DEBUG_UNSPECIFIED
)
2384 nios2_as_options
.relax
= relax_none
;
2386 /* Initialize the alignment data. */
2387 nios2_current_align_seg
= now_seg
;
2388 nios2_last_label
= NULL
;
2389 nios2_current_align
= 0;
2393 /* Assembles a single line of Nios II assembly language. */
2395 md_assemble (char *op_str
)
2398 char *op_strdup
= NULL
;
2399 unsigned long saved_pinfo
= 0;
2400 nios2_insn_infoS thisinsn
;
2401 nios2_insn_infoS
*insn
= &thisinsn
;
2403 /* Make sure we are aligned on a 4-byte boundary. */
2404 if (nios2_current_align
< 2)
2405 nios2_align (2, NULL
, nios2_last_label
);
2406 else if (nios2_current_align
> 2)
2407 nios2_current_align
= 2;
2408 nios2_last_label
= NULL
;
2410 /* We don't want to clobber to op_str
2411 because we want to be able to use it in messages. */
2412 op_strdup
= strdup (op_str
);
2413 insn
->insn_tokens
[0] = strtok (op_strdup
, " ");
2414 argstr
= strtok (NULL
, "");
2416 /* Assemble the opcode. */
2417 insn
->insn_nios2_opcode
= nios2_opcode_lookup (insn
->insn_tokens
[0]);
2418 insn
->insn_reloc
= NULL
;
2420 if (insn
->insn_nios2_opcode
!= NULL
)
2422 nios2_ps_insn_infoS
*ps_insn
= NULL
;
2423 /* Set the opcode for the instruction. */
2424 insn
->insn_code
= insn
->insn_nios2_opcode
->match
;
2425 insn
->constant_bits
= 0;
2427 /* Parse the arguments pointed to by argstr. */
2428 if (nios2_mode
== NIOS2_MODE_ASSEMBLE
)
2429 nios2_parse_args (insn
, argstr
, insn
->insn_nios2_opcode
->args
,
2430 (char **) &insn
->insn_tokens
[1]);
2432 nios2_parse_args (insn
, argstr
, insn
->insn_nios2_opcode
->args_test
,
2433 (char **) &insn
->insn_tokens
[1]);
2435 /* We need to preserve the MOVIA macro as this is clobbered by
2436 translate_pseudo_insn. */
2437 if (insn
->insn_nios2_opcode
->pinfo
== NIOS2_INSN_MACRO_MOVIA
)
2438 saved_pinfo
= NIOS2_INSN_MACRO_MOVIA
;
2439 /* If the instruction is an pseudo-instruction, we want to replace it
2440 with its real equivalent, and then continue. */
2441 if ((insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_MACRO
)
2442 == NIOS2_INSN_MACRO
)
2443 ps_insn
= nios2_translate_pseudo_insn (insn
);
2445 /* Assemble the parsed arguments into the instruction word. */
2446 nios2_assemble_args (insn
);
2448 /* Handle relaxation and other transformations. */
2449 if (nios2_as_options
.relax
!= relax_none
2450 && !nios2_as_options
.noat
2451 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_UBRANCH
)
2452 output_ubranch (insn
);
2453 else if (nios2_as_options
.relax
!= relax_none
2454 && !nios2_as_options
.noat
2455 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_CBRANCH
)
2456 output_cbranch (insn
);
2457 else if (nios2_as_options
.relax
== relax_all
2458 && !nios2_as_options
.noat
2459 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_CALL
2461 && ((insn
->insn_reloc
->reloc_type
2462 == BFD_RELOC_NIOS2_CALL26
)
2463 || (insn
->insn_reloc
->reloc_type
2464 == BFD_RELOC_NIOS2_CALL26_NOAT
)))
2466 else if (saved_pinfo
== NIOS2_INSN_MACRO_MOVIA
)
2467 output_movia (insn
);
2471 nios2_cleanup_pseudo_insn (insn
, ps_insn
);
2474 /* Unrecognised instruction - error. */
2475 as_bad (_("unrecognised instruction %s"), insn
->insn_tokens
[0]);
2477 /* Don't leak memory. */
2481 /* Round up section size. */
2483 md_section_align (asection
*seg ATTRIBUTE_UNUSED
, valueT size
)
2485 /* I think byte alignment is fine here. */
2489 /* Implement TC_FORCE_RELOCATION. */
2491 nios2_force_relocation (fixS
*fixp
)
2493 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
2494 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
2495 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_ALIGN
)
2498 return generic_force_reloc (fixp
);
2501 /* Implement tc_fix_adjustable. */
2503 nios2_fix_adjustable (fixS
*fixp
)
2505 if (fixp
->fx_addsy
== NULL
)
2509 /* Prevent all adjustments to global symbols. */
2510 if (OUTPUT_FLAVOR
== bfd_target_elf_flavour
2511 && (S_IS_EXTERNAL (fixp
->fx_addsy
) || S_IS_WEAK (fixp
->fx_addsy
)))
2514 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
2515 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
2518 /* Preserve relocations against symbols with function type. */
2519 if (symbol_get_bfdsym (fixp
->fx_addsy
)->flags
& BSF_FUNCTION
)
2522 /* Don't allow symbols to be discarded on GOT related relocs. */
2523 if (fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOT16
2524 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_CALL16
2525 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_LO
2526 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_HA
2527 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_GD16
2528 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDM16
2529 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDO16
2530 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_IE16
2531 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LE16
2532 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPMOD
2533 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPREL
2534 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_TPREL
2535 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF
2536 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOT_LO
2537 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOT_HA
2538 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_CALL_LO
2539 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_CALL_HA
2546 /* Implement tc_frob_symbol. This is called in adjust_reloc_syms;
2547 it is used to remove *ABS* references from the symbol table. */
2549 nios2_frob_symbol (symbolS
*symp
)
2551 if ((OUTPUT_FLAVOR
== bfd_target_elf_flavour
2552 && symp
== section_symbol (absolute_section
))
2553 || !S_IS_DEFINED (symp
))
2559 /* The function tc_gen_reloc creates a relocation structure for the
2560 fixup fixp, and returns a pointer to it. This structure is passed
2561 to bfd_install_relocation so that it can be written to the object
2562 file for linking. */
2564 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixp
)
2566 arelent
*reloc
= (arelent
*) xmalloc (sizeof (arelent
));
2567 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
2568 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
2570 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
2571 reloc
->addend
= fixp
->fx_offset
; /* fixp->fx_addnumber; */
2575 switch (fixp
->fx_r_type
)
2578 fixp
->fx_r_type
= BFD_RELOC_16_PCREL
;
2580 case BFD_RELOC_NIOS2_LO16
:
2581 fixp
->fx_r_type
= BFD_RELOC_NIOS2_PCREL_LO
;
2583 case BFD_RELOC_NIOS2_HIADJ16
:
2584 fixp
->fx_r_type
= BFD_RELOC_NIOS2_PCREL_HA
;
2591 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
2592 if (reloc
->howto
== NULL
)
2594 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2595 _("can't represent relocation type %s"),
2596 bfd_get_reloc_code_name (fixp
->fx_r_type
));
2598 /* Set howto to a garbage value so that we can keep going. */
2599 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, BFD_RELOC_32
);
2600 gas_assert (reloc
->howto
!= NULL
);
2606 md_pcrel_from (fixS
*fixP ATTRIBUTE_UNUSED
)
2611 /* Called just before the assembler exits. */
2615 /* FIXME - not yet implemented */
2618 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
2619 Otherwise we have no need to default values of symbols. */
2621 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
2624 if (name
[0] == '_' && name
[1] == 'G'
2625 && strcmp (name
, GLOBAL_OFFSET_TABLE_NAME
) == 0)
2629 if (symbol_find (name
))
2630 as_bad ("GOT already in the symbol table");
2632 GOT_symbol
= symbol_new (name
, undefined_section
,
2633 (valueT
) 0, &zero_address_frag
);
2643 /* Implement tc_frob_label. */
2645 nios2_frob_label (symbolS
*lab
)
2647 /* Emit dwarf information. */
2648 dwarf2_emit_label (lab
);
2650 /* Update the label's address with the current output pointer. */
2651 symbol_set_frag (lab
, frag_now
);
2652 S_SET_VALUE (lab
, (valueT
) frag_now_fix ());
2654 /* Record this label for future adjustment after we find out what
2655 kind of data it references, and the required alignment therewith. */
2656 nios2_last_label
= lab
;
2659 /* Implement md_cons_align. */
2661 nios2_cons_align (int size
)
2664 const char *pfill
= NULL
;
2666 while ((size
>>= 1) != 0)
2669 if (subseg_text_p (now_seg
))
2670 pfill
= (const char *) &nop
;
2674 if (nios2_auto_align_on
)
2675 nios2_align (log_size
, pfill
, NULL
);
2677 nios2_last_label
= NULL
;
2680 /* Map 's' to SHF_NIOS2_GPREL. */
2681 /* This is from the Alpha code tc-alpha.c. */
2683 nios2_elf_section_letter (int letter
, char **ptr_msg
)
2686 return SHF_NIOS2_GPREL
;
2688 *ptr_msg
= _("Bad .section directive: want a,s,w,x,M,S,G,T in string");
2692 /* Map SHF_ALPHA_GPREL to SEC_SMALL_DATA. */
2693 /* This is from the Alpha code tc-alpha.c. */
2695 nios2_elf_section_flags (flagword flags
, int attr
, int type ATTRIBUTE_UNUSED
)
2697 if (attr
& SHF_NIOS2_GPREL
)
2698 flags
|= SEC_SMALL_DATA
;
2702 /* Implement TC_PARSE_CONS_EXPRESSION to handle %tls_ldo(...) */
2703 bfd_reloc_code_real_type
2704 nios2_cons (expressionS
*exp
, int size
)
2706 bfd_reloc_code_real_type nios2_tls_ldo_reloc
= BFD_RELOC_NONE
;
2709 if (input_line_pointer
[0] == '%')
2711 if (strprefix (input_line_pointer
+ 1, "tls_ldo"))
2714 as_bad (_("Illegal operands: %%tls_ldo in %d-byte data field"),
2718 input_line_pointer
+= 8;
2719 nios2_tls_ldo_reloc
= BFD_RELOC_NIOS2_TLS_DTPREL
;
2722 if (nios2_tls_ldo_reloc
!= BFD_RELOC_NONE
)
2725 if (input_line_pointer
[0] != '(')
2726 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
2730 char *end
= ++input_line_pointer
;
2733 for (c
= *end
; !is_end_of_line
[c
]; end
++, c
= *end
)
2744 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
2750 if (input_line_pointer
!= end
)
2751 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
2754 input_line_pointer
++;
2756 c
= *input_line_pointer
;
2757 if (! is_end_of_line
[c
] && c
!= ',')
2758 as_bad (_("Illegal operands: garbage after %%tls_ldo()"));
2764 if (nios2_tls_ldo_reloc
== BFD_RELOC_NONE
)
2766 return nios2_tls_ldo_reloc
;
2769 /* Implement HANDLE_ALIGN. */
2771 nios2_handle_align (fragS
*fragp
)
2773 /* If we are expecting to relax in the linker, then we must output a
2774 relocation to tell the linker we are aligning code. */
2775 if (nios2_as_options
.relax
== relax_all
2776 && (fragp
->fr_type
== rs_align
|| fragp
->fr_type
== rs_align_code
)
2777 && fragp
->fr_address
+ fragp
->fr_fix
> 0
2778 && fragp
->fr_offset
> 1
2779 && now_seg
!= bss_section
)
2780 fix_new (fragp
, fragp
->fr_fix
, 0, &abs_symbol
, fragp
->fr_offset
, 0,
2781 BFD_RELOC_NIOS2_ALIGN
);
2784 /* Implement tc_regname_to_dw2regnum, to convert REGNAME to a DWARF-2
2787 nios2_regname_to_dw2regnum (char *regname
)
2789 struct nios2_reg
*r
= nios2_reg_lookup (regname
);
2795 /* Implement tc_cfi_frame_initial_instructions, to initialize the DWARF-2
2796 unwind information for this procedure. */
2798 nios2_frame_initial_instructions (void)
2800 cfi_add_CFA_def_cfa (27, 0);
2804 /* Some special processing for a Nios II ELF file. */
2807 nios2_elf_final_processing (void)
2809 elf_elfheader (stdoutput
)->e_flags
= nios2_architecture
;