*** empty log message ***
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
81912461 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
23fce1e3 3 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
ec2655a6 13 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
23 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
24 02110-1301, USA. */
252b5132
RH
25
26#include "as.h"
27#include "config.h"
28#include "subsegs.h"
3882b010 29#include "safe-ctype.h"
252b5132 30
252b5132
RH
31#include "opcode/mips.h"
32#include "itbl-ops.h"
c5dd6aab 33#include "dwarf2dbg.h"
5862107c 34#include "dw2gencfi.h"
252b5132
RH
35
36#ifdef DEBUG
37#define DBG(x) printf x
38#else
39#define DBG(x)
40#endif
41
42#ifdef OBJ_MAYBE_ELF
43/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
44static int mips_output_flavor (void);
45static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
46#undef OBJ_PROCESS_STAB
47#undef OUTPUT_FLAVOR
48#undef S_GET_ALIGN
49#undef S_GET_SIZE
50#undef S_SET_ALIGN
51#undef S_SET_SIZE
252b5132
RH
52#undef obj_frob_file
53#undef obj_frob_file_after_relocs
54#undef obj_frob_symbol
55#undef obj_pop_insert
56#undef obj_sec_sym_ok_for_reloc
57#undef OBJ_COPY_SYMBOL_ATTRIBUTES
58
59#include "obj-elf.h"
60/* Fix any of them that we actually care about. */
61#undef OUTPUT_FLAVOR
62#define OUTPUT_FLAVOR mips_output_flavor()
63#endif
64
65#if defined (OBJ_ELF)
66#include "elf/mips.h"
67#endif
68
69#ifndef ECOFF_DEBUGGING
70#define NO_ECOFF_DEBUGGING
71#define ECOFF_DEBUGGING 0
72#endif
73
ecb4347a
DJ
74int mips_flag_mdebug = -1;
75
dcd410fe
RO
76/* Control generation of .pdr sections. Off by default on IRIX: the native
77 linker doesn't know about and discards them, but relocations against them
78 remain, leading to rld crashes. */
79#ifdef TE_IRIX
80int mips_flag_pdr = FALSE;
81#else
82int mips_flag_pdr = TRUE;
83#endif
84
252b5132
RH
85#include "ecoff.h"
86
87#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
88static char *mips_regmask_frag;
89#endif
90
85b51719 91#define ZERO 0
741fe287 92#define ATREG 1
252b5132
RH
93#define TREG 24
94#define PIC_CALL_REG 25
95#define KT0 26
96#define KT1 27
97#define GP 28
98#define SP 29
99#define FP 30
100#define RA 31
101
102#define ILLEGAL_REG (32)
103
741fe287
MR
104#define AT mips_opts.at
105
252b5132
RH
106/* Allow override of standard little-endian ECOFF format. */
107
108#ifndef ECOFF_LITTLE_FORMAT
109#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
110#endif
111
112extern int target_big_endian;
113
252b5132 114/* The name of the readonly data section. */
4d0d148d 115#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
252b5132 116 ? ".rdata" \
056350c6
NC
117 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
118 ? ".rdata" \
252b5132
RH
119 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
120 ? ".rodata" \
121 : (abort (), ""))
122
47e39b9d
RS
123/* Information about an instruction, including its format, operands
124 and fixups. */
125struct mips_cl_insn
126{
127 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
128 const struct mips_opcode *insn_mo;
129
130 /* True if this is a mips16 instruction and if we want the extended
131 form of INSN_MO. */
132 bfd_boolean use_extend;
133
134 /* The 16-bit extension instruction to use when USE_EXTEND is true. */
135 unsigned short extend;
136
137 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
138 a copy of INSN_MO->match with the operands filled in. */
139 unsigned long insn_opcode;
140
141 /* The frag that contains the instruction. */
142 struct frag *frag;
143
144 /* The offset into FRAG of the first instruction byte. */
145 long where;
146
147 /* The relocs associated with the instruction, if any. */
148 fixS *fixp[3];
149
a38419a5
RS
150 /* True if this entry cannot be moved from its current position. */
151 unsigned int fixed_p : 1;
47e39b9d 152
708587a4 153 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
154 unsigned int noreorder_p : 1;
155
2fa15973
RS
156 /* True for mips16 instructions that jump to an absolute address. */
157 unsigned int mips16_absolute_jump_p : 1;
47e39b9d
RS
158};
159
a325df1d
TS
160/* The ABI to use. */
161enum mips_abi_level
162{
163 NO_ABI = 0,
164 O32_ABI,
165 O64_ABI,
166 N32_ABI,
167 N64_ABI,
168 EABI_ABI
169};
170
171/* MIPS ABI we are using for this output file. */
316f5878 172static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 173
143d77c5
EC
174/* Whether or not we have code that can call pic code. */
175int mips_abicalls = FALSE;
176
aa6975fb
ILT
177/* Whether or not we have code which can be put into a shared
178 library. */
179static bfd_boolean mips_in_shared = TRUE;
180
252b5132
RH
181/* This is the set of options which may be modified by the .set
182 pseudo-op. We use a struct so that .set push and .set pop are more
183 reliable. */
184
e972090a
NC
185struct mips_set_options
186{
252b5132
RH
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;
1f25f5d3
CD
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;
deec1734 195 int ase_mdmx;
e16bfa71 196 int ase_smartmips;
74cd071d 197 int ase_dsp;
8b082fb1 198 int ase_dspr2;
ef2e4d86 199 int ase_mt;
252b5132
RH
200 /* Whether we are assembling for the mips16 processor. 0 if we are
201 not, 1 if we are, and -1 if the value has not been initialized.
202 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
203 -nomips16 command line options, and the default CPU. */
204 int mips16;
205 /* Non-zero if we should not reorder instructions. Changed by `.set
206 reorder' and `.set noreorder'. */
207 int noreorder;
741fe287
MR
208 /* Non-zero if we should not permit the register designated "assembler
209 temporary" to be used in instructions. The value is the register
210 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
211 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
212 unsigned int at;
252b5132
RH
213 /* Non-zero if we should warn when a macro instruction expands into
214 more than one machine instruction. Changed by `.set nomacro' and
215 `.set macro'. */
216 int warn_about_macros;
217 /* Non-zero if we should not move instructions. Changed by `.set
218 move', `.set volatile', `.set nomove', and `.set novolatile'. */
219 int nomove;
220 /* Non-zero if we should not optimize branches by moving the target
221 of the branch into the delay slot. Actually, we don't perform
222 this optimization anyhow. Changed by `.set bopt' and `.set
223 nobopt'. */
224 int nobopt;
225 /* Non-zero if we should not autoextend mips16 instructions.
226 Changed by `.set autoextend' and `.set noautoextend'. */
227 int noautoextend;
a325df1d
TS
228 /* Restrict general purpose registers and floating point registers
229 to 32 bit. This is initially determined when -mgp32 or -mfp32
230 is passed but can changed if the assembler code uses .set mipsN. */
231 int gp32;
232 int fp32;
fef14a42
TS
233 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
234 command line option, and the default CPU. */
235 int arch;
aed1a261
RS
236 /* True if ".set sym32" is in effect. */
237 bfd_boolean sym32;
037b32b9
AN
238 /* True if floating-point operations are not allowed. Changed by .set
239 softfloat or .set hardfloat, by command line options -msoft-float or
240 -mhard-float. The default is false. */
241 bfd_boolean soft_float;
242
243 /* True if only single-precision floating-point operations are allowed.
244 Changed by .set singlefloat or .set doublefloat, command-line options
245 -msingle-float or -mdouble-float. The default is false. */
246 bfd_boolean single_float;
252b5132
RH
247};
248
037b32b9
AN
249/* This is the struct we use to hold the current set of options. Note
250 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
251 -1 to indicate that they have not been initialized. */
252
a325df1d 253/* True if -mgp32 was passed. */
a8e8e863 254static int file_mips_gp32 = -1;
a325df1d
TS
255
256/* True if -mfp32 was passed. */
a8e8e863 257static int file_mips_fp32 = -1;
a325df1d 258
037b32b9
AN
259/* 1 if -msoft-float, 0 if -mhard-float. The default is 0. */
260static int file_mips_soft_float = 0;
261
262/* 1 if -msingle-float, 0 if -mdouble-float. The default is 0. */
263static int file_mips_single_float = 0;
252b5132 264
e972090a
NC
265static struct mips_set_options mips_opts =
266{
037b32b9
AN
267 /* isa */ ISA_UNKNOWN, /* ase_mips3d */ -1, /* ase_mdmx */ -1,
268 /* ase_smartmips */ 0, /* ase_dsp */ -1, /* ase_dspr2 */ -1, /* ase_mt */ -1,
269 /* mips16 */ -1, /* noreorder */ 0, /* at */ ATREG,
270 /* warn_about_macros */ 0, /* nomove */ 0, /* nobopt */ 0,
271 /* noautoextend */ 0, /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN,
272 /* sym32 */ FALSE, /* soft_float */ FALSE, /* single_float */ FALSE
e7af610e 273};
252b5132
RH
274
275/* These variables are filled in with the masks of registers used.
276 The object format code reads them and puts them in the appropriate
277 place. */
278unsigned long mips_gprmask;
279unsigned long mips_cprmask[4];
280
281/* MIPS ISA we are using for this output file. */
e7af610e 282static int file_mips_isa = ISA_UNKNOWN;
252b5132 283
a4672219
TS
284/* True if -mips16 was passed or implied by arguments passed on the
285 command line (e.g., by -march). */
286static int file_ase_mips16;
287
3994f87e
TS
288#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
289 || mips_opts.isa == ISA_MIPS32R2 \
290 || mips_opts.isa == ISA_MIPS64 \
291 || mips_opts.isa == ISA_MIPS64R2)
292
b12dd2e4
CF
293/* True if we want to create R_MIPS_JALR for jalr $25. */
294#ifdef TE_IRIX
1180b5a4 295#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 296#else
1180b5a4
RS
297/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
298 because there's no place for any addend, the only acceptable
299 expression is a bare symbol. */
300#define MIPS_JALR_HINT_P(EXPR) \
301 (!HAVE_IN_PLACE_ADDENDS \
302 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
303#endif
304
1f25f5d3
CD
305/* True if -mips3d was passed or implied by arguments passed on the
306 command line (e.g., by -march). */
307static int file_ase_mips3d;
308
deec1734
CD
309/* True if -mdmx was passed or implied by arguments passed on the
310 command line (e.g., by -march). */
311static int file_ase_mdmx;
312
e16bfa71
TS
313/* True if -msmartmips was passed or implied by arguments passed on the
314 command line (e.g., by -march). */
315static int file_ase_smartmips;
316
ad3fea08
TS
317#define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32 \
318 || mips_opts.isa == ISA_MIPS32R2)
e16bfa71 319
74cd071d
CF
320/* True if -mdsp was passed or implied by arguments passed on the
321 command line (e.g., by -march). */
322static int file_ase_dsp;
323
ad3fea08
TS
324#define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2 \
325 || mips_opts.isa == ISA_MIPS64R2)
326
65263ce3
TS
327#define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
328
8b082fb1
TS
329/* True if -mdspr2 was passed or implied by arguments passed on the
330 command line (e.g., by -march). */
331static int file_ase_dspr2;
332
333#define ISA_SUPPORTS_DSPR2_ASE (mips_opts.isa == ISA_MIPS32R2 \
334 || mips_opts.isa == ISA_MIPS64R2)
335
ef2e4d86
CF
336/* True if -mmt was passed or implied by arguments passed on the
337 command line (e.g., by -march). */
338static int file_ase_mt;
339
ad3fea08
TS
340#define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2 \
341 || mips_opts.isa == ISA_MIPS64R2)
342
ec68c924 343/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 344static int file_mips_arch = CPU_UNKNOWN;
316f5878 345static const char *mips_arch_string;
ec68c924
EC
346
347/* The argument of the -mtune= flag. The architecture for which we
348 are optimizing. */
349static int mips_tune = CPU_UNKNOWN;
316f5878 350static const char *mips_tune_string;
ec68c924 351
316f5878 352/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
353static int mips_32bitmode = 0;
354
316f5878
RS
355/* True if the given ABI requires 32-bit registers. */
356#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
357
358/* Likewise 64-bit registers. */
707bfff6
TS
359#define ABI_NEEDS_64BIT_REGS(ABI) \
360 ((ABI) == N32_ABI \
361 || (ABI) == N64_ABI \
316f5878
RS
362 || (ABI) == O64_ABI)
363
ad3fea08 364/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
365#define ISA_HAS_64BIT_REGS(ISA) \
366 ((ISA) == ISA_MIPS3 \
367 || (ISA) == ISA_MIPS4 \
368 || (ISA) == ISA_MIPS5 \
369 || (ISA) == ISA_MIPS64 \
370 || (ISA) == ISA_MIPS64R2)
9ce8a5dd 371
ad3fea08
TS
372/* Return true if ISA supports 64 bit wide float registers. */
373#define ISA_HAS_64BIT_FPRS(ISA) \
374 ((ISA) == ISA_MIPS3 \
375 || (ISA) == ISA_MIPS4 \
376 || (ISA) == ISA_MIPS5 \
377 || (ISA) == ISA_MIPS32R2 \
378 || (ISA) == ISA_MIPS64 \
379 || (ISA) == ISA_MIPS64R2)
380
af7ee8bf
CD
381/* Return true if ISA supports 64-bit right rotate (dror et al.)
382 instructions. */
707bfff6
TS
383#define ISA_HAS_DROR(ISA) \
384 ((ISA) == ISA_MIPS64R2)
af7ee8bf
CD
385
386/* Return true if ISA supports 32-bit right rotate (ror et al.)
387 instructions. */
707bfff6
TS
388#define ISA_HAS_ROR(ISA) \
389 ((ISA) == ISA_MIPS32R2 \
390 || (ISA) == ISA_MIPS64R2 \
391 || mips_opts.ase_smartmips)
392
7455baf8
TS
393/* Return true if ISA supports single-precision floats in odd registers. */
394#define ISA_HAS_ODD_SINGLE_FPR(ISA) \
395 ((ISA) == ISA_MIPS32 \
396 || (ISA) == ISA_MIPS32R2 \
397 || (ISA) == ISA_MIPS64 \
398 || (ISA) == ISA_MIPS64R2)
af7ee8bf 399
ad3fea08
TS
400/* Return true if ISA supports move to/from high part of a 64-bit
401 floating-point register. */
402#define ISA_HAS_MXHC1(ISA) \
403 ((ISA) == ISA_MIPS32R2 \
404 || (ISA) == ISA_MIPS64R2)
405
e013f690 406#define HAVE_32BIT_GPRS \
ad3fea08 407 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 408
e013f690 409#define HAVE_32BIT_FPRS \
ad3fea08 410 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
ca4e0257 411
ad3fea08
TS
412#define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
413#define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
ca4e0257 414
316f5878 415#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 416
316f5878 417#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 418
3b91255e
RS
419/* True if relocations are stored in-place. */
420#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
421
aed1a261
RS
422/* The ABI-derived address size. */
423#define HAVE_64BIT_ADDRESSES \
424 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
425#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 426
aed1a261
RS
427/* The size of symbolic constants (i.e., expressions of the form
428 "SYMBOL" or "SYMBOL + OFFSET"). */
429#define HAVE_32BIT_SYMBOLS \
430 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
431#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 432
b7c7d6c1
TS
433/* Addresses are loaded in different ways, depending on the address size
434 in use. The n32 ABI Documentation also mandates the use of additions
435 with overflow checking, but existing implementations don't follow it. */
f899b4b8 436#define ADDRESS_ADD_INSN \
b7c7d6c1 437 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
438
439#define ADDRESS_ADDI_INSN \
b7c7d6c1 440 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
441
442#define ADDRESS_LOAD_INSN \
443 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
444
445#define ADDRESS_STORE_INSN \
446 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
447
a4672219 448/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
449#define CPU_HAS_MIPS16(cpu) \
450 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
451 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 452
60b63b72
RS
453/* True if CPU has a dror instruction. */
454#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
455
456/* True if CPU has a ror instruction. */
457#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
458
dd3cbb7e
NC
459/* True if CPU has seq/sne and seqi/snei instructions. */
460#define CPU_HAS_SEQ(CPU) ((CPU) == CPU_OCTEON)
461
b19e8a9b
AN
462/* True if CPU does not implement the all the coprocessor insns. For these
463 CPUs only those COP insns are accepted that are explicitly marked to be
464 available on the CPU. ISA membership for COP insns is ignored. */
465#define NO_ISA_COP(CPU) ((CPU) == CPU_OCTEON)
466
c8978940
CD
467/* True if mflo and mfhi can be immediately followed by instructions
468 which write to the HI and LO registers.
469
470 According to MIPS specifications, MIPS ISAs I, II, and III need
471 (at least) two instructions between the reads of HI/LO and
472 instructions which write them, and later ISAs do not. Contradicting
473 the MIPS specifications, some MIPS IV processor user manuals (e.g.
474 the UM for the NEC Vr5000) document needing the instructions between
475 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
476 MIPS64 and later ISAs to have the interlocks, plus any specific
477 earlier-ISA CPUs for which CPU documentation declares that the
478 instructions are really interlocked. */
479#define hilo_interlocks \
480 (mips_opts.isa == ISA_MIPS32 \
481 || mips_opts.isa == ISA_MIPS32R2 \
482 || mips_opts.isa == ISA_MIPS64 \
483 || mips_opts.isa == ISA_MIPS64R2 \
484 || mips_opts.arch == CPU_R4010 \
485 || mips_opts.arch == CPU_R10000 \
486 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
487 || mips_opts.arch == CPU_R14000 \
488 || mips_opts.arch == CPU_R16000 \
c8978940 489 || mips_opts.arch == CPU_RM7000 \
c8978940
CD
490 || mips_opts.arch == CPU_VR5500 \
491 )
252b5132
RH
492
493/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
494 from the GPRs after they are loaded from memory, and thus does not
495 require nops to be inserted. This applies to instructions marked
496 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
497 level I. */
252b5132 498#define gpr_interlocks \
e7af610e 499 (mips_opts.isa != ISA_MIPS1 \
fef14a42 500 || mips_opts.arch == CPU_R3900)
252b5132 501
81912461
ILT
502/* Whether the processor uses hardware interlocks to avoid delays
503 required by coprocessor instructions, and thus does not require
504 nops to be inserted. This applies to instructions marked
505 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
506 between instructions marked INSN_WRITE_COND_CODE and ones marked
507 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
508 levels I, II, and III. */
bdaaa2e1 509/* Itbl support may require additional care here. */
81912461
ILT
510#define cop_interlocks \
511 ((mips_opts.isa != ISA_MIPS1 \
512 && mips_opts.isa != ISA_MIPS2 \
513 && mips_opts.isa != ISA_MIPS3) \
514 || mips_opts.arch == CPU_R4300 \
81912461
ILT
515 )
516
517/* Whether the processor uses hardware interlocks to protect reads
518 from coprocessor registers after they are loaded from memory, and
519 thus does not require nops to be inserted. This applies to
520 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
521 requires at MIPS ISA level I. */
522#define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
252b5132 523
6b76fefe
CM
524/* Is this a mfhi or mflo instruction? */
525#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
526 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
527
528/* Returns true for a (non floating-point) coprocessor instruction. Reading
529 or writing the condition code is only possible on the coprocessors and
530 these insns are not marked with INSN_COP. Thus for these insns use the
a242dc0d 531 condition-code flags. */
b19e8a9b
AN
532#define COP_INSN(PINFO) \
533 (PINFO != INSN_MACRO \
a242dc0d
AN
534 && ((PINFO) & (FP_S | FP_D)) == 0 \
535 && ((PINFO) & (INSN_COP | INSN_READ_COND_CODE | INSN_WRITE_COND_CODE)))
6b76fefe 536
252b5132
RH
537/* MIPS PIC level. */
538
a161fe53 539enum mips_pic_level mips_pic;
252b5132 540
c9914766 541/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 542 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 543static int mips_big_got = 0;
252b5132
RH
544
545/* 1 if trap instructions should used for overflow rather than break
546 instructions. */
c9914766 547static int mips_trap = 0;
252b5132 548
119d663a 549/* 1 if double width floating point constants should not be constructed
b6ff326e 550 by assembling two single width halves into two single width floating
119d663a
NC
551 point registers which just happen to alias the double width destination
552 register. On some architectures this aliasing can be disabled by a bit
d547a75e 553 in the status register, and the setting of this bit cannot be determined
119d663a
NC
554 automatically at assemble time. */
555static int mips_disable_float_construction;
556
252b5132
RH
557/* Non-zero if any .set noreorder directives were used. */
558
559static int mips_any_noreorder;
560
6b76fefe
CM
561/* Non-zero if nops should be inserted when the register referenced in
562 an mfhi/mflo instruction is read in the next two instructions. */
563static int mips_7000_hilo_fix;
564
02ffd3e4 565/* The size of objects in the small data section. */
156c2f8b 566static unsigned int g_switch_value = 8;
252b5132
RH
567/* Whether the -G option was used. */
568static int g_switch_seen = 0;
569
570#define N_RMASK 0xc4
571#define N_VFP 0xd4
572
573/* If we can determine in advance that GP optimization won't be
574 possible, we can skip the relaxation stuff that tries to produce
575 GP-relative references. This makes delay slot optimization work
576 better.
577
578 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
579 gcc output. It needs to guess right for gcc, otherwise gcc
580 will put what it thinks is a GP-relative instruction in a branch
581 delay slot.
252b5132
RH
582
583 I don't know if a fix is needed for the SVR4_PIC mode. I've only
584 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 585static int nopic_need_relax (symbolS *, int);
252b5132
RH
586
587/* handle of the OPCODE hash table */
588static struct hash_control *op_hash = NULL;
589
590/* The opcode hash table we use for the mips16. */
591static struct hash_control *mips16_op_hash = NULL;
592
593/* This array holds the chars that always start a comment. If the
594 pre-processor is disabled, these aren't very useful */
595const char comment_chars[] = "#";
596
597/* This array holds the chars that only start a comment at the beginning of
598 a line. If the line seems to have the form '# 123 filename'
599 .line and .file directives will appear in the pre-processed output */
600/* Note that input_file.c hand checks for '#' at the beginning of the
601 first line of the input file. This is because the compiler outputs
bdaaa2e1 602 #NO_APP at the beginning of its output. */
252b5132
RH
603/* Also note that C style comments are always supported. */
604const char line_comment_chars[] = "#";
605
bdaaa2e1 606/* This array holds machine specific line separator characters. */
63a0b638 607const char line_separator_chars[] = ";";
252b5132
RH
608
609/* Chars that can be used to separate mant from exp in floating point nums */
610const char EXP_CHARS[] = "eE";
611
612/* Chars that mean this number is a floating point constant */
613/* As in 0f12.456 */
614/* or 0d1.2345e12 */
615const char FLT_CHARS[] = "rRsSfFdDxXpP";
616
617/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
618 changed in read.c . Ideally it shouldn't have to know about it at all,
619 but nothing is ideal around here.
620 */
621
622static char *insn_error;
623
624static int auto_align = 1;
625
626/* When outputting SVR4 PIC code, the assembler needs to know the
627 offset in the stack frame from which to restore the $gp register.
628 This is set by the .cprestore pseudo-op, and saved in this
629 variable. */
630static offsetT mips_cprestore_offset = -1;
631
67c1ffbe 632/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 633 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 634 offset and even an other register than $gp as global pointer. */
6478892d
TS
635static offsetT mips_cpreturn_offset = -1;
636static int mips_cpreturn_register = -1;
637static int mips_gp_register = GP;
def2e0dd 638static int mips_gprel_offset = 0;
6478892d 639
7a621144
DJ
640/* Whether mips_cprestore_offset has been set in the current function
641 (or whether it has already been warned about, if not). */
642static int mips_cprestore_valid = 0;
643
252b5132
RH
644/* This is the register which holds the stack frame, as set by the
645 .frame pseudo-op. This is needed to implement .cprestore. */
646static int mips_frame_reg = SP;
647
7a621144
DJ
648/* Whether mips_frame_reg has been set in the current function
649 (or whether it has already been warned about, if not). */
650static int mips_frame_reg_valid = 0;
651
252b5132
RH
652/* To output NOP instructions correctly, we need to keep information
653 about the previous two instructions. */
654
655/* Whether we are optimizing. The default value of 2 means to remove
656 unneeded NOPs and swap branch instructions when possible. A value
657 of 1 means to not swap branches. A value of 0 means to always
658 insert NOPs. */
659static int mips_optimize = 2;
660
661/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
662 equivalent to seeing no -g option at all. */
663static int mips_debug = 0;
664
7d8e00cf
RS
665/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
666#define MAX_VR4130_NOPS 4
667
668/* The maximum number of NOPs needed to fill delay slots. */
669#define MAX_DELAY_NOPS 2
670
671/* The maximum number of NOPs needed for any purpose. */
672#define MAX_NOPS 4
71400594
RS
673
674/* A list of previous instructions, with index 0 being the most recent.
675 We need to look back MAX_NOPS instructions when filling delay slots
676 or working around processor errata. We need to look back one
677 instruction further if we're thinking about using history[0] to
678 fill a branch delay slot. */
679static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 680
1e915849
RS
681/* Nop instructions used by emit_nop. */
682static struct mips_cl_insn nop_insn, mips16_nop_insn;
683
684/* The appropriate nop for the current mode. */
685#define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn : &nop_insn)
252b5132 686
252b5132
RH
687/* If this is set, it points to a frag holding nop instructions which
688 were inserted before the start of a noreorder section. If those
689 nops turn out to be unnecessary, the size of the frag can be
690 decreased. */
691static fragS *prev_nop_frag;
692
693/* The number of nop instructions we created in prev_nop_frag. */
694static int prev_nop_frag_holds;
695
696/* The number of nop instructions that we know we need in
bdaaa2e1 697 prev_nop_frag. */
252b5132
RH
698static int prev_nop_frag_required;
699
700/* The number of instructions we've seen since prev_nop_frag. */
701static int prev_nop_frag_since;
702
703/* For ECOFF and ELF, relocations against symbols are done in two
704 parts, with a HI relocation and a LO relocation. Each relocation
705 has only 16 bits of space to store an addend. This means that in
706 order for the linker to handle carries correctly, it must be able
707 to locate both the HI and the LO relocation. This means that the
708 relocations must appear in order in the relocation table.
709
710 In order to implement this, we keep track of each unmatched HI
711 relocation. We then sort them so that they immediately precede the
bdaaa2e1 712 corresponding LO relocation. */
252b5132 713
e972090a
NC
714struct mips_hi_fixup
715{
252b5132
RH
716 /* Next HI fixup. */
717 struct mips_hi_fixup *next;
718 /* This fixup. */
719 fixS *fixp;
720 /* The section this fixup is in. */
721 segT seg;
722};
723
724/* The list of unmatched HI relocs. */
725
726static struct mips_hi_fixup *mips_hi_fixup_list;
727
64bdfcaf
RS
728/* The frag containing the last explicit relocation operator.
729 Null if explicit relocations have not been used. */
730
731static fragS *prev_reloc_op_frag;
732
252b5132
RH
733/* Map normal MIPS register numbers to mips16 register numbers. */
734
735#define X ILLEGAL_REG
e972090a
NC
736static const int mips32_to_16_reg_map[] =
737{
252b5132
RH
738 X, X, 2, 3, 4, 5, 6, 7,
739 X, X, X, X, X, X, X, X,
740 0, 1, X, X, X, X, X, X,
741 X, X, X, X, X, X, X, X
742};
743#undef X
744
745/* Map mips16 register numbers to normal MIPS register numbers. */
746
e972090a
NC
747static const unsigned int mips16_to_32_reg_map[] =
748{
252b5132
RH
749 16, 17, 2, 3, 4, 5, 6, 7
750};
60b63b72 751
71400594
RS
752/* Classifies the kind of instructions we're interested in when
753 implementing -mfix-vr4120. */
754enum fix_vr4120_class {
755 FIX_VR4120_MACC,
756 FIX_VR4120_DMACC,
757 FIX_VR4120_MULT,
758 FIX_VR4120_DMULT,
759 FIX_VR4120_DIV,
760 FIX_VR4120_MTHILO,
761 NUM_FIX_VR4120_CLASSES
762};
763
764/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
765 there must be at least one other instruction between an instruction
766 of type X and an instruction of type Y. */
767static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
768
769/* True if -mfix-vr4120 is in force. */
d766e8ec 770static int mips_fix_vr4120;
4a6a3df4 771
7d8e00cf
RS
772/* ...likewise -mfix-vr4130. */
773static int mips_fix_vr4130;
774
6a32d874
CM
775/* ...likewise -mfix-24k. */
776static int mips_fix_24k;
777
4a6a3df4
AO
778/* We don't relax branches by default, since this causes us to expand
779 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
780 fail to compute the offset before expanding the macro to the most
781 efficient expansion. */
782
783static int mips_relax_branch;
252b5132 784\f
4d7206a2
RS
785/* The expansion of many macros depends on the type of symbol that
786 they refer to. For example, when generating position-dependent code,
787 a macro that refers to a symbol may have two different expansions,
788 one which uses GP-relative addresses and one which uses absolute
789 addresses. When generating SVR4-style PIC, a macro may have
790 different expansions for local and global symbols.
791
792 We handle these situations by generating both sequences and putting
793 them in variant frags. In position-dependent code, the first sequence
794 will be the GP-relative one and the second sequence will be the
795 absolute one. In SVR4 PIC, the first sequence will be for global
796 symbols and the second will be for local symbols.
797
584892a6
RS
798 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
799 SECOND are the lengths of the two sequences in bytes. These fields
800 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
801 the subtype has the following flags:
4d7206a2 802
584892a6
RS
803 RELAX_USE_SECOND
804 Set if it has been decided that we should use the second
805 sequence instead of the first.
806
807 RELAX_SECOND_LONGER
808 Set in the first variant frag if the macro's second implementation
809 is longer than its first. This refers to the macro as a whole,
810 not an individual relaxation.
811
812 RELAX_NOMACRO
813 Set in the first variant frag if the macro appeared in a .set nomacro
814 block and if one alternative requires a warning but the other does not.
815
816 RELAX_DELAY_SLOT
817 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
818 delay slot.
4d7206a2
RS
819
820 The frag's "opcode" points to the first fixup for relaxable code.
821
822 Relaxable macros are generated using a sequence such as:
823
824 relax_start (SYMBOL);
825 ... generate first expansion ...
826 relax_switch ();
827 ... generate second expansion ...
828 relax_end ();
829
830 The code and fixups for the unwanted alternative are discarded
831 by md_convert_frag. */
584892a6 832#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 833
584892a6
RS
834#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
835#define RELAX_SECOND(X) ((X) & 0xff)
836#define RELAX_USE_SECOND 0x10000
837#define RELAX_SECOND_LONGER 0x20000
838#define RELAX_NOMACRO 0x40000
839#define RELAX_DELAY_SLOT 0x80000
252b5132 840
4a6a3df4
AO
841/* Branch without likely bit. If label is out of range, we turn:
842
843 beq reg1, reg2, label
844 delay slot
845
846 into
847
848 bne reg1, reg2, 0f
849 nop
850 j label
851 0: delay slot
852
853 with the following opcode replacements:
854
855 beq <-> bne
856 blez <-> bgtz
857 bltz <-> bgez
858 bc1f <-> bc1t
859
860 bltzal <-> bgezal (with jal label instead of j label)
861
862 Even though keeping the delay slot instruction in the delay slot of
863 the branch would be more efficient, it would be very tricky to do
864 correctly, because we'd have to introduce a variable frag *after*
865 the delay slot instruction, and expand that instead. Let's do it
866 the easy way for now, even if the branch-not-taken case now costs
867 one additional instruction. Out-of-range branches are not supposed
868 to be common, anyway.
869
870 Branch likely. If label is out of range, we turn:
871
872 beql reg1, reg2, label
873 delay slot (annulled if branch not taken)
874
875 into
876
877 beql reg1, reg2, 1f
878 nop
879 beql $0, $0, 2f
880 nop
881 1: j[al] label
882 delay slot (executed only if branch taken)
883 2:
884
885 It would be possible to generate a shorter sequence by losing the
886 likely bit, generating something like:
b34976b6 887
4a6a3df4
AO
888 bne reg1, reg2, 0f
889 nop
890 j[al] label
891 delay slot (executed only if branch taken)
892 0:
893
894 beql -> bne
895 bnel -> beq
896 blezl -> bgtz
897 bgtzl -> blez
898 bltzl -> bgez
899 bgezl -> bltz
900 bc1fl -> bc1t
901 bc1tl -> bc1f
902
903 bltzall -> bgezal (with jal label instead of j label)
904 bgezall -> bltzal (ditto)
905
906
907 but it's not clear that it would actually improve performance. */
af6ae2ad 908#define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
4a6a3df4
AO
909 ((relax_substateT) \
910 (0xc0000000 \
911 | ((toofar) ? 1 : 0) \
912 | ((link) ? 2 : 0) \
913 | ((likely) ? 4 : 0) \
af6ae2ad 914 | ((uncond) ? 8 : 0)))
4a6a3df4 915#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
4a6a3df4
AO
916#define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
917#define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
918#define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
ae6063d4 919#define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
4a6a3df4 920
252b5132
RH
921/* For mips16 code, we use an entirely different form of relaxation.
922 mips16 supports two versions of most instructions which take
923 immediate values: a small one which takes some small value, and a
924 larger one which takes a 16 bit value. Since branches also follow
925 this pattern, relaxing these values is required.
926
927 We can assemble both mips16 and normal MIPS code in a single
928 object. Therefore, we need to support this type of relaxation at
929 the same time that we support the relaxation described above. We
930 use the high bit of the subtype field to distinguish these cases.
931
932 The information we store for this type of relaxation is the
933 argument code found in the opcode file for this relocation, whether
934 the user explicitly requested a small or extended form, and whether
935 the relocation is in a jump or jal delay slot. That tells us the
936 size of the value, and how it should be stored. We also store
937 whether the fragment is considered to be extended or not. We also
938 store whether this is known to be a branch to a different section,
939 whether we have tried to relax this frag yet, and whether we have
940 ever extended a PC relative fragment because of a shift count. */
941#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
942 (0x80000000 \
943 | ((type) & 0xff) \
944 | ((small) ? 0x100 : 0) \
945 | ((ext) ? 0x200 : 0) \
946 | ((dslot) ? 0x400 : 0) \
947 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 948#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
949#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
950#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
951#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
952#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
953#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
954#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
955#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
956#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
957#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
958#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
959#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95
CD
960
961/* Is the given value a sign-extended 32-bit value? */
962#define IS_SEXT_32BIT_NUM(x) \
963 (((x) &~ (offsetT) 0x7fffffff) == 0 \
964 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
965
966/* Is the given value a sign-extended 16-bit value? */
967#define IS_SEXT_16BIT_NUM(x) \
968 (((x) &~ (offsetT) 0x7fff) == 0 \
969 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
970
2051e8c4
MR
971/* Is the given value a zero-extended 32-bit value? Or a negated one? */
972#define IS_ZEXT_32BIT_NUM(x) \
973 (((x) &~ (offsetT) 0xffffffff) == 0 \
974 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
975
bf12938e
RS
976/* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
977 VALUE << SHIFT. VALUE is evaluated exactly once. */
978#define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
979 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
980 | (((VALUE) & (MASK)) << (SHIFT)))
981
982/* Extract bits MASK << SHIFT from STRUCT and shift them right
983 SHIFT places. */
984#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
985 (((STRUCT) >> (SHIFT)) & (MASK))
986
987/* Change INSN's opcode so that the operand given by FIELD has value VALUE.
988 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
989
990 include/opcode/mips.h specifies operand fields using the macros
991 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
992 with "MIPS16OP" instead of "OP". */
993#define INSERT_OPERAND(FIELD, INSN, VALUE) \
994 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
995#define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
996 INSERT_BITS ((INSN).insn_opcode, VALUE, \
997 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
998
999/* Extract the operand given by FIELD from mips_cl_insn INSN. */
1000#define EXTRACT_OPERAND(FIELD, INSN) \
1001 EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD)
1002#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1003 EXTRACT_BITS ((INSN).insn_opcode, \
1004 MIPS16OP_MASK_##FIELD, \
1005 MIPS16OP_SH_##FIELD)
4d7206a2
RS
1006\f
1007/* Global variables used when generating relaxable macros. See the
1008 comment above RELAX_ENCODE for more details about how relaxation
1009 is used. */
1010static struct {
1011 /* 0 if we're not emitting a relaxable macro.
1012 1 if we're emitting the first of the two relaxation alternatives.
1013 2 if we're emitting the second alternative. */
1014 int sequence;
1015
1016 /* The first relaxable fixup in the current frag. (In other words,
1017 the first fixup that refers to relaxable code.) */
1018 fixS *first_fixup;
1019
1020 /* sizes[0] says how many bytes of the first alternative are stored in
1021 the current frag. Likewise sizes[1] for the second alternative. */
1022 unsigned int sizes[2];
1023
1024 /* The symbol on which the choice of sequence depends. */
1025 symbolS *symbol;
1026} mips_relax;
252b5132 1027\f
584892a6
RS
1028/* Global variables used to decide whether a macro needs a warning. */
1029static struct {
1030 /* True if the macro is in a branch delay slot. */
1031 bfd_boolean delay_slot_p;
1032
1033 /* For relaxable macros, sizes[0] is the length of the first alternative
1034 in bytes and sizes[1] is the length of the second alternative.
1035 For non-relaxable macros, both elements give the length of the
1036 macro in bytes. */
1037 unsigned int sizes[2];
1038
1039 /* The first variant frag for this macro. */
1040 fragS *first_frag;
1041} mips_macro_warning;
1042\f
252b5132
RH
1043/* Prototypes for static functions. */
1044
17a2f251 1045#define internalError() \
252b5132 1046 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
252b5132
RH
1047
1048enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1049
b34976b6 1050static void append_insn
4d7206a2 1051 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
7d10b47d 1052static void mips_no_prev_insn (void);
b34976b6 1053static void mips16_macro_build
67c0d1eb
RS
1054 (expressionS *, const char *, const char *, va_list);
1055static void load_register (int, expressionS *, int);
584892a6
RS
1056static void macro_start (void);
1057static void macro_end (void);
17a2f251
TS
1058static void macro (struct mips_cl_insn * ip);
1059static void mips16_macro (struct mips_cl_insn * ip);
252b5132 1060#ifdef LOSING_COMPILER
17a2f251 1061static void macro2 (struct mips_cl_insn * ip);
252b5132 1062#endif
17a2f251
TS
1063static void mips_ip (char *str, struct mips_cl_insn * ip);
1064static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1065static void mips16_immed
17a2f251
TS
1066 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
1067 unsigned long *, bfd_boolean *, unsigned short *);
5e0116d5 1068static size_t my_getSmallExpression
17a2f251
TS
1069 (expressionS *, bfd_reloc_code_real_type *, char *);
1070static void my_getExpression (expressionS *, char *);
1071static void s_align (int);
1072static void s_change_sec (int);
1073static void s_change_section (int);
1074static void s_cons (int);
1075static void s_float_cons (int);
1076static void s_mips_globl (int);
1077static void s_option (int);
1078static void s_mipsset (int);
1079static void s_abicalls (int);
1080static void s_cpload (int);
1081static void s_cpsetup (int);
1082static void s_cplocal (int);
1083static void s_cprestore (int);
1084static void s_cpreturn (int);
741d6ea8
JM
1085static void s_dtprelword (int);
1086static void s_dtpreldword (int);
17a2f251
TS
1087static void s_gpvalue (int);
1088static void s_gpword (int);
1089static void s_gpdword (int);
1090static void s_cpadd (int);
1091static void s_insn (int);
1092static void md_obj_begin (void);
1093static void md_obj_end (void);
1094static void s_mips_ent (int);
1095static void s_mips_end (int);
1096static void s_mips_frame (int);
1097static void s_mips_mask (int reg_type);
1098static void s_mips_stab (int);
1099static void s_mips_weakext (int);
1100static void s_mips_file (int);
1101static void s_mips_loc (int);
1102static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1103static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 1104static int validate_mips_insn (const struct mips_opcode *);
e7af610e
NC
1105
1106/* Table and functions used to map between CPU/ISA names, and
1107 ISA levels, and CPU numbers. */
1108
e972090a
NC
1109struct mips_cpu_info
1110{
e7af610e 1111 const char *name; /* CPU or ISA name. */
ad3fea08 1112 int flags; /* ASEs available, or ISA flag. */
e7af610e
NC
1113 int isa; /* ISA level. */
1114 int cpu; /* CPU number (default CPU if ISA). */
1115};
1116
ad3fea08
TS
1117#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1118#define MIPS_CPU_ASE_SMARTMIPS 0x0002 /* CPU implements SmartMIPS ASE */
1119#define MIPS_CPU_ASE_DSP 0x0004 /* CPU implements DSP ASE */
1120#define MIPS_CPU_ASE_MT 0x0008 /* CPU implements MT ASE */
1121#define MIPS_CPU_ASE_MIPS3D 0x0010 /* CPU implements MIPS-3D ASE */
1122#define MIPS_CPU_ASE_MDMX 0x0020 /* CPU implements MDMX ASE */
8b082fb1 1123#define MIPS_CPU_ASE_DSPR2 0x0040 /* CPU implements DSP R2 ASE */
ad3fea08 1124
17a2f251
TS
1125static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1126static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1127static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
1128\f
1129/* Pseudo-op table.
1130
1131 The following pseudo-ops from the Kane and Heinrich MIPS book
1132 should be defined here, but are currently unsupported: .alias,
1133 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1134
1135 The following pseudo-ops from the Kane and Heinrich MIPS book are
1136 specific to the type of debugging information being generated, and
1137 should be defined by the object format: .aent, .begin, .bend,
1138 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1139 .vreg.
1140
1141 The following pseudo-ops from the Kane and Heinrich MIPS book are
1142 not MIPS CPU specific, but are also not specific to the object file
1143 format. This file is probably the best place to define them, but
d84bcf09 1144 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1145
e972090a
NC
1146static const pseudo_typeS mips_pseudo_table[] =
1147{
beae10d5 1148 /* MIPS specific pseudo-ops. */
252b5132
RH
1149 {"option", s_option, 0},
1150 {"set", s_mipsset, 0},
1151 {"rdata", s_change_sec, 'r'},
1152 {"sdata", s_change_sec, 's'},
1153 {"livereg", s_ignore, 0},
1154 {"abicalls", s_abicalls, 0},
1155 {"cpload", s_cpload, 0},
6478892d
TS
1156 {"cpsetup", s_cpsetup, 0},
1157 {"cplocal", s_cplocal, 0},
252b5132 1158 {"cprestore", s_cprestore, 0},
6478892d 1159 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1160 {"dtprelword", s_dtprelword, 0},
1161 {"dtpreldword", s_dtpreldword, 0},
6478892d 1162 {"gpvalue", s_gpvalue, 0},
252b5132 1163 {"gpword", s_gpword, 0},
10181a0d 1164 {"gpdword", s_gpdword, 0},
252b5132
RH
1165 {"cpadd", s_cpadd, 0},
1166 {"insn", s_insn, 0},
1167
beae10d5 1168 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1169 chips. */
38a57ae7 1170 {"asciiz", stringer, 8 + 1},
252b5132
RH
1171 {"bss", s_change_sec, 'b'},
1172 {"err", s_err, 0},
1173 {"half", s_cons, 1},
1174 {"dword", s_cons, 3},
1175 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1176 {"origin", s_org, 0},
1177 {"repeat", s_rept, 0},
252b5132 1178
beae10d5 1179 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1180 here for one reason or another. */
1181 {"align", s_align, 0},
1182 {"byte", s_cons, 0},
1183 {"data", s_change_sec, 'd'},
1184 {"double", s_float_cons, 'd'},
1185 {"float", s_float_cons, 'f'},
1186 {"globl", s_mips_globl, 0},
1187 {"global", s_mips_globl, 0},
1188 {"hword", s_cons, 1},
1189 {"int", s_cons, 2},
1190 {"long", s_cons, 2},
1191 {"octa", s_cons, 4},
1192 {"quad", s_cons, 3},
cca86cc8 1193 {"section", s_change_section, 0},
252b5132
RH
1194 {"short", s_cons, 1},
1195 {"single", s_float_cons, 'f'},
1196 {"stabn", s_mips_stab, 'n'},
1197 {"text", s_change_sec, 't'},
1198 {"word", s_cons, 2},
add56521 1199
add56521 1200 { "extern", ecoff_directive_extern, 0},
add56521 1201
43841e91 1202 { NULL, NULL, 0 },
252b5132
RH
1203};
1204
e972090a
NC
1205static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1206{
beae10d5
KH
1207 /* These pseudo-ops should be defined by the object file format.
1208 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1209 {"aent", s_mips_ent, 1},
1210 {"bgnb", s_ignore, 0},
1211 {"end", s_mips_end, 0},
1212 {"endb", s_ignore, 0},
1213 {"ent", s_mips_ent, 0},
c5dd6aab 1214 {"file", s_mips_file, 0},
252b5132
RH
1215 {"fmask", s_mips_mask, 'F'},
1216 {"frame", s_mips_frame, 0},
c5dd6aab 1217 {"loc", s_mips_loc, 0},
252b5132
RH
1218 {"mask", s_mips_mask, 'R'},
1219 {"verstamp", s_ignore, 0},
43841e91 1220 { NULL, NULL, 0 },
252b5132
RH
1221};
1222
17a2f251 1223extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1224
1225void
17a2f251 1226mips_pop_insert (void)
252b5132
RH
1227{
1228 pop_insert (mips_pseudo_table);
1229 if (! ECOFF_DEBUGGING)
1230 pop_insert (mips_nonecoff_pseudo_table);
1231}
1232\f
1233/* Symbols labelling the current insn. */
1234
e972090a
NC
1235struct insn_label_list
1236{
252b5132
RH
1237 struct insn_label_list *next;
1238 symbolS *label;
1239};
1240
252b5132 1241static struct insn_label_list *free_insn_labels;
742a56fe 1242#define label_list tc_segment_info_data.labels
252b5132 1243
17a2f251 1244static void mips_clear_insn_labels (void);
252b5132
RH
1245
1246static inline void
17a2f251 1247mips_clear_insn_labels (void)
252b5132
RH
1248{
1249 register struct insn_label_list **pl;
a8dbcb85 1250 segment_info_type *si;
252b5132 1251
a8dbcb85
TS
1252 if (now_seg)
1253 {
1254 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1255 ;
1256
1257 si = seg_info (now_seg);
1258 *pl = si->label_list;
1259 si->label_list = NULL;
1260 }
252b5132 1261}
a8dbcb85 1262
252b5132
RH
1263\f
1264static char *expr_end;
1265
1266/* Expressions which appear in instructions. These are set by
1267 mips_ip. */
1268
1269static expressionS imm_expr;
5f74bc13 1270static expressionS imm2_expr;
252b5132
RH
1271static expressionS offset_expr;
1272
1273/* Relocs associated with imm_expr and offset_expr. */
1274
f6688943
TS
1275static bfd_reloc_code_real_type imm_reloc[3]
1276 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1277static bfd_reloc_code_real_type offset_reloc[3]
1278 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1279
252b5132
RH
1280/* These are set by mips16_ip if an explicit extension is used. */
1281
b34976b6 1282static bfd_boolean mips16_small, mips16_ext;
252b5132 1283
7ed4a06a 1284#ifdef OBJ_ELF
ecb4347a
DJ
1285/* The pdr segment for per procedure frame/regmask info. Not used for
1286 ECOFF debugging. */
252b5132
RH
1287
1288static segT pdr_seg;
7ed4a06a 1289#endif
252b5132 1290
e013f690
TS
1291/* The default target format to use. */
1292
1293const char *
17a2f251 1294mips_target_format (void)
e013f690
TS
1295{
1296 switch (OUTPUT_FLAVOR)
1297 {
e013f690
TS
1298 case bfd_target_ecoff_flavour:
1299 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1300 case bfd_target_coff_flavour:
1301 return "pe-mips";
1302 case bfd_target_elf_flavour:
0a44bf69
RS
1303#ifdef TE_VXWORKS
1304 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1305 return (target_big_endian
1306 ? "elf32-bigmips-vxworks"
1307 : "elf32-littlemips-vxworks");
1308#endif
e013f690 1309#ifdef TE_TMIPS
cfe86eaa 1310 /* This is traditional mips. */
e013f690 1311 return (target_big_endian
cfe86eaa
TS
1312 ? (HAVE_64BIT_OBJECTS
1313 ? "elf64-tradbigmips"
1314 : (HAVE_NEWABI
1315 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1316 : (HAVE_64BIT_OBJECTS
1317 ? "elf64-tradlittlemips"
1318 : (HAVE_NEWABI
1319 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1320#else
1321 return (target_big_endian
cfe86eaa
TS
1322 ? (HAVE_64BIT_OBJECTS
1323 ? "elf64-bigmips"
1324 : (HAVE_NEWABI
1325 ? "elf32-nbigmips" : "elf32-bigmips"))
1326 : (HAVE_64BIT_OBJECTS
1327 ? "elf64-littlemips"
1328 : (HAVE_NEWABI
1329 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1330#endif
1331 default:
1332 abort ();
1333 return NULL;
1334 }
1335}
1336
1e915849
RS
1337/* Return the length of instruction INSN. */
1338
1339static inline unsigned int
1340insn_length (const struct mips_cl_insn *insn)
1341{
1342 if (!mips_opts.mips16)
1343 return 4;
1344 return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1345}
1346
1347/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1348
1349static void
1350create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1351{
1352 size_t i;
1353
1354 insn->insn_mo = mo;
1355 insn->use_extend = FALSE;
1356 insn->extend = 0;
1357 insn->insn_opcode = mo->match;
1358 insn->frag = NULL;
1359 insn->where = 0;
1360 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1361 insn->fixp[i] = NULL;
1362 insn->fixed_p = (mips_opts.noreorder > 0);
1363 insn->noreorder_p = (mips_opts.noreorder > 0);
1364 insn->mips16_absolute_jump_p = 0;
1365}
1366
742a56fe
RS
1367/* Record the current MIPS16 mode in now_seg. */
1368
1369static void
1370mips_record_mips16_mode (void)
1371{
1372 segment_info_type *si;
1373
1374 si = seg_info (now_seg);
1375 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1376 si->tc_segment_info_data.mips16 = mips_opts.mips16;
1377}
1378
1e915849
RS
1379/* Install INSN at the location specified by its "frag" and "where" fields. */
1380
1381static void
1382install_insn (const struct mips_cl_insn *insn)
1383{
1384 char *f = insn->frag->fr_literal + insn->where;
1385 if (!mips_opts.mips16)
1386 md_number_to_chars (f, insn->insn_opcode, 4);
1387 else if (insn->mips16_absolute_jump_p)
1388 {
1389 md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1390 md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1391 }
1392 else
1393 {
1394 if (insn->use_extend)
1395 {
1396 md_number_to_chars (f, 0xf000 | insn->extend, 2);
1397 f += 2;
1398 }
1399 md_number_to_chars (f, insn->insn_opcode, 2);
1400 }
742a56fe 1401 mips_record_mips16_mode ();
1e915849
RS
1402}
1403
1404/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1405 and install the opcode in the new location. */
1406
1407static void
1408move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1409{
1410 size_t i;
1411
1412 insn->frag = frag;
1413 insn->where = where;
1414 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1415 if (insn->fixp[i] != NULL)
1416 {
1417 insn->fixp[i]->fx_frag = frag;
1418 insn->fixp[i]->fx_where = where;
1419 }
1420 install_insn (insn);
1421}
1422
1423/* Add INSN to the end of the output. */
1424
1425static void
1426add_fixed_insn (struct mips_cl_insn *insn)
1427{
1428 char *f = frag_more (insn_length (insn));
1429 move_insn (insn, frag_now, f - frag_now->fr_literal);
1430}
1431
1432/* Start a variant frag and move INSN to the start of the variant part,
1433 marking it as fixed. The other arguments are as for frag_var. */
1434
1435static void
1436add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1437 relax_substateT subtype, symbolS *symbol, offsetT offset)
1438{
1439 frag_grow (max_chars);
1440 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1441 insn->fixed_p = 1;
1442 frag_var (rs_machine_dependent, max_chars, var,
1443 subtype, symbol, offset, NULL);
1444}
1445
1446/* Insert N copies of INSN into the history buffer, starting at
1447 position FIRST. Neither FIRST nor N need to be clipped. */
1448
1449static void
1450insert_into_history (unsigned int first, unsigned int n,
1451 const struct mips_cl_insn *insn)
1452{
1453 if (mips_relax.sequence != 2)
1454 {
1455 unsigned int i;
1456
1457 for (i = ARRAY_SIZE (history); i-- > first;)
1458 if (i >= first + n)
1459 history[i] = history[i - n];
1460 else
1461 history[i] = *insn;
1462 }
1463}
1464
1465/* Emit a nop instruction, recording it in the history buffer. */
1466
1467static void
1468emit_nop (void)
1469{
1470 add_fixed_insn (NOP_INSN);
1471 insert_into_history (0, 1, NOP_INSN);
1472}
1473
71400594
RS
1474/* Initialize vr4120_conflicts. There is a bit of duplication here:
1475 the idea is to make it obvious at a glance that each errata is
1476 included. */
1477
1478static void
1479init_vr4120_conflicts (void)
1480{
1481#define CONFLICT(FIRST, SECOND) \
1482 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1483
1484 /* Errata 21 - [D]DIV[U] after [D]MACC */
1485 CONFLICT (MACC, DIV);
1486 CONFLICT (DMACC, DIV);
1487
1488 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1489 CONFLICT (DMULT, DMULT);
1490 CONFLICT (DMULT, DMACC);
1491 CONFLICT (DMACC, DMULT);
1492 CONFLICT (DMACC, DMACC);
1493
1494 /* Errata 24 - MT{LO,HI} after [D]MACC */
1495 CONFLICT (MACC, MTHILO);
1496 CONFLICT (DMACC, MTHILO);
1497
1498 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1499 instruction is executed immediately after a MACC or DMACC
1500 instruction, the result of [either instruction] is incorrect." */
1501 CONFLICT (MACC, MULT);
1502 CONFLICT (MACC, DMULT);
1503 CONFLICT (DMACC, MULT);
1504 CONFLICT (DMACC, DMULT);
1505
1506 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1507 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1508 DDIV or DDIVU instruction, the result of the MACC or
1509 DMACC instruction is incorrect.". */
1510 CONFLICT (DMULT, MACC);
1511 CONFLICT (DMULT, DMACC);
1512 CONFLICT (DIV, MACC);
1513 CONFLICT (DIV, DMACC);
1514
1515#undef CONFLICT
1516}
1517
707bfff6
TS
1518struct regname {
1519 const char *name;
1520 unsigned int num;
1521};
1522
1523#define RTYPE_MASK 0x1ff00
1524#define RTYPE_NUM 0x00100
1525#define RTYPE_FPU 0x00200
1526#define RTYPE_FCC 0x00400
1527#define RTYPE_VEC 0x00800
1528#define RTYPE_GP 0x01000
1529#define RTYPE_CP0 0x02000
1530#define RTYPE_PC 0x04000
1531#define RTYPE_ACC 0x08000
1532#define RTYPE_CCC 0x10000
1533#define RNUM_MASK 0x000ff
1534#define RWARN 0x80000
1535
1536#define GENERIC_REGISTER_NUMBERS \
1537 {"$0", RTYPE_NUM | 0}, \
1538 {"$1", RTYPE_NUM | 1}, \
1539 {"$2", RTYPE_NUM | 2}, \
1540 {"$3", RTYPE_NUM | 3}, \
1541 {"$4", RTYPE_NUM | 4}, \
1542 {"$5", RTYPE_NUM | 5}, \
1543 {"$6", RTYPE_NUM | 6}, \
1544 {"$7", RTYPE_NUM | 7}, \
1545 {"$8", RTYPE_NUM | 8}, \
1546 {"$9", RTYPE_NUM | 9}, \
1547 {"$10", RTYPE_NUM | 10}, \
1548 {"$11", RTYPE_NUM | 11}, \
1549 {"$12", RTYPE_NUM | 12}, \
1550 {"$13", RTYPE_NUM | 13}, \
1551 {"$14", RTYPE_NUM | 14}, \
1552 {"$15", RTYPE_NUM | 15}, \
1553 {"$16", RTYPE_NUM | 16}, \
1554 {"$17", RTYPE_NUM | 17}, \
1555 {"$18", RTYPE_NUM | 18}, \
1556 {"$19", RTYPE_NUM | 19}, \
1557 {"$20", RTYPE_NUM | 20}, \
1558 {"$21", RTYPE_NUM | 21}, \
1559 {"$22", RTYPE_NUM | 22}, \
1560 {"$23", RTYPE_NUM | 23}, \
1561 {"$24", RTYPE_NUM | 24}, \
1562 {"$25", RTYPE_NUM | 25}, \
1563 {"$26", RTYPE_NUM | 26}, \
1564 {"$27", RTYPE_NUM | 27}, \
1565 {"$28", RTYPE_NUM | 28}, \
1566 {"$29", RTYPE_NUM | 29}, \
1567 {"$30", RTYPE_NUM | 30}, \
1568 {"$31", RTYPE_NUM | 31}
1569
1570#define FPU_REGISTER_NAMES \
1571 {"$f0", RTYPE_FPU | 0}, \
1572 {"$f1", RTYPE_FPU | 1}, \
1573 {"$f2", RTYPE_FPU | 2}, \
1574 {"$f3", RTYPE_FPU | 3}, \
1575 {"$f4", RTYPE_FPU | 4}, \
1576 {"$f5", RTYPE_FPU | 5}, \
1577 {"$f6", RTYPE_FPU | 6}, \
1578 {"$f7", RTYPE_FPU | 7}, \
1579 {"$f8", RTYPE_FPU | 8}, \
1580 {"$f9", RTYPE_FPU | 9}, \
1581 {"$f10", RTYPE_FPU | 10}, \
1582 {"$f11", RTYPE_FPU | 11}, \
1583 {"$f12", RTYPE_FPU | 12}, \
1584 {"$f13", RTYPE_FPU | 13}, \
1585 {"$f14", RTYPE_FPU | 14}, \
1586 {"$f15", RTYPE_FPU | 15}, \
1587 {"$f16", RTYPE_FPU | 16}, \
1588 {"$f17", RTYPE_FPU | 17}, \
1589 {"$f18", RTYPE_FPU | 18}, \
1590 {"$f19", RTYPE_FPU | 19}, \
1591 {"$f20", RTYPE_FPU | 20}, \
1592 {"$f21", RTYPE_FPU | 21}, \
1593 {"$f22", RTYPE_FPU | 22}, \
1594 {"$f23", RTYPE_FPU | 23}, \
1595 {"$f24", RTYPE_FPU | 24}, \
1596 {"$f25", RTYPE_FPU | 25}, \
1597 {"$f26", RTYPE_FPU | 26}, \
1598 {"$f27", RTYPE_FPU | 27}, \
1599 {"$f28", RTYPE_FPU | 28}, \
1600 {"$f29", RTYPE_FPU | 29}, \
1601 {"$f30", RTYPE_FPU | 30}, \
1602 {"$f31", RTYPE_FPU | 31}
1603
1604#define FPU_CONDITION_CODE_NAMES \
1605 {"$fcc0", RTYPE_FCC | 0}, \
1606 {"$fcc1", RTYPE_FCC | 1}, \
1607 {"$fcc2", RTYPE_FCC | 2}, \
1608 {"$fcc3", RTYPE_FCC | 3}, \
1609 {"$fcc4", RTYPE_FCC | 4}, \
1610 {"$fcc5", RTYPE_FCC | 5}, \
1611 {"$fcc6", RTYPE_FCC | 6}, \
1612 {"$fcc7", RTYPE_FCC | 7}
1613
1614#define COPROC_CONDITION_CODE_NAMES \
1615 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
1616 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
1617 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
1618 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
1619 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
1620 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
1621 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
1622 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
1623
1624#define N32N64_SYMBOLIC_REGISTER_NAMES \
1625 {"$a4", RTYPE_GP | 8}, \
1626 {"$a5", RTYPE_GP | 9}, \
1627 {"$a6", RTYPE_GP | 10}, \
1628 {"$a7", RTYPE_GP | 11}, \
1629 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
1630 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
1631 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
1632 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
1633 {"$t0", RTYPE_GP | 12}, \
1634 {"$t1", RTYPE_GP | 13}, \
1635 {"$t2", RTYPE_GP | 14}, \
1636 {"$t3", RTYPE_GP | 15}
1637
1638#define O32_SYMBOLIC_REGISTER_NAMES \
1639 {"$t0", RTYPE_GP | 8}, \
1640 {"$t1", RTYPE_GP | 9}, \
1641 {"$t2", RTYPE_GP | 10}, \
1642 {"$t3", RTYPE_GP | 11}, \
1643 {"$t4", RTYPE_GP | 12}, \
1644 {"$t5", RTYPE_GP | 13}, \
1645 {"$t6", RTYPE_GP | 14}, \
1646 {"$t7", RTYPE_GP | 15}, \
1647 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
1648 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
1649 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
1650 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
1651
1652/* Remaining symbolic register names */
1653#define SYMBOLIC_REGISTER_NAMES \
1654 {"$zero", RTYPE_GP | 0}, \
1655 {"$at", RTYPE_GP | 1}, \
1656 {"$AT", RTYPE_GP | 1}, \
1657 {"$v0", RTYPE_GP | 2}, \
1658 {"$v1", RTYPE_GP | 3}, \
1659 {"$a0", RTYPE_GP | 4}, \
1660 {"$a1", RTYPE_GP | 5}, \
1661 {"$a2", RTYPE_GP | 6}, \
1662 {"$a3", RTYPE_GP | 7}, \
1663 {"$s0", RTYPE_GP | 16}, \
1664 {"$s1", RTYPE_GP | 17}, \
1665 {"$s2", RTYPE_GP | 18}, \
1666 {"$s3", RTYPE_GP | 19}, \
1667 {"$s4", RTYPE_GP | 20}, \
1668 {"$s5", RTYPE_GP | 21}, \
1669 {"$s6", RTYPE_GP | 22}, \
1670 {"$s7", RTYPE_GP | 23}, \
1671 {"$t8", RTYPE_GP | 24}, \
1672 {"$t9", RTYPE_GP | 25}, \
1673 {"$k0", RTYPE_GP | 26}, \
1674 {"$kt0", RTYPE_GP | 26}, \
1675 {"$k1", RTYPE_GP | 27}, \
1676 {"$kt1", RTYPE_GP | 27}, \
1677 {"$gp", RTYPE_GP | 28}, \
1678 {"$sp", RTYPE_GP | 29}, \
1679 {"$s8", RTYPE_GP | 30}, \
1680 {"$fp", RTYPE_GP | 30}, \
1681 {"$ra", RTYPE_GP | 31}
1682
1683#define MIPS16_SPECIAL_REGISTER_NAMES \
1684 {"$pc", RTYPE_PC | 0}
1685
1686#define MDMX_VECTOR_REGISTER_NAMES \
1687 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
1688 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
1689 {"$v2", RTYPE_VEC | 2}, \
1690 {"$v3", RTYPE_VEC | 3}, \
1691 {"$v4", RTYPE_VEC | 4}, \
1692 {"$v5", RTYPE_VEC | 5}, \
1693 {"$v6", RTYPE_VEC | 6}, \
1694 {"$v7", RTYPE_VEC | 7}, \
1695 {"$v8", RTYPE_VEC | 8}, \
1696 {"$v9", RTYPE_VEC | 9}, \
1697 {"$v10", RTYPE_VEC | 10}, \
1698 {"$v11", RTYPE_VEC | 11}, \
1699 {"$v12", RTYPE_VEC | 12}, \
1700 {"$v13", RTYPE_VEC | 13}, \
1701 {"$v14", RTYPE_VEC | 14}, \
1702 {"$v15", RTYPE_VEC | 15}, \
1703 {"$v16", RTYPE_VEC | 16}, \
1704 {"$v17", RTYPE_VEC | 17}, \
1705 {"$v18", RTYPE_VEC | 18}, \
1706 {"$v19", RTYPE_VEC | 19}, \
1707 {"$v20", RTYPE_VEC | 20}, \
1708 {"$v21", RTYPE_VEC | 21}, \
1709 {"$v22", RTYPE_VEC | 22}, \
1710 {"$v23", RTYPE_VEC | 23}, \
1711 {"$v24", RTYPE_VEC | 24}, \
1712 {"$v25", RTYPE_VEC | 25}, \
1713 {"$v26", RTYPE_VEC | 26}, \
1714 {"$v27", RTYPE_VEC | 27}, \
1715 {"$v28", RTYPE_VEC | 28}, \
1716 {"$v29", RTYPE_VEC | 29}, \
1717 {"$v30", RTYPE_VEC | 30}, \
1718 {"$v31", RTYPE_VEC | 31}
1719
1720#define MIPS_DSP_ACCUMULATOR_NAMES \
1721 {"$ac0", RTYPE_ACC | 0}, \
1722 {"$ac1", RTYPE_ACC | 1}, \
1723 {"$ac2", RTYPE_ACC | 2}, \
1724 {"$ac3", RTYPE_ACC | 3}
1725
1726static const struct regname reg_names[] = {
1727 GENERIC_REGISTER_NUMBERS,
1728 FPU_REGISTER_NAMES,
1729 FPU_CONDITION_CODE_NAMES,
1730 COPROC_CONDITION_CODE_NAMES,
1731
1732 /* The $txx registers depends on the abi,
1733 these will be added later into the symbol table from
1734 one of the tables below once mips_abi is set after
1735 parsing of arguments from the command line. */
1736 SYMBOLIC_REGISTER_NAMES,
1737
1738 MIPS16_SPECIAL_REGISTER_NAMES,
1739 MDMX_VECTOR_REGISTER_NAMES,
1740 MIPS_DSP_ACCUMULATOR_NAMES,
1741 {0, 0}
1742};
1743
1744static const struct regname reg_names_o32[] = {
1745 O32_SYMBOLIC_REGISTER_NAMES,
1746 {0, 0}
1747};
1748
1749static const struct regname reg_names_n32n64[] = {
1750 N32N64_SYMBOLIC_REGISTER_NAMES,
1751 {0, 0}
1752};
1753
1754static int
1755reg_lookup (char **s, unsigned int types, unsigned int *regnop)
1756{
1757 symbolS *symbolP;
1758 char *e;
1759 char save_c;
1760 int reg = -1;
1761
1762 /* Find end of name. */
1763 e = *s;
1764 if (is_name_beginner (*e))
1765 ++e;
1766 while (is_part_of_name (*e))
1767 ++e;
1768
1769 /* Terminate name. */
1770 save_c = *e;
1771 *e = '\0';
1772
1773 /* Look for a register symbol. */
1774 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
1775 {
1776 int r = S_GET_VALUE (symbolP);
1777 if (r & types)
1778 reg = r & RNUM_MASK;
1779 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
1780 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
1781 reg = (r & RNUM_MASK) - 2;
1782 }
1783 /* Else see if this is a register defined in an itbl entry. */
1784 else if ((types & RTYPE_GP) && itbl_have_entries)
1785 {
1786 char *n = *s;
1787 unsigned long r;
1788
1789 if (*n == '$')
1790 ++n;
1791 if (itbl_get_reg_val (n, &r))
1792 reg = r & RNUM_MASK;
1793 }
1794
1795 /* Advance to next token if a register was recognised. */
1796 if (reg >= 0)
1797 *s = e;
1798 else if (types & RWARN)
20203fb9 1799 as_warn (_("Unrecognized register name `%s'"), *s);
707bfff6
TS
1800
1801 *e = save_c;
1802 if (regnop)
1803 *regnop = reg;
1804 return reg >= 0;
1805}
1806
037b32b9
AN
1807/* Return TRUE if opcode MO is valid on the currently selected ISA and
1808 architecture. If EXPANSIONP is TRUE then this check is done while
1809 expanding a macro. Use is_opcode_valid_16 for MIPS16 opcodes. */
1810
1811static bfd_boolean
1812is_opcode_valid (const struct mips_opcode *mo, bfd_boolean expansionp)
1813{
1814 int isa = mips_opts.isa;
1815 int fp_s, fp_d;
1816
1817 if (mips_opts.ase_mdmx)
1818 isa |= INSN_MDMX;
1819 if (mips_opts.ase_dsp)
1820 isa |= INSN_DSP;
1821 if (mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
1822 isa |= INSN_DSP64;
1823 if (mips_opts.ase_dspr2)
1824 isa |= INSN_DSPR2;
1825 if (mips_opts.ase_mt)
1826 isa |= INSN_MT;
1827 if (mips_opts.ase_mips3d)
1828 isa |= INSN_MIPS3D;
1829 if (mips_opts.ase_smartmips)
1830 isa |= INSN_SMARTMIPS;
1831
1832 /* For user code we don't check for mips_opts.mips16 since we want
1833 to allow jalx if -mips16 was specified on the command line. */
1834 if (expansionp ? mips_opts.mips16 : file_ase_mips16)
1835 isa |= INSN_MIPS16;
1836
b19e8a9b
AN
1837 /* Don't accept instructions based on the ISA if the CPU does not implement
1838 all the coprocessor insns. */
1839 if (NO_ISA_COP (mips_opts.arch)
1840 && COP_INSN (mo->pinfo))
1841 isa = 0;
1842
037b32b9
AN
1843 if (!OPCODE_IS_MEMBER (mo, isa, mips_opts.arch))
1844 return FALSE;
1845
1846 /* Check whether the instruction or macro requires single-precision or
1847 double-precision floating-point support. Note that this information is
1848 stored differently in the opcode table for insns and macros. */
1849 if (mo->pinfo == INSN_MACRO)
1850 {
1851 fp_s = mo->pinfo2 & INSN2_M_FP_S;
1852 fp_d = mo->pinfo2 & INSN2_M_FP_D;
1853 }
1854 else
1855 {
1856 fp_s = mo->pinfo & FP_S;
1857 fp_d = mo->pinfo & FP_D;
1858 }
1859
1860 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
1861 return FALSE;
1862
1863 if (fp_s && mips_opts.soft_float)
1864 return FALSE;
1865
1866 return TRUE;
1867}
1868
1869/* Return TRUE if the MIPS16 opcode MO is valid on the currently
1870 selected ISA and architecture. */
1871
1872static bfd_boolean
1873is_opcode_valid_16 (const struct mips_opcode *mo)
1874{
1875 return OPCODE_IS_MEMBER (mo, mips_opts.isa, mips_opts.arch) ? TRUE : FALSE;
1876}
1877
707bfff6
TS
1878/* This function is called once, at assembler startup time. It should set up
1879 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 1880
252b5132 1881void
17a2f251 1882md_begin (void)
252b5132 1883{
3994f87e 1884 const char *retval = NULL;
156c2f8b 1885 int i = 0;
252b5132 1886 int broken = 0;
1f25f5d3 1887
0a44bf69
RS
1888 if (mips_pic != NO_PIC)
1889 {
1890 if (g_switch_seen && g_switch_value != 0)
1891 as_bad (_("-G may not be used in position-independent code"));
1892 g_switch_value = 0;
1893 }
1894
fef14a42 1895 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
1896 as_warn (_("Could not set architecture and machine"));
1897
252b5132
RH
1898 op_hash = hash_new ();
1899
1900 for (i = 0; i < NUMOPCODES;)
1901 {
1902 const char *name = mips_opcodes[i].name;
1903
17a2f251 1904 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
1905 if (retval != NULL)
1906 {
1907 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1908 mips_opcodes[i].name, retval);
1909 /* Probably a memory allocation problem? Give up now. */
1910 as_fatal (_("Broken assembler. No assembly attempted."));
1911 }
1912 do
1913 {
1914 if (mips_opcodes[i].pinfo != INSN_MACRO)
1915 {
1916 if (!validate_mips_insn (&mips_opcodes[i]))
1917 broken = 1;
1e915849
RS
1918 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1919 {
1920 create_insn (&nop_insn, mips_opcodes + i);
1921 nop_insn.fixed_p = 1;
1922 }
252b5132
RH
1923 }
1924 ++i;
1925 }
1926 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1927 }
1928
1929 mips16_op_hash = hash_new ();
1930
1931 i = 0;
1932 while (i < bfd_mips16_num_opcodes)
1933 {
1934 const char *name = mips16_opcodes[i].name;
1935
17a2f251 1936 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
1937 if (retval != NULL)
1938 as_fatal (_("internal: can't hash `%s': %s"),
1939 mips16_opcodes[i].name, retval);
1940 do
1941 {
1942 if (mips16_opcodes[i].pinfo != INSN_MACRO
1943 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1944 != mips16_opcodes[i].match))
1945 {
1946 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1947 mips16_opcodes[i].name, mips16_opcodes[i].args);
1948 broken = 1;
1949 }
1e915849
RS
1950 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1951 {
1952 create_insn (&mips16_nop_insn, mips16_opcodes + i);
1953 mips16_nop_insn.fixed_p = 1;
1954 }
252b5132
RH
1955 ++i;
1956 }
1957 while (i < bfd_mips16_num_opcodes
1958 && strcmp (mips16_opcodes[i].name, name) == 0);
1959 }
1960
1961 if (broken)
1962 as_fatal (_("Broken assembler. No assembly attempted."));
1963
1964 /* We add all the general register names to the symbol table. This
1965 helps us detect invalid uses of them. */
707bfff6
TS
1966 for (i = 0; reg_names[i].name; i++)
1967 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 1968 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
1969 &zero_address_frag));
1970 if (HAVE_NEWABI)
1971 for (i = 0; reg_names_n32n64[i].name; i++)
1972 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 1973 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 1974 &zero_address_frag));
707bfff6
TS
1975 else
1976 for (i = 0; reg_names_o32[i].name; i++)
1977 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 1978 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 1979 &zero_address_frag));
6047c971 1980
7d10b47d 1981 mips_no_prev_insn ();
252b5132
RH
1982
1983 mips_gprmask = 0;
1984 mips_cprmask[0] = 0;
1985 mips_cprmask[1] = 0;
1986 mips_cprmask[2] = 0;
1987 mips_cprmask[3] = 0;
1988
1989 /* set the default alignment for the text section (2**2) */
1990 record_alignment (text_section, 2);
1991
4d0d148d 1992 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 1993
707bfff6 1994#ifdef OBJ_ELF
f43abd2b 1995 if (IS_ELF)
252b5132 1996 {
0a44bf69
RS
1997 /* On a native system other than VxWorks, sections must be aligned
1998 to 16 byte boundaries. When configured for an embedded ELF
1999 target, we don't bother. */
c41e87e3
CF
2000 if (strncmp (TARGET_OS, "elf", 3) != 0
2001 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132
RH
2002 {
2003 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2004 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2005 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2006 }
2007
2008 /* Create a .reginfo section for register masks and a .mdebug
2009 section for debugging information. */
2010 {
2011 segT seg;
2012 subsegT subseg;
2013 flagword flags;
2014 segT sec;
2015
2016 seg = now_seg;
2017 subseg = now_subseg;
2018
2019 /* The ABI says this section should be loaded so that the
2020 running program can access it. However, we don't load it
2021 if we are configured for an embedded target */
2022 flags = SEC_READONLY | SEC_DATA;
c41e87e3 2023 if (strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
2024 flags |= SEC_ALLOC | SEC_LOAD;
2025
316f5878 2026 if (mips_abi != N64_ABI)
252b5132
RH
2027 {
2028 sec = subseg_new (".reginfo", (subsegT) 0);
2029
195325d2
TS
2030 bfd_set_section_flags (stdoutput, sec, flags);
2031 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 2032
252b5132 2033 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
252b5132
RH
2034 }
2035 else
2036 {
2037 /* The 64-bit ABI uses a .MIPS.options section rather than
2038 .reginfo section. */
2039 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
2040 bfd_set_section_flags (stdoutput, sec, flags);
2041 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 2042
252b5132
RH
2043 /* Set up the option header. */
2044 {
2045 Elf_Internal_Options opthdr;
2046 char *f;
2047
2048 opthdr.kind = ODK_REGINFO;
2049 opthdr.size = (sizeof (Elf_External_Options)
2050 + sizeof (Elf64_External_RegInfo));
2051 opthdr.section = 0;
2052 opthdr.info = 0;
2053 f = frag_more (sizeof (Elf_External_Options));
2054 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2055 (Elf_External_Options *) f);
2056
2057 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2058 }
252b5132
RH
2059 }
2060
2061 if (ECOFF_DEBUGGING)
2062 {
2063 sec = subseg_new (".mdebug", (subsegT) 0);
2064 (void) bfd_set_section_flags (stdoutput, sec,
2065 SEC_HAS_CONTENTS | SEC_READONLY);
2066 (void) bfd_set_section_alignment (stdoutput, sec, 2);
2067 }
f43abd2b 2068 else if (mips_flag_pdr)
ecb4347a
DJ
2069 {
2070 pdr_seg = subseg_new (".pdr", (subsegT) 0);
2071 (void) bfd_set_section_flags (stdoutput, pdr_seg,
2072 SEC_READONLY | SEC_RELOC
2073 | SEC_DEBUGGING);
2074 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2075 }
252b5132
RH
2076
2077 subseg_set (seg, subseg);
2078 }
2079 }
707bfff6 2080#endif /* OBJ_ELF */
252b5132
RH
2081
2082 if (! ECOFF_DEBUGGING)
2083 md_obj_begin ();
71400594
RS
2084
2085 if (mips_fix_vr4120)
2086 init_vr4120_conflicts ();
252b5132
RH
2087}
2088
2089void
17a2f251 2090md_mips_end (void)
252b5132
RH
2091{
2092 if (! ECOFF_DEBUGGING)
2093 md_obj_end ();
2094}
2095
2096void
17a2f251 2097md_assemble (char *str)
252b5132
RH
2098{
2099 struct mips_cl_insn insn;
f6688943
TS
2100 bfd_reloc_code_real_type unused_reloc[3]
2101 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
2102
2103 imm_expr.X_op = O_absent;
5f74bc13 2104 imm2_expr.X_op = O_absent;
252b5132 2105 offset_expr.X_op = O_absent;
f6688943
TS
2106 imm_reloc[0] = BFD_RELOC_UNUSED;
2107 imm_reloc[1] = BFD_RELOC_UNUSED;
2108 imm_reloc[2] = BFD_RELOC_UNUSED;
2109 offset_reloc[0] = BFD_RELOC_UNUSED;
2110 offset_reloc[1] = BFD_RELOC_UNUSED;
2111 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
2112
2113 if (mips_opts.mips16)
2114 mips16_ip (str, &insn);
2115 else
2116 {
2117 mips_ip (str, &insn);
beae10d5
KH
2118 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2119 str, insn.insn_opcode));
252b5132
RH
2120 }
2121
2122 if (insn_error)
2123 {
2124 as_bad ("%s `%s'", insn_error, str);
2125 return;
2126 }
2127
2128 if (insn.insn_mo->pinfo == INSN_MACRO)
2129 {
584892a6 2130 macro_start ();
252b5132
RH
2131 if (mips_opts.mips16)
2132 mips16_macro (&insn);
2133 else
2134 macro (&insn);
584892a6 2135 macro_end ();
252b5132
RH
2136 }
2137 else
2138 {
2139 if (imm_expr.X_op != O_absent)
4d7206a2 2140 append_insn (&insn, &imm_expr, imm_reloc);
252b5132 2141 else if (offset_expr.X_op != O_absent)
4d7206a2 2142 append_insn (&insn, &offset_expr, offset_reloc);
252b5132 2143 else
4d7206a2 2144 append_insn (&insn, NULL, unused_reloc);
252b5132
RH
2145 }
2146}
2147
738e5348
RS
2148/* Convenience functions for abstracting away the differences between
2149 MIPS16 and non-MIPS16 relocations. */
2150
2151static inline bfd_boolean
2152mips16_reloc_p (bfd_reloc_code_real_type reloc)
2153{
2154 switch (reloc)
2155 {
2156 case BFD_RELOC_MIPS16_JMP:
2157 case BFD_RELOC_MIPS16_GPREL:
2158 case BFD_RELOC_MIPS16_GOT16:
2159 case BFD_RELOC_MIPS16_CALL16:
2160 case BFD_RELOC_MIPS16_HI16_S:
2161 case BFD_RELOC_MIPS16_HI16:
2162 case BFD_RELOC_MIPS16_LO16:
2163 return TRUE;
2164
2165 default:
2166 return FALSE;
2167 }
2168}
2169
2170static inline bfd_boolean
2171got16_reloc_p (bfd_reloc_code_real_type reloc)
2172{
2173 return reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16;
2174}
2175
2176static inline bfd_boolean
2177hi16_reloc_p (bfd_reloc_code_real_type reloc)
2178{
2179 return reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S;
2180}
2181
2182static inline bfd_boolean
2183lo16_reloc_p (bfd_reloc_code_real_type reloc)
2184{
2185 return reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16;
2186}
2187
5919d012 2188/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
2189 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
2190 need a matching %lo() when applied to local symbols. */
5919d012
RS
2191
2192static inline bfd_boolean
17a2f251 2193reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 2194{
3b91255e 2195 return (HAVE_IN_PLACE_ADDENDS
738e5348 2196 && (hi16_reloc_p (reloc)
0a44bf69
RS
2197 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2198 all GOT16 relocations evaluate to "G". */
738e5348
RS
2199 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
2200}
2201
2202/* Return the type of %lo() reloc needed by RELOC, given that
2203 reloc_needs_lo_p. */
2204
2205static inline bfd_reloc_code_real_type
2206matching_lo_reloc (bfd_reloc_code_real_type reloc)
2207{
2208 return mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16 : BFD_RELOC_LO16;
5919d012
RS
2209}
2210
2211/* Return true if the given fixup is followed by a matching R_MIPS_LO16
2212 relocation. */
2213
2214static inline bfd_boolean
17a2f251 2215fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
2216{
2217 return (fixp->fx_next != NULL
738e5348 2218 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
2219 && fixp->fx_addsy == fixp->fx_next->fx_addsy
2220 && fixp->fx_offset == fixp->fx_next->fx_offset);
2221}
2222
252b5132
RH
2223/* See whether instruction IP reads register REG. CLASS is the type
2224 of register. */
2225
2226static int
71400594 2227insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg,
96d56e9f 2228 enum mips_regclass regclass)
252b5132 2229{
96d56e9f 2230 if (regclass == MIPS16_REG)
252b5132 2231 {
9c2799c2 2232 gas_assert (mips_opts.mips16);
252b5132 2233 reg = mips16_to_32_reg_map[reg];
96d56e9f 2234 regclass = MIPS_GR_REG;
252b5132
RH
2235 }
2236
85b51719 2237 /* Don't report on general register ZERO, since it never changes. */
96d56e9f 2238 if (regclass == MIPS_GR_REG && reg == ZERO)
252b5132
RH
2239 return 0;
2240
96d56e9f 2241 if (regclass == MIPS_FP_REG)
252b5132 2242 {
9c2799c2 2243 gas_assert (! mips_opts.mips16);
252b5132
RH
2244 /* If we are called with either $f0 or $f1, we must check $f0.
2245 This is not optimal, because it will introduce an unnecessary
2246 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
2247 need to distinguish reading both $f0 and $f1 or just one of
2248 them. Note that we don't have to check the other way,
2249 because there is no instruction that sets both $f0 and $f1
2250 and requires a delay. */
2251 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
bf12938e 2252 && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1)
252b5132
RH
2253 == (reg &~ (unsigned) 1)))
2254 return 1;
2255 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
bf12938e 2256 && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1)
252b5132
RH
2257 == (reg &~ (unsigned) 1)))
2258 return 1;
2259 }
2260 else if (! mips_opts.mips16)
2261 {
2262 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
bf12938e 2263 && EXTRACT_OPERAND (RS, *ip) == reg)
252b5132
RH
2264 return 1;
2265 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
bf12938e 2266 && EXTRACT_OPERAND (RT, *ip) == reg)
252b5132
RH
2267 return 1;
2268 }
2269 else
2270 {
2271 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
bf12938e 2272 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg)
252b5132
RH
2273 return 1;
2274 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
bf12938e 2275 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg)
252b5132
RH
2276 return 1;
2277 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
bf12938e 2278 && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]
252b5132
RH
2279 == reg))
2280 return 1;
2281 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
2282 return 1;
2283 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
2284 return 1;
2285 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
2286 return 1;
2287 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
bf12938e 2288 && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg)
252b5132
RH
2289 return 1;
2290 }
2291
2292 return 0;
2293}
2294
2295/* This function returns true if modifying a register requires a
2296 delay. */
2297
2298static int
17a2f251 2299reg_needs_delay (unsigned int reg)
252b5132
RH
2300{
2301 unsigned long prev_pinfo;
2302
47e39b9d 2303 prev_pinfo = history[0].insn_mo->pinfo;
252b5132 2304 if (! mips_opts.noreorder
81912461
ILT
2305 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2306 && ! gpr_interlocks)
2307 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2308 && ! cop_interlocks)))
252b5132 2309 {
81912461
ILT
2310 /* A load from a coprocessor or from memory. All load delays
2311 delay the use of general register rt for one instruction. */
bdaaa2e1 2312 /* Itbl support may require additional care here. */
252b5132 2313 know (prev_pinfo & INSN_WRITE_GPR_T);
bf12938e 2314 if (reg == EXTRACT_OPERAND (RT, history[0]))
252b5132
RH
2315 return 1;
2316 }
2317
2318 return 0;
2319}
2320
404a8071
RS
2321/* Move all labels in insn_labels to the current insertion point. */
2322
2323static void
2324mips_move_labels (void)
2325{
a8dbcb85 2326 segment_info_type *si = seg_info (now_seg);
404a8071
RS
2327 struct insn_label_list *l;
2328 valueT val;
2329
a8dbcb85 2330 for (l = si->label_list; l != NULL; l = l->next)
404a8071 2331 {
9c2799c2 2332 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
2333 symbol_set_frag (l->label, frag_now);
2334 val = (valueT) frag_now_fix ();
2335 /* mips16 text labels are stored as odd. */
2336 if (mips_opts.mips16)
2337 ++val;
2338 S_SET_VALUE (l->label, val);
2339 }
2340}
2341
5f0fe04b
TS
2342static bfd_boolean
2343s_is_linkonce (symbolS *sym, segT from_seg)
2344{
2345 bfd_boolean linkonce = FALSE;
2346 segT symseg = S_GET_SEGMENT (sym);
2347
2348 if (symseg != from_seg && !S_IS_LOCAL (sym))
2349 {
2350 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2351 linkonce = TRUE;
2352#ifdef OBJ_ELF
2353 /* The GNU toolchain uses an extension for ELF: a section
2354 beginning with the magic string .gnu.linkonce is a
2355 linkonce section. */
2356 if (strncmp (segment_name (symseg), ".gnu.linkonce",
2357 sizeof ".gnu.linkonce" - 1) == 0)
2358 linkonce = TRUE;
2359#endif
2360 }
2361 return linkonce;
2362}
2363
252b5132
RH
2364/* Mark instruction labels in mips16 mode. This permits the linker to
2365 handle them specially, such as generating jalx instructions when
2366 needed. We also make them odd for the duration of the assembly, in
2367 order to generate the right sort of code. We will make them even
2368 in the adjust_symtab routine, while leaving them marked. This is
2369 convenient for the debugger and the disassembler. The linker knows
2370 to make them odd again. */
2371
2372static void
17a2f251 2373mips16_mark_labels (void)
252b5132 2374{
a8dbcb85
TS
2375 segment_info_type *si = seg_info (now_seg);
2376 struct insn_label_list *l;
252b5132 2377
a8dbcb85
TS
2378 if (!mips_opts.mips16)
2379 return;
2380
2381 for (l = si->label_list; l != NULL; l = l->next)
2382 {
2383 symbolS *label = l->label;
2384
2385#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
f43abd2b 2386 if (IS_ELF)
30c09090 2387 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
252b5132 2388#endif
5f0fe04b
TS
2389 if ((S_GET_VALUE (label) & 1) == 0
2390 /* Don't adjust the address if the label is global or weak, or
2391 in a link-once section, since we'll be emitting symbol reloc
2392 references to it which will be patched up by the linker, and
2393 the final value of the symbol may or may not be MIPS16. */
2394 && ! S_IS_WEAK (label)
2395 && ! S_IS_EXTERNAL (label)
2396 && ! s_is_linkonce (label, now_seg))
a8dbcb85 2397 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
252b5132
RH
2398 }
2399}
2400
4d7206a2
RS
2401/* End the current frag. Make it a variant frag and record the
2402 relaxation info. */
2403
2404static void
2405relax_close_frag (void)
2406{
584892a6 2407 mips_macro_warning.first_frag = frag_now;
4d7206a2 2408 frag_var (rs_machine_dependent, 0, 0,
584892a6 2409 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
2410 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2411
2412 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2413 mips_relax.first_fixup = 0;
2414}
2415
2416/* Start a new relaxation sequence whose expansion depends on SYMBOL.
2417 See the comment above RELAX_ENCODE for more details. */
2418
2419static void
2420relax_start (symbolS *symbol)
2421{
9c2799c2 2422 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
2423 mips_relax.sequence = 1;
2424 mips_relax.symbol = symbol;
2425}
2426
2427/* Start generating the second version of a relaxable sequence.
2428 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
2429
2430static void
4d7206a2
RS
2431relax_switch (void)
2432{
9c2799c2 2433 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
2434 mips_relax.sequence = 2;
2435}
2436
2437/* End the current relaxable sequence. */
2438
2439static void
2440relax_end (void)
2441{
9c2799c2 2442 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
2443 relax_close_frag ();
2444 mips_relax.sequence = 0;
2445}
2446
71400594
RS
2447/* Classify an instruction according to the FIX_VR4120_* enumeration.
2448 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
2449 by VR4120 errata. */
4d7206a2 2450
71400594
RS
2451static unsigned int
2452classify_vr4120_insn (const char *name)
252b5132 2453{
71400594
RS
2454 if (strncmp (name, "macc", 4) == 0)
2455 return FIX_VR4120_MACC;
2456 if (strncmp (name, "dmacc", 5) == 0)
2457 return FIX_VR4120_DMACC;
2458 if (strncmp (name, "mult", 4) == 0)
2459 return FIX_VR4120_MULT;
2460 if (strncmp (name, "dmult", 5) == 0)
2461 return FIX_VR4120_DMULT;
2462 if (strstr (name, "div"))
2463 return FIX_VR4120_DIV;
2464 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
2465 return FIX_VR4120_MTHILO;
2466 return NUM_FIX_VR4120_CLASSES;
2467}
252b5132 2468
ff239038
CM
2469#define INSN_ERET 0x42000018
2470#define INSN_DERET 0x4200001f
2471
71400594
RS
2472/* Return the number of instructions that must separate INSN1 and INSN2,
2473 where INSN1 is the earlier instruction. Return the worst-case value
2474 for any INSN2 if INSN2 is null. */
252b5132 2475
71400594
RS
2476static unsigned int
2477insns_between (const struct mips_cl_insn *insn1,
2478 const struct mips_cl_insn *insn2)
2479{
2480 unsigned long pinfo1, pinfo2;
2481
2482 /* This function needs to know which pinfo flags are set for INSN2
2483 and which registers INSN2 uses. The former is stored in PINFO2 and
2484 the latter is tested via INSN2_USES_REG. If INSN2 is null, PINFO2
2485 will have every flag set and INSN2_USES_REG will always return true. */
2486 pinfo1 = insn1->insn_mo->pinfo;
2487 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 2488
71400594
RS
2489#define INSN2_USES_REG(REG, CLASS) \
2490 (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS))
2491
2492 /* For most targets, write-after-read dependencies on the HI and LO
2493 registers must be separated by at least two instructions. */
2494 if (!hilo_interlocks)
252b5132 2495 {
71400594
RS
2496 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
2497 return 2;
2498 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
2499 return 2;
2500 }
2501
2502 /* If we're working around r7000 errata, there must be two instructions
2503 between an mfhi or mflo and any instruction that uses the result. */
2504 if (mips_7000_hilo_fix
2505 && MF_HILO_INSN (pinfo1)
2506 && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG))
2507 return 2;
2508
ff239038
CM
2509 /* If we're working around 24K errata, one instruction is required
2510 if an ERET or DERET is followed by a branch instruction. */
2511 if (mips_fix_24k)
2512 {
2513 if (insn1->insn_opcode == INSN_ERET
2514 || insn1->insn_opcode == INSN_DERET)
2515 {
2516 if (insn2 == NULL
2517 || insn2->insn_opcode == INSN_ERET
2518 || insn2->insn_opcode == INSN_DERET
2519 || (insn2->insn_mo->pinfo
2520 & (INSN_UNCOND_BRANCH_DELAY
2521 | INSN_COND_BRANCH_DELAY
2522 | INSN_COND_BRANCH_LIKELY)) != 0)
2523 return 1;
2524 }
2525 }
2526
71400594
RS
2527 /* If working around VR4120 errata, check for combinations that need
2528 a single intervening instruction. */
2529 if (mips_fix_vr4120)
2530 {
2531 unsigned int class1, class2;
252b5132 2532
71400594
RS
2533 class1 = classify_vr4120_insn (insn1->insn_mo->name);
2534 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 2535 {
71400594
RS
2536 if (insn2 == NULL)
2537 return 1;
2538 class2 = classify_vr4120_insn (insn2->insn_mo->name);
2539 if (vr4120_conflicts[class1] & (1 << class2))
2540 return 1;
252b5132 2541 }
71400594
RS
2542 }
2543
2544 if (!mips_opts.mips16)
2545 {
2546 /* Check for GPR or coprocessor load delays. All such delays
2547 are on the RT register. */
2548 /* Itbl support may require additional care here. */
2549 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
2550 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 2551 {
71400594
RS
2552 know (pinfo1 & INSN_WRITE_GPR_T);
2553 if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG))
2554 return 1;
2555 }
2556
2557 /* Check for generic coprocessor hazards.
2558
2559 This case is not handled very well. There is no special
2560 knowledge of CP0 handling, and the coprocessors other than
2561 the floating point unit are not distinguished at all. */
2562 /* Itbl support may require additional care here. FIXME!
2563 Need to modify this to include knowledge about
2564 user specified delays! */
2565 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
2566 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
2567 {
2568 /* Handle cases where INSN1 writes to a known general coprocessor
2569 register. There must be a one instruction delay before INSN2
2570 if INSN2 reads that register, otherwise no delay is needed. */
2571 if (pinfo1 & INSN_WRITE_FPR_T)
252b5132 2572 {
71400594
RS
2573 if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG))
2574 return 1;
252b5132 2575 }
71400594 2576 else if (pinfo1 & INSN_WRITE_FPR_S)
252b5132 2577 {
71400594
RS
2578 if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG))
2579 return 1;
252b5132
RH
2580 }
2581 else
2582 {
71400594
RS
2583 /* Read-after-write dependencies on the control registers
2584 require a two-instruction gap. */
2585 if ((pinfo1 & INSN_WRITE_COND_CODE)
2586 && (pinfo2 & INSN_READ_COND_CODE))
2587 return 2;
2588
2589 /* We don't know exactly what INSN1 does. If INSN2 is
2590 also a coprocessor instruction, assume there must be
2591 a one instruction gap. */
2592 if (pinfo2 & INSN_COP)
2593 return 1;
252b5132
RH
2594 }
2595 }
6b76fefe 2596
71400594
RS
2597 /* Check for read-after-write dependencies on the coprocessor
2598 control registers in cases where INSN1 does not need a general
2599 coprocessor delay. This means that INSN1 is a floating point
2600 comparison instruction. */
2601 /* Itbl support may require additional care here. */
2602 else if (!cop_interlocks
2603 && (pinfo1 & INSN_WRITE_COND_CODE)
2604 && (pinfo2 & INSN_READ_COND_CODE))
2605 return 1;
2606 }
6b76fefe 2607
71400594 2608#undef INSN2_USES_REG
6b76fefe 2609
71400594
RS
2610 return 0;
2611}
6b76fefe 2612
7d8e00cf
RS
2613/* Return the number of nops that would be needed to work around the
2614 VR4130 mflo/mfhi errata if instruction INSN immediately followed
91d6fa6a 2615 the MAX_VR4130_NOPS instructions described by HIST. */
7d8e00cf
RS
2616
2617static int
91d6fa6a 2618nops_for_vr4130 (const struct mips_cl_insn *hist,
7d8e00cf
RS
2619 const struct mips_cl_insn *insn)
2620{
2621 int i, j, reg;
2622
2623 /* Check if the instruction writes to HI or LO. MTHI and MTLO
2624 are not affected by the errata. */
2625 if (insn != 0
2626 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
2627 || strcmp (insn->insn_mo->name, "mtlo") == 0
2628 || strcmp (insn->insn_mo->name, "mthi") == 0))
2629 return 0;
2630
2631 /* Search for the first MFLO or MFHI. */
2632 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 2633 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
2634 {
2635 /* Extract the destination register. */
2636 if (mips_opts.mips16)
91d6fa6a 2637 reg = mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, hist[i])];
7d8e00cf 2638 else
91d6fa6a 2639 reg = EXTRACT_OPERAND (RD, hist[i]);
7d8e00cf
RS
2640
2641 /* No nops are needed if INSN reads that register. */
2642 if (insn != NULL && insn_uses_reg (insn, reg, MIPS_GR_REG))
2643 return 0;
2644
2645 /* ...or if any of the intervening instructions do. */
2646 for (j = 0; j < i; j++)
91d6fa6a 2647 if (insn_uses_reg (&hist[j], reg, MIPS_GR_REG))
7d8e00cf
RS
2648 return 0;
2649
2650 return MAX_VR4130_NOPS - i;
2651 }
2652 return 0;
2653}
2654
71400594 2655/* Return the number of nops that would be needed if instruction INSN
91d6fa6a
NC
2656 immediately followed the MAX_NOPS instructions given by HIST,
2657 where HIST[0] is the most recent instruction. If INSN is null,
71400594 2658 return the worse-case number of nops for any instruction. */
bdaaa2e1 2659
71400594 2660static int
91d6fa6a 2661nops_for_insn (const struct mips_cl_insn *hist,
71400594
RS
2662 const struct mips_cl_insn *insn)
2663{
2664 int i, nops, tmp_nops;
bdaaa2e1 2665
71400594 2666 nops = 0;
7d8e00cf 2667 for (i = 0; i < MAX_DELAY_NOPS; i++)
65b02341 2668 {
91d6fa6a 2669 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
2670 if (tmp_nops > nops)
2671 nops = tmp_nops;
2672 }
7d8e00cf
RS
2673
2674 if (mips_fix_vr4130)
2675 {
91d6fa6a 2676 tmp_nops = nops_for_vr4130 (hist, insn);
7d8e00cf
RS
2677 if (tmp_nops > nops)
2678 nops = tmp_nops;
2679 }
2680
71400594
RS
2681 return nops;
2682}
252b5132 2683
71400594 2684/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 2685 might be added to HIST. Return the largest number of nops that
71400594 2686 would be needed after the extended sequence. */
252b5132 2687
71400594 2688static int
91d6fa6a 2689nops_for_sequence (int num_insns, const struct mips_cl_insn *hist, ...)
71400594
RS
2690{
2691 va_list args;
2692 struct mips_cl_insn buffer[MAX_NOPS];
2693 struct mips_cl_insn *cursor;
2694 int nops;
2695
91d6fa6a 2696 va_start (args, hist);
71400594 2697 cursor = buffer + num_insns;
91d6fa6a 2698 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
2699 while (cursor > buffer)
2700 *--cursor = *va_arg (args, const struct mips_cl_insn *);
2701
2702 nops = nops_for_insn (buffer, NULL);
2703 va_end (args);
2704 return nops;
2705}
252b5132 2706
71400594
RS
2707/* Like nops_for_insn, but if INSN is a branch, take into account the
2708 worst-case delay for the branch target. */
252b5132 2709
71400594 2710static int
91d6fa6a 2711nops_for_insn_or_target (const struct mips_cl_insn *hist,
71400594
RS
2712 const struct mips_cl_insn *insn)
2713{
2714 int nops, tmp_nops;
60b63b72 2715
91d6fa6a 2716 nops = nops_for_insn (hist, insn);
71400594
RS
2717 if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2718 | INSN_COND_BRANCH_DELAY
2719 | INSN_COND_BRANCH_LIKELY))
2720 {
91d6fa6a 2721 tmp_nops = nops_for_sequence (2, hist, insn, NOP_INSN);
71400594
RS
2722 if (tmp_nops > nops)
2723 nops = tmp_nops;
2724 }
2725 else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH))
2726 {
91d6fa6a 2727 tmp_nops = nops_for_sequence (1, hist, insn);
71400594
RS
2728 if (tmp_nops > nops)
2729 nops = tmp_nops;
2730 }
2731 return nops;
2732}
2733
2734/* Output an instruction. IP is the instruction information.
2735 ADDRESS_EXPR is an operand of the instruction to be used with
2736 RELOC_TYPE. */
2737
2738static void
2739append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
2740 bfd_reloc_code_real_type *reloc_type)
2741{
3994f87e 2742 unsigned long prev_pinfo, pinfo;
71400594
RS
2743 relax_stateT prev_insn_frag_type = 0;
2744 bfd_boolean relaxed_branch = FALSE;
a8dbcb85 2745 segment_info_type *si = seg_info (now_seg);
71400594
RS
2746
2747 /* Mark instruction labels in mips16 mode. */
2748 mips16_mark_labels ();
2749
2750 prev_pinfo = history[0].insn_mo->pinfo;
2751 pinfo = ip->insn_mo->pinfo;
2752
2753 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2754 {
2755 /* There are a lot of optimizations we could do that we don't.
2756 In particular, we do not, in general, reorder instructions.
2757 If you use gcc with optimization, it will reorder
2758 instructions and generally do much more optimization then we
2759 do here; repeating all that work in the assembler would only
2760 benefit hand written assembly code, and does not seem worth
2761 it. */
2762 int nops = (mips_optimize == 0
2763 ? nops_for_insn (history, NULL)
2764 : nops_for_insn_or_target (history, ip));
2765 if (nops > 0)
252b5132
RH
2766 {
2767 fragS *old_frag;
2768 unsigned long old_frag_offset;
2769 int i;
252b5132
RH
2770
2771 old_frag = frag_now;
2772 old_frag_offset = frag_now_fix ();
2773
2774 for (i = 0; i < nops; i++)
2775 emit_nop ();
2776
2777 if (listing)
2778 {
2779 listing_prev_line ();
2780 /* We may be at the start of a variant frag. In case we
2781 are, make sure there is enough space for the frag
2782 after the frags created by listing_prev_line. The
2783 argument to frag_grow here must be at least as large
2784 as the argument to all other calls to frag_grow in
2785 this file. We don't have to worry about being in the
2786 middle of a variant frag, because the variants insert
2787 all needed nop instructions themselves. */
2788 frag_grow (40);
2789 }
2790
404a8071 2791 mips_move_labels ();
252b5132
RH
2792
2793#ifndef NO_ECOFF_DEBUGGING
2794 if (ECOFF_DEBUGGING)
2795 ecoff_fix_loc (old_frag, old_frag_offset);
2796#endif
2797 }
71400594
RS
2798 }
2799 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
2800 {
2801 /* Work out how many nops in prev_nop_frag are needed by IP. */
2802 int nops = nops_for_insn_or_target (history, ip);
9c2799c2 2803 gas_assert (nops <= prev_nop_frag_holds);
252b5132 2804
71400594
RS
2805 /* Enforce NOPS as a minimum. */
2806 if (nops > prev_nop_frag_required)
2807 prev_nop_frag_required = nops;
252b5132 2808
71400594
RS
2809 if (prev_nop_frag_holds == prev_nop_frag_required)
2810 {
2811 /* Settle for the current number of nops. Update the history
2812 accordingly (for the benefit of any future .set reorder code). */
2813 prev_nop_frag = NULL;
2814 insert_into_history (prev_nop_frag_since,
2815 prev_nop_frag_holds, NOP_INSN);
2816 }
2817 else
2818 {
2819 /* Allow this instruction to replace one of the nops that was
2820 tentatively added to prev_nop_frag. */
2821 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2822 prev_nop_frag_holds--;
2823 prev_nop_frag_since++;
252b5132
RH
2824 }
2825 }
2826
58e2ea4d
MR
2827#ifdef OBJ_ELF
2828 /* The value passed to dwarf2_emit_insn is the distance between
2829 the beginning of the current instruction and the address that
2830 should be recorded in the debug tables. For MIPS16 debug info
2831 we want to use ISA-encoded addresses, so we pass -1 for an
2832 address higher by one than the current. */
2833 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2834#endif
2835
895921c9 2836 /* Record the frag type before frag_var. */
47e39b9d
RS
2837 if (history[0].frag)
2838 prev_insn_frag_type = history[0].frag->fr_type;
895921c9 2839
4d7206a2 2840 if (address_expr
0b25d3e6 2841 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
2842 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2843 || pinfo & INSN_COND_BRANCH_LIKELY)
2844 && mips_relax_branch
2845 /* Don't try branch relaxation within .set nomacro, or within
2846 .set noat if we use $at for PIC computations. If it turns
2847 out that the branch was out-of-range, we'll get an error. */
2848 && !mips_opts.warn_about_macros
741fe287 2849 && (mips_opts.at || mips_pic == NO_PIC)
4a6a3df4
AO
2850 && !mips_opts.mips16)
2851 {
895921c9 2852 relaxed_branch = TRUE;
1e915849
RS
2853 add_relaxed_insn (ip, (relaxed_branch_length
2854 (NULL, NULL,
2855 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2856 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1
2857 : 0)), 4,
2858 RELAX_BRANCH_ENCODE
2859 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2860 pinfo & INSN_COND_BRANCH_LIKELY,
2861 pinfo & INSN_WRITE_GPR_31,
2862 0),
2863 address_expr->X_add_symbol,
2864 address_expr->X_add_number);
4a6a3df4
AO
2865 *reloc_type = BFD_RELOC_UNUSED;
2866 }
2867 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2868 {
2869 /* We need to set up a variant frag. */
9c2799c2 2870 gas_assert (mips_opts.mips16 && address_expr != NULL);
1e915849
RS
2871 add_relaxed_insn (ip, 4, 0,
2872 RELAX_MIPS16_ENCODE
2873 (*reloc_type - BFD_RELOC_UNUSED,
2874 mips16_small, mips16_ext,
2875 prev_pinfo & INSN_UNCOND_BRANCH_DELAY,
2876 history[0].mips16_absolute_jump_p),
2877 make_expr_symbol (address_expr), 0);
252b5132 2878 }
252b5132
RH
2879 else if (mips_opts.mips16
2880 && ! ip->use_extend
f6688943 2881 && *reloc_type != BFD_RELOC_MIPS16_JMP)
9497f5ac 2882 {
b8ee1a6e
DU
2883 if ((pinfo & INSN_UNCOND_BRANCH_DELAY) == 0)
2884 /* Make sure there is enough room to swap this instruction with
2885 a following jump instruction. */
2886 frag_grow (6);
1e915849 2887 add_fixed_insn (ip);
252b5132
RH
2888 }
2889 else
2890 {
2891 if (mips_opts.mips16
2892 && mips_opts.noreorder
2893 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2894 as_warn (_("extended instruction in delay slot"));
2895
4d7206a2
RS
2896 if (mips_relax.sequence)
2897 {
2898 /* If we've reached the end of this frag, turn it into a variant
2899 frag and record the information for the instructions we've
2900 written so far. */
2901 if (frag_room () < 4)
2902 relax_close_frag ();
2903 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2904 }
2905
584892a6
RS
2906 if (mips_relax.sequence != 2)
2907 mips_macro_warning.sizes[0] += 4;
2908 if (mips_relax.sequence != 1)
2909 mips_macro_warning.sizes[1] += 4;
2910
1e915849
RS
2911 if (mips_opts.mips16)
2912 {
2913 ip->fixed_p = 1;
2914 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2915 }
2916 add_fixed_insn (ip);
252b5132
RH
2917 }
2918
01a3f561 2919 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
252b5132
RH
2920 {
2921 if (address_expr->X_op == O_constant)
2922 {
f17c130b 2923 unsigned int tmp;
f6688943
TS
2924
2925 switch (*reloc_type)
252b5132
RH
2926 {
2927 case BFD_RELOC_32:
2928 ip->insn_opcode |= address_expr->X_add_number;
2929 break;
2930
f6688943 2931 case BFD_RELOC_MIPS_HIGHEST:
f17c130b
AM
2932 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2933 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2934 break;
2935
2936 case BFD_RELOC_MIPS_HIGHER:
f17c130b
AM
2937 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2938 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2939 break;
2940
2941 case BFD_RELOC_HI16_S:
f17c130b
AM
2942 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2943 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2944 break;
2945
2946 case BFD_RELOC_HI16:
2947 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2948 break;
2949
01a3f561 2950 case BFD_RELOC_UNUSED:
252b5132 2951 case BFD_RELOC_LO16:
ed6fb7bd 2952 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2953 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2954 break;
2955
2956 case BFD_RELOC_MIPS_JMP:
2957 if ((address_expr->X_add_number & 3) != 0)
2958 as_bad (_("jump to misaligned address (0x%lx)"),
2959 (unsigned long) address_expr->X_add_number);
2960 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2961 break;
2962
2963 case BFD_RELOC_MIPS16_JMP:
2964 if ((address_expr->X_add_number & 3) != 0)
2965 as_bad (_("jump to misaligned address (0x%lx)"),
2966 (unsigned long) address_expr->X_add_number);
2967 ip->insn_opcode |=
2968 (((address_expr->X_add_number & 0x7c0000) << 3)
2969 | ((address_expr->X_add_number & 0xf800000) >> 7)
2970 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2971 break;
2972
252b5132 2973 case BFD_RELOC_16_PCREL_S2:
bad36eac
DJ
2974 if ((address_expr->X_add_number & 3) != 0)
2975 as_bad (_("branch to misaligned address (0x%lx)"),
2976 (unsigned long) address_expr->X_add_number);
2977 if (mips_relax_branch)
2978 goto need_reloc;
2979 if ((address_expr->X_add_number + 0x20000) & ~0x3ffff)
2980 as_bad (_("branch address range overflow (0x%lx)"),
2981 (unsigned long) address_expr->X_add_number);
2982 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0xffff;
2983 break;
252b5132
RH
2984
2985 default:
2986 internalError ();
2987 }
2988 }
01a3f561 2989 else if (*reloc_type < BFD_RELOC_UNUSED)
252b5132 2990 need_reloc:
4d7206a2
RS
2991 {
2992 reloc_howto_type *howto;
2993 int i;
34ce925e 2994
4d7206a2
RS
2995 /* In a compound relocation, it is the final (outermost)
2996 operator that determines the relocated field. */
2997 for (i = 1; i < 3; i++)
2998 if (reloc_type[i] == BFD_RELOC_UNUSED)
2999 break;
34ce925e 3000
4d7206a2 3001 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
23fce1e3
NC
3002 if (howto == NULL)
3003 {
3004 /* To reproduce this failure try assembling gas/testsuites/
3005 gas/mips/mips16-intermix.s with a mips-ecoff targeted
3006 assembler. */
3007 as_bad (_("Unsupported MIPS relocation number %d"), reloc_type[i - 1]);
3008 howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
3009 }
3010
1e915849
RS
3011 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
3012 bfd_get_reloc_size (howto),
3013 address_expr,
3014 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
3015 reloc_type[0]);
4d7206a2 3016
b314ec0e
RS
3017 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
3018 if (reloc_type[0] == BFD_RELOC_MIPS16_JMP
3019 && ip->fixp[0]->fx_addsy)
3020 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
3021
4d7206a2
RS
3022 /* These relocations can have an addend that won't fit in
3023 4 octets for 64bit assembly. */
3024 if (HAVE_64BIT_GPRS
3025 && ! howto->partial_inplace
3026 && (reloc_type[0] == BFD_RELOC_16
3027 || reloc_type[0] == BFD_RELOC_32
3028 || reloc_type[0] == BFD_RELOC_MIPS_JMP
4d7206a2
RS
3029 || reloc_type[0] == BFD_RELOC_GPREL16
3030 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
3031 || reloc_type[0] == BFD_RELOC_GPREL32
3032 || reloc_type[0] == BFD_RELOC_64
3033 || reloc_type[0] == BFD_RELOC_CTOR
3034 || reloc_type[0] == BFD_RELOC_MIPS_SUB
3035 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
3036 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
3037 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
3038 || reloc_type[0] == BFD_RELOC_MIPS_REL16
d6f16593
MR
3039 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
3040 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
738e5348
RS
3041 || hi16_reloc_p (reloc_type[0])
3042 || lo16_reloc_p (reloc_type[0])))
1e915849 3043 ip->fixp[0]->fx_no_overflow = 1;
4d7206a2
RS
3044
3045 if (mips_relax.sequence)
3046 {
3047 if (mips_relax.first_fixup == 0)
1e915849 3048 mips_relax.first_fixup = ip->fixp[0];
4d7206a2
RS
3049 }
3050 else if (reloc_needs_lo_p (*reloc_type))
3051 {
3052 struct mips_hi_fixup *hi_fixup;
252b5132 3053
4d7206a2
RS
3054 /* Reuse the last entry if it already has a matching %lo. */
3055 hi_fixup = mips_hi_fixup_list;
3056 if (hi_fixup == 0
3057 || !fixup_has_matching_lo_p (hi_fixup->fixp))
3058 {
3059 hi_fixup = ((struct mips_hi_fixup *)
3060 xmalloc (sizeof (struct mips_hi_fixup)));
3061 hi_fixup->next = mips_hi_fixup_list;
3062 mips_hi_fixup_list = hi_fixup;
252b5132 3063 }
1e915849 3064 hi_fixup->fixp = ip->fixp[0];
4d7206a2
RS
3065 hi_fixup->seg = now_seg;
3066 }
f6688943 3067
4d7206a2
RS
3068 /* Add fixups for the second and third relocations, if given.
3069 Note that the ABI allows the second relocation to be
3070 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
3071 moment we only use RSS_UNDEF, but we could add support
3072 for the others if it ever becomes necessary. */
3073 for (i = 1; i < 3; i++)
3074 if (reloc_type[i] != BFD_RELOC_UNUSED)
3075 {
1e915849
RS
3076 ip->fixp[i] = fix_new (ip->frag, ip->where,
3077 ip->fixp[0]->fx_size, NULL, 0,
3078 FALSE, reloc_type[i]);
b1dca8ee
RS
3079
3080 /* Use fx_tcbit to mark compound relocs. */
1e915849
RS
3081 ip->fixp[0]->fx_tcbit = 1;
3082 ip->fixp[i]->fx_tcbit = 1;
4d7206a2 3083 }
252b5132
RH
3084 }
3085 }
1e915849 3086 install_insn (ip);
252b5132
RH
3087
3088 /* Update the register mask information. */
3089 if (! mips_opts.mips16)
3090 {
3091 if (pinfo & INSN_WRITE_GPR_D)
bf12938e 3092 mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
252b5132 3093 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
bf12938e 3094 mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
252b5132 3095 if (pinfo & INSN_READ_GPR_S)
bf12938e 3096 mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
252b5132 3097 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 3098 mips_gprmask |= 1 << RA;
252b5132 3099 if (pinfo & INSN_WRITE_FPR_D)
bf12938e 3100 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
252b5132 3101 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
bf12938e 3102 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
252b5132 3103 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
bf12938e 3104 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
252b5132 3105 if ((pinfo & INSN_READ_FPR_R) != 0)
bf12938e 3106 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
252b5132
RH
3107 if (pinfo & INSN_COP)
3108 {
bdaaa2e1
KH
3109 /* We don't keep enough information to sort these cases out.
3110 The itbl support does keep this information however, although
3111 we currently don't support itbl fprmats as part of the cop
3112 instruction. May want to add this support in the future. */
252b5132
RH
3113 }
3114 /* Never set the bit for $0, which is always zero. */
beae10d5 3115 mips_gprmask &= ~1 << 0;
252b5132
RH
3116 }
3117 else
3118 {
3119 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
bf12938e 3120 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
252b5132 3121 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
bf12938e 3122 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
252b5132 3123 if (pinfo & MIPS16_INSN_WRITE_Z)
bf12938e 3124 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132
RH
3125 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
3126 mips_gprmask |= 1 << TREG;
3127 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
3128 mips_gprmask |= 1 << SP;
3129 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
3130 mips_gprmask |= 1 << RA;
3131 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3132 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3133 if (pinfo & MIPS16_INSN_READ_Z)
bf12938e 3134 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
252b5132 3135 if (pinfo & MIPS16_INSN_READ_GPR_X)
bf12938e 3136 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
252b5132
RH
3137 }
3138
4d7206a2 3139 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
252b5132
RH
3140 {
3141 /* Filling the branch delay slot is more complex. We try to
3142 switch the branch with the previous instruction, which we can
3143 do if the previous instruction does not set up a condition
3144 that the branch tests and if the branch is not itself the
3145 target of any branch. */
3146 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
3147 || (pinfo & INSN_COND_BRANCH_DELAY))
3148 {
3149 if (mips_optimize < 2
3150 /* If we have seen .set volatile or .set nomove, don't
3151 optimize. */
3152 || mips_opts.nomove != 0
a38419a5
RS
3153 /* We can't swap if the previous instruction's position
3154 is fixed. */
3155 || history[0].fixed_p
252b5132
RH
3156 /* If the previous previous insn was in a .set
3157 noreorder, we can't swap. Actually, the MIPS
3158 assembler will swap in this situation. However, gcc
3159 configured -with-gnu-as will generate code like
3160 .set noreorder
3161 lw $4,XXX
3162 .set reorder
3163 INSN
3164 bne $4,$0,foo
3165 in which we can not swap the bne and INSN. If gcc is
3166 not configured -with-gnu-as, it does not output the
a38419a5 3167 .set pseudo-ops. */
47e39b9d 3168 || history[1].noreorder_p
252b5132
RH
3169 /* If the branch is itself the target of a branch, we
3170 can not swap. We cheat on this; all we check for is
3171 whether there is a label on this instruction. If
3172 there are any branches to anything other than a
3173 label, users must use .set noreorder. */
a8dbcb85 3174 || si->label_list != NULL
895921c9
MR
3175 /* If the previous instruction is in a variant frag
3176 other than this branch's one, we cannot do the swap.
3177 This does not apply to the mips16, which uses variant
3178 frags for different purposes. */
252b5132 3179 || (! mips_opts.mips16
895921c9 3180 && prev_insn_frag_type == rs_machine_dependent)
71400594
RS
3181 /* Check for conflicts between the branch and the instructions
3182 before the candidate delay slot. */
3183 || nops_for_insn (history + 1, ip) > 0
3184 /* Check for conflicts between the swapped sequence and the
3185 target of the branch. */
3186 || nops_for_sequence (2, history + 1, ip, history) > 0
252b5132
RH
3187 /* We do not swap with a trap instruction, since it
3188 complicates trap handlers to have the trap
3189 instruction be in a delay slot. */
3190 || (prev_pinfo & INSN_TRAP)
3191 /* If the branch reads a register that the previous
3192 instruction sets, we can not swap. */
3193 || (! mips_opts.mips16
3194 && (prev_pinfo & INSN_WRITE_GPR_T)
bf12938e 3195 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
252b5132
RH
3196 MIPS_GR_REG))
3197 || (! mips_opts.mips16
3198 && (prev_pinfo & INSN_WRITE_GPR_D)
bf12938e 3199 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
252b5132
RH
3200 MIPS_GR_REG))
3201 || (mips_opts.mips16
3202 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
bf12938e
RS
3203 && (insn_uses_reg
3204 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
3205 MIPS16_REG)))
252b5132 3206 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
bf12938e
RS
3207 && (insn_uses_reg
3208 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
3209 MIPS16_REG)))
252b5132 3210 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
bf12938e
RS
3211 && (insn_uses_reg
3212 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
3213 MIPS16_REG)))
252b5132
RH
3214 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
3215 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
3216 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
3217 && insn_uses_reg (ip, RA, MIPS_GR_REG))
3218 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
3219 && insn_uses_reg (ip,
47e39b9d
RS
3220 MIPS16OP_EXTRACT_REG32R
3221 (history[0].insn_opcode),
252b5132
RH
3222 MIPS_GR_REG))))
3223 /* If the branch writes a register that the previous
3224 instruction sets, we can not swap (we know that
3225 branches write only to RD or to $31). */
3226 || (! mips_opts.mips16
3227 && (prev_pinfo & INSN_WRITE_GPR_T)
3228 && (((pinfo & INSN_WRITE_GPR_D)
bf12938e
RS
3229 && (EXTRACT_OPERAND (RT, history[0])
3230 == EXTRACT_OPERAND (RD, *ip)))
252b5132 3231 || ((pinfo & INSN_WRITE_GPR_31)
bf12938e 3232 && EXTRACT_OPERAND (RT, history[0]) == RA)))
252b5132
RH
3233 || (! mips_opts.mips16
3234 && (prev_pinfo & INSN_WRITE_GPR_D)
3235 && (((pinfo & INSN_WRITE_GPR_D)
bf12938e
RS
3236 && (EXTRACT_OPERAND (RD, history[0])
3237 == EXTRACT_OPERAND (RD, *ip)))
252b5132 3238 || ((pinfo & INSN_WRITE_GPR_31)
bf12938e 3239 && EXTRACT_OPERAND (RD, history[0]) == RA)))
252b5132
RH
3240 || (mips_opts.mips16
3241 && (pinfo & MIPS16_INSN_WRITE_31)
3242 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
3243 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
47e39b9d 3244 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
252b5132
RH
3245 == RA))))
3246 /* If the branch writes a register that the previous
3247 instruction reads, we can not swap (we know that
3248 branches only write to RD or to $31). */
3249 || (! mips_opts.mips16
3250 && (pinfo & INSN_WRITE_GPR_D)
47e39b9d 3251 && insn_uses_reg (&history[0],
bf12938e 3252 EXTRACT_OPERAND (RD, *ip),
252b5132
RH
3253 MIPS_GR_REG))
3254 || (! mips_opts.mips16
3255 && (pinfo & INSN_WRITE_GPR_31)
47e39b9d 3256 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
252b5132
RH
3257 || (mips_opts.mips16
3258 && (pinfo & MIPS16_INSN_WRITE_31)
47e39b9d 3259 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
252b5132
RH
3260 /* If one instruction sets a condition code and the
3261 other one uses a condition code, we can not swap. */
3262 || ((pinfo & INSN_READ_COND_CODE)
3263 && (prev_pinfo & INSN_WRITE_COND_CODE))
3264 || ((pinfo & INSN_WRITE_COND_CODE)
3265 && (prev_pinfo & INSN_READ_COND_CODE))
3266 /* If the previous instruction uses the PC, we can not
3267 swap. */
3268 || (mips_opts.mips16
3269 && (prev_pinfo & MIPS16_INSN_READ_PC))
252b5132
RH
3270 /* If the previous instruction had a fixup in mips16
3271 mode, we can not swap. This normally means that the
3272 previous instruction was a 4 byte branch anyhow. */
47e39b9d 3273 || (mips_opts.mips16 && history[0].fixp[0])
bdaaa2e1
KH
3274 /* If the previous instruction is a sync, sync.l, or
3275 sync.p, we can not swap. */
6a32d874
CM
3276 || (prev_pinfo & INSN_SYNC)
3277 /* If the previous instruction is an ERET or
3278 DERET, avoid the swap. */
3279 || (history[0].insn_opcode == INSN_ERET)
3280 || (history[0].insn_opcode == INSN_DERET))
252b5132 3281 {
29024861
DU
3282 if (mips_opts.mips16
3283 && (pinfo & INSN_UNCOND_BRANCH_DELAY)
3284 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31))
3994f87e 3285 && ISA_SUPPORTS_MIPS16E)
29024861
DU
3286 {
3287 /* Convert MIPS16 jr/jalr into a "compact" jump. */
3288 ip->insn_opcode |= 0x0080;
3289 install_insn (ip);
3290 insert_into_history (0, 1, ip);
3291 }
3292 else
3293 {
3294 /* We could do even better for unconditional branches to
3295 portions of this object file; we could pick up the
3296 instruction at the destination, put it in the delay
3297 slot, and bump the destination address. */
3298 insert_into_history (0, 1, ip);
3299 emit_nop ();
3300 }
3301
dd22970f
ILT
3302 if (mips_relax.sequence)
3303 mips_relax.sizes[mips_relax.sequence - 1] += 4;
252b5132
RH
3304 }
3305 else
3306 {
3307 /* It looks like we can actually do the swap. */
1e915849
RS
3308 struct mips_cl_insn delay = history[0];
3309 if (mips_opts.mips16)
252b5132 3310 {
b8ee1a6e
DU
3311 know (delay.frag == ip->frag);
3312 move_insn (ip, delay.frag, delay.where);
3313 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
1e915849
RS
3314 }
3315 else if (relaxed_branch)
3316 {
3317 /* Add the delay slot instruction to the end of the
3318 current frag and shrink the fixed part of the
3319 original frag. If the branch occupies the tail of
3320 the latter, move it backwards to cover the gap. */
3321 delay.frag->fr_fix -= 4;
3322 if (delay.frag == ip->frag)
3323 move_insn (ip, ip->frag, ip->where - 4);
3324 add_fixed_insn (&delay);
252b5132
RH
3325 }
3326 else
3327 {
1e915849
RS
3328 move_insn (&delay, ip->frag, ip->where);
3329 move_insn (ip, history[0].frag, history[0].where);
252b5132 3330 }
1e915849
RS
3331 history[0] = *ip;
3332 delay.fixed_p = 1;
3333 insert_into_history (0, 1, &delay);
252b5132 3334 }
252b5132
RH
3335
3336 /* If that was an unconditional branch, forget the previous
3337 insn information. */
3338 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
6a32d874 3339 {
6a32d874
CM
3340 mips_no_prev_insn ();
3341 }
252b5132
RH
3342 }
3343 else if (pinfo & INSN_COND_BRANCH_LIKELY)
3344 {
3345 /* We don't yet optimize a branch likely. What we should do
3346 is look at the target, copy the instruction found there
3347 into the delay slot, and increment the branch to jump to
3348 the next instruction. */
1e915849 3349 insert_into_history (0, 1, ip);
252b5132 3350 emit_nop ();
252b5132
RH
3351 }
3352 else
1e915849 3353 insert_into_history (0, 1, ip);
252b5132 3354 }
1e915849
RS
3355 else
3356 insert_into_history (0, 1, ip);
252b5132
RH
3357
3358 /* We just output an insn, so the next one doesn't have a label. */
3359 mips_clear_insn_labels ();
252b5132
RH
3360}
3361
7d10b47d 3362/* Forget that there was any previous instruction or label. */
252b5132
RH
3363
3364static void
7d10b47d 3365mips_no_prev_insn (void)
252b5132 3366{
7d10b47d
RS
3367 prev_nop_frag = NULL;
3368 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
3369 mips_clear_insn_labels ();
3370}
3371
7d10b47d
RS
3372/* This function must be called before we emit something other than
3373 instructions. It is like mips_no_prev_insn except that it inserts
3374 any NOPS that might be needed by previous instructions. */
252b5132 3375
7d10b47d
RS
3376void
3377mips_emit_delays (void)
252b5132
RH
3378{
3379 if (! mips_opts.noreorder)
3380 {
71400594 3381 int nops = nops_for_insn (history, NULL);
252b5132
RH
3382 if (nops > 0)
3383 {
7d10b47d
RS
3384 while (nops-- > 0)
3385 add_fixed_insn (NOP_INSN);
3386 mips_move_labels ();
3387 }
3388 }
3389 mips_no_prev_insn ();
3390}
3391
3392/* Start a (possibly nested) noreorder block. */
3393
3394static void
3395start_noreorder (void)
3396{
3397 if (mips_opts.noreorder == 0)
3398 {
3399 unsigned int i;
3400 int nops;
3401
3402 /* None of the instructions before the .set noreorder can be moved. */
3403 for (i = 0; i < ARRAY_SIZE (history); i++)
3404 history[i].fixed_p = 1;
3405
3406 /* Insert any nops that might be needed between the .set noreorder
3407 block and the previous instructions. We will later remove any
3408 nops that turn out not to be needed. */
3409 nops = nops_for_insn (history, NULL);
3410 if (nops > 0)
3411 {
3412 if (mips_optimize != 0)
252b5132
RH
3413 {
3414 /* Record the frag which holds the nop instructions, so
3415 that we can remove them if we don't need them. */
3416 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
3417 prev_nop_frag = frag_now;
3418 prev_nop_frag_holds = nops;
3419 prev_nop_frag_required = 0;
3420 prev_nop_frag_since = 0;
3421 }
3422
3423 for (; nops > 0; --nops)
1e915849 3424 add_fixed_insn (NOP_INSN);
252b5132 3425
7d10b47d
RS
3426 /* Move on to a new frag, so that it is safe to simply
3427 decrease the size of prev_nop_frag. */
3428 frag_wane (frag_now);
3429 frag_new (0);
404a8071 3430 mips_move_labels ();
252b5132 3431 }
7d10b47d
RS
3432 mips16_mark_labels ();
3433 mips_clear_insn_labels ();
252b5132 3434 }
7d10b47d
RS
3435 mips_opts.noreorder++;
3436 mips_any_noreorder = 1;
3437}
252b5132 3438
7d10b47d 3439/* End a nested noreorder block. */
252b5132 3440
7d10b47d
RS
3441static void
3442end_noreorder (void)
3443{
6a32d874 3444
7d10b47d
RS
3445 mips_opts.noreorder--;
3446 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
3447 {
3448 /* Commit to inserting prev_nop_frag_required nops and go back to
3449 handling nop insertion the .set reorder way. */
3450 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
3451 * (mips_opts.mips16 ? 2 : 4));
3452 insert_into_history (prev_nop_frag_since,
3453 prev_nop_frag_required, NOP_INSN);
3454 prev_nop_frag = NULL;
3455 }
252b5132
RH
3456}
3457
584892a6
RS
3458/* Set up global variables for the start of a new macro. */
3459
3460static void
3461macro_start (void)
3462{
3463 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
3464 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
47e39b9d 3465 && (history[0].insn_mo->pinfo
584892a6
RS
3466 & (INSN_UNCOND_BRANCH_DELAY
3467 | INSN_COND_BRANCH_DELAY
3468 | INSN_COND_BRANCH_LIKELY)) != 0);
3469}
3470
3471/* Given that a macro is longer than 4 bytes, return the appropriate warning
3472 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
3473 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
3474
3475static const char *
3476macro_warning (relax_substateT subtype)
3477{
3478 if (subtype & RELAX_DELAY_SLOT)
3479 return _("Macro instruction expanded into multiple instructions"
3480 " in a branch delay slot");
3481 else if (subtype & RELAX_NOMACRO)
3482 return _("Macro instruction expanded into multiple instructions");
3483 else
3484 return 0;
3485}
3486
3487/* Finish up a macro. Emit warnings as appropriate. */
3488
3489static void
3490macro_end (void)
3491{
3492 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
3493 {
3494 relax_substateT subtype;
3495
3496 /* Set up the relaxation warning flags. */
3497 subtype = 0;
3498 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
3499 subtype |= RELAX_SECOND_LONGER;
3500 if (mips_opts.warn_about_macros)
3501 subtype |= RELAX_NOMACRO;
3502 if (mips_macro_warning.delay_slot_p)
3503 subtype |= RELAX_DELAY_SLOT;
3504
3505 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
3506 {
3507 /* Either the macro has a single implementation or both
3508 implementations are longer than 4 bytes. Emit the
3509 warning now. */
3510 const char *msg = macro_warning (subtype);
3511 if (msg != 0)
520725ea 3512 as_warn ("%s", msg);
584892a6
RS
3513 }
3514 else
3515 {
3516 /* One implementation might need a warning but the other
3517 definitely doesn't. */
3518 mips_macro_warning.first_frag->fr_subtype |= subtype;
3519 }
3520 }
3521}
3522
6e1304d8
RS
3523/* Read a macro's relocation codes from *ARGS and store them in *R.
3524 The first argument in *ARGS will be either the code for a single
3525 relocation or -1 followed by the three codes that make up a
3526 composite relocation. */
3527
3528static void
3529macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
3530{
3531 int i, next;
3532
3533 next = va_arg (*args, int);
3534 if (next >= 0)
3535 r[0] = (bfd_reloc_code_real_type) next;
3536 else
3537 for (i = 0; i < 3; i++)
3538 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
3539}
3540
252b5132
RH
3541/* Build an instruction created by a macro expansion. This is passed
3542 a pointer to the count of instructions created so far, an
3543 expression, the name of the instruction to build, an operand format
3544 string, and corresponding arguments. */
3545
252b5132 3546static void
67c0d1eb 3547macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 3548{
1e915849 3549 const struct mips_opcode *mo;
252b5132 3550 struct mips_cl_insn insn;
f6688943 3551 bfd_reloc_code_real_type r[3];
252b5132 3552 va_list args;
252b5132 3553
252b5132 3554 va_start (args, fmt);
252b5132 3555
252b5132
RH
3556 if (mips_opts.mips16)
3557 {
67c0d1eb 3558 mips16_macro_build (ep, name, fmt, args);
252b5132
RH
3559 va_end (args);
3560 return;
3561 }
3562
f6688943
TS
3563 r[0] = BFD_RELOC_UNUSED;
3564 r[1] = BFD_RELOC_UNUSED;
3565 r[2] = BFD_RELOC_UNUSED;
1e915849 3566 mo = (struct mips_opcode *) hash_find (op_hash, name);
9c2799c2
NC
3567 gas_assert (mo);
3568 gas_assert (strcmp (name, mo->name) == 0);
1e915849 3569
8b082fb1
TS
3570 while (1)
3571 {
3572 /* Search until we get a match for NAME. It is assumed here that
3573 macros will never generate MDMX, MIPS-3D, or MT instructions. */
3574 if (strcmp (fmt, mo->args) == 0
3575 && mo->pinfo != INSN_MACRO
037b32b9 3576 && is_opcode_valid (mo, TRUE))
8b082fb1
TS
3577 break;
3578
1e915849 3579 ++mo;
9c2799c2
NC
3580 gas_assert (mo->name);
3581 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
3582 }
3583
1e915849 3584 create_insn (&insn, mo);
252b5132
RH
3585 for (;;)
3586 {
3587 switch (*fmt++)
3588 {
3589 case '\0':
3590 break;
3591
3592 case ',':
3593 case '(':
3594 case ')':
3595 continue;
3596
5f74bc13
CD
3597 case '+':
3598 switch (*fmt++)
3599 {
3600 case 'A':
3601 case 'E':
bf12938e 3602 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
5f74bc13
CD
3603 continue;
3604
3605 case 'B':
3606 case 'F':
3607 /* Note that in the macro case, these arguments are already
3608 in MSB form. (When handling the instruction in the
3609 non-macro case, these arguments are sizes from which
3610 MSB values must be calculated.) */
bf12938e 3611 INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
5f74bc13
CD
3612 continue;
3613
3614 case 'C':
3615 case 'G':
3616 case 'H':
3617 /* Note that in the macro case, these arguments are already
3618 in MSBD form. (When handling the instruction in the
3619 non-macro case, these arguments are sizes from which
3620 MSBD values must be calculated.) */
bf12938e 3621 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
3622 continue;
3623
dd3cbb7e
NC
3624 case 'Q':
3625 INSERT_OPERAND (SEQI, insn, va_arg (args, int));
3626 continue;
3627
5f74bc13
CD
3628 default:
3629 internalError ();
3630 }
3631 continue;
3632
8b082fb1
TS
3633 case '2':
3634 INSERT_OPERAND (BP, insn, va_arg (args, int));
3635 continue;
3636
252b5132
RH
3637 case 't':
3638 case 'w':
3639 case 'E':
bf12938e 3640 INSERT_OPERAND (RT, insn, va_arg (args, int));
252b5132
RH
3641 continue;
3642
3643 case 'c':
bf12938e 3644 INSERT_OPERAND (CODE, insn, va_arg (args, int));
38487616
TS
3645 continue;
3646
252b5132
RH
3647 case 'T':
3648 case 'W':
bf12938e 3649 INSERT_OPERAND (FT, insn, va_arg (args, int));
252b5132
RH
3650 continue;
3651
3652 case 'd':
3653 case 'G':
af7ee8bf 3654 case 'K':
bf12938e 3655 INSERT_OPERAND (RD, insn, va_arg (args, int));
252b5132
RH
3656 continue;
3657
4372b673
NC
3658 case 'U':
3659 {
3660 int tmp = va_arg (args, int);
3661
bf12938e
RS
3662 INSERT_OPERAND (RT, insn, tmp);
3663 INSERT_OPERAND (RD, insn, tmp);
beae10d5 3664 continue;
4372b673
NC
3665 }
3666
252b5132
RH
3667 case 'V':
3668 case 'S':
bf12938e 3669 INSERT_OPERAND (FS, insn, va_arg (args, int));
252b5132
RH
3670 continue;
3671
3672 case 'z':
3673 continue;
3674
3675 case '<':
bf12938e 3676 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
252b5132
RH
3677 continue;
3678
3679 case 'D':
bf12938e 3680 INSERT_OPERAND (FD, insn, va_arg (args, int));
252b5132
RH
3681 continue;
3682
3683 case 'B':
bf12938e 3684 INSERT_OPERAND (CODE20, insn, va_arg (args, int));
252b5132
RH
3685 continue;
3686
4372b673 3687 case 'J':
bf12938e 3688 INSERT_OPERAND (CODE19, insn, va_arg (args, int));
4372b673
NC
3689 continue;
3690
252b5132 3691 case 'q':
bf12938e 3692 INSERT_OPERAND (CODE2, insn, va_arg (args, int));
252b5132
RH
3693 continue;
3694
3695 case 'b':
3696 case 's':
3697 case 'r':
3698 case 'v':
bf12938e 3699 INSERT_OPERAND (RS, insn, va_arg (args, int));
252b5132
RH
3700 continue;
3701
3702 case 'i':
3703 case 'j':
3704 case 'o':
6e1304d8 3705 macro_read_relocs (&args, r);
9c2799c2 3706 gas_assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3707 || *r == BFD_RELOC_MIPS_LITERAL
3708 || *r == BFD_RELOC_MIPS_HIGHER
3709 || *r == BFD_RELOC_HI16_S
3710 || *r == BFD_RELOC_LO16
3711 || *r == BFD_RELOC_MIPS_GOT16
3712 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3713 || *r == BFD_RELOC_MIPS_GOT_DISP
3714 || *r == BFD_RELOC_MIPS_GOT_PAGE
3715 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943 3716 || *r == BFD_RELOC_MIPS_GOT_LO16
3e722fb5 3717 || *r == BFD_RELOC_MIPS_CALL_LO16);
252b5132
RH
3718 continue;
3719
3720 case 'u':
6e1304d8 3721 macro_read_relocs (&args, r);
9c2799c2 3722 gas_assert (ep != NULL
252b5132
RH
3723 && (ep->X_op == O_constant
3724 || (ep->X_op == O_symbol
f6688943
TS
3725 && (*r == BFD_RELOC_MIPS_HIGHEST
3726 || *r == BFD_RELOC_HI16_S
3727 || *r == BFD_RELOC_HI16
3728 || *r == BFD_RELOC_GPREL16
3729 || *r == BFD_RELOC_MIPS_GOT_HI16
3e722fb5 3730 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
3731 continue;
3732
3733 case 'p':
9c2799c2 3734 gas_assert (ep != NULL);
bad36eac 3735
252b5132
RH
3736 /*
3737 * This allows macro() to pass an immediate expression for
3738 * creating short branches without creating a symbol.
bad36eac
DJ
3739 *
3740 * We don't allow branch relaxation for these branches, as
3741 * they should only appear in ".set nomacro" anyway.
252b5132
RH
3742 */
3743 if (ep->X_op == O_constant)
3744 {
bad36eac
DJ
3745 if ((ep->X_add_number & 3) != 0)
3746 as_bad (_("branch to misaligned address (0x%lx)"),
3747 (unsigned long) ep->X_add_number);
3748 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
3749 as_bad (_("branch address range overflow (0x%lx)"),
3750 (unsigned long) ep->X_add_number);
252b5132
RH
3751 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3752 ep = NULL;
3753 }
3754 else
0b25d3e6 3755 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3756 continue;
3757
3758 case 'a':
9c2799c2 3759 gas_assert (ep != NULL);
f6688943 3760 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3761 continue;
3762
3763 case 'C':
a9e24354 3764 INSERT_OPERAND (COPZ, insn, va_arg (args, unsigned long));
252b5132
RH
3765 continue;
3766
d43b4baf 3767 case 'k':
a9e24354 3768 INSERT_OPERAND (CACHE, insn, va_arg (args, unsigned long));
d43b4baf
TS
3769 continue;
3770
252b5132
RH
3771 default:
3772 internalError ();
3773 }
3774 break;
3775 }
3776 va_end (args);
9c2799c2 3777 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3778
4d7206a2 3779 append_insn (&insn, ep, r);
252b5132
RH
3780}
3781
3782static void
67c0d1eb 3783mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
17a2f251 3784 va_list args)
252b5132 3785{
1e915849 3786 struct mips_opcode *mo;
252b5132 3787 struct mips_cl_insn insn;
f6688943
TS
3788 bfd_reloc_code_real_type r[3]
3789 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3790
1e915849 3791 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
3792 gas_assert (mo);
3793 gas_assert (strcmp (name, mo->name) == 0);
252b5132 3794
1e915849 3795 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 3796 {
1e915849 3797 ++mo;
9c2799c2
NC
3798 gas_assert (mo->name);
3799 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
3800 }
3801
1e915849 3802 create_insn (&insn, mo);
252b5132
RH
3803 for (;;)
3804 {
3805 int c;
3806
3807 c = *fmt++;
3808 switch (c)
3809 {
3810 case '\0':
3811 break;
3812
3813 case ',':
3814 case '(':
3815 case ')':
3816 continue;
3817
3818 case 'y':
3819 case 'w':
bf12938e 3820 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
252b5132
RH
3821 continue;
3822
3823 case 'x':
3824 case 'v':
bf12938e 3825 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
252b5132
RH
3826 continue;
3827
3828 case 'z':
bf12938e 3829 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
252b5132
RH
3830 continue;
3831
3832 case 'Z':
bf12938e 3833 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
252b5132
RH
3834 continue;
3835
3836 case '0':
3837 case 'S':
3838 case 'P':
3839 case 'R':
3840 continue;
3841
3842 case 'X':
bf12938e 3843 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
252b5132
RH
3844 continue;
3845
3846 case 'Y':
3847 {
3848 int regno;
3849
3850 regno = va_arg (args, int);
3851 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
a9e24354 3852 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
252b5132
RH
3853 }
3854 continue;
3855
3856 case '<':
3857 case '>':
3858 case '4':
3859 case '5':
3860 case 'H':
3861 case 'W':
3862 case 'D':
3863 case 'j':
3864 case '8':
3865 case 'V':
3866 case 'C':
3867 case 'U':
3868 case 'k':
3869 case 'K':
3870 case 'p':
3871 case 'q':
3872 {
9c2799c2 3873 gas_assert (ep != NULL);
252b5132
RH
3874
3875 if (ep->X_op != O_constant)
874e8986 3876 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3877 else
3878 {
b34976b6
AM
3879 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3880 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3881 &insn.extend);
252b5132 3882 ep = NULL;
f6688943 3883 *r = BFD_RELOC_UNUSED;
252b5132
RH
3884 }
3885 }
3886 continue;
3887
3888 case '6':
bf12938e 3889 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
252b5132
RH
3890 continue;
3891 }
3892
3893 break;
3894 }
3895
9c2799c2 3896 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3897
4d7206a2 3898 append_insn (&insn, ep, r);
252b5132
RH
3899}
3900
2051e8c4
MR
3901/*
3902 * Sign-extend 32-bit mode constants that have bit 31 set and all
3903 * higher bits unset.
3904 */
9f872bbe 3905static void
2051e8c4
MR
3906normalize_constant_expr (expressionS *ex)
3907{
9ee2a2d4 3908 if (ex->X_op == O_constant
2051e8c4
MR
3909 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3910 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3911 - 0x80000000);
3912}
3913
3914/*
3915 * Sign-extend 32-bit mode address offsets that have bit 31 set and
3916 * all higher bits unset.
3917 */
3918static void
3919normalize_address_expr (expressionS *ex)
3920{
3921 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
3922 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
3923 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3924 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3925 - 0x80000000);
3926}
3927
438c16b8
TS
3928/*
3929 * Generate a "jalr" instruction with a relocation hint to the called
3930 * function. This occurs in NewABI PIC code.
3931 */
3932static void
67c0d1eb 3933macro_build_jalr (expressionS *ep)
438c16b8 3934{
685736be 3935 char *f = NULL;
b34976b6 3936
1180b5a4 3937 if (MIPS_JALR_HINT_P (ep))
f21f8242 3938 {
cc3d92a5 3939 frag_grow (8);
f21f8242
AO
3940 f = frag_more (0);
3941 }
67c0d1eb 3942 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 3943 if (MIPS_JALR_HINT_P (ep))
f21f8242 3944 fix_new_exp (frag_now, f - frag_now->fr_literal,
a105a300 3945 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3946}
3947
252b5132
RH
3948/*
3949 * Generate a "lui" instruction.
3950 */
3951static void
67c0d1eb 3952macro_build_lui (expressionS *ep, int regnum)
252b5132
RH
3953{
3954 expressionS high_expr;
1e915849 3955 const struct mips_opcode *mo;
252b5132 3956 struct mips_cl_insn insn;
f6688943
TS
3957 bfd_reloc_code_real_type r[3]
3958 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3959 const char *name = "lui";
3960 const char *fmt = "t,u";
252b5132 3961
9c2799c2 3962 gas_assert (! mips_opts.mips16);
252b5132 3963
4d7206a2 3964 high_expr = *ep;
252b5132
RH
3965
3966 if (high_expr.X_op == O_constant)
3967 {
54f4ddb3 3968 /* We can compute the instruction now without a relocation entry. */
e7d556df
TS
3969 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3970 >> 16) & 0xffff;
f6688943 3971 *r = BFD_RELOC_UNUSED;
252b5132 3972 }
78e1bb40 3973 else
252b5132 3974 {
9c2799c2 3975 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
3976 /* _gp_disp is a special case, used from s_cpload.
3977 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 3978 gas_assert (mips_pic == NO_PIC
78e1bb40 3979 || (! HAVE_NEWABI
aa6975fb
ILT
3980 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3981 || (! mips_in_shared
bbe506e8
TS
3982 && strcmp (S_GET_NAME (ep->X_add_symbol),
3983 "__gnu_local_gp") == 0));
f6688943 3984 *r = BFD_RELOC_HI16_S;
252b5132
RH
3985 }
3986
1e915849 3987 mo = hash_find (op_hash, name);
9c2799c2
NC
3988 gas_assert (strcmp (name, mo->name) == 0);
3989 gas_assert (strcmp (fmt, mo->args) == 0);
1e915849 3990 create_insn (&insn, mo);
252b5132 3991
bf12938e
RS
3992 insn.insn_opcode = insn.insn_mo->match;
3993 INSERT_OPERAND (RT, insn, regnum);
f6688943 3994 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3995 {
3996 insn.insn_opcode |= high_expr.X_add_number;
4d7206a2 3997 append_insn (&insn, NULL, r);
252b5132
RH
3998 }
3999 else
4d7206a2 4000 append_insn (&insn, &high_expr, r);
252b5132
RH
4001}
4002
885add95
CD
4003/* Generate a sequence of instructions to do a load or store from a constant
4004 offset off of a base register (breg) into/from a target register (treg),
4005 using AT if necessary. */
4006static void
67c0d1eb
RS
4007macro_build_ldst_constoffset (expressionS *ep, const char *op,
4008 int treg, int breg, int dbl)
885add95 4009{
9c2799c2 4010 gas_assert (ep->X_op == O_constant);
885add95 4011
256ab948 4012 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
4013 if (!dbl)
4014 normalize_constant_expr (ep);
256ab948 4015
67c1ffbe 4016 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 4017 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
4018 as_warn (_("operand overflow"));
4019
4020 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
4021 {
4022 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 4023 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
4024 }
4025 else
4026 {
4027 /* 32-bit offset, need multiple instructions and AT, like:
4028 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
4029 addu $tempreg,$tempreg,$breg
4030 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
4031 to handle the complete offset. */
67c0d1eb
RS
4032 macro_build_lui (ep, AT);
4033 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
4034 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 4035
741fe287 4036 if (!mips_opts.at)
8fc2e39e 4037 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
4038 }
4039}
4040
252b5132
RH
4041/* set_at()
4042 * Generates code to set the $at register to true (one)
4043 * if reg is less than the immediate expression.
4044 */
4045static void
67c0d1eb 4046set_at (int reg, int unsignedp)
252b5132
RH
4047{
4048 if (imm_expr.X_op == O_constant
4049 && imm_expr.X_add_number >= -0x8000
4050 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
4051 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
4052 AT, reg, BFD_RELOC_LO16);
252b5132
RH
4053 else
4054 {
67c0d1eb
RS
4055 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4056 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
4057 }
4058}
4059
4060/* Warn if an expression is not a constant. */
4061
4062static void
17a2f251 4063check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
4064{
4065 if (ex->X_op == O_big)
4066 as_bad (_("unsupported large constant"));
4067 else if (ex->X_op != O_constant)
9ee2a2d4
MR
4068 as_bad (_("Instruction %s requires absolute expression"),
4069 ip->insn_mo->name);
13757d0c 4070
9ee2a2d4
MR
4071 if (HAVE_32BIT_GPRS)
4072 normalize_constant_expr (ex);
252b5132
RH
4073}
4074
4075/* Count the leading zeroes by performing a binary chop. This is a
4076 bulky bit of source, but performance is a LOT better for the
4077 majority of values than a simple loop to count the bits:
4078 for (lcnt = 0; (lcnt < 32); lcnt++)
4079 if ((v) & (1 << (31 - lcnt)))
4080 break;
4081 However it is not code size friendly, and the gain will drop a bit
4082 on certain cached systems.
4083*/
4084#define COUNT_TOP_ZEROES(v) \
4085 (((v) & ~0xffff) == 0 \
4086 ? ((v) & ~0xff) == 0 \
4087 ? ((v) & ~0xf) == 0 \
4088 ? ((v) & ~0x3) == 0 \
4089 ? ((v) & ~0x1) == 0 \
4090 ? !(v) \
4091 ? 32 \
4092 : 31 \
4093 : 30 \
4094 : ((v) & ~0x7) == 0 \
4095 ? 29 \
4096 : 28 \
4097 : ((v) & ~0x3f) == 0 \
4098 ? ((v) & ~0x1f) == 0 \
4099 ? 27 \
4100 : 26 \
4101 : ((v) & ~0x7f) == 0 \
4102 ? 25 \
4103 : 24 \
4104 : ((v) & ~0xfff) == 0 \
4105 ? ((v) & ~0x3ff) == 0 \
4106 ? ((v) & ~0x1ff) == 0 \
4107 ? 23 \
4108 : 22 \
4109 : ((v) & ~0x7ff) == 0 \
4110 ? 21 \
4111 : 20 \
4112 : ((v) & ~0x3fff) == 0 \
4113 ? ((v) & ~0x1fff) == 0 \
4114 ? 19 \
4115 : 18 \
4116 : ((v) & ~0x7fff) == 0 \
4117 ? 17 \
4118 : 16 \
4119 : ((v) & ~0xffffff) == 0 \
4120 ? ((v) & ~0xfffff) == 0 \
4121 ? ((v) & ~0x3ffff) == 0 \
4122 ? ((v) & ~0x1ffff) == 0 \
4123 ? 15 \
4124 : 14 \
4125 : ((v) & ~0x7ffff) == 0 \
4126 ? 13 \
4127 : 12 \
4128 : ((v) & ~0x3fffff) == 0 \
4129 ? ((v) & ~0x1fffff) == 0 \
4130 ? 11 \
4131 : 10 \
4132 : ((v) & ~0x7fffff) == 0 \
4133 ? 9 \
4134 : 8 \
4135 : ((v) & ~0xfffffff) == 0 \
4136 ? ((v) & ~0x3ffffff) == 0 \
4137 ? ((v) & ~0x1ffffff) == 0 \
4138 ? 7 \
4139 : 6 \
4140 : ((v) & ~0x7ffffff) == 0 \
4141 ? 5 \
4142 : 4 \
4143 : ((v) & ~0x3fffffff) == 0 \
4144 ? ((v) & ~0x1fffffff) == 0 \
4145 ? 3 \
4146 : 2 \
4147 : ((v) & ~0x7fffffff) == 0 \
4148 ? 1 \
4149 : 0)
4150
4151/* load_register()
67c1ffbe 4152 * This routine generates the least number of instructions necessary to load
252b5132
RH
4153 * an absolute expression value into a register.
4154 */
4155static void
67c0d1eb 4156load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
4157{
4158 int freg;
4159 expressionS hi32, lo32;
4160
4161 if (ep->X_op != O_big)
4162 {
9c2799c2 4163 gas_assert (ep->X_op == O_constant);
256ab948
TS
4164
4165 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
4166 if (!dbl)
4167 normalize_constant_expr (ep);
256ab948
TS
4168
4169 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
4170 {
4171 /* We can handle 16 bit signed values with an addiu to
4172 $zero. No need to ever use daddiu here, since $zero and
4173 the result are always correct in 32 bit mode. */
67c0d1eb 4174 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
4175 return;
4176 }
4177 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
4178 {
4179 /* We can handle 16 bit unsigned values with an ori to
4180 $zero. */
67c0d1eb 4181 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
4182 return;
4183 }
256ab948 4184 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
4185 {
4186 /* 32 bit values require an lui. */
67c0d1eb 4187 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 4188 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 4189 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
4190 return;
4191 }
4192 }
4193
4194 /* The value is larger than 32 bits. */
4195
2051e8c4 4196 if (!dbl || HAVE_32BIT_GPRS)
252b5132 4197 {
55e08f71
NC
4198 char value[32];
4199
4200 sprintf_vma (value, ep->X_add_number);
20e1fcfd 4201 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 4202 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
4203 return;
4204 }
4205
4206 if (ep->X_op != O_big)
4207 {
4208 hi32 = *ep;
4209 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
4210 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
4211 hi32.X_add_number &= 0xffffffff;
4212 lo32 = *ep;
4213 lo32.X_add_number &= 0xffffffff;
4214 }
4215 else
4216 {
9c2799c2 4217 gas_assert (ep->X_add_number > 2);
252b5132
RH
4218 if (ep->X_add_number == 3)
4219 generic_bignum[3] = 0;
4220 else if (ep->X_add_number > 4)
4221 as_bad (_("Number larger than 64 bits"));
4222 lo32.X_op = O_constant;
4223 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
4224 hi32.X_op = O_constant;
4225 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
4226 }
4227
4228 if (hi32.X_add_number == 0)
4229 freg = 0;
4230 else
4231 {
4232 int shift, bit;
4233 unsigned long hi, lo;
4234
956cd1d6 4235 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
4236 {
4237 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
4238 {
67c0d1eb 4239 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
4240 return;
4241 }
4242 if (lo32.X_add_number & 0x80000000)
4243 {
67c0d1eb 4244 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 4245 if (lo32.X_add_number & 0xffff)
67c0d1eb 4246 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
4247 return;
4248 }
4249 }
252b5132
RH
4250
4251 /* Check for 16bit shifted constant. We know that hi32 is
4252 non-zero, so start the mask on the first bit of the hi32
4253 value. */
4254 shift = 17;
4255 do
beae10d5
KH
4256 {
4257 unsigned long himask, lomask;
4258
4259 if (shift < 32)
4260 {
4261 himask = 0xffff >> (32 - shift);
4262 lomask = (0xffff << shift) & 0xffffffff;
4263 }
4264 else
4265 {
4266 himask = 0xffff << (shift - 32);
4267 lomask = 0;
4268 }
4269 if ((hi32.X_add_number & ~(offsetT) himask) == 0
4270 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
4271 {
4272 expressionS tmp;
4273
4274 tmp.X_op = O_constant;
4275 if (shift < 32)
4276 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
4277 | (lo32.X_add_number >> shift));
4278 else
4279 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb
RS
4280 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
4281 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
4282 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
4283 return;
4284 }
f9419b05 4285 ++shift;
beae10d5
KH
4286 }
4287 while (shift <= (64 - 16));
252b5132
RH
4288
4289 /* Find the bit number of the lowest one bit, and store the
4290 shifted value in hi/lo. */
4291 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
4292 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
4293 if (lo != 0)
4294 {
4295 bit = 0;
4296 while ((lo & 1) == 0)
4297 {
4298 lo >>= 1;
4299 ++bit;
4300 }
4301 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
4302 hi >>= bit;
4303 }
4304 else
4305 {
4306 bit = 32;
4307 while ((hi & 1) == 0)
4308 {
4309 hi >>= 1;
4310 ++bit;
4311 }
4312 lo = hi;
4313 hi = 0;
4314 }
4315
4316 /* Optimize if the shifted value is a (power of 2) - 1. */
4317 if ((hi == 0 && ((lo + 1) & lo) == 0)
4318 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
4319 {
4320 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 4321 if (shift != 0)
beae10d5 4322 {
252b5132
RH
4323 expressionS tmp;
4324
4325 /* This instruction will set the register to be all
4326 ones. */
beae10d5
KH
4327 tmp.X_op = O_constant;
4328 tmp.X_add_number = (offsetT) -1;
67c0d1eb 4329 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
4330 if (bit != 0)
4331 {
4332 bit += shift;
67c0d1eb
RS
4333 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
4334 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 4335 }
67c0d1eb
RS
4336 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
4337 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
4338 return;
4339 }
4340 }
252b5132
RH
4341
4342 /* Sign extend hi32 before calling load_register, because we can
4343 generally get better code when we load a sign extended value. */
4344 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 4345 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 4346 load_register (reg, &hi32, 0);
252b5132
RH
4347 freg = reg;
4348 }
4349 if ((lo32.X_add_number & 0xffff0000) == 0)
4350 {
4351 if (freg != 0)
4352 {
67c0d1eb 4353 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
4354 freg = reg;
4355 }
4356 }
4357 else
4358 {
4359 expressionS mid16;
4360
956cd1d6 4361 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 4362 {
67c0d1eb
RS
4363 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
4364 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
4365 return;
4366 }
252b5132
RH
4367
4368 if (freg != 0)
4369 {
67c0d1eb 4370 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
252b5132
RH
4371 freg = reg;
4372 }
4373 mid16 = lo32;
4374 mid16.X_add_number >>= 16;
67c0d1eb
RS
4375 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
4376 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
252b5132
RH
4377 freg = reg;
4378 }
4379 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 4380 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
4381}
4382
269137b2
TS
4383static inline void
4384load_delay_nop (void)
4385{
4386 if (!gpr_interlocks)
4387 macro_build (NULL, "nop", "");
4388}
4389
252b5132
RH
4390/* Load an address into a register. */
4391
4392static void
67c0d1eb 4393load_address (int reg, expressionS *ep, int *used_at)
252b5132 4394{
252b5132
RH
4395 if (ep->X_op != O_constant
4396 && ep->X_op != O_symbol)
4397 {
4398 as_bad (_("expression too complex"));
4399 ep->X_op = O_constant;
4400 }
4401
4402 if (ep->X_op == O_constant)
4403 {
67c0d1eb 4404 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
4405 return;
4406 }
4407
4408 if (mips_pic == NO_PIC)
4409 {
4410 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4411 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4412 Otherwise we want
4413 lui $reg,<sym> (BFD_RELOC_HI16_S)
4414 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 4415 If we have an addend, we always use the latter form.
76b3015f 4416
d6bc6245
TS
4417 With 64bit address space and a usable $at we want
4418 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4419 lui $at,<sym> (BFD_RELOC_HI16_S)
4420 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
4421 daddiu $at,<sym> (BFD_RELOC_LO16)
4422 dsll32 $reg,0
3a482fd5 4423 daddu $reg,$reg,$at
76b3015f 4424
c03099e6 4425 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
4426 on superscalar processors.
4427 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4428 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
4429 dsll $reg,16
4430 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
4431 dsll $reg,16
4432 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
4433
4434 For GP relative symbols in 64bit address space we can use
4435 the same sequence as in 32bit address space. */
aed1a261 4436 if (HAVE_64BIT_SYMBOLS)
d6bc6245 4437 {
6caf9ef4
TS
4438 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
4439 && !nopic_need_relax (ep->X_add_symbol, 1))
4440 {
4441 relax_start (ep->X_add_symbol);
4442 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
4443 mips_gp_register, BFD_RELOC_GPREL16);
4444 relax_switch ();
4445 }
d6bc6245 4446
741fe287 4447 if (*used_at == 0 && mips_opts.at)
d6bc6245 4448 {
67c0d1eb
RS
4449 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
4450 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
4451 macro_build (ep, "daddiu", "t,r,j", reg, reg,
4452 BFD_RELOC_MIPS_HIGHER);
4453 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
4454 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
4455 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
4456 *used_at = 1;
4457 }
4458 else
4459 {
67c0d1eb
RS
4460 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
4461 macro_build (ep, "daddiu", "t,r,j", reg, reg,
4462 BFD_RELOC_MIPS_HIGHER);
4463 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4464 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
4465 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4466 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 4467 }
6caf9ef4
TS
4468
4469 if (mips_relax.sequence)
4470 relax_end ();
d6bc6245 4471 }
252b5132
RH
4472 else
4473 {
d6bc6245 4474 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 4475 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 4476 {
4d7206a2 4477 relax_start (ep->X_add_symbol);
67c0d1eb 4478 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 4479 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 4480 relax_switch ();
d6bc6245 4481 }
67c0d1eb
RS
4482 macro_build_lui (ep, reg);
4483 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
4484 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
4485 if (mips_relax.sequence)
4486 relax_end ();
d6bc6245 4487 }
252b5132 4488 }
0a44bf69 4489 else if (!mips_big_got)
252b5132
RH
4490 {
4491 expressionS ex;
4492
4493 /* If this is a reference to an external symbol, we want
4494 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4495 Otherwise we want
4496 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4497 nop
4498 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
4499 If there is a constant, it must be added in after.
4500
ed6fb7bd 4501 If we have NewABI, we want
f5040a92
AO
4502 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4503 unless we're referencing a global symbol with a non-zero
4504 offset, in which case cst must be added separately. */
ed6fb7bd
SC
4505 if (HAVE_NEWABI)
4506 {
f5040a92
AO
4507 if (ep->X_add_number)
4508 {
4d7206a2 4509 ex.X_add_number = ep->X_add_number;
f5040a92 4510 ep->X_add_number = 0;
4d7206a2 4511 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4512 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4513 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
4514 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4515 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4516 ex.X_op = O_constant;
67c0d1eb 4517 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 4518 reg, reg, BFD_RELOC_LO16);
f5040a92 4519 ep->X_add_number = ex.X_add_number;
4d7206a2 4520 relax_switch ();
f5040a92 4521 }
67c0d1eb 4522 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4523 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
4524 if (mips_relax.sequence)
4525 relax_end ();
ed6fb7bd
SC
4526 }
4527 else
4528 {
f5040a92
AO
4529 ex.X_add_number = ep->X_add_number;
4530 ep->X_add_number = 0;
67c0d1eb
RS
4531 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4532 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 4533 load_delay_nop ();
4d7206a2
RS
4534 relax_start (ep->X_add_symbol);
4535 relax_switch ();
67c0d1eb 4536 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4537 BFD_RELOC_LO16);
4d7206a2 4538 relax_end ();
ed6fb7bd 4539
f5040a92
AO
4540 if (ex.X_add_number != 0)
4541 {
4542 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4543 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4544 ex.X_op = O_constant;
67c0d1eb 4545 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 4546 reg, reg, BFD_RELOC_LO16);
f5040a92 4547 }
252b5132
RH
4548 }
4549 }
0a44bf69 4550 else if (mips_big_got)
252b5132
RH
4551 {
4552 expressionS ex;
252b5132
RH
4553
4554 /* This is the large GOT case. If this is a reference to an
4555 external symbol, we want
4556 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4557 addu $reg,$reg,$gp
4558 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
4559
4560 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
4561 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4562 nop
4563 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 4564 If there is a constant, it must be added in after.
f5040a92
AO
4565
4566 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
4567 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4568 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 4569 */
438c16b8
TS
4570 if (HAVE_NEWABI)
4571 {
4d7206a2 4572 ex.X_add_number = ep->X_add_number;
f5040a92 4573 ep->X_add_number = 0;
4d7206a2 4574 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4575 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4576 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4577 reg, reg, mips_gp_register);
4578 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4579 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
4580 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4581 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4582 else if (ex.X_add_number)
4583 {
4584 ex.X_op = O_constant;
67c0d1eb
RS
4585 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4586 BFD_RELOC_LO16);
f5040a92
AO
4587 }
4588
4589 ep->X_add_number = ex.X_add_number;
4d7206a2 4590 relax_switch ();
67c0d1eb 4591 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4592 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
4593 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4594 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 4595 relax_end ();
438c16b8 4596 }
252b5132 4597 else
438c16b8 4598 {
f5040a92
AO
4599 ex.X_add_number = ep->X_add_number;
4600 ep->X_add_number = 0;
4d7206a2 4601 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4602 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4603 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4604 reg, reg, mips_gp_register);
4605 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4606 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
4607 relax_switch ();
4608 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
4609 {
4610 /* We need a nop before loading from $gp. This special
4611 check is required because the lui which starts the main
4612 instruction stream does not refer to $gp, and so will not
4613 insert the nop which may be required. */
67c0d1eb 4614 macro_build (NULL, "nop", "");
438c16b8 4615 }
67c0d1eb 4616 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4617 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 4618 load_delay_nop ();
67c0d1eb 4619 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4620 BFD_RELOC_LO16);
4d7206a2 4621 relax_end ();
438c16b8 4622
f5040a92
AO
4623 if (ex.X_add_number != 0)
4624 {
4625 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4626 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4627 ex.X_op = O_constant;
67c0d1eb
RS
4628 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4629 BFD_RELOC_LO16);
f5040a92 4630 }
252b5132
RH
4631 }
4632 }
252b5132
RH
4633 else
4634 abort ();
8fc2e39e 4635
741fe287 4636 if (!mips_opts.at && *used_at == 1)
8fc2e39e 4637 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
4638}
4639
ea1fb5dc
RS
4640/* Move the contents of register SOURCE into register DEST. */
4641
4642static void
67c0d1eb 4643move_register (int dest, int source)
ea1fb5dc 4644{
67c0d1eb
RS
4645 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4646 dest, source, 0);
ea1fb5dc
RS
4647}
4648
4d7206a2 4649/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
4650 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4651 The two alternatives are:
4d7206a2
RS
4652
4653 Global symbol Local sybmol
4654 ------------- ------------
4655 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4656 ... ...
4657 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4658
4659 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
4660 emits the second for a 16-bit offset or add_got_offset_hilo emits
4661 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
4662
4663static void
67c0d1eb 4664load_got_offset (int dest, expressionS *local)
4d7206a2
RS
4665{
4666 expressionS global;
4667
4668 global = *local;
4669 global.X_add_number = 0;
4670
4671 relax_start (local->X_add_symbol);
67c0d1eb
RS
4672 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4673 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 4674 relax_switch ();
67c0d1eb
RS
4675 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4676 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
4677 relax_end ();
4678}
4679
4680static void
67c0d1eb 4681add_got_offset (int dest, expressionS *local)
4d7206a2
RS
4682{
4683 expressionS global;
4684
4685 global.X_op = O_constant;
4686 global.X_op_symbol = NULL;
4687 global.X_add_symbol = NULL;
4688 global.X_add_number = local->X_add_number;
4689
4690 relax_start (local->X_add_symbol);
67c0d1eb 4691 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
4692 dest, dest, BFD_RELOC_LO16);
4693 relax_switch ();
67c0d1eb 4694 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
4695 relax_end ();
4696}
4697
f6a22291
MR
4698static void
4699add_got_offset_hilo (int dest, expressionS *local, int tmp)
4700{
4701 expressionS global;
4702 int hold_mips_optimize;
4703
4704 global.X_op = O_constant;
4705 global.X_op_symbol = NULL;
4706 global.X_add_symbol = NULL;
4707 global.X_add_number = local->X_add_number;
4708
4709 relax_start (local->X_add_symbol);
4710 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4711 relax_switch ();
4712 /* Set mips_optimize around the lui instruction to avoid
4713 inserting an unnecessary nop after the lw. */
4714 hold_mips_optimize = mips_optimize;
4715 mips_optimize = 2;
4716 macro_build_lui (&global, tmp);
4717 mips_optimize = hold_mips_optimize;
4718 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4719 relax_end ();
4720
4721 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4722}
4723
252b5132
RH
4724/*
4725 * Build macros
4726 * This routine implements the seemingly endless macro or synthesized
4727 * instructions and addressing modes in the mips assembly language. Many
4728 * of these macros are simple and are similar to each other. These could
67c1ffbe 4729 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
4730 * this verbose method. Others are not simple macros but are more like
4731 * optimizing code generation.
4732 * One interesting optimization is when several store macros appear
67c1ffbe 4733 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
4734 * The ensuing load upper instructions are ommited. This implies some kind
4735 * of global optimization. We currently only optimize within a single macro.
4736 * For many of the load and store macros if the address is specified as a
4737 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4738 * first load register 'at' with zero and use it as the base register. The
4739 * mips assembler simply uses register $zero. Just one tiny optimization
4740 * we're missing.
4741 */
4742static void
17a2f251 4743macro (struct mips_cl_insn *ip)
252b5132 4744{
741fe287
MR
4745 unsigned int treg, sreg, dreg, breg;
4746 unsigned int tempreg;
252b5132 4747 int mask;
43841e91 4748 int used_at = 0;
252b5132
RH
4749 expressionS expr1;
4750 const char *s;
4751 const char *s2;
4752 const char *fmt;
4753 int likely = 0;
4754 int dbl = 0;
4755 int coproc = 0;
4756 int lr = 0;
4757 int imm = 0;
1abe91b1 4758 int call = 0;
252b5132 4759 int off;
67c0d1eb 4760 offsetT maxnum;
252b5132 4761 bfd_reloc_code_real_type r;
252b5132
RH
4762 int hold_mips_optimize;
4763
9c2799c2 4764 gas_assert (! mips_opts.mips16);
252b5132
RH
4765
4766 treg = (ip->insn_opcode >> 16) & 0x1f;
4767 dreg = (ip->insn_opcode >> 11) & 0x1f;
4768 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4769 mask = ip->insn_mo->mask;
4770
4771 expr1.X_op = O_constant;
4772 expr1.X_op_symbol = NULL;
4773 expr1.X_add_symbol = NULL;
4774 expr1.X_add_number = 1;
4775
4776 switch (mask)
4777 {
4778 case M_DABS:
4779 dbl = 1;
4780 case M_ABS:
4781 /* bgez $a0,.+12
4782 move v0,$a0
4783 sub v0,$zero,$a0
4784 */
4785
7d10b47d 4786 start_noreorder ();
252b5132
RH
4787
4788 expr1.X_add_number = 8;
67c0d1eb 4789 macro_build (&expr1, "bgez", "s,p", sreg);
252b5132 4790 if (dreg == sreg)
67c0d1eb 4791 macro_build (NULL, "nop", "", 0);
252b5132 4792 else
67c0d1eb
RS
4793 move_register (dreg, sreg);
4794 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132 4795
7d10b47d 4796 end_noreorder ();
8fc2e39e 4797 break;
252b5132
RH
4798
4799 case M_ADD_I:
4800 s = "addi";
4801 s2 = "add";
4802 goto do_addi;
4803 case M_ADDU_I:
4804 s = "addiu";
4805 s2 = "addu";
4806 goto do_addi;
4807 case M_DADD_I:
4808 dbl = 1;
4809 s = "daddi";
4810 s2 = "dadd";
4811 goto do_addi;
4812 case M_DADDU_I:
4813 dbl = 1;
4814 s = "daddiu";
4815 s2 = "daddu";
4816 do_addi:
4817 if (imm_expr.X_op == O_constant
4818 && imm_expr.X_add_number >= -0x8000
4819 && imm_expr.X_add_number < 0x8000)
4820 {
67c0d1eb 4821 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 4822 break;
252b5132 4823 }
8fc2e39e 4824 used_at = 1;
67c0d1eb
RS
4825 load_register (AT, &imm_expr, dbl);
4826 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4827 break;
4828
4829 case M_AND_I:
4830 s = "andi";
4831 s2 = "and";
4832 goto do_bit;
4833 case M_OR_I:
4834 s = "ori";
4835 s2 = "or";
4836 goto do_bit;
4837 case M_NOR_I:
4838 s = "";
4839 s2 = "nor";
4840 goto do_bit;
4841 case M_XOR_I:
4842 s = "xori";
4843 s2 = "xor";
4844 do_bit:
4845 if (imm_expr.X_op == O_constant
4846 && imm_expr.X_add_number >= 0
4847 && imm_expr.X_add_number < 0x10000)
4848 {
4849 if (mask != M_NOR_I)
67c0d1eb 4850 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4851 else
4852 {
67c0d1eb
RS
4853 macro_build (&imm_expr, "ori", "t,r,i",
4854 treg, sreg, BFD_RELOC_LO16);
4855 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 4856 }
8fc2e39e 4857 break;
252b5132
RH
4858 }
4859
8fc2e39e 4860 used_at = 1;
67c0d1eb
RS
4861 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4862 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4863 break;
4864
8b082fb1
TS
4865 case M_BALIGN:
4866 switch (imm_expr.X_add_number)
4867 {
4868 case 0:
4869 macro_build (NULL, "nop", "");
4870 break;
4871 case 2:
4872 macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
4873 break;
4874 default:
4875 macro_build (NULL, "balign", "t,s,2", treg, sreg,
4876 (int)imm_expr.X_add_number);
4877 break;
4878 }
4879 break;
4880
252b5132
RH
4881 case M_BEQ_I:
4882 s = "beq";
4883 goto beq_i;
4884 case M_BEQL_I:
4885 s = "beql";
4886 likely = 1;
4887 goto beq_i;
4888 case M_BNE_I:
4889 s = "bne";
4890 goto beq_i;
4891 case M_BNEL_I:
4892 s = "bnel";
4893 likely = 1;
4894 beq_i:
4895 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4896 {
67c0d1eb 4897 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
8fc2e39e 4898 break;
252b5132 4899 }
8fc2e39e 4900 used_at = 1;
67c0d1eb
RS
4901 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4902 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
252b5132
RH
4903 break;
4904
4905 case M_BGEL:
4906 likely = 1;
4907 case M_BGE:
4908 if (treg == 0)
4909 {
67c0d1eb 4910 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
8fc2e39e 4911 break;
252b5132
RH
4912 }
4913 if (sreg == 0)
4914 {
67c0d1eb 4915 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
8fc2e39e 4916 break;
252b5132 4917 }
8fc2e39e 4918 used_at = 1;
67c0d1eb
RS
4919 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4920 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4921 break;
4922
4923 case M_BGTL_I:
4924 likely = 1;
4925 case M_BGT_I:
4926 /* check for > max integer */
4927 maxnum = 0x7fffffff;
ca4e0257 4928 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4929 {
4930 maxnum <<= 16;
4931 maxnum |= 0xffff;
4932 maxnum <<= 16;
4933 maxnum |= 0xffff;
4934 }
4935 if (imm_expr.X_op == O_constant
4936 && imm_expr.X_add_number >= maxnum
ca4e0257 4937 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4938 {
4939 do_false:
4940 /* result is always false */
4941 if (! likely)
67c0d1eb 4942 macro_build (NULL, "nop", "", 0);
252b5132 4943 else
67c0d1eb 4944 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
8fc2e39e 4945 break;
252b5132
RH
4946 }
4947 if (imm_expr.X_op != O_constant)
4948 as_bad (_("Unsupported large constant"));
f9419b05 4949 ++imm_expr.X_add_number;
252b5132
RH
4950 /* FALLTHROUGH */
4951 case M_BGE_I:
4952 case M_BGEL_I:
4953 if (mask == M_BGEL_I)
4954 likely = 1;
4955 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4956 {
67c0d1eb 4957 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
8fc2e39e 4958 break;
252b5132
RH
4959 }
4960 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4961 {
67c0d1eb 4962 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
8fc2e39e 4963 break;
252b5132
RH
4964 }
4965 maxnum = 0x7fffffff;
ca4e0257 4966 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4967 {
4968 maxnum <<= 16;
4969 maxnum |= 0xffff;
4970 maxnum <<= 16;
4971 maxnum |= 0xffff;
4972 }
4973 maxnum = - maxnum - 1;
4974 if (imm_expr.X_op == O_constant
4975 && imm_expr.X_add_number <= maxnum
ca4e0257 4976 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4977 {
4978 do_true:
4979 /* result is always true */
4980 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 4981 macro_build (&offset_expr, "b", "p");
8fc2e39e 4982 break;
252b5132 4983 }
8fc2e39e 4984 used_at = 1;
67c0d1eb
RS
4985 set_at (sreg, 0);
4986 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4987 break;
4988
4989 case M_BGEUL:
4990 likely = 1;
4991 case M_BGEU:
4992 if (treg == 0)
4993 goto do_true;
4994 if (sreg == 0)
4995 {
67c0d1eb 4996 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4997 "s,t,p", 0, treg);
8fc2e39e 4998 break;
252b5132 4999 }
8fc2e39e 5000 used_at = 1;
67c0d1eb
RS
5001 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
5002 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
5003 break;
5004
5005 case M_BGTUL_I:
5006 likely = 1;
5007 case M_BGTU_I:
5008 if (sreg == 0
ca4e0257 5009 || (HAVE_32BIT_GPRS
252b5132 5010 && imm_expr.X_op == O_constant
956cd1d6 5011 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
5012 goto do_false;
5013 if (imm_expr.X_op != O_constant)
5014 as_bad (_("Unsupported large constant"));
f9419b05 5015 ++imm_expr.X_add_number;
252b5132
RH
5016 /* FALLTHROUGH */
5017 case M_BGEU_I:
5018 case M_BGEUL_I:
5019 if (mask == M_BGEUL_I)
5020 likely = 1;
5021 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5022 goto do_true;
5023 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
5024 {
67c0d1eb 5025 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 5026 "s,t,p", sreg, 0);
8fc2e39e 5027 break;
252b5132 5028 }
8fc2e39e 5029 used_at = 1;
67c0d1eb
RS
5030 set_at (sreg, 1);
5031 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
5032 break;
5033
5034 case M_BGTL:
5035 likely = 1;
5036 case M_BGT:
5037 if (treg == 0)
5038 {
67c0d1eb 5039 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
8fc2e39e 5040 break;
252b5132
RH
5041 }
5042 if (sreg == 0)
5043 {
67c0d1eb 5044 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
8fc2e39e 5045 break;
252b5132 5046 }
8fc2e39e 5047 used_at = 1;
67c0d1eb
RS
5048 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
5049 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
5050 break;
5051
5052 case M_BGTUL:
5053 likely = 1;
5054 case M_BGTU:
5055 if (treg == 0)
5056 {
67c0d1eb 5057 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 5058 "s,t,p", sreg, 0);
8fc2e39e 5059 break;
252b5132
RH
5060 }
5061 if (sreg == 0)
5062 goto do_false;
8fc2e39e 5063 used_at = 1;
67c0d1eb
RS
5064 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
5065 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
5066 break;
5067
5068 case M_BLEL:
5069 likely = 1;
5070 case M_BLE:
5071 if (treg == 0)
5072 {
67c0d1eb 5073 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
8fc2e39e 5074 break;
252b5132
RH
5075 }
5076 if (sreg == 0)
5077 {
67c0d1eb 5078 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
8fc2e39e 5079 break;
252b5132 5080 }
8fc2e39e 5081 used_at = 1;
67c0d1eb
RS
5082 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
5083 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
5084 break;
5085
5086 case M_BLEL_I:
5087 likely = 1;
5088 case M_BLE_I:
5089 maxnum = 0x7fffffff;
ca4e0257 5090 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
5091 {
5092 maxnum <<= 16;
5093 maxnum |= 0xffff;
5094 maxnum <<= 16;
5095 maxnum |= 0xffff;
5096 }
5097 if (imm_expr.X_op == O_constant
5098 && imm_expr.X_add_number >= maxnum
ca4e0257 5099 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
5100 goto do_true;
5101 if (imm_expr.X_op != O_constant)
5102 as_bad (_("Unsupported large constant"));
f9419b05 5103 ++imm_expr.X_add_number;
252b5132
RH
5104 /* FALLTHROUGH */
5105 case M_BLT_I:
5106 case M_BLTL_I:
5107 if (mask == M_BLTL_I)
5108 likely = 1;
5109 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5110 {
67c0d1eb 5111 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
8fc2e39e 5112 break;
252b5132
RH
5113 }
5114 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
5115 {
67c0d1eb 5116 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
8fc2e39e 5117 break;
252b5132 5118 }
8fc2e39e 5119 used_at = 1;
67c0d1eb
RS
5120 set_at (sreg, 0);
5121 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
5122 break;
5123
5124 case M_BLEUL:
5125 likely = 1;
5126 case M_BLEU:
5127 if (treg == 0)
5128 {
67c0d1eb 5129 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 5130 "s,t,p", sreg, 0);
8fc2e39e 5131 break;
252b5132
RH
5132 }
5133 if (sreg == 0)
5134 goto do_true;
8fc2e39e 5135 used_at = 1;
67c0d1eb
RS
5136 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
5137 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
5138 break;
5139
5140 case M_BLEUL_I:
5141 likely = 1;
5142 case M_BLEU_I:
5143 if (sreg == 0
ca4e0257 5144 || (HAVE_32BIT_GPRS
252b5132 5145 && imm_expr.X_op == O_constant
956cd1d6 5146 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
5147 goto do_true;
5148 if (imm_expr.X_op != O_constant)
5149 as_bad (_("Unsupported large constant"));
f9419b05 5150 ++imm_expr.X_add_number;
252b5132
RH
5151 /* FALLTHROUGH */
5152 case M_BLTU_I:
5153 case M_BLTUL_I:
5154 if (mask == M_BLTUL_I)
5155 likely = 1;
5156 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5157 goto do_false;
5158 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
5159 {
67c0d1eb 5160 macro_build (&offset_expr, likely ? "beql" : "beq",
252b5132 5161 "s,t,p", sreg, 0);
8fc2e39e 5162 break;
252b5132 5163 }
8fc2e39e 5164 used_at = 1;
67c0d1eb
RS
5165 set_at (sreg, 1);
5166 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
5167 break;
5168
5169 case M_BLTL:
5170 likely = 1;
5171 case M_BLT:
5172 if (treg == 0)
5173 {
67c0d1eb 5174 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
8fc2e39e 5175 break;
252b5132
RH
5176 }
5177 if (sreg == 0)
5178 {
67c0d1eb 5179 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
8fc2e39e 5180 break;
252b5132 5181 }
8fc2e39e 5182 used_at = 1;
67c0d1eb
RS
5183 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
5184 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
5185 break;
5186
5187 case M_BLTUL:
5188 likely = 1;
5189 case M_BLTU:
5190 if (treg == 0)
5191 goto do_false;
5192 if (sreg == 0)
5193 {
67c0d1eb 5194 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 5195 "s,t,p", 0, treg);
8fc2e39e 5196 break;
252b5132 5197 }
8fc2e39e 5198 used_at = 1;
67c0d1eb
RS
5199 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
5200 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
5201 break;
5202
5f74bc13
CD
5203 case M_DEXT:
5204 {
5205 unsigned long pos;
5206 unsigned long size;
5207
5208 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5209 {
5210 as_bad (_("Unsupported large constant"));
5211 pos = size = 1;
5212 }
5213 else
5214 {
5215 pos = (unsigned long) imm_expr.X_add_number;
5216 size = (unsigned long) imm2_expr.X_add_number;
5217 }
5218
5219 if (pos > 63)
5220 {
5221 as_bad (_("Improper position (%lu)"), pos);
5222 pos = 1;
5223 }
5224 if (size == 0 || size > 64
5225 || (pos + size - 1) > 63)
5226 {
5227 as_bad (_("Improper extract size (%lu, position %lu)"),
5228 size, pos);
5229 size = 1;
5230 }
5231
5232 if (size <= 32 && pos < 32)
5233 {
5234 s = "dext";
5235 fmt = "t,r,+A,+C";
5236 }
5237 else if (size <= 32)
5238 {
5239 s = "dextu";
5240 fmt = "t,r,+E,+H";
5241 }
5242 else
5243 {
5244 s = "dextm";
5245 fmt = "t,r,+A,+G";
5246 }
67c0d1eb 5247 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
5f74bc13 5248 }
8fc2e39e 5249 break;
5f74bc13
CD
5250
5251 case M_DINS:
5252 {
5253 unsigned long pos;
5254 unsigned long size;
5255
5256 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5257 {
5258 as_bad (_("Unsupported large constant"));
5259 pos = size = 1;
5260 }
5261 else
5262 {
5263 pos = (unsigned long) imm_expr.X_add_number;
5264 size = (unsigned long) imm2_expr.X_add_number;
5265 }
5266
5267 if (pos > 63)
5268 {
5269 as_bad (_("Improper position (%lu)"), pos);
5270 pos = 1;
5271 }
5272 if (size == 0 || size > 64
5273 || (pos + size - 1) > 63)
5274 {
5275 as_bad (_("Improper insert size (%lu, position %lu)"),
5276 size, pos);
5277 size = 1;
5278 }
5279
5280 if (pos < 32 && (pos + size - 1) < 32)
5281 {
5282 s = "dins";
5283 fmt = "t,r,+A,+B";
5284 }
5285 else if (pos >= 32)
5286 {
5287 s = "dinsu";
5288 fmt = "t,r,+E,+F";
5289 }
5290 else
5291 {
5292 s = "dinsm";
5293 fmt = "t,r,+A,+F";
5294 }
750bdd57
AS
5295 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
5296 (int) (pos + size - 1));
5f74bc13 5297 }
8fc2e39e 5298 break;
5f74bc13 5299
252b5132
RH
5300 case M_DDIV_3:
5301 dbl = 1;
5302 case M_DIV_3:
5303 s = "mflo";
5304 goto do_div3;
5305 case M_DREM_3:
5306 dbl = 1;
5307 case M_REM_3:
5308 s = "mfhi";
5309 do_div3:
5310 if (treg == 0)
5311 {
5312 as_warn (_("Divide by zero."));
5313 if (mips_trap)
67c0d1eb 5314 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 5315 else
67c0d1eb 5316 macro_build (NULL, "break", "c", 7);
8fc2e39e 5317 break;
252b5132
RH
5318 }
5319
7d10b47d 5320 start_noreorder ();
252b5132
RH
5321 if (mips_trap)
5322 {
67c0d1eb
RS
5323 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
5324 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
5325 }
5326 else
5327 {
5328 expr1.X_add_number = 8;
67c0d1eb
RS
5329 macro_build (&expr1, "bne", "s,t,p", treg, 0);
5330 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
5331 macro_build (NULL, "break", "c", 7);
252b5132
RH
5332 }
5333 expr1.X_add_number = -1;
8fc2e39e 5334 used_at = 1;
f6a22291 5335 load_register (AT, &expr1, dbl);
252b5132 5336 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
67c0d1eb 5337 macro_build (&expr1, "bne", "s,t,p", treg, AT);
252b5132
RH
5338 if (dbl)
5339 {
5340 expr1.X_add_number = 1;
f6a22291 5341 load_register (AT, &expr1, dbl);
67c0d1eb 5342 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
252b5132
RH
5343 }
5344 else
5345 {
5346 expr1.X_add_number = 0x80000000;
67c0d1eb 5347 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
252b5132
RH
5348 }
5349 if (mips_trap)
5350 {
67c0d1eb 5351 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
252b5132
RH
5352 /* We want to close the noreorder block as soon as possible, so
5353 that later insns are available for delay slot filling. */
7d10b47d 5354 end_noreorder ();
252b5132
RH
5355 }
5356 else
5357 {
5358 expr1.X_add_number = 8;
67c0d1eb
RS
5359 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
5360 macro_build (NULL, "nop", "", 0);
252b5132
RH
5361
5362 /* We want to close the noreorder block as soon as possible, so
5363 that later insns are available for delay slot filling. */
7d10b47d 5364 end_noreorder ();
252b5132 5365
67c0d1eb 5366 macro_build (NULL, "break", "c", 6);
252b5132 5367 }
67c0d1eb 5368 macro_build (NULL, s, "d", dreg);
252b5132
RH
5369 break;
5370
5371 case M_DIV_3I:
5372 s = "div";
5373 s2 = "mflo";
5374 goto do_divi;
5375 case M_DIVU_3I:
5376 s = "divu";
5377 s2 = "mflo";
5378 goto do_divi;
5379 case M_REM_3I:
5380 s = "div";
5381 s2 = "mfhi";
5382 goto do_divi;
5383 case M_REMU_3I:
5384 s = "divu";
5385 s2 = "mfhi";
5386 goto do_divi;
5387 case M_DDIV_3I:
5388 dbl = 1;
5389 s = "ddiv";
5390 s2 = "mflo";
5391 goto do_divi;
5392 case M_DDIVU_3I:
5393 dbl = 1;
5394 s = "ddivu";
5395 s2 = "mflo";
5396 goto do_divi;
5397 case M_DREM_3I:
5398 dbl = 1;
5399 s = "ddiv";
5400 s2 = "mfhi";
5401 goto do_divi;
5402 case M_DREMU_3I:
5403 dbl = 1;
5404 s = "ddivu";
5405 s2 = "mfhi";
5406 do_divi:
5407 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5408 {
5409 as_warn (_("Divide by zero."));
5410 if (mips_trap)
67c0d1eb 5411 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 5412 else
67c0d1eb 5413 macro_build (NULL, "break", "c", 7);
8fc2e39e 5414 break;
252b5132
RH
5415 }
5416 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
5417 {
5418 if (strcmp (s2, "mflo") == 0)
67c0d1eb 5419 move_register (dreg, sreg);
252b5132 5420 else
67c0d1eb 5421 move_register (dreg, 0);
8fc2e39e 5422 break;
252b5132
RH
5423 }
5424 if (imm_expr.X_op == O_constant
5425 && imm_expr.X_add_number == -1
5426 && s[strlen (s) - 1] != 'u')
5427 {
5428 if (strcmp (s2, "mflo") == 0)
5429 {
67c0d1eb 5430 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
5431 }
5432 else
67c0d1eb 5433 move_register (dreg, 0);
8fc2e39e 5434 break;
252b5132
RH
5435 }
5436
8fc2e39e 5437 used_at = 1;
67c0d1eb
RS
5438 load_register (AT, &imm_expr, dbl);
5439 macro_build (NULL, s, "z,s,t", sreg, AT);
5440 macro_build (NULL, s2, "d", dreg);
252b5132
RH
5441 break;
5442
5443 case M_DIVU_3:
5444 s = "divu";
5445 s2 = "mflo";
5446 goto do_divu3;
5447 case M_REMU_3:
5448 s = "divu";
5449 s2 = "mfhi";
5450 goto do_divu3;
5451 case M_DDIVU_3:
5452 s = "ddivu";
5453 s2 = "mflo";
5454 goto do_divu3;
5455 case M_DREMU_3:
5456 s = "ddivu";
5457 s2 = "mfhi";
5458 do_divu3:
7d10b47d 5459 start_noreorder ();
252b5132
RH
5460 if (mips_trap)
5461 {
67c0d1eb
RS
5462 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
5463 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
5464 /* We want to close the noreorder block as soon as possible, so
5465 that later insns are available for delay slot filling. */
7d10b47d 5466 end_noreorder ();
252b5132
RH
5467 }
5468 else
5469 {
5470 expr1.X_add_number = 8;
67c0d1eb
RS
5471 macro_build (&expr1, "bne", "s,t,p", treg, 0);
5472 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
5473
5474 /* We want to close the noreorder block as soon as possible, so
5475 that later insns are available for delay slot filling. */
7d10b47d 5476 end_noreorder ();
67c0d1eb 5477 macro_build (NULL, "break", "c", 7);
252b5132 5478 }
67c0d1eb 5479 macro_build (NULL, s2, "d", dreg);
8fc2e39e 5480 break;
252b5132 5481
1abe91b1
MR
5482 case M_DLCA_AB:
5483 dbl = 1;
5484 case M_LCA_AB:
5485 call = 1;
5486 goto do_la;
252b5132
RH
5487 case M_DLA_AB:
5488 dbl = 1;
5489 case M_LA_AB:
1abe91b1 5490 do_la:
252b5132
RH
5491 /* Load the address of a symbol into a register. If breg is not
5492 zero, we then add a base register to it. */
5493
3bec30a8
TS
5494 if (dbl && HAVE_32BIT_GPRS)
5495 as_warn (_("dla used to load 32-bit register"));
5496
c90bbe5b 5497 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
5498 as_warn (_("la used to load 64-bit address"));
5499
0c11417f
MR
5500 if (offset_expr.X_op == O_constant
5501 && offset_expr.X_add_number >= -0x8000
5502 && offset_expr.X_add_number < 0x8000)
5503 {
aed1a261 5504 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 5505 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 5506 break;
0c11417f
MR
5507 }
5508
741fe287 5509 if (mips_opts.at && (treg == breg))
afdbd6d0
CD
5510 {
5511 tempreg = AT;
5512 used_at = 1;
5513 }
5514 else
5515 {
5516 tempreg = treg;
afdbd6d0
CD
5517 }
5518
252b5132
RH
5519 if (offset_expr.X_op != O_symbol
5520 && offset_expr.X_op != O_constant)
5521 {
5522 as_bad (_("expression too complex"));
5523 offset_expr.X_op = O_constant;
5524 }
5525
252b5132 5526 if (offset_expr.X_op == O_constant)
aed1a261 5527 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
5528 else if (mips_pic == NO_PIC)
5529 {
d6bc6245 5530 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 5531 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5532 Otherwise we want
5533 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5534 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5535 If we have a constant, we need two instructions anyhow,
d6bc6245 5536 so we may as well always use the latter form.
76b3015f 5537
6caf9ef4
TS
5538 With 64bit address space and a usable $at we want
5539 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5540 lui $at,<sym> (BFD_RELOC_HI16_S)
5541 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5542 daddiu $at,<sym> (BFD_RELOC_LO16)
5543 dsll32 $tempreg,0
5544 daddu $tempreg,$tempreg,$at
5545
5546 If $at is already in use, we use a path which is suboptimal
5547 on superscalar processors.
5548 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5549 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5550 dsll $tempreg,16
5551 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5552 dsll $tempreg,16
5553 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
5554
5555 For GP relative symbols in 64bit address space we can use
5556 the same sequence as in 32bit address space. */
aed1a261 5557 if (HAVE_64BIT_SYMBOLS)
252b5132 5558 {
6caf9ef4
TS
5559 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5560 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5561 {
5562 relax_start (offset_expr.X_add_symbol);
5563 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5564 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5565 relax_switch ();
5566 }
d6bc6245 5567
741fe287 5568 if (used_at == 0 && mips_opts.at)
98d3f06f 5569 {
67c0d1eb 5570 macro_build (&offset_expr, "lui", "t,u",
17a2f251 5571 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 5572 macro_build (&offset_expr, "lui", "t,u",
17a2f251 5573 AT, BFD_RELOC_HI16_S);
67c0d1eb 5574 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5575 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 5576 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5577 AT, AT, BFD_RELOC_LO16);
67c0d1eb
RS
5578 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5579 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
5580 used_at = 1;
5581 }
5582 else
5583 {
67c0d1eb 5584 macro_build (&offset_expr, "lui", "t,u",
17a2f251 5585 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 5586 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5587 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb
RS
5588 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5589 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5590 tempreg, tempreg, BFD_RELOC_HI16_S);
67c0d1eb
RS
5591 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5592 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5593 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 5594 }
6caf9ef4
TS
5595
5596 if (mips_relax.sequence)
5597 relax_end ();
98d3f06f
KH
5598 }
5599 else
5600 {
5601 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 5602 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 5603 {
4d7206a2 5604 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5605 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5606 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5607 relax_switch ();
98d3f06f 5608 }
6943caf0
ILT
5609 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5610 as_bad (_("offset too large"));
67c0d1eb
RS
5611 macro_build_lui (&offset_expr, tempreg);
5612 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5613 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
5614 if (mips_relax.sequence)
5615 relax_end ();
98d3f06f 5616 }
252b5132 5617 }
0a44bf69 5618 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 5619 {
9117d219
NC
5620 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5621
252b5132
RH
5622 /* If this is a reference to an external symbol, and there
5623 is no constant, we want
5624 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 5625 or for lca or if tempreg is PIC_CALL_REG
9117d219 5626 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
5627 For a local symbol, we want
5628 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5629 nop
5630 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5631
5632 If we have a small constant, and this is a reference to
5633 an external symbol, we want
5634 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5635 nop
5636 addiu $tempreg,$tempreg,<constant>
5637 For a local symbol, we want the same instruction
5638 sequence, but we output a BFD_RELOC_LO16 reloc on the
5639 addiu instruction.
5640
5641 If we have a large constant, and this is a reference to
5642 an external symbol, we want
5643 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5644 lui $at,<hiconstant>
5645 addiu $at,$at,<loconstant>
5646 addu $tempreg,$tempreg,$at
5647 For a local symbol, we want the same instruction
5648 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 5649 addiu instruction.
ed6fb7bd
SC
5650 */
5651
4d7206a2 5652 if (offset_expr.X_add_number == 0)
252b5132 5653 {
0a44bf69
RS
5654 if (mips_pic == SVR4_PIC
5655 && breg == 0
5656 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
5657 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5658
5659 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5660 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5661 lw_reloc_type, mips_gp_register);
4d7206a2 5662 if (breg != 0)
252b5132
RH
5663 {
5664 /* We're going to put in an addu instruction using
5665 tempreg, so we may as well insert the nop right
5666 now. */
269137b2 5667 load_delay_nop ();
252b5132 5668 }
4d7206a2 5669 relax_switch ();
67c0d1eb
RS
5670 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5671 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5672 load_delay_nop ();
67c0d1eb
RS
5673 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5674 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 5675 relax_end ();
252b5132
RH
5676 /* FIXME: If breg == 0, and the next instruction uses
5677 $tempreg, then if this variant case is used an extra
5678 nop will be generated. */
5679 }
4d7206a2
RS
5680 else if (offset_expr.X_add_number >= -0x8000
5681 && offset_expr.X_add_number < 0x8000)
252b5132 5682 {
67c0d1eb 5683 load_got_offset (tempreg, &offset_expr);
269137b2 5684 load_delay_nop ();
67c0d1eb 5685 add_got_offset (tempreg, &offset_expr);
252b5132
RH
5686 }
5687 else
5688 {
4d7206a2
RS
5689 expr1.X_add_number = offset_expr.X_add_number;
5690 offset_expr.X_add_number =
5691 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
67c0d1eb 5692 load_got_offset (tempreg, &offset_expr);
f6a22291 5693 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
5694 /* If we are going to add in a base register, and the
5695 target register and the base register are the same,
5696 then we are using AT as a temporary register. Since
5697 we want to load the constant into AT, we add our
5698 current AT (from the global offset table) and the
5699 register into the register now, and pretend we were
5700 not using a base register. */
67c0d1eb 5701 if (breg == treg)
252b5132 5702 {
269137b2 5703 load_delay_nop ();
67c0d1eb 5704 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5705 treg, AT, breg);
252b5132
RH
5706 breg = 0;
5707 tempreg = treg;
252b5132 5708 }
f6a22291 5709 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
5710 used_at = 1;
5711 }
5712 }
0a44bf69 5713 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 5714 {
67c0d1eb 5715 int add_breg_early = 0;
f5040a92
AO
5716
5717 /* If this is a reference to an external, and there is no
5718 constant, or local symbol (*), with or without a
5719 constant, we want
5720 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 5721 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5722 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5723
5724 If we have a small constant, and this is a reference to
5725 an external symbol, we want
5726 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5727 addiu $tempreg,$tempreg,<constant>
5728
5729 If we have a large constant, and this is a reference to
5730 an external symbol, we want
5731 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5732 lui $at,<hiconstant>
5733 addiu $at,$at,<loconstant>
5734 addu $tempreg,$tempreg,$at
5735
5736 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5737 local symbols, even though it introduces an additional
5738 instruction. */
5739
f5040a92
AO
5740 if (offset_expr.X_add_number)
5741 {
4d7206a2 5742 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5743 offset_expr.X_add_number = 0;
5744
4d7206a2 5745 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5746 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5747 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5748
5749 if (expr1.X_add_number >= -0x8000
5750 && expr1.X_add_number < 0x8000)
5751 {
67c0d1eb
RS
5752 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5753 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5754 }
ecd13cd3 5755 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 5756 {
f5040a92
AO
5757 /* If we are going to add in a base register, and the
5758 target register and the base register are the same,
5759 then we are using AT as a temporary register. Since
5760 we want to load the constant into AT, we add our
5761 current AT (from the global offset table) and the
5762 register into the register now, and pretend we were
5763 not using a base register. */
5764 if (breg != treg)
5765 dreg = tempreg;
5766 else
5767 {
9c2799c2 5768 gas_assert (tempreg == AT);
67c0d1eb
RS
5769 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5770 treg, AT, breg);
f5040a92 5771 dreg = treg;
67c0d1eb 5772 add_breg_early = 1;
f5040a92
AO
5773 }
5774
f6a22291 5775 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5776 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5777 dreg, dreg, AT);
f5040a92 5778
f5040a92
AO
5779 used_at = 1;
5780 }
5781 else
5782 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5783
4d7206a2 5784 relax_switch ();
f5040a92
AO
5785 offset_expr.X_add_number = expr1.X_add_number;
5786
67c0d1eb
RS
5787 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5788 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5789 if (add_breg_early)
f5040a92 5790 {
67c0d1eb 5791 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 5792 treg, tempreg, breg);
f5040a92
AO
5793 breg = 0;
5794 tempreg = treg;
5795 }
4d7206a2 5796 relax_end ();
f5040a92 5797 }
4d7206a2 5798 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 5799 {
4d7206a2 5800 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5801 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5802 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 5803 relax_switch ();
67c0d1eb
RS
5804 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5805 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 5806 relax_end ();
f5040a92 5807 }
4d7206a2 5808 else
f5040a92 5809 {
67c0d1eb
RS
5810 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5811 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5812 }
5813 }
0a44bf69 5814 else if (mips_big_got && !HAVE_NEWABI)
252b5132 5815 {
67c0d1eb 5816 int gpdelay;
9117d219
NC
5817 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5818 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5819 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5820
5821 /* This is the large GOT case. If this is a reference to an
5822 external symbol, and there is no constant, we want
5823 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5824 addu $tempreg,$tempreg,$gp
5825 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5826 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
5827 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5828 addu $tempreg,$tempreg,$gp
5829 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5830 For a local symbol, we want
5831 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5832 nop
5833 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5834
5835 If we have a small constant, and this is a reference to
5836 an external symbol, we want
5837 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5838 addu $tempreg,$tempreg,$gp
5839 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5840 nop
5841 addiu $tempreg,$tempreg,<constant>
5842 For a local symbol, we want
5843 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5844 nop
5845 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5846
5847 If we have a large constant, and this is a reference to
5848 an external symbol, we want
5849 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5850 addu $tempreg,$tempreg,$gp
5851 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5852 lui $at,<hiconstant>
5853 addiu $at,$at,<loconstant>
5854 addu $tempreg,$tempreg,$at
5855 For a local symbol, we want
5856 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5857 lui $at,<hiconstant>
5858 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5859 addu $tempreg,$tempreg,$at
f5040a92 5860 */
438c16b8 5861
252b5132
RH
5862 expr1.X_add_number = offset_expr.X_add_number;
5863 offset_expr.X_add_number = 0;
4d7206a2 5864 relax_start (offset_expr.X_add_symbol);
67c0d1eb 5865 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
5866 if (expr1.X_add_number == 0 && breg == 0
5867 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
5868 {
5869 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5870 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5871 }
67c0d1eb
RS
5872 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5873 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5874 tempreg, tempreg, mips_gp_register);
67c0d1eb 5875 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 5876 tempreg, lw_reloc_type, tempreg);
252b5132
RH
5877 if (expr1.X_add_number == 0)
5878 {
67c0d1eb 5879 if (breg != 0)
252b5132
RH
5880 {
5881 /* We're going to put in an addu instruction using
5882 tempreg, so we may as well insert the nop right
5883 now. */
269137b2 5884 load_delay_nop ();
252b5132 5885 }
252b5132
RH
5886 }
5887 else if (expr1.X_add_number >= -0x8000
5888 && expr1.X_add_number < 0x8000)
5889 {
269137b2 5890 load_delay_nop ();
67c0d1eb 5891 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5892 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5893 }
5894 else
5895 {
252b5132
RH
5896 /* If we are going to add in a base register, and the
5897 target register and the base register are the same,
5898 then we are using AT as a temporary register. Since
5899 we want to load the constant into AT, we add our
5900 current AT (from the global offset table) and the
5901 register into the register now, and pretend we were
5902 not using a base register. */
5903 if (breg != treg)
67c0d1eb 5904 dreg = tempreg;
252b5132
RH
5905 else
5906 {
9c2799c2 5907 gas_assert (tempreg == AT);
269137b2 5908 load_delay_nop ();
67c0d1eb 5909 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5910 treg, AT, breg);
252b5132 5911 dreg = treg;
252b5132
RH
5912 }
5913
f6a22291 5914 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5915 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 5916
252b5132
RH
5917 used_at = 1;
5918 }
4d7206a2
RS
5919 offset_expr.X_add_number =
5920 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5921 relax_switch ();
252b5132 5922
67c0d1eb 5923 if (gpdelay)
252b5132
RH
5924 {
5925 /* This is needed because this instruction uses $gp, but
f5040a92 5926 the first instruction on the main stream does not. */
67c0d1eb 5927 macro_build (NULL, "nop", "");
252b5132 5928 }
ed6fb7bd 5929
67c0d1eb
RS
5930 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5931 local_reloc_type, mips_gp_register);
f5040a92 5932 if (expr1.X_add_number >= -0x8000
252b5132
RH
5933 && expr1.X_add_number < 0x8000)
5934 {
269137b2 5935 load_delay_nop ();
67c0d1eb
RS
5936 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5937 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 5938 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5939 register, the external symbol case ended with a load,
5940 so if the symbol turns out to not be external, and
5941 the next instruction uses tempreg, an unnecessary nop
5942 will be inserted. */
252b5132
RH
5943 }
5944 else
5945 {
5946 if (breg == treg)
5947 {
5948 /* We must add in the base register now, as in the
f5040a92 5949 external symbol case. */
9c2799c2 5950 gas_assert (tempreg == AT);
269137b2 5951 load_delay_nop ();
67c0d1eb 5952 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5953 treg, AT, breg);
252b5132
RH
5954 tempreg = treg;
5955 /* We set breg to 0 because we have arranged to add
f5040a92 5956 it in in both cases. */
252b5132
RH
5957 breg = 0;
5958 }
5959
67c0d1eb
RS
5960 macro_build_lui (&expr1, AT);
5961 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5962 AT, AT, BFD_RELOC_LO16);
67c0d1eb 5963 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5964 tempreg, tempreg, AT);
8fc2e39e 5965 used_at = 1;
252b5132 5966 }
4d7206a2 5967 relax_end ();
252b5132 5968 }
0a44bf69 5969 else if (mips_big_got && HAVE_NEWABI)
f5040a92 5970 {
f5040a92
AO
5971 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5972 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 5973 int add_breg_early = 0;
f5040a92
AO
5974
5975 /* This is the large GOT case. If this is a reference to an
5976 external symbol, and there is no constant, we want
5977 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5978 add $tempreg,$tempreg,$gp
5979 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5980 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5981 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5982 add $tempreg,$tempreg,$gp
5983 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5984
5985 If we have a small constant, and this is a reference to
5986 an external symbol, we want
5987 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5988 add $tempreg,$tempreg,$gp
5989 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5990 addi $tempreg,$tempreg,<constant>
5991
5992 If we have a large constant, and this is a reference to
5993 an external symbol, we want
5994 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5995 addu $tempreg,$tempreg,$gp
5996 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5997 lui $at,<hiconstant>
5998 addi $at,$at,<loconstant>
5999 add $tempreg,$tempreg,$at
6000
6001 If we have NewABI, and we know it's a local symbol, we want
6002 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6003 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
6004 otherwise we have to resort to GOT_HI16/GOT_LO16. */
6005
4d7206a2 6006 relax_start (offset_expr.X_add_symbol);
f5040a92 6007
4d7206a2 6008 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
6009 offset_expr.X_add_number = 0;
6010
1abe91b1
MR
6011 if (expr1.X_add_number == 0 && breg == 0
6012 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
6013 {
6014 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
6015 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
6016 }
67c0d1eb
RS
6017 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
6018 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6019 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
6020 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6021 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
6022
6023 if (expr1.X_add_number == 0)
4d7206a2 6024 ;
f5040a92
AO
6025 else if (expr1.X_add_number >= -0x8000
6026 && expr1.X_add_number < 0x8000)
6027 {
67c0d1eb 6028 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 6029 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 6030 }
ecd13cd3 6031 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 6032 {
f5040a92
AO
6033 /* If we are going to add in a base register, and the
6034 target register and the base register are the same,
6035 then we are using AT as a temporary register. Since
6036 we want to load the constant into AT, we add our
6037 current AT (from the global offset table) and the
6038 register into the register now, and pretend we were
6039 not using a base register. */
6040 if (breg != treg)
6041 dreg = tempreg;
6042 else
6043 {
9c2799c2 6044 gas_assert (tempreg == AT);
67c0d1eb 6045 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6046 treg, AT, breg);
f5040a92 6047 dreg = treg;
67c0d1eb 6048 add_breg_early = 1;
f5040a92
AO
6049 }
6050
f6a22291 6051 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 6052 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 6053
f5040a92
AO
6054 used_at = 1;
6055 }
6056 else
6057 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
6058
4d7206a2 6059 relax_switch ();
f5040a92 6060 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6061 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6062 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6063 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6064 tempreg, BFD_RELOC_MIPS_GOT_OFST);
6065 if (add_breg_early)
f5040a92 6066 {
67c0d1eb 6067 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6068 treg, tempreg, breg);
f5040a92
AO
6069 breg = 0;
6070 tempreg = treg;
6071 }
4d7206a2 6072 relax_end ();
f5040a92 6073 }
252b5132
RH
6074 else
6075 abort ();
6076
6077 if (breg != 0)
aed1a261 6078 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
6079 break;
6080
52b6b6b9
JM
6081 case M_MSGSND:
6082 {
6083 unsigned long temp = (treg << 16) | (0x01);
6084 macro_build (NULL, "c2", "C", temp);
6085 }
6086 /* AT is not used, just return */
6087 return;
6088
6089 case M_MSGLD:
6090 {
6091 unsigned long temp = (0x02);
6092 macro_build (NULL, "c2", "C", temp);
6093 }
6094 /* AT is not used, just return */
6095 return;
6096
6097 case M_MSGLD_T:
6098 {
6099 unsigned long temp = (treg << 16) | (0x02);
6100 macro_build (NULL, "c2", "C", temp);
6101 }
6102 /* AT is not used, just return */
6103 return;
6104
6105 case M_MSGWAIT:
6106 macro_build (NULL, "c2", "C", 3);
6107 /* AT is not used, just return */
6108 return;
6109
6110 case M_MSGWAIT_T:
6111 {
6112 unsigned long temp = (treg << 16) | 0x03;
6113 macro_build (NULL, "c2", "C", temp);
6114 }
6115 /* AT is not used, just return */
6116 return;
6117
252b5132
RH
6118 case M_J_A:
6119 /* The j instruction may not be used in PIC code, since it
6120 requires an absolute address. We convert it to a b
6121 instruction. */
6122 if (mips_pic == NO_PIC)
67c0d1eb 6123 macro_build (&offset_expr, "j", "a");
252b5132 6124 else
67c0d1eb 6125 macro_build (&offset_expr, "b", "p");
8fc2e39e 6126 break;
252b5132
RH
6127
6128 /* The jal instructions must be handled as macros because when
6129 generating PIC code they expand to multi-instruction
6130 sequences. Normally they are simple instructions. */
6131 case M_JAL_1:
6132 dreg = RA;
6133 /* Fall through. */
6134 case M_JAL_2:
3e722fb5 6135 if (mips_pic == NO_PIC)
67c0d1eb 6136 macro_build (NULL, "jalr", "d,s", dreg, sreg);
0a44bf69 6137 else
252b5132
RH
6138 {
6139 if (sreg != PIC_CALL_REG)
6140 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 6141
67c0d1eb 6142 macro_build (NULL, "jalr", "d,s", dreg, sreg);
0a44bf69 6143 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 6144 {
6478892d
TS
6145 if (mips_cprestore_offset < 0)
6146 as_warn (_("No .cprestore pseudo-op used in PIC code"));
6147 else
6148 {
7a621144
DJ
6149 if (! mips_frame_reg_valid)
6150 {
6151 as_warn (_("No .frame pseudo-op used in PIC code"));
6152 /* Quiet this warning. */
6153 mips_frame_reg_valid = 1;
6154 }
6155 if (! mips_cprestore_valid)
6156 {
6157 as_warn (_("No .cprestore pseudo-op used in PIC code"));
6158 /* Quiet this warning. */
6159 mips_cprestore_valid = 1;
6160 }
6478892d 6161 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 6162 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 6163 mips_gp_register,
256ab948
TS
6164 mips_frame_reg,
6165 HAVE_64BIT_ADDRESSES);
6478892d 6166 }
252b5132
RH
6167 }
6168 }
252b5132 6169
8fc2e39e 6170 break;
252b5132
RH
6171
6172 case M_JAL_A:
6173 if (mips_pic == NO_PIC)
67c0d1eb 6174 macro_build (&offset_expr, "jal", "a");
252b5132
RH
6175 else if (mips_pic == SVR4_PIC)
6176 {
6177 /* If this is a reference to an external symbol, and we are
6178 using a small GOT, we want
6179 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
6180 nop
f9419b05 6181 jalr $ra,$25
252b5132
RH
6182 nop
6183 lw $gp,cprestore($sp)
6184 The cprestore value is set using the .cprestore
6185 pseudo-op. If we are using a big GOT, we want
6186 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
6187 addu $25,$25,$gp
6188 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
6189 nop
f9419b05 6190 jalr $ra,$25
252b5132
RH
6191 nop
6192 lw $gp,cprestore($sp)
6193 If the symbol is not external, we want
6194 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6195 nop
6196 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 6197 jalr $ra,$25
252b5132 6198 nop
438c16b8 6199 lw $gp,cprestore($sp)
f5040a92
AO
6200
6201 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
6202 sequences above, minus nops, unless the symbol is local,
6203 which enables us to use GOT_PAGE/GOT_OFST (big got) or
6204 GOT_DISP. */
438c16b8 6205 if (HAVE_NEWABI)
252b5132 6206 {
f5040a92
AO
6207 if (! mips_big_got)
6208 {
4d7206a2 6209 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6210 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6211 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 6212 mips_gp_register);
4d7206a2 6213 relax_switch ();
67c0d1eb
RS
6214 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6215 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
6216 mips_gp_register);
6217 relax_end ();
f5040a92
AO
6218 }
6219 else
6220 {
4d7206a2 6221 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6222 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
6223 BFD_RELOC_MIPS_CALL_HI16);
6224 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
6225 PIC_CALL_REG, mips_gp_register);
6226 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6227 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
6228 PIC_CALL_REG);
4d7206a2 6229 relax_switch ();
67c0d1eb
RS
6230 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6231 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
6232 mips_gp_register);
6233 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6234 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 6235 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 6236 relax_end ();
f5040a92 6237 }
684022ea 6238
67c0d1eb 6239 macro_build_jalr (&offset_expr);
252b5132
RH
6240 }
6241 else
6242 {
4d7206a2 6243 relax_start (offset_expr.X_add_symbol);
438c16b8
TS
6244 if (! mips_big_got)
6245 {
67c0d1eb
RS
6246 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6247 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 6248 mips_gp_register);
269137b2 6249 load_delay_nop ();
4d7206a2 6250 relax_switch ();
438c16b8 6251 }
252b5132 6252 else
252b5132 6253 {
67c0d1eb
RS
6254 int gpdelay;
6255
6256 gpdelay = reg_needs_delay (mips_gp_register);
6257 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
6258 BFD_RELOC_MIPS_CALL_HI16);
6259 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
6260 PIC_CALL_REG, mips_gp_register);
6261 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6262 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
6263 PIC_CALL_REG);
269137b2 6264 load_delay_nop ();
4d7206a2 6265 relax_switch ();
67c0d1eb
RS
6266 if (gpdelay)
6267 macro_build (NULL, "nop", "");
252b5132 6268 }
67c0d1eb
RS
6269 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6270 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 6271 mips_gp_register);
269137b2 6272 load_delay_nop ();
67c0d1eb
RS
6273 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6274 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 6275 relax_end ();
67c0d1eb 6276 macro_build_jalr (&offset_expr);
438c16b8 6277
6478892d
TS
6278 if (mips_cprestore_offset < 0)
6279 as_warn (_("No .cprestore pseudo-op used in PIC code"));
6280 else
6281 {
7a621144
DJ
6282 if (! mips_frame_reg_valid)
6283 {
6284 as_warn (_("No .frame pseudo-op used in PIC code"));
6285 /* Quiet this warning. */
6286 mips_frame_reg_valid = 1;
6287 }
6288 if (! mips_cprestore_valid)
6289 {
6290 as_warn (_("No .cprestore pseudo-op used in PIC code"));
6291 /* Quiet this warning. */
6292 mips_cprestore_valid = 1;
6293 }
6478892d 6294 if (mips_opts.noreorder)
67c0d1eb 6295 macro_build (NULL, "nop", "");
6478892d 6296 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 6297 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 6298 mips_gp_register,
256ab948
TS
6299 mips_frame_reg,
6300 HAVE_64BIT_ADDRESSES);
6478892d 6301 }
252b5132
RH
6302 }
6303 }
0a44bf69
RS
6304 else if (mips_pic == VXWORKS_PIC)
6305 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
6306 else
6307 abort ();
6308
8fc2e39e 6309 break;
252b5132
RH
6310
6311 case M_LB_AB:
6312 s = "lb";
6313 goto ld;
6314 case M_LBU_AB:
6315 s = "lbu";
6316 goto ld;
6317 case M_LH_AB:
6318 s = "lh";
6319 goto ld;
6320 case M_LHU_AB:
6321 s = "lhu";
6322 goto ld;
6323 case M_LW_AB:
6324 s = "lw";
6325 goto ld;
6326 case M_LWC0_AB:
6327 s = "lwc0";
bdaaa2e1 6328 /* Itbl support may require additional care here. */
252b5132
RH
6329 coproc = 1;
6330 goto ld;
6331 case M_LWC1_AB:
6332 s = "lwc1";
bdaaa2e1 6333 /* Itbl support may require additional care here. */
252b5132
RH
6334 coproc = 1;
6335 goto ld;
6336 case M_LWC2_AB:
6337 s = "lwc2";
bdaaa2e1 6338 /* Itbl support may require additional care here. */
252b5132
RH
6339 coproc = 1;
6340 goto ld;
6341 case M_LWC3_AB:
6342 s = "lwc3";
bdaaa2e1 6343 /* Itbl support may require additional care here. */
252b5132
RH
6344 coproc = 1;
6345 goto ld;
6346 case M_LWL_AB:
6347 s = "lwl";
6348 lr = 1;
6349 goto ld;
6350 case M_LWR_AB:
6351 s = "lwr";
6352 lr = 1;
6353 goto ld;
6354 case M_LDC1_AB:
252b5132 6355 s = "ldc1";
bdaaa2e1 6356 /* Itbl support may require additional care here. */
252b5132
RH
6357 coproc = 1;
6358 goto ld;
6359 case M_LDC2_AB:
6360 s = "ldc2";
bdaaa2e1 6361 /* Itbl support may require additional care here. */
252b5132
RH
6362 coproc = 1;
6363 goto ld;
6364 case M_LDC3_AB:
6365 s = "ldc3";
bdaaa2e1 6366 /* Itbl support may require additional care here. */
252b5132
RH
6367 coproc = 1;
6368 goto ld;
6369 case M_LDL_AB:
6370 s = "ldl";
6371 lr = 1;
6372 goto ld;
6373 case M_LDR_AB:
6374 s = "ldr";
6375 lr = 1;
6376 goto ld;
6377 case M_LL_AB:
6378 s = "ll";
6379 goto ld;
6380 case M_LLD_AB:
6381 s = "lld";
6382 goto ld;
6383 case M_LWU_AB:
6384 s = "lwu";
6385 ld:
8fc2e39e 6386 if (breg == treg || coproc || lr)
252b5132
RH
6387 {
6388 tempreg = AT;
6389 used_at = 1;
6390 }
6391 else
6392 {
6393 tempreg = treg;
252b5132
RH
6394 }
6395 goto ld_st;
6396 case M_SB_AB:
6397 s = "sb";
6398 goto st;
6399 case M_SH_AB:
6400 s = "sh";
6401 goto st;
6402 case M_SW_AB:
6403 s = "sw";
6404 goto st;
6405 case M_SWC0_AB:
6406 s = "swc0";
bdaaa2e1 6407 /* Itbl support may require additional care here. */
252b5132
RH
6408 coproc = 1;
6409 goto st;
6410 case M_SWC1_AB:
6411 s = "swc1";
bdaaa2e1 6412 /* Itbl support may require additional care here. */
252b5132
RH
6413 coproc = 1;
6414 goto st;
6415 case M_SWC2_AB:
6416 s = "swc2";
bdaaa2e1 6417 /* Itbl support may require additional care here. */
252b5132
RH
6418 coproc = 1;
6419 goto st;
6420 case M_SWC3_AB:
6421 s = "swc3";
bdaaa2e1 6422 /* Itbl support may require additional care here. */
252b5132
RH
6423 coproc = 1;
6424 goto st;
6425 case M_SWL_AB:
6426 s = "swl";
6427 goto st;
6428 case M_SWR_AB:
6429 s = "swr";
6430 goto st;
6431 case M_SC_AB:
6432 s = "sc";
6433 goto st;
6434 case M_SCD_AB:
6435 s = "scd";
6436 goto st;
d43b4baf
TS
6437 case M_CACHE_AB:
6438 s = "cache";
6439 goto st;
252b5132 6440 case M_SDC1_AB:
252b5132
RH
6441 s = "sdc1";
6442 coproc = 1;
bdaaa2e1 6443 /* Itbl support may require additional care here. */
252b5132
RH
6444 goto st;
6445 case M_SDC2_AB:
6446 s = "sdc2";
bdaaa2e1 6447 /* Itbl support may require additional care here. */
252b5132
RH
6448 coproc = 1;
6449 goto st;
6450 case M_SDC3_AB:
6451 s = "sdc3";
bdaaa2e1 6452 /* Itbl support may require additional care here. */
252b5132
RH
6453 coproc = 1;
6454 goto st;
6455 case M_SDL_AB:
6456 s = "sdl";
6457 goto st;
6458 case M_SDR_AB:
6459 s = "sdr";
6460 st:
8fc2e39e
TS
6461 tempreg = AT;
6462 used_at = 1;
252b5132 6463 ld_st:
b19e8a9b
AN
6464 if (coproc
6465 && NO_ISA_COP (mips_opts.arch)
6466 && (ip->insn_mo->pinfo2 & (INSN2_M_FP_S | INSN2_M_FP_D)) == 0)
6467 {
6468 as_bad (_("opcode not supported on this processor: %s"),
6469 mips_cpu_info_from_arch (mips_opts.arch)->name);
6470 break;
6471 }
6472
bdaaa2e1 6473 /* Itbl support may require additional care here. */
252b5132
RH
6474 if (mask == M_LWC1_AB
6475 || mask == M_SWC1_AB
6476 || mask == M_LDC1_AB
6477 || mask == M_SDC1_AB
6478 || mask == M_L_DAB
6479 || mask == M_S_DAB)
6480 fmt = "T,o(b)";
d43b4baf
TS
6481 else if (mask == M_CACHE_AB)
6482 fmt = "k,o(b)";
252b5132
RH
6483 else if (coproc)
6484 fmt = "E,o(b)";
6485 else
6486 fmt = "t,o(b)";
6487
6488 if (offset_expr.X_op != O_constant
6489 && offset_expr.X_op != O_symbol)
6490 {
6491 as_bad (_("expression too complex"));
6492 offset_expr.X_op = O_constant;
6493 }
6494
2051e8c4
MR
6495 if (HAVE_32BIT_ADDRESSES
6496 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
6497 {
6498 char value [32];
6499
6500 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 6501 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 6502 }
2051e8c4 6503
252b5132
RH
6504 /* A constant expression in PIC code can be handled just as it
6505 is in non PIC code. */
aed1a261
RS
6506 if (offset_expr.X_op == O_constant)
6507 {
aed1a261
RS
6508 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
6509 & ~(bfd_vma) 0xffff);
2051e8c4 6510 normalize_address_expr (&expr1);
aed1a261
RS
6511 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
6512 if (breg != 0)
6513 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6514 tempreg, tempreg, breg);
6515 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6516 }
6517 else if (mips_pic == NO_PIC)
252b5132
RH
6518 {
6519 /* If this is a reference to a GP relative symbol, and there
6520 is no base register, we want
cdf6fd85 6521 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6522 Otherwise, if there is no base register, we want
6523 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6524 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6525 If we have a constant, we need two instructions anyhow,
6526 so we always use the latter form.
6527
6528 If we have a base register, and this is a reference to a
6529 GP relative symbol, we want
6530 addu $tempreg,$breg,$gp
cdf6fd85 6531 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6532 Otherwise we want
6533 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6534 addu $tempreg,$tempreg,$breg
6535 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 6536 With a constant we always use the latter case.
76b3015f 6537
d6bc6245
TS
6538 With 64bit address space and no base register and $at usable,
6539 we want
6540 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6541 lui $at,<sym> (BFD_RELOC_HI16_S)
6542 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6543 dsll32 $tempreg,0
6544 daddu $tempreg,$at
6545 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6546 If we have a base register, we want
6547 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6548 lui $at,<sym> (BFD_RELOC_HI16_S)
6549 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6550 daddu $at,$breg
6551 dsll32 $tempreg,0
6552 daddu $tempreg,$at
6553 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6554
6555 Without $at we can't generate the optimal path for superscalar
6556 processors here since this would require two temporary registers.
6557 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6558 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6559 dsll $tempreg,16
6560 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6561 dsll $tempreg,16
6562 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6563 If we have a base register, we want
6564 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6565 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6566 dsll $tempreg,16
6567 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6568 dsll $tempreg,16
6569 daddu $tempreg,$tempreg,$breg
6570 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 6571
6caf9ef4 6572 For GP relative symbols in 64bit address space we can use
aed1a261
RS
6573 the same sequence as in 32bit address space. */
6574 if (HAVE_64BIT_SYMBOLS)
d6bc6245 6575 {
aed1a261 6576 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
6577 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6578 {
6579 relax_start (offset_expr.X_add_symbol);
6580 if (breg == 0)
6581 {
6582 macro_build (&offset_expr, s, fmt, treg,
6583 BFD_RELOC_GPREL16, mips_gp_register);
6584 }
6585 else
6586 {
6587 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6588 tempreg, breg, mips_gp_register);
6589 macro_build (&offset_expr, s, fmt, treg,
6590 BFD_RELOC_GPREL16, tempreg);
6591 }
6592 relax_switch ();
6593 }
d6bc6245 6594
741fe287 6595 if (used_at == 0 && mips_opts.at)
d6bc6245 6596 {
67c0d1eb
RS
6597 macro_build (&offset_expr, "lui", "t,u", tempreg,
6598 BFD_RELOC_MIPS_HIGHEST);
6599 macro_build (&offset_expr, "lui", "t,u", AT,
6600 BFD_RELOC_HI16_S);
6601 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6602 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 6603 if (breg != 0)
67c0d1eb
RS
6604 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
6605 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
6606 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
6607 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
6608 tempreg);
d6bc6245
TS
6609 used_at = 1;
6610 }
6611 else
6612 {
67c0d1eb
RS
6613 macro_build (&offset_expr, "lui", "t,u", tempreg,
6614 BFD_RELOC_MIPS_HIGHEST);
6615 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6616 tempreg, BFD_RELOC_MIPS_HIGHER);
6617 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6618 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6619 tempreg, BFD_RELOC_HI16_S);
6620 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
d6bc6245 6621 if (breg != 0)
67c0d1eb 6622 macro_build (NULL, "daddu", "d,v,t",
17a2f251 6623 tempreg, tempreg, breg);
67c0d1eb 6624 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6625 BFD_RELOC_LO16, tempreg);
d6bc6245 6626 }
6caf9ef4
TS
6627
6628 if (mips_relax.sequence)
6629 relax_end ();
8fc2e39e 6630 break;
d6bc6245 6631 }
256ab948 6632
252b5132
RH
6633 if (breg == 0)
6634 {
67c0d1eb 6635 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6636 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6637 {
4d7206a2 6638 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6639 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6640 mips_gp_register);
4d7206a2 6641 relax_switch ();
252b5132 6642 }
67c0d1eb
RS
6643 macro_build_lui (&offset_expr, tempreg);
6644 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6645 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6646 if (mips_relax.sequence)
6647 relax_end ();
252b5132
RH
6648 }
6649 else
6650 {
67c0d1eb 6651 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6652 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6653 {
4d7206a2 6654 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6655 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6656 tempreg, breg, mips_gp_register);
67c0d1eb 6657 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6658 BFD_RELOC_GPREL16, tempreg);
4d7206a2 6659 relax_switch ();
252b5132 6660 }
67c0d1eb
RS
6661 macro_build_lui (&offset_expr, tempreg);
6662 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6663 tempreg, tempreg, breg);
67c0d1eb 6664 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6665 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6666 if (mips_relax.sequence)
6667 relax_end ();
252b5132
RH
6668 }
6669 }
0a44bf69 6670 else if (!mips_big_got)
252b5132 6671 {
ed6fb7bd 6672 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6673
252b5132
RH
6674 /* If this is a reference to an external symbol, we want
6675 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6676 nop
6677 <op> $treg,0($tempreg)
6678 Otherwise we want
6679 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6680 nop
6681 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6682 <op> $treg,0($tempreg)
f5040a92
AO
6683
6684 For NewABI, we want
6685 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6686 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6687
252b5132
RH
6688 If there is a base register, we add it to $tempreg before
6689 the <op>. If there is a constant, we stick it in the
6690 <op> instruction. We don't handle constants larger than
6691 16 bits, because we have no way to load the upper 16 bits
6692 (actually, we could handle them for the subset of cases
6693 in which we are not using $at). */
9c2799c2 6694 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6695 if (HAVE_NEWABI)
6696 {
67c0d1eb
RS
6697 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6698 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6699 if (breg != 0)
67c0d1eb 6700 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6701 tempreg, tempreg, breg);
67c0d1eb 6702 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6703 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
6704 break;
6705 }
252b5132
RH
6706 expr1.X_add_number = offset_expr.X_add_number;
6707 offset_expr.X_add_number = 0;
6708 if (expr1.X_add_number < -0x8000
6709 || expr1.X_add_number >= 0x8000)
6710 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
6711 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6712 lw_reloc_type, mips_gp_register);
269137b2 6713 load_delay_nop ();
4d7206a2
RS
6714 relax_start (offset_expr.X_add_symbol);
6715 relax_switch ();
67c0d1eb
RS
6716 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6717 tempreg, BFD_RELOC_LO16);
4d7206a2 6718 relax_end ();
252b5132 6719 if (breg != 0)
67c0d1eb 6720 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6721 tempreg, tempreg, breg);
67c0d1eb 6722 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6723 }
0a44bf69 6724 else if (mips_big_got && !HAVE_NEWABI)
252b5132 6725 {
67c0d1eb 6726 int gpdelay;
252b5132
RH
6727
6728 /* If this is a reference to an external symbol, we want
6729 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6730 addu $tempreg,$tempreg,$gp
6731 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6732 <op> $treg,0($tempreg)
6733 Otherwise we want
6734 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6735 nop
6736 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6737 <op> $treg,0($tempreg)
6738 If there is a base register, we add it to $tempreg before
6739 the <op>. If there is a constant, we stick it in the
6740 <op> instruction. We don't handle constants larger than
6741 16 bits, because we have no way to load the upper 16 bits
6742 (actually, we could handle them for the subset of cases
f5040a92 6743 in which we are not using $at). */
9c2799c2 6744 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
6745 expr1.X_add_number = offset_expr.X_add_number;
6746 offset_expr.X_add_number = 0;
6747 if (expr1.X_add_number < -0x8000
6748 || expr1.X_add_number >= 0x8000)
6749 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6750 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6751 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6752 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6753 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6754 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6755 mips_gp_register);
6756 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6757 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 6758 relax_switch ();
67c0d1eb
RS
6759 if (gpdelay)
6760 macro_build (NULL, "nop", "");
6761 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6762 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6763 load_delay_nop ();
67c0d1eb
RS
6764 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6765 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
6766 relax_end ();
6767
252b5132 6768 if (breg != 0)
67c0d1eb 6769 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6770 tempreg, tempreg, breg);
67c0d1eb 6771 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6772 }
0a44bf69 6773 else if (mips_big_got && HAVE_NEWABI)
f5040a92 6774 {
f5040a92
AO
6775 /* If this is a reference to an external symbol, we want
6776 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6777 add $tempreg,$tempreg,$gp
6778 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6779 <op> $treg,<ofst>($tempreg)
6780 Otherwise, for local symbols, we want:
6781 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6782 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 6783 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 6784 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
6785 offset_expr.X_add_number = 0;
6786 if (expr1.X_add_number < -0x8000
6787 || expr1.X_add_number >= 0x8000)
6788 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 6789 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6790 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6791 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6792 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6793 mips_gp_register);
6794 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6795 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 6796 if (breg != 0)
67c0d1eb 6797 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6798 tempreg, tempreg, breg);
67c0d1eb 6799 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 6800
4d7206a2 6801 relax_switch ();
f5040a92 6802 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6803 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6804 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6805 if (breg != 0)
67c0d1eb 6806 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6807 tempreg, tempreg, breg);
67c0d1eb 6808 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6809 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 6810 relax_end ();
f5040a92 6811 }
252b5132
RH
6812 else
6813 abort ();
6814
252b5132
RH
6815 break;
6816
6817 case M_LI:
6818 case M_LI_S:
67c0d1eb 6819 load_register (treg, &imm_expr, 0);
8fc2e39e 6820 break;
252b5132
RH
6821
6822 case M_DLI:
67c0d1eb 6823 load_register (treg, &imm_expr, 1);
8fc2e39e 6824 break;
252b5132
RH
6825
6826 case M_LI_SS:
6827 if (imm_expr.X_op == O_constant)
6828 {
8fc2e39e 6829 used_at = 1;
67c0d1eb
RS
6830 load_register (AT, &imm_expr, 0);
6831 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6832 break;
6833 }
6834 else
6835 {
9c2799c2 6836 gas_assert (offset_expr.X_op == O_symbol
252b5132
RH
6837 && strcmp (segment_name (S_GET_SEGMENT
6838 (offset_expr.X_add_symbol)),
6839 ".lit4") == 0
6840 && offset_expr.X_add_number == 0);
67c0d1eb 6841 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 6842 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 6843 break;
252b5132
RH
6844 }
6845
6846 case M_LI_D:
ca4e0257
RS
6847 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6848 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6849 order 32 bits of the value and the low order 32 bits are either
6850 zero or in OFFSET_EXPR. */
252b5132
RH
6851 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6852 {
ca4e0257 6853 if (HAVE_64BIT_GPRS)
67c0d1eb 6854 load_register (treg, &imm_expr, 1);
252b5132
RH
6855 else
6856 {
6857 int hreg, lreg;
6858
6859 if (target_big_endian)
6860 {
6861 hreg = treg;
6862 lreg = treg + 1;
6863 }
6864 else
6865 {
6866 hreg = treg + 1;
6867 lreg = treg;
6868 }
6869
6870 if (hreg <= 31)
67c0d1eb 6871 load_register (hreg, &imm_expr, 0);
252b5132
RH
6872 if (lreg <= 31)
6873 {
6874 if (offset_expr.X_op == O_absent)
67c0d1eb 6875 move_register (lreg, 0);
252b5132
RH
6876 else
6877 {
9c2799c2 6878 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 6879 load_register (lreg, &offset_expr, 0);
252b5132
RH
6880 }
6881 }
6882 }
8fc2e39e 6883 break;
252b5132
RH
6884 }
6885
6886 /* We know that sym is in the .rdata section. First we get the
6887 upper 16 bits of the address. */
6888 if (mips_pic == NO_PIC)
6889 {
67c0d1eb 6890 macro_build_lui (&offset_expr, AT);
8fc2e39e 6891 used_at = 1;
252b5132 6892 }
0a44bf69 6893 else
252b5132 6894 {
67c0d1eb
RS
6895 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6896 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 6897 used_at = 1;
252b5132 6898 }
bdaaa2e1 6899
252b5132 6900 /* Now we load the register(s). */
ca4e0257 6901 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
6902 {
6903 used_at = 1;
6904 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6905 }
252b5132
RH
6906 else
6907 {
8fc2e39e 6908 used_at = 1;
67c0d1eb 6909 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 6910 if (treg != RA)
252b5132
RH
6911 {
6912 /* FIXME: How in the world do we deal with the possible
6913 overflow here? */
6914 offset_expr.X_add_number += 4;
67c0d1eb 6915 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 6916 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
6917 }
6918 }
252b5132
RH
6919 break;
6920
6921 case M_LI_DD:
ca4e0257
RS
6922 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6923 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6924 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6925 the value and the low order 32 bits are either zero or in
6926 OFFSET_EXPR. */
252b5132
RH
6927 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6928 {
8fc2e39e 6929 used_at = 1;
67c0d1eb 6930 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
6931 if (HAVE_64BIT_FPRS)
6932 {
9c2799c2 6933 gas_assert (HAVE_64BIT_GPRS);
67c0d1eb 6934 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 6935 }
252b5132
RH
6936 else
6937 {
67c0d1eb 6938 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 6939 if (offset_expr.X_op == O_absent)
67c0d1eb 6940 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
6941 else
6942 {
9c2799c2 6943 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
6944 load_register (AT, &offset_expr, 0);
6945 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6946 }
6947 }
6948 break;
6949 }
6950
9c2799c2 6951 gas_assert (offset_expr.X_op == O_symbol
252b5132
RH
6952 && offset_expr.X_add_number == 0);
6953 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6954 if (strcmp (s, ".lit8") == 0)
6955 {
e7af610e 6956 if (mips_opts.isa != ISA_MIPS1)
252b5132 6957 {
67c0d1eb 6958 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 6959 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 6960 break;
252b5132 6961 }
c9914766 6962 breg = mips_gp_register;
252b5132
RH
6963 r = BFD_RELOC_MIPS_LITERAL;
6964 goto dob;
6965 }
6966 else
6967 {
9c2799c2 6968 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 6969 used_at = 1;
0a44bf69 6970 if (mips_pic != NO_PIC)
67c0d1eb
RS
6971 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6972 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6973 else
6974 {
6975 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 6976 macro_build_lui (&offset_expr, AT);
252b5132 6977 }
bdaaa2e1 6978
e7af610e 6979 if (mips_opts.isa != ISA_MIPS1)
252b5132 6980 {
67c0d1eb
RS
6981 macro_build (&offset_expr, "ldc1", "T,o(b)",
6982 treg, BFD_RELOC_LO16, AT);
252b5132
RH
6983 break;
6984 }
6985 breg = AT;
6986 r = BFD_RELOC_LO16;
6987 goto dob;
6988 }
6989
6990 case M_L_DOB:
252b5132
RH
6991 /* Even on a big endian machine $fn comes before $fn+1. We have
6992 to adjust when loading from memory. */
6993 r = BFD_RELOC_LO16;
6994 dob:
9c2799c2 6995 gas_assert (mips_opts.isa == ISA_MIPS1);
67c0d1eb 6996 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6997 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
6998 /* FIXME: A possible overflow which I don't know how to deal
6999 with. */
7000 offset_expr.X_add_number += 4;
67c0d1eb 7001 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 7002 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
7003 break;
7004
7005 case M_L_DAB:
7006 /*
7007 * The MIPS assembler seems to check for X_add_number not
7008 * being double aligned and generating:
7009 * lui at,%hi(foo+1)
7010 * addu at,at,v1
7011 * addiu at,at,%lo(foo+1)
7012 * lwc1 f2,0(at)
7013 * lwc1 f3,4(at)
7014 * But, the resulting address is the same after relocation so why
7015 * generate the extra instruction?
7016 */
bdaaa2e1 7017 /* Itbl support may require additional care here. */
252b5132 7018 coproc = 1;
e7af610e 7019 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
7020 {
7021 s = "ldc1";
7022 goto ld;
7023 }
7024
7025 s = "lwc1";
7026 fmt = "T,o(b)";
7027 goto ldd_std;
7028
7029 case M_S_DAB:
e7af610e 7030 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
7031 {
7032 s = "sdc1";
7033 goto st;
7034 }
7035
7036 s = "swc1";
7037 fmt = "T,o(b)";
bdaaa2e1 7038 /* Itbl support may require additional care here. */
252b5132
RH
7039 coproc = 1;
7040 goto ldd_std;
7041
7042 case M_LD_AB:
ca4e0257 7043 if (HAVE_64BIT_GPRS)
252b5132
RH
7044 {
7045 s = "ld";
7046 goto ld;
7047 }
7048
7049 s = "lw";
7050 fmt = "t,o(b)";
7051 goto ldd_std;
7052
7053 case M_SD_AB:
ca4e0257 7054 if (HAVE_64BIT_GPRS)
252b5132
RH
7055 {
7056 s = "sd";
7057 goto st;
7058 }
7059
7060 s = "sw";
7061 fmt = "t,o(b)";
7062
7063 ldd_std:
7064 if (offset_expr.X_op != O_symbol
7065 && offset_expr.X_op != O_constant)
7066 {
7067 as_bad (_("expression too complex"));
7068 offset_expr.X_op = O_constant;
7069 }
7070
2051e8c4
MR
7071 if (HAVE_32BIT_ADDRESSES
7072 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
7073 {
7074 char value [32];
7075
7076 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 7077 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 7078 }
2051e8c4 7079
252b5132
RH
7080 /* Even on a big endian machine $fn comes before $fn+1. We have
7081 to adjust when loading from memory. We set coproc if we must
7082 load $fn+1 first. */
bdaaa2e1 7083 /* Itbl support may require additional care here. */
252b5132
RH
7084 if (! target_big_endian)
7085 coproc = 0;
7086
7087 if (mips_pic == NO_PIC
7088 || offset_expr.X_op == O_constant)
7089 {
7090 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
7091 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
7092 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
7093 If we have a base register, we use this
7094 addu $at,$breg,$gp
cdf6fd85
TS
7095 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
7096 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
7097 If this is not a GP relative symbol, we want
7098 lui $at,<sym> (BFD_RELOC_HI16_S)
7099 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
7100 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
7101 If there is a base register, we add it to $at after the
7102 lui instruction. If there is a constant, we always use
7103 the last case. */
39a59cf8
MR
7104 if (offset_expr.X_op == O_symbol
7105 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 7106 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 7107 {
4d7206a2 7108 relax_start (offset_expr.X_add_symbol);
252b5132
RH
7109 if (breg == 0)
7110 {
c9914766 7111 tempreg = mips_gp_register;
252b5132
RH
7112 }
7113 else
7114 {
67c0d1eb 7115 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7116 AT, breg, mips_gp_register);
252b5132 7117 tempreg = AT;
252b5132
RH
7118 used_at = 1;
7119 }
7120
beae10d5 7121 /* Itbl support may require additional care here. */
67c0d1eb 7122 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 7123 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
7124 offset_expr.X_add_number += 4;
7125
7126 /* Set mips_optimize to 2 to avoid inserting an
7127 undesired nop. */
7128 hold_mips_optimize = mips_optimize;
7129 mips_optimize = 2;
beae10d5 7130 /* Itbl support may require additional care here. */
67c0d1eb 7131 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 7132 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
7133 mips_optimize = hold_mips_optimize;
7134
4d7206a2 7135 relax_switch ();
252b5132
RH
7136
7137 /* We just generated two relocs. When tc_gen_reloc
7138 handles this case, it will skip the first reloc and
7139 handle the second. The second reloc already has an
7140 extra addend of 4, which we added above. We must
7141 subtract it out, and then subtract another 4 to make
7142 the first reloc come out right. The second reloc
7143 will come out right because we are going to add 4 to
7144 offset_expr when we build its instruction below.
7145
7146 If we have a symbol, then we don't want to include
7147 the offset, because it will wind up being included
7148 when we generate the reloc. */
7149
7150 if (offset_expr.X_op == O_constant)
7151 offset_expr.X_add_number -= 8;
7152 else
7153 {
7154 offset_expr.X_add_number = -4;
7155 offset_expr.X_op = O_constant;
7156 }
7157 }
8fc2e39e 7158 used_at = 1;
67c0d1eb 7159 macro_build_lui (&offset_expr, AT);
252b5132 7160 if (breg != 0)
67c0d1eb 7161 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 7162 /* Itbl support may require additional care here. */
67c0d1eb 7163 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 7164 BFD_RELOC_LO16, AT);
252b5132
RH
7165 /* FIXME: How do we handle overflow here? */
7166 offset_expr.X_add_number += 4;
beae10d5 7167 /* Itbl support may require additional care here. */
67c0d1eb 7168 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 7169 BFD_RELOC_LO16, AT);
4d7206a2
RS
7170 if (mips_relax.sequence)
7171 relax_end ();
bdaaa2e1 7172 }
0a44bf69 7173 else if (!mips_big_got)
252b5132 7174 {
252b5132
RH
7175 /* If this is a reference to an external symbol, we want
7176 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7177 nop
7178 <op> $treg,0($at)
7179 <op> $treg+1,4($at)
7180 Otherwise we want
7181 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7182 nop
7183 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
7184 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
7185 If there is a base register we add it to $at before the
7186 lwc1 instructions. If there is a constant we include it
7187 in the lwc1 instructions. */
7188 used_at = 1;
7189 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
7190 if (expr1.X_add_number < -0x8000
7191 || expr1.X_add_number >= 0x8000 - 4)
7192 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 7193 load_got_offset (AT, &offset_expr);
269137b2 7194 load_delay_nop ();
252b5132 7195 if (breg != 0)
67c0d1eb 7196 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
7197
7198 /* Set mips_optimize to 2 to avoid inserting an undesired
7199 nop. */
7200 hold_mips_optimize = mips_optimize;
7201 mips_optimize = 2;
4d7206a2 7202
beae10d5 7203 /* Itbl support may require additional care here. */
4d7206a2 7204 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7205 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
7206 BFD_RELOC_LO16, AT);
4d7206a2 7207 expr1.X_add_number += 4;
67c0d1eb
RS
7208 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
7209 BFD_RELOC_LO16, AT);
4d7206a2 7210 relax_switch ();
67c0d1eb
RS
7211 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
7212 BFD_RELOC_LO16, AT);
4d7206a2 7213 offset_expr.X_add_number += 4;
67c0d1eb
RS
7214 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
7215 BFD_RELOC_LO16, AT);
4d7206a2 7216 relax_end ();
252b5132 7217
4d7206a2 7218 mips_optimize = hold_mips_optimize;
252b5132 7219 }
0a44bf69 7220 else if (mips_big_got)
252b5132 7221 {
67c0d1eb 7222 int gpdelay;
252b5132
RH
7223
7224 /* If this is a reference to an external symbol, we want
7225 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7226 addu $at,$at,$gp
7227 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
7228 nop
7229 <op> $treg,0($at)
7230 <op> $treg+1,4($at)
7231 Otherwise we want
7232 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7233 nop
7234 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
7235 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
7236 If there is a base register we add it to $at before the
7237 lwc1 instructions. If there is a constant we include it
7238 in the lwc1 instructions. */
7239 used_at = 1;
7240 expr1.X_add_number = offset_expr.X_add_number;
7241 offset_expr.X_add_number = 0;
7242 if (expr1.X_add_number < -0x8000
7243 || expr1.X_add_number >= 0x8000 - 4)
7244 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 7245 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 7246 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7247 macro_build (&offset_expr, "lui", "t,u",
7248 AT, BFD_RELOC_MIPS_GOT_HI16);
7249 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7250 AT, AT, mips_gp_register);
67c0d1eb 7251 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 7252 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 7253 load_delay_nop ();
252b5132 7254 if (breg != 0)
67c0d1eb 7255 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 7256 /* Itbl support may require additional care here. */
67c0d1eb 7257 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 7258 BFD_RELOC_LO16, AT);
252b5132
RH
7259 expr1.X_add_number += 4;
7260
7261 /* Set mips_optimize to 2 to avoid inserting an undesired
7262 nop. */
7263 hold_mips_optimize = mips_optimize;
7264 mips_optimize = 2;
beae10d5 7265 /* Itbl support may require additional care here. */
67c0d1eb 7266 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 7267 BFD_RELOC_LO16, AT);
252b5132
RH
7268 mips_optimize = hold_mips_optimize;
7269 expr1.X_add_number -= 4;
7270
4d7206a2
RS
7271 relax_switch ();
7272 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
7273 if (gpdelay)
7274 macro_build (NULL, "nop", "");
7275 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
7276 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7277 load_delay_nop ();
252b5132 7278 if (breg != 0)
67c0d1eb 7279 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 7280 /* Itbl support may require additional care here. */
67c0d1eb
RS
7281 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
7282 BFD_RELOC_LO16, AT);
4d7206a2 7283 offset_expr.X_add_number += 4;
252b5132
RH
7284
7285 /* Set mips_optimize to 2 to avoid inserting an undesired
7286 nop. */
7287 hold_mips_optimize = mips_optimize;
7288 mips_optimize = 2;
beae10d5 7289 /* Itbl support may require additional care here. */
67c0d1eb
RS
7290 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
7291 BFD_RELOC_LO16, AT);
252b5132 7292 mips_optimize = hold_mips_optimize;
4d7206a2 7293 relax_end ();
252b5132 7294 }
252b5132
RH
7295 else
7296 abort ();
7297
252b5132
RH
7298 break;
7299
7300 case M_LD_OB:
7301 s = "lw";
7302 goto sd_ob;
7303 case M_SD_OB:
7304 s = "sw";
7305 sd_ob:
9c2799c2 7306 gas_assert (HAVE_32BIT_ADDRESSES);
67c0d1eb 7307 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132 7308 offset_expr.X_add_number += 4;
67c0d1eb 7309 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
8fc2e39e 7310 break;
252b5132
RH
7311
7312 /* New code added to support COPZ instructions.
7313 This code builds table entries out of the macros in mip_opcodes.
7314 R4000 uses interlocks to handle coproc delays.
7315 Other chips (like the R3000) require nops to be inserted for delays.
7316
f72c8c98 7317 FIXME: Currently, we require that the user handle delays.
252b5132
RH
7318 In order to fill delay slots for non-interlocked chips,
7319 we must have a way to specify delays based on the coprocessor.
7320 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7321 What are the side-effects of the cop instruction?
7322 What cache support might we have and what are its effects?
7323 Both coprocessor & memory require delays. how long???
bdaaa2e1 7324 What registers are read/set/modified?
252b5132
RH
7325
7326 If an itbl is provided to interpret cop instructions,
bdaaa2e1 7327 this knowledge can be encoded in the itbl spec. */
252b5132
RH
7328
7329 case M_COP0:
7330 s = "c0";
7331 goto copz;
7332 case M_COP1:
7333 s = "c1";
7334 goto copz;
7335 case M_COP2:
7336 s = "c2";
7337 goto copz;
7338 case M_COP3:
7339 s = "c3";
7340 copz:
b19e8a9b
AN
7341 if (NO_ISA_COP (mips_opts.arch)
7342 && (ip->insn_mo->pinfo2 & INSN2_M_FP_S) == 0)
7343 {
7344 as_bad (_("opcode not supported on this processor: %s"),
7345 mips_cpu_info_from_arch (mips_opts.arch)->name);
7346 break;
7347 }
7348
252b5132
RH
7349 /* For now we just do C (same as Cz). The parameter will be
7350 stored in insn_opcode by mips_ip. */
67c0d1eb 7351 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 7352 break;
252b5132 7353
ea1fb5dc 7354 case M_MOVE:
67c0d1eb 7355 move_register (dreg, sreg);
8fc2e39e 7356 break;
ea1fb5dc 7357
252b5132
RH
7358#ifdef LOSING_COMPILER
7359 default:
7360 /* Try and see if this is a new itbl instruction.
7361 This code builds table entries out of the macros in mip_opcodes.
7362 FIXME: For now we just assemble the expression and pass it's
7363 value along as a 32-bit immediate.
bdaaa2e1 7364 We may want to have the assembler assemble this value,
252b5132
RH
7365 so that we gain the assembler's knowledge of delay slots,
7366 symbols, etc.
7367 Would it be more efficient to use mask (id) here? */
bdaaa2e1 7368 if (itbl_have_entries
252b5132 7369 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 7370 {
252b5132
RH
7371 s = ip->insn_mo->name;
7372 s2 = "cop3";
7373 coproc = ITBL_DECODE_PNUM (immed_expr);;
67c0d1eb 7374 macro_build (&immed_expr, s, "C");
8fc2e39e 7375 break;
beae10d5 7376 }
252b5132 7377 macro2 (ip);
8fc2e39e 7378 break;
252b5132 7379 }
741fe287 7380 if (!mips_opts.at && used_at)
8fc2e39e 7381 as_bad (_("Macro used $at after \".set noat\""));
252b5132 7382}
bdaaa2e1 7383
252b5132 7384static void
17a2f251 7385macro2 (struct mips_cl_insn *ip)
252b5132 7386{
741fe287
MR
7387 unsigned int treg, sreg, dreg, breg;
7388 unsigned int tempreg;
252b5132 7389 int mask;
252b5132
RH
7390 int used_at;
7391 expressionS expr1;
7392 const char *s;
7393 const char *s2;
7394 const char *fmt;
7395 int likely = 0;
7396 int dbl = 0;
7397 int coproc = 0;
7398 int lr = 0;
7399 int imm = 0;
7400 int off;
7401 offsetT maxnum;
7402 bfd_reloc_code_real_type r;
bdaaa2e1 7403
252b5132
RH
7404 treg = (ip->insn_opcode >> 16) & 0x1f;
7405 dreg = (ip->insn_opcode >> 11) & 0x1f;
7406 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7407 mask = ip->insn_mo->mask;
bdaaa2e1 7408
252b5132
RH
7409 expr1.X_op = O_constant;
7410 expr1.X_op_symbol = NULL;
7411 expr1.X_add_symbol = NULL;
7412 expr1.X_add_number = 1;
bdaaa2e1 7413
252b5132
RH
7414 switch (mask)
7415 {
7416#endif /* LOSING_COMPILER */
7417
7418 case M_DMUL:
7419 dbl = 1;
7420 case M_MUL:
67c0d1eb
RS
7421 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
7422 macro_build (NULL, "mflo", "d", dreg);
8fc2e39e 7423 break;
252b5132
RH
7424
7425 case M_DMUL_I:
7426 dbl = 1;
7427 case M_MUL_I:
7428 /* The MIPS assembler some times generates shifts and adds. I'm
7429 not trying to be that fancy. GCC should do this for us
7430 anyway. */
8fc2e39e 7431 used_at = 1;
67c0d1eb
RS
7432 load_register (AT, &imm_expr, dbl);
7433 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
7434 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
7435 break;
7436
7437 case M_DMULO_I:
7438 dbl = 1;
7439 case M_MULO_I:
7440 imm = 1;
7441 goto do_mulo;
7442
7443 case M_DMULO:
7444 dbl = 1;
7445 case M_MULO:
7446 do_mulo:
7d10b47d 7447 start_noreorder ();
8fc2e39e 7448 used_at = 1;
252b5132 7449 if (imm)
67c0d1eb
RS
7450 load_register (AT, &imm_expr, dbl);
7451 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
7452 macro_build (NULL, "mflo", "d", dreg);
7453 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
7454 macro_build (NULL, "mfhi", "d", AT);
252b5132 7455 if (mips_trap)
67c0d1eb 7456 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
252b5132
RH
7457 else
7458 {
7459 expr1.X_add_number = 8;
67c0d1eb
RS
7460 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
7461 macro_build (NULL, "nop", "", 0);
7462 macro_build (NULL, "break", "c", 6);
252b5132 7463 }
7d10b47d 7464 end_noreorder ();
67c0d1eb 7465 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
7466 break;
7467
7468 case M_DMULOU_I:
7469 dbl = 1;
7470 case M_MULOU_I:
7471 imm = 1;
7472 goto do_mulou;
7473
7474 case M_DMULOU:
7475 dbl = 1;
7476 case M_MULOU:
7477 do_mulou:
7d10b47d 7478 start_noreorder ();
8fc2e39e 7479 used_at = 1;
252b5132 7480 if (imm)
67c0d1eb
RS
7481 load_register (AT, &imm_expr, dbl);
7482 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 7483 sreg, imm ? AT : treg);
67c0d1eb
RS
7484 macro_build (NULL, "mfhi", "d", AT);
7485 macro_build (NULL, "mflo", "d", dreg);
252b5132 7486 if (mips_trap)
67c0d1eb 7487 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
252b5132
RH
7488 else
7489 {
7490 expr1.X_add_number = 8;
67c0d1eb
RS
7491 macro_build (&expr1, "beq", "s,t,p", AT, 0);
7492 macro_build (NULL, "nop", "", 0);
7493 macro_build (NULL, "break", "c", 6);
252b5132 7494 }
7d10b47d 7495 end_noreorder ();
252b5132
RH
7496 break;
7497
771c7ce4 7498 case M_DROL:
fef14a42 7499 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7500 {
7501 if (dreg == sreg)
7502 {
7503 tempreg = AT;
7504 used_at = 1;
7505 }
7506 else
7507 {
7508 tempreg = dreg;
82dd0097 7509 }
67c0d1eb
RS
7510 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
7511 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 7512 break;
82dd0097 7513 }
8fc2e39e 7514 used_at = 1;
67c0d1eb
RS
7515 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7516 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7517 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7518 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7519 break;
7520
252b5132 7521 case M_ROL:
fef14a42 7522 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
7523 {
7524 if (dreg == sreg)
7525 {
7526 tempreg = AT;
7527 used_at = 1;
7528 }
7529 else
7530 {
7531 tempreg = dreg;
82dd0097 7532 }
67c0d1eb
RS
7533 macro_build (NULL, "negu", "d,w", tempreg, treg);
7534 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 7535 break;
82dd0097 7536 }
8fc2e39e 7537 used_at = 1;
67c0d1eb
RS
7538 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7539 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
7540 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
7541 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7542 break;
7543
771c7ce4
TS
7544 case M_DROL_I:
7545 {
7546 unsigned int rot;
91d6fa6a
NC
7547 char *l;
7548 char *rr;
771c7ce4
TS
7549
7550 if (imm_expr.X_op != O_constant)
82dd0097 7551 as_bad (_("Improper rotate count"));
771c7ce4 7552 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7553 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
7554 {
7555 rot = (64 - rot) & 0x3f;
7556 if (rot >= 32)
67c0d1eb 7557 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
60b63b72 7558 else
67c0d1eb 7559 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7560 break;
60b63b72 7561 }
483fc7cd 7562 if (rot == 0)
483fc7cd 7563 {
67c0d1eb 7564 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7565 break;
483fc7cd 7566 }
82dd0097 7567 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 7568 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 7569 rot &= 0x1f;
8fc2e39e 7570 used_at = 1;
67c0d1eb 7571 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
91d6fa6a 7572 macro_build (NULL, rr, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 7573 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7574 }
7575 break;
7576
252b5132 7577 case M_ROL_I:
771c7ce4
TS
7578 {
7579 unsigned int rot;
7580
7581 if (imm_expr.X_op != O_constant)
82dd0097 7582 as_bad (_("Improper rotate count"));
771c7ce4 7583 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7584 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 7585 {
67c0d1eb 7586 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 7587 break;
60b63b72 7588 }
483fc7cd 7589 if (rot == 0)
483fc7cd 7590 {
67c0d1eb 7591 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7592 break;
483fc7cd 7593 }
8fc2e39e 7594 used_at = 1;
67c0d1eb
RS
7595 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
7596 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7597 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7598 }
7599 break;
7600
7601 case M_DROR:
fef14a42 7602 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 7603 {
67c0d1eb 7604 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 7605 break;
82dd0097 7606 }
8fc2e39e 7607 used_at = 1;
67c0d1eb
RS
7608 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7609 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
7610 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7611 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7612 break;
7613
7614 case M_ROR:
fef14a42 7615 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7616 {
67c0d1eb 7617 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 7618 break;
82dd0097 7619 }
8fc2e39e 7620 used_at = 1;
67c0d1eb
RS
7621 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7622 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7623 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7624 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7625 break;
7626
771c7ce4
TS
7627 case M_DROR_I:
7628 {
7629 unsigned int rot;
91d6fa6a
NC
7630 char *l;
7631 char *rr;
771c7ce4
TS
7632
7633 if (imm_expr.X_op != O_constant)
82dd0097 7634 as_bad (_("Improper rotate count"));
771c7ce4 7635 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7636 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7637 {
7638 if (rot >= 32)
67c0d1eb 7639 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
82dd0097 7640 else
67c0d1eb 7641 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7642 break;
82dd0097 7643 }
483fc7cd 7644 if (rot == 0)
483fc7cd 7645 {
67c0d1eb 7646 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7647 break;
483fc7cd 7648 }
91d6fa6a 7649 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
7650 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7651 rot &= 0x1f;
8fc2e39e 7652 used_at = 1;
91d6fa6a 7653 macro_build (NULL, rr, "d,w,<", AT, sreg, rot);
67c0d1eb
RS
7654 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7655 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7656 }
7657 break;
7658
252b5132 7659 case M_ROR_I:
771c7ce4
TS
7660 {
7661 unsigned int rot;
7662
7663 if (imm_expr.X_op != O_constant)
82dd0097 7664 as_bad (_("Improper rotate count"));
771c7ce4 7665 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7666 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7667 {
67c0d1eb 7668 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7669 break;
82dd0097 7670 }
483fc7cd 7671 if (rot == 0)
483fc7cd 7672 {
67c0d1eb 7673 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7674 break;
483fc7cd 7675 }
8fc2e39e 7676 used_at = 1;
67c0d1eb
RS
7677 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7678 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7679 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 7680 }
252b5132
RH
7681 break;
7682
7683 case M_S_DOB:
9c2799c2 7684 gas_assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7685 /* Even on a big endian machine $fn comes before $fn+1. We have
7686 to adjust when storing to memory. */
67c0d1eb
RS
7687 macro_build (&offset_expr, "swc1", "T,o(b)",
7688 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
252b5132 7689 offset_expr.X_add_number += 4;
67c0d1eb
RS
7690 macro_build (&offset_expr, "swc1", "T,o(b)",
7691 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8fc2e39e 7692 break;
252b5132
RH
7693
7694 case M_SEQ:
7695 if (sreg == 0)
67c0d1eb 7696 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 7697 else if (treg == 0)
67c0d1eb 7698 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7699 else
7700 {
67c0d1eb
RS
7701 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7702 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 7703 }
8fc2e39e 7704 break;
252b5132
RH
7705
7706 case M_SEQ_I:
7707 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7708 {
67c0d1eb 7709 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7710 break;
252b5132
RH
7711 }
7712 if (sreg == 0)
7713 {
7714 as_warn (_("Instruction %s: result is always false"),
7715 ip->insn_mo->name);
67c0d1eb 7716 move_register (dreg, 0);
8fc2e39e 7717 break;
252b5132 7718 }
dd3cbb7e
NC
7719 if (CPU_HAS_SEQ (mips_opts.arch)
7720 && -512 <= imm_expr.X_add_number
7721 && imm_expr.X_add_number < 512)
7722 {
7723 macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
750bdd57 7724 (int) imm_expr.X_add_number);
dd3cbb7e
NC
7725 break;
7726 }
252b5132
RH
7727 if (imm_expr.X_op == O_constant
7728 && imm_expr.X_add_number >= 0
7729 && imm_expr.X_add_number < 0x10000)
7730 {
67c0d1eb 7731 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7732 }
7733 else if (imm_expr.X_op == O_constant
7734 && imm_expr.X_add_number > -0x8000
7735 && imm_expr.X_add_number < 0)
7736 {
7737 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7738 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7739 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 7740 }
dd3cbb7e
NC
7741 else if (CPU_HAS_SEQ (mips_opts.arch))
7742 {
7743 used_at = 1;
7744 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7745 macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
7746 break;
7747 }
252b5132
RH
7748 else
7749 {
67c0d1eb
RS
7750 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7751 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7752 used_at = 1;
7753 }
67c0d1eb 7754 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7755 break;
252b5132
RH
7756
7757 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7758 s = "slt";
7759 goto sge;
7760 case M_SGEU:
7761 s = "sltu";
7762 sge:
67c0d1eb
RS
7763 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7764 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7765 break;
252b5132
RH
7766
7767 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7768 case M_SGEU_I:
7769 if (imm_expr.X_op == O_constant
7770 && imm_expr.X_add_number >= -0x8000
7771 && imm_expr.X_add_number < 0x8000)
7772 {
67c0d1eb
RS
7773 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7774 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7775 }
7776 else
7777 {
67c0d1eb
RS
7778 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7779 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7780 dreg, sreg, AT);
252b5132
RH
7781 used_at = 1;
7782 }
67c0d1eb 7783 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7784 break;
252b5132
RH
7785
7786 case M_SGT: /* sreg > treg <==> treg < sreg */
7787 s = "slt";
7788 goto sgt;
7789 case M_SGTU:
7790 s = "sltu";
7791 sgt:
67c0d1eb 7792 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 7793 break;
252b5132
RH
7794
7795 case M_SGT_I: /* sreg > I <==> I < sreg */
7796 s = "slt";
7797 goto sgti;
7798 case M_SGTU_I:
7799 s = "sltu";
7800 sgti:
8fc2e39e 7801 used_at = 1;
67c0d1eb
RS
7802 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7803 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
7804 break;
7805
2396cfb9 7806 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7807 s = "slt";
7808 goto sle;
7809 case M_SLEU:
7810 s = "sltu";
7811 sle:
67c0d1eb
RS
7812 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7813 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7814 break;
252b5132 7815
2396cfb9 7816 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7817 s = "slt";
7818 goto slei;
7819 case M_SLEU_I:
7820 s = "sltu";
7821 slei:
8fc2e39e 7822 used_at = 1;
67c0d1eb
RS
7823 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7824 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7825 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7826 break;
7827
7828 case M_SLT_I:
7829 if (imm_expr.X_op == O_constant
7830 && imm_expr.X_add_number >= -0x8000
7831 && imm_expr.X_add_number < 0x8000)
7832 {
67c0d1eb 7833 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7834 break;
252b5132 7835 }
8fc2e39e 7836 used_at = 1;
67c0d1eb
RS
7837 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7838 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
7839 break;
7840
7841 case M_SLTU_I:
7842 if (imm_expr.X_op == O_constant
7843 && imm_expr.X_add_number >= -0x8000
7844 && imm_expr.X_add_number < 0x8000)
7845 {
67c0d1eb 7846 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 7847 BFD_RELOC_LO16);
8fc2e39e 7848 break;
252b5132 7849 }
8fc2e39e 7850 used_at = 1;
67c0d1eb
RS
7851 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7852 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7853 break;
7854
7855 case M_SNE:
7856 if (sreg == 0)
67c0d1eb 7857 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 7858 else if (treg == 0)
67c0d1eb 7859 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7860 else
7861 {
67c0d1eb
RS
7862 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7863 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 7864 }
8fc2e39e 7865 break;
252b5132
RH
7866
7867 case M_SNE_I:
7868 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7869 {
67c0d1eb 7870 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 7871 break;
252b5132
RH
7872 }
7873 if (sreg == 0)
7874 {
7875 as_warn (_("Instruction %s: result is always true"),
7876 ip->insn_mo->name);
67c0d1eb
RS
7877 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7878 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 7879 break;
252b5132 7880 }
dd3cbb7e
NC
7881 if (CPU_HAS_SEQ (mips_opts.arch)
7882 && -512 <= imm_expr.X_add_number
7883 && imm_expr.X_add_number < 512)
7884 {
7885 macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
750bdd57 7886 (int) imm_expr.X_add_number);
dd3cbb7e
NC
7887 break;
7888 }
252b5132
RH
7889 if (imm_expr.X_op == O_constant
7890 && imm_expr.X_add_number >= 0
7891 && imm_expr.X_add_number < 0x10000)
7892 {
67c0d1eb 7893 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7894 }
7895 else if (imm_expr.X_op == O_constant
7896 && imm_expr.X_add_number > -0x8000
7897 && imm_expr.X_add_number < 0)
7898 {
7899 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7900 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7901 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 7902 }
dd3cbb7e
NC
7903 else if (CPU_HAS_SEQ (mips_opts.arch))
7904 {
7905 used_at = 1;
7906 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7907 macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
7908 break;
7909 }
252b5132
RH
7910 else
7911 {
67c0d1eb
RS
7912 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7913 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7914 used_at = 1;
7915 }
67c0d1eb 7916 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 7917 break;
252b5132
RH
7918
7919 case M_DSUB_I:
7920 dbl = 1;
7921 case M_SUB_I:
7922 if (imm_expr.X_op == O_constant
7923 && imm_expr.X_add_number > -0x8000
7924 && imm_expr.X_add_number <= 0x8000)
7925 {
7926 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7927 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7928 dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7929 break;
252b5132 7930 }
8fc2e39e 7931 used_at = 1;
67c0d1eb
RS
7932 load_register (AT, &imm_expr, dbl);
7933 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7934 break;
7935
7936 case M_DSUBU_I:
7937 dbl = 1;
7938 case M_SUBU_I:
7939 if (imm_expr.X_op == O_constant
7940 && imm_expr.X_add_number > -0x8000
7941 && imm_expr.X_add_number <= 0x8000)
7942 {
7943 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7944 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7945 dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7946 break;
252b5132 7947 }
8fc2e39e 7948 used_at = 1;
67c0d1eb
RS
7949 load_register (AT, &imm_expr, dbl);
7950 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7951 break;
7952
7953 case M_TEQ_I:
7954 s = "teq";
7955 goto trap;
7956 case M_TGE_I:
7957 s = "tge";
7958 goto trap;
7959 case M_TGEU_I:
7960 s = "tgeu";
7961 goto trap;
7962 case M_TLT_I:
7963 s = "tlt";
7964 goto trap;
7965 case M_TLTU_I:
7966 s = "tltu";
7967 goto trap;
7968 case M_TNE_I:
7969 s = "tne";
7970 trap:
8fc2e39e 7971 used_at = 1;
67c0d1eb
RS
7972 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7973 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
7974 break;
7975
252b5132 7976 case M_TRUNCWS:
43841e91 7977 case M_TRUNCWD:
9c2799c2 7978 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 7979 used_at = 1;
252b5132
RH
7980 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7981 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7982
7983 /*
7984 * Is the double cfc1 instruction a bug in the mips assembler;
7985 * or is there a reason for it?
7986 */
7d10b47d 7987 start_noreorder ();
67c0d1eb
RS
7988 macro_build (NULL, "cfc1", "t,G", treg, RA);
7989 macro_build (NULL, "cfc1", "t,G", treg, RA);
7990 macro_build (NULL, "nop", "");
252b5132 7991 expr1.X_add_number = 3;
67c0d1eb 7992 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 7993 expr1.X_add_number = 2;
67c0d1eb
RS
7994 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7995 macro_build (NULL, "ctc1", "t,G", AT, RA);
7996 macro_build (NULL, "nop", "");
7997 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7998 dreg, sreg);
7999 macro_build (NULL, "ctc1", "t,G", treg, RA);
8000 macro_build (NULL, "nop", "");
7d10b47d 8001 end_noreorder ();
252b5132
RH
8002 break;
8003
8004 case M_ULH:
8005 s = "lb";
8006 goto ulh;
8007 case M_ULHU:
8008 s = "lbu";
8009 ulh:
8fc2e39e 8010 used_at = 1;
252b5132
RH
8011 if (offset_expr.X_add_number >= 0x7fff)
8012 as_bad (_("operand overflow"));
252b5132 8013 if (! target_big_endian)
f9419b05 8014 ++offset_expr.X_add_number;
67c0d1eb 8015 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132 8016 if (! target_big_endian)
f9419b05 8017 --offset_expr.X_add_number;
252b5132 8018 else
f9419b05 8019 ++offset_expr.X_add_number;
67c0d1eb
RS
8020 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
8021 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
8022 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
8023 break;
8024
8025 case M_ULD:
8026 s = "ldl";
8027 s2 = "ldr";
8028 off = 7;
8029 goto ulw;
8030 case M_ULW:
8031 s = "lwl";
8032 s2 = "lwr";
8033 off = 3;
8034 ulw:
8035 if (offset_expr.X_add_number >= 0x8000 - off)
8036 as_bad (_("operand overflow"));
af22f5b2
CD
8037 if (treg != breg)
8038 tempreg = treg;
8039 else
8fc2e39e
TS
8040 {
8041 used_at = 1;
8042 tempreg = AT;
8043 }
252b5132
RH
8044 if (! target_big_endian)
8045 offset_expr.X_add_number += off;
67c0d1eb 8046 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132
RH
8047 if (! target_big_endian)
8048 offset_expr.X_add_number -= off;
8049 else
8050 offset_expr.X_add_number += off;
67c0d1eb 8051 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
af22f5b2
CD
8052
8053 /* If necessary, move the result in tempreg the final destination. */
8054 if (treg == tempreg)
8fc2e39e 8055 break;
af22f5b2 8056 /* Protect second load's delay slot. */
017315e4 8057 load_delay_nop ();
67c0d1eb 8058 move_register (treg, tempreg);
af22f5b2 8059 break;
252b5132
RH
8060
8061 case M_ULD_A:
8062 s = "ldl";
8063 s2 = "ldr";
8064 off = 7;
8065 goto ulwa;
8066 case M_ULW_A:
8067 s = "lwl";
8068 s2 = "lwr";
8069 off = 3;
8070 ulwa:
d6bc6245 8071 used_at = 1;
67c0d1eb 8072 load_address (AT, &offset_expr, &used_at);
252b5132 8073 if (breg != 0)
67c0d1eb 8074 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
8075 if (! target_big_endian)
8076 expr1.X_add_number = off;
8077 else
8078 expr1.X_add_number = 0;
67c0d1eb 8079 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
8080 if (! target_big_endian)
8081 expr1.X_add_number = 0;
8082 else
8083 expr1.X_add_number = off;
67c0d1eb 8084 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
8085 break;
8086
8087 case M_ULH_A:
8088 case M_ULHU_A:
d6bc6245 8089 used_at = 1;
67c0d1eb 8090 load_address (AT, &offset_expr, &used_at);
252b5132 8091 if (breg != 0)
67c0d1eb 8092 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
8093 if (target_big_endian)
8094 expr1.X_add_number = 0;
67c0d1eb 8095 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
17a2f251 8096 treg, BFD_RELOC_LO16, AT);
252b5132
RH
8097 if (target_big_endian)
8098 expr1.X_add_number = 1;
8099 else
8100 expr1.X_add_number = 0;
67c0d1eb
RS
8101 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
8102 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
8103 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
8104 break;
8105
8106 case M_USH:
8fc2e39e 8107 used_at = 1;
252b5132
RH
8108 if (offset_expr.X_add_number >= 0x7fff)
8109 as_bad (_("operand overflow"));
8110 if (target_big_endian)
f9419b05 8111 ++offset_expr.X_add_number;
67c0d1eb
RS
8112 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
8113 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
252b5132 8114 if (target_big_endian)
f9419b05 8115 --offset_expr.X_add_number;
252b5132 8116 else
f9419b05 8117 ++offset_expr.X_add_number;
67c0d1eb 8118 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132
RH
8119 break;
8120
8121 case M_USD:
8122 s = "sdl";
8123 s2 = "sdr";
8124 off = 7;
8125 goto usw;
8126 case M_USW:
8127 s = "swl";
8128 s2 = "swr";
8129 off = 3;
8130 usw:
8131 if (offset_expr.X_add_number >= 0x8000 - off)
8132 as_bad (_("operand overflow"));
8133 if (! target_big_endian)
8134 offset_expr.X_add_number += off;
67c0d1eb 8135 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
8136 if (! target_big_endian)
8137 offset_expr.X_add_number -= off;
8138 else
8139 offset_expr.X_add_number += off;
67c0d1eb 8140 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
8fc2e39e 8141 break;
252b5132
RH
8142
8143 case M_USD_A:
8144 s = "sdl";
8145 s2 = "sdr";
8146 off = 7;
8147 goto uswa;
8148 case M_USW_A:
8149 s = "swl";
8150 s2 = "swr";
8151 off = 3;
8152 uswa:
d6bc6245 8153 used_at = 1;
67c0d1eb 8154 load_address (AT, &offset_expr, &used_at);
252b5132 8155 if (breg != 0)
67c0d1eb 8156 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
8157 if (! target_big_endian)
8158 expr1.X_add_number = off;
8159 else
8160 expr1.X_add_number = 0;
67c0d1eb 8161 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
8162 if (! target_big_endian)
8163 expr1.X_add_number = 0;
8164 else
8165 expr1.X_add_number = off;
67c0d1eb 8166 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
8167 break;
8168
8169 case M_USH_A:
d6bc6245 8170 used_at = 1;
67c0d1eb 8171 load_address (AT, &offset_expr, &used_at);
252b5132 8172 if (breg != 0)
67c0d1eb 8173 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
8174 if (! target_big_endian)
8175 expr1.X_add_number = 0;
67c0d1eb
RS
8176 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8177 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
252b5132
RH
8178 if (! target_big_endian)
8179 expr1.X_add_number = 1;
8180 else
8181 expr1.X_add_number = 0;
67c0d1eb 8182 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
8183 if (! target_big_endian)
8184 expr1.X_add_number = 0;
8185 else
8186 expr1.X_add_number = 1;
67c0d1eb
RS
8187 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
8188 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
8189 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
8190 break;
8191
8192 default:
8193 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 8194 are added dynamically. */
252b5132
RH
8195 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
8196 break;
8197 }
741fe287 8198 if (!mips_opts.at && used_at)
8fc2e39e 8199 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
8200}
8201
8202/* Implement macros in mips16 mode. */
8203
8204static void
17a2f251 8205mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
8206{
8207 int mask;
8208 int xreg, yreg, zreg, tmp;
252b5132
RH
8209 expressionS expr1;
8210 int dbl;
8211 const char *s, *s2, *s3;
8212
8213 mask = ip->insn_mo->mask;
8214
bf12938e
RS
8215 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
8216 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
8217 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 8218
252b5132
RH
8219 expr1.X_op = O_constant;
8220 expr1.X_op_symbol = NULL;
8221 expr1.X_add_symbol = NULL;
8222 expr1.X_add_number = 1;
8223
8224 dbl = 0;
8225
8226 switch (mask)
8227 {
8228 default:
8229 internalError ();
8230
8231 case M_DDIV_3:
8232 dbl = 1;
8233 case M_DIV_3:
8234 s = "mflo";
8235 goto do_div3;
8236 case M_DREM_3:
8237 dbl = 1;
8238 case M_REM_3:
8239 s = "mfhi";
8240 do_div3:
7d10b47d 8241 start_noreorder ();
67c0d1eb 8242 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 8243 expr1.X_add_number = 2;
67c0d1eb
RS
8244 macro_build (&expr1, "bnez", "x,p", yreg);
8245 macro_build (NULL, "break", "6", 7);
bdaaa2e1 8246
252b5132
RH
8247 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
8248 since that causes an overflow. We should do that as well,
8249 but I don't see how to do the comparisons without a temporary
8250 register. */
7d10b47d 8251 end_noreorder ();
67c0d1eb 8252 macro_build (NULL, s, "x", zreg);
252b5132
RH
8253 break;
8254
8255 case M_DIVU_3:
8256 s = "divu";
8257 s2 = "mflo";
8258 goto do_divu3;
8259 case M_REMU_3:
8260 s = "divu";
8261 s2 = "mfhi";
8262 goto do_divu3;
8263 case M_DDIVU_3:
8264 s = "ddivu";
8265 s2 = "mflo";
8266 goto do_divu3;
8267 case M_DREMU_3:
8268 s = "ddivu";
8269 s2 = "mfhi";
8270 do_divu3:
7d10b47d 8271 start_noreorder ();
67c0d1eb 8272 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 8273 expr1.X_add_number = 2;
67c0d1eb
RS
8274 macro_build (&expr1, "bnez", "x,p", yreg);
8275 macro_build (NULL, "break", "6", 7);
7d10b47d 8276 end_noreorder ();
67c0d1eb 8277 macro_build (NULL, s2, "x", zreg);
252b5132
RH
8278 break;
8279
8280 case M_DMUL:
8281 dbl = 1;
8282 case M_MUL:
67c0d1eb
RS
8283 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
8284 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 8285 break;
252b5132
RH
8286
8287 case M_DSUBU_I:
8288 dbl = 1;
8289 goto do_subu;
8290 case M_SUBU_I:
8291 do_subu:
8292 if (imm_expr.X_op != O_constant)
8293 as_bad (_("Unsupported large constant"));
8294 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 8295 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
8296 break;
8297
8298 case M_SUBU_I_2:
8299 if (imm_expr.X_op != O_constant)
8300 as_bad (_("Unsupported large constant"));
8301 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 8302 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
8303 break;
8304
8305 case M_DSUBU_I_2:
8306 if (imm_expr.X_op != O_constant)
8307 as_bad (_("Unsupported large constant"));
8308 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 8309 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
8310 break;
8311
8312 case M_BEQ:
8313 s = "cmp";
8314 s2 = "bteqz";
8315 goto do_branch;
8316 case M_BNE:
8317 s = "cmp";
8318 s2 = "btnez";
8319 goto do_branch;
8320 case M_BLT:
8321 s = "slt";
8322 s2 = "btnez";
8323 goto do_branch;
8324 case M_BLTU:
8325 s = "sltu";
8326 s2 = "btnez";
8327 goto do_branch;
8328 case M_BLE:
8329 s = "slt";
8330 s2 = "bteqz";
8331 goto do_reverse_branch;
8332 case M_BLEU:
8333 s = "sltu";
8334 s2 = "bteqz";
8335 goto do_reverse_branch;
8336 case M_BGE:
8337 s = "slt";
8338 s2 = "bteqz";
8339 goto do_branch;
8340 case M_BGEU:
8341 s = "sltu";
8342 s2 = "bteqz";
8343 goto do_branch;
8344 case M_BGT:
8345 s = "slt";
8346 s2 = "btnez";
8347 goto do_reverse_branch;
8348 case M_BGTU:
8349 s = "sltu";
8350 s2 = "btnez";
8351
8352 do_reverse_branch:
8353 tmp = xreg;
8354 xreg = yreg;
8355 yreg = tmp;
8356
8357 do_branch:
67c0d1eb
RS
8358 macro_build (NULL, s, "x,y", xreg, yreg);
8359 macro_build (&offset_expr, s2, "p");
252b5132
RH
8360 break;
8361
8362 case M_BEQ_I:
8363 s = "cmpi";
8364 s2 = "bteqz";
8365 s3 = "x,U";
8366 goto do_branch_i;
8367 case M_BNE_I:
8368 s = "cmpi";
8369 s2 = "btnez";
8370 s3 = "x,U";
8371 goto do_branch_i;
8372 case M_BLT_I:
8373 s = "slti";
8374 s2 = "btnez";
8375 s3 = "x,8";
8376 goto do_branch_i;
8377 case M_BLTU_I:
8378 s = "sltiu";
8379 s2 = "btnez";
8380 s3 = "x,8";
8381 goto do_branch_i;
8382 case M_BLE_I:
8383 s = "slti";
8384 s2 = "btnez";
8385 s3 = "x,8";
8386 goto do_addone_branch_i;
8387 case M_BLEU_I:
8388 s = "sltiu";
8389 s2 = "btnez";
8390 s3 = "x,8";
8391 goto do_addone_branch_i;
8392 case M_BGE_I:
8393 s = "slti";
8394 s2 = "bteqz";
8395 s3 = "x,8";
8396 goto do_branch_i;
8397 case M_BGEU_I:
8398 s = "sltiu";
8399 s2 = "bteqz";
8400 s3 = "x,8";
8401 goto do_branch_i;
8402 case M_BGT_I:
8403 s = "slti";
8404 s2 = "bteqz";
8405 s3 = "x,8";
8406 goto do_addone_branch_i;
8407 case M_BGTU_I:
8408 s = "sltiu";
8409 s2 = "bteqz";
8410 s3 = "x,8";
8411
8412 do_addone_branch_i:
8413 if (imm_expr.X_op != O_constant)
8414 as_bad (_("Unsupported large constant"));
8415 ++imm_expr.X_add_number;
8416
8417 do_branch_i:
67c0d1eb
RS
8418 macro_build (&imm_expr, s, s3, xreg);
8419 macro_build (&offset_expr, s2, "p");
252b5132
RH
8420 break;
8421
8422 case M_ABS:
8423 expr1.X_add_number = 0;
67c0d1eb 8424 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 8425 if (xreg != yreg)
67c0d1eb 8426 move_register (xreg, yreg);
252b5132 8427 expr1.X_add_number = 2;
67c0d1eb
RS
8428 macro_build (&expr1, "bteqz", "p");
8429 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
8430 }
8431}
8432
8433/* For consistency checking, verify that all bits are specified either
8434 by the match/mask part of the instruction definition, or by the
8435 operand list. */
8436static int
17a2f251 8437validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
8438{
8439 const char *p = opc->args;
8440 char c;
8441 unsigned long used_bits = opc->mask;
8442
8443 if ((used_bits & opc->match) != opc->match)
8444 {
8445 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8446 opc->name, opc->args);
8447 return 0;
8448 }
8449#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8450 while (*p)
8451 switch (c = *p++)
8452 {
8453 case ',': break;
8454 case '(': break;
8455 case ')': break;
af7ee8bf
CD
8456 case '+':
8457 switch (c = *p++)
8458 {
9bcd4f99
TS
8459 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
8460 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
8461 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
8462 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
af7ee8bf
CD
8463 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8464 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8465 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
8466 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8467 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
8468 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8469 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8470 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8471 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8472 case 'I': break;
ef2e4d86
CF
8473 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8474 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
8475 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
bb35fb24
NC
8476 case 'x': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
8477 case 'X': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
8478 case 'p': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
8479 case 'P': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
dd3cbb7e 8480 case 'Q': USE_BITS (OP_MASK_SEQI, OP_SH_SEQI); break;
bb35fb24
NC
8481 case 's': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
8482 case 'S': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
8483
af7ee8bf
CD
8484 default:
8485 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8486 c, opc->name, opc->args);
8487 return 0;
8488 }
8489 break;
252b5132
RH
8490 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8491 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8492 case 'A': break;
4372b673 8493 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
8494 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8495 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8496 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8497 case 'F': break;
8498 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 8499 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 8500 case 'I': break;
e972090a 8501 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 8502 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
8503 case 'L': break;
8504 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8505 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
8506 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8507 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8508 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8509 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8510 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8511 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8512 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8513 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
8514 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8515 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8516 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8517 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8518 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8519 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8520 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8521 case 'f': break;
8522 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8523 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8524 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8525 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8526 case 'l': break;
8527 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8528 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8529 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8530 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8531 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8532 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8533 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8534 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8535 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8536 case 'x': break;
8537 case 'z': break;
8538 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
8539 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8540 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
8541 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8542 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8543 case '[': break;
8544 case ']': break;
620edafd 8545 case '1': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8b082fb1 8546 case '2': USE_BITS (OP_MASK_BP, OP_SH_BP); break;
74cd071d
CF
8547 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
8548 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
8549 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
8550 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8551 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
8552 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
8553 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
8554 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
8555 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
8556 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
8557 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
8558 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
8559 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
8560 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
8561 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
8562 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
8563 default:
8564 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8565 c, opc->name, opc->args);
8566 return 0;
8567 }
8568#undef USE_BITS
8569 if (used_bits != 0xffffffff)
8570 {
8571 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8572 ~used_bits & 0xffffffff, opc->name, opc->args);
8573 return 0;
8574 }
8575 return 1;
8576}
8577
9bcd4f99
TS
8578/* UDI immediates. */
8579struct mips_immed {
8580 char type;
8581 unsigned int shift;
8582 unsigned long mask;
8583 const char * desc;
8584};
8585
8586static const struct mips_immed mips_immed[] = {
8587 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
8588 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
8589 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
8590 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
8591 { 0,0,0,0 }
8592};
8593
7455baf8
TS
8594/* Check whether an odd floating-point register is allowed. */
8595static int
8596mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
8597{
8598 const char *s = insn->name;
8599
8600 if (insn->pinfo == INSN_MACRO)
8601 /* Let a macro pass, we'll catch it later when it is expanded. */
8602 return 1;
8603
8604 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa))
8605 {
8606 /* Allow odd registers for single-precision ops. */
8607 switch (insn->pinfo & (FP_S | FP_D))
8608 {
8609 case FP_S:
8610 case 0:
8611 return 1; /* both single precision - ok */
8612 case FP_D:
8613 return 0; /* both double precision - fail */
8614 default:
8615 break;
8616 }
8617
8618 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
8619 s = strchr (insn->name, '.');
8620 if (argnum == 2)
8621 s = s != NULL ? strchr (s + 1, '.') : NULL;
8622 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
8623 }
8624
8625 /* Single-precision coprocessor loads and moves are OK too. */
8626 if ((insn->pinfo & FP_S)
8627 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
8628 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
8629 return 1;
8630
8631 return 0;
8632}
8633
252b5132
RH
8634/* This routine assembles an instruction into its binary format. As a
8635 side effect, it sets one of the global variables imm_reloc or
8636 offset_reloc to the type of relocation to do if one of the operands
8637 is an address expression. */
8638
8639static void
17a2f251 8640mips_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
8641{
8642 char *s;
8643 const char *args;
43841e91 8644 char c = 0;
252b5132
RH
8645 struct mips_opcode *insn;
8646 char *argsStart;
8647 unsigned int regno;
8648 unsigned int lastregno = 0;
af7ee8bf 8649 unsigned int lastpos = 0;
071742cf 8650 unsigned int limlo, limhi;
252b5132
RH
8651 char *s_reset;
8652 char save_c = 0;
74cd071d 8653 offsetT min_range, max_range;
707bfff6
TS
8654 int argnum;
8655 unsigned int rtype;
252b5132
RH
8656
8657 insn_error = NULL;
8658
8659 /* If the instruction contains a '.', we first try to match an instruction
8660 including the '.'. Then we try again without the '.'. */
8661 insn = NULL;
3882b010 8662 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
8663 continue;
8664
8665 /* If we stopped on whitespace, then replace the whitespace with null for
8666 the call to hash_find. Save the character we replaced just in case we
8667 have to re-parse the instruction. */
3882b010 8668 if (ISSPACE (*s))
252b5132
RH
8669 {
8670 save_c = *s;
8671 *s++ = '\0';
8672 }
bdaaa2e1 8673
252b5132
RH
8674 insn = (struct mips_opcode *) hash_find (op_hash, str);
8675
8676 /* If we didn't find the instruction in the opcode table, try again, but
8677 this time with just the instruction up to, but not including the
8678 first '.'. */
8679 if (insn == NULL)
8680 {
bdaaa2e1 8681 /* Restore the character we overwrite above (if any). */
252b5132
RH
8682 if (save_c)
8683 *(--s) = save_c;
8684
8685 /* Scan up to the first '.' or whitespace. */
3882b010
L
8686 for (s = str;
8687 *s != '\0' && *s != '.' && !ISSPACE (*s);
8688 ++s)
252b5132
RH
8689 continue;
8690
8691 /* If we did not find a '.', then we can quit now. */
8692 if (*s != '.')
8693 {
20203fb9 8694 insn_error = _("unrecognized opcode");
252b5132
RH
8695 return;
8696 }
8697
8698 /* Lookup the instruction in the hash table. */
8699 *s++ = '\0';
8700 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8701 {
20203fb9 8702 insn_error = _("unrecognized opcode");
252b5132
RH
8703 return;
8704 }
252b5132
RH
8705 }
8706
8707 argsStart = s;
8708 for (;;)
8709 {
b34976b6 8710 bfd_boolean ok;
252b5132 8711
9c2799c2 8712 gas_assert (strcmp (insn->name, str) == 0);
252b5132 8713
037b32b9 8714 ok = is_opcode_valid (insn, FALSE);
252b5132
RH
8715 if (! ok)
8716 {
8717 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8718 && strcmp (insn->name, insn[1].name) == 0)
8719 {
8720 ++insn;
8721 continue;
8722 }
252b5132 8723 else
beae10d5 8724 {
268f6bed
L
8725 if (!insn_error)
8726 {
8727 static char buf[100];
fef14a42
TS
8728 sprintf (buf,
8729 _("opcode not supported on this processor: %s (%s)"),
8730 mips_cpu_info_from_arch (mips_opts.arch)->name,
8731 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8732 insn_error = buf;
8733 }
8734 if (save_c)
8735 *(--s) = save_c;
2bd7f1f3 8736 return;
252b5132 8737 }
252b5132
RH
8738 }
8739
1e915849 8740 create_insn (ip, insn);
268f6bed 8741 insn_error = NULL;
707bfff6 8742 argnum = 1;
24864476 8743 lastregno = 0xffffffff;
252b5132
RH
8744 for (args = insn->args;; ++args)
8745 {
deec1734
CD
8746 int is_mdmx;
8747
ad8d3bb3 8748 s += strspn (s, " \t");
deec1734 8749 is_mdmx = 0;
252b5132
RH
8750 switch (*args)
8751 {
8752 case '\0': /* end of args */
8753 if (*s == '\0')
8754 return;
8755 break;
8756
8b082fb1
TS
8757 case '2': /* dsp 2-bit unsigned immediate in bit 11 */
8758 my_getExpression (&imm_expr, s);
8759 check_absolute_expr (ip, &imm_expr);
8760 if ((unsigned long) imm_expr.X_add_number != 1
8761 && (unsigned long) imm_expr.X_add_number != 3)
8762 {
8763 as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
8764 (unsigned long) imm_expr.X_add_number);
8765 }
8766 INSERT_OPERAND (BP, *ip, imm_expr.X_add_number);
8767 imm_expr.X_op = O_absent;
8768 s = expr_end;
8769 continue;
8770
74cd071d
CF
8771 case '3': /* dsp 3-bit unsigned immediate in bit 21 */
8772 my_getExpression (&imm_expr, s);
8773 check_absolute_expr (ip, &imm_expr);
8774 if (imm_expr.X_add_number & ~OP_MASK_SA3)
8775 {
a9e24354
TS
8776 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8777 OP_MASK_SA3, (unsigned long) imm_expr.X_add_number);
74cd071d 8778 }
a9e24354 8779 INSERT_OPERAND (SA3, *ip, imm_expr.X_add_number);
74cd071d
CF
8780 imm_expr.X_op = O_absent;
8781 s = expr_end;
8782 continue;
8783
8784 case '4': /* dsp 4-bit unsigned immediate in bit 21 */
8785 my_getExpression (&imm_expr, s);
8786 check_absolute_expr (ip, &imm_expr);
8787 if (imm_expr.X_add_number & ~OP_MASK_SA4)
8788 {
a9e24354
TS
8789 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8790 OP_MASK_SA4, (unsigned long) imm_expr.X_add_number);
74cd071d 8791 }
a9e24354 8792 INSERT_OPERAND (SA4, *ip, imm_expr.X_add_number);
74cd071d
CF
8793 imm_expr.X_op = O_absent;
8794 s = expr_end;
8795 continue;
8796
8797 case '5': /* dsp 8-bit unsigned immediate in bit 16 */
8798 my_getExpression (&imm_expr, s);
8799 check_absolute_expr (ip, &imm_expr);
8800 if (imm_expr.X_add_number & ~OP_MASK_IMM8)
8801 {
a9e24354
TS
8802 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8803 OP_MASK_IMM8, (unsigned long) imm_expr.X_add_number);
74cd071d 8804 }
a9e24354 8805 INSERT_OPERAND (IMM8, *ip, imm_expr.X_add_number);
74cd071d
CF
8806 imm_expr.X_op = O_absent;
8807 s = expr_end;
8808 continue;
8809
8810 case '6': /* dsp 5-bit unsigned immediate in bit 21 */
8811 my_getExpression (&imm_expr, s);
8812 check_absolute_expr (ip, &imm_expr);
8813 if (imm_expr.X_add_number & ~OP_MASK_RS)
8814 {
a9e24354
TS
8815 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8816 OP_MASK_RS, (unsigned long) imm_expr.X_add_number);
74cd071d 8817 }
a9e24354 8818 INSERT_OPERAND (RS, *ip, imm_expr.X_add_number);
74cd071d
CF
8819 imm_expr.X_op = O_absent;
8820 s = expr_end;
8821 continue;
8822
8823 case '7': /* four dsp accumulators in bits 11,12 */
8824 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8825 s[3] >= '0' && s[3] <= '3')
8826 {
8827 regno = s[3] - '0';
8828 s += 4;
a9e24354 8829 INSERT_OPERAND (DSPACC, *ip, regno);
74cd071d
CF
8830 continue;
8831 }
8832 else
8833 as_bad (_("Invalid dsp acc register"));
8834 break;
8835
8836 case '8': /* dsp 6-bit unsigned immediate in bit 11 */
8837 my_getExpression (&imm_expr, s);
8838 check_absolute_expr (ip, &imm_expr);
8839 if (imm_expr.X_add_number & ~OP_MASK_WRDSP)
8840 {
a9e24354
TS
8841 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8842 OP_MASK_WRDSP,
8843 (unsigned long) imm_expr.X_add_number);
74cd071d 8844 }
a9e24354 8845 INSERT_OPERAND (WRDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
8846 imm_expr.X_op = O_absent;
8847 s = expr_end;
8848 continue;
8849
8850 case '9': /* four dsp accumulators in bits 21,22 */
8851 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8852 s[3] >= '0' && s[3] <= '3')
8853 {
8854 regno = s[3] - '0';
8855 s += 4;
a9e24354 8856 INSERT_OPERAND (DSPACC_S, *ip, regno);
74cd071d
CF
8857 continue;
8858 }
8859 else
8860 as_bad (_("Invalid dsp acc register"));
8861 break;
8862
8863 case '0': /* dsp 6-bit signed immediate in bit 20 */
8864 my_getExpression (&imm_expr, s);
8865 check_absolute_expr (ip, &imm_expr);
8866 min_range = -((OP_MASK_DSPSFT + 1) >> 1);
8867 max_range = ((OP_MASK_DSPSFT + 1) >> 1) - 1;
8868 if (imm_expr.X_add_number < min_range ||
8869 imm_expr.X_add_number > max_range)
8870 {
a9e24354
TS
8871 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8872 (long) min_range, (long) max_range,
8873 (long) imm_expr.X_add_number);
74cd071d 8874 }
a9e24354 8875 INSERT_OPERAND (DSPSFT, *ip, imm_expr.X_add_number);
74cd071d
CF
8876 imm_expr.X_op = O_absent;
8877 s = expr_end;
8878 continue;
8879
8880 case '\'': /* dsp 6-bit unsigned immediate in bit 16 */
8881 my_getExpression (&imm_expr, s);
8882 check_absolute_expr (ip, &imm_expr);
8883 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
8884 {
a9e24354
TS
8885 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8886 OP_MASK_RDDSP,
8887 (unsigned long) imm_expr.X_add_number);
74cd071d 8888 }
a9e24354 8889 INSERT_OPERAND (RDDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
8890 imm_expr.X_op = O_absent;
8891 s = expr_end;
8892 continue;
8893
8894 case ':': /* dsp 7-bit signed immediate in bit 19 */
8895 my_getExpression (&imm_expr, s);
8896 check_absolute_expr (ip, &imm_expr);
8897 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
8898 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
8899 if (imm_expr.X_add_number < min_range ||
8900 imm_expr.X_add_number > max_range)
8901 {
a9e24354
TS
8902 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8903 (long) min_range, (long) max_range,
8904 (long) imm_expr.X_add_number);
74cd071d 8905 }
a9e24354 8906 INSERT_OPERAND (DSPSFT_7, *ip, imm_expr.X_add_number);
74cd071d
CF
8907 imm_expr.X_op = O_absent;
8908 s = expr_end;
8909 continue;
8910
8911 case '@': /* dsp 10-bit signed immediate in bit 16 */
8912 my_getExpression (&imm_expr, s);
8913 check_absolute_expr (ip, &imm_expr);
8914 min_range = -((OP_MASK_IMM10 + 1) >> 1);
8915 max_range = ((OP_MASK_IMM10 + 1) >> 1) - 1;
8916 if (imm_expr.X_add_number < min_range ||
8917 imm_expr.X_add_number > max_range)
8918 {
a9e24354
TS
8919 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8920 (long) min_range, (long) max_range,
8921 (long) imm_expr.X_add_number);
74cd071d 8922 }
a9e24354 8923 INSERT_OPERAND (IMM10, *ip, imm_expr.X_add_number);
74cd071d
CF
8924 imm_expr.X_op = O_absent;
8925 s = expr_end;
8926 continue;
8927
a9e24354 8928 case '!': /* MT usermode flag bit. */
ef2e4d86
CF
8929 my_getExpression (&imm_expr, s);
8930 check_absolute_expr (ip, &imm_expr);
8931 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
a9e24354
TS
8932 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
8933 (unsigned long) imm_expr.X_add_number);
8934 INSERT_OPERAND (MT_U, *ip, imm_expr.X_add_number);
ef2e4d86
CF
8935 imm_expr.X_op = O_absent;
8936 s = expr_end;
8937 continue;
8938
a9e24354 8939 case '$': /* MT load high flag bit. */
ef2e4d86
CF
8940 my_getExpression (&imm_expr, s);
8941 check_absolute_expr (ip, &imm_expr);
8942 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
a9e24354
TS
8943 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
8944 (unsigned long) imm_expr.X_add_number);
8945 INSERT_OPERAND (MT_H, *ip, imm_expr.X_add_number);
ef2e4d86
CF
8946 imm_expr.X_op = O_absent;
8947 s = expr_end;
8948 continue;
8949
8950 case '*': /* four dsp accumulators in bits 18,19 */
8951 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8952 s[3] >= '0' && s[3] <= '3')
8953 {
8954 regno = s[3] - '0';
8955 s += 4;
a9e24354 8956 INSERT_OPERAND (MTACC_T, *ip, regno);
ef2e4d86
CF
8957 continue;
8958 }
8959 else
8960 as_bad (_("Invalid dsp/smartmips acc register"));
8961 break;
8962
8963 case '&': /* four dsp accumulators in bits 13,14 */
8964 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8965 s[3] >= '0' && s[3] <= '3')
8966 {
8967 regno = s[3] - '0';
8968 s += 4;
a9e24354 8969 INSERT_OPERAND (MTACC_D, *ip, regno);
ef2e4d86
CF
8970 continue;
8971 }
8972 else
8973 as_bad (_("Invalid dsp/smartmips acc register"));
8974 break;
8975
252b5132 8976 case ',':
a339155f 8977 ++argnum;
252b5132
RH
8978 if (*s++ == *args)
8979 continue;
8980 s--;
8981 switch (*++args)
8982 {
8983 case 'r':
8984 case 'v':
bf12938e 8985 INSERT_OPERAND (RS, *ip, lastregno);
252b5132
RH
8986 continue;
8987
8988 case 'w':
bf12938e 8989 INSERT_OPERAND (RT, *ip, lastregno);
38487616
TS
8990 continue;
8991
252b5132 8992 case 'W':
bf12938e 8993 INSERT_OPERAND (FT, *ip, lastregno);
252b5132
RH
8994 continue;
8995
8996 case 'V':
bf12938e 8997 INSERT_OPERAND (FS, *ip, lastregno);
252b5132
RH
8998 continue;
8999 }
9000 break;
9001
9002 case '(':
9003 /* Handle optional base register.
9004 Either the base register is omitted or
bdaaa2e1 9005 we must have a left paren. */
252b5132
RH
9006 /* This is dependent on the next operand specifier
9007 is a base register specification. */
9c2799c2 9008 gas_assert (args[1] == 'b' || args[1] == '5'
252b5132
RH
9009 || args[1] == '-' || args[1] == '4');
9010 if (*s == '\0')
9011 return;
9012
9013 case ')': /* these must match exactly */
60b63b72
RS
9014 case '[':
9015 case ']':
252b5132
RH
9016 if (*s++ == *args)
9017 continue;
9018 break;
9019
af7ee8bf
CD
9020 case '+': /* Opcode extension character. */
9021 switch (*++args)
9022 {
9bcd4f99
TS
9023 case '1': /* UDI immediates. */
9024 case '2':
9025 case '3':
9026 case '4':
9027 {
9028 const struct mips_immed *imm = mips_immed;
9029
9030 while (imm->type && imm->type != *args)
9031 ++imm;
9032 if (! imm->type)
9033 internalError ();
9034 my_getExpression (&imm_expr, s);
9035 check_absolute_expr (ip, &imm_expr);
9036 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
9037 {
9038 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
9039 imm->desc ? imm->desc : ip->insn_mo->name,
9040 (unsigned long) imm_expr.X_add_number,
9041 (unsigned long) imm_expr.X_add_number);
9042 imm_expr.X_add_number &= imm->mask;
9043 }
9044 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
9045 << imm->shift);
9046 imm_expr.X_op = O_absent;
9047 s = expr_end;
9048 }
9049 continue;
9050
071742cf
CD
9051 case 'A': /* ins/ext position, becomes LSB. */
9052 limlo = 0;
9053 limhi = 31;
5f74bc13
CD
9054 goto do_lsb;
9055 case 'E':
9056 limlo = 32;
9057 limhi = 63;
9058 goto do_lsb;
9059do_lsb:
071742cf
CD
9060 my_getExpression (&imm_expr, s);
9061 check_absolute_expr (ip, &imm_expr);
9062 if ((unsigned long) imm_expr.X_add_number < limlo
9063 || (unsigned long) imm_expr.X_add_number > limhi)
9064 {
9065 as_bad (_("Improper position (%lu)"),
9066 (unsigned long) imm_expr.X_add_number);
9067 imm_expr.X_add_number = limlo;
9068 }
9069 lastpos = imm_expr.X_add_number;
bf12938e 9070 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
071742cf
CD
9071 imm_expr.X_op = O_absent;
9072 s = expr_end;
9073 continue;
9074
9075 case 'B': /* ins size, becomes MSB. */
9076 limlo = 1;
9077 limhi = 32;
5f74bc13
CD
9078 goto do_msb;
9079 case 'F':
9080 limlo = 33;
9081 limhi = 64;
9082 goto do_msb;
9083do_msb:
071742cf
CD
9084 my_getExpression (&imm_expr, s);
9085 check_absolute_expr (ip, &imm_expr);
9086 /* Check for negative input so that small negative numbers
9087 will not succeed incorrectly. The checks against
9088 (pos+size) transitively check "size" itself,
9089 assuming that "pos" is reasonable. */
9090 if ((long) imm_expr.X_add_number < 0
9091 || ((unsigned long) imm_expr.X_add_number
9092 + lastpos) < limlo
9093 || ((unsigned long) imm_expr.X_add_number
9094 + lastpos) > limhi)
9095 {
9096 as_bad (_("Improper insert size (%lu, position %lu)"),
9097 (unsigned long) imm_expr.X_add_number,
9098 (unsigned long) lastpos);
9099 imm_expr.X_add_number = limlo - lastpos;
9100 }
bf12938e
RS
9101 INSERT_OPERAND (INSMSB, *ip,
9102 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
9103 imm_expr.X_op = O_absent;
9104 s = expr_end;
9105 continue;
9106
9107 case 'C': /* ext size, becomes MSBD. */
9108 limlo = 1;
9109 limhi = 32;
5f74bc13
CD
9110 goto do_msbd;
9111 case 'G':
9112 limlo = 33;
9113 limhi = 64;
9114 goto do_msbd;
9115 case 'H':
9116 limlo = 33;
9117 limhi = 64;
9118 goto do_msbd;
9119do_msbd:
071742cf
CD
9120 my_getExpression (&imm_expr, s);
9121 check_absolute_expr (ip, &imm_expr);
9122 /* Check for negative input so that small negative numbers
9123 will not succeed incorrectly. The checks against
9124 (pos+size) transitively check "size" itself,
9125 assuming that "pos" is reasonable. */
9126 if ((long) imm_expr.X_add_number < 0
9127 || ((unsigned long) imm_expr.X_add_number
9128 + lastpos) < limlo
9129 || ((unsigned long) imm_expr.X_add_number
9130 + lastpos) > limhi)
9131 {
9132 as_bad (_("Improper extract size (%lu, position %lu)"),
9133 (unsigned long) imm_expr.X_add_number,
9134 (unsigned long) lastpos);
9135 imm_expr.X_add_number = limlo - lastpos;
9136 }
bf12938e 9137 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
9138 imm_expr.X_op = O_absent;
9139 s = expr_end;
9140 continue;
af7ee8bf 9141
bbcc0807
CD
9142 case 'D':
9143 /* +D is for disassembly only; never match. */
9144 break;
9145
5f74bc13
CD
9146 case 'I':
9147 /* "+I" is like "I", except that imm2_expr is used. */
9148 my_getExpression (&imm2_expr, s);
9149 if (imm2_expr.X_op != O_big
9150 && imm2_expr.X_op != O_constant)
9151 insn_error = _("absolute expression required");
9ee2a2d4
MR
9152 if (HAVE_32BIT_GPRS)
9153 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
9154 s = expr_end;
9155 continue;
9156
707bfff6 9157 case 'T': /* Coprocessor register. */
ef2e4d86
CF
9158 /* +T is for disassembly only; never match. */
9159 break;
9160
707bfff6 9161 case 't': /* Coprocessor register number. */
ef2e4d86
CF
9162 if (s[0] == '$' && ISDIGIT (s[1]))
9163 {
9164 ++s;
9165 regno = 0;
9166 do
9167 {
9168 regno *= 10;
9169 regno += *s - '0';
9170 ++s;
9171 }
9172 while (ISDIGIT (*s));
9173 if (regno > 31)
9174 as_bad (_("Invalid register number (%d)"), regno);
9175 else
9176 {
a9e24354 9177 INSERT_OPERAND (RT, *ip, regno);
ef2e4d86
CF
9178 continue;
9179 }
9180 }
9181 else
9182 as_bad (_("Invalid coprocessor 0 register number"));
9183 break;
9184
bb35fb24
NC
9185 case 'x':
9186 /* bbit[01] and bbit[01]32 bit index. Give error if index
9187 is not in the valid range. */
9188 my_getExpression (&imm_expr, s);
9189 check_absolute_expr (ip, &imm_expr);
9190 if ((unsigned) imm_expr.X_add_number > 31)
9191 {
9192 as_bad (_("Improper bit index (%lu)"),
9193 (unsigned long) imm_expr.X_add_number);
9194 imm_expr.X_add_number = 0;
9195 }
9196 INSERT_OPERAND (BBITIND, *ip, imm_expr.X_add_number);
9197 imm_expr.X_op = O_absent;
9198 s = expr_end;
9199 continue;
9200
9201 case 'X':
9202 /* bbit[01] bit index when bbit is used but we generate
9203 bbit[01]32 because the index is over 32. Move to the
9204 next candidate if index is not in the valid range. */
9205 my_getExpression (&imm_expr, s);
9206 check_absolute_expr (ip, &imm_expr);
9207 if ((unsigned) imm_expr.X_add_number < 32
9208 || (unsigned) imm_expr.X_add_number > 63)
9209 break;
9210 INSERT_OPERAND (BBITIND, *ip, imm_expr.X_add_number - 32);
9211 imm_expr.X_op = O_absent;
9212 s = expr_end;
9213 continue;
9214
9215 case 'p':
9216 /* cins, cins32, exts and exts32 position field. Give error
9217 if it's not in the valid range. */
9218 my_getExpression (&imm_expr, s);
9219 check_absolute_expr (ip, &imm_expr);
9220 if ((unsigned) imm_expr.X_add_number > 31)
9221 {
9222 as_bad (_("Improper position (%lu)"),
9223 (unsigned long) imm_expr.X_add_number);
9224 imm_expr.X_add_number = 0;
9225 }
9226 /* Make the pos explicit to simplify +S. */
9227 lastpos = imm_expr.X_add_number + 32;
9228 INSERT_OPERAND (CINSPOS, *ip, imm_expr.X_add_number);
9229 imm_expr.X_op = O_absent;
9230 s = expr_end;
9231 continue;
9232
9233 case 'P':
9234 /* cins, cins32, exts and exts32 position field. Move to
9235 the next candidate if it's not in the valid range. */
9236 my_getExpression (&imm_expr, s);
9237 check_absolute_expr (ip, &imm_expr);
9238 if ((unsigned) imm_expr.X_add_number < 32
9239 || (unsigned) imm_expr.X_add_number > 63)
9240 break;
9241 lastpos = imm_expr.X_add_number;
9242 INSERT_OPERAND (CINSPOS, *ip, imm_expr.X_add_number - 32);
9243 imm_expr.X_op = O_absent;
9244 s = expr_end;
9245 continue;
9246
9247 case 's':
9248 /* cins and exts length-minus-one field. */
9249 my_getExpression (&imm_expr, s);
9250 check_absolute_expr (ip, &imm_expr);
9251 if ((unsigned long) imm_expr.X_add_number > 31)
9252 {
9253 as_bad (_("Improper size (%lu)"),
9254 (unsigned long) imm_expr.X_add_number);
9255 imm_expr.X_add_number = 0;
9256 }
9257 INSERT_OPERAND (CINSLM1, *ip, imm_expr.X_add_number);
9258 imm_expr.X_op = O_absent;
9259 s = expr_end;
9260 continue;
9261
9262 case 'S':
9263 /* cins32/exts32 and cins/exts aliasing cint32/exts32
9264 length-minus-one field. */
9265 my_getExpression (&imm_expr, s);
9266 check_absolute_expr (ip, &imm_expr);
9267 if ((long) imm_expr.X_add_number < 0
9268 || (unsigned long) imm_expr.X_add_number + lastpos > 63)
9269 {
9270 as_bad (_("Improper size (%lu)"),
9271 (unsigned long) imm_expr.X_add_number);
9272 imm_expr.X_add_number = 0;
9273 }
9274 INSERT_OPERAND (CINSLM1, *ip, imm_expr.X_add_number);
9275 imm_expr.X_op = O_absent;
9276 s = expr_end;
9277 continue;
9278
dd3cbb7e
NC
9279 case 'Q':
9280 /* seqi/snei immediate field. */
9281 my_getExpression (&imm_expr, s);
9282 check_absolute_expr (ip, &imm_expr);
9283 if ((long) imm_expr.X_add_number < -512
9284 || (long) imm_expr.X_add_number >= 512)
9285 {
9286 as_bad (_("Improper immediate (%ld)"),
9287 (long) imm_expr.X_add_number);
9288 imm_expr.X_add_number = 0;
9289 }
9290 INSERT_OPERAND (SEQI, *ip, imm_expr.X_add_number);
9291 imm_expr.X_op = O_absent;
9292 s = expr_end;
9293 continue;
9294
af7ee8bf
CD
9295 default:
9296 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
9297 *args, insn->name, insn->args);
9298 /* Further processing is fruitless. */
9299 return;
9300 }
9301 break;
9302
252b5132
RH
9303 case '<': /* must be at least one digit */
9304 /*
9305 * According to the manual, if the shift amount is greater
b6ff326e
KH
9306 * than 31 or less than 0, then the shift amount should be
9307 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
9308 * We issue a warning and mask out all but the low 5 bits.
9309 */
9310 my_getExpression (&imm_expr, s);
9311 check_absolute_expr (ip, &imm_expr);
9312 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
9313 as_warn (_("Improper shift amount (%lu)"),
9314 (unsigned long) imm_expr.X_add_number);
9315 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
9316 imm_expr.X_op = O_absent;
9317 s = expr_end;
9318 continue;
9319
9320 case '>': /* shift amount minus 32 */
9321 my_getExpression (&imm_expr, s);
9322 check_absolute_expr (ip, &imm_expr);
9323 if ((unsigned long) imm_expr.X_add_number < 32
9324 || (unsigned long) imm_expr.X_add_number > 63)
9325 break;
bf12938e 9326 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
9327 imm_expr.X_op = O_absent;
9328 s = expr_end;
9329 continue;
9330
252b5132
RH
9331 case 'k': /* cache code */
9332 case 'h': /* prefx code */
620edafd 9333 case '1': /* sync type */
252b5132
RH
9334 my_getExpression (&imm_expr, s);
9335 check_absolute_expr (ip, &imm_expr);
9336 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
9337 as_warn (_("Invalid value for `%s' (%lu)"),
9338 ip->insn_mo->name,
9339 (unsigned long) imm_expr.X_add_number);
252b5132 9340 if (*args == 'k')
bf12938e 9341 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
620edafd 9342 else if (*args == 'h')
bf12938e 9343 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
620edafd
CF
9344 else
9345 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
9346 imm_expr.X_op = O_absent;
9347 s = expr_end;
9348 continue;
9349
9350 case 'c': /* break code */
9351 my_getExpression (&imm_expr, s);
9352 check_absolute_expr (ip, &imm_expr);
a9e24354
TS
9353 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE)
9354 as_warn (_("Code for %s not in range 0..1023 (%lu)"),
9355 ip->insn_mo->name,
bf12938e
RS
9356 (unsigned long) imm_expr.X_add_number);
9357 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
252b5132
RH
9358 imm_expr.X_op = O_absent;
9359 s = expr_end;
9360 continue;
9361
9362 case 'q': /* lower break code */
9363 my_getExpression (&imm_expr, s);
9364 check_absolute_expr (ip, &imm_expr);
a9e24354
TS
9365 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE2)
9366 as_warn (_("Lower code for %s not in range 0..1023 (%lu)"),
9367 ip->insn_mo->name,
bf12938e
RS
9368 (unsigned long) imm_expr.X_add_number);
9369 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
252b5132
RH
9370 imm_expr.X_op = O_absent;
9371 s = expr_end;
9372 continue;
9373
4372b673 9374 case 'B': /* 20-bit syscall/break code. */
156c2f8b 9375 my_getExpression (&imm_expr, s);
156c2f8b 9376 check_absolute_expr (ip, &imm_expr);
793b27f4 9377 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
a9e24354
TS
9378 as_warn (_("Code for %s not in range 0..1048575 (%lu)"),
9379 ip->insn_mo->name,
793b27f4 9380 (unsigned long) imm_expr.X_add_number);
bf12938e 9381 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
252b5132
RH
9382 imm_expr.X_op = O_absent;
9383 s = expr_end;
9384 continue;
9385
98d3f06f 9386 case 'C': /* Coprocessor code */
beae10d5 9387 my_getExpression (&imm_expr, s);
252b5132 9388 check_absolute_expr (ip, &imm_expr);
a9e24354 9389 if ((unsigned long) imm_expr.X_add_number > OP_MASK_COPZ)
252b5132 9390 {
793b27f4
TS
9391 as_warn (_("Coproccesor code > 25 bits (%lu)"),
9392 (unsigned long) imm_expr.X_add_number);
a9e24354 9393 imm_expr.X_add_number &= OP_MASK_COPZ;
252b5132 9394 }
a9e24354 9395 INSERT_OPERAND (COPZ, *ip, imm_expr.X_add_number);
beae10d5
KH
9396 imm_expr.X_op = O_absent;
9397 s = expr_end;
9398 continue;
252b5132 9399
4372b673
NC
9400 case 'J': /* 19-bit wait code. */
9401 my_getExpression (&imm_expr, s);
9402 check_absolute_expr (ip, &imm_expr);
793b27f4 9403 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
a9e24354
TS
9404 {
9405 as_warn (_("Illegal 19-bit code (%lu)"),
9406 (unsigned long) imm_expr.X_add_number);
9407 imm_expr.X_add_number &= OP_MASK_CODE19;
9408 }
bf12938e 9409 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
9410 imm_expr.X_op = O_absent;
9411 s = expr_end;
9412 continue;
9413
707bfff6 9414 case 'P': /* Performance register. */
beae10d5 9415 my_getExpression (&imm_expr, s);
252b5132 9416 check_absolute_expr (ip, &imm_expr);
beae10d5 9417 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
9418 as_warn (_("Invalid performance register (%lu)"),
9419 (unsigned long) imm_expr.X_add_number);
9420 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
9421 imm_expr.X_op = O_absent;
9422 s = expr_end;
9423 continue;
252b5132 9424
707bfff6
TS
9425 case 'G': /* Coprocessor destination register. */
9426 if (((ip->insn_opcode >> OP_SH_OP) & OP_MASK_OP) == OP_OP_COP0)
9427 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_CP0, &regno);
9428 else
9429 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
a9e24354 9430 INSERT_OPERAND (RD, *ip, regno);
707bfff6
TS
9431 if (ok)
9432 {
9433 lastregno = regno;
9434 continue;
9435 }
9436 else
9437 break;
9438
252b5132
RH
9439 case 'b': /* base register */
9440 case 'd': /* destination register */
9441 case 's': /* source register */
9442 case 't': /* target register */
9443 case 'r': /* both target and source */
9444 case 'v': /* both dest and source */
9445 case 'w': /* both dest and target */
9446 case 'E': /* coprocessor target register */
af7ee8bf 9447 case 'K': /* 'rdhwr' destination register */
252b5132
RH
9448 case 'x': /* ignore register name */
9449 case 'z': /* must be zero register */
4372b673 9450 case 'U': /* destination register (clo/clz). */
ef2e4d86 9451 case 'g': /* coprocessor destination register */
707bfff6
TS
9452 s_reset = s;
9453 if (*args == 'E' || *args == 'K')
9454 ok = reg_lookup (&s, RTYPE_NUM, &regno);
9455 else
9456 {
9457 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
741fe287
MR
9458 if (regno == AT && mips_opts.at)
9459 {
9460 if (mips_opts.at == ATREG)
9461 as_warn (_("used $at without \".set noat\""));
9462 else
9463 as_warn (_("used $%u with \".set at=$%u\""),
9464 regno, mips_opts.at);
9465 }
707bfff6
TS
9466 }
9467 if (ok)
252b5132 9468 {
252b5132
RH
9469 c = *args;
9470 if (*s == ' ')
f9419b05 9471 ++s;
252b5132
RH
9472 if (args[1] != *s)
9473 {
9474 if (c == 'r' || c == 'v' || c == 'w')
9475 {
9476 regno = lastregno;
9477 s = s_reset;
f9419b05 9478 ++args;
252b5132
RH
9479 }
9480 }
9481 /* 'z' only matches $0. */
9482 if (c == 'z' && regno != 0)
9483 break;
9484
24864476 9485 if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
e7c604dd
CM
9486 {
9487 if (regno == lastregno)
9488 {
24864476 9489 insn_error = _("source and destination must be different");
e7c604dd
CM
9490 continue;
9491 }
24864476 9492 if (regno == 31 && lastregno == 0xffffffff)
e7c604dd
CM
9493 {
9494 insn_error = _("a destination register must be supplied");
9495 continue;
9496 }
9497 }
bdaaa2e1
KH
9498 /* Now that we have assembled one operand, we use the args string
9499 * to figure out where it goes in the instruction. */
252b5132
RH
9500 switch (c)
9501 {
9502 case 'r':
9503 case 's':
9504 case 'v':
9505 case 'b':
bf12938e 9506 INSERT_OPERAND (RS, *ip, regno);
252b5132
RH
9507 break;
9508 case 'd':
9509 case 'G':
af7ee8bf 9510 case 'K':
ef2e4d86 9511 case 'g':
bf12938e 9512 INSERT_OPERAND (RD, *ip, regno);
252b5132 9513 break;
4372b673 9514 case 'U':
bf12938e
RS
9515 INSERT_OPERAND (RD, *ip, regno);
9516 INSERT_OPERAND (RT, *ip, regno);
4372b673 9517 break;
252b5132
RH
9518 case 'w':
9519 case 't':
9520 case 'E':
bf12938e 9521 INSERT_OPERAND (RT, *ip, regno);
252b5132
RH
9522 break;
9523 case 'x':
9524 /* This case exists because on the r3000 trunc
9525 expands into a macro which requires a gp
9526 register. On the r6000 or r4000 it is
9527 assembled into a single instruction which
9528 ignores the register. Thus the insn version
9529 is MIPS_ISA2 and uses 'x', and the macro
9530 version is MIPS_ISA1 and uses 't'. */
9531 break;
9532 case 'z':
9533 /* This case is for the div instruction, which
9534 acts differently if the destination argument
9535 is $0. This only matches $0, and is checked
9536 outside the switch. */
9537 break;
9538 case 'D':
9539 /* Itbl operand; not yet implemented. FIXME ?? */
9540 break;
9541 /* What about all other operands like 'i', which
9542 can be specified in the opcode table? */
9543 }
9544 lastregno = regno;
9545 continue;
9546 }
252b5132
RH
9547 switch (*args++)
9548 {
9549 case 'r':
9550 case 'v':
bf12938e 9551 INSERT_OPERAND (RS, *ip, lastregno);
252b5132
RH
9552 continue;
9553 case 'w':
bf12938e 9554 INSERT_OPERAND (RT, *ip, lastregno);
252b5132
RH
9555 continue;
9556 }
9557 break;
9558
deec1734
CD
9559 case 'O': /* MDMX alignment immediate constant. */
9560 my_getExpression (&imm_expr, s);
9561 check_absolute_expr (ip, &imm_expr);
9562 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
20203fb9 9563 as_warn (_("Improper align amount (%ld), using low bits"),
bf12938e
RS
9564 (long) imm_expr.X_add_number);
9565 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
deec1734
CD
9566 imm_expr.X_op = O_absent;
9567 s = expr_end;
9568 continue;
9569
9570 case 'Q': /* MDMX vector, element sel, or const. */
9571 if (s[0] != '$')
9572 {
9573 /* MDMX Immediate. */
9574 my_getExpression (&imm_expr, s);
9575 check_absolute_expr (ip, &imm_expr);
9576 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
9577 as_warn (_("Invalid MDMX Immediate (%ld)"),
9578 (long) imm_expr.X_add_number);
9579 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
deec1734
CD
9580 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9581 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
9582 else
9583 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
9584 imm_expr.X_op = O_absent;
9585 s = expr_end;
9586 continue;
9587 }
9588 /* Not MDMX Immediate. Fall through. */
9589 case 'X': /* MDMX destination register. */
9590 case 'Y': /* MDMX source register. */
9591 case 'Z': /* MDMX target register. */
9592 is_mdmx = 1;
252b5132
RH
9593 case 'D': /* floating point destination register */
9594 case 'S': /* floating point source register */
9595 case 'T': /* floating point target register */
9596 case 'R': /* floating point source register */
9597 case 'V':
9598 case 'W':
707bfff6
TS
9599 rtype = RTYPE_FPU;
9600 if (is_mdmx
9601 || (mips_opts.ase_mdmx
9602 && (ip->insn_mo->pinfo & FP_D)
9603 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
9604 | INSN_COPROC_MEMORY_DELAY
9605 | INSN_LOAD_COPROC_DELAY
9606 | INSN_LOAD_MEMORY_DELAY
9607 | INSN_STORE_MEMORY))))
9608 rtype |= RTYPE_VEC;
252b5132 9609 s_reset = s;
707bfff6 9610 if (reg_lookup (&s, rtype, &regno))
252b5132 9611 {
252b5132 9612 if ((regno & 1) != 0
ca4e0257 9613 && HAVE_32BIT_FPRS
7455baf8 9614 && ! mips_oddfpreg_ok (ip->insn_mo, argnum))
252b5132
RH
9615 as_warn (_("Float register should be even, was %d"),
9616 regno);
9617
9618 c = *args;
9619 if (*s == ' ')
f9419b05 9620 ++s;
252b5132
RH
9621 if (args[1] != *s)
9622 {
9623 if (c == 'V' || c == 'W')
9624 {
9625 regno = lastregno;
9626 s = s_reset;
f9419b05 9627 ++args;
252b5132
RH
9628 }
9629 }
9630 switch (c)
9631 {
9632 case 'D':
deec1734 9633 case 'X':
bf12938e 9634 INSERT_OPERAND (FD, *ip, regno);
252b5132
RH
9635 break;
9636 case 'V':
9637 case 'S':
deec1734 9638 case 'Y':
bf12938e 9639 INSERT_OPERAND (FS, *ip, regno);
252b5132 9640 break;
deec1734
CD
9641 case 'Q':
9642 /* This is like 'Z', but also needs to fix the MDMX
9643 vector/scalar select bits. Note that the
9644 scalar immediate case is handled above. */
9645 if (*s == '[')
9646 {
9647 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9648 int max_el = (is_qh ? 3 : 7);
9649 s++;
9650 my_getExpression(&imm_expr, s);
9651 check_absolute_expr (ip, &imm_expr);
9652 s = expr_end;
9653 if (imm_expr.X_add_number > max_el)
20203fb9
NC
9654 as_bad (_("Bad element selector %ld"),
9655 (long) imm_expr.X_add_number);
deec1734
CD
9656 imm_expr.X_add_number &= max_el;
9657 ip->insn_opcode |= (imm_expr.X_add_number
9658 << (OP_SH_VSEL +
9659 (is_qh ? 2 : 1)));
01a3f561 9660 imm_expr.X_op = O_absent;
deec1734 9661 if (*s != ']')
20203fb9 9662 as_warn (_("Expecting ']' found '%s'"), s);
deec1734
CD
9663 else
9664 s++;
9665 }
9666 else
9667 {
9668 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9669 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9670 << OP_SH_VSEL);
9671 else
9672 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9673 OP_SH_VSEL);
9674 }
9675 /* Fall through */
252b5132
RH
9676 case 'W':
9677 case 'T':
deec1734 9678 case 'Z':
bf12938e 9679 INSERT_OPERAND (FT, *ip, regno);
252b5132
RH
9680 break;
9681 case 'R':
bf12938e 9682 INSERT_OPERAND (FR, *ip, regno);
252b5132
RH
9683 break;
9684 }
9685 lastregno = regno;
9686 continue;
9687 }
9688
252b5132
RH
9689 switch (*args++)
9690 {
9691 case 'V':
bf12938e 9692 INSERT_OPERAND (FS, *ip, lastregno);
252b5132
RH
9693 continue;
9694 case 'W':
bf12938e 9695 INSERT_OPERAND (FT, *ip, lastregno);
252b5132
RH
9696 continue;
9697 }
9698 break;
9699
9700 case 'I':
9701 my_getExpression (&imm_expr, s);
9702 if (imm_expr.X_op != O_big
9703 && imm_expr.X_op != O_constant)
9704 insn_error = _("absolute expression required");
9ee2a2d4
MR
9705 if (HAVE_32BIT_GPRS)
9706 normalize_constant_expr (&imm_expr);
252b5132
RH
9707 s = expr_end;
9708 continue;
9709
9710 case 'A':
9711 my_getExpression (&offset_expr, s);
2051e8c4 9712 normalize_address_expr (&offset_expr);
f6688943 9713 *imm_reloc = BFD_RELOC_32;
252b5132
RH
9714 s = expr_end;
9715 continue;
9716
9717 case 'F':
9718 case 'L':
9719 case 'f':
9720 case 'l':
9721 {
9722 int f64;
ca4e0257 9723 int using_gprs;
252b5132
RH
9724 char *save_in;
9725 char *err;
9726 unsigned char temp[8];
9727 int len;
9728 unsigned int length;
9729 segT seg;
9730 subsegT subseg;
9731 char *p;
9732
9733 /* These only appear as the last operand in an
9734 instruction, and every instruction that accepts
9735 them in any variant accepts them in all variants.
9736 This means we don't have to worry about backing out
9737 any changes if the instruction does not match.
9738
9739 The difference between them is the size of the
9740 floating point constant and where it goes. For 'F'
9741 and 'L' the constant is 64 bits; for 'f' and 'l' it
9742 is 32 bits. Where the constant is placed is based
9743 on how the MIPS assembler does things:
9744 F -- .rdata
9745 L -- .lit8
9746 f -- immediate value
9747 l -- .lit4
9748
9749 The .lit4 and .lit8 sections are only used if
9750 permitted by the -G argument.
9751
ca4e0257
RS
9752 The code below needs to know whether the target register
9753 is 32 or 64 bits wide. It relies on the fact 'f' and
9754 'F' are used with GPR-based instructions and 'l' and
9755 'L' are used with FPR-based instructions. */
252b5132
RH
9756
9757 f64 = *args == 'F' || *args == 'L';
ca4e0257 9758 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
9759
9760 save_in = input_line_pointer;
9761 input_line_pointer = s;
9762 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9763 length = len;
9764 s = input_line_pointer;
9765 input_line_pointer = save_in;
9766 if (err != NULL && *err != '\0')
9767 {
9768 as_bad (_("Bad floating point constant: %s"), err);
9769 memset (temp, '\0', sizeof temp);
9770 length = f64 ? 8 : 4;
9771 }
9772
9c2799c2 9773 gas_assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
9774
9775 if (*args == 'f'
9776 || (*args == 'l'
3e722fb5 9777 && (g_switch_value < 4
252b5132
RH
9778 || (temp[0] == 0 && temp[1] == 0)
9779 || (temp[2] == 0 && temp[3] == 0))))
9780 {
9781 imm_expr.X_op = O_constant;
9782 if (! target_big_endian)
9783 imm_expr.X_add_number = bfd_getl32 (temp);
9784 else
9785 imm_expr.X_add_number = bfd_getb32 (temp);
9786 }
9787 else if (length > 4
119d663a 9788 && ! mips_disable_float_construction
ca4e0257
RS
9789 /* Constants can only be constructed in GPRs and
9790 copied to FPRs if the GPRs are at least as wide
9791 as the FPRs. Force the constant into memory if
9792 we are using 64-bit FPRs but the GPRs are only
9793 32 bits wide. */
9794 && (using_gprs
9795 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
9796 && ((temp[0] == 0 && temp[1] == 0)
9797 || (temp[2] == 0 && temp[3] == 0))
9798 && ((temp[4] == 0 && temp[5] == 0)
9799 || (temp[6] == 0 && temp[7] == 0)))
9800 {
ca4e0257
RS
9801 /* The value is simple enough to load with a couple of
9802 instructions. If using 32-bit registers, set
9803 imm_expr to the high order 32 bits and offset_expr to
9804 the low order 32 bits. Otherwise, set imm_expr to
9805 the entire 64 bit constant. */
9806 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
9807 {
9808 imm_expr.X_op = O_constant;
9809 offset_expr.X_op = O_constant;
9810 if (! target_big_endian)
9811 {
9812 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9813 offset_expr.X_add_number = bfd_getl32 (temp);
9814 }
9815 else
9816 {
9817 imm_expr.X_add_number = bfd_getb32 (temp);
9818 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9819 }
9820 if (offset_expr.X_add_number == 0)
9821 offset_expr.X_op = O_absent;
9822 }
9823 else if (sizeof (imm_expr.X_add_number) > 4)
9824 {
9825 imm_expr.X_op = O_constant;
9826 if (! target_big_endian)
9827 imm_expr.X_add_number = bfd_getl64 (temp);
9828 else
9829 imm_expr.X_add_number = bfd_getb64 (temp);
9830 }
9831 else
9832 {
9833 imm_expr.X_op = O_big;
9834 imm_expr.X_add_number = 4;
9835 if (! target_big_endian)
9836 {
9837 generic_bignum[0] = bfd_getl16 (temp);
9838 generic_bignum[1] = bfd_getl16 (temp + 2);
9839 generic_bignum[2] = bfd_getl16 (temp + 4);
9840 generic_bignum[3] = bfd_getl16 (temp + 6);
9841 }
9842 else
9843 {
9844 generic_bignum[0] = bfd_getb16 (temp + 6);
9845 generic_bignum[1] = bfd_getb16 (temp + 4);
9846 generic_bignum[2] = bfd_getb16 (temp + 2);
9847 generic_bignum[3] = bfd_getb16 (temp);
9848 }
9849 }
9850 }
9851 else
9852 {
9853 const char *newname;
9854 segT new_seg;
9855
9856 /* Switch to the right section. */
9857 seg = now_seg;
9858 subseg = now_subseg;
9859 switch (*args)
9860 {
9861 default: /* unused default case avoids warnings. */
9862 case 'L':
9863 newname = RDATA_SECTION_NAME;
3e722fb5 9864 if (g_switch_value >= 8)
252b5132
RH
9865 newname = ".lit8";
9866 break;
9867 case 'F':
3e722fb5 9868 newname = RDATA_SECTION_NAME;
252b5132
RH
9869 break;
9870 case 'l':
9c2799c2 9871 gas_assert (g_switch_value >= 4);
252b5132
RH
9872 newname = ".lit4";
9873 break;
9874 }
9875 new_seg = subseg_new (newname, (subsegT) 0);
f43abd2b 9876 if (IS_ELF)
252b5132
RH
9877 bfd_set_section_flags (stdoutput, new_seg,
9878 (SEC_ALLOC
9879 | SEC_LOAD
9880 | SEC_READONLY
9881 | SEC_DATA));
9882 frag_align (*args == 'l' ? 2 : 3, 0, 0);
c41e87e3 9883 if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
9884 record_alignment (new_seg, 4);
9885 else
9886 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9887 if (seg == now_seg)
9888 as_bad (_("Can't use floating point insn in this section"));
9889
9890 /* Set the argument to the current address in the
9891 section. */
9892 offset_expr.X_op = O_symbol;
9893 offset_expr.X_add_symbol =
9894 symbol_new ("L0\001", now_seg,
9895 (valueT) frag_now_fix (), frag_now);
9896 offset_expr.X_add_number = 0;
9897
9898 /* Put the floating point number into the section. */
9899 p = frag_more ((int) length);
9900 memcpy (p, temp, length);
9901
9902 /* Switch back to the original section. */
9903 subseg_set (seg, subseg);
9904 }
9905 }
9906 continue;
9907
9908 case 'i': /* 16 bit unsigned immediate */
9909 case 'j': /* 16 bit signed immediate */
f6688943 9910 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9911 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9912 {
9913 int more;
5e0116d5
RS
9914 offsetT minval, maxval;
9915
9916 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9917 && strcmp (insn->name, insn[1].name) == 0);
9918
9919 /* If the expression was written as an unsigned number,
9920 only treat it as signed if there are no more
9921 alternatives. */
9922 if (more
9923 && *args == 'j'
9924 && sizeof (imm_expr.X_add_number) <= 4
9925 && imm_expr.X_op == O_constant
9926 && imm_expr.X_add_number < 0
9927 && imm_expr.X_unsigned
9928 && HAVE_64BIT_GPRS)
9929 break;
9930
9931 /* For compatibility with older assemblers, we accept
9932 0x8000-0xffff as signed 16-bit numbers when only
9933 signed numbers are allowed. */
9934 if (*args == 'i')
9935 minval = 0, maxval = 0xffff;
9936 else if (more)
9937 minval = -0x8000, maxval = 0x7fff;
252b5132 9938 else
5e0116d5
RS
9939 minval = -0x8000, maxval = 0xffff;
9940
9941 if (imm_expr.X_op != O_constant
9942 || imm_expr.X_add_number < minval
9943 || imm_expr.X_add_number > maxval)
252b5132
RH
9944 {
9945 if (more)
9946 break;
2ae7e77b
AH
9947 if (imm_expr.X_op == O_constant
9948 || imm_expr.X_op == O_big)
5e0116d5 9949 as_bad (_("expression out of range"));
252b5132
RH
9950 }
9951 }
9952 s = expr_end;
9953 continue;
9954
9955 case 'o': /* 16 bit offset */
5e0116d5
RS
9956 /* Check whether there is only a single bracketed expression
9957 left. If so, it must be the base register and the
9958 constant must be zero. */
9959 if (*s == '(' && strchr (s + 1, '(') == 0)
9960 {
9961 offset_expr.X_op = O_constant;
9962 offset_expr.X_add_number = 0;
9963 continue;
9964 }
252b5132
RH
9965
9966 /* If this value won't fit into a 16 bit offset, then go
9967 find a macro that will generate the 32 bit offset
afdbd6d0 9968 code pattern. */
5e0116d5 9969 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9970 && (offset_expr.X_op != O_constant
9971 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9972 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9973 break;
9974
252b5132
RH
9975 s = expr_end;
9976 continue;
9977
9978 case 'p': /* pc relative offset */
0b25d3e6 9979 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9980 my_getExpression (&offset_expr, s);
9981 s = expr_end;
9982 continue;
9983
9984 case 'u': /* upper 16 bits */
5e0116d5
RS
9985 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9986 && imm_expr.X_op == O_constant
9987 && (imm_expr.X_add_number < 0
9988 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9989 as_bad (_("lui expression not in range 0..65535"));
9990 s = expr_end;
9991 continue;
9992
9993 case 'a': /* 26 bit address */
9994 my_getExpression (&offset_expr, s);
9995 s = expr_end;
f6688943 9996 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9997 continue;
9998
9999 case 'N': /* 3 bit branch condition code */
10000 case 'M': /* 3 bit compare condition code */
707bfff6
TS
10001 rtype = RTYPE_CCC;
10002 if (ip->insn_mo->pinfo & (FP_D| FP_S))
10003 rtype |= RTYPE_FCC;
10004 if (!reg_lookup (&s, rtype, &regno))
252b5132 10005 break;
30c378fd
CD
10006 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
10007 || strcmp(str + strlen(str) - 5, "any2f") == 0
10008 || strcmp(str + strlen(str) - 5, "any2t") == 0)
10009 && (regno & 1) != 0)
20203fb9
NC
10010 as_warn (_("Condition code register should be even for %s, was %d"),
10011 str, regno);
30c378fd
CD
10012 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
10013 || strcmp(str + strlen(str) - 5, "any4t") == 0)
10014 && (regno & 3) != 0)
20203fb9
NC
10015 as_warn (_("Condition code register should be 0 or 4 for %s, was %d"),
10016 str, regno);
252b5132 10017 if (*args == 'N')
bf12938e 10018 INSERT_OPERAND (BCC, *ip, regno);
252b5132 10019 else
bf12938e 10020 INSERT_OPERAND (CCC, *ip, regno);
beae10d5 10021 continue;
252b5132 10022
156c2f8b
NC
10023 case 'H':
10024 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
10025 s += 2;
3882b010 10026 if (ISDIGIT (*s))
156c2f8b
NC
10027 {
10028 c = 0;
10029 do
10030 {
10031 c *= 10;
10032 c += *s - '0';
10033 ++s;
10034 }
3882b010 10035 while (ISDIGIT (*s));
156c2f8b
NC
10036 }
10037 else
10038 c = 8; /* Invalid sel value. */
10039
10040 if (c > 7)
10041 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
10042 ip->insn_opcode |= c;
10043 continue;
10044
60b63b72
RS
10045 case 'e':
10046 /* Must be at least one digit. */
10047 my_getExpression (&imm_expr, s);
10048 check_absolute_expr (ip, &imm_expr);
10049
10050 if ((unsigned long) imm_expr.X_add_number
10051 > (unsigned long) OP_MASK_VECBYTE)
10052 {
10053 as_bad (_("bad byte vector index (%ld)"),
10054 (long) imm_expr.X_add_number);
10055 imm_expr.X_add_number = 0;
10056 }
10057
bf12938e 10058 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
60b63b72
RS
10059 imm_expr.X_op = O_absent;
10060 s = expr_end;
10061 continue;
10062
10063 case '%':
10064 my_getExpression (&imm_expr, s);
10065 check_absolute_expr (ip, &imm_expr);
10066
10067 if ((unsigned long) imm_expr.X_add_number
10068 > (unsigned long) OP_MASK_VECALIGN)
10069 {
10070 as_bad (_("bad byte vector index (%ld)"),
10071 (long) imm_expr.X_add_number);
10072 imm_expr.X_add_number = 0;
10073 }
10074
bf12938e 10075 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
60b63b72
RS
10076 imm_expr.X_op = O_absent;
10077 s = expr_end;
10078 continue;
10079
252b5132
RH
10080 default:
10081 as_bad (_("bad char = '%c'\n"), *args);
10082 internalError ();
10083 }
10084 break;
10085 }
10086 /* Args don't match. */
10087 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
10088 !strcmp (insn->name, insn[1].name))
10089 {
10090 ++insn;
10091 s = argsStart;
268f6bed 10092 insn_error = _("illegal operands");
252b5132
RH
10093 continue;
10094 }
268f6bed 10095 if (save_c)
570de991 10096 *(--argsStart) = save_c;
252b5132
RH
10097 insn_error = _("illegal operands");
10098 return;
10099 }
10100}
10101
0499d65b
TS
10102#define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
10103
252b5132
RH
10104/* This routine assembles an instruction into its binary format when
10105 assembling for the mips16. As a side effect, it sets one of the
10106 global variables imm_reloc or offset_reloc to the type of
10107 relocation to do if one of the operands is an address expression.
10108 It also sets mips16_small and mips16_ext if the user explicitly
10109 requested a small or extended instruction. */
10110
10111static void
17a2f251 10112mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
10113{
10114 char *s;
10115 const char *args;
10116 struct mips_opcode *insn;
10117 char *argsstart;
10118 unsigned int regno;
10119 unsigned int lastregno = 0;
10120 char *s_reset;
d6f16593 10121 size_t i;
252b5132
RH
10122
10123 insn_error = NULL;
10124
b34976b6
AM
10125 mips16_small = FALSE;
10126 mips16_ext = FALSE;
252b5132 10127
3882b010 10128 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
10129 ;
10130 switch (*s)
10131 {
10132 case '\0':
10133 break;
10134
10135 case ' ':
10136 *s++ = '\0';
10137 break;
10138
10139 case '.':
10140 if (s[1] == 't' && s[2] == ' ')
10141 {
10142 *s = '\0';
b34976b6 10143 mips16_small = TRUE;
252b5132
RH
10144 s += 3;
10145 break;
10146 }
10147 else if (s[1] == 'e' && s[2] == ' ')
10148 {
10149 *s = '\0';
b34976b6 10150 mips16_ext = TRUE;
252b5132
RH
10151 s += 3;
10152 break;
10153 }
10154 /* Fall through. */
10155 default:
10156 insn_error = _("unknown opcode");
10157 return;
10158 }
10159
10160 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 10161 mips16_small = TRUE;
252b5132
RH
10162
10163 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
10164 {
10165 insn_error = _("unrecognized opcode");
10166 return;
10167 }
10168
10169 argsstart = s;
10170 for (;;)
10171 {
9b3f89ee
TS
10172 bfd_boolean ok;
10173
9c2799c2 10174 gas_assert (strcmp (insn->name, str) == 0);
252b5132 10175
037b32b9 10176 ok = is_opcode_valid_16 (insn);
9b3f89ee
TS
10177 if (! ok)
10178 {
10179 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
10180 && strcmp (insn->name, insn[1].name) == 0)
10181 {
10182 ++insn;
10183 continue;
10184 }
10185 else
10186 {
10187 if (!insn_error)
10188 {
10189 static char buf[100];
10190 sprintf (buf,
10191 _("opcode not supported on this processor: %s (%s)"),
10192 mips_cpu_info_from_arch (mips_opts.arch)->name,
10193 mips_cpu_info_from_isa (mips_opts.isa)->name);
10194 insn_error = buf;
10195 }
10196 return;
10197 }
10198 }
10199
1e915849 10200 create_insn (ip, insn);
252b5132 10201 imm_expr.X_op = O_absent;
f6688943
TS
10202 imm_reloc[0] = BFD_RELOC_UNUSED;
10203 imm_reloc[1] = BFD_RELOC_UNUSED;
10204 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 10205 imm2_expr.X_op = O_absent;
252b5132 10206 offset_expr.X_op = O_absent;
f6688943
TS
10207 offset_reloc[0] = BFD_RELOC_UNUSED;
10208 offset_reloc[1] = BFD_RELOC_UNUSED;
10209 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
10210 for (args = insn->args; 1; ++args)
10211 {
10212 int c;
10213
10214 if (*s == ' ')
10215 ++s;
10216
10217 /* In this switch statement we call break if we did not find
10218 a match, continue if we did find a match, or return if we
10219 are done. */
10220
10221 c = *args;
10222 switch (c)
10223 {
10224 case '\0':
10225 if (*s == '\0')
10226 {
10227 /* Stuff the immediate value in now, if we can. */
10228 if (imm_expr.X_op == O_constant
f6688943 10229 && *imm_reloc > BFD_RELOC_UNUSED
738e5348
RS
10230 && *imm_reloc != BFD_RELOC_MIPS16_GOT16
10231 && *imm_reloc != BFD_RELOC_MIPS16_CALL16
252b5132
RH
10232 && insn->pinfo != INSN_MACRO)
10233 {
d6f16593
MR
10234 valueT tmp;
10235
10236 switch (*offset_reloc)
10237 {
10238 case BFD_RELOC_MIPS16_HI16_S:
10239 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
10240 break;
10241
10242 case BFD_RELOC_MIPS16_HI16:
10243 tmp = imm_expr.X_add_number >> 16;
10244 break;
10245
10246 case BFD_RELOC_MIPS16_LO16:
10247 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
10248 - 0x8000;
10249 break;
10250
10251 case BFD_RELOC_UNUSED:
10252 tmp = imm_expr.X_add_number;
10253 break;
10254
10255 default:
10256 internalError ();
10257 }
10258 *offset_reloc = BFD_RELOC_UNUSED;
10259
c4e7957c 10260 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
d6f16593 10261 tmp, TRUE, mips16_small,
252b5132
RH
10262 mips16_ext, &ip->insn_opcode,
10263 &ip->use_extend, &ip->extend);
10264 imm_expr.X_op = O_absent;
f6688943 10265 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
10266 }
10267
10268 return;
10269 }
10270 break;
10271
10272 case ',':
10273 if (*s++ == c)
10274 continue;
10275 s--;
10276 switch (*++args)
10277 {
10278 case 'v':
bf12938e 10279 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
10280 continue;
10281 case 'w':
bf12938e 10282 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
10283 continue;
10284 }
10285 break;
10286
10287 case '(':
10288 case ')':
10289 if (*s++ == c)
10290 continue;
10291 break;
10292
10293 case 'v':
10294 case 'w':
10295 if (s[0] != '$')
10296 {
10297 if (c == 'v')
bf12938e 10298 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 10299 else
bf12938e 10300 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
10301 ++args;
10302 continue;
10303 }
10304 /* Fall through. */
10305 case 'x':
10306 case 'y':
10307 case 'z':
10308 case 'Z':
10309 case '0':
10310 case 'S':
10311 case 'R':
10312 case 'X':
10313 case 'Y':
707bfff6
TS
10314 s_reset = s;
10315 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
252b5132 10316 {
707bfff6 10317 if (c == 'v' || c == 'w')
85b51719 10318 {
707bfff6 10319 if (c == 'v')
a9e24354 10320 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
707bfff6 10321 else
a9e24354 10322 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
707bfff6
TS
10323 ++args;
10324 continue;
85b51719 10325 }
707bfff6 10326 break;
252b5132
RH
10327 }
10328
10329 if (*s == ' ')
10330 ++s;
10331 if (args[1] != *s)
10332 {
10333 if (c == 'v' || c == 'w')
10334 {
10335 regno = mips16_to_32_reg_map[lastregno];
10336 s = s_reset;
f9419b05 10337 ++args;
252b5132
RH
10338 }
10339 }
10340
10341 switch (c)
10342 {
10343 case 'x':
10344 case 'y':
10345 case 'z':
10346 case 'v':
10347 case 'w':
10348 case 'Z':
10349 regno = mips32_to_16_reg_map[regno];
10350 break;
10351
10352 case '0':
10353 if (regno != 0)
10354 regno = ILLEGAL_REG;
10355 break;
10356
10357 case 'S':
10358 if (regno != SP)
10359 regno = ILLEGAL_REG;
10360 break;
10361
10362 case 'R':
10363 if (regno != RA)
10364 regno = ILLEGAL_REG;
10365 break;
10366
10367 case 'X':
10368 case 'Y':
741fe287
MR
10369 if (regno == AT && mips_opts.at)
10370 {
10371 if (mips_opts.at == ATREG)
10372 as_warn (_("used $at without \".set noat\""));
10373 else
10374 as_warn (_("used $%u with \".set at=$%u\""),
10375 regno, mips_opts.at);
10376 }
252b5132
RH
10377 break;
10378
10379 default:
10380 internalError ();
10381 }
10382
10383 if (regno == ILLEGAL_REG)
10384 break;
10385
10386 switch (c)
10387 {
10388 case 'x':
10389 case 'v':
bf12938e 10390 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
10391 break;
10392 case 'y':
10393 case 'w':
bf12938e 10394 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
10395 break;
10396 case 'z':
bf12938e 10397 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
10398 break;
10399 case 'Z':
bf12938e 10400 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
10401 case '0':
10402 case 'S':
10403 case 'R':
10404 break;
10405 case 'X':
bf12938e 10406 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
10407 break;
10408 case 'Y':
10409 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 10410 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
10411 break;
10412 default:
10413 internalError ();
10414 }
10415
10416 lastregno = regno;
10417 continue;
10418
10419 case 'P':
10420 if (strncmp (s, "$pc", 3) == 0)
10421 {
10422 s += 3;
10423 continue;
10424 }
10425 break;
10426
252b5132
RH
10427 case '5':
10428 case 'H':
10429 case 'W':
10430 case 'D':
10431 case 'j':
252b5132
RH
10432 case 'V':
10433 case 'C':
10434 case 'U':
10435 case 'k':
10436 case 'K':
d6f16593
MR
10437 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
10438 if (i > 0)
252b5132 10439 {
d6f16593 10440 if (imm_expr.X_op != O_constant)
252b5132 10441 {
b34976b6 10442 mips16_ext = TRUE;
b34976b6 10443 ip->use_extend = TRUE;
252b5132 10444 ip->extend = 0;
252b5132 10445 }
d6f16593
MR
10446 else
10447 {
10448 /* We need to relax this instruction. */
10449 *offset_reloc = *imm_reloc;
10450 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10451 }
10452 s = expr_end;
10453 continue;
252b5132 10454 }
d6f16593
MR
10455 *imm_reloc = BFD_RELOC_UNUSED;
10456 /* Fall through. */
10457 case '<':
10458 case '>':
10459 case '[':
10460 case ']':
10461 case '4':
10462 case '8':
10463 my_getExpression (&imm_expr, s);
252b5132
RH
10464 if (imm_expr.X_op == O_register)
10465 {
10466 /* What we thought was an expression turned out to
10467 be a register. */
10468
10469 if (s[0] == '(' && args[1] == '(')
10470 {
10471 /* It looks like the expression was omitted
10472 before a register indirection, which means
10473 that the expression is implicitly zero. We
10474 still set up imm_expr, so that we handle
10475 explicit extensions correctly. */
10476 imm_expr.X_op = O_constant;
10477 imm_expr.X_add_number = 0;
f6688943 10478 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
10479 continue;
10480 }
10481
10482 break;
10483 }
10484
10485 /* We need to relax this instruction. */
f6688943 10486 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
10487 s = expr_end;
10488 continue;
10489
10490 case 'p':
10491 case 'q':
10492 case 'A':
10493 case 'B':
10494 case 'E':
10495 /* We use offset_reloc rather than imm_reloc for the PC
10496 relative operands. This lets macros with both
10497 immediate and address operands work correctly. */
10498 my_getExpression (&offset_expr, s);
10499
10500 if (offset_expr.X_op == O_register)
10501 break;
10502
10503 /* We need to relax this instruction. */
f6688943 10504 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
10505 s = expr_end;
10506 continue;
10507
10508 case '6': /* break code */
10509 my_getExpression (&imm_expr, s);
10510 check_absolute_expr (ip, &imm_expr);
10511 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
10512 as_warn (_("Invalid value for `%s' (%lu)"),
10513 ip->insn_mo->name,
10514 (unsigned long) imm_expr.X_add_number);
10515 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
10516 imm_expr.X_op = O_absent;
10517 s = expr_end;
10518 continue;
10519
10520 case 'a': /* 26 bit address */
10521 my_getExpression (&offset_expr, s);
10522 s = expr_end;
f6688943 10523 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
10524 ip->insn_opcode <<= 16;
10525 continue;
10526
10527 case 'l': /* register list for entry macro */
10528 case 'L': /* register list for exit macro */
10529 {
10530 int mask;
10531
10532 if (c == 'l')
10533 mask = 0;
10534 else
10535 mask = 7 << 3;
10536 while (*s != '\0')
10537 {
707bfff6 10538 unsigned int freg, reg1, reg2;
252b5132
RH
10539
10540 while (*s == ' ' || *s == ',')
10541 ++s;
707bfff6 10542 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
252b5132 10543 freg = 0;
707bfff6
TS
10544 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
10545 freg = 1;
252b5132
RH
10546 else
10547 {
707bfff6
TS
10548 as_bad (_("can't parse register list"));
10549 break;
252b5132
RH
10550 }
10551 if (*s == ' ')
10552 ++s;
10553 if (*s != '-')
10554 reg2 = reg1;
10555 else
10556 {
10557 ++s;
707bfff6
TS
10558 if (!reg_lookup (&s, freg ? RTYPE_FPU
10559 : (RTYPE_GP | RTYPE_NUM), &reg2))
252b5132 10560 {
707bfff6
TS
10561 as_bad (_("invalid register list"));
10562 break;
252b5132
RH
10563 }
10564 }
10565 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
10566 {
10567 mask &= ~ (7 << 3);
10568 mask |= 5 << 3;
10569 }
10570 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
10571 {
10572 mask &= ~ (7 << 3);
10573 mask |= 6 << 3;
10574 }
10575 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
10576 mask |= (reg2 - 3) << 3;
10577 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
10578 mask |= (reg2 - 15) << 1;
f9419b05 10579 else if (reg1 == RA && reg2 == RA)
252b5132
RH
10580 mask |= 1;
10581 else
10582 {
10583 as_bad (_("invalid register list"));
10584 break;
10585 }
10586 }
10587 /* The mask is filled in in the opcode table for the
10588 benefit of the disassembler. We remove it before
10589 applying the actual mask. */
10590 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10591 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10592 }
10593 continue;
10594
0499d65b
TS
10595 case 'm': /* Register list for save insn. */
10596 case 'M': /* Register list for restore insn. */
10597 {
10598 int opcode = 0;
10599 int framesz = 0, seen_framesz = 0;
91d6fa6a 10600 int nargs = 0, statics = 0, sregs = 0;
0499d65b
TS
10601
10602 while (*s != '\0')
10603 {
10604 unsigned int reg1, reg2;
10605
10606 SKIP_SPACE_TABS (s);
10607 while (*s == ',')
10608 ++s;
10609 SKIP_SPACE_TABS (s);
10610
10611 my_getExpression (&imm_expr, s);
10612 if (imm_expr.X_op == O_constant)
10613 {
10614 /* Handle the frame size. */
10615 if (seen_framesz)
10616 {
10617 as_bad (_("more than one frame size in list"));
10618 break;
10619 }
10620 seen_framesz = 1;
10621 framesz = imm_expr.X_add_number;
10622 imm_expr.X_op = O_absent;
10623 s = expr_end;
10624 continue;
10625 }
10626
707bfff6 10627 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
0499d65b
TS
10628 {
10629 as_bad (_("can't parse register list"));
10630 break;
10631 }
0499d65b 10632
707bfff6
TS
10633 while (*s == ' ')
10634 ++s;
10635
0499d65b
TS
10636 if (*s != '-')
10637 reg2 = reg1;
10638 else
10639 {
10640 ++s;
707bfff6
TS
10641 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
10642 || reg2 < reg1)
0499d65b
TS
10643 {
10644 as_bad (_("can't parse register list"));
10645 break;
10646 }
0499d65b
TS
10647 }
10648
10649 while (reg1 <= reg2)
10650 {
10651 if (reg1 >= 4 && reg1 <= 7)
10652 {
3a93f742 10653 if (!seen_framesz)
0499d65b 10654 /* args $a0-$a3 */
91d6fa6a 10655 nargs |= 1 << (reg1 - 4);
0499d65b
TS
10656 else
10657 /* statics $a0-$a3 */
10658 statics |= 1 << (reg1 - 4);
10659 }
10660 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
10661 {
10662 /* $s0-$s8 */
10663 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
10664 }
10665 else if (reg1 == 31)
10666 {
10667 /* Add $ra to insn. */
10668 opcode |= 0x40;
10669 }
10670 else
10671 {
10672 as_bad (_("unexpected register in list"));
10673 break;
10674 }
10675 if (++reg1 == 24)
10676 reg1 = 30;
10677 }
10678 }
10679
10680 /* Encode args/statics combination. */
91d6fa6a 10681 if (nargs & statics)
0499d65b 10682 as_bad (_("arg/static registers overlap"));
91d6fa6a 10683 else if (nargs == 0xf)
0499d65b
TS
10684 /* All $a0-$a3 are args. */
10685 opcode |= MIPS16_ALL_ARGS << 16;
10686 else if (statics == 0xf)
10687 /* All $a0-$a3 are statics. */
10688 opcode |= MIPS16_ALL_STATICS << 16;
10689 else
10690 {
10691 int narg = 0, nstat = 0;
10692
10693 /* Count arg registers. */
91d6fa6a 10694 while (nargs & 0x1)
0499d65b 10695 {
91d6fa6a 10696 nargs >>= 1;
0499d65b
TS
10697 narg++;
10698 }
91d6fa6a 10699 if (nargs != 0)
0499d65b
TS
10700 as_bad (_("invalid arg register list"));
10701
10702 /* Count static registers. */
10703 while (statics & 0x8)
10704 {
10705 statics = (statics << 1) & 0xf;
10706 nstat++;
10707 }
10708 if (statics != 0)
10709 as_bad (_("invalid static register list"));
10710
10711 /* Encode args/statics. */
10712 opcode |= ((narg << 2) | nstat) << 16;
10713 }
10714
10715 /* Encode $s0/$s1. */
10716 if (sregs & (1 << 0)) /* $s0 */
10717 opcode |= 0x20;
10718 if (sregs & (1 << 1)) /* $s1 */
10719 opcode |= 0x10;
10720 sregs >>= 2;
10721
10722 if (sregs != 0)
10723 {
10724 /* Count regs $s2-$s8. */
10725 int nsreg = 0;
10726 while (sregs & 1)
10727 {
10728 sregs >>= 1;
10729 nsreg++;
10730 }
10731 if (sregs != 0)
10732 as_bad (_("invalid static register list"));
10733 /* Encode $s2-$s8. */
10734 opcode |= nsreg << 24;
10735 }
10736
10737 /* Encode frame size. */
10738 if (!seen_framesz)
10739 as_bad (_("missing frame size"));
10740 else if ((framesz & 7) != 0 || framesz < 0
10741 || framesz > 0xff * 8)
10742 as_bad (_("invalid frame size"));
10743 else if (framesz != 128 || (opcode >> 16) != 0)
10744 {
10745 framesz /= 8;
10746 opcode |= (((framesz & 0xf0) << 16)
10747 | (framesz & 0x0f));
10748 }
10749
10750 /* Finally build the instruction. */
10751 if ((opcode >> 16) != 0 || framesz == 0)
10752 {
10753 ip->use_extend = TRUE;
10754 ip->extend = opcode >> 16;
10755 }
10756 ip->insn_opcode |= opcode & 0x7f;
10757 }
10758 continue;
10759
252b5132
RH
10760 case 'e': /* extend code */
10761 my_getExpression (&imm_expr, s);
10762 check_absolute_expr (ip, &imm_expr);
10763 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10764 {
10765 as_warn (_("Invalid value for `%s' (%lu)"),
10766 ip->insn_mo->name,
10767 (unsigned long) imm_expr.X_add_number);
10768 imm_expr.X_add_number &= 0x7ff;
10769 }
10770 ip->insn_opcode |= imm_expr.X_add_number;
10771 imm_expr.X_op = O_absent;
10772 s = expr_end;
10773 continue;
10774
10775 default:
10776 internalError ();
10777 }
10778 break;
10779 }
10780
10781 /* Args don't match. */
10782 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10783 strcmp (insn->name, insn[1].name) == 0)
10784 {
10785 ++insn;
10786 s = argsstart;
10787 continue;
10788 }
10789
10790 insn_error = _("illegal operands");
10791
10792 return;
10793 }
10794}
10795
10796/* This structure holds information we know about a mips16 immediate
10797 argument type. */
10798
e972090a
NC
10799struct mips16_immed_operand
10800{
252b5132
RH
10801 /* The type code used in the argument string in the opcode table. */
10802 int type;
10803 /* The number of bits in the short form of the opcode. */
10804 int nbits;
10805 /* The number of bits in the extended form of the opcode. */
10806 int extbits;
10807 /* The amount by which the short form is shifted when it is used;
10808 for example, the sw instruction has a shift count of 2. */
10809 int shift;
10810 /* The amount by which the short form is shifted when it is stored
10811 into the instruction code. */
10812 int op_shift;
10813 /* Non-zero if the short form is unsigned. */
10814 int unsp;
10815 /* Non-zero if the extended form is unsigned. */
10816 int extu;
10817 /* Non-zero if the value is PC relative. */
10818 int pcrel;
10819};
10820
10821/* The mips16 immediate operand types. */
10822
10823static const struct mips16_immed_operand mips16_immed_operands[] =
10824{
10825 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10826 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10827 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10828 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10829 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10830 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10831 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10832 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10833 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10834 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10835 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10836 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10837 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10838 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10839 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10840 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10841 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10842 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10843 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10844 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10845 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10846};
10847
10848#define MIPS16_NUM_IMMED \
10849 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10850
10851/* Handle a mips16 instruction with an immediate value. This or's the
10852 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10853 whether an extended value is needed; if one is needed, it sets
10854 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10855 If SMALL is true, an unextended opcode was explicitly requested.
10856 If EXT is true, an extended opcode was explicitly requested. If
10857 WARN is true, warn if EXT does not match reality. */
10858
10859static void
17a2f251
TS
10860mips16_immed (char *file, unsigned int line, int type, offsetT val,
10861 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10862 unsigned long *insn, bfd_boolean *use_extend,
10863 unsigned short *extend)
252b5132 10864{
3994f87e 10865 const struct mips16_immed_operand *op;
252b5132 10866 int mintiny, maxtiny;
b34976b6 10867 bfd_boolean needext;
252b5132
RH
10868
10869 op = mips16_immed_operands;
10870 while (op->type != type)
10871 {
10872 ++op;
9c2799c2 10873 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
10874 }
10875
10876 if (op->unsp)
10877 {
10878 if (type == '<' || type == '>' || type == '[' || type == ']')
10879 {
10880 mintiny = 1;
10881 maxtiny = 1 << op->nbits;
10882 }
10883 else
10884 {
10885 mintiny = 0;
10886 maxtiny = (1 << op->nbits) - 1;
10887 }
10888 }
10889 else
10890 {
10891 mintiny = - (1 << (op->nbits - 1));
10892 maxtiny = (1 << (op->nbits - 1)) - 1;
10893 }
10894
10895 /* Branch offsets have an implicit 0 in the lowest bit. */
10896 if (type == 'p' || type == 'q')
10897 val /= 2;
10898
10899 if ((val & ((1 << op->shift) - 1)) != 0
10900 || val < (mintiny << op->shift)
10901 || val > (maxtiny << op->shift))
b34976b6 10902 needext = TRUE;
252b5132 10903 else
b34976b6 10904 needext = FALSE;
252b5132
RH
10905
10906 if (warn && ext && ! needext)
beae10d5
KH
10907 as_warn_where (file, line,
10908 _("extended operand requested but not required"));
252b5132
RH
10909 if (small && needext)
10910 as_bad_where (file, line, _("invalid unextended operand value"));
10911
10912 if (small || (! ext && ! needext))
10913 {
10914 int insnval;
10915
b34976b6 10916 *use_extend = FALSE;
252b5132
RH
10917 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10918 insnval <<= op->op_shift;
10919 *insn |= insnval;
10920 }
10921 else
10922 {
10923 long minext, maxext;
10924 int extval;
10925
10926 if (op->extu)
10927 {
10928 minext = 0;
10929 maxext = (1 << op->extbits) - 1;
10930 }
10931 else
10932 {
10933 minext = - (1 << (op->extbits - 1));
10934 maxext = (1 << (op->extbits - 1)) - 1;
10935 }
10936 if (val < minext || val > maxext)
10937 as_bad_where (file, line,
10938 _("operand value out of range for instruction"));
10939
b34976b6 10940 *use_extend = TRUE;
252b5132
RH
10941 if (op->extbits == 16)
10942 {
10943 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10944 val &= 0x1f;
10945 }
10946 else if (op->extbits == 15)
10947 {
10948 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10949 val &= 0xf;
10950 }
10951 else
10952 {
10953 extval = ((val & 0x1f) << 6) | (val & 0x20);
10954 val = 0;
10955 }
10956
10957 *extend = (unsigned short) extval;
10958 *insn |= val;
10959 }
10960}
10961\f
d6f16593 10962struct percent_op_match
ad8d3bb3 10963{
5e0116d5
RS
10964 const char *str;
10965 bfd_reloc_code_real_type reloc;
d6f16593
MR
10966};
10967
10968static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 10969{
5e0116d5 10970 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 10971#ifdef OBJ_ELF
5e0116d5
RS
10972 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10973 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10974 {"%call16", BFD_RELOC_MIPS_CALL16},
10975 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10976 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10977 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10978 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10979 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10980 {"%got", BFD_RELOC_MIPS_GOT16},
10981 {"%gp_rel", BFD_RELOC_GPREL16},
10982 {"%half", BFD_RELOC_16},
10983 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10984 {"%higher", BFD_RELOC_MIPS_HIGHER},
10985 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
10986 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
10987 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
10988 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
10989 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
10990 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
10991 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
10992 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
ad8d3bb3 10993#endif
5e0116d5 10994 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
10995};
10996
d6f16593
MR
10997static const struct percent_op_match mips16_percent_op[] =
10998{
10999 {"%lo", BFD_RELOC_MIPS16_LO16},
11000 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
11001 {"%got", BFD_RELOC_MIPS16_GOT16},
11002 {"%call16", BFD_RELOC_MIPS16_CALL16},
d6f16593
MR
11003 {"%hi", BFD_RELOC_MIPS16_HI16_S}
11004};
11005
252b5132 11006
5e0116d5
RS
11007/* Return true if *STR points to a relocation operator. When returning true,
11008 move *STR over the operator and store its relocation code in *RELOC.
11009 Leave both *STR and *RELOC alone when returning false. */
11010
11011static bfd_boolean
17a2f251 11012parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 11013{
d6f16593
MR
11014 const struct percent_op_match *percent_op;
11015 size_t limit, i;
11016
11017 if (mips_opts.mips16)
11018 {
11019 percent_op = mips16_percent_op;
11020 limit = ARRAY_SIZE (mips16_percent_op);
11021 }
11022 else
11023 {
11024 percent_op = mips_percent_op;
11025 limit = ARRAY_SIZE (mips_percent_op);
11026 }
76b3015f 11027
d6f16593 11028 for (i = 0; i < limit; i++)
5e0116d5 11029 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 11030 {
3f98094e
DJ
11031 int len = strlen (percent_op[i].str);
11032
11033 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
11034 continue;
11035
5e0116d5
RS
11036 *str += strlen (percent_op[i].str);
11037 *reloc = percent_op[i].reloc;
394f9b3a 11038
5e0116d5
RS
11039 /* Check whether the output BFD supports this relocation.
11040 If not, issue an error and fall back on something safe. */
11041 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 11042 {
20203fb9 11043 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 11044 percent_op[i].str);
01a3f561 11045 *reloc = BFD_RELOC_UNUSED;
394f9b3a 11046 }
5e0116d5 11047 return TRUE;
394f9b3a 11048 }
5e0116d5 11049 return FALSE;
394f9b3a 11050}
ad8d3bb3 11051
ad8d3bb3 11052
5e0116d5
RS
11053/* Parse string STR as a 16-bit relocatable operand. Store the
11054 expression in *EP and the relocations in the array starting
11055 at RELOC. Return the number of relocation operators used.
ad8d3bb3 11056
01a3f561 11057 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 11058
5e0116d5 11059static size_t
17a2f251
TS
11060my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
11061 char *str)
ad8d3bb3 11062{
5e0116d5
RS
11063 bfd_reloc_code_real_type reversed_reloc[3];
11064 size_t reloc_index, i;
09b8f35a
RS
11065 int crux_depth, str_depth;
11066 char *crux;
5e0116d5
RS
11067
11068 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
11069 in REVERSED_RELOC. End the loop with CRUX pointing to the start
11070 of the main expression and with CRUX_DEPTH containing the number
11071 of open brackets at that point. */
11072 reloc_index = -1;
11073 str_depth = 0;
11074 do
fb1b3232 11075 {
09b8f35a
RS
11076 reloc_index++;
11077 crux = str;
11078 crux_depth = str_depth;
11079
11080 /* Skip over whitespace and brackets, keeping count of the number
11081 of brackets. */
11082 while (*str == ' ' || *str == '\t' || *str == '(')
11083 if (*str++ == '(')
11084 str_depth++;
5e0116d5 11085 }
09b8f35a
RS
11086 while (*str == '%'
11087 && reloc_index < (HAVE_NEWABI ? 3 : 1)
11088 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 11089
09b8f35a 11090 my_getExpression (ep, crux);
5e0116d5 11091 str = expr_end;
394f9b3a 11092
5e0116d5 11093 /* Match every open bracket. */
09b8f35a 11094 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 11095 if (*str++ == ')')
09b8f35a 11096 crux_depth--;
394f9b3a 11097
09b8f35a 11098 if (crux_depth > 0)
20203fb9 11099 as_bad (_("unclosed '('"));
394f9b3a 11100
5e0116d5 11101 expr_end = str;
252b5132 11102
01a3f561 11103 if (reloc_index != 0)
64bdfcaf
RS
11104 {
11105 prev_reloc_op_frag = frag_now;
11106 for (i = 0; i < reloc_index; i++)
11107 reloc[i] = reversed_reloc[reloc_index - 1 - i];
11108 }
fb1b3232 11109
5e0116d5 11110 return reloc_index;
252b5132
RH
11111}
11112
11113static void
17a2f251 11114my_getExpression (expressionS *ep, char *str)
252b5132
RH
11115{
11116 char *save_in;
98aa84af 11117 valueT val;
252b5132
RH
11118
11119 save_in = input_line_pointer;
11120 input_line_pointer = str;
11121 expression (ep);
11122 expr_end = input_line_pointer;
11123 input_line_pointer = save_in;
11124
11125 /* If we are in mips16 mode, and this is an expression based on `.',
11126 then we bump the value of the symbol by 1 since that is how other
11127 text symbols are handled. We don't bother to handle complex
11128 expressions, just `.' plus or minus a constant. */
11129 if (mips_opts.mips16
11130 && ep->X_op == O_symbol
11131 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
11132 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
11133 && symbol_get_frag (ep->X_add_symbol) == frag_now
11134 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
11135 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
11136 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
11137}
11138
252b5132 11139char *
17a2f251 11140md_atof (int type, char *litP, int *sizeP)
252b5132 11141{
499ac353 11142 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
11143}
11144
11145void
17a2f251 11146md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
11147{
11148 if (target_big_endian)
11149 number_to_chars_bigendian (buf, val, n);
11150 else
11151 number_to_chars_littleendian (buf, val, n);
11152}
11153\f
ae948b86 11154#ifdef OBJ_ELF
e013f690
TS
11155static int support_64bit_objects(void)
11156{
11157 const char **list, **l;
aa3d8fdf 11158 int yes;
e013f690
TS
11159
11160 list = bfd_target_list ();
11161 for (l = list; *l != NULL; l++)
11162#ifdef TE_TMIPS
11163 /* This is traditional mips */
11164 if (strcmp (*l, "elf64-tradbigmips") == 0
11165 || strcmp (*l, "elf64-tradlittlemips") == 0)
11166#else
11167 if (strcmp (*l, "elf64-bigmips") == 0
11168 || strcmp (*l, "elf64-littlemips") == 0)
11169#endif
11170 break;
aa3d8fdf 11171 yes = (*l != NULL);
e013f690 11172 free (list);
aa3d8fdf 11173 return yes;
e013f690 11174}
ae948b86 11175#endif /* OBJ_ELF */
e013f690 11176
78849248 11177const char *md_shortopts = "O::g::G:";
252b5132 11178
23fce1e3
NC
11179enum options
11180 {
11181 OPTION_MARCH = OPTION_MD_BASE,
11182 OPTION_MTUNE,
11183 OPTION_MIPS1,
11184 OPTION_MIPS2,
11185 OPTION_MIPS3,
11186 OPTION_MIPS4,
11187 OPTION_MIPS5,
11188 OPTION_MIPS32,
11189 OPTION_MIPS64,
11190 OPTION_MIPS32R2,
11191 OPTION_MIPS64R2,
11192 OPTION_MIPS16,
11193 OPTION_NO_MIPS16,
11194 OPTION_MIPS3D,
11195 OPTION_NO_MIPS3D,
11196 OPTION_MDMX,
11197 OPTION_NO_MDMX,
11198 OPTION_DSP,
11199 OPTION_NO_DSP,
11200 OPTION_MT,
11201 OPTION_NO_MT,
11202 OPTION_SMARTMIPS,
11203 OPTION_NO_SMARTMIPS,
11204 OPTION_DSPR2,
11205 OPTION_NO_DSPR2,
11206 OPTION_COMPAT_ARCH_BASE,
11207 OPTION_M4650,
11208 OPTION_NO_M4650,
11209 OPTION_M4010,
11210 OPTION_NO_M4010,
11211 OPTION_M4100,
11212 OPTION_NO_M4100,
11213 OPTION_M3900,
11214 OPTION_NO_M3900,
11215 OPTION_M7000_HILO_FIX,
6a32d874
CM
11216 OPTION_MNO_7000_HILO_FIX,
11217 OPTION_FIX_24K,
11218 OPTION_NO_FIX_24K,
23fce1e3
NC
11219 OPTION_FIX_VR4120,
11220 OPTION_NO_FIX_VR4120,
11221 OPTION_FIX_VR4130,
11222 OPTION_NO_FIX_VR4130,
11223 OPTION_TRAP,
11224 OPTION_BREAK,
11225 OPTION_EB,
11226 OPTION_EL,
11227 OPTION_FP32,
11228 OPTION_GP32,
11229 OPTION_CONSTRUCT_FLOATS,
11230 OPTION_NO_CONSTRUCT_FLOATS,
11231 OPTION_FP64,
11232 OPTION_GP64,
11233 OPTION_RELAX_BRANCH,
11234 OPTION_NO_RELAX_BRANCH,
11235 OPTION_MSHARED,
11236 OPTION_MNO_SHARED,
11237 OPTION_MSYM32,
11238 OPTION_MNO_SYM32,
11239 OPTION_SOFT_FLOAT,
11240 OPTION_HARD_FLOAT,
11241 OPTION_SINGLE_FLOAT,
11242 OPTION_DOUBLE_FLOAT,
11243 OPTION_32,
11244#ifdef OBJ_ELF
11245 OPTION_CALL_SHARED,
11246 OPTION_CALL_NONPIC,
11247 OPTION_NON_SHARED,
11248 OPTION_XGOT,
11249 OPTION_MABI,
11250 OPTION_N32,
11251 OPTION_64,
11252 OPTION_MDEBUG,
11253 OPTION_NO_MDEBUG,
11254 OPTION_PDR,
11255 OPTION_NO_PDR,
11256 OPTION_MVXWORKS_PIC,
11257#endif /* OBJ_ELF */
11258 OPTION_END_OF_ENUM
11259 };
11260
e972090a
NC
11261struct option md_longopts[] =
11262{
f9b4148d 11263 /* Options which specify architecture. */
f9b4148d 11264 {"march", required_argument, NULL, OPTION_MARCH},
f9b4148d 11265 {"mtune", required_argument, NULL, OPTION_MTUNE},
252b5132
RH
11266 {"mips0", no_argument, NULL, OPTION_MIPS1},
11267 {"mips1", no_argument, NULL, OPTION_MIPS1},
252b5132 11268 {"mips2", no_argument, NULL, OPTION_MIPS2},
252b5132 11269 {"mips3", no_argument, NULL, OPTION_MIPS3},
252b5132 11270 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86 11271 {"mips5", no_argument, NULL, OPTION_MIPS5},
ae948b86 11272 {"mips32", no_argument, NULL, OPTION_MIPS32},
ae948b86 11273 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d 11274 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13 11275 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
11276
11277 /* Options which specify Application Specific Extensions (ASEs). */
f9b4148d 11278 {"mips16", no_argument, NULL, OPTION_MIPS16},
f9b4148d 11279 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
f9b4148d 11280 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
f9b4148d 11281 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
f9b4148d 11282 {"mdmx", no_argument, NULL, OPTION_MDMX},
f9b4148d 11283 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
74cd071d 11284 {"mdsp", no_argument, NULL, OPTION_DSP},
74cd071d 11285 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
ef2e4d86 11286 {"mmt", no_argument, NULL, OPTION_MT},
ef2e4d86 11287 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
e16bfa71 11288 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
e16bfa71 11289 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
8b082fb1 11290 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
8b082fb1 11291 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
f9b4148d
CD
11292
11293 /* Old-style architecture options. Don't add more of these. */
f9b4148d 11294 {"m4650", no_argument, NULL, OPTION_M4650},
f9b4148d 11295 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
f9b4148d 11296 {"m4010", no_argument, NULL, OPTION_M4010},
f9b4148d 11297 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
f9b4148d 11298 {"m4100", no_argument, NULL, OPTION_M4100},
f9b4148d 11299 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
f9b4148d 11300 {"m3900", no_argument, NULL, OPTION_M3900},
f9b4148d
CD
11301 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
11302
11303 /* Options which enable bug fixes. */
f9b4148d 11304 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
f9b4148d
CD
11305 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
11306 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
d766e8ec
RS
11307 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
11308 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
7d8e00cf
RS
11309 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
11310 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
6a32d874
CM
11311 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
11312 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
f9b4148d
CD
11313
11314 /* Miscellaneous options. */
252b5132
RH
11315 {"trap", no_argument, NULL, OPTION_TRAP},
11316 {"no-break", no_argument, NULL, OPTION_TRAP},
252b5132
RH
11317 {"break", no_argument, NULL, OPTION_BREAK},
11318 {"no-trap", no_argument, NULL, OPTION_BREAK},
252b5132 11319 {"EB", no_argument, NULL, OPTION_EB},
252b5132 11320 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 11321 {"mfp32", no_argument, NULL, OPTION_FP32},
c97ef257 11322 {"mgp32", no_argument, NULL, OPTION_GP32},
119d663a 11323 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
119d663a 11324 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
316f5878 11325 {"mfp64", no_argument, NULL, OPTION_FP64},
ae948b86 11326 {"mgp64", no_argument, NULL, OPTION_GP64},
4a6a3df4
AO
11327 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
11328 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
aa6975fb
ILT
11329 {"mshared", no_argument, NULL, OPTION_MSHARED},
11330 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
aed1a261
RS
11331 {"msym32", no_argument, NULL, OPTION_MSYM32},
11332 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
037b32b9
AN
11333 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
11334 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
037b32b9
AN
11335 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
11336 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
23fce1e3
NC
11337
11338 /* Strictly speaking this next option is ELF specific,
11339 but we allow it for other ports as well in order to
11340 make testing easier. */
11341 {"32", no_argument, NULL, OPTION_32},
037b32b9 11342
f9b4148d 11343 /* ELF-specific options. */
156c2f8b 11344#ifdef OBJ_ELF
156c2f8b
NC
11345 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
11346 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
861fb55a 11347 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
156c2f8b
NC
11348 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
11349 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86 11350 {"mabi", required_argument, NULL, OPTION_MABI},
e013f690 11351 {"n32", no_argument, NULL, OPTION_N32},
156c2f8b 11352 {"64", no_argument, NULL, OPTION_64},
ecb4347a 11353 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
ecb4347a 11354 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe 11355 {"mpdr", no_argument, NULL, OPTION_PDR},
dcd410fe 11356 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
0a44bf69 11357 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ae948b86 11358#endif /* OBJ_ELF */
f9b4148d 11359
252b5132
RH
11360 {NULL, no_argument, NULL, 0}
11361};
156c2f8b 11362size_t md_longopts_size = sizeof (md_longopts);
252b5132 11363
316f5878
RS
11364/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
11365 NEW_VALUE. Warn if another value was already specified. Note:
11366 we have to defer parsing the -march and -mtune arguments in order
11367 to handle 'from-abi' correctly, since the ABI might be specified
11368 in a later argument. */
11369
11370static void
17a2f251 11371mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
11372{
11373 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
11374 as_warn (_("A different %s was already specified, is now %s"),
11375 string_ptr == &mips_arch_string ? "-march" : "-mtune",
11376 new_value);
11377
11378 *string_ptr = new_value;
11379}
11380
252b5132 11381int
17a2f251 11382md_parse_option (int c, char *arg)
252b5132
RH
11383{
11384 switch (c)
11385 {
119d663a
NC
11386 case OPTION_CONSTRUCT_FLOATS:
11387 mips_disable_float_construction = 0;
11388 break;
bdaaa2e1 11389
119d663a
NC
11390 case OPTION_NO_CONSTRUCT_FLOATS:
11391 mips_disable_float_construction = 1;
11392 break;
bdaaa2e1 11393
252b5132
RH
11394 case OPTION_TRAP:
11395 mips_trap = 1;
11396 break;
11397
11398 case OPTION_BREAK:
11399 mips_trap = 0;
11400 break;
11401
11402 case OPTION_EB:
11403 target_big_endian = 1;
11404 break;
11405
11406 case OPTION_EL:
11407 target_big_endian = 0;
11408 break;
11409
11410 case 'O':
4ffff32f
TS
11411 if (arg == NULL)
11412 mips_optimize = 1;
11413 else if (arg[0] == '0')
11414 mips_optimize = 0;
11415 else if (arg[0] == '1')
252b5132
RH
11416 mips_optimize = 1;
11417 else
11418 mips_optimize = 2;
11419 break;
11420
11421 case 'g':
11422 if (arg == NULL)
11423 mips_debug = 2;
11424 else
11425 mips_debug = atoi (arg);
252b5132
RH
11426 break;
11427
11428 case OPTION_MIPS1:
316f5878 11429 file_mips_isa = ISA_MIPS1;
252b5132
RH
11430 break;
11431
11432 case OPTION_MIPS2:
316f5878 11433 file_mips_isa = ISA_MIPS2;
252b5132
RH
11434 break;
11435
11436 case OPTION_MIPS3:
316f5878 11437 file_mips_isa = ISA_MIPS3;
252b5132
RH
11438 break;
11439
11440 case OPTION_MIPS4:
316f5878 11441 file_mips_isa = ISA_MIPS4;
e7af610e
NC
11442 break;
11443
84ea6cf2 11444 case OPTION_MIPS5:
316f5878 11445 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
11446 break;
11447
e7af610e 11448 case OPTION_MIPS32:
316f5878 11449 file_mips_isa = ISA_MIPS32;
252b5132
RH
11450 break;
11451
af7ee8bf
CD
11452 case OPTION_MIPS32R2:
11453 file_mips_isa = ISA_MIPS32R2;
11454 break;
11455
5f74bc13
CD
11456 case OPTION_MIPS64R2:
11457 file_mips_isa = ISA_MIPS64R2;
11458 break;
11459
84ea6cf2 11460 case OPTION_MIPS64:
316f5878 11461 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
11462 break;
11463
ec68c924 11464 case OPTION_MTUNE:
316f5878
RS
11465 mips_set_option_string (&mips_tune_string, arg);
11466 break;
ec68c924 11467
316f5878
RS
11468 case OPTION_MARCH:
11469 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
11470 break;
11471
11472 case OPTION_M4650:
316f5878
RS
11473 mips_set_option_string (&mips_arch_string, "4650");
11474 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
11475 break;
11476
11477 case OPTION_NO_M4650:
11478 break;
11479
11480 case OPTION_M4010:
316f5878
RS
11481 mips_set_option_string (&mips_arch_string, "4010");
11482 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
11483 break;
11484
11485 case OPTION_NO_M4010:
11486 break;
11487
11488 case OPTION_M4100:
316f5878
RS
11489 mips_set_option_string (&mips_arch_string, "4100");
11490 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
11491 break;
11492
11493 case OPTION_NO_M4100:
11494 break;
11495
252b5132 11496 case OPTION_M3900:
316f5878
RS
11497 mips_set_option_string (&mips_arch_string, "3900");
11498 mips_set_option_string (&mips_tune_string, "3900");
252b5132 11499 break;
bdaaa2e1 11500
252b5132
RH
11501 case OPTION_NO_M3900:
11502 break;
11503
deec1734
CD
11504 case OPTION_MDMX:
11505 mips_opts.ase_mdmx = 1;
11506 break;
11507
11508 case OPTION_NO_MDMX:
11509 mips_opts.ase_mdmx = 0;
11510 break;
11511
74cd071d
CF
11512 case OPTION_DSP:
11513 mips_opts.ase_dsp = 1;
8b082fb1 11514 mips_opts.ase_dspr2 = 0;
74cd071d
CF
11515 break;
11516
11517 case OPTION_NO_DSP:
8b082fb1
TS
11518 mips_opts.ase_dsp = 0;
11519 mips_opts.ase_dspr2 = 0;
11520 break;
11521
11522 case OPTION_DSPR2:
11523 mips_opts.ase_dspr2 = 1;
11524 mips_opts.ase_dsp = 1;
11525 break;
11526
11527 case OPTION_NO_DSPR2:
11528 mips_opts.ase_dspr2 = 0;
74cd071d
CF
11529 mips_opts.ase_dsp = 0;
11530 break;
11531
ef2e4d86
CF
11532 case OPTION_MT:
11533 mips_opts.ase_mt = 1;
11534 break;
11535
11536 case OPTION_NO_MT:
11537 mips_opts.ase_mt = 0;
11538 break;
11539
252b5132
RH
11540 case OPTION_MIPS16:
11541 mips_opts.mips16 = 1;
7d10b47d 11542 mips_no_prev_insn ();
252b5132
RH
11543 break;
11544
11545 case OPTION_NO_MIPS16:
11546 mips_opts.mips16 = 0;
7d10b47d 11547 mips_no_prev_insn ();
252b5132
RH
11548 break;
11549
1f25f5d3
CD
11550 case OPTION_MIPS3D:
11551 mips_opts.ase_mips3d = 1;
11552 break;
11553
11554 case OPTION_NO_MIPS3D:
11555 mips_opts.ase_mips3d = 0;
11556 break;
11557
e16bfa71
TS
11558 case OPTION_SMARTMIPS:
11559 mips_opts.ase_smartmips = 1;
11560 break;
11561
11562 case OPTION_NO_SMARTMIPS:
11563 mips_opts.ase_smartmips = 0;
11564 break;
11565
6a32d874
CM
11566 case OPTION_FIX_24K:
11567 mips_fix_24k = 1;
11568 break;
11569
11570 case OPTION_NO_FIX_24K:
11571 mips_fix_24k = 0;
11572 break;
11573
d766e8ec
RS
11574 case OPTION_FIX_VR4120:
11575 mips_fix_vr4120 = 1;
60b63b72
RS
11576 break;
11577
d766e8ec
RS
11578 case OPTION_NO_FIX_VR4120:
11579 mips_fix_vr4120 = 0;
60b63b72
RS
11580 break;
11581
7d8e00cf
RS
11582 case OPTION_FIX_VR4130:
11583 mips_fix_vr4130 = 1;
11584 break;
11585
11586 case OPTION_NO_FIX_VR4130:
11587 mips_fix_vr4130 = 0;
11588 break;
11589
4a6a3df4
AO
11590 case OPTION_RELAX_BRANCH:
11591 mips_relax_branch = 1;
11592 break;
11593
11594 case OPTION_NO_RELAX_BRANCH:
11595 mips_relax_branch = 0;
11596 break;
11597
aa6975fb
ILT
11598 case OPTION_MSHARED:
11599 mips_in_shared = TRUE;
11600 break;
11601
11602 case OPTION_MNO_SHARED:
11603 mips_in_shared = FALSE;
11604 break;
11605
aed1a261
RS
11606 case OPTION_MSYM32:
11607 mips_opts.sym32 = TRUE;
11608 break;
11609
11610 case OPTION_MNO_SYM32:
11611 mips_opts.sym32 = FALSE;
11612 break;
11613
0f074f60 11614#ifdef OBJ_ELF
252b5132
RH
11615 /* When generating ELF code, we permit -KPIC and -call_shared to
11616 select SVR4_PIC, and -non_shared to select no PIC. This is
11617 intended to be compatible with Irix 5. */
11618 case OPTION_CALL_SHARED:
f43abd2b 11619 if (!IS_ELF)
252b5132
RH
11620 {
11621 as_bad (_("-call_shared is supported only for ELF format"));
11622 return 0;
11623 }
11624 mips_pic = SVR4_PIC;
143d77c5 11625 mips_abicalls = TRUE;
252b5132
RH
11626 break;
11627
861fb55a
DJ
11628 case OPTION_CALL_NONPIC:
11629 if (!IS_ELF)
11630 {
11631 as_bad (_("-call_nonpic is supported only for ELF format"));
11632 return 0;
11633 }
11634 mips_pic = NO_PIC;
11635 mips_abicalls = TRUE;
11636 break;
11637
252b5132 11638 case OPTION_NON_SHARED:
f43abd2b 11639 if (!IS_ELF)
252b5132
RH
11640 {
11641 as_bad (_("-non_shared is supported only for ELF format"));
11642 return 0;
11643 }
11644 mips_pic = NO_PIC;
143d77c5 11645 mips_abicalls = FALSE;
252b5132
RH
11646 break;
11647
44075ae2
TS
11648 /* The -xgot option tells the assembler to use 32 bit offsets
11649 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
11650 compatibility. */
11651 case OPTION_XGOT:
11652 mips_big_got = 1;
11653 break;
0f074f60 11654#endif /* OBJ_ELF */
252b5132
RH
11655
11656 case 'G':
6caf9ef4
TS
11657 g_switch_value = atoi (arg);
11658 g_switch_seen = 1;
252b5132
RH
11659 break;
11660
34ba82a8
TS
11661 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
11662 and -mabi=64. */
252b5132 11663 case OPTION_32:
23fce1e3
NC
11664 if (IS_ELF)
11665 mips_abi = O32_ABI;
11666 /* We silently ignore -32 for non-ELF targets. This greatly
11667 simplifies the construction of the MIPS GAS test cases. */
252b5132
RH
11668 break;
11669
23fce1e3 11670#ifdef OBJ_ELF
e013f690 11671 case OPTION_N32:
f43abd2b 11672 if (!IS_ELF)
34ba82a8
TS
11673 {
11674 as_bad (_("-n32 is supported for ELF format only"));
11675 return 0;
11676 }
316f5878 11677 mips_abi = N32_ABI;
e013f690 11678 break;
252b5132 11679
e013f690 11680 case OPTION_64:
f43abd2b 11681 if (!IS_ELF)
34ba82a8
TS
11682 {
11683 as_bad (_("-64 is supported for ELF format only"));
11684 return 0;
11685 }
316f5878 11686 mips_abi = N64_ABI;
f43abd2b 11687 if (!support_64bit_objects())
e013f690 11688 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 11689 break;
ae948b86 11690#endif /* OBJ_ELF */
252b5132 11691
c97ef257 11692 case OPTION_GP32:
a325df1d 11693 file_mips_gp32 = 1;
c97ef257
AH
11694 break;
11695
11696 case OPTION_GP64:
a325df1d 11697 file_mips_gp32 = 0;
c97ef257 11698 break;
252b5132 11699
ca4e0257 11700 case OPTION_FP32:
a325df1d 11701 file_mips_fp32 = 1;
316f5878
RS
11702 break;
11703
11704 case OPTION_FP64:
11705 file_mips_fp32 = 0;
ca4e0257
RS
11706 break;
11707
037b32b9
AN
11708 case OPTION_SINGLE_FLOAT:
11709 file_mips_single_float = 1;
11710 break;
11711
11712 case OPTION_DOUBLE_FLOAT:
11713 file_mips_single_float = 0;
11714 break;
11715
11716 case OPTION_SOFT_FLOAT:
11717 file_mips_soft_float = 1;
11718 break;
11719
11720 case OPTION_HARD_FLOAT:
11721 file_mips_soft_float = 0;
11722 break;
11723
ae948b86 11724#ifdef OBJ_ELF
252b5132 11725 case OPTION_MABI:
f43abd2b 11726 if (!IS_ELF)
34ba82a8
TS
11727 {
11728 as_bad (_("-mabi is supported for ELF format only"));
11729 return 0;
11730 }
e013f690 11731 if (strcmp (arg, "32") == 0)
316f5878 11732 mips_abi = O32_ABI;
e013f690 11733 else if (strcmp (arg, "o64") == 0)
316f5878 11734 mips_abi = O64_ABI;
e013f690 11735 else if (strcmp (arg, "n32") == 0)
316f5878 11736 mips_abi = N32_ABI;
e013f690
TS
11737 else if (strcmp (arg, "64") == 0)
11738 {
316f5878 11739 mips_abi = N64_ABI;
e013f690
TS
11740 if (! support_64bit_objects())
11741 as_fatal (_("No compiled in support for 64 bit object file "
11742 "format"));
11743 }
11744 else if (strcmp (arg, "eabi") == 0)
316f5878 11745 mips_abi = EABI_ABI;
e013f690 11746 else
da0e507f
TS
11747 {
11748 as_fatal (_("invalid abi -mabi=%s"), arg);
11749 return 0;
11750 }
252b5132 11751 break;
e013f690 11752#endif /* OBJ_ELF */
252b5132 11753
6b76fefe 11754 case OPTION_M7000_HILO_FIX:
b34976b6 11755 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
11756 break;
11757
9ee72ff1 11758 case OPTION_MNO_7000_HILO_FIX:
b34976b6 11759 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
11760 break;
11761
ecb4347a
DJ
11762#ifdef OBJ_ELF
11763 case OPTION_MDEBUG:
b34976b6 11764 mips_flag_mdebug = TRUE;
ecb4347a
DJ
11765 break;
11766
11767 case OPTION_NO_MDEBUG:
b34976b6 11768 mips_flag_mdebug = FALSE;
ecb4347a 11769 break;
dcd410fe
RO
11770
11771 case OPTION_PDR:
11772 mips_flag_pdr = TRUE;
11773 break;
11774
11775 case OPTION_NO_PDR:
11776 mips_flag_pdr = FALSE;
11777 break;
0a44bf69
RS
11778
11779 case OPTION_MVXWORKS_PIC:
11780 mips_pic = VXWORKS_PIC;
11781 break;
ecb4347a
DJ
11782#endif /* OBJ_ELF */
11783
252b5132
RH
11784 default:
11785 return 0;
11786 }
11787
11788 return 1;
11789}
316f5878
RS
11790\f
11791/* Set up globals to generate code for the ISA or processor
11792 described by INFO. */
252b5132 11793
252b5132 11794static void
17a2f251 11795mips_set_architecture (const struct mips_cpu_info *info)
252b5132 11796{
316f5878 11797 if (info != 0)
252b5132 11798 {
fef14a42
TS
11799 file_mips_arch = info->cpu;
11800 mips_opts.arch = info->cpu;
316f5878 11801 mips_opts.isa = info->isa;
252b5132 11802 }
252b5132
RH
11803}
11804
252b5132 11805
316f5878 11806/* Likewise for tuning. */
252b5132 11807
316f5878 11808static void
17a2f251 11809mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
11810{
11811 if (info != 0)
fef14a42 11812 mips_tune = info->cpu;
316f5878 11813}
80cc45a5 11814
34ba82a8 11815
252b5132 11816void
17a2f251 11817mips_after_parse_args (void)
e9670677 11818{
fef14a42
TS
11819 const struct mips_cpu_info *arch_info = 0;
11820 const struct mips_cpu_info *tune_info = 0;
11821
e9670677 11822 /* GP relative stuff not working for PE */
6caf9ef4 11823 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 11824 {
6caf9ef4 11825 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
11826 as_bad (_("-G not supported in this configuration."));
11827 g_switch_value = 0;
11828 }
11829
cac012d6
AO
11830 if (mips_abi == NO_ABI)
11831 mips_abi = MIPS_DEFAULT_ABI;
11832
22923709
RS
11833 /* The following code determines the architecture and register size.
11834 Similar code was added to GCC 3.3 (see override_options() in
11835 config/mips/mips.c). The GAS and GCC code should be kept in sync
11836 as much as possible. */
e9670677 11837
316f5878 11838 if (mips_arch_string != 0)
fef14a42 11839 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 11840
316f5878 11841 if (file_mips_isa != ISA_UNKNOWN)
e9670677 11842 {
316f5878 11843 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 11844 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 11845 the -march selection (if any). */
fef14a42 11846 if (arch_info != 0)
e9670677 11847 {
316f5878
RS
11848 /* -march takes precedence over -mipsN, since it is more descriptive.
11849 There's no harm in specifying both as long as the ISA levels
11850 are the same. */
fef14a42 11851 if (file_mips_isa != arch_info->isa)
316f5878
RS
11852 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11853 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 11854 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 11855 }
316f5878 11856 else
fef14a42 11857 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
11858 }
11859
fef14a42
TS
11860 if (arch_info == 0)
11861 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
e9670677 11862
fef14a42 11863 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 11864 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
11865 arch_info->name);
11866
11867 mips_set_architecture (arch_info);
11868
11869 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
11870 if (mips_tune_string != 0)
11871 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 11872
fef14a42
TS
11873 if (tune_info == 0)
11874 mips_set_tune (arch_info);
11875 else
11876 mips_set_tune (tune_info);
e9670677 11877
316f5878 11878 if (file_mips_gp32 >= 0)
e9670677 11879 {
316f5878
RS
11880 /* The user specified the size of the integer registers. Make sure
11881 it agrees with the ABI and ISA. */
11882 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11883 as_bad (_("-mgp64 used with a 32-bit processor"));
11884 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11885 as_bad (_("-mgp32 used with a 64-bit ABI"));
11886 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11887 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
11888 }
11889 else
11890 {
316f5878
RS
11891 /* Infer the integer register size from the ABI and processor.
11892 Restrict ourselves to 32-bit registers if that's all the
11893 processor has, or if the ABI cannot handle 64-bit registers. */
11894 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11895 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
11896 }
11897
ad3fea08
TS
11898 switch (file_mips_fp32)
11899 {
11900 default:
11901 case -1:
11902 /* No user specified float register size.
11903 ??? GAS treats single-float processors as though they had 64-bit
11904 float registers (although it complains when double-precision
11905 instructions are used). As things stand, saying they have 32-bit
11906 registers would lead to spurious "register must be even" messages.
11907 So here we assume float registers are never smaller than the
11908 integer ones. */
11909 if (file_mips_gp32 == 0)
11910 /* 64-bit integer registers implies 64-bit float registers. */
11911 file_mips_fp32 = 0;
11912 else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
11913 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
11914 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
11915 file_mips_fp32 = 0;
11916 else
11917 /* 32-bit float registers. */
11918 file_mips_fp32 = 1;
11919 break;
11920
11921 /* The user specified the size of the float registers. Check if it
11922 agrees with the ABI and ISA. */
11923 case 0:
11924 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
11925 as_bad (_("-mfp64 used with a 32-bit fpu"));
11926 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
11927 && !ISA_HAS_MXHC1 (mips_opts.isa))
11928 as_warn (_("-mfp64 used with a 32-bit ABI"));
11929 break;
11930 case 1:
11931 if (ABI_NEEDS_64BIT_REGS (mips_abi))
11932 as_warn (_("-mfp32 used with a 64-bit ABI"));
11933 break;
11934 }
e9670677 11935
316f5878 11936 /* End of GCC-shared inference code. */
e9670677 11937
17a2f251
TS
11938 /* This flag is set when we have a 64-bit capable CPU but use only
11939 32-bit wide registers. Note that EABI does not use it. */
11940 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11941 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11942 || mips_abi == O32_ABI))
316f5878 11943 mips_32bitmode = 1;
e9670677
MR
11944
11945 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11946 as_bad (_("trap exception not supported at ISA 1"));
11947
e9670677
MR
11948 /* If the selected architecture includes support for ASEs, enable
11949 generation of code for them. */
a4672219 11950 if (mips_opts.mips16 == -1)
fef14a42 11951 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
ffdefa66 11952 if (mips_opts.ase_mips3d == -1)
65263ce3 11953 mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
ad3fea08
TS
11954 && file_mips_fp32 == 0) ? 1 : 0;
11955 if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
11956 as_bad (_("-mfp32 used with -mips3d"));
11957
ffdefa66 11958 if (mips_opts.ase_mdmx == -1)
65263ce3 11959 mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
ad3fea08
TS
11960 && file_mips_fp32 == 0) ? 1 : 0;
11961 if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
11962 as_bad (_("-mfp32 used with -mdmx"));
11963
11964 if (mips_opts.ase_smartmips == -1)
11965 mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
11966 if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
20203fb9
NC
11967 as_warn (_("%s ISA does not support SmartMIPS"),
11968 mips_cpu_info_from_isa (mips_opts.isa)->name);
ad3fea08 11969
74cd071d 11970 if (mips_opts.ase_dsp == -1)
ad3fea08
TS
11971 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
11972 if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
20203fb9
NC
11973 as_warn (_("%s ISA does not support DSP ASE"),
11974 mips_cpu_info_from_isa (mips_opts.isa)->name);
ad3fea08 11975
8b082fb1
TS
11976 if (mips_opts.ase_dspr2 == -1)
11977 {
11978 mips_opts.ase_dspr2 = (arch_info->flags & MIPS_CPU_ASE_DSPR2) ? 1 : 0;
11979 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
11980 }
11981 if (mips_opts.ase_dspr2 && !ISA_SUPPORTS_DSPR2_ASE)
20203fb9
NC
11982 as_warn (_("%s ISA does not support DSP R2 ASE"),
11983 mips_cpu_info_from_isa (mips_opts.isa)->name);
8b082fb1 11984
ef2e4d86 11985 if (mips_opts.ase_mt == -1)
ad3fea08
TS
11986 mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
11987 if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
20203fb9
NC
11988 as_warn (_("%s ISA does not support MT ASE"),
11989 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 11990
e9670677 11991 file_mips_isa = mips_opts.isa;
a4672219 11992 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
11993 file_ase_mips3d = mips_opts.ase_mips3d;
11994 file_ase_mdmx = mips_opts.ase_mdmx;
e16bfa71 11995 file_ase_smartmips = mips_opts.ase_smartmips;
74cd071d 11996 file_ase_dsp = mips_opts.ase_dsp;
8b082fb1 11997 file_ase_dspr2 = mips_opts.ase_dspr2;
ef2e4d86 11998 file_ase_mt = mips_opts.ase_mt;
e9670677
MR
11999 mips_opts.gp32 = file_mips_gp32;
12000 mips_opts.fp32 = file_mips_fp32;
037b32b9
AN
12001 mips_opts.soft_float = file_mips_soft_float;
12002 mips_opts.single_float = file_mips_single_float;
e9670677 12003
ecb4347a
DJ
12004 if (mips_flag_mdebug < 0)
12005 {
12006#ifdef OBJ_MAYBE_ECOFF
12007 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
12008 mips_flag_mdebug = 1;
12009 else
12010#endif /* OBJ_MAYBE_ECOFF */
12011 mips_flag_mdebug = 0;
12012 }
e9670677
MR
12013}
12014\f
12015void
17a2f251 12016mips_init_after_args (void)
252b5132
RH
12017{
12018 /* initialize opcodes */
12019 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 12020 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
12021}
12022
12023long
17a2f251 12024md_pcrel_from (fixS *fixP)
252b5132 12025{
a7ebbfdf
TS
12026 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
12027 switch (fixP->fx_r_type)
12028 {
12029 case BFD_RELOC_16_PCREL_S2:
12030 case BFD_RELOC_MIPS_JMP:
12031 /* Return the address of the delay slot. */
12032 return addr + 4;
12033 default:
58ea3d6a 12034 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
12035 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
12036 as_bad_where (fixP->fx_file, fixP->fx_line,
12037 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
12038 return addr;
12039 }
252b5132
RH
12040}
12041
252b5132
RH
12042/* This is called before the symbol table is processed. In order to
12043 work with gcc when using mips-tfile, we must keep all local labels.
12044 However, in other cases, we want to discard them. If we were
12045 called with -g, but we didn't see any debugging information, it may
12046 mean that gcc is smuggling debugging information through to
12047 mips-tfile, in which case we must generate all local labels. */
12048
12049void
17a2f251 12050mips_frob_file_before_adjust (void)
252b5132
RH
12051{
12052#ifndef NO_ECOFF_DEBUGGING
12053 if (ECOFF_DEBUGGING
12054 && mips_debug != 0
12055 && ! ecoff_debugging_seen)
12056 flag_keep_locals = 1;
12057#endif
12058}
12059
3b91255e 12060/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 12061 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
12062 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
12063 relocation operators.
12064
12065 For our purposes, a %lo() expression matches a %got() or %hi()
12066 expression if:
12067
12068 (a) it refers to the same symbol; and
12069 (b) the offset applied in the %lo() expression is no lower than
12070 the offset applied in the %got() or %hi().
12071
12072 (b) allows us to cope with code like:
12073
12074 lui $4,%hi(foo)
12075 lh $4,%lo(foo+2)($4)
12076
12077 ...which is legal on RELA targets, and has a well-defined behaviour
12078 if the user knows that adding 2 to "foo" will not induce a carry to
12079 the high 16 bits.
12080
12081 When several %lo()s match a particular %got() or %hi(), we use the
12082 following rules to distinguish them:
12083
12084 (1) %lo()s with smaller offsets are a better match than %lo()s with
12085 higher offsets.
12086
12087 (2) %lo()s with no matching %got() or %hi() are better than those
12088 that already have a matching %got() or %hi().
12089
12090 (3) later %lo()s are better than earlier %lo()s.
12091
12092 These rules are applied in order.
12093
12094 (1) means, among other things, that %lo()s with identical offsets are
12095 chosen if they exist.
12096
12097 (2) means that we won't associate several high-part relocations with
12098 the same low-part relocation unless there's no alternative. Having
12099 several high parts for the same low part is a GNU extension; this rule
12100 allows careful users to avoid it.
12101
12102 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
12103 with the last high-part relocation being at the front of the list.
12104 It therefore makes sense to choose the last matching low-part
12105 relocation, all other things being equal. It's also easier
12106 to code that way. */
252b5132
RH
12107
12108void
17a2f251 12109mips_frob_file (void)
252b5132
RH
12110{
12111 struct mips_hi_fixup *l;
35903be0 12112 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
12113
12114 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
12115 {
12116 segment_info_type *seginfo;
3b91255e
RS
12117 bfd_boolean matched_lo_p;
12118 fixS **hi_pos, **lo_pos, **pos;
252b5132 12119
9c2799c2 12120 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 12121
5919d012
RS
12122 /* If a GOT16 relocation turns out to be against a global symbol,
12123 there isn't supposed to be a matching LO. */
738e5348 12124 if (got16_reloc_p (l->fixp->fx_r_type)
5919d012
RS
12125 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
12126 continue;
12127
12128 /* Check quickly whether the next fixup happens to be a matching %lo. */
12129 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
12130 continue;
12131
252b5132 12132 seginfo = seg_info (l->seg);
252b5132 12133
3b91255e
RS
12134 /* Set HI_POS to the position of this relocation in the chain.
12135 Set LO_POS to the position of the chosen low-part relocation.
12136 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
12137 relocation that matches an immediately-preceding high-part
12138 relocation. */
12139 hi_pos = NULL;
12140 lo_pos = NULL;
12141 matched_lo_p = FALSE;
738e5348 12142 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 12143
3b91255e
RS
12144 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
12145 {
12146 if (*pos == l->fixp)
12147 hi_pos = pos;
12148
35903be0 12149 if ((*pos)->fx_r_type == looking_for_rtype
3b91255e
RS
12150 && (*pos)->fx_addsy == l->fixp->fx_addsy
12151 && (*pos)->fx_offset >= l->fixp->fx_offset
12152 && (lo_pos == NULL
12153 || (*pos)->fx_offset < (*lo_pos)->fx_offset
12154 || (!matched_lo_p
12155 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
12156 lo_pos = pos;
12157
12158 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
12159 && fixup_has_matching_lo_p (*pos));
12160 }
12161
12162 /* If we found a match, remove the high-part relocation from its
12163 current position and insert it before the low-part relocation.
12164 Make the offsets match so that fixup_has_matching_lo_p()
12165 will return true.
12166
12167 We don't warn about unmatched high-part relocations since some
12168 versions of gcc have been known to emit dead "lui ...%hi(...)"
12169 instructions. */
12170 if (lo_pos != NULL)
12171 {
12172 l->fixp->fx_offset = (*lo_pos)->fx_offset;
12173 if (l->fixp->fx_next != *lo_pos)
252b5132 12174 {
3b91255e
RS
12175 *hi_pos = l->fixp->fx_next;
12176 l->fixp->fx_next = *lo_pos;
12177 *lo_pos = l->fixp;
252b5132 12178 }
252b5132
RH
12179 }
12180 }
12181}
12182
3e722fb5 12183/* We may have combined relocations without symbols in the N32/N64 ABI.
f6688943 12184 We have to prevent gas from dropping them. */
252b5132 12185
252b5132 12186int
17a2f251 12187mips_force_relocation (fixS *fixp)
252b5132 12188{
ae6063d4 12189 if (generic_force_reloc (fixp))
252b5132
RH
12190 return 1;
12191
f6688943
TS
12192 if (HAVE_NEWABI
12193 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
12194 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
738e5348
RS
12195 || hi16_reloc_p (fixp->fx_r_type)
12196 || lo16_reloc_p (fixp->fx_r_type)))
f6688943
TS
12197 return 1;
12198
3e722fb5 12199 return 0;
252b5132
RH
12200}
12201
12202/* Apply a fixup to the object file. */
12203
94f592af 12204void
55cf6793 12205md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 12206{
874e8986 12207 bfd_byte *buf;
98aa84af 12208 long insn;
a7ebbfdf 12209 reloc_howto_type *howto;
252b5132 12210
a7ebbfdf
TS
12211 /* We ignore generic BFD relocations we don't know about. */
12212 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
12213 if (! howto)
12214 return;
65551fa4 12215
9c2799c2 12216 gas_assert (fixP->fx_size == 4
252b5132
RH
12217 || fixP->fx_r_type == BFD_RELOC_16
12218 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
12219 || fixP->fx_r_type == BFD_RELOC_CTOR
12220 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
252b5132 12221 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
741d6ea8
JM
12222 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
12223 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 12224
a7ebbfdf 12225 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132 12226
9c2799c2 12227 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2);
b1dca8ee
RS
12228
12229 /* Don't treat parts of a composite relocation as done. There are two
12230 reasons for this:
12231
12232 (1) The second and third parts will be against 0 (RSS_UNDEF) but
12233 should nevertheless be emitted if the first part is.
12234
12235 (2) In normal usage, composite relocations are never assembly-time
12236 constants. The easiest way of dealing with the pathological
12237 exceptions is to generate a relocation against STN_UNDEF and
12238 leave everything up to the linker. */
3994f87e 12239 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
12240 fixP->fx_done = 1;
12241
12242 switch (fixP->fx_r_type)
12243 {
3f98094e
DJ
12244 case BFD_RELOC_MIPS_TLS_GD:
12245 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
12246 case BFD_RELOC_MIPS_TLS_DTPREL32:
12247 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
12248 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
12249 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
12250 case BFD_RELOC_MIPS_TLS_GOTTPREL:
12251 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
12252 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
12253 S_SET_THREAD_LOCAL (fixP->fx_addsy);
12254 /* fall through */
12255
252b5132 12256 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
12257 case BFD_RELOC_MIPS_SHIFT5:
12258 case BFD_RELOC_MIPS_SHIFT6:
12259 case BFD_RELOC_MIPS_GOT_DISP:
12260 case BFD_RELOC_MIPS_GOT_PAGE:
12261 case BFD_RELOC_MIPS_GOT_OFST:
12262 case BFD_RELOC_MIPS_SUB:
12263 case BFD_RELOC_MIPS_INSERT_A:
12264 case BFD_RELOC_MIPS_INSERT_B:
12265 case BFD_RELOC_MIPS_DELETE:
12266 case BFD_RELOC_MIPS_HIGHEST:
12267 case BFD_RELOC_MIPS_HIGHER:
12268 case BFD_RELOC_MIPS_SCN_DISP:
12269 case BFD_RELOC_MIPS_REL16:
12270 case BFD_RELOC_MIPS_RELGOT:
12271 case BFD_RELOC_MIPS_JALR:
252b5132
RH
12272 case BFD_RELOC_HI16:
12273 case BFD_RELOC_HI16_S:
cdf6fd85 12274 case BFD_RELOC_GPREL16:
252b5132
RH
12275 case BFD_RELOC_MIPS_LITERAL:
12276 case BFD_RELOC_MIPS_CALL16:
12277 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 12278 case BFD_RELOC_GPREL32:
252b5132
RH
12279 case BFD_RELOC_MIPS_GOT_HI16:
12280 case BFD_RELOC_MIPS_GOT_LO16:
12281 case BFD_RELOC_MIPS_CALL_HI16:
12282 case BFD_RELOC_MIPS_CALL_LO16:
12283 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
12284 case BFD_RELOC_MIPS16_GOT16:
12285 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
12286 case BFD_RELOC_MIPS16_HI16:
12287 case BFD_RELOC_MIPS16_HI16_S:
252b5132 12288 case BFD_RELOC_MIPS16_JMP:
54f4ddb3 12289 /* Nothing needed to do. The value comes from the reloc entry. */
252b5132
RH
12290 break;
12291
252b5132
RH
12292 case BFD_RELOC_64:
12293 /* This is handled like BFD_RELOC_32, but we output a sign
12294 extended value if we are only 32 bits. */
3e722fb5 12295 if (fixP->fx_done)
252b5132
RH
12296 {
12297 if (8 <= sizeof (valueT))
2132e3a3 12298 md_number_to_chars ((char *) buf, *valP, 8);
252b5132
RH
12299 else
12300 {
a7ebbfdf 12301 valueT hiv;
252b5132 12302
a7ebbfdf 12303 if ((*valP & 0x80000000) != 0)
252b5132
RH
12304 hiv = 0xffffffff;
12305 else
12306 hiv = 0;
b215186b 12307 md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
a7ebbfdf 12308 *valP, 4);
b215186b 12309 md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
a7ebbfdf 12310 hiv, 4);
252b5132
RH
12311 }
12312 }
12313 break;
12314
056350c6 12315 case BFD_RELOC_RVA:
252b5132 12316 case BFD_RELOC_32:
252b5132
RH
12317 case BFD_RELOC_16:
12318 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
12319 value now. This can happen if we have a .word which is not
12320 resolved when it appears but is later defined. */
252b5132 12321 if (fixP->fx_done)
54f4ddb3 12322 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
252b5132
RH
12323 break;
12324
12325 case BFD_RELOC_LO16:
d6f16593 12326 case BFD_RELOC_MIPS16_LO16:
3e722fb5
CD
12327 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
12328 may be safe to remove, but if so it's not obvious. */
252b5132
RH
12329 /* When handling an embedded PIC switch statement, we can wind
12330 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
12331 if (fixP->fx_done)
12332 {
a7ebbfdf 12333 if (*valP + 0x8000 > 0xffff)
252b5132
RH
12334 as_bad_where (fixP->fx_file, fixP->fx_line,
12335 _("relocation overflow"));
252b5132
RH
12336 if (target_big_endian)
12337 buf += 2;
2132e3a3 12338 md_number_to_chars ((char *) buf, *valP, 2);
252b5132
RH
12339 }
12340 break;
12341
12342 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 12343 if ((*valP & 0x3) != 0)
cb56d3d3 12344 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 12345 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 12346
54f4ddb3
TS
12347 /* We need to save the bits in the instruction since fixup_segment()
12348 might be deleting the relocation entry (i.e., a branch within
12349 the current segment). */
a7ebbfdf 12350 if (! fixP->fx_done)
bb2d6cd7 12351 break;
252b5132 12352
54f4ddb3 12353 /* Update old instruction data. */
252b5132
RH
12354 if (target_big_endian)
12355 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
12356 else
12357 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
12358
a7ebbfdf
TS
12359 if (*valP + 0x20000 <= 0x3ffff)
12360 {
12361 insn |= (*valP >> 2) & 0xffff;
2132e3a3 12362 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
12363 }
12364 else if (mips_pic == NO_PIC
12365 && fixP->fx_done
12366 && fixP->fx_frag->fr_address >= text_section->vma
12367 && (fixP->fx_frag->fr_address
587aac4e 12368 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
12369 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
12370 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
12371 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
12372 {
12373 /* The branch offset is too large. If this is an
12374 unconditional branch, and we are not generating PIC code,
12375 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
12376 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
12377 insn = 0x0c000000; /* jal */
252b5132 12378 else
a7ebbfdf
TS
12379 insn = 0x08000000; /* j */
12380 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
12381 fixP->fx_done = 0;
12382 fixP->fx_addsy = section_symbol (text_section);
12383 *valP += md_pcrel_from (fixP);
2132e3a3 12384 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
12385 }
12386 else
12387 {
12388 /* If we got here, we have branch-relaxation disabled,
12389 and there's nothing we can do to fix this instruction
12390 without turning it into a longer sequence. */
12391 as_bad_where (fixP->fx_file, fixP->fx_line,
12392 _("Branch out of range"));
252b5132 12393 }
252b5132
RH
12394 break;
12395
12396 case BFD_RELOC_VTABLE_INHERIT:
12397 fixP->fx_done = 0;
12398 if (fixP->fx_addsy
12399 && !S_IS_DEFINED (fixP->fx_addsy)
12400 && !S_IS_WEAK (fixP->fx_addsy))
12401 S_SET_WEAK (fixP->fx_addsy);
12402 break;
12403
12404 case BFD_RELOC_VTABLE_ENTRY:
12405 fixP->fx_done = 0;
12406 break;
12407
12408 default:
12409 internalError ();
12410 }
a7ebbfdf
TS
12411
12412 /* Remember value for tc_gen_reloc. */
12413 fixP->fx_addnumber = *valP;
252b5132
RH
12414}
12415
252b5132 12416static symbolS *
17a2f251 12417get_symbol (void)
252b5132
RH
12418{
12419 int c;
12420 char *name;
12421 symbolS *p;
12422
12423 name = input_line_pointer;
12424 c = get_symbol_end ();
12425 p = (symbolS *) symbol_find_or_make (name);
12426 *input_line_pointer = c;
12427 return p;
12428}
12429
742a56fe
RS
12430/* Align the current frag to a given power of two. If a particular
12431 fill byte should be used, FILL points to an integer that contains
12432 that byte, otherwise FILL is null.
12433
12434 The MIPS assembler also automatically adjusts any preceding
12435 label. */
252b5132
RH
12436
12437static void
742a56fe 12438mips_align (int to, int *fill, symbolS *label)
252b5132 12439{
7d10b47d 12440 mips_emit_delays ();
742a56fe
RS
12441 mips_record_mips16_mode ();
12442 if (fill == NULL && subseg_text_p (now_seg))
12443 frag_align_code (to, 0);
12444 else
12445 frag_align (to, fill ? *fill : 0, 0);
252b5132
RH
12446 record_alignment (now_seg, to);
12447 if (label != NULL)
12448 {
9c2799c2 12449 gas_assert (S_GET_SEGMENT (label) == now_seg);
49309057 12450 symbol_set_frag (label, frag_now);
252b5132
RH
12451 S_SET_VALUE (label, (valueT) frag_now_fix ());
12452 }
12453}
12454
12455/* Align to a given power of two. .align 0 turns off the automatic
12456 alignment used by the data creating pseudo-ops. */
12457
12458static void
17a2f251 12459s_align (int x ATTRIBUTE_UNUSED)
252b5132 12460{
742a56fe 12461 int temp, fill_value, *fill_ptr;
49954fb4 12462 long max_alignment = 28;
252b5132 12463
54f4ddb3 12464 /* o Note that the assembler pulls down any immediately preceding label
252b5132 12465 to the aligned address.
54f4ddb3 12466 o It's not documented but auto alignment is reinstated by
252b5132 12467 a .align pseudo instruction.
54f4ddb3 12468 o Note also that after auto alignment is turned off the mips assembler
252b5132 12469 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 12470 We don't. */
252b5132
RH
12471
12472 temp = get_absolute_expression ();
12473 if (temp > max_alignment)
12474 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
12475 else if (temp < 0)
12476 {
12477 as_warn (_("Alignment negative: 0 assumed."));
12478 temp = 0;
12479 }
12480 if (*input_line_pointer == ',')
12481 {
f9419b05 12482 ++input_line_pointer;
742a56fe
RS
12483 fill_value = get_absolute_expression ();
12484 fill_ptr = &fill_value;
252b5132
RH
12485 }
12486 else
742a56fe 12487 fill_ptr = 0;
252b5132
RH
12488 if (temp)
12489 {
a8dbcb85
TS
12490 segment_info_type *si = seg_info (now_seg);
12491 struct insn_label_list *l = si->label_list;
54f4ddb3 12492 /* Auto alignment should be switched on by next section change. */
252b5132 12493 auto_align = 1;
742a56fe 12494 mips_align (temp, fill_ptr, l != NULL ? l->label : NULL);
252b5132
RH
12495 }
12496 else
12497 {
12498 auto_align = 0;
12499 }
12500
12501 demand_empty_rest_of_line ();
12502}
12503
252b5132 12504static void
17a2f251 12505s_change_sec (int sec)
252b5132
RH
12506{
12507 segT seg;
12508
252b5132
RH
12509#ifdef OBJ_ELF
12510 /* The ELF backend needs to know that we are changing sections, so
12511 that .previous works correctly. We could do something like check
b6ff326e 12512 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
12513 as it would not be appropriate to use it in the section changing
12514 functions in read.c, since obj-elf.c intercepts those. FIXME:
12515 This should be cleaner, somehow. */
f43abd2b
TS
12516 if (IS_ELF)
12517 obj_elf_section_change_hook ();
252b5132
RH
12518#endif
12519
7d10b47d 12520 mips_emit_delays ();
6a32d874 12521
252b5132
RH
12522 switch (sec)
12523 {
12524 case 't':
12525 s_text (0);
12526 break;
12527 case 'd':
12528 s_data (0);
12529 break;
12530 case 'b':
12531 subseg_set (bss_section, (subsegT) get_absolute_expression ());
12532 demand_empty_rest_of_line ();
12533 break;
12534
12535 case 'r':
4d0d148d
TS
12536 seg = subseg_new (RDATA_SECTION_NAME,
12537 (subsegT) get_absolute_expression ());
f43abd2b 12538 if (IS_ELF)
252b5132 12539 {
4d0d148d
TS
12540 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
12541 | SEC_READONLY | SEC_RELOC
12542 | SEC_DATA));
c41e87e3 12543 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 12544 record_alignment (seg, 4);
252b5132 12545 }
4d0d148d 12546 demand_empty_rest_of_line ();
252b5132
RH
12547 break;
12548
12549 case 's':
4d0d148d 12550 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f43abd2b 12551 if (IS_ELF)
252b5132 12552 {
4d0d148d
TS
12553 bfd_set_section_flags (stdoutput, seg,
12554 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
c41e87e3 12555 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 12556 record_alignment (seg, 4);
252b5132 12557 }
4d0d148d
TS
12558 demand_empty_rest_of_line ();
12559 break;
252b5132
RH
12560 }
12561
12562 auto_align = 1;
12563}
b34976b6 12564
cca86cc8 12565void
17a2f251 12566s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 12567{
7ed4a06a 12568#ifdef OBJ_ELF
cca86cc8
SC
12569 char *section_name;
12570 char c;
684022ea 12571 char next_c = 0;
cca86cc8
SC
12572 int section_type;
12573 int section_flag;
12574 int section_entry_size;
12575 int section_alignment;
b34976b6 12576
f43abd2b 12577 if (!IS_ELF)
7ed4a06a
TS
12578 return;
12579
cca86cc8
SC
12580 section_name = input_line_pointer;
12581 c = get_symbol_end ();
a816d1ed
AO
12582 if (c)
12583 next_c = *(input_line_pointer + 1);
cca86cc8 12584
4cf0dd0d
TS
12585 /* Do we have .section Name<,"flags">? */
12586 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 12587 {
4cf0dd0d
TS
12588 /* just after name is now '\0'. */
12589 *input_line_pointer = c;
cca86cc8
SC
12590 input_line_pointer = section_name;
12591 obj_elf_section (ignore);
12592 return;
12593 }
12594 input_line_pointer++;
12595
12596 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
12597 if (c == ',')
12598 section_type = get_absolute_expression ();
12599 else
12600 section_type = 0;
12601 if (*input_line_pointer++ == ',')
12602 section_flag = get_absolute_expression ();
12603 else
12604 section_flag = 0;
12605 if (*input_line_pointer++ == ',')
12606 section_entry_size = get_absolute_expression ();
12607 else
12608 section_entry_size = 0;
12609 if (*input_line_pointer++ == ',')
12610 section_alignment = get_absolute_expression ();
12611 else
12612 section_alignment = 0;
12613
a816d1ed
AO
12614 section_name = xstrdup (section_name);
12615
8ab8a5c8
RS
12616 /* When using the generic form of .section (as implemented by obj-elf.c),
12617 there's no way to set the section type to SHT_MIPS_DWARF. Users have
12618 traditionally had to fall back on the more common @progbits instead.
12619
12620 There's nothing really harmful in this, since bfd will correct
12621 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 12622 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
12623 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
12624
12625 Even so, we shouldn't force users of the MIPS .section syntax to
12626 incorrectly label the sections as SHT_PROGBITS. The best compromise
12627 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
12628 generic type-checking code. */
12629 if (section_type == SHT_MIPS_DWARF)
12630 section_type = SHT_PROGBITS;
12631
cca86cc8
SC
12632 obj_elf_change_section (section_name, section_type, section_flag,
12633 section_entry_size, 0, 0, 0);
a816d1ed
AO
12634
12635 if (now_seg->name != section_name)
12636 free (section_name);
7ed4a06a 12637#endif /* OBJ_ELF */
cca86cc8 12638}
252b5132
RH
12639
12640void
17a2f251 12641mips_enable_auto_align (void)
252b5132
RH
12642{
12643 auto_align = 1;
12644}
12645
12646static void
17a2f251 12647s_cons (int log_size)
252b5132 12648{
a8dbcb85
TS
12649 segment_info_type *si = seg_info (now_seg);
12650 struct insn_label_list *l = si->label_list;
252b5132
RH
12651 symbolS *label;
12652
a8dbcb85 12653 label = l != NULL ? l->label : NULL;
7d10b47d 12654 mips_emit_delays ();
252b5132
RH
12655 if (log_size > 0 && auto_align)
12656 mips_align (log_size, 0, label);
12657 mips_clear_insn_labels ();
12658 cons (1 << log_size);
12659}
12660
12661static void
17a2f251 12662s_float_cons (int type)
252b5132 12663{
a8dbcb85
TS
12664 segment_info_type *si = seg_info (now_seg);
12665 struct insn_label_list *l = si->label_list;
252b5132
RH
12666 symbolS *label;
12667
a8dbcb85 12668 label = l != NULL ? l->label : NULL;
252b5132 12669
7d10b47d 12670 mips_emit_delays ();
252b5132
RH
12671
12672 if (auto_align)
49309057
ILT
12673 {
12674 if (type == 'd')
12675 mips_align (3, 0, label);
12676 else
12677 mips_align (2, 0, label);
12678 }
252b5132
RH
12679
12680 mips_clear_insn_labels ();
12681
12682 float_cons (type);
12683}
12684
12685/* Handle .globl. We need to override it because on Irix 5 you are
12686 permitted to say
12687 .globl foo .text
12688 where foo is an undefined symbol, to mean that foo should be
12689 considered to be the address of a function. */
12690
12691static void
17a2f251 12692s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
12693{
12694 char *name;
12695 int c;
12696 symbolS *symbolP;
12697 flagword flag;
12698
8a06b769 12699 do
252b5132 12700 {
8a06b769 12701 name = input_line_pointer;
252b5132 12702 c = get_symbol_end ();
8a06b769
TS
12703 symbolP = symbol_find_or_make (name);
12704 S_SET_EXTERNAL (symbolP);
12705
252b5132 12706 *input_line_pointer = c;
8a06b769 12707 SKIP_WHITESPACE ();
252b5132 12708
8a06b769
TS
12709 /* On Irix 5, every global symbol that is not explicitly labelled as
12710 being a function is apparently labelled as being an object. */
12711 flag = BSF_OBJECT;
252b5132 12712
8a06b769
TS
12713 if (!is_end_of_line[(unsigned char) *input_line_pointer]
12714 && (*input_line_pointer != ','))
12715 {
12716 char *secname;
12717 asection *sec;
12718
12719 secname = input_line_pointer;
12720 c = get_symbol_end ();
12721 sec = bfd_get_section_by_name (stdoutput, secname);
12722 if (sec == NULL)
12723 as_bad (_("%s: no such section"), secname);
12724 *input_line_pointer = c;
12725
12726 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12727 flag = BSF_FUNCTION;
12728 }
12729
12730 symbol_get_bfdsym (symbolP)->flags |= flag;
12731
12732 c = *input_line_pointer;
12733 if (c == ',')
12734 {
12735 input_line_pointer++;
12736 SKIP_WHITESPACE ();
12737 if (is_end_of_line[(unsigned char) *input_line_pointer])
12738 c = '\n';
12739 }
12740 }
12741 while (c == ',');
252b5132 12742
252b5132
RH
12743 demand_empty_rest_of_line ();
12744}
12745
12746static void
17a2f251 12747s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
12748{
12749 char *opt;
12750 char c;
12751
12752 opt = input_line_pointer;
12753 c = get_symbol_end ();
12754
12755 if (*opt == 'O')
12756 {
12757 /* FIXME: What does this mean? */
12758 }
12759 else if (strncmp (opt, "pic", 3) == 0)
12760 {
12761 int i;
12762
12763 i = atoi (opt + 3);
12764 if (i == 0)
12765 mips_pic = NO_PIC;
12766 else if (i == 2)
143d77c5 12767 {
252b5132 12768 mips_pic = SVR4_PIC;
143d77c5
EC
12769 mips_abicalls = TRUE;
12770 }
252b5132
RH
12771 else
12772 as_bad (_(".option pic%d not supported"), i);
12773
4d0d148d 12774 if (mips_pic == SVR4_PIC)
252b5132
RH
12775 {
12776 if (g_switch_seen && g_switch_value != 0)
12777 as_warn (_("-G may not be used with SVR4 PIC code"));
12778 g_switch_value = 0;
12779 bfd_set_gp_size (stdoutput, 0);
12780 }
12781 }
12782 else
12783 as_warn (_("Unrecognized option \"%s\""), opt);
12784
12785 *input_line_pointer = c;
12786 demand_empty_rest_of_line ();
12787}
12788
12789/* This structure is used to hold a stack of .set values. */
12790
e972090a
NC
12791struct mips_option_stack
12792{
252b5132
RH
12793 struct mips_option_stack *next;
12794 struct mips_set_options options;
12795};
12796
12797static struct mips_option_stack *mips_opts_stack;
12798
12799/* Handle the .set pseudo-op. */
12800
12801static void
17a2f251 12802s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
12803{
12804 char *name = input_line_pointer, ch;
12805
12806 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 12807 ++input_line_pointer;
252b5132
RH
12808 ch = *input_line_pointer;
12809 *input_line_pointer = '\0';
12810
12811 if (strcmp (name, "reorder") == 0)
12812 {
7d10b47d
RS
12813 if (mips_opts.noreorder)
12814 end_noreorder ();
252b5132
RH
12815 }
12816 else if (strcmp (name, "noreorder") == 0)
12817 {
7d10b47d
RS
12818 if (!mips_opts.noreorder)
12819 start_noreorder ();
252b5132 12820 }
741fe287
MR
12821 else if (strncmp (name, "at=", 3) == 0)
12822 {
12823 char *s = name + 3;
12824
12825 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
12826 as_bad (_("Unrecognized register name `%s'"), s);
12827 }
252b5132
RH
12828 else if (strcmp (name, "at") == 0)
12829 {
741fe287 12830 mips_opts.at = ATREG;
252b5132
RH
12831 }
12832 else if (strcmp (name, "noat") == 0)
12833 {
741fe287 12834 mips_opts.at = ZERO;
252b5132
RH
12835 }
12836 else if (strcmp (name, "macro") == 0)
12837 {
12838 mips_opts.warn_about_macros = 0;
12839 }
12840 else if (strcmp (name, "nomacro") == 0)
12841 {
12842 if (mips_opts.noreorder == 0)
12843 as_bad (_("`noreorder' must be set before `nomacro'"));
12844 mips_opts.warn_about_macros = 1;
12845 }
12846 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12847 {
12848 mips_opts.nomove = 0;
12849 }
12850 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12851 {
12852 mips_opts.nomove = 1;
12853 }
12854 else if (strcmp (name, "bopt") == 0)
12855 {
12856 mips_opts.nobopt = 0;
12857 }
12858 else if (strcmp (name, "nobopt") == 0)
12859 {
12860 mips_opts.nobopt = 1;
12861 }
ad3fea08
TS
12862 else if (strcmp (name, "gp=default") == 0)
12863 mips_opts.gp32 = file_mips_gp32;
12864 else if (strcmp (name, "gp=32") == 0)
12865 mips_opts.gp32 = 1;
12866 else if (strcmp (name, "gp=64") == 0)
12867 {
12868 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 12869 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08
TS
12870 mips_cpu_info_from_isa (mips_opts.isa)->name);
12871 mips_opts.gp32 = 0;
12872 }
12873 else if (strcmp (name, "fp=default") == 0)
12874 mips_opts.fp32 = file_mips_fp32;
12875 else if (strcmp (name, "fp=32") == 0)
12876 mips_opts.fp32 = 1;
12877 else if (strcmp (name, "fp=64") == 0)
12878 {
12879 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 12880 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08
TS
12881 mips_cpu_info_from_isa (mips_opts.isa)->name);
12882 mips_opts.fp32 = 0;
12883 }
037b32b9
AN
12884 else if (strcmp (name, "softfloat") == 0)
12885 mips_opts.soft_float = 1;
12886 else if (strcmp (name, "hardfloat") == 0)
12887 mips_opts.soft_float = 0;
12888 else if (strcmp (name, "singlefloat") == 0)
12889 mips_opts.single_float = 1;
12890 else if (strcmp (name, "doublefloat") == 0)
12891 mips_opts.single_float = 0;
252b5132
RH
12892 else if (strcmp (name, "mips16") == 0
12893 || strcmp (name, "MIPS-16") == 0)
12894 mips_opts.mips16 = 1;
12895 else if (strcmp (name, "nomips16") == 0
12896 || strcmp (name, "noMIPS-16") == 0)
12897 mips_opts.mips16 = 0;
e16bfa71
TS
12898 else if (strcmp (name, "smartmips") == 0)
12899 {
ad3fea08 12900 if (!ISA_SUPPORTS_SMARTMIPS)
20203fb9 12901 as_warn (_("%s ISA does not support SmartMIPS ASE"),
e16bfa71
TS
12902 mips_cpu_info_from_isa (mips_opts.isa)->name);
12903 mips_opts.ase_smartmips = 1;
12904 }
12905 else if (strcmp (name, "nosmartmips") == 0)
12906 mips_opts.ase_smartmips = 0;
1f25f5d3
CD
12907 else if (strcmp (name, "mips3d") == 0)
12908 mips_opts.ase_mips3d = 1;
12909 else if (strcmp (name, "nomips3d") == 0)
12910 mips_opts.ase_mips3d = 0;
a4672219
TS
12911 else if (strcmp (name, "mdmx") == 0)
12912 mips_opts.ase_mdmx = 1;
12913 else if (strcmp (name, "nomdmx") == 0)
12914 mips_opts.ase_mdmx = 0;
74cd071d 12915 else if (strcmp (name, "dsp") == 0)
ad3fea08
TS
12916 {
12917 if (!ISA_SUPPORTS_DSP_ASE)
20203fb9 12918 as_warn (_("%s ISA does not support DSP ASE"),
ad3fea08
TS
12919 mips_cpu_info_from_isa (mips_opts.isa)->name);
12920 mips_opts.ase_dsp = 1;
8b082fb1 12921 mips_opts.ase_dspr2 = 0;
ad3fea08 12922 }
74cd071d 12923 else if (strcmp (name, "nodsp") == 0)
8b082fb1
TS
12924 {
12925 mips_opts.ase_dsp = 0;
12926 mips_opts.ase_dspr2 = 0;
12927 }
12928 else if (strcmp (name, "dspr2") == 0)
12929 {
12930 if (!ISA_SUPPORTS_DSPR2_ASE)
20203fb9 12931 as_warn (_("%s ISA does not support DSP R2 ASE"),
8b082fb1
TS
12932 mips_cpu_info_from_isa (mips_opts.isa)->name);
12933 mips_opts.ase_dspr2 = 1;
12934 mips_opts.ase_dsp = 1;
12935 }
12936 else if (strcmp (name, "nodspr2") == 0)
12937 {
12938 mips_opts.ase_dspr2 = 0;
12939 mips_opts.ase_dsp = 0;
12940 }
ef2e4d86 12941 else if (strcmp (name, "mt") == 0)
ad3fea08
TS
12942 {
12943 if (!ISA_SUPPORTS_MT_ASE)
20203fb9 12944 as_warn (_("%s ISA does not support MT ASE"),
ad3fea08
TS
12945 mips_cpu_info_from_isa (mips_opts.isa)->name);
12946 mips_opts.ase_mt = 1;
12947 }
ef2e4d86
CF
12948 else if (strcmp (name, "nomt") == 0)
12949 mips_opts.ase_mt = 0;
1a2c1fad 12950 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 12951 {
af7ee8bf 12952 int reset = 0;
252b5132 12953
1a2c1fad
CD
12954 /* Permit the user to change the ISA and architecture on the fly.
12955 Needless to say, misuse can cause serious problems. */
81a21e38 12956 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
12957 {
12958 reset = 1;
12959 mips_opts.isa = file_mips_isa;
1a2c1fad 12960 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
12961 }
12962 else if (strncmp (name, "arch=", 5) == 0)
12963 {
12964 const struct mips_cpu_info *p;
12965
12966 p = mips_parse_cpu("internal use", name + 5);
12967 if (!p)
12968 as_bad (_("unknown architecture %s"), name + 5);
12969 else
12970 {
12971 mips_opts.arch = p->cpu;
12972 mips_opts.isa = p->isa;
12973 }
12974 }
81a21e38
TS
12975 else if (strncmp (name, "mips", 4) == 0)
12976 {
12977 const struct mips_cpu_info *p;
12978
12979 p = mips_parse_cpu("internal use", name);
12980 if (!p)
12981 as_bad (_("unknown ISA level %s"), name + 4);
12982 else
12983 {
12984 mips_opts.arch = p->cpu;
12985 mips_opts.isa = p->isa;
12986 }
12987 }
af7ee8bf 12988 else
81a21e38 12989 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
12990
12991 switch (mips_opts.isa)
98d3f06f
KH
12992 {
12993 case 0:
98d3f06f 12994 break;
af7ee8bf
CD
12995 case ISA_MIPS1:
12996 case ISA_MIPS2:
12997 case ISA_MIPS32:
12998 case ISA_MIPS32R2:
98d3f06f
KH
12999 mips_opts.gp32 = 1;
13000 mips_opts.fp32 = 1;
13001 break;
af7ee8bf
CD
13002 case ISA_MIPS3:
13003 case ISA_MIPS4:
13004 case ISA_MIPS5:
13005 case ISA_MIPS64:
5f74bc13 13006 case ISA_MIPS64R2:
98d3f06f
KH
13007 mips_opts.gp32 = 0;
13008 mips_opts.fp32 = 0;
13009 break;
13010 default:
13011 as_bad (_("unknown ISA level %s"), name + 4);
13012 break;
13013 }
af7ee8bf 13014 if (reset)
98d3f06f 13015 {
af7ee8bf
CD
13016 mips_opts.gp32 = file_mips_gp32;
13017 mips_opts.fp32 = file_mips_fp32;
98d3f06f 13018 }
252b5132
RH
13019 }
13020 else if (strcmp (name, "autoextend") == 0)
13021 mips_opts.noautoextend = 0;
13022 else if (strcmp (name, "noautoextend") == 0)
13023 mips_opts.noautoextend = 1;
13024 else if (strcmp (name, "push") == 0)
13025 {
13026 struct mips_option_stack *s;
13027
13028 s = (struct mips_option_stack *) xmalloc (sizeof *s);
13029 s->next = mips_opts_stack;
13030 s->options = mips_opts;
13031 mips_opts_stack = s;
13032 }
13033 else if (strcmp (name, "pop") == 0)
13034 {
13035 struct mips_option_stack *s;
13036
13037 s = mips_opts_stack;
13038 if (s == NULL)
13039 as_bad (_(".set pop with no .set push"));
13040 else
13041 {
13042 /* If we're changing the reorder mode we need to handle
13043 delay slots correctly. */
13044 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 13045 start_noreorder ();
252b5132 13046 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 13047 end_noreorder ();
252b5132
RH
13048
13049 mips_opts = s->options;
13050 mips_opts_stack = s->next;
13051 free (s);
13052 }
13053 }
aed1a261
RS
13054 else if (strcmp (name, "sym32") == 0)
13055 mips_opts.sym32 = TRUE;
13056 else if (strcmp (name, "nosym32") == 0)
13057 mips_opts.sym32 = FALSE;
e6559e01
JM
13058 else if (strchr (name, ','))
13059 {
13060 /* Generic ".set" directive; use the generic handler. */
13061 *input_line_pointer = ch;
13062 input_line_pointer = name;
13063 s_set (0);
13064 return;
13065 }
252b5132
RH
13066 else
13067 {
13068 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
13069 }
13070 *input_line_pointer = ch;
13071 demand_empty_rest_of_line ();
13072}
13073
13074/* Handle the .abicalls pseudo-op. I believe this is equivalent to
13075 .option pic2. It means to generate SVR4 PIC calls. */
13076
13077static void
17a2f251 13078s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
13079{
13080 mips_pic = SVR4_PIC;
143d77c5 13081 mips_abicalls = TRUE;
4d0d148d
TS
13082
13083 if (g_switch_seen && g_switch_value != 0)
13084 as_warn (_("-G may not be used with SVR4 PIC code"));
13085 g_switch_value = 0;
13086
252b5132
RH
13087 bfd_set_gp_size (stdoutput, 0);
13088 demand_empty_rest_of_line ();
13089}
13090
13091/* Handle the .cpload pseudo-op. This is used when generating SVR4
13092 PIC code. It sets the $gp register for the function based on the
13093 function address, which is in the register named in the argument.
13094 This uses a relocation against _gp_disp, which is handled specially
13095 by the linker. The result is:
13096 lui $gp,%hi(_gp_disp)
13097 addiu $gp,$gp,%lo(_gp_disp)
13098 addu $gp,$gp,.cpload argument
aa6975fb
ILT
13099 The .cpload argument is normally $25 == $t9.
13100
13101 The -mno-shared option changes this to:
bbe506e8
TS
13102 lui $gp,%hi(__gnu_local_gp)
13103 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
13104 and the argument is ignored. This saves an instruction, but the
13105 resulting code is not position independent; it uses an absolute
bbe506e8
TS
13106 address for __gnu_local_gp. Thus code assembled with -mno-shared
13107 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
13108
13109static void
17a2f251 13110s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
13111{
13112 expressionS ex;
aa6975fb
ILT
13113 int reg;
13114 int in_shared;
252b5132 13115
6478892d
TS
13116 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
13117 .cpload is ignored. */
13118 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
13119 {
13120 s_ignore (0);
13121 return;
13122 }
13123
d3ecfc59 13124 /* .cpload should be in a .set noreorder section. */
252b5132
RH
13125 if (mips_opts.noreorder == 0)
13126 as_warn (_(".cpload not in noreorder section"));
13127
aa6975fb
ILT
13128 reg = tc_get_register (0);
13129
13130 /* If we need to produce a 64-bit address, we are better off using
13131 the default instruction sequence. */
aed1a261 13132 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 13133
252b5132 13134 ex.X_op = O_symbol;
bbe506e8
TS
13135 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
13136 "__gnu_local_gp");
252b5132
RH
13137 ex.X_op_symbol = NULL;
13138 ex.X_add_number = 0;
13139
13140 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 13141 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 13142
584892a6 13143 macro_start ();
67c0d1eb
RS
13144 macro_build_lui (&ex, mips_gp_register);
13145 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 13146 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
13147 if (in_shared)
13148 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
13149 mips_gp_register, reg);
584892a6 13150 macro_end ();
252b5132
RH
13151
13152 demand_empty_rest_of_line ();
13153}
13154
6478892d
TS
13155/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
13156 .cpsetup $reg1, offset|$reg2, label
13157
13158 If offset is given, this results in:
13159 sd $gp, offset($sp)
956cd1d6 13160 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
13161 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
13162 daddu $gp, $gp, $reg1
6478892d
TS
13163
13164 If $reg2 is given, this results in:
13165 daddu $reg2, $gp, $0
956cd1d6 13166 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
13167 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
13168 daddu $gp, $gp, $reg1
aa6975fb
ILT
13169 $reg1 is normally $25 == $t9.
13170
13171 The -mno-shared option replaces the last three instructions with
13172 lui $gp,%hi(_gp)
54f4ddb3 13173 addiu $gp,$gp,%lo(_gp) */
aa6975fb 13174
6478892d 13175static void
17a2f251 13176s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
13177{
13178 expressionS ex_off;
13179 expressionS ex_sym;
13180 int reg1;
6478892d 13181
8586fc66 13182 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
13183 We also need NewABI support. */
13184 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
13185 {
13186 s_ignore (0);
13187 return;
13188 }
13189
13190 reg1 = tc_get_register (0);
13191 SKIP_WHITESPACE ();
13192 if (*input_line_pointer != ',')
13193 {
13194 as_bad (_("missing argument separator ',' for .cpsetup"));
13195 return;
13196 }
13197 else
80245285 13198 ++input_line_pointer;
6478892d
TS
13199 SKIP_WHITESPACE ();
13200 if (*input_line_pointer == '$')
80245285
TS
13201 {
13202 mips_cpreturn_register = tc_get_register (0);
13203 mips_cpreturn_offset = -1;
13204 }
6478892d 13205 else
80245285
TS
13206 {
13207 mips_cpreturn_offset = get_absolute_expression ();
13208 mips_cpreturn_register = -1;
13209 }
6478892d
TS
13210 SKIP_WHITESPACE ();
13211 if (*input_line_pointer != ',')
13212 {
13213 as_bad (_("missing argument separator ',' for .cpsetup"));
13214 return;
13215 }
13216 else
f9419b05 13217 ++input_line_pointer;
6478892d 13218 SKIP_WHITESPACE ();
f21f8242 13219 expression (&ex_sym);
6478892d 13220
584892a6 13221 macro_start ();
6478892d
TS
13222 if (mips_cpreturn_register == -1)
13223 {
13224 ex_off.X_op = O_constant;
13225 ex_off.X_add_symbol = NULL;
13226 ex_off.X_op_symbol = NULL;
13227 ex_off.X_add_number = mips_cpreturn_offset;
13228
67c0d1eb 13229 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 13230 BFD_RELOC_LO16, SP);
6478892d
TS
13231 }
13232 else
67c0d1eb 13233 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 13234 mips_gp_register, 0);
6478892d 13235
aed1a261 13236 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb
ILT
13237 {
13238 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
13239 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
13240 BFD_RELOC_HI16_S);
13241
13242 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
13243 mips_gp_register, -1, BFD_RELOC_GPREL16,
13244 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
13245
13246 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
13247 mips_gp_register, reg1);
13248 }
13249 else
13250 {
13251 expressionS ex;
13252
13253 ex.X_op = O_symbol;
4184909a 13254 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
13255 ex.X_op_symbol = NULL;
13256 ex.X_add_number = 0;
6e1304d8 13257
aa6975fb
ILT
13258 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
13259 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
13260
13261 macro_build_lui (&ex, mips_gp_register);
13262 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
13263 mips_gp_register, BFD_RELOC_LO16);
13264 }
f21f8242 13265
584892a6 13266 macro_end ();
6478892d
TS
13267
13268 demand_empty_rest_of_line ();
13269}
13270
13271static void
17a2f251 13272s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
13273{
13274 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 13275 .cplocal is ignored. */
6478892d
TS
13276 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
13277 {
13278 s_ignore (0);
13279 return;
13280 }
13281
13282 mips_gp_register = tc_get_register (0);
85b51719 13283 demand_empty_rest_of_line ();
6478892d
TS
13284}
13285
252b5132
RH
13286/* Handle the .cprestore pseudo-op. This stores $gp into a given
13287 offset from $sp. The offset is remembered, and after making a PIC
13288 call $gp is restored from that location. */
13289
13290static void
17a2f251 13291s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
13292{
13293 expressionS ex;
252b5132 13294
6478892d 13295 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 13296 .cprestore is ignored. */
6478892d 13297 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
13298 {
13299 s_ignore (0);
13300 return;
13301 }
13302
13303 mips_cprestore_offset = get_absolute_expression ();
7a621144 13304 mips_cprestore_valid = 1;
252b5132
RH
13305
13306 ex.X_op = O_constant;
13307 ex.X_add_symbol = NULL;
13308 ex.X_op_symbol = NULL;
13309 ex.X_add_number = mips_cprestore_offset;
13310
584892a6 13311 macro_start ();
67c0d1eb
RS
13312 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
13313 SP, HAVE_64BIT_ADDRESSES);
584892a6 13314 macro_end ();
252b5132
RH
13315
13316 demand_empty_rest_of_line ();
13317}
13318
6478892d 13319/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 13320 was given in the preceding .cpsetup, it results in:
6478892d 13321 ld $gp, offset($sp)
76b3015f 13322
6478892d 13323 If a register $reg2 was given there, it results in:
54f4ddb3
TS
13324 daddu $gp, $reg2, $0 */
13325
6478892d 13326static void
17a2f251 13327s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
13328{
13329 expressionS ex;
6478892d
TS
13330
13331 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
13332 We also need NewABI support. */
13333 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
13334 {
13335 s_ignore (0);
13336 return;
13337 }
13338
584892a6 13339 macro_start ();
6478892d
TS
13340 if (mips_cpreturn_register == -1)
13341 {
13342 ex.X_op = O_constant;
13343 ex.X_add_symbol = NULL;
13344 ex.X_op_symbol = NULL;
13345 ex.X_add_number = mips_cpreturn_offset;
13346
67c0d1eb 13347 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
13348 }
13349 else
67c0d1eb 13350 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 13351 mips_cpreturn_register, 0);
584892a6 13352 macro_end ();
6478892d
TS
13353
13354 demand_empty_rest_of_line ();
13355}
13356
741d6ea8
JM
13357/* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
13358 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
13359 use in DWARF debug information. */
13360
13361static void
13362s_dtprel_internal (size_t bytes)
13363{
13364 expressionS ex;
13365 char *p;
13366
13367 expression (&ex);
13368
13369 if (ex.X_op != O_symbol)
13370 {
13371 as_bad (_("Unsupported use of %s"), (bytes == 8
13372 ? ".dtpreldword"
13373 : ".dtprelword"));
13374 ignore_rest_of_line ();
13375 }
13376
13377 p = frag_more (bytes);
13378 md_number_to_chars (p, 0, bytes);
13379 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE,
13380 (bytes == 8
13381 ? BFD_RELOC_MIPS_TLS_DTPREL64
13382 : BFD_RELOC_MIPS_TLS_DTPREL32));
13383
13384 demand_empty_rest_of_line ();
13385}
13386
13387/* Handle .dtprelword. */
13388
13389static void
13390s_dtprelword (int ignore ATTRIBUTE_UNUSED)
13391{
13392 s_dtprel_internal (4);
13393}
13394
13395/* Handle .dtpreldword. */
13396
13397static void
13398s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
13399{
13400 s_dtprel_internal (8);
13401}
13402
6478892d
TS
13403/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
13404 code. It sets the offset to use in gp_rel relocations. */
13405
13406static void
17a2f251 13407s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
13408{
13409 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
13410 We also need NewABI support. */
13411 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
13412 {
13413 s_ignore (0);
13414 return;
13415 }
13416
def2e0dd 13417 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
13418
13419 demand_empty_rest_of_line ();
13420}
13421
252b5132
RH
13422/* Handle the .gpword pseudo-op. This is used when generating PIC
13423 code. It generates a 32 bit GP relative reloc. */
13424
13425static void
17a2f251 13426s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 13427{
a8dbcb85
TS
13428 segment_info_type *si;
13429 struct insn_label_list *l;
252b5132
RH
13430 symbolS *label;
13431 expressionS ex;
13432 char *p;
13433
13434 /* When not generating PIC code, this is treated as .word. */
13435 if (mips_pic != SVR4_PIC)
13436 {
13437 s_cons (2);
13438 return;
13439 }
13440
a8dbcb85
TS
13441 si = seg_info (now_seg);
13442 l = si->label_list;
13443 label = l != NULL ? l->label : NULL;
7d10b47d 13444 mips_emit_delays ();
252b5132
RH
13445 if (auto_align)
13446 mips_align (2, 0, label);
13447 mips_clear_insn_labels ();
13448
13449 expression (&ex);
13450
13451 if (ex.X_op != O_symbol || ex.X_add_number != 0)
13452 {
13453 as_bad (_("Unsupported use of .gpword"));
13454 ignore_rest_of_line ();
13455 }
13456
13457 p = frag_more (4);
17a2f251 13458 md_number_to_chars (p, 0, 4);
b34976b6 13459 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 13460 BFD_RELOC_GPREL32);
252b5132
RH
13461
13462 demand_empty_rest_of_line ();
13463}
13464
10181a0d 13465static void
17a2f251 13466s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 13467{
a8dbcb85
TS
13468 segment_info_type *si;
13469 struct insn_label_list *l;
10181a0d
AO
13470 symbolS *label;
13471 expressionS ex;
13472 char *p;
13473
13474 /* When not generating PIC code, this is treated as .dword. */
13475 if (mips_pic != SVR4_PIC)
13476 {
13477 s_cons (3);
13478 return;
13479 }
13480
a8dbcb85
TS
13481 si = seg_info (now_seg);
13482 l = si->label_list;
13483 label = l != NULL ? l->label : NULL;
7d10b47d 13484 mips_emit_delays ();
10181a0d
AO
13485 if (auto_align)
13486 mips_align (3, 0, label);
13487 mips_clear_insn_labels ();
13488
13489 expression (&ex);
13490
13491 if (ex.X_op != O_symbol || ex.X_add_number != 0)
13492 {
13493 as_bad (_("Unsupported use of .gpdword"));
13494 ignore_rest_of_line ();
13495 }
13496
13497 p = frag_more (8);
17a2f251 13498 md_number_to_chars (p, 0, 8);
a105a300 13499 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 13500 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
13501
13502 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
13503 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
13504 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
13505
13506 demand_empty_rest_of_line ();
13507}
13508
252b5132
RH
13509/* Handle the .cpadd pseudo-op. This is used when dealing with switch
13510 tables in SVR4 PIC code. */
13511
13512static void
17a2f251 13513s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 13514{
252b5132
RH
13515 int reg;
13516
10181a0d
AO
13517 /* This is ignored when not generating SVR4 PIC code. */
13518 if (mips_pic != SVR4_PIC)
252b5132
RH
13519 {
13520 s_ignore (0);
13521 return;
13522 }
13523
13524 /* Add $gp to the register named as an argument. */
584892a6 13525 macro_start ();
252b5132 13526 reg = tc_get_register (0);
67c0d1eb 13527 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 13528 macro_end ();
252b5132 13529
bdaaa2e1 13530 demand_empty_rest_of_line ();
252b5132
RH
13531}
13532
13533/* Handle the .insn pseudo-op. This marks instruction labels in
13534 mips16 mode. This permits the linker to handle them specially,
13535 such as generating jalx instructions when needed. We also make
13536 them odd for the duration of the assembly, in order to generate the
13537 right sort of code. We will make them even in the adjust_symtab
13538 routine, while leaving them marked. This is convenient for the
13539 debugger and the disassembler. The linker knows to make them odd
13540 again. */
13541
13542static void
17a2f251 13543s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 13544{
f9419b05 13545 mips16_mark_labels ();
252b5132
RH
13546
13547 demand_empty_rest_of_line ();
13548}
13549
13550/* Handle a .stabn directive. We need these in order to mark a label
13551 as being a mips16 text label correctly. Sometimes the compiler
13552 will emit a label, followed by a .stabn, and then switch sections.
13553 If the label and .stabn are in mips16 mode, then the label is
13554 really a mips16 text label. */
13555
13556static void
17a2f251 13557s_mips_stab (int type)
252b5132 13558{
f9419b05 13559 if (type == 'n')
252b5132
RH
13560 mips16_mark_labels ();
13561
13562 s_stab (type);
13563}
13564
54f4ddb3 13565/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
13566
13567static void
17a2f251 13568s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
13569{
13570 char *name;
13571 int c;
13572 symbolS *symbolP;
13573 expressionS exp;
13574
13575 name = input_line_pointer;
13576 c = get_symbol_end ();
13577 symbolP = symbol_find_or_make (name);
13578 S_SET_WEAK (symbolP);
13579 *input_line_pointer = c;
13580
13581 SKIP_WHITESPACE ();
13582
13583 if (! is_end_of_line[(unsigned char) *input_line_pointer])
13584 {
13585 if (S_IS_DEFINED (symbolP))
13586 {
20203fb9 13587 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
13588 S_GET_NAME (symbolP));
13589 ignore_rest_of_line ();
13590 return;
13591 }
bdaaa2e1 13592
252b5132
RH
13593 if (*input_line_pointer == ',')
13594 {
13595 ++input_line_pointer;
13596 SKIP_WHITESPACE ();
13597 }
bdaaa2e1 13598
252b5132
RH
13599 expression (&exp);
13600 if (exp.X_op != O_symbol)
13601 {
20203fb9 13602 as_bad (_("bad .weakext directive"));
98d3f06f 13603 ignore_rest_of_line ();
252b5132
RH
13604 return;
13605 }
49309057 13606 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
13607 }
13608
13609 demand_empty_rest_of_line ();
13610}
13611
13612/* Parse a register string into a number. Called from the ECOFF code
13613 to parse .frame. The argument is non-zero if this is the frame
13614 register, so that we can record it in mips_frame_reg. */
13615
13616int
17a2f251 13617tc_get_register (int frame)
252b5132 13618{
707bfff6 13619 unsigned int reg;
252b5132
RH
13620
13621 SKIP_WHITESPACE ();
707bfff6
TS
13622 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
13623 reg = 0;
252b5132 13624 if (frame)
7a621144
DJ
13625 {
13626 mips_frame_reg = reg != 0 ? reg : SP;
13627 mips_frame_reg_valid = 1;
13628 mips_cprestore_valid = 0;
13629 }
252b5132
RH
13630 return reg;
13631}
13632
13633valueT
17a2f251 13634md_section_align (asection *seg, valueT addr)
252b5132
RH
13635{
13636 int align = bfd_get_section_alignment (stdoutput, seg);
13637
b4c71f56
TS
13638 if (IS_ELF)
13639 {
13640 /* We don't need to align ELF sections to the full alignment.
13641 However, Irix 5 may prefer that we align them at least to a 16
13642 byte boundary. We don't bother to align the sections if we
13643 are targeted for an embedded system. */
c41e87e3 13644 if (strncmp (TARGET_OS, "elf", 3) == 0)
b4c71f56
TS
13645 return addr;
13646 if (align > 4)
13647 align = 4;
13648 }
252b5132
RH
13649
13650 return ((addr + (1 << align) - 1) & (-1 << align));
13651}
13652
13653/* Utility routine, called from above as well. If called while the
13654 input file is still being read, it's only an approximation. (For
13655 example, a symbol may later become defined which appeared to be
13656 undefined earlier.) */
13657
13658static int
17a2f251 13659nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
13660{
13661 if (sym == 0)
13662 return 0;
13663
4d0d148d 13664 if (g_switch_value > 0)
252b5132
RH
13665 {
13666 const char *symname;
13667 int change;
13668
c9914766 13669 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
13670 register. It can be if it is smaller than the -G size or if
13671 it is in the .sdata or .sbss section. Certain symbols can
c9914766 13672 not be referenced off the $gp, although it appears as though
252b5132
RH
13673 they can. */
13674 symname = S_GET_NAME (sym);
13675 if (symname != (const char *) NULL
13676 && (strcmp (symname, "eprol") == 0
13677 || strcmp (symname, "etext") == 0
13678 || strcmp (symname, "_gp") == 0
13679 || strcmp (symname, "edata") == 0
13680 || strcmp (symname, "_fbss") == 0
13681 || strcmp (symname, "_fdata") == 0
13682 || strcmp (symname, "_ftext") == 0
13683 || strcmp (symname, "end") == 0
13684 || strcmp (symname, "_gp_disp") == 0))
13685 change = 1;
13686 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
13687 && (0
13688#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
13689 || (symbol_get_obj (sym)->ecoff_extern_size != 0
13690 && (symbol_get_obj (sym)->ecoff_extern_size
13691 <= g_switch_value))
252b5132
RH
13692#endif
13693 /* We must defer this decision until after the whole
13694 file has been read, since there might be a .extern
13695 after the first use of this symbol. */
13696 || (before_relaxing
13697#ifndef NO_ECOFF_DEBUGGING
49309057 13698 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
13699#endif
13700 && S_GET_VALUE (sym) == 0)
13701 || (S_GET_VALUE (sym) != 0
13702 && S_GET_VALUE (sym) <= g_switch_value)))
13703 change = 0;
13704 else
13705 {
13706 const char *segname;
13707
13708 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 13709 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
13710 && strcmp (segname, ".lit4") != 0);
13711 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
13712 && strcmp (segname, ".sbss") != 0
13713 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
13714 && strncmp (segname, ".sbss.", 6) != 0
13715 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 13716 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
13717 }
13718 return change;
13719 }
13720 else
c9914766 13721 /* We are not optimizing for the $gp register. */
252b5132
RH
13722 return 1;
13723}
13724
5919d012
RS
13725
13726/* Return true if the given symbol should be considered local for SVR4 PIC. */
13727
13728static bfd_boolean
17a2f251 13729pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
13730{
13731 asection *symsec;
5919d012
RS
13732
13733 /* Handle the case of a symbol equated to another symbol. */
13734 while (symbol_equated_reloc_p (sym))
13735 {
13736 symbolS *n;
13737
5f0fe04b 13738 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
13739 n = symbol_get_value_expression (sym)->X_add_symbol;
13740 if (n == sym)
13741 break;
13742 sym = n;
13743 }
13744
df1f3cda
DD
13745 if (symbol_section_p (sym))
13746 return TRUE;
13747
5919d012
RS
13748 symsec = S_GET_SEGMENT (sym);
13749
5919d012
RS
13750 /* This must duplicate the test in adjust_reloc_syms. */
13751 return (symsec != &bfd_und_section
13752 && symsec != &bfd_abs_section
5f0fe04b
TS
13753 && !bfd_is_com_section (symsec)
13754 && !s_is_linkonce (sym, segtype)
5919d012
RS
13755#ifdef OBJ_ELF
13756 /* A global or weak symbol is treated as external. */
f43abd2b 13757 && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
5919d012
RS
13758#endif
13759 );
13760}
13761
13762
252b5132
RH
13763/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13764 extended opcode. SEC is the section the frag is in. */
13765
13766static int
17a2f251 13767mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
13768{
13769 int type;
3994f87e 13770 const struct mips16_immed_operand *op;
252b5132
RH
13771 offsetT val;
13772 int mintiny, maxtiny;
13773 segT symsec;
98aa84af 13774 fragS *sym_frag;
252b5132
RH
13775
13776 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13777 return 0;
13778 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13779 return 1;
13780
13781 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13782 op = mips16_immed_operands;
13783 while (op->type != type)
13784 {
13785 ++op;
9c2799c2 13786 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
13787 }
13788
13789 if (op->unsp)
13790 {
13791 if (type == '<' || type == '>' || type == '[' || type == ']')
13792 {
13793 mintiny = 1;
13794 maxtiny = 1 << op->nbits;
13795 }
13796 else
13797 {
13798 mintiny = 0;
13799 maxtiny = (1 << op->nbits) - 1;
13800 }
13801 }
13802 else
13803 {
13804 mintiny = - (1 << (op->nbits - 1));
13805 maxtiny = (1 << (op->nbits - 1)) - 1;
13806 }
13807
98aa84af 13808 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 13809 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 13810 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
13811
13812 if (op->pcrel)
13813 {
13814 addressT addr;
13815
13816 /* We won't have the section when we are called from
13817 mips_relax_frag. However, we will always have been called
13818 from md_estimate_size_before_relax first. If this is a
13819 branch to a different section, we mark it as such. If SEC is
13820 NULL, and the frag is not marked, then it must be a branch to
13821 the same section. */
13822 if (sec == NULL)
13823 {
13824 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13825 return 1;
13826 }
13827 else
13828 {
98aa84af 13829 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
13830 if (symsec != sec)
13831 {
13832 fragp->fr_subtype =
13833 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13834
13835 /* FIXME: We should support this, and let the linker
13836 catch branches and loads that are out of range. */
13837 as_bad_where (fragp->fr_file, fragp->fr_line,
13838 _("unsupported PC relative reference to different section"));
13839
13840 return 1;
13841 }
98aa84af
AM
13842 if (fragp != sym_frag && sym_frag->fr_address == 0)
13843 /* Assume non-extended on the first relaxation pass.
13844 The address we have calculated will be bogus if this is
13845 a forward branch to another frag, as the forward frag
13846 will have fr_address == 0. */
13847 return 0;
252b5132
RH
13848 }
13849
13850 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
13851 the same section. If the relax_marker of the symbol fragment
13852 differs from the relax_marker of this fragment, we have not
13853 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
13854 in STRETCH in order to get a better estimate of the address.
13855 This particularly matters because of the shift bits. */
13856 if (stretch != 0
98aa84af 13857 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
13858 {
13859 fragS *f;
13860
13861 /* Adjust stretch for any alignment frag. Note that if have
13862 been expanding the earlier code, the symbol may be
13863 defined in what appears to be an earlier frag. FIXME:
13864 This doesn't handle the fr_subtype field, which specifies
13865 a maximum number of bytes to skip when doing an
13866 alignment. */
98aa84af 13867 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
13868 {
13869 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13870 {
13871 if (stretch < 0)
13872 stretch = - ((- stretch)
13873 & ~ ((1 << (int) f->fr_offset) - 1));
13874 else
13875 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13876 if (stretch == 0)
13877 break;
13878 }
13879 }
13880 if (f != NULL)
13881 val += stretch;
13882 }
13883
13884 addr = fragp->fr_address + fragp->fr_fix;
13885
13886 /* The base address rules are complicated. The base address of
13887 a branch is the following instruction. The base address of a
13888 PC relative load or add is the instruction itself, but if it
13889 is in a delay slot (in which case it can not be extended) use
13890 the address of the instruction whose delay slot it is in. */
13891 if (type == 'p' || type == 'q')
13892 {
13893 addr += 2;
13894
13895 /* If we are currently assuming that this frag should be
13896 extended, then, the current address is two bytes
bdaaa2e1 13897 higher. */
252b5132
RH
13898 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13899 addr += 2;
13900
13901 /* Ignore the low bit in the target, since it will be set
13902 for a text label. */
13903 if ((val & 1) != 0)
13904 --val;
13905 }
13906 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13907 addr -= 4;
13908 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13909 addr -= 2;
13910
13911 val -= addr & ~ ((1 << op->shift) - 1);
13912
13913 /* Branch offsets have an implicit 0 in the lowest bit. */
13914 if (type == 'p' || type == 'q')
13915 val /= 2;
13916
13917 /* If any of the shifted bits are set, we must use an extended
13918 opcode. If the address depends on the size of this
13919 instruction, this can lead to a loop, so we arrange to always
13920 use an extended opcode. We only check this when we are in
13921 the main relaxation loop, when SEC is NULL. */
13922 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13923 {
13924 fragp->fr_subtype =
13925 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13926 return 1;
13927 }
13928
13929 /* If we are about to mark a frag as extended because the value
13930 is precisely maxtiny + 1, then there is a chance of an
13931 infinite loop as in the following code:
13932 la $4,foo
13933 .skip 1020
13934 .align 2
13935 foo:
13936 In this case when the la is extended, foo is 0x3fc bytes
13937 away, so the la can be shrunk, but then foo is 0x400 away, so
13938 the la must be extended. To avoid this loop, we mark the
13939 frag as extended if it was small, and is about to become
13940 extended with a value of maxtiny + 1. */
13941 if (val == ((maxtiny + 1) << op->shift)
13942 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13943 && sec == NULL)
13944 {
13945 fragp->fr_subtype =
13946 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13947 return 1;
13948 }
13949 }
13950 else if (symsec != absolute_section && sec != NULL)
13951 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13952
13953 if ((val & ((1 << op->shift) - 1)) != 0
13954 || val < (mintiny << op->shift)
13955 || val > (maxtiny << op->shift))
13956 return 1;
13957 else
13958 return 0;
13959}
13960
4a6a3df4
AO
13961/* Compute the length of a branch sequence, and adjust the
13962 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13963 worst-case length is computed, with UPDATE being used to indicate
13964 whether an unconditional (-1), branch-likely (+1) or regular (0)
13965 branch is to be computed. */
13966static int
17a2f251 13967relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 13968{
b34976b6 13969 bfd_boolean toofar;
4a6a3df4
AO
13970 int length;
13971
13972 if (fragp
13973 && S_IS_DEFINED (fragp->fr_symbol)
13974 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13975 {
13976 addressT addr;
13977 offsetT val;
13978
13979 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13980
13981 addr = fragp->fr_address + fragp->fr_fix + 4;
13982
13983 val -= addr;
13984
13985 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13986 }
13987 else if (fragp)
13988 /* If the symbol is not defined or it's in a different segment,
13989 assume the user knows what's going on and emit a short
13990 branch. */
b34976b6 13991 toofar = FALSE;
4a6a3df4 13992 else
b34976b6 13993 toofar = TRUE;
4a6a3df4
AO
13994
13995 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13996 fragp->fr_subtype
af6ae2ad 13997 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
13998 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13999 RELAX_BRANCH_LINK (fragp->fr_subtype),
14000 toofar);
14001
14002 length = 4;
14003 if (toofar)
14004 {
14005 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
14006 length += 8;
14007
14008 if (mips_pic != NO_PIC)
14009 {
14010 /* Additional space for PIC loading of target address. */
14011 length += 8;
14012 if (mips_opts.isa == ISA_MIPS1)
14013 /* Additional space for $at-stabilizing nop. */
14014 length += 4;
14015 }
14016
14017 /* If branch is conditional. */
14018 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
14019 length += 8;
14020 }
b34976b6 14021
4a6a3df4
AO
14022 return length;
14023}
14024
252b5132
RH
14025/* Estimate the size of a frag before relaxing. Unless this is the
14026 mips16, we are not really relaxing here, and the final size is
14027 encoded in the subtype information. For the mips16, we have to
14028 decide whether we are using an extended opcode or not. */
14029
252b5132 14030int
17a2f251 14031md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 14032{
5919d012 14033 int change;
252b5132 14034
4a6a3df4
AO
14035 if (RELAX_BRANCH_P (fragp->fr_subtype))
14036 {
14037
b34976b6
AM
14038 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
14039
4a6a3df4
AO
14040 return fragp->fr_var;
14041 }
14042
252b5132 14043 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
14044 /* We don't want to modify the EXTENDED bit here; it might get us
14045 into infinite loops. We change it only in mips_relax_frag(). */
14046 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
14047
14048 if (mips_pic == NO_PIC)
5919d012 14049 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 14050 else if (mips_pic == SVR4_PIC)
5919d012 14051 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
14052 else if (mips_pic == VXWORKS_PIC)
14053 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
14054 change = 0;
252b5132
RH
14055 else
14056 abort ();
14057
14058 if (change)
14059 {
4d7206a2 14060 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 14061 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 14062 }
4d7206a2
RS
14063 else
14064 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
14065}
14066
14067/* This is called to see whether a reloc against a defined symbol
de7e6852 14068 should be converted into a reloc against a section. */
252b5132
RH
14069
14070int
17a2f251 14071mips_fix_adjustable (fixS *fixp)
252b5132 14072{
252b5132
RH
14073 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
14074 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
14075 return 0;
a161fe53 14076
252b5132
RH
14077 if (fixp->fx_addsy == NULL)
14078 return 1;
a161fe53 14079
de7e6852
RS
14080 /* If symbol SYM is in a mergeable section, relocations of the form
14081 SYM + 0 can usually be made section-relative. The mergeable data
14082 is then identified by the section offset rather than by the symbol.
14083
14084 However, if we're generating REL LO16 relocations, the offset is split
14085 between the LO16 and parterning high part relocation. The linker will
14086 need to recalculate the complete offset in order to correctly identify
14087 the merge data.
14088
14089 The linker has traditionally not looked for the parterning high part
14090 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
14091 placed anywhere. Rather than break backwards compatibility by changing
14092 this, it seems better not to force the issue, and instead keep the
14093 original symbol. This will work with either linker behavior. */
738e5348 14094 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 14095 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
14096 && HAVE_IN_PLACE_ADDENDS
14097 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
14098 return 0;
14099
1180b5a4
RS
14100 /* There is no place to store an in-place offset for JALR relocations. */
14101 if (fixp->fx_r_type == BFD_RELOC_MIPS_JALR && HAVE_IN_PLACE_ADDENDS)
14102 return 0;
14103
252b5132 14104#ifdef OBJ_ELF
b314ec0e
RS
14105 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
14106 to a floating-point stub. The same is true for non-R_MIPS16_26
14107 relocations against MIPS16 functions; in this case, the stub becomes
14108 the function's canonical address.
14109
14110 Floating-point stubs are stored in unique .mips16.call.* or
14111 .mips16.fn.* sections. If a stub T for function F is in section S,
14112 the first relocation in section S must be against F; this is how the
14113 linker determines the target function. All relocations that might
14114 resolve to T must also be against F. We therefore have the following
14115 restrictions, which are given in an intentionally-redundant way:
14116
14117 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
14118 symbols.
14119
14120 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
14121 if that stub might be used.
14122
14123 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
14124 symbols.
14125
14126 4. We cannot reduce a stub's relocations against MIPS16 symbols if
14127 that stub might be used.
14128
14129 There is a further restriction:
14130
14131 5. We cannot reduce R_MIPS16_26 relocations against MIPS16 symbols
14132 on targets with in-place addends; the relocation field cannot
14133 encode the low bit.
14134
14135 For simplicity, we deal with (3)-(5) by not reducing _any_ relocation
14136 against a MIPS16 symbol.
14137
14138 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
14139 relocation against some symbol R, no relocation against R may be
14140 reduced. (Note that this deals with (2) as well as (1) because
14141 relocations against global symbols will never be reduced on ELF
14142 targets.) This approach is a little simpler than trying to detect
14143 stub sections, and gives the "all or nothing" per-symbol consistency
14144 that we have for MIPS16 symbols. */
f43abd2b 14145 if (IS_ELF
b314ec0e 14146 && fixp->fx_subsy == NULL
30c09090 14147 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
b314ec0e 14148 || *symbol_get_tc (fixp->fx_addsy)))
252b5132
RH
14149 return 0;
14150#endif
a161fe53 14151
252b5132
RH
14152 return 1;
14153}
14154
14155/* Translate internal representation of relocation info to BFD target
14156 format. */
14157
14158arelent **
17a2f251 14159tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
14160{
14161 static arelent *retval[4];
14162 arelent *reloc;
14163 bfd_reloc_code_real_type code;
14164
4b0cff4e
TS
14165 memset (retval, 0, sizeof(retval));
14166 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
14167 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
14168 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
14169 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
14170
bad36eac
DJ
14171 if (fixp->fx_pcrel)
14172 {
9c2799c2 14173 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2);
bad36eac
DJ
14174
14175 /* At this point, fx_addnumber is "symbol offset - pcrel address".
14176 Relocations want only the symbol offset. */
14177 reloc->addend = fixp->fx_addnumber + reloc->address;
f43abd2b 14178 if (!IS_ELF)
bad36eac
DJ
14179 {
14180 /* A gruesome hack which is a result of the gruesome gas
14181 reloc handling. What's worse, for COFF (as opposed to
14182 ECOFF), we might need yet another copy of reloc->address.
14183 See bfd_install_relocation. */
14184 reloc->addend += reloc->address;
14185 }
14186 }
14187 else
14188 reloc->addend = fixp->fx_addnumber;
252b5132 14189
438c16b8
TS
14190 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
14191 entry to be used in the relocation's section offset. */
14192 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
14193 {
14194 reloc->address = reloc->addend;
14195 reloc->addend = 0;
14196 }
14197
252b5132 14198 code = fixp->fx_r_type;
252b5132 14199
bad36eac 14200 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
14201 if (reloc->howto == NULL)
14202 {
14203 as_bad_where (fixp->fx_file, fixp->fx_line,
14204 _("Can not represent %s relocation in this object file format"),
14205 bfd_get_reloc_code_name (code));
14206 retval[0] = NULL;
14207 }
14208
14209 return retval;
14210}
14211
14212/* Relax a machine dependent frag. This returns the amount by which
14213 the current size of the frag should change. */
14214
14215int
17a2f251 14216mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 14217{
4a6a3df4
AO
14218 if (RELAX_BRANCH_P (fragp->fr_subtype))
14219 {
14220 offsetT old_var = fragp->fr_var;
b34976b6
AM
14221
14222 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
14223
14224 return fragp->fr_var - old_var;
14225 }
14226
252b5132
RH
14227 if (! RELAX_MIPS16_P (fragp->fr_subtype))
14228 return 0;
14229
c4e7957c 14230 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
14231 {
14232 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14233 return 0;
14234 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
14235 return 2;
14236 }
14237 else
14238 {
14239 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14240 return 0;
14241 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
14242 return -2;
14243 }
14244
14245 return 0;
14246}
14247
14248/* Convert a machine dependent frag. */
14249
14250void
17a2f251 14251md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 14252{
4a6a3df4
AO
14253 if (RELAX_BRANCH_P (fragp->fr_subtype))
14254 {
14255 bfd_byte *buf;
14256 unsigned long insn;
14257 expressionS exp;
14258 fixS *fixp;
b34976b6 14259
4a6a3df4
AO
14260 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
14261
14262 if (target_big_endian)
14263 insn = bfd_getb32 (buf);
14264 else
14265 insn = bfd_getl32 (buf);
b34976b6 14266
4a6a3df4
AO
14267 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
14268 {
14269 /* We generate a fixup instead of applying it right now
14270 because, if there are linker relaxations, we're going to
14271 need the relocations. */
14272 exp.X_op = O_symbol;
14273 exp.X_add_symbol = fragp->fr_symbol;
14274 exp.X_add_number = fragp->fr_offset;
14275
14276 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 14277 4, &exp, TRUE, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
14278 fixp->fx_file = fragp->fr_file;
14279 fixp->fx_line = fragp->fr_line;
b34976b6 14280
2132e3a3 14281 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
14282 buf += 4;
14283 }
14284 else
14285 {
14286 int i;
14287
14288 as_warn_where (fragp->fr_file, fragp->fr_line,
14289 _("relaxed out-of-range branch into a jump"));
14290
14291 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
14292 goto uncond;
14293
14294 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14295 {
14296 /* Reverse the branch. */
14297 switch ((insn >> 28) & 0xf)
14298 {
14299 case 4:
14300 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
14301 have the condition reversed by tweaking a single
14302 bit, and their opcodes all have 0x4???????. */
9c2799c2 14303 gas_assert ((insn & 0xf1000000) == 0x41000000);
4a6a3df4
AO
14304 insn ^= 0x00010000;
14305 break;
14306
14307 case 0:
14308 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 14309 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 14310 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
14311 insn ^= 0x00010000;
14312 break;
b34976b6 14313
4a6a3df4
AO
14314 case 1:
14315 /* beq 0x10000000 bne 0x14000000
54f4ddb3 14316 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
14317 insn ^= 0x04000000;
14318 break;
14319
14320 default:
14321 abort ();
14322 }
14323 }
14324
14325 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14326 {
14327 /* Clear the and-link bit. */
9c2799c2 14328 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 14329
54f4ddb3
TS
14330 /* bltzal 0x04100000 bgezal 0x04110000
14331 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
14332 insn &= ~0x00100000;
14333 }
14334
14335 /* Branch over the branch (if the branch was likely) or the
14336 full jump (not likely case). Compute the offset from the
14337 current instruction to branch to. */
14338 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14339 i = 16;
14340 else
14341 {
14342 /* How many bytes in instructions we've already emitted? */
14343 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14344 /* How many bytes in instructions from here to the end? */
14345 i = fragp->fr_var - i;
14346 }
14347 /* Convert to instruction count. */
14348 i >>= 2;
14349 /* Branch counts from the next instruction. */
b34976b6 14350 i--;
4a6a3df4
AO
14351 insn |= i;
14352 /* Branch over the jump. */
2132e3a3 14353 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
14354 buf += 4;
14355
54f4ddb3 14356 /* nop */
2132e3a3 14357 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
14358 buf += 4;
14359
14360 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14361 {
14362 /* beql $0, $0, 2f */
14363 insn = 0x50000000;
14364 /* Compute the PC offset from the current instruction to
14365 the end of the variable frag. */
14366 /* How many bytes in instructions we've already emitted? */
14367 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14368 /* How many bytes in instructions from here to the end? */
14369 i = fragp->fr_var - i;
14370 /* Convert to instruction count. */
14371 i >>= 2;
14372 /* Don't decrement i, because we want to branch over the
14373 delay slot. */
14374
14375 insn |= i;
2132e3a3 14376 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
14377 buf += 4;
14378
2132e3a3 14379 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
14380 buf += 4;
14381 }
14382
14383 uncond:
14384 if (mips_pic == NO_PIC)
14385 {
14386 /* j or jal. */
14387 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
14388 ? 0x0c000000 : 0x08000000);
14389 exp.X_op = O_symbol;
14390 exp.X_add_symbol = fragp->fr_symbol;
14391 exp.X_add_number = fragp->fr_offset;
14392
14393 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 14394 4, &exp, FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
14395 fixp->fx_file = fragp->fr_file;
14396 fixp->fx_line = fragp->fr_line;
14397
2132e3a3 14398 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
14399 buf += 4;
14400 }
14401 else
14402 {
14403 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
14404 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
14405 exp.X_op = O_symbol;
14406 exp.X_add_symbol = fragp->fr_symbol;
14407 exp.X_add_number = fragp->fr_offset;
14408
14409 if (fragp->fr_offset)
14410 {
14411 exp.X_add_symbol = make_expr_symbol (&exp);
14412 exp.X_add_number = 0;
14413 }
14414
14415 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 14416 4, &exp, FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
14417 fixp->fx_file = fragp->fr_file;
14418 fixp->fx_line = fragp->fr_line;
14419
2132e3a3 14420 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4 14421 buf += 4;
b34976b6 14422
4a6a3df4
AO
14423 if (mips_opts.isa == ISA_MIPS1)
14424 {
14425 /* nop */
2132e3a3 14426 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
14427 buf += 4;
14428 }
14429
14430 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
14431 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
14432
14433 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 14434 4, &exp, FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
14435 fixp->fx_file = fragp->fr_file;
14436 fixp->fx_line = fragp->fr_line;
b34976b6 14437
2132e3a3 14438 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
14439 buf += 4;
14440
14441 /* j(al)r $at. */
14442 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14443 insn = 0x0020f809;
14444 else
14445 insn = 0x00200008;
14446
2132e3a3 14447 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
14448 buf += 4;
14449 }
14450 }
14451
9c2799c2 14452 gas_assert (buf == (bfd_byte *)fragp->fr_literal
4a6a3df4
AO
14453 + fragp->fr_fix + fragp->fr_var);
14454
14455 fragp->fr_fix += fragp->fr_var;
14456
14457 return;
14458 }
14459
252b5132
RH
14460 if (RELAX_MIPS16_P (fragp->fr_subtype))
14461 {
14462 int type;
3994f87e 14463 const struct mips16_immed_operand *op;
b34976b6 14464 bfd_boolean small, ext;
252b5132
RH
14465 offsetT val;
14466 bfd_byte *buf;
14467 unsigned long insn;
b34976b6 14468 bfd_boolean use_extend;
252b5132
RH
14469 unsigned short extend;
14470
14471 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
14472 op = mips16_immed_operands;
14473 while (op->type != type)
14474 ++op;
14475
14476 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14477 {
b34976b6
AM
14478 small = FALSE;
14479 ext = TRUE;
252b5132
RH
14480 }
14481 else
14482 {
b34976b6
AM
14483 small = TRUE;
14484 ext = FALSE;
252b5132
RH
14485 }
14486
6386f3a7 14487 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
14488 val = S_GET_VALUE (fragp->fr_symbol);
14489 if (op->pcrel)
14490 {
14491 addressT addr;
14492
14493 addr = fragp->fr_address + fragp->fr_fix;
14494
14495 /* The rules for the base address of a PC relative reloc are
14496 complicated; see mips16_extended_frag. */
14497 if (type == 'p' || type == 'q')
14498 {
14499 addr += 2;
14500 if (ext)
14501 addr += 2;
14502 /* Ignore the low bit in the target, since it will be
14503 set for a text label. */
14504 if ((val & 1) != 0)
14505 --val;
14506 }
14507 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
14508 addr -= 4;
14509 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
14510 addr -= 2;
14511
14512 addr &= ~ (addressT) ((1 << op->shift) - 1);
14513 val -= addr;
14514
14515 /* Make sure the section winds up with the alignment we have
14516 assumed. */
14517 if (op->shift > 0)
14518 record_alignment (asec, op->shift);
14519 }
14520
14521 if (ext
14522 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
14523 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
14524 as_warn_where (fragp->fr_file, fragp->fr_line,
14525 _("extended instruction in delay slot"));
14526
14527 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
14528
14529 if (target_big_endian)
14530 insn = bfd_getb16 (buf);
14531 else
14532 insn = bfd_getl16 (buf);
14533
14534 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
14535 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
14536 small, ext, &insn, &use_extend, &extend);
14537
14538 if (use_extend)
14539 {
2132e3a3 14540 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
14541 fragp->fr_fix += 2;
14542 buf += 2;
14543 }
14544
2132e3a3 14545 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
14546 fragp->fr_fix += 2;
14547 buf += 2;
14548 }
14549 else
14550 {
4d7206a2
RS
14551 int first, second;
14552 fixS *fixp;
252b5132 14553
4d7206a2
RS
14554 first = RELAX_FIRST (fragp->fr_subtype);
14555 second = RELAX_SECOND (fragp->fr_subtype);
14556 fixp = (fixS *) fragp->fr_opcode;
252b5132 14557
584892a6
RS
14558 /* Possibly emit a warning if we've chosen the longer option. */
14559 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
14560 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
14561 {
14562 const char *msg = macro_warning (fragp->fr_subtype);
14563 if (msg != 0)
520725ea 14564 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
584892a6
RS
14565 }
14566
4d7206a2
RS
14567 /* Go through all the fixups for the first sequence. Disable them
14568 (by marking them as done) if we're going to use the second
14569 sequence instead. */
14570 while (fixp
14571 && fixp->fx_frag == fragp
14572 && fixp->fx_where < fragp->fr_fix - second)
14573 {
14574 if (fragp->fr_subtype & RELAX_USE_SECOND)
14575 fixp->fx_done = 1;
14576 fixp = fixp->fx_next;
14577 }
252b5132 14578
4d7206a2
RS
14579 /* Go through the fixups for the second sequence. Disable them if
14580 we're going to use the first sequence, otherwise adjust their
14581 addresses to account for the relaxation. */
14582 while (fixp && fixp->fx_frag == fragp)
14583 {
14584 if (fragp->fr_subtype & RELAX_USE_SECOND)
14585 fixp->fx_where -= first;
14586 else
14587 fixp->fx_done = 1;
14588 fixp = fixp->fx_next;
14589 }
14590
14591 /* Now modify the frag contents. */
14592 if (fragp->fr_subtype & RELAX_USE_SECOND)
14593 {
14594 char *start;
14595
14596 start = fragp->fr_literal + fragp->fr_fix - first - second;
14597 memmove (start, start + first, second);
14598 fragp->fr_fix -= first;
14599 }
14600 else
14601 fragp->fr_fix -= second;
252b5132
RH
14602 }
14603}
14604
14605#ifdef OBJ_ELF
14606
14607/* This function is called after the relocs have been generated.
14608 We've been storing mips16 text labels as odd. Here we convert them
14609 back to even for the convenience of the debugger. */
14610
14611void
17a2f251 14612mips_frob_file_after_relocs (void)
252b5132
RH
14613{
14614 asymbol **syms;
14615 unsigned int count, i;
14616
f43abd2b 14617 if (!IS_ELF)
252b5132
RH
14618 return;
14619
14620 syms = bfd_get_outsymbols (stdoutput);
14621 count = bfd_get_symcount (stdoutput);
14622 for (i = 0; i < count; i++, syms++)
14623 {
30c09090 14624 if (ELF_ST_IS_MIPS16 (elf_symbol (*syms)->internal_elf_sym.st_other)
252b5132
RH
14625 && ((*syms)->value & 1) != 0)
14626 {
14627 (*syms)->value &= ~1;
14628 /* If the symbol has an odd size, it was probably computed
14629 incorrectly, so adjust that as well. */
14630 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
14631 ++elf_symbol (*syms)->internal_elf_sym.st_size;
14632 }
14633 }
14634}
14635
14636#endif
14637
14638/* This function is called whenever a label is defined. It is used
14639 when handling branch delays; if a branch has a label, we assume we
14640 can not move it. */
14641
14642void
17a2f251 14643mips_define_label (symbolS *sym)
252b5132 14644{
a8dbcb85 14645 segment_info_type *si = seg_info (now_seg);
252b5132
RH
14646 struct insn_label_list *l;
14647
14648 if (free_insn_labels == NULL)
14649 l = (struct insn_label_list *) xmalloc (sizeof *l);
14650 else
14651 {
14652 l = free_insn_labels;
14653 free_insn_labels = l->next;
14654 }
14655
14656 l->label = sym;
a8dbcb85
TS
14657 l->next = si->label_list;
14658 si->label_list = l;
07a53e5c
RH
14659
14660#ifdef OBJ_ELF
14661 dwarf2_emit_label (sym);
14662#endif
252b5132
RH
14663}
14664\f
14665#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14666
14667/* Some special processing for a MIPS ELF file. */
14668
14669void
17a2f251 14670mips_elf_final_processing (void)
252b5132
RH
14671{
14672 /* Write out the register information. */
316f5878 14673 if (mips_abi != N64_ABI)
252b5132
RH
14674 {
14675 Elf32_RegInfo s;
14676
14677 s.ri_gprmask = mips_gprmask;
14678 s.ri_cprmask[0] = mips_cprmask[0];
14679 s.ri_cprmask[1] = mips_cprmask[1];
14680 s.ri_cprmask[2] = mips_cprmask[2];
14681 s.ri_cprmask[3] = mips_cprmask[3];
14682 /* The gp_value field is set by the MIPS ELF backend. */
14683
14684 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14685 ((Elf32_External_RegInfo *)
14686 mips_regmask_frag));
14687 }
14688 else
14689 {
14690 Elf64_Internal_RegInfo s;
14691
14692 s.ri_gprmask = mips_gprmask;
14693 s.ri_pad = 0;
14694 s.ri_cprmask[0] = mips_cprmask[0];
14695 s.ri_cprmask[1] = mips_cprmask[1];
14696 s.ri_cprmask[2] = mips_cprmask[2];
14697 s.ri_cprmask[3] = mips_cprmask[3];
14698 /* The gp_value field is set by the MIPS ELF backend. */
14699
14700 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14701 ((Elf64_External_RegInfo *)
14702 mips_regmask_frag));
14703 }
14704
14705 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
14706 sort of BFD interface for this. */
14707 if (mips_any_noreorder)
14708 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14709 if (mips_pic != NO_PIC)
143d77c5 14710 {
252b5132 14711 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
14712 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14713 }
14714 if (mips_abicalls)
14715 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 14716
98d3f06f 14717 /* Set MIPS ELF flags for ASEs. */
74cd071d
CF
14718 /* We may need to define a new flag for DSP ASE, and set this flag when
14719 file_ase_dsp is true. */
8b082fb1 14720 /* Same for DSP R2. */
ef2e4d86
CF
14721 /* We may need to define a new flag for MT ASE, and set this flag when
14722 file_ase_mt is true. */
a4672219
TS
14723 if (file_ase_mips16)
14724 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
14725#if 0 /* XXX FIXME */
14726 if (file_ase_mips3d)
14727 elf_elfheader (stdoutput)->e_flags |= ???;
14728#endif
deec1734
CD
14729 if (file_ase_mdmx)
14730 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 14731
bdaaa2e1 14732 /* Set the MIPS ELF ABI flags. */
316f5878 14733 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 14734 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 14735 else if (mips_abi == O64_ABI)
252b5132 14736 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 14737 else if (mips_abi == EABI_ABI)
252b5132 14738 {
316f5878 14739 if (!file_mips_gp32)
252b5132
RH
14740 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14741 else
14742 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14743 }
316f5878 14744 else if (mips_abi == N32_ABI)
be00bddd
TS
14745 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14746
c9914766 14747 /* Nothing to do for N64_ABI. */
252b5132
RH
14748
14749 if (mips_32bitmode)
14750 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08
TS
14751
14752#if 0 /* XXX FIXME */
14753 /* 32 bit code with 64 bit FP registers. */
14754 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
14755 elf_elfheader (stdoutput)->e_flags |= ???;
14756#endif
252b5132
RH
14757}
14758
14759#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14760\f
beae10d5 14761typedef struct proc {
9b2f1d35
EC
14762 symbolS *func_sym;
14763 symbolS *func_end_sym;
beae10d5
KH
14764 unsigned long reg_mask;
14765 unsigned long reg_offset;
14766 unsigned long fpreg_mask;
14767 unsigned long fpreg_offset;
14768 unsigned long frame_offset;
14769 unsigned long frame_reg;
14770 unsigned long pc_reg;
14771} procS;
252b5132
RH
14772
14773static procS cur_proc;
14774static procS *cur_proc_ptr;
14775static int numprocs;
14776
742a56fe
RS
14777/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1" and a normal
14778 nop as "0". */
14779
14780char
14781mips_nop_opcode (void)
14782{
14783 return seg_info (now_seg)->tc_segment_info_data.mips16;
14784}
14785
14786/* Fill in an rs_align_code fragment. This only needs to do something
14787 for MIPS16 code, where 0 is not a nop. */
a19d8eb0 14788
0a9ef439 14789void
17a2f251 14790mips_handle_align (fragS *fragp)
a19d8eb0 14791{
742a56fe
RS
14792 char *p;
14793
0a9ef439
RH
14794 if (fragp->fr_type != rs_align_code)
14795 return;
14796
742a56fe
RS
14797 p = fragp->fr_literal + fragp->fr_fix;
14798 if (*p)
a19d8eb0 14799 {
0a9ef439 14800 int bytes;
a19d8eb0 14801
0a9ef439 14802 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
0a9ef439
RH
14803 if (bytes & 1)
14804 {
14805 *p++ = 0;
f9419b05 14806 fragp->fr_fix++;
0a9ef439 14807 }
742a56fe 14808 md_number_to_chars (p, mips16_nop_insn.insn_opcode, 2);
0a9ef439 14809 fragp->fr_var = 2;
a19d8eb0 14810 }
a19d8eb0
CP
14811}
14812
252b5132 14813static void
17a2f251 14814md_obj_begin (void)
252b5132
RH
14815{
14816}
14817
14818static void
17a2f251 14819md_obj_end (void)
252b5132 14820{
54f4ddb3 14821 /* Check for premature end, nesting errors, etc. */
252b5132 14822 if (cur_proc_ptr)
9a41af64 14823 as_warn (_("missing .end at end of assembly"));
252b5132
RH
14824}
14825
14826static long
17a2f251 14827get_number (void)
252b5132
RH
14828{
14829 int negative = 0;
14830 long val = 0;
14831
14832 if (*input_line_pointer == '-')
14833 {
14834 ++input_line_pointer;
14835 negative = 1;
14836 }
3882b010 14837 if (!ISDIGIT (*input_line_pointer))
956cd1d6 14838 as_bad (_("expected simple number"));
252b5132
RH
14839 if (input_line_pointer[0] == '0')
14840 {
14841 if (input_line_pointer[1] == 'x')
14842 {
14843 input_line_pointer += 2;
3882b010 14844 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
14845 {
14846 val <<= 4;
14847 val |= hex_value (*input_line_pointer++);
14848 }
14849 return negative ? -val : val;
14850 }
14851 else
14852 {
14853 ++input_line_pointer;
3882b010 14854 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14855 {
14856 val <<= 3;
14857 val |= *input_line_pointer++ - '0';
14858 }
14859 return negative ? -val : val;
14860 }
14861 }
3882b010 14862 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
14863 {
14864 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14865 *input_line_pointer, *input_line_pointer);
956cd1d6 14866 as_warn (_("invalid number"));
252b5132
RH
14867 return -1;
14868 }
3882b010 14869 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14870 {
14871 val *= 10;
14872 val += *input_line_pointer++ - '0';
14873 }
14874 return negative ? -val : val;
14875}
14876
14877/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
14878 is an initial number which is the ECOFF file index. In the non-ECOFF
14879 case .file implies DWARF-2. */
14880
14881static void
17a2f251 14882s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 14883{
ecb4347a
DJ
14884 static int first_file_directive = 0;
14885
c5dd6aab
DJ
14886 if (ECOFF_DEBUGGING)
14887 {
14888 get_number ();
14889 s_app_file (0);
14890 }
14891 else
ecb4347a
DJ
14892 {
14893 char *filename;
14894
14895 filename = dwarf2_directive_file (0);
14896
14897 /* Versions of GCC up to 3.1 start files with a ".file"
14898 directive even for stabs output. Make sure that this
14899 ".file" is handled. Note that you need a version of GCC
14900 after 3.1 in order to support DWARF-2 on MIPS. */
14901 if (filename != NULL && ! first_file_directive)
14902 {
14903 (void) new_logical_line (filename, -1);
c04f5787 14904 s_app_file_string (filename, 0);
ecb4347a
DJ
14905 }
14906 first_file_directive = 1;
14907 }
c5dd6aab
DJ
14908}
14909
14910/* The .loc directive, implying DWARF-2. */
252b5132
RH
14911
14912static void
17a2f251 14913s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 14914{
c5dd6aab
DJ
14915 if (!ECOFF_DEBUGGING)
14916 dwarf2_directive_loc (0);
252b5132
RH
14917}
14918
252b5132
RH
14919/* The .end directive. */
14920
14921static void
17a2f251 14922s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
14923{
14924 symbolS *p;
252b5132 14925
7a621144
DJ
14926 /* Following functions need their own .frame and .cprestore directives. */
14927 mips_frame_reg_valid = 0;
14928 mips_cprestore_valid = 0;
14929
252b5132
RH
14930 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14931 {
14932 p = get_symbol ();
14933 demand_empty_rest_of_line ();
14934 }
14935 else
14936 p = NULL;
14937
14949570 14938 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14939 as_warn (_(".end not in text section"));
14940
14941 if (!cur_proc_ptr)
14942 {
14943 as_warn (_(".end directive without a preceding .ent directive."));
14944 demand_empty_rest_of_line ();
14945 return;
14946 }
14947
14948 if (p != NULL)
14949 {
9c2799c2 14950 gas_assert (S_GET_NAME (p));
9b2f1d35 14951 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 14952 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
14953
14954 if (debug_type == DEBUG_STABS)
14955 stabs_generate_asm_endfunc (S_GET_NAME (p),
14956 S_GET_NAME (p));
252b5132
RH
14957 }
14958 else
14959 as_warn (_(".end directive missing or unknown symbol"));
14960
2132e3a3 14961#ifdef OBJ_ELF
9b2f1d35
EC
14962 /* Create an expression to calculate the size of the function. */
14963 if (p && cur_proc_ptr)
14964 {
14965 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
14966 expressionS *exp = xmalloc (sizeof (expressionS));
14967
14968 obj->size = exp;
14969 exp->X_op = O_subtract;
14970 exp->X_add_symbol = symbol_temp_new_now ();
14971 exp->X_op_symbol = p;
14972 exp->X_add_number = 0;
14973
14974 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
14975 }
14976
ecb4347a 14977 /* Generate a .pdr section. */
f43abd2b 14978 if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
14979 {
14980 segT saved_seg = now_seg;
14981 subsegT saved_subseg = now_subseg;
14982 valueT dot;
14983 expressionS exp;
14984 char *fragp;
252b5132 14985
ecb4347a 14986 dot = frag_now_fix ();
252b5132
RH
14987
14988#ifdef md_flush_pending_output
ecb4347a 14989 md_flush_pending_output ();
252b5132
RH
14990#endif
14991
9c2799c2 14992 gas_assert (pdr_seg);
ecb4347a 14993 subseg_set (pdr_seg, 0);
252b5132 14994
ecb4347a
DJ
14995 /* Write the symbol. */
14996 exp.X_op = O_symbol;
14997 exp.X_add_symbol = p;
14998 exp.X_add_number = 0;
14999 emit_expr (&exp, 4);
252b5132 15000
ecb4347a 15001 fragp = frag_more (7 * 4);
252b5132 15002
17a2f251
TS
15003 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
15004 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
15005 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
15006 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
15007 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
15008 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
15009 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 15010
ecb4347a
DJ
15011 subseg_set (saved_seg, saved_subseg);
15012 }
15013#endif /* OBJ_ELF */
252b5132
RH
15014
15015 cur_proc_ptr = NULL;
15016}
15017
15018/* The .aent and .ent directives. */
15019
15020static void
17a2f251 15021s_mips_ent (int aent)
252b5132 15022{
252b5132 15023 symbolS *symbolP;
252b5132
RH
15024
15025 symbolP = get_symbol ();
15026 if (*input_line_pointer == ',')
f9419b05 15027 ++input_line_pointer;
252b5132 15028 SKIP_WHITESPACE ();
3882b010 15029 if (ISDIGIT (*input_line_pointer)
d9a62219 15030 || *input_line_pointer == '-')
874e8986 15031 get_number ();
252b5132 15032
14949570 15033 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
15034 as_warn (_(".ent or .aent not in text section."));
15035
15036 if (!aent && cur_proc_ptr)
9a41af64 15037 as_warn (_("missing .end"));
252b5132
RH
15038
15039 if (!aent)
15040 {
7a621144
DJ
15041 /* This function needs its own .frame and .cprestore directives. */
15042 mips_frame_reg_valid = 0;
15043 mips_cprestore_valid = 0;
15044
252b5132
RH
15045 cur_proc_ptr = &cur_proc;
15046 memset (cur_proc_ptr, '\0', sizeof (procS));
15047
9b2f1d35 15048 cur_proc_ptr->func_sym = symbolP;
252b5132 15049
49309057 15050 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 15051
f9419b05 15052 ++numprocs;
ecb4347a
DJ
15053
15054 if (debug_type == DEBUG_STABS)
15055 stabs_generate_asm_func (S_GET_NAME (symbolP),
15056 S_GET_NAME (symbolP));
252b5132
RH
15057 }
15058
15059 demand_empty_rest_of_line ();
15060}
15061
15062/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 15063 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 15064 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 15065 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
15066 symbol table (in the mdebug section). */
15067
15068static void
17a2f251 15069s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 15070{
ecb4347a 15071#ifdef OBJ_ELF
f43abd2b 15072 if (IS_ELF && !ECOFF_DEBUGGING)
ecb4347a
DJ
15073 {
15074 long val;
252b5132 15075
ecb4347a
DJ
15076 if (cur_proc_ptr == (procS *) NULL)
15077 {
15078 as_warn (_(".frame outside of .ent"));
15079 demand_empty_rest_of_line ();
15080 return;
15081 }
252b5132 15082
ecb4347a
DJ
15083 cur_proc_ptr->frame_reg = tc_get_register (1);
15084
15085 SKIP_WHITESPACE ();
15086 if (*input_line_pointer++ != ','
15087 || get_absolute_expression_and_terminator (&val) != ',')
15088 {
15089 as_warn (_("Bad .frame directive"));
15090 --input_line_pointer;
15091 demand_empty_rest_of_line ();
15092 return;
15093 }
252b5132 15094
ecb4347a
DJ
15095 cur_proc_ptr->frame_offset = val;
15096 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 15097
252b5132 15098 demand_empty_rest_of_line ();
252b5132 15099 }
ecb4347a
DJ
15100 else
15101#endif /* OBJ_ELF */
15102 s_ignore (ignore);
252b5132
RH
15103}
15104
bdaaa2e1
KH
15105/* The .fmask and .mask directives. If the mdebug section is present
15106 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 15107 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 15108 information correctly. We can't use the ecoff routines because they
252b5132
RH
15109 make reference to the ecoff symbol table (in the mdebug section). */
15110
15111static void
17a2f251 15112s_mips_mask (int reg_type)
252b5132 15113{
ecb4347a 15114#ifdef OBJ_ELF
f43abd2b 15115 if (IS_ELF && !ECOFF_DEBUGGING)
252b5132 15116 {
ecb4347a 15117 long mask, off;
252b5132 15118
ecb4347a
DJ
15119 if (cur_proc_ptr == (procS *) NULL)
15120 {
15121 as_warn (_(".mask/.fmask outside of .ent"));
15122 demand_empty_rest_of_line ();
15123 return;
15124 }
252b5132 15125
ecb4347a
DJ
15126 if (get_absolute_expression_and_terminator (&mask) != ',')
15127 {
15128 as_warn (_("Bad .mask/.fmask directive"));
15129 --input_line_pointer;
15130 demand_empty_rest_of_line ();
15131 return;
15132 }
252b5132 15133
ecb4347a
DJ
15134 off = get_absolute_expression ();
15135
15136 if (reg_type == 'F')
15137 {
15138 cur_proc_ptr->fpreg_mask = mask;
15139 cur_proc_ptr->fpreg_offset = off;
15140 }
15141 else
15142 {
15143 cur_proc_ptr->reg_mask = mask;
15144 cur_proc_ptr->reg_offset = off;
15145 }
15146
15147 demand_empty_rest_of_line ();
252b5132
RH
15148 }
15149 else
ecb4347a
DJ
15150#endif /* OBJ_ELF */
15151 s_ignore (reg_type);
252b5132
RH
15152}
15153
316f5878
RS
15154/* A table describing all the processors gas knows about. Names are
15155 matched in the order listed.
e7af610e 15156
316f5878
RS
15157 To ease comparison, please keep this table in the same order as
15158 gcc's mips_cpu_info_table[]. */
e972090a
NC
15159static const struct mips_cpu_info mips_cpu_info_table[] =
15160{
316f5878 15161 /* Entries for generic ISAs */
ad3fea08
TS
15162 { "mips1", MIPS_CPU_IS_ISA, ISA_MIPS1, CPU_R3000 },
15163 { "mips2", MIPS_CPU_IS_ISA, ISA_MIPS2, CPU_R6000 },
15164 { "mips3", MIPS_CPU_IS_ISA, ISA_MIPS3, CPU_R4000 },
15165 { "mips4", MIPS_CPU_IS_ISA, ISA_MIPS4, CPU_R8000 },
15166 { "mips5", MIPS_CPU_IS_ISA, ISA_MIPS5, CPU_MIPS5 },
15167 { "mips32", MIPS_CPU_IS_ISA, ISA_MIPS32, CPU_MIPS32 },
15168 { "mips32r2", MIPS_CPU_IS_ISA, ISA_MIPS32R2, CPU_MIPS32R2 },
15169 { "mips64", MIPS_CPU_IS_ISA, ISA_MIPS64, CPU_MIPS64 },
15170 { "mips64r2", MIPS_CPU_IS_ISA, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
15171
15172 /* MIPS I */
ad3fea08
TS
15173 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
15174 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
15175 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
15176
15177 /* MIPS II */
ad3fea08 15178 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
15179
15180 /* MIPS III */
ad3fea08
TS
15181 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
15182 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
15183 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
15184 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
15185 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
15186 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
15187 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
15188 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
15189 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
15190 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
15191 { "orion", 0, ISA_MIPS3, CPU_R4600 },
15192 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
b15591bb
AN
15193 /* ST Microelectronics Loongson 2E and 2F cores */
15194 { "loongson2e", 0, ISA_MIPS3, CPU_LOONGSON_2E },
15195 { "loongson2f", 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
15196
15197 /* MIPS IV */
ad3fea08
TS
15198 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
15199 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
15200 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
3aa3176b
TS
15201 { "r14000", 0, ISA_MIPS4, CPU_R14000 },
15202 { "r16000", 0, ISA_MIPS4, CPU_R16000 },
ad3fea08
TS
15203 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
15204 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
15205 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
15206 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
15207 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
15208 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
15209 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
15210 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
15211 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
15212 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
15213
15214 /* MIPS 32 */
ad3fea08
TS
15215 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
15216 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
15217 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
15218 { "4ksc", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
15219
15220 /* MIPS 32 Release 2 */
15221 { "4kec", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
15222 { "4kem", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
15223 { "4kep", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
15224 { "4ksd", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
15225 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
15226 { "m4kp", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 15227 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 15228 { "24kf2_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 15229 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
15230 { "24kf1_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
15231 /* Deprecated forms of the above. */
15232 { "24kfx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 15233 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 15234 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
ad3fea08 15235 { "24kec", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 15236 { "24kef2_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 15237 { "24kef", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
15238 { "24kef1_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
15239 /* Deprecated forms of the above. */
15240 { "24kefx", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
65263ce3 15241 { "24kex", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 15242 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
a360e743
TS
15243 { "34kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15244 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
15245 { "34kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15246 ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
15247 { "34kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15248 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
15249 { "34kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15250 ISA_MIPS32R2, CPU_MIPS32R2 },
15251 /* Deprecated forms of the above. */
15252 { "34kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15253 ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
15254 { "34kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15255 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
15256 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
15257 { "74kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
15258 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
15259 { "74kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
15260 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
15261 { "74kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
15262 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
15263 { "74kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
15264 ISA_MIPS32R2, CPU_MIPS32R2 },
15265 { "74kf3_2", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
15266 ISA_MIPS32R2, CPU_MIPS32R2 },
15267 /* Deprecated forms of the above. */
15268 { "74kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
15269 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
15270 { "74kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
15271 ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a
SL
15272 /* 1004K cores are multiprocessor versions of the 34K. */
15273 { "1004kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15274 ISA_MIPS32R2, CPU_MIPS32R2 },
15275 { "1004kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15276 ISA_MIPS32R2, CPU_MIPS32R2 },
15277 { "1004kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15278 ISA_MIPS32R2, CPU_MIPS32R2 },
15279 { "1004kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
15280 ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 15281
316f5878 15282 /* MIPS 64 */
ad3fea08
TS
15283 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
15284 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
15285 { "20kc", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
7764b395 15286 { "25kf", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 15287
c7a23324 15288 /* Broadcom SB-1 CPU core */
65263ce3
TS
15289 { "sb1", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
15290 ISA_MIPS64, CPU_SB1 },
1e85aad8
JW
15291 /* Broadcom SB-1A CPU core */
15292 { "sb1a", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
15293 ISA_MIPS64, CPU_SB1 },
e7af610e 15294
ed163775
MR
15295 /* MIPS 64 Release 2 */
15296
967344c6
AN
15297 /* Cavium Networks Octeon CPU core */
15298 { "octeon", 0, ISA_MIPS64R2, CPU_OCTEON },
15299
52b6b6b9
JM
15300 /* RMI Xlr */
15301 { "xlr", 0, ISA_MIPS64, CPU_XLR },
15302
316f5878
RS
15303 /* End marker */
15304 { NULL, 0, 0, 0 }
15305};
e7af610e 15306
84ea6cf2 15307
316f5878
RS
15308/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
15309 with a final "000" replaced by "k". Ignore case.
e7af610e 15310
316f5878 15311 Note: this function is shared between GCC and GAS. */
c6c98b38 15312
b34976b6 15313static bfd_boolean
17a2f251 15314mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
15315{
15316 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
15317 given++, canonical++;
15318
15319 return ((*given == 0 && *canonical == 0)
15320 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
15321}
15322
15323
15324/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
15325 CPU name. We've traditionally allowed a lot of variation here.
15326
15327 Note: this function is shared between GCC and GAS. */
15328
b34976b6 15329static bfd_boolean
17a2f251 15330mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
15331{
15332 /* First see if the name matches exactly, or with a final "000"
15333 turned into "k". */
15334 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 15335 return TRUE;
316f5878
RS
15336
15337 /* If not, try comparing based on numerical designation alone.
15338 See if GIVEN is an unadorned number, or 'r' followed by a number. */
15339 if (TOLOWER (*given) == 'r')
15340 given++;
15341 if (!ISDIGIT (*given))
b34976b6 15342 return FALSE;
316f5878
RS
15343
15344 /* Skip over some well-known prefixes in the canonical name,
15345 hoping to find a number there too. */
15346 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
15347 canonical += 2;
15348 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
15349 canonical += 2;
15350 else if (TOLOWER (canonical[0]) == 'r')
15351 canonical += 1;
15352
15353 return mips_strict_matching_cpu_name_p (canonical, given);
15354}
15355
15356
15357/* Parse an option that takes the name of a processor as its argument.
15358 OPTION is the name of the option and CPU_STRING is the argument.
15359 Return the corresponding processor enumeration if the CPU_STRING is
15360 recognized, otherwise report an error and return null.
15361
15362 A similar function exists in GCC. */
e7af610e
NC
15363
15364static const struct mips_cpu_info *
17a2f251 15365mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 15366{
316f5878 15367 const struct mips_cpu_info *p;
e7af610e 15368
316f5878
RS
15369 /* 'from-abi' selects the most compatible architecture for the given
15370 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
15371 EABIs, we have to decide whether we're using the 32-bit or 64-bit
15372 version. Look first at the -mgp options, if given, otherwise base
15373 the choice on MIPS_DEFAULT_64BIT.
e7af610e 15374
316f5878
RS
15375 Treat NO_ABI like the EABIs. One reason to do this is that the
15376 plain 'mips' and 'mips64' configs have 'from-abi' as their default
15377 architecture. This code picks MIPS I for 'mips' and MIPS III for
15378 'mips64', just as we did in the days before 'from-abi'. */
15379 if (strcasecmp (cpu_string, "from-abi") == 0)
15380 {
15381 if (ABI_NEEDS_32BIT_REGS (mips_abi))
15382 return mips_cpu_info_from_isa (ISA_MIPS1);
15383
15384 if (ABI_NEEDS_64BIT_REGS (mips_abi))
15385 return mips_cpu_info_from_isa (ISA_MIPS3);
15386
15387 if (file_mips_gp32 >= 0)
15388 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
15389
15390 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
15391 ? ISA_MIPS3
15392 : ISA_MIPS1);
15393 }
15394
15395 /* 'default' has traditionally been a no-op. Probably not very useful. */
15396 if (strcasecmp (cpu_string, "default") == 0)
15397 return 0;
15398
15399 for (p = mips_cpu_info_table; p->name != 0; p++)
15400 if (mips_matching_cpu_name_p (p->name, cpu_string))
15401 return p;
15402
20203fb9 15403 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
316f5878 15404 return 0;
e7af610e
NC
15405}
15406
316f5878
RS
15407/* Return the canonical processor information for ISA (a member of the
15408 ISA_MIPS* enumeration). */
15409
e7af610e 15410static const struct mips_cpu_info *
17a2f251 15411mips_cpu_info_from_isa (int isa)
e7af610e
NC
15412{
15413 int i;
15414
15415 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 15416 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 15417 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
15418 return (&mips_cpu_info_table[i]);
15419
e972090a 15420 return NULL;
e7af610e 15421}
fef14a42
TS
15422
15423static const struct mips_cpu_info *
17a2f251 15424mips_cpu_info_from_arch (int arch)
fef14a42
TS
15425{
15426 int i;
15427
15428 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
15429 if (arch == mips_cpu_info_table[i].cpu)
15430 return (&mips_cpu_info_table[i]);
15431
15432 return NULL;
15433}
316f5878
RS
15434\f
15435static void
17a2f251 15436show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
15437{
15438 if (*first_p)
15439 {
15440 fprintf (stream, "%24s", "");
15441 *col_p = 24;
15442 }
15443 else
15444 {
15445 fprintf (stream, ", ");
15446 *col_p += 2;
15447 }
e7af610e 15448
316f5878
RS
15449 if (*col_p + strlen (string) > 72)
15450 {
15451 fprintf (stream, "\n%24s", "");
15452 *col_p = 24;
15453 }
15454
15455 fprintf (stream, "%s", string);
15456 *col_p += strlen (string);
15457
15458 *first_p = 0;
15459}
15460
15461void
17a2f251 15462md_show_usage (FILE *stream)
e7af610e 15463{
316f5878
RS
15464 int column, first;
15465 size_t i;
15466
15467 fprintf (stream, _("\
15468MIPS options:\n\
316f5878
RS
15469-EB generate big endian output\n\
15470-EL generate little endian output\n\
15471-g, -g2 do not remove unneeded NOPs or swap branches\n\
15472-G NUM allow referencing objects up to NUM bytes\n\
15473 implicitly with the gp register [default 8]\n"));
15474 fprintf (stream, _("\
15475-mips1 generate MIPS ISA I instructions\n\
15476-mips2 generate MIPS ISA II instructions\n\
15477-mips3 generate MIPS ISA III instructions\n\
15478-mips4 generate MIPS ISA IV instructions\n\
15479-mips5 generate MIPS ISA V instructions\n\
15480-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 15481-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 15482-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 15483-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
15484-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
15485
15486 first = 1;
e7af610e
NC
15487
15488 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
15489 show (stream, mips_cpu_info_table[i].name, &column, &first);
15490 show (stream, "from-abi", &column, &first);
15491 fputc ('\n', stream);
e7af610e 15492
316f5878
RS
15493 fprintf (stream, _("\
15494-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
15495-no-mCPU don't generate code specific to CPU.\n\
15496 For -mCPU and -no-mCPU, CPU must be one of:\n"));
15497
15498 first = 1;
15499
15500 show (stream, "3900", &column, &first);
15501 show (stream, "4010", &column, &first);
15502 show (stream, "4100", &column, &first);
15503 show (stream, "4650", &column, &first);
15504 fputc ('\n', stream);
15505
15506 fprintf (stream, _("\
15507-mips16 generate mips16 instructions\n\
15508-no-mips16 do not generate mips16 instructions\n"));
15509 fprintf (stream, _("\
e16bfa71
TS
15510-msmartmips generate smartmips instructions\n\
15511-mno-smartmips do not generate smartmips instructions\n"));
15512 fprintf (stream, _("\
74cd071d
CF
15513-mdsp generate DSP instructions\n\
15514-mno-dsp do not generate DSP instructions\n"));
15515 fprintf (stream, _("\
8b082fb1
TS
15516-mdspr2 generate DSP R2 instructions\n\
15517-mno-dspr2 do not generate DSP R2 instructions\n"));
15518 fprintf (stream, _("\
ef2e4d86
CF
15519-mmt generate MT instructions\n\
15520-mno-mt do not generate MT instructions\n"));
15521 fprintf (stream, _("\
d766e8ec 15522-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 15523-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 15524-mfix-24k insert a nop after ERET and DERET instructions\n\
316f5878
RS
15525-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
15526-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 15527-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
15528-O0 remove unneeded NOPs, do not swap branches\n\
15529-O remove unneeded NOPs and swap branches\n\
316f5878
RS
15530--trap, --no-break trap exception on div by 0 and mult overflow\n\
15531--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
15532 fprintf (stream, _("\
15533-mhard-float allow floating-point instructions\n\
15534-msoft-float do not allow floating-point instructions\n\
15535-msingle-float only allow 32-bit floating-point operations\n\
15536-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
15537--[no-]construct-floats [dis]allow floating point values to be constructed\n"
15538 ));
316f5878
RS
15539#ifdef OBJ_ELF
15540 fprintf (stream, _("\
15541-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 15542-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 15543-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 15544-non_shared do not generate code that can operate with DSOs\n\
316f5878 15545-xgot assume a 32 bit GOT\n\
dcd410fe 15546-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 15547-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 15548 position dependent (non shared) code\n\
316f5878
RS
15549-mabi=ABI create ABI conformant object file for:\n"));
15550
15551 first = 1;
15552
15553 show (stream, "32", &column, &first);
15554 show (stream, "o64", &column, &first);
15555 show (stream, "n32", &column, &first);
15556 show (stream, "64", &column, &first);
15557 show (stream, "eabi", &column, &first);
15558
15559 fputc ('\n', stream);
15560
15561 fprintf (stream, _("\
15562-32 create o32 ABI object file (default)\n\
15563-n32 create n32 ABI object file\n\
15564-64 create 64 ABI object file\n"));
15565#endif
e7af610e 15566}
14e777e0
KB
15567
15568enum dwarf2_format
413a266c 15569mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 15570{
369943fe 15571 if (HAVE_64BIT_SYMBOLS)
1de5b6a1
AO
15572 {
15573#ifdef TE_IRIX
15574 return dwarf2_format_64bit_irix;
15575#else
15576 return dwarf2_format_64bit;
15577#endif
15578 }
14e777e0
KB
15579 else
15580 return dwarf2_format_32bit;
15581}
73369e65
EC
15582
15583int
15584mips_dwarf2_addr_size (void)
15585{
6b6b3450 15586 if (HAVE_64BIT_OBJECTS)
73369e65 15587 return 8;
73369e65
EC
15588 else
15589 return 4;
15590}
5862107c
EC
15591
15592/* Standard calling conventions leave the CFA at SP on entry. */
15593void
15594mips_cfi_frame_initial_instructions (void)
15595{
15596 cfi_add_CFA_def_cfa_register (SP);
15597}
15598
707bfff6
TS
15599int
15600tc_mips_regname_to_dw2regnum (char *regname)
15601{
15602 unsigned int regnum = -1;
15603 unsigned int reg;
15604
15605 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
15606 regnum = reg;
15607
15608 return regnum;
15609}
This page took 1.988979 seconds and 4 git commands to generate.