Add Nios II arch flags and compatibility tests
[deliverable/binutils-gdb.git] / gas / config / tc-nios2.c
1 /* Altera Nios II assembler.
2 Copyright (C) 2012-2015 Free Software Foundation, Inc.
3 Contributed by Nigel Gray (ngray@altera.com).
4 Contributed by Mentor Graphics, Inc.
5
6 This file is part of GAS, the GNU Assembler.
7
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.
12
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.
17
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. */
22
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 "libbfd.h"
29 #include "dwarf2dbg.h"
30 #include "subsegs.h"
31 #include "safe-ctype.h"
32 #include "dw2gencfi.h"
33
34 #ifndef OBJ_ELF
35 /* We are not supporting any other target so we throw a compile time error. */
36 OBJ_ELF not defined
37 #endif
38
39 /* We can choose our endianness at run-time, regardless of configuration. */
40 extern int target_big_endian;
41
42 /* This array holds the chars that always start a comment. If the
43 pre-processor is disabled, these aren't very useful. */
44 const char comment_chars[] = "#";
45
46 /* This array holds the chars that only start a comment at the beginning of
47 a line. If the line seems to have the form '# 123 filename'
48 .line and .file directives will appear in the pre-processed output. */
49 /* Note that input_file.c hand checks for '#' at the beginning of the
50 first line of the input file. This is because the compiler outputs
51 #NO_APP at the beginning of its output. */
52 /* Also note that C style comments are always supported. */
53 const char line_comment_chars[] = "#";
54
55 /* This array holds machine specific line separator characters. */
56 const char line_separator_chars[] = ";";
57
58 /* Chars that can be used to separate mant from exp in floating point nums. */
59 const char EXP_CHARS[] = "eE";
60
61 /* Chars that mean this number is a floating point constant. */
62 /* As in 0f12.456 */
63 /* or 0d1.2345e12 */
64 const char FLT_CHARS[] = "rRsSfFdDxXpP";
65
66 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
67 changed in read.c. Ideally it shouldn't have to know about it at all,
68 but nothing is ideal around here. */
69
70 /* Machine-dependent command-line options. */
71
72 const char *md_shortopts = "r";
73
74 struct option md_longopts[] = {
75 #define OPTION_RELAX_ALL (OPTION_MD_BASE + 0)
76 {"relax-all", no_argument, NULL, OPTION_RELAX_ALL},
77 #define OPTION_NORELAX (OPTION_MD_BASE + 1)
78 {"no-relax", no_argument, NULL, OPTION_NORELAX},
79 #define OPTION_RELAX_SECTION (OPTION_MD_BASE + 2)
80 {"relax-section", no_argument, NULL, OPTION_RELAX_SECTION},
81 #define OPTION_EB (OPTION_MD_BASE + 3)
82 {"EB", no_argument, NULL, OPTION_EB},
83 #define OPTION_EL (OPTION_MD_BASE + 4)
84 {"EL", no_argument, NULL, OPTION_EL},
85 #define OPTION_MARCH (OPTION_MD_BASE + 5)
86 {"march", required_argument, NULL, OPTION_MARCH}
87 };
88
89 size_t md_longopts_size = sizeof (md_longopts);
90
91 /* The assembler supports three different relaxation modes, controlled by
92 command-line options. */
93 typedef enum
94 {
95 relax_section = 0,
96 relax_none,
97 relax_all
98 } relax_optionT;
99
100 /* Struct contains all assembler options set with .set. */
101 struct
102 {
103 /* .set noat -> noat = 1 allows assembly code to use at without warning
104 and macro expansions generate a warning.
105 .set at -> noat = 0, assembly code using at warn but macro expansions
106 do not generate warnings. */
107 bfd_boolean noat;
108
109 /* .set nobreak -> nobreak = 1 allows assembly code to use ba,bt without
110 warning.
111 .set break -> nobreak = 0, assembly code using ba,bt warns. */
112 bfd_boolean nobreak;
113
114 /* .cmd line option -relax-all allows all branches and calls to be replaced
115 with longer versions.
116 -no-relax inhibits branch/call conversion.
117 The default value is relax_section, which relaxes branches within
118 a section. */
119 relax_optionT relax;
120
121 } nios2_as_options = {FALSE, FALSE, relax_section};
122
123
124 typedef struct nios2_insn_reloc
125 {
126 /* Any expression in the instruction is parsed into this field,
127 which is passed to fix_new_exp() to generate a fixup. */
128 expressionS reloc_expression;
129
130 /* The type of the relocation to be applied. */
131 bfd_reloc_code_real_type reloc_type;
132
133 /* PC-relative. */
134 unsigned int reloc_pcrel;
135
136 /* The next relocation to be applied to the instruction. */
137 struct nios2_insn_reloc *reloc_next;
138 } nios2_insn_relocS;
139
140 /* This struct is used to hold state when assembling instructions. */
141 typedef struct nios2_insn_info
142 {
143 /* Assembled instruction. */
144 unsigned long insn_code;
145
146 /* Constant bits masked into insn_code for self-check mode. */
147 unsigned long constant_bits;
148
149 /* Pointer to the relevant bit of the opcode table. */
150 const struct nios2_opcode *insn_nios2_opcode;
151 /* After parsing ptrs to the tokens in the instruction fill this array
152 it is terminated with a null pointer (hence the first +1).
153 The second +1 is because in some parts of the code the opcode
154 is not counted as a token, but still placed in this array. */
155 const char *insn_tokens[NIOS2_MAX_INSN_TOKENS + 1 + 1];
156
157 /* This holds information used to generate fixups
158 and eventually relocations if it is not null. */
159 nios2_insn_relocS *insn_reloc;
160 } nios2_insn_infoS;
161
162
163 /* This struct is used to convert Nios II pseudo-ops into the
164 corresponding real op. */
165 typedef struct nios2_ps_insn_info
166 {
167 /* Map this pseudo_op... */
168 const char *pseudo_insn;
169
170 /* ...to this real instruction. */
171 const char *insn;
172
173 /* Call this function to modify the operands.... */
174 void (*arg_modifer_func) (char ** parsed_args, const char *arg, int num,
175 int start);
176
177 /* ...with these arguments. */
178 const char *arg_modifier;
179 int num;
180 int index;
181
182 /* If arg_modifier_func allocates new memory, provide this function
183 to free it afterwards. */
184 void (*arg_cleanup_func) (char **parsed_args, int num, int start);
185 } nios2_ps_insn_infoS;
186
187 /* Opcode hash table. */
188 static struct hash_control *nios2_opcode_hash = NULL;
189 #define nios2_opcode_lookup(NAME) \
190 ((struct nios2_opcode *) hash_find (nios2_opcode_hash, (NAME)))
191
192 /* Register hash table. */
193 static struct hash_control *nios2_reg_hash = NULL;
194 #define nios2_reg_lookup(NAME) \
195 ((struct nios2_reg *) hash_find (nios2_reg_hash, (NAME)))
196
197
198 /* Pseudo-op hash table. */
199 static struct hash_control *nios2_ps_hash = NULL;
200 #define nios2_ps_lookup(NAME) \
201 ((nios2_ps_insn_infoS *) hash_find (nios2_ps_hash, (NAME)))
202
203 /* The known current alignment of the current section. */
204 static int nios2_current_align;
205 static segT nios2_current_align_seg;
206
207 static int nios2_auto_align_on = 1;
208
209 /* The last seen label in the current section. This is used to auto-align
210 labels preceeding instructions. */
211 static symbolS *nios2_last_label;
212
213 #ifdef OBJ_ELF
214 /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
215 symbolS *GOT_symbol;
216 #endif
217
218 /* The processor architecture value, EF_NIOS2_ARCH_R1 by default. */
219 static int nios2_architecture = EF_NIOS2_ARCH_R1;
220
221 \f
222 /** Utility routines. */
223 /* Function md_chars_to_number takes the sequence of
224 bytes in buf and returns the corresponding value
225 in an int. n must be 1, 2 or 4. */
226 static valueT
227 md_chars_to_number (char *buf, int n)
228 {
229 int i;
230 valueT val;
231
232 gas_assert (n == 1 || n == 2 || n == 4);
233
234 val = 0;
235 if (target_big_endian)
236 for (i = 0; i < n; ++i)
237 val = val | ((buf[i] & 0xff) << 8 * (n - (i + 1)));
238 else
239 for (i = 0; i < n; ++i)
240 val = val | ((buf[i] & 0xff) << 8 * i);
241 return val;
242 }
243
244
245 /* This function turns a C long int, short int or char
246 into the series of bytes that represent the number
247 on the target machine. */
248 void
249 md_number_to_chars (char *buf, valueT val, int n)
250 {
251 gas_assert (n == 1 || n == 2 || n == 4 || n == 8);
252 if (target_big_endian)
253 number_to_chars_bigendian (buf, val, n);
254 else
255 number_to_chars_littleendian (buf, val, n);
256 }
257
258 /* Turn a string in input_line_pointer into a floating point constant
259 of type TYPE, and store the appropriate bytes in *LITP. The number
260 of LITTLENUMS emitted is stored in *SIZEP. An error message is
261 returned, or NULL on OK. */
262 char *
263 md_atof (int type, char *litP, int *sizeP)
264 {
265 int prec;
266 LITTLENUM_TYPE words[4];
267 char *t;
268 int i;
269
270 switch (type)
271 {
272 case 'f':
273 prec = 2;
274 break;
275 case 'd':
276 prec = 4;
277 break;
278 default:
279 *sizeP = 0;
280 return _("bad call to md_atof");
281 }
282
283 t = atof_ieee (input_line_pointer, type, words);
284 if (t)
285 input_line_pointer = t;
286
287 *sizeP = prec * 2;
288
289 if (! target_big_endian)
290 for (i = prec - 1; i >= 0; i--, litP += 2)
291 md_number_to_chars (litP, (valueT) words[i], 2);
292 else
293 for (i = 0; i < prec; i++, litP += 2)
294 md_number_to_chars (litP, (valueT) words[i], 2);
295
296 return NULL;
297 }
298
299 /* Return true if STR starts with PREFIX, which should be a string literal. */
300 #define strprefix(STR, PREFIX) \
301 (strncmp ((STR), PREFIX, strlen (PREFIX)) == 0)
302
303
304 /* Return true if STR is prefixed with a special relocation operator. */
305 static int
306 nios2_special_relocation_p (const char *str)
307 {
308 return (strprefix (str, "%lo")
309 || strprefix (str, "%hi")
310 || strprefix (str, "%hiadj")
311 || strprefix (str, "%gprel")
312 || strprefix (str, "%got")
313 || strprefix (str, "%call")
314 || strprefix (str, "%gotoff_lo")
315 || strprefix (str, "%gotoff_hiadj")
316 || strprefix (str, "%tls_gd")
317 || strprefix (str, "%tls_ldm")
318 || strprefix (str, "%tls_ldo")
319 || strprefix (str, "%tls_ie")
320 || strprefix (str, "%tls_le")
321 || strprefix (str, "%gotoff"));
322 }
323
324
325 /* nop fill pattern for text section. */
326 static char const nop[4] = { 0x3a, 0x88, 0x01, 0x00 };
327
328 /* Handles all machine-dependent alignment needs. */
329 static void
330 nios2_align (int log_size, const char *pfill, symbolS *label)
331 {
332 int align;
333 long max_alignment = 15;
334
335 /* The front end is prone to changing segments out from under us
336 temporarily when -g is in effect. */
337 int switched_seg_p = (nios2_current_align_seg != now_seg);
338
339 align = log_size;
340 if (align > max_alignment)
341 {
342 align = max_alignment;
343 as_bad (_("Alignment too large: %d. assumed"), align);
344 }
345 else if (align < 0)
346 {
347 as_warn (_("Alignment negative: 0 assumed"));
348 align = 0;
349 }
350
351 if (align != 0)
352 {
353 if (subseg_text_p (now_seg) && align >= 2)
354 {
355 /* First, make sure we're on a four-byte boundary, in case
356 someone has been putting .byte values the text section. */
357 if (nios2_current_align < 2 || switched_seg_p)
358 frag_align (2, 0, 0);
359
360 /* Now fill in the alignment pattern. */
361 if (pfill != NULL)
362 frag_align_pattern (align, pfill, sizeof nop, 0);
363 else
364 frag_align (align, 0, 0);
365 }
366 else
367 frag_align (align, 0, 0);
368
369 if (!switched_seg_p)
370 nios2_current_align = align;
371
372 /* If the last label was in a different section we can't align it. */
373 if (label != NULL && !switched_seg_p)
374 {
375 symbolS *sym;
376 int label_seen = FALSE;
377 struct frag *old_frag;
378 valueT old_value;
379 valueT new_value;
380
381 gas_assert (S_GET_SEGMENT (label) == now_seg);
382
383 old_frag = symbol_get_frag (label);
384 old_value = S_GET_VALUE (label);
385 new_value = (valueT) frag_now_fix ();
386
387 /* It is possible to have more than one label at a particular
388 address, especially if debugging is enabled, so we must
389 take care to adjust all the labels at this address in this
390 fragment. To save time we search from the end of the symbol
391 list, backwards, since the symbols we are interested in are
392 almost certainly the ones that were most recently added.
393 Also to save time we stop searching once we have seen at least
394 one matching label, and we encounter a label that is no longer
395 in the target fragment. Note, this search is guaranteed to
396 find at least one match when sym == label, so no special case
397 code is necessary. */
398 for (sym = symbol_lastP; sym != NULL; sym = symbol_previous (sym))
399 if (symbol_get_frag (sym) == old_frag
400 && S_GET_VALUE (sym) == old_value)
401 {
402 label_seen = TRUE;
403 symbol_set_frag (sym, frag_now);
404 S_SET_VALUE (sym, new_value);
405 }
406 else if (label_seen && symbol_get_frag (sym) != old_frag)
407 break;
408 }
409 record_alignment (now_seg, align);
410 }
411 }
412
413 \f
414 /** Support for self-check mode. */
415
416 /* Mode of the assembler. */
417 typedef enum
418 {
419 NIOS2_MODE_ASSEMBLE, /* Ordinary operation. */
420 NIOS2_MODE_TEST /* Hidden mode used for self testing. */
421 } NIOS2_MODE;
422
423 static NIOS2_MODE nios2_mode = NIOS2_MODE_ASSEMBLE;
424
425 /* This function is used to in self-checking mode
426 to check the assembled instruction
427 opcode should be the assembled opcode, and exp_opcode
428 the parsed string representing the expected opcode. */
429 static void
430 nios2_check_assembly (unsigned int opcode, const char *exp_opcode)
431 {
432 if (nios2_mode == NIOS2_MODE_TEST)
433 {
434 if (exp_opcode == NULL)
435 as_bad (_("expecting opcode string in self test mode"));
436 else if (opcode != strtoul (exp_opcode, NULL, 16))
437 as_bad (_("assembly 0x%08x, expected %s"), opcode, exp_opcode);
438 }
439 }
440
441 \f
442 /** Support for machine-dependent assembler directives. */
443 /* Handle the .align pseudo-op. This aligns to a power of two. It
444 also adjusts any current instruction label. We treat this the same
445 way the MIPS port does: .align 0 turns off auto alignment. */
446 static void
447 s_nios2_align (int ignore ATTRIBUTE_UNUSED)
448 {
449 int align;
450 char fill;
451 const char *pfill = NULL;
452 long max_alignment = 15;
453
454 align = get_absolute_expression ();
455 if (align > max_alignment)
456 {
457 align = max_alignment;
458 as_bad (_("Alignment too large: %d. assumed"), align);
459 }
460 else if (align < 0)
461 {
462 as_warn (_("Alignment negative: 0 assumed"));
463 align = 0;
464 }
465
466 if (*input_line_pointer == ',')
467 {
468 input_line_pointer++;
469 fill = get_absolute_expression ();
470 pfill = (const char *) &fill;
471 }
472 else if (subseg_text_p (now_seg))
473 pfill = (const char *) &nop;
474 else
475 {
476 pfill = NULL;
477 nios2_last_label = NULL;
478 }
479
480 if (align != 0)
481 {
482 nios2_auto_align_on = 1;
483 nios2_align (align, pfill, nios2_last_label);
484 nios2_last_label = NULL;
485 }
486 else
487 nios2_auto_align_on = 0;
488
489 demand_empty_rest_of_line ();
490 }
491
492 /* Handle the .text pseudo-op. This is like the usual one, but it
493 clears the saved last label and resets known alignment. */
494 static void
495 s_nios2_text (int i)
496 {
497 s_text (i);
498 nios2_last_label = NULL;
499 nios2_current_align = 0;
500 nios2_current_align_seg = now_seg;
501 }
502
503 /* Handle the .data pseudo-op. This is like the usual one, but it
504 clears the saved last label and resets known alignment. */
505 static void
506 s_nios2_data (int i)
507 {
508 s_data (i);
509 nios2_last_label = NULL;
510 nios2_current_align = 0;
511 nios2_current_align_seg = now_seg;
512 }
513
514 /* Handle the .section pseudo-op. This is like the usual one, but it
515 clears the saved last label and resets known alignment. */
516 static void
517 s_nios2_section (int ignore)
518 {
519 obj_elf_section (ignore);
520 nios2_last_label = NULL;
521 nios2_current_align = 0;
522 nios2_current_align_seg = now_seg;
523 }
524
525 /* Explicitly unaligned cons. */
526 static void
527 s_nios2_ucons (int nbytes)
528 {
529 int hold;
530 hold = nios2_auto_align_on;
531 nios2_auto_align_on = 0;
532 cons (nbytes);
533 nios2_auto_align_on = hold;
534 }
535
536 /* Handle the .sdata directive. */
537 static void
538 s_nios2_sdata (int ignore ATTRIBUTE_UNUSED)
539 {
540 get_absolute_expression (); /* Ignored. */
541 subseg_new (".sdata", 0);
542 demand_empty_rest_of_line ();
543 }
544
545 /* .set sets assembler options eg noat/at and is also used
546 to set symbol values (.equ, .equiv ). */
547 static void
548 s_nios2_set (int equiv)
549 {
550 char *directive = input_line_pointer;
551 char delim = get_symbol_end ();
552 char *endline = input_line_pointer;
553 *endline = delim;
554
555 /* We only want to handle ".set XXX" if the
556 user has tried ".set XXX, YYY" they are not
557 trying a directive. This prevents
558 us from polluting the name space. */
559 SKIP_WHITESPACE ();
560 if (is_end_of_line[(unsigned char) *input_line_pointer])
561 {
562 bfd_boolean done = TRUE;
563 *endline = 0;
564
565 if (!strcmp (directive, "noat"))
566 nios2_as_options.noat = TRUE;
567 else if (!strcmp (directive, "at"))
568 nios2_as_options.noat = FALSE;
569 else if (!strcmp (directive, "nobreak"))
570 nios2_as_options.nobreak = TRUE;
571 else if (!strcmp (directive, "break"))
572 nios2_as_options.nobreak = FALSE;
573 else if (!strcmp (directive, "norelax"))
574 nios2_as_options.relax = relax_none;
575 else if (!strcmp (directive, "relaxsection"))
576 nios2_as_options.relax = relax_section;
577 else if (!strcmp (directive, "relaxall"))
578 nios2_as_options.relax = relax_all;
579 else
580 done = FALSE;
581
582 if (done)
583 {
584 *endline = delim;
585 demand_empty_rest_of_line ();
586 return;
587 }
588 }
589
590 /* If we fall through to here, either we have ".set XXX, YYY"
591 or we have ".set XXX" where XXX is unknown or we have
592 a syntax error. */
593 input_line_pointer = directive;
594 *endline = delim;
595 s_set (equiv);
596 }
597
598 /* Machine-dependent assembler directives.
599 Format of each entry is:
600 { "directive", handler_func, param } */
601 const pseudo_typeS md_pseudo_table[] = {
602 {"align", s_nios2_align, 0},
603 {"text", s_nios2_text, 0},
604 {"data", s_nios2_data, 0},
605 {"section", s_nios2_section, 0},
606 {"section.s", s_nios2_section, 0},
607 {"sect", s_nios2_section, 0},
608 {"sect.s", s_nios2_section, 0},
609 /* .dword and .half are included for compatibility with MIPS. */
610 {"dword", cons, 8},
611 {"half", cons, 2},
612 /* NIOS2 native word size is 4 bytes, so we override
613 the GAS default of 2. */
614 {"word", cons, 4},
615 /* Explicitly unaligned directives. */
616 {"2byte", s_nios2_ucons, 2},
617 {"4byte", s_nios2_ucons, 4},
618 {"8byte", s_nios2_ucons, 8},
619 {"16byte", s_nios2_ucons, 16},
620 #ifdef OBJ_ELF
621 {"sdata", s_nios2_sdata, 0},
622 #endif
623 {"set", s_nios2_set, 0},
624 {NULL, NULL, 0}
625 };
626
627 \f
628 /** Relaxation support. */
629
630 /* We support two relaxation modes: a limited PC-relative mode with
631 -relax-section (the default), and an absolute jump mode with -relax-all.
632
633 Nios II PC-relative branch instructions only support 16-bit offsets.
634 And, there's no good way to add a 32-bit constant to the PC without
635 using two registers.
636
637 To deal with this, for the pc-relative relaxation mode we convert
638 br label
639 into a series of 16-bit adds, like:
640 nextpc at
641 addi at, at, 32767
642 ...
643 addi at, at, remainder
644 jmp at
645
646 Similarly, conditional branches are converted from
647 b(condition) r, s, label
648 into a series like:
649 b(opposite condition) r, s, skip
650 nextpc at
651 addi at, at, 32767
652 ...
653 addi at, at, remainder
654 jmp at
655 skip:
656
657 The compiler can do a better job, either by converting the branch
658 directly into a JMP (going through the GOT for PIC) or by allocating
659 a second register for the 32-bit displacement.
660
661 For the -relax-all relaxation mode, the conversions are
662 movhi at, %hi(symbol+offset)
663 ori at, %lo(symbol+offset)
664 jmp at
665 and
666 b(opposite condition), r, s, skip
667 movhi at, %hi(symbol+offset)
668 ori at, %lo(symbol+offset)
669 jmp at
670 skip:
671 respectively.
672 */
673
674 /* Arbitrarily limit the number of addis we can insert; we need to be able
675 to specify the maximum growth size for each frag that contains a
676 relaxable branch. There's no point in specifying a huge number here
677 since that means the assembler needs to allocate that much extra
678 memory for every branch, and almost no real code will ever need it.
679 Plus, as already noted a better solution is to just use a jmp, or
680 allocate a second register to hold a 32-bit displacement.
681 FIXME: Rather than making this a constant, it could be controlled by
682 a command-line argument. */
683 #define RELAX_MAX_ADDI 32
684
685 /* The fr_subtype field represents the target-specific relocation state.
686 It has type relax_substateT (unsigned int). We use it to track the
687 number of addis necessary, plus a bit to track whether this is a
688 conditional branch.
689 Regardless of the smaller RELAX_MAX_ADDI limit, we reserve 16 bits
690 in the fr_subtype to encode the number of addis so that the whole
691 theoretically-valid range is representable.
692 For the -relax-all mode, N = 0 represents an in-range branch and N = 1
693 represents a branch that needs to be relaxed. */
694 #define UBRANCH (0 << 16)
695 #define CBRANCH (1 << 16)
696 #define IS_CBRANCH(SUBTYPE) ((SUBTYPE) & CBRANCH)
697 #define IS_UBRANCH(SUBTYPE) (!IS_CBRANCH (SUBTYPE))
698 #define UBRANCH_SUBTYPE(N) (UBRANCH | (N))
699 #define CBRANCH_SUBTYPE(N) (CBRANCH | (N))
700 #define SUBTYPE_ADDIS(SUBTYPE) ((SUBTYPE) & 0xffff)
701
702 /* For the -relax-section mode, unconditional branches require 2 extra i
703 nstructions besides the addis, conditional branches require 3. */
704 #define UBRANCH_ADDIS_TO_SIZE(N) (((N) + 2) * 4)
705 #define CBRANCH_ADDIS_TO_SIZE(N) (((N) + 3) * 4)
706
707 /* For the -relax-all mode, unconditional branches require 3 instructions
708 and conditional branches require 4. */
709 #define UBRANCH_JUMP_SIZE 12
710 #define CBRANCH_JUMP_SIZE 16
711
712 /* Maximum sizes of relaxation sequences. */
713 #define UBRANCH_MAX_SIZE \
714 (nios2_as_options.relax == relax_all \
715 ? UBRANCH_JUMP_SIZE \
716 : UBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
717 #define CBRANCH_MAX_SIZE \
718 (nios2_as_options.relax == relax_all \
719 ? CBRANCH_JUMP_SIZE \
720 : CBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
721
722 /* Register number of AT, the assembler temporary. */
723 #define AT_REGNUM 1
724
725 /* Determine how many bytes are required to represent the sequence
726 indicated by SUBTYPE. */
727 static int
728 nios2_relax_subtype_size (relax_substateT subtype)
729 {
730 int n = SUBTYPE_ADDIS (subtype);
731 if (n == 0)
732 /* Regular conditional/unconditional branch instruction. */
733 return 4;
734 else if (nios2_as_options.relax == relax_all)
735 return (IS_CBRANCH (subtype) ? CBRANCH_JUMP_SIZE : UBRANCH_JUMP_SIZE);
736 else if (IS_CBRANCH (subtype))
737 return CBRANCH_ADDIS_TO_SIZE (n);
738 else
739 return UBRANCH_ADDIS_TO_SIZE (n);
740 }
741
742 /* Estimate size of fragp before relaxation.
743 This could also examine the offset in fragp and adjust
744 fragp->fr_subtype, but we will do that in nios2_relax_frag anyway. */
745 int
746 md_estimate_size_before_relax (fragS *fragp, segT segment ATTRIBUTE_UNUSED)
747 {
748 return nios2_relax_subtype_size (fragp->fr_subtype);
749 }
750
751 /* Implement md_relax_frag, returning the change in size of the frag. */
752 long
753 nios2_relax_frag (segT segment, fragS *fragp, long stretch)
754 {
755 addressT target = fragp->fr_offset;
756 relax_substateT subtype = fragp->fr_subtype;
757 symbolS *symbolp = fragp->fr_symbol;
758
759 if (symbolp)
760 {
761 fragS *sym_frag = symbol_get_frag (symbolp);
762 offsetT offset;
763 int n;
764
765 target += S_GET_VALUE (symbolp);
766
767 /* See comments in write.c:relax_frag about handling of stretch. */
768 if (stretch != 0
769 && sym_frag->relax_marker != fragp->relax_marker)
770 {
771 if (stretch < 0 || sym_frag->region == fragp->region)
772 target += stretch;
773 else if (target < fragp->fr_address)
774 target = fragp->fr_next->fr_address + stretch;
775 }
776
777 /* We subtract fr_var (4 for 32-bit insns) because all pc relative
778 branches are from the next instruction. */
779 offset = target - fragp->fr_address - fragp->fr_fix - fragp->fr_var;
780 if (offset >= -32768 && offset <= 32764)
781 /* Fits in PC-relative branch. */
782 n = 0;
783 else if (nios2_as_options.relax == relax_all)
784 /* Convert to jump. */
785 n = 1;
786 else if (nios2_as_options.relax == relax_section
787 && S_GET_SEGMENT (symbolp) == segment
788 && S_IS_DEFINED (symbolp))
789 /* Attempt a PC-relative relaxation on a branch to a defined
790 symbol in the same segment. */
791 {
792 /* The relaxation for conditional branches is offset by 4
793 bytes because we insert the inverted branch around the
794 sequence. */
795 if (IS_CBRANCH (subtype))
796 offset = offset - 4;
797 if (offset > 0)
798 n = offset / 32767 + 1;
799 else
800 n = offset / -32768 + 1;
801
802 /* Bail out immediately if relaxation has failed. If we try to
803 defer the diagnostic to md_convert_frag, some pathological test
804 cases (e.g. gcc/testsuite/gcc.c-torture/compile/20001226-1.c)
805 apparently never converge. By returning 0 here we could pretend
806 to the caller that nothing has changed, but that leaves things
807 in an inconsistent state when we get to md_convert_frag. */
808 if (n > RELAX_MAX_ADDI)
809 {
810 as_bad_where (fragp->fr_file, fragp->fr_line,
811 _("branch offset out of range\n"));
812 as_fatal (_("branch relaxation failed\n"));
813 }
814 }
815 else
816 /* We cannot handle this case, diagnose overflow later. */
817 return 0;
818
819 if (IS_CBRANCH (subtype))
820 fragp->fr_subtype = CBRANCH_SUBTYPE (n);
821 else
822 fragp->fr_subtype = UBRANCH_SUBTYPE (n);
823
824 return (nios2_relax_subtype_size (fragp->fr_subtype)
825 - nios2_relax_subtype_size (subtype));
826 }
827
828 /* If we got here, it's probably an error. */
829 return 0;
830 }
831
832
833 /* Complete fragp using the data from the relaxation pass. */
834 void
835 md_convert_frag (bfd *headers ATTRIBUTE_UNUSED, segT segment ATTRIBUTE_UNUSED,
836 fragS *fragp)
837 {
838 char *buffer = fragp->fr_literal + fragp->fr_fix;
839 relax_substateT subtype = fragp->fr_subtype;
840 int n = SUBTYPE_ADDIS (subtype);
841 addressT target = fragp->fr_offset;
842 symbolS *symbolp = fragp->fr_symbol;
843 offsetT offset;
844 unsigned int addend_mask, addi_mask;
845 offsetT addend, remainder;
846 int i;
847
848 /* If we didn't or can't relax, this is a regular branch instruction.
849 We just need to generate the fixup for the symbol and offset. */
850 if (n == 0)
851 {
852 fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol, fragp->fr_offset, 1,
853 BFD_RELOC_16_PCREL);
854 fragp->fr_fix += 4;
855 return;
856 }
857
858 /* Replace the cbranch at fr_fix with one that has the opposite condition
859 in order to jump around the block of instructions we'll be adding. */
860 if (IS_CBRANCH (subtype))
861 {
862 unsigned int br_opcode;
863 unsigned int old_op, new_op;
864 int nbytes;
865
866 /* Account for the nextpc and jmp in the pc-relative case, or the two
867 load instructions and jump in the absolute case. */
868 if (nios2_as_options.relax == relax_section)
869 nbytes = (n + 2) * 4;
870 else
871 nbytes = 12;
872
873 br_opcode = md_chars_to_number (buffer, 4);
874 old_op = GET_IW_R1_OP (br_opcode);
875 switch (old_op)
876 {
877 case R1_OP_BEQ:
878 new_op = R1_OP_BNE;
879 break;
880 case R1_OP_BNE:
881 new_op = R1_OP_BEQ;
882 break;
883 case R1_OP_BGE:
884 new_op = R1_OP_BLT;
885 break;
886 case R1_OP_BGEU:
887 new_op = R1_OP_BLTU;
888 break;
889 case R1_OP_BLT:
890 new_op = R1_OP_BGE;
891 break;
892 case R1_OP_BLTU:
893 new_op = R1_OP_BGEU;
894 break;
895 default:
896 as_bad_where (fragp->fr_file, fragp->fr_line,
897 _("expecting conditional branch for relaxation\n"));
898 abort ();
899 }
900
901 br_opcode = (br_opcode & ~IW_R1_OP_SHIFTED_MASK) | SET_IW_R1_OP (new_op);
902 br_opcode = br_opcode | SET_IW_I_IMM16 (nbytes);
903 md_number_to_chars (buffer, br_opcode, 4);
904 fragp->fr_fix += 4;
905 buffer += 4;
906 }
907
908 /* Load at for the PC-relative case. */
909 if (nios2_as_options.relax == relax_section)
910 {
911 /* Insert the nextpc instruction. */
912 md_number_to_chars (buffer,
913 MATCH_R1_NEXTPC | SET_IW_R_C (AT_REGNUM), 4);
914 fragp->fr_fix += 4;
915 buffer += 4;
916
917 /* We need to know whether the offset is positive or negative. */
918 target += S_GET_VALUE (symbolp);
919 offset = target - fragp->fr_address - fragp->fr_fix;
920 if (offset > 0)
921 addend = 32767;
922 else
923 addend = -32768;
924 addend_mask = SET_IW_I_IMM16 ((unsigned int)addend);
925
926 /* Insert n-1 addi instructions. */
927 addi_mask = (MATCH_R1_ADDI
928 | SET_IW_I_B (AT_REGNUM)
929 | SET_IW_I_A (AT_REGNUM));
930 for (i = 0; i < n - 1; i ++)
931 {
932 md_number_to_chars (buffer, addi_mask | addend_mask, 4);
933 fragp->fr_fix += 4;
934 buffer += 4;
935 }
936
937 /* Insert the last addi instruction to hold the remainder. */
938 remainder = offset - addend * (n - 1);
939 gas_assert (remainder >= -32768 && remainder <= 32767);
940 addend_mask = SET_IW_I_IMM16 ((unsigned int)remainder);
941 md_number_to_chars (buffer, addi_mask | addend_mask, 4);
942 fragp->fr_fix += 4;
943 buffer += 4;
944 }
945
946 /* Load at for the absolute case. */
947 else
948 {
949 md_number_to_chars (buffer,
950 (MATCH_R1_ORHI | SET_IW_I_B (AT_REGNUM)
951 | SET_IW_I_A (0)),
952 4);
953 fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol, fragp->fr_offset,
954 0, BFD_RELOC_NIOS2_HI16);
955 fragp->fr_fix += 4;
956 buffer += 4;
957 md_number_to_chars (buffer,
958 (MATCH_R1_ORI | SET_IW_I_B (AT_REGNUM)
959 | SET_IW_I_A (AT_REGNUM)),
960 4);
961 fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol, fragp->fr_offset,
962 0, BFD_RELOC_NIOS2_LO16);
963 fragp->fr_fix += 4;
964 buffer += 4;
965 }
966
967 /* Insert the jmp instruction. */
968 md_number_to_chars (buffer, MATCH_R1_JMP | SET_IW_R_A (AT_REGNUM), 4);
969 fragp->fr_fix += 4;
970 buffer += 4;
971 }
972
973 \f
974 /** Fixups and overflow checking. */
975
976 /* Check a fixup for overflow. */
977 static bfd_boolean
978 nios2_check_overflow (valueT fixup, reloc_howto_type *howto)
979 {
980 /* Apply the rightshift before checking for overflow. */
981 fixup = ((signed)fixup) >> howto->rightshift;
982
983 /* Check for overflow - return TRUE if overflow, FALSE if not. */
984 switch (howto->complain_on_overflow)
985 {
986 case complain_overflow_dont:
987 break;
988 case complain_overflow_bitfield:
989 if ((fixup >> howto->bitsize) != 0
990 && ((signed) fixup >> howto->bitsize) != -1)
991 return TRUE;
992 break;
993 case complain_overflow_signed:
994 if ((fixup & 0x80000000) > 0)
995 {
996 /* Check for negative overflow. */
997 if ((signed) fixup < ((signed) 0x80000000 >> howto->bitsize))
998 return TRUE;
999 }
1000 else
1001 {
1002 /* Check for positive overflow. */
1003 if (fixup >= ((unsigned) 1 << (howto->bitsize - 1)))
1004 return TRUE;
1005 }
1006 break;
1007 case complain_overflow_unsigned:
1008 if ((fixup >> howto->bitsize) != 0)
1009 return TRUE;
1010 break;
1011 default:
1012 as_bad (_("error checking for overflow - broken assembler"));
1013 break;
1014 }
1015 return FALSE;
1016 }
1017
1018 /* Emit diagnostic for fixup overflow. */
1019 static void
1020 nios2_diagnose_overflow (valueT fixup, reloc_howto_type *howto,
1021 fixS *fixP, valueT value)
1022 {
1023 if (fixP->fx_r_type == BFD_RELOC_8
1024 || fixP->fx_r_type == BFD_RELOC_16
1025 || fixP->fx_r_type == BFD_RELOC_32)
1026 /* These relocs are against data, not instructions. */
1027 as_bad_where (fixP->fx_file, fixP->fx_line,
1028 _("immediate value 0x%x truncated to 0x%x"),
1029 (unsigned int) fixup,
1030 (unsigned int) (~(~(valueT) 0 << howto->bitsize) & fixup));
1031 else
1032 {
1033 /* What opcode is the instruction? This will determine
1034 whether we check for overflow in immediate values
1035 and what error message we get. */
1036 const struct nios2_opcode *opcode;
1037 enum overflow_type overflow_msg_type;
1038 unsigned int range_min;
1039 unsigned int range_max;
1040 unsigned int address;
1041
1042 opcode = nios2_find_opcode_hash (value, bfd_get_mach (stdoutput));
1043 gas_assert (opcode);
1044 gas_assert (fixP->fx_size == opcode->size);
1045 overflow_msg_type = opcode->overflow_msg;
1046 switch (overflow_msg_type)
1047 {
1048 case call_target_overflow:
1049 range_min
1050 = ((fixP->fx_frag->fr_address + fixP->fx_where) & 0xf0000000);
1051 range_max = range_min + 0x0fffffff;
1052 address = fixup | range_min;
1053
1054 as_bad_where (fixP->fx_file, fixP->fx_line,
1055 _("call target address 0x%08x out of range 0x%08x to 0x%08x"),
1056 address, range_min, range_max);
1057 break;
1058 case branch_target_overflow:
1059 as_bad_where (fixP->fx_file, fixP->fx_line,
1060 _("branch offset %d out of range %d to %d"),
1061 (int)fixup, -32768, 32767);
1062 break;
1063 case address_offset_overflow:
1064 as_bad_where (fixP->fx_file, fixP->fx_line,
1065 _("%s offset %d out of range %d to %d"),
1066 opcode->name, (int)fixup, -32768, 32767);
1067 break;
1068 case signed_immed16_overflow:
1069 as_bad_where (fixP->fx_file, fixP->fx_line,
1070 _("immediate value %d out of range %d to %d"),
1071 (int)fixup, -32768, 32767);
1072 break;
1073 case unsigned_immed16_overflow:
1074 as_bad_where (fixP->fx_file, fixP->fx_line,
1075 _("immediate value %u out of range %u to %u"),
1076 (unsigned int)fixup, 0, 65535);
1077 break;
1078 case unsigned_immed5_overflow:
1079 as_bad_where (fixP->fx_file, fixP->fx_line,
1080 _("immediate value %u out of range %u to %u"),
1081 (unsigned int)fixup, 0, 31);
1082 break;
1083 case custom_opcode_overflow:
1084 as_bad_where (fixP->fx_file, fixP->fx_line,
1085 _("custom instruction opcode %u out of range %u to %u"),
1086 (unsigned int)fixup, 0, 255);
1087 break;
1088 default:
1089 as_bad_where (fixP->fx_file, fixP->fx_line,
1090 _("overflow in immediate argument"));
1091 break;
1092 }
1093 }
1094 }
1095
1096 /* Apply a fixup to the object file. */
1097 void
1098 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
1099 {
1100 /* Assert that the fixup is one we can handle. */
1101 gas_assert (fixP != NULL && valP != NULL
1102 && (fixP->fx_r_type == BFD_RELOC_8
1103 || fixP->fx_r_type == BFD_RELOC_16
1104 || fixP->fx_r_type == BFD_RELOC_32
1105 || fixP->fx_r_type == BFD_RELOC_64
1106 || fixP->fx_r_type == BFD_RELOC_NIOS2_S16
1107 || fixP->fx_r_type == BFD_RELOC_NIOS2_U16
1108 || fixP->fx_r_type == BFD_RELOC_16_PCREL
1109 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL26
1110 || fixP->fx_r_type == BFD_RELOC_NIOS2_IMM5
1111 || fixP->fx_r_type == BFD_RELOC_NIOS2_CACHE_OPX
1112 || fixP->fx_r_type == BFD_RELOC_NIOS2_IMM6
1113 || fixP->fx_r_type == BFD_RELOC_NIOS2_IMM8
1114 || fixP->fx_r_type == BFD_RELOC_NIOS2_HI16
1115 || fixP->fx_r_type == BFD_RELOC_NIOS2_LO16
1116 || fixP->fx_r_type == BFD_RELOC_NIOS2_HIADJ16
1117 || fixP->fx_r_type == BFD_RELOC_NIOS2_GPREL
1118 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1119 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
1120 || fixP->fx_r_type == BFD_RELOC_NIOS2_UJMP
1121 || fixP->fx_r_type == BFD_RELOC_NIOS2_CJMP
1122 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALLR
1123 || fixP->fx_r_type == BFD_RELOC_NIOS2_ALIGN
1124 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOT16
1125 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL16
1126 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_LO
1127 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_HA
1128 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_GD16
1129 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LDM16
1130 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LDO16
1131 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_IE16
1132 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LE16
1133 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF
1134 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPREL
1135 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL26_NOAT
1136 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOT_LO
1137 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOT_HA
1138 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL_LO
1139 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL_HA
1140 /* Add other relocs here as we generate them. */
1141 ));
1142
1143 if (fixP->fx_r_type == BFD_RELOC_64)
1144 {
1145 /* We may reach here due to .8byte directives, but we never output
1146 BFD_RELOC_64; it must be resolved. */
1147 if (fixP->fx_addsy != NULL)
1148 as_bad_where (fixP->fx_file, fixP->fx_line,
1149 _("cannot create 64-bit relocation"));
1150 else
1151 {
1152 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1153 *valP, 8);
1154 fixP->fx_done = 1;
1155 }
1156 return;
1157 }
1158
1159 /* The value passed in valP can be the value of a fully
1160 resolved expression, or it can be the value of a partially
1161 resolved expression. In the former case, both fixP->fx_addsy
1162 and fixP->fx_subsy are NULL, and fixP->fx_offset == *valP, and
1163 we can fix up the instruction that fixP relates to.
1164 In the latter case, one or both of fixP->fx_addsy and
1165 fixP->fx_subsy are not NULL, and fixP->fx_offset may or may not
1166 equal *valP. We don't need to check for fixP->fx_subsy being null
1167 because the generic part of the assembler generates an error if
1168 it is not an absolute symbol. */
1169 if (fixP->fx_addsy != NULL)
1170 /* Partially resolved expression. */
1171 {
1172 fixP->fx_addnumber = fixP->fx_offset;
1173 fixP->fx_done = 0;
1174
1175 switch (fixP->fx_r_type)
1176 {
1177 case BFD_RELOC_NIOS2_TLS_GD16:
1178 case BFD_RELOC_NIOS2_TLS_LDM16:
1179 case BFD_RELOC_NIOS2_TLS_LDO16:
1180 case BFD_RELOC_NIOS2_TLS_IE16:
1181 case BFD_RELOC_NIOS2_TLS_LE16:
1182 case BFD_RELOC_NIOS2_TLS_DTPMOD:
1183 case BFD_RELOC_NIOS2_TLS_DTPREL:
1184 case BFD_RELOC_NIOS2_TLS_TPREL:
1185 S_SET_THREAD_LOCAL (fixP->fx_addsy);
1186 break;
1187 default:
1188 break;
1189 }
1190 }
1191 else
1192 /* Fully resolved fixup. */
1193 {
1194 reloc_howto_type *howto
1195 = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
1196
1197 if (howto == NULL)
1198 as_bad_where (fixP->fx_file, fixP->fx_line,
1199 _("relocation is not supported"));
1200 else
1201 {
1202 valueT fixup = *valP;
1203 valueT value;
1204 char *buf;
1205
1206 /* If this is a pc-relative relocation, we need to
1207 subtract the current offset within the object file
1208 FIXME : for some reason fixP->fx_pcrel isn't 1 when it should be
1209 so I'm using the howto structure instead to determine this. */
1210 if (howto->pc_relative == 1)
1211 fixup = fixup - (fixP->fx_frag->fr_address + fixP->fx_where
1212 + fixP->fx_size);
1213
1214 /* Get the instruction or data to be fixed up. */
1215 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
1216 value = md_chars_to_number (buf, fixP->fx_size);
1217
1218 /* Check for overflow, emitting a diagnostic if necessary. */
1219 if (nios2_check_overflow (fixup, howto))
1220 nios2_diagnose_overflow (fixup, howto, fixP, value);
1221
1222 /* Apply the right shift. */
1223 fixup = ((signed)fixup) >> howto->rightshift;
1224
1225 /* Truncate the fixup to right size. */
1226 switch (fixP->fx_r_type)
1227 {
1228 case BFD_RELOC_NIOS2_HI16:
1229 fixup = (fixup >> 16) & 0xFFFF;
1230 break;
1231 case BFD_RELOC_NIOS2_LO16:
1232 fixup = fixup & 0xFFFF;
1233 break;
1234 case BFD_RELOC_NIOS2_HIADJ16:
1235 fixup = ((((fixup >> 16) & 0xFFFF) + ((fixup >> 15) & 0x01))
1236 & 0xFFFF);
1237 break;
1238 default:
1239 {
1240 int n = sizeof (fixup) * 8 - howto->bitsize;
1241 fixup = (fixup << n) >> n;
1242 break;
1243 }
1244 }
1245
1246 /* Fix up the instruction. */
1247 value = (value & ~howto->dst_mask) | (fixup << howto->bitpos);
1248 md_number_to_chars (buf, value, fixP->fx_size);
1249 }
1250
1251 fixP->fx_done = 1;
1252 }
1253
1254 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
1255 {
1256 fixP->fx_done = 0;
1257 if (fixP->fx_addsy
1258 && !S_IS_DEFINED (fixP->fx_addsy) && !S_IS_WEAK (fixP->fx_addsy))
1259 S_SET_WEAK (fixP->fx_addsy);
1260 }
1261 else if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1262 fixP->fx_done = 0;
1263 }
1264
1265
1266 \f
1267 /** Instruction parsing support. */
1268
1269 /* General internal error routine. */
1270
1271 static void
1272 bad_opcode (const struct nios2_opcode *op)
1273 {
1274 fprintf (stderr, _("internal error: broken opcode descriptor for `%s %s'\n"),
1275 op->name, op->args);
1276 as_fatal (_("Broken assembler. No assembly attempted."));
1277 }
1278
1279 /* Special relocation directive strings. */
1280
1281 struct nios2_special_relocS
1282 {
1283 const char *string;
1284 bfd_reloc_code_real_type reloc_type;
1285 };
1286
1287 /* This table is sorted so that prefix strings are listed after the longer
1288 strings that include them -- e.g., %got after %got_hiadj, etc. */
1289
1290 struct nios2_special_relocS nios2_special_reloc[] = {
1291 {"%hiadj", BFD_RELOC_NIOS2_HIADJ16},
1292 {"%hi", BFD_RELOC_NIOS2_HI16},
1293 {"%lo", BFD_RELOC_NIOS2_LO16},
1294 {"%gprel", BFD_RELOC_NIOS2_GPREL},
1295 {"%call_lo", BFD_RELOC_NIOS2_CALL_LO},
1296 {"%call_hiadj", BFD_RELOC_NIOS2_CALL_HA},
1297 {"%call", BFD_RELOC_NIOS2_CALL16},
1298 {"%gotoff_lo", BFD_RELOC_NIOS2_GOTOFF_LO},
1299 {"%gotoff_hiadj", BFD_RELOC_NIOS2_GOTOFF_HA},
1300 {"%gotoff", BFD_RELOC_NIOS2_GOTOFF},
1301 {"%got_hiadj", BFD_RELOC_NIOS2_GOT_HA},
1302 {"%got_lo", BFD_RELOC_NIOS2_GOT_LO},
1303 {"%got", BFD_RELOC_NIOS2_GOT16},
1304 {"%tls_gd", BFD_RELOC_NIOS2_TLS_GD16},
1305 {"%tls_ldm", BFD_RELOC_NIOS2_TLS_LDM16},
1306 {"%tls_ldo", BFD_RELOC_NIOS2_TLS_LDO16},
1307 {"%tls_ie", BFD_RELOC_NIOS2_TLS_IE16},
1308 {"%tls_le", BFD_RELOC_NIOS2_TLS_LE16},
1309 };
1310
1311 #define NIOS2_NUM_SPECIAL_RELOCS \
1312 (sizeof(nios2_special_reloc)/sizeof(nios2_special_reloc[0]))
1313 const int nios2_num_special_relocs = NIOS2_NUM_SPECIAL_RELOCS;
1314
1315 /* Creates a new nios2_insn_relocS and returns a pointer to it. */
1316 static nios2_insn_relocS *
1317 nios2_insn_reloc_new (bfd_reloc_code_real_type reloc_type, unsigned int pcrel)
1318 {
1319 nios2_insn_relocS *retval;
1320 retval = (nios2_insn_relocS *) malloc (sizeof (nios2_insn_relocS));
1321 if (retval == NULL)
1322 {
1323 as_bad (_("can't create relocation"));
1324 abort ();
1325 }
1326
1327 /* Fill out the fields with default values. */
1328 retval->reloc_next = NULL;
1329 retval->reloc_type = reloc_type;
1330 retval->reloc_pcrel = pcrel;
1331 return retval;
1332 }
1333
1334 /* Frees up memory previously allocated by nios2_insn_reloc_new(). */
1335 /* FIXME: this is never called; memory leak? */
1336 #if 0
1337 static void
1338 nios2_insn_reloc_destroy (nios2_insn_relocS *reloc)
1339 {
1340 gas_assert (reloc != NULL);
1341 free (reloc);
1342 }
1343 #endif
1344
1345 /* Look up a register name and validate it for the given regtype.
1346 Return the register mapping or NULL on failure. */
1347 static struct nios2_reg *
1348 nios2_parse_reg (const char *token, unsigned long regtype)
1349 {
1350 struct nios2_reg *reg = nios2_reg_lookup (token);
1351
1352 if (reg == NULL)
1353 {
1354 as_bad (_("unknown register %s"), token);
1355 return NULL;
1356 }
1357
1358 /* Matched a register, but is it the wrong type? */
1359 if (!(regtype & reg->regtype))
1360 {
1361 if (regtype & REG_CONTROL)
1362 as_bad (_("expecting control register"));
1363 else if (reg->regtype & REG_CONTROL)
1364 as_bad (_("illegal use of control register"));
1365 else if (reg->regtype & REG_COPROCESSOR)
1366 as_bad (_("illegal use of coprocessor register"));
1367 else
1368 as_bad (_("invalid register %s"), token);
1369 return NULL;
1370 }
1371
1372 /* Warn for explicit use of special registers. */
1373 if (reg->regtype & REG_NORMAL)
1374 {
1375 if (!nios2_as_options.noat && reg->index == 1)
1376 as_warn (_("Register at (r1) can sometimes be corrupted by "
1377 "assembler optimizations.\n"
1378 "Use .set noat to turn off those optimizations "
1379 "(and this warning)."));
1380 if (!nios2_as_options.nobreak && reg->index == 25)
1381 as_warn (_("The debugger will corrupt bt (r25).\n"
1382 "If you don't need to debug this "
1383 "code use .set nobreak to turn off this warning."));
1384 if (!nios2_as_options.nobreak && reg->index == 30)
1385 as_warn (_("The debugger will corrupt sstatus/ba (r30).\n"
1386 "If you don't need to debug this "
1387 "code use .set nobreak to turn off this warning."));
1388 }
1389
1390 return reg;
1391 }
1392
1393 /* The various nios2_assemble_* functions call this
1394 function to generate an expression from a string representing an expression.
1395 It then tries to evaluate the expression, and if it can, returns its value.
1396 If not, it creates a new nios2_insn_relocS and stores the expression and
1397 reloc_type for future use. */
1398 static unsigned long
1399 nios2_assemble_expression (const char *exprstr,
1400 nios2_insn_infoS *insn,
1401 bfd_reloc_code_real_type reloc_type,
1402 unsigned int pcrel)
1403 {
1404 nios2_insn_relocS *reloc;
1405 char *saved_line_ptr;
1406 unsigned short value;
1407 int i;
1408
1409 gas_assert (exprstr != NULL);
1410 gas_assert (insn != NULL);
1411
1412 /* Check for relocation operators.
1413 Change the relocation type and advance the ptr to the start of
1414 the expression proper. */
1415 for (i = 0; i < nios2_num_special_relocs; i++)
1416 if (strstr (exprstr, nios2_special_reloc[i].string) != NULL)
1417 {
1418 reloc_type = nios2_special_reloc[i].reloc_type;
1419 exprstr += strlen (nios2_special_reloc[i].string) + 1;
1420
1421 /* %lo and %hiadj have different meanings for PC-relative
1422 expressions. */
1423 if (pcrel)
1424 {
1425 if (reloc_type == BFD_RELOC_NIOS2_LO16)
1426 reloc_type = BFD_RELOC_NIOS2_PCREL_LO;
1427 if (reloc_type == BFD_RELOC_NIOS2_HIADJ16)
1428 reloc_type = BFD_RELOC_NIOS2_PCREL_HA;
1429 }
1430
1431 break;
1432 }
1433
1434 /* We potentially have a relocation. */
1435 reloc = nios2_insn_reloc_new (reloc_type, pcrel);
1436 reloc->reloc_next = insn->insn_reloc;
1437 insn->insn_reloc = reloc;
1438
1439 /* Parse the expression string. */
1440 saved_line_ptr = input_line_pointer;
1441 input_line_pointer = (char *) exprstr;
1442 expression (&reloc->reloc_expression);
1443 input_line_pointer = saved_line_ptr;
1444
1445 /* This is redundant as the fixup will put this into
1446 the instruction, but it is included here so that
1447 self-test mode (-r) works. */
1448 value = 0;
1449 if (nios2_mode == NIOS2_MODE_TEST
1450 && reloc->reloc_expression.X_op == O_constant)
1451 value = reloc->reloc_expression.X_add_number;
1452
1453 return (unsigned long) value;
1454 }
1455
1456
1457 /* Argument assemble functions. */
1458 static void
1459 nios2_assemble_arg_c (const char *token, nios2_insn_infoS *insn)
1460 {
1461 struct nios2_reg *reg = nios2_parse_reg (token, REG_CONTROL);
1462 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1463
1464 if (reg == NULL)
1465 return;
1466
1467 switch (op->format)
1468 {
1469 case iw_r_type:
1470 insn->insn_code |= SET_IW_R_IMM5 (reg->index);
1471 break;
1472 default:
1473 bad_opcode (op);
1474 }
1475 }
1476
1477 static void
1478 nios2_assemble_arg_d (const char *token, nios2_insn_infoS *insn)
1479 {
1480 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1481 unsigned long regtype = REG_NORMAL;
1482 struct nios2_reg *reg;
1483
1484 if (op->format == iw_custom_type)
1485 regtype |= REG_COPROCESSOR;
1486 reg = nios2_parse_reg (token, regtype);
1487 if (reg == NULL)
1488 return;
1489
1490 switch (op->format)
1491 {
1492 case iw_r_type:
1493 insn->insn_code |= SET_IW_R_C (reg->index);
1494 break;
1495 case iw_custom_type:
1496 insn->insn_code |= SET_IW_CUSTOM_C (reg->index);
1497 if (reg->regtype & REG_COPROCESSOR)
1498 insn->insn_code |= SET_IW_CUSTOM_READC (0);
1499 else
1500 insn->insn_code |= SET_IW_CUSTOM_READC (1);
1501 break;
1502 default:
1503 bad_opcode (op);
1504 }
1505 }
1506
1507 static void
1508 nios2_assemble_arg_s (const char *token, nios2_insn_infoS *insn)
1509 {
1510 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1511 unsigned long regtype = REG_NORMAL;
1512 struct nios2_reg *reg;
1513
1514 if (op->format == iw_custom_type)
1515 regtype |= REG_COPROCESSOR;
1516 reg = nios2_parse_reg (token, regtype);
1517 if (reg == NULL)
1518 return;
1519
1520 switch (op->format)
1521 {
1522 case iw_r_type:
1523 insn->insn_code |= SET_IW_R_A (reg->index);
1524 break;
1525 case iw_i_type:
1526 insn->insn_code |= SET_IW_I_A (reg->index);
1527 break;
1528 case iw_custom_type:
1529 insn->insn_code |= SET_IW_CUSTOM_A (reg->index);
1530 if (reg->regtype & REG_COPROCESSOR)
1531 insn->insn_code |= SET_IW_CUSTOM_READA (0);
1532 else
1533 insn->insn_code |= SET_IW_CUSTOM_READA (1);
1534 break;
1535 default:
1536 bad_opcode (op);
1537 }
1538 }
1539
1540 static void
1541 nios2_assemble_arg_t (const char *token, nios2_insn_infoS *insn)
1542 {
1543 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1544 unsigned long regtype = REG_NORMAL;
1545 struct nios2_reg *reg;
1546
1547 if (op->format == iw_custom_type)
1548 regtype |= REG_COPROCESSOR;
1549 reg = nios2_parse_reg (token, regtype);
1550 if (reg == NULL)
1551 return;
1552
1553 switch (op->format)
1554 {
1555 case iw_r_type:
1556 insn->insn_code |= SET_IW_R_B (reg->index);
1557 break;
1558 case iw_i_type:
1559 insn->insn_code |= SET_IW_I_B (reg->index);
1560 break;
1561 case iw_custom_type:
1562 insn->insn_code |= SET_IW_CUSTOM_B (reg->index);
1563 if (reg->regtype & REG_COPROCESSOR)
1564 insn->insn_code |= SET_IW_CUSTOM_READB (0);
1565 else
1566 insn->insn_code |= SET_IW_CUSTOM_READB (1);
1567 break;
1568 default:
1569 bad_opcode (op);
1570 }
1571 }
1572
1573 static void
1574 nios2_assemble_arg_i (const char *token, nios2_insn_infoS *insn)
1575 {
1576 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1577 unsigned int val;
1578
1579 switch (op->format)
1580 {
1581 case iw_i_type:
1582 val = nios2_assemble_expression (token, insn,
1583 BFD_RELOC_NIOS2_S16, 0);
1584 insn->constant_bits |= SET_IW_I_IMM16 (val);
1585 break;
1586 default:
1587 bad_opcode (op);
1588 }
1589 }
1590
1591 static void
1592 nios2_assemble_arg_u (const char *token, nios2_insn_infoS *insn)
1593 {
1594 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1595 unsigned int val;
1596
1597 switch (op->format)
1598 {
1599 case iw_i_type:
1600 val = nios2_assemble_expression (token, insn,
1601 BFD_RELOC_NIOS2_U16, 0);
1602 insn->constant_bits |= SET_IW_I_IMM16 (val);
1603 break;
1604 default:
1605 bad_opcode (op);
1606 }
1607 }
1608
1609 static void
1610 nios2_assemble_arg_o (const char *token, nios2_insn_infoS *insn)
1611 {
1612 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1613 unsigned int val;
1614
1615 switch (op->format)
1616 {
1617 case iw_i_type:
1618 val = nios2_assemble_expression (token, insn,
1619 BFD_RELOC_16_PCREL, 1);
1620 insn->constant_bits |= SET_IW_I_IMM16 (val);
1621 break;
1622 default:
1623 bad_opcode (op);
1624 }
1625 }
1626
1627 static void
1628 nios2_assemble_arg_j (const char *token, nios2_insn_infoS *insn)
1629 {
1630 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1631 unsigned int val;
1632
1633 switch (op->format)
1634 {
1635 case iw_r_type:
1636 val = nios2_assemble_expression (token, insn,
1637 BFD_RELOC_NIOS2_IMM5, 0);
1638 insn->constant_bits |= SET_IW_R_IMM5 (val);
1639 break;
1640 default:
1641 bad_opcode (op);
1642 }
1643 }
1644
1645 static void
1646 nios2_assemble_arg_l (const char *token, nios2_insn_infoS *insn)
1647 {
1648 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1649 unsigned int val;
1650
1651 switch (op->format)
1652 {
1653 case iw_custom_type:
1654 val = nios2_assemble_expression (token, insn,
1655 BFD_RELOC_NIOS2_IMM8, 0);
1656 insn->constant_bits |= SET_IW_CUSTOM_N (val);
1657 break;
1658 default:
1659 bad_opcode (op);
1660 }
1661 }
1662
1663 static void
1664 nios2_assemble_arg_m (const char *token, nios2_insn_infoS *insn)
1665 {
1666 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1667 unsigned int val;
1668
1669 switch (op->format)
1670 {
1671 case iw_j_type:
1672 val = nios2_assemble_expression (token, insn,
1673 (nios2_as_options.noat
1674 ? BFD_RELOC_NIOS2_CALL26_NOAT
1675 : BFD_RELOC_NIOS2_CALL26),
1676 0);
1677 insn->constant_bits |= SET_IW_J_IMM26 (val);
1678 break;
1679 default:
1680 bad_opcode (op);
1681 }
1682 }
1683
1684 static void
1685 nios2_assemble_args (nios2_insn_infoS *insn)
1686 {
1687 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1688 const char *argptr;
1689 unsigned int tokidx, ntok;
1690
1691 /* Make sure there are enough arguments. */
1692 ntok = (op->pinfo & NIOS2_INSN_OPTARG) ? op->num_args - 1 : op->num_args;
1693 for (tokidx = 1; tokidx <= ntok; tokidx++)
1694 if (insn->insn_tokens[tokidx] == NULL)
1695 {
1696 as_bad ("missing argument");
1697 return;
1698 }
1699
1700 for (argptr = op->args, tokidx = 1;
1701 *argptr && insn->insn_tokens[tokidx];
1702 argptr++)
1703 switch (*argptr)
1704 {
1705 case ',':
1706 case '(':
1707 case ')':
1708 break;
1709
1710 case 'c':
1711 nios2_assemble_arg_c (insn->insn_tokens[tokidx++], insn);
1712 break;
1713
1714 case 'd':
1715 nios2_assemble_arg_d (insn->insn_tokens[tokidx++], insn);
1716 break;
1717
1718 case 's':
1719 nios2_assemble_arg_s (insn->insn_tokens[tokidx++], insn);
1720 break;
1721
1722 case 't':
1723 nios2_assemble_arg_t (insn->insn_tokens[tokidx++], insn);
1724 break;
1725
1726 case 'i':
1727 nios2_assemble_arg_i (insn->insn_tokens[tokidx++], insn);
1728 break;
1729
1730 case 'u':
1731 nios2_assemble_arg_u (insn->insn_tokens[tokidx++], insn);
1732 break;
1733
1734 case 'o':
1735 nios2_assemble_arg_o (insn->insn_tokens[tokidx++], insn);
1736 break;
1737
1738 case 'j':
1739 nios2_assemble_arg_j (insn->insn_tokens[tokidx++], insn);
1740 break;
1741
1742 case 'l':
1743 nios2_assemble_arg_l (insn->insn_tokens[tokidx++], insn);
1744 break;
1745
1746 case 'm':
1747 nios2_assemble_arg_m (insn->insn_tokens[tokidx++], insn);
1748 break;
1749
1750 default:
1751 bad_opcode (op);
1752 break;
1753 }
1754
1755 /* Perform argument checking. */
1756 nios2_check_assembly (insn->insn_code | insn->constant_bits,
1757 insn->insn_tokens[tokidx]);
1758 }
1759
1760
1761 /* The function consume_arg takes a pointer into a string
1762 of instruction tokens (args) and a pointer into a string
1763 representing the expected sequence of tokens and separators.
1764 It checks whether the first argument in argstr is of the
1765 expected type, throwing an error if it is not, and returns
1766 the pointer argstr. */
1767 static char *
1768 nios2_consume_arg (char *argstr, const char *parsestr)
1769 {
1770 char *temp;
1771
1772 switch (*parsestr)
1773 {
1774 case 'c':
1775 case 'd':
1776 case 's':
1777 case 't':
1778 break;
1779
1780 case 'i':
1781 case 'u':
1782 if (*argstr == '%')
1783 {
1784 if (nios2_special_relocation_p (argstr))
1785 {
1786 /* We zap the parentheses because we don't want them confused
1787 with separators. */
1788 temp = strchr (argstr, '(');
1789 if (temp != NULL)
1790 *temp = ' ';
1791 temp = strchr (argstr, ')');
1792 if (temp != NULL)
1793 *temp = ' ';
1794 }
1795 else
1796 as_bad (_("badly formed expression near %s"), argstr);
1797 }
1798 break;
1799 case 'm':
1800 case 'j':
1801 case 'l':
1802 /* We can't have %hi, %lo or %hiadj here. */
1803 if (*argstr == '%')
1804 as_bad (_("badly formed expression near %s"), argstr);
1805 break;
1806 case 'o':
1807 case 'E':
1808 break;
1809 default:
1810 BAD_CASE (*parsestr);
1811 break;
1812 }
1813
1814 return argstr;
1815 }
1816
1817 /* The function consume_separator takes a pointer into a string
1818 of instruction tokens (args) and a pointer into a string representing
1819 the expected sequence of tokens and separators. It finds the first
1820 instance of the character pointed to by separator in argstr, and
1821 returns a pointer to the next element of argstr, which is the
1822 following token in the sequence. */
1823 static char *
1824 nios2_consume_separator (char *argstr, const char *separator)
1825 {
1826 char *p;
1827
1828 /* If we have a opcode reg, expr(reg) type instruction, and
1829 * we are separating the expr from the (reg), we find the last
1830 * (, just in case the expression has parentheses. */
1831
1832 if (*separator == '(')
1833 p = strrchr (argstr, *separator);
1834 else
1835 p = strchr (argstr, *separator);
1836
1837 if (p != NULL)
1838 *p++ = 0;
1839 return p;
1840 }
1841
1842
1843 /* The principal argument parsing function which takes a string argstr
1844 representing the instruction arguments for insn, and extracts the argument
1845 tokens matching parsestr into parsed_args. */
1846 static void
1847 nios2_parse_args (nios2_insn_infoS *insn, char *argstr,
1848 const char *parsestr, char **parsed_args)
1849 {
1850 char *p;
1851 char *end = NULL;
1852 int i;
1853 p = argstr;
1854 i = 0;
1855 bfd_boolean terminate = FALSE;
1856
1857 /* This rest of this function is it too fragile and it mostly works,
1858 therefore special case this one. */
1859 if (*parsestr == 0 && argstr != 0)
1860 {
1861 as_bad (_("too many arguments"));
1862 parsed_args[0] = NULL;
1863 return;
1864 }
1865
1866 while (p != NULL && !terminate && i < NIOS2_MAX_INSN_TOKENS)
1867 {
1868 parsed_args[i] = nios2_consume_arg (p, parsestr);
1869 ++parsestr;
1870 while (*parsestr == '(' || *parsestr == ')' || *parsestr == ',')
1871 {
1872 char *context = p;
1873 p = nios2_consume_separator (p, parsestr);
1874 /* Check for missing separators. */
1875 if (!p && !(insn->insn_nios2_opcode->pinfo & NIOS2_INSN_OPTARG))
1876 {
1877 as_bad (_("expecting %c near %s"), *parsestr, context);
1878 break;
1879 }
1880 ++parsestr;
1881 }
1882
1883 if (*parsestr == '\0')
1884 {
1885 /* Check that the argument string has no trailing arguments. */
1886 end = strpbrk (p, ",");
1887 if (end != NULL)
1888 as_bad (_("too many arguments"));
1889 }
1890
1891 if (*parsestr == '\0' || (p != NULL && *p == '\0'))
1892 terminate = TRUE;
1893 ++i;
1894 }
1895
1896 parsed_args[i] = NULL;
1897 }
1898
1899
1900 \f
1901 /** Support for pseudo-op parsing. These are macro-like opcodes that
1902 expand into real insns by suitable fiddling with the operands. */
1903
1904 /* Append the string modifier to the string contained in the argument at
1905 parsed_args[ndx]. */
1906 static void
1907 nios2_modify_arg (char **parsed_args, const char *modifier,
1908 int unused ATTRIBUTE_UNUSED, int ndx)
1909 {
1910 char *tmp = parsed_args[ndx];
1911
1912 parsed_args[ndx]
1913 = (char *) malloc (strlen (parsed_args[ndx]) + strlen (modifier) + 1);
1914 strcpy (parsed_args[ndx], tmp);
1915 strcat (parsed_args[ndx], modifier);
1916 }
1917
1918 /* Modify parsed_args[ndx] by negating that argument. */
1919 static void
1920 nios2_negate_arg (char **parsed_args, const char *modifier ATTRIBUTE_UNUSED,
1921 int unused ATTRIBUTE_UNUSED, int ndx)
1922 {
1923 char *tmp = parsed_args[ndx];
1924
1925 parsed_args[ndx]
1926 = (char *) malloc (strlen ("~(") + strlen (parsed_args[ndx]) +
1927 strlen (")+1") + 1);
1928
1929 strcpy (parsed_args[ndx], "~(");
1930 strcat (parsed_args[ndx], tmp);
1931 strcat (parsed_args[ndx], ")+1");
1932 }
1933
1934 /* The function nios2_swap_args swaps the pointers at indices index_1 and
1935 index_2 in the array parsed_args[] - this is used for operand swapping
1936 for comparison operations. */
1937 static void
1938 nios2_swap_args (char **parsed_args, const char *unused ATTRIBUTE_UNUSED,
1939 int index_1, int index_2)
1940 {
1941 char *tmp;
1942 gas_assert (index_1 < NIOS2_MAX_INSN_TOKENS
1943 && index_2 < NIOS2_MAX_INSN_TOKENS);
1944 tmp = parsed_args[index_1];
1945 parsed_args[index_1] = parsed_args[index_2];
1946 parsed_args[index_2] = tmp;
1947 }
1948
1949 /* This function appends the string appnd to the array of strings in
1950 parsed_args num times starting at index start in the array. */
1951 static void
1952 nios2_append_arg (char **parsed_args, const char *appnd, int num,
1953 int start)
1954 {
1955 int i, count;
1956 char *tmp;
1957
1958 gas_assert ((start + num) < NIOS2_MAX_INSN_TOKENS);
1959
1960 if (nios2_mode == NIOS2_MODE_TEST)
1961 tmp = parsed_args[start];
1962 else
1963 tmp = NULL;
1964
1965 for (i = start, count = num; count > 0; ++i, --count)
1966 parsed_args[i] = (char *) appnd;
1967
1968 gas_assert (i == (start + num));
1969 parsed_args[i] = tmp;
1970 parsed_args[i + 1] = NULL;
1971 }
1972
1973 /* This function inserts the string insert num times in the array
1974 parsed_args, starting at the index start. */
1975 static void
1976 nios2_insert_arg (char **parsed_args, const char *insert, int num,
1977 int start)
1978 {
1979 int i, count;
1980
1981 gas_assert ((start + num) < NIOS2_MAX_INSN_TOKENS);
1982
1983 /* Move the existing arguments up to create space. */
1984 for (i = NIOS2_MAX_INSN_TOKENS; i - num >= start; --i)
1985 parsed_args[i] = parsed_args[i - num];
1986
1987 for (i = start, count = num; count > 0; ++i, --count)
1988 parsed_args[i] = (char *) insert;
1989 }
1990
1991 /* Cleanup function to free malloc'ed arg strings. */
1992 static void
1993 nios2_free_arg (char **parsed_args, int num ATTRIBUTE_UNUSED, int start)
1994 {
1995 if (parsed_args[start])
1996 {
1997 free (parsed_args[start]);
1998 parsed_args[start] = NULL;
1999 }
2000 }
2001
2002 /* This function swaps the pseudo-op for a real op. */
2003 static nios2_ps_insn_infoS*
2004 nios2_translate_pseudo_insn (nios2_insn_infoS *insn)
2005 {
2006
2007 nios2_ps_insn_infoS *ps_insn;
2008
2009 /* Find which real insn the pseudo-op transates to and
2010 switch the insn_info ptr to point to it. */
2011 ps_insn = nios2_ps_lookup (insn->insn_nios2_opcode->name);
2012
2013 if (ps_insn != NULL)
2014 {
2015 insn->insn_nios2_opcode = nios2_opcode_lookup (ps_insn->insn);
2016 insn->insn_tokens[0] = insn->insn_nios2_opcode->name;
2017 /* Modify the args so they work with the real insn. */
2018 ps_insn->arg_modifer_func ((char **) insn->insn_tokens,
2019 ps_insn->arg_modifier, ps_insn->num,
2020 ps_insn->index);
2021 }
2022 else
2023 /* we cannot recover from this. */
2024 as_fatal (_("unrecognized pseudo-instruction %s"),
2025 ps_insn->pseudo_insn);
2026 return ps_insn;
2027 }
2028
2029 /* Invoke the cleanup handler for pseudo-insn ps_insn on insn. */
2030 static void
2031 nios2_cleanup_pseudo_insn (nios2_insn_infoS *insn,
2032 nios2_ps_insn_infoS *ps_insn)
2033 {
2034 if (ps_insn->arg_cleanup_func)
2035 (ps_insn->arg_cleanup_func) ((char **) insn->insn_tokens,
2036 ps_insn->num, ps_insn->index);
2037 }
2038
2039 const nios2_ps_insn_infoS nios2_ps_insn_info_structs[] = {
2040 /* pseudo-op, real-op, arg, arg_modifier_func, num, index, arg_cleanup_func */
2041 {"mov", "add", nios2_append_arg, "zero", 1, 3, NULL},
2042 {"movi", "addi", nios2_insert_arg, "zero", 1, 2, NULL},
2043 {"movhi", "orhi", nios2_insert_arg, "zero", 1, 2, NULL},
2044 {"movui", "ori", nios2_insert_arg, "zero", 1, 2, NULL},
2045 {"movia", "orhi", nios2_insert_arg, "zero", 1, 2, NULL},
2046 {"nop", "add", nios2_append_arg, "zero", 3, 1, NULL},
2047 {"bgt", "blt", nios2_swap_args, "", 1, 2, NULL},
2048 {"bgtu", "bltu", nios2_swap_args, "", 1, 2, NULL},
2049 {"ble", "bge", nios2_swap_args, "", 1, 2, NULL},
2050 {"bleu", "bgeu", nios2_swap_args, "", 1, 2, NULL},
2051 {"cmpgt", "cmplt", nios2_swap_args, "", 2, 3, NULL},
2052 {"cmpgtu", "cmpltu", nios2_swap_args, "", 2, 3, NULL},
2053 {"cmple", "cmpge", nios2_swap_args, "", 2, 3, NULL},
2054 {"cmpleu", "cmpgeu", nios2_swap_args, "", 2, 3, NULL},
2055 {"cmpgti", "cmpgei", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
2056 {"cmpgtui", "cmpgeui", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
2057 {"cmplei", "cmplti", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
2058 {"cmpleui", "cmpltui", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
2059 {"subi", "addi", nios2_negate_arg, "", 0, 3, nios2_free_arg}
2060 /* Add further pseudo-ops here. */
2061 };
2062
2063 #define NIOS2_NUM_PSEUDO_INSNS \
2064 ((sizeof(nios2_ps_insn_info_structs)/ \
2065 sizeof(nios2_ps_insn_info_structs[0])))
2066 const int nios2_num_ps_insn_info_structs = NIOS2_NUM_PSEUDO_INSNS;
2067
2068 \f
2069 /** Assembler output support. */
2070
2071 /* Output a normal instruction. */
2072 static void
2073 output_insn (nios2_insn_infoS *insn)
2074 {
2075 char *f;
2076 nios2_insn_relocS *reloc;
2077 f = frag_more (insn->insn_nios2_opcode->size);
2078 /* This allocates enough space for the instruction
2079 and puts it in the current frag. */
2080 md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size);
2081 /* Emit debug info. */
2082 dwarf2_emit_insn (insn->insn_nios2_opcode->size);
2083 /* Create any fixups to be acted on later. */
2084
2085 for (reloc = insn->insn_reloc; reloc != NULL; reloc = reloc->reloc_next)
2086 fix_new_exp (frag_now, f - frag_now->fr_literal,
2087 insn->insn_nios2_opcode->size,
2088 &reloc->reloc_expression, reloc->reloc_pcrel,
2089 reloc->reloc_type);
2090 }
2091
2092 /* Output an unconditional branch. */
2093 static void
2094 output_ubranch (nios2_insn_infoS *insn)
2095 {
2096 nios2_insn_relocS *reloc = insn->insn_reloc;
2097
2098 /* If the reloc is NULL, there was an error assembling the branch. */
2099 if (reloc != NULL)
2100 {
2101 symbolS *symp = reloc->reloc_expression.X_add_symbol;
2102 offsetT offset = reloc->reloc_expression.X_add_number;
2103 char *f;
2104
2105 /* Tag dwarf2 debug info to the address at the start of the insn.
2106 We must do it before frag_var() below closes off the frag. */
2107 dwarf2_emit_insn (0);
2108
2109 /* We create a machine dependent frag which can grow
2110 to accommodate the largest possible instruction sequence
2111 this may generate. */
2112 f = frag_var (rs_machine_dependent,
2113 UBRANCH_MAX_SIZE, insn->insn_nios2_opcode->size,
2114 UBRANCH_SUBTYPE (0), symp, offset, NULL);
2115
2116 md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size);
2117
2118 /* We leave fixup generation to md_convert_frag. */
2119 }
2120 }
2121
2122 /* Output a conditional branch. */
2123 static void
2124 output_cbranch (nios2_insn_infoS *insn)
2125 {
2126 nios2_insn_relocS *reloc = insn->insn_reloc;
2127
2128 /* If the reloc is NULL, there was an error assembling the branch. */
2129 if (reloc != NULL)
2130 {
2131 symbolS *symp = reloc->reloc_expression.X_add_symbol;
2132 offsetT offset = reloc->reloc_expression.X_add_number;
2133 char *f;
2134
2135 /* Tag dwarf2 debug info to the address at the start of the insn.
2136 We must do it before frag_var() below closes off the frag. */
2137 dwarf2_emit_insn (0);
2138
2139 /* We create a machine dependent frag which can grow
2140 to accommodate the largest possible instruction sequence
2141 this may generate. */
2142 f = frag_var (rs_machine_dependent,
2143 CBRANCH_MAX_SIZE, insn->insn_nios2_opcode->size,
2144 CBRANCH_SUBTYPE (0), symp, offset, NULL);
2145
2146 md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size);
2147
2148 /* We leave fixup generation to md_convert_frag. */
2149 }
2150 }
2151
2152 /* Output a call sequence. Since calls are not pc-relative for NIOS2,
2153 but are page-relative, we cannot tell at any stage in assembly
2154 whether a call will be out of range since a section may be linked
2155 at any address. So if we are relaxing, we convert all call instructions
2156 to long call sequences, and rely on the linker to relax them back to
2157 short calls. */
2158 static void
2159 output_call (nios2_insn_infoS *insn)
2160 {
2161 /* This allocates enough space for the instruction
2162 and puts it in the current frag. */
2163 char *f = frag_more (12);
2164 nios2_insn_relocS *reloc = insn->insn_reloc;
2165
2166 md_number_to_chars (f,
2167 (MATCH_R1_ORHI | SET_IW_I_B (AT_REGNUM)
2168 | SET_IW_I_A (0)),
2169 4);
2170 dwarf2_emit_insn (4);
2171 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2172 &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_HI16);
2173 md_number_to_chars (f + 4,
2174 (MATCH_R1_ORI | SET_IW_I_B (AT_REGNUM)
2175 | SET_IW_I_A (AT_REGNUM)),
2176 4);
2177 dwarf2_emit_insn (4);
2178 fix_new_exp (frag_now, f - frag_now->fr_literal + 4, 4,
2179 &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_LO16);
2180 md_number_to_chars (f + 8, MATCH_R1_CALLR | SET_IW_R_A (AT_REGNUM), 4);
2181 dwarf2_emit_insn (4);
2182 }
2183
2184 /* Output a movhi/addi pair for the movia pseudo-op. */
2185 static void
2186 output_movia (nios2_insn_infoS *insn)
2187 {
2188 /* This allocates enough space for the instruction
2189 and puts it in the current frag. */
2190 char *f = frag_more (8);
2191 nios2_insn_relocS *reloc = insn->insn_reloc;
2192 unsigned long reg_index = GET_IW_I_B (insn->insn_code);
2193
2194 /* If the reloc is NULL, there was an error assembling the movia. */
2195 if (reloc != NULL)
2196 {
2197 md_number_to_chars (f, insn->insn_code, 4);
2198 dwarf2_emit_insn (4);
2199 fix_new (frag_now, f - frag_now->fr_literal, 4,
2200 reloc->reloc_expression.X_add_symbol,
2201 reloc->reloc_expression.X_add_number, 0,
2202 BFD_RELOC_NIOS2_HIADJ16);
2203 md_number_to_chars (f + 4,
2204 (MATCH_R1_ADDI | SET_IW_I_A (reg_index)
2205 | SET_IW_I_B (reg_index)),
2206 4);
2207 dwarf2_emit_insn (4);
2208 fix_new (frag_now, f + 4 - frag_now->fr_literal, 4,
2209 reloc->reloc_expression.X_add_symbol,
2210 reloc->reloc_expression.X_add_number, 0, BFD_RELOC_NIOS2_LO16);
2211 }
2212 }
2213
2214
2215 \f
2216 /** External interfaces. */
2217
2218 /* Update the selected architecture based on ARCH, giving an error if
2219 ARCH is an invalid value. */
2220
2221 static void
2222 nios2_use_arch (const char *arch)
2223 {
2224 if (strcmp (arch, "nios2") == 0 || strcmp (arch, "r1") == 0)
2225 {
2226 nios2_architecture |= EF_NIOS2_ARCH_R1;
2227 nios2_opcodes = (struct nios2_opcode *) nios2_r1_opcodes;
2228 nios2_num_opcodes = nios2_num_r1_opcodes;
2229 nop32 = nop_r1;
2230 nop16 = NULL;
2231 return;
2232 }
2233 else if (strcmp (arch, "r2") == 0)
2234 {
2235 nios2_architecture |= EF_NIOS2_ARCH_R2;
2236 nios2_opcodes = (struct nios2_opcode *) nios2_r2_opcodes;
2237 nios2_num_opcodes = nios2_num_r2_opcodes;
2238 nop32 = nop_r2;
2239 nop16 = nop_r2_cdx;
2240 return;
2241 }
2242
2243 as_bad (_("unknown architecture '%s'"), arch);
2244 }
2245
2246 /* The following functions are called by machine-independent parts of
2247 the assembler. */
2248 int
2249 md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
2250 {
2251 switch (c)
2252 {
2253 case 'r':
2254 /* Hidden option for self-test mode. */
2255 nios2_mode = NIOS2_MODE_TEST;
2256 break;
2257 case OPTION_RELAX_ALL:
2258 nios2_as_options.relax = relax_all;
2259 break;
2260 case OPTION_NORELAX:
2261 nios2_as_options.relax = relax_none;
2262 break;
2263 case OPTION_RELAX_SECTION:
2264 nios2_as_options.relax = relax_section;
2265 break;
2266 case OPTION_EB:
2267 target_big_endian = 1;
2268 break;
2269 case OPTION_EL:
2270 target_big_endian = 0;
2271 break;
2272 case OPTION_MARCH:
2273 nios2_use_arch (arg);
2274 break;
2275 default:
2276 return 0;
2277 break;
2278 }
2279
2280 return 1;
2281 }
2282
2283 /* Implement TARGET_FORMAT. We can choose to be big-endian or
2284 little-endian at runtime based on a switch. */
2285 const char *
2286 nios2_target_format (void)
2287 {
2288 return target_big_endian ? "elf32-bignios2" : "elf32-littlenios2";
2289 }
2290
2291 /* Machine-dependent usage message. */
2292 void
2293 md_show_usage (FILE *stream)
2294 {
2295 fprintf (stream, " NIOS2 options:\n"
2296 " -relax-all replace all branch and call "
2297 "instructions with jmp and callr sequences\n"
2298 " -relax-section replace identified out of range "
2299 "branches with jmp sequences (default)\n"
2300 " -no-relax do not replace any branches or calls\n"
2301 " -EB force big-endian byte ordering\n"
2302 " -EL force little-endian byte ordering\n"
2303 " -march=ARCH enable instructions from architecture ARCH\n");
2304 }
2305
2306
2307 /* This function is called once, at assembler startup time.
2308 It should set up all the tables, etc. that the MD part of the
2309 assembler will need. */
2310 void
2311 md_begin (void)
2312 {
2313 int i;
2314 const char *inserted;
2315
2316 switch (nios2_architecture)
2317 {
2318 default:
2319 case EF_NIOS2_ARCH_R1:
2320 bfd_default_set_arch_mach (stdoutput, bfd_arch_nios2, bfd_mach_nios2r1);
2321 break;
2322 case EF_NIOS2_ARCH_R2:
2323 if (target_big_endian)
2324 as_fatal (_("Big-endian R2 is not supported."));
2325 bfd_default_set_arch_mach (stdoutput, bfd_arch_nios2, bfd_mach_nios2r2);
2326 break;
2327 }
2328
2329 /* Create and fill a hashtable for the Nios II opcodes, registers and
2330 arguments. */
2331 nios2_opcode_hash = hash_new ();
2332 nios2_reg_hash = hash_new ();
2333 nios2_ps_hash = hash_new ();
2334
2335 for (i = 0; i < nios2_num_opcodes; ++i)
2336 {
2337 inserted
2338 = hash_insert (nios2_opcode_hash, nios2_opcodes[i].name,
2339 (PTR) & nios2_opcodes[i]);
2340 if (inserted != NULL)
2341 {
2342 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2343 nios2_opcodes[i].name, inserted);
2344 /* Probably a memory allocation problem? Give up now. */
2345 as_fatal (_("Broken assembler. No assembly attempted."));
2346 }
2347 }
2348
2349 for (i = 0; i < nios2_num_regs; ++i)
2350 {
2351 inserted
2352 = hash_insert (nios2_reg_hash, nios2_regs[i].name,
2353 (PTR) & nios2_regs[i]);
2354 if (inserted != NULL)
2355 {
2356 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2357 nios2_regs[i].name, inserted);
2358 /* Probably a memory allocation problem? Give up now. */
2359 as_fatal (_("Broken assembler. No assembly attempted."));
2360 }
2361
2362 }
2363
2364 for (i = 0; i < nios2_num_ps_insn_info_structs; ++i)
2365 {
2366 inserted
2367 = hash_insert (nios2_ps_hash, nios2_ps_insn_info_structs[i].pseudo_insn,
2368 (PTR) & nios2_ps_insn_info_structs[i]);
2369 if (inserted != NULL)
2370 {
2371 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2372 nios2_ps_insn_info_structs[i].pseudo_insn, inserted);
2373 /* Probably a memory allocation problem? Give up now. */
2374 as_fatal (_("Broken assembler. No assembly attempted."));
2375 }
2376 }
2377
2378 /* Assembler option defaults. */
2379 nios2_as_options.noat = FALSE;
2380 nios2_as_options.nobreak = FALSE;
2381
2382 /* Debug information is incompatible with relaxation. */
2383 if (debug_type != DEBUG_UNSPECIFIED)
2384 nios2_as_options.relax = relax_none;
2385
2386 /* Initialize the alignment data. */
2387 nios2_current_align_seg = now_seg;
2388 nios2_last_label = NULL;
2389 nios2_current_align = 0;
2390 }
2391
2392
2393 /* Assembles a single line of Nios II assembly language. */
2394 void
2395 md_assemble (char *op_str)
2396 {
2397 char *argstr;
2398 char *op_strdup = NULL;
2399 unsigned long saved_pinfo = 0;
2400 nios2_insn_infoS thisinsn;
2401 nios2_insn_infoS *insn = &thisinsn;
2402
2403 /* Make sure we are aligned on a 4-byte boundary. */
2404 if (nios2_current_align < 2)
2405 nios2_align (2, NULL, nios2_last_label);
2406 else if (nios2_current_align > 2)
2407 nios2_current_align = 2;
2408 nios2_last_label = NULL;
2409
2410 /* We don't want to clobber to op_str
2411 because we want to be able to use it in messages. */
2412 op_strdup = strdup (op_str);
2413 insn->insn_tokens[0] = strtok (op_strdup, " ");
2414 argstr = strtok (NULL, "");
2415
2416 /* Assemble the opcode. */
2417 insn->insn_nios2_opcode = nios2_opcode_lookup (insn->insn_tokens[0]);
2418 insn->insn_reloc = NULL;
2419
2420 if (insn->insn_nios2_opcode != NULL)
2421 {
2422 nios2_ps_insn_infoS *ps_insn = NULL;
2423 /* Set the opcode for the instruction. */
2424 insn->insn_code = insn->insn_nios2_opcode->match;
2425 insn->constant_bits = 0;
2426
2427 /* Parse the arguments pointed to by argstr. */
2428 if (nios2_mode == NIOS2_MODE_ASSEMBLE)
2429 nios2_parse_args (insn, argstr, insn->insn_nios2_opcode->args,
2430 (char **) &insn->insn_tokens[1]);
2431 else
2432 nios2_parse_args (insn, argstr, insn->insn_nios2_opcode->args_test,
2433 (char **) &insn->insn_tokens[1]);
2434
2435 /* We need to preserve the MOVIA macro as this is clobbered by
2436 translate_pseudo_insn. */
2437 if (insn->insn_nios2_opcode->pinfo == NIOS2_INSN_MACRO_MOVIA)
2438 saved_pinfo = NIOS2_INSN_MACRO_MOVIA;
2439 /* If the instruction is an pseudo-instruction, we want to replace it
2440 with its real equivalent, and then continue. */
2441 if ((insn->insn_nios2_opcode->pinfo & NIOS2_INSN_MACRO)
2442 == NIOS2_INSN_MACRO)
2443 ps_insn = nios2_translate_pseudo_insn (insn);
2444
2445 /* Assemble the parsed arguments into the instruction word. */
2446 nios2_assemble_args (insn);
2447
2448 /* Handle relaxation and other transformations. */
2449 if (nios2_as_options.relax != relax_none
2450 && !nios2_as_options.noat
2451 && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_UBRANCH)
2452 output_ubranch (insn);
2453 else if (nios2_as_options.relax != relax_none
2454 && !nios2_as_options.noat
2455 && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_CBRANCH)
2456 output_cbranch (insn);
2457 else if (nios2_as_options.relax == relax_all
2458 && !nios2_as_options.noat
2459 && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_CALL
2460 && insn->insn_reloc
2461 && ((insn->insn_reloc->reloc_type
2462 == BFD_RELOC_NIOS2_CALL26)
2463 || (insn->insn_reloc->reloc_type
2464 == BFD_RELOC_NIOS2_CALL26_NOAT)))
2465 output_call (insn);
2466 else if (saved_pinfo == NIOS2_INSN_MACRO_MOVIA)
2467 output_movia (insn);
2468 else
2469 output_insn (insn);
2470 if (ps_insn)
2471 nios2_cleanup_pseudo_insn (insn, ps_insn);
2472 }
2473 else
2474 /* Unrecognised instruction - error. */
2475 as_bad (_("unrecognised instruction %s"), insn->insn_tokens[0]);
2476
2477 /* Don't leak memory. */
2478 free (op_strdup);
2479 }
2480
2481 /* Round up section size. */
2482 valueT
2483 md_section_align (asection *seg ATTRIBUTE_UNUSED, valueT size)
2484 {
2485 /* I think byte alignment is fine here. */
2486 return size;
2487 }
2488
2489 /* Implement TC_FORCE_RELOCATION. */
2490 int
2491 nios2_force_relocation (fixS *fixp)
2492 {
2493 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2494 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2495 || fixp->fx_r_type == BFD_RELOC_NIOS2_ALIGN)
2496 return 1;
2497
2498 return generic_force_reloc (fixp);
2499 }
2500
2501 /* Implement tc_fix_adjustable. */
2502 int
2503 nios2_fix_adjustable (fixS *fixp)
2504 {
2505 if (fixp->fx_addsy == NULL)
2506 return 1;
2507
2508 #ifdef OBJ_ELF
2509 /* Prevent all adjustments to global symbols. */
2510 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
2511 && (S_IS_EXTERNAL (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
2512 return 0;
2513 #endif
2514 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2515 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2516 return 0;
2517
2518 /* Preserve relocations against symbols with function type. */
2519 if (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION)
2520 return 0;
2521
2522 /* Don't allow symbols to be discarded on GOT related relocs. */
2523 if (fixp->fx_r_type == BFD_RELOC_NIOS2_GOT16
2524 || fixp->fx_r_type == BFD_RELOC_NIOS2_CALL16
2525 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_LO
2526 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_HA
2527 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_GD16
2528 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LDM16
2529 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LDO16
2530 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_IE16
2531 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LE16
2532 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPMOD
2533 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPREL
2534 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_TPREL
2535 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF
2536 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOT_LO
2537 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOT_HA
2538 || fixp->fx_r_type == BFD_RELOC_NIOS2_CALL_LO
2539 || fixp->fx_r_type == BFD_RELOC_NIOS2_CALL_HA
2540 )
2541 return 0;
2542
2543 return 1;
2544 }
2545
2546 /* Implement tc_frob_symbol. This is called in adjust_reloc_syms;
2547 it is used to remove *ABS* references from the symbol table. */
2548 int
2549 nios2_frob_symbol (symbolS *symp)
2550 {
2551 if ((OUTPUT_FLAVOR == bfd_target_elf_flavour
2552 && symp == section_symbol (absolute_section))
2553 || !S_IS_DEFINED (symp))
2554 return 1;
2555 else
2556 return 0;
2557 }
2558
2559 /* The function tc_gen_reloc creates a relocation structure for the
2560 fixup fixp, and returns a pointer to it. This structure is passed
2561 to bfd_install_relocation so that it can be written to the object
2562 file for linking. */
2563 arelent *
2564 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
2565 {
2566 arelent *reloc = (arelent *) xmalloc (sizeof (arelent));
2567 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2568 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2569
2570 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2571 reloc->addend = fixp->fx_offset; /* fixp->fx_addnumber; */
2572
2573 if (fixp->fx_pcrel)
2574 {
2575 switch (fixp->fx_r_type)
2576 {
2577 case BFD_RELOC_16:
2578 fixp->fx_r_type = BFD_RELOC_16_PCREL;
2579 break;
2580 case BFD_RELOC_NIOS2_LO16:
2581 fixp->fx_r_type = BFD_RELOC_NIOS2_PCREL_LO;
2582 break;
2583 case BFD_RELOC_NIOS2_HIADJ16:
2584 fixp->fx_r_type = BFD_RELOC_NIOS2_PCREL_HA;
2585 break;
2586 default:
2587 break;
2588 }
2589 }
2590
2591 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2592 if (reloc->howto == NULL)
2593 {
2594 as_bad_where (fixp->fx_file, fixp->fx_line,
2595 _("can't represent relocation type %s"),
2596 bfd_get_reloc_code_name (fixp->fx_r_type));
2597
2598 /* Set howto to a garbage value so that we can keep going. */
2599 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
2600 gas_assert (reloc->howto != NULL);
2601 }
2602 return reloc;
2603 }
2604
2605 long
2606 md_pcrel_from (fixS *fixP ATTRIBUTE_UNUSED)
2607 {
2608 return 0;
2609 }
2610
2611 /* Called just before the assembler exits. */
2612 void
2613 md_end ()
2614 {
2615 /* FIXME - not yet implemented */
2616 }
2617
2618 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
2619 Otherwise we have no need to default values of symbols. */
2620 symbolS *
2621 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
2622 {
2623 #ifdef OBJ_ELF
2624 if (name[0] == '_' && name[1] == 'G'
2625 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
2626 {
2627 if (!GOT_symbol)
2628 {
2629 if (symbol_find (name))
2630 as_bad ("GOT already in the symbol table");
2631
2632 GOT_symbol = symbol_new (name, undefined_section,
2633 (valueT) 0, &zero_address_frag);
2634 }
2635
2636 return GOT_symbol;
2637 }
2638 #endif
2639
2640 return 0;
2641 }
2642
2643 /* Implement tc_frob_label. */
2644 void
2645 nios2_frob_label (symbolS *lab)
2646 {
2647 /* Emit dwarf information. */
2648 dwarf2_emit_label (lab);
2649
2650 /* Update the label's address with the current output pointer. */
2651 symbol_set_frag (lab, frag_now);
2652 S_SET_VALUE (lab, (valueT) frag_now_fix ());
2653
2654 /* Record this label for future adjustment after we find out what
2655 kind of data it references, and the required alignment therewith. */
2656 nios2_last_label = lab;
2657 }
2658
2659 /* Implement md_cons_align. */
2660 void
2661 nios2_cons_align (int size)
2662 {
2663 int log_size = 0;
2664 const char *pfill = NULL;
2665
2666 while ((size >>= 1) != 0)
2667 ++log_size;
2668
2669 if (subseg_text_p (now_seg))
2670 pfill = (const char *) &nop;
2671 else
2672 pfill = NULL;
2673
2674 if (nios2_auto_align_on)
2675 nios2_align (log_size, pfill, NULL);
2676
2677 nios2_last_label = NULL;
2678 }
2679
2680 /* Map 's' to SHF_NIOS2_GPREL. */
2681 /* This is from the Alpha code tc-alpha.c. */
2682 int
2683 nios2_elf_section_letter (int letter, char **ptr_msg)
2684 {
2685 if (letter == 's')
2686 return SHF_NIOS2_GPREL;
2687
2688 *ptr_msg = _("Bad .section directive: want a,s,w,x,M,S,G,T in string");
2689 return -1;
2690 }
2691
2692 /* Map SHF_ALPHA_GPREL to SEC_SMALL_DATA. */
2693 /* This is from the Alpha code tc-alpha.c. */
2694 flagword
2695 nios2_elf_section_flags (flagword flags, int attr, int type ATTRIBUTE_UNUSED)
2696 {
2697 if (attr & SHF_NIOS2_GPREL)
2698 flags |= SEC_SMALL_DATA;
2699 return flags;
2700 }
2701
2702 /* Implement TC_PARSE_CONS_EXPRESSION to handle %tls_ldo(...) */
2703 bfd_reloc_code_real_type
2704 nios2_cons (expressionS *exp, int size)
2705 {
2706 bfd_reloc_code_real_type nios2_tls_ldo_reloc = BFD_RELOC_NONE;
2707
2708 SKIP_WHITESPACE ();
2709 if (input_line_pointer[0] == '%')
2710 {
2711 if (strprefix (input_line_pointer + 1, "tls_ldo"))
2712 {
2713 if (size != 4)
2714 as_bad (_("Illegal operands: %%tls_ldo in %d-byte data field"),
2715 size);
2716 else
2717 {
2718 input_line_pointer += 8;
2719 nios2_tls_ldo_reloc = BFD_RELOC_NIOS2_TLS_DTPREL;
2720 }
2721 }
2722 if (nios2_tls_ldo_reloc != BFD_RELOC_NONE)
2723 {
2724 SKIP_WHITESPACE ();
2725 if (input_line_pointer[0] != '(')
2726 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
2727 else
2728 {
2729 int c;
2730 char *end = ++input_line_pointer;
2731 int npar = 0;
2732
2733 for (c = *end; !is_end_of_line[c]; end++, c = *end)
2734 if (c == '(')
2735 npar++;
2736 else if (c == ')')
2737 {
2738 if (!npar)
2739 break;
2740 npar--;
2741 }
2742
2743 if (c != ')')
2744 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
2745 else
2746 {
2747 *end = '\0';
2748 expression (exp);
2749 *end = c;
2750 if (input_line_pointer != end)
2751 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
2752 else
2753 {
2754 input_line_pointer++;
2755 SKIP_WHITESPACE ();
2756 c = *input_line_pointer;
2757 if (! is_end_of_line[c] && c != ',')
2758 as_bad (_("Illegal operands: garbage after %%tls_ldo()"));
2759 }
2760 }
2761 }
2762 }
2763 }
2764 if (nios2_tls_ldo_reloc == BFD_RELOC_NONE)
2765 expression (exp);
2766 return nios2_tls_ldo_reloc;
2767 }
2768
2769 /* Implement HANDLE_ALIGN. */
2770 void
2771 nios2_handle_align (fragS *fragp)
2772 {
2773 /* If we are expecting to relax in the linker, then we must output a
2774 relocation to tell the linker we are aligning code. */
2775 if (nios2_as_options.relax == relax_all
2776 && (fragp->fr_type == rs_align || fragp->fr_type == rs_align_code)
2777 && fragp->fr_address + fragp->fr_fix > 0
2778 && fragp->fr_offset > 1
2779 && now_seg != bss_section)
2780 fix_new (fragp, fragp->fr_fix, 0, &abs_symbol, fragp->fr_offset, 0,
2781 BFD_RELOC_NIOS2_ALIGN);
2782 }
2783
2784 /* Implement tc_regname_to_dw2regnum, to convert REGNAME to a DWARF-2
2785 register number. */
2786 int
2787 nios2_regname_to_dw2regnum (char *regname)
2788 {
2789 struct nios2_reg *r = nios2_reg_lookup (regname);
2790 if (r == NULL)
2791 return -1;
2792 return r->index;
2793 }
2794
2795 /* Implement tc_cfi_frame_initial_instructions, to initialize the DWARF-2
2796 unwind information for this procedure. */
2797 void
2798 nios2_frame_initial_instructions (void)
2799 {
2800 cfi_add_CFA_def_cfa (27, 0);
2801 }
2802
2803 #ifdef OBJ_ELF
2804 /* Some special processing for a Nios II ELF file. */
2805
2806 void
2807 nios2_elf_final_processing (void)
2808 {
2809 elf_elfheader (stdoutput)->e_flags = nios2_architecture;
2810 }
2811 #endif
This page took 0.105199 seconds and 4 git commands to generate.