1 /* Altera Nios II assembler. 2 Copyright (C) 2012-2020 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 "dwarf2dbg.h" 29 #include "subsegs.h" 30 #include "safe-ctype.h" 31 #include "dw2gencfi.h" 32 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 37 38 /* We can choose our endianness at run-time, regardless of configuration. */ 39 extern int target_big_endian; 40 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[] = "#"; 44 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[] = "#"; 53 54 /* This array holds machine specific line separator characters. */ 55 const char line_separator_chars[] = ";"; 56 57 /* Chars that can be used to separate mant from exp in floating point nums. */ 58 const char EXP_CHARS[] = "eE"; 59 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"; 64 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. */ 68 69 /* Machine-dependent command-line options. */ 70 71 const char *md_shortopts = "r"; 72 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} 86 }; 87 88 size_t md_longopts_size = sizeof (md_longopts); 89 90 /* The assembler supports three different relaxation modes, controlled by 91 command-line options. */ 92 typedef enum 93 { 94 relax_section = 0, 95 relax_none, 96 relax_all 97 } relax_optionT; 98 99 /* Struct contains all assembler options set with .set. */ 100 static struct 101 { 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 bfd_boolean noat; 107 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 bfd_boolean nobreak; 112 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; 119 120 } nios2_as_options = {FALSE, FALSE, relax_section}; 121 122 123 typedef struct nios2_insn_reloc 124 { 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; 128 129 /* The type of the relocation to be applied. */ 130 bfd_reloc_code_real_type reloc_type; 131 132 /* PC-relative. */ 133 unsigned int reloc_pcrel; 134 135 /* The next relocation to be applied to the instruction. */ 136 struct nios2_insn_reloc *reloc_next; 137 } nios2_insn_relocS; 138 139 /* This struct is used to hold state when assembling instructions. */ 140 typedef struct nios2_insn_info 141 { 142 /* Assembled instruction. */ 143 unsigned long insn_code; 144 145 /* Constant bits masked into insn_code for self-check mode. */ 146 unsigned long constant_bits; 147 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]; 155 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; 160 161 162 /* This struct is used to convert Nios II pseudo-ops into the 163 corresponding real op. */ 164 typedef struct nios2_ps_insn_info 165 { 166 /* Map this pseudo_op... */ 167 const char *pseudo_insn; 168 169 /* ...to this real instruction. */ 170 const char *insn; 171 172 /* Call this function to modify the operands.... */ 173 void (*arg_modifer_func) (char ** parsed_args, const char *arg, int num, 174 int start); 175 176 /* ...with these arguments. */ 177 const char *arg_modifier; 178 int num; 179 int index; 180 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; 185 186 /* Opcode hash table. */ 187 static struct hash_control *nios2_opcode_hash = NULL; 188 #define nios2_opcode_lookup(NAME) \ 189 ((struct nios2_opcode *) hash_find (nios2_opcode_hash, (NAME))) 190 191 /* Register hash table. */ 192 static struct hash_control *nios2_reg_hash = NULL; 193 #define nios2_reg_lookup(NAME) \ 194 ((struct nios2_reg *) hash_find (nios2_reg_hash, (NAME))) 195 196 197 /* Pseudo-op hash table. */ 198 static struct hash_control *nios2_ps_hash = NULL; 199 #define nios2_ps_lookup(NAME) \ 200 ((nios2_ps_insn_infoS *) hash_find (nios2_ps_hash, (NAME))) 201 202 /* The known current alignment of the current section. */ 203 static int nios2_current_align; 204 static segT nios2_current_align_seg; 205 206 static int nios2_auto_align_on = 1; 207 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; 211 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; 216 217 #ifdef OBJ_ELF 218 /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */ 219 symbolS *GOT_symbol; 220 #endif 221 222 /* The processor architecture value, EF_NIOS2_ARCH_R1 by default. */ 223 static int nios2_architecture = EF_NIOS2_ARCH_R1; 224 225 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) 232 { 233 int i; 234 valueT val; 235 236 gas_assert (n == 1 || n == 2 || n == 4); 237 238 val = 0; 239 if (target_big_endian) 240 for (i = 0; i < n; ++i) 241 val = val | ((buf[i] & 0xff) << 8 * (n - (i + 1))); 242 else 243 for (i = 0; i < n; ++i) 244 val = val | ((buf[i] & 0xff) << 8 * i); 245 return val; 246 } 247 248 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) 254 { 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); 260 } 261 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) 268 { 269 int prec; 270 LITTLENUM_TYPE words[4]; 271 char *t; 272 int i; 273 274 switch (type) 275 { 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"); 285 } 286 287 t = atof_ieee (input_line_pointer, type, words); 288 if (t) 289 input_line_pointer = t; 290 291 *sizeP = prec * 2; 292 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); 299 300 return NULL; 301 } 302 303 /* Return true if STR starts with PREFIX, which should be a string literal. */ 304 #define strprefix(STR, PREFIX) \ 305 (strncmp ((STR), PREFIX, strlen (PREFIX)) == 0) 306 307 308 /* Return true if STR is prefixed with a special relocation operator. */ 309 static int 310 nios2_special_relocation_p (const char *str) 311 { 312 return (strprefix (str, "%lo") 313 || strprefix (str, "%hi") 314 || strprefix (str, "%hiadj") 315 || strprefix (str, "%gprel") 316 || strprefix (str, "%got") 317 || strprefix (str, "%call") 318 || strprefix (str, "%gotoff_lo") 319 || strprefix (str, "%gotoff_hiadj") 320 || strprefix (str, "%tls_gd") 321 || strprefix (str, "%tls_ldm") 322 || strprefix (str, "%tls_ldo") 323 || strprefix (str, "%tls_ie") 324 || strprefix (str, "%tls_le") 325 || strprefix (str, "%gotoff")); 326 } 327 328 329 /* nop fill patterns for text section. */ 330 static char const nop_r1[4] = { 0x3a, 0x88, 0x01, 0x00 }; 331 static char const nop_r2[4] = { 0x20, 0x00, 0x00, 0xc4 }; 332 static char const nop_r2_cdx[2] = { 0x3b, 0x00 }; 333 static char const *nop32 = nop_r1; 334 static char const *nop16 = NULL; 335 336 /* Handles all machine-dependent alignment needs. */ 337 static void 338 nios2_align (int log_size, const char *pfill, symbolS *label) 339 { 340 int align; 341 long max_alignment = 15; 342 343 /* The front end is prone to changing segments out from under us 344 temporarily when -g is in effect. */ 345 int switched_seg_p = (nios2_current_align_seg != now_seg); 346 347 align = log_size; 348 if (align > max_alignment) 349 { 350 align = max_alignment; 351 as_bad (_("Alignment too large: %d. assumed"), align); 352 } 353 else if (align < 0) 354 { 355 as_warn (_("Alignment negative: 0 assumed")); 356 align = 0; 357 } 358 359 if (align != 0) 360 { 361 if (subseg_text_p (now_seg) && align >= nios2_min_align) 362 { 363 /* First, make sure we're on the minimum boundary, in case 364 someone has been putting .byte values the text section. */ 365 if (nios2_current_align < nios2_min_align || switched_seg_p) 366 frag_align (nios2_min_align, 0, 0); 367 368 /* If we might be on a 2-byte boundary, first align to a 369 4-byte boundary using the 2-byte nop as fill. */ 370 if (nios2_min_align == 1 371 && align > nios2_min_align 372 && pfill == nop32 ) 373 { 374 gas_assert (nop16); 375 frag_align_pattern (2, nop16, 2, 0); 376 } 377 378 /* Now fill in the alignment pattern. */ 379 if (pfill != NULL) 380 frag_align_pattern (align, pfill, 4, 0); 381 else 382 frag_align (align, 0, 0); 383 } 384 else 385 frag_align (align, 0, 0); 386 387 if (!switched_seg_p) 388 nios2_current_align = align; 389 390 /* If the last label was in a different section we can't align it. */ 391 if (label != NULL && !switched_seg_p) 392 { 393 symbolS *sym; 394 int label_seen = FALSE; 395 struct frag *old_frag; 396 valueT old_value; 397 valueT new_value; 398 399 gas_assert (S_GET_SEGMENT (label) == now_seg); 400 401 old_frag = symbol_get_frag (label); 402 old_value = S_GET_VALUE (label); 403 new_value = (valueT) frag_now_fix (); 404 405 /* It is possible to have more than one label at a particular 406 address, especially if debugging is enabled, so we must 407 take care to adjust all the labels at this address in this 408 fragment. To save time we search from the end of the symbol 409 list, backwards, since the symbols we are interested in are 410 almost certainly the ones that were most recently added. 411 Also to save time we stop searching once we have seen at least 412 one matching label, and we encounter a label that is no longer 413 in the target fragment. Note, this search is guaranteed to 414 find at least one match when sym == label, so no special case 415 code is necessary. */ 416 for (sym = symbol_lastP; sym != NULL; sym = symbol_previous (sym)) 417 if (symbol_get_frag (sym) == old_frag 418 && S_GET_VALUE (sym) == old_value) 419 { 420 label_seen = TRUE; 421 symbol_set_frag (sym, frag_now); 422 S_SET_VALUE (sym, new_value); 423 } 424 else if (label_seen && symbol_get_frag (sym) != old_frag) 425 break; 426 } 427 record_alignment (now_seg, align); 428 } 429 } 430 431 432 /** Support for self-check mode. */ 433 434 /* Mode of the assembler. */ 435 typedef enum 436 { 437 NIOS2_MODE_ASSEMBLE, /* Ordinary operation. */ 438 NIOS2_MODE_TEST /* Hidden mode used for self testing. */ 439 } NIOS2_MODE; 440 441 static NIOS2_MODE nios2_mode = NIOS2_MODE_ASSEMBLE; 442 443 /* This function is used to in self-checking mode 444 to check the assembled instruction 445 opcode should be the assembled opcode, and exp_opcode 446 the parsed string representing the expected opcode. */ 447 static void 448 nios2_check_assembly (unsigned int opcode, const char *exp_opcode) 449 { 450 if (nios2_mode == NIOS2_MODE_TEST) 451 { 452 if (exp_opcode == NULL) 453 as_bad (_("expecting opcode string in self test mode")); 454 else if (opcode != strtoul (exp_opcode, NULL, 16)) 455 as_bad (_("assembly 0x%08x, expected %s"), opcode, exp_opcode); 456 } 457 } 458 459 460 /** Support for machine-dependent assembler directives. */ 461 /* Handle the .align pseudo-op. This aligns to a power of two. It 462 also adjusts any current instruction label. We treat this the same 463 way the MIPS port does: .align 0 turns off auto alignment. */ 464 static void 465 s_nios2_align (int ignore ATTRIBUTE_UNUSED) 466 { 467 int align; 468 char fill; 469 const char *pfill = NULL; 470 long max_alignment = 15; 471 472 align = get_absolute_expression (); 473 if (align > max_alignment) 474 { 475 align = max_alignment; 476 as_bad (_("Alignment too large: %d. assumed"), align); 477 } 478 else if (align < 0) 479 { 480 as_warn (_("Alignment negative: 0 assumed")); 481 align = 0; 482 } 483 484 if (*input_line_pointer == ',') 485 { 486 input_line_pointer++; 487 fill = get_absolute_expression (); 488 pfill = (const char *) &fill; 489 } 490 else if (subseg_text_p (now_seg)) 491 pfill = (const char *) nop32; 492 else 493 { 494 pfill = NULL; 495 nios2_last_label = NULL; 496 } 497 498 if (align != 0) 499 { 500 nios2_auto_align_on = 1; 501 nios2_align (align, pfill, nios2_last_label); 502 nios2_last_label = NULL; 503 } 504 else 505 nios2_auto_align_on = 0; 506 507 demand_empty_rest_of_line (); 508 } 509 510 /* Handle the .text pseudo-op. This is like the usual one, but it 511 clears the saved last label and resets known alignment. */ 512 static void 513 s_nios2_text (int i) 514 { 515 s_text (i); 516 nios2_last_label = NULL; 517 nios2_current_align = 0; 518 nios2_current_align_seg = now_seg; 519 } 520 521 /* Handle the .data pseudo-op. This is like the usual one, but it 522 clears the saved last label and resets known alignment. */ 523 static void 524 s_nios2_data (int i) 525 { 526 s_data (i); 527 nios2_last_label = NULL; 528 nios2_current_align = 0; 529 nios2_current_align_seg = now_seg; 530 } 531 532 /* Handle the .section pseudo-op. This is like the usual one, but it 533 clears the saved last label and resets known alignment. */ 534 static void 535 s_nios2_section (int ignore) 536 { 537 obj_elf_section (ignore); 538 nios2_last_label = NULL; 539 nios2_current_align = 0; 540 nios2_current_align_seg = now_seg; 541 } 542 543 /* Explicitly unaligned cons. */ 544 static void 545 s_nios2_ucons (int nbytes) 546 { 547 int hold; 548 hold = nios2_auto_align_on; 549 nios2_auto_align_on = 0; 550 cons (nbytes); 551 nios2_auto_align_on = hold; 552 } 553 554 /* Handle the .sdata directive. */ 555 static void 556 s_nios2_sdata (int ignore ATTRIBUTE_UNUSED) 557 { 558 get_absolute_expression (); /* Ignored. */ 559 subseg_new (".sdata", 0); 560 demand_empty_rest_of_line (); 561 } 562 563 /* .set sets assembler options eg noat/at and is also used 564 to set symbol values (.equ, .equiv ). */ 565 static void 566 s_nios2_set (int equiv) 567 { 568 char *save = input_line_pointer; 569 char *directive; 570 char delim = get_symbol_name (&directive); 571 char *endline = input_line_pointer; 572 573 (void) restore_line_pointer (delim); 574 575 /* We only want to handle ".set XXX" if the 576 user has tried ".set XXX, YYY" they are not 577 trying a directive. This prevents 578 us from polluting the name space. */ 579 SKIP_WHITESPACE (); 580 if (is_end_of_line[(unsigned char) *input_line_pointer]) 581 { 582 bfd_boolean done = TRUE; 583 *endline = 0; 584 585 if (!strcmp (directive, "noat")) 586 nios2_as_options.noat = TRUE; 587 else if (!strcmp (directive, "at")) 588 nios2_as_options.noat = FALSE; 589 else if (!strcmp (directive, "nobreak")) 590 nios2_as_options.nobreak = TRUE; 591 else if (!strcmp (directive, "break")) 592 nios2_as_options.nobreak = FALSE; 593 else if (!strcmp (directive, "norelax")) 594 nios2_as_options.relax = relax_none; 595 else if (!strcmp (directive, "relaxsection")) 596 nios2_as_options.relax = relax_section; 597 else if (!strcmp (directive, "relaxall")) 598 nios2_as_options.relax = relax_all; 599 else 600 done = FALSE; 601 602 if (done) 603 { 604 *endline = delim; 605 demand_empty_rest_of_line (); 606 return; 607 } 608 } 609 610 /* If we fall through to here, either we have ".set XXX, YYY" 611 or we have ".set XXX" where XXX is unknown or we have 612 a syntax error. */ 613 input_line_pointer = save; 614 s_set (equiv); 615 } 616 617 /* Machine-dependent assembler directives. 618 Format of each entry is: 619 { "directive", handler_func, param } */ 620 const pseudo_typeS md_pseudo_table[] = { 621 {"align", s_nios2_align, 0}, 622 {"text", s_nios2_text, 0}, 623 {"data", s_nios2_data, 0}, 624 {"section", s_nios2_section, 0}, 625 {"section.s", s_nios2_section, 0}, 626 {"sect", s_nios2_section, 0}, 627 {"sect.s", s_nios2_section, 0}, 628 /* .dword and .half are included for compatibility with MIPS. */ 629 {"dword", cons, 8}, 630 {"half", cons, 2}, 631 /* NIOS2 native word size is 4 bytes, so we override 632 the GAS default of 2. */ 633 {"word", cons, 4}, 634 /* Explicitly unaligned directives. */ 635 {"2byte", s_nios2_ucons, 2}, 636 {"4byte", s_nios2_ucons, 4}, 637 {"8byte", s_nios2_ucons, 8}, 638 {"16byte", s_nios2_ucons, 16}, 639 #ifdef OBJ_ELF 640 {"sdata", s_nios2_sdata, 0}, 641 #endif 642 {"set", s_nios2_set, 0}, 643 {NULL, NULL, 0} 644 }; 645 646 647 /** Relaxation support. */ 648 649 /* We support two relaxation modes: a limited PC-relative mode with 650 -relax-section (the default), and an absolute jump mode with -relax-all. 651 652 Nios II PC-relative branch instructions only support 16-bit offsets. 653 And, there's no good way to add a 32-bit constant to the PC without 654 using two registers. 655 656 To deal with this, for the pc-relative relaxation mode we convert 657 br label 658 into a series of 16-bit adds, like: 659 nextpc at 660 addi at, at, 32767 661 ... 662 addi at, at, remainder 663 jmp at 664 665 Similarly, conditional branches are converted from 666 b(condition) r, s, label 667 into a series like: 668 b(opposite condition) r, s, skip 669 nextpc at 670 addi at, at, 32767 671 ... 672 addi at, at, remainder 673 jmp at 674 skip: 675 676 The compiler can do a better job, either by converting the branch 677 directly into a JMP (going through the GOT for PIC) or by allocating 678 a second register for the 32-bit displacement. 679 680 For the -relax-all relaxation mode, the conversions are 681 movhi at, %hi(symbol+offset) 682 ori at, %lo(symbol+offset) 683 jmp at 684 and 685 b(opposite condition), r, s, skip 686 movhi at, %hi(symbol+offset) 687 ori at, %lo(symbol+offset) 688 jmp at 689 skip: 690 respectively. 691 692 16-bit CDX branch instructions are relaxed first into equivalent 693 32-bit branches and then the above transformations are applied 694 if necessary. 695 696 */ 697 698 /* Arbitrarily limit the number of addis we can insert; we need to be able 699 to specify the maximum growth size for each frag that contains a 700 relaxable branch. There's no point in specifying a huge number here 701 since that means the assembler needs to allocate that much extra 702 memory for every branch, and almost no real code will ever need it. 703 Plus, as already noted a better solution is to just use a jmp, or 704 allocate a second register to hold a 32-bit displacement. 705 FIXME: Rather than making this a constant, it could be controlled by 706 a command-line argument. */ 707 #define RELAX_MAX_ADDI 32 708 709 /* The fr_subtype field represents the target-specific relocation state. 710 It has type relax_substateT (unsigned int). We use it to track the 711 number of addis necessary, plus a bit to track whether this is a 712 conditional branch and a bit for 16-bit CDX instructions. 713 Regardless of the smaller RELAX_MAX_ADDI limit, we reserve 16 bits 714 in the fr_subtype to encode the number of addis so that the whole 715 theoretically-valid range is representable. 716 For the -relax-all mode, N = 0 represents an in-range branch and N = 1 717 represents a branch that needs to be relaxed. */ 718 #define UBRANCH (0 << 16) 719 #define CBRANCH (1 << 16) 720 #define CDXBRANCH (1 << 17) 721 #define IS_CBRANCH(SUBTYPE) ((SUBTYPE) & CBRANCH) 722 #define IS_UBRANCH(SUBTYPE) (!IS_CBRANCH (SUBTYPE)) 723 #define IS_CDXBRANCH(SUBTYPE) ((SUBTYPE) & CDXBRANCH) 724 #define UBRANCH_SUBTYPE(N) (UBRANCH | (N)) 725 #define CBRANCH_SUBTYPE(N) (CBRANCH | (N)) 726 #define CDX_UBRANCH_SUBTYPE(N) (CDXBRANCH | UBRANCH | (N)) 727 #define CDX_CBRANCH_SUBTYPE(N) (CDXBRANCH | CBRANCH | (N)) 728 #define SUBTYPE_ADDIS(SUBTYPE) ((SUBTYPE) & 0xffff) 729 730 /* For the -relax-section mode, unconditional branches require 2 extra 731 instructions besides the addis, conditional branches require 3. */ 732 #define UBRANCH_ADDIS_TO_SIZE(N) (((N) + 2) * 4) 733 #define CBRANCH_ADDIS_TO_SIZE(N) (((N) + 3) * 4) 734 735 /* For the -relax-all mode, unconditional branches require 3 instructions 736 and conditional branches require 4. */ 737 #define UBRANCH_JUMP_SIZE 12 738 #define CBRANCH_JUMP_SIZE 16 739 740 /* Maximum sizes of relaxation sequences. */ 741 #define UBRANCH_MAX_SIZE \ 742 (nios2_as_options.relax == relax_all \ 743 ? UBRANCH_JUMP_SIZE \ 744 : UBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI)) 745 #define CBRANCH_MAX_SIZE \ 746 (nios2_as_options.relax == relax_all \ 747 ? CBRANCH_JUMP_SIZE \ 748 : CBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI)) 749 750 /* Register number of AT, the assembler temporary. */ 751 #define AT_REGNUM 1 752 753 /* Determine how many bytes are required to represent the sequence 754 indicated by SUBTYPE. */ 755 static int 756 nios2_relax_subtype_size (relax_substateT subtype) 757 { 758 int n = SUBTYPE_ADDIS (subtype); 759 if (n == 0) 760 /* Regular conditional/unconditional branch instruction. */ 761 return (IS_CDXBRANCH (subtype) ? 2 : 4); 762 else if (nios2_as_options.relax == relax_all) 763 return (IS_CBRANCH (subtype) ? CBRANCH_JUMP_SIZE : UBRANCH_JUMP_SIZE); 764 else if (IS_CBRANCH (subtype)) 765 return CBRANCH_ADDIS_TO_SIZE (n); 766 else 767 return UBRANCH_ADDIS_TO_SIZE (n); 768 } 769 770 /* Estimate size of fragp before relaxation. 771 This could also examine the offset in fragp and adjust 772 fragp->fr_subtype, but we will do that in nios2_relax_frag anyway. */ 773 int 774 md_estimate_size_before_relax (fragS *fragp, segT segment ATTRIBUTE_UNUSED) 775 { 776 return nios2_relax_subtype_size (fragp->fr_subtype); 777 } 778 779 /* Implement md_relax_frag, returning the change in size of the frag. */ 780 long 781 nios2_relax_frag (segT segment, fragS *fragp, long stretch) 782 { 783 addressT target = fragp->fr_offset; 784 relax_substateT subtype = fragp->fr_subtype; 785 symbolS *symbolp = fragp->fr_symbol; 786 787 if (symbolp) 788 { 789 fragS *sym_frag = symbol_get_frag (symbolp); 790 offsetT offset; 791 int n; 792 bfd_boolean is_cdx = FALSE; 793 794 target += S_GET_VALUE (symbolp); 795 796 /* See comments in write.c:relax_frag about handling of stretch. */ 797 if (stretch != 0 798 && sym_frag->relax_marker != fragp->relax_marker) 799 { 800 if (stretch < 0 || sym_frag->region == fragp->region) 801 target += stretch; 802 else if (target < fragp->fr_address) 803 target = fragp->fr_next->fr_address + stretch; 804 } 805 806 /* We subtract fr_var (4 for 32-bit insns) because all pc relative 807 branches are from the next instruction. */ 808 offset = target - fragp->fr_address - fragp->fr_fix - fragp->fr_var; 809 if (IS_CDXBRANCH (subtype) && IS_UBRANCH (subtype) 810 && offset >= -1024 && offset < 1024) 811 /* PC-relative CDX branch with 11-bit offset. */ 812 is_cdx = TRUE; 813 else if (IS_CDXBRANCH (subtype) && IS_CBRANCH (subtype) 814 && offset >= -128 && offset < 128) 815 /* PC-relative CDX branch with 8-bit offset. */ 816 is_cdx = TRUE; 817 else if (offset >= -32768 && offset < 32768) 818 /* Fits in PC-relative branch. */ 819 n = 0; 820 else if (nios2_as_options.relax == relax_all) 821 /* Convert to jump. */ 822 n = 1; 823 else if (nios2_as_options.relax == relax_section 824 && S_GET_SEGMENT (symbolp) == segment 825 && S_IS_DEFINED (symbolp)) 826 /* Attempt a PC-relative relaxation on a branch to a defined 827 symbol in the same segment. */ 828 { 829 /* The relaxation for conditional branches is offset by 4 830 bytes because we insert the inverted branch around the 831 sequence. */ 832 if (IS_CBRANCH (subtype)) 833 offset = offset - 4; 834 if (offset > 0) 835 n = offset / 32767 + 1; 836 else 837 n = offset / -32768 + 1; 838 839 /* Bail out immediately if relaxation has failed. If we try to 840 defer the diagnostic to md_convert_frag, some pathological test 841 cases (e.g. gcc/testsuite/gcc.c-torture/compile/20001226-1.c) 842 apparently never converge. By returning 0 here we could pretend 843 to the caller that nothing has changed, but that leaves things 844 in an inconsistent state when we get to md_convert_frag. */ 845 if (n > RELAX_MAX_ADDI) 846 { 847 as_bad_where (fragp->fr_file, fragp->fr_line, 848 _("branch offset out of range\n")); 849 as_fatal (_("branch relaxation failed\n")); 850 } 851 } 852 else 853 /* We cannot handle this case, diagnose overflow later. */ 854 return 0; 855 856 if (is_cdx) 857 fragp->fr_subtype = subtype; 858 else if (IS_CBRANCH (subtype)) 859 fragp->fr_subtype = CBRANCH_SUBTYPE (n); 860 else 861 fragp->fr_subtype = UBRANCH_SUBTYPE (n); 862 863 return (nios2_relax_subtype_size (fragp->fr_subtype) 864 - nios2_relax_subtype_size (subtype)); 865 } 866 867 /* If we got here, it's probably an error. */ 868 return 0; 869 } 870 871 872 /* Complete fragp using the data from the relaxation pass. */ 873 void 874 md_convert_frag (bfd *headers ATTRIBUTE_UNUSED, segT segment ATTRIBUTE_UNUSED, 875 fragS *fragp) 876 { 877 char *buffer = fragp->fr_literal + fragp->fr_fix; 878 relax_substateT subtype = fragp->fr_subtype; 879 int n = SUBTYPE_ADDIS (subtype); 880 addressT target = fragp->fr_offset; 881 symbolS *symbolp = fragp->fr_symbol; 882 offsetT offset; 883 unsigned int addend_mask, addi_mask, op; 884 offsetT addend, remainder; 885 int i; 886 bfd_boolean is_r2 = (bfd_get_mach (stdoutput) == bfd_mach_nios2r2); 887 888 /* If this is a CDX branch we're not relaxing, just generate the fixup. */ 889 if (IS_CDXBRANCH (subtype)) 890 { 891 gas_assert (is_r2); 892 fix_new (fragp, fragp->fr_fix, 2, fragp->fr_symbol, 893 fragp->fr_offset, 1, 894 (IS_UBRANCH (subtype) 895 ? BFD_RELOC_NIOS2_R2_I10_1_PCREL 896 : BFD_RELOC_NIOS2_R2_T1I7_1_PCREL)); 897 fragp->fr_fix += 2; 898 return; 899 } 900 901 /* If this is a CDX branch we are relaxing, turn it into an equivalent 902 32-bit branch and then fall through to the normal non-CDX cases. */ 903 if (fragp->fr_var == 2) 904 { 905 unsigned int opcode = md_chars_to_number (buffer, 2); 906 gas_assert (is_r2); 907 if (IS_CBRANCH (subtype)) 908 { 909 unsigned int reg = nios2_r2_reg3_mappings[GET_IW_T1I7_A3 (opcode)]; 910 if (GET_IW_R2_OP (opcode) == R2_OP_BNEZ_N) 911 opcode = MATCH_R2_BNE | SET_IW_F2I16_A (reg); 912 else 913 opcode = MATCH_R2_BEQ | SET_IW_F2I16_A (reg); 914 } 915 else 916 opcode = MATCH_R2_BR; 917 md_number_to_chars (buffer, opcode, 4); 918 fragp->fr_var = 4; 919 } 920 921 /* If we didn't or can't relax, this is a regular branch instruction. 922 We just need to generate the fixup for the symbol and offset. */ 923 if (n == 0) 924 { 925 fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol, 926 fragp->fr_offset, 1, BFD_RELOC_16_PCREL); 927 fragp->fr_fix += 4; 928 return; 929 } 930 931 /* Replace the cbranch at fr_fix with one that has the opposite condition 932 in order to jump around the block of instructions we'll be adding. */ 933 if (IS_CBRANCH (subtype)) 934 { 935 unsigned int br_opcode; 936 unsigned int old_op, new_op; 937 int nbytes; 938 939 /* Account for the nextpc and jmp in the pc-relative case, or the two 940 load instructions and jump in the absolute case. */ 941 if (nios2_as_options.relax == relax_section) 942 nbytes = (n + 2) * 4; 943 else 944 nbytes = 12; 945 946 br_opcode = md_chars_to_number (buffer, 4); 947 if (is_r2) 948 { 949 old_op = GET_IW_R2_OP (br_opcode); 950 switch (old_op) 951 { 952 case R2_OP_BEQ: 953 new_op = R2_OP_BNE; 954 break; 955 case R2_OP_BNE: 956 new_op = R2_OP_BEQ; 957 break; 958 case R2_OP_BGE: 959 new_op = R2_OP_BLT; 960 break; 961 case R2_OP_BGEU: 962 new_op = R2_OP_BLTU; 963 break; 964 case R2_OP_BLT: 965 new_op = R2_OP_BGE; 966 break; 967 case R2_OP_BLTU: 968 new_op = R2_OP_BGEU; 969 break; 970 default: 971 abort (); 972 } 973 br_opcode = ((br_opcode & ~IW_R2_OP_SHIFTED_MASK) 974 | SET_IW_R2_OP (new_op)); 975 br_opcode = br_opcode | SET_IW_F2I16_IMM16 (nbytes); 976 } 977 else 978 { 979 old_op = GET_IW_R1_OP (br_opcode); 980 switch (old_op) 981 { 982 case R1_OP_BEQ: 983 new_op = R1_OP_BNE; 984 break; 985 case R1_OP_BNE: 986 new_op = R1_OP_BEQ; 987 break; 988 case R1_OP_BGE: 989 new_op = R1_OP_BLT; 990 break; 991 case R1_OP_BGEU: 992 new_op = R1_OP_BLTU; 993 break; 994 case R1_OP_BLT: 995 new_op = R1_OP_BGE; 996 break; 997 case R1_OP_BLTU: 998 new_op = R1_OP_BGEU; 999 break; 1000 default: 1001 abort (); 1002 } 1003 br_opcode = ((br_opcode & ~IW_R1_OP_SHIFTED_MASK) 1004 | SET_IW_R1_OP (new_op)); 1005 br_opcode = br_opcode | SET_IW_I_IMM16 (nbytes); 1006 } 1007 md_number_to_chars (buffer, br_opcode, 4); 1008 fragp->fr_fix += 4; 1009 buffer += 4; 1010 } 1011 1012 /* Load at for the PC-relative case. */ 1013 if (nios2_as_options.relax == relax_section) 1014 { 1015 /* Insert the nextpc instruction. */ 1016 if (is_r2) 1017 op = MATCH_R2_NEXTPC | SET_IW_F3X6L5_C (AT_REGNUM); 1018 else 1019 op = MATCH_R1_NEXTPC | SET_IW_R_C (AT_REGNUM); 1020 md_number_to_chars (buffer, op, 4); 1021 fragp->fr_fix += 4; 1022 buffer += 4; 1023 1024 /* We need to know whether the offset is positive or negative. */ 1025 target += S_GET_VALUE (symbolp); 1026 offset = target - fragp->fr_address - fragp->fr_fix; 1027 if (offset > 0) 1028 addend = 32767; 1029 else 1030 addend = -32768; 1031 if (is_r2) 1032 addend_mask = SET_IW_F2I16_IMM16 ((unsigned int)addend); 1033 else 1034 addend_mask = SET_IW_I_IMM16 ((unsigned int)addend); 1035 1036 /* Insert n-1 addi instructions. */ 1037 if (is_r2) 1038 addi_mask = (MATCH_R2_ADDI 1039 | SET_IW_F2I16_B (AT_REGNUM) 1040 | SET_IW_F2I16_A (AT_REGNUM)); 1041 else 1042 addi_mask = (MATCH_R1_ADDI 1043 | SET_IW_I_B (AT_REGNUM) 1044 | SET_IW_I_A (AT_REGNUM)); 1045 for (i = 0; i < n - 1; i ++) 1046 { 1047 md_number_to_chars (buffer, addi_mask | addend_mask, 4); 1048 fragp->fr_fix += 4; 1049 buffer += 4; 1050 } 1051 1052 /* Insert the last addi instruction to hold the remainder. */ 1053 remainder = offset - addend * (n - 1); 1054 gas_assert (remainder >= -32768 && remainder <= 32767); 1055 if (is_r2) 1056 addend_mask = SET_IW_F2I16_IMM16 ((unsigned int)remainder); 1057 else 1058 addend_mask = SET_IW_I_IMM16 ((unsigned int)remainder); 1059 md_number_to_chars (buffer, addi_mask | addend_mask, 4); 1060 fragp->fr_fix += 4; 1061 buffer += 4; 1062 } 1063 1064 /* Load at for the absolute case. */ 1065 else 1066 { 1067 if (is_r2) 1068 op = MATCH_R2_ORHI | SET_IW_F2I16_B (AT_REGNUM) | SET_IW_F2I16_A (0); 1069 else 1070 op = MATCH_R1_ORHI | SET_IW_I_B (AT_REGNUM) | SET_IW_I_A (0); 1071 md_number_to_chars (buffer, op, 4); 1072 fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol, fragp->fr_offset, 1073 0, BFD_RELOC_NIOS2_HI16); 1074 fragp->fr_fix += 4; 1075 buffer += 4; 1076 if (is_r2) 1077 op = (MATCH_R2_ORI | SET_IW_F2I16_B (AT_REGNUM) 1078 | SET_IW_F2I16_A (AT_REGNUM)); 1079 else 1080 op = (MATCH_R1_ORI | SET_IW_I_B (AT_REGNUM) 1081 | SET_IW_I_A (AT_REGNUM)); 1082 md_number_to_chars (buffer, op, 4); 1083 fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol, fragp->fr_offset, 1084 0, BFD_RELOC_NIOS2_LO16); 1085 fragp->fr_fix += 4; 1086 buffer += 4; 1087 } 1088 1089 /* Insert the jmp instruction. */ 1090 if (is_r2) 1091 op = MATCH_R2_JMP | SET_IW_F3X6L5_A (AT_REGNUM); 1092 else 1093 op = MATCH_R1_JMP | SET_IW_R_A (AT_REGNUM); 1094 md_number_to_chars (buffer, op, 4); 1095 fragp->fr_fix += 4; 1096 buffer += 4; 1097 } 1098 1099 1100 /** Fixups and overflow checking. */ 1101 1102 /* Check a fixup for overflow. */ 1103 static bfd_boolean 1104 nios2_check_overflow (valueT fixup, reloc_howto_type *howto) 1105 { 1106 /* If there is a rightshift, check that the low-order bits are 1107 zero before applying it. */ 1108 if (howto->rightshift) 1109 { 1110 if ((~(~((valueT) 0) << howto->rightshift) & fixup) 1111 && howto->complain_on_overflow != complain_overflow_dont) 1112 return TRUE; 1113 fixup = ((signed)fixup) >> howto->rightshift; 1114 } 1115 1116 /* Check for overflow - return TRUE if overflow, FALSE if not. */ 1117 switch (howto->complain_on_overflow) 1118 { 1119 case complain_overflow_dont: 1120 break; 1121 case complain_overflow_bitfield: 1122 if ((fixup >> howto->bitsize) != 0 1123 && ((signed) fixup >> howto->bitsize) != -1) 1124 return TRUE; 1125 break; 1126 case complain_overflow_signed: 1127 if ((fixup & 0x80000000) > 0) 1128 { 1129 /* Check for negative overflow. */ 1130 if ((signed) fixup < (signed) (~0U << (howto->bitsize - 1))) 1131 return TRUE; 1132 } 1133 else 1134 { 1135 /* Check for positive overflow. */ 1136 if (fixup >= ((unsigned) 1 << (howto->bitsize - 1))) 1137 return TRUE; 1138 } 1139 break; 1140 case complain_overflow_unsigned: 1141 if ((fixup >> howto->bitsize) != 0) 1142 return TRUE; 1143 break; 1144 default: 1145 as_bad (_("error checking for overflow - broken assembler")); 1146 break; 1147 } 1148 return FALSE; 1149 } 1150 1151 /* Emit diagnostic for fixup overflow. */ 1152 static void 1153 nios2_diagnose_overflow (valueT fixup, reloc_howto_type *howto, 1154 fixS *fixP, valueT value) 1155 { 1156 if (fixP->fx_r_type == BFD_RELOC_8 1157 || fixP->fx_r_type == BFD_RELOC_16 1158 || fixP->fx_r_type == BFD_RELOC_32) 1159 /* These relocs are against data, not instructions. */ 1160 as_bad_where (fixP->fx_file, fixP->fx_line, 1161 _("immediate value 0x%x truncated to 0x%x"), 1162 (unsigned int) fixup, 1163 (unsigned int) (~(~(valueT) 0 << howto->bitsize) & fixup)); 1164 else 1165 { 1166 /* What opcode is the instruction? This will determine 1167 whether we check for overflow in immediate values 1168 and what error message we get. */ 1169 const struct nios2_opcode *opcode; 1170 enum overflow_type overflow_msg_type; 1171 unsigned int range_min; 1172 unsigned int range_max; 1173 unsigned int address; 1174 1175 opcode = nios2_find_opcode_hash (value, bfd_get_mach (stdoutput)); 1176 gas_assert (opcode); 1177 gas_assert (fixP->fx_size == opcode->size); 1178 overflow_msg_type = opcode->overflow_msg; 1179 switch (overflow_msg_type) 1180 { 1181 case call_target_overflow: 1182 range_min 1183 = ((fixP->fx_frag->fr_address + fixP->fx_where) & 0xf0000000); 1184 range_max = range_min + 0x0fffffff; 1185 address = fixup | range_min; 1186 1187 as_bad_where (fixP->fx_file, fixP->fx_line, 1188 _("call target address 0x%08x out of range 0x%08x to 0x%08x"), 1189 address, range_min, range_max); 1190 break; 1191 case branch_target_overflow: 1192 if (opcode->format == iw_i_type || opcode->format == iw_F2I16_type) 1193 as_bad_where (fixP->fx_file, fixP->fx_line, 1194 _("branch offset %d out of range %d to %d"), 1195 (int)fixup, -32768, 32767); 1196 else 1197 as_bad_where (fixP->fx_file, fixP->fx_line, 1198 _("branch offset %d out of range"), 1199 (int)fixup); 1200 break; 1201 case address_offset_overflow: 1202 if (opcode->format == iw_i_type || opcode->format == iw_F2I16_type) 1203 as_bad_where (fixP->fx_file, fixP->fx_line, 1204 _("%s offset %d out of range %d to %d"), 1205 opcode->name, (int)fixup, -32768, 32767); 1206 else 1207 as_bad_where (fixP->fx_file, fixP->fx_line, 1208 _("%s offset %d out of range"), 1209 opcode->name, (int)fixup); 1210 break; 1211 case signed_immed16_overflow: 1212 as_bad_where (fixP->fx_file, fixP->fx_line, 1213 _("immediate value %d out of range %d to %d"), 1214 (int)fixup, -32768, 32767); 1215 break; 1216 case unsigned_immed16_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, 65535); 1220 break; 1221 case unsigned_immed5_overflow: 1222 as_bad_where (fixP->fx_file, fixP->fx_line, 1223 _("immediate value %u out of range %u to %u"), 1224 (unsigned int)fixup, 0, 31); 1225 break; 1226 case signed_immed12_overflow: 1227 as_bad_where (fixP->fx_file, fixP->fx_line, 1228 _("immediate value %d out of range %d to %d"), 1229 (int)fixup, -2048, 2047); 1230 break; 1231 case custom_opcode_overflow: 1232 as_bad_where (fixP->fx_file, fixP->fx_line, 1233 _("custom instruction opcode %u out of range %u to %u"), 1234 (unsigned int)fixup, 0, 255); 1235 break; 1236 default: 1237 as_bad_where (fixP->fx_file, fixP->fx_line, 1238 _("overflow in immediate argument")); 1239 break; 1240 } 1241 } 1242 } 1243 1244 /* Apply a fixup to the object file. */ 1245 void 1246 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED) 1247 { 1248 /* Assert that the fixup is one we can handle. */ 1249 gas_assert (fixP != NULL && valP != NULL 1250 && (fixP->fx_r_type == BFD_RELOC_8 1251 || fixP->fx_r_type == BFD_RELOC_16 1252 || fixP->fx_r_type == BFD_RELOC_32 1253 || fixP->fx_r_type == BFD_RELOC_64 1254 || fixP->fx_r_type == BFD_RELOC_NIOS2_S16 1255 || fixP->fx_r_type == BFD_RELOC_NIOS2_U16 1256 || fixP->fx_r_type == BFD_RELOC_16_PCREL 1257 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL26 1258 || fixP->fx_r_type == BFD_RELOC_NIOS2_IMM5 1259 || fixP->fx_r_type == BFD_RELOC_NIOS2_CACHE_OPX 1260 || fixP->fx_r_type == BFD_RELOC_NIOS2_IMM6 1261 || fixP->fx_r_type == BFD_RELOC_NIOS2_IMM8 1262 || fixP->fx_r_type == BFD_RELOC_NIOS2_HI16 1263 || fixP->fx_r_type == BFD_RELOC_NIOS2_LO16 1264 || fixP->fx_r_type == BFD_RELOC_NIOS2_HIADJ16 1265 || fixP->fx_r_type == BFD_RELOC_NIOS2_GPREL 1266 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT 1267 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY 1268 || fixP->fx_r_type == BFD_RELOC_NIOS2_UJMP 1269 || fixP->fx_r_type == BFD_RELOC_NIOS2_CJMP 1270 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALLR 1271 || fixP->fx_r_type == BFD_RELOC_NIOS2_ALIGN 1272 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOT16 1273 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL16 1274 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_LO 1275 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_HA 1276 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_GD16 1277 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LDM16 1278 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LDO16 1279 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_IE16 1280 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LE16 1281 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF 1282 || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPREL 1283 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL26_NOAT 1284 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOT_LO 1285 || fixP->fx_r_type == BFD_RELOC_NIOS2_GOT_HA 1286 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL_LO 1287 || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL_HA 1288 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_S12 1289 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_I10_1_PCREL 1290 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T1I7_1_PCREL 1291 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T1I7_2 1292 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T2I4 1293 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T2I4_1 1294 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T2I4_2 1295 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_X1I7_2 1296 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_X2L5 1297 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_F1I5_2 1298 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_L5I4X1 1299 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T1X1I6 1300 || fixP->fx_r_type == BFD_RELOC_NIOS2_R2_T1X1I6_2 1301 /* Add other relocs here as we generate them. */ 1302 )); 1303 1304 if (fixP->fx_r_type == BFD_RELOC_64) 1305 { 1306 /* We may reach here due to .8byte directives, but we never output 1307 BFD_RELOC_64; it must be resolved. */ 1308 if (fixP->fx_addsy != NULL) 1309 as_bad_where (fixP->fx_file, fixP->fx_line, 1310 _("cannot create 64-bit relocation")); 1311 else 1312 { 1313 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where, 1314 *valP, 8); 1315 fixP->fx_done = 1; 1316 } 1317 return; 1318 } 1319 1320 /* The value passed in valP can be the value of a fully 1321 resolved expression, or it can be the value of a partially 1322 resolved expression. In the former case, both fixP->fx_addsy 1323 and fixP->fx_subsy are NULL, and fixP->fx_offset == *valP, and 1324 we can fix up the instruction that fixP relates to. 1325 In the latter case, one or both of fixP->fx_addsy and 1326 fixP->fx_subsy are not NULL, and fixP->fx_offset may or may not 1327 equal *valP. We don't need to check for fixP->fx_subsy being null 1328 because the generic part of the assembler generates an error if 1329 it is not an absolute symbol. */ 1330 if (fixP->fx_addsy != NULL) 1331 /* Partially resolved expression. */ 1332 { 1333 fixP->fx_addnumber = fixP->fx_offset; 1334 fixP->fx_done = 0; 1335 1336 switch (fixP->fx_r_type) 1337 { 1338 case BFD_RELOC_NIOS2_TLS_GD16: 1339 case BFD_RELOC_NIOS2_TLS_LDM16: 1340 case BFD_RELOC_NIOS2_TLS_LDO16: 1341 case BFD_RELOC_NIOS2_TLS_IE16: 1342 case BFD_RELOC_NIOS2_TLS_LE16: 1343 case BFD_RELOC_NIOS2_TLS_DTPMOD: 1344 case BFD_RELOC_NIOS2_TLS_DTPREL: 1345 case BFD_RELOC_NIOS2_TLS_TPREL: 1346 S_SET_THREAD_LOCAL (fixP->fx_addsy); 1347 break; 1348 default: 1349 break; 1350 } 1351 } 1352 else 1353 /* Fully resolved fixup. */ 1354 { 1355 reloc_howto_type *howto 1356 = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type); 1357 1358 if (howto == NULL) 1359 as_bad_where (fixP->fx_file, fixP->fx_line, 1360 _("relocation is not supported")); 1361 else 1362 { 1363 valueT fixup = *valP; 1364 valueT value; 1365 char *buf; 1366 1367 /* If this is a pc-relative relocation, we need to 1368 subtract the current offset within the object file 1369 FIXME : for some reason fixP->fx_pcrel isn't 1 when it should be 1370 so I'm using the howto structure instead to determine this. */ 1371 if (howto->pc_relative == 1) 1372 { 1373 fixup = (fixup - (fixP->fx_frag->fr_address + fixP->fx_where 1374 + fixP->fx_size)); 1375 *valP = fixup; 1376 } 1377 1378 /* Get the instruction or data to be fixed up. */ 1379 buf = fixP->fx_frag->fr_literal + fixP->fx_where; 1380 value = md_chars_to_number (buf, fixP->fx_size); 1381 1382 /* Check for overflow, emitting a diagnostic if necessary. */ 1383 if (nios2_check_overflow (fixup, howto)) 1384 nios2_diagnose_overflow (fixup, howto, fixP, value); 1385 1386 /* Apply the right shift. */ 1387 fixup = (offsetT) fixup >> howto->rightshift; 1388 1389 /* Truncate the fixup to right size. */ 1390 switch (fixP->fx_r_type) 1391 { 1392 case BFD_RELOC_NIOS2_HI16: 1393 fixup = (fixup >> 16) & 0xFFFF; 1394 break; 1395 case BFD_RELOC_NIOS2_LO16: 1396 fixup = fixup & 0xFFFF; 1397 break; 1398 case BFD_RELOC_NIOS2_HIADJ16: 1399 fixup = ((fixup + 0x8000) >> 16) & 0xFFFF; 1400 break; 1401 default: 1402 { 1403 fixup &= ((valueT) 1 << howto->bitsize) - 1; 1404 break; 1405 } 1406 } 1407 1408 /* Fix up the instruction. */ 1409 value = (value & ~howto->dst_mask) | (fixup << howto->bitpos); 1410 md_number_to_chars (buf, value, fixP->fx_size); 1411 } 1412 1413 fixP->fx_done = 1; 1414 } 1415 1416 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT) 1417 { 1418 fixP->fx_done = 0; 1419 if (fixP->fx_addsy 1420 && !S_IS_DEFINED (fixP->fx_addsy) && !S_IS_WEAK (fixP->fx_addsy)) 1421 S_SET_WEAK (fixP->fx_addsy); 1422 } 1423 else if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY) 1424 fixP->fx_done = 0; 1425 } 1426 1427 1428 1429 /** Instruction parsing support. */ 1430 1431 /* General internal error routine. */ 1432 1433 static void 1434 bad_opcode (const struct nios2_opcode *op) 1435 { 1436 fprintf (stderr, _("internal error: broken opcode descriptor for `%s %s'\n"), 1437 op->name, op->args); 1438 as_fatal (_("Broken assembler. No assembly attempted.")); 1439 } 1440 1441 /* Special relocation directive strings. */ 1442 1443 struct nios2_special_relocS 1444 { 1445 const char *string; 1446 bfd_reloc_code_real_type reloc_type; 1447 }; 1448 1449 /* This table is sorted so that prefix strings are listed after the longer 1450 strings that include them -- e.g., %got after %got_hiadj, etc. */ 1451 1452 struct nios2_special_relocS nios2_special_reloc[] = { 1453 {"%hiadj", BFD_RELOC_NIOS2_HIADJ16}, 1454 {"%hi", BFD_RELOC_NIOS2_HI16}, 1455 {"%lo", BFD_RELOC_NIOS2_LO16}, 1456 {"%gprel", BFD_RELOC_NIOS2_GPREL}, 1457 {"%call_lo", BFD_RELOC_NIOS2_CALL_LO}, 1458 {"%call_hiadj", BFD_RELOC_NIOS2_CALL_HA}, 1459 {"%call", BFD_RELOC_NIOS2_CALL16}, 1460 {"%gotoff_lo", BFD_RELOC_NIOS2_GOTOFF_LO}, 1461 {"%gotoff_hiadj", BFD_RELOC_NIOS2_GOTOFF_HA}, 1462 {"%gotoff", BFD_RELOC_NIOS2_GOTOFF}, 1463 {"%got_hiadj", BFD_RELOC_NIOS2_GOT_HA}, 1464 {"%got_lo", BFD_RELOC_NIOS2_GOT_LO}, 1465 {"%got", BFD_RELOC_NIOS2_GOT16}, 1466 {"%tls_gd", BFD_RELOC_NIOS2_TLS_GD16}, 1467 {"%tls_ldm", BFD_RELOC_NIOS2_TLS_LDM16}, 1468 {"%tls_ldo", BFD_RELOC_NIOS2_TLS_LDO16}, 1469 {"%tls_ie", BFD_RELOC_NIOS2_TLS_IE16}, 1470 {"%tls_le", BFD_RELOC_NIOS2_TLS_LE16}, 1471 }; 1472 1473 #define NIOS2_NUM_SPECIAL_RELOCS \ 1474 (sizeof(nios2_special_reloc)/sizeof(nios2_special_reloc[0])) 1475 const int nios2_num_special_relocs = NIOS2_NUM_SPECIAL_RELOCS; 1476 1477 /* Creates a new nios2_insn_relocS and returns a pointer to it. */ 1478 static nios2_insn_relocS * 1479 nios2_insn_reloc_new (bfd_reloc_code_real_type reloc_type, unsigned int pcrel) 1480 { 1481 nios2_insn_relocS *retval; 1482 retval = XNEW (nios2_insn_relocS); 1483 if (retval == NULL) 1484 { 1485 as_bad (_("can't create relocation")); 1486 abort (); 1487 } 1488 1489 /* Fill out the fields with default values. */ 1490 retval->reloc_next = NULL; 1491 retval->reloc_type = reloc_type; 1492 retval->reloc_pcrel = pcrel; 1493 return retval; 1494 } 1495 1496 /* Frees up memory previously allocated by nios2_insn_reloc_new(). */ 1497 /* FIXME: this is never called; memory leak? */ 1498 #if 0 1499 static void 1500 nios2_insn_reloc_destroy (nios2_insn_relocS *reloc) 1501 { 1502 gas_assert (reloc != NULL); 1503 free (reloc); 1504 } 1505 #endif 1506 1507 /* Look up a register name and validate it for the given regtype. 1508 Return the register mapping or NULL on failure. */ 1509 static struct nios2_reg * 1510 nios2_parse_reg (const char *token, unsigned long regtype) 1511 { 1512 struct nios2_reg *reg = nios2_reg_lookup (token); 1513 1514 if (reg == NULL) 1515 { 1516 as_bad (_("unknown register %s"), token); 1517 return NULL; 1518 } 1519 1520 /* Matched a register, but is it the wrong type? */ 1521 if (!(regtype & reg->regtype)) 1522 { 1523 if (regtype & REG_CONTROL) 1524 as_bad (_("expecting control register")); 1525 else if (reg->regtype & REG_CONTROL) 1526 as_bad (_("illegal use of control register")); 1527 else if (reg->regtype & REG_COPROCESSOR) 1528 as_bad (_("illegal use of coprocessor register")); 1529 else 1530 as_bad (_("invalid register %s"), token); 1531 return NULL; 1532 } 1533 1534 /* Warn for explicit use of special registers. */ 1535 if (reg->regtype & REG_NORMAL) 1536 { 1537 if (!nios2_as_options.noat && reg->index == 1) 1538 as_warn (_("Register at (r1) can sometimes be corrupted by " 1539 "assembler optimizations.\n" 1540 "Use .set noat to turn off those optimizations " 1541 "(and this warning).")); 1542 if (!nios2_as_options.nobreak && reg->index == 25) 1543 as_warn (_("The debugger will corrupt bt (r25).\n" 1544 "If you don't need to debug this " 1545 "code use .set nobreak to turn off this warning.")); 1546 if (!nios2_as_options.nobreak && reg->index == 30) 1547 as_warn (_("The debugger will corrupt sstatus/ba (r30).\n" 1548 "If you don't need to debug this " 1549 "code use .set nobreak to turn off this warning.")); 1550 } 1551 1552 return reg; 1553 } 1554 1555 /* This function parses a reglist for ldwm/stwm and push.n/pop.n 1556 instructions, given as a brace-enclosed register list. The tokenizer 1557 has replaced commas in the token with spaces. 1558 The return value is a bitmask of registers in the set. It also 1559 sets nios2_reglist_mask and nios2_reglist_dir to allow error checking 1560 when parsing the base register. */ 1561 1562 static unsigned long nios2_reglist_mask; 1563 static int nios2_reglist_dir; 1564 1565 static unsigned long 1566 nios2_parse_reglist (char *token, const struct nios2_opcode *op) 1567 { 1568 unsigned long mask = 0; 1569 int dir = 0; 1570 unsigned long regtype = 0; 1571 int last = -1; 1572 const char *regname; 1573 1574 nios2_reglist_mask = 0; 1575 nios2_reglist_dir = 0; 1576 1577 if (op->match == MATCH_R2_LDWM || op->match == MATCH_R2_STWM) 1578 { 1579 regtype = REG_LDWM; 1580 dir = 0; 1581 } 1582 else if (op->match == MATCH_R2_PUSH_N) 1583 { 1584 regtype = REG_POP; 1585 dir = -1; 1586 } 1587 else if (op->match == MATCH_R2_POP_N) 1588 { 1589 regtype = REG_POP; 1590 dir = 1; 1591 } 1592 else 1593 bad_opcode (op); 1594 1595 for (regname = strtok (token, "{ }"); 1596 regname; 1597 regname = strtok (NULL, "{ }")) 1598 { 1599 int regno; 1600 struct nios2_reg *reg = nios2_parse_reg (regname, regtype); 1601 1602 if (!reg) 1603 break; 1604 regno = reg->index; 1605 1606 /* Make sure registers are listed in proper sequence. */ 1607 if (last >= 0) 1608 { 1609 if (regno == last) 1610 { 1611 as_bad ("duplicate register %s\n", reg->name); 1612 return 0; 1613 } 1614 else if (dir == 0) 1615 dir = (regno < last ? -1 : 1); 1616 else if ((dir > 0 && regno < last) 1617 || (dir < 0 && regno > last) 1618 || (op->match == MATCH_R2_PUSH_N 1619 && ! ((last == 31 && regno == 28) 1620 || (last == 31 && regno <= 23) 1621 || (last == 28 && regno <= 23) 1622 || (regno < 23 && regno == last - 1))) 1623 || (op->match == MATCH_R2_POP_N 1624 && ! ((regno == 31 && last == 28) 1625 || (regno == 31 && last <= 23) 1626 || (regno == 28 && last <= 23) 1627 || (last < 23 && last == regno - 1)))) 1628 { 1629 as_bad ("invalid register order"); 1630 return 0; 1631 } 1632 } 1633 1634 mask |= 1 << regno; 1635 last = regno; 1636 } 1637 1638 /* Check that all ldwm/stwm regs belong to the same set. */ 1639 if ((op->match == MATCH_R2_LDWM || op->match == MATCH_R2_STWM) 1640 && (mask & 0x00003ffc) && (mask & 0x90ffc000)) 1641 { 1642 as_bad ("invalid register set in reglist"); 1643 return 0; 1644 } 1645 1646 /* Check that push.n/pop.n regs include RA. */ 1647 if ((op->match == MATCH_R2_PUSH_N || op->match == MATCH_R2_POP_N) 1648 && ((mask & 0x80000000) == 0)) 1649 { 1650 as_bad ("reglist must include ra (r31)"); 1651 return 0; 1652 } 1653 1654 /* Check that there is at least one register in the set. */ 1655 if (!mask) 1656 { 1657 as_bad ("reglist must include at least one register"); 1658 return 0; 1659 } 1660 1661 /* OK, reglist passed validation. */ 1662 nios2_reglist_mask = mask; 1663 nios2_reglist_dir = dir; 1664 return mask; 1665 } 1666 1667 /* This function parses the base register and options used by the ldwm/stwm 1668 instructions. Returns the base register and sets the option arguments 1669 accordingly. On failure, returns NULL. */ 1670 static struct nios2_reg * 1671 nios2_parse_base_register (char *str, int *direction, int *writeback, int *ret) 1672 { 1673 char *regname; 1674 struct nios2_reg *reg; 1675 1676 *direction = 0; 1677 *writeback = 0; 1678 *ret = 0; 1679 1680 /* Check for --. */ 1681 if (strncmp (str, "--", 2) == 0) 1682 { 1683 str += 2; 1684 *direction -= 1; 1685 } 1686 1687 /* Extract the base register. */ 1688 if (*str != '(') 1689 { 1690 as_bad ("expected '(' before base register"); 1691 return NULL; 1692 } 1693 str++; 1694 regname = str; 1695 str = strchr (str, ')'); 1696 if (!str) 1697 { 1698 as_bad ("expected ')' after base register"); 1699 return NULL; 1700 } 1701 *str = '\0'; 1702 str++; 1703 reg = nios2_parse_reg (regname, REG_NORMAL); 1704 if (reg == NULL) 1705 return NULL; 1706 1707 /* Check for ++. */ 1708 if (strncmp (str, "++", 2) == 0) 1709 { 1710 str += 2; 1711 *direction += 1; 1712 } 1713 1714 /* Ensure that either -- or ++ is specified, but not both. */ 1715 if (*direction == 0) 1716 { 1717 as_bad ("invalid base register syntax"); 1718 return NULL;; 1719 } 1720 1721 /* Check for options. The tokenizer has replaced commas with spaces. */ 1722 while (*str) 1723 { 1724 while (*str == ' ') 1725 str++; 1726 if (strncmp (str, "writeback", 9) == 0) 1727 { 1728 *writeback = 1; 1729 str += 9; 1730 } 1731 else if (strncmp (str, "ret", 3) == 0) 1732 { 1733 *ret = 1; 1734 str += 3; 1735 } 1736 else if (*str) 1737 { 1738 as_bad ("invalid option syntax"); 1739 return NULL; 1740 } 1741 } 1742 1743 return reg; 1744 } 1745 1746 1747 /* The various nios2_assemble_* functions call this 1748 function to generate an expression from a string representing an expression. 1749 It then tries to evaluate the expression, and if it can, returns its value. 1750 If not, it creates a new nios2_insn_relocS and stores the expression and 1751 reloc_type for future use. */ 1752 static unsigned long 1753 nios2_assemble_expression (const char *exprstr, 1754 nios2_insn_infoS *insn, 1755 bfd_reloc_code_real_type orig_reloc_type, 1756 unsigned int pcrel) 1757 { 1758 nios2_insn_relocS *reloc; 1759 char *saved_line_ptr; 1760 unsigned long value = 0; 1761 int i; 1762 bfd_reloc_code_real_type reloc_type = orig_reloc_type; 1763 1764 gas_assert (exprstr != NULL); 1765 gas_assert (insn != NULL); 1766 1767 /* Check for relocation operators. 1768 Change the relocation type and advance the ptr to the start of 1769 the expression proper. */ 1770 for (i = 0; i < nios2_num_special_relocs; i++) 1771 if (strstr (exprstr, nios2_special_reloc[i].string) != NULL) 1772 { 1773 reloc_type = nios2_special_reloc[i].reloc_type; 1774 exprstr += strlen (nios2_special_reloc[i].string) + 1; 1775 1776 /* %lo and %hiadj have different meanings for PC-relative 1777 expressions. */ 1778 if (pcrel) 1779 { 1780 if (reloc_type == BFD_RELOC_NIOS2_LO16) 1781 reloc_type = BFD_RELOC_NIOS2_PCREL_LO; 1782 if (reloc_type == BFD_RELOC_NIOS2_HIADJ16) 1783 reloc_type = BFD_RELOC_NIOS2_PCREL_HA; 1784 } 1785 1786 break; 1787 } 1788 1789 /* No relocation allowed; we must have a constant expression. */ 1790 if (orig_reloc_type == BFD_RELOC_NONE) 1791 { 1792 expressionS exp; 1793 1794 /* Parse the expression string. */ 1795 saved_line_ptr = input_line_pointer; 1796 input_line_pointer = (char *) exprstr; 1797 expression (&exp); 1798 input_line_pointer = saved_line_ptr; 1799 1800 /* If we don't have a constant, give an error. */ 1801 if (reloc_type != orig_reloc_type || exp.X_op != O_constant) 1802 as_bad (_("expression must be constant")); 1803 else 1804 value = exp.X_add_number; 1805 return (unsigned long) value; 1806 } 1807 1808 /* We potentially have a relocation. */ 1809 reloc = nios2_insn_reloc_new (reloc_type, pcrel); 1810 reloc->reloc_next = insn->insn_reloc; 1811 insn->insn_reloc = reloc; 1812 1813 /* Parse the expression string. */ 1814 saved_line_ptr = input_line_pointer; 1815 input_line_pointer = (char *) exprstr; 1816 expression (&reloc->reloc_expression); 1817 input_line_pointer = saved_line_ptr; 1818 1819 /* This is redundant as the fixup will put this into 1820 the instruction, but it is included here so that 1821 self-test mode (-r) works. */ 1822 if (nios2_mode == NIOS2_MODE_TEST 1823 && reloc->reloc_expression.X_op == O_constant) 1824 value = reloc->reloc_expression.X_add_number; 1825 1826 return (unsigned long) value; 1827 } 1828 1829 /* Encode a 3-bit register number, giving an error if this is not possible. */ 1830 static unsigned int 1831 nios2_assemble_reg3 (const char *token) 1832 { 1833 struct nios2_reg *reg = nios2_parse_reg (token, REG_3BIT); 1834 int j; 1835 1836 if (reg == NULL) 1837 return 0; 1838 1839 for (j = 0; j < nios2_num_r2_reg3_mappings; j++) 1840 if (nios2_r2_reg3_mappings[j] == reg->index) 1841 return j; 1842 1843 /* Should never get here if we passed validation. */ 1844 as_bad (_("invalid register %s"), token); 1845 return 0; 1846 } 1847 1848 /* Argument assemble functions. */ 1849 1850 1851 /* Control register index. */ 1852 static void 1853 nios2_assemble_arg_c (const char *token, nios2_insn_infoS *insn) 1854 { 1855 struct nios2_reg *reg = nios2_parse_reg (token, REG_CONTROL); 1856 const struct nios2_opcode *op = insn->insn_nios2_opcode; 1857 1858 if (reg == NULL) 1859 return; 1860 1861 switch (op->format) 1862 { 1863 case iw_r_type: 1864 insn->insn_code |= SET_IW_R_IMM5 (reg->index); 1865 break; 1866 case iw_F3X6L5_type: 1867 insn->insn_code |= SET_IW_F3X6L5_IMM5 (reg->index); 1868 break; 1869 default: 1870 bad_opcode (op); 1871 } 1872 } 1873 1874 /* Destination register. */ 1875 static void 1876 nios2_assemble_arg_d (const char *token, nios2_insn_infoS *insn) 1877 { 1878 const struct nios2_opcode *op = insn->insn_nios2_opcode; 1879 unsigned long regtype = REG_NORMAL; 1880 struct nios2_reg *reg; 1881 1882 if (op->format == iw_custom_type || op->format == iw_F3X8_type) 1883 regtype |= REG_COPROCESSOR; 1884 reg = nios2_parse_reg (token, regtype); 1885 if (reg == NULL) 1886 return; 1887 1888 switch (op->format) 1889 { 1890 case iw_r_type: 1891 insn->insn_code |= SET_IW_R_C (reg->index); 1892 break; 1893 case iw_custom_type: 1894 insn->insn_code |= SET_IW_CUSTOM_C (reg->index); 1895 if (reg->regtype & REG_COPROCESSOR) 1896 insn->insn_code |= SET_IW_CUSTOM_READC (0); 1897 else 1898 insn->insn_code |= SET_IW_CUSTOM_READC (1); 1899 break; 1900 case iw_F3X6L5_type: 1901 case iw_F3X6_type: 1902 insn->insn_code |= SET_IW_F3X6L5_C (reg->index); 1903 break; 1904 case iw_F3X8_type: 1905 insn->insn_code |= SET_IW_F3X8_C (reg->index); 1906 if (reg->regtype & REG_COPROCESSOR) 1907 insn->insn_code |= SET_IW_F3X8_READC (0); 1908 else 1909 insn->insn_code |= SET_IW_F3X8_READC (1); 1910 break; 1911 case iw_F2_type: 1912 insn->insn_code |= SET_IW_F2_B (reg->index); 1913 break; 1914 default: 1915 bad_opcode (op); 1916 } 1917 } 1918 1919 /* Source register 1. */ 1920 static void 1921 nios2_assemble_arg_s (const char *token, nios2_insn_infoS *insn) 1922 { 1923 const struct nios2_opcode *op = insn->insn_nios2_opcode; 1924 unsigned long regtype = REG_NORMAL; 1925 struct nios2_reg *reg; 1926 1927 if (op->format == iw_custom_type || op->format == iw_F3X8_type) 1928 regtype |= REG_COPROCESSOR; 1929 reg = nios2_parse_reg (token, regtype); 1930 if (reg == NULL) 1931 return; 1932 1933 switch (op->format) 1934 { 1935 case iw_r_type: 1936 if (op->match == MATCH_R1_JMP && reg->index == 31) 1937 as_bad (_("r31 cannot be used with jmp; use ret instead")); 1938 insn->insn_code |= SET_IW_R_A (reg->index); 1939 break; 1940 case iw_i_type: 1941 insn->insn_code |= SET_IW_I_A (reg->index); 1942 break; 1943 case iw_custom_type: 1944 insn->insn_code |= SET_IW_CUSTOM_A (reg->index); 1945 if (reg->regtype & REG_COPROCESSOR) 1946 insn->insn_code |= SET_IW_CUSTOM_READA (0); 1947 else 1948 insn->insn_code |= SET_IW_CUSTOM_READA (1); 1949 break; 1950 case iw_F2I16_type: 1951 insn->insn_code |= SET_IW_F2I16_A (reg->index); 1952 break; 1953 case iw_F2X4I12_type: 1954 insn->insn_code |= SET_IW_F2X4I12_A (reg->index); 1955 break; 1956 case iw_F1X4I12_type: 1957 insn->insn_code |= SET_IW_F1X4I12_A (reg->index); 1958 break; 1959 case iw_F1X4L17_type: 1960 insn->insn_code |= SET_IW_F1X4L17_A (reg->index); 1961 break; 1962 case iw_F3X6L5_type: 1963 case iw_F3X6_type: 1964 if (op->match == MATCH_R2_JMP && reg->index == 31) 1965 as_bad (_("r31 cannot be used with jmp; use ret instead")); 1966 insn->insn_code |= SET_IW_F3X6L5_A (reg->index); 1967 break; 1968 case iw_F2X6L10_type: 1969 insn->insn_code |= SET_IW_F2X6L10_A (reg->index); 1970 break; 1971 case iw_F3X8_type: 1972 insn->insn_code |= SET_IW_F3X8_A (reg->index); 1973 if (reg->regtype & REG_COPROCESSOR) 1974 insn->insn_code |= SET_IW_F3X8_READA (0); 1975 else 1976 insn->insn_code |= SET_IW_F3X8_READA (1); 1977 break; 1978 case iw_F1X1_type: 1979 if (op->match == MATCH_R2_JMPR_N && reg->index == 31) 1980 as_bad (_("r31 cannot be used with jmpr.n; use ret.n instead")); 1981 insn->insn_code |= SET_IW_F1X1_A (reg->index); 1982 break; 1983 case iw_F1I5_type: 1984 /* Implicit stack pointer reference. */ 1985 if (reg->index != 27) 1986 as_bad (_("invalid register %s"), token); 1987 break; 1988 case iw_F2_type: 1989 insn->insn_code |= SET_IW_F2_A (reg->index); 1990 break; 1991 default: 1992 bad_opcode (op); 1993 } 1994 } 1995 1996 /* Source register 2. */ 1997 static void 1998 nios2_assemble_arg_t (const char *token, nios2_insn_infoS *insn) 1999 { 2000 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2001 unsigned long regtype = REG_NORMAL; 2002 struct nios2_reg *reg; 2003 2004 if (op->format == iw_custom_type || op->format == iw_F3X8_type) 2005 regtype |= REG_COPROCESSOR; 2006 reg = nios2_parse_reg (token, regtype); 2007 if (reg == NULL) 2008 return; 2009 2010 switch (op->format) 2011 { 2012 case iw_r_type: 2013 insn->insn_code |= SET_IW_R_B (reg->index); 2014 break; 2015 case iw_i_type: 2016 insn->insn_code |= SET_IW_I_B (reg->index); 2017 break; 2018 case iw_custom_type: 2019 insn->insn_code |= SET_IW_CUSTOM_B (reg->index); 2020 if (reg->regtype & REG_COPROCESSOR) 2021 insn->insn_code |= SET_IW_CUSTOM_READB (0); 2022 else 2023 insn->insn_code |= SET_IW_CUSTOM_READB (1); 2024 break; 2025 case iw_F2I16_type: 2026 insn->insn_code |= SET_IW_F2I16_B (reg->index); 2027 break; 2028 case iw_F2X4I12_type: 2029 insn->insn_code |= SET_IW_F2X4I12_B (reg->index); 2030 break; 2031 case iw_F3X6L5_type: 2032 case iw_F3X6_type: 2033 insn->insn_code |= SET_IW_F3X6L5_B (reg->index); 2034 break; 2035 case iw_F2X6L10_type: 2036 insn->insn_code |= SET_IW_F2X6L10_B (reg->index); 2037 break; 2038 case iw_F3X8_type: 2039 insn->insn_code |= SET_IW_F3X8_B (reg->index); 2040 if (reg->regtype & REG_COPROCESSOR) 2041 insn->insn_code |= SET_IW_F3X8_READB (0); 2042 else 2043 insn->insn_code |= SET_IW_F3X8_READB (1); 2044 break; 2045 case iw_F1I5_type: 2046 insn->insn_code |= SET_IW_F1I5_B (reg->index); 2047 break; 2048 case iw_F2_type: 2049 insn->insn_code |= SET_IW_F2_B (reg->index); 2050 break; 2051 case iw_T1X1I6_type: 2052 /* Implicit zero register reference. */ 2053 if (reg->index != 0) 2054 as_bad (_("invalid register %s"), token); 2055 break; 2056 2057 default: 2058 bad_opcode (op); 2059 } 2060 } 2061 2062 /* Destination register w/3-bit encoding. */ 2063 static void 2064 nios2_assemble_arg_D (const char *token, nios2_insn_infoS *insn) 2065 { 2066 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2067 int reg = nios2_assemble_reg3 (token); 2068 2069 switch (op->format) 2070 { 2071 case iw_T1I7_type: 2072 insn->insn_code |= SET_IW_T1I7_A3 (reg); 2073 break; 2074 case iw_T2X1L3_type: 2075 insn->insn_code |= SET_IW_T2X1L3_B3 (reg); 2076 break; 2077 case iw_T2X1I3_type: 2078 insn->insn_code |= SET_IW_T2X1I3_B3 (reg); 2079 break; 2080 case iw_T3X1_type: 2081 insn->insn_code |= SET_IW_T3X1_C3 (reg); 2082 break; 2083 case iw_T2X3_type: 2084 /* Some instructions using this encoding take 3 register arguments, 2085 requiring the destination register to be the same as the first 2086 source register. */ 2087 if (op->num_args == 3) 2088 insn->insn_code |= SET_IW_T2X3_A3 (reg); 2089 else 2090 insn->insn_code |= SET_IW_T2X3_B3 (reg); 2091 break; 2092 default: 2093 bad_opcode (op); 2094 } 2095 } 2096 2097 /* Source register w/3-bit encoding. */ 2098 static void 2099 nios2_assemble_arg_S (const char *token, nios2_insn_infoS *insn) 2100 { 2101 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2102 int reg = nios2_assemble_reg3 (token); 2103 2104 switch (op->format) 2105 { 2106 case iw_T1I7_type: 2107 insn->insn_code |= SET_IW_T1I7_A3 (reg); 2108 break; 2109 case iw_T2I4_type: 2110 insn->insn_code |= SET_IW_T2I4_A3 (reg); 2111 break; 2112 case iw_T2X1L3_type: 2113 insn->insn_code |= SET_IW_T2X1L3_A3 (reg); 2114 break; 2115 case iw_T2X1I3_type: 2116 insn->insn_code |= SET_IW_T2X1I3_A3 (reg); 2117 break; 2118 case iw_T3X1_type: 2119 insn->insn_code |= SET_IW_T3X1_A3 (reg); 2120 break; 2121 case iw_T2X3_type: 2122 /* Some instructions using this encoding take 3 register arguments, 2123 requiring the destination register to be the same as the first 2124 source register. */ 2125 if (op->num_args == 3) 2126 { 2127 int dreg = GET_IW_T2X3_A3 (insn->insn_code); 2128 if (dreg != reg) 2129 as_bad ("source and destination registers must be the same"); 2130 } 2131 else 2132 insn->insn_code |= SET_IW_T2X3_A3 (reg); 2133 break; 2134 case iw_T1X1I6_type: 2135 insn->insn_code |= SET_IW_T1X1I6_A3 (reg); 2136 break; 2137 default: 2138 bad_opcode (op); 2139 } 2140 } 2141 2142 /* Source register 2 w/3-bit encoding. */ 2143 static void 2144 nios2_assemble_arg_T (const char *token, nios2_insn_infoS *insn) 2145 { 2146 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2147 int reg = nios2_assemble_reg3 (token); 2148 2149 switch (op->format) 2150 { 2151 case iw_T2I4_type: 2152 insn->insn_code |= SET_IW_T2I4_B3 (reg); 2153 break; 2154 case iw_T3X1_type: 2155 insn->insn_code |= SET_IW_T3X1_B3 (reg); 2156 break; 2157 case iw_T2X3_type: 2158 insn->insn_code |= SET_IW_T2X3_B3 (reg); 2159 break; 2160 default: 2161 bad_opcode (op); 2162 } 2163 } 2164 2165 /* 16-bit signed immediate. */ 2166 static void 2167 nios2_assemble_arg_i (const char *token, nios2_insn_infoS *insn) 2168 { 2169 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2170 unsigned int val; 2171 2172 switch (op->format) 2173 { 2174 case iw_i_type: 2175 val = nios2_assemble_expression (token, insn, 2176 BFD_RELOC_NIOS2_S16, 0); 2177 insn->constant_bits |= SET_IW_I_IMM16 (val); 2178 break; 2179 case iw_F2I16_type: 2180 val = nios2_assemble_expression (token, insn, 2181 BFD_RELOC_NIOS2_S16, 0); 2182 insn->constant_bits |= SET_IW_F2I16_IMM16 (val); 2183 break; 2184 default: 2185 bad_opcode (op); 2186 } 2187 } 2188 2189 /* 12-bit signed immediate. */ 2190 static void 2191 nios2_assemble_arg_I (const char *token, nios2_insn_infoS *insn) 2192 { 2193 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2194 unsigned int val; 2195 2196 switch (op->format) 2197 { 2198 case iw_F2X4I12_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 case iw_F1X4I12_type: 2204 val = nios2_assemble_expression (token, insn, 2205 BFD_RELOC_NIOS2_R2_S12, 0); 2206 insn->constant_bits |= SET_IW_F2X4I12_IMM12 (val); 2207 break; 2208 default: 2209 bad_opcode (op); 2210 } 2211 } 2212 2213 /* 16-bit unsigned immediate. */ 2214 static void 2215 nios2_assemble_arg_u (const char *token, nios2_insn_infoS *insn) 2216 { 2217 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2218 unsigned int val; 2219 2220 switch (op->format) 2221 { 2222 case iw_i_type: 2223 val = nios2_assemble_expression (token, insn, 2224 BFD_RELOC_NIOS2_U16, 0); 2225 insn->constant_bits |= SET_IW_I_IMM16 (val); 2226 break; 2227 case iw_F2I16_type: 2228 val = nios2_assemble_expression (token, insn, 2229 BFD_RELOC_NIOS2_U16, 0); 2230 insn->constant_bits |= SET_IW_F2I16_IMM16 (val); 2231 break; 2232 default: 2233 bad_opcode (op); 2234 } 2235 } 2236 2237 /* 7-bit unsigned immediate with 2-bit shift. */ 2238 static void 2239 nios2_assemble_arg_U (const char *token, nios2_insn_infoS *insn) 2240 { 2241 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2242 unsigned int val; 2243 2244 switch (op->format) 2245 { 2246 case iw_T1I7_type: 2247 val = nios2_assemble_expression (token, insn, 2248 BFD_RELOC_NIOS2_R2_T1I7_2, 0); 2249 insn->constant_bits |= SET_IW_T1I7_IMM7 (val >> 2); 2250 break; 2251 case iw_X1I7_type: 2252 val = nios2_assemble_expression (token, insn, 2253 BFD_RELOC_NIOS2_R2_X1I7_2, 0); 2254 insn->constant_bits |= SET_IW_X1I7_IMM7 (val >> 2); 2255 break; 2256 default: 2257 bad_opcode (op); 2258 } 2259 } 2260 2261 /* 5-bit unsigned immediate with 2-bit shift. */ 2262 static void 2263 nios2_assemble_arg_V (const char *token, nios2_insn_infoS *insn) 2264 { 2265 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2266 unsigned int val; 2267 2268 switch (op->format) 2269 { 2270 case iw_F1I5_type: 2271 val = nios2_assemble_expression (token, insn, 2272 BFD_RELOC_NIOS2_R2_F1I5_2, 0); 2273 insn->constant_bits |= SET_IW_F1I5_IMM5 (val >> 2); 2274 break; 2275 default: 2276 bad_opcode (op); 2277 } 2278 } 2279 2280 /* 4-bit unsigned immediate with 2-bit shift. */ 2281 static void 2282 nios2_assemble_arg_W (const char *token, nios2_insn_infoS *insn) 2283 { 2284 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2285 unsigned int val; 2286 2287 switch (op->format) 2288 { 2289 case iw_T2I4_type: 2290 val = nios2_assemble_expression (token, insn, 2291 BFD_RELOC_NIOS2_R2_T2I4_2, 0); 2292 insn->constant_bits |= SET_IW_T2I4_IMM4 (val >> 2); 2293 break; 2294 case iw_L5I4X1_type: 2295 /* This argument is optional for push.n/pop.n, and defaults to 2296 zero if unspecified. */ 2297 if (token == NULL) 2298 return; 2299 2300 val = nios2_assemble_expression (token, insn, 2301 BFD_RELOC_NIOS2_R2_L5I4X1, 0); 2302 insn->constant_bits |= SET_IW_L5I4X1_IMM4 (val >> 2); 2303 break; 2304 default: 2305 bad_opcode (op); 2306 } 2307 } 2308 2309 /* 4-bit unsigned immediate with 1-bit shift. */ 2310 static void 2311 nios2_assemble_arg_X (const char *token, nios2_insn_infoS *insn) 2312 { 2313 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2314 unsigned int val; 2315 2316 switch (op->format) 2317 { 2318 case iw_T2I4_type: 2319 val = nios2_assemble_expression (token, insn, 2320 BFD_RELOC_NIOS2_R2_T2I4_1, 0); 2321 insn->constant_bits |= SET_IW_T2I4_IMM4 (val >> 1); 2322 break; 2323 default: 2324 bad_opcode (op); 2325 } 2326 } 2327 2328 /* 4-bit unsigned immediate without shift. */ 2329 static void 2330 nios2_assemble_arg_Y (const char *token, nios2_insn_infoS *insn) 2331 { 2332 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2333 unsigned int val; 2334 2335 switch (op->format) 2336 { 2337 case iw_T2I4_type: 2338 val = nios2_assemble_expression (token, insn, 2339 BFD_RELOC_NIOS2_R2_T2I4, 0); 2340 insn->constant_bits |= SET_IW_T2I4_IMM4 (val); 2341 break; 2342 default: 2343 bad_opcode (op); 2344 } 2345 } 2346 2347 2348 /* 16-bit signed immediate address offset. */ 2349 static void 2350 nios2_assemble_arg_o (const char *token, nios2_insn_infoS *insn) 2351 { 2352 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2353 unsigned int val; 2354 2355 switch (op->format) 2356 { 2357 case iw_i_type: 2358 val = nios2_assemble_expression (token, insn, 2359 BFD_RELOC_16_PCREL, 1); 2360 insn->constant_bits |= SET_IW_I_IMM16 (val); 2361 break; 2362 case iw_F2I16_type: 2363 val = nios2_assemble_expression (token, insn, 2364 BFD_RELOC_16_PCREL, 1); 2365 insn->constant_bits |= SET_IW_F2I16_IMM16 (val); 2366 break; 2367 default: 2368 bad_opcode (op); 2369 } 2370 } 2371 2372 /* 10-bit signed address offset with 1-bit shift. */ 2373 static void 2374 nios2_assemble_arg_O (const char *token, nios2_insn_infoS *insn) 2375 { 2376 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2377 unsigned int val; 2378 2379 switch (op->format) 2380 { 2381 case iw_I10_type: 2382 val = nios2_assemble_expression (token, insn, 2383 BFD_RELOC_NIOS2_R2_I10_1_PCREL, 1); 2384 insn->constant_bits |= SET_IW_I10_IMM10 (val >> 1); 2385 break; 2386 default: 2387 bad_opcode (op); 2388 } 2389 } 2390 2391 /* 7-bit signed address offset with 1-bit shift. */ 2392 static void 2393 nios2_assemble_arg_P (const char *token, nios2_insn_infoS *insn) 2394 { 2395 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2396 unsigned int val; 2397 2398 switch (op->format) 2399 { 2400 case iw_T1I7_type: 2401 val = nios2_assemble_expression (token, insn, 2402 BFD_RELOC_NIOS2_R2_T1I7_1_PCREL, 1); 2403 insn->constant_bits |= SET_IW_T1I7_IMM7 (val >> 1); 2404 break; 2405 default: 2406 bad_opcode (op); 2407 } 2408 } 2409 2410 /* 5-bit unsigned immediate. */ 2411 static void 2412 nios2_assemble_arg_j (const char *token, nios2_insn_infoS *insn) 2413 { 2414 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2415 unsigned int val; 2416 2417 switch (op->format) 2418 { 2419 case iw_r_type: 2420 val = nios2_assemble_expression (token, insn, 2421 BFD_RELOC_NIOS2_IMM5, 0); 2422 insn->constant_bits |= SET_IW_R_IMM5 (val); 2423 break; 2424 case iw_F3X6L5_type: 2425 if (op->match == MATCH_R2_ENI) 2426 /* Value must be constant 0 or 1. */ 2427 { 2428 val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0); 2429 if (val != 0 && val != 1) 2430 as_bad ("invalid eni argument %u", val); 2431 insn->insn_code |= SET_IW_F3X6L5_IMM5 (val); 2432 } 2433 else 2434 { 2435 val = nios2_assemble_expression (token, insn, 2436 BFD_RELOC_NIOS2_IMM5, 0); 2437 insn->constant_bits |= SET_IW_F3X6L5_IMM5 (val); 2438 } 2439 break; 2440 case iw_F2X6L10_type: 2441 /* Only constant expression without relocation permitted for 2442 bit position. */ 2443 val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0); 2444 if (val > 31) 2445 as_bad ("invalid bit position %u", val); 2446 insn->insn_code |= SET_IW_F2X6L10_MSB (val); 2447 break; 2448 case iw_X2L5_type: 2449 val = nios2_assemble_expression (token, insn, 2450 BFD_RELOC_NIOS2_R2_X2L5, 0); 2451 insn->constant_bits |= SET_IW_X2L5_IMM5 (val); 2452 break; 2453 default: 2454 bad_opcode (op); 2455 } 2456 } 2457 2458 /* Second 5-bit unsigned immediate field. */ 2459 static void 2460 nios2_assemble_arg_k (const char *token, nios2_insn_infoS *insn) 2461 { 2462 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2463 unsigned int val; 2464 2465 switch (op->format) 2466 { 2467 case iw_F2X6L10_type: 2468 /* Only constant expression without relocation permitted for 2469 bit position. */ 2470 val = nios2_assemble_expression (token, insn, 2471 BFD_RELOC_NONE, 0); 2472 if (val > 31) 2473 as_bad ("invalid bit position %u", val); 2474 else if (GET_IW_F2X6L10_MSB (insn->insn_code) < val) 2475 as_bad ("MSB must be greater than or equal to LSB"); 2476 insn->insn_code |= SET_IW_F2X6L10_LSB (val); 2477 break; 2478 default: 2479 bad_opcode (op); 2480 } 2481 } 2482 2483 /* 8-bit unsigned immediate. */ 2484 static void 2485 nios2_assemble_arg_l (const char *token, nios2_insn_infoS *insn) 2486 { 2487 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2488 unsigned int val; 2489 2490 switch (op->format) 2491 { 2492 case iw_custom_type: 2493 val = nios2_assemble_expression (token, insn, 2494 BFD_RELOC_NIOS2_IMM8, 0); 2495 insn->constant_bits |= SET_IW_CUSTOM_N (val); 2496 break; 2497 case iw_F3X8_type: 2498 val = nios2_assemble_expression (token, insn, 2499 BFD_RELOC_NIOS2_IMM8, 0); 2500 insn->constant_bits |= SET_IW_F3X8_N (val); 2501 break; 2502 default: 2503 bad_opcode (op); 2504 } 2505 } 2506 2507 /* 26-bit unsigned immediate. */ 2508 static void 2509 nios2_assemble_arg_m (const char *token, nios2_insn_infoS *insn) 2510 { 2511 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2512 unsigned int val; 2513 2514 switch (op->format) 2515 { 2516 case iw_j_type: 2517 val = nios2_assemble_expression (token, insn, 2518 (nios2_as_options.noat 2519 ? BFD_RELOC_NIOS2_CALL26_NOAT 2520 : BFD_RELOC_NIOS2_CALL26), 2521 0); 2522 insn->constant_bits |= SET_IW_J_IMM26 (val); 2523 break; 2524 case iw_L26_type: 2525 val = nios2_assemble_expression (token, insn, 2526 (nios2_as_options.noat 2527 ? BFD_RELOC_NIOS2_CALL26_NOAT 2528 : BFD_RELOC_NIOS2_CALL26), 2529 0); 2530 insn->constant_bits |= SET_IW_L26_IMM26 (val); 2531 break; 2532 default: 2533 bad_opcode (op); 2534 } 2535 } 2536 2537 /* 6-bit unsigned immediate with no shifting. */ 2538 static void 2539 nios2_assemble_arg_M (const char *token, nios2_insn_infoS *insn) 2540 { 2541 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2542 unsigned int val; 2543 2544 switch (op->format) 2545 { 2546 case iw_T1X1I6_type: 2547 val = nios2_assemble_expression (token, insn, 2548 BFD_RELOC_NIOS2_R2_T1X1I6, 0); 2549 insn->constant_bits |= SET_IW_T1X1I6_IMM6 (val); 2550 break; 2551 default: 2552 bad_opcode (op); 2553 } 2554 } 2555 2556 /* 6-bit unsigned immediate with 2-bit shift. */ 2557 static void 2558 nios2_assemble_arg_N (const char *token, nios2_insn_infoS *insn) 2559 { 2560 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2561 unsigned int val; 2562 2563 switch (op->format) 2564 { 2565 case iw_T1X1I6_type: 2566 val = nios2_assemble_expression (token, insn, 2567 BFD_RELOC_NIOS2_R2_T1X1I6_2, 0); 2568 insn->constant_bits |= SET_IW_T1X1I6_IMM6 (val >> 2); 2569 break; 2570 default: 2571 bad_opcode (op); 2572 } 2573 } 2574 2575 2576 /* Encoded enumeration for addi.n/subi.n. */ 2577 static void 2578 nios2_assemble_arg_e (const char *token, nios2_insn_infoS *insn) 2579 { 2580 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2581 unsigned int val; 2582 int i; 2583 2584 switch (op->format) 2585 { 2586 case iw_T2X1I3_type: 2587 val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0); 2588 for (i = 0; i < nios2_num_r2_asi_n_mappings; i++) 2589 if (val == nios2_r2_asi_n_mappings[i]) 2590 break; 2591 if (i == nios2_num_r2_asi_n_mappings) 2592 { 2593 as_bad (_("Invalid constant operand %s"), token); 2594 return; 2595 } 2596 insn->insn_code |= SET_IW_T2X1I3_IMM3 ((unsigned)i); 2597 break; 2598 default: 2599 bad_opcode (op); 2600 } 2601 } 2602 2603 /* Encoded enumeration for slli.n/srli.n. */ 2604 static void 2605 nios2_assemble_arg_f (const char *token, nios2_insn_infoS *insn) 2606 { 2607 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2608 unsigned int val; 2609 int i; 2610 2611 switch (op->format) 2612 { 2613 case iw_T2X1L3_type: 2614 val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0); 2615 for (i = 0; i < nios2_num_r2_shi_n_mappings; i++) 2616 if (val == nios2_r2_shi_n_mappings[i]) 2617 break; 2618 if (i == nios2_num_r2_shi_n_mappings) 2619 { 2620 as_bad (_("Invalid constant operand %s"), token); 2621 return; 2622 } 2623 insn->insn_code |= SET_IW_T2X1L3_SHAMT ((unsigned)i); 2624 break; 2625 default: 2626 bad_opcode (op); 2627 } 2628 } 2629 2630 /* Encoded enumeration for andi.n. */ 2631 static void 2632 nios2_assemble_arg_g (const char *token, nios2_insn_infoS *insn) 2633 { 2634 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2635 unsigned int val; 2636 int i; 2637 2638 switch (op->format) 2639 { 2640 case iw_T2I4_type: 2641 val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0); 2642 for (i = 0; i < nios2_num_r2_andi_n_mappings; i++) 2643 if (val == nios2_r2_andi_n_mappings[i]) 2644 break; 2645 if (i == nios2_num_r2_andi_n_mappings) 2646 { 2647 as_bad (_("Invalid constant operand %s"), token); 2648 return; 2649 } 2650 insn->insn_code |= SET_IW_T2I4_IMM4 ((unsigned)i); 2651 break; 2652 default: 2653 bad_opcode (op); 2654 } 2655 } 2656 2657 /* Encoded enumeration for movi.n. */ 2658 static void 2659 nios2_assemble_arg_h (const char *token, nios2_insn_infoS *insn) 2660 { 2661 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2662 unsigned int val; 2663 int i; 2664 2665 switch (op->format) 2666 { 2667 case iw_T1I7_type: 2668 val = nios2_assemble_expression (token, insn, BFD_RELOC_NONE, 0); 2669 i = (signed) val; 2670 if ((signed) i == -1) 2671 val = 127; 2672 else if (i == -2) 2673 val = 126; 2674 else if (i == 0xff) 2675 val = 125; 2676 else if (i < 0 || i > 125) 2677 { 2678 as_bad (_("Invalid constant operand %s"), token); 2679 return; 2680 } 2681 insn->insn_code |= SET_IW_T1I7_IMM7 (val); 2682 break; 2683 default: 2684 bad_opcode (op); 2685 } 2686 } 2687 2688 /* Encoded REGMASK for ldwm/stwm or push.n/pop.n. */ 2689 static void 2690 nios2_assemble_arg_R (const char *token, nios2_insn_infoS *insn) 2691 { 2692 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2693 unsigned long mask; 2694 char *buf = strdup (token); 2695 unsigned long reglist = nios2_parse_reglist (buf, op); 2696 free (buf); 2697 2698 if (reglist == 0) 2699 return; 2700 2701 switch (op->format) 2702 { 2703 case iw_F1X4L17_type: 2704 /* Encoding for ldwm/stwm. */ 2705 if (reglist & 0x00003ffc) 2706 mask = reglist >> 2; 2707 else 2708 { 2709 insn->insn_code |= SET_IW_F1X4L17_RS (1); 2710 mask = (reglist & 0x00ffc000) >> 14; 2711 if (reglist & (1 << 28)) 2712 mask |= 1 << 10; 2713 if (reglist & (1 << 31)) 2714 mask |= 1 << 11; 2715 } 2716 insn->insn_code |= SET_IW_F1X4L17_REGMASK (mask); 2717 break; 2718 2719 case iw_L5I4X1_type: 2720 /* Encoding for push.n/pop.n. */ 2721 if (reglist & (1 << 28)) 2722 insn->insn_code |= SET_IW_L5I4X1_FP (1); 2723 mask = reglist & 0x00ff0000; 2724 if (mask) 2725 { 2726 int i; 2727 2728 for (i = 0; i < nios2_num_r2_reg_range_mappings; i++) 2729 if (nios2_r2_reg_range_mappings[i] == mask) 2730 break; 2731 if (i == nios2_num_r2_reg_range_mappings) 2732 { 2733 as_bad ("invalid reglist"); 2734 return; 2735 } 2736 insn->insn_code |= SET_IW_L5I4X1_REGRANGE (i); 2737 insn->insn_code |= SET_IW_L5I4X1_CS (1); 2738 } 2739 break; 2740 2741 default: 2742 bad_opcode (op); 2743 } 2744 } 2745 2746 /* Base register for ldwm/stwm. */ 2747 static void 2748 nios2_assemble_arg_B (const char *token, nios2_insn_infoS *insn) 2749 { 2750 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2751 int direction, writeback, ret; 2752 char *str = strdup (token); 2753 struct nios2_reg *reg 2754 = nios2_parse_base_register (str, &direction, &writeback, &ret); 2755 2756 free (str); 2757 if (!reg) 2758 return; 2759 2760 switch (op->format) 2761 { 2762 case iw_F1X4L17_type: 2763 /* For ldwm, check to see if the base register is already inside the 2764 register list. */ 2765 if (op->match == MATCH_R2_LDWM 2766 && (nios2_reglist_mask & (1 << reg->index))) 2767 { 2768 as_bad ("invalid base register; %s is inside the reglist", reg->name); 2769 return; 2770 } 2771 2772 /* For stwm, ret option is not allowed. */ 2773 if (op->match == MATCH_R2_STWM && ret) 2774 { 2775 as_bad ("invalid option syntax"); 2776 return; 2777 } 2778 2779 /* Check that the direction matches the ordering of the reglist. */ 2780 if (nios2_reglist_dir && direction != nios2_reglist_dir) 2781 { 2782 as_bad ("reglist order does not match increment/decrement mode"); 2783 return; 2784 } 2785 2786 insn->insn_code |= SET_IW_F1X4L17_A (reg->index); 2787 if (direction > 0) 2788 insn->insn_code |= SET_IW_F1X4L17_ID (1); 2789 if (writeback) 2790 insn->insn_code |= SET_IW_F1X4L17_WB (1); 2791 if (ret) 2792 insn->insn_code |= SET_IW_F1X4L17_PC (1); 2793 break; 2794 2795 default: 2796 bad_opcode (op); 2797 } 2798 } 2799 2800 static void 2801 nios2_assemble_args (nios2_insn_infoS *insn) 2802 { 2803 const struct nios2_opcode *op = insn->insn_nios2_opcode; 2804 const char *argptr; 2805 unsigned int tokidx, ntok; 2806 2807 /* Make sure there are enough arguments. */ 2808 ntok = (op->pinfo & NIOS2_INSN_OPTARG) ? op->num_args - 1 : op->num_args; 2809 for (tokidx = 1; tokidx <= ntok; tokidx++) 2810 if (insn->insn_tokens[tokidx] == NULL) 2811 { 2812 as_bad ("missing argument"); 2813 return; 2814 } 2815 2816 for (argptr = op->args, tokidx = 1; 2817 *argptr && insn->insn_tokens[tokidx]; 2818 argptr++) 2819 switch (*argptr) 2820 { 2821 case ',': 2822 case '(': 2823 case ')': 2824 break; 2825 2826 case 'c': 2827 nios2_assemble_arg_c (insn->insn_tokens[tokidx++], insn); 2828 break; 2829 2830 case 'd': 2831 nios2_assemble_arg_d (insn->insn_tokens[tokidx++], insn); 2832 break; 2833 2834 case 's': 2835 nios2_assemble_arg_s (insn->insn_tokens[tokidx++], insn); 2836 break; 2837 2838 case 't': 2839 nios2_assemble_arg_t (insn->insn_tokens[tokidx++], insn); 2840 break; 2841 2842 case 'D': 2843 nios2_assemble_arg_D (insn->insn_tokens[tokidx++], insn); 2844 break; 2845 2846 case 'S': 2847 nios2_assemble_arg_S (insn->insn_tokens[tokidx++], insn); 2848 break; 2849 2850 case 'T': 2851 nios2_assemble_arg_T (insn->insn_tokens[tokidx++], insn); 2852 break; 2853 2854 case 'i': 2855 nios2_assemble_arg_i (insn->insn_tokens[tokidx++], insn); 2856 break; 2857 2858 case 'I': 2859 nios2_assemble_arg_I (insn->insn_tokens[tokidx++], insn); 2860 break; 2861 2862 case 'u': 2863 nios2_assemble_arg_u (insn->insn_tokens[tokidx++], insn); 2864 break; 2865 2866 case 'U': 2867 nios2_assemble_arg_U (insn->insn_tokens[tokidx++], insn); 2868 break; 2869 2870 case 'V': 2871 nios2_assemble_arg_V (insn->insn_tokens[tokidx++], insn); 2872 break; 2873 2874 case 'W': 2875 nios2_assemble_arg_W (insn->insn_tokens[tokidx++], insn); 2876 break; 2877 2878 case 'X': 2879 nios2_assemble_arg_X (insn->insn_tokens[tokidx++], insn); 2880 break; 2881 2882 case 'Y': 2883 nios2_assemble_arg_Y (insn->insn_tokens[tokidx++], insn); 2884 break; 2885 2886 case 'o': 2887 nios2_assemble_arg_o (insn->insn_tokens[tokidx++], insn); 2888 break; 2889 2890 case 'O': 2891 nios2_assemble_arg_O (insn->insn_tokens[tokidx++], insn); 2892 break; 2893 2894 case 'P': 2895 nios2_assemble_arg_P (insn->insn_tokens[tokidx++], insn); 2896 break; 2897 2898 case 'j': 2899 nios2_assemble_arg_j (insn->insn_tokens[tokidx++], insn); 2900 break; 2901 2902 case 'k': 2903 nios2_assemble_arg_k (insn->insn_tokens[tokidx++], insn); 2904 break; 2905 2906 case 'l': 2907 nios2_assemble_arg_l (insn->insn_tokens[tokidx++], insn); 2908 break; 2909 2910 case 'm': 2911 nios2_assemble_arg_m (insn->insn_tokens[tokidx++], insn); 2912 break; 2913 2914 case 'M': 2915 nios2_assemble_arg_M (insn->insn_tokens[tokidx++], insn); 2916 break; 2917 2918 case 'N': 2919 nios2_assemble_arg_N (insn->insn_tokens[tokidx++], insn); 2920 break; 2921 2922 case 'e': 2923 nios2_assemble_arg_e (insn->insn_tokens[tokidx++], insn); 2924 break; 2925 2926 case 'f': 2927 nios2_assemble_arg_f (insn->insn_tokens[tokidx++], insn); 2928 break; 2929 2930 case 'g': 2931 nios2_assemble_arg_g (insn->insn_tokens[tokidx++], insn); 2932 break; 2933 2934 case 'h': 2935 nios2_assemble_arg_h (insn->insn_tokens[tokidx++], insn); 2936 break; 2937 2938 case 'R': 2939 nios2_assemble_arg_R (insn->insn_tokens[tokidx++], insn); 2940 break; 2941 2942 case 'B': 2943 nios2_assemble_arg_B (insn->insn_tokens[tokidx++], insn); 2944 break; 2945 2946 default: 2947 bad_opcode (op); 2948 break; 2949 } 2950 2951 /* Perform argument checking. */ 2952 nios2_check_assembly (insn->insn_code | insn->constant_bits, 2953 insn->insn_tokens[tokidx]); 2954 } 2955 2956 2957 /* The function consume_arg takes a pointer into a string 2958 of instruction tokens (args) and a pointer into a string 2959 representing the expected sequence of tokens and separators. 2960 It checks whether the first argument in argstr is of the 2961 expected type, throwing an error if it is not, and returns 2962 the pointer argstr. */ 2963 static char * 2964 nios2_consume_arg (char *argstr, const char *parsestr) 2965 { 2966 char *temp; 2967 2968 switch (*parsestr) 2969 { 2970 case 'c': 2971 case 'd': 2972 case 's': 2973 case 't': 2974 case 'D': 2975 case 'S': 2976 case 'T': 2977 break; 2978 2979 case 'i': 2980 case 'u': 2981 if (*argstr == '%') 2982 { 2983 if (nios2_special_relocation_p (argstr)) 2984 { 2985 /* We zap the parentheses because we don't want them confused 2986 with separators. */ 2987 temp = strchr (argstr, '('); 2988 if (temp != NULL) 2989 *temp = ' '; 2990 temp = strchr (argstr, ')'); 2991 if (temp != NULL) 2992 *temp = ' '; 2993 } 2994 else 2995 as_bad (_("badly formed expression near %s"), argstr); 2996 } 2997 break; 2998 case 'm': 2999 case 'j': 3000 case 'k': 3001 case 'l': 3002 case 'I': 3003 case 'U': 3004 case 'V': 3005 case 'W': 3006 case 'X': 3007 case 'Y': 3008 case 'O': 3009 case 'P': 3010 case 'e': 3011 case 'f': 3012 case 'g': 3013 case 'h': 3014 case 'M': 3015 case 'N': 3016 3017 /* We can't have %hi, %lo or %hiadj here. */ 3018 if (*argstr == '%') 3019 as_bad (_("badly formed expression near %s"), argstr); 3020 break; 3021 3022 case 'R': 3023 /* Register list for ldwm/stwm or push.n/pop.n. Replace the commas 3024 in the list with spaces so we don't confuse them with separators. */ 3025 if (*argstr != '{') 3026 { 3027 as_bad ("missing '{' in register list"); 3028 break; 3029 } 3030 for (temp = argstr + 1; *temp; temp++) 3031 { 3032 if (*temp == '}') 3033 break; 3034 else if (*temp == ',') 3035 *temp = ' '; 3036 } 3037 if (!*temp) 3038 { 3039 as_bad ("missing '}' in register list"); 3040 break; 3041 } 3042 break; 3043 3044 case 'B': 3045 /* Base register and options for ldwm/stwm. This is the final argument 3046 and consumes the rest of the argument string; replace commas 3047 with spaces so that the token splitter doesn't think they are 3048 separate arguments. */ 3049 for (temp = argstr; *temp; temp++) 3050 if (*temp == ',') 3051 *temp = ' '; 3052 break; 3053 3054 case 'o': 3055 case 'E': 3056 break; 3057 default: 3058 BAD_CASE (*parsestr); 3059 break; 3060 } 3061 3062 return argstr; 3063 } 3064 3065 /* The function consume_separator takes a pointer into a string 3066 of instruction tokens (args) and a pointer into a string representing 3067 the expected sequence of tokens and separators. It finds the first 3068 instance of the character pointed to by separator in argstr, and 3069 returns a pointer to the next element of argstr, which is the 3070 following token in the sequence. */ 3071 static char * 3072 nios2_consume_separator (char *argstr, const char *separator) 3073 { 3074 char *p; 3075 3076 /* If we have a opcode reg, expr(reg) type instruction, and 3077 * we are separating the expr from the (reg), we find the last 3078 * (, just in case the expression has parentheses. */ 3079 3080 if (*separator == '(') 3081 p = strrchr (argstr, *separator); 3082 else 3083 p = strchr (argstr, *separator); 3084 3085 if (p != NULL) 3086 *p++ = 0; 3087 return p; 3088 } 3089 3090 /* The principal argument parsing function which takes a string argstr 3091 representing the instruction arguments for insn, and extracts the argument 3092 tokens matching parsestr into parsed_args. */ 3093 static void 3094 nios2_parse_args (nios2_insn_infoS *insn, char *argstr, 3095 const char *parsestr, char **parsed_args) 3096 { 3097 char *p; 3098 char *end = NULL; 3099 int i; 3100 p = argstr; 3101 i = 0; 3102 bfd_boolean terminate = FALSE; 3103 3104 /* This rest of this function is it too fragile and it mostly works, 3105 therefore special case this one. */ 3106 if (*parsestr == 0 && argstr != 0) 3107 { 3108 as_bad (_("too many arguments")); 3109 parsed_args[0] = NULL; 3110 return; 3111 } 3112 3113 while (p != NULL && !terminate && i < NIOS2_MAX_INSN_TOKENS) 3114 { 3115 parsed_args[i] = nios2_consume_arg (p, parsestr); 3116 ++parsestr; 3117 while (*parsestr == '(' || *parsestr == ')' || *parsestr == ',') 3118 { 3119 char *context = p; 3120 p = nios2_consume_separator (p, parsestr); 3121 /* Check for missing separators. */ 3122 if (!p && !(insn->insn_nios2_opcode->pinfo & NIOS2_INSN_OPTARG)) 3123 { 3124 as_bad (_("expecting %c near %s"), *parsestr, context); 3125 break; 3126 } 3127 ++parsestr; 3128 } 3129 3130 if (*parsestr == '\0') 3131 { 3132 /* Check that the argument string has no trailing arguments. */ 3133 end = strpbrk (p, ","); 3134 if (end != NULL) 3135 as_bad (_("too many arguments")); 3136 } 3137 3138 if (*parsestr == '\0' || (p != NULL && *p == '\0')) 3139 terminate = TRUE; 3140 ++i; 3141 } 3142 3143 parsed_args[i] = NULL; 3144 } 3145 3146 3147 3148 /** Support for pseudo-op parsing. These are macro-like opcodes that 3149 expand into real insns by suitable fiddling with the operands. */ 3150 3151 /* Append the string modifier to the string contained in the argument at 3152 parsed_args[ndx]. */ 3153 static void 3154 nios2_modify_arg (char **parsed_args, const char *modifier, 3155 int unused ATTRIBUTE_UNUSED, int ndx) 3156 { 3157 char *tmp = parsed_args[ndx]; 3158 3159 parsed_args[ndx] = concat (tmp, modifier, (char *) NULL); 3160 } 3161 3162 /* Modify parsed_args[ndx] by negating that argument. */ 3163 static void 3164 nios2_negate_arg (char **parsed_args, const char *modifier ATTRIBUTE_UNUSED, 3165 int unused ATTRIBUTE_UNUSED, int ndx) 3166 { 3167 char *tmp = parsed_args[ndx]; 3168 3169 parsed_args[ndx] = concat ("~(", tmp, ")+1", (char *) NULL); 3170 } 3171 3172 /* The function nios2_swap_args swaps the pointers at indices index_1 and 3173 index_2 in the array parsed_args[] - this is used for operand swapping 3174 for comparison operations. */ 3175 static void 3176 nios2_swap_args (char **parsed_args, const char *unused ATTRIBUTE_UNUSED, 3177 int index_1, int index_2) 3178 { 3179 char *tmp; 3180 gas_assert (index_1 < NIOS2_MAX_INSN_TOKENS 3181 && index_2 < NIOS2_MAX_INSN_TOKENS); 3182 tmp = parsed_args[index_1]; 3183 parsed_args[index_1] = parsed_args[index_2]; 3184 parsed_args[index_2] = tmp; 3185 } 3186 3187 /* This function appends the string appnd to the array of strings in 3188 parsed_args num times starting at index start in the array. */ 3189 static void 3190 nios2_append_arg (char **parsed_args, const char *appnd, int num, 3191 int start) 3192 { 3193 int i, count; 3194 char *tmp; 3195 3196 gas_assert ((start + num) < NIOS2_MAX_INSN_TOKENS); 3197 3198 if (nios2_mode == NIOS2_MODE_TEST) 3199 tmp = parsed_args[start]; 3200 else 3201 tmp = NULL; 3202 3203 for (i = start, count = num; count > 0; ++i, --count) 3204 parsed_args[i] = (char *) appnd; 3205 3206 gas_assert (i == (start + num)); 3207 parsed_args[i] = tmp; 3208 parsed_args[i + 1] = NULL; 3209 } 3210 3211 /* This function inserts the string insert num times in the array 3212 parsed_args, starting at the index start. */ 3213 static void 3214 nios2_insert_arg (char **parsed_args, const char *insert, int num, 3215 int start) 3216 { 3217 int i, count; 3218 3219 gas_assert ((start + num) < NIOS2_MAX_INSN_TOKENS); 3220 3221 /* Move the existing arguments up to create space. */ 3222 for (i = NIOS2_MAX_INSN_TOKENS; i - num >= start; --i) 3223 parsed_args[i] = parsed_args[i - num]; 3224 3225 for (i = start, count = num; count > 0; ++i, --count) 3226 parsed_args[i] = (char *) insert; 3227 } 3228 3229 /* Cleanup function to free malloc'ed arg strings. */ 3230 static void 3231 nios2_free_arg (char **parsed_args, int num ATTRIBUTE_UNUSED, int start) 3232 { 3233 if (parsed_args[start]) 3234 { 3235 free (parsed_args[start]); 3236 parsed_args[start] = NULL; 3237 } 3238 } 3239 3240 /* This function swaps the pseudo-op for a real op. */ 3241 static nios2_ps_insn_infoS* 3242 nios2_translate_pseudo_insn (nios2_insn_infoS *insn) 3243 { 3244 3245 const struct nios2_opcode *op = insn->insn_nios2_opcode; 3246 nios2_ps_insn_infoS *ps_insn; 3247 unsigned int tokidx, ntok; 3248 3249 /* Find which real insn the pseudo-op translates to and 3250 switch the insn_info ptr to point to it. */ 3251 ps_insn = nios2_ps_lookup (op->name); 3252 3253 if (ps_insn != NULL) 3254 { 3255 insn->insn_nios2_opcode = nios2_opcode_lookup (ps_insn->insn); 3256 insn->insn_tokens[0] = insn->insn_nios2_opcode->name; 3257 3258 /* Make sure there are enough arguments. */ 3259 ntok = ((op->pinfo & NIOS2_INSN_OPTARG) 3260 ? op->num_args - 1 : op->num_args); 3261 for (tokidx = 1; tokidx <= ntok; tokidx++) 3262 if (insn->insn_tokens[tokidx] == NULL) 3263 { 3264 as_bad ("missing argument"); 3265 return NULL; 3266 } 3267 3268 /* Modify the args so they work with the real insn. */ 3269 ps_insn->arg_modifer_func ((char **) insn->insn_tokens, 3270 ps_insn->arg_modifier, ps_insn->num, 3271 ps_insn->index); 3272 } 3273 else 3274 /* we cannot recover from this. */ 3275 as_fatal (_("unrecognized pseudo-instruction %s"), 3276 insn->insn_nios2_opcode->name); 3277 return ps_insn; 3278 } 3279 3280 /* Invoke the cleanup handler for pseudo-insn ps_insn on insn. */ 3281 static void 3282 nios2_cleanup_pseudo_insn (nios2_insn_infoS *insn, 3283 nios2_ps_insn_infoS *ps_insn) 3284 { 3285 if (ps_insn->arg_cleanup_func) 3286 (ps_insn->arg_cleanup_func) ((char **) insn->insn_tokens, 3287 ps_insn->num, ps_insn->index); 3288 } 3289 3290 const nios2_ps_insn_infoS nios2_ps_insn_info_structs[] = { 3291 /* pseudo-op, real-op, arg, arg_modifier_func, num, index, arg_cleanup_func */ 3292 {"mov", "add", nios2_append_arg, "zero", 1, 3, NULL}, 3293 {"movi", "addi", nios2_insert_arg, "zero", 1, 2, NULL}, 3294 {"movhi", "orhi", nios2_insert_arg, "zero", 1, 2, NULL}, 3295 {"movui", "ori", nios2_insert_arg, "zero", 1, 2, NULL}, 3296 {"movia", "orhi", nios2_insert_arg, "zero", 1, 2, NULL}, 3297 {"nop", "add", nios2_append_arg, "zero", 3, 1, NULL}, 3298 {"bgt", "blt", nios2_swap_args, "", 1, 2, NULL}, 3299 {"bgtu", "bltu", nios2_swap_args, "", 1, 2, NULL}, 3300 {"ble", "bge", nios2_swap_args, "", 1, 2, NULL}, 3301 {"bleu", "bgeu", nios2_swap_args, "", 1, 2, NULL}, 3302 {"cmpgt", "cmplt", nios2_swap_args, "", 2, 3, NULL}, 3303 {"cmpgtu", "cmpltu", nios2_swap_args, "", 2, 3, NULL}, 3304 {"cmple", "cmpge", nios2_swap_args, "", 2, 3, NULL}, 3305 {"cmpleu", "cmpgeu", nios2_swap_args, "", 2, 3, NULL}, 3306 {"cmpgti", "cmpgei", nios2_modify_arg, "+1", 0, 3, nios2_free_arg}, 3307 {"cmpgtui", "cmpgeui", nios2_modify_arg, "+1", 0, 3, nios2_free_arg}, 3308 {"cmplei", "cmplti", nios2_modify_arg, "+1", 0, 3, nios2_free_arg}, 3309 {"cmpleui", "cmpltui", nios2_modify_arg, "+1", 0, 3, nios2_free_arg}, 3310 {"subi", "addi", nios2_negate_arg, "", 0, 3, nios2_free_arg}, 3311 {"nop.n", "mov.n", nios2_append_arg, "zero", 2, 1, NULL} 3312 /* Add further pseudo-ops here. */ 3313 }; 3314 3315 #define NIOS2_NUM_PSEUDO_INSNS \ 3316 ((sizeof(nios2_ps_insn_info_structs)/ \ 3317 sizeof(nios2_ps_insn_info_structs[0]))) 3318 const int nios2_num_ps_insn_info_structs = NIOS2_NUM_PSEUDO_INSNS; 3319 3320 3321 /** Assembler output support. */ 3322 3323 /* Output a normal instruction. */ 3324 static void 3325 output_insn (nios2_insn_infoS *insn) 3326 { 3327 char *f; 3328 nios2_insn_relocS *reloc; 3329 f = frag_more (insn->insn_nios2_opcode->size); 3330 /* This allocates enough space for the instruction 3331 and puts it in the current frag. */ 3332 md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size); 3333 /* Emit debug info. */ 3334 dwarf2_emit_insn (insn->insn_nios2_opcode->size); 3335 /* Create any fixups to be acted on later. */ 3336 3337 for (reloc = insn->insn_reloc; reloc != NULL; reloc = reloc->reloc_next) 3338 fix_new_exp (frag_now, f - frag_now->fr_literal, 3339 insn->insn_nios2_opcode->size, 3340 &reloc->reloc_expression, reloc->reloc_pcrel, 3341 reloc->reloc_type); 3342 } 3343 3344 /* Output an unconditional branch. */ 3345 static void 3346 output_ubranch (nios2_insn_infoS *insn) 3347 { 3348 nios2_insn_relocS *reloc = insn->insn_reloc; 3349 3350 /* If the reloc is NULL, there was an error assembling the branch. */ 3351 if (reloc != NULL) 3352 { 3353 symbolS *symp = reloc->reloc_expression.X_add_symbol; 3354 offsetT offset = reloc->reloc_expression.X_add_number; 3355 char *f; 3356 bfd_boolean is_cdx = (insn->insn_nios2_opcode->size == 2); 3357 3358 /* Tag dwarf2 debug info to the address at the start of the insn. 3359 We must do it before frag_var() below closes off the frag. */ 3360 dwarf2_emit_insn (0); 3361 3362 /* We create a machine dependent frag which can grow 3363 to accommodate the largest possible instruction sequence 3364 this may generate. */ 3365 f = frag_var (rs_machine_dependent, 3366 UBRANCH_MAX_SIZE, insn->insn_nios2_opcode->size, 3367 (is_cdx ? CDX_UBRANCH_SUBTYPE (0) : UBRANCH_SUBTYPE (0)), 3368 symp, offset, NULL); 3369 3370 md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size); 3371 3372 /* We leave fixup generation to md_convert_frag. */ 3373 } 3374 } 3375 3376 /* Output a conditional branch. */ 3377 static void 3378 output_cbranch (nios2_insn_infoS *insn) 3379 { 3380 nios2_insn_relocS *reloc = insn->insn_reloc; 3381 3382 /* If the reloc is NULL, there was an error assembling the branch. */ 3383 if (reloc != NULL) 3384 { 3385 symbolS *symp = reloc->reloc_expression.X_add_symbol; 3386 offsetT offset = reloc->reloc_expression.X_add_number; 3387 char *f; 3388 bfd_boolean is_cdx = (insn->insn_nios2_opcode->size == 2); 3389 3390 /* Tag dwarf2 debug info to the address at the start of the insn. 3391 We must do it before frag_var() below closes off the frag. */ 3392 dwarf2_emit_insn (0); 3393 3394 /* We create a machine dependent frag which can grow 3395 to accommodate the largest possible instruction sequence 3396 this may generate. */ 3397 f = frag_var (rs_machine_dependent, 3398 CBRANCH_MAX_SIZE, insn->insn_nios2_opcode->size, 3399 (is_cdx ? CDX_CBRANCH_SUBTYPE (0) : CBRANCH_SUBTYPE (0)), 3400 symp, offset, NULL); 3401 3402 md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size); 3403 3404 /* We leave fixup generation to md_convert_frag. */ 3405 } 3406 } 3407 3408 /* Output a call sequence. Since calls are not pc-relative for NIOS2, 3409 but are page-relative, we cannot tell at any stage in assembly 3410 whether a call will be out of range since a section may be linked 3411 at any address. So if we are relaxing, we convert all call instructions 3412 to long call sequences, and rely on the linker to relax them back to 3413 short calls. */ 3414 static void 3415 output_call (nios2_insn_infoS *insn) 3416 { 3417 /* This allocates enough space for the instruction 3418 and puts it in the current frag. */ 3419 char *f = frag_more (12); 3420 nios2_insn_relocS *reloc = insn->insn_reloc; 3421 const struct nios2_opcode *op = insn->insn_nios2_opcode; 3422 3423 switch (op->format) 3424 { 3425 case iw_j_type: 3426 md_number_to_chars (f, 3427 (MATCH_R1_ORHI | SET_IW_I_B (AT_REGNUM) 3428 | SET_IW_I_A (0)), 3429 4); 3430 dwarf2_emit_insn (4); 3431 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, 3432 &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_HI16); 3433 md_number_to_chars (f + 4, 3434 (MATCH_R1_ORI | SET_IW_I_B (AT_REGNUM) 3435 | SET_IW_I_A (AT_REGNUM)), 3436 4); 3437 dwarf2_emit_insn (4); 3438 fix_new_exp (frag_now, f - frag_now->fr_literal + 4, 4, 3439 &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_LO16); 3440 md_number_to_chars (f + 8, MATCH_R1_CALLR | SET_IW_R_A (AT_REGNUM), 4); 3441 dwarf2_emit_insn (4); 3442 break; 3443 case iw_L26_type: 3444 md_number_to_chars (f, 3445 (MATCH_R2_ORHI | SET_IW_F2I16_B (AT_REGNUM) 3446 | SET_IW_F2I16_A (0)), 3447 4); 3448 dwarf2_emit_insn (4); 3449 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, 3450 &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_HI16); 3451 md_number_to_chars (f + 4, 3452 (MATCH_R2_ORI | SET_IW_F2I16_B (AT_REGNUM) 3453 | SET_IW_F2I16_A (AT_REGNUM)), 3454 4); 3455 dwarf2_emit_insn (4); 3456 fix_new_exp (frag_now, f - frag_now->fr_literal + 4, 4, 3457 &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_LO16); 3458 md_number_to_chars (f + 8, MATCH_R2_CALLR | SET_IW_F3X6L5_A (AT_REGNUM), 3459 4); 3460 dwarf2_emit_insn (4); 3461 break; 3462 default: 3463 bad_opcode (op); 3464 } 3465 } 3466 3467 /* Output a movhi/addi pair for the movia pseudo-op. */ 3468 static void 3469 output_movia (nios2_insn_infoS *insn) 3470 { 3471 /* This allocates enough space for the instruction 3472 and puts it in the current frag. */ 3473 char *f = frag_more (8); 3474 nios2_insn_relocS *reloc = insn->insn_reloc; 3475 unsigned long reg, code = 0; 3476 const struct nios2_opcode *op = insn->insn_nios2_opcode; 3477 3478 /* If the reloc is NULL, there was an error assembling the movia. */ 3479 if (reloc != NULL) 3480 { 3481 switch (op->format) 3482 { 3483 case iw_i_type: 3484 reg = GET_IW_I_B (insn->insn_code); 3485 code = MATCH_R1_ADDI | SET_IW_I_A (reg) | SET_IW_I_B (reg); 3486 break; 3487 case iw_F2I16_type: 3488 reg = GET_IW_F2I16_B (insn->insn_code); 3489 code = MATCH_R2_ADDI | SET_IW_F2I16_A (reg) | SET_IW_F2I16_B (reg); 3490 break; 3491 default: 3492 bad_opcode (op); 3493 } 3494 3495 md_number_to_chars (f, insn->insn_code, 4); 3496 dwarf2_emit_insn (4); 3497 fix_new (frag_now, f - frag_now->fr_literal, 4, 3498 reloc->reloc_expression.X_add_symbol, 3499 reloc->reloc_expression.X_add_number, 0, 3500 BFD_RELOC_NIOS2_HIADJ16); 3501 md_number_to_chars (f + 4, code, 4); 3502 dwarf2_emit_insn (4); 3503 fix_new (frag_now, f + 4 - frag_now->fr_literal, 4, 3504 reloc->reloc_expression.X_add_symbol, 3505 reloc->reloc_expression.X_add_number, 0, BFD_RELOC_NIOS2_LO16); 3506 } 3507 } 3508 3509 3510 3511 /** External interfaces. */ 3512 3513 /* Update the selected architecture based on ARCH, giving an error if 3514 ARCH is an invalid value. */ 3515 3516 static void 3517 nios2_use_arch (const char *arch) 3518 { 3519 if (strcmp (arch, "nios2") == 0 || strcmp (arch, "r1") == 0) 3520 { 3521 nios2_architecture |= EF_NIOS2_ARCH_R1; 3522 nios2_opcodes = (struct nios2_opcode *) nios2_r1_opcodes; 3523 nios2_num_opcodes = nios2_num_r1_opcodes; 3524 nop32 = nop_r1; 3525 nop16 = NULL; 3526 return; 3527 } 3528 else if (strcmp (arch, "r2") == 0) 3529 { 3530 nios2_architecture |= EF_NIOS2_ARCH_R2; 3531 nios2_opcodes = (struct nios2_opcode *) nios2_r2_opcodes; 3532 nios2_num_opcodes = nios2_num_r2_opcodes; 3533 nop32 = nop_r2; 3534 nop16 = nop_r2_cdx; 3535 return; 3536 } 3537 3538 as_bad (_("unknown architecture '%s'"), arch); 3539 } 3540 3541 /* The following functions are called by machine-independent parts of 3542 the assembler. */ 3543 int 3544 md_parse_option (int c, const char *arg ATTRIBUTE_UNUSED) 3545 { 3546 switch (c) 3547 { 3548 case 'r': 3549 /* Hidden option for self-test mode. */ 3550 nios2_mode = NIOS2_MODE_TEST; 3551 break; 3552 case OPTION_RELAX_ALL: 3553 nios2_as_options.relax = relax_all; 3554 break; 3555 case OPTION_NORELAX: 3556 nios2_as_options.relax = relax_none; 3557 break; 3558 case OPTION_RELAX_SECTION: 3559 nios2_as_options.relax = relax_section; 3560 break; 3561 case OPTION_EB: 3562 target_big_endian = 1; 3563 break; 3564 case OPTION_EL: 3565 target_big_endian = 0; 3566 break; 3567 case OPTION_MARCH: 3568 nios2_use_arch (arg); 3569 break; 3570 default: 3571 return 0; 3572 break; 3573 } 3574 3575 return 1; 3576 } 3577 3578 /* Implement TARGET_FORMAT. We can choose to be big-endian or 3579 little-endian at runtime based on a switch. */ 3580 const char * 3581 nios2_target_format (void) 3582 { 3583 return target_big_endian ? "elf32-bignios2" : "elf32-littlenios2"; 3584 } 3585 3586 /* Machine-dependent usage message. */ 3587 void 3588 md_show_usage (FILE *stream) 3589 { 3590 fprintf (stream, " NIOS2 options:\n" 3591 " -relax-all replace all branch and call " 3592 "instructions with jmp and callr sequences\n" 3593 " -relax-section replace identified out of range " 3594 "branches with jmp sequences (default)\n" 3595 " -no-relax do not replace any branches or calls\n" 3596 " -EB force big-endian byte ordering\n" 3597 " -EL force little-endian byte ordering\n" 3598 " -march=ARCH enable instructions from architecture ARCH\n"); 3599 } 3600 3601 3602 /* This function is called once, at assembler startup time. 3603 It should set up all the tables, etc. that the MD part of the 3604 assembler will need. */ 3605 void 3606 md_begin (void) 3607 { 3608 int i; 3609 const char *inserted; 3610 3611 switch (nios2_architecture) 3612 { 3613 default: 3614 case EF_NIOS2_ARCH_R1: 3615 bfd_default_set_arch_mach (stdoutput, bfd_arch_nios2, bfd_mach_nios2r1); 3616 break; 3617 case EF_NIOS2_ARCH_R2: 3618 if (target_big_endian) 3619 as_fatal (_("Big-endian R2 is not supported.")); 3620 bfd_default_set_arch_mach (stdoutput, bfd_arch_nios2, bfd_mach_nios2r2); 3621 break; 3622 } 3623 3624 /* Create and fill a hashtable for the Nios II opcodes, registers and 3625 arguments. */ 3626 nios2_opcode_hash = hash_new (); 3627 nios2_reg_hash = hash_new (); 3628 nios2_ps_hash = hash_new (); 3629 3630 for (i = 0; i < nios2_num_opcodes; ++i) 3631 { 3632 inserted 3633 = hash_insert (nios2_opcode_hash, nios2_opcodes[i].name, 3634 (PTR) & nios2_opcodes[i]); 3635 if (inserted != NULL) 3636 { 3637 fprintf (stderr, _("internal error: can't hash `%s': %s\n"), 3638 nios2_opcodes[i].name, inserted); 3639 /* Probably a memory allocation problem? Give up now. */ 3640 as_fatal (_("Broken assembler. No assembly attempted.")); 3641 } 3642 } 3643 3644 for (i = 0; i < nios2_num_regs; ++i) 3645 { 3646 inserted 3647 = hash_insert (nios2_reg_hash, nios2_regs[i].name, 3648 (PTR) & nios2_regs[i]); 3649 if (inserted != NULL) 3650 { 3651 fprintf (stderr, _("internal error: can't hash `%s': %s\n"), 3652 nios2_regs[i].name, inserted); 3653 /* Probably a memory allocation problem? Give up now. */ 3654 as_fatal (_("Broken assembler. No assembly attempted.")); 3655 } 3656 3657 } 3658 3659 for (i = 0; i < nios2_num_ps_insn_info_structs; ++i) 3660 { 3661 inserted 3662 = hash_insert (nios2_ps_hash, nios2_ps_insn_info_structs[i].pseudo_insn, 3663 (PTR) & nios2_ps_insn_info_structs[i]); 3664 if (inserted != NULL) 3665 { 3666 fprintf (stderr, _("internal error: can't hash `%s': %s\n"), 3667 nios2_ps_insn_info_structs[i].pseudo_insn, inserted); 3668 /* Probably a memory allocation problem? Give up now. */ 3669 as_fatal (_("Broken assembler. No assembly attempted.")); 3670 } 3671 } 3672 3673 /* Assembler option defaults. */ 3674 nios2_as_options.noat = FALSE; 3675 nios2_as_options.nobreak = FALSE; 3676 3677 /* Debug information is incompatible with relaxation. */ 3678 if (debug_type != DEBUG_UNSPECIFIED) 3679 nios2_as_options.relax = relax_none; 3680 3681 /* Initialize the alignment data. */ 3682 nios2_current_align_seg = now_seg; 3683 nios2_last_label = NULL; 3684 nios2_current_align = 0; 3685 nios2_min_align = 2; 3686 } 3687 3688 3689 /* Assembles a single line of Nios II assembly language. */ 3690 void 3691 md_assemble (char *op_str) 3692 { 3693 char *argstr; 3694 char *op_strdup = NULL; 3695 unsigned long saved_pinfo = 0; 3696 nios2_insn_infoS thisinsn; 3697 nios2_insn_infoS *insn = &thisinsn; 3698 bfd_boolean ps_error = FALSE; 3699 3700 /* Make sure we are aligned on an appropriate boundary. */ 3701 if (nios2_current_align < nios2_min_align) 3702 nios2_align (nios2_min_align, NULL, nios2_last_label); 3703 else if (nios2_current_align > nios2_min_align) 3704 nios2_current_align = nios2_min_align; 3705 nios2_last_label = NULL; 3706 3707 /* We don't want to clobber to op_str 3708 because we want to be able to use it in messages. */ 3709 op_strdup = strdup (op_str); 3710 insn->insn_tokens[0] = strtok (op_strdup, " "); 3711 argstr = strtok (NULL, ""); 3712 3713 /* Assemble the opcode. */ 3714 insn->insn_nios2_opcode = nios2_opcode_lookup (insn->insn_tokens[0]); 3715 insn->insn_reloc = NULL; 3716 3717 if (insn->insn_nios2_opcode != NULL) 3718 { 3719 nios2_ps_insn_infoS *ps_insn = NULL; 3720 3721 /* Note if we've seen a 16-bit instruction. */ 3722 if (insn->insn_nios2_opcode->size == 2) 3723 nios2_min_align = 1; 3724 3725 /* Set the opcode for the instruction. */ 3726 insn->insn_code = insn->insn_nios2_opcode->match; 3727 insn->constant_bits = 0; 3728 3729 /* Parse the arguments pointed to by argstr. */ 3730 if (nios2_mode == NIOS2_MODE_ASSEMBLE) 3731 nios2_parse_args (insn, argstr, insn->insn_nios2_opcode->args, 3732 (char **) &insn->insn_tokens[1]); 3733 else 3734 nios2_parse_args (insn, argstr, insn->insn_nios2_opcode->args_test, 3735 (char **) &insn->insn_tokens[1]); 3736 3737 /* We need to preserve the MOVIA macro as this is clobbered by 3738 translate_pseudo_insn. */ 3739 if (insn->insn_nios2_opcode->pinfo == NIOS2_INSN_MACRO_MOVIA) 3740 saved_pinfo = NIOS2_INSN_MACRO_MOVIA; 3741 /* If the instruction is an pseudo-instruction, we want to replace it 3742 with its real equivalent, and then continue. */ 3743 if ((insn->insn_nios2_opcode->pinfo & NIOS2_INSN_MACRO) 3744 == NIOS2_INSN_MACRO) 3745 { 3746 ps_insn = nios2_translate_pseudo_insn (insn); 3747 if (!ps_insn) 3748 ps_error = TRUE; 3749 } 3750 3751 /* If we found invalid pseudo-instruction syntax, the error's already 3752 been diagnosed in nios2_translate_pseudo_insn, so skip 3753 remaining processing. */ 3754 if (!ps_error) 3755 { 3756 /* Assemble the parsed arguments into the instruction word. */ 3757 nios2_assemble_args (insn); 3758 3759 /* Handle relaxation and other transformations. */ 3760 if (nios2_as_options.relax != relax_none 3761 && !nios2_as_options.noat 3762 && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_UBRANCH) 3763 output_ubranch (insn); 3764 else if (nios2_as_options.relax != relax_none 3765 && !nios2_as_options.noat 3766 && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_CBRANCH) 3767 output_cbranch (insn); 3768 else if (nios2_as_options.relax == relax_all 3769 && !nios2_as_options.noat 3770 && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_CALL 3771 && insn->insn_reloc 3772 && ((insn->insn_reloc->reloc_type 3773 == BFD_RELOC_NIOS2_CALL26) 3774 || (insn->insn_reloc->reloc_type 3775 == BFD_RELOC_NIOS2_CALL26_NOAT))) 3776 output_call (insn); 3777 else if (saved_pinfo == NIOS2_INSN_MACRO_MOVIA) 3778 output_movia (insn); 3779 else 3780 output_insn (insn); 3781 if (ps_insn) 3782 nios2_cleanup_pseudo_insn (insn, ps_insn); 3783 } 3784 } 3785 else 3786 /* Unrecognised instruction - error. */ 3787 as_bad (_("unrecognised instruction %s"), insn->insn_tokens[0]); 3788 3789 /* Don't leak memory. */ 3790 free (op_strdup); 3791 } 3792 3793 /* Round up section size. */ 3794 valueT 3795 md_section_align (asection *seg ATTRIBUTE_UNUSED, valueT size) 3796 { 3797 /* I think byte alignment is fine here. */ 3798 return size; 3799 } 3800 3801 /* Implement TC_FORCE_RELOCATION. */ 3802 int 3803 nios2_force_relocation (fixS *fixp) 3804 { 3805 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT 3806 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY 3807 || fixp->fx_r_type == BFD_RELOC_NIOS2_ALIGN) 3808 return 1; 3809 3810 return generic_force_reloc (fixp); 3811 } 3812 3813 /* Implement tc_fix_adjustable. */ 3814 int 3815 nios2_fix_adjustable (fixS *fixp) 3816 { 3817 if (fixp->fx_addsy == NULL) 3818 return 1; 3819 3820 #ifdef OBJ_ELF 3821 /* Prevent all adjustments to global symbols. */ 3822 if (OUTPUT_FLAVOR == bfd_target_elf_flavour 3823 && (S_IS_EXTERNAL (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy))) 3824 return 0; 3825 #endif 3826 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT 3827 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY) 3828 return 0; 3829 3830 /* Preserve relocations against symbols with function type. */ 3831 if (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION) 3832 return 0; 3833 3834 /* Don't allow symbols to be discarded on GOT related relocs. */ 3835 if (fixp->fx_r_type == BFD_RELOC_NIOS2_GOT16 3836 || fixp->fx_r_type == BFD_RELOC_NIOS2_CALL16 3837 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_LO 3838 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_HA 3839 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_GD16 3840 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LDM16 3841 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LDO16 3842 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_IE16 3843 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LE16 3844 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPMOD 3845 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPREL 3846 || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_TPREL 3847 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF 3848 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOT_LO 3849 || fixp->fx_r_type == BFD_RELOC_NIOS2_GOT_HA 3850 || fixp->fx_r_type == BFD_RELOC_NIOS2_CALL_LO 3851 || fixp->fx_r_type == BFD_RELOC_NIOS2_CALL_HA 3852 ) 3853 return 0; 3854 3855 return 1; 3856 } 3857 3858 /* Implement tc_frob_symbol. This is called in adjust_reloc_syms; 3859 it is used to remove *ABS* references from the symbol table. */ 3860 int 3861 nios2_frob_symbol (symbolS *symp) 3862 { 3863 if ((OUTPUT_FLAVOR == bfd_target_elf_flavour 3864 && symp == section_symbol (absolute_section)) 3865 || !S_IS_DEFINED (symp)) 3866 return 1; 3867 else 3868 return 0; 3869 } 3870 3871 /* The function tc_gen_reloc creates a relocation structure for the 3872 fixup fixp, and returns a pointer to it. This structure is passed 3873 to bfd_install_relocation so that it can be written to the object 3874 file for linking. */ 3875 arelent * 3876 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp) 3877 { 3878 arelent *reloc = XNEW (arelent); 3879 reloc->sym_ptr_ptr = XNEW (asymbol *); 3880 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy); 3881 3882 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where; 3883 reloc->addend = fixp->fx_offset; /* fixp->fx_addnumber; */ 3884 3885 if (fixp->fx_pcrel) 3886 { 3887 switch (fixp->fx_r_type) 3888 { 3889 case BFD_RELOC_16: 3890 fixp->fx_r_type = BFD_RELOC_16_PCREL; 3891 break; 3892 case BFD_RELOC_NIOS2_LO16: 3893 fixp->fx_r_type = BFD_RELOC_NIOS2_PCREL_LO; 3894 break; 3895 case BFD_RELOC_NIOS2_HIADJ16: 3896 fixp->fx_r_type = BFD_RELOC_NIOS2_PCREL_HA; 3897 break; 3898 default: 3899 break; 3900 } 3901 } 3902 3903 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type); 3904 if (reloc->howto == NULL) 3905 { 3906 as_bad_where (fixp->fx_file, fixp->fx_line, 3907 _("can't represent relocation type %s"), 3908 bfd_get_reloc_code_name (fixp->fx_r_type)); 3909 3910 /* Set howto to a garbage value so that we can keep going. */ 3911 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32); 3912 gas_assert (reloc->howto != NULL); 3913 } 3914 return reloc; 3915 } 3916 3917 long 3918 md_pcrel_from (fixS *fixP ATTRIBUTE_UNUSED) 3919 { 3920 return 0; 3921 } 3922 3923 /* Called just before the assembler exits. */ 3924 void 3925 md_end (void) 3926 { 3927 /* FIXME - not yet implemented */ 3928 } 3929 3930 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE. 3931 Otherwise we have no need to default values of symbols. */ 3932 symbolS * 3933 md_undefined_symbol (char *name ATTRIBUTE_UNUSED) 3934 { 3935 #ifdef OBJ_ELF 3936 if (name[0] == '_' && name[1] == 'G' 3937 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0) 3938 { 3939 if (!GOT_symbol) 3940 { 3941 if (symbol_find (name)) 3942 as_bad ("GOT already in the symbol table"); 3943 3944 GOT_symbol = symbol_new (name, undefined_section, 3945 (valueT) 0, &zero_address_frag); 3946 } 3947 3948 return GOT_symbol; 3949 } 3950 #endif 3951 3952 return 0; 3953 } 3954 3955 /* Implement tc_frob_label. */ 3956 void 3957 nios2_frob_label (symbolS *lab) 3958 { 3959 /* Emit dwarf information. */ 3960 dwarf2_emit_label (lab); 3961 3962 /* Update the label's address with the current output pointer. */ 3963 symbol_set_frag (lab, frag_now); 3964 S_SET_VALUE (lab, (valueT) frag_now_fix ()); 3965 3966 /* Record this label for future adjustment after we find out what 3967 kind of data it references, and the required alignment therewith. */ 3968 nios2_last_label = lab; 3969 } 3970 3971 /* Implement md_cons_align. */ 3972 void 3973 nios2_cons_align (int size) 3974 { 3975 int log_size = 0; 3976 const char *pfill = NULL; 3977 3978 while ((size >>= 1) != 0) 3979 ++log_size; 3980 3981 if (subseg_text_p (now_seg)) 3982 pfill = (const char *) nop32; 3983 else 3984 pfill = NULL; 3985 3986 if (nios2_auto_align_on) 3987 nios2_align (log_size, pfill, NULL); 3988 3989 nios2_last_label = NULL; 3990 } 3991 3992 /* Map 's' to SHF_NIOS2_GPREL. */ 3993 /* This is from the Alpha code tc-alpha.c. */ 3994 int 3995 nios2_elf_section_letter (int letter, const char **ptr_msg) 3996 { 3997 if (letter == 's') 3998 return SHF_NIOS2_GPREL; 3999 4000 *ptr_msg = _("Bad .section directive: want a,s,w,x,M,S,G,T in string"); 4001 return -1; 4002 } 4003 4004 /* Map SHF_ALPHA_GPREL to SEC_SMALL_DATA. */ 4005 /* This is from the Alpha code tc-alpha.c. */ 4006 flagword 4007 nios2_elf_section_flags (flagword flags, int attr, int type ATTRIBUTE_UNUSED) 4008 { 4009 if (attr & SHF_NIOS2_GPREL) 4010 flags |= SEC_SMALL_DATA; 4011 return flags; 4012 } 4013 4014 /* Implement TC_PARSE_CONS_EXPRESSION to handle %tls_ldo(...) */ 4015 bfd_reloc_code_real_type 4016 nios2_cons (expressionS *exp, int size) 4017 { 4018 bfd_reloc_code_real_type nios2_tls_ldo_reloc = BFD_RELOC_NONE; 4019 4020 SKIP_WHITESPACE (); 4021 if (input_line_pointer[0] == '%') 4022 { 4023 if (strprefix (input_line_pointer + 1, "tls_ldo")) 4024 { 4025 if (size != 4) 4026 as_bad (_("Illegal operands: %%tls_ldo in %d-byte data field"), 4027 size); 4028 else 4029 { 4030 input_line_pointer += 8; 4031 nios2_tls_ldo_reloc = BFD_RELOC_NIOS2_TLS_DTPREL; 4032 } 4033 } 4034 if (nios2_tls_ldo_reloc != BFD_RELOC_NONE) 4035 { 4036 SKIP_WHITESPACE (); 4037 if (input_line_pointer[0] != '(') 4038 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()")); 4039 else 4040 { 4041 int c; 4042 char *end = ++input_line_pointer; 4043 int npar = 0; 4044 4045 for (c = *end; !is_end_of_line[c]; end++, c = *end) 4046 if (c == '(') 4047 npar++; 4048 else if (c == ')') 4049 { 4050 if (!npar) 4051 break; 4052 npar--; 4053 } 4054 4055 if (c != ')') 4056 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()")); 4057 else 4058 { 4059 *end = '\0'; 4060 expression (exp); 4061 *end = c; 4062 if (input_line_pointer != end) 4063 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()")); 4064 else 4065 { 4066 input_line_pointer++; 4067 SKIP_WHITESPACE (); 4068 c = *input_line_pointer; 4069 if (! is_end_of_line[c] && c != ',') 4070 as_bad (_("Illegal operands: garbage after %%tls_ldo()")); 4071 } 4072 } 4073 } 4074 } 4075 } 4076 if (nios2_tls_ldo_reloc == BFD_RELOC_NONE) 4077 expression (exp); 4078 return nios2_tls_ldo_reloc; 4079 } 4080 4081 /* Implement HANDLE_ALIGN. */ 4082 void 4083 nios2_handle_align (fragS *fragp) 4084 { 4085 /* If we are expecting to relax in the linker, then we must output a 4086 relocation to tell the linker we are aligning code. */ 4087 if (nios2_as_options.relax == relax_all 4088 && (fragp->fr_type == rs_align || fragp->fr_type == rs_align_code) 4089 && fragp->fr_address + fragp->fr_fix > 0 4090 && fragp->fr_offset > 1 4091 && now_seg != bss_section) 4092 fix_new (fragp, fragp->fr_fix, 0, &abs_symbol, fragp->fr_offset, 0, 4093 BFD_RELOC_NIOS2_ALIGN); 4094 } 4095 4096 /* Implement tc_regname_to_dw2regnum, to convert REGNAME to a DWARF-2 4097 register number. */ 4098 int 4099 nios2_regname_to_dw2regnum (char *regname) 4100 { 4101 struct nios2_reg *r = nios2_reg_lookup (regname); 4102 if (r == NULL) 4103 return -1; 4104 return r->index; 4105 } 4106 4107 /* Implement tc_cfi_frame_initial_instructions, to initialize the DWARF-2 4108 unwind information for this procedure. */ 4109 void 4110 nios2_frame_initial_instructions (void) 4111 { 4112 cfi_add_CFA_def_cfa (27, 0); 4113 } 4114 4115 #ifdef OBJ_ELF 4116 /* Some special processing for a Nios II ELF file. */ 4117 4118 void 4119 nios2_elf_final_processing (void) 4120 { 4121 elf_elfheader (stdoutput)->e_flags = nios2_architecture; 4122 } 4123 #endif 4124