gdb/testsuite: fix gdb.trace/signal.exp on x86
[binutils-gdb/blckswan.git] / gas / config / tc-nios2.c
blob2c9fb421b86f75a3423d87d7a5b72a456d55772f
1 /* Altera Nios II assembler.
2 Copyright (C) 2012-2022 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)
11 any later version.
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
21 02110-1301, USA. */
23 #include "as.h"
24 #include "opcode/nios2.h"
25 #include "elf/nios2.h"
26 #include "tc-nios2.h"
27 #include "bfd.h"
28 #include "dwarf2dbg.h"
29 #include "subsegs.h"
30 #include "safe-ctype.h"
31 #include "dw2gencfi.h"
33 #ifndef OBJ_ELF
34 /* We are not supporting any other target so we throw a compile time error. */
35 OBJ_ELF not defined
36 #endif
38 /* We can choose our endianness at run-time, regardless of configuration. */
39 extern int target_big_endian;
41 /* This array holds the chars that always start a comment. If the
42 pre-processor is disabled, these aren't very useful. */
43 const char comment_chars[] = "#";
45 /* This array holds the chars that only start a comment at the beginning of
46 a line. If the line seems to have the form '# 123 filename'
47 .line and .file directives will appear in the pre-processed output. */
48 /* Note that input_file.c hand checks for '#' at the beginning of the
49 first line of the input file. This is because the compiler outputs
50 #NO_APP at the beginning of its output. */
51 /* Also note that C style comments are always supported. */
52 const char line_comment_chars[] = "#";
54 /* This array holds machine specific line separator characters. */
55 const char line_separator_chars[] = ";";
57 /* Chars that can be used to separate mant from exp in floating point nums. */
58 const char EXP_CHARS[] = "eE";
60 /* Chars that mean this number is a floating point constant. */
61 /* As in 0f12.456 */
62 /* or 0d1.2345e12 */
63 const char FLT_CHARS[] = "rRsSfFdDxXpP";
65 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
66 changed in read.c. Ideally it shouldn't have to know about it at all,
67 but nothing is ideal around here. */
69 /* Machine-dependent command-line options. */
71 const char *md_shortopts = "r";
73 struct option md_longopts[] = {
74 #define OPTION_RELAX_ALL (OPTION_MD_BASE + 0)
75 {"relax-all", no_argument, NULL, OPTION_RELAX_ALL},
76 #define OPTION_NORELAX (OPTION_MD_BASE + 1)
77 {"no-relax", no_argument, NULL, OPTION_NORELAX},
78 #define OPTION_RELAX_SECTION (OPTION_MD_BASE + 2)
79 {"relax-section", no_argument, NULL, OPTION_RELAX_SECTION},
80 #define OPTION_EB (OPTION_MD_BASE + 3)
81 {"EB", no_argument, NULL, OPTION_EB},
82 #define OPTION_EL (OPTION_MD_BASE + 4)
83 {"EL", no_argument, NULL, OPTION_EL},
84 #define OPTION_MARCH (OPTION_MD_BASE + 5)
85 {"march", required_argument, NULL, OPTION_MARCH}
88 size_t md_longopts_size = sizeof (md_longopts);
90 /* The assembler supports three different relaxation modes, controlled by
91 command-line options. */
92 typedef enum
94 relax_section = 0,
95 relax_none,
96 relax_all
97 } relax_optionT;
99 /* Struct contains all assembler options set with .set. */
100 static struct
102 /* .set noat -> noat = 1 allows assembly code to use at without warning
103 and macro expansions generate a warning.
104 .set at -> noat = 0, assembly code using at warn but macro expansions
105 do not generate warnings. */
106 bool noat;
108 /* .set nobreak -> nobreak = 1 allows assembly code to use ba,bt without
109 warning.
110 .set break -> nobreak = 0, assembly code using ba,bt warns. */
111 bool nobreak;
113 /* .cmd line option -relax-all allows all branches and calls to be replaced
114 with longer versions.
115 -no-relax inhibits branch/call conversion.
116 The default value is relax_section, which relaxes branches within
117 a section. */
118 relax_optionT relax;
120 } nios2_as_options = {false, false, relax_section};
123 typedef struct nios2_insn_reloc
125 /* Any expression in the instruction is parsed into this field,
126 which is passed to fix_new_exp() to generate a fixup. */
127 expressionS reloc_expression;
129 /* The type of the relocation to be applied. */
130 bfd_reloc_code_real_type reloc_type;
132 /* PC-relative. */
133 unsigned int reloc_pcrel;
135 /* The next relocation to be applied to the instruction. */
136 struct nios2_insn_reloc *reloc_next;
137 } nios2_insn_relocS;
139 /* This struct is used to hold state when assembling instructions. */
140 typedef struct nios2_insn_info
142 /* Assembled instruction. */
143 unsigned long insn_code;
145 /* Constant bits masked into insn_code for self-check mode. */
146 unsigned long constant_bits;
148 /* Pointer to the relevant bit of the opcode table. */
149 const struct nios2_opcode *insn_nios2_opcode;
150 /* After parsing ptrs to the tokens in the instruction fill this array
151 it is terminated with a null pointer (hence the first +1).
152 The second +1 is because in some parts of the code the opcode
153 is not counted as a token, but still placed in this array. */
154 const char *insn_tokens[NIOS2_MAX_INSN_TOKENS + 1 + 1];
156 /* This holds information used to generate fixups
157 and eventually relocations if it is not null. */
158 nios2_insn_relocS *insn_reloc;
159 } nios2_insn_infoS;
162 /* This struct is used to convert Nios II pseudo-ops into the
163 corresponding real op. */
164 typedef struct nios2_ps_insn_info
166 /* Map this pseudo_op... */
167 const char *pseudo_insn;
169 /* ...to this real instruction. */
170 const char *insn;
172 /* Call this function to modify the operands.... */
173 void (*arg_modifer_func) (char ** parsed_args, const char *arg, int num,
174 int start);
176 /* ...with these arguments. */
177 const char *arg_modifier;
178 int num;
179 int index;
181 /* If arg_modifier_func allocates new memory, provide this function
182 to free it afterwards. */
183 void (*arg_cleanup_func) (char **parsed_args, int num, int start);
184 } nios2_ps_insn_infoS;
186 /* Opcode hash table. */
187 static htab_t nios2_opcode_hash = NULL;
188 #define nios2_opcode_lookup(NAME) \
189 ((struct nios2_opcode *) str_hash_find (nios2_opcode_hash, (NAME)))
191 /* Register hash table. */
192 static htab_t nios2_reg_hash = NULL;
193 #define nios2_reg_lookup(NAME) \
194 ((struct nios2_reg *) str_hash_find (nios2_reg_hash, (NAME)))
197 /* Pseudo-op hash table. */
198 static htab_t nios2_ps_hash = NULL;
199 #define nios2_ps_lookup(NAME) \
200 ((nios2_ps_insn_infoS *) str_hash_find (nios2_ps_hash, (NAME)))
202 /* The known current alignment of the current section. */
203 static int nios2_current_align;
204 static segT nios2_current_align_seg;
206 static int nios2_auto_align_on = 1;
208 /* The last seen label in the current section. This is used to auto-align
209 labels preceding instructions. */
210 static symbolS *nios2_last_label;
212 /* If we saw a 16-bit CDX instruction, we can align on 2-byte boundaries
213 instead of 4-bytes. Use this to keep track of the minimum power-of-2
214 alignment. */
215 static int nios2_min_align = 2;
217 #ifdef OBJ_ELF
218 /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
219 symbolS *GOT_symbol;
220 #endif
222 /* The processor architecture value, EF_NIOS2_ARCH_R1 by default. */
223 static int nios2_architecture = EF_NIOS2_ARCH_R1;
226 /** Utility routines. */
227 /* Function md_chars_to_number takes the sequence of
228 bytes in buf and returns the corresponding value
229 in an int. n must be 1, 2 or 4. */
230 static valueT
231 md_chars_to_number (char *buf, int n)
233 int i;
234 valueT val;
236 gas_assert (n == 1 || n == 2 || n == 4);
238 val = 0;
239 if (target_big_endian)
240 for (i = 0; i < n; ++i)
241 val = val | ((valueT) (buf[i] & 0xff) << 8 * (n - (i + 1)));
242 else
243 for (i = 0; i < n; ++i)
244 val = val | ((valueT) (buf[i] & 0xff) << 8 * i);
245 return val;
249 /* This function turns a C long int, short int or char
250 into the series of bytes that represent the number
251 on the target machine. */
252 void
253 md_number_to_chars (char *buf, valueT val, int n)
255 gas_assert (n == 1 || n == 2 || n == 4 || n == 8);
256 if (target_big_endian)
257 number_to_chars_bigendian (buf, val, n);
258 else
259 number_to_chars_littleendian (buf, val, n);
262 /* Turn a string in input_line_pointer into a floating point constant
263 of type TYPE, and store the appropriate bytes in *LITP. The number
264 of LITTLENUMS emitted is stored in *SIZEP. An error message is
265 returned, or NULL on OK. */
266 const char *
267 md_atof (int type, char *litP, int *sizeP)
269 int prec;
270 LITTLENUM_TYPE words[4];
271 char *t;
272 int i;
274 switch (type)
276 case 'f':
277 prec = 2;
278 break;
279 case 'd':
280 prec = 4;
281 break;
282 default:
283 *sizeP = 0;
284 return _("bad call to md_atof");
287 t = atof_ieee (input_line_pointer, type, words);
288 if (t)
289 input_line_pointer = t;
291 *sizeP = prec * 2;
293 if (! target_big_endian)
294 for (i = prec - 1; i >= 0; i--, litP += 2)
295 md_number_to_chars (litP, (valueT) words[i], 2);
296 else
297 for (i = 0; i < prec; i++, litP += 2)
298 md_number_to_chars (litP, (valueT) words[i], 2);
300 return NULL;
303 /* Return true if STR is prefixed with a special relocation operator. */
304 static int
305 nios2_special_relocation_p (const char *str)
307 return (startswith (str, "%lo")
308 || startswith (str, "%hi")
309 || startswith (str, "%hiadj")
310 || startswith (str, "%gprel")
311 || startswith (str, "%got")
312 || startswith (str, "%call")
313 || startswith (str, "%gotoff_lo")
314 || startswith (str, "%gotoff_hiadj")
315 || startswith (str, "%tls_gd")
316 || startswith (str, "%tls_ldm")
317 || startswith (str, "%tls_ldo")
318 || startswith (str, "%tls_ie")
319 || startswith (str, "%tls_le")
320 || startswith (str, "%gotoff"));
324 /* nop fill patterns for text section. */
325 static char const nop_r1[4] = { 0x3a, 0x88, 0x01, 0x00 };
326 static char const nop_r2[4] = { 0x20, 0x00, 0x00, 0xc4 };
327 static char const nop_r2_cdx[2] = { 0x3b, 0x00 };
328 static char const *nop32 = nop_r1;
329 static char const *nop16 = NULL;
331 /* Handles all machine-dependent alignment needs. */
332 static void
333 nios2_align (int log_size, const char *pfill, symbolS *label)
335 int align;
336 long max_alignment = 15;
338 /* The front end is prone to changing segments out from under us
339 temporarily when -g is in effect. */
340 int switched_seg_p = (nios2_current_align_seg != now_seg);
342 align = log_size;
343 if (align > max_alignment)
345 align = max_alignment;
346 as_bad (_("Alignment too large: %d. assumed"), align);
348 else if (align < 0)
350 as_warn (_("Alignment negative: 0 assumed"));
351 align = 0;
354 if (align != 0)
356 if (subseg_text_p (now_seg) && align >= nios2_min_align)
358 /* First, make sure we're on the minimum boundary, in case
359 someone has been putting .byte values the text section. */
360 if (nios2_current_align < nios2_min_align || switched_seg_p)
361 frag_align (nios2_min_align, 0, 0);
363 /* If we might be on a 2-byte boundary, first align to a
364 4-byte boundary using the 2-byte nop as fill. */
365 if (nios2_min_align == 1
366 && align > nios2_min_align
367 && pfill == nop32 )
369 gas_assert (nop16);
370 frag_align_pattern (2, nop16, 2, 0);
373 /* Now fill in the alignment pattern. */
374 if (pfill != NULL)
375 frag_align_pattern (align, pfill, 4, 0);
376 else
377 frag_align (align, 0, 0);
379 else
380 frag_align (align, 0, 0);
382 if (!switched_seg_p)
383 nios2_current_align = align;
385 /* If the last label was in a different section we can't align it. */
386 if (label != NULL && !switched_seg_p)
388 symbolS *sym;
389 int label_seen = false;
390 struct frag *old_frag;
391 valueT old_value;
392 valueT new_value;
394 gas_assert (S_GET_SEGMENT (label) == now_seg);
396 old_frag = symbol_get_frag (label);
397 old_value = S_GET_VALUE (label);
398 new_value = (valueT) frag_now_fix ();
400 /* It is possible to have more than one label at a particular
401 address, especially if debugging is enabled, so we must
402 take care to adjust all the labels at this address in this
403 fragment. To save time we search from the end of the symbol
404 list, backwards, since the symbols we are interested in are
405 almost certainly the ones that were most recently added.
406 Also to save time we stop searching once we have seen at least
407 one matching label, and we encounter a label that is no longer
408 in the target fragment. Note, this search is guaranteed to
409 find at least one match when sym == label, so no special case
410 code is necessary. */
411 for (sym = symbol_lastP; sym != NULL; sym = symbol_previous (sym))
412 if (symbol_get_frag (sym) == old_frag
413 && S_GET_VALUE (sym) == old_value)
415 label_seen = true;
416 symbol_set_frag (sym, frag_now);
417 S_SET_VALUE (sym, new_value);
419 else if (label_seen && symbol_get_frag (sym) != old_frag)
420 break;
422 record_alignment (now_seg, align);
427 /** Support for self-check mode. */
429 /* Mode of the assembler. */
430 typedef enum
432 NIOS2_MODE_ASSEMBLE, /* Ordinary operation. */
433 NIOS2_MODE_TEST /* Hidden mode used for self testing. */
434 } NIOS2_MODE;
436 static NIOS2_MODE nios2_mode = NIOS2_MODE_ASSEMBLE;
438 /* This function is used to in self-checking mode
439 to check the assembled instruction
440 opcode should be the assembled opcode, and exp_opcode
441 the parsed string representing the expected opcode. */
442 static void
443 nios2_check_assembly (unsigned int opcode, const char *exp_opcode)
445 if (nios2_mode == NIOS2_MODE_TEST)
447 if (exp_opcode == NULL)
448 as_bad (_("expecting opcode string in self test mode"));
449 else if (opcode != strtoul (exp_opcode, NULL, 16))
450 as_bad (_("assembly 0x%08x, expected %s"), opcode, exp_opcode);
455 /** Support for machine-dependent assembler directives. */
456 /* Handle the .align pseudo-op. This aligns to a power of two. It
457 also adjusts any current instruction label. We treat this the same
458 way the MIPS port does: .align 0 turns off auto alignment. */
459 static void
460 s_nios2_align (int ignore ATTRIBUTE_UNUSED)
462 int align;
463 char fill;
464 const char *pfill = NULL;
465 long max_alignment = 15;
467 align = get_absolute_expression ();
468 if (align > max_alignment)
470 align = max_alignment;
471 as_bad (_("Alignment too large: %d. assumed"), align);
473 else if (align < 0)
475 as_warn (_("Alignment negative: 0 assumed"));
476 align = 0;
479 if (*input_line_pointer == ',')
481 input_line_pointer++;
482 fill = get_absolute_expression ();
483 pfill = (const char *) &fill;
485 else if (subseg_text_p (now_seg))
486 pfill = (const char *) nop32;
487 else
489 pfill = NULL;
490 nios2_last_label = NULL;
493 if (align != 0)
495 nios2_auto_align_on = 1;
496 nios2_align (align, pfill, nios2_last_label);
497 nios2_last_label = NULL;
499 else
500 nios2_auto_align_on = 0;
502 demand_empty_rest_of_line ();
505 /* Handle the .text pseudo-op. This is like the usual one, but it
506 clears the saved last label and resets known alignment. */
507 static void
508 s_nios2_text (int i)
510 s_text (i);
511 nios2_last_label = NULL;
512 nios2_current_align = 0;
513 nios2_current_align_seg = now_seg;
516 /* Handle the .data pseudo-op. This is like the usual one, but it
517 clears the saved last label and resets known alignment. */
518 static void
519 s_nios2_data (int i)
521 s_data (i);
522 nios2_last_label = NULL;
523 nios2_current_align = 0;
524 nios2_current_align_seg = now_seg;
527 /* Handle the .section pseudo-op. This is like the usual one, but it
528 clears the saved last label and resets known alignment. */
529 static void
530 s_nios2_section (int ignore)
532 obj_elf_section (ignore);
533 nios2_last_label = NULL;
534 nios2_current_align = 0;
535 nios2_current_align_seg = now_seg;
538 /* Explicitly unaligned cons. */
539 static void
540 s_nios2_ucons (int nbytes)
542 int hold;
543 hold = nios2_auto_align_on;
544 nios2_auto_align_on = 0;
545 cons (nbytes);
546 nios2_auto_align_on = hold;
549 /* Handle the .sdata directive. */
550 static void
551 s_nios2_sdata (int ignore ATTRIBUTE_UNUSED)
553 get_absolute_expression (); /* Ignored. */
554 subseg_new (".sdata", 0);
555 demand_empty_rest_of_line ();
558 /* .set sets assembler options eg noat/at and is also used
559 to set symbol values (.equ, .equiv ). */
560 static void
561 s_nios2_set (int equiv)
563 char *save = input_line_pointer;
564 char *directive;
565 char delim = get_symbol_name (&directive);
566 char *endline = input_line_pointer;
568 (void) restore_line_pointer (delim);
570 /* We only want to handle ".set XXX" if the
571 user has tried ".set XXX, YYY" they are not
572 trying a directive. This prevents
573 us from polluting the name space. */
574 SKIP_WHITESPACE ();
575 if (is_end_of_line[(unsigned char) *input_line_pointer])
577 bool done = true;
578 *endline = 0;
580 if (!strcmp (directive, "noat"))
581 nios2_as_options.noat = true;
582 else if (!strcmp (directive, "at"))
583 nios2_as_options.noat = false;
584 else if (!strcmp (directive, "nobreak"))
585 nios2_as_options.nobreak = true;
586 else if (!strcmp (directive, "break"))
587 nios2_as_options.nobreak = false;
588 else if (!strcmp (directive, "norelax"))
589 nios2_as_options.relax = relax_none;
590 else if (!strcmp (directive, "relaxsection"))
591 nios2_as_options.relax = relax_section;
592 else if (!strcmp (directive, "relaxall"))
593 nios2_as_options.relax = relax_all;
594 else
595 done = false;
597 if (done)
599 *endline = delim;
600 demand_empty_rest_of_line ();
601 return;
605 /* If we fall through to here, either we have ".set XXX, YYY"
606 or we have ".set XXX" where XXX is unknown or we have
607 a syntax error. */
608 input_line_pointer = save;
609 s_set (equiv);
612 /* Machine-dependent assembler directives.
613 Format of each entry is:
614 { "directive", handler_func, param } */
615 const pseudo_typeS md_pseudo_table[] = {
616 {"align", s_nios2_align, 0},
617 {"text", s_nios2_text, 0},
618 {"data", s_nios2_data, 0},
619 {"section", s_nios2_section, 0},
620 {"section.s", s_nios2_section, 0},
621 {"sect", s_nios2_section, 0},
622 {"sect.s", s_nios2_section, 0},
623 /* .dword and .half are included for compatibility with MIPS. */
624 {"dword", cons, 8},
625 {"half", cons, 2},
626 /* NIOS2 native word size is 4 bytes, so we override
627 the GAS default of 2. */
628 {"word", cons, 4},
629 /* Explicitly unaligned directives. */
630 {"2byte", s_nios2_ucons, 2},
631 {"4byte", s_nios2_ucons, 4},
632 {"8byte", s_nios2_ucons, 8},
633 {"16byte", s_nios2_ucons, 16},
634 #ifdef OBJ_ELF
635 {"sdata", s_nios2_sdata, 0},
636 #endif
637 {"set", s_nios2_set, 0},
638 {NULL, NULL, 0}
642 /** Relaxation support. */
644 /* We support two relaxation modes: a limited PC-relative mode with
645 -relax-section (the default), and an absolute jump mode with -relax-all.
647 Nios II PC-relative branch instructions only support 16-bit offsets.
648 And, there's no good way to add a 32-bit constant to the PC without
649 using two registers.
651 To deal with this, for the pc-relative relaxation mode we convert
652 br label
653 into a series of 16-bit adds, like:
654 nextpc at
655 addi at, at, 32767
657 addi at, at, remainder
658 jmp at
660 Similarly, conditional branches are converted from
661 b(condition) r, s, label
662 into a series like:
663 b(opposite condition) r, s, skip
664 nextpc at
665 addi at, at, 32767
667 addi at, at, remainder
668 jmp at
669 skip:
671 The compiler can do a better job, either by converting the branch
672 directly into a JMP (going through the GOT for PIC) or by allocating
673 a second register for the 32-bit displacement.
675 For the -relax-all relaxation mode, the conversions are
676 movhi at, %hi(symbol+offset)
677 ori at, %lo(symbol+offset)
678 jmp at
680 b(opposite condition), r, s, skip
681 movhi at, %hi(symbol+offset)
682 ori at, %lo(symbol+offset)
683 jmp at
684 skip:
685 respectively.
687 16-bit CDX branch instructions are relaxed first into equivalent
688 32-bit branches and then the above transformations are applied
689 if necessary.
693 /* Arbitrarily limit the number of addis we can insert; we need to be able
694 to specify the maximum growth size for each frag that contains a
695 relaxable branch. There's no point in specifying a huge number here
696 since that means the assembler needs to allocate that much extra
697 memory for every branch, and almost no real code will ever need it.
698 Plus, as already noted a better solution is to just use a jmp, or
699 allocate a second register to hold a 32-bit displacement.
700 FIXME: Rather than making this a constant, it could be controlled by
701 a command-line argument. */
702 #define RELAX_MAX_ADDI 32
704 /* The fr_subtype field represents the target-specific relocation state.
705 It has type relax_substateT (unsigned int). We use it to track the
706 number of addis necessary, plus a bit to track whether this is a
707 conditional branch and a bit for 16-bit CDX instructions.
708 Regardless of the smaller RELAX_MAX_ADDI limit, we reserve 16 bits
709 in the fr_subtype to encode the number of addis so that the whole
710 theoretically-valid range is representable.
711 For the -relax-all mode, N = 0 represents an in-range branch and N = 1
712 represents a branch that needs to be relaxed. */
713 #define UBRANCH (0 << 16)
714 #define CBRANCH (1 << 16)
715 #define CDXBRANCH (1 << 17)
716 #define IS_CBRANCH(SUBTYPE) ((SUBTYPE) & CBRANCH)
717 #define IS_UBRANCH(SUBTYPE) (!IS_CBRANCH (SUBTYPE))
718 #define IS_CDXBRANCH(SUBTYPE) ((SUBTYPE) & CDXBRANCH)
719 #define UBRANCH_SUBTYPE(N) (UBRANCH | (N))
720 #define CBRANCH_SUBTYPE(N) (CBRANCH | (N))
721 #define CDX_UBRANCH_SUBTYPE(N) (CDXBRANCH | UBRANCH | (N))
722 #define CDX_CBRANCH_SUBTYPE(N) (CDXBRANCH | CBRANCH | (N))
723 #define SUBTYPE_ADDIS(SUBTYPE) ((SUBTYPE) & 0xffff)
725 /* For the -relax-section mode, unconditional branches require 2 extra
726 instructions besides the addis, conditional branches require 3. */
727 #define UBRANCH_ADDIS_TO_SIZE(N) (((N) + 2) * 4)
728 #define CBRANCH_ADDIS_TO_SIZE(N) (((N) + 3) * 4)
730 /* For the -relax-all mode, unconditional branches require 3 instructions
731 and conditional branches require 4. */
732 #define UBRANCH_JUMP_SIZE 12
733 #define CBRANCH_JUMP_SIZE 16
735 /* Maximum sizes of relaxation sequences. */
736 #define UBRANCH_MAX_SIZE \
737 (nios2_as_options.relax == relax_all \
738 ? UBRANCH_JUMP_SIZE \
739 : UBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
740 #define CBRANCH_MAX_SIZE \
741 (nios2_as_options.relax == relax_all \
742 ? CBRANCH_JUMP_SIZE \
743 : CBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
745 /* Register number of AT, the assembler temporary. */
746 #define AT_REGNUM 1
748 /* Determine how many bytes are required to represent the sequence
749 indicated by SUBTYPE. */
750 static int
751 nios2_relax_subtype_size (relax_substateT subtype)
753 int n = SUBTYPE_ADDIS (subtype);
754 if (n == 0)
755 /* Regular conditional/unconditional branch instruction. */
756 return (IS_CDXBRANCH (subtype) ? 2 : 4);
757 else if (nios2_as_options.relax == relax_all)
758 return (IS_CBRANCH (subtype) ? CBRANCH_JUMP_SIZE : UBRANCH_JUMP_SIZE);
759 else if (IS_CBRANCH (subtype))
760 return CBRANCH_ADDIS_TO_SIZE (n);
761 else
762 return UBRANCH_ADDIS_TO_SIZE (n);
765 /* Estimate size of fragp before relaxation.
766 This could also examine the offset in fragp and adjust
767 fragp->fr_subtype, but we will do that in nios2_relax_frag anyway. */
769 md_estimate_size_before_relax (fragS *fragp, segT segment ATTRIBUTE_UNUSED)
771 return nios2_relax_subtype_size (fragp->fr_subtype);
774 /* Implement md_relax_frag, returning the change in size of the frag. */
775 long
776 nios2_relax_frag (segT segment, fragS *fragp, long stretch)
778 addressT target = fragp->fr_offset;
779 relax_substateT subtype = fragp->fr_subtype;
780 symbolS *symbolp = fragp->fr_symbol;
782 if (symbolp)
784 fragS *sym_frag = symbol_get_frag (symbolp);
785 offsetT offset;
786 int n;
787 bool is_cdx = false;
789 target += S_GET_VALUE (symbolp);
791 /* See comments in write.c:relax_frag about handling of stretch. */
792 if (stretch != 0
793 && sym_frag->relax_marker != fragp->relax_marker)
795 if (stretch < 0 || sym_frag->region == fragp->region)
796 target += stretch;
797 else if (target < fragp->fr_address)
798 target = fragp->fr_next->fr_address + stretch;
801 /* We subtract fr_var (4 for 32-bit insns) because all pc relative
802 branches are from the next instruction. */
803 offset = target - fragp->fr_address - fragp->fr_fix - fragp->fr_var;
804 if (IS_CDXBRANCH (subtype) && IS_UBRANCH (subtype)
805 && offset >= -1024 && offset < 1024)
806 /* PC-relative CDX branch with 11-bit offset. */
807 is_cdx = true;
808 else if (IS_CDXBRANCH (subtype) && IS_CBRANCH (subtype)
809 && offset >= -128 && offset < 128)
810 /* PC-relative CDX branch with 8-bit offset. */
811 is_cdx = true;
812 else if (offset >= -32768 && offset < 32768)
813 /* Fits in PC-relative branch. */
814 n = 0;
815 else if (nios2_as_options.relax == relax_all)
816 /* Convert to jump. */
817 n = 1;
818 else if (nios2_as_options.relax == relax_section
819 && S_GET_SEGMENT (symbolp) == segment
820 && S_IS_DEFINED (symbolp))
821 /* Attempt a PC-relative relaxation on a branch to a defined
822 symbol in the same segment. */
824 /* The relaxation for conditional branches is offset by 4
825 bytes because we insert the inverted branch around the
826 sequence. */
827 if (IS_CBRANCH (subtype))
828 offset = offset - 4;
829 if (offset > 0)
830 n = offset / 32767 + 1;
831 else
832 n = offset / -32768 + 1;
834 /* Bail out immediately if relaxation has failed. If we try to
835 defer the diagnostic to md_convert_frag, some pathological test
836 cases (e.g. gcc/testsuite/gcc.c-torture/compile/20001226-1.c)
837 apparently never converge. By returning 0 here we could pretend
838 to the caller that nothing has changed, but that leaves things
839 in an inconsistent state when we get to md_convert_frag. */
840 if (n > RELAX_MAX_ADDI)
842 as_bad_where (fragp->fr_file, fragp->fr_line,
843 _("branch offset out of range\n"));
844 as_fatal (_("branch relaxation failed\n"));
847 else
848 /* We cannot handle this case, diagnose overflow later. */
849 return 0;
851 if (is_cdx)
852 fragp->fr_subtype = subtype;
853 else if (IS_CBRANCH (subtype))
854 fragp->fr_subtype = CBRANCH_SUBTYPE (n);
855 else
856 fragp->fr_subtype = UBRANCH_SUBTYPE (n);
858 return (nios2_relax_subtype_size (fragp->fr_subtype)
859 - nios2_relax_subtype_size (subtype));
862 /* If we got here, it's probably an error. */
863 return 0;
867 /* Complete fragp using the data from the relaxation pass. */
868 void
869 md_convert_frag (bfd *headers ATTRIBUTE_UNUSED, segT segment ATTRIBUTE_UNUSED,
870 fragS *fragp)
872 char *buffer = fragp->fr_literal + fragp->fr_fix;
873 relax_substateT subtype = fragp->fr_subtype;
874 int n = SUBTYPE_ADDIS (subtype);
875 addressT target = fragp->fr_offset;
876 symbolS *symbolp = fragp->fr_symbol;
877 offsetT offset;
878 unsigned int addend_mask, addi_mask, op;
879 offsetT addend, remainder;
880 int i;
881 bool is_r2 = (bfd_get_mach (stdoutput) == bfd_mach_nios2r2);
883 /* If this is a CDX branch we're not relaxing, just generate the fixup. */
884 if (IS_CDXBRANCH (subtype))
886 gas_assert (is_r2);
887 fix_new (fragp, fragp->fr_fix, 2, fragp->fr_symbol,
888 fragp->fr_offset, 1,
889 (IS_UBRANCH (subtype)
890 ? BFD_RELOC_NIOS2_R2_I10_1_PCREL
891 : BFD_RELOC_NIOS2_R2_T1I7_1_PCREL));
892 fragp->fr_fix += 2;
893 return;
896 /* If this is a CDX branch we are relaxing, turn it into an equivalent
897 32-bit branch and then fall through to the normal non-CDX cases. */
898 if (fragp->fr_var == 2)
900 unsigned int opcode = md_chars_to_number (buffer, 2);
901 gas_assert (is_r2);
902 if (IS_CBRANCH (subtype))
904 unsigned int reg = nios2_r2_reg3_mappings[GET_IW_T1I7_A3 (opcode)];
905 if (GET_IW_R2_OP (opcode) == R2_OP_BNEZ_N)
906 opcode = MATCH_R2_BNE | SET_IW_F2I16_A (reg);
907 else
908 opcode = MATCH_R2_BEQ | SET_IW_F2I16_A (reg);
910 else
911 opcode = MATCH_R2_BR;
912 md_number_to_chars (buffer, opcode, 4);
913 fragp->fr_var = 4;
916 /* If we didn't or can't relax, this is a regular branch instruction.
917 We just need to generate the fixup for the symbol and offset. */
918 if (n == 0)
920 fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol,
921 fragp->fr_offset, 1, BFD_RELOC_16_PCREL);
922 fragp->fr_fix += 4;
923 return;
926 /* Replace the cbranch at fr_fix with one that has the opposite condition
927 in order to jump around the block of instructions we'll be adding. */
928 if (IS_CBRANCH (subtype))
930 unsigned int br_opcode;
931 unsigned int old_op, new_op;
932 int nbytes;
934 /* Account for the nextpc and jmp in the pc-relative case, or the two
935 load instructions and jump in the absolute case. */
936 if (nios2_as_options.relax == relax_section)
937 nbytes = (n + 2) * 4;
938 else
939 nbytes = 12;
941 br_opcode = md_chars_to_number (buffer, 4);
942 if (is_r2)
944 old_op = GET_IW_R2_OP (br_opcode);
945 switch (old_op)
947 case R2_OP_BEQ:
948 new_op = R2_OP_BNE;
949 break;
950 case R2_OP_BNE:
951 new_op = R2_OP_BEQ;
952 break;
953 case R2_OP_BGE:
954 new_op = R2_OP_BLT;
955 break;
956 case R2_OP_BGEU:
957 new_op = R2_OP_BLTU;
958 break;
959 case R2_OP_BLT:
960 new_op = R2_OP_BGE;
961 break;
962 case R2_OP_BLTU:
963 new_op = R2_OP_BGEU;
964 break;
965 default:
966 abort ();
968 br_opcode = ((br_opcode & ~IW_R2_OP_SHIFTED_MASK)
969 | SET_IW_R2_OP (new_op));
970 br_opcode = br_opcode | SET_IW_F2I16_IMM16 (nbytes);
972 else
974 old_op = GET_IW_R1_OP (br_opcode);
975 switch (old_op)
977 case R1_OP_BEQ:
978 new_op = R1_OP_BNE;
979 break;
980 case R1_OP_BNE:
981 new_op = R1_OP_BEQ;
982 break;
983 case R1_OP_BGE:
984 new_op = R1_OP_BLT;
985 break;
986 case R1_OP_BGEU:
987 new_op = R1_OP_BLTU;
988 break;
989 case R1_OP_BLT:
990 new_op = R1_OP_BGE;
991 break;
992 case R1_OP_BLTU:
993 new_op = R1_OP_BGEU;
994 break;
995 default:
996 abort ();
998 br_opcode = ((br_opcode & ~IW_R1_OP_SHIFTED_MASK)
999 | SET_IW_R1_OP (new_op));
1000 br_opcode = br_opcode | SET_IW_I_IMM16 (nbytes);
1002 md_number_to_chars (buffer, br_opcode, 4);
1003 fragp->fr_fix += 4;
1004 buffer += 4;
1007 /* Load at for the PC-relative case. */
1008 if (nios2_as_options.relax == relax_section)
1010 /* Insert the nextpc instruction. */
1011 if (is_r2)
1012 op = MATCH_R2_NEXTPC | SET_IW_F3X6L5_C (AT_REGNUM);
1013 else
1014 op = MATCH_R1_NEXTPC | SET_IW_R_C (AT_REGNUM);
1015 md_number_to_chars (buffer, op, 4);
1016 fragp->fr_fix += 4;
1017 buffer += 4;
1019 /* We need to know whether the offset is positive or negative. */
1020 target += S_GET_VALUE (symbolp);
1021 offset = target - fragp->fr_address - fragp->fr_fix;
1022 if (offset > 0)
1023 addend = 32767;
1024 else
1025 addend = -32768;
1026 if (is_r2)
1027 addend_mask = SET_IW_F2I16_IMM16 ((unsigned int)addend);
1028 else
1029 addend_mask = SET_IW_I_IMM16 ((unsigned int)addend);
1031 /* Insert n-1 addi instructions. */
1032 if (is_r2)
1033 addi_mask = (MATCH_R2_ADDI
1034 | SET_IW_F2I16_B (AT_REGNUM)
1035 | SET_IW_F2I16_A (AT_REGNUM));
1036 else
1037 addi_mask = (MATCH_R1_ADDI
1038 | SET_IW_I_B (AT_REGNUM)
1039 | SET_IW_I_A (AT_REGNUM));
1040 for (i = 0; i < n - 1; i ++)
1042 md_number_to_chars (buffer, addi_mask | addend_mask, 4);
1043 fragp->fr_fix += 4;
1044 buffer += 4;
1047 /* Insert the last addi instruction to hold the remainder. */
1048 remainder = offset - addend * (n - 1);
1049 gas_assert (remainder >= -32768 && remainder <= 32767);
1050 if (is_r2)
1051 addend_mask = SET_IW_F2I16_IMM16 ((unsigned int)remainder);
1052 else
1053 addend_mask = SET_IW_I_IMM16 ((unsigned int)remainder);
1054 md_number_to_chars (buffer, addi_mask | addend_mask, 4);
1055 fragp->fr_fix += 4;
1056 buffer += 4;
1059 /* Load at for the absolute case. */
1060 else
1062 if (is_r2)
1063 op = MATCH_R2_ORHI | SET_IW_F2I16_B (AT_REGNUM) | SET_IW_F2I16_A (0);
1064 else
1065 op = MATCH_R1_ORHI | SET_IW_I_B (AT_REGNUM) | SET_IW_I_A (0);
1066 md_number_to_chars (buffer, op, 4);
1067 fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol, fragp->fr_offset,
1068 0, BFD_RELOC_NIOS2_HI16);
1069 fragp->fr_fix += 4;
1070 buffer += 4;
1071 if (is_r2)
1072 op = (MATCH_R2_ORI | SET_IW_F2I16_B (AT_REGNUM)
1073 | SET_IW_F2I16_A (AT_REGNUM));
1074 else
1075 op = (MATCH_R1_ORI | SET_IW_I_B (AT_REGNUM)
1076 | SET_IW_I_A (AT_REGNUM));
1077 md_number_to_chars (buffer, op, 4);
1078 fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol, fragp->fr_offset,
1079 0, BFD_RELOC_NIOS2_LO16);
1080 fragp->fr_fix += 4;
1081 buffer += 4;
1084 /* Insert the jmp instruction. */
1085 if (is_r2)
1086 op = MATCH_R2_JMP | SET_IW_F3X6L5_A (AT_REGNUM);
1087 else
1088 op = MATCH_R1_JMP | SET_IW_R_A (AT_REGNUM);
1089 md_number_to_chars (buffer, op, 4);
1090 fragp->fr_fix += 4;
1091 buffer += 4;
1095 /** Fixups and overflow checking. */
1097 /* Check a fixup for overflow. */
1098 static bool
1099 nios2_check_overflow (valueT fixup, reloc_howto_type *howto)
1101 /* If there is a rightshift, check that the low-order bits are
1102 zero before applying it. */
1103 if (howto->rightshift)
1105 if ((~(~((valueT) 0) << howto->rightshift) & fixup)
1106 && howto->complain_on_overflow != complain_overflow_dont)
1107 return true;
1108 fixup = ((signed)fixup) >> howto->rightshift;
1111 /* Check for overflow - return TRUE if overflow, FALSE if not. */
1112 switch (howto->complain_on_overflow)
1114 case complain_overflow_dont:
1115 break;
1116 case complain_overflow_bitfield:
1117 if ((fixup >> howto->bitsize) != 0
1118 && ((signed) fixup >> howto->bitsize) != -1)
1119 return true;
1120 break;
1121 case complain_overflow_signed:
1122 if ((fixup & 0x80000000) > 0)
1124 /* Check for negative overflow. */
1125 if ((signed) fixup < (signed) (~0U << (howto->bitsize - 1)))
1126 return true;
1128 else
1130 /* Check for positive overflow. */
1131 if (fixup >= ((unsigned) 1 << (howto->bitsize - 1)))
1132 return true;
1134 break;
1135 case complain_overflow_unsigned:
1136 if ((fixup >> howto->bitsize) != 0)
1137 return true;
1138 break;
1139 default:
1140 as_bad (_("error checking for overflow - broken assembler"));
1141 break;
1143 return false;
1146 /* Emit diagnostic for fixup overflow. */
1147 static void
1148 nios2_diagnose_overflow (valueT fixup, reloc_howto_type *howto,
1149 fixS *fixP, valueT value)
1151 if (fixP->fx_r_type == BFD_RELOC_8
1152 || fixP->fx_r_type == BFD_RELOC_16
1153 || fixP->fx_r_type == BFD_RELOC_32)
1154 /* These relocs are against data, not instructions. */
1155 as_bad_where (fixP->fx_file, fixP->fx_line,
1156 _("immediate value 0x%x truncated to 0x%x"),
1157 (unsigned int) fixup,
1158 (unsigned int) (~(~(valueT) 0 << howto->bitsize) & fixup));
1159 else
1161 /* What opcode is the instruction? This will determine
1162 whether we check for overflow in immediate values
1163 and what error message we get. */
1164 const struct nios2_opcode *opcode;
1165 enum overflow_type overflow_msg_type;
1166 unsigned int range_min;
1167 unsigned int range_max;
1168 unsigned int address;
1170 opcode = nios2_find_opcode_hash (value, bfd_get_mach (stdoutput));
1171 gas_assert (opcode);
1172 gas_assert (fixP->fx_size == opcode->size);
1173 overflow_msg_type = opcode->overflow_msg;
1174 switch (overflow_msg_type)
1176 case call_target_overflow:
1177 range_min
1178 = ((fixP->fx_frag->fr_address + fixP->fx_where) & 0xf0000000);
1179 range_max = range_min + 0x0fffffff;
1180 address = fixup | range_min;
1182 as_bad_where (fixP->fx_file, fixP->fx_line,
1183 _("call target address 0x%08x out of range 0x%08x to 0x%08x"),
1184 address, range_min, range_max);
1185 break;
1186 case branch_target_overflow:
1187 if (opcode->format == iw_i_type || opcode->format == iw_F2I16_type)
1188 as_bad_where (fixP->fx_file, fixP->fx_line,
1189 _("branch offset %d out of range %d to %d"),
1190 (int)fixup, -32768, 32767);
1191 else
1192 as_bad_where (fixP->fx_file, fixP->fx_line,
1193 _("branch offset %d out of range"),
1194 (int)fixup);
1195 break;
1196 case address_offset_overflow:
1197 if (opcode->format == iw_i_type || opcode->format == iw_F2I16_type)
1198 as_bad_where (fixP->fx_file, fixP->fx_line,
1199 _("%s offset %d out of range %d to %d"),
1200 opcode->name, (int)fixup, -32768, 32767);
1201 else
1202 as_bad_where (fixP->fx_file, fixP->fx_line,
1203 _("%s offset %d out of range"),
1204 opcode->name, (int)fixup);
1205 break;
1206 case signed_immed16_overflow:
1207 as_bad_where (fixP->fx_file, fixP->fx_line,
1208 _("immediate value %d out of range %d to %d"),
1209 (int)fixup, -32768, 32767);
1210 break;
1211 case unsigned_immed16_overflow:
1212 as_bad_where (fixP->fx_file, fixP->fx_line,
1213 _("immediate value %u out of range %u to %u"),
1214 (unsigned int)fixup, 0, 65535);
1215 break;
1216 case unsigned_immed5_overflow:
1217 as_bad_where (fixP->fx_file, fixP->fx_line,
1218 _("immediate value %u out of range %u to %u"),
1219 (unsigned int)fixup, 0, 31);
1220 break;
1221 case signed_immed12_overflow:
1222 as_bad_where (fixP->fx_file, fixP->fx_line,
1223 _("immediate value %d out of range %d to %d"),
1224 (int)fixup, -2048, 2047);
1225 break;
1226 case custom_opcode_overflow:
1227 as_bad_where (fixP->fx_file, fixP->fx_line,
1228 _("custom instruction opcode %u out of range %u to %u"),
1229 (unsigned int)fixup, 0, 255);
1230 break;
1231 default:
1232 as_bad_where (fixP->fx_file, fixP->fx_line,
1233 _("overflow in immediate argument"));
1234 break;
1239 /* Apply a fixup to the object file. */
1240 void
1241 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
1243 /* Assert that the fixup is one we can handle. */
1244 gas_assert (fixP != NULL && valP != NULL
1245 && (fixP->fx_r_type == BFD_RELOC_8
1246 || fixP->fx_r_type == BFD_RELOC_16
1247 || fixP->fx_r_type == BFD_RELOC_32
1248 || fixP->fx_r_type == BFD_RELOC_64
1249 || fixP->fx_r_type == BFD_RELOC_NIOS2_S16
1250 || fixP->fx_r_type == BFD_RELOC_NIOS2_U16
1251 || fixP->fx_r_type == BFD_RELOC_16_PCREL
1252 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL26
1253 || fixP->fx_r_type == BFD_RELOC_NIOS2_IMM5
1254 || fixP->fx_r_type == BFD_RELOC_NIOS2_CACHE_OPX
1255 || fixP->fx_r_type == BFD_RELOC_NIOS2_IMM6
1256 || fixP->fx_r_type == BFD_RELOC_NIOS2_IMM8
1257 || fixP->fx_r_type == BFD_RELOC_NIOS2_HI16
1258 || fixP->fx_r_type == BFD_RELOC_NIOS2_LO16
1259 || fixP->fx_r_type == BFD_RELOC_NIOS2_HIADJ16
1260 || fixP->fx_r_type == BFD_RELOC_NIOS2_GPREL
1261 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1262 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
1263 || fixP->fx_r_type == BFD_RELOC_NIOS2_UJMP
1264 || fixP->fx_r_type == BFD_RELOC_NIOS2_CJMP
1265 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALLR
1266 || fixP->fx_r_type == BFD_RELOC_NIOS2_ALIGN
1267 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOT16
1268 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL16
1269 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_LO
1270 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_HA
1271 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_GD16
1272 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LDM16
1273 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LDO16
1274 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_IE16
1275 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LE16
1276 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF
1277 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPREL
1278 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL26_NOAT
1279 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOT_LO
1280 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOT_HA
1281 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL_LO
1282 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL_HA
1283 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_S12
1284 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_I10_1_PCREL
1285 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T1I7_1_PCREL
1286 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T1I7_2
1287 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T2I4
1288 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T2I4_1
1289 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T2I4_2
1290 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_X1I7_2
1291 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_X2L5
1292 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_F1I5_2
1293 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_L5I4X1
1294 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T1X1I6
1295 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T1X1I6_2
1296 /* Add other relocs here as we generate them. */
1299 if (fixP->fx_r_type == BFD_RELOC_64)
1301 /* We may reach here due to .8byte directives, but we never output
1302 BFD_RELOC_64; it must be resolved. */
1303 if (fixP->fx_addsy != NULL)
1304 as_bad_where (fixP->fx_file, fixP->fx_line,
1305 _("cannot create 64-bit relocation"));
1306 else
1308 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1309 *valP, 8);
1310 fixP->fx_done = 1;
1312 return;
1315 /* The value passed in valP can be the value of a fully
1316 resolved expression, or it can be the value of a partially
1317 resolved expression. In the former case, both fixP->fx_addsy
1318 and fixP->fx_subsy are NULL, and fixP->fx_offset == *valP, and
1319 we can fix up the instruction that fixP relates to.
1320 In the latter case, one or both of fixP->fx_addsy and
1321 fixP->fx_subsy are not NULL, and fixP->fx_offset may or may not
1322 equal *valP. We don't need to check for fixP->fx_subsy being null
1323 because the generic part of the assembler generates an error if
1324 it is not an absolute symbol. */
1325 if (fixP->fx_addsy != NULL)
1326 /* Partially resolved expression. */
1328 fixP->fx_addnumber = fixP->fx_offset;
1329 fixP->fx_done = 0;
1331 switch (fixP->fx_r_type)
1333 case BFD_RELOC_NIOS2_TLS_GD16:
1334 case BFD_RELOC_NIOS2_TLS_LDM16:
1335 case BFD_RELOC_NIOS2_TLS_LDO16:
1336 case BFD_RELOC_NIOS2_TLS_IE16:
1337 case BFD_RELOC_NIOS2_TLS_LE16:
1338 case BFD_RELOC_NIOS2_TLS_DTPMOD:
1339 case BFD_RELOC_NIOS2_TLS_DTPREL:
1340 case BFD_RELOC_NIOS2_TLS_TPREL:
1341 S_SET_THREAD_LOCAL (fixP->fx_addsy);
1342 break;
1343 default:
1344 break;
1347 else
1348 /* Fully resolved fixup. */
1350 reloc_howto_type *howto
1351 = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
1353 if (howto == NULL)
1354 as_bad_where (fixP->fx_file, fixP->fx_line,
1355 _("relocation is not supported"));
1356 else
1358 valueT fixup = *valP;
1359 valueT value;
1360 char *buf;
1362 /* If this is a pc-relative relocation, we need to
1363 subtract the current offset within the object file
1364 FIXME : for some reason fixP->fx_pcrel isn't 1 when it should be
1365 so I'm using the howto structure instead to determine this. */
1366 if (howto->pc_relative == 1)
1368 fixup = (fixup - (fixP->fx_frag->fr_address + fixP->fx_where
1369 + fixP->fx_size));
1370 *valP = fixup;
1373 /* Get the instruction or data to be fixed up. */
1374 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
1375 value = md_chars_to_number (buf, fixP->fx_size);
1377 /* Check for overflow, emitting a diagnostic if necessary. */
1378 if (nios2_check_overflow (fixup, howto))
1379 nios2_diagnose_overflow (fixup, howto, fixP, value);
1381 /* Apply the right shift. */
1382 fixup = (offsetT) fixup >> howto->rightshift;
1384 /* Truncate the fixup to right size. */
1385 switch (fixP->fx_r_type)
1387 case BFD_RELOC_NIOS2_HI16:
1388 fixup = (fixup >> 16) & 0xFFFF;
1389 break;
1390 case BFD_RELOC_NIOS2_LO16:
1391 fixup = fixup & 0xFFFF;
1392 break;
1393 case BFD_RELOC_NIOS2_HIADJ16:
1394 fixup = ((fixup + 0x8000) >> 16) & 0xFFFF;
1395 break;
1396 default:
1398 fixup &= ((valueT) 2 << (howto->bitsize - 1)) - 1;
1399 break;
1403 /* Fix up the instruction. */
1404 value = (value & ~howto->dst_mask) | (fixup << howto->bitpos);
1405 md_number_to_chars (buf, value, fixP->fx_size);
1408 fixP->fx_done = 1;
1411 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
1413 fixP->fx_done = 0;
1414 if (fixP->fx_addsy
1415 && !S_IS_DEFINED (fixP->fx_addsy) && !S_IS_WEAK (fixP->fx_addsy))
1416 S_SET_WEAK (fixP->fx_addsy);
1418 else if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1419 fixP->fx_done = 0;
1424 /** Instruction parsing support. */
1426 /* General internal error routine. */
1428 static void
1429 bad_opcode (const struct nios2_opcode *op)
1431 fprintf (stderr, _("internal error: broken opcode descriptor for `%s %s'\n"),
1432 op->name, op->args);
1433 as_fatal (_("Broken assembler. No assembly attempted."));
1436 /* Special relocation directive strings. */
1438 struct nios2_special_relocS
1440 const char *string;
1441 bfd_reloc_code_real_type reloc_type;
1444 /* This table is sorted so that prefix strings are listed after the longer
1445 strings that include them -- e.g., %got after %got_hiadj, etc. */
1447 struct nios2_special_relocS nios2_special_reloc[] = {
1448 {"%hiadj", BFD_RELOC_NIOS2_HIADJ16},
1449 {"%hi", BFD_RELOC_NIOS2_HI16},
1450 {"%lo", BFD_RELOC_NIOS2_LO16},
1451 {"%gprel", BFD_RELOC_NIOS2_GPREL},
1452 {"%call_lo", BFD_RELOC_NIOS2_CALL_LO},
1453 {"%call_hiadj", BFD_RELOC_NIOS2_CALL_HA},
1454 {"%call", BFD_RELOC_NIOS2_CALL16},
1455 {"%gotoff_lo", BFD_RELOC_NIOS2_GOTOFF_LO},
1456 {"%gotoff_hiadj", BFD_RELOC_NIOS2_GOTOFF_HA},
1457 {"%gotoff", BFD_RELOC_NIOS2_GOTOFF},
1458 {"%got_hiadj", BFD_RELOC_NIOS2_GOT_HA},
1459 {"%got_lo", BFD_RELOC_NIOS2_GOT_LO},
1460 {"%got", BFD_RELOC_NIOS2_GOT16},
1461 {"%tls_gd", BFD_RELOC_NIOS2_TLS_GD16},
1462 {"%tls_ldm", BFD_RELOC_NIOS2_TLS_LDM16},
1463 {"%tls_ldo", BFD_RELOC_NIOS2_TLS_LDO16},
1464 {"%tls_ie", BFD_RELOC_NIOS2_TLS_IE16},
1465 {"%tls_le", BFD_RELOC_NIOS2_TLS_LE16},
1468 #define NIOS2_NUM_SPECIAL_RELOCS \
1469 (sizeof(nios2_special_reloc)/sizeof(nios2_special_reloc[0]))
1470 const int nios2_num_special_relocs = NIOS2_NUM_SPECIAL_RELOCS;
1472 /* Creates a new nios2_insn_relocS and returns a pointer to it. */
1473 static nios2_insn_relocS *
1474 nios2_insn_reloc_new (bfd_reloc_code_real_type reloc_type, unsigned int pcrel)
1476 nios2_insn_relocS *retval;
1477 retval = XNEW (nios2_insn_relocS);
1478 if (retval == NULL)
1480 as_bad (_("can't create relocation"));
1481 abort ();
1484 /* Fill out the fields with default values. */
1485 retval->reloc_next = NULL;
1486 retval->reloc_type = reloc_type;
1487 retval->reloc_pcrel = pcrel;
1488 return retval;
1491 /* Frees up memory previously allocated by nios2_insn_reloc_new(). */
1492 /* FIXME: this is never called; memory leak? */
1493 #if 0
1494 static void
1495 nios2_insn_reloc_destroy (nios2_insn_relocS *reloc)
1497 gas_assert (reloc != NULL);
1498 free (reloc);
1500 #endif
1502 /* Look up a register name and validate it for the given regtype.
1503 Return the register mapping or NULL on failure. */
1504 static struct nios2_reg *
1505 nios2_parse_reg (const char *token, unsigned long regtype)
1507 struct nios2_reg *reg = nios2_reg_lookup (token);
1509 if (reg == NULL)
1511 as_bad (_("unknown register %s"), token);
1512 return NULL;
1515 /* Matched a register, but is it the wrong type? */
1516 if (!(regtype & reg->regtype))
1518 if (regtype & REG_CONTROL)
1519 as_bad (_("expecting control register"));
1520 else if (reg->regtype & REG_CONTROL)
1521 as_bad (_("illegal use of control register"));
1522 else if (reg->regtype & REG_COPROCESSOR)
1523 as_bad (_("illegal use of coprocessor register"));
1524 else
1525 as_bad (_("invalid register %s"), token);
1526 return NULL;
1529 /* Warn for explicit use of special registers. */
1530 if (reg->regtype & REG_NORMAL)
1532 if (!nios2_as_options.noat && reg->index == 1)
1533 as_warn (_("Register at (r1) can sometimes be corrupted by "
1534 "assembler optimizations.\n"
1535 "Use .set noat to turn off those optimizations "
1536 "(and this warning)."));
1537 if (!nios2_as_options.nobreak && reg->index == 25)
1538 as_warn (_("The debugger will corrupt bt (r25).\n"
1539 "If you don't need to debug this "
1540 "code use .set nobreak to turn off this warning."));
1541 if (!nios2_as_options.nobreak && reg->index == 30)
1542 as_warn (_("The debugger will corrupt sstatus/ba (r30).\n"
1543 "If you don't need to debug this "
1544 "code use .set nobreak to turn off this warning."));
1547 return reg;
1550 /* This function parses a reglist for ldwm/stwm and push.n/pop.n
1551 instructions, given as a brace-enclosed register list. The tokenizer
1552 has replaced commas in the token with spaces.
1553 The return value is a bitmask of registers in the set. It also
1554 sets nios2_reglist_mask and nios2_reglist_dir to allow error checking
1555 when parsing the base register. */
1557 static unsigned long nios2_reglist_mask;
1558 static int nios2_reglist_dir;
1560 static unsigned long
1561 nios2_parse_reglist (char *token, const struct nios2_opcode *op)
1563 unsigned long mask = 0;
1564 int dir = 0;
1565 unsigned long regtype = 0;
1566 int last = -1;
1567 const char *regname;
1569 nios2_reglist_mask = 0;
1570 nios2_reglist_dir = 0;
1572 if (op->match == MATCH_R2_LDWM || op->match == MATCH_R2_STWM)
1574 regtype = REG_LDWM;
1575 dir = 0;
1577 else if (op->match == MATCH_R2_PUSH_N)
1579 regtype = REG_POP;
1580 dir = -1;
1582 else if (op->match == MATCH_R2_POP_N)
1584 regtype = REG_POP;
1585 dir = 1;
1587 else
1588 bad_opcode (op);
1590 for (regname = strtok (token, "{ }");
1591 regname;
1592 regname = strtok (NULL, "{ }"))
1594 int regno;
1595 struct nios2_reg *reg = nios2_parse_reg (regname, regtype);
1597 if (!reg)
1598 break;
1599 regno = reg->index;
1601 /* Make sure registers are listed in proper sequence. */
1602 if (last >= 0)
1604 if (regno == last)
1606 as_bad ("duplicate register %s\n", reg->name);
1607 return 0;
1609 else if (dir == 0)
1610 dir = (regno < last ? -1 : 1);
1611 else if ((dir > 0 && regno < last)
1612 || (dir < 0 && regno > last)
1613 || (op->match == MATCH_R2_PUSH_N
1614 && ! ((last == 31 && regno == 28)
1615 || (last == 31 && regno <= 23)
1616 || (last == 28 && regno <= 23)
1617 || (regno < 23 && regno == last - 1)))
1618 || (op->match == MATCH_R2_POP_N
1619 && ! ((regno == 31 && last == 28)
1620 || (regno == 31 && last <= 23)
1621 || (regno == 28 && last <= 23)
1622 || (last < 23 && last == regno - 1))))
1624 as_bad ("invalid register order");
1625 return 0;
1629 mask |= 1UL << regno;
1630 last = regno;
1633 /* Check that all ldwm/stwm regs belong to the same set. */
1634 if ((op->match == MATCH_R2_LDWM || op->match == MATCH_R2_STWM)
1635 && (mask & 0x00003ffc) && (mask & 0x90ffc000))
1637 as_bad ("invalid register set in reglist");
1638 return 0;
1641 /* Check that push.n/pop.n regs include RA. */
1642 if ((op->match == MATCH_R2_PUSH_N || op->match == MATCH_R2_POP_N)
1643 && ((mask & 0x80000000) == 0))
1645 as_bad ("reglist must include ra (r31)");
1646 return 0;
1649 /* Check that there is at least one register in the set. */
1650 if (!mask)
1652 as_bad ("reglist must include at least one register");
1653 return 0;
1656 /* OK, reglist passed validation. */
1657 nios2_reglist_mask = mask;
1658 nios2_reglist_dir = dir;
1659 return mask;
1662 /* This function parses the base register and options used by the ldwm/stwm
1663 instructions. Returns the base register and sets the option arguments
1664 accordingly. On failure, returns NULL. */
1665 static struct nios2_reg *
1666 nios2_parse_base_register (char *str, int *direction, int *writeback, int *ret)
1668 char *regname;
1669 struct nios2_reg *reg;
1671 *direction = 0;
1672 *writeback = 0;
1673 *ret = 0;
1675 /* Check for --. */
1676 if (startswith (str, "--"))
1678 str += 2;
1679 *direction -= 1;
1682 /* Extract the base register. */
1683 if (*str != '(')
1685 as_bad ("expected '(' before base register");
1686 return NULL;
1688 str++;
1689 regname = str;
1690 str = strchr (str, ')');
1691 if (!str)
1693 as_bad ("expected ')' after base register");
1694 return NULL;
1696 *str = '\0';
1697 str++;
1698 reg = nios2_parse_reg (regname, REG_NORMAL);
1699 if (reg == NULL)
1700 return NULL;
1702 /* Check for ++. */
1703 if (startswith (str, "++"))
1705 str += 2;
1706 *direction += 1;
1709 /* Ensure that either -- or ++ is specified, but not both. */
1710 if (*direction == 0)
1712 as_bad ("invalid base register syntax");
1713 return NULL;;
1716 /* Check for options. The tokenizer has replaced commas with spaces. */
1717 while (*str)
1719 while (*str == ' ')
1720 str++;
1721 if (startswith (str, "writeback"))
1723 *writeback = 1;
1724 str += 9;
1726 else if (startswith (str, "ret"))
1728 *ret = 1;
1729 str += 3;
1731 else if (*str)
1733 as_bad ("invalid option syntax");
1734 return NULL;
1738 return reg;
1742 /* The various nios2_assemble_* functions call this
1743 function to generate an expression from a string representing an expression.
1744 It then tries to evaluate the expression, and if it can, returns its value.
1745 If not, it creates a new nios2_insn_relocS and stores the expression and
1746 reloc_type for future use. */
1747 static unsigned long
1748 nios2_assemble_expression (const char *exprstr,
1749 nios2_insn_infoS *insn,
1750 bfd_reloc_code_real_type orig_reloc_type,
1751 unsigned int pcrel)
1753 nios2_insn_relocS *reloc;
1754 char *saved_line_ptr;
1755 unsigned long value = 0;
1756 int i;
1757 bfd_reloc_code_real_type reloc_type = orig_reloc_type;
1759 gas_assert (exprstr != NULL);
1760 gas_assert (insn != NULL);
1762 /* Check for relocation operators.
1763 Change the relocation type and advance the ptr to the start of
1764 the expression proper. */
1765 for (i = 0; i < nios2_num_special_relocs; i++)
1766 if (strstr (exprstr, nios2_special_reloc[i].string) != NULL)
1768 reloc_type = nios2_special_reloc[i].reloc_type;
1769 exprstr += strlen (nios2_special_reloc[i].string) + 1;
1771 /* %lo and %hiadj have different meanings for PC-relative
1772 expressions. */
1773 if (pcrel)
1775 if (reloc_type == BFD_RELOC_NIOS2_LO16)
1776 reloc_type = BFD_RELOC_NIOS2_PCREL_LO;
1777 if (reloc_type == BFD_RELOC_NIOS2_HIADJ16)
1778 reloc_type = BFD_RELOC_NIOS2_PCREL_HA;
1781 break;
1784 /* No relocation allowed; we must have a constant expression. */
1785 if (orig_reloc_type == BFD_RELOC_NONE)
1787 expressionS exp;
1789 /* Parse the expression string. */
1790 saved_line_ptr = input_line_pointer;
1791 input_line_pointer = (char *) exprstr;
1792 expression (&exp);
1793 input_line_pointer = saved_line_ptr;
1795 /* If we don't have a constant, give an error. */
1796 if (reloc_type != orig_reloc_type || exp.X_op != O_constant)
1797 as_bad (_("expression must be constant"));
1798 else
1799 value = exp.X_add_number;
1800 return (unsigned long) value;
1803 /* We potentially have a relocation. */
1804 reloc = nios2_insn_reloc_new (reloc_type, pcrel);
1805 reloc->reloc_next = insn->insn_reloc;
1806 insn->insn_reloc = reloc;
1808 /* Parse the expression string. */
1809 saved_line_ptr = input_line_pointer;
1810 input_line_pointer = (char *) exprstr;
1811 expression (&reloc->reloc_expression);
1812 input_line_pointer = saved_line_ptr;
1814 /* This is redundant as the fixup will put this into
1815 the instruction, but it is included here so that
1816 self-test mode (-r) works. */
1817 if (nios2_mode == NIOS2_MODE_TEST
1818 && reloc->reloc_expression.X_op == O_constant)
1819 value = reloc->reloc_expression.X_add_number;
1821 return (unsigned long) value;
1824 /* Encode a 3-bit register number, giving an error if this is not possible. */
1825 static unsigned int
1826 nios2_assemble_reg3 (const char *token)
1828 struct nios2_reg *reg = nios2_parse_reg (token, REG_3BIT);
1829 int j;
1831 if (reg == NULL)
1832 return 0;
1834 for (j = 0; j < nios2_num_r2_reg3_mappings; j++)
1835 if (nios2_r2_reg3_mappings[j] == reg->index)
1836 return j;
1838 /* Should never get here if we passed validation. */
1839 as_bad (_("invalid register %s"), token);
1840 return 0;
1843 /* Argument assemble functions. */
1846 /* Control register index. */
1847 static void
1848 nios2_assemble_arg_c (const char *token, nios2_insn_infoS *insn)
1850 struct nios2_reg *reg = nios2_parse_reg (token, REG_CONTROL);
1851 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1853 if (reg == NULL)
1854 return;
1856 switch (op->format)
1858 case iw_r_type:
1859 insn->insn_code |= SET_IW_R_IMM5 (reg->index);
1860 break;
1861 case iw_F3X6L5_type:
1862 insn->insn_code |= SET_IW_F3X6L5_IMM5 (reg->index);
1863 break;
1864 default:
1865 bad_opcode (op);
1869 /* Destination register. */
1870 static void
1871 nios2_assemble_arg_d (const char *token, nios2_insn_infoS *insn)
1873 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1874 unsigned long regtype = REG_NORMAL;
1875 struct nios2_reg *reg;
1877 if (op->format == iw_custom_type || op->format == iw_F3X8_type)
1878 regtype |= REG_COPROCESSOR;
1879 reg = nios2_parse_reg (token, regtype);
1880 if (reg == NULL)
1881 return;
1883 switch (op->format)
1885 case iw_r_type:
1886 insn->insn_code |= SET_IW_R_C (reg->index);
1887 break;
1888 case iw_custom_type:
1889 insn->insn_code |= SET_IW_CUSTOM_C (reg->index);
1890 if (reg->regtype & REG_COPROCESSOR)
1891 insn->insn_code |= SET_IW_CUSTOM_READC (0);
1892 else
1893 insn->insn_code |= SET_IW_CUSTOM_READC (1);
1894 break;
1895 case iw_F3X6L5_type:
1896 case iw_F3X6_type:
1897 insn->insn_code |= SET_IW_F3X6L5_C (reg->index);
1898 break;
1899 case iw_F3X8_type:
1900 insn->insn_code |= SET_IW_F3X8_C (reg->index);
1901 if (reg->regtype & REG_COPROCESSOR)
1902 insn->insn_code |= SET_IW_F3X8_READC (0);
1903 else
1904 insn->insn_code |= SET_IW_F3X8_READC (1);
1905 break;
1906 case iw_F2_type:
1907 insn->insn_code |= SET_IW_F2_B (reg->index);
1908 break;
1909 default:
1910 bad_opcode (op);
1914 /* Source register 1. */
1915 static void
1916 nios2_assemble_arg_s (const char *token, nios2_insn_infoS *insn)
1918 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1919 unsigned long regtype = REG_NORMAL;
1920 struct nios2_reg *reg;
1922 if (op->format == iw_custom_type || op->format == iw_F3X8_type)
1923 regtype |= REG_COPROCESSOR;
1924 reg = nios2_parse_reg (token, regtype);
1925 if (reg == NULL)
1926 return;
1928 switch (op->format)
1930 case iw_r_type:
1931 if (op->match == MATCH_R1_JMP && reg->index == 31)
1932 as_bad (_("r31 cannot be used with jmp; use ret instead"));
1933 insn->insn_code |= SET_IW_R_A (reg->index);
1934 break;
1935 case iw_i_type:
1936 insn->insn_code |= SET_IW_I_A (reg->index);
1937 break;
1938 case iw_custom_type:
1939 insn->insn_code |= SET_IW_CUSTOM_A (reg->index);
1940 if (reg->regtype & REG_COPROCESSOR)
1941 insn->insn_code |= SET_IW_CUSTOM_READA (0);
1942 else
1943 insn->insn_code |= SET_IW_CUSTOM_READA (1);
1944 break;
1945 case iw_F2I16_type:
1946 insn->insn_code |= SET_IW_F2I16_A (reg->index);
1947 break;
1948 case iw_F2X4I12_type:
1949 insn->insn_code |= SET_IW_F2X4I12_A (reg->index);
1950 break;
1951 case iw_F1X4I12_type:
1952 insn->insn_code |= SET_IW_F1X4I12_A (reg->index);
1953 break;
1954 case iw_F1X4L17_type:
1955 insn->insn_code |= SET_IW_F1X4L17_A (reg->index);
1956 break;
1957 case iw_F3X6L5_type:
1958 case iw_F3X6_type:
1959 if (op->match == MATCH_R2_JMP && reg->index == 31)
1960 as_bad (_("r31 cannot be used with jmp; use ret instead"));
1961 insn->insn_code |= SET_IW_F3X6L5_A (reg->index);
1962 break;
1963 case iw_F2X6L10_type:
1964 insn->insn_code |= SET_IW_F2X6L10_A (reg->index);
1965 break;
1966 case iw_F3X8_type:
1967 insn->insn_code |= SET_IW_F3X8_A (reg->index);
1968 if (reg->regtype & REG_COPROCESSOR)
1969 insn->insn_code |= SET_IW_F3X8_READA (0);
1970 else
1971 insn->insn_code |= SET_IW_F3X8_READA (1);
1972 break;
1973 case iw_F1X1_type:
1974 if (op->match == MATCH_R2_JMPR_N && reg->index == 31)
1975 as_bad (_("r31 cannot be used with jmpr.n; use ret.n instead"));
1976 insn->insn_code |= SET_IW_F1X1_A (reg->index);
1977 break;
1978 case iw_F1I5_type:
1979 /* Implicit stack pointer reference. */
1980 if (reg->index != 27)
1981 as_bad (_("invalid register %s"), token);
1982 break;
1983 case iw_F2_type:
1984 insn->insn_code |= SET_IW_F2_A (reg->index);
1985 break;
1986 default:
1987 bad_opcode (op);
1991 /* Source register 2. */
1992 static void
1993 nios2_assemble_arg_t (const char *token, nios2_insn_infoS *insn)
1995 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1996 unsigned long regtype = REG_NORMAL;
1997 struct nios2_reg *reg;
1999 if (op->format == iw_custom_type || op->format == iw_F3X8_type)
2000 regtype |= REG_COPROCESSOR;
2001 reg = nios2_parse_reg (token, regtype);
2002 if (reg == NULL)
2003 return;
2005 switch (op->format)
2007 case iw_r_type:
2008 insn->insn_code |= SET_IW_R_B (reg->index);
2009 break;
2010 case iw_i_type:
2011 insn->insn_code |= SET_IW_I_B (reg->index);
2012 break;
2013 case iw_custom_type:
2014 insn->insn_code |= SET_IW_CUSTOM_B (reg->index);
2015 if (reg->regtype & REG_COPROCESSOR)
2016 insn->insn_code |= SET_IW_CUSTOM_READB (0);
2017 else
2018 insn->insn_code |= SET_IW_CUSTOM_READB (1);
2019 break;
2020 case iw_F2I16_type:
2021 insn->insn_code |= SET_IW_F2I16_B (reg->index);
2022 break;
2023 case iw_F2X4I12_type:
2024 insn->insn_code |= SET_IW_F2X4I12_B (reg->index);
2025 break;
2026 case iw_F3X6L5_type:
2027 case iw_F3X6_type:
2028 insn->insn_code |= SET_IW_F3X6L5_B (reg->index);
2029 break;
2030 case iw_F2X6L10_type:
2031 insn->insn_code |= SET_IW_F2X6L10_B (reg->index);
2032 break;
2033 case iw_F3X8_type:
2034 insn->insn_code |= SET_IW_F3X8_B (reg->index);
2035 if (reg->regtype & REG_COPROCESSOR)
2036 insn->insn_code |= SET_IW_F3X8_READB (0);
2037 else
2038 insn->insn_code |= SET_IW_F3X8_READB (1);
2039 break;
2040 case iw_F1I5_type:
2041 insn->insn_code |= SET_IW_F1I5_B (reg->index);
2042 break;
2043 case iw_F2_type:
2044 insn->insn_code |= SET_IW_F2_B (reg->index);
2045 break;
2046 case iw_T1X1I6_type:
2047 /* Implicit zero register reference. */
2048 if (reg->index != 0)
2049 as_bad (_("invalid register %s"), token);
2050 break;
2052 default:
2053 bad_opcode (op);
2057 /* Destination register w/3-bit encoding. */
2058 static void
2059 nios2_assemble_arg_D (const char *token, nios2_insn_infoS *insn)
2061 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2062 int reg = nios2_assemble_reg3 (token);
2064 switch (op->format)
2066 case iw_T1I7_type:
2067 insn->insn_code |= SET_IW_T1I7_A3 (reg);
2068 break;
2069 case iw_T2X1L3_type:
2070 insn->insn_code |= SET_IW_T2X1L3_B3 (reg);
2071 break;
2072 case iw_T2X1I3_type:
2073 insn->insn_code |= SET_IW_T2X1I3_B3 (reg);
2074 break;
2075 case iw_T3X1_type:
2076 insn->insn_code |= SET_IW_T3X1_C3 (reg);
2077 break;
2078 case iw_T2X3_type:
2079 /* Some instructions using this encoding take 3 register arguments,
2080 requiring the destination register to be the same as the first
2081 source register. */
2082 if (op->num_args == 3)
2083 insn->insn_code |= SET_IW_T2X3_A3 (reg);
2084 else
2085 insn->insn_code |= SET_IW_T2X3_B3 (reg);
2086 break;
2087 default:
2088 bad_opcode (op);
2092 /* Source register w/3-bit encoding. */
2093 static void
2094 nios2_assemble_arg_S (const char *token, nios2_insn_infoS *insn)
2096 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2097 int reg = nios2_assemble_reg3 (token);
2099 switch (op->format)
2101 case iw_T1I7_type:
2102 insn->insn_code |= SET_IW_T1I7_A3 (reg);
2103 break;
2104 case iw_T2I4_type:
2105 insn->insn_code |= SET_IW_T2I4_A3 (reg);
2106 break;
2107 case iw_T2X1L3_type:
2108 insn->insn_code |= SET_IW_T2X1L3_A3 (reg);
2109 break;
2110 case iw_T2X1I3_type:
2111 insn->insn_code |= SET_IW_T2X1I3_A3 (reg);
2112 break;
2113 case iw_T3X1_type:
2114 insn->insn_code |= SET_IW_T3X1_A3 (reg);
2115 break;
2116 case iw_T2X3_type:
2117 /* Some instructions using this encoding take 3 register arguments,
2118 requiring the destination register to be the same as the first
2119 source register. */
2120 if (op->num_args == 3)
2122 int dreg = GET_IW_T2X3_A3 (insn->insn_code);
2123 if (dreg != reg)
2124 as_bad ("source and destination registers must be the same");
2126 else
2127 insn->insn_code |= SET_IW_T2X3_A3 (reg);
2128 break;
2129 case iw_T1X1I6_type:
2130 insn->insn_code |= SET_IW_T1X1I6_A3 (reg);
2131 break;
2132 default:
2133 bad_opcode (op);
2137 /* Source register 2 w/3-bit encoding. */
2138 static void
2139 nios2_assemble_arg_T (const char *token, nios2_insn_infoS *insn)
2141 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2142 int reg = nios2_assemble_reg3 (token);
2144 switch (op->format)
2146 case iw_T2I4_type:
2147 insn->insn_code |= SET_IW_T2I4_B3 (reg);
2148 break;
2149 case iw_T3X1_type:
2150 insn->insn_code |= SET_IW_T3X1_B3 (reg);
2151 break;
2152 case iw_T2X3_type:
2153 insn->insn_code |= SET_IW_T2X3_B3 (reg);
2154 break;
2155 default:
2156 bad_opcode (op);
2160 /* 16-bit signed immediate. */
2161 static void
2162 nios2_assemble_arg_i (const char *token, nios2_insn_infoS *insn)
2164 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2165 unsigned int val;
2167 switch (op->format)
2169 case iw_i_type:
2170 val = nios2_assemble_expression (token, insn,
2171 BFD_RELOC_NIOS2_S16, 0);
2172 insn->constant_bits |= SET_IW_I_IMM16 (val);
2173 break;
2174 case iw_F2I16_type:
2175 val = nios2_assemble_expression (token, insn,
2176 BFD_RELOC_NIOS2_S16, 0);
2177 insn->constant_bits |= SET_IW_F2I16_IMM16 (val);
2178 break;
2179 default:
2180 bad_opcode (op);
2184 /* 12-bit signed immediate. */
2185 static void
2186 nios2_assemble_arg_I (const char *token, nios2_insn_infoS *insn)
2188 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2189 unsigned int val;
2191 switch (op->format)
2193 case iw_F2X4I12_type:
2194 val = nios2_assemble_expression (token, insn,
2195 BFD_RELOC_NIOS2_R2_S12, 0);
2196 insn->constant_bits |= SET_IW_F2X4I12_IMM12 (val);
2197 break;
2198 case iw_F1X4I12_type:
2199 val = nios2_assemble_expression (token, insn,
2200 BFD_RELOC_NIOS2_R2_S12, 0);
2201 insn->constant_bits |= SET_IW_F2X4I12_IMM12 (val);
2202 break;
2203 default:
2204 bad_opcode (op);
2208 /* 16-bit unsigned immediate. */
2209 static void
2210 nios2_assemble_arg_u (const char *token, nios2_insn_infoS *insn)
2212 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2213 unsigned int val;
2215 switch (op->format)
2217 case iw_i_type:
2218 val = nios2_assemble_expression (token, insn,
2219 BFD_RELOC_NIOS2_U16, 0);
2220 insn->constant_bits |= SET_IW_I_IMM16 (val);
2221 break;
2222 case iw_F2I16_type:
2223 val = nios2_assemble_expression (token, insn,
2224 BFD_RELOC_NIOS2_U16, 0);
2225 insn->constant_bits |= SET_IW_F2I16_IMM16 (val);
2226 break;
2227 default:
2228 bad_opcode (op);
2232 /* 7-bit unsigned immediate with 2-bit shift. */
2233 static void
2234 nios2_assemble_arg_U (const char *token, nios2_insn_infoS *insn)
2236 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2237 unsigned int val;
2239 switch (op->format)
2241 case iw_T1I7_type:
2242 val = nios2_assemble_expression (token, insn,
2243 BFD_RELOC_NIOS2_R2_T1I7_2, 0);
2244 insn->constant_bits |= SET_IW_T1I7_IMM7 (val >> 2);
2245 break;
2246 case iw_X1I7_type:
2247 val = nios2_assemble_expression (token, insn,
2248 BFD_RELOC_NIOS2_R2_X1I7_2, 0);
2249 insn->constant_bits |= SET_IW_X1I7_IMM7 (val >> 2);
2250 break;
2251 default:
2252 bad_opcode (op);
2256 /* 5-bit unsigned immediate with 2-bit shift. */
2257 static void
2258 nios2_assemble_arg_V (const char *token, nios2_insn_infoS *insn)
2260 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2261 unsigned int val;
2263 switch (op->format)
2265 case iw_F1I5_type:
2266 val = nios2_assemble_expression (token, insn,
2267 BFD_RELOC_NIOS2_R2_F1I5_2, 0);
2268 insn->constant_bits |= SET_IW_F1I5_IMM5 (val >> 2);
2269 break;
2270 default:
2271 bad_opcode (op);
2275 /* 4-bit unsigned immediate with 2-bit shift. */
2276 static void
2277 nios2_assemble_arg_W (const char *token, nios2_insn_infoS *insn)
2279 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2280 unsigned int val;
2282 switch (op->format)
2284 case iw_T2I4_type:
2285 val = nios2_assemble_expression (token, insn,
2286 BFD_RELOC_NIOS2_R2_T2I4_2, 0);
2287 insn->constant_bits |= SET_IW_T2I4_IMM4 (val >> 2);
2288 break;
2289 case iw_L5I4X1_type:
2290 /* This argument is optional for push.n/pop.n, and defaults to
2291 zero if unspecified. */
2292 if (token == NULL)
2293 return;
2295 val = nios2_assemble_expression (token, insn,
2296 BFD_RELOC_NIOS2_R2_L5I4X1, 0);
2297 insn->constant_bits |= SET_IW_L5I4X1_IMM4 (val >> 2);
2298 break;
2299 default:
2300 bad_opcode (op);
2304 /* 4-bit unsigned immediate with 1-bit shift. */
2305 static void
2306 nios2_assemble_arg_X (const char *token, nios2_insn_infoS *insn)
2308 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2309 unsigned int val;
2311 switch (op->format)
2313 case iw_T2I4_type:
2314 val = nios2_assemble_expression (token, insn,
2315 BFD_RELOC_NIOS2_R2_T2I4_1, 0);
2316 insn->constant_bits |= SET_IW_T2I4_IMM4 (val >> 1);
2317 break;
2318 default:
2319 bad_opcode (op);
2323 /* 4-bit unsigned immediate without shift. */
2324 static void
2325 nios2_assemble_arg_Y (const char *token, nios2_insn_infoS *insn)
2327 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2328 unsigned int val;
2330 switch (op->format)
2332 case iw_T2I4_type:
2333 val = nios2_assemble_expression (token, insn,
2334 BFD_RELOC_NIOS2_R2_T2I4, 0);
2335 insn->constant_bits |= SET_IW_T2I4_IMM4 (val);
2336 break;
2337 default:
2338 bad_opcode (op);
2343 /* 16-bit signed immediate address offset. */
2344 static void
2345 nios2_assemble_arg_o (const char *token, nios2_insn_infoS *insn)
2347 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2348 unsigned int val;
2350 switch (op->format)
2352 case iw_i_type:
2353 val = nios2_assemble_expression (token, insn,
2354 BFD_RELOC_16_PCREL, 1);
2355 insn->constant_bits |= SET_IW_I_IMM16 (val);
2356 break;
2357 case iw_F2I16_type:
2358 val = nios2_assemble_expression (token, insn,
2359 BFD_RELOC_16_PCREL, 1);
2360 insn->constant_bits |= SET_IW_F2I16_IMM16 (val);
2361 break;
2362 default:
2363 bad_opcode (op);
2367 /* 10-bit signed address offset with 1-bit shift. */
2368 static void
2369 nios2_assemble_arg_O (const char *token, nios2_insn_infoS *insn)
2371 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2372 unsigned int val;
2374 switch (op->format)
2376 case iw_I10_type:
2377 val = nios2_assemble_expression (token, insn,
2378 BFD_RELOC_NIOS2_R2_I10_1_PCREL, 1);
2379 insn->constant_bits |= SET_IW_I10_IMM10 (val >> 1);
2380 break;
2381 default:
2382 bad_opcode (op);
2386 /* 7-bit signed address offset with 1-bit shift. */
2387 static void
2388 nios2_assemble_arg_P (const char *token, nios2_insn_infoS *insn)
2390 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2391 unsigned int val;
2393 switch (op->format)
2395 case iw_T1I7_type:
2396 val = nios2_assemble_expression (token, insn,
2397 BFD_RELOC_NIOS2_R2_T1I7_1_PCREL, 1);
2398 insn->constant_bits |= SET_IW_T1I7_IMM7 (val >> 1);
2399 break;
2400 default:
2401 bad_opcode (op);
2405 /* 5-bit unsigned immediate. */
2406 static void
2407 nios2_assemble_arg_j (const char *token, nios2_insn_infoS *insn)
2409 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2410 unsigned int val;
2412 switch (op->format)
2414 case iw_r_type:
2415 val = nios2_assemble_expression (token, insn,
2416 BFD_RELOC_NIOS2_IMM5, 0);
2417 insn->constant_bits |= SET_IW_R_IMM5 (val);
2418 break;
2419 case iw_F3X6L5_type:
2420 if (op->match == MATCH_R2_ENI)
2421 /* Value must be constant 0 or 1. */
2423 val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0);
2424 if (val != 0 && val != 1)
2425 as_bad ("invalid eni argument %u", val);
2426 insn->insn_code |= SET_IW_F3X6L5_IMM5 (val);
2428 else
2430 val = nios2_assemble_expression (token, insn,
2431 BFD_RELOC_NIOS2_IMM5, 0);
2432 insn->constant_bits |= SET_IW_F3X6L5_IMM5 (val);
2434 break;
2435 case iw_F2X6L10_type:
2436 /* Only constant expression without relocation permitted for
2437 bit position. */
2438 val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0);
2439 if (val > 31)
2440 as_bad ("invalid bit position %u", val);
2441 insn->insn_code |= SET_IW_F2X6L10_MSB (val);
2442 break;
2443 case iw_X2L5_type:
2444 val = nios2_assemble_expression (token, insn,
2445 BFD_RELOC_NIOS2_R2_X2L5, 0);
2446 insn->constant_bits |= SET_IW_X2L5_IMM5 (val);
2447 break;
2448 default:
2449 bad_opcode (op);
2453 /* Second 5-bit unsigned immediate field. */
2454 static void
2455 nios2_assemble_arg_k (const char *token, nios2_insn_infoS *insn)
2457 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2458 unsigned int val;
2460 switch (op->format)
2462 case iw_F2X6L10_type:
2463 /* Only constant expression without relocation permitted for
2464 bit position. */
2465 val = nios2_assemble_expression (token, insn,
2466 BFD_RELOC_NONE, 0);
2467 if (val > 31)
2468 as_bad ("invalid bit position %u", val);
2469 else if (GET_IW_F2X6L10_MSB (insn->insn_code) < val)
2470 as_bad ("MSB must be greater than or equal to LSB");
2471 insn->insn_code |= SET_IW_F2X6L10_LSB (val);
2472 break;
2473 default:
2474 bad_opcode (op);
2478 /* 8-bit unsigned immediate. */
2479 static void
2480 nios2_assemble_arg_l (const char *token, nios2_insn_infoS *insn)
2482 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2483 unsigned int val;
2485 switch (op->format)
2487 case iw_custom_type:
2488 val = nios2_assemble_expression (token, insn,
2489 BFD_RELOC_NIOS2_IMM8, 0);
2490 insn->constant_bits |= SET_IW_CUSTOM_N (val);
2491 break;
2492 case iw_F3X8_type:
2493 val = nios2_assemble_expression (token, insn,
2494 BFD_RELOC_NIOS2_IMM8, 0);
2495 insn->constant_bits |= SET_IW_F3X8_N (val);
2496 break;
2497 default:
2498 bad_opcode (op);
2502 /* 26-bit unsigned immediate. */
2503 static void
2504 nios2_assemble_arg_m (const char *token, nios2_insn_infoS *insn)
2506 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2507 unsigned int val;
2509 switch (op->format)
2511 case iw_j_type:
2512 val = nios2_assemble_expression (token, insn,
2513 (nios2_as_options.noat
2514 ? BFD_RELOC_NIOS2_CALL26_NOAT
2515 : BFD_RELOC_NIOS2_CALL26),
2517 insn->constant_bits |= SET_IW_J_IMM26 (val);
2518 break;
2519 case iw_L26_type:
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_L26_IMM26 (val);
2526 break;
2527 default:
2528 bad_opcode (op);
2532 /* 6-bit unsigned immediate with no shifting. */
2533 static void
2534 nios2_assemble_arg_M (const char *token, nios2_insn_infoS *insn)
2536 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2537 unsigned int val;
2539 switch (op->format)
2541 case iw_T1X1I6_type:
2542 val = nios2_assemble_expression (token, insn,
2543 BFD_RELOC_NIOS2_R2_T1X1I6, 0);
2544 insn->constant_bits |= SET_IW_T1X1I6_IMM6 (val);
2545 break;
2546 default:
2547 bad_opcode (op);
2551 /* 6-bit unsigned immediate with 2-bit shift. */
2552 static void
2553 nios2_assemble_arg_N (const char *token, nios2_insn_infoS *insn)
2555 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2556 unsigned int val;
2558 switch (op->format)
2560 case iw_T1X1I6_type:
2561 val = nios2_assemble_expression (token, insn,
2562 BFD_RELOC_NIOS2_R2_T1X1I6_2, 0);
2563 insn->constant_bits |= SET_IW_T1X1I6_IMM6 (val >> 2);
2564 break;
2565 default:
2566 bad_opcode (op);
2571 /* Encoded enumeration for addi.n/subi.n. */
2572 static void
2573 nios2_assemble_arg_e (const char *token, nios2_insn_infoS *insn)
2575 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2576 unsigned int val;
2577 int i;
2579 switch (op->format)
2581 case iw_T2X1I3_type:
2582 val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0);
2583 for (i = 0; i < nios2_num_r2_asi_n_mappings; i++)
2584 if (val == nios2_r2_asi_n_mappings[i])
2585 break;
2586 if (i == nios2_num_r2_asi_n_mappings)
2588 as_bad (_("Invalid constant operand %s"), token);
2589 return;
2591 insn->insn_code |= SET_IW_T2X1I3_IMM3 ((unsigned)i);
2592 break;
2593 default:
2594 bad_opcode (op);
2598 /* Encoded enumeration for slli.n/srli.n. */
2599 static void
2600 nios2_assemble_arg_f (const char *token, nios2_insn_infoS *insn)
2602 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2603 unsigned int val;
2604 int i;
2606 switch (op->format)
2608 case iw_T2X1L3_type:
2609 val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0);
2610 for (i = 0; i < nios2_num_r2_shi_n_mappings; i++)
2611 if (val == nios2_r2_shi_n_mappings[i])
2612 break;
2613 if (i == nios2_num_r2_shi_n_mappings)
2615 as_bad (_("Invalid constant operand %s"), token);
2616 return;
2618 insn->insn_code |= SET_IW_T2X1L3_SHAMT ((unsigned)i);
2619 break;
2620 default:
2621 bad_opcode (op);
2625 /* Encoded enumeration for andi.n. */
2626 static void
2627 nios2_assemble_arg_g (const char *token, nios2_insn_infoS *insn)
2629 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2630 unsigned int val;
2631 int i;
2633 switch (op->format)
2635 case iw_T2I4_type:
2636 val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0);
2637 for (i = 0; i < nios2_num_r2_andi_n_mappings; i++)
2638 if (val == nios2_r2_andi_n_mappings[i])
2639 break;
2640 if (i == nios2_num_r2_andi_n_mappings)
2642 as_bad (_("Invalid constant operand %s"), token);
2643 return;
2645 insn->insn_code |= SET_IW_T2I4_IMM4 ((unsigned)i);
2646 break;
2647 default:
2648 bad_opcode (op);
2652 /* Encoded enumeration for movi.n. */
2653 static void
2654 nios2_assemble_arg_h (const char *token, nios2_insn_infoS *insn)
2656 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2657 unsigned int val;
2658 int i;
2660 switch (op->format)
2662 case iw_T1I7_type:
2663 val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0);
2664 i = (signed) val;
2665 if ((signed) i == -1)
2666 val = 127;
2667 else if (i == -2)
2668 val = 126;
2669 else if (i == 0xff)
2670 val = 125;
2671 else if (i < 0 || i > 125)
2673 as_bad (_("Invalid constant operand %s"), token);
2674 return;
2676 insn->insn_code |= SET_IW_T1I7_IMM7 (val);
2677 break;
2678 default:
2679 bad_opcode (op);
2683 /* Encoded REGMASK for ldwm/stwm or push.n/pop.n. */
2684 static void
2685 nios2_assemble_arg_R (const char *token, nios2_insn_infoS *insn)
2687 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2688 unsigned long mask;
2689 char *buf = strdup (token);
2690 unsigned long reglist = nios2_parse_reglist (buf, op);
2691 free (buf);
2693 if (reglist == 0)
2694 return;
2696 switch (op->format)
2698 case iw_F1X4L17_type:
2699 /* Encoding for ldwm/stwm. */
2700 if (reglist & 0x00003ffc)
2701 mask = reglist >> 2;
2702 else
2704 insn->insn_code |= SET_IW_F1X4L17_RS (1);
2705 mask = (reglist & 0x00ffc000) >> 14;
2706 if (reglist & (1 << 28))
2707 mask |= 1 << 10;
2708 if (reglist & (1u << 31))
2709 mask |= 1 << 11;
2711 insn->insn_code |= SET_IW_F1X4L17_REGMASK (mask);
2712 break;
2714 case iw_L5I4X1_type:
2715 /* Encoding for push.n/pop.n. */
2716 if (reglist & (1 << 28))
2717 insn->insn_code |= SET_IW_L5I4X1_FP (1);
2718 mask = reglist & 0x00ff0000;
2719 if (mask)
2721 int i;
2723 for (i = 0; i < nios2_num_r2_reg_range_mappings; i++)
2724 if (nios2_r2_reg_range_mappings[i] == mask)
2725 break;
2726 if (i == nios2_num_r2_reg_range_mappings)
2728 as_bad ("invalid reglist");
2729 return;
2731 insn->insn_code |= SET_IW_L5I4X1_REGRANGE (i);
2732 insn->insn_code |= SET_IW_L5I4X1_CS (1);
2734 break;
2736 default:
2737 bad_opcode (op);
2741 /* Base register for ldwm/stwm. */
2742 static void
2743 nios2_assemble_arg_B (const char *token, nios2_insn_infoS *insn)
2745 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2746 int direction, writeback, ret;
2747 char *str = strdup (token);
2748 struct nios2_reg *reg
2749 = nios2_parse_base_register (str, &direction, &writeback, &ret);
2751 free (str);
2752 if (!reg)
2753 return;
2755 switch (op->format)
2757 case iw_F1X4L17_type:
2758 /* For ldwm, check to see if the base register is already inside the
2759 register list. */
2760 if (op->match == MATCH_R2_LDWM
2761 && (nios2_reglist_mask & (1 << reg->index)))
2763 as_bad ("invalid base register; %s is inside the reglist", reg->name);
2764 return;
2767 /* For stwm, ret option is not allowed. */
2768 if (op->match == MATCH_R2_STWM && ret)
2770 as_bad ("invalid option syntax");
2771 return;
2774 /* Check that the direction matches the ordering of the reglist. */
2775 if (nios2_reglist_dir && direction != nios2_reglist_dir)
2777 as_bad ("reglist order does not match increment/decrement mode");
2778 return;
2781 insn->insn_code |= SET_IW_F1X4L17_A (reg->index);
2782 if (direction > 0)
2783 insn->insn_code |= SET_IW_F1X4L17_ID (1);
2784 if (writeback)
2785 insn->insn_code |= SET_IW_F1X4L17_WB (1);
2786 if (ret)
2787 insn->insn_code |= SET_IW_F1X4L17_PC (1);
2788 break;
2790 default:
2791 bad_opcode (op);
2795 static void
2796 nios2_assemble_args (nios2_insn_infoS *insn)
2798 const struct nios2_opcode *op = insn->insn_nios2_opcode;
2799 const char *argptr;
2800 unsigned int tokidx, ntok;
2802 /* Make sure there are enough arguments. */
2803 ntok = (op->pinfo & NIOS2_INSN_OPTARG) ? op->num_args - 1 : op->num_args;
2804 for (tokidx = 1; tokidx <= ntok; tokidx++)
2805 if (insn->insn_tokens[tokidx] == NULL)
2807 as_bad ("missing argument");
2808 return;
2811 for (argptr = op->args, tokidx = 1;
2812 *argptr && insn->insn_tokens[tokidx];
2813 argptr++)
2814 switch (*argptr)
2816 case ',':
2817 case '(':
2818 case ')':
2819 break;
2821 case 'c':
2822 nios2_assemble_arg_c (insn->insn_tokens[tokidx++], insn);
2823 break;
2825 case 'd':
2826 nios2_assemble_arg_d (insn->insn_tokens[tokidx++], insn);
2827 break;
2829 case 's':
2830 nios2_assemble_arg_s (insn->insn_tokens[tokidx++], insn);
2831 break;
2833 case 't':
2834 nios2_assemble_arg_t (insn->insn_tokens[tokidx++], insn);
2835 break;
2837 case 'D':
2838 nios2_assemble_arg_D (insn->insn_tokens[tokidx++], insn);
2839 break;
2841 case 'S':
2842 nios2_assemble_arg_S (insn->insn_tokens[tokidx++], insn);
2843 break;
2845 case 'T':
2846 nios2_assemble_arg_T (insn->insn_tokens[tokidx++], insn);
2847 break;
2849 case 'i':
2850 nios2_assemble_arg_i (insn->insn_tokens[tokidx++], insn);
2851 break;
2853 case 'I':
2854 nios2_assemble_arg_I (insn->insn_tokens[tokidx++], insn);
2855 break;
2857 case 'u':
2858 nios2_assemble_arg_u (insn->insn_tokens[tokidx++], insn);
2859 break;
2861 case 'U':
2862 nios2_assemble_arg_U (insn->insn_tokens[tokidx++], insn);
2863 break;
2865 case 'V':
2866 nios2_assemble_arg_V (insn->insn_tokens[tokidx++], insn);
2867 break;
2869 case 'W':
2870 nios2_assemble_arg_W (insn->insn_tokens[tokidx++], insn);
2871 break;
2873 case 'X':
2874 nios2_assemble_arg_X (insn->insn_tokens[tokidx++], insn);
2875 break;
2877 case 'Y':
2878 nios2_assemble_arg_Y (insn->insn_tokens[tokidx++], insn);
2879 break;
2881 case 'o':
2882 nios2_assemble_arg_o (insn->insn_tokens[tokidx++], insn);
2883 break;
2885 case 'O':
2886 nios2_assemble_arg_O (insn->insn_tokens[tokidx++], insn);
2887 break;
2889 case 'P':
2890 nios2_assemble_arg_P (insn->insn_tokens[tokidx++], insn);
2891 break;
2893 case 'j':
2894 nios2_assemble_arg_j (insn->insn_tokens[tokidx++], insn);
2895 break;
2897 case 'k':
2898 nios2_assemble_arg_k (insn->insn_tokens[tokidx++], insn);
2899 break;
2901 case 'l':
2902 nios2_assemble_arg_l (insn->insn_tokens[tokidx++], insn);
2903 break;
2905 case 'm':
2906 nios2_assemble_arg_m (insn->insn_tokens[tokidx++], insn);
2907 break;
2909 case 'M':
2910 nios2_assemble_arg_M (insn->insn_tokens[tokidx++], insn);
2911 break;
2913 case 'N':
2914 nios2_assemble_arg_N (insn->insn_tokens[tokidx++], insn);
2915 break;
2917 case 'e':
2918 nios2_assemble_arg_e (insn->insn_tokens[tokidx++], insn);
2919 break;
2921 case 'f':
2922 nios2_assemble_arg_f (insn->insn_tokens[tokidx++], insn);
2923 break;
2925 case 'g':
2926 nios2_assemble_arg_g (insn->insn_tokens[tokidx++], insn);
2927 break;
2929 case 'h':
2930 nios2_assemble_arg_h (insn->insn_tokens[tokidx++], insn);
2931 break;
2933 case 'R':
2934 nios2_assemble_arg_R (insn->insn_tokens[tokidx++], insn);
2935 break;
2937 case 'B':
2938 nios2_assemble_arg_B (insn->insn_tokens[tokidx++], insn);
2939 break;
2941 default:
2942 bad_opcode (op);
2943 break;
2946 /* Perform argument checking. */
2947 nios2_check_assembly (insn->insn_code | insn->constant_bits,
2948 insn->insn_tokens[tokidx]);
2952 /* The function consume_arg takes a pointer into a string
2953 of instruction tokens (args) and a pointer into a string
2954 representing the expected sequence of tokens and separators.
2955 It checks whether the first argument in argstr is of the
2956 expected type, throwing an error if it is not, and returns
2957 the pointer argstr. */
2958 static char *
2959 nios2_consume_arg (char *argstr, const char *parsestr)
2961 char *temp;
2963 switch (*parsestr)
2965 case 'c':
2966 case 'd':
2967 case 's':
2968 case 't':
2969 case 'D':
2970 case 'S':
2971 case 'T':
2972 break;
2974 case 'i':
2975 case 'u':
2976 if (*argstr == '%')
2978 if (nios2_special_relocation_p (argstr))
2980 /* We zap the parentheses because we don't want them confused
2981 with separators. */
2982 temp = strchr (argstr, '(');
2983 if (temp != NULL)
2984 *temp = ' ';
2985 temp = strchr (argstr, ')');
2986 if (temp != NULL)
2987 *temp = ' ';
2989 else
2990 as_bad (_("badly formed expression near %s"), argstr);
2992 break;
2993 case 'm':
2994 case 'j':
2995 case 'k':
2996 case 'l':
2997 case 'I':
2998 case 'U':
2999 case 'V':
3000 case 'W':
3001 case 'X':
3002 case 'Y':
3003 case 'O':
3004 case 'P':
3005 case 'e':
3006 case 'f':
3007 case 'g':
3008 case 'h':
3009 case 'M':
3010 case 'N':
3012 /* We can't have %hi, %lo or %hiadj here. */
3013 if (*argstr == '%')
3014 as_bad (_("badly formed expression near %s"), argstr);
3015 break;
3017 case 'R':
3018 /* Register list for ldwm/stwm or push.n/pop.n. Replace the commas
3019 in the list with spaces so we don't confuse them with separators. */
3020 if (*argstr != '{')
3022 as_bad ("missing '{' in register list");
3023 break;
3025 for (temp = argstr + 1; *temp; temp++)
3027 if (*temp == '}')
3028 break;
3029 else if (*temp == ',')
3030 *temp = ' ';
3032 if (!*temp)
3034 as_bad ("missing '}' in register list");
3035 break;
3037 break;
3039 case 'B':
3040 /* Base register and options for ldwm/stwm. This is the final argument
3041 and consumes the rest of the argument string; replace commas
3042 with spaces so that the token splitter doesn't think they are
3043 separate arguments. */
3044 for (temp = argstr; *temp; temp++)
3045 if (*temp == ',')
3046 *temp = ' ';
3047 break;
3049 case 'o':
3050 case 'E':
3051 break;
3052 default:
3053 BAD_CASE (*parsestr);
3054 break;
3057 return argstr;
3060 /* The function consume_separator takes a pointer into a string
3061 of instruction tokens (args) and a pointer into a string representing
3062 the expected sequence of tokens and separators. It finds the first
3063 instance of the character pointed to by separator in argstr, and
3064 returns a pointer to the next element of argstr, which is the
3065 following token in the sequence. */
3066 static char *
3067 nios2_consume_separator (char *argstr, const char *separator)
3069 char *p;
3071 /* If we have a opcode reg, expr(reg) type instruction, and
3072 * we are separating the expr from the (reg), we find the last
3073 * (, just in case the expression has parentheses. */
3075 if (*separator == '(')
3076 p = strrchr (argstr, *separator);
3077 else
3078 p = strchr (argstr, *separator);
3080 if (p != NULL)
3081 *p++ = 0;
3082 return p;
3085 /* The principal argument parsing function which takes a string argstr
3086 representing the instruction arguments for insn, and extracts the argument
3087 tokens matching parsestr into parsed_args. */
3088 static void
3089 nios2_parse_args (nios2_insn_infoS *insn, char *argstr,
3090 const char *parsestr, char **parsed_args)
3092 char *p;
3093 char *end = NULL;
3094 int i;
3095 p = argstr;
3096 i = 0;
3097 bool terminate = false;
3099 /* This rest of this function is it too fragile and it mostly works,
3100 therefore special case this one. */
3101 if (*parsestr == 0 && argstr != 0)
3103 as_bad (_("too many arguments"));
3104 parsed_args[0] = NULL;
3105 return;
3108 while (p != NULL && !terminate && i < NIOS2_MAX_INSN_TOKENS)
3110 parsed_args[i] = nios2_consume_arg (p, parsestr);
3111 ++parsestr;
3112 while (*parsestr == '(' || *parsestr == ')' || *parsestr == ',')
3114 char *context = p;
3115 p = nios2_consume_separator (p, parsestr);
3116 /* Check for missing separators. */
3117 if (!p && !(insn->insn_nios2_opcode->pinfo & NIOS2_INSN_OPTARG))
3119 as_bad (_("expecting %c near %s"), *parsestr, context);
3120 break;
3122 ++parsestr;
3125 if (*parsestr == '\0')
3127 /* Check that the argument string has no trailing arguments. */
3128 end = strpbrk (p, ",");
3129 if (end != NULL)
3130 as_bad (_("too many arguments"));
3133 if (*parsestr == '\0' || (p != NULL && *p == '\0'))
3134 terminate = true;
3135 ++i;
3138 parsed_args[i] = NULL;
3143 /** Support for pseudo-op parsing. These are macro-like opcodes that
3144 expand into real insns by suitable fiddling with the operands. */
3146 /* Append the string modifier to the string contained in the argument at
3147 parsed_args[ndx]. */
3148 static void
3149 nios2_modify_arg (char **parsed_args, const char *modifier,
3150 int unused ATTRIBUTE_UNUSED, int ndx)
3152 char *tmp = parsed_args[ndx];
3154 parsed_args[ndx] = concat (tmp, modifier, (char *) NULL);
3157 /* Modify parsed_args[ndx] by negating that argument. */
3158 static void
3159 nios2_negate_arg (char **parsed_args, const char *modifier ATTRIBUTE_UNUSED,
3160 int unused ATTRIBUTE_UNUSED, int ndx)
3162 char *tmp = parsed_args[ndx];
3164 parsed_args[ndx] = concat ("~(", tmp, ")+1", (char *) NULL);
3167 /* The function nios2_swap_args swaps the pointers at indices index_1 and
3168 index_2 in the array parsed_args[] - this is used for operand swapping
3169 for comparison operations. */
3170 static void
3171 nios2_swap_args (char **parsed_args, const char *unused ATTRIBUTE_UNUSED,
3172 int index_1, int index_2)
3174 char *tmp;
3175 gas_assert (index_1 < NIOS2_MAX_INSN_TOKENS
3176 && index_2 < NIOS2_MAX_INSN_TOKENS);
3177 tmp = parsed_args[index_1];
3178 parsed_args[index_1] = parsed_args[index_2];
3179 parsed_args[index_2] = tmp;
3182 /* This function appends the string appnd to the array of strings in
3183 parsed_args num times starting at index start in the array. */
3184 static void
3185 nios2_append_arg (char **parsed_args, const char *appnd, int num,
3186 int start)
3188 int i, count;
3189 char *tmp;
3191 gas_assert ((start + num) < NIOS2_MAX_INSN_TOKENS);
3193 if (nios2_mode == NIOS2_MODE_TEST)
3194 tmp = parsed_args[start];
3195 else
3196 tmp = NULL;
3198 for (i = start, count = num; count > 0; ++i, --count)
3199 parsed_args[i] = (char *) appnd;
3201 gas_assert (i == (start + num));
3202 parsed_args[i] = tmp;
3203 parsed_args[i + 1] = NULL;
3206 /* This function inserts the string insert num times in the array
3207 parsed_args, starting at the index start. */
3208 static void
3209 nios2_insert_arg (char **parsed_args, const char *insert, int num,
3210 int start)
3212 int i, count;
3214 gas_assert ((start + num) < NIOS2_MAX_INSN_TOKENS);
3216 /* Move the existing arguments up to create space. */
3217 for (i = NIOS2_MAX_INSN_TOKENS; i - num >= start; --i)
3218 parsed_args[i] = parsed_args[i - num];
3220 for (i = start, count = num; count > 0; ++i, --count)
3221 parsed_args[i] = (char *) insert;
3224 /* Cleanup function to free malloc'ed arg strings. */
3225 static void
3226 nios2_free_arg (char **parsed_args, int num ATTRIBUTE_UNUSED, int start)
3228 free (parsed_args[start]);
3229 parsed_args[start] = NULL;
3232 /* This function swaps the pseudo-op for a real op. */
3233 static nios2_ps_insn_infoS*
3234 nios2_translate_pseudo_insn (nios2_insn_infoS *insn)
3237 const struct nios2_opcode *op = insn->insn_nios2_opcode;
3238 nios2_ps_insn_infoS *ps_insn;
3239 unsigned int tokidx, ntok;
3241 /* Find which real insn the pseudo-op translates to and
3242 switch the insn_info ptr to point to it. */
3243 ps_insn = nios2_ps_lookup (op->name);
3245 if (ps_insn != NULL)
3247 insn->insn_nios2_opcode = nios2_opcode_lookup (ps_insn->insn);
3248 insn->insn_tokens[0] = insn->insn_nios2_opcode->name;
3250 /* Make sure there are enough arguments. */
3251 ntok = ((op->pinfo & NIOS2_INSN_OPTARG)
3252 ? op->num_args - 1 : op->num_args);
3253 for (tokidx = 1; tokidx <= ntok; tokidx++)
3254 if (insn->insn_tokens[tokidx] == NULL)
3256 as_bad ("missing argument");
3257 return NULL;
3260 /* Modify the args so they work with the real insn. */
3261 ps_insn->arg_modifer_func ((char **) insn->insn_tokens,
3262 ps_insn->arg_modifier, ps_insn->num,
3263 ps_insn->index);
3265 else
3266 /* we cannot recover from this. */
3267 as_fatal (_("unrecognized pseudo-instruction %s"),
3268 insn->insn_nios2_opcode->name);
3269 return ps_insn;
3272 /* Invoke the cleanup handler for pseudo-insn ps_insn on insn. */
3273 static void
3274 nios2_cleanup_pseudo_insn (nios2_insn_infoS *insn,
3275 nios2_ps_insn_infoS *ps_insn)
3277 if (ps_insn->arg_cleanup_func)
3278 (ps_insn->arg_cleanup_func) ((char **) insn->insn_tokens,
3279 ps_insn->num, ps_insn->index);
3282 const nios2_ps_insn_infoS nios2_ps_insn_info_structs[] = {
3283 /* pseudo-op, real-op, arg, arg_modifier_func, num, index, arg_cleanup_func */
3284 {"mov", "add", nios2_append_arg, "zero", 1, 3, NULL},
3285 {"movi", "addi", nios2_insert_arg, "zero", 1, 2, NULL},
3286 {"movhi", "orhi", nios2_insert_arg, "zero", 1, 2, NULL},
3287 {"movui", "ori", nios2_insert_arg, "zero", 1, 2, NULL},
3288 {"movia", "orhi", nios2_insert_arg, "zero", 1, 2, NULL},
3289 {"nop", "add", nios2_append_arg, "zero", 3, 1, NULL},
3290 {"bgt", "blt", nios2_swap_args, "", 1, 2, NULL},
3291 {"bgtu", "bltu", nios2_swap_args, "", 1, 2, NULL},
3292 {"ble", "bge", nios2_swap_args, "", 1, 2, NULL},
3293 {"bleu", "bgeu", nios2_swap_args, "", 1, 2, NULL},
3294 {"cmpgt", "cmplt", nios2_swap_args, "", 2, 3, NULL},
3295 {"cmpgtu", "cmpltu", nios2_swap_args, "", 2, 3, NULL},
3296 {"cmple", "cmpge", nios2_swap_args, "", 2, 3, NULL},
3297 {"cmpleu", "cmpgeu", nios2_swap_args, "", 2, 3, NULL},
3298 {"cmpgti", "cmpgei", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
3299 {"cmpgtui", "cmpgeui", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
3300 {"cmplei", "cmplti", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
3301 {"cmpleui", "cmpltui", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
3302 {"subi", "addi", nios2_negate_arg, "", 0, 3, nios2_free_arg},
3303 {"nop.n", "mov.n", nios2_append_arg, "zero", 2, 1, NULL}
3304 /* Add further pseudo-ops here. */
3307 #define NIOS2_NUM_PSEUDO_INSNS \
3308 ((sizeof(nios2_ps_insn_info_structs)/ \
3309 sizeof(nios2_ps_insn_info_structs[0])))
3310 const int nios2_num_ps_insn_info_structs = NIOS2_NUM_PSEUDO_INSNS;
3313 /** Assembler output support. */
3315 /* Output a normal instruction. */
3316 static void
3317 output_insn (nios2_insn_infoS *insn)
3319 char *f;
3320 nios2_insn_relocS *reloc;
3321 f = frag_more (insn->insn_nios2_opcode->size);
3322 /* This allocates enough space for the instruction
3323 and puts it in the current frag. */
3324 md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size);
3325 /* Emit debug info. */
3326 dwarf2_emit_insn (insn->insn_nios2_opcode->size);
3327 /* Create any fixups to be acted on later. */
3329 for (reloc = insn->insn_reloc; reloc != NULL; reloc = reloc->reloc_next)
3330 fix_new_exp (frag_now, f - frag_now->fr_literal,
3331 insn->insn_nios2_opcode->size,
3332 &reloc->reloc_expression, reloc->reloc_pcrel,
3333 reloc->reloc_type);
3336 /* Output an unconditional branch. */
3337 static void
3338 output_ubranch (nios2_insn_infoS *insn)
3340 nios2_insn_relocS *reloc = insn->insn_reloc;
3342 /* If the reloc is NULL, there was an error assembling the branch. */
3343 if (reloc != NULL)
3345 symbolS *symp = reloc->reloc_expression.X_add_symbol;
3346 offsetT offset = reloc->reloc_expression.X_add_number;
3347 char *f;
3348 bool is_cdx = (insn->insn_nios2_opcode->size == 2);
3350 /* Tag dwarf2 debug info to the address at the start of the insn.
3351 We must do it before frag_var() below closes off the frag. */
3352 dwarf2_emit_insn (0);
3354 /* We create a machine dependent frag which can grow
3355 to accommodate the largest possible instruction sequence
3356 this may generate. */
3357 f = frag_var (rs_machine_dependent,
3358 UBRANCH_MAX_SIZE, insn->insn_nios2_opcode->size,
3359 (is_cdx ? CDX_UBRANCH_SUBTYPE (0) : UBRANCH_SUBTYPE (0)),
3360 symp, offset, NULL);
3362 md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size);
3364 /* We leave fixup generation to md_convert_frag. */
3368 /* Output a conditional branch. */
3369 static void
3370 output_cbranch (nios2_insn_infoS *insn)
3372 nios2_insn_relocS *reloc = insn->insn_reloc;
3374 /* If the reloc is NULL, there was an error assembling the branch. */
3375 if (reloc != NULL)
3377 symbolS *symp = reloc->reloc_expression.X_add_symbol;
3378 offsetT offset = reloc->reloc_expression.X_add_number;
3379 char *f;
3380 bool is_cdx = (insn->insn_nios2_opcode->size == 2);
3382 /* Tag dwarf2 debug info to the address at the start of the insn.
3383 We must do it before frag_var() below closes off the frag. */
3384 dwarf2_emit_insn (0);
3386 /* We create a machine dependent frag which can grow
3387 to accommodate the largest possible instruction sequence
3388 this may generate. */
3389 f = frag_var (rs_machine_dependent,
3390 CBRANCH_MAX_SIZE, insn->insn_nios2_opcode->size,
3391 (is_cdx ? CDX_CBRANCH_SUBTYPE (0) : CBRANCH_SUBTYPE (0)),
3392 symp, offset, NULL);
3394 md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size);
3396 /* We leave fixup generation to md_convert_frag. */
3400 /* Output a call sequence. Since calls are not pc-relative for NIOS2,
3401 but are page-relative, we cannot tell at any stage in assembly
3402 whether a call will be out of range since a section may be linked
3403 at any address. So if we are relaxing, we convert all call instructions
3404 to long call sequences, and rely on the linker to relax them back to
3405 short calls. */
3406 static void
3407 output_call (nios2_insn_infoS *insn)
3409 /* This allocates enough space for the instruction
3410 and puts it in the current frag. */
3411 char *f = frag_more (12);
3412 nios2_insn_relocS *reloc = insn->insn_reloc;
3413 const struct nios2_opcode *op = insn->insn_nios2_opcode;
3415 switch (op->format)
3417 case iw_j_type:
3418 md_number_to_chars (f,
3419 (MATCH_R1_ORHI | SET_IW_I_B (AT_REGNUM)
3420 | SET_IW_I_A (0)),
3422 dwarf2_emit_insn (4);
3423 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
3424 &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_HI16);
3425 md_number_to_chars (f + 4,
3426 (MATCH_R1_ORI | SET_IW_I_B (AT_REGNUM)
3427 | SET_IW_I_A (AT_REGNUM)),
3429 dwarf2_emit_insn (4);
3430 fix_new_exp (frag_now, f - frag_now->fr_literal + 4, 4,
3431 &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_LO16);
3432 md_number_to_chars (f + 8, MATCH_R1_CALLR | SET_IW_R_A (AT_REGNUM), 4);
3433 dwarf2_emit_insn (4);
3434 break;
3435 case iw_L26_type:
3436 md_number_to_chars (f,
3437 (MATCH_R2_ORHI | SET_IW_F2I16_B (AT_REGNUM)
3438 | SET_IW_F2I16_A (0)),
3440 dwarf2_emit_insn (4);
3441 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
3442 &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_HI16);
3443 md_number_to_chars (f + 4,
3444 (MATCH_R2_ORI | SET_IW_F2I16_B (AT_REGNUM)
3445 | SET_IW_F2I16_A (AT_REGNUM)),
3447 dwarf2_emit_insn (4);
3448 fix_new_exp (frag_now, f - frag_now->fr_literal + 4, 4,
3449 &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_LO16);
3450 md_number_to_chars (f + 8, MATCH_R2_CALLR | SET_IW_F3X6L5_A (AT_REGNUM),
3452 dwarf2_emit_insn (4);
3453 break;
3454 default:
3455 bad_opcode (op);
3459 /* Output a movhi/addi pair for the movia pseudo-op. */
3460 static void
3461 output_movia (nios2_insn_infoS *insn)
3463 /* This allocates enough space for the instruction
3464 and puts it in the current frag. */
3465 char *f = frag_more (8);
3466 nios2_insn_relocS *reloc = insn->insn_reloc;
3467 unsigned long reg, code = 0;
3468 const struct nios2_opcode *op = insn->insn_nios2_opcode;
3470 /* If the reloc is NULL, there was an error assembling the movia. */
3471 if (reloc != NULL)
3473 switch (op->format)
3475 case iw_i_type:
3476 reg = GET_IW_I_B (insn->insn_code);
3477 code = MATCH_R1_ADDI | SET_IW_I_A (reg) | SET_IW_I_B (reg);
3478 break;
3479 case iw_F2I16_type:
3480 reg = GET_IW_F2I16_B (insn->insn_code);
3481 code = MATCH_R2_ADDI | SET_IW_F2I16_A (reg) | SET_IW_F2I16_B (reg);
3482 break;
3483 default:
3484 bad_opcode (op);
3487 md_number_to_chars (f, insn->insn_code, 4);
3488 dwarf2_emit_insn (4);
3489 fix_new (frag_now, f - frag_now->fr_literal, 4,
3490 reloc->reloc_expression.X_add_symbol,
3491 reloc->reloc_expression.X_add_number, 0,
3492 BFD_RELOC_NIOS2_HIADJ16);
3493 md_number_to_chars (f + 4, code, 4);
3494 dwarf2_emit_insn (4);
3495 fix_new (frag_now, f + 4 - frag_now->fr_literal, 4,
3496 reloc->reloc_expression.X_add_symbol,
3497 reloc->reloc_expression.X_add_number, 0, BFD_RELOC_NIOS2_LO16);
3503 /** External interfaces. */
3505 /* Update the selected architecture based on ARCH, giving an error if
3506 ARCH is an invalid value. */
3508 static void
3509 nios2_use_arch (const char *arch)
3511 if (strcmp (arch, "nios2") == 0 || strcmp (arch, "r1") == 0)
3513 nios2_architecture |= EF_NIOS2_ARCH_R1;
3514 nios2_opcodes = (struct nios2_opcode *) nios2_r1_opcodes;
3515 nios2_num_opcodes = nios2_num_r1_opcodes;
3516 nop32 = nop_r1;
3517 nop16 = NULL;
3518 return;
3520 else if (strcmp (arch, "r2") == 0)
3522 nios2_architecture |= EF_NIOS2_ARCH_R2;
3523 nios2_opcodes = (struct nios2_opcode *) nios2_r2_opcodes;
3524 nios2_num_opcodes = nios2_num_r2_opcodes;
3525 nop32 = nop_r2;
3526 nop16 = nop_r2_cdx;
3527 return;
3530 as_bad (_("unknown architecture '%s'"), arch);
3533 /* The following functions are called by machine-independent parts of
3534 the assembler. */
3536 md_parse_option (int c, const char *arg ATTRIBUTE_UNUSED)
3538 switch (c)
3540 case 'r':
3541 /* Hidden option for self-test mode. */
3542 nios2_mode = NIOS2_MODE_TEST;
3543 break;
3544 case OPTION_RELAX_ALL:
3545 nios2_as_options.relax = relax_all;
3546 break;
3547 case OPTION_NORELAX:
3548 nios2_as_options.relax = relax_none;
3549 break;
3550 case OPTION_RELAX_SECTION:
3551 nios2_as_options.relax = relax_section;
3552 break;
3553 case OPTION_EB:
3554 target_big_endian = 1;
3555 break;
3556 case OPTION_EL:
3557 target_big_endian = 0;
3558 break;
3559 case OPTION_MARCH:
3560 nios2_use_arch (arg);
3561 break;
3562 default:
3563 return 0;
3564 break;
3567 return 1;
3570 /* Implement TARGET_FORMAT. We can choose to be big-endian or
3571 little-endian at runtime based on a switch. */
3572 const char *
3573 nios2_target_format (void)
3575 return target_big_endian ? "elf32-bignios2" : "elf32-littlenios2";
3578 /* Machine-dependent usage message. */
3579 void
3580 md_show_usage (FILE *stream)
3582 fprintf (stream, " NIOS2 options:\n"
3583 " -relax-all replace all branch and call "
3584 "instructions with jmp and callr sequences\n"
3585 " -relax-section replace identified out of range "
3586 "branches with jmp sequences (default)\n"
3587 " -no-relax do not replace any branches or calls\n"
3588 " -EB force big-endian byte ordering\n"
3589 " -EL force little-endian byte ordering\n"
3590 " -march=ARCH enable instructions from architecture ARCH\n");
3594 /* This function is called once, at assembler startup time.
3595 It should set up all the tables, etc. that the MD part of the
3596 assembler will need. */
3597 void
3598 md_begin (void)
3600 int i;
3602 switch (nios2_architecture)
3604 default:
3605 case EF_NIOS2_ARCH_R1:
3606 bfd_default_set_arch_mach (stdoutput, bfd_arch_nios2, bfd_mach_nios2r1);
3607 break;
3608 case EF_NIOS2_ARCH_R2:
3609 if (target_big_endian)
3610 as_fatal (_("Big-endian R2 is not supported."));
3611 bfd_default_set_arch_mach (stdoutput, bfd_arch_nios2, bfd_mach_nios2r2);
3612 break;
3615 /* Create and fill a hashtable for the Nios II opcodes, registers and
3616 arguments. */
3617 nios2_opcode_hash = str_htab_create ();
3618 nios2_reg_hash = str_htab_create ();
3619 nios2_ps_hash = str_htab_create ();
3621 for (i = 0; i < nios2_num_opcodes; ++i)
3622 if (str_hash_insert (nios2_opcode_hash, nios2_opcodes[i].name,
3623 &nios2_opcodes[i], 0) != NULL)
3624 as_fatal (_("duplicate %s"), nios2_opcodes[i].name);
3626 for (i = 0; i < nios2_num_regs; ++i)
3627 if (str_hash_insert (nios2_reg_hash, nios2_regs[i].name,
3628 &nios2_regs[i], 0) != NULL)
3629 as_fatal (_("duplicate %s"), nios2_regs[i].name);
3631 for (i = 0; i < nios2_num_ps_insn_info_structs; ++i)
3632 if (str_hash_insert (nios2_ps_hash,
3633 nios2_ps_insn_info_structs[i].pseudo_insn,
3634 &nios2_ps_insn_info_structs[i], 0) != NULL)
3635 as_fatal (_("duplicate %s"), nios2_ps_insn_info_structs[i].pseudo_insn);
3637 /* Assembler option defaults. */
3638 nios2_as_options.noat = false;
3639 nios2_as_options.nobreak = false;
3641 /* Initialize the alignment data. */
3642 nios2_current_align_seg = now_seg;
3643 nios2_last_label = NULL;
3644 nios2_current_align = 0;
3645 nios2_min_align = 2;
3649 /* Assembles a single line of Nios II assembly language. */
3650 void
3651 md_assemble (char *op_str)
3653 char *argstr;
3654 char *op_strdup = NULL;
3655 unsigned long saved_pinfo = 0;
3656 nios2_insn_infoS thisinsn;
3657 nios2_insn_infoS *insn = &thisinsn;
3658 bool ps_error = false;
3660 /* Make sure we are aligned on an appropriate boundary. */
3661 if (nios2_current_align < nios2_min_align)
3662 nios2_align (nios2_min_align, NULL, nios2_last_label);
3663 else if (nios2_current_align > nios2_min_align)
3664 nios2_current_align = nios2_min_align;
3665 nios2_last_label = NULL;
3667 /* We don't want to clobber to op_str
3668 because we want to be able to use it in messages. */
3669 op_strdup = strdup (op_str);
3670 insn->insn_tokens[0] = strtok (op_strdup, " ");
3671 argstr = strtok (NULL, "");
3673 /* Assemble the opcode. */
3674 insn->insn_nios2_opcode = nios2_opcode_lookup (insn->insn_tokens[0]);
3675 insn->insn_reloc = NULL;
3677 if (insn->insn_nios2_opcode != NULL)
3679 nios2_ps_insn_infoS *ps_insn = NULL;
3681 /* Note if we've seen a 16-bit instruction. */
3682 if (insn->insn_nios2_opcode->size == 2)
3683 nios2_min_align = 1;
3685 /* Set the opcode for the instruction. */
3686 insn->insn_code = insn->insn_nios2_opcode->match;
3687 insn->constant_bits = 0;
3689 /* Parse the arguments pointed to by argstr. */
3690 if (nios2_mode == NIOS2_MODE_ASSEMBLE)
3691 nios2_parse_args (insn, argstr, insn->insn_nios2_opcode->args,
3692 (char **) &insn->insn_tokens[1]);
3693 else
3694 nios2_parse_args (insn, argstr, insn->insn_nios2_opcode->args_test,
3695 (char **) &insn->insn_tokens[1]);
3697 /* We need to preserve the MOVIA macro as this is clobbered by
3698 translate_pseudo_insn. */
3699 if (insn->insn_nios2_opcode->pinfo == NIOS2_INSN_MACRO_MOVIA)
3700 saved_pinfo = NIOS2_INSN_MACRO_MOVIA;
3701 /* If the instruction is an pseudo-instruction, we want to replace it
3702 with its real equivalent, and then continue. */
3703 if ((insn->insn_nios2_opcode->pinfo & NIOS2_INSN_MACRO)
3704 == NIOS2_INSN_MACRO)
3706 ps_insn = nios2_translate_pseudo_insn (insn);
3707 if (!ps_insn)
3708 ps_error = true;
3711 /* If we found invalid pseudo-instruction syntax, the error's already
3712 been diagnosed in nios2_translate_pseudo_insn, so skip
3713 remaining processing. */
3714 if (!ps_error)
3716 /* Assemble the parsed arguments into the instruction word. */
3717 nios2_assemble_args (insn);
3719 /* Handle relaxation and other transformations. */
3720 if (nios2_as_options.relax != relax_none
3721 && !nios2_as_options.noat
3722 && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_UBRANCH)
3723 output_ubranch (insn);
3724 else if (nios2_as_options.relax != relax_none
3725 && !nios2_as_options.noat
3726 && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_CBRANCH)
3727 output_cbranch (insn);
3728 else if (nios2_as_options.relax == relax_all
3729 && !nios2_as_options.noat
3730 && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_CALL
3731 && insn->insn_reloc
3732 && ((insn->insn_reloc->reloc_type
3733 == BFD_RELOC_NIOS2_CALL26)
3734 || (insn->insn_reloc->reloc_type
3735 == BFD_RELOC_NIOS2_CALL26_NOAT)))
3736 output_call (insn);
3737 else if (saved_pinfo == NIOS2_INSN_MACRO_MOVIA)
3738 output_movia (insn);
3739 else
3740 output_insn (insn);
3741 if (ps_insn)
3742 nios2_cleanup_pseudo_insn (insn, ps_insn);
3745 else
3746 /* Unrecognised instruction - error. */
3747 as_bad (_("unrecognised instruction %s"), insn->insn_tokens[0]);
3749 /* Don't leak memory. */
3750 free (op_strdup);
3753 /* Round up section size. */
3754 valueT
3755 md_section_align (asection *seg ATTRIBUTE_UNUSED, valueT size)
3757 /* I think byte alignment is fine here. */
3758 return size;
3761 /* Implement TC_FORCE_RELOCATION. */
3763 nios2_force_relocation (fixS *fixp)
3765 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3766 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
3767 || fixp->fx_r_type == BFD_RELOC_NIOS2_ALIGN)
3768 return 1;
3770 return generic_force_reloc (fixp);
3773 /* Implement tc_fix_adjustable. */
3775 nios2_fix_adjustable (fixS *fixp)
3777 if (fixp->fx_addsy == NULL)
3778 return 1;
3780 #ifdef OBJ_ELF
3781 /* Prevent all adjustments to global symbols. */
3782 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
3783 && (S_IS_EXTERNAL (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
3784 return 0;
3785 #endif
3786 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3787 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3788 return 0;
3790 /* Preserve relocations against symbols with function type. */
3791 if (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION)
3792 return 0;
3794 /* Don't allow symbols to be discarded on GOT related relocs. */
3795 if (fixp->fx_r_type == BFD_RELOC_NIOS2_GOT16
3796 || fixp->fx_r_type == BFD_RELOC_NIOS2_CALL16
3797 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_LO
3798 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_HA
3799 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_GD16
3800 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LDM16
3801 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LDO16
3802 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_IE16
3803 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LE16
3804 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPMOD
3805 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPREL
3806 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_TPREL
3807 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF
3808 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOT_LO
3809 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOT_HA
3810 || fixp->fx_r_type == BFD_RELOC_NIOS2_CALL_LO
3811 || fixp->fx_r_type == BFD_RELOC_NIOS2_CALL_HA
3813 return 0;
3815 return 1;
3818 /* Implement tc_frob_symbol. This is called in adjust_reloc_syms;
3819 it is used to remove *ABS* references from the symbol table. */
3821 nios2_frob_symbol (symbolS *symp)
3823 if ((OUTPUT_FLAVOR == bfd_target_elf_flavour
3824 && symp == section_symbol (absolute_section))
3825 || !S_IS_DEFINED (symp))
3826 return 1;
3827 else
3828 return 0;
3831 /* The function tc_gen_reloc creates a relocation structure for the
3832 fixup fixp, and returns a pointer to it. This structure is passed
3833 to bfd_install_relocation so that it can be written to the object
3834 file for linking. */
3835 arelent *
3836 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
3838 arelent *reloc = XNEW (arelent);
3839 reloc->sym_ptr_ptr = XNEW (asymbol *);
3840 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
3842 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
3843 reloc->addend = fixp->fx_offset; /* fixp->fx_addnumber; */
3845 if (fixp->fx_pcrel)
3847 switch (fixp->fx_r_type)
3849 case BFD_RELOC_16:
3850 fixp->fx_r_type = BFD_RELOC_16_PCREL;
3851 break;
3852 case BFD_RELOC_NIOS2_LO16:
3853 fixp->fx_r_type = BFD_RELOC_NIOS2_PCREL_LO;
3854 break;
3855 case BFD_RELOC_NIOS2_HIADJ16:
3856 fixp->fx_r_type = BFD_RELOC_NIOS2_PCREL_HA;
3857 break;
3858 default:
3859 break;
3863 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
3864 if (reloc->howto == NULL)
3866 as_bad_where (fixp->fx_file, fixp->fx_line,
3867 _("can't represent relocation type %s"),
3868 bfd_get_reloc_code_name (fixp->fx_r_type));
3870 /* Set howto to a garbage value so that we can keep going. */
3871 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
3872 gas_assert (reloc->howto != NULL);
3874 return reloc;
3877 long
3878 md_pcrel_from (fixS *fixP ATTRIBUTE_UNUSED)
3880 return 0;
3883 /* Called just before the assembler exits. */
3884 void
3885 md_end (void)
3887 /* FIXME - not yet implemented */
3890 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
3891 Otherwise we have no need to default values of symbols. */
3892 symbolS *
3893 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
3895 #ifdef OBJ_ELF
3896 if (name[0] == '_' && name[1] == 'G'
3897 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
3899 if (!GOT_symbol)
3901 if (symbol_find (name))
3902 as_bad ("GOT already in the symbol table");
3904 GOT_symbol = symbol_new (name, undefined_section,
3905 &zero_address_frag, 0);
3908 return GOT_symbol;
3910 #endif
3912 return 0;
3915 /* Implement tc_frob_label. */
3916 void
3917 nios2_frob_label (symbolS *lab)
3919 /* Emit dwarf information. */
3920 dwarf2_emit_label (lab);
3922 /* Update the label's address with the current output pointer. */
3923 symbol_set_frag (lab, frag_now);
3924 S_SET_VALUE (lab, (valueT) frag_now_fix ());
3926 /* Record this label for future adjustment after we find out what
3927 kind of data it references, and the required alignment therewith. */
3928 nios2_last_label = lab;
3931 /* Implement md_cons_align. */
3932 void
3933 nios2_cons_align (int size)
3935 int log_size = 0;
3936 const char *pfill = NULL;
3938 while ((size >>= 1) != 0)
3939 ++log_size;
3941 if (subseg_text_p (now_seg))
3942 pfill = (const char *) nop32;
3943 else
3944 pfill = NULL;
3946 if (nios2_auto_align_on)
3947 nios2_align (log_size, pfill, NULL);
3949 nios2_last_label = NULL;
3952 /* Map 's' to SHF_NIOS2_GPREL. */
3953 /* This is from the Alpha code tc-alpha.c. */
3955 nios2_elf_section_letter (int letter, const char **ptr_msg)
3957 if (letter == 's')
3958 return SHF_NIOS2_GPREL;
3960 *ptr_msg = _("Bad .section directive: want a,s,w,x,M,S,G,T in string");
3961 return -1;
3964 /* Map SHF_ALPHA_GPREL to SEC_SMALL_DATA. */
3965 /* This is from the Alpha code tc-alpha.c. */
3966 flagword
3967 nios2_elf_section_flags (flagword flags, int attr, int type ATTRIBUTE_UNUSED)
3969 if (attr & SHF_NIOS2_GPREL)
3970 flags |= SEC_SMALL_DATA;
3971 return flags;
3974 /* Implement TC_PARSE_CONS_EXPRESSION to handle %tls_ldo(...) and
3975 %gotoff(...). */
3976 bfd_reloc_code_real_type
3977 nios2_cons (expressionS *exp, int size)
3979 bfd_reloc_code_real_type explicit_reloc = BFD_RELOC_NONE;
3980 const char *reloc_name = NULL;
3982 SKIP_WHITESPACE ();
3983 if (input_line_pointer[0] == '%')
3985 if (startswith (input_line_pointer + 1, "tls_ldo"))
3987 reloc_name = "%tls_ldo";
3988 if (size != 4)
3989 as_bad (_("Illegal operands: %%tls_ldo in %d-byte data field"),
3990 size);
3991 else
3993 input_line_pointer += 8;
3994 explicit_reloc = BFD_RELOC_NIOS2_TLS_DTPREL;
3997 else if (startswith (input_line_pointer + 1, "gotoff"))
3999 reloc_name = "%gotoff";
4000 if (size != 4)
4001 as_bad (_("Illegal operands: %%gotoff in %d-byte data field"),
4002 size);
4003 else
4005 input_line_pointer += 7;
4006 explicit_reloc = BFD_RELOC_NIOS2_GOTOFF;
4010 if (explicit_reloc != BFD_RELOC_NONE)
4012 SKIP_WHITESPACE ();
4013 if (input_line_pointer[0] != '(')
4014 as_bad (_("Illegal operands: %s requires arguments in ()"),
4015 reloc_name);
4016 else
4018 int c;
4019 char *end = ++input_line_pointer;
4020 int npar = 0;
4022 for (c = *end; !is_end_of_line[c]; end++, c = *end)
4023 if (c == '(')
4024 npar++;
4025 else if (c == ')')
4027 if (!npar)
4028 break;
4029 npar--;
4032 if (c != ')')
4033 as_bad (_("Illegal operands: %s requires arguments in ()"),
4034 reloc_name);
4035 else
4037 *end = '\0';
4038 expression (exp);
4039 *end = c;
4040 if (input_line_pointer != end)
4041 as_bad (_("Illegal operands: %s requires arguments in ()"),
4042 reloc_name);
4043 else
4045 input_line_pointer++;
4046 SKIP_WHITESPACE ();
4047 c = *input_line_pointer;
4048 if (! is_end_of_line[c] && c != ',')
4049 as_bad (_("Illegal operands: garbage after %s()"),
4050 reloc_name);
4056 if (explicit_reloc == BFD_RELOC_NONE)
4057 expression (exp);
4058 return explicit_reloc;
4061 /* Implement HANDLE_ALIGN. */
4062 void
4063 nios2_handle_align (fragS *fragp)
4065 /* If we are expecting to relax in the linker, then we must output a
4066 relocation to tell the linker we are aligning code. */
4067 if (nios2_as_options.relax == relax_all
4068 && (fragp->fr_type == rs_align || fragp->fr_type == rs_align_code)
4069 && fragp->fr_address + fragp->fr_fix > 0
4070 && fragp->fr_offset > 1
4071 && now_seg != bss_section)
4072 fix_new (fragp, fragp->fr_fix, 0, &abs_symbol, fragp->fr_offset, 0,
4073 BFD_RELOC_NIOS2_ALIGN);
4076 /* Implement tc_regname_to_dw2regnum, to convert REGNAME to a DWARF-2
4077 register number. */
4079 nios2_regname_to_dw2regnum (char *regname)
4081 struct nios2_reg *r = nios2_reg_lookup (regname);
4082 if (r == NULL)
4083 return -1;
4084 return r->index;
4087 /* Implement tc_cfi_frame_initial_instructions, to initialize the DWARF-2
4088 unwind information for this procedure. */
4089 void
4090 nios2_frame_initial_instructions (void)
4092 cfi_add_CFA_def_cfa (27, 0);
4095 #ifdef OBJ_ELF
4096 /* Some special processing for a Nios II ELF file. */
4098 void
4099 nios2_elf_final_processing (void)
4101 elf_elfheader (stdoutput)->e_flags = nios2_architecture;
4103 #endif