1 /* tc-mips.c -- assemble code for a MIPS chip. 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 3 2003, 2004, 2005 Free Software Foundation, Inc. 4 Contributed by the OSF and Ralph Campbell. 5 Written by Keith Knowles and Ralph Campbell, working independently. 6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus 7 Support. 8 9 This file is part of GAS. 10 11 GAS is free software; you can redistribute it and/or modify 12 it under the terms of the GNU General Public License as published by 13 the Free Software Foundation; either version 2, or (at your option) 14 any later version. 15 16 GAS is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License for more details. 20 21 You should have received a copy of the GNU General Public License 22 along with GAS; see the file COPYING. If not, write to the Free 23 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 24 02110-1301, USA. */ 25 26 #include "as.h" 27 #include "config.h" 28 #include "subsegs.h" 29 #include "safe-ctype.h" 30 31 #include <stdarg.h> 32 33 #include "opcode/mips.h" 34 #include "itbl-ops.h" 35 #include "dwarf2dbg.h" 36 #include "dw2gencfi.h" 37 38 #ifdef DEBUG 39 #define DBG(x) printf x 40 #else 41 #define DBG(x) 42 #endif 43 44 #ifdef OBJ_MAYBE_ELF 45 /* Clean up namespace so we can include obj-elf.h too. */ 46 static int mips_output_flavor (void); 47 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; } 48 #undef OBJ_PROCESS_STAB 49 #undef OUTPUT_FLAVOR 50 #undef S_GET_ALIGN 51 #undef S_GET_SIZE 52 #undef S_SET_ALIGN 53 #undef S_SET_SIZE 54 #undef obj_frob_file 55 #undef obj_frob_file_after_relocs 56 #undef obj_frob_symbol 57 #undef obj_pop_insert 58 #undef obj_sec_sym_ok_for_reloc 59 #undef OBJ_COPY_SYMBOL_ATTRIBUTES 60 61 #include "obj-elf.h" 62 /* Fix any of them that we actually care about. */ 63 #undef OUTPUT_FLAVOR 64 #define OUTPUT_FLAVOR mips_output_flavor() 65 #endif 66 67 #if defined (OBJ_ELF) 68 #include "elf/mips.h" 69 #endif 70 71 #ifndef ECOFF_DEBUGGING 72 #define NO_ECOFF_DEBUGGING 73 #define ECOFF_DEBUGGING 0 74 #endif 75 76 int mips_flag_mdebug = -1; 77 78 /* Control generation of .pdr sections. Off by default on IRIX: the native 79 linker doesn't know about and discards them, but relocations against them 80 remain, leading to rld crashes. */ 81 #ifdef TE_IRIX 82 int mips_flag_pdr = FALSE; 83 #else 84 int mips_flag_pdr = TRUE; 85 #endif 86 87 #include "ecoff.h" 88 89 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) 90 static char *mips_regmask_frag; 91 #endif 92 93 #define ZERO 0 94 #define AT 1 95 #define TREG 24 96 #define PIC_CALL_REG 25 97 #define KT0 26 98 #define KT1 27 99 #define GP 28 100 #define SP 29 101 #define FP 30 102 #define RA 31 103 104 #define ILLEGAL_REG (32) 105 106 /* Allow override of standard little-endian ECOFF format. */ 107 108 #ifndef ECOFF_LITTLE_FORMAT 109 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips" 110 #endif 111 112 extern int target_big_endian; 113 114 /* The name of the readonly data section. */ 115 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \ 116 ? ".rdata" \ 117 : OUTPUT_FLAVOR == bfd_target_coff_flavour \ 118 ? ".rdata" \ 119 : OUTPUT_FLAVOR == bfd_target_elf_flavour \ 120 ? ".rodata" \ 121 : (abort (), "")) 122 123 /* Information about an instruction, including its format, operands 124 and fixups. */ 125 struct mips_cl_insn 126 { 127 /* The opcode's entry in mips_opcodes or mips16_opcodes. */ 128 const struct mips_opcode *insn_mo; 129 130 /* True if this is a mips16 instruction and if we want the extended 131 form of INSN_MO. */ 132 bfd_boolean use_extend; 133 134 /* The 16-bit extension instruction to use when USE_EXTEND is true. */ 135 unsigned short extend; 136 137 /* The 16-bit or 32-bit bitstring of the instruction itself. This is 138 a copy of INSN_MO->match with the operands filled in. */ 139 unsigned long insn_opcode; 140 141 /* The frag that contains the instruction. */ 142 struct frag *frag; 143 144 /* The offset into FRAG of the first instruction byte. */ 145 long where; 146 147 /* The relocs associated with the instruction, if any. */ 148 fixS *fixp[3]; 149 150 /* True if this entry cannot be moved from its current position. */ 151 unsigned int fixed_p : 1; 152 153 /* True if this instruction occured in a .set noreorder block. */ 154 unsigned int noreorder_p : 1; 155 156 /* True for mips16 instructions that jump to an absolute address. */ 157 unsigned int mips16_absolute_jump_p : 1; 158 }; 159 160 /* The ABI to use. */ 161 enum mips_abi_level 162 { 163 NO_ABI = 0, 164 O32_ABI, 165 O64_ABI, 166 N32_ABI, 167 N64_ABI, 168 EABI_ABI 169 }; 170 171 /* MIPS ABI we are using for this output file. */ 172 static enum mips_abi_level mips_abi = NO_ABI; 173 174 /* Whether or not we have code that can call pic code. */ 175 int mips_abicalls = FALSE; 176 177 /* Whether or not we have code which can be put into a shared 178 library. */ 179 static bfd_boolean mips_in_shared = TRUE; 180 181 /* This is the set of options which may be modified by the .set 182 pseudo-op. We use a struct so that .set push and .set pop are more 183 reliable. */ 184 185 struct mips_set_options 186 { 187 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1 188 if it has not been initialized. Changed by `.set mipsN', and the 189 -mipsN command line option, and the default CPU. */ 190 int isa; 191 /* Enabled Application Specific Extensions (ASEs). These are set to -1 192 if they have not been initialized. Changed by `.set <asename>', by 193 command line options, and based on the default architecture. */ 194 int ase_mips3d; 195 int ase_mdmx; 196 int ase_dsp; 197 int ase_mt; 198 /* Whether we are assembling for the mips16 processor. 0 if we are 199 not, 1 if we are, and -1 if the value has not been initialized. 200 Changed by `.set mips16' and `.set nomips16', and the -mips16 and 201 -nomips16 command line options, and the default CPU. */ 202 int mips16; 203 /* Non-zero if we should not reorder instructions. Changed by `.set 204 reorder' and `.set noreorder'. */ 205 int noreorder; 206 /* Non-zero if we should not permit the $at ($1) register to be used 207 in instructions. Changed by `.set at' and `.set noat'. */ 208 int noat; 209 /* Non-zero if we should warn when a macro instruction expands into 210 more than one machine instruction. Changed by `.set nomacro' and 211 `.set macro'. */ 212 int warn_about_macros; 213 /* Non-zero if we should not move instructions. Changed by `.set 214 move', `.set volatile', `.set nomove', and `.set novolatile'. */ 215 int nomove; 216 /* Non-zero if we should not optimize branches by moving the target 217 of the branch into the delay slot. Actually, we don't perform 218 this optimization anyhow. Changed by `.set bopt' and `.set 219 nobopt'. */ 220 int nobopt; 221 /* Non-zero if we should not autoextend mips16 instructions. 222 Changed by `.set autoextend' and `.set noautoextend'. */ 223 int noautoextend; 224 /* Restrict general purpose registers and floating point registers 225 to 32 bit. This is initially determined when -mgp32 or -mfp32 226 is passed but can changed if the assembler code uses .set mipsN. */ 227 int gp32; 228 int fp32; 229 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march 230 command line option, and the default CPU. */ 231 int arch; 232 /* True if ".set sym32" is in effect. */ 233 bfd_boolean sym32; 234 }; 235 236 /* True if -mgp32 was passed. */ 237 static int file_mips_gp32 = -1; 238 239 /* True if -mfp32 was passed. */ 240 static int file_mips_fp32 = -1; 241 242 /* This is the struct we use to hold the current set of options. Note 243 that we must set the isa field to ISA_UNKNOWN and the ASE fields to 244 -1 to indicate that they have not been initialized. */ 245 246 static struct mips_set_options mips_opts = 247 { 248 ISA_UNKNOWN, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN, FALSE 249 }; 250 251 /* These variables are filled in with the masks of registers used. 252 The object format code reads them and puts them in the appropriate 253 place. */ 254 unsigned long mips_gprmask; 255 unsigned long mips_cprmask[4]; 256 257 /* MIPS ISA we are using for this output file. */ 258 static int file_mips_isa = ISA_UNKNOWN; 259 260 /* True if -mips16 was passed or implied by arguments passed on the 261 command line (e.g., by -march). */ 262 static int file_ase_mips16; 263 264 /* True if -mips3d was passed or implied by arguments passed on the 265 command line (e.g., by -march). */ 266 static int file_ase_mips3d; 267 268 /* True if -mdmx was passed or implied by arguments passed on the 269 command line (e.g., by -march). */ 270 static int file_ase_mdmx; 271 272 /* True if -mdsp was passed or implied by arguments passed on the 273 command line (e.g., by -march). */ 274 static int file_ase_dsp; 275 276 /* True if -mmt was passed or implied by arguments passed on the 277 command line (e.g., by -march). */ 278 static int file_ase_mt; 279 280 /* The argument of the -march= flag. The architecture we are assembling. */ 281 static int file_mips_arch = CPU_UNKNOWN; 282 static const char *mips_arch_string; 283 284 /* The argument of the -mtune= flag. The architecture for which we 285 are optimizing. */ 286 static int mips_tune = CPU_UNKNOWN; 287 static const char *mips_tune_string; 288 289 /* True when generating 32-bit code for a 64-bit processor. */ 290 static int mips_32bitmode = 0; 291 292 /* True if the given ABI requires 32-bit registers. */ 293 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI) 294 295 /* Likewise 64-bit registers. */ 296 #define ABI_NEEDS_64BIT_REGS(ABI) \ 297 ((ABI) == N32_ABI \ 298 || (ABI) == N64_ABI \ 299 || (ABI) == O64_ABI) 300 301 /* Return true if ISA supports 64 bit wide gp register instructions. */ 302 #define ISA_HAS_64BIT_REGS(ISA) ( \ 303 (ISA) == ISA_MIPS3 \ 304 || (ISA) == ISA_MIPS4 \ 305 || (ISA) == ISA_MIPS5 \ 306 || (ISA) == ISA_MIPS64 \ 307 || (ISA) == ISA_MIPS64R2 \ 308 ) 309 310 /* Return true if ISA supports 64 bit wide float registers. */ 311 #define ISA_HAS_64BIT_FPRS(ISA) \ 312 ((ISA) == ISA_MIPS3 \ 313 || (ISA) == ISA_MIPS4 \ 314 || (ISA) == ISA_MIPS5 \ 315 || (ISA) == ISA_MIPS32R2 \ 316 || (ISA) == ISA_MIPS64 \ 317 || (ISA) == ISA_MIPS64R2) 318 319 /* Return true if ISA supports 64-bit right rotate (dror et al.) 320 instructions. */ 321 #define ISA_HAS_DROR(ISA) ( \ 322 (ISA) == ISA_MIPS64R2 \ 323 ) 324 325 /* Return true if ISA supports 32-bit right rotate (ror et al.) 326 instructions. */ 327 #define ISA_HAS_ROR(ISA) ( \ 328 (ISA) == ISA_MIPS32R2 \ 329 || (ISA) == ISA_MIPS64R2 \ 330 ) 331 332 /* Return true if ISA supports single-precision floats in odd registers. */ 333 #define ISA_HAS_ODD_SINGLE_FPR(ISA) \ 334 ((ISA) == ISA_MIPS32 \ 335 || (ISA) == ISA_MIPS32R2 \ 336 || (ISA) == ISA_MIPS64 \ 337 || (ISA) == ISA_MIPS64R2) 338 339 #define HAVE_32BIT_GPRS \ 340 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa)) 341 342 #define HAVE_32BIT_FPRS \ 343 (mips_opts.fp32 || ! ISA_HAS_64BIT_FPRS (mips_opts.isa)) 344 345 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS) 346 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS) 347 348 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI) 349 350 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI) 351 352 /* True if relocations are stored in-place. */ 353 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI) 354 355 /* The ABI-derived address size. */ 356 #define HAVE_64BIT_ADDRESSES \ 357 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI)) 358 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES) 359 360 /* The size of symbolic constants (i.e., expressions of the form 361 "SYMBOL" or "SYMBOL + OFFSET"). */ 362 #define HAVE_32BIT_SYMBOLS \ 363 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32) 364 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS) 365 366 /* Addresses are loaded in different ways, depending on the address size 367 in use. The n32 ABI Documentation also mandates the use of additions 368 with overflow checking, but existing implementations don't follow it. */ 369 #define ADDRESS_ADD_INSN \ 370 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu") 371 372 #define ADDRESS_ADDI_INSN \ 373 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu") 374 375 #define ADDRESS_LOAD_INSN \ 376 (HAVE_32BIT_ADDRESSES ? "lw" : "ld") 377 378 #define ADDRESS_STORE_INSN \ 379 (HAVE_32BIT_ADDRESSES ? "sw" : "sd") 380 381 /* Return true if the given CPU supports the MIPS16 ASE. */ 382 #define CPU_HAS_MIPS16(cpu) \ 383 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \ 384 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0) 385 386 /* Return true if the given CPU supports the MIPS3D ASE. */ 387 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \ 388 ) 389 390 /* Return true if the given CPU supports the MDMX ASE. */ 391 #define CPU_HAS_MDMX(cpu) (FALSE \ 392 ) 393 394 /* Return true if the given CPU supports the DSP ASE. */ 395 #define CPU_HAS_DSP(cpu) (FALSE \ 396 ) 397 398 /* Return true if the given CPU supports the MT ASE. */ 399 #define CPU_HAS_MT(cpu) (FALSE \ 400 ) 401 402 /* True if CPU has a dror instruction. */ 403 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500) 404 405 /* True if CPU has a ror instruction. */ 406 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU) 407 408 /* True if mflo and mfhi can be immediately followed by instructions 409 which write to the HI and LO registers. 410 411 According to MIPS specifications, MIPS ISAs I, II, and III need 412 (at least) two instructions between the reads of HI/LO and 413 instructions which write them, and later ISAs do not. Contradicting 414 the MIPS specifications, some MIPS IV processor user manuals (e.g. 415 the UM for the NEC Vr5000) document needing the instructions between 416 HI/LO reads and writes, as well. Therefore, we declare only MIPS32, 417 MIPS64 and later ISAs to have the interlocks, plus any specific 418 earlier-ISA CPUs for which CPU documentation declares that the 419 instructions are really interlocked. */ 420 #define hilo_interlocks \ 421 (mips_opts.isa == ISA_MIPS32 \ 422 || mips_opts.isa == ISA_MIPS32R2 \ 423 || mips_opts.isa == ISA_MIPS64 \ 424 || mips_opts.isa == ISA_MIPS64R2 \ 425 || mips_opts.arch == CPU_R4010 \ 426 || mips_opts.arch == CPU_R10000 \ 427 || mips_opts.arch == CPU_R12000 \ 428 || mips_opts.arch == CPU_RM7000 \ 429 || mips_opts.arch == CPU_VR5500 \ 430 ) 431 432 /* Whether the processor uses hardware interlocks to protect reads 433 from the GPRs after they are loaded from memory, and thus does not 434 require nops to be inserted. This applies to instructions marked 435 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA 436 level I. */ 437 #define gpr_interlocks \ 438 (mips_opts.isa != ISA_MIPS1 \ 439 || mips_opts.arch == CPU_R3900) 440 441 /* Whether the processor uses hardware interlocks to avoid delays 442 required by coprocessor instructions, and thus does not require 443 nops to be inserted. This applies to instructions marked 444 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays 445 between instructions marked INSN_WRITE_COND_CODE and ones marked 446 INSN_READ_COND_CODE. These nops are only required at MIPS ISA 447 levels I, II, and III. */ 448 /* Itbl support may require additional care here. */ 449 #define cop_interlocks \ 450 ((mips_opts.isa != ISA_MIPS1 \ 451 && mips_opts.isa != ISA_MIPS2 \ 452 && mips_opts.isa != ISA_MIPS3) \ 453 || mips_opts.arch == CPU_R4300 \ 454 ) 455 456 /* Whether the processor uses hardware interlocks to protect reads 457 from coprocessor registers after they are loaded from memory, and 458 thus does not require nops to be inserted. This applies to 459 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only 460 requires at MIPS ISA level I. */ 461 #define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1) 462 463 /* Is this a mfhi or mflo instruction? */ 464 #define MF_HILO_INSN(PINFO) \ 465 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO)) 466 467 /* MIPS PIC level. */ 468 469 enum mips_pic_level mips_pic; 470 471 /* 1 if we should generate 32 bit offsets from the $gp register in 472 SVR4_PIC mode. Currently has no meaning in other modes. */ 473 static int mips_big_got = 0; 474 475 /* 1 if trap instructions should used for overflow rather than break 476 instructions. */ 477 static int mips_trap = 0; 478 479 /* 1 if double width floating point constants should not be constructed 480 by assembling two single width halves into two single width floating 481 point registers which just happen to alias the double width destination 482 register. On some architectures this aliasing can be disabled by a bit 483 in the status register, and the setting of this bit cannot be determined 484 automatically at assemble time. */ 485 static int mips_disable_float_construction; 486 487 /* Non-zero if any .set noreorder directives were used. */ 488 489 static int mips_any_noreorder; 490 491 /* Non-zero if nops should be inserted when the register referenced in 492 an mfhi/mflo instruction is read in the next two instructions. */ 493 static int mips_7000_hilo_fix; 494 495 /* The size of the small data section. */ 496 static unsigned int g_switch_value = 8; 497 /* Whether the -G option was used. */ 498 static int g_switch_seen = 0; 499 500 #define N_RMASK 0xc4 501 #define N_VFP 0xd4 502 503 /* If we can determine in advance that GP optimization won't be 504 possible, we can skip the relaxation stuff that tries to produce 505 GP-relative references. This makes delay slot optimization work 506 better. 507 508 This function can only provide a guess, but it seems to work for 509 gcc output. It needs to guess right for gcc, otherwise gcc 510 will put what it thinks is a GP-relative instruction in a branch 511 delay slot. 512 513 I don't know if a fix is needed for the SVR4_PIC mode. I've only 514 fixed it for the non-PIC mode. KR 95/04/07 */ 515 static int nopic_need_relax (symbolS *, int); 516 517 /* handle of the OPCODE hash table */ 518 static struct hash_control *op_hash = NULL; 519 520 /* The opcode hash table we use for the mips16. */ 521 static struct hash_control *mips16_op_hash = NULL; 522 523 /* This array holds the chars that always start a comment. If the 524 pre-processor is disabled, these aren't very useful */ 525 const char comment_chars[] = "#"; 526 527 /* This array holds the chars that only start a comment at the beginning of 528 a line. If the line seems to have the form '# 123 filename' 529 .line and .file directives will appear in the pre-processed output */ 530 /* Note that input_file.c hand checks for '#' at the beginning of the 531 first line of the input file. This is because the compiler outputs 532 #NO_APP at the beginning of its output. */ 533 /* Also note that C style comments are always supported. */ 534 const char line_comment_chars[] = "#"; 535 536 /* This array holds machine specific line separator characters. */ 537 const char line_separator_chars[] = ";"; 538 539 /* Chars that can be used to separate mant from exp in floating point nums */ 540 const char EXP_CHARS[] = "eE"; 541 542 /* Chars that mean this number is a floating point constant */ 543 /* As in 0f12.456 */ 544 /* or 0d1.2345e12 */ 545 const char FLT_CHARS[] = "rRsSfFdDxXpP"; 546 547 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be 548 changed in read.c . Ideally it shouldn't have to know about it at all, 549 but nothing is ideal around here. 550 */ 551 552 static char *insn_error; 553 554 static int auto_align = 1; 555 556 /* When outputting SVR4 PIC code, the assembler needs to know the 557 offset in the stack frame from which to restore the $gp register. 558 This is set by the .cprestore pseudo-op, and saved in this 559 variable. */ 560 static offsetT mips_cprestore_offset = -1; 561 562 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some 563 more optimizations, it can use a register value instead of a memory-saved 564 offset and even an other register than $gp as global pointer. */ 565 static offsetT mips_cpreturn_offset = -1; 566 static int mips_cpreturn_register = -1; 567 static int mips_gp_register = GP; 568 static int mips_gprel_offset = 0; 569 570 /* Whether mips_cprestore_offset has been set in the current function 571 (or whether it has already been warned about, if not). */ 572 static int mips_cprestore_valid = 0; 573 574 /* This is the register which holds the stack frame, as set by the 575 .frame pseudo-op. This is needed to implement .cprestore. */ 576 static int mips_frame_reg = SP; 577 578 /* Whether mips_frame_reg has been set in the current function 579 (or whether it has already been warned about, if not). */ 580 static int mips_frame_reg_valid = 0; 581 582 /* To output NOP instructions correctly, we need to keep information 583 about the previous two instructions. */ 584 585 /* Whether we are optimizing. The default value of 2 means to remove 586 unneeded NOPs and swap branch instructions when possible. A value 587 of 1 means to not swap branches. A value of 0 means to always 588 insert NOPs. */ 589 static int mips_optimize = 2; 590 591 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is 592 equivalent to seeing no -g option at all. */ 593 static int mips_debug = 0; 594 595 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */ 596 #define MAX_VR4130_NOPS 4 597 598 /* The maximum number of NOPs needed to fill delay slots. */ 599 #define MAX_DELAY_NOPS 2 600 601 /* The maximum number of NOPs needed for any purpose. */ 602 #define MAX_NOPS 4 603 604 /* A list of previous instructions, with index 0 being the most recent. 605 We need to look back MAX_NOPS instructions when filling delay slots 606 or working around processor errata. We need to look back one 607 instruction further if we're thinking about using history[0] to 608 fill a branch delay slot. */ 609 static struct mips_cl_insn history[1 + MAX_NOPS]; 610 611 /* Nop instructions used by emit_nop. */ 612 static struct mips_cl_insn nop_insn, mips16_nop_insn; 613 614 /* The appropriate nop for the current mode. */ 615 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn : &nop_insn) 616 617 /* If this is set, it points to a frag holding nop instructions which 618 were inserted before the start of a noreorder section. If those 619 nops turn out to be unnecessary, the size of the frag can be 620 decreased. */ 621 static fragS *prev_nop_frag; 622 623 /* The number of nop instructions we created in prev_nop_frag. */ 624 static int prev_nop_frag_holds; 625 626 /* The number of nop instructions that we know we need in 627 prev_nop_frag. */ 628 static int prev_nop_frag_required; 629 630 /* The number of instructions we've seen since prev_nop_frag. */ 631 static int prev_nop_frag_since; 632 633 /* For ECOFF and ELF, relocations against symbols are done in two 634 parts, with a HI relocation and a LO relocation. Each relocation 635 has only 16 bits of space to store an addend. This means that in 636 order for the linker to handle carries correctly, it must be able 637 to locate both the HI and the LO relocation. This means that the 638 relocations must appear in order in the relocation table. 639 640 In order to implement this, we keep track of each unmatched HI 641 relocation. We then sort them so that they immediately precede the 642 corresponding LO relocation. */ 643 644 struct mips_hi_fixup 645 { 646 /* Next HI fixup. */ 647 struct mips_hi_fixup *next; 648 /* This fixup. */ 649 fixS *fixp; 650 /* The section this fixup is in. */ 651 segT seg; 652 }; 653 654 /* The list of unmatched HI relocs. */ 655 656 static struct mips_hi_fixup *mips_hi_fixup_list; 657 658 /* The frag containing the last explicit relocation operator. 659 Null if explicit relocations have not been used. */ 660 661 static fragS *prev_reloc_op_frag; 662 663 /* Map normal MIPS register numbers to mips16 register numbers. */ 664 665 #define X ILLEGAL_REG 666 static const int mips32_to_16_reg_map[] = 667 { 668 X, X, 2, 3, 4, 5, 6, 7, 669 X, X, X, X, X, X, X, X, 670 0, 1, X, X, X, X, X, X, 671 X, X, X, X, X, X, X, X 672 }; 673 #undef X 674 675 /* Map mips16 register numbers to normal MIPS register numbers. */ 676 677 static const unsigned int mips16_to_32_reg_map[] = 678 { 679 16, 17, 2, 3, 4, 5, 6, 7 680 }; 681 682 /* Classifies the kind of instructions we're interested in when 683 implementing -mfix-vr4120. */ 684 enum fix_vr4120_class { 685 FIX_VR4120_MACC, 686 FIX_VR4120_DMACC, 687 FIX_VR4120_MULT, 688 FIX_VR4120_DMULT, 689 FIX_VR4120_DIV, 690 FIX_VR4120_MTHILO, 691 NUM_FIX_VR4120_CLASSES 692 }; 693 694 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if 695 there must be at least one other instruction between an instruction 696 of type X and an instruction of type Y. */ 697 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES]; 698 699 /* True if -mfix-vr4120 is in force. */ 700 static int mips_fix_vr4120; 701 702 /* ...likewise -mfix-vr4130. */ 703 static int mips_fix_vr4130; 704 705 /* We don't relax branches by default, since this causes us to expand 706 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we 707 fail to compute the offset before expanding the macro to the most 708 efficient expansion. */ 709 710 static int mips_relax_branch; 711 712 static int mips_fix_loongson2f_btb; 713 714 /* The expansion of many macros depends on the type of symbol that 715 they refer to. For example, when generating position-dependent code, 716 a macro that refers to a symbol may have two different expansions, 717 one which uses GP-relative addresses and one which uses absolute 718 addresses. When generating SVR4-style PIC, a macro may have 719 different expansions for local and global symbols. 720 721 We handle these situations by generating both sequences and putting 722 them in variant frags. In position-dependent code, the first sequence 723 will be the GP-relative one and the second sequence will be the 724 absolute one. In SVR4 PIC, the first sequence will be for global 725 symbols and the second will be for local symbols. 726 727 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and 728 SECOND are the lengths of the two sequences in bytes. These fields 729 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition, 730 the subtype has the following flags: 731 732 RELAX_USE_SECOND 733 Set if it has been decided that we should use the second 734 sequence instead of the first. 735 736 RELAX_SECOND_LONGER 737 Set in the first variant frag if the macro's second implementation 738 is longer than its first. This refers to the macro as a whole, 739 not an individual relaxation. 740 741 RELAX_NOMACRO 742 Set in the first variant frag if the macro appeared in a .set nomacro 743 block and if one alternative requires a warning but the other does not. 744 745 RELAX_DELAY_SLOT 746 Like RELAX_NOMACRO, but indicates that the macro appears in a branch 747 delay slot. 748 749 The frag's "opcode" points to the first fixup for relaxable code. 750 751 Relaxable macros are generated using a sequence such as: 752 753 relax_start (SYMBOL); 754 ... generate first expansion ... 755 relax_switch (); 756 ... generate second expansion ... 757 relax_end (); 758 759 The code and fixups for the unwanted alternative are discarded 760 by md_convert_frag. */ 761 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND)) 762 763 #define RELAX_FIRST(X) (((X) >> 8) & 0xff) 764 #define RELAX_SECOND(X) ((X) & 0xff) 765 #define RELAX_USE_SECOND 0x10000 766 #define RELAX_SECOND_LONGER 0x20000 767 #define RELAX_NOMACRO 0x40000 768 #define RELAX_DELAY_SLOT 0x80000 769 770 /* Branch without likely bit. If label is out of range, we turn: 771 772 beq reg1, reg2, label 773 delay slot 774 775 into 776 777 bne reg1, reg2, 0f 778 nop 779 j label 780 0: delay slot 781 782 with the following opcode replacements: 783 784 beq <-> bne 785 blez <-> bgtz 786 bltz <-> bgez 787 bc1f <-> bc1t 788 789 bltzal <-> bgezal (with jal label instead of j label) 790 791 Even though keeping the delay slot instruction in the delay slot of 792 the branch would be more efficient, it would be very tricky to do 793 correctly, because we'd have to introduce a variable frag *after* 794 the delay slot instruction, and expand that instead. Let's do it 795 the easy way for now, even if the branch-not-taken case now costs 796 one additional instruction. Out-of-range branches are not supposed 797 to be common, anyway. 798 799 Branch likely. If label is out of range, we turn: 800 801 beql reg1, reg2, label 802 delay slot (annulled if branch not taken) 803 804 into 805 806 beql reg1, reg2, 1f 807 nop 808 beql $0, $0, 2f 809 nop 810 1: j[al] label 811 delay slot (executed only if branch taken) 812 2: 813 814 It would be possible to generate a shorter sequence by losing the 815 likely bit, generating something like: 816 817 bne reg1, reg2, 0f 818 nop 819 j[al] label 820 delay slot (executed only if branch taken) 821 0: 822 823 beql -> bne 824 bnel -> beq 825 blezl -> bgtz 826 bgtzl -> blez 827 bltzl -> bgez 828 bgezl -> bltz 829 bc1fl -> bc1t 830 bc1tl -> bc1f 831 832 bltzall -> bgezal (with jal label instead of j label) 833 bgezall -> bltzal (ditto) 834 835 836 but it's not clear that it would actually improve performance. */ 837 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \ 838 ((relax_substateT) \ 839 (0xc0000000 \ 840 | ((toofar) ? 1 : 0) \ 841 | ((link) ? 2 : 0) \ 842 | ((likely) ? 4 : 0) \ 843 | ((uncond) ? 8 : 0))) 844 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000) 845 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0) 846 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0) 847 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0) 848 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0) 849 850 /* For mips16 code, we use an entirely different form of relaxation. 851 mips16 supports two versions of most instructions which take 852 immediate values: a small one which takes some small value, and a 853 larger one which takes a 16 bit value. Since branches also follow 854 this pattern, relaxing these values is required. 855 856 We can assemble both mips16 and normal MIPS code in a single 857 object. Therefore, we need to support this type of relaxation at 858 the same time that we support the relaxation described above. We 859 use the high bit of the subtype field to distinguish these cases. 860 861 The information we store for this type of relaxation is the 862 argument code found in the opcode file for this relocation, whether 863 the user explicitly requested a small or extended form, and whether 864 the relocation is in a jump or jal delay slot. That tells us the 865 size of the value, and how it should be stored. We also store 866 whether the fragment is considered to be extended or not. We also 867 store whether this is known to be a branch to a different section, 868 whether we have tried to relax this frag yet, and whether we have 869 ever extended a PC relative fragment because of a shift count. */ 870 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \ 871 (0x80000000 \ 872 | ((type) & 0xff) \ 873 | ((small) ? 0x100 : 0) \ 874 | ((ext) ? 0x200 : 0) \ 875 | ((dslot) ? 0x400 : 0) \ 876 | ((jal_dslot) ? 0x800 : 0)) 877 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000) 878 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff) 879 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0) 880 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0) 881 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0) 882 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0) 883 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0) 884 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000) 885 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000) 886 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0) 887 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000) 888 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000) 889 890 /* Is the given value a sign-extended 32-bit value? */ 891 #define IS_SEXT_32BIT_NUM(x) \ 892 (((x) &~ (offsetT) 0x7fffffff) == 0 \ 893 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff)) 894 895 /* Is the given value a sign-extended 16-bit value? */ 896 #define IS_SEXT_16BIT_NUM(x) \ 897 (((x) &~ (offsetT) 0x7fff) == 0 \ 898 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff)) 899 900 /* Is the given value a zero-extended 32-bit value? Or a negated one? */ 901 #define IS_ZEXT_32BIT_NUM(x) \ 902 (((x) &~ (offsetT) 0xffffffff) == 0 \ 903 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff)) 904 905 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in 906 VALUE << SHIFT. VALUE is evaluated exactly once. */ 907 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \ 908 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \ 909 | (((VALUE) & (MASK)) << (SHIFT))) 910 911 /* Extract bits MASK << SHIFT from STRUCT and shift them right 912 SHIFT places. */ 913 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \ 914 (((STRUCT) >> (SHIFT)) & (MASK)) 915 916 /* Change INSN's opcode so that the operand given by FIELD has value VALUE. 917 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once. 918 919 include/opcode/mips.h specifies operand fields using the macros 920 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start 921 with "MIPS16OP" instead of "OP". */ 922 #define INSERT_OPERAND(FIELD, INSN, VALUE) \ 923 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD) 924 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \ 925 INSERT_BITS ((INSN).insn_opcode, VALUE, \ 926 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD) 927 928 /* Extract the operand given by FIELD from mips_cl_insn INSN. */ 929 #define EXTRACT_OPERAND(FIELD, INSN) \ 930 EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) 931 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \ 932 EXTRACT_BITS ((INSN).insn_opcode, \ 933 MIPS16OP_MASK_##FIELD, \ 934 MIPS16OP_SH_##FIELD) 935 936 /* Global variables used when generating relaxable macros. See the 937 comment above RELAX_ENCODE for more details about how relaxation 938 is used. */ 939 static struct { 940 /* 0 if we're not emitting a relaxable macro. 941 1 if we're emitting the first of the two relaxation alternatives. 942 2 if we're emitting the second alternative. */ 943 int sequence; 944 945 /* The first relaxable fixup in the current frag. (In other words, 946 the first fixup that refers to relaxable code.) */ 947 fixS *first_fixup; 948 949 /* sizes[0] says how many bytes of the first alternative are stored in 950 the current frag. Likewise sizes[1] for the second alternative. */ 951 unsigned int sizes[2]; 952 953 /* The symbol on which the choice of sequence depends. */ 954 symbolS *symbol; 955 } mips_relax; 956 957 /* Global variables used to decide whether a macro needs a warning. */ 958 static struct { 959 /* True if the macro is in a branch delay slot. */ 960 bfd_boolean delay_slot_p; 961 962 /* For relaxable macros, sizes[0] is the length of the first alternative 963 in bytes and sizes[1] is the length of the second alternative. 964 For non-relaxable macros, both elements give the length of the 965 macro in bytes. */ 966 unsigned int sizes[2]; 967 968 /* The first variant frag for this macro. */ 969 fragS *first_frag; 970 } mips_macro_warning; 971 972 /* Prototypes for static functions. */ 973 974 #define internalError() \ 975 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__) 976 977 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG }; 978 979 static void append_insn 980 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r); 981 static void mips_no_prev_insn (void); 982 static void mips16_macro_build 983 (expressionS *, const char *, const char *, va_list); 984 static void load_register (int, expressionS *, int); 985 static void macro_build (expressionS *, const char *, const char *, ...); 986 static void macro_start (void); 987 static void macro_end (void); 988 static void macro (struct mips_cl_insn * ip); 989 static void mips16_macro (struct mips_cl_insn * ip); 990 #ifdef LOSING_COMPILER 991 static void macro2 (struct mips_cl_insn * ip); 992 #endif 993 static void mips_ip (char *str, struct mips_cl_insn * ip); 994 static void mips16_ip (char *str, struct mips_cl_insn * ip); 995 static void mips16_immed 996 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean, 997 unsigned long *, bfd_boolean *, unsigned short *); 998 static size_t my_getSmallExpression 999 (expressionS *, bfd_reloc_code_real_type *, char *); 1000 static void my_getExpression (expressionS *, char *); 1001 static void s_align (int); 1002 static void s_change_sec (int); 1003 static void s_change_section (int); 1004 static void s_cons (int); 1005 static void s_float_cons (int); 1006 static void s_mips_globl (int); 1007 static void s_option (int); 1008 static void s_mipsset (int); 1009 static void s_abicalls (int); 1010 static void s_cpload (int); 1011 static void s_cpsetup (int); 1012 static void s_cplocal (int); 1013 static void s_cprestore (int); 1014 static void s_cpreturn (int); 1015 static void s_gpvalue (int); 1016 static void s_gpword (int); 1017 static void s_gpdword (int); 1018 static void s_cpadd (int); 1019 static void s_insn (int); 1020 static void md_obj_begin (void); 1021 static void md_obj_end (void); 1022 static void s_mips_ent (int); 1023 static void s_mips_end (int); 1024 static void s_mips_frame (int); 1025 static void s_mips_mask (int reg_type); 1026 static void s_mips_stab (int); 1027 static void s_mips_weakext (int); 1028 static void s_mips_file (int); 1029 static void s_mips_loc (int); 1030 static bfd_boolean pic_need_relax (symbolS *, asection *); 1031 static int relaxed_branch_length (fragS *, asection *, int); 1032 static int validate_mips_insn (const struct mips_opcode *); 1033 1034 /* Table and functions used to map between CPU/ISA names, and 1035 ISA levels, and CPU numbers. */ 1036 1037 struct mips_cpu_info 1038 { 1039 const char *name; /* CPU or ISA name. */ 1040 int flags; /* ASEs available, or ISA flag. */ 1041 int isa; /* ISA level. */ 1042 int cpu; /* CPU number (default CPU if ISA). */ 1043 }; 1044 1045 #define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */ 1046 #define MIPS_CPU_ASE_SMARTMIPS 0x0002 /* CPU implements SmartMIPS ASE */ 1047 #define MIPS_CPU_ASE_DSP 0x0004 /* CPU implements DSP ASE */ 1048 #define MIPS_CPU_ASE_MT 0x0008 /* CPU implements MT ASE */ 1049 #define MIPS_CPU_ASE_MIPS3D 0x0010 /* CPU implements MIPS-3D ASE */ 1050 #define MIPS_CPU_ASE_MDMX 0x0020 /* CPU implements MDMX ASE */ 1051 1052 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *); 1053 static const struct mips_cpu_info *mips_cpu_info_from_isa (int); 1054 static const struct mips_cpu_info *mips_cpu_info_from_arch (int); 1055 1056 /* Pseudo-op table. 1057 1058 The following pseudo-ops from the Kane and Heinrich MIPS book 1059 should be defined here, but are currently unsupported: .alias, 1060 .galive, .gjaldef, .gjrlive, .livereg, .noalias. 1061 1062 The following pseudo-ops from the Kane and Heinrich MIPS book are 1063 specific to the type of debugging information being generated, and 1064 should be defined by the object format: .aent, .begin, .bend, 1065 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp, 1066 .vreg. 1067 1068 The following pseudo-ops from the Kane and Heinrich MIPS book are 1069 not MIPS CPU specific, but are also not specific to the object file 1070 format. This file is probably the best place to define them, but 1071 they are not currently supported: .asm0, .endr, .lab, .repeat, 1072 .struct. */ 1073 1074 static const pseudo_typeS mips_pseudo_table[] = 1075 { 1076 /* MIPS specific pseudo-ops. */ 1077 {"option", s_option, 0}, 1078 {"set", s_mipsset, 0}, 1079 {"rdata", s_change_sec, 'r'}, 1080 {"sdata", s_change_sec, 's'}, 1081 {"livereg", s_ignore, 0}, 1082 {"abicalls", s_abicalls, 0}, 1083 {"cpload", s_cpload, 0}, 1084 {"cpsetup", s_cpsetup, 0}, 1085 {"cplocal", s_cplocal, 0}, 1086 {"cprestore", s_cprestore, 0}, 1087 {"cpreturn", s_cpreturn, 0}, 1088 {"gpvalue", s_gpvalue, 0}, 1089 {"gpword", s_gpword, 0}, 1090 {"gpdword", s_gpdword, 0}, 1091 {"cpadd", s_cpadd, 0}, 1092 {"insn", s_insn, 0}, 1093 1094 /* Relatively generic pseudo-ops that happen to be used on MIPS 1095 chips. */ 1096 {"asciiz", stringer, 1}, 1097 {"bss", s_change_sec, 'b'}, 1098 {"err", s_err, 0}, 1099 {"half", s_cons, 1}, 1100 {"dword", s_cons, 3}, 1101 {"weakext", s_mips_weakext, 0}, 1102 1103 /* These pseudo-ops are defined in read.c, but must be overridden 1104 here for one reason or another. */ 1105 {"align", s_align, 0}, 1106 {"byte", s_cons, 0}, 1107 {"data", s_change_sec, 'd'}, 1108 {"double", s_float_cons, 'd'}, 1109 {"float", s_float_cons, 'f'}, 1110 {"globl", s_mips_globl, 0}, 1111 {"global", s_mips_globl, 0}, 1112 {"hword", s_cons, 1}, 1113 {"int", s_cons, 2}, 1114 {"long", s_cons, 2}, 1115 {"octa", s_cons, 4}, 1116 {"quad", s_cons, 3}, 1117 {"section", s_change_section, 0}, 1118 {"short", s_cons, 1}, 1119 {"single", s_float_cons, 'f'}, 1120 {"stabn", s_mips_stab, 'n'}, 1121 {"text", s_change_sec, 't'}, 1122 {"word", s_cons, 2}, 1123 1124 { "extern", ecoff_directive_extern, 0}, 1125 1126 { NULL, NULL, 0 }, 1127 }; 1128 1129 static const pseudo_typeS mips_nonecoff_pseudo_table[] = 1130 { 1131 /* These pseudo-ops should be defined by the object file format. 1132 However, a.out doesn't support them, so we have versions here. */ 1133 {"aent", s_mips_ent, 1}, 1134 {"bgnb", s_ignore, 0}, 1135 {"end", s_mips_end, 0}, 1136 {"endb", s_ignore, 0}, 1137 {"ent", s_mips_ent, 0}, 1138 {"file", s_mips_file, 0}, 1139 {"fmask", s_mips_mask, 'F'}, 1140 {"frame", s_mips_frame, 0}, 1141 {"loc", s_mips_loc, 0}, 1142 {"mask", s_mips_mask, 'R'}, 1143 {"verstamp", s_ignore, 0}, 1144 { NULL, NULL, 0 }, 1145 }; 1146 1147 extern void pop_insert (const pseudo_typeS *); 1148 1149 void 1150 mips_pop_insert (void) 1151 { 1152 pop_insert (mips_pseudo_table); 1153 if (! ECOFF_DEBUGGING) 1154 pop_insert (mips_nonecoff_pseudo_table); 1155 } 1156 1157 /* Symbols labelling the current insn. */ 1158 1159 struct insn_label_list 1160 { 1161 struct insn_label_list *next; 1162 symbolS *label; 1163 }; 1164 1165 static struct insn_label_list *insn_labels; 1166 static struct insn_label_list *free_insn_labels; 1167 1168 static void mips_clear_insn_labels (void); 1169 1170 static inline void 1171 mips_clear_insn_labels (void) 1172 { 1173 register struct insn_label_list **pl; 1174 1175 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next) 1176 ; 1177 *pl = insn_labels; 1178 insn_labels = NULL; 1179 } 1180 1181 static char *expr_end; 1182 1183 /* Expressions which appear in instructions. These are set by 1184 mips_ip. */ 1185 1186 static expressionS imm_expr; 1187 static expressionS imm2_expr; 1188 static expressionS offset_expr; 1189 1190 /* Relocs associated with imm_expr and offset_expr. */ 1191 1192 static bfd_reloc_code_real_type imm_reloc[3] 1193 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED}; 1194 static bfd_reloc_code_real_type offset_reloc[3] 1195 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED}; 1196 1197 /* These are set by mips16_ip if an explicit extension is used. */ 1198 1199 static bfd_boolean mips16_small, mips16_ext; 1200 1201 #ifdef OBJ_ELF 1202 /* The pdr segment for per procedure frame/regmask info. Not used for 1203 ECOFF debugging. */ 1204 1205 static segT pdr_seg; 1206 #endif 1207 1208 /* The default target format to use. */ 1209 1210 const char * 1211 mips_target_format (void) 1212 { 1213 switch (OUTPUT_FLAVOR) 1214 { 1215 case bfd_target_ecoff_flavour: 1216 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT; 1217 case bfd_target_coff_flavour: 1218 return "pe-mips"; 1219 case bfd_target_elf_flavour: 1220 #ifdef TE_VXWORKS 1221 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI) 1222 return (target_big_endian 1223 ? "elf32-bigmips-vxworks" 1224 : "elf32-littlemips-vxworks"); 1225 #endif 1226 #ifdef TE_TMIPS 1227 /* This is traditional mips. */ 1228 return (target_big_endian 1229 ? (HAVE_64BIT_OBJECTS 1230 ? "elf64-tradbigmips" 1231 : (HAVE_NEWABI 1232 ? "elf32-ntradbigmips" : "elf32-tradbigmips")) 1233 : (HAVE_64BIT_OBJECTS 1234 ? "elf64-tradlittlemips" 1235 : (HAVE_NEWABI 1236 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips"))); 1237 #else 1238 return (target_big_endian 1239 ? (HAVE_64BIT_OBJECTS 1240 ? "elf64-bigmips" 1241 : (HAVE_NEWABI 1242 ? "elf32-nbigmips" : "elf32-bigmips")) 1243 : (HAVE_64BIT_OBJECTS 1244 ? "elf64-littlemips" 1245 : (HAVE_NEWABI 1246 ? "elf32-nlittlemips" : "elf32-littlemips"))); 1247 #endif 1248 default: 1249 abort (); 1250 return NULL; 1251 } 1252 } 1253 1254 /* Return the length of instruction INSN. */ 1255 1256 static inline unsigned int 1257 insn_length (const struct mips_cl_insn *insn) 1258 { 1259 if (!mips_opts.mips16) 1260 return 4; 1261 return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2; 1262 } 1263 1264 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */ 1265 1266 static void 1267 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo) 1268 { 1269 size_t i; 1270 1271 insn->insn_mo = mo; 1272 insn->use_extend = FALSE; 1273 insn->extend = 0; 1274 insn->insn_opcode = mo->match; 1275 insn->frag = NULL; 1276 insn->where = 0; 1277 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++) 1278 insn->fixp[i] = NULL; 1279 insn->fixed_p = (mips_opts.noreorder > 0); 1280 insn->noreorder_p = (mips_opts.noreorder > 0); 1281 insn->mips16_absolute_jump_p = 0; 1282 } 1283 1284 /* Install INSN at the location specified by its "frag" and "where" fields. */ 1285 1286 static void 1287 install_insn (const struct mips_cl_insn *insn) 1288 { 1289 char *f = insn->frag->fr_literal + insn->where; 1290 if (!mips_opts.mips16) 1291 md_number_to_chars (f, insn->insn_opcode, 4); 1292 else if (insn->mips16_absolute_jump_p) 1293 { 1294 md_number_to_chars (f, insn->insn_opcode >> 16, 2); 1295 md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2); 1296 } 1297 else 1298 { 1299 if (insn->use_extend) 1300 { 1301 md_number_to_chars (f, 0xf000 | insn->extend, 2); 1302 f += 2; 1303 } 1304 md_number_to_chars (f, insn->insn_opcode, 2); 1305 } 1306 } 1307 1308 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly 1309 and install the opcode in the new location. */ 1310 1311 static void 1312 move_insn (struct mips_cl_insn *insn, fragS *frag, long where) 1313 { 1314 size_t i; 1315 1316 insn->frag = frag; 1317 insn->where = where; 1318 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++) 1319 if (insn->fixp[i] != NULL) 1320 { 1321 insn->fixp[i]->fx_frag = frag; 1322 insn->fixp[i]->fx_where = where; 1323 } 1324 install_insn (insn); 1325 } 1326 1327 /* Add INSN to the end of the output. */ 1328 1329 static void 1330 add_fixed_insn (struct mips_cl_insn *insn) 1331 { 1332 char *f = frag_more (insn_length (insn)); 1333 move_insn (insn, frag_now, f - frag_now->fr_literal); 1334 } 1335 1336 /* Start a variant frag and move INSN to the start of the variant part, 1337 marking it as fixed. The other arguments are as for frag_var. */ 1338 1339 static void 1340 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var, 1341 relax_substateT subtype, symbolS *symbol, offsetT offset) 1342 { 1343 frag_grow (max_chars); 1344 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal); 1345 insn->fixed_p = 1; 1346 frag_var (rs_machine_dependent, max_chars, var, 1347 subtype, symbol, offset, NULL); 1348 } 1349 1350 /* Insert N copies of INSN into the history buffer, starting at 1351 position FIRST. Neither FIRST nor N need to be clipped. */ 1352 1353 static void 1354 insert_into_history (unsigned int first, unsigned int n, 1355 const struct mips_cl_insn *insn) 1356 { 1357 if (mips_relax.sequence != 2) 1358 { 1359 unsigned int i; 1360 1361 for (i = ARRAY_SIZE (history); i-- > first;) 1362 if (i >= first + n) 1363 history[i] = history[i - n]; 1364 else 1365 history[i] = *insn; 1366 } 1367 } 1368 1369 /* Emit a nop instruction, recording it in the history buffer. */ 1370 1371 static void 1372 emit_nop (void) 1373 { 1374 add_fixed_insn (NOP_INSN); 1375 insert_into_history (0, 1, NOP_INSN); 1376 } 1377 1378 /* Initialize vr4120_conflicts. There is a bit of duplication here: 1379 the idea is to make it obvious at a glance that each errata is 1380 included. */ 1381 1382 static void 1383 init_vr4120_conflicts (void) 1384 { 1385 #define CONFLICT(FIRST, SECOND) \ 1386 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND 1387 1388 /* Errata 21 - [D]DIV[U] after [D]MACC */ 1389 CONFLICT (MACC, DIV); 1390 CONFLICT (DMACC, DIV); 1391 1392 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */ 1393 CONFLICT (DMULT, DMULT); 1394 CONFLICT (DMULT, DMACC); 1395 CONFLICT (DMACC, DMULT); 1396 CONFLICT (DMACC, DMACC); 1397 1398 /* Errata 24 - MT{LO,HI} after [D]MACC */ 1399 CONFLICT (MACC, MTHILO); 1400 CONFLICT (DMACC, MTHILO); 1401 1402 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU 1403 instruction is executed immediately after a MACC or DMACC 1404 instruction, the result of [either instruction] is incorrect." */ 1405 CONFLICT (MACC, MULT); 1406 CONFLICT (MACC, DMULT); 1407 CONFLICT (DMACC, MULT); 1408 CONFLICT (DMACC, DMULT); 1409 1410 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is 1411 executed immediately after a DMULT, DMULTU, DIV, DIVU, 1412 DDIV or DDIVU instruction, the result of the MACC or 1413 DMACC instruction is incorrect.". */ 1414 CONFLICT (DMULT, MACC); 1415 CONFLICT (DMULT, DMACC); 1416 CONFLICT (DIV, MACC); 1417 CONFLICT (DIV, DMACC); 1418 1419 #undef CONFLICT 1420 } 1421 1422 struct regname { 1423 const char *name; 1424 unsigned int num; 1425 }; 1426 1427 #define RTYPE_MASK 0x1ff00 1428 #define RTYPE_NUM 0x00100 1429 #define RTYPE_FPU 0x00200 1430 #define RTYPE_FCC 0x00400 1431 #define RTYPE_VEC 0x00800 1432 #define RTYPE_GP 0x01000 1433 #define RTYPE_CP0 0x02000 1434 #define RTYPE_PC 0x04000 1435 #define RTYPE_ACC 0x08000 1436 #define RTYPE_CCC 0x10000 1437 #define RNUM_MASK 0x000ff 1438 #define RWARN 0x80000 1439 1440 #define GENERIC_REGISTER_NUMBERS \ 1441 {"$0", RTYPE_NUM | 0}, \ 1442 {"$1", RTYPE_NUM | 1}, \ 1443 {"$2", RTYPE_NUM | 2}, \ 1444 {"$3", RTYPE_NUM | 3}, \ 1445 {"$4", RTYPE_NUM | 4}, \ 1446 {"$5", RTYPE_NUM | 5}, \ 1447 {"$6", RTYPE_NUM | 6}, \ 1448 {"$7", RTYPE_NUM | 7}, \ 1449 {"$8", RTYPE_NUM | 8}, \ 1450 {"$9", RTYPE_NUM | 9}, \ 1451 {"$10", RTYPE_NUM | 10}, \ 1452 {"$11", RTYPE_NUM | 11}, \ 1453 {"$12", RTYPE_NUM | 12}, \ 1454 {"$13", RTYPE_NUM | 13}, \ 1455 {"$14", RTYPE_NUM | 14}, \ 1456 {"$15", RTYPE_NUM | 15}, \ 1457 {"$16", RTYPE_NUM | 16}, \ 1458 {"$17", RTYPE_NUM | 17}, \ 1459 {"$18", RTYPE_NUM | 18}, \ 1460 {"$19", RTYPE_NUM | 19}, \ 1461 {"$20", RTYPE_NUM | 20}, \ 1462 {"$21", RTYPE_NUM | 21}, \ 1463 {"$22", RTYPE_NUM | 22}, \ 1464 {"$23", RTYPE_NUM | 23}, \ 1465 {"$24", RTYPE_NUM | 24}, \ 1466 {"$25", RTYPE_NUM | 25}, \ 1467 {"$26", RTYPE_NUM | 26}, \ 1468 {"$27", RTYPE_NUM | 27}, \ 1469 {"$28", RTYPE_NUM | 28}, \ 1470 {"$29", RTYPE_NUM | 29}, \ 1471 {"$30", RTYPE_NUM | 30}, \ 1472 {"$31", RTYPE_NUM | 31} 1473 1474 #define FPU_REGISTER_NAMES \ 1475 {"$f0", RTYPE_FPU | 0}, \ 1476 {"$f1", RTYPE_FPU | 1}, \ 1477 {"$f2", RTYPE_FPU | 2}, \ 1478 {"$f3", RTYPE_FPU | 3}, \ 1479 {"$f4", RTYPE_FPU | 4}, \ 1480 {"$f5", RTYPE_FPU | 5}, \ 1481 {"$f6", RTYPE_FPU | 6}, \ 1482 {"$f7", RTYPE_FPU | 7}, \ 1483 {"$f8", RTYPE_FPU | 8}, \ 1484 {"$f9", RTYPE_FPU | 9}, \ 1485 {"$f10", RTYPE_FPU | 10}, \ 1486 {"$f11", RTYPE_FPU | 11}, \ 1487 {"$f12", RTYPE_FPU | 12}, \ 1488 {"$f13", RTYPE_FPU | 13}, \ 1489 {"$f14", RTYPE_FPU | 14}, \ 1490 {"$f15", RTYPE_FPU | 15}, \ 1491 {"$f16", RTYPE_FPU | 16}, \ 1492 {"$f17", RTYPE_FPU | 17}, \ 1493 {"$f18", RTYPE_FPU | 18}, \ 1494 {"$f19", RTYPE_FPU | 19}, \ 1495 {"$f20", RTYPE_FPU | 20}, \ 1496 {"$f21", RTYPE_FPU | 21}, \ 1497 {"$f22", RTYPE_FPU | 22}, \ 1498 {"$f23", RTYPE_FPU | 23}, \ 1499 {"$f24", RTYPE_FPU | 24}, \ 1500 {"$f25", RTYPE_FPU | 25}, \ 1501 {"$f26", RTYPE_FPU | 26}, \ 1502 {"$f27", RTYPE_FPU | 27}, \ 1503 {"$f28", RTYPE_FPU | 28}, \ 1504 {"$f29", RTYPE_FPU | 29}, \ 1505 {"$f30", RTYPE_FPU | 30}, \ 1506 {"$f31", RTYPE_FPU | 31} 1507 1508 #define FPU_CONDITION_CODE_NAMES \ 1509 {"$fcc0", RTYPE_FCC | 0}, \ 1510 {"$fcc1", RTYPE_FCC | 1}, \ 1511 {"$fcc2", RTYPE_FCC | 2}, \ 1512 {"$fcc3", RTYPE_FCC | 3}, \ 1513 {"$fcc4", RTYPE_FCC | 4}, \ 1514 {"$fcc5", RTYPE_FCC | 5}, \ 1515 {"$fcc6", RTYPE_FCC | 6}, \ 1516 {"$fcc7", RTYPE_FCC | 7} 1517 1518 #define COPROC_CONDITION_CODE_NAMES \ 1519 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \ 1520 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \ 1521 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \ 1522 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \ 1523 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \ 1524 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \ 1525 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \ 1526 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7} 1527 1528 #define N32N64_SYMBOLIC_REGISTER_NAMES \ 1529 {"$a4", RTYPE_GP | 8}, \ 1530 {"$a5", RTYPE_GP | 9}, \ 1531 {"$a6", RTYPE_GP | 10}, \ 1532 {"$a7", RTYPE_GP | 11}, \ 1533 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \ 1534 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \ 1535 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \ 1536 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \ 1537 {"$t0", RTYPE_GP | 12}, \ 1538 {"$t1", RTYPE_GP | 13}, \ 1539 {"$t2", RTYPE_GP | 14}, \ 1540 {"$t3", RTYPE_GP | 15} 1541 1542 #define O32_SYMBOLIC_REGISTER_NAMES \ 1543 {"$t0", RTYPE_GP | 8}, \ 1544 {"$t1", RTYPE_GP | 9}, \ 1545 {"$t2", RTYPE_GP | 10}, \ 1546 {"$t3", RTYPE_GP | 11}, \ 1547 {"$t4", RTYPE_GP | 12}, \ 1548 {"$t5", RTYPE_GP | 13}, \ 1549 {"$t6", RTYPE_GP | 14}, \ 1550 {"$t7", RTYPE_GP | 15}, \ 1551 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \ 1552 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \ 1553 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \ 1554 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */ 1555 1556 /* Remaining symbolic register names */ 1557 #define SYMBOLIC_REGISTER_NAMES \ 1558 {"$zero", RTYPE_GP | 0}, \ 1559 {"$at", RTYPE_GP | 1}, \ 1560 {"$AT", RTYPE_GP | 1}, \ 1561 {"$v0", RTYPE_GP | 2}, \ 1562 {"$v1", RTYPE_GP | 3}, \ 1563 {"$a0", RTYPE_GP | 4}, \ 1564 {"$a1", RTYPE_GP | 5}, \ 1565 {"$a2", RTYPE_GP | 6}, \ 1566 {"$a3", RTYPE_GP | 7}, \ 1567 {"$s0", RTYPE_GP | 16}, \ 1568 {"$s1", RTYPE_GP | 17}, \ 1569 {"$s2", RTYPE_GP | 18}, \ 1570 {"$s3", RTYPE_GP | 19}, \ 1571 {"$s4", RTYPE_GP | 20}, \ 1572 {"$s5", RTYPE_GP | 21}, \ 1573 {"$s6", RTYPE_GP | 22}, \ 1574 {"$s7", RTYPE_GP | 23}, \ 1575 {"$t8", RTYPE_GP | 24}, \ 1576 {"$t9", RTYPE_GP | 25}, \ 1577 {"$k0", RTYPE_GP | 26}, \ 1578 {"$kt0", RTYPE_GP | 26}, \ 1579 {"$k1", RTYPE_GP | 27}, \ 1580 {"$kt1", RTYPE_GP | 27}, \ 1581 {"$gp", RTYPE_GP | 28}, \ 1582 {"$sp", RTYPE_GP | 29}, \ 1583 {"$s8", RTYPE_GP | 30}, \ 1584 {"$fp", RTYPE_GP | 30}, \ 1585 {"$ra", RTYPE_GP | 31} 1586 1587 #define MIPS16_SPECIAL_REGISTER_NAMES \ 1588 {"$pc", RTYPE_PC | 0} 1589 1590 #define MDMX_VECTOR_REGISTER_NAMES \ 1591 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \ 1592 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \ 1593 {"$v2", RTYPE_VEC | 2}, \ 1594 {"$v3", RTYPE_VEC | 3}, \ 1595 {"$v4", RTYPE_VEC | 4}, \ 1596 {"$v5", RTYPE_VEC | 5}, \ 1597 {"$v6", RTYPE_VEC | 6}, \ 1598 {"$v7", RTYPE_VEC | 7}, \ 1599 {"$v8", RTYPE_VEC | 8}, \ 1600 {"$v9", RTYPE_VEC | 9}, \ 1601 {"$v10", RTYPE_VEC | 10}, \ 1602 {"$v11", RTYPE_VEC | 11}, \ 1603 {"$v12", RTYPE_VEC | 12}, \ 1604 {"$v13", RTYPE_VEC | 13}, \ 1605 {"$v14", RTYPE_VEC | 14}, \ 1606 {"$v15", RTYPE_VEC | 15}, \ 1607 {"$v16", RTYPE_VEC | 16}, \ 1608 {"$v17", RTYPE_VEC | 17}, \ 1609 {"$v18", RTYPE_VEC | 18}, \ 1610 {"$v19", RTYPE_VEC | 19}, \ 1611 {"$v20", RTYPE_VEC | 20}, \ 1612 {"$v21", RTYPE_VEC | 21}, \ 1613 {"$v22", RTYPE_VEC | 22}, \ 1614 {"$v23", RTYPE_VEC | 23}, \ 1615 {"$v24", RTYPE_VEC | 24}, \ 1616 {"$v25", RTYPE_VEC | 25}, \ 1617 {"$v26", RTYPE_VEC | 26}, \ 1618 {"$v27", RTYPE_VEC | 27}, \ 1619 {"$v28", RTYPE_VEC | 28}, \ 1620 {"$v29", RTYPE_VEC | 29}, \ 1621 {"$v30", RTYPE_VEC | 30}, \ 1622 {"$v31", RTYPE_VEC | 31} 1623 1624 #define MIPS_DSP_ACCUMULATOR_NAMES \ 1625 {"$ac0", RTYPE_ACC | 0}, \ 1626 {"$ac1", RTYPE_ACC | 1}, \ 1627 {"$ac2", RTYPE_ACC | 2}, \ 1628 {"$ac3", RTYPE_ACC | 3} 1629 1630 static const struct regname reg_names[] = { 1631 GENERIC_REGISTER_NUMBERS, 1632 FPU_REGISTER_NAMES, 1633 FPU_CONDITION_CODE_NAMES, 1634 COPROC_CONDITION_CODE_NAMES, 1635 1636 /* The $txx registers depends on the abi, 1637 these will be added later into the symbol table from 1638 one of the tables below once mips_abi is set after 1639 parsing of arguments from the command line. */ 1640 SYMBOLIC_REGISTER_NAMES, 1641 1642 MIPS16_SPECIAL_REGISTER_NAMES, 1643 MDMX_VECTOR_REGISTER_NAMES, 1644 MIPS_DSP_ACCUMULATOR_NAMES, 1645 {0, 0} 1646 }; 1647 1648 static const struct regname reg_names_o32[] = { 1649 O32_SYMBOLIC_REGISTER_NAMES, 1650 {0, 0} 1651 }; 1652 1653 static const struct regname reg_names_n32n64[] = { 1654 N32N64_SYMBOLIC_REGISTER_NAMES, 1655 {0, 0} 1656 }; 1657 1658 static int 1659 reg_lookup (char **s, unsigned int types, unsigned int *regnop) 1660 { 1661 symbolS *symbolP; 1662 char *e; 1663 char save_c; 1664 int reg = -1; 1665 1666 /* Find end of name. */ 1667 e = *s; 1668 if (is_name_beginner (*e)) 1669 ++e; 1670 while (is_part_of_name (*e)) 1671 ++e; 1672 1673 /* Terminate name. */ 1674 save_c = *e; 1675 *e = '\0'; 1676 1677 /* Look for a register symbol. */ 1678 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section) 1679 { 1680 int r = S_GET_VALUE (symbolP); 1681 if (r & types) 1682 reg = r & RNUM_MASK; 1683 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2)) 1684 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */ 1685 reg = (r & RNUM_MASK) - 2; 1686 } 1687 /* Else see if this is a register defined in an itbl entry. */ 1688 else if ((types & RTYPE_GP) && itbl_have_entries) 1689 { 1690 char *n = *s; 1691 unsigned long r; 1692 1693 if (*n == '$') 1694 ++n; 1695 if (itbl_get_reg_val (n, &r)) 1696 reg = r & RNUM_MASK; 1697 } 1698 1699 /* Advance to next token if a register was recognised. */ 1700 if (reg >= 0) 1701 *s = e; 1702 else if (types & RWARN) 1703 as_warn ("Unrecognized register name `%s'", *s); 1704 1705 *e = save_c; 1706 if (regnop) 1707 *regnop = reg; 1708 return reg >= 0; 1709 } 1710 1711 /* This function is called once, at assembler startup time. It should 1712 set up all the tables, etc. that the MD part of the assembler will need. */ 1713 1714 void 1715 md_begin (void) 1716 { 1717 register const char *retval = NULL; 1718 int i = 0; 1719 int broken = 0; 1720 1721 if (mips_pic != NO_PIC) 1722 { 1723 if (g_switch_seen && g_switch_value != 0) 1724 as_bad (_("-G may not be used in position-independent code")); 1725 g_switch_value = 0; 1726 } 1727 1728 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch)) 1729 as_warn (_("Could not set architecture and machine")); 1730 1731 op_hash = hash_new (); 1732 1733 for (i = 0; i < NUMOPCODES;) 1734 { 1735 const char *name = mips_opcodes[i].name; 1736 1737 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]); 1738 if (retval != NULL) 1739 { 1740 fprintf (stderr, _("internal error: can't hash `%s': %s\n"), 1741 mips_opcodes[i].name, retval); 1742 /* Probably a memory allocation problem? Give up now. */ 1743 as_fatal (_("Broken assembler. No assembly attempted.")); 1744 } 1745 do 1746 { 1747 if (mips_opcodes[i].pinfo != INSN_MACRO) 1748 { 1749 if (!validate_mips_insn (&mips_opcodes[i])) 1750 broken = 1; 1751 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0) 1752 { 1753 create_insn (&nop_insn, mips_opcodes + i); 1754 nop_insn.fixed_p = 1; 1755 } 1756 } 1757 ++i; 1758 } 1759 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name)); 1760 } 1761 1762 mips16_op_hash = hash_new (); 1763 1764 i = 0; 1765 while (i < bfd_mips16_num_opcodes) 1766 { 1767 const char *name = mips16_opcodes[i].name; 1768 1769 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]); 1770 if (retval != NULL) 1771 as_fatal (_("internal: can't hash `%s': %s"), 1772 mips16_opcodes[i].name, retval); 1773 do 1774 { 1775 if (mips16_opcodes[i].pinfo != INSN_MACRO 1776 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask) 1777 != mips16_opcodes[i].match)) 1778 { 1779 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"), 1780 mips16_opcodes[i].name, mips16_opcodes[i].args); 1781 broken = 1; 1782 } 1783 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0) 1784 { 1785 create_insn (&mips16_nop_insn, mips16_opcodes + i); 1786 mips16_nop_insn.fixed_p = 1; 1787 } 1788 ++i; 1789 } 1790 while (i < bfd_mips16_num_opcodes 1791 && strcmp (mips16_opcodes[i].name, name) == 0); 1792 } 1793 1794 if (broken) 1795 as_fatal (_("Broken assembler. No assembly attempted.")); 1796 1797 /* We add all the general register names to the symbol table. This 1798 helps us detect invalid uses of them. */ 1799 for (i = 0; reg_names[i].name; i++) 1800 symbol_table_insert (symbol_new (reg_names[i].name, reg_section, 1801 reg_names[i].num, // & RNUM_MASK, 1802 &zero_address_frag)); 1803 if (HAVE_NEWABI) 1804 for (i = 0; reg_names_n32n64[i].name; i++) 1805 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section, 1806 reg_names_n32n64[i].num, // & RNUM_MASK, 1807 &zero_address_frag)); 1808 else 1809 for (i = 0; reg_names_o32[i].name; i++) 1810 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section, 1811 reg_names_o32[i].num, // & RNUM_MASK, 1812 &zero_address_frag)); 1813 1814 mips_no_prev_insn (); 1815 1816 mips_gprmask = 0; 1817 mips_cprmask[0] = 0; 1818 mips_cprmask[1] = 0; 1819 mips_cprmask[2] = 0; 1820 mips_cprmask[3] = 0; 1821 1822 /* set the default alignment for the text section (2**2) */ 1823 record_alignment (text_section, 2); 1824 1825 bfd_set_gp_size (stdoutput, g_switch_value); 1826 1827 #ifdef OBJ_ELF 1828 if (OUTPUT_FLAVOR == bfd_target_elf_flavour) 1829 { 1830 /* On a native system other than VxWorks, sections must be aligned 1831 to 16 byte boundaries. When configured for an embedded ELF 1832 target, we don't bother. */ 1833 if (strcmp (TARGET_OS, "elf") != 0 1834 && strcmp (TARGET_OS, "vxworks") != 0) 1835 { 1836 (void) bfd_set_section_alignment (stdoutput, text_section, 4); 1837 (void) bfd_set_section_alignment (stdoutput, data_section, 4); 1838 (void) bfd_set_section_alignment (stdoutput, bss_section, 4); 1839 } 1840 1841 /* Create a .reginfo section for register masks and a .mdebug 1842 section for debugging information. */ 1843 { 1844 segT seg; 1845 subsegT subseg; 1846 flagword flags; 1847 segT sec; 1848 1849 seg = now_seg; 1850 subseg = now_subseg; 1851 1852 /* The ABI says this section should be loaded so that the 1853 running program can access it. However, we don't load it 1854 if we are configured for an embedded target */ 1855 flags = SEC_READONLY | SEC_DATA; 1856 if (strcmp (TARGET_OS, "elf") != 0) 1857 flags |= SEC_ALLOC | SEC_LOAD; 1858 1859 if (mips_abi != N64_ABI) 1860 { 1861 sec = subseg_new (".reginfo", (subsegT) 0); 1862 1863 bfd_set_section_flags (stdoutput, sec, flags); 1864 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2); 1865 1866 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo)); 1867 } 1868 else 1869 { 1870 /* The 64-bit ABI uses a .MIPS.options section rather than 1871 .reginfo section. */ 1872 sec = subseg_new (".MIPS.options", (subsegT) 0); 1873 bfd_set_section_flags (stdoutput, sec, flags); 1874 bfd_set_section_alignment (stdoutput, sec, 3); 1875 1876 /* Set up the option header. */ 1877 { 1878 Elf_Internal_Options opthdr; 1879 char *f; 1880 1881 opthdr.kind = ODK_REGINFO; 1882 opthdr.size = (sizeof (Elf_External_Options) 1883 + sizeof (Elf64_External_RegInfo)); 1884 opthdr.section = 0; 1885 opthdr.info = 0; 1886 f = frag_more (sizeof (Elf_External_Options)); 1887 bfd_mips_elf_swap_options_out (stdoutput, &opthdr, 1888 (Elf_External_Options *) f); 1889 1890 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo)); 1891 } 1892 } 1893 1894 if (ECOFF_DEBUGGING) 1895 { 1896 sec = subseg_new (".mdebug", (subsegT) 0); 1897 (void) bfd_set_section_flags (stdoutput, sec, 1898 SEC_HAS_CONTENTS | SEC_READONLY); 1899 (void) bfd_set_section_alignment (stdoutput, sec, 2); 1900 } 1901 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr) 1902 { 1903 pdr_seg = subseg_new (".pdr", (subsegT) 0); 1904 (void) bfd_set_section_flags (stdoutput, pdr_seg, 1905 SEC_READONLY | SEC_RELOC 1906 | SEC_DEBUGGING); 1907 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2); 1908 } 1909 1910 subseg_set (seg, subseg); 1911 } 1912 } 1913 #endif /* OBJ_ELF */ 1914 1915 if (! ECOFF_DEBUGGING) 1916 md_obj_begin (); 1917 1918 if (mips_fix_vr4120) 1919 init_vr4120_conflicts (); 1920 } 1921 1922 void 1923 md_mips_end (void) 1924 { 1925 if (! ECOFF_DEBUGGING) 1926 md_obj_end (); 1927 } 1928 1929 void 1930 md_assemble (char *str) 1931 { 1932 struct mips_cl_insn insn; 1933 bfd_reloc_code_real_type unused_reloc[3] 1934 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED}; 1935 1936 imm_expr.X_op = O_absent; 1937 imm2_expr.X_op = O_absent; 1938 offset_expr.X_op = O_absent; 1939 imm_reloc[0] = BFD_RELOC_UNUSED; 1940 imm_reloc[1] = BFD_RELOC_UNUSED; 1941 imm_reloc[2] = BFD_RELOC_UNUSED; 1942 offset_reloc[0] = BFD_RELOC_UNUSED; 1943 offset_reloc[1] = BFD_RELOC_UNUSED; 1944 offset_reloc[2] = BFD_RELOC_UNUSED; 1945 1946 if (mips_opts.mips16) 1947 mips16_ip (str, &insn); 1948 else 1949 { 1950 mips_ip (str, &insn); 1951 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"), 1952 str, insn.insn_opcode)); 1953 } 1954 1955 if (insn_error) 1956 { 1957 as_bad ("%s `%s'", insn_error, str); 1958 return; 1959 } 1960 1961 if (insn.insn_mo->pinfo == INSN_MACRO) 1962 { 1963 macro_start (); 1964 if (mips_opts.mips16) 1965 mips16_macro (&insn); 1966 else 1967 macro (&insn); 1968 macro_end (); 1969 } 1970 else 1971 { 1972 if (imm_expr.X_op != O_absent) 1973 append_insn (&insn, &imm_expr, imm_reloc); 1974 else if (offset_expr.X_op != O_absent) 1975 append_insn (&insn, &offset_expr, offset_reloc); 1976 else 1977 append_insn (&insn, NULL, unused_reloc); 1978 } 1979 } 1980 1981 /* Return true if the given relocation might need a matching %lo(). 1982 This is only "might" because SVR4 R_MIPS_GOT16 relocations only 1983 need a matching %lo() when applied to local symbols. */ 1984 1985 static inline bfd_boolean 1986 reloc_needs_lo_p (bfd_reloc_code_real_type reloc) 1987 { 1988 return (HAVE_IN_PLACE_ADDENDS 1989 && (reloc == BFD_RELOC_HI16_S 1990 || reloc == BFD_RELOC_MIPS16_HI16_S 1991 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo(); 1992 all GOT16 relocations evaluate to "G". */ 1993 || (reloc == BFD_RELOC_MIPS_GOT16 && mips_pic != VXWORKS_PIC))); 1994 } 1995 1996 /* Return true if the given fixup is followed by a matching R_MIPS_LO16 1997 relocation. */ 1998 1999 static inline bfd_boolean 2000 fixup_has_matching_lo_p (fixS *fixp) 2001 { 2002 return (fixp->fx_next != NULL 2003 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16 2004 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16) 2005 && fixp->fx_addsy == fixp->fx_next->fx_addsy 2006 && fixp->fx_offset == fixp->fx_next->fx_offset); 2007 } 2008 2009 /* See whether instruction IP reads register REG. CLASS is the type 2010 of register. */ 2011 2012 static int 2013 insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg, 2014 enum mips_regclass class) 2015 { 2016 if (class == MIPS16_REG) 2017 { 2018 assert (mips_opts.mips16); 2019 reg = mips16_to_32_reg_map[reg]; 2020 class = MIPS_GR_REG; 2021 } 2022 2023 /* Don't report on general register ZERO, since it never changes. */ 2024 if (class == MIPS_GR_REG && reg == ZERO) 2025 return 0; 2026 2027 if (class == MIPS_FP_REG) 2028 { 2029 assert (! mips_opts.mips16); 2030 /* If we are called with either $f0 or $f1, we must check $f0. 2031 This is not optimal, because it will introduce an unnecessary 2032 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would 2033 need to distinguish reading both $f0 and $f1 or just one of 2034 them. Note that we don't have to check the other way, 2035 because there is no instruction that sets both $f0 and $f1 2036 and requires a delay. */ 2037 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S) 2038 && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1) 2039 == (reg &~ (unsigned) 1))) 2040 return 1; 2041 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T) 2042 && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1) 2043 == (reg &~ (unsigned) 1))) 2044 return 1; 2045 } 2046 else if (! mips_opts.mips16) 2047 { 2048 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S) 2049 && EXTRACT_OPERAND (RS, *ip) == reg) 2050 return 1; 2051 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T) 2052 && EXTRACT_OPERAND (RT, *ip) == reg) 2053 return 1; 2054 } 2055 else 2056 { 2057 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X) 2058 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg) 2059 return 1; 2060 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y) 2061 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg) 2062 return 1; 2063 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z) 2064 && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)] 2065 == reg)) 2066 return 1; 2067 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG) 2068 return 1; 2069 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP) 2070 return 1; 2071 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA) 2072 return 1; 2073 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X) 2074 && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg) 2075 return 1; 2076 } 2077 2078 return 0; 2079 } 2080 2081 /* This function returns true if modifying a register requires a 2082 delay. */ 2083 2084 static int 2085 reg_needs_delay (unsigned int reg) 2086 { 2087 unsigned long prev_pinfo; 2088 2089 prev_pinfo = history[0].insn_mo->pinfo; 2090 if (! mips_opts.noreorder 2091 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY) 2092 && ! gpr_interlocks) 2093 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY) 2094 && ! cop_interlocks))) 2095 { 2096 /* A load from a coprocessor or from memory. All load delays 2097 delay the use of general register rt for one instruction. */ 2098 /* Itbl support may require additional care here. */ 2099 know (prev_pinfo & INSN_WRITE_GPR_T); 2100 if (reg == EXTRACT_OPERAND (RT, history[0])) 2101 return 1; 2102 } 2103 2104 return 0; 2105 } 2106 2107 /* Move all labels in insn_labels to the current insertion point. */ 2108 2109 static void 2110 mips_move_labels (void) 2111 { 2112 struct insn_label_list *l; 2113 valueT val; 2114 2115 for (l = insn_labels; l != NULL; l = l->next) 2116 { 2117 assert (S_GET_SEGMENT (l->label) == now_seg); 2118 symbol_set_frag (l->label, frag_now); 2119 val = (valueT) frag_now_fix (); 2120 /* mips16 text labels are stored as odd. */ 2121 if (mips_opts.mips16) 2122 ++val; 2123 S_SET_VALUE (l->label, val); 2124 } 2125 } 2126 2127 /* Mark instruction labels in mips16 mode. This permits the linker to 2128 handle them specially, such as generating jalx instructions when 2129 needed. We also make them odd for the duration of the assembly, in 2130 order to generate the right sort of code. We will make them even 2131 in the adjust_symtab routine, while leaving them marked. This is 2132 convenient for the debugger and the disassembler. The linker knows 2133 to make them odd again. */ 2134 2135 static void 2136 mips16_mark_labels (void) 2137 { 2138 if (mips_opts.mips16) 2139 { 2140 struct insn_label_list *l; 2141 valueT val; 2142 2143 for (l = insn_labels; l != NULL; l = l->next) 2144 { 2145 #ifdef OBJ_ELF 2146 if (OUTPUT_FLAVOR == bfd_target_elf_flavour) 2147 S_SET_OTHER (l->label, STO_MIPS16); 2148 #endif 2149 val = S_GET_VALUE (l->label); 2150 if ((val & 1) == 0) 2151 S_SET_VALUE (l->label, val + 1); 2152 } 2153 } 2154 } 2155 2156 /* End the current frag. Make it a variant frag and record the 2157 relaxation info. */ 2158 2159 static void 2160 relax_close_frag (void) 2161 { 2162 mips_macro_warning.first_frag = frag_now; 2163 frag_var (rs_machine_dependent, 0, 0, 2164 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]), 2165 mips_relax.symbol, 0, (char *) mips_relax.first_fixup); 2166 2167 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes)); 2168 mips_relax.first_fixup = 0; 2169 } 2170 2171 /* Start a new relaxation sequence whose expansion depends on SYMBOL. 2172 See the comment above RELAX_ENCODE for more details. */ 2173 2174 static void 2175 relax_start (symbolS *symbol) 2176 { 2177 assert (mips_relax.sequence == 0); 2178 mips_relax.sequence = 1; 2179 mips_relax.symbol = symbol; 2180 } 2181 2182 /* Start generating the second version of a relaxable sequence. 2183 See the comment above RELAX_ENCODE for more details. */ 2184 2185 static void 2186 relax_switch (void) 2187 { 2188 assert (mips_relax.sequence == 1); 2189 mips_relax.sequence = 2; 2190 } 2191 2192 /* End the current relaxable sequence. */ 2193 2194 static void 2195 relax_end (void) 2196 { 2197 assert (mips_relax.sequence == 2); 2198 relax_close_frag (); 2199 mips_relax.sequence = 0; 2200 } 2201 2202 /* Classify an instruction according to the FIX_VR4120_* enumeration. 2203 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected 2204 by VR4120 errata. */ 2205 2206 static unsigned int 2207 classify_vr4120_insn (const char *name) 2208 { 2209 if (strncmp (name, "macc", 4) == 0) 2210 return FIX_VR4120_MACC; 2211 if (strncmp (name, "dmacc", 5) == 0) 2212 return FIX_VR4120_DMACC; 2213 if (strncmp (name, "mult", 4) == 0) 2214 return FIX_VR4120_MULT; 2215 if (strncmp (name, "dmult", 5) == 0) 2216 return FIX_VR4120_DMULT; 2217 if (strstr (name, "div")) 2218 return FIX_VR4120_DIV; 2219 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0) 2220 return FIX_VR4120_MTHILO; 2221 return NUM_FIX_VR4120_CLASSES; 2222 } 2223 2224 /* Return the number of instructions that must separate INSN1 and INSN2, 2225 where INSN1 is the earlier instruction. Return the worst-case value 2226 for any INSN2 if INSN2 is null. */ 2227 2228 static unsigned int 2229 insns_between (const struct mips_cl_insn *insn1, 2230 const struct mips_cl_insn *insn2) 2231 { 2232 unsigned long pinfo1, pinfo2; 2233 2234 /* This function needs to know which pinfo flags are set for INSN2 2235 and which registers INSN2 uses. The former is stored in PINFO2 and 2236 the latter is tested via INSN2_USES_REG. If INSN2 is null, PINFO2 2237 will have every flag set and INSN2_USES_REG will always return true. */ 2238 pinfo1 = insn1->insn_mo->pinfo; 2239 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U; 2240 2241 #define INSN2_USES_REG(REG, CLASS) \ 2242 (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS)) 2243 2244 /* For most targets, write-after-read dependencies on the HI and LO 2245 registers must be separated by at least two instructions. */ 2246 if (!hilo_interlocks) 2247 { 2248 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO)) 2249 return 2; 2250 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI)) 2251 return 2; 2252 } 2253 2254 /* If we're working around r7000 errata, there must be two instructions 2255 between an mfhi or mflo and any instruction that uses the result. */ 2256 if (mips_7000_hilo_fix 2257 && MF_HILO_INSN (pinfo1) 2258 && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG)) 2259 return 2; 2260 2261 /* If working around VR4120 errata, check for combinations that need 2262 a single intervening instruction. */ 2263 if (mips_fix_vr4120) 2264 { 2265 unsigned int class1, class2; 2266 2267 class1 = classify_vr4120_insn (insn1->insn_mo->name); 2268 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0) 2269 { 2270 if (insn2 == NULL) 2271 return 1; 2272 class2 = classify_vr4120_insn (insn2->insn_mo->name); 2273 if (vr4120_conflicts[class1] & (1 << class2)) 2274 return 1; 2275 } 2276 } 2277 2278 if (!mips_opts.mips16) 2279 { 2280 /* Check for GPR or coprocessor load delays. All such delays 2281 are on the RT register. */ 2282 /* Itbl support may require additional care here. */ 2283 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY)) 2284 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY))) 2285 { 2286 know (pinfo1 & INSN_WRITE_GPR_T); 2287 if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG)) 2288 return 1; 2289 } 2290 2291 /* Check for generic coprocessor hazards. 2292 2293 This case is not handled very well. There is no special 2294 knowledge of CP0 handling, and the coprocessors other than 2295 the floating point unit are not distinguished at all. */ 2296 /* Itbl support may require additional care here. FIXME! 2297 Need to modify this to include knowledge about 2298 user specified delays! */ 2299 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY)) 2300 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY))) 2301 { 2302 /* Handle cases where INSN1 writes to a known general coprocessor 2303 register. There must be a one instruction delay before INSN2 2304 if INSN2 reads that register, otherwise no delay is needed. */ 2305 if (pinfo1 & INSN_WRITE_FPR_T) 2306 { 2307 if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG)) 2308 return 1; 2309 } 2310 else if (pinfo1 & INSN_WRITE_FPR_S) 2311 { 2312 if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG)) 2313 return 1; 2314 } 2315 else 2316 { 2317 /* Read-after-write dependencies on the control registers 2318 require a two-instruction gap. */ 2319 if ((pinfo1 & INSN_WRITE_COND_CODE) 2320 && (pinfo2 & INSN_READ_COND_CODE)) 2321 return 2; 2322 2323 /* We don't know exactly what INSN1 does. If INSN2 is 2324 also a coprocessor instruction, assume there must be 2325 a one instruction gap. */ 2326 if (pinfo2 & INSN_COP) 2327 return 1; 2328 } 2329 } 2330 2331 /* Check for read-after-write dependencies on the coprocessor 2332 control registers in cases where INSN1 does not need a general 2333 coprocessor delay. This means that INSN1 is a floating point 2334 comparison instruction. */ 2335 /* Itbl support may require additional care here. */ 2336 else if (!cop_interlocks 2337 && (pinfo1 & INSN_WRITE_COND_CODE) 2338 && (pinfo2 & INSN_READ_COND_CODE)) 2339 return 1; 2340 } 2341 2342 #undef INSN2_USES_REG 2343 2344 return 0; 2345 } 2346 2347 /* Return the number of nops that would be needed to work around the 2348 VR4130 mflo/mfhi errata if instruction INSN immediately followed 2349 the MAX_VR4130_NOPS instructions described by HISTORY. */ 2350 2351 static int 2352 nops_for_vr4130 (const struct mips_cl_insn *history, 2353 const struct mips_cl_insn *insn) 2354 { 2355 int i, j, reg; 2356 2357 /* Check if the instruction writes to HI or LO. MTHI and MTLO 2358 are not affected by the errata. */ 2359 if (insn != 0 2360 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0 2361 || strcmp (insn->insn_mo->name, "mtlo") == 0 2362 || strcmp (insn->insn_mo->name, "mthi") == 0)) 2363 return 0; 2364 2365 /* Search for the first MFLO or MFHI. */ 2366 for (i = 0; i < MAX_VR4130_NOPS; i++) 2367 if (!history[i].noreorder_p && MF_HILO_INSN (history[i].insn_mo->pinfo)) 2368 { 2369 /* Extract the destination register. */ 2370 if (mips_opts.mips16) 2371 reg = mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, history[i])]; 2372 else 2373 reg = EXTRACT_OPERAND (RD, history[i]); 2374 2375 /* No nops are needed if INSN reads that register. */ 2376 if (insn != NULL && insn_uses_reg (insn, reg, MIPS_GR_REG)) 2377 return 0; 2378 2379 /* ...or if any of the intervening instructions do. */ 2380 for (j = 0; j < i; j++) 2381 if (insn_uses_reg (&history[j], reg, MIPS_GR_REG)) 2382 return 0; 2383 2384 return MAX_VR4130_NOPS - i; 2385 } 2386 return 0; 2387 } 2388 2389 /* Return the number of nops that would be needed if instruction INSN 2390 immediately followed the MAX_NOPS instructions given by HISTORY, 2391 where HISTORY[0] is the most recent instruction. If INSN is null, 2392 return the worse-case number of nops for any instruction. */ 2393 2394 static int 2395 nops_for_insn (const struct mips_cl_insn *history, 2396 const struct mips_cl_insn *insn) 2397 { 2398 int i, nops, tmp_nops; 2399 2400 nops = 0; 2401 for (i = 0; i < MAX_DELAY_NOPS; i++) 2402 if (!history[i].noreorder_p) 2403 { 2404 tmp_nops = insns_between (history + i, insn) - i; 2405 if (tmp_nops > nops) 2406 nops = tmp_nops; 2407 } 2408 2409 if (mips_fix_vr4130) 2410 { 2411 tmp_nops = nops_for_vr4130 (history, insn); 2412 if (tmp_nops > nops) 2413 nops = tmp_nops; 2414 } 2415 2416 return nops; 2417 } 2418 2419 /* The variable arguments provide NUM_INSNS extra instructions that 2420 might be added to HISTORY. Return the largest number of nops that 2421 would be needed after the extended sequence. */ 2422 2423 static int 2424 nops_for_sequence (int num_insns, const struct mips_cl_insn *history, ...) 2425 { 2426 va_list args; 2427 struct mips_cl_insn buffer[MAX_NOPS]; 2428 struct mips_cl_insn *cursor; 2429 int nops; 2430 2431 va_start (args, history); 2432 cursor = buffer + num_insns; 2433 memcpy (cursor, history, (MAX_NOPS - num_insns) * sizeof (*cursor)); 2434 while (cursor > buffer) 2435 *--cursor = *va_arg (args, const struct mips_cl_insn *); 2436 2437 nops = nops_for_insn (buffer, NULL); 2438 va_end (args); 2439 return nops; 2440 } 2441 2442 /* Like nops_for_insn, but if INSN is a branch, take into account the 2443 worst-case delay for the branch target. */ 2444 2445 static int 2446 nops_for_insn_or_target (const struct mips_cl_insn *history, 2447 const struct mips_cl_insn *insn) 2448 { 2449 int nops, tmp_nops; 2450 2451 nops = nops_for_insn (history, insn); 2452 if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY 2453 | INSN_COND_BRANCH_DELAY 2454 | INSN_COND_BRANCH_LIKELY)) 2455 { 2456 tmp_nops = nops_for_sequence (2, history, insn, NOP_INSN); 2457 if (tmp_nops > nops) 2458 nops = tmp_nops; 2459 } 2460 else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH)) 2461 { 2462 tmp_nops = nops_for_sequence (1, history, insn); 2463 if (tmp_nops > nops) 2464 nops = tmp_nops; 2465 } 2466 return nops; 2467 } 2468 2469 /* Output an instruction. IP is the instruction information. 2470 ADDRESS_EXPR is an operand of the instruction to be used with 2471 RELOC_TYPE. */ 2472 2473 static void 2474 append_insn (struct mips_cl_insn *ip, expressionS *address_expr, 2475 bfd_reloc_code_real_type *reloc_type) 2476 { 2477 register unsigned long prev_pinfo, pinfo; 2478 relax_stateT prev_insn_frag_type = 0; 2479 bfd_boolean relaxed_branch = FALSE; 2480 2481 /* Mark instruction labels in mips16 mode. */ 2482 mips16_mark_labels (); 2483 2484 prev_pinfo = history[0].insn_mo->pinfo; 2485 pinfo = ip->insn_mo->pinfo; 2486 2487 if (mips_relax.sequence != 2 && !mips_opts.noreorder) 2488 { 2489 /* There are a lot of optimizations we could do that we don't. 2490 In particular, we do not, in general, reorder instructions. 2491 If you use gcc with optimization, it will reorder 2492 instructions and generally do much more optimization then we 2493 do here; repeating all that work in the assembler would only 2494 benefit hand written assembly code, and does not seem worth 2495 it. */ 2496 int nops = (mips_optimize == 0 2497 ? nops_for_insn (history, NULL) 2498 : nops_for_insn_or_target (history, ip)); 2499 if (nops > 0) 2500 { 2501 fragS *old_frag; 2502 unsigned long old_frag_offset; 2503 int i; 2504 2505 old_frag = frag_now; 2506 old_frag_offset = frag_now_fix (); 2507 2508 for (i = 0; i < nops; i++) 2509 emit_nop (); 2510 2511 if (listing) 2512 { 2513 listing_prev_line (); 2514 /* We may be at the start of a variant frag. In case we 2515 are, make sure there is enough space for the frag 2516 after the frags created by listing_prev_line. The 2517 argument to frag_grow here must be at least as large 2518 as the argument to all other calls to frag_grow in 2519 this file. We don't have to worry about being in the 2520 middle of a variant frag, because the variants insert 2521 all needed nop instructions themselves. */ 2522 frag_grow (40); 2523 } 2524 2525 mips_move_labels (); 2526 2527 #ifndef NO_ECOFF_DEBUGGING 2528 if (ECOFF_DEBUGGING) 2529 ecoff_fix_loc (old_frag, old_frag_offset); 2530 #endif 2531 } 2532 } 2533 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL) 2534 { 2535 /* Work out how many nops in prev_nop_frag are needed by IP. */ 2536 int nops = nops_for_insn_or_target (history, ip); 2537 assert (nops <= prev_nop_frag_holds); 2538 2539 /* Enforce NOPS as a minimum. */ 2540 if (nops > prev_nop_frag_required) 2541 prev_nop_frag_required = nops; 2542 2543 if (prev_nop_frag_holds == prev_nop_frag_required) 2544 { 2545 /* Settle for the current number of nops. Update the history 2546 accordingly (for the benefit of any future .set reorder code). */ 2547 prev_nop_frag = NULL; 2548 insert_into_history (prev_nop_frag_since, 2549 prev_nop_frag_holds, NOP_INSN); 2550 } 2551 else 2552 { 2553 /* Allow this instruction to replace one of the nops that was 2554 tentatively added to prev_nop_frag. */ 2555 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4; 2556 prev_nop_frag_holds--; 2557 prev_nop_frag_since++; 2558 } 2559 } 2560 2561 #ifdef OBJ_ELF 2562 /* The value passed to dwarf2_emit_insn is the distance between 2563 the beginning of the current instruction and the address that 2564 should be recorded in the debug tables. For MIPS16 debug info 2565 we want to use ISA-encoded addresses, so we pass -1 for an 2566 address higher by one than the current. */ 2567 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0); 2568 #endif 2569 2570 /* Record the frag type before frag_var. */ 2571 if (history[0].frag) 2572 prev_insn_frag_type = history[0].frag->fr_type; 2573 2574 if (address_expr 2575 && *reloc_type == BFD_RELOC_16_PCREL_S2 2576 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY 2577 || pinfo & INSN_COND_BRANCH_LIKELY) 2578 && mips_relax_branch 2579 /* Don't try branch relaxation within .set nomacro, or within 2580 .set noat if we use $at for PIC computations. If it turns 2581 out that the branch was out-of-range, we'll get an error. */ 2582 && !mips_opts.warn_about_macros 2583 && !(mips_opts.noat && mips_pic != NO_PIC) 2584 && !mips_opts.mips16) 2585 { 2586 relaxed_branch = TRUE; 2587 add_relaxed_insn (ip, (relaxed_branch_length 2588 (NULL, NULL, 2589 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1 2590 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 2591 : 0)), 4, 2592 RELAX_BRANCH_ENCODE 2593 (pinfo & INSN_UNCOND_BRANCH_DELAY, 2594 pinfo & INSN_COND_BRANCH_LIKELY, 2595 pinfo & INSN_WRITE_GPR_31, 2596 0), 2597 address_expr->X_add_symbol, 2598 address_expr->X_add_number); 2599 *reloc_type = BFD_RELOC_UNUSED; 2600 } 2601 else if (*reloc_type > BFD_RELOC_UNUSED) 2602 { 2603 /* We need to set up a variant frag. */ 2604 assert (mips_opts.mips16 && address_expr != NULL); 2605 add_relaxed_insn (ip, 4, 0, 2606 RELAX_MIPS16_ENCODE 2607 (*reloc_type - BFD_RELOC_UNUSED, 2608 mips16_small, mips16_ext, 2609 prev_pinfo & INSN_UNCOND_BRANCH_DELAY, 2610 history[0].mips16_absolute_jump_p), 2611 make_expr_symbol (address_expr), 0); 2612 } 2613 else if (mips_opts.mips16 2614 && ! ip->use_extend 2615 && *reloc_type != BFD_RELOC_MIPS16_JMP) 2616 { 2617 if ((pinfo & INSN_UNCOND_BRANCH_DELAY) == 0) 2618 /* Make sure there is enough room to swap this instruction with 2619 a following jump instruction. */ 2620 frag_grow (6); 2621 add_fixed_insn (ip); 2622 } 2623 else 2624 { 2625 if (mips_opts.mips16 2626 && mips_opts.noreorder 2627 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0) 2628 as_warn (_("extended instruction in delay slot")); 2629 2630 if (mips_relax.sequence) 2631 { 2632 /* If we've reached the end of this frag, turn it into a variant 2633 frag and record the information for the instructions we've 2634 written so far. */ 2635 if (frag_room () < 4) 2636 relax_close_frag (); 2637 mips_relax.sizes[mips_relax.sequence - 1] += 4; 2638 } 2639 2640 if (mips_relax.sequence != 2) 2641 mips_macro_warning.sizes[0] += 4; 2642 if (mips_relax.sequence != 1) 2643 mips_macro_warning.sizes[1] += 4; 2644 2645 if (mips_opts.mips16) 2646 { 2647 ip->fixed_p = 1; 2648 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP); 2649 } 2650 add_fixed_insn (ip); 2651 } 2652 2653 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED) 2654 { 2655 if (address_expr->X_op == O_constant) 2656 { 2657 unsigned int tmp; 2658 2659 switch (*reloc_type) 2660 { 2661 case BFD_RELOC_32: 2662 ip->insn_opcode |= address_expr->X_add_number; 2663 break; 2664 2665 case BFD_RELOC_MIPS_HIGHEST: 2666 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48; 2667 ip->insn_opcode |= tmp & 0xffff; 2668 break; 2669 2670 case BFD_RELOC_MIPS_HIGHER: 2671 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32; 2672 ip->insn_opcode |= tmp & 0xffff; 2673 break; 2674 2675 case BFD_RELOC_HI16_S: 2676 tmp = (address_expr->X_add_number + 0x8000) >> 16; 2677 ip->insn_opcode |= tmp & 0xffff; 2678 break; 2679 2680 case BFD_RELOC_HI16: 2681 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff; 2682 break; 2683 2684 case BFD_RELOC_UNUSED: 2685 case BFD_RELOC_LO16: 2686 case BFD_RELOC_MIPS_GOT_DISP: 2687 ip->insn_opcode |= address_expr->X_add_number & 0xffff; 2688 break; 2689 2690 case BFD_RELOC_MIPS_JMP: 2691 if ((address_expr->X_add_number & 3) != 0) 2692 as_bad (_("jump to misaligned address (0x%lx)"), 2693 (unsigned long) address_expr->X_add_number); 2694 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff; 2695 break; 2696 2697 case BFD_RELOC_MIPS16_JMP: 2698 if ((address_expr->X_add_number & 3) != 0) 2699 as_bad (_("jump to misaligned address (0x%lx)"), 2700 (unsigned long) address_expr->X_add_number); 2701 ip->insn_opcode |= 2702 (((address_expr->X_add_number & 0x7c0000) << 3) 2703 | ((address_expr->X_add_number & 0xf800000) >> 7) 2704 | ((address_expr->X_add_number & 0x3fffc) >> 2)); 2705 break; 2706 2707 case BFD_RELOC_16_PCREL_S2: 2708 if ((address_expr->X_add_number & 3) != 0) 2709 as_bad (_("branch to misaligned address (0x%lx)"), 2710 (unsigned long) address_expr->X_add_number); 2711 if (mips_relax_branch) 2712 goto need_reloc; 2713 if ((address_expr->X_add_number + 0x20000) & ~0x3ffff) 2714 as_bad (_("branch address range overflow (0x%lx)"), 2715 (unsigned long) address_expr->X_add_number); 2716 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0xffff; 2717 break; 2718 2719 default: 2720 internalError (); 2721 } 2722 } 2723 else if (*reloc_type < BFD_RELOC_UNUSED) 2724 need_reloc: 2725 { 2726 reloc_howto_type *howto; 2727 int i; 2728 2729 /* In a compound relocation, it is the final (outermost) 2730 operator that determines the relocated field. */ 2731 for (i = 1; i < 3; i++) 2732 if (reloc_type[i] == BFD_RELOC_UNUSED) 2733 break; 2734 2735 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]); 2736 ip->fixp[0] = fix_new_exp (ip->frag, ip->where, 2737 bfd_get_reloc_size (howto), 2738 address_expr, 2739 reloc_type[0] == BFD_RELOC_16_PCREL_S2, 2740 reloc_type[0]); 2741 2742 /* These relocations can have an addend that won't fit in 2743 4 octets for 64bit assembly. */ 2744 if (HAVE_64BIT_GPRS 2745 && ! howto->partial_inplace 2746 && (reloc_type[0] == BFD_RELOC_16 2747 || reloc_type[0] == BFD_RELOC_32 2748 || reloc_type[0] == BFD_RELOC_MIPS_JMP 2749 || reloc_type[0] == BFD_RELOC_HI16_S 2750 || reloc_type[0] == BFD_RELOC_LO16 2751 || reloc_type[0] == BFD_RELOC_GPREL16 2752 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL 2753 || reloc_type[0] == BFD_RELOC_GPREL32 2754 || reloc_type[0] == BFD_RELOC_64 2755 || reloc_type[0] == BFD_RELOC_CTOR 2756 || reloc_type[0] == BFD_RELOC_MIPS_SUB 2757 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST 2758 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER 2759 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP 2760 || reloc_type[0] == BFD_RELOC_MIPS_REL16 2761 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT 2762 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL 2763 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S 2764 || reloc_type[0] == BFD_RELOC_MIPS16_LO16)) 2765 ip->fixp[0]->fx_no_overflow = 1; 2766 2767 if (mips_relax.sequence) 2768 { 2769 if (mips_relax.first_fixup == 0) 2770 mips_relax.first_fixup = ip->fixp[0]; 2771 } 2772 else if (reloc_needs_lo_p (*reloc_type)) 2773 { 2774 struct mips_hi_fixup *hi_fixup; 2775 2776 /* Reuse the last entry if it already has a matching %lo. */ 2777 hi_fixup = mips_hi_fixup_list; 2778 if (hi_fixup == 0 2779 || !fixup_has_matching_lo_p (hi_fixup->fixp)) 2780 { 2781 hi_fixup = ((struct mips_hi_fixup *) 2782 xmalloc (sizeof (struct mips_hi_fixup))); 2783 hi_fixup->next = mips_hi_fixup_list; 2784 mips_hi_fixup_list = hi_fixup; 2785 } 2786 hi_fixup->fixp = ip->fixp[0]; 2787 hi_fixup->seg = now_seg; 2788 } 2789 2790 /* Add fixups for the second and third relocations, if given. 2791 Note that the ABI allows the second relocation to be 2792 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the 2793 moment we only use RSS_UNDEF, but we could add support 2794 for the others if it ever becomes necessary. */ 2795 for (i = 1; i < 3; i++) 2796 if (reloc_type[i] != BFD_RELOC_UNUSED) 2797 { 2798 ip->fixp[i] = fix_new (ip->frag, ip->where, 2799 ip->fixp[0]->fx_size, NULL, 0, 2800 FALSE, reloc_type[i]); 2801 2802 /* Use fx_tcbit to mark compound relocs. */ 2803 ip->fixp[0]->fx_tcbit = 1; 2804 ip->fixp[i]->fx_tcbit = 1; 2805 } 2806 } 2807 } 2808 install_insn (ip); 2809 2810 /* Update the register mask information. */ 2811 if (! mips_opts.mips16) 2812 { 2813 if (pinfo & INSN_WRITE_GPR_D) 2814 mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip); 2815 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0) 2816 mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip); 2817 if (pinfo & INSN_READ_GPR_S) 2818 mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip); 2819 if (pinfo & INSN_WRITE_GPR_31) 2820 mips_gprmask |= 1 << RA; 2821 if (pinfo & INSN_WRITE_FPR_D) 2822 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip); 2823 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0) 2824 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip); 2825 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0) 2826 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip); 2827 if ((pinfo & INSN_READ_FPR_R) != 0) 2828 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip); 2829 if (pinfo & INSN_COP) 2830 { 2831 /* We don't keep enough information to sort these cases out. 2832 The itbl support does keep this information however, although 2833 we currently don't support itbl fprmats as part of the cop 2834 instruction. May want to add this support in the future. */ 2835 } 2836 /* Never set the bit for $0, which is always zero. */ 2837 mips_gprmask &= ~1 << 0; 2838 } 2839 else 2840 { 2841 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X)) 2842 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip); 2843 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y)) 2844 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip); 2845 if (pinfo & MIPS16_INSN_WRITE_Z) 2846 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip); 2847 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T)) 2848 mips_gprmask |= 1 << TREG; 2849 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP)) 2850 mips_gprmask |= 1 << SP; 2851 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31)) 2852 mips_gprmask |= 1 << RA; 2853 if (pinfo & MIPS16_INSN_WRITE_GPR_Y) 2854 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode); 2855 if (pinfo & MIPS16_INSN_READ_Z) 2856 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip); 2857 if (pinfo & MIPS16_INSN_READ_GPR_X) 2858 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip); 2859 } 2860 2861 if (mips_relax.sequence != 2 && !mips_opts.noreorder) 2862 { 2863 /* Filling the branch delay slot is more complex. We try to 2864 switch the branch with the previous instruction, which we can 2865 do if the previous instruction does not set up a condition 2866 that the branch tests and if the branch is not itself the 2867 target of any branch. */ 2868 if ((pinfo & INSN_UNCOND_BRANCH_DELAY) 2869 || (pinfo & INSN_COND_BRANCH_DELAY)) 2870 { 2871 if (mips_optimize < 2 2872 /* If we have seen .set volatile or .set nomove, don't 2873 optimize. */ 2874 || mips_opts.nomove != 0 2875 /* We can't swap if the previous instruction's position 2876 is fixed. */ 2877 || history[0].fixed_p 2878 /* If the previous previous insn was in a .set 2879 noreorder, we can't swap. Actually, the MIPS 2880 assembler will swap in this situation. However, gcc 2881 configured -with-gnu-as will generate code like 2882 .set noreorder 2883 lw $4,XXX 2884 .set reorder 2885 INSN 2886 bne $4,$0,foo 2887 in which we can not swap the bne and INSN. If gcc is 2888 not configured -with-gnu-as, it does not output the 2889 .set pseudo-ops. */ 2890 || history[1].noreorder_p 2891 /* If the branch is itself the target of a branch, we 2892 can not swap. We cheat on this; all we check for is 2893 whether there is a label on this instruction. If 2894 there are any branches to anything other than a 2895 label, users must use .set noreorder. */ 2896 || insn_labels != NULL 2897 /* If the previous instruction is in a variant frag 2898 other than this branch's one, we cannot do the swap. 2899 This does not apply to the mips16, which uses variant 2900 frags for different purposes. */ 2901 || (! mips_opts.mips16 2902 && prev_insn_frag_type == rs_machine_dependent) 2903 /* Check for conflicts between the branch and the instructions 2904 before the candidate delay slot. */ 2905 || nops_for_insn (history + 1, ip) > 0 2906 /* Check for conflicts between the swapped sequence and the 2907 target of the branch. */ 2908 || nops_for_sequence (2, history + 1, ip, history) > 0 2909 /* We do not swap with a trap instruction, since it 2910 complicates trap handlers to have the trap 2911 instruction be in a delay slot. */ 2912 || (prev_pinfo & INSN_TRAP) 2913 /* If the branch reads a register that the previous 2914 instruction sets, we can not swap. */ 2915 || (! mips_opts.mips16 2916 && (prev_pinfo & INSN_WRITE_GPR_T) 2917 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]), 2918 MIPS_GR_REG)) 2919 || (! mips_opts.mips16 2920 && (prev_pinfo & INSN_WRITE_GPR_D) 2921 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]), 2922 MIPS_GR_REG)) 2923 || (mips_opts.mips16 2924 && (((prev_pinfo & MIPS16_INSN_WRITE_X) 2925 && (insn_uses_reg 2926 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]), 2927 MIPS16_REG))) 2928 || ((prev_pinfo & MIPS16_INSN_WRITE_Y) 2929 && (insn_uses_reg 2930 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]), 2931 MIPS16_REG))) 2932 || ((prev_pinfo & MIPS16_INSN_WRITE_Z) 2933 && (insn_uses_reg 2934 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]), 2935 MIPS16_REG))) 2936 || ((prev_pinfo & MIPS16_INSN_WRITE_T) 2937 && insn_uses_reg (ip, TREG, MIPS_GR_REG)) 2938 || ((prev_pinfo & MIPS16_INSN_WRITE_31) 2939 && insn_uses_reg (ip, RA, MIPS_GR_REG)) 2940 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y) 2941 && insn_uses_reg (ip, 2942 MIPS16OP_EXTRACT_REG32R 2943 (history[0].insn_opcode), 2944 MIPS_GR_REG)))) 2945 /* If the branch writes a register that the previous 2946 instruction sets, we can not swap (we know that 2947 branches write only to RD or to $31). */ 2948 || (! mips_opts.mips16 2949 && (prev_pinfo & INSN_WRITE_GPR_T) 2950 && (((pinfo & INSN_WRITE_GPR_D) 2951 && (EXTRACT_OPERAND (RT, history[0]) 2952 == EXTRACT_OPERAND (RD, *ip))) 2953 || ((pinfo & INSN_WRITE_GPR_31) 2954 && EXTRACT_OPERAND (RT, history[0]) == RA))) 2955 || (! mips_opts.mips16 2956 && (prev_pinfo & INSN_WRITE_GPR_D) 2957 && (((pinfo & INSN_WRITE_GPR_D) 2958 && (EXTRACT_OPERAND (RD, history[0]) 2959 == EXTRACT_OPERAND (RD, *ip))) 2960 || ((pinfo & INSN_WRITE_GPR_31) 2961 && EXTRACT_OPERAND (RD, history[0]) == RA))) 2962 || (mips_opts.mips16 2963 && (pinfo & MIPS16_INSN_WRITE_31) 2964 && ((prev_pinfo & MIPS16_INSN_WRITE_31) 2965 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y) 2966 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode) 2967 == RA)))) 2968 /* If the branch writes a register that the previous 2969 instruction reads, we can not swap (we know that 2970 branches only write to RD or to $31). */ 2971 || (! mips_opts.mips16 2972 && (pinfo & INSN_WRITE_GPR_D) 2973 && insn_uses_reg (&history[0], 2974 EXTRACT_OPERAND (RD, *ip), 2975 MIPS_GR_REG)) 2976 || (! mips_opts.mips16 2977 && (pinfo & INSN_WRITE_GPR_31) 2978 && insn_uses_reg (&history[0], RA, MIPS_GR_REG)) 2979 || (mips_opts.mips16 2980 && (pinfo & MIPS16_INSN_WRITE_31) 2981 && insn_uses_reg (&history[0], RA, MIPS_GR_REG)) 2982 /* If one instruction sets a condition code and the 2983 other one uses a condition code, we can not swap. */ 2984 || ((pinfo & INSN_READ_COND_CODE) 2985 && (prev_pinfo & INSN_WRITE_COND_CODE)) 2986 || ((pinfo & INSN_WRITE_COND_CODE) 2987 && (prev_pinfo & INSN_READ_COND_CODE)) 2988 /* If the previous instruction uses the PC, we can not 2989 swap. */ 2990 || (mips_opts.mips16 2991 && (prev_pinfo & MIPS16_INSN_READ_PC)) 2992 /* If the previous instruction had a fixup in mips16 2993 mode, we can not swap. This normally means that the 2994 previous instruction was a 4 byte branch anyhow. */ 2995 || (mips_opts.mips16 && history[0].fixp[0]) 2996 /* If the previous instruction is a sync, sync.l, or 2997 sync.p, we can not swap. */ 2998 || (prev_pinfo & INSN_SYNC)) 2999 { 3000 if (mips_opts.mips16 3001 && (pinfo & INSN_UNCOND_BRANCH_DELAY) 3002 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)) 3003 && (mips_opts.isa == ISA_MIPS32 3004 || mips_opts.isa == ISA_MIPS32R2 3005 || mips_opts.isa == ISA_MIPS64 3006 || mips_opts.isa == ISA_MIPS64R2)) 3007 { 3008 /* Convert MIPS16 jr/jalr into a "compact" jump. */ 3009 ip->insn_opcode |= 0x0080; 3010 install_insn (ip); 3011 insert_into_history (0, 1, ip); 3012 } 3013 else 3014 { 3015 /* We could do even better for unconditional branches to 3016 portions of this object file; we could pick up the 3017 instruction at the destination, put it in the delay 3018 slot, and bump the destination address. */ 3019 insert_into_history (0, 1, ip); 3020 emit_nop (); 3021 } 3022 3023 if (mips_relax.sequence) 3024 mips_relax.sizes[mips_relax.sequence - 1] += 4; 3025 } 3026 else 3027 { 3028 /* It looks like we can actually do the swap. */ 3029 struct mips_cl_insn delay = history[0]; 3030 if (mips_opts.mips16) 3031 { 3032 know (delay.frag == ip->frag); 3033 move_insn (ip, delay.frag, delay.where); 3034 move_insn (&delay, ip->frag, ip->where + insn_length (ip)); 3035 } 3036 else if (relaxed_branch) 3037 { 3038 /* Add the delay slot instruction to the end of the 3039 current frag and shrink the fixed part of the 3040 original frag. If the branch occupies the tail of 3041 the latter, move it backwards to cover the gap. */ 3042 delay.frag->fr_fix -= 4; 3043 if (delay.frag == ip->frag) 3044 move_insn (ip, ip->frag, ip->where - 4); 3045 add_fixed_insn (&delay); 3046 } 3047 else 3048 { 3049 move_insn (&delay, ip->frag, ip->where); 3050 move_insn (ip, history[0].frag, history[0].where); 3051 } 3052 history[0] = *ip; 3053 delay.fixed_p = 1; 3054 insert_into_history (0, 1, &delay); 3055 } 3056 3057 /* If that was an unconditional branch, forget the previous 3058 insn information. */ 3059 if (pinfo & INSN_UNCOND_BRANCH_DELAY) 3060 mips_no_prev_insn (); 3061 } 3062 else if (pinfo & INSN_COND_BRANCH_LIKELY) 3063 { 3064 /* We don't yet optimize a branch likely. What we should do 3065 is look at the target, copy the instruction found there 3066 into the delay slot, and increment the branch to jump to 3067 the next instruction. */ 3068 insert_into_history (0, 1, ip); 3069 emit_nop (); 3070 } 3071 else 3072 insert_into_history (0, 1, ip); 3073 } 3074 else 3075 insert_into_history (0, 1, ip); 3076 3077 /* We just output an insn, so the next one doesn't have a label. */ 3078 mips_clear_insn_labels (); 3079 } 3080 3081 /* Forget that there was any previous instruction or label. */ 3082 3083 static void 3084 mips_no_prev_insn (void) 3085 { 3086 prev_nop_frag = NULL; 3087 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN); 3088 mips_clear_insn_labels (); 3089 } 3090 3091 /* This function must be called before we emit something other than 3092 instructions. It is like mips_no_prev_insn except that it inserts 3093 any NOPS that might be needed by previous instructions. */ 3094 3095 void 3096 mips_emit_delays (void) 3097 { 3098 if (! mips_opts.noreorder) 3099 { 3100 int nops = nops_for_insn (history, NULL); 3101 if (nops > 0) 3102 { 3103 while (nops-- > 0) 3104 add_fixed_insn (NOP_INSN); 3105 mips_move_labels (); 3106 } 3107 } 3108 mips_no_prev_insn (); 3109 } 3110 3111 /* Start a (possibly nested) noreorder block. */ 3112 3113 static void 3114 start_noreorder (void) 3115 { 3116 if (mips_opts.noreorder == 0) 3117 { 3118 unsigned int i; 3119 int nops; 3120 3121 /* None of the instructions before the .set noreorder can be moved. */ 3122 for (i = 0; i < ARRAY_SIZE (history); i++) 3123 history[i].fixed_p = 1; 3124 3125 /* Insert any nops that might be needed between the .set noreorder 3126 block and the previous instructions. We will later remove any 3127 nops that turn out not to be needed. */ 3128 nops = nops_for_insn (history, NULL); 3129 if (nops > 0) 3130 { 3131 if (mips_optimize != 0) 3132 { 3133 /* Record the frag which holds the nop instructions, so 3134 that we can remove them if we don't need them. */ 3135 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4); 3136 prev_nop_frag = frag_now; 3137 prev_nop_frag_holds = nops; 3138 prev_nop_frag_required = 0; 3139 prev_nop_frag_since = 0; 3140 } 3141 3142 for (; nops > 0; --nops) 3143 add_fixed_insn (NOP_INSN); 3144 3145 /* Move on to a new frag, so that it is safe to simply 3146 decrease the size of prev_nop_frag. */ 3147 frag_wane (frag_now); 3148 frag_new (0); 3149 mips_move_labels (); 3150 } 3151 mips16_mark_labels (); 3152 mips_clear_insn_labels (); 3153 } 3154 mips_opts.noreorder++; 3155 mips_any_noreorder = 1; 3156 } 3157 3158 /* End a nested noreorder block. */ 3159 3160 static void 3161 end_noreorder (void) 3162 { 3163 mips_opts.noreorder--; 3164 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL) 3165 { 3166 /* Commit to inserting prev_nop_frag_required nops and go back to 3167 handling nop insertion the .set reorder way. */ 3168 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required) 3169 * (mips_opts.mips16 ? 2 : 4)); 3170 insert_into_history (prev_nop_frag_since, 3171 prev_nop_frag_required, NOP_INSN); 3172 prev_nop_frag = NULL; 3173 } 3174 } 3175 3176 /* Set up global variables for the start of a new macro. */ 3177 3178 static void 3179 macro_start (void) 3180 { 3181 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes)); 3182 mips_macro_warning.delay_slot_p = (mips_opts.noreorder 3183 && (history[0].insn_mo->pinfo 3184 & (INSN_UNCOND_BRANCH_DELAY 3185 | INSN_COND_BRANCH_DELAY 3186 | INSN_COND_BRANCH_LIKELY)) != 0); 3187 } 3188 3189 /* Given that a macro is longer than 4 bytes, return the appropriate warning 3190 for it. Return null if no warning is needed. SUBTYPE is a bitmask of 3191 RELAX_DELAY_SLOT and RELAX_NOMACRO. */ 3192 3193 static const char * 3194 macro_warning (relax_substateT subtype) 3195 { 3196 if (subtype & RELAX_DELAY_SLOT) 3197 return _("Macro instruction expanded into multiple instructions" 3198 " in a branch delay slot"); 3199 else if (subtype & RELAX_NOMACRO) 3200 return _("Macro instruction expanded into multiple instructions"); 3201 else 3202 return 0; 3203 } 3204 3205 /* Finish up a macro. Emit warnings as appropriate. */ 3206 3207 static void 3208 macro_end (void) 3209 { 3210 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4) 3211 { 3212 relax_substateT subtype; 3213 3214 /* Set up the relaxation warning flags. */ 3215 subtype = 0; 3216 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0]) 3217 subtype |= RELAX_SECOND_LONGER; 3218 if (mips_opts.warn_about_macros) 3219 subtype |= RELAX_NOMACRO; 3220 if (mips_macro_warning.delay_slot_p) 3221 subtype |= RELAX_DELAY_SLOT; 3222 3223 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4) 3224 { 3225 /* Either the macro has a single implementation or both 3226 implementations are longer than 4 bytes. Emit the 3227 warning now. */ 3228 const char *msg = macro_warning (subtype); 3229 if (msg != 0) 3230 as_warn ("%s", msg); 3231 } 3232 else 3233 { 3234 /* One implementation might need a warning but the other 3235 definitely doesn't. */ 3236 mips_macro_warning.first_frag->fr_subtype |= subtype; 3237 } 3238 } 3239 } 3240 3241 /* Read a macro's relocation codes from *ARGS and store them in *R. 3242 The first argument in *ARGS will be either the code for a single 3243 relocation or -1 followed by the three codes that make up a 3244 composite relocation. */ 3245 3246 static void 3247 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r) 3248 { 3249 int i, next; 3250 3251 next = va_arg (*args, int); 3252 if (next >= 0) 3253 r[0] = (bfd_reloc_code_real_type) next; 3254 else 3255 for (i = 0; i < 3; i++) 3256 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int); 3257 } 3258 3259 /* Fix jump through register issue on loongson2f processor for kernel code: 3260 force a BTB clear before the jump to prevent it from being incorrectly 3261 prefetched by the branch prediction engine. */ 3262 3263 static void 3264 macro_build_jrpatch (expressionS *ep, unsigned int sreg) 3265 { 3266 if (!mips_fix_loongson2f_btb) 3267 return; 3268 3269 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == AT) 3270 return; 3271 3272 if (mips_opts.noat) 3273 { 3274 as_warn (_("unable to apply loongson2f BTB workaround when .set noat")); 3275 return; 3276 } 3277 3278 /* li $at, COP_0_BTB_CLEAR | COP_0_RAS_DISABLE */ 3279 ep->X_op = O_constant; 3280 ep->X_add_number = 3; 3281 macro_build (ep, "ori", "t,r,i", AT, ZERO, BFD_RELOC_LO16); 3282 3283 /* dmtc0 $at, COP_0_DIAG */ 3284 macro_build (NULL, "dmtc0", "t,G", AT, 22); 3285 3286 /* Hide these two instructions to avoid getting a ``macro expanded into 3287 multiple instructions'' warning. */ 3288 if (mips_relax.sequence != 2) 3289 mips_macro_warning.sizes[0] -= 2 * 4; 3290 if (mips_relax.sequence != 1) 3291 mips_macro_warning.sizes[1] -= 2 * 4; 3292 } 3293 3294 /* Build an instruction created by a macro expansion. This is passed 3295 a pointer to the count of instructions created so far, an 3296 expression, the name of the instruction to build, an operand format 3297 string, and corresponding arguments. */ 3298 3299 static void 3300 macro_build (expressionS *ep, const char *name, const char *fmt, ...) 3301 { 3302 const struct mips_opcode *mo; 3303 struct mips_cl_insn insn; 3304 bfd_reloc_code_real_type r[3]; 3305 va_list args; 3306 3307 va_start (args, fmt); 3308 3309 if (mips_opts.mips16) 3310 { 3311 mips16_macro_build (ep, name, fmt, args); 3312 va_end (args); 3313 return; 3314 } 3315 3316 r[0] = BFD_RELOC_UNUSED; 3317 r[1] = BFD_RELOC_UNUSED; 3318 r[2] = BFD_RELOC_UNUSED; 3319 mo = (struct mips_opcode *) hash_find (op_hash, name); 3320 assert (mo); 3321 assert (strcmp (name, mo->name) == 0); 3322 3323 /* Search until we get a match for NAME. It is assumed here that 3324 macros will never generate MDMX or MIPS-3D instructions. */ 3325 while (strcmp (fmt, mo->args) != 0 3326 || mo->pinfo == INSN_MACRO 3327 || !OPCODE_IS_MEMBER (mo, 3328 (mips_opts.isa 3329 | (file_ase_mips16 ? INSN_MIPS16 : 0)), 3330 mips_opts.arch) 3331 || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0)) 3332 { 3333 ++mo; 3334 assert (mo->name); 3335 assert (strcmp (name, mo->name) == 0); 3336 } 3337 3338 create_insn (&insn, mo); 3339 for (;;) 3340 { 3341 switch (*fmt++) 3342 { 3343 case '\0': 3344 break; 3345 3346 case ',': 3347 case '(': 3348 case ')': 3349 continue; 3350 3351 case '+': 3352 switch (*fmt++) 3353 { 3354 case 'A': 3355 case 'E': 3356 INSERT_OPERAND (SHAMT, insn, va_arg (args, int)); 3357 continue; 3358 3359 case 'B': 3360 case 'F': 3361 /* Note that in the macro case, these arguments are already 3362 in MSB form. (When handling the instruction in the 3363 non-macro case, these arguments are sizes from which 3364 MSB values must be calculated.) */ 3365 INSERT_OPERAND (INSMSB, insn, va_arg (args, int)); 3366 continue; 3367 3368 case 'C': 3369 case 'G': 3370 case 'H': 3371 /* Note that in the macro case, these arguments are already 3372 in MSBD form. (When handling the instruction in the 3373 non-macro case, these arguments are sizes from which 3374 MSBD values must be calculated.) */ 3375 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int)); 3376 continue; 3377 3378 default: 3379 internalError (); 3380 } 3381 continue; 3382 3383 case 't': 3384 case 'w': 3385 case 'E': 3386 INSERT_OPERAND (RT, insn, va_arg (args, int)); 3387 continue; 3388 3389 case 'c': 3390 INSERT_OPERAND (CODE, insn, va_arg (args, int)); 3391 continue; 3392 3393 case 'T': 3394 case 'W': 3395 INSERT_OPERAND (FT, insn, va_arg (args, int)); 3396 continue; 3397 3398 case 'd': 3399 case 'G': 3400 case 'K': 3401 INSERT_OPERAND (RD, insn, va_arg (args, int)); 3402 continue; 3403 3404 case 'U': 3405 { 3406 int tmp = va_arg (args, int); 3407 3408 INSERT_OPERAND (RT, insn, tmp); 3409 INSERT_OPERAND (RD, insn, tmp); 3410 continue; 3411 } 3412 3413 case 'V': 3414 case 'S': 3415 INSERT_OPERAND (FS, insn, va_arg (args, int)); 3416 continue; 3417 3418 case 'z': 3419 continue; 3420 3421 case '<': 3422 INSERT_OPERAND (SHAMT, insn, va_arg (args, int)); 3423 continue; 3424 3425 case 'D': 3426 INSERT_OPERAND (FD, insn, va_arg (args, int)); 3427 continue; 3428 3429 case 'B': 3430 INSERT_OPERAND (CODE20, insn, va_arg (args, int)); 3431 continue; 3432 3433 case 'J': 3434 INSERT_OPERAND (CODE19, insn, va_arg (args, int)); 3435 continue; 3436 3437 case 'q': 3438 INSERT_OPERAND (CODE2, insn, va_arg (args, int)); 3439 continue; 3440 3441 case 'b': 3442 case 's': 3443 case 'r': 3444 case 'v': 3445 INSERT_OPERAND (RS, insn, va_arg (args, int)); 3446 continue; 3447 3448 case 'i': 3449 case 'j': 3450 case 'o': 3451 macro_read_relocs (&args, r); 3452 assert (*r == BFD_RELOC_GPREL16 3453 || *r == BFD_RELOC_MIPS_LITERAL 3454 || *r == BFD_RELOC_MIPS_HIGHER 3455 || *r == BFD_RELOC_HI16_S 3456 || *r == BFD_RELOC_LO16 3457 || *r == BFD_RELOC_MIPS_GOT16 3458 || *r == BFD_RELOC_MIPS_CALL16 3459 || *r == BFD_RELOC_MIPS_GOT_DISP 3460 || *r == BFD_RELOC_MIPS_GOT_PAGE 3461 || *r == BFD_RELOC_MIPS_GOT_OFST 3462 || *r == BFD_RELOC_MIPS_GOT_LO16 3463 || *r == BFD_RELOC_MIPS_CALL_LO16); 3464 continue; 3465 3466 case 'u': 3467 macro_read_relocs (&args, r); 3468 assert (ep != NULL 3469 && (ep->X_op == O_constant 3470 || (ep->X_op == O_symbol 3471 && (*r == BFD_RELOC_MIPS_HIGHEST 3472 || *r == BFD_RELOC_HI16_S 3473 || *r == BFD_RELOC_HI16 3474 || *r == BFD_RELOC_GPREL16 3475 || *r == BFD_RELOC_MIPS_GOT_HI16 3476 || *r == BFD_RELOC_MIPS_CALL_HI16)))); 3477 continue; 3478 3479 case 'p': 3480 assert (ep != NULL); 3481 3482 /* 3483 * This allows macro() to pass an immediate expression for 3484 * creating short branches without creating a symbol. 3485 * 3486 * We don't allow branch relaxation for these branches, as 3487 * they should only appear in ".set nomacro" anyway. 3488 */ 3489 if (ep->X_op == O_constant) 3490 { 3491 if ((ep->X_add_number & 3) != 0) 3492 as_bad (_("branch to misaligned address (0x%lx)"), 3493 (unsigned long) ep->X_add_number); 3494 if ((ep->X_add_number + 0x20000) & ~0x3ffff) 3495 as_bad (_("branch address range overflow (0x%lx)"), 3496 (unsigned long) ep->X_add_number); 3497 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff; 3498 ep = NULL; 3499 } 3500 else 3501 *r = BFD_RELOC_16_PCREL_S2; 3502 continue; 3503 3504 case 'a': 3505 assert (ep != NULL); 3506 *r = BFD_RELOC_MIPS_JMP; 3507 continue; 3508 3509 case 'C': 3510 insn.insn_opcode |= va_arg (args, unsigned long); 3511 continue; 3512 3513 default: 3514 internalError (); 3515 } 3516 break; 3517 } 3518 va_end (args); 3519 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL); 3520 3521 append_insn (&insn, ep, r); 3522 } 3523 3524 static void 3525 mips16_macro_build (expressionS *ep, const char *name, const char *fmt, 3526 va_list args) 3527 { 3528 struct mips_opcode *mo; 3529 struct mips_cl_insn insn; 3530 bfd_reloc_code_real_type r[3] 3531 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED}; 3532 3533 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name); 3534 assert (mo); 3535 assert (strcmp (name, mo->name) == 0); 3536 3537 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO) 3538 { 3539 ++mo; 3540 assert (mo->name); 3541 assert (strcmp (name, mo->name) == 0); 3542 } 3543 3544 create_insn (&insn, mo); 3545 for (;;) 3546 { 3547 int c; 3548 3549 c = *fmt++; 3550 switch (c) 3551 { 3552 case '\0': 3553 break; 3554 3555 case ',': 3556 case '(': 3557 case ')': 3558 continue; 3559 3560 case 'y': 3561 case 'w': 3562 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int)); 3563 continue; 3564 3565 case 'x': 3566 case 'v': 3567 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int)); 3568 continue; 3569 3570 case 'z': 3571 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int)); 3572 continue; 3573 3574 case 'Z': 3575 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int)); 3576 continue; 3577 3578 case '0': 3579 case 'S': 3580 case 'P': 3581 case 'R': 3582 continue; 3583 3584 case 'X': 3585 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int)); 3586 continue; 3587 3588 case 'Y': 3589 { 3590 int regno; 3591 3592 regno = va_arg (args, int); 3593 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3); 3594 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R; 3595 } 3596 continue; 3597 3598 case '<': 3599 case '>': 3600 case '4': 3601 case '5': 3602 case 'H': 3603 case 'W': 3604 case 'D': 3605 case 'j': 3606 case '8': 3607 case 'V': 3608 case 'C': 3609 case 'U': 3610 case 'k': 3611 case 'K': 3612 case 'p': 3613 case 'q': 3614 { 3615 assert (ep != NULL); 3616 3617 if (ep->X_op != O_constant) 3618 *r = (int) BFD_RELOC_UNUSED + c; 3619 else 3620 { 3621 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE, 3622 FALSE, &insn.insn_opcode, &insn.use_extend, 3623 &insn.extend); 3624 ep = NULL; 3625 *r = BFD_RELOC_UNUSED; 3626 } 3627 } 3628 continue; 3629 3630 case '6': 3631 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int)); 3632 continue; 3633 } 3634 3635 break; 3636 } 3637 3638 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL); 3639 3640 append_insn (&insn, ep, r); 3641 } 3642 3643 /* 3644 * Sign-extend 32-bit mode constants that have bit 31 set and all 3645 * higher bits unset. 3646 */ 3647 static void 3648 normalize_constant_expr (expressionS *ex) 3649 { 3650 if (ex->X_op == O_constant 3651 && IS_ZEXT_32BIT_NUM (ex->X_add_number)) 3652 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000) 3653 - 0x80000000); 3654 } 3655 3656 /* 3657 * Sign-extend 32-bit mode address offsets that have bit 31 set and 3658 * all higher bits unset. 3659 */ 3660 static void 3661 normalize_address_expr (expressionS *ex) 3662 { 3663 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES) 3664 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS)) 3665 && IS_ZEXT_32BIT_NUM (ex->X_add_number)) 3666 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000) 3667 - 0x80000000); 3668 } 3669 3670 /* 3671 * Generate a "jalr" instruction with a relocation hint to the called 3672 * function. This occurs in NewABI PIC code. 3673 */ 3674 static void 3675 macro_build_jalr (expressionS *ep) 3676 { 3677 char *f = NULL; 3678 3679 if (HAVE_NEWABI) 3680 { 3681 frag_grow (8); 3682 f = frag_more (0); 3683 } 3684 macro_build_jrpatch (ep, PIC_CALL_REG); 3685 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG); 3686 if (HAVE_NEWABI) 3687 fix_new_exp (frag_now, f - frag_now->fr_literal, 3688 4, ep, FALSE, BFD_RELOC_MIPS_JALR); 3689 } 3690 3691 /* 3692 * Generate a "lui" instruction. 3693 */ 3694 static void 3695 macro_build_lui (expressionS *ep, int regnum) 3696 { 3697 expressionS high_expr; 3698 const struct mips_opcode *mo; 3699 struct mips_cl_insn insn; 3700 bfd_reloc_code_real_type r[3] 3701 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED}; 3702 const char *name = "lui"; 3703 const char *fmt = "t,u"; 3704 3705 assert (! mips_opts.mips16); 3706 3707 high_expr = *ep; 3708 3709 if (high_expr.X_op == O_constant) 3710 { 3711 /* we can compute the instruction now without a relocation entry */ 3712 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000) 3713 >> 16) & 0xffff; 3714 *r = BFD_RELOC_UNUSED; 3715 } 3716 else 3717 { 3718 assert (ep->X_op == O_symbol); 3719 /* _gp_disp is a special case, used from s_cpload. 3720 __gnu_local_gp is used if mips_no_shared. */ 3721 assert (mips_pic == NO_PIC 3722 || (! HAVE_NEWABI 3723 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0) 3724 || (! mips_in_shared 3725 && strcmp (S_GET_NAME (ep->X_add_symbol), 3726 "__gnu_local_gp") == 0)); 3727 *r = BFD_RELOC_HI16_S; 3728 } 3729 3730 mo = hash_find (op_hash, name); 3731 assert (strcmp (name, mo->name) == 0); 3732 assert (strcmp (fmt, mo->args) == 0); 3733 create_insn (&insn, mo); 3734 3735 insn.insn_opcode = insn.insn_mo->match; 3736 INSERT_OPERAND (RT, insn, regnum); 3737 if (*r == BFD_RELOC_UNUSED) 3738 { 3739 insn.insn_opcode |= high_expr.X_add_number; 3740 append_insn (&insn, NULL, r); 3741 } 3742 else 3743 append_insn (&insn, &high_expr, r); 3744 } 3745 3746 /* Generate a sequence of instructions to do a load or store from a constant 3747 offset off of a base register (breg) into/from a target register (treg), 3748 using AT if necessary. */ 3749 static void 3750 macro_build_ldst_constoffset (expressionS *ep, const char *op, 3751 int treg, int breg, int dbl) 3752 { 3753 assert (ep->X_op == O_constant); 3754 3755 /* Sign-extending 32-bit constants makes their handling easier. */ 3756 if (!dbl) 3757 normalize_constant_expr (ep); 3758 3759 /* Right now, this routine can only handle signed 32-bit constants. */ 3760 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000)) 3761 as_warn (_("operand overflow")); 3762 3763 if (IS_SEXT_16BIT_NUM(ep->X_add_number)) 3764 { 3765 /* Signed 16-bit offset will fit in the op. Easy! */ 3766 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg); 3767 } 3768 else 3769 { 3770 /* 32-bit offset, need multiple instructions and AT, like: 3771 lui $tempreg,const_hi (BFD_RELOC_HI16_S) 3772 addu $tempreg,$tempreg,$breg 3773 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16) 3774 to handle the complete offset. */ 3775 macro_build_lui (ep, AT); 3776 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg); 3777 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT); 3778 3779 if (mips_opts.noat) 3780 as_bad (_("Macro used $at after \".set noat\"")); 3781 } 3782 } 3783 3784 /* set_at() 3785 * Generates code to set the $at register to true (one) 3786 * if reg is less than the immediate expression. 3787 */ 3788 static void 3789 set_at (int reg, int unsignedp) 3790 { 3791 if (imm_expr.X_op == O_constant 3792 && imm_expr.X_add_number >= -0x8000 3793 && imm_expr.X_add_number < 0x8000) 3794 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j", 3795 AT, reg, BFD_RELOC_LO16); 3796 else 3797 { 3798 load_register (AT, &imm_expr, HAVE_64BIT_GPRS); 3799 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT); 3800 } 3801 } 3802 3803 /* Warn if an expression is not a constant. */ 3804 3805 static void 3806 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex) 3807 { 3808 if (ex->X_op == O_big) 3809 as_bad (_("unsupported large constant")); 3810 else if (ex->X_op != O_constant) 3811 as_bad (_("Instruction %s requires absolute expression"), 3812 ip->insn_mo->name); 3813 3814 if (HAVE_32BIT_GPRS) 3815 normalize_constant_expr (ex); 3816 } 3817 3818 /* Count the leading zeroes by performing a binary chop. This is a 3819 bulky bit of source, but performance is a LOT better for the 3820 majority of values than a simple loop to count the bits: 3821 for (lcnt = 0; (lcnt < 32); lcnt++) 3822 if ((v) & (1 << (31 - lcnt))) 3823 break; 3824 However it is not code size friendly, and the gain will drop a bit 3825 on certain cached systems. 3826 */ 3827 #define COUNT_TOP_ZEROES(v) \ 3828 (((v) & ~0xffff) == 0 \ 3829 ? ((v) & ~0xff) == 0 \ 3830 ? ((v) & ~0xf) == 0 \ 3831 ? ((v) & ~0x3) == 0 \ 3832 ? ((v) & ~0x1) == 0 \ 3833 ? !(v) \ 3834 ? 32 \ 3835 : 31 \ 3836 : 30 \ 3837 : ((v) & ~0x7) == 0 \ 3838 ? 29 \ 3839 : 28 \ 3840 : ((v) & ~0x3f) == 0 \ 3841 ? ((v) & ~0x1f) == 0 \ 3842 ? 27 \ 3843 : 26 \ 3844 : ((v) & ~0x7f) == 0 \ 3845 ? 25 \ 3846 : 24 \ 3847 : ((v) & ~0xfff) == 0 \ 3848 ? ((v) & ~0x3ff) == 0 \ 3849 ? ((v) & ~0x1ff) == 0 \ 3850 ? 23 \ 3851 : 22 \ 3852 : ((v) & ~0x7ff) == 0 \ 3853 ? 21 \ 3854 : 20 \ 3855 : ((v) & ~0x3fff) == 0 \ 3856 ? ((v) & ~0x1fff) == 0 \ 3857 ? 19 \ 3858 : 18 \ 3859 : ((v) & ~0x7fff) == 0 \ 3860 ? 17 \ 3861 : 16 \ 3862 : ((v) & ~0xffffff) == 0 \ 3863 ? ((v) & ~0xfffff) == 0 \ 3864 ? ((v) & ~0x3ffff) == 0 \ 3865 ? ((v) & ~0x1ffff) == 0 \ 3866 ? 15 \ 3867 : 14 \ 3868 : ((v) & ~0x7ffff) == 0 \ 3869 ? 13 \ 3870 : 12 \ 3871 : ((v) & ~0x3fffff) == 0 \ 3872 ? ((v) & ~0x1fffff) == 0 \ 3873 ? 11 \ 3874 : 10 \ 3875 : ((v) & ~0x7fffff) == 0 \ 3876 ? 9 \ 3877 : 8 \ 3878 : ((v) & ~0xfffffff) == 0 \ 3879 ? ((v) & ~0x3ffffff) == 0 \ 3880 ? ((v) & ~0x1ffffff) == 0 \ 3881 ? 7 \ 3882 : 6 \ 3883 : ((v) & ~0x7ffffff) == 0 \ 3884 ? 5 \ 3885 : 4 \ 3886 : ((v) & ~0x3fffffff) == 0 \ 3887 ? ((v) & ~0x1fffffff) == 0 \ 3888 ? 3 \ 3889 : 2 \ 3890 : ((v) & ~0x7fffffff) == 0 \ 3891 ? 1 \ 3892 : 0) 3893 3894 /* load_register() 3895 * This routine generates the least number of instructions necessary to load 3896 * an absolute expression value into a register. 3897 */ 3898 static void 3899 load_register (int reg, expressionS *ep, int dbl) 3900 { 3901 int freg; 3902 expressionS hi32, lo32; 3903 3904 if (ep->X_op != O_big) 3905 { 3906 assert (ep->X_op == O_constant); 3907 3908 /* Sign-extending 32-bit constants makes their handling easier. */ 3909 if (!dbl) 3910 normalize_constant_expr (ep); 3911 3912 if (IS_SEXT_16BIT_NUM (ep->X_add_number)) 3913 { 3914 /* We can handle 16 bit signed values with an addiu to 3915 $zero. No need to ever use daddiu here, since $zero and 3916 the result are always correct in 32 bit mode. */ 3917 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16); 3918 return; 3919 } 3920 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000) 3921 { 3922 /* We can handle 16 bit unsigned values with an ori to 3923 $zero. */ 3924 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16); 3925 return; 3926 } 3927 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number))) 3928 { 3929 /* 32 bit values require an lui. */ 3930 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16); 3931 if ((ep->X_add_number & 0xffff) != 0) 3932 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16); 3933 return; 3934 } 3935 } 3936 3937 /* The value is larger than 32 bits. */ 3938 3939 if (!dbl || HAVE_32BIT_GPRS) 3940 { 3941 char value[32]; 3942 3943 sprintf_vma (value, ep->X_add_number); 3944 as_bad (_("Number (0x%s) larger than 32 bits"), value); 3945 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16); 3946 return; 3947 } 3948 3949 if (ep->X_op != O_big) 3950 { 3951 hi32 = *ep; 3952 hi32.X_add_number = (valueT) hi32.X_add_number >> 16; 3953 hi32.X_add_number = (valueT) hi32.X_add_number >> 16; 3954 hi32.X_add_number &= 0xffffffff; 3955 lo32 = *ep; 3956 lo32.X_add_number &= 0xffffffff; 3957 } 3958 else 3959 { 3960 assert (ep->X_add_number > 2); 3961 if (ep->X_add_number == 3) 3962 generic_bignum[3] = 0; 3963 else if (ep->X_add_number > 4) 3964 as_bad (_("Number larger than 64 bits")); 3965 lo32.X_op = O_constant; 3966 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16); 3967 hi32.X_op = O_constant; 3968 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16); 3969 } 3970 3971 if (hi32.X_add_number == 0) 3972 freg = 0; 3973 else 3974 { 3975 int shift, bit; 3976 unsigned long hi, lo; 3977 3978 if (hi32.X_add_number == (offsetT) 0xffffffff) 3979 { 3980 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000) 3981 { 3982 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16); 3983 return; 3984 } 3985 if (lo32.X_add_number & 0x80000000) 3986 { 3987 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16); 3988 if (lo32.X_add_number & 0xffff) 3989 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16); 3990 return; 3991 } 3992 } 3993 3994 /* Check for 16bit shifted constant. We know that hi32 is 3995 non-zero, so start the mask on the first bit of the hi32 3996 value. */ 3997 shift = 17; 3998 do 3999 { 4000 unsigned long himask, lomask; 4001 4002 if (shift < 32) 4003 { 4004 himask = 0xffff >> (32 - shift); 4005 lomask = (0xffff << shift) & 0xffffffff; 4006 } 4007 else 4008 { 4009 himask = 0xffff << (shift - 32); 4010 lomask = 0; 4011 } 4012 if ((hi32.X_add_number & ~(offsetT) himask) == 0 4013 && (lo32.X_add_number & ~(offsetT) lomask) == 0) 4014 { 4015 expressionS tmp; 4016 4017 tmp.X_op = O_constant; 4018 if (shift < 32) 4019 tmp.X_add_number = ((hi32.X_add_number << (32 - shift)) 4020 | (lo32.X_add_number >> shift)); 4021 else 4022 tmp.X_add_number = hi32.X_add_number >> (shift - 32); 4023 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16); 4024 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<", 4025 reg, reg, (shift >= 32) ? shift - 32 : shift); 4026 return; 4027 } 4028 ++shift; 4029 } 4030 while (shift <= (64 - 16)); 4031 4032 /* Find the bit number of the lowest one bit, and store the 4033 shifted value in hi/lo. */ 4034 hi = (unsigned long) (hi32.X_add_number & 0xffffffff); 4035 lo = (unsigned long) (lo32.X_add_number & 0xffffffff); 4036 if (lo != 0) 4037 { 4038 bit = 0; 4039 while ((lo & 1) == 0) 4040 { 4041 lo >>= 1; 4042 ++bit; 4043 } 4044 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit); 4045 hi >>= bit; 4046 } 4047 else 4048 { 4049 bit = 32; 4050 while ((hi & 1) == 0) 4051 { 4052 hi >>= 1; 4053 ++bit; 4054 } 4055 lo = hi; 4056 hi = 0; 4057 } 4058 4059 /* Optimize if the shifted value is a (power of 2) - 1. */ 4060 if ((hi == 0 && ((lo + 1) & lo) == 0) 4061 || (lo == 0xffffffff && ((hi + 1) & hi) == 0)) 4062 { 4063 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number); 4064 if (shift != 0) 4065 { 4066 expressionS tmp; 4067 4068 /* This instruction will set the register to be all 4069 ones. */ 4070 tmp.X_op = O_constant; 4071 tmp.X_add_number = (offsetT) -1; 4072 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16); 4073 if (bit != 0) 4074 { 4075 bit += shift; 4076 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<", 4077 reg, reg, (bit >= 32) ? bit - 32 : bit); 4078 } 4079 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<", 4080 reg, reg, (shift >= 32) ? shift - 32 : shift); 4081 return; 4082 } 4083 } 4084 4085 /* Sign extend hi32 before calling load_register, because we can 4086 generally get better code when we load a sign extended value. */ 4087 if ((hi32.X_add_number & 0x80000000) != 0) 4088 hi32.X_add_number |= ~(offsetT) 0xffffffff; 4089 load_register (reg, &hi32, 0); 4090 freg = reg; 4091 } 4092 if ((lo32.X_add_number & 0xffff0000) == 0) 4093 { 4094 if (freg != 0) 4095 { 4096 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0); 4097 freg = reg; 4098 } 4099 } 4100 else 4101 { 4102 expressionS mid16; 4103 4104 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff)) 4105 { 4106 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16); 4107 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0); 4108 return; 4109 } 4110 4111 if (freg != 0) 4112 { 4113 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16); 4114 freg = reg; 4115 } 4116 mid16 = lo32; 4117 mid16.X_add_number >>= 16; 4118 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16); 4119 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16); 4120 freg = reg; 4121 } 4122 if ((lo32.X_add_number & 0xffff) != 0) 4123 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16); 4124 } 4125 4126 static inline void 4127 load_delay_nop (void) 4128 { 4129 if (!gpr_interlocks) 4130 macro_build (NULL, "nop", ""); 4131 } 4132 4133 /* Load an address into a register. */ 4134 4135 static void 4136 load_address (int reg, expressionS *ep, int *used_at) 4137 { 4138 if (ep->X_op != O_constant 4139 && ep->X_op != O_symbol) 4140 { 4141 as_bad (_("expression too complex")); 4142 ep->X_op = O_constant; 4143 } 4144 4145 if (ep->X_op == O_constant) 4146 { 4147 load_register (reg, ep, HAVE_64BIT_ADDRESSES); 4148 return; 4149 } 4150 4151 if (mips_pic == NO_PIC) 4152 { 4153 /* If this is a reference to a GP relative symbol, we want 4154 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16) 4155 Otherwise we want 4156 lui $reg,<sym> (BFD_RELOC_HI16_S) 4157 addiu $reg,$reg,<sym> (BFD_RELOC_LO16) 4158 If we have an addend, we always use the latter form. 4159 4160 With 64bit address space and a usable $at we want 4161 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST) 4162 lui $at,<sym> (BFD_RELOC_HI16_S) 4163 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER) 4164 daddiu $at,<sym> (BFD_RELOC_LO16) 4165 dsll32 $reg,0 4166 daddu $reg,$reg,$at 4167 4168 If $at is already in use, we use a path which is suboptimal 4169 on superscalar processors. 4170 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST) 4171 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER) 4172 dsll $reg,16 4173 daddiu $reg,<sym> (BFD_RELOC_HI16_S) 4174 dsll $reg,16 4175 daddiu $reg,<sym> (BFD_RELOC_LO16) 4176 4177 For GP relative symbols in 64bit address space we can use 4178 the same sequence as in 32bit address space. */ 4179 if (HAVE_64BIT_SYMBOLS) 4180 { 4181 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET 4182 && !nopic_need_relax (ep->X_add_symbol, 1)) 4183 { 4184 relax_start (ep->X_add_symbol); 4185 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, 4186 mips_gp_register, BFD_RELOC_GPREL16); 4187 relax_switch (); 4188 } 4189 4190 if (*used_at == 0 && !mips_opts.noat) 4191 { 4192 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST); 4193 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S); 4194 macro_build (ep, "daddiu", "t,r,j", reg, reg, 4195 BFD_RELOC_MIPS_HIGHER); 4196 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16); 4197 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0); 4198 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT); 4199 *used_at = 1; 4200 } 4201 else 4202 { 4203 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST); 4204 macro_build (ep, "daddiu", "t,r,j", reg, reg, 4205 BFD_RELOC_MIPS_HIGHER); 4206 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16); 4207 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S); 4208 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16); 4209 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16); 4210 } 4211 4212 if (mips_relax.sequence) 4213 relax_end (); 4214 } 4215 else 4216 { 4217 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET 4218 && !nopic_need_relax (ep->X_add_symbol, 1)) 4219 { 4220 relax_start (ep->X_add_symbol); 4221 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, 4222 mips_gp_register, BFD_RELOC_GPREL16); 4223 relax_switch (); 4224 } 4225 macro_build_lui (ep, reg); 4226 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", 4227 reg, reg, BFD_RELOC_LO16); 4228 if (mips_relax.sequence) 4229 relax_end (); 4230 } 4231 } 4232 else if (!mips_big_got) 4233 { 4234 expressionS ex; 4235 4236 /* If this is a reference to an external symbol, we want 4237 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 4238 Otherwise we want 4239 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 4240 nop 4241 addiu $reg,$reg,<sym> (BFD_RELOC_LO16) 4242 If there is a constant, it must be added in after. 4243 4244 If we have NewABI, we want 4245 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP) 4246 unless we're referencing a global symbol with a non-zero 4247 offset, in which case cst must be added separately. */ 4248 if (HAVE_NEWABI) 4249 { 4250 if (ep->X_add_number) 4251 { 4252 ex.X_add_number = ep->X_add_number; 4253 ep->X_add_number = 0; 4254 relax_start (ep->X_add_symbol); 4255 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg, 4256 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register); 4257 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000) 4258 as_bad (_("PIC code offset overflow (max 16 signed bits)")); 4259 ex.X_op = O_constant; 4260 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", 4261 reg, reg, BFD_RELOC_LO16); 4262 ep->X_add_number = ex.X_add_number; 4263 relax_switch (); 4264 } 4265 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg, 4266 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register); 4267 if (mips_relax.sequence) 4268 relax_end (); 4269 } 4270 else 4271 { 4272 ex.X_add_number = ep->X_add_number; 4273 ep->X_add_number = 0; 4274 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg, 4275 BFD_RELOC_MIPS_GOT16, mips_gp_register); 4276 load_delay_nop (); 4277 relax_start (ep->X_add_symbol); 4278 relax_switch (); 4279 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg, 4280 BFD_RELOC_LO16); 4281 relax_end (); 4282 4283 if (ex.X_add_number != 0) 4284 { 4285 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000) 4286 as_bad (_("PIC code offset overflow (max 16 signed bits)")); 4287 ex.X_op = O_constant; 4288 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", 4289 reg, reg, BFD_RELOC_LO16); 4290 } 4291 } 4292 } 4293 else if (mips_big_got) 4294 { 4295 expressionS ex; 4296 4297 /* This is the large GOT case. If this is a reference to an 4298 external symbol, we want 4299 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16) 4300 addu $reg,$reg,$gp 4301 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16) 4302 4303 Otherwise, for a reference to a local symbol in old ABI, we want 4304 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 4305 nop 4306 addiu $reg,$reg,<sym> (BFD_RELOC_LO16) 4307 If there is a constant, it must be added in after. 4308 4309 In the NewABI, for local symbols, with or without offsets, we want: 4310 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE) 4311 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST) 4312 */ 4313 if (HAVE_NEWABI) 4314 { 4315 ex.X_add_number = ep->X_add_number; 4316 ep->X_add_number = 0; 4317 relax_start (ep->X_add_symbol); 4318 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16); 4319 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 4320 reg, reg, mips_gp_register); 4321 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", 4322 reg, BFD_RELOC_MIPS_GOT_LO16, reg); 4323 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000) 4324 as_bad (_("PIC code offset overflow (max 16 signed bits)")); 4325 else if (ex.X_add_number) 4326 { 4327 ex.X_op = O_constant; 4328 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg, 4329 BFD_RELOC_LO16); 4330 } 4331 4332 ep->X_add_number = ex.X_add_number; 4333 relax_switch (); 4334 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg, 4335 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register); 4336 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg, 4337 BFD_RELOC_MIPS_GOT_OFST); 4338 relax_end (); 4339 } 4340 else 4341 { 4342 ex.X_add_number = ep->X_add_number; 4343 ep->X_add_number = 0; 4344 relax_start (ep->X_add_symbol); 4345 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16); 4346 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 4347 reg, reg, mips_gp_register); 4348 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", 4349 reg, BFD_RELOC_MIPS_GOT_LO16, reg); 4350 relax_switch (); 4351 if (reg_needs_delay (mips_gp_register)) 4352 { 4353 /* We need a nop before loading from $gp. This special 4354 check is required because the lui which starts the main 4355 instruction stream does not refer to $gp, and so will not 4356 insert the nop which may be required. */ 4357 macro_build (NULL, "nop", ""); 4358 } 4359 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg, 4360 BFD_RELOC_MIPS_GOT16, mips_gp_register); 4361 load_delay_nop (); 4362 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg, 4363 BFD_RELOC_LO16); 4364 relax_end (); 4365 4366 if (ex.X_add_number != 0) 4367 { 4368 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000) 4369 as_bad (_("PIC code offset overflow (max 16 signed bits)")); 4370 ex.X_op = O_constant; 4371 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg, 4372 BFD_RELOC_LO16); 4373 } 4374 } 4375 } 4376 else 4377 abort (); 4378 4379 if (mips_opts.noat && *used_at == 1) 4380 as_bad (_("Macro used $at after \".set noat\"")); 4381 } 4382 4383 /* Move the contents of register SOURCE into register DEST. */ 4384 4385 static void 4386 move_register (int dest, int source) 4387 { 4388 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t", 4389 dest, source, 0); 4390 } 4391 4392 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where 4393 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement. 4394 The two alternatives are: 4395 4396 Global symbol Local sybmol 4397 ------------- ------------ 4398 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET) 4399 ... ... 4400 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET) 4401 4402 load_got_offset emits the first instruction and add_got_offset 4403 emits the second for a 16-bit offset or add_got_offset_hilo emits 4404 a sequence to add a 32-bit offset using a scratch register. */ 4405 4406 static void 4407 load_got_offset (int dest, expressionS *local) 4408 { 4409 expressionS global; 4410 4411 global = *local; 4412 global.X_add_number = 0; 4413 4414 relax_start (local->X_add_symbol); 4415 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest, 4416 BFD_RELOC_MIPS_GOT16, mips_gp_register); 4417 relax_switch (); 4418 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest, 4419 BFD_RELOC_MIPS_GOT16, mips_gp_register); 4420 relax_end (); 4421 } 4422 4423 static void 4424 add_got_offset (int dest, expressionS *local) 4425 { 4426 expressionS global; 4427 4428 global.X_op = O_constant; 4429 global.X_op_symbol = NULL; 4430 global.X_add_symbol = NULL; 4431 global.X_add_number = local->X_add_number; 4432 4433 relax_start (local->X_add_symbol); 4434 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j", 4435 dest, dest, BFD_RELOC_LO16); 4436 relax_switch (); 4437 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16); 4438 relax_end (); 4439 } 4440 4441 static void 4442 add_got_offset_hilo (int dest, expressionS *local, int tmp) 4443 { 4444 expressionS global; 4445 int hold_mips_optimize; 4446 4447 global.X_op = O_constant; 4448 global.X_op_symbol = NULL; 4449 global.X_add_symbol = NULL; 4450 global.X_add_number = local->X_add_number; 4451 4452 relax_start (local->X_add_symbol); 4453 load_register (tmp, &global, HAVE_64BIT_ADDRESSES); 4454 relax_switch (); 4455 /* Set mips_optimize around the lui instruction to avoid 4456 inserting an unnecessary nop after the lw. */ 4457 hold_mips_optimize = mips_optimize; 4458 mips_optimize = 2; 4459 macro_build_lui (&global, tmp); 4460 mips_optimize = hold_mips_optimize; 4461 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16); 4462 relax_end (); 4463 4464 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp); 4465 } 4466 4467 /* 4468 * Build macros 4469 * This routine implements the seemingly endless macro or synthesized 4470 * instructions and addressing modes in the mips assembly language. Many 4471 * of these macros are simple and are similar to each other. These could 4472 * probably be handled by some kind of table or grammar approach instead of 4473 * this verbose method. Others are not simple macros but are more like 4474 * optimizing code generation. 4475 * One interesting optimization is when several store macros appear 4476 * consecutively that would load AT with the upper half of the same address. 4477 * The ensuing load upper instructions are ommited. This implies some kind 4478 * of global optimization. We currently only optimize within a single macro. 4479 * For many of the load and store macros if the address is specified as a 4480 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we 4481 * first load register 'at' with zero and use it as the base register. The 4482 * mips assembler simply uses register $zero. Just one tiny optimization 4483 * we're missing. 4484 */ 4485 static void 4486 macro (struct mips_cl_insn *ip) 4487 { 4488 register int treg, sreg, dreg, breg; 4489 int tempreg; 4490 int mask; 4491 int used_at = 0; 4492 expressionS expr1; 4493 const char *s; 4494 const char *s2; 4495 const char *fmt; 4496 int likely = 0; 4497 int dbl = 0; 4498 int coproc = 0; 4499 int lr = 0; 4500 int imm = 0; 4501 int call = 0; 4502 int off; 4503 offsetT maxnum; 4504 bfd_reloc_code_real_type r; 4505 int hold_mips_optimize; 4506 4507 assert (! mips_opts.mips16); 4508 4509 treg = (ip->insn_opcode >> 16) & 0x1f; 4510 dreg = (ip->insn_opcode >> 11) & 0x1f; 4511 sreg = breg = (ip->insn_opcode >> 21) & 0x1f; 4512 mask = ip->insn_mo->mask; 4513 4514 expr1.X_op = O_constant; 4515 expr1.X_op_symbol = NULL; 4516 expr1.X_add_symbol = NULL; 4517 expr1.X_add_number = 1; 4518 4519 switch (mask) 4520 { 4521 case M_DABS: 4522 dbl = 1; 4523 case M_ABS: 4524 /* bgez $a0,.+12 4525 move v0,$a0 4526 sub v0,$zero,$a0 4527 */ 4528 4529 start_noreorder (); 4530 4531 expr1.X_add_number = 8; 4532 macro_build (&expr1, "bgez", "s,p", sreg); 4533 if (dreg == sreg) 4534 macro_build (NULL, "nop", "", 0); 4535 else 4536 move_register (dreg, sreg); 4537 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg); 4538 4539 end_noreorder (); 4540 break; 4541 4542 case M_ADD_I: 4543 s = "addi"; 4544 s2 = "add"; 4545 goto do_addi; 4546 case M_ADDU_I: 4547 s = "addiu"; 4548 s2 = "addu"; 4549 goto do_addi; 4550 case M_DADD_I: 4551 dbl = 1; 4552 s = "daddi"; 4553 s2 = "dadd"; 4554 goto do_addi; 4555 case M_DADDU_I: 4556 dbl = 1; 4557 s = "daddiu"; 4558 s2 = "daddu"; 4559 do_addi: 4560 if (imm_expr.X_op == O_constant 4561 && imm_expr.X_add_number >= -0x8000 4562 && imm_expr.X_add_number < 0x8000) 4563 { 4564 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16); 4565 break; 4566 } 4567 used_at = 1; 4568 load_register (AT, &imm_expr, dbl); 4569 macro_build (NULL, s2, "d,v,t", treg, sreg, AT); 4570 break; 4571 4572 case M_AND_I: 4573 s = "andi"; 4574 s2 = "and"; 4575 goto do_bit; 4576 case M_OR_I: 4577 s = "ori"; 4578 s2 = "or"; 4579 goto do_bit; 4580 case M_NOR_I: 4581 s = ""; 4582 s2 = "nor"; 4583 goto do_bit; 4584 case M_XOR_I: 4585 s = "xori"; 4586 s2 = "xor"; 4587 do_bit: 4588 if (imm_expr.X_op == O_constant 4589 && imm_expr.X_add_number >= 0 4590 && imm_expr.X_add_number < 0x10000) 4591 { 4592 if (mask != M_NOR_I) 4593 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16); 4594 else 4595 { 4596 macro_build (&imm_expr, "ori", "t,r,i", 4597 treg, sreg, BFD_RELOC_LO16); 4598 macro_build (NULL, "nor", "d,v,t", treg, treg, 0); 4599 } 4600 break; 4601 } 4602 4603 used_at = 1; 4604 load_register (AT, &imm_expr, HAVE_64BIT_GPRS); 4605 macro_build (NULL, s2, "d,v,t", treg, sreg, AT); 4606 break; 4607 4608 case M_BEQ_I: 4609 s = "beq"; 4610 goto beq_i; 4611 case M_BEQL_I: 4612 s = "beql"; 4613 likely = 1; 4614 goto beq_i; 4615 case M_BNE_I: 4616 s = "bne"; 4617 goto beq_i; 4618 case M_BNEL_I: 4619 s = "bnel"; 4620 likely = 1; 4621 beq_i: 4622 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0) 4623 { 4624 macro_build (&offset_expr, s, "s,t,p", sreg, 0); 4625 break; 4626 } 4627 used_at = 1; 4628 load_register (AT, &imm_expr, HAVE_64BIT_GPRS); 4629 macro_build (&offset_expr, s, "s,t,p", sreg, AT); 4630 break; 4631 4632 case M_BGEL: 4633 likely = 1; 4634 case M_BGE: 4635 if (treg == 0) 4636 { 4637 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg); 4638 break; 4639 } 4640 if (sreg == 0) 4641 { 4642 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg); 4643 break; 4644 } 4645 used_at = 1; 4646 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg); 4647 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0); 4648 break; 4649 4650 case M_BGTL_I: 4651 likely = 1; 4652 case M_BGT_I: 4653 /* check for > max integer */ 4654 maxnum = 0x7fffffff; 4655 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4) 4656 { 4657 maxnum <<= 16; 4658 maxnum |= 0xffff; 4659 maxnum <<= 16; 4660 maxnum |= 0xffff; 4661 } 4662 if (imm_expr.X_op == O_constant 4663 && imm_expr.X_add_number >= maxnum 4664 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4)) 4665 { 4666 do_false: 4667 /* result is always false */ 4668 if (! likely) 4669 macro_build (NULL, "nop", "", 0); 4670 else 4671 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0); 4672 break; 4673 } 4674 if (imm_expr.X_op != O_constant) 4675 as_bad (_("Unsupported large constant")); 4676 ++imm_expr.X_add_number; 4677 /* FALLTHROUGH */ 4678 case M_BGE_I: 4679 case M_BGEL_I: 4680 if (mask == M_BGEL_I) 4681 likely = 1; 4682 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0) 4683 { 4684 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg); 4685 break; 4686 } 4687 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1) 4688 { 4689 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg); 4690 break; 4691 } 4692 maxnum = 0x7fffffff; 4693 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4) 4694 { 4695 maxnum <<= 16; 4696 maxnum |= 0xffff; 4697 maxnum <<= 16; 4698 maxnum |= 0xffff; 4699 } 4700 maxnum = - maxnum - 1; 4701 if (imm_expr.X_op == O_constant 4702 && imm_expr.X_add_number <= maxnum 4703 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4)) 4704 { 4705 do_true: 4706 /* result is always true */ 4707 as_warn (_("Branch %s is always true"), ip->insn_mo->name); 4708 macro_build (&offset_expr, "b", "p"); 4709 break; 4710 } 4711 used_at = 1; 4712 set_at (sreg, 0); 4713 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0); 4714 break; 4715 4716 case M_BGEUL: 4717 likely = 1; 4718 case M_BGEU: 4719 if (treg == 0) 4720 goto do_true; 4721 if (sreg == 0) 4722 { 4723 macro_build (&offset_expr, likely ? "beql" : "beq", 4724 "s,t,p", 0, treg); 4725 break; 4726 } 4727 used_at = 1; 4728 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg); 4729 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0); 4730 break; 4731 4732 case M_BGTUL_I: 4733 likely = 1; 4734 case M_BGTU_I: 4735 if (sreg == 0 4736 || (HAVE_32BIT_GPRS 4737 && imm_expr.X_op == O_constant 4738 && imm_expr.X_add_number == (offsetT) 0xffffffff)) 4739 goto do_false; 4740 if (imm_expr.X_op != O_constant) 4741 as_bad (_("Unsupported large constant")); 4742 ++imm_expr.X_add_number; 4743 /* FALLTHROUGH */ 4744 case M_BGEU_I: 4745 case M_BGEUL_I: 4746 if (mask == M_BGEUL_I) 4747 likely = 1; 4748 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0) 4749 goto do_true; 4750 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1) 4751 { 4752 macro_build (&offset_expr, likely ? "bnel" : "bne", 4753 "s,t,p", sreg, 0); 4754 break; 4755 } 4756 used_at = 1; 4757 set_at (sreg, 1); 4758 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0); 4759 break; 4760 4761 case M_BGTL: 4762 likely = 1; 4763 case M_BGT: 4764 if (treg == 0) 4765 { 4766 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg); 4767 break; 4768 } 4769 if (sreg == 0) 4770 { 4771 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg); 4772 break; 4773 } 4774 used_at = 1; 4775 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg); 4776 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0); 4777 break; 4778 4779 case M_BGTUL: 4780 likely = 1; 4781 case M_BGTU: 4782 if (treg == 0) 4783 { 4784 macro_build (&offset_expr, likely ? "bnel" : "bne", 4785 "s,t,p", sreg, 0); 4786 break; 4787 } 4788 if (sreg == 0) 4789 goto do_false; 4790 used_at = 1; 4791 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg); 4792 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0); 4793 break; 4794 4795 case M_BLEL: 4796 likely = 1; 4797 case M_BLE: 4798 if (treg == 0) 4799 { 4800 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg); 4801 break; 4802 } 4803 if (sreg == 0) 4804 { 4805 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg); 4806 break; 4807 } 4808 used_at = 1; 4809 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg); 4810 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0); 4811 break; 4812 4813 case M_BLEL_I: 4814 likely = 1; 4815 case M_BLE_I: 4816 maxnum = 0x7fffffff; 4817 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4) 4818 { 4819 maxnum <<= 16; 4820 maxnum |= 0xffff; 4821 maxnum <<= 16; 4822 maxnum |= 0xffff; 4823 } 4824 if (imm_expr.X_op == O_constant 4825 && imm_expr.X_add_number >= maxnum 4826 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4)) 4827 goto do_true; 4828 if (imm_expr.X_op != O_constant) 4829 as_bad (_("Unsupported large constant")); 4830 ++imm_expr.X_add_number; 4831 /* FALLTHROUGH */ 4832 case M_BLT_I: 4833 case M_BLTL_I: 4834 if (mask == M_BLTL_I) 4835 likely = 1; 4836 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0) 4837 { 4838 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg); 4839 break; 4840 } 4841 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1) 4842 { 4843 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg); 4844 break; 4845 } 4846 used_at = 1; 4847 set_at (sreg, 0); 4848 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0); 4849 break; 4850 4851 case M_BLEUL: 4852 likely = 1; 4853 case M_BLEU: 4854 if (treg == 0) 4855 { 4856 macro_build (&offset_expr, likely ? "beql" : "beq", 4857 "s,t,p", sreg, 0); 4858 break; 4859 } 4860 if (sreg == 0) 4861 goto do_true; 4862 used_at = 1; 4863 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg); 4864 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0); 4865 break; 4866 4867 case M_BLEUL_I: 4868 likely = 1; 4869 case M_BLEU_I: 4870 if (sreg == 0 4871 || (HAVE_32BIT_GPRS 4872 && imm_expr.X_op == O_constant 4873 && imm_expr.X_add_number == (offsetT) 0xffffffff)) 4874 goto do_true; 4875 if (imm_expr.X_op != O_constant) 4876 as_bad (_("Unsupported large constant")); 4877 ++imm_expr.X_add_number; 4878 /* FALLTHROUGH */ 4879 case M_BLTU_I: 4880 case M_BLTUL_I: 4881 if (mask == M_BLTUL_I) 4882 likely = 1; 4883 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0) 4884 goto do_false; 4885 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1) 4886 { 4887 macro_build (&offset_expr, likely ? "beql" : "beq", 4888 "s,t,p", sreg, 0); 4889 break; 4890 } 4891 used_at = 1; 4892 set_at (sreg, 1); 4893 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0); 4894 break; 4895 4896 case M_BLTL: 4897 likely = 1; 4898 case M_BLT: 4899 if (treg == 0) 4900 { 4901 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg); 4902 break; 4903 } 4904 if (sreg == 0) 4905 { 4906 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg); 4907 break; 4908 } 4909 used_at = 1; 4910 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg); 4911 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0); 4912 break; 4913 4914 case M_BLTUL: 4915 likely = 1; 4916 case M_BLTU: 4917 if (treg == 0) 4918 goto do_false; 4919 if (sreg == 0) 4920 { 4921 macro_build (&offset_expr, likely ? "bnel" : "bne", 4922 "s,t,p", 0, treg); 4923 break; 4924 } 4925 used_at = 1; 4926 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg); 4927 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0); 4928 break; 4929 4930 case M_DEXT: 4931 { 4932 unsigned long pos; 4933 unsigned long size; 4934 4935 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant) 4936 { 4937 as_bad (_("Unsupported large constant")); 4938 pos = size = 1; 4939 } 4940 else 4941 { 4942 pos = (unsigned long) imm_expr.X_add_number; 4943 size = (unsigned long) imm2_expr.X_add_number; 4944 } 4945 4946 if (pos > 63) 4947 { 4948 as_bad (_("Improper position (%lu)"), pos); 4949 pos = 1; 4950 } 4951 if (size == 0 || size > 64 4952 || (pos + size - 1) > 63) 4953 { 4954 as_bad (_("Improper extract size (%lu, position %lu)"), 4955 size, pos); 4956 size = 1; 4957 } 4958 4959 if (size <= 32 && pos < 32) 4960 { 4961 s = "dext"; 4962 fmt = "t,r,+A,+C"; 4963 } 4964 else if (size <= 32) 4965 { 4966 s = "dextu"; 4967 fmt = "t,r,+E,+H"; 4968 } 4969 else 4970 { 4971 s = "dextm"; 4972 fmt = "t,r,+A,+G"; 4973 } 4974 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1); 4975 } 4976 break; 4977 4978 case M_DINS: 4979 { 4980 unsigned long pos; 4981 unsigned long size; 4982 4983 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant) 4984 { 4985 as_bad (_("Unsupported large constant")); 4986 pos = size = 1; 4987 } 4988 else 4989 { 4990 pos = (unsigned long) imm_expr.X_add_number; 4991 size = (unsigned long) imm2_expr.X_add_number; 4992 } 4993 4994 if (pos > 63) 4995 { 4996 as_bad (_("Improper position (%lu)"), pos); 4997 pos = 1; 4998 } 4999 if (size == 0 || size > 64 5000 || (pos + size - 1) > 63) 5001 { 5002 as_bad (_("Improper insert size (%lu, position %lu)"), 5003 size, pos); 5004 size = 1; 5005 } 5006 5007 if (pos < 32 && (pos + size - 1) < 32) 5008 { 5009 s = "dins"; 5010 fmt = "t,r,+A,+B"; 5011 } 5012 else if (pos >= 32) 5013 { 5014 s = "dinsu"; 5015 fmt = "t,r,+E,+F"; 5016 } 5017 else 5018 { 5019 s = "dinsm"; 5020 fmt = "t,r,+A,+F"; 5021 } 5022 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, 5023 pos + size - 1); 5024 } 5025 break; 5026 5027 case M_DDIV_3: 5028 dbl = 1; 5029 case M_DIV_3: 5030 s = "mflo"; 5031 goto do_div3; 5032 case M_DREM_3: 5033 dbl = 1; 5034 case M_REM_3: 5035 s = "mfhi"; 5036 do_div3: 5037 if (treg == 0) 5038 { 5039 as_warn (_("Divide by zero.")); 5040 if (mips_trap) 5041 macro_build (NULL, "teq", "s,t,q", 0, 0, 7); 5042 else 5043 macro_build (NULL, "break", "c", 7); 5044 break; 5045 } 5046 5047 start_noreorder (); 5048 if (mips_trap) 5049 { 5050 macro_build (NULL, "teq", "s,t,q", treg, 0, 7); 5051 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg); 5052 } 5053 else 5054 { 5055 expr1.X_add_number = 8; 5056 macro_build (&expr1, "bne", "s,t,p", treg, 0); 5057 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg); 5058 macro_build (NULL, "break", "c", 7); 5059 } 5060 expr1.X_add_number = -1; 5061 used_at = 1; 5062 load_register (AT, &expr1, dbl); 5063 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16); 5064 macro_build (&expr1, "bne", "s,t,p", treg, AT); 5065 if (dbl) 5066 { 5067 expr1.X_add_number = 1; 5068 load_register (AT, &expr1, dbl); 5069 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31); 5070 } 5071 else 5072 { 5073 expr1.X_add_number = 0x80000000; 5074 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16); 5075 } 5076 if (mips_trap) 5077 { 5078 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6); 5079 /* We want to close the noreorder block as soon as possible, so 5080 that later insns are available for delay slot filling. */ 5081 end_noreorder (); 5082 } 5083 else 5084 { 5085 expr1.X_add_number = 8; 5086 macro_build (&expr1, "bne", "s,t,p", sreg, AT); 5087 macro_build (NULL, "nop", "", 0); 5088 5089 /* We want to close the noreorder block as soon as possible, so 5090 that later insns are available for delay slot filling. */ 5091 end_noreorder (); 5092 5093 macro_build (NULL, "break", "c", 6); 5094 } 5095 macro_build (NULL, s, "d", dreg); 5096 break; 5097 5098 case M_DIV_3I: 5099 s = "div"; 5100 s2 = "mflo"; 5101 goto do_divi; 5102 case M_DIVU_3I: 5103 s = "divu"; 5104 s2 = "mflo"; 5105 goto do_divi; 5106 case M_REM_3I: 5107 s = "div"; 5108 s2 = "mfhi"; 5109 goto do_divi; 5110 case M_REMU_3I: 5111 s = "divu"; 5112 s2 = "mfhi"; 5113 goto do_divi; 5114 case M_DDIV_3I: 5115 dbl = 1; 5116 s = "ddiv"; 5117 s2 = "mflo"; 5118 goto do_divi; 5119 case M_DDIVU_3I: 5120 dbl = 1; 5121 s = "ddivu"; 5122 s2 = "mflo"; 5123 goto do_divi; 5124 case M_DREM_3I: 5125 dbl = 1; 5126 s = "ddiv"; 5127 s2 = "mfhi"; 5128 goto do_divi; 5129 case M_DREMU_3I: 5130 dbl = 1; 5131 s = "ddivu"; 5132 s2 = "mfhi"; 5133 do_divi: 5134 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0) 5135 { 5136 as_warn (_("Divide by zero.")); 5137 if (mips_trap) 5138 macro_build (NULL, "teq", "s,t,q", 0, 0, 7); 5139 else 5140 macro_build (NULL, "break", "c", 7); 5141 break; 5142 } 5143 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1) 5144 { 5145 if (strcmp (s2, "mflo") == 0) 5146 move_register (dreg, sreg); 5147 else 5148 move_register (dreg, 0); 5149 break; 5150 } 5151 if (imm_expr.X_op == O_constant 5152 && imm_expr.X_add_number == -1 5153 && s[strlen (s) - 1] != 'u') 5154 { 5155 if (strcmp (s2, "mflo") == 0) 5156 { 5157 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg); 5158 } 5159 else 5160 move_register (dreg, 0); 5161 break; 5162 } 5163 5164 used_at = 1; 5165 load_register (AT, &imm_expr, dbl); 5166 macro_build (NULL, s, "z,s,t", sreg, AT); 5167 macro_build (NULL, s2, "d", dreg); 5168 break; 5169 5170 case M_DIVU_3: 5171 s = "divu"; 5172 s2 = "mflo"; 5173 goto do_divu3; 5174 case M_REMU_3: 5175 s = "divu"; 5176 s2 = "mfhi"; 5177 goto do_divu3; 5178 case M_DDIVU_3: 5179 s = "ddivu"; 5180 s2 = "mflo"; 5181 goto do_divu3; 5182 case M_DREMU_3: 5183 s = "ddivu"; 5184 s2 = "mfhi"; 5185 do_divu3: 5186 start_noreorder (); 5187 if (mips_trap) 5188 { 5189 macro_build (NULL, "teq", "s,t,q", treg, 0, 7); 5190 macro_build (NULL, s, "z,s,t", sreg, treg); 5191 /* We want to close the noreorder block as soon as possible, so 5192 that later insns are available for delay slot filling. */ 5193 end_noreorder (); 5194 } 5195 else 5196 { 5197 expr1.X_add_number = 8; 5198 macro_build (&expr1, "bne", "s,t,p", treg, 0); 5199 macro_build (NULL, s, "z,s,t", sreg, treg); 5200 5201 /* We want to close the noreorder block as soon as possible, so 5202 that later insns are available for delay slot filling. */ 5203 end_noreorder (); 5204 macro_build (NULL, "break", "c", 7); 5205 } 5206 macro_build (NULL, s2, "d", dreg); 5207 break; 5208 5209 case M_DLCA_AB: 5210 dbl = 1; 5211 case M_LCA_AB: 5212 call = 1; 5213 goto do_la; 5214 case M_DLA_AB: 5215 dbl = 1; 5216 case M_LA_AB: 5217 do_la: 5218 /* Load the address of a symbol into a register. If breg is not 5219 zero, we then add a base register to it. */ 5220 5221 if (dbl && HAVE_32BIT_GPRS) 5222 as_warn (_("dla used to load 32-bit register")); 5223 5224 if (! dbl && HAVE_64BIT_OBJECTS) 5225 as_warn (_("la used to load 64-bit address")); 5226 5227 if (offset_expr.X_op == O_constant 5228 && offset_expr.X_add_number >= -0x8000 5229 && offset_expr.X_add_number < 0x8000) 5230 { 5231 macro_build (&offset_expr, ADDRESS_ADDI_INSN, 5232 "t,r,j", treg, sreg, BFD_RELOC_LO16); 5233 break; 5234 } 5235 5236 if (!mips_opts.noat && (treg == breg)) 5237 { 5238 tempreg = AT; 5239 used_at = 1; 5240 } 5241 else 5242 { 5243 tempreg = treg; 5244 } 5245 5246 if (offset_expr.X_op != O_symbol 5247 && offset_expr.X_op != O_constant) 5248 { 5249 as_bad (_("expression too complex")); 5250 offset_expr.X_op = O_constant; 5251 } 5252 5253 if (offset_expr.X_op == O_constant) 5254 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES); 5255 else if (mips_pic == NO_PIC) 5256 { 5257 /* If this is a reference to a GP relative symbol, we want 5258 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16) 5259 Otherwise we want 5260 lui $tempreg,<sym> (BFD_RELOC_HI16_S) 5261 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16) 5262 If we have a constant, we need two instructions anyhow, 5263 so we may as well always use the latter form. 5264 5265 With 64bit address space and a usable $at we want 5266 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST) 5267 lui $at,<sym> (BFD_RELOC_HI16_S) 5268 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER) 5269 daddiu $at,<sym> (BFD_RELOC_LO16) 5270 dsll32 $tempreg,0 5271 daddu $tempreg,$tempreg,$at 5272 5273 If $at is already in use, we use a path which is suboptimal 5274 on superscalar processors. 5275 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST) 5276 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER) 5277 dsll $tempreg,16 5278 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S) 5279 dsll $tempreg,16 5280 daddiu $tempreg,<sym> (BFD_RELOC_LO16) 5281 5282 For GP relative symbols in 64bit address space we can use 5283 the same sequence as in 32bit address space. */ 5284 if (HAVE_64BIT_SYMBOLS) 5285 { 5286 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET 5287 && !nopic_need_relax (offset_expr.X_add_symbol, 1)) 5288 { 5289 relax_start (offset_expr.X_add_symbol); 5290 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", 5291 tempreg, mips_gp_register, BFD_RELOC_GPREL16); 5292 relax_switch (); 5293 } 5294 5295 if (used_at == 0 && !mips_opts.noat) 5296 { 5297 macro_build (&offset_expr, "lui", "t,u", 5298 tempreg, BFD_RELOC_MIPS_HIGHEST); 5299 macro_build (&offset_expr, "lui", "t,u", 5300 AT, BFD_RELOC_HI16_S); 5301 macro_build (&offset_expr, "daddiu", "t,r,j", 5302 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER); 5303 macro_build (&offset_expr, "daddiu", "t,r,j", 5304 AT, AT, BFD_RELOC_LO16); 5305 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0); 5306 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT); 5307 used_at = 1; 5308 } 5309 else 5310 { 5311 macro_build (&offset_expr, "lui", "t,u", 5312 tempreg, BFD_RELOC_MIPS_HIGHEST); 5313 macro_build (&offset_expr, "daddiu", "t,r,j", 5314 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER); 5315 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16); 5316 macro_build (&offset_expr, "daddiu", "t,r,j", 5317 tempreg, tempreg, BFD_RELOC_HI16_S); 5318 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16); 5319 macro_build (&offset_expr, "daddiu", "t,r,j", 5320 tempreg, tempreg, BFD_RELOC_LO16); 5321 } 5322 5323 if (mips_relax.sequence) 5324 relax_end (); 5325 } 5326 else 5327 { 5328 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET 5329 && !nopic_need_relax (offset_expr.X_add_symbol, 1)) 5330 { 5331 relax_start (offset_expr.X_add_symbol); 5332 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", 5333 tempreg, mips_gp_register, BFD_RELOC_GPREL16); 5334 relax_switch (); 5335 } 5336 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number)) 5337 as_bad (_("offset too large")); 5338 macro_build_lui (&offset_expr, tempreg); 5339 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", 5340 tempreg, tempreg, BFD_RELOC_LO16); 5341 if (mips_relax.sequence) 5342 relax_end (); 5343 } 5344 } 5345 else if (!mips_big_got && !HAVE_NEWABI) 5346 { 5347 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16; 5348 5349 /* If this is a reference to an external symbol, and there 5350 is no constant, we want 5351 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 5352 or for lca or if tempreg is PIC_CALL_REG 5353 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16) 5354 For a local symbol, we want 5355 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 5356 nop 5357 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16) 5358 5359 If we have a small constant, and this is a reference to 5360 an external symbol, we want 5361 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 5362 nop 5363 addiu $tempreg,$tempreg,<constant> 5364 For a local symbol, we want the same instruction 5365 sequence, but we output a BFD_RELOC_LO16 reloc on the 5366 addiu instruction. 5367 5368 If we have a large constant, and this is a reference to 5369 an external symbol, we want 5370 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 5371 lui $at,<hiconstant> 5372 addiu $at,$at,<loconstant> 5373 addu $tempreg,$tempreg,$at 5374 For a local symbol, we want the same instruction 5375 sequence, but we output a BFD_RELOC_LO16 reloc on the 5376 addiu instruction. 5377 */ 5378 5379 if (offset_expr.X_add_number == 0) 5380 { 5381 if (mips_pic == SVR4_PIC 5382 && breg == 0 5383 && (call || tempreg == PIC_CALL_REG)) 5384 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16; 5385 5386 relax_start (offset_expr.X_add_symbol); 5387 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg, 5388 lw_reloc_type, mips_gp_register); 5389 if (breg != 0) 5390 { 5391 /* We're going to put in an addu instruction using 5392 tempreg, so we may as well insert the nop right 5393 now. */ 5394 load_delay_nop (); 5395 } 5396 relax_switch (); 5397 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", 5398 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register); 5399 load_delay_nop (); 5400 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", 5401 tempreg, tempreg, BFD_RELOC_LO16); 5402 relax_end (); 5403 /* FIXME: If breg == 0, and the next instruction uses 5404 $tempreg, then if this variant case is used an extra 5405 nop will be generated. */ 5406 } 5407 else if (offset_expr.X_add_number >= -0x8000 5408 && offset_expr.X_add_number < 0x8000) 5409 { 5410 load_got_offset (tempreg, &offset_expr); 5411 load_delay_nop (); 5412 add_got_offset (tempreg, &offset_expr); 5413 } 5414 else 5415 { 5416 expr1.X_add_number = offset_expr.X_add_number; 5417 offset_expr.X_add_number = 5418 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000; 5419 load_got_offset (tempreg, &offset_expr); 5420 offset_expr.X_add_number = expr1.X_add_number; 5421 /* If we are going to add in a base register, and the 5422 target register and the base register are the same, 5423 then we are using AT as a temporary register. Since 5424 we want to load the constant into AT, we add our 5425 current AT (from the global offset table) and the 5426 register into the register now, and pretend we were 5427 not using a base register. */ 5428 if (breg == treg) 5429 { 5430 load_delay_nop (); 5431 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 5432 treg, AT, breg); 5433 breg = 0; 5434 tempreg = treg; 5435 } 5436 add_got_offset_hilo (tempreg, &offset_expr, AT); 5437 used_at = 1; 5438 } 5439 } 5440 else if (!mips_big_got && HAVE_NEWABI) 5441 { 5442 int add_breg_early = 0; 5443 5444 /* If this is a reference to an external, and there is no 5445 constant, or local symbol (*), with or without a 5446 constant, we want 5447 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP) 5448 or for lca or if tempreg is PIC_CALL_REG 5449 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16) 5450 5451 If we have a small constant, and this is a reference to 5452 an external symbol, we want 5453 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP) 5454 addiu $tempreg,$tempreg,<constant> 5455 5456 If we have a large constant, and this is a reference to 5457 an external symbol, we want 5458 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP) 5459 lui $at,<hiconstant> 5460 addiu $at,$at,<loconstant> 5461 addu $tempreg,$tempreg,$at 5462 5463 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for 5464 local symbols, even though it introduces an additional 5465 instruction. */ 5466 5467 if (offset_expr.X_add_number) 5468 { 5469 expr1.X_add_number = offset_expr.X_add_number; 5470 offset_expr.X_add_number = 0; 5471 5472 relax_start (offset_expr.X_add_symbol); 5473 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg, 5474 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register); 5475 5476 if (expr1.X_add_number >= -0x8000 5477 && expr1.X_add_number < 0x8000) 5478 { 5479 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j", 5480 tempreg, tempreg, BFD_RELOC_LO16); 5481 } 5482 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000)) 5483 { 5484 int dreg; 5485 5486 /* If we are going to add in a base register, and the 5487 target register and the base register are the same, 5488 then we are using AT as a temporary register. Since 5489 we want to load the constant into AT, we add our 5490 current AT (from the global offset table) and the 5491 register into the register now, and pretend we were 5492 not using a base register. */ 5493 if (breg != treg) 5494 dreg = tempreg; 5495 else 5496 { 5497 assert (tempreg == AT); 5498 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 5499 treg, AT, breg); 5500 dreg = treg; 5501 add_breg_early = 1; 5502 } 5503 5504 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES); 5505 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 5506 dreg, dreg, AT); 5507 5508 used_at = 1; 5509 } 5510 else 5511 as_bad (_("PIC code offset overflow (max 32 signed bits)")); 5512 5513 relax_switch (); 5514 offset_expr.X_add_number = expr1.X_add_number; 5515 5516 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg, 5517 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register); 5518 if (add_breg_early) 5519 { 5520 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 5521 treg, tempreg, breg); 5522 breg = 0; 5523 tempreg = treg; 5524 } 5525 relax_end (); 5526 } 5527 else if (breg == 0 && (call || tempreg == PIC_CALL_REG)) 5528 { 5529 relax_start (offset_expr.X_add_symbol); 5530 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg, 5531 BFD_RELOC_MIPS_CALL16, mips_gp_register); 5532 relax_switch (); 5533 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg, 5534 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register); 5535 relax_end (); 5536 } 5537 else 5538 { 5539 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg, 5540 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register); 5541 } 5542 } 5543 else if (mips_big_got && !HAVE_NEWABI) 5544 { 5545 int gpdelay; 5546 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16; 5547 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16; 5548 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16; 5549 5550 /* This is the large GOT case. If this is a reference to an 5551 external symbol, and there is no constant, we want 5552 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16) 5553 addu $tempreg,$tempreg,$gp 5554 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16) 5555 or for lca or if tempreg is PIC_CALL_REG 5556 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16) 5557 addu $tempreg,$tempreg,$gp 5558 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16) 5559 For a local symbol, we want 5560 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 5561 nop 5562 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16) 5563 5564 If we have a small constant, and this is a reference to 5565 an external symbol, we want 5566 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16) 5567 addu $tempreg,$tempreg,$gp 5568 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16) 5569 nop 5570 addiu $tempreg,$tempreg,<constant> 5571 For a local symbol, we want 5572 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 5573 nop 5574 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16) 5575 5576 If we have a large constant, and this is a reference to 5577 an external symbol, we want 5578 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16) 5579 addu $tempreg,$tempreg,$gp 5580 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16) 5581 lui $at,<hiconstant> 5582 addiu $at,$at,<loconstant> 5583 addu $tempreg,$tempreg,$at 5584 For a local symbol, we want 5585 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 5586 lui $at,<hiconstant> 5587 addiu $at,$at,<loconstant> (BFD_RELOC_LO16) 5588 addu $tempreg,$tempreg,$at 5589 */ 5590 5591 expr1.X_add_number = offset_expr.X_add_number; 5592 offset_expr.X_add_number = 0; 5593 relax_start (offset_expr.X_add_symbol); 5594 gpdelay = reg_needs_delay (mips_gp_register); 5595 if (expr1.X_add_number == 0 && breg == 0 5596 && (call || tempreg == PIC_CALL_REG)) 5597 { 5598 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16; 5599 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16; 5600 } 5601 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type); 5602 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 5603 tempreg, tempreg, mips_gp_register); 5604 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", 5605 tempreg, lw_reloc_type, tempreg); 5606 if (expr1.X_add_number == 0) 5607 { 5608 if (breg != 0) 5609 { 5610 /* We're going to put in an addu instruction using 5611 tempreg, so we may as well insert the nop right 5612 now. */ 5613 load_delay_nop (); 5614 } 5615 } 5616 else if (expr1.X_add_number >= -0x8000 5617 && expr1.X_add_number < 0x8000) 5618 { 5619 load_delay_nop (); 5620 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j", 5621 tempreg, tempreg, BFD_RELOC_LO16); 5622 } 5623 else 5624 { 5625 int dreg; 5626 5627 /* If we are going to add in a base register, and the 5628 target register and the base register are the same, 5629 then we are using AT as a temporary register. Since 5630 we want to load the constant into AT, we add our 5631 current AT (from the global offset table) and the 5632 register into the register now, and pretend we were 5633 not using a base register. */ 5634 if (breg != treg) 5635 dreg = tempreg; 5636 else 5637 { 5638 assert (tempreg == AT); 5639 load_delay_nop (); 5640 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 5641 treg, AT, breg); 5642 dreg = treg; 5643 } 5644 5645 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES); 5646 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT); 5647 5648 used_at = 1; 5649 } 5650 offset_expr.X_add_number = 5651 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000; 5652 relax_switch (); 5653 5654 if (gpdelay) 5655 { 5656 /* This is needed because this instruction uses $gp, but 5657 the first instruction on the main stream does not. */ 5658 macro_build (NULL, "nop", ""); 5659 } 5660 5661 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg, 5662 local_reloc_type, mips_gp_register); 5663 if (expr1.X_add_number >= -0x8000 5664 && expr1.X_add_number < 0x8000) 5665 { 5666 load_delay_nop (); 5667 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", 5668 tempreg, tempreg, BFD_RELOC_LO16); 5669 /* FIXME: If add_number is 0, and there was no base 5670 register, the external symbol case ended with a load, 5671 so if the symbol turns out to not be external, and 5672 the next instruction uses tempreg, an unnecessary nop 5673 will be inserted. */ 5674 } 5675 else 5676 { 5677 if (breg == treg) 5678 { 5679 /* We must add in the base register now, as in the 5680 external symbol case. */ 5681 assert (tempreg == AT); 5682 load_delay_nop (); 5683 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 5684 treg, AT, breg); 5685 tempreg = treg; 5686 /* We set breg to 0 because we have arranged to add 5687 it in in both cases. */ 5688 breg = 0; 5689 } 5690 5691 macro_build_lui (&expr1, AT); 5692 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", 5693 AT, AT, BFD_RELOC_LO16); 5694 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 5695 tempreg, tempreg, AT); 5696 used_at = 1; 5697 } 5698 relax_end (); 5699 } 5700 else if (mips_big_got && HAVE_NEWABI) 5701 { 5702 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16; 5703 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16; 5704 int add_breg_early = 0; 5705 5706 /* This is the large GOT case. If this is a reference to an 5707 external symbol, and there is no constant, we want 5708 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16) 5709 add $tempreg,$tempreg,$gp 5710 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16) 5711 or for lca or if tempreg is PIC_CALL_REG 5712 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16) 5713 add $tempreg,$tempreg,$gp 5714 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16) 5715 5716 If we have a small constant, and this is a reference to 5717 an external symbol, we want 5718 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16) 5719 add $tempreg,$tempreg,$gp 5720 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16) 5721 addi $tempreg,$tempreg,<constant> 5722 5723 If we have a large constant, and this is a reference to 5724 an external symbol, we want 5725 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16) 5726 addu $tempreg,$tempreg,$gp 5727 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16) 5728 lui $at,<hiconstant> 5729 addi $at,$at,<loconstant> 5730 add $tempreg,$tempreg,$at 5731 5732 If we have NewABI, and we know it's a local symbol, we want 5733 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE) 5734 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST) 5735 otherwise we have to resort to GOT_HI16/GOT_LO16. */ 5736 5737 relax_start (offset_expr.X_add_symbol); 5738 5739 expr1.X_add_number = offset_expr.X_add_number; 5740 offset_expr.X_add_number = 0; 5741 5742 if (expr1.X_add_number == 0 && breg == 0 5743 && (call || tempreg == PIC_CALL_REG)) 5744 { 5745 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16; 5746 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16; 5747 } 5748 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type); 5749 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 5750 tempreg, tempreg, mips_gp_register); 5751 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", 5752 tempreg, lw_reloc_type, tempreg); 5753 5754 if (expr1.X_add_number == 0) 5755 ; 5756 else if (expr1.X_add_number >= -0x8000 5757 && expr1.X_add_number < 0x8000) 5758 { 5759 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j", 5760 tempreg, tempreg, BFD_RELOC_LO16); 5761 } 5762 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000)) 5763 { 5764 int dreg; 5765 5766 /* If we are going to add in a base register, and the 5767 target register and the base register are the same, 5768 then we are using AT as a temporary register. Since 5769 we want to load the constant into AT, we add our 5770 current AT (from the global offset table) and the 5771 register into the register now, and pretend we were 5772 not using a base register. */ 5773 if (breg != treg) 5774 dreg = tempreg; 5775 else 5776 { 5777 assert (tempreg == AT); 5778 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 5779 treg, AT, breg); 5780 dreg = treg; 5781 add_breg_early = 1; 5782 } 5783 5784 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES); 5785 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT); 5786 5787 used_at = 1; 5788 } 5789 else 5790 as_bad (_("PIC code offset overflow (max 32 signed bits)")); 5791 5792 relax_switch (); 5793 offset_expr.X_add_number = expr1.X_add_number; 5794 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg, 5795 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register); 5796 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg, 5797 tempreg, BFD_RELOC_MIPS_GOT_OFST); 5798 if (add_breg_early) 5799 { 5800 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 5801 treg, tempreg, breg); 5802 breg = 0; 5803 tempreg = treg; 5804 } 5805 relax_end (); 5806 } 5807 else 5808 abort (); 5809 5810 if (breg != 0) 5811 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg); 5812 break; 5813 5814 case M_JR_S: 5815 macro_build_jrpatch (&expr1, sreg); 5816 macro_build (NULL, "jr", "s", sreg); 5817 return; /* didn't modify $at */ 5818 5819 case M_J_S: 5820 macro_build_jrpatch (&expr1, sreg); 5821 macro_build (NULL, "j", "s", sreg); 5822 return; /* didn't modify $at */ 5823 5824 case M_JALR_S: 5825 macro_build_jrpatch (&expr1, sreg); 5826 macro_build (NULL, "jalr", "s", sreg); 5827 return; /* didn't modify $at */ 5828 5829 case M_JALR_DS: 5830 macro_build_jrpatch (&expr1, sreg); 5831 macro_build (NULL, "jalr", "d,s", dreg, sreg); 5832 return; /* didn't modify $at */ 5833 5834 case M_J_A: 5835 /* The j instruction may not be used in PIC code, since it 5836 requires an absolute address. We convert it to a b 5837 instruction. */ 5838 if (mips_pic == NO_PIC) 5839 macro_build (&offset_expr, "j", "a"); 5840 else 5841 macro_build (&offset_expr, "b", "p"); 5842 break; 5843 5844 /* The jal instructions must be handled as macros because when 5845 generating PIC code they expand to multi-instruction 5846 sequences. Normally they are simple instructions. */ 5847 case M_JAL_1: 5848 dreg = RA; 5849 /* Fall through. */ 5850 case M_JAL_2: 5851 if (mips_pic == NO_PIC) 5852 { 5853 macro_build_jrpatch (&expr1, sreg); 5854 macro_build (NULL, "jalr", "d,s", dreg, sreg); 5855 } 5856 else 5857 { 5858 if (sreg != PIC_CALL_REG) 5859 as_warn (_("MIPS PIC call to register other than $25")); 5860 5861 macro_build_jrpatch (&expr1, sreg); 5862 macro_build (NULL, "jalr", "d,s", dreg, sreg); 5863 if (mips_pic == SVR4_PIC && !HAVE_NEWABI) 5864 { 5865 if (mips_cprestore_offset < 0) 5866 as_warn (_("No .cprestore pseudo-op used in PIC code")); 5867 else 5868 { 5869 if (! mips_frame_reg_valid) 5870 { 5871 as_warn (_("No .frame pseudo-op used in PIC code")); 5872 /* Quiet this warning. */ 5873 mips_frame_reg_valid = 1; 5874 } 5875 if (! mips_cprestore_valid) 5876 { 5877 as_warn (_("No .cprestore pseudo-op used in PIC code")); 5878 /* Quiet this warning. */ 5879 mips_cprestore_valid = 1; 5880 } 5881 expr1.X_add_number = mips_cprestore_offset; 5882 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN, 5883 mips_gp_register, 5884 mips_frame_reg, 5885 HAVE_64BIT_ADDRESSES); 5886 } 5887 } 5888 } 5889 5890 break; 5891 5892 case M_JAL_A: 5893 if (mips_pic == NO_PIC) 5894 macro_build (&offset_expr, "jal", "a"); 5895 else if (mips_pic == SVR4_PIC) 5896 { 5897 /* If this is a reference to an external symbol, and we are 5898 using a small GOT, we want 5899 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16) 5900 nop 5901 jalr $ra,$25 5902 nop 5903 lw $gp,cprestore($sp) 5904 The cprestore value is set using the .cprestore 5905 pseudo-op. If we are using a big GOT, we want 5906 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16) 5907 addu $25,$25,$gp 5908 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16) 5909 nop 5910 jalr $ra,$25 5911 nop 5912 lw $gp,cprestore($sp) 5913 If the symbol is not external, we want 5914 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 5915 nop 5916 addiu $25,$25,<sym> (BFD_RELOC_LO16) 5917 jalr $ra,$25 5918 nop 5919 lw $gp,cprestore($sp) 5920 5921 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16 5922 sequences above, minus nops, unless the symbol is local, 5923 which enables us to use GOT_PAGE/GOT_OFST (big got) or 5924 GOT_DISP. */ 5925 if (HAVE_NEWABI) 5926 { 5927 if (! mips_big_got) 5928 { 5929 relax_start (offset_expr.X_add_symbol); 5930 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", 5931 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16, 5932 mips_gp_register); 5933 relax_switch (); 5934 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", 5935 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP, 5936 mips_gp_register); 5937 relax_end (); 5938 } 5939 else 5940 { 5941 relax_start (offset_expr.X_add_symbol); 5942 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG, 5943 BFD_RELOC_MIPS_CALL_HI16); 5944 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG, 5945 PIC_CALL_REG, mips_gp_register); 5946 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", 5947 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16, 5948 PIC_CALL_REG); 5949 relax_switch (); 5950 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", 5951 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE, 5952 mips_gp_register); 5953 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", 5954 PIC_CALL_REG, PIC_CALL_REG, 5955 BFD_RELOC_MIPS_GOT_OFST); 5956 relax_end (); 5957 } 5958 5959 macro_build_jalr (&offset_expr); 5960 } 5961 else 5962 { 5963 relax_start (offset_expr.X_add_symbol); 5964 if (! mips_big_got) 5965 { 5966 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", 5967 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16, 5968 mips_gp_register); 5969 load_delay_nop (); 5970 relax_switch (); 5971 } 5972 else 5973 { 5974 int gpdelay; 5975 5976 gpdelay = reg_needs_delay (mips_gp_register); 5977 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG, 5978 BFD_RELOC_MIPS_CALL_HI16); 5979 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG, 5980 PIC_CALL_REG, mips_gp_register); 5981 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", 5982 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16, 5983 PIC_CALL_REG); 5984 load_delay_nop (); 5985 relax_switch (); 5986 if (gpdelay) 5987 macro_build (NULL, "nop", ""); 5988 } 5989 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", 5990 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16, 5991 mips_gp_register); 5992 load_delay_nop (); 5993 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", 5994 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16); 5995 relax_end (); 5996 macro_build_jalr (&offset_expr); 5997 5998 if (mips_cprestore_offset < 0) 5999 as_warn (_("No .cprestore pseudo-op used in PIC code")); 6000 else 6001 { 6002 if (! mips_frame_reg_valid) 6003 { 6004 as_warn (_("No .frame pseudo-op used in PIC code")); 6005 /* Quiet this warning. */ 6006 mips_frame_reg_valid = 1; 6007 } 6008 if (! mips_cprestore_valid) 6009 { 6010 as_warn (_("No .cprestore pseudo-op used in PIC code")); 6011 /* Quiet this warning. */ 6012 mips_cprestore_valid = 1; 6013 } 6014 if (mips_opts.noreorder) 6015 macro_build (NULL, "nop", ""); 6016 expr1.X_add_number = mips_cprestore_offset; 6017 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN, 6018 mips_gp_register, 6019 mips_frame_reg, 6020 HAVE_64BIT_ADDRESSES); 6021 } 6022 } 6023 } 6024 else if (mips_pic == VXWORKS_PIC) 6025 as_bad (_("Non-PIC jump used in PIC library")); 6026 else 6027 abort (); 6028 6029 break; 6030 6031 case M_LB_AB: 6032 s = "lb"; 6033 goto ld; 6034 case M_LBU_AB: 6035 s = "lbu"; 6036 goto ld; 6037 case M_LH_AB: 6038 s = "lh"; 6039 goto ld; 6040 case M_LHU_AB: 6041 s = "lhu"; 6042 goto ld; 6043 case M_LW_AB: 6044 s = "lw"; 6045 goto ld; 6046 case M_LWC0_AB: 6047 s = "lwc0"; 6048 /* Itbl support may require additional care here. */ 6049 coproc = 1; 6050 goto ld; 6051 case M_LWC1_AB: 6052 s = "lwc1"; 6053 /* Itbl support may require additional care here. */ 6054 coproc = 1; 6055 goto ld; 6056 case M_LWC2_AB: 6057 s = "lwc2"; 6058 /* Itbl support may require additional care here. */ 6059 coproc = 1; 6060 goto ld; 6061 case M_LWC3_AB: 6062 s = "lwc3"; 6063 /* Itbl support may require additional care here. */ 6064 coproc = 1; 6065 goto ld; 6066 case M_LWL_AB: 6067 s = "lwl"; 6068 lr = 1; 6069 goto ld; 6070 case M_LWR_AB: 6071 s = "lwr"; 6072 lr = 1; 6073 goto ld; 6074 case M_LDC1_AB: 6075 if (mips_opts.arch == CPU_R4650) 6076 { 6077 as_bad (_("opcode not supported on this processor")); 6078 break; 6079 } 6080 s = "ldc1"; 6081 /* Itbl support may require additional care here. */ 6082 coproc = 1; 6083 goto ld; 6084 case M_LDC2_AB: 6085 s = "ldc2"; 6086 /* Itbl support may require additional care here. */ 6087 coproc = 1; 6088 goto ld; 6089 case M_LDC3_AB: 6090 s = "ldc3"; 6091 /* Itbl support may require additional care here. */ 6092 coproc = 1; 6093 goto ld; 6094 case M_LDL_AB: 6095 s = "ldl"; 6096 lr = 1; 6097 goto ld; 6098 case M_LDR_AB: 6099 s = "ldr"; 6100 lr = 1; 6101 goto ld; 6102 case M_LL_AB: 6103 s = "ll"; 6104 goto ld; 6105 case M_LLD_AB: 6106 s = "lld"; 6107 goto ld; 6108 case M_LWU_AB: 6109 s = "lwu"; 6110 ld: 6111 if (breg == treg || coproc || lr) 6112 { 6113 tempreg = AT; 6114 used_at = 1; 6115 } 6116 else 6117 { 6118 tempreg = treg; 6119 } 6120 goto ld_st; 6121 case M_SB_AB: 6122 s = "sb"; 6123 goto st; 6124 case M_SH_AB: 6125 s = "sh"; 6126 goto st; 6127 case M_SW_AB: 6128 s = "sw"; 6129 goto st; 6130 case M_SWC0_AB: 6131 s = "swc0"; 6132 /* Itbl support may require additional care here. */ 6133 coproc = 1; 6134 goto st; 6135 case M_SWC1_AB: 6136 s = "swc1"; 6137 /* Itbl support may require additional care here. */ 6138 coproc = 1; 6139 goto st; 6140 case M_SWC2_AB: 6141 s = "swc2"; 6142 /* Itbl support may require additional care here. */ 6143 coproc = 1; 6144 goto st; 6145 case M_SWC3_AB: 6146 s = "swc3"; 6147 /* Itbl support may require additional care here. */ 6148 coproc = 1; 6149 goto st; 6150 case M_SWL_AB: 6151 s = "swl"; 6152 goto st; 6153 case M_SWR_AB: 6154 s = "swr"; 6155 goto st; 6156 case M_SC_AB: 6157 s = "sc"; 6158 goto st; 6159 case M_SCD_AB: 6160 s = "scd"; 6161 goto st; 6162 case M_SDC1_AB: 6163 if (mips_opts.arch == CPU_R4650) 6164 { 6165 as_bad (_("opcode not supported on this processor")); 6166 break; 6167 } 6168 s = "sdc1"; 6169 coproc = 1; 6170 /* Itbl support may require additional care here. */ 6171 goto st; 6172 case M_SDC2_AB: 6173 s = "sdc2"; 6174 /* Itbl support may require additional care here. */ 6175 coproc = 1; 6176 goto st; 6177 case M_SDC3_AB: 6178 s = "sdc3"; 6179 /* Itbl support may require additional care here. */ 6180 coproc = 1; 6181 goto st; 6182 case M_SDL_AB: 6183 s = "sdl"; 6184 goto st; 6185 case M_SDR_AB: 6186 s = "sdr"; 6187 st: 6188 tempreg = AT; 6189 used_at = 1; 6190 ld_st: 6191 /* Itbl support may require additional care here. */ 6192 if (mask == M_LWC1_AB 6193 || mask == M_SWC1_AB 6194 || mask == M_LDC1_AB 6195 || mask == M_SDC1_AB 6196 || mask == M_L_DAB 6197 || mask == M_S_DAB) 6198 fmt = "T,o(b)"; 6199 else if (coproc) 6200 fmt = "E,o(b)"; 6201 else 6202 fmt = "t,o(b)"; 6203 6204 if (offset_expr.X_op != O_constant 6205 && offset_expr.X_op != O_symbol) 6206 { 6207 as_bad (_("expression too complex")); 6208 offset_expr.X_op = O_constant; 6209 } 6210 6211 if (HAVE_32BIT_ADDRESSES 6212 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number)) 6213 { 6214 char value [32]; 6215 6216 sprintf_vma (value, offset_expr.X_add_number); 6217 as_bad (_("Number (0x%s) larger than 32 bits"), value); 6218 } 6219 6220 /* A constant expression in PIC code can be handled just as it 6221 is in non PIC code. */ 6222 if (offset_expr.X_op == O_constant) 6223 { 6224 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000) 6225 & ~(bfd_vma) 0xffff); 6226 normalize_address_expr (&expr1); 6227 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES); 6228 if (breg != 0) 6229 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 6230 tempreg, tempreg, breg); 6231 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg); 6232 } 6233 else if (mips_pic == NO_PIC) 6234 { 6235 /* If this is a reference to a GP relative symbol, and there 6236 is no base register, we want 6237 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16) 6238 Otherwise, if there is no base register, we want 6239 lui $tempreg,<sym> (BFD_RELOC_HI16_S) 6240 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16) 6241 If we have a constant, we need two instructions anyhow, 6242 so we always use the latter form. 6243 6244 If we have a base register, and this is a reference to a 6245 GP relative symbol, we want 6246 addu $tempreg,$breg,$gp 6247 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16) 6248 Otherwise we want 6249 lui $tempreg,<sym> (BFD_RELOC_HI16_S) 6250 addu $tempreg,$tempreg,$breg 6251 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16) 6252 With a constant we always use the latter case. 6253 6254 With 64bit address space and no base register and $at usable, 6255 we want 6256 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST) 6257 lui $at,<sym> (BFD_RELOC_HI16_S) 6258 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER) 6259 dsll32 $tempreg,0 6260 daddu $tempreg,$at 6261 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16) 6262 If we have a base register, we want 6263 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST) 6264 lui $at,<sym> (BFD_RELOC_HI16_S) 6265 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER) 6266 daddu $at,$breg 6267 dsll32 $tempreg,0 6268 daddu $tempreg,$at 6269 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16) 6270 6271 Without $at we can't generate the optimal path for superscalar 6272 processors here since this would require two temporary registers. 6273 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST) 6274 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER) 6275 dsll $tempreg,16 6276 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S) 6277 dsll $tempreg,16 6278 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16) 6279 If we have a base register, we want 6280 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST) 6281 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER) 6282 dsll $tempreg,16 6283 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S) 6284 dsll $tempreg,16 6285 daddu $tempreg,$tempreg,$breg 6286 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16) 6287 6288 For GP relative symbols in 64bit address space we can use 6289 the same sequence as in 32bit address space. */ 6290 if (HAVE_64BIT_SYMBOLS) 6291 { 6292 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET 6293 && !nopic_need_relax (offset_expr.X_add_symbol, 1)) 6294 { 6295 relax_start (offset_expr.X_add_symbol); 6296 if (breg == 0) 6297 { 6298 macro_build (&offset_expr, s, fmt, treg, 6299 BFD_RELOC_GPREL16, mips_gp_register); 6300 } 6301 else 6302 { 6303 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 6304 tempreg, breg, mips_gp_register); 6305 macro_build (&offset_expr, s, fmt, treg, 6306 BFD_RELOC_GPREL16, tempreg); 6307 } 6308 relax_switch (); 6309 } 6310 6311 if (used_at == 0 && !mips_opts.noat) 6312 { 6313 macro_build (&offset_expr, "lui", "t,u", tempreg, 6314 BFD_RELOC_MIPS_HIGHEST); 6315 macro_build (&offset_expr, "lui", "t,u", AT, 6316 BFD_RELOC_HI16_S); 6317 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg, 6318 tempreg, BFD_RELOC_MIPS_HIGHER); 6319 if (breg != 0) 6320 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg); 6321 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0); 6322 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT); 6323 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, 6324 tempreg); 6325 used_at = 1; 6326 } 6327 else 6328 { 6329 macro_build (&offset_expr, "lui", "t,u", tempreg, 6330 BFD_RELOC_MIPS_HIGHEST); 6331 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg, 6332 tempreg, BFD_RELOC_MIPS_HIGHER); 6333 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16); 6334 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg, 6335 tempreg, BFD_RELOC_HI16_S); 6336 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16); 6337 if (breg != 0) 6338 macro_build (NULL, "daddu", "d,v,t", 6339 tempreg, tempreg, breg); 6340 macro_build (&offset_expr, s, fmt, treg, 6341 BFD_RELOC_LO16, tempreg); 6342 } 6343 6344 if (mips_relax.sequence) 6345 relax_end (); 6346 break; 6347 } 6348 6349 if (breg == 0) 6350 { 6351 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET 6352 && !nopic_need_relax (offset_expr.X_add_symbol, 1)) 6353 { 6354 relax_start (offset_expr.X_add_symbol); 6355 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16, 6356 mips_gp_register); 6357 relax_switch (); 6358 } 6359 macro_build_lui (&offset_expr, tempreg); 6360 macro_build (&offset_expr, s, fmt, treg, 6361 BFD_RELOC_LO16, tempreg); 6362 if (mips_relax.sequence) 6363 relax_end (); 6364 } 6365 else 6366 { 6367 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET 6368 && !nopic_need_relax (offset_expr.X_add_symbol, 1)) 6369 { 6370 relax_start (offset_expr.X_add_symbol); 6371 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 6372 tempreg, breg, mips_gp_register); 6373 macro_build (&offset_expr, s, fmt, treg, 6374 BFD_RELOC_GPREL16, tempreg); 6375 relax_switch (); 6376 } 6377 macro_build_lui (&offset_expr, tempreg); 6378 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 6379 tempreg, tempreg, breg); 6380 macro_build (&offset_expr, s, fmt, treg, 6381 BFD_RELOC_LO16, tempreg); 6382 if (mips_relax.sequence) 6383 relax_end (); 6384 } 6385 } 6386 else if (!mips_big_got) 6387 { 6388 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16; 6389 6390 /* If this is a reference to an external symbol, we want 6391 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 6392 nop 6393 <op> $treg,0($tempreg) 6394 Otherwise we want 6395 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 6396 nop 6397 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16) 6398 <op> $treg,0($tempreg) 6399 6400 For NewABI, we want 6401 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE) 6402 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) 6403 6404 If there is a base register, we add it to $tempreg before 6405 the <op>. If there is a constant, we stick it in the 6406 <op> instruction. We don't handle constants larger than 6407 16 bits, because we have no way to load the upper 16 bits 6408 (actually, we could handle them for the subset of cases 6409 in which we are not using $at). */ 6410 assert (offset_expr.X_op == O_symbol); 6411 if (HAVE_NEWABI) 6412 { 6413 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg, 6414 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register); 6415 if (breg != 0) 6416 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 6417 tempreg, tempreg, breg); 6418 macro_build (&offset_expr, s, fmt, treg, 6419 BFD_RELOC_MIPS_GOT_OFST, tempreg); 6420 break; 6421 } 6422 expr1.X_add_number = offset_expr.X_add_number; 6423 offset_expr.X_add_number = 0; 6424 if (expr1.X_add_number < -0x8000 6425 || expr1.X_add_number >= 0x8000) 6426 as_bad (_("PIC code offset overflow (max 16 signed bits)")); 6427 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg, 6428 lw_reloc_type, mips_gp_register); 6429 load_delay_nop (); 6430 relax_start (offset_expr.X_add_symbol); 6431 relax_switch (); 6432 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg, 6433 tempreg, BFD_RELOC_LO16); 6434 relax_end (); 6435 if (breg != 0) 6436 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 6437 tempreg, tempreg, breg); 6438 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg); 6439 } 6440 else if (mips_big_got && !HAVE_NEWABI) 6441 { 6442 int gpdelay; 6443 6444 /* If this is a reference to an external symbol, we want 6445 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16) 6446 addu $tempreg,$tempreg,$gp 6447 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16) 6448 <op> $treg,0($tempreg) 6449 Otherwise we want 6450 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 6451 nop 6452 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16) 6453 <op> $treg,0($tempreg) 6454 If there is a base register, we add it to $tempreg before 6455 the <op>. If there is a constant, we stick it in the 6456 <op> instruction. We don't handle constants larger than 6457 16 bits, because we have no way to load the upper 16 bits 6458 (actually, we could handle them for the subset of cases 6459 in which we are not using $at). */ 6460 assert (offset_expr.X_op == O_symbol); 6461 expr1.X_add_number = offset_expr.X_add_number; 6462 offset_expr.X_add_number = 0; 6463 if (expr1.X_add_number < -0x8000 6464 || expr1.X_add_number >= 0x8000) 6465 as_bad (_("PIC code offset overflow (max 16 signed bits)")); 6466 gpdelay = reg_needs_delay (mips_gp_register); 6467 relax_start (offset_expr.X_add_symbol); 6468 macro_build (&offset_expr, "lui", "t,u", tempreg, 6469 BFD_RELOC_MIPS_GOT_HI16); 6470 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg, 6471 mips_gp_register); 6472 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg, 6473 BFD_RELOC_MIPS_GOT_LO16, tempreg); 6474 relax_switch (); 6475 if (gpdelay) 6476 macro_build (NULL, "nop", ""); 6477 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg, 6478 BFD_RELOC_MIPS_GOT16, mips_gp_register); 6479 load_delay_nop (); 6480 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg, 6481 tempreg, BFD_RELOC_LO16); 6482 relax_end (); 6483 6484 if (breg != 0) 6485 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 6486 tempreg, tempreg, breg); 6487 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg); 6488 } 6489 else if (mips_big_got && HAVE_NEWABI) 6490 { 6491 /* If this is a reference to an external symbol, we want 6492 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16) 6493 add $tempreg,$tempreg,$gp 6494 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16) 6495 <op> $treg,<ofst>($tempreg) 6496 Otherwise, for local symbols, we want: 6497 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE) 6498 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */ 6499 assert (offset_expr.X_op == O_symbol); 6500 expr1.X_add_number = offset_expr.X_add_number; 6501 offset_expr.X_add_number = 0; 6502 if (expr1.X_add_number < -0x8000 6503 || expr1.X_add_number >= 0x8000) 6504 as_bad (_("PIC code offset overflow (max 16 signed bits)")); 6505 relax_start (offset_expr.X_add_symbol); 6506 macro_build (&offset_expr, "lui", "t,u", tempreg, 6507 BFD_RELOC_MIPS_GOT_HI16); 6508 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg, 6509 mips_gp_register); 6510 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg, 6511 BFD_RELOC_MIPS_GOT_LO16, tempreg); 6512 if (breg != 0) 6513 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 6514 tempreg, tempreg, breg); 6515 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg); 6516 6517 relax_switch (); 6518 offset_expr.X_add_number = expr1.X_add_number; 6519 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg, 6520 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register); 6521 if (breg != 0) 6522 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 6523 tempreg, tempreg, breg); 6524 macro_build (&offset_expr, s, fmt, treg, 6525 BFD_RELOC_MIPS_GOT_OFST, tempreg); 6526 relax_end (); 6527 } 6528 else 6529 abort (); 6530 6531 break; 6532 6533 case M_LI: 6534 case M_LI_S: 6535 load_register (treg, &imm_expr, 0); 6536 break; 6537 6538 case M_DLI: 6539 load_register (treg, &imm_expr, 1); 6540 break; 6541 6542 case M_LI_SS: 6543 if (imm_expr.X_op == O_constant) 6544 { 6545 used_at = 1; 6546 load_register (AT, &imm_expr, 0); 6547 macro_build (NULL, "mtc1", "t,G", AT, treg); 6548 break; 6549 } 6550 else 6551 { 6552 assert (offset_expr.X_op == O_symbol 6553 && strcmp (segment_name (S_GET_SEGMENT 6554 (offset_expr.X_add_symbol)), 6555 ".lit4") == 0 6556 && offset_expr.X_add_number == 0); 6557 macro_build (&offset_expr, "lwc1", "T,o(b)", treg, 6558 BFD_RELOC_MIPS_LITERAL, mips_gp_register); 6559 break; 6560 } 6561 6562 case M_LI_D: 6563 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits 6564 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high 6565 order 32 bits of the value and the low order 32 bits are either 6566 zero or in OFFSET_EXPR. */ 6567 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big) 6568 { 6569 if (HAVE_64BIT_GPRS) 6570 load_register (treg, &imm_expr, 1); 6571 else 6572 { 6573 int hreg, lreg; 6574 6575 if (target_big_endian) 6576 { 6577 hreg = treg; 6578 lreg = treg + 1; 6579 } 6580 else 6581 { 6582 hreg = treg + 1; 6583 lreg = treg; 6584 } 6585 6586 if (hreg <= 31) 6587 load_register (hreg, &imm_expr, 0); 6588 if (lreg <= 31) 6589 { 6590 if (offset_expr.X_op == O_absent) 6591 move_register (lreg, 0); 6592 else 6593 { 6594 assert (offset_expr.X_op == O_constant); 6595 load_register (lreg, &offset_expr, 0); 6596 } 6597 } 6598 } 6599 break; 6600 } 6601 6602 /* We know that sym is in the .rdata section. First we get the 6603 upper 16 bits of the address. */ 6604 if (mips_pic == NO_PIC) 6605 { 6606 macro_build_lui (&offset_expr, AT); 6607 used_at = 1; 6608 } 6609 else 6610 { 6611 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT, 6612 BFD_RELOC_MIPS_GOT16, mips_gp_register); 6613 used_at = 1; 6614 } 6615 6616 /* Now we load the register(s). */ 6617 if (HAVE_64BIT_GPRS) 6618 { 6619 used_at = 1; 6620 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT); 6621 } 6622 else 6623 { 6624 used_at = 1; 6625 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT); 6626 if (treg != RA) 6627 { 6628 /* FIXME: How in the world do we deal with the possible 6629 overflow here? */ 6630 offset_expr.X_add_number += 4; 6631 macro_build (&offset_expr, "lw", "t,o(b)", 6632 treg + 1, BFD_RELOC_LO16, AT); 6633 } 6634 } 6635 break; 6636 6637 case M_LI_DD: 6638 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits 6639 wide, IMM_EXPR is the entire value and the GPRs are known to be 64 6640 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of 6641 the value and the low order 32 bits are either zero or in 6642 OFFSET_EXPR. */ 6643 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big) 6644 { 6645 used_at = 1; 6646 load_register (AT, &imm_expr, HAVE_64BIT_FPRS); 6647 if (HAVE_64BIT_FPRS) 6648 { 6649 assert (HAVE_64BIT_GPRS); 6650 macro_build (NULL, "dmtc1", "t,S", AT, treg); 6651 } 6652 else 6653 { 6654 macro_build (NULL, "mtc1", "t,G", AT, treg + 1); 6655 if (offset_expr.X_op == O_absent) 6656 macro_build (NULL, "mtc1", "t,G", 0, treg); 6657 else 6658 { 6659 assert (offset_expr.X_op == O_constant); 6660 load_register (AT, &offset_expr, 0); 6661 macro_build (NULL, "mtc1", "t,G", AT, treg); 6662 } 6663 } 6664 break; 6665 } 6666 6667 assert (offset_expr.X_op == O_symbol 6668 && offset_expr.X_add_number == 0); 6669 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol)); 6670 if (strcmp (s, ".lit8") == 0) 6671 { 6672 if (mips_opts.isa != ISA_MIPS1) 6673 { 6674 macro_build (&offset_expr, "ldc1", "T,o(b)", treg, 6675 BFD_RELOC_MIPS_LITERAL, mips_gp_register); 6676 break; 6677 } 6678 breg = mips_gp_register; 6679 r = BFD_RELOC_MIPS_LITERAL; 6680 goto dob; 6681 } 6682 else 6683 { 6684 assert (strcmp (s, RDATA_SECTION_NAME) == 0); 6685 used_at = 1; 6686 if (mips_pic != NO_PIC) 6687 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT, 6688 BFD_RELOC_MIPS_GOT16, mips_gp_register); 6689 else 6690 { 6691 /* FIXME: This won't work for a 64 bit address. */ 6692 macro_build_lui (&offset_expr, AT); 6693 } 6694 6695 if (mips_opts.isa != ISA_MIPS1) 6696 { 6697 macro_build (&offset_expr, "ldc1", "T,o(b)", 6698 treg, BFD_RELOC_LO16, AT); 6699 break; 6700 } 6701 breg = AT; 6702 r = BFD_RELOC_LO16; 6703 goto dob; 6704 } 6705 6706 case M_L_DOB: 6707 if (mips_opts.arch == CPU_R4650) 6708 { 6709 as_bad (_("opcode not supported on this processor")); 6710 break; 6711 } 6712 /* Even on a big endian machine $fn comes before $fn+1. We have 6713 to adjust when loading from memory. */ 6714 r = BFD_RELOC_LO16; 6715 dob: 6716 assert (mips_opts.isa == ISA_MIPS1); 6717 macro_build (&offset_expr, "lwc1", "T,o(b)", 6718 target_big_endian ? treg + 1 : treg, r, breg); 6719 /* FIXME: A possible overflow which I don't know how to deal 6720 with. */ 6721 offset_expr.X_add_number += 4; 6722 macro_build (&offset_expr, "lwc1", "T,o(b)", 6723 target_big_endian ? treg : treg + 1, r, breg); 6724 break; 6725 6726 case M_L_DAB: 6727 /* 6728 * The MIPS assembler seems to check for X_add_number not 6729 * being double aligned and generating: 6730 * lui at,%hi(foo+1) 6731 * addu at,at,v1 6732 * addiu at,at,%lo(foo+1) 6733 * lwc1 f2,0(at) 6734 * lwc1 f3,4(at) 6735 * But, the resulting address is the same after relocation so why 6736 * generate the extra instruction? 6737 */ 6738 if (mips_opts.arch == CPU_R4650) 6739 { 6740 as_bad (_("opcode not supported on this processor")); 6741 break; 6742 } 6743 /* Itbl support may require additional care here. */ 6744 coproc = 1; 6745 if (mips_opts.isa != ISA_MIPS1) 6746 { 6747 s = "ldc1"; 6748 goto ld; 6749 } 6750 6751 s = "lwc1"; 6752 fmt = "T,o(b)"; 6753 goto ldd_std; 6754 6755 case M_S_DAB: 6756 if (mips_opts.arch == CPU_R4650) 6757 { 6758 as_bad (_("opcode not supported on this processor")); 6759 break; 6760 } 6761 6762 if (mips_opts.isa != ISA_MIPS1) 6763 { 6764 s = "sdc1"; 6765 goto st; 6766 } 6767 6768 s = "swc1"; 6769 fmt = "T,o(b)"; 6770 /* Itbl support may require additional care here. */ 6771 coproc = 1; 6772 goto ldd_std; 6773 6774 case M_LD_AB: 6775 if (HAVE_64BIT_GPRS) 6776 { 6777 s = "ld"; 6778 goto ld; 6779 } 6780 6781 s = "lw"; 6782 fmt = "t,o(b)"; 6783 goto ldd_std; 6784 6785 case M_SD_AB: 6786 if (HAVE_64BIT_GPRS) 6787 { 6788 s = "sd"; 6789 goto st; 6790 } 6791 6792 s = "sw"; 6793 fmt = "t,o(b)"; 6794 6795 ldd_std: 6796 if (offset_expr.X_op != O_symbol 6797 && offset_expr.X_op != O_constant) 6798 { 6799 as_bad (_("expression too complex")); 6800 offset_expr.X_op = O_constant; 6801 } 6802 6803 if (HAVE_32BIT_ADDRESSES 6804 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number)) 6805 { 6806 char value [32]; 6807 6808 sprintf_vma (value, offset_expr.X_add_number); 6809 as_bad (_("Number (0x%s) larger than 32 bits"), value); 6810 } 6811 6812 /* Even on a big endian machine $fn comes before $fn+1. We have 6813 to adjust when loading from memory. We set coproc if we must 6814 load $fn+1 first. */ 6815 /* Itbl support may require additional care here. */ 6816 if (! target_big_endian) 6817 coproc = 0; 6818 6819 if (mips_pic == NO_PIC 6820 || offset_expr.X_op == O_constant) 6821 { 6822 /* If this is a reference to a GP relative symbol, we want 6823 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16) 6824 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16) 6825 If we have a base register, we use this 6826 addu $at,$breg,$gp 6827 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16) 6828 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16) 6829 If this is not a GP relative symbol, we want 6830 lui $at,<sym> (BFD_RELOC_HI16_S) 6831 <op> $treg,<sym>($at) (BFD_RELOC_LO16) 6832 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16) 6833 If there is a base register, we add it to $at after the 6834 lui instruction. If there is a constant, we always use 6835 the last case. */ 6836 if (offset_expr.X_op == O_symbol 6837 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET 6838 && !nopic_need_relax (offset_expr.X_add_symbol, 1)) 6839 { 6840 relax_start (offset_expr.X_add_symbol); 6841 if (breg == 0) 6842 { 6843 tempreg = mips_gp_register; 6844 } 6845 else 6846 { 6847 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 6848 AT, breg, mips_gp_register); 6849 tempreg = AT; 6850 used_at = 1; 6851 } 6852 6853 /* Itbl support may require additional care here. */ 6854 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg, 6855 BFD_RELOC_GPREL16, tempreg); 6856 offset_expr.X_add_number += 4; 6857 6858 /* Set mips_optimize to 2 to avoid inserting an 6859 undesired nop. */ 6860 hold_mips_optimize = mips_optimize; 6861 mips_optimize = 2; 6862 /* Itbl support may require additional care here. */ 6863 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1, 6864 BFD_RELOC_GPREL16, tempreg); 6865 mips_optimize = hold_mips_optimize; 6866 6867 relax_switch (); 6868 6869 /* We just generated two relocs. When tc_gen_reloc 6870 handles this case, it will skip the first reloc and 6871 handle the second. The second reloc already has an 6872 extra addend of 4, which we added above. We must 6873 subtract it out, and then subtract another 4 to make 6874 the first reloc come out right. The second reloc 6875 will come out right because we are going to add 4 to 6876 offset_expr when we build its instruction below. 6877 6878 If we have a symbol, then we don't want to include 6879 the offset, because it will wind up being included 6880 when we generate the reloc. */ 6881 6882 if (offset_expr.X_op == O_constant) 6883 offset_expr.X_add_number -= 8; 6884 else 6885 { 6886 offset_expr.X_add_number = -4; 6887 offset_expr.X_op = O_constant; 6888 } 6889 } 6890 used_at = 1; 6891 macro_build_lui (&offset_expr, AT); 6892 if (breg != 0) 6893 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT); 6894 /* Itbl support may require additional care here. */ 6895 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg, 6896 BFD_RELOC_LO16, AT); 6897 /* FIXME: How do we handle overflow here? */ 6898 offset_expr.X_add_number += 4; 6899 /* Itbl support may require additional care here. */ 6900 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1, 6901 BFD_RELOC_LO16, AT); 6902 if (mips_relax.sequence) 6903 relax_end (); 6904 } 6905 else if (!mips_big_got) 6906 { 6907 /* If this is a reference to an external symbol, we want 6908 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 6909 nop 6910 <op> $treg,0($at) 6911 <op> $treg+1,4($at) 6912 Otherwise we want 6913 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 6914 nop 6915 <op> $treg,<sym>($at) (BFD_RELOC_LO16) 6916 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16) 6917 If there is a base register we add it to $at before the 6918 lwc1 instructions. If there is a constant we include it 6919 in the lwc1 instructions. */ 6920 used_at = 1; 6921 expr1.X_add_number = offset_expr.X_add_number; 6922 if (expr1.X_add_number < -0x8000 6923 || expr1.X_add_number >= 0x8000 - 4) 6924 as_bad (_("PIC code offset overflow (max 16 signed bits)")); 6925 load_got_offset (AT, &offset_expr); 6926 load_delay_nop (); 6927 if (breg != 0) 6928 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT); 6929 6930 /* Set mips_optimize to 2 to avoid inserting an undesired 6931 nop. */ 6932 hold_mips_optimize = mips_optimize; 6933 mips_optimize = 2; 6934 6935 /* Itbl support may require additional care here. */ 6936 relax_start (offset_expr.X_add_symbol); 6937 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg, 6938 BFD_RELOC_LO16, AT); 6939 expr1.X_add_number += 4; 6940 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1, 6941 BFD_RELOC_LO16, AT); 6942 relax_switch (); 6943 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg, 6944 BFD_RELOC_LO16, AT); 6945 offset_expr.X_add_number += 4; 6946 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1, 6947 BFD_RELOC_LO16, AT); 6948 relax_end (); 6949 6950 mips_optimize = hold_mips_optimize; 6951 } 6952 else if (mips_big_got) 6953 { 6954 int gpdelay; 6955 6956 /* If this is a reference to an external symbol, we want 6957 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16) 6958 addu $at,$at,$gp 6959 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16) 6960 nop 6961 <op> $treg,0($at) 6962 <op> $treg+1,4($at) 6963 Otherwise we want 6964 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16) 6965 nop 6966 <op> $treg,<sym>($at) (BFD_RELOC_LO16) 6967 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16) 6968 If there is a base register we add it to $at before the 6969 lwc1 instructions. If there is a constant we include it 6970 in the lwc1 instructions. */ 6971 used_at = 1; 6972 expr1.X_add_number = offset_expr.X_add_number; 6973 offset_expr.X_add_number = 0; 6974 if (expr1.X_add_number < -0x8000 6975 || expr1.X_add_number >= 0x8000 - 4) 6976 as_bad (_("PIC code offset overflow (max 16 signed bits)")); 6977 gpdelay = reg_needs_delay (mips_gp_register); 6978 relax_start (offset_expr.X_add_symbol); 6979 macro_build (&offset_expr, "lui", "t,u", 6980 AT, BFD_RELOC_MIPS_GOT_HI16); 6981 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", 6982 AT, AT, mips_gp_register); 6983 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", 6984 AT, BFD_RELOC_MIPS_GOT_LO16, AT); 6985 load_delay_nop (); 6986 if (breg != 0) 6987 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT); 6988 /* Itbl support may require additional care here. */ 6989 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg, 6990 BFD_RELOC_LO16, AT); 6991 expr1.X_add_number += 4; 6992 6993 /* Set mips_optimize to 2 to avoid inserting an undesired 6994 nop. */ 6995 hold_mips_optimize = mips_optimize; 6996 mips_optimize = 2; 6997 /* Itbl support may require additional care here. */ 6998 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1, 6999 BFD_RELOC_LO16, AT); 7000 mips_optimize = hold_mips_optimize; 7001 expr1.X_add_number -= 4; 7002 7003 relax_switch (); 7004 offset_expr.X_add_number = expr1.X_add_number; 7005 if (gpdelay) 7006 macro_build (NULL, "nop", ""); 7007 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT, 7008 BFD_RELOC_MIPS_GOT16, mips_gp_register); 7009 load_delay_nop (); 7010 if (breg != 0) 7011 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT); 7012 /* Itbl support may require additional care here. */ 7013 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg, 7014 BFD_RELOC_LO16, AT); 7015 offset_expr.X_add_number += 4; 7016 7017 /* Set mips_optimize to 2 to avoid inserting an undesired 7018 nop. */ 7019 hold_mips_optimize = mips_optimize; 7020 mips_optimize = 2; 7021 /* Itbl support may require additional care here. */ 7022 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1, 7023 BFD_RELOC_LO16, AT); 7024 mips_optimize = hold_mips_optimize; 7025 relax_end (); 7026 } 7027 else 7028 abort (); 7029 7030 break; 7031 7032 case M_LD_OB: 7033 s = "lw"; 7034 goto sd_ob; 7035 case M_SD_OB: 7036 s = "sw"; 7037 sd_ob: 7038 assert (HAVE_32BIT_ADDRESSES); 7039 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg); 7040 offset_expr.X_add_number += 4; 7041 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg); 7042 break; 7043 7044 /* New code added to support COPZ instructions. 7045 This code builds table entries out of the macros in mip_opcodes. 7046 R4000 uses interlocks to handle coproc delays. 7047 Other chips (like the R3000) require nops to be inserted for delays. 7048 7049 FIXME: Currently, we require that the user handle delays. 7050 In order to fill delay slots for non-interlocked chips, 7051 we must have a way to specify delays based on the coprocessor. 7052 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc. 7053 What are the side-effects of the cop instruction? 7054 What cache support might we have and what are its effects? 7055 Both coprocessor & memory require delays. how long??? 7056 What registers are read/set/modified? 7057 7058 If an itbl is provided to interpret cop instructions, 7059 this knowledge can be encoded in the itbl spec. */ 7060 7061 case M_COP0: 7062 s = "c0"; 7063 goto copz; 7064 case M_COP1: 7065 s = "c1"; 7066 goto copz; 7067 case M_COP2: 7068 s = "c2"; 7069 goto copz; 7070 case M_COP3: 7071 s = "c3"; 7072 copz: 7073 /* For now we just do C (same as Cz). The parameter will be 7074 stored in insn_opcode by mips_ip. */ 7075 macro_build (NULL, s, "C", ip->insn_opcode); 7076 break; 7077 7078 case M_MOVE: 7079 move_register (dreg, sreg); 7080 break; 7081 7082 #ifdef LOSING_COMPILER 7083 default: 7084 /* Try and see if this is a new itbl instruction. 7085 This code builds table entries out of the macros in mip_opcodes. 7086 FIXME: For now we just assemble the expression and pass it's 7087 value along as a 32-bit immediate. 7088 We may want to have the assembler assemble this value, 7089 so that we gain the assembler's knowledge of delay slots, 7090 symbols, etc. 7091 Would it be more efficient to use mask (id) here? */ 7092 if (itbl_have_entries 7093 && (immed_expr = itbl_assemble (ip->insn_mo->name, ""))) 7094 { 7095 s = ip->insn_mo->name; 7096 s2 = "cop3"; 7097 coproc = ITBL_DECODE_PNUM (immed_expr);; 7098 macro_build (&immed_expr, s, "C"); 7099 break; 7100 } 7101 macro2 (ip); 7102 break; 7103 } 7104 if (mips_opts.noat && used_at) 7105 as_bad (_("Macro used $at after \".set noat\"")); 7106 } 7107 7108 static void 7109 macro2 (struct mips_cl_insn *ip) 7110 { 7111 register int treg, sreg, dreg, breg; 7112 int tempreg; 7113 int mask; 7114 int used_at; 7115 expressionS expr1; 7116 const char *s; 7117 const char *s2; 7118 const char *fmt; 7119 int likely = 0; 7120 int dbl = 0; 7121 int coproc = 0; 7122 int lr = 0; 7123 int imm = 0; 7124 int off; 7125 offsetT maxnum; 7126 bfd_reloc_code_real_type r; 7127 7128 treg = (ip->insn_opcode >> 16) & 0x1f; 7129 dreg = (ip->insn_opcode >> 11) & 0x1f; 7130 sreg = breg = (ip->insn_opcode >> 21) & 0x1f; 7131 mask = ip->insn_mo->mask; 7132 7133 expr1.X_op = O_constant; 7134 expr1.X_op_symbol = NULL; 7135 expr1.X_add_symbol = NULL; 7136 expr1.X_add_number = 1; 7137 7138 switch (mask) 7139 { 7140 #endif /* LOSING_COMPILER */ 7141 7142 case M_DMUL: 7143 dbl = 1; 7144 case M_MUL: 7145 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg); 7146 macro_build (NULL, "mflo", "d", dreg); 7147 break; 7148 7149 case M_DMUL_I: 7150 dbl = 1; 7151 case M_MUL_I: 7152 /* The MIPS assembler some times generates shifts and adds. I'm 7153 not trying to be that fancy. GCC should do this for us 7154 anyway. */ 7155 used_at = 1; 7156 load_register (AT, &imm_expr, dbl); 7157 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT); 7158 macro_build (NULL, "mflo", "d", dreg); 7159 break; 7160 7161 case M_DMULO_I: 7162 dbl = 1; 7163 case M_MULO_I: 7164 imm = 1; 7165 goto do_mulo; 7166 7167 case M_DMULO: 7168 dbl = 1; 7169 case M_MULO: 7170 do_mulo: 7171 start_noreorder (); 7172 used_at = 1; 7173 if (imm) 7174 load_register (AT, &imm_expr, dbl); 7175 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg); 7176 macro_build (NULL, "mflo", "d", dreg); 7177 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA); 7178 macro_build (NULL, "mfhi", "d", AT); 7179 if (mips_trap) 7180 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6); 7181 else 7182 { 7183 expr1.X_add_number = 8; 7184 macro_build (&expr1, "beq", "s,t,p", dreg, AT); 7185 macro_build (NULL, "nop", "", 0); 7186 macro_build (NULL, "break", "c", 6); 7187 } 7188 end_noreorder (); 7189 macro_build (NULL, "mflo", "d", dreg); 7190 break; 7191 7192 case M_DMULOU_I: 7193 dbl = 1; 7194 case M_MULOU_I: 7195 imm = 1; 7196 goto do_mulou; 7197 7198 case M_DMULOU: 7199 dbl = 1; 7200 case M_MULOU: 7201 do_mulou: 7202 start_noreorder (); 7203 used_at = 1; 7204 if (imm) 7205 load_register (AT, &imm_expr, dbl); 7206 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", 7207 sreg, imm ? AT : treg); 7208 macro_build (NULL, "mfhi", "d", AT); 7209 macro_build (NULL, "mflo", "d", dreg); 7210 if (mips_trap) 7211 macro_build (NULL, "tne", "s,t,q", AT, 0, 6); 7212 else 7213 { 7214 expr1.X_add_number = 8; 7215 macro_build (&expr1, "beq", "s,t,p", AT, 0); 7216 macro_build (NULL, "nop", "", 0); 7217 macro_build (NULL, "break", "c", 6); 7218 } 7219 end_noreorder (); 7220 break; 7221 7222 case M_DROL: 7223 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch)) 7224 { 7225 if (dreg == sreg) 7226 { 7227 tempreg = AT; 7228 used_at = 1; 7229 } 7230 else 7231 { 7232 tempreg = dreg; 7233 } 7234 macro_build (NULL, "dnegu", "d,w", tempreg, treg); 7235 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg); 7236 break; 7237 } 7238 used_at = 1; 7239 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg); 7240 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT); 7241 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg); 7242 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT); 7243 break; 7244 7245 case M_ROL: 7246 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch)) 7247 { 7248 if (dreg == sreg) 7249 { 7250 tempreg = AT; 7251 used_at = 1; 7252 } 7253 else 7254 { 7255 tempreg = dreg; 7256 } 7257 macro_build (NULL, "negu", "d,w", tempreg, treg); 7258 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg); 7259 break; 7260 } 7261 used_at = 1; 7262 macro_build (NULL, "subu", "d,v,t", AT, 0, treg); 7263 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT); 7264 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg); 7265 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT); 7266 break; 7267 7268 case M_DROL_I: 7269 { 7270 unsigned int rot; 7271 char *l, *r; 7272 7273 if (imm_expr.X_op != O_constant) 7274 as_bad (_("Improper rotate count")); 7275 rot = imm_expr.X_add_number & 0x3f; 7276 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch)) 7277 { 7278 rot = (64 - rot) & 0x3f; 7279 if (rot >= 32) 7280 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32); 7281 else 7282 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot); 7283 break; 7284 } 7285 if (rot == 0) 7286 { 7287 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0); 7288 break; 7289 } 7290 l = (rot < 0x20) ? "dsll" : "dsll32"; 7291 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32"; 7292 rot &= 0x1f; 7293 used_at = 1; 7294 macro_build (NULL, l, "d,w,<", AT, sreg, rot); 7295 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f); 7296 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT); 7297 } 7298 break; 7299 7300 case M_ROL_I: 7301 { 7302 unsigned int rot; 7303 7304 if (imm_expr.X_op != O_constant) 7305 as_bad (_("Improper rotate count")); 7306 rot = imm_expr.X_add_number & 0x1f; 7307 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch)) 7308 { 7309 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f); 7310 break; 7311 } 7312 if (rot == 0) 7313 { 7314 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0); 7315 break; 7316 } 7317 used_at = 1; 7318 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot); 7319 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f); 7320 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT); 7321 } 7322 break; 7323 7324 case M_DROR: 7325 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch)) 7326 { 7327 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg); 7328 break; 7329 } 7330 used_at = 1; 7331 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg); 7332 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT); 7333 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg); 7334 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT); 7335 break; 7336 7337 case M_ROR: 7338 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch)) 7339 { 7340 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg); 7341 break; 7342 } 7343 used_at = 1; 7344 macro_build (NULL, "subu", "d,v,t", AT, 0, treg); 7345 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT); 7346 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg); 7347 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT); 7348 break; 7349 7350 case M_DROR_I: 7351 { 7352 unsigned int rot; 7353 char *l, *r; 7354 7355 if (imm_expr.X_op != O_constant) 7356 as_bad (_("Improper rotate count")); 7357 rot = imm_expr.X_add_number & 0x3f; 7358 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch)) 7359 { 7360 if (rot >= 32) 7361 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32); 7362 else 7363 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot); 7364 break; 7365 } 7366 if (rot == 0) 7367 { 7368 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0); 7369 break; 7370 } 7371 r = (rot < 0x20) ? "dsrl" : "dsrl32"; 7372 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32"; 7373 rot &= 0x1f; 7374 used_at = 1; 7375 macro_build (NULL, r, "d,w,<", AT, sreg, rot); 7376 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f); 7377 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT); 7378 } 7379 break; 7380 7381 case M_ROR_I: 7382 { 7383 unsigned int rot; 7384 7385 if (imm_expr.X_op != O_constant) 7386 as_bad (_("Improper rotate count")); 7387 rot = imm_expr.X_add_number & 0x1f; 7388 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch)) 7389 { 7390 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot); 7391 break; 7392 } 7393 if (rot == 0) 7394 { 7395 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0); 7396 break; 7397 } 7398 used_at = 1; 7399 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot); 7400 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f); 7401 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT); 7402 } 7403 break; 7404 7405 case M_S_DOB: 7406 if (mips_opts.arch == CPU_R4650) 7407 { 7408 as_bad (_("opcode not supported on this processor")); 7409 break; 7410 } 7411 assert (mips_opts.isa == ISA_MIPS1); 7412 /* Even on a big endian machine $fn comes before $fn+1. We have 7413 to adjust when storing to memory. */ 7414 macro_build (&offset_expr, "swc1", "T,o(b)", 7415 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg); 7416 offset_expr.X_add_number += 4; 7417 macro_build (&offset_expr, "swc1", "T,o(b)", 7418 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg); 7419 break; 7420 7421 case M_SEQ: 7422 if (sreg == 0) 7423 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16); 7424 else if (treg == 0) 7425 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16); 7426 else 7427 { 7428 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg); 7429 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16); 7430 } 7431 break; 7432 7433 case M_SEQ_I: 7434 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0) 7435 { 7436 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16); 7437 break; 7438 } 7439 if (sreg == 0) 7440 { 7441 as_warn (_("Instruction %s: result is always false"), 7442 ip->insn_mo->name); 7443 move_register (dreg, 0); 7444 break; 7445 } 7446 if (imm_expr.X_op == O_constant 7447 && imm_expr.X_add_number >= 0 7448 && imm_expr.X_add_number < 0x10000) 7449 { 7450 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16); 7451 } 7452 else if (imm_expr.X_op == O_constant 7453 && imm_expr.X_add_number > -0x8000 7454 && imm_expr.X_add_number < 0) 7455 { 7456 imm_expr.X_add_number = -imm_expr.X_add_number; 7457 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu", 7458 "t,r,j", dreg, sreg, BFD_RELOC_LO16); 7459 } 7460 else 7461 { 7462 load_register (AT, &imm_expr, HAVE_64BIT_GPRS); 7463 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT); 7464 used_at = 1; 7465 } 7466 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16); 7467 break; 7468 7469 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */ 7470 s = "slt"; 7471 goto sge; 7472 case M_SGEU: 7473 s = "sltu"; 7474 sge: 7475 macro_build (NULL, s, "d,v,t", dreg, sreg, treg); 7476 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16); 7477 break; 7478 7479 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */ 7480 case M_SGEU_I: 7481 if (imm_expr.X_op == O_constant 7482 && imm_expr.X_add_number >= -0x8000 7483 && imm_expr.X_add_number < 0x8000) 7484 { 7485 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j", 7486 dreg, sreg, BFD_RELOC_LO16); 7487 } 7488 else 7489 { 7490 load_register (AT, &imm_expr, HAVE_64BIT_GPRS); 7491 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t", 7492 dreg, sreg, AT); 7493 used_at = 1; 7494 } 7495 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16); 7496 break; 7497 7498 case M_SGT: /* sreg > treg <==> treg < sreg */ 7499 s = "slt"; 7500 goto sgt; 7501 case M_SGTU: 7502 s = "sltu"; 7503 sgt: 7504 macro_build (NULL, s, "d,v,t", dreg, treg, sreg); 7505 break; 7506 7507 case M_SGT_I: /* sreg > I <==> I < sreg */ 7508 s = "slt"; 7509 goto sgti; 7510 case M_SGTU_I: 7511 s = "sltu"; 7512 sgti: 7513 used_at = 1; 7514 load_register (AT, &imm_expr, HAVE_64BIT_GPRS); 7515 macro_build (NULL, s, "d,v,t", dreg, AT, sreg); 7516 break; 7517 7518 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */ 7519 s = "slt"; 7520 goto sle; 7521 case M_SLEU: 7522 s = "sltu"; 7523 sle: 7524 macro_build (NULL, s, "d,v,t", dreg, treg, sreg); 7525 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16); 7526 break; 7527 7528 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */ 7529 s = "slt"; 7530 goto slei; 7531 case M_SLEU_I: 7532 s = "sltu"; 7533 slei: 7534 used_at = 1; 7535 load_register (AT, &imm_expr, HAVE_64BIT_GPRS); 7536 macro_build (NULL, s, "d,v,t", dreg, AT, sreg); 7537 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16); 7538 break; 7539 7540 case M_SLT_I: 7541 if (imm_expr.X_op == O_constant 7542 && imm_expr.X_add_number >= -0x8000 7543 && imm_expr.X_add_number < 0x8000) 7544 { 7545 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16); 7546 break; 7547 } 7548 used_at = 1; 7549 load_register (AT, &imm_expr, HAVE_64BIT_GPRS); 7550 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT); 7551 break; 7552 7553 case M_SLTU_I: 7554 if (imm_expr.X_op == O_constant 7555 && imm_expr.X_add_number >= -0x8000 7556 && imm_expr.X_add_number < 0x8000) 7557 { 7558 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg, 7559 BFD_RELOC_LO16); 7560 break; 7561 } 7562 used_at = 1; 7563 load_register (AT, &imm_expr, HAVE_64BIT_GPRS); 7564 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT); 7565 break; 7566 7567 case M_SNE: 7568 if (sreg == 0) 7569 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg); 7570 else if (treg == 0) 7571 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg); 7572 else 7573 { 7574 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg); 7575 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg); 7576 } 7577 break; 7578 7579 case M_SNE_I: 7580 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0) 7581 { 7582 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg); 7583 break; 7584 } 7585 if (sreg == 0) 7586 { 7587 as_warn (_("Instruction %s: result is always true"), 7588 ip->insn_mo->name); 7589 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j", 7590 dreg, 0, BFD_RELOC_LO16); 7591 break; 7592 } 7593 if (imm_expr.X_op == O_constant 7594 && imm_expr.X_add_number >= 0 7595 && imm_expr.X_add_number < 0x10000) 7596 { 7597 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16); 7598 } 7599 else if (imm_expr.X_op == O_constant 7600 && imm_expr.X_add_number > -0x8000 7601 && imm_expr.X_add_number < 0) 7602 { 7603 imm_expr.X_add_number = -imm_expr.X_add_number; 7604 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu", 7605 "t,r,j", dreg, sreg, BFD_RELOC_LO16); 7606 } 7607 else 7608 { 7609 load_register (AT, &imm_expr, HAVE_64BIT_GPRS); 7610 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT); 7611 used_at = 1; 7612 } 7613 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg); 7614 break; 7615 7616 case M_DSUB_I: 7617 dbl = 1; 7618 case M_SUB_I: 7619 if (imm_expr.X_op == O_constant 7620 && imm_expr.X_add_number > -0x8000 7621 && imm_expr.X_add_number <= 0x8000) 7622 { 7623 imm_expr.X_add_number = -imm_expr.X_add_number; 7624 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j", 7625 dreg, sreg, BFD_RELOC_LO16); 7626 break; 7627 } 7628 used_at = 1; 7629 load_register (AT, &imm_expr, dbl); 7630 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT); 7631 break; 7632 7633 case M_DSUBU_I: 7634 dbl = 1; 7635 case M_SUBU_I: 7636 if (imm_expr.X_op == O_constant 7637 && imm_expr.X_add_number > -0x8000 7638 && imm_expr.X_add_number <= 0x8000) 7639 { 7640 imm_expr.X_add_number = -imm_expr.X_add_number; 7641 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j", 7642 dreg, sreg, BFD_RELOC_LO16); 7643 break; 7644 } 7645 used_at = 1; 7646 load_register (AT, &imm_expr, dbl); 7647 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT); 7648 break; 7649 7650 case M_TEQ_I: 7651 s = "teq"; 7652 goto trap; 7653 case M_TGE_I: 7654 s = "tge"; 7655 goto trap; 7656 case M_TGEU_I: 7657 s = "tgeu"; 7658 goto trap; 7659 case M_TLT_I: 7660 s = "tlt"; 7661 goto trap; 7662 case M_TLTU_I: 7663 s = "tltu"; 7664 goto trap; 7665 case M_TNE_I: 7666 s = "tne"; 7667 trap: 7668 used_at = 1; 7669 load_register (AT, &imm_expr, HAVE_64BIT_GPRS); 7670 macro_build (NULL, s, "s,t", sreg, AT); 7671 break; 7672 7673 case M_TRUNCWS: 7674 case M_TRUNCWD: 7675 assert (mips_opts.isa == ISA_MIPS1); 7676 used_at = 1; 7677 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */ 7678 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */ 7679 7680 /* 7681 * Is the double cfc1 instruction a bug in the mips assembler; 7682 * or is there a reason for it? 7683 */ 7684 start_noreorder (); 7685 macro_build (NULL, "cfc1", "t,G", treg, RA); 7686 macro_build (NULL, "cfc1", "t,G", treg, RA); 7687 macro_build (NULL, "nop", ""); 7688 expr1.X_add_number = 3; 7689 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16); 7690 expr1.X_add_number = 2; 7691 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16); 7692 macro_build (NULL, "ctc1", "t,G", AT, RA); 7693 macro_build (NULL, "nop", ""); 7694 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", 7695 dreg, sreg); 7696 macro_build (NULL, "ctc1", "t,G", treg, RA); 7697 macro_build (NULL, "nop", ""); 7698 end_noreorder (); 7699 break; 7700 7701 case M_ULH: 7702 s = "lb"; 7703 goto ulh; 7704 case M_ULHU: 7705 s = "lbu"; 7706 ulh: 7707 used_at = 1; 7708 if (offset_expr.X_add_number >= 0x7fff) 7709 as_bad (_("operand overflow")); 7710 if (! target_big_endian) 7711 ++offset_expr.X_add_number; 7712 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg); 7713 if (! target_big_endian) 7714 --offset_expr.X_add_number; 7715 else 7716 ++offset_expr.X_add_number; 7717 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg); 7718 macro_build (NULL, "sll", "d,w,<", AT, AT, 8); 7719 macro_build (NULL, "or", "d,v,t", treg, treg, AT); 7720 break; 7721 7722 case M_ULD: 7723 s = "ldl"; 7724 s2 = "ldr"; 7725 off = 7; 7726 goto ulw; 7727 case M_ULW: 7728 s = "lwl"; 7729 s2 = "lwr"; 7730 off = 3; 7731 ulw: 7732 if (offset_expr.X_add_number >= 0x8000 - off) 7733 as_bad (_("operand overflow")); 7734 if (treg != breg) 7735 tempreg = treg; 7736 else 7737 { 7738 used_at = 1; 7739 tempreg = AT; 7740 } 7741 if (! target_big_endian) 7742 offset_expr.X_add_number += off; 7743 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg); 7744 if (! target_big_endian) 7745 offset_expr.X_add_number -= off; 7746 else 7747 offset_expr.X_add_number += off; 7748 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg); 7749 7750 /* If necessary, move the result in tempreg the final destination. */ 7751 if (treg == tempreg) 7752 break; 7753 /* Protect second load's delay slot. */ 7754 load_delay_nop (); 7755 move_register (treg, tempreg); 7756 break; 7757 7758 case M_ULD_A: 7759 s = "ldl"; 7760 s2 = "ldr"; 7761 off = 7; 7762 goto ulwa; 7763 case M_ULW_A: 7764 s = "lwl"; 7765 s2 = "lwr"; 7766 off = 3; 7767 ulwa: 7768 used_at = 1; 7769 load_address (AT, &offset_expr, &used_at); 7770 if (breg != 0) 7771 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg); 7772 if (! target_big_endian) 7773 expr1.X_add_number = off; 7774 else 7775 expr1.X_add_number = 0; 7776 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT); 7777 if (! target_big_endian) 7778 expr1.X_add_number = 0; 7779 else 7780 expr1.X_add_number = off; 7781 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT); 7782 break; 7783 7784 case M_ULH_A: 7785 case M_ULHU_A: 7786 used_at = 1; 7787 load_address (AT, &offset_expr, &used_at); 7788 if (breg != 0) 7789 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg); 7790 if (target_big_endian) 7791 expr1.X_add_number = 0; 7792 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", 7793 treg, BFD_RELOC_LO16, AT); 7794 if (target_big_endian) 7795 expr1.X_add_number = 1; 7796 else 7797 expr1.X_add_number = 0; 7798 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT); 7799 macro_build (NULL, "sll", "d,w,<", treg, treg, 8); 7800 macro_build (NULL, "or", "d,v,t", treg, treg, AT); 7801 break; 7802 7803 case M_USH: 7804 used_at = 1; 7805 if (offset_expr.X_add_number >= 0x7fff) 7806 as_bad (_("operand overflow")); 7807 if (target_big_endian) 7808 ++offset_expr.X_add_number; 7809 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg); 7810 macro_build (NULL, "srl", "d,w,<", AT, treg, 8); 7811 if (target_big_endian) 7812 --offset_expr.X_add_number; 7813 else 7814 ++offset_expr.X_add_number; 7815 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg); 7816 break; 7817 7818 case M_USD: 7819 s = "sdl"; 7820 s2 = "sdr"; 7821 off = 7; 7822 goto usw; 7823 case M_USW: 7824 s = "swl"; 7825 s2 = "swr"; 7826 off = 3; 7827 usw: 7828 if (offset_expr.X_add_number >= 0x8000 - off) 7829 as_bad (_("operand overflow")); 7830 if (! target_big_endian) 7831 offset_expr.X_add_number += off; 7832 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg); 7833 if (! target_big_endian) 7834 offset_expr.X_add_number -= off; 7835 else 7836 offset_expr.X_add_number += off; 7837 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg); 7838 break; 7839 7840 case M_USD_A: 7841 s = "sdl"; 7842 s2 = "sdr"; 7843 off = 7; 7844 goto uswa; 7845 case M_USW_A: 7846 s = "swl"; 7847 s2 = "swr"; 7848 off = 3; 7849 uswa: 7850 used_at = 1; 7851 load_address (AT, &offset_expr, &used_at); 7852 if (breg != 0) 7853 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg); 7854 if (! target_big_endian) 7855 expr1.X_add_number = off; 7856 else 7857 expr1.X_add_number = 0; 7858 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT); 7859 if (! target_big_endian) 7860 expr1.X_add_number = 0; 7861 else 7862 expr1.X_add_number = off; 7863 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT); 7864 break; 7865 7866 case M_USH_A: 7867 used_at = 1; 7868 load_address (AT, &offset_expr, &used_at); 7869 if (breg != 0) 7870 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg); 7871 if (! target_big_endian) 7872 expr1.X_add_number = 0; 7873 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT); 7874 macro_build (NULL, "srl", "d,w,<", treg, treg, 8); 7875 if (! target_big_endian) 7876 expr1.X_add_number = 1; 7877 else 7878 expr1.X_add_number = 0; 7879 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT); 7880 if (! target_big_endian) 7881 expr1.X_add_number = 0; 7882 else 7883 expr1.X_add_number = 1; 7884 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT); 7885 macro_build (NULL, "sll", "d,w,<", treg, treg, 8); 7886 macro_build (NULL, "or", "d,v,t", treg, treg, AT); 7887 break; 7888 7889 default: 7890 /* FIXME: Check if this is one of the itbl macros, since they 7891 are added dynamically. */ 7892 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name); 7893 break; 7894 } 7895 if (mips_opts.noat && used_at) 7896 as_bad (_("Macro used $at after \".set noat\"")); 7897 } 7898 7899 /* Implement macros in mips16 mode. */ 7900 7901 static void 7902 mips16_macro (struct mips_cl_insn *ip) 7903 { 7904 int mask; 7905 int xreg, yreg, zreg, tmp; 7906 expressionS expr1; 7907 int dbl; 7908 const char *s, *s2, *s3; 7909 7910 mask = ip->insn_mo->mask; 7911 7912 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip); 7913 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip); 7914 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip); 7915 7916 expr1.X_op = O_constant; 7917 expr1.X_op_symbol = NULL; 7918 expr1.X_add_symbol = NULL; 7919 expr1.X_add_number = 1; 7920 7921 dbl = 0; 7922 7923 switch (mask) 7924 { 7925 default: 7926 internalError (); 7927 7928 case M_DDIV_3: 7929 dbl = 1; 7930 case M_DIV_3: 7931 s = "mflo"; 7932 goto do_div3; 7933 case M_DREM_3: 7934 dbl = 1; 7935 case M_REM_3: 7936 s = "mfhi"; 7937 do_div3: 7938 start_noreorder (); 7939 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg); 7940 expr1.X_add_number = 2; 7941 macro_build (&expr1, "bnez", "x,p", yreg); 7942 macro_build (NULL, "break", "6", 7); 7943 7944 /* FIXME: The normal code checks for of -1 / -0x80000000 here, 7945 since that causes an overflow. We should do that as well, 7946 but I don't see how to do the comparisons without a temporary 7947 register. */ 7948 end_noreorder (); 7949 macro_build (NULL, s, "x", zreg); 7950 break; 7951 7952 case M_DIVU_3: 7953 s = "divu"; 7954 s2 = "mflo"; 7955 goto do_divu3; 7956 case M_REMU_3: 7957 s = "divu"; 7958 s2 = "mfhi"; 7959 goto do_divu3; 7960 case M_DDIVU_3: 7961 s = "ddivu"; 7962 s2 = "mflo"; 7963 goto do_divu3; 7964 case M_DREMU_3: 7965 s = "ddivu"; 7966 s2 = "mfhi"; 7967 do_divu3: 7968 start_noreorder (); 7969 macro_build (NULL, s, "0,x,y", xreg, yreg); 7970 expr1.X_add_number = 2; 7971 macro_build (&expr1, "bnez", "x,p", yreg); 7972 macro_build (NULL, "break", "6", 7); 7973 end_noreorder (); 7974 macro_build (NULL, s2, "x", zreg); 7975 break; 7976 7977 case M_DMUL: 7978 dbl = 1; 7979 case M_MUL: 7980 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg); 7981 macro_build (NULL, "mflo", "x", zreg); 7982 break; 7983 7984 case M_DSUBU_I: 7985 dbl = 1; 7986 goto do_subu; 7987 case M_SUBU_I: 7988 do_subu: 7989 if (imm_expr.X_op != O_constant) 7990 as_bad (_("Unsupported large constant")); 7991 imm_expr.X_add_number = -imm_expr.X_add_number; 7992 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg); 7993 break; 7994 7995 case M_SUBU_I_2: 7996 if (imm_expr.X_op != O_constant) 7997 as_bad (_("Unsupported large constant")); 7998 imm_expr.X_add_number = -imm_expr.X_add_number; 7999 macro_build (&imm_expr, "addiu", "x,k", xreg); 8000 break; 8001 8002 case M_DSUBU_I_2: 8003 if (imm_expr.X_op != O_constant) 8004 as_bad (_("Unsupported large constant")); 8005 imm_expr.X_add_number = -imm_expr.X_add_number; 8006 macro_build (&imm_expr, "daddiu", "y,j", yreg); 8007 break; 8008 8009 case M_BEQ: 8010 s = "cmp"; 8011 s2 = "bteqz"; 8012 goto do_branch; 8013 case M_BNE: 8014 s = "cmp"; 8015 s2 = "btnez"; 8016 goto do_branch; 8017 case M_BLT: 8018 s = "slt"; 8019 s2 = "btnez"; 8020 goto do_branch; 8021 case M_BLTU: 8022 s = "sltu"; 8023 s2 = "btnez"; 8024 goto do_branch; 8025 case M_BLE: 8026 s = "slt"; 8027 s2 = "bteqz"; 8028 goto do_reverse_branch; 8029 case M_BLEU: 8030 s = "sltu"; 8031 s2 = "bteqz"; 8032 goto do_reverse_branch; 8033 case M_BGE: 8034 s = "slt"; 8035 s2 = "bteqz"; 8036 goto do_branch; 8037 case M_BGEU: 8038 s = "sltu"; 8039 s2 = "bteqz"; 8040 goto do_branch; 8041 case M_BGT: 8042 s = "slt"; 8043 s2 = "btnez"; 8044 goto do_reverse_branch; 8045 case M_BGTU: 8046 s = "sltu"; 8047 s2 = "btnez"; 8048 8049 do_reverse_branch: 8050 tmp = xreg; 8051 xreg = yreg; 8052 yreg = tmp; 8053 8054 do_branch: 8055 macro_build (NULL, s, "x,y", xreg, yreg); 8056 macro_build (&offset_expr, s2, "p"); 8057 break; 8058 8059 case M_BEQ_I: 8060 s = "cmpi"; 8061 s2 = "bteqz"; 8062 s3 = "x,U"; 8063 goto do_branch_i; 8064 case M_BNE_I: 8065 s = "cmpi"; 8066 s2 = "btnez"; 8067 s3 = "x,U"; 8068 goto do_branch_i; 8069 case M_BLT_I: 8070 s = "slti"; 8071 s2 = "btnez"; 8072 s3 = "x,8"; 8073 goto do_branch_i; 8074 case M_BLTU_I: 8075 s = "sltiu"; 8076 s2 = "btnez"; 8077 s3 = "x,8"; 8078 goto do_branch_i; 8079 case M_BLE_I: 8080 s = "slti"; 8081 s2 = "btnez"; 8082 s3 = "x,8"; 8083 goto do_addone_branch_i; 8084 case M_BLEU_I: 8085 s = "sltiu"; 8086 s2 = "btnez"; 8087 s3 = "x,8"; 8088 goto do_addone_branch_i; 8089 case M_BGE_I: 8090 s = "slti"; 8091 s2 = "bteqz"; 8092 s3 = "x,8"; 8093 goto do_branch_i; 8094 case M_BGEU_I: 8095 s = "sltiu"; 8096 s2 = "bteqz"; 8097 s3 = "x,8"; 8098 goto do_branch_i; 8099 case M_BGT_I: 8100 s = "slti"; 8101 s2 = "bteqz"; 8102 s3 = "x,8"; 8103 goto do_addone_branch_i; 8104 case M_BGTU_I: 8105 s = "sltiu"; 8106 s2 = "bteqz"; 8107 s3 = "x,8"; 8108 8109 do_addone_branch_i: 8110 if (imm_expr.X_op != O_constant) 8111 as_bad (_("Unsupported large constant")); 8112 ++imm_expr.X_add_number; 8113 8114 do_branch_i: 8115 macro_build (&imm_expr, s, s3, xreg); 8116 macro_build (&offset_expr, s2, "p"); 8117 break; 8118 8119 case M_ABS: 8120 expr1.X_add_number = 0; 8121 macro_build (&expr1, "slti", "x,8", yreg); 8122 if (xreg != yreg) 8123 move_register (xreg, yreg); 8124 expr1.X_add_number = 2; 8125 macro_build (&expr1, "bteqz", "p"); 8126 macro_build (NULL, "neg", "x,w", xreg, xreg); 8127 } 8128 } 8129 8130 /* For consistency checking, verify that all bits are specified either 8131 by the match/mask part of the instruction definition, or by the 8132 operand list. */ 8133 static int 8134 validate_mips_insn (const struct mips_opcode *opc) 8135 { 8136 const char *p = opc->args; 8137 char c; 8138 unsigned long used_bits = opc->mask; 8139 8140 if ((used_bits & opc->match) != opc->match) 8141 { 8142 as_bad (_("internal: bad mips opcode (mask error): %s %s"), 8143 opc->name, opc->args); 8144 return 0; 8145 } 8146 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift))) 8147 while (*p) 8148 switch (c = *p++) 8149 { 8150 case ',': break; 8151 case '(': break; 8152 case ')': break; 8153 case '+': 8154 switch (c = *p++) 8155 { 8156 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break; 8157 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break; 8158 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break; 8159 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD); 8160 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break; 8161 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break; 8162 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break; 8163 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break; 8164 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break; 8165 case 'I': break; 8166 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break; 8167 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT); 8168 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break; 8169 default: 8170 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"), 8171 c, opc->name, opc->args); 8172 return 0; 8173 } 8174 break; 8175 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break; 8176 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break; 8177 case 'A': break; 8178 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break; 8179 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break; 8180 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break; 8181 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break; 8182 case 'F': break; 8183 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break; 8184 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break; 8185 case 'I': break; 8186 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break; 8187 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break; 8188 case 'L': break; 8189 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break; 8190 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break; 8191 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break; 8192 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL); 8193 USE_BITS (OP_MASK_FT, OP_SH_FT); break; 8194 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break; 8195 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break; 8196 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break; 8197 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break; 8198 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break; 8199 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break; 8200 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break; 8201 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break; 8202 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break; 8203 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break; 8204 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break; 8205 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break; 8206 case 'f': break; 8207 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break; 8208 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break; 8209 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break; 8210 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break; 8211 case 'l': break; 8212 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break; 8213 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break; 8214 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break; 8215 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break; 8216 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break; 8217 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break; 8218 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break; 8219 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break; 8220 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break; 8221 case 'x': break; 8222 case 'z': break; 8223 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break; 8224 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD); 8225 USE_BITS (OP_MASK_RT, OP_SH_RT); break; 8226 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break; 8227 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break; 8228 case '[': break; 8229 case ']': break; 8230 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break; 8231 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break; 8232 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break; 8233 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break; 8234 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break; 8235 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break; 8236 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break; 8237 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break; 8238 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break; 8239 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break; 8240 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break; 8241 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break; 8242 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break; 8243 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break; 8244 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break; 8245 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break; 8246 default: 8247 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"), 8248 c, opc->name, opc->args); 8249 return 0; 8250 } 8251 #undef USE_BITS 8252 if (used_bits != 0xffffffff) 8253 { 8254 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"), 8255 ~used_bits & 0xffffffff, opc->name, opc->args); 8256 return 0; 8257 } 8258 return 1; 8259 } 8260 8261 /* This routine assembles an instruction into its binary format. As a 8262 side effect, it sets one of the global variables imm_reloc or 8263 offset_reloc to the type of relocation to do if one of the operands 8264 is an address expression. */ 8265 8266 static void 8267 mips_ip (char *str, struct mips_cl_insn *ip) 8268 { 8269 char *s; 8270 const char *args; 8271 char c = 0; 8272 struct mips_opcode *insn; 8273 char *argsStart; 8274 unsigned int regno; 8275 unsigned int lastregno = 0; 8276 unsigned int lastpos = 0; 8277 unsigned int limlo, limhi; 8278 char *s_reset; 8279 char save_c = 0; 8280 offsetT min_range, max_range; 8281 int argnum; 8282 unsigned int rtype; 8283 8284 insn_error = NULL; 8285 8286 /* If the instruction contains a '.', we first try to match an instruction 8287 including the '.'. Then we try again without the '.'. */ 8288 insn = NULL; 8289 for (s = str; *s != '\0' && !ISSPACE (*s); ++s) 8290 continue; 8291 8292 /* If we stopped on whitespace, then replace the whitespace with null for 8293 the call to hash_find. Save the character we replaced just in case we 8294 have to re-parse the instruction. */ 8295 if (ISSPACE (*s)) 8296 { 8297 save_c = *s; 8298 *s++ = '\0'; 8299 } 8300 8301 insn = (struct mips_opcode *) hash_find (op_hash, str); 8302 8303 /* If we didn't find the instruction in the opcode table, try again, but 8304 this time with just the instruction up to, but not including the 8305 first '.'. */ 8306 if (insn == NULL) 8307 { 8308 /* Restore the character we overwrite above (if any). */ 8309 if (save_c) 8310 *(--s) = save_c; 8311 8312 /* Scan up to the first '.' or whitespace. */ 8313 for (s = str; 8314 *s != '\0' && *s != '.' && !ISSPACE (*s); 8315 ++s) 8316 continue; 8317 8318 /* If we did not find a '.', then we can quit now. */ 8319 if (*s != '.') 8320 { 8321 insn_error = "unrecognized opcode"; 8322 return; 8323 } 8324 8325 /* Lookup the instruction in the hash table. */ 8326 *s++ = '\0'; 8327 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL) 8328 { 8329 insn_error = "unrecognized opcode"; 8330 return; 8331 } 8332 } 8333 8334 argsStart = s; 8335 for (;;) 8336 { 8337 bfd_boolean ok; 8338 8339 assert (strcmp (insn->name, str) == 0); 8340 8341 if (OPCODE_IS_MEMBER (insn, 8342 (mips_opts.isa 8343 | (file_ase_mips16 ? INSN_MIPS16 : 0) 8344 | (mips_opts.ase_mdmx ? INSN_MDMX : 0) 8345 | (mips_opts.ase_dsp ? INSN_DSP : 0) 8346 | (mips_opts.ase_mt ? INSN_MT : 0) 8347 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)), 8348 mips_opts.arch)) 8349 ok = TRUE; 8350 else 8351 ok = FALSE; 8352 8353 if (insn->pinfo != INSN_MACRO) 8354 { 8355 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0) 8356 ok = FALSE; 8357 } 8358 8359 if (! ok) 8360 { 8361 if (insn + 1 < &mips_opcodes[NUMOPCODES] 8362 && strcmp (insn->name, insn[1].name) == 0) 8363 { 8364 ++insn; 8365 continue; 8366 } 8367 else 8368 { 8369 if (!insn_error) 8370 { 8371 static char buf[100]; 8372 sprintf (buf, 8373 _("opcode not supported on this processor: %s (%s)"), 8374 mips_cpu_info_from_arch (mips_opts.arch)->name, 8375 mips_cpu_info_from_isa (mips_opts.isa)->name); 8376 insn_error = buf; 8377 } 8378 if (save_c) 8379 *(--s) = save_c; 8380 return; 8381 } 8382 } 8383 8384 create_insn (ip, insn); 8385 insn_error = NULL; 8386 argnum = 1; 8387 for (args = insn->args;; ++args) 8388 { 8389 int is_mdmx; 8390 8391 s += strspn (s, " \t"); 8392 is_mdmx = 0; 8393 switch (*args) 8394 { 8395 case '\0': /* end of args */ 8396 if (*s == '\0') 8397 return; 8398 break; 8399 8400 case '3': /* dsp 3-bit unsigned immediate in bit 21 */ 8401 my_getExpression (&imm_expr, s); 8402 check_absolute_expr (ip, &imm_expr); 8403 if (imm_expr.X_add_number & ~OP_MASK_SA3) 8404 { 8405 as_warn (_("DSP immediate not in range 0..%d (%lu)"), 8406 OP_MASK_SA3, (unsigned long) imm_expr.X_add_number); 8407 imm_expr.X_add_number &= OP_MASK_SA3; 8408 } 8409 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SA3; 8410 imm_expr.X_op = O_absent; 8411 s = expr_end; 8412 continue; 8413 8414 case '4': /* dsp 4-bit unsigned immediate in bit 21 */ 8415 my_getExpression (&imm_expr, s); 8416 check_absolute_expr (ip, &imm_expr); 8417 if (imm_expr.X_add_number & ~OP_MASK_SA4) 8418 { 8419 as_warn (_("DSP immediate not in range 0..%d (%lu)"), 8420 OP_MASK_SA4, (unsigned long) imm_expr.X_add_number); 8421 imm_expr.X_add_number &= OP_MASK_SA4; 8422 } 8423 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SA4; 8424 imm_expr.X_op = O_absent; 8425 s = expr_end; 8426 continue; 8427 8428 case '5': /* dsp 8-bit unsigned immediate in bit 16 */ 8429 my_getExpression (&imm_expr, s); 8430 check_absolute_expr (ip, &imm_expr); 8431 if (imm_expr.X_add_number & ~OP_MASK_IMM8) 8432 { 8433 as_warn (_("DSP immediate not in range 0..%d (%lu)"), 8434 OP_MASK_IMM8, (unsigned long) imm_expr.X_add_number); 8435 imm_expr.X_add_number &= OP_MASK_IMM8; 8436 } 8437 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_IMM8; 8438 imm_expr.X_op = O_absent; 8439 s = expr_end; 8440 continue; 8441 8442 case '6': /* dsp 5-bit unsigned immediate in bit 21 */ 8443 my_getExpression (&imm_expr, s); 8444 check_absolute_expr (ip, &imm_expr); 8445 if (imm_expr.X_add_number & ~OP_MASK_RS) 8446 { 8447 as_warn (_("DSP immediate not in range 0..%d (%lu)"), 8448 OP_MASK_RS, (unsigned long) imm_expr.X_add_number); 8449 imm_expr.X_add_number &= OP_MASK_RS; 8450 } 8451 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_RS; 8452 imm_expr.X_op = O_absent; 8453 s = expr_end; 8454 continue; 8455 8456 case '7': /* four dsp accumulators in bits 11,12 */ 8457 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' && 8458 s[3] >= '0' && s[3] <= '3') 8459 { 8460 regno = s[3] - '0'; 8461 s += 4; 8462 ip->insn_opcode |= regno << OP_SH_DSPACC; 8463 continue; 8464 } 8465 else 8466 as_bad (_("Invalid dsp acc register")); 8467 break; 8468 8469 case '8': /* dsp 6-bit unsigned immediate in bit 11 */ 8470 my_getExpression (&imm_expr, s); 8471 check_absolute_expr (ip, &imm_expr); 8472 if (imm_expr.X_add_number & ~OP_MASK_WRDSP) 8473 { 8474 as_warn (_("DSP immediate not in range 0..%d (%lu)"), 8475 OP_MASK_WRDSP, 8476 (unsigned long) imm_expr.X_add_number); 8477 imm_expr.X_add_number &= OP_MASK_WRDSP; 8478 } 8479 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_WRDSP; 8480 imm_expr.X_op = O_absent; 8481 s = expr_end; 8482 continue; 8483 8484 case '9': /* four dsp accumulators in bits 21,22 */ 8485 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' && 8486 s[3] >= '0' && s[3] <= '3') 8487 { 8488 regno = s[3] - '0'; 8489 s += 4; 8490 ip->insn_opcode |= regno << OP_SH_DSPACC_S; 8491 continue; 8492 } 8493 else 8494 as_bad (_("Invalid dsp acc register")); 8495 break; 8496 8497 case '0': /* dsp 6-bit signed immediate in bit 20 */ 8498 my_getExpression (&imm_expr, s); 8499 check_absolute_expr (ip, &imm_expr); 8500 min_range = -((OP_MASK_DSPSFT + 1) >> 1); 8501 max_range = ((OP_MASK_DSPSFT + 1) >> 1) - 1; 8502 if (imm_expr.X_add_number < min_range || 8503 imm_expr.X_add_number > max_range) 8504 { 8505 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"), 8506 (long) min_range, (long) max_range, 8507 (long) imm_expr.X_add_number); 8508 } 8509 imm_expr.X_add_number &= OP_MASK_DSPSFT; 8510 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number 8511 << OP_SH_DSPSFT); 8512 imm_expr.X_op = O_absent; 8513 s = expr_end; 8514 continue; 8515 8516 case '\'': /* dsp 6-bit unsigned immediate in bit 16 */ 8517 my_getExpression (&imm_expr, s); 8518 check_absolute_expr (ip, &imm_expr); 8519 if (imm_expr.X_add_number & ~OP_MASK_RDDSP) 8520 { 8521 as_warn (_("DSP immediate not in range 0..%d (%lu)"), 8522 OP_MASK_RDDSP, 8523 (unsigned long) imm_expr.X_add_number); 8524 imm_expr.X_add_number &= OP_MASK_RDDSP; 8525 } 8526 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_RDDSP; 8527 imm_expr.X_op = O_absent; 8528 s = expr_end; 8529 continue; 8530 8531 case ':': /* dsp 7-bit signed immediate in bit 19 */ 8532 my_getExpression (&imm_expr, s); 8533 check_absolute_expr (ip, &imm_expr); 8534 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1); 8535 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1; 8536 if (imm_expr.X_add_number < min_range || 8537 imm_expr.X_add_number > max_range) 8538 { 8539 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"), 8540 (long) min_range, (long) max_range, 8541 (long) imm_expr.X_add_number); 8542 } 8543 imm_expr.X_add_number &= OP_MASK_DSPSFT_7; 8544 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number 8545 << OP_SH_DSPSFT_7); 8546 imm_expr.X_op = O_absent; 8547 s = expr_end; 8548 continue; 8549 8550 case '@': /* dsp 10-bit signed immediate in bit 16 */ 8551 my_getExpression (&imm_expr, s); 8552 check_absolute_expr (ip, &imm_expr); 8553 min_range = -((OP_MASK_IMM10 + 1) >> 1); 8554 max_range = ((OP_MASK_IMM10 + 1) >> 1) - 1; 8555 if (imm_expr.X_add_number < min_range || 8556 imm_expr.X_add_number > max_range) 8557 { 8558 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"), 8559 (long) min_range, (long) max_range, 8560 (long) imm_expr.X_add_number); 8561 } 8562 imm_expr.X_add_number &= OP_MASK_IMM10; 8563 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number 8564 << OP_SH_IMM10); 8565 imm_expr.X_op = O_absent; 8566 s = expr_end; 8567 continue; 8568 8569 case '!': /* mt 1-bit unsigned immediate in bit 5 */ 8570 my_getExpression (&imm_expr, s); 8571 check_absolute_expr (ip, &imm_expr); 8572 if (imm_expr.X_add_number & ~OP_MASK_MT_U) 8573 { 8574 as_warn (_("MT immediate not in range 0..%d (%lu)"), 8575 OP_MASK_MT_U, (unsigned long) imm_expr.X_add_number); 8576 imm_expr.X_add_number &= OP_MASK_MT_U; 8577 } 8578 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_MT_U; 8579 imm_expr.X_op = O_absent; 8580 s = expr_end; 8581 continue; 8582 8583 case '$': /* mt 1-bit unsigned immediate in bit 4 */ 8584 my_getExpression (&imm_expr, s); 8585 check_absolute_expr (ip, &imm_expr); 8586 if (imm_expr.X_add_number & ~OP_MASK_MT_H) 8587 { 8588 as_warn (_("MT immediate not in range 0..%d (%lu)"), 8589 OP_MASK_MT_H, (unsigned long) imm_expr.X_add_number); 8590 imm_expr.X_add_number &= OP_MASK_MT_H; 8591 } 8592 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_MT_H; 8593 imm_expr.X_op = O_absent; 8594 s = expr_end; 8595 continue; 8596 8597 case '*': /* four dsp accumulators in bits 18,19 */ 8598 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' && 8599 s[3] >= '0' && s[3] <= '3') 8600 { 8601 regno = s[3] - '0'; 8602 s += 4; 8603 ip->insn_opcode |= regno << OP_SH_MTACC_T; 8604 continue; 8605 } 8606 else 8607 as_bad (_("Invalid dsp/smartmips acc register")); 8608 break; 8609 8610 case '&': /* four dsp accumulators in bits 13,14 */ 8611 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' && 8612 s[3] >= '0' && s[3] <= '3') 8613 { 8614 regno = s[3] - '0'; 8615 s += 4; 8616 ip->insn_opcode |= regno << OP_SH_MTACC_D; 8617 continue; 8618 } 8619 else 8620 as_bad (_("Invalid dsp/smartmips acc register")); 8621 break; 8622 8623 case ',': 8624 if (*s++ == *args) 8625 continue; 8626 s--; 8627 switch (*++args) 8628 { 8629 case 'r': 8630 case 'v': 8631 INSERT_OPERAND (RS, *ip, lastregno); 8632 continue; 8633 8634 case 'w': 8635 INSERT_OPERAND (RT, *ip, lastregno); 8636 continue; 8637 8638 case 'W': 8639 INSERT_OPERAND (FT, *ip, lastregno); 8640 continue; 8641 8642 case 'V': 8643 INSERT_OPERAND (FS, *ip, lastregno); 8644 continue; 8645 } 8646 break; 8647 8648 case '(': 8649 /* Handle optional base register. 8650 Either the base register is omitted or 8651 we must have a left paren. */ 8652 /* This is dependent on the next operand specifier 8653 is a base register specification. */ 8654 assert (args[1] == 'b' || args[1] == '5' 8655 || args[1] == '-' || args[1] == '4'); 8656 if (*s == '\0') 8657 return; 8658 8659 case ')': /* these must match exactly */ 8660 case '[': 8661 case ']': 8662 if (*s++ == *args) 8663 continue; 8664 break; 8665 8666 case '+': /* Opcode extension character. */ 8667 switch (*++args) 8668 { 8669 case 'A': /* ins/ext position, becomes LSB. */ 8670 limlo = 0; 8671 limhi = 31; 8672 goto do_lsb; 8673 case 'E': 8674 limlo = 32; 8675 limhi = 63; 8676 goto do_lsb; 8677 do_lsb: 8678 my_getExpression (&imm_expr, s); 8679 check_absolute_expr (ip, &imm_expr); 8680 if ((unsigned long) imm_expr.X_add_number < limlo 8681 || (unsigned long) imm_expr.X_add_number > limhi) 8682 { 8683 as_bad (_("Improper position (%lu)"), 8684 (unsigned long) imm_expr.X_add_number); 8685 imm_expr.X_add_number = limlo; 8686 } 8687 lastpos = imm_expr.X_add_number; 8688 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number); 8689 imm_expr.X_op = O_absent; 8690 s = expr_end; 8691 continue; 8692 8693 case 'B': /* ins size, becomes MSB. */ 8694 limlo = 1; 8695 limhi = 32; 8696 goto do_msb; 8697 case 'F': 8698 limlo = 33; 8699 limhi = 64; 8700 goto do_msb; 8701 do_msb: 8702 my_getExpression (&imm_expr, s); 8703 check_absolute_expr (ip, &imm_expr); 8704 /* Check for negative input so that small negative numbers 8705 will not succeed incorrectly. The checks against 8706 (pos+size) transitively check "size" itself, 8707 assuming that "pos" is reasonable. */ 8708 if ((long) imm_expr.X_add_number < 0 8709 || ((unsigned long) imm_expr.X_add_number 8710 + lastpos) < limlo 8711 || ((unsigned long) imm_expr.X_add_number 8712 + lastpos) > limhi) 8713 { 8714 as_bad (_("Improper insert size (%lu, position %lu)"), 8715 (unsigned long) imm_expr.X_add_number, 8716 (unsigned long) lastpos); 8717 imm_expr.X_add_number = limlo - lastpos; 8718 } 8719 INSERT_OPERAND (INSMSB, *ip, 8720 lastpos + imm_expr.X_add_number - 1); 8721 imm_expr.X_op = O_absent; 8722 s = expr_end; 8723 continue; 8724 8725 case 'C': /* ext size, becomes MSBD. */ 8726 limlo = 1; 8727 limhi = 32; 8728 goto do_msbd; 8729 case 'G': 8730 limlo = 33; 8731 limhi = 64; 8732 goto do_msbd; 8733 case 'H': 8734 limlo = 33; 8735 limhi = 64; 8736 goto do_msbd; 8737 do_msbd: 8738 my_getExpression (&imm_expr, s); 8739 check_absolute_expr (ip, &imm_expr); 8740 /* Check for negative input so that small negative numbers 8741 will not succeed incorrectly. The checks against 8742 (pos+size) transitively check "size" itself, 8743 assuming that "pos" is reasonable. */ 8744 if ((long) imm_expr.X_add_number < 0 8745 || ((unsigned long) imm_expr.X_add_number 8746 + lastpos) < limlo 8747 || ((unsigned long) imm_expr.X_add_number 8748 + lastpos) > limhi) 8749 { 8750 as_bad (_("Improper extract size (%lu, position %lu)"), 8751 (unsigned long) imm_expr.X_add_number, 8752 (unsigned long) lastpos); 8753 imm_expr.X_add_number = limlo - lastpos; 8754 } 8755 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1); 8756 imm_expr.X_op = O_absent; 8757 s = expr_end; 8758 continue; 8759 8760 case 'D': 8761 /* +D is for disassembly only; never match. */ 8762 break; 8763 8764 case 'I': 8765 /* "+I" is like "I", except that imm2_expr is used. */ 8766 my_getExpression (&imm2_expr, s); 8767 if (imm2_expr.X_op != O_big 8768 && imm2_expr.X_op != O_constant) 8769 insn_error = _("absolute expression required"); 8770 if (HAVE_32BIT_GPRS) 8771 normalize_constant_expr (&imm2_expr); 8772 s = expr_end; 8773 continue; 8774 8775 case 'T': /* Coprocessor register */ 8776 /* +T is for disassembly only; never match. */ 8777 break; 8778 8779 case 't': /* Coprocessor register number */ 8780 if (s[0] == '$' && ISDIGIT (s[1])) 8781 { 8782 ++s; 8783 regno = 0; 8784 do 8785 { 8786 regno *= 10; 8787 regno += *s - '0'; 8788 ++s; 8789 } 8790 while (ISDIGIT (*s)); 8791 if (regno > 31) 8792 as_bad (_("Invalid register number (%d)"), regno); 8793 else 8794 { 8795 ip->insn_opcode |= regno << OP_SH_RT; 8796 continue; 8797 } 8798 } 8799 else 8800 as_bad (_("Invalid coprocessor 0 register number")); 8801 break; 8802 8803 default: 8804 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"), 8805 *args, insn->name, insn->args); 8806 /* Further processing is fruitless. */ 8807 return; 8808 } 8809 break; 8810 8811 case '<': /* must be at least one digit */ 8812 /* 8813 * According to the manual, if the shift amount is greater 8814 * than 31 or less than 0, then the shift amount should be 8815 * mod 32. In reality the mips assembler issues an error. 8816 * We issue a warning and mask out all but the low 5 bits. 8817 */ 8818 my_getExpression (&imm_expr, s); 8819 check_absolute_expr (ip, &imm_expr); 8820 if ((unsigned long) imm_expr.X_add_number > 31) 8821 as_warn (_("Improper shift amount (%lu)"), 8822 (unsigned long) imm_expr.X_add_number); 8823 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number); 8824 imm_expr.X_op = O_absent; 8825 s = expr_end; 8826 continue; 8827 8828 case '>': /* shift amount minus 32 */ 8829 my_getExpression (&imm_expr, s); 8830 check_absolute_expr (ip, &imm_expr); 8831 if ((unsigned long) imm_expr.X_add_number < 32 8832 || (unsigned long) imm_expr.X_add_number > 63) 8833 break; 8834 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32); 8835 imm_expr.X_op = O_absent; 8836 s = expr_end; 8837 continue; 8838 8839 case 'k': /* cache code */ 8840 case 'h': /* prefx code */ 8841 my_getExpression (&imm_expr, s); 8842 check_absolute_expr (ip, &imm_expr); 8843 if ((unsigned long) imm_expr.X_add_number > 31) 8844 as_warn (_("Invalid value for `%s' (%lu)"), 8845 ip->insn_mo->name, 8846 (unsigned long) imm_expr.X_add_number); 8847 if (*args == 'k') 8848 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number); 8849 else 8850 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number); 8851 imm_expr.X_op = O_absent; 8852 s = expr_end; 8853 continue; 8854 8855 case 'c': /* break code */ 8856 my_getExpression (&imm_expr, s); 8857 check_absolute_expr (ip, &imm_expr); 8858 if ((unsigned long) imm_expr.X_add_number > 1023) 8859 as_warn (_("Illegal break code (%lu)"), 8860 (unsigned long) imm_expr.X_add_number); 8861 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number); 8862 imm_expr.X_op = O_absent; 8863 s = expr_end; 8864 continue; 8865 8866 case 'q': /* lower break code */ 8867 my_getExpression (&imm_expr, s); 8868 check_absolute_expr (ip, &imm_expr); 8869 if ((unsigned long) imm_expr.X_add_number > 1023) 8870 as_warn (_("Illegal lower break code (%lu)"), 8871 (unsigned long) imm_expr.X_add_number); 8872 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number); 8873 imm_expr.X_op = O_absent; 8874 s = expr_end; 8875 continue; 8876 8877 case 'B': /* 20-bit syscall/break code. */ 8878 my_getExpression (&imm_expr, s); 8879 check_absolute_expr (ip, &imm_expr); 8880 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20) 8881 as_warn (_("Illegal 20-bit code (%lu)"), 8882 (unsigned long) imm_expr.X_add_number); 8883 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number); 8884 imm_expr.X_op = O_absent; 8885 s = expr_end; 8886 continue; 8887 8888 case 'C': /* Coprocessor code */ 8889 my_getExpression (&imm_expr, s); 8890 check_absolute_expr (ip, &imm_expr); 8891 if ((unsigned long) imm_expr.X_add_number >= (1 << 25)) 8892 { 8893 as_warn (_("Coproccesor code > 25 bits (%lu)"), 8894 (unsigned long) imm_expr.X_add_number); 8895 imm_expr.X_add_number &= ((1 << 25) - 1); 8896 } 8897 ip->insn_opcode |= imm_expr.X_add_number; 8898 imm_expr.X_op = O_absent; 8899 s = expr_end; 8900 continue; 8901 8902 case 'J': /* 19-bit wait code. */ 8903 my_getExpression (&imm_expr, s); 8904 check_absolute_expr (ip, &imm_expr); 8905 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19) 8906 as_warn (_("Illegal 19-bit code (%lu)"), 8907 (unsigned long) imm_expr.X_add_number); 8908 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number); 8909 imm_expr.X_op = O_absent; 8910 s = expr_end; 8911 continue; 8912 8913 case 'P': /* Performance register */ 8914 my_getExpression (&imm_expr, s); 8915 check_absolute_expr (ip, &imm_expr); 8916 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1) 8917 as_warn (_("Invalid performance register (%lu)"), 8918 (unsigned long) imm_expr.X_add_number); 8919 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number); 8920 imm_expr.X_op = O_absent; 8921 s = expr_end; 8922 continue; 8923 8924 case 'G': /* Coprocessor destination register. */ 8925 if (((ip->insn_opcode >> OP_SH_OP) & OP_MASK_OP) == OP_OP_COP0) 8926 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_CP0, ®no); 8927 else 8928 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, ®no); 8929 ip->insn_opcode |= regno << OP_SH_RD; 8930 if (ok) 8931 { 8932 lastregno = regno; 8933 continue; 8934 } 8935 else 8936 break; 8937 8938 case 'b': /* base register */ 8939 case 'd': /* destination register */ 8940 case 's': /* source register */ 8941 case 't': /* target register */ 8942 case 'r': /* both target and source */ 8943 case 'v': /* both dest and source */ 8944 case 'w': /* both dest and target */ 8945 case 'E': /* coprocessor target register */ 8946 case 'K': /* 'rdhwr' destination register */ 8947 case 'x': /* ignore register name */ 8948 case 'z': /* must be zero register */ 8949 case 'U': /* destination register (clo/clz). */ 8950 case 'g': /* coprocessor destination register */ 8951 s_reset = s; 8952 if (*args == 'E' || *args == 'K') 8953 ok = reg_lookup (&s, RTYPE_NUM, ®no); 8954 else 8955 { 8956 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, ®no); 8957 if (regno == AT && ! mips_opts.noat) 8958 as_warn ("Used $at without \".set noat\""); 8959 } 8960 if (ok) 8961 { 8962 c = *args; 8963 if (*s == ' ') 8964 ++s; 8965 if (args[1] != *s) 8966 { 8967 if (c == 'r' || c == 'v' || c == 'w') 8968 { 8969 regno = lastregno; 8970 s = s_reset; 8971 ++args; 8972 } 8973 } 8974 /* 'z' only matches $0. */ 8975 if (c == 'z' && regno != 0) 8976 break; 8977 8978 /* Now that we have assembled one operand, we use the args string 8979 * to figure out where it goes in the instruction. */ 8980 switch (c) 8981 { 8982 case 'r': 8983 case 's': 8984 case 'v': 8985 case 'b': 8986 INSERT_OPERAND (RS, *ip, regno); 8987 break; 8988 case 'd': 8989 case 'G': 8990 case 'K': 8991 case 'g': 8992 INSERT_OPERAND (RD, *ip, regno); 8993 break; 8994 case 'U': 8995 INSERT_OPERAND (RD, *ip, regno); 8996 INSERT_OPERAND (RT, *ip, regno); 8997 break; 8998 case 'w': 8999 case 't': 9000 case 'E': 9001 INSERT_OPERAND (RT, *ip, regno); 9002 break; 9003 case 'x': 9004 /* This case exists because on the r3000 trunc 9005 expands into a macro which requires a gp 9006 register. On the r6000 or r4000 it is 9007 assembled into a single instruction which 9008 ignores the register. Thus the insn version 9009 is MIPS_ISA2 and uses 'x', and the macro 9010 version is MIPS_ISA1 and uses 't'. */ 9011 break; 9012 case 'z': 9013 /* This case is for the div instruction, which 9014 acts differently if the destination argument 9015 is $0. This only matches $0, and is checked 9016 outside the switch. */ 9017 break; 9018 case 'D': 9019 /* Itbl operand; not yet implemented. FIXME ?? */ 9020 break; 9021 /* What about all other operands like 'i', which 9022 can be specified in the opcode table? */ 9023 } 9024 lastregno = regno; 9025 continue; 9026 } 9027 switch (*args++) 9028 { 9029 case 'r': 9030 case 'v': 9031 INSERT_OPERAND (RS, *ip, lastregno); 9032 continue; 9033 case 'w': 9034 INSERT_OPERAND (RT, *ip, lastregno); 9035 continue; 9036 } 9037 break; 9038 9039 case 'O': /* MDMX alignment immediate constant. */ 9040 my_getExpression (&imm_expr, s); 9041 check_absolute_expr (ip, &imm_expr); 9042 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN) 9043 as_warn ("Improper align amount (%ld), using low bits", 9044 (long) imm_expr.X_add_number); 9045 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number); 9046 imm_expr.X_op = O_absent; 9047 s = expr_end; 9048 continue; 9049 9050 case 'Q': /* MDMX vector, element sel, or const. */ 9051 if (s[0] != '$') 9052 { 9053 /* MDMX Immediate. */ 9054 my_getExpression (&imm_expr, s); 9055 check_absolute_expr (ip, &imm_expr); 9056 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT) 9057 as_warn (_("Invalid MDMX Immediate (%ld)"), 9058 (long) imm_expr.X_add_number); 9059 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number); 9060 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL)) 9061 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL; 9062 else 9063 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL; 9064 imm_expr.X_op = O_absent; 9065 s = expr_end; 9066 continue; 9067 } 9068 /* Not MDMX Immediate. Fall through. */ 9069 case 'X': /* MDMX destination register. */ 9070 case 'Y': /* MDMX source register. */ 9071 case 'Z': /* MDMX target register. */ 9072 is_mdmx = 1; 9073 case 'D': /* floating point destination register */ 9074 case 'S': /* floating point source register */ 9075 case 'T': /* floating point target register */ 9076 case 'R': /* floating point source register */ 9077 case 'V': 9078 case 'W': 9079 rtype = RTYPE_FPU; 9080 if (is_mdmx 9081 || (mips_opts.ase_mdmx 9082 && (ip->insn_mo->pinfo & FP_D) 9083 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY 9084 | INSN_COPROC_MEMORY_DELAY 9085 | INSN_LOAD_COPROC_DELAY 9086 | INSN_LOAD_MEMORY_DELAY 9087 | INSN_STORE_MEMORY)))) 9088 rtype |= RTYPE_VEC; 9089 s_reset = s; 9090 if (reg_lookup (&s, rtype, ®no)) 9091 { 9092 if ((regno & 1) != 0 9093 && HAVE_32BIT_FPRS 9094 && ! (strcmp (str, "mtc1") == 0 9095 || strcmp (str, "mfc1") == 0 9096 || strcmp (str, "lwc1") == 0 9097 || strcmp (str, "swc1") == 0 9098 || strcmp (str, "l.s") == 0 9099 || strcmp (str, "s.s") == 0 9100 || strcmp (str, "mftc1") == 0 9101 || strcmp (str, "mfthc1") == 0 9102 || strcmp (str, "cftc1") == 0 9103 || strcmp (str, "mttc1") == 0 9104 || strcmp (str, "mtthc1") == 0 9105 || strcmp (str, "cttc1") == 0)) 9106 as_warn (_("Float register should be even, was %d"), 9107 regno); 9108 9109 c = *args; 9110 if (*s == ' ') 9111 ++s; 9112 if (args[1] != *s) 9113 { 9114 if (c == 'V' || c == 'W') 9115 { 9116 regno = lastregno; 9117 s = s_reset; 9118 ++args; 9119 } 9120 } 9121 switch (c) 9122 { 9123 case 'D': 9124 case 'X': 9125 INSERT_OPERAND (FD, *ip, regno); 9126 break; 9127 case 'V': 9128 case 'S': 9129 case 'Y': 9130 INSERT_OPERAND (FS, *ip, regno); 9131 break; 9132 case 'Q': 9133 /* This is like 'Z', but also needs to fix the MDMX 9134 vector/scalar select bits. Note that the 9135 scalar immediate case is handled above. */ 9136 if (*s == '[') 9137 { 9138 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL)); 9139 int max_el = (is_qh ? 3 : 7); 9140 s++; 9141 my_getExpression(&imm_expr, s); 9142 check_absolute_expr (ip, &imm_expr); 9143 s = expr_end; 9144 if (imm_expr.X_add_number > max_el) 9145 as_bad(_("Bad element selector %ld"), 9146 (long) imm_expr.X_add_number); 9147 imm_expr.X_add_number &= max_el; 9148 ip->insn_opcode |= (imm_expr.X_add_number 9149 << (OP_SH_VSEL + 9150 (is_qh ? 2 : 1))); 9151 imm_expr.X_op = O_absent; 9152 if (*s != ']') 9153 as_warn(_("Expecting ']' found '%s'"), s); 9154 else 9155 s++; 9156 } 9157 else 9158 { 9159 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL)) 9160 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH 9161 << OP_SH_VSEL); 9162 else 9163 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB << 9164 OP_SH_VSEL); 9165 } 9166 /* Fall through */ 9167 case 'W': 9168 case 'T': 9169 case 'Z': 9170 INSERT_OPERAND (FT, *ip, regno); 9171 break; 9172 case 'R': 9173 INSERT_OPERAND (FR, *ip, regno); 9174 break; 9175 } 9176 lastregno = regno; 9177 continue; 9178 } 9179 9180 switch (*args++) 9181 { 9182 case 'V': 9183 INSERT_OPERAND (FS, *ip, lastregno); 9184 continue; 9185 case 'W': 9186 INSERT_OPERAND (FT, *ip, lastregno); 9187 continue; 9188 } 9189 break; 9190 9191 case 'I': 9192 my_getExpression (&imm_expr, s); 9193 if (imm_expr.X_op != O_big 9194 && imm_expr.X_op != O_constant) 9195 insn_error = _("absolute expression required"); 9196 if (HAVE_32BIT_GPRS) 9197 normalize_constant_expr (&imm_expr); 9198 s = expr_end; 9199 continue; 9200 9201 case 'A': 9202 my_getExpression (&offset_expr, s); 9203 normalize_address_expr (&offset_expr); 9204 *imm_reloc = BFD_RELOC_32; 9205 s = expr_end; 9206 continue; 9207 9208 case 'F': 9209 case 'L': 9210 case 'f': 9211 case 'l': 9212 { 9213 int f64; 9214 int using_gprs; 9215 char *save_in; 9216 char *err; 9217 unsigned char temp[8]; 9218 int len; 9219 unsigned int length; 9220 segT seg; 9221 subsegT subseg; 9222 char *p; 9223 9224 /* These only appear as the last operand in an 9225 instruction, and every instruction that accepts 9226 them in any variant accepts them in all variants. 9227 This means we don't have to worry about backing out 9228 any changes if the instruction does not match. 9229 9230 The difference between them is the size of the 9231 floating point constant and where it goes. For 'F' 9232 and 'L' the constant is 64 bits; for 'f' and 'l' it 9233 is 32 bits. Where the constant is placed is based 9234 on how the MIPS assembler does things: 9235 F -- .rdata 9236 L -- .lit8 9237 f -- immediate value 9238 l -- .lit4 9239 9240 The .lit4 and .lit8 sections are only used if 9241 permitted by the -G argument. 9242 9243 The code below needs to know whether the target register 9244 is 32 or 64 bits wide. It relies on the fact 'f' and 9245 'F' are used with GPR-based instructions and 'l' and 9246 'L' are used with FPR-based instructions. */ 9247 9248 f64 = *args == 'F' || *args == 'L'; 9249 using_gprs = *args == 'F' || *args == 'f'; 9250 9251 save_in = input_line_pointer; 9252 input_line_pointer = s; 9253 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len); 9254 length = len; 9255 s = input_line_pointer; 9256 input_line_pointer = save_in; 9257 if (err != NULL && *err != '\0') 9258 { 9259 as_bad (_("Bad floating point constant: %s"), err); 9260 memset (temp, '\0', sizeof temp); 9261 length = f64 ? 8 : 4; 9262 } 9263 9264 assert (length == (unsigned) (f64 ? 8 : 4)); 9265 9266 if (*args == 'f' 9267 || (*args == 'l' 9268 && (g_switch_value < 4 9269 || (temp[0] == 0 && temp[1] == 0) 9270 || (temp[2] == 0 && temp[3] == 0)))) 9271 { 9272 imm_expr.X_op = O_constant; 9273 if (! target_big_endian) 9274 imm_expr.X_add_number = bfd_getl32 (temp); 9275 else 9276 imm_expr.X_add_number = bfd_getb32 (temp); 9277 } 9278 else if (length > 4 9279 && ! mips_disable_float_construction 9280 /* Constants can only be constructed in GPRs and 9281 copied to FPRs if the GPRs are at least as wide 9282 as the FPRs. Force the constant into memory if 9283 we are using 64-bit FPRs but the GPRs are only 9284 32 bits wide. */ 9285 && (using_gprs 9286 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS)) 9287 && ((temp[0] == 0 && temp[1] == 0) 9288 || (temp[2] == 0 && temp[3] == 0)) 9289 && ((temp[4] == 0 && temp[5] == 0) 9290 || (temp[6] == 0 && temp[7] == 0))) 9291 { 9292 /* The value is simple enough to load with a couple of 9293 instructions. If using 32-bit registers, set 9294 imm_expr to the high order 32 bits and offset_expr to 9295 the low order 32 bits. Otherwise, set imm_expr to 9296 the entire 64 bit constant. */ 9297 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS) 9298 { 9299 imm_expr.X_op = O_constant; 9300 offset_expr.X_op = O_constant; 9301 if (! target_big_endian) 9302 { 9303 imm_expr.X_add_number = bfd_getl32 (temp + 4); 9304 offset_expr.X_add_number = bfd_getl32 (temp); 9305 } 9306 else 9307 { 9308 imm_expr.X_add_number = bfd_getb32 (temp); 9309 offset_expr.X_add_number = bfd_getb32 (temp + 4); 9310 } 9311 if (offset_expr.X_add_number == 0) 9312 offset_expr.X_op = O_absent; 9313 } 9314 else if (sizeof (imm_expr.X_add_number) > 4) 9315 { 9316 imm_expr.X_op = O_constant; 9317 if (! target_big_endian) 9318 imm_expr.X_add_number = bfd_getl64 (temp); 9319 else 9320 imm_expr.X_add_number = bfd_getb64 (temp); 9321 } 9322 else 9323 { 9324 imm_expr.X_op = O_big; 9325 imm_expr.X_add_number = 4; 9326 if (! target_big_endian) 9327 { 9328 generic_bignum[0] = bfd_getl16 (temp); 9329 generic_bignum[1] = bfd_getl16 (temp + 2); 9330 generic_bignum[2] = bfd_getl16 (temp + 4); 9331 generic_bignum[3] = bfd_getl16 (temp + 6); 9332 } 9333 else 9334 { 9335 generic_bignum[0] = bfd_getb16 (temp + 6); 9336 generic_bignum[1] = bfd_getb16 (temp + 4); 9337 generic_bignum[2] = bfd_getb16 (temp + 2); 9338 generic_bignum[3] = bfd_getb16 (temp); 9339 } 9340 } 9341 } 9342 else 9343 { 9344 const char *newname; 9345 segT new_seg; 9346 9347 /* Switch to the right section. */ 9348 seg = now_seg; 9349 subseg = now_subseg; 9350 switch (*args) 9351 { 9352 default: /* unused default case avoids warnings. */ 9353 case 'L': 9354 newname = RDATA_SECTION_NAME; 9355 if (g_switch_value >= 8) 9356 newname = ".lit8"; 9357 break; 9358 case 'F': 9359 newname = RDATA_SECTION_NAME; 9360 break; 9361 case 'l': 9362 assert (g_switch_value >= 4); 9363 newname = ".lit4"; 9364 break; 9365 } 9366 new_seg = subseg_new (newname, (subsegT) 0); 9367 if (OUTPUT_FLAVOR == bfd_target_elf_flavour) 9368 bfd_set_section_flags (stdoutput, new_seg, 9369 (SEC_ALLOC 9370 | SEC_LOAD 9371 | SEC_READONLY 9372 | SEC_DATA)); 9373 frag_align (*args == 'l' ? 2 : 3, 0, 0); 9374 if (OUTPUT_FLAVOR == bfd_target_elf_flavour 9375 && strcmp (TARGET_OS, "elf") != 0) 9376 record_alignment (new_seg, 4); 9377 else 9378 record_alignment (new_seg, *args == 'l' ? 2 : 3); 9379 if (seg == now_seg) 9380 as_bad (_("Can't use floating point insn in this section")); 9381 9382 /* Set the argument to the current address in the 9383 section. */ 9384 offset_expr.X_op = O_symbol; 9385 offset_expr.X_add_symbol = 9386 symbol_new ("L0\001", now_seg, 9387 (valueT) frag_now_fix (), frag_now); 9388 offset_expr.X_add_number = 0; 9389 9390 /* Put the floating point number into the section. */ 9391 p = frag_more ((int) length); 9392 memcpy (p, temp, length); 9393 9394 /* Switch back to the original section. */ 9395 subseg_set (seg, subseg); 9396 } 9397 } 9398 continue; 9399 9400 case 'i': /* 16 bit unsigned immediate */ 9401 case 'j': /* 16 bit signed immediate */ 9402 *imm_reloc = BFD_RELOC_LO16; 9403 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0) 9404 { 9405 int more; 9406 offsetT minval, maxval; 9407 9408 more = (insn + 1 < &mips_opcodes[NUMOPCODES] 9409 && strcmp (insn->name, insn[1].name) == 0); 9410 9411 /* If the expression was written as an unsigned number, 9412 only treat it as signed if there are no more 9413 alternatives. */ 9414 if (more 9415 && *args == 'j' 9416 && sizeof (imm_expr.X_add_number) <= 4 9417 && imm_expr.X_op == O_constant 9418 && imm_expr.X_add_number < 0 9419 && imm_expr.X_unsigned 9420 && HAVE_64BIT_GPRS) 9421 break; 9422 9423 /* For compatibility with older assemblers, we accept 9424 0x8000-0xffff as signed 16-bit numbers when only 9425 signed numbers are allowed. */ 9426 if (*args == 'i') 9427 minval = 0, maxval = 0xffff; 9428 else if (more) 9429 minval = -0x8000, maxval = 0x7fff; 9430 else 9431 minval = -0x8000, maxval = 0xffff; 9432 9433 if (imm_expr.X_op != O_constant 9434 || imm_expr.X_add_number < minval 9435 || imm_expr.X_add_number > maxval) 9436 { 9437 if (more) 9438 break; 9439 if (imm_expr.X_op == O_constant 9440 || imm_expr.X_op == O_big) 9441 as_bad (_("expression out of range")); 9442 } 9443 } 9444 s = expr_end; 9445 continue; 9446 9447 case 'o': /* 16 bit offset */ 9448 /* Check whether there is only a single bracketed expression 9449 left. If so, it must be the base register and the 9450 constant must be zero. */ 9451 if (*s == '(' && strchr (s + 1, '(') == 0) 9452 { 9453 offset_expr.X_op = O_constant; 9454 offset_expr.X_add_number = 0; 9455 continue; 9456 } 9457 9458 /* If this value won't fit into a 16 bit offset, then go 9459 find a macro that will generate the 32 bit offset 9460 code pattern. */ 9461 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0 9462 && (offset_expr.X_op != O_constant 9463 || offset_expr.X_add_number >= 0x8000 9464 || offset_expr.X_add_number < -0x8000)) 9465 break; 9466 9467 s = expr_end; 9468 continue; 9469 9470 case 'p': /* pc relative offset */ 9471 *offset_reloc = BFD_RELOC_16_PCREL_S2; 9472 my_getExpression (&offset_expr, s); 9473 s = expr_end; 9474 continue; 9475 9476 case 'u': /* upper 16 bits */ 9477 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0 9478 && imm_expr.X_op == O_constant 9479 && (imm_expr.X_add_number < 0 9480 || imm_expr.X_add_number >= 0x10000)) 9481 as_bad (_("lui expression not in range 0..65535")); 9482 s = expr_end; 9483 continue; 9484 9485 case 'a': /* 26 bit address */ 9486 my_getExpression (&offset_expr, s); 9487 s = expr_end; 9488 *offset_reloc = BFD_RELOC_MIPS_JMP; 9489 continue; 9490 9491 case 'N': /* 3 bit branch condition code */ 9492 case 'M': /* 3 bit compare condition code */ 9493 rtype = RTYPE_CCC; 9494 if (ip->insn_mo->pinfo & (FP_D| FP_S)) 9495 rtype |= RTYPE_FCC; 9496 if (!reg_lookup (&s, rtype, ®no)) 9497 break; 9498 if ((strcmp(str + strlen(str) - 3, ".ps") == 0 9499 || strcmp(str + strlen(str) - 5, "any2f") == 0 9500 || strcmp(str + strlen(str) - 5, "any2t") == 0) 9501 && (regno & 1) != 0) 9502 as_warn(_("Condition code register should be even for %s, was %d"), 9503 str, regno); 9504 if ((strcmp(str + strlen(str) - 5, "any4f") == 0 9505 || strcmp(str + strlen(str) - 5, "any4t") == 0) 9506 && (regno & 3) != 0) 9507 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"), 9508 str, regno); 9509 if (*args == 'N') 9510 INSERT_OPERAND (BCC, *ip, regno); 9511 else 9512 INSERT_OPERAND (CCC, *ip, regno); 9513 continue; 9514 9515 case 'H': 9516 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) 9517 s += 2; 9518 if (ISDIGIT (*s)) 9519 { 9520 c = 0; 9521 do 9522 { 9523 c *= 10; 9524 c += *s - '0'; 9525 ++s; 9526 } 9527 while (ISDIGIT (*s)); 9528 } 9529 else 9530 c = 8; /* Invalid sel value. */ 9531 9532 if (c > 7) 9533 as_bad (_("invalid coprocessor sub-selection value (0-7)")); 9534 ip->insn_opcode |= c; 9535 continue; 9536 9537 case 'e': 9538 /* Must be at least one digit. */ 9539 my_getExpression (&imm_expr, s); 9540 check_absolute_expr (ip, &imm_expr); 9541 9542 if ((unsigned long) imm_expr.X_add_number 9543 > (unsigned long) OP_MASK_VECBYTE) 9544 { 9545 as_bad (_("bad byte vector index (%ld)"), 9546 (long) imm_expr.X_add_number); 9547 imm_expr.X_add_number = 0; 9548 } 9549 9550 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number); 9551 imm_expr.X_op = O_absent; 9552 s = expr_end; 9553 continue; 9554 9555 case '%': 9556 my_getExpression (&imm_expr, s); 9557 check_absolute_expr (ip, &imm_expr); 9558 9559 if ((unsigned long) imm_expr.X_add_number 9560 > (unsigned long) OP_MASK_VECALIGN) 9561 { 9562 as_bad (_("bad byte vector index (%ld)"), 9563 (long) imm_expr.X_add_number); 9564 imm_expr.X_add_number = 0; 9565 } 9566 9567 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number); 9568 imm_expr.X_op = O_absent; 9569 s = expr_end; 9570 continue; 9571 9572 default: 9573 as_bad (_("bad char = '%c'\n"), *args); 9574 internalError (); 9575 } 9576 break; 9577 } 9578 /* Args don't match. */ 9579 if (insn + 1 < &mips_opcodes[NUMOPCODES] && 9580 !strcmp (insn->name, insn[1].name)) 9581 { 9582 ++insn; 9583 s = argsStart; 9584 insn_error = _("illegal operands"); 9585 continue; 9586 } 9587 if (save_c) 9588 *(--s) = save_c; 9589 insn_error = _("illegal operands"); 9590 return; 9591 } 9592 } 9593 9594 #define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); } 9595 9596 /* This routine assembles an instruction into its binary format when 9597 assembling for the mips16. As a side effect, it sets one of the 9598 global variables imm_reloc or offset_reloc to the type of 9599 relocation to do if one of the operands is an address expression. 9600 It also sets mips16_small and mips16_ext if the user explicitly 9601 requested a small or extended instruction. */ 9602 9603 static void 9604 mips16_ip (char *str, struct mips_cl_insn *ip) 9605 { 9606 char *s; 9607 const char *args; 9608 struct mips_opcode *insn; 9609 char *argsstart; 9610 unsigned int regno; 9611 unsigned int lastregno = 0; 9612 char *s_reset; 9613 size_t i; 9614 9615 insn_error = NULL; 9616 9617 mips16_small = FALSE; 9618 mips16_ext = FALSE; 9619 9620 for (s = str; ISLOWER (*s); ++s) 9621 ; 9622 switch (*s) 9623 { 9624 case '\0': 9625 break; 9626 9627 case ' ': 9628 *s++ = '\0'; 9629 break; 9630 9631 case '.': 9632 if (s[1] == 't' && s[2] == ' ') 9633 { 9634 *s = '\0'; 9635 mips16_small = TRUE; 9636 s += 3; 9637 break; 9638 } 9639 else if (s[1] == 'e' && s[2] == ' ') 9640 { 9641 *s = '\0'; 9642 mips16_ext = TRUE; 9643 s += 3; 9644 break; 9645 } 9646 /* Fall through. */ 9647 default: 9648 insn_error = _("unknown opcode"); 9649 return; 9650 } 9651 9652 if (mips_opts.noautoextend && ! mips16_ext) 9653 mips16_small = TRUE; 9654 9655 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL) 9656 { 9657 insn_error = _("unrecognized opcode"); 9658 return; 9659 } 9660 9661 argsstart = s; 9662 for (;;) 9663 { 9664 assert (strcmp (insn->name, str) == 0); 9665 9666 create_insn (ip, insn); 9667 imm_expr.X_op = O_absent; 9668 imm_reloc[0] = BFD_RELOC_UNUSED; 9669 imm_reloc[1] = BFD_RELOC_UNUSED; 9670 imm_reloc[2] = BFD_RELOC_UNUSED; 9671 imm2_expr.X_op = O_absent; 9672 offset_expr.X_op = O_absent; 9673 offset_reloc[0] = BFD_RELOC_UNUSED; 9674 offset_reloc[1] = BFD_RELOC_UNUSED; 9675 offset_reloc[2] = BFD_RELOC_UNUSED; 9676 for (args = insn->args; 1; ++args) 9677 { 9678 int c; 9679 9680 if (*s == ' ') 9681 ++s; 9682 9683 /* In this switch statement we call break if we did not find 9684 a match, continue if we did find a match, or return if we 9685 are done. */ 9686 9687 c = *args; 9688 switch (c) 9689 { 9690 case '\0': 9691 if (*s == '\0') 9692 { 9693 /* Stuff the immediate value in now, if we can. */ 9694 if (imm_expr.X_op == O_constant 9695 && *imm_reloc > BFD_RELOC_UNUSED 9696 && insn->pinfo != INSN_MACRO) 9697 { 9698 valueT tmp; 9699 9700 switch (*offset_reloc) 9701 { 9702 case BFD_RELOC_MIPS16_HI16_S: 9703 tmp = (imm_expr.X_add_number + 0x8000) >> 16; 9704 break; 9705 9706 case BFD_RELOC_MIPS16_HI16: 9707 tmp = imm_expr.X_add_number >> 16; 9708 break; 9709 9710 case BFD_RELOC_MIPS16_LO16: 9711 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff) 9712 - 0x8000; 9713 break; 9714 9715 case BFD_RELOC_UNUSED: 9716 tmp = imm_expr.X_add_number; 9717 break; 9718 9719 default: 9720 internalError (); 9721 } 9722 *offset_reloc = BFD_RELOC_UNUSED; 9723 9724 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED, 9725 tmp, TRUE, mips16_small, 9726 mips16_ext, &ip->insn_opcode, 9727 &ip->use_extend, &ip->extend); 9728 imm_expr.X_op = O_absent; 9729 *imm_reloc = BFD_RELOC_UNUSED; 9730 } 9731 9732 return; 9733 } 9734 break; 9735 9736 case ',': 9737 if (*s++ == c) 9738 continue; 9739 s--; 9740 switch (*++args) 9741 { 9742 case 'v': 9743 MIPS16_INSERT_OPERAND (RX, *ip, lastregno); 9744 continue; 9745 case 'w': 9746 MIPS16_INSERT_OPERAND (RY, *ip, lastregno); 9747 continue; 9748 } 9749 break; 9750 9751 case '(': 9752 case ')': 9753 if (*s++ == c) 9754 continue; 9755 break; 9756 9757 case 'v': 9758 case 'w': 9759 if (s[0] != '$') 9760 { 9761 if (c == 'v') 9762 MIPS16_INSERT_OPERAND (RX, *ip, lastregno); 9763 else 9764 MIPS16_INSERT_OPERAND (RY, *ip, lastregno); 9765 ++args; 9766 continue; 9767 } 9768 /* Fall through. */ 9769 case 'x': 9770 case 'y': 9771 case 'z': 9772 case 'Z': 9773 case '0': 9774 case 'S': 9775 case 'R': 9776 case 'X': 9777 case 'Y': 9778 s_reset = s; 9779 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, ®no)) 9780 { 9781 if (c == 'v' || c == 'w') 9782 { 9783 if (c == 'v') 9784 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX; 9785 else 9786 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY; 9787 ++args; 9788 continue; 9789 } 9790 break; 9791 } 9792 9793 if (*s == ' ') 9794 ++s; 9795 if (args[1] != *s) 9796 { 9797 if (c == 'v' || c == 'w') 9798 { 9799 regno = mips16_to_32_reg_map[lastregno]; 9800 s = s_reset; 9801 ++args; 9802 } 9803 } 9804 9805 switch (c) 9806 { 9807 case 'x': 9808 case 'y': 9809 case 'z': 9810 case 'v': 9811 case 'w': 9812 case 'Z': 9813 regno = mips32_to_16_reg_map[regno]; 9814 break; 9815 9816 case '0': 9817 if (regno != 0) 9818 regno = ILLEGAL_REG; 9819 break; 9820 9821 case 'S': 9822 if (regno != SP) 9823 regno = ILLEGAL_REG; 9824 break; 9825 9826 case 'R': 9827 if (regno != RA) 9828 regno = ILLEGAL_REG; 9829 break; 9830 9831 case 'X': 9832 case 'Y': 9833 if (regno == AT && ! mips_opts.noat) 9834 as_warn (_("used $at without \".set noat\"")); 9835 break; 9836 9837 default: 9838 internalError (); 9839 } 9840 9841 if (regno == ILLEGAL_REG) 9842 break; 9843 9844 switch (c) 9845 { 9846 case 'x': 9847 case 'v': 9848 MIPS16_INSERT_OPERAND (RX, *ip, regno); 9849 break; 9850 case 'y': 9851 case 'w': 9852 MIPS16_INSERT_OPERAND (RY, *ip, regno); 9853 break; 9854 case 'z': 9855 MIPS16_INSERT_OPERAND (RZ, *ip, regno); 9856 break; 9857 case 'Z': 9858 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno); 9859 case '0': 9860 case 'S': 9861 case 'R': 9862 break; 9863 case 'X': 9864 MIPS16_INSERT_OPERAND (REGR32, *ip, regno); 9865 break; 9866 case 'Y': 9867 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3); 9868 MIPS16_INSERT_OPERAND (REG32R, *ip, regno); 9869 break; 9870 default: 9871 internalError (); 9872 } 9873 9874 lastregno = regno; 9875 continue; 9876 9877 case 'P': 9878 if (strncmp (s, "$pc", 3) == 0) 9879 { 9880 s += 3; 9881 continue; 9882 } 9883 break; 9884 9885 case '5': 9886 case 'H': 9887 case 'W': 9888 case 'D': 9889 case 'j': 9890 case 'V': 9891 case 'C': 9892 case 'U': 9893 case 'k': 9894 case 'K': 9895 i = my_getSmallExpression (&imm_expr, imm_reloc, s); 9896 if (i > 0) 9897 { 9898 if (imm_expr.X_op != O_constant) 9899 { 9900 mips16_ext = TRUE; 9901 ip->use_extend = TRUE; 9902 ip->extend = 0; 9903 } 9904 else 9905 { 9906 /* We need to relax this instruction. */ 9907 *offset_reloc = *imm_reloc; 9908 *imm_reloc = (int) BFD_RELOC_UNUSED + c; 9909 } 9910 s = expr_end; 9911 continue; 9912 } 9913 *imm_reloc = BFD_RELOC_UNUSED; 9914 /* Fall through. */ 9915 case '<': 9916 case '>': 9917 case '[': 9918 case ']': 9919 case '4': 9920 case '8': 9921 my_getExpression (&imm_expr, s); 9922 if (imm_expr.X_op == O_register) 9923 { 9924 /* What we thought was an expression turned out to 9925 be a register. */ 9926 9927 if (s[0] == '(' && args[1] == '(') 9928 { 9929 /* It looks like the expression was omitted 9930 before a register indirection, which means 9931 that the expression is implicitly zero. We 9932 still set up imm_expr, so that we handle 9933 explicit extensions correctly. */ 9934 imm_expr.X_op = O_constant; 9935 imm_expr.X_add_number = 0; 9936 *imm_reloc = (int) BFD_RELOC_UNUSED + c; 9937 continue; 9938 } 9939 9940 break; 9941 } 9942 9943 /* We need to relax this instruction. */ 9944 *imm_reloc = (int) BFD_RELOC_UNUSED + c; 9945 s = expr_end; 9946 continue; 9947 9948 case 'p': 9949 case 'q': 9950 case 'A': 9951 case 'B': 9952 case 'E': 9953 /* We use offset_reloc rather than imm_reloc for the PC 9954 relative operands. This lets macros with both 9955 immediate and address operands work correctly. */ 9956 my_getExpression (&offset_expr, s); 9957 9958 if (offset_expr.X_op == O_register) 9959 break; 9960 9961 /* We need to relax this instruction. */ 9962 *offset_reloc = (int) BFD_RELOC_UNUSED + c; 9963 s = expr_end; 9964 continue; 9965 9966 case '6': /* break code */ 9967 my_getExpression (&imm_expr, s); 9968 check_absolute_expr (ip, &imm_expr); 9969 if ((unsigned long) imm_expr.X_add_number > 63) 9970 as_warn (_("Invalid value for `%s' (%lu)"), 9971 ip->insn_mo->name, 9972 (unsigned long) imm_expr.X_add_number); 9973 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number); 9974 imm_expr.X_op = O_absent; 9975 s = expr_end; 9976 continue; 9977 9978 case 'a': /* 26 bit address */ 9979 my_getExpression (&offset_expr, s); 9980 s = expr_end; 9981 *offset_reloc = BFD_RELOC_MIPS16_JMP; 9982 ip->insn_opcode <<= 16; 9983 continue; 9984 9985 case 'l': /* register list for entry macro */ 9986 case 'L': /* register list for exit macro */ 9987 { 9988 int mask; 9989 9990 if (c == 'l') 9991 mask = 0; 9992 else 9993 mask = 7 << 3; 9994 while (*s != '\0') 9995 { 9996 unsigned int freg, reg1, reg2; 9997 9998 while (*s == ' ' || *s == ',') 9999 ++s; 10000 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, ®1)) 10001 freg = 0; 10002 else if (reg_lookup (&s, RTYPE_FPU, ®1)) 10003 freg = 1; 10004 else 10005 { 10006 as_bad (_("can't parse register list")); 10007 break; 10008 } 10009 if (*s == ' ') 10010 ++s; 10011 if (*s != '-') 10012 reg2 = reg1; 10013 else 10014 { 10015 ++s; 10016 if (!reg_lookup (&s, freg ? RTYPE_FPU 10017 : (RTYPE_GP | RTYPE_NUM), ®2)) 10018 { 10019 as_bad (_("invalid register list")); 10020 break; 10021 } 10022 } 10023 if (freg && reg1 == 0 && reg2 == 0 && c == 'L') 10024 { 10025 mask &= ~ (7 << 3); 10026 mask |= 5 << 3; 10027 } 10028 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L') 10029 { 10030 mask &= ~ (7 << 3); 10031 mask |= 6 << 3; 10032 } 10033 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L') 10034 mask |= (reg2 - 3) << 3; 10035 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17) 10036 mask |= (reg2 - 15) << 1; 10037 else if (reg1 == RA && reg2 == RA) 10038 mask |= 1; 10039 else 10040 { 10041 as_bad (_("invalid register list")); 10042 break; 10043 } 10044 } 10045 /* The mask is filled in in the opcode table for the 10046 benefit of the disassembler. We remove it before 10047 applying the actual mask. */ 10048 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6); 10049 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6; 10050 } 10051 continue; 10052 10053 case 'm': /* Register list for save insn. */ 10054 case 'M': /* Register list for restore insn. */ 10055 { 10056 int opcode = 0; 10057 int framesz = 0, seen_framesz = 0; 10058 int args = 0, statics = 0, sregs = 0; 10059 10060 while (*s != '\0') 10061 { 10062 unsigned int reg1, reg2; 10063 10064 SKIP_SPACE_TABS (s); 10065 while (*s == ',') 10066 ++s; 10067 SKIP_SPACE_TABS (s); 10068 10069 my_getExpression (&imm_expr, s); 10070 if (imm_expr.X_op == O_constant) 10071 { 10072 /* Handle the frame size. */ 10073 if (seen_framesz) 10074 { 10075 as_bad (_("more than one frame size in list")); 10076 break; 10077 } 10078 seen_framesz = 1; 10079 framesz = imm_expr.X_add_number; 10080 imm_expr.X_op = O_absent; 10081 s = expr_end; 10082 continue; 10083 } 10084 10085 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, ®1)) 10086 { 10087 as_bad (_("can't parse register list")); 10088 break; 10089 } 10090 10091 while (*s == ' ') 10092 ++s; 10093 10094 if (*s != '-') 10095 reg2 = reg1; 10096 else 10097 { 10098 ++s; 10099 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, ®2) 10100 || reg2 < reg1) 10101 { 10102 as_bad (_("can't parse register list")); 10103 break; 10104 } 10105 ++s; 10106 reg2 = 0; 10107 while (ISDIGIT (*s)) 10108 { 10109 reg2 *= 10; 10110 reg2 += *s - '0'; 10111 ++s; 10112 } 10113 } 10114 10115 while (reg1 <= reg2) 10116 { 10117 if (reg1 >= 4 && reg1 <= 7) 10118 { 10119 if (c == 'm' && !seen_framesz) 10120 /* args $a0-$a3 */ 10121 args |= 1 << (reg1 - 4); 10122 else 10123 /* statics $a0-$a3 */ 10124 statics |= 1 << (reg1 - 4); 10125 } 10126 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30) 10127 { 10128 /* $s0-$s8 */ 10129 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16)); 10130 } 10131 else if (reg1 == 31) 10132 { 10133 /* Add $ra to insn. */ 10134 opcode |= 0x40; 10135 } 10136 else 10137 { 10138 as_bad (_("unexpected register in list")); 10139 break; 10140 } 10141 if (++reg1 == 24) 10142 reg1 = 30; 10143 } 10144 } 10145 10146 /* Encode args/statics combination. */ 10147 if (args & statics) 10148 as_bad (_("arg/static registers overlap")); 10149 else if (args == 0xf) 10150 /* All $a0-$a3 are args. */ 10151 opcode |= MIPS16_ALL_ARGS << 16; 10152 else if (statics == 0xf) 10153 /* All $a0-$a3 are statics. */ 10154 opcode |= MIPS16_ALL_STATICS << 16; 10155 else 10156 { 10157 int narg = 0, nstat = 0; 10158 10159 /* Count arg registers. */ 10160 while (args & 0x1) 10161 { 10162 args >>= 1; 10163 narg++; 10164 } 10165 if (args != 0) 10166 as_bad (_("invalid arg register list")); 10167 10168 /* Count static registers. */ 10169 while (statics & 0x8) 10170 { 10171 statics = (statics << 1) & 0xf; 10172 nstat++; 10173 } 10174 if (statics != 0) 10175 as_bad (_("invalid static register list")); 10176 10177 /* Encode args/statics. */ 10178 opcode |= ((narg << 2) | nstat) << 16; 10179 } 10180 10181 /* Encode $s0/$s1. */ 10182 if (sregs & (1 << 0)) /* $s0 */ 10183 opcode |= 0x20; 10184 if (sregs & (1 << 1)) /* $s1 */ 10185 opcode |= 0x10; 10186 sregs >>= 2; 10187 10188 if (sregs != 0) 10189 { 10190 /* Count regs $s2-$s8. */ 10191 int nsreg = 0; 10192 while (sregs & 1) 10193 { 10194 sregs >>= 1; 10195 nsreg++; 10196 } 10197 if (sregs != 0) 10198 as_bad (_("invalid static register list")); 10199 /* Encode $s2-$s8. */ 10200 opcode |= nsreg << 24; 10201 } 10202 10203 /* Encode frame size. */ 10204 if (!seen_framesz) 10205 as_bad (_("missing frame size")); 10206 else if ((framesz & 7) != 0 || framesz < 0 10207 || framesz > 0xff * 8) 10208 as_bad (_("invalid frame size")); 10209 else if (framesz != 128 || (opcode >> 16) != 0) 10210 { 10211 framesz /= 8; 10212 opcode |= (((framesz & 0xf0) << 16) 10213 | (framesz & 0x0f)); 10214 } 10215 10216 /* Finally build the instruction. */ 10217 if ((opcode >> 16) != 0 || framesz == 0) 10218 { 10219 ip->use_extend = TRUE; 10220 ip->extend = opcode >> 16; 10221 } 10222 ip->insn_opcode |= opcode & 0x7f; 10223 } 10224 continue; 10225 10226 case 'e': /* extend code */ 10227 my_getExpression (&imm_expr, s); 10228 check_absolute_expr (ip, &imm_expr); 10229 if ((unsigned long) imm_expr.X_add_number > 0x7ff) 10230 { 10231 as_warn (_("Invalid value for `%s' (%lu)"), 10232 ip->insn_mo->name, 10233 (unsigned long) imm_expr.X_add_number); 10234 imm_expr.X_add_number &= 0x7ff; 10235 } 10236 ip->insn_opcode |= imm_expr.X_add_number; 10237 imm_expr.X_op = O_absent; 10238 s = expr_end; 10239 continue; 10240 10241 default: 10242 internalError (); 10243 } 10244 break; 10245 } 10246 10247 /* Args don't match. */ 10248 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] && 10249 strcmp (insn->name, insn[1].name) == 0) 10250 { 10251 ++insn; 10252 s = argsstart; 10253 continue; 10254 } 10255 10256 insn_error = _("illegal operands"); 10257 10258 return; 10259 } 10260 } 10261 10262 /* This structure holds information we know about a mips16 immediate 10263 argument type. */ 10264 10265 struct mips16_immed_operand 10266 { 10267 /* The type code used in the argument string in the opcode table. */ 10268 int type; 10269 /* The number of bits in the short form of the opcode. */ 10270 int nbits; 10271 /* The number of bits in the extended form of the opcode. */ 10272 int extbits; 10273 /* The amount by which the short form is shifted when it is used; 10274 for example, the sw instruction has a shift count of 2. */ 10275 int shift; 10276 /* The amount by which the short form is shifted when it is stored 10277 into the instruction code. */ 10278 int op_shift; 10279 /* Non-zero if the short form is unsigned. */ 10280 int unsp; 10281 /* Non-zero if the extended form is unsigned. */ 10282 int extu; 10283 /* Non-zero if the value is PC relative. */ 10284 int pcrel; 10285 }; 10286 10287 /* The mips16 immediate operand types. */ 10288 10289 static const struct mips16_immed_operand mips16_immed_operands[] = 10290 { 10291 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 }, 10292 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 }, 10293 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 }, 10294 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 }, 10295 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 }, 10296 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 }, 10297 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 }, 10298 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 }, 10299 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 }, 10300 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 }, 10301 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 }, 10302 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 }, 10303 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 }, 10304 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 }, 10305 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 }, 10306 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 }, 10307 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 }, 10308 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 }, 10309 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 }, 10310 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 }, 10311 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 } 10312 }; 10313 10314 #define MIPS16_NUM_IMMED \ 10315 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0]) 10316 10317 /* Handle a mips16 instruction with an immediate value. This or's the 10318 small immediate value into *INSN. It sets *USE_EXTEND to indicate 10319 whether an extended value is needed; if one is needed, it sets 10320 *EXTEND to the value. The argument type is TYPE. The value is VAL. 10321 If SMALL is true, an unextended opcode was explicitly requested. 10322 If EXT is true, an extended opcode was explicitly requested. If 10323 WARN is true, warn if EXT does not match reality. */ 10324 10325 static void 10326 mips16_immed (char *file, unsigned int line, int type, offsetT val, 10327 bfd_boolean warn, bfd_boolean small, bfd_boolean ext, 10328 unsigned long *insn, bfd_boolean *use_extend, 10329 unsigned short *extend) 10330 { 10331 register const struct mips16_immed_operand *op; 10332 int mintiny, maxtiny; 10333 bfd_boolean needext; 10334 10335 op = mips16_immed_operands; 10336 while (op->type != type) 10337 { 10338 ++op; 10339 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED); 10340 } 10341 10342 if (op->unsp) 10343 { 10344 if (type == '<' || type == '>' || type == '[' || type == ']') 10345 { 10346 mintiny = 1; 10347 maxtiny = 1 << op->nbits; 10348 } 10349 else 10350 { 10351 mintiny = 0; 10352 maxtiny = (1 << op->nbits) - 1; 10353 } 10354 } 10355 else 10356 { 10357 mintiny = - (1 << (op->nbits - 1)); 10358 maxtiny = (1 << (op->nbits - 1)) - 1; 10359 } 10360 10361 /* Branch offsets have an implicit 0 in the lowest bit. */ 10362 if (type == 'p' || type == 'q') 10363 val /= 2; 10364 10365 if ((val & ((1 << op->shift) - 1)) != 0 10366 || val < (mintiny << op->shift) 10367 || val > (maxtiny << op->shift)) 10368 needext = TRUE; 10369 else 10370 needext = FALSE; 10371 10372 if (warn && ext && ! needext) 10373 as_warn_where (file, line, 10374 _("extended operand requested but not required")); 10375 if (small && needext) 10376 as_bad_where (file, line, _("invalid unextended operand value")); 10377 10378 if (small || (! ext && ! needext)) 10379 { 10380 int insnval; 10381 10382 *use_extend = FALSE; 10383 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1)); 10384 insnval <<= op->op_shift; 10385 *insn |= insnval; 10386 } 10387 else 10388 { 10389 long minext, maxext; 10390 int extval; 10391 10392 if (op->extu) 10393 { 10394 minext = 0; 10395 maxext = (1 << op->extbits) - 1; 10396 } 10397 else 10398 { 10399 minext = - (1 << (op->extbits - 1)); 10400 maxext = (1 << (op->extbits - 1)) - 1; 10401 } 10402 if (val < minext || val > maxext) 10403 as_bad_where (file, line, 10404 _("operand value out of range for instruction")); 10405 10406 *use_extend = TRUE; 10407 if (op->extbits == 16) 10408 { 10409 extval = ((val >> 11) & 0x1f) | (val & 0x7e0); 10410 val &= 0x1f; 10411 } 10412 else if (op->extbits == 15) 10413 { 10414 extval = ((val >> 11) & 0xf) | (val & 0x7f0); 10415 val &= 0xf; 10416 } 10417 else 10418 { 10419 extval = ((val & 0x1f) << 6) | (val & 0x20); 10420 val = 0; 10421 } 10422 10423 *extend = (unsigned short) extval; 10424 *insn |= val; 10425 } 10426 } 10427 10428 struct percent_op_match 10429 { 10430 const char *str; 10431 bfd_reloc_code_real_type reloc; 10432 }; 10433 10434 static const struct percent_op_match mips_percent_op[] = 10435 { 10436 {"%lo", BFD_RELOC_LO16}, 10437 #ifdef OBJ_ELF 10438 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16}, 10439 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16}, 10440 {"%call16", BFD_RELOC_MIPS_CALL16}, 10441 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP}, 10442 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE}, 10443 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST}, 10444 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16}, 10445 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16}, 10446 {"%got", BFD_RELOC_MIPS_GOT16}, 10447 {"%gp_rel", BFD_RELOC_GPREL16}, 10448 {"%half", BFD_RELOC_16}, 10449 {"%highest", BFD_RELOC_MIPS_HIGHEST}, 10450 {"%higher", BFD_RELOC_MIPS_HIGHER}, 10451 {"%neg", BFD_RELOC_MIPS_SUB}, 10452 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD}, 10453 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM}, 10454 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16}, 10455 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16}, 10456 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16}, 10457 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16}, 10458 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL}, 10459 #endif 10460 {"%hi", BFD_RELOC_HI16_S} 10461 }; 10462 10463 static const struct percent_op_match mips16_percent_op[] = 10464 { 10465 {"%lo", BFD_RELOC_MIPS16_LO16}, 10466 {"%gprel", BFD_RELOC_MIPS16_GPREL}, 10467 {"%hi", BFD_RELOC_MIPS16_HI16_S} 10468 }; 10469 10470 10471 /* Return true if *STR points to a relocation operator. When returning true, 10472 move *STR over the operator and store its relocation code in *RELOC. 10473 Leave both *STR and *RELOC alone when returning false. */ 10474 10475 static bfd_boolean 10476 parse_relocation (char **str, bfd_reloc_code_real_type *reloc) 10477 { 10478 const struct percent_op_match *percent_op; 10479 size_t limit, i; 10480 10481 if (mips_opts.mips16) 10482 { 10483 percent_op = mips16_percent_op; 10484 limit = ARRAY_SIZE (mips16_percent_op); 10485 } 10486 else 10487 { 10488 percent_op = mips_percent_op; 10489 limit = ARRAY_SIZE (mips_percent_op); 10490 } 10491 10492 for (i = 0; i < limit; i++) 10493 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0) 10494 { 10495 int len = strlen (percent_op[i].str); 10496 10497 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(') 10498 continue; 10499 10500 *str += strlen (percent_op[i].str); 10501 *reloc = percent_op[i].reloc; 10502 10503 /* Check whether the output BFD supports this relocation. 10504 If not, issue an error and fall back on something safe. */ 10505 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc)) 10506 { 10507 as_bad ("relocation %s isn't supported by the current ABI", 10508 percent_op[i].str); 10509 *reloc = BFD_RELOC_UNUSED; 10510 } 10511 return TRUE; 10512 } 10513 return FALSE; 10514 } 10515 10516 10517 /* Parse string STR as a 16-bit relocatable operand. Store the 10518 expression in *EP and the relocations in the array starting 10519 at RELOC. Return the number of relocation operators used. 10520 10521 On exit, EXPR_END points to the first character after the expression. */ 10522 10523 static size_t 10524 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc, 10525 char *str) 10526 { 10527 bfd_reloc_code_real_type reversed_reloc[3]; 10528 size_t reloc_index, i; 10529 int crux_depth, str_depth; 10530 char *crux; 10531 10532 /* Search for the start of the main expression, recoding relocations 10533 in REVERSED_RELOC. End the loop with CRUX pointing to the start 10534 of the main expression and with CRUX_DEPTH containing the number 10535 of open brackets at that point. */ 10536 reloc_index = -1; 10537 str_depth = 0; 10538 do 10539 { 10540 reloc_index++; 10541 crux = str; 10542 crux_depth = str_depth; 10543 10544 /* Skip over whitespace and brackets, keeping count of the number 10545 of brackets. */ 10546 while (*str == ' ' || *str == '\t' || *str == '(') 10547 if (*str++ == '(') 10548 str_depth++; 10549 } 10550 while (*str == '%' 10551 && reloc_index < (HAVE_NEWABI ? 3 : 1) 10552 && parse_relocation (&str, &reversed_reloc[reloc_index])); 10553 10554 my_getExpression (ep, crux); 10555 str = expr_end; 10556 10557 /* Match every open bracket. */ 10558 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t')) 10559 if (*str++ == ')') 10560 crux_depth--; 10561 10562 if (crux_depth > 0) 10563 as_bad ("unclosed '('"); 10564 10565 expr_end = str; 10566 10567 if (reloc_index != 0) 10568 { 10569 prev_reloc_op_frag = frag_now; 10570 for (i = 0; i < reloc_index; i++) 10571 reloc[i] = reversed_reloc[reloc_index - 1 - i]; 10572 } 10573 10574 return reloc_index; 10575 } 10576 10577 static void 10578 my_getExpression (expressionS *ep, char *str) 10579 { 10580 char *save_in; 10581 valueT val; 10582 10583 save_in = input_line_pointer; 10584 input_line_pointer = str; 10585 expression (ep); 10586 expr_end = input_line_pointer; 10587 input_line_pointer = save_in; 10588 10589 /* If we are in mips16 mode, and this is an expression based on `.', 10590 then we bump the value of the symbol by 1 since that is how other 10591 text symbols are handled. We don't bother to handle complex 10592 expressions, just `.' plus or minus a constant. */ 10593 if (mips_opts.mips16 10594 && ep->X_op == O_symbol 10595 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0 10596 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg 10597 && symbol_get_frag (ep->X_add_symbol) == frag_now 10598 && symbol_constant_p (ep->X_add_symbol) 10599 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ()) 10600 S_SET_VALUE (ep->X_add_symbol, val + 1); 10601 } 10602 10603 /* Turn a string in input_line_pointer into a floating point constant 10604 of type TYPE, and store the appropriate bytes in *LITP. The number 10605 of LITTLENUMS emitted is stored in *SIZEP. An error message is 10606 returned, or NULL on OK. */ 10607 10608 char * 10609 md_atof (int type, char *litP, int *sizeP) 10610 { 10611 int prec; 10612 LITTLENUM_TYPE words[4]; 10613 char *t; 10614 int i; 10615 10616 switch (type) 10617 { 10618 case 'f': 10619 prec = 2; 10620 break; 10621 10622 case 'd': 10623 prec = 4; 10624 break; 10625 10626 default: 10627 *sizeP = 0; 10628 return _("bad call to md_atof"); 10629 } 10630 10631 t = atof_ieee (input_line_pointer, type, words); 10632 if (t) 10633 input_line_pointer = t; 10634 10635 *sizeP = prec * 2; 10636 10637 if (! target_big_endian) 10638 { 10639 for (i = prec - 1; i >= 0; i--) 10640 { 10641 md_number_to_chars (litP, words[i], 2); 10642 litP += 2; 10643 } 10644 } 10645 else 10646 { 10647 for (i = 0; i < prec; i++) 10648 { 10649 md_number_to_chars (litP, words[i], 2); 10650 litP += 2; 10651 } 10652 } 10653 10654 return NULL; 10655 } 10656 10657 void 10658 md_number_to_chars (char *buf, valueT val, int n) 10659 { 10660 if (target_big_endian) 10661 number_to_chars_bigendian (buf, val, n); 10662 else 10663 number_to_chars_littleendian (buf, val, n); 10664 } 10665 10666 #ifdef OBJ_ELF 10667 static int support_64bit_objects(void) 10668 { 10669 const char **list, **l; 10670 int yes; 10671 10672 list = bfd_target_list (); 10673 for (l = list; *l != NULL; l++) 10674 #ifdef TE_TMIPS 10675 /* This is traditional mips */ 10676 if (strcmp (*l, "elf64-tradbigmips") == 0 10677 || strcmp (*l, "elf64-tradlittlemips") == 0) 10678 #else 10679 if (strcmp (*l, "elf64-bigmips") == 0 10680 || strcmp (*l, "elf64-littlemips") == 0) 10681 #endif 10682 break; 10683 yes = (*l != NULL); 10684 free (list); 10685 return yes; 10686 } 10687 #endif /* OBJ_ELF */ 10688 10689 const char *md_shortopts = "O::g::G:"; 10690 10691 struct option md_longopts[] = 10692 { 10693 /* Options which specify architecture. */ 10694 #define OPTION_ARCH_BASE (OPTION_MD_BASE) 10695 #define OPTION_MARCH (OPTION_ARCH_BASE + 0) 10696 {"march", required_argument, NULL, OPTION_MARCH}, 10697 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1) 10698 {"mtune", required_argument, NULL, OPTION_MTUNE}, 10699 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2) 10700 {"mips0", no_argument, NULL, OPTION_MIPS1}, 10701 {"mips1", no_argument, NULL, OPTION_MIPS1}, 10702 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3) 10703 {"mips2", no_argument, NULL, OPTION_MIPS2}, 10704 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4) 10705 {"mips3", no_argument, NULL, OPTION_MIPS3}, 10706 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5) 10707 {"mips4", no_argument, NULL, OPTION_MIPS4}, 10708 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6) 10709 {"mips5", no_argument, NULL, OPTION_MIPS5}, 10710 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7) 10711 {"mips32", no_argument, NULL, OPTION_MIPS32}, 10712 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8) 10713 {"mips64", no_argument, NULL, OPTION_MIPS64}, 10714 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9) 10715 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2}, 10716 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10) 10717 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2}, 10718 10719 /* Options which specify Application Specific Extensions (ASEs). */ 10720 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11) 10721 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0) 10722 {"mips16", no_argument, NULL, OPTION_MIPS16}, 10723 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1) 10724 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16}, 10725 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2) 10726 {"mips3d", no_argument, NULL, OPTION_MIPS3D}, 10727 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3) 10728 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D}, 10729 #define OPTION_MDMX (OPTION_ASE_BASE + 4) 10730 {"mdmx", no_argument, NULL, OPTION_MDMX}, 10731 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5) 10732 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX}, 10733 #define OPTION_DSP (OPTION_ASE_BASE + 6) 10734 {"mdsp", no_argument, NULL, OPTION_DSP}, 10735 #define OPTION_NO_DSP (OPTION_ASE_BASE + 7) 10736 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP}, 10737 #define OPTION_MT (OPTION_ASE_BASE + 8) 10738 {"mmt", no_argument, NULL, OPTION_MT}, 10739 #define OPTION_NO_MT (OPTION_ASE_BASE + 9) 10740 {"mno-mt", no_argument, NULL, OPTION_NO_MT}, 10741 10742 /* Old-style architecture options. Don't add more of these. */ 10743 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 10) 10744 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0) 10745 {"m4650", no_argument, NULL, OPTION_M4650}, 10746 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1) 10747 {"no-m4650", no_argument, NULL, OPTION_NO_M4650}, 10748 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2) 10749 {"m4010", no_argument, NULL, OPTION_M4010}, 10750 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3) 10751 {"no-m4010", no_argument, NULL, OPTION_NO_M4010}, 10752 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4) 10753 {"m4100", no_argument, NULL, OPTION_M4100}, 10754 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5) 10755 {"no-m4100", no_argument, NULL, OPTION_NO_M4100}, 10756 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6) 10757 {"m3900", no_argument, NULL, OPTION_M3900}, 10758 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7) 10759 {"no-m3900", no_argument, NULL, OPTION_NO_M3900}, 10760 10761 /* Options which enable bug fixes. */ 10762 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8) 10763 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0) 10764 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX}, 10765 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1) 10766 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX}, 10767 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX}, 10768 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2) 10769 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3) 10770 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120}, 10771 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120}, 10772 #define OPTION_FIX_VR4130 (OPTION_FIX_BASE + 4) 10773 #define OPTION_NO_FIX_VR4130 (OPTION_FIX_BASE + 5) 10774 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130}, 10775 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130}, 10776 #define OPTION_FIX_LOONGSON2F_BTB (OPTION_FIX_BASE + 6) 10777 #define OPTION_NO_FIX_LOONGSON2F_BTB (OPTION_FIX_BASE + 7) 10778 {"mfix-loongson2f-btb", no_argument, NULL, OPTION_FIX_LOONGSON2F_BTB}, 10779 {"mno-fix-loongson2f-btb", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_BTB}, 10780 10781 /* Miscellaneous options. */ 10782 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 8) 10783 #define OPTION_TRAP (OPTION_MISC_BASE + 0) 10784 {"trap", no_argument, NULL, OPTION_TRAP}, 10785 {"no-break", no_argument, NULL, OPTION_TRAP}, 10786 #define OPTION_BREAK (OPTION_MISC_BASE + 1) 10787 {"break", no_argument, NULL, OPTION_BREAK}, 10788 {"no-trap", no_argument, NULL, OPTION_BREAK}, 10789 #define OPTION_EB (OPTION_MISC_BASE + 2) 10790 {"EB", no_argument, NULL, OPTION_EB}, 10791 #define OPTION_EL (OPTION_MISC_BASE + 3) 10792 {"EL", no_argument, NULL, OPTION_EL}, 10793 #define OPTION_FP32 (OPTION_MISC_BASE + 4) 10794 {"mfp32", no_argument, NULL, OPTION_FP32}, 10795 #define OPTION_GP32 (OPTION_MISC_BASE + 5) 10796 {"mgp32", no_argument, NULL, OPTION_GP32}, 10797 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6) 10798 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS}, 10799 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7) 10800 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS}, 10801 #define OPTION_FP64 (OPTION_MISC_BASE + 8) 10802 {"mfp64", no_argument, NULL, OPTION_FP64}, 10803 #define OPTION_GP64 (OPTION_MISC_BASE + 9) 10804 {"mgp64", no_argument, NULL, OPTION_GP64}, 10805 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10) 10806 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11) 10807 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH}, 10808 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH}, 10809 #define OPTION_MSHARED (OPTION_MISC_BASE + 12) 10810 #define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13) 10811 {"mshared", no_argument, NULL, OPTION_MSHARED}, 10812 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED}, 10813 #define OPTION_MSYM32 (OPTION_MISC_BASE + 14) 10814 #define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15) 10815 {"msym32", no_argument, NULL, OPTION_MSYM32}, 10816 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32}, 10817 10818 /* ELF-specific options. */ 10819 #ifdef OBJ_ELF 10820 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 16) 10821 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0) 10822 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED}, 10823 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED}, 10824 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1) 10825 {"non_shared", no_argument, NULL, OPTION_NON_SHARED}, 10826 #define OPTION_XGOT (OPTION_ELF_BASE + 2) 10827 {"xgot", no_argument, NULL, OPTION_XGOT}, 10828 #define OPTION_MABI (OPTION_ELF_BASE + 3) 10829 {"mabi", required_argument, NULL, OPTION_MABI}, 10830 #define OPTION_32 (OPTION_ELF_BASE + 4) 10831 {"32", no_argument, NULL, OPTION_32}, 10832 #define OPTION_N32 (OPTION_ELF_BASE + 5) 10833 {"n32", no_argument, NULL, OPTION_N32}, 10834 #define OPTION_64 (OPTION_ELF_BASE + 6) 10835 {"64", no_argument, NULL, OPTION_64}, 10836 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7) 10837 {"mdebug", no_argument, NULL, OPTION_MDEBUG}, 10838 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8) 10839 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG}, 10840 #define OPTION_PDR (OPTION_ELF_BASE + 9) 10841 {"mpdr", no_argument, NULL, OPTION_PDR}, 10842 #define OPTION_NO_PDR (OPTION_ELF_BASE + 10) 10843 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR}, 10844 #define OPTION_MVXWORKS_PIC (OPTION_ELF_BASE + 11) 10845 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC}, 10846 #endif /* OBJ_ELF */ 10847 10848 {NULL, no_argument, NULL, 0} 10849 }; 10850 size_t md_longopts_size = sizeof (md_longopts); 10851 10852 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to 10853 NEW_VALUE. Warn if another value was already specified. Note: 10854 we have to defer parsing the -march and -mtune arguments in order 10855 to handle 'from-abi' correctly, since the ABI might be specified 10856 in a later argument. */ 10857 10858 static void 10859 mips_set_option_string (const char **string_ptr, const char *new_value) 10860 { 10861 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0) 10862 as_warn (_("A different %s was already specified, is now %s"), 10863 string_ptr == &mips_arch_string ? "-march" : "-mtune", 10864 new_value); 10865 10866 *string_ptr = new_value; 10867 } 10868 10869 int 10870 md_parse_option (int c, char *arg) 10871 { 10872 switch (c) 10873 { 10874 case OPTION_CONSTRUCT_FLOATS: 10875 mips_disable_float_construction = 0; 10876 break; 10877 10878 case OPTION_NO_CONSTRUCT_FLOATS: 10879 mips_disable_float_construction = 1; 10880 break; 10881 10882 case OPTION_TRAP: 10883 mips_trap = 1; 10884 break; 10885 10886 case OPTION_BREAK: 10887 mips_trap = 0; 10888 break; 10889 10890 case OPTION_EB: 10891 target_big_endian = 1; 10892 break; 10893 10894 case OPTION_EL: 10895 target_big_endian = 0; 10896 break; 10897 10898 case 'O': 10899 if (arg && arg[1] == '0') 10900 mips_optimize = 1; 10901 else 10902 mips_optimize = 2; 10903 break; 10904 10905 case 'g': 10906 if (arg == NULL) 10907 mips_debug = 2; 10908 else 10909 mips_debug = atoi (arg); 10910 /* When the MIPS assembler sees -g or -g2, it does not do 10911 optimizations which limit full symbolic debugging. We take 10912 that to be equivalent to -O0. */ 10913 if (mips_debug == 2) 10914 mips_optimize = 1; 10915 break; 10916 10917 case OPTION_MIPS1: 10918 file_mips_isa = ISA_MIPS1; 10919 break; 10920 10921 case OPTION_MIPS2: 10922 file_mips_isa = ISA_MIPS2; 10923 break; 10924 10925 case OPTION_MIPS3: 10926 file_mips_isa = ISA_MIPS3; 10927 break; 10928 10929 case OPTION_MIPS4: 10930 file_mips_isa = ISA_MIPS4; 10931 break; 10932 10933 case OPTION_MIPS5: 10934 file_mips_isa = ISA_MIPS5; 10935 break; 10936 10937 case OPTION_MIPS32: 10938 file_mips_isa = ISA_MIPS32; 10939 break; 10940 10941 case OPTION_MIPS32R2: 10942 file_mips_isa = ISA_MIPS32R2; 10943 break; 10944 10945 case OPTION_MIPS64R2: 10946 file_mips_isa = ISA_MIPS64R2; 10947 break; 10948 10949 case OPTION_MIPS64: 10950 file_mips_isa = ISA_MIPS64; 10951 break; 10952 10953 case OPTION_MTUNE: 10954 mips_set_option_string (&mips_tune_string, arg); 10955 break; 10956 10957 case OPTION_MARCH: 10958 mips_set_option_string (&mips_arch_string, arg); 10959 break; 10960 10961 case OPTION_M4650: 10962 mips_set_option_string (&mips_arch_string, "4650"); 10963 mips_set_option_string (&mips_tune_string, "4650"); 10964 break; 10965 10966 case OPTION_NO_M4650: 10967 break; 10968 10969 case OPTION_M4010: 10970 mips_set_option_string (&mips_arch_string, "4010"); 10971 mips_set_option_string (&mips_tune_string, "4010"); 10972 break; 10973 10974 case OPTION_NO_M4010: 10975 break; 10976 10977 case OPTION_M4100: 10978 mips_set_option_string (&mips_arch_string, "4100"); 10979 mips_set_option_string (&mips_tune_string, "4100"); 10980 break; 10981 10982 case OPTION_NO_M4100: 10983 break; 10984 10985 case OPTION_M3900: 10986 mips_set_option_string (&mips_arch_string, "3900"); 10987 mips_set_option_string (&mips_tune_string, "3900"); 10988 break; 10989 10990 case OPTION_NO_M3900: 10991 break; 10992 10993 case OPTION_MDMX: 10994 mips_opts.ase_mdmx = 1; 10995 break; 10996 10997 case OPTION_NO_MDMX: 10998 mips_opts.ase_mdmx = 0; 10999 break; 11000 11001 case OPTION_DSP: 11002 mips_opts.ase_dsp = 1; 11003 break; 11004 11005 case OPTION_NO_DSP: 11006 mips_opts.ase_dsp = 0; 11007 break; 11008 11009 case OPTION_MT: 11010 mips_opts.ase_mt = 1; 11011 break; 11012 11013 case OPTION_NO_MT: 11014 mips_opts.ase_mt = 0; 11015 break; 11016 11017 case OPTION_MIPS16: 11018 mips_opts.mips16 = 1; 11019 mips_no_prev_insn (); 11020 break; 11021 11022 case OPTION_NO_MIPS16: 11023 mips_opts.mips16 = 0; 11024 mips_no_prev_insn (); 11025 break; 11026 11027 case OPTION_MIPS3D: 11028 mips_opts.ase_mips3d = 1; 11029 break; 11030 11031 case OPTION_NO_MIPS3D: 11032 mips_opts.ase_mips3d = 0; 11033 break; 11034 11035 case OPTION_FIX_VR4120: 11036 mips_fix_vr4120 = 1; 11037 break; 11038 11039 case OPTION_NO_FIX_VR4120: 11040 mips_fix_vr4120 = 0; 11041 break; 11042 11043 case OPTION_FIX_VR4130: 11044 mips_fix_vr4130 = 1; 11045 break; 11046 11047 case OPTION_NO_FIX_VR4130: 11048 mips_fix_vr4130 = 0; 11049 break; 11050 11051 case OPTION_FIX_LOONGSON2F_BTB: 11052 mips_fix_loongson2f_btb = 1; 11053 break; 11054 11055 case OPTION_NO_FIX_LOONGSON2F_BTB: 11056 mips_fix_loongson2f_btb = 0; 11057 break; 11058 11059 case OPTION_RELAX_BRANCH: 11060 mips_relax_branch = 1; 11061 break; 11062 11063 case OPTION_NO_RELAX_BRANCH: 11064 mips_relax_branch = 0; 11065 break; 11066 11067 case OPTION_MSHARED: 11068 mips_in_shared = TRUE; 11069 break; 11070 11071 case OPTION_MNO_SHARED: 11072 mips_in_shared = FALSE; 11073 break; 11074 11075 case OPTION_MSYM32: 11076 mips_opts.sym32 = TRUE; 11077 break; 11078 11079 case OPTION_MNO_SYM32: 11080 mips_opts.sym32 = FALSE; 11081 break; 11082 11083 #ifdef OBJ_ELF 11084 /* When generating ELF code, we permit -KPIC and -call_shared to 11085 select SVR4_PIC, and -non_shared to select no PIC. This is 11086 intended to be compatible with Irix 5. */ 11087 case OPTION_CALL_SHARED: 11088 if (OUTPUT_FLAVOR != bfd_target_elf_flavour) 11089 { 11090 as_bad (_("-call_shared is supported only for ELF format")); 11091 return 0; 11092 } 11093 mips_pic = SVR4_PIC; 11094 mips_abicalls = TRUE; 11095 break; 11096 11097 case OPTION_NON_SHARED: 11098 if (OUTPUT_FLAVOR != bfd_target_elf_flavour) 11099 { 11100 as_bad (_("-non_shared is supported only for ELF format")); 11101 return 0; 11102 } 11103 mips_pic = NO_PIC; 11104 mips_abicalls = FALSE; 11105 break; 11106 11107 /* The -xgot option tells the assembler to use 32 bit offsets 11108 when accessing the got in SVR4_PIC mode. It is for Irix 11109 compatibility. */ 11110 case OPTION_XGOT: 11111 mips_big_got = 1; 11112 break; 11113 #endif /* OBJ_ELF */ 11114 11115 case 'G': 11116 g_switch_value = atoi (arg); 11117 g_switch_seen = 1; 11118 break; 11119 11120 #ifdef OBJ_ELF 11121 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32 11122 and -mabi=64. */ 11123 case OPTION_32: 11124 if (OUTPUT_FLAVOR != bfd_target_elf_flavour) 11125 { 11126 as_bad (_("-32 is supported for ELF format only")); 11127 return 0; 11128 } 11129 mips_abi = O32_ABI; 11130 break; 11131 11132 case OPTION_N32: 11133 if (OUTPUT_FLAVOR != bfd_target_elf_flavour) 11134 { 11135 as_bad (_("-n32 is supported for ELF format only")); 11136 return 0; 11137 } 11138 mips_abi = N32_ABI; 11139 break; 11140 11141 case OPTION_64: 11142 if (OUTPUT_FLAVOR != bfd_target_elf_flavour) 11143 { 11144 as_bad (_("-64 is supported for ELF format only")); 11145 return 0; 11146 } 11147 mips_abi = N64_ABI; 11148 if (! support_64bit_objects()) 11149 as_fatal (_("No compiled in support for 64 bit object file format")); 11150 break; 11151 #endif /* OBJ_ELF */ 11152 11153 case OPTION_GP32: 11154 file_mips_gp32 = 1; 11155 break; 11156 11157 case OPTION_GP64: 11158 file_mips_gp32 = 0; 11159 break; 11160 11161 case OPTION_FP32: 11162 file_mips_fp32 = 1; 11163 break; 11164 11165 case OPTION_FP64: 11166 file_mips_fp32 = 0; 11167 break; 11168 11169 #ifdef OBJ_ELF 11170 case OPTION_MABI: 11171 if (OUTPUT_FLAVOR != bfd_target_elf_flavour) 11172 { 11173 as_bad (_("-mabi is supported for ELF format only")); 11174 return 0; 11175 } 11176 if (strcmp (arg, "32") == 0) 11177 mips_abi = O32_ABI; 11178 else if (strcmp (arg, "o64") == 0) 11179 mips_abi = O64_ABI; 11180 else if (strcmp (arg, "n32") == 0) 11181 mips_abi = N32_ABI; 11182 else if (strcmp (arg, "64") == 0) 11183 { 11184 mips_abi = N64_ABI; 11185 if (! support_64bit_objects()) 11186 as_fatal (_("No compiled in support for 64 bit object file " 11187 "format")); 11188 } 11189 else if (strcmp (arg, "eabi") == 0) 11190 mips_abi = EABI_ABI; 11191 else 11192 { 11193 as_fatal (_("invalid abi -mabi=%s"), arg); 11194 return 0; 11195 } 11196 break; 11197 #endif /* OBJ_ELF */ 11198 11199 case OPTION_M7000_HILO_FIX: 11200 mips_7000_hilo_fix = TRUE; 11201 break; 11202 11203 case OPTION_MNO_7000_HILO_FIX: 11204 mips_7000_hilo_fix = FALSE; 11205 break; 11206 11207 #ifdef OBJ_ELF 11208 case OPTION_MDEBUG: 11209 mips_flag_mdebug = TRUE; 11210 break; 11211 11212 case OPTION_NO_MDEBUG: 11213 mips_flag_mdebug = FALSE; 11214 break; 11215 11216 case OPTION_PDR: 11217 mips_flag_pdr = TRUE; 11218 break; 11219 11220 case OPTION_NO_PDR: 11221 mips_flag_pdr = FALSE; 11222 break; 11223 11224 case OPTION_MVXWORKS_PIC: 11225 mips_pic = VXWORKS_PIC; 11226 break; 11227 #endif /* OBJ_ELF */ 11228 11229 default: 11230 return 0; 11231 } 11232 11233 return 1; 11234 } 11235 11236 /* Set up globals to generate code for the ISA or processor 11237 described by INFO. */ 11238 11239 static void 11240 mips_set_architecture (const struct mips_cpu_info *info) 11241 { 11242 if (info != 0) 11243 { 11244 file_mips_arch = info->cpu; 11245 mips_opts.arch = info->cpu; 11246 mips_opts.isa = info->isa; 11247 } 11248 } 11249 11250 11251 /* Likewise for tuning. */ 11252 11253 static void 11254 mips_set_tune (const struct mips_cpu_info *info) 11255 { 11256 if (info != 0) 11257 mips_tune = info->cpu; 11258 } 11259 11260 11261 void 11262 mips_after_parse_args (void) 11263 { 11264 const struct mips_cpu_info *arch_info = 0; 11265 const struct mips_cpu_info *tune_info = 0; 11266 11267 /* GP relative stuff not working for PE */ 11268 if (strncmp (TARGET_OS, "pe", 2) == 0) 11269 { 11270 if (g_switch_seen && g_switch_value != 0) 11271 as_bad (_("-G not supported in this configuration.")); 11272 g_switch_value = 0; 11273 } 11274 11275 if (mips_abi == NO_ABI) 11276 mips_abi = MIPS_DEFAULT_ABI; 11277 11278 /* The following code determines the architecture and register size. 11279 Similar code was added to GCC 3.3 (see override_options() in 11280 config/mips/mips.c). The GAS and GCC code should be kept in sync 11281 as much as possible. */ 11282 11283 if (mips_arch_string != 0) 11284 arch_info = mips_parse_cpu ("-march", mips_arch_string); 11285 11286 if (file_mips_isa != ISA_UNKNOWN) 11287 { 11288 /* Handle -mipsN. At this point, file_mips_isa contains the 11289 ISA level specified by -mipsN, while arch_info->isa contains 11290 the -march selection (if any). */ 11291 if (arch_info != 0) 11292 { 11293 /* -march takes precedence over -mipsN, since it is more descriptive. 11294 There's no harm in specifying both as long as the ISA levels 11295 are the same. */ 11296 if (file_mips_isa != arch_info->isa) 11297 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"), 11298 mips_cpu_info_from_isa (file_mips_isa)->name, 11299 mips_cpu_info_from_isa (arch_info->isa)->name); 11300 } 11301 else 11302 arch_info = mips_cpu_info_from_isa (file_mips_isa); 11303 } 11304 11305 if (arch_info == 0) 11306 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT); 11307 11308 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa)) 11309 as_bad ("-march=%s is not compatible with the selected ABI", 11310 arch_info->name); 11311 11312 mips_set_architecture (arch_info); 11313 11314 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */ 11315 if (mips_tune_string != 0) 11316 tune_info = mips_parse_cpu ("-mtune", mips_tune_string); 11317 11318 if (tune_info == 0) 11319 mips_set_tune (arch_info); 11320 else 11321 mips_set_tune (tune_info); 11322 11323 if (file_mips_gp32 >= 0) 11324 { 11325 /* The user specified the size of the integer registers. Make sure 11326 it agrees with the ABI and ISA. */ 11327 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa)) 11328 as_bad (_("-mgp64 used with a 32-bit processor")); 11329 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi)) 11330 as_bad (_("-mgp32 used with a 64-bit ABI")); 11331 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi)) 11332 as_bad (_("-mgp64 used with a 32-bit ABI")); 11333 } 11334 else 11335 { 11336 /* Infer the integer register size from the ABI and processor. 11337 Restrict ourselves to 32-bit registers if that's all the 11338 processor has, or if the ABI cannot handle 64-bit registers. */ 11339 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi) 11340 || !ISA_HAS_64BIT_REGS (mips_opts.isa)); 11341 } 11342 11343 /* ??? GAS treats single-float processors as though they had 64-bit 11344 float registers (although it complains when double-precision 11345 instructions are used). As things stand, saying they have 32-bit 11346 registers would lead to spurious "register must be even" messages. 11347 So here we assume float registers are always the same size as 11348 integer ones, unless the user says otherwise. */ 11349 if (file_mips_fp32 < 0) 11350 file_mips_fp32 = file_mips_gp32; 11351 11352 /* End of GCC-shared inference code. */ 11353 11354 /* This flag is set when we have a 64-bit capable CPU but use only 11355 32-bit wide registers. Note that EABI does not use it. */ 11356 if (ISA_HAS_64BIT_REGS (mips_opts.isa) 11357 && ((mips_abi == NO_ABI && file_mips_gp32 == 1) 11358 || mips_abi == O32_ABI)) 11359 mips_32bitmode = 1; 11360 11361 if (mips_opts.isa == ISA_MIPS1 && mips_trap) 11362 as_bad (_("trap exception not supported at ISA 1")); 11363 11364 /* If the selected architecture includes support for ASEs, enable 11365 generation of code for them. */ 11366 if (mips_opts.mips16 == -1) 11367 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0; 11368 if (mips_opts.ase_mips3d == -1) 11369 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0; 11370 if (mips_opts.ase_mdmx == -1) 11371 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0; 11372 if (mips_opts.ase_dsp == -1) 11373 mips_opts.ase_dsp = (CPU_HAS_DSP (file_mips_arch)) ? 1 : 0; 11374 if (mips_opts.ase_mt == -1) 11375 mips_opts.ase_mt = (CPU_HAS_MT (file_mips_arch)) ? 1 : 0; 11376 11377 file_mips_isa = mips_opts.isa; 11378 file_ase_mips16 = mips_opts.mips16; 11379 file_ase_mips3d = mips_opts.ase_mips3d; 11380 file_ase_mdmx = mips_opts.ase_mdmx; 11381 file_ase_dsp = mips_opts.ase_dsp; 11382 file_ase_mt = mips_opts.ase_mt; 11383 mips_opts.gp32 = file_mips_gp32; 11384 mips_opts.fp32 = file_mips_fp32; 11385 11386 if (mips_flag_mdebug < 0) 11387 { 11388 #ifdef OBJ_MAYBE_ECOFF 11389 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour) 11390 mips_flag_mdebug = 1; 11391 else 11392 #endif /* OBJ_MAYBE_ECOFF */ 11393 mips_flag_mdebug = 0; 11394 } 11395 } 11396 11397 void 11398 mips_init_after_args (void) 11399 { 11400 /* initialize opcodes */ 11401 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes; 11402 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes; 11403 } 11404 11405 long 11406 md_pcrel_from (fixS *fixP) 11407 { 11408 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address; 11409 switch (fixP->fx_r_type) 11410 { 11411 case BFD_RELOC_16_PCREL_S2: 11412 case BFD_RELOC_MIPS_JMP: 11413 /* Return the address of the delay slot. */ 11414 return addr + 4; 11415 default: 11416 return addr; 11417 } 11418 } 11419 11420 /* This is called before the symbol table is processed. In order to 11421 work with gcc when using mips-tfile, we must keep all local labels. 11422 However, in other cases, we want to discard them. If we were 11423 called with -g, but we didn't see any debugging information, it may 11424 mean that gcc is smuggling debugging information through to 11425 mips-tfile, in which case we must generate all local labels. */ 11426 11427 void 11428 mips_frob_file_before_adjust (void) 11429 { 11430 #ifndef NO_ECOFF_DEBUGGING 11431 if (ECOFF_DEBUGGING 11432 && mips_debug != 0 11433 && ! ecoff_debugging_seen) 11434 flag_keep_locals = 1; 11435 #endif 11436 } 11437 11438 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede 11439 the corresponding LO16 reloc. This is called before md_apply_fix and 11440 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit 11441 relocation operators. 11442 11443 For our purposes, a %lo() expression matches a %got() or %hi() 11444 expression if: 11445 11446 (a) it refers to the same symbol; and 11447 (b) the offset applied in the %lo() expression is no lower than 11448 the offset applied in the %got() or %hi(). 11449 11450 (b) allows us to cope with code like: 11451 11452 lui $4,%hi(foo) 11453 lh $4,%lo(foo+2)($4) 11454 11455 ...which is legal on RELA targets, and has a well-defined behaviour 11456 if the user knows that adding 2 to "foo" will not induce a carry to 11457 the high 16 bits. 11458 11459 When several %lo()s match a particular %got() or %hi(), we use the 11460 following rules to distinguish them: 11461 11462 (1) %lo()s with smaller offsets are a better match than %lo()s with 11463 higher offsets. 11464 11465 (2) %lo()s with no matching %got() or %hi() are better than those 11466 that already have a matching %got() or %hi(). 11467 11468 (3) later %lo()s are better than earlier %lo()s. 11469 11470 These rules are applied in order. 11471 11472 (1) means, among other things, that %lo()s with identical offsets are 11473 chosen if they exist. 11474 11475 (2) means that we won't associate several high-part relocations with 11476 the same low-part relocation unless there's no alternative. Having 11477 several high parts for the same low part is a GNU extension; this rule 11478 allows careful users to avoid it. 11479 11480 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order, 11481 with the last high-part relocation being at the front of the list. 11482 It therefore makes sense to choose the last matching low-part 11483 relocation, all other things being equal. It's also easier 11484 to code that way. */ 11485 11486 void 11487 mips_frob_file (void) 11488 { 11489 struct mips_hi_fixup *l; 11490 11491 for (l = mips_hi_fixup_list; l != NULL; l = l->next) 11492 { 11493 segment_info_type *seginfo; 11494 bfd_boolean matched_lo_p; 11495 fixS **hi_pos, **lo_pos, **pos; 11496 11497 assert (reloc_needs_lo_p (l->fixp->fx_r_type)); 11498 11499 /* If a GOT16 relocation turns out to be against a global symbol, 11500 there isn't supposed to be a matching LO. */ 11501 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16 11502 && !pic_need_relax (l->fixp->fx_addsy, l->seg)) 11503 continue; 11504 11505 /* Check quickly whether the next fixup happens to be a matching %lo. */ 11506 if (fixup_has_matching_lo_p (l->fixp)) 11507 continue; 11508 11509 seginfo = seg_info (l->seg); 11510 11511 /* Set HI_POS to the position of this relocation in the chain. 11512 Set LO_POS to the position of the chosen low-part relocation. 11513 MATCHED_LO_P is true on entry to the loop if *POS is a low-part 11514 relocation that matches an immediately-preceding high-part 11515 relocation. */ 11516 hi_pos = NULL; 11517 lo_pos = NULL; 11518 matched_lo_p = FALSE; 11519 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next) 11520 { 11521 if (*pos == l->fixp) 11522 hi_pos = pos; 11523 11524 if (((*pos)->fx_r_type == BFD_RELOC_LO16 11525 || (*pos)->fx_r_type == BFD_RELOC_MIPS16_LO16) 11526 && (*pos)->fx_addsy == l->fixp->fx_addsy 11527 && (*pos)->fx_offset >= l->fixp->fx_offset 11528 && (lo_pos == NULL 11529 || (*pos)->fx_offset < (*lo_pos)->fx_offset 11530 || (!matched_lo_p 11531 && (*pos)->fx_offset == (*lo_pos)->fx_offset))) 11532 lo_pos = pos; 11533 11534 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type) 11535 && fixup_has_matching_lo_p (*pos)); 11536 } 11537 11538 /* If we found a match, remove the high-part relocation from its 11539 current position and insert it before the low-part relocation. 11540 Make the offsets match so that fixup_has_matching_lo_p() 11541 will return true. 11542 11543 We don't warn about unmatched high-part relocations since some 11544 versions of gcc have been known to emit dead "lui ...%hi(...)" 11545 instructions. */ 11546 if (lo_pos != NULL) 11547 { 11548 l->fixp->fx_offset = (*lo_pos)->fx_offset; 11549 if (l->fixp->fx_next != *lo_pos) 11550 { 11551 *hi_pos = l->fixp->fx_next; 11552 l->fixp->fx_next = *lo_pos; 11553 *lo_pos = l->fixp; 11554 } 11555 } 11556 } 11557 } 11558 11559 /* We may have combined relocations without symbols in the N32/N64 ABI. 11560 We have to prevent gas from dropping them. */ 11561 11562 int 11563 mips_force_relocation (fixS *fixp) 11564 { 11565 if (generic_force_reloc (fixp)) 11566 return 1; 11567 11568 if (HAVE_NEWABI 11569 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr 11570 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB 11571 || fixp->fx_r_type == BFD_RELOC_HI16_S 11572 || fixp->fx_r_type == BFD_RELOC_LO16)) 11573 return 1; 11574 11575 return 0; 11576 } 11577 11578 /* Apply a fixup to the object file. */ 11579 11580 void 11581 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED) 11582 { 11583 bfd_byte *buf; 11584 long insn; 11585 reloc_howto_type *howto; 11586 11587 /* We ignore generic BFD relocations we don't know about. */ 11588 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type); 11589 if (! howto) 11590 return; 11591 11592 assert (fixP->fx_size == 4 11593 || fixP->fx_r_type == BFD_RELOC_16 11594 || fixP->fx_r_type == BFD_RELOC_64 11595 || fixP->fx_r_type == BFD_RELOC_CTOR 11596 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB 11597 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT 11598 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY); 11599 11600 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where); 11601 11602 assert (! fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2); 11603 11604 /* Don't treat parts of a composite relocation as done. There are two 11605 reasons for this: 11606 11607 (1) The second and third parts will be against 0 (RSS_UNDEF) but 11608 should nevertheless be emitted if the first part is. 11609 11610 (2) In normal usage, composite relocations are never assembly-time 11611 constants. The easiest way of dealing with the pathological 11612 exceptions is to generate a relocation against STN_UNDEF and 11613 leave everything up to the linker. */ 11614 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel && fixP->fx_tcbit == 0) 11615 fixP->fx_done = 1; 11616 11617 switch (fixP->fx_r_type) 11618 { 11619 case BFD_RELOC_MIPS_TLS_GD: 11620 case BFD_RELOC_MIPS_TLS_LDM: 11621 case BFD_RELOC_MIPS_TLS_DTPREL_HI16: 11622 case BFD_RELOC_MIPS_TLS_DTPREL_LO16: 11623 case BFD_RELOC_MIPS_TLS_GOTTPREL: 11624 case BFD_RELOC_MIPS_TLS_TPREL_HI16: 11625 case BFD_RELOC_MIPS_TLS_TPREL_LO16: 11626 S_SET_THREAD_LOCAL (fixP->fx_addsy); 11627 /* fall through */ 11628 11629 case BFD_RELOC_MIPS_JMP: 11630 case BFD_RELOC_MIPS_SHIFT5: 11631 case BFD_RELOC_MIPS_SHIFT6: 11632 case BFD_RELOC_MIPS_GOT_DISP: 11633 case BFD_RELOC_MIPS_GOT_PAGE: 11634 case BFD_RELOC_MIPS_GOT_OFST: 11635 case BFD_RELOC_MIPS_SUB: 11636 case BFD_RELOC_MIPS_INSERT_A: 11637 case BFD_RELOC_MIPS_INSERT_B: 11638 case BFD_RELOC_MIPS_DELETE: 11639 case BFD_RELOC_MIPS_HIGHEST: 11640 case BFD_RELOC_MIPS_HIGHER: 11641 case BFD_RELOC_MIPS_SCN_DISP: 11642 case BFD_RELOC_MIPS_REL16: 11643 case BFD_RELOC_MIPS_RELGOT: 11644 case BFD_RELOC_MIPS_JALR: 11645 case BFD_RELOC_HI16: 11646 case BFD_RELOC_HI16_S: 11647 case BFD_RELOC_GPREL16: 11648 case BFD_RELOC_MIPS_LITERAL: 11649 case BFD_RELOC_MIPS_CALL16: 11650 case BFD_RELOC_MIPS_GOT16: 11651 case BFD_RELOC_GPREL32: 11652 case BFD_RELOC_MIPS_GOT_HI16: 11653 case BFD_RELOC_MIPS_GOT_LO16: 11654 case BFD_RELOC_MIPS_CALL_HI16: 11655 case BFD_RELOC_MIPS_CALL_LO16: 11656 case BFD_RELOC_MIPS16_GPREL: 11657 case BFD_RELOC_MIPS16_HI16: 11658 case BFD_RELOC_MIPS16_HI16_S: 11659 /* Nothing needed to do. The value comes from the reloc entry */ 11660 break; 11661 11662 case BFD_RELOC_MIPS16_JMP: 11663 /* We currently always generate a reloc against a symbol, which 11664 means that we don't want an addend even if the symbol is 11665 defined. */ 11666 *valP = 0; 11667 break; 11668 11669 case BFD_RELOC_64: 11670 /* This is handled like BFD_RELOC_32, but we output a sign 11671 extended value if we are only 32 bits. */ 11672 if (fixP->fx_done) 11673 { 11674 if (8 <= sizeof (valueT)) 11675 md_number_to_chars ((char *) buf, *valP, 8); 11676 else 11677 { 11678 valueT hiv; 11679 11680 if ((*valP & 0x80000000) != 0) 11681 hiv = 0xffffffff; 11682 else 11683 hiv = 0; 11684 md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)), 11685 *valP, 4); 11686 md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)), 11687 hiv, 4); 11688 } 11689 } 11690 break; 11691 11692 case BFD_RELOC_RVA: 11693 case BFD_RELOC_32: 11694 /* If we are deleting this reloc entry, we must fill in the 11695 value now. This can happen if we have a .word which is not 11696 resolved when it appears but is later defined. */ 11697 if (fixP->fx_done) 11698 md_number_to_chars ((char *) buf, *valP, 4); 11699 break; 11700 11701 case BFD_RELOC_16: 11702 /* If we are deleting this reloc entry, we must fill in the 11703 value now. */ 11704 if (fixP->fx_done) 11705 md_number_to_chars ((char *) buf, *valP, 2); 11706 break; 11707 11708 case BFD_RELOC_LO16: 11709 case BFD_RELOC_MIPS16_LO16: 11710 /* FIXME: Now that embedded-PIC is gone, some of this code/comment 11711 may be safe to remove, but if so it's not obvious. */ 11712 /* When handling an embedded PIC switch statement, we can wind 11713 up deleting a LO16 reloc. See the 'o' case in mips_ip. */ 11714 if (fixP->fx_done) 11715 { 11716 if (*valP + 0x8000 > 0xffff) 11717 as_bad_where (fixP->fx_file, fixP->fx_line, 11718 _("relocation overflow")); 11719 if (target_big_endian) 11720 buf += 2; 11721 md_number_to_chars ((char *) buf, *valP, 2); 11722 } 11723 break; 11724 11725 case BFD_RELOC_16_PCREL_S2: 11726 if ((*valP & 0x3) != 0) 11727 as_bad_where (fixP->fx_file, fixP->fx_line, 11728 _("Branch to misaligned address (%lx)"), (long) *valP); 11729 11730 /* 11731 * We need to save the bits in the instruction since fixup_segment() 11732 * might be deleting the relocation entry (i.e., a branch within 11733 * the current segment). 11734 */ 11735 if (! fixP->fx_done) 11736 break; 11737 11738 /* update old instruction data */ 11739 if (target_big_endian) 11740 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; 11741 else 11742 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; 11743 11744 if (*valP + 0x20000 <= 0x3ffff) 11745 { 11746 insn |= (*valP >> 2) & 0xffff; 11747 md_number_to_chars ((char *) buf, insn, 4); 11748 } 11749 else if (mips_pic == NO_PIC 11750 && fixP->fx_done 11751 && fixP->fx_frag->fr_address >= text_section->vma 11752 && (fixP->fx_frag->fr_address 11753 < text_section->vma + bfd_get_section_size (text_section)) 11754 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */ 11755 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */ 11756 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */ 11757 { 11758 /* The branch offset is too large. If this is an 11759 unconditional branch, and we are not generating PIC code, 11760 we can convert it to an absolute jump instruction. */ 11761 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */ 11762 insn = 0x0c000000; /* jal */ 11763 else 11764 insn = 0x08000000; /* j */ 11765 fixP->fx_r_type = BFD_RELOC_MIPS_JMP; 11766 fixP->fx_done = 0; 11767 fixP->fx_addsy = section_symbol (text_section); 11768 *valP += md_pcrel_from (fixP); 11769 md_number_to_chars ((char *) buf, insn, 4); 11770 } 11771 else 11772 { 11773 /* If we got here, we have branch-relaxation disabled, 11774 and there's nothing we can do to fix this instruction 11775 without turning it into a longer sequence. */ 11776 as_bad_where (fixP->fx_file, fixP->fx_line, 11777 _("Branch out of range")); 11778 } 11779 break; 11780 11781 case BFD_RELOC_VTABLE_INHERIT: 11782 fixP->fx_done = 0; 11783 if (fixP->fx_addsy 11784 && !S_IS_DEFINED (fixP->fx_addsy) 11785 && !S_IS_WEAK (fixP->fx_addsy)) 11786 S_SET_WEAK (fixP->fx_addsy); 11787 break; 11788 11789 case BFD_RELOC_VTABLE_ENTRY: 11790 fixP->fx_done = 0; 11791 break; 11792 11793 default: 11794 internalError (); 11795 } 11796 11797 /* Remember value for tc_gen_reloc. */ 11798 fixP->fx_addnumber = *valP; 11799 } 11800 11801 static symbolS * 11802 get_symbol (void) 11803 { 11804 int c; 11805 char *name; 11806 symbolS *p; 11807 11808 name = input_line_pointer; 11809 c = get_symbol_end (); 11810 p = (symbolS *) symbol_find_or_make (name); 11811 *input_line_pointer = c; 11812 return p; 11813 } 11814 11815 /* Align the current frag to a given power of two. The MIPS assembler 11816 also automatically adjusts any preceding label. */ 11817 11818 static void 11819 mips_align (int to, int fill, symbolS *label) 11820 { 11821 mips_emit_delays (); 11822 frag_align (to, fill, 0); 11823 record_alignment (now_seg, to); 11824 if (label != NULL) 11825 { 11826 assert (S_GET_SEGMENT (label) == now_seg); 11827 symbol_set_frag (label, frag_now); 11828 S_SET_VALUE (label, (valueT) frag_now_fix ()); 11829 } 11830 } 11831 11832 /* Align to a given power of two. .align 0 turns off the automatic 11833 alignment used by the data creating pseudo-ops. */ 11834 11835 static void 11836 s_align (int x ATTRIBUTE_UNUSED) 11837 { 11838 register int temp; 11839 register long temp_fill; 11840 long max_alignment = 15; 11841 11842 /* 11843 11844 o Note that the assembler pulls down any immediately preceding label 11845 to the aligned address. 11846 o It's not documented but auto alignment is reinstated by 11847 a .align pseudo instruction. 11848 o Note also that after auto alignment is turned off the mips assembler 11849 issues an error on attempt to assemble an improperly aligned data item. 11850 We don't. 11851 11852 */ 11853 11854 temp = get_absolute_expression (); 11855 if (temp > max_alignment) 11856 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment); 11857 else if (temp < 0) 11858 { 11859 as_warn (_("Alignment negative: 0 assumed.")); 11860 temp = 0; 11861 } 11862 if (*input_line_pointer == ',') 11863 { 11864 ++input_line_pointer; 11865 temp_fill = get_absolute_expression (); 11866 } 11867 else 11868 temp_fill = 0; 11869 if (temp) 11870 { 11871 auto_align = 1; 11872 mips_align (temp, (int) temp_fill, 11873 insn_labels != NULL ? insn_labels->label : NULL); 11874 } 11875 else 11876 { 11877 auto_align = 0; 11878 } 11879 11880 demand_empty_rest_of_line (); 11881 } 11882 11883 static void 11884 s_change_sec (int sec) 11885 { 11886 segT seg; 11887 11888 #ifdef OBJ_ELF 11889 /* The ELF backend needs to know that we are changing sections, so 11890 that .previous works correctly. We could do something like check 11891 for an obj_section_change_hook macro, but that might be confusing 11892 as it would not be appropriate to use it in the section changing 11893 functions in read.c, since obj-elf.c intercepts those. FIXME: 11894 This should be cleaner, somehow. */ 11895 obj_elf_section_change_hook (); 11896 #endif 11897 11898 mips_emit_delays (); 11899 switch (sec) 11900 { 11901 case 't': 11902 s_text (0); 11903 break; 11904 case 'd': 11905 s_data (0); 11906 break; 11907 case 'b': 11908 subseg_set (bss_section, (subsegT) get_absolute_expression ()); 11909 demand_empty_rest_of_line (); 11910 break; 11911 11912 case 'r': 11913 seg = subseg_new (RDATA_SECTION_NAME, 11914 (subsegT) get_absolute_expression ()); 11915 if (OUTPUT_FLAVOR == bfd_target_elf_flavour) 11916 { 11917 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD 11918 | SEC_READONLY | SEC_RELOC 11919 | SEC_DATA)); 11920 if (strcmp (TARGET_OS, "elf") != 0) 11921 record_alignment (seg, 4); 11922 } 11923 demand_empty_rest_of_line (); 11924 break; 11925 11926 case 's': 11927 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ()); 11928 if (OUTPUT_FLAVOR == bfd_target_elf_flavour) 11929 { 11930 bfd_set_section_flags (stdoutput, seg, 11931 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA); 11932 if (strcmp (TARGET_OS, "elf") != 0) 11933 record_alignment (seg, 4); 11934 } 11935 demand_empty_rest_of_line (); 11936 break; 11937 } 11938 11939 auto_align = 1; 11940 } 11941 11942 void 11943 s_change_section (int ignore ATTRIBUTE_UNUSED) 11944 { 11945 #ifdef OBJ_ELF 11946 char *section_name; 11947 char c; 11948 char next_c = 0; 11949 int section_type; 11950 int section_flag; 11951 int section_entry_size; 11952 int section_alignment; 11953 11954 if (OUTPUT_FLAVOR != bfd_target_elf_flavour) 11955 return; 11956 11957 section_name = input_line_pointer; 11958 c = get_symbol_end (); 11959 if (c) 11960 next_c = *(input_line_pointer + 1); 11961 11962 /* Do we have .section Name<,"flags">? */ 11963 if (c != ',' || (c == ',' && next_c == '"')) 11964 { 11965 /* just after name is now '\0'. */ 11966 *input_line_pointer = c; 11967 input_line_pointer = section_name; 11968 obj_elf_section (ignore); 11969 return; 11970 } 11971 input_line_pointer++; 11972 11973 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */ 11974 if (c == ',') 11975 section_type = get_absolute_expression (); 11976 else 11977 section_type = 0; 11978 if (*input_line_pointer++ == ',') 11979 section_flag = get_absolute_expression (); 11980 else 11981 section_flag = 0; 11982 if (*input_line_pointer++ == ',') 11983 section_entry_size = get_absolute_expression (); 11984 else 11985 section_entry_size = 0; 11986 if (*input_line_pointer++ == ',') 11987 section_alignment = get_absolute_expression (); 11988 else 11989 section_alignment = 0; 11990 11991 section_name = xstrdup (section_name); 11992 11993 /* When using the generic form of .section (as implemented by obj-elf.c), 11994 there's no way to set the section type to SHT_MIPS_DWARF. Users have 11995 traditionally had to fall back on the more common @progbits instead. 11996 11997 There's nothing really harmful in this, since bfd will correct 11998 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it 11999 means that, for backwards compatibiltiy, the special_section entries 12000 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF. 12001 12002 Even so, we shouldn't force users of the MIPS .section syntax to 12003 incorrectly label the sections as SHT_PROGBITS. The best compromise 12004 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the 12005 generic type-checking code. */ 12006 if (section_type == SHT_MIPS_DWARF) 12007 section_type = SHT_PROGBITS; 12008 12009 obj_elf_change_section (section_name, section_type, section_flag, 12010 section_entry_size, 0, 0, 0); 12011 12012 if (now_seg->name != section_name) 12013 free (section_name); 12014 #endif /* OBJ_ELF */ 12015 } 12016 12017 void 12018 mips_enable_auto_align (void) 12019 { 12020 auto_align = 1; 12021 } 12022 12023 static void 12024 s_cons (int log_size) 12025 { 12026 symbolS *label; 12027 12028 label = insn_labels != NULL ? insn_labels->label : NULL; 12029 mips_emit_delays (); 12030 if (log_size > 0 && auto_align) 12031 mips_align (log_size, 0, label); 12032 mips_clear_insn_labels (); 12033 cons (1 << log_size); 12034 } 12035 12036 static void 12037 s_float_cons (int type) 12038 { 12039 symbolS *label; 12040 12041 label = insn_labels != NULL ? insn_labels->label : NULL; 12042 12043 mips_emit_delays (); 12044 12045 if (auto_align) 12046 { 12047 if (type == 'd') 12048 mips_align (3, 0, label); 12049 else 12050 mips_align (2, 0, label); 12051 } 12052 12053 mips_clear_insn_labels (); 12054 12055 float_cons (type); 12056 } 12057 12058 /* Handle .globl. We need to override it because on Irix 5 you are 12059 permitted to say 12060 .globl foo .text 12061 where foo is an undefined symbol, to mean that foo should be 12062 considered to be the address of a function. */ 12063 12064 static void 12065 s_mips_globl (int x ATTRIBUTE_UNUSED) 12066 { 12067 char *name; 12068 int c; 12069 symbolS *symbolP; 12070 flagword flag; 12071 12072 do 12073 { 12074 name = input_line_pointer; 12075 c = get_symbol_end (); 12076 symbolP = symbol_find_or_make (name); 12077 S_SET_EXTERNAL (symbolP); 12078 12079 *input_line_pointer = c; 12080 SKIP_WHITESPACE (); 12081 12082 /* On Irix 5, every global symbol that is not explicitly labelled as 12083 being a function is apparently labelled as being an object. */ 12084 flag = BSF_OBJECT; 12085 12086 if (!is_end_of_line[(unsigned char) *input_line_pointer] 12087 && (*input_line_pointer != ',')) 12088 { 12089 char *secname; 12090 asection *sec; 12091 12092 secname = input_line_pointer; 12093 c = get_symbol_end (); 12094 sec = bfd_get_section_by_name (stdoutput, secname); 12095 if (sec == NULL) 12096 as_bad (_("%s: no such section"), secname); 12097 *input_line_pointer = c; 12098 12099 if (sec != NULL && (sec->flags & SEC_CODE) != 0) 12100 flag = BSF_FUNCTION; 12101 } 12102 12103 symbol_get_bfdsym (symbolP)->flags |= flag; 12104 12105 c = *input_line_pointer; 12106 if (c == ',') 12107 { 12108 input_line_pointer++; 12109 SKIP_WHITESPACE (); 12110 if (is_end_of_line[(unsigned char) *input_line_pointer]) 12111 c = '\n'; 12112 } 12113 } 12114 while (c == ','); 12115 12116 demand_empty_rest_of_line (); 12117 } 12118 12119 static void 12120 s_option (int x ATTRIBUTE_UNUSED) 12121 { 12122 char *opt; 12123 char c; 12124 12125 opt = input_line_pointer; 12126 c = get_symbol_end (); 12127 12128 if (*opt == 'O') 12129 { 12130 /* FIXME: What does this mean? */ 12131 } 12132 else if (strncmp (opt, "pic", 3) == 0) 12133 { 12134 int i; 12135 12136 i = atoi (opt + 3); 12137 if (i == 0) 12138 mips_pic = NO_PIC; 12139 else if (i == 2) 12140 { 12141 mips_pic = SVR4_PIC; 12142 mips_abicalls = TRUE; 12143 } 12144 else 12145 as_bad (_(".option pic%d not supported"), i); 12146 12147 if (mips_pic == SVR4_PIC) 12148 { 12149 if (g_switch_seen && g_switch_value != 0) 12150 as_warn (_("-G may not be used with SVR4 PIC code")); 12151 g_switch_value = 0; 12152 bfd_set_gp_size (stdoutput, 0); 12153 } 12154 } 12155 else 12156 as_warn (_("Unrecognized option \"%s\""), opt); 12157 12158 *input_line_pointer = c; 12159 demand_empty_rest_of_line (); 12160 } 12161 12162 /* This structure is used to hold a stack of .set values. */ 12163 12164 struct mips_option_stack 12165 { 12166 struct mips_option_stack *next; 12167 struct mips_set_options options; 12168 }; 12169 12170 static struct mips_option_stack *mips_opts_stack; 12171 12172 /* Handle the .set pseudo-op. */ 12173 12174 static void 12175 s_mipsset (int x ATTRIBUTE_UNUSED) 12176 { 12177 char *name = input_line_pointer, ch; 12178 12179 while (!is_end_of_line[(unsigned char) *input_line_pointer]) 12180 ++input_line_pointer; 12181 ch = *input_line_pointer; 12182 *input_line_pointer = '\0'; 12183 12184 if (strcmp (name, "reorder") == 0) 12185 { 12186 if (mips_opts.noreorder) 12187 end_noreorder (); 12188 } 12189 else if (strcmp (name, "noreorder") == 0) 12190 { 12191 if (!mips_opts.noreorder) 12192 start_noreorder (); 12193 } 12194 else if (strcmp (name, "at") == 0) 12195 { 12196 mips_opts.noat = 0; 12197 } 12198 else if (strcmp (name, "noat") == 0) 12199 { 12200 mips_opts.noat = 1; 12201 } 12202 else if (strcmp (name, "macro") == 0) 12203 { 12204 mips_opts.warn_about_macros = 0; 12205 } 12206 else if (strcmp (name, "nomacro") == 0) 12207 { 12208 if (mips_opts.noreorder == 0) 12209 as_bad (_("`noreorder' must be set before `nomacro'")); 12210 mips_opts.warn_about_macros = 1; 12211 } 12212 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0) 12213 { 12214 mips_opts.nomove = 0; 12215 } 12216 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0) 12217 { 12218 mips_opts.nomove = 1; 12219 } 12220 else if (strcmp (name, "bopt") == 0) 12221 { 12222 mips_opts.nobopt = 0; 12223 } 12224 else if (strcmp (name, "nobopt") == 0) 12225 { 12226 mips_opts.nobopt = 1; 12227 } 12228 else if (strcmp (name, "mips16") == 0 12229 || strcmp (name, "MIPS-16") == 0) 12230 mips_opts.mips16 = 1; 12231 else if (strcmp (name, "nomips16") == 0 12232 || strcmp (name, "noMIPS-16") == 0) 12233 mips_opts.mips16 = 0; 12234 else if (strcmp (name, "mips3d") == 0) 12235 mips_opts.ase_mips3d = 1; 12236 else if (strcmp (name, "nomips3d") == 0) 12237 mips_opts.ase_mips3d = 0; 12238 else if (strcmp (name, "mdmx") == 0) 12239 mips_opts.ase_mdmx = 1; 12240 else if (strcmp (name, "nomdmx") == 0) 12241 mips_opts.ase_mdmx = 0; 12242 else if (strcmp (name, "dsp") == 0) 12243 mips_opts.ase_dsp = 1; 12244 else if (strcmp (name, "nodsp") == 0) 12245 mips_opts.ase_dsp = 0; 12246 else if (strcmp (name, "mt") == 0) 12247 mips_opts.ase_mt = 1; 12248 else if (strcmp (name, "nomt") == 0) 12249 mips_opts.ase_mt = 0; 12250 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0) 12251 { 12252 int reset = 0; 12253 12254 /* Permit the user to change the ISA and architecture on the fly. 12255 Needless to say, misuse can cause serious problems. */ 12256 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0) 12257 { 12258 reset = 1; 12259 mips_opts.isa = file_mips_isa; 12260 mips_opts.arch = file_mips_arch; 12261 } 12262 else if (strncmp (name, "arch=", 5) == 0) 12263 { 12264 const struct mips_cpu_info *p; 12265 12266 p = mips_parse_cpu("internal use", name + 5); 12267 if (!p) 12268 as_bad (_("unknown architecture %s"), name + 5); 12269 else 12270 { 12271 mips_opts.arch = p->cpu; 12272 mips_opts.isa = p->isa; 12273 } 12274 } 12275 else if (strncmp (name, "mips", 4) == 0) 12276 { 12277 const struct mips_cpu_info *p; 12278 12279 p = mips_parse_cpu("internal use", name); 12280 if (!p) 12281 as_bad (_("unknown ISA level %s"), name + 4); 12282 else 12283 { 12284 mips_opts.arch = p->cpu; 12285 mips_opts.isa = p->isa; 12286 } 12287 } 12288 else 12289 as_bad (_("unknown ISA or architecture %s"), name); 12290 12291 switch (mips_opts.isa) 12292 { 12293 case 0: 12294 break; 12295 case ISA_MIPS1: 12296 case ISA_MIPS2: 12297 case ISA_MIPS32: 12298 case ISA_MIPS32R2: 12299 mips_opts.gp32 = 1; 12300 mips_opts.fp32 = 1; 12301 break; 12302 case ISA_MIPS3: 12303 case ISA_MIPS4: 12304 case ISA_MIPS5: 12305 case ISA_MIPS64: 12306 case ISA_MIPS64R2: 12307 mips_opts.gp32 = 0; 12308 mips_opts.fp32 = 0; 12309 break; 12310 default: 12311 as_bad (_("unknown ISA level %s"), name + 4); 12312 break; 12313 } 12314 if (reset) 12315 { 12316 mips_opts.gp32 = file_mips_gp32; 12317 mips_opts.fp32 = file_mips_fp32; 12318 } 12319 } 12320 else if (strcmp (name, "autoextend") == 0) 12321 mips_opts.noautoextend = 0; 12322 else if (strcmp (name, "noautoextend") == 0) 12323 mips_opts.noautoextend = 1; 12324 else if (strcmp (name, "push") == 0) 12325 { 12326 struct mips_option_stack *s; 12327 12328 s = (struct mips_option_stack *) xmalloc (sizeof *s); 12329 s->next = mips_opts_stack; 12330 s->options = mips_opts; 12331 mips_opts_stack = s; 12332 } 12333 else if (strcmp (name, "pop") == 0) 12334 { 12335 struct mips_option_stack *s; 12336 12337 s = mips_opts_stack; 12338 if (s == NULL) 12339 as_bad (_(".set pop with no .set push")); 12340 else 12341 { 12342 /* If we're changing the reorder mode we need to handle 12343 delay slots correctly. */ 12344 if (s->options.noreorder && ! mips_opts.noreorder) 12345 start_noreorder (); 12346 else if (! s->options.noreorder && mips_opts.noreorder) 12347 end_noreorder (); 12348 12349 mips_opts = s->options; 12350 mips_opts_stack = s->next; 12351 free (s); 12352 } 12353 } 12354 else if (strcmp (name, "sym32") == 0) 12355 mips_opts.sym32 = TRUE; 12356 else if (strcmp (name, "nosym32") == 0) 12357 mips_opts.sym32 = FALSE; 12358 else if (strcmp (name, "hardfloat") == 0) 12359 /* ignored */; 12360 else 12361 { 12362 as_warn (_("Tried to set unrecognized symbol: %s\n"), name); 12363 } 12364 *input_line_pointer = ch; 12365 demand_empty_rest_of_line (); 12366 } 12367 12368 /* Handle the .abicalls pseudo-op. I believe this is equivalent to 12369 .option pic2. It means to generate SVR4 PIC calls. */ 12370 12371 static void 12372 s_abicalls (int ignore ATTRIBUTE_UNUSED) 12373 { 12374 mips_pic = SVR4_PIC; 12375 mips_abicalls = TRUE; 12376 12377 if (g_switch_seen && g_switch_value != 0) 12378 as_warn (_("-G may not be used with SVR4 PIC code")); 12379 g_switch_value = 0; 12380 12381 bfd_set_gp_size (stdoutput, 0); 12382 demand_empty_rest_of_line (); 12383 } 12384 12385 /* Handle the .cpload pseudo-op. This is used when generating SVR4 12386 PIC code. It sets the $gp register for the function based on the 12387 function address, which is in the register named in the argument. 12388 This uses a relocation against _gp_disp, which is handled specially 12389 by the linker. The result is: 12390 lui $gp,%hi(_gp_disp) 12391 addiu $gp,$gp,%lo(_gp_disp) 12392 addu $gp,$gp,.cpload argument 12393 The .cpload argument is normally $25 == $t9. 12394 12395 The -mno-shared option changes this to: 12396 lui $gp,%hi(__gnu_local_gp) 12397 addiu $gp,$gp,%lo(__gnu_local_gp) 12398 and the argument is ignored. This saves an instruction, but the 12399 resulting code is not position independent; it uses an absolute 12400 address for __gnu_local_gp. Thus code assembled with -mno-shared 12401 can go into an ordinary executable, but not into a shared library. */ 12402 12403 static void 12404 s_cpload (int ignore ATTRIBUTE_UNUSED) 12405 { 12406 expressionS ex; 12407 int reg; 12408 int in_shared; 12409 12410 /* If we are not generating SVR4 PIC code, or if this is NewABI code, 12411 .cpload is ignored. */ 12412 if (mips_pic != SVR4_PIC || HAVE_NEWABI) 12413 { 12414 s_ignore (0); 12415 return; 12416 } 12417 12418 /* .cpload should be in a .set noreorder section. */ 12419 if (mips_opts.noreorder == 0) 12420 as_warn (_(".cpload not in noreorder section")); 12421 12422 reg = tc_get_register (0); 12423 12424 /* If we need to produce a 64-bit address, we are better off using 12425 the default instruction sequence. */ 12426 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS; 12427 12428 ex.X_op = O_symbol; 12429 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" : 12430 "__gnu_local_gp"); 12431 ex.X_op_symbol = NULL; 12432 ex.X_add_number = 0; 12433 12434 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */ 12435 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT; 12436 12437 macro_start (); 12438 macro_build_lui (&ex, mips_gp_register); 12439 macro_build (&ex, "addiu", "t,r,j", mips_gp_register, 12440 mips_gp_register, BFD_RELOC_LO16); 12441 if (in_shared) 12442 macro_build (NULL, "addu", "d,v,t", mips_gp_register, 12443 mips_gp_register, reg); 12444 macro_end (); 12445 12446 demand_empty_rest_of_line (); 12447 } 12448 12449 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is: 12450 .cpsetup $reg1, offset|$reg2, label 12451 12452 If offset is given, this results in: 12453 sd $gp, offset($sp) 12454 lui $gp, %hi(%neg(%gp_rel(label))) 12455 addiu $gp, $gp, %lo(%neg(%gp_rel(label))) 12456 daddu $gp, $gp, $reg1 12457 12458 If $reg2 is given, this results in: 12459 daddu $reg2, $gp, $0 12460 lui $gp, %hi(%neg(%gp_rel(label))) 12461 addiu $gp, $gp, %lo(%neg(%gp_rel(label))) 12462 daddu $gp, $gp, $reg1 12463 $reg1 is normally $25 == $t9. 12464 12465 The -mno-shared option replaces the last three instructions with 12466 lui $gp,%hi(_gp) 12467 addiu $gp,$gp,%lo(_gp) 12468 */ 12469 12470 static void 12471 s_cpsetup (int ignore ATTRIBUTE_UNUSED) 12472 { 12473 expressionS ex_off; 12474 expressionS ex_sym; 12475 int reg1; 12476 12477 /* If we are not generating SVR4 PIC code, .cpsetup is ignored. 12478 We also need NewABI support. */ 12479 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI) 12480 { 12481 s_ignore (0); 12482 return; 12483 } 12484 12485 reg1 = tc_get_register (0); 12486 SKIP_WHITESPACE (); 12487 if (*input_line_pointer != ',') 12488 { 12489 as_bad (_("missing argument separator ',' for .cpsetup")); 12490 return; 12491 } 12492 else 12493 ++input_line_pointer; 12494 SKIP_WHITESPACE (); 12495 if (*input_line_pointer == '$') 12496 { 12497 mips_cpreturn_register = tc_get_register (0); 12498 mips_cpreturn_offset = -1; 12499 } 12500 else 12501 { 12502 mips_cpreturn_offset = get_absolute_expression (); 12503 mips_cpreturn_register = -1; 12504 } 12505 SKIP_WHITESPACE (); 12506 if (*input_line_pointer != ',') 12507 { 12508 as_bad (_("missing argument separator ',' for .cpsetup")); 12509 return; 12510 } 12511 else 12512 ++input_line_pointer; 12513 SKIP_WHITESPACE (); 12514 expression (&ex_sym); 12515 12516 macro_start (); 12517 if (mips_cpreturn_register == -1) 12518 { 12519 ex_off.X_op = O_constant; 12520 ex_off.X_add_symbol = NULL; 12521 ex_off.X_op_symbol = NULL; 12522 ex_off.X_add_number = mips_cpreturn_offset; 12523 12524 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register, 12525 BFD_RELOC_LO16, SP); 12526 } 12527 else 12528 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register, 12529 mips_gp_register, 0); 12530 12531 if (mips_in_shared || HAVE_64BIT_SYMBOLS) 12532 { 12533 macro_build (&ex_sym, "lui", "t,u", mips_gp_register, 12534 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB, 12535 BFD_RELOC_HI16_S); 12536 12537 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register, 12538 mips_gp_register, -1, BFD_RELOC_GPREL16, 12539 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16); 12540 12541 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register, 12542 mips_gp_register, reg1); 12543 } 12544 else 12545 { 12546 expressionS ex; 12547 12548 ex.X_op = O_symbol; 12549 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp"); 12550 ex.X_op_symbol = NULL; 12551 ex.X_add_number = 0; 12552 12553 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */ 12554 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT; 12555 12556 macro_build_lui (&ex, mips_gp_register); 12557 macro_build (&ex, "addiu", "t,r,j", mips_gp_register, 12558 mips_gp_register, BFD_RELOC_LO16); 12559 } 12560 12561 macro_end (); 12562 12563 demand_empty_rest_of_line (); 12564 } 12565 12566 static void 12567 s_cplocal (int ignore ATTRIBUTE_UNUSED) 12568 { 12569 /* If we are not generating SVR4 PIC code, or if this is not NewABI code, 12570 .cplocal is ignored. */ 12571 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI) 12572 { 12573 s_ignore (0); 12574 return; 12575 } 12576 12577 mips_gp_register = tc_get_register (0); 12578 demand_empty_rest_of_line (); 12579 } 12580 12581 /* Handle the .cprestore pseudo-op. This stores $gp into a given 12582 offset from $sp. The offset is remembered, and after making a PIC 12583 call $gp is restored from that location. */ 12584 12585 static void 12586 s_cprestore (int ignore ATTRIBUTE_UNUSED) 12587 { 12588 expressionS ex; 12589 12590 /* If we are not generating SVR4 PIC code, or if this is NewABI code, 12591 .cprestore is ignored. */ 12592 if (mips_pic != SVR4_PIC || HAVE_NEWABI) 12593 { 12594 s_ignore (0); 12595 return; 12596 } 12597 12598 mips_cprestore_offset = get_absolute_expression (); 12599 mips_cprestore_valid = 1; 12600 12601 ex.X_op = O_constant; 12602 ex.X_add_symbol = NULL; 12603 ex.X_op_symbol = NULL; 12604 ex.X_add_number = mips_cprestore_offset; 12605 12606 macro_start (); 12607 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register, 12608 SP, HAVE_64BIT_ADDRESSES); 12609 macro_end (); 12610 12611 demand_empty_rest_of_line (); 12612 } 12613 12614 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset 12615 was given in the preceding .cpsetup, it results in: 12616 ld $gp, offset($sp) 12617 12618 If a register $reg2 was given there, it results in: 12619 daddu $gp, $reg2, $0 12620 */ 12621 static void 12622 s_cpreturn (int ignore ATTRIBUTE_UNUSED) 12623 { 12624 expressionS ex; 12625 12626 /* If we are not generating SVR4 PIC code, .cpreturn is ignored. 12627 We also need NewABI support. */ 12628 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI) 12629 { 12630 s_ignore (0); 12631 return; 12632 } 12633 12634 macro_start (); 12635 if (mips_cpreturn_register == -1) 12636 { 12637 ex.X_op = O_constant; 12638 ex.X_add_symbol = NULL; 12639 ex.X_op_symbol = NULL; 12640 ex.X_add_number = mips_cpreturn_offset; 12641 12642 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP); 12643 } 12644 else 12645 macro_build (NULL, "daddu", "d,v,t", mips_gp_register, 12646 mips_cpreturn_register, 0); 12647 macro_end (); 12648 12649 demand_empty_rest_of_line (); 12650 } 12651 12652 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC 12653 code. It sets the offset to use in gp_rel relocations. */ 12654 12655 static void 12656 s_gpvalue (int ignore ATTRIBUTE_UNUSED) 12657 { 12658 /* If we are not generating SVR4 PIC code, .gpvalue is ignored. 12659 We also need NewABI support. */ 12660 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI) 12661 { 12662 s_ignore (0); 12663 return; 12664 } 12665 12666 mips_gprel_offset = get_absolute_expression (); 12667 12668 demand_empty_rest_of_line (); 12669 } 12670 12671 /* Handle the .gpword pseudo-op. This is used when generating PIC 12672 code. It generates a 32 bit GP relative reloc. */ 12673 12674 static void 12675 s_gpword (int ignore ATTRIBUTE_UNUSED) 12676 { 12677 symbolS *label; 12678 expressionS ex; 12679 char *p; 12680 12681 /* When not generating PIC code, this is treated as .word. */ 12682 if (mips_pic != SVR4_PIC) 12683 { 12684 s_cons (2); 12685 return; 12686 } 12687 12688 label = insn_labels != NULL ? insn_labels->label : NULL; 12689 mips_emit_delays (); 12690 if (auto_align) 12691 mips_align (2, 0, label); 12692 mips_clear_insn_labels (); 12693 12694 expression (&ex); 12695 12696 if (ex.X_op != O_symbol || ex.X_add_number != 0) 12697 { 12698 as_bad (_("Unsupported use of .gpword")); 12699 ignore_rest_of_line (); 12700 } 12701 12702 p = frag_more (4); 12703 md_number_to_chars (p, 0, 4); 12704 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE, 12705 BFD_RELOC_GPREL32); 12706 12707 demand_empty_rest_of_line (); 12708 } 12709 12710 static void 12711 s_gpdword (int ignore ATTRIBUTE_UNUSED) 12712 { 12713 symbolS *label; 12714 expressionS ex; 12715 char *p; 12716 12717 /* When not generating PIC code, this is treated as .dword. */ 12718 if (mips_pic != SVR4_PIC) 12719 { 12720 s_cons (3); 12721 return; 12722 } 12723 12724 label = insn_labels != NULL ? insn_labels->label : NULL; 12725 mips_emit_delays (); 12726 if (auto_align) 12727 mips_align (3, 0, label); 12728 mips_clear_insn_labels (); 12729 12730 expression (&ex); 12731 12732 if (ex.X_op != O_symbol || ex.X_add_number != 0) 12733 { 12734 as_bad (_("Unsupported use of .gpdword")); 12735 ignore_rest_of_line (); 12736 } 12737 12738 p = frag_more (8); 12739 md_number_to_chars (p, 0, 8); 12740 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE, 12741 BFD_RELOC_GPREL32)->fx_tcbit = 1; 12742 12743 /* GPREL32 composed with 64 gives a 64-bit GP offset. */ 12744 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0, 12745 FALSE, BFD_RELOC_64)->fx_tcbit = 1; 12746 12747 demand_empty_rest_of_line (); 12748 } 12749 12750 /* Handle the .cpadd pseudo-op. This is used when dealing with switch 12751 tables in SVR4 PIC code. */ 12752 12753 static void 12754 s_cpadd (int ignore ATTRIBUTE_UNUSED) 12755 { 12756 int reg; 12757 12758 /* This is ignored when not generating SVR4 PIC code. */ 12759 if (mips_pic != SVR4_PIC) 12760 { 12761 s_ignore (0); 12762 return; 12763 } 12764 12765 /* Add $gp to the register named as an argument. */ 12766 macro_start (); 12767 reg = tc_get_register (0); 12768 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register); 12769 macro_end (); 12770 12771 demand_empty_rest_of_line (); 12772 } 12773 12774 /* Handle the .insn pseudo-op. This marks instruction labels in 12775 mips16 mode. This permits the linker to handle them specially, 12776 such as generating jalx instructions when needed. We also make 12777 them odd for the duration of the assembly, in order to generate the 12778 right sort of code. We will make them even in the adjust_symtab 12779 routine, while leaving them marked. This is convenient for the 12780 debugger and the disassembler. The linker knows to make them odd 12781 again. */ 12782 12783 static void 12784 s_insn (int ignore ATTRIBUTE_UNUSED) 12785 { 12786 mips16_mark_labels (); 12787 12788 demand_empty_rest_of_line (); 12789 } 12790 12791 /* Handle a .stabn directive. We need these in order to mark a label 12792 as being a mips16 text label correctly. Sometimes the compiler 12793 will emit a label, followed by a .stabn, and then switch sections. 12794 If the label and .stabn are in mips16 mode, then the label is 12795 really a mips16 text label. */ 12796 12797 static void 12798 s_mips_stab (int type) 12799 { 12800 if (type == 'n') 12801 mips16_mark_labels (); 12802 12803 s_stab (type); 12804 } 12805 12806 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich. 12807 */ 12808 12809 static void 12810 s_mips_weakext (int ignore ATTRIBUTE_UNUSED) 12811 { 12812 char *name; 12813 int c; 12814 symbolS *symbolP; 12815 expressionS exp; 12816 12817 name = input_line_pointer; 12818 c = get_symbol_end (); 12819 symbolP = symbol_find_or_make (name); 12820 S_SET_WEAK (symbolP); 12821 *input_line_pointer = c; 12822 12823 SKIP_WHITESPACE (); 12824 12825 if (! is_end_of_line[(unsigned char) *input_line_pointer]) 12826 { 12827 if (S_IS_DEFINED (symbolP)) 12828 { 12829 as_bad ("ignoring attempt to redefine symbol %s", 12830 S_GET_NAME (symbolP)); 12831 ignore_rest_of_line (); 12832 return; 12833 } 12834 12835 if (*input_line_pointer == ',') 12836 { 12837 ++input_line_pointer; 12838 SKIP_WHITESPACE (); 12839 } 12840 12841 expression (&exp); 12842 if (exp.X_op != O_symbol) 12843 { 12844 as_bad ("bad .weakext directive"); 12845 ignore_rest_of_line (); 12846 return; 12847 } 12848 symbol_set_value_expression (symbolP, &exp); 12849 } 12850 12851 demand_empty_rest_of_line (); 12852 } 12853 12854 /* Parse a register string into a number. Called from the ECOFF code 12855 to parse .frame. The argument is non-zero if this is the frame 12856 register, so that we can record it in mips_frame_reg. */ 12857 12858 int 12859 tc_get_register (int frame) 12860 { 12861 unsigned int reg; 12862 12863 SKIP_WHITESPACE (); 12864 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, ®)) 12865 reg = 0; 12866 if (frame) 12867 { 12868 mips_frame_reg = reg != 0 ? reg : SP; 12869 mips_frame_reg_valid = 1; 12870 mips_cprestore_valid = 0; 12871 } 12872 return reg; 12873 } 12874 12875 valueT 12876 md_section_align (asection *seg, valueT addr) 12877 { 12878 int align = bfd_get_section_alignment (stdoutput, seg); 12879 12880 #ifdef OBJ_ELF 12881 /* We don't need to align ELF sections to the full alignment. 12882 However, Irix 5 may prefer that we align them at least to a 16 12883 byte boundary. We don't bother to align the sections if we are 12884 targeted for an embedded system. */ 12885 if (strcmp (TARGET_OS, "elf") == 0) 12886 return addr; 12887 if (align > 4) 12888 align = 4; 12889 #endif 12890 12891 return ((addr + (1 << align) - 1) & (-1 << align)); 12892 } 12893 12894 /* Utility routine, called from above as well. If called while the 12895 input file is still being read, it's only an approximation. (For 12896 example, a symbol may later become defined which appeared to be 12897 undefined earlier.) */ 12898 12899 static int 12900 nopic_need_relax (symbolS *sym, int before_relaxing) 12901 { 12902 if (sym == 0) 12903 return 0; 12904 12905 if (g_switch_value > 0) 12906 { 12907 const char *symname; 12908 int change; 12909 12910 /* Find out whether this symbol can be referenced off the $gp 12911 register. It can be if it is smaller than the -G size or if 12912 it is in the .sdata or .sbss section. Certain symbols can 12913 not be referenced off the $gp, although it appears as though 12914 they can. */ 12915 symname = S_GET_NAME (sym); 12916 if (symname != (const char *) NULL 12917 && (strcmp (symname, "eprol") == 0 12918 || strcmp (symname, "etext") == 0 12919 || strcmp (symname, "_gp") == 0 12920 || strcmp (symname, "edata") == 0 12921 || strcmp (symname, "_fbss") == 0 12922 || strcmp (symname, "_fdata") == 0 12923 || strcmp (symname, "_ftext") == 0 12924 || strcmp (symname, "end") == 0 12925 || strcmp (symname, "_gp_disp") == 0)) 12926 change = 1; 12927 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym)) 12928 && (0 12929 #ifndef NO_ECOFF_DEBUGGING 12930 || (symbol_get_obj (sym)->ecoff_extern_size != 0 12931 && (symbol_get_obj (sym)->ecoff_extern_size 12932 <= g_switch_value)) 12933 #endif 12934 /* We must defer this decision until after the whole 12935 file has been read, since there might be a .extern 12936 after the first use of this symbol. */ 12937 || (before_relaxing 12938 #ifndef NO_ECOFF_DEBUGGING 12939 && symbol_get_obj (sym)->ecoff_extern_size == 0 12940 #endif 12941 && S_GET_VALUE (sym) == 0) 12942 || (S_GET_VALUE (sym) != 0 12943 && S_GET_VALUE (sym) <= g_switch_value))) 12944 change = 0; 12945 else 12946 { 12947 const char *segname; 12948 12949 segname = segment_name (S_GET_SEGMENT (sym)); 12950 assert (strcmp (segname, ".lit8") != 0 12951 && strcmp (segname, ".lit4") != 0); 12952 change = (strcmp (segname, ".sdata") != 0 12953 && strcmp (segname, ".sbss") != 0 12954 && strncmp (segname, ".sdata.", 7) != 0 12955 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0); 12956 } 12957 return change; 12958 } 12959 else 12960 /* We are not optimizing for the $gp register. */ 12961 return 1; 12962 } 12963 12964 12965 /* Return true if the given symbol should be considered local for SVR4 PIC. */ 12966 12967 static bfd_boolean 12968 pic_need_relax (symbolS *sym, asection *segtype) 12969 { 12970 asection *symsec; 12971 bfd_boolean linkonce; 12972 12973 /* Handle the case of a symbol equated to another symbol. */ 12974 while (symbol_equated_reloc_p (sym)) 12975 { 12976 symbolS *n; 12977 12978 /* It's possible to get a loop here in a badly written 12979 program. */ 12980 n = symbol_get_value_expression (sym)->X_add_symbol; 12981 if (n == sym) 12982 break; 12983 sym = n; 12984 } 12985 12986 symsec = S_GET_SEGMENT (sym); 12987 12988 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */ 12989 linkonce = FALSE; 12990 if (symsec != segtype && ! S_IS_LOCAL (sym)) 12991 { 12992 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE) 12993 != 0) 12994 linkonce = TRUE; 12995 12996 /* The GNU toolchain uses an extension for ELF: a section 12997 beginning with the magic string .gnu.linkonce is a linkonce 12998 section. */ 12999 if (strncmp (segment_name (symsec), ".gnu.linkonce", 13000 sizeof ".gnu.linkonce" - 1) == 0) 13001 linkonce = TRUE; 13002 } 13003 13004 /* This must duplicate the test in adjust_reloc_syms. */ 13005 return (symsec != &bfd_und_section 13006 && symsec != &bfd_abs_section 13007 && ! bfd_is_com_section (symsec) 13008 && !linkonce 13009 #ifdef OBJ_ELF 13010 /* A global or weak symbol is treated as external. */ 13011 && (OUTPUT_FLAVOR != bfd_target_elf_flavour 13012 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym))) 13013 #endif 13014 ); 13015 } 13016 13017 13018 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an 13019 extended opcode. SEC is the section the frag is in. */ 13020 13021 static int 13022 mips16_extended_frag (fragS *fragp, asection *sec, long stretch) 13023 { 13024 int type; 13025 register const struct mips16_immed_operand *op; 13026 offsetT val; 13027 int mintiny, maxtiny; 13028 segT symsec; 13029 fragS *sym_frag; 13030 13031 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype)) 13032 return 0; 13033 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype)) 13034 return 1; 13035 13036 type = RELAX_MIPS16_TYPE (fragp->fr_subtype); 13037 op = mips16_immed_operands; 13038 while (op->type != type) 13039 { 13040 ++op; 13041 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED); 13042 } 13043 13044 if (op->unsp) 13045 { 13046 if (type == '<' || type == '>' || type == '[' || type == ']') 13047 { 13048 mintiny = 1; 13049 maxtiny = 1 << op->nbits; 13050 } 13051 else 13052 { 13053 mintiny = 0; 13054 maxtiny = (1 << op->nbits) - 1; 13055 } 13056 } 13057 else 13058 { 13059 mintiny = - (1 << (op->nbits - 1)); 13060 maxtiny = (1 << (op->nbits - 1)) - 1; 13061 } 13062 13063 sym_frag = symbol_get_frag (fragp->fr_symbol); 13064 val = S_GET_VALUE (fragp->fr_symbol); 13065 symsec = S_GET_SEGMENT (fragp->fr_symbol); 13066 13067 if (op->pcrel) 13068 { 13069 addressT addr; 13070 13071 /* We won't have the section when we are called from 13072 mips_relax_frag. However, we will always have been called 13073 from md_estimate_size_before_relax first. If this is a 13074 branch to a different section, we mark it as such. If SEC is 13075 NULL, and the frag is not marked, then it must be a branch to 13076 the same section. */ 13077 if (sec == NULL) 13078 { 13079 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype)) 13080 return 1; 13081 } 13082 else 13083 { 13084 /* Must have been called from md_estimate_size_before_relax. */ 13085 if (symsec != sec) 13086 { 13087 fragp->fr_subtype = 13088 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype); 13089 13090 /* FIXME: We should support this, and let the linker 13091 catch branches and loads that are out of range. */ 13092 as_bad_where (fragp->fr_file, fragp->fr_line, 13093 _("unsupported PC relative reference to different section")); 13094 13095 return 1; 13096 } 13097 if (fragp != sym_frag && sym_frag->fr_address == 0) 13098 /* Assume non-extended on the first relaxation pass. 13099 The address we have calculated will be bogus if this is 13100 a forward branch to another frag, as the forward frag 13101 will have fr_address == 0. */ 13102 return 0; 13103 } 13104 13105 /* In this case, we know for sure that the symbol fragment is in 13106 the same section. If the relax_marker of the symbol fragment 13107 differs from the relax_marker of this fragment, we have not 13108 yet adjusted the symbol fragment fr_address. We want to add 13109 in STRETCH in order to get a better estimate of the address. 13110 This particularly matters because of the shift bits. */ 13111 if (stretch != 0 13112 && sym_frag->relax_marker != fragp->relax_marker) 13113 { 13114 fragS *f; 13115 13116 /* Adjust stretch for any alignment frag. Note that if have 13117 been expanding the earlier code, the symbol may be 13118 defined in what appears to be an earlier frag. FIXME: 13119 This doesn't handle the fr_subtype field, which specifies 13120 a maximum number of bytes to skip when doing an 13121 alignment. */ 13122 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next) 13123 { 13124 if (f->fr_type == rs_align || f->fr_type == rs_align_code) 13125 { 13126 if (stretch < 0) 13127 stretch = - ((- stretch) 13128 & ~ ((1 << (int) f->fr_offset) - 1)); 13129 else 13130 stretch &= ~ ((1 << (int) f->fr_offset) - 1); 13131 if (stretch == 0) 13132 break; 13133 } 13134 } 13135 if (f != NULL) 13136 val += stretch; 13137 } 13138 13139 addr = fragp->fr_address + fragp->fr_fix; 13140 13141 /* The base address rules are complicated. The base address of 13142 a branch is the following instruction. The base address of a 13143 PC relative load or add is the instruction itself, but if it 13144 is in a delay slot (in which case it can not be extended) use 13145 the address of the instruction whose delay slot it is in. */ 13146 if (type == 'p' || type == 'q') 13147 { 13148 addr += 2; 13149 13150 /* If we are currently assuming that this frag should be 13151 extended, then, the current address is two bytes 13152 higher. */ 13153 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype)) 13154 addr += 2; 13155 13156 /* Ignore the low bit in the target, since it will be set 13157 for a text label. */ 13158 if ((val & 1) != 0) 13159 --val; 13160 } 13161 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)) 13162 addr -= 4; 13163 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype)) 13164 addr -= 2; 13165 13166 val -= addr & ~ ((1 << op->shift) - 1); 13167 13168 /* Branch offsets have an implicit 0 in the lowest bit. */ 13169 if (type == 'p' || type == 'q') 13170 val /= 2; 13171 13172 /* If any of the shifted bits are set, we must use an extended 13173 opcode. If the address depends on the size of this 13174 instruction, this can lead to a loop, so we arrange to always 13175 use an extended opcode. We only check this when we are in 13176 the main relaxation loop, when SEC is NULL. */ 13177 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL) 13178 { 13179 fragp->fr_subtype = 13180 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype); 13181 return 1; 13182 } 13183 13184 /* If we are about to mark a frag as extended because the value 13185 is precisely maxtiny + 1, then there is a chance of an 13186 infinite loop as in the following code: 13187 la $4,foo 13188 .skip 1020 13189 .align 2 13190 foo: 13191 In this case when the la is extended, foo is 0x3fc bytes 13192 away, so the la can be shrunk, but then foo is 0x400 away, so 13193 the la must be extended. To avoid this loop, we mark the 13194 frag as extended if it was small, and is about to become 13195 extended with a value of maxtiny + 1. */ 13196 if (val == ((maxtiny + 1) << op->shift) 13197 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype) 13198 && sec == NULL) 13199 { 13200 fragp->fr_subtype = 13201 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype); 13202 return 1; 13203 } 13204 } 13205 else if (symsec != absolute_section && sec != NULL) 13206 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation")); 13207 13208 if ((val & ((1 << op->shift) - 1)) != 0 13209 || val < (mintiny << op->shift) 13210 || val > (maxtiny << op->shift)) 13211 return 1; 13212 else 13213 return 0; 13214 } 13215 13216 /* Compute the length of a branch sequence, and adjust the 13217 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the 13218 worst-case length is computed, with UPDATE being used to indicate 13219 whether an unconditional (-1), branch-likely (+1) or regular (0) 13220 branch is to be computed. */ 13221 static int 13222 relaxed_branch_length (fragS *fragp, asection *sec, int update) 13223 { 13224 bfd_boolean toofar; 13225 int length; 13226 13227 if (fragp 13228 && S_IS_DEFINED (fragp->fr_symbol) 13229 && sec == S_GET_SEGMENT (fragp->fr_symbol)) 13230 { 13231 addressT addr; 13232 offsetT val; 13233 13234 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset; 13235 13236 addr = fragp->fr_address + fragp->fr_fix + 4; 13237 13238 val -= addr; 13239 13240 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2); 13241 } 13242 else if (fragp) 13243 /* If the symbol is not defined or it's in a different segment, 13244 assume the user knows what's going on and emit a short 13245 branch. */ 13246 toofar = FALSE; 13247 else 13248 toofar = TRUE; 13249 13250 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype)) 13251 fragp->fr_subtype 13252 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype), 13253 RELAX_BRANCH_LIKELY (fragp->fr_subtype), 13254 RELAX_BRANCH_LINK (fragp->fr_subtype), 13255 toofar); 13256 13257 length = 4; 13258 if (toofar) 13259 { 13260 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0)) 13261 length += 8; 13262 13263 if (mips_pic != NO_PIC) 13264 { 13265 /* Additional space for PIC loading of target address. */ 13266 length += 8; 13267 if (mips_opts.isa == ISA_MIPS1) 13268 /* Additional space for $at-stabilizing nop. */ 13269 length += 4; 13270 } 13271 13272 /* If branch is conditional. */ 13273 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0)) 13274 length += 8; 13275 } 13276 13277 return length; 13278 } 13279 13280 /* Estimate the size of a frag before relaxing. Unless this is the 13281 mips16, we are not really relaxing here, and the final size is 13282 encoded in the subtype information. For the mips16, we have to 13283 decide whether we are using an extended opcode or not. */ 13284 13285 int 13286 md_estimate_size_before_relax (fragS *fragp, asection *segtype) 13287 { 13288 int change; 13289 13290 if (RELAX_BRANCH_P (fragp->fr_subtype)) 13291 { 13292 13293 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE); 13294 13295 return fragp->fr_var; 13296 } 13297 13298 if (RELAX_MIPS16_P (fragp->fr_subtype)) 13299 /* We don't want to modify the EXTENDED bit here; it might get us 13300 into infinite loops. We change it only in mips_relax_frag(). */ 13301 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2); 13302 13303 if (mips_pic == NO_PIC) 13304 change = nopic_need_relax (fragp->fr_symbol, 0); 13305 else if (mips_pic == SVR4_PIC) 13306 change = pic_need_relax (fragp->fr_symbol, segtype); 13307 else if (mips_pic == VXWORKS_PIC) 13308 /* For vxworks, GOT16 relocations never have a corresponding LO16. */ 13309 change = 0; 13310 else 13311 abort (); 13312 13313 if (change) 13314 { 13315 fragp->fr_subtype |= RELAX_USE_SECOND; 13316 return -RELAX_FIRST (fragp->fr_subtype); 13317 } 13318 else 13319 return -RELAX_SECOND (fragp->fr_subtype); 13320 } 13321 13322 /* This is called to see whether a reloc against a defined symbol 13323 should be converted into a reloc against a section. */ 13324 13325 int 13326 mips_fix_adjustable (fixS *fixp) 13327 { 13328 /* Don't adjust MIPS16 jump relocations, so we don't have to worry 13329 about the format of the offset in the .o file. */ 13330 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP) 13331 return 0; 13332 13333 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT 13334 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY) 13335 return 0; 13336 13337 if (fixp->fx_addsy == NULL) 13338 return 1; 13339 13340 /* If symbol SYM is in a mergeable section, relocations of the form 13341 SYM + 0 can usually be made section-relative. The mergeable data 13342 is then identified by the section offset rather than by the symbol. 13343 13344 However, if we're generating REL LO16 relocations, the offset is split 13345 between the LO16 and parterning high part relocation. The linker will 13346 need to recalculate the complete offset in order to correctly identify 13347 the merge data. 13348 13349 The linker has traditionally not looked for the parterning high part 13350 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be 13351 placed anywhere. Rather than break backwards compatibility by changing 13352 this, it seems better not to force the issue, and instead keep the 13353 original symbol. This will work with either linker behavior. */ 13354 if ((fixp->fx_r_type == BFD_RELOC_LO16 13355 || fixp->fx_r_type == BFD_RELOC_MIPS16_LO16 13356 || reloc_needs_lo_p (fixp->fx_r_type)) 13357 && HAVE_IN_PLACE_ADDENDS 13358 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0) 13359 return 0; 13360 13361 #ifdef OBJ_ELF 13362 /* Don't adjust relocations against mips16 symbols, so that the linker 13363 can find them if it needs to set up a stub. */ 13364 if (OUTPUT_FLAVOR == bfd_target_elf_flavour 13365 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16 13366 && fixp->fx_subsy == NULL) 13367 return 0; 13368 #endif 13369 13370 return 1; 13371 } 13372 13373 /* Translate internal representation of relocation info to BFD target 13374 format. */ 13375 13376 arelent ** 13377 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp) 13378 { 13379 static arelent *retval[4]; 13380 arelent *reloc; 13381 bfd_reloc_code_real_type code; 13382 13383 memset (retval, 0, sizeof(retval)); 13384 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent)); 13385 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *)); 13386 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy); 13387 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where; 13388 13389 if (fixp->fx_pcrel) 13390 { 13391 assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2); 13392 13393 /* At this point, fx_addnumber is "symbol offset - pcrel address". 13394 Relocations want only the symbol offset. */ 13395 reloc->addend = fixp->fx_addnumber + reloc->address; 13396 if (OUTPUT_FLAVOR != bfd_target_elf_flavour) 13397 { 13398 /* A gruesome hack which is a result of the gruesome gas 13399 reloc handling. What's worse, for COFF (as opposed to 13400 ECOFF), we might need yet another copy of reloc->address. 13401 See bfd_install_relocation. */ 13402 reloc->addend += reloc->address; 13403 } 13404 } 13405 else 13406 reloc->addend = fixp->fx_addnumber; 13407 13408 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable 13409 entry to be used in the relocation's section offset. */ 13410 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY) 13411 { 13412 reloc->address = reloc->addend; 13413 reloc->addend = 0; 13414 } 13415 13416 code = fixp->fx_r_type; 13417 13418 reloc->howto = bfd_reloc_type_lookup (stdoutput, code); 13419 if (reloc->howto == NULL) 13420 { 13421 as_bad_where (fixp->fx_file, fixp->fx_line, 13422 _("Can not represent %s relocation in this object file format"), 13423 bfd_get_reloc_code_name (code)); 13424 retval[0] = NULL; 13425 } 13426 13427 return retval; 13428 } 13429 13430 /* Relax a machine dependent frag. This returns the amount by which 13431 the current size of the frag should change. */ 13432 13433 int 13434 mips_relax_frag (asection *sec, fragS *fragp, long stretch) 13435 { 13436 if (RELAX_BRANCH_P (fragp->fr_subtype)) 13437 { 13438 offsetT old_var = fragp->fr_var; 13439 13440 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE); 13441 13442 return fragp->fr_var - old_var; 13443 } 13444 13445 if (! RELAX_MIPS16_P (fragp->fr_subtype)) 13446 return 0; 13447 13448 if (mips16_extended_frag (fragp, NULL, stretch)) 13449 { 13450 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype)) 13451 return 0; 13452 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype); 13453 return 2; 13454 } 13455 else 13456 { 13457 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)) 13458 return 0; 13459 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype); 13460 return -2; 13461 } 13462 13463 return 0; 13464 } 13465 13466 /* Convert a machine dependent frag. */ 13467 13468 void 13469 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp) 13470 { 13471 if (RELAX_BRANCH_P (fragp->fr_subtype)) 13472 { 13473 bfd_byte *buf; 13474 unsigned long insn; 13475 expressionS exp; 13476 fixS *fixp; 13477 13478 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix; 13479 13480 if (target_big_endian) 13481 insn = bfd_getb32 (buf); 13482 else 13483 insn = bfd_getl32 (buf); 13484 13485 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype)) 13486 { 13487 /* We generate a fixup instead of applying it right now 13488 because, if there are linker relaxations, we're going to 13489 need the relocations. */ 13490 exp.X_op = O_symbol; 13491 exp.X_add_symbol = fragp->fr_symbol; 13492 exp.X_add_number = fragp->fr_offset; 13493 13494 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal, 13495 4, &exp, 1, BFD_RELOC_16_PCREL_S2); 13496 fixp->fx_file = fragp->fr_file; 13497 fixp->fx_line = fragp->fr_line; 13498 13499 md_number_to_chars ((char *) buf, insn, 4); 13500 buf += 4; 13501 } 13502 else 13503 { 13504 int i; 13505 13506 as_warn_where (fragp->fr_file, fragp->fr_line, 13507 _("relaxed out-of-range branch into a jump")); 13508 13509 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype)) 13510 goto uncond; 13511 13512 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype)) 13513 { 13514 /* Reverse the branch. */ 13515 switch ((insn >> 28) & 0xf) 13516 { 13517 case 4: 13518 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can 13519 have the condition reversed by tweaking a single 13520 bit, and their opcodes all have 0x4???????. */ 13521 assert ((insn & 0xf1000000) == 0x41000000); 13522 insn ^= 0x00010000; 13523 break; 13524 13525 case 0: 13526 /* bltz 0x04000000 bgez 0x04010000 13527 bltzal 0x04100000 bgezal 0x04110000 */ 13528 assert ((insn & 0xfc0e0000) == 0x04000000); 13529 insn ^= 0x00010000; 13530 break; 13531 13532 case 1: 13533 /* beq 0x10000000 bne 0x14000000 13534 blez 0x18000000 bgtz 0x1c000000 */ 13535 insn ^= 0x04000000; 13536 break; 13537 13538 default: 13539 abort (); 13540 } 13541 } 13542 13543 if (RELAX_BRANCH_LINK (fragp->fr_subtype)) 13544 { 13545 /* Clear the and-link bit. */ 13546 assert ((insn & 0xfc1c0000) == 0x04100000); 13547 13548 /* bltzal 0x04100000 bgezal 0x04110000 13549 bltzall 0x04120000 bgezall 0x04130000 */ 13550 insn &= ~0x00100000; 13551 } 13552 13553 /* Branch over the branch (if the branch was likely) or the 13554 full jump (not likely case). Compute the offset from the 13555 current instruction to branch to. */ 13556 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype)) 13557 i = 16; 13558 else 13559 { 13560 /* How many bytes in instructions we've already emitted? */ 13561 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix; 13562 /* How many bytes in instructions from here to the end? */ 13563 i = fragp->fr_var - i; 13564 } 13565 /* Convert to instruction count. */ 13566 i >>= 2; 13567 /* Branch counts from the next instruction. */ 13568 i--; 13569 insn |= i; 13570 /* Branch over the jump. */ 13571 md_number_to_chars ((char *) buf, insn, 4); 13572 buf += 4; 13573 13574 /* Nop */ 13575 md_number_to_chars ((char *) buf, 0, 4); 13576 buf += 4; 13577 13578 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype)) 13579 { 13580 /* beql $0, $0, 2f */ 13581 insn = 0x50000000; 13582 /* Compute the PC offset from the current instruction to 13583 the end of the variable frag. */ 13584 /* How many bytes in instructions we've already emitted? */ 13585 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix; 13586 /* How many bytes in instructions from here to the end? */ 13587 i = fragp->fr_var - i; 13588 /* Convert to instruction count. */ 13589 i >>= 2; 13590 /* Don't decrement i, because we want to branch over the 13591 delay slot. */ 13592 13593 insn |= i; 13594 md_number_to_chars ((char *) buf, insn, 4); 13595 buf += 4; 13596 13597 md_number_to_chars ((char *) buf, 0, 4); 13598 buf += 4; 13599 } 13600 13601 uncond: 13602 if (mips_pic == NO_PIC) 13603 { 13604 /* j or jal. */ 13605 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype) 13606 ? 0x0c000000 : 0x08000000); 13607 exp.X_op = O_symbol; 13608 exp.X_add_symbol = fragp->fr_symbol; 13609 exp.X_add_number = fragp->fr_offset; 13610 13611 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal, 13612 4, &exp, 0, BFD_RELOC_MIPS_JMP); 13613 fixp->fx_file = fragp->fr_file; 13614 fixp->fx_line = fragp->fr_line; 13615 13616 md_number_to_chars ((char *) buf, insn, 4); 13617 buf += 4; 13618 } 13619 else 13620 { 13621 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */ 13622 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000; 13623 exp.X_op = O_symbol; 13624 exp.X_add_symbol = fragp->fr_symbol; 13625 exp.X_add_number = fragp->fr_offset; 13626 13627 if (fragp->fr_offset) 13628 { 13629 exp.X_add_symbol = make_expr_symbol (&exp); 13630 exp.X_add_number = 0; 13631 } 13632 13633 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal, 13634 4, &exp, 0, BFD_RELOC_MIPS_GOT16); 13635 fixp->fx_file = fragp->fr_file; 13636 fixp->fx_line = fragp->fr_line; 13637 13638 md_number_to_chars ((char *) buf, insn, 4); 13639 buf += 4; 13640 13641 if (mips_opts.isa == ISA_MIPS1) 13642 { 13643 /* nop */ 13644 md_number_to_chars ((char *) buf, 0, 4); 13645 buf += 4; 13646 } 13647 13648 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */ 13649 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000; 13650 13651 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal, 13652 4, &exp, 0, BFD_RELOC_LO16); 13653 fixp->fx_file = fragp->fr_file; 13654 fixp->fx_line = fragp->fr_line; 13655 13656 md_number_to_chars ((char *) buf, insn, 4); 13657 buf += 4; 13658 13659 /* j(al)r $at. */ 13660 if (RELAX_BRANCH_LINK (fragp->fr_subtype)) 13661 insn = 0x0020f809; 13662 else 13663 insn = 0x00200008; 13664 13665 md_number_to_chars ((char *) buf, insn, 4); 13666 buf += 4; 13667 } 13668 } 13669 13670 assert (buf == (bfd_byte *)fragp->fr_literal 13671 + fragp->fr_fix + fragp->fr_var); 13672 13673 fragp->fr_fix += fragp->fr_var; 13674 13675 return; 13676 } 13677 13678 if (RELAX_MIPS16_P (fragp->fr_subtype)) 13679 { 13680 int type; 13681 register const struct mips16_immed_operand *op; 13682 bfd_boolean small, ext; 13683 offsetT val; 13684 bfd_byte *buf; 13685 unsigned long insn; 13686 bfd_boolean use_extend; 13687 unsigned short extend; 13688 13689 type = RELAX_MIPS16_TYPE (fragp->fr_subtype); 13690 op = mips16_immed_operands; 13691 while (op->type != type) 13692 ++op; 13693 13694 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype)) 13695 { 13696 small = FALSE; 13697 ext = TRUE; 13698 } 13699 else 13700 { 13701 small = TRUE; 13702 ext = FALSE; 13703 } 13704 13705 resolve_symbol_value (fragp->fr_symbol); 13706 val = S_GET_VALUE (fragp->fr_symbol); 13707 if (op->pcrel) 13708 { 13709 addressT addr; 13710 13711 addr = fragp->fr_address + fragp->fr_fix; 13712 13713 /* The rules for the base address of a PC relative reloc are 13714 complicated; see mips16_extended_frag. */ 13715 if (type == 'p' || type == 'q') 13716 { 13717 addr += 2; 13718 if (ext) 13719 addr += 2; 13720 /* Ignore the low bit in the target, since it will be 13721 set for a text label. */ 13722 if ((val & 1) != 0) 13723 --val; 13724 } 13725 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)) 13726 addr -= 4; 13727 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype)) 13728 addr -= 2; 13729 13730 addr &= ~ (addressT) ((1 << op->shift) - 1); 13731 val -= addr; 13732 13733 /* Make sure the section winds up with the alignment we have 13734 assumed. */ 13735 if (op->shift > 0) 13736 record_alignment (asec, op->shift); 13737 } 13738 13739 if (ext 13740 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype) 13741 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))) 13742 as_warn_where (fragp->fr_file, fragp->fr_line, 13743 _("extended instruction in delay slot")); 13744 13745 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix); 13746 13747 if (target_big_endian) 13748 insn = bfd_getb16 (buf); 13749 else 13750 insn = bfd_getl16 (buf); 13751 13752 mips16_immed (fragp->fr_file, fragp->fr_line, type, val, 13753 RELAX_MIPS16_USER_EXT (fragp->fr_subtype), 13754 small, ext, &insn, &use_extend, &extend); 13755 13756 if (use_extend) 13757 { 13758 md_number_to_chars ((char *) buf, 0xf000 | extend, 2); 13759 fragp->fr_fix += 2; 13760 buf += 2; 13761 } 13762 13763 md_number_to_chars ((char *) buf, insn, 2); 13764 fragp->fr_fix += 2; 13765 buf += 2; 13766 } 13767 else 13768 { 13769 int first, second; 13770 fixS *fixp; 13771 13772 first = RELAX_FIRST (fragp->fr_subtype); 13773 second = RELAX_SECOND (fragp->fr_subtype); 13774 fixp = (fixS *) fragp->fr_opcode; 13775 13776 /* Possibly emit a warning if we've chosen the longer option. */ 13777 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0) 13778 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0)) 13779 { 13780 const char *msg = macro_warning (fragp->fr_subtype); 13781 if (msg != 0) 13782 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg); 13783 } 13784 13785 /* Go through all the fixups for the first sequence. Disable them 13786 (by marking them as done) if we're going to use the second 13787 sequence instead. */ 13788 while (fixp 13789 && fixp->fx_frag == fragp 13790 && fixp->fx_where < fragp->fr_fix - second) 13791 { 13792 if (fragp->fr_subtype & RELAX_USE_SECOND) 13793 fixp->fx_done = 1; 13794 fixp = fixp->fx_next; 13795 } 13796 13797 /* Go through the fixups for the second sequence. Disable them if 13798 we're going to use the first sequence, otherwise adjust their 13799 addresses to account for the relaxation. */ 13800 while (fixp && fixp->fx_frag == fragp) 13801 { 13802 if (fragp->fr_subtype & RELAX_USE_SECOND) 13803 fixp->fx_where -= first; 13804 else 13805 fixp->fx_done = 1; 13806 fixp = fixp->fx_next; 13807 } 13808 13809 /* Now modify the frag contents. */ 13810 if (fragp->fr_subtype & RELAX_USE_SECOND) 13811 { 13812 char *start; 13813 13814 start = fragp->fr_literal + fragp->fr_fix - first - second; 13815 memmove (start, start + first, second); 13816 fragp->fr_fix -= first; 13817 } 13818 else 13819 fragp->fr_fix -= second; 13820 } 13821 } 13822 13823 #ifdef OBJ_ELF 13824 13825 /* This function is called after the relocs have been generated. 13826 We've been storing mips16 text labels as odd. Here we convert them 13827 back to even for the convenience of the debugger. */ 13828 13829 void 13830 mips_frob_file_after_relocs (void) 13831 { 13832 asymbol **syms; 13833 unsigned int count, i; 13834 13835 if (OUTPUT_FLAVOR != bfd_target_elf_flavour) 13836 return; 13837 13838 syms = bfd_get_outsymbols (stdoutput); 13839 count = bfd_get_symcount (stdoutput); 13840 for (i = 0; i < count; i++, syms++) 13841 { 13842 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16 13843 && ((*syms)->value & 1) != 0) 13844 { 13845 (*syms)->value &= ~1; 13846 /* If the symbol has an odd size, it was probably computed 13847 incorrectly, so adjust that as well. */ 13848 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0) 13849 ++elf_symbol (*syms)->internal_elf_sym.st_size; 13850 } 13851 } 13852 } 13853 13854 #endif 13855 13856 /* This function is called whenever a label is defined. It is used 13857 when handling branch delays; if a branch has a label, we assume we 13858 can not move it. */ 13859 13860 void 13861 mips_define_label (symbolS *sym) 13862 { 13863 struct insn_label_list *l; 13864 13865 if (free_insn_labels == NULL) 13866 l = (struct insn_label_list *) xmalloc (sizeof *l); 13867 else 13868 { 13869 l = free_insn_labels; 13870 free_insn_labels = l->next; 13871 } 13872 13873 l->label = sym; 13874 l->next = insn_labels; 13875 insn_labels = l; 13876 13877 #ifdef OBJ_ELF 13878 dwarf2_emit_label (sym); 13879 #endif 13880 } 13881 13882 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) 13883 13884 /* Some special processing for a MIPS ELF file. */ 13885 13886 void 13887 mips_elf_final_processing (void) 13888 { 13889 /* Write out the register information. */ 13890 if (mips_abi != N64_ABI) 13891 { 13892 Elf32_RegInfo s; 13893 13894 s.ri_gprmask = mips_gprmask; 13895 s.ri_cprmask[0] = mips_cprmask[0]; 13896 s.ri_cprmask[1] = mips_cprmask[1]; 13897 s.ri_cprmask[2] = mips_cprmask[2]; 13898 s.ri_cprmask[3] = mips_cprmask[3]; 13899 /* The gp_value field is set by the MIPS ELF backend. */ 13900 13901 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s, 13902 ((Elf32_External_RegInfo *) 13903 mips_regmask_frag)); 13904 } 13905 else 13906 { 13907 Elf64_Internal_RegInfo s; 13908 13909 s.ri_gprmask = mips_gprmask; 13910 s.ri_pad = 0; 13911 s.ri_cprmask[0] = mips_cprmask[0]; 13912 s.ri_cprmask[1] = mips_cprmask[1]; 13913 s.ri_cprmask[2] = mips_cprmask[2]; 13914 s.ri_cprmask[3] = mips_cprmask[3]; 13915 /* The gp_value field is set by the MIPS ELF backend. */ 13916 13917 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s, 13918 ((Elf64_External_RegInfo *) 13919 mips_regmask_frag)); 13920 } 13921 13922 /* Set the MIPS ELF flag bits. FIXME: There should probably be some 13923 sort of BFD interface for this. */ 13924 if (mips_any_noreorder) 13925 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER; 13926 if (mips_pic != NO_PIC) 13927 { 13928 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC; 13929 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC; 13930 } 13931 if (mips_abicalls) 13932 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC; 13933 13934 /* Set MIPS ELF flags for ASEs. */ 13935 /* We may need to define a new flag for DSP ASE, and set this flag when 13936 file_ase_dsp is true. */ 13937 /* We may need to define a new flag for MT ASE, and set this flag when 13938 file_ase_mt is true. */ 13939 if (file_ase_mips16) 13940 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16; 13941 #if 0 /* XXX FIXME */ 13942 if (file_ase_mips3d) 13943 elf_elfheader (stdoutput)->e_flags |= ???; 13944 #endif 13945 if (file_ase_mdmx) 13946 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX; 13947 13948 /* Set the MIPS ELF ABI flags. */ 13949 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32) 13950 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32; 13951 else if (mips_abi == O64_ABI) 13952 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64; 13953 else if (mips_abi == EABI_ABI) 13954 { 13955 if (!file_mips_gp32) 13956 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64; 13957 else 13958 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32; 13959 } 13960 else if (mips_abi == N32_ABI) 13961 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2; 13962 13963 /* Nothing to do for N64_ABI. */ 13964 13965 if (mips_32bitmode) 13966 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE; 13967 } 13968 13969 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */ 13970 13971 typedef struct proc { 13972 symbolS *func_sym; 13973 symbolS *func_end_sym; 13974 unsigned long reg_mask; 13975 unsigned long reg_offset; 13976 unsigned long fpreg_mask; 13977 unsigned long fpreg_offset; 13978 unsigned long frame_offset; 13979 unsigned long frame_reg; 13980 unsigned long pc_reg; 13981 } procS; 13982 13983 static procS cur_proc; 13984 static procS *cur_proc_ptr; 13985 static int numprocs; 13986 13987 /* Fill in an rs_align_code fragment. */ 13988 13989 void 13990 mips_handle_align (fragS *fragp) 13991 { 13992 if (fragp->fr_type != rs_align_code) 13993 return; 13994 13995 if (mips_opts.mips16) 13996 { 13997 static const unsigned char be_nop[] = { 0x65, 0x00 }; 13998 static const unsigned char le_nop[] = { 0x00, 0x65 }; 13999 14000 int bytes; 14001 char *p; 14002 14003 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix; 14004 p = fragp->fr_literal + fragp->fr_fix; 14005 14006 if (bytes & 1) 14007 { 14008 *p++ = 0; 14009 fragp->fr_fix++; 14010 } 14011 14012 memcpy (p, (target_big_endian ? be_nop : le_nop), 2); 14013 fragp->fr_var = 2; 14014 } 14015 14016 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */ 14017 } 14018 14019 static void 14020 md_obj_begin (void) 14021 { 14022 } 14023 14024 static void 14025 md_obj_end (void) 14026 { 14027 /* check for premature end, nesting errors, etc */ 14028 if (cur_proc_ptr) 14029 as_warn (_("missing .end at end of assembly")); 14030 } 14031 14032 static long 14033 get_number (void) 14034 { 14035 int negative = 0; 14036 long val = 0; 14037 14038 if (*input_line_pointer == '-') 14039 { 14040 ++input_line_pointer; 14041 negative = 1; 14042 } 14043 if (!ISDIGIT (*input_line_pointer)) 14044 as_bad (_("expected simple number")); 14045 if (input_line_pointer[0] == '0') 14046 { 14047 if (input_line_pointer[1] == 'x') 14048 { 14049 input_line_pointer += 2; 14050 while (ISXDIGIT (*input_line_pointer)) 14051 { 14052 val <<= 4; 14053 val |= hex_value (*input_line_pointer++); 14054 } 14055 return negative ? -val : val; 14056 } 14057 else 14058 { 14059 ++input_line_pointer; 14060 while (ISDIGIT (*input_line_pointer)) 14061 { 14062 val <<= 3; 14063 val |= *input_line_pointer++ - '0'; 14064 } 14065 return negative ? -val : val; 14066 } 14067 } 14068 if (!ISDIGIT (*input_line_pointer)) 14069 { 14070 printf (_(" *input_line_pointer == '%c' 0x%02x\n"), 14071 *input_line_pointer, *input_line_pointer); 14072 as_warn (_("invalid number")); 14073 return -1; 14074 } 14075 while (ISDIGIT (*input_line_pointer)) 14076 { 14077 val *= 10; 14078 val += *input_line_pointer++ - '0'; 14079 } 14080 return negative ? -val : val; 14081 } 14082 14083 /* The .file directive; just like the usual .file directive, but there 14084 is an initial number which is the ECOFF file index. In the non-ECOFF 14085 case .file implies DWARF-2. */ 14086 14087 static void 14088 s_mips_file (int x ATTRIBUTE_UNUSED) 14089 { 14090 static int first_file_directive = 0; 14091 14092 if (ECOFF_DEBUGGING) 14093 { 14094 get_number (); 14095 s_app_file (0); 14096 } 14097 else 14098 { 14099 char *filename; 14100 14101 filename = dwarf2_directive_file (0); 14102 14103 /* Versions of GCC up to 3.1 start files with a ".file" 14104 directive even for stabs output. Make sure that this 14105 ".file" is handled. Note that you need a version of GCC 14106 after 3.1 in order to support DWARF-2 on MIPS. */ 14107 if (filename != NULL && ! first_file_directive) 14108 { 14109 (void) new_logical_line (filename, -1); 14110 s_app_file_string (filename, 0); 14111 } 14112 first_file_directive = 1; 14113 } 14114 } 14115 14116 /* The .loc directive, implying DWARF-2. */ 14117 14118 static void 14119 s_mips_loc (int x ATTRIBUTE_UNUSED) 14120 { 14121 if (!ECOFF_DEBUGGING) 14122 dwarf2_directive_loc (0); 14123 } 14124 14125 /* The .end directive. */ 14126 14127 static void 14128 s_mips_end (int x ATTRIBUTE_UNUSED) 14129 { 14130 symbolS *p; 14131 14132 /* Following functions need their own .frame and .cprestore directives. */ 14133 mips_frame_reg_valid = 0; 14134 mips_cprestore_valid = 0; 14135 14136 if (!is_end_of_line[(unsigned char) *input_line_pointer]) 14137 { 14138 p = get_symbol (); 14139 demand_empty_rest_of_line (); 14140 } 14141 else 14142 p = NULL; 14143 14144 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0) 14145 as_warn (_(".end not in text section")); 14146 14147 if (!cur_proc_ptr) 14148 { 14149 as_warn (_(".end directive without a preceding .ent directive.")); 14150 demand_empty_rest_of_line (); 14151 return; 14152 } 14153 14154 if (p != NULL) 14155 { 14156 assert (S_GET_NAME (p)); 14157 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym))) 14158 as_warn (_(".end symbol does not match .ent symbol.")); 14159 14160 if (debug_type == DEBUG_STABS) 14161 stabs_generate_asm_endfunc (S_GET_NAME (p), 14162 S_GET_NAME (p)); 14163 } 14164 else 14165 as_warn (_(".end directive missing or unknown symbol")); 14166 14167 #ifdef OBJ_ELF 14168 /* Create an expression to calculate the size of the function. */ 14169 if (p && cur_proc_ptr) 14170 { 14171 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p); 14172 expressionS *exp = xmalloc (sizeof (expressionS)); 14173 14174 obj->size = exp; 14175 exp->X_op = O_subtract; 14176 exp->X_add_symbol = symbol_temp_new_now (); 14177 exp->X_op_symbol = p; 14178 exp->X_add_number = 0; 14179 14180 cur_proc_ptr->func_end_sym = exp->X_add_symbol; 14181 } 14182 14183 /* Generate a .pdr section. */ 14184 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING 14185 && mips_flag_pdr) 14186 { 14187 segT saved_seg = now_seg; 14188 subsegT saved_subseg = now_subseg; 14189 valueT dot; 14190 expressionS exp; 14191 char *fragp; 14192 14193 dot = frag_now_fix (); 14194 14195 #ifdef md_flush_pending_output 14196 md_flush_pending_output (); 14197 #endif 14198 14199 assert (pdr_seg); 14200 subseg_set (pdr_seg, 0); 14201 14202 /* Write the symbol. */ 14203 exp.X_op = O_symbol; 14204 exp.X_add_symbol = p; 14205 exp.X_add_number = 0; 14206 emit_expr (&exp, 4); 14207 14208 fragp = frag_more (7 * 4); 14209 14210 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4); 14211 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4); 14212 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4); 14213 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4); 14214 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4); 14215 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4); 14216 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4); 14217 14218 subseg_set (saved_seg, saved_subseg); 14219 } 14220 #endif /* OBJ_ELF */ 14221 14222 cur_proc_ptr = NULL; 14223 } 14224 14225 /* The .aent and .ent directives. */ 14226 14227 static void 14228 s_mips_ent (int aent) 14229 { 14230 symbolS *symbolP; 14231 14232 symbolP = get_symbol (); 14233 if (*input_line_pointer == ',') 14234 ++input_line_pointer; 14235 SKIP_WHITESPACE (); 14236 if (ISDIGIT (*input_line_pointer) 14237 || *input_line_pointer == '-') 14238 get_number (); 14239 14240 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0) 14241 as_warn (_(".ent or .aent not in text section.")); 14242 14243 if (!aent && cur_proc_ptr) 14244 as_warn (_("missing .end")); 14245 14246 if (!aent) 14247 { 14248 /* This function needs its own .frame and .cprestore directives. */ 14249 mips_frame_reg_valid = 0; 14250 mips_cprestore_valid = 0; 14251 14252 cur_proc_ptr = &cur_proc; 14253 memset (cur_proc_ptr, '\0', sizeof (procS)); 14254 14255 cur_proc_ptr->func_sym = symbolP; 14256 14257 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION; 14258 14259 ++numprocs; 14260 14261 if (debug_type == DEBUG_STABS) 14262 stabs_generate_asm_func (S_GET_NAME (symbolP), 14263 S_GET_NAME (symbolP)); 14264 } 14265 14266 demand_empty_rest_of_line (); 14267 } 14268 14269 /* The .frame directive. If the mdebug section is present (IRIX 5 native) 14270 then ecoff.c (ecoff_directive_frame) is used. For embedded targets, 14271 s_mips_frame is used so that we can set the PDR information correctly. 14272 We can't use the ecoff routines because they make reference to the ecoff 14273 symbol table (in the mdebug section). */ 14274 14275 static void 14276 s_mips_frame (int ignore ATTRIBUTE_UNUSED) 14277 { 14278 #ifdef OBJ_ELF 14279 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING) 14280 { 14281 long val; 14282 14283 if (cur_proc_ptr == (procS *) NULL) 14284 { 14285 as_warn (_(".frame outside of .ent")); 14286 demand_empty_rest_of_line (); 14287 return; 14288 } 14289 14290 cur_proc_ptr->frame_reg = tc_get_register (1); 14291 14292 SKIP_WHITESPACE (); 14293 if (*input_line_pointer++ != ',' 14294 || get_absolute_expression_and_terminator (&val) != ',') 14295 { 14296 as_warn (_("Bad .frame directive")); 14297 --input_line_pointer; 14298 demand_empty_rest_of_line (); 14299 return; 14300 } 14301 14302 cur_proc_ptr->frame_offset = val; 14303 cur_proc_ptr->pc_reg = tc_get_register (0); 14304 14305 demand_empty_rest_of_line (); 14306 } 14307 else 14308 #endif /* OBJ_ELF */ 14309 s_ignore (ignore); 14310 } 14311 14312 /* The .fmask and .mask directives. If the mdebug section is present 14313 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For 14314 embedded targets, s_mips_mask is used so that we can set the PDR 14315 information correctly. We can't use the ecoff routines because they 14316 make reference to the ecoff symbol table (in the mdebug section). */ 14317 14318 static void 14319 s_mips_mask (int reg_type) 14320 { 14321 #ifdef OBJ_ELF 14322 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING) 14323 { 14324 long mask, off; 14325 14326 if (cur_proc_ptr == (procS *) NULL) 14327 { 14328 as_warn (_(".mask/.fmask outside of .ent")); 14329 demand_empty_rest_of_line (); 14330 return; 14331 } 14332 14333 if (get_absolute_expression_and_terminator (&mask) != ',') 14334 { 14335 as_warn (_("Bad .mask/.fmask directive")); 14336 --input_line_pointer; 14337 demand_empty_rest_of_line (); 14338 return; 14339 } 14340 14341 off = get_absolute_expression (); 14342 14343 if (reg_type == 'F') 14344 { 14345 cur_proc_ptr->fpreg_mask = mask; 14346 cur_proc_ptr->fpreg_offset = off; 14347 } 14348 else 14349 { 14350 cur_proc_ptr->reg_mask = mask; 14351 cur_proc_ptr->reg_offset = off; 14352 } 14353 14354 demand_empty_rest_of_line (); 14355 } 14356 else 14357 #endif /* OBJ_ELF */ 14358 s_ignore (reg_type); 14359 } 14360 14361 /* A table describing all the processors gas knows about. Names are 14362 matched in the order listed. 14363 14364 To ease comparison, please keep this table in the same order as 14365 gcc's mips_cpu_info_table[]. */ 14366 static const struct mips_cpu_info mips_cpu_info_table[] = 14367 { 14368 /* Entries for generic ISAs */ 14369 { "mips1", MIPS_CPU_IS_ISA, ISA_MIPS1, CPU_R3000 }, 14370 { "mips2", MIPS_CPU_IS_ISA, ISA_MIPS2, CPU_R6000 }, 14371 { "mips3", MIPS_CPU_IS_ISA, ISA_MIPS3, CPU_R4000 }, 14372 { "mips4", MIPS_CPU_IS_ISA, ISA_MIPS4, CPU_R8000 }, 14373 { "mips5", MIPS_CPU_IS_ISA, ISA_MIPS5, CPU_MIPS5 }, 14374 { "mips32", MIPS_CPU_IS_ISA, ISA_MIPS32, CPU_MIPS32 }, 14375 { "mips32r2", MIPS_CPU_IS_ISA, ISA_MIPS32R2, CPU_MIPS32R2 }, 14376 { "mips64", MIPS_CPU_IS_ISA, ISA_MIPS64, CPU_MIPS64 }, 14377 { "mips64r2", MIPS_CPU_IS_ISA, ISA_MIPS64R2, CPU_MIPS64R2 }, 14378 14379 /* MIPS I */ 14380 { "r3000", 0, ISA_MIPS1, CPU_R3000 }, 14381 { "r2000", 0, ISA_MIPS1, CPU_R3000 }, 14382 { "r3900", 0, ISA_MIPS1, CPU_R3900 }, 14383 14384 /* MIPS II */ 14385 { "r6000", 0, ISA_MIPS2, CPU_R6000 }, 14386 14387 /* MIPS III */ 14388 { "r4000", 0, ISA_MIPS3, CPU_R4000 }, 14389 { "r4010", 0, ISA_MIPS2, CPU_R4010 }, 14390 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 }, 14391 { "vr4111", 0, ISA_MIPS3, CPU_R4111 }, 14392 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 }, 14393 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 }, 14394 { "vr4181", 0, ISA_MIPS3, CPU_R4111 }, 14395 { "vr4300", 0, ISA_MIPS3, CPU_R4300 }, 14396 { "r4400", 0, ISA_MIPS3, CPU_R4400 }, 14397 { "r4600", 0, ISA_MIPS3, CPU_R4600 }, 14398 { "orion", 0, ISA_MIPS3, CPU_R4600 }, 14399 { "r4650", 0, ISA_MIPS3, CPU_R4650 }, 14400 14401 /* XXX: First step to full Loongson support. */ 14402 /* Not just a mips3, not quite anything else. */ 14403 /* Comment out for now to avoid build errors. 14404 { "loongson2f", 0, ISA_L2F, CPU_L2F }, 14405 */ 14406 14407 /* Cavium Networks Octeon CPU core */ 14408 { "octeon", 0, ISA_MIPS64R2, CPU_OCTEON }, 14409 14410 /* MIPS IV */ 14411 { "r8000", 0, ISA_MIPS4, CPU_R8000 }, 14412 { "r10000", 0, ISA_MIPS4, CPU_R10000 }, 14413 { "r12000", 0, ISA_MIPS4, CPU_R12000 }, 14414 { "vr5000", 0, ISA_MIPS4, CPU_R5000 }, 14415 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 }, 14416 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 }, 14417 { "rm5200", 0, ISA_MIPS4, CPU_R5000 }, 14418 { "rm5230", 0, ISA_MIPS4, CPU_R5000 }, 14419 { "rm5231", 0, ISA_MIPS4, CPU_R5000 }, 14420 { "rm5261", 0, ISA_MIPS4, CPU_R5000 }, 14421 { "rm5721", 0, ISA_MIPS4, CPU_R5000 }, 14422 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 }, 14423 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 }, 14424 14425 /* MIPS 32 */ 14426 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 }, 14427 { "4km", 0, ISA_MIPS32, CPU_MIPS32 }, 14428 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 }, 14429 14430 /* MIPS32 Release 2 */ 14431 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 }, 14432 { "24k", 0, ISA_MIPS32R2, CPU_MIPS32R2 }, 14433 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 }, 14434 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 }, 14435 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 }, 14436 14437 /* MIPS 64 */ 14438 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 }, 14439 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 }, 14440 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 }, 14441 14442 /* Broadcom SB-1 CPU core */ 14443 { "sb1", 0, ISA_MIPS64, CPU_SB1 }, 14444 14445 /* End marker */ 14446 { NULL, 0, 0, 0 } 14447 }; 14448 14449 14450 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL 14451 with a final "000" replaced by "k". Ignore case. 14452 14453 Note: this function is shared between GCC and GAS. */ 14454 14455 static bfd_boolean 14456 mips_strict_matching_cpu_name_p (const char *canonical, const char *given) 14457 { 14458 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical)) 14459 given++, canonical++; 14460 14461 return ((*given == 0 && *canonical == 0) 14462 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0)); 14463 } 14464 14465 14466 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied 14467 CPU name. We've traditionally allowed a lot of variation here. 14468 14469 Note: this function is shared between GCC and GAS. */ 14470 14471 static bfd_boolean 14472 mips_matching_cpu_name_p (const char *canonical, const char *given) 14473 { 14474 /* First see if the name matches exactly, or with a final "000" 14475 turned into "k". */ 14476 if (mips_strict_matching_cpu_name_p (canonical, given)) 14477 return TRUE; 14478 14479 /* If not, try comparing based on numerical designation alone. 14480 See if GIVEN is an unadorned number, or 'r' followed by a number. */ 14481 if (TOLOWER (*given) == 'r') 14482 given++; 14483 if (!ISDIGIT (*given)) 14484 return FALSE; 14485 14486 /* Skip over some well-known prefixes in the canonical name, 14487 hoping to find a number there too. */ 14488 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r') 14489 canonical += 2; 14490 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm') 14491 canonical += 2; 14492 else if (TOLOWER (canonical[0]) == 'r') 14493 canonical += 1; 14494 14495 return mips_strict_matching_cpu_name_p (canonical, given); 14496 } 14497 14498 14499 /* Parse an option that takes the name of a processor as its argument. 14500 OPTION is the name of the option and CPU_STRING is the argument. 14501 Return the corresponding processor enumeration if the CPU_STRING is 14502 recognized, otherwise report an error and return null. 14503 14504 A similar function exists in GCC. */ 14505 14506 static const struct mips_cpu_info * 14507 mips_parse_cpu (const char *option, const char *cpu_string) 14508 { 14509 const struct mips_cpu_info *p; 14510 14511 /* 'from-abi' selects the most compatible architecture for the given 14512 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the 14513 EABIs, we have to decide whether we're using the 32-bit or 64-bit 14514 version. Look first at the -mgp options, if given, otherwise base 14515 the choice on MIPS_DEFAULT_64BIT. 14516 14517 Treat NO_ABI like the EABIs. One reason to do this is that the 14518 plain 'mips' and 'mips64' configs have 'from-abi' as their default 14519 architecture. This code picks MIPS I for 'mips' and MIPS III for 14520 'mips64', just as we did in the days before 'from-abi'. */ 14521 if (strcasecmp (cpu_string, "from-abi") == 0) 14522 { 14523 if (ABI_NEEDS_32BIT_REGS (mips_abi)) 14524 return mips_cpu_info_from_isa (ISA_MIPS1); 14525 14526 if (ABI_NEEDS_64BIT_REGS (mips_abi)) 14527 return mips_cpu_info_from_isa (ISA_MIPS3); 14528 14529 if (file_mips_gp32 >= 0) 14530 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3); 14531 14532 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT 14533 ? ISA_MIPS3 14534 : ISA_MIPS1); 14535 } 14536 14537 /* 'default' has traditionally been a no-op. Probably not very useful. */ 14538 if (strcasecmp (cpu_string, "default") == 0) 14539 return 0; 14540 14541 for (p = mips_cpu_info_table; p->name != 0; p++) 14542 if (mips_matching_cpu_name_p (p->name, cpu_string)) 14543 return p; 14544 14545 as_bad ("Bad value (%s) for %s", cpu_string, option); 14546 return 0; 14547 } 14548 14549 /* Return the canonical processor information for ISA (a member of the 14550 ISA_MIPS* enumeration). */ 14551 14552 static const struct mips_cpu_info * 14553 mips_cpu_info_from_isa (int isa) 14554 { 14555 int i; 14556 14557 for (i = 0; mips_cpu_info_table[i].name != NULL; i++) 14558 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA) 14559 && isa == mips_cpu_info_table[i].isa) 14560 return (&mips_cpu_info_table[i]); 14561 14562 return NULL; 14563 } 14564 14565 static const struct mips_cpu_info * 14566 mips_cpu_info_from_arch (int arch) 14567 { 14568 int i; 14569 14570 for (i = 0; mips_cpu_info_table[i].name != NULL; i++) 14571 if (arch == mips_cpu_info_table[i].cpu) 14572 return (&mips_cpu_info_table[i]); 14573 14574 return NULL; 14575 } 14576 14577 static void 14578 show (FILE *stream, const char *string, int *col_p, int *first_p) 14579 { 14580 if (*first_p) 14581 { 14582 fprintf (stream, "%24s", ""); 14583 *col_p = 24; 14584 } 14585 else 14586 { 14587 fprintf (stream, ", "); 14588 *col_p += 2; 14589 } 14590 14591 if (*col_p + strlen (string) > 72) 14592 { 14593 fprintf (stream, "\n%24s", ""); 14594 *col_p = 24; 14595 } 14596 14597 fprintf (stream, "%s", string); 14598 *col_p += strlen (string); 14599 14600 *first_p = 0; 14601 } 14602 14603 void 14604 md_show_usage (FILE *stream) 14605 { 14606 int column, first; 14607 size_t i; 14608 14609 fprintf (stream, _("\ 14610 MIPS options:\n\ 14611 -EB generate big endian output\n\ 14612 -EL generate little endian output\n\ 14613 -g, -g2 do not remove unneeded NOPs or swap branches\n\ 14614 -G NUM allow referencing objects up to NUM bytes\n\ 14615 implicitly with the gp register [default 8]\n")); 14616 fprintf (stream, _("\ 14617 -mips1 generate MIPS ISA I instructions\n\ 14618 -mips2 generate MIPS ISA II instructions\n\ 14619 -mips3 generate MIPS ISA III instructions\n\ 14620 -mips4 generate MIPS ISA IV instructions\n\ 14621 -mips5 generate MIPS ISA V instructions\n\ 14622 -mips32 generate MIPS32 ISA instructions\n\ 14623 -mips32r2 generate MIPS32 release 2 ISA instructions\n\ 14624 -mips64 generate MIPS64 ISA instructions\n\ 14625 -mips64r2 generate MIPS64 release 2 ISA instructions\n\ 14626 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n")); 14627 14628 first = 1; 14629 14630 for (i = 0; mips_cpu_info_table[i].name != NULL; i++) 14631 show (stream, mips_cpu_info_table[i].name, &column, &first); 14632 show (stream, "from-abi", &column, &first); 14633 fputc ('\n', stream); 14634 14635 fprintf (stream, _("\ 14636 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\ 14637 -no-mCPU don't generate code specific to CPU.\n\ 14638 For -mCPU and -no-mCPU, CPU must be one of:\n")); 14639 14640 first = 1; 14641 14642 show (stream, "3900", &column, &first); 14643 show (stream, "4010", &column, &first); 14644 show (stream, "4100", &column, &first); 14645 show (stream, "4650", &column, &first); 14646 fputc ('\n', stream); 14647 14648 fprintf (stream, _("\ 14649 -mips16 generate mips16 instructions\n\ 14650 -no-mips16 do not generate mips16 instructions\n")); 14651 fprintf (stream, _("\ 14652 -mdsp generate DSP instructions\n\ 14653 -mno-dsp do not generate DSP instructions\n")); 14654 fprintf (stream, _("\ 14655 -mmt generate MT instructions\n\ 14656 -mno-mt do not generate MT instructions\n")); 14657 fprintf (stream, _("\ 14658 -mfix-vr4120 work around certain VR4120 errata\n\ 14659 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\ 14660 -mfix-loongson2f-btb work around Loongson2F BTB errata\n\ 14661 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\ 14662 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\ 14663 -mno-shared optimize output for executables\n\ 14664 -msym32 assume all symbols have 32-bit values\n\ 14665 -O0 remove unneeded NOPs, do not swap branches\n\ 14666 -O remove unneeded NOPs and swap branches\n\ 14667 --[no-]construct-floats [dis]allow floating point values to be constructed\n\ 14668 --trap, --no-break trap exception on div by 0 and mult overflow\n\ 14669 --break, --no-trap break exception on div by 0 and mult overflow\n")); 14670 #ifdef OBJ_ELF 14671 fprintf (stream, _("\ 14672 -KPIC, -call_shared generate SVR4 position independent code\n\ 14673 -non_shared do not generate position independent code\n\ 14674 -xgot assume a 32 bit GOT\n\ 14675 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\ 14676 -mshared, -mno-shared disable/enable .cpload optimization for\n\ 14677 non-shared code\n\ 14678 -mabi=ABI create ABI conformant object file for:\n")); 14679 14680 first = 1; 14681 14682 show (stream, "32", &column, &first); 14683 show (stream, "o64", &column, &first); 14684 show (stream, "n32", &column, &first); 14685 show (stream, "64", &column, &first); 14686 show (stream, "eabi", &column, &first); 14687 14688 fputc ('\n', stream); 14689 14690 fprintf (stream, _("\ 14691 -32 create o32 ABI object file (default)\n\ 14692 -n32 create n32 ABI object file\n\ 14693 -64 create 64 ABI object file\n")); 14694 #endif 14695 } 14696 14697 enum dwarf2_format 14698 mips_dwarf2_format (void) 14699 { 14700 if (mips_abi == N64_ABI) 14701 { 14702 #ifdef TE_IRIX 14703 return dwarf2_format_64bit_irix; 14704 #else 14705 return dwarf2_format_64bit; 14706 #endif 14707 } 14708 else 14709 return dwarf2_format_32bit; 14710 } 14711 14712 int 14713 mips_dwarf2_addr_size (void) 14714 { 14715 if (mips_abi == N64_ABI) 14716 return 8; 14717 else 14718 return 4; 14719 } 14720 14721 /* Standard calling conventions leave the CFA at SP on entry. */ 14722 void 14723 mips_cfi_frame_initial_instructions (void) 14724 { 14725 cfi_add_CFA_def_cfa_register (SP); 14726 } 14727 14728 int 14729 tc_mips_regname_to_dw2regnum (char *regname) 14730 { 14731 unsigned int regnum = -1; 14732 unsigned int reg; 14733 14734 if (reg_lookup (®name, RTYPE_GP | RTYPE_NUM, ®)) 14735 regnum = reg; 14736 14737 return regnum; 14738 } 14739