2006-05-24 Paul Brook <paul@codesourcery.com>
[binutils.git] / gas / config / tc-mips.c
blobd19a14987c25f1e77b7dc65f9a45d1dbc2a6ab08
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 occurred 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_smartmips;
197 int ase_dsp;
198 int ase_mt;
199 /* Whether we are assembling for the mips16 processor. 0 if we are
200 not, 1 if we are, and -1 if the value has not been initialized.
201 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
202 -nomips16 command line options, and the default CPU. */
203 int mips16;
204 /* Non-zero if we should not reorder instructions. Changed by `.set
205 reorder' and `.set noreorder'. */
206 int noreorder;
207 /* Non-zero if we should not permit the $at ($1) register to be used
208 in instructions. Changed by `.set at' and `.set noat'. */
209 int noat;
210 /* Non-zero if we should warn when a macro instruction expands into
211 more than one machine instruction. Changed by `.set nomacro' and
212 `.set macro'. */
213 int warn_about_macros;
214 /* Non-zero if we should not move instructions. Changed by `.set
215 move', `.set volatile', `.set nomove', and `.set novolatile'. */
216 int nomove;
217 /* Non-zero if we should not optimize branches by moving the target
218 of the branch into the delay slot. Actually, we don't perform
219 this optimization anyhow. Changed by `.set bopt' and `.set
220 nobopt'. */
221 int nobopt;
222 /* Non-zero if we should not autoextend mips16 instructions.
223 Changed by `.set autoextend' and `.set noautoextend'. */
224 int noautoextend;
225 /* Restrict general purpose registers and floating point registers
226 to 32 bit. This is initially determined when -mgp32 or -mfp32
227 is passed but can changed if the assembler code uses .set mipsN. */
228 int gp32;
229 int fp32;
230 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
231 command line option, and the default CPU. */
232 int arch;
233 /* True if ".set sym32" is in effect. */
234 bfd_boolean sym32;
237 /* True if -mgp32 was passed. */
238 static int file_mips_gp32 = -1;
240 /* True if -mfp32 was passed. */
241 static int file_mips_fp32 = -1;
243 /* This is the struct we use to hold the current set of options. Note
244 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
245 -1 to indicate that they have not been initialized. */
247 static struct mips_set_options mips_opts =
249 ISA_UNKNOWN, -1, -1, 0, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN, FALSE
252 /* These variables are filled in with the masks of registers used.
253 The object format code reads them and puts them in the appropriate
254 place. */
255 unsigned long mips_gprmask;
256 unsigned long mips_cprmask[4];
258 /* MIPS ISA we are using for this output file. */
259 static int file_mips_isa = ISA_UNKNOWN;
261 /* True if -mips16 was passed or implied by arguments passed on the
262 command line (e.g., by -march). */
263 static int file_ase_mips16;
265 /* True if -mips3d was passed or implied by arguments passed on the
266 command line (e.g., by -march). */
267 static int file_ase_mips3d;
269 /* True if -mdmx was passed or implied by arguments passed on the
270 command line (e.g., by -march). */
271 static int file_ase_mdmx;
273 /* True if -msmartmips was passed or implied by arguments passed on the
274 command line (e.g., by -march). */
275 static int file_ase_smartmips;
277 #define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32 \
278 || mips_opts.isa == ISA_MIPS32R2)
280 /* True if -mdsp was passed or implied by arguments passed on the
281 command line (e.g., by -march). */
282 static int file_ase_dsp;
284 #define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2 \
285 || mips_opts.isa == ISA_MIPS64R2)
287 /* True if -mmt was passed or implied by arguments passed on the
288 command line (e.g., by -march). */
289 static int file_ase_mt;
291 #define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2 \
292 || mips_opts.isa == ISA_MIPS64R2)
294 /* The argument of the -march= flag. The architecture we are assembling. */
295 static int file_mips_arch = CPU_UNKNOWN;
296 static const char *mips_arch_string;
298 /* The argument of the -mtune= flag. The architecture for which we
299 are optimizing. */
300 static int mips_tune = CPU_UNKNOWN;
301 static const char *mips_tune_string;
303 /* True when generating 32-bit code for a 64-bit processor. */
304 static int mips_32bitmode = 0;
306 /* True if the given ABI requires 32-bit registers. */
307 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
309 /* Likewise 64-bit registers. */
310 #define ABI_NEEDS_64BIT_REGS(ABI) \
311 ((ABI) == N32_ABI \
312 || (ABI) == N64_ABI \
313 || (ABI) == O64_ABI)
315 /* Return true if ISA supports 64 bit wide gp registers. */
316 #define ISA_HAS_64BIT_REGS(ISA) \
317 ((ISA) == ISA_MIPS3 \
318 || (ISA) == ISA_MIPS4 \
319 || (ISA) == ISA_MIPS5 \
320 || (ISA) == ISA_MIPS64 \
321 || (ISA) == ISA_MIPS64R2)
323 /* Return true if ISA supports 64 bit wide float registers. */
324 #define ISA_HAS_64BIT_FPRS(ISA) \
325 ((ISA) == ISA_MIPS3 \
326 || (ISA) == ISA_MIPS4 \
327 || (ISA) == ISA_MIPS5 \
328 || (ISA) == ISA_MIPS32R2 \
329 || (ISA) == ISA_MIPS64 \
330 || (ISA) == ISA_MIPS64R2)
332 /* Return true if ISA supports 64-bit right rotate (dror et al.)
333 instructions. */
334 #define ISA_HAS_DROR(ISA) \
335 ((ISA) == ISA_MIPS64R2)
337 /* Return true if ISA supports 32-bit right rotate (ror et al.)
338 instructions. */
339 #define ISA_HAS_ROR(ISA) \
340 ((ISA) == ISA_MIPS32R2 \
341 || (ISA) == ISA_MIPS64R2 \
342 || mips_opts.ase_smartmips)
344 /* Return true if ISA supports single-precision floats in odd registers. */
345 #define ISA_HAS_ODD_SINGLE_FPR(ISA) \
346 ((ISA) == ISA_MIPS32 \
347 || (ISA) == ISA_MIPS32R2 \
348 || (ISA) == ISA_MIPS64 \
349 || (ISA) == ISA_MIPS64R2)
351 /* Return true if ISA supports move to/from high part of a 64-bit
352 floating-point register. */
353 #define ISA_HAS_MXHC1(ISA) \
354 ((ISA) == ISA_MIPS32R2 \
355 || (ISA) == ISA_MIPS64R2)
357 #define HAVE_32BIT_GPRS \
358 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
360 #define HAVE_32BIT_FPRS \
361 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
363 #define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
364 #define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
366 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
368 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
370 /* True if relocations are stored in-place. */
371 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
373 /* The ABI-derived address size. */
374 #define HAVE_64BIT_ADDRESSES \
375 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
376 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
378 /* The size of symbolic constants (i.e., expressions of the form
379 "SYMBOL" or "SYMBOL + OFFSET"). */
380 #define HAVE_32BIT_SYMBOLS \
381 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
382 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
384 /* Addresses are loaded in different ways, depending on the address size
385 in use. The n32 ABI Documentation also mandates the use of additions
386 with overflow checking, but existing implementations don't follow it. */
387 #define ADDRESS_ADD_INSN \
388 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
390 #define ADDRESS_ADDI_INSN \
391 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
393 #define ADDRESS_LOAD_INSN \
394 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
396 #define ADDRESS_STORE_INSN \
397 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
399 /* Return true if the given CPU supports the MIPS16 ASE. */
400 #define CPU_HAS_MIPS16(cpu) \
401 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
402 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
404 /* Return true if the given CPU supports the MIPS3D ASE. */
405 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
408 /* Return true if the given CPU supports the MDMX ASE. */
409 #define CPU_HAS_MDMX(cpu) (FALSE \
412 /* Return true if the given CPU supports the DSP ASE. */
413 #define CPU_HAS_DSP(cpu) (FALSE \
416 /* Return true if the given CPU supports the MT ASE. */
417 #define CPU_HAS_MT(cpu) (FALSE \
420 /* True if CPU has a dror instruction. */
421 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
423 /* True if CPU has a ror instruction. */
424 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
426 /* True if mflo and mfhi can be immediately followed by instructions
427 which write to the HI and LO registers.
429 According to MIPS specifications, MIPS ISAs I, II, and III need
430 (at least) two instructions between the reads of HI/LO and
431 instructions which write them, and later ISAs do not. Contradicting
432 the MIPS specifications, some MIPS IV processor user manuals (e.g.
433 the UM for the NEC Vr5000) document needing the instructions between
434 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
435 MIPS64 and later ISAs to have the interlocks, plus any specific
436 earlier-ISA CPUs for which CPU documentation declares that the
437 instructions are really interlocked. */
438 #define hilo_interlocks \
439 (mips_opts.isa == ISA_MIPS32 \
440 || mips_opts.isa == ISA_MIPS32R2 \
441 || mips_opts.isa == ISA_MIPS64 \
442 || mips_opts.isa == ISA_MIPS64R2 \
443 || mips_opts.arch == CPU_R4010 \
444 || mips_opts.arch == CPU_R10000 \
445 || mips_opts.arch == CPU_R12000 \
446 || mips_opts.arch == CPU_RM7000 \
447 || mips_opts.arch == CPU_VR5500 \
450 /* Whether the processor uses hardware interlocks to protect reads
451 from the GPRs after they are loaded from memory, and thus does not
452 require nops to be inserted. This applies to instructions marked
453 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
454 level I. */
455 #define gpr_interlocks \
456 (mips_opts.isa != ISA_MIPS1 \
457 || mips_opts.arch == CPU_R3900)
459 /* Whether the processor uses hardware interlocks to avoid delays
460 required by coprocessor instructions, and thus does not require
461 nops to be inserted. This applies to instructions marked
462 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
463 between instructions marked INSN_WRITE_COND_CODE and ones marked
464 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
465 levels I, II, and III. */
466 /* Itbl support may require additional care here. */
467 #define cop_interlocks \
468 ((mips_opts.isa != ISA_MIPS1 \
469 && mips_opts.isa != ISA_MIPS2 \
470 && mips_opts.isa != ISA_MIPS3) \
471 || mips_opts.arch == CPU_R4300 \
474 /* Whether the processor uses hardware interlocks to protect reads
475 from coprocessor registers after they are loaded from memory, and
476 thus does not require nops to be inserted. This applies to
477 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
478 requires at MIPS ISA level I. */
479 #define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
481 /* Is this a mfhi or mflo instruction? */
482 #define MF_HILO_INSN(PINFO) \
483 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
485 /* MIPS PIC level. */
487 enum mips_pic_level mips_pic;
489 /* 1 if we should generate 32 bit offsets from the $gp register in
490 SVR4_PIC mode. Currently has no meaning in other modes. */
491 static int mips_big_got = 0;
493 /* 1 if trap instructions should used for overflow rather than break
494 instructions. */
495 static int mips_trap = 0;
497 /* 1 if double width floating point constants should not be constructed
498 by assembling two single width halves into two single width floating
499 point registers which just happen to alias the double width destination
500 register. On some architectures this aliasing can be disabled by a bit
501 in the status register, and the setting of this bit cannot be determined
502 automatically at assemble time. */
503 static int mips_disable_float_construction;
505 /* Non-zero if any .set noreorder directives were used. */
507 static int mips_any_noreorder;
509 /* Non-zero if nops should be inserted when the register referenced in
510 an mfhi/mflo instruction is read in the next two instructions. */
511 static int mips_7000_hilo_fix;
513 /* The size of the small data section. */
514 static unsigned int g_switch_value = 8;
515 /* Whether the -G option was used. */
516 static int g_switch_seen = 0;
518 #define N_RMASK 0xc4
519 #define N_VFP 0xd4
521 /* If we can determine in advance that GP optimization won't be
522 possible, we can skip the relaxation stuff that tries to produce
523 GP-relative references. This makes delay slot optimization work
524 better.
526 This function can only provide a guess, but it seems to work for
527 gcc output. It needs to guess right for gcc, otherwise gcc
528 will put what it thinks is a GP-relative instruction in a branch
529 delay slot.
531 I don't know if a fix is needed for the SVR4_PIC mode. I've only
532 fixed it for the non-PIC mode. KR 95/04/07 */
533 static int nopic_need_relax (symbolS *, int);
535 /* handle of the OPCODE hash table */
536 static struct hash_control *op_hash = NULL;
538 /* The opcode hash table we use for the mips16. */
539 static struct hash_control *mips16_op_hash = NULL;
541 /* This array holds the chars that always start a comment. If the
542 pre-processor is disabled, these aren't very useful */
543 const char comment_chars[] = "#";
545 /* This array holds the chars that only start a comment at the beginning of
546 a line. If the line seems to have the form '# 123 filename'
547 .line and .file directives will appear in the pre-processed output */
548 /* Note that input_file.c hand checks for '#' at the beginning of the
549 first line of the input file. This is because the compiler outputs
550 #NO_APP at the beginning of its output. */
551 /* Also note that C style comments are always supported. */
552 const char line_comment_chars[] = "#";
554 /* This array holds machine specific line separator characters. */
555 const char line_separator_chars[] = ";";
557 /* Chars that can be used to separate mant from exp in floating point nums */
558 const char EXP_CHARS[] = "eE";
560 /* Chars that mean this number is a floating point constant */
561 /* As in 0f12.456 */
562 /* or 0d1.2345e12 */
563 const char FLT_CHARS[] = "rRsSfFdDxXpP";
565 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
566 changed in read.c . Ideally it shouldn't have to know about it at all,
567 but nothing is ideal around here.
570 static char *insn_error;
572 static int auto_align = 1;
574 /* When outputting SVR4 PIC code, the assembler needs to know the
575 offset in the stack frame from which to restore the $gp register.
576 This is set by the .cprestore pseudo-op, and saved in this
577 variable. */
578 static offsetT mips_cprestore_offset = -1;
580 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
581 more optimizations, it can use a register value instead of a memory-saved
582 offset and even an other register than $gp as global pointer. */
583 static offsetT mips_cpreturn_offset = -1;
584 static int mips_cpreturn_register = -1;
585 static int mips_gp_register = GP;
586 static int mips_gprel_offset = 0;
588 /* Whether mips_cprestore_offset has been set in the current function
589 (or whether it has already been warned about, if not). */
590 static int mips_cprestore_valid = 0;
592 /* This is the register which holds the stack frame, as set by the
593 .frame pseudo-op. This is needed to implement .cprestore. */
594 static int mips_frame_reg = SP;
596 /* Whether mips_frame_reg has been set in the current function
597 (or whether it has already been warned about, if not). */
598 static int mips_frame_reg_valid = 0;
600 /* To output NOP instructions correctly, we need to keep information
601 about the previous two instructions. */
603 /* Whether we are optimizing. The default value of 2 means to remove
604 unneeded NOPs and swap branch instructions when possible. A value
605 of 1 means to not swap branches. A value of 0 means to always
606 insert NOPs. */
607 static int mips_optimize = 2;
609 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
610 equivalent to seeing no -g option at all. */
611 static int mips_debug = 0;
613 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
614 #define MAX_VR4130_NOPS 4
616 /* The maximum number of NOPs needed to fill delay slots. */
617 #define MAX_DELAY_NOPS 2
619 /* The maximum number of NOPs needed for any purpose. */
620 #define MAX_NOPS 4
622 /* A list of previous instructions, with index 0 being the most recent.
623 We need to look back MAX_NOPS instructions when filling delay slots
624 or working around processor errata. We need to look back one
625 instruction further if we're thinking about using history[0] to
626 fill a branch delay slot. */
627 static struct mips_cl_insn history[1 + MAX_NOPS];
629 /* Nop instructions used by emit_nop. */
630 static struct mips_cl_insn nop_insn, mips16_nop_insn;
632 /* The appropriate nop for the current mode. */
633 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn : &nop_insn)
635 /* If this is set, it points to a frag holding nop instructions which
636 were inserted before the start of a noreorder section. If those
637 nops turn out to be unnecessary, the size of the frag can be
638 decreased. */
639 static fragS *prev_nop_frag;
641 /* The number of nop instructions we created in prev_nop_frag. */
642 static int prev_nop_frag_holds;
644 /* The number of nop instructions that we know we need in
645 prev_nop_frag. */
646 static int prev_nop_frag_required;
648 /* The number of instructions we've seen since prev_nop_frag. */
649 static int prev_nop_frag_since;
651 /* For ECOFF and ELF, relocations against symbols are done in two
652 parts, with a HI relocation and a LO relocation. Each relocation
653 has only 16 bits of space to store an addend. This means that in
654 order for the linker to handle carries correctly, it must be able
655 to locate both the HI and the LO relocation. This means that the
656 relocations must appear in order in the relocation table.
658 In order to implement this, we keep track of each unmatched HI
659 relocation. We then sort them so that they immediately precede the
660 corresponding LO relocation. */
662 struct mips_hi_fixup
664 /* Next HI fixup. */
665 struct mips_hi_fixup *next;
666 /* This fixup. */
667 fixS *fixp;
668 /* The section this fixup is in. */
669 segT seg;
672 /* The list of unmatched HI relocs. */
674 static struct mips_hi_fixup *mips_hi_fixup_list;
676 /* The frag containing the last explicit relocation operator.
677 Null if explicit relocations have not been used. */
679 static fragS *prev_reloc_op_frag;
681 /* Map normal MIPS register numbers to mips16 register numbers. */
683 #define X ILLEGAL_REG
684 static const int mips32_to_16_reg_map[] =
686 X, X, 2, 3, 4, 5, 6, 7,
687 X, X, X, X, X, X, X, X,
688 0, 1, X, X, X, X, X, X,
689 X, X, X, X, X, X, X, X
691 #undef X
693 /* Map mips16 register numbers to normal MIPS register numbers. */
695 static const unsigned int mips16_to_32_reg_map[] =
697 16, 17, 2, 3, 4, 5, 6, 7
700 /* Classifies the kind of instructions we're interested in when
701 implementing -mfix-vr4120. */
702 enum fix_vr4120_class {
703 FIX_VR4120_MACC,
704 FIX_VR4120_DMACC,
705 FIX_VR4120_MULT,
706 FIX_VR4120_DMULT,
707 FIX_VR4120_DIV,
708 FIX_VR4120_MTHILO,
709 NUM_FIX_VR4120_CLASSES
712 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
713 there must be at least one other instruction between an instruction
714 of type X and an instruction of type Y. */
715 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
717 /* True if -mfix-vr4120 is in force. */
718 static int mips_fix_vr4120;
720 /* ...likewise -mfix-vr4130. */
721 static int mips_fix_vr4130;
723 /* We don't relax branches by default, since this causes us to expand
724 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
725 fail to compute the offset before expanding the macro to the most
726 efficient expansion. */
728 static int mips_relax_branch;
730 /* The expansion of many macros depends on the type of symbol that
731 they refer to. For example, when generating position-dependent code,
732 a macro that refers to a symbol may have two different expansions,
733 one which uses GP-relative addresses and one which uses absolute
734 addresses. When generating SVR4-style PIC, a macro may have
735 different expansions for local and global symbols.
737 We handle these situations by generating both sequences and putting
738 them in variant frags. In position-dependent code, the first sequence
739 will be the GP-relative one and the second sequence will be the
740 absolute one. In SVR4 PIC, the first sequence will be for global
741 symbols and the second will be for local symbols.
743 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
744 SECOND are the lengths of the two sequences in bytes. These fields
745 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
746 the subtype has the following flags:
748 RELAX_USE_SECOND
749 Set if it has been decided that we should use the second
750 sequence instead of the first.
752 RELAX_SECOND_LONGER
753 Set in the first variant frag if the macro's second implementation
754 is longer than its first. This refers to the macro as a whole,
755 not an individual relaxation.
757 RELAX_NOMACRO
758 Set in the first variant frag if the macro appeared in a .set nomacro
759 block and if one alternative requires a warning but the other does not.
761 RELAX_DELAY_SLOT
762 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
763 delay slot.
765 The frag's "opcode" points to the first fixup for relaxable code.
767 Relaxable macros are generated using a sequence such as:
769 relax_start (SYMBOL);
770 ... generate first expansion ...
771 relax_switch ();
772 ... generate second expansion ...
773 relax_end ();
775 The code and fixups for the unwanted alternative are discarded
776 by md_convert_frag. */
777 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
779 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
780 #define RELAX_SECOND(X) ((X) & 0xff)
781 #define RELAX_USE_SECOND 0x10000
782 #define RELAX_SECOND_LONGER 0x20000
783 #define RELAX_NOMACRO 0x40000
784 #define RELAX_DELAY_SLOT 0x80000
786 /* Branch without likely bit. If label is out of range, we turn:
788 beq reg1, reg2, label
789 delay slot
791 into
793 bne reg1, reg2, 0f
795 j label
796 0: delay slot
798 with the following opcode replacements:
800 beq <-> bne
801 blez <-> bgtz
802 bltz <-> bgez
803 bc1f <-> bc1t
805 bltzal <-> bgezal (with jal label instead of j label)
807 Even though keeping the delay slot instruction in the delay slot of
808 the branch would be more efficient, it would be very tricky to do
809 correctly, because we'd have to introduce a variable frag *after*
810 the delay slot instruction, and expand that instead. Let's do it
811 the easy way for now, even if the branch-not-taken case now costs
812 one additional instruction. Out-of-range branches are not supposed
813 to be common, anyway.
815 Branch likely. If label is out of range, we turn:
817 beql reg1, reg2, label
818 delay slot (annulled if branch not taken)
820 into
822 beql reg1, reg2, 1f
824 beql $0, $0, 2f
826 1: j[al] label
827 delay slot (executed only if branch taken)
830 It would be possible to generate a shorter sequence by losing the
831 likely bit, generating something like:
833 bne reg1, reg2, 0f
835 j[al] label
836 delay slot (executed only if branch taken)
839 beql -> bne
840 bnel -> beq
841 blezl -> bgtz
842 bgtzl -> blez
843 bltzl -> bgez
844 bgezl -> bltz
845 bc1fl -> bc1t
846 bc1tl -> bc1f
848 bltzall -> bgezal (with jal label instead of j label)
849 bgezall -> bltzal (ditto)
852 but it's not clear that it would actually improve performance. */
853 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
854 ((relax_substateT) \
855 (0xc0000000 \
856 | ((toofar) ? 1 : 0) \
857 | ((link) ? 2 : 0) \
858 | ((likely) ? 4 : 0) \
859 | ((uncond) ? 8 : 0)))
860 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
861 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
862 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
863 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
864 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
866 /* For mips16 code, we use an entirely different form of relaxation.
867 mips16 supports two versions of most instructions which take
868 immediate values: a small one which takes some small value, and a
869 larger one which takes a 16 bit value. Since branches also follow
870 this pattern, relaxing these values is required.
872 We can assemble both mips16 and normal MIPS code in a single
873 object. Therefore, we need to support this type of relaxation at
874 the same time that we support the relaxation described above. We
875 use the high bit of the subtype field to distinguish these cases.
877 The information we store for this type of relaxation is the
878 argument code found in the opcode file for this relocation, whether
879 the user explicitly requested a small or extended form, and whether
880 the relocation is in a jump or jal delay slot. That tells us the
881 size of the value, and how it should be stored. We also store
882 whether the fragment is considered to be extended or not. We also
883 store whether this is known to be a branch to a different section,
884 whether we have tried to relax this frag yet, and whether we have
885 ever extended a PC relative fragment because of a shift count. */
886 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
887 (0x80000000 \
888 | ((type) & 0xff) \
889 | ((small) ? 0x100 : 0) \
890 | ((ext) ? 0x200 : 0) \
891 | ((dslot) ? 0x400 : 0) \
892 | ((jal_dslot) ? 0x800 : 0))
893 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
894 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
895 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
896 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
897 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
898 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
899 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
900 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
901 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
902 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
903 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
904 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
906 /* Is the given value a sign-extended 32-bit value? */
907 #define IS_SEXT_32BIT_NUM(x) \
908 (((x) &~ (offsetT) 0x7fffffff) == 0 \
909 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
911 /* Is the given value a sign-extended 16-bit value? */
912 #define IS_SEXT_16BIT_NUM(x) \
913 (((x) &~ (offsetT) 0x7fff) == 0 \
914 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
916 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
917 #define IS_ZEXT_32BIT_NUM(x) \
918 (((x) &~ (offsetT) 0xffffffff) == 0 \
919 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
921 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
922 VALUE << SHIFT. VALUE is evaluated exactly once. */
923 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
924 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
925 | (((VALUE) & (MASK)) << (SHIFT)))
927 /* Extract bits MASK << SHIFT from STRUCT and shift them right
928 SHIFT places. */
929 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
930 (((STRUCT) >> (SHIFT)) & (MASK))
932 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
933 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
935 include/opcode/mips.h specifies operand fields using the macros
936 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
937 with "MIPS16OP" instead of "OP". */
938 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
939 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
940 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
941 INSERT_BITS ((INSN).insn_opcode, VALUE, \
942 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
944 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
945 #define EXTRACT_OPERAND(FIELD, INSN) \
946 EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD)
947 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
948 EXTRACT_BITS ((INSN).insn_opcode, \
949 MIPS16OP_MASK_##FIELD, \
950 MIPS16OP_SH_##FIELD)
952 /* Global variables used when generating relaxable macros. See the
953 comment above RELAX_ENCODE for more details about how relaxation
954 is used. */
955 static struct {
956 /* 0 if we're not emitting a relaxable macro.
957 1 if we're emitting the first of the two relaxation alternatives.
958 2 if we're emitting the second alternative. */
959 int sequence;
961 /* The first relaxable fixup in the current frag. (In other words,
962 the first fixup that refers to relaxable code.) */
963 fixS *first_fixup;
965 /* sizes[0] says how many bytes of the first alternative are stored in
966 the current frag. Likewise sizes[1] for the second alternative. */
967 unsigned int sizes[2];
969 /* The symbol on which the choice of sequence depends. */
970 symbolS *symbol;
971 } mips_relax;
973 /* Global variables used to decide whether a macro needs a warning. */
974 static struct {
975 /* True if the macro is in a branch delay slot. */
976 bfd_boolean delay_slot_p;
978 /* For relaxable macros, sizes[0] is the length of the first alternative
979 in bytes and sizes[1] is the length of the second alternative.
980 For non-relaxable macros, both elements give the length of the
981 macro in bytes. */
982 unsigned int sizes[2];
984 /* The first variant frag for this macro. */
985 fragS *first_frag;
986 } mips_macro_warning;
988 /* Prototypes for static functions. */
990 #define internalError() \
991 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
993 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
995 static void append_insn
996 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
997 static void mips_no_prev_insn (void);
998 static void mips16_macro_build
999 (expressionS *, const char *, const char *, va_list);
1000 static void load_register (int, expressionS *, int);
1001 static void macro_start (void);
1002 static void macro_end (void);
1003 static void macro (struct mips_cl_insn * ip);
1004 static void mips16_macro (struct mips_cl_insn * ip);
1005 #ifdef LOSING_COMPILER
1006 static void macro2 (struct mips_cl_insn * ip);
1007 #endif
1008 static void mips_ip (char *str, struct mips_cl_insn * ip);
1009 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1010 static void mips16_immed
1011 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
1012 unsigned long *, bfd_boolean *, unsigned short *);
1013 static size_t my_getSmallExpression
1014 (expressionS *, bfd_reloc_code_real_type *, char *);
1015 static void my_getExpression (expressionS *, char *);
1016 static void s_align (int);
1017 static void s_change_sec (int);
1018 static void s_change_section (int);
1019 static void s_cons (int);
1020 static void s_float_cons (int);
1021 static void s_mips_globl (int);
1022 static void s_option (int);
1023 static void s_mipsset (int);
1024 static void s_abicalls (int);
1025 static void s_cpload (int);
1026 static void s_cpsetup (int);
1027 static void s_cplocal (int);
1028 static void s_cprestore (int);
1029 static void s_cpreturn (int);
1030 static void s_gpvalue (int);
1031 static void s_gpword (int);
1032 static void s_gpdword (int);
1033 static void s_cpadd (int);
1034 static void s_insn (int);
1035 static void md_obj_begin (void);
1036 static void md_obj_end (void);
1037 static void s_mips_ent (int);
1038 static void s_mips_end (int);
1039 static void s_mips_frame (int);
1040 static void s_mips_mask (int reg_type);
1041 static void s_mips_stab (int);
1042 static void s_mips_weakext (int);
1043 static void s_mips_file (int);
1044 static void s_mips_loc (int);
1045 static bfd_boolean pic_need_relax (symbolS *, asection *);
1046 static int relaxed_branch_length (fragS *, asection *, int);
1047 static int validate_mips_insn (const struct mips_opcode *);
1049 /* Table and functions used to map between CPU/ISA names, and
1050 ISA levels, and CPU numbers. */
1052 struct mips_cpu_info
1054 const char *name; /* CPU or ISA name. */
1055 int flags; /* ASEs available, or ISA flag. */
1056 int isa; /* ISA level. */
1057 int cpu; /* CPU number (default CPU if ISA). */
1060 #define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1061 #define MIPS_CPU_ASE_SMARTMIPS 0x0002 /* CPU implements SmartMIPS ASE */
1062 #define MIPS_CPU_ASE_DSP 0x0004 /* CPU implements DSP ASE */
1063 #define MIPS_CPU_ASE_MT 0x0008 /* CPU implements MT ASE */
1064 #define MIPS_CPU_ASE_MIPS3D 0x0010 /* CPU implements MIPS-3D ASE */
1065 #define MIPS_CPU_ASE_MDMX 0x0020 /* CPU implements MDMX ASE */
1067 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1068 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1069 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1071 /* Pseudo-op table.
1073 The following pseudo-ops from the Kane and Heinrich MIPS book
1074 should be defined here, but are currently unsupported: .alias,
1075 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1077 The following pseudo-ops from the Kane and Heinrich MIPS book are
1078 specific to the type of debugging information being generated, and
1079 should be defined by the object format: .aent, .begin, .bend,
1080 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1081 .vreg.
1083 The following pseudo-ops from the Kane and Heinrich MIPS book are
1084 not MIPS CPU specific, but are also not specific to the object file
1085 format. This file is probably the best place to define them, but
1086 they are not currently supported: .asm0, .endr, .lab, .repeat,
1087 .struct. */
1089 static const pseudo_typeS mips_pseudo_table[] =
1091 /* MIPS specific pseudo-ops. */
1092 {"option", s_option, 0},
1093 {"set", s_mipsset, 0},
1094 {"rdata", s_change_sec, 'r'},
1095 {"sdata", s_change_sec, 's'},
1096 {"livereg", s_ignore, 0},
1097 {"abicalls", s_abicalls, 0},
1098 {"cpload", s_cpload, 0},
1099 {"cpsetup", s_cpsetup, 0},
1100 {"cplocal", s_cplocal, 0},
1101 {"cprestore", s_cprestore, 0},
1102 {"cpreturn", s_cpreturn, 0},
1103 {"gpvalue", s_gpvalue, 0},
1104 {"gpword", s_gpword, 0},
1105 {"gpdword", s_gpdword, 0},
1106 {"cpadd", s_cpadd, 0},
1107 {"insn", s_insn, 0},
1109 /* Relatively generic pseudo-ops that happen to be used on MIPS
1110 chips. */
1111 {"asciiz", stringer, 1},
1112 {"bss", s_change_sec, 'b'},
1113 {"err", s_err, 0},
1114 {"half", s_cons, 1},
1115 {"dword", s_cons, 3},
1116 {"weakext", s_mips_weakext, 0},
1118 /* These pseudo-ops are defined in read.c, but must be overridden
1119 here for one reason or another. */
1120 {"align", s_align, 0},
1121 {"byte", s_cons, 0},
1122 {"data", s_change_sec, 'd'},
1123 {"double", s_float_cons, 'd'},
1124 {"float", s_float_cons, 'f'},
1125 {"globl", s_mips_globl, 0},
1126 {"global", s_mips_globl, 0},
1127 {"hword", s_cons, 1},
1128 {"int", s_cons, 2},
1129 {"long", s_cons, 2},
1130 {"octa", s_cons, 4},
1131 {"quad", s_cons, 3},
1132 {"section", s_change_section, 0},
1133 {"short", s_cons, 1},
1134 {"single", s_float_cons, 'f'},
1135 {"stabn", s_mips_stab, 'n'},
1136 {"text", s_change_sec, 't'},
1137 {"word", s_cons, 2},
1139 { "extern", ecoff_directive_extern, 0},
1141 { NULL, NULL, 0 },
1144 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1146 /* These pseudo-ops should be defined by the object file format.
1147 However, a.out doesn't support them, so we have versions here. */
1148 {"aent", s_mips_ent, 1},
1149 {"bgnb", s_ignore, 0},
1150 {"end", s_mips_end, 0},
1151 {"endb", s_ignore, 0},
1152 {"ent", s_mips_ent, 0},
1153 {"file", s_mips_file, 0},
1154 {"fmask", s_mips_mask, 'F'},
1155 {"frame", s_mips_frame, 0},
1156 {"loc", s_mips_loc, 0},
1157 {"mask", s_mips_mask, 'R'},
1158 {"verstamp", s_ignore, 0},
1159 { NULL, NULL, 0 },
1162 extern void pop_insert (const pseudo_typeS *);
1164 void
1165 mips_pop_insert (void)
1167 pop_insert (mips_pseudo_table);
1168 if (! ECOFF_DEBUGGING)
1169 pop_insert (mips_nonecoff_pseudo_table);
1172 /* Symbols labelling the current insn. */
1174 struct insn_label_list
1176 struct insn_label_list *next;
1177 symbolS *label;
1180 static struct insn_label_list *insn_labels;
1181 static struct insn_label_list *free_insn_labels;
1183 static void mips_clear_insn_labels (void);
1185 static inline void
1186 mips_clear_insn_labels (void)
1188 register struct insn_label_list **pl;
1190 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1192 *pl = insn_labels;
1193 insn_labels = NULL;
1196 static char *expr_end;
1198 /* Expressions which appear in instructions. These are set by
1199 mips_ip. */
1201 static expressionS imm_expr;
1202 static expressionS imm2_expr;
1203 static expressionS offset_expr;
1205 /* Relocs associated with imm_expr and offset_expr. */
1207 static bfd_reloc_code_real_type imm_reloc[3]
1208 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1209 static bfd_reloc_code_real_type offset_reloc[3]
1210 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1212 /* These are set by mips16_ip if an explicit extension is used. */
1214 static bfd_boolean mips16_small, mips16_ext;
1216 #ifdef OBJ_ELF
1217 /* The pdr segment for per procedure frame/regmask info. Not used for
1218 ECOFF debugging. */
1220 static segT pdr_seg;
1221 #endif
1223 /* The default target format to use. */
1225 const char *
1226 mips_target_format (void)
1228 switch (OUTPUT_FLAVOR)
1230 case bfd_target_ecoff_flavour:
1231 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1232 case bfd_target_coff_flavour:
1233 return "pe-mips";
1234 case bfd_target_elf_flavour:
1235 #ifdef TE_VXWORKS
1236 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1237 return (target_big_endian
1238 ? "elf32-bigmips-vxworks"
1239 : "elf32-littlemips-vxworks");
1240 #endif
1241 #ifdef TE_TMIPS
1242 /* This is traditional mips. */
1243 return (target_big_endian
1244 ? (HAVE_64BIT_OBJECTS
1245 ? "elf64-tradbigmips"
1246 : (HAVE_NEWABI
1247 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1248 : (HAVE_64BIT_OBJECTS
1249 ? "elf64-tradlittlemips"
1250 : (HAVE_NEWABI
1251 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1252 #else
1253 return (target_big_endian
1254 ? (HAVE_64BIT_OBJECTS
1255 ? "elf64-bigmips"
1256 : (HAVE_NEWABI
1257 ? "elf32-nbigmips" : "elf32-bigmips"))
1258 : (HAVE_64BIT_OBJECTS
1259 ? "elf64-littlemips"
1260 : (HAVE_NEWABI
1261 ? "elf32-nlittlemips" : "elf32-littlemips")));
1262 #endif
1263 default:
1264 abort ();
1265 return NULL;
1269 /* Return the length of instruction INSN. */
1271 static inline unsigned int
1272 insn_length (const struct mips_cl_insn *insn)
1274 if (!mips_opts.mips16)
1275 return 4;
1276 return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1279 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1281 static void
1282 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1284 size_t i;
1286 insn->insn_mo = mo;
1287 insn->use_extend = FALSE;
1288 insn->extend = 0;
1289 insn->insn_opcode = mo->match;
1290 insn->frag = NULL;
1291 insn->where = 0;
1292 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1293 insn->fixp[i] = NULL;
1294 insn->fixed_p = (mips_opts.noreorder > 0);
1295 insn->noreorder_p = (mips_opts.noreorder > 0);
1296 insn->mips16_absolute_jump_p = 0;
1299 /* Install INSN at the location specified by its "frag" and "where" fields. */
1301 static void
1302 install_insn (const struct mips_cl_insn *insn)
1304 char *f = insn->frag->fr_literal + insn->where;
1305 if (!mips_opts.mips16)
1306 md_number_to_chars (f, insn->insn_opcode, 4);
1307 else if (insn->mips16_absolute_jump_p)
1309 md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1310 md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1312 else
1314 if (insn->use_extend)
1316 md_number_to_chars (f, 0xf000 | insn->extend, 2);
1317 f += 2;
1319 md_number_to_chars (f, insn->insn_opcode, 2);
1323 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1324 and install the opcode in the new location. */
1326 static void
1327 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1329 size_t i;
1331 insn->frag = frag;
1332 insn->where = where;
1333 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1334 if (insn->fixp[i] != NULL)
1336 insn->fixp[i]->fx_frag = frag;
1337 insn->fixp[i]->fx_where = where;
1339 install_insn (insn);
1342 /* Add INSN to the end of the output. */
1344 static void
1345 add_fixed_insn (struct mips_cl_insn *insn)
1347 char *f = frag_more (insn_length (insn));
1348 move_insn (insn, frag_now, f - frag_now->fr_literal);
1351 /* Start a variant frag and move INSN to the start of the variant part,
1352 marking it as fixed. The other arguments are as for frag_var. */
1354 static void
1355 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1356 relax_substateT subtype, symbolS *symbol, offsetT offset)
1358 frag_grow (max_chars);
1359 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1360 insn->fixed_p = 1;
1361 frag_var (rs_machine_dependent, max_chars, var,
1362 subtype, symbol, offset, NULL);
1365 /* Insert N copies of INSN into the history buffer, starting at
1366 position FIRST. Neither FIRST nor N need to be clipped. */
1368 static void
1369 insert_into_history (unsigned int first, unsigned int n,
1370 const struct mips_cl_insn *insn)
1372 if (mips_relax.sequence != 2)
1374 unsigned int i;
1376 for (i = ARRAY_SIZE (history); i-- > first;)
1377 if (i >= first + n)
1378 history[i] = history[i - n];
1379 else
1380 history[i] = *insn;
1384 /* Emit a nop instruction, recording it in the history buffer. */
1386 static void
1387 emit_nop (void)
1389 add_fixed_insn (NOP_INSN);
1390 insert_into_history (0, 1, NOP_INSN);
1393 /* Initialize vr4120_conflicts. There is a bit of duplication here:
1394 the idea is to make it obvious at a glance that each errata is
1395 included. */
1397 static void
1398 init_vr4120_conflicts (void)
1400 #define CONFLICT(FIRST, SECOND) \
1401 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1403 /* Errata 21 - [D]DIV[U] after [D]MACC */
1404 CONFLICT (MACC, DIV);
1405 CONFLICT (DMACC, DIV);
1407 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1408 CONFLICT (DMULT, DMULT);
1409 CONFLICT (DMULT, DMACC);
1410 CONFLICT (DMACC, DMULT);
1411 CONFLICT (DMACC, DMACC);
1413 /* Errata 24 - MT{LO,HI} after [D]MACC */
1414 CONFLICT (MACC, MTHILO);
1415 CONFLICT (DMACC, MTHILO);
1417 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1418 instruction is executed immediately after a MACC or DMACC
1419 instruction, the result of [either instruction] is incorrect." */
1420 CONFLICT (MACC, MULT);
1421 CONFLICT (MACC, DMULT);
1422 CONFLICT (DMACC, MULT);
1423 CONFLICT (DMACC, DMULT);
1425 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1426 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1427 DDIV or DDIVU instruction, the result of the MACC or
1428 DMACC instruction is incorrect.". */
1429 CONFLICT (DMULT, MACC);
1430 CONFLICT (DMULT, DMACC);
1431 CONFLICT (DIV, MACC);
1432 CONFLICT (DIV, DMACC);
1434 #undef CONFLICT
1437 struct regname {
1438 const char *name;
1439 unsigned int num;
1442 #define RTYPE_MASK 0x1ff00
1443 #define RTYPE_NUM 0x00100
1444 #define RTYPE_FPU 0x00200
1445 #define RTYPE_FCC 0x00400
1446 #define RTYPE_VEC 0x00800
1447 #define RTYPE_GP 0x01000
1448 #define RTYPE_CP0 0x02000
1449 #define RTYPE_PC 0x04000
1450 #define RTYPE_ACC 0x08000
1451 #define RTYPE_CCC 0x10000
1452 #define RNUM_MASK 0x000ff
1453 #define RWARN 0x80000
1455 #define GENERIC_REGISTER_NUMBERS \
1456 {"$0", RTYPE_NUM | 0}, \
1457 {"$1", RTYPE_NUM | 1}, \
1458 {"$2", RTYPE_NUM | 2}, \
1459 {"$3", RTYPE_NUM | 3}, \
1460 {"$4", RTYPE_NUM | 4}, \
1461 {"$5", RTYPE_NUM | 5}, \
1462 {"$6", RTYPE_NUM | 6}, \
1463 {"$7", RTYPE_NUM | 7}, \
1464 {"$8", RTYPE_NUM | 8}, \
1465 {"$9", RTYPE_NUM | 9}, \
1466 {"$10", RTYPE_NUM | 10}, \
1467 {"$11", RTYPE_NUM | 11}, \
1468 {"$12", RTYPE_NUM | 12}, \
1469 {"$13", RTYPE_NUM | 13}, \
1470 {"$14", RTYPE_NUM | 14}, \
1471 {"$15", RTYPE_NUM | 15}, \
1472 {"$16", RTYPE_NUM | 16}, \
1473 {"$17", RTYPE_NUM | 17}, \
1474 {"$18", RTYPE_NUM | 18}, \
1475 {"$19", RTYPE_NUM | 19}, \
1476 {"$20", RTYPE_NUM | 20}, \
1477 {"$21", RTYPE_NUM | 21}, \
1478 {"$22", RTYPE_NUM | 22}, \
1479 {"$23", RTYPE_NUM | 23}, \
1480 {"$24", RTYPE_NUM | 24}, \
1481 {"$25", RTYPE_NUM | 25}, \
1482 {"$26", RTYPE_NUM | 26}, \
1483 {"$27", RTYPE_NUM | 27}, \
1484 {"$28", RTYPE_NUM | 28}, \
1485 {"$29", RTYPE_NUM | 29}, \
1486 {"$30", RTYPE_NUM | 30}, \
1487 {"$31", RTYPE_NUM | 31}
1489 #define FPU_REGISTER_NAMES \
1490 {"$f0", RTYPE_FPU | 0}, \
1491 {"$f1", RTYPE_FPU | 1}, \
1492 {"$f2", RTYPE_FPU | 2}, \
1493 {"$f3", RTYPE_FPU | 3}, \
1494 {"$f4", RTYPE_FPU | 4}, \
1495 {"$f5", RTYPE_FPU | 5}, \
1496 {"$f6", RTYPE_FPU | 6}, \
1497 {"$f7", RTYPE_FPU | 7}, \
1498 {"$f8", RTYPE_FPU | 8}, \
1499 {"$f9", RTYPE_FPU | 9}, \
1500 {"$f10", RTYPE_FPU | 10}, \
1501 {"$f11", RTYPE_FPU | 11}, \
1502 {"$f12", RTYPE_FPU | 12}, \
1503 {"$f13", RTYPE_FPU | 13}, \
1504 {"$f14", RTYPE_FPU | 14}, \
1505 {"$f15", RTYPE_FPU | 15}, \
1506 {"$f16", RTYPE_FPU | 16}, \
1507 {"$f17", RTYPE_FPU | 17}, \
1508 {"$f18", RTYPE_FPU | 18}, \
1509 {"$f19", RTYPE_FPU | 19}, \
1510 {"$f20", RTYPE_FPU | 20}, \
1511 {"$f21", RTYPE_FPU | 21}, \
1512 {"$f22", RTYPE_FPU | 22}, \
1513 {"$f23", RTYPE_FPU | 23}, \
1514 {"$f24", RTYPE_FPU | 24}, \
1515 {"$f25", RTYPE_FPU | 25}, \
1516 {"$f26", RTYPE_FPU | 26}, \
1517 {"$f27", RTYPE_FPU | 27}, \
1518 {"$f28", RTYPE_FPU | 28}, \
1519 {"$f29", RTYPE_FPU | 29}, \
1520 {"$f30", RTYPE_FPU | 30}, \
1521 {"$f31", RTYPE_FPU | 31}
1523 #define FPU_CONDITION_CODE_NAMES \
1524 {"$fcc0", RTYPE_FCC | 0}, \
1525 {"$fcc1", RTYPE_FCC | 1}, \
1526 {"$fcc2", RTYPE_FCC | 2}, \
1527 {"$fcc3", RTYPE_FCC | 3}, \
1528 {"$fcc4", RTYPE_FCC | 4}, \
1529 {"$fcc5", RTYPE_FCC | 5}, \
1530 {"$fcc6", RTYPE_FCC | 6}, \
1531 {"$fcc7", RTYPE_FCC | 7}
1533 #define COPROC_CONDITION_CODE_NAMES \
1534 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
1535 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
1536 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
1537 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
1538 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
1539 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
1540 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
1541 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
1543 #define N32N64_SYMBOLIC_REGISTER_NAMES \
1544 {"$a4", RTYPE_GP | 8}, \
1545 {"$a5", RTYPE_GP | 9}, \
1546 {"$a6", RTYPE_GP | 10}, \
1547 {"$a7", RTYPE_GP | 11}, \
1548 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
1549 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
1550 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
1551 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
1552 {"$t0", RTYPE_GP | 12}, \
1553 {"$t1", RTYPE_GP | 13}, \
1554 {"$t2", RTYPE_GP | 14}, \
1555 {"$t3", RTYPE_GP | 15}
1557 #define O32_SYMBOLIC_REGISTER_NAMES \
1558 {"$t0", RTYPE_GP | 8}, \
1559 {"$t1", RTYPE_GP | 9}, \
1560 {"$t2", RTYPE_GP | 10}, \
1561 {"$t3", RTYPE_GP | 11}, \
1562 {"$t4", RTYPE_GP | 12}, \
1563 {"$t5", RTYPE_GP | 13}, \
1564 {"$t6", RTYPE_GP | 14}, \
1565 {"$t7", RTYPE_GP | 15}, \
1566 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
1567 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
1568 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
1569 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
1571 /* Remaining symbolic register names */
1572 #define SYMBOLIC_REGISTER_NAMES \
1573 {"$zero", RTYPE_GP | 0}, \
1574 {"$at", RTYPE_GP | 1}, \
1575 {"$AT", RTYPE_GP | 1}, \
1576 {"$v0", RTYPE_GP | 2}, \
1577 {"$v1", RTYPE_GP | 3}, \
1578 {"$a0", RTYPE_GP | 4}, \
1579 {"$a1", RTYPE_GP | 5}, \
1580 {"$a2", RTYPE_GP | 6}, \
1581 {"$a3", RTYPE_GP | 7}, \
1582 {"$s0", RTYPE_GP | 16}, \
1583 {"$s1", RTYPE_GP | 17}, \
1584 {"$s2", RTYPE_GP | 18}, \
1585 {"$s3", RTYPE_GP | 19}, \
1586 {"$s4", RTYPE_GP | 20}, \
1587 {"$s5", RTYPE_GP | 21}, \
1588 {"$s6", RTYPE_GP | 22}, \
1589 {"$s7", RTYPE_GP | 23}, \
1590 {"$t8", RTYPE_GP | 24}, \
1591 {"$t9", RTYPE_GP | 25}, \
1592 {"$k0", RTYPE_GP | 26}, \
1593 {"$kt0", RTYPE_GP | 26}, \
1594 {"$k1", RTYPE_GP | 27}, \
1595 {"$kt1", RTYPE_GP | 27}, \
1596 {"$gp", RTYPE_GP | 28}, \
1597 {"$sp", RTYPE_GP | 29}, \
1598 {"$s8", RTYPE_GP | 30}, \
1599 {"$fp", RTYPE_GP | 30}, \
1600 {"$ra", RTYPE_GP | 31}
1602 #define MIPS16_SPECIAL_REGISTER_NAMES \
1603 {"$pc", RTYPE_PC | 0}
1605 #define MDMX_VECTOR_REGISTER_NAMES \
1606 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
1607 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
1608 {"$v2", RTYPE_VEC | 2}, \
1609 {"$v3", RTYPE_VEC | 3}, \
1610 {"$v4", RTYPE_VEC | 4}, \
1611 {"$v5", RTYPE_VEC | 5}, \
1612 {"$v6", RTYPE_VEC | 6}, \
1613 {"$v7", RTYPE_VEC | 7}, \
1614 {"$v8", RTYPE_VEC | 8}, \
1615 {"$v9", RTYPE_VEC | 9}, \
1616 {"$v10", RTYPE_VEC | 10}, \
1617 {"$v11", RTYPE_VEC | 11}, \
1618 {"$v12", RTYPE_VEC | 12}, \
1619 {"$v13", RTYPE_VEC | 13}, \
1620 {"$v14", RTYPE_VEC | 14}, \
1621 {"$v15", RTYPE_VEC | 15}, \
1622 {"$v16", RTYPE_VEC | 16}, \
1623 {"$v17", RTYPE_VEC | 17}, \
1624 {"$v18", RTYPE_VEC | 18}, \
1625 {"$v19", RTYPE_VEC | 19}, \
1626 {"$v20", RTYPE_VEC | 20}, \
1627 {"$v21", RTYPE_VEC | 21}, \
1628 {"$v22", RTYPE_VEC | 22}, \
1629 {"$v23", RTYPE_VEC | 23}, \
1630 {"$v24", RTYPE_VEC | 24}, \
1631 {"$v25", RTYPE_VEC | 25}, \
1632 {"$v26", RTYPE_VEC | 26}, \
1633 {"$v27", RTYPE_VEC | 27}, \
1634 {"$v28", RTYPE_VEC | 28}, \
1635 {"$v29", RTYPE_VEC | 29}, \
1636 {"$v30", RTYPE_VEC | 30}, \
1637 {"$v31", RTYPE_VEC | 31}
1639 #define MIPS_DSP_ACCUMULATOR_NAMES \
1640 {"$ac0", RTYPE_ACC | 0}, \
1641 {"$ac1", RTYPE_ACC | 1}, \
1642 {"$ac2", RTYPE_ACC | 2}, \
1643 {"$ac3", RTYPE_ACC | 3}
1645 static const struct regname reg_names[] = {
1646 GENERIC_REGISTER_NUMBERS,
1647 FPU_REGISTER_NAMES,
1648 FPU_CONDITION_CODE_NAMES,
1649 COPROC_CONDITION_CODE_NAMES,
1651 /* The $txx registers depends on the abi,
1652 these will be added later into the symbol table from
1653 one of the tables below once mips_abi is set after
1654 parsing of arguments from the command line. */
1655 SYMBOLIC_REGISTER_NAMES,
1657 MIPS16_SPECIAL_REGISTER_NAMES,
1658 MDMX_VECTOR_REGISTER_NAMES,
1659 MIPS_DSP_ACCUMULATOR_NAMES,
1660 {0, 0}
1663 static const struct regname reg_names_o32[] = {
1664 O32_SYMBOLIC_REGISTER_NAMES,
1665 {0, 0}
1668 static const struct regname reg_names_n32n64[] = {
1669 N32N64_SYMBOLIC_REGISTER_NAMES,
1670 {0, 0}
1673 static int
1674 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
1676 symbolS *symbolP;
1677 char *e;
1678 char save_c;
1679 int reg = -1;
1681 /* Find end of name. */
1682 e = *s;
1683 if (is_name_beginner (*e))
1684 ++e;
1685 while (is_part_of_name (*e))
1686 ++e;
1688 /* Terminate name. */
1689 save_c = *e;
1690 *e = '\0';
1692 /* Look for a register symbol. */
1693 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
1695 int r = S_GET_VALUE (symbolP);
1696 if (r & types)
1697 reg = r & RNUM_MASK;
1698 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
1699 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
1700 reg = (r & RNUM_MASK) - 2;
1702 /* Else see if this is a register defined in an itbl entry. */
1703 else if ((types & RTYPE_GP) && itbl_have_entries)
1705 char *n = *s;
1706 unsigned long r;
1708 if (*n == '$')
1709 ++n;
1710 if (itbl_get_reg_val (n, &r))
1711 reg = r & RNUM_MASK;
1714 /* Advance to next token if a register was recognised. */
1715 if (reg >= 0)
1716 *s = e;
1717 else if (types & RWARN)
1718 as_warn ("Unrecognized register name `%s'", *s);
1720 *e = save_c;
1721 if (regnop)
1722 *regnop = reg;
1723 return reg >= 0;
1726 /* This function is called once, at assembler startup time. It should set up
1727 all the tables, etc. that the MD part of the assembler will need. */
1729 void
1730 md_begin (void)
1732 register const char *retval = NULL;
1733 int i = 0;
1734 int broken = 0;
1736 if (mips_pic != NO_PIC)
1738 if (g_switch_seen && g_switch_value != 0)
1739 as_bad (_("-G may not be used in position-independent code"));
1740 g_switch_value = 0;
1743 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1744 as_warn (_("Could not set architecture and machine"));
1746 op_hash = hash_new ();
1748 for (i = 0; i < NUMOPCODES;)
1750 const char *name = mips_opcodes[i].name;
1752 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1753 if (retval != NULL)
1755 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1756 mips_opcodes[i].name, retval);
1757 /* Probably a memory allocation problem? Give up now. */
1758 as_fatal (_("Broken assembler. No assembly attempted."));
1762 if (mips_opcodes[i].pinfo != INSN_MACRO)
1764 if (!validate_mips_insn (&mips_opcodes[i]))
1765 broken = 1;
1766 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1768 create_insn (&nop_insn, mips_opcodes + i);
1769 nop_insn.fixed_p = 1;
1772 ++i;
1774 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1777 mips16_op_hash = hash_new ();
1779 i = 0;
1780 while (i < bfd_mips16_num_opcodes)
1782 const char *name = mips16_opcodes[i].name;
1784 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1785 if (retval != NULL)
1786 as_fatal (_("internal: can't hash `%s': %s"),
1787 mips16_opcodes[i].name, retval);
1790 if (mips16_opcodes[i].pinfo != INSN_MACRO
1791 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1792 != mips16_opcodes[i].match))
1794 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1795 mips16_opcodes[i].name, mips16_opcodes[i].args);
1796 broken = 1;
1798 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1800 create_insn (&mips16_nop_insn, mips16_opcodes + i);
1801 mips16_nop_insn.fixed_p = 1;
1803 ++i;
1805 while (i < bfd_mips16_num_opcodes
1806 && strcmp (mips16_opcodes[i].name, name) == 0);
1809 if (broken)
1810 as_fatal (_("Broken assembler. No assembly attempted."));
1812 /* We add all the general register names to the symbol table. This
1813 helps us detect invalid uses of them. */
1814 for (i = 0; reg_names[i].name; i++)
1815 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
1816 reg_names[i].num, // & RNUM_MASK,
1817 &zero_address_frag));
1818 if (HAVE_NEWABI)
1819 for (i = 0; reg_names_n32n64[i].name; i++)
1820 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
1821 reg_names_n32n64[i].num, // & RNUM_MASK,
1822 &zero_address_frag));
1823 else
1824 for (i = 0; reg_names_o32[i].name; i++)
1825 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
1826 reg_names_o32[i].num, // & RNUM_MASK,
1827 &zero_address_frag));
1829 mips_no_prev_insn ();
1831 mips_gprmask = 0;
1832 mips_cprmask[0] = 0;
1833 mips_cprmask[1] = 0;
1834 mips_cprmask[2] = 0;
1835 mips_cprmask[3] = 0;
1837 /* set the default alignment for the text section (2**2) */
1838 record_alignment (text_section, 2);
1840 bfd_set_gp_size (stdoutput, g_switch_value);
1842 #ifdef OBJ_ELF
1843 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1845 /* On a native system other than VxWorks, sections must be aligned
1846 to 16 byte boundaries. When configured for an embedded ELF
1847 target, we don't bother. */
1848 if (strcmp (TARGET_OS, "elf") != 0
1849 && strcmp (TARGET_OS, "vxworks") != 0)
1851 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1852 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1853 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1856 /* Create a .reginfo section for register masks and a .mdebug
1857 section for debugging information. */
1859 segT seg;
1860 subsegT subseg;
1861 flagword flags;
1862 segT sec;
1864 seg = now_seg;
1865 subseg = now_subseg;
1867 /* The ABI says this section should be loaded so that the
1868 running program can access it. However, we don't load it
1869 if we are configured for an embedded target */
1870 flags = SEC_READONLY | SEC_DATA;
1871 if (strcmp (TARGET_OS, "elf") != 0)
1872 flags |= SEC_ALLOC | SEC_LOAD;
1874 if (mips_abi != N64_ABI)
1876 sec = subseg_new (".reginfo", (subsegT) 0);
1878 bfd_set_section_flags (stdoutput, sec, flags);
1879 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1881 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1883 else
1885 /* The 64-bit ABI uses a .MIPS.options section rather than
1886 .reginfo section. */
1887 sec = subseg_new (".MIPS.options", (subsegT) 0);
1888 bfd_set_section_flags (stdoutput, sec, flags);
1889 bfd_set_section_alignment (stdoutput, sec, 3);
1891 /* Set up the option header. */
1893 Elf_Internal_Options opthdr;
1894 char *f;
1896 opthdr.kind = ODK_REGINFO;
1897 opthdr.size = (sizeof (Elf_External_Options)
1898 + sizeof (Elf64_External_RegInfo));
1899 opthdr.section = 0;
1900 opthdr.info = 0;
1901 f = frag_more (sizeof (Elf_External_Options));
1902 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1903 (Elf_External_Options *) f);
1905 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1909 if (ECOFF_DEBUGGING)
1911 sec = subseg_new (".mdebug", (subsegT) 0);
1912 (void) bfd_set_section_flags (stdoutput, sec,
1913 SEC_HAS_CONTENTS | SEC_READONLY);
1914 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1916 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1918 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1919 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1920 SEC_READONLY | SEC_RELOC
1921 | SEC_DEBUGGING);
1922 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1925 subseg_set (seg, subseg);
1928 #endif /* OBJ_ELF */
1930 if (! ECOFF_DEBUGGING)
1931 md_obj_begin ();
1933 if (mips_fix_vr4120)
1934 init_vr4120_conflicts ();
1937 void
1938 md_mips_end (void)
1940 if (! ECOFF_DEBUGGING)
1941 md_obj_end ();
1944 void
1945 md_assemble (char *str)
1947 struct mips_cl_insn insn;
1948 bfd_reloc_code_real_type unused_reloc[3]
1949 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1951 imm_expr.X_op = O_absent;
1952 imm2_expr.X_op = O_absent;
1953 offset_expr.X_op = O_absent;
1954 imm_reloc[0] = BFD_RELOC_UNUSED;
1955 imm_reloc[1] = BFD_RELOC_UNUSED;
1956 imm_reloc[2] = BFD_RELOC_UNUSED;
1957 offset_reloc[0] = BFD_RELOC_UNUSED;
1958 offset_reloc[1] = BFD_RELOC_UNUSED;
1959 offset_reloc[2] = BFD_RELOC_UNUSED;
1961 if (mips_opts.mips16)
1962 mips16_ip (str, &insn);
1963 else
1965 mips_ip (str, &insn);
1966 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1967 str, insn.insn_opcode));
1970 if (insn_error)
1972 as_bad ("%s `%s'", insn_error, str);
1973 return;
1976 if (insn.insn_mo->pinfo == INSN_MACRO)
1978 macro_start ();
1979 if (mips_opts.mips16)
1980 mips16_macro (&insn);
1981 else
1982 macro (&insn);
1983 macro_end ();
1985 else
1987 if (imm_expr.X_op != O_absent)
1988 append_insn (&insn, &imm_expr, imm_reloc);
1989 else if (offset_expr.X_op != O_absent)
1990 append_insn (&insn, &offset_expr, offset_reloc);
1991 else
1992 append_insn (&insn, NULL, unused_reloc);
1996 /* Return true if the given relocation might need a matching %lo().
1997 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
1998 need a matching %lo() when applied to local symbols. */
2000 static inline bfd_boolean
2001 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
2003 return (HAVE_IN_PLACE_ADDENDS
2004 && (reloc == BFD_RELOC_HI16_S
2005 || reloc == BFD_RELOC_MIPS16_HI16_S
2006 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2007 all GOT16 relocations evaluate to "G". */
2008 || (reloc == BFD_RELOC_MIPS_GOT16 && mips_pic != VXWORKS_PIC)));
2011 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
2012 relocation. */
2014 static inline bfd_boolean
2015 fixup_has_matching_lo_p (fixS *fixp)
2017 return (fixp->fx_next != NULL
2018 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
2019 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
2020 && fixp->fx_addsy == fixp->fx_next->fx_addsy
2021 && fixp->fx_offset == fixp->fx_next->fx_offset);
2024 /* See whether instruction IP reads register REG. CLASS is the type
2025 of register. */
2027 static int
2028 insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg,
2029 enum mips_regclass class)
2031 if (class == MIPS16_REG)
2033 assert (mips_opts.mips16);
2034 reg = mips16_to_32_reg_map[reg];
2035 class = MIPS_GR_REG;
2038 /* Don't report on general register ZERO, since it never changes. */
2039 if (class == MIPS_GR_REG && reg == ZERO)
2040 return 0;
2042 if (class == MIPS_FP_REG)
2044 assert (! mips_opts.mips16);
2045 /* If we are called with either $f0 or $f1, we must check $f0.
2046 This is not optimal, because it will introduce an unnecessary
2047 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
2048 need to distinguish reading both $f0 and $f1 or just one of
2049 them. Note that we don't have to check the other way,
2050 because there is no instruction that sets both $f0 and $f1
2051 and requires a delay. */
2052 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
2053 && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1)
2054 == (reg &~ (unsigned) 1)))
2055 return 1;
2056 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
2057 && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1)
2058 == (reg &~ (unsigned) 1)))
2059 return 1;
2061 else if (! mips_opts.mips16)
2063 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
2064 && EXTRACT_OPERAND (RS, *ip) == reg)
2065 return 1;
2066 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
2067 && EXTRACT_OPERAND (RT, *ip) == reg)
2068 return 1;
2070 else
2072 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
2073 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg)
2074 return 1;
2075 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
2076 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg)
2077 return 1;
2078 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
2079 && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]
2080 == reg))
2081 return 1;
2082 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
2083 return 1;
2084 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
2085 return 1;
2086 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
2087 return 1;
2088 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
2089 && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg)
2090 return 1;
2093 return 0;
2096 /* This function returns true if modifying a register requires a
2097 delay. */
2099 static int
2100 reg_needs_delay (unsigned int reg)
2102 unsigned long prev_pinfo;
2104 prev_pinfo = history[0].insn_mo->pinfo;
2105 if (! mips_opts.noreorder
2106 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2107 && ! gpr_interlocks)
2108 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2109 && ! cop_interlocks)))
2111 /* A load from a coprocessor or from memory. All load delays
2112 delay the use of general register rt for one instruction. */
2113 /* Itbl support may require additional care here. */
2114 know (prev_pinfo & INSN_WRITE_GPR_T);
2115 if (reg == EXTRACT_OPERAND (RT, history[0]))
2116 return 1;
2119 return 0;
2122 /* Move all labels in insn_labels to the current insertion point. */
2124 static void
2125 mips_move_labels (void)
2127 struct insn_label_list *l;
2128 valueT val;
2130 for (l = insn_labels; l != NULL; l = l->next)
2132 assert (S_GET_SEGMENT (l->label) == now_seg);
2133 symbol_set_frag (l->label, frag_now);
2134 val = (valueT) frag_now_fix ();
2135 /* mips16 text labels are stored as odd. */
2136 if (mips_opts.mips16)
2137 ++val;
2138 S_SET_VALUE (l->label, val);
2142 /* Mark instruction labels in mips16 mode. This permits the linker to
2143 handle them specially, such as generating jalx instructions when
2144 needed. We also make them odd for the duration of the assembly, in
2145 order to generate the right sort of code. We will make them even
2146 in the adjust_symtab routine, while leaving them marked. This is
2147 convenient for the debugger and the disassembler. The linker knows
2148 to make them odd again. */
2150 static void
2151 mips16_mark_labels (void)
2153 if (mips_opts.mips16)
2155 struct insn_label_list *l;
2156 valueT val;
2158 for (l = insn_labels; l != NULL; l = l->next)
2160 #ifdef OBJ_ELF
2161 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
2162 S_SET_OTHER (l->label, STO_MIPS16);
2163 #endif
2164 val = S_GET_VALUE (l->label);
2165 if ((val & 1) == 0)
2166 S_SET_VALUE (l->label, val + 1);
2171 /* End the current frag. Make it a variant frag and record the
2172 relaxation info. */
2174 static void
2175 relax_close_frag (void)
2177 mips_macro_warning.first_frag = frag_now;
2178 frag_var (rs_machine_dependent, 0, 0,
2179 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
2180 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2182 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2183 mips_relax.first_fixup = 0;
2186 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
2187 See the comment above RELAX_ENCODE for more details. */
2189 static void
2190 relax_start (symbolS *symbol)
2192 assert (mips_relax.sequence == 0);
2193 mips_relax.sequence = 1;
2194 mips_relax.symbol = symbol;
2197 /* Start generating the second version of a relaxable sequence.
2198 See the comment above RELAX_ENCODE for more details. */
2200 static void
2201 relax_switch (void)
2203 assert (mips_relax.sequence == 1);
2204 mips_relax.sequence = 2;
2207 /* End the current relaxable sequence. */
2209 static void
2210 relax_end (void)
2212 assert (mips_relax.sequence == 2);
2213 relax_close_frag ();
2214 mips_relax.sequence = 0;
2217 /* Classify an instruction according to the FIX_VR4120_* enumeration.
2218 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
2219 by VR4120 errata. */
2221 static unsigned int
2222 classify_vr4120_insn (const char *name)
2224 if (strncmp (name, "macc", 4) == 0)
2225 return FIX_VR4120_MACC;
2226 if (strncmp (name, "dmacc", 5) == 0)
2227 return FIX_VR4120_DMACC;
2228 if (strncmp (name, "mult", 4) == 0)
2229 return FIX_VR4120_MULT;
2230 if (strncmp (name, "dmult", 5) == 0)
2231 return FIX_VR4120_DMULT;
2232 if (strstr (name, "div"))
2233 return FIX_VR4120_DIV;
2234 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
2235 return FIX_VR4120_MTHILO;
2236 return NUM_FIX_VR4120_CLASSES;
2239 /* Return the number of instructions that must separate INSN1 and INSN2,
2240 where INSN1 is the earlier instruction. Return the worst-case value
2241 for any INSN2 if INSN2 is null. */
2243 static unsigned int
2244 insns_between (const struct mips_cl_insn *insn1,
2245 const struct mips_cl_insn *insn2)
2247 unsigned long pinfo1, pinfo2;
2249 /* This function needs to know which pinfo flags are set for INSN2
2250 and which registers INSN2 uses. The former is stored in PINFO2 and
2251 the latter is tested via INSN2_USES_REG. If INSN2 is null, PINFO2
2252 will have every flag set and INSN2_USES_REG will always return true. */
2253 pinfo1 = insn1->insn_mo->pinfo;
2254 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
2256 #define INSN2_USES_REG(REG, CLASS) \
2257 (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS))
2259 /* For most targets, write-after-read dependencies on the HI and LO
2260 registers must be separated by at least two instructions. */
2261 if (!hilo_interlocks)
2263 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
2264 return 2;
2265 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
2266 return 2;
2269 /* If we're working around r7000 errata, there must be two instructions
2270 between an mfhi or mflo and any instruction that uses the result. */
2271 if (mips_7000_hilo_fix
2272 && MF_HILO_INSN (pinfo1)
2273 && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG))
2274 return 2;
2276 /* If working around VR4120 errata, check for combinations that need
2277 a single intervening instruction. */
2278 if (mips_fix_vr4120)
2280 unsigned int class1, class2;
2282 class1 = classify_vr4120_insn (insn1->insn_mo->name);
2283 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
2285 if (insn2 == NULL)
2286 return 1;
2287 class2 = classify_vr4120_insn (insn2->insn_mo->name);
2288 if (vr4120_conflicts[class1] & (1 << class2))
2289 return 1;
2293 if (!mips_opts.mips16)
2295 /* Check for GPR or coprocessor load delays. All such delays
2296 are on the RT register. */
2297 /* Itbl support may require additional care here. */
2298 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
2299 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
2301 know (pinfo1 & INSN_WRITE_GPR_T);
2302 if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG))
2303 return 1;
2306 /* Check for generic coprocessor hazards.
2308 This case is not handled very well. There is no special
2309 knowledge of CP0 handling, and the coprocessors other than
2310 the floating point unit are not distinguished at all. */
2311 /* Itbl support may require additional care here. FIXME!
2312 Need to modify this to include knowledge about
2313 user specified delays! */
2314 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
2315 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
2317 /* Handle cases where INSN1 writes to a known general coprocessor
2318 register. There must be a one instruction delay before INSN2
2319 if INSN2 reads that register, otherwise no delay is needed. */
2320 if (pinfo1 & INSN_WRITE_FPR_T)
2322 if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG))
2323 return 1;
2325 else if (pinfo1 & INSN_WRITE_FPR_S)
2327 if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG))
2328 return 1;
2330 else
2332 /* Read-after-write dependencies on the control registers
2333 require a two-instruction gap. */
2334 if ((pinfo1 & INSN_WRITE_COND_CODE)
2335 && (pinfo2 & INSN_READ_COND_CODE))
2336 return 2;
2338 /* We don't know exactly what INSN1 does. If INSN2 is
2339 also a coprocessor instruction, assume there must be
2340 a one instruction gap. */
2341 if (pinfo2 & INSN_COP)
2342 return 1;
2346 /* Check for read-after-write dependencies on the coprocessor
2347 control registers in cases where INSN1 does not need a general
2348 coprocessor delay. This means that INSN1 is a floating point
2349 comparison instruction. */
2350 /* Itbl support may require additional care here. */
2351 else if (!cop_interlocks
2352 && (pinfo1 & INSN_WRITE_COND_CODE)
2353 && (pinfo2 & INSN_READ_COND_CODE))
2354 return 1;
2357 #undef INSN2_USES_REG
2359 return 0;
2362 /* Return the number of nops that would be needed to work around the
2363 VR4130 mflo/mfhi errata if instruction INSN immediately followed
2364 the MAX_VR4130_NOPS instructions described by HISTORY. */
2366 static int
2367 nops_for_vr4130 (const struct mips_cl_insn *history,
2368 const struct mips_cl_insn *insn)
2370 int i, j, reg;
2372 /* Check if the instruction writes to HI or LO. MTHI and MTLO
2373 are not affected by the errata. */
2374 if (insn != 0
2375 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
2376 || strcmp (insn->insn_mo->name, "mtlo") == 0
2377 || strcmp (insn->insn_mo->name, "mthi") == 0))
2378 return 0;
2380 /* Search for the first MFLO or MFHI. */
2381 for (i = 0; i < MAX_VR4130_NOPS; i++)
2382 if (!history[i].noreorder_p && MF_HILO_INSN (history[i].insn_mo->pinfo))
2384 /* Extract the destination register. */
2385 if (mips_opts.mips16)
2386 reg = mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, history[i])];
2387 else
2388 reg = EXTRACT_OPERAND (RD, history[i]);
2390 /* No nops are needed if INSN reads that register. */
2391 if (insn != NULL && insn_uses_reg (insn, reg, MIPS_GR_REG))
2392 return 0;
2394 /* ...or if any of the intervening instructions do. */
2395 for (j = 0; j < i; j++)
2396 if (insn_uses_reg (&history[j], reg, MIPS_GR_REG))
2397 return 0;
2399 return MAX_VR4130_NOPS - i;
2401 return 0;
2404 /* Return the number of nops that would be needed if instruction INSN
2405 immediately followed the MAX_NOPS instructions given by HISTORY,
2406 where HISTORY[0] is the most recent instruction. If INSN is null,
2407 return the worse-case number of nops for any instruction. */
2409 static int
2410 nops_for_insn (const struct mips_cl_insn *history,
2411 const struct mips_cl_insn *insn)
2413 int i, nops, tmp_nops;
2415 nops = 0;
2416 for (i = 0; i < MAX_DELAY_NOPS; i++)
2417 if (!history[i].noreorder_p)
2419 tmp_nops = insns_between (history + i, insn) - i;
2420 if (tmp_nops > nops)
2421 nops = tmp_nops;
2424 if (mips_fix_vr4130)
2426 tmp_nops = nops_for_vr4130 (history, insn);
2427 if (tmp_nops > nops)
2428 nops = tmp_nops;
2431 return nops;
2434 /* The variable arguments provide NUM_INSNS extra instructions that
2435 might be added to HISTORY. Return the largest number of nops that
2436 would be needed after the extended sequence. */
2438 static int
2439 nops_for_sequence (int num_insns, const struct mips_cl_insn *history, ...)
2441 va_list args;
2442 struct mips_cl_insn buffer[MAX_NOPS];
2443 struct mips_cl_insn *cursor;
2444 int nops;
2446 va_start (args, history);
2447 cursor = buffer + num_insns;
2448 memcpy (cursor, history, (MAX_NOPS - num_insns) * sizeof (*cursor));
2449 while (cursor > buffer)
2450 *--cursor = *va_arg (args, const struct mips_cl_insn *);
2452 nops = nops_for_insn (buffer, NULL);
2453 va_end (args);
2454 return nops;
2457 /* Like nops_for_insn, but if INSN is a branch, take into account the
2458 worst-case delay for the branch target. */
2460 static int
2461 nops_for_insn_or_target (const struct mips_cl_insn *history,
2462 const struct mips_cl_insn *insn)
2464 int nops, tmp_nops;
2466 nops = nops_for_insn (history, insn);
2467 if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2468 | INSN_COND_BRANCH_DELAY
2469 | INSN_COND_BRANCH_LIKELY))
2471 tmp_nops = nops_for_sequence (2, history, insn, NOP_INSN);
2472 if (tmp_nops > nops)
2473 nops = tmp_nops;
2475 else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH))
2477 tmp_nops = nops_for_sequence (1, history, insn);
2478 if (tmp_nops > nops)
2479 nops = tmp_nops;
2481 return nops;
2484 /* Output an instruction. IP is the instruction information.
2485 ADDRESS_EXPR is an operand of the instruction to be used with
2486 RELOC_TYPE. */
2488 static void
2489 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
2490 bfd_reloc_code_real_type *reloc_type)
2492 register unsigned long prev_pinfo, pinfo;
2493 relax_stateT prev_insn_frag_type = 0;
2494 bfd_boolean relaxed_branch = FALSE;
2496 /* Mark instruction labels in mips16 mode. */
2497 mips16_mark_labels ();
2499 prev_pinfo = history[0].insn_mo->pinfo;
2500 pinfo = ip->insn_mo->pinfo;
2502 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2504 /* There are a lot of optimizations we could do that we don't.
2505 In particular, we do not, in general, reorder instructions.
2506 If you use gcc with optimization, it will reorder
2507 instructions and generally do much more optimization then we
2508 do here; repeating all that work in the assembler would only
2509 benefit hand written assembly code, and does not seem worth
2510 it. */
2511 int nops = (mips_optimize == 0
2512 ? nops_for_insn (history, NULL)
2513 : nops_for_insn_or_target (history, ip));
2514 if (nops > 0)
2516 fragS *old_frag;
2517 unsigned long old_frag_offset;
2518 int i;
2520 old_frag = frag_now;
2521 old_frag_offset = frag_now_fix ();
2523 for (i = 0; i < nops; i++)
2524 emit_nop ();
2526 if (listing)
2528 listing_prev_line ();
2529 /* We may be at the start of a variant frag. In case we
2530 are, make sure there is enough space for the frag
2531 after the frags created by listing_prev_line. The
2532 argument to frag_grow here must be at least as large
2533 as the argument to all other calls to frag_grow in
2534 this file. We don't have to worry about being in the
2535 middle of a variant frag, because the variants insert
2536 all needed nop instructions themselves. */
2537 frag_grow (40);
2540 mips_move_labels ();
2542 #ifndef NO_ECOFF_DEBUGGING
2543 if (ECOFF_DEBUGGING)
2544 ecoff_fix_loc (old_frag, old_frag_offset);
2545 #endif
2548 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
2550 /* Work out how many nops in prev_nop_frag are needed by IP. */
2551 int nops = nops_for_insn_or_target (history, ip);
2552 assert (nops <= prev_nop_frag_holds);
2554 /* Enforce NOPS as a minimum. */
2555 if (nops > prev_nop_frag_required)
2556 prev_nop_frag_required = nops;
2558 if (prev_nop_frag_holds == prev_nop_frag_required)
2560 /* Settle for the current number of nops. Update the history
2561 accordingly (for the benefit of any future .set reorder code). */
2562 prev_nop_frag = NULL;
2563 insert_into_history (prev_nop_frag_since,
2564 prev_nop_frag_holds, NOP_INSN);
2566 else
2568 /* Allow this instruction to replace one of the nops that was
2569 tentatively added to prev_nop_frag. */
2570 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2571 prev_nop_frag_holds--;
2572 prev_nop_frag_since++;
2576 #ifdef OBJ_ELF
2577 /* The value passed to dwarf2_emit_insn is the distance between
2578 the beginning of the current instruction and the address that
2579 should be recorded in the debug tables. For MIPS16 debug info
2580 we want to use ISA-encoded addresses, so we pass -1 for an
2581 address higher by one than the current. */
2582 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2583 #endif
2585 /* Record the frag type before frag_var. */
2586 if (history[0].frag)
2587 prev_insn_frag_type = history[0].frag->fr_type;
2589 if (address_expr
2590 && *reloc_type == BFD_RELOC_16_PCREL_S2
2591 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2592 || pinfo & INSN_COND_BRANCH_LIKELY)
2593 && mips_relax_branch
2594 /* Don't try branch relaxation within .set nomacro, or within
2595 .set noat if we use $at for PIC computations. If it turns
2596 out that the branch was out-of-range, we'll get an error. */
2597 && !mips_opts.warn_about_macros
2598 && !(mips_opts.noat && mips_pic != NO_PIC)
2599 && !mips_opts.mips16)
2601 relaxed_branch = TRUE;
2602 add_relaxed_insn (ip, (relaxed_branch_length
2603 (NULL, NULL,
2604 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2605 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1
2606 : 0)), 4,
2607 RELAX_BRANCH_ENCODE
2608 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2609 pinfo & INSN_COND_BRANCH_LIKELY,
2610 pinfo & INSN_WRITE_GPR_31,
2612 address_expr->X_add_symbol,
2613 address_expr->X_add_number);
2614 *reloc_type = BFD_RELOC_UNUSED;
2616 else if (*reloc_type > BFD_RELOC_UNUSED)
2618 /* We need to set up a variant frag. */
2619 assert (mips_opts.mips16 && address_expr != NULL);
2620 add_relaxed_insn (ip, 4, 0,
2621 RELAX_MIPS16_ENCODE
2622 (*reloc_type - BFD_RELOC_UNUSED,
2623 mips16_small, mips16_ext,
2624 prev_pinfo & INSN_UNCOND_BRANCH_DELAY,
2625 history[0].mips16_absolute_jump_p),
2626 make_expr_symbol (address_expr), 0);
2628 else if (mips_opts.mips16
2629 && ! ip->use_extend
2630 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2632 if ((pinfo & INSN_UNCOND_BRANCH_DELAY) == 0)
2633 /* Make sure there is enough room to swap this instruction with
2634 a following jump instruction. */
2635 frag_grow (6);
2636 add_fixed_insn (ip);
2638 else
2640 if (mips_opts.mips16
2641 && mips_opts.noreorder
2642 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2643 as_warn (_("extended instruction in delay slot"));
2645 if (mips_relax.sequence)
2647 /* If we've reached the end of this frag, turn it into a variant
2648 frag and record the information for the instructions we've
2649 written so far. */
2650 if (frag_room () < 4)
2651 relax_close_frag ();
2652 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2655 if (mips_relax.sequence != 2)
2656 mips_macro_warning.sizes[0] += 4;
2657 if (mips_relax.sequence != 1)
2658 mips_macro_warning.sizes[1] += 4;
2660 if (mips_opts.mips16)
2662 ip->fixed_p = 1;
2663 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2665 add_fixed_insn (ip);
2668 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
2670 if (address_expr->X_op == O_constant)
2672 unsigned int tmp;
2674 switch (*reloc_type)
2676 case BFD_RELOC_32:
2677 ip->insn_opcode |= address_expr->X_add_number;
2678 break;
2680 case BFD_RELOC_MIPS_HIGHEST:
2681 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2682 ip->insn_opcode |= tmp & 0xffff;
2683 break;
2685 case BFD_RELOC_MIPS_HIGHER:
2686 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2687 ip->insn_opcode |= tmp & 0xffff;
2688 break;
2690 case BFD_RELOC_HI16_S:
2691 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2692 ip->insn_opcode |= tmp & 0xffff;
2693 break;
2695 case BFD_RELOC_HI16:
2696 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2697 break;
2699 case BFD_RELOC_UNUSED:
2700 case BFD_RELOC_LO16:
2701 case BFD_RELOC_MIPS_GOT_DISP:
2702 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2703 break;
2705 case BFD_RELOC_MIPS_JMP:
2706 if ((address_expr->X_add_number & 3) != 0)
2707 as_bad (_("jump to misaligned address (0x%lx)"),
2708 (unsigned long) address_expr->X_add_number);
2709 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2710 break;
2712 case BFD_RELOC_MIPS16_JMP:
2713 if ((address_expr->X_add_number & 3) != 0)
2714 as_bad (_("jump to misaligned address (0x%lx)"),
2715 (unsigned long) address_expr->X_add_number);
2716 ip->insn_opcode |=
2717 (((address_expr->X_add_number & 0x7c0000) << 3)
2718 | ((address_expr->X_add_number & 0xf800000) >> 7)
2719 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2720 break;
2722 case BFD_RELOC_16_PCREL_S2:
2723 if ((address_expr->X_add_number & 3) != 0)
2724 as_bad (_("branch to misaligned address (0x%lx)"),
2725 (unsigned long) address_expr->X_add_number);
2726 if (mips_relax_branch)
2727 goto need_reloc;
2728 if ((address_expr->X_add_number + 0x20000) & ~0x3ffff)
2729 as_bad (_("branch address range overflow (0x%lx)"),
2730 (unsigned long) address_expr->X_add_number);
2731 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0xffff;
2732 break;
2734 default:
2735 internalError ();
2738 else if (*reloc_type < BFD_RELOC_UNUSED)
2739 need_reloc:
2741 reloc_howto_type *howto;
2742 int i;
2744 /* In a compound relocation, it is the final (outermost)
2745 operator that determines the relocated field. */
2746 for (i = 1; i < 3; i++)
2747 if (reloc_type[i] == BFD_RELOC_UNUSED)
2748 break;
2750 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2751 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
2752 bfd_get_reloc_size (howto),
2753 address_expr,
2754 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2755 reloc_type[0]);
2757 /* These relocations can have an addend that won't fit in
2758 4 octets for 64bit assembly. */
2759 if (HAVE_64BIT_GPRS
2760 && ! howto->partial_inplace
2761 && (reloc_type[0] == BFD_RELOC_16
2762 || reloc_type[0] == BFD_RELOC_32
2763 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2764 || reloc_type[0] == BFD_RELOC_HI16_S
2765 || reloc_type[0] == BFD_RELOC_LO16
2766 || reloc_type[0] == BFD_RELOC_GPREL16
2767 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2768 || reloc_type[0] == BFD_RELOC_GPREL32
2769 || reloc_type[0] == BFD_RELOC_64
2770 || reloc_type[0] == BFD_RELOC_CTOR
2771 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2772 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2773 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2774 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2775 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2776 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2777 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2778 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2779 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
2780 ip->fixp[0]->fx_no_overflow = 1;
2782 if (mips_relax.sequence)
2784 if (mips_relax.first_fixup == 0)
2785 mips_relax.first_fixup = ip->fixp[0];
2787 else if (reloc_needs_lo_p (*reloc_type))
2789 struct mips_hi_fixup *hi_fixup;
2791 /* Reuse the last entry if it already has a matching %lo. */
2792 hi_fixup = mips_hi_fixup_list;
2793 if (hi_fixup == 0
2794 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2796 hi_fixup = ((struct mips_hi_fixup *)
2797 xmalloc (sizeof (struct mips_hi_fixup)));
2798 hi_fixup->next = mips_hi_fixup_list;
2799 mips_hi_fixup_list = hi_fixup;
2801 hi_fixup->fixp = ip->fixp[0];
2802 hi_fixup->seg = now_seg;
2805 /* Add fixups for the second and third relocations, if given.
2806 Note that the ABI allows the second relocation to be
2807 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2808 moment we only use RSS_UNDEF, but we could add support
2809 for the others if it ever becomes necessary. */
2810 for (i = 1; i < 3; i++)
2811 if (reloc_type[i] != BFD_RELOC_UNUSED)
2813 ip->fixp[i] = fix_new (ip->frag, ip->where,
2814 ip->fixp[0]->fx_size, NULL, 0,
2815 FALSE, reloc_type[i]);
2817 /* Use fx_tcbit to mark compound relocs. */
2818 ip->fixp[0]->fx_tcbit = 1;
2819 ip->fixp[i]->fx_tcbit = 1;
2823 install_insn (ip);
2825 /* Update the register mask information. */
2826 if (! mips_opts.mips16)
2828 if (pinfo & INSN_WRITE_GPR_D)
2829 mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
2830 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2831 mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
2832 if (pinfo & INSN_READ_GPR_S)
2833 mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
2834 if (pinfo & INSN_WRITE_GPR_31)
2835 mips_gprmask |= 1 << RA;
2836 if (pinfo & INSN_WRITE_FPR_D)
2837 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
2838 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2839 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
2840 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2841 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
2842 if ((pinfo & INSN_READ_FPR_R) != 0)
2843 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
2844 if (pinfo & INSN_COP)
2846 /* We don't keep enough information to sort these cases out.
2847 The itbl support does keep this information however, although
2848 we currently don't support itbl fprmats as part of the cop
2849 instruction. May want to add this support in the future. */
2851 /* Never set the bit for $0, which is always zero. */
2852 mips_gprmask &= ~1 << 0;
2854 else
2856 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2857 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
2858 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2859 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
2860 if (pinfo & MIPS16_INSN_WRITE_Z)
2861 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
2862 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2863 mips_gprmask |= 1 << TREG;
2864 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2865 mips_gprmask |= 1 << SP;
2866 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2867 mips_gprmask |= 1 << RA;
2868 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2869 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2870 if (pinfo & MIPS16_INSN_READ_Z)
2871 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
2872 if (pinfo & MIPS16_INSN_READ_GPR_X)
2873 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
2876 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2878 /* Filling the branch delay slot is more complex. We try to
2879 switch the branch with the previous instruction, which we can
2880 do if the previous instruction does not set up a condition
2881 that the branch tests and if the branch is not itself the
2882 target of any branch. */
2883 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2884 || (pinfo & INSN_COND_BRANCH_DELAY))
2886 if (mips_optimize < 2
2887 /* If we have seen .set volatile or .set nomove, don't
2888 optimize. */
2889 || mips_opts.nomove != 0
2890 /* We can't swap if the previous instruction's position
2891 is fixed. */
2892 || history[0].fixed_p
2893 /* If the previous previous insn was in a .set
2894 noreorder, we can't swap. Actually, the MIPS
2895 assembler will swap in this situation. However, gcc
2896 configured -with-gnu-as will generate code like
2897 .set noreorder
2898 lw $4,XXX
2899 .set reorder
2900 INSN
2901 bne $4,$0,foo
2902 in which we can not swap the bne and INSN. If gcc is
2903 not configured -with-gnu-as, it does not output the
2904 .set pseudo-ops. */
2905 || history[1].noreorder_p
2906 /* If the branch is itself the target of a branch, we
2907 can not swap. We cheat on this; all we check for is
2908 whether there is a label on this instruction. If
2909 there are any branches to anything other than a
2910 label, users must use .set noreorder. */
2911 || insn_labels != NULL
2912 /* If the previous instruction is in a variant frag
2913 other than this branch's one, we cannot do the swap.
2914 This does not apply to the mips16, which uses variant
2915 frags for different purposes. */
2916 || (! mips_opts.mips16
2917 && prev_insn_frag_type == rs_machine_dependent)
2918 /* Check for conflicts between the branch and the instructions
2919 before the candidate delay slot. */
2920 || nops_for_insn (history + 1, ip) > 0
2921 /* Check for conflicts between the swapped sequence and the
2922 target of the branch. */
2923 || nops_for_sequence (2, history + 1, ip, history) > 0
2924 /* We do not swap with a trap instruction, since it
2925 complicates trap handlers to have the trap
2926 instruction be in a delay slot. */
2927 || (prev_pinfo & INSN_TRAP)
2928 /* If the branch reads a register that the previous
2929 instruction sets, we can not swap. */
2930 || (! mips_opts.mips16
2931 && (prev_pinfo & INSN_WRITE_GPR_T)
2932 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
2933 MIPS_GR_REG))
2934 || (! mips_opts.mips16
2935 && (prev_pinfo & INSN_WRITE_GPR_D)
2936 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
2937 MIPS_GR_REG))
2938 || (mips_opts.mips16
2939 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2940 && (insn_uses_reg
2941 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
2942 MIPS16_REG)))
2943 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2944 && (insn_uses_reg
2945 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
2946 MIPS16_REG)))
2947 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2948 && (insn_uses_reg
2949 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
2950 MIPS16_REG)))
2951 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2952 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2953 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2954 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2955 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2956 && insn_uses_reg (ip,
2957 MIPS16OP_EXTRACT_REG32R
2958 (history[0].insn_opcode),
2959 MIPS_GR_REG))))
2960 /* If the branch writes a register that the previous
2961 instruction sets, we can not swap (we know that
2962 branches write only to RD or to $31). */
2963 || (! mips_opts.mips16
2964 && (prev_pinfo & INSN_WRITE_GPR_T)
2965 && (((pinfo & INSN_WRITE_GPR_D)
2966 && (EXTRACT_OPERAND (RT, history[0])
2967 == EXTRACT_OPERAND (RD, *ip)))
2968 || ((pinfo & INSN_WRITE_GPR_31)
2969 && EXTRACT_OPERAND (RT, history[0]) == RA)))
2970 || (! mips_opts.mips16
2971 && (prev_pinfo & INSN_WRITE_GPR_D)
2972 && (((pinfo & INSN_WRITE_GPR_D)
2973 && (EXTRACT_OPERAND (RD, history[0])
2974 == EXTRACT_OPERAND (RD, *ip)))
2975 || ((pinfo & INSN_WRITE_GPR_31)
2976 && EXTRACT_OPERAND (RD, history[0]) == RA)))
2977 || (mips_opts.mips16
2978 && (pinfo & MIPS16_INSN_WRITE_31)
2979 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2980 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2981 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
2982 == RA))))
2983 /* If the branch writes a register that the previous
2984 instruction reads, we can not swap (we know that
2985 branches only write to RD or to $31). */
2986 || (! mips_opts.mips16
2987 && (pinfo & INSN_WRITE_GPR_D)
2988 && insn_uses_reg (&history[0],
2989 EXTRACT_OPERAND (RD, *ip),
2990 MIPS_GR_REG))
2991 || (! mips_opts.mips16
2992 && (pinfo & INSN_WRITE_GPR_31)
2993 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2994 || (mips_opts.mips16
2995 && (pinfo & MIPS16_INSN_WRITE_31)
2996 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2997 /* If one instruction sets a condition code and the
2998 other one uses a condition code, we can not swap. */
2999 || ((pinfo & INSN_READ_COND_CODE)
3000 && (prev_pinfo & INSN_WRITE_COND_CODE))
3001 || ((pinfo & INSN_WRITE_COND_CODE)
3002 && (prev_pinfo & INSN_READ_COND_CODE))
3003 /* If the previous instruction uses the PC, we can not
3004 swap. */
3005 || (mips_opts.mips16
3006 && (prev_pinfo & MIPS16_INSN_READ_PC))
3007 /* If the previous instruction had a fixup in mips16
3008 mode, we can not swap. This normally means that the
3009 previous instruction was a 4 byte branch anyhow. */
3010 || (mips_opts.mips16 && history[0].fixp[0])
3011 /* If the previous instruction is a sync, sync.l, or
3012 sync.p, we can not swap. */
3013 || (prev_pinfo & INSN_SYNC))
3015 if (mips_opts.mips16
3016 && (pinfo & INSN_UNCOND_BRANCH_DELAY)
3017 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31))
3018 && (mips_opts.isa == ISA_MIPS32
3019 || mips_opts.isa == ISA_MIPS32R2
3020 || mips_opts.isa == ISA_MIPS64
3021 || mips_opts.isa == ISA_MIPS64R2))
3023 /* Convert MIPS16 jr/jalr into a "compact" jump. */
3024 ip->insn_opcode |= 0x0080;
3025 install_insn (ip);
3026 insert_into_history (0, 1, ip);
3028 else
3030 /* We could do even better for unconditional branches to
3031 portions of this object file; we could pick up the
3032 instruction at the destination, put it in the delay
3033 slot, and bump the destination address. */
3034 insert_into_history (0, 1, ip);
3035 emit_nop ();
3038 if (mips_relax.sequence)
3039 mips_relax.sizes[mips_relax.sequence - 1] += 4;
3041 else
3043 /* It looks like we can actually do the swap. */
3044 struct mips_cl_insn delay = history[0];
3045 if (mips_opts.mips16)
3047 know (delay.frag == ip->frag);
3048 move_insn (ip, delay.frag, delay.where);
3049 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
3051 else if (relaxed_branch)
3053 /* Add the delay slot instruction to the end of the
3054 current frag and shrink the fixed part of the
3055 original frag. If the branch occupies the tail of
3056 the latter, move it backwards to cover the gap. */
3057 delay.frag->fr_fix -= 4;
3058 if (delay.frag == ip->frag)
3059 move_insn (ip, ip->frag, ip->where - 4);
3060 add_fixed_insn (&delay);
3062 else
3064 move_insn (&delay, ip->frag, ip->where);
3065 move_insn (ip, history[0].frag, history[0].where);
3067 history[0] = *ip;
3068 delay.fixed_p = 1;
3069 insert_into_history (0, 1, &delay);
3072 /* If that was an unconditional branch, forget the previous
3073 insn information. */
3074 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
3075 mips_no_prev_insn ();
3077 else if (pinfo & INSN_COND_BRANCH_LIKELY)
3079 /* We don't yet optimize a branch likely. What we should do
3080 is look at the target, copy the instruction found there
3081 into the delay slot, and increment the branch to jump to
3082 the next instruction. */
3083 insert_into_history (0, 1, ip);
3084 emit_nop ();
3086 else
3087 insert_into_history (0, 1, ip);
3089 else
3090 insert_into_history (0, 1, ip);
3092 /* We just output an insn, so the next one doesn't have a label. */
3093 mips_clear_insn_labels ();
3096 /* Forget that there was any previous instruction or label. */
3098 static void
3099 mips_no_prev_insn (void)
3101 prev_nop_frag = NULL;
3102 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
3103 mips_clear_insn_labels ();
3106 /* This function must be called before we emit something other than
3107 instructions. It is like mips_no_prev_insn except that it inserts
3108 any NOPS that might be needed by previous instructions. */
3110 void
3111 mips_emit_delays (void)
3113 if (! mips_opts.noreorder)
3115 int nops = nops_for_insn (history, NULL);
3116 if (nops > 0)
3118 while (nops-- > 0)
3119 add_fixed_insn (NOP_INSN);
3120 mips_move_labels ();
3123 mips_no_prev_insn ();
3126 /* Start a (possibly nested) noreorder block. */
3128 static void
3129 start_noreorder (void)
3131 if (mips_opts.noreorder == 0)
3133 unsigned int i;
3134 int nops;
3136 /* None of the instructions before the .set noreorder can be moved. */
3137 for (i = 0; i < ARRAY_SIZE (history); i++)
3138 history[i].fixed_p = 1;
3140 /* Insert any nops that might be needed between the .set noreorder
3141 block and the previous instructions. We will later remove any
3142 nops that turn out not to be needed. */
3143 nops = nops_for_insn (history, NULL);
3144 if (nops > 0)
3146 if (mips_optimize != 0)
3148 /* Record the frag which holds the nop instructions, so
3149 that we can remove them if we don't need them. */
3150 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
3151 prev_nop_frag = frag_now;
3152 prev_nop_frag_holds = nops;
3153 prev_nop_frag_required = 0;
3154 prev_nop_frag_since = 0;
3157 for (; nops > 0; --nops)
3158 add_fixed_insn (NOP_INSN);
3160 /* Move on to a new frag, so that it is safe to simply
3161 decrease the size of prev_nop_frag. */
3162 frag_wane (frag_now);
3163 frag_new (0);
3164 mips_move_labels ();
3166 mips16_mark_labels ();
3167 mips_clear_insn_labels ();
3169 mips_opts.noreorder++;
3170 mips_any_noreorder = 1;
3173 /* End a nested noreorder block. */
3175 static void
3176 end_noreorder (void)
3178 mips_opts.noreorder--;
3179 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
3181 /* Commit to inserting prev_nop_frag_required nops and go back to
3182 handling nop insertion the .set reorder way. */
3183 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
3184 * (mips_opts.mips16 ? 2 : 4));
3185 insert_into_history (prev_nop_frag_since,
3186 prev_nop_frag_required, NOP_INSN);
3187 prev_nop_frag = NULL;
3191 /* Set up global variables for the start of a new macro. */
3193 static void
3194 macro_start (void)
3196 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
3197 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
3198 && (history[0].insn_mo->pinfo
3199 & (INSN_UNCOND_BRANCH_DELAY
3200 | INSN_COND_BRANCH_DELAY
3201 | INSN_COND_BRANCH_LIKELY)) != 0);
3204 /* Given that a macro is longer than 4 bytes, return the appropriate warning
3205 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
3206 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
3208 static const char *
3209 macro_warning (relax_substateT subtype)
3211 if (subtype & RELAX_DELAY_SLOT)
3212 return _("Macro instruction expanded into multiple instructions"
3213 " in a branch delay slot");
3214 else if (subtype & RELAX_NOMACRO)
3215 return _("Macro instruction expanded into multiple instructions");
3216 else
3217 return 0;
3220 /* Finish up a macro. Emit warnings as appropriate. */
3222 static void
3223 macro_end (void)
3225 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
3227 relax_substateT subtype;
3229 /* Set up the relaxation warning flags. */
3230 subtype = 0;
3231 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
3232 subtype |= RELAX_SECOND_LONGER;
3233 if (mips_opts.warn_about_macros)
3234 subtype |= RELAX_NOMACRO;
3235 if (mips_macro_warning.delay_slot_p)
3236 subtype |= RELAX_DELAY_SLOT;
3238 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
3240 /* Either the macro has a single implementation or both
3241 implementations are longer than 4 bytes. Emit the
3242 warning now. */
3243 const char *msg = macro_warning (subtype);
3244 if (msg != 0)
3245 as_warn (msg);
3247 else
3249 /* One implementation might need a warning but the other
3250 definitely doesn't. */
3251 mips_macro_warning.first_frag->fr_subtype |= subtype;
3256 /* Read a macro's relocation codes from *ARGS and store them in *R.
3257 The first argument in *ARGS will be either the code for a single
3258 relocation or -1 followed by the three codes that make up a
3259 composite relocation. */
3261 static void
3262 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
3264 int i, next;
3266 next = va_arg (*args, int);
3267 if (next >= 0)
3268 r[0] = (bfd_reloc_code_real_type) next;
3269 else
3270 for (i = 0; i < 3; i++)
3271 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
3274 /* Build an instruction created by a macro expansion. This is passed
3275 a pointer to the count of instructions created so far, an
3276 expression, the name of the instruction to build, an operand format
3277 string, and corresponding arguments. */
3279 static void
3280 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
3282 const struct mips_opcode *mo;
3283 struct mips_cl_insn insn;
3284 bfd_reloc_code_real_type r[3];
3285 va_list args;
3287 va_start (args, fmt);
3289 if (mips_opts.mips16)
3291 mips16_macro_build (ep, name, fmt, args);
3292 va_end (args);
3293 return;
3296 r[0] = BFD_RELOC_UNUSED;
3297 r[1] = BFD_RELOC_UNUSED;
3298 r[2] = BFD_RELOC_UNUSED;
3299 mo = (struct mips_opcode *) hash_find (op_hash, name);
3300 assert (mo);
3301 assert (strcmp (name, mo->name) == 0);
3303 /* Search until we get a match for NAME. It is assumed here that
3304 macros will never generate MDMX or MIPS-3D instructions. */
3305 while (strcmp (fmt, mo->args) != 0
3306 || mo->pinfo == INSN_MACRO
3307 || !OPCODE_IS_MEMBER (mo,
3308 (mips_opts.isa
3309 | (mips_opts.mips16 ? INSN_MIPS16 : 0)
3310 | (mips_opts.ase_smartmips ? INSN_SMARTMIPS : 0)),
3311 mips_opts.arch)
3312 || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0))
3314 ++mo;
3315 assert (mo->name);
3316 assert (strcmp (name, mo->name) == 0);
3319 create_insn (&insn, mo);
3320 for (;;)
3322 switch (*fmt++)
3324 case '\0':
3325 break;
3327 case ',':
3328 case '(':
3329 case ')':
3330 continue;
3332 case '+':
3333 switch (*fmt++)
3335 case 'A':
3336 case 'E':
3337 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
3338 continue;
3340 case 'B':
3341 case 'F':
3342 /* Note that in the macro case, these arguments are already
3343 in MSB form. (When handling the instruction in the
3344 non-macro case, these arguments are sizes from which
3345 MSB values must be calculated.) */
3346 INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
3347 continue;
3349 case 'C':
3350 case 'G':
3351 case 'H':
3352 /* Note that in the macro case, these arguments are already
3353 in MSBD form. (When handling the instruction in the
3354 non-macro case, these arguments are sizes from which
3355 MSBD values must be calculated.) */
3356 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
3357 continue;
3359 default:
3360 internalError ();
3362 continue;
3364 case 't':
3365 case 'w':
3366 case 'E':
3367 INSERT_OPERAND (RT, insn, va_arg (args, int));
3368 continue;
3370 case 'c':
3371 INSERT_OPERAND (CODE, insn, va_arg (args, int));
3372 continue;
3374 case 'T':
3375 case 'W':
3376 INSERT_OPERAND (FT, insn, va_arg (args, int));
3377 continue;
3379 case 'd':
3380 case 'G':
3381 case 'K':
3382 INSERT_OPERAND (RD, insn, va_arg (args, int));
3383 continue;
3385 case 'U':
3387 int tmp = va_arg (args, int);
3389 INSERT_OPERAND (RT, insn, tmp);
3390 INSERT_OPERAND (RD, insn, tmp);
3391 continue;
3394 case 'V':
3395 case 'S':
3396 INSERT_OPERAND (FS, insn, va_arg (args, int));
3397 continue;
3399 case 'z':
3400 continue;
3402 case '<':
3403 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
3404 continue;
3406 case 'D':
3407 INSERT_OPERAND (FD, insn, va_arg (args, int));
3408 continue;
3410 case 'B':
3411 INSERT_OPERAND (CODE20, insn, va_arg (args, int));
3412 continue;
3414 case 'J':
3415 INSERT_OPERAND (CODE19, insn, va_arg (args, int));
3416 continue;
3418 case 'q':
3419 INSERT_OPERAND (CODE2, insn, va_arg (args, int));
3420 continue;
3422 case 'b':
3423 case 's':
3424 case 'r':
3425 case 'v':
3426 INSERT_OPERAND (RS, insn, va_arg (args, int));
3427 continue;
3429 case 'i':
3430 case 'j':
3431 case 'o':
3432 macro_read_relocs (&args, r);
3433 assert (*r == BFD_RELOC_GPREL16
3434 || *r == BFD_RELOC_MIPS_LITERAL
3435 || *r == BFD_RELOC_MIPS_HIGHER
3436 || *r == BFD_RELOC_HI16_S
3437 || *r == BFD_RELOC_LO16
3438 || *r == BFD_RELOC_MIPS_GOT16
3439 || *r == BFD_RELOC_MIPS_CALL16
3440 || *r == BFD_RELOC_MIPS_GOT_DISP
3441 || *r == BFD_RELOC_MIPS_GOT_PAGE
3442 || *r == BFD_RELOC_MIPS_GOT_OFST
3443 || *r == BFD_RELOC_MIPS_GOT_LO16
3444 || *r == BFD_RELOC_MIPS_CALL_LO16);
3445 continue;
3447 case 'u':
3448 macro_read_relocs (&args, r);
3449 assert (ep != NULL
3450 && (ep->X_op == O_constant
3451 || (ep->X_op == O_symbol
3452 && (*r == BFD_RELOC_MIPS_HIGHEST
3453 || *r == BFD_RELOC_HI16_S
3454 || *r == BFD_RELOC_HI16
3455 || *r == BFD_RELOC_GPREL16
3456 || *r == BFD_RELOC_MIPS_GOT_HI16
3457 || *r == BFD_RELOC_MIPS_CALL_HI16))));
3458 continue;
3460 case 'p':
3461 assert (ep != NULL);
3464 * This allows macro() to pass an immediate expression for
3465 * creating short branches without creating a symbol.
3467 * We don't allow branch relaxation for these branches, as
3468 * they should only appear in ".set nomacro" anyway.
3470 if (ep->X_op == O_constant)
3472 if ((ep->X_add_number & 3) != 0)
3473 as_bad (_("branch to misaligned address (0x%lx)"),
3474 (unsigned long) ep->X_add_number);
3475 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
3476 as_bad (_("branch address range overflow (0x%lx)"),
3477 (unsigned long) ep->X_add_number);
3478 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3479 ep = NULL;
3481 else
3482 *r = BFD_RELOC_16_PCREL_S2;
3483 continue;
3485 case 'a':
3486 assert (ep != NULL);
3487 *r = BFD_RELOC_MIPS_JMP;
3488 continue;
3490 case 'C':
3491 insn.insn_opcode |= va_arg (args, unsigned long);
3492 continue;
3494 case 'k':
3495 insn.insn_opcode |= va_arg (args, unsigned long) << OP_SH_CACHE;
3496 continue;
3498 default:
3499 internalError ();
3501 break;
3503 va_end (args);
3504 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3506 append_insn (&insn, ep, r);
3509 static void
3510 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
3511 va_list args)
3513 struct mips_opcode *mo;
3514 struct mips_cl_insn insn;
3515 bfd_reloc_code_real_type r[3]
3516 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3518 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3519 assert (mo);
3520 assert (strcmp (name, mo->name) == 0);
3522 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
3524 ++mo;
3525 assert (mo->name);
3526 assert (strcmp (name, mo->name) == 0);
3529 create_insn (&insn, mo);
3530 for (;;)
3532 int c;
3534 c = *fmt++;
3535 switch (c)
3537 case '\0':
3538 break;
3540 case ',':
3541 case '(':
3542 case ')':
3543 continue;
3545 case 'y':
3546 case 'w':
3547 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
3548 continue;
3550 case 'x':
3551 case 'v':
3552 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
3553 continue;
3555 case 'z':
3556 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
3557 continue;
3559 case 'Z':
3560 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
3561 continue;
3563 case '0':
3564 case 'S':
3565 case 'P':
3566 case 'R':
3567 continue;
3569 case 'X':
3570 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
3571 continue;
3573 case 'Y':
3575 int regno;
3577 regno = va_arg (args, int);
3578 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3579 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3581 continue;
3583 case '<':
3584 case '>':
3585 case '4':
3586 case '5':
3587 case 'H':
3588 case 'W':
3589 case 'D':
3590 case 'j':
3591 case '8':
3592 case 'V':
3593 case 'C':
3594 case 'U':
3595 case 'k':
3596 case 'K':
3597 case 'p':
3598 case 'q':
3600 assert (ep != NULL);
3602 if (ep->X_op != O_constant)
3603 *r = (int) BFD_RELOC_UNUSED + c;
3604 else
3606 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3607 FALSE, &insn.insn_opcode, &insn.use_extend,
3608 &insn.extend);
3609 ep = NULL;
3610 *r = BFD_RELOC_UNUSED;
3613 continue;
3615 case '6':
3616 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
3617 continue;
3620 break;
3623 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3625 append_insn (&insn, ep, r);
3629 * Sign-extend 32-bit mode constants that have bit 31 set and all
3630 * higher bits unset.
3632 static void
3633 normalize_constant_expr (expressionS *ex)
3635 if (ex->X_op == O_constant
3636 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3637 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3638 - 0x80000000);
3642 * Sign-extend 32-bit mode address offsets that have bit 31 set and
3643 * all higher bits unset.
3645 static void
3646 normalize_address_expr (expressionS *ex)
3648 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
3649 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
3650 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3651 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3652 - 0x80000000);
3656 * Generate a "jalr" instruction with a relocation hint to the called
3657 * function. This occurs in NewABI PIC code.
3659 static void
3660 macro_build_jalr (expressionS *ep)
3662 char *f = NULL;
3664 if (HAVE_NEWABI)
3666 frag_grow (8);
3667 f = frag_more (0);
3669 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3670 if (HAVE_NEWABI)
3671 fix_new_exp (frag_now, f - frag_now->fr_literal,
3672 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3676 * Generate a "lui" instruction.
3678 static void
3679 macro_build_lui (expressionS *ep, int regnum)
3681 expressionS high_expr;
3682 const struct mips_opcode *mo;
3683 struct mips_cl_insn insn;
3684 bfd_reloc_code_real_type r[3]
3685 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3686 const char *name = "lui";
3687 const char *fmt = "t,u";
3689 assert (! mips_opts.mips16);
3691 high_expr = *ep;
3693 if (high_expr.X_op == O_constant)
3695 /* we can compute the instruction now without a relocation entry */
3696 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3697 >> 16) & 0xffff;
3698 *r = BFD_RELOC_UNUSED;
3700 else
3702 assert (ep->X_op == O_symbol);
3703 /* _gp_disp is a special case, used from s_cpload.
3704 __gnu_local_gp is used if mips_no_shared. */
3705 assert (mips_pic == NO_PIC
3706 || (! HAVE_NEWABI
3707 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3708 || (! mips_in_shared
3709 && strcmp (S_GET_NAME (ep->X_add_symbol),
3710 "__gnu_local_gp") == 0));
3711 *r = BFD_RELOC_HI16_S;
3714 mo = hash_find (op_hash, name);
3715 assert (strcmp (name, mo->name) == 0);
3716 assert (strcmp (fmt, mo->args) == 0);
3717 create_insn (&insn, mo);
3719 insn.insn_opcode = insn.insn_mo->match;
3720 INSERT_OPERAND (RT, insn, regnum);
3721 if (*r == BFD_RELOC_UNUSED)
3723 insn.insn_opcode |= high_expr.X_add_number;
3724 append_insn (&insn, NULL, r);
3726 else
3727 append_insn (&insn, &high_expr, r);
3730 /* Generate a sequence of instructions to do a load or store from a constant
3731 offset off of a base register (breg) into/from a target register (treg),
3732 using AT if necessary. */
3733 static void
3734 macro_build_ldst_constoffset (expressionS *ep, const char *op,
3735 int treg, int breg, int dbl)
3737 assert (ep->X_op == O_constant);
3739 /* Sign-extending 32-bit constants makes their handling easier. */
3740 if (!dbl)
3741 normalize_constant_expr (ep);
3743 /* Right now, this routine can only handle signed 32-bit constants. */
3744 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3745 as_warn (_("operand overflow"));
3747 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3749 /* Signed 16-bit offset will fit in the op. Easy! */
3750 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
3752 else
3754 /* 32-bit offset, need multiple instructions and AT, like:
3755 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3756 addu $tempreg,$tempreg,$breg
3757 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3758 to handle the complete offset. */
3759 macro_build_lui (ep, AT);
3760 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3761 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
3763 if (mips_opts.noat)
3764 as_bad (_("Macro used $at after \".set noat\""));
3768 /* set_at()
3769 * Generates code to set the $at register to true (one)
3770 * if reg is less than the immediate expression.
3772 static void
3773 set_at (int reg, int unsignedp)
3775 if (imm_expr.X_op == O_constant
3776 && imm_expr.X_add_number >= -0x8000
3777 && imm_expr.X_add_number < 0x8000)
3778 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3779 AT, reg, BFD_RELOC_LO16);
3780 else
3782 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3783 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
3787 /* Warn if an expression is not a constant. */
3789 static void
3790 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3792 if (ex->X_op == O_big)
3793 as_bad (_("unsupported large constant"));
3794 else if (ex->X_op != O_constant)
3795 as_bad (_("Instruction %s requires absolute expression"),
3796 ip->insn_mo->name);
3798 if (HAVE_32BIT_GPRS)
3799 normalize_constant_expr (ex);
3802 /* Count the leading zeroes by performing a binary chop. This is a
3803 bulky bit of source, but performance is a LOT better for the
3804 majority of values than a simple loop to count the bits:
3805 for (lcnt = 0; (lcnt < 32); lcnt++)
3806 if ((v) & (1 << (31 - lcnt)))
3807 break;
3808 However it is not code size friendly, and the gain will drop a bit
3809 on certain cached systems.
3811 #define COUNT_TOP_ZEROES(v) \
3812 (((v) & ~0xffff) == 0 \
3813 ? ((v) & ~0xff) == 0 \
3814 ? ((v) & ~0xf) == 0 \
3815 ? ((v) & ~0x3) == 0 \
3816 ? ((v) & ~0x1) == 0 \
3817 ? !(v) \
3818 ? 32 \
3819 : 31 \
3820 : 30 \
3821 : ((v) & ~0x7) == 0 \
3822 ? 29 \
3823 : 28 \
3824 : ((v) & ~0x3f) == 0 \
3825 ? ((v) & ~0x1f) == 0 \
3826 ? 27 \
3827 : 26 \
3828 : ((v) & ~0x7f) == 0 \
3829 ? 25 \
3830 : 24 \
3831 : ((v) & ~0xfff) == 0 \
3832 ? ((v) & ~0x3ff) == 0 \
3833 ? ((v) & ~0x1ff) == 0 \
3834 ? 23 \
3835 : 22 \
3836 : ((v) & ~0x7ff) == 0 \
3837 ? 21 \
3838 : 20 \
3839 : ((v) & ~0x3fff) == 0 \
3840 ? ((v) & ~0x1fff) == 0 \
3841 ? 19 \
3842 : 18 \
3843 : ((v) & ~0x7fff) == 0 \
3844 ? 17 \
3845 : 16 \
3846 : ((v) & ~0xffffff) == 0 \
3847 ? ((v) & ~0xfffff) == 0 \
3848 ? ((v) & ~0x3ffff) == 0 \
3849 ? ((v) & ~0x1ffff) == 0 \
3850 ? 15 \
3851 : 14 \
3852 : ((v) & ~0x7ffff) == 0 \
3853 ? 13 \
3854 : 12 \
3855 : ((v) & ~0x3fffff) == 0 \
3856 ? ((v) & ~0x1fffff) == 0 \
3857 ? 11 \
3858 : 10 \
3859 : ((v) & ~0x7fffff) == 0 \
3860 ? 9 \
3861 : 8 \
3862 : ((v) & ~0xfffffff) == 0 \
3863 ? ((v) & ~0x3ffffff) == 0 \
3864 ? ((v) & ~0x1ffffff) == 0 \
3865 ? 7 \
3866 : 6 \
3867 : ((v) & ~0x7ffffff) == 0 \
3868 ? 5 \
3869 : 4 \
3870 : ((v) & ~0x3fffffff) == 0 \
3871 ? ((v) & ~0x1fffffff) == 0 \
3872 ? 3 \
3873 : 2 \
3874 : ((v) & ~0x7fffffff) == 0 \
3875 ? 1 \
3876 : 0)
3878 /* load_register()
3879 * This routine generates the least number of instructions necessary to load
3880 * an absolute expression value into a register.
3882 static void
3883 load_register (int reg, expressionS *ep, int dbl)
3885 int freg;
3886 expressionS hi32, lo32;
3888 if (ep->X_op != O_big)
3890 assert (ep->X_op == O_constant);
3892 /* Sign-extending 32-bit constants makes their handling easier. */
3893 if (!dbl)
3894 normalize_constant_expr (ep);
3896 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3898 /* We can handle 16 bit signed values with an addiu to
3899 $zero. No need to ever use daddiu here, since $zero and
3900 the result are always correct in 32 bit mode. */
3901 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3902 return;
3904 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3906 /* We can handle 16 bit unsigned values with an ori to
3907 $zero. */
3908 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3909 return;
3911 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3913 /* 32 bit values require an lui. */
3914 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3915 if ((ep->X_add_number & 0xffff) != 0)
3916 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3917 return;
3921 /* The value is larger than 32 bits. */
3923 if (!dbl || HAVE_32BIT_GPRS)
3925 char value[32];
3927 sprintf_vma (value, ep->X_add_number);
3928 as_bad (_("Number (0x%s) larger than 32 bits"), value);
3929 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3930 return;
3933 if (ep->X_op != O_big)
3935 hi32 = *ep;
3936 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3937 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3938 hi32.X_add_number &= 0xffffffff;
3939 lo32 = *ep;
3940 lo32.X_add_number &= 0xffffffff;
3942 else
3944 assert (ep->X_add_number > 2);
3945 if (ep->X_add_number == 3)
3946 generic_bignum[3] = 0;
3947 else if (ep->X_add_number > 4)
3948 as_bad (_("Number larger than 64 bits"));
3949 lo32.X_op = O_constant;
3950 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3951 hi32.X_op = O_constant;
3952 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3955 if (hi32.X_add_number == 0)
3956 freg = 0;
3957 else
3959 int shift, bit;
3960 unsigned long hi, lo;
3962 if (hi32.X_add_number == (offsetT) 0xffffffff)
3964 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3966 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3967 return;
3969 if (lo32.X_add_number & 0x80000000)
3971 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3972 if (lo32.X_add_number & 0xffff)
3973 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3974 return;
3978 /* Check for 16bit shifted constant. We know that hi32 is
3979 non-zero, so start the mask on the first bit of the hi32
3980 value. */
3981 shift = 17;
3984 unsigned long himask, lomask;
3986 if (shift < 32)
3988 himask = 0xffff >> (32 - shift);
3989 lomask = (0xffff << shift) & 0xffffffff;
3991 else
3993 himask = 0xffff << (shift - 32);
3994 lomask = 0;
3996 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3997 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3999 expressionS tmp;
4001 tmp.X_op = O_constant;
4002 if (shift < 32)
4003 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
4004 | (lo32.X_add_number >> shift));
4005 else
4006 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
4007 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
4008 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
4009 reg, reg, (shift >= 32) ? shift - 32 : shift);
4010 return;
4012 ++shift;
4014 while (shift <= (64 - 16));
4016 /* Find the bit number of the lowest one bit, and store the
4017 shifted value in hi/lo. */
4018 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
4019 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
4020 if (lo != 0)
4022 bit = 0;
4023 while ((lo & 1) == 0)
4025 lo >>= 1;
4026 ++bit;
4028 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
4029 hi >>= bit;
4031 else
4033 bit = 32;
4034 while ((hi & 1) == 0)
4036 hi >>= 1;
4037 ++bit;
4039 lo = hi;
4040 hi = 0;
4043 /* Optimize if the shifted value is a (power of 2) - 1. */
4044 if ((hi == 0 && ((lo + 1) & lo) == 0)
4045 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
4047 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
4048 if (shift != 0)
4050 expressionS tmp;
4052 /* This instruction will set the register to be all
4053 ones. */
4054 tmp.X_op = O_constant;
4055 tmp.X_add_number = (offsetT) -1;
4056 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
4057 if (bit != 0)
4059 bit += shift;
4060 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
4061 reg, reg, (bit >= 32) ? bit - 32 : bit);
4063 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
4064 reg, reg, (shift >= 32) ? shift - 32 : shift);
4065 return;
4069 /* Sign extend hi32 before calling load_register, because we can
4070 generally get better code when we load a sign extended value. */
4071 if ((hi32.X_add_number & 0x80000000) != 0)
4072 hi32.X_add_number |= ~(offsetT) 0xffffffff;
4073 load_register (reg, &hi32, 0);
4074 freg = reg;
4076 if ((lo32.X_add_number & 0xffff0000) == 0)
4078 if (freg != 0)
4080 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
4081 freg = reg;
4084 else
4086 expressionS mid16;
4088 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
4090 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
4091 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
4092 return;
4095 if (freg != 0)
4097 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
4098 freg = reg;
4100 mid16 = lo32;
4101 mid16.X_add_number >>= 16;
4102 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
4103 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4104 freg = reg;
4106 if ((lo32.X_add_number & 0xffff) != 0)
4107 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
4110 static inline void
4111 load_delay_nop (void)
4113 if (!gpr_interlocks)
4114 macro_build (NULL, "nop", "");
4117 /* Load an address into a register. */
4119 static void
4120 load_address (int reg, expressionS *ep, int *used_at)
4122 if (ep->X_op != O_constant
4123 && ep->X_op != O_symbol)
4125 as_bad (_("expression too complex"));
4126 ep->X_op = O_constant;
4129 if (ep->X_op == O_constant)
4131 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
4132 return;
4135 if (mips_pic == NO_PIC)
4137 /* If this is a reference to a GP relative symbol, we want
4138 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
4139 Otherwise we want
4140 lui $reg,<sym> (BFD_RELOC_HI16_S)
4141 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
4142 If we have an addend, we always use the latter form.
4144 With 64bit address space and a usable $at we want
4145 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4146 lui $at,<sym> (BFD_RELOC_HI16_S)
4147 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
4148 daddiu $at,<sym> (BFD_RELOC_LO16)
4149 dsll32 $reg,0
4150 daddu $reg,$reg,$at
4152 If $at is already in use, we use a path which is suboptimal
4153 on superscalar processors.
4154 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4155 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
4156 dsll $reg,16
4157 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
4158 dsll $reg,16
4159 daddiu $reg,<sym> (BFD_RELOC_LO16)
4161 For GP relative symbols in 64bit address space we can use
4162 the same sequence as in 32bit address space. */
4163 if (HAVE_64BIT_SYMBOLS)
4165 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
4166 && !nopic_need_relax (ep->X_add_symbol, 1))
4168 relax_start (ep->X_add_symbol);
4169 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
4170 mips_gp_register, BFD_RELOC_GPREL16);
4171 relax_switch ();
4174 if (*used_at == 0 && !mips_opts.noat)
4176 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
4177 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
4178 macro_build (ep, "daddiu", "t,r,j", reg, reg,
4179 BFD_RELOC_MIPS_HIGHER);
4180 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
4181 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
4182 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
4183 *used_at = 1;
4185 else
4187 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
4188 macro_build (ep, "daddiu", "t,r,j", reg, reg,
4189 BFD_RELOC_MIPS_HIGHER);
4190 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4191 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
4192 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4193 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
4196 if (mips_relax.sequence)
4197 relax_end ();
4199 else
4201 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
4202 && !nopic_need_relax (ep->X_add_symbol, 1))
4204 relax_start (ep->X_add_symbol);
4205 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
4206 mips_gp_register, BFD_RELOC_GPREL16);
4207 relax_switch ();
4209 macro_build_lui (ep, reg);
4210 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
4211 reg, reg, BFD_RELOC_LO16);
4212 if (mips_relax.sequence)
4213 relax_end ();
4216 else if (!mips_big_got)
4218 expressionS ex;
4220 /* If this is a reference to an external symbol, we want
4221 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4222 Otherwise we want
4223 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4225 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
4226 If there is a constant, it must be added in after.
4228 If we have NewABI, we want
4229 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4230 unless we're referencing a global symbol with a non-zero
4231 offset, in which case cst must be added separately. */
4232 if (HAVE_NEWABI)
4234 if (ep->X_add_number)
4236 ex.X_add_number = ep->X_add_number;
4237 ep->X_add_number = 0;
4238 relax_start (ep->X_add_symbol);
4239 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4240 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4241 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4242 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4243 ex.X_op = O_constant;
4244 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
4245 reg, reg, BFD_RELOC_LO16);
4246 ep->X_add_number = ex.X_add_number;
4247 relax_switch ();
4249 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4250 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4251 if (mips_relax.sequence)
4252 relax_end ();
4254 else
4256 ex.X_add_number = ep->X_add_number;
4257 ep->X_add_number = 0;
4258 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4259 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4260 load_delay_nop ();
4261 relax_start (ep->X_add_symbol);
4262 relax_switch ();
4263 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4264 BFD_RELOC_LO16);
4265 relax_end ();
4267 if (ex.X_add_number != 0)
4269 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4270 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4271 ex.X_op = O_constant;
4272 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
4273 reg, reg, BFD_RELOC_LO16);
4277 else if (mips_big_got)
4279 expressionS ex;
4281 /* This is the large GOT case. If this is a reference to an
4282 external symbol, we want
4283 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4284 addu $reg,$reg,$gp
4285 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
4287 Otherwise, for a reference to a local symbol in old ABI, we want
4288 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4290 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
4291 If there is a constant, it must be added in after.
4293 In the NewABI, for local symbols, with or without offsets, we want:
4294 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4295 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4297 if (HAVE_NEWABI)
4299 ex.X_add_number = ep->X_add_number;
4300 ep->X_add_number = 0;
4301 relax_start (ep->X_add_symbol);
4302 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4303 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4304 reg, reg, mips_gp_register);
4305 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4306 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4307 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4308 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4309 else if (ex.X_add_number)
4311 ex.X_op = O_constant;
4312 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4313 BFD_RELOC_LO16);
4316 ep->X_add_number = ex.X_add_number;
4317 relax_switch ();
4318 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4319 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
4320 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4321 BFD_RELOC_MIPS_GOT_OFST);
4322 relax_end ();
4324 else
4326 ex.X_add_number = ep->X_add_number;
4327 ep->X_add_number = 0;
4328 relax_start (ep->X_add_symbol);
4329 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4330 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4331 reg, reg, mips_gp_register);
4332 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4333 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4334 relax_switch ();
4335 if (reg_needs_delay (mips_gp_register))
4337 /* We need a nop before loading from $gp. This special
4338 check is required because the lui which starts the main
4339 instruction stream does not refer to $gp, and so will not
4340 insert the nop which may be required. */
4341 macro_build (NULL, "nop", "");
4343 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4344 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4345 load_delay_nop ();
4346 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4347 BFD_RELOC_LO16);
4348 relax_end ();
4350 if (ex.X_add_number != 0)
4352 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4353 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4354 ex.X_op = O_constant;
4355 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4356 BFD_RELOC_LO16);
4360 else
4361 abort ();
4363 if (mips_opts.noat && *used_at == 1)
4364 as_bad (_("Macro used $at after \".set noat\""));
4367 /* Move the contents of register SOURCE into register DEST. */
4369 static void
4370 move_register (int dest, int source)
4372 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4373 dest, source, 0);
4376 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
4377 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4378 The two alternatives are:
4380 Global symbol Local sybmol
4381 ------------- ------------
4382 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4383 ... ...
4384 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4386 load_got_offset emits the first instruction and add_got_offset
4387 emits the second for a 16-bit offset or add_got_offset_hilo emits
4388 a sequence to add a 32-bit offset using a scratch register. */
4390 static void
4391 load_got_offset (int dest, expressionS *local)
4393 expressionS global;
4395 global = *local;
4396 global.X_add_number = 0;
4398 relax_start (local->X_add_symbol);
4399 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4400 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4401 relax_switch ();
4402 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4403 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4404 relax_end ();
4407 static void
4408 add_got_offset (int dest, expressionS *local)
4410 expressionS global;
4412 global.X_op = O_constant;
4413 global.X_op_symbol = NULL;
4414 global.X_add_symbol = NULL;
4415 global.X_add_number = local->X_add_number;
4417 relax_start (local->X_add_symbol);
4418 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4419 dest, dest, BFD_RELOC_LO16);
4420 relax_switch ();
4421 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4422 relax_end ();
4425 static void
4426 add_got_offset_hilo (int dest, expressionS *local, int tmp)
4428 expressionS global;
4429 int hold_mips_optimize;
4431 global.X_op = O_constant;
4432 global.X_op_symbol = NULL;
4433 global.X_add_symbol = NULL;
4434 global.X_add_number = local->X_add_number;
4436 relax_start (local->X_add_symbol);
4437 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4438 relax_switch ();
4439 /* Set mips_optimize around the lui instruction to avoid
4440 inserting an unnecessary nop after the lw. */
4441 hold_mips_optimize = mips_optimize;
4442 mips_optimize = 2;
4443 macro_build_lui (&global, tmp);
4444 mips_optimize = hold_mips_optimize;
4445 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4446 relax_end ();
4448 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4452 * Build macros
4453 * This routine implements the seemingly endless macro or synthesized
4454 * instructions and addressing modes in the mips assembly language. Many
4455 * of these macros are simple and are similar to each other. These could
4456 * probably be handled by some kind of table or grammar approach instead of
4457 * this verbose method. Others are not simple macros but are more like
4458 * optimizing code generation.
4459 * One interesting optimization is when several store macros appear
4460 * consecutively that would load AT with the upper half of the same address.
4461 * The ensuing load upper instructions are ommited. This implies some kind
4462 * of global optimization. We currently only optimize within a single macro.
4463 * For many of the load and store macros if the address is specified as a
4464 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4465 * first load register 'at' with zero and use it as the base register. The
4466 * mips assembler simply uses register $zero. Just one tiny optimization
4467 * we're missing.
4469 static void
4470 macro (struct mips_cl_insn *ip)
4472 register int treg, sreg, dreg, breg;
4473 int tempreg;
4474 int mask;
4475 int used_at = 0;
4476 expressionS expr1;
4477 const char *s;
4478 const char *s2;
4479 const char *fmt;
4480 int likely = 0;
4481 int dbl = 0;
4482 int coproc = 0;
4483 int lr = 0;
4484 int imm = 0;
4485 int call = 0;
4486 int off;
4487 offsetT maxnum;
4488 bfd_reloc_code_real_type r;
4489 int hold_mips_optimize;
4491 assert (! mips_opts.mips16);
4493 treg = (ip->insn_opcode >> 16) & 0x1f;
4494 dreg = (ip->insn_opcode >> 11) & 0x1f;
4495 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4496 mask = ip->insn_mo->mask;
4498 expr1.X_op = O_constant;
4499 expr1.X_op_symbol = NULL;
4500 expr1.X_add_symbol = NULL;
4501 expr1.X_add_number = 1;
4503 switch (mask)
4505 case M_DABS:
4506 dbl = 1;
4507 case M_ABS:
4508 /* bgez $a0,.+12
4509 move v0,$a0
4510 sub v0,$zero,$a0
4513 start_noreorder ();
4515 expr1.X_add_number = 8;
4516 macro_build (&expr1, "bgez", "s,p", sreg);
4517 if (dreg == sreg)
4518 macro_build (NULL, "nop", "", 0);
4519 else
4520 move_register (dreg, sreg);
4521 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4523 end_noreorder ();
4524 break;
4526 case M_ADD_I:
4527 s = "addi";
4528 s2 = "add";
4529 goto do_addi;
4530 case M_ADDU_I:
4531 s = "addiu";
4532 s2 = "addu";
4533 goto do_addi;
4534 case M_DADD_I:
4535 dbl = 1;
4536 s = "daddi";
4537 s2 = "dadd";
4538 goto do_addi;
4539 case M_DADDU_I:
4540 dbl = 1;
4541 s = "daddiu";
4542 s2 = "daddu";
4543 do_addi:
4544 if (imm_expr.X_op == O_constant
4545 && imm_expr.X_add_number >= -0x8000
4546 && imm_expr.X_add_number < 0x8000)
4548 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
4549 break;
4551 used_at = 1;
4552 load_register (AT, &imm_expr, dbl);
4553 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4554 break;
4556 case M_AND_I:
4557 s = "andi";
4558 s2 = "and";
4559 goto do_bit;
4560 case M_OR_I:
4561 s = "ori";
4562 s2 = "or";
4563 goto do_bit;
4564 case M_NOR_I:
4565 s = "";
4566 s2 = "nor";
4567 goto do_bit;
4568 case M_XOR_I:
4569 s = "xori";
4570 s2 = "xor";
4571 do_bit:
4572 if (imm_expr.X_op == O_constant
4573 && imm_expr.X_add_number >= 0
4574 && imm_expr.X_add_number < 0x10000)
4576 if (mask != M_NOR_I)
4577 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
4578 else
4580 macro_build (&imm_expr, "ori", "t,r,i",
4581 treg, sreg, BFD_RELOC_LO16);
4582 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
4584 break;
4587 used_at = 1;
4588 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4589 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4590 break;
4592 case M_BEQ_I:
4593 s = "beq";
4594 goto beq_i;
4595 case M_BEQL_I:
4596 s = "beql";
4597 likely = 1;
4598 goto beq_i;
4599 case M_BNE_I:
4600 s = "bne";
4601 goto beq_i;
4602 case M_BNEL_I:
4603 s = "bnel";
4604 likely = 1;
4605 beq_i:
4606 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4608 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
4609 break;
4611 used_at = 1;
4612 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4613 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
4614 break;
4616 case M_BGEL:
4617 likely = 1;
4618 case M_BGE:
4619 if (treg == 0)
4621 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4622 break;
4624 if (sreg == 0)
4626 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
4627 break;
4629 used_at = 1;
4630 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4631 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4632 break;
4634 case M_BGTL_I:
4635 likely = 1;
4636 case M_BGT_I:
4637 /* check for > max integer */
4638 maxnum = 0x7fffffff;
4639 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4641 maxnum <<= 16;
4642 maxnum |= 0xffff;
4643 maxnum <<= 16;
4644 maxnum |= 0xffff;
4646 if (imm_expr.X_op == O_constant
4647 && imm_expr.X_add_number >= maxnum
4648 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4650 do_false:
4651 /* result is always false */
4652 if (! likely)
4653 macro_build (NULL, "nop", "", 0);
4654 else
4655 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
4656 break;
4658 if (imm_expr.X_op != O_constant)
4659 as_bad (_("Unsupported large constant"));
4660 ++imm_expr.X_add_number;
4661 /* FALLTHROUGH */
4662 case M_BGE_I:
4663 case M_BGEL_I:
4664 if (mask == M_BGEL_I)
4665 likely = 1;
4666 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4668 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4669 break;
4671 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4673 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4674 break;
4676 maxnum = 0x7fffffff;
4677 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4679 maxnum <<= 16;
4680 maxnum |= 0xffff;
4681 maxnum <<= 16;
4682 maxnum |= 0xffff;
4684 maxnum = - maxnum - 1;
4685 if (imm_expr.X_op == O_constant
4686 && imm_expr.X_add_number <= maxnum
4687 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4689 do_true:
4690 /* result is always true */
4691 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4692 macro_build (&offset_expr, "b", "p");
4693 break;
4695 used_at = 1;
4696 set_at (sreg, 0);
4697 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4698 break;
4700 case M_BGEUL:
4701 likely = 1;
4702 case M_BGEU:
4703 if (treg == 0)
4704 goto do_true;
4705 if (sreg == 0)
4707 macro_build (&offset_expr, likely ? "beql" : "beq",
4708 "s,t,p", 0, treg);
4709 break;
4711 used_at = 1;
4712 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4713 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4714 break;
4716 case M_BGTUL_I:
4717 likely = 1;
4718 case M_BGTU_I:
4719 if (sreg == 0
4720 || (HAVE_32BIT_GPRS
4721 && imm_expr.X_op == O_constant
4722 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4723 goto do_false;
4724 if (imm_expr.X_op != O_constant)
4725 as_bad (_("Unsupported large constant"));
4726 ++imm_expr.X_add_number;
4727 /* FALLTHROUGH */
4728 case M_BGEU_I:
4729 case M_BGEUL_I:
4730 if (mask == M_BGEUL_I)
4731 likely = 1;
4732 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4733 goto do_true;
4734 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4736 macro_build (&offset_expr, likely ? "bnel" : "bne",
4737 "s,t,p", sreg, 0);
4738 break;
4740 used_at = 1;
4741 set_at (sreg, 1);
4742 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4743 break;
4745 case M_BGTL:
4746 likely = 1;
4747 case M_BGT:
4748 if (treg == 0)
4750 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4751 break;
4753 if (sreg == 0)
4755 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
4756 break;
4758 used_at = 1;
4759 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4760 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4761 break;
4763 case M_BGTUL:
4764 likely = 1;
4765 case M_BGTU:
4766 if (treg == 0)
4768 macro_build (&offset_expr, likely ? "bnel" : "bne",
4769 "s,t,p", sreg, 0);
4770 break;
4772 if (sreg == 0)
4773 goto do_false;
4774 used_at = 1;
4775 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4776 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4777 break;
4779 case M_BLEL:
4780 likely = 1;
4781 case M_BLE:
4782 if (treg == 0)
4784 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4785 break;
4787 if (sreg == 0)
4789 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
4790 break;
4792 used_at = 1;
4793 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4794 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4795 break;
4797 case M_BLEL_I:
4798 likely = 1;
4799 case M_BLE_I:
4800 maxnum = 0x7fffffff;
4801 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4803 maxnum <<= 16;
4804 maxnum |= 0xffff;
4805 maxnum <<= 16;
4806 maxnum |= 0xffff;
4808 if (imm_expr.X_op == O_constant
4809 && imm_expr.X_add_number >= maxnum
4810 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4811 goto do_true;
4812 if (imm_expr.X_op != O_constant)
4813 as_bad (_("Unsupported large constant"));
4814 ++imm_expr.X_add_number;
4815 /* FALLTHROUGH */
4816 case M_BLT_I:
4817 case M_BLTL_I:
4818 if (mask == M_BLTL_I)
4819 likely = 1;
4820 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4822 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4823 break;
4825 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4827 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4828 break;
4830 used_at = 1;
4831 set_at (sreg, 0);
4832 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4833 break;
4835 case M_BLEUL:
4836 likely = 1;
4837 case M_BLEU:
4838 if (treg == 0)
4840 macro_build (&offset_expr, likely ? "beql" : "beq",
4841 "s,t,p", sreg, 0);
4842 break;
4844 if (sreg == 0)
4845 goto do_true;
4846 used_at = 1;
4847 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4848 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4849 break;
4851 case M_BLEUL_I:
4852 likely = 1;
4853 case M_BLEU_I:
4854 if (sreg == 0
4855 || (HAVE_32BIT_GPRS
4856 && imm_expr.X_op == O_constant
4857 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4858 goto do_true;
4859 if (imm_expr.X_op != O_constant)
4860 as_bad (_("Unsupported large constant"));
4861 ++imm_expr.X_add_number;
4862 /* FALLTHROUGH */
4863 case M_BLTU_I:
4864 case M_BLTUL_I:
4865 if (mask == M_BLTUL_I)
4866 likely = 1;
4867 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4868 goto do_false;
4869 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4871 macro_build (&offset_expr, likely ? "beql" : "beq",
4872 "s,t,p", sreg, 0);
4873 break;
4875 used_at = 1;
4876 set_at (sreg, 1);
4877 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4878 break;
4880 case M_BLTL:
4881 likely = 1;
4882 case M_BLT:
4883 if (treg == 0)
4885 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4886 break;
4888 if (sreg == 0)
4890 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
4891 break;
4893 used_at = 1;
4894 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4895 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4896 break;
4898 case M_BLTUL:
4899 likely = 1;
4900 case M_BLTU:
4901 if (treg == 0)
4902 goto do_false;
4903 if (sreg == 0)
4905 macro_build (&offset_expr, likely ? "bnel" : "bne",
4906 "s,t,p", 0, treg);
4907 break;
4909 used_at = 1;
4910 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4911 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4912 break;
4914 case M_DEXT:
4916 unsigned long pos;
4917 unsigned long size;
4919 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4921 as_bad (_("Unsupported large constant"));
4922 pos = size = 1;
4924 else
4926 pos = (unsigned long) imm_expr.X_add_number;
4927 size = (unsigned long) imm2_expr.X_add_number;
4930 if (pos > 63)
4932 as_bad (_("Improper position (%lu)"), pos);
4933 pos = 1;
4935 if (size == 0 || size > 64
4936 || (pos + size - 1) > 63)
4938 as_bad (_("Improper extract size (%lu, position %lu)"),
4939 size, pos);
4940 size = 1;
4943 if (size <= 32 && pos < 32)
4945 s = "dext";
4946 fmt = "t,r,+A,+C";
4948 else if (size <= 32)
4950 s = "dextu";
4951 fmt = "t,r,+E,+H";
4953 else
4955 s = "dextm";
4956 fmt = "t,r,+A,+G";
4958 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
4960 break;
4962 case M_DINS:
4964 unsigned long pos;
4965 unsigned long size;
4967 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4969 as_bad (_("Unsupported large constant"));
4970 pos = size = 1;
4972 else
4974 pos = (unsigned long) imm_expr.X_add_number;
4975 size = (unsigned long) imm2_expr.X_add_number;
4978 if (pos > 63)
4980 as_bad (_("Improper position (%lu)"), pos);
4981 pos = 1;
4983 if (size == 0 || size > 64
4984 || (pos + size - 1) > 63)
4986 as_bad (_("Improper insert size (%lu, position %lu)"),
4987 size, pos);
4988 size = 1;
4991 if (pos < 32 && (pos + size - 1) < 32)
4993 s = "dins";
4994 fmt = "t,r,+A,+B";
4996 else if (pos >= 32)
4998 s = "dinsu";
4999 fmt = "t,r,+E,+F";
5001 else
5003 s = "dinsm";
5004 fmt = "t,r,+A,+F";
5006 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
5007 pos + size - 1);
5009 break;
5011 case M_DDIV_3:
5012 dbl = 1;
5013 case M_DIV_3:
5014 s = "mflo";
5015 goto do_div3;
5016 case M_DREM_3:
5017 dbl = 1;
5018 case M_REM_3:
5019 s = "mfhi";
5020 do_div3:
5021 if (treg == 0)
5023 as_warn (_("Divide by zero."));
5024 if (mips_trap)
5025 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
5026 else
5027 macro_build (NULL, "break", "c", 7);
5028 break;
5031 start_noreorder ();
5032 if (mips_trap)
5034 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
5035 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
5037 else
5039 expr1.X_add_number = 8;
5040 macro_build (&expr1, "bne", "s,t,p", treg, 0);
5041 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
5042 macro_build (NULL, "break", "c", 7);
5044 expr1.X_add_number = -1;
5045 used_at = 1;
5046 load_register (AT, &expr1, dbl);
5047 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
5048 macro_build (&expr1, "bne", "s,t,p", treg, AT);
5049 if (dbl)
5051 expr1.X_add_number = 1;
5052 load_register (AT, &expr1, dbl);
5053 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
5055 else
5057 expr1.X_add_number = 0x80000000;
5058 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
5060 if (mips_trap)
5062 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
5063 /* We want to close the noreorder block as soon as possible, so
5064 that later insns are available for delay slot filling. */
5065 end_noreorder ();
5067 else
5069 expr1.X_add_number = 8;
5070 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
5071 macro_build (NULL, "nop", "", 0);
5073 /* We want to close the noreorder block as soon as possible, so
5074 that later insns are available for delay slot filling. */
5075 end_noreorder ();
5077 macro_build (NULL, "break", "c", 6);
5079 macro_build (NULL, s, "d", dreg);
5080 break;
5082 case M_DIV_3I:
5083 s = "div";
5084 s2 = "mflo";
5085 goto do_divi;
5086 case M_DIVU_3I:
5087 s = "divu";
5088 s2 = "mflo";
5089 goto do_divi;
5090 case M_REM_3I:
5091 s = "div";
5092 s2 = "mfhi";
5093 goto do_divi;
5094 case M_REMU_3I:
5095 s = "divu";
5096 s2 = "mfhi";
5097 goto do_divi;
5098 case M_DDIV_3I:
5099 dbl = 1;
5100 s = "ddiv";
5101 s2 = "mflo";
5102 goto do_divi;
5103 case M_DDIVU_3I:
5104 dbl = 1;
5105 s = "ddivu";
5106 s2 = "mflo";
5107 goto do_divi;
5108 case M_DREM_3I:
5109 dbl = 1;
5110 s = "ddiv";
5111 s2 = "mfhi";
5112 goto do_divi;
5113 case M_DREMU_3I:
5114 dbl = 1;
5115 s = "ddivu";
5116 s2 = "mfhi";
5117 do_divi:
5118 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5120 as_warn (_("Divide by zero."));
5121 if (mips_trap)
5122 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
5123 else
5124 macro_build (NULL, "break", "c", 7);
5125 break;
5127 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
5129 if (strcmp (s2, "mflo") == 0)
5130 move_register (dreg, sreg);
5131 else
5132 move_register (dreg, 0);
5133 break;
5135 if (imm_expr.X_op == O_constant
5136 && imm_expr.X_add_number == -1
5137 && s[strlen (s) - 1] != 'u')
5139 if (strcmp (s2, "mflo") == 0)
5141 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
5143 else
5144 move_register (dreg, 0);
5145 break;
5148 used_at = 1;
5149 load_register (AT, &imm_expr, dbl);
5150 macro_build (NULL, s, "z,s,t", sreg, AT);
5151 macro_build (NULL, s2, "d", dreg);
5152 break;
5154 case M_DIVU_3:
5155 s = "divu";
5156 s2 = "mflo";
5157 goto do_divu3;
5158 case M_REMU_3:
5159 s = "divu";
5160 s2 = "mfhi";
5161 goto do_divu3;
5162 case M_DDIVU_3:
5163 s = "ddivu";
5164 s2 = "mflo";
5165 goto do_divu3;
5166 case M_DREMU_3:
5167 s = "ddivu";
5168 s2 = "mfhi";
5169 do_divu3:
5170 start_noreorder ();
5171 if (mips_trap)
5173 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
5174 macro_build (NULL, s, "z,s,t", sreg, treg);
5175 /* We want to close the noreorder block as soon as possible, so
5176 that later insns are available for delay slot filling. */
5177 end_noreorder ();
5179 else
5181 expr1.X_add_number = 8;
5182 macro_build (&expr1, "bne", "s,t,p", treg, 0);
5183 macro_build (NULL, s, "z,s,t", sreg, treg);
5185 /* We want to close the noreorder block as soon as possible, so
5186 that later insns are available for delay slot filling. */
5187 end_noreorder ();
5188 macro_build (NULL, "break", "c", 7);
5190 macro_build (NULL, s2, "d", dreg);
5191 break;
5193 case M_DLCA_AB:
5194 dbl = 1;
5195 case M_LCA_AB:
5196 call = 1;
5197 goto do_la;
5198 case M_DLA_AB:
5199 dbl = 1;
5200 case M_LA_AB:
5201 do_la:
5202 /* Load the address of a symbol into a register. If breg is not
5203 zero, we then add a base register to it. */
5205 if (dbl && HAVE_32BIT_GPRS)
5206 as_warn (_("dla used to load 32-bit register"));
5208 if (! dbl && HAVE_64BIT_OBJECTS)
5209 as_warn (_("la used to load 64-bit address"));
5211 if (offset_expr.X_op == O_constant
5212 && offset_expr.X_add_number >= -0x8000
5213 && offset_expr.X_add_number < 0x8000)
5215 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
5216 "t,r,j", treg, sreg, BFD_RELOC_LO16);
5217 break;
5220 if (!mips_opts.noat && (treg == breg))
5222 tempreg = AT;
5223 used_at = 1;
5225 else
5227 tempreg = treg;
5230 if (offset_expr.X_op != O_symbol
5231 && offset_expr.X_op != O_constant)
5233 as_bad (_("expression too complex"));
5234 offset_expr.X_op = O_constant;
5237 if (offset_expr.X_op == O_constant)
5238 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
5239 else if (mips_pic == NO_PIC)
5241 /* If this is a reference to a GP relative symbol, we want
5242 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
5243 Otherwise we want
5244 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5245 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5246 If we have a constant, we need two instructions anyhow,
5247 so we may as well always use the latter form.
5249 With 64bit address space and a usable $at we want
5250 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5251 lui $at,<sym> (BFD_RELOC_HI16_S)
5252 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5253 daddiu $at,<sym> (BFD_RELOC_LO16)
5254 dsll32 $tempreg,0
5255 daddu $tempreg,$tempreg,$at
5257 If $at is already in use, we use a path which is suboptimal
5258 on superscalar processors.
5259 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5260 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5261 dsll $tempreg,16
5262 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5263 dsll $tempreg,16
5264 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
5266 For GP relative symbols in 64bit address space we can use
5267 the same sequence as in 32bit address space. */
5268 if (HAVE_64BIT_SYMBOLS)
5270 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5271 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5273 relax_start (offset_expr.X_add_symbol);
5274 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5275 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5276 relax_switch ();
5279 if (used_at == 0 && !mips_opts.noat)
5281 macro_build (&offset_expr, "lui", "t,u",
5282 tempreg, BFD_RELOC_MIPS_HIGHEST);
5283 macro_build (&offset_expr, "lui", "t,u",
5284 AT, BFD_RELOC_HI16_S);
5285 macro_build (&offset_expr, "daddiu", "t,r,j",
5286 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5287 macro_build (&offset_expr, "daddiu", "t,r,j",
5288 AT, AT, BFD_RELOC_LO16);
5289 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5290 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5291 used_at = 1;
5293 else
5295 macro_build (&offset_expr, "lui", "t,u",
5296 tempreg, BFD_RELOC_MIPS_HIGHEST);
5297 macro_build (&offset_expr, "daddiu", "t,r,j",
5298 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5299 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5300 macro_build (&offset_expr, "daddiu", "t,r,j",
5301 tempreg, tempreg, BFD_RELOC_HI16_S);
5302 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5303 macro_build (&offset_expr, "daddiu", "t,r,j",
5304 tempreg, tempreg, BFD_RELOC_LO16);
5307 if (mips_relax.sequence)
5308 relax_end ();
5310 else
5312 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5313 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5315 relax_start (offset_expr.X_add_symbol);
5316 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5317 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5318 relax_switch ();
5320 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5321 as_bad (_("offset too large"));
5322 macro_build_lui (&offset_expr, tempreg);
5323 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5324 tempreg, tempreg, BFD_RELOC_LO16);
5325 if (mips_relax.sequence)
5326 relax_end ();
5329 else if (!mips_big_got && !HAVE_NEWABI)
5331 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5333 /* If this is a reference to an external symbol, and there
5334 is no constant, we want
5335 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5336 or for lca or if tempreg is PIC_CALL_REG
5337 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5338 For a local symbol, we want
5339 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5341 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5343 If we have a small constant, and this is a reference to
5344 an external symbol, we want
5345 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5347 addiu $tempreg,$tempreg,<constant>
5348 For a local symbol, we want the same instruction
5349 sequence, but we output a BFD_RELOC_LO16 reloc on the
5350 addiu instruction.
5352 If we have a large constant, and this is a reference to
5353 an external symbol, we want
5354 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5355 lui $at,<hiconstant>
5356 addiu $at,$at,<loconstant>
5357 addu $tempreg,$tempreg,$at
5358 For a local symbol, we want the same instruction
5359 sequence, but we output a BFD_RELOC_LO16 reloc on the
5360 addiu instruction.
5363 if (offset_expr.X_add_number == 0)
5365 if (mips_pic == SVR4_PIC
5366 && breg == 0
5367 && (call || tempreg == PIC_CALL_REG))
5368 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5370 relax_start (offset_expr.X_add_symbol);
5371 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5372 lw_reloc_type, mips_gp_register);
5373 if (breg != 0)
5375 /* We're going to put in an addu instruction using
5376 tempreg, so we may as well insert the nop right
5377 now. */
5378 load_delay_nop ();
5380 relax_switch ();
5381 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5382 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
5383 load_delay_nop ();
5384 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5385 tempreg, tempreg, BFD_RELOC_LO16);
5386 relax_end ();
5387 /* FIXME: If breg == 0, and the next instruction uses
5388 $tempreg, then if this variant case is used an extra
5389 nop will be generated. */
5391 else if (offset_expr.X_add_number >= -0x8000
5392 && offset_expr.X_add_number < 0x8000)
5394 load_got_offset (tempreg, &offset_expr);
5395 load_delay_nop ();
5396 add_got_offset (tempreg, &offset_expr);
5398 else
5400 expr1.X_add_number = offset_expr.X_add_number;
5401 offset_expr.X_add_number =
5402 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
5403 load_got_offset (tempreg, &offset_expr);
5404 offset_expr.X_add_number = expr1.X_add_number;
5405 /* If we are going to add in a base register, and the
5406 target register and the base register are the same,
5407 then we are using AT as a temporary register. Since
5408 we want to load the constant into AT, we add our
5409 current AT (from the global offset table) and the
5410 register into the register now, and pretend we were
5411 not using a base register. */
5412 if (breg == treg)
5414 load_delay_nop ();
5415 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5416 treg, AT, breg);
5417 breg = 0;
5418 tempreg = treg;
5420 add_got_offset_hilo (tempreg, &offset_expr, AT);
5421 used_at = 1;
5424 else if (!mips_big_got && HAVE_NEWABI)
5426 int add_breg_early = 0;
5428 /* If this is a reference to an external, and there is no
5429 constant, or local symbol (*), with or without a
5430 constant, we want
5431 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5432 or for lca or if tempreg is PIC_CALL_REG
5433 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5435 If we have a small constant, and this is a reference to
5436 an external symbol, we want
5437 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5438 addiu $tempreg,$tempreg,<constant>
5440 If we have a large constant, and this is a reference to
5441 an external symbol, we want
5442 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5443 lui $at,<hiconstant>
5444 addiu $at,$at,<loconstant>
5445 addu $tempreg,$tempreg,$at
5447 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5448 local symbols, even though it introduces an additional
5449 instruction. */
5451 if (offset_expr.X_add_number)
5453 expr1.X_add_number = offset_expr.X_add_number;
5454 offset_expr.X_add_number = 0;
5456 relax_start (offset_expr.X_add_symbol);
5457 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5458 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5460 if (expr1.X_add_number >= -0x8000
5461 && expr1.X_add_number < 0x8000)
5463 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5464 tempreg, tempreg, BFD_RELOC_LO16);
5466 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5468 int dreg;
5470 /* If we are going to add in a base register, and the
5471 target register and the base register are the same,
5472 then we are using AT as a temporary register. Since
5473 we want to load the constant into AT, we add our
5474 current AT (from the global offset table) and the
5475 register into the register now, and pretend we were
5476 not using a base register. */
5477 if (breg != treg)
5478 dreg = tempreg;
5479 else
5481 assert (tempreg == AT);
5482 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5483 treg, AT, breg);
5484 dreg = treg;
5485 add_breg_early = 1;
5488 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5489 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5490 dreg, dreg, AT);
5492 used_at = 1;
5494 else
5495 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5497 relax_switch ();
5498 offset_expr.X_add_number = expr1.X_add_number;
5500 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5501 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5502 if (add_breg_early)
5504 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5505 treg, tempreg, breg);
5506 breg = 0;
5507 tempreg = treg;
5509 relax_end ();
5511 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5513 relax_start (offset_expr.X_add_symbol);
5514 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5515 BFD_RELOC_MIPS_CALL16, mips_gp_register);
5516 relax_switch ();
5517 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5518 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5519 relax_end ();
5521 else
5523 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5524 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5527 else if (mips_big_got && !HAVE_NEWABI)
5529 int gpdelay;
5530 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5531 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5532 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5534 /* This is the large GOT case. If this is a reference to an
5535 external symbol, and there is no constant, we want
5536 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5537 addu $tempreg,$tempreg,$gp
5538 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5539 or for lca or if tempreg is PIC_CALL_REG
5540 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5541 addu $tempreg,$tempreg,$gp
5542 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5543 For a local symbol, we want
5544 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5546 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5548 If we have a small constant, and this is a reference to
5549 an external symbol, we want
5550 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5551 addu $tempreg,$tempreg,$gp
5552 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5554 addiu $tempreg,$tempreg,<constant>
5555 For a local symbol, we want
5556 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5558 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5560 If we have a large constant, and this is a reference to
5561 an external symbol, we want
5562 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5563 addu $tempreg,$tempreg,$gp
5564 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5565 lui $at,<hiconstant>
5566 addiu $at,$at,<loconstant>
5567 addu $tempreg,$tempreg,$at
5568 For a local symbol, we want
5569 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5570 lui $at,<hiconstant>
5571 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5572 addu $tempreg,$tempreg,$at
5575 expr1.X_add_number = offset_expr.X_add_number;
5576 offset_expr.X_add_number = 0;
5577 relax_start (offset_expr.X_add_symbol);
5578 gpdelay = reg_needs_delay (mips_gp_register);
5579 if (expr1.X_add_number == 0 && breg == 0
5580 && (call || tempreg == PIC_CALL_REG))
5582 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5583 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5585 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5586 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5587 tempreg, tempreg, mips_gp_register);
5588 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5589 tempreg, lw_reloc_type, tempreg);
5590 if (expr1.X_add_number == 0)
5592 if (breg != 0)
5594 /* We're going to put in an addu instruction using
5595 tempreg, so we may as well insert the nop right
5596 now. */
5597 load_delay_nop ();
5600 else if (expr1.X_add_number >= -0x8000
5601 && expr1.X_add_number < 0x8000)
5603 load_delay_nop ();
5604 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5605 tempreg, tempreg, BFD_RELOC_LO16);
5607 else
5609 int dreg;
5611 /* If we are going to add in a base register, and the
5612 target register and the base register are the same,
5613 then we are using AT as a temporary register. Since
5614 we want to load the constant into AT, we add our
5615 current AT (from the global offset table) and the
5616 register into the register now, and pretend we were
5617 not using a base register. */
5618 if (breg != treg)
5619 dreg = tempreg;
5620 else
5622 assert (tempreg == AT);
5623 load_delay_nop ();
5624 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5625 treg, AT, breg);
5626 dreg = treg;
5629 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5630 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5632 used_at = 1;
5634 offset_expr.X_add_number =
5635 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5636 relax_switch ();
5638 if (gpdelay)
5640 /* This is needed because this instruction uses $gp, but
5641 the first instruction on the main stream does not. */
5642 macro_build (NULL, "nop", "");
5645 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5646 local_reloc_type, mips_gp_register);
5647 if (expr1.X_add_number >= -0x8000
5648 && expr1.X_add_number < 0x8000)
5650 load_delay_nop ();
5651 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5652 tempreg, tempreg, BFD_RELOC_LO16);
5653 /* FIXME: If add_number is 0, and there was no base
5654 register, the external symbol case ended with a load,
5655 so if the symbol turns out to not be external, and
5656 the next instruction uses tempreg, an unnecessary nop
5657 will be inserted. */
5659 else
5661 if (breg == treg)
5663 /* We must add in the base register now, as in the
5664 external symbol case. */
5665 assert (tempreg == AT);
5666 load_delay_nop ();
5667 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5668 treg, AT, breg);
5669 tempreg = treg;
5670 /* We set breg to 0 because we have arranged to add
5671 it in in both cases. */
5672 breg = 0;
5675 macro_build_lui (&expr1, AT);
5676 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5677 AT, AT, BFD_RELOC_LO16);
5678 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5679 tempreg, tempreg, AT);
5680 used_at = 1;
5682 relax_end ();
5684 else if (mips_big_got && HAVE_NEWABI)
5686 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5687 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5688 int add_breg_early = 0;
5690 /* This is the large GOT case. If this is a reference to an
5691 external symbol, and there is no constant, we want
5692 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5693 add $tempreg,$tempreg,$gp
5694 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5695 or for lca or if tempreg is PIC_CALL_REG
5696 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5697 add $tempreg,$tempreg,$gp
5698 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5700 If we have a small constant, and this is a reference to
5701 an external symbol, we want
5702 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5703 add $tempreg,$tempreg,$gp
5704 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5705 addi $tempreg,$tempreg,<constant>
5707 If we have a large constant, and this is a reference to
5708 an external symbol, we want
5709 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5710 addu $tempreg,$tempreg,$gp
5711 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5712 lui $at,<hiconstant>
5713 addi $at,$at,<loconstant>
5714 add $tempreg,$tempreg,$at
5716 If we have NewABI, and we know it's a local symbol, we want
5717 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5718 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5719 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5721 relax_start (offset_expr.X_add_symbol);
5723 expr1.X_add_number = offset_expr.X_add_number;
5724 offset_expr.X_add_number = 0;
5726 if (expr1.X_add_number == 0 && breg == 0
5727 && (call || tempreg == PIC_CALL_REG))
5729 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5730 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5732 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5733 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5734 tempreg, tempreg, mips_gp_register);
5735 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5736 tempreg, lw_reloc_type, tempreg);
5738 if (expr1.X_add_number == 0)
5740 else if (expr1.X_add_number >= -0x8000
5741 && expr1.X_add_number < 0x8000)
5743 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5744 tempreg, tempreg, BFD_RELOC_LO16);
5746 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5748 int dreg;
5750 /* If we are going to add in a base register, and the
5751 target register and the base register are the same,
5752 then we are using AT as a temporary register. Since
5753 we want to load the constant into AT, we add our
5754 current AT (from the global offset table) and the
5755 register into the register now, and pretend we were
5756 not using a base register. */
5757 if (breg != treg)
5758 dreg = tempreg;
5759 else
5761 assert (tempreg == AT);
5762 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5763 treg, AT, breg);
5764 dreg = treg;
5765 add_breg_early = 1;
5768 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5769 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5771 used_at = 1;
5773 else
5774 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5776 relax_switch ();
5777 offset_expr.X_add_number = expr1.X_add_number;
5778 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5779 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5780 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5781 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5782 if (add_breg_early)
5784 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5785 treg, tempreg, breg);
5786 breg = 0;
5787 tempreg = treg;
5789 relax_end ();
5791 else
5792 abort ();
5794 if (breg != 0)
5795 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
5796 break;
5798 case M_J_A:
5799 /* The j instruction may not be used in PIC code, since it
5800 requires an absolute address. We convert it to a b
5801 instruction. */
5802 if (mips_pic == NO_PIC)
5803 macro_build (&offset_expr, "j", "a");
5804 else
5805 macro_build (&offset_expr, "b", "p");
5806 break;
5808 /* The jal instructions must be handled as macros because when
5809 generating PIC code they expand to multi-instruction
5810 sequences. Normally they are simple instructions. */
5811 case M_JAL_1:
5812 dreg = RA;
5813 /* Fall through. */
5814 case M_JAL_2:
5815 if (mips_pic == NO_PIC)
5816 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5817 else
5819 if (sreg != PIC_CALL_REG)
5820 as_warn (_("MIPS PIC call to register other than $25"));
5822 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5823 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
5825 if (mips_cprestore_offset < 0)
5826 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5827 else
5829 if (! mips_frame_reg_valid)
5831 as_warn (_("No .frame pseudo-op used in PIC code"));
5832 /* Quiet this warning. */
5833 mips_frame_reg_valid = 1;
5835 if (! mips_cprestore_valid)
5837 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5838 /* Quiet this warning. */
5839 mips_cprestore_valid = 1;
5841 expr1.X_add_number = mips_cprestore_offset;
5842 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5843 mips_gp_register,
5844 mips_frame_reg,
5845 HAVE_64BIT_ADDRESSES);
5850 break;
5852 case M_JAL_A:
5853 if (mips_pic == NO_PIC)
5854 macro_build (&offset_expr, "jal", "a");
5855 else if (mips_pic == SVR4_PIC)
5857 /* If this is a reference to an external symbol, and we are
5858 using a small GOT, we want
5859 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5861 jalr $ra,$25
5863 lw $gp,cprestore($sp)
5864 The cprestore value is set using the .cprestore
5865 pseudo-op. If we are using a big GOT, we want
5866 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5867 addu $25,$25,$gp
5868 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5870 jalr $ra,$25
5872 lw $gp,cprestore($sp)
5873 If the symbol is not external, we want
5874 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5876 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5877 jalr $ra,$25
5879 lw $gp,cprestore($sp)
5881 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5882 sequences above, minus nops, unless the symbol is local,
5883 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5884 GOT_DISP. */
5885 if (HAVE_NEWABI)
5887 if (! mips_big_got)
5889 relax_start (offset_expr.X_add_symbol);
5890 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5891 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5892 mips_gp_register);
5893 relax_switch ();
5894 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5895 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
5896 mips_gp_register);
5897 relax_end ();
5899 else
5901 relax_start (offset_expr.X_add_symbol);
5902 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5903 BFD_RELOC_MIPS_CALL_HI16);
5904 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5905 PIC_CALL_REG, mips_gp_register);
5906 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5907 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5908 PIC_CALL_REG);
5909 relax_switch ();
5910 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5911 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5912 mips_gp_register);
5913 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5914 PIC_CALL_REG, PIC_CALL_REG,
5915 BFD_RELOC_MIPS_GOT_OFST);
5916 relax_end ();
5919 macro_build_jalr (&offset_expr);
5921 else
5923 relax_start (offset_expr.X_add_symbol);
5924 if (! mips_big_got)
5926 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5927 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5928 mips_gp_register);
5929 load_delay_nop ();
5930 relax_switch ();
5932 else
5934 int gpdelay;
5936 gpdelay = reg_needs_delay (mips_gp_register);
5937 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5938 BFD_RELOC_MIPS_CALL_HI16);
5939 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5940 PIC_CALL_REG, mips_gp_register);
5941 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5942 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5943 PIC_CALL_REG);
5944 load_delay_nop ();
5945 relax_switch ();
5946 if (gpdelay)
5947 macro_build (NULL, "nop", "");
5949 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5950 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5951 mips_gp_register);
5952 load_delay_nop ();
5953 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5954 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
5955 relax_end ();
5956 macro_build_jalr (&offset_expr);
5958 if (mips_cprestore_offset < 0)
5959 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5960 else
5962 if (! mips_frame_reg_valid)
5964 as_warn (_("No .frame pseudo-op used in PIC code"));
5965 /* Quiet this warning. */
5966 mips_frame_reg_valid = 1;
5968 if (! mips_cprestore_valid)
5970 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5971 /* Quiet this warning. */
5972 mips_cprestore_valid = 1;
5974 if (mips_opts.noreorder)
5975 macro_build (NULL, "nop", "");
5976 expr1.X_add_number = mips_cprestore_offset;
5977 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5978 mips_gp_register,
5979 mips_frame_reg,
5980 HAVE_64BIT_ADDRESSES);
5984 else if (mips_pic == VXWORKS_PIC)
5985 as_bad (_("Non-PIC jump used in PIC library"));
5986 else
5987 abort ();
5989 break;
5991 case M_LB_AB:
5992 s = "lb";
5993 goto ld;
5994 case M_LBU_AB:
5995 s = "lbu";
5996 goto ld;
5997 case M_LH_AB:
5998 s = "lh";
5999 goto ld;
6000 case M_LHU_AB:
6001 s = "lhu";
6002 goto ld;
6003 case M_LW_AB:
6004 s = "lw";
6005 goto ld;
6006 case M_LWC0_AB:
6007 s = "lwc0";
6008 /* Itbl support may require additional care here. */
6009 coproc = 1;
6010 goto ld;
6011 case M_LWC1_AB:
6012 s = "lwc1";
6013 /* Itbl support may require additional care here. */
6014 coproc = 1;
6015 goto ld;
6016 case M_LWC2_AB:
6017 s = "lwc2";
6018 /* Itbl support may require additional care here. */
6019 coproc = 1;
6020 goto ld;
6021 case M_LWC3_AB:
6022 s = "lwc3";
6023 /* Itbl support may require additional care here. */
6024 coproc = 1;
6025 goto ld;
6026 case M_LWL_AB:
6027 s = "lwl";
6028 lr = 1;
6029 goto ld;
6030 case M_LWR_AB:
6031 s = "lwr";
6032 lr = 1;
6033 goto ld;
6034 case M_LDC1_AB:
6035 if (mips_opts.arch == CPU_R4650)
6037 as_bad (_("opcode not supported on this processor"));
6038 break;
6040 s = "ldc1";
6041 /* Itbl support may require additional care here. */
6042 coproc = 1;
6043 goto ld;
6044 case M_LDC2_AB:
6045 s = "ldc2";
6046 /* Itbl support may require additional care here. */
6047 coproc = 1;
6048 goto ld;
6049 case M_LDC3_AB:
6050 s = "ldc3";
6051 /* Itbl support may require additional care here. */
6052 coproc = 1;
6053 goto ld;
6054 case M_LDL_AB:
6055 s = "ldl";
6056 lr = 1;
6057 goto ld;
6058 case M_LDR_AB:
6059 s = "ldr";
6060 lr = 1;
6061 goto ld;
6062 case M_LL_AB:
6063 s = "ll";
6064 goto ld;
6065 case M_LLD_AB:
6066 s = "lld";
6067 goto ld;
6068 case M_LWU_AB:
6069 s = "lwu";
6071 if (breg == treg || coproc || lr)
6073 tempreg = AT;
6074 used_at = 1;
6076 else
6078 tempreg = treg;
6080 goto ld_st;
6081 case M_SB_AB:
6082 s = "sb";
6083 goto st;
6084 case M_SH_AB:
6085 s = "sh";
6086 goto st;
6087 case M_SW_AB:
6088 s = "sw";
6089 goto st;
6090 case M_SWC0_AB:
6091 s = "swc0";
6092 /* Itbl support may require additional care here. */
6093 coproc = 1;
6094 goto st;
6095 case M_SWC1_AB:
6096 s = "swc1";
6097 /* Itbl support may require additional care here. */
6098 coproc = 1;
6099 goto st;
6100 case M_SWC2_AB:
6101 s = "swc2";
6102 /* Itbl support may require additional care here. */
6103 coproc = 1;
6104 goto st;
6105 case M_SWC3_AB:
6106 s = "swc3";
6107 /* Itbl support may require additional care here. */
6108 coproc = 1;
6109 goto st;
6110 case M_SWL_AB:
6111 s = "swl";
6112 goto st;
6113 case M_SWR_AB:
6114 s = "swr";
6115 goto st;
6116 case M_SC_AB:
6117 s = "sc";
6118 goto st;
6119 case M_SCD_AB:
6120 s = "scd";
6121 goto st;
6122 case M_CACHE_AB:
6123 s = "cache";
6124 goto st;
6125 case M_SDC1_AB:
6126 if (mips_opts.arch == CPU_R4650)
6128 as_bad (_("opcode not supported on this processor"));
6129 break;
6131 s = "sdc1";
6132 coproc = 1;
6133 /* Itbl support may require additional care here. */
6134 goto st;
6135 case M_SDC2_AB:
6136 s = "sdc2";
6137 /* Itbl support may require additional care here. */
6138 coproc = 1;
6139 goto st;
6140 case M_SDC3_AB:
6141 s = "sdc3";
6142 /* Itbl support may require additional care here. */
6143 coproc = 1;
6144 goto st;
6145 case M_SDL_AB:
6146 s = "sdl";
6147 goto st;
6148 case M_SDR_AB:
6149 s = "sdr";
6151 tempreg = AT;
6152 used_at = 1;
6153 ld_st:
6154 /* Itbl support may require additional care here. */
6155 if (mask == M_LWC1_AB
6156 || mask == M_SWC1_AB
6157 || mask == M_LDC1_AB
6158 || mask == M_SDC1_AB
6159 || mask == M_L_DAB
6160 || mask == M_S_DAB)
6161 fmt = "T,o(b)";
6162 else if (mask == M_CACHE_AB)
6163 fmt = "k,o(b)";
6164 else if (coproc)
6165 fmt = "E,o(b)";
6166 else
6167 fmt = "t,o(b)";
6169 if (offset_expr.X_op != O_constant
6170 && offset_expr.X_op != O_symbol)
6172 as_bad (_("expression too complex"));
6173 offset_expr.X_op = O_constant;
6176 if (HAVE_32BIT_ADDRESSES
6177 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6179 char value [32];
6181 sprintf_vma (value, offset_expr.X_add_number);
6182 as_bad (_("Number (0x%s) larger than 32 bits"), value);
6185 /* A constant expression in PIC code can be handled just as it
6186 is in non PIC code. */
6187 if (offset_expr.X_op == O_constant)
6189 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
6190 & ~(bfd_vma) 0xffff);
6191 normalize_address_expr (&expr1);
6192 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
6193 if (breg != 0)
6194 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6195 tempreg, tempreg, breg);
6196 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6198 else if (mips_pic == NO_PIC)
6200 /* If this is a reference to a GP relative symbol, and there
6201 is no base register, we want
6202 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6203 Otherwise, if there is no base register, we want
6204 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6205 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6206 If we have a constant, we need two instructions anyhow,
6207 so we always use the latter form.
6209 If we have a base register, and this is a reference to a
6210 GP relative symbol, we want
6211 addu $tempreg,$breg,$gp
6212 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
6213 Otherwise we want
6214 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6215 addu $tempreg,$tempreg,$breg
6216 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6217 With a constant we always use the latter case.
6219 With 64bit address space and no base register and $at usable,
6220 we want
6221 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6222 lui $at,<sym> (BFD_RELOC_HI16_S)
6223 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6224 dsll32 $tempreg,0
6225 daddu $tempreg,$at
6226 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6227 If we have a base register, we want
6228 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6229 lui $at,<sym> (BFD_RELOC_HI16_S)
6230 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6231 daddu $at,$breg
6232 dsll32 $tempreg,0
6233 daddu $tempreg,$at
6234 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6236 Without $at we can't generate the optimal path for superscalar
6237 processors here since this would require two temporary registers.
6238 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6239 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6240 dsll $tempreg,16
6241 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6242 dsll $tempreg,16
6243 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6244 If we have a base register, we want
6245 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6246 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6247 dsll $tempreg,16
6248 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6249 dsll $tempreg,16
6250 daddu $tempreg,$tempreg,$breg
6251 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6253 For GP relative symbols in 64bit address space we can use
6254 the same sequence as in 32bit address space. */
6255 if (HAVE_64BIT_SYMBOLS)
6257 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6258 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6260 relax_start (offset_expr.X_add_symbol);
6261 if (breg == 0)
6263 macro_build (&offset_expr, s, fmt, treg,
6264 BFD_RELOC_GPREL16, mips_gp_register);
6266 else
6268 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6269 tempreg, breg, mips_gp_register);
6270 macro_build (&offset_expr, s, fmt, treg,
6271 BFD_RELOC_GPREL16, tempreg);
6273 relax_switch ();
6276 if (used_at == 0 && !mips_opts.noat)
6278 macro_build (&offset_expr, "lui", "t,u", tempreg,
6279 BFD_RELOC_MIPS_HIGHEST);
6280 macro_build (&offset_expr, "lui", "t,u", AT,
6281 BFD_RELOC_HI16_S);
6282 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6283 tempreg, BFD_RELOC_MIPS_HIGHER);
6284 if (breg != 0)
6285 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
6286 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
6287 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
6288 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
6289 tempreg);
6290 used_at = 1;
6292 else
6294 macro_build (&offset_expr, "lui", "t,u", tempreg,
6295 BFD_RELOC_MIPS_HIGHEST);
6296 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6297 tempreg, BFD_RELOC_MIPS_HIGHER);
6298 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6299 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6300 tempreg, BFD_RELOC_HI16_S);
6301 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6302 if (breg != 0)
6303 macro_build (NULL, "daddu", "d,v,t",
6304 tempreg, tempreg, breg);
6305 macro_build (&offset_expr, s, fmt, treg,
6306 BFD_RELOC_LO16, tempreg);
6309 if (mips_relax.sequence)
6310 relax_end ();
6311 break;
6314 if (breg == 0)
6316 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6317 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6319 relax_start (offset_expr.X_add_symbol);
6320 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6321 mips_gp_register);
6322 relax_switch ();
6324 macro_build_lui (&offset_expr, tempreg);
6325 macro_build (&offset_expr, s, fmt, treg,
6326 BFD_RELOC_LO16, tempreg);
6327 if (mips_relax.sequence)
6328 relax_end ();
6330 else
6332 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6333 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6335 relax_start (offset_expr.X_add_symbol);
6336 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6337 tempreg, breg, mips_gp_register);
6338 macro_build (&offset_expr, s, fmt, treg,
6339 BFD_RELOC_GPREL16, tempreg);
6340 relax_switch ();
6342 macro_build_lui (&offset_expr, tempreg);
6343 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6344 tempreg, tempreg, breg);
6345 macro_build (&offset_expr, s, fmt, treg,
6346 BFD_RELOC_LO16, tempreg);
6347 if (mips_relax.sequence)
6348 relax_end ();
6351 else if (!mips_big_got)
6353 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6355 /* If this is a reference to an external symbol, we want
6356 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6358 <op> $treg,0($tempreg)
6359 Otherwise we want
6360 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6362 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6363 <op> $treg,0($tempreg)
6365 For NewABI, we want
6366 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6367 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6369 If there is a base register, we add it to $tempreg before
6370 the <op>. If there is a constant, we stick it in the
6371 <op> instruction. We don't handle constants larger than
6372 16 bits, because we have no way to load the upper 16 bits
6373 (actually, we could handle them for the subset of cases
6374 in which we are not using $at). */
6375 assert (offset_expr.X_op == O_symbol);
6376 if (HAVE_NEWABI)
6378 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6379 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6380 if (breg != 0)
6381 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6382 tempreg, tempreg, breg);
6383 macro_build (&offset_expr, s, fmt, treg,
6384 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6385 break;
6387 expr1.X_add_number = offset_expr.X_add_number;
6388 offset_expr.X_add_number = 0;
6389 if (expr1.X_add_number < -0x8000
6390 || expr1.X_add_number >= 0x8000)
6391 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6392 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6393 lw_reloc_type, mips_gp_register);
6394 load_delay_nop ();
6395 relax_start (offset_expr.X_add_symbol);
6396 relax_switch ();
6397 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6398 tempreg, BFD_RELOC_LO16);
6399 relax_end ();
6400 if (breg != 0)
6401 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6402 tempreg, tempreg, breg);
6403 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6405 else if (mips_big_got && !HAVE_NEWABI)
6407 int gpdelay;
6409 /* If this is a reference to an external symbol, we want
6410 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6411 addu $tempreg,$tempreg,$gp
6412 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6413 <op> $treg,0($tempreg)
6414 Otherwise we want
6415 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6417 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6418 <op> $treg,0($tempreg)
6419 If there is a base register, we add it to $tempreg before
6420 the <op>. If there is a constant, we stick it in the
6421 <op> instruction. We don't handle constants larger than
6422 16 bits, because we have no way to load the upper 16 bits
6423 (actually, we could handle them for the subset of cases
6424 in which we are not using $at). */
6425 assert (offset_expr.X_op == O_symbol);
6426 expr1.X_add_number = offset_expr.X_add_number;
6427 offset_expr.X_add_number = 0;
6428 if (expr1.X_add_number < -0x8000
6429 || expr1.X_add_number >= 0x8000)
6430 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6431 gpdelay = reg_needs_delay (mips_gp_register);
6432 relax_start (offset_expr.X_add_symbol);
6433 macro_build (&offset_expr, "lui", "t,u", tempreg,
6434 BFD_RELOC_MIPS_GOT_HI16);
6435 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6436 mips_gp_register);
6437 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6438 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6439 relax_switch ();
6440 if (gpdelay)
6441 macro_build (NULL, "nop", "");
6442 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6443 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6444 load_delay_nop ();
6445 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6446 tempreg, BFD_RELOC_LO16);
6447 relax_end ();
6449 if (breg != 0)
6450 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6451 tempreg, tempreg, breg);
6452 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6454 else if (mips_big_got && HAVE_NEWABI)
6456 /* If this is a reference to an external symbol, we want
6457 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6458 add $tempreg,$tempreg,$gp
6459 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6460 <op> $treg,<ofst>($tempreg)
6461 Otherwise, for local symbols, we want:
6462 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6463 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6464 assert (offset_expr.X_op == O_symbol);
6465 expr1.X_add_number = offset_expr.X_add_number;
6466 offset_expr.X_add_number = 0;
6467 if (expr1.X_add_number < -0x8000
6468 || expr1.X_add_number >= 0x8000)
6469 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6470 relax_start (offset_expr.X_add_symbol);
6471 macro_build (&offset_expr, "lui", "t,u", tempreg,
6472 BFD_RELOC_MIPS_GOT_HI16);
6473 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6474 mips_gp_register);
6475 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6476 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6477 if (breg != 0)
6478 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6479 tempreg, tempreg, breg);
6480 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6482 relax_switch ();
6483 offset_expr.X_add_number = expr1.X_add_number;
6484 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6485 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6486 if (breg != 0)
6487 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6488 tempreg, tempreg, breg);
6489 macro_build (&offset_expr, s, fmt, treg,
6490 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6491 relax_end ();
6493 else
6494 abort ();
6496 break;
6498 case M_LI:
6499 case M_LI_S:
6500 load_register (treg, &imm_expr, 0);
6501 break;
6503 case M_DLI:
6504 load_register (treg, &imm_expr, 1);
6505 break;
6507 case M_LI_SS:
6508 if (imm_expr.X_op == O_constant)
6510 used_at = 1;
6511 load_register (AT, &imm_expr, 0);
6512 macro_build (NULL, "mtc1", "t,G", AT, treg);
6513 break;
6515 else
6517 assert (offset_expr.X_op == O_symbol
6518 && strcmp (segment_name (S_GET_SEGMENT
6519 (offset_expr.X_add_symbol)),
6520 ".lit4") == 0
6521 && offset_expr.X_add_number == 0);
6522 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
6523 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6524 break;
6527 case M_LI_D:
6528 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6529 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6530 order 32 bits of the value and the low order 32 bits are either
6531 zero or in OFFSET_EXPR. */
6532 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6534 if (HAVE_64BIT_GPRS)
6535 load_register (treg, &imm_expr, 1);
6536 else
6538 int hreg, lreg;
6540 if (target_big_endian)
6542 hreg = treg;
6543 lreg = treg + 1;
6545 else
6547 hreg = treg + 1;
6548 lreg = treg;
6551 if (hreg <= 31)
6552 load_register (hreg, &imm_expr, 0);
6553 if (lreg <= 31)
6555 if (offset_expr.X_op == O_absent)
6556 move_register (lreg, 0);
6557 else
6559 assert (offset_expr.X_op == O_constant);
6560 load_register (lreg, &offset_expr, 0);
6564 break;
6567 /* We know that sym is in the .rdata section. First we get the
6568 upper 16 bits of the address. */
6569 if (mips_pic == NO_PIC)
6571 macro_build_lui (&offset_expr, AT);
6572 used_at = 1;
6574 else
6576 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6577 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6578 used_at = 1;
6581 /* Now we load the register(s). */
6582 if (HAVE_64BIT_GPRS)
6584 used_at = 1;
6585 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6587 else
6589 used_at = 1;
6590 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6591 if (treg != RA)
6593 /* FIXME: How in the world do we deal with the possible
6594 overflow here? */
6595 offset_expr.X_add_number += 4;
6596 macro_build (&offset_expr, "lw", "t,o(b)",
6597 treg + 1, BFD_RELOC_LO16, AT);
6600 break;
6602 case M_LI_DD:
6603 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6604 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6605 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6606 the value and the low order 32 bits are either zero or in
6607 OFFSET_EXPR. */
6608 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6610 used_at = 1;
6611 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
6612 if (HAVE_64BIT_FPRS)
6614 assert (HAVE_64BIT_GPRS);
6615 macro_build (NULL, "dmtc1", "t,S", AT, treg);
6617 else
6619 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
6620 if (offset_expr.X_op == O_absent)
6621 macro_build (NULL, "mtc1", "t,G", 0, treg);
6622 else
6624 assert (offset_expr.X_op == O_constant);
6625 load_register (AT, &offset_expr, 0);
6626 macro_build (NULL, "mtc1", "t,G", AT, treg);
6629 break;
6632 assert (offset_expr.X_op == O_symbol
6633 && offset_expr.X_add_number == 0);
6634 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6635 if (strcmp (s, ".lit8") == 0)
6637 if (mips_opts.isa != ISA_MIPS1)
6639 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
6640 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6641 break;
6643 breg = mips_gp_register;
6644 r = BFD_RELOC_MIPS_LITERAL;
6645 goto dob;
6647 else
6649 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6650 used_at = 1;
6651 if (mips_pic != NO_PIC)
6652 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6653 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6654 else
6656 /* FIXME: This won't work for a 64 bit address. */
6657 macro_build_lui (&offset_expr, AT);
6660 if (mips_opts.isa != ISA_MIPS1)
6662 macro_build (&offset_expr, "ldc1", "T,o(b)",
6663 treg, BFD_RELOC_LO16, AT);
6664 break;
6666 breg = AT;
6667 r = BFD_RELOC_LO16;
6668 goto dob;
6671 case M_L_DOB:
6672 if (mips_opts.arch == CPU_R4650)
6674 as_bad (_("opcode not supported on this processor"));
6675 break;
6677 /* Even on a big endian machine $fn comes before $fn+1. We have
6678 to adjust when loading from memory. */
6679 r = BFD_RELOC_LO16;
6680 dob:
6681 assert (mips_opts.isa == ISA_MIPS1);
6682 macro_build (&offset_expr, "lwc1", "T,o(b)",
6683 target_big_endian ? treg + 1 : treg, r, breg);
6684 /* FIXME: A possible overflow which I don't know how to deal
6685 with. */
6686 offset_expr.X_add_number += 4;
6687 macro_build (&offset_expr, "lwc1", "T,o(b)",
6688 target_big_endian ? treg : treg + 1, r, breg);
6689 break;
6691 case M_L_DAB:
6693 * The MIPS assembler seems to check for X_add_number not
6694 * being double aligned and generating:
6695 * lui at,%hi(foo+1)
6696 * addu at,at,v1
6697 * addiu at,at,%lo(foo+1)
6698 * lwc1 f2,0(at)
6699 * lwc1 f3,4(at)
6700 * But, the resulting address is the same after relocation so why
6701 * generate the extra instruction?
6703 if (mips_opts.arch == CPU_R4650)
6705 as_bad (_("opcode not supported on this processor"));
6706 break;
6708 /* Itbl support may require additional care here. */
6709 coproc = 1;
6710 if (mips_opts.isa != ISA_MIPS1)
6712 s = "ldc1";
6713 goto ld;
6716 s = "lwc1";
6717 fmt = "T,o(b)";
6718 goto ldd_std;
6720 case M_S_DAB:
6721 if (mips_opts.arch == CPU_R4650)
6723 as_bad (_("opcode not supported on this processor"));
6724 break;
6727 if (mips_opts.isa != ISA_MIPS1)
6729 s = "sdc1";
6730 goto st;
6733 s = "swc1";
6734 fmt = "T,o(b)";
6735 /* Itbl support may require additional care here. */
6736 coproc = 1;
6737 goto ldd_std;
6739 case M_LD_AB:
6740 if (HAVE_64BIT_GPRS)
6742 s = "ld";
6743 goto ld;
6746 s = "lw";
6747 fmt = "t,o(b)";
6748 goto ldd_std;
6750 case M_SD_AB:
6751 if (HAVE_64BIT_GPRS)
6753 s = "sd";
6754 goto st;
6757 s = "sw";
6758 fmt = "t,o(b)";
6760 ldd_std:
6761 if (offset_expr.X_op != O_symbol
6762 && offset_expr.X_op != O_constant)
6764 as_bad (_("expression too complex"));
6765 offset_expr.X_op = O_constant;
6768 if (HAVE_32BIT_ADDRESSES
6769 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6771 char value [32];
6773 sprintf_vma (value, offset_expr.X_add_number);
6774 as_bad (_("Number (0x%s) larger than 32 bits"), value);
6777 /* Even on a big endian machine $fn comes before $fn+1. We have
6778 to adjust when loading from memory. We set coproc if we must
6779 load $fn+1 first. */
6780 /* Itbl support may require additional care here. */
6781 if (! target_big_endian)
6782 coproc = 0;
6784 if (mips_pic == NO_PIC
6785 || offset_expr.X_op == O_constant)
6787 /* If this is a reference to a GP relative symbol, we want
6788 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6789 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6790 If we have a base register, we use this
6791 addu $at,$breg,$gp
6792 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6793 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6794 If this is not a GP relative symbol, we want
6795 lui $at,<sym> (BFD_RELOC_HI16_S)
6796 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6797 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6798 If there is a base register, we add it to $at after the
6799 lui instruction. If there is a constant, we always use
6800 the last case. */
6801 if (offset_expr.X_op == O_symbol
6802 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6803 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6805 relax_start (offset_expr.X_add_symbol);
6806 if (breg == 0)
6808 tempreg = mips_gp_register;
6810 else
6812 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6813 AT, breg, mips_gp_register);
6814 tempreg = AT;
6815 used_at = 1;
6818 /* Itbl support may require additional care here. */
6819 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6820 BFD_RELOC_GPREL16, tempreg);
6821 offset_expr.X_add_number += 4;
6823 /* Set mips_optimize to 2 to avoid inserting an
6824 undesired nop. */
6825 hold_mips_optimize = mips_optimize;
6826 mips_optimize = 2;
6827 /* Itbl support may require additional care here. */
6828 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6829 BFD_RELOC_GPREL16, tempreg);
6830 mips_optimize = hold_mips_optimize;
6832 relax_switch ();
6834 /* We just generated two relocs. When tc_gen_reloc
6835 handles this case, it will skip the first reloc and
6836 handle the second. The second reloc already has an
6837 extra addend of 4, which we added above. We must
6838 subtract it out, and then subtract another 4 to make
6839 the first reloc come out right. The second reloc
6840 will come out right because we are going to add 4 to
6841 offset_expr when we build its instruction below.
6843 If we have a symbol, then we don't want to include
6844 the offset, because it will wind up being included
6845 when we generate the reloc. */
6847 if (offset_expr.X_op == O_constant)
6848 offset_expr.X_add_number -= 8;
6849 else
6851 offset_expr.X_add_number = -4;
6852 offset_expr.X_op = O_constant;
6855 used_at = 1;
6856 macro_build_lui (&offset_expr, AT);
6857 if (breg != 0)
6858 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6859 /* Itbl support may require additional care here. */
6860 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6861 BFD_RELOC_LO16, AT);
6862 /* FIXME: How do we handle overflow here? */
6863 offset_expr.X_add_number += 4;
6864 /* Itbl support may require additional care here. */
6865 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6866 BFD_RELOC_LO16, AT);
6867 if (mips_relax.sequence)
6868 relax_end ();
6870 else if (!mips_big_got)
6872 /* If this is a reference to an external symbol, we want
6873 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6875 <op> $treg,0($at)
6876 <op> $treg+1,4($at)
6877 Otherwise we want
6878 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6880 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6881 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6882 If there is a base register we add it to $at before the
6883 lwc1 instructions. If there is a constant we include it
6884 in the lwc1 instructions. */
6885 used_at = 1;
6886 expr1.X_add_number = offset_expr.X_add_number;
6887 if (expr1.X_add_number < -0x8000
6888 || expr1.X_add_number >= 0x8000 - 4)
6889 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6890 load_got_offset (AT, &offset_expr);
6891 load_delay_nop ();
6892 if (breg != 0)
6893 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6895 /* Set mips_optimize to 2 to avoid inserting an undesired
6896 nop. */
6897 hold_mips_optimize = mips_optimize;
6898 mips_optimize = 2;
6900 /* Itbl support may require additional care here. */
6901 relax_start (offset_expr.X_add_symbol);
6902 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6903 BFD_RELOC_LO16, AT);
6904 expr1.X_add_number += 4;
6905 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6906 BFD_RELOC_LO16, AT);
6907 relax_switch ();
6908 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6909 BFD_RELOC_LO16, AT);
6910 offset_expr.X_add_number += 4;
6911 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6912 BFD_RELOC_LO16, AT);
6913 relax_end ();
6915 mips_optimize = hold_mips_optimize;
6917 else if (mips_big_got)
6919 int gpdelay;
6921 /* If this is a reference to an external symbol, we want
6922 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6923 addu $at,$at,$gp
6924 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6926 <op> $treg,0($at)
6927 <op> $treg+1,4($at)
6928 Otherwise we want
6929 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6931 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6932 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6933 If there is a base register we add it to $at before the
6934 lwc1 instructions. If there is a constant we include it
6935 in the lwc1 instructions. */
6936 used_at = 1;
6937 expr1.X_add_number = offset_expr.X_add_number;
6938 offset_expr.X_add_number = 0;
6939 if (expr1.X_add_number < -0x8000
6940 || expr1.X_add_number >= 0x8000 - 4)
6941 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6942 gpdelay = reg_needs_delay (mips_gp_register);
6943 relax_start (offset_expr.X_add_symbol);
6944 macro_build (&offset_expr, "lui", "t,u",
6945 AT, BFD_RELOC_MIPS_GOT_HI16);
6946 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6947 AT, AT, mips_gp_register);
6948 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6949 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6950 load_delay_nop ();
6951 if (breg != 0)
6952 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6953 /* Itbl support may require additional care here. */
6954 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6955 BFD_RELOC_LO16, AT);
6956 expr1.X_add_number += 4;
6958 /* Set mips_optimize to 2 to avoid inserting an undesired
6959 nop. */
6960 hold_mips_optimize = mips_optimize;
6961 mips_optimize = 2;
6962 /* Itbl support may require additional care here. */
6963 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6964 BFD_RELOC_LO16, AT);
6965 mips_optimize = hold_mips_optimize;
6966 expr1.X_add_number -= 4;
6968 relax_switch ();
6969 offset_expr.X_add_number = expr1.X_add_number;
6970 if (gpdelay)
6971 macro_build (NULL, "nop", "");
6972 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6973 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6974 load_delay_nop ();
6975 if (breg != 0)
6976 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6977 /* Itbl support may require additional care here. */
6978 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6979 BFD_RELOC_LO16, AT);
6980 offset_expr.X_add_number += 4;
6982 /* Set mips_optimize to 2 to avoid inserting an undesired
6983 nop. */
6984 hold_mips_optimize = mips_optimize;
6985 mips_optimize = 2;
6986 /* Itbl support may require additional care here. */
6987 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6988 BFD_RELOC_LO16, AT);
6989 mips_optimize = hold_mips_optimize;
6990 relax_end ();
6992 else
6993 abort ();
6995 break;
6997 case M_LD_OB:
6998 s = "lw";
6999 goto sd_ob;
7000 case M_SD_OB:
7001 s = "sw";
7002 sd_ob:
7003 assert (HAVE_32BIT_ADDRESSES);
7004 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7005 offset_expr.X_add_number += 4;
7006 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
7007 break;
7009 /* New code added to support COPZ instructions.
7010 This code builds table entries out of the macros in mip_opcodes.
7011 R4000 uses interlocks to handle coproc delays.
7012 Other chips (like the R3000) require nops to be inserted for delays.
7014 FIXME: Currently, we require that the user handle delays.
7015 In order to fill delay slots for non-interlocked chips,
7016 we must have a way to specify delays based on the coprocessor.
7017 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7018 What are the side-effects of the cop instruction?
7019 What cache support might we have and what are its effects?
7020 Both coprocessor & memory require delays. how long???
7021 What registers are read/set/modified?
7023 If an itbl is provided to interpret cop instructions,
7024 this knowledge can be encoded in the itbl spec. */
7026 case M_COP0:
7027 s = "c0";
7028 goto copz;
7029 case M_COP1:
7030 s = "c1";
7031 goto copz;
7032 case M_COP2:
7033 s = "c2";
7034 goto copz;
7035 case M_COP3:
7036 s = "c3";
7037 copz:
7038 /* For now we just do C (same as Cz). The parameter will be
7039 stored in insn_opcode by mips_ip. */
7040 macro_build (NULL, s, "C", ip->insn_opcode);
7041 break;
7043 case M_MOVE:
7044 move_register (dreg, sreg);
7045 break;
7047 #ifdef LOSING_COMPILER
7048 default:
7049 /* Try and see if this is a new itbl instruction.
7050 This code builds table entries out of the macros in mip_opcodes.
7051 FIXME: For now we just assemble the expression and pass it's
7052 value along as a 32-bit immediate.
7053 We may want to have the assembler assemble this value,
7054 so that we gain the assembler's knowledge of delay slots,
7055 symbols, etc.
7056 Would it be more efficient to use mask (id) here? */
7057 if (itbl_have_entries
7058 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
7060 s = ip->insn_mo->name;
7061 s2 = "cop3";
7062 coproc = ITBL_DECODE_PNUM (immed_expr);;
7063 macro_build (&immed_expr, s, "C");
7064 break;
7066 macro2 (ip);
7067 break;
7069 if (mips_opts.noat && used_at)
7070 as_bad (_("Macro used $at after \".set noat\""));
7073 static void
7074 macro2 (struct mips_cl_insn *ip)
7076 register int treg, sreg, dreg, breg;
7077 int tempreg;
7078 int mask;
7079 int used_at;
7080 expressionS expr1;
7081 const char *s;
7082 const char *s2;
7083 const char *fmt;
7084 int likely = 0;
7085 int dbl = 0;
7086 int coproc = 0;
7087 int lr = 0;
7088 int imm = 0;
7089 int off;
7090 offsetT maxnum;
7091 bfd_reloc_code_real_type r;
7093 treg = (ip->insn_opcode >> 16) & 0x1f;
7094 dreg = (ip->insn_opcode >> 11) & 0x1f;
7095 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7096 mask = ip->insn_mo->mask;
7098 expr1.X_op = O_constant;
7099 expr1.X_op_symbol = NULL;
7100 expr1.X_add_symbol = NULL;
7101 expr1.X_add_number = 1;
7103 switch (mask)
7105 #endif /* LOSING_COMPILER */
7107 case M_DMUL:
7108 dbl = 1;
7109 case M_MUL:
7110 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
7111 macro_build (NULL, "mflo", "d", dreg);
7112 break;
7114 case M_DMUL_I:
7115 dbl = 1;
7116 case M_MUL_I:
7117 /* The MIPS assembler some times generates shifts and adds. I'm
7118 not trying to be that fancy. GCC should do this for us
7119 anyway. */
7120 used_at = 1;
7121 load_register (AT, &imm_expr, dbl);
7122 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
7123 macro_build (NULL, "mflo", "d", dreg);
7124 break;
7126 case M_DMULO_I:
7127 dbl = 1;
7128 case M_MULO_I:
7129 imm = 1;
7130 goto do_mulo;
7132 case M_DMULO:
7133 dbl = 1;
7134 case M_MULO:
7135 do_mulo:
7136 start_noreorder ();
7137 used_at = 1;
7138 if (imm)
7139 load_register (AT, &imm_expr, dbl);
7140 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
7141 macro_build (NULL, "mflo", "d", dreg);
7142 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
7143 macro_build (NULL, "mfhi", "d", AT);
7144 if (mips_trap)
7145 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
7146 else
7148 expr1.X_add_number = 8;
7149 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
7150 macro_build (NULL, "nop", "", 0);
7151 macro_build (NULL, "break", "c", 6);
7153 end_noreorder ();
7154 macro_build (NULL, "mflo", "d", dreg);
7155 break;
7157 case M_DMULOU_I:
7158 dbl = 1;
7159 case M_MULOU_I:
7160 imm = 1;
7161 goto do_mulou;
7163 case M_DMULOU:
7164 dbl = 1;
7165 case M_MULOU:
7166 do_mulou:
7167 start_noreorder ();
7168 used_at = 1;
7169 if (imm)
7170 load_register (AT, &imm_expr, dbl);
7171 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
7172 sreg, imm ? AT : treg);
7173 macro_build (NULL, "mfhi", "d", AT);
7174 macro_build (NULL, "mflo", "d", dreg);
7175 if (mips_trap)
7176 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
7177 else
7179 expr1.X_add_number = 8;
7180 macro_build (&expr1, "beq", "s,t,p", AT, 0);
7181 macro_build (NULL, "nop", "", 0);
7182 macro_build (NULL, "break", "c", 6);
7184 end_noreorder ();
7185 break;
7187 case M_DROL:
7188 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7190 if (dreg == sreg)
7192 tempreg = AT;
7193 used_at = 1;
7195 else
7197 tempreg = dreg;
7199 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
7200 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
7201 break;
7203 used_at = 1;
7204 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7205 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7206 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7207 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7208 break;
7210 case M_ROL:
7211 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7213 if (dreg == sreg)
7215 tempreg = AT;
7216 used_at = 1;
7218 else
7220 tempreg = dreg;
7222 macro_build (NULL, "negu", "d,w", tempreg, treg);
7223 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
7224 break;
7226 used_at = 1;
7227 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7228 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
7229 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
7230 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7231 break;
7233 case M_DROL_I:
7235 unsigned int rot;
7236 char *l, *r;
7238 if (imm_expr.X_op != O_constant)
7239 as_bad (_("Improper rotate count"));
7240 rot = imm_expr.X_add_number & 0x3f;
7241 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7243 rot = (64 - rot) & 0x3f;
7244 if (rot >= 32)
7245 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
7246 else
7247 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
7248 break;
7250 if (rot == 0)
7252 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7253 break;
7255 l = (rot < 0x20) ? "dsll" : "dsll32";
7256 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7257 rot &= 0x1f;
7258 used_at = 1;
7259 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
7260 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7261 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7263 break;
7265 case M_ROL_I:
7267 unsigned int rot;
7269 if (imm_expr.X_op != O_constant)
7270 as_bad (_("Improper rotate count"));
7271 rot = imm_expr.X_add_number & 0x1f;
7272 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7274 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7275 break;
7277 if (rot == 0)
7279 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7280 break;
7282 used_at = 1;
7283 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
7284 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7285 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7287 break;
7289 case M_DROR:
7290 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7292 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
7293 break;
7295 used_at = 1;
7296 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7297 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
7298 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7299 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7300 break;
7302 case M_ROR:
7303 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7305 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
7306 break;
7308 used_at = 1;
7309 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7310 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7311 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7312 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7313 break;
7315 case M_DROR_I:
7317 unsigned int rot;
7318 char *l, *r;
7320 if (imm_expr.X_op != O_constant)
7321 as_bad (_("Improper rotate count"));
7322 rot = imm_expr.X_add_number & 0x3f;
7323 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7325 if (rot >= 32)
7326 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
7327 else
7328 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
7329 break;
7331 if (rot == 0)
7333 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7334 break;
7336 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7337 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7338 rot &= 0x1f;
7339 used_at = 1;
7340 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7341 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7342 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7344 break;
7346 case M_ROR_I:
7348 unsigned int rot;
7350 if (imm_expr.X_op != O_constant)
7351 as_bad (_("Improper rotate count"));
7352 rot = imm_expr.X_add_number & 0x1f;
7353 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7355 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
7356 break;
7358 if (rot == 0)
7360 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7361 break;
7363 used_at = 1;
7364 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7365 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7366 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7368 break;
7370 case M_S_DOB:
7371 if (mips_opts.arch == CPU_R4650)
7373 as_bad (_("opcode not supported on this processor"));
7374 break;
7376 assert (mips_opts.isa == ISA_MIPS1);
7377 /* Even on a big endian machine $fn comes before $fn+1. We have
7378 to adjust when storing to memory. */
7379 macro_build (&offset_expr, "swc1", "T,o(b)",
7380 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
7381 offset_expr.X_add_number += 4;
7382 macro_build (&offset_expr, "swc1", "T,o(b)",
7383 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
7384 break;
7386 case M_SEQ:
7387 if (sreg == 0)
7388 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
7389 else if (treg == 0)
7390 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7391 else
7393 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7394 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7396 break;
7398 case M_SEQ_I:
7399 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7401 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7402 break;
7404 if (sreg == 0)
7406 as_warn (_("Instruction %s: result is always false"),
7407 ip->insn_mo->name);
7408 move_register (dreg, 0);
7409 break;
7411 if (imm_expr.X_op == O_constant
7412 && imm_expr.X_add_number >= 0
7413 && imm_expr.X_add_number < 0x10000)
7415 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7417 else if (imm_expr.X_op == O_constant
7418 && imm_expr.X_add_number > -0x8000
7419 && imm_expr.X_add_number < 0)
7421 imm_expr.X_add_number = -imm_expr.X_add_number;
7422 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7423 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7425 else
7427 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7428 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7429 used_at = 1;
7431 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7432 break;
7434 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7435 s = "slt";
7436 goto sge;
7437 case M_SGEU:
7438 s = "sltu";
7439 sge:
7440 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7441 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7442 break;
7444 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7445 case M_SGEU_I:
7446 if (imm_expr.X_op == O_constant
7447 && imm_expr.X_add_number >= -0x8000
7448 && imm_expr.X_add_number < 0x8000)
7450 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7451 dreg, sreg, BFD_RELOC_LO16);
7453 else
7455 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7456 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7457 dreg, sreg, AT);
7458 used_at = 1;
7460 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7461 break;
7463 case M_SGT: /* sreg > treg <==> treg < sreg */
7464 s = "slt";
7465 goto sgt;
7466 case M_SGTU:
7467 s = "sltu";
7468 sgt:
7469 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7470 break;
7472 case M_SGT_I: /* sreg > I <==> I < sreg */
7473 s = "slt";
7474 goto sgti;
7475 case M_SGTU_I:
7476 s = "sltu";
7477 sgti:
7478 used_at = 1;
7479 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7480 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7481 break;
7483 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7484 s = "slt";
7485 goto sle;
7486 case M_SLEU:
7487 s = "sltu";
7488 sle:
7489 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7490 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7491 break;
7493 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7494 s = "slt";
7495 goto slei;
7496 case M_SLEU_I:
7497 s = "sltu";
7498 slei:
7499 used_at = 1;
7500 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7501 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7502 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7503 break;
7505 case M_SLT_I:
7506 if (imm_expr.X_op == O_constant
7507 && imm_expr.X_add_number >= -0x8000
7508 && imm_expr.X_add_number < 0x8000)
7510 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7511 break;
7513 used_at = 1;
7514 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7515 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
7516 break;
7518 case M_SLTU_I:
7519 if (imm_expr.X_op == O_constant
7520 && imm_expr.X_add_number >= -0x8000
7521 && imm_expr.X_add_number < 0x8000)
7523 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
7524 BFD_RELOC_LO16);
7525 break;
7527 used_at = 1;
7528 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7529 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
7530 break;
7532 case M_SNE:
7533 if (sreg == 0)
7534 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
7535 else if (treg == 0)
7536 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7537 else
7539 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7540 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7542 break;
7544 case M_SNE_I:
7545 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7547 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7548 break;
7550 if (sreg == 0)
7552 as_warn (_("Instruction %s: result is always true"),
7553 ip->insn_mo->name);
7554 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7555 dreg, 0, BFD_RELOC_LO16);
7556 break;
7558 if (imm_expr.X_op == O_constant
7559 && imm_expr.X_add_number >= 0
7560 && imm_expr.X_add_number < 0x10000)
7562 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7564 else if (imm_expr.X_op == O_constant
7565 && imm_expr.X_add_number > -0x8000
7566 && imm_expr.X_add_number < 0)
7568 imm_expr.X_add_number = -imm_expr.X_add_number;
7569 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7570 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7572 else
7574 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7575 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7576 used_at = 1;
7578 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7579 break;
7581 case M_DSUB_I:
7582 dbl = 1;
7583 case M_SUB_I:
7584 if (imm_expr.X_op == O_constant
7585 && imm_expr.X_add_number > -0x8000
7586 && imm_expr.X_add_number <= 0x8000)
7588 imm_expr.X_add_number = -imm_expr.X_add_number;
7589 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7590 dreg, sreg, BFD_RELOC_LO16);
7591 break;
7593 used_at = 1;
7594 load_register (AT, &imm_expr, dbl);
7595 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7596 break;
7598 case M_DSUBU_I:
7599 dbl = 1;
7600 case M_SUBU_I:
7601 if (imm_expr.X_op == O_constant
7602 && imm_expr.X_add_number > -0x8000
7603 && imm_expr.X_add_number <= 0x8000)
7605 imm_expr.X_add_number = -imm_expr.X_add_number;
7606 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7607 dreg, sreg, BFD_RELOC_LO16);
7608 break;
7610 used_at = 1;
7611 load_register (AT, &imm_expr, dbl);
7612 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7613 break;
7615 case M_TEQ_I:
7616 s = "teq";
7617 goto trap;
7618 case M_TGE_I:
7619 s = "tge";
7620 goto trap;
7621 case M_TGEU_I:
7622 s = "tgeu";
7623 goto trap;
7624 case M_TLT_I:
7625 s = "tlt";
7626 goto trap;
7627 case M_TLTU_I:
7628 s = "tltu";
7629 goto trap;
7630 case M_TNE_I:
7631 s = "tne";
7632 trap:
7633 used_at = 1;
7634 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7635 macro_build (NULL, s, "s,t", sreg, AT);
7636 break;
7638 case M_TRUNCWS:
7639 case M_TRUNCWD:
7640 assert (mips_opts.isa == ISA_MIPS1);
7641 used_at = 1;
7642 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7643 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7646 * Is the double cfc1 instruction a bug in the mips assembler;
7647 * or is there a reason for it?
7649 start_noreorder ();
7650 macro_build (NULL, "cfc1", "t,G", treg, RA);
7651 macro_build (NULL, "cfc1", "t,G", treg, RA);
7652 macro_build (NULL, "nop", "");
7653 expr1.X_add_number = 3;
7654 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
7655 expr1.X_add_number = 2;
7656 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7657 macro_build (NULL, "ctc1", "t,G", AT, RA);
7658 macro_build (NULL, "nop", "");
7659 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7660 dreg, sreg);
7661 macro_build (NULL, "ctc1", "t,G", treg, RA);
7662 macro_build (NULL, "nop", "");
7663 end_noreorder ();
7664 break;
7666 case M_ULH:
7667 s = "lb";
7668 goto ulh;
7669 case M_ULHU:
7670 s = "lbu";
7671 ulh:
7672 used_at = 1;
7673 if (offset_expr.X_add_number >= 0x7fff)
7674 as_bad (_("operand overflow"));
7675 if (! target_big_endian)
7676 ++offset_expr.X_add_number;
7677 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
7678 if (! target_big_endian)
7679 --offset_expr.X_add_number;
7680 else
7681 ++offset_expr.X_add_number;
7682 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7683 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7684 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7685 break;
7687 case M_ULD:
7688 s = "ldl";
7689 s2 = "ldr";
7690 off = 7;
7691 goto ulw;
7692 case M_ULW:
7693 s = "lwl";
7694 s2 = "lwr";
7695 off = 3;
7696 ulw:
7697 if (offset_expr.X_add_number >= 0x8000 - off)
7698 as_bad (_("operand overflow"));
7699 if (treg != breg)
7700 tempreg = treg;
7701 else
7703 used_at = 1;
7704 tempreg = AT;
7706 if (! target_big_endian)
7707 offset_expr.X_add_number += off;
7708 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7709 if (! target_big_endian)
7710 offset_expr.X_add_number -= off;
7711 else
7712 offset_expr.X_add_number += off;
7713 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7715 /* If necessary, move the result in tempreg the final destination. */
7716 if (treg == tempreg)
7717 break;
7718 /* Protect second load's delay slot. */
7719 load_delay_nop ();
7720 move_register (treg, tempreg);
7721 break;
7723 case M_ULD_A:
7724 s = "ldl";
7725 s2 = "ldr";
7726 off = 7;
7727 goto ulwa;
7728 case M_ULW_A:
7729 s = "lwl";
7730 s2 = "lwr";
7731 off = 3;
7732 ulwa:
7733 used_at = 1;
7734 load_address (AT, &offset_expr, &used_at);
7735 if (breg != 0)
7736 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7737 if (! target_big_endian)
7738 expr1.X_add_number = off;
7739 else
7740 expr1.X_add_number = 0;
7741 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7742 if (! target_big_endian)
7743 expr1.X_add_number = 0;
7744 else
7745 expr1.X_add_number = off;
7746 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7747 break;
7749 case M_ULH_A:
7750 case M_ULHU_A:
7751 used_at = 1;
7752 load_address (AT, &offset_expr, &used_at);
7753 if (breg != 0)
7754 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7755 if (target_big_endian)
7756 expr1.X_add_number = 0;
7757 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7758 treg, BFD_RELOC_LO16, AT);
7759 if (target_big_endian)
7760 expr1.X_add_number = 1;
7761 else
7762 expr1.X_add_number = 0;
7763 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7764 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7765 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7766 break;
7768 case M_USH:
7769 used_at = 1;
7770 if (offset_expr.X_add_number >= 0x7fff)
7771 as_bad (_("operand overflow"));
7772 if (target_big_endian)
7773 ++offset_expr.X_add_number;
7774 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7775 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
7776 if (target_big_endian)
7777 --offset_expr.X_add_number;
7778 else
7779 ++offset_expr.X_add_number;
7780 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
7781 break;
7783 case M_USD:
7784 s = "sdl";
7785 s2 = "sdr";
7786 off = 7;
7787 goto usw;
7788 case M_USW:
7789 s = "swl";
7790 s2 = "swr";
7791 off = 3;
7792 usw:
7793 if (offset_expr.X_add_number >= 0x8000 - off)
7794 as_bad (_("operand overflow"));
7795 if (! target_big_endian)
7796 offset_expr.X_add_number += off;
7797 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7798 if (! target_big_endian)
7799 offset_expr.X_add_number -= off;
7800 else
7801 offset_expr.X_add_number += off;
7802 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7803 break;
7805 case M_USD_A:
7806 s = "sdl";
7807 s2 = "sdr";
7808 off = 7;
7809 goto uswa;
7810 case M_USW_A:
7811 s = "swl";
7812 s2 = "swr";
7813 off = 3;
7814 uswa:
7815 used_at = 1;
7816 load_address (AT, &offset_expr, &used_at);
7817 if (breg != 0)
7818 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7819 if (! target_big_endian)
7820 expr1.X_add_number = off;
7821 else
7822 expr1.X_add_number = 0;
7823 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7824 if (! target_big_endian)
7825 expr1.X_add_number = 0;
7826 else
7827 expr1.X_add_number = off;
7828 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7829 break;
7831 case M_USH_A:
7832 used_at = 1;
7833 load_address (AT, &offset_expr, &used_at);
7834 if (breg != 0)
7835 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7836 if (! target_big_endian)
7837 expr1.X_add_number = 0;
7838 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7839 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
7840 if (! target_big_endian)
7841 expr1.X_add_number = 1;
7842 else
7843 expr1.X_add_number = 0;
7844 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7845 if (! target_big_endian)
7846 expr1.X_add_number = 0;
7847 else
7848 expr1.X_add_number = 1;
7849 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7850 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7851 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7852 break;
7854 default:
7855 /* FIXME: Check if this is one of the itbl macros, since they
7856 are added dynamically. */
7857 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7858 break;
7860 if (mips_opts.noat && used_at)
7861 as_bad (_("Macro used $at after \".set noat\""));
7864 /* Implement macros in mips16 mode. */
7866 static void
7867 mips16_macro (struct mips_cl_insn *ip)
7869 int mask;
7870 int xreg, yreg, zreg, tmp;
7871 expressionS expr1;
7872 int dbl;
7873 const char *s, *s2, *s3;
7875 mask = ip->insn_mo->mask;
7877 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
7878 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
7879 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
7881 expr1.X_op = O_constant;
7882 expr1.X_op_symbol = NULL;
7883 expr1.X_add_symbol = NULL;
7884 expr1.X_add_number = 1;
7886 dbl = 0;
7888 switch (mask)
7890 default:
7891 internalError ();
7893 case M_DDIV_3:
7894 dbl = 1;
7895 case M_DIV_3:
7896 s = "mflo";
7897 goto do_div3;
7898 case M_DREM_3:
7899 dbl = 1;
7900 case M_REM_3:
7901 s = "mfhi";
7902 do_div3:
7903 start_noreorder ();
7904 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
7905 expr1.X_add_number = 2;
7906 macro_build (&expr1, "bnez", "x,p", yreg);
7907 macro_build (NULL, "break", "6", 7);
7909 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7910 since that causes an overflow. We should do that as well,
7911 but I don't see how to do the comparisons without a temporary
7912 register. */
7913 end_noreorder ();
7914 macro_build (NULL, s, "x", zreg);
7915 break;
7917 case M_DIVU_3:
7918 s = "divu";
7919 s2 = "mflo";
7920 goto do_divu3;
7921 case M_REMU_3:
7922 s = "divu";
7923 s2 = "mfhi";
7924 goto do_divu3;
7925 case M_DDIVU_3:
7926 s = "ddivu";
7927 s2 = "mflo";
7928 goto do_divu3;
7929 case M_DREMU_3:
7930 s = "ddivu";
7931 s2 = "mfhi";
7932 do_divu3:
7933 start_noreorder ();
7934 macro_build (NULL, s, "0,x,y", xreg, yreg);
7935 expr1.X_add_number = 2;
7936 macro_build (&expr1, "bnez", "x,p", yreg);
7937 macro_build (NULL, "break", "6", 7);
7938 end_noreorder ();
7939 macro_build (NULL, s2, "x", zreg);
7940 break;
7942 case M_DMUL:
7943 dbl = 1;
7944 case M_MUL:
7945 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7946 macro_build (NULL, "mflo", "x", zreg);
7947 break;
7949 case M_DSUBU_I:
7950 dbl = 1;
7951 goto do_subu;
7952 case M_SUBU_I:
7953 do_subu:
7954 if (imm_expr.X_op != O_constant)
7955 as_bad (_("Unsupported large constant"));
7956 imm_expr.X_add_number = -imm_expr.X_add_number;
7957 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7958 break;
7960 case M_SUBU_I_2:
7961 if (imm_expr.X_op != O_constant)
7962 as_bad (_("Unsupported large constant"));
7963 imm_expr.X_add_number = -imm_expr.X_add_number;
7964 macro_build (&imm_expr, "addiu", "x,k", xreg);
7965 break;
7967 case M_DSUBU_I_2:
7968 if (imm_expr.X_op != O_constant)
7969 as_bad (_("Unsupported large constant"));
7970 imm_expr.X_add_number = -imm_expr.X_add_number;
7971 macro_build (&imm_expr, "daddiu", "y,j", yreg);
7972 break;
7974 case M_BEQ:
7975 s = "cmp";
7976 s2 = "bteqz";
7977 goto do_branch;
7978 case M_BNE:
7979 s = "cmp";
7980 s2 = "btnez";
7981 goto do_branch;
7982 case M_BLT:
7983 s = "slt";
7984 s2 = "btnez";
7985 goto do_branch;
7986 case M_BLTU:
7987 s = "sltu";
7988 s2 = "btnez";
7989 goto do_branch;
7990 case M_BLE:
7991 s = "slt";
7992 s2 = "bteqz";
7993 goto do_reverse_branch;
7994 case M_BLEU:
7995 s = "sltu";
7996 s2 = "bteqz";
7997 goto do_reverse_branch;
7998 case M_BGE:
7999 s = "slt";
8000 s2 = "bteqz";
8001 goto do_branch;
8002 case M_BGEU:
8003 s = "sltu";
8004 s2 = "bteqz";
8005 goto do_branch;
8006 case M_BGT:
8007 s = "slt";
8008 s2 = "btnez";
8009 goto do_reverse_branch;
8010 case M_BGTU:
8011 s = "sltu";
8012 s2 = "btnez";
8014 do_reverse_branch:
8015 tmp = xreg;
8016 xreg = yreg;
8017 yreg = tmp;
8019 do_branch:
8020 macro_build (NULL, s, "x,y", xreg, yreg);
8021 macro_build (&offset_expr, s2, "p");
8022 break;
8024 case M_BEQ_I:
8025 s = "cmpi";
8026 s2 = "bteqz";
8027 s3 = "x,U";
8028 goto do_branch_i;
8029 case M_BNE_I:
8030 s = "cmpi";
8031 s2 = "btnez";
8032 s3 = "x,U";
8033 goto do_branch_i;
8034 case M_BLT_I:
8035 s = "slti";
8036 s2 = "btnez";
8037 s3 = "x,8";
8038 goto do_branch_i;
8039 case M_BLTU_I:
8040 s = "sltiu";
8041 s2 = "btnez";
8042 s3 = "x,8";
8043 goto do_branch_i;
8044 case M_BLE_I:
8045 s = "slti";
8046 s2 = "btnez";
8047 s3 = "x,8";
8048 goto do_addone_branch_i;
8049 case M_BLEU_I:
8050 s = "sltiu";
8051 s2 = "btnez";
8052 s3 = "x,8";
8053 goto do_addone_branch_i;
8054 case M_BGE_I:
8055 s = "slti";
8056 s2 = "bteqz";
8057 s3 = "x,8";
8058 goto do_branch_i;
8059 case M_BGEU_I:
8060 s = "sltiu";
8061 s2 = "bteqz";
8062 s3 = "x,8";
8063 goto do_branch_i;
8064 case M_BGT_I:
8065 s = "slti";
8066 s2 = "bteqz";
8067 s3 = "x,8";
8068 goto do_addone_branch_i;
8069 case M_BGTU_I:
8070 s = "sltiu";
8071 s2 = "bteqz";
8072 s3 = "x,8";
8074 do_addone_branch_i:
8075 if (imm_expr.X_op != O_constant)
8076 as_bad (_("Unsupported large constant"));
8077 ++imm_expr.X_add_number;
8079 do_branch_i:
8080 macro_build (&imm_expr, s, s3, xreg);
8081 macro_build (&offset_expr, s2, "p");
8082 break;
8084 case M_ABS:
8085 expr1.X_add_number = 0;
8086 macro_build (&expr1, "slti", "x,8", yreg);
8087 if (xreg != yreg)
8088 move_register (xreg, yreg);
8089 expr1.X_add_number = 2;
8090 macro_build (&expr1, "bteqz", "p");
8091 macro_build (NULL, "neg", "x,w", xreg, xreg);
8095 /* For consistency checking, verify that all bits are specified either
8096 by the match/mask part of the instruction definition, or by the
8097 operand list. */
8098 static int
8099 validate_mips_insn (const struct mips_opcode *opc)
8101 const char *p = opc->args;
8102 char c;
8103 unsigned long used_bits = opc->mask;
8105 if ((used_bits & opc->match) != opc->match)
8107 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8108 opc->name, opc->args);
8109 return 0;
8111 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8112 while (*p)
8113 switch (c = *p++)
8115 case ',': break;
8116 case '(': break;
8117 case ')': break;
8118 case '+':
8119 switch (c = *p++)
8121 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
8122 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
8123 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
8124 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
8125 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8126 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8127 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8128 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8129 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
8130 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8131 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8132 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8133 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8134 case 'I': break;
8135 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8136 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
8137 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
8138 default:
8139 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8140 c, opc->name, opc->args);
8141 return 0;
8143 break;
8144 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8145 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8146 case 'A': break;
8147 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
8148 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8149 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8150 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8151 case 'F': break;
8152 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8153 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
8154 case 'I': break;
8155 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
8156 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8157 case 'L': break;
8158 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8159 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
8160 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8161 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8162 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8163 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8164 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8165 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8166 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8167 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8168 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8169 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8170 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8171 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8172 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8173 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8174 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8175 case 'f': break;
8176 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8177 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8178 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8179 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8180 case 'l': break;
8181 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8182 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8183 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8184 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8185 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8186 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8187 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8188 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8189 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8190 case 'x': break;
8191 case 'z': break;
8192 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
8193 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8194 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8195 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8196 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8197 case '[': break;
8198 case ']': break;
8199 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
8200 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
8201 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
8202 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8203 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
8204 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
8205 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
8206 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
8207 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
8208 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
8209 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
8210 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
8211 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
8212 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
8213 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
8214 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8215 default:
8216 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8217 c, opc->name, opc->args);
8218 return 0;
8220 #undef USE_BITS
8221 if (used_bits != 0xffffffff)
8223 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8224 ~used_bits & 0xffffffff, opc->name, opc->args);
8225 return 0;
8227 return 1;
8230 /* UDI immediates. */
8231 struct mips_immed {
8232 char type;
8233 unsigned int shift;
8234 unsigned long mask;
8235 const char * desc;
8238 static const struct mips_immed mips_immed[] = {
8239 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
8240 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
8241 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
8242 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
8243 { 0,0,0,0 }
8246 /* Check whether an odd floating-point register is allowed. */
8247 static int
8248 mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
8250 const char *s = insn->name;
8252 if (insn->pinfo == INSN_MACRO)
8253 /* Let a macro pass, we'll catch it later when it is expanded. */
8254 return 1;
8256 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa))
8258 /* Allow odd registers for single-precision ops. */
8259 switch (insn->pinfo & (FP_S | FP_D))
8261 case FP_S:
8262 case 0:
8263 return 1; /* both single precision - ok */
8264 case FP_D:
8265 return 0; /* both double precision - fail */
8266 default:
8267 break;
8270 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
8271 s = strchr (insn->name, '.');
8272 if (argnum == 2)
8273 s = s != NULL ? strchr (s + 1, '.') : NULL;
8274 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
8277 /* Single-precision coprocessor loads and moves are OK too. */
8278 if ((insn->pinfo & FP_S)
8279 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
8280 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
8281 return 1;
8283 return 0;
8286 /* This routine assembles an instruction into its binary format. As a
8287 side effect, it sets one of the global variables imm_reloc or
8288 offset_reloc to the type of relocation to do if one of the operands
8289 is an address expression. */
8291 static void
8292 mips_ip (char *str, struct mips_cl_insn *ip)
8294 char *s;
8295 const char *args;
8296 char c = 0;
8297 struct mips_opcode *insn;
8298 char *argsStart;
8299 unsigned int regno;
8300 unsigned int lastregno = 0;
8301 unsigned int lastpos = 0;
8302 unsigned int limlo, limhi;
8303 char *s_reset;
8304 char save_c = 0;
8305 offsetT min_range, max_range;
8306 int argnum;
8307 unsigned int rtype;
8309 insn_error = NULL;
8311 /* If the instruction contains a '.', we first try to match an instruction
8312 including the '.'. Then we try again without the '.'. */
8313 insn = NULL;
8314 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8315 continue;
8317 /* If we stopped on whitespace, then replace the whitespace with null for
8318 the call to hash_find. Save the character we replaced just in case we
8319 have to re-parse the instruction. */
8320 if (ISSPACE (*s))
8322 save_c = *s;
8323 *s++ = '\0';
8326 insn = (struct mips_opcode *) hash_find (op_hash, str);
8328 /* If we didn't find the instruction in the opcode table, try again, but
8329 this time with just the instruction up to, but not including the
8330 first '.'. */
8331 if (insn == NULL)
8333 /* Restore the character we overwrite above (if any). */
8334 if (save_c)
8335 *(--s) = save_c;
8337 /* Scan up to the first '.' or whitespace. */
8338 for (s = str;
8339 *s != '\0' && *s != '.' && !ISSPACE (*s);
8340 ++s)
8341 continue;
8343 /* If we did not find a '.', then we can quit now. */
8344 if (*s != '.')
8346 insn_error = "unrecognized opcode";
8347 return;
8350 /* Lookup the instruction in the hash table. */
8351 *s++ = '\0';
8352 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8354 insn_error = "unrecognized opcode";
8355 return;
8359 argsStart = s;
8360 for (;;)
8362 bfd_boolean ok;
8364 assert (strcmp (insn->name, str) == 0);
8366 if (OPCODE_IS_MEMBER (insn,
8367 (mips_opts.isa
8368 /* We don't check for mips_opts.mips16 here since
8369 we want to allow jalx if -mips16 was specified
8370 on the command line. */
8371 | (file_ase_mips16 ? INSN_MIPS16 : 0)
8372 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8373 | (mips_opts.ase_dsp ? INSN_DSP : 0)
8374 | (mips_opts.ase_mt ? INSN_MT : 0)
8375 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)
8376 | (mips_opts.ase_smartmips ? INSN_SMARTMIPS : 0)),
8377 mips_opts.arch))
8378 ok = TRUE;
8379 else
8380 ok = FALSE;
8382 if (insn->pinfo != INSN_MACRO)
8384 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8385 ok = FALSE;
8388 if (! ok)
8390 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8391 && strcmp (insn->name, insn[1].name) == 0)
8393 ++insn;
8394 continue;
8396 else
8398 if (!insn_error)
8400 static char buf[100];
8401 sprintf (buf,
8402 _("opcode not supported on this processor: %s (%s)"),
8403 mips_cpu_info_from_arch (mips_opts.arch)->name,
8404 mips_cpu_info_from_isa (mips_opts.isa)->name);
8405 insn_error = buf;
8407 if (save_c)
8408 *(--s) = save_c;
8409 return;
8413 create_insn (ip, insn);
8414 insn_error = NULL;
8415 argnum = 1;
8416 for (args = insn->args;; ++args)
8418 int is_mdmx;
8420 s += strspn (s, " \t");
8421 is_mdmx = 0;
8422 switch (*args)
8424 case '\0': /* end of args */
8425 if (*s == '\0')
8426 return;
8427 break;
8429 case '3': /* dsp 3-bit unsigned immediate in bit 21 */
8430 my_getExpression (&imm_expr, s);
8431 check_absolute_expr (ip, &imm_expr);
8432 if (imm_expr.X_add_number & ~OP_MASK_SA3)
8434 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8435 OP_MASK_SA3, (unsigned long) imm_expr.X_add_number);
8436 imm_expr.X_add_number &= OP_MASK_SA3;
8438 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SA3;
8439 imm_expr.X_op = O_absent;
8440 s = expr_end;
8441 continue;
8443 case '4': /* dsp 4-bit unsigned immediate in bit 21 */
8444 my_getExpression (&imm_expr, s);
8445 check_absolute_expr (ip, &imm_expr);
8446 if (imm_expr.X_add_number & ~OP_MASK_SA4)
8448 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8449 OP_MASK_SA4, (unsigned long) imm_expr.X_add_number);
8450 imm_expr.X_add_number &= OP_MASK_SA4;
8452 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SA4;
8453 imm_expr.X_op = O_absent;
8454 s = expr_end;
8455 continue;
8457 case '5': /* dsp 8-bit unsigned immediate in bit 16 */
8458 my_getExpression (&imm_expr, s);
8459 check_absolute_expr (ip, &imm_expr);
8460 if (imm_expr.X_add_number & ~OP_MASK_IMM8)
8462 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8463 OP_MASK_IMM8, (unsigned long) imm_expr.X_add_number);
8464 imm_expr.X_add_number &= OP_MASK_IMM8;
8466 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_IMM8;
8467 imm_expr.X_op = O_absent;
8468 s = expr_end;
8469 continue;
8471 case '6': /* dsp 5-bit unsigned immediate in bit 21 */
8472 my_getExpression (&imm_expr, s);
8473 check_absolute_expr (ip, &imm_expr);
8474 if (imm_expr.X_add_number & ~OP_MASK_RS)
8476 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8477 OP_MASK_RS, (unsigned long) imm_expr.X_add_number);
8478 imm_expr.X_add_number &= OP_MASK_RS;
8480 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_RS;
8481 imm_expr.X_op = O_absent;
8482 s = expr_end;
8483 continue;
8485 case '7': /* four dsp accumulators in bits 11,12 */
8486 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8487 s[3] >= '0' && s[3] <= '3')
8489 regno = s[3] - '0';
8490 s += 4;
8491 ip->insn_opcode |= regno << OP_SH_DSPACC;
8492 continue;
8494 else
8495 as_bad (_("Invalid dsp acc register"));
8496 break;
8498 case '8': /* dsp 6-bit unsigned immediate in bit 11 */
8499 my_getExpression (&imm_expr, s);
8500 check_absolute_expr (ip, &imm_expr);
8501 if (imm_expr.X_add_number & ~OP_MASK_WRDSP)
8503 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8504 OP_MASK_WRDSP,
8505 (unsigned long) imm_expr.X_add_number);
8506 imm_expr.X_add_number &= OP_MASK_WRDSP;
8508 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_WRDSP;
8509 imm_expr.X_op = O_absent;
8510 s = expr_end;
8511 continue;
8513 case '9': /* four dsp accumulators in bits 21,22 */
8514 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8515 s[3] >= '0' && s[3] <= '3')
8517 regno = s[3] - '0';
8518 s += 4;
8519 ip->insn_opcode |= regno << OP_SH_DSPACC_S;
8520 continue;
8522 else
8523 as_bad (_("Invalid dsp acc register"));
8524 break;
8526 case '0': /* dsp 6-bit signed immediate in bit 20 */
8527 my_getExpression (&imm_expr, s);
8528 check_absolute_expr (ip, &imm_expr);
8529 min_range = -((OP_MASK_DSPSFT + 1) >> 1);
8530 max_range = ((OP_MASK_DSPSFT + 1) >> 1) - 1;
8531 if (imm_expr.X_add_number < min_range ||
8532 imm_expr.X_add_number > max_range)
8534 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8535 (long) min_range, (long) max_range,
8536 (long) imm_expr.X_add_number);
8538 imm_expr.X_add_number &= OP_MASK_DSPSFT;
8539 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8540 << OP_SH_DSPSFT);
8541 imm_expr.X_op = O_absent;
8542 s = expr_end;
8543 continue;
8545 case '\'': /* dsp 6-bit unsigned immediate in bit 16 */
8546 my_getExpression (&imm_expr, s);
8547 check_absolute_expr (ip, &imm_expr);
8548 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
8550 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8551 OP_MASK_RDDSP,
8552 (unsigned long) imm_expr.X_add_number);
8553 imm_expr.X_add_number &= OP_MASK_RDDSP;
8555 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_RDDSP;
8556 imm_expr.X_op = O_absent;
8557 s = expr_end;
8558 continue;
8560 case ':': /* dsp 7-bit signed immediate in bit 19 */
8561 my_getExpression (&imm_expr, s);
8562 check_absolute_expr (ip, &imm_expr);
8563 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
8564 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
8565 if (imm_expr.X_add_number < min_range ||
8566 imm_expr.X_add_number > max_range)
8568 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8569 (long) min_range, (long) max_range,
8570 (long) imm_expr.X_add_number);
8572 imm_expr.X_add_number &= OP_MASK_DSPSFT_7;
8573 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8574 << OP_SH_DSPSFT_7);
8575 imm_expr.X_op = O_absent;
8576 s = expr_end;
8577 continue;
8579 case '@': /* dsp 10-bit signed immediate in bit 16 */
8580 my_getExpression (&imm_expr, s);
8581 check_absolute_expr (ip, &imm_expr);
8582 min_range = -((OP_MASK_IMM10 + 1) >> 1);
8583 max_range = ((OP_MASK_IMM10 + 1) >> 1) - 1;
8584 if (imm_expr.X_add_number < min_range ||
8585 imm_expr.X_add_number > max_range)
8587 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8588 (long) min_range, (long) max_range,
8589 (long) imm_expr.X_add_number);
8591 imm_expr.X_add_number &= OP_MASK_IMM10;
8592 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8593 << OP_SH_IMM10);
8594 imm_expr.X_op = O_absent;
8595 s = expr_end;
8596 continue;
8598 case '!': /* mt 1-bit unsigned immediate in bit 5 */
8599 my_getExpression (&imm_expr, s);
8600 check_absolute_expr (ip, &imm_expr);
8601 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
8603 as_warn (_("MT immediate not in range 0..%d (%lu)"),
8604 OP_MASK_MT_U, (unsigned long) imm_expr.X_add_number);
8605 imm_expr.X_add_number &= OP_MASK_MT_U;
8607 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_MT_U;
8608 imm_expr.X_op = O_absent;
8609 s = expr_end;
8610 continue;
8612 case '$': /* mt 1-bit unsigned immediate in bit 4 */
8613 my_getExpression (&imm_expr, s);
8614 check_absolute_expr (ip, &imm_expr);
8615 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
8617 as_warn (_("MT immediate not in range 0..%d (%lu)"),
8618 OP_MASK_MT_H, (unsigned long) imm_expr.X_add_number);
8619 imm_expr.X_add_number &= OP_MASK_MT_H;
8621 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_MT_H;
8622 imm_expr.X_op = O_absent;
8623 s = expr_end;
8624 continue;
8626 case '*': /* four dsp accumulators in bits 18,19 */
8627 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8628 s[3] >= '0' && s[3] <= '3')
8630 regno = s[3] - '0';
8631 s += 4;
8632 ip->insn_opcode |= regno << OP_SH_MTACC_T;
8633 continue;
8635 else
8636 as_bad (_("Invalid dsp/smartmips acc register"));
8637 break;
8639 case '&': /* four dsp accumulators in bits 13,14 */
8640 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8641 s[3] >= '0' && s[3] <= '3')
8643 regno = s[3] - '0';
8644 s += 4;
8645 ip->insn_opcode |= regno << OP_SH_MTACC_D;
8646 continue;
8648 else
8649 as_bad (_("Invalid dsp/smartmips acc register"));
8650 break;
8652 case ',':
8653 if (*s++ == *args)
8654 continue;
8655 s--;
8656 switch (*++args)
8658 case 'r':
8659 case 'v':
8660 INSERT_OPERAND (RS, *ip, lastregno);
8661 continue;
8663 case 'w':
8664 INSERT_OPERAND (RT, *ip, lastregno);
8665 continue;
8667 case 'W':
8668 INSERT_OPERAND (FT, *ip, lastregno);
8669 continue;
8671 case 'V':
8672 INSERT_OPERAND (FS, *ip, lastregno);
8673 continue;
8675 break;
8677 case '(':
8678 /* Handle optional base register.
8679 Either the base register is omitted or
8680 we must have a left paren. */
8681 /* This is dependent on the next operand specifier
8682 is a base register specification. */
8683 assert (args[1] == 'b' || args[1] == '5'
8684 || args[1] == '-' || args[1] == '4');
8685 if (*s == '\0')
8686 return;
8688 case ')': /* these must match exactly */
8689 case '[':
8690 case ']':
8691 if (*s++ == *args)
8692 continue;
8693 break;
8695 case '+': /* Opcode extension character. */
8696 switch (*++args)
8698 case '1': /* UDI immediates. */
8699 case '2':
8700 case '3':
8701 case '4':
8703 const struct mips_immed *imm = mips_immed;
8705 while (imm->type && imm->type != *args)
8706 ++imm;
8707 if (! imm->type)
8708 internalError ();
8709 my_getExpression (&imm_expr, s);
8710 check_absolute_expr (ip, &imm_expr);
8711 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
8713 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
8714 imm->desc ? imm->desc : ip->insn_mo->name,
8715 (unsigned long) imm_expr.X_add_number,
8716 (unsigned long) imm_expr.X_add_number);
8717 imm_expr.X_add_number &= imm->mask;
8719 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8720 << imm->shift);
8721 imm_expr.X_op = O_absent;
8722 s = expr_end;
8724 continue;
8726 case 'A': /* ins/ext position, becomes LSB. */
8727 limlo = 0;
8728 limhi = 31;
8729 goto do_lsb;
8730 case 'E':
8731 limlo = 32;
8732 limhi = 63;
8733 goto do_lsb;
8734 do_lsb:
8735 my_getExpression (&imm_expr, s);
8736 check_absolute_expr (ip, &imm_expr);
8737 if ((unsigned long) imm_expr.X_add_number < limlo
8738 || (unsigned long) imm_expr.X_add_number > limhi)
8740 as_bad (_("Improper position (%lu)"),
8741 (unsigned long) imm_expr.X_add_number);
8742 imm_expr.X_add_number = limlo;
8744 lastpos = imm_expr.X_add_number;
8745 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8746 imm_expr.X_op = O_absent;
8747 s = expr_end;
8748 continue;
8750 case 'B': /* ins size, becomes MSB. */
8751 limlo = 1;
8752 limhi = 32;
8753 goto do_msb;
8754 case 'F':
8755 limlo = 33;
8756 limhi = 64;
8757 goto do_msb;
8758 do_msb:
8759 my_getExpression (&imm_expr, s);
8760 check_absolute_expr (ip, &imm_expr);
8761 /* Check for negative input so that small negative numbers
8762 will not succeed incorrectly. The checks against
8763 (pos+size) transitively check "size" itself,
8764 assuming that "pos" is reasonable. */
8765 if ((long) imm_expr.X_add_number < 0
8766 || ((unsigned long) imm_expr.X_add_number
8767 + lastpos) < limlo
8768 || ((unsigned long) imm_expr.X_add_number
8769 + lastpos) > limhi)
8771 as_bad (_("Improper insert size (%lu, position %lu)"),
8772 (unsigned long) imm_expr.X_add_number,
8773 (unsigned long) lastpos);
8774 imm_expr.X_add_number = limlo - lastpos;
8776 INSERT_OPERAND (INSMSB, *ip,
8777 lastpos + imm_expr.X_add_number - 1);
8778 imm_expr.X_op = O_absent;
8779 s = expr_end;
8780 continue;
8782 case 'C': /* ext size, becomes MSBD. */
8783 limlo = 1;
8784 limhi = 32;
8785 goto do_msbd;
8786 case 'G':
8787 limlo = 33;
8788 limhi = 64;
8789 goto do_msbd;
8790 case 'H':
8791 limlo = 33;
8792 limhi = 64;
8793 goto do_msbd;
8794 do_msbd:
8795 my_getExpression (&imm_expr, s);
8796 check_absolute_expr (ip, &imm_expr);
8797 /* Check for negative input so that small negative numbers
8798 will not succeed incorrectly. The checks against
8799 (pos+size) transitively check "size" itself,
8800 assuming that "pos" is reasonable. */
8801 if ((long) imm_expr.X_add_number < 0
8802 || ((unsigned long) imm_expr.X_add_number
8803 + lastpos) < limlo
8804 || ((unsigned long) imm_expr.X_add_number
8805 + lastpos) > limhi)
8807 as_bad (_("Improper extract size (%lu, position %lu)"),
8808 (unsigned long) imm_expr.X_add_number,
8809 (unsigned long) lastpos);
8810 imm_expr.X_add_number = limlo - lastpos;
8812 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
8813 imm_expr.X_op = O_absent;
8814 s = expr_end;
8815 continue;
8817 case 'D':
8818 /* +D is for disassembly only; never match. */
8819 break;
8821 case 'I':
8822 /* "+I" is like "I", except that imm2_expr is used. */
8823 my_getExpression (&imm2_expr, s);
8824 if (imm2_expr.X_op != O_big
8825 && imm2_expr.X_op != O_constant)
8826 insn_error = _("absolute expression required");
8827 if (HAVE_32BIT_GPRS)
8828 normalize_constant_expr (&imm2_expr);
8829 s = expr_end;
8830 continue;
8832 case 'T': /* Coprocessor register. */
8833 /* +T is for disassembly only; never match. */
8834 break;
8836 case 't': /* Coprocessor register number. */
8837 if (s[0] == '$' && ISDIGIT (s[1]))
8839 ++s;
8840 regno = 0;
8843 regno *= 10;
8844 regno += *s - '0';
8845 ++s;
8847 while (ISDIGIT (*s));
8848 if (regno > 31)
8849 as_bad (_("Invalid register number (%d)"), regno);
8850 else
8852 ip->insn_opcode |= regno << OP_SH_RT;
8853 continue;
8856 else
8857 as_bad (_("Invalid coprocessor 0 register number"));
8858 break;
8860 default:
8861 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8862 *args, insn->name, insn->args);
8863 /* Further processing is fruitless. */
8864 return;
8866 break;
8868 case '<': /* must be at least one digit */
8870 * According to the manual, if the shift amount is greater
8871 * than 31 or less than 0, then the shift amount should be
8872 * mod 32. In reality the mips assembler issues an error.
8873 * We issue a warning and mask out all but the low 5 bits.
8875 my_getExpression (&imm_expr, s);
8876 check_absolute_expr (ip, &imm_expr);
8877 if ((unsigned long) imm_expr.X_add_number > 31)
8878 as_warn (_("Improper shift amount (%lu)"),
8879 (unsigned long) imm_expr.X_add_number);
8880 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8881 imm_expr.X_op = O_absent;
8882 s = expr_end;
8883 continue;
8885 case '>': /* shift amount minus 32 */
8886 my_getExpression (&imm_expr, s);
8887 check_absolute_expr (ip, &imm_expr);
8888 if ((unsigned long) imm_expr.X_add_number < 32
8889 || (unsigned long) imm_expr.X_add_number > 63)
8890 break;
8891 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
8892 imm_expr.X_op = O_absent;
8893 s = expr_end;
8894 continue;
8896 case 'k': /* cache code */
8897 case 'h': /* prefx code */
8898 my_getExpression (&imm_expr, s);
8899 check_absolute_expr (ip, &imm_expr);
8900 if ((unsigned long) imm_expr.X_add_number > 31)
8901 as_warn (_("Invalid value for `%s' (%lu)"),
8902 ip->insn_mo->name,
8903 (unsigned long) imm_expr.X_add_number);
8904 if (*args == 'k')
8905 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
8906 else
8907 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
8908 imm_expr.X_op = O_absent;
8909 s = expr_end;
8910 continue;
8912 case 'c': /* break code */
8913 my_getExpression (&imm_expr, s);
8914 check_absolute_expr (ip, &imm_expr);
8915 if ((unsigned long) imm_expr.X_add_number > 1023)
8916 as_warn (_("Illegal break code (%lu)"),
8917 (unsigned long) imm_expr.X_add_number);
8918 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
8919 imm_expr.X_op = O_absent;
8920 s = expr_end;
8921 continue;
8923 case 'q': /* lower break code */
8924 my_getExpression (&imm_expr, s);
8925 check_absolute_expr (ip, &imm_expr);
8926 if ((unsigned long) imm_expr.X_add_number > 1023)
8927 as_warn (_("Illegal lower break code (%lu)"),
8928 (unsigned long) imm_expr.X_add_number);
8929 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
8930 imm_expr.X_op = O_absent;
8931 s = expr_end;
8932 continue;
8934 case 'B': /* 20-bit syscall/break code. */
8935 my_getExpression (&imm_expr, s);
8936 check_absolute_expr (ip, &imm_expr);
8937 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8938 as_warn (_("Illegal 20-bit code (%lu)"),
8939 (unsigned long) imm_expr.X_add_number);
8940 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
8941 imm_expr.X_op = O_absent;
8942 s = expr_end;
8943 continue;
8945 case 'C': /* Coprocessor code */
8946 my_getExpression (&imm_expr, s);
8947 check_absolute_expr (ip, &imm_expr);
8948 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8950 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8951 (unsigned long) imm_expr.X_add_number);
8952 imm_expr.X_add_number &= ((1 << 25) - 1);
8954 ip->insn_opcode |= imm_expr.X_add_number;
8955 imm_expr.X_op = O_absent;
8956 s = expr_end;
8957 continue;
8959 case 'J': /* 19-bit wait code. */
8960 my_getExpression (&imm_expr, s);
8961 check_absolute_expr (ip, &imm_expr);
8962 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8963 as_warn (_("Illegal 19-bit code (%lu)"),
8964 (unsigned long) imm_expr.X_add_number);
8965 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
8966 imm_expr.X_op = O_absent;
8967 s = expr_end;
8968 continue;
8970 case 'P': /* Performance register. */
8971 my_getExpression (&imm_expr, s);
8972 check_absolute_expr (ip, &imm_expr);
8973 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8974 as_warn (_("Invalid performance register (%lu)"),
8975 (unsigned long) imm_expr.X_add_number);
8976 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
8977 imm_expr.X_op = O_absent;
8978 s = expr_end;
8979 continue;
8981 case 'G': /* Coprocessor destination register. */
8982 if (((ip->insn_opcode >> OP_SH_OP) & OP_MASK_OP) == OP_OP_COP0)
8983 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_CP0, &regno);
8984 else
8985 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
8986 ip->insn_opcode |= regno << OP_SH_RD;
8987 if (ok)
8989 lastregno = regno;
8990 continue;
8992 else
8993 break;
8995 case 'b': /* base register */
8996 case 'd': /* destination register */
8997 case 's': /* source register */
8998 case 't': /* target register */
8999 case 'r': /* both target and source */
9000 case 'v': /* both dest and source */
9001 case 'w': /* both dest and target */
9002 case 'E': /* coprocessor target register */
9003 case 'K': /* 'rdhwr' destination register */
9004 case 'x': /* ignore register name */
9005 case 'z': /* must be zero register */
9006 case 'U': /* destination register (clo/clz). */
9007 case 'g': /* coprocessor destination register */
9008 s_reset = s;
9009 if (*args == 'E' || *args == 'K')
9010 ok = reg_lookup (&s, RTYPE_NUM, &regno);
9011 else
9013 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
9014 if (regno == AT && ! mips_opts.noat)
9015 as_warn ("Used $at without \".set noat\"");
9017 if (ok)
9019 c = *args;
9020 if (*s == ' ')
9021 ++s;
9022 if (args[1] != *s)
9024 if (c == 'r' || c == 'v' || c == 'w')
9026 regno = lastregno;
9027 s = s_reset;
9028 ++args;
9031 /* 'z' only matches $0. */
9032 if (c == 'z' && regno != 0)
9033 break;
9035 /* Now that we have assembled one operand, we use the args string
9036 * to figure out where it goes in the instruction. */
9037 switch (c)
9039 case 'r':
9040 case 's':
9041 case 'v':
9042 case 'b':
9043 INSERT_OPERAND (RS, *ip, regno);
9044 break;
9045 case 'd':
9046 case 'G':
9047 case 'K':
9048 case 'g':
9049 INSERT_OPERAND (RD, *ip, regno);
9050 break;
9051 case 'U':
9052 INSERT_OPERAND (RD, *ip, regno);
9053 INSERT_OPERAND (RT, *ip, regno);
9054 break;
9055 case 'w':
9056 case 't':
9057 case 'E':
9058 INSERT_OPERAND (RT, *ip, regno);
9059 break;
9060 case 'x':
9061 /* This case exists because on the r3000 trunc
9062 expands into a macro which requires a gp
9063 register. On the r6000 or r4000 it is
9064 assembled into a single instruction which
9065 ignores the register. Thus the insn version
9066 is MIPS_ISA2 and uses 'x', and the macro
9067 version is MIPS_ISA1 and uses 't'. */
9068 break;
9069 case 'z':
9070 /* This case is for the div instruction, which
9071 acts differently if the destination argument
9072 is $0. This only matches $0, and is checked
9073 outside the switch. */
9074 break;
9075 case 'D':
9076 /* Itbl operand; not yet implemented. FIXME ?? */
9077 break;
9078 /* What about all other operands like 'i', which
9079 can be specified in the opcode table? */
9081 lastregno = regno;
9082 continue;
9084 switch (*args++)
9086 case 'r':
9087 case 'v':
9088 INSERT_OPERAND (RS, *ip, lastregno);
9089 continue;
9090 case 'w':
9091 INSERT_OPERAND (RT, *ip, lastregno);
9092 continue;
9094 break;
9096 case 'O': /* MDMX alignment immediate constant. */
9097 my_getExpression (&imm_expr, s);
9098 check_absolute_expr (ip, &imm_expr);
9099 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
9100 as_warn ("Improper align amount (%ld), using low bits",
9101 (long) imm_expr.X_add_number);
9102 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
9103 imm_expr.X_op = O_absent;
9104 s = expr_end;
9105 continue;
9107 case 'Q': /* MDMX vector, element sel, or const. */
9108 if (s[0] != '$')
9110 /* MDMX Immediate. */
9111 my_getExpression (&imm_expr, s);
9112 check_absolute_expr (ip, &imm_expr);
9113 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
9114 as_warn (_("Invalid MDMX Immediate (%ld)"),
9115 (long) imm_expr.X_add_number);
9116 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
9117 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9118 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
9119 else
9120 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
9121 imm_expr.X_op = O_absent;
9122 s = expr_end;
9123 continue;
9125 /* Not MDMX Immediate. Fall through. */
9126 case 'X': /* MDMX destination register. */
9127 case 'Y': /* MDMX source register. */
9128 case 'Z': /* MDMX target register. */
9129 is_mdmx = 1;
9130 case 'D': /* floating point destination register */
9131 case 'S': /* floating point source register */
9132 case 'T': /* floating point target register */
9133 case 'R': /* floating point source register */
9134 case 'V':
9135 case 'W':
9136 rtype = RTYPE_FPU;
9137 if (is_mdmx
9138 || (mips_opts.ase_mdmx
9139 && (ip->insn_mo->pinfo & FP_D)
9140 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
9141 | INSN_COPROC_MEMORY_DELAY
9142 | INSN_LOAD_COPROC_DELAY
9143 | INSN_LOAD_MEMORY_DELAY
9144 | INSN_STORE_MEMORY))))
9145 rtype |= RTYPE_VEC;
9146 s_reset = s;
9147 if (reg_lookup (&s, rtype, &regno))
9149 if ((regno & 1) != 0
9150 && HAVE_32BIT_FPRS
9151 && ! mips_oddfpreg_ok (ip->insn_mo, argnum))
9152 as_warn (_("Float register should be even, was %d"),
9153 regno);
9155 c = *args;
9156 if (*s == ' ')
9157 ++s;
9158 if (args[1] != *s)
9160 if (c == 'V' || c == 'W')
9162 regno = lastregno;
9163 s = s_reset;
9164 ++args;
9167 switch (c)
9169 case 'D':
9170 case 'X':
9171 INSERT_OPERAND (FD, *ip, regno);
9172 break;
9173 case 'V':
9174 case 'S':
9175 case 'Y':
9176 INSERT_OPERAND (FS, *ip, regno);
9177 break;
9178 case 'Q':
9179 /* This is like 'Z', but also needs to fix the MDMX
9180 vector/scalar select bits. Note that the
9181 scalar immediate case is handled above. */
9182 if (*s == '[')
9184 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9185 int max_el = (is_qh ? 3 : 7);
9186 s++;
9187 my_getExpression(&imm_expr, s);
9188 check_absolute_expr (ip, &imm_expr);
9189 s = expr_end;
9190 if (imm_expr.X_add_number > max_el)
9191 as_bad(_("Bad element selector %ld"),
9192 (long) imm_expr.X_add_number);
9193 imm_expr.X_add_number &= max_el;
9194 ip->insn_opcode |= (imm_expr.X_add_number
9195 << (OP_SH_VSEL +
9196 (is_qh ? 2 : 1)));
9197 imm_expr.X_op = O_absent;
9198 if (*s != ']')
9199 as_warn(_("Expecting ']' found '%s'"), s);
9200 else
9201 s++;
9203 else
9205 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9206 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9207 << OP_SH_VSEL);
9208 else
9209 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9210 OP_SH_VSEL);
9212 /* Fall through */
9213 case 'W':
9214 case 'T':
9215 case 'Z':
9216 INSERT_OPERAND (FT, *ip, regno);
9217 break;
9218 case 'R':
9219 INSERT_OPERAND (FR, *ip, regno);
9220 break;
9222 lastregno = regno;
9223 continue;
9226 switch (*args++)
9228 case 'V':
9229 INSERT_OPERAND (FS, *ip, lastregno);
9230 continue;
9231 case 'W':
9232 INSERT_OPERAND (FT, *ip, lastregno);
9233 continue;
9235 break;
9237 case 'I':
9238 my_getExpression (&imm_expr, s);
9239 if (imm_expr.X_op != O_big
9240 && imm_expr.X_op != O_constant)
9241 insn_error = _("absolute expression required");
9242 if (HAVE_32BIT_GPRS)
9243 normalize_constant_expr (&imm_expr);
9244 s = expr_end;
9245 continue;
9247 case 'A':
9248 my_getExpression (&offset_expr, s);
9249 normalize_address_expr (&offset_expr);
9250 *imm_reloc = BFD_RELOC_32;
9251 s = expr_end;
9252 continue;
9254 case 'F':
9255 case 'L':
9256 case 'f':
9257 case 'l':
9259 int f64;
9260 int using_gprs;
9261 char *save_in;
9262 char *err;
9263 unsigned char temp[8];
9264 int len;
9265 unsigned int length;
9266 segT seg;
9267 subsegT subseg;
9268 char *p;
9270 /* These only appear as the last operand in an
9271 instruction, and every instruction that accepts
9272 them in any variant accepts them in all variants.
9273 This means we don't have to worry about backing out
9274 any changes if the instruction does not match.
9276 The difference between them is the size of the
9277 floating point constant and where it goes. For 'F'
9278 and 'L' the constant is 64 bits; for 'f' and 'l' it
9279 is 32 bits. Where the constant is placed is based
9280 on how the MIPS assembler does things:
9281 F -- .rdata
9282 L -- .lit8
9283 f -- immediate value
9284 l -- .lit4
9286 The .lit4 and .lit8 sections are only used if
9287 permitted by the -G argument.
9289 The code below needs to know whether the target register
9290 is 32 or 64 bits wide. It relies on the fact 'f' and
9291 'F' are used with GPR-based instructions and 'l' and
9292 'L' are used with FPR-based instructions. */
9294 f64 = *args == 'F' || *args == 'L';
9295 using_gprs = *args == 'F' || *args == 'f';
9297 save_in = input_line_pointer;
9298 input_line_pointer = s;
9299 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9300 length = len;
9301 s = input_line_pointer;
9302 input_line_pointer = save_in;
9303 if (err != NULL && *err != '\0')
9305 as_bad (_("Bad floating point constant: %s"), err);
9306 memset (temp, '\0', sizeof temp);
9307 length = f64 ? 8 : 4;
9310 assert (length == (unsigned) (f64 ? 8 : 4));
9312 if (*args == 'f'
9313 || (*args == 'l'
9314 && (g_switch_value < 4
9315 || (temp[0] == 0 && temp[1] == 0)
9316 || (temp[2] == 0 && temp[3] == 0))))
9318 imm_expr.X_op = O_constant;
9319 if (! target_big_endian)
9320 imm_expr.X_add_number = bfd_getl32 (temp);
9321 else
9322 imm_expr.X_add_number = bfd_getb32 (temp);
9324 else if (length > 4
9325 && ! mips_disable_float_construction
9326 /* Constants can only be constructed in GPRs and
9327 copied to FPRs if the GPRs are at least as wide
9328 as the FPRs. Force the constant into memory if
9329 we are using 64-bit FPRs but the GPRs are only
9330 32 bits wide. */
9331 && (using_gprs
9332 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
9333 && ((temp[0] == 0 && temp[1] == 0)
9334 || (temp[2] == 0 && temp[3] == 0))
9335 && ((temp[4] == 0 && temp[5] == 0)
9336 || (temp[6] == 0 && temp[7] == 0)))
9338 /* The value is simple enough to load with a couple of
9339 instructions. If using 32-bit registers, set
9340 imm_expr to the high order 32 bits and offset_expr to
9341 the low order 32 bits. Otherwise, set imm_expr to
9342 the entire 64 bit constant. */
9343 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
9345 imm_expr.X_op = O_constant;
9346 offset_expr.X_op = O_constant;
9347 if (! target_big_endian)
9349 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9350 offset_expr.X_add_number = bfd_getl32 (temp);
9352 else
9354 imm_expr.X_add_number = bfd_getb32 (temp);
9355 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9357 if (offset_expr.X_add_number == 0)
9358 offset_expr.X_op = O_absent;
9360 else if (sizeof (imm_expr.X_add_number) > 4)
9362 imm_expr.X_op = O_constant;
9363 if (! target_big_endian)
9364 imm_expr.X_add_number = bfd_getl64 (temp);
9365 else
9366 imm_expr.X_add_number = bfd_getb64 (temp);
9368 else
9370 imm_expr.X_op = O_big;
9371 imm_expr.X_add_number = 4;
9372 if (! target_big_endian)
9374 generic_bignum[0] = bfd_getl16 (temp);
9375 generic_bignum[1] = bfd_getl16 (temp + 2);
9376 generic_bignum[2] = bfd_getl16 (temp + 4);
9377 generic_bignum[3] = bfd_getl16 (temp + 6);
9379 else
9381 generic_bignum[0] = bfd_getb16 (temp + 6);
9382 generic_bignum[1] = bfd_getb16 (temp + 4);
9383 generic_bignum[2] = bfd_getb16 (temp + 2);
9384 generic_bignum[3] = bfd_getb16 (temp);
9388 else
9390 const char *newname;
9391 segT new_seg;
9393 /* Switch to the right section. */
9394 seg = now_seg;
9395 subseg = now_subseg;
9396 switch (*args)
9398 default: /* unused default case avoids warnings. */
9399 case 'L':
9400 newname = RDATA_SECTION_NAME;
9401 if (g_switch_value >= 8)
9402 newname = ".lit8";
9403 break;
9404 case 'F':
9405 newname = RDATA_SECTION_NAME;
9406 break;
9407 case 'l':
9408 assert (g_switch_value >= 4);
9409 newname = ".lit4";
9410 break;
9412 new_seg = subseg_new (newname, (subsegT) 0);
9413 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9414 bfd_set_section_flags (stdoutput, new_seg,
9415 (SEC_ALLOC
9416 | SEC_LOAD
9417 | SEC_READONLY
9418 | SEC_DATA));
9419 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9420 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9421 && strcmp (TARGET_OS, "elf") != 0)
9422 record_alignment (new_seg, 4);
9423 else
9424 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9425 if (seg == now_seg)
9426 as_bad (_("Can't use floating point insn in this section"));
9428 /* Set the argument to the current address in the
9429 section. */
9430 offset_expr.X_op = O_symbol;
9431 offset_expr.X_add_symbol =
9432 symbol_new ("L0\001", now_seg,
9433 (valueT) frag_now_fix (), frag_now);
9434 offset_expr.X_add_number = 0;
9436 /* Put the floating point number into the section. */
9437 p = frag_more ((int) length);
9438 memcpy (p, temp, length);
9440 /* Switch back to the original section. */
9441 subseg_set (seg, subseg);
9444 continue;
9446 case 'i': /* 16 bit unsigned immediate */
9447 case 'j': /* 16 bit signed immediate */
9448 *imm_reloc = BFD_RELOC_LO16;
9449 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9451 int more;
9452 offsetT minval, maxval;
9454 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9455 && strcmp (insn->name, insn[1].name) == 0);
9457 /* If the expression was written as an unsigned number,
9458 only treat it as signed if there are no more
9459 alternatives. */
9460 if (more
9461 && *args == 'j'
9462 && sizeof (imm_expr.X_add_number) <= 4
9463 && imm_expr.X_op == O_constant
9464 && imm_expr.X_add_number < 0
9465 && imm_expr.X_unsigned
9466 && HAVE_64BIT_GPRS)
9467 break;
9469 /* For compatibility with older assemblers, we accept
9470 0x8000-0xffff as signed 16-bit numbers when only
9471 signed numbers are allowed. */
9472 if (*args == 'i')
9473 minval = 0, maxval = 0xffff;
9474 else if (more)
9475 minval = -0x8000, maxval = 0x7fff;
9476 else
9477 minval = -0x8000, maxval = 0xffff;
9479 if (imm_expr.X_op != O_constant
9480 || imm_expr.X_add_number < minval
9481 || imm_expr.X_add_number > maxval)
9483 if (more)
9484 break;
9485 if (imm_expr.X_op == O_constant
9486 || imm_expr.X_op == O_big)
9487 as_bad (_("expression out of range"));
9490 s = expr_end;
9491 continue;
9493 case 'o': /* 16 bit offset */
9494 /* Check whether there is only a single bracketed expression
9495 left. If so, it must be the base register and the
9496 constant must be zero. */
9497 if (*s == '(' && strchr (s + 1, '(') == 0)
9499 offset_expr.X_op = O_constant;
9500 offset_expr.X_add_number = 0;
9501 continue;
9504 /* If this value won't fit into a 16 bit offset, then go
9505 find a macro that will generate the 32 bit offset
9506 code pattern. */
9507 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9508 && (offset_expr.X_op != O_constant
9509 || offset_expr.X_add_number >= 0x8000
9510 || offset_expr.X_add_number < -0x8000))
9511 break;
9513 s = expr_end;
9514 continue;
9516 case 'p': /* pc relative offset */
9517 *offset_reloc = BFD_RELOC_16_PCREL_S2;
9518 my_getExpression (&offset_expr, s);
9519 s = expr_end;
9520 continue;
9522 case 'u': /* upper 16 bits */
9523 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9524 && imm_expr.X_op == O_constant
9525 && (imm_expr.X_add_number < 0
9526 || imm_expr.X_add_number >= 0x10000))
9527 as_bad (_("lui expression not in range 0..65535"));
9528 s = expr_end;
9529 continue;
9531 case 'a': /* 26 bit address */
9532 my_getExpression (&offset_expr, s);
9533 s = expr_end;
9534 *offset_reloc = BFD_RELOC_MIPS_JMP;
9535 continue;
9537 case 'N': /* 3 bit branch condition code */
9538 case 'M': /* 3 bit compare condition code */
9539 rtype = RTYPE_CCC;
9540 if (ip->insn_mo->pinfo & (FP_D| FP_S))
9541 rtype |= RTYPE_FCC;
9542 if (!reg_lookup (&s, rtype, &regno))
9543 break;
9544 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9545 || strcmp(str + strlen(str) - 5, "any2f") == 0
9546 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9547 && (regno & 1) != 0)
9548 as_warn(_("Condition code register should be even for %s, was %d"),
9549 str, regno);
9550 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9551 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9552 && (regno & 3) != 0)
9553 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9554 str, regno);
9555 if (*args == 'N')
9556 INSERT_OPERAND (BCC, *ip, regno);
9557 else
9558 INSERT_OPERAND (CCC, *ip, regno);
9559 continue;
9561 case 'H':
9562 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9563 s += 2;
9564 if (ISDIGIT (*s))
9566 c = 0;
9569 c *= 10;
9570 c += *s - '0';
9571 ++s;
9573 while (ISDIGIT (*s));
9575 else
9576 c = 8; /* Invalid sel value. */
9578 if (c > 7)
9579 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9580 ip->insn_opcode |= c;
9581 continue;
9583 case 'e':
9584 /* Must be at least one digit. */
9585 my_getExpression (&imm_expr, s);
9586 check_absolute_expr (ip, &imm_expr);
9588 if ((unsigned long) imm_expr.X_add_number
9589 > (unsigned long) OP_MASK_VECBYTE)
9591 as_bad (_("bad byte vector index (%ld)"),
9592 (long) imm_expr.X_add_number);
9593 imm_expr.X_add_number = 0;
9596 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
9597 imm_expr.X_op = O_absent;
9598 s = expr_end;
9599 continue;
9601 case '%':
9602 my_getExpression (&imm_expr, s);
9603 check_absolute_expr (ip, &imm_expr);
9605 if ((unsigned long) imm_expr.X_add_number
9606 > (unsigned long) OP_MASK_VECALIGN)
9608 as_bad (_("bad byte vector index (%ld)"),
9609 (long) imm_expr.X_add_number);
9610 imm_expr.X_add_number = 0;
9613 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
9614 imm_expr.X_op = O_absent;
9615 s = expr_end;
9616 continue;
9618 default:
9619 as_bad (_("bad char = '%c'\n"), *args);
9620 internalError ();
9622 break;
9624 /* Args don't match. */
9625 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9626 !strcmp (insn->name, insn[1].name))
9628 ++insn;
9629 s = argsStart;
9630 insn_error = _("illegal operands");
9631 continue;
9633 if (save_c)
9634 *(--s) = save_c;
9635 insn_error = _("illegal operands");
9636 return;
9640 #define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
9642 /* This routine assembles an instruction into its binary format when
9643 assembling for the mips16. As a side effect, it sets one of the
9644 global variables imm_reloc or offset_reloc to the type of
9645 relocation to do if one of the operands is an address expression.
9646 It also sets mips16_small and mips16_ext if the user explicitly
9647 requested a small or extended instruction. */
9649 static void
9650 mips16_ip (char *str, struct mips_cl_insn *ip)
9652 char *s;
9653 const char *args;
9654 struct mips_opcode *insn;
9655 char *argsstart;
9656 unsigned int regno;
9657 unsigned int lastregno = 0;
9658 char *s_reset;
9659 size_t i;
9661 insn_error = NULL;
9663 mips16_small = FALSE;
9664 mips16_ext = FALSE;
9666 for (s = str; ISLOWER (*s); ++s)
9668 switch (*s)
9670 case '\0':
9671 break;
9673 case ' ':
9674 *s++ = '\0';
9675 break;
9677 case '.':
9678 if (s[1] == 't' && s[2] == ' ')
9680 *s = '\0';
9681 mips16_small = TRUE;
9682 s += 3;
9683 break;
9685 else if (s[1] == 'e' && s[2] == ' ')
9687 *s = '\0';
9688 mips16_ext = TRUE;
9689 s += 3;
9690 break;
9692 /* Fall through. */
9693 default:
9694 insn_error = _("unknown opcode");
9695 return;
9698 if (mips_opts.noautoextend && ! mips16_ext)
9699 mips16_small = TRUE;
9701 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9703 insn_error = _("unrecognized opcode");
9704 return;
9707 argsstart = s;
9708 for (;;)
9710 bfd_boolean ok;
9712 assert (strcmp (insn->name, str) == 0);
9714 if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_opts.arch))
9715 ok = TRUE;
9716 else
9717 ok = FALSE;
9719 if (! ok)
9721 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
9722 && strcmp (insn->name, insn[1].name) == 0)
9724 ++insn;
9725 continue;
9727 else
9729 if (!insn_error)
9731 static char buf[100];
9732 sprintf (buf,
9733 _("opcode not supported on this processor: %s (%s)"),
9734 mips_cpu_info_from_arch (mips_opts.arch)->name,
9735 mips_cpu_info_from_isa (mips_opts.isa)->name);
9736 insn_error = buf;
9738 return;
9742 create_insn (ip, insn);
9743 imm_expr.X_op = O_absent;
9744 imm_reloc[0] = BFD_RELOC_UNUSED;
9745 imm_reloc[1] = BFD_RELOC_UNUSED;
9746 imm_reloc[2] = BFD_RELOC_UNUSED;
9747 imm2_expr.X_op = O_absent;
9748 offset_expr.X_op = O_absent;
9749 offset_reloc[0] = BFD_RELOC_UNUSED;
9750 offset_reloc[1] = BFD_RELOC_UNUSED;
9751 offset_reloc[2] = BFD_RELOC_UNUSED;
9752 for (args = insn->args; 1; ++args)
9754 int c;
9756 if (*s == ' ')
9757 ++s;
9759 /* In this switch statement we call break if we did not find
9760 a match, continue if we did find a match, or return if we
9761 are done. */
9763 c = *args;
9764 switch (c)
9766 case '\0':
9767 if (*s == '\0')
9769 /* Stuff the immediate value in now, if we can. */
9770 if (imm_expr.X_op == O_constant
9771 && *imm_reloc > BFD_RELOC_UNUSED
9772 && insn->pinfo != INSN_MACRO)
9774 valueT tmp;
9776 switch (*offset_reloc)
9778 case BFD_RELOC_MIPS16_HI16_S:
9779 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9780 break;
9782 case BFD_RELOC_MIPS16_HI16:
9783 tmp = imm_expr.X_add_number >> 16;
9784 break;
9786 case BFD_RELOC_MIPS16_LO16:
9787 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9788 - 0x8000;
9789 break;
9791 case BFD_RELOC_UNUSED:
9792 tmp = imm_expr.X_add_number;
9793 break;
9795 default:
9796 internalError ();
9798 *offset_reloc = BFD_RELOC_UNUSED;
9800 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9801 tmp, TRUE, mips16_small,
9802 mips16_ext, &ip->insn_opcode,
9803 &ip->use_extend, &ip->extend);
9804 imm_expr.X_op = O_absent;
9805 *imm_reloc = BFD_RELOC_UNUSED;
9808 return;
9810 break;
9812 case ',':
9813 if (*s++ == c)
9814 continue;
9815 s--;
9816 switch (*++args)
9818 case 'v':
9819 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9820 continue;
9821 case 'w':
9822 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9823 continue;
9825 break;
9827 case '(':
9828 case ')':
9829 if (*s++ == c)
9830 continue;
9831 break;
9833 case 'v':
9834 case 'w':
9835 if (s[0] != '$')
9837 if (c == 'v')
9838 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9839 else
9840 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9841 ++args;
9842 continue;
9844 /* Fall through. */
9845 case 'x':
9846 case 'y':
9847 case 'z':
9848 case 'Z':
9849 case '0':
9850 case 'S':
9851 case 'R':
9852 case 'X':
9853 case 'Y':
9854 s_reset = s;
9855 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
9857 if (c == 'v' || c == 'w')
9859 if (c == 'v')
9860 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9861 else
9862 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9863 ++args;
9864 continue;
9866 break;
9869 if (*s == ' ')
9870 ++s;
9871 if (args[1] != *s)
9873 if (c == 'v' || c == 'w')
9875 regno = mips16_to_32_reg_map[lastregno];
9876 s = s_reset;
9877 ++args;
9881 switch (c)
9883 case 'x':
9884 case 'y':
9885 case 'z':
9886 case 'v':
9887 case 'w':
9888 case 'Z':
9889 regno = mips32_to_16_reg_map[regno];
9890 break;
9892 case '0':
9893 if (regno != 0)
9894 regno = ILLEGAL_REG;
9895 break;
9897 case 'S':
9898 if (regno != SP)
9899 regno = ILLEGAL_REG;
9900 break;
9902 case 'R':
9903 if (regno != RA)
9904 regno = ILLEGAL_REG;
9905 break;
9907 case 'X':
9908 case 'Y':
9909 if (regno == AT && ! mips_opts.noat)
9910 as_warn (_("used $at without \".set noat\""));
9911 break;
9913 default:
9914 internalError ();
9917 if (regno == ILLEGAL_REG)
9918 break;
9920 switch (c)
9922 case 'x':
9923 case 'v':
9924 MIPS16_INSERT_OPERAND (RX, *ip, regno);
9925 break;
9926 case 'y':
9927 case 'w':
9928 MIPS16_INSERT_OPERAND (RY, *ip, regno);
9929 break;
9930 case 'z':
9931 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
9932 break;
9933 case 'Z':
9934 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
9935 case '0':
9936 case 'S':
9937 case 'R':
9938 break;
9939 case 'X':
9940 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
9941 break;
9942 case 'Y':
9943 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9944 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
9945 break;
9946 default:
9947 internalError ();
9950 lastregno = regno;
9951 continue;
9953 case 'P':
9954 if (strncmp (s, "$pc", 3) == 0)
9956 s += 3;
9957 continue;
9959 break;
9961 case '5':
9962 case 'H':
9963 case 'W':
9964 case 'D':
9965 case 'j':
9966 case 'V':
9967 case 'C':
9968 case 'U':
9969 case 'k':
9970 case 'K':
9971 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9972 if (i > 0)
9974 if (imm_expr.X_op != O_constant)
9976 mips16_ext = TRUE;
9977 ip->use_extend = TRUE;
9978 ip->extend = 0;
9980 else
9982 /* We need to relax this instruction. */
9983 *offset_reloc = *imm_reloc;
9984 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9986 s = expr_end;
9987 continue;
9989 *imm_reloc = BFD_RELOC_UNUSED;
9990 /* Fall through. */
9991 case '<':
9992 case '>':
9993 case '[':
9994 case ']':
9995 case '4':
9996 case '8':
9997 my_getExpression (&imm_expr, s);
9998 if (imm_expr.X_op == O_register)
10000 /* What we thought was an expression turned out to
10001 be a register. */
10003 if (s[0] == '(' && args[1] == '(')
10005 /* It looks like the expression was omitted
10006 before a register indirection, which means
10007 that the expression is implicitly zero. We
10008 still set up imm_expr, so that we handle
10009 explicit extensions correctly. */
10010 imm_expr.X_op = O_constant;
10011 imm_expr.X_add_number = 0;
10012 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10013 continue;
10016 break;
10019 /* We need to relax this instruction. */
10020 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10021 s = expr_end;
10022 continue;
10024 case 'p':
10025 case 'q':
10026 case 'A':
10027 case 'B':
10028 case 'E':
10029 /* We use offset_reloc rather than imm_reloc for the PC
10030 relative operands. This lets macros with both
10031 immediate and address operands work correctly. */
10032 my_getExpression (&offset_expr, s);
10034 if (offset_expr.X_op == O_register)
10035 break;
10037 /* We need to relax this instruction. */
10038 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
10039 s = expr_end;
10040 continue;
10042 case '6': /* break code */
10043 my_getExpression (&imm_expr, s);
10044 check_absolute_expr (ip, &imm_expr);
10045 if ((unsigned long) imm_expr.X_add_number > 63)
10046 as_warn (_("Invalid value for `%s' (%lu)"),
10047 ip->insn_mo->name,
10048 (unsigned long) imm_expr.X_add_number);
10049 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
10050 imm_expr.X_op = O_absent;
10051 s = expr_end;
10052 continue;
10054 case 'a': /* 26 bit address */
10055 my_getExpression (&offset_expr, s);
10056 s = expr_end;
10057 *offset_reloc = BFD_RELOC_MIPS16_JMP;
10058 ip->insn_opcode <<= 16;
10059 continue;
10061 case 'l': /* register list for entry macro */
10062 case 'L': /* register list for exit macro */
10064 int mask;
10066 if (c == 'l')
10067 mask = 0;
10068 else
10069 mask = 7 << 3;
10070 while (*s != '\0')
10072 unsigned int freg, reg1, reg2;
10074 while (*s == ' ' || *s == ',')
10075 ++s;
10076 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
10077 freg = 0;
10078 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
10079 freg = 1;
10080 else
10082 as_bad (_("can't parse register list"));
10083 break;
10085 if (*s == ' ')
10086 ++s;
10087 if (*s != '-')
10088 reg2 = reg1;
10089 else
10091 ++s;
10092 if (!reg_lookup (&s, freg ? RTYPE_FPU
10093 : (RTYPE_GP | RTYPE_NUM), &reg2))
10095 as_bad (_("invalid register list"));
10096 break;
10099 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
10101 mask &= ~ (7 << 3);
10102 mask |= 5 << 3;
10104 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
10106 mask &= ~ (7 << 3);
10107 mask |= 6 << 3;
10109 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
10110 mask |= (reg2 - 3) << 3;
10111 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
10112 mask |= (reg2 - 15) << 1;
10113 else if (reg1 == RA && reg2 == RA)
10114 mask |= 1;
10115 else
10117 as_bad (_("invalid register list"));
10118 break;
10121 /* The mask is filled in in the opcode table for the
10122 benefit of the disassembler. We remove it before
10123 applying the actual mask. */
10124 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10125 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10127 continue;
10129 case 'm': /* Register list for save insn. */
10130 case 'M': /* Register list for restore insn. */
10132 int opcode = 0;
10133 int framesz = 0, seen_framesz = 0;
10134 int args = 0, statics = 0, sregs = 0;
10136 while (*s != '\0')
10138 unsigned int reg1, reg2;
10140 SKIP_SPACE_TABS (s);
10141 while (*s == ',')
10142 ++s;
10143 SKIP_SPACE_TABS (s);
10145 my_getExpression (&imm_expr, s);
10146 if (imm_expr.X_op == O_constant)
10148 /* Handle the frame size. */
10149 if (seen_framesz)
10151 as_bad (_("more than one frame size in list"));
10152 break;
10154 seen_framesz = 1;
10155 framesz = imm_expr.X_add_number;
10156 imm_expr.X_op = O_absent;
10157 s = expr_end;
10158 continue;
10161 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
10163 as_bad (_("can't parse register list"));
10164 break;
10167 while (*s == ' ')
10168 ++s;
10170 if (*s != '-')
10171 reg2 = reg1;
10172 else
10174 ++s;
10175 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
10176 || reg2 < reg1)
10178 as_bad (_("can't parse register list"));
10179 break;
10183 while (reg1 <= reg2)
10185 if (reg1 >= 4 && reg1 <= 7)
10187 if (c == 'm' && !seen_framesz)
10188 /* args $a0-$a3 */
10189 args |= 1 << (reg1 - 4);
10190 else
10191 /* statics $a0-$a3 */
10192 statics |= 1 << (reg1 - 4);
10194 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
10196 /* $s0-$s8 */
10197 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
10199 else if (reg1 == 31)
10201 /* Add $ra to insn. */
10202 opcode |= 0x40;
10204 else
10206 as_bad (_("unexpected register in list"));
10207 break;
10209 if (++reg1 == 24)
10210 reg1 = 30;
10214 /* Encode args/statics combination. */
10215 if (args & statics)
10216 as_bad (_("arg/static registers overlap"));
10217 else if (args == 0xf)
10218 /* All $a0-$a3 are args. */
10219 opcode |= MIPS16_ALL_ARGS << 16;
10220 else if (statics == 0xf)
10221 /* All $a0-$a3 are statics. */
10222 opcode |= MIPS16_ALL_STATICS << 16;
10223 else
10225 int narg = 0, nstat = 0;
10227 /* Count arg registers. */
10228 while (args & 0x1)
10230 args >>= 1;
10231 narg++;
10233 if (args != 0)
10234 as_bad (_("invalid arg register list"));
10236 /* Count static registers. */
10237 while (statics & 0x8)
10239 statics = (statics << 1) & 0xf;
10240 nstat++;
10242 if (statics != 0)
10243 as_bad (_("invalid static register list"));
10245 /* Encode args/statics. */
10246 opcode |= ((narg << 2) | nstat) << 16;
10249 /* Encode $s0/$s1. */
10250 if (sregs & (1 << 0)) /* $s0 */
10251 opcode |= 0x20;
10252 if (sregs & (1 << 1)) /* $s1 */
10253 opcode |= 0x10;
10254 sregs >>= 2;
10256 if (sregs != 0)
10258 /* Count regs $s2-$s8. */
10259 int nsreg = 0;
10260 while (sregs & 1)
10262 sregs >>= 1;
10263 nsreg++;
10265 if (sregs != 0)
10266 as_bad (_("invalid static register list"));
10267 /* Encode $s2-$s8. */
10268 opcode |= nsreg << 24;
10271 /* Encode frame size. */
10272 if (!seen_framesz)
10273 as_bad (_("missing frame size"));
10274 else if ((framesz & 7) != 0 || framesz < 0
10275 || framesz > 0xff * 8)
10276 as_bad (_("invalid frame size"));
10277 else if (framesz != 128 || (opcode >> 16) != 0)
10279 framesz /= 8;
10280 opcode |= (((framesz & 0xf0) << 16)
10281 | (framesz & 0x0f));
10284 /* Finally build the instruction. */
10285 if ((opcode >> 16) != 0 || framesz == 0)
10287 ip->use_extend = TRUE;
10288 ip->extend = opcode >> 16;
10290 ip->insn_opcode |= opcode & 0x7f;
10292 continue;
10294 case 'e': /* extend code */
10295 my_getExpression (&imm_expr, s);
10296 check_absolute_expr (ip, &imm_expr);
10297 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10299 as_warn (_("Invalid value for `%s' (%lu)"),
10300 ip->insn_mo->name,
10301 (unsigned long) imm_expr.X_add_number);
10302 imm_expr.X_add_number &= 0x7ff;
10304 ip->insn_opcode |= imm_expr.X_add_number;
10305 imm_expr.X_op = O_absent;
10306 s = expr_end;
10307 continue;
10309 default:
10310 internalError ();
10312 break;
10315 /* Args don't match. */
10316 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10317 strcmp (insn->name, insn[1].name) == 0)
10319 ++insn;
10320 s = argsstart;
10321 continue;
10324 insn_error = _("illegal operands");
10326 return;
10330 /* This structure holds information we know about a mips16 immediate
10331 argument type. */
10333 struct mips16_immed_operand
10335 /* The type code used in the argument string in the opcode table. */
10336 int type;
10337 /* The number of bits in the short form of the opcode. */
10338 int nbits;
10339 /* The number of bits in the extended form of the opcode. */
10340 int extbits;
10341 /* The amount by which the short form is shifted when it is used;
10342 for example, the sw instruction has a shift count of 2. */
10343 int shift;
10344 /* The amount by which the short form is shifted when it is stored
10345 into the instruction code. */
10346 int op_shift;
10347 /* Non-zero if the short form is unsigned. */
10348 int unsp;
10349 /* Non-zero if the extended form is unsigned. */
10350 int extu;
10351 /* Non-zero if the value is PC relative. */
10352 int pcrel;
10355 /* The mips16 immediate operand types. */
10357 static const struct mips16_immed_operand mips16_immed_operands[] =
10359 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10360 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10361 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10362 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10363 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10364 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10365 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10366 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10367 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10368 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10369 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10370 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10371 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10372 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10373 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10374 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10375 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10376 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10377 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10378 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10379 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10382 #define MIPS16_NUM_IMMED \
10383 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10385 /* Handle a mips16 instruction with an immediate value. This or's the
10386 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10387 whether an extended value is needed; if one is needed, it sets
10388 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10389 If SMALL is true, an unextended opcode was explicitly requested.
10390 If EXT is true, an extended opcode was explicitly requested. If
10391 WARN is true, warn if EXT does not match reality. */
10393 static void
10394 mips16_immed (char *file, unsigned int line, int type, offsetT val,
10395 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10396 unsigned long *insn, bfd_boolean *use_extend,
10397 unsigned short *extend)
10399 register const struct mips16_immed_operand *op;
10400 int mintiny, maxtiny;
10401 bfd_boolean needext;
10403 op = mips16_immed_operands;
10404 while (op->type != type)
10406 ++op;
10407 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10410 if (op->unsp)
10412 if (type == '<' || type == '>' || type == '[' || type == ']')
10414 mintiny = 1;
10415 maxtiny = 1 << op->nbits;
10417 else
10419 mintiny = 0;
10420 maxtiny = (1 << op->nbits) - 1;
10423 else
10425 mintiny = - (1 << (op->nbits - 1));
10426 maxtiny = (1 << (op->nbits - 1)) - 1;
10429 /* Branch offsets have an implicit 0 in the lowest bit. */
10430 if (type == 'p' || type == 'q')
10431 val /= 2;
10433 if ((val & ((1 << op->shift) - 1)) != 0
10434 || val < (mintiny << op->shift)
10435 || val > (maxtiny << op->shift))
10436 needext = TRUE;
10437 else
10438 needext = FALSE;
10440 if (warn && ext && ! needext)
10441 as_warn_where (file, line,
10442 _("extended operand requested but not required"));
10443 if (small && needext)
10444 as_bad_where (file, line, _("invalid unextended operand value"));
10446 if (small || (! ext && ! needext))
10448 int insnval;
10450 *use_extend = FALSE;
10451 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10452 insnval <<= op->op_shift;
10453 *insn |= insnval;
10455 else
10457 long minext, maxext;
10458 int extval;
10460 if (op->extu)
10462 minext = 0;
10463 maxext = (1 << op->extbits) - 1;
10465 else
10467 minext = - (1 << (op->extbits - 1));
10468 maxext = (1 << (op->extbits - 1)) - 1;
10470 if (val < minext || val > maxext)
10471 as_bad_where (file, line,
10472 _("operand value out of range for instruction"));
10474 *use_extend = TRUE;
10475 if (op->extbits == 16)
10477 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10478 val &= 0x1f;
10480 else if (op->extbits == 15)
10482 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10483 val &= 0xf;
10485 else
10487 extval = ((val & 0x1f) << 6) | (val & 0x20);
10488 val = 0;
10491 *extend = (unsigned short) extval;
10492 *insn |= val;
10496 struct percent_op_match
10498 const char *str;
10499 bfd_reloc_code_real_type reloc;
10502 static const struct percent_op_match mips_percent_op[] =
10504 {"%lo", BFD_RELOC_LO16},
10505 #ifdef OBJ_ELF
10506 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10507 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10508 {"%call16", BFD_RELOC_MIPS_CALL16},
10509 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10510 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10511 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10512 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10513 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10514 {"%got", BFD_RELOC_MIPS_GOT16},
10515 {"%gp_rel", BFD_RELOC_GPREL16},
10516 {"%half", BFD_RELOC_16},
10517 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10518 {"%higher", BFD_RELOC_MIPS_HIGHER},
10519 {"%neg", BFD_RELOC_MIPS_SUB},
10520 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
10521 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
10522 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
10523 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
10524 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
10525 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
10526 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
10527 #endif
10528 {"%hi", BFD_RELOC_HI16_S}
10531 static const struct percent_op_match mips16_percent_op[] =
10533 {"%lo", BFD_RELOC_MIPS16_LO16},
10534 {"%gprel", BFD_RELOC_MIPS16_GPREL},
10535 {"%hi", BFD_RELOC_MIPS16_HI16_S}
10539 /* Return true if *STR points to a relocation operator. When returning true,
10540 move *STR over the operator and store its relocation code in *RELOC.
10541 Leave both *STR and *RELOC alone when returning false. */
10543 static bfd_boolean
10544 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
10546 const struct percent_op_match *percent_op;
10547 size_t limit, i;
10549 if (mips_opts.mips16)
10551 percent_op = mips16_percent_op;
10552 limit = ARRAY_SIZE (mips16_percent_op);
10554 else
10556 percent_op = mips_percent_op;
10557 limit = ARRAY_SIZE (mips_percent_op);
10560 for (i = 0; i < limit; i++)
10561 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10563 int len = strlen (percent_op[i].str);
10565 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
10566 continue;
10568 *str += strlen (percent_op[i].str);
10569 *reloc = percent_op[i].reloc;
10571 /* Check whether the output BFD supports this relocation.
10572 If not, issue an error and fall back on something safe. */
10573 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10575 as_bad ("relocation %s isn't supported by the current ABI",
10576 percent_op[i].str);
10577 *reloc = BFD_RELOC_UNUSED;
10579 return TRUE;
10581 return FALSE;
10585 /* Parse string STR as a 16-bit relocatable operand. Store the
10586 expression in *EP and the relocations in the array starting
10587 at RELOC. Return the number of relocation operators used.
10589 On exit, EXPR_END points to the first character after the expression. */
10591 static size_t
10592 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10593 char *str)
10595 bfd_reloc_code_real_type reversed_reloc[3];
10596 size_t reloc_index, i;
10597 int crux_depth, str_depth;
10598 char *crux;
10600 /* Search for the start of the main expression, recoding relocations
10601 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10602 of the main expression and with CRUX_DEPTH containing the number
10603 of open brackets at that point. */
10604 reloc_index = -1;
10605 str_depth = 0;
10608 reloc_index++;
10609 crux = str;
10610 crux_depth = str_depth;
10612 /* Skip over whitespace and brackets, keeping count of the number
10613 of brackets. */
10614 while (*str == ' ' || *str == '\t' || *str == '(')
10615 if (*str++ == '(')
10616 str_depth++;
10618 while (*str == '%'
10619 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10620 && parse_relocation (&str, &reversed_reloc[reloc_index]));
10622 my_getExpression (ep, crux);
10623 str = expr_end;
10625 /* Match every open bracket. */
10626 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10627 if (*str++ == ')')
10628 crux_depth--;
10630 if (crux_depth > 0)
10631 as_bad ("unclosed '('");
10633 expr_end = str;
10635 if (reloc_index != 0)
10637 prev_reloc_op_frag = frag_now;
10638 for (i = 0; i < reloc_index; i++)
10639 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10642 return reloc_index;
10645 static void
10646 my_getExpression (expressionS *ep, char *str)
10648 char *save_in;
10649 valueT val;
10651 save_in = input_line_pointer;
10652 input_line_pointer = str;
10653 expression (ep);
10654 expr_end = input_line_pointer;
10655 input_line_pointer = save_in;
10657 /* If we are in mips16 mode, and this is an expression based on `.',
10658 then we bump the value of the symbol by 1 since that is how other
10659 text symbols are handled. We don't bother to handle complex
10660 expressions, just `.' plus or minus a constant. */
10661 if (mips_opts.mips16
10662 && ep->X_op == O_symbol
10663 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10664 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10665 && symbol_get_frag (ep->X_add_symbol) == frag_now
10666 && symbol_constant_p (ep->X_add_symbol)
10667 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10668 S_SET_VALUE (ep->X_add_symbol, val + 1);
10671 /* Turn a string in input_line_pointer into a floating point constant
10672 of type TYPE, and store the appropriate bytes in *LITP. The number
10673 of LITTLENUMS emitted is stored in *SIZEP. An error message is
10674 returned, or NULL on OK. */
10676 char *
10677 md_atof (int type, char *litP, int *sizeP)
10679 int prec;
10680 LITTLENUM_TYPE words[4];
10681 char *t;
10682 int i;
10684 switch (type)
10686 case 'f':
10687 prec = 2;
10688 break;
10690 case 'd':
10691 prec = 4;
10692 break;
10694 default:
10695 *sizeP = 0;
10696 return _("bad call to md_atof");
10699 t = atof_ieee (input_line_pointer, type, words);
10700 if (t)
10701 input_line_pointer = t;
10703 *sizeP = prec * 2;
10705 if (! target_big_endian)
10707 for (i = prec - 1; i >= 0; i--)
10709 md_number_to_chars (litP, words[i], 2);
10710 litP += 2;
10713 else
10715 for (i = 0; i < prec; i++)
10717 md_number_to_chars (litP, words[i], 2);
10718 litP += 2;
10722 return NULL;
10725 void
10726 md_number_to_chars (char *buf, valueT val, int n)
10728 if (target_big_endian)
10729 number_to_chars_bigendian (buf, val, n);
10730 else
10731 number_to_chars_littleendian (buf, val, n);
10734 #ifdef OBJ_ELF
10735 static int support_64bit_objects(void)
10737 const char **list, **l;
10738 int yes;
10740 list = bfd_target_list ();
10741 for (l = list; *l != NULL; l++)
10742 #ifdef TE_TMIPS
10743 /* This is traditional mips */
10744 if (strcmp (*l, "elf64-tradbigmips") == 0
10745 || strcmp (*l, "elf64-tradlittlemips") == 0)
10746 #else
10747 if (strcmp (*l, "elf64-bigmips") == 0
10748 || strcmp (*l, "elf64-littlemips") == 0)
10749 #endif
10750 break;
10751 yes = (*l != NULL);
10752 free (list);
10753 return yes;
10755 #endif /* OBJ_ELF */
10757 const char *md_shortopts = "O::g::G:";
10759 struct option md_longopts[] =
10761 /* Options which specify architecture. */
10762 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
10763 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10764 {"march", required_argument, NULL, OPTION_MARCH},
10765 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10766 {"mtune", required_argument, NULL, OPTION_MTUNE},
10767 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10768 {"mips0", no_argument, NULL, OPTION_MIPS1},
10769 {"mips1", no_argument, NULL, OPTION_MIPS1},
10770 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10771 {"mips2", no_argument, NULL, OPTION_MIPS2},
10772 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10773 {"mips3", no_argument, NULL, OPTION_MIPS3},
10774 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10775 {"mips4", no_argument, NULL, OPTION_MIPS4},
10776 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10777 {"mips5", no_argument, NULL, OPTION_MIPS5},
10778 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10779 {"mips32", no_argument, NULL, OPTION_MIPS32},
10780 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10781 {"mips64", no_argument, NULL, OPTION_MIPS64},
10782 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10783 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10784 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10785 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
10787 /* Options which specify Application Specific Extensions (ASEs). */
10788 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
10789 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10790 {"mips16", no_argument, NULL, OPTION_MIPS16},
10791 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10792 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10793 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10794 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10795 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10796 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10797 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10798 {"mdmx", no_argument, NULL, OPTION_MDMX},
10799 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10800 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10801 #define OPTION_DSP (OPTION_ASE_BASE + 6)
10802 {"mdsp", no_argument, NULL, OPTION_DSP},
10803 #define OPTION_NO_DSP (OPTION_ASE_BASE + 7)
10804 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
10805 #define OPTION_MT (OPTION_ASE_BASE + 8)
10806 {"mmt", no_argument, NULL, OPTION_MT},
10807 #define OPTION_NO_MT (OPTION_ASE_BASE + 9)
10808 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
10809 #define OPTION_SMARTMIPS (OPTION_ASE_BASE + 10)
10810 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
10811 #define OPTION_NO_SMARTMIPS (OPTION_ASE_BASE + 11)
10812 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
10814 /* Old-style architecture options. Don't add more of these. */
10815 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 12)
10816 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10817 {"m4650", no_argument, NULL, OPTION_M4650},
10818 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10819 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10820 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10821 {"m4010", no_argument, NULL, OPTION_M4010},
10822 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10823 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10824 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10825 {"m4100", no_argument, NULL, OPTION_M4100},
10826 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10827 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10828 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10829 {"m3900", no_argument, NULL, OPTION_M3900},
10830 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10831 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10833 /* Options which enable bug fixes. */
10834 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10835 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10836 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10837 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10838 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10839 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10840 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10841 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10842 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10843 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
10844 #define OPTION_FIX_VR4130 (OPTION_FIX_BASE + 4)
10845 #define OPTION_NO_FIX_VR4130 (OPTION_FIX_BASE + 5)
10846 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
10847 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
10849 /* Miscellaneous options. */
10850 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 6)
10851 #define OPTION_TRAP (OPTION_MISC_BASE + 0)
10852 {"trap", no_argument, NULL, OPTION_TRAP},
10853 {"no-break", no_argument, NULL, OPTION_TRAP},
10854 #define OPTION_BREAK (OPTION_MISC_BASE + 1)
10855 {"break", no_argument, NULL, OPTION_BREAK},
10856 {"no-trap", no_argument, NULL, OPTION_BREAK},
10857 #define OPTION_EB (OPTION_MISC_BASE + 2)
10858 {"EB", no_argument, NULL, OPTION_EB},
10859 #define OPTION_EL (OPTION_MISC_BASE + 3)
10860 {"EL", no_argument, NULL, OPTION_EL},
10861 #define OPTION_FP32 (OPTION_MISC_BASE + 4)
10862 {"mfp32", no_argument, NULL, OPTION_FP32},
10863 #define OPTION_GP32 (OPTION_MISC_BASE + 5)
10864 {"mgp32", no_argument, NULL, OPTION_GP32},
10865 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
10866 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10867 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10868 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10869 #define OPTION_FP64 (OPTION_MISC_BASE + 8)
10870 {"mfp64", no_argument, NULL, OPTION_FP64},
10871 #define OPTION_GP64 (OPTION_MISC_BASE + 9)
10872 {"mgp64", no_argument, NULL, OPTION_GP64},
10873 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10874 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10875 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10876 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10877 #define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10878 #define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10879 {"mshared", no_argument, NULL, OPTION_MSHARED},
10880 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
10881 #define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10882 #define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10883 {"msym32", no_argument, NULL, OPTION_MSYM32},
10884 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
10886 /* ELF-specific options. */
10887 #ifdef OBJ_ELF
10888 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
10889 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10890 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10891 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10892 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10893 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10894 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10895 {"xgot", no_argument, NULL, OPTION_XGOT},
10896 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10897 {"mabi", required_argument, NULL, OPTION_MABI},
10898 #define OPTION_32 (OPTION_ELF_BASE + 4)
10899 {"32", no_argument, NULL, OPTION_32},
10900 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10901 {"n32", no_argument, NULL, OPTION_N32},
10902 #define OPTION_64 (OPTION_ELF_BASE + 6)
10903 {"64", no_argument, NULL, OPTION_64},
10904 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10905 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10906 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10907 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10908 #define OPTION_PDR (OPTION_ELF_BASE + 9)
10909 {"mpdr", no_argument, NULL, OPTION_PDR},
10910 #define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10911 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10912 #define OPTION_MVXWORKS_PIC (OPTION_ELF_BASE + 11)
10913 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
10914 #endif /* OBJ_ELF */
10916 {NULL, no_argument, NULL, 0}
10918 size_t md_longopts_size = sizeof (md_longopts);
10920 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10921 NEW_VALUE. Warn if another value was already specified. Note:
10922 we have to defer parsing the -march and -mtune arguments in order
10923 to handle 'from-abi' correctly, since the ABI might be specified
10924 in a later argument. */
10926 static void
10927 mips_set_option_string (const char **string_ptr, const char *new_value)
10929 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10930 as_warn (_("A different %s was already specified, is now %s"),
10931 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10932 new_value);
10934 *string_ptr = new_value;
10938 md_parse_option (int c, char *arg)
10940 switch (c)
10942 case OPTION_CONSTRUCT_FLOATS:
10943 mips_disable_float_construction = 0;
10944 break;
10946 case OPTION_NO_CONSTRUCT_FLOATS:
10947 mips_disable_float_construction = 1;
10948 break;
10950 case OPTION_TRAP:
10951 mips_trap = 1;
10952 break;
10954 case OPTION_BREAK:
10955 mips_trap = 0;
10956 break;
10958 case OPTION_EB:
10959 target_big_endian = 1;
10960 break;
10962 case OPTION_EL:
10963 target_big_endian = 0;
10964 break;
10966 case 'O':
10967 if (arg && arg[1] == '0')
10968 mips_optimize = 1;
10969 else
10970 mips_optimize = 2;
10971 break;
10973 case 'g':
10974 if (arg == NULL)
10975 mips_debug = 2;
10976 else
10977 mips_debug = atoi (arg);
10978 /* When the MIPS assembler sees -g or -g2, it does not do
10979 optimizations which limit full symbolic debugging. We take
10980 that to be equivalent to -O0. */
10981 if (mips_debug == 2)
10982 mips_optimize = 1;
10983 break;
10985 case OPTION_MIPS1:
10986 file_mips_isa = ISA_MIPS1;
10987 break;
10989 case OPTION_MIPS2:
10990 file_mips_isa = ISA_MIPS2;
10991 break;
10993 case OPTION_MIPS3:
10994 file_mips_isa = ISA_MIPS3;
10995 break;
10997 case OPTION_MIPS4:
10998 file_mips_isa = ISA_MIPS4;
10999 break;
11001 case OPTION_MIPS5:
11002 file_mips_isa = ISA_MIPS5;
11003 break;
11005 case OPTION_MIPS32:
11006 file_mips_isa = ISA_MIPS32;
11007 break;
11009 case OPTION_MIPS32R2:
11010 file_mips_isa = ISA_MIPS32R2;
11011 break;
11013 case OPTION_MIPS64R2:
11014 file_mips_isa = ISA_MIPS64R2;
11015 break;
11017 case OPTION_MIPS64:
11018 file_mips_isa = ISA_MIPS64;
11019 break;
11021 case OPTION_MTUNE:
11022 mips_set_option_string (&mips_tune_string, arg);
11023 break;
11025 case OPTION_MARCH:
11026 mips_set_option_string (&mips_arch_string, arg);
11027 break;
11029 case OPTION_M4650:
11030 mips_set_option_string (&mips_arch_string, "4650");
11031 mips_set_option_string (&mips_tune_string, "4650");
11032 break;
11034 case OPTION_NO_M4650:
11035 break;
11037 case OPTION_M4010:
11038 mips_set_option_string (&mips_arch_string, "4010");
11039 mips_set_option_string (&mips_tune_string, "4010");
11040 break;
11042 case OPTION_NO_M4010:
11043 break;
11045 case OPTION_M4100:
11046 mips_set_option_string (&mips_arch_string, "4100");
11047 mips_set_option_string (&mips_tune_string, "4100");
11048 break;
11050 case OPTION_NO_M4100:
11051 break;
11053 case OPTION_M3900:
11054 mips_set_option_string (&mips_arch_string, "3900");
11055 mips_set_option_string (&mips_tune_string, "3900");
11056 break;
11058 case OPTION_NO_M3900:
11059 break;
11061 case OPTION_MDMX:
11062 mips_opts.ase_mdmx = 1;
11063 break;
11065 case OPTION_NO_MDMX:
11066 mips_opts.ase_mdmx = 0;
11067 break;
11069 case OPTION_DSP:
11070 mips_opts.ase_dsp = 1;
11071 break;
11073 case OPTION_NO_DSP:
11074 mips_opts.ase_dsp = 0;
11075 break;
11077 case OPTION_MT:
11078 mips_opts.ase_mt = 1;
11079 break;
11081 case OPTION_NO_MT:
11082 mips_opts.ase_mt = 0;
11083 break;
11085 case OPTION_MIPS16:
11086 mips_opts.mips16 = 1;
11087 mips_no_prev_insn ();
11088 break;
11090 case OPTION_NO_MIPS16:
11091 mips_opts.mips16 = 0;
11092 mips_no_prev_insn ();
11093 break;
11095 case OPTION_MIPS3D:
11096 mips_opts.ase_mips3d = 1;
11097 break;
11099 case OPTION_NO_MIPS3D:
11100 mips_opts.ase_mips3d = 0;
11101 break;
11103 case OPTION_SMARTMIPS:
11104 mips_opts.ase_smartmips = 1;
11105 break;
11107 case OPTION_NO_SMARTMIPS:
11108 mips_opts.ase_smartmips = 0;
11109 break;
11111 case OPTION_FIX_VR4120:
11112 mips_fix_vr4120 = 1;
11113 break;
11115 case OPTION_NO_FIX_VR4120:
11116 mips_fix_vr4120 = 0;
11117 break;
11119 case OPTION_FIX_VR4130:
11120 mips_fix_vr4130 = 1;
11121 break;
11123 case OPTION_NO_FIX_VR4130:
11124 mips_fix_vr4130 = 0;
11125 break;
11127 case OPTION_RELAX_BRANCH:
11128 mips_relax_branch = 1;
11129 break;
11131 case OPTION_NO_RELAX_BRANCH:
11132 mips_relax_branch = 0;
11133 break;
11135 case OPTION_MSHARED:
11136 mips_in_shared = TRUE;
11137 break;
11139 case OPTION_MNO_SHARED:
11140 mips_in_shared = FALSE;
11141 break;
11143 case OPTION_MSYM32:
11144 mips_opts.sym32 = TRUE;
11145 break;
11147 case OPTION_MNO_SYM32:
11148 mips_opts.sym32 = FALSE;
11149 break;
11151 #ifdef OBJ_ELF
11152 /* When generating ELF code, we permit -KPIC and -call_shared to
11153 select SVR4_PIC, and -non_shared to select no PIC. This is
11154 intended to be compatible with Irix 5. */
11155 case OPTION_CALL_SHARED:
11156 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11158 as_bad (_("-call_shared is supported only for ELF format"));
11159 return 0;
11161 mips_pic = SVR4_PIC;
11162 mips_abicalls = TRUE;
11163 break;
11165 case OPTION_NON_SHARED:
11166 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11168 as_bad (_("-non_shared is supported only for ELF format"));
11169 return 0;
11171 mips_pic = NO_PIC;
11172 mips_abicalls = FALSE;
11173 break;
11175 /* The -xgot option tells the assembler to use 32 bit offsets
11176 when accessing the got in SVR4_PIC mode. It is for Irix
11177 compatibility. */
11178 case OPTION_XGOT:
11179 mips_big_got = 1;
11180 break;
11181 #endif /* OBJ_ELF */
11183 case 'G':
11184 g_switch_value = atoi (arg);
11185 g_switch_seen = 1;
11186 break;
11188 #ifdef OBJ_ELF
11189 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
11190 and -mabi=64. */
11191 case OPTION_32:
11192 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11194 as_bad (_("-32 is supported for ELF format only"));
11195 return 0;
11197 mips_abi = O32_ABI;
11198 break;
11200 case OPTION_N32:
11201 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11203 as_bad (_("-n32 is supported for ELF format only"));
11204 return 0;
11206 mips_abi = N32_ABI;
11207 break;
11209 case OPTION_64:
11210 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11212 as_bad (_("-64 is supported for ELF format only"));
11213 return 0;
11215 mips_abi = N64_ABI;
11216 if (! support_64bit_objects())
11217 as_fatal (_("No compiled in support for 64 bit object file format"));
11218 break;
11219 #endif /* OBJ_ELF */
11221 case OPTION_GP32:
11222 file_mips_gp32 = 1;
11223 break;
11225 case OPTION_GP64:
11226 file_mips_gp32 = 0;
11227 break;
11229 case OPTION_FP32:
11230 file_mips_fp32 = 1;
11231 break;
11233 case OPTION_FP64:
11234 file_mips_fp32 = 0;
11235 break;
11237 #ifdef OBJ_ELF
11238 case OPTION_MABI:
11239 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11241 as_bad (_("-mabi is supported for ELF format only"));
11242 return 0;
11244 if (strcmp (arg, "32") == 0)
11245 mips_abi = O32_ABI;
11246 else if (strcmp (arg, "o64") == 0)
11247 mips_abi = O64_ABI;
11248 else if (strcmp (arg, "n32") == 0)
11249 mips_abi = N32_ABI;
11250 else if (strcmp (arg, "64") == 0)
11252 mips_abi = N64_ABI;
11253 if (! support_64bit_objects())
11254 as_fatal (_("No compiled in support for 64 bit object file "
11255 "format"));
11257 else if (strcmp (arg, "eabi") == 0)
11258 mips_abi = EABI_ABI;
11259 else
11261 as_fatal (_("invalid abi -mabi=%s"), arg);
11262 return 0;
11264 break;
11265 #endif /* OBJ_ELF */
11267 case OPTION_M7000_HILO_FIX:
11268 mips_7000_hilo_fix = TRUE;
11269 break;
11271 case OPTION_MNO_7000_HILO_FIX:
11272 mips_7000_hilo_fix = FALSE;
11273 break;
11275 #ifdef OBJ_ELF
11276 case OPTION_MDEBUG:
11277 mips_flag_mdebug = TRUE;
11278 break;
11280 case OPTION_NO_MDEBUG:
11281 mips_flag_mdebug = FALSE;
11282 break;
11284 case OPTION_PDR:
11285 mips_flag_pdr = TRUE;
11286 break;
11288 case OPTION_NO_PDR:
11289 mips_flag_pdr = FALSE;
11290 break;
11292 case OPTION_MVXWORKS_PIC:
11293 mips_pic = VXWORKS_PIC;
11294 break;
11295 #endif /* OBJ_ELF */
11297 default:
11298 return 0;
11301 return 1;
11304 /* Set up globals to generate code for the ISA or processor
11305 described by INFO. */
11307 static void
11308 mips_set_architecture (const struct mips_cpu_info *info)
11310 if (info != 0)
11312 file_mips_arch = info->cpu;
11313 mips_opts.arch = info->cpu;
11314 mips_opts.isa = info->isa;
11319 /* Likewise for tuning. */
11321 static void
11322 mips_set_tune (const struct mips_cpu_info *info)
11324 if (info != 0)
11325 mips_tune = info->cpu;
11329 void
11330 mips_after_parse_args (void)
11332 const struct mips_cpu_info *arch_info = 0;
11333 const struct mips_cpu_info *tune_info = 0;
11335 /* GP relative stuff not working for PE */
11336 if (strncmp (TARGET_OS, "pe", 2) == 0)
11338 if (g_switch_seen && g_switch_value != 0)
11339 as_bad (_("-G not supported in this configuration."));
11340 g_switch_value = 0;
11343 if (mips_abi == NO_ABI)
11344 mips_abi = MIPS_DEFAULT_ABI;
11346 /* The following code determines the architecture and register size.
11347 Similar code was added to GCC 3.3 (see override_options() in
11348 config/mips/mips.c). The GAS and GCC code should be kept in sync
11349 as much as possible. */
11351 if (mips_arch_string != 0)
11352 arch_info = mips_parse_cpu ("-march", mips_arch_string);
11354 if (file_mips_isa != ISA_UNKNOWN)
11356 /* Handle -mipsN. At this point, file_mips_isa contains the
11357 ISA level specified by -mipsN, while arch_info->isa contains
11358 the -march selection (if any). */
11359 if (arch_info != 0)
11361 /* -march takes precedence over -mipsN, since it is more descriptive.
11362 There's no harm in specifying both as long as the ISA levels
11363 are the same. */
11364 if (file_mips_isa != arch_info->isa)
11365 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11366 mips_cpu_info_from_isa (file_mips_isa)->name,
11367 mips_cpu_info_from_isa (arch_info->isa)->name);
11369 else
11370 arch_info = mips_cpu_info_from_isa (file_mips_isa);
11373 if (arch_info == 0)
11374 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
11376 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
11377 as_bad ("-march=%s is not compatible with the selected ABI",
11378 arch_info->name);
11380 mips_set_architecture (arch_info);
11382 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
11383 if (mips_tune_string != 0)
11384 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
11386 if (tune_info == 0)
11387 mips_set_tune (arch_info);
11388 else
11389 mips_set_tune (tune_info);
11391 if (file_mips_gp32 >= 0)
11393 /* The user specified the size of the integer registers. Make sure
11394 it agrees with the ABI and ISA. */
11395 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11396 as_bad (_("-mgp64 used with a 32-bit processor"));
11397 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11398 as_bad (_("-mgp32 used with a 64-bit ABI"));
11399 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11400 as_bad (_("-mgp64 used with a 32-bit ABI"));
11402 else
11404 /* Infer the integer register size from the ABI and processor.
11405 Restrict ourselves to 32-bit registers if that's all the
11406 processor has, or if the ABI cannot handle 64-bit registers. */
11407 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11408 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
11411 switch (file_mips_fp32)
11413 default:
11414 case -1:
11415 /* No user specified float register size.
11416 ??? GAS treats single-float processors as though they had 64-bit
11417 float registers (although it complains when double-precision
11418 instructions are used). As things stand, saying they have 32-bit
11419 registers would lead to spurious "register must be even" messages.
11420 So here we assume float registers are never smaller than the
11421 integer ones. */
11422 if (file_mips_gp32 == 0)
11423 /* 64-bit integer registers implies 64-bit float registers. */
11424 file_mips_fp32 = 0;
11425 else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
11426 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
11427 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
11428 file_mips_fp32 = 0;
11429 else
11430 /* 32-bit float registers. */
11431 file_mips_fp32 = 1;
11432 break;
11434 /* The user specified the size of the float registers. Check if it
11435 agrees with the ABI and ISA. */
11436 case 0:
11437 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
11438 as_bad (_("-mfp64 used with a 32-bit fpu"));
11439 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
11440 && !ISA_HAS_MXHC1 (mips_opts.isa))
11441 as_warn (_("-mfp64 used with a 32-bit ABI"));
11442 break;
11443 case 1:
11444 if (ABI_NEEDS_64BIT_REGS (mips_abi))
11445 as_warn (_("-mfp32 used with a 64-bit ABI"));
11446 break;
11449 /* End of GCC-shared inference code. */
11451 /* This flag is set when we have a 64-bit capable CPU but use only
11452 32-bit wide registers. Note that EABI does not use it. */
11453 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11454 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11455 || mips_abi == O32_ABI))
11456 mips_32bitmode = 1;
11458 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11459 as_bad (_("trap exception not supported at ISA 1"));
11461 /* If the selected architecture includes support for ASEs, enable
11462 generation of code for them. */
11463 if (mips_opts.mips16 == -1)
11464 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
11465 if (mips_opts.ase_mips3d == -1)
11466 mips_opts.ase_mips3d = ((CPU_HAS_MIPS3D (file_mips_arch)
11467 || (arch_info->flags & MIPS_CPU_ASE_MIPS3D))
11468 && file_mips_fp32 == 0) ? 1 : 0;
11469 if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
11470 as_bad (_("-mfp32 used with -mips3d"));
11472 if (mips_opts.ase_mdmx == -1)
11473 mips_opts.ase_mdmx = ((CPU_HAS_MDMX (file_mips_arch)
11474 || (arch_info->flags & MIPS_CPU_ASE_MDMX))
11475 && file_mips_fp32 == 0) ? 1 : 0;
11476 if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
11477 as_bad (_("-mfp32 used with -mdmx"));
11479 if (mips_opts.ase_smartmips == -1)
11480 mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
11481 if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
11482 as_warn ("%s ISA does not support SmartMIPS",
11483 mips_cpu_info_from_isa (mips_opts.isa)->name);
11485 if (mips_opts.ase_dsp == -1)
11486 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
11487 if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
11488 as_warn ("%s ISA does not support DSP ASE",
11489 mips_cpu_info_from_isa (mips_opts.isa)->name);
11491 if (mips_opts.ase_mt == -1)
11492 mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
11493 if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
11494 as_warn ("%s ISA does not support MT ASE",
11495 mips_cpu_info_from_isa (mips_opts.isa)->name);
11497 file_mips_isa = mips_opts.isa;
11498 file_ase_mips16 = mips_opts.mips16;
11499 file_ase_mips3d = mips_opts.ase_mips3d;
11500 file_ase_mdmx = mips_opts.ase_mdmx;
11501 file_ase_smartmips = mips_opts.ase_smartmips;
11502 file_ase_dsp = mips_opts.ase_dsp;
11503 file_ase_mt = mips_opts.ase_mt;
11504 mips_opts.gp32 = file_mips_gp32;
11505 mips_opts.fp32 = file_mips_fp32;
11507 if (mips_flag_mdebug < 0)
11509 #ifdef OBJ_MAYBE_ECOFF
11510 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11511 mips_flag_mdebug = 1;
11512 else
11513 #endif /* OBJ_MAYBE_ECOFF */
11514 mips_flag_mdebug = 0;
11518 void
11519 mips_init_after_args (void)
11521 /* initialize opcodes */
11522 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
11523 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
11526 long
11527 md_pcrel_from (fixS *fixP)
11529 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11530 switch (fixP->fx_r_type)
11532 case BFD_RELOC_16_PCREL_S2:
11533 case BFD_RELOC_MIPS_JMP:
11534 /* Return the address of the delay slot. */
11535 return addr + 4;
11536 default:
11537 return addr;
11541 /* This is called before the symbol table is processed. In order to
11542 work with gcc when using mips-tfile, we must keep all local labels.
11543 However, in other cases, we want to discard them. If we were
11544 called with -g, but we didn't see any debugging information, it may
11545 mean that gcc is smuggling debugging information through to
11546 mips-tfile, in which case we must generate all local labels. */
11548 void
11549 mips_frob_file_before_adjust (void)
11551 #ifndef NO_ECOFF_DEBUGGING
11552 if (ECOFF_DEBUGGING
11553 && mips_debug != 0
11554 && ! ecoff_debugging_seen)
11555 flag_keep_locals = 1;
11556 #endif
11559 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
11560 the corresponding LO16 reloc. This is called before md_apply_fix and
11561 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
11562 relocation operators.
11564 For our purposes, a %lo() expression matches a %got() or %hi()
11565 expression if:
11567 (a) it refers to the same symbol; and
11568 (b) the offset applied in the %lo() expression is no lower than
11569 the offset applied in the %got() or %hi().
11571 (b) allows us to cope with code like:
11573 lui $4,%hi(foo)
11574 lh $4,%lo(foo+2)($4)
11576 ...which is legal on RELA targets, and has a well-defined behaviour
11577 if the user knows that adding 2 to "foo" will not induce a carry to
11578 the high 16 bits.
11580 When several %lo()s match a particular %got() or %hi(), we use the
11581 following rules to distinguish them:
11583 (1) %lo()s with smaller offsets are a better match than %lo()s with
11584 higher offsets.
11586 (2) %lo()s with no matching %got() or %hi() are better than those
11587 that already have a matching %got() or %hi().
11589 (3) later %lo()s are better than earlier %lo()s.
11591 These rules are applied in order.
11593 (1) means, among other things, that %lo()s with identical offsets are
11594 chosen if they exist.
11596 (2) means that we won't associate several high-part relocations with
11597 the same low-part relocation unless there's no alternative. Having
11598 several high parts for the same low part is a GNU extension; this rule
11599 allows careful users to avoid it.
11601 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
11602 with the last high-part relocation being at the front of the list.
11603 It therefore makes sense to choose the last matching low-part
11604 relocation, all other things being equal. It's also easier
11605 to code that way. */
11607 void
11608 mips_frob_file (void)
11610 struct mips_hi_fixup *l;
11612 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11614 segment_info_type *seginfo;
11615 bfd_boolean matched_lo_p;
11616 fixS **hi_pos, **lo_pos, **pos;
11618 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
11620 /* If a GOT16 relocation turns out to be against a global symbol,
11621 there isn't supposed to be a matching LO. */
11622 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11623 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11624 continue;
11626 /* Check quickly whether the next fixup happens to be a matching %lo. */
11627 if (fixup_has_matching_lo_p (l->fixp))
11628 continue;
11630 seginfo = seg_info (l->seg);
11632 /* Set HI_POS to the position of this relocation in the chain.
11633 Set LO_POS to the position of the chosen low-part relocation.
11634 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
11635 relocation that matches an immediately-preceding high-part
11636 relocation. */
11637 hi_pos = NULL;
11638 lo_pos = NULL;
11639 matched_lo_p = FALSE;
11640 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
11642 if (*pos == l->fixp)
11643 hi_pos = pos;
11645 if (((*pos)->fx_r_type == BFD_RELOC_LO16
11646 || (*pos)->fx_r_type == BFD_RELOC_MIPS16_LO16)
11647 && (*pos)->fx_addsy == l->fixp->fx_addsy
11648 && (*pos)->fx_offset >= l->fixp->fx_offset
11649 && (lo_pos == NULL
11650 || (*pos)->fx_offset < (*lo_pos)->fx_offset
11651 || (!matched_lo_p
11652 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
11653 lo_pos = pos;
11655 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
11656 && fixup_has_matching_lo_p (*pos));
11659 /* If we found a match, remove the high-part relocation from its
11660 current position and insert it before the low-part relocation.
11661 Make the offsets match so that fixup_has_matching_lo_p()
11662 will return true.
11664 We don't warn about unmatched high-part relocations since some
11665 versions of gcc have been known to emit dead "lui ...%hi(...)"
11666 instructions. */
11667 if (lo_pos != NULL)
11669 l->fixp->fx_offset = (*lo_pos)->fx_offset;
11670 if (l->fixp->fx_next != *lo_pos)
11672 *hi_pos = l->fixp->fx_next;
11673 l->fixp->fx_next = *lo_pos;
11674 *lo_pos = l->fixp;
11680 /* We may have combined relocations without symbols in the N32/N64 ABI.
11681 We have to prevent gas from dropping them. */
11684 mips_force_relocation (fixS *fixp)
11686 if (generic_force_reloc (fixp))
11687 return 1;
11689 if (HAVE_NEWABI
11690 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11691 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11692 || fixp->fx_r_type == BFD_RELOC_HI16_S
11693 || fixp->fx_r_type == BFD_RELOC_LO16))
11694 return 1;
11696 return 0;
11699 /* Apply a fixup to the object file. */
11701 void
11702 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
11704 bfd_byte *buf;
11705 long insn;
11706 reloc_howto_type *howto;
11708 /* We ignore generic BFD relocations we don't know about. */
11709 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11710 if (! howto)
11711 return;
11713 assert (fixP->fx_size == 4
11714 || fixP->fx_r_type == BFD_RELOC_16
11715 || fixP->fx_r_type == BFD_RELOC_64
11716 || fixP->fx_r_type == BFD_RELOC_CTOR
11717 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11718 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11719 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11721 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11723 assert (! fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2);
11725 /* Don't treat parts of a composite relocation as done. There are two
11726 reasons for this:
11728 (1) The second and third parts will be against 0 (RSS_UNDEF) but
11729 should nevertheless be emitted if the first part is.
11731 (2) In normal usage, composite relocations are never assembly-time
11732 constants. The easiest way of dealing with the pathological
11733 exceptions is to generate a relocation against STN_UNDEF and
11734 leave everything up to the linker. */
11735 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel && fixP->fx_tcbit == 0)
11736 fixP->fx_done = 1;
11738 switch (fixP->fx_r_type)
11740 case BFD_RELOC_MIPS_TLS_GD:
11741 case BFD_RELOC_MIPS_TLS_LDM:
11742 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
11743 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
11744 case BFD_RELOC_MIPS_TLS_GOTTPREL:
11745 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
11746 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
11747 S_SET_THREAD_LOCAL (fixP->fx_addsy);
11748 /* fall through */
11750 case BFD_RELOC_MIPS_JMP:
11751 case BFD_RELOC_MIPS_SHIFT5:
11752 case BFD_RELOC_MIPS_SHIFT6:
11753 case BFD_RELOC_MIPS_GOT_DISP:
11754 case BFD_RELOC_MIPS_GOT_PAGE:
11755 case BFD_RELOC_MIPS_GOT_OFST:
11756 case BFD_RELOC_MIPS_SUB:
11757 case BFD_RELOC_MIPS_INSERT_A:
11758 case BFD_RELOC_MIPS_INSERT_B:
11759 case BFD_RELOC_MIPS_DELETE:
11760 case BFD_RELOC_MIPS_HIGHEST:
11761 case BFD_RELOC_MIPS_HIGHER:
11762 case BFD_RELOC_MIPS_SCN_DISP:
11763 case BFD_RELOC_MIPS_REL16:
11764 case BFD_RELOC_MIPS_RELGOT:
11765 case BFD_RELOC_MIPS_JALR:
11766 case BFD_RELOC_HI16:
11767 case BFD_RELOC_HI16_S:
11768 case BFD_RELOC_GPREL16:
11769 case BFD_RELOC_MIPS_LITERAL:
11770 case BFD_RELOC_MIPS_CALL16:
11771 case BFD_RELOC_MIPS_GOT16:
11772 case BFD_RELOC_GPREL32:
11773 case BFD_RELOC_MIPS_GOT_HI16:
11774 case BFD_RELOC_MIPS_GOT_LO16:
11775 case BFD_RELOC_MIPS_CALL_HI16:
11776 case BFD_RELOC_MIPS_CALL_LO16:
11777 case BFD_RELOC_MIPS16_GPREL:
11778 case BFD_RELOC_MIPS16_HI16:
11779 case BFD_RELOC_MIPS16_HI16_S:
11780 /* Nothing needed to do. The value comes from the reloc entry */
11781 break;
11783 case BFD_RELOC_MIPS16_JMP:
11784 /* We currently always generate a reloc against a symbol, which
11785 means that we don't want an addend even if the symbol is
11786 defined. */
11787 *valP = 0;
11788 break;
11790 case BFD_RELOC_64:
11791 /* This is handled like BFD_RELOC_32, but we output a sign
11792 extended value if we are only 32 bits. */
11793 if (fixP->fx_done)
11795 if (8 <= sizeof (valueT))
11796 md_number_to_chars ((char *) buf, *valP, 8);
11797 else
11799 valueT hiv;
11801 if ((*valP & 0x80000000) != 0)
11802 hiv = 0xffffffff;
11803 else
11804 hiv = 0;
11805 md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
11806 *valP, 4);
11807 md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
11808 hiv, 4);
11811 break;
11813 case BFD_RELOC_RVA:
11814 case BFD_RELOC_32:
11815 /* If we are deleting this reloc entry, we must fill in the
11816 value now. This can happen if we have a .word which is not
11817 resolved when it appears but is later defined. */
11818 if (fixP->fx_done)
11819 md_number_to_chars ((char *) buf, *valP, 4);
11820 break;
11822 case BFD_RELOC_16:
11823 /* If we are deleting this reloc entry, we must fill in the
11824 value now. */
11825 if (fixP->fx_done)
11826 md_number_to_chars ((char *) buf, *valP, 2);
11827 break;
11829 case BFD_RELOC_LO16:
11830 case BFD_RELOC_MIPS16_LO16:
11831 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11832 may be safe to remove, but if so it's not obvious. */
11833 /* When handling an embedded PIC switch statement, we can wind
11834 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11835 if (fixP->fx_done)
11837 if (*valP + 0x8000 > 0xffff)
11838 as_bad_where (fixP->fx_file, fixP->fx_line,
11839 _("relocation overflow"));
11840 if (target_big_endian)
11841 buf += 2;
11842 md_number_to_chars ((char *) buf, *valP, 2);
11844 break;
11846 case BFD_RELOC_16_PCREL_S2:
11847 if ((*valP & 0x3) != 0)
11848 as_bad_where (fixP->fx_file, fixP->fx_line,
11849 _("Branch to misaligned address (%lx)"), (long) *valP);
11852 * We need to save the bits in the instruction since fixup_segment()
11853 * might be deleting the relocation entry (i.e., a branch within
11854 * the current segment).
11856 if (! fixP->fx_done)
11857 break;
11859 /* update old instruction data */
11860 if (target_big_endian)
11861 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11862 else
11863 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11865 if (*valP + 0x20000 <= 0x3ffff)
11867 insn |= (*valP >> 2) & 0xffff;
11868 md_number_to_chars ((char *) buf, insn, 4);
11870 else if (mips_pic == NO_PIC
11871 && fixP->fx_done
11872 && fixP->fx_frag->fr_address >= text_section->vma
11873 && (fixP->fx_frag->fr_address
11874 < text_section->vma + bfd_get_section_size (text_section))
11875 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11876 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11877 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11879 /* The branch offset is too large. If this is an
11880 unconditional branch, and we are not generating PIC code,
11881 we can convert it to an absolute jump instruction. */
11882 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11883 insn = 0x0c000000; /* jal */
11884 else
11885 insn = 0x08000000; /* j */
11886 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11887 fixP->fx_done = 0;
11888 fixP->fx_addsy = section_symbol (text_section);
11889 *valP += md_pcrel_from (fixP);
11890 md_number_to_chars ((char *) buf, insn, 4);
11892 else
11894 /* If we got here, we have branch-relaxation disabled,
11895 and there's nothing we can do to fix this instruction
11896 without turning it into a longer sequence. */
11897 as_bad_where (fixP->fx_file, fixP->fx_line,
11898 _("Branch out of range"));
11900 break;
11902 case BFD_RELOC_VTABLE_INHERIT:
11903 fixP->fx_done = 0;
11904 if (fixP->fx_addsy
11905 && !S_IS_DEFINED (fixP->fx_addsy)
11906 && !S_IS_WEAK (fixP->fx_addsy))
11907 S_SET_WEAK (fixP->fx_addsy);
11908 break;
11910 case BFD_RELOC_VTABLE_ENTRY:
11911 fixP->fx_done = 0;
11912 break;
11914 default:
11915 internalError ();
11918 /* Remember value for tc_gen_reloc. */
11919 fixP->fx_addnumber = *valP;
11922 static symbolS *
11923 get_symbol (void)
11925 int c;
11926 char *name;
11927 symbolS *p;
11929 name = input_line_pointer;
11930 c = get_symbol_end ();
11931 p = (symbolS *) symbol_find_or_make (name);
11932 *input_line_pointer = c;
11933 return p;
11936 /* Align the current frag to a given power of two. The MIPS assembler
11937 also automatically adjusts any preceding label. */
11939 static void
11940 mips_align (int to, int fill, symbolS *label)
11942 mips_emit_delays ();
11943 frag_align (to, fill, 0);
11944 record_alignment (now_seg, to);
11945 if (label != NULL)
11947 assert (S_GET_SEGMENT (label) == now_seg);
11948 symbol_set_frag (label, frag_now);
11949 S_SET_VALUE (label, (valueT) frag_now_fix ());
11953 /* Align to a given power of two. .align 0 turns off the automatic
11954 alignment used by the data creating pseudo-ops. */
11956 static void
11957 s_align (int x ATTRIBUTE_UNUSED)
11959 register int temp;
11960 register long temp_fill;
11961 long max_alignment = 15;
11965 o Note that the assembler pulls down any immediately preceding label
11966 to the aligned address.
11967 o It's not documented but auto alignment is reinstated by
11968 a .align pseudo instruction.
11969 o Note also that after auto alignment is turned off the mips assembler
11970 issues an error on attempt to assemble an improperly aligned data item.
11971 We don't.
11975 temp = get_absolute_expression ();
11976 if (temp > max_alignment)
11977 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11978 else if (temp < 0)
11980 as_warn (_("Alignment negative: 0 assumed."));
11981 temp = 0;
11983 if (*input_line_pointer == ',')
11985 ++input_line_pointer;
11986 temp_fill = get_absolute_expression ();
11988 else
11989 temp_fill = 0;
11990 if (temp)
11992 auto_align = 1;
11993 mips_align (temp, (int) temp_fill,
11994 insn_labels != NULL ? insn_labels->label : NULL);
11996 else
11998 auto_align = 0;
12001 demand_empty_rest_of_line ();
12004 static void
12005 s_change_sec (int sec)
12007 segT seg;
12009 #ifdef OBJ_ELF
12010 /* The ELF backend needs to know that we are changing sections, so
12011 that .previous works correctly. We could do something like check
12012 for an obj_section_change_hook macro, but that might be confusing
12013 as it would not be appropriate to use it in the section changing
12014 functions in read.c, since obj-elf.c intercepts those. FIXME:
12015 This should be cleaner, somehow. */
12016 obj_elf_section_change_hook ();
12017 #endif
12019 mips_emit_delays ();
12020 switch (sec)
12022 case 't':
12023 s_text (0);
12024 break;
12025 case 'd':
12026 s_data (0);
12027 break;
12028 case 'b':
12029 subseg_set (bss_section, (subsegT) get_absolute_expression ());
12030 demand_empty_rest_of_line ();
12031 break;
12033 case 'r':
12034 seg = subseg_new (RDATA_SECTION_NAME,
12035 (subsegT) get_absolute_expression ());
12036 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12038 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
12039 | SEC_READONLY | SEC_RELOC
12040 | SEC_DATA));
12041 if (strcmp (TARGET_OS, "elf") != 0)
12042 record_alignment (seg, 4);
12044 demand_empty_rest_of_line ();
12045 break;
12047 case 's':
12048 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
12049 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12051 bfd_set_section_flags (stdoutput, seg,
12052 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
12053 if (strcmp (TARGET_OS, "elf") != 0)
12054 record_alignment (seg, 4);
12056 demand_empty_rest_of_line ();
12057 break;
12060 auto_align = 1;
12063 void
12064 s_change_section (int ignore ATTRIBUTE_UNUSED)
12066 #ifdef OBJ_ELF
12067 char *section_name;
12068 char c;
12069 char next_c = 0;
12070 int section_type;
12071 int section_flag;
12072 int section_entry_size;
12073 int section_alignment;
12075 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12076 return;
12078 section_name = input_line_pointer;
12079 c = get_symbol_end ();
12080 if (c)
12081 next_c = *(input_line_pointer + 1);
12083 /* Do we have .section Name<,"flags">? */
12084 if (c != ',' || (c == ',' && next_c == '"'))
12086 /* just after name is now '\0'. */
12087 *input_line_pointer = c;
12088 input_line_pointer = section_name;
12089 obj_elf_section (ignore);
12090 return;
12092 input_line_pointer++;
12094 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
12095 if (c == ',')
12096 section_type = get_absolute_expression ();
12097 else
12098 section_type = 0;
12099 if (*input_line_pointer++ == ',')
12100 section_flag = get_absolute_expression ();
12101 else
12102 section_flag = 0;
12103 if (*input_line_pointer++ == ',')
12104 section_entry_size = get_absolute_expression ();
12105 else
12106 section_entry_size = 0;
12107 if (*input_line_pointer++ == ',')
12108 section_alignment = get_absolute_expression ();
12109 else
12110 section_alignment = 0;
12112 section_name = xstrdup (section_name);
12114 /* When using the generic form of .section (as implemented by obj-elf.c),
12115 there's no way to set the section type to SHT_MIPS_DWARF. Users have
12116 traditionally had to fall back on the more common @progbits instead.
12118 There's nothing really harmful in this, since bfd will correct
12119 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
12120 means that, for backwards compatibility, the special_section entries
12121 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
12123 Even so, we shouldn't force users of the MIPS .section syntax to
12124 incorrectly label the sections as SHT_PROGBITS. The best compromise
12125 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
12126 generic type-checking code. */
12127 if (section_type == SHT_MIPS_DWARF)
12128 section_type = SHT_PROGBITS;
12130 obj_elf_change_section (section_name, section_type, section_flag,
12131 section_entry_size, 0, 0, 0);
12133 if (now_seg->name != section_name)
12134 free (section_name);
12135 #endif /* OBJ_ELF */
12138 void
12139 mips_enable_auto_align (void)
12141 auto_align = 1;
12144 static void
12145 s_cons (int log_size)
12147 symbolS *label;
12149 label = insn_labels != NULL ? insn_labels->label : NULL;
12150 mips_emit_delays ();
12151 if (log_size > 0 && auto_align)
12152 mips_align (log_size, 0, label);
12153 mips_clear_insn_labels ();
12154 cons (1 << log_size);
12157 static void
12158 s_float_cons (int type)
12160 symbolS *label;
12162 label = insn_labels != NULL ? insn_labels->label : NULL;
12164 mips_emit_delays ();
12166 if (auto_align)
12168 if (type == 'd')
12169 mips_align (3, 0, label);
12170 else
12171 mips_align (2, 0, label);
12174 mips_clear_insn_labels ();
12176 float_cons (type);
12179 /* Handle .globl. We need to override it because on Irix 5 you are
12180 permitted to say
12181 .globl foo .text
12182 where foo is an undefined symbol, to mean that foo should be
12183 considered to be the address of a function. */
12185 static void
12186 s_mips_globl (int x ATTRIBUTE_UNUSED)
12188 char *name;
12189 int c;
12190 symbolS *symbolP;
12191 flagword flag;
12195 name = input_line_pointer;
12196 c = get_symbol_end ();
12197 symbolP = symbol_find_or_make (name);
12198 S_SET_EXTERNAL (symbolP);
12200 *input_line_pointer = c;
12201 SKIP_WHITESPACE ();
12203 /* On Irix 5, every global symbol that is not explicitly labelled as
12204 being a function is apparently labelled as being an object. */
12205 flag = BSF_OBJECT;
12207 if (!is_end_of_line[(unsigned char) *input_line_pointer]
12208 && (*input_line_pointer != ','))
12210 char *secname;
12211 asection *sec;
12213 secname = input_line_pointer;
12214 c = get_symbol_end ();
12215 sec = bfd_get_section_by_name (stdoutput, secname);
12216 if (sec == NULL)
12217 as_bad (_("%s: no such section"), secname);
12218 *input_line_pointer = c;
12220 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12221 flag = BSF_FUNCTION;
12224 symbol_get_bfdsym (symbolP)->flags |= flag;
12226 c = *input_line_pointer;
12227 if (c == ',')
12229 input_line_pointer++;
12230 SKIP_WHITESPACE ();
12231 if (is_end_of_line[(unsigned char) *input_line_pointer])
12232 c = '\n';
12235 while (c == ',');
12237 demand_empty_rest_of_line ();
12240 static void
12241 s_option (int x ATTRIBUTE_UNUSED)
12243 char *opt;
12244 char c;
12246 opt = input_line_pointer;
12247 c = get_symbol_end ();
12249 if (*opt == 'O')
12251 /* FIXME: What does this mean? */
12253 else if (strncmp (opt, "pic", 3) == 0)
12255 int i;
12257 i = atoi (opt + 3);
12258 if (i == 0)
12259 mips_pic = NO_PIC;
12260 else if (i == 2)
12262 mips_pic = SVR4_PIC;
12263 mips_abicalls = TRUE;
12265 else
12266 as_bad (_(".option pic%d not supported"), i);
12268 if (mips_pic == SVR4_PIC)
12270 if (g_switch_seen && g_switch_value != 0)
12271 as_warn (_("-G may not be used with SVR4 PIC code"));
12272 g_switch_value = 0;
12273 bfd_set_gp_size (stdoutput, 0);
12276 else
12277 as_warn (_("Unrecognized option \"%s\""), opt);
12279 *input_line_pointer = c;
12280 demand_empty_rest_of_line ();
12283 /* This structure is used to hold a stack of .set values. */
12285 struct mips_option_stack
12287 struct mips_option_stack *next;
12288 struct mips_set_options options;
12291 static struct mips_option_stack *mips_opts_stack;
12293 /* Handle the .set pseudo-op. */
12295 static void
12296 s_mipsset (int x ATTRIBUTE_UNUSED)
12298 char *name = input_line_pointer, ch;
12300 while (!is_end_of_line[(unsigned char) *input_line_pointer])
12301 ++input_line_pointer;
12302 ch = *input_line_pointer;
12303 *input_line_pointer = '\0';
12305 if (strcmp (name, "reorder") == 0)
12307 if (mips_opts.noreorder)
12308 end_noreorder ();
12310 else if (strcmp (name, "noreorder") == 0)
12312 if (!mips_opts.noreorder)
12313 start_noreorder ();
12315 else if (strcmp (name, "at") == 0)
12317 mips_opts.noat = 0;
12319 else if (strcmp (name, "noat") == 0)
12321 mips_opts.noat = 1;
12323 else if (strcmp (name, "macro") == 0)
12325 mips_opts.warn_about_macros = 0;
12327 else if (strcmp (name, "nomacro") == 0)
12329 if (mips_opts.noreorder == 0)
12330 as_bad (_("`noreorder' must be set before `nomacro'"));
12331 mips_opts.warn_about_macros = 1;
12333 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12335 mips_opts.nomove = 0;
12337 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12339 mips_opts.nomove = 1;
12341 else if (strcmp (name, "bopt") == 0)
12343 mips_opts.nobopt = 0;
12345 else if (strcmp (name, "nobopt") == 0)
12347 mips_opts.nobopt = 1;
12349 else if (strcmp (name, "gp=default") == 0)
12350 mips_opts.gp32 = file_mips_gp32;
12351 else if (strcmp (name, "gp=32") == 0)
12352 mips_opts.gp32 = 1;
12353 else if (strcmp (name, "gp=64") == 0)
12355 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
12356 as_warn ("%s isa does not support 64-bit registers",
12357 mips_cpu_info_from_isa (mips_opts.isa)->name);
12358 mips_opts.gp32 = 0;
12360 else if (strcmp (name, "fp=default") == 0)
12361 mips_opts.fp32 = file_mips_fp32;
12362 else if (strcmp (name, "fp=32") == 0)
12363 mips_opts.fp32 = 1;
12364 else if (strcmp (name, "fp=64") == 0)
12366 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
12367 as_warn ("%s isa does not support 64-bit floating point registers",
12368 mips_cpu_info_from_isa (mips_opts.isa)->name);
12369 mips_opts.fp32 = 0;
12371 else if (strcmp (name, "mips16") == 0
12372 || strcmp (name, "MIPS-16") == 0)
12373 mips_opts.mips16 = 1;
12374 else if (strcmp (name, "nomips16") == 0
12375 || strcmp (name, "noMIPS-16") == 0)
12376 mips_opts.mips16 = 0;
12377 else if (strcmp (name, "smartmips") == 0)
12379 if (!ISA_SUPPORTS_SMARTMIPS)
12380 as_warn ("%s ISA does not support SmartMIPS ASE",
12381 mips_cpu_info_from_isa (mips_opts.isa)->name);
12382 mips_opts.ase_smartmips = 1;
12384 else if (strcmp (name, "nosmartmips") == 0)
12385 mips_opts.ase_smartmips = 0;
12386 else if (strcmp (name, "mips3d") == 0)
12387 mips_opts.ase_mips3d = 1;
12388 else if (strcmp (name, "nomips3d") == 0)
12389 mips_opts.ase_mips3d = 0;
12390 else if (strcmp (name, "mdmx") == 0)
12391 mips_opts.ase_mdmx = 1;
12392 else if (strcmp (name, "nomdmx") == 0)
12393 mips_opts.ase_mdmx = 0;
12394 else if (strcmp (name, "dsp") == 0)
12396 if (!ISA_SUPPORTS_DSP_ASE)
12397 as_warn ("%s ISA does not support DSP ASE",
12398 mips_cpu_info_from_isa (mips_opts.isa)->name);
12399 mips_opts.ase_dsp = 1;
12401 else if (strcmp (name, "nodsp") == 0)
12402 mips_opts.ase_dsp = 0;
12403 else if (strcmp (name, "mt") == 0)
12405 if (!ISA_SUPPORTS_MT_ASE)
12406 as_warn ("%s ISA does not support MT ASE",
12407 mips_cpu_info_from_isa (mips_opts.isa)->name);
12408 mips_opts.ase_mt = 1;
12410 else if (strcmp (name, "nomt") == 0)
12411 mips_opts.ase_mt = 0;
12412 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
12414 int reset = 0;
12416 /* Permit the user to change the ISA and architecture on the fly.
12417 Needless to say, misuse can cause serious problems. */
12418 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
12420 reset = 1;
12421 mips_opts.isa = file_mips_isa;
12422 mips_opts.arch = file_mips_arch;
12424 else if (strncmp (name, "arch=", 5) == 0)
12426 const struct mips_cpu_info *p;
12428 p = mips_parse_cpu("internal use", name + 5);
12429 if (!p)
12430 as_bad (_("unknown architecture %s"), name + 5);
12431 else
12433 mips_opts.arch = p->cpu;
12434 mips_opts.isa = p->isa;
12437 else if (strncmp (name, "mips", 4) == 0)
12439 const struct mips_cpu_info *p;
12441 p = mips_parse_cpu("internal use", name);
12442 if (!p)
12443 as_bad (_("unknown ISA level %s"), name + 4);
12444 else
12446 mips_opts.arch = p->cpu;
12447 mips_opts.isa = p->isa;
12450 else
12451 as_bad (_("unknown ISA or architecture %s"), name);
12453 switch (mips_opts.isa)
12455 case 0:
12456 break;
12457 case ISA_MIPS1:
12458 case ISA_MIPS2:
12459 case ISA_MIPS32:
12460 case ISA_MIPS32R2:
12461 mips_opts.gp32 = 1;
12462 mips_opts.fp32 = 1;
12463 break;
12464 case ISA_MIPS3:
12465 case ISA_MIPS4:
12466 case ISA_MIPS5:
12467 case ISA_MIPS64:
12468 case ISA_MIPS64R2:
12469 mips_opts.gp32 = 0;
12470 mips_opts.fp32 = 0;
12471 break;
12472 default:
12473 as_bad (_("unknown ISA level %s"), name + 4);
12474 break;
12476 if (reset)
12478 mips_opts.gp32 = file_mips_gp32;
12479 mips_opts.fp32 = file_mips_fp32;
12482 else if (strcmp (name, "autoextend") == 0)
12483 mips_opts.noautoextend = 0;
12484 else if (strcmp (name, "noautoextend") == 0)
12485 mips_opts.noautoextend = 1;
12486 else if (strcmp (name, "push") == 0)
12488 struct mips_option_stack *s;
12490 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12491 s->next = mips_opts_stack;
12492 s->options = mips_opts;
12493 mips_opts_stack = s;
12495 else if (strcmp (name, "pop") == 0)
12497 struct mips_option_stack *s;
12499 s = mips_opts_stack;
12500 if (s == NULL)
12501 as_bad (_(".set pop with no .set push"));
12502 else
12504 /* If we're changing the reorder mode we need to handle
12505 delay slots correctly. */
12506 if (s->options.noreorder && ! mips_opts.noreorder)
12507 start_noreorder ();
12508 else if (! s->options.noreorder && mips_opts.noreorder)
12509 end_noreorder ();
12511 mips_opts = s->options;
12512 mips_opts_stack = s->next;
12513 free (s);
12516 else if (strcmp (name, "sym32") == 0)
12517 mips_opts.sym32 = TRUE;
12518 else if (strcmp (name, "nosym32") == 0)
12519 mips_opts.sym32 = FALSE;
12520 else
12522 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12524 *input_line_pointer = ch;
12525 demand_empty_rest_of_line ();
12528 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
12529 .option pic2. It means to generate SVR4 PIC calls. */
12531 static void
12532 s_abicalls (int ignore ATTRIBUTE_UNUSED)
12534 mips_pic = SVR4_PIC;
12535 mips_abicalls = TRUE;
12537 if (g_switch_seen && g_switch_value != 0)
12538 as_warn (_("-G may not be used with SVR4 PIC code"));
12539 g_switch_value = 0;
12541 bfd_set_gp_size (stdoutput, 0);
12542 demand_empty_rest_of_line ();
12545 /* Handle the .cpload pseudo-op. This is used when generating SVR4
12546 PIC code. It sets the $gp register for the function based on the
12547 function address, which is in the register named in the argument.
12548 This uses a relocation against _gp_disp, which is handled specially
12549 by the linker. The result is:
12550 lui $gp,%hi(_gp_disp)
12551 addiu $gp,$gp,%lo(_gp_disp)
12552 addu $gp,$gp,.cpload argument
12553 The .cpload argument is normally $25 == $t9.
12555 The -mno-shared option changes this to:
12556 lui $gp,%hi(__gnu_local_gp)
12557 addiu $gp,$gp,%lo(__gnu_local_gp)
12558 and the argument is ignored. This saves an instruction, but the
12559 resulting code is not position independent; it uses an absolute
12560 address for __gnu_local_gp. Thus code assembled with -mno-shared
12561 can go into an ordinary executable, but not into a shared library. */
12563 static void
12564 s_cpload (int ignore ATTRIBUTE_UNUSED)
12566 expressionS ex;
12567 int reg;
12568 int in_shared;
12570 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12571 .cpload is ignored. */
12572 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12574 s_ignore (0);
12575 return;
12578 /* .cpload should be in a .set noreorder section. */
12579 if (mips_opts.noreorder == 0)
12580 as_warn (_(".cpload not in noreorder section"));
12582 reg = tc_get_register (0);
12584 /* If we need to produce a 64-bit address, we are better off using
12585 the default instruction sequence. */
12586 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
12588 ex.X_op = O_symbol;
12589 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
12590 "__gnu_local_gp");
12591 ex.X_op_symbol = NULL;
12592 ex.X_add_number = 0;
12594 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12595 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12597 macro_start ();
12598 macro_build_lui (&ex, mips_gp_register);
12599 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12600 mips_gp_register, BFD_RELOC_LO16);
12601 if (in_shared)
12602 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
12603 mips_gp_register, reg);
12604 macro_end ();
12606 demand_empty_rest_of_line ();
12609 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12610 .cpsetup $reg1, offset|$reg2, label
12612 If offset is given, this results in:
12613 sd $gp, offset($sp)
12614 lui $gp, %hi(%neg(%gp_rel(label)))
12615 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12616 daddu $gp, $gp, $reg1
12618 If $reg2 is given, this results in:
12619 daddu $reg2, $gp, $0
12620 lui $gp, %hi(%neg(%gp_rel(label)))
12621 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12622 daddu $gp, $gp, $reg1
12623 $reg1 is normally $25 == $t9.
12625 The -mno-shared option replaces the last three instructions with
12626 lui $gp,%hi(_gp)
12627 addiu $gp,$gp,%lo(_gp)
12630 static void
12631 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
12633 expressionS ex_off;
12634 expressionS ex_sym;
12635 int reg1;
12637 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12638 We also need NewABI support. */
12639 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12641 s_ignore (0);
12642 return;
12645 reg1 = tc_get_register (0);
12646 SKIP_WHITESPACE ();
12647 if (*input_line_pointer != ',')
12649 as_bad (_("missing argument separator ',' for .cpsetup"));
12650 return;
12652 else
12653 ++input_line_pointer;
12654 SKIP_WHITESPACE ();
12655 if (*input_line_pointer == '$')
12657 mips_cpreturn_register = tc_get_register (0);
12658 mips_cpreturn_offset = -1;
12660 else
12662 mips_cpreturn_offset = get_absolute_expression ();
12663 mips_cpreturn_register = -1;
12665 SKIP_WHITESPACE ();
12666 if (*input_line_pointer != ',')
12668 as_bad (_("missing argument separator ',' for .cpsetup"));
12669 return;
12671 else
12672 ++input_line_pointer;
12673 SKIP_WHITESPACE ();
12674 expression (&ex_sym);
12676 macro_start ();
12677 if (mips_cpreturn_register == -1)
12679 ex_off.X_op = O_constant;
12680 ex_off.X_add_symbol = NULL;
12681 ex_off.X_op_symbol = NULL;
12682 ex_off.X_add_number = mips_cpreturn_offset;
12684 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
12685 BFD_RELOC_LO16, SP);
12687 else
12688 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
12689 mips_gp_register, 0);
12691 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
12693 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
12694 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
12695 BFD_RELOC_HI16_S);
12697 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
12698 mips_gp_register, -1, BFD_RELOC_GPREL16,
12699 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
12701 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12702 mips_gp_register, reg1);
12704 else
12706 expressionS ex;
12708 ex.X_op = O_symbol;
12709 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
12710 ex.X_op_symbol = NULL;
12711 ex.X_add_number = 0;
12713 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12714 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12716 macro_build_lui (&ex, mips_gp_register);
12717 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12718 mips_gp_register, BFD_RELOC_LO16);
12721 macro_end ();
12723 demand_empty_rest_of_line ();
12726 static void
12727 s_cplocal (int ignore ATTRIBUTE_UNUSED)
12729 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12730 .cplocal is ignored. */
12731 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12733 s_ignore (0);
12734 return;
12737 mips_gp_register = tc_get_register (0);
12738 demand_empty_rest_of_line ();
12741 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12742 offset from $sp. The offset is remembered, and after making a PIC
12743 call $gp is restored from that location. */
12745 static void
12746 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12748 expressionS ex;
12750 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12751 .cprestore is ignored. */
12752 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12754 s_ignore (0);
12755 return;
12758 mips_cprestore_offset = get_absolute_expression ();
12759 mips_cprestore_valid = 1;
12761 ex.X_op = O_constant;
12762 ex.X_add_symbol = NULL;
12763 ex.X_op_symbol = NULL;
12764 ex.X_add_number = mips_cprestore_offset;
12766 macro_start ();
12767 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12768 SP, HAVE_64BIT_ADDRESSES);
12769 macro_end ();
12771 demand_empty_rest_of_line ();
12774 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12775 was given in the preceding .cpsetup, it results in:
12776 ld $gp, offset($sp)
12778 If a register $reg2 was given there, it results in:
12779 daddu $gp, $reg2, $0
12781 static void
12782 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12784 expressionS ex;
12786 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12787 We also need NewABI support. */
12788 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12790 s_ignore (0);
12791 return;
12794 macro_start ();
12795 if (mips_cpreturn_register == -1)
12797 ex.X_op = O_constant;
12798 ex.X_add_symbol = NULL;
12799 ex.X_op_symbol = NULL;
12800 ex.X_add_number = mips_cpreturn_offset;
12802 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
12804 else
12805 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
12806 mips_cpreturn_register, 0);
12807 macro_end ();
12809 demand_empty_rest_of_line ();
12812 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12813 code. It sets the offset to use in gp_rel relocations. */
12815 static void
12816 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12818 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12819 We also need NewABI support. */
12820 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12822 s_ignore (0);
12823 return;
12826 mips_gprel_offset = get_absolute_expression ();
12828 demand_empty_rest_of_line ();
12831 /* Handle the .gpword pseudo-op. This is used when generating PIC
12832 code. It generates a 32 bit GP relative reloc. */
12834 static void
12835 s_gpword (int ignore ATTRIBUTE_UNUSED)
12837 symbolS *label;
12838 expressionS ex;
12839 char *p;
12841 /* When not generating PIC code, this is treated as .word. */
12842 if (mips_pic != SVR4_PIC)
12844 s_cons (2);
12845 return;
12848 label = insn_labels != NULL ? insn_labels->label : NULL;
12849 mips_emit_delays ();
12850 if (auto_align)
12851 mips_align (2, 0, label);
12852 mips_clear_insn_labels ();
12854 expression (&ex);
12856 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12858 as_bad (_("Unsupported use of .gpword"));
12859 ignore_rest_of_line ();
12862 p = frag_more (4);
12863 md_number_to_chars (p, 0, 4);
12864 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12865 BFD_RELOC_GPREL32);
12867 demand_empty_rest_of_line ();
12870 static void
12871 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12873 symbolS *label;
12874 expressionS ex;
12875 char *p;
12877 /* When not generating PIC code, this is treated as .dword. */
12878 if (mips_pic != SVR4_PIC)
12880 s_cons (3);
12881 return;
12884 label = insn_labels != NULL ? insn_labels->label : NULL;
12885 mips_emit_delays ();
12886 if (auto_align)
12887 mips_align (3, 0, label);
12888 mips_clear_insn_labels ();
12890 expression (&ex);
12892 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12894 as_bad (_("Unsupported use of .gpdword"));
12895 ignore_rest_of_line ();
12898 p = frag_more (8);
12899 md_number_to_chars (p, 0, 8);
12900 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12901 BFD_RELOC_GPREL32)->fx_tcbit = 1;
12903 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12904 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12905 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
12907 demand_empty_rest_of_line ();
12910 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12911 tables in SVR4 PIC code. */
12913 static void
12914 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12916 int reg;
12918 /* This is ignored when not generating SVR4 PIC code. */
12919 if (mips_pic != SVR4_PIC)
12921 s_ignore (0);
12922 return;
12925 /* Add $gp to the register named as an argument. */
12926 macro_start ();
12927 reg = tc_get_register (0);
12928 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
12929 macro_end ();
12931 demand_empty_rest_of_line ();
12934 /* Handle the .insn pseudo-op. This marks instruction labels in
12935 mips16 mode. This permits the linker to handle them specially,
12936 such as generating jalx instructions when needed. We also make
12937 them odd for the duration of the assembly, in order to generate the
12938 right sort of code. We will make them even in the adjust_symtab
12939 routine, while leaving them marked. This is convenient for the
12940 debugger and the disassembler. The linker knows to make them odd
12941 again. */
12943 static void
12944 s_insn (int ignore ATTRIBUTE_UNUSED)
12946 mips16_mark_labels ();
12948 demand_empty_rest_of_line ();
12951 /* Handle a .stabn directive. We need these in order to mark a label
12952 as being a mips16 text label correctly. Sometimes the compiler
12953 will emit a label, followed by a .stabn, and then switch sections.
12954 If the label and .stabn are in mips16 mode, then the label is
12955 really a mips16 text label. */
12957 static void
12958 s_mips_stab (int type)
12960 if (type == 'n')
12961 mips16_mark_labels ();
12963 s_stab (type);
12966 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12969 static void
12970 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12972 char *name;
12973 int c;
12974 symbolS *symbolP;
12975 expressionS exp;
12977 name = input_line_pointer;
12978 c = get_symbol_end ();
12979 symbolP = symbol_find_or_make (name);
12980 S_SET_WEAK (symbolP);
12981 *input_line_pointer = c;
12983 SKIP_WHITESPACE ();
12985 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12987 if (S_IS_DEFINED (symbolP))
12989 as_bad ("ignoring attempt to redefine symbol %s",
12990 S_GET_NAME (symbolP));
12991 ignore_rest_of_line ();
12992 return;
12995 if (*input_line_pointer == ',')
12997 ++input_line_pointer;
12998 SKIP_WHITESPACE ();
13001 expression (&exp);
13002 if (exp.X_op != O_symbol)
13004 as_bad ("bad .weakext directive");
13005 ignore_rest_of_line ();
13006 return;
13008 symbol_set_value_expression (symbolP, &exp);
13011 demand_empty_rest_of_line ();
13014 /* Parse a register string into a number. Called from the ECOFF code
13015 to parse .frame. The argument is non-zero if this is the frame
13016 register, so that we can record it in mips_frame_reg. */
13019 tc_get_register (int frame)
13021 unsigned int reg;
13023 SKIP_WHITESPACE ();
13024 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
13025 reg = 0;
13026 if (frame)
13028 mips_frame_reg = reg != 0 ? reg : SP;
13029 mips_frame_reg_valid = 1;
13030 mips_cprestore_valid = 0;
13032 return reg;
13035 valueT
13036 md_section_align (asection *seg, valueT addr)
13038 int align = bfd_get_section_alignment (stdoutput, seg);
13040 #ifdef OBJ_ELF
13041 /* We don't need to align ELF sections to the full alignment.
13042 However, Irix 5 may prefer that we align them at least to a 16
13043 byte boundary. We don't bother to align the sections if we are
13044 targeted for an embedded system. */
13045 if (strcmp (TARGET_OS, "elf") == 0)
13046 return addr;
13047 if (align > 4)
13048 align = 4;
13049 #endif
13051 return ((addr + (1 << align) - 1) & (-1 << align));
13054 /* Utility routine, called from above as well. If called while the
13055 input file is still being read, it's only an approximation. (For
13056 example, a symbol may later become defined which appeared to be
13057 undefined earlier.) */
13059 static int
13060 nopic_need_relax (symbolS *sym, int before_relaxing)
13062 if (sym == 0)
13063 return 0;
13065 if (g_switch_value > 0)
13067 const char *symname;
13068 int change;
13070 /* Find out whether this symbol can be referenced off the $gp
13071 register. It can be if it is smaller than the -G size or if
13072 it is in the .sdata or .sbss section. Certain symbols can
13073 not be referenced off the $gp, although it appears as though
13074 they can. */
13075 symname = S_GET_NAME (sym);
13076 if (symname != (const char *) NULL
13077 && (strcmp (symname, "eprol") == 0
13078 || strcmp (symname, "etext") == 0
13079 || strcmp (symname, "_gp") == 0
13080 || strcmp (symname, "edata") == 0
13081 || strcmp (symname, "_fbss") == 0
13082 || strcmp (symname, "_fdata") == 0
13083 || strcmp (symname, "_ftext") == 0
13084 || strcmp (symname, "end") == 0
13085 || strcmp (symname, "_gp_disp") == 0))
13086 change = 1;
13087 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
13088 && (0
13089 #ifndef NO_ECOFF_DEBUGGING
13090 || (symbol_get_obj (sym)->ecoff_extern_size != 0
13091 && (symbol_get_obj (sym)->ecoff_extern_size
13092 <= g_switch_value))
13093 #endif
13094 /* We must defer this decision until after the whole
13095 file has been read, since there might be a .extern
13096 after the first use of this symbol. */
13097 || (before_relaxing
13098 #ifndef NO_ECOFF_DEBUGGING
13099 && symbol_get_obj (sym)->ecoff_extern_size == 0
13100 #endif
13101 && S_GET_VALUE (sym) == 0)
13102 || (S_GET_VALUE (sym) != 0
13103 && S_GET_VALUE (sym) <= g_switch_value)))
13104 change = 0;
13105 else
13107 const char *segname;
13109 segname = segment_name (S_GET_SEGMENT (sym));
13110 assert (strcmp (segname, ".lit8") != 0
13111 && strcmp (segname, ".lit4") != 0);
13112 change = (strcmp (segname, ".sdata") != 0
13113 && strcmp (segname, ".sbss") != 0
13114 && strncmp (segname, ".sdata.", 7) != 0
13115 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
13117 return change;
13119 else
13120 /* We are not optimizing for the $gp register. */
13121 return 1;
13125 /* Return true if the given symbol should be considered local for SVR4 PIC. */
13127 static bfd_boolean
13128 pic_need_relax (symbolS *sym, asection *segtype)
13130 asection *symsec;
13131 bfd_boolean linkonce;
13133 /* Handle the case of a symbol equated to another symbol. */
13134 while (symbol_equated_reloc_p (sym))
13136 symbolS *n;
13138 /* It's possible to get a loop here in a badly written
13139 program. */
13140 n = symbol_get_value_expression (sym)->X_add_symbol;
13141 if (n == sym)
13142 break;
13143 sym = n;
13146 symsec = S_GET_SEGMENT (sym);
13148 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
13149 linkonce = FALSE;
13150 if (symsec != segtype && ! S_IS_LOCAL (sym))
13152 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
13153 != 0)
13154 linkonce = TRUE;
13156 /* The GNU toolchain uses an extension for ELF: a section
13157 beginning with the magic string .gnu.linkonce is a linkonce
13158 section. */
13159 if (strncmp (segment_name (symsec), ".gnu.linkonce",
13160 sizeof ".gnu.linkonce" - 1) == 0)
13161 linkonce = TRUE;
13164 /* This must duplicate the test in adjust_reloc_syms. */
13165 return (symsec != &bfd_und_section
13166 && symsec != &bfd_abs_section
13167 && ! bfd_is_com_section (symsec)
13168 && !linkonce
13169 #ifdef OBJ_ELF
13170 /* A global or weak symbol is treated as external. */
13171 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13172 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
13173 #endif
13178 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13179 extended opcode. SEC is the section the frag is in. */
13181 static int
13182 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
13184 int type;
13185 register const struct mips16_immed_operand *op;
13186 offsetT val;
13187 int mintiny, maxtiny;
13188 segT symsec;
13189 fragS *sym_frag;
13191 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13192 return 0;
13193 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13194 return 1;
13196 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13197 op = mips16_immed_operands;
13198 while (op->type != type)
13200 ++op;
13201 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13204 if (op->unsp)
13206 if (type == '<' || type == '>' || type == '[' || type == ']')
13208 mintiny = 1;
13209 maxtiny = 1 << op->nbits;
13211 else
13213 mintiny = 0;
13214 maxtiny = (1 << op->nbits) - 1;
13217 else
13219 mintiny = - (1 << (op->nbits - 1));
13220 maxtiny = (1 << (op->nbits - 1)) - 1;
13223 sym_frag = symbol_get_frag (fragp->fr_symbol);
13224 val = S_GET_VALUE (fragp->fr_symbol);
13225 symsec = S_GET_SEGMENT (fragp->fr_symbol);
13227 if (op->pcrel)
13229 addressT addr;
13231 /* We won't have the section when we are called from
13232 mips_relax_frag. However, we will always have been called
13233 from md_estimate_size_before_relax first. If this is a
13234 branch to a different section, we mark it as such. If SEC is
13235 NULL, and the frag is not marked, then it must be a branch to
13236 the same section. */
13237 if (sec == NULL)
13239 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13240 return 1;
13242 else
13244 /* Must have been called from md_estimate_size_before_relax. */
13245 if (symsec != sec)
13247 fragp->fr_subtype =
13248 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13250 /* FIXME: We should support this, and let the linker
13251 catch branches and loads that are out of range. */
13252 as_bad_where (fragp->fr_file, fragp->fr_line,
13253 _("unsupported PC relative reference to different section"));
13255 return 1;
13257 if (fragp != sym_frag && sym_frag->fr_address == 0)
13258 /* Assume non-extended on the first relaxation pass.
13259 The address we have calculated will be bogus if this is
13260 a forward branch to another frag, as the forward frag
13261 will have fr_address == 0. */
13262 return 0;
13265 /* In this case, we know for sure that the symbol fragment is in
13266 the same section. If the relax_marker of the symbol fragment
13267 differs from the relax_marker of this fragment, we have not
13268 yet adjusted the symbol fragment fr_address. We want to add
13269 in STRETCH in order to get a better estimate of the address.
13270 This particularly matters because of the shift bits. */
13271 if (stretch != 0
13272 && sym_frag->relax_marker != fragp->relax_marker)
13274 fragS *f;
13276 /* Adjust stretch for any alignment frag. Note that if have
13277 been expanding the earlier code, the symbol may be
13278 defined in what appears to be an earlier frag. FIXME:
13279 This doesn't handle the fr_subtype field, which specifies
13280 a maximum number of bytes to skip when doing an
13281 alignment. */
13282 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
13284 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13286 if (stretch < 0)
13287 stretch = - ((- stretch)
13288 & ~ ((1 << (int) f->fr_offset) - 1));
13289 else
13290 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13291 if (stretch == 0)
13292 break;
13295 if (f != NULL)
13296 val += stretch;
13299 addr = fragp->fr_address + fragp->fr_fix;
13301 /* The base address rules are complicated. The base address of
13302 a branch is the following instruction. The base address of a
13303 PC relative load or add is the instruction itself, but if it
13304 is in a delay slot (in which case it can not be extended) use
13305 the address of the instruction whose delay slot it is in. */
13306 if (type == 'p' || type == 'q')
13308 addr += 2;
13310 /* If we are currently assuming that this frag should be
13311 extended, then, the current address is two bytes
13312 higher. */
13313 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13314 addr += 2;
13316 /* Ignore the low bit in the target, since it will be set
13317 for a text label. */
13318 if ((val & 1) != 0)
13319 --val;
13321 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13322 addr -= 4;
13323 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13324 addr -= 2;
13326 val -= addr & ~ ((1 << op->shift) - 1);
13328 /* Branch offsets have an implicit 0 in the lowest bit. */
13329 if (type == 'p' || type == 'q')
13330 val /= 2;
13332 /* If any of the shifted bits are set, we must use an extended
13333 opcode. If the address depends on the size of this
13334 instruction, this can lead to a loop, so we arrange to always
13335 use an extended opcode. We only check this when we are in
13336 the main relaxation loop, when SEC is NULL. */
13337 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13339 fragp->fr_subtype =
13340 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13341 return 1;
13344 /* If we are about to mark a frag as extended because the value
13345 is precisely maxtiny + 1, then there is a chance of an
13346 infinite loop as in the following code:
13347 la $4,foo
13348 .skip 1020
13349 .align 2
13350 foo:
13351 In this case when the la is extended, foo is 0x3fc bytes
13352 away, so the la can be shrunk, but then foo is 0x400 away, so
13353 the la must be extended. To avoid this loop, we mark the
13354 frag as extended if it was small, and is about to become
13355 extended with a value of maxtiny + 1. */
13356 if (val == ((maxtiny + 1) << op->shift)
13357 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13358 && sec == NULL)
13360 fragp->fr_subtype =
13361 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13362 return 1;
13365 else if (symsec != absolute_section && sec != NULL)
13366 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13368 if ((val & ((1 << op->shift) - 1)) != 0
13369 || val < (mintiny << op->shift)
13370 || val > (maxtiny << op->shift))
13371 return 1;
13372 else
13373 return 0;
13376 /* Compute the length of a branch sequence, and adjust the
13377 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13378 worst-case length is computed, with UPDATE being used to indicate
13379 whether an unconditional (-1), branch-likely (+1) or regular (0)
13380 branch is to be computed. */
13381 static int
13382 relaxed_branch_length (fragS *fragp, asection *sec, int update)
13384 bfd_boolean toofar;
13385 int length;
13387 if (fragp
13388 && S_IS_DEFINED (fragp->fr_symbol)
13389 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13391 addressT addr;
13392 offsetT val;
13394 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13396 addr = fragp->fr_address + fragp->fr_fix + 4;
13398 val -= addr;
13400 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13402 else if (fragp)
13403 /* If the symbol is not defined or it's in a different segment,
13404 assume the user knows what's going on and emit a short
13405 branch. */
13406 toofar = FALSE;
13407 else
13408 toofar = TRUE;
13410 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13411 fragp->fr_subtype
13412 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13413 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13414 RELAX_BRANCH_LINK (fragp->fr_subtype),
13415 toofar);
13417 length = 4;
13418 if (toofar)
13420 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13421 length += 8;
13423 if (mips_pic != NO_PIC)
13425 /* Additional space for PIC loading of target address. */
13426 length += 8;
13427 if (mips_opts.isa == ISA_MIPS1)
13428 /* Additional space for $at-stabilizing nop. */
13429 length += 4;
13432 /* If branch is conditional. */
13433 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13434 length += 8;
13437 return length;
13440 /* Estimate the size of a frag before relaxing. Unless this is the
13441 mips16, we are not really relaxing here, and the final size is
13442 encoded in the subtype information. For the mips16, we have to
13443 decide whether we are using an extended opcode or not. */
13446 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
13448 int change;
13450 if (RELAX_BRANCH_P (fragp->fr_subtype))
13453 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13455 return fragp->fr_var;
13458 if (RELAX_MIPS16_P (fragp->fr_subtype))
13459 /* We don't want to modify the EXTENDED bit here; it might get us
13460 into infinite loops. We change it only in mips_relax_frag(). */
13461 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13463 if (mips_pic == NO_PIC)
13464 change = nopic_need_relax (fragp->fr_symbol, 0);
13465 else if (mips_pic == SVR4_PIC)
13466 change = pic_need_relax (fragp->fr_symbol, segtype);
13467 else if (mips_pic == VXWORKS_PIC)
13468 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
13469 change = 0;
13470 else
13471 abort ();
13473 if (change)
13475 fragp->fr_subtype |= RELAX_USE_SECOND;
13476 return -RELAX_FIRST (fragp->fr_subtype);
13478 else
13479 return -RELAX_SECOND (fragp->fr_subtype);
13482 /* This is called to see whether a reloc against a defined symbol
13483 should be converted into a reloc against a section. */
13486 mips_fix_adjustable (fixS *fixp)
13488 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
13489 about the format of the offset in the .o file. */
13490 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13491 return 0;
13493 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13494 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13495 return 0;
13497 if (fixp->fx_addsy == NULL)
13498 return 1;
13500 /* If symbol SYM is in a mergeable section, relocations of the form
13501 SYM + 0 can usually be made section-relative. The mergeable data
13502 is then identified by the section offset rather than by the symbol.
13504 However, if we're generating REL LO16 relocations, the offset is split
13505 between the LO16 and parterning high part relocation. The linker will
13506 need to recalculate the complete offset in order to correctly identify
13507 the merge data.
13509 The linker has traditionally not looked for the parterning high part
13510 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
13511 placed anywhere. Rather than break backwards compatibility by changing
13512 this, it seems better not to force the issue, and instead keep the
13513 original symbol. This will work with either linker behavior. */
13514 if ((fixp->fx_r_type == BFD_RELOC_LO16
13515 || fixp->fx_r_type == BFD_RELOC_MIPS16_LO16
13516 || reloc_needs_lo_p (fixp->fx_r_type))
13517 && HAVE_IN_PLACE_ADDENDS
13518 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
13519 return 0;
13521 #ifdef OBJ_ELF
13522 /* Don't adjust relocations against mips16 symbols, so that the linker
13523 can find them if it needs to set up a stub. */
13524 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13525 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13526 && fixp->fx_subsy == NULL)
13527 return 0;
13528 #endif
13530 return 1;
13533 /* Translate internal representation of relocation info to BFD target
13534 format. */
13536 arelent **
13537 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
13539 static arelent *retval[4];
13540 arelent *reloc;
13541 bfd_reloc_code_real_type code;
13543 memset (retval, 0, sizeof(retval));
13544 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
13545 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13546 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13547 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13549 if (fixp->fx_pcrel)
13551 assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2);
13553 /* At this point, fx_addnumber is "symbol offset - pcrel address".
13554 Relocations want only the symbol offset. */
13555 reloc->addend = fixp->fx_addnumber + reloc->address;
13556 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13558 /* A gruesome hack which is a result of the gruesome gas
13559 reloc handling. What's worse, for COFF (as opposed to
13560 ECOFF), we might need yet another copy of reloc->address.
13561 See bfd_install_relocation. */
13562 reloc->addend += reloc->address;
13565 else
13566 reloc->addend = fixp->fx_addnumber;
13568 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13569 entry to be used in the relocation's section offset. */
13570 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13572 reloc->address = reloc->addend;
13573 reloc->addend = 0;
13576 code = fixp->fx_r_type;
13578 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13579 if (reloc->howto == NULL)
13581 as_bad_where (fixp->fx_file, fixp->fx_line,
13582 _("Can not represent %s relocation in this object file format"),
13583 bfd_get_reloc_code_name (code));
13584 retval[0] = NULL;
13587 return retval;
13590 /* Relax a machine dependent frag. This returns the amount by which
13591 the current size of the frag should change. */
13594 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
13596 if (RELAX_BRANCH_P (fragp->fr_subtype))
13598 offsetT old_var = fragp->fr_var;
13600 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13602 return fragp->fr_var - old_var;
13605 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13606 return 0;
13608 if (mips16_extended_frag (fragp, NULL, stretch))
13610 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13611 return 0;
13612 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13613 return 2;
13615 else
13617 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13618 return 0;
13619 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13620 return -2;
13623 return 0;
13626 /* Convert a machine dependent frag. */
13628 void
13629 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
13631 if (RELAX_BRANCH_P (fragp->fr_subtype))
13633 bfd_byte *buf;
13634 unsigned long insn;
13635 expressionS exp;
13636 fixS *fixp;
13638 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13640 if (target_big_endian)
13641 insn = bfd_getb32 (buf);
13642 else
13643 insn = bfd_getl32 (buf);
13645 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13647 /* We generate a fixup instead of applying it right now
13648 because, if there are linker relaxations, we're going to
13649 need the relocations. */
13650 exp.X_op = O_symbol;
13651 exp.X_add_symbol = fragp->fr_symbol;
13652 exp.X_add_number = fragp->fr_offset;
13654 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13655 4, &exp, 1, BFD_RELOC_16_PCREL_S2);
13656 fixp->fx_file = fragp->fr_file;
13657 fixp->fx_line = fragp->fr_line;
13659 md_number_to_chars ((char *) buf, insn, 4);
13660 buf += 4;
13662 else
13664 int i;
13666 as_warn_where (fragp->fr_file, fragp->fr_line,
13667 _("relaxed out-of-range branch into a jump"));
13669 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13670 goto uncond;
13672 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13674 /* Reverse the branch. */
13675 switch ((insn >> 28) & 0xf)
13677 case 4:
13678 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13679 have the condition reversed by tweaking a single
13680 bit, and their opcodes all have 0x4???????. */
13681 assert ((insn & 0xf1000000) == 0x41000000);
13682 insn ^= 0x00010000;
13683 break;
13685 case 0:
13686 /* bltz 0x04000000 bgez 0x04010000
13687 bltzal 0x04100000 bgezal 0x04110000 */
13688 assert ((insn & 0xfc0e0000) == 0x04000000);
13689 insn ^= 0x00010000;
13690 break;
13692 case 1:
13693 /* beq 0x10000000 bne 0x14000000
13694 blez 0x18000000 bgtz 0x1c000000 */
13695 insn ^= 0x04000000;
13696 break;
13698 default:
13699 abort ();
13703 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13705 /* Clear the and-link bit. */
13706 assert ((insn & 0xfc1c0000) == 0x04100000);
13708 /* bltzal 0x04100000 bgezal 0x04110000
13709 bltzall 0x04120000 bgezall 0x04130000 */
13710 insn &= ~0x00100000;
13713 /* Branch over the branch (if the branch was likely) or the
13714 full jump (not likely case). Compute the offset from the
13715 current instruction to branch to. */
13716 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13717 i = 16;
13718 else
13720 /* How many bytes in instructions we've already emitted? */
13721 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13722 /* How many bytes in instructions from here to the end? */
13723 i = fragp->fr_var - i;
13725 /* Convert to instruction count. */
13726 i >>= 2;
13727 /* Branch counts from the next instruction. */
13728 i--;
13729 insn |= i;
13730 /* Branch over the jump. */
13731 md_number_to_chars ((char *) buf, insn, 4);
13732 buf += 4;
13734 /* Nop */
13735 md_number_to_chars ((char *) buf, 0, 4);
13736 buf += 4;
13738 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13740 /* beql $0, $0, 2f */
13741 insn = 0x50000000;
13742 /* Compute the PC offset from the current instruction to
13743 the end of the variable frag. */
13744 /* How many bytes in instructions we've already emitted? */
13745 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13746 /* How many bytes in instructions from here to the end? */
13747 i = fragp->fr_var - i;
13748 /* Convert to instruction count. */
13749 i >>= 2;
13750 /* Don't decrement i, because we want to branch over the
13751 delay slot. */
13753 insn |= i;
13754 md_number_to_chars ((char *) buf, insn, 4);
13755 buf += 4;
13757 md_number_to_chars ((char *) buf, 0, 4);
13758 buf += 4;
13761 uncond:
13762 if (mips_pic == NO_PIC)
13764 /* j or jal. */
13765 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13766 ? 0x0c000000 : 0x08000000);
13767 exp.X_op = O_symbol;
13768 exp.X_add_symbol = fragp->fr_symbol;
13769 exp.X_add_number = fragp->fr_offset;
13771 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13772 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13773 fixp->fx_file = fragp->fr_file;
13774 fixp->fx_line = fragp->fr_line;
13776 md_number_to_chars ((char *) buf, insn, 4);
13777 buf += 4;
13779 else
13781 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13782 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13783 exp.X_op = O_symbol;
13784 exp.X_add_symbol = fragp->fr_symbol;
13785 exp.X_add_number = fragp->fr_offset;
13787 if (fragp->fr_offset)
13789 exp.X_add_symbol = make_expr_symbol (&exp);
13790 exp.X_add_number = 0;
13793 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13794 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13795 fixp->fx_file = fragp->fr_file;
13796 fixp->fx_line = fragp->fr_line;
13798 md_number_to_chars ((char *) buf, insn, 4);
13799 buf += 4;
13801 if (mips_opts.isa == ISA_MIPS1)
13803 /* nop */
13804 md_number_to_chars ((char *) buf, 0, 4);
13805 buf += 4;
13808 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13809 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13811 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13812 4, &exp, 0, BFD_RELOC_LO16);
13813 fixp->fx_file = fragp->fr_file;
13814 fixp->fx_line = fragp->fr_line;
13816 md_number_to_chars ((char *) buf, insn, 4);
13817 buf += 4;
13819 /* j(al)r $at. */
13820 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13821 insn = 0x0020f809;
13822 else
13823 insn = 0x00200008;
13825 md_number_to_chars ((char *) buf, insn, 4);
13826 buf += 4;
13830 assert (buf == (bfd_byte *)fragp->fr_literal
13831 + fragp->fr_fix + fragp->fr_var);
13833 fragp->fr_fix += fragp->fr_var;
13835 return;
13838 if (RELAX_MIPS16_P (fragp->fr_subtype))
13840 int type;
13841 register const struct mips16_immed_operand *op;
13842 bfd_boolean small, ext;
13843 offsetT val;
13844 bfd_byte *buf;
13845 unsigned long insn;
13846 bfd_boolean use_extend;
13847 unsigned short extend;
13849 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13850 op = mips16_immed_operands;
13851 while (op->type != type)
13852 ++op;
13854 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13856 small = FALSE;
13857 ext = TRUE;
13859 else
13861 small = TRUE;
13862 ext = FALSE;
13865 resolve_symbol_value (fragp->fr_symbol);
13866 val = S_GET_VALUE (fragp->fr_symbol);
13867 if (op->pcrel)
13869 addressT addr;
13871 addr = fragp->fr_address + fragp->fr_fix;
13873 /* The rules for the base address of a PC relative reloc are
13874 complicated; see mips16_extended_frag. */
13875 if (type == 'p' || type == 'q')
13877 addr += 2;
13878 if (ext)
13879 addr += 2;
13880 /* Ignore the low bit in the target, since it will be
13881 set for a text label. */
13882 if ((val & 1) != 0)
13883 --val;
13885 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13886 addr -= 4;
13887 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13888 addr -= 2;
13890 addr &= ~ (addressT) ((1 << op->shift) - 1);
13891 val -= addr;
13893 /* Make sure the section winds up with the alignment we have
13894 assumed. */
13895 if (op->shift > 0)
13896 record_alignment (asec, op->shift);
13899 if (ext
13900 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13901 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13902 as_warn_where (fragp->fr_file, fragp->fr_line,
13903 _("extended instruction in delay slot"));
13905 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13907 if (target_big_endian)
13908 insn = bfd_getb16 (buf);
13909 else
13910 insn = bfd_getl16 (buf);
13912 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13913 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13914 small, ext, &insn, &use_extend, &extend);
13916 if (use_extend)
13918 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13919 fragp->fr_fix += 2;
13920 buf += 2;
13923 md_number_to_chars ((char *) buf, insn, 2);
13924 fragp->fr_fix += 2;
13925 buf += 2;
13927 else
13929 int first, second;
13930 fixS *fixp;
13932 first = RELAX_FIRST (fragp->fr_subtype);
13933 second = RELAX_SECOND (fragp->fr_subtype);
13934 fixp = (fixS *) fragp->fr_opcode;
13936 /* Possibly emit a warning if we've chosen the longer option. */
13937 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13938 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13940 const char *msg = macro_warning (fragp->fr_subtype);
13941 if (msg != 0)
13942 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13945 /* Go through all the fixups for the first sequence. Disable them
13946 (by marking them as done) if we're going to use the second
13947 sequence instead. */
13948 while (fixp
13949 && fixp->fx_frag == fragp
13950 && fixp->fx_where < fragp->fr_fix - second)
13952 if (fragp->fr_subtype & RELAX_USE_SECOND)
13953 fixp->fx_done = 1;
13954 fixp = fixp->fx_next;
13957 /* Go through the fixups for the second sequence. Disable them if
13958 we're going to use the first sequence, otherwise adjust their
13959 addresses to account for the relaxation. */
13960 while (fixp && fixp->fx_frag == fragp)
13962 if (fragp->fr_subtype & RELAX_USE_SECOND)
13963 fixp->fx_where -= first;
13964 else
13965 fixp->fx_done = 1;
13966 fixp = fixp->fx_next;
13969 /* Now modify the frag contents. */
13970 if (fragp->fr_subtype & RELAX_USE_SECOND)
13972 char *start;
13974 start = fragp->fr_literal + fragp->fr_fix - first - second;
13975 memmove (start, start + first, second);
13976 fragp->fr_fix -= first;
13978 else
13979 fragp->fr_fix -= second;
13983 #ifdef OBJ_ELF
13985 /* This function is called after the relocs have been generated.
13986 We've been storing mips16 text labels as odd. Here we convert them
13987 back to even for the convenience of the debugger. */
13989 void
13990 mips_frob_file_after_relocs (void)
13992 asymbol **syms;
13993 unsigned int count, i;
13995 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13996 return;
13998 syms = bfd_get_outsymbols (stdoutput);
13999 count = bfd_get_symcount (stdoutput);
14000 for (i = 0; i < count; i++, syms++)
14002 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
14003 && ((*syms)->value & 1) != 0)
14005 (*syms)->value &= ~1;
14006 /* If the symbol has an odd size, it was probably computed
14007 incorrectly, so adjust that as well. */
14008 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
14009 ++elf_symbol (*syms)->internal_elf_sym.st_size;
14014 #endif
14016 /* This function is called whenever a label is defined. It is used
14017 when handling branch delays; if a branch has a label, we assume we
14018 can not move it. */
14020 void
14021 mips_define_label (symbolS *sym)
14023 struct insn_label_list *l;
14025 if (free_insn_labels == NULL)
14026 l = (struct insn_label_list *) xmalloc (sizeof *l);
14027 else
14029 l = free_insn_labels;
14030 free_insn_labels = l->next;
14033 l->label = sym;
14034 l->next = insn_labels;
14035 insn_labels = l;
14037 #ifdef OBJ_ELF
14038 dwarf2_emit_label (sym);
14039 #endif
14042 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14044 /* Some special processing for a MIPS ELF file. */
14046 void
14047 mips_elf_final_processing (void)
14049 /* Write out the register information. */
14050 if (mips_abi != N64_ABI)
14052 Elf32_RegInfo s;
14054 s.ri_gprmask = mips_gprmask;
14055 s.ri_cprmask[0] = mips_cprmask[0];
14056 s.ri_cprmask[1] = mips_cprmask[1];
14057 s.ri_cprmask[2] = mips_cprmask[2];
14058 s.ri_cprmask[3] = mips_cprmask[3];
14059 /* The gp_value field is set by the MIPS ELF backend. */
14061 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14062 ((Elf32_External_RegInfo *)
14063 mips_regmask_frag));
14065 else
14067 Elf64_Internal_RegInfo s;
14069 s.ri_gprmask = mips_gprmask;
14070 s.ri_pad = 0;
14071 s.ri_cprmask[0] = mips_cprmask[0];
14072 s.ri_cprmask[1] = mips_cprmask[1];
14073 s.ri_cprmask[2] = mips_cprmask[2];
14074 s.ri_cprmask[3] = mips_cprmask[3];
14075 /* The gp_value field is set by the MIPS ELF backend. */
14077 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14078 ((Elf64_External_RegInfo *)
14079 mips_regmask_frag));
14082 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
14083 sort of BFD interface for this. */
14084 if (mips_any_noreorder)
14085 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14086 if (mips_pic != NO_PIC)
14088 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
14089 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14091 if (mips_abicalls)
14092 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14094 /* Set MIPS ELF flags for ASEs. */
14095 /* We may need to define a new flag for DSP ASE, and set this flag when
14096 file_ase_dsp is true. */
14097 /* We may need to define a new flag for MT ASE, and set this flag when
14098 file_ase_mt is true. */
14099 if (file_ase_mips16)
14100 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
14101 #if 0 /* XXX FIXME */
14102 if (file_ase_mips3d)
14103 elf_elfheader (stdoutput)->e_flags |= ???;
14104 #endif
14105 if (file_ase_mdmx)
14106 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
14108 /* Set the MIPS ELF ABI flags. */
14109 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
14110 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
14111 else if (mips_abi == O64_ABI)
14112 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
14113 else if (mips_abi == EABI_ABI)
14115 if (!file_mips_gp32)
14116 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14117 else
14118 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14120 else if (mips_abi == N32_ABI)
14121 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14123 /* Nothing to do for N64_ABI. */
14125 if (mips_32bitmode)
14126 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14128 #if 0 /* XXX FIXME */
14129 /* 32 bit code with 64 bit FP registers. */
14130 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
14131 elf_elfheader (stdoutput)->e_flags |= ???;
14132 #endif
14135 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14137 typedef struct proc {
14138 symbolS *func_sym;
14139 symbolS *func_end_sym;
14140 unsigned long reg_mask;
14141 unsigned long reg_offset;
14142 unsigned long fpreg_mask;
14143 unsigned long fpreg_offset;
14144 unsigned long frame_offset;
14145 unsigned long frame_reg;
14146 unsigned long pc_reg;
14147 } procS;
14149 static procS cur_proc;
14150 static procS *cur_proc_ptr;
14151 static int numprocs;
14153 /* Fill in an rs_align_code fragment. */
14155 void
14156 mips_handle_align (fragS *fragp)
14158 if (fragp->fr_type != rs_align_code)
14159 return;
14161 if (mips_opts.mips16)
14163 static const unsigned char be_nop[] = { 0x65, 0x00 };
14164 static const unsigned char le_nop[] = { 0x00, 0x65 };
14166 int bytes;
14167 char *p;
14169 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14170 p = fragp->fr_literal + fragp->fr_fix;
14172 if (bytes & 1)
14174 *p++ = 0;
14175 fragp->fr_fix++;
14178 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14179 fragp->fr_var = 2;
14182 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
14185 static void
14186 md_obj_begin (void)
14190 static void
14191 md_obj_end (void)
14193 /* check for premature end, nesting errors, etc */
14194 if (cur_proc_ptr)
14195 as_warn (_("missing .end at end of assembly"));
14198 static long
14199 get_number (void)
14201 int negative = 0;
14202 long val = 0;
14204 if (*input_line_pointer == '-')
14206 ++input_line_pointer;
14207 negative = 1;
14209 if (!ISDIGIT (*input_line_pointer))
14210 as_bad (_("expected simple number"));
14211 if (input_line_pointer[0] == '0')
14213 if (input_line_pointer[1] == 'x')
14215 input_line_pointer += 2;
14216 while (ISXDIGIT (*input_line_pointer))
14218 val <<= 4;
14219 val |= hex_value (*input_line_pointer++);
14221 return negative ? -val : val;
14223 else
14225 ++input_line_pointer;
14226 while (ISDIGIT (*input_line_pointer))
14228 val <<= 3;
14229 val |= *input_line_pointer++ - '0';
14231 return negative ? -val : val;
14234 if (!ISDIGIT (*input_line_pointer))
14236 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14237 *input_line_pointer, *input_line_pointer);
14238 as_warn (_("invalid number"));
14239 return -1;
14241 while (ISDIGIT (*input_line_pointer))
14243 val *= 10;
14244 val += *input_line_pointer++ - '0';
14246 return negative ? -val : val;
14249 /* The .file directive; just like the usual .file directive, but there
14250 is an initial number which is the ECOFF file index. In the non-ECOFF
14251 case .file implies DWARF-2. */
14253 static void
14254 s_mips_file (int x ATTRIBUTE_UNUSED)
14256 static int first_file_directive = 0;
14258 if (ECOFF_DEBUGGING)
14260 get_number ();
14261 s_app_file (0);
14263 else
14265 char *filename;
14267 filename = dwarf2_directive_file (0);
14269 /* Versions of GCC up to 3.1 start files with a ".file"
14270 directive even for stabs output. Make sure that this
14271 ".file" is handled. Note that you need a version of GCC
14272 after 3.1 in order to support DWARF-2 on MIPS. */
14273 if (filename != NULL && ! first_file_directive)
14275 (void) new_logical_line (filename, -1);
14276 s_app_file_string (filename, 0);
14278 first_file_directive = 1;
14282 /* The .loc directive, implying DWARF-2. */
14284 static void
14285 s_mips_loc (int x ATTRIBUTE_UNUSED)
14287 if (!ECOFF_DEBUGGING)
14288 dwarf2_directive_loc (0);
14291 /* The .end directive. */
14293 static void
14294 s_mips_end (int x ATTRIBUTE_UNUSED)
14296 symbolS *p;
14298 /* Following functions need their own .frame and .cprestore directives. */
14299 mips_frame_reg_valid = 0;
14300 mips_cprestore_valid = 0;
14302 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14304 p = get_symbol ();
14305 demand_empty_rest_of_line ();
14307 else
14308 p = NULL;
14310 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14311 as_warn (_(".end not in text section"));
14313 if (!cur_proc_ptr)
14315 as_warn (_(".end directive without a preceding .ent directive."));
14316 demand_empty_rest_of_line ();
14317 return;
14320 if (p != NULL)
14322 assert (S_GET_NAME (p));
14323 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
14324 as_warn (_(".end symbol does not match .ent symbol."));
14326 if (debug_type == DEBUG_STABS)
14327 stabs_generate_asm_endfunc (S_GET_NAME (p),
14328 S_GET_NAME (p));
14330 else
14331 as_warn (_(".end directive missing or unknown symbol"));
14333 #ifdef OBJ_ELF
14334 /* Create an expression to calculate the size of the function. */
14335 if (p && cur_proc_ptr)
14337 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
14338 expressionS *exp = xmalloc (sizeof (expressionS));
14340 obj->size = exp;
14341 exp->X_op = O_subtract;
14342 exp->X_add_symbol = symbol_temp_new_now ();
14343 exp->X_op_symbol = p;
14344 exp->X_add_number = 0;
14346 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
14349 /* Generate a .pdr section. */
14350 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
14351 && mips_flag_pdr)
14353 segT saved_seg = now_seg;
14354 subsegT saved_subseg = now_subseg;
14355 valueT dot;
14356 expressionS exp;
14357 char *fragp;
14359 dot = frag_now_fix ();
14361 #ifdef md_flush_pending_output
14362 md_flush_pending_output ();
14363 #endif
14365 assert (pdr_seg);
14366 subseg_set (pdr_seg, 0);
14368 /* Write the symbol. */
14369 exp.X_op = O_symbol;
14370 exp.X_add_symbol = p;
14371 exp.X_add_number = 0;
14372 emit_expr (&exp, 4);
14374 fragp = frag_more (7 * 4);
14376 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14377 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14378 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14379 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14380 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14381 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14382 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
14384 subseg_set (saved_seg, saved_subseg);
14386 #endif /* OBJ_ELF */
14388 cur_proc_ptr = NULL;
14391 /* The .aent and .ent directives. */
14393 static void
14394 s_mips_ent (int aent)
14396 symbolS *symbolP;
14398 symbolP = get_symbol ();
14399 if (*input_line_pointer == ',')
14400 ++input_line_pointer;
14401 SKIP_WHITESPACE ();
14402 if (ISDIGIT (*input_line_pointer)
14403 || *input_line_pointer == '-')
14404 get_number ();
14406 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14407 as_warn (_(".ent or .aent not in text section."));
14409 if (!aent && cur_proc_ptr)
14410 as_warn (_("missing .end"));
14412 if (!aent)
14414 /* This function needs its own .frame and .cprestore directives. */
14415 mips_frame_reg_valid = 0;
14416 mips_cprestore_valid = 0;
14418 cur_proc_ptr = &cur_proc;
14419 memset (cur_proc_ptr, '\0', sizeof (procS));
14421 cur_proc_ptr->func_sym = symbolP;
14423 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14425 ++numprocs;
14427 if (debug_type == DEBUG_STABS)
14428 stabs_generate_asm_func (S_GET_NAME (symbolP),
14429 S_GET_NAME (symbolP));
14432 demand_empty_rest_of_line ();
14435 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14436 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14437 s_mips_frame is used so that we can set the PDR information correctly.
14438 We can't use the ecoff routines because they make reference to the ecoff
14439 symbol table (in the mdebug section). */
14441 static void
14442 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
14444 #ifdef OBJ_ELF
14445 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14447 long val;
14449 if (cur_proc_ptr == (procS *) NULL)
14451 as_warn (_(".frame outside of .ent"));
14452 demand_empty_rest_of_line ();
14453 return;
14456 cur_proc_ptr->frame_reg = tc_get_register (1);
14458 SKIP_WHITESPACE ();
14459 if (*input_line_pointer++ != ','
14460 || get_absolute_expression_and_terminator (&val) != ',')
14462 as_warn (_("Bad .frame directive"));
14463 --input_line_pointer;
14464 demand_empty_rest_of_line ();
14465 return;
14468 cur_proc_ptr->frame_offset = val;
14469 cur_proc_ptr->pc_reg = tc_get_register (0);
14471 demand_empty_rest_of_line ();
14473 else
14474 #endif /* OBJ_ELF */
14475 s_ignore (ignore);
14478 /* The .fmask and .mask directives. If the mdebug section is present
14479 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14480 embedded targets, s_mips_mask is used so that we can set the PDR
14481 information correctly. We can't use the ecoff routines because they
14482 make reference to the ecoff symbol table (in the mdebug section). */
14484 static void
14485 s_mips_mask (int reg_type)
14487 #ifdef OBJ_ELF
14488 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14490 long mask, off;
14492 if (cur_proc_ptr == (procS *) NULL)
14494 as_warn (_(".mask/.fmask outside of .ent"));
14495 demand_empty_rest_of_line ();
14496 return;
14499 if (get_absolute_expression_and_terminator (&mask) != ',')
14501 as_warn (_("Bad .mask/.fmask directive"));
14502 --input_line_pointer;
14503 demand_empty_rest_of_line ();
14504 return;
14507 off = get_absolute_expression ();
14509 if (reg_type == 'F')
14511 cur_proc_ptr->fpreg_mask = mask;
14512 cur_proc_ptr->fpreg_offset = off;
14514 else
14516 cur_proc_ptr->reg_mask = mask;
14517 cur_proc_ptr->reg_offset = off;
14520 demand_empty_rest_of_line ();
14522 else
14523 #endif /* OBJ_ELF */
14524 s_ignore (reg_type);
14527 /* A table describing all the processors gas knows about. Names are
14528 matched in the order listed.
14530 To ease comparison, please keep this table in the same order as
14531 gcc's mips_cpu_info_table[]. */
14532 static const struct mips_cpu_info mips_cpu_info_table[] =
14534 /* Entries for generic ISAs */
14535 { "mips1", MIPS_CPU_IS_ISA, ISA_MIPS1, CPU_R3000 },
14536 { "mips2", MIPS_CPU_IS_ISA, ISA_MIPS2, CPU_R6000 },
14537 { "mips3", MIPS_CPU_IS_ISA, ISA_MIPS3, CPU_R4000 },
14538 { "mips4", MIPS_CPU_IS_ISA, ISA_MIPS4, CPU_R8000 },
14539 { "mips5", MIPS_CPU_IS_ISA, ISA_MIPS5, CPU_MIPS5 },
14540 { "mips32", MIPS_CPU_IS_ISA, ISA_MIPS32, CPU_MIPS32 },
14541 { "mips32r2", MIPS_CPU_IS_ISA, ISA_MIPS32R2, CPU_MIPS32R2 },
14542 { "mips64", MIPS_CPU_IS_ISA, ISA_MIPS64, CPU_MIPS64 },
14543 { "mips64r2", MIPS_CPU_IS_ISA, ISA_MIPS64R2, CPU_MIPS64R2 },
14545 /* MIPS I */
14546 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14547 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14548 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14550 /* MIPS II */
14551 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14553 /* MIPS III */
14554 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14555 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14556 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14557 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
14558 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14559 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14560 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
14561 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14562 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14563 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14564 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14565 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14567 /* MIPS IV */
14568 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14569 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14570 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14571 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
14572 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14573 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
14574 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14575 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14576 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14577 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14578 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14579 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
14580 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
14582 /* MIPS 32 */
14583 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
14584 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14585 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
14586 { "4ksc", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
14588 /* MIPS 32 Release 2 */
14589 { "4kec", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14590 { "4kem", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14591 { "4kep", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14592 { "4ksd", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
14593 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14594 { "m4kp", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14595 { "24k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14596 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14597 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14598 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14599 /* 24ke is a 24k with DSP ASE, other ASEs are optional. */
14600 { "24ke", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
14601 { "24kec", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
14602 { "24kef", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
14603 { "24kex", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
14604 /* 34k is a 24k with MT ASE, other ASEs are optional. */
14605 { "34kc", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
14606 { "34kf", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
14607 { "34kx", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
14609 /* MIPS 64 */
14610 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14611 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
14612 { "20kc", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
14614 /* MIPS 64 Release 2 */
14615 { "25kf", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64R2, CPU_MIPS64R2 },
14617 /* Broadcom SB-1 CPU core */
14618 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
14620 /* End marker */
14621 { NULL, 0, 0, 0 }
14625 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14626 with a final "000" replaced by "k". Ignore case.
14628 Note: this function is shared between GCC and GAS. */
14630 static bfd_boolean
14631 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
14633 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14634 given++, canonical++;
14636 return ((*given == 0 && *canonical == 0)
14637 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14641 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14642 CPU name. We've traditionally allowed a lot of variation here.
14644 Note: this function is shared between GCC and GAS. */
14646 static bfd_boolean
14647 mips_matching_cpu_name_p (const char *canonical, const char *given)
14649 /* First see if the name matches exactly, or with a final "000"
14650 turned into "k". */
14651 if (mips_strict_matching_cpu_name_p (canonical, given))
14652 return TRUE;
14654 /* If not, try comparing based on numerical designation alone.
14655 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14656 if (TOLOWER (*given) == 'r')
14657 given++;
14658 if (!ISDIGIT (*given))
14659 return FALSE;
14661 /* Skip over some well-known prefixes in the canonical name,
14662 hoping to find a number there too. */
14663 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14664 canonical += 2;
14665 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14666 canonical += 2;
14667 else if (TOLOWER (canonical[0]) == 'r')
14668 canonical += 1;
14670 return mips_strict_matching_cpu_name_p (canonical, given);
14674 /* Parse an option that takes the name of a processor as its argument.
14675 OPTION is the name of the option and CPU_STRING is the argument.
14676 Return the corresponding processor enumeration if the CPU_STRING is
14677 recognized, otherwise report an error and return null.
14679 A similar function exists in GCC. */
14681 static const struct mips_cpu_info *
14682 mips_parse_cpu (const char *option, const char *cpu_string)
14684 const struct mips_cpu_info *p;
14686 /* 'from-abi' selects the most compatible architecture for the given
14687 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14688 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14689 version. Look first at the -mgp options, if given, otherwise base
14690 the choice on MIPS_DEFAULT_64BIT.
14692 Treat NO_ABI like the EABIs. One reason to do this is that the
14693 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14694 architecture. This code picks MIPS I for 'mips' and MIPS III for
14695 'mips64', just as we did in the days before 'from-abi'. */
14696 if (strcasecmp (cpu_string, "from-abi") == 0)
14698 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14699 return mips_cpu_info_from_isa (ISA_MIPS1);
14701 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14702 return mips_cpu_info_from_isa (ISA_MIPS3);
14704 if (file_mips_gp32 >= 0)
14705 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14707 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14708 ? ISA_MIPS3
14709 : ISA_MIPS1);
14712 /* 'default' has traditionally been a no-op. Probably not very useful. */
14713 if (strcasecmp (cpu_string, "default") == 0)
14714 return 0;
14716 for (p = mips_cpu_info_table; p->name != 0; p++)
14717 if (mips_matching_cpu_name_p (p->name, cpu_string))
14718 return p;
14720 as_bad ("Bad value (%s) for %s", cpu_string, option);
14721 return 0;
14724 /* Return the canonical processor information for ISA (a member of the
14725 ISA_MIPS* enumeration). */
14727 static const struct mips_cpu_info *
14728 mips_cpu_info_from_isa (int isa)
14730 int i;
14732 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14733 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
14734 && isa == mips_cpu_info_table[i].isa)
14735 return (&mips_cpu_info_table[i]);
14737 return NULL;
14740 static const struct mips_cpu_info *
14741 mips_cpu_info_from_arch (int arch)
14743 int i;
14745 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14746 if (arch == mips_cpu_info_table[i].cpu)
14747 return (&mips_cpu_info_table[i]);
14749 return NULL;
14752 static void
14753 show (FILE *stream, const char *string, int *col_p, int *first_p)
14755 if (*first_p)
14757 fprintf (stream, "%24s", "");
14758 *col_p = 24;
14760 else
14762 fprintf (stream, ", ");
14763 *col_p += 2;
14766 if (*col_p + strlen (string) > 72)
14768 fprintf (stream, "\n%24s", "");
14769 *col_p = 24;
14772 fprintf (stream, "%s", string);
14773 *col_p += strlen (string);
14775 *first_p = 0;
14778 void
14779 md_show_usage (FILE *stream)
14781 int column, first;
14782 size_t i;
14784 fprintf (stream, _("\
14785 MIPS options:\n\
14786 -EB generate big endian output\n\
14787 -EL generate little endian output\n\
14788 -g, -g2 do not remove unneeded NOPs or swap branches\n\
14789 -G NUM allow referencing objects up to NUM bytes\n\
14790 implicitly with the gp register [default 8]\n"));
14791 fprintf (stream, _("\
14792 -mips1 generate MIPS ISA I instructions\n\
14793 -mips2 generate MIPS ISA II instructions\n\
14794 -mips3 generate MIPS ISA III instructions\n\
14795 -mips4 generate MIPS ISA IV instructions\n\
14796 -mips5 generate MIPS ISA V instructions\n\
14797 -mips32 generate MIPS32 ISA instructions\n\
14798 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
14799 -mips64 generate MIPS64 ISA instructions\n\
14800 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
14801 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14803 first = 1;
14805 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14806 show (stream, mips_cpu_info_table[i].name, &column, &first);
14807 show (stream, "from-abi", &column, &first);
14808 fputc ('\n', stream);
14810 fprintf (stream, _("\
14811 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14812 -no-mCPU don't generate code specific to CPU.\n\
14813 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14815 first = 1;
14817 show (stream, "3900", &column, &first);
14818 show (stream, "4010", &column, &first);
14819 show (stream, "4100", &column, &first);
14820 show (stream, "4650", &column, &first);
14821 fputc ('\n', stream);
14823 fprintf (stream, _("\
14824 -mips16 generate mips16 instructions\n\
14825 -no-mips16 do not generate mips16 instructions\n"));
14826 fprintf (stream, _("\
14827 -msmartmips generate smartmips instructions\n\
14828 -mno-smartmips do not generate smartmips instructions\n"));
14829 fprintf (stream, _("\
14830 -mdsp generate DSP instructions\n\
14831 -mno-dsp do not generate DSP instructions\n"));
14832 fprintf (stream, _("\
14833 -mmt generate MT instructions\n\
14834 -mno-mt do not generate MT instructions\n"));
14835 fprintf (stream, _("\
14836 -mfix-vr4120 work around certain VR4120 errata\n\
14837 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
14838 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14839 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14840 -mno-shared optimize output for executables\n\
14841 -msym32 assume all symbols have 32-bit values\n\
14842 -O0 remove unneeded NOPs, do not swap branches\n\
14843 -O remove unneeded NOPs and swap branches\n\
14844 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14845 --trap, --no-break trap exception on div by 0 and mult overflow\n\
14846 --break, --no-trap break exception on div by 0 and mult overflow\n"));
14847 #ifdef OBJ_ELF
14848 fprintf (stream, _("\
14849 -KPIC, -call_shared generate SVR4 position independent code\n\
14850 -non_shared do not generate position independent code\n\
14851 -xgot assume a 32 bit GOT\n\
14852 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
14853 -mshared, -mno-shared disable/enable .cpload optimization for\n\
14854 non-shared code\n\
14855 -mabi=ABI create ABI conformant object file for:\n"));
14857 first = 1;
14859 show (stream, "32", &column, &first);
14860 show (stream, "o64", &column, &first);
14861 show (stream, "n32", &column, &first);
14862 show (stream, "64", &column, &first);
14863 show (stream, "eabi", &column, &first);
14865 fputc ('\n', stream);
14867 fprintf (stream, _("\
14868 -32 create o32 ABI object file (default)\n\
14869 -n32 create n32 ABI object file\n\
14870 -64 create 64 ABI object file\n"));
14871 #endif
14874 enum dwarf2_format
14875 mips_dwarf2_format (void)
14877 if (mips_abi == N64_ABI)
14879 #ifdef TE_IRIX
14880 return dwarf2_format_64bit_irix;
14881 #else
14882 return dwarf2_format_64bit;
14883 #endif
14885 else
14886 return dwarf2_format_32bit;
14890 mips_dwarf2_addr_size (void)
14892 if (mips_abi == N64_ABI)
14893 return 8;
14894 else
14895 return 4;
14898 /* Standard calling conventions leave the CFA at SP on entry. */
14899 void
14900 mips_cfi_frame_initial_instructions (void)
14902 cfi_add_CFA_def_cfa_register (SP);
14906 tc_mips_regname_to_dw2regnum (char *regname)
14908 unsigned int regnum = -1;
14909 unsigned int reg;
14911 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
14912 regnum = reg;
14914 return regnum;