* config/tc-mips.c (append_insn): Correctly handle mips16 case
[binutils.git] / gas / config / tc-mips.c
bloba2879b12ac5546fb13c839c99ceb20dba32f2bfd
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.
9 This file is part of GAS.
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.
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.
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. */
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
31 #include <stdarg.h>
33 #include "opcode/mips.h"
34 #include "itbl-ops.h"
35 #include "dwarf2dbg.h"
36 #include "dw2gencfi.h"
38 #ifdef DEBUG
39 #define DBG(x) printf x
40 #else
41 #define DBG(x)
42 #endif
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
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
67 #if defined (OBJ_ELF)
68 #include "elf/mips.h"
69 #endif
71 #ifndef ECOFF_DEBUGGING
72 #define NO_ECOFF_DEBUGGING
73 #define ECOFF_DEBUGGING 0
74 #endif
76 int mips_flag_mdebug = -1;
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
87 #include "ecoff.h"
89 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
90 static char *mips_regmask_frag;
91 #endif
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
104 #define ILLEGAL_REG (32)
106 /* Allow override of standard little-endian ECOFF format. */
108 #ifndef ECOFF_LITTLE_FORMAT
109 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
110 #endif
112 extern int target_big_endian;
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 (), ""))
123 /* Information about an instruction, including its format, operands
124 and fixups. */
125 struct mips_cl_insn
127 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
128 const struct mips_opcode *insn_mo;
130 /* True if this is a mips16 instruction and if we want the extended
131 form of INSN_MO. */
132 bfd_boolean use_extend;
134 /* The 16-bit extension instruction to use when USE_EXTEND is true. */
135 unsigned short extend;
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;
141 /* The frag that contains the instruction. */
142 struct frag *frag;
144 /* The offset into FRAG of the first instruction byte. */
145 long where;
147 /* The relocs associated with the instruction, if any. */
148 fixS *fixp[3];
150 /* True if this entry cannot be moved from its current position. */
151 unsigned int fixed_p : 1;
153 /* True if this instruction occured in a .set noreorder block. */
154 unsigned int noreorder_p : 1;
156 /* True for mips16 instructions that jump to an absolute address. */
157 unsigned int mips16_absolute_jump_p : 1;
160 /* The ABI to use. */
161 enum mips_abi_level
163 NO_ABI = 0,
164 O32_ABI,
165 O64_ABI,
166 N32_ABI,
167 N64_ABI,
168 EABI_ABI
171 /* MIPS ABI we are using for this output file. */
172 static enum mips_abi_level mips_abi = NO_ABI;
174 /* Whether or not we have code that can call pic code. */
175 int mips_abicalls = FALSE;
177 /* Whether or not we have code which can be put into a shared
178 library. */
179 static bfd_boolean mips_in_shared = TRUE;
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. */
185 struct mips_set_options
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 /* Whether we are assembling for the mips16 processor. 0 if we are
198 not, 1 if we are, and -1 if the value has not been initialized.
199 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
200 -nomips16 command line options, and the default CPU. */
201 int mips16;
202 /* Non-zero if we should not reorder instructions. Changed by `.set
203 reorder' and `.set noreorder'. */
204 int noreorder;
205 /* Non-zero if we should not permit the $at ($1) register to be used
206 in instructions. Changed by `.set at' and `.set noat'. */
207 int noat;
208 /* Non-zero if we should warn when a macro instruction expands into
209 more than one machine instruction. Changed by `.set nomacro' and
210 `.set macro'. */
211 int warn_about_macros;
212 /* Non-zero if we should not move instructions. Changed by `.set
213 move', `.set volatile', `.set nomove', and `.set novolatile'. */
214 int nomove;
215 /* Non-zero if we should not optimize branches by moving the target
216 of the branch into the delay slot. Actually, we don't perform
217 this optimization anyhow. Changed by `.set bopt' and `.set
218 nobopt'. */
219 int nobopt;
220 /* Non-zero if we should not autoextend mips16 instructions.
221 Changed by `.set autoextend' and `.set noautoextend'. */
222 int noautoextend;
223 /* Restrict general purpose registers and floating point registers
224 to 32 bit. This is initially determined when -mgp32 or -mfp32
225 is passed but can changed if the assembler code uses .set mipsN. */
226 int gp32;
227 int fp32;
228 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
229 command line option, and the default CPU. */
230 int arch;
231 /* True if ".set sym32" is in effect. */
232 bfd_boolean sym32;
235 /* True if -mgp32 was passed. */
236 static int file_mips_gp32 = -1;
238 /* True if -mfp32 was passed. */
239 static int file_mips_fp32 = -1;
241 /* This is the struct we use to hold the current set of options. Note
242 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
243 -1 to indicate that they have not been initialized. */
245 static struct mips_set_options mips_opts =
247 ISA_UNKNOWN, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN, FALSE
250 /* These variables are filled in with the masks of registers used.
251 The object format code reads them and puts them in the appropriate
252 place. */
253 unsigned long mips_gprmask;
254 unsigned long mips_cprmask[4];
256 /* MIPS ISA we are using for this output file. */
257 static int file_mips_isa = ISA_UNKNOWN;
259 /* True if -mips16 was passed or implied by arguments passed on the
260 command line (e.g., by -march). */
261 static int file_ase_mips16;
263 /* True if -mips3d was passed or implied by arguments passed on the
264 command line (e.g., by -march). */
265 static int file_ase_mips3d;
267 /* True if -mdmx was passed or implied by arguments passed on the
268 command line (e.g., by -march). */
269 static int file_ase_mdmx;
271 /* True if -mdsp was passed or implied by arguments passed on the
272 command line (e.g., by -march). */
273 static int file_ase_dsp;
275 /* The argument of the -march= flag. The architecture we are assembling. */
276 static int file_mips_arch = CPU_UNKNOWN;
277 static const char *mips_arch_string;
279 /* The argument of the -mtune= flag. The architecture for which we
280 are optimizing. */
281 static int mips_tune = CPU_UNKNOWN;
282 static const char *mips_tune_string;
284 /* True when generating 32-bit code for a 64-bit processor. */
285 static int mips_32bitmode = 0;
287 /* True if the given ABI requires 32-bit registers. */
288 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
290 /* Likewise 64-bit registers. */
291 #define ABI_NEEDS_64BIT_REGS(ABI) \
292 ((ABI) == N32_ABI \
293 || (ABI) == N64_ABI \
294 || (ABI) == O64_ABI)
296 /* Return true if ISA supports 64 bit gp register instructions. */
297 #define ISA_HAS_64BIT_REGS(ISA) ( \
298 (ISA) == ISA_MIPS3 \
299 || (ISA) == ISA_MIPS4 \
300 || (ISA) == ISA_MIPS5 \
301 || (ISA) == ISA_MIPS64 \
302 || (ISA) == ISA_MIPS64R2 \
305 /* Return true if ISA supports 64-bit right rotate (dror et al.)
306 instructions. */
307 #define ISA_HAS_DROR(ISA) ( \
308 (ISA) == ISA_MIPS64R2 \
311 /* Return true if ISA supports 32-bit right rotate (ror et al.)
312 instructions. */
313 #define ISA_HAS_ROR(ISA) ( \
314 (ISA) == ISA_MIPS32R2 \
315 || (ISA) == ISA_MIPS64R2 \
318 #define HAVE_32BIT_GPRS \
319 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
321 #define HAVE_32BIT_FPRS \
322 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
324 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
325 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
327 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
329 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
331 /* True if relocations are stored in-place. */
332 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
334 /* The ABI-derived address size. */
335 #define HAVE_64BIT_ADDRESSES \
336 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
337 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
339 /* The size of symbolic constants (i.e., expressions of the form
340 "SYMBOL" or "SYMBOL + OFFSET"). */
341 #define HAVE_32BIT_SYMBOLS \
342 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
343 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
345 /* Addresses are loaded in different ways, depending on the address size
346 in use. The n32 ABI Documentation also mandates the use of additions
347 with overflow checking, but existing implementations don't follow it. */
348 #define ADDRESS_ADD_INSN \
349 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
351 #define ADDRESS_ADDI_INSN \
352 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
354 #define ADDRESS_LOAD_INSN \
355 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
357 #define ADDRESS_STORE_INSN \
358 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
360 /* Return true if the given CPU supports the MIPS16 ASE. */
361 #define CPU_HAS_MIPS16(cpu) \
362 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
363 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
365 /* Return true if the given CPU supports the MIPS3D ASE. */
366 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
369 /* Return true if the given CPU supports the MDMX ASE. */
370 #define CPU_HAS_MDMX(cpu) (FALSE \
373 /* Return true if the given CPU supports the DSP ASE. */
374 #define CPU_HAS_DSP(cpu) (FALSE \
377 /* True if CPU has a dror instruction. */
378 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
380 /* True if CPU has a ror instruction. */
381 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
383 /* True if mflo and mfhi can be immediately followed by instructions
384 which write to the HI and LO registers.
386 According to MIPS specifications, MIPS ISAs I, II, and III need
387 (at least) two instructions between the reads of HI/LO and
388 instructions which write them, and later ISAs do not. Contradicting
389 the MIPS specifications, some MIPS IV processor user manuals (e.g.
390 the UM for the NEC Vr5000) document needing the instructions between
391 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
392 MIPS64 and later ISAs to have the interlocks, plus any specific
393 earlier-ISA CPUs for which CPU documentation declares that the
394 instructions are really interlocked. */
395 #define hilo_interlocks \
396 (mips_opts.isa == ISA_MIPS32 \
397 || mips_opts.isa == ISA_MIPS32R2 \
398 || mips_opts.isa == ISA_MIPS64 \
399 || mips_opts.isa == ISA_MIPS64R2 \
400 || mips_opts.arch == CPU_R4010 \
401 || mips_opts.arch == CPU_R10000 \
402 || mips_opts.arch == CPU_R12000 \
403 || mips_opts.arch == CPU_RM7000 \
404 || mips_opts.arch == CPU_VR5500 \
407 /* Whether the processor uses hardware interlocks to protect reads
408 from the GPRs after they are loaded from memory, and thus does not
409 require nops to be inserted. This applies to instructions marked
410 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
411 level I. */
412 #define gpr_interlocks \
413 (mips_opts.isa != ISA_MIPS1 \
414 || mips_opts.arch == CPU_R3900)
416 /* Whether the processor uses hardware interlocks to avoid delays
417 required by coprocessor instructions, and thus does not require
418 nops to be inserted. This applies to instructions marked
419 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
420 between instructions marked INSN_WRITE_COND_CODE and ones marked
421 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
422 levels I, II, and III. */
423 /* Itbl support may require additional care here. */
424 #define cop_interlocks \
425 ((mips_opts.isa != ISA_MIPS1 \
426 && mips_opts.isa != ISA_MIPS2 \
427 && mips_opts.isa != ISA_MIPS3) \
428 || mips_opts.arch == CPU_R4300 \
431 /* Whether the processor uses hardware interlocks to protect reads
432 from coprocessor registers after they are loaded from memory, and
433 thus does not require nops to be inserted. This applies to
434 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
435 requires at MIPS ISA level I. */
436 #define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
438 /* Is this a mfhi or mflo instruction? */
439 #define MF_HILO_INSN(PINFO) \
440 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
442 /* MIPS PIC level. */
444 enum mips_pic_level mips_pic;
446 /* 1 if we should generate 32 bit offsets from the $gp register in
447 SVR4_PIC mode. Currently has no meaning in other modes. */
448 static int mips_big_got = 0;
450 /* 1 if trap instructions should used for overflow rather than break
451 instructions. */
452 static int mips_trap = 0;
454 /* 1 if double width floating point constants should not be constructed
455 by assembling two single width halves into two single width floating
456 point registers which just happen to alias the double width destination
457 register. On some architectures this aliasing can be disabled by a bit
458 in the status register, and the setting of this bit cannot be determined
459 automatically at assemble time. */
460 static int mips_disable_float_construction;
462 /* Non-zero if any .set noreorder directives were used. */
464 static int mips_any_noreorder;
466 /* Non-zero if nops should be inserted when the register referenced in
467 an mfhi/mflo instruction is read in the next two instructions. */
468 static int mips_7000_hilo_fix;
470 /* The size of the small data section. */
471 static unsigned int g_switch_value = 8;
472 /* Whether the -G option was used. */
473 static int g_switch_seen = 0;
475 #define N_RMASK 0xc4
476 #define N_VFP 0xd4
478 /* If we can determine in advance that GP optimization won't be
479 possible, we can skip the relaxation stuff that tries to produce
480 GP-relative references. This makes delay slot optimization work
481 better.
483 This function can only provide a guess, but it seems to work for
484 gcc output. It needs to guess right for gcc, otherwise gcc
485 will put what it thinks is a GP-relative instruction in a branch
486 delay slot.
488 I don't know if a fix is needed for the SVR4_PIC mode. I've only
489 fixed it for the non-PIC mode. KR 95/04/07 */
490 static int nopic_need_relax (symbolS *, int);
492 /* handle of the OPCODE hash table */
493 static struct hash_control *op_hash = NULL;
495 /* The opcode hash table we use for the mips16. */
496 static struct hash_control *mips16_op_hash = NULL;
498 /* This array holds the chars that always start a comment. If the
499 pre-processor is disabled, these aren't very useful */
500 const char comment_chars[] = "#";
502 /* This array holds the chars that only start a comment at the beginning of
503 a line. If the line seems to have the form '# 123 filename'
504 .line and .file directives will appear in the pre-processed output */
505 /* Note that input_file.c hand checks for '#' at the beginning of the
506 first line of the input file. This is because the compiler outputs
507 #NO_APP at the beginning of its output. */
508 /* Also note that C style comments are always supported. */
509 const char line_comment_chars[] = "#";
511 /* This array holds machine specific line separator characters. */
512 const char line_separator_chars[] = ";";
514 /* Chars that can be used to separate mant from exp in floating point nums */
515 const char EXP_CHARS[] = "eE";
517 /* Chars that mean this number is a floating point constant */
518 /* As in 0f12.456 */
519 /* or 0d1.2345e12 */
520 const char FLT_CHARS[] = "rRsSfFdDxXpP";
522 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
523 changed in read.c . Ideally it shouldn't have to know about it at all,
524 but nothing is ideal around here.
527 static char *insn_error;
529 static int auto_align = 1;
531 /* When outputting SVR4 PIC code, the assembler needs to know the
532 offset in the stack frame from which to restore the $gp register.
533 This is set by the .cprestore pseudo-op, and saved in this
534 variable. */
535 static offsetT mips_cprestore_offset = -1;
537 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
538 more optimizations, it can use a register value instead of a memory-saved
539 offset and even an other register than $gp as global pointer. */
540 static offsetT mips_cpreturn_offset = -1;
541 static int mips_cpreturn_register = -1;
542 static int mips_gp_register = GP;
543 static int mips_gprel_offset = 0;
545 /* Whether mips_cprestore_offset has been set in the current function
546 (or whether it has already been warned about, if not). */
547 static int mips_cprestore_valid = 0;
549 /* This is the register which holds the stack frame, as set by the
550 .frame pseudo-op. This is needed to implement .cprestore. */
551 static int mips_frame_reg = SP;
553 /* Whether mips_frame_reg has been set in the current function
554 (or whether it has already been warned about, if not). */
555 static int mips_frame_reg_valid = 0;
557 /* To output NOP instructions correctly, we need to keep information
558 about the previous two instructions. */
560 /* Whether we are optimizing. The default value of 2 means to remove
561 unneeded NOPs and swap branch instructions when possible. A value
562 of 1 means to not swap branches. A value of 0 means to always
563 insert NOPs. */
564 static int mips_optimize = 2;
566 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
567 equivalent to seeing no -g option at all. */
568 static int mips_debug = 0;
570 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
571 #define MAX_VR4130_NOPS 4
573 /* The maximum number of NOPs needed to fill delay slots. */
574 #define MAX_DELAY_NOPS 2
576 /* The maximum number of NOPs needed for any purpose. */
577 #define MAX_NOPS 4
579 /* A list of previous instructions, with index 0 being the most recent.
580 We need to look back MAX_NOPS instructions when filling delay slots
581 or working around processor errata. We need to look back one
582 instruction further if we're thinking about using history[0] to
583 fill a branch delay slot. */
584 static struct mips_cl_insn history[1 + MAX_NOPS];
586 /* Nop instructions used by emit_nop. */
587 static struct mips_cl_insn nop_insn, mips16_nop_insn;
589 /* The appropriate nop for the current mode. */
590 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn : &nop_insn)
592 /* If this is set, it points to a frag holding nop instructions which
593 were inserted before the start of a noreorder section. If those
594 nops turn out to be unnecessary, the size of the frag can be
595 decreased. */
596 static fragS *prev_nop_frag;
598 /* The number of nop instructions we created in prev_nop_frag. */
599 static int prev_nop_frag_holds;
601 /* The number of nop instructions that we know we need in
602 prev_nop_frag. */
603 static int prev_nop_frag_required;
605 /* The number of instructions we've seen since prev_nop_frag. */
606 static int prev_nop_frag_since;
608 /* For ECOFF and ELF, relocations against symbols are done in two
609 parts, with a HI relocation and a LO relocation. Each relocation
610 has only 16 bits of space to store an addend. This means that in
611 order for the linker to handle carries correctly, it must be able
612 to locate both the HI and the LO relocation. This means that the
613 relocations must appear in order in the relocation table.
615 In order to implement this, we keep track of each unmatched HI
616 relocation. We then sort them so that they immediately precede the
617 corresponding LO relocation. */
619 struct mips_hi_fixup
621 /* Next HI fixup. */
622 struct mips_hi_fixup *next;
623 /* This fixup. */
624 fixS *fixp;
625 /* The section this fixup is in. */
626 segT seg;
629 /* The list of unmatched HI relocs. */
631 static struct mips_hi_fixup *mips_hi_fixup_list;
633 /* The frag containing the last explicit relocation operator.
634 Null if explicit relocations have not been used. */
636 static fragS *prev_reloc_op_frag;
638 /* Map normal MIPS register numbers to mips16 register numbers. */
640 #define X ILLEGAL_REG
641 static const int mips32_to_16_reg_map[] =
643 X, X, 2, 3, 4, 5, 6, 7,
644 X, X, X, X, X, X, X, X,
645 0, 1, X, X, X, X, X, X,
646 X, X, X, X, X, X, X, X
648 #undef X
650 /* Map mips16 register numbers to normal MIPS register numbers. */
652 static const unsigned int mips16_to_32_reg_map[] =
654 16, 17, 2, 3, 4, 5, 6, 7
657 /* Classifies the kind of instructions we're interested in when
658 implementing -mfix-vr4120. */
659 enum fix_vr4120_class {
660 FIX_VR4120_MACC,
661 FIX_VR4120_DMACC,
662 FIX_VR4120_MULT,
663 FIX_VR4120_DMULT,
664 FIX_VR4120_DIV,
665 FIX_VR4120_MTHILO,
666 NUM_FIX_VR4120_CLASSES
669 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
670 there must be at least one other instruction between an instruction
671 of type X and an instruction of type Y. */
672 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
674 /* True if -mfix-vr4120 is in force. */
675 static int mips_fix_vr4120;
677 /* ...likewise -mfix-vr4130. */
678 static int mips_fix_vr4130;
680 /* We don't relax branches by default, since this causes us to expand
681 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
682 fail to compute the offset before expanding the macro to the most
683 efficient expansion. */
685 static int mips_relax_branch;
687 /* The expansion of many macros depends on the type of symbol that
688 they refer to. For example, when generating position-dependent code,
689 a macro that refers to a symbol may have two different expansions,
690 one which uses GP-relative addresses and one which uses absolute
691 addresses. When generating SVR4-style PIC, a macro may have
692 different expansions for local and global symbols.
694 We handle these situations by generating both sequences and putting
695 them in variant frags. In position-dependent code, the first sequence
696 will be the GP-relative one and the second sequence will be the
697 absolute one. In SVR4 PIC, the first sequence will be for global
698 symbols and the second will be for local symbols.
700 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
701 SECOND are the lengths of the two sequences in bytes. These fields
702 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
703 the subtype has the following flags:
705 RELAX_USE_SECOND
706 Set if it has been decided that we should use the second
707 sequence instead of the first.
709 RELAX_SECOND_LONGER
710 Set in the first variant frag if the macro's second implementation
711 is longer than its first. This refers to the macro as a whole,
712 not an individual relaxation.
714 RELAX_NOMACRO
715 Set in the first variant frag if the macro appeared in a .set nomacro
716 block and if one alternative requires a warning but the other does not.
718 RELAX_DELAY_SLOT
719 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
720 delay slot.
722 The frag's "opcode" points to the first fixup for relaxable code.
724 Relaxable macros are generated using a sequence such as:
726 relax_start (SYMBOL);
727 ... generate first expansion ...
728 relax_switch ();
729 ... generate second expansion ...
730 relax_end ();
732 The code and fixups for the unwanted alternative are discarded
733 by md_convert_frag. */
734 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
736 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
737 #define RELAX_SECOND(X) ((X) & 0xff)
738 #define RELAX_USE_SECOND 0x10000
739 #define RELAX_SECOND_LONGER 0x20000
740 #define RELAX_NOMACRO 0x40000
741 #define RELAX_DELAY_SLOT 0x80000
743 /* Branch without likely bit. If label is out of range, we turn:
745 beq reg1, reg2, label
746 delay slot
748 into
750 bne reg1, reg2, 0f
752 j label
753 0: delay slot
755 with the following opcode replacements:
757 beq <-> bne
758 blez <-> bgtz
759 bltz <-> bgez
760 bc1f <-> bc1t
762 bltzal <-> bgezal (with jal label instead of j label)
764 Even though keeping the delay slot instruction in the delay slot of
765 the branch would be more efficient, it would be very tricky to do
766 correctly, because we'd have to introduce a variable frag *after*
767 the delay slot instruction, and expand that instead. Let's do it
768 the easy way for now, even if the branch-not-taken case now costs
769 one additional instruction. Out-of-range branches are not supposed
770 to be common, anyway.
772 Branch likely. If label is out of range, we turn:
774 beql reg1, reg2, label
775 delay slot (annulled if branch not taken)
777 into
779 beql reg1, reg2, 1f
781 beql $0, $0, 2f
783 1: j[al] label
784 delay slot (executed only if branch taken)
787 It would be possible to generate a shorter sequence by losing the
788 likely bit, generating something like:
790 bne reg1, reg2, 0f
792 j[al] label
793 delay slot (executed only if branch taken)
796 beql -> bne
797 bnel -> beq
798 blezl -> bgtz
799 bgtzl -> blez
800 bltzl -> bgez
801 bgezl -> bltz
802 bc1fl -> bc1t
803 bc1tl -> bc1f
805 bltzall -> bgezal (with jal label instead of j label)
806 bgezall -> bltzal (ditto)
809 but it's not clear that it would actually improve performance. */
810 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
811 ((relax_substateT) \
812 (0xc0000000 \
813 | ((toofar) ? 1 : 0) \
814 | ((link) ? 2 : 0) \
815 | ((likely) ? 4 : 0) \
816 | ((uncond) ? 8 : 0)))
817 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
818 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
819 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
820 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
821 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
823 /* For mips16 code, we use an entirely different form of relaxation.
824 mips16 supports two versions of most instructions which take
825 immediate values: a small one which takes some small value, and a
826 larger one which takes a 16 bit value. Since branches also follow
827 this pattern, relaxing these values is required.
829 We can assemble both mips16 and normal MIPS code in a single
830 object. Therefore, we need to support this type of relaxation at
831 the same time that we support the relaxation described above. We
832 use the high bit of the subtype field to distinguish these cases.
834 The information we store for this type of relaxation is the
835 argument code found in the opcode file for this relocation, whether
836 the user explicitly requested a small or extended form, and whether
837 the relocation is in a jump or jal delay slot. That tells us the
838 size of the value, and how it should be stored. We also store
839 whether the fragment is considered to be extended or not. We also
840 store whether this is known to be a branch to a different section,
841 whether we have tried to relax this frag yet, and whether we have
842 ever extended a PC relative fragment because of a shift count. */
843 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
844 (0x80000000 \
845 | ((type) & 0xff) \
846 | ((small) ? 0x100 : 0) \
847 | ((ext) ? 0x200 : 0) \
848 | ((dslot) ? 0x400 : 0) \
849 | ((jal_dslot) ? 0x800 : 0))
850 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
851 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
852 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
853 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
854 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
855 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
856 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
857 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
858 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
859 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
860 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
861 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
863 /* Is the given value a sign-extended 32-bit value? */
864 #define IS_SEXT_32BIT_NUM(x) \
865 (((x) &~ (offsetT) 0x7fffffff) == 0 \
866 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
868 /* Is the given value a sign-extended 16-bit value? */
869 #define IS_SEXT_16BIT_NUM(x) \
870 (((x) &~ (offsetT) 0x7fff) == 0 \
871 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
873 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
874 #define IS_ZEXT_32BIT_NUM(x) \
875 (((x) &~ (offsetT) 0xffffffff) == 0 \
876 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
878 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
879 VALUE << SHIFT. VALUE is evaluated exactly once. */
880 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
881 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
882 | (((VALUE) & (MASK)) << (SHIFT)))
884 /* Extract bits MASK << SHIFT from STRUCT and shift them right
885 SHIFT places. */
886 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
887 (((STRUCT) >> (SHIFT)) & (MASK))
889 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
890 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
892 include/opcode/mips.h specifies operand fields using the macros
893 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
894 with "MIPS16OP" instead of "OP". */
895 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
896 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
897 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
898 INSERT_BITS ((INSN).insn_opcode, VALUE, \
899 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
901 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
902 #define EXTRACT_OPERAND(FIELD, INSN) \
903 EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD)
904 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
905 EXTRACT_BITS ((INSN).insn_opcode, \
906 MIPS16OP_MASK_##FIELD, \
907 MIPS16OP_SH_##FIELD)
909 /* Global variables used when generating relaxable macros. See the
910 comment above RELAX_ENCODE for more details about how relaxation
911 is used. */
912 static struct {
913 /* 0 if we're not emitting a relaxable macro.
914 1 if we're emitting the first of the two relaxation alternatives.
915 2 if we're emitting the second alternative. */
916 int sequence;
918 /* The first relaxable fixup in the current frag. (In other words,
919 the first fixup that refers to relaxable code.) */
920 fixS *first_fixup;
922 /* sizes[0] says how many bytes of the first alternative are stored in
923 the current frag. Likewise sizes[1] for the second alternative. */
924 unsigned int sizes[2];
926 /* The symbol on which the choice of sequence depends. */
927 symbolS *symbol;
928 } mips_relax;
930 /* Global variables used to decide whether a macro needs a warning. */
931 static struct {
932 /* True if the macro is in a branch delay slot. */
933 bfd_boolean delay_slot_p;
935 /* For relaxable macros, sizes[0] is the length of the first alternative
936 in bytes and sizes[1] is the length of the second alternative.
937 For non-relaxable macros, both elements give the length of the
938 macro in bytes. */
939 unsigned int sizes[2];
941 /* The first variant frag for this macro. */
942 fragS *first_frag;
943 } mips_macro_warning;
945 /* Prototypes for static functions. */
947 #define internalError() \
948 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
950 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
952 static void append_insn
953 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
954 static void mips_no_prev_insn (void);
955 static void mips16_macro_build
956 (expressionS *, const char *, const char *, va_list);
957 static void load_register (int, expressionS *, int);
958 static void macro_start (void);
959 static void macro_end (void);
960 static void macro (struct mips_cl_insn * ip);
961 static void mips16_macro (struct mips_cl_insn * ip);
962 #ifdef LOSING_COMPILER
963 static void macro2 (struct mips_cl_insn * ip);
964 #endif
965 static void mips_ip (char *str, struct mips_cl_insn * ip);
966 static void mips16_ip (char *str, struct mips_cl_insn * ip);
967 static void mips16_immed
968 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
969 unsigned long *, bfd_boolean *, unsigned short *);
970 static size_t my_getSmallExpression
971 (expressionS *, bfd_reloc_code_real_type *, char *);
972 static void my_getExpression (expressionS *, char *);
973 static void s_align (int);
974 static void s_change_sec (int);
975 static void s_change_section (int);
976 static void s_cons (int);
977 static void s_float_cons (int);
978 static void s_mips_globl (int);
979 static void s_option (int);
980 static void s_mipsset (int);
981 static void s_abicalls (int);
982 static void s_cpload (int);
983 static void s_cpsetup (int);
984 static void s_cplocal (int);
985 static void s_cprestore (int);
986 static void s_cpreturn (int);
987 static void s_gpvalue (int);
988 static void s_gpword (int);
989 static void s_gpdword (int);
990 static void s_cpadd (int);
991 static void s_insn (int);
992 static void md_obj_begin (void);
993 static void md_obj_end (void);
994 static void s_mips_ent (int);
995 static void s_mips_end (int);
996 static void s_mips_frame (int);
997 static void s_mips_mask (int reg_type);
998 static void s_mips_stab (int);
999 static void s_mips_weakext (int);
1000 static void s_mips_file (int);
1001 static void s_mips_loc (int);
1002 static bfd_boolean pic_need_relax (symbolS *, asection *);
1003 static int relaxed_branch_length (fragS *, asection *, int);
1004 static int validate_mips_insn (const struct mips_opcode *);
1006 /* Table and functions used to map between CPU/ISA names, and
1007 ISA levels, and CPU numbers. */
1009 struct mips_cpu_info
1011 const char *name; /* CPU or ISA name. */
1012 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
1013 int isa; /* ISA level. */
1014 int cpu; /* CPU number (default CPU if ISA). */
1017 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1018 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1019 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1021 /* Pseudo-op table.
1023 The following pseudo-ops from the Kane and Heinrich MIPS book
1024 should be defined here, but are currently unsupported: .alias,
1025 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1027 The following pseudo-ops from the Kane and Heinrich MIPS book are
1028 specific to the type of debugging information being generated, and
1029 should be defined by the object format: .aent, .begin, .bend,
1030 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1031 .vreg.
1033 The following pseudo-ops from the Kane and Heinrich MIPS book are
1034 not MIPS CPU specific, but are also not specific to the object file
1035 format. This file is probably the best place to define them, but
1036 they are not currently supported: .asm0, .endr, .lab, .repeat,
1037 .struct. */
1039 static const pseudo_typeS mips_pseudo_table[] =
1041 /* MIPS specific pseudo-ops. */
1042 {"option", s_option, 0},
1043 {"set", s_mipsset, 0},
1044 {"rdata", s_change_sec, 'r'},
1045 {"sdata", s_change_sec, 's'},
1046 {"livereg", s_ignore, 0},
1047 {"abicalls", s_abicalls, 0},
1048 {"cpload", s_cpload, 0},
1049 {"cpsetup", s_cpsetup, 0},
1050 {"cplocal", s_cplocal, 0},
1051 {"cprestore", s_cprestore, 0},
1052 {"cpreturn", s_cpreturn, 0},
1053 {"gpvalue", s_gpvalue, 0},
1054 {"gpword", s_gpword, 0},
1055 {"gpdword", s_gpdword, 0},
1056 {"cpadd", s_cpadd, 0},
1057 {"insn", s_insn, 0},
1059 /* Relatively generic pseudo-ops that happen to be used on MIPS
1060 chips. */
1061 {"asciiz", stringer, 1},
1062 {"bss", s_change_sec, 'b'},
1063 {"err", s_err, 0},
1064 {"half", s_cons, 1},
1065 {"dword", s_cons, 3},
1066 {"weakext", s_mips_weakext, 0},
1068 /* These pseudo-ops are defined in read.c, but must be overridden
1069 here for one reason or another. */
1070 {"align", s_align, 0},
1071 {"byte", s_cons, 0},
1072 {"data", s_change_sec, 'd'},
1073 {"double", s_float_cons, 'd'},
1074 {"float", s_float_cons, 'f'},
1075 {"globl", s_mips_globl, 0},
1076 {"global", s_mips_globl, 0},
1077 {"hword", s_cons, 1},
1078 {"int", s_cons, 2},
1079 {"long", s_cons, 2},
1080 {"octa", s_cons, 4},
1081 {"quad", s_cons, 3},
1082 {"section", s_change_section, 0},
1083 {"short", s_cons, 1},
1084 {"single", s_float_cons, 'f'},
1085 {"stabn", s_mips_stab, 'n'},
1086 {"text", s_change_sec, 't'},
1087 {"word", s_cons, 2},
1089 { "extern", ecoff_directive_extern, 0},
1091 { NULL, NULL, 0 },
1094 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1096 /* These pseudo-ops should be defined by the object file format.
1097 However, a.out doesn't support them, so we have versions here. */
1098 {"aent", s_mips_ent, 1},
1099 {"bgnb", s_ignore, 0},
1100 {"end", s_mips_end, 0},
1101 {"endb", s_ignore, 0},
1102 {"ent", s_mips_ent, 0},
1103 {"file", s_mips_file, 0},
1104 {"fmask", s_mips_mask, 'F'},
1105 {"frame", s_mips_frame, 0},
1106 {"loc", s_mips_loc, 0},
1107 {"mask", s_mips_mask, 'R'},
1108 {"verstamp", s_ignore, 0},
1109 { NULL, NULL, 0 },
1112 extern void pop_insert (const pseudo_typeS *);
1114 void
1115 mips_pop_insert (void)
1117 pop_insert (mips_pseudo_table);
1118 if (! ECOFF_DEBUGGING)
1119 pop_insert (mips_nonecoff_pseudo_table);
1122 /* Symbols labelling the current insn. */
1124 struct insn_label_list
1126 struct insn_label_list *next;
1127 symbolS *label;
1130 static struct insn_label_list *insn_labels;
1131 static struct insn_label_list *free_insn_labels;
1133 static void mips_clear_insn_labels (void);
1135 static inline void
1136 mips_clear_insn_labels (void)
1138 register struct insn_label_list **pl;
1140 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1142 *pl = insn_labels;
1143 insn_labels = NULL;
1146 static char *expr_end;
1148 /* Expressions which appear in instructions. These are set by
1149 mips_ip. */
1151 static expressionS imm_expr;
1152 static expressionS imm2_expr;
1153 static expressionS offset_expr;
1155 /* Relocs associated with imm_expr and offset_expr. */
1157 static bfd_reloc_code_real_type imm_reloc[3]
1158 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1159 static bfd_reloc_code_real_type offset_reloc[3]
1160 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1162 /* These are set by mips16_ip if an explicit extension is used. */
1164 static bfd_boolean mips16_small, mips16_ext;
1166 #ifdef OBJ_ELF
1167 /* The pdr segment for per procedure frame/regmask info. Not used for
1168 ECOFF debugging. */
1170 static segT pdr_seg;
1171 #endif
1173 /* The default target format to use. */
1175 const char *
1176 mips_target_format (void)
1178 switch (OUTPUT_FLAVOR)
1180 case bfd_target_ecoff_flavour:
1181 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1182 case bfd_target_coff_flavour:
1183 return "pe-mips";
1184 case bfd_target_elf_flavour:
1185 #ifdef TE_TMIPS
1186 /* This is traditional mips. */
1187 return (target_big_endian
1188 ? (HAVE_64BIT_OBJECTS
1189 ? "elf64-tradbigmips"
1190 : (HAVE_NEWABI
1191 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1192 : (HAVE_64BIT_OBJECTS
1193 ? "elf64-tradlittlemips"
1194 : (HAVE_NEWABI
1195 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1196 #else
1197 return (target_big_endian
1198 ? (HAVE_64BIT_OBJECTS
1199 ? "elf64-bigmips"
1200 : (HAVE_NEWABI
1201 ? "elf32-nbigmips" : "elf32-bigmips"))
1202 : (HAVE_64BIT_OBJECTS
1203 ? "elf64-littlemips"
1204 : (HAVE_NEWABI
1205 ? "elf32-nlittlemips" : "elf32-littlemips")));
1206 #endif
1207 default:
1208 abort ();
1209 return NULL;
1213 /* Return the length of instruction INSN. */
1215 static inline unsigned int
1216 insn_length (const struct mips_cl_insn *insn)
1218 if (!mips_opts.mips16)
1219 return 4;
1220 return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1223 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1225 static void
1226 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1228 size_t i;
1230 insn->insn_mo = mo;
1231 insn->use_extend = FALSE;
1232 insn->extend = 0;
1233 insn->insn_opcode = mo->match;
1234 insn->frag = NULL;
1235 insn->where = 0;
1236 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1237 insn->fixp[i] = NULL;
1238 insn->fixed_p = (mips_opts.noreorder > 0);
1239 insn->noreorder_p = (mips_opts.noreorder > 0);
1240 insn->mips16_absolute_jump_p = 0;
1243 /* Install INSN at the location specified by its "frag" and "where" fields. */
1245 static void
1246 install_insn (const struct mips_cl_insn *insn)
1248 char *f = insn->frag->fr_literal + insn->where;
1249 if (!mips_opts.mips16)
1250 md_number_to_chars (f, insn->insn_opcode, 4);
1251 else if (insn->mips16_absolute_jump_p)
1253 md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1254 md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1256 else
1258 if (insn->use_extend)
1260 md_number_to_chars (f, 0xf000 | insn->extend, 2);
1261 f += 2;
1263 md_number_to_chars (f, insn->insn_opcode, 2);
1267 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1268 and install the opcode in the new location. */
1270 static void
1271 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1273 size_t i;
1275 insn->frag = frag;
1276 insn->where = where;
1277 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1278 if (insn->fixp[i] != NULL)
1280 insn->fixp[i]->fx_frag = frag;
1281 insn->fixp[i]->fx_where = where;
1283 install_insn (insn);
1286 /* Add INSN to the end of the output. */
1288 static void
1289 add_fixed_insn (struct mips_cl_insn *insn)
1291 char *f = frag_more (insn_length (insn));
1292 move_insn (insn, frag_now, f - frag_now->fr_literal);
1295 /* Start a variant frag and move INSN to the start of the variant part,
1296 marking it as fixed. The other arguments are as for frag_var. */
1298 static void
1299 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1300 relax_substateT subtype, symbolS *symbol, offsetT offset)
1302 frag_grow (max_chars);
1303 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1304 insn->fixed_p = 1;
1305 frag_var (rs_machine_dependent, max_chars, var,
1306 subtype, symbol, offset, NULL);
1309 /* Insert N copies of INSN into the history buffer, starting at
1310 position FIRST. Neither FIRST nor N need to be clipped. */
1312 static void
1313 insert_into_history (unsigned int first, unsigned int n,
1314 const struct mips_cl_insn *insn)
1316 if (mips_relax.sequence != 2)
1318 unsigned int i;
1320 for (i = ARRAY_SIZE (history); i-- > first;)
1321 if (i >= first + n)
1322 history[i] = history[i - n];
1323 else
1324 history[i] = *insn;
1328 /* Emit a nop instruction, recording it in the history buffer. */
1330 static void
1331 emit_nop (void)
1333 add_fixed_insn (NOP_INSN);
1334 insert_into_history (0, 1, NOP_INSN);
1337 /* Initialize vr4120_conflicts. There is a bit of duplication here:
1338 the idea is to make it obvious at a glance that each errata is
1339 included. */
1341 static void
1342 init_vr4120_conflicts (void)
1344 #define CONFLICT(FIRST, SECOND) \
1345 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1347 /* Errata 21 - [D]DIV[U] after [D]MACC */
1348 CONFLICT (MACC, DIV);
1349 CONFLICT (DMACC, DIV);
1351 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1352 CONFLICT (DMULT, DMULT);
1353 CONFLICT (DMULT, DMACC);
1354 CONFLICT (DMACC, DMULT);
1355 CONFLICT (DMACC, DMACC);
1357 /* Errata 24 - MT{LO,HI} after [D]MACC */
1358 CONFLICT (MACC, MTHILO);
1359 CONFLICT (DMACC, MTHILO);
1361 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1362 instruction is executed immediately after a MACC or DMACC
1363 instruction, the result of [either instruction] is incorrect." */
1364 CONFLICT (MACC, MULT);
1365 CONFLICT (MACC, DMULT);
1366 CONFLICT (DMACC, MULT);
1367 CONFLICT (DMACC, DMULT);
1369 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1370 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1371 DDIV or DDIVU instruction, the result of the MACC or
1372 DMACC instruction is incorrect.". */
1373 CONFLICT (DMULT, MACC);
1374 CONFLICT (DMULT, DMACC);
1375 CONFLICT (DIV, MACC);
1376 CONFLICT (DIV, DMACC);
1378 #undef CONFLICT
1381 /* This function is called once, at assembler startup time. It should
1382 set up all the tables, etc. that the MD part of the assembler will need. */
1384 void
1385 md_begin (void)
1387 register const char *retval = NULL;
1388 int i = 0;
1389 int broken = 0;
1391 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1392 as_warn (_("Could not set architecture and machine"));
1394 op_hash = hash_new ();
1396 for (i = 0; i < NUMOPCODES;)
1398 const char *name = mips_opcodes[i].name;
1400 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1401 if (retval != NULL)
1403 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1404 mips_opcodes[i].name, retval);
1405 /* Probably a memory allocation problem? Give up now. */
1406 as_fatal (_("Broken assembler. No assembly attempted."));
1410 if (mips_opcodes[i].pinfo != INSN_MACRO)
1412 if (!validate_mips_insn (&mips_opcodes[i]))
1413 broken = 1;
1414 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1416 create_insn (&nop_insn, mips_opcodes + i);
1417 nop_insn.fixed_p = 1;
1420 ++i;
1422 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1425 mips16_op_hash = hash_new ();
1427 i = 0;
1428 while (i < bfd_mips16_num_opcodes)
1430 const char *name = mips16_opcodes[i].name;
1432 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1433 if (retval != NULL)
1434 as_fatal (_("internal: can't hash `%s': %s"),
1435 mips16_opcodes[i].name, retval);
1438 if (mips16_opcodes[i].pinfo != INSN_MACRO
1439 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1440 != mips16_opcodes[i].match))
1442 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1443 mips16_opcodes[i].name, mips16_opcodes[i].args);
1444 broken = 1;
1446 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1448 create_insn (&mips16_nop_insn, mips16_opcodes + i);
1449 mips16_nop_insn.fixed_p = 1;
1451 ++i;
1453 while (i < bfd_mips16_num_opcodes
1454 && strcmp (mips16_opcodes[i].name, name) == 0);
1457 if (broken)
1458 as_fatal (_("Broken assembler. No assembly attempted."));
1460 /* We add all the general register names to the symbol table. This
1461 helps us detect invalid uses of them. */
1462 for (i = 0; i < 32; i++)
1464 char buf[5];
1466 sprintf (buf, "$%d", i);
1467 symbol_table_insert (symbol_new (buf, reg_section, i,
1468 &zero_address_frag));
1470 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1471 &zero_address_frag));
1472 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1473 &zero_address_frag));
1474 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1475 &zero_address_frag));
1476 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1477 &zero_address_frag));
1478 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1479 &zero_address_frag));
1480 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1481 &zero_address_frag));
1482 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1483 &zero_address_frag));
1484 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1485 &zero_address_frag));
1486 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1487 &zero_address_frag));
1489 /* If we don't add these register names to the symbol table, they
1490 may end up being added as regular symbols by operand(), and then
1491 make it to the object file as undefined in case they're not
1492 regarded as local symbols. They're local in o32, since `$' is a
1493 local symbol prefix, but not in n32 or n64. */
1494 for (i = 0; i < 8; i++)
1496 char buf[6];
1498 sprintf (buf, "$fcc%i", i);
1499 symbol_table_insert (symbol_new (buf, reg_section, -1,
1500 &zero_address_frag));
1503 mips_no_prev_insn ();
1505 mips_gprmask = 0;
1506 mips_cprmask[0] = 0;
1507 mips_cprmask[1] = 0;
1508 mips_cprmask[2] = 0;
1509 mips_cprmask[3] = 0;
1511 /* set the default alignment for the text section (2**2) */
1512 record_alignment (text_section, 2);
1514 bfd_set_gp_size (stdoutput, g_switch_value);
1516 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1518 /* On a native system, sections must be aligned to 16 byte
1519 boundaries. When configured for an embedded ELF target, we
1520 don't bother. */
1521 if (strcmp (TARGET_OS, "elf") != 0)
1523 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1524 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1525 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1528 /* Create a .reginfo section for register masks and a .mdebug
1529 section for debugging information. */
1531 segT seg;
1532 subsegT subseg;
1533 flagword flags;
1534 segT sec;
1536 seg = now_seg;
1537 subseg = now_subseg;
1539 /* The ABI says this section should be loaded so that the
1540 running program can access it. However, we don't load it
1541 if we are configured for an embedded target */
1542 flags = SEC_READONLY | SEC_DATA;
1543 if (strcmp (TARGET_OS, "elf") != 0)
1544 flags |= SEC_ALLOC | SEC_LOAD;
1546 if (mips_abi != N64_ABI)
1548 sec = subseg_new (".reginfo", (subsegT) 0);
1550 bfd_set_section_flags (stdoutput, sec, flags);
1551 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1553 #ifdef OBJ_ELF
1554 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1555 #endif
1557 else
1559 /* The 64-bit ABI uses a .MIPS.options section rather than
1560 .reginfo section. */
1561 sec = subseg_new (".MIPS.options", (subsegT) 0);
1562 bfd_set_section_flags (stdoutput, sec, flags);
1563 bfd_set_section_alignment (stdoutput, sec, 3);
1565 #ifdef OBJ_ELF
1566 /* Set up the option header. */
1568 Elf_Internal_Options opthdr;
1569 char *f;
1571 opthdr.kind = ODK_REGINFO;
1572 opthdr.size = (sizeof (Elf_External_Options)
1573 + sizeof (Elf64_External_RegInfo));
1574 opthdr.section = 0;
1575 opthdr.info = 0;
1576 f = frag_more (sizeof (Elf_External_Options));
1577 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1578 (Elf_External_Options *) f);
1580 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1582 #endif
1585 if (ECOFF_DEBUGGING)
1587 sec = subseg_new (".mdebug", (subsegT) 0);
1588 (void) bfd_set_section_flags (stdoutput, sec,
1589 SEC_HAS_CONTENTS | SEC_READONLY);
1590 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1592 #ifdef OBJ_ELF
1593 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1595 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1596 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1597 SEC_READONLY | SEC_RELOC
1598 | SEC_DEBUGGING);
1599 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1601 #endif
1603 subseg_set (seg, subseg);
1607 if (! ECOFF_DEBUGGING)
1608 md_obj_begin ();
1610 if (mips_fix_vr4120)
1611 init_vr4120_conflicts ();
1614 void
1615 md_mips_end (void)
1617 if (! ECOFF_DEBUGGING)
1618 md_obj_end ();
1621 void
1622 md_assemble (char *str)
1624 struct mips_cl_insn insn;
1625 bfd_reloc_code_real_type unused_reloc[3]
1626 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1628 imm_expr.X_op = O_absent;
1629 imm2_expr.X_op = O_absent;
1630 offset_expr.X_op = O_absent;
1631 imm_reloc[0] = BFD_RELOC_UNUSED;
1632 imm_reloc[1] = BFD_RELOC_UNUSED;
1633 imm_reloc[2] = BFD_RELOC_UNUSED;
1634 offset_reloc[0] = BFD_RELOC_UNUSED;
1635 offset_reloc[1] = BFD_RELOC_UNUSED;
1636 offset_reloc[2] = BFD_RELOC_UNUSED;
1638 if (mips_opts.mips16)
1639 mips16_ip (str, &insn);
1640 else
1642 mips_ip (str, &insn);
1643 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1644 str, insn.insn_opcode));
1647 if (insn_error)
1649 as_bad ("%s `%s'", insn_error, str);
1650 return;
1653 if (insn.insn_mo->pinfo == INSN_MACRO)
1655 macro_start ();
1656 if (mips_opts.mips16)
1657 mips16_macro (&insn);
1658 else
1659 macro (&insn);
1660 macro_end ();
1662 else
1664 if (imm_expr.X_op != O_absent)
1665 append_insn (&insn, &imm_expr, imm_reloc);
1666 else if (offset_expr.X_op != O_absent)
1667 append_insn (&insn, &offset_expr, offset_reloc);
1668 else
1669 append_insn (&insn, NULL, unused_reloc);
1673 /* Return true if the given relocation might need a matching %lo().
1674 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1675 applied to local symbols. */
1677 static inline bfd_boolean
1678 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1680 return (HAVE_IN_PLACE_ADDENDS
1681 && (reloc == BFD_RELOC_HI16_S
1682 || reloc == BFD_RELOC_MIPS_GOT16
1683 || reloc == BFD_RELOC_MIPS16_HI16_S));
1686 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1687 relocation. */
1689 static inline bfd_boolean
1690 fixup_has_matching_lo_p (fixS *fixp)
1692 return (fixp->fx_next != NULL
1693 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1694 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
1695 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1696 && fixp->fx_offset == fixp->fx_next->fx_offset);
1699 /* See whether instruction IP reads register REG. CLASS is the type
1700 of register. */
1702 static int
1703 insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg,
1704 enum mips_regclass class)
1706 if (class == MIPS16_REG)
1708 assert (mips_opts.mips16);
1709 reg = mips16_to_32_reg_map[reg];
1710 class = MIPS_GR_REG;
1713 /* Don't report on general register ZERO, since it never changes. */
1714 if (class == MIPS_GR_REG && reg == ZERO)
1715 return 0;
1717 if (class == MIPS_FP_REG)
1719 assert (! mips_opts.mips16);
1720 /* If we are called with either $f0 or $f1, we must check $f0.
1721 This is not optimal, because it will introduce an unnecessary
1722 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1723 need to distinguish reading both $f0 and $f1 or just one of
1724 them. Note that we don't have to check the other way,
1725 because there is no instruction that sets both $f0 and $f1
1726 and requires a delay. */
1727 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1728 && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1)
1729 == (reg &~ (unsigned) 1)))
1730 return 1;
1731 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1732 && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1)
1733 == (reg &~ (unsigned) 1)))
1734 return 1;
1736 else if (! mips_opts.mips16)
1738 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1739 && EXTRACT_OPERAND (RS, *ip) == reg)
1740 return 1;
1741 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1742 && EXTRACT_OPERAND (RT, *ip) == reg)
1743 return 1;
1745 else
1747 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1748 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg)
1749 return 1;
1750 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1751 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg)
1752 return 1;
1753 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1754 && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]
1755 == reg))
1756 return 1;
1757 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1758 return 1;
1759 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1760 return 1;
1761 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1762 return 1;
1763 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1764 && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg)
1765 return 1;
1768 return 0;
1771 /* This function returns true if modifying a register requires a
1772 delay. */
1774 static int
1775 reg_needs_delay (unsigned int reg)
1777 unsigned long prev_pinfo;
1779 prev_pinfo = history[0].insn_mo->pinfo;
1780 if (! mips_opts.noreorder
1781 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1782 && ! gpr_interlocks)
1783 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1784 && ! cop_interlocks)))
1786 /* A load from a coprocessor or from memory. All load delays
1787 delay the use of general register rt for one instruction. */
1788 /* Itbl support may require additional care here. */
1789 know (prev_pinfo & INSN_WRITE_GPR_T);
1790 if (reg == EXTRACT_OPERAND (RT, history[0]))
1791 return 1;
1794 return 0;
1797 /* Move all labels in insn_labels to the current insertion point. */
1799 static void
1800 mips_move_labels (void)
1802 struct insn_label_list *l;
1803 valueT val;
1805 for (l = insn_labels; l != NULL; l = l->next)
1807 assert (S_GET_SEGMENT (l->label) == now_seg);
1808 symbol_set_frag (l->label, frag_now);
1809 val = (valueT) frag_now_fix ();
1810 /* mips16 text labels are stored as odd. */
1811 if (mips_opts.mips16)
1812 ++val;
1813 S_SET_VALUE (l->label, val);
1817 /* Mark instruction labels in mips16 mode. This permits the linker to
1818 handle them specially, such as generating jalx instructions when
1819 needed. We also make them odd for the duration of the assembly, in
1820 order to generate the right sort of code. We will make them even
1821 in the adjust_symtab routine, while leaving them marked. This is
1822 convenient for the debugger and the disassembler. The linker knows
1823 to make them odd again. */
1825 static void
1826 mips16_mark_labels (void)
1828 if (mips_opts.mips16)
1830 struct insn_label_list *l;
1831 valueT val;
1833 for (l = insn_labels; l != NULL; l = l->next)
1835 #ifdef OBJ_ELF
1836 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1837 S_SET_OTHER (l->label, STO_MIPS16);
1838 #endif
1839 val = S_GET_VALUE (l->label);
1840 if ((val & 1) == 0)
1841 S_SET_VALUE (l->label, val + 1);
1846 /* End the current frag. Make it a variant frag and record the
1847 relaxation info. */
1849 static void
1850 relax_close_frag (void)
1852 mips_macro_warning.first_frag = frag_now;
1853 frag_var (rs_machine_dependent, 0, 0,
1854 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
1855 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1857 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1858 mips_relax.first_fixup = 0;
1861 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
1862 See the comment above RELAX_ENCODE for more details. */
1864 static void
1865 relax_start (symbolS *symbol)
1867 assert (mips_relax.sequence == 0);
1868 mips_relax.sequence = 1;
1869 mips_relax.symbol = symbol;
1872 /* Start generating the second version of a relaxable sequence.
1873 See the comment above RELAX_ENCODE for more details. */
1875 static void
1876 relax_switch (void)
1878 assert (mips_relax.sequence == 1);
1879 mips_relax.sequence = 2;
1882 /* End the current relaxable sequence. */
1884 static void
1885 relax_end (void)
1887 assert (mips_relax.sequence == 2);
1888 relax_close_frag ();
1889 mips_relax.sequence = 0;
1892 /* Classify an instruction according to the FIX_VR4120_* enumeration.
1893 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
1894 by VR4120 errata. */
1896 static unsigned int
1897 classify_vr4120_insn (const char *name)
1899 if (strncmp (name, "macc", 4) == 0)
1900 return FIX_VR4120_MACC;
1901 if (strncmp (name, "dmacc", 5) == 0)
1902 return FIX_VR4120_DMACC;
1903 if (strncmp (name, "mult", 4) == 0)
1904 return FIX_VR4120_MULT;
1905 if (strncmp (name, "dmult", 5) == 0)
1906 return FIX_VR4120_DMULT;
1907 if (strstr (name, "div"))
1908 return FIX_VR4120_DIV;
1909 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
1910 return FIX_VR4120_MTHILO;
1911 return NUM_FIX_VR4120_CLASSES;
1914 /* Return the number of instructions that must separate INSN1 and INSN2,
1915 where INSN1 is the earlier instruction. Return the worst-case value
1916 for any INSN2 if INSN2 is null. */
1918 static unsigned int
1919 insns_between (const struct mips_cl_insn *insn1,
1920 const struct mips_cl_insn *insn2)
1922 unsigned long pinfo1, pinfo2;
1924 /* This function needs to know which pinfo flags are set for INSN2
1925 and which registers INSN2 uses. The former is stored in PINFO2 and
1926 the latter is tested via INSN2_USES_REG. If INSN2 is null, PINFO2
1927 will have every flag set and INSN2_USES_REG will always return true. */
1928 pinfo1 = insn1->insn_mo->pinfo;
1929 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
1931 #define INSN2_USES_REG(REG, CLASS) \
1932 (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS))
1934 /* For most targets, write-after-read dependencies on the HI and LO
1935 registers must be separated by at least two instructions. */
1936 if (!hilo_interlocks)
1938 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
1939 return 2;
1940 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
1941 return 2;
1944 /* If we're working around r7000 errata, there must be two instructions
1945 between an mfhi or mflo and any instruction that uses the result. */
1946 if (mips_7000_hilo_fix
1947 && MF_HILO_INSN (pinfo1)
1948 && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG))
1949 return 2;
1951 /* If working around VR4120 errata, check for combinations that need
1952 a single intervening instruction. */
1953 if (mips_fix_vr4120)
1955 unsigned int class1, class2;
1957 class1 = classify_vr4120_insn (insn1->insn_mo->name);
1958 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
1960 if (insn2 == NULL)
1961 return 1;
1962 class2 = classify_vr4120_insn (insn2->insn_mo->name);
1963 if (vr4120_conflicts[class1] & (1 << class2))
1964 return 1;
1968 if (!mips_opts.mips16)
1970 /* Check for GPR or coprocessor load delays. All such delays
1971 are on the RT register. */
1972 /* Itbl support may require additional care here. */
1973 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
1974 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
1976 know (pinfo1 & INSN_WRITE_GPR_T);
1977 if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG))
1978 return 1;
1981 /* Check for generic coprocessor hazards.
1983 This case is not handled very well. There is no special
1984 knowledge of CP0 handling, and the coprocessors other than
1985 the floating point unit are not distinguished at all. */
1986 /* Itbl support may require additional care here. FIXME!
1987 Need to modify this to include knowledge about
1988 user specified delays! */
1989 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
1990 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
1992 /* Handle cases where INSN1 writes to a known general coprocessor
1993 register. There must be a one instruction delay before INSN2
1994 if INSN2 reads that register, otherwise no delay is needed. */
1995 if (pinfo1 & INSN_WRITE_FPR_T)
1997 if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG))
1998 return 1;
2000 else if (pinfo1 & INSN_WRITE_FPR_S)
2002 if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG))
2003 return 1;
2005 else
2007 /* Read-after-write dependencies on the control registers
2008 require a two-instruction gap. */
2009 if ((pinfo1 & INSN_WRITE_COND_CODE)
2010 && (pinfo2 & INSN_READ_COND_CODE))
2011 return 2;
2013 /* We don't know exactly what INSN1 does. If INSN2 is
2014 also a coprocessor instruction, assume there must be
2015 a one instruction gap. */
2016 if (pinfo2 & INSN_COP)
2017 return 1;
2021 /* Check for read-after-write dependencies on the coprocessor
2022 control registers in cases where INSN1 does not need a general
2023 coprocessor delay. This means that INSN1 is a floating point
2024 comparison instruction. */
2025 /* Itbl support may require additional care here. */
2026 else if (!cop_interlocks
2027 && (pinfo1 & INSN_WRITE_COND_CODE)
2028 && (pinfo2 & INSN_READ_COND_CODE))
2029 return 1;
2032 #undef INSN2_USES_REG
2034 return 0;
2037 /* Return the number of nops that would be needed to work around the
2038 VR4130 mflo/mfhi errata if instruction INSN immediately followed
2039 the MAX_VR4130_NOPS instructions described by HISTORY. */
2041 static int
2042 nops_for_vr4130 (const struct mips_cl_insn *history,
2043 const struct mips_cl_insn *insn)
2045 int i, j, reg;
2047 /* Check if the instruction writes to HI or LO. MTHI and MTLO
2048 are not affected by the errata. */
2049 if (insn != 0
2050 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
2051 || strcmp (insn->insn_mo->name, "mtlo") == 0
2052 || strcmp (insn->insn_mo->name, "mthi") == 0))
2053 return 0;
2055 /* Search for the first MFLO or MFHI. */
2056 for (i = 0; i < MAX_VR4130_NOPS; i++)
2057 if (!history[i].noreorder_p && MF_HILO_INSN (history[i].insn_mo->pinfo))
2059 /* Extract the destination register. */
2060 if (mips_opts.mips16)
2061 reg = mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, history[i])];
2062 else
2063 reg = EXTRACT_OPERAND (RD, history[i]);
2065 /* No nops are needed if INSN reads that register. */
2066 if (insn != NULL && insn_uses_reg (insn, reg, MIPS_GR_REG))
2067 return 0;
2069 /* ...or if any of the intervening instructions do. */
2070 for (j = 0; j < i; j++)
2071 if (insn_uses_reg (&history[j], reg, MIPS_GR_REG))
2072 return 0;
2074 return MAX_VR4130_NOPS - i;
2076 return 0;
2079 /* Return the number of nops that would be needed if instruction INSN
2080 immediately followed the MAX_NOPS instructions given by HISTORY,
2081 where HISTORY[0] is the most recent instruction. If INSN is null,
2082 return the worse-case number of nops for any instruction. */
2084 static int
2085 nops_for_insn (const struct mips_cl_insn *history,
2086 const struct mips_cl_insn *insn)
2088 int i, nops, tmp_nops;
2090 nops = 0;
2091 for (i = 0; i < MAX_DELAY_NOPS; i++)
2092 if (!history[i].noreorder_p)
2094 tmp_nops = insns_between (history + i, insn) - i;
2095 if (tmp_nops > nops)
2096 nops = tmp_nops;
2099 if (mips_fix_vr4130)
2101 tmp_nops = nops_for_vr4130 (history, insn);
2102 if (tmp_nops > nops)
2103 nops = tmp_nops;
2106 return nops;
2109 /* The variable arguments provide NUM_INSNS extra instructions that
2110 might be added to HISTORY. Return the largest number of nops that
2111 would be needed after the extended sequence. */
2113 static int
2114 nops_for_sequence (int num_insns, const struct mips_cl_insn *history, ...)
2116 va_list args;
2117 struct mips_cl_insn buffer[MAX_NOPS];
2118 struct mips_cl_insn *cursor;
2119 int nops;
2121 va_start (args, history);
2122 cursor = buffer + num_insns;
2123 memcpy (cursor, history, (MAX_NOPS - num_insns) * sizeof (*cursor));
2124 while (cursor > buffer)
2125 *--cursor = *va_arg (args, const struct mips_cl_insn *);
2127 nops = nops_for_insn (buffer, NULL);
2128 va_end (args);
2129 return nops;
2132 /* Like nops_for_insn, but if INSN is a branch, take into account the
2133 worst-case delay for the branch target. */
2135 static int
2136 nops_for_insn_or_target (const struct mips_cl_insn *history,
2137 const struct mips_cl_insn *insn)
2139 int nops, tmp_nops;
2141 nops = nops_for_insn (history, insn);
2142 if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2143 | INSN_COND_BRANCH_DELAY
2144 | INSN_COND_BRANCH_LIKELY))
2146 tmp_nops = nops_for_sequence (2, history, insn, NOP_INSN);
2147 if (tmp_nops > nops)
2148 nops = tmp_nops;
2150 else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH))
2152 tmp_nops = nops_for_sequence (1, history, insn);
2153 if (tmp_nops > nops)
2154 nops = tmp_nops;
2156 return nops;
2159 /* Output an instruction. IP is the instruction information.
2160 ADDRESS_EXPR is an operand of the instruction to be used with
2161 RELOC_TYPE. */
2163 static void
2164 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
2165 bfd_reloc_code_real_type *reloc_type)
2167 register unsigned long prev_pinfo, pinfo;
2168 relax_stateT prev_insn_frag_type = 0;
2169 bfd_boolean relaxed_branch = FALSE;
2171 /* Mark instruction labels in mips16 mode. */
2172 mips16_mark_labels ();
2174 prev_pinfo = history[0].insn_mo->pinfo;
2175 pinfo = ip->insn_mo->pinfo;
2177 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2179 /* There are a lot of optimizations we could do that we don't.
2180 In particular, we do not, in general, reorder instructions.
2181 If you use gcc with optimization, it will reorder
2182 instructions and generally do much more optimization then we
2183 do here; repeating all that work in the assembler would only
2184 benefit hand written assembly code, and does not seem worth
2185 it. */
2186 int nops = (mips_optimize == 0
2187 ? nops_for_insn (history, NULL)
2188 : nops_for_insn_or_target (history, ip));
2189 if (nops > 0)
2191 fragS *old_frag;
2192 unsigned long old_frag_offset;
2193 int i;
2195 old_frag = frag_now;
2196 old_frag_offset = frag_now_fix ();
2198 for (i = 0; i < nops; i++)
2199 emit_nop ();
2201 if (listing)
2203 listing_prev_line ();
2204 /* We may be at the start of a variant frag. In case we
2205 are, make sure there is enough space for the frag
2206 after the frags created by listing_prev_line. The
2207 argument to frag_grow here must be at least as large
2208 as the argument to all other calls to frag_grow in
2209 this file. We don't have to worry about being in the
2210 middle of a variant frag, because the variants insert
2211 all needed nop instructions themselves. */
2212 frag_grow (40);
2215 mips_move_labels ();
2217 #ifndef NO_ECOFF_DEBUGGING
2218 if (ECOFF_DEBUGGING)
2219 ecoff_fix_loc (old_frag, old_frag_offset);
2220 #endif
2223 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
2225 /* Work out how many nops in prev_nop_frag are needed by IP. */
2226 int nops = nops_for_insn_or_target (history, ip);
2227 assert (nops <= prev_nop_frag_holds);
2229 /* Enforce NOPS as a minimum. */
2230 if (nops > prev_nop_frag_required)
2231 prev_nop_frag_required = nops;
2233 if (prev_nop_frag_holds == prev_nop_frag_required)
2235 /* Settle for the current number of nops. Update the history
2236 accordingly (for the benefit of any future .set reorder code). */
2237 prev_nop_frag = NULL;
2238 insert_into_history (prev_nop_frag_since,
2239 prev_nop_frag_holds, NOP_INSN);
2241 else
2243 /* Allow this instruction to replace one of the nops that was
2244 tentatively added to prev_nop_frag. */
2245 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2246 prev_nop_frag_holds--;
2247 prev_nop_frag_since++;
2251 #ifdef OBJ_ELF
2252 /* The value passed to dwarf2_emit_insn is the distance between
2253 the beginning of the current instruction and the address that
2254 should be recorded in the debug tables. For MIPS16 debug info
2255 we want to use ISA-encoded addresses, so we pass -1 for an
2256 address higher by one than the current. */
2257 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2258 #endif
2260 /* Record the frag type before frag_var. */
2261 if (history[0].frag)
2262 prev_insn_frag_type = history[0].frag->fr_type;
2264 if (address_expr
2265 && *reloc_type == BFD_RELOC_16_PCREL_S2
2266 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2267 || pinfo & INSN_COND_BRANCH_LIKELY)
2268 && mips_relax_branch
2269 /* Don't try branch relaxation within .set nomacro, or within
2270 .set noat if we use $at for PIC computations. If it turns
2271 out that the branch was out-of-range, we'll get an error. */
2272 && !mips_opts.warn_about_macros
2273 && !(mips_opts.noat && mips_pic != NO_PIC)
2274 && !mips_opts.mips16)
2276 relaxed_branch = TRUE;
2277 add_relaxed_insn (ip, (relaxed_branch_length
2278 (NULL, NULL,
2279 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2280 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1
2281 : 0)), 4,
2282 RELAX_BRANCH_ENCODE
2283 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2284 pinfo & INSN_COND_BRANCH_LIKELY,
2285 pinfo & INSN_WRITE_GPR_31,
2287 address_expr->X_add_symbol,
2288 address_expr->X_add_number);
2289 *reloc_type = BFD_RELOC_UNUSED;
2291 else if (*reloc_type > BFD_RELOC_UNUSED)
2293 /* We need to set up a variant frag. */
2294 assert (mips_opts.mips16 && address_expr != NULL);
2295 add_relaxed_insn (ip, 4, 0,
2296 RELAX_MIPS16_ENCODE
2297 (*reloc_type - BFD_RELOC_UNUSED,
2298 mips16_small, mips16_ext,
2299 prev_pinfo & INSN_UNCOND_BRANCH_DELAY,
2300 history[0].mips16_absolute_jump_p),
2301 make_expr_symbol (address_expr), 0);
2303 else if (mips_opts.mips16
2304 && ! ip->use_extend
2305 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2307 /* Make sure there is enough room to swap this instruction with
2308 a following jump instruction. */
2309 frag_grow (6);
2310 add_fixed_insn (ip);
2312 else
2314 if (mips_opts.mips16
2315 && mips_opts.noreorder
2316 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2317 as_warn (_("extended instruction in delay slot"));
2319 if (mips_relax.sequence)
2321 /* If we've reached the end of this frag, turn it into a variant
2322 frag and record the information for the instructions we've
2323 written so far. */
2324 if (frag_room () < 4)
2325 relax_close_frag ();
2326 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2329 if (mips_relax.sequence != 2)
2330 mips_macro_warning.sizes[0] += 4;
2331 if (mips_relax.sequence != 1)
2332 mips_macro_warning.sizes[1] += 4;
2334 if (mips_opts.mips16)
2336 ip->fixed_p = 1;
2337 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2339 add_fixed_insn (ip);
2342 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
2344 if (address_expr->X_op == O_constant)
2346 unsigned int tmp;
2348 switch (*reloc_type)
2350 case BFD_RELOC_32:
2351 ip->insn_opcode |= address_expr->X_add_number;
2352 break;
2354 case BFD_RELOC_MIPS_HIGHEST:
2355 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2356 ip->insn_opcode |= tmp & 0xffff;
2357 break;
2359 case BFD_RELOC_MIPS_HIGHER:
2360 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2361 ip->insn_opcode |= tmp & 0xffff;
2362 break;
2364 case BFD_RELOC_HI16_S:
2365 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2366 ip->insn_opcode |= tmp & 0xffff;
2367 break;
2369 case BFD_RELOC_HI16:
2370 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2371 break;
2373 case BFD_RELOC_UNUSED:
2374 case BFD_RELOC_LO16:
2375 case BFD_RELOC_MIPS_GOT_DISP:
2376 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2377 break;
2379 case BFD_RELOC_MIPS_JMP:
2380 if ((address_expr->X_add_number & 3) != 0)
2381 as_bad (_("jump to misaligned address (0x%lx)"),
2382 (unsigned long) address_expr->X_add_number);
2383 if (address_expr->X_add_number & ~0xfffffff)
2384 as_bad (_("jump address range overflow (0x%lx)"),
2385 (unsigned long) address_expr->X_add_number);
2386 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2387 break;
2389 case BFD_RELOC_MIPS16_JMP:
2390 if ((address_expr->X_add_number & 3) != 0)
2391 as_bad (_("jump to misaligned address (0x%lx)"),
2392 (unsigned long) address_expr->X_add_number);
2393 if (address_expr->X_add_number & ~0xfffffff)
2394 as_bad (_("jump address range overflow (0x%lx)"),
2395 (unsigned long) address_expr->X_add_number);
2396 ip->insn_opcode |=
2397 (((address_expr->X_add_number & 0x7c0000) << 3)
2398 | ((address_expr->X_add_number & 0xf800000) >> 7)
2399 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2400 break;
2402 case BFD_RELOC_16_PCREL_S2:
2403 goto need_reloc;
2405 default:
2406 internalError ();
2409 else if (*reloc_type < BFD_RELOC_UNUSED)
2410 need_reloc:
2412 reloc_howto_type *howto;
2413 int i;
2415 /* In a compound relocation, it is the final (outermost)
2416 operator that determines the relocated field. */
2417 for (i = 1; i < 3; i++)
2418 if (reloc_type[i] == BFD_RELOC_UNUSED)
2419 break;
2421 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2422 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
2423 bfd_get_reloc_size (howto),
2424 address_expr,
2425 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2426 reloc_type[0]);
2428 /* These relocations can have an addend that won't fit in
2429 4 octets for 64bit assembly. */
2430 if (HAVE_64BIT_GPRS
2431 && ! howto->partial_inplace
2432 && (reloc_type[0] == BFD_RELOC_16
2433 || reloc_type[0] == BFD_RELOC_32
2434 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2435 || reloc_type[0] == BFD_RELOC_HI16_S
2436 || reloc_type[0] == BFD_RELOC_LO16
2437 || reloc_type[0] == BFD_RELOC_GPREL16
2438 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2439 || reloc_type[0] == BFD_RELOC_GPREL32
2440 || reloc_type[0] == BFD_RELOC_64
2441 || reloc_type[0] == BFD_RELOC_CTOR
2442 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2443 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2444 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2445 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2446 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2447 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2448 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2449 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2450 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
2451 ip->fixp[0]->fx_no_overflow = 1;
2453 if (mips_relax.sequence)
2455 if (mips_relax.first_fixup == 0)
2456 mips_relax.first_fixup = ip->fixp[0];
2458 else if (reloc_needs_lo_p (*reloc_type))
2460 struct mips_hi_fixup *hi_fixup;
2462 /* Reuse the last entry if it already has a matching %lo. */
2463 hi_fixup = mips_hi_fixup_list;
2464 if (hi_fixup == 0
2465 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2467 hi_fixup = ((struct mips_hi_fixup *)
2468 xmalloc (sizeof (struct mips_hi_fixup)));
2469 hi_fixup->next = mips_hi_fixup_list;
2470 mips_hi_fixup_list = hi_fixup;
2472 hi_fixup->fixp = ip->fixp[0];
2473 hi_fixup->seg = now_seg;
2476 /* Add fixups for the second and third relocations, if given.
2477 Note that the ABI allows the second relocation to be
2478 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2479 moment we only use RSS_UNDEF, but we could add support
2480 for the others if it ever becomes necessary. */
2481 for (i = 1; i < 3; i++)
2482 if (reloc_type[i] != BFD_RELOC_UNUSED)
2484 ip->fixp[i] = fix_new (ip->frag, ip->where,
2485 ip->fixp[0]->fx_size, NULL, 0,
2486 FALSE, reloc_type[i]);
2488 /* Use fx_tcbit to mark compound relocs. */
2489 ip->fixp[0]->fx_tcbit = 1;
2490 ip->fixp[i]->fx_tcbit = 1;
2494 install_insn (ip);
2496 /* Update the register mask information. */
2497 if (! mips_opts.mips16)
2499 if (pinfo & INSN_WRITE_GPR_D)
2500 mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
2501 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2502 mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
2503 if (pinfo & INSN_READ_GPR_S)
2504 mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
2505 if (pinfo & INSN_WRITE_GPR_31)
2506 mips_gprmask |= 1 << RA;
2507 if (pinfo & INSN_WRITE_FPR_D)
2508 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
2509 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2510 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
2511 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2512 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
2513 if ((pinfo & INSN_READ_FPR_R) != 0)
2514 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
2515 if (pinfo & INSN_COP)
2517 /* We don't keep enough information to sort these cases out.
2518 The itbl support does keep this information however, although
2519 we currently don't support itbl fprmats as part of the cop
2520 instruction. May want to add this support in the future. */
2522 /* Never set the bit for $0, which is always zero. */
2523 mips_gprmask &= ~1 << 0;
2525 else
2527 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2528 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
2529 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2530 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
2531 if (pinfo & MIPS16_INSN_WRITE_Z)
2532 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
2533 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2534 mips_gprmask |= 1 << TREG;
2535 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2536 mips_gprmask |= 1 << SP;
2537 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2538 mips_gprmask |= 1 << RA;
2539 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2540 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2541 if (pinfo & MIPS16_INSN_READ_Z)
2542 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
2543 if (pinfo & MIPS16_INSN_READ_GPR_X)
2544 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
2547 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2549 /* Filling the branch delay slot is more complex. We try to
2550 switch the branch with the previous instruction, which we can
2551 do if the previous instruction does not set up a condition
2552 that the branch tests and if the branch is not itself the
2553 target of any branch. */
2554 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2555 || (pinfo & INSN_COND_BRANCH_DELAY))
2557 if (mips_optimize < 2
2558 /* If we have seen .set volatile or .set nomove, don't
2559 optimize. */
2560 || mips_opts.nomove != 0
2561 /* We can't swap if the previous instruction's position
2562 is fixed. */
2563 || history[0].fixed_p
2564 /* If the previous previous insn was in a .set
2565 noreorder, we can't swap. Actually, the MIPS
2566 assembler will swap in this situation. However, gcc
2567 configured -with-gnu-as will generate code like
2568 .set noreorder
2569 lw $4,XXX
2570 .set reorder
2571 INSN
2572 bne $4,$0,foo
2573 in which we can not swap the bne and INSN. If gcc is
2574 not configured -with-gnu-as, it does not output the
2575 .set pseudo-ops. */
2576 || history[1].noreorder_p
2577 /* If the branch is itself the target of a branch, we
2578 can not swap. We cheat on this; all we check for is
2579 whether there is a label on this instruction. If
2580 there are any branches to anything other than a
2581 label, users must use .set noreorder. */
2582 || insn_labels != NULL
2583 /* If the previous instruction is in a variant frag
2584 other than this branch's one, we cannot do the swap.
2585 This does not apply to the mips16, which uses variant
2586 frags for different purposes. */
2587 || (! mips_opts.mips16
2588 && prev_insn_frag_type == rs_machine_dependent)
2589 /* Check for conflicts between the branch and the instructions
2590 before the candidate delay slot. */
2591 || nops_for_insn (history + 1, ip) > 0
2592 /* Check for conflicts between the swapped sequence and the
2593 target of the branch. */
2594 || nops_for_sequence (2, history + 1, ip, history) > 0
2595 /* We do not swap with a trap instruction, since it
2596 complicates trap handlers to have the trap
2597 instruction be in a delay slot. */
2598 || (prev_pinfo & INSN_TRAP)
2599 /* If the branch reads a register that the previous
2600 instruction sets, we can not swap. */
2601 || (! mips_opts.mips16
2602 && (prev_pinfo & INSN_WRITE_GPR_T)
2603 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
2604 MIPS_GR_REG))
2605 || (! mips_opts.mips16
2606 && (prev_pinfo & INSN_WRITE_GPR_D)
2607 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
2608 MIPS_GR_REG))
2609 || (mips_opts.mips16
2610 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2611 && (insn_uses_reg
2612 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
2613 MIPS16_REG)))
2614 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2615 && (insn_uses_reg
2616 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
2617 MIPS16_REG)))
2618 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2619 && (insn_uses_reg
2620 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
2621 MIPS16_REG)))
2622 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2623 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2624 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2625 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2626 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2627 && insn_uses_reg (ip,
2628 MIPS16OP_EXTRACT_REG32R
2629 (history[0].insn_opcode),
2630 MIPS_GR_REG))))
2631 /* If the branch writes a register that the previous
2632 instruction sets, we can not swap (we know that
2633 branches write only to RD or to $31). */
2634 || (! mips_opts.mips16
2635 && (prev_pinfo & INSN_WRITE_GPR_T)
2636 && (((pinfo & INSN_WRITE_GPR_D)
2637 && (EXTRACT_OPERAND (RT, history[0])
2638 == EXTRACT_OPERAND (RD, *ip)))
2639 || ((pinfo & INSN_WRITE_GPR_31)
2640 && EXTRACT_OPERAND (RT, history[0]) == RA)))
2641 || (! mips_opts.mips16
2642 && (prev_pinfo & INSN_WRITE_GPR_D)
2643 && (((pinfo & INSN_WRITE_GPR_D)
2644 && (EXTRACT_OPERAND (RD, history[0])
2645 == EXTRACT_OPERAND (RD, *ip)))
2646 || ((pinfo & INSN_WRITE_GPR_31)
2647 && EXTRACT_OPERAND (RD, history[0]) == RA)))
2648 || (mips_opts.mips16
2649 && (pinfo & MIPS16_INSN_WRITE_31)
2650 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2651 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2652 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
2653 == RA))))
2654 /* If the branch writes a register that the previous
2655 instruction reads, we can not swap (we know that
2656 branches only write to RD or to $31). */
2657 || (! mips_opts.mips16
2658 && (pinfo & INSN_WRITE_GPR_D)
2659 && insn_uses_reg (&history[0],
2660 EXTRACT_OPERAND (RD, *ip),
2661 MIPS_GR_REG))
2662 || (! mips_opts.mips16
2663 && (pinfo & INSN_WRITE_GPR_31)
2664 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2665 || (mips_opts.mips16
2666 && (pinfo & MIPS16_INSN_WRITE_31)
2667 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2668 /* If one instruction sets a condition code and the
2669 other one uses a condition code, we can not swap. */
2670 || ((pinfo & INSN_READ_COND_CODE)
2671 && (prev_pinfo & INSN_WRITE_COND_CODE))
2672 || ((pinfo & INSN_WRITE_COND_CODE)
2673 && (prev_pinfo & INSN_READ_COND_CODE))
2674 /* If the previous instruction uses the PC, we can not
2675 swap. */
2676 || (mips_opts.mips16
2677 && (prev_pinfo & MIPS16_INSN_READ_PC))
2678 /* If the previous instruction had a fixup in mips16
2679 mode, we can not swap. This normally means that the
2680 previous instruction was a 4 byte branch anyhow. */
2681 || (mips_opts.mips16 && history[0].fixp[0])
2682 /* If the previous instruction is a sync, sync.l, or
2683 sync.p, we can not swap. */
2684 || (prev_pinfo & INSN_SYNC))
2686 /* We could do even better for unconditional branches to
2687 portions of this object file; we could pick up the
2688 instruction at the destination, put it in the delay
2689 slot, and bump the destination address. */
2690 insert_into_history (0, 1, ip);
2691 emit_nop ();
2692 if (mips_relax.sequence)
2693 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2695 else
2697 /* It looks like we can actually do the swap. */
2698 struct mips_cl_insn delay = history[0];
2699 if (mips_opts.mips16)
2701 if (delay.frag == ip->frag)
2703 move_insn (ip, delay.frag, delay.where);
2704 move_insn (&delay, ip->frag, delay.where
2705 + insn_length (ip));
2707 else if (insn_length (ip) == insn_length (&delay))
2709 move_insn (&delay, ip->frag, ip->where);
2710 move_insn (ip, history[0].frag, history[0].where);
2712 else
2714 add_fixed_insn (NOP_INSN);
2715 delay = *NOP_INSN;
2718 else if (relaxed_branch)
2720 /* Add the delay slot instruction to the end of the
2721 current frag and shrink the fixed part of the
2722 original frag. If the branch occupies the tail of
2723 the latter, move it backwards to cover the gap. */
2724 delay.frag->fr_fix -= 4;
2725 if (delay.frag == ip->frag)
2726 move_insn (ip, ip->frag, ip->where - 4);
2727 add_fixed_insn (&delay);
2729 else
2731 move_insn (&delay, ip->frag, ip->where);
2732 move_insn (ip, history[0].frag, history[0].where);
2734 history[0] = *ip;
2735 delay.fixed_p = 1;
2736 insert_into_history (0, 1, &delay);
2739 /* If that was an unconditional branch, forget the previous
2740 insn information. */
2741 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2742 mips_no_prev_insn ();
2744 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2746 /* We don't yet optimize a branch likely. What we should do
2747 is look at the target, copy the instruction found there
2748 into the delay slot, and increment the branch to jump to
2749 the next instruction. */
2750 insert_into_history (0, 1, ip);
2751 emit_nop ();
2753 else
2754 insert_into_history (0, 1, ip);
2756 else
2757 insert_into_history (0, 1, ip);
2759 /* We just output an insn, so the next one doesn't have a label. */
2760 mips_clear_insn_labels ();
2763 /* Forget that there was any previous instruction or label. */
2765 static void
2766 mips_no_prev_insn (void)
2768 prev_nop_frag = NULL;
2769 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
2770 mips_clear_insn_labels ();
2773 /* This function must be called before we emit something other than
2774 instructions. It is like mips_no_prev_insn except that it inserts
2775 any NOPS that might be needed by previous instructions. */
2777 void
2778 mips_emit_delays (void)
2780 if (! mips_opts.noreorder)
2782 int nops = nops_for_insn (history, NULL);
2783 if (nops > 0)
2785 while (nops-- > 0)
2786 add_fixed_insn (NOP_INSN);
2787 mips_move_labels ();
2790 mips_no_prev_insn ();
2793 /* Start a (possibly nested) noreorder block. */
2795 static void
2796 start_noreorder (void)
2798 if (mips_opts.noreorder == 0)
2800 unsigned int i;
2801 int nops;
2803 /* None of the instructions before the .set noreorder can be moved. */
2804 for (i = 0; i < ARRAY_SIZE (history); i++)
2805 history[i].fixed_p = 1;
2807 /* Insert any nops that might be needed between the .set noreorder
2808 block and the previous instructions. We will later remove any
2809 nops that turn out not to be needed. */
2810 nops = nops_for_insn (history, NULL);
2811 if (nops > 0)
2813 if (mips_optimize != 0)
2815 /* Record the frag which holds the nop instructions, so
2816 that we can remove them if we don't need them. */
2817 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2818 prev_nop_frag = frag_now;
2819 prev_nop_frag_holds = nops;
2820 prev_nop_frag_required = 0;
2821 prev_nop_frag_since = 0;
2824 for (; nops > 0; --nops)
2825 add_fixed_insn (NOP_INSN);
2827 /* Move on to a new frag, so that it is safe to simply
2828 decrease the size of prev_nop_frag. */
2829 frag_wane (frag_now);
2830 frag_new (0);
2831 mips_move_labels ();
2833 mips16_mark_labels ();
2834 mips_clear_insn_labels ();
2836 mips_opts.noreorder++;
2837 mips_any_noreorder = 1;
2840 /* End a nested noreorder block. */
2842 static void
2843 end_noreorder (void)
2845 mips_opts.noreorder--;
2846 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
2848 /* Commit to inserting prev_nop_frag_required nops and go back to
2849 handling nop insertion the .set reorder way. */
2850 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
2851 * (mips_opts.mips16 ? 2 : 4));
2852 insert_into_history (prev_nop_frag_since,
2853 prev_nop_frag_required, NOP_INSN);
2854 prev_nop_frag = NULL;
2858 /* Set up global variables for the start of a new macro. */
2860 static void
2861 macro_start (void)
2863 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2864 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2865 && (history[0].insn_mo->pinfo
2866 & (INSN_UNCOND_BRANCH_DELAY
2867 | INSN_COND_BRANCH_DELAY
2868 | INSN_COND_BRANCH_LIKELY)) != 0);
2871 /* Given that a macro is longer than 4 bytes, return the appropriate warning
2872 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2873 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2875 static const char *
2876 macro_warning (relax_substateT subtype)
2878 if (subtype & RELAX_DELAY_SLOT)
2879 return _("Macro instruction expanded into multiple instructions"
2880 " in a branch delay slot");
2881 else if (subtype & RELAX_NOMACRO)
2882 return _("Macro instruction expanded into multiple instructions");
2883 else
2884 return 0;
2887 /* Finish up a macro. Emit warnings as appropriate. */
2889 static void
2890 macro_end (void)
2892 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2894 relax_substateT subtype;
2896 /* Set up the relaxation warning flags. */
2897 subtype = 0;
2898 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2899 subtype |= RELAX_SECOND_LONGER;
2900 if (mips_opts.warn_about_macros)
2901 subtype |= RELAX_NOMACRO;
2902 if (mips_macro_warning.delay_slot_p)
2903 subtype |= RELAX_DELAY_SLOT;
2905 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2907 /* Either the macro has a single implementation or both
2908 implementations are longer than 4 bytes. Emit the
2909 warning now. */
2910 const char *msg = macro_warning (subtype);
2911 if (msg != 0)
2912 as_warn (msg);
2914 else
2916 /* One implementation might need a warning but the other
2917 definitely doesn't. */
2918 mips_macro_warning.first_frag->fr_subtype |= subtype;
2923 /* Read a macro's relocation codes from *ARGS and store them in *R.
2924 The first argument in *ARGS will be either the code for a single
2925 relocation or -1 followed by the three codes that make up a
2926 composite relocation. */
2928 static void
2929 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
2931 int i, next;
2933 next = va_arg (*args, int);
2934 if (next >= 0)
2935 r[0] = (bfd_reloc_code_real_type) next;
2936 else
2937 for (i = 0; i < 3; i++)
2938 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
2941 /* Build an instruction created by a macro expansion. This is passed
2942 a pointer to the count of instructions created so far, an
2943 expression, the name of the instruction to build, an operand format
2944 string, and corresponding arguments. */
2946 static void
2947 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
2949 const struct mips_opcode *mo;
2950 struct mips_cl_insn insn;
2951 bfd_reloc_code_real_type r[3];
2952 va_list args;
2954 va_start (args, fmt);
2956 if (mips_opts.mips16)
2958 mips16_macro_build (ep, name, fmt, args);
2959 va_end (args);
2960 return;
2963 r[0] = BFD_RELOC_UNUSED;
2964 r[1] = BFD_RELOC_UNUSED;
2965 r[2] = BFD_RELOC_UNUSED;
2966 mo = (struct mips_opcode *) hash_find (op_hash, name);
2967 assert (mo);
2968 assert (strcmp (name, mo->name) == 0);
2970 /* Search until we get a match for NAME. It is assumed here that
2971 macros will never generate MDMX or MIPS-3D instructions. */
2972 while (strcmp (fmt, mo->args) != 0
2973 || mo->pinfo == INSN_MACRO
2974 || !OPCODE_IS_MEMBER (mo,
2975 (mips_opts.isa
2976 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2977 mips_opts.arch)
2978 || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0))
2980 ++mo;
2981 assert (mo->name);
2982 assert (strcmp (name, mo->name) == 0);
2985 create_insn (&insn, mo);
2986 for (;;)
2988 switch (*fmt++)
2990 case '\0':
2991 break;
2993 case ',':
2994 case '(':
2995 case ')':
2996 continue;
2998 case '+':
2999 switch (*fmt++)
3001 case 'A':
3002 case 'E':
3003 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
3004 continue;
3006 case 'B':
3007 case 'F':
3008 /* Note that in the macro case, these arguments are already
3009 in MSB form. (When handling the instruction in the
3010 non-macro case, these arguments are sizes from which
3011 MSB values must be calculated.) */
3012 INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
3013 continue;
3015 case 'C':
3016 case 'G':
3017 case 'H':
3018 /* Note that in the macro case, these arguments are already
3019 in MSBD form. (When handling the instruction in the
3020 non-macro case, these arguments are sizes from which
3021 MSBD values must be calculated.) */
3022 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
3023 continue;
3025 default:
3026 internalError ();
3028 continue;
3030 case 't':
3031 case 'w':
3032 case 'E':
3033 INSERT_OPERAND (RT, insn, va_arg (args, int));
3034 continue;
3036 case 'c':
3037 INSERT_OPERAND (CODE, insn, va_arg (args, int));
3038 continue;
3040 case 'T':
3041 case 'W':
3042 INSERT_OPERAND (FT, insn, va_arg (args, int));
3043 continue;
3045 case 'd':
3046 case 'G':
3047 case 'K':
3048 INSERT_OPERAND (RD, insn, va_arg (args, int));
3049 continue;
3051 case 'U':
3053 int tmp = va_arg (args, int);
3055 INSERT_OPERAND (RT, insn, tmp);
3056 INSERT_OPERAND (RD, insn, tmp);
3057 continue;
3060 case 'V':
3061 case 'S':
3062 INSERT_OPERAND (FS, insn, va_arg (args, int));
3063 continue;
3065 case 'z':
3066 continue;
3068 case '<':
3069 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
3070 continue;
3072 case 'D':
3073 INSERT_OPERAND (FD, insn, va_arg (args, int));
3074 continue;
3076 case 'B':
3077 INSERT_OPERAND (CODE20, insn, va_arg (args, int));
3078 continue;
3080 case 'J':
3081 INSERT_OPERAND (CODE19, insn, va_arg (args, int));
3082 continue;
3084 case 'q':
3085 INSERT_OPERAND (CODE2, insn, va_arg (args, int));
3086 continue;
3088 case 'b':
3089 case 's':
3090 case 'r':
3091 case 'v':
3092 INSERT_OPERAND (RS, insn, va_arg (args, int));
3093 continue;
3095 case 'i':
3096 case 'j':
3097 case 'o':
3098 macro_read_relocs (&args, r);
3099 assert (*r == BFD_RELOC_GPREL16
3100 || *r == BFD_RELOC_MIPS_LITERAL
3101 || *r == BFD_RELOC_MIPS_HIGHER
3102 || *r == BFD_RELOC_HI16_S
3103 || *r == BFD_RELOC_LO16
3104 || *r == BFD_RELOC_MIPS_GOT16
3105 || *r == BFD_RELOC_MIPS_CALL16
3106 || *r == BFD_RELOC_MIPS_GOT_DISP
3107 || *r == BFD_RELOC_MIPS_GOT_PAGE
3108 || *r == BFD_RELOC_MIPS_GOT_OFST
3109 || *r == BFD_RELOC_MIPS_GOT_LO16
3110 || *r == BFD_RELOC_MIPS_CALL_LO16);
3111 continue;
3113 case 'u':
3114 macro_read_relocs (&args, r);
3115 assert (ep != NULL
3116 && (ep->X_op == O_constant
3117 || (ep->X_op == O_symbol
3118 && (*r == BFD_RELOC_MIPS_HIGHEST
3119 || *r == BFD_RELOC_HI16_S
3120 || *r == BFD_RELOC_HI16
3121 || *r == BFD_RELOC_GPREL16
3122 || *r == BFD_RELOC_MIPS_GOT_HI16
3123 || *r == BFD_RELOC_MIPS_CALL_HI16))));
3124 continue;
3126 case 'p':
3127 assert (ep != NULL);
3129 * This allows macro() to pass an immediate expression for
3130 * creating short branches without creating a symbol.
3131 * Note that the expression still might come from the assembly
3132 * input, in which case the value is not checked for range nor
3133 * is a relocation entry generated (yuck).
3135 if (ep->X_op == O_constant)
3137 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3138 ep = NULL;
3140 else
3141 *r = BFD_RELOC_16_PCREL_S2;
3142 continue;
3144 case 'a':
3145 assert (ep != NULL);
3146 *r = BFD_RELOC_MIPS_JMP;
3147 continue;
3149 case 'C':
3150 insn.insn_opcode |= va_arg (args, unsigned long);
3151 continue;
3153 default:
3154 internalError ();
3156 break;
3158 va_end (args);
3159 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3161 append_insn (&insn, ep, r);
3164 static void
3165 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
3166 va_list args)
3168 struct mips_opcode *mo;
3169 struct mips_cl_insn insn;
3170 bfd_reloc_code_real_type r[3]
3171 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3173 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3174 assert (mo);
3175 assert (strcmp (name, mo->name) == 0);
3177 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
3179 ++mo;
3180 assert (mo->name);
3181 assert (strcmp (name, mo->name) == 0);
3184 create_insn (&insn, mo);
3185 for (;;)
3187 int c;
3189 c = *fmt++;
3190 switch (c)
3192 case '\0':
3193 break;
3195 case ',':
3196 case '(':
3197 case ')':
3198 continue;
3200 case 'y':
3201 case 'w':
3202 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
3203 continue;
3205 case 'x':
3206 case 'v':
3207 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
3208 continue;
3210 case 'z':
3211 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
3212 continue;
3214 case 'Z':
3215 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
3216 continue;
3218 case '0':
3219 case 'S':
3220 case 'P':
3221 case 'R':
3222 continue;
3224 case 'X':
3225 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
3226 continue;
3228 case 'Y':
3230 int regno;
3232 regno = va_arg (args, int);
3233 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3234 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3236 continue;
3238 case '<':
3239 case '>':
3240 case '4':
3241 case '5':
3242 case 'H':
3243 case 'W':
3244 case 'D':
3245 case 'j':
3246 case '8':
3247 case 'V':
3248 case 'C':
3249 case 'U':
3250 case 'k':
3251 case 'K':
3252 case 'p':
3253 case 'q':
3255 assert (ep != NULL);
3257 if (ep->X_op != O_constant)
3258 *r = (int) BFD_RELOC_UNUSED + c;
3259 else
3261 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3262 FALSE, &insn.insn_opcode, &insn.use_extend,
3263 &insn.extend);
3264 ep = NULL;
3265 *r = BFD_RELOC_UNUSED;
3268 continue;
3270 case '6':
3271 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
3272 continue;
3275 break;
3278 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3280 append_insn (&insn, ep, r);
3284 * Sign-extend 32-bit mode constants that have bit 31 set and all
3285 * higher bits unset.
3287 static void
3288 normalize_constant_expr (expressionS *ex)
3290 if (ex->X_op == O_constant
3291 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3292 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3293 - 0x80000000);
3297 * Sign-extend 32-bit mode address offsets that have bit 31 set and
3298 * all higher bits unset.
3300 static void
3301 normalize_address_expr (expressionS *ex)
3303 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
3304 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
3305 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3306 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3307 - 0x80000000);
3311 * Generate a "jalr" instruction with a relocation hint to the called
3312 * function. This occurs in NewABI PIC code.
3314 static void
3315 macro_build_jalr (expressionS *ep)
3317 char *f = NULL;
3319 if (HAVE_NEWABI)
3321 frag_grow (8);
3322 f = frag_more (0);
3324 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3325 if (HAVE_NEWABI)
3326 fix_new_exp (frag_now, f - frag_now->fr_literal,
3327 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3331 * Generate a "lui" instruction.
3333 static void
3334 macro_build_lui (expressionS *ep, int regnum)
3336 expressionS high_expr;
3337 const struct mips_opcode *mo;
3338 struct mips_cl_insn insn;
3339 bfd_reloc_code_real_type r[3]
3340 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3341 const char *name = "lui";
3342 const char *fmt = "t,u";
3344 assert (! mips_opts.mips16);
3346 high_expr = *ep;
3348 if (high_expr.X_op == O_constant)
3350 /* we can compute the instruction now without a relocation entry */
3351 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3352 >> 16) & 0xffff;
3353 *r = BFD_RELOC_UNUSED;
3355 else
3357 assert (ep->X_op == O_symbol);
3358 /* _gp_disp is a special case, used from s_cpload.
3359 __gnu_local_gp is used if mips_no_shared. */
3360 assert (mips_pic == NO_PIC
3361 || (! HAVE_NEWABI
3362 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3363 || (! mips_in_shared
3364 && strcmp (S_GET_NAME (ep->X_add_symbol),
3365 "__gnu_local_gp") == 0));
3366 *r = BFD_RELOC_HI16_S;
3369 mo = hash_find (op_hash, name);
3370 assert (strcmp (name, mo->name) == 0);
3371 assert (strcmp (fmt, mo->args) == 0);
3372 create_insn (&insn, mo);
3374 insn.insn_opcode = insn.insn_mo->match;
3375 INSERT_OPERAND (RT, insn, regnum);
3376 if (*r == BFD_RELOC_UNUSED)
3378 insn.insn_opcode |= high_expr.X_add_number;
3379 append_insn (&insn, NULL, r);
3381 else
3382 append_insn (&insn, &high_expr, r);
3385 /* Generate a sequence of instructions to do a load or store from a constant
3386 offset off of a base register (breg) into/from a target register (treg),
3387 using AT if necessary. */
3388 static void
3389 macro_build_ldst_constoffset (expressionS *ep, const char *op,
3390 int treg, int breg, int dbl)
3392 assert (ep->X_op == O_constant);
3394 /* Sign-extending 32-bit constants makes their handling easier. */
3395 if (!dbl)
3396 normalize_constant_expr (ep);
3398 /* Right now, this routine can only handle signed 32-bit constants. */
3399 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3400 as_warn (_("operand overflow"));
3402 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3404 /* Signed 16-bit offset will fit in the op. Easy! */
3405 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
3407 else
3409 /* 32-bit offset, need multiple instructions and AT, like:
3410 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3411 addu $tempreg,$tempreg,$breg
3412 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3413 to handle the complete offset. */
3414 macro_build_lui (ep, AT);
3415 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3416 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
3418 if (mips_opts.noat)
3419 as_bad (_("Macro used $at after \".set noat\""));
3423 /* set_at()
3424 * Generates code to set the $at register to true (one)
3425 * if reg is less than the immediate expression.
3427 static void
3428 set_at (int reg, int unsignedp)
3430 if (imm_expr.X_op == O_constant
3431 && imm_expr.X_add_number >= -0x8000
3432 && imm_expr.X_add_number < 0x8000)
3433 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3434 AT, reg, BFD_RELOC_LO16);
3435 else
3437 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3438 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
3442 /* Warn if an expression is not a constant. */
3444 static void
3445 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3447 if (ex->X_op == O_big)
3448 as_bad (_("unsupported large constant"));
3449 else if (ex->X_op != O_constant)
3450 as_bad (_("Instruction %s requires absolute expression"),
3451 ip->insn_mo->name);
3453 if (HAVE_32BIT_GPRS)
3454 normalize_constant_expr (ex);
3457 /* Count the leading zeroes by performing a binary chop. This is a
3458 bulky bit of source, but performance is a LOT better for the
3459 majority of values than a simple loop to count the bits:
3460 for (lcnt = 0; (lcnt < 32); lcnt++)
3461 if ((v) & (1 << (31 - lcnt)))
3462 break;
3463 However it is not code size friendly, and the gain will drop a bit
3464 on certain cached systems.
3466 #define COUNT_TOP_ZEROES(v) \
3467 (((v) & ~0xffff) == 0 \
3468 ? ((v) & ~0xff) == 0 \
3469 ? ((v) & ~0xf) == 0 \
3470 ? ((v) & ~0x3) == 0 \
3471 ? ((v) & ~0x1) == 0 \
3472 ? !(v) \
3473 ? 32 \
3474 : 31 \
3475 : 30 \
3476 : ((v) & ~0x7) == 0 \
3477 ? 29 \
3478 : 28 \
3479 : ((v) & ~0x3f) == 0 \
3480 ? ((v) & ~0x1f) == 0 \
3481 ? 27 \
3482 : 26 \
3483 : ((v) & ~0x7f) == 0 \
3484 ? 25 \
3485 : 24 \
3486 : ((v) & ~0xfff) == 0 \
3487 ? ((v) & ~0x3ff) == 0 \
3488 ? ((v) & ~0x1ff) == 0 \
3489 ? 23 \
3490 : 22 \
3491 : ((v) & ~0x7ff) == 0 \
3492 ? 21 \
3493 : 20 \
3494 : ((v) & ~0x3fff) == 0 \
3495 ? ((v) & ~0x1fff) == 0 \
3496 ? 19 \
3497 : 18 \
3498 : ((v) & ~0x7fff) == 0 \
3499 ? 17 \
3500 : 16 \
3501 : ((v) & ~0xffffff) == 0 \
3502 ? ((v) & ~0xfffff) == 0 \
3503 ? ((v) & ~0x3ffff) == 0 \
3504 ? ((v) & ~0x1ffff) == 0 \
3505 ? 15 \
3506 : 14 \
3507 : ((v) & ~0x7ffff) == 0 \
3508 ? 13 \
3509 : 12 \
3510 : ((v) & ~0x3fffff) == 0 \
3511 ? ((v) & ~0x1fffff) == 0 \
3512 ? 11 \
3513 : 10 \
3514 : ((v) & ~0x7fffff) == 0 \
3515 ? 9 \
3516 : 8 \
3517 : ((v) & ~0xfffffff) == 0 \
3518 ? ((v) & ~0x3ffffff) == 0 \
3519 ? ((v) & ~0x1ffffff) == 0 \
3520 ? 7 \
3521 : 6 \
3522 : ((v) & ~0x7ffffff) == 0 \
3523 ? 5 \
3524 : 4 \
3525 : ((v) & ~0x3fffffff) == 0 \
3526 ? ((v) & ~0x1fffffff) == 0 \
3527 ? 3 \
3528 : 2 \
3529 : ((v) & ~0x7fffffff) == 0 \
3530 ? 1 \
3531 : 0)
3533 /* load_register()
3534 * This routine generates the least number of instructions necessary to load
3535 * an absolute expression value into a register.
3537 static void
3538 load_register (int reg, expressionS *ep, int dbl)
3540 int freg;
3541 expressionS hi32, lo32;
3543 if (ep->X_op != O_big)
3545 assert (ep->X_op == O_constant);
3547 /* Sign-extending 32-bit constants makes their handling easier. */
3548 if (!dbl)
3549 normalize_constant_expr (ep);
3551 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3553 /* We can handle 16 bit signed values with an addiu to
3554 $zero. No need to ever use daddiu here, since $zero and
3555 the result are always correct in 32 bit mode. */
3556 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3557 return;
3559 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3561 /* We can handle 16 bit unsigned values with an ori to
3562 $zero. */
3563 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3564 return;
3566 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3568 /* 32 bit values require an lui. */
3569 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3570 if ((ep->X_add_number & 0xffff) != 0)
3571 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3572 return;
3576 /* The value is larger than 32 bits. */
3578 if (!dbl || HAVE_32BIT_GPRS)
3580 char value[32];
3582 sprintf_vma (value, ep->X_add_number);
3583 as_bad (_("Number (0x%s) larger than 32 bits"), value);
3584 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3585 return;
3588 if (ep->X_op != O_big)
3590 hi32 = *ep;
3591 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3592 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3593 hi32.X_add_number &= 0xffffffff;
3594 lo32 = *ep;
3595 lo32.X_add_number &= 0xffffffff;
3597 else
3599 assert (ep->X_add_number > 2);
3600 if (ep->X_add_number == 3)
3601 generic_bignum[3] = 0;
3602 else if (ep->X_add_number > 4)
3603 as_bad (_("Number larger than 64 bits"));
3604 lo32.X_op = O_constant;
3605 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3606 hi32.X_op = O_constant;
3607 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3610 if (hi32.X_add_number == 0)
3611 freg = 0;
3612 else
3614 int shift, bit;
3615 unsigned long hi, lo;
3617 if (hi32.X_add_number == (offsetT) 0xffffffff)
3619 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3621 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3622 return;
3624 if (lo32.X_add_number & 0x80000000)
3626 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3627 if (lo32.X_add_number & 0xffff)
3628 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3629 return;
3633 /* Check for 16bit shifted constant. We know that hi32 is
3634 non-zero, so start the mask on the first bit of the hi32
3635 value. */
3636 shift = 17;
3639 unsigned long himask, lomask;
3641 if (shift < 32)
3643 himask = 0xffff >> (32 - shift);
3644 lomask = (0xffff << shift) & 0xffffffff;
3646 else
3648 himask = 0xffff << (shift - 32);
3649 lomask = 0;
3651 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3652 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3654 expressionS tmp;
3656 tmp.X_op = O_constant;
3657 if (shift < 32)
3658 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3659 | (lo32.X_add_number >> shift));
3660 else
3661 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3662 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3663 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3664 reg, reg, (shift >= 32) ? shift - 32 : shift);
3665 return;
3667 ++shift;
3669 while (shift <= (64 - 16));
3671 /* Find the bit number of the lowest one bit, and store the
3672 shifted value in hi/lo. */
3673 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3674 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3675 if (lo != 0)
3677 bit = 0;
3678 while ((lo & 1) == 0)
3680 lo >>= 1;
3681 ++bit;
3683 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3684 hi >>= bit;
3686 else
3688 bit = 32;
3689 while ((hi & 1) == 0)
3691 hi >>= 1;
3692 ++bit;
3694 lo = hi;
3695 hi = 0;
3698 /* Optimize if the shifted value is a (power of 2) - 1. */
3699 if ((hi == 0 && ((lo + 1) & lo) == 0)
3700 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3702 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3703 if (shift != 0)
3705 expressionS tmp;
3707 /* This instruction will set the register to be all
3708 ones. */
3709 tmp.X_op = O_constant;
3710 tmp.X_add_number = (offsetT) -1;
3711 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3712 if (bit != 0)
3714 bit += shift;
3715 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3716 reg, reg, (bit >= 32) ? bit - 32 : bit);
3718 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3719 reg, reg, (shift >= 32) ? shift - 32 : shift);
3720 return;
3724 /* Sign extend hi32 before calling load_register, because we can
3725 generally get better code when we load a sign extended value. */
3726 if ((hi32.X_add_number & 0x80000000) != 0)
3727 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3728 load_register (reg, &hi32, 0);
3729 freg = reg;
3731 if ((lo32.X_add_number & 0xffff0000) == 0)
3733 if (freg != 0)
3735 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
3736 freg = reg;
3739 else
3741 expressionS mid16;
3743 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3745 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3746 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
3747 return;
3750 if (freg != 0)
3752 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
3753 freg = reg;
3755 mid16 = lo32;
3756 mid16.X_add_number >>= 16;
3757 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3758 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3759 freg = reg;
3761 if ((lo32.X_add_number & 0xffff) != 0)
3762 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3765 static inline void
3766 load_delay_nop (void)
3768 if (!gpr_interlocks)
3769 macro_build (NULL, "nop", "");
3772 /* Load an address into a register. */
3774 static void
3775 load_address (int reg, expressionS *ep, int *used_at)
3777 if (ep->X_op != O_constant
3778 && ep->X_op != O_symbol)
3780 as_bad (_("expression too complex"));
3781 ep->X_op = O_constant;
3784 if (ep->X_op == O_constant)
3786 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
3787 return;
3790 if (mips_pic == NO_PIC)
3792 /* If this is a reference to a GP relative symbol, we want
3793 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3794 Otherwise we want
3795 lui $reg,<sym> (BFD_RELOC_HI16_S)
3796 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3797 If we have an addend, we always use the latter form.
3799 With 64bit address space and a usable $at we want
3800 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3801 lui $at,<sym> (BFD_RELOC_HI16_S)
3802 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3803 daddiu $at,<sym> (BFD_RELOC_LO16)
3804 dsll32 $reg,0
3805 daddu $reg,$reg,$at
3807 If $at is already in use, we use a path which is suboptimal
3808 on superscalar processors.
3809 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3810 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3811 dsll $reg,16
3812 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3813 dsll $reg,16
3814 daddiu $reg,<sym> (BFD_RELOC_LO16)
3816 For GP relative symbols in 64bit address space we can use
3817 the same sequence as in 32bit address space. */
3818 if (HAVE_64BIT_SYMBOLS)
3820 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3821 && !nopic_need_relax (ep->X_add_symbol, 1))
3823 relax_start (ep->X_add_symbol);
3824 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3825 mips_gp_register, BFD_RELOC_GPREL16);
3826 relax_switch ();
3829 if (*used_at == 0 && !mips_opts.noat)
3831 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3832 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3833 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3834 BFD_RELOC_MIPS_HIGHER);
3835 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3836 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3837 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
3838 *used_at = 1;
3840 else
3842 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3843 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3844 BFD_RELOC_MIPS_HIGHER);
3845 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3846 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3847 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3848 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
3851 if (mips_relax.sequence)
3852 relax_end ();
3854 else
3856 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3857 && !nopic_need_relax (ep->X_add_symbol, 1))
3859 relax_start (ep->X_add_symbol);
3860 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3861 mips_gp_register, BFD_RELOC_GPREL16);
3862 relax_switch ();
3864 macro_build_lui (ep, reg);
3865 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3866 reg, reg, BFD_RELOC_LO16);
3867 if (mips_relax.sequence)
3868 relax_end ();
3871 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3873 expressionS ex;
3875 /* If this is a reference to an external symbol, we want
3876 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3877 Otherwise we want
3878 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3880 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3881 If there is a constant, it must be added in after.
3883 If we have NewABI, we want
3884 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3885 unless we're referencing a global symbol with a non-zero
3886 offset, in which case cst must be added separately. */
3887 if (HAVE_NEWABI)
3889 if (ep->X_add_number)
3891 ex.X_add_number = ep->X_add_number;
3892 ep->X_add_number = 0;
3893 relax_start (ep->X_add_symbol);
3894 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3895 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3896 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3897 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3898 ex.X_op = O_constant;
3899 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3900 reg, reg, BFD_RELOC_LO16);
3901 ep->X_add_number = ex.X_add_number;
3902 relax_switch ();
3904 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3905 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3906 if (mips_relax.sequence)
3907 relax_end ();
3909 else
3911 ex.X_add_number = ep->X_add_number;
3912 ep->X_add_number = 0;
3913 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3914 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3915 load_delay_nop ();
3916 relax_start (ep->X_add_symbol);
3917 relax_switch ();
3918 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3919 BFD_RELOC_LO16);
3920 relax_end ();
3922 if (ex.X_add_number != 0)
3924 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3925 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3926 ex.X_op = O_constant;
3927 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3928 reg, reg, BFD_RELOC_LO16);
3932 else if (mips_pic == SVR4_PIC)
3934 expressionS ex;
3936 /* This is the large GOT case. If this is a reference to an
3937 external symbol, we want
3938 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3939 addu $reg,$reg,$gp
3940 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3942 Otherwise, for a reference to a local symbol in old ABI, we want
3943 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3945 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3946 If there is a constant, it must be added in after.
3948 In the NewABI, for local symbols, with or without offsets, we want:
3949 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3950 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
3952 if (HAVE_NEWABI)
3954 ex.X_add_number = ep->X_add_number;
3955 ep->X_add_number = 0;
3956 relax_start (ep->X_add_symbol);
3957 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3958 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3959 reg, reg, mips_gp_register);
3960 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3961 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
3962 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3963 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3964 else if (ex.X_add_number)
3966 ex.X_op = O_constant;
3967 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3968 BFD_RELOC_LO16);
3971 ep->X_add_number = ex.X_add_number;
3972 relax_switch ();
3973 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3974 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3975 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3976 BFD_RELOC_MIPS_GOT_OFST);
3977 relax_end ();
3979 else
3981 ex.X_add_number = ep->X_add_number;
3982 ep->X_add_number = 0;
3983 relax_start (ep->X_add_symbol);
3984 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3985 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3986 reg, reg, mips_gp_register);
3987 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3988 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
3989 relax_switch ();
3990 if (reg_needs_delay (mips_gp_register))
3992 /* We need a nop before loading from $gp. This special
3993 check is required because the lui which starts the main
3994 instruction stream does not refer to $gp, and so will not
3995 insert the nop which may be required. */
3996 macro_build (NULL, "nop", "");
3998 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3999 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4000 load_delay_nop ();
4001 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4002 BFD_RELOC_LO16);
4003 relax_end ();
4005 if (ex.X_add_number != 0)
4007 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4008 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4009 ex.X_op = O_constant;
4010 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4011 BFD_RELOC_LO16);
4015 else
4016 abort ();
4018 if (mips_opts.noat && *used_at == 1)
4019 as_bad (_("Macro used $at after \".set noat\""));
4022 /* Move the contents of register SOURCE into register DEST. */
4024 static void
4025 move_register (int dest, int source)
4027 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4028 dest, source, 0);
4031 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
4032 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4033 The two alternatives are:
4035 Global symbol Local sybmol
4036 ------------- ------------
4037 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4038 ... ...
4039 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4041 load_got_offset emits the first instruction and add_got_offset
4042 emits the second for a 16-bit offset or add_got_offset_hilo emits
4043 a sequence to add a 32-bit offset using a scratch register. */
4045 static void
4046 load_got_offset (int dest, expressionS *local)
4048 expressionS global;
4050 global = *local;
4051 global.X_add_number = 0;
4053 relax_start (local->X_add_symbol);
4054 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4055 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4056 relax_switch ();
4057 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4058 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4059 relax_end ();
4062 static void
4063 add_got_offset (int dest, expressionS *local)
4065 expressionS global;
4067 global.X_op = O_constant;
4068 global.X_op_symbol = NULL;
4069 global.X_add_symbol = NULL;
4070 global.X_add_number = local->X_add_number;
4072 relax_start (local->X_add_symbol);
4073 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4074 dest, dest, BFD_RELOC_LO16);
4075 relax_switch ();
4076 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4077 relax_end ();
4080 static void
4081 add_got_offset_hilo (int dest, expressionS *local, int tmp)
4083 expressionS global;
4084 int hold_mips_optimize;
4086 global.X_op = O_constant;
4087 global.X_op_symbol = NULL;
4088 global.X_add_symbol = NULL;
4089 global.X_add_number = local->X_add_number;
4091 relax_start (local->X_add_symbol);
4092 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4093 relax_switch ();
4094 /* Set mips_optimize around the lui instruction to avoid
4095 inserting an unnecessary nop after the lw. */
4096 hold_mips_optimize = mips_optimize;
4097 mips_optimize = 2;
4098 macro_build_lui (&global, tmp);
4099 mips_optimize = hold_mips_optimize;
4100 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4101 relax_end ();
4103 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4107 * Build macros
4108 * This routine implements the seemingly endless macro or synthesized
4109 * instructions and addressing modes in the mips assembly language. Many
4110 * of these macros are simple and are similar to each other. These could
4111 * probably be handled by some kind of table or grammar approach instead of
4112 * this verbose method. Others are not simple macros but are more like
4113 * optimizing code generation.
4114 * One interesting optimization is when several store macros appear
4115 * consecutively that would load AT with the upper half of the same address.
4116 * The ensuing load upper instructions are ommited. This implies some kind
4117 * of global optimization. We currently only optimize within a single macro.
4118 * For many of the load and store macros if the address is specified as a
4119 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4120 * first load register 'at' with zero and use it as the base register. The
4121 * mips assembler simply uses register $zero. Just one tiny optimization
4122 * we're missing.
4124 static void
4125 macro (struct mips_cl_insn *ip)
4127 register int treg, sreg, dreg, breg;
4128 int tempreg;
4129 int mask;
4130 int used_at = 0;
4131 expressionS expr1;
4132 const char *s;
4133 const char *s2;
4134 const char *fmt;
4135 int likely = 0;
4136 int dbl = 0;
4137 int coproc = 0;
4138 int lr = 0;
4139 int imm = 0;
4140 int call = 0;
4141 int off;
4142 offsetT maxnum;
4143 bfd_reloc_code_real_type r;
4144 int hold_mips_optimize;
4146 assert (! mips_opts.mips16);
4148 treg = (ip->insn_opcode >> 16) & 0x1f;
4149 dreg = (ip->insn_opcode >> 11) & 0x1f;
4150 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4151 mask = ip->insn_mo->mask;
4153 expr1.X_op = O_constant;
4154 expr1.X_op_symbol = NULL;
4155 expr1.X_add_symbol = NULL;
4156 expr1.X_add_number = 1;
4158 switch (mask)
4160 case M_DABS:
4161 dbl = 1;
4162 case M_ABS:
4163 /* bgez $a0,.+12
4164 move v0,$a0
4165 sub v0,$zero,$a0
4168 start_noreorder ();
4170 expr1.X_add_number = 8;
4171 macro_build (&expr1, "bgez", "s,p", sreg);
4172 if (dreg == sreg)
4173 macro_build (NULL, "nop", "", 0);
4174 else
4175 move_register (dreg, sreg);
4176 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4178 end_noreorder ();
4179 break;
4181 case M_ADD_I:
4182 s = "addi";
4183 s2 = "add";
4184 goto do_addi;
4185 case M_ADDU_I:
4186 s = "addiu";
4187 s2 = "addu";
4188 goto do_addi;
4189 case M_DADD_I:
4190 dbl = 1;
4191 s = "daddi";
4192 s2 = "dadd";
4193 goto do_addi;
4194 case M_DADDU_I:
4195 dbl = 1;
4196 s = "daddiu";
4197 s2 = "daddu";
4198 do_addi:
4199 if (imm_expr.X_op == O_constant
4200 && imm_expr.X_add_number >= -0x8000
4201 && imm_expr.X_add_number < 0x8000)
4203 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
4204 break;
4206 used_at = 1;
4207 load_register (AT, &imm_expr, dbl);
4208 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4209 break;
4211 case M_AND_I:
4212 s = "andi";
4213 s2 = "and";
4214 goto do_bit;
4215 case M_OR_I:
4216 s = "ori";
4217 s2 = "or";
4218 goto do_bit;
4219 case M_NOR_I:
4220 s = "";
4221 s2 = "nor";
4222 goto do_bit;
4223 case M_XOR_I:
4224 s = "xori";
4225 s2 = "xor";
4226 do_bit:
4227 if (imm_expr.X_op == O_constant
4228 && imm_expr.X_add_number >= 0
4229 && imm_expr.X_add_number < 0x10000)
4231 if (mask != M_NOR_I)
4232 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
4233 else
4235 macro_build (&imm_expr, "ori", "t,r,i",
4236 treg, sreg, BFD_RELOC_LO16);
4237 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
4239 break;
4242 used_at = 1;
4243 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4244 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4245 break;
4247 case M_BEQ_I:
4248 s = "beq";
4249 goto beq_i;
4250 case M_BEQL_I:
4251 s = "beql";
4252 likely = 1;
4253 goto beq_i;
4254 case M_BNE_I:
4255 s = "bne";
4256 goto beq_i;
4257 case M_BNEL_I:
4258 s = "bnel";
4259 likely = 1;
4260 beq_i:
4261 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4263 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
4264 break;
4266 used_at = 1;
4267 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4268 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
4269 break;
4271 case M_BGEL:
4272 likely = 1;
4273 case M_BGE:
4274 if (treg == 0)
4276 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4277 break;
4279 if (sreg == 0)
4281 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
4282 break;
4284 used_at = 1;
4285 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4286 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4287 break;
4289 case M_BGTL_I:
4290 likely = 1;
4291 case M_BGT_I:
4292 /* check for > max integer */
4293 maxnum = 0x7fffffff;
4294 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4296 maxnum <<= 16;
4297 maxnum |= 0xffff;
4298 maxnum <<= 16;
4299 maxnum |= 0xffff;
4301 if (imm_expr.X_op == O_constant
4302 && imm_expr.X_add_number >= maxnum
4303 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4305 do_false:
4306 /* result is always false */
4307 if (! likely)
4308 macro_build (NULL, "nop", "", 0);
4309 else
4310 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
4311 break;
4313 if (imm_expr.X_op != O_constant)
4314 as_bad (_("Unsupported large constant"));
4315 ++imm_expr.X_add_number;
4316 /* FALLTHROUGH */
4317 case M_BGE_I:
4318 case M_BGEL_I:
4319 if (mask == M_BGEL_I)
4320 likely = 1;
4321 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4323 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4324 break;
4326 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4328 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4329 break;
4331 maxnum = 0x7fffffff;
4332 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4334 maxnum <<= 16;
4335 maxnum |= 0xffff;
4336 maxnum <<= 16;
4337 maxnum |= 0xffff;
4339 maxnum = - maxnum - 1;
4340 if (imm_expr.X_op == O_constant
4341 && imm_expr.X_add_number <= maxnum
4342 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4344 do_true:
4345 /* result is always true */
4346 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4347 macro_build (&offset_expr, "b", "p");
4348 break;
4350 used_at = 1;
4351 set_at (sreg, 0);
4352 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4353 break;
4355 case M_BGEUL:
4356 likely = 1;
4357 case M_BGEU:
4358 if (treg == 0)
4359 goto do_true;
4360 if (sreg == 0)
4362 macro_build (&offset_expr, likely ? "beql" : "beq",
4363 "s,t,p", 0, treg);
4364 break;
4366 used_at = 1;
4367 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4368 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4369 break;
4371 case M_BGTUL_I:
4372 likely = 1;
4373 case M_BGTU_I:
4374 if (sreg == 0
4375 || (HAVE_32BIT_GPRS
4376 && imm_expr.X_op == O_constant
4377 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4378 goto do_false;
4379 if (imm_expr.X_op != O_constant)
4380 as_bad (_("Unsupported large constant"));
4381 ++imm_expr.X_add_number;
4382 /* FALLTHROUGH */
4383 case M_BGEU_I:
4384 case M_BGEUL_I:
4385 if (mask == M_BGEUL_I)
4386 likely = 1;
4387 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4388 goto do_true;
4389 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4391 macro_build (&offset_expr, likely ? "bnel" : "bne",
4392 "s,t,p", sreg, 0);
4393 break;
4395 used_at = 1;
4396 set_at (sreg, 1);
4397 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4398 break;
4400 case M_BGTL:
4401 likely = 1;
4402 case M_BGT:
4403 if (treg == 0)
4405 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4406 break;
4408 if (sreg == 0)
4410 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
4411 break;
4413 used_at = 1;
4414 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4415 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4416 break;
4418 case M_BGTUL:
4419 likely = 1;
4420 case M_BGTU:
4421 if (treg == 0)
4423 macro_build (&offset_expr, likely ? "bnel" : "bne",
4424 "s,t,p", sreg, 0);
4425 break;
4427 if (sreg == 0)
4428 goto do_false;
4429 used_at = 1;
4430 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4431 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4432 break;
4434 case M_BLEL:
4435 likely = 1;
4436 case M_BLE:
4437 if (treg == 0)
4439 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4440 break;
4442 if (sreg == 0)
4444 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
4445 break;
4447 used_at = 1;
4448 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4449 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4450 break;
4452 case M_BLEL_I:
4453 likely = 1;
4454 case M_BLE_I:
4455 maxnum = 0x7fffffff;
4456 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4458 maxnum <<= 16;
4459 maxnum |= 0xffff;
4460 maxnum <<= 16;
4461 maxnum |= 0xffff;
4463 if (imm_expr.X_op == O_constant
4464 && imm_expr.X_add_number >= maxnum
4465 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4466 goto do_true;
4467 if (imm_expr.X_op != O_constant)
4468 as_bad (_("Unsupported large constant"));
4469 ++imm_expr.X_add_number;
4470 /* FALLTHROUGH */
4471 case M_BLT_I:
4472 case M_BLTL_I:
4473 if (mask == M_BLTL_I)
4474 likely = 1;
4475 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4477 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4478 break;
4480 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4482 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4483 break;
4485 used_at = 1;
4486 set_at (sreg, 0);
4487 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4488 break;
4490 case M_BLEUL:
4491 likely = 1;
4492 case M_BLEU:
4493 if (treg == 0)
4495 macro_build (&offset_expr, likely ? "beql" : "beq",
4496 "s,t,p", sreg, 0);
4497 break;
4499 if (sreg == 0)
4500 goto do_true;
4501 used_at = 1;
4502 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4503 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4504 break;
4506 case M_BLEUL_I:
4507 likely = 1;
4508 case M_BLEU_I:
4509 if (sreg == 0
4510 || (HAVE_32BIT_GPRS
4511 && imm_expr.X_op == O_constant
4512 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4513 goto do_true;
4514 if (imm_expr.X_op != O_constant)
4515 as_bad (_("Unsupported large constant"));
4516 ++imm_expr.X_add_number;
4517 /* FALLTHROUGH */
4518 case M_BLTU_I:
4519 case M_BLTUL_I:
4520 if (mask == M_BLTUL_I)
4521 likely = 1;
4522 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4523 goto do_false;
4524 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4526 macro_build (&offset_expr, likely ? "beql" : "beq",
4527 "s,t,p", sreg, 0);
4528 break;
4530 used_at = 1;
4531 set_at (sreg, 1);
4532 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4533 break;
4535 case M_BLTL:
4536 likely = 1;
4537 case M_BLT:
4538 if (treg == 0)
4540 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4541 break;
4543 if (sreg == 0)
4545 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
4546 break;
4548 used_at = 1;
4549 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4550 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4551 break;
4553 case M_BLTUL:
4554 likely = 1;
4555 case M_BLTU:
4556 if (treg == 0)
4557 goto do_false;
4558 if (sreg == 0)
4560 macro_build (&offset_expr, likely ? "bnel" : "bne",
4561 "s,t,p", 0, treg);
4562 break;
4564 used_at = 1;
4565 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4566 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4567 break;
4569 case M_DEXT:
4571 unsigned long pos;
4572 unsigned long size;
4574 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4576 as_bad (_("Unsupported large constant"));
4577 pos = size = 1;
4579 else
4581 pos = (unsigned long) imm_expr.X_add_number;
4582 size = (unsigned long) imm2_expr.X_add_number;
4585 if (pos > 63)
4587 as_bad (_("Improper position (%lu)"), pos);
4588 pos = 1;
4590 if (size == 0 || size > 64
4591 || (pos + size - 1) > 63)
4593 as_bad (_("Improper extract size (%lu, position %lu)"),
4594 size, pos);
4595 size = 1;
4598 if (size <= 32 && pos < 32)
4600 s = "dext";
4601 fmt = "t,r,+A,+C";
4603 else if (size <= 32)
4605 s = "dextu";
4606 fmt = "t,r,+E,+H";
4608 else
4610 s = "dextm";
4611 fmt = "t,r,+A,+G";
4613 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
4615 break;
4617 case M_DINS:
4619 unsigned long pos;
4620 unsigned long size;
4622 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4624 as_bad (_("Unsupported large constant"));
4625 pos = size = 1;
4627 else
4629 pos = (unsigned long) imm_expr.X_add_number;
4630 size = (unsigned long) imm2_expr.X_add_number;
4633 if (pos > 63)
4635 as_bad (_("Improper position (%lu)"), pos);
4636 pos = 1;
4638 if (size == 0 || size > 64
4639 || (pos + size - 1) > 63)
4641 as_bad (_("Improper insert size (%lu, position %lu)"),
4642 size, pos);
4643 size = 1;
4646 if (pos < 32 && (pos + size - 1) < 32)
4648 s = "dins";
4649 fmt = "t,r,+A,+B";
4651 else if (pos >= 32)
4653 s = "dinsu";
4654 fmt = "t,r,+E,+F";
4656 else
4658 s = "dinsm";
4659 fmt = "t,r,+A,+F";
4661 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4662 pos + size - 1);
4664 break;
4666 case M_DDIV_3:
4667 dbl = 1;
4668 case M_DIV_3:
4669 s = "mflo";
4670 goto do_div3;
4671 case M_DREM_3:
4672 dbl = 1;
4673 case M_REM_3:
4674 s = "mfhi";
4675 do_div3:
4676 if (treg == 0)
4678 as_warn (_("Divide by zero."));
4679 if (mips_trap)
4680 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4681 else
4682 macro_build (NULL, "break", "c", 7);
4683 break;
4686 start_noreorder ();
4687 if (mips_trap)
4689 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4690 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4692 else
4694 expr1.X_add_number = 8;
4695 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4696 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4697 macro_build (NULL, "break", "c", 7);
4699 expr1.X_add_number = -1;
4700 used_at = 1;
4701 load_register (AT, &expr1, dbl);
4702 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4703 macro_build (&expr1, "bne", "s,t,p", treg, AT);
4704 if (dbl)
4706 expr1.X_add_number = 1;
4707 load_register (AT, &expr1, dbl);
4708 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
4710 else
4712 expr1.X_add_number = 0x80000000;
4713 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
4715 if (mips_trap)
4717 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
4718 /* We want to close the noreorder block as soon as possible, so
4719 that later insns are available for delay slot filling. */
4720 end_noreorder ();
4722 else
4724 expr1.X_add_number = 8;
4725 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4726 macro_build (NULL, "nop", "", 0);
4728 /* We want to close the noreorder block as soon as possible, so
4729 that later insns are available for delay slot filling. */
4730 end_noreorder ();
4732 macro_build (NULL, "break", "c", 6);
4734 macro_build (NULL, s, "d", dreg);
4735 break;
4737 case M_DIV_3I:
4738 s = "div";
4739 s2 = "mflo";
4740 goto do_divi;
4741 case M_DIVU_3I:
4742 s = "divu";
4743 s2 = "mflo";
4744 goto do_divi;
4745 case M_REM_3I:
4746 s = "div";
4747 s2 = "mfhi";
4748 goto do_divi;
4749 case M_REMU_3I:
4750 s = "divu";
4751 s2 = "mfhi";
4752 goto do_divi;
4753 case M_DDIV_3I:
4754 dbl = 1;
4755 s = "ddiv";
4756 s2 = "mflo";
4757 goto do_divi;
4758 case M_DDIVU_3I:
4759 dbl = 1;
4760 s = "ddivu";
4761 s2 = "mflo";
4762 goto do_divi;
4763 case M_DREM_3I:
4764 dbl = 1;
4765 s = "ddiv";
4766 s2 = "mfhi";
4767 goto do_divi;
4768 case M_DREMU_3I:
4769 dbl = 1;
4770 s = "ddivu";
4771 s2 = "mfhi";
4772 do_divi:
4773 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4775 as_warn (_("Divide by zero."));
4776 if (mips_trap)
4777 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4778 else
4779 macro_build (NULL, "break", "c", 7);
4780 break;
4782 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4784 if (strcmp (s2, "mflo") == 0)
4785 move_register (dreg, sreg);
4786 else
4787 move_register (dreg, 0);
4788 break;
4790 if (imm_expr.X_op == O_constant
4791 && imm_expr.X_add_number == -1
4792 && s[strlen (s) - 1] != 'u')
4794 if (strcmp (s2, "mflo") == 0)
4796 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4798 else
4799 move_register (dreg, 0);
4800 break;
4803 used_at = 1;
4804 load_register (AT, &imm_expr, dbl);
4805 macro_build (NULL, s, "z,s,t", sreg, AT);
4806 macro_build (NULL, s2, "d", dreg);
4807 break;
4809 case M_DIVU_3:
4810 s = "divu";
4811 s2 = "mflo";
4812 goto do_divu3;
4813 case M_REMU_3:
4814 s = "divu";
4815 s2 = "mfhi";
4816 goto do_divu3;
4817 case M_DDIVU_3:
4818 s = "ddivu";
4819 s2 = "mflo";
4820 goto do_divu3;
4821 case M_DREMU_3:
4822 s = "ddivu";
4823 s2 = "mfhi";
4824 do_divu3:
4825 start_noreorder ();
4826 if (mips_trap)
4828 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4829 macro_build (NULL, s, "z,s,t", sreg, treg);
4830 /* We want to close the noreorder block as soon as possible, so
4831 that later insns are available for delay slot filling. */
4832 end_noreorder ();
4834 else
4836 expr1.X_add_number = 8;
4837 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4838 macro_build (NULL, s, "z,s,t", sreg, treg);
4840 /* We want to close the noreorder block as soon as possible, so
4841 that later insns are available for delay slot filling. */
4842 end_noreorder ();
4843 macro_build (NULL, "break", "c", 7);
4845 macro_build (NULL, s2, "d", dreg);
4846 break;
4848 case M_DLCA_AB:
4849 dbl = 1;
4850 case M_LCA_AB:
4851 call = 1;
4852 goto do_la;
4853 case M_DLA_AB:
4854 dbl = 1;
4855 case M_LA_AB:
4856 do_la:
4857 /* Load the address of a symbol into a register. If breg is not
4858 zero, we then add a base register to it. */
4860 if (dbl && HAVE_32BIT_GPRS)
4861 as_warn (_("dla used to load 32-bit register"));
4863 if (! dbl && HAVE_64BIT_OBJECTS)
4864 as_warn (_("la used to load 64-bit address"));
4866 if (offset_expr.X_op == O_constant
4867 && offset_expr.X_add_number >= -0x8000
4868 && offset_expr.X_add_number < 0x8000)
4870 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
4871 "t,r,j", treg, sreg, BFD_RELOC_LO16);
4872 break;
4875 if (!mips_opts.noat && (treg == breg))
4877 tempreg = AT;
4878 used_at = 1;
4880 else
4882 tempreg = treg;
4885 if (offset_expr.X_op != O_symbol
4886 && offset_expr.X_op != O_constant)
4888 as_bad (_("expression too complex"));
4889 offset_expr.X_op = O_constant;
4892 if (offset_expr.X_op == O_constant)
4893 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
4894 else if (mips_pic == NO_PIC)
4896 /* If this is a reference to a GP relative symbol, we want
4897 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4898 Otherwise we want
4899 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4900 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4901 If we have a constant, we need two instructions anyhow,
4902 so we may as well always use the latter form.
4904 With 64bit address space and a usable $at we want
4905 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4906 lui $at,<sym> (BFD_RELOC_HI16_S)
4907 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4908 daddiu $at,<sym> (BFD_RELOC_LO16)
4909 dsll32 $tempreg,0
4910 daddu $tempreg,$tempreg,$at
4912 If $at is already in use, we use a path which is suboptimal
4913 on superscalar processors.
4914 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4915 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4916 dsll $tempreg,16
4917 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4918 dsll $tempreg,16
4919 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4921 For GP relative symbols in 64bit address space we can use
4922 the same sequence as in 32bit address space. */
4923 if (HAVE_64BIT_SYMBOLS)
4925 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4926 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4928 relax_start (offset_expr.X_add_symbol);
4929 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4930 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4931 relax_switch ();
4934 if (used_at == 0 && !mips_opts.noat)
4936 macro_build (&offset_expr, "lui", "t,u",
4937 tempreg, BFD_RELOC_MIPS_HIGHEST);
4938 macro_build (&offset_expr, "lui", "t,u",
4939 AT, BFD_RELOC_HI16_S);
4940 macro_build (&offset_expr, "daddiu", "t,r,j",
4941 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4942 macro_build (&offset_expr, "daddiu", "t,r,j",
4943 AT, AT, BFD_RELOC_LO16);
4944 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4945 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
4946 used_at = 1;
4948 else
4950 macro_build (&offset_expr, "lui", "t,u",
4951 tempreg, BFD_RELOC_MIPS_HIGHEST);
4952 macro_build (&offset_expr, "daddiu", "t,r,j",
4953 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4954 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4955 macro_build (&offset_expr, "daddiu", "t,r,j",
4956 tempreg, tempreg, BFD_RELOC_HI16_S);
4957 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4958 macro_build (&offset_expr, "daddiu", "t,r,j",
4959 tempreg, tempreg, BFD_RELOC_LO16);
4962 if (mips_relax.sequence)
4963 relax_end ();
4965 else
4967 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4968 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4970 relax_start (offset_expr.X_add_symbol);
4971 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4972 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4973 relax_switch ();
4975 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
4976 as_bad (_("offset too large"));
4977 macro_build_lui (&offset_expr, tempreg);
4978 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4979 tempreg, tempreg, BFD_RELOC_LO16);
4980 if (mips_relax.sequence)
4981 relax_end ();
4984 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
4986 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4988 /* If this is a reference to an external symbol, and there
4989 is no constant, we want
4990 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4991 or for lca or if tempreg is PIC_CALL_REG
4992 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4993 For a local symbol, we want
4994 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4996 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4998 If we have a small constant, and this is a reference to
4999 an external symbol, we want
5000 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5002 addiu $tempreg,$tempreg,<constant>
5003 For a local symbol, we want the same instruction
5004 sequence, but we output a BFD_RELOC_LO16 reloc on the
5005 addiu instruction.
5007 If we have a large constant, and this is a reference to
5008 an external symbol, we want
5009 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5010 lui $at,<hiconstant>
5011 addiu $at,$at,<loconstant>
5012 addu $tempreg,$tempreg,$at
5013 For a local symbol, we want the same instruction
5014 sequence, but we output a BFD_RELOC_LO16 reloc on the
5015 addiu instruction.
5018 if (offset_expr.X_add_number == 0)
5020 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5021 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5023 relax_start (offset_expr.X_add_symbol);
5024 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5025 lw_reloc_type, mips_gp_register);
5026 if (breg != 0)
5028 /* We're going to put in an addu instruction using
5029 tempreg, so we may as well insert the nop right
5030 now. */
5031 load_delay_nop ();
5033 relax_switch ();
5034 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5035 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
5036 load_delay_nop ();
5037 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5038 tempreg, tempreg, BFD_RELOC_LO16);
5039 relax_end ();
5040 /* FIXME: If breg == 0, and the next instruction uses
5041 $tempreg, then if this variant case is used an extra
5042 nop will be generated. */
5044 else if (offset_expr.X_add_number >= -0x8000
5045 && offset_expr.X_add_number < 0x8000)
5047 load_got_offset (tempreg, &offset_expr);
5048 load_delay_nop ();
5049 add_got_offset (tempreg, &offset_expr);
5051 else
5053 expr1.X_add_number = offset_expr.X_add_number;
5054 offset_expr.X_add_number =
5055 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
5056 load_got_offset (tempreg, &offset_expr);
5057 offset_expr.X_add_number = expr1.X_add_number;
5058 /* If we are going to add in a base register, and the
5059 target register and the base register are the same,
5060 then we are using AT as a temporary register. Since
5061 we want to load the constant into AT, we add our
5062 current AT (from the global offset table) and the
5063 register into the register now, and pretend we were
5064 not using a base register. */
5065 if (breg == treg)
5067 load_delay_nop ();
5068 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5069 treg, AT, breg);
5070 breg = 0;
5071 tempreg = treg;
5073 add_got_offset_hilo (tempreg, &offset_expr, AT);
5074 used_at = 1;
5077 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5079 int add_breg_early = 0;
5081 /* If this is a reference to an external, and there is no
5082 constant, or local symbol (*), with or without a
5083 constant, we want
5084 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5085 or for lca or if tempreg is PIC_CALL_REG
5086 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5088 If we have a small constant, and this is a reference to
5089 an external symbol, we want
5090 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5091 addiu $tempreg,$tempreg,<constant>
5093 If we have a large constant, and this is a reference to
5094 an external symbol, we want
5095 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5096 lui $at,<hiconstant>
5097 addiu $at,$at,<loconstant>
5098 addu $tempreg,$tempreg,$at
5100 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5101 local symbols, even though it introduces an additional
5102 instruction. */
5104 if (offset_expr.X_add_number)
5106 expr1.X_add_number = offset_expr.X_add_number;
5107 offset_expr.X_add_number = 0;
5109 relax_start (offset_expr.X_add_symbol);
5110 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5111 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5113 if (expr1.X_add_number >= -0x8000
5114 && expr1.X_add_number < 0x8000)
5116 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5117 tempreg, tempreg, BFD_RELOC_LO16);
5119 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5121 int dreg;
5123 /* If we are going to add in a base register, and the
5124 target register and the base register are the same,
5125 then we are using AT as a temporary register. Since
5126 we want to load the constant into AT, we add our
5127 current AT (from the global offset table) and the
5128 register into the register now, and pretend we were
5129 not using a base register. */
5130 if (breg != treg)
5131 dreg = tempreg;
5132 else
5134 assert (tempreg == AT);
5135 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5136 treg, AT, breg);
5137 dreg = treg;
5138 add_breg_early = 1;
5141 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5142 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5143 dreg, dreg, AT);
5145 used_at = 1;
5147 else
5148 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5150 relax_switch ();
5151 offset_expr.X_add_number = expr1.X_add_number;
5153 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5154 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5155 if (add_breg_early)
5157 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5158 treg, tempreg, breg);
5159 breg = 0;
5160 tempreg = treg;
5162 relax_end ();
5164 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5166 relax_start (offset_expr.X_add_symbol);
5167 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5168 BFD_RELOC_MIPS_CALL16, mips_gp_register);
5169 relax_switch ();
5170 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5171 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5172 relax_end ();
5174 else
5176 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5177 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5180 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5182 int gpdelay;
5183 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5184 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5185 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5187 /* This is the large GOT case. If this is a reference to an
5188 external symbol, and there is no constant, we want
5189 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5190 addu $tempreg,$tempreg,$gp
5191 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5192 or for lca or if tempreg is PIC_CALL_REG
5193 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5194 addu $tempreg,$tempreg,$gp
5195 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5196 For a local symbol, we want
5197 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5199 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5201 If we have a small constant, and this is a reference to
5202 an external symbol, we want
5203 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5204 addu $tempreg,$tempreg,$gp
5205 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5207 addiu $tempreg,$tempreg,<constant>
5208 For a local symbol, we want
5209 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5211 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5213 If we have a large constant, and this is a reference to
5214 an external symbol, we want
5215 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5216 addu $tempreg,$tempreg,$gp
5217 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5218 lui $at,<hiconstant>
5219 addiu $at,$at,<loconstant>
5220 addu $tempreg,$tempreg,$at
5221 For a local symbol, we want
5222 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5223 lui $at,<hiconstant>
5224 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5225 addu $tempreg,$tempreg,$at
5228 expr1.X_add_number = offset_expr.X_add_number;
5229 offset_expr.X_add_number = 0;
5230 relax_start (offset_expr.X_add_symbol);
5231 gpdelay = reg_needs_delay (mips_gp_register);
5232 if (expr1.X_add_number == 0 && breg == 0
5233 && (call || tempreg == PIC_CALL_REG))
5235 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5236 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5238 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5239 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5240 tempreg, tempreg, mips_gp_register);
5241 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5242 tempreg, lw_reloc_type, tempreg);
5243 if (expr1.X_add_number == 0)
5245 if (breg != 0)
5247 /* We're going to put in an addu instruction using
5248 tempreg, so we may as well insert the nop right
5249 now. */
5250 load_delay_nop ();
5253 else if (expr1.X_add_number >= -0x8000
5254 && expr1.X_add_number < 0x8000)
5256 load_delay_nop ();
5257 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5258 tempreg, tempreg, BFD_RELOC_LO16);
5260 else
5262 int dreg;
5264 /* If we are going to add in a base register, and the
5265 target register and the base register are the same,
5266 then we are using AT as a temporary register. Since
5267 we want to load the constant into AT, we add our
5268 current AT (from the global offset table) and the
5269 register into the register now, and pretend we were
5270 not using a base register. */
5271 if (breg != treg)
5272 dreg = tempreg;
5273 else
5275 assert (tempreg == AT);
5276 load_delay_nop ();
5277 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5278 treg, AT, breg);
5279 dreg = treg;
5282 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5283 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5285 used_at = 1;
5287 offset_expr.X_add_number =
5288 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5289 relax_switch ();
5291 if (gpdelay)
5293 /* This is needed because this instruction uses $gp, but
5294 the first instruction on the main stream does not. */
5295 macro_build (NULL, "nop", "");
5298 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5299 local_reloc_type, mips_gp_register);
5300 if (expr1.X_add_number >= -0x8000
5301 && expr1.X_add_number < 0x8000)
5303 load_delay_nop ();
5304 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5305 tempreg, tempreg, BFD_RELOC_LO16);
5306 /* FIXME: If add_number is 0, and there was no base
5307 register, the external symbol case ended with a load,
5308 so if the symbol turns out to not be external, and
5309 the next instruction uses tempreg, an unnecessary nop
5310 will be inserted. */
5312 else
5314 if (breg == treg)
5316 /* We must add in the base register now, as in the
5317 external symbol case. */
5318 assert (tempreg == AT);
5319 load_delay_nop ();
5320 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5321 treg, AT, breg);
5322 tempreg = treg;
5323 /* We set breg to 0 because we have arranged to add
5324 it in in both cases. */
5325 breg = 0;
5328 macro_build_lui (&expr1, AT);
5329 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5330 AT, AT, BFD_RELOC_LO16);
5331 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5332 tempreg, tempreg, AT);
5333 used_at = 1;
5335 relax_end ();
5337 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5339 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5340 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5341 int add_breg_early = 0;
5343 /* This is the large GOT case. If this is a reference to an
5344 external symbol, and there is no constant, we want
5345 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5346 add $tempreg,$tempreg,$gp
5347 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5348 or for lca or if tempreg is PIC_CALL_REG
5349 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5350 add $tempreg,$tempreg,$gp
5351 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5353 If we have a small constant, and this is a reference to
5354 an external symbol, we want
5355 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5356 add $tempreg,$tempreg,$gp
5357 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5358 addi $tempreg,$tempreg,<constant>
5360 If we have a large constant, and this is a reference to
5361 an external symbol, we want
5362 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5363 addu $tempreg,$tempreg,$gp
5364 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5365 lui $at,<hiconstant>
5366 addi $at,$at,<loconstant>
5367 add $tempreg,$tempreg,$at
5369 If we have NewABI, and we know it's a local symbol, we want
5370 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5371 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5372 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5374 relax_start (offset_expr.X_add_symbol);
5376 expr1.X_add_number = offset_expr.X_add_number;
5377 offset_expr.X_add_number = 0;
5379 if (expr1.X_add_number == 0 && breg == 0
5380 && (call || tempreg == PIC_CALL_REG))
5382 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5383 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5385 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5386 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5387 tempreg, tempreg, mips_gp_register);
5388 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5389 tempreg, lw_reloc_type, tempreg);
5391 if (expr1.X_add_number == 0)
5393 else if (expr1.X_add_number >= -0x8000
5394 && expr1.X_add_number < 0x8000)
5396 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5397 tempreg, tempreg, BFD_RELOC_LO16);
5399 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5401 int dreg;
5403 /* If we are going to add in a base register, and the
5404 target register and the base register are the same,
5405 then we are using AT as a temporary register. Since
5406 we want to load the constant into AT, we add our
5407 current AT (from the global offset table) and the
5408 register into the register now, and pretend we were
5409 not using a base register. */
5410 if (breg != treg)
5411 dreg = tempreg;
5412 else
5414 assert (tempreg == AT);
5415 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5416 treg, AT, breg);
5417 dreg = treg;
5418 add_breg_early = 1;
5421 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5422 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5424 used_at = 1;
5426 else
5427 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5429 relax_switch ();
5430 offset_expr.X_add_number = expr1.X_add_number;
5431 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5432 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5433 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5434 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5435 if (add_breg_early)
5437 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5438 treg, tempreg, breg);
5439 breg = 0;
5440 tempreg = treg;
5442 relax_end ();
5444 else
5445 abort ();
5447 if (breg != 0)
5448 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
5449 break;
5451 case M_J_A:
5452 /* The j instruction may not be used in PIC code, since it
5453 requires an absolute address. We convert it to a b
5454 instruction. */
5455 if (mips_pic == NO_PIC)
5456 macro_build (&offset_expr, "j", "a");
5457 else
5458 macro_build (&offset_expr, "b", "p");
5459 break;
5461 /* The jal instructions must be handled as macros because when
5462 generating PIC code they expand to multi-instruction
5463 sequences. Normally they are simple instructions. */
5464 case M_JAL_1:
5465 dreg = RA;
5466 /* Fall through. */
5467 case M_JAL_2:
5468 if (mips_pic == NO_PIC)
5469 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5470 else if (mips_pic == SVR4_PIC)
5472 if (sreg != PIC_CALL_REG)
5473 as_warn (_("MIPS PIC call to register other than $25"));
5475 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5476 if (! HAVE_NEWABI)
5478 if (mips_cprestore_offset < 0)
5479 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5480 else
5482 if (! mips_frame_reg_valid)
5484 as_warn (_("No .frame pseudo-op used in PIC code"));
5485 /* Quiet this warning. */
5486 mips_frame_reg_valid = 1;
5488 if (! mips_cprestore_valid)
5490 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5491 /* Quiet this warning. */
5492 mips_cprestore_valid = 1;
5494 expr1.X_add_number = mips_cprestore_offset;
5495 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5496 mips_gp_register,
5497 mips_frame_reg,
5498 HAVE_64BIT_ADDRESSES);
5502 else
5503 abort ();
5505 break;
5507 case M_JAL_A:
5508 if (mips_pic == NO_PIC)
5509 macro_build (&offset_expr, "jal", "a");
5510 else if (mips_pic == SVR4_PIC)
5512 /* If this is a reference to an external symbol, and we are
5513 using a small GOT, we want
5514 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5516 jalr $ra,$25
5518 lw $gp,cprestore($sp)
5519 The cprestore value is set using the .cprestore
5520 pseudo-op. If we are using a big GOT, we want
5521 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5522 addu $25,$25,$gp
5523 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5525 jalr $ra,$25
5527 lw $gp,cprestore($sp)
5528 If the symbol is not external, we want
5529 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5531 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5532 jalr $ra,$25
5534 lw $gp,cprestore($sp)
5536 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5537 sequences above, minus nops, unless the symbol is local,
5538 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5539 GOT_DISP. */
5540 if (HAVE_NEWABI)
5542 if (! mips_big_got)
5544 relax_start (offset_expr.X_add_symbol);
5545 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5546 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5547 mips_gp_register);
5548 relax_switch ();
5549 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5550 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
5551 mips_gp_register);
5552 relax_end ();
5554 else
5556 relax_start (offset_expr.X_add_symbol);
5557 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5558 BFD_RELOC_MIPS_CALL_HI16);
5559 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5560 PIC_CALL_REG, mips_gp_register);
5561 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5562 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5563 PIC_CALL_REG);
5564 relax_switch ();
5565 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5566 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5567 mips_gp_register);
5568 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5569 PIC_CALL_REG, PIC_CALL_REG,
5570 BFD_RELOC_MIPS_GOT_OFST);
5571 relax_end ();
5574 macro_build_jalr (&offset_expr);
5576 else
5578 relax_start (offset_expr.X_add_symbol);
5579 if (! mips_big_got)
5581 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5582 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5583 mips_gp_register);
5584 load_delay_nop ();
5585 relax_switch ();
5587 else
5589 int gpdelay;
5591 gpdelay = reg_needs_delay (mips_gp_register);
5592 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5593 BFD_RELOC_MIPS_CALL_HI16);
5594 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5595 PIC_CALL_REG, mips_gp_register);
5596 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5597 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5598 PIC_CALL_REG);
5599 load_delay_nop ();
5600 relax_switch ();
5601 if (gpdelay)
5602 macro_build (NULL, "nop", "");
5604 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5605 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5606 mips_gp_register);
5607 load_delay_nop ();
5608 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5609 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
5610 relax_end ();
5611 macro_build_jalr (&offset_expr);
5613 if (mips_cprestore_offset < 0)
5614 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5615 else
5617 if (! mips_frame_reg_valid)
5619 as_warn (_("No .frame pseudo-op used in PIC code"));
5620 /* Quiet this warning. */
5621 mips_frame_reg_valid = 1;
5623 if (! mips_cprestore_valid)
5625 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5626 /* Quiet this warning. */
5627 mips_cprestore_valid = 1;
5629 if (mips_opts.noreorder)
5630 macro_build (NULL, "nop", "");
5631 expr1.X_add_number = mips_cprestore_offset;
5632 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5633 mips_gp_register,
5634 mips_frame_reg,
5635 HAVE_64BIT_ADDRESSES);
5639 else
5640 abort ();
5642 break;
5644 case M_LB_AB:
5645 s = "lb";
5646 goto ld;
5647 case M_LBU_AB:
5648 s = "lbu";
5649 goto ld;
5650 case M_LH_AB:
5651 s = "lh";
5652 goto ld;
5653 case M_LHU_AB:
5654 s = "lhu";
5655 goto ld;
5656 case M_LW_AB:
5657 s = "lw";
5658 goto ld;
5659 case M_LWC0_AB:
5660 s = "lwc0";
5661 /* Itbl support may require additional care here. */
5662 coproc = 1;
5663 goto ld;
5664 case M_LWC1_AB:
5665 s = "lwc1";
5666 /* Itbl support may require additional care here. */
5667 coproc = 1;
5668 goto ld;
5669 case M_LWC2_AB:
5670 s = "lwc2";
5671 /* Itbl support may require additional care here. */
5672 coproc = 1;
5673 goto ld;
5674 case M_LWC3_AB:
5675 s = "lwc3";
5676 /* Itbl support may require additional care here. */
5677 coproc = 1;
5678 goto ld;
5679 case M_LWL_AB:
5680 s = "lwl";
5681 lr = 1;
5682 goto ld;
5683 case M_LWR_AB:
5684 s = "lwr";
5685 lr = 1;
5686 goto ld;
5687 case M_LDC1_AB:
5688 if (mips_opts.arch == CPU_R4650)
5690 as_bad (_("opcode not supported on this processor"));
5691 break;
5693 s = "ldc1";
5694 /* Itbl support may require additional care here. */
5695 coproc = 1;
5696 goto ld;
5697 case M_LDC2_AB:
5698 s = "ldc2";
5699 /* Itbl support may require additional care here. */
5700 coproc = 1;
5701 goto ld;
5702 case M_LDC3_AB:
5703 s = "ldc3";
5704 /* Itbl support may require additional care here. */
5705 coproc = 1;
5706 goto ld;
5707 case M_LDL_AB:
5708 s = "ldl";
5709 lr = 1;
5710 goto ld;
5711 case M_LDR_AB:
5712 s = "ldr";
5713 lr = 1;
5714 goto ld;
5715 case M_LL_AB:
5716 s = "ll";
5717 goto ld;
5718 case M_LLD_AB:
5719 s = "lld";
5720 goto ld;
5721 case M_LWU_AB:
5722 s = "lwu";
5724 if (breg == treg || coproc || lr)
5726 tempreg = AT;
5727 used_at = 1;
5729 else
5731 tempreg = treg;
5733 goto ld_st;
5734 case M_SB_AB:
5735 s = "sb";
5736 goto st;
5737 case M_SH_AB:
5738 s = "sh";
5739 goto st;
5740 case M_SW_AB:
5741 s = "sw";
5742 goto st;
5743 case M_SWC0_AB:
5744 s = "swc0";
5745 /* Itbl support may require additional care here. */
5746 coproc = 1;
5747 goto st;
5748 case M_SWC1_AB:
5749 s = "swc1";
5750 /* Itbl support may require additional care here. */
5751 coproc = 1;
5752 goto st;
5753 case M_SWC2_AB:
5754 s = "swc2";
5755 /* Itbl support may require additional care here. */
5756 coproc = 1;
5757 goto st;
5758 case M_SWC3_AB:
5759 s = "swc3";
5760 /* Itbl support may require additional care here. */
5761 coproc = 1;
5762 goto st;
5763 case M_SWL_AB:
5764 s = "swl";
5765 goto st;
5766 case M_SWR_AB:
5767 s = "swr";
5768 goto st;
5769 case M_SC_AB:
5770 s = "sc";
5771 goto st;
5772 case M_SCD_AB:
5773 s = "scd";
5774 goto st;
5775 case M_SDC1_AB:
5776 if (mips_opts.arch == CPU_R4650)
5778 as_bad (_("opcode not supported on this processor"));
5779 break;
5781 s = "sdc1";
5782 coproc = 1;
5783 /* Itbl support may require additional care here. */
5784 goto st;
5785 case M_SDC2_AB:
5786 s = "sdc2";
5787 /* Itbl support may require additional care here. */
5788 coproc = 1;
5789 goto st;
5790 case M_SDC3_AB:
5791 s = "sdc3";
5792 /* Itbl support may require additional care here. */
5793 coproc = 1;
5794 goto st;
5795 case M_SDL_AB:
5796 s = "sdl";
5797 goto st;
5798 case M_SDR_AB:
5799 s = "sdr";
5801 tempreg = AT;
5802 used_at = 1;
5803 ld_st:
5804 /* Itbl support may require additional care here. */
5805 if (mask == M_LWC1_AB
5806 || mask == M_SWC1_AB
5807 || mask == M_LDC1_AB
5808 || mask == M_SDC1_AB
5809 || mask == M_L_DAB
5810 || mask == M_S_DAB)
5811 fmt = "T,o(b)";
5812 else if (coproc)
5813 fmt = "E,o(b)";
5814 else
5815 fmt = "t,o(b)";
5817 if (offset_expr.X_op != O_constant
5818 && offset_expr.X_op != O_symbol)
5820 as_bad (_("expression too complex"));
5821 offset_expr.X_op = O_constant;
5824 if (HAVE_32BIT_ADDRESSES
5825 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5827 char value [32];
5829 sprintf_vma (value, offset_expr.X_add_number);
5830 as_bad (_("Number (0x%s) larger than 32 bits"), value);
5833 /* A constant expression in PIC code can be handled just as it
5834 is in non PIC code. */
5835 if (offset_expr.X_op == O_constant)
5837 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
5838 & ~(bfd_vma) 0xffff);
5839 normalize_address_expr (&expr1);
5840 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
5841 if (breg != 0)
5842 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5843 tempreg, tempreg, breg);
5844 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5846 else if (mips_pic == NO_PIC)
5848 /* If this is a reference to a GP relative symbol, and there
5849 is no base register, we want
5850 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5851 Otherwise, if there is no base register, we want
5852 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5853 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5854 If we have a constant, we need two instructions anyhow,
5855 so we always use the latter form.
5857 If we have a base register, and this is a reference to a
5858 GP relative symbol, we want
5859 addu $tempreg,$breg,$gp
5860 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5861 Otherwise we want
5862 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5863 addu $tempreg,$tempreg,$breg
5864 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5865 With a constant we always use the latter case.
5867 With 64bit address space and no base register and $at usable,
5868 we want
5869 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5870 lui $at,<sym> (BFD_RELOC_HI16_S)
5871 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5872 dsll32 $tempreg,0
5873 daddu $tempreg,$at
5874 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5875 If we have a base register, we want
5876 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5877 lui $at,<sym> (BFD_RELOC_HI16_S)
5878 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5879 daddu $at,$breg
5880 dsll32 $tempreg,0
5881 daddu $tempreg,$at
5882 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5884 Without $at we can't generate the optimal path for superscalar
5885 processors here since this would require two temporary registers.
5886 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5887 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5888 dsll $tempreg,16
5889 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5890 dsll $tempreg,16
5891 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5892 If we have a base register, we want
5893 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5894 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5895 dsll $tempreg,16
5896 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5897 dsll $tempreg,16
5898 daddu $tempreg,$tempreg,$breg
5899 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5901 For GP relative symbols in 64bit address space we can use
5902 the same sequence as in 32bit address space. */
5903 if (HAVE_64BIT_SYMBOLS)
5905 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5906 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5908 relax_start (offset_expr.X_add_symbol);
5909 if (breg == 0)
5911 macro_build (&offset_expr, s, fmt, treg,
5912 BFD_RELOC_GPREL16, mips_gp_register);
5914 else
5916 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5917 tempreg, breg, mips_gp_register);
5918 macro_build (&offset_expr, s, fmt, treg,
5919 BFD_RELOC_GPREL16, tempreg);
5921 relax_switch ();
5924 if (used_at == 0 && !mips_opts.noat)
5926 macro_build (&offset_expr, "lui", "t,u", tempreg,
5927 BFD_RELOC_MIPS_HIGHEST);
5928 macro_build (&offset_expr, "lui", "t,u", AT,
5929 BFD_RELOC_HI16_S);
5930 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5931 tempreg, BFD_RELOC_MIPS_HIGHER);
5932 if (breg != 0)
5933 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
5934 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5935 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5936 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
5937 tempreg);
5938 used_at = 1;
5940 else
5942 macro_build (&offset_expr, "lui", "t,u", tempreg,
5943 BFD_RELOC_MIPS_HIGHEST);
5944 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5945 tempreg, BFD_RELOC_MIPS_HIGHER);
5946 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5947 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5948 tempreg, BFD_RELOC_HI16_S);
5949 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5950 if (breg != 0)
5951 macro_build (NULL, "daddu", "d,v,t",
5952 tempreg, tempreg, breg);
5953 macro_build (&offset_expr, s, fmt, treg,
5954 BFD_RELOC_LO16, tempreg);
5957 if (mips_relax.sequence)
5958 relax_end ();
5959 break;
5962 if (breg == 0)
5964 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5965 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5967 relax_start (offset_expr.X_add_symbol);
5968 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
5969 mips_gp_register);
5970 relax_switch ();
5972 macro_build_lui (&offset_expr, tempreg);
5973 macro_build (&offset_expr, s, fmt, treg,
5974 BFD_RELOC_LO16, tempreg);
5975 if (mips_relax.sequence)
5976 relax_end ();
5978 else
5980 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5981 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5983 relax_start (offset_expr.X_add_symbol);
5984 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5985 tempreg, breg, mips_gp_register);
5986 macro_build (&offset_expr, s, fmt, treg,
5987 BFD_RELOC_GPREL16, tempreg);
5988 relax_switch ();
5990 macro_build_lui (&offset_expr, tempreg);
5991 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5992 tempreg, tempreg, breg);
5993 macro_build (&offset_expr, s, fmt, treg,
5994 BFD_RELOC_LO16, tempreg);
5995 if (mips_relax.sequence)
5996 relax_end ();
5999 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6001 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6003 /* If this is a reference to an external symbol, we want
6004 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6006 <op> $treg,0($tempreg)
6007 Otherwise we want
6008 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6010 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6011 <op> $treg,0($tempreg)
6013 For NewABI, we want
6014 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6015 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6017 If there is a base register, we add it to $tempreg before
6018 the <op>. If there is a constant, we stick it in the
6019 <op> instruction. We don't handle constants larger than
6020 16 bits, because we have no way to load the upper 16 bits
6021 (actually, we could handle them for the subset of cases
6022 in which we are not using $at). */
6023 assert (offset_expr.X_op == O_symbol);
6024 if (HAVE_NEWABI)
6026 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6027 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6028 if (breg != 0)
6029 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6030 tempreg, tempreg, breg);
6031 macro_build (&offset_expr, s, fmt, treg,
6032 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6033 break;
6035 expr1.X_add_number = offset_expr.X_add_number;
6036 offset_expr.X_add_number = 0;
6037 if (expr1.X_add_number < -0x8000
6038 || expr1.X_add_number >= 0x8000)
6039 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6040 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6041 lw_reloc_type, mips_gp_register);
6042 load_delay_nop ();
6043 relax_start (offset_expr.X_add_symbol);
6044 relax_switch ();
6045 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6046 tempreg, BFD_RELOC_LO16);
6047 relax_end ();
6048 if (breg != 0)
6049 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6050 tempreg, tempreg, breg);
6051 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6053 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6055 int gpdelay;
6057 /* If this is a reference to an external symbol, we want
6058 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6059 addu $tempreg,$tempreg,$gp
6060 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6061 <op> $treg,0($tempreg)
6062 Otherwise we want
6063 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6065 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6066 <op> $treg,0($tempreg)
6067 If there is a base register, we add it to $tempreg before
6068 the <op>. If there is a constant, we stick it in the
6069 <op> instruction. We don't handle constants larger than
6070 16 bits, because we have no way to load the upper 16 bits
6071 (actually, we could handle them for the subset of cases
6072 in which we are not using $at). */
6073 assert (offset_expr.X_op == O_symbol);
6074 expr1.X_add_number = offset_expr.X_add_number;
6075 offset_expr.X_add_number = 0;
6076 if (expr1.X_add_number < -0x8000
6077 || expr1.X_add_number >= 0x8000)
6078 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6079 gpdelay = reg_needs_delay (mips_gp_register);
6080 relax_start (offset_expr.X_add_symbol);
6081 macro_build (&offset_expr, "lui", "t,u", tempreg,
6082 BFD_RELOC_MIPS_GOT_HI16);
6083 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6084 mips_gp_register);
6085 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6086 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6087 relax_switch ();
6088 if (gpdelay)
6089 macro_build (NULL, "nop", "");
6090 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6091 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6092 load_delay_nop ();
6093 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6094 tempreg, BFD_RELOC_LO16);
6095 relax_end ();
6097 if (breg != 0)
6098 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6099 tempreg, tempreg, breg);
6100 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6102 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6104 /* If this is a reference to an external symbol, we want
6105 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6106 add $tempreg,$tempreg,$gp
6107 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6108 <op> $treg,<ofst>($tempreg)
6109 Otherwise, for local symbols, we want:
6110 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6111 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6112 assert (offset_expr.X_op == O_symbol);
6113 expr1.X_add_number = offset_expr.X_add_number;
6114 offset_expr.X_add_number = 0;
6115 if (expr1.X_add_number < -0x8000
6116 || expr1.X_add_number >= 0x8000)
6117 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6118 relax_start (offset_expr.X_add_symbol);
6119 macro_build (&offset_expr, "lui", "t,u", tempreg,
6120 BFD_RELOC_MIPS_GOT_HI16);
6121 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6122 mips_gp_register);
6123 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6124 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6125 if (breg != 0)
6126 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6127 tempreg, tempreg, breg);
6128 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6130 relax_switch ();
6131 offset_expr.X_add_number = expr1.X_add_number;
6132 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6133 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6134 if (breg != 0)
6135 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6136 tempreg, tempreg, breg);
6137 macro_build (&offset_expr, s, fmt, treg,
6138 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6139 relax_end ();
6141 else
6142 abort ();
6144 break;
6146 case M_LI:
6147 case M_LI_S:
6148 load_register (treg, &imm_expr, 0);
6149 break;
6151 case M_DLI:
6152 load_register (treg, &imm_expr, 1);
6153 break;
6155 case M_LI_SS:
6156 if (imm_expr.X_op == O_constant)
6158 used_at = 1;
6159 load_register (AT, &imm_expr, 0);
6160 macro_build (NULL, "mtc1", "t,G", AT, treg);
6161 break;
6163 else
6165 assert (offset_expr.X_op == O_symbol
6166 && strcmp (segment_name (S_GET_SEGMENT
6167 (offset_expr.X_add_symbol)),
6168 ".lit4") == 0
6169 && offset_expr.X_add_number == 0);
6170 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
6171 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6172 break;
6175 case M_LI_D:
6176 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6177 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6178 order 32 bits of the value and the low order 32 bits are either
6179 zero or in OFFSET_EXPR. */
6180 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6182 if (HAVE_64BIT_GPRS)
6183 load_register (treg, &imm_expr, 1);
6184 else
6186 int hreg, lreg;
6188 if (target_big_endian)
6190 hreg = treg;
6191 lreg = treg + 1;
6193 else
6195 hreg = treg + 1;
6196 lreg = treg;
6199 if (hreg <= 31)
6200 load_register (hreg, &imm_expr, 0);
6201 if (lreg <= 31)
6203 if (offset_expr.X_op == O_absent)
6204 move_register (lreg, 0);
6205 else
6207 assert (offset_expr.X_op == O_constant);
6208 load_register (lreg, &offset_expr, 0);
6212 break;
6215 /* We know that sym is in the .rdata section. First we get the
6216 upper 16 bits of the address. */
6217 if (mips_pic == NO_PIC)
6219 macro_build_lui (&offset_expr, AT);
6220 used_at = 1;
6222 else if (mips_pic == SVR4_PIC)
6224 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6225 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6226 used_at = 1;
6228 else
6229 abort ();
6231 /* Now we load the register(s). */
6232 if (HAVE_64BIT_GPRS)
6234 used_at = 1;
6235 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6237 else
6239 used_at = 1;
6240 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6241 if (treg != RA)
6243 /* FIXME: How in the world do we deal with the possible
6244 overflow here? */
6245 offset_expr.X_add_number += 4;
6246 macro_build (&offset_expr, "lw", "t,o(b)",
6247 treg + 1, BFD_RELOC_LO16, AT);
6250 break;
6252 case M_LI_DD:
6253 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6254 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6255 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6256 the value and the low order 32 bits are either zero or in
6257 OFFSET_EXPR. */
6258 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6260 used_at = 1;
6261 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
6262 if (HAVE_64BIT_FPRS)
6264 assert (HAVE_64BIT_GPRS);
6265 macro_build (NULL, "dmtc1", "t,S", AT, treg);
6267 else
6269 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
6270 if (offset_expr.X_op == O_absent)
6271 macro_build (NULL, "mtc1", "t,G", 0, treg);
6272 else
6274 assert (offset_expr.X_op == O_constant);
6275 load_register (AT, &offset_expr, 0);
6276 macro_build (NULL, "mtc1", "t,G", AT, treg);
6279 break;
6282 assert (offset_expr.X_op == O_symbol
6283 && offset_expr.X_add_number == 0);
6284 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6285 if (strcmp (s, ".lit8") == 0)
6287 if (mips_opts.isa != ISA_MIPS1)
6289 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
6290 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6291 break;
6293 breg = mips_gp_register;
6294 r = BFD_RELOC_MIPS_LITERAL;
6295 goto dob;
6297 else
6299 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6300 used_at = 1;
6301 if (mips_pic == SVR4_PIC)
6302 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6303 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6304 else
6306 /* FIXME: This won't work for a 64 bit address. */
6307 macro_build_lui (&offset_expr, AT);
6310 if (mips_opts.isa != ISA_MIPS1)
6312 macro_build (&offset_expr, "ldc1", "T,o(b)",
6313 treg, BFD_RELOC_LO16, AT);
6314 break;
6316 breg = AT;
6317 r = BFD_RELOC_LO16;
6318 goto dob;
6321 case M_L_DOB:
6322 if (mips_opts.arch == CPU_R4650)
6324 as_bad (_("opcode not supported on this processor"));
6325 break;
6327 /* Even on a big endian machine $fn comes before $fn+1. We have
6328 to adjust when loading from memory. */
6329 r = BFD_RELOC_LO16;
6330 dob:
6331 assert (mips_opts.isa == ISA_MIPS1);
6332 macro_build (&offset_expr, "lwc1", "T,o(b)",
6333 target_big_endian ? treg + 1 : treg, r, breg);
6334 /* FIXME: A possible overflow which I don't know how to deal
6335 with. */
6336 offset_expr.X_add_number += 4;
6337 macro_build (&offset_expr, "lwc1", "T,o(b)",
6338 target_big_endian ? treg : treg + 1, r, breg);
6339 break;
6341 case M_L_DAB:
6343 * The MIPS assembler seems to check for X_add_number not
6344 * being double aligned and generating:
6345 * lui at,%hi(foo+1)
6346 * addu at,at,v1
6347 * addiu at,at,%lo(foo+1)
6348 * lwc1 f2,0(at)
6349 * lwc1 f3,4(at)
6350 * But, the resulting address is the same after relocation so why
6351 * generate the extra instruction?
6353 if (mips_opts.arch == CPU_R4650)
6355 as_bad (_("opcode not supported on this processor"));
6356 break;
6358 /* Itbl support may require additional care here. */
6359 coproc = 1;
6360 if (mips_opts.isa != ISA_MIPS1)
6362 s = "ldc1";
6363 goto ld;
6366 s = "lwc1";
6367 fmt = "T,o(b)";
6368 goto ldd_std;
6370 case M_S_DAB:
6371 if (mips_opts.arch == CPU_R4650)
6373 as_bad (_("opcode not supported on this processor"));
6374 break;
6377 if (mips_opts.isa != ISA_MIPS1)
6379 s = "sdc1";
6380 goto st;
6383 s = "swc1";
6384 fmt = "T,o(b)";
6385 /* Itbl support may require additional care here. */
6386 coproc = 1;
6387 goto ldd_std;
6389 case M_LD_AB:
6390 if (HAVE_64BIT_GPRS)
6392 s = "ld";
6393 goto ld;
6396 s = "lw";
6397 fmt = "t,o(b)";
6398 goto ldd_std;
6400 case M_SD_AB:
6401 if (HAVE_64BIT_GPRS)
6403 s = "sd";
6404 goto st;
6407 s = "sw";
6408 fmt = "t,o(b)";
6410 ldd_std:
6411 if (offset_expr.X_op != O_symbol
6412 && offset_expr.X_op != O_constant)
6414 as_bad (_("expression too complex"));
6415 offset_expr.X_op = O_constant;
6418 if (HAVE_32BIT_ADDRESSES
6419 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6421 char value [32];
6423 sprintf_vma (value, offset_expr.X_add_number);
6424 as_bad (_("Number (0x%s) larger than 32 bits"), value);
6427 /* Even on a big endian machine $fn comes before $fn+1. We have
6428 to adjust when loading from memory. We set coproc if we must
6429 load $fn+1 first. */
6430 /* Itbl support may require additional care here. */
6431 if (! target_big_endian)
6432 coproc = 0;
6434 if (mips_pic == NO_PIC
6435 || offset_expr.X_op == O_constant)
6437 /* If this is a reference to a GP relative symbol, we want
6438 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6439 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6440 If we have a base register, we use this
6441 addu $at,$breg,$gp
6442 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6443 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6444 If this is not a GP relative symbol, we want
6445 lui $at,<sym> (BFD_RELOC_HI16_S)
6446 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6447 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6448 If there is a base register, we add it to $at after the
6449 lui instruction. If there is a constant, we always use
6450 the last case. */
6451 if (offset_expr.X_op == O_symbol
6452 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6453 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6455 relax_start (offset_expr.X_add_symbol);
6456 if (breg == 0)
6458 tempreg = mips_gp_register;
6460 else
6462 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6463 AT, breg, mips_gp_register);
6464 tempreg = AT;
6465 used_at = 1;
6468 /* Itbl support may require additional care here. */
6469 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6470 BFD_RELOC_GPREL16, tempreg);
6471 offset_expr.X_add_number += 4;
6473 /* Set mips_optimize to 2 to avoid inserting an
6474 undesired nop. */
6475 hold_mips_optimize = mips_optimize;
6476 mips_optimize = 2;
6477 /* Itbl support may require additional care here. */
6478 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6479 BFD_RELOC_GPREL16, tempreg);
6480 mips_optimize = hold_mips_optimize;
6482 relax_switch ();
6484 /* We just generated two relocs. When tc_gen_reloc
6485 handles this case, it will skip the first reloc and
6486 handle the second. The second reloc already has an
6487 extra addend of 4, which we added above. We must
6488 subtract it out, and then subtract another 4 to make
6489 the first reloc come out right. The second reloc
6490 will come out right because we are going to add 4 to
6491 offset_expr when we build its instruction below.
6493 If we have a symbol, then we don't want to include
6494 the offset, because it will wind up being included
6495 when we generate the reloc. */
6497 if (offset_expr.X_op == O_constant)
6498 offset_expr.X_add_number -= 8;
6499 else
6501 offset_expr.X_add_number = -4;
6502 offset_expr.X_op = O_constant;
6505 used_at = 1;
6506 macro_build_lui (&offset_expr, AT);
6507 if (breg != 0)
6508 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6509 /* Itbl support may require additional care here. */
6510 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6511 BFD_RELOC_LO16, AT);
6512 /* FIXME: How do we handle overflow here? */
6513 offset_expr.X_add_number += 4;
6514 /* Itbl support may require additional care here. */
6515 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6516 BFD_RELOC_LO16, AT);
6517 if (mips_relax.sequence)
6518 relax_end ();
6520 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6522 /* If this is a reference to an external symbol, we want
6523 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6525 <op> $treg,0($at)
6526 <op> $treg+1,4($at)
6527 Otherwise we want
6528 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6530 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6531 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6532 If there is a base register we add it to $at before the
6533 lwc1 instructions. If there is a constant we include it
6534 in the lwc1 instructions. */
6535 used_at = 1;
6536 expr1.X_add_number = offset_expr.X_add_number;
6537 if (expr1.X_add_number < -0x8000
6538 || expr1.X_add_number >= 0x8000 - 4)
6539 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6540 load_got_offset (AT, &offset_expr);
6541 load_delay_nop ();
6542 if (breg != 0)
6543 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6545 /* Set mips_optimize to 2 to avoid inserting an undesired
6546 nop. */
6547 hold_mips_optimize = mips_optimize;
6548 mips_optimize = 2;
6550 /* Itbl support may require additional care here. */
6551 relax_start (offset_expr.X_add_symbol);
6552 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6553 BFD_RELOC_LO16, AT);
6554 expr1.X_add_number += 4;
6555 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6556 BFD_RELOC_LO16, AT);
6557 relax_switch ();
6558 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6559 BFD_RELOC_LO16, AT);
6560 offset_expr.X_add_number += 4;
6561 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6562 BFD_RELOC_LO16, AT);
6563 relax_end ();
6565 mips_optimize = hold_mips_optimize;
6567 else if (mips_pic == SVR4_PIC)
6569 int gpdelay;
6571 /* If this is a reference to an external symbol, we want
6572 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6573 addu $at,$at,$gp
6574 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6576 <op> $treg,0($at)
6577 <op> $treg+1,4($at)
6578 Otherwise we want
6579 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6581 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6582 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6583 If there is a base register we add it to $at before the
6584 lwc1 instructions. If there is a constant we include it
6585 in the lwc1 instructions. */
6586 used_at = 1;
6587 expr1.X_add_number = offset_expr.X_add_number;
6588 offset_expr.X_add_number = 0;
6589 if (expr1.X_add_number < -0x8000
6590 || expr1.X_add_number >= 0x8000 - 4)
6591 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6592 gpdelay = reg_needs_delay (mips_gp_register);
6593 relax_start (offset_expr.X_add_symbol);
6594 macro_build (&offset_expr, "lui", "t,u",
6595 AT, BFD_RELOC_MIPS_GOT_HI16);
6596 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6597 AT, AT, mips_gp_register);
6598 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6599 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6600 load_delay_nop ();
6601 if (breg != 0)
6602 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6603 /* Itbl support may require additional care here. */
6604 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6605 BFD_RELOC_LO16, AT);
6606 expr1.X_add_number += 4;
6608 /* Set mips_optimize to 2 to avoid inserting an undesired
6609 nop. */
6610 hold_mips_optimize = mips_optimize;
6611 mips_optimize = 2;
6612 /* Itbl support may require additional care here. */
6613 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6614 BFD_RELOC_LO16, AT);
6615 mips_optimize = hold_mips_optimize;
6616 expr1.X_add_number -= 4;
6618 relax_switch ();
6619 offset_expr.X_add_number = expr1.X_add_number;
6620 if (gpdelay)
6621 macro_build (NULL, "nop", "");
6622 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6623 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6624 load_delay_nop ();
6625 if (breg != 0)
6626 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6627 /* Itbl support may require additional care here. */
6628 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6629 BFD_RELOC_LO16, AT);
6630 offset_expr.X_add_number += 4;
6632 /* Set mips_optimize to 2 to avoid inserting an undesired
6633 nop. */
6634 hold_mips_optimize = mips_optimize;
6635 mips_optimize = 2;
6636 /* Itbl support may require additional care here. */
6637 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6638 BFD_RELOC_LO16, AT);
6639 mips_optimize = hold_mips_optimize;
6640 relax_end ();
6642 else
6643 abort ();
6645 break;
6647 case M_LD_OB:
6648 s = "lw";
6649 goto sd_ob;
6650 case M_SD_OB:
6651 s = "sw";
6652 sd_ob:
6653 assert (HAVE_32BIT_ADDRESSES);
6654 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
6655 offset_expr.X_add_number += 4;
6656 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
6657 break;
6659 /* New code added to support COPZ instructions.
6660 This code builds table entries out of the macros in mip_opcodes.
6661 R4000 uses interlocks to handle coproc delays.
6662 Other chips (like the R3000) require nops to be inserted for delays.
6664 FIXME: Currently, we require that the user handle delays.
6665 In order to fill delay slots for non-interlocked chips,
6666 we must have a way to specify delays based on the coprocessor.
6667 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6668 What are the side-effects of the cop instruction?
6669 What cache support might we have and what are its effects?
6670 Both coprocessor & memory require delays. how long???
6671 What registers are read/set/modified?
6673 If an itbl is provided to interpret cop instructions,
6674 this knowledge can be encoded in the itbl spec. */
6676 case M_COP0:
6677 s = "c0";
6678 goto copz;
6679 case M_COP1:
6680 s = "c1";
6681 goto copz;
6682 case M_COP2:
6683 s = "c2";
6684 goto copz;
6685 case M_COP3:
6686 s = "c3";
6687 copz:
6688 /* For now we just do C (same as Cz). The parameter will be
6689 stored in insn_opcode by mips_ip. */
6690 macro_build (NULL, s, "C", ip->insn_opcode);
6691 break;
6693 case M_MOVE:
6694 move_register (dreg, sreg);
6695 break;
6697 #ifdef LOSING_COMPILER
6698 default:
6699 /* Try and see if this is a new itbl instruction.
6700 This code builds table entries out of the macros in mip_opcodes.
6701 FIXME: For now we just assemble the expression and pass it's
6702 value along as a 32-bit immediate.
6703 We may want to have the assembler assemble this value,
6704 so that we gain the assembler's knowledge of delay slots,
6705 symbols, etc.
6706 Would it be more efficient to use mask (id) here? */
6707 if (itbl_have_entries
6708 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6710 s = ip->insn_mo->name;
6711 s2 = "cop3";
6712 coproc = ITBL_DECODE_PNUM (immed_expr);;
6713 macro_build (&immed_expr, s, "C");
6714 break;
6716 macro2 (ip);
6717 break;
6719 if (mips_opts.noat && used_at)
6720 as_bad (_("Macro used $at after \".set noat\""));
6723 static void
6724 macro2 (struct mips_cl_insn *ip)
6726 register int treg, sreg, dreg, breg;
6727 int tempreg;
6728 int mask;
6729 int used_at;
6730 expressionS expr1;
6731 const char *s;
6732 const char *s2;
6733 const char *fmt;
6734 int likely = 0;
6735 int dbl = 0;
6736 int coproc = 0;
6737 int lr = 0;
6738 int imm = 0;
6739 int off;
6740 offsetT maxnum;
6741 bfd_reloc_code_real_type r;
6743 treg = (ip->insn_opcode >> 16) & 0x1f;
6744 dreg = (ip->insn_opcode >> 11) & 0x1f;
6745 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6746 mask = ip->insn_mo->mask;
6748 expr1.X_op = O_constant;
6749 expr1.X_op_symbol = NULL;
6750 expr1.X_add_symbol = NULL;
6751 expr1.X_add_number = 1;
6753 switch (mask)
6755 #endif /* LOSING_COMPILER */
6757 case M_DMUL:
6758 dbl = 1;
6759 case M_MUL:
6760 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6761 macro_build (NULL, "mflo", "d", dreg);
6762 break;
6764 case M_DMUL_I:
6765 dbl = 1;
6766 case M_MUL_I:
6767 /* The MIPS assembler some times generates shifts and adds. I'm
6768 not trying to be that fancy. GCC should do this for us
6769 anyway. */
6770 used_at = 1;
6771 load_register (AT, &imm_expr, dbl);
6772 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6773 macro_build (NULL, "mflo", "d", dreg);
6774 break;
6776 case M_DMULO_I:
6777 dbl = 1;
6778 case M_MULO_I:
6779 imm = 1;
6780 goto do_mulo;
6782 case M_DMULO:
6783 dbl = 1;
6784 case M_MULO:
6785 do_mulo:
6786 start_noreorder ();
6787 used_at = 1;
6788 if (imm)
6789 load_register (AT, &imm_expr, dbl);
6790 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6791 macro_build (NULL, "mflo", "d", dreg);
6792 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6793 macro_build (NULL, "mfhi", "d", AT);
6794 if (mips_trap)
6795 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
6796 else
6798 expr1.X_add_number = 8;
6799 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6800 macro_build (NULL, "nop", "", 0);
6801 macro_build (NULL, "break", "c", 6);
6803 end_noreorder ();
6804 macro_build (NULL, "mflo", "d", dreg);
6805 break;
6807 case M_DMULOU_I:
6808 dbl = 1;
6809 case M_MULOU_I:
6810 imm = 1;
6811 goto do_mulou;
6813 case M_DMULOU:
6814 dbl = 1;
6815 case M_MULOU:
6816 do_mulou:
6817 start_noreorder ();
6818 used_at = 1;
6819 if (imm)
6820 load_register (AT, &imm_expr, dbl);
6821 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
6822 sreg, imm ? AT : treg);
6823 macro_build (NULL, "mfhi", "d", AT);
6824 macro_build (NULL, "mflo", "d", dreg);
6825 if (mips_trap)
6826 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
6827 else
6829 expr1.X_add_number = 8;
6830 macro_build (&expr1, "beq", "s,t,p", AT, 0);
6831 macro_build (NULL, "nop", "", 0);
6832 macro_build (NULL, "break", "c", 6);
6834 end_noreorder ();
6835 break;
6837 case M_DROL:
6838 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6840 if (dreg == sreg)
6842 tempreg = AT;
6843 used_at = 1;
6845 else
6847 tempreg = dreg;
6849 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
6850 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
6851 break;
6853 used_at = 1;
6854 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6855 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
6856 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
6857 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6858 break;
6860 case M_ROL:
6861 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6863 if (dreg == sreg)
6865 tempreg = AT;
6866 used_at = 1;
6868 else
6870 tempreg = dreg;
6872 macro_build (NULL, "negu", "d,w", tempreg, treg);
6873 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
6874 break;
6876 used_at = 1;
6877 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6878 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
6879 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
6880 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6881 break;
6883 case M_DROL_I:
6885 unsigned int rot;
6886 char *l, *r;
6888 if (imm_expr.X_op != O_constant)
6889 as_bad (_("Improper rotate count"));
6890 rot = imm_expr.X_add_number & 0x3f;
6891 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6893 rot = (64 - rot) & 0x3f;
6894 if (rot >= 32)
6895 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6896 else
6897 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6898 break;
6900 if (rot == 0)
6902 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6903 break;
6905 l = (rot < 0x20) ? "dsll" : "dsll32";
6906 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6907 rot &= 0x1f;
6908 used_at = 1;
6909 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
6910 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6911 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6913 break;
6915 case M_ROL_I:
6917 unsigned int rot;
6919 if (imm_expr.X_op != O_constant)
6920 as_bad (_("Improper rotate count"));
6921 rot = imm_expr.X_add_number & 0x1f;
6922 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6924 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
6925 break;
6927 if (rot == 0)
6929 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
6930 break;
6932 used_at = 1;
6933 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
6934 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6935 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6937 break;
6939 case M_DROR:
6940 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6942 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
6943 break;
6945 used_at = 1;
6946 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6947 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
6948 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
6949 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6950 break;
6952 case M_ROR:
6953 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6955 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
6956 break;
6958 used_at = 1;
6959 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6960 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
6961 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
6962 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6963 break;
6965 case M_DROR_I:
6967 unsigned int rot;
6968 char *l, *r;
6970 if (imm_expr.X_op != O_constant)
6971 as_bad (_("Improper rotate count"));
6972 rot = imm_expr.X_add_number & 0x3f;
6973 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6975 if (rot >= 32)
6976 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6977 else
6978 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6979 break;
6981 if (rot == 0)
6983 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6984 break;
6986 r = (rot < 0x20) ? "dsrl" : "dsrl32";
6987 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6988 rot &= 0x1f;
6989 used_at = 1;
6990 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
6991 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6992 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6994 break;
6996 case M_ROR_I:
6998 unsigned int rot;
7000 if (imm_expr.X_op != O_constant)
7001 as_bad (_("Improper rotate count"));
7002 rot = imm_expr.X_add_number & 0x1f;
7003 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7005 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
7006 break;
7008 if (rot == 0)
7010 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7011 break;
7013 used_at = 1;
7014 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7015 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7016 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7018 break;
7020 case M_S_DOB:
7021 if (mips_opts.arch == CPU_R4650)
7023 as_bad (_("opcode not supported on this processor"));
7024 break;
7026 assert (mips_opts.isa == ISA_MIPS1);
7027 /* Even on a big endian machine $fn comes before $fn+1. We have
7028 to adjust when storing to memory. */
7029 macro_build (&offset_expr, "swc1", "T,o(b)",
7030 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
7031 offset_expr.X_add_number += 4;
7032 macro_build (&offset_expr, "swc1", "T,o(b)",
7033 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
7034 break;
7036 case M_SEQ:
7037 if (sreg == 0)
7038 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
7039 else if (treg == 0)
7040 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7041 else
7043 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7044 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7046 break;
7048 case M_SEQ_I:
7049 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7051 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7052 break;
7054 if (sreg == 0)
7056 as_warn (_("Instruction %s: result is always false"),
7057 ip->insn_mo->name);
7058 move_register (dreg, 0);
7059 break;
7061 if (imm_expr.X_op == O_constant
7062 && imm_expr.X_add_number >= 0
7063 && imm_expr.X_add_number < 0x10000)
7065 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7067 else if (imm_expr.X_op == O_constant
7068 && imm_expr.X_add_number > -0x8000
7069 && imm_expr.X_add_number < 0)
7071 imm_expr.X_add_number = -imm_expr.X_add_number;
7072 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7073 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7075 else
7077 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7078 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7079 used_at = 1;
7081 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7082 break;
7084 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7085 s = "slt";
7086 goto sge;
7087 case M_SGEU:
7088 s = "sltu";
7089 sge:
7090 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7091 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7092 break;
7094 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7095 case M_SGEU_I:
7096 if (imm_expr.X_op == O_constant
7097 && imm_expr.X_add_number >= -0x8000
7098 && imm_expr.X_add_number < 0x8000)
7100 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7101 dreg, sreg, BFD_RELOC_LO16);
7103 else
7105 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7106 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7107 dreg, sreg, AT);
7108 used_at = 1;
7110 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7111 break;
7113 case M_SGT: /* sreg > treg <==> treg < sreg */
7114 s = "slt";
7115 goto sgt;
7116 case M_SGTU:
7117 s = "sltu";
7118 sgt:
7119 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7120 break;
7122 case M_SGT_I: /* sreg > I <==> I < sreg */
7123 s = "slt";
7124 goto sgti;
7125 case M_SGTU_I:
7126 s = "sltu";
7127 sgti:
7128 used_at = 1;
7129 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7130 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7131 break;
7133 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7134 s = "slt";
7135 goto sle;
7136 case M_SLEU:
7137 s = "sltu";
7138 sle:
7139 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7140 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7141 break;
7143 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7144 s = "slt";
7145 goto slei;
7146 case M_SLEU_I:
7147 s = "sltu";
7148 slei:
7149 used_at = 1;
7150 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7151 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7152 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7153 break;
7155 case M_SLT_I:
7156 if (imm_expr.X_op == O_constant
7157 && imm_expr.X_add_number >= -0x8000
7158 && imm_expr.X_add_number < 0x8000)
7160 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7161 break;
7163 used_at = 1;
7164 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7165 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
7166 break;
7168 case M_SLTU_I:
7169 if (imm_expr.X_op == O_constant
7170 && imm_expr.X_add_number >= -0x8000
7171 && imm_expr.X_add_number < 0x8000)
7173 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
7174 BFD_RELOC_LO16);
7175 break;
7177 used_at = 1;
7178 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7179 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
7180 break;
7182 case M_SNE:
7183 if (sreg == 0)
7184 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
7185 else if (treg == 0)
7186 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7187 else
7189 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7190 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7192 break;
7194 case M_SNE_I:
7195 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7197 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7198 break;
7200 if (sreg == 0)
7202 as_warn (_("Instruction %s: result is always true"),
7203 ip->insn_mo->name);
7204 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7205 dreg, 0, BFD_RELOC_LO16);
7206 break;
7208 if (imm_expr.X_op == O_constant
7209 && imm_expr.X_add_number >= 0
7210 && imm_expr.X_add_number < 0x10000)
7212 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7214 else if (imm_expr.X_op == O_constant
7215 && imm_expr.X_add_number > -0x8000
7216 && imm_expr.X_add_number < 0)
7218 imm_expr.X_add_number = -imm_expr.X_add_number;
7219 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7220 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7222 else
7224 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7225 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7226 used_at = 1;
7228 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7229 break;
7231 case M_DSUB_I:
7232 dbl = 1;
7233 case M_SUB_I:
7234 if (imm_expr.X_op == O_constant
7235 && imm_expr.X_add_number > -0x8000
7236 && imm_expr.X_add_number <= 0x8000)
7238 imm_expr.X_add_number = -imm_expr.X_add_number;
7239 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7240 dreg, sreg, BFD_RELOC_LO16);
7241 break;
7243 used_at = 1;
7244 load_register (AT, &imm_expr, dbl);
7245 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7246 break;
7248 case M_DSUBU_I:
7249 dbl = 1;
7250 case M_SUBU_I:
7251 if (imm_expr.X_op == O_constant
7252 && imm_expr.X_add_number > -0x8000
7253 && imm_expr.X_add_number <= 0x8000)
7255 imm_expr.X_add_number = -imm_expr.X_add_number;
7256 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7257 dreg, sreg, BFD_RELOC_LO16);
7258 break;
7260 used_at = 1;
7261 load_register (AT, &imm_expr, dbl);
7262 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7263 break;
7265 case M_TEQ_I:
7266 s = "teq";
7267 goto trap;
7268 case M_TGE_I:
7269 s = "tge";
7270 goto trap;
7271 case M_TGEU_I:
7272 s = "tgeu";
7273 goto trap;
7274 case M_TLT_I:
7275 s = "tlt";
7276 goto trap;
7277 case M_TLTU_I:
7278 s = "tltu";
7279 goto trap;
7280 case M_TNE_I:
7281 s = "tne";
7282 trap:
7283 used_at = 1;
7284 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7285 macro_build (NULL, s, "s,t", sreg, AT);
7286 break;
7288 case M_TRUNCWS:
7289 case M_TRUNCWD:
7290 assert (mips_opts.isa == ISA_MIPS1);
7291 used_at = 1;
7292 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7293 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7296 * Is the double cfc1 instruction a bug in the mips assembler;
7297 * or is there a reason for it?
7299 start_noreorder ();
7300 macro_build (NULL, "cfc1", "t,G", treg, RA);
7301 macro_build (NULL, "cfc1", "t,G", treg, RA);
7302 macro_build (NULL, "nop", "");
7303 expr1.X_add_number = 3;
7304 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
7305 expr1.X_add_number = 2;
7306 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7307 macro_build (NULL, "ctc1", "t,G", AT, RA);
7308 macro_build (NULL, "nop", "");
7309 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7310 dreg, sreg);
7311 macro_build (NULL, "ctc1", "t,G", treg, RA);
7312 macro_build (NULL, "nop", "");
7313 end_noreorder ();
7314 break;
7316 case M_ULH:
7317 s = "lb";
7318 goto ulh;
7319 case M_ULHU:
7320 s = "lbu";
7321 ulh:
7322 used_at = 1;
7323 if (offset_expr.X_add_number >= 0x7fff)
7324 as_bad (_("operand overflow"));
7325 if (! target_big_endian)
7326 ++offset_expr.X_add_number;
7327 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
7328 if (! target_big_endian)
7329 --offset_expr.X_add_number;
7330 else
7331 ++offset_expr.X_add_number;
7332 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7333 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7334 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7335 break;
7337 case M_ULD:
7338 s = "ldl";
7339 s2 = "ldr";
7340 off = 7;
7341 goto ulw;
7342 case M_ULW:
7343 s = "lwl";
7344 s2 = "lwr";
7345 off = 3;
7346 ulw:
7347 if (offset_expr.X_add_number >= 0x8000 - off)
7348 as_bad (_("operand overflow"));
7349 if (treg != breg)
7350 tempreg = treg;
7351 else
7353 used_at = 1;
7354 tempreg = AT;
7356 if (! target_big_endian)
7357 offset_expr.X_add_number += off;
7358 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7359 if (! target_big_endian)
7360 offset_expr.X_add_number -= off;
7361 else
7362 offset_expr.X_add_number += off;
7363 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7365 /* If necessary, move the result in tempreg the final destination. */
7366 if (treg == tempreg)
7367 break;
7368 /* Protect second load's delay slot. */
7369 load_delay_nop ();
7370 move_register (treg, tempreg);
7371 break;
7373 case M_ULD_A:
7374 s = "ldl";
7375 s2 = "ldr";
7376 off = 7;
7377 goto ulwa;
7378 case M_ULW_A:
7379 s = "lwl";
7380 s2 = "lwr";
7381 off = 3;
7382 ulwa:
7383 used_at = 1;
7384 load_address (AT, &offset_expr, &used_at);
7385 if (breg != 0)
7386 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7387 if (! target_big_endian)
7388 expr1.X_add_number = off;
7389 else
7390 expr1.X_add_number = 0;
7391 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7392 if (! target_big_endian)
7393 expr1.X_add_number = 0;
7394 else
7395 expr1.X_add_number = off;
7396 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7397 break;
7399 case M_ULH_A:
7400 case M_ULHU_A:
7401 used_at = 1;
7402 load_address (AT, &offset_expr, &used_at);
7403 if (breg != 0)
7404 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7405 if (target_big_endian)
7406 expr1.X_add_number = 0;
7407 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7408 treg, BFD_RELOC_LO16, AT);
7409 if (target_big_endian)
7410 expr1.X_add_number = 1;
7411 else
7412 expr1.X_add_number = 0;
7413 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7414 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7415 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7416 break;
7418 case M_USH:
7419 used_at = 1;
7420 if (offset_expr.X_add_number >= 0x7fff)
7421 as_bad (_("operand overflow"));
7422 if (target_big_endian)
7423 ++offset_expr.X_add_number;
7424 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7425 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
7426 if (target_big_endian)
7427 --offset_expr.X_add_number;
7428 else
7429 ++offset_expr.X_add_number;
7430 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
7431 break;
7433 case M_USD:
7434 s = "sdl";
7435 s2 = "sdr";
7436 off = 7;
7437 goto usw;
7438 case M_USW:
7439 s = "swl";
7440 s2 = "swr";
7441 off = 3;
7442 usw:
7443 if (offset_expr.X_add_number >= 0x8000 - off)
7444 as_bad (_("operand overflow"));
7445 if (! target_big_endian)
7446 offset_expr.X_add_number += off;
7447 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7448 if (! target_big_endian)
7449 offset_expr.X_add_number -= off;
7450 else
7451 offset_expr.X_add_number += off;
7452 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7453 break;
7455 case M_USD_A:
7456 s = "sdl";
7457 s2 = "sdr";
7458 off = 7;
7459 goto uswa;
7460 case M_USW_A:
7461 s = "swl";
7462 s2 = "swr";
7463 off = 3;
7464 uswa:
7465 used_at = 1;
7466 load_address (AT, &offset_expr, &used_at);
7467 if (breg != 0)
7468 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7469 if (! target_big_endian)
7470 expr1.X_add_number = off;
7471 else
7472 expr1.X_add_number = 0;
7473 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7474 if (! target_big_endian)
7475 expr1.X_add_number = 0;
7476 else
7477 expr1.X_add_number = off;
7478 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7479 break;
7481 case M_USH_A:
7482 used_at = 1;
7483 load_address (AT, &offset_expr, &used_at);
7484 if (breg != 0)
7485 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7486 if (! target_big_endian)
7487 expr1.X_add_number = 0;
7488 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7489 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
7490 if (! target_big_endian)
7491 expr1.X_add_number = 1;
7492 else
7493 expr1.X_add_number = 0;
7494 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7495 if (! target_big_endian)
7496 expr1.X_add_number = 0;
7497 else
7498 expr1.X_add_number = 1;
7499 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7500 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7501 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7502 break;
7504 default:
7505 /* FIXME: Check if this is one of the itbl macros, since they
7506 are added dynamically. */
7507 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7508 break;
7510 if (mips_opts.noat && used_at)
7511 as_bad (_("Macro used $at after \".set noat\""));
7514 /* Implement macros in mips16 mode. */
7516 static void
7517 mips16_macro (struct mips_cl_insn *ip)
7519 int mask;
7520 int xreg, yreg, zreg, tmp;
7521 expressionS expr1;
7522 int dbl;
7523 const char *s, *s2, *s3;
7525 mask = ip->insn_mo->mask;
7527 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
7528 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
7529 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
7531 expr1.X_op = O_constant;
7532 expr1.X_op_symbol = NULL;
7533 expr1.X_add_symbol = NULL;
7534 expr1.X_add_number = 1;
7536 dbl = 0;
7538 switch (mask)
7540 default:
7541 internalError ();
7543 case M_DDIV_3:
7544 dbl = 1;
7545 case M_DIV_3:
7546 s = "mflo";
7547 goto do_div3;
7548 case M_DREM_3:
7549 dbl = 1;
7550 case M_REM_3:
7551 s = "mfhi";
7552 do_div3:
7553 start_noreorder ();
7554 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
7555 expr1.X_add_number = 2;
7556 macro_build (&expr1, "bnez", "x,p", yreg);
7557 macro_build (NULL, "break", "6", 7);
7559 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7560 since that causes an overflow. We should do that as well,
7561 but I don't see how to do the comparisons without a temporary
7562 register. */
7563 end_noreorder ();
7564 macro_build (NULL, s, "x", zreg);
7565 break;
7567 case M_DIVU_3:
7568 s = "divu";
7569 s2 = "mflo";
7570 goto do_divu3;
7571 case M_REMU_3:
7572 s = "divu";
7573 s2 = "mfhi";
7574 goto do_divu3;
7575 case M_DDIVU_3:
7576 s = "ddivu";
7577 s2 = "mflo";
7578 goto do_divu3;
7579 case M_DREMU_3:
7580 s = "ddivu";
7581 s2 = "mfhi";
7582 do_divu3:
7583 start_noreorder ();
7584 macro_build (NULL, s, "0,x,y", xreg, yreg);
7585 expr1.X_add_number = 2;
7586 macro_build (&expr1, "bnez", "x,p", yreg);
7587 macro_build (NULL, "break", "6", 7);
7588 end_noreorder ();
7589 macro_build (NULL, s2, "x", zreg);
7590 break;
7592 case M_DMUL:
7593 dbl = 1;
7594 case M_MUL:
7595 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7596 macro_build (NULL, "mflo", "x", zreg);
7597 break;
7599 case M_DSUBU_I:
7600 dbl = 1;
7601 goto do_subu;
7602 case M_SUBU_I:
7603 do_subu:
7604 if (imm_expr.X_op != O_constant)
7605 as_bad (_("Unsupported large constant"));
7606 imm_expr.X_add_number = -imm_expr.X_add_number;
7607 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7608 break;
7610 case M_SUBU_I_2:
7611 if (imm_expr.X_op != O_constant)
7612 as_bad (_("Unsupported large constant"));
7613 imm_expr.X_add_number = -imm_expr.X_add_number;
7614 macro_build (&imm_expr, "addiu", "x,k", xreg);
7615 break;
7617 case M_DSUBU_I_2:
7618 if (imm_expr.X_op != O_constant)
7619 as_bad (_("Unsupported large constant"));
7620 imm_expr.X_add_number = -imm_expr.X_add_number;
7621 macro_build (&imm_expr, "daddiu", "y,j", yreg);
7622 break;
7624 case M_BEQ:
7625 s = "cmp";
7626 s2 = "bteqz";
7627 goto do_branch;
7628 case M_BNE:
7629 s = "cmp";
7630 s2 = "btnez";
7631 goto do_branch;
7632 case M_BLT:
7633 s = "slt";
7634 s2 = "btnez";
7635 goto do_branch;
7636 case M_BLTU:
7637 s = "sltu";
7638 s2 = "btnez";
7639 goto do_branch;
7640 case M_BLE:
7641 s = "slt";
7642 s2 = "bteqz";
7643 goto do_reverse_branch;
7644 case M_BLEU:
7645 s = "sltu";
7646 s2 = "bteqz";
7647 goto do_reverse_branch;
7648 case M_BGE:
7649 s = "slt";
7650 s2 = "bteqz";
7651 goto do_branch;
7652 case M_BGEU:
7653 s = "sltu";
7654 s2 = "bteqz";
7655 goto do_branch;
7656 case M_BGT:
7657 s = "slt";
7658 s2 = "btnez";
7659 goto do_reverse_branch;
7660 case M_BGTU:
7661 s = "sltu";
7662 s2 = "btnez";
7664 do_reverse_branch:
7665 tmp = xreg;
7666 xreg = yreg;
7667 yreg = tmp;
7669 do_branch:
7670 macro_build (NULL, s, "x,y", xreg, yreg);
7671 macro_build (&offset_expr, s2, "p");
7672 break;
7674 case M_BEQ_I:
7675 s = "cmpi";
7676 s2 = "bteqz";
7677 s3 = "x,U";
7678 goto do_branch_i;
7679 case M_BNE_I:
7680 s = "cmpi";
7681 s2 = "btnez";
7682 s3 = "x,U";
7683 goto do_branch_i;
7684 case M_BLT_I:
7685 s = "slti";
7686 s2 = "btnez";
7687 s3 = "x,8";
7688 goto do_branch_i;
7689 case M_BLTU_I:
7690 s = "sltiu";
7691 s2 = "btnez";
7692 s3 = "x,8";
7693 goto do_branch_i;
7694 case M_BLE_I:
7695 s = "slti";
7696 s2 = "btnez";
7697 s3 = "x,8";
7698 goto do_addone_branch_i;
7699 case M_BLEU_I:
7700 s = "sltiu";
7701 s2 = "btnez";
7702 s3 = "x,8";
7703 goto do_addone_branch_i;
7704 case M_BGE_I:
7705 s = "slti";
7706 s2 = "bteqz";
7707 s3 = "x,8";
7708 goto do_branch_i;
7709 case M_BGEU_I:
7710 s = "sltiu";
7711 s2 = "bteqz";
7712 s3 = "x,8";
7713 goto do_branch_i;
7714 case M_BGT_I:
7715 s = "slti";
7716 s2 = "bteqz";
7717 s3 = "x,8";
7718 goto do_addone_branch_i;
7719 case M_BGTU_I:
7720 s = "sltiu";
7721 s2 = "bteqz";
7722 s3 = "x,8";
7724 do_addone_branch_i:
7725 if (imm_expr.X_op != O_constant)
7726 as_bad (_("Unsupported large constant"));
7727 ++imm_expr.X_add_number;
7729 do_branch_i:
7730 macro_build (&imm_expr, s, s3, xreg);
7731 macro_build (&offset_expr, s2, "p");
7732 break;
7734 case M_ABS:
7735 expr1.X_add_number = 0;
7736 macro_build (&expr1, "slti", "x,8", yreg);
7737 if (xreg != yreg)
7738 move_register (xreg, yreg);
7739 expr1.X_add_number = 2;
7740 macro_build (&expr1, "bteqz", "p");
7741 macro_build (NULL, "neg", "x,w", xreg, xreg);
7745 /* For consistency checking, verify that all bits are specified either
7746 by the match/mask part of the instruction definition, or by the
7747 operand list. */
7748 static int
7749 validate_mips_insn (const struct mips_opcode *opc)
7751 const char *p = opc->args;
7752 char c;
7753 unsigned long used_bits = opc->mask;
7755 if ((used_bits & opc->match) != opc->match)
7757 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7758 opc->name, opc->args);
7759 return 0;
7761 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7762 while (*p)
7763 switch (c = *p++)
7765 case ',': break;
7766 case '(': break;
7767 case ')': break;
7768 case '+':
7769 switch (c = *p++)
7771 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7772 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7773 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7774 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7775 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7776 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7777 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7778 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7779 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7780 case 'I': break;
7781 default:
7782 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7783 c, opc->name, opc->args);
7784 return 0;
7786 break;
7787 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7788 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7789 case 'A': break;
7790 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7791 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7792 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7793 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7794 case 'F': break;
7795 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7796 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7797 case 'I': break;
7798 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7799 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7800 case 'L': break;
7801 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7802 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7803 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7804 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7805 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7806 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7807 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7808 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7809 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7810 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7811 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7812 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7813 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7814 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7815 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7816 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7817 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7818 case 'f': break;
7819 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7820 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7821 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7822 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7823 case 'l': break;
7824 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7825 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7826 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7827 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7828 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7829 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7830 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7831 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7832 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7833 case 'x': break;
7834 case 'z': break;
7835 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7836 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7837 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7838 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7839 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7840 case '[': break;
7841 case ']': break;
7842 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
7843 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
7844 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
7845 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7846 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
7847 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
7848 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
7849 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
7850 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
7851 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
7852 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
7853 default:
7854 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7855 c, opc->name, opc->args);
7856 return 0;
7858 #undef USE_BITS
7859 if (used_bits != 0xffffffff)
7861 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7862 ~used_bits & 0xffffffff, opc->name, opc->args);
7863 return 0;
7865 return 1;
7868 /* This routine assembles an instruction into its binary format. As a
7869 side effect, it sets one of the global variables imm_reloc or
7870 offset_reloc to the type of relocation to do if one of the operands
7871 is an address expression. */
7873 static void
7874 mips_ip (char *str, struct mips_cl_insn *ip)
7876 char *s;
7877 const char *args;
7878 char c = 0;
7879 struct mips_opcode *insn;
7880 char *argsStart;
7881 unsigned int regno;
7882 unsigned int lastregno = 0;
7883 unsigned int lastpos = 0;
7884 unsigned int limlo, limhi;
7885 char *s_reset;
7886 char save_c = 0;
7887 offsetT min_range, max_range;
7889 insn_error = NULL;
7891 /* If the instruction contains a '.', we first try to match an instruction
7892 including the '.'. Then we try again without the '.'. */
7893 insn = NULL;
7894 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7895 continue;
7897 /* If we stopped on whitespace, then replace the whitespace with null for
7898 the call to hash_find. Save the character we replaced just in case we
7899 have to re-parse the instruction. */
7900 if (ISSPACE (*s))
7902 save_c = *s;
7903 *s++ = '\0';
7906 insn = (struct mips_opcode *) hash_find (op_hash, str);
7908 /* If we didn't find the instruction in the opcode table, try again, but
7909 this time with just the instruction up to, but not including the
7910 first '.'. */
7911 if (insn == NULL)
7913 /* Restore the character we overwrite above (if any). */
7914 if (save_c)
7915 *(--s) = save_c;
7917 /* Scan up to the first '.' or whitespace. */
7918 for (s = str;
7919 *s != '\0' && *s != '.' && !ISSPACE (*s);
7920 ++s)
7921 continue;
7923 /* If we did not find a '.', then we can quit now. */
7924 if (*s != '.')
7926 insn_error = "unrecognized opcode";
7927 return;
7930 /* Lookup the instruction in the hash table. */
7931 *s++ = '\0';
7932 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7934 insn_error = "unrecognized opcode";
7935 return;
7939 argsStart = s;
7940 for (;;)
7942 bfd_boolean ok;
7944 assert (strcmp (insn->name, str) == 0);
7946 if (OPCODE_IS_MEMBER (insn,
7947 (mips_opts.isa
7948 | (file_ase_mips16 ? INSN_MIPS16 : 0)
7949 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7950 | (mips_opts.ase_dsp ? INSN_DSP : 0)
7951 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7952 mips_opts.arch))
7953 ok = TRUE;
7954 else
7955 ok = FALSE;
7957 if (insn->pinfo != INSN_MACRO)
7959 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7960 ok = FALSE;
7963 if (! ok)
7965 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7966 && strcmp (insn->name, insn[1].name) == 0)
7968 ++insn;
7969 continue;
7971 else
7973 if (!insn_error)
7975 static char buf[100];
7976 sprintf (buf,
7977 _("opcode not supported on this processor: %s (%s)"),
7978 mips_cpu_info_from_arch (mips_opts.arch)->name,
7979 mips_cpu_info_from_isa (mips_opts.isa)->name);
7980 insn_error = buf;
7982 if (save_c)
7983 *(--s) = save_c;
7984 return;
7988 create_insn (ip, insn);
7989 insn_error = NULL;
7990 for (args = insn->args;; ++args)
7992 int is_mdmx;
7994 s += strspn (s, " \t");
7995 is_mdmx = 0;
7996 switch (*args)
7998 case '\0': /* end of args */
7999 if (*s == '\0')
8000 return;
8001 break;
8003 case '3': /* dsp 3-bit unsigned immediate in bit 21 */
8004 my_getExpression (&imm_expr, s);
8005 check_absolute_expr (ip, &imm_expr);
8006 if (imm_expr.X_add_number & ~OP_MASK_SA3)
8008 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8009 OP_MASK_SA3, (unsigned long) imm_expr.X_add_number);
8010 imm_expr.X_add_number &= OP_MASK_SA3;
8012 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SA3;
8013 imm_expr.X_op = O_absent;
8014 s = expr_end;
8015 continue;
8017 case '4': /* dsp 4-bit unsigned immediate in bit 21 */
8018 my_getExpression (&imm_expr, s);
8019 check_absolute_expr (ip, &imm_expr);
8020 if (imm_expr.X_add_number & ~OP_MASK_SA4)
8022 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8023 OP_MASK_SA4, (unsigned long) imm_expr.X_add_number);
8024 imm_expr.X_add_number &= OP_MASK_SA4;
8026 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SA4;
8027 imm_expr.X_op = O_absent;
8028 s = expr_end;
8029 continue;
8031 case '5': /* dsp 8-bit unsigned immediate in bit 16 */
8032 my_getExpression (&imm_expr, s);
8033 check_absolute_expr (ip, &imm_expr);
8034 if (imm_expr.X_add_number & ~OP_MASK_IMM8)
8036 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8037 OP_MASK_IMM8, (unsigned long) imm_expr.X_add_number);
8038 imm_expr.X_add_number &= OP_MASK_IMM8;
8040 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_IMM8;
8041 imm_expr.X_op = O_absent;
8042 s = expr_end;
8043 continue;
8045 case '6': /* dsp 5-bit unsigned immediate in bit 21 */
8046 my_getExpression (&imm_expr, s);
8047 check_absolute_expr (ip, &imm_expr);
8048 if (imm_expr.X_add_number & ~OP_MASK_RS)
8050 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8051 OP_MASK_RS, (unsigned long) imm_expr.X_add_number);
8052 imm_expr.X_add_number &= OP_MASK_RS;
8054 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_RS;
8055 imm_expr.X_op = O_absent;
8056 s = expr_end;
8057 continue;
8059 case '7': /* four dsp accumulators in bits 11,12 */
8060 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8061 s[3] >= '0' && s[3] <= '3')
8063 regno = s[3] - '0';
8064 s += 4;
8065 ip->insn_opcode |= regno << OP_SH_DSPACC;
8066 continue;
8068 else
8069 as_bad (_("Invalid dsp acc register"));
8070 break;
8072 case '8': /* dsp 6-bit unsigned immediate in bit 11 */
8073 my_getExpression (&imm_expr, s);
8074 check_absolute_expr (ip, &imm_expr);
8075 if (imm_expr.X_add_number & ~OP_MASK_WRDSP)
8077 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8078 OP_MASK_WRDSP,
8079 (unsigned long) imm_expr.X_add_number);
8080 imm_expr.X_add_number &= OP_MASK_WRDSP;
8082 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_WRDSP;
8083 imm_expr.X_op = O_absent;
8084 s = expr_end;
8085 continue;
8087 case '9': /* four dsp accumulators in bits 21,22 */
8088 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8089 s[3] >= '0' && s[3] <= '3')
8091 regno = s[3] - '0';
8092 s += 4;
8093 ip->insn_opcode |= regno << OP_SH_DSPACC_S;
8094 continue;
8096 else
8097 as_bad (_("Invalid dsp acc register"));
8098 break;
8100 case '0': /* dsp 6-bit signed immediate in bit 20 */
8101 my_getExpression (&imm_expr, s);
8102 check_absolute_expr (ip, &imm_expr);
8103 min_range = -((OP_MASK_DSPSFT + 1) >> 1);
8104 max_range = ((OP_MASK_DSPSFT + 1) >> 1) - 1;
8105 if (imm_expr.X_add_number < min_range ||
8106 imm_expr.X_add_number > max_range)
8108 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8109 (long) min_range, (long) max_range,
8110 (long) imm_expr.X_add_number);
8112 imm_expr.X_add_number &= OP_MASK_DSPSFT;
8113 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8114 << OP_SH_DSPSFT);
8115 imm_expr.X_op = O_absent;
8116 s = expr_end;
8117 continue;
8119 case '\'': /* dsp 6-bit unsigned immediate in bit 16 */
8120 my_getExpression (&imm_expr, s);
8121 check_absolute_expr (ip, &imm_expr);
8122 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
8124 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8125 OP_MASK_RDDSP,
8126 (unsigned long) imm_expr.X_add_number);
8127 imm_expr.X_add_number &= OP_MASK_RDDSP;
8129 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_RDDSP;
8130 imm_expr.X_op = O_absent;
8131 s = expr_end;
8132 continue;
8134 case ':': /* dsp 7-bit signed immediate in bit 19 */
8135 my_getExpression (&imm_expr, s);
8136 check_absolute_expr (ip, &imm_expr);
8137 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
8138 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
8139 if (imm_expr.X_add_number < min_range ||
8140 imm_expr.X_add_number > max_range)
8142 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8143 (long) min_range, (long) max_range,
8144 (long) imm_expr.X_add_number);
8146 imm_expr.X_add_number &= OP_MASK_DSPSFT_7;
8147 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8148 << OP_SH_DSPSFT_7);
8149 imm_expr.X_op = O_absent;
8150 s = expr_end;
8151 continue;
8153 case '@': /* dsp 10-bit signed immediate in bit 16 */
8154 my_getExpression (&imm_expr, s);
8155 check_absolute_expr (ip, &imm_expr);
8156 min_range = -((OP_MASK_IMM10 + 1) >> 1);
8157 max_range = ((OP_MASK_IMM10 + 1) >> 1) - 1;
8158 if (imm_expr.X_add_number < min_range ||
8159 imm_expr.X_add_number > max_range)
8161 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8162 (long) min_range, (long) max_range,
8163 (long) imm_expr.X_add_number);
8165 imm_expr.X_add_number &= OP_MASK_IMM10;
8166 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8167 << OP_SH_IMM10);
8168 imm_expr.X_op = O_absent;
8169 s = expr_end;
8170 continue;
8172 case ',':
8173 if (*s++ == *args)
8174 continue;
8175 s--;
8176 switch (*++args)
8178 case 'r':
8179 case 'v':
8180 INSERT_OPERAND (RS, *ip, lastregno);
8181 continue;
8183 case 'w':
8184 INSERT_OPERAND (RT, *ip, lastregno);
8185 continue;
8187 case 'W':
8188 INSERT_OPERAND (FT, *ip, lastregno);
8189 continue;
8191 case 'V':
8192 INSERT_OPERAND (FS, *ip, lastregno);
8193 continue;
8195 break;
8197 case '(':
8198 /* Handle optional base register.
8199 Either the base register is omitted or
8200 we must have a left paren. */
8201 /* This is dependent on the next operand specifier
8202 is a base register specification. */
8203 assert (args[1] == 'b' || args[1] == '5'
8204 || args[1] == '-' || args[1] == '4');
8205 if (*s == '\0')
8206 return;
8208 case ')': /* these must match exactly */
8209 case '[':
8210 case ']':
8211 if (*s++ == *args)
8212 continue;
8213 break;
8215 case '+': /* Opcode extension character. */
8216 switch (*++args)
8218 case 'A': /* ins/ext position, becomes LSB. */
8219 limlo = 0;
8220 limhi = 31;
8221 goto do_lsb;
8222 case 'E':
8223 limlo = 32;
8224 limhi = 63;
8225 goto do_lsb;
8226 do_lsb:
8227 my_getExpression (&imm_expr, s);
8228 check_absolute_expr (ip, &imm_expr);
8229 if ((unsigned long) imm_expr.X_add_number < limlo
8230 || (unsigned long) imm_expr.X_add_number > limhi)
8232 as_bad (_("Improper position (%lu)"),
8233 (unsigned long) imm_expr.X_add_number);
8234 imm_expr.X_add_number = limlo;
8236 lastpos = imm_expr.X_add_number;
8237 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8238 imm_expr.X_op = O_absent;
8239 s = expr_end;
8240 continue;
8242 case 'B': /* ins size, becomes MSB. */
8243 limlo = 1;
8244 limhi = 32;
8245 goto do_msb;
8246 case 'F':
8247 limlo = 33;
8248 limhi = 64;
8249 goto do_msb;
8250 do_msb:
8251 my_getExpression (&imm_expr, s);
8252 check_absolute_expr (ip, &imm_expr);
8253 /* Check for negative input so that small negative numbers
8254 will not succeed incorrectly. The checks against
8255 (pos+size) transitively check "size" itself,
8256 assuming that "pos" is reasonable. */
8257 if ((long) imm_expr.X_add_number < 0
8258 || ((unsigned long) imm_expr.X_add_number
8259 + lastpos) < limlo
8260 || ((unsigned long) imm_expr.X_add_number
8261 + lastpos) > limhi)
8263 as_bad (_("Improper insert size (%lu, position %lu)"),
8264 (unsigned long) imm_expr.X_add_number,
8265 (unsigned long) lastpos);
8266 imm_expr.X_add_number = limlo - lastpos;
8268 INSERT_OPERAND (INSMSB, *ip,
8269 lastpos + imm_expr.X_add_number - 1);
8270 imm_expr.X_op = O_absent;
8271 s = expr_end;
8272 continue;
8274 case 'C': /* ext size, becomes MSBD. */
8275 limlo = 1;
8276 limhi = 32;
8277 goto do_msbd;
8278 case 'G':
8279 limlo = 33;
8280 limhi = 64;
8281 goto do_msbd;
8282 case 'H':
8283 limlo = 33;
8284 limhi = 64;
8285 goto do_msbd;
8286 do_msbd:
8287 my_getExpression (&imm_expr, s);
8288 check_absolute_expr (ip, &imm_expr);
8289 /* Check for negative input so that small negative numbers
8290 will not succeed incorrectly. The checks against
8291 (pos+size) transitively check "size" itself,
8292 assuming that "pos" is reasonable. */
8293 if ((long) imm_expr.X_add_number < 0
8294 || ((unsigned long) imm_expr.X_add_number
8295 + lastpos) < limlo
8296 || ((unsigned long) imm_expr.X_add_number
8297 + lastpos) > limhi)
8299 as_bad (_("Improper extract size (%lu, position %lu)"),
8300 (unsigned long) imm_expr.X_add_number,
8301 (unsigned long) lastpos);
8302 imm_expr.X_add_number = limlo - lastpos;
8304 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
8305 imm_expr.X_op = O_absent;
8306 s = expr_end;
8307 continue;
8309 case 'D':
8310 /* +D is for disassembly only; never match. */
8311 break;
8313 case 'I':
8314 /* "+I" is like "I", except that imm2_expr is used. */
8315 my_getExpression (&imm2_expr, s);
8316 if (imm2_expr.X_op != O_big
8317 && imm2_expr.X_op != O_constant)
8318 insn_error = _("absolute expression required");
8319 if (HAVE_32BIT_GPRS)
8320 normalize_constant_expr (&imm2_expr);
8321 s = expr_end;
8322 continue;
8324 default:
8325 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8326 *args, insn->name, insn->args);
8327 /* Further processing is fruitless. */
8328 return;
8330 break;
8332 case '<': /* must be at least one digit */
8334 * According to the manual, if the shift amount is greater
8335 * than 31 or less than 0, then the shift amount should be
8336 * mod 32. In reality the mips assembler issues an error.
8337 * We issue a warning and mask out all but the low 5 bits.
8339 my_getExpression (&imm_expr, s);
8340 check_absolute_expr (ip, &imm_expr);
8341 if ((unsigned long) imm_expr.X_add_number > 31)
8342 as_warn (_("Improper shift amount (%lu)"),
8343 (unsigned long) imm_expr.X_add_number);
8344 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8345 imm_expr.X_op = O_absent;
8346 s = expr_end;
8347 continue;
8349 case '>': /* shift amount minus 32 */
8350 my_getExpression (&imm_expr, s);
8351 check_absolute_expr (ip, &imm_expr);
8352 if ((unsigned long) imm_expr.X_add_number < 32
8353 || (unsigned long) imm_expr.X_add_number > 63)
8354 break;
8355 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
8356 imm_expr.X_op = O_absent;
8357 s = expr_end;
8358 continue;
8360 case 'k': /* cache code */
8361 case 'h': /* prefx code */
8362 my_getExpression (&imm_expr, s);
8363 check_absolute_expr (ip, &imm_expr);
8364 if ((unsigned long) imm_expr.X_add_number > 31)
8365 as_warn (_("Invalid value for `%s' (%lu)"),
8366 ip->insn_mo->name,
8367 (unsigned long) imm_expr.X_add_number);
8368 if (*args == 'k')
8369 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
8370 else
8371 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
8372 imm_expr.X_op = O_absent;
8373 s = expr_end;
8374 continue;
8376 case 'c': /* break code */
8377 my_getExpression (&imm_expr, s);
8378 check_absolute_expr (ip, &imm_expr);
8379 if ((unsigned long) imm_expr.X_add_number > 1023)
8380 as_warn (_("Illegal break code (%lu)"),
8381 (unsigned long) imm_expr.X_add_number);
8382 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
8383 imm_expr.X_op = O_absent;
8384 s = expr_end;
8385 continue;
8387 case 'q': /* lower break code */
8388 my_getExpression (&imm_expr, s);
8389 check_absolute_expr (ip, &imm_expr);
8390 if ((unsigned long) imm_expr.X_add_number > 1023)
8391 as_warn (_("Illegal lower break code (%lu)"),
8392 (unsigned long) imm_expr.X_add_number);
8393 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
8394 imm_expr.X_op = O_absent;
8395 s = expr_end;
8396 continue;
8398 case 'B': /* 20-bit syscall/break code. */
8399 my_getExpression (&imm_expr, s);
8400 check_absolute_expr (ip, &imm_expr);
8401 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8402 as_warn (_("Illegal 20-bit code (%lu)"),
8403 (unsigned long) imm_expr.X_add_number);
8404 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
8405 imm_expr.X_op = O_absent;
8406 s = expr_end;
8407 continue;
8409 case 'C': /* Coprocessor code */
8410 my_getExpression (&imm_expr, s);
8411 check_absolute_expr (ip, &imm_expr);
8412 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8414 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8415 (unsigned long) imm_expr.X_add_number);
8416 imm_expr.X_add_number &= ((1 << 25) - 1);
8418 ip->insn_opcode |= imm_expr.X_add_number;
8419 imm_expr.X_op = O_absent;
8420 s = expr_end;
8421 continue;
8423 case 'J': /* 19-bit wait code. */
8424 my_getExpression (&imm_expr, s);
8425 check_absolute_expr (ip, &imm_expr);
8426 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8427 as_warn (_("Illegal 19-bit code (%lu)"),
8428 (unsigned long) imm_expr.X_add_number);
8429 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
8430 imm_expr.X_op = O_absent;
8431 s = expr_end;
8432 continue;
8434 case 'P': /* Performance register */
8435 my_getExpression (&imm_expr, s);
8436 check_absolute_expr (ip, &imm_expr);
8437 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8438 as_warn (_("Invalid performance register (%lu)"),
8439 (unsigned long) imm_expr.X_add_number);
8440 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
8441 imm_expr.X_op = O_absent;
8442 s = expr_end;
8443 continue;
8445 case 'b': /* base register */
8446 case 'd': /* destination register */
8447 case 's': /* source register */
8448 case 't': /* target register */
8449 case 'r': /* both target and source */
8450 case 'v': /* both dest and source */
8451 case 'w': /* both dest and target */
8452 case 'E': /* coprocessor target register */
8453 case 'G': /* coprocessor destination register */
8454 case 'K': /* 'rdhwr' destination register */
8455 case 'x': /* ignore register name */
8456 case 'z': /* must be zero register */
8457 case 'U': /* destination register (clo/clz). */
8458 s_reset = s;
8459 if (s[0] == '$')
8462 if (ISDIGIT (s[1]))
8464 ++s;
8465 regno = 0;
8468 regno *= 10;
8469 regno += *s - '0';
8470 ++s;
8472 while (ISDIGIT (*s));
8473 if (regno > 31)
8474 as_bad (_("Invalid register number (%d)"), regno);
8476 else if (*args == 'E' || *args == 'G' || *args == 'K')
8477 goto notreg;
8478 else
8480 if (s[1] == 'r' && s[2] == 'a')
8482 s += 3;
8483 regno = RA;
8485 else if (s[1] == 'f' && s[2] == 'p')
8487 s += 3;
8488 regno = FP;
8490 else if (s[1] == 's' && s[2] == 'p')
8492 s += 3;
8493 regno = SP;
8495 else if (s[1] == 'g' && s[2] == 'p')
8497 s += 3;
8498 regno = GP;
8500 else if (s[1] == 'a' && s[2] == 't')
8502 s += 3;
8503 regno = AT;
8505 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8507 s += 4;
8508 regno = KT0;
8510 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8512 s += 4;
8513 regno = KT1;
8515 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8517 s += 5;
8518 regno = ZERO;
8520 else if (itbl_have_entries)
8522 char *p, *n;
8523 unsigned long r;
8525 p = s + 1; /* advance past '$' */
8526 n = itbl_get_field (&p); /* n is name */
8528 /* See if this is a register defined in an
8529 itbl entry. */
8530 if (itbl_get_reg_val (n, &r))
8532 /* Get_field advances to the start of
8533 the next field, so we need to back
8534 rack to the end of the last field. */
8535 if (p)
8536 s = p - 1;
8537 else
8538 s = strchr (s, '\0');
8539 regno = r;
8541 else
8542 goto notreg;
8544 else
8545 goto notreg;
8547 if (regno == AT
8548 && ! mips_opts.noat
8549 && *args != 'E'
8550 && *args != 'G'
8551 && *args != 'K')
8552 as_warn (_("Used $at without \".set noat\""));
8553 c = *args;
8554 if (*s == ' ')
8555 ++s;
8556 if (args[1] != *s)
8558 if (c == 'r' || c == 'v' || c == 'w')
8560 regno = lastregno;
8561 s = s_reset;
8562 ++args;
8565 /* 'z' only matches $0. */
8566 if (c == 'z' && regno != 0)
8567 break;
8569 /* Now that we have assembled one operand, we use the args string
8570 * to figure out where it goes in the instruction. */
8571 switch (c)
8573 case 'r':
8574 case 's':
8575 case 'v':
8576 case 'b':
8577 INSERT_OPERAND (RS, *ip, regno);
8578 break;
8579 case 'd':
8580 case 'G':
8581 case 'K':
8582 INSERT_OPERAND (RD, *ip, regno);
8583 break;
8584 case 'U':
8585 INSERT_OPERAND (RD, *ip, regno);
8586 INSERT_OPERAND (RT, *ip, regno);
8587 break;
8588 case 'w':
8589 case 't':
8590 case 'E':
8591 INSERT_OPERAND (RT, *ip, regno);
8592 break;
8593 case 'x':
8594 /* This case exists because on the r3000 trunc
8595 expands into a macro which requires a gp
8596 register. On the r6000 or r4000 it is
8597 assembled into a single instruction which
8598 ignores the register. Thus the insn version
8599 is MIPS_ISA2 and uses 'x', and the macro
8600 version is MIPS_ISA1 and uses 't'. */
8601 break;
8602 case 'z':
8603 /* This case is for the div instruction, which
8604 acts differently if the destination argument
8605 is $0. This only matches $0, and is checked
8606 outside the switch. */
8607 break;
8608 case 'D':
8609 /* Itbl operand; not yet implemented. FIXME ?? */
8610 break;
8611 /* What about all other operands like 'i', which
8612 can be specified in the opcode table? */
8614 lastregno = regno;
8615 continue;
8617 notreg:
8618 switch (*args++)
8620 case 'r':
8621 case 'v':
8622 INSERT_OPERAND (RS, *ip, lastregno);
8623 continue;
8624 case 'w':
8625 INSERT_OPERAND (RT, *ip, lastregno);
8626 continue;
8628 break;
8630 case 'O': /* MDMX alignment immediate constant. */
8631 my_getExpression (&imm_expr, s);
8632 check_absolute_expr (ip, &imm_expr);
8633 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8634 as_warn ("Improper align amount (%ld), using low bits",
8635 (long) imm_expr.X_add_number);
8636 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
8637 imm_expr.X_op = O_absent;
8638 s = expr_end;
8639 continue;
8641 case 'Q': /* MDMX vector, element sel, or const. */
8642 if (s[0] != '$')
8644 /* MDMX Immediate. */
8645 my_getExpression (&imm_expr, s);
8646 check_absolute_expr (ip, &imm_expr);
8647 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8648 as_warn (_("Invalid MDMX Immediate (%ld)"),
8649 (long) imm_expr.X_add_number);
8650 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
8651 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8652 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8653 else
8654 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8655 imm_expr.X_op = O_absent;
8656 s = expr_end;
8657 continue;
8659 /* Not MDMX Immediate. Fall through. */
8660 case 'X': /* MDMX destination register. */
8661 case 'Y': /* MDMX source register. */
8662 case 'Z': /* MDMX target register. */
8663 is_mdmx = 1;
8664 case 'D': /* floating point destination register */
8665 case 'S': /* floating point source register */
8666 case 'T': /* floating point target register */
8667 case 'R': /* floating point source register */
8668 case 'V':
8669 case 'W':
8670 s_reset = s;
8671 /* Accept $fN for FP and MDMX register numbers, and in
8672 addition accept $vN for MDMX register numbers. */
8673 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8674 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8675 && ISDIGIT (s[2])))
8677 s += 2;
8678 regno = 0;
8681 regno *= 10;
8682 regno += *s - '0';
8683 ++s;
8685 while (ISDIGIT (*s));
8687 if (regno > 31)
8688 as_bad (_("Invalid float register number (%d)"), regno);
8690 if ((regno & 1) != 0
8691 && HAVE_32BIT_FPRS
8692 && ! (strcmp (str, "mtc1") == 0
8693 || strcmp (str, "mfc1") == 0
8694 || strcmp (str, "lwc1") == 0
8695 || strcmp (str, "swc1") == 0
8696 || strcmp (str, "l.s") == 0
8697 || strcmp (str, "s.s") == 0))
8698 as_warn (_("Float register should be even, was %d"),
8699 regno);
8701 c = *args;
8702 if (*s == ' ')
8703 ++s;
8704 if (args[1] != *s)
8706 if (c == 'V' || c == 'W')
8708 regno = lastregno;
8709 s = s_reset;
8710 ++args;
8713 switch (c)
8715 case 'D':
8716 case 'X':
8717 INSERT_OPERAND (FD, *ip, regno);
8718 break;
8719 case 'V':
8720 case 'S':
8721 case 'Y':
8722 INSERT_OPERAND (FS, *ip, regno);
8723 break;
8724 case 'Q':
8725 /* This is like 'Z', but also needs to fix the MDMX
8726 vector/scalar select bits. Note that the
8727 scalar immediate case is handled above. */
8728 if (*s == '[')
8730 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8731 int max_el = (is_qh ? 3 : 7);
8732 s++;
8733 my_getExpression(&imm_expr, s);
8734 check_absolute_expr (ip, &imm_expr);
8735 s = expr_end;
8736 if (imm_expr.X_add_number > max_el)
8737 as_bad(_("Bad element selector %ld"),
8738 (long) imm_expr.X_add_number);
8739 imm_expr.X_add_number &= max_el;
8740 ip->insn_opcode |= (imm_expr.X_add_number
8741 << (OP_SH_VSEL +
8742 (is_qh ? 2 : 1)));
8743 imm_expr.X_op = O_absent;
8744 if (*s != ']')
8745 as_warn(_("Expecting ']' found '%s'"), s);
8746 else
8747 s++;
8749 else
8751 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8752 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8753 << OP_SH_VSEL);
8754 else
8755 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8756 OP_SH_VSEL);
8758 /* Fall through */
8759 case 'W':
8760 case 'T':
8761 case 'Z':
8762 INSERT_OPERAND (FT, *ip, regno);
8763 break;
8764 case 'R':
8765 INSERT_OPERAND (FR, *ip, regno);
8766 break;
8768 lastregno = regno;
8769 continue;
8772 switch (*args++)
8774 case 'V':
8775 INSERT_OPERAND (FS, *ip, lastregno);
8776 continue;
8777 case 'W':
8778 INSERT_OPERAND (FT, *ip, lastregno);
8779 continue;
8781 break;
8783 case 'I':
8784 my_getExpression (&imm_expr, s);
8785 if (imm_expr.X_op != O_big
8786 && imm_expr.X_op != O_constant)
8787 insn_error = _("absolute expression required");
8788 if (HAVE_32BIT_GPRS)
8789 normalize_constant_expr (&imm_expr);
8790 s = expr_end;
8791 continue;
8793 case 'A':
8794 my_getExpression (&offset_expr, s);
8795 normalize_address_expr (&offset_expr);
8796 *imm_reloc = BFD_RELOC_32;
8797 s = expr_end;
8798 continue;
8800 case 'F':
8801 case 'L':
8802 case 'f':
8803 case 'l':
8805 int f64;
8806 int using_gprs;
8807 char *save_in;
8808 char *err;
8809 unsigned char temp[8];
8810 int len;
8811 unsigned int length;
8812 segT seg;
8813 subsegT subseg;
8814 char *p;
8816 /* These only appear as the last operand in an
8817 instruction, and every instruction that accepts
8818 them in any variant accepts them in all variants.
8819 This means we don't have to worry about backing out
8820 any changes if the instruction does not match.
8822 The difference between them is the size of the
8823 floating point constant and where it goes. For 'F'
8824 and 'L' the constant is 64 bits; for 'f' and 'l' it
8825 is 32 bits. Where the constant is placed is based
8826 on how the MIPS assembler does things:
8827 F -- .rdata
8828 L -- .lit8
8829 f -- immediate value
8830 l -- .lit4
8832 The .lit4 and .lit8 sections are only used if
8833 permitted by the -G argument.
8835 The code below needs to know whether the target register
8836 is 32 or 64 bits wide. It relies on the fact 'f' and
8837 'F' are used with GPR-based instructions and 'l' and
8838 'L' are used with FPR-based instructions. */
8840 f64 = *args == 'F' || *args == 'L';
8841 using_gprs = *args == 'F' || *args == 'f';
8843 save_in = input_line_pointer;
8844 input_line_pointer = s;
8845 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8846 length = len;
8847 s = input_line_pointer;
8848 input_line_pointer = save_in;
8849 if (err != NULL && *err != '\0')
8851 as_bad (_("Bad floating point constant: %s"), err);
8852 memset (temp, '\0', sizeof temp);
8853 length = f64 ? 8 : 4;
8856 assert (length == (unsigned) (f64 ? 8 : 4));
8858 if (*args == 'f'
8859 || (*args == 'l'
8860 && (g_switch_value < 4
8861 || (temp[0] == 0 && temp[1] == 0)
8862 || (temp[2] == 0 && temp[3] == 0))))
8864 imm_expr.X_op = O_constant;
8865 if (! target_big_endian)
8866 imm_expr.X_add_number = bfd_getl32 (temp);
8867 else
8868 imm_expr.X_add_number = bfd_getb32 (temp);
8870 else if (length > 4
8871 && ! mips_disable_float_construction
8872 /* Constants can only be constructed in GPRs and
8873 copied to FPRs if the GPRs are at least as wide
8874 as the FPRs. Force the constant into memory if
8875 we are using 64-bit FPRs but the GPRs are only
8876 32 bits wide. */
8877 && (using_gprs
8878 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8879 && ((temp[0] == 0 && temp[1] == 0)
8880 || (temp[2] == 0 && temp[3] == 0))
8881 && ((temp[4] == 0 && temp[5] == 0)
8882 || (temp[6] == 0 && temp[7] == 0)))
8884 /* The value is simple enough to load with a couple of
8885 instructions. If using 32-bit registers, set
8886 imm_expr to the high order 32 bits and offset_expr to
8887 the low order 32 bits. Otherwise, set imm_expr to
8888 the entire 64 bit constant. */
8889 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8891 imm_expr.X_op = O_constant;
8892 offset_expr.X_op = O_constant;
8893 if (! target_big_endian)
8895 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8896 offset_expr.X_add_number = bfd_getl32 (temp);
8898 else
8900 imm_expr.X_add_number = bfd_getb32 (temp);
8901 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8903 if (offset_expr.X_add_number == 0)
8904 offset_expr.X_op = O_absent;
8906 else if (sizeof (imm_expr.X_add_number) > 4)
8908 imm_expr.X_op = O_constant;
8909 if (! target_big_endian)
8910 imm_expr.X_add_number = bfd_getl64 (temp);
8911 else
8912 imm_expr.X_add_number = bfd_getb64 (temp);
8914 else
8916 imm_expr.X_op = O_big;
8917 imm_expr.X_add_number = 4;
8918 if (! target_big_endian)
8920 generic_bignum[0] = bfd_getl16 (temp);
8921 generic_bignum[1] = bfd_getl16 (temp + 2);
8922 generic_bignum[2] = bfd_getl16 (temp + 4);
8923 generic_bignum[3] = bfd_getl16 (temp + 6);
8925 else
8927 generic_bignum[0] = bfd_getb16 (temp + 6);
8928 generic_bignum[1] = bfd_getb16 (temp + 4);
8929 generic_bignum[2] = bfd_getb16 (temp + 2);
8930 generic_bignum[3] = bfd_getb16 (temp);
8934 else
8936 const char *newname;
8937 segT new_seg;
8939 /* Switch to the right section. */
8940 seg = now_seg;
8941 subseg = now_subseg;
8942 switch (*args)
8944 default: /* unused default case avoids warnings. */
8945 case 'L':
8946 newname = RDATA_SECTION_NAME;
8947 if (g_switch_value >= 8)
8948 newname = ".lit8";
8949 break;
8950 case 'F':
8951 newname = RDATA_SECTION_NAME;
8952 break;
8953 case 'l':
8954 assert (g_switch_value >= 4);
8955 newname = ".lit4";
8956 break;
8958 new_seg = subseg_new (newname, (subsegT) 0);
8959 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8960 bfd_set_section_flags (stdoutput, new_seg,
8961 (SEC_ALLOC
8962 | SEC_LOAD
8963 | SEC_READONLY
8964 | SEC_DATA));
8965 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8966 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8967 && strcmp (TARGET_OS, "elf") != 0)
8968 record_alignment (new_seg, 4);
8969 else
8970 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8971 if (seg == now_seg)
8972 as_bad (_("Can't use floating point insn in this section"));
8974 /* Set the argument to the current address in the
8975 section. */
8976 offset_expr.X_op = O_symbol;
8977 offset_expr.X_add_symbol =
8978 symbol_new ("L0\001", now_seg,
8979 (valueT) frag_now_fix (), frag_now);
8980 offset_expr.X_add_number = 0;
8982 /* Put the floating point number into the section. */
8983 p = frag_more ((int) length);
8984 memcpy (p, temp, length);
8986 /* Switch back to the original section. */
8987 subseg_set (seg, subseg);
8990 continue;
8992 case 'i': /* 16 bit unsigned immediate */
8993 case 'j': /* 16 bit signed immediate */
8994 *imm_reloc = BFD_RELOC_LO16;
8995 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
8997 int more;
8998 offsetT minval, maxval;
9000 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9001 && strcmp (insn->name, insn[1].name) == 0);
9003 /* If the expression was written as an unsigned number,
9004 only treat it as signed if there are no more
9005 alternatives. */
9006 if (more
9007 && *args == 'j'
9008 && sizeof (imm_expr.X_add_number) <= 4
9009 && imm_expr.X_op == O_constant
9010 && imm_expr.X_add_number < 0
9011 && imm_expr.X_unsigned
9012 && HAVE_64BIT_GPRS)
9013 break;
9015 /* For compatibility with older assemblers, we accept
9016 0x8000-0xffff as signed 16-bit numbers when only
9017 signed numbers are allowed. */
9018 if (*args == 'i')
9019 minval = 0, maxval = 0xffff;
9020 else if (more)
9021 minval = -0x8000, maxval = 0x7fff;
9022 else
9023 minval = -0x8000, maxval = 0xffff;
9025 if (imm_expr.X_op != O_constant
9026 || imm_expr.X_add_number < minval
9027 || imm_expr.X_add_number > maxval)
9029 if (more)
9030 break;
9031 if (imm_expr.X_op == O_constant
9032 || imm_expr.X_op == O_big)
9033 as_bad (_("expression out of range"));
9036 s = expr_end;
9037 continue;
9039 case 'o': /* 16 bit offset */
9040 /* Check whether there is only a single bracketed expression
9041 left. If so, it must be the base register and the
9042 constant must be zero. */
9043 if (*s == '(' && strchr (s + 1, '(') == 0)
9045 offset_expr.X_op = O_constant;
9046 offset_expr.X_add_number = 0;
9047 continue;
9050 /* If this value won't fit into a 16 bit offset, then go
9051 find a macro that will generate the 32 bit offset
9052 code pattern. */
9053 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9054 && (offset_expr.X_op != O_constant
9055 || offset_expr.X_add_number >= 0x8000
9056 || offset_expr.X_add_number < -0x8000))
9057 break;
9059 s = expr_end;
9060 continue;
9062 case 'p': /* pc relative offset */
9063 *offset_reloc = BFD_RELOC_16_PCREL_S2;
9064 my_getExpression (&offset_expr, s);
9065 s = expr_end;
9066 continue;
9068 case 'u': /* upper 16 bits */
9069 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9070 && imm_expr.X_op == O_constant
9071 && (imm_expr.X_add_number < 0
9072 || imm_expr.X_add_number >= 0x10000))
9073 as_bad (_("lui expression not in range 0..65535"));
9074 s = expr_end;
9075 continue;
9077 case 'a': /* 26 bit address */
9078 my_getExpression (&offset_expr, s);
9079 s = expr_end;
9080 *offset_reloc = BFD_RELOC_MIPS_JMP;
9081 continue;
9083 case 'N': /* 3 bit branch condition code */
9084 case 'M': /* 3 bit compare condition code */
9085 if (strncmp (s, "$fcc", 4) != 0)
9086 break;
9087 s += 4;
9088 regno = 0;
9091 regno *= 10;
9092 regno += *s - '0';
9093 ++s;
9095 while (ISDIGIT (*s));
9096 if (regno > 7)
9097 as_bad (_("Invalid condition code register $fcc%d"), regno);
9098 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9099 || strcmp(str + strlen(str) - 5, "any2f") == 0
9100 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9101 && (regno & 1) != 0)
9102 as_warn(_("Condition code register should be even for %s, was %d"),
9103 str, regno);
9104 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9105 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9106 && (regno & 3) != 0)
9107 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9108 str, regno);
9109 if (*args == 'N')
9110 INSERT_OPERAND (BCC, *ip, regno);
9111 else
9112 INSERT_OPERAND (CCC, *ip, regno);
9113 continue;
9115 case 'H':
9116 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9117 s += 2;
9118 if (ISDIGIT (*s))
9120 c = 0;
9123 c *= 10;
9124 c += *s - '0';
9125 ++s;
9127 while (ISDIGIT (*s));
9129 else
9130 c = 8; /* Invalid sel value. */
9132 if (c > 7)
9133 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9134 ip->insn_opcode |= c;
9135 continue;
9137 case 'e':
9138 /* Must be at least one digit. */
9139 my_getExpression (&imm_expr, s);
9140 check_absolute_expr (ip, &imm_expr);
9142 if ((unsigned long) imm_expr.X_add_number
9143 > (unsigned long) OP_MASK_VECBYTE)
9145 as_bad (_("bad byte vector index (%ld)"),
9146 (long) imm_expr.X_add_number);
9147 imm_expr.X_add_number = 0;
9150 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
9151 imm_expr.X_op = O_absent;
9152 s = expr_end;
9153 continue;
9155 case '%':
9156 my_getExpression (&imm_expr, s);
9157 check_absolute_expr (ip, &imm_expr);
9159 if ((unsigned long) imm_expr.X_add_number
9160 > (unsigned long) OP_MASK_VECALIGN)
9162 as_bad (_("bad byte vector index (%ld)"),
9163 (long) imm_expr.X_add_number);
9164 imm_expr.X_add_number = 0;
9167 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
9168 imm_expr.X_op = O_absent;
9169 s = expr_end;
9170 continue;
9172 default:
9173 as_bad (_("bad char = '%c'\n"), *args);
9174 internalError ();
9176 break;
9178 /* Args don't match. */
9179 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9180 !strcmp (insn->name, insn[1].name))
9182 ++insn;
9183 s = argsStart;
9184 insn_error = _("illegal operands");
9185 continue;
9187 if (save_c)
9188 *(--s) = save_c;
9189 insn_error = _("illegal operands");
9190 return;
9194 /* This routine assembles an instruction into its binary format when
9195 assembling for the mips16. As a side effect, it sets one of the
9196 global variables imm_reloc or offset_reloc to the type of
9197 relocation to do if one of the operands is an address expression.
9198 It also sets mips16_small and mips16_ext if the user explicitly
9199 requested a small or extended instruction. */
9201 static void
9202 mips16_ip (char *str, struct mips_cl_insn *ip)
9204 char *s;
9205 const char *args;
9206 struct mips_opcode *insn;
9207 char *argsstart;
9208 unsigned int regno;
9209 unsigned int lastregno = 0;
9210 char *s_reset;
9211 size_t i;
9213 insn_error = NULL;
9215 mips16_small = FALSE;
9216 mips16_ext = FALSE;
9218 for (s = str; ISLOWER (*s); ++s)
9220 switch (*s)
9222 case '\0':
9223 break;
9225 case ' ':
9226 *s++ = '\0';
9227 break;
9229 case '.':
9230 if (s[1] == 't' && s[2] == ' ')
9232 *s = '\0';
9233 mips16_small = TRUE;
9234 s += 3;
9235 break;
9237 else if (s[1] == 'e' && s[2] == ' ')
9239 *s = '\0';
9240 mips16_ext = TRUE;
9241 s += 3;
9242 break;
9244 /* Fall through. */
9245 default:
9246 insn_error = _("unknown opcode");
9247 return;
9250 if (mips_opts.noautoextend && ! mips16_ext)
9251 mips16_small = TRUE;
9253 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9255 insn_error = _("unrecognized opcode");
9256 return;
9259 argsstart = s;
9260 for (;;)
9262 assert (strcmp (insn->name, str) == 0);
9264 create_insn (ip, insn);
9265 imm_expr.X_op = O_absent;
9266 imm_reloc[0] = BFD_RELOC_UNUSED;
9267 imm_reloc[1] = BFD_RELOC_UNUSED;
9268 imm_reloc[2] = BFD_RELOC_UNUSED;
9269 imm2_expr.X_op = O_absent;
9270 offset_expr.X_op = O_absent;
9271 offset_reloc[0] = BFD_RELOC_UNUSED;
9272 offset_reloc[1] = BFD_RELOC_UNUSED;
9273 offset_reloc[2] = BFD_RELOC_UNUSED;
9274 for (args = insn->args; 1; ++args)
9276 int c;
9278 if (*s == ' ')
9279 ++s;
9281 /* In this switch statement we call break if we did not find
9282 a match, continue if we did find a match, or return if we
9283 are done. */
9285 c = *args;
9286 switch (c)
9288 case '\0':
9289 if (*s == '\0')
9291 /* Stuff the immediate value in now, if we can. */
9292 if (imm_expr.X_op == O_constant
9293 && *imm_reloc > BFD_RELOC_UNUSED
9294 && insn->pinfo != INSN_MACRO)
9296 valueT tmp;
9298 switch (*offset_reloc)
9300 case BFD_RELOC_MIPS16_HI16_S:
9301 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9302 break;
9304 case BFD_RELOC_MIPS16_HI16:
9305 tmp = imm_expr.X_add_number >> 16;
9306 break;
9308 case BFD_RELOC_MIPS16_LO16:
9309 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9310 - 0x8000;
9311 break;
9313 case BFD_RELOC_UNUSED:
9314 tmp = imm_expr.X_add_number;
9315 break;
9317 default:
9318 internalError ();
9320 *offset_reloc = BFD_RELOC_UNUSED;
9322 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9323 tmp, TRUE, mips16_small,
9324 mips16_ext, &ip->insn_opcode,
9325 &ip->use_extend, &ip->extend);
9326 imm_expr.X_op = O_absent;
9327 *imm_reloc = BFD_RELOC_UNUSED;
9330 return;
9332 break;
9334 case ',':
9335 if (*s++ == c)
9336 continue;
9337 s--;
9338 switch (*++args)
9340 case 'v':
9341 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9342 continue;
9343 case 'w':
9344 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9345 continue;
9347 break;
9349 case '(':
9350 case ')':
9351 if (*s++ == c)
9352 continue;
9353 break;
9355 case 'v':
9356 case 'w':
9357 if (s[0] != '$')
9359 if (c == 'v')
9360 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9361 else
9362 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9363 ++args;
9364 continue;
9366 /* Fall through. */
9367 case 'x':
9368 case 'y':
9369 case 'z':
9370 case 'Z':
9371 case '0':
9372 case 'S':
9373 case 'R':
9374 case 'X':
9375 case 'Y':
9376 if (s[0] != '$')
9377 break;
9378 s_reset = s;
9379 if (ISDIGIT (s[1]))
9381 ++s;
9382 regno = 0;
9385 regno *= 10;
9386 regno += *s - '0';
9387 ++s;
9389 while (ISDIGIT (*s));
9390 if (regno > 31)
9392 as_bad (_("invalid register number (%d)"), regno);
9393 regno = 2;
9396 else
9398 if (s[1] == 'r' && s[2] == 'a')
9400 s += 3;
9401 regno = RA;
9403 else if (s[1] == 'f' && s[2] == 'p')
9405 s += 3;
9406 regno = FP;
9408 else if (s[1] == 's' && s[2] == 'p')
9410 s += 3;
9411 regno = SP;
9413 else if (s[1] == 'g' && s[2] == 'p')
9415 s += 3;
9416 regno = GP;
9418 else if (s[1] == 'a' && s[2] == 't')
9420 s += 3;
9421 regno = AT;
9423 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9425 s += 4;
9426 regno = KT0;
9428 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9430 s += 4;
9431 regno = KT1;
9433 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9435 s += 5;
9436 regno = ZERO;
9438 else
9439 break;
9442 if (*s == ' ')
9443 ++s;
9444 if (args[1] != *s)
9446 if (c == 'v' || c == 'w')
9448 regno = mips16_to_32_reg_map[lastregno];
9449 s = s_reset;
9450 ++args;
9454 switch (c)
9456 case 'x':
9457 case 'y':
9458 case 'z':
9459 case 'v':
9460 case 'w':
9461 case 'Z':
9462 regno = mips32_to_16_reg_map[regno];
9463 break;
9465 case '0':
9466 if (regno != 0)
9467 regno = ILLEGAL_REG;
9468 break;
9470 case 'S':
9471 if (regno != SP)
9472 regno = ILLEGAL_REG;
9473 break;
9475 case 'R':
9476 if (regno != RA)
9477 regno = ILLEGAL_REG;
9478 break;
9480 case 'X':
9481 case 'Y':
9482 if (regno == AT && ! mips_opts.noat)
9483 as_warn (_("used $at without \".set noat\""));
9484 break;
9486 default:
9487 internalError ();
9490 if (regno == ILLEGAL_REG)
9491 break;
9493 switch (c)
9495 case 'x':
9496 case 'v':
9497 MIPS16_INSERT_OPERAND (RX, *ip, regno);
9498 break;
9499 case 'y':
9500 case 'w':
9501 MIPS16_INSERT_OPERAND (RY, *ip, regno);
9502 break;
9503 case 'z':
9504 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
9505 break;
9506 case 'Z':
9507 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
9508 case '0':
9509 case 'S':
9510 case 'R':
9511 break;
9512 case 'X':
9513 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
9514 break;
9515 case 'Y':
9516 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9517 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
9518 break;
9519 default:
9520 internalError ();
9523 lastregno = regno;
9524 continue;
9526 case 'P':
9527 if (strncmp (s, "$pc", 3) == 0)
9529 s += 3;
9530 continue;
9532 break;
9534 case '5':
9535 case 'H':
9536 case 'W':
9537 case 'D':
9538 case 'j':
9539 case 'V':
9540 case 'C':
9541 case 'U':
9542 case 'k':
9543 case 'K':
9544 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9545 if (i > 0)
9547 if (imm_expr.X_op != O_constant)
9549 mips16_ext = TRUE;
9550 ip->use_extend = TRUE;
9551 ip->extend = 0;
9553 else
9555 /* We need to relax this instruction. */
9556 *offset_reloc = *imm_reloc;
9557 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9559 s = expr_end;
9560 continue;
9562 *imm_reloc = BFD_RELOC_UNUSED;
9563 /* Fall through. */
9564 case '<':
9565 case '>':
9566 case '[':
9567 case ']':
9568 case '4':
9569 case '8':
9570 my_getExpression (&imm_expr, s);
9571 if (imm_expr.X_op == O_register)
9573 /* What we thought was an expression turned out to
9574 be a register. */
9576 if (s[0] == '(' && args[1] == '(')
9578 /* It looks like the expression was omitted
9579 before a register indirection, which means
9580 that the expression is implicitly zero. We
9581 still set up imm_expr, so that we handle
9582 explicit extensions correctly. */
9583 imm_expr.X_op = O_constant;
9584 imm_expr.X_add_number = 0;
9585 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9586 continue;
9589 break;
9592 /* We need to relax this instruction. */
9593 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9594 s = expr_end;
9595 continue;
9597 case 'p':
9598 case 'q':
9599 case 'A':
9600 case 'B':
9601 case 'E':
9602 /* We use offset_reloc rather than imm_reloc for the PC
9603 relative operands. This lets macros with both
9604 immediate and address operands work correctly. */
9605 my_getExpression (&offset_expr, s);
9607 if (offset_expr.X_op == O_register)
9608 break;
9610 /* We need to relax this instruction. */
9611 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9612 s = expr_end;
9613 continue;
9615 case '6': /* break code */
9616 my_getExpression (&imm_expr, s);
9617 check_absolute_expr (ip, &imm_expr);
9618 if ((unsigned long) imm_expr.X_add_number > 63)
9619 as_warn (_("Invalid value for `%s' (%lu)"),
9620 ip->insn_mo->name,
9621 (unsigned long) imm_expr.X_add_number);
9622 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
9623 imm_expr.X_op = O_absent;
9624 s = expr_end;
9625 continue;
9627 case 'a': /* 26 bit address */
9628 my_getExpression (&offset_expr, s);
9629 s = expr_end;
9630 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9631 ip->insn_opcode <<= 16;
9632 continue;
9634 case 'l': /* register list for entry macro */
9635 case 'L': /* register list for exit macro */
9637 int mask;
9639 if (c == 'l')
9640 mask = 0;
9641 else
9642 mask = 7 << 3;
9643 while (*s != '\0')
9645 int freg, reg1, reg2;
9647 while (*s == ' ' || *s == ',')
9648 ++s;
9649 if (*s != '$')
9651 as_bad (_("can't parse register list"));
9652 break;
9654 ++s;
9655 if (*s != 'f')
9656 freg = 0;
9657 else
9659 freg = 1;
9660 ++s;
9662 reg1 = 0;
9663 while (ISDIGIT (*s))
9665 reg1 *= 10;
9666 reg1 += *s - '0';
9667 ++s;
9669 if (*s == ' ')
9670 ++s;
9671 if (*s != '-')
9672 reg2 = reg1;
9673 else
9675 ++s;
9676 if (*s != '$')
9677 break;
9678 ++s;
9679 if (freg)
9681 if (*s == 'f')
9682 ++s;
9683 else
9685 as_bad (_("invalid register list"));
9686 break;
9689 reg2 = 0;
9690 while (ISDIGIT (*s))
9692 reg2 *= 10;
9693 reg2 += *s - '0';
9694 ++s;
9697 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9699 mask &= ~ (7 << 3);
9700 mask |= 5 << 3;
9702 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9704 mask &= ~ (7 << 3);
9705 mask |= 6 << 3;
9707 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9708 mask |= (reg2 - 3) << 3;
9709 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9710 mask |= (reg2 - 15) << 1;
9711 else if (reg1 == RA && reg2 == RA)
9712 mask |= 1;
9713 else
9715 as_bad (_("invalid register list"));
9716 break;
9719 /* The mask is filled in in the opcode table for the
9720 benefit of the disassembler. We remove it before
9721 applying the actual mask. */
9722 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9723 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9725 continue;
9727 case 'e': /* extend code */
9728 my_getExpression (&imm_expr, s);
9729 check_absolute_expr (ip, &imm_expr);
9730 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9732 as_warn (_("Invalid value for `%s' (%lu)"),
9733 ip->insn_mo->name,
9734 (unsigned long) imm_expr.X_add_number);
9735 imm_expr.X_add_number &= 0x7ff;
9737 ip->insn_opcode |= imm_expr.X_add_number;
9738 imm_expr.X_op = O_absent;
9739 s = expr_end;
9740 continue;
9742 default:
9743 internalError ();
9745 break;
9748 /* Args don't match. */
9749 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9750 strcmp (insn->name, insn[1].name) == 0)
9752 ++insn;
9753 s = argsstart;
9754 continue;
9757 insn_error = _("illegal operands");
9759 return;
9763 /* This structure holds information we know about a mips16 immediate
9764 argument type. */
9766 struct mips16_immed_operand
9768 /* The type code used in the argument string in the opcode table. */
9769 int type;
9770 /* The number of bits in the short form of the opcode. */
9771 int nbits;
9772 /* The number of bits in the extended form of the opcode. */
9773 int extbits;
9774 /* The amount by which the short form is shifted when it is used;
9775 for example, the sw instruction has a shift count of 2. */
9776 int shift;
9777 /* The amount by which the short form is shifted when it is stored
9778 into the instruction code. */
9779 int op_shift;
9780 /* Non-zero if the short form is unsigned. */
9781 int unsp;
9782 /* Non-zero if the extended form is unsigned. */
9783 int extu;
9784 /* Non-zero if the value is PC relative. */
9785 int pcrel;
9788 /* The mips16 immediate operand types. */
9790 static const struct mips16_immed_operand mips16_immed_operands[] =
9792 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9793 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9794 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9795 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9796 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9797 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9798 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9799 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9800 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9801 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9802 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9803 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9804 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9805 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9806 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9807 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9808 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9809 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9810 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9811 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9812 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9815 #define MIPS16_NUM_IMMED \
9816 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9818 /* Handle a mips16 instruction with an immediate value. This or's the
9819 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9820 whether an extended value is needed; if one is needed, it sets
9821 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9822 If SMALL is true, an unextended opcode was explicitly requested.
9823 If EXT is true, an extended opcode was explicitly requested. If
9824 WARN is true, warn if EXT does not match reality. */
9826 static void
9827 mips16_immed (char *file, unsigned int line, int type, offsetT val,
9828 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9829 unsigned long *insn, bfd_boolean *use_extend,
9830 unsigned short *extend)
9832 register const struct mips16_immed_operand *op;
9833 int mintiny, maxtiny;
9834 bfd_boolean needext;
9836 op = mips16_immed_operands;
9837 while (op->type != type)
9839 ++op;
9840 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9843 if (op->unsp)
9845 if (type == '<' || type == '>' || type == '[' || type == ']')
9847 mintiny = 1;
9848 maxtiny = 1 << op->nbits;
9850 else
9852 mintiny = 0;
9853 maxtiny = (1 << op->nbits) - 1;
9856 else
9858 mintiny = - (1 << (op->nbits - 1));
9859 maxtiny = (1 << (op->nbits - 1)) - 1;
9862 /* Branch offsets have an implicit 0 in the lowest bit. */
9863 if (type == 'p' || type == 'q')
9864 val /= 2;
9866 if ((val & ((1 << op->shift) - 1)) != 0
9867 || val < (mintiny << op->shift)
9868 || val > (maxtiny << op->shift))
9869 needext = TRUE;
9870 else
9871 needext = FALSE;
9873 if (warn && ext && ! needext)
9874 as_warn_where (file, line,
9875 _("extended operand requested but not required"));
9876 if (small && needext)
9877 as_bad_where (file, line, _("invalid unextended operand value"));
9879 if (small || (! ext && ! needext))
9881 int insnval;
9883 *use_extend = FALSE;
9884 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9885 insnval <<= op->op_shift;
9886 *insn |= insnval;
9888 else
9890 long minext, maxext;
9891 int extval;
9893 if (op->extu)
9895 minext = 0;
9896 maxext = (1 << op->extbits) - 1;
9898 else
9900 minext = - (1 << (op->extbits - 1));
9901 maxext = (1 << (op->extbits - 1)) - 1;
9903 if (val < minext || val > maxext)
9904 as_bad_where (file, line,
9905 _("operand value out of range for instruction"));
9907 *use_extend = TRUE;
9908 if (op->extbits == 16)
9910 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9911 val &= 0x1f;
9913 else if (op->extbits == 15)
9915 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9916 val &= 0xf;
9918 else
9920 extval = ((val & 0x1f) << 6) | (val & 0x20);
9921 val = 0;
9924 *extend = (unsigned short) extval;
9925 *insn |= val;
9929 struct percent_op_match
9931 const char *str;
9932 bfd_reloc_code_real_type reloc;
9935 static const struct percent_op_match mips_percent_op[] =
9937 {"%lo", BFD_RELOC_LO16},
9938 #ifdef OBJ_ELF
9939 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9940 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9941 {"%call16", BFD_RELOC_MIPS_CALL16},
9942 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9943 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9944 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9945 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9946 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9947 {"%got", BFD_RELOC_MIPS_GOT16},
9948 {"%gp_rel", BFD_RELOC_GPREL16},
9949 {"%half", BFD_RELOC_16},
9950 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9951 {"%higher", BFD_RELOC_MIPS_HIGHER},
9952 {"%neg", BFD_RELOC_MIPS_SUB},
9953 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
9954 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
9955 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
9956 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
9957 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
9958 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
9959 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
9960 #endif
9961 {"%hi", BFD_RELOC_HI16_S}
9964 static const struct percent_op_match mips16_percent_op[] =
9966 {"%lo", BFD_RELOC_MIPS16_LO16},
9967 {"%gprel", BFD_RELOC_MIPS16_GPREL},
9968 {"%hi", BFD_RELOC_MIPS16_HI16_S}
9972 /* Return true if *STR points to a relocation operator. When returning true,
9973 move *STR over the operator and store its relocation code in *RELOC.
9974 Leave both *STR and *RELOC alone when returning false. */
9976 static bfd_boolean
9977 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
9979 const struct percent_op_match *percent_op;
9980 size_t limit, i;
9982 if (mips_opts.mips16)
9984 percent_op = mips16_percent_op;
9985 limit = ARRAY_SIZE (mips16_percent_op);
9987 else
9989 percent_op = mips_percent_op;
9990 limit = ARRAY_SIZE (mips_percent_op);
9993 for (i = 0; i < limit; i++)
9994 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
9996 int len = strlen (percent_op[i].str);
9998 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
9999 continue;
10001 *str += strlen (percent_op[i].str);
10002 *reloc = percent_op[i].reloc;
10004 /* Check whether the output BFD supports this relocation.
10005 If not, issue an error and fall back on something safe. */
10006 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10008 as_bad ("relocation %s isn't supported by the current ABI",
10009 percent_op[i].str);
10010 *reloc = BFD_RELOC_UNUSED;
10012 return TRUE;
10014 return FALSE;
10018 /* Parse string STR as a 16-bit relocatable operand. Store the
10019 expression in *EP and the relocations in the array starting
10020 at RELOC. Return the number of relocation operators used.
10022 On exit, EXPR_END points to the first character after the expression. */
10024 static size_t
10025 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10026 char *str)
10028 bfd_reloc_code_real_type reversed_reloc[3];
10029 size_t reloc_index, i;
10030 int crux_depth, str_depth;
10031 char *crux;
10033 /* Search for the start of the main expression, recoding relocations
10034 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10035 of the main expression and with CRUX_DEPTH containing the number
10036 of open brackets at that point. */
10037 reloc_index = -1;
10038 str_depth = 0;
10041 reloc_index++;
10042 crux = str;
10043 crux_depth = str_depth;
10045 /* Skip over whitespace and brackets, keeping count of the number
10046 of brackets. */
10047 while (*str == ' ' || *str == '\t' || *str == '(')
10048 if (*str++ == '(')
10049 str_depth++;
10051 while (*str == '%'
10052 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10053 && parse_relocation (&str, &reversed_reloc[reloc_index]));
10055 my_getExpression (ep, crux);
10056 str = expr_end;
10058 /* Match every open bracket. */
10059 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10060 if (*str++ == ')')
10061 crux_depth--;
10063 if (crux_depth > 0)
10064 as_bad ("unclosed '('");
10066 expr_end = str;
10068 if (reloc_index != 0)
10070 prev_reloc_op_frag = frag_now;
10071 for (i = 0; i < reloc_index; i++)
10072 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10075 return reloc_index;
10078 static void
10079 my_getExpression (expressionS *ep, char *str)
10081 char *save_in;
10082 valueT val;
10084 save_in = input_line_pointer;
10085 input_line_pointer = str;
10086 expression (ep);
10087 expr_end = input_line_pointer;
10088 input_line_pointer = save_in;
10090 /* If we are in mips16 mode, and this is an expression based on `.',
10091 then we bump the value of the symbol by 1 since that is how other
10092 text symbols are handled. We don't bother to handle complex
10093 expressions, just `.' plus or minus a constant. */
10094 if (mips_opts.mips16
10095 && ep->X_op == O_symbol
10096 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10097 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10098 && symbol_get_frag (ep->X_add_symbol) == frag_now
10099 && symbol_constant_p (ep->X_add_symbol)
10100 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10101 S_SET_VALUE (ep->X_add_symbol, val + 1);
10104 /* Turn a string in input_line_pointer into a floating point constant
10105 of type TYPE, and store the appropriate bytes in *LITP. The number
10106 of LITTLENUMS emitted is stored in *SIZEP. An error message is
10107 returned, or NULL on OK. */
10109 char *
10110 md_atof (int type, char *litP, int *sizeP)
10112 int prec;
10113 LITTLENUM_TYPE words[4];
10114 char *t;
10115 int i;
10117 switch (type)
10119 case 'f':
10120 prec = 2;
10121 break;
10123 case 'd':
10124 prec = 4;
10125 break;
10127 default:
10128 *sizeP = 0;
10129 return _("bad call to md_atof");
10132 t = atof_ieee (input_line_pointer, type, words);
10133 if (t)
10134 input_line_pointer = t;
10136 *sizeP = prec * 2;
10138 if (! target_big_endian)
10140 for (i = prec - 1; i >= 0; i--)
10142 md_number_to_chars (litP, words[i], 2);
10143 litP += 2;
10146 else
10148 for (i = 0; i < prec; i++)
10150 md_number_to_chars (litP, words[i], 2);
10151 litP += 2;
10155 return NULL;
10158 void
10159 md_number_to_chars (char *buf, valueT val, int n)
10161 if (target_big_endian)
10162 number_to_chars_bigendian (buf, val, n);
10163 else
10164 number_to_chars_littleendian (buf, val, n);
10167 #ifdef OBJ_ELF
10168 static int support_64bit_objects(void)
10170 const char **list, **l;
10171 int yes;
10173 list = bfd_target_list ();
10174 for (l = list; *l != NULL; l++)
10175 #ifdef TE_TMIPS
10176 /* This is traditional mips */
10177 if (strcmp (*l, "elf64-tradbigmips") == 0
10178 || strcmp (*l, "elf64-tradlittlemips") == 0)
10179 #else
10180 if (strcmp (*l, "elf64-bigmips") == 0
10181 || strcmp (*l, "elf64-littlemips") == 0)
10182 #endif
10183 break;
10184 yes = (*l != NULL);
10185 free (list);
10186 return yes;
10188 #endif /* OBJ_ELF */
10190 const char *md_shortopts = "O::g::G:";
10192 struct option md_longopts[] =
10194 /* Options which specify architecture. */
10195 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
10196 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10197 {"march", required_argument, NULL, OPTION_MARCH},
10198 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10199 {"mtune", required_argument, NULL, OPTION_MTUNE},
10200 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10201 {"mips0", no_argument, NULL, OPTION_MIPS1},
10202 {"mips1", no_argument, NULL, OPTION_MIPS1},
10203 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10204 {"mips2", no_argument, NULL, OPTION_MIPS2},
10205 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10206 {"mips3", no_argument, NULL, OPTION_MIPS3},
10207 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10208 {"mips4", no_argument, NULL, OPTION_MIPS4},
10209 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10210 {"mips5", no_argument, NULL, OPTION_MIPS5},
10211 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10212 {"mips32", no_argument, NULL, OPTION_MIPS32},
10213 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10214 {"mips64", no_argument, NULL, OPTION_MIPS64},
10215 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10216 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10217 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10218 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
10220 /* Options which specify Application Specific Extensions (ASEs). */
10221 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
10222 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10223 {"mips16", no_argument, NULL, OPTION_MIPS16},
10224 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10225 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10226 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10227 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10228 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10229 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10230 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10231 {"mdmx", no_argument, NULL, OPTION_MDMX},
10232 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10233 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10234 #define OPTION_DSP (OPTION_ASE_BASE + 6)
10235 {"mdsp", no_argument, NULL, OPTION_DSP},
10236 #define OPTION_NO_DSP (OPTION_ASE_BASE + 7)
10237 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
10239 /* Old-style architecture options. Don't add more of these. */
10240 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 8)
10241 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10242 {"m4650", no_argument, NULL, OPTION_M4650},
10243 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10244 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10245 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10246 {"m4010", no_argument, NULL, OPTION_M4010},
10247 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10248 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10249 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10250 {"m4100", no_argument, NULL, OPTION_M4100},
10251 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10252 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10253 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10254 {"m3900", no_argument, NULL, OPTION_M3900},
10255 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10256 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10258 /* Options which enable bug fixes. */
10259 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10260 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10261 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10262 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10263 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10264 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10265 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10266 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10267 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10268 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
10269 #define OPTION_FIX_VR4130 (OPTION_FIX_BASE + 4)
10270 #define OPTION_NO_FIX_VR4130 (OPTION_FIX_BASE + 5)
10271 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
10272 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
10274 /* Miscellaneous options. */
10275 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 6)
10276 #define OPTION_TRAP (OPTION_MISC_BASE + 0)
10277 {"trap", no_argument, NULL, OPTION_TRAP},
10278 {"no-break", no_argument, NULL, OPTION_TRAP},
10279 #define OPTION_BREAK (OPTION_MISC_BASE + 1)
10280 {"break", no_argument, NULL, OPTION_BREAK},
10281 {"no-trap", no_argument, NULL, OPTION_BREAK},
10282 #define OPTION_EB (OPTION_MISC_BASE + 2)
10283 {"EB", no_argument, NULL, OPTION_EB},
10284 #define OPTION_EL (OPTION_MISC_BASE + 3)
10285 {"EL", no_argument, NULL, OPTION_EL},
10286 #define OPTION_FP32 (OPTION_MISC_BASE + 4)
10287 {"mfp32", no_argument, NULL, OPTION_FP32},
10288 #define OPTION_GP32 (OPTION_MISC_BASE + 5)
10289 {"mgp32", no_argument, NULL, OPTION_GP32},
10290 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
10291 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10292 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10293 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10294 #define OPTION_FP64 (OPTION_MISC_BASE + 8)
10295 {"mfp64", no_argument, NULL, OPTION_FP64},
10296 #define OPTION_GP64 (OPTION_MISC_BASE + 9)
10297 {"mgp64", no_argument, NULL, OPTION_GP64},
10298 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10299 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10300 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10301 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10302 #define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10303 #define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10304 {"mshared", no_argument, NULL, OPTION_MSHARED},
10305 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
10306 #define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10307 #define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10308 {"msym32", no_argument, NULL, OPTION_MSYM32},
10309 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
10311 /* ELF-specific options. */
10312 #ifdef OBJ_ELF
10313 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
10314 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10315 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10316 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10317 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10318 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10319 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10320 {"xgot", no_argument, NULL, OPTION_XGOT},
10321 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10322 {"mabi", required_argument, NULL, OPTION_MABI},
10323 #define OPTION_32 (OPTION_ELF_BASE + 4)
10324 {"32", no_argument, NULL, OPTION_32},
10325 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10326 {"n32", no_argument, NULL, OPTION_N32},
10327 #define OPTION_64 (OPTION_ELF_BASE + 6)
10328 {"64", no_argument, NULL, OPTION_64},
10329 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10330 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10331 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10332 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10333 #define OPTION_PDR (OPTION_ELF_BASE + 9)
10334 {"mpdr", no_argument, NULL, OPTION_PDR},
10335 #define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10336 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10337 #endif /* OBJ_ELF */
10339 {NULL, no_argument, NULL, 0}
10341 size_t md_longopts_size = sizeof (md_longopts);
10343 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10344 NEW_VALUE. Warn if another value was already specified. Note:
10345 we have to defer parsing the -march and -mtune arguments in order
10346 to handle 'from-abi' correctly, since the ABI might be specified
10347 in a later argument. */
10349 static void
10350 mips_set_option_string (const char **string_ptr, const char *new_value)
10352 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10353 as_warn (_("A different %s was already specified, is now %s"),
10354 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10355 new_value);
10357 *string_ptr = new_value;
10361 md_parse_option (int c, char *arg)
10363 switch (c)
10365 case OPTION_CONSTRUCT_FLOATS:
10366 mips_disable_float_construction = 0;
10367 break;
10369 case OPTION_NO_CONSTRUCT_FLOATS:
10370 mips_disable_float_construction = 1;
10371 break;
10373 case OPTION_TRAP:
10374 mips_trap = 1;
10375 break;
10377 case OPTION_BREAK:
10378 mips_trap = 0;
10379 break;
10381 case OPTION_EB:
10382 target_big_endian = 1;
10383 break;
10385 case OPTION_EL:
10386 target_big_endian = 0;
10387 break;
10389 case 'O':
10390 if (arg && arg[1] == '0')
10391 mips_optimize = 1;
10392 else
10393 mips_optimize = 2;
10394 break;
10396 case 'g':
10397 if (arg == NULL)
10398 mips_debug = 2;
10399 else
10400 mips_debug = atoi (arg);
10401 /* When the MIPS assembler sees -g or -g2, it does not do
10402 optimizations which limit full symbolic debugging. We take
10403 that to be equivalent to -O0. */
10404 if (mips_debug == 2)
10405 mips_optimize = 1;
10406 break;
10408 case OPTION_MIPS1:
10409 file_mips_isa = ISA_MIPS1;
10410 break;
10412 case OPTION_MIPS2:
10413 file_mips_isa = ISA_MIPS2;
10414 break;
10416 case OPTION_MIPS3:
10417 file_mips_isa = ISA_MIPS3;
10418 break;
10420 case OPTION_MIPS4:
10421 file_mips_isa = ISA_MIPS4;
10422 break;
10424 case OPTION_MIPS5:
10425 file_mips_isa = ISA_MIPS5;
10426 break;
10428 case OPTION_MIPS32:
10429 file_mips_isa = ISA_MIPS32;
10430 break;
10432 case OPTION_MIPS32R2:
10433 file_mips_isa = ISA_MIPS32R2;
10434 break;
10436 case OPTION_MIPS64R2:
10437 file_mips_isa = ISA_MIPS64R2;
10438 break;
10440 case OPTION_MIPS64:
10441 file_mips_isa = ISA_MIPS64;
10442 break;
10444 case OPTION_MTUNE:
10445 mips_set_option_string (&mips_tune_string, arg);
10446 break;
10448 case OPTION_MARCH:
10449 mips_set_option_string (&mips_arch_string, arg);
10450 break;
10452 case OPTION_M4650:
10453 mips_set_option_string (&mips_arch_string, "4650");
10454 mips_set_option_string (&mips_tune_string, "4650");
10455 break;
10457 case OPTION_NO_M4650:
10458 break;
10460 case OPTION_M4010:
10461 mips_set_option_string (&mips_arch_string, "4010");
10462 mips_set_option_string (&mips_tune_string, "4010");
10463 break;
10465 case OPTION_NO_M4010:
10466 break;
10468 case OPTION_M4100:
10469 mips_set_option_string (&mips_arch_string, "4100");
10470 mips_set_option_string (&mips_tune_string, "4100");
10471 break;
10473 case OPTION_NO_M4100:
10474 break;
10476 case OPTION_M3900:
10477 mips_set_option_string (&mips_arch_string, "3900");
10478 mips_set_option_string (&mips_tune_string, "3900");
10479 break;
10481 case OPTION_NO_M3900:
10482 break;
10484 case OPTION_MDMX:
10485 mips_opts.ase_mdmx = 1;
10486 break;
10488 case OPTION_NO_MDMX:
10489 mips_opts.ase_mdmx = 0;
10490 break;
10492 case OPTION_DSP:
10493 mips_opts.ase_dsp = 1;
10494 break;
10496 case OPTION_NO_DSP:
10497 mips_opts.ase_dsp = 0;
10498 break;
10500 case OPTION_MIPS16:
10501 mips_opts.mips16 = 1;
10502 mips_no_prev_insn ();
10503 break;
10505 case OPTION_NO_MIPS16:
10506 mips_opts.mips16 = 0;
10507 mips_no_prev_insn ();
10508 break;
10510 case OPTION_MIPS3D:
10511 mips_opts.ase_mips3d = 1;
10512 break;
10514 case OPTION_NO_MIPS3D:
10515 mips_opts.ase_mips3d = 0;
10516 break;
10518 case OPTION_FIX_VR4120:
10519 mips_fix_vr4120 = 1;
10520 break;
10522 case OPTION_NO_FIX_VR4120:
10523 mips_fix_vr4120 = 0;
10524 break;
10526 case OPTION_FIX_VR4130:
10527 mips_fix_vr4130 = 1;
10528 break;
10530 case OPTION_NO_FIX_VR4130:
10531 mips_fix_vr4130 = 0;
10532 break;
10534 case OPTION_RELAX_BRANCH:
10535 mips_relax_branch = 1;
10536 break;
10538 case OPTION_NO_RELAX_BRANCH:
10539 mips_relax_branch = 0;
10540 break;
10542 case OPTION_MSHARED:
10543 mips_in_shared = TRUE;
10544 break;
10546 case OPTION_MNO_SHARED:
10547 mips_in_shared = FALSE;
10548 break;
10550 case OPTION_MSYM32:
10551 mips_opts.sym32 = TRUE;
10552 break;
10554 case OPTION_MNO_SYM32:
10555 mips_opts.sym32 = FALSE;
10556 break;
10558 #ifdef OBJ_ELF
10559 /* When generating ELF code, we permit -KPIC and -call_shared to
10560 select SVR4_PIC, and -non_shared to select no PIC. This is
10561 intended to be compatible with Irix 5. */
10562 case OPTION_CALL_SHARED:
10563 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10565 as_bad (_("-call_shared is supported only for ELF format"));
10566 return 0;
10568 mips_pic = SVR4_PIC;
10569 mips_abicalls = TRUE;
10570 if (g_switch_seen && g_switch_value != 0)
10572 as_bad (_("-G may not be used with SVR4 PIC code"));
10573 return 0;
10575 g_switch_value = 0;
10576 break;
10578 case OPTION_NON_SHARED:
10579 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10581 as_bad (_("-non_shared is supported only for ELF format"));
10582 return 0;
10584 mips_pic = NO_PIC;
10585 mips_abicalls = FALSE;
10586 break;
10588 /* The -xgot option tells the assembler to use 32 offsets when
10589 accessing the got in SVR4_PIC mode. It is for Irix
10590 compatibility. */
10591 case OPTION_XGOT:
10592 mips_big_got = 1;
10593 break;
10594 #endif /* OBJ_ELF */
10596 case 'G':
10597 g_switch_value = atoi (arg);
10598 g_switch_seen = 1;
10599 if (mips_pic == SVR4_PIC && g_switch_value != 0)
10601 as_bad (_("-G may not be used with SVR4 PIC code"));
10602 return 0;
10604 break;
10606 #ifdef OBJ_ELF
10607 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10608 and -mabi=64. */
10609 case OPTION_32:
10610 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10612 as_bad (_("-32 is supported for ELF format only"));
10613 return 0;
10615 mips_abi = O32_ABI;
10616 break;
10618 case OPTION_N32:
10619 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10621 as_bad (_("-n32 is supported for ELF format only"));
10622 return 0;
10624 mips_abi = N32_ABI;
10625 break;
10627 case OPTION_64:
10628 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10630 as_bad (_("-64 is supported for ELF format only"));
10631 return 0;
10633 mips_abi = N64_ABI;
10634 if (! support_64bit_objects())
10635 as_fatal (_("No compiled in support for 64 bit object file format"));
10636 break;
10637 #endif /* OBJ_ELF */
10639 case OPTION_GP32:
10640 file_mips_gp32 = 1;
10641 break;
10643 case OPTION_GP64:
10644 file_mips_gp32 = 0;
10645 break;
10647 case OPTION_FP32:
10648 file_mips_fp32 = 1;
10649 break;
10651 case OPTION_FP64:
10652 file_mips_fp32 = 0;
10653 break;
10655 #ifdef OBJ_ELF
10656 case OPTION_MABI:
10657 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10659 as_bad (_("-mabi is supported for ELF format only"));
10660 return 0;
10662 if (strcmp (arg, "32") == 0)
10663 mips_abi = O32_ABI;
10664 else if (strcmp (arg, "o64") == 0)
10665 mips_abi = O64_ABI;
10666 else if (strcmp (arg, "n32") == 0)
10667 mips_abi = N32_ABI;
10668 else if (strcmp (arg, "64") == 0)
10670 mips_abi = N64_ABI;
10671 if (! support_64bit_objects())
10672 as_fatal (_("No compiled in support for 64 bit object file "
10673 "format"));
10675 else if (strcmp (arg, "eabi") == 0)
10676 mips_abi = EABI_ABI;
10677 else
10679 as_fatal (_("invalid abi -mabi=%s"), arg);
10680 return 0;
10682 break;
10683 #endif /* OBJ_ELF */
10685 case OPTION_M7000_HILO_FIX:
10686 mips_7000_hilo_fix = TRUE;
10687 break;
10689 case OPTION_MNO_7000_HILO_FIX:
10690 mips_7000_hilo_fix = FALSE;
10691 break;
10693 #ifdef OBJ_ELF
10694 case OPTION_MDEBUG:
10695 mips_flag_mdebug = TRUE;
10696 break;
10698 case OPTION_NO_MDEBUG:
10699 mips_flag_mdebug = FALSE;
10700 break;
10702 case OPTION_PDR:
10703 mips_flag_pdr = TRUE;
10704 break;
10706 case OPTION_NO_PDR:
10707 mips_flag_pdr = FALSE;
10708 break;
10709 #endif /* OBJ_ELF */
10711 default:
10712 return 0;
10715 return 1;
10718 /* Set up globals to generate code for the ISA or processor
10719 described by INFO. */
10721 static void
10722 mips_set_architecture (const struct mips_cpu_info *info)
10724 if (info != 0)
10726 file_mips_arch = info->cpu;
10727 mips_opts.arch = info->cpu;
10728 mips_opts.isa = info->isa;
10733 /* Likewise for tuning. */
10735 static void
10736 mips_set_tune (const struct mips_cpu_info *info)
10738 if (info != 0)
10739 mips_tune = info->cpu;
10743 void
10744 mips_after_parse_args (void)
10746 const struct mips_cpu_info *arch_info = 0;
10747 const struct mips_cpu_info *tune_info = 0;
10749 /* GP relative stuff not working for PE */
10750 if (strncmp (TARGET_OS, "pe", 2) == 0)
10752 if (g_switch_seen && g_switch_value != 0)
10753 as_bad (_("-G not supported in this configuration."));
10754 g_switch_value = 0;
10757 if (mips_abi == NO_ABI)
10758 mips_abi = MIPS_DEFAULT_ABI;
10760 /* The following code determines the architecture and register size.
10761 Similar code was added to GCC 3.3 (see override_options() in
10762 config/mips/mips.c). The GAS and GCC code should be kept in sync
10763 as much as possible. */
10765 if (mips_arch_string != 0)
10766 arch_info = mips_parse_cpu ("-march", mips_arch_string);
10768 if (file_mips_isa != ISA_UNKNOWN)
10770 /* Handle -mipsN. At this point, file_mips_isa contains the
10771 ISA level specified by -mipsN, while arch_info->isa contains
10772 the -march selection (if any). */
10773 if (arch_info != 0)
10775 /* -march takes precedence over -mipsN, since it is more descriptive.
10776 There's no harm in specifying both as long as the ISA levels
10777 are the same. */
10778 if (file_mips_isa != arch_info->isa)
10779 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10780 mips_cpu_info_from_isa (file_mips_isa)->name,
10781 mips_cpu_info_from_isa (arch_info->isa)->name);
10783 else
10784 arch_info = mips_cpu_info_from_isa (file_mips_isa);
10787 if (arch_info == 0)
10788 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
10790 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
10791 as_bad ("-march=%s is not compatible with the selected ABI",
10792 arch_info->name);
10794 mips_set_architecture (arch_info);
10796 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10797 if (mips_tune_string != 0)
10798 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
10800 if (tune_info == 0)
10801 mips_set_tune (arch_info);
10802 else
10803 mips_set_tune (tune_info);
10805 if (file_mips_gp32 >= 0)
10807 /* The user specified the size of the integer registers. Make sure
10808 it agrees with the ABI and ISA. */
10809 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10810 as_bad (_("-mgp64 used with a 32-bit processor"));
10811 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10812 as_bad (_("-mgp32 used with a 64-bit ABI"));
10813 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10814 as_bad (_("-mgp64 used with a 32-bit ABI"));
10816 else
10818 /* Infer the integer register size from the ABI and processor.
10819 Restrict ourselves to 32-bit registers if that's all the
10820 processor has, or if the ABI cannot handle 64-bit registers. */
10821 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10822 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10825 /* ??? GAS treats single-float processors as though they had 64-bit
10826 float registers (although it complains when double-precision
10827 instructions are used). As things stand, saying they have 32-bit
10828 registers would lead to spurious "register must be even" messages.
10829 So here we assume float registers are always the same size as
10830 integer ones, unless the user says otherwise. */
10831 if (file_mips_fp32 < 0)
10832 file_mips_fp32 = file_mips_gp32;
10834 /* End of GCC-shared inference code. */
10836 /* This flag is set when we have a 64-bit capable CPU but use only
10837 32-bit wide registers. Note that EABI does not use it. */
10838 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10839 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10840 || mips_abi == O32_ABI))
10841 mips_32bitmode = 1;
10843 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10844 as_bad (_("trap exception not supported at ISA 1"));
10846 /* If the selected architecture includes support for ASEs, enable
10847 generation of code for them. */
10848 if (mips_opts.mips16 == -1)
10849 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
10850 if (mips_opts.ase_mips3d == -1)
10851 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
10852 if (mips_opts.ase_mdmx == -1)
10853 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
10854 if (mips_opts.ase_dsp == -1)
10855 mips_opts.ase_dsp = (CPU_HAS_DSP (file_mips_arch)) ? 1 : 0;
10857 file_mips_isa = mips_opts.isa;
10858 file_ase_mips16 = mips_opts.mips16;
10859 file_ase_mips3d = mips_opts.ase_mips3d;
10860 file_ase_mdmx = mips_opts.ase_mdmx;
10861 file_ase_dsp = mips_opts.ase_dsp;
10862 mips_opts.gp32 = file_mips_gp32;
10863 mips_opts.fp32 = file_mips_fp32;
10865 if (mips_flag_mdebug < 0)
10867 #ifdef OBJ_MAYBE_ECOFF
10868 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10869 mips_flag_mdebug = 1;
10870 else
10871 #endif /* OBJ_MAYBE_ECOFF */
10872 mips_flag_mdebug = 0;
10876 void
10877 mips_init_after_args (void)
10879 /* initialize opcodes */
10880 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10881 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10884 long
10885 md_pcrel_from (fixS *fixP)
10887 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10888 switch (fixP->fx_r_type)
10890 case BFD_RELOC_16_PCREL_S2:
10891 case BFD_RELOC_MIPS_JMP:
10892 /* Return the address of the delay slot. */
10893 return addr + 4;
10894 default:
10895 return addr;
10899 /* This is called before the symbol table is processed. In order to
10900 work with gcc when using mips-tfile, we must keep all local labels.
10901 However, in other cases, we want to discard them. If we were
10902 called with -g, but we didn't see any debugging information, it may
10903 mean that gcc is smuggling debugging information through to
10904 mips-tfile, in which case we must generate all local labels. */
10906 void
10907 mips_frob_file_before_adjust (void)
10909 #ifndef NO_ECOFF_DEBUGGING
10910 if (ECOFF_DEBUGGING
10911 && mips_debug != 0
10912 && ! ecoff_debugging_seen)
10913 flag_keep_locals = 1;
10914 #endif
10917 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
10918 the corresponding LO16 reloc. This is called before md_apply_fix and
10919 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
10920 relocation operators.
10922 For our purposes, a %lo() expression matches a %got() or %hi()
10923 expression if:
10925 (a) it refers to the same symbol; and
10926 (b) the offset applied in the %lo() expression is no lower than
10927 the offset applied in the %got() or %hi().
10929 (b) allows us to cope with code like:
10931 lui $4,%hi(foo)
10932 lh $4,%lo(foo+2)($4)
10934 ...which is legal on RELA targets, and has a well-defined behaviour
10935 if the user knows that adding 2 to "foo" will not induce a carry to
10936 the high 16 bits.
10938 When several %lo()s match a particular %got() or %hi(), we use the
10939 following rules to distinguish them:
10941 (1) %lo()s with smaller offsets are a better match than %lo()s with
10942 higher offsets.
10944 (2) %lo()s with no matching %got() or %hi() are better than those
10945 that already have a matching %got() or %hi().
10947 (3) later %lo()s are better than earlier %lo()s.
10949 These rules are applied in order.
10951 (1) means, among other things, that %lo()s with identical offsets are
10952 chosen if they exist.
10954 (2) means that we won't associate several high-part relocations with
10955 the same low-part relocation unless there's no alternative. Having
10956 several high parts for the same low part is a GNU extension; this rule
10957 allows careful users to avoid it.
10959 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
10960 with the last high-part relocation being at the front of the list.
10961 It therefore makes sense to choose the last matching low-part
10962 relocation, all other things being equal. It's also easier
10963 to code that way. */
10965 void
10966 mips_frob_file (void)
10968 struct mips_hi_fixup *l;
10970 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10972 segment_info_type *seginfo;
10973 bfd_boolean matched_lo_p;
10974 fixS **hi_pos, **lo_pos, **pos;
10976 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10978 /* If a GOT16 relocation turns out to be against a global symbol,
10979 there isn't supposed to be a matching LO. */
10980 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10981 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10982 continue;
10984 /* Check quickly whether the next fixup happens to be a matching %lo. */
10985 if (fixup_has_matching_lo_p (l->fixp))
10986 continue;
10988 seginfo = seg_info (l->seg);
10990 /* Set HI_POS to the position of this relocation in the chain.
10991 Set LO_POS to the position of the chosen low-part relocation.
10992 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
10993 relocation that matches an immediately-preceding high-part
10994 relocation. */
10995 hi_pos = NULL;
10996 lo_pos = NULL;
10997 matched_lo_p = FALSE;
10998 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
11000 if (*pos == l->fixp)
11001 hi_pos = pos;
11003 if (((*pos)->fx_r_type == BFD_RELOC_LO16
11004 || (*pos)->fx_r_type == BFD_RELOC_MIPS16_LO16)
11005 && (*pos)->fx_addsy == l->fixp->fx_addsy
11006 && (*pos)->fx_offset >= l->fixp->fx_offset
11007 && (lo_pos == NULL
11008 || (*pos)->fx_offset < (*lo_pos)->fx_offset
11009 || (!matched_lo_p
11010 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
11011 lo_pos = pos;
11013 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
11014 && fixup_has_matching_lo_p (*pos));
11017 /* If we found a match, remove the high-part relocation from its
11018 current position and insert it before the low-part relocation.
11019 Make the offsets match so that fixup_has_matching_lo_p()
11020 will return true.
11022 We don't warn about unmatched high-part relocations since some
11023 versions of gcc have been known to emit dead "lui ...%hi(...)"
11024 instructions. */
11025 if (lo_pos != NULL)
11027 l->fixp->fx_offset = (*lo_pos)->fx_offset;
11028 if (l->fixp->fx_next != *lo_pos)
11030 *hi_pos = l->fixp->fx_next;
11031 l->fixp->fx_next = *lo_pos;
11032 *lo_pos = l->fixp;
11038 /* We may have combined relocations without symbols in the N32/N64 ABI.
11039 We have to prevent gas from dropping them. */
11042 mips_force_relocation (fixS *fixp)
11044 if (generic_force_reloc (fixp))
11045 return 1;
11047 if (HAVE_NEWABI
11048 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11049 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11050 || fixp->fx_r_type == BFD_RELOC_HI16_S
11051 || fixp->fx_r_type == BFD_RELOC_LO16))
11052 return 1;
11054 return 0;
11057 /* This hook is called before a fix is simplified. We don't really
11058 decide whether to skip a fix here. Rather, we turn global symbols
11059 used as branch targets into local symbols, such that they undergo
11060 simplification. We can only do this if the symbol is defined and
11061 it is in the same section as the branch. If this doesn't hold, we
11062 emit a better error message than just saying the relocation is not
11063 valid for the selected object format.
11065 FIXP is the fix-up we're going to try to simplify, SEG is the
11066 segment in which the fix up occurs. The return value should be
11067 non-zero to indicate the fix-up is valid for further
11068 simplifications. */
11071 mips_validate_fix (struct fix *fixP, asection *seg)
11073 /* There's a lot of discussion on whether it should be possible to
11074 use R_MIPS_PC16 to represent branch relocations. The outcome
11075 seems to be that it can, but gas/bfd are very broken in creating
11076 RELA relocations for this, so for now we only accept branches to
11077 symbols in the same section. Anything else is of dubious value,
11078 since there's no guarantee that at link time the symbol would be
11079 in range. Even for branches to local symbols this is arguably
11080 wrong, since it we assume the symbol is not going to be
11081 overridden, which should be possible per ELF library semantics,
11082 but then, there isn't a dynamic relocation that could be used to
11083 this effect, and the target would likely be out of range as well.
11085 Unfortunately, it seems that there is too much code out there
11086 that relies on branches to symbols that are global to be resolved
11087 as if they were local, like the IRIX tools do, so we do it as
11088 well, but with a warning so that people are reminded to fix their
11089 code. If we ever get back to using R_MIPS_PC16 for branch
11090 targets, this entire block should go away (and probably the
11091 whole function). */
11093 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11094 && ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11095 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11096 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11097 && fixP->fx_addsy)
11099 if (! S_IS_DEFINED (fixP->fx_addsy))
11101 as_bad_where (fixP->fx_file, fixP->fx_line,
11102 _("Cannot branch to undefined symbol."));
11103 /* Avoid any further errors about this fixup. */
11104 fixP->fx_done = 1;
11106 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11108 as_bad_where (fixP->fx_file, fixP->fx_line,
11109 _("Cannot branch to symbol in another section."));
11110 fixP->fx_done = 1;
11112 else if (S_IS_EXTERNAL (fixP->fx_addsy))
11114 symbolS *sym = fixP->fx_addsy;
11116 if (mips_pic == SVR4_PIC)
11117 as_warn_where (fixP->fx_file, fixP->fx_line,
11118 _("Pretending global symbol used as branch target is local."));
11120 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11121 S_GET_SEGMENT (sym),
11122 S_GET_VALUE (sym),
11123 symbol_get_frag (sym));
11124 copy_symbol_attributes (fixP->fx_addsy, sym);
11125 S_CLEAR_EXTERNAL (fixP->fx_addsy);
11126 assert (symbol_resolved_p (sym));
11127 symbol_mark_resolved (fixP->fx_addsy);
11131 return 1;
11134 /* Apply a fixup to the object file. */
11136 void
11137 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
11139 bfd_byte *buf;
11140 long insn;
11141 reloc_howto_type *howto;
11143 /* We ignore generic BFD relocations we don't know about. */
11144 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11145 if (! howto)
11146 return;
11148 assert (fixP->fx_size == 4
11149 || fixP->fx_r_type == BFD_RELOC_16
11150 || fixP->fx_r_type == BFD_RELOC_64
11151 || fixP->fx_r_type == BFD_RELOC_CTOR
11152 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11153 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11154 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11156 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11158 assert (! fixP->fx_pcrel);
11160 /* Don't treat parts of a composite relocation as done. There are two
11161 reasons for this:
11163 (1) The second and third parts will be against 0 (RSS_UNDEF) but
11164 should nevertheless be emitted if the first part is.
11166 (2) In normal usage, composite relocations are never assembly-time
11167 constants. The easiest way of dealing with the pathological
11168 exceptions is to generate a relocation against STN_UNDEF and
11169 leave everything up to the linker. */
11170 if (fixP->fx_addsy == NULL && fixP->fx_tcbit == 0)
11171 fixP->fx_done = 1;
11173 switch (fixP->fx_r_type)
11175 case BFD_RELOC_MIPS_TLS_GD:
11176 case BFD_RELOC_MIPS_TLS_LDM:
11177 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
11178 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
11179 case BFD_RELOC_MIPS_TLS_GOTTPREL:
11180 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
11181 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
11182 S_SET_THREAD_LOCAL (fixP->fx_addsy);
11183 /* fall through */
11185 case BFD_RELOC_MIPS_JMP:
11186 case BFD_RELOC_MIPS_SHIFT5:
11187 case BFD_RELOC_MIPS_SHIFT6:
11188 case BFD_RELOC_MIPS_GOT_DISP:
11189 case BFD_RELOC_MIPS_GOT_PAGE:
11190 case BFD_RELOC_MIPS_GOT_OFST:
11191 case BFD_RELOC_MIPS_SUB:
11192 case BFD_RELOC_MIPS_INSERT_A:
11193 case BFD_RELOC_MIPS_INSERT_B:
11194 case BFD_RELOC_MIPS_DELETE:
11195 case BFD_RELOC_MIPS_HIGHEST:
11196 case BFD_RELOC_MIPS_HIGHER:
11197 case BFD_RELOC_MIPS_SCN_DISP:
11198 case BFD_RELOC_MIPS_REL16:
11199 case BFD_RELOC_MIPS_RELGOT:
11200 case BFD_RELOC_MIPS_JALR:
11201 case BFD_RELOC_HI16:
11202 case BFD_RELOC_HI16_S:
11203 case BFD_RELOC_GPREL16:
11204 case BFD_RELOC_MIPS_LITERAL:
11205 case BFD_RELOC_MIPS_CALL16:
11206 case BFD_RELOC_MIPS_GOT16:
11207 case BFD_RELOC_GPREL32:
11208 case BFD_RELOC_MIPS_GOT_HI16:
11209 case BFD_RELOC_MIPS_GOT_LO16:
11210 case BFD_RELOC_MIPS_CALL_HI16:
11211 case BFD_RELOC_MIPS_CALL_LO16:
11212 case BFD_RELOC_MIPS16_GPREL:
11213 case BFD_RELOC_MIPS16_HI16:
11214 case BFD_RELOC_MIPS16_HI16_S:
11215 assert (! fixP->fx_pcrel);
11216 /* Nothing needed to do. The value comes from the reloc entry */
11217 break;
11219 case BFD_RELOC_MIPS16_JMP:
11220 /* We currently always generate a reloc against a symbol, which
11221 means that we don't want an addend even if the symbol is
11222 defined. */
11223 *valP = 0;
11224 break;
11226 case BFD_RELOC_64:
11227 /* This is handled like BFD_RELOC_32, but we output a sign
11228 extended value if we are only 32 bits. */
11229 if (fixP->fx_done)
11231 if (8 <= sizeof (valueT))
11232 md_number_to_chars ((char *) buf, *valP, 8);
11233 else
11235 valueT hiv;
11237 if ((*valP & 0x80000000) != 0)
11238 hiv = 0xffffffff;
11239 else
11240 hiv = 0;
11241 md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
11242 *valP, 4);
11243 md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
11244 hiv, 4);
11247 break;
11249 case BFD_RELOC_RVA:
11250 case BFD_RELOC_32:
11251 /* If we are deleting this reloc entry, we must fill in the
11252 value now. This can happen if we have a .word which is not
11253 resolved when it appears but is later defined. */
11254 if (fixP->fx_done)
11255 md_number_to_chars ((char *) buf, *valP, 4);
11256 break;
11258 case BFD_RELOC_16:
11259 /* If we are deleting this reloc entry, we must fill in the
11260 value now. */
11261 if (fixP->fx_done)
11262 md_number_to_chars ((char *) buf, *valP, 2);
11263 break;
11265 case BFD_RELOC_LO16:
11266 case BFD_RELOC_MIPS16_LO16:
11267 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11268 may be safe to remove, but if so it's not obvious. */
11269 /* When handling an embedded PIC switch statement, we can wind
11270 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11271 if (fixP->fx_done)
11273 if (*valP + 0x8000 > 0xffff)
11274 as_bad_where (fixP->fx_file, fixP->fx_line,
11275 _("relocation overflow"));
11276 if (target_big_endian)
11277 buf += 2;
11278 md_number_to_chars ((char *) buf, *valP, 2);
11280 break;
11282 case BFD_RELOC_16_PCREL_S2:
11283 if ((*valP & 0x3) != 0)
11284 as_bad_where (fixP->fx_file, fixP->fx_line,
11285 _("Branch to odd address (%lx)"), (long) *valP);
11288 * We need to save the bits in the instruction since fixup_segment()
11289 * might be deleting the relocation entry (i.e., a branch within
11290 * the current segment).
11292 if (! fixP->fx_done)
11293 break;
11295 /* update old instruction data */
11296 if (target_big_endian)
11297 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11298 else
11299 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11301 if (*valP + 0x20000 <= 0x3ffff)
11303 insn |= (*valP >> 2) & 0xffff;
11304 md_number_to_chars ((char *) buf, insn, 4);
11306 else if (mips_pic == NO_PIC
11307 && fixP->fx_done
11308 && fixP->fx_frag->fr_address >= text_section->vma
11309 && (fixP->fx_frag->fr_address
11310 < text_section->vma + bfd_get_section_size (text_section))
11311 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11312 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11313 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11315 /* The branch offset is too large. If this is an
11316 unconditional branch, and we are not generating PIC code,
11317 we can convert it to an absolute jump instruction. */
11318 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11319 insn = 0x0c000000; /* jal */
11320 else
11321 insn = 0x08000000; /* j */
11322 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11323 fixP->fx_done = 0;
11324 fixP->fx_addsy = section_symbol (text_section);
11325 *valP += md_pcrel_from (fixP);
11326 md_number_to_chars ((char *) buf, insn, 4);
11328 else
11330 /* If we got here, we have branch-relaxation disabled,
11331 and there's nothing we can do to fix this instruction
11332 without turning it into a longer sequence. */
11333 as_bad_where (fixP->fx_file, fixP->fx_line,
11334 _("Branch out of range"));
11336 break;
11338 case BFD_RELOC_VTABLE_INHERIT:
11339 fixP->fx_done = 0;
11340 if (fixP->fx_addsy
11341 && !S_IS_DEFINED (fixP->fx_addsy)
11342 && !S_IS_WEAK (fixP->fx_addsy))
11343 S_SET_WEAK (fixP->fx_addsy);
11344 break;
11346 case BFD_RELOC_VTABLE_ENTRY:
11347 fixP->fx_done = 0;
11348 break;
11350 default:
11351 internalError ();
11354 /* Remember value for tc_gen_reloc. */
11355 fixP->fx_addnumber = *valP;
11358 static symbolS *
11359 get_symbol (void)
11361 int c;
11362 char *name;
11363 symbolS *p;
11365 name = input_line_pointer;
11366 c = get_symbol_end ();
11367 p = (symbolS *) symbol_find_or_make (name);
11368 *input_line_pointer = c;
11369 return p;
11372 /* Align the current frag to a given power of two. The MIPS assembler
11373 also automatically adjusts any preceding label. */
11375 static void
11376 mips_align (int to, int fill, symbolS *label)
11378 mips_emit_delays ();
11379 frag_align (to, fill, 0);
11380 record_alignment (now_seg, to);
11381 if (label != NULL)
11383 assert (S_GET_SEGMENT (label) == now_seg);
11384 symbol_set_frag (label, frag_now);
11385 S_SET_VALUE (label, (valueT) frag_now_fix ());
11389 /* Align to a given power of two. .align 0 turns off the automatic
11390 alignment used by the data creating pseudo-ops. */
11392 static void
11393 s_align (int x ATTRIBUTE_UNUSED)
11395 register int temp;
11396 register long temp_fill;
11397 long max_alignment = 15;
11401 o Note that the assembler pulls down any immediately preceding label
11402 to the aligned address.
11403 o It's not documented but auto alignment is reinstated by
11404 a .align pseudo instruction.
11405 o Note also that after auto alignment is turned off the mips assembler
11406 issues an error on attempt to assemble an improperly aligned data item.
11407 We don't.
11411 temp = get_absolute_expression ();
11412 if (temp > max_alignment)
11413 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11414 else if (temp < 0)
11416 as_warn (_("Alignment negative: 0 assumed."));
11417 temp = 0;
11419 if (*input_line_pointer == ',')
11421 ++input_line_pointer;
11422 temp_fill = get_absolute_expression ();
11424 else
11425 temp_fill = 0;
11426 if (temp)
11428 auto_align = 1;
11429 mips_align (temp, (int) temp_fill,
11430 insn_labels != NULL ? insn_labels->label : NULL);
11432 else
11434 auto_align = 0;
11437 demand_empty_rest_of_line ();
11440 static void
11441 s_change_sec (int sec)
11443 segT seg;
11445 #ifdef OBJ_ELF
11446 /* The ELF backend needs to know that we are changing sections, so
11447 that .previous works correctly. We could do something like check
11448 for an obj_section_change_hook macro, but that might be confusing
11449 as it would not be appropriate to use it in the section changing
11450 functions in read.c, since obj-elf.c intercepts those. FIXME:
11451 This should be cleaner, somehow. */
11452 obj_elf_section_change_hook ();
11453 #endif
11455 mips_emit_delays ();
11456 switch (sec)
11458 case 't':
11459 s_text (0);
11460 break;
11461 case 'd':
11462 s_data (0);
11463 break;
11464 case 'b':
11465 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11466 demand_empty_rest_of_line ();
11467 break;
11469 case 'r':
11470 seg = subseg_new (RDATA_SECTION_NAME,
11471 (subsegT) get_absolute_expression ());
11472 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11474 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11475 | SEC_READONLY | SEC_RELOC
11476 | SEC_DATA));
11477 if (strcmp (TARGET_OS, "elf") != 0)
11478 record_alignment (seg, 4);
11480 demand_empty_rest_of_line ();
11481 break;
11483 case 's':
11484 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11485 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11487 bfd_set_section_flags (stdoutput, seg,
11488 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11489 if (strcmp (TARGET_OS, "elf") != 0)
11490 record_alignment (seg, 4);
11492 demand_empty_rest_of_line ();
11493 break;
11496 auto_align = 1;
11499 void
11500 s_change_section (int ignore ATTRIBUTE_UNUSED)
11502 #ifdef OBJ_ELF
11503 char *section_name;
11504 char c;
11505 char next_c = 0;
11506 int section_type;
11507 int section_flag;
11508 int section_entry_size;
11509 int section_alignment;
11511 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11512 return;
11514 section_name = input_line_pointer;
11515 c = get_symbol_end ();
11516 if (c)
11517 next_c = *(input_line_pointer + 1);
11519 /* Do we have .section Name<,"flags">? */
11520 if (c != ',' || (c == ',' && next_c == '"'))
11522 /* just after name is now '\0'. */
11523 *input_line_pointer = c;
11524 input_line_pointer = section_name;
11525 obj_elf_section (ignore);
11526 return;
11528 input_line_pointer++;
11530 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11531 if (c == ',')
11532 section_type = get_absolute_expression ();
11533 else
11534 section_type = 0;
11535 if (*input_line_pointer++ == ',')
11536 section_flag = get_absolute_expression ();
11537 else
11538 section_flag = 0;
11539 if (*input_line_pointer++ == ',')
11540 section_entry_size = get_absolute_expression ();
11541 else
11542 section_entry_size = 0;
11543 if (*input_line_pointer++ == ',')
11544 section_alignment = get_absolute_expression ();
11545 else
11546 section_alignment = 0;
11548 section_name = xstrdup (section_name);
11550 /* When using the generic form of .section (as implemented by obj-elf.c),
11551 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11552 traditionally had to fall back on the more common @progbits instead.
11554 There's nothing really harmful in this, since bfd will correct
11555 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11556 means that, for backwards compatibiltiy, the special_section entries
11557 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11559 Even so, we shouldn't force users of the MIPS .section syntax to
11560 incorrectly label the sections as SHT_PROGBITS. The best compromise
11561 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11562 generic type-checking code. */
11563 if (section_type == SHT_MIPS_DWARF)
11564 section_type = SHT_PROGBITS;
11566 obj_elf_change_section (section_name, section_type, section_flag,
11567 section_entry_size, 0, 0, 0);
11569 if (now_seg->name != section_name)
11570 free (section_name);
11571 #endif /* OBJ_ELF */
11574 void
11575 mips_enable_auto_align (void)
11577 auto_align = 1;
11580 static void
11581 s_cons (int log_size)
11583 symbolS *label;
11585 label = insn_labels != NULL ? insn_labels->label : NULL;
11586 mips_emit_delays ();
11587 if (log_size > 0 && auto_align)
11588 mips_align (log_size, 0, label);
11589 mips_clear_insn_labels ();
11590 cons (1 << log_size);
11593 static void
11594 s_float_cons (int type)
11596 symbolS *label;
11598 label = insn_labels != NULL ? insn_labels->label : NULL;
11600 mips_emit_delays ();
11602 if (auto_align)
11604 if (type == 'd')
11605 mips_align (3, 0, label);
11606 else
11607 mips_align (2, 0, label);
11610 mips_clear_insn_labels ();
11612 float_cons (type);
11615 /* Handle .globl. We need to override it because on Irix 5 you are
11616 permitted to say
11617 .globl foo .text
11618 where foo is an undefined symbol, to mean that foo should be
11619 considered to be the address of a function. */
11621 static void
11622 s_mips_globl (int x ATTRIBUTE_UNUSED)
11624 char *name;
11625 int c;
11626 symbolS *symbolP;
11627 flagword flag;
11631 name = input_line_pointer;
11632 c = get_symbol_end ();
11633 symbolP = symbol_find_or_make (name);
11634 S_SET_EXTERNAL (symbolP);
11636 *input_line_pointer = c;
11637 SKIP_WHITESPACE ();
11639 /* On Irix 5, every global symbol that is not explicitly labelled as
11640 being a function is apparently labelled as being an object. */
11641 flag = BSF_OBJECT;
11643 if (!is_end_of_line[(unsigned char) *input_line_pointer]
11644 && (*input_line_pointer != ','))
11646 char *secname;
11647 asection *sec;
11649 secname = input_line_pointer;
11650 c = get_symbol_end ();
11651 sec = bfd_get_section_by_name (stdoutput, secname);
11652 if (sec == NULL)
11653 as_bad (_("%s: no such section"), secname);
11654 *input_line_pointer = c;
11656 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11657 flag = BSF_FUNCTION;
11660 symbol_get_bfdsym (symbolP)->flags |= flag;
11662 c = *input_line_pointer;
11663 if (c == ',')
11665 input_line_pointer++;
11666 SKIP_WHITESPACE ();
11667 if (is_end_of_line[(unsigned char) *input_line_pointer])
11668 c = '\n';
11671 while (c == ',');
11673 demand_empty_rest_of_line ();
11676 static void
11677 s_option (int x ATTRIBUTE_UNUSED)
11679 char *opt;
11680 char c;
11682 opt = input_line_pointer;
11683 c = get_symbol_end ();
11685 if (*opt == 'O')
11687 /* FIXME: What does this mean? */
11689 else if (strncmp (opt, "pic", 3) == 0)
11691 int i;
11693 i = atoi (opt + 3);
11694 if (i == 0)
11695 mips_pic = NO_PIC;
11696 else if (i == 2)
11698 mips_pic = SVR4_PIC;
11699 mips_abicalls = TRUE;
11701 else
11702 as_bad (_(".option pic%d not supported"), i);
11704 if (mips_pic == SVR4_PIC)
11706 if (g_switch_seen && g_switch_value != 0)
11707 as_warn (_("-G may not be used with SVR4 PIC code"));
11708 g_switch_value = 0;
11709 bfd_set_gp_size (stdoutput, 0);
11712 else
11713 as_warn (_("Unrecognized option \"%s\""), opt);
11715 *input_line_pointer = c;
11716 demand_empty_rest_of_line ();
11719 /* This structure is used to hold a stack of .set values. */
11721 struct mips_option_stack
11723 struct mips_option_stack *next;
11724 struct mips_set_options options;
11727 static struct mips_option_stack *mips_opts_stack;
11729 /* Handle the .set pseudo-op. */
11731 static void
11732 s_mipsset (int x ATTRIBUTE_UNUSED)
11734 char *name = input_line_pointer, ch;
11736 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11737 ++input_line_pointer;
11738 ch = *input_line_pointer;
11739 *input_line_pointer = '\0';
11741 if (strcmp (name, "reorder") == 0)
11743 if (mips_opts.noreorder)
11744 end_noreorder ();
11746 else if (strcmp (name, "noreorder") == 0)
11748 if (!mips_opts.noreorder)
11749 start_noreorder ();
11751 else if (strcmp (name, "at") == 0)
11753 mips_opts.noat = 0;
11755 else if (strcmp (name, "noat") == 0)
11757 mips_opts.noat = 1;
11759 else if (strcmp (name, "macro") == 0)
11761 mips_opts.warn_about_macros = 0;
11763 else if (strcmp (name, "nomacro") == 0)
11765 if (mips_opts.noreorder == 0)
11766 as_bad (_("`noreorder' must be set before `nomacro'"));
11767 mips_opts.warn_about_macros = 1;
11769 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11771 mips_opts.nomove = 0;
11773 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11775 mips_opts.nomove = 1;
11777 else if (strcmp (name, "bopt") == 0)
11779 mips_opts.nobopt = 0;
11781 else if (strcmp (name, "nobopt") == 0)
11783 mips_opts.nobopt = 1;
11785 else if (strcmp (name, "mips16") == 0
11786 || strcmp (name, "MIPS-16") == 0)
11787 mips_opts.mips16 = 1;
11788 else if (strcmp (name, "nomips16") == 0
11789 || strcmp (name, "noMIPS-16") == 0)
11790 mips_opts.mips16 = 0;
11791 else if (strcmp (name, "mips3d") == 0)
11792 mips_opts.ase_mips3d = 1;
11793 else if (strcmp (name, "nomips3d") == 0)
11794 mips_opts.ase_mips3d = 0;
11795 else if (strcmp (name, "mdmx") == 0)
11796 mips_opts.ase_mdmx = 1;
11797 else if (strcmp (name, "nomdmx") == 0)
11798 mips_opts.ase_mdmx = 0;
11799 else if (strcmp (name, "dsp") == 0)
11800 mips_opts.ase_dsp = 1;
11801 else if (strcmp (name, "nodsp") == 0)
11802 mips_opts.ase_dsp = 0;
11803 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
11805 int reset = 0;
11807 /* Permit the user to change the ISA and architecture on the fly.
11808 Needless to say, misuse can cause serious problems. */
11809 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
11811 reset = 1;
11812 mips_opts.isa = file_mips_isa;
11813 mips_opts.arch = file_mips_arch;
11815 else if (strncmp (name, "arch=", 5) == 0)
11817 const struct mips_cpu_info *p;
11819 p = mips_parse_cpu("internal use", name + 5);
11820 if (!p)
11821 as_bad (_("unknown architecture %s"), name + 5);
11822 else
11824 mips_opts.arch = p->cpu;
11825 mips_opts.isa = p->isa;
11828 else if (strncmp (name, "mips", 4) == 0)
11830 const struct mips_cpu_info *p;
11832 p = mips_parse_cpu("internal use", name);
11833 if (!p)
11834 as_bad (_("unknown ISA level %s"), name + 4);
11835 else
11837 mips_opts.arch = p->cpu;
11838 mips_opts.isa = p->isa;
11841 else
11842 as_bad (_("unknown ISA or architecture %s"), name);
11844 switch (mips_opts.isa)
11846 case 0:
11847 break;
11848 case ISA_MIPS1:
11849 case ISA_MIPS2:
11850 case ISA_MIPS32:
11851 case ISA_MIPS32R2:
11852 mips_opts.gp32 = 1;
11853 mips_opts.fp32 = 1;
11854 break;
11855 case ISA_MIPS3:
11856 case ISA_MIPS4:
11857 case ISA_MIPS5:
11858 case ISA_MIPS64:
11859 case ISA_MIPS64R2:
11860 mips_opts.gp32 = 0;
11861 mips_opts.fp32 = 0;
11862 break;
11863 default:
11864 as_bad (_("unknown ISA level %s"), name + 4);
11865 break;
11867 if (reset)
11869 mips_opts.gp32 = file_mips_gp32;
11870 mips_opts.fp32 = file_mips_fp32;
11873 else if (strcmp (name, "autoextend") == 0)
11874 mips_opts.noautoextend = 0;
11875 else if (strcmp (name, "noautoextend") == 0)
11876 mips_opts.noautoextend = 1;
11877 else if (strcmp (name, "push") == 0)
11879 struct mips_option_stack *s;
11881 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11882 s->next = mips_opts_stack;
11883 s->options = mips_opts;
11884 mips_opts_stack = s;
11886 else if (strcmp (name, "pop") == 0)
11888 struct mips_option_stack *s;
11890 s = mips_opts_stack;
11891 if (s == NULL)
11892 as_bad (_(".set pop with no .set push"));
11893 else
11895 /* If we're changing the reorder mode we need to handle
11896 delay slots correctly. */
11897 if (s->options.noreorder && ! mips_opts.noreorder)
11898 start_noreorder ();
11899 else if (! s->options.noreorder && mips_opts.noreorder)
11900 end_noreorder ();
11902 mips_opts = s->options;
11903 mips_opts_stack = s->next;
11904 free (s);
11907 else if (strcmp (name, "sym32") == 0)
11908 mips_opts.sym32 = TRUE;
11909 else if (strcmp (name, "nosym32") == 0)
11910 mips_opts.sym32 = FALSE;
11911 else
11913 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11915 *input_line_pointer = ch;
11916 demand_empty_rest_of_line ();
11919 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11920 .option pic2. It means to generate SVR4 PIC calls. */
11922 static void
11923 s_abicalls (int ignore ATTRIBUTE_UNUSED)
11925 mips_pic = SVR4_PIC;
11926 mips_abicalls = TRUE;
11928 if (g_switch_seen && g_switch_value != 0)
11929 as_warn (_("-G may not be used with SVR4 PIC code"));
11930 g_switch_value = 0;
11932 bfd_set_gp_size (stdoutput, 0);
11933 demand_empty_rest_of_line ();
11936 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11937 PIC code. It sets the $gp register for the function based on the
11938 function address, which is in the register named in the argument.
11939 This uses a relocation against _gp_disp, which is handled specially
11940 by the linker. The result is:
11941 lui $gp,%hi(_gp_disp)
11942 addiu $gp,$gp,%lo(_gp_disp)
11943 addu $gp,$gp,.cpload argument
11944 The .cpload argument is normally $25 == $t9.
11946 The -mno-shared option changes this to:
11947 lui $gp,%hi(__gnu_local_gp)
11948 addiu $gp,$gp,%lo(__gnu_local_gp)
11949 and the argument is ignored. This saves an instruction, but the
11950 resulting code is not position independent; it uses an absolute
11951 address for __gnu_local_gp. Thus code assembled with -mno-shared
11952 can go into an ordinary executable, but not into a shared library. */
11954 static void
11955 s_cpload (int ignore ATTRIBUTE_UNUSED)
11957 expressionS ex;
11958 int reg;
11959 int in_shared;
11961 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11962 .cpload is ignored. */
11963 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11965 s_ignore (0);
11966 return;
11969 /* .cpload should be in a .set noreorder section. */
11970 if (mips_opts.noreorder == 0)
11971 as_warn (_(".cpload not in noreorder section"));
11973 reg = tc_get_register (0);
11975 /* If we need to produce a 64-bit address, we are better off using
11976 the default instruction sequence. */
11977 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
11979 ex.X_op = O_symbol;
11980 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
11981 "__gnu_local_gp");
11982 ex.X_op_symbol = NULL;
11983 ex.X_add_number = 0;
11985 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11986 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11988 macro_start ();
11989 macro_build_lui (&ex, mips_gp_register);
11990 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11991 mips_gp_register, BFD_RELOC_LO16);
11992 if (in_shared)
11993 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
11994 mips_gp_register, reg);
11995 macro_end ();
11997 demand_empty_rest_of_line ();
12000 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12001 .cpsetup $reg1, offset|$reg2, label
12003 If offset is given, this results in:
12004 sd $gp, offset($sp)
12005 lui $gp, %hi(%neg(%gp_rel(label)))
12006 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12007 daddu $gp, $gp, $reg1
12009 If $reg2 is given, this results in:
12010 daddu $reg2, $gp, $0
12011 lui $gp, %hi(%neg(%gp_rel(label)))
12012 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12013 daddu $gp, $gp, $reg1
12014 $reg1 is normally $25 == $t9.
12016 The -mno-shared option replaces the last three instructions with
12017 lui $gp,%hi(_gp)
12018 addiu $gp,$gp,%lo(_gp)
12021 static void
12022 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
12024 expressionS ex_off;
12025 expressionS ex_sym;
12026 int reg1;
12028 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12029 We also need NewABI support. */
12030 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12032 s_ignore (0);
12033 return;
12036 reg1 = tc_get_register (0);
12037 SKIP_WHITESPACE ();
12038 if (*input_line_pointer != ',')
12040 as_bad (_("missing argument separator ',' for .cpsetup"));
12041 return;
12043 else
12044 ++input_line_pointer;
12045 SKIP_WHITESPACE ();
12046 if (*input_line_pointer == '$')
12048 mips_cpreturn_register = tc_get_register (0);
12049 mips_cpreturn_offset = -1;
12051 else
12053 mips_cpreturn_offset = get_absolute_expression ();
12054 mips_cpreturn_register = -1;
12056 SKIP_WHITESPACE ();
12057 if (*input_line_pointer != ',')
12059 as_bad (_("missing argument separator ',' for .cpsetup"));
12060 return;
12062 else
12063 ++input_line_pointer;
12064 SKIP_WHITESPACE ();
12065 expression (&ex_sym);
12067 macro_start ();
12068 if (mips_cpreturn_register == -1)
12070 ex_off.X_op = O_constant;
12071 ex_off.X_add_symbol = NULL;
12072 ex_off.X_op_symbol = NULL;
12073 ex_off.X_add_number = mips_cpreturn_offset;
12075 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
12076 BFD_RELOC_LO16, SP);
12078 else
12079 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
12080 mips_gp_register, 0);
12082 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
12084 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
12085 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
12086 BFD_RELOC_HI16_S);
12088 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
12089 mips_gp_register, -1, BFD_RELOC_GPREL16,
12090 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
12092 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12093 mips_gp_register, reg1);
12095 else
12097 expressionS ex;
12099 ex.X_op = O_symbol;
12100 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
12101 ex.X_op_symbol = NULL;
12102 ex.X_add_number = 0;
12104 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12105 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12107 macro_build_lui (&ex, mips_gp_register);
12108 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12109 mips_gp_register, BFD_RELOC_LO16);
12112 macro_end ();
12114 demand_empty_rest_of_line ();
12117 static void
12118 s_cplocal (int ignore ATTRIBUTE_UNUSED)
12120 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12121 .cplocal is ignored. */
12122 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12124 s_ignore (0);
12125 return;
12128 mips_gp_register = tc_get_register (0);
12129 demand_empty_rest_of_line ();
12132 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12133 offset from $sp. The offset is remembered, and after making a PIC
12134 call $gp is restored from that location. */
12136 static void
12137 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12139 expressionS ex;
12141 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12142 .cprestore is ignored. */
12143 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12145 s_ignore (0);
12146 return;
12149 mips_cprestore_offset = get_absolute_expression ();
12150 mips_cprestore_valid = 1;
12152 ex.X_op = O_constant;
12153 ex.X_add_symbol = NULL;
12154 ex.X_op_symbol = NULL;
12155 ex.X_add_number = mips_cprestore_offset;
12157 macro_start ();
12158 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12159 SP, HAVE_64BIT_ADDRESSES);
12160 macro_end ();
12162 demand_empty_rest_of_line ();
12165 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12166 was given in the preceding .cpsetup, it results in:
12167 ld $gp, offset($sp)
12169 If a register $reg2 was given there, it results in:
12170 daddu $gp, $reg2, $0
12172 static void
12173 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12175 expressionS ex;
12177 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12178 We also need NewABI support. */
12179 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12181 s_ignore (0);
12182 return;
12185 macro_start ();
12186 if (mips_cpreturn_register == -1)
12188 ex.X_op = O_constant;
12189 ex.X_add_symbol = NULL;
12190 ex.X_op_symbol = NULL;
12191 ex.X_add_number = mips_cpreturn_offset;
12193 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
12195 else
12196 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
12197 mips_cpreturn_register, 0);
12198 macro_end ();
12200 demand_empty_rest_of_line ();
12203 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12204 code. It sets the offset to use in gp_rel relocations. */
12206 static void
12207 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12209 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12210 We also need NewABI support. */
12211 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12213 s_ignore (0);
12214 return;
12217 mips_gprel_offset = get_absolute_expression ();
12219 demand_empty_rest_of_line ();
12222 /* Handle the .gpword pseudo-op. This is used when generating PIC
12223 code. It generates a 32 bit GP relative reloc. */
12225 static void
12226 s_gpword (int ignore ATTRIBUTE_UNUSED)
12228 symbolS *label;
12229 expressionS ex;
12230 char *p;
12232 /* When not generating PIC code, this is treated as .word. */
12233 if (mips_pic != SVR4_PIC)
12235 s_cons (2);
12236 return;
12239 label = insn_labels != NULL ? insn_labels->label : NULL;
12240 mips_emit_delays ();
12241 if (auto_align)
12242 mips_align (2, 0, label);
12243 mips_clear_insn_labels ();
12245 expression (&ex);
12247 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12249 as_bad (_("Unsupported use of .gpword"));
12250 ignore_rest_of_line ();
12253 p = frag_more (4);
12254 md_number_to_chars (p, 0, 4);
12255 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12256 BFD_RELOC_GPREL32);
12258 demand_empty_rest_of_line ();
12261 static void
12262 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12264 symbolS *label;
12265 expressionS ex;
12266 char *p;
12268 /* When not generating PIC code, this is treated as .dword. */
12269 if (mips_pic != SVR4_PIC)
12271 s_cons (3);
12272 return;
12275 label = insn_labels != NULL ? insn_labels->label : NULL;
12276 mips_emit_delays ();
12277 if (auto_align)
12278 mips_align (3, 0, label);
12279 mips_clear_insn_labels ();
12281 expression (&ex);
12283 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12285 as_bad (_("Unsupported use of .gpdword"));
12286 ignore_rest_of_line ();
12289 p = frag_more (8);
12290 md_number_to_chars (p, 0, 8);
12291 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12292 BFD_RELOC_GPREL32)->fx_tcbit = 1;
12294 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12295 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12296 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
12298 demand_empty_rest_of_line ();
12301 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12302 tables in SVR4 PIC code. */
12304 static void
12305 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12307 int reg;
12309 /* This is ignored when not generating SVR4 PIC code. */
12310 if (mips_pic != SVR4_PIC)
12312 s_ignore (0);
12313 return;
12316 /* Add $gp to the register named as an argument. */
12317 macro_start ();
12318 reg = tc_get_register (0);
12319 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
12320 macro_end ();
12322 demand_empty_rest_of_line ();
12325 /* Handle the .insn pseudo-op. This marks instruction labels in
12326 mips16 mode. This permits the linker to handle them specially,
12327 such as generating jalx instructions when needed. We also make
12328 them odd for the duration of the assembly, in order to generate the
12329 right sort of code. We will make them even in the adjust_symtab
12330 routine, while leaving them marked. This is convenient for the
12331 debugger and the disassembler. The linker knows to make them odd
12332 again. */
12334 static void
12335 s_insn (int ignore ATTRIBUTE_UNUSED)
12337 mips16_mark_labels ();
12339 demand_empty_rest_of_line ();
12342 /* Handle a .stabn directive. We need these in order to mark a label
12343 as being a mips16 text label correctly. Sometimes the compiler
12344 will emit a label, followed by a .stabn, and then switch sections.
12345 If the label and .stabn are in mips16 mode, then the label is
12346 really a mips16 text label. */
12348 static void
12349 s_mips_stab (int type)
12351 if (type == 'n')
12352 mips16_mark_labels ();
12354 s_stab (type);
12357 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12360 static void
12361 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12363 char *name;
12364 int c;
12365 symbolS *symbolP;
12366 expressionS exp;
12368 name = input_line_pointer;
12369 c = get_symbol_end ();
12370 symbolP = symbol_find_or_make (name);
12371 S_SET_WEAK (symbolP);
12372 *input_line_pointer = c;
12374 SKIP_WHITESPACE ();
12376 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12378 if (S_IS_DEFINED (symbolP))
12380 as_bad ("ignoring attempt to redefine symbol %s",
12381 S_GET_NAME (symbolP));
12382 ignore_rest_of_line ();
12383 return;
12386 if (*input_line_pointer == ',')
12388 ++input_line_pointer;
12389 SKIP_WHITESPACE ();
12392 expression (&exp);
12393 if (exp.X_op != O_symbol)
12395 as_bad ("bad .weakext directive");
12396 ignore_rest_of_line ();
12397 return;
12399 symbol_set_value_expression (symbolP, &exp);
12402 demand_empty_rest_of_line ();
12405 /* Parse a register string into a number. Called from the ECOFF code
12406 to parse .frame. The argument is non-zero if this is the frame
12407 register, so that we can record it in mips_frame_reg. */
12410 tc_get_register (int frame)
12412 int reg;
12414 SKIP_WHITESPACE ();
12415 if (*input_line_pointer++ != '$')
12417 as_warn (_("expected `$'"));
12418 reg = ZERO;
12420 else if (ISDIGIT (*input_line_pointer))
12422 reg = get_absolute_expression ();
12423 if (reg < 0 || reg >= 32)
12425 as_warn (_("Bad register number"));
12426 reg = ZERO;
12429 else
12431 if (strncmp (input_line_pointer, "ra", 2) == 0)
12433 reg = RA;
12434 input_line_pointer += 2;
12436 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12438 reg = FP;
12439 input_line_pointer += 2;
12441 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12443 reg = SP;
12444 input_line_pointer += 2;
12446 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12448 reg = GP;
12449 input_line_pointer += 2;
12451 else if (strncmp (input_line_pointer, "at", 2) == 0)
12453 reg = AT;
12454 input_line_pointer += 2;
12456 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12458 reg = KT0;
12459 input_line_pointer += 3;
12461 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12463 reg = KT1;
12464 input_line_pointer += 3;
12466 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12468 reg = ZERO;
12469 input_line_pointer += 4;
12471 else
12473 as_warn (_("Unrecognized register name"));
12474 reg = ZERO;
12475 while (ISALNUM(*input_line_pointer))
12476 input_line_pointer++;
12479 if (frame)
12481 mips_frame_reg = reg != 0 ? reg : SP;
12482 mips_frame_reg_valid = 1;
12483 mips_cprestore_valid = 0;
12485 return reg;
12488 valueT
12489 md_section_align (asection *seg, valueT addr)
12491 int align = bfd_get_section_alignment (stdoutput, seg);
12493 #ifdef OBJ_ELF
12494 /* We don't need to align ELF sections to the full alignment.
12495 However, Irix 5 may prefer that we align them at least to a 16
12496 byte boundary. We don't bother to align the sections if we are
12497 targeted for an embedded system. */
12498 if (strcmp (TARGET_OS, "elf") == 0)
12499 return addr;
12500 if (align > 4)
12501 align = 4;
12502 #endif
12504 return ((addr + (1 << align) - 1) & (-1 << align));
12507 /* Utility routine, called from above as well. If called while the
12508 input file is still being read, it's only an approximation. (For
12509 example, a symbol may later become defined which appeared to be
12510 undefined earlier.) */
12512 static int
12513 nopic_need_relax (symbolS *sym, int before_relaxing)
12515 if (sym == 0)
12516 return 0;
12518 if (g_switch_value > 0)
12520 const char *symname;
12521 int change;
12523 /* Find out whether this symbol can be referenced off the $gp
12524 register. It can be if it is smaller than the -G size or if
12525 it is in the .sdata or .sbss section. Certain symbols can
12526 not be referenced off the $gp, although it appears as though
12527 they can. */
12528 symname = S_GET_NAME (sym);
12529 if (symname != (const char *) NULL
12530 && (strcmp (symname, "eprol") == 0
12531 || strcmp (symname, "etext") == 0
12532 || strcmp (symname, "_gp") == 0
12533 || strcmp (symname, "edata") == 0
12534 || strcmp (symname, "_fbss") == 0
12535 || strcmp (symname, "_fdata") == 0
12536 || strcmp (symname, "_ftext") == 0
12537 || strcmp (symname, "end") == 0
12538 || strcmp (symname, "_gp_disp") == 0))
12539 change = 1;
12540 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12541 && (0
12542 #ifndef NO_ECOFF_DEBUGGING
12543 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12544 && (symbol_get_obj (sym)->ecoff_extern_size
12545 <= g_switch_value))
12546 #endif
12547 /* We must defer this decision until after the whole
12548 file has been read, since there might be a .extern
12549 after the first use of this symbol. */
12550 || (before_relaxing
12551 #ifndef NO_ECOFF_DEBUGGING
12552 && symbol_get_obj (sym)->ecoff_extern_size == 0
12553 #endif
12554 && S_GET_VALUE (sym) == 0)
12555 || (S_GET_VALUE (sym) != 0
12556 && S_GET_VALUE (sym) <= g_switch_value)))
12557 change = 0;
12558 else
12560 const char *segname;
12562 segname = segment_name (S_GET_SEGMENT (sym));
12563 assert (strcmp (segname, ".lit8") != 0
12564 && strcmp (segname, ".lit4") != 0);
12565 change = (strcmp (segname, ".sdata") != 0
12566 && strcmp (segname, ".sbss") != 0
12567 && strncmp (segname, ".sdata.", 7) != 0
12568 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12570 return change;
12572 else
12573 /* We are not optimizing for the $gp register. */
12574 return 1;
12578 /* Return true if the given symbol should be considered local for SVR4 PIC. */
12580 static bfd_boolean
12581 pic_need_relax (symbolS *sym, asection *segtype)
12583 asection *symsec;
12584 bfd_boolean linkonce;
12586 /* Handle the case of a symbol equated to another symbol. */
12587 while (symbol_equated_reloc_p (sym))
12589 symbolS *n;
12591 /* It's possible to get a loop here in a badly written
12592 program. */
12593 n = symbol_get_value_expression (sym)->X_add_symbol;
12594 if (n == sym)
12595 break;
12596 sym = n;
12599 symsec = S_GET_SEGMENT (sym);
12601 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12602 linkonce = FALSE;
12603 if (symsec != segtype && ! S_IS_LOCAL (sym))
12605 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12606 != 0)
12607 linkonce = TRUE;
12609 /* The GNU toolchain uses an extension for ELF: a section
12610 beginning with the magic string .gnu.linkonce is a linkonce
12611 section. */
12612 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12613 sizeof ".gnu.linkonce" - 1) == 0)
12614 linkonce = TRUE;
12617 /* This must duplicate the test in adjust_reloc_syms. */
12618 return (symsec != &bfd_und_section
12619 && symsec != &bfd_abs_section
12620 && ! bfd_is_com_section (symsec)
12621 && !linkonce
12622 #ifdef OBJ_ELF
12623 /* A global or weak symbol is treated as external. */
12624 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12625 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
12626 #endif
12631 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12632 extended opcode. SEC is the section the frag is in. */
12634 static int
12635 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
12637 int type;
12638 register const struct mips16_immed_operand *op;
12639 offsetT val;
12640 int mintiny, maxtiny;
12641 segT symsec;
12642 fragS *sym_frag;
12644 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12645 return 0;
12646 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12647 return 1;
12649 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12650 op = mips16_immed_operands;
12651 while (op->type != type)
12653 ++op;
12654 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12657 if (op->unsp)
12659 if (type == '<' || type == '>' || type == '[' || type == ']')
12661 mintiny = 1;
12662 maxtiny = 1 << op->nbits;
12664 else
12666 mintiny = 0;
12667 maxtiny = (1 << op->nbits) - 1;
12670 else
12672 mintiny = - (1 << (op->nbits - 1));
12673 maxtiny = (1 << (op->nbits - 1)) - 1;
12676 sym_frag = symbol_get_frag (fragp->fr_symbol);
12677 val = S_GET_VALUE (fragp->fr_symbol);
12678 symsec = S_GET_SEGMENT (fragp->fr_symbol);
12680 if (op->pcrel)
12682 addressT addr;
12684 /* We won't have the section when we are called from
12685 mips_relax_frag. However, we will always have been called
12686 from md_estimate_size_before_relax first. If this is a
12687 branch to a different section, we mark it as such. If SEC is
12688 NULL, and the frag is not marked, then it must be a branch to
12689 the same section. */
12690 if (sec == NULL)
12692 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12693 return 1;
12695 else
12697 /* Must have been called from md_estimate_size_before_relax. */
12698 if (symsec != sec)
12700 fragp->fr_subtype =
12701 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12703 /* FIXME: We should support this, and let the linker
12704 catch branches and loads that are out of range. */
12705 as_bad_where (fragp->fr_file, fragp->fr_line,
12706 _("unsupported PC relative reference to different section"));
12708 return 1;
12710 if (fragp != sym_frag && sym_frag->fr_address == 0)
12711 /* Assume non-extended on the first relaxation pass.
12712 The address we have calculated will be bogus if this is
12713 a forward branch to another frag, as the forward frag
12714 will have fr_address == 0. */
12715 return 0;
12718 /* In this case, we know for sure that the symbol fragment is in
12719 the same section. If the relax_marker of the symbol fragment
12720 differs from the relax_marker of this fragment, we have not
12721 yet adjusted the symbol fragment fr_address. We want to add
12722 in STRETCH in order to get a better estimate of the address.
12723 This particularly matters because of the shift bits. */
12724 if (stretch != 0
12725 && sym_frag->relax_marker != fragp->relax_marker)
12727 fragS *f;
12729 /* Adjust stretch for any alignment frag. Note that if have
12730 been expanding the earlier code, the symbol may be
12731 defined in what appears to be an earlier frag. FIXME:
12732 This doesn't handle the fr_subtype field, which specifies
12733 a maximum number of bytes to skip when doing an
12734 alignment. */
12735 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12737 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12739 if (stretch < 0)
12740 stretch = - ((- stretch)
12741 & ~ ((1 << (int) f->fr_offset) - 1));
12742 else
12743 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12744 if (stretch == 0)
12745 break;
12748 if (f != NULL)
12749 val += stretch;
12752 addr = fragp->fr_address + fragp->fr_fix;
12754 /* The base address rules are complicated. The base address of
12755 a branch is the following instruction. The base address of a
12756 PC relative load or add is the instruction itself, but if it
12757 is in a delay slot (in which case it can not be extended) use
12758 the address of the instruction whose delay slot it is in. */
12759 if (type == 'p' || type == 'q')
12761 addr += 2;
12763 /* If we are currently assuming that this frag should be
12764 extended, then, the current address is two bytes
12765 higher. */
12766 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12767 addr += 2;
12769 /* Ignore the low bit in the target, since it will be set
12770 for a text label. */
12771 if ((val & 1) != 0)
12772 --val;
12774 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12775 addr -= 4;
12776 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12777 addr -= 2;
12779 val -= addr & ~ ((1 << op->shift) - 1);
12781 /* Branch offsets have an implicit 0 in the lowest bit. */
12782 if (type == 'p' || type == 'q')
12783 val /= 2;
12785 /* If any of the shifted bits are set, we must use an extended
12786 opcode. If the address depends on the size of this
12787 instruction, this can lead to a loop, so we arrange to always
12788 use an extended opcode. We only check this when we are in
12789 the main relaxation loop, when SEC is NULL. */
12790 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12792 fragp->fr_subtype =
12793 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12794 return 1;
12797 /* If we are about to mark a frag as extended because the value
12798 is precisely maxtiny + 1, then there is a chance of an
12799 infinite loop as in the following code:
12800 la $4,foo
12801 .skip 1020
12802 .align 2
12803 foo:
12804 In this case when the la is extended, foo is 0x3fc bytes
12805 away, so the la can be shrunk, but then foo is 0x400 away, so
12806 the la must be extended. To avoid this loop, we mark the
12807 frag as extended if it was small, and is about to become
12808 extended with a value of maxtiny + 1. */
12809 if (val == ((maxtiny + 1) << op->shift)
12810 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12811 && sec == NULL)
12813 fragp->fr_subtype =
12814 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12815 return 1;
12818 else if (symsec != absolute_section && sec != NULL)
12819 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12821 if ((val & ((1 << op->shift) - 1)) != 0
12822 || val < (mintiny << op->shift)
12823 || val > (maxtiny << op->shift))
12824 return 1;
12825 else
12826 return 0;
12829 /* Compute the length of a branch sequence, and adjust the
12830 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12831 worst-case length is computed, with UPDATE being used to indicate
12832 whether an unconditional (-1), branch-likely (+1) or regular (0)
12833 branch is to be computed. */
12834 static int
12835 relaxed_branch_length (fragS *fragp, asection *sec, int update)
12837 bfd_boolean toofar;
12838 int length;
12840 if (fragp
12841 && S_IS_DEFINED (fragp->fr_symbol)
12842 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12844 addressT addr;
12845 offsetT val;
12847 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12849 addr = fragp->fr_address + fragp->fr_fix + 4;
12851 val -= addr;
12853 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12855 else if (fragp)
12856 /* If the symbol is not defined or it's in a different segment,
12857 assume the user knows what's going on and emit a short
12858 branch. */
12859 toofar = FALSE;
12860 else
12861 toofar = TRUE;
12863 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12864 fragp->fr_subtype
12865 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12866 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12867 RELAX_BRANCH_LINK (fragp->fr_subtype),
12868 toofar);
12870 length = 4;
12871 if (toofar)
12873 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12874 length += 8;
12876 if (mips_pic != NO_PIC)
12878 /* Additional space for PIC loading of target address. */
12879 length += 8;
12880 if (mips_opts.isa == ISA_MIPS1)
12881 /* Additional space for $at-stabilizing nop. */
12882 length += 4;
12885 /* If branch is conditional. */
12886 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12887 length += 8;
12890 return length;
12893 /* Estimate the size of a frag before relaxing. Unless this is the
12894 mips16, we are not really relaxing here, and the final size is
12895 encoded in the subtype information. For the mips16, we have to
12896 decide whether we are using an extended opcode or not. */
12899 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
12901 int change;
12903 if (RELAX_BRANCH_P (fragp->fr_subtype))
12906 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12908 return fragp->fr_var;
12911 if (RELAX_MIPS16_P (fragp->fr_subtype))
12912 /* We don't want to modify the EXTENDED bit here; it might get us
12913 into infinite loops. We change it only in mips_relax_frag(). */
12914 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12916 if (mips_pic == NO_PIC)
12917 change = nopic_need_relax (fragp->fr_symbol, 0);
12918 else if (mips_pic == SVR4_PIC)
12919 change = pic_need_relax (fragp->fr_symbol, segtype);
12920 else
12921 abort ();
12923 if (change)
12925 fragp->fr_subtype |= RELAX_USE_SECOND;
12926 return -RELAX_FIRST (fragp->fr_subtype);
12928 else
12929 return -RELAX_SECOND (fragp->fr_subtype);
12932 /* This is called to see whether a reloc against a defined symbol
12933 should be converted into a reloc against a section. */
12936 mips_fix_adjustable (fixS *fixp)
12938 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
12939 about the format of the offset in the .o file. */
12940 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12941 return 0;
12943 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12944 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12945 return 0;
12947 if (fixp->fx_addsy == NULL)
12948 return 1;
12950 /* If symbol SYM is in a mergeable section, relocations of the form
12951 SYM + 0 can usually be made section-relative. The mergeable data
12952 is then identified by the section offset rather than by the symbol.
12954 However, if we're generating REL LO16 relocations, the offset is split
12955 between the LO16 and parterning high part relocation. The linker will
12956 need to recalculate the complete offset in order to correctly identify
12957 the merge data.
12959 The linker has traditionally not looked for the parterning high part
12960 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
12961 placed anywhere. Rather than break backwards compatibility by changing
12962 this, it seems better not to force the issue, and instead keep the
12963 original symbol. This will work with either linker behavior. */
12964 if ((fixp->fx_r_type == BFD_RELOC_LO16
12965 || fixp->fx_r_type == BFD_RELOC_MIPS16_LO16
12966 || reloc_needs_lo_p (fixp->fx_r_type))
12967 && HAVE_IN_PLACE_ADDENDS
12968 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
12969 return 0;
12971 #ifdef OBJ_ELF
12972 /* Don't adjust relocations against mips16 symbols, so that the linker
12973 can find them if it needs to set up a stub. */
12974 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12975 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12976 && fixp->fx_subsy == NULL)
12977 return 0;
12978 #endif
12980 return 1;
12983 /* Translate internal representation of relocation info to BFD target
12984 format. */
12986 arelent **
12987 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
12989 static arelent *retval[4];
12990 arelent *reloc;
12991 bfd_reloc_code_real_type code;
12993 memset (retval, 0, sizeof(retval));
12994 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
12995 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12996 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12997 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12999 assert (! fixp->fx_pcrel);
13000 reloc->addend = fixp->fx_addnumber;
13002 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13003 entry to be used in the relocation's section offset. */
13004 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13006 reloc->address = reloc->addend;
13007 reloc->addend = 0;
13010 code = fixp->fx_r_type;
13012 /* To support a PC relative reloc, we used a Cygnus extension.
13013 We check for that here to make sure that we don't let such a
13014 reloc escape normally. (FIXME: This was formerly used by
13015 embedded-PIC support, but is now used by branch handling in
13016 general. That probably should be fixed.) */
13017 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13018 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13019 && code == BFD_RELOC_16_PCREL_S2)
13020 reloc->howto = NULL;
13021 else
13022 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13024 if (reloc->howto == NULL)
13026 as_bad_where (fixp->fx_file, fixp->fx_line,
13027 _("Can not represent %s relocation in this object file format"),
13028 bfd_get_reloc_code_name (code));
13029 retval[0] = NULL;
13032 return retval;
13035 /* Relax a machine dependent frag. This returns the amount by which
13036 the current size of the frag should change. */
13039 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
13041 if (RELAX_BRANCH_P (fragp->fr_subtype))
13043 offsetT old_var = fragp->fr_var;
13045 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13047 return fragp->fr_var - old_var;
13050 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13051 return 0;
13053 if (mips16_extended_frag (fragp, NULL, stretch))
13055 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13056 return 0;
13057 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13058 return 2;
13060 else
13062 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13063 return 0;
13064 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13065 return -2;
13068 return 0;
13071 /* Convert a machine dependent frag. */
13073 void
13074 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
13076 if (RELAX_BRANCH_P (fragp->fr_subtype))
13078 bfd_byte *buf;
13079 unsigned long insn;
13080 expressionS exp;
13081 fixS *fixp;
13083 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13085 if (target_big_endian)
13086 insn = bfd_getb32 (buf);
13087 else
13088 insn = bfd_getl32 (buf);
13090 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13092 /* We generate a fixup instead of applying it right now
13093 because, if there are linker relaxations, we're going to
13094 need the relocations. */
13095 exp.X_op = O_symbol;
13096 exp.X_add_symbol = fragp->fr_symbol;
13097 exp.X_add_number = fragp->fr_offset;
13099 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13100 4, &exp, 1,
13101 BFD_RELOC_16_PCREL_S2);
13102 fixp->fx_file = fragp->fr_file;
13103 fixp->fx_line = fragp->fr_line;
13105 md_number_to_chars ((char *) buf, insn, 4);
13106 buf += 4;
13108 else
13110 int i;
13112 as_warn_where (fragp->fr_file, fragp->fr_line,
13113 _("relaxed out-of-range branch into a jump"));
13115 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13116 goto uncond;
13118 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13120 /* Reverse the branch. */
13121 switch ((insn >> 28) & 0xf)
13123 case 4:
13124 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13125 have the condition reversed by tweaking a single
13126 bit, and their opcodes all have 0x4???????. */
13127 assert ((insn & 0xf1000000) == 0x41000000);
13128 insn ^= 0x00010000;
13129 break;
13131 case 0:
13132 /* bltz 0x04000000 bgez 0x04010000
13133 bltzal 0x04100000 bgezal 0x04110000 */
13134 assert ((insn & 0xfc0e0000) == 0x04000000);
13135 insn ^= 0x00010000;
13136 break;
13138 case 1:
13139 /* beq 0x10000000 bne 0x14000000
13140 blez 0x18000000 bgtz 0x1c000000 */
13141 insn ^= 0x04000000;
13142 break;
13144 default:
13145 abort ();
13149 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13151 /* Clear the and-link bit. */
13152 assert ((insn & 0xfc1c0000) == 0x04100000);
13154 /* bltzal 0x04100000 bgezal 0x04110000
13155 bltzall 0x04120000 bgezall 0x04130000 */
13156 insn &= ~0x00100000;
13159 /* Branch over the branch (if the branch was likely) or the
13160 full jump (not likely case). Compute the offset from the
13161 current instruction to branch to. */
13162 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13163 i = 16;
13164 else
13166 /* How many bytes in instructions we've already emitted? */
13167 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13168 /* How many bytes in instructions from here to the end? */
13169 i = fragp->fr_var - i;
13171 /* Convert to instruction count. */
13172 i >>= 2;
13173 /* Branch counts from the next instruction. */
13174 i--;
13175 insn |= i;
13176 /* Branch over the jump. */
13177 md_number_to_chars ((char *) buf, insn, 4);
13178 buf += 4;
13180 /* Nop */
13181 md_number_to_chars ((char *) buf, 0, 4);
13182 buf += 4;
13184 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13186 /* beql $0, $0, 2f */
13187 insn = 0x50000000;
13188 /* Compute the PC offset from the current instruction to
13189 the end of the variable frag. */
13190 /* How many bytes in instructions we've already emitted? */
13191 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13192 /* How many bytes in instructions from here to the end? */
13193 i = fragp->fr_var - i;
13194 /* Convert to instruction count. */
13195 i >>= 2;
13196 /* Don't decrement i, because we want to branch over the
13197 delay slot. */
13199 insn |= i;
13200 md_number_to_chars ((char *) buf, insn, 4);
13201 buf += 4;
13203 md_number_to_chars ((char *) buf, 0, 4);
13204 buf += 4;
13207 uncond:
13208 if (mips_pic == NO_PIC)
13210 /* j or jal. */
13211 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13212 ? 0x0c000000 : 0x08000000);
13213 exp.X_op = O_symbol;
13214 exp.X_add_symbol = fragp->fr_symbol;
13215 exp.X_add_number = fragp->fr_offset;
13217 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13218 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13219 fixp->fx_file = fragp->fr_file;
13220 fixp->fx_line = fragp->fr_line;
13222 md_number_to_chars ((char *) buf, insn, 4);
13223 buf += 4;
13225 else
13227 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13228 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13229 exp.X_op = O_symbol;
13230 exp.X_add_symbol = fragp->fr_symbol;
13231 exp.X_add_number = fragp->fr_offset;
13233 if (fragp->fr_offset)
13235 exp.X_add_symbol = make_expr_symbol (&exp);
13236 exp.X_add_number = 0;
13239 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13240 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13241 fixp->fx_file = fragp->fr_file;
13242 fixp->fx_line = fragp->fr_line;
13244 md_number_to_chars ((char *) buf, insn, 4);
13245 buf += 4;
13247 if (mips_opts.isa == ISA_MIPS1)
13249 /* nop */
13250 md_number_to_chars ((char *) buf, 0, 4);
13251 buf += 4;
13254 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13255 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13257 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13258 4, &exp, 0, BFD_RELOC_LO16);
13259 fixp->fx_file = fragp->fr_file;
13260 fixp->fx_line = fragp->fr_line;
13262 md_number_to_chars ((char *) buf, insn, 4);
13263 buf += 4;
13265 /* j(al)r $at. */
13266 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13267 insn = 0x0020f809;
13268 else
13269 insn = 0x00200008;
13271 md_number_to_chars ((char *) buf, insn, 4);
13272 buf += 4;
13276 assert (buf == (bfd_byte *)fragp->fr_literal
13277 + fragp->fr_fix + fragp->fr_var);
13279 fragp->fr_fix += fragp->fr_var;
13281 return;
13284 if (RELAX_MIPS16_P (fragp->fr_subtype))
13286 int type;
13287 register const struct mips16_immed_operand *op;
13288 bfd_boolean small, ext;
13289 offsetT val;
13290 bfd_byte *buf;
13291 unsigned long insn;
13292 bfd_boolean use_extend;
13293 unsigned short extend;
13295 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13296 op = mips16_immed_operands;
13297 while (op->type != type)
13298 ++op;
13300 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13302 small = FALSE;
13303 ext = TRUE;
13305 else
13307 small = TRUE;
13308 ext = FALSE;
13311 resolve_symbol_value (fragp->fr_symbol);
13312 val = S_GET_VALUE (fragp->fr_symbol);
13313 if (op->pcrel)
13315 addressT addr;
13317 addr = fragp->fr_address + fragp->fr_fix;
13319 /* The rules for the base address of a PC relative reloc are
13320 complicated; see mips16_extended_frag. */
13321 if (type == 'p' || type == 'q')
13323 addr += 2;
13324 if (ext)
13325 addr += 2;
13326 /* Ignore the low bit in the target, since it will be
13327 set for a text label. */
13328 if ((val & 1) != 0)
13329 --val;
13331 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13332 addr -= 4;
13333 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13334 addr -= 2;
13336 addr &= ~ (addressT) ((1 << op->shift) - 1);
13337 val -= addr;
13339 /* Make sure the section winds up with the alignment we have
13340 assumed. */
13341 if (op->shift > 0)
13342 record_alignment (asec, op->shift);
13345 if (ext
13346 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13347 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13348 as_warn_where (fragp->fr_file, fragp->fr_line,
13349 _("extended instruction in delay slot"));
13351 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13353 if (target_big_endian)
13354 insn = bfd_getb16 (buf);
13355 else
13356 insn = bfd_getl16 (buf);
13358 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13359 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13360 small, ext, &insn, &use_extend, &extend);
13362 if (use_extend)
13364 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13365 fragp->fr_fix += 2;
13366 buf += 2;
13369 md_number_to_chars ((char *) buf, insn, 2);
13370 fragp->fr_fix += 2;
13371 buf += 2;
13373 else
13375 int first, second;
13376 fixS *fixp;
13378 first = RELAX_FIRST (fragp->fr_subtype);
13379 second = RELAX_SECOND (fragp->fr_subtype);
13380 fixp = (fixS *) fragp->fr_opcode;
13382 /* Possibly emit a warning if we've chosen the longer option. */
13383 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13384 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13386 const char *msg = macro_warning (fragp->fr_subtype);
13387 if (msg != 0)
13388 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13391 /* Go through all the fixups for the first sequence. Disable them
13392 (by marking them as done) if we're going to use the second
13393 sequence instead. */
13394 while (fixp
13395 && fixp->fx_frag == fragp
13396 && fixp->fx_where < fragp->fr_fix - second)
13398 if (fragp->fr_subtype & RELAX_USE_SECOND)
13399 fixp->fx_done = 1;
13400 fixp = fixp->fx_next;
13403 /* Go through the fixups for the second sequence. Disable them if
13404 we're going to use the first sequence, otherwise adjust their
13405 addresses to account for the relaxation. */
13406 while (fixp && fixp->fx_frag == fragp)
13408 if (fragp->fr_subtype & RELAX_USE_SECOND)
13409 fixp->fx_where -= first;
13410 else
13411 fixp->fx_done = 1;
13412 fixp = fixp->fx_next;
13415 /* Now modify the frag contents. */
13416 if (fragp->fr_subtype & RELAX_USE_SECOND)
13418 char *start;
13420 start = fragp->fr_literal + fragp->fr_fix - first - second;
13421 memmove (start, start + first, second);
13422 fragp->fr_fix -= first;
13424 else
13425 fragp->fr_fix -= second;
13429 #ifdef OBJ_ELF
13431 /* This function is called after the relocs have been generated.
13432 We've been storing mips16 text labels as odd. Here we convert them
13433 back to even for the convenience of the debugger. */
13435 void
13436 mips_frob_file_after_relocs (void)
13438 asymbol **syms;
13439 unsigned int count, i;
13441 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13442 return;
13444 syms = bfd_get_outsymbols (stdoutput);
13445 count = bfd_get_symcount (stdoutput);
13446 for (i = 0; i < count; i++, syms++)
13448 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13449 && ((*syms)->value & 1) != 0)
13451 (*syms)->value &= ~1;
13452 /* If the symbol has an odd size, it was probably computed
13453 incorrectly, so adjust that as well. */
13454 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13455 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13460 #endif
13462 /* This function is called whenever a label is defined. It is used
13463 when handling branch delays; if a branch has a label, we assume we
13464 can not move it. */
13466 void
13467 mips_define_label (symbolS *sym)
13469 struct insn_label_list *l;
13471 if (free_insn_labels == NULL)
13472 l = (struct insn_label_list *) xmalloc (sizeof *l);
13473 else
13475 l = free_insn_labels;
13476 free_insn_labels = l->next;
13479 l->label = sym;
13480 l->next = insn_labels;
13481 insn_labels = l;
13484 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13486 /* Some special processing for a MIPS ELF file. */
13488 void
13489 mips_elf_final_processing (void)
13491 /* Write out the register information. */
13492 if (mips_abi != N64_ABI)
13494 Elf32_RegInfo s;
13496 s.ri_gprmask = mips_gprmask;
13497 s.ri_cprmask[0] = mips_cprmask[0];
13498 s.ri_cprmask[1] = mips_cprmask[1];
13499 s.ri_cprmask[2] = mips_cprmask[2];
13500 s.ri_cprmask[3] = mips_cprmask[3];
13501 /* The gp_value field is set by the MIPS ELF backend. */
13503 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13504 ((Elf32_External_RegInfo *)
13505 mips_regmask_frag));
13507 else
13509 Elf64_Internal_RegInfo s;
13511 s.ri_gprmask = mips_gprmask;
13512 s.ri_pad = 0;
13513 s.ri_cprmask[0] = mips_cprmask[0];
13514 s.ri_cprmask[1] = mips_cprmask[1];
13515 s.ri_cprmask[2] = mips_cprmask[2];
13516 s.ri_cprmask[3] = mips_cprmask[3];
13517 /* The gp_value field is set by the MIPS ELF backend. */
13519 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13520 ((Elf64_External_RegInfo *)
13521 mips_regmask_frag));
13524 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13525 sort of BFD interface for this. */
13526 if (mips_any_noreorder)
13527 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13528 if (mips_pic != NO_PIC)
13530 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13531 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13533 if (mips_abicalls)
13534 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13536 /* Set MIPS ELF flags for ASEs. */
13537 /* We may need to define a new flag for DSP ASE, and set this flag when
13538 file_ase_dsp is true. */
13539 if (file_ase_mips16)
13540 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13541 #if 0 /* XXX FIXME */
13542 if (file_ase_mips3d)
13543 elf_elfheader (stdoutput)->e_flags |= ???;
13544 #endif
13545 if (file_ase_mdmx)
13546 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13548 /* Set the MIPS ELF ABI flags. */
13549 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13550 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13551 else if (mips_abi == O64_ABI)
13552 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13553 else if (mips_abi == EABI_ABI)
13555 if (!file_mips_gp32)
13556 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13557 else
13558 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13560 else if (mips_abi == N32_ABI)
13561 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13563 /* Nothing to do for N64_ABI. */
13565 if (mips_32bitmode)
13566 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13569 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13571 typedef struct proc {
13572 symbolS *func_sym;
13573 symbolS *func_end_sym;
13574 unsigned long reg_mask;
13575 unsigned long reg_offset;
13576 unsigned long fpreg_mask;
13577 unsigned long fpreg_offset;
13578 unsigned long frame_offset;
13579 unsigned long frame_reg;
13580 unsigned long pc_reg;
13581 } procS;
13583 static procS cur_proc;
13584 static procS *cur_proc_ptr;
13585 static int numprocs;
13587 /* Fill in an rs_align_code fragment. */
13589 void
13590 mips_handle_align (fragS *fragp)
13592 if (fragp->fr_type != rs_align_code)
13593 return;
13595 if (mips_opts.mips16)
13597 static const unsigned char be_nop[] = { 0x65, 0x00 };
13598 static const unsigned char le_nop[] = { 0x00, 0x65 };
13600 int bytes;
13601 char *p;
13603 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13604 p = fragp->fr_literal + fragp->fr_fix;
13606 if (bytes & 1)
13608 *p++ = 0;
13609 fragp->fr_fix++;
13612 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13613 fragp->fr_var = 2;
13616 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13619 static void
13620 md_obj_begin (void)
13624 static void
13625 md_obj_end (void)
13627 /* check for premature end, nesting errors, etc */
13628 if (cur_proc_ptr)
13629 as_warn (_("missing .end at end of assembly"));
13632 static long
13633 get_number (void)
13635 int negative = 0;
13636 long val = 0;
13638 if (*input_line_pointer == '-')
13640 ++input_line_pointer;
13641 negative = 1;
13643 if (!ISDIGIT (*input_line_pointer))
13644 as_bad (_("expected simple number"));
13645 if (input_line_pointer[0] == '0')
13647 if (input_line_pointer[1] == 'x')
13649 input_line_pointer += 2;
13650 while (ISXDIGIT (*input_line_pointer))
13652 val <<= 4;
13653 val |= hex_value (*input_line_pointer++);
13655 return negative ? -val : val;
13657 else
13659 ++input_line_pointer;
13660 while (ISDIGIT (*input_line_pointer))
13662 val <<= 3;
13663 val |= *input_line_pointer++ - '0';
13665 return negative ? -val : val;
13668 if (!ISDIGIT (*input_line_pointer))
13670 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13671 *input_line_pointer, *input_line_pointer);
13672 as_warn (_("invalid number"));
13673 return -1;
13675 while (ISDIGIT (*input_line_pointer))
13677 val *= 10;
13678 val += *input_line_pointer++ - '0';
13680 return negative ? -val : val;
13683 /* The .file directive; just like the usual .file directive, but there
13684 is an initial number which is the ECOFF file index. In the non-ECOFF
13685 case .file implies DWARF-2. */
13687 static void
13688 s_mips_file (int x ATTRIBUTE_UNUSED)
13690 static int first_file_directive = 0;
13692 if (ECOFF_DEBUGGING)
13694 get_number ();
13695 s_app_file (0);
13697 else
13699 char *filename;
13701 filename = dwarf2_directive_file (0);
13703 /* Versions of GCC up to 3.1 start files with a ".file"
13704 directive even for stabs output. Make sure that this
13705 ".file" is handled. Note that you need a version of GCC
13706 after 3.1 in order to support DWARF-2 on MIPS. */
13707 if (filename != NULL && ! first_file_directive)
13709 (void) new_logical_line (filename, -1);
13710 s_app_file_string (filename, 0);
13712 first_file_directive = 1;
13716 /* The .loc directive, implying DWARF-2. */
13718 static void
13719 s_mips_loc (int x ATTRIBUTE_UNUSED)
13721 if (!ECOFF_DEBUGGING)
13722 dwarf2_directive_loc (0);
13725 /* The .end directive. */
13727 static void
13728 s_mips_end (int x ATTRIBUTE_UNUSED)
13730 symbolS *p;
13732 /* Following functions need their own .frame and .cprestore directives. */
13733 mips_frame_reg_valid = 0;
13734 mips_cprestore_valid = 0;
13736 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13738 p = get_symbol ();
13739 demand_empty_rest_of_line ();
13741 else
13742 p = NULL;
13744 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13745 as_warn (_(".end not in text section"));
13747 if (!cur_proc_ptr)
13749 as_warn (_(".end directive without a preceding .ent directive."));
13750 demand_empty_rest_of_line ();
13751 return;
13754 if (p != NULL)
13756 assert (S_GET_NAME (p));
13757 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
13758 as_warn (_(".end symbol does not match .ent symbol."));
13760 if (debug_type == DEBUG_STABS)
13761 stabs_generate_asm_endfunc (S_GET_NAME (p),
13762 S_GET_NAME (p));
13764 else
13765 as_warn (_(".end directive missing or unknown symbol"));
13767 #ifdef OBJ_ELF
13768 /* Create an expression to calculate the size of the function. */
13769 if (p && cur_proc_ptr)
13771 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
13772 expressionS *exp = xmalloc (sizeof (expressionS));
13774 obj->size = exp;
13775 exp->X_op = O_subtract;
13776 exp->X_add_symbol = symbol_temp_new_now ();
13777 exp->X_op_symbol = p;
13778 exp->X_add_number = 0;
13780 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
13783 /* Generate a .pdr section. */
13784 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13785 && mips_flag_pdr)
13787 segT saved_seg = now_seg;
13788 subsegT saved_subseg = now_subseg;
13789 valueT dot;
13790 expressionS exp;
13791 char *fragp;
13793 dot = frag_now_fix ();
13795 #ifdef md_flush_pending_output
13796 md_flush_pending_output ();
13797 #endif
13799 assert (pdr_seg);
13800 subseg_set (pdr_seg, 0);
13802 /* Write the symbol. */
13803 exp.X_op = O_symbol;
13804 exp.X_add_symbol = p;
13805 exp.X_add_number = 0;
13806 emit_expr (&exp, 4);
13808 fragp = frag_more (7 * 4);
13810 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13811 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13812 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13813 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13814 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13815 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13816 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
13818 subseg_set (saved_seg, saved_subseg);
13820 #endif /* OBJ_ELF */
13822 cur_proc_ptr = NULL;
13825 /* The .aent and .ent directives. */
13827 static void
13828 s_mips_ent (int aent)
13830 symbolS *symbolP;
13832 symbolP = get_symbol ();
13833 if (*input_line_pointer == ',')
13834 ++input_line_pointer;
13835 SKIP_WHITESPACE ();
13836 if (ISDIGIT (*input_line_pointer)
13837 || *input_line_pointer == '-')
13838 get_number ();
13840 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13841 as_warn (_(".ent or .aent not in text section."));
13843 if (!aent && cur_proc_ptr)
13844 as_warn (_("missing .end"));
13846 if (!aent)
13848 /* This function needs its own .frame and .cprestore directives. */
13849 mips_frame_reg_valid = 0;
13850 mips_cprestore_valid = 0;
13852 cur_proc_ptr = &cur_proc;
13853 memset (cur_proc_ptr, '\0', sizeof (procS));
13855 cur_proc_ptr->func_sym = symbolP;
13857 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13859 ++numprocs;
13861 if (debug_type == DEBUG_STABS)
13862 stabs_generate_asm_func (S_GET_NAME (symbolP),
13863 S_GET_NAME (symbolP));
13866 demand_empty_rest_of_line ();
13869 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13870 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13871 s_mips_frame is used so that we can set the PDR information correctly.
13872 We can't use the ecoff routines because they make reference to the ecoff
13873 symbol table (in the mdebug section). */
13875 static void
13876 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
13878 #ifdef OBJ_ELF
13879 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13881 long val;
13883 if (cur_proc_ptr == (procS *) NULL)
13885 as_warn (_(".frame outside of .ent"));
13886 demand_empty_rest_of_line ();
13887 return;
13890 cur_proc_ptr->frame_reg = tc_get_register (1);
13892 SKIP_WHITESPACE ();
13893 if (*input_line_pointer++ != ','
13894 || get_absolute_expression_and_terminator (&val) != ',')
13896 as_warn (_("Bad .frame directive"));
13897 --input_line_pointer;
13898 demand_empty_rest_of_line ();
13899 return;
13902 cur_proc_ptr->frame_offset = val;
13903 cur_proc_ptr->pc_reg = tc_get_register (0);
13905 demand_empty_rest_of_line ();
13907 else
13908 #endif /* OBJ_ELF */
13909 s_ignore (ignore);
13912 /* The .fmask and .mask directives. If the mdebug section is present
13913 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13914 embedded targets, s_mips_mask is used so that we can set the PDR
13915 information correctly. We can't use the ecoff routines because they
13916 make reference to the ecoff symbol table (in the mdebug section). */
13918 static void
13919 s_mips_mask (int reg_type)
13921 #ifdef OBJ_ELF
13922 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13924 long mask, off;
13926 if (cur_proc_ptr == (procS *) NULL)
13928 as_warn (_(".mask/.fmask outside of .ent"));
13929 demand_empty_rest_of_line ();
13930 return;
13933 if (get_absolute_expression_and_terminator (&mask) != ',')
13935 as_warn (_("Bad .mask/.fmask directive"));
13936 --input_line_pointer;
13937 demand_empty_rest_of_line ();
13938 return;
13941 off = get_absolute_expression ();
13943 if (reg_type == 'F')
13945 cur_proc_ptr->fpreg_mask = mask;
13946 cur_proc_ptr->fpreg_offset = off;
13948 else
13950 cur_proc_ptr->reg_mask = mask;
13951 cur_proc_ptr->reg_offset = off;
13954 demand_empty_rest_of_line ();
13956 else
13957 #endif /* OBJ_ELF */
13958 s_ignore (reg_type);
13961 /* A table describing all the processors gas knows about. Names are
13962 matched in the order listed.
13964 To ease comparison, please keep this table in the same order as
13965 gcc's mips_cpu_info_table[]. */
13966 static const struct mips_cpu_info mips_cpu_info_table[] =
13968 /* Entries for generic ISAs */
13969 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
13970 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
13971 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
13972 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
13973 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
13974 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
13975 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
13976 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
13977 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
13979 /* MIPS I */
13980 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
13981 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
13982 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
13984 /* MIPS II */
13985 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
13987 /* MIPS III */
13988 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
13989 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
13990 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
13991 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
13992 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
13993 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
13994 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
13995 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
13996 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
13997 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
13998 { "orion", 0, ISA_MIPS3, CPU_R4600 },
13999 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14001 /* MIPS IV */
14002 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14003 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14004 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14005 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
14006 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14007 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
14008 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14009 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14010 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14011 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14012 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14013 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
14014 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
14016 /* MIPS 32 */
14017 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
14018 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14019 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
14021 /* MIPS32 Release 2 */
14022 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14023 { "24k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14024 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14025 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14026 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14028 /* MIPS 64 */
14029 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14030 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
14031 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
14033 /* Broadcom SB-1 CPU core */
14034 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
14036 /* End marker */
14037 { NULL, 0, 0, 0 }
14041 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14042 with a final "000" replaced by "k". Ignore case.
14044 Note: this function is shared between GCC and GAS. */
14046 static bfd_boolean
14047 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
14049 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14050 given++, canonical++;
14052 return ((*given == 0 && *canonical == 0)
14053 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14057 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14058 CPU name. We've traditionally allowed a lot of variation here.
14060 Note: this function is shared between GCC and GAS. */
14062 static bfd_boolean
14063 mips_matching_cpu_name_p (const char *canonical, const char *given)
14065 /* First see if the name matches exactly, or with a final "000"
14066 turned into "k". */
14067 if (mips_strict_matching_cpu_name_p (canonical, given))
14068 return TRUE;
14070 /* If not, try comparing based on numerical designation alone.
14071 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14072 if (TOLOWER (*given) == 'r')
14073 given++;
14074 if (!ISDIGIT (*given))
14075 return FALSE;
14077 /* Skip over some well-known prefixes in the canonical name,
14078 hoping to find a number there too. */
14079 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14080 canonical += 2;
14081 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14082 canonical += 2;
14083 else if (TOLOWER (canonical[0]) == 'r')
14084 canonical += 1;
14086 return mips_strict_matching_cpu_name_p (canonical, given);
14090 /* Parse an option that takes the name of a processor as its argument.
14091 OPTION is the name of the option and CPU_STRING is the argument.
14092 Return the corresponding processor enumeration if the CPU_STRING is
14093 recognized, otherwise report an error and return null.
14095 A similar function exists in GCC. */
14097 static const struct mips_cpu_info *
14098 mips_parse_cpu (const char *option, const char *cpu_string)
14100 const struct mips_cpu_info *p;
14102 /* 'from-abi' selects the most compatible architecture for the given
14103 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14104 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14105 version. Look first at the -mgp options, if given, otherwise base
14106 the choice on MIPS_DEFAULT_64BIT.
14108 Treat NO_ABI like the EABIs. One reason to do this is that the
14109 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14110 architecture. This code picks MIPS I for 'mips' and MIPS III for
14111 'mips64', just as we did in the days before 'from-abi'. */
14112 if (strcasecmp (cpu_string, "from-abi") == 0)
14114 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14115 return mips_cpu_info_from_isa (ISA_MIPS1);
14117 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14118 return mips_cpu_info_from_isa (ISA_MIPS3);
14120 if (file_mips_gp32 >= 0)
14121 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14123 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14124 ? ISA_MIPS3
14125 : ISA_MIPS1);
14128 /* 'default' has traditionally been a no-op. Probably not very useful. */
14129 if (strcasecmp (cpu_string, "default") == 0)
14130 return 0;
14132 for (p = mips_cpu_info_table; p->name != 0; p++)
14133 if (mips_matching_cpu_name_p (p->name, cpu_string))
14134 return p;
14136 as_bad ("Bad value (%s) for %s", cpu_string, option);
14137 return 0;
14140 /* Return the canonical processor information for ISA (a member of the
14141 ISA_MIPS* enumeration). */
14143 static const struct mips_cpu_info *
14144 mips_cpu_info_from_isa (int isa)
14146 int i;
14148 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14149 if (mips_cpu_info_table[i].is_isa
14150 && isa == mips_cpu_info_table[i].isa)
14151 return (&mips_cpu_info_table[i]);
14153 return NULL;
14156 static const struct mips_cpu_info *
14157 mips_cpu_info_from_arch (int arch)
14159 int i;
14161 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14162 if (arch == mips_cpu_info_table[i].cpu)
14163 return (&mips_cpu_info_table[i]);
14165 return NULL;
14168 static void
14169 show (FILE *stream, const char *string, int *col_p, int *first_p)
14171 if (*first_p)
14173 fprintf (stream, "%24s", "");
14174 *col_p = 24;
14176 else
14178 fprintf (stream, ", ");
14179 *col_p += 2;
14182 if (*col_p + strlen (string) > 72)
14184 fprintf (stream, "\n%24s", "");
14185 *col_p = 24;
14188 fprintf (stream, "%s", string);
14189 *col_p += strlen (string);
14191 *first_p = 0;
14194 void
14195 md_show_usage (FILE *stream)
14197 int column, first;
14198 size_t i;
14200 fprintf (stream, _("\
14201 MIPS options:\n\
14202 -EB generate big endian output\n\
14203 -EL generate little endian output\n\
14204 -g, -g2 do not remove unneeded NOPs or swap branches\n\
14205 -G NUM allow referencing objects up to NUM bytes\n\
14206 implicitly with the gp register [default 8]\n"));
14207 fprintf (stream, _("\
14208 -mips1 generate MIPS ISA I instructions\n\
14209 -mips2 generate MIPS ISA II instructions\n\
14210 -mips3 generate MIPS ISA III instructions\n\
14211 -mips4 generate MIPS ISA IV instructions\n\
14212 -mips5 generate MIPS ISA V instructions\n\
14213 -mips32 generate MIPS32 ISA instructions\n\
14214 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
14215 -mips64 generate MIPS64 ISA instructions\n\
14216 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
14217 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14219 first = 1;
14221 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14222 show (stream, mips_cpu_info_table[i].name, &column, &first);
14223 show (stream, "from-abi", &column, &first);
14224 fputc ('\n', stream);
14226 fprintf (stream, _("\
14227 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14228 -no-mCPU don't generate code specific to CPU.\n\
14229 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14231 first = 1;
14233 show (stream, "3900", &column, &first);
14234 show (stream, "4010", &column, &first);
14235 show (stream, "4100", &column, &first);
14236 show (stream, "4650", &column, &first);
14237 fputc ('\n', stream);
14239 fprintf (stream, _("\
14240 -mips16 generate mips16 instructions\n\
14241 -no-mips16 do not generate mips16 instructions\n"));
14242 fprintf (stream, _("\
14243 -mdsp generate DSP instructions\n\
14244 -mno-dsp do not generate DSP instructions\n"));
14245 fprintf (stream, _("\
14246 -mfix-vr4120 work around certain VR4120 errata\n\
14247 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
14248 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14249 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14250 -mno-shared optimize output for executables\n\
14251 -msym32 assume all symbols have 32-bit values\n\
14252 -O0 remove unneeded NOPs, do not swap branches\n\
14253 -O remove unneeded NOPs and swap branches\n\
14254 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14255 --trap, --no-break trap exception on div by 0 and mult overflow\n\
14256 --break, --no-trap break exception on div by 0 and mult overflow\n"));
14257 #ifdef OBJ_ELF
14258 fprintf (stream, _("\
14259 -KPIC, -call_shared generate SVR4 position independent code\n\
14260 -non_shared do not generate position independent code\n\
14261 -xgot assume a 32 bit GOT\n\
14262 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
14263 -mshared, -mno-shared disable/enable .cpload optimization for\n\
14264 non-shared code\n\
14265 -mabi=ABI create ABI conformant object file for:\n"));
14267 first = 1;
14269 show (stream, "32", &column, &first);
14270 show (stream, "o64", &column, &first);
14271 show (stream, "n32", &column, &first);
14272 show (stream, "64", &column, &first);
14273 show (stream, "eabi", &column, &first);
14275 fputc ('\n', stream);
14277 fprintf (stream, _("\
14278 -32 create o32 ABI object file (default)\n\
14279 -n32 create n32 ABI object file\n\
14280 -64 create 64 ABI object file\n"));
14281 #endif
14284 enum dwarf2_format
14285 mips_dwarf2_format (void)
14287 if (mips_abi == N64_ABI)
14289 #ifdef TE_IRIX
14290 return dwarf2_format_64bit_irix;
14291 #else
14292 return dwarf2_format_64bit;
14293 #endif
14295 else
14296 return dwarf2_format_32bit;
14300 mips_dwarf2_addr_size (void)
14302 if (mips_abi == N64_ABI)
14303 return 8;
14304 else
14305 return 4;
14308 /* Standard calling conventions leave the CFA at SP on entry. */
14309 void
14310 mips_cfi_frame_initial_instructions (void)
14312 cfi_add_CFA_def_cfa_register (SP);