]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-mips.c
gdb/
[thirdparty/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,
e407c74b 3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
c67a084a 4 Free Software Foundation, Inc.
252b5132
RH
5 Contributed by the OSF and Ralph Campbell.
6 Written by Keith Knowles and Ralph Campbell, working independently.
7 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
8 Support.
9
10 This file is part of GAS.
11
12 GAS is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
ec2655a6 14 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
15 any later version.
16
17 GAS is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
24 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
25 02110-1301, USA. */
252b5132
RH
26
27#include "as.h"
28#include "config.h"
29#include "subsegs.h"
3882b010 30#include "safe-ctype.h"
252b5132 31
252b5132
RH
32#include "opcode/mips.h"
33#include "itbl-ops.h"
c5dd6aab 34#include "dwarf2dbg.h"
5862107c 35#include "dw2gencfi.h"
252b5132
RH
36
37#ifdef DEBUG
38#define DBG(x) printf x
39#else
40#define DBG(x)
41#endif
42
43#ifdef OBJ_MAYBE_ELF
44/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
45static int mips_output_flavor (void);
46static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
47#undef OBJ_PROCESS_STAB
48#undef OUTPUT_FLAVOR
49#undef S_GET_ALIGN
50#undef S_GET_SIZE
51#undef S_SET_ALIGN
52#undef S_SET_SIZE
252b5132
RH
53#undef obj_frob_file
54#undef obj_frob_file_after_relocs
55#undef obj_frob_symbol
56#undef obj_pop_insert
57#undef obj_sec_sym_ok_for_reloc
58#undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60#include "obj-elf.h"
61/* Fix any of them that we actually care about. */
62#undef OUTPUT_FLAVOR
63#define OUTPUT_FLAVOR mips_output_flavor()
64#endif
65
66#if defined (OBJ_ELF)
67#include "elf/mips.h"
68#endif
69
70#ifndef ECOFF_DEBUGGING
71#define NO_ECOFF_DEBUGGING
72#define ECOFF_DEBUGGING 0
73#endif
74
ecb4347a
DJ
75int mips_flag_mdebug = -1;
76
dcd410fe
RO
77/* Control generation of .pdr sections. Off by default on IRIX: the native
78 linker doesn't know about and discards them, but relocations against them
79 remain, leading to rld crashes. */
80#ifdef TE_IRIX
81int mips_flag_pdr = FALSE;
82#else
83int mips_flag_pdr = TRUE;
84#endif
85
252b5132
RH
86#include "ecoff.h"
87
88#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89static char *mips_regmask_frag;
90#endif
91
85b51719 92#define ZERO 0
741fe287 93#define ATREG 1
df58fc94
RS
94#define S0 16
95#define S7 23
252b5132
RH
96#define TREG 24
97#define PIC_CALL_REG 25
98#define KT0 26
99#define KT1 27
100#define GP 28
101#define SP 29
102#define FP 30
103#define RA 31
104
105#define ILLEGAL_REG (32)
106
741fe287
MR
107#define AT mips_opts.at
108
252b5132
RH
109/* Allow override of standard little-endian ECOFF format. */
110
111#ifndef ECOFF_LITTLE_FORMAT
112#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
113#endif
114
115extern int target_big_endian;
116
252b5132 117/* The name of the readonly data section. */
4d0d148d 118#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
252b5132 119 ? ".rdata" \
056350c6
NC
120 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
121 ? ".rdata" \
252b5132
RH
122 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
123 ? ".rodata" \
124 : (abort (), ""))
125
a4e06468
RS
126/* Ways in which an instruction can be "appended" to the output. */
127enum append_method {
128 /* Just add it normally. */
129 APPEND_ADD,
130
131 /* Add it normally and then add a nop. */
132 APPEND_ADD_WITH_NOP,
133
134 /* Turn an instruction with a delay slot into a "compact" version. */
135 APPEND_ADD_COMPACT,
136
137 /* Insert the instruction before the last one. */
138 APPEND_SWAP
139};
140
47e39b9d
RS
141/* Information about an instruction, including its format, operands
142 and fixups. */
143struct mips_cl_insn
144{
145 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
146 const struct mips_opcode *insn_mo;
147
47e39b9d 148 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
149 a copy of INSN_MO->match with the operands filled in. If we have
150 decided to use an extended MIPS16 instruction, this includes the
151 extension. */
47e39b9d
RS
152 unsigned long insn_opcode;
153
154 /* The frag that contains the instruction. */
155 struct frag *frag;
156
157 /* The offset into FRAG of the first instruction byte. */
158 long where;
159
160 /* The relocs associated with the instruction, if any. */
161 fixS *fixp[3];
162
a38419a5
RS
163 /* True if this entry cannot be moved from its current position. */
164 unsigned int fixed_p : 1;
47e39b9d 165
708587a4 166 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
167 unsigned int noreorder_p : 1;
168
2fa15973
RS
169 /* True for mips16 instructions that jump to an absolute address. */
170 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
171
172 /* True if this instruction is complete. */
173 unsigned int complete_p : 1;
e407c74b
NC
174
175 /* True if this instruction is cleared from history by unconditional
176 branch. */
177 unsigned int cleared_p : 1;
47e39b9d
RS
178};
179
a325df1d
TS
180/* The ABI to use. */
181enum mips_abi_level
182{
183 NO_ABI = 0,
184 O32_ABI,
185 O64_ABI,
186 N32_ABI,
187 N64_ABI,
188 EABI_ABI
189};
190
191/* MIPS ABI we are using for this output file. */
316f5878 192static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 193
143d77c5
EC
194/* Whether or not we have code that can call pic code. */
195int mips_abicalls = FALSE;
196
aa6975fb
ILT
197/* Whether or not we have code which can be put into a shared
198 library. */
199static bfd_boolean mips_in_shared = TRUE;
200
252b5132
RH
201/* This is the set of options which may be modified by the .set
202 pseudo-op. We use a struct so that .set push and .set pop are more
203 reliable. */
204
e972090a
NC
205struct mips_set_options
206{
252b5132
RH
207 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
208 if it has not been initialized. Changed by `.set mipsN', and the
209 -mipsN command line option, and the default CPU. */
210 int isa;
1f25f5d3
CD
211 /* Enabled Application Specific Extensions (ASEs). These are set to -1
212 if they have not been initialized. Changed by `.set <asename>', by
213 command line options, and based on the default architecture. */
214 int ase_mips3d;
deec1734 215 int ase_mdmx;
e16bfa71 216 int ase_smartmips;
74cd071d 217 int ase_dsp;
8b082fb1 218 int ase_dspr2;
ef2e4d86 219 int ase_mt;
dec0624d 220 int ase_mcu;
b015e599 221 int ase_virt;
252b5132
RH
222 /* Whether we are assembling for the mips16 processor. 0 if we are
223 not, 1 if we are, and -1 if the value has not been initialized.
224 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
225 -nomips16 command line options, and the default CPU. */
226 int mips16;
df58fc94
RS
227 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
228 1 if we are, and -1 if the value has not been initialized. Changed
229 by `.set micromips' and `.set nomicromips', and the -mmicromips
230 and -mno-micromips command line options, and the default CPU. */
231 int micromips;
252b5132
RH
232 /* Non-zero if we should not reorder instructions. Changed by `.set
233 reorder' and `.set noreorder'. */
234 int noreorder;
741fe287
MR
235 /* Non-zero if we should not permit the register designated "assembler
236 temporary" to be used in instructions. The value is the register
237 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
238 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
239 unsigned int at;
252b5132
RH
240 /* Non-zero if we should warn when a macro instruction expands into
241 more than one machine instruction. Changed by `.set nomacro' and
242 `.set macro'. */
243 int warn_about_macros;
244 /* Non-zero if we should not move instructions. Changed by `.set
245 move', `.set volatile', `.set nomove', and `.set novolatile'. */
246 int nomove;
247 /* Non-zero if we should not optimize branches by moving the target
248 of the branch into the delay slot. Actually, we don't perform
249 this optimization anyhow. Changed by `.set bopt' and `.set
250 nobopt'. */
251 int nobopt;
252 /* Non-zero if we should not autoextend mips16 instructions.
253 Changed by `.set autoextend' and `.set noautoextend'. */
254 int noautoextend;
a325df1d
TS
255 /* Restrict general purpose registers and floating point registers
256 to 32 bit. This is initially determined when -mgp32 or -mfp32
257 is passed but can changed if the assembler code uses .set mipsN. */
258 int gp32;
259 int fp32;
fef14a42
TS
260 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
261 command line option, and the default CPU. */
262 int arch;
aed1a261
RS
263 /* True if ".set sym32" is in effect. */
264 bfd_boolean sym32;
037b32b9
AN
265 /* True if floating-point operations are not allowed. Changed by .set
266 softfloat or .set hardfloat, by command line options -msoft-float or
267 -mhard-float. The default is false. */
268 bfd_boolean soft_float;
269
270 /* True if only single-precision floating-point operations are allowed.
271 Changed by .set singlefloat or .set doublefloat, command-line options
272 -msingle-float or -mdouble-float. The default is false. */
273 bfd_boolean single_float;
252b5132
RH
274};
275
037b32b9
AN
276/* This is the struct we use to hold the current set of options. Note
277 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
278 -1 to indicate that they have not been initialized. */
279
a325df1d 280/* True if -mgp32 was passed. */
a8e8e863 281static int file_mips_gp32 = -1;
a325df1d
TS
282
283/* True if -mfp32 was passed. */
a8e8e863 284static int file_mips_fp32 = -1;
a325df1d 285
037b32b9
AN
286/* 1 if -msoft-float, 0 if -mhard-float. The default is 0. */
287static int file_mips_soft_float = 0;
288
289/* 1 if -msingle-float, 0 if -mdouble-float. The default is 0. */
290static int file_mips_single_float = 0;
252b5132 291
e972090a
NC
292static struct mips_set_options mips_opts =
293{
037b32b9
AN
294 /* isa */ ISA_UNKNOWN, /* ase_mips3d */ -1, /* ase_mdmx */ -1,
295 /* ase_smartmips */ 0, /* ase_dsp */ -1, /* ase_dspr2 */ -1, /* ase_mt */ -1,
b015e599
AP
296 /* ase_mcu */ -1, /* ase_virt */ -1, /* mips16 */ -1,/* micromips */ -1,
297 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
298 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* gp32 */ 0,
299 /* fp32 */ 0, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
300 /* soft_float */ FALSE, /* single_float */ FALSE
e7af610e 301};
252b5132
RH
302
303/* These variables are filled in with the masks of registers used.
304 The object format code reads them and puts them in the appropriate
305 place. */
306unsigned long mips_gprmask;
307unsigned long mips_cprmask[4];
308
309/* MIPS ISA we are using for this output file. */
e7af610e 310static int file_mips_isa = ISA_UNKNOWN;
252b5132 311
738f4d98 312/* True if any MIPS16 code was produced. */
a4672219
TS
313static int file_ase_mips16;
314
3994f87e
TS
315#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
316 || mips_opts.isa == ISA_MIPS32R2 \
317 || mips_opts.isa == ISA_MIPS64 \
318 || mips_opts.isa == ISA_MIPS64R2)
319
df58fc94
RS
320/* True if any microMIPS code was produced. */
321static int file_ase_micromips;
322
b12dd2e4
CF
323/* True if we want to create R_MIPS_JALR for jalr $25. */
324#ifdef TE_IRIX
1180b5a4 325#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 326#else
1180b5a4
RS
327/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
328 because there's no place for any addend, the only acceptable
329 expression is a bare symbol. */
330#define MIPS_JALR_HINT_P(EXPR) \
331 (!HAVE_IN_PLACE_ADDENDS \
332 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
333#endif
334
1f25f5d3
CD
335/* True if -mips3d was passed or implied by arguments passed on the
336 command line (e.g., by -march). */
337static int file_ase_mips3d;
338
deec1734
CD
339/* True if -mdmx was passed or implied by arguments passed on the
340 command line (e.g., by -march). */
341static int file_ase_mdmx;
342
e16bfa71
TS
343/* True if -msmartmips was passed or implied by arguments passed on the
344 command line (e.g., by -march). */
345static int file_ase_smartmips;
346
ad3fea08
TS
347#define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32 \
348 || mips_opts.isa == ISA_MIPS32R2)
e16bfa71 349
74cd071d
CF
350/* True if -mdsp was passed or implied by arguments passed on the
351 command line (e.g., by -march). */
352static int file_ase_dsp;
353
ad3fea08 354#define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2 \
03f66e8a
MR
355 || mips_opts.isa == ISA_MIPS64R2 \
356 || mips_opts.micromips)
ad3fea08 357
65263ce3
TS
358#define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
359
8b082fb1
TS
360/* True if -mdspr2 was passed or implied by arguments passed on the
361 command line (e.g., by -march). */
362static int file_ase_dspr2;
363
364#define ISA_SUPPORTS_DSPR2_ASE (mips_opts.isa == ISA_MIPS32R2 \
03f66e8a
MR
365 || mips_opts.isa == ISA_MIPS64R2 \
366 || mips_opts.micromips)
8b082fb1 367
ef2e4d86
CF
368/* True if -mmt was passed or implied by arguments passed on the
369 command line (e.g., by -march). */
370static int file_ase_mt;
371
ad3fea08
TS
372#define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2 \
373 || mips_opts.isa == ISA_MIPS64R2)
374
dec0624d 375#define ISA_SUPPORTS_MCU_ASE (mips_opts.isa == ISA_MIPS32R2 \
9ddc84cc
MR
376 || mips_opts.isa == ISA_MIPS64R2 \
377 || mips_opts.micromips)
dec0624d 378
b015e599
AP
379/* True if -mvirt was passed or implied by arguments passed on the
380 command line (e.g., by -march). */
381static int file_ase_virt;
382
383#define ISA_SUPPORTS_VIRT_ASE (mips_opts.isa == ISA_MIPS32R2 \
384 || mips_opts.isa == ISA_MIPS64R2)
385
386#define ISA_SUPPORTS_VIRT64_ASE (mips_opts.isa == ISA_MIPS64R2)
387
ec68c924 388/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 389static int file_mips_arch = CPU_UNKNOWN;
316f5878 390static const char *mips_arch_string;
ec68c924
EC
391
392/* The argument of the -mtune= flag. The architecture for which we
393 are optimizing. */
394static int mips_tune = CPU_UNKNOWN;
316f5878 395static const char *mips_tune_string;
ec68c924 396
316f5878 397/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
398static int mips_32bitmode = 0;
399
316f5878
RS
400/* True if the given ABI requires 32-bit registers. */
401#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
402
403/* Likewise 64-bit registers. */
707bfff6
TS
404#define ABI_NEEDS_64BIT_REGS(ABI) \
405 ((ABI) == N32_ABI \
406 || (ABI) == N64_ABI \
316f5878
RS
407 || (ABI) == O64_ABI)
408
ad3fea08 409/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
410#define ISA_HAS_64BIT_REGS(ISA) \
411 ((ISA) == ISA_MIPS3 \
412 || (ISA) == ISA_MIPS4 \
413 || (ISA) == ISA_MIPS5 \
414 || (ISA) == ISA_MIPS64 \
415 || (ISA) == ISA_MIPS64R2)
9ce8a5dd 416
ad3fea08
TS
417/* Return true if ISA supports 64 bit wide float registers. */
418#define ISA_HAS_64BIT_FPRS(ISA) \
419 ((ISA) == ISA_MIPS3 \
420 || (ISA) == ISA_MIPS4 \
421 || (ISA) == ISA_MIPS5 \
422 || (ISA) == ISA_MIPS32R2 \
423 || (ISA) == ISA_MIPS64 \
424 || (ISA) == ISA_MIPS64R2)
425
af7ee8bf
CD
426/* Return true if ISA supports 64-bit right rotate (dror et al.)
427 instructions. */
707bfff6 428#define ISA_HAS_DROR(ISA) \
df58fc94
RS
429 ((ISA) == ISA_MIPS64R2 \
430 || (mips_opts.micromips \
431 && ISA_HAS_64BIT_REGS (ISA)) \
432 )
af7ee8bf
CD
433
434/* Return true if ISA supports 32-bit right rotate (ror et al.)
435 instructions. */
707bfff6
TS
436#define ISA_HAS_ROR(ISA) \
437 ((ISA) == ISA_MIPS32R2 \
438 || (ISA) == ISA_MIPS64R2 \
df58fc94
RS
439 || mips_opts.ase_smartmips \
440 || mips_opts.micromips \
441 )
707bfff6 442
7455baf8
TS
443/* Return true if ISA supports single-precision floats in odd registers. */
444#define ISA_HAS_ODD_SINGLE_FPR(ISA) \
445 ((ISA) == ISA_MIPS32 \
446 || (ISA) == ISA_MIPS32R2 \
447 || (ISA) == ISA_MIPS64 \
448 || (ISA) == ISA_MIPS64R2)
af7ee8bf 449
ad3fea08
TS
450/* Return true if ISA supports move to/from high part of a 64-bit
451 floating-point register. */
452#define ISA_HAS_MXHC1(ISA) \
453 ((ISA) == ISA_MIPS32R2 \
454 || (ISA) == ISA_MIPS64R2)
455
e013f690 456#define HAVE_32BIT_GPRS \
ad3fea08 457 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 458
e013f690 459#define HAVE_32BIT_FPRS \
ad3fea08 460 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
ca4e0257 461
ad3fea08
TS
462#define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
463#define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
ca4e0257 464
316f5878 465#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 466
316f5878 467#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 468
3b91255e
RS
469/* True if relocations are stored in-place. */
470#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
471
aed1a261
RS
472/* The ABI-derived address size. */
473#define HAVE_64BIT_ADDRESSES \
474 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
475#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 476
aed1a261
RS
477/* The size of symbolic constants (i.e., expressions of the form
478 "SYMBOL" or "SYMBOL + OFFSET"). */
479#define HAVE_32BIT_SYMBOLS \
480 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
481#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 482
b7c7d6c1
TS
483/* Addresses are loaded in different ways, depending on the address size
484 in use. The n32 ABI Documentation also mandates the use of additions
485 with overflow checking, but existing implementations don't follow it. */
f899b4b8 486#define ADDRESS_ADD_INSN \
b7c7d6c1 487 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
488
489#define ADDRESS_ADDI_INSN \
b7c7d6c1 490 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
491
492#define ADDRESS_LOAD_INSN \
493 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
494
495#define ADDRESS_STORE_INSN \
496 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
497
a4672219 498/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
499#define CPU_HAS_MIPS16(cpu) \
500 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
501 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 502
2309ddf2 503/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
504#define CPU_HAS_MICROMIPS(cpu) 0
505
60b63b72
RS
506/* True if CPU has a dror instruction. */
507#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
508
509/* True if CPU has a ror instruction. */
510#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
511
dd6a37e7 512/* True if CPU is in the Octeon family */
432233b3 513#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
dd6a37e7 514
dd3cbb7e 515/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 516#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 517
0aa27725
RS
518/* True, if CPU has support for ldc1 and sdc1. */
519#define CPU_HAS_LDC1_SDC1(CPU) \
520 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
521
c8978940
CD
522/* True if mflo and mfhi can be immediately followed by instructions
523 which write to the HI and LO registers.
524
525 According to MIPS specifications, MIPS ISAs I, II, and III need
526 (at least) two instructions between the reads of HI/LO and
527 instructions which write them, and later ISAs do not. Contradicting
528 the MIPS specifications, some MIPS IV processor user manuals (e.g.
529 the UM for the NEC Vr5000) document needing the instructions between
530 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
531 MIPS64 and later ISAs to have the interlocks, plus any specific
532 earlier-ISA CPUs for which CPU documentation declares that the
533 instructions are really interlocked. */
534#define hilo_interlocks \
535 (mips_opts.isa == ISA_MIPS32 \
536 || mips_opts.isa == ISA_MIPS32R2 \
537 || mips_opts.isa == ISA_MIPS64 \
538 || mips_opts.isa == ISA_MIPS64R2 \
539 || mips_opts.arch == CPU_R4010 \
e407c74b 540 || mips_opts.arch == CPU_R5900 \
c8978940
CD
541 || mips_opts.arch == CPU_R10000 \
542 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
543 || mips_opts.arch == CPU_R14000 \
544 || mips_opts.arch == CPU_R16000 \
c8978940 545 || mips_opts.arch == CPU_RM7000 \
c8978940 546 || mips_opts.arch == CPU_VR5500 \
df58fc94 547 || mips_opts.micromips \
c8978940 548 )
252b5132
RH
549
550/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
551 from the GPRs after they are loaded from memory, and thus does not
552 require nops to be inserted. This applies to instructions marked
553 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
df58fc94
RS
554 level I and microMIPS mode instructions are always interlocked. */
555#define gpr_interlocks \
556 (mips_opts.isa != ISA_MIPS1 \
557 || mips_opts.arch == CPU_R3900 \
e407c74b 558 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
559 || mips_opts.micromips \
560 )
252b5132 561
81912461
ILT
562/* Whether the processor uses hardware interlocks to avoid delays
563 required by coprocessor instructions, and thus does not require
564 nops to be inserted. This applies to instructions marked
565 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
566 between instructions marked INSN_WRITE_COND_CODE and ones marked
567 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
568 levels I, II, and III and microMIPS mode instructions are always
569 interlocked. */
bdaaa2e1 570/* Itbl support may require additional care here. */
81912461
ILT
571#define cop_interlocks \
572 ((mips_opts.isa != ISA_MIPS1 \
573 && mips_opts.isa != ISA_MIPS2 \
574 && mips_opts.isa != ISA_MIPS3) \
575 || mips_opts.arch == CPU_R4300 \
df58fc94 576 || mips_opts.micromips \
81912461
ILT
577 )
578
579/* Whether the processor uses hardware interlocks to protect reads
580 from coprocessor registers after they are loaded from memory, and
581 thus does not require nops to be inserted. This applies to
582 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
583 requires at MIPS ISA level I and microMIPS mode instructions are
584 always interlocked. */
585#define cop_mem_interlocks \
586 (mips_opts.isa != ISA_MIPS1 \
587 || mips_opts.micromips \
588 )
252b5132 589
6b76fefe
CM
590/* Is this a mfhi or mflo instruction? */
591#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
592 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
593
df58fc94
RS
594/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
595 has been selected. This implies, in particular, that addresses of text
596 labels have their LSB set. */
597#define HAVE_CODE_COMPRESSION \
598 ((mips_opts.mips16 | mips_opts.micromips) != 0)
599
252b5132
RH
600/* MIPS PIC level. */
601
a161fe53 602enum mips_pic_level mips_pic;
252b5132 603
c9914766 604/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 605 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 606static int mips_big_got = 0;
252b5132
RH
607
608/* 1 if trap instructions should used for overflow rather than break
609 instructions. */
c9914766 610static int mips_trap = 0;
252b5132 611
119d663a 612/* 1 if double width floating point constants should not be constructed
b6ff326e 613 by assembling two single width halves into two single width floating
119d663a
NC
614 point registers which just happen to alias the double width destination
615 register. On some architectures this aliasing can be disabled by a bit
d547a75e 616 in the status register, and the setting of this bit cannot be determined
119d663a
NC
617 automatically at assemble time. */
618static int mips_disable_float_construction;
619
252b5132
RH
620/* Non-zero if any .set noreorder directives were used. */
621
622static int mips_any_noreorder;
623
6b76fefe
CM
624/* Non-zero if nops should be inserted when the register referenced in
625 an mfhi/mflo instruction is read in the next two instructions. */
626static int mips_7000_hilo_fix;
627
02ffd3e4 628/* The size of objects in the small data section. */
156c2f8b 629static unsigned int g_switch_value = 8;
252b5132
RH
630/* Whether the -G option was used. */
631static int g_switch_seen = 0;
632
633#define N_RMASK 0xc4
634#define N_VFP 0xd4
635
636/* If we can determine in advance that GP optimization won't be
637 possible, we can skip the relaxation stuff that tries to produce
638 GP-relative references. This makes delay slot optimization work
639 better.
640
641 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
642 gcc output. It needs to guess right for gcc, otherwise gcc
643 will put what it thinks is a GP-relative instruction in a branch
644 delay slot.
252b5132
RH
645
646 I don't know if a fix is needed for the SVR4_PIC mode. I've only
647 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 648static int nopic_need_relax (symbolS *, int);
252b5132
RH
649
650/* handle of the OPCODE hash table */
651static struct hash_control *op_hash = NULL;
652
653/* The opcode hash table we use for the mips16. */
654static struct hash_control *mips16_op_hash = NULL;
655
df58fc94
RS
656/* The opcode hash table we use for the microMIPS ASE. */
657static struct hash_control *micromips_op_hash = NULL;
658
252b5132
RH
659/* This array holds the chars that always start a comment. If the
660 pre-processor is disabled, these aren't very useful */
661const char comment_chars[] = "#";
662
663/* This array holds the chars that only start a comment at the beginning of
664 a line. If the line seems to have the form '# 123 filename'
665 .line and .file directives will appear in the pre-processed output */
666/* Note that input_file.c hand checks for '#' at the beginning of the
667 first line of the input file. This is because the compiler outputs
bdaaa2e1 668 #NO_APP at the beginning of its output. */
252b5132
RH
669/* Also note that C style comments are always supported. */
670const char line_comment_chars[] = "#";
671
bdaaa2e1 672/* This array holds machine specific line separator characters. */
63a0b638 673const char line_separator_chars[] = ";";
252b5132
RH
674
675/* Chars that can be used to separate mant from exp in floating point nums */
676const char EXP_CHARS[] = "eE";
677
678/* Chars that mean this number is a floating point constant */
679/* As in 0f12.456 */
680/* or 0d1.2345e12 */
681const char FLT_CHARS[] = "rRsSfFdDxXpP";
682
683/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
684 changed in read.c . Ideally it shouldn't have to know about it at all,
685 but nothing is ideal around here.
686 */
687
688static char *insn_error;
689
690static int auto_align = 1;
691
692/* When outputting SVR4 PIC code, the assembler needs to know the
693 offset in the stack frame from which to restore the $gp register.
694 This is set by the .cprestore pseudo-op, and saved in this
695 variable. */
696static offsetT mips_cprestore_offset = -1;
697
67c1ffbe 698/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 699 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 700 offset and even an other register than $gp as global pointer. */
6478892d
TS
701static offsetT mips_cpreturn_offset = -1;
702static int mips_cpreturn_register = -1;
703static int mips_gp_register = GP;
def2e0dd 704static int mips_gprel_offset = 0;
6478892d 705
7a621144
DJ
706/* Whether mips_cprestore_offset has been set in the current function
707 (or whether it has already been warned about, if not). */
708static int mips_cprestore_valid = 0;
709
252b5132
RH
710/* This is the register which holds the stack frame, as set by the
711 .frame pseudo-op. This is needed to implement .cprestore. */
712static int mips_frame_reg = SP;
713
7a621144
DJ
714/* Whether mips_frame_reg has been set in the current function
715 (or whether it has already been warned about, if not). */
716static int mips_frame_reg_valid = 0;
717
252b5132
RH
718/* To output NOP instructions correctly, we need to keep information
719 about the previous two instructions. */
720
721/* Whether we are optimizing. The default value of 2 means to remove
722 unneeded NOPs and swap branch instructions when possible. A value
723 of 1 means to not swap branches. A value of 0 means to always
724 insert NOPs. */
725static int mips_optimize = 2;
726
727/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
728 equivalent to seeing no -g option at all. */
729static int mips_debug = 0;
730
7d8e00cf
RS
731/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
732#define MAX_VR4130_NOPS 4
733
734/* The maximum number of NOPs needed to fill delay slots. */
735#define MAX_DELAY_NOPS 2
736
737/* The maximum number of NOPs needed for any purpose. */
738#define MAX_NOPS 4
71400594
RS
739
740/* A list of previous instructions, with index 0 being the most recent.
741 We need to look back MAX_NOPS instructions when filling delay slots
742 or working around processor errata. We need to look back one
743 instruction further if we're thinking about using history[0] to
744 fill a branch delay slot. */
745static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 746
1e915849 747/* Nop instructions used by emit_nop. */
df58fc94
RS
748static struct mips_cl_insn nop_insn;
749static struct mips_cl_insn mips16_nop_insn;
750static struct mips_cl_insn micromips_nop16_insn;
751static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
752
753/* The appropriate nop for the current mode. */
df58fc94
RS
754#define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn \
755 : (mips_opts.micromips ? &micromips_nop16_insn : &nop_insn))
756
757/* The size of NOP_INSN in bytes. */
758#define NOP_INSN_SIZE (HAVE_CODE_COMPRESSION ? 2 : 4)
252b5132 759
252b5132
RH
760/* If this is set, it points to a frag holding nop instructions which
761 were inserted before the start of a noreorder section. If those
762 nops turn out to be unnecessary, the size of the frag can be
763 decreased. */
764static fragS *prev_nop_frag;
765
766/* The number of nop instructions we created in prev_nop_frag. */
767static int prev_nop_frag_holds;
768
769/* The number of nop instructions that we know we need in
bdaaa2e1 770 prev_nop_frag. */
252b5132
RH
771static int prev_nop_frag_required;
772
773/* The number of instructions we've seen since prev_nop_frag. */
774static int prev_nop_frag_since;
775
776/* For ECOFF and ELF, relocations against symbols are done in two
777 parts, with a HI relocation and a LO relocation. Each relocation
778 has only 16 bits of space to store an addend. This means that in
779 order for the linker to handle carries correctly, it must be able
780 to locate both the HI and the LO relocation. This means that the
781 relocations must appear in order in the relocation table.
782
783 In order to implement this, we keep track of each unmatched HI
784 relocation. We then sort them so that they immediately precede the
bdaaa2e1 785 corresponding LO relocation. */
252b5132 786
e972090a
NC
787struct mips_hi_fixup
788{
252b5132
RH
789 /* Next HI fixup. */
790 struct mips_hi_fixup *next;
791 /* This fixup. */
792 fixS *fixp;
793 /* The section this fixup is in. */
794 segT seg;
795};
796
797/* The list of unmatched HI relocs. */
798
799static struct mips_hi_fixup *mips_hi_fixup_list;
800
64bdfcaf
RS
801/* The frag containing the last explicit relocation operator.
802 Null if explicit relocations have not been used. */
803
804static fragS *prev_reloc_op_frag;
805
252b5132
RH
806/* Map normal MIPS register numbers to mips16 register numbers. */
807
808#define X ILLEGAL_REG
e972090a
NC
809static const int mips32_to_16_reg_map[] =
810{
252b5132
RH
811 X, X, 2, 3, 4, 5, 6, 7,
812 X, X, X, X, X, X, X, X,
813 0, 1, X, X, X, X, X, X,
814 X, X, X, X, X, X, X, X
815};
816#undef X
817
818/* Map mips16 register numbers to normal MIPS register numbers. */
819
e972090a
NC
820static const unsigned int mips16_to_32_reg_map[] =
821{
252b5132
RH
822 16, 17, 2, 3, 4, 5, 6, 7
823};
60b63b72 824
df58fc94
RS
825/* Map normal MIPS register numbers to microMIPS register numbers. */
826
827#define mips32_to_micromips_reg_b_map mips32_to_16_reg_map
828#define mips32_to_micromips_reg_c_map mips32_to_16_reg_map
829#define mips32_to_micromips_reg_d_map mips32_to_16_reg_map
830#define mips32_to_micromips_reg_e_map mips32_to_16_reg_map
831#define mips32_to_micromips_reg_f_map mips32_to_16_reg_map
832#define mips32_to_micromips_reg_g_map mips32_to_16_reg_map
833#define mips32_to_micromips_reg_l_map mips32_to_16_reg_map
834
835#define X ILLEGAL_REG
836/* reg type h: 4, 5, 6. */
837static const int mips32_to_micromips_reg_h_map[] =
838{
839 X, X, X, X, 4, 5, 6, X,
840 X, X, X, X, X, X, X, X,
841 X, X, X, X, X, X, X, X,
842 X, X, X, X, X, X, X, X
843};
844
845/* reg type m: 0, 17, 2, 3, 16, 18, 19, 20. */
846static const int mips32_to_micromips_reg_m_map[] =
847{
848 0, X, 2, 3, X, X, X, X,
849 X, X, X, X, X, X, X, X,
850 4, 1, 5, 6, 7, X, X, X,
851 X, X, X, X, X, X, X, X
852};
853
854/* reg type q: 0, 2-7. 17. */
855static const int mips32_to_micromips_reg_q_map[] =
856{
857 0, X, 2, 3, 4, 5, 6, 7,
858 X, X, X, X, X, X, X, X,
859 X, 1, X, X, X, X, X, X,
860 X, X, X, X, X, X, X, X
861};
862
863#define mips32_to_micromips_reg_n_map mips32_to_micromips_reg_m_map
864#undef X
865
866/* Map microMIPS register numbers to normal MIPS register numbers. */
867
868#define micromips_to_32_reg_b_map mips16_to_32_reg_map
869#define micromips_to_32_reg_c_map mips16_to_32_reg_map
870#define micromips_to_32_reg_d_map mips16_to_32_reg_map
871#define micromips_to_32_reg_e_map mips16_to_32_reg_map
872#define micromips_to_32_reg_f_map mips16_to_32_reg_map
873#define micromips_to_32_reg_g_map mips16_to_32_reg_map
874
875/* The microMIPS registers with type h. */
876static const unsigned int micromips_to_32_reg_h_map[] =
877{
878 5, 5, 6, 4, 4, 4, 4, 4
879};
880
881/* The microMIPS registers with type i. */
882static const unsigned int micromips_to_32_reg_i_map[] =
883{
884 6, 7, 7, 21, 22, 5, 6, 7
885};
886
887#define micromips_to_32_reg_l_map mips16_to_32_reg_map
888
889/* The microMIPS registers with type m. */
890static const unsigned int micromips_to_32_reg_m_map[] =
891{
892 0, 17, 2, 3, 16, 18, 19, 20
893};
894
895#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
896
897/* The microMIPS registers with type q. */
898static const unsigned int micromips_to_32_reg_q_map[] =
899{
900 0, 17, 2, 3, 4, 5, 6, 7
901};
902
903/* microMIPS imm type B. */
904static const int micromips_imm_b_map[] =
905{
906 1, 4, 8, 12, 16, 20, 24, -1
907};
908
909/* microMIPS imm type C. */
910static const int micromips_imm_c_map[] =
911{
912 128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
913};
914
71400594
RS
915/* Classifies the kind of instructions we're interested in when
916 implementing -mfix-vr4120. */
c67a084a
NC
917enum fix_vr4120_class
918{
71400594
RS
919 FIX_VR4120_MACC,
920 FIX_VR4120_DMACC,
921 FIX_VR4120_MULT,
922 FIX_VR4120_DMULT,
923 FIX_VR4120_DIV,
924 FIX_VR4120_MTHILO,
925 NUM_FIX_VR4120_CLASSES
926};
927
c67a084a
NC
928/* ...likewise -mfix-loongson2f-jump. */
929static bfd_boolean mips_fix_loongson2f_jump;
930
931/* ...likewise -mfix-loongson2f-nop. */
932static bfd_boolean mips_fix_loongson2f_nop;
933
934/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
935static bfd_boolean mips_fix_loongson2f;
936
71400594
RS
937/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
938 there must be at least one other instruction between an instruction
939 of type X and an instruction of type Y. */
940static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
941
942/* True if -mfix-vr4120 is in force. */
d766e8ec 943static int mips_fix_vr4120;
4a6a3df4 944
7d8e00cf
RS
945/* ...likewise -mfix-vr4130. */
946static int mips_fix_vr4130;
947
6a32d874
CM
948/* ...likewise -mfix-24k. */
949static int mips_fix_24k;
950
d954098f
DD
951/* ...likewise -mfix-cn63xxp1 */
952static bfd_boolean mips_fix_cn63xxp1;
953
4a6a3df4
AO
954/* We don't relax branches by default, since this causes us to expand
955 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
956 fail to compute the offset before expanding the macro to the most
957 efficient expansion. */
958
959static int mips_relax_branch;
252b5132 960\f
4d7206a2
RS
961/* The expansion of many macros depends on the type of symbol that
962 they refer to. For example, when generating position-dependent code,
963 a macro that refers to a symbol may have two different expansions,
964 one which uses GP-relative addresses and one which uses absolute
965 addresses. When generating SVR4-style PIC, a macro may have
966 different expansions for local and global symbols.
967
968 We handle these situations by generating both sequences and putting
969 them in variant frags. In position-dependent code, the first sequence
970 will be the GP-relative one and the second sequence will be the
971 absolute one. In SVR4 PIC, the first sequence will be for global
972 symbols and the second will be for local symbols.
973
584892a6
RS
974 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
975 SECOND are the lengths of the two sequences in bytes. These fields
976 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
977 the subtype has the following flags:
4d7206a2 978
584892a6
RS
979 RELAX_USE_SECOND
980 Set if it has been decided that we should use the second
981 sequence instead of the first.
982
983 RELAX_SECOND_LONGER
984 Set in the first variant frag if the macro's second implementation
985 is longer than its first. This refers to the macro as a whole,
986 not an individual relaxation.
987
988 RELAX_NOMACRO
989 Set in the first variant frag if the macro appeared in a .set nomacro
990 block and if one alternative requires a warning but the other does not.
991
992 RELAX_DELAY_SLOT
993 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
994 delay slot.
4d7206a2 995
df58fc94
RS
996 RELAX_DELAY_SLOT_16BIT
997 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
998 16-bit instruction.
999
1000 RELAX_DELAY_SLOT_SIZE_FIRST
1001 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
1002 the macro is of the wrong size for the branch delay slot.
1003
1004 RELAX_DELAY_SLOT_SIZE_SECOND
1005 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1006 the macro is of the wrong size for the branch delay slot.
1007
4d7206a2
RS
1008 The frag's "opcode" points to the first fixup for relaxable code.
1009
1010 Relaxable macros are generated using a sequence such as:
1011
1012 relax_start (SYMBOL);
1013 ... generate first expansion ...
1014 relax_switch ();
1015 ... generate second expansion ...
1016 relax_end ();
1017
1018 The code and fixups for the unwanted alternative are discarded
1019 by md_convert_frag. */
584892a6 1020#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 1021
584892a6
RS
1022#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1023#define RELAX_SECOND(X) ((X) & 0xff)
1024#define RELAX_USE_SECOND 0x10000
1025#define RELAX_SECOND_LONGER 0x20000
1026#define RELAX_NOMACRO 0x40000
1027#define RELAX_DELAY_SLOT 0x80000
df58fc94
RS
1028#define RELAX_DELAY_SLOT_16BIT 0x100000
1029#define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
1030#define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
252b5132 1031
4a6a3df4
AO
1032/* Branch without likely bit. If label is out of range, we turn:
1033
1034 beq reg1, reg2, label
1035 delay slot
1036
1037 into
1038
1039 bne reg1, reg2, 0f
1040 nop
1041 j label
1042 0: delay slot
1043
1044 with the following opcode replacements:
1045
1046 beq <-> bne
1047 blez <-> bgtz
1048 bltz <-> bgez
1049 bc1f <-> bc1t
1050
1051 bltzal <-> bgezal (with jal label instead of j label)
1052
1053 Even though keeping the delay slot instruction in the delay slot of
1054 the branch would be more efficient, it would be very tricky to do
1055 correctly, because we'd have to introduce a variable frag *after*
1056 the delay slot instruction, and expand that instead. Let's do it
1057 the easy way for now, even if the branch-not-taken case now costs
1058 one additional instruction. Out-of-range branches are not supposed
1059 to be common, anyway.
1060
1061 Branch likely. If label is out of range, we turn:
1062
1063 beql reg1, reg2, label
1064 delay slot (annulled if branch not taken)
1065
1066 into
1067
1068 beql reg1, reg2, 1f
1069 nop
1070 beql $0, $0, 2f
1071 nop
1072 1: j[al] label
1073 delay slot (executed only if branch taken)
1074 2:
1075
1076 It would be possible to generate a shorter sequence by losing the
1077 likely bit, generating something like:
b34976b6 1078
4a6a3df4
AO
1079 bne reg1, reg2, 0f
1080 nop
1081 j[al] label
1082 delay slot (executed only if branch taken)
1083 0:
1084
1085 beql -> bne
1086 bnel -> beq
1087 blezl -> bgtz
1088 bgtzl -> blez
1089 bltzl -> bgez
1090 bgezl -> bltz
1091 bc1fl -> bc1t
1092 bc1tl -> bc1f
1093
1094 bltzall -> bgezal (with jal label instead of j label)
1095 bgezall -> bltzal (ditto)
1096
1097
1098 but it's not clear that it would actually improve performance. */
66b3e8da
MR
1099#define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1100 ((relax_substateT) \
1101 (0xc0000000 \
1102 | ((at) & 0x1f) \
1103 | ((toofar) ? 0x20 : 0) \
1104 | ((link) ? 0x40 : 0) \
1105 | ((likely) ? 0x80 : 0) \
1106 | ((uncond) ? 0x100 : 0)))
4a6a3df4 1107#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
66b3e8da
MR
1108#define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1109#define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1110#define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1111#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1112#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1113
252b5132
RH
1114/* For mips16 code, we use an entirely different form of relaxation.
1115 mips16 supports two versions of most instructions which take
1116 immediate values: a small one which takes some small value, and a
1117 larger one which takes a 16 bit value. Since branches also follow
1118 this pattern, relaxing these values is required.
1119
1120 We can assemble both mips16 and normal MIPS code in a single
1121 object. Therefore, we need to support this type of relaxation at
1122 the same time that we support the relaxation described above. We
1123 use the high bit of the subtype field to distinguish these cases.
1124
1125 The information we store for this type of relaxation is the
1126 argument code found in the opcode file for this relocation, whether
1127 the user explicitly requested a small or extended form, and whether
1128 the relocation is in a jump or jal delay slot. That tells us the
1129 size of the value, and how it should be stored. We also store
1130 whether the fragment is considered to be extended or not. We also
1131 store whether this is known to be a branch to a different section,
1132 whether we have tried to relax this frag yet, and whether we have
1133 ever extended a PC relative fragment because of a shift count. */
1134#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1135 (0x80000000 \
1136 | ((type) & 0xff) \
1137 | ((small) ? 0x100 : 0) \
1138 | ((ext) ? 0x200 : 0) \
1139 | ((dslot) ? 0x400 : 0) \
1140 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 1141#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
1142#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1143#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1144#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1145#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1146#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1147#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1148#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1149#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1150#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1151#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1152#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95 1153
df58fc94
RS
1154/* For microMIPS code, we use relaxation similar to one we use for
1155 MIPS16 code. Some instructions that take immediate values support
1156 two encodings: a small one which takes some small value, and a
1157 larger one which takes a 16 bit value. As some branches also follow
1158 this pattern, relaxing these values is required.
1159
1160 We can assemble both microMIPS and normal MIPS code in a single
1161 object. Therefore, we need to support this type of relaxation at
1162 the same time that we support the relaxation described above. We
1163 use one of the high bits of the subtype field to distinguish these
1164 cases.
1165
1166 The information we store for this type of relaxation is the argument
1167 code found in the opcode file for this relocation, the register
40209cad
MR
1168 selected as the assembler temporary, whether the branch is
1169 unconditional, whether it is compact, whether it stores the link
1170 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1171 branches to a sequence of instructions is enabled, and whether the
1172 displacement of a branch is too large to fit as an immediate argument
1173 of a 16-bit and a 32-bit branch, respectively. */
1174#define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1175 relax32, toofar16, toofar32) \
1176 (0x40000000 \
1177 | ((type) & 0xff) \
1178 | (((at) & 0x1f) << 8) \
1179 | ((uncond) ? 0x2000 : 0) \
1180 | ((compact) ? 0x4000 : 0) \
1181 | ((link) ? 0x8000 : 0) \
1182 | ((relax32) ? 0x10000 : 0) \
1183 | ((toofar16) ? 0x20000 : 0) \
1184 | ((toofar32) ? 0x40000 : 0))
df58fc94
RS
1185#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1186#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1187#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
40209cad
MR
1188#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1189#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1190#define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1191#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1192
1193#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1194#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1195#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1196#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1197#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1198#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
df58fc94 1199
43c0598f
RS
1200/* Sign-extend 16-bit value X. */
1201#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1202
885add95
CD
1203/* Is the given value a sign-extended 32-bit value? */
1204#define IS_SEXT_32BIT_NUM(x) \
1205 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1206 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1207
1208/* Is the given value a sign-extended 16-bit value? */
1209#define IS_SEXT_16BIT_NUM(x) \
1210 (((x) &~ (offsetT) 0x7fff) == 0 \
1211 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1212
df58fc94
RS
1213/* Is the given value a sign-extended 12-bit value? */
1214#define IS_SEXT_12BIT_NUM(x) \
1215 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1216
2051e8c4
MR
1217/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1218#define IS_ZEXT_32BIT_NUM(x) \
1219 (((x) &~ (offsetT) 0xffffffff) == 0 \
1220 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1221
bf12938e
RS
1222/* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1223 VALUE << SHIFT. VALUE is evaluated exactly once. */
1224#define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1225 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1226 | (((VALUE) & (MASK)) << (SHIFT)))
1227
1228/* Extract bits MASK << SHIFT from STRUCT and shift them right
1229 SHIFT places. */
1230#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1231 (((STRUCT) >> (SHIFT)) & (MASK))
1232
1233/* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1234 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1235
1236 include/opcode/mips.h specifies operand fields using the macros
1237 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
1238 with "MIPS16OP" instead of "OP". */
df58fc94
RS
1239#define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1240 do \
1241 if (!(MICROMIPS)) \
1242 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1243 OP_MASK_##FIELD, OP_SH_##FIELD); \
1244 else \
1245 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1246 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1247 while (0)
bf12938e
RS
1248#define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1249 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1250 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1251
1252/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1253#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1254 (!(MICROMIPS) \
1255 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1256 : EXTRACT_BITS ((INSN).insn_opcode, \
1257 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1258#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1259 EXTRACT_BITS ((INSN).insn_opcode, \
1260 MIPS16OP_MASK_##FIELD, \
1261 MIPS16OP_SH_##FIELD)
5c04167a
RS
1262
1263/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1264#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1265\f
df58fc94
RS
1266/* Whether or not we are emitting a branch-likely macro. */
1267static bfd_boolean emit_branch_likely_macro = FALSE;
1268
4d7206a2
RS
1269/* Global variables used when generating relaxable macros. See the
1270 comment above RELAX_ENCODE for more details about how relaxation
1271 is used. */
1272static struct {
1273 /* 0 if we're not emitting a relaxable macro.
1274 1 if we're emitting the first of the two relaxation alternatives.
1275 2 if we're emitting the second alternative. */
1276 int sequence;
1277
1278 /* The first relaxable fixup in the current frag. (In other words,
1279 the first fixup that refers to relaxable code.) */
1280 fixS *first_fixup;
1281
1282 /* sizes[0] says how many bytes of the first alternative are stored in
1283 the current frag. Likewise sizes[1] for the second alternative. */
1284 unsigned int sizes[2];
1285
1286 /* The symbol on which the choice of sequence depends. */
1287 symbolS *symbol;
1288} mips_relax;
252b5132 1289\f
584892a6
RS
1290/* Global variables used to decide whether a macro needs a warning. */
1291static struct {
1292 /* True if the macro is in a branch delay slot. */
1293 bfd_boolean delay_slot_p;
1294
df58fc94
RS
1295 /* Set to the length in bytes required if the macro is in a delay slot
1296 that requires a specific length of instruction, otherwise zero. */
1297 unsigned int delay_slot_length;
1298
584892a6
RS
1299 /* For relaxable macros, sizes[0] is the length of the first alternative
1300 in bytes and sizes[1] is the length of the second alternative.
1301 For non-relaxable macros, both elements give the length of the
1302 macro in bytes. */
1303 unsigned int sizes[2];
1304
df58fc94
RS
1305 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1306 instruction of the first alternative in bytes and first_insn_sizes[1]
1307 is the length of the first instruction of the second alternative.
1308 For non-relaxable macros, both elements give the length of the first
1309 instruction in bytes.
1310
1311 Set to zero if we haven't yet seen the first instruction. */
1312 unsigned int first_insn_sizes[2];
1313
1314 /* For relaxable macros, insns[0] is the number of instructions for the
1315 first alternative and insns[1] is the number of instructions for the
1316 second alternative.
1317
1318 For non-relaxable macros, both elements give the number of
1319 instructions for the macro. */
1320 unsigned int insns[2];
1321
584892a6
RS
1322 /* The first variant frag for this macro. */
1323 fragS *first_frag;
1324} mips_macro_warning;
1325\f
252b5132
RH
1326/* Prototypes for static functions. */
1327
252b5132
RH
1328enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1329
b34976b6 1330static void append_insn
df58fc94
RS
1331 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1332 bfd_boolean expansionp);
7d10b47d 1333static void mips_no_prev_insn (void);
c67a084a 1334static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1335static void mips16_macro_build
03ea81db 1336 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1337static void load_register (int, expressionS *, int);
584892a6
RS
1338static void macro_start (void);
1339static void macro_end (void);
17a2f251
TS
1340static void macro (struct mips_cl_insn * ip);
1341static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1342static void mips_ip (char *str, struct mips_cl_insn * ip);
1343static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1344static void mips16_immed
43c0598f
RS
1345 (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1346 unsigned int, unsigned long *);
5e0116d5 1347static size_t my_getSmallExpression
17a2f251
TS
1348 (expressionS *, bfd_reloc_code_real_type *, char *);
1349static void my_getExpression (expressionS *, char *);
1350static void s_align (int);
1351static void s_change_sec (int);
1352static void s_change_section (int);
1353static void s_cons (int);
1354static void s_float_cons (int);
1355static void s_mips_globl (int);
1356static void s_option (int);
1357static void s_mipsset (int);
1358static void s_abicalls (int);
1359static void s_cpload (int);
1360static void s_cpsetup (int);
1361static void s_cplocal (int);
1362static void s_cprestore (int);
1363static void s_cpreturn (int);
741d6ea8
JM
1364static void s_dtprelword (int);
1365static void s_dtpreldword (int);
d0f13682
CLT
1366static void s_tprelword (int);
1367static void s_tpreldword (int);
17a2f251
TS
1368static void s_gpvalue (int);
1369static void s_gpword (int);
1370static void s_gpdword (int);
a3f278e2 1371static void s_ehword (int);
17a2f251
TS
1372static void s_cpadd (int);
1373static void s_insn (int);
1374static void md_obj_begin (void);
1375static void md_obj_end (void);
1376static void s_mips_ent (int);
1377static void s_mips_end (int);
1378static void s_mips_frame (int);
1379static void s_mips_mask (int reg_type);
1380static void s_mips_stab (int);
1381static void s_mips_weakext (int);
1382static void s_mips_file (int);
1383static void s_mips_loc (int);
1384static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1385static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 1386static int validate_mips_insn (const struct mips_opcode *);
df58fc94
RS
1387static int validate_micromips_insn (const struct mips_opcode *);
1388static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1389static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
e7af610e
NC
1390
1391/* Table and functions used to map between CPU/ISA names, and
1392 ISA levels, and CPU numbers. */
1393
e972090a
NC
1394struct mips_cpu_info
1395{
e7af610e 1396 const char *name; /* CPU or ISA name. */
ad3fea08 1397 int flags; /* ASEs available, or ISA flag. */
e7af610e
NC
1398 int isa; /* ISA level. */
1399 int cpu; /* CPU number (default CPU if ISA). */
1400};
1401
ad3fea08
TS
1402#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1403#define MIPS_CPU_ASE_SMARTMIPS 0x0002 /* CPU implements SmartMIPS ASE */
1404#define MIPS_CPU_ASE_DSP 0x0004 /* CPU implements DSP ASE */
1405#define MIPS_CPU_ASE_MT 0x0008 /* CPU implements MT ASE */
1406#define MIPS_CPU_ASE_MIPS3D 0x0010 /* CPU implements MIPS-3D ASE */
1407#define MIPS_CPU_ASE_MDMX 0x0020 /* CPU implements MDMX ASE */
8b082fb1 1408#define MIPS_CPU_ASE_DSPR2 0x0040 /* CPU implements DSP R2 ASE */
dec0624d 1409#define MIPS_CPU_ASE_MCU 0x0080 /* CPU implements MCU ASE */
b015e599 1410#define MIPS_CPU_ASE_VIRT 0x0100 /* CPU implements Virtualization ASE */
ad3fea08 1411
17a2f251
TS
1412static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1413static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1414static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
1415\f
1416/* Pseudo-op table.
1417
1418 The following pseudo-ops from the Kane and Heinrich MIPS book
1419 should be defined here, but are currently unsupported: .alias,
1420 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1421
1422 The following pseudo-ops from the Kane and Heinrich MIPS book are
1423 specific to the type of debugging information being generated, and
1424 should be defined by the object format: .aent, .begin, .bend,
1425 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1426 .vreg.
1427
1428 The following pseudo-ops from the Kane and Heinrich MIPS book are
1429 not MIPS CPU specific, but are also not specific to the object file
1430 format. This file is probably the best place to define them, but
d84bcf09 1431 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1432
e972090a
NC
1433static const pseudo_typeS mips_pseudo_table[] =
1434{
beae10d5 1435 /* MIPS specific pseudo-ops. */
252b5132
RH
1436 {"option", s_option, 0},
1437 {"set", s_mipsset, 0},
1438 {"rdata", s_change_sec, 'r'},
1439 {"sdata", s_change_sec, 's'},
1440 {"livereg", s_ignore, 0},
1441 {"abicalls", s_abicalls, 0},
1442 {"cpload", s_cpload, 0},
6478892d
TS
1443 {"cpsetup", s_cpsetup, 0},
1444 {"cplocal", s_cplocal, 0},
252b5132 1445 {"cprestore", s_cprestore, 0},
6478892d 1446 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1447 {"dtprelword", s_dtprelword, 0},
1448 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1449 {"tprelword", s_tprelword, 0},
1450 {"tpreldword", s_tpreldword, 0},
6478892d 1451 {"gpvalue", s_gpvalue, 0},
252b5132 1452 {"gpword", s_gpword, 0},
10181a0d 1453 {"gpdword", s_gpdword, 0},
a3f278e2 1454 {"ehword", s_ehword, 0},
252b5132
RH
1455 {"cpadd", s_cpadd, 0},
1456 {"insn", s_insn, 0},
1457
beae10d5 1458 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1459 chips. */
38a57ae7 1460 {"asciiz", stringer, 8 + 1},
252b5132
RH
1461 {"bss", s_change_sec, 'b'},
1462 {"err", s_err, 0},
1463 {"half", s_cons, 1},
1464 {"dword", s_cons, 3},
1465 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1466 {"origin", s_org, 0},
1467 {"repeat", s_rept, 0},
252b5132 1468
998b3c36
MR
1469 /* For MIPS this is non-standard, but we define it for consistency. */
1470 {"sbss", s_change_sec, 'B'},
1471
beae10d5 1472 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1473 here for one reason or another. */
1474 {"align", s_align, 0},
1475 {"byte", s_cons, 0},
1476 {"data", s_change_sec, 'd'},
1477 {"double", s_float_cons, 'd'},
1478 {"float", s_float_cons, 'f'},
1479 {"globl", s_mips_globl, 0},
1480 {"global", s_mips_globl, 0},
1481 {"hword", s_cons, 1},
1482 {"int", s_cons, 2},
1483 {"long", s_cons, 2},
1484 {"octa", s_cons, 4},
1485 {"quad", s_cons, 3},
cca86cc8 1486 {"section", s_change_section, 0},
252b5132
RH
1487 {"short", s_cons, 1},
1488 {"single", s_float_cons, 'f'},
754e2bb9 1489 {"stabd", s_mips_stab, 'd'},
252b5132 1490 {"stabn", s_mips_stab, 'n'},
754e2bb9 1491 {"stabs", s_mips_stab, 's'},
252b5132
RH
1492 {"text", s_change_sec, 't'},
1493 {"word", s_cons, 2},
add56521 1494
add56521 1495 { "extern", ecoff_directive_extern, 0},
add56521 1496
43841e91 1497 { NULL, NULL, 0 },
252b5132
RH
1498};
1499
e972090a
NC
1500static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1501{
beae10d5
KH
1502 /* These pseudo-ops should be defined by the object file format.
1503 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1504 {"aent", s_mips_ent, 1},
1505 {"bgnb", s_ignore, 0},
1506 {"end", s_mips_end, 0},
1507 {"endb", s_ignore, 0},
1508 {"ent", s_mips_ent, 0},
c5dd6aab 1509 {"file", s_mips_file, 0},
252b5132
RH
1510 {"fmask", s_mips_mask, 'F'},
1511 {"frame", s_mips_frame, 0},
c5dd6aab 1512 {"loc", s_mips_loc, 0},
252b5132
RH
1513 {"mask", s_mips_mask, 'R'},
1514 {"verstamp", s_ignore, 0},
43841e91 1515 { NULL, NULL, 0 },
252b5132
RH
1516};
1517
3ae8dd8d
MR
1518/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1519 purpose of the `.dc.a' internal pseudo-op. */
1520
1521int
1522mips_address_bytes (void)
1523{
1524 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1525}
1526
17a2f251 1527extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1528
1529void
17a2f251 1530mips_pop_insert (void)
252b5132
RH
1531{
1532 pop_insert (mips_pseudo_table);
1533 if (! ECOFF_DEBUGGING)
1534 pop_insert (mips_nonecoff_pseudo_table);
1535}
1536\f
1537/* Symbols labelling the current insn. */
1538
e972090a
NC
1539struct insn_label_list
1540{
252b5132
RH
1541 struct insn_label_list *next;
1542 symbolS *label;
1543};
1544
252b5132 1545static struct insn_label_list *free_insn_labels;
742a56fe 1546#define label_list tc_segment_info_data.labels
252b5132 1547
17a2f251 1548static void mips_clear_insn_labels (void);
df58fc94
RS
1549static void mips_mark_labels (void);
1550static void mips_compressed_mark_labels (void);
252b5132
RH
1551
1552static inline void
17a2f251 1553mips_clear_insn_labels (void)
252b5132
RH
1554{
1555 register struct insn_label_list **pl;
a8dbcb85 1556 segment_info_type *si;
252b5132 1557
a8dbcb85
TS
1558 if (now_seg)
1559 {
1560 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1561 ;
1562
1563 si = seg_info (now_seg);
1564 *pl = si->label_list;
1565 si->label_list = NULL;
1566 }
252b5132 1567}
a8dbcb85 1568
df58fc94
RS
1569/* Mark instruction labels in MIPS16/microMIPS mode. */
1570
1571static inline void
1572mips_mark_labels (void)
1573{
1574 if (HAVE_CODE_COMPRESSION)
1575 mips_compressed_mark_labels ();
1576}
252b5132
RH
1577\f
1578static char *expr_end;
1579
1580/* Expressions which appear in instructions. These are set by
1581 mips_ip. */
1582
1583static expressionS imm_expr;
5f74bc13 1584static expressionS imm2_expr;
252b5132
RH
1585static expressionS offset_expr;
1586
1587/* Relocs associated with imm_expr and offset_expr. */
1588
f6688943
TS
1589static bfd_reloc_code_real_type imm_reloc[3]
1590 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1591static bfd_reloc_code_real_type offset_reloc[3]
1592 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1593
df58fc94
RS
1594/* This is set to the resulting size of the instruction to be produced
1595 by mips16_ip if an explicit extension is used or by mips_ip if an
1596 explicit size is supplied. */
252b5132 1597
df58fc94 1598static unsigned int forced_insn_length;
252b5132 1599
e1b47bd5
RS
1600/* True if we are assembling an instruction. All dot symbols defined during
1601 this time should be treated as code labels. */
1602
1603static bfd_boolean mips_assembling_insn;
1604
7ed4a06a 1605#ifdef OBJ_ELF
ecb4347a
DJ
1606/* The pdr segment for per procedure frame/regmask info. Not used for
1607 ECOFF debugging. */
252b5132
RH
1608
1609static segT pdr_seg;
7ed4a06a 1610#endif
252b5132 1611
e013f690
TS
1612/* The default target format to use. */
1613
aeffff67
RS
1614#if defined (TE_FreeBSD)
1615#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1616#elif defined (TE_TMIPS)
1617#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1618#else
1619#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1620#endif
1621
e013f690 1622const char *
17a2f251 1623mips_target_format (void)
e013f690
TS
1624{
1625 switch (OUTPUT_FLAVOR)
1626 {
e013f690
TS
1627 case bfd_target_ecoff_flavour:
1628 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1629 case bfd_target_coff_flavour:
1630 return "pe-mips";
1631 case bfd_target_elf_flavour:
0a44bf69
RS
1632#ifdef TE_VXWORKS
1633 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1634 return (target_big_endian
1635 ? "elf32-bigmips-vxworks"
1636 : "elf32-littlemips-vxworks");
1637#endif
e013f690 1638 return (target_big_endian
cfe86eaa 1639 ? (HAVE_64BIT_OBJECTS
aeffff67 1640 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1641 : (HAVE_NEWABI
aeffff67
RS
1642 ? ELF_TARGET ("elf32-n", "big")
1643 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1644 : (HAVE_64BIT_OBJECTS
aeffff67 1645 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1646 : (HAVE_NEWABI
aeffff67
RS
1647 ? ELF_TARGET ("elf32-n", "little")
1648 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1649 default:
1650 abort ();
1651 return NULL;
1652 }
1653}
1654
df58fc94
RS
1655/* Return the length of a microMIPS instruction in bytes. If bits of
1656 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1657 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1658 major opcode) will require further modifications to the opcode
1659 table. */
1660
1661static inline unsigned int
1662micromips_insn_length (const struct mips_opcode *mo)
1663{
1664 return (mo->mask >> 16) == 0 ? 2 : 4;
1665}
1666
5c04167a
RS
1667/* Return the length of MIPS16 instruction OPCODE. */
1668
1669static inline unsigned int
1670mips16_opcode_length (unsigned long opcode)
1671{
1672 return (opcode >> 16) == 0 ? 2 : 4;
1673}
1674
1e915849
RS
1675/* Return the length of instruction INSN. */
1676
1677static inline unsigned int
1678insn_length (const struct mips_cl_insn *insn)
1679{
df58fc94
RS
1680 if (mips_opts.micromips)
1681 return micromips_insn_length (insn->insn_mo);
1682 else if (mips_opts.mips16)
5c04167a 1683 return mips16_opcode_length (insn->insn_opcode);
df58fc94 1684 else
1e915849 1685 return 4;
1e915849
RS
1686}
1687
1688/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1689
1690static void
1691create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1692{
1693 size_t i;
1694
1695 insn->insn_mo = mo;
1e915849
RS
1696 insn->insn_opcode = mo->match;
1697 insn->frag = NULL;
1698 insn->where = 0;
1699 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1700 insn->fixp[i] = NULL;
1701 insn->fixed_p = (mips_opts.noreorder > 0);
1702 insn->noreorder_p = (mips_opts.noreorder > 0);
1703 insn->mips16_absolute_jump_p = 0;
15be625d 1704 insn->complete_p = 0;
e407c74b 1705 insn->cleared_p = 0;
1e915849
RS
1706}
1707
df58fc94 1708/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
1709
1710static void
df58fc94 1711mips_record_compressed_mode (void)
742a56fe
RS
1712{
1713 segment_info_type *si;
1714
1715 si = seg_info (now_seg);
1716 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1717 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
1718 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
1719 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
1720}
1721
4d68580a
RS
1722/* Read a standard MIPS instruction from BUF. */
1723
1724static unsigned long
1725read_insn (char *buf)
1726{
1727 if (target_big_endian)
1728 return bfd_getb32 ((bfd_byte *) buf);
1729 else
1730 return bfd_getl32 ((bfd_byte *) buf);
1731}
1732
1733/* Write standard MIPS instruction INSN to BUF. Return a pointer to
1734 the next byte. */
1735
1736static char *
1737write_insn (char *buf, unsigned int insn)
1738{
1739 md_number_to_chars (buf, insn, 4);
1740 return buf + 4;
1741}
1742
1743/* Read a microMIPS or MIPS16 opcode from BUF, given that it
1744 has length LENGTH. */
1745
1746static unsigned long
1747read_compressed_insn (char *buf, unsigned int length)
1748{
1749 unsigned long insn;
1750 unsigned int i;
1751
1752 insn = 0;
1753 for (i = 0; i < length; i += 2)
1754 {
1755 insn <<= 16;
1756 if (target_big_endian)
1757 insn |= bfd_getb16 ((char *) buf);
1758 else
1759 insn |= bfd_getl16 ((char *) buf);
1760 buf += 2;
1761 }
1762 return insn;
1763}
1764
5c04167a
RS
1765/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
1766 instruction is LENGTH bytes long. Return a pointer to the next byte. */
1767
1768static char *
1769write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
1770{
1771 unsigned int i;
1772
1773 for (i = 0; i < length; i += 2)
1774 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
1775 return buf + length;
1776}
1777
1e915849
RS
1778/* Install INSN at the location specified by its "frag" and "where" fields. */
1779
1780static void
1781install_insn (const struct mips_cl_insn *insn)
1782{
1783 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
1784 if (HAVE_CODE_COMPRESSION)
1785 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 1786 else
4d68580a 1787 write_insn (f, insn->insn_opcode);
df58fc94 1788 mips_record_compressed_mode ();
1e915849
RS
1789}
1790
1791/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1792 and install the opcode in the new location. */
1793
1794static void
1795move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1796{
1797 size_t i;
1798
1799 insn->frag = frag;
1800 insn->where = where;
1801 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1802 if (insn->fixp[i] != NULL)
1803 {
1804 insn->fixp[i]->fx_frag = frag;
1805 insn->fixp[i]->fx_where = where;
1806 }
1807 install_insn (insn);
1808}
1809
1810/* Add INSN to the end of the output. */
1811
1812static void
1813add_fixed_insn (struct mips_cl_insn *insn)
1814{
1815 char *f = frag_more (insn_length (insn));
1816 move_insn (insn, frag_now, f - frag_now->fr_literal);
1817}
1818
1819/* Start a variant frag and move INSN to the start of the variant part,
1820 marking it as fixed. The other arguments are as for frag_var. */
1821
1822static void
1823add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1824 relax_substateT subtype, symbolS *symbol, offsetT offset)
1825{
1826 frag_grow (max_chars);
1827 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1828 insn->fixed_p = 1;
1829 frag_var (rs_machine_dependent, max_chars, var,
1830 subtype, symbol, offset, NULL);
1831}
1832
1833/* Insert N copies of INSN into the history buffer, starting at
1834 position FIRST. Neither FIRST nor N need to be clipped. */
1835
1836static void
1837insert_into_history (unsigned int first, unsigned int n,
1838 const struct mips_cl_insn *insn)
1839{
1840 if (mips_relax.sequence != 2)
1841 {
1842 unsigned int i;
1843
1844 for (i = ARRAY_SIZE (history); i-- > first;)
1845 if (i >= first + n)
1846 history[i] = history[i - n];
1847 else
1848 history[i] = *insn;
1849 }
1850}
1851
71400594
RS
1852/* Initialize vr4120_conflicts. There is a bit of duplication here:
1853 the idea is to make it obvious at a glance that each errata is
1854 included. */
1855
1856static void
1857init_vr4120_conflicts (void)
1858{
1859#define CONFLICT(FIRST, SECOND) \
1860 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1861
1862 /* Errata 21 - [D]DIV[U] after [D]MACC */
1863 CONFLICT (MACC, DIV);
1864 CONFLICT (DMACC, DIV);
1865
1866 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1867 CONFLICT (DMULT, DMULT);
1868 CONFLICT (DMULT, DMACC);
1869 CONFLICT (DMACC, DMULT);
1870 CONFLICT (DMACC, DMACC);
1871
1872 /* Errata 24 - MT{LO,HI} after [D]MACC */
1873 CONFLICT (MACC, MTHILO);
1874 CONFLICT (DMACC, MTHILO);
1875
1876 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1877 instruction is executed immediately after a MACC or DMACC
1878 instruction, the result of [either instruction] is incorrect." */
1879 CONFLICT (MACC, MULT);
1880 CONFLICT (MACC, DMULT);
1881 CONFLICT (DMACC, MULT);
1882 CONFLICT (DMACC, DMULT);
1883
1884 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1885 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1886 DDIV or DDIVU instruction, the result of the MACC or
1887 DMACC instruction is incorrect.". */
1888 CONFLICT (DMULT, MACC);
1889 CONFLICT (DMULT, DMACC);
1890 CONFLICT (DIV, MACC);
1891 CONFLICT (DIV, DMACC);
1892
1893#undef CONFLICT
1894}
1895
707bfff6
TS
1896struct regname {
1897 const char *name;
1898 unsigned int num;
1899};
1900
1901#define RTYPE_MASK 0x1ff00
1902#define RTYPE_NUM 0x00100
1903#define RTYPE_FPU 0x00200
1904#define RTYPE_FCC 0x00400
1905#define RTYPE_VEC 0x00800
1906#define RTYPE_GP 0x01000
1907#define RTYPE_CP0 0x02000
1908#define RTYPE_PC 0x04000
1909#define RTYPE_ACC 0x08000
1910#define RTYPE_CCC 0x10000
1911#define RNUM_MASK 0x000ff
1912#define RWARN 0x80000
1913
1914#define GENERIC_REGISTER_NUMBERS \
1915 {"$0", RTYPE_NUM | 0}, \
1916 {"$1", RTYPE_NUM | 1}, \
1917 {"$2", RTYPE_NUM | 2}, \
1918 {"$3", RTYPE_NUM | 3}, \
1919 {"$4", RTYPE_NUM | 4}, \
1920 {"$5", RTYPE_NUM | 5}, \
1921 {"$6", RTYPE_NUM | 6}, \
1922 {"$7", RTYPE_NUM | 7}, \
1923 {"$8", RTYPE_NUM | 8}, \
1924 {"$9", RTYPE_NUM | 9}, \
1925 {"$10", RTYPE_NUM | 10}, \
1926 {"$11", RTYPE_NUM | 11}, \
1927 {"$12", RTYPE_NUM | 12}, \
1928 {"$13", RTYPE_NUM | 13}, \
1929 {"$14", RTYPE_NUM | 14}, \
1930 {"$15", RTYPE_NUM | 15}, \
1931 {"$16", RTYPE_NUM | 16}, \
1932 {"$17", RTYPE_NUM | 17}, \
1933 {"$18", RTYPE_NUM | 18}, \
1934 {"$19", RTYPE_NUM | 19}, \
1935 {"$20", RTYPE_NUM | 20}, \
1936 {"$21", RTYPE_NUM | 21}, \
1937 {"$22", RTYPE_NUM | 22}, \
1938 {"$23", RTYPE_NUM | 23}, \
1939 {"$24", RTYPE_NUM | 24}, \
1940 {"$25", RTYPE_NUM | 25}, \
1941 {"$26", RTYPE_NUM | 26}, \
1942 {"$27", RTYPE_NUM | 27}, \
1943 {"$28", RTYPE_NUM | 28}, \
1944 {"$29", RTYPE_NUM | 29}, \
1945 {"$30", RTYPE_NUM | 30}, \
1946 {"$31", RTYPE_NUM | 31}
1947
1948#define FPU_REGISTER_NAMES \
1949 {"$f0", RTYPE_FPU | 0}, \
1950 {"$f1", RTYPE_FPU | 1}, \
1951 {"$f2", RTYPE_FPU | 2}, \
1952 {"$f3", RTYPE_FPU | 3}, \
1953 {"$f4", RTYPE_FPU | 4}, \
1954 {"$f5", RTYPE_FPU | 5}, \
1955 {"$f6", RTYPE_FPU | 6}, \
1956 {"$f7", RTYPE_FPU | 7}, \
1957 {"$f8", RTYPE_FPU | 8}, \
1958 {"$f9", RTYPE_FPU | 9}, \
1959 {"$f10", RTYPE_FPU | 10}, \
1960 {"$f11", RTYPE_FPU | 11}, \
1961 {"$f12", RTYPE_FPU | 12}, \
1962 {"$f13", RTYPE_FPU | 13}, \
1963 {"$f14", RTYPE_FPU | 14}, \
1964 {"$f15", RTYPE_FPU | 15}, \
1965 {"$f16", RTYPE_FPU | 16}, \
1966 {"$f17", RTYPE_FPU | 17}, \
1967 {"$f18", RTYPE_FPU | 18}, \
1968 {"$f19", RTYPE_FPU | 19}, \
1969 {"$f20", RTYPE_FPU | 20}, \
1970 {"$f21", RTYPE_FPU | 21}, \
1971 {"$f22", RTYPE_FPU | 22}, \
1972 {"$f23", RTYPE_FPU | 23}, \
1973 {"$f24", RTYPE_FPU | 24}, \
1974 {"$f25", RTYPE_FPU | 25}, \
1975 {"$f26", RTYPE_FPU | 26}, \
1976 {"$f27", RTYPE_FPU | 27}, \
1977 {"$f28", RTYPE_FPU | 28}, \
1978 {"$f29", RTYPE_FPU | 29}, \
1979 {"$f30", RTYPE_FPU | 30}, \
1980 {"$f31", RTYPE_FPU | 31}
1981
1982#define FPU_CONDITION_CODE_NAMES \
1983 {"$fcc0", RTYPE_FCC | 0}, \
1984 {"$fcc1", RTYPE_FCC | 1}, \
1985 {"$fcc2", RTYPE_FCC | 2}, \
1986 {"$fcc3", RTYPE_FCC | 3}, \
1987 {"$fcc4", RTYPE_FCC | 4}, \
1988 {"$fcc5", RTYPE_FCC | 5}, \
1989 {"$fcc6", RTYPE_FCC | 6}, \
1990 {"$fcc7", RTYPE_FCC | 7}
1991
1992#define COPROC_CONDITION_CODE_NAMES \
1993 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
1994 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
1995 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
1996 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
1997 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
1998 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
1999 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2000 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2001
2002#define N32N64_SYMBOLIC_REGISTER_NAMES \
2003 {"$a4", RTYPE_GP | 8}, \
2004 {"$a5", RTYPE_GP | 9}, \
2005 {"$a6", RTYPE_GP | 10}, \
2006 {"$a7", RTYPE_GP | 11}, \
2007 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2008 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2009 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2010 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2011 {"$t0", RTYPE_GP | 12}, \
2012 {"$t1", RTYPE_GP | 13}, \
2013 {"$t2", RTYPE_GP | 14}, \
2014 {"$t3", RTYPE_GP | 15}
2015
2016#define O32_SYMBOLIC_REGISTER_NAMES \
2017 {"$t0", RTYPE_GP | 8}, \
2018 {"$t1", RTYPE_GP | 9}, \
2019 {"$t2", RTYPE_GP | 10}, \
2020 {"$t3", RTYPE_GP | 11}, \
2021 {"$t4", RTYPE_GP | 12}, \
2022 {"$t5", RTYPE_GP | 13}, \
2023 {"$t6", RTYPE_GP | 14}, \
2024 {"$t7", RTYPE_GP | 15}, \
2025 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2026 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2027 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2028 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2029
2030/* Remaining symbolic register names */
2031#define SYMBOLIC_REGISTER_NAMES \
2032 {"$zero", RTYPE_GP | 0}, \
2033 {"$at", RTYPE_GP | 1}, \
2034 {"$AT", RTYPE_GP | 1}, \
2035 {"$v0", RTYPE_GP | 2}, \
2036 {"$v1", RTYPE_GP | 3}, \
2037 {"$a0", RTYPE_GP | 4}, \
2038 {"$a1", RTYPE_GP | 5}, \
2039 {"$a2", RTYPE_GP | 6}, \
2040 {"$a3", RTYPE_GP | 7}, \
2041 {"$s0", RTYPE_GP | 16}, \
2042 {"$s1", RTYPE_GP | 17}, \
2043 {"$s2", RTYPE_GP | 18}, \
2044 {"$s3", RTYPE_GP | 19}, \
2045 {"$s4", RTYPE_GP | 20}, \
2046 {"$s5", RTYPE_GP | 21}, \
2047 {"$s6", RTYPE_GP | 22}, \
2048 {"$s7", RTYPE_GP | 23}, \
2049 {"$t8", RTYPE_GP | 24}, \
2050 {"$t9", RTYPE_GP | 25}, \
2051 {"$k0", RTYPE_GP | 26}, \
2052 {"$kt0", RTYPE_GP | 26}, \
2053 {"$k1", RTYPE_GP | 27}, \
2054 {"$kt1", RTYPE_GP | 27}, \
2055 {"$gp", RTYPE_GP | 28}, \
2056 {"$sp", RTYPE_GP | 29}, \
2057 {"$s8", RTYPE_GP | 30}, \
2058 {"$fp", RTYPE_GP | 30}, \
2059 {"$ra", RTYPE_GP | 31}
2060
2061#define MIPS16_SPECIAL_REGISTER_NAMES \
2062 {"$pc", RTYPE_PC | 0}
2063
2064#define MDMX_VECTOR_REGISTER_NAMES \
2065 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2066 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2067 {"$v2", RTYPE_VEC | 2}, \
2068 {"$v3", RTYPE_VEC | 3}, \
2069 {"$v4", RTYPE_VEC | 4}, \
2070 {"$v5", RTYPE_VEC | 5}, \
2071 {"$v6", RTYPE_VEC | 6}, \
2072 {"$v7", RTYPE_VEC | 7}, \
2073 {"$v8", RTYPE_VEC | 8}, \
2074 {"$v9", RTYPE_VEC | 9}, \
2075 {"$v10", RTYPE_VEC | 10}, \
2076 {"$v11", RTYPE_VEC | 11}, \
2077 {"$v12", RTYPE_VEC | 12}, \
2078 {"$v13", RTYPE_VEC | 13}, \
2079 {"$v14", RTYPE_VEC | 14}, \
2080 {"$v15", RTYPE_VEC | 15}, \
2081 {"$v16", RTYPE_VEC | 16}, \
2082 {"$v17", RTYPE_VEC | 17}, \
2083 {"$v18", RTYPE_VEC | 18}, \
2084 {"$v19", RTYPE_VEC | 19}, \
2085 {"$v20", RTYPE_VEC | 20}, \
2086 {"$v21", RTYPE_VEC | 21}, \
2087 {"$v22", RTYPE_VEC | 22}, \
2088 {"$v23", RTYPE_VEC | 23}, \
2089 {"$v24", RTYPE_VEC | 24}, \
2090 {"$v25", RTYPE_VEC | 25}, \
2091 {"$v26", RTYPE_VEC | 26}, \
2092 {"$v27", RTYPE_VEC | 27}, \
2093 {"$v28", RTYPE_VEC | 28}, \
2094 {"$v29", RTYPE_VEC | 29}, \
2095 {"$v30", RTYPE_VEC | 30}, \
2096 {"$v31", RTYPE_VEC | 31}
2097
2098#define MIPS_DSP_ACCUMULATOR_NAMES \
2099 {"$ac0", RTYPE_ACC | 0}, \
2100 {"$ac1", RTYPE_ACC | 1}, \
2101 {"$ac2", RTYPE_ACC | 2}, \
2102 {"$ac3", RTYPE_ACC | 3}
2103
2104static const struct regname reg_names[] = {
2105 GENERIC_REGISTER_NUMBERS,
2106 FPU_REGISTER_NAMES,
2107 FPU_CONDITION_CODE_NAMES,
2108 COPROC_CONDITION_CODE_NAMES,
2109
2110 /* The $txx registers depends on the abi,
2111 these will be added later into the symbol table from
2112 one of the tables below once mips_abi is set after
2113 parsing of arguments from the command line. */
2114 SYMBOLIC_REGISTER_NAMES,
2115
2116 MIPS16_SPECIAL_REGISTER_NAMES,
2117 MDMX_VECTOR_REGISTER_NAMES,
2118 MIPS_DSP_ACCUMULATOR_NAMES,
2119 {0, 0}
2120};
2121
2122static const struct regname reg_names_o32[] = {
2123 O32_SYMBOLIC_REGISTER_NAMES,
2124 {0, 0}
2125};
2126
2127static const struct regname reg_names_n32n64[] = {
2128 N32N64_SYMBOLIC_REGISTER_NAMES,
2129 {0, 0}
2130};
2131
df58fc94
RS
2132/* Check if S points at a valid register specifier according to TYPES.
2133 If so, then return 1, advance S to consume the specifier and store
2134 the register's number in REGNOP, otherwise return 0. */
2135
707bfff6
TS
2136static int
2137reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2138{
2139 symbolS *symbolP;
2140 char *e;
2141 char save_c;
2142 int reg = -1;
2143
2144 /* Find end of name. */
2145 e = *s;
2146 if (is_name_beginner (*e))
2147 ++e;
2148 while (is_part_of_name (*e))
2149 ++e;
2150
2151 /* Terminate name. */
2152 save_c = *e;
2153 *e = '\0';
2154
2155 /* Look for a register symbol. */
2156 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2157 {
2158 int r = S_GET_VALUE (symbolP);
2159 if (r & types)
2160 reg = r & RNUM_MASK;
2161 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2162 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
2163 reg = (r & RNUM_MASK) - 2;
2164 }
2165 /* Else see if this is a register defined in an itbl entry. */
2166 else if ((types & RTYPE_GP) && itbl_have_entries)
2167 {
2168 char *n = *s;
2169 unsigned long r;
2170
2171 if (*n == '$')
2172 ++n;
2173 if (itbl_get_reg_val (n, &r))
2174 reg = r & RNUM_MASK;
2175 }
2176
2177 /* Advance to next token if a register was recognised. */
2178 if (reg >= 0)
2179 *s = e;
2180 else if (types & RWARN)
20203fb9 2181 as_warn (_("Unrecognized register name `%s'"), *s);
707bfff6
TS
2182
2183 *e = save_c;
2184 if (regnop)
2185 *regnop = reg;
2186 return reg >= 0;
2187}
2188
df58fc94
RS
2189/* Check if S points at a valid register list according to TYPES.
2190 If so, then return 1, advance S to consume the list and store
2191 the registers present on the list as a bitmask of ones in REGLISTP,
2192 otherwise return 0. A valid list comprises a comma-separated
2193 enumeration of valid single registers and/or dash-separated
2194 contiguous register ranges as determined by their numbers.
2195
2196 As a special exception if one of s0-s7 registers is specified as
2197 the range's lower delimiter and s8 (fp) is its upper one, then no
2198 registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2309ddf2 2199 are selected; they have to be listed separately if needed. */
df58fc94
RS
2200
2201static int
2202reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2203{
2204 unsigned int reglist = 0;
2205 unsigned int lastregno;
2206 bfd_boolean ok = TRUE;
2207 unsigned int regmask;
2309ddf2 2208 char *s_endlist = *s;
df58fc94 2209 char *s_reset = *s;
2309ddf2 2210 unsigned int regno;
df58fc94
RS
2211
2212 while (reg_lookup (s, types, &regno))
2213 {
2214 lastregno = regno;
2215 if (**s == '-')
2216 {
2217 (*s)++;
2218 ok = reg_lookup (s, types, &lastregno);
2219 if (ok && lastregno < regno)
2220 ok = FALSE;
2221 if (!ok)
2222 break;
2223 }
2224
2225 if (lastregno == FP && regno >= S0 && regno <= S7)
2226 {
2227 lastregno = S7;
2228 reglist |= 1 << FP;
2229 }
2230 regmask = 1 << lastregno;
2231 regmask = (regmask << 1) - 1;
2232 regmask ^= (1 << regno) - 1;
2233 reglist |= regmask;
2234
2309ddf2 2235 s_endlist = *s;
df58fc94
RS
2236 if (**s != ',')
2237 break;
2238 (*s)++;
2239 }
2240
2241 if (ok)
2309ddf2 2242 *s = s_endlist;
df58fc94
RS
2243 else
2244 *s = s_reset;
2245 if (reglistp)
2246 *reglistp = reglist;
2247 return ok && reglist != 0;
2248}
2249
037b32b9 2250/* Return TRUE if opcode MO is valid on the currently selected ISA and
f79e2745 2251 architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
2252
2253static bfd_boolean
f79e2745 2254is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
2255{
2256 int isa = mips_opts.isa;
2257 int fp_s, fp_d;
2258
2259 if (mips_opts.ase_mdmx)
2260 isa |= INSN_MDMX;
2261 if (mips_opts.ase_dsp)
2262 isa |= INSN_DSP;
2263 if (mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
2264 isa |= INSN_DSP64;
2265 if (mips_opts.ase_dspr2)
2266 isa |= INSN_DSPR2;
2267 if (mips_opts.ase_mt)
2268 isa |= INSN_MT;
2269 if (mips_opts.ase_mips3d)
2270 isa |= INSN_MIPS3D;
2271 if (mips_opts.ase_smartmips)
2272 isa |= INSN_SMARTMIPS;
dec0624d
MR
2273 if (mips_opts.ase_mcu)
2274 isa |= INSN_MCU;
b015e599
AP
2275 if (mips_opts.ase_virt)
2276 isa |= INSN_VIRT;
2277 if (mips_opts.ase_virt && ISA_SUPPORTS_VIRT64_ASE)
2278 isa |= INSN_VIRT64;
037b32b9 2279
35d0a169 2280 if (!opcode_is_member (mo, isa, mips_opts.arch))
037b32b9
AN
2281 return FALSE;
2282
2283 /* Check whether the instruction or macro requires single-precision or
2284 double-precision floating-point support. Note that this information is
2285 stored differently in the opcode table for insns and macros. */
2286 if (mo->pinfo == INSN_MACRO)
2287 {
2288 fp_s = mo->pinfo2 & INSN2_M_FP_S;
2289 fp_d = mo->pinfo2 & INSN2_M_FP_D;
2290 }
2291 else
2292 {
2293 fp_s = mo->pinfo & FP_S;
2294 fp_d = mo->pinfo & FP_D;
2295 }
2296
2297 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2298 return FALSE;
2299
2300 if (fp_s && mips_opts.soft_float)
2301 return FALSE;
2302
2303 return TRUE;
2304}
2305
2306/* Return TRUE if the MIPS16 opcode MO is valid on the currently
2307 selected ISA and architecture. */
2308
2309static bfd_boolean
2310is_opcode_valid_16 (const struct mips_opcode *mo)
2311{
35d0a169 2312 return opcode_is_member (mo, mips_opts.isa, mips_opts.arch);
037b32b9
AN
2313}
2314
df58fc94
RS
2315/* Return TRUE if the size of the microMIPS opcode MO matches one
2316 explicitly requested. Always TRUE in the standard MIPS mode. */
2317
2318static bfd_boolean
2319is_size_valid (const struct mips_opcode *mo)
2320{
2321 if (!mips_opts.micromips)
2322 return TRUE;
2323
2324 if (!forced_insn_length)
2325 return TRUE;
2326 if (mo->pinfo == INSN_MACRO)
2327 return FALSE;
2328 return forced_insn_length == micromips_insn_length (mo);
2329}
2330
2331/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
2332 of the preceding instruction. Always TRUE in the standard MIPS mode.
2333
2334 We don't accept macros in 16-bit delay slots to avoid a case where
2335 a macro expansion fails because it relies on a preceding 32-bit real
2336 instruction to have matched and does not handle the operands correctly.
2337 The only macros that may expand to 16-bit instructions are JAL that
2338 cannot be placed in a delay slot anyway, and corner cases of BALIGN
2339 and BGT (that likewise cannot be placed in a delay slot) that decay to
2340 a NOP. In all these cases the macros precede any corresponding real
2341 instruction definitions in the opcode table, so they will match in the
2342 second pass where the size of the delay slot is ignored and therefore
2343 produce correct code. */
df58fc94
RS
2344
2345static bfd_boolean
2346is_delay_slot_valid (const struct mips_opcode *mo)
2347{
2348 if (!mips_opts.micromips)
2349 return TRUE;
2350
2351 if (mo->pinfo == INSN_MACRO)
c06dec14 2352 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
2353 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2354 && micromips_insn_length (mo) != 4)
2355 return FALSE;
2356 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2357 && micromips_insn_length (mo) != 2)
2358 return FALSE;
2359
2360 return TRUE;
2361}
2362
707bfff6
TS
2363/* This function is called once, at assembler startup time. It should set up
2364 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 2365
252b5132 2366void
17a2f251 2367md_begin (void)
252b5132 2368{
3994f87e 2369 const char *retval = NULL;
156c2f8b 2370 int i = 0;
252b5132 2371 int broken = 0;
1f25f5d3 2372
0a44bf69
RS
2373 if (mips_pic != NO_PIC)
2374 {
2375 if (g_switch_seen && g_switch_value != 0)
2376 as_bad (_("-G may not be used in position-independent code"));
2377 g_switch_value = 0;
2378 }
2379
fef14a42 2380 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
2381 as_warn (_("Could not set architecture and machine"));
2382
252b5132
RH
2383 op_hash = hash_new ();
2384
2385 for (i = 0; i < NUMOPCODES;)
2386 {
2387 const char *name = mips_opcodes[i].name;
2388
17a2f251 2389 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
2390 if (retval != NULL)
2391 {
2392 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2393 mips_opcodes[i].name, retval);
2394 /* Probably a memory allocation problem? Give up now. */
2395 as_fatal (_("Broken assembler. No assembly attempted."));
2396 }
2397 do
2398 {
2399 if (mips_opcodes[i].pinfo != INSN_MACRO)
2400 {
2401 if (!validate_mips_insn (&mips_opcodes[i]))
2402 broken = 1;
1e915849
RS
2403 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2404 {
2405 create_insn (&nop_insn, mips_opcodes + i);
c67a084a
NC
2406 if (mips_fix_loongson2f_nop)
2407 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
1e915849
RS
2408 nop_insn.fixed_p = 1;
2409 }
252b5132
RH
2410 }
2411 ++i;
2412 }
2413 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2414 }
2415
2416 mips16_op_hash = hash_new ();
2417
2418 i = 0;
2419 while (i < bfd_mips16_num_opcodes)
2420 {
2421 const char *name = mips16_opcodes[i].name;
2422
17a2f251 2423 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
2424 if (retval != NULL)
2425 as_fatal (_("internal: can't hash `%s': %s"),
2426 mips16_opcodes[i].name, retval);
2427 do
2428 {
2429 if (mips16_opcodes[i].pinfo != INSN_MACRO
2430 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2431 != mips16_opcodes[i].match))
2432 {
2433 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2434 mips16_opcodes[i].name, mips16_opcodes[i].args);
2435 broken = 1;
2436 }
1e915849
RS
2437 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2438 {
2439 create_insn (&mips16_nop_insn, mips16_opcodes + i);
2440 mips16_nop_insn.fixed_p = 1;
2441 }
252b5132
RH
2442 ++i;
2443 }
2444 while (i < bfd_mips16_num_opcodes
2445 && strcmp (mips16_opcodes[i].name, name) == 0);
2446 }
2447
df58fc94
RS
2448 micromips_op_hash = hash_new ();
2449
2450 i = 0;
2451 while (i < bfd_micromips_num_opcodes)
2452 {
2453 const char *name = micromips_opcodes[i].name;
2454
2455 retval = hash_insert (micromips_op_hash, name,
2456 (void *) &micromips_opcodes[i]);
2457 if (retval != NULL)
2458 as_fatal (_("internal: can't hash `%s': %s"),
2459 micromips_opcodes[i].name, retval);
2460 do
2461 if (micromips_opcodes[i].pinfo != INSN_MACRO)
2462 {
2463 struct mips_cl_insn *micromips_nop_insn;
2464
2465 if (!validate_micromips_insn (&micromips_opcodes[i]))
2466 broken = 1;
2467
2468 if (micromips_insn_length (micromips_opcodes + i) == 2)
2469 micromips_nop_insn = &micromips_nop16_insn;
2470 else if (micromips_insn_length (micromips_opcodes + i) == 4)
2471 micromips_nop_insn = &micromips_nop32_insn;
2472 else
2473 continue;
2474
2475 if (micromips_nop_insn->insn_mo == NULL
2476 && strcmp (name, "nop") == 0)
2477 {
2478 create_insn (micromips_nop_insn, micromips_opcodes + i);
2479 micromips_nop_insn->fixed_p = 1;
2480 }
2481 }
2482 while (++i < bfd_micromips_num_opcodes
2483 && strcmp (micromips_opcodes[i].name, name) == 0);
2484 }
2485
252b5132
RH
2486 if (broken)
2487 as_fatal (_("Broken assembler. No assembly attempted."));
2488
2489 /* We add all the general register names to the symbol table. This
2490 helps us detect invalid uses of them. */
707bfff6
TS
2491 for (i = 0; reg_names[i].name; i++)
2492 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 2493 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
2494 &zero_address_frag));
2495 if (HAVE_NEWABI)
2496 for (i = 0; reg_names_n32n64[i].name; i++)
2497 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 2498 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 2499 &zero_address_frag));
707bfff6
TS
2500 else
2501 for (i = 0; reg_names_o32[i].name; i++)
2502 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 2503 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 2504 &zero_address_frag));
6047c971 2505
7d10b47d 2506 mips_no_prev_insn ();
252b5132
RH
2507
2508 mips_gprmask = 0;
2509 mips_cprmask[0] = 0;
2510 mips_cprmask[1] = 0;
2511 mips_cprmask[2] = 0;
2512 mips_cprmask[3] = 0;
2513
2514 /* set the default alignment for the text section (2**2) */
2515 record_alignment (text_section, 2);
2516
4d0d148d 2517 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 2518
707bfff6 2519#ifdef OBJ_ELF
f43abd2b 2520 if (IS_ELF)
252b5132 2521 {
0a44bf69
RS
2522 /* On a native system other than VxWorks, sections must be aligned
2523 to 16 byte boundaries. When configured for an embedded ELF
2524 target, we don't bother. */
c41e87e3
CF
2525 if (strncmp (TARGET_OS, "elf", 3) != 0
2526 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132
RH
2527 {
2528 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2529 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2530 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2531 }
2532
2533 /* Create a .reginfo section for register masks and a .mdebug
2534 section for debugging information. */
2535 {
2536 segT seg;
2537 subsegT subseg;
2538 flagword flags;
2539 segT sec;
2540
2541 seg = now_seg;
2542 subseg = now_subseg;
2543
2544 /* The ABI says this section should be loaded so that the
2545 running program can access it. However, we don't load it
2546 if we are configured for an embedded target */
2547 flags = SEC_READONLY | SEC_DATA;
c41e87e3 2548 if (strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
2549 flags |= SEC_ALLOC | SEC_LOAD;
2550
316f5878 2551 if (mips_abi != N64_ABI)
252b5132
RH
2552 {
2553 sec = subseg_new (".reginfo", (subsegT) 0);
2554
195325d2
TS
2555 bfd_set_section_flags (stdoutput, sec, flags);
2556 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 2557
252b5132 2558 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
252b5132
RH
2559 }
2560 else
2561 {
2562 /* The 64-bit ABI uses a .MIPS.options section rather than
2563 .reginfo section. */
2564 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
2565 bfd_set_section_flags (stdoutput, sec, flags);
2566 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 2567
252b5132
RH
2568 /* Set up the option header. */
2569 {
2570 Elf_Internal_Options opthdr;
2571 char *f;
2572
2573 opthdr.kind = ODK_REGINFO;
2574 opthdr.size = (sizeof (Elf_External_Options)
2575 + sizeof (Elf64_External_RegInfo));
2576 opthdr.section = 0;
2577 opthdr.info = 0;
2578 f = frag_more (sizeof (Elf_External_Options));
2579 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2580 (Elf_External_Options *) f);
2581
2582 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2583 }
252b5132
RH
2584 }
2585
2586 if (ECOFF_DEBUGGING)
2587 {
2588 sec = subseg_new (".mdebug", (subsegT) 0);
2589 (void) bfd_set_section_flags (stdoutput, sec,
2590 SEC_HAS_CONTENTS | SEC_READONLY);
2591 (void) bfd_set_section_alignment (stdoutput, sec, 2);
2592 }
f43abd2b 2593 else if (mips_flag_pdr)
ecb4347a
DJ
2594 {
2595 pdr_seg = subseg_new (".pdr", (subsegT) 0);
2596 (void) bfd_set_section_flags (stdoutput, pdr_seg,
2597 SEC_READONLY | SEC_RELOC
2598 | SEC_DEBUGGING);
2599 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2600 }
252b5132
RH
2601
2602 subseg_set (seg, subseg);
2603 }
2604 }
707bfff6 2605#endif /* OBJ_ELF */
252b5132
RH
2606
2607 if (! ECOFF_DEBUGGING)
2608 md_obj_begin ();
71400594
RS
2609
2610 if (mips_fix_vr4120)
2611 init_vr4120_conflicts ();
252b5132
RH
2612}
2613
2614void
17a2f251 2615md_mips_end (void)
252b5132 2616{
02b1ab82 2617 mips_emit_delays ();
252b5132
RH
2618 if (! ECOFF_DEBUGGING)
2619 md_obj_end ();
2620}
2621
2622void
17a2f251 2623md_assemble (char *str)
252b5132
RH
2624{
2625 struct mips_cl_insn insn;
f6688943
TS
2626 bfd_reloc_code_real_type unused_reloc[3]
2627 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
2628
2629 imm_expr.X_op = O_absent;
5f74bc13 2630 imm2_expr.X_op = O_absent;
252b5132 2631 offset_expr.X_op = O_absent;
f6688943
TS
2632 imm_reloc[0] = BFD_RELOC_UNUSED;
2633 imm_reloc[1] = BFD_RELOC_UNUSED;
2634 imm_reloc[2] = BFD_RELOC_UNUSED;
2635 offset_reloc[0] = BFD_RELOC_UNUSED;
2636 offset_reloc[1] = BFD_RELOC_UNUSED;
2637 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 2638
e1b47bd5
RS
2639 mips_mark_labels ();
2640 mips_assembling_insn = TRUE;
2641
252b5132
RH
2642 if (mips_opts.mips16)
2643 mips16_ip (str, &insn);
2644 else
2645 {
2646 mips_ip (str, &insn);
beae10d5
KH
2647 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2648 str, insn.insn_opcode));
252b5132
RH
2649 }
2650
2651 if (insn_error)
e1b47bd5
RS
2652 as_bad ("%s `%s'", insn_error, str);
2653 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 2654 {
584892a6 2655 macro_start ();
252b5132
RH
2656 if (mips_opts.mips16)
2657 mips16_macro (&insn);
2658 else
2659 macro (&insn);
584892a6 2660 macro_end ();
252b5132
RH
2661 }
2662 else
2663 {
2664 if (imm_expr.X_op != O_absent)
df58fc94 2665 append_insn (&insn, &imm_expr, imm_reloc, FALSE);
252b5132 2666 else if (offset_expr.X_op != O_absent)
df58fc94 2667 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 2668 else
df58fc94 2669 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 2670 }
e1b47bd5
RS
2671
2672 mips_assembling_insn = FALSE;
252b5132
RH
2673}
2674
738e5348
RS
2675/* Convenience functions for abstracting away the differences between
2676 MIPS16 and non-MIPS16 relocations. */
2677
2678static inline bfd_boolean
2679mips16_reloc_p (bfd_reloc_code_real_type reloc)
2680{
2681 switch (reloc)
2682 {
2683 case BFD_RELOC_MIPS16_JMP:
2684 case BFD_RELOC_MIPS16_GPREL:
2685 case BFD_RELOC_MIPS16_GOT16:
2686 case BFD_RELOC_MIPS16_CALL16:
2687 case BFD_RELOC_MIPS16_HI16_S:
2688 case BFD_RELOC_MIPS16_HI16:
2689 case BFD_RELOC_MIPS16_LO16:
2690 return TRUE;
2691
2692 default:
2693 return FALSE;
2694 }
2695}
2696
df58fc94
RS
2697static inline bfd_boolean
2698micromips_reloc_p (bfd_reloc_code_real_type reloc)
2699{
2700 switch (reloc)
2701 {
2702 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2703 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2704 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2705 case BFD_RELOC_MICROMIPS_GPREL16:
2706 case BFD_RELOC_MICROMIPS_JMP:
2707 case BFD_RELOC_MICROMIPS_HI16:
2708 case BFD_RELOC_MICROMIPS_HI16_S:
2709 case BFD_RELOC_MICROMIPS_LO16:
2710 case BFD_RELOC_MICROMIPS_LITERAL:
2711 case BFD_RELOC_MICROMIPS_GOT16:
2712 case BFD_RELOC_MICROMIPS_CALL16:
2713 case BFD_RELOC_MICROMIPS_GOT_HI16:
2714 case BFD_RELOC_MICROMIPS_GOT_LO16:
2715 case BFD_RELOC_MICROMIPS_CALL_HI16:
2716 case BFD_RELOC_MICROMIPS_CALL_LO16:
2717 case BFD_RELOC_MICROMIPS_SUB:
2718 case BFD_RELOC_MICROMIPS_GOT_PAGE:
2719 case BFD_RELOC_MICROMIPS_GOT_OFST:
2720 case BFD_RELOC_MICROMIPS_GOT_DISP:
2721 case BFD_RELOC_MICROMIPS_HIGHEST:
2722 case BFD_RELOC_MICROMIPS_HIGHER:
2723 case BFD_RELOC_MICROMIPS_SCN_DISP:
2724 case BFD_RELOC_MICROMIPS_JALR:
2725 return TRUE;
2726
2727 default:
2728 return FALSE;
2729 }
2730}
2731
2309ddf2
MR
2732static inline bfd_boolean
2733jmp_reloc_p (bfd_reloc_code_real_type reloc)
2734{
2735 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
2736}
2737
738e5348
RS
2738static inline bfd_boolean
2739got16_reloc_p (bfd_reloc_code_real_type reloc)
2740{
2309ddf2 2741 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 2742 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
2743}
2744
2745static inline bfd_boolean
2746hi16_reloc_p (bfd_reloc_code_real_type reloc)
2747{
2309ddf2 2748 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 2749 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
2750}
2751
2752static inline bfd_boolean
2753lo16_reloc_p (bfd_reloc_code_real_type reloc)
2754{
2309ddf2 2755 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
2756 || reloc == BFD_RELOC_MICROMIPS_LO16);
2757}
2758
df58fc94
RS
2759static inline bfd_boolean
2760jalr_reloc_p (bfd_reloc_code_real_type reloc)
2761{
2309ddf2 2762 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
2763}
2764
2de39019
CM
2765/* Return true if RELOC is a PC-relative relocation that does not have
2766 full address range. */
2767
2768static inline bfd_boolean
2769limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
2770{
2771 switch (reloc)
2772 {
2773 case BFD_RELOC_16_PCREL_S2:
2774 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2775 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2776 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2777 return TRUE;
2778
b47468a6
CM
2779 case BFD_RELOC_32_PCREL:
2780 return HAVE_64BIT_ADDRESSES;
2781
2de39019
CM
2782 default:
2783 return FALSE;
2784 }
2785}
b47468a6 2786
5919d012 2787/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
2788 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
2789 need a matching %lo() when applied to local symbols. */
5919d012
RS
2790
2791static inline bfd_boolean
17a2f251 2792reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 2793{
3b91255e 2794 return (HAVE_IN_PLACE_ADDENDS
738e5348 2795 && (hi16_reloc_p (reloc)
0a44bf69
RS
2796 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2797 all GOT16 relocations evaluate to "G". */
738e5348
RS
2798 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
2799}
2800
2801/* Return the type of %lo() reloc needed by RELOC, given that
2802 reloc_needs_lo_p. */
2803
2804static inline bfd_reloc_code_real_type
2805matching_lo_reloc (bfd_reloc_code_real_type reloc)
2806{
df58fc94
RS
2807 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
2808 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
2809 : BFD_RELOC_LO16));
5919d012
RS
2810}
2811
2812/* Return true if the given fixup is followed by a matching R_MIPS_LO16
2813 relocation. */
2814
2815static inline bfd_boolean
17a2f251 2816fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
2817{
2818 return (fixp->fx_next != NULL
738e5348 2819 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
2820 && fixp->fx_addsy == fixp->fx_next->fx_addsy
2821 && fixp->fx_offset == fixp->fx_next->fx_offset);
2822}
2823
252b5132
RH
2824/* This function returns true if modifying a register requires a
2825 delay. */
2826
2827static int
17a2f251 2828reg_needs_delay (unsigned int reg)
252b5132
RH
2829{
2830 unsigned long prev_pinfo;
2831
47e39b9d 2832 prev_pinfo = history[0].insn_mo->pinfo;
252b5132 2833 if (! mips_opts.noreorder
81912461
ILT
2834 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2835 && ! gpr_interlocks)
2836 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2837 && ! cop_interlocks)))
252b5132 2838 {
81912461
ILT
2839 /* A load from a coprocessor or from memory. All load delays
2840 delay the use of general register rt for one instruction. */
bdaaa2e1 2841 /* Itbl support may require additional care here. */
252b5132 2842 know (prev_pinfo & INSN_WRITE_GPR_T);
df58fc94 2843 if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
252b5132
RH
2844 return 1;
2845 }
2846
2847 return 0;
2848}
2849
462427c4
RS
2850/* Move all labels in LABELS to the current insertion point. TEXT_P
2851 says whether the labels refer to text or data. */
404a8071
RS
2852
2853static void
462427c4 2854mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
2855{
2856 struct insn_label_list *l;
2857 valueT val;
2858
462427c4 2859 for (l = labels; l != NULL; l = l->next)
404a8071 2860 {
9c2799c2 2861 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
2862 symbol_set_frag (l->label, frag_now);
2863 val = (valueT) frag_now_fix ();
df58fc94 2864 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 2865 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
2866 ++val;
2867 S_SET_VALUE (l->label, val);
2868 }
2869}
2870
462427c4
RS
2871/* Move all labels in insn_labels to the current insertion point
2872 and treat them as text labels. */
2873
2874static void
2875mips_move_text_labels (void)
2876{
2877 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
2878}
2879
5f0fe04b
TS
2880static bfd_boolean
2881s_is_linkonce (symbolS *sym, segT from_seg)
2882{
2883 bfd_boolean linkonce = FALSE;
2884 segT symseg = S_GET_SEGMENT (sym);
2885
2886 if (symseg != from_seg && !S_IS_LOCAL (sym))
2887 {
2888 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2889 linkonce = TRUE;
2890#ifdef OBJ_ELF
2891 /* The GNU toolchain uses an extension for ELF: a section
2892 beginning with the magic string .gnu.linkonce is a
2893 linkonce section. */
2894 if (strncmp (segment_name (symseg), ".gnu.linkonce",
2895 sizeof ".gnu.linkonce" - 1) == 0)
2896 linkonce = TRUE;
2897#endif
2898 }
2899 return linkonce;
2900}
2901
e1b47bd5 2902/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
2903 linker to handle them specially, such as generating jalx instructions
2904 when needed. We also make them odd for the duration of the assembly,
2905 in order to generate the right sort of code. We will make them even
252b5132
RH
2906 in the adjust_symtab routine, while leaving them marked. This is
2907 convenient for the debugger and the disassembler. The linker knows
2908 to make them odd again. */
2909
2910static void
e1b47bd5 2911mips_compressed_mark_label (symbolS *label)
252b5132 2912{
df58fc94 2913 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 2914
a8dbcb85 2915#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
e1b47bd5
RS
2916 if (IS_ELF)
2917 {
2918 if (mips_opts.mips16)
2919 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
2920 else
2921 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
252b5132 2922 }
e1b47bd5
RS
2923#endif
2924 if ((S_GET_VALUE (label) & 1) == 0
2925 /* Don't adjust the address if the label is global or weak, or
2926 in a link-once section, since we'll be emitting symbol reloc
2927 references to it which will be patched up by the linker, and
2928 the final value of the symbol may or may not be MIPS16/microMIPS. */
2929 && !S_IS_WEAK (label)
2930 && !S_IS_EXTERNAL (label)
2931 && !s_is_linkonce (label, now_seg))
2932 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
2933}
2934
2935/* Mark preceding MIPS16 or microMIPS instruction labels. */
2936
2937static void
2938mips_compressed_mark_labels (void)
2939{
2940 struct insn_label_list *l;
2941
2942 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
2943 mips_compressed_mark_label (l->label);
252b5132
RH
2944}
2945
4d7206a2
RS
2946/* End the current frag. Make it a variant frag and record the
2947 relaxation info. */
2948
2949static void
2950relax_close_frag (void)
2951{
584892a6 2952 mips_macro_warning.first_frag = frag_now;
4d7206a2 2953 frag_var (rs_machine_dependent, 0, 0,
584892a6 2954 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
2955 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2956
2957 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2958 mips_relax.first_fixup = 0;
2959}
2960
2961/* Start a new relaxation sequence whose expansion depends on SYMBOL.
2962 See the comment above RELAX_ENCODE for more details. */
2963
2964static void
2965relax_start (symbolS *symbol)
2966{
9c2799c2 2967 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
2968 mips_relax.sequence = 1;
2969 mips_relax.symbol = symbol;
2970}
2971
2972/* Start generating the second version of a relaxable sequence.
2973 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
2974
2975static void
4d7206a2
RS
2976relax_switch (void)
2977{
9c2799c2 2978 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
2979 mips_relax.sequence = 2;
2980}
2981
2982/* End the current relaxable sequence. */
2983
2984static void
2985relax_end (void)
2986{
9c2799c2 2987 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
2988 relax_close_frag ();
2989 mips_relax.sequence = 0;
2990}
2991
11625dd8
RS
2992/* Return true if IP is a delayed branch or jump. */
2993
2994static inline bfd_boolean
2995delayed_branch_p (const struct mips_cl_insn *ip)
2996{
2997 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2998 | INSN_COND_BRANCH_DELAY
2999 | INSN_COND_BRANCH_LIKELY)) != 0;
3000}
3001
3002/* Return true if IP is a compact branch or jump. */
3003
3004static inline bfd_boolean
3005compact_branch_p (const struct mips_cl_insn *ip)
3006{
3007 if (mips_opts.mips16)
3008 return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
3009 | MIPS16_INSN_COND_BRANCH)) != 0;
3010 else
3011 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
3012 | INSN2_COND_BRANCH)) != 0;
3013}
3014
3015/* Return true if IP is an unconditional branch or jump. */
3016
3017static inline bfd_boolean
3018uncond_branch_p (const struct mips_cl_insn *ip)
3019{
3020 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
3021 || (mips_opts.mips16
3022 ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
3023 : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
3024}
3025
3026/* Return true if IP is a branch-likely instruction. */
3027
3028static inline bfd_boolean
3029branch_likely_p (const struct mips_cl_insn *ip)
3030{
3031 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
3032}
3033
14fe068b
RS
3034/* Return the type of nop that should be used to fill the delay slot
3035 of delayed branch IP. */
3036
3037static struct mips_cl_insn *
3038get_delay_slot_nop (const struct mips_cl_insn *ip)
3039{
3040 if (mips_opts.micromips
3041 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3042 return &micromips_nop32_insn;
3043 return NOP_INSN;
3044}
3045
2309ddf2 3046/* Return the mask of core registers that IP reads or writes. */
df58fc94
RS
3047
3048static unsigned int
3049gpr_mod_mask (const struct mips_cl_insn *ip)
3050{
2309ddf2 3051 unsigned long pinfo2;
df58fc94
RS
3052 unsigned int mask;
3053
3054 mask = 0;
df58fc94
RS
3055 pinfo2 = ip->insn_mo->pinfo2;
3056 if (mips_opts.micromips)
3057 {
df58fc94
RS
3058 if (pinfo2 & INSN2_MOD_GPR_MD)
3059 mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
df58fc94
RS
3060 if (pinfo2 & INSN2_MOD_GPR_MF)
3061 mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
df58fc94
RS
3062 if (pinfo2 & INSN2_MOD_SP)
3063 mask |= 1 << SP;
3064 }
3065 return mask;
3066}
3067
4c260379
RS
3068/* Return the mask of core registers that IP reads. */
3069
3070static unsigned int
3071gpr_read_mask (const struct mips_cl_insn *ip)
3072{
3073 unsigned long pinfo, pinfo2;
3074 unsigned int mask;
3075
df58fc94 3076 mask = gpr_mod_mask (ip);
4c260379
RS
3077 pinfo = ip->insn_mo->pinfo;
3078 pinfo2 = ip->insn_mo->pinfo2;
3079 if (mips_opts.mips16)
3080 {
3081 if (pinfo & MIPS16_INSN_READ_X)
3082 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3083 if (pinfo & MIPS16_INSN_READ_Y)
3084 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3085 if (pinfo & MIPS16_INSN_READ_T)
3086 mask |= 1 << TREG;
3087 if (pinfo & MIPS16_INSN_READ_SP)
3088 mask |= 1 << SP;
3089 if (pinfo & MIPS16_INSN_READ_31)
3090 mask |= 1 << RA;
3091 if (pinfo & MIPS16_INSN_READ_Z)
3092 mask |= 1 << (mips16_to_32_reg_map
3093 [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3094 if (pinfo & MIPS16_INSN_READ_GPR_X)
3095 mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3096 }
3097 else
3098 {
3099 if (pinfo2 & INSN2_READ_GPR_D)
2309ddf2 3100 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3101 if (pinfo & INSN_READ_GPR_T)
2309ddf2 3102 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4c260379 3103 if (pinfo & INSN_READ_GPR_S)
2309ddf2
MR
3104 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3105 if (pinfo2 & INSN2_READ_GP)
3106 mask |= 1 << GP;
3107 if (pinfo2 & INSN2_READ_GPR_31)
3108 mask |= 1 << RA;
4c260379 3109 if (pinfo2 & INSN2_READ_GPR_Z)
2309ddf2 3110 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3111 }
2b0c8b40
MR
3112 if (mips_opts.micromips)
3113 {
3114 if (pinfo2 & INSN2_READ_GPR_MC)
3115 mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3116 if (pinfo2 & INSN2_READ_GPR_ME)
3117 mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3118 if (pinfo2 & INSN2_READ_GPR_MG)
3119 mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3120 if (pinfo2 & INSN2_READ_GPR_MJ)
3121 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3122 if (pinfo2 & INSN2_READ_GPR_MMN)
3123 {
3124 mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3125 mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3126 }
3127 if (pinfo2 & INSN2_READ_GPR_MP)
3128 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3129 if (pinfo2 & INSN2_READ_GPR_MQ)
3130 mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3131 }
fe35f09f
RS
3132 /* Don't include register 0. */
3133 return mask & ~1;
4c260379
RS
3134}
3135
3136/* Return the mask of core registers that IP writes. */
3137
3138static unsigned int
3139gpr_write_mask (const struct mips_cl_insn *ip)
3140{
3141 unsigned long pinfo, pinfo2;
3142 unsigned int mask;
3143
df58fc94 3144 mask = gpr_mod_mask (ip);
4c260379
RS
3145 pinfo = ip->insn_mo->pinfo;
3146 pinfo2 = ip->insn_mo->pinfo2;
3147 if (mips_opts.mips16)
3148 {
3149 if (pinfo & MIPS16_INSN_WRITE_X)
3150 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3151 if (pinfo & MIPS16_INSN_WRITE_Y)
3152 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3153 if (pinfo & MIPS16_INSN_WRITE_Z)
3154 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3155 if (pinfo & MIPS16_INSN_WRITE_T)
3156 mask |= 1 << TREG;
3157 if (pinfo & MIPS16_INSN_WRITE_SP)
3158 mask |= 1 << SP;
3159 if (pinfo & MIPS16_INSN_WRITE_31)
3160 mask |= 1 << RA;
3161 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3162 mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3163 }
3164 else
3165 {
3166 if (pinfo & INSN_WRITE_GPR_D)
df58fc94 3167 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3168 if (pinfo & INSN_WRITE_GPR_T)
df58fc94 3169 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
2b0c8b40 3170 if (pinfo & INSN_WRITE_GPR_S)
2309ddf2 3171 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379
RS
3172 if (pinfo & INSN_WRITE_GPR_31)
3173 mask |= 1 << RA;
3174 if (pinfo2 & INSN2_WRITE_GPR_Z)
df58fc94 3175 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3176 }
2b0c8b40
MR
3177 if (mips_opts.micromips)
3178 {
3179 if (pinfo2 & INSN2_WRITE_GPR_MB)
3180 mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3181 if (pinfo2 & INSN2_WRITE_GPR_MHI)
3182 {
3183 mask |= 1 << micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
3184 mask |= 1 << micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
3185 }
3186 if (pinfo2 & INSN2_WRITE_GPR_MJ)
3187 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3188 if (pinfo2 & INSN2_WRITE_GPR_MP)
3189 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3190 }
fe35f09f
RS
3191 /* Don't include register 0. */
3192 return mask & ~1;
4c260379
RS
3193}
3194
3195/* Return the mask of floating-point registers that IP reads. */
3196
3197static unsigned int
3198fpr_read_mask (const struct mips_cl_insn *ip)
3199{
3200 unsigned long pinfo, pinfo2;
3201 unsigned int mask;
3202
3203 mask = 0;
3204 pinfo = ip->insn_mo->pinfo;
3205 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3206 if (!mips_opts.mips16)
df58fc94
RS
3207 {
3208 if (pinfo2 & INSN2_READ_FPR_D)
2309ddf2 3209 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3210 if (pinfo & INSN_READ_FPR_S)
df58fc94 3211 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3212 if (pinfo & INSN_READ_FPR_T)
df58fc94 3213 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3214 if (pinfo & INSN_READ_FPR_R)
df58fc94 3215 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
4c260379 3216 if (pinfo2 & INSN2_READ_FPR_Z)
df58fc94 3217 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3218 }
3219 /* Conservatively treat all operands to an FP_D instruction are doubles.
3220 (This is overly pessimistic for things like cvt.d.s.) */
3221 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3222 mask |= mask << 1;
3223 return mask;
3224}
3225
3226/* Return the mask of floating-point registers that IP writes. */
3227
3228static unsigned int
3229fpr_write_mask (const struct mips_cl_insn *ip)
3230{
3231 unsigned long pinfo, pinfo2;
3232 unsigned int mask;
3233
3234 mask = 0;
3235 pinfo = ip->insn_mo->pinfo;
3236 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3237 if (!mips_opts.mips16)
4c260379
RS
3238 {
3239 if (pinfo & INSN_WRITE_FPR_D)
df58fc94 3240 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3241 if (pinfo & INSN_WRITE_FPR_S)
df58fc94 3242 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3243 if (pinfo & INSN_WRITE_FPR_T)
df58fc94 3244 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3245 if (pinfo2 & INSN2_WRITE_FPR_Z)
df58fc94 3246 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3247 }
3248 /* Conservatively treat all operands to an FP_D instruction are doubles.
3249 (This is overly pessimistic for things like cvt.s.d.) */
3250 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3251 mask |= mask << 1;
3252 return mask;
3253}
3254
71400594
RS
3255/* Classify an instruction according to the FIX_VR4120_* enumeration.
3256 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3257 by VR4120 errata. */
4d7206a2 3258
71400594
RS
3259static unsigned int
3260classify_vr4120_insn (const char *name)
252b5132 3261{
71400594
RS
3262 if (strncmp (name, "macc", 4) == 0)
3263 return FIX_VR4120_MACC;
3264 if (strncmp (name, "dmacc", 5) == 0)
3265 return FIX_VR4120_DMACC;
3266 if (strncmp (name, "mult", 4) == 0)
3267 return FIX_VR4120_MULT;
3268 if (strncmp (name, "dmult", 5) == 0)
3269 return FIX_VR4120_DMULT;
3270 if (strstr (name, "div"))
3271 return FIX_VR4120_DIV;
3272 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3273 return FIX_VR4120_MTHILO;
3274 return NUM_FIX_VR4120_CLASSES;
3275}
252b5132 3276
ff239038
CM
3277#define INSN_ERET 0x42000018
3278#define INSN_DERET 0x4200001f
3279
71400594
RS
3280/* Return the number of instructions that must separate INSN1 and INSN2,
3281 where INSN1 is the earlier instruction. Return the worst-case value
3282 for any INSN2 if INSN2 is null. */
252b5132 3283
71400594
RS
3284static unsigned int
3285insns_between (const struct mips_cl_insn *insn1,
3286 const struct mips_cl_insn *insn2)
3287{
3288 unsigned long pinfo1, pinfo2;
4c260379 3289 unsigned int mask;
71400594
RS
3290
3291 /* This function needs to know which pinfo flags are set for INSN2
3292 and which registers INSN2 uses. The former is stored in PINFO2 and
4c260379
RS
3293 the latter is tested via INSN2_USES_GPR. If INSN2 is null, PINFO2
3294 will have every flag set and INSN2_USES_GPR will always return true. */
71400594
RS
3295 pinfo1 = insn1->insn_mo->pinfo;
3296 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 3297
4c260379
RS
3298#define INSN2_USES_GPR(REG) \
3299 (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
71400594
RS
3300
3301 /* For most targets, write-after-read dependencies on the HI and LO
3302 registers must be separated by at least two instructions. */
3303 if (!hilo_interlocks)
252b5132 3304 {
71400594
RS
3305 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3306 return 2;
3307 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3308 return 2;
3309 }
3310
3311 /* If we're working around r7000 errata, there must be two instructions
3312 between an mfhi or mflo and any instruction that uses the result. */
3313 if (mips_7000_hilo_fix
df58fc94 3314 && !mips_opts.micromips
71400594 3315 && MF_HILO_INSN (pinfo1)
df58fc94 3316 && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
71400594
RS
3317 return 2;
3318
ff239038
CM
3319 /* If we're working around 24K errata, one instruction is required
3320 if an ERET or DERET is followed by a branch instruction. */
df58fc94 3321 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
3322 {
3323 if (insn1->insn_opcode == INSN_ERET
3324 || insn1->insn_opcode == INSN_DERET)
3325 {
3326 if (insn2 == NULL
3327 || insn2->insn_opcode == INSN_ERET
3328 || insn2->insn_opcode == INSN_DERET
11625dd8 3329 || delayed_branch_p (insn2))
ff239038
CM
3330 return 1;
3331 }
3332 }
3333
71400594
RS
3334 /* If working around VR4120 errata, check for combinations that need
3335 a single intervening instruction. */
df58fc94 3336 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
3337 {
3338 unsigned int class1, class2;
252b5132 3339
71400594
RS
3340 class1 = classify_vr4120_insn (insn1->insn_mo->name);
3341 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 3342 {
71400594
RS
3343 if (insn2 == NULL)
3344 return 1;
3345 class2 = classify_vr4120_insn (insn2->insn_mo->name);
3346 if (vr4120_conflicts[class1] & (1 << class2))
3347 return 1;
252b5132 3348 }
71400594
RS
3349 }
3350
df58fc94 3351 if (!HAVE_CODE_COMPRESSION)
71400594
RS
3352 {
3353 /* Check for GPR or coprocessor load delays. All such delays
3354 are on the RT register. */
3355 /* Itbl support may require additional care here. */
3356 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3357 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 3358 {
71400594 3359 know (pinfo1 & INSN_WRITE_GPR_T);
df58fc94 3360 if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
71400594
RS
3361 return 1;
3362 }
3363
3364 /* Check for generic coprocessor hazards.
3365
3366 This case is not handled very well. There is no special
3367 knowledge of CP0 handling, and the coprocessors other than
3368 the floating point unit are not distinguished at all. */
3369 /* Itbl support may require additional care here. FIXME!
3370 Need to modify this to include knowledge about
3371 user specified delays! */
3372 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3373 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3374 {
3375 /* Handle cases where INSN1 writes to a known general coprocessor
3376 register. There must be a one instruction delay before INSN2
3377 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
3378 mask = fpr_write_mask (insn1);
3379 if (mask != 0)
252b5132 3380 {
4c260379 3381 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 3382 return 1;
252b5132
RH
3383 }
3384 else
3385 {
71400594
RS
3386 /* Read-after-write dependencies on the control registers
3387 require a two-instruction gap. */
3388 if ((pinfo1 & INSN_WRITE_COND_CODE)
3389 && (pinfo2 & INSN_READ_COND_CODE))
3390 return 2;
3391
3392 /* We don't know exactly what INSN1 does. If INSN2 is
3393 also a coprocessor instruction, assume there must be
3394 a one instruction gap. */
3395 if (pinfo2 & INSN_COP)
3396 return 1;
252b5132
RH
3397 }
3398 }
6b76fefe 3399
71400594
RS
3400 /* Check for read-after-write dependencies on the coprocessor
3401 control registers in cases where INSN1 does not need a general
3402 coprocessor delay. This means that INSN1 is a floating point
3403 comparison instruction. */
3404 /* Itbl support may require additional care here. */
3405 else if (!cop_interlocks
3406 && (pinfo1 & INSN_WRITE_COND_CODE)
3407 && (pinfo2 & INSN_READ_COND_CODE))
3408 return 1;
3409 }
6b76fefe 3410
4c260379 3411#undef INSN2_USES_GPR
6b76fefe 3412
71400594
RS
3413 return 0;
3414}
6b76fefe 3415
7d8e00cf
RS
3416/* Return the number of nops that would be needed to work around the
3417 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
3418 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
3419 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
3420
3421static int
932d1a1b 3422nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
3423 const struct mips_cl_insn *insn)
3424{
4c260379
RS
3425 int i, j;
3426 unsigned int mask;
7d8e00cf
RS
3427
3428 /* Check if the instruction writes to HI or LO. MTHI and MTLO
3429 are not affected by the errata. */
3430 if (insn != 0
3431 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3432 || strcmp (insn->insn_mo->name, "mtlo") == 0
3433 || strcmp (insn->insn_mo->name, "mthi") == 0))
3434 return 0;
3435
3436 /* Search for the first MFLO or MFHI. */
3437 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 3438 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
3439 {
3440 /* Extract the destination register. */
4c260379 3441 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
3442
3443 /* No nops are needed if INSN reads that register. */
4c260379 3444 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
3445 return 0;
3446
3447 /* ...or if any of the intervening instructions do. */
3448 for (j = 0; j < i; j++)
4c260379 3449 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
3450 return 0;
3451
932d1a1b
RS
3452 if (i >= ignore)
3453 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
3454 }
3455 return 0;
3456}
3457
15be625d
CM
3458#define BASE_REG_EQ(INSN1, INSN2) \
3459 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3460 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3461
3462/* Return the minimum alignment for this store instruction. */
3463
3464static int
3465fix_24k_align_to (const struct mips_opcode *mo)
3466{
3467 if (strcmp (mo->name, "sh") == 0)
3468 return 2;
3469
3470 if (strcmp (mo->name, "swc1") == 0
3471 || strcmp (mo->name, "swc2") == 0
3472 || strcmp (mo->name, "sw") == 0
3473 || strcmp (mo->name, "sc") == 0
3474 || strcmp (mo->name, "s.s") == 0)
3475 return 4;
3476
3477 if (strcmp (mo->name, "sdc1") == 0
3478 || strcmp (mo->name, "sdc2") == 0
3479 || strcmp (mo->name, "s.d") == 0)
3480 return 8;
3481
3482 /* sb, swl, swr */
3483 return 1;
3484}
3485
3486struct fix_24k_store_info
3487 {
3488 /* Immediate offset, if any, for this store instruction. */
3489 short off;
3490 /* Alignment required by this store instruction. */
3491 int align_to;
3492 /* True for register offsets. */
3493 int register_offset;
3494 };
3495
3496/* Comparison function used by qsort. */
3497
3498static int
3499fix_24k_sort (const void *a, const void *b)
3500{
3501 const struct fix_24k_store_info *pos1 = a;
3502 const struct fix_24k_store_info *pos2 = b;
3503
3504 return (pos1->off - pos2->off);
3505}
3506
3507/* INSN is a store instruction. Try to record the store information
3508 in STINFO. Return false if the information isn't known. */
3509
3510static bfd_boolean
3511fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 3512 const struct mips_cl_insn *insn)
15be625d
CM
3513{
3514 /* The instruction must have a known offset. */
3515 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3516 return FALSE;
3517
3518 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3519 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3520 return TRUE;
3521}
3522
932d1a1b
RS
3523/* Return the number of nops that would be needed to work around the 24k
3524 "lost data on stores during refill" errata if instruction INSN
3525 immediately followed the 2 instructions described by HIST.
3526 Ignore hazards that are contained within the first IGNORE
3527 instructions of HIST.
3528
3529 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3530 for the data cache refills and store data. The following describes
3531 the scenario where the store data could be lost.
3532
3533 * A data cache miss, due to either a load or a store, causing fill
3534 data to be supplied by the memory subsystem
3535 * The first three doublewords of fill data are returned and written
3536 into the cache
3537 * A sequence of four stores occurs in consecutive cycles around the
3538 final doubleword of the fill:
3539 * Store A
3540 * Store B
3541 * Store C
3542 * Zero, One or more instructions
3543 * Store D
3544
3545 The four stores A-D must be to different doublewords of the line that
3546 is being filled. The fourth instruction in the sequence above permits
3547 the fill of the final doubleword to be transferred from the FSB into
3548 the cache. In the sequence above, the stores may be either integer
3549 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3550 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3551 different doublewords on the line. If the floating point unit is
3552 running in 1:2 mode, it is not possible to create the sequence above
3553 using only floating point store instructions.
15be625d
CM
3554
3555 In this case, the cache line being filled is incorrectly marked
3556 invalid, thereby losing the data from any store to the line that
3557 occurs between the original miss and the completion of the five
3558 cycle sequence shown above.
3559
932d1a1b 3560 The workarounds are:
15be625d 3561
932d1a1b
RS
3562 * Run the data cache in write-through mode.
3563 * Insert a non-store instruction between
3564 Store A and Store B or Store B and Store C. */
15be625d
CM
3565
3566static int
932d1a1b 3567nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
3568 const struct mips_cl_insn *insn)
3569{
3570 struct fix_24k_store_info pos[3];
3571 int align, i, base_offset;
3572
932d1a1b
RS
3573 if (ignore >= 2)
3574 return 0;
3575
ab9794cf
RS
3576 /* If the previous instruction wasn't a store, there's nothing to
3577 worry about. */
15be625d
CM
3578 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3579 return 0;
3580
ab9794cf
RS
3581 /* If the instructions after the previous one are unknown, we have
3582 to assume the worst. */
3583 if (!insn)
15be625d
CM
3584 return 1;
3585
ab9794cf
RS
3586 /* Check whether we are dealing with three consecutive stores. */
3587 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3588 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
3589 return 0;
3590
3591 /* If we don't know the relationship between the store addresses,
3592 assume the worst. */
ab9794cf 3593 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
3594 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3595 return 1;
3596
3597 if (!fix_24k_record_store_info (&pos[0], insn)
3598 || !fix_24k_record_store_info (&pos[1], &hist[0])
3599 || !fix_24k_record_store_info (&pos[2], &hist[1]))
3600 return 1;
3601
3602 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3603
3604 /* Pick a value of ALIGN and X such that all offsets are adjusted by
3605 X bytes and such that the base register + X is known to be aligned
3606 to align bytes. */
3607
3608 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3609 align = 8;
3610 else
3611 {
3612 align = pos[0].align_to;
3613 base_offset = pos[0].off;
3614 for (i = 1; i < 3; i++)
3615 if (align < pos[i].align_to)
3616 {
3617 align = pos[i].align_to;
3618 base_offset = pos[i].off;
3619 }
3620 for (i = 0; i < 3; i++)
3621 pos[i].off -= base_offset;
3622 }
3623
3624 pos[0].off &= ~align + 1;
3625 pos[1].off &= ~align + 1;
3626 pos[2].off &= ~align + 1;
3627
3628 /* If any two stores write to the same chunk, they also write to the
3629 same doubleword. The offsets are still sorted at this point. */
3630 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3631 return 0;
3632
3633 /* A range of at least 9 bytes is needed for the stores to be in
3634 non-overlapping doublewords. */
3635 if (pos[2].off - pos[0].off <= 8)
3636 return 0;
3637
3638 if (pos[2].off - pos[1].off >= 24
3639 || pos[1].off - pos[0].off >= 24
3640 || pos[2].off - pos[0].off >= 32)
3641 return 0;
3642
3643 return 1;
3644}
3645
71400594 3646/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 3647 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
3648 where HIST[0] is the most recent instruction. Ignore hazards
3649 between INSN and the first IGNORE instructions in HIST.
3650
3651 If INSN is null, return the worse-case number of nops for any
3652 instruction. */
bdaaa2e1 3653
71400594 3654static int
932d1a1b 3655nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
3656 const struct mips_cl_insn *insn)
3657{
3658 int i, nops, tmp_nops;
bdaaa2e1 3659
71400594 3660 nops = 0;
932d1a1b 3661 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 3662 {
91d6fa6a 3663 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
3664 if (tmp_nops > nops)
3665 nops = tmp_nops;
3666 }
7d8e00cf 3667
df58fc94 3668 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 3669 {
932d1a1b 3670 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
3671 if (tmp_nops > nops)
3672 nops = tmp_nops;
3673 }
3674
df58fc94 3675 if (mips_fix_24k && !mips_opts.micromips)
15be625d 3676 {
932d1a1b 3677 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
3678 if (tmp_nops > nops)
3679 nops = tmp_nops;
3680 }
3681
71400594
RS
3682 return nops;
3683}
252b5132 3684
71400594 3685/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 3686 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
3687 would be needed after the extended sequence, ignoring hazards
3688 in the first IGNORE instructions. */
252b5132 3689
71400594 3690static int
932d1a1b
RS
3691nops_for_sequence (int num_insns, int ignore,
3692 const struct mips_cl_insn *hist, ...)
71400594
RS
3693{
3694 va_list args;
3695 struct mips_cl_insn buffer[MAX_NOPS];
3696 struct mips_cl_insn *cursor;
3697 int nops;
3698
91d6fa6a 3699 va_start (args, hist);
71400594 3700 cursor = buffer + num_insns;
91d6fa6a 3701 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
3702 while (cursor > buffer)
3703 *--cursor = *va_arg (args, const struct mips_cl_insn *);
3704
932d1a1b 3705 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
3706 va_end (args);
3707 return nops;
3708}
252b5132 3709
71400594
RS
3710/* Like nops_for_insn, but if INSN is a branch, take into account the
3711 worst-case delay for the branch target. */
252b5132 3712
71400594 3713static int
932d1a1b 3714nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
3715 const struct mips_cl_insn *insn)
3716{
3717 int nops, tmp_nops;
60b63b72 3718
932d1a1b 3719 nops = nops_for_insn (ignore, hist, insn);
11625dd8 3720 if (delayed_branch_p (insn))
71400594 3721 {
932d1a1b 3722 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 3723 hist, insn, get_delay_slot_nop (insn));
71400594
RS
3724 if (tmp_nops > nops)
3725 nops = tmp_nops;
3726 }
11625dd8 3727 else if (compact_branch_p (insn))
71400594 3728 {
932d1a1b 3729 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
3730 if (tmp_nops > nops)
3731 nops = tmp_nops;
3732 }
3733 return nops;
3734}
3735
c67a084a
NC
3736/* Fix NOP issue: Replace nops by "or at,at,zero". */
3737
3738static void
3739fix_loongson2f_nop (struct mips_cl_insn * ip)
3740{
df58fc94 3741 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
3742 if (strcmp (ip->insn_mo->name, "nop") == 0)
3743 ip->insn_opcode = LOONGSON2F_NOP_INSN;
3744}
3745
3746/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
3747 jr target pc &= 'hffff_ffff_cfff_ffff. */
3748
3749static void
3750fix_loongson2f_jump (struct mips_cl_insn * ip)
3751{
df58fc94 3752 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
3753 if (strcmp (ip->insn_mo->name, "j") == 0
3754 || strcmp (ip->insn_mo->name, "jr") == 0
3755 || strcmp (ip->insn_mo->name, "jalr") == 0)
3756 {
3757 int sreg;
3758 expressionS ep;
3759
3760 if (! mips_opts.at)
3761 return;
3762
df58fc94 3763 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
3764 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
3765 return;
3766
3767 ep.X_op = O_constant;
3768 ep.X_add_number = 0xcfff0000;
3769 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
3770 ep.X_add_number = 0xffff;
3771 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
3772 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
3773 }
3774}
3775
3776static void
3777fix_loongson2f (struct mips_cl_insn * ip)
3778{
3779 if (mips_fix_loongson2f_nop)
3780 fix_loongson2f_nop (ip);
3781
3782 if (mips_fix_loongson2f_jump)
3783 fix_loongson2f_jump (ip);
3784}
3785
a4e06468
RS
3786/* IP is a branch that has a delay slot, and we need to fill it
3787 automatically. Return true if we can do that by swapping IP
e407c74b
NC
3788 with the previous instruction.
3789 ADDRESS_EXPR is an operand of the instruction to be used with
3790 RELOC_TYPE. */
a4e06468
RS
3791
3792static bfd_boolean
e407c74b
NC
3793can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
3794 bfd_reloc_code_real_type *reloc_type)
a4e06468 3795{
2b0c8b40 3796 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468
RS
3797 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
3798
3799 /* -O2 and above is required for this optimization. */
3800 if (mips_optimize < 2)
3801 return FALSE;
3802
3803 /* If we have seen .set volatile or .set nomove, don't optimize. */
3804 if (mips_opts.nomove)
3805 return FALSE;
3806
3807 /* We can't swap if the previous instruction's position is fixed. */
3808 if (history[0].fixed_p)
3809 return FALSE;
3810
3811 /* If the previous previous insn was in a .set noreorder, we can't
3812 swap. Actually, the MIPS assembler will swap in this situation.
3813 However, gcc configured -with-gnu-as will generate code like
3814
3815 .set noreorder
3816 lw $4,XXX
3817 .set reorder
3818 INSN
3819 bne $4,$0,foo
3820
3821 in which we can not swap the bne and INSN. If gcc is not configured
3822 -with-gnu-as, it does not output the .set pseudo-ops. */
3823 if (history[1].noreorder_p)
3824 return FALSE;
3825
87333bb7
MR
3826 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
3827 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
3828 if (mips_opts.mips16 && history[0].fixp[0])
3829 return FALSE;
3830
3831 /* If the branch is itself the target of a branch, we can not swap.
3832 We cheat on this; all we check for is whether there is a label on
3833 this instruction. If there are any branches to anything other than
3834 a label, users must use .set noreorder. */
3835 if (seg_info (now_seg)->label_list)
3836 return FALSE;
3837
3838 /* If the previous instruction is in a variant frag other than this
2309ddf2 3839 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
3840 MIPS16 code, which uses variant frags for different purposes. */
3841 if (!mips_opts.mips16
a4e06468
RS
3842 && history[0].frag
3843 && history[0].frag->fr_type == rs_machine_dependent)
3844 return FALSE;
3845
bcd530a7
RS
3846 /* We do not swap with instructions that cannot architecturally
3847 be placed in a branch delay slot, such as SYNC or ERET. We
3848 also refrain from swapping with a trap instruction, since it
3849 complicates trap handlers to have the trap instruction be in
3850 a delay slot. */
a4e06468 3851 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 3852 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
3853 return FALSE;
3854
3855 /* Check for conflicts between the branch and the instructions
3856 before the candidate delay slot. */
3857 if (nops_for_insn (0, history + 1, ip) > 0)
3858 return FALSE;
3859
3860 /* Check for conflicts between the swapped sequence and the
3861 target of the branch. */
3862 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
3863 return FALSE;
3864
3865 /* If the branch reads a register that the previous
3866 instruction sets, we can not swap. */
3867 gpr_read = gpr_read_mask (ip);
3868 prev_gpr_write = gpr_write_mask (&history[0]);
3869 if (gpr_read & prev_gpr_write)
3870 return FALSE;
3871
3872 /* If the branch writes a register that the previous
3873 instruction sets, we can not swap. */
3874 gpr_write = gpr_write_mask (ip);
3875 if (gpr_write & prev_gpr_write)
3876 return FALSE;
3877
3878 /* If the branch writes a register that the previous
3879 instruction reads, we can not swap. */
3880 prev_gpr_read = gpr_read_mask (&history[0]);
3881 if (gpr_write & prev_gpr_read)
3882 return FALSE;
3883
3884 /* If one instruction sets a condition code and the
3885 other one uses a condition code, we can not swap. */
3886 pinfo = ip->insn_mo->pinfo;
3887 if ((pinfo & INSN_READ_COND_CODE)
3888 && (prev_pinfo & INSN_WRITE_COND_CODE))
3889 return FALSE;
3890 if ((pinfo & INSN_WRITE_COND_CODE)
3891 && (prev_pinfo & INSN_READ_COND_CODE))
3892 return FALSE;
3893
3894 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 3895 prev_pinfo2 = history[0].insn_mo->pinfo2;
a4e06468
RS
3896 if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
3897 return FALSE;
2b0c8b40
MR
3898 if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
3899 return FALSE;
a4e06468 3900
df58fc94
RS
3901 /* If the previous instruction has an incorrect size for a fixed
3902 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
3903 pinfo2 = ip->insn_mo->pinfo2;
3904 if (mips_opts.micromips
3905 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
3906 && insn_length (history) != 2)
3907 return FALSE;
3908 if (mips_opts.micromips
3909 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
3910 && insn_length (history) != 4)
3911 return FALSE;
3912
e407c74b
NC
3913 /* On R5900 short loops need to be fixed by inserting a nop in
3914 the branch delay slots.
3915 A short loop can be terminated too early. */
3916 if (mips_opts.arch == CPU_R5900
3917 /* Check if instruction has a parameter, ignore "j $31". */
3918 && (address_expr != NULL)
3919 /* Parameter must be 16 bit. */
3920 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
3921 /* Branch to same segment. */
3922 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
3923 /* Branch to same code fragment. */
3924 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
3925 /* Can only calculate branch offset if value is known. */
3926 && symbol_constant_p(address_expr->X_add_symbol)
3927 /* Check if branch is really conditional. */
3928 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
3929 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
3930 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
3931 {
3932 int distance;
3933 /* Check if loop is shorter than 6 instructions including
3934 branch and delay slot. */
3935 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
3936 if (distance <= 20)
3937 {
3938 int i;
3939 int rv;
3940
3941 rv = FALSE;
3942 /* When the loop includes branches or jumps,
3943 it is not a short loop. */
3944 for (i = 0; i < (distance / 4); i++)
3945 {
3946 if ((history[i].cleared_p)
3947 || delayed_branch_p(&history[i]))
3948 {
3949 rv = TRUE;
3950 break;
3951 }
3952 }
3953 if (rv == FALSE)
3954 {
3955 /* Insert nop after branch to fix short loop. */
3956 return FALSE;
3957 }
3958 }
3959 }
3960
a4e06468
RS
3961 return TRUE;
3962}
3963
e407c74b
NC
3964/* Decide how we should add IP to the instruction stream.
3965 ADDRESS_EXPR is an operand of the instruction to be used with
3966 RELOC_TYPE. */
a4e06468
RS
3967
3968static enum append_method
e407c74b
NC
3969get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
3970 bfd_reloc_code_real_type *reloc_type)
a4e06468
RS
3971{
3972 unsigned long pinfo;
3973
3974 /* The relaxed version of a macro sequence must be inherently
3975 hazard-free. */
3976 if (mips_relax.sequence == 2)
3977 return APPEND_ADD;
3978
3979 /* We must not dabble with instructions in a ".set norerorder" block. */
3980 if (mips_opts.noreorder)
3981 return APPEND_ADD;
3982
3983 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 3984 if (delayed_branch_p (ip))
a4e06468 3985 {
e407c74b
NC
3986 if (!branch_likely_p (ip)
3987 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
3988 return APPEND_SWAP;
3989
11625dd8 3990 pinfo = ip->insn_mo->pinfo;
a4e06468
RS
3991 if (mips_opts.mips16
3992 && ISA_SUPPORTS_MIPS16E
a4e06468
RS
3993 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
3994 return APPEND_ADD_COMPACT;
3995
3996 return APPEND_ADD_WITH_NOP;
3997 }
3998
a4e06468
RS
3999 return APPEND_ADD;
4000}
4001
ceb94aa5
RS
4002/* IP is a MIPS16 instruction whose opcode we have just changed.
4003 Point IP->insn_mo to the new opcode's definition. */
4004
4005static void
4006find_altered_mips16_opcode (struct mips_cl_insn *ip)
4007{
4008 const struct mips_opcode *mo, *end;
4009
4010 end = &mips16_opcodes[bfd_mips16_num_opcodes];
4011 for (mo = ip->insn_mo; mo < end; mo++)
4012 if ((ip->insn_opcode & mo->mask) == mo->match)
4013 {
4014 ip->insn_mo = mo;
4015 return;
4016 }
4017 abort ();
4018}
4019
df58fc94
RS
4020/* For microMIPS macros, we need to generate a local number label
4021 as the target of branches. */
4022#define MICROMIPS_LABEL_CHAR '\037'
4023static unsigned long micromips_target_label;
4024static char micromips_target_name[32];
4025
4026static char *
4027micromips_label_name (void)
4028{
4029 char *p = micromips_target_name;
4030 char symbol_name_temporary[24];
4031 unsigned long l;
4032 int i;
4033
4034 if (*p)
4035 return p;
4036
4037 i = 0;
4038 l = micromips_target_label;
4039#ifdef LOCAL_LABEL_PREFIX
4040 *p++ = LOCAL_LABEL_PREFIX;
4041#endif
4042 *p++ = 'L';
4043 *p++ = MICROMIPS_LABEL_CHAR;
4044 do
4045 {
4046 symbol_name_temporary[i++] = l % 10 + '0';
4047 l /= 10;
4048 }
4049 while (l != 0);
4050 while (i > 0)
4051 *p++ = symbol_name_temporary[--i];
4052 *p = '\0';
4053
4054 return micromips_target_name;
4055}
4056
4057static void
4058micromips_label_expr (expressionS *label_expr)
4059{
4060 label_expr->X_op = O_symbol;
4061 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
4062 label_expr->X_add_number = 0;
4063}
4064
4065static void
4066micromips_label_inc (void)
4067{
4068 micromips_target_label++;
4069 *micromips_target_name = '\0';
4070}
4071
4072static void
4073micromips_add_label (void)
4074{
4075 symbolS *s;
4076
4077 s = colon (micromips_label_name ());
4078 micromips_label_inc ();
4079#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
4080 if (IS_ELF)
4081 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
db9b2be4
AM
4082#else
4083 (void) s;
df58fc94
RS
4084#endif
4085}
4086
4087/* If assembling microMIPS code, then return the microMIPS reloc
4088 corresponding to the requested one if any. Otherwise return
4089 the reloc unchanged. */
4090
4091static bfd_reloc_code_real_type
4092micromips_map_reloc (bfd_reloc_code_real_type reloc)
4093{
4094 static const bfd_reloc_code_real_type relocs[][2] =
4095 {
4096 /* Keep sorted incrementally by the left-hand key. */
4097 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
4098 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
4099 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
4100 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
4101 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
4102 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
4103 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
4104 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
4105 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
4106 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
4107 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
4108 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
4109 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
4110 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
4111 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
4112 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
4113 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
4114 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
4115 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
4116 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
4117 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
4118 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
4119 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
4120 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
4121 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
4122 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
4123 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
4124 };
4125 bfd_reloc_code_real_type r;
4126 size_t i;
4127
4128 if (!mips_opts.micromips)
4129 return reloc;
4130 for (i = 0; i < ARRAY_SIZE (relocs); i++)
4131 {
4132 r = relocs[i][0];
4133 if (r > reloc)
4134 return reloc;
4135 if (r == reloc)
4136 return relocs[i][1];
4137 }
4138 return reloc;
4139}
4140
b886a2ab
RS
4141/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
4142 Return true on success, storing the resolved value in RESULT. */
4143
4144static bfd_boolean
4145calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
4146 offsetT *result)
4147{
4148 switch (reloc)
4149 {
4150 case BFD_RELOC_MIPS_HIGHEST:
4151 case BFD_RELOC_MICROMIPS_HIGHEST:
4152 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
4153 return TRUE;
4154
4155 case BFD_RELOC_MIPS_HIGHER:
4156 case BFD_RELOC_MICROMIPS_HIGHER:
4157 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
4158 return TRUE;
4159
4160 case BFD_RELOC_HI16_S:
4161 case BFD_RELOC_MICROMIPS_HI16_S:
4162 case BFD_RELOC_MIPS16_HI16_S:
4163 *result = ((operand + 0x8000) >> 16) & 0xffff;
4164 return TRUE;
4165
4166 case BFD_RELOC_HI16:
4167 case BFD_RELOC_MICROMIPS_HI16:
4168 case BFD_RELOC_MIPS16_HI16:
4169 *result = (operand >> 16) & 0xffff;
4170 return TRUE;
4171
4172 case BFD_RELOC_LO16:
4173 case BFD_RELOC_MICROMIPS_LO16:
4174 case BFD_RELOC_MIPS16_LO16:
4175 *result = operand & 0xffff;
4176 return TRUE;
4177
4178 case BFD_RELOC_UNUSED:
4179 *result = operand;
4180 return TRUE;
4181
4182 default:
4183 return FALSE;
4184 }
4185}
4186
71400594
RS
4187/* Output an instruction. IP is the instruction information.
4188 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
4189 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
4190 a macro expansion. */
71400594
RS
4191
4192static void
4193append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 4194 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 4195{
14fe068b 4196 unsigned long prev_pinfo2, pinfo;
71400594 4197 bfd_boolean relaxed_branch = FALSE;
a4e06468 4198 enum append_method method;
2309ddf2 4199 bfd_boolean relax32;
2b0c8b40 4200 int branch_disp;
71400594 4201
2309ddf2 4202 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
4203 fix_loongson2f (ip);
4204
738f4d98 4205 file_ase_mips16 |= mips_opts.mips16;
df58fc94 4206 file_ase_micromips |= mips_opts.micromips;
738f4d98 4207
df58fc94 4208 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 4209 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
4210
4211 if (mips_opts.micromips
4212 && !expansionp
4213 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4214 && micromips_insn_length (ip->insn_mo) != 2)
4215 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4216 && micromips_insn_length (ip->insn_mo) != 4)))
4217 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4218 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 4219
15be625d
CM
4220 if (address_expr == NULL)
4221 ip->complete_p = 1;
b886a2ab
RS
4222 else if (reloc_type[0] <= BFD_RELOC_UNUSED
4223 && reloc_type[1] == BFD_RELOC_UNUSED
4224 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
4225 && address_expr->X_op == O_constant)
4226 {
15be625d
CM
4227 switch (*reloc_type)
4228 {
15be625d 4229 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
4230 {
4231 int shift;
4232
4233 shift = mips_opts.micromips ? 1 : 2;
4234 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4235 as_bad (_("jump to misaligned address (0x%lx)"),
4236 (unsigned long) address_expr->X_add_number);
4237 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4238 & 0x3ffffff);
335574df 4239 ip->complete_p = 1;
df58fc94 4240 }
15be625d
CM
4241 break;
4242
4243 case BFD_RELOC_MIPS16_JMP:
4244 if ((address_expr->X_add_number & 3) != 0)
4245 as_bad (_("jump to misaligned address (0x%lx)"),
4246 (unsigned long) address_expr->X_add_number);
4247 ip->insn_opcode |=
4248 (((address_expr->X_add_number & 0x7c0000) << 3)
4249 | ((address_expr->X_add_number & 0xf800000) >> 7)
4250 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 4251 ip->complete_p = 1;
15be625d
CM
4252 break;
4253
4254 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
4255 {
4256 int shift;
4257
4258 shift = mips_opts.micromips ? 1 : 2;
4259 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4260 as_bad (_("branch to misaligned address (0x%lx)"),
4261 (unsigned long) address_expr->X_add_number);
4262 if (!mips_relax_branch)
4263 {
4264 if ((address_expr->X_add_number + (1 << (shift + 15)))
4265 & ~((1 << (shift + 16)) - 1))
4266 as_bad (_("branch address range overflow (0x%lx)"),
4267 (unsigned long) address_expr->X_add_number);
4268 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4269 & 0xffff);
4270 }
df58fc94 4271 }
15be625d
CM
4272 break;
4273
4274 default:
b886a2ab
RS
4275 {
4276 offsetT value;
4277
4278 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
4279 &value))
4280 {
4281 ip->insn_opcode |= value & 0xffff;
4282 ip->complete_p = 1;
4283 }
4284 }
4285 break;
4286 }
15be625d
CM
4287 }
4288
71400594
RS
4289 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4290 {
4291 /* There are a lot of optimizations we could do that we don't.
4292 In particular, we do not, in general, reorder instructions.
4293 If you use gcc with optimization, it will reorder
4294 instructions and generally do much more optimization then we
4295 do here; repeating all that work in the assembler would only
4296 benefit hand written assembly code, and does not seem worth
4297 it. */
4298 int nops = (mips_optimize == 0
932d1a1b
RS
4299 ? nops_for_insn (0, history, NULL)
4300 : nops_for_insn_or_target (0, history, ip));
71400594 4301 if (nops > 0)
252b5132
RH
4302 {
4303 fragS *old_frag;
4304 unsigned long old_frag_offset;
4305 int i;
252b5132
RH
4306
4307 old_frag = frag_now;
4308 old_frag_offset = frag_now_fix ();
4309
4310 for (i = 0; i < nops; i++)
14fe068b
RS
4311 add_fixed_insn (NOP_INSN);
4312 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
4313
4314 if (listing)
4315 {
4316 listing_prev_line ();
4317 /* We may be at the start of a variant frag. In case we
4318 are, make sure there is enough space for the frag
4319 after the frags created by listing_prev_line. The
4320 argument to frag_grow here must be at least as large
4321 as the argument to all other calls to frag_grow in
4322 this file. We don't have to worry about being in the
4323 middle of a variant frag, because the variants insert
4324 all needed nop instructions themselves. */
4325 frag_grow (40);
4326 }
4327
462427c4 4328 mips_move_text_labels ();
252b5132
RH
4329
4330#ifndef NO_ECOFF_DEBUGGING
4331 if (ECOFF_DEBUGGING)
4332 ecoff_fix_loc (old_frag, old_frag_offset);
4333#endif
4334 }
71400594
RS
4335 }
4336 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4337 {
932d1a1b
RS
4338 int nops;
4339
4340 /* Work out how many nops in prev_nop_frag are needed by IP,
4341 ignoring hazards generated by the first prev_nop_frag_since
4342 instructions. */
4343 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 4344 gas_assert (nops <= prev_nop_frag_holds);
252b5132 4345
71400594
RS
4346 /* Enforce NOPS as a minimum. */
4347 if (nops > prev_nop_frag_required)
4348 prev_nop_frag_required = nops;
252b5132 4349
71400594
RS
4350 if (prev_nop_frag_holds == prev_nop_frag_required)
4351 {
4352 /* Settle for the current number of nops. Update the history
4353 accordingly (for the benefit of any future .set reorder code). */
4354 prev_nop_frag = NULL;
4355 insert_into_history (prev_nop_frag_since,
4356 prev_nop_frag_holds, NOP_INSN);
4357 }
4358 else
4359 {
4360 /* Allow this instruction to replace one of the nops that was
4361 tentatively added to prev_nop_frag. */
df58fc94 4362 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
4363 prev_nop_frag_holds--;
4364 prev_nop_frag_since++;
252b5132
RH
4365 }
4366 }
4367
e407c74b 4368 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 4369 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 4370
58e2ea4d
MR
4371#ifdef OBJ_ELF
4372 /* The value passed to dwarf2_emit_insn is the distance between
4373 the beginning of the current instruction and the address that
e3a82c8e
MR
4374 should be recorded in the debug tables. This is normally the
4375 current address.
4376
df58fc94
RS
4377 For MIPS16/microMIPS debug info we want to use ISA-encoded
4378 addresses, so we use -1 for an address higher by one than the
4379 current one.
e3a82c8e
MR
4380
4381 If the instruction produced is a branch that we will swap with
4382 the preceding instruction, then we add the displacement by which
4383 the branch will be moved backwards. This is more appropriate
2309ddf2
MR
4384 and for MIPS16/microMIPS code also prevents a debugger from
4385 placing a breakpoint in the middle of the branch (and corrupting
4386 code if software breakpoints are used). */
2b0c8b40 4387 dwarf2_emit_insn ((HAVE_CODE_COMPRESSION ? -1 : 0) + branch_disp);
58e2ea4d
MR
4388#endif
4389
df58fc94
RS
4390 relax32 = (mips_relax_branch
4391 /* Don't try branch relaxation within .set nomacro, or within
4392 .set noat if we use $at for PIC computations. If it turns
4393 out that the branch was out-of-range, we'll get an error. */
4394 && !mips_opts.warn_about_macros
4395 && (mips_opts.at || mips_pic == NO_PIC)
4396 /* Don't relax BPOSGE32/64 as they have no complementing
4397 branches. */
40209cad 4398 && !(ip->insn_mo->membership & (INSN_DSP64 | INSN_DSP)));
df58fc94
RS
4399
4400 if (!HAVE_CODE_COMPRESSION
4401 && address_expr
4402 && relax32
0b25d3e6 4403 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 4404 && delayed_branch_p (ip))
4a6a3df4 4405 {
895921c9 4406 relaxed_branch = TRUE;
1e915849
RS
4407 add_relaxed_insn (ip, (relaxed_branch_length
4408 (NULL, NULL,
11625dd8
RS
4409 uncond_branch_p (ip) ? -1
4410 : branch_likely_p (ip) ? 1
1e915849
RS
4411 : 0)), 4,
4412 RELAX_BRANCH_ENCODE
66b3e8da 4413 (AT,
11625dd8
RS
4414 uncond_branch_p (ip),
4415 branch_likely_p (ip),
1e915849
RS
4416 pinfo & INSN_WRITE_GPR_31,
4417 0),
4418 address_expr->X_add_symbol,
4419 address_expr->X_add_number);
4a6a3df4
AO
4420 *reloc_type = BFD_RELOC_UNUSED;
4421 }
df58fc94
RS
4422 else if (mips_opts.micromips
4423 && address_expr
4424 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4425 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
4426 && (delayed_branch_p (ip) || compact_branch_p (ip))
4427 /* Don't try branch relaxation when users specify
4428 16-bit/32-bit instructions. */
4429 && !forced_insn_length)
df58fc94
RS
4430 {
4431 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4432 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
4433 int uncond = uncond_branch_p (ip) ? -1 : 0;
4434 int compact = compact_branch_p (ip);
df58fc94
RS
4435 int al = pinfo & INSN_WRITE_GPR_31;
4436 int length32;
4437
4438 gas_assert (address_expr != NULL);
4439 gas_assert (!mips_relax.sequence);
4440
2b0c8b40 4441 relaxed_branch = TRUE;
df58fc94
RS
4442 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4443 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
4444 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4445 relax32, 0, 0),
df58fc94
RS
4446 address_expr->X_add_symbol,
4447 address_expr->X_add_number);
4448 *reloc_type = BFD_RELOC_UNUSED;
4449 }
4450 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
4451 {
4452 /* We need to set up a variant frag. */
df58fc94 4453 gas_assert (address_expr != NULL);
1e915849
RS
4454 add_relaxed_insn (ip, 4, 0,
4455 RELAX_MIPS16_ENCODE
4456 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 4457 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 4458 delayed_branch_p (&history[0]),
1e915849
RS
4459 history[0].mips16_absolute_jump_p),
4460 make_expr_symbol (address_expr), 0);
252b5132 4461 }
5c04167a 4462 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 4463 {
11625dd8 4464 if (!delayed_branch_p (ip))
b8ee1a6e
DU
4465 /* Make sure there is enough room to swap this instruction with
4466 a following jump instruction. */
4467 frag_grow (6);
1e915849 4468 add_fixed_insn (ip);
252b5132
RH
4469 }
4470 else
4471 {
4472 if (mips_opts.mips16
4473 && mips_opts.noreorder
11625dd8 4474 && delayed_branch_p (&history[0]))
252b5132
RH
4475 as_warn (_("extended instruction in delay slot"));
4476
4d7206a2
RS
4477 if (mips_relax.sequence)
4478 {
4479 /* If we've reached the end of this frag, turn it into a variant
4480 frag and record the information for the instructions we've
4481 written so far. */
4482 if (frag_room () < 4)
4483 relax_close_frag ();
df58fc94 4484 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
4485 }
4486
584892a6 4487 if (mips_relax.sequence != 2)
df58fc94
RS
4488 {
4489 if (mips_macro_warning.first_insn_sizes[0] == 0)
4490 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4491 mips_macro_warning.sizes[0] += insn_length (ip);
4492 mips_macro_warning.insns[0]++;
4493 }
584892a6 4494 if (mips_relax.sequence != 1)
df58fc94
RS
4495 {
4496 if (mips_macro_warning.first_insn_sizes[1] == 0)
4497 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4498 mips_macro_warning.sizes[1] += insn_length (ip);
4499 mips_macro_warning.insns[1]++;
4500 }
584892a6 4501
1e915849
RS
4502 if (mips_opts.mips16)
4503 {
4504 ip->fixed_p = 1;
4505 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4506 }
4507 add_fixed_insn (ip);
252b5132
RH
4508 }
4509
9fe77896 4510 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 4511 {
df58fc94 4512 bfd_reloc_code_real_type final_type[3];
2309ddf2 4513 reloc_howto_type *howto0;
9fe77896
RS
4514 reloc_howto_type *howto;
4515 int i;
34ce925e 4516
df58fc94
RS
4517 /* Perform any necessary conversion to microMIPS relocations
4518 and find out how many relocations there actually are. */
4519 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4520 final_type[i] = micromips_map_reloc (reloc_type[i]);
4521
9fe77896
RS
4522 /* In a compound relocation, it is the final (outermost)
4523 operator that determines the relocated field. */
2309ddf2
MR
4524 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
4525
9fe77896
RS
4526 if (howto == NULL)
4527 {
4528 /* To reproduce this failure try assembling gas/testsuites/
4529 gas/mips/mips16-intermix.s with a mips-ecoff targeted
4530 assembler. */
df58fc94
RS
4531 as_bad (_("Unsupported MIPS relocation number %d"),
4532 final_type[i - 1]);
9fe77896
RS
4533 howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
4534 }
2309ddf2
MR
4535
4536 if (i > 1)
4537 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
4538 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4539 bfd_get_reloc_size (howto),
4540 address_expr,
2309ddf2
MR
4541 howto0 && howto0->pc_relative,
4542 final_type[0]);
9fe77896
RS
4543
4544 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 4545 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
4546 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4547
4548 /* These relocations can have an addend that won't fit in
4549 4 octets for 64bit assembly. */
4550 if (HAVE_64BIT_GPRS
4551 && ! howto->partial_inplace
4552 && (reloc_type[0] == BFD_RELOC_16
4553 || reloc_type[0] == BFD_RELOC_32
4554 || reloc_type[0] == BFD_RELOC_MIPS_JMP
4555 || reloc_type[0] == BFD_RELOC_GPREL16
4556 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4557 || reloc_type[0] == BFD_RELOC_GPREL32
4558 || reloc_type[0] == BFD_RELOC_64
4559 || reloc_type[0] == BFD_RELOC_CTOR
4560 || reloc_type[0] == BFD_RELOC_MIPS_SUB
4561 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4562 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4563 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4564 || reloc_type[0] == BFD_RELOC_MIPS_REL16
4565 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4566 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4567 || hi16_reloc_p (reloc_type[0])
4568 || lo16_reloc_p (reloc_type[0])))
4569 ip->fixp[0]->fx_no_overflow = 1;
4570
ddaf2c41
MR
4571 /* These relocations can have an addend that won't fit in 2 octets. */
4572 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
4573 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
4574 ip->fixp[0]->fx_no_overflow = 1;
4575
9fe77896
RS
4576 if (mips_relax.sequence)
4577 {
4578 if (mips_relax.first_fixup == 0)
4579 mips_relax.first_fixup = ip->fixp[0];
4580 }
4581 else if (reloc_needs_lo_p (*reloc_type))
4582 {
4583 struct mips_hi_fixup *hi_fixup;
4584
4585 /* Reuse the last entry if it already has a matching %lo. */
4586 hi_fixup = mips_hi_fixup_list;
4587 if (hi_fixup == 0
4588 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 4589 {
9fe77896
RS
4590 hi_fixup = ((struct mips_hi_fixup *)
4591 xmalloc (sizeof (struct mips_hi_fixup)));
4592 hi_fixup->next = mips_hi_fixup_list;
4593 mips_hi_fixup_list = hi_fixup;
4d7206a2 4594 }
9fe77896
RS
4595 hi_fixup->fixp = ip->fixp[0];
4596 hi_fixup->seg = now_seg;
4597 }
252b5132 4598
9fe77896
RS
4599 /* Add fixups for the second and third relocations, if given.
4600 Note that the ABI allows the second relocation to be
4601 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
4602 moment we only use RSS_UNDEF, but we could add support
4603 for the others if it ever becomes necessary. */
4604 for (i = 1; i < 3; i++)
4605 if (reloc_type[i] != BFD_RELOC_UNUSED)
4606 {
4607 ip->fixp[i] = fix_new (ip->frag, ip->where,
4608 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 4609 FALSE, final_type[i]);
f6688943 4610
9fe77896
RS
4611 /* Use fx_tcbit to mark compound relocs. */
4612 ip->fixp[0]->fx_tcbit = 1;
4613 ip->fixp[i]->fx_tcbit = 1;
4614 }
252b5132 4615 }
1e915849 4616 install_insn (ip);
252b5132
RH
4617
4618 /* Update the register mask information. */
4c260379
RS
4619 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4620 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 4621
a4e06468 4622 switch (method)
252b5132 4623 {
a4e06468
RS
4624 case APPEND_ADD:
4625 insert_into_history (0, 1, ip);
4626 break;
4627
4628 case APPEND_ADD_WITH_NOP:
14fe068b
RS
4629 {
4630 struct mips_cl_insn *nop;
4631
4632 insert_into_history (0, 1, ip);
4633 nop = get_delay_slot_nop (ip);
4634 add_fixed_insn (nop);
4635 insert_into_history (0, 1, nop);
4636 if (mips_relax.sequence)
4637 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4638 }
a4e06468
RS
4639 break;
4640
4641 case APPEND_ADD_COMPACT:
4642 /* Convert MIPS16 jr/jalr into a "compact" jump. */
4643 gas_assert (mips_opts.mips16);
4644 ip->insn_opcode |= 0x0080;
4645 find_altered_mips16_opcode (ip);
4646 install_insn (ip);
4647 insert_into_history (0, 1, ip);
4648 break;
4649
4650 case APPEND_SWAP:
4651 {
4652 struct mips_cl_insn delay = history[0];
4653 if (mips_opts.mips16)
4654 {
4655 know (delay.frag == ip->frag);
4656 move_insn (ip, delay.frag, delay.where);
4657 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4658 }
464ab0e5 4659 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
4660 {
4661 /* Add the delay slot instruction to the end of the
4662 current frag and shrink the fixed part of the
4663 original frag. If the branch occupies the tail of
4664 the latter, move it backwards to cover the gap. */
2b0c8b40 4665 delay.frag->fr_fix -= branch_disp;
a4e06468 4666 if (delay.frag == ip->frag)
2b0c8b40 4667 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
4668 add_fixed_insn (&delay);
4669 }
4670 else
4671 {
2b0c8b40
MR
4672 move_insn (&delay, ip->frag,
4673 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
4674 move_insn (ip, history[0].frag, history[0].where);
4675 }
4676 history[0] = *ip;
4677 delay.fixed_p = 1;
4678 insert_into_history (0, 1, &delay);
4679 }
4680 break;
252b5132
RH
4681 }
4682
13408f1e 4683 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
4684 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4685 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
4686 {
4687 unsigned int i;
4688
79850f26 4689 mips_no_prev_insn ();
13408f1e 4690
e407c74b 4691 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 4692 history[i].cleared_p = 1;
e407c74b
NC
4693 }
4694
df58fc94
RS
4695 /* We need to emit a label at the end of branch-likely macros. */
4696 if (emit_branch_likely_macro)
4697 {
4698 emit_branch_likely_macro = FALSE;
4699 micromips_add_label ();
4700 }
4701
252b5132
RH
4702 /* We just output an insn, so the next one doesn't have a label. */
4703 mips_clear_insn_labels ();
252b5132
RH
4704}
4705
e407c74b
NC
4706/* Forget that there was any previous instruction or label.
4707 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
4708
4709static void
7d10b47d 4710mips_no_prev_insn (void)
252b5132 4711{
7d10b47d
RS
4712 prev_nop_frag = NULL;
4713 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
4714 mips_clear_insn_labels ();
4715}
4716
7d10b47d
RS
4717/* This function must be called before we emit something other than
4718 instructions. It is like mips_no_prev_insn except that it inserts
4719 any NOPS that might be needed by previous instructions. */
252b5132 4720
7d10b47d
RS
4721void
4722mips_emit_delays (void)
252b5132
RH
4723{
4724 if (! mips_opts.noreorder)
4725 {
932d1a1b 4726 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
4727 if (nops > 0)
4728 {
7d10b47d
RS
4729 while (nops-- > 0)
4730 add_fixed_insn (NOP_INSN);
462427c4 4731 mips_move_text_labels ();
7d10b47d
RS
4732 }
4733 }
4734 mips_no_prev_insn ();
4735}
4736
4737/* Start a (possibly nested) noreorder block. */
4738
4739static void
4740start_noreorder (void)
4741{
4742 if (mips_opts.noreorder == 0)
4743 {
4744 unsigned int i;
4745 int nops;
4746
4747 /* None of the instructions before the .set noreorder can be moved. */
4748 for (i = 0; i < ARRAY_SIZE (history); i++)
4749 history[i].fixed_p = 1;
4750
4751 /* Insert any nops that might be needed between the .set noreorder
4752 block and the previous instructions. We will later remove any
4753 nops that turn out not to be needed. */
932d1a1b 4754 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
4755 if (nops > 0)
4756 {
4757 if (mips_optimize != 0)
252b5132
RH
4758 {
4759 /* Record the frag which holds the nop instructions, so
4760 that we can remove them if we don't need them. */
df58fc94 4761 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
4762 prev_nop_frag = frag_now;
4763 prev_nop_frag_holds = nops;
4764 prev_nop_frag_required = 0;
4765 prev_nop_frag_since = 0;
4766 }
4767
4768 for (; nops > 0; --nops)
1e915849 4769 add_fixed_insn (NOP_INSN);
252b5132 4770
7d10b47d
RS
4771 /* Move on to a new frag, so that it is safe to simply
4772 decrease the size of prev_nop_frag. */
4773 frag_wane (frag_now);
4774 frag_new (0);
462427c4 4775 mips_move_text_labels ();
252b5132 4776 }
df58fc94 4777 mips_mark_labels ();
7d10b47d 4778 mips_clear_insn_labels ();
252b5132 4779 }
7d10b47d
RS
4780 mips_opts.noreorder++;
4781 mips_any_noreorder = 1;
4782}
252b5132 4783
7d10b47d 4784/* End a nested noreorder block. */
252b5132 4785
7d10b47d
RS
4786static void
4787end_noreorder (void)
4788{
4789 mips_opts.noreorder--;
4790 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
4791 {
4792 /* Commit to inserting prev_nop_frag_required nops and go back to
4793 handling nop insertion the .set reorder way. */
4794 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 4795 * NOP_INSN_SIZE);
7d10b47d
RS
4796 insert_into_history (prev_nop_frag_since,
4797 prev_nop_frag_required, NOP_INSN);
4798 prev_nop_frag = NULL;
4799 }
252b5132
RH
4800}
4801
584892a6
RS
4802/* Set up global variables for the start of a new macro. */
4803
4804static void
4805macro_start (void)
4806{
4807 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
4808 memset (&mips_macro_warning.first_insn_sizes, 0,
4809 sizeof (mips_macro_warning.first_insn_sizes));
4810 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 4811 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 4812 && delayed_branch_p (&history[0]));
df58fc94
RS
4813 switch (history[0].insn_mo->pinfo2
4814 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
4815 {
4816 case INSN2_BRANCH_DELAY_32BIT:
4817 mips_macro_warning.delay_slot_length = 4;
4818 break;
4819 case INSN2_BRANCH_DELAY_16BIT:
4820 mips_macro_warning.delay_slot_length = 2;
4821 break;
4822 default:
4823 mips_macro_warning.delay_slot_length = 0;
4824 break;
4825 }
4826 mips_macro_warning.first_frag = NULL;
584892a6
RS
4827}
4828
df58fc94
RS
4829/* Given that a macro is longer than one instruction or of the wrong size,
4830 return the appropriate warning for it. Return null if no warning is
4831 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
4832 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
4833 and RELAX_NOMACRO. */
584892a6
RS
4834
4835static const char *
4836macro_warning (relax_substateT subtype)
4837{
4838 if (subtype & RELAX_DELAY_SLOT)
4839 return _("Macro instruction expanded into multiple instructions"
4840 " in a branch delay slot");
4841 else if (subtype & RELAX_NOMACRO)
4842 return _("Macro instruction expanded into multiple instructions");
df58fc94
RS
4843 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
4844 | RELAX_DELAY_SLOT_SIZE_SECOND))
4845 return ((subtype & RELAX_DELAY_SLOT_16BIT)
4846 ? _("Macro instruction expanded into a wrong size instruction"
4847 " in a 16-bit branch delay slot")
4848 : _("Macro instruction expanded into a wrong size instruction"
4849 " in a 32-bit branch delay slot"));
584892a6
RS
4850 else
4851 return 0;
4852}
4853
4854/* Finish up a macro. Emit warnings as appropriate. */
4855
4856static void
4857macro_end (void)
4858{
df58fc94
RS
4859 /* Relaxation warning flags. */
4860 relax_substateT subtype = 0;
4861
4862 /* Check delay slot size requirements. */
4863 if (mips_macro_warning.delay_slot_length == 2)
4864 subtype |= RELAX_DELAY_SLOT_16BIT;
4865 if (mips_macro_warning.delay_slot_length != 0)
584892a6 4866 {
df58fc94
RS
4867 if (mips_macro_warning.delay_slot_length
4868 != mips_macro_warning.first_insn_sizes[0])
4869 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
4870 if (mips_macro_warning.delay_slot_length
4871 != mips_macro_warning.first_insn_sizes[1])
4872 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
4873 }
584892a6 4874
df58fc94
RS
4875 /* Check instruction count requirements. */
4876 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
4877 {
4878 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
4879 subtype |= RELAX_SECOND_LONGER;
4880 if (mips_opts.warn_about_macros)
4881 subtype |= RELAX_NOMACRO;
4882 if (mips_macro_warning.delay_slot_p)
4883 subtype |= RELAX_DELAY_SLOT;
df58fc94 4884 }
584892a6 4885
df58fc94
RS
4886 /* If both alternatives fail to fill a delay slot correctly,
4887 emit the warning now. */
4888 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
4889 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
4890 {
4891 relax_substateT s;
4892 const char *msg;
4893
4894 s = subtype & (RELAX_DELAY_SLOT_16BIT
4895 | RELAX_DELAY_SLOT_SIZE_FIRST
4896 | RELAX_DELAY_SLOT_SIZE_SECOND);
4897 msg = macro_warning (s);
4898 if (msg != NULL)
4899 as_warn ("%s", msg);
4900 subtype &= ~s;
4901 }
4902
4903 /* If both implementations are longer than 1 instruction, then emit the
4904 warning now. */
4905 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
4906 {
4907 relax_substateT s;
4908 const char *msg;
4909
4910 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
4911 msg = macro_warning (s);
4912 if (msg != NULL)
4913 as_warn ("%s", msg);
4914 subtype &= ~s;
584892a6 4915 }
df58fc94
RS
4916
4917 /* If any flags still set, then one implementation might need a warning
4918 and the other either will need one of a different kind or none at all.
4919 Pass any remaining flags over to relaxation. */
4920 if (mips_macro_warning.first_frag != NULL)
4921 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
4922}
4923
df58fc94
RS
4924/* Instruction operand formats used in macros that vary between
4925 standard MIPS and microMIPS code. */
4926
4927static const char * const brk_fmt[2] = { "c", "mF" };
4928static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
4929static const char * const jalr_fmt[2] = { "d,s", "t,s" };
4930static const char * const lui_fmt[2] = { "t,u", "s,u" };
4931static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
4932static const char * const mfhl_fmt[2] = { "d", "mj" };
4933static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
4934static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
4935
4936#define BRK_FMT (brk_fmt[mips_opts.micromips])
4937#define COP12_FMT (cop12_fmt[mips_opts.micromips])
4938#define JALR_FMT (jalr_fmt[mips_opts.micromips])
4939#define LUI_FMT (lui_fmt[mips_opts.micromips])
4940#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
4941#define MFHL_FMT (mfhl_fmt[mips_opts.micromips])
4942#define SHFT_FMT (shft_fmt[mips_opts.micromips])
4943#define TRAP_FMT (trap_fmt[mips_opts.micromips])
4944
6e1304d8
RS
4945/* Read a macro's relocation codes from *ARGS and store them in *R.
4946 The first argument in *ARGS will be either the code for a single
4947 relocation or -1 followed by the three codes that make up a
4948 composite relocation. */
4949
4950static void
4951macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
4952{
4953 int i, next;
4954
4955 next = va_arg (*args, int);
4956 if (next >= 0)
4957 r[0] = (bfd_reloc_code_real_type) next;
4958 else
4959 for (i = 0; i < 3; i++)
4960 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
4961}
4962
252b5132
RH
4963/* Build an instruction created by a macro expansion. This is passed
4964 a pointer to the count of instructions created so far, an
4965 expression, the name of the instruction to build, an operand format
4966 string, and corresponding arguments. */
4967
252b5132 4968static void
67c0d1eb 4969macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 4970{
df58fc94 4971 const struct mips_opcode *mo = NULL;
f6688943 4972 bfd_reloc_code_real_type r[3];
df58fc94
RS
4973 const struct mips_opcode *amo;
4974 struct hash_control *hash;
4975 struct mips_cl_insn insn;
252b5132 4976 va_list args;
252b5132 4977
252b5132 4978 va_start (args, fmt);
252b5132 4979
252b5132
RH
4980 if (mips_opts.mips16)
4981 {
03ea81db 4982 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
4983 va_end (args);
4984 return;
4985 }
4986
f6688943
TS
4987 r[0] = BFD_RELOC_UNUSED;
4988 r[1] = BFD_RELOC_UNUSED;
4989 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
4990 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
4991 amo = (struct mips_opcode *) hash_find (hash, name);
4992 gas_assert (amo);
4993 gas_assert (strcmp (name, amo->name) == 0);
1e915849 4994
df58fc94 4995 do
8b082fb1
TS
4996 {
4997 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
4998 macros will never generate MDMX, MIPS-3D, or MT instructions.
4999 We try to match an instruction that fulfils the branch delay
5000 slot instruction length requirement (if any) of the previous
5001 instruction. While doing this we record the first instruction
5002 seen that matches all the other conditions and use it anyway
5003 if the requirement cannot be met; we will issue an appropriate
5004 warning later on. */
5005 if (strcmp (fmt, amo->args) == 0
5006 && amo->pinfo != INSN_MACRO
5007 && is_opcode_valid (amo)
5008 && is_size_valid (amo))
5009 {
5010 if (is_delay_slot_valid (amo))
5011 {
5012 mo = amo;
5013 break;
5014 }
5015 else if (!mo)
5016 mo = amo;
5017 }
8b082fb1 5018
df58fc94
RS
5019 ++amo;
5020 gas_assert (amo->name);
252b5132 5021 }
df58fc94 5022 while (strcmp (name, amo->name) == 0);
252b5132 5023
df58fc94 5024 gas_assert (mo);
1e915849 5025 create_insn (&insn, mo);
252b5132
RH
5026 for (;;)
5027 {
5028 switch (*fmt++)
5029 {
5030 case '\0':
5031 break;
5032
5033 case ',':
5034 case '(':
5035 case ')':
5036 continue;
5037
5f74bc13
CD
5038 case '+':
5039 switch (*fmt++)
5040 {
5041 case 'A':
5042 case 'E':
df58fc94
RS
5043 INSERT_OPERAND (mips_opts.micromips,
5044 EXTLSB, insn, va_arg (args, int));
5f74bc13
CD
5045 continue;
5046
5047 case 'B':
5048 case 'F':
5049 /* Note that in the macro case, these arguments are already
5050 in MSB form. (When handling the instruction in the
5051 non-macro case, these arguments are sizes from which
5052 MSB values must be calculated.) */
df58fc94
RS
5053 INSERT_OPERAND (mips_opts.micromips,
5054 INSMSB, insn, va_arg (args, int));
5f74bc13
CD
5055 continue;
5056
b015e599
AP
5057 case 'J':
5058 gas_assert (!mips_opts.micromips);
5059 INSERT_OPERAND (0, CODE10, insn, va_arg (args, int));
5060 continue;
5061
5f74bc13
CD
5062 case 'C':
5063 case 'G':
5064 case 'H':
5065 /* Note that in the macro case, these arguments are already
5066 in MSBD form. (When handling the instruction in the
5067 non-macro case, these arguments are sizes from which
5068 MSBD values must be calculated.) */
df58fc94
RS
5069 INSERT_OPERAND (mips_opts.micromips,
5070 EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
5071 continue;
5072
dd3cbb7e 5073 case 'Q':
df58fc94
RS
5074 gas_assert (!mips_opts.micromips);
5075 INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
dd3cbb7e
NC
5076 continue;
5077
5f74bc13 5078 default:
b37df7c4 5079 abort ();
5f74bc13
CD
5080 }
5081 continue;
5082
8b082fb1 5083 case '2':
03f66e8a 5084 INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
8b082fb1
TS
5085 continue;
5086
df58fc94
RS
5087 case 'n':
5088 gas_assert (mips_opts.micromips);
252b5132
RH
5089 case 't':
5090 case 'w':
5091 case 'E':
df58fc94 5092 INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
252b5132
RH
5093 continue;
5094
5095 case 'c':
df58fc94
RS
5096 gas_assert (!mips_opts.micromips);
5097 INSERT_OPERAND (0, CODE, insn, va_arg (args, int));
38487616
TS
5098 continue;
5099
252b5132 5100 case 'W':
df58fc94
RS
5101 gas_assert (!mips_opts.micromips);
5102 case 'T':
5103 INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
252b5132
RH
5104 continue;
5105
252b5132 5106 case 'G':
df58fc94
RS
5107 if (mips_opts.micromips)
5108 INSERT_OPERAND (1, RS, insn, va_arg (args, int));
5109 else
5110 INSERT_OPERAND (0, RD, insn, va_arg (args, int));
5111 continue;
5112
af7ee8bf 5113 case 'K':
df58fc94
RS
5114 gas_assert (!mips_opts.micromips);
5115 case 'd':
5116 INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
252b5132
RH
5117 continue;
5118
4372b673 5119 case 'U':
df58fc94 5120 gas_assert (!mips_opts.micromips);
4372b673
NC
5121 {
5122 int tmp = va_arg (args, int);
5123
df58fc94
RS
5124 INSERT_OPERAND (0, RT, insn, tmp);
5125 INSERT_OPERAND (0, RD, insn, tmp);
4372b673 5126 }
df58fc94 5127 continue;
4372b673 5128
252b5132
RH
5129 case 'V':
5130 case 'S':
df58fc94
RS
5131 gas_assert (!mips_opts.micromips);
5132 INSERT_OPERAND (0, FS, insn, va_arg (args, int));
252b5132
RH
5133 continue;
5134
5135 case 'z':
5136 continue;
5137
5138 case '<':
df58fc94
RS
5139 INSERT_OPERAND (mips_opts.micromips,
5140 SHAMT, insn, va_arg (args, int));
252b5132
RH
5141 continue;
5142
5143 case 'D':
df58fc94
RS
5144 gas_assert (!mips_opts.micromips);
5145 INSERT_OPERAND (0, FD, insn, va_arg (args, int));
252b5132
RH
5146 continue;
5147
5148 case 'B':
df58fc94
RS
5149 gas_assert (!mips_opts.micromips);
5150 INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
252b5132
RH
5151 continue;
5152
4372b673 5153 case 'J':
df58fc94
RS
5154 gas_assert (!mips_opts.micromips);
5155 INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
4372b673
NC
5156 continue;
5157
252b5132 5158 case 'q':
df58fc94
RS
5159 gas_assert (!mips_opts.micromips);
5160 INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
252b5132
RH
5161 continue;
5162
5163 case 'b':
5164 case 's':
5165 case 'r':
5166 case 'v':
df58fc94 5167 INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
252b5132
RH
5168 continue;
5169
5170 case 'i':
5171 case 'j':
6e1304d8 5172 macro_read_relocs (&args, r);
9c2799c2 5173 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
5174 || *r == BFD_RELOC_MIPS_HIGHER
5175 || *r == BFD_RELOC_HI16_S
5176 || *r == BFD_RELOC_LO16
5177 || *r == BFD_RELOC_MIPS_GOT_OFST);
5178 continue;
5179
5180 case 'o':
5181 macro_read_relocs (&args, r);
252b5132
RH
5182 continue;
5183
5184 case 'u':
6e1304d8 5185 macro_read_relocs (&args, r);
9c2799c2 5186 gas_assert (ep != NULL
90ecf173
MR
5187 && (ep->X_op == O_constant
5188 || (ep->X_op == O_symbol
5189 && (*r == BFD_RELOC_MIPS_HIGHEST
5190 || *r == BFD_RELOC_HI16_S
5191 || *r == BFD_RELOC_HI16
5192 || *r == BFD_RELOC_GPREL16
5193 || *r == BFD_RELOC_MIPS_GOT_HI16
5194 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
5195 continue;
5196
5197 case 'p':
9c2799c2 5198 gas_assert (ep != NULL);
bad36eac 5199
252b5132
RH
5200 /*
5201 * This allows macro() to pass an immediate expression for
5202 * creating short branches without creating a symbol.
bad36eac
DJ
5203 *
5204 * We don't allow branch relaxation for these branches, as
5205 * they should only appear in ".set nomacro" anyway.
252b5132
RH
5206 */
5207 if (ep->X_op == O_constant)
5208 {
df58fc94
RS
5209 /* For microMIPS we always use relocations for branches.
5210 So we should not resolve immediate values. */
5211 gas_assert (!mips_opts.micromips);
5212
bad36eac
DJ
5213 if ((ep->X_add_number & 3) != 0)
5214 as_bad (_("branch to misaligned address (0x%lx)"),
5215 (unsigned long) ep->X_add_number);
5216 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5217 as_bad (_("branch address range overflow (0x%lx)"),
5218 (unsigned long) ep->X_add_number);
252b5132
RH
5219 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5220 ep = NULL;
5221 }
5222 else
0b25d3e6 5223 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
5224 continue;
5225
5226 case 'a':
9c2799c2 5227 gas_assert (ep != NULL);
f6688943 5228 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
5229 continue;
5230
5231 case 'C':
df58fc94
RS
5232 gas_assert (!mips_opts.micromips);
5233 INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
252b5132
RH
5234 continue;
5235
d43b4baf 5236 case 'k':
df58fc94
RS
5237 INSERT_OPERAND (mips_opts.micromips,
5238 CACHE, insn, va_arg (args, unsigned long));
5239 continue;
5240
5241 case '|':
5242 gas_assert (mips_opts.micromips);
5243 INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5244 continue;
5245
5246 case '.':
5247 gas_assert (mips_opts.micromips);
5248 INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5249 continue;
5250
dec0624d
MR
5251 case '\\':
5252 INSERT_OPERAND (mips_opts.micromips,
5253 3BITPOS, insn, va_arg (args, unsigned int));
5254 continue;
5255
df58fc94 5256 case '~':
dec0624d
MR
5257 INSERT_OPERAND (mips_opts.micromips,
5258 OFFSET12, insn, va_arg (args, unsigned long));
df58fc94
RS
5259 continue;
5260
5261 case 'N':
5262 gas_assert (mips_opts.micromips);
5263 INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5264 continue;
5265
5266 case 'm': /* Opcode extension character. */
5267 gas_assert (mips_opts.micromips);
5268 switch (*fmt++)
5269 {
5270 case 'j':
5271 INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5272 break;
5273
5274 case 'p':
5275 INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5276 break;
5277
5278 case 'F':
5279 INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5280 break;
5281
5282 default:
b37df7c4 5283 abort ();
df58fc94 5284 }
d43b4baf
TS
5285 continue;
5286
252b5132 5287 default:
b37df7c4 5288 abort ();
252b5132
RH
5289 }
5290 break;
5291 }
5292 va_end (args);
9c2799c2 5293 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5294
df58fc94 5295 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5296}
5297
5298static void
67c0d1eb 5299mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 5300 va_list *args)
252b5132 5301{
1e915849 5302 struct mips_opcode *mo;
252b5132 5303 struct mips_cl_insn insn;
f6688943
TS
5304 bfd_reloc_code_real_type r[3]
5305 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 5306
1e915849 5307 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
5308 gas_assert (mo);
5309 gas_assert (strcmp (name, mo->name) == 0);
252b5132 5310
1e915849 5311 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 5312 {
1e915849 5313 ++mo;
9c2799c2
NC
5314 gas_assert (mo->name);
5315 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
5316 }
5317
1e915849 5318 create_insn (&insn, mo);
252b5132
RH
5319 for (;;)
5320 {
5321 int c;
5322
5323 c = *fmt++;
5324 switch (c)
5325 {
5326 case '\0':
5327 break;
5328
5329 case ',':
5330 case '(':
5331 case ')':
5332 continue;
5333
5334 case 'y':
5335 case 'w':
03ea81db 5336 MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
252b5132
RH
5337 continue;
5338
5339 case 'x':
5340 case 'v':
03ea81db 5341 MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
252b5132
RH
5342 continue;
5343
5344 case 'z':
03ea81db 5345 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
252b5132
RH
5346 continue;
5347
5348 case 'Z':
03ea81db 5349 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
252b5132
RH
5350 continue;
5351
5352 case '0':
5353 case 'S':
5354 case 'P':
5355 case 'R':
5356 continue;
5357
5358 case 'X':
03ea81db 5359 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
252b5132
RH
5360 continue;
5361
5362 case 'Y':
5363 {
5364 int regno;
5365
03ea81db 5366 regno = va_arg (*args, int);
252b5132 5367 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
a9e24354 5368 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
252b5132
RH
5369 }
5370 continue;
5371
5372 case '<':
5373 case '>':
5374 case '4':
5375 case '5':
5376 case 'H':
5377 case 'W':
5378 case 'D':
5379 case 'j':
5380 case '8':
5381 case 'V':
5382 case 'C':
5383 case 'U':
5384 case 'k':
5385 case 'K':
5386 case 'p':
5387 case 'q':
5388 {
b886a2ab
RS
5389 offsetT value;
5390
9c2799c2 5391 gas_assert (ep != NULL);
252b5132
RH
5392
5393 if (ep->X_op != O_constant)
874e8986 5394 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 5395 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 5396 {
b886a2ab 5397 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 5398 ep = NULL;
f6688943 5399 *r = BFD_RELOC_UNUSED;
252b5132
RH
5400 }
5401 }
5402 continue;
5403
5404 case '6':
03ea81db 5405 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
252b5132
RH
5406 continue;
5407 }
5408
5409 break;
5410 }
5411
9c2799c2 5412 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5413
df58fc94 5414 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5415}
5416
2051e8c4
MR
5417/*
5418 * Sign-extend 32-bit mode constants that have bit 31 set and all
5419 * higher bits unset.
5420 */
9f872bbe 5421static void
2051e8c4
MR
5422normalize_constant_expr (expressionS *ex)
5423{
9ee2a2d4 5424 if (ex->X_op == O_constant
2051e8c4
MR
5425 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5426 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5427 - 0x80000000);
5428}
5429
5430/*
5431 * Sign-extend 32-bit mode address offsets that have bit 31 set and
5432 * all higher bits unset.
5433 */
5434static void
5435normalize_address_expr (expressionS *ex)
5436{
5437 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5438 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5439 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5440 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5441 - 0x80000000);
5442}
5443
438c16b8
TS
5444/*
5445 * Generate a "jalr" instruction with a relocation hint to the called
5446 * function. This occurs in NewABI PIC code.
5447 */
5448static void
df58fc94 5449macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 5450{
df58fc94
RS
5451 static const bfd_reloc_code_real_type jalr_relocs[2]
5452 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5453 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5454 const char *jalr;
685736be 5455 char *f = NULL;
b34976b6 5456
1180b5a4 5457 if (MIPS_JALR_HINT_P (ep))
f21f8242 5458 {
cc3d92a5 5459 frag_grow (8);
f21f8242
AO
5460 f = frag_more (0);
5461 }
2906b037 5462 if (mips_opts.micromips)
df58fc94
RS
5463 {
5464 jalr = mips_opts.noreorder && !cprestore ? "jalr" : "jalrs";
e64af278
MR
5465 if (MIPS_JALR_HINT_P (ep)
5466 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
5467 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5468 else
5469 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5470 }
2906b037
MR
5471 else
5472 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 5473 if (MIPS_JALR_HINT_P (ep))
df58fc94 5474 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
5475}
5476
252b5132
RH
5477/*
5478 * Generate a "lui" instruction.
5479 */
5480static void
67c0d1eb 5481macro_build_lui (expressionS *ep, int regnum)
252b5132 5482{
9c2799c2 5483 gas_assert (! mips_opts.mips16);
252b5132 5484
df58fc94 5485 if (ep->X_op != O_constant)
252b5132 5486 {
9c2799c2 5487 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
5488 /* _gp_disp is a special case, used from s_cpload.
5489 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 5490 gas_assert (mips_pic == NO_PIC
78e1bb40 5491 || (! HAVE_NEWABI
aa6975fb
ILT
5492 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5493 || (! mips_in_shared
bbe506e8
TS
5494 && strcmp (S_GET_NAME (ep->X_add_symbol),
5495 "__gnu_local_gp") == 0));
252b5132
RH
5496 }
5497
df58fc94 5498 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
5499}
5500
885add95
CD
5501/* Generate a sequence of instructions to do a load or store from a constant
5502 offset off of a base register (breg) into/from a target register (treg),
5503 using AT if necessary. */
5504static void
67c0d1eb
RS
5505macro_build_ldst_constoffset (expressionS *ep, const char *op,
5506 int treg, int breg, int dbl)
885add95 5507{
9c2799c2 5508 gas_assert (ep->X_op == O_constant);
885add95 5509
256ab948 5510 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5511 if (!dbl)
5512 normalize_constant_expr (ep);
256ab948 5513
67c1ffbe 5514 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 5515 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
5516 as_warn (_("operand overflow"));
5517
5518 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5519 {
5520 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 5521 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
5522 }
5523 else
5524 {
5525 /* 32-bit offset, need multiple instructions and AT, like:
5526 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
5527 addu $tempreg,$tempreg,$breg
5528 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
5529 to handle the complete offset. */
67c0d1eb
RS
5530 macro_build_lui (ep, AT);
5531 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5532 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 5533
741fe287 5534 if (!mips_opts.at)
8fc2e39e 5535 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
5536 }
5537}
5538
252b5132
RH
5539/* set_at()
5540 * Generates code to set the $at register to true (one)
5541 * if reg is less than the immediate expression.
5542 */
5543static void
67c0d1eb 5544set_at (int reg, int unsignedp)
252b5132
RH
5545{
5546 if (imm_expr.X_op == O_constant
5547 && imm_expr.X_add_number >= -0x8000
5548 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
5549 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5550 AT, reg, BFD_RELOC_LO16);
252b5132
RH
5551 else
5552 {
67c0d1eb
RS
5553 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5554 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
5555 }
5556}
5557
5558/* Warn if an expression is not a constant. */
5559
5560static void
17a2f251 5561check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
5562{
5563 if (ex->X_op == O_big)
5564 as_bad (_("unsupported large constant"));
5565 else if (ex->X_op != O_constant)
9ee2a2d4
MR
5566 as_bad (_("Instruction %s requires absolute expression"),
5567 ip->insn_mo->name);
13757d0c 5568
9ee2a2d4
MR
5569 if (HAVE_32BIT_GPRS)
5570 normalize_constant_expr (ex);
252b5132
RH
5571}
5572
5573/* Count the leading zeroes by performing a binary chop. This is a
5574 bulky bit of source, but performance is a LOT better for the
5575 majority of values than a simple loop to count the bits:
5576 for (lcnt = 0; (lcnt < 32); lcnt++)
5577 if ((v) & (1 << (31 - lcnt)))
5578 break;
5579 However it is not code size friendly, and the gain will drop a bit
5580 on certain cached systems.
5581*/
5582#define COUNT_TOP_ZEROES(v) \
5583 (((v) & ~0xffff) == 0 \
5584 ? ((v) & ~0xff) == 0 \
5585 ? ((v) & ~0xf) == 0 \
5586 ? ((v) & ~0x3) == 0 \
5587 ? ((v) & ~0x1) == 0 \
5588 ? !(v) \
5589 ? 32 \
5590 : 31 \
5591 : 30 \
5592 : ((v) & ~0x7) == 0 \
5593 ? 29 \
5594 : 28 \
5595 : ((v) & ~0x3f) == 0 \
5596 ? ((v) & ~0x1f) == 0 \
5597 ? 27 \
5598 : 26 \
5599 : ((v) & ~0x7f) == 0 \
5600 ? 25 \
5601 : 24 \
5602 : ((v) & ~0xfff) == 0 \
5603 ? ((v) & ~0x3ff) == 0 \
5604 ? ((v) & ~0x1ff) == 0 \
5605 ? 23 \
5606 : 22 \
5607 : ((v) & ~0x7ff) == 0 \
5608 ? 21 \
5609 : 20 \
5610 : ((v) & ~0x3fff) == 0 \
5611 ? ((v) & ~0x1fff) == 0 \
5612 ? 19 \
5613 : 18 \
5614 : ((v) & ~0x7fff) == 0 \
5615 ? 17 \
5616 : 16 \
5617 : ((v) & ~0xffffff) == 0 \
5618 ? ((v) & ~0xfffff) == 0 \
5619 ? ((v) & ~0x3ffff) == 0 \
5620 ? ((v) & ~0x1ffff) == 0 \
5621 ? 15 \
5622 : 14 \
5623 : ((v) & ~0x7ffff) == 0 \
5624 ? 13 \
5625 : 12 \
5626 : ((v) & ~0x3fffff) == 0 \
5627 ? ((v) & ~0x1fffff) == 0 \
5628 ? 11 \
5629 : 10 \
5630 : ((v) & ~0x7fffff) == 0 \
5631 ? 9 \
5632 : 8 \
5633 : ((v) & ~0xfffffff) == 0 \
5634 ? ((v) & ~0x3ffffff) == 0 \
5635 ? ((v) & ~0x1ffffff) == 0 \
5636 ? 7 \
5637 : 6 \
5638 : ((v) & ~0x7ffffff) == 0 \
5639 ? 5 \
5640 : 4 \
5641 : ((v) & ~0x3fffffff) == 0 \
5642 ? ((v) & ~0x1fffffff) == 0 \
5643 ? 3 \
5644 : 2 \
5645 : ((v) & ~0x7fffffff) == 0 \
5646 ? 1 \
5647 : 0)
5648
5649/* load_register()
67c1ffbe 5650 * This routine generates the least number of instructions necessary to load
252b5132
RH
5651 * an absolute expression value into a register.
5652 */
5653static void
67c0d1eb 5654load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
5655{
5656 int freg;
5657 expressionS hi32, lo32;
5658
5659 if (ep->X_op != O_big)
5660 {
9c2799c2 5661 gas_assert (ep->X_op == O_constant);
256ab948
TS
5662
5663 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5664 if (!dbl)
5665 normalize_constant_expr (ep);
256ab948
TS
5666
5667 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
5668 {
5669 /* We can handle 16 bit signed values with an addiu to
5670 $zero. No need to ever use daddiu here, since $zero and
5671 the result are always correct in 32 bit mode. */
67c0d1eb 5672 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5673 return;
5674 }
5675 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5676 {
5677 /* We can handle 16 bit unsigned values with an ori to
5678 $zero. */
67c0d1eb 5679 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5680 return;
5681 }
256ab948 5682 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
5683 {
5684 /* 32 bit values require an lui. */
df58fc94 5685 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 5686 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 5687 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
5688 return;
5689 }
5690 }
5691
5692 /* The value is larger than 32 bits. */
5693
2051e8c4 5694 if (!dbl || HAVE_32BIT_GPRS)
252b5132 5695 {
55e08f71
NC
5696 char value[32];
5697
5698 sprintf_vma (value, ep->X_add_number);
20e1fcfd 5699 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 5700 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5701 return;
5702 }
5703
5704 if (ep->X_op != O_big)
5705 {
5706 hi32 = *ep;
5707 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5708 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5709 hi32.X_add_number &= 0xffffffff;
5710 lo32 = *ep;
5711 lo32.X_add_number &= 0xffffffff;
5712 }
5713 else
5714 {
9c2799c2 5715 gas_assert (ep->X_add_number > 2);
252b5132
RH
5716 if (ep->X_add_number == 3)
5717 generic_bignum[3] = 0;
5718 else if (ep->X_add_number > 4)
5719 as_bad (_("Number larger than 64 bits"));
5720 lo32.X_op = O_constant;
5721 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
5722 hi32.X_op = O_constant;
5723 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
5724 }
5725
5726 if (hi32.X_add_number == 0)
5727 freg = 0;
5728 else
5729 {
5730 int shift, bit;
5731 unsigned long hi, lo;
5732
956cd1d6 5733 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
5734 {
5735 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
5736 {
67c0d1eb 5737 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
5738 return;
5739 }
5740 if (lo32.X_add_number & 0x80000000)
5741 {
df58fc94 5742 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 5743 if (lo32.X_add_number & 0xffff)
67c0d1eb 5744 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
5745 return;
5746 }
5747 }
252b5132
RH
5748
5749 /* Check for 16bit shifted constant. We know that hi32 is
5750 non-zero, so start the mask on the first bit of the hi32
5751 value. */
5752 shift = 17;
5753 do
beae10d5
KH
5754 {
5755 unsigned long himask, lomask;
5756
5757 if (shift < 32)
5758 {
5759 himask = 0xffff >> (32 - shift);
5760 lomask = (0xffff << shift) & 0xffffffff;
5761 }
5762 else
5763 {
5764 himask = 0xffff << (shift - 32);
5765 lomask = 0;
5766 }
5767 if ((hi32.X_add_number & ~(offsetT) himask) == 0
5768 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
5769 {
5770 expressionS tmp;
5771
5772 tmp.X_op = O_constant;
5773 if (shift < 32)
5774 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
5775 | (lo32.X_add_number >> shift));
5776 else
5777 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 5778 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 5779 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 5780 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
5781 return;
5782 }
f9419b05 5783 ++shift;
beae10d5
KH
5784 }
5785 while (shift <= (64 - 16));
252b5132
RH
5786
5787 /* Find the bit number of the lowest one bit, and store the
5788 shifted value in hi/lo. */
5789 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
5790 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
5791 if (lo != 0)
5792 {
5793 bit = 0;
5794 while ((lo & 1) == 0)
5795 {
5796 lo >>= 1;
5797 ++bit;
5798 }
5799 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
5800 hi >>= bit;
5801 }
5802 else
5803 {
5804 bit = 32;
5805 while ((hi & 1) == 0)
5806 {
5807 hi >>= 1;
5808 ++bit;
5809 }
5810 lo = hi;
5811 hi = 0;
5812 }
5813
5814 /* Optimize if the shifted value is a (power of 2) - 1. */
5815 if ((hi == 0 && ((lo + 1) & lo) == 0)
5816 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
5817 {
5818 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 5819 if (shift != 0)
beae10d5 5820 {
252b5132
RH
5821 expressionS tmp;
5822
5823 /* This instruction will set the register to be all
5824 ones. */
beae10d5
KH
5825 tmp.X_op = O_constant;
5826 tmp.X_add_number = (offsetT) -1;
67c0d1eb 5827 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
5828 if (bit != 0)
5829 {
5830 bit += shift;
df58fc94 5831 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 5832 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 5833 }
df58fc94 5834 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 5835 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
5836 return;
5837 }
5838 }
252b5132
RH
5839
5840 /* Sign extend hi32 before calling load_register, because we can
5841 generally get better code when we load a sign extended value. */
5842 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 5843 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 5844 load_register (reg, &hi32, 0);
252b5132
RH
5845 freg = reg;
5846 }
5847 if ((lo32.X_add_number & 0xffff0000) == 0)
5848 {
5849 if (freg != 0)
5850 {
df58fc94 5851 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
5852 freg = reg;
5853 }
5854 }
5855 else
5856 {
5857 expressionS mid16;
5858
956cd1d6 5859 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 5860 {
df58fc94
RS
5861 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5862 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
5863 return;
5864 }
252b5132
RH
5865
5866 if (freg != 0)
5867 {
df58fc94 5868 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
5869 freg = reg;
5870 }
5871 mid16 = lo32;
5872 mid16.X_add_number >>= 16;
67c0d1eb 5873 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 5874 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
5875 freg = reg;
5876 }
5877 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 5878 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
5879}
5880
269137b2
TS
5881static inline void
5882load_delay_nop (void)
5883{
5884 if (!gpr_interlocks)
5885 macro_build (NULL, "nop", "");
5886}
5887
252b5132
RH
5888/* Load an address into a register. */
5889
5890static void
67c0d1eb 5891load_address (int reg, expressionS *ep, int *used_at)
252b5132 5892{
252b5132
RH
5893 if (ep->X_op != O_constant
5894 && ep->X_op != O_symbol)
5895 {
5896 as_bad (_("expression too complex"));
5897 ep->X_op = O_constant;
5898 }
5899
5900 if (ep->X_op == O_constant)
5901 {
67c0d1eb 5902 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
5903 return;
5904 }
5905
5906 if (mips_pic == NO_PIC)
5907 {
5908 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 5909 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5910 Otherwise we want
5911 lui $reg,<sym> (BFD_RELOC_HI16_S)
5912 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 5913 If we have an addend, we always use the latter form.
76b3015f 5914
d6bc6245
TS
5915 With 64bit address space and a usable $at we want
5916 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5917 lui $at,<sym> (BFD_RELOC_HI16_S)
5918 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5919 daddiu $at,<sym> (BFD_RELOC_LO16)
5920 dsll32 $reg,0
3a482fd5 5921 daddu $reg,$reg,$at
76b3015f 5922
c03099e6 5923 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
5924 on superscalar processors.
5925 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5926 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5927 dsll $reg,16
5928 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
5929 dsll $reg,16
5930 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
5931
5932 For GP relative symbols in 64bit address space we can use
5933 the same sequence as in 32bit address space. */
aed1a261 5934 if (HAVE_64BIT_SYMBOLS)
d6bc6245 5935 {
6caf9ef4
TS
5936 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5937 && !nopic_need_relax (ep->X_add_symbol, 1))
5938 {
5939 relax_start (ep->X_add_symbol);
5940 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5941 mips_gp_register, BFD_RELOC_GPREL16);
5942 relax_switch ();
5943 }
d6bc6245 5944
741fe287 5945 if (*used_at == 0 && mips_opts.at)
d6bc6245 5946 {
df58fc94
RS
5947 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5948 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
5949 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5950 BFD_RELOC_MIPS_HIGHER);
5951 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 5952 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 5953 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
5954 *used_at = 1;
5955 }
5956 else
5957 {
df58fc94 5958 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
5959 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5960 BFD_RELOC_MIPS_HIGHER);
df58fc94 5961 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 5962 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 5963 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 5964 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 5965 }
6caf9ef4
TS
5966
5967 if (mips_relax.sequence)
5968 relax_end ();
d6bc6245 5969 }
252b5132
RH
5970 else
5971 {
d6bc6245 5972 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 5973 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 5974 {
4d7206a2 5975 relax_start (ep->X_add_symbol);
67c0d1eb 5976 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 5977 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5978 relax_switch ();
d6bc6245 5979 }
67c0d1eb
RS
5980 macro_build_lui (ep, reg);
5981 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
5982 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
5983 if (mips_relax.sequence)
5984 relax_end ();
d6bc6245 5985 }
252b5132 5986 }
0a44bf69 5987 else if (!mips_big_got)
252b5132
RH
5988 {
5989 expressionS ex;
5990
5991 /* If this is a reference to an external symbol, we want
5992 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5993 Otherwise we want
5994 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5995 nop
5996 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
5997 If there is a constant, it must be added in after.
5998
ed6fb7bd 5999 If we have NewABI, we want
f5040a92
AO
6000 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
6001 unless we're referencing a global symbol with a non-zero
6002 offset, in which case cst must be added separately. */
ed6fb7bd
SC
6003 if (HAVE_NEWABI)
6004 {
f5040a92
AO
6005 if (ep->X_add_number)
6006 {
4d7206a2 6007 ex.X_add_number = ep->X_add_number;
f5040a92 6008 ep->X_add_number = 0;
4d7206a2 6009 relax_start (ep->X_add_symbol);
67c0d1eb
RS
6010 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6011 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
6012 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6013 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6014 ex.X_op = O_constant;
67c0d1eb 6015 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 6016 reg, reg, BFD_RELOC_LO16);
f5040a92 6017 ep->X_add_number = ex.X_add_number;
4d7206a2 6018 relax_switch ();
f5040a92 6019 }
67c0d1eb 6020 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6021 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
6022 if (mips_relax.sequence)
6023 relax_end ();
ed6fb7bd
SC
6024 }
6025 else
6026 {
f5040a92
AO
6027 ex.X_add_number = ep->X_add_number;
6028 ep->X_add_number = 0;
67c0d1eb
RS
6029 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6030 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6031 load_delay_nop ();
4d7206a2
RS
6032 relax_start (ep->X_add_symbol);
6033 relax_switch ();
67c0d1eb 6034 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 6035 BFD_RELOC_LO16);
4d7206a2 6036 relax_end ();
ed6fb7bd 6037
f5040a92
AO
6038 if (ex.X_add_number != 0)
6039 {
6040 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6041 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6042 ex.X_op = O_constant;
67c0d1eb 6043 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 6044 reg, reg, BFD_RELOC_LO16);
f5040a92 6045 }
252b5132
RH
6046 }
6047 }
0a44bf69 6048 else if (mips_big_got)
252b5132
RH
6049 {
6050 expressionS ex;
252b5132
RH
6051
6052 /* This is the large GOT case. If this is a reference to an
6053 external symbol, we want
6054 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6055 addu $reg,$reg,$gp
6056 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
6057
6058 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
6059 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6060 nop
6061 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 6062 If there is a constant, it must be added in after.
f5040a92
AO
6063
6064 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
6065 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6066 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 6067 */
438c16b8
TS
6068 if (HAVE_NEWABI)
6069 {
4d7206a2 6070 ex.X_add_number = ep->X_add_number;
f5040a92 6071 ep->X_add_number = 0;
4d7206a2 6072 relax_start (ep->X_add_symbol);
df58fc94 6073 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6074 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6075 reg, reg, mips_gp_register);
6076 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6077 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
6078 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6079 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6080 else if (ex.X_add_number)
6081 {
6082 ex.X_op = O_constant;
67c0d1eb
RS
6083 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6084 BFD_RELOC_LO16);
f5040a92
AO
6085 }
6086
6087 ep->X_add_number = ex.X_add_number;
4d7206a2 6088 relax_switch ();
67c0d1eb 6089 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6090 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
6091 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6092 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 6093 relax_end ();
438c16b8 6094 }
252b5132 6095 else
438c16b8 6096 {
f5040a92
AO
6097 ex.X_add_number = ep->X_add_number;
6098 ep->X_add_number = 0;
4d7206a2 6099 relax_start (ep->X_add_symbol);
df58fc94 6100 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6101 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6102 reg, reg, mips_gp_register);
6103 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6104 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
6105 relax_switch ();
6106 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
6107 {
6108 /* We need a nop before loading from $gp. This special
6109 check is required because the lui which starts the main
6110 instruction stream does not refer to $gp, and so will not
6111 insert the nop which may be required. */
67c0d1eb 6112 macro_build (NULL, "nop", "");
438c16b8 6113 }
67c0d1eb 6114 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6115 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6116 load_delay_nop ();
67c0d1eb 6117 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 6118 BFD_RELOC_LO16);
4d7206a2 6119 relax_end ();
438c16b8 6120
f5040a92
AO
6121 if (ex.X_add_number != 0)
6122 {
6123 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6124 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6125 ex.X_op = O_constant;
67c0d1eb
RS
6126 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6127 BFD_RELOC_LO16);
f5040a92 6128 }
252b5132
RH
6129 }
6130 }
252b5132
RH
6131 else
6132 abort ();
8fc2e39e 6133
741fe287 6134 if (!mips_opts.at && *used_at == 1)
8fc2e39e 6135 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
6136}
6137
ea1fb5dc
RS
6138/* Move the contents of register SOURCE into register DEST. */
6139
6140static void
67c0d1eb 6141move_register (int dest, int source)
ea1fb5dc 6142{
df58fc94
RS
6143 /* Prefer to use a 16-bit microMIPS instruction unless the previous
6144 instruction specifically requires a 32-bit one. */
6145 if (mips_opts.micromips
6146 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 6147 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94
RS
6148 else
6149 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
6150 dest, source, 0);
ea1fb5dc
RS
6151}
6152
4d7206a2 6153/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
6154 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
6155 The two alternatives are:
4d7206a2
RS
6156
6157 Global symbol Local sybmol
6158 ------------- ------------
6159 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
6160 ... ...
6161 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
6162
6163 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
6164 emits the second for a 16-bit offset or add_got_offset_hilo emits
6165 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
6166
6167static void
67c0d1eb 6168load_got_offset (int dest, expressionS *local)
4d7206a2
RS
6169{
6170 expressionS global;
6171
6172 global = *local;
6173 global.X_add_number = 0;
6174
6175 relax_start (local->X_add_symbol);
67c0d1eb
RS
6176 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6177 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 6178 relax_switch ();
67c0d1eb
RS
6179 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6180 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
6181 relax_end ();
6182}
6183
6184static void
67c0d1eb 6185add_got_offset (int dest, expressionS *local)
4d7206a2
RS
6186{
6187 expressionS global;
6188
6189 global.X_op = O_constant;
6190 global.X_op_symbol = NULL;
6191 global.X_add_symbol = NULL;
6192 global.X_add_number = local->X_add_number;
6193
6194 relax_start (local->X_add_symbol);
67c0d1eb 6195 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
6196 dest, dest, BFD_RELOC_LO16);
6197 relax_switch ();
67c0d1eb 6198 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
6199 relax_end ();
6200}
6201
f6a22291
MR
6202static void
6203add_got_offset_hilo (int dest, expressionS *local, int tmp)
6204{
6205 expressionS global;
6206 int hold_mips_optimize;
6207
6208 global.X_op = O_constant;
6209 global.X_op_symbol = NULL;
6210 global.X_add_symbol = NULL;
6211 global.X_add_number = local->X_add_number;
6212
6213 relax_start (local->X_add_symbol);
6214 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6215 relax_switch ();
6216 /* Set mips_optimize around the lui instruction to avoid
6217 inserting an unnecessary nop after the lw. */
6218 hold_mips_optimize = mips_optimize;
6219 mips_optimize = 2;
6220 macro_build_lui (&global, tmp);
6221 mips_optimize = hold_mips_optimize;
6222 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6223 relax_end ();
6224
6225 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6226}
6227
df58fc94
RS
6228/* Emit a sequence of instructions to emulate a branch likely operation.
6229 BR is an ordinary branch corresponding to one to be emulated. BRNEG
6230 is its complementing branch with the original condition negated.
6231 CALL is set if the original branch specified the link operation.
6232 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6233
6234 Code like this is produced in the noreorder mode:
6235
6236 BRNEG <args>, 1f
6237 nop
6238 b <sym>
6239 delay slot (executed only if branch taken)
6240 1:
6241
6242 or, if CALL is set:
6243
6244 BRNEG <args>, 1f
6245 nop
6246 bal <sym>
6247 delay slot (executed only if branch taken)
6248 1:
6249
6250 In the reorder mode the delay slot would be filled with a nop anyway,
6251 so code produced is simply:
6252
6253 BR <args>, <sym>
6254 nop
6255
6256 This function is used when producing code for the microMIPS ASE that
6257 does not implement branch likely instructions in hardware. */
6258
6259static void
6260macro_build_branch_likely (const char *br, const char *brneg,
6261 int call, expressionS *ep, const char *fmt,
6262 unsigned int sreg, unsigned int treg)
6263{
6264 int noreorder = mips_opts.noreorder;
6265 expressionS expr1;
6266
6267 gas_assert (mips_opts.micromips);
6268 start_noreorder ();
6269 if (noreorder)
6270 {
6271 micromips_label_expr (&expr1);
6272 macro_build (&expr1, brneg, fmt, sreg, treg);
6273 macro_build (NULL, "nop", "");
6274 macro_build (ep, call ? "bal" : "b", "p");
6275
6276 /* Set to true so that append_insn adds a label. */
6277 emit_branch_likely_macro = TRUE;
6278 }
6279 else
6280 {
6281 macro_build (ep, br, fmt, sreg, treg);
6282 macro_build (NULL, "nop", "");
6283 }
6284 end_noreorder ();
6285}
6286
6287/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6288 the condition code tested. EP specifies the branch target. */
6289
6290static void
6291macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6292{
6293 const int call = 0;
6294 const char *brneg;
6295 const char *br;
6296
6297 switch (type)
6298 {
6299 case M_BC1FL:
6300 br = "bc1f";
6301 brneg = "bc1t";
6302 break;
6303 case M_BC1TL:
6304 br = "bc1t";
6305 brneg = "bc1f";
6306 break;
6307 case M_BC2FL:
6308 br = "bc2f";
6309 brneg = "bc2t";
6310 break;
6311 case M_BC2TL:
6312 br = "bc2t";
6313 brneg = "bc2f";
6314 break;
6315 default:
6316 abort ();
6317 }
6318 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6319}
6320
6321/* Emit a two-argument branch macro specified by TYPE, using SREG as
6322 the register tested. EP specifies the branch target. */
6323
6324static void
6325macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6326{
6327 const char *brneg = NULL;
6328 const char *br;
6329 int call = 0;
6330
6331 switch (type)
6332 {
6333 case M_BGEZ:
6334 br = "bgez";
6335 break;
6336 case M_BGEZL:
6337 br = mips_opts.micromips ? "bgez" : "bgezl";
6338 brneg = "bltz";
6339 break;
6340 case M_BGEZALL:
6341 gas_assert (mips_opts.micromips);
6342 br = "bgezals";
6343 brneg = "bltz";
6344 call = 1;
6345 break;
6346 case M_BGTZ:
6347 br = "bgtz";
6348 break;
6349 case M_BGTZL:
6350 br = mips_opts.micromips ? "bgtz" : "bgtzl";
6351 brneg = "blez";
6352 break;
6353 case M_BLEZ:
6354 br = "blez";
6355 break;
6356 case M_BLEZL:
6357 br = mips_opts.micromips ? "blez" : "blezl";
6358 brneg = "bgtz";
6359 break;
6360 case M_BLTZ:
6361 br = "bltz";
6362 break;
6363 case M_BLTZL:
6364 br = mips_opts.micromips ? "bltz" : "bltzl";
6365 brneg = "bgez";
6366 break;
6367 case M_BLTZALL:
6368 gas_assert (mips_opts.micromips);
6369 br = "bltzals";
6370 brneg = "bgez";
6371 call = 1;
6372 break;
6373 default:
6374 abort ();
6375 }
6376 if (mips_opts.micromips && brneg)
6377 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6378 else
6379 macro_build (ep, br, "s,p", sreg);
6380}
6381
6382/* Emit a three-argument branch macro specified by TYPE, using SREG and
6383 TREG as the registers tested. EP specifies the branch target. */
6384
6385static void
6386macro_build_branch_rsrt (int type, expressionS *ep,
6387 unsigned int sreg, unsigned int treg)
6388{
6389 const char *brneg = NULL;
6390 const int call = 0;
6391 const char *br;
6392
6393 switch (type)
6394 {
6395 case M_BEQ:
6396 case M_BEQ_I:
6397 br = "beq";
6398 break;
6399 case M_BEQL:
6400 case M_BEQL_I:
6401 br = mips_opts.micromips ? "beq" : "beql";
6402 brneg = "bne";
6403 break;
6404 case M_BNE:
6405 case M_BNE_I:
6406 br = "bne";
6407 break;
6408 case M_BNEL:
6409 case M_BNEL_I:
6410 br = mips_opts.micromips ? "bne" : "bnel";
6411 brneg = "beq";
6412 break;
6413 default:
6414 abort ();
6415 }
6416 if (mips_opts.micromips && brneg)
6417 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6418 else
6419 macro_build (ep, br, "s,t,p", sreg, treg);
6420}
6421
252b5132
RH
6422/*
6423 * Build macros
6424 * This routine implements the seemingly endless macro or synthesized
6425 * instructions and addressing modes in the mips assembly language. Many
6426 * of these macros are simple and are similar to each other. These could
67c1ffbe 6427 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
6428 * this verbose method. Others are not simple macros but are more like
6429 * optimizing code generation.
6430 * One interesting optimization is when several store macros appear
67c1ffbe 6431 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
6432 * The ensuing load upper instructions are ommited. This implies some kind
6433 * of global optimization. We currently only optimize within a single macro.
6434 * For many of the load and store macros if the address is specified as a
6435 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6436 * first load register 'at' with zero and use it as the base register. The
6437 * mips assembler simply uses register $zero. Just one tiny optimization
6438 * we're missing.
6439 */
6440static void
17a2f251 6441macro (struct mips_cl_insn *ip)
252b5132 6442{
741fe287
MR
6443 unsigned int treg, sreg, dreg, breg;
6444 unsigned int tempreg;
252b5132 6445 int mask;
43841e91 6446 int used_at = 0;
df58fc94 6447 expressionS label_expr;
252b5132 6448 expressionS expr1;
df58fc94 6449 expressionS *ep;
252b5132
RH
6450 const char *s;
6451 const char *s2;
6452 const char *fmt;
6453 int likely = 0;
252b5132 6454 int coproc = 0;
df58fc94 6455 int off12 = 0;
1abe91b1 6456 int call = 0;
df58fc94
RS
6457 int jals = 0;
6458 int dbl = 0;
6459 int imm = 0;
6460 int ust = 0;
6461 int lp = 0;
6462 int ab = 0;
dd6a37e7 6463 int off0 = 0;
252b5132 6464 int off;
67c0d1eb 6465 offsetT maxnum;
252b5132 6466 bfd_reloc_code_real_type r;
252b5132
RH
6467 int hold_mips_optimize;
6468
9c2799c2 6469 gas_assert (! mips_opts.mips16);
252b5132 6470
df58fc94
RS
6471 treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6472 dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6473 sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
252b5132
RH
6474 mask = ip->insn_mo->mask;
6475
df58fc94
RS
6476 label_expr.X_op = O_constant;
6477 label_expr.X_op_symbol = NULL;
6478 label_expr.X_add_symbol = NULL;
6479 label_expr.X_add_number = 0;
6480
252b5132
RH
6481 expr1.X_op = O_constant;
6482 expr1.X_op_symbol = NULL;
6483 expr1.X_add_symbol = NULL;
6484 expr1.X_add_number = 1;
6485
6486 switch (mask)
6487 {
6488 case M_DABS:
6489 dbl = 1;
6490 case M_ABS:
df58fc94
RS
6491 /* bgez $a0,1f
6492 move v0,$a0
6493 sub v0,$zero,$a0
6494 1:
6495 */
252b5132 6496
7d10b47d 6497 start_noreorder ();
252b5132 6498
df58fc94
RS
6499 if (mips_opts.micromips)
6500 micromips_label_expr (&label_expr);
6501 else
6502 label_expr.X_add_number = 8;
6503 macro_build (&label_expr, "bgez", "s,p", sreg);
252b5132 6504 if (dreg == sreg)
a605d2b3 6505 macro_build (NULL, "nop", "");
252b5132 6506 else
67c0d1eb
RS
6507 move_register (dreg, sreg);
6508 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
df58fc94
RS
6509 if (mips_opts.micromips)
6510 micromips_add_label ();
252b5132 6511
7d10b47d 6512 end_noreorder ();
8fc2e39e 6513 break;
252b5132
RH
6514
6515 case M_ADD_I:
6516 s = "addi";
6517 s2 = "add";
6518 goto do_addi;
6519 case M_ADDU_I:
6520 s = "addiu";
6521 s2 = "addu";
6522 goto do_addi;
6523 case M_DADD_I:
6524 dbl = 1;
6525 s = "daddi";
6526 s2 = "dadd";
df58fc94
RS
6527 if (!mips_opts.micromips)
6528 goto do_addi;
6529 if (imm_expr.X_op == O_constant
6530 && imm_expr.X_add_number >= -0x200
6531 && imm_expr.X_add_number < 0x200)
6532 {
6533 macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6534 break;
6535 }
6536 goto do_addi_i;
252b5132
RH
6537 case M_DADDU_I:
6538 dbl = 1;
6539 s = "daddiu";
6540 s2 = "daddu";
6541 do_addi:
6542 if (imm_expr.X_op == O_constant
6543 && imm_expr.X_add_number >= -0x8000
6544 && imm_expr.X_add_number < 0x8000)
6545 {
67c0d1eb 6546 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 6547 break;
252b5132 6548 }
df58fc94 6549 do_addi_i:
8fc2e39e 6550 used_at = 1;
67c0d1eb
RS
6551 load_register (AT, &imm_expr, dbl);
6552 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6553 break;
6554
6555 case M_AND_I:
6556 s = "andi";
6557 s2 = "and";
6558 goto do_bit;
6559 case M_OR_I:
6560 s = "ori";
6561 s2 = "or";
6562 goto do_bit;
6563 case M_NOR_I:
6564 s = "";
6565 s2 = "nor";
6566 goto do_bit;
6567 case M_XOR_I:
6568 s = "xori";
6569 s2 = "xor";
6570 do_bit:
6571 if (imm_expr.X_op == O_constant
6572 && imm_expr.X_add_number >= 0
6573 && imm_expr.X_add_number < 0x10000)
6574 {
6575 if (mask != M_NOR_I)
67c0d1eb 6576 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
6577 else
6578 {
67c0d1eb
RS
6579 macro_build (&imm_expr, "ori", "t,r,i",
6580 treg, sreg, BFD_RELOC_LO16);
6581 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 6582 }
8fc2e39e 6583 break;
252b5132
RH
6584 }
6585
8fc2e39e 6586 used_at = 1;
67c0d1eb
RS
6587 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6588 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6589 break;
6590
8b082fb1
TS
6591 case M_BALIGN:
6592 switch (imm_expr.X_add_number)
6593 {
6594 case 0:
6595 macro_build (NULL, "nop", "");
6596 break;
6597 case 2:
6598 macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6599 break;
03f66e8a
MR
6600 case 1:
6601 case 3:
8b082fb1 6602 macro_build (NULL, "balign", "t,s,2", treg, sreg,
90ecf173 6603 (int) imm_expr.X_add_number);
8b082fb1 6604 break;
03f66e8a
MR
6605 default:
6606 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6607 (unsigned long) imm_expr.X_add_number);
6608 break;
8b082fb1
TS
6609 }
6610 break;
6611
df58fc94
RS
6612 case M_BC1FL:
6613 case M_BC1TL:
6614 case M_BC2FL:
6615 case M_BC2TL:
6616 gas_assert (mips_opts.micromips);
6617 macro_build_branch_ccl (mask, &offset_expr,
6618 EXTRACT_OPERAND (1, BCC, *ip));
6619 break;
6620
252b5132 6621 case M_BEQ_I:
252b5132 6622 case M_BEQL_I:
252b5132 6623 case M_BNE_I:
252b5132 6624 case M_BNEL_I:
252b5132 6625 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
6626 treg = 0;
6627 else
252b5132 6628 {
df58fc94
RS
6629 treg = AT;
6630 used_at = 1;
6631 load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
252b5132 6632 }
df58fc94
RS
6633 /* Fall through. */
6634 case M_BEQL:
6635 case M_BNEL:
6636 macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
252b5132
RH
6637 break;
6638
6639 case M_BGEL:
6640 likely = 1;
6641 case M_BGE:
6642 if (treg == 0)
df58fc94
RS
6643 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6644 else if (sreg == 0)
6645 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6646 else
252b5132 6647 {
df58fc94
RS
6648 used_at = 1;
6649 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6650 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6651 &offset_expr, AT, ZERO);
252b5132 6652 }
df58fc94
RS
6653 break;
6654
6655 case M_BGEZL:
6656 case M_BGEZALL:
6657 case M_BGTZL:
6658 case M_BLEZL:
6659 case M_BLTZL:
6660 case M_BLTZALL:
6661 macro_build_branch_rs (mask, &offset_expr, sreg);
252b5132
RH
6662 break;
6663
6664 case M_BGTL_I:
6665 likely = 1;
6666 case M_BGT_I:
90ecf173 6667 /* Check for > max integer. */
252b5132 6668 maxnum = 0x7fffffff;
ca4e0257 6669 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6670 {
6671 maxnum <<= 16;
6672 maxnum |= 0xffff;
6673 maxnum <<= 16;
6674 maxnum |= 0xffff;
6675 }
6676 if (imm_expr.X_op == O_constant
6677 && imm_expr.X_add_number >= maxnum
ca4e0257 6678 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6679 {
6680 do_false:
90ecf173 6681 /* Result is always false. */
252b5132 6682 if (! likely)
a605d2b3 6683 macro_build (NULL, "nop", "");
252b5132 6684 else
df58fc94 6685 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 6686 break;
252b5132
RH
6687 }
6688 if (imm_expr.X_op != O_constant)
6689 as_bad (_("Unsupported large constant"));
f9419b05 6690 ++imm_expr.X_add_number;
252b5132
RH
6691 /* FALLTHROUGH */
6692 case M_BGE_I:
6693 case M_BGEL_I:
6694 if (mask == M_BGEL_I)
6695 likely = 1;
6696 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6697 {
df58fc94
RS
6698 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
6699 &offset_expr, sreg);
8fc2e39e 6700 break;
252b5132
RH
6701 }
6702 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6703 {
df58fc94
RS
6704 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
6705 &offset_expr, sreg);
8fc2e39e 6706 break;
252b5132
RH
6707 }
6708 maxnum = 0x7fffffff;
ca4e0257 6709 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6710 {
6711 maxnum <<= 16;
6712 maxnum |= 0xffff;
6713 maxnum <<= 16;
6714 maxnum |= 0xffff;
6715 }
6716 maxnum = - maxnum - 1;
6717 if (imm_expr.X_op == O_constant
6718 && imm_expr.X_add_number <= maxnum
ca4e0257 6719 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6720 {
6721 do_true:
6722 /* result is always true */
6723 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 6724 macro_build (&offset_expr, "b", "p");
8fc2e39e 6725 break;
252b5132 6726 }
8fc2e39e 6727 used_at = 1;
67c0d1eb 6728 set_at (sreg, 0);
df58fc94
RS
6729 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6730 &offset_expr, AT, ZERO);
252b5132
RH
6731 break;
6732
6733 case M_BGEUL:
6734 likely = 1;
6735 case M_BGEU:
6736 if (treg == 0)
6737 goto do_true;
df58fc94
RS
6738 else if (sreg == 0)
6739 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6740 &offset_expr, ZERO, treg);
6741 else
252b5132 6742 {
df58fc94
RS
6743 used_at = 1;
6744 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6745 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6746 &offset_expr, AT, ZERO);
252b5132 6747 }
252b5132
RH
6748 break;
6749
6750 case M_BGTUL_I:
6751 likely = 1;
6752 case M_BGTU_I:
6753 if (sreg == 0
ca4e0257 6754 || (HAVE_32BIT_GPRS
252b5132 6755 && imm_expr.X_op == O_constant
f01dc953 6756 && imm_expr.X_add_number == -1))
252b5132
RH
6757 goto do_false;
6758 if (imm_expr.X_op != O_constant)
6759 as_bad (_("Unsupported large constant"));
f9419b05 6760 ++imm_expr.X_add_number;
252b5132
RH
6761 /* FALLTHROUGH */
6762 case M_BGEU_I:
6763 case M_BGEUL_I:
6764 if (mask == M_BGEUL_I)
6765 likely = 1;
6766 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6767 goto do_true;
df58fc94
RS
6768 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6769 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6770 &offset_expr, sreg, ZERO);
6771 else
252b5132 6772 {
df58fc94
RS
6773 used_at = 1;
6774 set_at (sreg, 1);
6775 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6776 &offset_expr, AT, ZERO);
252b5132 6777 }
252b5132
RH
6778 break;
6779
6780 case M_BGTL:
6781 likely = 1;
6782 case M_BGT:
6783 if (treg == 0)
df58fc94
RS
6784 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
6785 else if (sreg == 0)
6786 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
6787 else
252b5132 6788 {
df58fc94
RS
6789 used_at = 1;
6790 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6791 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6792 &offset_expr, AT, ZERO);
252b5132 6793 }
252b5132
RH
6794 break;
6795
6796 case M_BGTUL:
6797 likely = 1;
6798 case M_BGTU:
6799 if (treg == 0)
df58fc94
RS
6800 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6801 &offset_expr, sreg, ZERO);
6802 else if (sreg == 0)
6803 goto do_false;
6804 else
252b5132 6805 {
df58fc94
RS
6806 used_at = 1;
6807 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6808 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6809 &offset_expr, AT, ZERO);
252b5132 6810 }
252b5132
RH
6811 break;
6812
6813 case M_BLEL:
6814 likely = 1;
6815 case M_BLE:
6816 if (treg == 0)
df58fc94
RS
6817 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6818 else if (sreg == 0)
6819 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
6820 else
252b5132 6821 {
df58fc94
RS
6822 used_at = 1;
6823 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6824 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6825 &offset_expr, AT, ZERO);
252b5132 6826 }
252b5132
RH
6827 break;
6828
6829 case M_BLEL_I:
6830 likely = 1;
6831 case M_BLE_I:
6832 maxnum = 0x7fffffff;
ca4e0257 6833 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6834 {
6835 maxnum <<= 16;
6836 maxnum |= 0xffff;
6837 maxnum <<= 16;
6838 maxnum |= 0xffff;
6839 }
6840 if (imm_expr.X_op == O_constant
6841 && imm_expr.X_add_number >= maxnum
ca4e0257 6842 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6843 goto do_true;
6844 if (imm_expr.X_op != O_constant)
6845 as_bad (_("Unsupported large constant"));
f9419b05 6846 ++imm_expr.X_add_number;
252b5132
RH
6847 /* FALLTHROUGH */
6848 case M_BLT_I:
6849 case M_BLTL_I:
6850 if (mask == M_BLTL_I)
6851 likely = 1;
6852 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
6853 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6854 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6855 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6856 else
252b5132 6857 {
df58fc94
RS
6858 used_at = 1;
6859 set_at (sreg, 0);
6860 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6861 &offset_expr, AT, ZERO);
252b5132 6862 }
252b5132
RH
6863 break;
6864
6865 case M_BLEUL:
6866 likely = 1;
6867 case M_BLEU:
6868 if (treg == 0)
df58fc94
RS
6869 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6870 &offset_expr, sreg, ZERO);
6871 else if (sreg == 0)
6872 goto do_true;
6873 else
252b5132 6874 {
df58fc94
RS
6875 used_at = 1;
6876 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6877 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6878 &offset_expr, AT, ZERO);
252b5132 6879 }
252b5132
RH
6880 break;
6881
6882 case M_BLEUL_I:
6883 likely = 1;
6884 case M_BLEU_I:
6885 if (sreg == 0
ca4e0257 6886 || (HAVE_32BIT_GPRS
252b5132 6887 && imm_expr.X_op == O_constant
f01dc953 6888 && imm_expr.X_add_number == -1))
252b5132
RH
6889 goto do_true;
6890 if (imm_expr.X_op != O_constant)
6891 as_bad (_("Unsupported large constant"));
f9419b05 6892 ++imm_expr.X_add_number;
252b5132
RH
6893 /* FALLTHROUGH */
6894 case M_BLTU_I:
6895 case M_BLTUL_I:
6896 if (mask == M_BLTUL_I)
6897 likely = 1;
6898 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6899 goto do_false;
df58fc94
RS
6900 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6901 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6902 &offset_expr, sreg, ZERO);
6903 else
252b5132 6904 {
df58fc94
RS
6905 used_at = 1;
6906 set_at (sreg, 1);
6907 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6908 &offset_expr, AT, ZERO);
252b5132 6909 }
252b5132
RH
6910 break;
6911
6912 case M_BLTL:
6913 likely = 1;
6914 case M_BLT:
6915 if (treg == 0)
df58fc94
RS
6916 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6917 else if (sreg == 0)
6918 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
6919 else
252b5132 6920 {
df58fc94
RS
6921 used_at = 1;
6922 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6923 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6924 &offset_expr, AT, ZERO);
252b5132 6925 }
252b5132
RH
6926 break;
6927
6928 case M_BLTUL:
6929 likely = 1;
6930 case M_BLTU:
6931 if (treg == 0)
6932 goto do_false;
df58fc94
RS
6933 else if (sreg == 0)
6934 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6935 &offset_expr, ZERO, treg);
6936 else
252b5132 6937 {
df58fc94
RS
6938 used_at = 1;
6939 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6940 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6941 &offset_expr, AT, ZERO);
252b5132 6942 }
252b5132
RH
6943 break;
6944
5f74bc13
CD
6945 case M_DEXT:
6946 {
d5818fca
MR
6947 /* Use unsigned arithmetic. */
6948 addressT pos;
6949 addressT size;
5f74bc13 6950
90ecf173 6951 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
6952 {
6953 as_bad (_("Unsupported large constant"));
6954 pos = size = 1;
6955 }
6956 else
6957 {
d5818fca
MR
6958 pos = imm_expr.X_add_number;
6959 size = imm2_expr.X_add_number;
5f74bc13
CD
6960 }
6961
6962 if (pos > 63)
6963 {
d5818fca 6964 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
6965 pos = 1;
6966 }
90ecf173 6967 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
6968 {
6969 as_bad (_("Improper extract size (%lu, position %lu)"),
d5818fca 6970 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
6971 size = 1;
6972 }
6973
6974 if (size <= 32 && pos < 32)
6975 {
6976 s = "dext";
6977 fmt = "t,r,+A,+C";
6978 }
6979 else if (size <= 32)
6980 {
6981 s = "dextu";
6982 fmt = "t,r,+E,+H";
6983 }
6984 else
6985 {
6986 s = "dextm";
6987 fmt = "t,r,+A,+G";
6988 }
d5818fca
MR
6989 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6990 (int) (size - 1));
5f74bc13 6991 }
8fc2e39e 6992 break;
5f74bc13
CD
6993
6994 case M_DINS:
6995 {
d5818fca
MR
6996 /* Use unsigned arithmetic. */
6997 addressT pos;
6998 addressT size;
5f74bc13 6999
90ecf173 7000 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
7001 {
7002 as_bad (_("Unsupported large constant"));
7003 pos = size = 1;
7004 }
7005 else
7006 {
d5818fca
MR
7007 pos = imm_expr.X_add_number;
7008 size = imm2_expr.X_add_number;
5f74bc13
CD
7009 }
7010
7011 if (pos > 63)
7012 {
d5818fca 7013 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
7014 pos = 1;
7015 }
90ecf173 7016 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
7017 {
7018 as_bad (_("Improper insert size (%lu, position %lu)"),
d5818fca 7019 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
7020 size = 1;
7021 }
7022
7023 if (pos < 32 && (pos + size - 1) < 32)
7024 {
7025 s = "dins";
7026 fmt = "t,r,+A,+B";
7027 }
7028 else if (pos >= 32)
7029 {
7030 s = "dinsu";
7031 fmt = "t,r,+E,+F";
7032 }
7033 else
7034 {
7035 s = "dinsm";
7036 fmt = "t,r,+A,+F";
7037 }
750bdd57
AS
7038 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
7039 (int) (pos + size - 1));
5f74bc13 7040 }
8fc2e39e 7041 break;
5f74bc13 7042
252b5132
RH
7043 case M_DDIV_3:
7044 dbl = 1;
7045 case M_DIV_3:
7046 s = "mflo";
7047 goto do_div3;
7048 case M_DREM_3:
7049 dbl = 1;
7050 case M_REM_3:
7051 s = "mfhi";
7052 do_div3:
7053 if (treg == 0)
7054 {
7055 as_warn (_("Divide by zero."));
7056 if (mips_trap)
df58fc94 7057 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 7058 else
df58fc94 7059 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 7060 break;
252b5132
RH
7061 }
7062
7d10b47d 7063 start_noreorder ();
252b5132
RH
7064 if (mips_trap)
7065 {
df58fc94 7066 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 7067 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
7068 }
7069 else
7070 {
df58fc94
RS
7071 if (mips_opts.micromips)
7072 micromips_label_expr (&label_expr);
7073 else
7074 label_expr.X_add_number = 8;
7075 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 7076 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
df58fc94
RS
7077 macro_build (NULL, "break", BRK_FMT, 7);
7078 if (mips_opts.micromips)
7079 micromips_add_label ();
252b5132
RH
7080 }
7081 expr1.X_add_number = -1;
8fc2e39e 7082 used_at = 1;
f6a22291 7083 load_register (AT, &expr1, dbl);
df58fc94
RS
7084 if (mips_opts.micromips)
7085 micromips_label_expr (&label_expr);
7086 else
7087 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
7088 macro_build (&label_expr, "bne", "s,t,p", treg, AT);
252b5132
RH
7089 if (dbl)
7090 {
7091 expr1.X_add_number = 1;
f6a22291 7092 load_register (AT, &expr1, dbl);
df58fc94 7093 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
7094 }
7095 else
7096 {
7097 expr1.X_add_number = 0x80000000;
df58fc94 7098 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
7099 }
7100 if (mips_trap)
7101 {
df58fc94 7102 macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
252b5132
RH
7103 /* We want to close the noreorder block as soon as possible, so
7104 that later insns are available for delay slot filling. */
7d10b47d 7105 end_noreorder ();
252b5132
RH
7106 }
7107 else
7108 {
df58fc94
RS
7109 if (mips_opts.micromips)
7110 micromips_label_expr (&label_expr);
7111 else
7112 label_expr.X_add_number = 8;
7113 macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
a605d2b3 7114 macro_build (NULL, "nop", "");
252b5132
RH
7115
7116 /* We want to close the noreorder block as soon as possible, so
7117 that later insns are available for delay slot filling. */
7d10b47d 7118 end_noreorder ();
252b5132 7119
df58fc94 7120 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 7121 }
df58fc94
RS
7122 if (mips_opts.micromips)
7123 micromips_add_label ();
7124 macro_build (NULL, s, MFHL_FMT, dreg);
252b5132
RH
7125 break;
7126
7127 case M_DIV_3I:
7128 s = "div";
7129 s2 = "mflo";
7130 goto do_divi;
7131 case M_DIVU_3I:
7132 s = "divu";
7133 s2 = "mflo";
7134 goto do_divi;
7135 case M_REM_3I:
7136 s = "div";
7137 s2 = "mfhi";
7138 goto do_divi;
7139 case M_REMU_3I:
7140 s = "divu";
7141 s2 = "mfhi";
7142 goto do_divi;
7143 case M_DDIV_3I:
7144 dbl = 1;
7145 s = "ddiv";
7146 s2 = "mflo";
7147 goto do_divi;
7148 case M_DDIVU_3I:
7149 dbl = 1;
7150 s = "ddivu";
7151 s2 = "mflo";
7152 goto do_divi;
7153 case M_DREM_3I:
7154 dbl = 1;
7155 s = "ddiv";
7156 s2 = "mfhi";
7157 goto do_divi;
7158 case M_DREMU_3I:
7159 dbl = 1;
7160 s = "ddivu";
7161 s2 = "mfhi";
7162 do_divi:
7163 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7164 {
7165 as_warn (_("Divide by zero."));
7166 if (mips_trap)
df58fc94 7167 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 7168 else
df58fc94 7169 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 7170 break;
252b5132
RH
7171 }
7172 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7173 {
7174 if (strcmp (s2, "mflo") == 0)
67c0d1eb 7175 move_register (dreg, sreg);
252b5132 7176 else
c80c840e 7177 move_register (dreg, ZERO);
8fc2e39e 7178 break;
252b5132
RH
7179 }
7180 if (imm_expr.X_op == O_constant
7181 && imm_expr.X_add_number == -1
7182 && s[strlen (s) - 1] != 'u')
7183 {
7184 if (strcmp (s2, "mflo") == 0)
7185 {
67c0d1eb 7186 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
7187 }
7188 else
c80c840e 7189 move_register (dreg, ZERO);
8fc2e39e 7190 break;
252b5132
RH
7191 }
7192
8fc2e39e 7193 used_at = 1;
67c0d1eb
RS
7194 load_register (AT, &imm_expr, dbl);
7195 macro_build (NULL, s, "z,s,t", sreg, AT);
df58fc94 7196 macro_build (NULL, s2, MFHL_FMT, dreg);
252b5132
RH
7197 break;
7198
7199 case M_DIVU_3:
7200 s = "divu";
7201 s2 = "mflo";
7202 goto do_divu3;
7203 case M_REMU_3:
7204 s = "divu";
7205 s2 = "mfhi";
7206 goto do_divu3;
7207 case M_DDIVU_3:
7208 s = "ddivu";
7209 s2 = "mflo";
7210 goto do_divu3;
7211 case M_DREMU_3:
7212 s = "ddivu";
7213 s2 = "mfhi";
7214 do_divu3:
7d10b47d 7215 start_noreorder ();
252b5132
RH
7216 if (mips_trap)
7217 {
df58fc94 7218 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 7219 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7220 /* We want to close the noreorder block as soon as possible, so
7221 that later insns are available for delay slot filling. */
7d10b47d 7222 end_noreorder ();
252b5132
RH
7223 }
7224 else
7225 {
df58fc94
RS
7226 if (mips_opts.micromips)
7227 micromips_label_expr (&label_expr);
7228 else
7229 label_expr.X_add_number = 8;
7230 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 7231 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7232
7233 /* We want to close the noreorder block as soon as possible, so
7234 that later insns are available for delay slot filling. */
7d10b47d 7235 end_noreorder ();
df58fc94
RS
7236 macro_build (NULL, "break", BRK_FMT, 7);
7237 if (mips_opts.micromips)
7238 micromips_add_label ();
252b5132 7239 }
df58fc94 7240 macro_build (NULL, s2, MFHL_FMT, dreg);
8fc2e39e 7241 break;
252b5132 7242
1abe91b1
MR
7243 case M_DLCA_AB:
7244 dbl = 1;
7245 case M_LCA_AB:
7246 call = 1;
7247 goto do_la;
252b5132
RH
7248 case M_DLA_AB:
7249 dbl = 1;
7250 case M_LA_AB:
1abe91b1 7251 do_la:
252b5132
RH
7252 /* Load the address of a symbol into a register. If breg is not
7253 zero, we then add a base register to it. */
7254
3bec30a8
TS
7255 if (dbl && HAVE_32BIT_GPRS)
7256 as_warn (_("dla used to load 32-bit register"));
7257
90ecf173 7258 if (!dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
7259 as_warn (_("la used to load 64-bit address"));
7260
0c11417f
MR
7261 if (offset_expr.X_op == O_constant
7262 && offset_expr.X_add_number >= -0x8000
7263 && offset_expr.X_add_number < 0x8000)
7264 {
aed1a261 7265 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 7266 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 7267 break;
0c11417f
MR
7268 }
7269
741fe287 7270 if (mips_opts.at && (treg == breg))
afdbd6d0
CD
7271 {
7272 tempreg = AT;
7273 used_at = 1;
7274 }
7275 else
7276 {
7277 tempreg = treg;
afdbd6d0
CD
7278 }
7279
252b5132
RH
7280 if (offset_expr.X_op != O_symbol
7281 && offset_expr.X_op != O_constant)
7282 {
f71d0d44 7283 as_bad (_("Expression too complex"));
252b5132
RH
7284 offset_expr.X_op = O_constant;
7285 }
7286
252b5132 7287 if (offset_expr.X_op == O_constant)
aed1a261 7288 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
7289 else if (mips_pic == NO_PIC)
7290 {
d6bc6245 7291 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 7292 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
7293 Otherwise we want
7294 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
7295 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7296 If we have a constant, we need two instructions anyhow,
d6bc6245 7297 so we may as well always use the latter form.
76b3015f 7298
6caf9ef4
TS
7299 With 64bit address space and a usable $at we want
7300 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7301 lui $at,<sym> (BFD_RELOC_HI16_S)
7302 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7303 daddiu $at,<sym> (BFD_RELOC_LO16)
7304 dsll32 $tempreg,0
7305 daddu $tempreg,$tempreg,$at
7306
7307 If $at is already in use, we use a path which is suboptimal
7308 on superscalar processors.
7309 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7310 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7311 dsll $tempreg,16
7312 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
7313 dsll $tempreg,16
7314 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
7315
7316 For GP relative symbols in 64bit address space we can use
7317 the same sequence as in 32bit address space. */
aed1a261 7318 if (HAVE_64BIT_SYMBOLS)
252b5132 7319 {
6caf9ef4
TS
7320 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7321 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7322 {
7323 relax_start (offset_expr.X_add_symbol);
7324 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7325 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7326 relax_switch ();
7327 }
d6bc6245 7328
741fe287 7329 if (used_at == 0 && mips_opts.at)
98d3f06f 7330 {
df58fc94 7331 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7332 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 7333 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7334 AT, BFD_RELOC_HI16_S);
67c0d1eb 7335 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7336 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 7337 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7338 AT, AT, BFD_RELOC_LO16);
df58fc94 7339 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 7340 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
7341 used_at = 1;
7342 }
7343 else
7344 {
df58fc94 7345 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7346 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 7347 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7348 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 7349 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7350 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7351 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 7352 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7353 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7354 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 7355 }
6caf9ef4
TS
7356
7357 if (mips_relax.sequence)
7358 relax_end ();
98d3f06f
KH
7359 }
7360 else
7361 {
7362 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 7363 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 7364 {
4d7206a2 7365 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7366 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7367 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 7368 relax_switch ();
98d3f06f 7369 }
6943caf0 7370 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
f71d0d44 7371 as_bad (_("Offset too large"));
67c0d1eb
RS
7372 macro_build_lui (&offset_expr, tempreg);
7373 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7374 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
7375 if (mips_relax.sequence)
7376 relax_end ();
98d3f06f 7377 }
252b5132 7378 }
0a44bf69 7379 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 7380 {
9117d219
NC
7381 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7382
252b5132
RH
7383 /* If this is a reference to an external symbol, and there
7384 is no constant, we want
7385 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 7386 or for lca or if tempreg is PIC_CALL_REG
9117d219 7387 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
7388 For a local symbol, we want
7389 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7390 nop
7391 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7392
7393 If we have a small constant, and this is a reference to
7394 an external symbol, we want
7395 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7396 nop
7397 addiu $tempreg,$tempreg,<constant>
7398 For a local symbol, we want the same instruction
7399 sequence, but we output a BFD_RELOC_LO16 reloc on the
7400 addiu instruction.
7401
7402 If we have a large constant, and this is a reference to
7403 an external symbol, we want
7404 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7405 lui $at,<hiconstant>
7406 addiu $at,$at,<loconstant>
7407 addu $tempreg,$tempreg,$at
7408 For a local symbol, we want the same instruction
7409 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 7410 addiu instruction.
ed6fb7bd
SC
7411 */
7412
4d7206a2 7413 if (offset_expr.X_add_number == 0)
252b5132 7414 {
0a44bf69
RS
7415 if (mips_pic == SVR4_PIC
7416 && breg == 0
7417 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
7418 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7419
7420 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7421 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7422 lw_reloc_type, mips_gp_register);
4d7206a2 7423 if (breg != 0)
252b5132
RH
7424 {
7425 /* We're going to put in an addu instruction using
7426 tempreg, so we may as well insert the nop right
7427 now. */
269137b2 7428 load_delay_nop ();
252b5132 7429 }
4d7206a2 7430 relax_switch ();
67c0d1eb
RS
7431 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7432 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7433 load_delay_nop ();
67c0d1eb
RS
7434 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7435 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 7436 relax_end ();
252b5132
RH
7437 /* FIXME: If breg == 0, and the next instruction uses
7438 $tempreg, then if this variant case is used an extra
7439 nop will be generated. */
7440 }
4d7206a2
RS
7441 else if (offset_expr.X_add_number >= -0x8000
7442 && offset_expr.X_add_number < 0x8000)
252b5132 7443 {
67c0d1eb 7444 load_got_offset (tempreg, &offset_expr);
269137b2 7445 load_delay_nop ();
67c0d1eb 7446 add_got_offset (tempreg, &offset_expr);
252b5132
RH
7447 }
7448 else
7449 {
4d7206a2
RS
7450 expr1.X_add_number = offset_expr.X_add_number;
7451 offset_expr.X_add_number =
43c0598f 7452 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 7453 load_got_offset (tempreg, &offset_expr);
f6a22291 7454 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
7455 /* If we are going to add in a base register, and the
7456 target register and the base register are the same,
7457 then we are using AT as a temporary register. Since
7458 we want to load the constant into AT, we add our
7459 current AT (from the global offset table) and the
7460 register into the register now, and pretend we were
7461 not using a base register. */
67c0d1eb 7462 if (breg == treg)
252b5132 7463 {
269137b2 7464 load_delay_nop ();
67c0d1eb 7465 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7466 treg, AT, breg);
252b5132
RH
7467 breg = 0;
7468 tempreg = treg;
252b5132 7469 }
f6a22291 7470 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
7471 used_at = 1;
7472 }
7473 }
0a44bf69 7474 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 7475 {
67c0d1eb 7476 int add_breg_early = 0;
f5040a92
AO
7477
7478 /* If this is a reference to an external, and there is no
7479 constant, or local symbol (*), with or without a
7480 constant, we want
7481 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 7482 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
7483 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7484
7485 If we have a small constant, and this is a reference to
7486 an external symbol, we want
7487 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7488 addiu $tempreg,$tempreg,<constant>
7489
7490 If we have a large constant, and this is a reference to
7491 an external symbol, we want
7492 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7493 lui $at,<hiconstant>
7494 addiu $at,$at,<loconstant>
7495 addu $tempreg,$tempreg,$at
7496
7497 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7498 local symbols, even though it introduces an additional
7499 instruction. */
7500
f5040a92
AO
7501 if (offset_expr.X_add_number)
7502 {
4d7206a2 7503 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
7504 offset_expr.X_add_number = 0;
7505
4d7206a2 7506 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7507 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7508 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7509
7510 if (expr1.X_add_number >= -0x8000
7511 && expr1.X_add_number < 0x8000)
7512 {
67c0d1eb
RS
7513 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7514 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 7515 }
ecd13cd3 7516 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 7517 {
f5040a92
AO
7518 /* If we are going to add in a base register, and the
7519 target register and the base register are the same,
7520 then we are using AT as a temporary register. Since
7521 we want to load the constant into AT, we add our
7522 current AT (from the global offset table) and the
7523 register into the register now, and pretend we were
7524 not using a base register. */
7525 if (breg != treg)
7526 dreg = tempreg;
7527 else
7528 {
9c2799c2 7529 gas_assert (tempreg == AT);
67c0d1eb
RS
7530 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7531 treg, AT, breg);
f5040a92 7532 dreg = treg;
67c0d1eb 7533 add_breg_early = 1;
f5040a92
AO
7534 }
7535
f6a22291 7536 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7537 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7538 dreg, dreg, AT);
f5040a92 7539
f5040a92
AO
7540 used_at = 1;
7541 }
7542 else
7543 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7544
4d7206a2 7545 relax_switch ();
f5040a92
AO
7546 offset_expr.X_add_number = expr1.X_add_number;
7547
67c0d1eb
RS
7548 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7549 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7550 if (add_breg_early)
f5040a92 7551 {
67c0d1eb 7552 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 7553 treg, tempreg, breg);
f5040a92
AO
7554 breg = 0;
7555 tempreg = treg;
7556 }
4d7206a2 7557 relax_end ();
f5040a92 7558 }
4d7206a2 7559 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 7560 {
4d7206a2 7561 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7562 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7563 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 7564 relax_switch ();
67c0d1eb
RS
7565 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7566 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 7567 relax_end ();
f5040a92 7568 }
4d7206a2 7569 else
f5040a92 7570 {
67c0d1eb
RS
7571 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7572 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7573 }
7574 }
0a44bf69 7575 else if (mips_big_got && !HAVE_NEWABI)
252b5132 7576 {
67c0d1eb 7577 int gpdelay;
9117d219
NC
7578 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7579 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 7580 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
7581
7582 /* This is the large GOT case. If this is a reference to an
7583 external symbol, and there is no constant, we want
7584 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7585 addu $tempreg,$tempreg,$gp
7586 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 7587 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
7588 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7589 addu $tempreg,$tempreg,$gp
7590 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
7591 For a local symbol, we want
7592 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7593 nop
7594 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7595
7596 If we have a small constant, and this is a reference to
7597 an external symbol, we want
7598 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7599 addu $tempreg,$tempreg,$gp
7600 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7601 nop
7602 addiu $tempreg,$tempreg,<constant>
7603 For a local symbol, we want
7604 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7605 nop
7606 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7607
7608 If we have a large constant, and this is a reference to
7609 an external symbol, we want
7610 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7611 addu $tempreg,$tempreg,$gp
7612 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7613 lui $at,<hiconstant>
7614 addiu $at,$at,<loconstant>
7615 addu $tempreg,$tempreg,$at
7616 For a local symbol, we want
7617 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7618 lui $at,<hiconstant>
7619 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
7620 addu $tempreg,$tempreg,$at
f5040a92 7621 */
438c16b8 7622
252b5132
RH
7623 expr1.X_add_number = offset_expr.X_add_number;
7624 offset_expr.X_add_number = 0;
4d7206a2 7625 relax_start (offset_expr.X_add_symbol);
67c0d1eb 7626 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
7627 if (expr1.X_add_number == 0 && breg == 0
7628 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
7629 {
7630 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7631 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7632 }
df58fc94 7633 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 7634 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7635 tempreg, tempreg, mips_gp_register);
67c0d1eb 7636 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 7637 tempreg, lw_reloc_type, tempreg);
252b5132
RH
7638 if (expr1.X_add_number == 0)
7639 {
67c0d1eb 7640 if (breg != 0)
252b5132
RH
7641 {
7642 /* We're going to put in an addu instruction using
7643 tempreg, so we may as well insert the nop right
7644 now. */
269137b2 7645 load_delay_nop ();
252b5132 7646 }
252b5132
RH
7647 }
7648 else if (expr1.X_add_number >= -0x8000
7649 && expr1.X_add_number < 0x8000)
7650 {
269137b2 7651 load_delay_nop ();
67c0d1eb 7652 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7653 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
7654 }
7655 else
7656 {
252b5132
RH
7657 /* If we are going to add in a base register, and the
7658 target register and the base register are the same,
7659 then we are using AT as a temporary register. Since
7660 we want to load the constant into AT, we add our
7661 current AT (from the global offset table) and the
7662 register into the register now, and pretend we were
7663 not using a base register. */
7664 if (breg != treg)
67c0d1eb 7665 dreg = tempreg;
252b5132
RH
7666 else
7667 {
9c2799c2 7668 gas_assert (tempreg == AT);
269137b2 7669 load_delay_nop ();
67c0d1eb 7670 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7671 treg, AT, breg);
252b5132 7672 dreg = treg;
252b5132
RH
7673 }
7674
f6a22291 7675 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7676 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 7677
252b5132
RH
7678 used_at = 1;
7679 }
43c0598f 7680 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 7681 relax_switch ();
252b5132 7682
67c0d1eb 7683 if (gpdelay)
252b5132
RH
7684 {
7685 /* This is needed because this instruction uses $gp, but
f5040a92 7686 the first instruction on the main stream does not. */
67c0d1eb 7687 macro_build (NULL, "nop", "");
252b5132 7688 }
ed6fb7bd 7689
67c0d1eb
RS
7690 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7691 local_reloc_type, mips_gp_register);
f5040a92 7692 if (expr1.X_add_number >= -0x8000
252b5132
RH
7693 && expr1.X_add_number < 0x8000)
7694 {
269137b2 7695 load_delay_nop ();
67c0d1eb
RS
7696 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7697 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 7698 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
7699 register, the external symbol case ended with a load,
7700 so if the symbol turns out to not be external, and
7701 the next instruction uses tempreg, an unnecessary nop
7702 will be inserted. */
252b5132
RH
7703 }
7704 else
7705 {
7706 if (breg == treg)
7707 {
7708 /* We must add in the base register now, as in the
f5040a92 7709 external symbol case. */
9c2799c2 7710 gas_assert (tempreg == AT);
269137b2 7711 load_delay_nop ();
67c0d1eb 7712 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7713 treg, AT, breg);
252b5132
RH
7714 tempreg = treg;
7715 /* We set breg to 0 because we have arranged to add
f5040a92 7716 it in in both cases. */
252b5132
RH
7717 breg = 0;
7718 }
7719
67c0d1eb
RS
7720 macro_build_lui (&expr1, AT);
7721 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7722 AT, AT, BFD_RELOC_LO16);
67c0d1eb 7723 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7724 tempreg, tempreg, AT);
8fc2e39e 7725 used_at = 1;
252b5132 7726 }
4d7206a2 7727 relax_end ();
252b5132 7728 }
0a44bf69 7729 else if (mips_big_got && HAVE_NEWABI)
f5040a92 7730 {
f5040a92
AO
7731 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7732 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 7733 int add_breg_early = 0;
f5040a92
AO
7734
7735 /* This is the large GOT case. If this is a reference to an
7736 external symbol, and there is no constant, we want
7737 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7738 add $tempreg,$tempreg,$gp
7739 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 7740 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
7741 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7742 add $tempreg,$tempreg,$gp
7743 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7744
7745 If we have a small constant, and this is a reference to
7746 an external symbol, we want
7747 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7748 add $tempreg,$tempreg,$gp
7749 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7750 addi $tempreg,$tempreg,<constant>
7751
7752 If we have a large constant, and this is a reference to
7753 an external symbol, we want
7754 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7755 addu $tempreg,$tempreg,$gp
7756 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7757 lui $at,<hiconstant>
7758 addi $at,$at,<loconstant>
7759 add $tempreg,$tempreg,$at
7760
7761 If we have NewABI, and we know it's a local symbol, we want
7762 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
7763 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
7764 otherwise we have to resort to GOT_HI16/GOT_LO16. */
7765
4d7206a2 7766 relax_start (offset_expr.X_add_symbol);
f5040a92 7767
4d7206a2 7768 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
7769 offset_expr.X_add_number = 0;
7770
1abe91b1
MR
7771 if (expr1.X_add_number == 0 && breg == 0
7772 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
7773 {
7774 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7775 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7776 }
df58fc94 7777 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 7778 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7779 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
7780 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7781 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
7782
7783 if (expr1.X_add_number == 0)
4d7206a2 7784 ;
f5040a92
AO
7785 else if (expr1.X_add_number >= -0x8000
7786 && expr1.X_add_number < 0x8000)
7787 {
67c0d1eb 7788 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7789 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 7790 }
ecd13cd3 7791 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 7792 {
f5040a92
AO
7793 /* If we are going to add in a base register, and the
7794 target register and the base register are the same,
7795 then we are using AT as a temporary register. Since
7796 we want to load the constant into AT, we add our
7797 current AT (from the global offset table) and the
7798 register into the register now, and pretend we were
7799 not using a base register. */
7800 if (breg != treg)
7801 dreg = tempreg;
7802 else
7803 {
9c2799c2 7804 gas_assert (tempreg == AT);
67c0d1eb 7805 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7806 treg, AT, breg);
f5040a92 7807 dreg = treg;
67c0d1eb 7808 add_breg_early = 1;
f5040a92
AO
7809 }
7810
f6a22291 7811 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7812 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 7813
f5040a92
AO
7814 used_at = 1;
7815 }
7816 else
7817 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7818
4d7206a2 7819 relax_switch ();
f5040a92 7820 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
7821 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7822 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
7823 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
7824 tempreg, BFD_RELOC_MIPS_GOT_OFST);
7825 if (add_breg_early)
f5040a92 7826 {
67c0d1eb 7827 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7828 treg, tempreg, breg);
f5040a92
AO
7829 breg = 0;
7830 tempreg = treg;
7831 }
4d7206a2 7832 relax_end ();
f5040a92 7833 }
252b5132
RH
7834 else
7835 abort ();
7836
7837 if (breg != 0)
aed1a261 7838 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
7839 break;
7840
52b6b6b9 7841 case M_MSGSND:
df58fc94 7842 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7843 {
7844 unsigned long temp = (treg << 16) | (0x01);
7845 macro_build (NULL, "c2", "C", temp);
7846 }
c7af4273 7847 break;
52b6b6b9
JM
7848
7849 case M_MSGLD:
df58fc94 7850 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7851 {
7852 unsigned long temp = (0x02);
7853 macro_build (NULL, "c2", "C", temp);
7854 }
c7af4273 7855 break;
52b6b6b9
JM
7856
7857 case M_MSGLD_T:
df58fc94 7858 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7859 {
7860 unsigned long temp = (treg << 16) | (0x02);
7861 macro_build (NULL, "c2", "C", temp);
7862 }
c7af4273 7863 break;
52b6b6b9
JM
7864
7865 case M_MSGWAIT:
df58fc94 7866 gas_assert (!mips_opts.micromips);
52b6b6b9 7867 macro_build (NULL, "c2", "C", 3);
c7af4273 7868 break;
52b6b6b9
JM
7869
7870 case M_MSGWAIT_T:
df58fc94 7871 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7872 {
7873 unsigned long temp = (treg << 16) | 0x03;
7874 macro_build (NULL, "c2", "C", temp);
7875 }
c7af4273 7876 break;
52b6b6b9 7877
252b5132
RH
7878 case M_J_A:
7879 /* The j instruction may not be used in PIC code, since it
7880 requires an absolute address. We convert it to a b
7881 instruction. */
7882 if (mips_pic == NO_PIC)
67c0d1eb 7883 macro_build (&offset_expr, "j", "a");
252b5132 7884 else
67c0d1eb 7885 macro_build (&offset_expr, "b", "p");
8fc2e39e 7886 break;
252b5132
RH
7887
7888 /* The jal instructions must be handled as macros because when
7889 generating PIC code they expand to multi-instruction
7890 sequences. Normally they are simple instructions. */
df58fc94
RS
7891 case M_JALS_1:
7892 dreg = RA;
7893 /* Fall through. */
7894 case M_JALS_2:
7895 gas_assert (mips_opts.micromips);
7896 jals = 1;
7897 goto jal;
252b5132
RH
7898 case M_JAL_1:
7899 dreg = RA;
7900 /* Fall through. */
7901 case M_JAL_2:
df58fc94 7902 jal:
3e722fb5 7903 if (mips_pic == NO_PIC)
df58fc94
RS
7904 {
7905 s = jals ? "jalrs" : "jalr";
e64af278
MR
7906 if (mips_opts.micromips
7907 && dreg == RA
7908 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
7909 macro_build (NULL, s, "mj", sreg);
7910 else
7911 macro_build (NULL, s, JALR_FMT, dreg, sreg);
7912 }
0a44bf69 7913 else
252b5132 7914 {
df58fc94
RS
7915 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
7916 && mips_cprestore_offset >= 0);
7917
252b5132
RH
7918 if (sreg != PIC_CALL_REG)
7919 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 7920
df58fc94
RS
7921 s = (mips_opts.micromips && (!mips_opts.noreorder || cprestore)
7922 ? "jalrs" : "jalr");
e64af278
MR
7923 if (mips_opts.micromips
7924 && dreg == RA
7925 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
7926 macro_build (NULL, s, "mj", sreg);
7927 else
7928 macro_build (NULL, s, JALR_FMT, dreg, sreg);
0a44bf69 7929 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 7930 {
6478892d
TS
7931 if (mips_cprestore_offset < 0)
7932 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7933 else
7934 {
90ecf173 7935 if (!mips_frame_reg_valid)
7a621144
DJ
7936 {
7937 as_warn (_("No .frame pseudo-op used in PIC code"));
7938 /* Quiet this warning. */
7939 mips_frame_reg_valid = 1;
7940 }
90ecf173 7941 if (!mips_cprestore_valid)
7a621144
DJ
7942 {
7943 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7944 /* Quiet this warning. */
7945 mips_cprestore_valid = 1;
7946 }
d3fca0b5
MR
7947 if (mips_opts.noreorder)
7948 macro_build (NULL, "nop", "");
6478892d 7949 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 7950 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 7951 mips_gp_register,
256ab948
TS
7952 mips_frame_reg,
7953 HAVE_64BIT_ADDRESSES);
6478892d 7954 }
252b5132
RH
7955 }
7956 }
252b5132 7957
8fc2e39e 7958 break;
252b5132 7959
df58fc94
RS
7960 case M_JALS_A:
7961 gas_assert (mips_opts.micromips);
7962 jals = 1;
7963 /* Fall through. */
252b5132
RH
7964 case M_JAL_A:
7965 if (mips_pic == NO_PIC)
df58fc94 7966 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
7967 else if (mips_pic == SVR4_PIC)
7968 {
7969 /* If this is a reference to an external symbol, and we are
7970 using a small GOT, we want
7971 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7972 nop
f9419b05 7973 jalr $ra,$25
252b5132
RH
7974 nop
7975 lw $gp,cprestore($sp)
7976 The cprestore value is set using the .cprestore
7977 pseudo-op. If we are using a big GOT, we want
7978 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7979 addu $25,$25,$gp
7980 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
7981 nop
f9419b05 7982 jalr $ra,$25
252b5132
RH
7983 nop
7984 lw $gp,cprestore($sp)
7985 If the symbol is not external, we want
7986 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7987 nop
7988 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 7989 jalr $ra,$25
252b5132 7990 nop
438c16b8 7991 lw $gp,cprestore($sp)
f5040a92
AO
7992
7993 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
7994 sequences above, minus nops, unless the symbol is local,
7995 which enables us to use GOT_PAGE/GOT_OFST (big got) or
7996 GOT_DISP. */
438c16b8 7997 if (HAVE_NEWABI)
252b5132 7998 {
90ecf173 7999 if (!mips_big_got)
f5040a92 8000 {
4d7206a2 8001 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
8002 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8003 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 8004 mips_gp_register);
4d7206a2 8005 relax_switch ();
67c0d1eb
RS
8006 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8007 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
8008 mips_gp_register);
8009 relax_end ();
f5040a92
AO
8010 }
8011 else
8012 {
4d7206a2 8013 relax_start (offset_expr.X_add_symbol);
df58fc94 8014 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
8015 BFD_RELOC_MIPS_CALL_HI16);
8016 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8017 PIC_CALL_REG, mips_gp_register);
8018 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8019 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8020 PIC_CALL_REG);
4d7206a2 8021 relax_switch ();
67c0d1eb
RS
8022 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8023 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
8024 mips_gp_register);
8025 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8026 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 8027 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 8028 relax_end ();
f5040a92 8029 }
684022ea 8030
df58fc94 8031 macro_build_jalr (&offset_expr, 0);
252b5132
RH
8032 }
8033 else
8034 {
4d7206a2 8035 relax_start (offset_expr.X_add_symbol);
90ecf173 8036 if (!mips_big_got)
438c16b8 8037 {
67c0d1eb
RS
8038 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8039 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 8040 mips_gp_register);
269137b2 8041 load_delay_nop ();
4d7206a2 8042 relax_switch ();
438c16b8 8043 }
252b5132 8044 else
252b5132 8045 {
67c0d1eb
RS
8046 int gpdelay;
8047
8048 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 8049 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
8050 BFD_RELOC_MIPS_CALL_HI16);
8051 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8052 PIC_CALL_REG, mips_gp_register);
8053 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8054 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8055 PIC_CALL_REG);
269137b2 8056 load_delay_nop ();
4d7206a2 8057 relax_switch ();
67c0d1eb
RS
8058 if (gpdelay)
8059 macro_build (NULL, "nop", "");
252b5132 8060 }
67c0d1eb
RS
8061 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8062 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 8063 mips_gp_register);
269137b2 8064 load_delay_nop ();
67c0d1eb
RS
8065 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8066 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 8067 relax_end ();
df58fc94 8068 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 8069
6478892d
TS
8070 if (mips_cprestore_offset < 0)
8071 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8072 else
8073 {
90ecf173 8074 if (!mips_frame_reg_valid)
7a621144
DJ
8075 {
8076 as_warn (_("No .frame pseudo-op used in PIC code"));
8077 /* Quiet this warning. */
8078 mips_frame_reg_valid = 1;
8079 }
90ecf173 8080 if (!mips_cprestore_valid)
7a621144
DJ
8081 {
8082 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8083 /* Quiet this warning. */
8084 mips_cprestore_valid = 1;
8085 }
6478892d 8086 if (mips_opts.noreorder)
67c0d1eb 8087 macro_build (NULL, "nop", "");
6478892d 8088 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 8089 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 8090 mips_gp_register,
256ab948
TS
8091 mips_frame_reg,
8092 HAVE_64BIT_ADDRESSES);
6478892d 8093 }
252b5132
RH
8094 }
8095 }
0a44bf69
RS
8096 else if (mips_pic == VXWORKS_PIC)
8097 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
8098 else
8099 abort ();
8100
8fc2e39e 8101 break;
252b5132 8102
dec0624d
MR
8103 case M_ACLR_AB:
8104 ab = 1;
8105 case M_ACLR_OB:
8106 s = "aclr";
8107 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8108 fmt = "\\,~(b)";
8109 off12 = 1;
8110 goto ld_st;
8111 case M_ASET_AB:
8112 ab = 1;
8113 case M_ASET_OB:
8114 s = "aset";
8115 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8116 fmt = "\\,~(b)";
8117 off12 = 1;
8118 goto ld_st;
252b5132 8119 case M_LB_AB:
df58fc94 8120 ab = 1;
252b5132 8121 s = "lb";
df58fc94 8122 fmt = "t,o(b)";
252b5132
RH
8123 goto ld;
8124 case M_LBU_AB:
df58fc94 8125 ab = 1;
252b5132 8126 s = "lbu";
df58fc94 8127 fmt = "t,o(b)";
252b5132
RH
8128 goto ld;
8129 case M_LH_AB:
df58fc94 8130 ab = 1;
252b5132 8131 s = "lh";
df58fc94 8132 fmt = "t,o(b)";
252b5132
RH
8133 goto ld;
8134 case M_LHU_AB:
df58fc94 8135 ab = 1;
252b5132 8136 s = "lhu";
df58fc94 8137 fmt = "t,o(b)";
252b5132
RH
8138 goto ld;
8139 case M_LW_AB:
df58fc94 8140 ab = 1;
252b5132 8141 s = "lw";
df58fc94 8142 fmt = "t,o(b)";
252b5132
RH
8143 goto ld;
8144 case M_LWC0_AB:
df58fc94
RS
8145 ab = 1;
8146 gas_assert (!mips_opts.micromips);
252b5132 8147 s = "lwc0";
df58fc94 8148 fmt = "E,o(b)";
bdaaa2e1 8149 /* Itbl support may require additional care here. */
252b5132 8150 coproc = 1;
df58fc94 8151 goto ld_st;
252b5132 8152 case M_LWC1_AB:
df58fc94 8153 ab = 1;
252b5132 8154 s = "lwc1";
df58fc94 8155 fmt = "T,o(b)";
bdaaa2e1 8156 /* Itbl support may require additional care here. */
252b5132 8157 coproc = 1;
df58fc94 8158 goto ld_st;
252b5132 8159 case M_LWC2_AB:
df58fc94
RS
8160 ab = 1;
8161 case M_LWC2_OB:
252b5132 8162 s = "lwc2";
df58fc94
RS
8163 fmt = COP12_FMT;
8164 off12 = mips_opts.micromips;
bdaaa2e1 8165 /* Itbl support may require additional care here. */
252b5132 8166 coproc = 1;
df58fc94 8167 goto ld_st;
252b5132 8168 case M_LWC3_AB:
df58fc94
RS
8169 ab = 1;
8170 gas_assert (!mips_opts.micromips);
252b5132 8171 s = "lwc3";
df58fc94 8172 fmt = "E,o(b)";
bdaaa2e1 8173 /* Itbl support may require additional care here. */
252b5132 8174 coproc = 1;
df58fc94 8175 goto ld_st;
252b5132 8176 case M_LWL_AB:
df58fc94
RS
8177 ab = 1;
8178 case M_LWL_OB:
252b5132 8179 s = "lwl";
df58fc94
RS
8180 fmt = MEM12_FMT;
8181 off12 = mips_opts.micromips;
8182 goto ld_st;
252b5132 8183 case M_LWR_AB:
df58fc94
RS
8184 ab = 1;
8185 case M_LWR_OB:
252b5132 8186 s = "lwr";
df58fc94
RS
8187 fmt = MEM12_FMT;
8188 off12 = mips_opts.micromips;
8189 goto ld_st;
252b5132 8190 case M_LDC1_AB:
df58fc94 8191 ab = 1;
252b5132 8192 s = "ldc1";
df58fc94 8193 fmt = "T,o(b)";
bdaaa2e1 8194 /* Itbl support may require additional care here. */
252b5132 8195 coproc = 1;
df58fc94 8196 goto ld_st;
252b5132 8197 case M_LDC2_AB:
df58fc94
RS
8198 ab = 1;
8199 case M_LDC2_OB:
252b5132 8200 s = "ldc2";
df58fc94
RS
8201 fmt = COP12_FMT;
8202 off12 = mips_opts.micromips;
bdaaa2e1 8203 /* Itbl support may require additional care here. */
252b5132 8204 coproc = 1;
df58fc94 8205 goto ld_st;
c77c0862
RS
8206 case M_LQC2_AB:
8207 ab = 1;
8208 s = "lqc2";
8209 fmt = "E,o(b)";
8210 /* Itbl support may require additional care here. */
8211 coproc = 1;
8212 goto ld_st;
252b5132 8213 case M_LDC3_AB:
df58fc94 8214 ab = 1;
252b5132 8215 s = "ldc3";
df58fc94 8216 fmt = "E,o(b)";
bdaaa2e1 8217 /* Itbl support may require additional care here. */
252b5132 8218 coproc = 1;
df58fc94 8219 goto ld_st;
252b5132 8220 case M_LDL_AB:
df58fc94
RS
8221 ab = 1;
8222 case M_LDL_OB:
252b5132 8223 s = "ldl";
df58fc94
RS
8224 fmt = MEM12_FMT;
8225 off12 = mips_opts.micromips;
8226 goto ld_st;
252b5132 8227 case M_LDR_AB:
df58fc94
RS
8228 ab = 1;
8229 case M_LDR_OB:
252b5132 8230 s = "ldr";
df58fc94
RS
8231 fmt = MEM12_FMT;
8232 off12 = mips_opts.micromips;
8233 goto ld_st;
252b5132 8234 case M_LL_AB:
df58fc94
RS
8235 ab = 1;
8236 case M_LL_OB:
252b5132 8237 s = "ll";
df58fc94
RS
8238 fmt = MEM12_FMT;
8239 off12 = mips_opts.micromips;
252b5132
RH
8240 goto ld;
8241 case M_LLD_AB:
df58fc94
RS
8242 ab = 1;
8243 case M_LLD_OB:
252b5132 8244 s = "lld";
df58fc94
RS
8245 fmt = MEM12_FMT;
8246 off12 = mips_opts.micromips;
252b5132
RH
8247 goto ld;
8248 case M_LWU_AB:
df58fc94
RS
8249 ab = 1;
8250 case M_LWU_OB:
252b5132 8251 s = "lwu";
df58fc94
RS
8252 fmt = MEM12_FMT;
8253 off12 = mips_opts.micromips;
8254 goto ld;
8255 case M_LWP_AB:
8256 ab = 1;
8257 case M_LWP_OB:
8258 gas_assert (mips_opts.micromips);
8259 s = "lwp";
8260 fmt = "t,~(b)";
8261 off12 = 1;
8262 lp = 1;
8263 goto ld;
8264 case M_LDP_AB:
8265 ab = 1;
8266 case M_LDP_OB:
8267 gas_assert (mips_opts.micromips);
8268 s = "ldp";
8269 fmt = "t,~(b)";
8270 off12 = 1;
8271 lp = 1;
8272 goto ld;
8273 case M_LWM_AB:
8274 ab = 1;
8275 case M_LWM_OB:
8276 gas_assert (mips_opts.micromips);
8277 s = "lwm";
8278 fmt = "n,~(b)";
8279 off12 = 1;
8280 goto ld_st;
8281 case M_LDM_AB:
8282 ab = 1;
8283 case M_LDM_OB:
8284 gas_assert (mips_opts.micromips);
8285 s = "ldm";
8286 fmt = "n,~(b)";
8287 off12 = 1;
8288 goto ld_st;
8289
252b5132 8290 ld:
f19ccbda
MR
8291 /* We don't want to use $0 as tempreg. */
8292 if (breg == treg + lp || treg + lp == ZERO)
df58fc94 8293 goto ld_st;
252b5132 8294 else
df58fc94
RS
8295 tempreg = treg + lp;
8296 goto ld_noat;
8297
252b5132 8298 case M_SB_AB:
df58fc94 8299 ab = 1;
252b5132 8300 s = "sb";
df58fc94
RS
8301 fmt = "t,o(b)";
8302 goto ld_st;
252b5132 8303 case M_SH_AB:
df58fc94 8304 ab = 1;
252b5132 8305 s = "sh";
df58fc94
RS
8306 fmt = "t,o(b)";
8307 goto ld_st;
252b5132 8308 case M_SW_AB:
df58fc94 8309 ab = 1;
252b5132 8310 s = "sw";
df58fc94
RS
8311 fmt = "t,o(b)";
8312 goto ld_st;
252b5132 8313 case M_SWC0_AB:
df58fc94
RS
8314 ab = 1;
8315 gas_assert (!mips_opts.micromips);
252b5132 8316 s = "swc0";
df58fc94 8317 fmt = "E,o(b)";
bdaaa2e1 8318 /* Itbl support may require additional care here. */
252b5132 8319 coproc = 1;
df58fc94 8320 goto ld_st;
252b5132 8321 case M_SWC1_AB:
df58fc94 8322 ab = 1;
252b5132 8323 s = "swc1";
df58fc94 8324 fmt = "T,o(b)";
bdaaa2e1 8325 /* Itbl support may require additional care here. */
252b5132 8326 coproc = 1;
df58fc94 8327 goto ld_st;
252b5132 8328 case M_SWC2_AB:
df58fc94
RS
8329 ab = 1;
8330 case M_SWC2_OB:
252b5132 8331 s = "swc2";
df58fc94
RS
8332 fmt = COP12_FMT;
8333 off12 = mips_opts.micromips;
bdaaa2e1 8334 /* Itbl support may require additional care here. */
252b5132 8335 coproc = 1;
df58fc94 8336 goto ld_st;
252b5132 8337 case M_SWC3_AB:
df58fc94
RS
8338 ab = 1;
8339 gas_assert (!mips_opts.micromips);
252b5132 8340 s = "swc3";
df58fc94 8341 fmt = "E,o(b)";
bdaaa2e1 8342 /* Itbl support may require additional care here. */
252b5132 8343 coproc = 1;
df58fc94 8344 goto ld_st;
252b5132 8345 case M_SWL_AB:
df58fc94
RS
8346 ab = 1;
8347 case M_SWL_OB:
252b5132 8348 s = "swl";
df58fc94
RS
8349 fmt = MEM12_FMT;
8350 off12 = mips_opts.micromips;
8351 goto ld_st;
252b5132 8352 case M_SWR_AB:
df58fc94
RS
8353 ab = 1;
8354 case M_SWR_OB:
252b5132 8355 s = "swr";
df58fc94
RS
8356 fmt = MEM12_FMT;
8357 off12 = mips_opts.micromips;
8358 goto ld_st;
252b5132 8359 case M_SC_AB:
df58fc94
RS
8360 ab = 1;
8361 case M_SC_OB:
252b5132 8362 s = "sc";
df58fc94
RS
8363 fmt = MEM12_FMT;
8364 off12 = mips_opts.micromips;
8365 goto ld_st;
252b5132 8366 case M_SCD_AB:
df58fc94
RS
8367 ab = 1;
8368 case M_SCD_OB:
252b5132 8369 s = "scd";
df58fc94
RS
8370 fmt = MEM12_FMT;
8371 off12 = mips_opts.micromips;
8372 goto ld_st;
d43b4baf 8373 case M_CACHE_AB:
df58fc94
RS
8374 ab = 1;
8375 case M_CACHE_OB:
d43b4baf 8376 s = "cache";
df58fc94
RS
8377 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
8378 off12 = mips_opts.micromips;
8379 goto ld_st;
3eebd5eb 8380 case M_PREF_AB:
df58fc94
RS
8381 ab = 1;
8382 case M_PREF_OB:
3eebd5eb 8383 s = "pref";
df58fc94
RS
8384 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
8385 off12 = mips_opts.micromips;
8386 goto ld_st;
252b5132 8387 case M_SDC1_AB:
df58fc94 8388 ab = 1;
252b5132 8389 s = "sdc1";
df58fc94 8390 fmt = "T,o(b)";
252b5132 8391 coproc = 1;
bdaaa2e1 8392 /* Itbl support may require additional care here. */
df58fc94 8393 goto ld_st;
252b5132 8394 case M_SDC2_AB:
df58fc94
RS
8395 ab = 1;
8396 case M_SDC2_OB:
252b5132 8397 s = "sdc2";
df58fc94
RS
8398 fmt = COP12_FMT;
8399 off12 = mips_opts.micromips;
c77c0862
RS
8400 /* Itbl support may require additional care here. */
8401 coproc = 1;
8402 goto ld_st;
8403 case M_SQC2_AB:
8404 ab = 1;
8405 s = "sqc2";
8406 fmt = "E,o(b)";
bdaaa2e1 8407 /* Itbl support may require additional care here. */
252b5132 8408 coproc = 1;
df58fc94 8409 goto ld_st;
252b5132 8410 case M_SDC3_AB:
df58fc94
RS
8411 ab = 1;
8412 gas_assert (!mips_opts.micromips);
252b5132 8413 s = "sdc3";
df58fc94 8414 fmt = "E,o(b)";
bdaaa2e1 8415 /* Itbl support may require additional care here. */
252b5132 8416 coproc = 1;
df58fc94 8417 goto ld_st;
252b5132 8418 case M_SDL_AB:
df58fc94
RS
8419 ab = 1;
8420 case M_SDL_OB:
252b5132 8421 s = "sdl";
df58fc94
RS
8422 fmt = MEM12_FMT;
8423 off12 = mips_opts.micromips;
8424 goto ld_st;
252b5132 8425 case M_SDR_AB:
df58fc94
RS
8426 ab = 1;
8427 case M_SDR_OB:
252b5132 8428 s = "sdr";
df58fc94
RS
8429 fmt = MEM12_FMT;
8430 off12 = mips_opts.micromips;
8431 goto ld_st;
8432 case M_SWP_AB:
8433 ab = 1;
8434 case M_SWP_OB:
8435 gas_assert (mips_opts.micromips);
8436 s = "swp";
8437 fmt = "t,~(b)";
8438 off12 = 1;
8439 goto ld_st;
8440 case M_SDP_AB:
8441 ab = 1;
8442 case M_SDP_OB:
8443 gas_assert (mips_opts.micromips);
8444 s = "sdp";
8445 fmt = "t,~(b)";
8446 off12 = 1;
8447 goto ld_st;
8448 case M_SWM_AB:
8449 ab = 1;
8450 case M_SWM_OB:
8451 gas_assert (mips_opts.micromips);
8452 s = "swm";
8453 fmt = "n,~(b)";
8454 off12 = 1;
8455 goto ld_st;
8456 case M_SDM_AB:
8457 ab = 1;
8458 case M_SDM_OB:
8459 gas_assert (mips_opts.micromips);
8460 s = "sdm";
8461 fmt = "n,~(b)";
8462 off12 = 1;
8463
8464 ld_st:
8fc2e39e
TS
8465 tempreg = AT;
8466 used_at = 1;
df58fc94 8467 ld_noat:
252b5132
RH
8468 if (offset_expr.X_op != O_constant
8469 && offset_expr.X_op != O_symbol)
8470 {
f71d0d44 8471 as_bad (_("Expression too complex"));
252b5132
RH
8472 offset_expr.X_op = O_constant;
8473 }
8474
2051e8c4
MR
8475 if (HAVE_32BIT_ADDRESSES
8476 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
8477 {
8478 char value [32];
8479
8480 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 8481 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 8482 }
2051e8c4 8483
252b5132
RH
8484 /* A constant expression in PIC code can be handled just as it
8485 is in non PIC code. */
aed1a261
RS
8486 if (offset_expr.X_op == O_constant)
8487 {
df58fc94
RS
8488 int hipart = 0;
8489
842f8b2a 8490 expr1.X_add_number = offset_expr.X_add_number;
2051e8c4 8491 normalize_address_expr (&expr1);
df58fc94 8492 if (!off12 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
842f8b2a
MR
8493 {
8494 expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8495 & ~(bfd_vma) 0xffff);
df58fc94
RS
8496 hipart = 1;
8497 }
8498 else if (off12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
8499 {
8500 expr1.X_add_number = ((expr1.X_add_number + 0x800)
8501 & ~(bfd_vma) 0xfff);
8502 hipart = 1;
8503 }
8504 if (hipart)
8505 {
842f8b2a
MR
8506 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8507 if (breg != 0)
8508 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8509 tempreg, tempreg, breg);
8510 breg = tempreg;
8511 }
dd6a37e7
AP
8512 if (off0)
8513 {
8514 if (offset_expr.X_add_number == 0)
8515 tempreg = breg;
8516 else
8517 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8518 "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8519 macro_build (NULL, s, fmt, treg, tempreg);
8520 }
8521 else if (!off12)
df58fc94
RS
8522 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8523 else
8524 macro_build (NULL, s, fmt,
8525 treg, (unsigned long) offset_expr.X_add_number, breg);
8526 }
dd6a37e7 8527 else if (off12 || off0)
df58fc94 8528 {
dd6a37e7
AP
8529 /* A 12-bit or 0-bit offset field is too narrow to be used
8530 for a low-part relocation, so load the whole address into
8531 the auxillary register. In the case of "A(b)" addresses,
8532 we first load absolute address "A" into the register and
8533 then add base register "b". In the case of "o(b)" addresses,
8534 we simply need to add 16-bit offset "o" to base register "b", and
df58fc94
RS
8535 offset_reloc already contains the relocations associated
8536 with "o". */
8537 if (ab)
8538 {
8539 load_address (tempreg, &offset_expr, &used_at);
8540 if (breg != 0)
8541 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8542 tempreg, tempreg, breg);
8543 }
8544 else
8545 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8546 tempreg, breg, -1,
8547 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8548 expr1.X_add_number = 0;
dd6a37e7
AP
8549 if (off0)
8550 macro_build (NULL, s, fmt, treg, tempreg);
8551 else
8552 macro_build (NULL, s, fmt,
8553 treg, (unsigned long) expr1.X_add_number, tempreg);
aed1a261
RS
8554 }
8555 else if (mips_pic == NO_PIC)
252b5132
RH
8556 {
8557 /* If this is a reference to a GP relative symbol, and there
8558 is no base register, we want
cdf6fd85 8559 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
8560 Otherwise, if there is no base register, we want
8561 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8562 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8563 If we have a constant, we need two instructions anyhow,
8564 so we always use the latter form.
8565
8566 If we have a base register, and this is a reference to a
8567 GP relative symbol, we want
8568 addu $tempreg,$breg,$gp
cdf6fd85 8569 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
8570 Otherwise we want
8571 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8572 addu $tempreg,$tempreg,$breg
8573 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 8574 With a constant we always use the latter case.
76b3015f 8575
d6bc6245
TS
8576 With 64bit address space and no base register and $at usable,
8577 we want
8578 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8579 lui $at,<sym> (BFD_RELOC_HI16_S)
8580 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8581 dsll32 $tempreg,0
8582 daddu $tempreg,$at
8583 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8584 If we have a base register, we want
8585 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8586 lui $at,<sym> (BFD_RELOC_HI16_S)
8587 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8588 daddu $at,$breg
8589 dsll32 $tempreg,0
8590 daddu $tempreg,$at
8591 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8592
8593 Without $at we can't generate the optimal path for superscalar
8594 processors here since this would require two temporary registers.
8595 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8596 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8597 dsll $tempreg,16
8598 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8599 dsll $tempreg,16
8600 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8601 If we have a base register, we want
8602 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8603 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8604 dsll $tempreg,16
8605 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8606 dsll $tempreg,16
8607 daddu $tempreg,$tempreg,$breg
8608 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 8609
6caf9ef4 8610 For GP relative symbols in 64bit address space we can use
aed1a261
RS
8611 the same sequence as in 32bit address space. */
8612 if (HAVE_64BIT_SYMBOLS)
d6bc6245 8613 {
aed1a261 8614 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
8615 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8616 {
8617 relax_start (offset_expr.X_add_symbol);
8618 if (breg == 0)
8619 {
8620 macro_build (&offset_expr, s, fmt, treg,
8621 BFD_RELOC_GPREL16, mips_gp_register);
8622 }
8623 else
8624 {
8625 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8626 tempreg, breg, mips_gp_register);
8627 macro_build (&offset_expr, s, fmt, treg,
8628 BFD_RELOC_GPREL16, tempreg);
8629 }
8630 relax_switch ();
8631 }
d6bc6245 8632
741fe287 8633 if (used_at == 0 && mips_opts.at)
d6bc6245 8634 {
df58fc94 8635 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 8636 BFD_RELOC_MIPS_HIGHEST);
df58fc94 8637 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
8638 BFD_RELOC_HI16_S);
8639 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8640 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 8641 if (breg != 0)
67c0d1eb 8642 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 8643 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb
RS
8644 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
8645 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
8646 tempreg);
d6bc6245
TS
8647 used_at = 1;
8648 }
8649 else
8650 {
df58fc94 8651 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
8652 BFD_RELOC_MIPS_HIGHEST);
8653 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8654 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 8655 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
8656 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8657 tempreg, BFD_RELOC_HI16_S);
df58fc94 8658 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 8659 if (breg != 0)
67c0d1eb 8660 macro_build (NULL, "daddu", "d,v,t",
17a2f251 8661 tempreg, tempreg, breg);
67c0d1eb 8662 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8663 BFD_RELOC_LO16, tempreg);
d6bc6245 8664 }
6caf9ef4
TS
8665
8666 if (mips_relax.sequence)
8667 relax_end ();
8fc2e39e 8668 break;
d6bc6245 8669 }
256ab948 8670
252b5132
RH
8671 if (breg == 0)
8672 {
67c0d1eb 8673 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8674 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 8675 {
4d7206a2 8676 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
8677 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
8678 mips_gp_register);
4d7206a2 8679 relax_switch ();
252b5132 8680 }
67c0d1eb
RS
8681 macro_build_lui (&offset_expr, tempreg);
8682 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8683 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
8684 if (mips_relax.sequence)
8685 relax_end ();
252b5132
RH
8686 }
8687 else
8688 {
67c0d1eb 8689 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8690 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 8691 {
4d7206a2 8692 relax_start (offset_expr.X_add_symbol);
67c0d1eb 8693 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8694 tempreg, breg, mips_gp_register);
67c0d1eb 8695 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8696 BFD_RELOC_GPREL16, tempreg);
4d7206a2 8697 relax_switch ();
252b5132 8698 }
67c0d1eb
RS
8699 macro_build_lui (&offset_expr, tempreg);
8700 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8701 tempreg, tempreg, breg);
67c0d1eb 8702 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8703 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
8704 if (mips_relax.sequence)
8705 relax_end ();
252b5132
RH
8706 }
8707 }
0a44bf69 8708 else if (!mips_big_got)
252b5132 8709 {
ed6fb7bd 8710 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 8711
252b5132
RH
8712 /* If this is a reference to an external symbol, we want
8713 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8714 nop
8715 <op> $treg,0($tempreg)
8716 Otherwise we want
8717 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8718 nop
8719 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8720 <op> $treg,0($tempreg)
f5040a92
AO
8721
8722 For NewABI, we want
8723 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8724 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
8725
252b5132
RH
8726 If there is a base register, we add it to $tempreg before
8727 the <op>. If there is a constant, we stick it in the
8728 <op> instruction. We don't handle constants larger than
8729 16 bits, because we have no way to load the upper 16 bits
8730 (actually, we could handle them for the subset of cases
8731 in which we are not using $at). */
9c2799c2 8732 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
8733 if (HAVE_NEWABI)
8734 {
67c0d1eb
RS
8735 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8736 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 8737 if (breg != 0)
67c0d1eb 8738 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8739 tempreg, tempreg, breg);
67c0d1eb 8740 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8741 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
8742 break;
8743 }
252b5132
RH
8744 expr1.X_add_number = offset_expr.X_add_number;
8745 offset_expr.X_add_number = 0;
8746 if (expr1.X_add_number < -0x8000
8747 || expr1.X_add_number >= 0x8000)
8748 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
8749 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8750 lw_reloc_type, mips_gp_register);
269137b2 8751 load_delay_nop ();
4d7206a2
RS
8752 relax_start (offset_expr.X_add_symbol);
8753 relax_switch ();
67c0d1eb
RS
8754 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8755 tempreg, BFD_RELOC_LO16);
4d7206a2 8756 relax_end ();
252b5132 8757 if (breg != 0)
67c0d1eb 8758 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8759 tempreg, tempreg, breg);
67c0d1eb 8760 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 8761 }
0a44bf69 8762 else if (mips_big_got && !HAVE_NEWABI)
252b5132 8763 {
67c0d1eb 8764 int gpdelay;
252b5132
RH
8765
8766 /* If this is a reference to an external symbol, we want
8767 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8768 addu $tempreg,$tempreg,$gp
8769 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8770 <op> $treg,0($tempreg)
8771 Otherwise we want
8772 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8773 nop
8774 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8775 <op> $treg,0($tempreg)
8776 If there is a base register, we add it to $tempreg before
8777 the <op>. If there is a constant, we stick it in the
8778 <op> instruction. We don't handle constants larger than
8779 16 bits, because we have no way to load the upper 16 bits
8780 (actually, we could handle them for the subset of cases
f5040a92 8781 in which we are not using $at). */
9c2799c2 8782 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
8783 expr1.X_add_number = offset_expr.X_add_number;
8784 offset_expr.X_add_number = 0;
8785 if (expr1.X_add_number < -0x8000
8786 || expr1.X_add_number >= 0x8000)
8787 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 8788 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 8789 relax_start (offset_expr.X_add_symbol);
df58fc94 8790 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 8791 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8792 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8793 mips_gp_register);
8794 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8795 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 8796 relax_switch ();
67c0d1eb
RS
8797 if (gpdelay)
8798 macro_build (NULL, "nop", "");
8799 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8800 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 8801 load_delay_nop ();
67c0d1eb
RS
8802 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8803 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
8804 relax_end ();
8805
252b5132 8806 if (breg != 0)
67c0d1eb 8807 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8808 tempreg, tempreg, breg);
67c0d1eb 8809 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 8810 }
0a44bf69 8811 else if (mips_big_got && HAVE_NEWABI)
f5040a92 8812 {
f5040a92
AO
8813 /* If this is a reference to an external symbol, we want
8814 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8815 add $tempreg,$tempreg,$gp
8816 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8817 <op> $treg,<ofst>($tempreg)
8818 Otherwise, for local symbols, we want:
8819 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8820 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 8821 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 8822 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
8823 offset_expr.X_add_number = 0;
8824 if (expr1.X_add_number < -0x8000
8825 || expr1.X_add_number >= 0x8000)
8826 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 8827 relax_start (offset_expr.X_add_symbol);
df58fc94 8828 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 8829 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8830 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8831 mips_gp_register);
8832 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8833 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 8834 if (breg != 0)
67c0d1eb 8835 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8836 tempreg, tempreg, breg);
67c0d1eb 8837 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 8838
4d7206a2 8839 relax_switch ();
f5040a92 8840 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
8841 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8842 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 8843 if (breg != 0)
67c0d1eb 8844 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8845 tempreg, tempreg, breg);
67c0d1eb 8846 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8847 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 8848 relax_end ();
f5040a92 8849 }
252b5132
RH
8850 else
8851 abort ();
8852
252b5132
RH
8853 break;
8854
8855 case M_LI:
8856 case M_LI_S:
67c0d1eb 8857 load_register (treg, &imm_expr, 0);
8fc2e39e 8858 break;
252b5132
RH
8859
8860 case M_DLI:
67c0d1eb 8861 load_register (treg, &imm_expr, 1);
8fc2e39e 8862 break;
252b5132
RH
8863
8864 case M_LI_SS:
8865 if (imm_expr.X_op == O_constant)
8866 {
8fc2e39e 8867 used_at = 1;
67c0d1eb
RS
8868 load_register (AT, &imm_expr, 0);
8869 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
8870 break;
8871 }
8872 else
8873 {
9c2799c2 8874 gas_assert (offset_expr.X_op == O_symbol
90ecf173
MR
8875 && strcmp (segment_name (S_GET_SEGMENT
8876 (offset_expr.X_add_symbol)),
8877 ".lit4") == 0
8878 && offset_expr.X_add_number == 0);
67c0d1eb 8879 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 8880 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 8881 break;
252b5132
RH
8882 }
8883
8884 case M_LI_D:
ca4e0257
RS
8885 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
8886 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
8887 order 32 bits of the value and the low order 32 bits are either
8888 zero or in OFFSET_EXPR. */
252b5132
RH
8889 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8890 {
ca4e0257 8891 if (HAVE_64BIT_GPRS)
67c0d1eb 8892 load_register (treg, &imm_expr, 1);
252b5132
RH
8893 else
8894 {
8895 int hreg, lreg;
8896
8897 if (target_big_endian)
8898 {
8899 hreg = treg;
8900 lreg = treg + 1;
8901 }
8902 else
8903 {
8904 hreg = treg + 1;
8905 lreg = treg;
8906 }
8907
8908 if (hreg <= 31)
67c0d1eb 8909 load_register (hreg, &imm_expr, 0);
252b5132
RH
8910 if (lreg <= 31)
8911 {
8912 if (offset_expr.X_op == O_absent)
67c0d1eb 8913 move_register (lreg, 0);
252b5132
RH
8914 else
8915 {
9c2799c2 8916 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 8917 load_register (lreg, &offset_expr, 0);
252b5132
RH
8918 }
8919 }
8920 }
8fc2e39e 8921 break;
252b5132
RH
8922 }
8923
8924 /* We know that sym is in the .rdata section. First we get the
8925 upper 16 bits of the address. */
8926 if (mips_pic == NO_PIC)
8927 {
67c0d1eb 8928 macro_build_lui (&offset_expr, AT);
8fc2e39e 8929 used_at = 1;
252b5132 8930 }
0a44bf69 8931 else
252b5132 8932 {
67c0d1eb
RS
8933 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8934 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 8935 used_at = 1;
252b5132 8936 }
bdaaa2e1 8937
252b5132 8938 /* Now we load the register(s). */
ca4e0257 8939 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
8940 {
8941 used_at = 1;
8942 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8943 }
252b5132
RH
8944 else
8945 {
8fc2e39e 8946 used_at = 1;
67c0d1eb 8947 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 8948 if (treg != RA)
252b5132
RH
8949 {
8950 /* FIXME: How in the world do we deal with the possible
8951 overflow here? */
8952 offset_expr.X_add_number += 4;
67c0d1eb 8953 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 8954 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
8955 }
8956 }
252b5132
RH
8957 break;
8958
8959 case M_LI_DD:
ca4e0257
RS
8960 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
8961 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
8962 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
8963 the value and the low order 32 bits are either zero or in
8964 OFFSET_EXPR. */
252b5132
RH
8965 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8966 {
8fc2e39e 8967 used_at = 1;
67c0d1eb 8968 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
8969 if (HAVE_64BIT_FPRS)
8970 {
9c2799c2 8971 gas_assert (HAVE_64BIT_GPRS);
67c0d1eb 8972 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 8973 }
252b5132
RH
8974 else
8975 {
67c0d1eb 8976 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 8977 if (offset_expr.X_op == O_absent)
67c0d1eb 8978 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
8979 else
8980 {
9c2799c2 8981 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
8982 load_register (AT, &offset_expr, 0);
8983 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
8984 }
8985 }
8986 break;
8987 }
8988
9c2799c2 8989 gas_assert (offset_expr.X_op == O_symbol
90ecf173 8990 && offset_expr.X_add_number == 0);
252b5132
RH
8991 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
8992 if (strcmp (s, ".lit8") == 0)
8993 {
0aa27725 8994 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
252b5132 8995 {
67c0d1eb 8996 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 8997 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 8998 break;
252b5132 8999 }
c9914766 9000 breg = mips_gp_register;
252b5132
RH
9001 r = BFD_RELOC_MIPS_LITERAL;
9002 goto dob;
9003 }
9004 else
9005 {
9c2799c2 9006 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 9007 used_at = 1;
0a44bf69 9008 if (mips_pic != NO_PIC)
67c0d1eb
RS
9009 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9010 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
9011 else
9012 {
9013 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 9014 macro_build_lui (&offset_expr, AT);
252b5132 9015 }
bdaaa2e1 9016
0aa27725 9017 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
252b5132 9018 {
67c0d1eb
RS
9019 macro_build (&offset_expr, "ldc1", "T,o(b)",
9020 treg, BFD_RELOC_LO16, AT);
252b5132
RH
9021 break;
9022 }
9023 breg = AT;
9024 r = BFD_RELOC_LO16;
9025 goto dob;
9026 }
9027
9028 case M_L_DOB:
252b5132
RH
9029 /* Even on a big endian machine $fn comes before $fn+1. We have
9030 to adjust when loading from memory. */
9031 r = BFD_RELOC_LO16;
9032 dob:
df58fc94 9033 gas_assert (!mips_opts.micromips);
0aa27725 9034 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
67c0d1eb 9035 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 9036 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
9037 /* FIXME: A possible overflow which I don't know how to deal
9038 with. */
9039 offset_expr.X_add_number += 4;
67c0d1eb 9040 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 9041 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
9042 break;
9043
c4a68bea 9044 case M_S_DOB:
df58fc94 9045 gas_assert (!mips_opts.micromips);
0aa27725 9046 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
c4a68bea
MR
9047 /* Even on a big endian machine $fn comes before $fn+1. We have
9048 to adjust when storing to memory. */
9049 macro_build (&offset_expr, "swc1", "T,o(b)",
9050 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
9051 offset_expr.X_add_number += 4;
9052 macro_build (&offset_expr, "swc1", "T,o(b)",
9053 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
9054 break;
9055
252b5132 9056 case M_L_DAB:
df58fc94 9057 gas_assert (!mips_opts.micromips);
252b5132
RH
9058 /*
9059 * The MIPS assembler seems to check for X_add_number not
9060 * being double aligned and generating:
9061 * lui at,%hi(foo+1)
9062 * addu at,at,v1
9063 * addiu at,at,%lo(foo+1)
9064 * lwc1 f2,0(at)
9065 * lwc1 f3,4(at)
9066 * But, the resulting address is the same after relocation so why
9067 * generate the extra instruction?
9068 */
bdaaa2e1 9069 /* Itbl support may require additional care here. */
252b5132 9070 coproc = 1;
df58fc94 9071 fmt = "T,o(b)";
0aa27725 9072 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
9073 {
9074 s = "ldc1";
df58fc94 9075 goto ld_st;
252b5132 9076 }
252b5132 9077 s = "lwc1";
252b5132
RH
9078 goto ldd_std;
9079
9080 case M_S_DAB:
df58fc94
RS
9081 gas_assert (!mips_opts.micromips);
9082 /* Itbl support may require additional care here. */
9083 coproc = 1;
9084 fmt = "T,o(b)";
0aa27725 9085 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
9086 {
9087 s = "sdc1";
df58fc94 9088 goto ld_st;
252b5132 9089 }
252b5132 9090 s = "swc1";
252b5132
RH
9091 goto ldd_std;
9092
e407c74b
NC
9093 case M_LQ_AB:
9094 fmt = "t,o(b)";
9095 s = "lq";
9096 goto ld;
9097
9098 case M_SQ_AB:
9099 fmt = "t,o(b)";
9100 s = "sq";
9101 goto ld_st;
9102
252b5132 9103 case M_LD_AB:
df58fc94 9104 fmt = "t,o(b)";
ca4e0257 9105 if (HAVE_64BIT_GPRS)
252b5132
RH
9106 {
9107 s = "ld";
9108 goto ld;
9109 }
252b5132 9110 s = "lw";
252b5132
RH
9111 goto ldd_std;
9112
9113 case M_SD_AB:
df58fc94 9114 fmt = "t,o(b)";
ca4e0257 9115 if (HAVE_64BIT_GPRS)
252b5132
RH
9116 {
9117 s = "sd";
df58fc94 9118 goto ld_st;
252b5132 9119 }
252b5132 9120 s = "sw";
252b5132
RH
9121
9122 ldd_std:
9123 if (offset_expr.X_op != O_symbol
9124 && offset_expr.X_op != O_constant)
9125 {
f71d0d44 9126 as_bad (_("Expression too complex"));
252b5132
RH
9127 offset_expr.X_op = O_constant;
9128 }
9129
2051e8c4
MR
9130 if (HAVE_32BIT_ADDRESSES
9131 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
9132 {
9133 char value [32];
9134
9135 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 9136 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 9137 }
2051e8c4 9138
252b5132
RH
9139 /* Even on a big endian machine $fn comes before $fn+1. We have
9140 to adjust when loading from memory. We set coproc if we must
9141 load $fn+1 first. */
bdaaa2e1 9142 /* Itbl support may require additional care here. */
90ecf173 9143 if (!target_big_endian)
252b5132
RH
9144 coproc = 0;
9145
90ecf173 9146 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
9147 {
9148 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
9149 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
9150 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
9151 If we have a base register, we use this
9152 addu $at,$breg,$gp
cdf6fd85
TS
9153 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
9154 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
9155 If this is not a GP relative symbol, we want
9156 lui $at,<sym> (BFD_RELOC_HI16_S)
9157 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9158 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9159 If there is a base register, we add it to $at after the
9160 lui instruction. If there is a constant, we always use
9161 the last case. */
39a59cf8
MR
9162 if (offset_expr.X_op == O_symbol
9163 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9164 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 9165 {
4d7206a2 9166 relax_start (offset_expr.X_add_symbol);
252b5132
RH
9167 if (breg == 0)
9168 {
c9914766 9169 tempreg = mips_gp_register;
252b5132
RH
9170 }
9171 else
9172 {
67c0d1eb 9173 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9174 AT, breg, mips_gp_register);
252b5132 9175 tempreg = AT;
252b5132
RH
9176 used_at = 1;
9177 }
9178
beae10d5 9179 /* Itbl support may require additional care here. */
67c0d1eb 9180 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9181 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
9182 offset_expr.X_add_number += 4;
9183
9184 /* Set mips_optimize to 2 to avoid inserting an
9185 undesired nop. */
9186 hold_mips_optimize = mips_optimize;
9187 mips_optimize = 2;
beae10d5 9188 /* Itbl support may require additional care here. */
67c0d1eb 9189 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9190 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
9191 mips_optimize = hold_mips_optimize;
9192
4d7206a2 9193 relax_switch ();
252b5132 9194
0970e49e 9195 offset_expr.X_add_number -= 4;
252b5132 9196 }
8fc2e39e 9197 used_at = 1;
67c0d1eb 9198 macro_build_lui (&offset_expr, AT);
252b5132 9199 if (breg != 0)
67c0d1eb 9200 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9201 /* Itbl support may require additional care here. */
67c0d1eb 9202 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9203 BFD_RELOC_LO16, AT);
252b5132
RH
9204 /* FIXME: How do we handle overflow here? */
9205 offset_expr.X_add_number += 4;
beae10d5 9206 /* Itbl support may require additional care here. */
67c0d1eb 9207 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9208 BFD_RELOC_LO16, AT);
4d7206a2
RS
9209 if (mips_relax.sequence)
9210 relax_end ();
bdaaa2e1 9211 }
0a44bf69 9212 else if (!mips_big_got)
252b5132 9213 {
252b5132
RH
9214 /* If this is a reference to an external symbol, we want
9215 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9216 nop
9217 <op> $treg,0($at)
9218 <op> $treg+1,4($at)
9219 Otherwise we want
9220 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9221 nop
9222 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9223 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9224 If there is a base register we add it to $at before the
9225 lwc1 instructions. If there is a constant we include it
9226 in the lwc1 instructions. */
9227 used_at = 1;
9228 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
9229 if (expr1.X_add_number < -0x8000
9230 || expr1.X_add_number >= 0x8000 - 4)
9231 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9232 load_got_offset (AT, &offset_expr);
269137b2 9233 load_delay_nop ();
252b5132 9234 if (breg != 0)
67c0d1eb 9235 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
9236
9237 /* Set mips_optimize to 2 to avoid inserting an undesired
9238 nop. */
9239 hold_mips_optimize = mips_optimize;
9240 mips_optimize = 2;
4d7206a2 9241
beae10d5 9242 /* Itbl support may require additional care here. */
4d7206a2 9243 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9244 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9245 BFD_RELOC_LO16, AT);
4d7206a2 9246 expr1.X_add_number += 4;
67c0d1eb
RS
9247 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9248 BFD_RELOC_LO16, AT);
4d7206a2 9249 relax_switch ();
67c0d1eb
RS
9250 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9251 BFD_RELOC_LO16, AT);
4d7206a2 9252 offset_expr.X_add_number += 4;
67c0d1eb
RS
9253 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9254 BFD_RELOC_LO16, AT);
4d7206a2 9255 relax_end ();
252b5132 9256
4d7206a2 9257 mips_optimize = hold_mips_optimize;
252b5132 9258 }
0a44bf69 9259 else if (mips_big_got)
252b5132 9260 {
67c0d1eb 9261 int gpdelay;
252b5132
RH
9262
9263 /* If this is a reference to an external symbol, we want
9264 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9265 addu $at,$at,$gp
9266 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
9267 nop
9268 <op> $treg,0($at)
9269 <op> $treg+1,4($at)
9270 Otherwise we want
9271 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9272 nop
9273 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9274 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9275 If there is a base register we add it to $at before the
9276 lwc1 instructions. If there is a constant we include it
9277 in the lwc1 instructions. */
9278 used_at = 1;
9279 expr1.X_add_number = offset_expr.X_add_number;
9280 offset_expr.X_add_number = 0;
9281 if (expr1.X_add_number < -0x8000
9282 || expr1.X_add_number >= 0x8000 - 4)
9283 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9284 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 9285 relax_start (offset_expr.X_add_symbol);
df58fc94 9286 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
9287 AT, BFD_RELOC_MIPS_GOT_HI16);
9288 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9289 AT, AT, mips_gp_register);
67c0d1eb 9290 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 9291 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 9292 load_delay_nop ();
252b5132 9293 if (breg != 0)
67c0d1eb 9294 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9295 /* Itbl support may require additional care here. */
67c0d1eb 9296 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9297 BFD_RELOC_LO16, AT);
252b5132
RH
9298 expr1.X_add_number += 4;
9299
9300 /* Set mips_optimize to 2 to avoid inserting an undesired
9301 nop. */
9302 hold_mips_optimize = mips_optimize;
9303 mips_optimize = 2;
beae10d5 9304 /* Itbl support may require additional care here. */
67c0d1eb 9305 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 9306 BFD_RELOC_LO16, AT);
252b5132
RH
9307 mips_optimize = hold_mips_optimize;
9308 expr1.X_add_number -= 4;
9309
4d7206a2
RS
9310 relax_switch ();
9311 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
9312 if (gpdelay)
9313 macro_build (NULL, "nop", "");
9314 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9315 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9316 load_delay_nop ();
252b5132 9317 if (breg != 0)
67c0d1eb 9318 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9319 /* Itbl support may require additional care here. */
67c0d1eb
RS
9320 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9321 BFD_RELOC_LO16, AT);
4d7206a2 9322 offset_expr.X_add_number += 4;
252b5132
RH
9323
9324 /* Set mips_optimize to 2 to avoid inserting an undesired
9325 nop. */
9326 hold_mips_optimize = mips_optimize;
9327 mips_optimize = 2;
beae10d5 9328 /* Itbl support may require additional care here. */
67c0d1eb
RS
9329 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9330 BFD_RELOC_LO16, AT);
252b5132 9331 mips_optimize = hold_mips_optimize;
4d7206a2 9332 relax_end ();
252b5132 9333 }
252b5132
RH
9334 else
9335 abort ();
9336
252b5132
RH
9337 break;
9338
9339 case M_LD_OB:
704897fb 9340 s = HAVE_64BIT_GPRS ? "ld" : "lw";
252b5132
RH
9341 goto sd_ob;
9342 case M_SD_OB:
704897fb 9343 s = HAVE_64BIT_GPRS ? "sd" : "sw";
252b5132 9344 sd_ob:
4614d845
MR
9345 macro_build (&offset_expr, s, "t,o(b)", treg,
9346 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9347 breg);
704897fb
MR
9348 if (!HAVE_64BIT_GPRS)
9349 {
9350 offset_expr.X_add_number += 4;
9351 macro_build (&offset_expr, s, "t,o(b)", treg + 1,
4614d845
MR
9352 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9353 breg);
704897fb 9354 }
8fc2e39e 9355 break;
252b5132 9356
dd6a37e7
AP
9357
9358 case M_SAA_AB:
9359 ab = 1;
9360 case M_SAA_OB:
9361 s = "saa";
9362 off0 = 1;
9363 fmt = "t,(b)";
9364 goto ld_st;
9365 case M_SAAD_AB:
9366 ab = 1;
9367 case M_SAAD_OB:
9368 s = "saad";
9369 off0 = 1;
9370 fmt = "t,(b)";
9371 goto ld_st;
9372
252b5132
RH
9373 /* New code added to support COPZ instructions.
9374 This code builds table entries out of the macros in mip_opcodes.
9375 R4000 uses interlocks to handle coproc delays.
9376 Other chips (like the R3000) require nops to be inserted for delays.
9377
f72c8c98 9378 FIXME: Currently, we require that the user handle delays.
252b5132
RH
9379 In order to fill delay slots for non-interlocked chips,
9380 we must have a way to specify delays based on the coprocessor.
9381 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9382 What are the side-effects of the cop instruction?
9383 What cache support might we have and what are its effects?
9384 Both coprocessor & memory require delays. how long???
bdaaa2e1 9385 What registers are read/set/modified?
252b5132
RH
9386
9387 If an itbl is provided to interpret cop instructions,
bdaaa2e1 9388 this knowledge can be encoded in the itbl spec. */
252b5132
RH
9389
9390 case M_COP0:
9391 s = "c0";
9392 goto copz;
9393 case M_COP1:
9394 s = "c1";
9395 goto copz;
9396 case M_COP2:
9397 s = "c2";
9398 goto copz;
9399 case M_COP3:
9400 s = "c3";
9401 copz:
df58fc94 9402 gas_assert (!mips_opts.micromips);
252b5132
RH
9403 /* For now we just do C (same as Cz). The parameter will be
9404 stored in insn_opcode by mips_ip. */
67c0d1eb 9405 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 9406 break;
252b5132 9407
ea1fb5dc 9408 case M_MOVE:
67c0d1eb 9409 move_register (dreg, sreg);
8fc2e39e 9410 break;
ea1fb5dc 9411
252b5132
RH
9412 case M_DMUL:
9413 dbl = 1;
9414 case M_MUL:
e407c74b
NC
9415 if (mips_opts.arch == CPU_R5900)
9416 {
9417 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", dreg, sreg, treg);
9418 }
9419 else
9420 {
67c0d1eb 9421 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
df58fc94 9422 macro_build (NULL, "mflo", MFHL_FMT, dreg);
e407c74b 9423 }
8fc2e39e 9424 break;
252b5132
RH
9425
9426 case M_DMUL_I:
9427 dbl = 1;
9428 case M_MUL_I:
9429 /* The MIPS assembler some times generates shifts and adds. I'm
9430 not trying to be that fancy. GCC should do this for us
9431 anyway. */
8fc2e39e 9432 used_at = 1;
67c0d1eb
RS
9433 load_register (AT, &imm_expr, dbl);
9434 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
df58fc94 9435 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9436 break;
9437
9438 case M_DMULO_I:
9439 dbl = 1;
9440 case M_MULO_I:
9441 imm = 1;
9442 goto do_mulo;
9443
9444 case M_DMULO:
9445 dbl = 1;
9446 case M_MULO:
9447 do_mulo:
7d10b47d 9448 start_noreorder ();
8fc2e39e 9449 used_at = 1;
252b5132 9450 if (imm)
67c0d1eb
RS
9451 load_register (AT, &imm_expr, dbl);
9452 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
df58fc94
RS
9453 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9454 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9455 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 9456 if (mips_trap)
df58fc94 9457 macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
252b5132
RH
9458 else
9459 {
df58fc94
RS
9460 if (mips_opts.micromips)
9461 micromips_label_expr (&label_expr);
9462 else
9463 label_expr.X_add_number = 8;
9464 macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
a605d2b3 9465 macro_build (NULL, "nop", "");
df58fc94
RS
9466 macro_build (NULL, "break", BRK_FMT, 6);
9467 if (mips_opts.micromips)
9468 micromips_add_label ();
252b5132 9469 }
7d10b47d 9470 end_noreorder ();
df58fc94 9471 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9472 break;
9473
9474 case M_DMULOU_I:
9475 dbl = 1;
9476 case M_MULOU_I:
9477 imm = 1;
9478 goto do_mulou;
9479
9480 case M_DMULOU:
9481 dbl = 1;
9482 case M_MULOU:
9483 do_mulou:
7d10b47d 9484 start_noreorder ();
8fc2e39e 9485 used_at = 1;
252b5132 9486 if (imm)
67c0d1eb
RS
9487 load_register (AT, &imm_expr, dbl);
9488 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 9489 sreg, imm ? AT : treg);
df58fc94
RS
9490 macro_build (NULL, "mfhi", MFHL_FMT, AT);
9491 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132 9492 if (mips_trap)
df58fc94 9493 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
9494 else
9495 {
df58fc94
RS
9496 if (mips_opts.micromips)
9497 micromips_label_expr (&label_expr);
9498 else
9499 label_expr.X_add_number = 8;
9500 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 9501 macro_build (NULL, "nop", "");
df58fc94
RS
9502 macro_build (NULL, "break", BRK_FMT, 6);
9503 if (mips_opts.micromips)
9504 micromips_add_label ();
252b5132 9505 }
7d10b47d 9506 end_noreorder ();
252b5132
RH
9507 break;
9508
771c7ce4 9509 case M_DROL:
fef14a42 9510 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
9511 {
9512 if (dreg == sreg)
9513 {
9514 tempreg = AT;
9515 used_at = 1;
9516 }
9517 else
9518 {
9519 tempreg = dreg;
82dd0097 9520 }
67c0d1eb
RS
9521 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9522 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9523 break;
82dd0097 9524 }
8fc2e39e 9525 used_at = 1;
c80c840e 9526 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9527 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9528 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9529 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9530 break;
9531
252b5132 9532 case M_ROL:
fef14a42 9533 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
9534 {
9535 if (dreg == sreg)
9536 {
9537 tempreg = AT;
9538 used_at = 1;
9539 }
9540 else
9541 {
9542 tempreg = dreg;
82dd0097 9543 }
67c0d1eb
RS
9544 macro_build (NULL, "negu", "d,w", tempreg, treg);
9545 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9546 break;
82dd0097 9547 }
8fc2e39e 9548 used_at = 1;
c80c840e 9549 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9550 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9551 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9552 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9553 break;
9554
771c7ce4
TS
9555 case M_DROL_I:
9556 {
9557 unsigned int rot;
91d6fa6a
NC
9558 char *l;
9559 char *rr;
771c7ce4
TS
9560
9561 if (imm_expr.X_op != O_constant)
82dd0097 9562 as_bad (_("Improper rotate count"));
771c7ce4 9563 rot = imm_expr.X_add_number & 0x3f;
fef14a42 9564 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
9565 {
9566 rot = (64 - rot) & 0x3f;
9567 if (rot >= 32)
df58fc94 9568 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
60b63b72 9569 else
df58fc94 9570 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9571 break;
60b63b72 9572 }
483fc7cd 9573 if (rot == 0)
483fc7cd 9574 {
df58fc94 9575 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9576 break;
483fc7cd 9577 }
82dd0097 9578 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 9579 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 9580 rot &= 0x1f;
8fc2e39e 9581 used_at = 1;
df58fc94
RS
9582 macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
9583 macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9584 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9585 }
9586 break;
9587
252b5132 9588 case M_ROL_I:
771c7ce4
TS
9589 {
9590 unsigned int rot;
9591
9592 if (imm_expr.X_op != O_constant)
82dd0097 9593 as_bad (_("Improper rotate count"));
771c7ce4 9594 rot = imm_expr.X_add_number & 0x1f;
fef14a42 9595 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 9596 {
df58fc94 9597 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 9598 break;
60b63b72 9599 }
483fc7cd 9600 if (rot == 0)
483fc7cd 9601 {
df58fc94 9602 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9603 break;
483fc7cd 9604 }
8fc2e39e 9605 used_at = 1;
df58fc94
RS
9606 macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
9607 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9608 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9609 }
9610 break;
9611
9612 case M_DROR:
fef14a42 9613 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 9614 {
67c0d1eb 9615 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 9616 break;
82dd0097 9617 }
8fc2e39e 9618 used_at = 1;
c80c840e 9619 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9620 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
9621 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
9622 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9623 break;
9624
9625 case M_ROR:
fef14a42 9626 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 9627 {
67c0d1eb 9628 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 9629 break;
82dd0097 9630 }
8fc2e39e 9631 used_at = 1;
c80c840e 9632 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9633 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
9634 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
9635 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9636 break;
9637
771c7ce4
TS
9638 case M_DROR_I:
9639 {
9640 unsigned int rot;
91d6fa6a
NC
9641 char *l;
9642 char *rr;
771c7ce4
TS
9643
9644 if (imm_expr.X_op != O_constant)
82dd0097 9645 as_bad (_("Improper rotate count"));
771c7ce4 9646 rot = imm_expr.X_add_number & 0x3f;
fef14a42 9647 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
9648 {
9649 if (rot >= 32)
df58fc94 9650 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
82dd0097 9651 else
df58fc94 9652 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9653 break;
82dd0097 9654 }
483fc7cd 9655 if (rot == 0)
483fc7cd 9656 {
df58fc94 9657 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9658 break;
483fc7cd 9659 }
91d6fa6a 9660 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
9661 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
9662 rot &= 0x1f;
8fc2e39e 9663 used_at = 1;
df58fc94
RS
9664 macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
9665 macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9666 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9667 }
9668 break;
9669
252b5132 9670 case M_ROR_I:
771c7ce4
TS
9671 {
9672 unsigned int rot;
9673
9674 if (imm_expr.X_op != O_constant)
82dd0097 9675 as_bad (_("Improper rotate count"));
771c7ce4 9676 rot = imm_expr.X_add_number & 0x1f;
fef14a42 9677 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 9678 {
df58fc94 9679 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9680 break;
82dd0097 9681 }
483fc7cd 9682 if (rot == 0)
483fc7cd 9683 {
df58fc94 9684 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9685 break;
483fc7cd 9686 }
8fc2e39e 9687 used_at = 1;
df58fc94
RS
9688 macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
9689 macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9690 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 9691 }
252b5132
RH
9692 break;
9693
252b5132
RH
9694 case M_SEQ:
9695 if (sreg == 0)
67c0d1eb 9696 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 9697 else if (treg == 0)
67c0d1eb 9698 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9699 else
9700 {
67c0d1eb
RS
9701 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9702 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 9703 }
8fc2e39e 9704 break;
252b5132
RH
9705
9706 case M_SEQ_I:
9707 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9708 {
67c0d1eb 9709 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9710 break;
252b5132
RH
9711 }
9712 if (sreg == 0)
9713 {
9714 as_warn (_("Instruction %s: result is always false"),
9715 ip->insn_mo->name);
67c0d1eb 9716 move_register (dreg, 0);
8fc2e39e 9717 break;
252b5132 9718 }
dd3cbb7e
NC
9719 if (CPU_HAS_SEQ (mips_opts.arch)
9720 && -512 <= imm_expr.X_add_number
9721 && imm_expr.X_add_number < 512)
9722 {
9723 macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
750bdd57 9724 (int) imm_expr.X_add_number);
dd3cbb7e
NC
9725 break;
9726 }
252b5132
RH
9727 if (imm_expr.X_op == O_constant
9728 && imm_expr.X_add_number >= 0
9729 && imm_expr.X_add_number < 0x10000)
9730 {
67c0d1eb 9731 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9732 }
9733 else if (imm_expr.X_op == O_constant
9734 && imm_expr.X_add_number > -0x8000
9735 && imm_expr.X_add_number < 0)
9736 {
9737 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 9738 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 9739 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 9740 }
dd3cbb7e
NC
9741 else if (CPU_HAS_SEQ (mips_opts.arch))
9742 {
9743 used_at = 1;
9744 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9745 macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
9746 break;
9747 }
252b5132
RH
9748 else
9749 {
67c0d1eb
RS
9750 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9751 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
9752 used_at = 1;
9753 }
67c0d1eb 9754 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9755 break;
252b5132
RH
9756
9757 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
9758 s = "slt";
9759 goto sge;
9760 case M_SGEU:
9761 s = "sltu";
9762 sge:
67c0d1eb
RS
9763 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
9764 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9765 break;
252b5132
RH
9766
9767 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
9768 case M_SGEU_I:
9769 if (imm_expr.X_op == O_constant
9770 && imm_expr.X_add_number >= -0x8000
9771 && imm_expr.X_add_number < 0x8000)
9772 {
67c0d1eb
RS
9773 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
9774 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9775 }
9776 else
9777 {
67c0d1eb
RS
9778 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9779 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
9780 dreg, sreg, AT);
252b5132
RH
9781 used_at = 1;
9782 }
67c0d1eb 9783 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9784 break;
252b5132
RH
9785
9786 case M_SGT: /* sreg > treg <==> treg < sreg */
9787 s = "slt";
9788 goto sgt;
9789 case M_SGTU:
9790 s = "sltu";
9791 sgt:
67c0d1eb 9792 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 9793 break;
252b5132
RH
9794
9795 case M_SGT_I: /* sreg > I <==> I < sreg */
9796 s = "slt";
9797 goto sgti;
9798 case M_SGTU_I:
9799 s = "sltu";
9800 sgti:
8fc2e39e 9801 used_at = 1;
67c0d1eb
RS
9802 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9803 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
9804 break;
9805
2396cfb9 9806 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
9807 s = "slt";
9808 goto sle;
9809 case M_SLEU:
9810 s = "sltu";
9811 sle:
67c0d1eb
RS
9812 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9813 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9814 break;
252b5132 9815
2396cfb9 9816 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
9817 s = "slt";
9818 goto slei;
9819 case M_SLEU_I:
9820 s = "sltu";
9821 slei:
8fc2e39e 9822 used_at = 1;
67c0d1eb
RS
9823 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9824 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9825 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
9826 break;
9827
9828 case M_SLT_I:
9829 if (imm_expr.X_op == O_constant
9830 && imm_expr.X_add_number >= -0x8000
9831 && imm_expr.X_add_number < 0x8000)
9832 {
67c0d1eb 9833 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9834 break;
252b5132 9835 }
8fc2e39e 9836 used_at = 1;
67c0d1eb
RS
9837 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9838 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
9839 break;
9840
9841 case M_SLTU_I:
9842 if (imm_expr.X_op == O_constant
9843 && imm_expr.X_add_number >= -0x8000
9844 && imm_expr.X_add_number < 0x8000)
9845 {
67c0d1eb 9846 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 9847 BFD_RELOC_LO16);
8fc2e39e 9848 break;
252b5132 9849 }
8fc2e39e 9850 used_at = 1;
67c0d1eb
RS
9851 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9852 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
9853 break;
9854
9855 case M_SNE:
9856 if (sreg == 0)
67c0d1eb 9857 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 9858 else if (treg == 0)
67c0d1eb 9859 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
9860 else
9861 {
67c0d1eb
RS
9862 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9863 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 9864 }
8fc2e39e 9865 break;
252b5132
RH
9866
9867 case M_SNE_I:
9868 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9869 {
67c0d1eb 9870 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 9871 break;
252b5132
RH
9872 }
9873 if (sreg == 0)
9874 {
9875 as_warn (_("Instruction %s: result is always true"),
9876 ip->insn_mo->name);
67c0d1eb
RS
9877 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
9878 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 9879 break;
252b5132 9880 }
dd3cbb7e
NC
9881 if (CPU_HAS_SEQ (mips_opts.arch)
9882 && -512 <= imm_expr.X_add_number
9883 && imm_expr.X_add_number < 512)
9884 {
9885 macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
750bdd57 9886 (int) imm_expr.X_add_number);
dd3cbb7e
NC
9887 break;
9888 }
252b5132
RH
9889 if (imm_expr.X_op == O_constant
9890 && imm_expr.X_add_number >= 0
9891 && imm_expr.X_add_number < 0x10000)
9892 {
67c0d1eb 9893 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9894 }
9895 else if (imm_expr.X_op == O_constant
9896 && imm_expr.X_add_number > -0x8000
9897 && imm_expr.X_add_number < 0)
9898 {
9899 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 9900 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 9901 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 9902 }
dd3cbb7e
NC
9903 else if (CPU_HAS_SEQ (mips_opts.arch))
9904 {
9905 used_at = 1;
9906 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9907 macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
9908 break;
9909 }
252b5132
RH
9910 else
9911 {
67c0d1eb
RS
9912 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9913 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
9914 used_at = 1;
9915 }
67c0d1eb 9916 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 9917 break;
252b5132 9918
df58fc94
RS
9919 case M_SUB_I:
9920 s = "addi";
9921 s2 = "sub";
9922 goto do_subi;
9923 case M_SUBU_I:
9924 s = "addiu";
9925 s2 = "subu";
9926 goto do_subi;
252b5132
RH
9927 case M_DSUB_I:
9928 dbl = 1;
df58fc94
RS
9929 s = "daddi";
9930 s2 = "dsub";
9931 if (!mips_opts.micromips)
9932 goto do_subi;
252b5132 9933 if (imm_expr.X_op == O_constant
df58fc94
RS
9934 && imm_expr.X_add_number > -0x200
9935 && imm_expr.X_add_number <= 0x200)
252b5132 9936 {
df58fc94 9937 macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
8fc2e39e 9938 break;
252b5132 9939 }
df58fc94 9940 goto do_subi_i;
252b5132
RH
9941 case M_DSUBU_I:
9942 dbl = 1;
df58fc94
RS
9943 s = "daddiu";
9944 s2 = "dsubu";
9945 do_subi:
252b5132
RH
9946 if (imm_expr.X_op == O_constant
9947 && imm_expr.X_add_number > -0x8000
9948 && imm_expr.X_add_number <= 0x8000)
9949 {
9950 imm_expr.X_add_number = -imm_expr.X_add_number;
df58fc94 9951 macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9952 break;
252b5132 9953 }
df58fc94 9954 do_subi_i:
8fc2e39e 9955 used_at = 1;
67c0d1eb 9956 load_register (AT, &imm_expr, dbl);
df58fc94 9957 macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
252b5132
RH
9958 break;
9959
9960 case M_TEQ_I:
9961 s = "teq";
9962 goto trap;
9963 case M_TGE_I:
9964 s = "tge";
9965 goto trap;
9966 case M_TGEU_I:
9967 s = "tgeu";
9968 goto trap;
9969 case M_TLT_I:
9970 s = "tlt";
9971 goto trap;
9972 case M_TLTU_I:
9973 s = "tltu";
9974 goto trap;
9975 case M_TNE_I:
9976 s = "tne";
9977 trap:
8fc2e39e 9978 used_at = 1;
67c0d1eb
RS
9979 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9980 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
9981 break;
9982
252b5132 9983 case M_TRUNCWS:
43841e91 9984 case M_TRUNCWD:
df58fc94 9985 gas_assert (!mips_opts.micromips);
0aa27725 9986 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 9987 used_at = 1;
252b5132
RH
9988 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
9989 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
9990
9991 /*
9992 * Is the double cfc1 instruction a bug in the mips assembler;
9993 * or is there a reason for it?
9994 */
7d10b47d 9995 start_noreorder ();
67c0d1eb
RS
9996 macro_build (NULL, "cfc1", "t,G", treg, RA);
9997 macro_build (NULL, "cfc1", "t,G", treg, RA);
9998 macro_build (NULL, "nop", "");
252b5132 9999 expr1.X_add_number = 3;
67c0d1eb 10000 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 10001 expr1.X_add_number = 2;
67c0d1eb
RS
10002 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
10003 macro_build (NULL, "ctc1", "t,G", AT, RA);
10004 macro_build (NULL, "nop", "");
10005 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
10006 dreg, sreg);
10007 macro_build (NULL, "ctc1", "t,G", treg, RA);
10008 macro_build (NULL, "nop", "");
7d10b47d 10009 end_noreorder ();
252b5132
RH
10010 break;
10011
df58fc94
RS
10012 case M_ULH_A:
10013 ab = 1;
252b5132
RH
10014 case M_ULH:
10015 s = "lb";
df58fc94
RS
10016 s2 = "lbu";
10017 off = 1;
10018 goto uld_st;
10019 case M_ULHU_A:
10020 ab = 1;
252b5132
RH
10021 case M_ULHU:
10022 s = "lbu";
df58fc94
RS
10023 s2 = "lbu";
10024 off = 1;
10025 goto uld_st;
10026 case M_ULW_A:
10027 ab = 1;
10028 case M_ULW:
10029 s = "lwl";
10030 s2 = "lwr";
10031 off12 = mips_opts.micromips;
10032 off = 3;
10033 goto uld_st;
10034 case M_ULD_A:
10035 ab = 1;
252b5132
RH
10036 case M_ULD:
10037 s = "ldl";
10038 s2 = "ldr";
df58fc94 10039 off12 = mips_opts.micromips;
252b5132 10040 off = 7;
df58fc94
RS
10041 goto uld_st;
10042 case M_USH_A:
10043 ab = 1;
10044 case M_USH:
10045 s = "sb";
10046 s2 = "sb";
10047 off = 1;
10048 ust = 1;
10049 goto uld_st;
10050 case M_USW_A:
10051 ab = 1;
10052 case M_USW:
10053 s = "swl";
10054 s2 = "swr";
10055 off12 = mips_opts.micromips;
252b5132 10056 off = 3;
df58fc94
RS
10057 ust = 1;
10058 goto uld_st;
10059 case M_USD_A:
10060 ab = 1;
10061 case M_USD:
10062 s = "sdl";
10063 s2 = "sdr";
10064 off12 = mips_opts.micromips;
10065 off = 7;
10066 ust = 1;
10067
10068 uld_st:
10069 if (!ab && offset_expr.X_add_number >= 0x8000 - off)
f71d0d44 10070 as_bad (_("Operand overflow"));
df58fc94
RS
10071
10072 ep = &offset_expr;
10073 expr1.X_add_number = 0;
10074 if (ab)
10075 {
10076 used_at = 1;
10077 tempreg = AT;
10078 load_address (tempreg, ep, &used_at);
10079 if (breg != 0)
10080 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10081 tempreg, tempreg, breg);
10082 breg = tempreg;
10083 tempreg = treg;
10084 ep = &expr1;
10085 }
10086 else if (off12
10087 && (offset_expr.X_op != O_constant
10088 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
10089 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
10090 {
10091 used_at = 1;
10092 tempreg = AT;
10093 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
10094 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10095 breg = tempreg;
10096 tempreg = treg;
10097 ep = &expr1;
10098 }
10099 else if (!ust && treg == breg)
8fc2e39e
TS
10100 {
10101 used_at = 1;
10102 tempreg = AT;
10103 }
252b5132 10104 else
df58fc94 10105 tempreg = treg;
af22f5b2 10106
df58fc94
RS
10107 if (off == 1)
10108 goto ulh_sh;
252b5132 10109
90ecf173 10110 if (!target_big_endian)
df58fc94
RS
10111 ep->X_add_number += off;
10112 if (!off12)
10113 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 10114 else
df58fc94
RS
10115 macro_build (NULL, s, "t,~(b)",
10116 tempreg, (unsigned long) ep->X_add_number, breg);
10117
90ecf173 10118 if (!target_big_endian)
df58fc94 10119 ep->X_add_number -= off;
252b5132 10120 else
df58fc94
RS
10121 ep->X_add_number += off;
10122 if (!off12)
10123 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10124 else
10125 macro_build (NULL, s2, "t,~(b)",
10126 tempreg, (unsigned long) ep->X_add_number, breg);
252b5132 10127
df58fc94
RS
10128 /* If necessary, move the result in tempreg to the final destination. */
10129 if (!ust && treg != tempreg)
10130 {
10131 /* Protect second load's delay slot. */
10132 load_delay_nop ();
10133 move_register (treg, tempreg);
10134 }
8fc2e39e 10135 break;
252b5132 10136
df58fc94 10137 ulh_sh:
d6bc6245 10138 used_at = 1;
df58fc94
RS
10139 if (target_big_endian == ust)
10140 ep->X_add_number += off;
10141 tempreg = ust || ab ? treg : AT;
10142 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10143
10144 /* For halfword transfers we need a temporary register to shuffle
10145 bytes. Unfortunately for M_USH_A we have none available before
10146 the next store as AT holds the base address. We deal with this
10147 case by clobbering TREG and then restoring it as with ULH. */
10148 tempreg = ust == ab ? treg : AT;
10149 if (ust)
10150 macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10151
10152 if (target_big_endian == ust)
10153 ep->X_add_number -= off;
252b5132 10154 else
df58fc94
RS
10155 ep->X_add_number += off;
10156 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 10157
df58fc94
RS
10158 /* For M_USH_A re-retrieve the LSB. */
10159 if (ust && ab)
10160 {
10161 if (target_big_endian)
10162 ep->X_add_number += off;
10163 else
10164 ep->X_add_number -= off;
10165 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
10166 }
10167 /* For ULH and M_USH_A OR the LSB in. */
10168 if (!ust || ab)
10169 {
10170 tempreg = !ab ? AT : treg;
10171 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10172 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10173 }
252b5132
RH
10174 break;
10175
10176 default:
10177 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 10178 are added dynamically. */
252b5132
RH
10179 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10180 break;
10181 }
741fe287 10182 if (!mips_opts.at && used_at)
8fc2e39e 10183 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
10184}
10185
10186/* Implement macros in mips16 mode. */
10187
10188static void
17a2f251 10189mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
10190{
10191 int mask;
10192 int xreg, yreg, zreg, tmp;
252b5132
RH
10193 expressionS expr1;
10194 int dbl;
10195 const char *s, *s2, *s3;
10196
10197 mask = ip->insn_mo->mask;
10198
bf12938e
RS
10199 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10200 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10201 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 10202
252b5132
RH
10203 expr1.X_op = O_constant;
10204 expr1.X_op_symbol = NULL;
10205 expr1.X_add_symbol = NULL;
10206 expr1.X_add_number = 1;
10207
10208 dbl = 0;
10209
10210 switch (mask)
10211 {
10212 default:
b37df7c4 10213 abort ();
252b5132
RH
10214
10215 case M_DDIV_3:
10216 dbl = 1;
10217 case M_DIV_3:
10218 s = "mflo";
10219 goto do_div3;
10220 case M_DREM_3:
10221 dbl = 1;
10222 case M_REM_3:
10223 s = "mfhi";
10224 do_div3:
7d10b47d 10225 start_noreorder ();
67c0d1eb 10226 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 10227 expr1.X_add_number = 2;
67c0d1eb
RS
10228 macro_build (&expr1, "bnez", "x,p", yreg);
10229 macro_build (NULL, "break", "6", 7);
bdaaa2e1 10230
252b5132
RH
10231 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10232 since that causes an overflow. We should do that as well,
10233 but I don't see how to do the comparisons without a temporary
10234 register. */
7d10b47d 10235 end_noreorder ();
67c0d1eb 10236 macro_build (NULL, s, "x", zreg);
252b5132
RH
10237 break;
10238
10239 case M_DIVU_3:
10240 s = "divu";
10241 s2 = "mflo";
10242 goto do_divu3;
10243 case M_REMU_3:
10244 s = "divu";
10245 s2 = "mfhi";
10246 goto do_divu3;
10247 case M_DDIVU_3:
10248 s = "ddivu";
10249 s2 = "mflo";
10250 goto do_divu3;
10251 case M_DREMU_3:
10252 s = "ddivu";
10253 s2 = "mfhi";
10254 do_divu3:
7d10b47d 10255 start_noreorder ();
67c0d1eb 10256 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 10257 expr1.X_add_number = 2;
67c0d1eb
RS
10258 macro_build (&expr1, "bnez", "x,p", yreg);
10259 macro_build (NULL, "break", "6", 7);
7d10b47d 10260 end_noreorder ();
67c0d1eb 10261 macro_build (NULL, s2, "x", zreg);
252b5132
RH
10262 break;
10263
10264 case M_DMUL:
10265 dbl = 1;
10266 case M_MUL:
67c0d1eb
RS
10267 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10268 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 10269 break;
252b5132
RH
10270
10271 case M_DSUBU_I:
10272 dbl = 1;
10273 goto do_subu;
10274 case M_SUBU_I:
10275 do_subu:
10276 if (imm_expr.X_op != O_constant)
10277 as_bad (_("Unsupported large constant"));
10278 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10279 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
10280 break;
10281
10282 case M_SUBU_I_2:
10283 if (imm_expr.X_op != O_constant)
10284 as_bad (_("Unsupported large constant"));
10285 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10286 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
10287 break;
10288
10289 case M_DSUBU_I_2:
10290 if (imm_expr.X_op != O_constant)
10291 as_bad (_("Unsupported large constant"));
10292 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10293 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
10294 break;
10295
10296 case M_BEQ:
10297 s = "cmp";
10298 s2 = "bteqz";
10299 goto do_branch;
10300 case M_BNE:
10301 s = "cmp";
10302 s2 = "btnez";
10303 goto do_branch;
10304 case M_BLT:
10305 s = "slt";
10306 s2 = "btnez";
10307 goto do_branch;
10308 case M_BLTU:
10309 s = "sltu";
10310 s2 = "btnez";
10311 goto do_branch;
10312 case M_BLE:
10313 s = "slt";
10314 s2 = "bteqz";
10315 goto do_reverse_branch;
10316 case M_BLEU:
10317 s = "sltu";
10318 s2 = "bteqz";
10319 goto do_reverse_branch;
10320 case M_BGE:
10321 s = "slt";
10322 s2 = "bteqz";
10323 goto do_branch;
10324 case M_BGEU:
10325 s = "sltu";
10326 s2 = "bteqz";
10327 goto do_branch;
10328 case M_BGT:
10329 s = "slt";
10330 s2 = "btnez";
10331 goto do_reverse_branch;
10332 case M_BGTU:
10333 s = "sltu";
10334 s2 = "btnez";
10335
10336 do_reverse_branch:
10337 tmp = xreg;
10338 xreg = yreg;
10339 yreg = tmp;
10340
10341 do_branch:
67c0d1eb
RS
10342 macro_build (NULL, s, "x,y", xreg, yreg);
10343 macro_build (&offset_expr, s2, "p");
252b5132
RH
10344 break;
10345
10346 case M_BEQ_I:
10347 s = "cmpi";
10348 s2 = "bteqz";
10349 s3 = "x,U";
10350 goto do_branch_i;
10351 case M_BNE_I:
10352 s = "cmpi";
10353 s2 = "btnez";
10354 s3 = "x,U";
10355 goto do_branch_i;
10356 case M_BLT_I:
10357 s = "slti";
10358 s2 = "btnez";
10359 s3 = "x,8";
10360 goto do_branch_i;
10361 case M_BLTU_I:
10362 s = "sltiu";
10363 s2 = "btnez";
10364 s3 = "x,8";
10365 goto do_branch_i;
10366 case M_BLE_I:
10367 s = "slti";
10368 s2 = "btnez";
10369 s3 = "x,8";
10370 goto do_addone_branch_i;
10371 case M_BLEU_I:
10372 s = "sltiu";
10373 s2 = "btnez";
10374 s3 = "x,8";
10375 goto do_addone_branch_i;
10376 case M_BGE_I:
10377 s = "slti";
10378 s2 = "bteqz";
10379 s3 = "x,8";
10380 goto do_branch_i;
10381 case M_BGEU_I:
10382 s = "sltiu";
10383 s2 = "bteqz";
10384 s3 = "x,8";
10385 goto do_branch_i;
10386 case M_BGT_I:
10387 s = "slti";
10388 s2 = "bteqz";
10389 s3 = "x,8";
10390 goto do_addone_branch_i;
10391 case M_BGTU_I:
10392 s = "sltiu";
10393 s2 = "bteqz";
10394 s3 = "x,8";
10395
10396 do_addone_branch_i:
10397 if (imm_expr.X_op != O_constant)
10398 as_bad (_("Unsupported large constant"));
10399 ++imm_expr.X_add_number;
10400
10401 do_branch_i:
67c0d1eb
RS
10402 macro_build (&imm_expr, s, s3, xreg);
10403 macro_build (&offset_expr, s2, "p");
252b5132
RH
10404 break;
10405
10406 case M_ABS:
10407 expr1.X_add_number = 0;
67c0d1eb 10408 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 10409 if (xreg != yreg)
67c0d1eb 10410 move_register (xreg, yreg);
252b5132 10411 expr1.X_add_number = 2;
67c0d1eb
RS
10412 macro_build (&expr1, "bteqz", "p");
10413 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
10414 }
10415}
10416
10417/* For consistency checking, verify that all bits are specified either
10418 by the match/mask part of the instruction definition, or by the
10419 operand list. */
10420static int
17a2f251 10421validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
10422{
10423 const char *p = opc->args;
10424 char c;
10425 unsigned long used_bits = opc->mask;
10426
10427 if ((used_bits & opc->match) != opc->match)
10428 {
10429 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10430 opc->name, opc->args);
10431 return 0;
10432 }
10433#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
10434 while (*p)
10435 switch (c = *p++)
10436 {
10437 case ',': break;
10438 case '(': break;
10439 case ')': break;
af7ee8bf
CD
10440 case '+':
10441 switch (c = *p++)
10442 {
9bcd4f99
TS
10443 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
10444 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
10445 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
10446 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
af7ee8bf
CD
10447 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10448 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10449 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
10450 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
10451 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
10452 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10453 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10454 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10455 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10456 case 'I': break;
b015e599 10457 case 'J': USE_BITS (OP_MASK_CODE10, OP_SH_CODE10); break;
ef2e4d86
CF
10458 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10459 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
10460 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
bb35fb24
NC
10461 case 'x': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10462 case 'X': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10463 case 'p': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
10464 case 'P': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
dd3cbb7e 10465 case 'Q': USE_BITS (OP_MASK_SEQI, OP_SH_SEQI); break;
bb35fb24
NC
10466 case 's': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
10467 case 'S': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
98675402
RS
10468 case 'z': USE_BITS (OP_MASK_RZ, OP_SH_RZ); break;
10469 case 'Z': USE_BITS (OP_MASK_FZ, OP_SH_FZ); break;
10470 case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10471 case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10472 case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
bb35fb24 10473
af7ee8bf
CD
10474 default:
10475 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10476 c, opc->name, opc->args);
10477 return 0;
10478 }
10479 break;
252b5132
RH
10480 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10481 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10482 case 'A': break;
4372b673 10483 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
10484 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
10485 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10486 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10487 case 'F': break;
10488 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 10489 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 10490 case 'I': break;
e972090a 10491 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 10492 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10493 case 'L': break;
10494 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
10495 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
10496 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
10497 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
10498 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10499 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
10500 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10501 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10502 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10503 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
10504 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10505 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10506 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10507 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
10508 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10509 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
10510 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10511 case 'f': break;
10512 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
10513 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10514 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10515 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
10516 case 'l': break;
10517 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10518 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10519 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
10520 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10521 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10522 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10523 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10524 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10525 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10526 case 'x': break;
10527 case 'z': break;
10528 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
10529 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
10530 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
10531 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
10532 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
10533 case '[': break;
10534 case ']': break;
620edafd 10535 case '1': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8b082fb1 10536 case '2': USE_BITS (OP_MASK_BP, OP_SH_BP); break;
74cd071d
CF
10537 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
10538 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
10539 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
10540 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10541 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
10542 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
10543 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
10544 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
10545 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
10546 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
10547 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
10548 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
10549 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
10550 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
10551 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
dec0624d
MR
10552 case '\\': USE_BITS (OP_MASK_3BITPOS, OP_SH_3BITPOS); break;
10553 case '~': USE_BITS (OP_MASK_OFFSET12, OP_SH_OFFSET12); break;
ef2e4d86 10554 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10555 default:
10556 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10557 c, opc->name, opc->args);
10558 return 0;
10559 }
10560#undef USE_BITS
10561 if (used_bits != 0xffffffff)
10562 {
10563 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
10564 ~used_bits & 0xffffffff, opc->name, opc->args);
10565 return 0;
10566 }
10567 return 1;
10568}
10569
df58fc94
RS
10570/* For consistency checking, verify that the length implied matches the
10571 major opcode and that all bits are specified either by the match/mask
10572 part of the instruction definition, or by the operand list. */
10573
10574static int
10575validate_micromips_insn (const struct mips_opcode *opc)
10576{
10577 unsigned long match = opc->match;
10578 unsigned long mask = opc->mask;
10579 const char *p = opc->args;
10580 unsigned long insn_bits;
10581 unsigned long used_bits;
10582 unsigned long major;
10583 unsigned int length;
10584 char e;
10585 char c;
10586
10587 if ((mask & match) != match)
10588 {
10589 as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
10590 opc->name, opc->args);
10591 return 0;
10592 }
10593 length = micromips_insn_length (opc);
10594 if (length != 2 && length != 4)
10595 {
10596 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
10597 "%s %s"), length, opc->name, opc->args);
10598 return 0;
10599 }
10600 major = match >> (10 + 8 * (length - 2));
10601 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
10602 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
10603 {
10604 as_bad (_("Internal error: bad microMIPS opcode "
10605 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
10606 return 0;
10607 }
10608
10609 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
10610 insn_bits = 1 << 4 * length;
10611 insn_bits <<= 4 * length;
10612 insn_bits -= 1;
10613 used_bits = mask;
10614#define USE_BITS(field) \
10615 (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
10616 while (*p)
10617 switch (c = *p++)
10618 {
10619 case ',': break;
10620 case '(': break;
10621 case ')': break;
10622 case '+':
10623 e = c;
10624 switch (c = *p++)
10625 {
10626 case 'A': USE_BITS (EXTLSB); break;
10627 case 'B': USE_BITS (INSMSB); break;
10628 case 'C': USE_BITS (EXTMSBD); break;
10629 case 'D': USE_BITS (RS); USE_BITS (SEL); break;
10630 case 'E': USE_BITS (EXTLSB); break;
10631 case 'F': USE_BITS (INSMSB); break;
10632 case 'G': USE_BITS (EXTMSBD); break;
10633 case 'H': USE_BITS (EXTMSBD); break;
10634 default:
10635 as_bad (_("Internal error: bad mips opcode "
10636 "(unknown extension operand type `%c%c'): %s %s"),
10637 e, c, opc->name, opc->args);
10638 return 0;
10639 }
10640 break;
10641 case 'm':
10642 e = c;
10643 switch (c = *p++)
10644 {
10645 case 'A': USE_BITS (IMMA); break;
10646 case 'B': USE_BITS (IMMB); break;
10647 case 'C': USE_BITS (IMMC); break;
10648 case 'D': USE_BITS (IMMD); break;
10649 case 'E': USE_BITS (IMME); break;
10650 case 'F': USE_BITS (IMMF); break;
10651 case 'G': USE_BITS (IMMG); break;
10652 case 'H': USE_BITS (IMMH); break;
10653 case 'I': USE_BITS (IMMI); break;
10654 case 'J': USE_BITS (IMMJ); break;
10655 case 'L': USE_BITS (IMML); break;
10656 case 'M': USE_BITS (IMMM); break;
10657 case 'N': USE_BITS (IMMN); break;
10658 case 'O': USE_BITS (IMMO); break;
10659 case 'P': USE_BITS (IMMP); break;
10660 case 'Q': USE_BITS (IMMQ); break;
10661 case 'U': USE_BITS (IMMU); break;
10662 case 'W': USE_BITS (IMMW); break;
10663 case 'X': USE_BITS (IMMX); break;
10664 case 'Y': USE_BITS (IMMY); break;
10665 case 'Z': break;
10666 case 'a': break;
10667 case 'b': USE_BITS (MB); break;
10668 case 'c': USE_BITS (MC); break;
10669 case 'd': USE_BITS (MD); break;
10670 case 'e': USE_BITS (ME); break;
10671 case 'f': USE_BITS (MF); break;
10672 case 'g': USE_BITS (MG); break;
10673 case 'h': USE_BITS (MH); break;
10674 case 'i': USE_BITS (MI); break;
10675 case 'j': USE_BITS (MJ); break;
10676 case 'l': USE_BITS (ML); break;
10677 case 'm': USE_BITS (MM); break;
10678 case 'n': USE_BITS (MN); break;
10679 case 'p': USE_BITS (MP); break;
10680 case 'q': USE_BITS (MQ); break;
10681 case 'r': break;
10682 case 's': break;
10683 case 't': break;
10684 case 'x': break;
10685 case 'y': break;
10686 case 'z': break;
10687 default:
10688 as_bad (_("Internal error: bad mips opcode "
10689 "(unknown extension operand type `%c%c'): %s %s"),
10690 e, c, opc->name, opc->args);
10691 return 0;
10692 }
10693 break;
10694 case '.': USE_BITS (OFFSET10); break;
10695 case '1': USE_BITS (STYPE); break;
03f66e8a
MR
10696 case '2': USE_BITS (BP); break;
10697 case '3': USE_BITS (SA3); break;
10698 case '4': USE_BITS (SA4); break;
10699 case '5': USE_BITS (IMM8); break;
10700 case '6': USE_BITS (RS); break;
10701 case '7': USE_BITS (DSPACC); break;
10702 case '8': USE_BITS (WRDSP); break;
10703 case '0': USE_BITS (DSPSFT); break;
df58fc94
RS
10704 case '<': USE_BITS (SHAMT); break;
10705 case '>': USE_BITS (SHAMT); break;
03f66e8a 10706 case '@': USE_BITS (IMM10); break;
df58fc94
RS
10707 case 'B': USE_BITS (CODE10); break;
10708 case 'C': USE_BITS (COPZ); break;
10709 case 'D': USE_BITS (FD); break;
10710 case 'E': USE_BITS (RT); break;
10711 case 'G': USE_BITS (RS); break;
444d75be 10712 case 'H': USE_BITS (SEL); break;
df58fc94
RS
10713 case 'K': USE_BITS (RS); break;
10714 case 'M': USE_BITS (CCC); break;
10715 case 'N': USE_BITS (BCC); break;
10716 case 'R': USE_BITS (FR); break;
10717 case 'S': USE_BITS (FS); break;
10718 case 'T': USE_BITS (FT); break;
10719 case 'V': USE_BITS (FS); break;
dec0624d 10720 case '\\': USE_BITS (3BITPOS); break;
03f66e8a 10721 case '^': USE_BITS (RD); break;
df58fc94
RS
10722 case 'a': USE_BITS (TARGET); break;
10723 case 'b': USE_BITS (RS); break;
10724 case 'c': USE_BITS (CODE); break;
10725 case 'd': USE_BITS (RD); break;
10726 case 'h': USE_BITS (PREFX); break;
10727 case 'i': USE_BITS (IMMEDIATE); break;
10728 case 'j': USE_BITS (DELTA); break;
10729 case 'k': USE_BITS (CACHE); break;
10730 case 'n': USE_BITS (RT); break;
10731 case 'o': USE_BITS (DELTA); break;
10732 case 'p': USE_BITS (DELTA); break;
10733 case 'q': USE_BITS (CODE2); break;
10734 case 'r': USE_BITS (RS); break;
10735 case 's': USE_BITS (RS); break;
10736 case 't': USE_BITS (RT); break;
10737 case 'u': USE_BITS (IMMEDIATE); break;
10738 case 'v': USE_BITS (RS); break;
10739 case 'w': USE_BITS (RT); break;
10740 case 'y': USE_BITS (RS3); break;
10741 case 'z': break;
10742 case '|': USE_BITS (TRAP); break;
10743 case '~': USE_BITS (OFFSET12); break;
10744 default:
10745 as_bad (_("Internal error: bad microMIPS opcode "
10746 "(unknown operand type `%c'): %s %s"),
10747 c, opc->name, opc->args);
10748 return 0;
10749 }
10750#undef USE_BITS
10751 if (used_bits != insn_bits)
10752 {
10753 if (~used_bits & insn_bits)
10754 as_bad (_("Internal error: bad microMIPS opcode "
10755 "(bits 0x%lx undefined): %s %s"),
10756 ~used_bits & insn_bits, opc->name, opc->args);
10757 if (used_bits & ~insn_bits)
10758 as_bad (_("Internal error: bad microMIPS opcode "
10759 "(bits 0x%lx defined): %s %s"),
10760 used_bits & ~insn_bits, opc->name, opc->args);
10761 return 0;
10762 }
10763 return 1;
10764}
10765
9bcd4f99
TS
10766/* UDI immediates. */
10767struct mips_immed {
10768 char type;
10769 unsigned int shift;
10770 unsigned long mask;
10771 const char * desc;
10772};
10773
10774static const struct mips_immed mips_immed[] = {
10775 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
10776 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
10777 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
10778 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
10779 { 0,0,0,0 }
10780};
10781
7455baf8
TS
10782/* Check whether an odd floating-point register is allowed. */
10783static int
10784mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
10785{
10786 const char *s = insn->name;
10787
10788 if (insn->pinfo == INSN_MACRO)
10789 /* Let a macro pass, we'll catch it later when it is expanded. */
10790 return 1;
10791
e407c74b 10792 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || (mips_opts.arch == CPU_R5900))
7455baf8
TS
10793 {
10794 /* Allow odd registers for single-precision ops. */
10795 switch (insn->pinfo & (FP_S | FP_D))
10796 {
10797 case FP_S:
10798 case 0:
10799 return 1; /* both single precision - ok */
10800 case FP_D:
10801 return 0; /* both double precision - fail */
10802 default:
10803 break;
10804 }
10805
10806 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
10807 s = strchr (insn->name, '.');
10808 if (argnum == 2)
10809 s = s != NULL ? strchr (s + 1, '.') : NULL;
10810 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
10811 }
10812
10813 /* Single-precision coprocessor loads and moves are OK too. */
10814 if ((insn->pinfo & FP_S)
10815 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
10816 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
10817 return 1;
10818
10819 return 0;
10820}
10821
df58fc94
RS
10822/* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
10823 taking bits from BIT up. */
10824static int
10825expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
10826{
10827 return (ep->X_op == O_constant
10828 && (ep->X_add_number & ((1 << bit) - 1)) == 0
10829 && ep->X_add_number >= min << bit
10830 && ep->X_add_number < max << bit);
10831}
10832
252b5132
RH
10833/* This routine assembles an instruction into its binary format. As a
10834 side effect, it sets one of the global variables imm_reloc or
10835 offset_reloc to the type of relocation to do if one of the operands
10836 is an address expression. */
10837
10838static void
17a2f251 10839mips_ip (char *str, struct mips_cl_insn *ip)
252b5132 10840{
df58fc94
RS
10841 bfd_boolean wrong_delay_slot_insns = FALSE;
10842 bfd_boolean need_delay_slot_ok = TRUE;
10843 struct mips_opcode *firstinsn = NULL;
10844 const struct mips_opcode *past;
10845 struct hash_control *hash;
252b5132
RH
10846 char *s;
10847 const char *args;
43841e91 10848 char c = 0;
252b5132
RH
10849 struct mips_opcode *insn;
10850 char *argsStart;
10851 unsigned int regno;
34224acf 10852 unsigned int lastregno;
df58fc94 10853 unsigned int destregno = 0;
af7ee8bf 10854 unsigned int lastpos = 0;
071742cf 10855 unsigned int limlo, limhi;
f02d8318 10856 int sizelo;
252b5132 10857 char *s_reset;
74cd071d 10858 offsetT min_range, max_range;
df58fc94 10859 long opend;
a40bc9dd 10860 char *name;
707bfff6
TS
10861 int argnum;
10862 unsigned int rtype;
df58fc94 10863 char *dot;
a40bc9dd 10864 long end;
252b5132
RH
10865
10866 insn_error = NULL;
10867
df58fc94
RS
10868 if (mips_opts.micromips)
10869 {
10870 hash = micromips_op_hash;
10871 past = &micromips_opcodes[bfd_micromips_num_opcodes];
10872 }
10873 else
10874 {
10875 hash = op_hash;
10876 past = &mips_opcodes[NUMOPCODES];
10877 }
10878 forced_insn_length = 0;
252b5132 10879 insn = NULL;
252b5132 10880
df58fc94 10881 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
10882 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
10883 continue;
bdaaa2e1 10884
a40bc9dd
RS
10885 /* Make a copy of the instruction so that we can fiddle with it. */
10886 name = alloca (end + 1);
10887 memcpy (name, str, end);
10888 name[end] = '\0';
252b5132 10889
df58fc94
RS
10890 for (;;)
10891 {
10892 insn = (struct mips_opcode *) hash_find (hash, name);
10893
10894 if (insn != NULL || !mips_opts.micromips)
10895 break;
10896 if (forced_insn_length)
10897 break;
10898
10899 /* See if there's an instruction size override suffix,
10900 either `16' or `32', at the end of the mnemonic proper,
10901 that defines the operation, i.e. before the first `.'
10902 character if any. Strip it and retry. */
10903 dot = strchr (name, '.');
10904 opend = dot != NULL ? dot - name : end;
10905 if (opend < 3)
10906 break;
10907 if (name[opend - 2] == '1' && name[opend - 1] == '6')
10908 forced_insn_length = 2;
10909 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
10910 forced_insn_length = 4;
10911 else
10912 break;
10913 memcpy (name + opend - 2, name + opend, end - opend + 1);
10914 }
252b5132
RH
10915 if (insn == NULL)
10916 {
a40bc9dd
RS
10917 insn_error = _("Unrecognized opcode");
10918 return;
252b5132
RH
10919 }
10920
df58fc94
RS
10921 /* For microMIPS instructions placed in a fixed-length branch delay slot
10922 we make up to two passes over the relevant fragment of the opcode
10923 table. First we try instructions that meet the delay slot's length
10924 requirement. If none matched, then we retry with the remaining ones
10925 and if one matches, then we use it and then issue an appropriate
10926 warning later on. */
a40bc9dd 10927 argsStart = s = str + end;
252b5132
RH
10928 for (;;)
10929 {
df58fc94
RS
10930 bfd_boolean delay_slot_ok;
10931 bfd_boolean size_ok;
b34976b6 10932 bfd_boolean ok;
252b5132 10933
a40bc9dd 10934 gas_assert (strcmp (insn->name, name) == 0);
252b5132 10935
f79e2745 10936 ok = is_opcode_valid (insn);
df58fc94
RS
10937 size_ok = is_size_valid (insn);
10938 delay_slot_ok = is_delay_slot_valid (insn);
10939 if (!delay_slot_ok && !wrong_delay_slot_insns)
252b5132 10940 {
df58fc94
RS
10941 firstinsn = insn;
10942 wrong_delay_slot_insns = TRUE;
10943 }
10944 if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
10945 {
10946 static char buf[256];
10947
10948 if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
252b5132
RH
10949 {
10950 ++insn;
10951 continue;
10952 }
df58fc94 10953 if (wrong_delay_slot_insns && need_delay_slot_ok)
beae10d5 10954 {
df58fc94
RS
10955 gas_assert (firstinsn);
10956 need_delay_slot_ok = FALSE;
10957 past = insn + 1;
10958 insn = firstinsn;
10959 continue;
252b5132 10960 }
df58fc94
RS
10961
10962 if (insn_error)
10963 return;
10964
10965 if (!ok)
7bd942df 10966 sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
df58fc94
RS
10967 mips_cpu_info_from_arch (mips_opts.arch)->name,
10968 mips_cpu_info_from_isa (mips_opts.isa)->name);
10969 else
10970 sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
10971 8 * forced_insn_length);
10972 insn_error = buf;
10973
10974 return;
252b5132
RH
10975 }
10976
1e915849 10977 create_insn (ip, insn);
268f6bed 10978 insn_error = NULL;
707bfff6 10979 argnum = 1;
24864476 10980 lastregno = 0xffffffff;
252b5132
RH
10981 for (args = insn->args;; ++args)
10982 {
deec1734
CD
10983 int is_mdmx;
10984
ad8d3bb3 10985 s += strspn (s, " \t");
deec1734 10986 is_mdmx = 0;
252b5132
RH
10987 switch (*args)
10988 {
10989 case '\0': /* end of args */
10990 if (*s == '\0')
10991 return;
10992 break;
10993
03f66e8a
MR
10994 case '2':
10995 /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
10996 code) or 14 (for microMIPS code). */
8b082fb1
TS
10997 my_getExpression (&imm_expr, s);
10998 check_absolute_expr (ip, &imm_expr);
10999 if ((unsigned long) imm_expr.X_add_number != 1
11000 && (unsigned long) imm_expr.X_add_number != 3)
11001 {
11002 as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
11003 (unsigned long) imm_expr.X_add_number);
11004 }
03f66e8a
MR
11005 INSERT_OPERAND (mips_opts.micromips,
11006 BP, *ip, imm_expr.X_add_number);
8b082fb1
TS
11007 imm_expr.X_op = O_absent;
11008 s = expr_end;
11009 continue;
11010
03f66e8a
MR
11011 case '3':
11012 /* DSP 3-bit unsigned immediate in bit 13 (for standard MIPS
11013 code) or 21 (for microMIPS code). */
11014 {
11015 unsigned long mask = (mips_opts.micromips
11016 ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
11017
11018 my_getExpression (&imm_expr, s);
11019 check_absolute_expr (ip, &imm_expr);
11020 if ((unsigned long) imm_expr.X_add_number > mask)
11021 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11022 mask, (unsigned long) imm_expr.X_add_number);
11023 INSERT_OPERAND (mips_opts.micromips,
11024 SA3, *ip, imm_expr.X_add_number);
11025 imm_expr.X_op = O_absent;
11026 s = expr_end;
11027 }
74cd071d
CF
11028 continue;
11029
03f66e8a
MR
11030 case '4':
11031 /* DSP 4-bit unsigned immediate in bit 12 (for standard MIPS
11032 code) or 21 (for microMIPS code). */
11033 {
11034 unsigned long mask = (mips_opts.micromips
11035 ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
11036
11037 my_getExpression (&imm_expr, s);
11038 check_absolute_expr (ip, &imm_expr);
11039 if ((unsigned long) imm_expr.X_add_number > mask)
11040 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11041 mask, (unsigned long) imm_expr.X_add_number);
11042 INSERT_OPERAND (mips_opts.micromips,
11043 SA4, *ip, imm_expr.X_add_number);
11044 imm_expr.X_op = O_absent;
11045 s = expr_end;
11046 }
74cd071d
CF
11047 continue;
11048
03f66e8a
MR
11049 case '5':
11050 /* DSP 8-bit unsigned immediate in bit 13 (for standard MIPS
11051 code) or 16 (for microMIPS code). */
11052 {
11053 unsigned long mask = (mips_opts.micromips
11054 ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
11055
11056 my_getExpression (&imm_expr, s);
11057 check_absolute_expr (ip, &imm_expr);
11058 if ((unsigned long) imm_expr.X_add_number > mask)
11059 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11060 mask, (unsigned long) imm_expr.X_add_number);
11061 INSERT_OPERAND (mips_opts.micromips,
11062 IMM8, *ip, imm_expr.X_add_number);
11063 imm_expr.X_op = O_absent;
11064 s = expr_end;
11065 }
74cd071d
CF
11066 continue;
11067
03f66e8a
MR
11068 case '6':
11069 /* DSP 5-bit unsigned immediate in bit 16 (for standard MIPS
11070 code) or 21 (for microMIPS code). */
11071 {
11072 unsigned long mask = (mips_opts.micromips
11073 ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
11074
11075 my_getExpression (&imm_expr, s);
11076 check_absolute_expr (ip, &imm_expr);
11077 if ((unsigned long) imm_expr.X_add_number > mask)
11078 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11079 mask, (unsigned long) imm_expr.X_add_number);
11080 INSERT_OPERAND (mips_opts.micromips,
11081 RS, *ip, imm_expr.X_add_number);
11082 imm_expr.X_op = O_absent;
11083 s = expr_end;
11084 }
74cd071d
CF
11085 continue;
11086
90ecf173 11087 case '7': /* Four DSP accumulators in bits 11,12. */
03f66e8a
MR
11088 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11089 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
11090 {
11091 regno = s[3] - '0';
11092 s += 4;
03f66e8a 11093 INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
74cd071d
CF
11094 continue;
11095 }
11096 else
11097 as_bad (_("Invalid dsp acc register"));
11098 break;
11099
03f66e8a
MR
11100 case '8':
11101 /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
11102 code) or 14 (for microMIPS code). */
11103 {
11104 unsigned long mask = (mips_opts.micromips
11105 ? MICROMIPSOP_MASK_WRDSP
11106 : OP_MASK_WRDSP);
11107
11108 my_getExpression (&imm_expr, s);
11109 check_absolute_expr (ip, &imm_expr);
11110 if ((unsigned long) imm_expr.X_add_number > mask)
11111 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11112 mask, (unsigned long) imm_expr.X_add_number);
11113 INSERT_OPERAND (mips_opts.micromips,
11114 WRDSP, *ip, imm_expr.X_add_number);
11115 imm_expr.X_op = O_absent;
11116 s = expr_end;
11117 }
74cd071d
CF
11118 continue;
11119
90ecf173 11120 case '9': /* Four DSP accumulators in bits 21,22. */
df58fc94 11121 gas_assert (!mips_opts.micromips);
03f66e8a
MR
11122 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11123 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
11124 {
11125 regno = s[3] - '0';
11126 s += 4;
df58fc94 11127 INSERT_OPERAND (0, DSPACC_S, *ip, regno);
74cd071d
CF
11128 continue;
11129 }
11130 else
11131 as_bad (_("Invalid dsp acc register"));
11132 break;
11133
03f66e8a
MR
11134 case '0':
11135 /* DSP 6-bit signed immediate in bit 16 (for standard MIPS
11136 code) or 20 (for microMIPS code). */
11137 {
11138 long mask = (mips_opts.micromips
11139 ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
11140
11141 my_getExpression (&imm_expr, s);
11142 check_absolute_expr (ip, &imm_expr);
11143 min_range = -((mask + 1) >> 1);
11144 max_range = ((mask + 1) >> 1) - 1;
11145 if (imm_expr.X_add_number < min_range
11146 || imm_expr.X_add_number > max_range)
a9e24354
TS
11147 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11148 (long) min_range, (long) max_range,
11149 (long) imm_expr.X_add_number);
03f66e8a
MR
11150 INSERT_OPERAND (mips_opts.micromips,
11151 DSPSFT, *ip, imm_expr.X_add_number);
11152 imm_expr.X_op = O_absent;
11153 s = expr_end;
11154 }
74cd071d
CF
11155 continue;
11156
90ecf173 11157 case '\'': /* DSP 6-bit unsigned immediate in bit 16. */
df58fc94 11158 gas_assert (!mips_opts.micromips);
74cd071d
CF
11159 my_getExpression (&imm_expr, s);
11160 check_absolute_expr (ip, &imm_expr);
11161 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
11162 {
a9e24354
TS
11163 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11164 OP_MASK_RDDSP,
11165 (unsigned long) imm_expr.X_add_number);
74cd071d 11166 }
df58fc94 11167 INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
11168 imm_expr.X_op = O_absent;
11169 s = expr_end;
11170 continue;
11171
90ecf173 11172 case ':': /* DSP 7-bit signed immediate in bit 19. */
df58fc94 11173 gas_assert (!mips_opts.micromips);
74cd071d
CF
11174 my_getExpression (&imm_expr, s);
11175 check_absolute_expr (ip, &imm_expr);
11176 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
11177 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
11178 if (imm_expr.X_add_number < min_range ||
11179 imm_expr.X_add_number > max_range)
11180 {
a9e24354
TS
11181 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11182 (long) min_range, (long) max_range,
11183 (long) imm_expr.X_add_number);
74cd071d 11184 }
df58fc94 11185 INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
74cd071d
CF
11186 imm_expr.X_op = O_absent;
11187 s = expr_end;
11188 continue;
11189
90ecf173 11190 case '@': /* DSP 10-bit signed immediate in bit 16. */
03f66e8a
MR
11191 {
11192 long mask = (mips_opts.micromips
11193 ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
11194
11195 my_getExpression (&imm_expr, s);
11196 check_absolute_expr (ip, &imm_expr);
11197 min_range = -((mask + 1) >> 1);
11198 max_range = ((mask + 1) >> 1) - 1;
11199 if (imm_expr.X_add_number < min_range
11200 || imm_expr.X_add_number > max_range)
a9e24354
TS
11201 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11202 (long) min_range, (long) max_range,
11203 (long) imm_expr.X_add_number);
03f66e8a
MR
11204 INSERT_OPERAND (mips_opts.micromips,
11205 IMM10, *ip, imm_expr.X_add_number);
11206 imm_expr.X_op = O_absent;
11207 s = expr_end;
11208 }
11209 continue;
11210
11211 case '^': /* DSP 5-bit unsigned immediate in bit 11. */
11212 gas_assert (mips_opts.micromips);
11213 my_getExpression (&imm_expr, s);
11214 check_absolute_expr (ip, &imm_expr);
11215 if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11216 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11217 MICROMIPSOP_MASK_RD,
11218 (unsigned long) imm_expr.X_add_number);
11219 INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
74cd071d
CF
11220 imm_expr.X_op = O_absent;
11221 s = expr_end;
11222 continue;
11223
a9e24354 11224 case '!': /* MT usermode flag bit. */
df58fc94 11225 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11226 my_getExpression (&imm_expr, s);
11227 check_absolute_expr (ip, &imm_expr);
11228 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
a9e24354
TS
11229 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11230 (unsigned long) imm_expr.X_add_number);
df58fc94 11231 INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11232 imm_expr.X_op = O_absent;
11233 s = expr_end;
11234 continue;
11235
a9e24354 11236 case '$': /* MT load high flag bit. */
df58fc94 11237 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11238 my_getExpression (&imm_expr, s);
11239 check_absolute_expr (ip, &imm_expr);
11240 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
a9e24354
TS
11241 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11242 (unsigned long) imm_expr.X_add_number);
df58fc94 11243 INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11244 imm_expr.X_op = O_absent;
11245 s = expr_end;
11246 continue;
11247
90ecf173 11248 case '*': /* Four DSP accumulators in bits 18,19. */
df58fc94 11249 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11250 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11251 s[3] >= '0' && s[3] <= '3')
11252 {
11253 regno = s[3] - '0';
11254 s += 4;
df58fc94 11255 INSERT_OPERAND (0, MTACC_T, *ip, regno);
ef2e4d86
CF
11256 continue;
11257 }
11258 else
11259 as_bad (_("Invalid dsp/smartmips acc register"));
11260 break;
11261
90ecf173 11262 case '&': /* Four DSP accumulators in bits 13,14. */
df58fc94 11263 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11264 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11265 s[3] >= '0' && s[3] <= '3')
11266 {
11267 regno = s[3] - '0';
11268 s += 4;
df58fc94 11269 INSERT_OPERAND (0, MTACC_D, *ip, regno);
ef2e4d86
CF
11270 continue;
11271 }
11272 else
11273 as_bad (_("Invalid dsp/smartmips acc register"));
11274 break;
11275
dec0624d
MR
11276 case '\\': /* 3-bit bit position. */
11277 {
2906b037
MR
11278 unsigned long mask = (mips_opts.micromips
11279 ? MICROMIPSOP_MASK_3BITPOS
11280 : OP_MASK_3BITPOS);
dec0624d
MR
11281
11282 my_getExpression (&imm_expr, s);
11283 check_absolute_expr (ip, &imm_expr);
11284 if ((unsigned long) imm_expr.X_add_number > mask)
11285 as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11286 ip->insn_mo->name,
11287 mask, (unsigned long) imm_expr.X_add_number);
11288 INSERT_OPERAND (mips_opts.micromips,
11289 3BITPOS, *ip, imm_expr.X_add_number);
11290 imm_expr.X_op = O_absent;
11291 s = expr_end;
11292 }
11293 continue;
11294
252b5132 11295 case ',':
a339155f 11296 ++argnum;
252b5132
RH
11297 if (*s++ == *args)
11298 continue;
11299 s--;
11300 switch (*++args)
11301 {
11302 case 'r':
11303 case 'v':
df58fc94 11304 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132
RH
11305 continue;
11306
11307 case 'w':
df58fc94 11308 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
38487616
TS
11309 continue;
11310
252b5132 11311 case 'W':
df58fc94
RS
11312 gas_assert (!mips_opts.micromips);
11313 INSERT_OPERAND (0, FT, *ip, lastregno);
252b5132
RH
11314 continue;
11315
11316 case 'V':
df58fc94 11317 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132
RH
11318 continue;
11319 }
11320 break;
11321
11322 case '(':
11323 /* Handle optional base register.
11324 Either the base register is omitted or
bdaaa2e1 11325 we must have a left paren. */
252b5132
RH
11326 /* This is dependent on the next operand specifier
11327 is a base register specification. */
df58fc94
RS
11328 gas_assert (args[1] == 'b'
11329 || (mips_opts.micromips
11330 && args[1] == 'm'
11331 && (args[2] == 'l' || args[2] == 'n'
11332 || args[2] == 's' || args[2] == 'a')));
11333 if (*s == '\0' && args[1] == 'b')
252b5132 11334 return;
df58fc94 11335 /* Fall through. */
252b5132 11336
90ecf173 11337 case ')': /* These must match exactly. */
df58fc94
RS
11338 if (*s++ == *args)
11339 continue;
11340 break;
11341
11342 case '[': /* These must match exactly. */
60b63b72 11343 case ']':
df58fc94 11344 gas_assert (!mips_opts.micromips);
252b5132
RH
11345 if (*s++ == *args)
11346 continue;
11347 break;
11348
af7ee8bf
CD
11349 case '+': /* Opcode extension character. */
11350 switch (*++args)
11351 {
9bcd4f99
TS
11352 case '1': /* UDI immediates. */
11353 case '2':
11354 case '3':
11355 case '4':
df58fc94 11356 gas_assert (!mips_opts.micromips);
9bcd4f99
TS
11357 {
11358 const struct mips_immed *imm = mips_immed;
11359
11360 while (imm->type && imm->type != *args)
11361 ++imm;
11362 if (! imm->type)
b37df7c4 11363 abort ();
9bcd4f99
TS
11364 my_getExpression (&imm_expr, s);
11365 check_absolute_expr (ip, &imm_expr);
11366 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11367 {
11368 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11369 imm->desc ? imm->desc : ip->insn_mo->name,
11370 (unsigned long) imm_expr.X_add_number,
11371 (unsigned long) imm_expr.X_add_number);
90ecf173 11372 imm_expr.X_add_number &= imm->mask;
9bcd4f99
TS
11373 }
11374 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11375 << imm->shift);
11376 imm_expr.X_op = O_absent;
11377 s = expr_end;
11378 }
11379 continue;
90ecf173 11380
b015e599
AP
11381 case 'J': /* 10-bit hypcall code. */
11382 gas_assert (!mips_opts.micromips);
11383 {
11384 unsigned long mask = OP_MASK_CODE10;
11385
11386 my_getExpression (&imm_expr, s);
11387 check_absolute_expr (ip, &imm_expr);
11388 if ((unsigned long) imm_expr.X_add_number > mask)
11389 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11390 ip->insn_mo->name,
11391 mask, (unsigned long) imm_expr.X_add_number);
11392 INSERT_OPERAND (0, CODE10, *ip, imm_expr.X_add_number);
11393 imm_expr.X_op = O_absent;
11394 s = expr_end;
11395 }
11396 continue;
11397
071742cf
CD
11398 case 'A': /* ins/ext position, becomes LSB. */
11399 limlo = 0;
11400 limhi = 31;
5f74bc13
CD
11401 goto do_lsb;
11402 case 'E':
11403 limlo = 32;
11404 limhi = 63;
11405 goto do_lsb;
90ecf173 11406 do_lsb:
071742cf
CD
11407 my_getExpression (&imm_expr, s);
11408 check_absolute_expr (ip, &imm_expr);
11409 if ((unsigned long) imm_expr.X_add_number < limlo
11410 || (unsigned long) imm_expr.X_add_number > limhi)
11411 {
11412 as_bad (_("Improper position (%lu)"),
11413 (unsigned long) imm_expr.X_add_number);
11414 imm_expr.X_add_number = limlo;
11415 }
11416 lastpos = imm_expr.X_add_number;
df58fc94
RS
11417 INSERT_OPERAND (mips_opts.micromips,
11418 EXTLSB, *ip, imm_expr.X_add_number);
071742cf
CD
11419 imm_expr.X_op = O_absent;
11420 s = expr_end;
11421 continue;
11422
11423 case 'B': /* ins size, becomes MSB. */
11424 limlo = 1;
11425 limhi = 32;
5f74bc13
CD
11426 goto do_msb;
11427 case 'F':
11428 limlo = 33;
11429 limhi = 64;
11430 goto do_msb;
90ecf173 11431 do_msb:
071742cf
CD
11432 my_getExpression (&imm_expr, s);
11433 check_absolute_expr (ip, &imm_expr);
11434 /* Check for negative input so that small negative numbers
11435 will not succeed incorrectly. The checks against
11436 (pos+size) transitively check "size" itself,
11437 assuming that "pos" is reasonable. */
11438 if ((long) imm_expr.X_add_number < 0
11439 || ((unsigned long) imm_expr.X_add_number
11440 + lastpos) < limlo
11441 || ((unsigned long) imm_expr.X_add_number
11442 + lastpos) > limhi)
11443 {
11444 as_bad (_("Improper insert size (%lu, position %lu)"),
11445 (unsigned long) imm_expr.X_add_number,
11446 (unsigned long) lastpos);
11447 imm_expr.X_add_number = limlo - lastpos;
11448 }
df58fc94
RS
11449 INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11450 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
11451 imm_expr.X_op = O_absent;
11452 s = expr_end;
11453 continue;
11454
11455 case 'C': /* ext size, becomes MSBD. */
11456 limlo = 1;
11457 limhi = 32;
f02d8318 11458 sizelo = 1;
5f74bc13
CD
11459 goto do_msbd;
11460 case 'G':
11461 limlo = 33;
11462 limhi = 64;
f02d8318 11463 sizelo = 33;
5f74bc13
CD
11464 goto do_msbd;
11465 case 'H':
11466 limlo = 33;
11467 limhi = 64;
f02d8318 11468 sizelo = 1;
5f74bc13 11469 goto do_msbd;
90ecf173 11470 do_msbd:
071742cf
CD
11471 my_getExpression (&imm_expr, s);
11472 check_absolute_expr (ip, &imm_expr);
f02d8318
CF
11473 /* The checks against (pos+size) don't transitively check
11474 "size" itself, assuming that "pos" is reasonable.
11475 We also need to check the lower bound of "size". */
11476 if ((long) imm_expr.X_add_number < sizelo
071742cf
CD
11477 || ((unsigned long) imm_expr.X_add_number
11478 + lastpos) < limlo
11479 || ((unsigned long) imm_expr.X_add_number
11480 + lastpos) > limhi)
11481 {
11482 as_bad (_("Improper extract size (%lu, position %lu)"),
11483 (unsigned long) imm_expr.X_add_number,
11484 (unsigned long) lastpos);
11485 imm_expr.X_add_number = limlo - lastpos;
11486 }
df58fc94
RS
11487 INSERT_OPERAND (mips_opts.micromips,
11488 EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
11489 imm_expr.X_op = O_absent;
11490 s = expr_end;
11491 continue;
af7ee8bf 11492
bbcc0807
CD
11493 case 'D':
11494 /* +D is for disassembly only; never match. */
11495 break;
11496
5f74bc13
CD
11497 case 'I':
11498 /* "+I" is like "I", except that imm2_expr is used. */
11499 my_getExpression (&imm2_expr, s);
11500 if (imm2_expr.X_op != O_big
11501 && imm2_expr.X_op != O_constant)
11502 insn_error = _("absolute expression required");
9ee2a2d4
MR
11503 if (HAVE_32BIT_GPRS)
11504 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
11505 s = expr_end;
11506 continue;
11507
707bfff6 11508 case 'T': /* Coprocessor register. */
df58fc94 11509 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11510 /* +T is for disassembly only; never match. */
11511 break;
11512
707bfff6 11513 case 't': /* Coprocessor register number. */
df58fc94 11514 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11515 if (s[0] == '$' && ISDIGIT (s[1]))
11516 {
11517 ++s;
11518 regno = 0;
11519 do
11520 {
11521 regno *= 10;
11522 regno += *s - '0';
11523 ++s;
11524 }
11525 while (ISDIGIT (*s));
11526 if (regno > 31)
11527 as_bad (_("Invalid register number (%d)"), regno);
11528 else
11529 {
df58fc94 11530 INSERT_OPERAND (0, RT, *ip, regno);
ef2e4d86
CF
11531 continue;
11532 }
11533 }
11534 else
11535 as_bad (_("Invalid coprocessor 0 register number"));
11536 break;
11537
bb35fb24
NC
11538 case 'x':
11539 /* bbit[01] and bbit[01]32 bit index. Give error if index
11540 is not in the valid range. */
df58fc94 11541 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11542 my_getExpression (&imm_expr, s);
11543 check_absolute_expr (ip, &imm_expr);
11544 if ((unsigned) imm_expr.X_add_number > 31)
11545 {
11546 as_bad (_("Improper bit index (%lu)"),
11547 (unsigned long) imm_expr.X_add_number);
11548 imm_expr.X_add_number = 0;
11549 }
df58fc94 11550 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
bb35fb24
NC
11551 imm_expr.X_op = O_absent;
11552 s = expr_end;
11553 continue;
11554
11555 case 'X':
11556 /* bbit[01] bit index when bbit is used but we generate
11557 bbit[01]32 because the index is over 32. Move to the
11558 next candidate if index is not in the valid range. */
df58fc94 11559 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11560 my_getExpression (&imm_expr, s);
11561 check_absolute_expr (ip, &imm_expr);
11562 if ((unsigned) imm_expr.X_add_number < 32
11563 || (unsigned) imm_expr.X_add_number > 63)
11564 break;
df58fc94 11565 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
11566 imm_expr.X_op = O_absent;
11567 s = expr_end;
11568 continue;
11569
11570 case 'p':
11571 /* cins, cins32, exts and exts32 position field. Give error
11572 if it's not in the valid range. */
df58fc94 11573 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11574 my_getExpression (&imm_expr, s);
11575 check_absolute_expr (ip, &imm_expr);
11576 if ((unsigned) imm_expr.X_add_number > 31)
11577 {
11578 as_bad (_("Improper position (%lu)"),
11579 (unsigned long) imm_expr.X_add_number);
11580 imm_expr.X_add_number = 0;
11581 }
11582 /* Make the pos explicit to simplify +S. */
11583 lastpos = imm_expr.X_add_number + 32;
df58fc94 11584 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
bb35fb24
NC
11585 imm_expr.X_op = O_absent;
11586 s = expr_end;
11587 continue;
11588
11589 case 'P':
11590 /* cins, cins32, exts and exts32 position field. Move to
11591 the next candidate if it's not in the valid range. */
df58fc94 11592 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11593 my_getExpression (&imm_expr, s);
11594 check_absolute_expr (ip, &imm_expr);
11595 if ((unsigned) imm_expr.X_add_number < 32
11596 || (unsigned) imm_expr.X_add_number > 63)
11597 break;
11598 lastpos = imm_expr.X_add_number;
df58fc94 11599 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
11600 imm_expr.X_op = O_absent;
11601 s = expr_end;
11602 continue;
11603
11604 case 's':
11605 /* cins and exts length-minus-one field. */
df58fc94 11606 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11607 my_getExpression (&imm_expr, s);
11608 check_absolute_expr (ip, &imm_expr);
11609 if ((unsigned long) imm_expr.X_add_number > 31)
11610 {
11611 as_bad (_("Improper size (%lu)"),
11612 (unsigned long) imm_expr.X_add_number);
11613 imm_expr.X_add_number = 0;
11614 }
df58fc94 11615 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
11616 imm_expr.X_op = O_absent;
11617 s = expr_end;
11618 continue;
11619
11620 case 'S':
11621 /* cins32/exts32 and cins/exts aliasing cint32/exts32
11622 length-minus-one field. */
df58fc94 11623 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11624 my_getExpression (&imm_expr, s);
11625 check_absolute_expr (ip, &imm_expr);
11626 if ((long) imm_expr.X_add_number < 0
11627 || (unsigned long) imm_expr.X_add_number + lastpos > 63)
11628 {
11629 as_bad (_("Improper size (%lu)"),
11630 (unsigned long) imm_expr.X_add_number);
11631 imm_expr.X_add_number = 0;
11632 }
df58fc94 11633 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
11634 imm_expr.X_op = O_absent;
11635 s = expr_end;
11636 continue;
11637
dd3cbb7e
NC
11638 case 'Q':
11639 /* seqi/snei immediate field. */
df58fc94 11640 gas_assert (!mips_opts.micromips);
dd3cbb7e
NC
11641 my_getExpression (&imm_expr, s);
11642 check_absolute_expr (ip, &imm_expr);
11643 if ((long) imm_expr.X_add_number < -512
11644 || (long) imm_expr.X_add_number >= 512)
11645 {
11646 as_bad (_("Improper immediate (%ld)"),
11647 (long) imm_expr.X_add_number);
11648 imm_expr.X_add_number = 0;
11649 }
df58fc94 11650 INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
dd3cbb7e
NC
11651 imm_expr.X_op = O_absent;
11652 s = expr_end;
11653 continue;
11654
98675402 11655 case 'a': /* 8-bit signed offset in bit 6 */
df58fc94 11656 gas_assert (!mips_opts.micromips);
98675402
RS
11657 my_getExpression (&imm_expr, s);
11658 check_absolute_expr (ip, &imm_expr);
11659 min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
11660 max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
11661 if (imm_expr.X_add_number < min_range
11662 || imm_expr.X_add_number > max_range)
11663 {
c95354ed 11664 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11665 (long) min_range, (long) max_range,
11666 (long) imm_expr.X_add_number);
11667 }
df58fc94 11668 INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
98675402
RS
11669 imm_expr.X_op = O_absent;
11670 s = expr_end;
11671 continue;
11672
11673 case 'b': /* 8-bit signed offset in bit 3 */
df58fc94 11674 gas_assert (!mips_opts.micromips);
98675402
RS
11675 my_getExpression (&imm_expr, s);
11676 check_absolute_expr (ip, &imm_expr);
11677 min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
11678 max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
11679 if (imm_expr.X_add_number < min_range
11680 || imm_expr.X_add_number > max_range)
11681 {
c95354ed 11682 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11683 (long) min_range, (long) max_range,
11684 (long) imm_expr.X_add_number);
11685 }
df58fc94 11686 INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
98675402
RS
11687 imm_expr.X_op = O_absent;
11688 s = expr_end;
11689 continue;
11690
11691 case 'c': /* 9-bit signed offset in bit 6 */
df58fc94 11692 gas_assert (!mips_opts.micromips);
98675402
RS
11693 my_getExpression (&imm_expr, s);
11694 check_absolute_expr (ip, &imm_expr);
11695 min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
11696 max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
c95354ed
MX
11697 /* We check the offset range before adjusted. */
11698 min_range <<= 4;
11699 max_range <<= 4;
98675402
RS
11700 if (imm_expr.X_add_number < min_range
11701 || imm_expr.X_add_number > max_range)
11702 {
c95354ed 11703 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11704 (long) min_range, (long) max_range,
11705 (long) imm_expr.X_add_number);
11706 }
c95354ed
MX
11707 if (imm_expr.X_add_number & 0xf)
11708 {
11709 as_bad (_("Offset not 16 bytes alignment (%ld)"),
11710 (long) imm_expr.X_add_number);
11711 }
11712 /* Right shift 4 bits to adjust the offset operand. */
df58fc94
RS
11713 INSERT_OPERAND (0, OFFSET_C, *ip,
11714 imm_expr.X_add_number >> 4);
98675402
RS
11715 imm_expr.X_op = O_absent;
11716 s = expr_end;
11717 continue;
11718
11719 case 'z':
df58fc94 11720 gas_assert (!mips_opts.micromips);
98675402
RS
11721 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
11722 break;
11723 if (regno == AT && mips_opts.at)
11724 {
11725 if (mips_opts.at == ATREG)
11726 as_warn (_("used $at without \".set noat\""));
11727 else
11728 as_warn (_("used $%u with \".set at=$%u\""),
11729 regno, mips_opts.at);
11730 }
df58fc94 11731 INSERT_OPERAND (0, RZ, *ip, regno);
98675402
RS
11732 continue;
11733
11734 case 'Z':
df58fc94 11735 gas_assert (!mips_opts.micromips);
98675402
RS
11736 if (!reg_lookup (&s, RTYPE_FPU, &regno))
11737 break;
df58fc94 11738 INSERT_OPERAND (0, FZ, *ip, regno);
98675402
RS
11739 continue;
11740
af7ee8bf 11741 default:
df58fc94 11742 as_bad (_("Internal error: bad %s opcode "
90ecf173 11743 "(unknown extension operand type `+%c'): %s %s"),
df58fc94 11744 mips_opts.micromips ? "microMIPS" : "MIPS",
90ecf173 11745 *args, insn->name, insn->args);
af7ee8bf
CD
11746 /* Further processing is fruitless. */
11747 return;
11748 }
11749 break;
11750
df58fc94 11751 case '.': /* 10-bit offset. */
df58fc94 11752 gas_assert (mips_opts.micromips);
dec0624d 11753 case '~': /* 12-bit offset. */
df58fc94
RS
11754 {
11755 int shift = *args == '.' ? 9 : 11;
11756 size_t i;
11757
11758 /* Check whether there is only a single bracketed expression
11759 left. If so, it must be the base register and the
11760 constant must be zero. */
11761 if (*s == '(' && strchr (s + 1, '(') == 0)
11762 continue;
11763
11764 /* If this value won't fit into the offset, then go find
11765 a macro that will generate a 16- or 32-bit offset code
11766 pattern. */
11767 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11768 if ((i == 0 && (imm_expr.X_op != O_constant
11769 || imm_expr.X_add_number >= 1 << shift
11770 || imm_expr.X_add_number < -1 << shift))
11771 || i > 0)
11772 {
11773 imm_expr.X_op = O_absent;
11774 break;
11775 }
11776 if (shift == 9)
11777 INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
11778 else
dec0624d
MR
11779 INSERT_OPERAND (mips_opts.micromips,
11780 OFFSET12, *ip, imm_expr.X_add_number);
df58fc94
RS
11781 imm_expr.X_op = O_absent;
11782 s = expr_end;
11783 }
11784 continue;
11785
252b5132
RH
11786 case '<': /* must be at least one digit */
11787 /*
11788 * According to the manual, if the shift amount is greater
b6ff326e
KH
11789 * than 31 or less than 0, then the shift amount should be
11790 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
11791 * We issue a warning and mask out all but the low 5 bits.
11792 */
11793 my_getExpression (&imm_expr, s);
11794 check_absolute_expr (ip, &imm_expr);
11795 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
11796 as_warn (_("Improper shift amount (%lu)"),
11797 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11798 INSERT_OPERAND (mips_opts.micromips,
11799 SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
11800 imm_expr.X_op = O_absent;
11801 s = expr_end;
11802 continue;
11803
11804 case '>': /* shift amount minus 32 */
11805 my_getExpression (&imm_expr, s);
11806 check_absolute_expr (ip, &imm_expr);
11807 if ((unsigned long) imm_expr.X_add_number < 32
11808 || (unsigned long) imm_expr.X_add_number > 63)
11809 break;
df58fc94
RS
11810 INSERT_OPERAND (mips_opts.micromips,
11811 SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
11812 imm_expr.X_op = O_absent;
11813 s = expr_end;
11814 continue;
11815
90ecf173
MR
11816 case 'k': /* CACHE code. */
11817 case 'h': /* PREFX code. */
11818 case '1': /* SYNC type. */
252b5132
RH
11819 my_getExpression (&imm_expr, s);
11820 check_absolute_expr (ip, &imm_expr);
11821 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
11822 as_warn (_("Invalid value for `%s' (%lu)"),
11823 ip->insn_mo->name,
11824 (unsigned long) imm_expr.X_add_number);
df58fc94 11825 switch (*args)
d954098f 11826 {
df58fc94
RS
11827 case 'k':
11828 if (mips_fix_cn63xxp1
11829 && !mips_opts.micromips
11830 && strcmp ("pref", insn->name) == 0)
d954098f
DD
11831 switch (imm_expr.X_add_number)
11832 {
11833 case 5:
11834 case 25:
11835 case 26:
11836 case 27:
11837 case 28:
11838 case 29:
11839 case 30:
11840 case 31: /* These are ok. */
11841 break;
11842
11843 default: /* The rest must be changed to 28. */
11844 imm_expr.X_add_number = 28;
11845 break;
11846 }
df58fc94
RS
11847 INSERT_OPERAND (mips_opts.micromips,
11848 CACHE, *ip, imm_expr.X_add_number);
11849 break;
11850 case 'h':
11851 INSERT_OPERAND (mips_opts.micromips,
11852 PREFX, *ip, imm_expr.X_add_number);
11853 break;
11854 case '1':
11855 INSERT_OPERAND (mips_opts.micromips,
11856 STYPE, *ip, imm_expr.X_add_number);
11857 break;
d954098f 11858 }
252b5132
RH
11859 imm_expr.X_op = O_absent;
11860 s = expr_end;
11861 continue;
11862
90ecf173 11863 case 'c': /* BREAK code. */
df58fc94
RS
11864 {
11865 unsigned long mask = (mips_opts.micromips
11866 ? MICROMIPSOP_MASK_CODE
11867 : OP_MASK_CODE);
11868
11869 my_getExpression (&imm_expr, s);
11870 check_absolute_expr (ip, &imm_expr);
11871 if ((unsigned long) imm_expr.X_add_number > mask)
11872 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11873 ip->insn_mo->name,
11874 mask, (unsigned long) imm_expr.X_add_number);
11875 INSERT_OPERAND (mips_opts.micromips,
11876 CODE, *ip, imm_expr.X_add_number);
11877 imm_expr.X_op = O_absent;
11878 s = expr_end;
11879 }
252b5132
RH
11880 continue;
11881
90ecf173 11882 case 'q': /* Lower BREAK code. */
df58fc94
RS
11883 {
11884 unsigned long mask = (mips_opts.micromips
11885 ? MICROMIPSOP_MASK_CODE2
11886 : OP_MASK_CODE2);
11887
11888 my_getExpression (&imm_expr, s);
11889 check_absolute_expr (ip, &imm_expr);
11890 if ((unsigned long) imm_expr.X_add_number > mask)
11891 as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
11892 ip->insn_mo->name,
11893 mask, (unsigned long) imm_expr.X_add_number);
11894 INSERT_OPERAND (mips_opts.micromips,
11895 CODE2, *ip, imm_expr.X_add_number);
11896 imm_expr.X_op = O_absent;
11897 s = expr_end;
11898 }
252b5132
RH
11899 continue;
11900
df58fc94
RS
11901 case 'B': /* 20- or 10-bit syscall/break/wait code. */
11902 {
11903 unsigned long mask = (mips_opts.micromips
11904 ? MICROMIPSOP_MASK_CODE10
11905 : OP_MASK_CODE20);
11906
11907 my_getExpression (&imm_expr, s);
11908 check_absolute_expr (ip, &imm_expr);
11909 if ((unsigned long) imm_expr.X_add_number > mask)
11910 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11911 ip->insn_mo->name,
11912 mask, (unsigned long) imm_expr.X_add_number);
11913 if (mips_opts.micromips)
11914 INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
11915 else
11916 INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
11917 imm_expr.X_op = O_absent;
11918 s = expr_end;
11919 }
252b5132
RH
11920 continue;
11921
df58fc94
RS
11922 case 'C': /* 25- or 23-bit coprocessor code. */
11923 {
11924 unsigned long mask = (mips_opts.micromips
11925 ? MICROMIPSOP_MASK_COPZ
11926 : OP_MASK_COPZ);
11927
11928 my_getExpression (&imm_expr, s);
11929 check_absolute_expr (ip, &imm_expr);
11930 if ((unsigned long) imm_expr.X_add_number > mask)
11931 as_warn (_("Coproccesor code > %u bits (%lu)"),
11932 mips_opts.micromips ? 23U : 25U,
793b27f4 11933 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11934 INSERT_OPERAND (mips_opts.micromips,
11935 COPZ, *ip, imm_expr.X_add_number);
11936 imm_expr.X_op = O_absent;
11937 s = expr_end;
11938 }
beae10d5 11939 continue;
252b5132 11940
df58fc94
RS
11941 case 'J': /* 19-bit WAIT code. */
11942 gas_assert (!mips_opts.micromips);
4372b673
NC
11943 my_getExpression (&imm_expr, s);
11944 check_absolute_expr (ip, &imm_expr);
793b27f4 11945 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
df58fc94
RS
11946 {
11947 as_warn (_("Illegal 19-bit code (%lu)"),
a9e24354 11948 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11949 imm_expr.X_add_number &= OP_MASK_CODE19;
11950 }
11951 INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
11952 imm_expr.X_op = O_absent;
11953 s = expr_end;
11954 continue;
11955
707bfff6 11956 case 'P': /* Performance register. */
df58fc94 11957 gas_assert (!mips_opts.micromips);
beae10d5 11958 my_getExpression (&imm_expr, s);
252b5132 11959 check_absolute_expr (ip, &imm_expr);
beae10d5 11960 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
11961 as_warn (_("Invalid performance register (%lu)"),
11962 (unsigned long) imm_expr.X_add_number);
e407c74b
NC
11963 if (imm_expr.X_add_number != 0 && mips_opts.arch == CPU_R5900
11964 && (!strcmp(insn->name,"mfps") || !strcmp(insn->name,"mtps")))
11965 as_warn (_("Invalid performance register (%lu)"),
11966 (unsigned long) imm_expr.X_add_number);
df58fc94 11967 INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
11968 imm_expr.X_op = O_absent;
11969 s = expr_end;
11970 continue;
252b5132 11971
707bfff6 11972 case 'G': /* Coprocessor destination register. */
df58fc94
RS
11973 {
11974 unsigned long opcode = ip->insn_opcode;
11975 unsigned long mask;
11976 unsigned int types;
11977 int cop0;
11978
11979 if (mips_opts.micromips)
11980 {
11981 mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
11982 | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
11983 | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
11984 opcode &= mask;
11985 switch (opcode)
11986 {
11987 case 0x000000fc: /* mfc0 */
11988 case 0x000002fc: /* mtc0 */
11989 case 0x580000fc: /* dmfc0 */
11990 case 0x580002fc: /* dmtc0 */
11991 cop0 = 1;
11992 break;
11993 default:
11994 cop0 = 0;
11995 break;
11996 }
11997 }
11998 else
11999 {
12000 opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
12001 cop0 = opcode == OP_OP_COP0;
12002 }
12003 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
12004 ok = reg_lookup (&s, types, &regno);
12005 if (mips_opts.micromips)
12006 INSERT_OPERAND (1, RS, *ip, regno);
12007 else
12008 INSERT_OPERAND (0, RD, *ip, regno);
12009 if (ok)
12010 {
12011 lastregno = regno;
12012 continue;
12013 }
12014 }
12015 break;
707bfff6 12016
df58fc94
RS
12017 case 'y': /* ALNV.PS source register. */
12018 gas_assert (mips_opts.micromips);
12019 goto do_reg;
12020 case 'x': /* Ignore register name. */
12021 case 'U': /* Destination register (CLO/CLZ). */
12022 case 'g': /* Coprocessor destination register. */
12023 gas_assert (!mips_opts.micromips);
90ecf173
MR
12024 case 'b': /* Base register. */
12025 case 'd': /* Destination register. */
12026 case 's': /* Source register. */
12027 case 't': /* Target register. */
12028 case 'r': /* Both target and source. */
12029 case 'v': /* Both dest and source. */
12030 case 'w': /* Both dest and target. */
12031 case 'E': /* Coprocessor target register. */
12032 case 'K': /* RDHWR destination register. */
90ecf173 12033 case 'z': /* Must be zero register. */
df58fc94 12034 do_reg:
90ecf173 12035 s_reset = s;
707bfff6
TS
12036 if (*args == 'E' || *args == 'K')
12037 ok = reg_lookup (&s, RTYPE_NUM, &regno);
12038 else
12039 {
12040 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
741fe287
MR
12041 if (regno == AT && mips_opts.at)
12042 {
12043 if (mips_opts.at == ATREG)
f71d0d44 12044 as_warn (_("Used $at without \".set noat\""));
741fe287 12045 else
f71d0d44 12046 as_warn (_("Used $%u with \".set at=$%u\""),
741fe287
MR
12047 regno, mips_opts.at);
12048 }
707bfff6
TS
12049 }
12050 if (ok)
252b5132 12051 {
252b5132
RH
12052 c = *args;
12053 if (*s == ' ')
f9419b05 12054 ++s;
252b5132
RH
12055 if (args[1] != *s)
12056 {
12057 if (c == 'r' || c == 'v' || c == 'w')
12058 {
12059 regno = lastregno;
12060 s = s_reset;
f9419b05 12061 ++args;
252b5132
RH
12062 }
12063 }
12064 /* 'z' only matches $0. */
12065 if (c == 'z' && regno != 0)
12066 break;
12067
24864476 12068 if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
e7c604dd
CM
12069 {
12070 if (regno == lastregno)
90ecf173
MR
12071 {
12072 insn_error
f71d0d44 12073 = _("Source and destination must be different");
e7c604dd 12074 continue;
90ecf173 12075 }
24864476 12076 if (regno == 31 && lastregno == 0xffffffff)
90ecf173
MR
12077 {
12078 insn_error
f71d0d44 12079 = _("A destination register must be supplied");
e7c604dd 12080 continue;
90ecf173 12081 }
e7c604dd 12082 }
90ecf173
MR
12083 /* Now that we have assembled one operand, we use the args
12084 string to figure out where it goes in the instruction. */
252b5132
RH
12085 switch (c)
12086 {
12087 case 'r':
12088 case 's':
12089 case 'v':
12090 case 'b':
df58fc94 12091 INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
252b5132 12092 break;
df58fc94 12093
af7ee8bf 12094 case 'K':
df58fc94
RS
12095 if (mips_opts.micromips)
12096 INSERT_OPERAND (1, RS, *ip, regno);
12097 else
12098 INSERT_OPERAND (0, RD, *ip, regno);
12099 break;
12100
12101 case 'd':
ef2e4d86 12102 case 'g':
df58fc94 12103 INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
252b5132 12104 break;
df58fc94 12105
4372b673 12106 case 'U':
df58fc94
RS
12107 gas_assert (!mips_opts.micromips);
12108 INSERT_OPERAND (0, RD, *ip, regno);
12109 INSERT_OPERAND (0, RT, *ip, regno);
4372b673 12110 break;
df58fc94 12111
252b5132
RH
12112 case 'w':
12113 case 't':
12114 case 'E':
df58fc94
RS
12115 INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
12116 break;
12117
12118 case 'y':
12119 gas_assert (mips_opts.micromips);
12120 INSERT_OPERAND (1, RS3, *ip, regno);
252b5132 12121 break;
df58fc94 12122
252b5132
RH
12123 case 'x':
12124 /* This case exists because on the r3000 trunc
12125 expands into a macro which requires a gp
12126 register. On the r6000 or r4000 it is
12127 assembled into a single instruction which
12128 ignores the register. Thus the insn version
12129 is MIPS_ISA2 and uses 'x', and the macro
12130 version is MIPS_ISA1 and uses 't'. */
12131 break;
df58fc94 12132
252b5132
RH
12133 case 'z':
12134 /* This case is for the div instruction, which
12135 acts differently if the destination argument
12136 is $0. This only matches $0, and is checked
12137 outside the switch. */
12138 break;
252b5132
RH
12139 }
12140 lastregno = regno;
12141 continue;
12142 }
252b5132
RH
12143 switch (*args++)
12144 {
12145 case 'r':
12146 case 'v':
df58fc94 12147 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132 12148 continue;
df58fc94 12149
252b5132 12150 case 'w':
df58fc94 12151 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
252b5132
RH
12152 continue;
12153 }
12154 break;
12155
deec1734 12156 case 'O': /* MDMX alignment immediate constant. */
df58fc94 12157 gas_assert (!mips_opts.micromips);
deec1734
CD
12158 my_getExpression (&imm_expr, s);
12159 check_absolute_expr (ip, &imm_expr);
12160 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
20203fb9 12161 as_warn (_("Improper align amount (%ld), using low bits"),
bf12938e 12162 (long) imm_expr.X_add_number);
df58fc94 12163 INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
deec1734
CD
12164 imm_expr.X_op = O_absent;
12165 s = expr_end;
12166 continue;
12167
12168 case 'Q': /* MDMX vector, element sel, or const. */
12169 if (s[0] != '$')
12170 {
12171 /* MDMX Immediate. */
df58fc94 12172 gas_assert (!mips_opts.micromips);
deec1734
CD
12173 my_getExpression (&imm_expr, s);
12174 check_absolute_expr (ip, &imm_expr);
12175 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
12176 as_warn (_("Invalid MDMX Immediate (%ld)"),
12177 (long) imm_expr.X_add_number);
df58fc94 12178 INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
deec1734
CD
12179 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12180 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12181 else
12182 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
12183 imm_expr.X_op = O_absent;
12184 s = expr_end;
12185 continue;
12186 }
12187 /* Not MDMX Immediate. Fall through. */
12188 case 'X': /* MDMX destination register. */
12189 case 'Y': /* MDMX source register. */
12190 case 'Z': /* MDMX target register. */
12191 is_mdmx = 1;
df58fc94
RS
12192 case 'W':
12193 gas_assert (!mips_opts.micromips);
90ecf173
MR
12194 case 'D': /* Floating point destination register. */
12195 case 'S': /* Floating point source register. */
12196 case 'T': /* Floating point target register. */
12197 case 'R': /* Floating point source register. */
252b5132 12198 case 'V':
707bfff6
TS
12199 rtype = RTYPE_FPU;
12200 if (is_mdmx
12201 || (mips_opts.ase_mdmx
12202 && (ip->insn_mo->pinfo & FP_D)
12203 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12204 | INSN_COPROC_MEMORY_DELAY
12205 | INSN_LOAD_COPROC_DELAY
12206 | INSN_LOAD_MEMORY_DELAY
12207 | INSN_STORE_MEMORY))))
12208 rtype |= RTYPE_VEC;
252b5132 12209 s_reset = s;
707bfff6 12210 if (reg_lookup (&s, rtype, &regno))
252b5132 12211 {
252b5132 12212 if ((regno & 1) != 0
ca4e0257 12213 && HAVE_32BIT_FPRS
90ecf173 12214 && !mips_oddfpreg_ok (ip->insn_mo, argnum))
252b5132
RH
12215 as_warn (_("Float register should be even, was %d"),
12216 regno);
12217
12218 c = *args;
12219 if (*s == ' ')
f9419b05 12220 ++s;
252b5132
RH
12221 if (args[1] != *s)
12222 {
12223 if (c == 'V' || c == 'W')
12224 {
12225 regno = lastregno;
12226 s = s_reset;
f9419b05 12227 ++args;
252b5132
RH
12228 }
12229 }
12230 switch (c)
12231 {
12232 case 'D':
deec1734 12233 case 'X':
df58fc94 12234 INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
252b5132 12235 break;
df58fc94 12236
252b5132
RH
12237 case 'V':
12238 case 'S':
deec1734 12239 case 'Y':
df58fc94 12240 INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
252b5132 12241 break;
df58fc94 12242
deec1734
CD
12243 case 'Q':
12244 /* This is like 'Z', but also needs to fix the MDMX
12245 vector/scalar select bits. Note that the
12246 scalar immediate case is handled above. */
12247 if (*s == '[')
12248 {
12249 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12250 int max_el = (is_qh ? 3 : 7);
12251 s++;
12252 my_getExpression(&imm_expr, s);
12253 check_absolute_expr (ip, &imm_expr);
12254 s = expr_end;
12255 if (imm_expr.X_add_number > max_el)
20203fb9
NC
12256 as_bad (_("Bad element selector %ld"),
12257 (long) imm_expr.X_add_number);
deec1734
CD
12258 imm_expr.X_add_number &= max_el;
12259 ip->insn_opcode |= (imm_expr.X_add_number
12260 << (OP_SH_VSEL +
12261 (is_qh ? 2 : 1)));
01a3f561 12262 imm_expr.X_op = O_absent;
deec1734 12263 if (*s != ']')
20203fb9 12264 as_warn (_("Expecting ']' found '%s'"), s);
deec1734
CD
12265 else
12266 s++;
12267 }
12268 else
12269 {
12270 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12271 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12272 << OP_SH_VSEL);
12273 else
12274 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12275 OP_SH_VSEL);
12276 }
90ecf173 12277 /* Fall through. */
252b5132
RH
12278 case 'W':
12279 case 'T':
deec1734 12280 case 'Z':
df58fc94 12281 INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
252b5132 12282 break;
df58fc94 12283
252b5132 12284 case 'R':
df58fc94 12285 INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
252b5132
RH
12286 break;
12287 }
12288 lastregno = regno;
12289 continue;
12290 }
12291
252b5132
RH
12292 switch (*args++)
12293 {
12294 case 'V':
df58fc94 12295 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132 12296 continue;
df58fc94 12297
252b5132 12298 case 'W':
df58fc94 12299 INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
252b5132
RH
12300 continue;
12301 }
12302 break;
12303
12304 case 'I':
12305 my_getExpression (&imm_expr, s);
12306 if (imm_expr.X_op != O_big
12307 && imm_expr.X_op != O_constant)
12308 insn_error = _("absolute expression required");
9ee2a2d4
MR
12309 if (HAVE_32BIT_GPRS)
12310 normalize_constant_expr (&imm_expr);
252b5132
RH
12311 s = expr_end;
12312 continue;
12313
12314 case 'A':
12315 my_getExpression (&offset_expr, s);
2051e8c4 12316 normalize_address_expr (&offset_expr);
f6688943 12317 *imm_reloc = BFD_RELOC_32;
252b5132
RH
12318 s = expr_end;
12319 continue;
12320
12321 case 'F':
12322 case 'L':
12323 case 'f':
12324 case 'l':
12325 {
12326 int f64;
ca4e0257 12327 int using_gprs;
252b5132
RH
12328 char *save_in;
12329 char *err;
12330 unsigned char temp[8];
12331 int len;
12332 unsigned int length;
12333 segT seg;
12334 subsegT subseg;
12335 char *p;
12336
12337 /* These only appear as the last operand in an
12338 instruction, and every instruction that accepts
12339 them in any variant accepts them in all variants.
12340 This means we don't have to worry about backing out
12341 any changes if the instruction does not match.
12342
12343 The difference between them is the size of the
12344 floating point constant and where it goes. For 'F'
12345 and 'L' the constant is 64 bits; for 'f' and 'l' it
12346 is 32 bits. Where the constant is placed is based
12347 on how the MIPS assembler does things:
12348 F -- .rdata
12349 L -- .lit8
12350 f -- immediate value
12351 l -- .lit4
12352
12353 The .lit4 and .lit8 sections are only used if
12354 permitted by the -G argument.
12355
ca4e0257
RS
12356 The code below needs to know whether the target register
12357 is 32 or 64 bits wide. It relies on the fact 'f' and
12358 'F' are used with GPR-based instructions and 'l' and
12359 'L' are used with FPR-based instructions. */
252b5132
RH
12360
12361 f64 = *args == 'F' || *args == 'L';
ca4e0257 12362 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
12363
12364 save_in = input_line_pointer;
12365 input_line_pointer = s;
12366 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12367 length = len;
12368 s = input_line_pointer;
12369 input_line_pointer = save_in;
12370 if (err != NULL && *err != '\0')
12371 {
12372 as_bad (_("Bad floating point constant: %s"), err);
12373 memset (temp, '\0', sizeof temp);
12374 length = f64 ? 8 : 4;
12375 }
12376
9c2799c2 12377 gas_assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
12378
12379 if (*args == 'f'
12380 || (*args == 'l'
3e722fb5 12381 && (g_switch_value < 4
252b5132
RH
12382 || (temp[0] == 0 && temp[1] == 0)
12383 || (temp[2] == 0 && temp[3] == 0))))
12384 {
12385 imm_expr.X_op = O_constant;
90ecf173 12386 if (!target_big_endian)
252b5132
RH
12387 imm_expr.X_add_number = bfd_getl32 (temp);
12388 else
12389 imm_expr.X_add_number = bfd_getb32 (temp);
12390 }
12391 else if (length > 4
90ecf173 12392 && !mips_disable_float_construction
ca4e0257
RS
12393 /* Constants can only be constructed in GPRs and
12394 copied to FPRs if the GPRs are at least as wide
12395 as the FPRs. Force the constant into memory if
12396 we are using 64-bit FPRs but the GPRs are only
12397 32 bits wide. */
12398 && (using_gprs
90ecf173 12399 || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
12400 && ((temp[0] == 0 && temp[1] == 0)
12401 || (temp[2] == 0 && temp[3] == 0))
12402 && ((temp[4] == 0 && temp[5] == 0)
12403 || (temp[6] == 0 && temp[7] == 0)))
12404 {
ca4e0257 12405 /* The value is simple enough to load with a couple of
90ecf173
MR
12406 instructions. If using 32-bit registers, set
12407 imm_expr to the high order 32 bits and offset_expr to
12408 the low order 32 bits. Otherwise, set imm_expr to
12409 the entire 64 bit constant. */
ca4e0257 12410 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
12411 {
12412 imm_expr.X_op = O_constant;
12413 offset_expr.X_op = O_constant;
90ecf173 12414 if (!target_big_endian)
252b5132
RH
12415 {
12416 imm_expr.X_add_number = bfd_getl32 (temp + 4);
12417 offset_expr.X_add_number = bfd_getl32 (temp);
12418 }
12419 else
12420 {
12421 imm_expr.X_add_number = bfd_getb32 (temp);
12422 offset_expr.X_add_number = bfd_getb32 (temp + 4);
12423 }
12424 if (offset_expr.X_add_number == 0)
12425 offset_expr.X_op = O_absent;
12426 }
12427 else if (sizeof (imm_expr.X_add_number) > 4)
12428 {
12429 imm_expr.X_op = O_constant;
90ecf173 12430 if (!target_big_endian)
252b5132
RH
12431 imm_expr.X_add_number = bfd_getl64 (temp);
12432 else
12433 imm_expr.X_add_number = bfd_getb64 (temp);
12434 }
12435 else
12436 {
12437 imm_expr.X_op = O_big;
12438 imm_expr.X_add_number = 4;
90ecf173 12439 if (!target_big_endian)
252b5132
RH
12440 {
12441 generic_bignum[0] = bfd_getl16 (temp);
12442 generic_bignum[1] = bfd_getl16 (temp + 2);
12443 generic_bignum[2] = bfd_getl16 (temp + 4);
12444 generic_bignum[3] = bfd_getl16 (temp + 6);
12445 }
12446 else
12447 {
12448 generic_bignum[0] = bfd_getb16 (temp + 6);
12449 generic_bignum[1] = bfd_getb16 (temp + 4);
12450 generic_bignum[2] = bfd_getb16 (temp + 2);
12451 generic_bignum[3] = bfd_getb16 (temp);
12452 }
12453 }
12454 }
12455 else
12456 {
12457 const char *newname;
12458 segT new_seg;
12459
12460 /* Switch to the right section. */
12461 seg = now_seg;
12462 subseg = now_subseg;
12463 switch (*args)
12464 {
12465 default: /* unused default case avoids warnings. */
12466 case 'L':
12467 newname = RDATA_SECTION_NAME;
3e722fb5 12468 if (g_switch_value >= 8)
252b5132
RH
12469 newname = ".lit8";
12470 break;
12471 case 'F':
3e722fb5 12472 newname = RDATA_SECTION_NAME;
252b5132
RH
12473 break;
12474 case 'l':
9c2799c2 12475 gas_assert (g_switch_value >= 4);
252b5132
RH
12476 newname = ".lit4";
12477 break;
12478 }
12479 new_seg = subseg_new (newname, (subsegT) 0);
f43abd2b 12480 if (IS_ELF)
252b5132
RH
12481 bfd_set_section_flags (stdoutput, new_seg,
12482 (SEC_ALLOC
12483 | SEC_LOAD
12484 | SEC_READONLY
12485 | SEC_DATA));
12486 frag_align (*args == 'l' ? 2 : 3, 0, 0);
c41e87e3 12487 if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
12488 record_alignment (new_seg, 4);
12489 else
12490 record_alignment (new_seg, *args == 'l' ? 2 : 3);
12491 if (seg == now_seg)
12492 as_bad (_("Can't use floating point insn in this section"));
12493
df58fc94
RS
12494 /* Set the argument to the current address in the
12495 section. */
12496 offset_expr.X_op = O_symbol;
12497 offset_expr.X_add_symbol = symbol_temp_new_now ();
12498 offset_expr.X_add_number = 0;
12499
12500 /* Put the floating point number into the section. */
12501 p = frag_more ((int) length);
12502 memcpy (p, temp, length);
12503
12504 /* Switch back to the original section. */
12505 subseg_set (seg, subseg);
12506 }
12507 }
12508 continue;
12509
12510 case 'i': /* 16-bit unsigned immediate. */
12511 case 'j': /* 16-bit signed immediate. */
12512 *imm_reloc = BFD_RELOC_LO16;
12513 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12514 {
12515 int more;
12516 offsetT minval, maxval;
12517
12518 more = (insn + 1 < past
12519 && strcmp (insn->name, insn[1].name) == 0);
12520
12521 /* If the expression was written as an unsigned number,
12522 only treat it as signed if there are no more
12523 alternatives. */
12524 if (more
12525 && *args == 'j'
12526 && sizeof (imm_expr.X_add_number) <= 4
12527 && imm_expr.X_op == O_constant
12528 && imm_expr.X_add_number < 0
12529 && imm_expr.X_unsigned
12530 && HAVE_64BIT_GPRS)
12531 break;
12532
12533 /* For compatibility with older assemblers, we accept
12534 0x8000-0xffff as signed 16-bit numbers when only
12535 signed numbers are allowed. */
12536 if (*args == 'i')
12537 minval = 0, maxval = 0xffff;
12538 else if (more)
12539 minval = -0x8000, maxval = 0x7fff;
12540 else
12541 minval = -0x8000, maxval = 0xffff;
12542
12543 if (imm_expr.X_op != O_constant
12544 || imm_expr.X_add_number < minval
12545 || imm_expr.X_add_number > maxval)
12546 {
12547 if (more)
12548 break;
12549 if (imm_expr.X_op == O_constant
12550 || imm_expr.X_op == O_big)
12551 as_bad (_("Expression out of range"));
12552 }
12553 }
12554 s = expr_end;
12555 continue;
12556
12557 case 'o': /* 16-bit offset. */
12558 offset_reloc[0] = BFD_RELOC_LO16;
12559 offset_reloc[1] = BFD_RELOC_UNUSED;
12560 offset_reloc[2] = BFD_RELOC_UNUSED;
12561
12562 /* Check whether there is only a single bracketed expression
12563 left. If so, it must be the base register and the
12564 constant must be zero. */
12565 if (*s == '(' && strchr (s + 1, '(') == 0)
12566 {
12567 offset_expr.X_op = O_constant;
12568 offset_expr.X_add_number = 0;
12569 continue;
12570 }
12571
12572 /* If this value won't fit into a 16 bit offset, then go
12573 find a macro that will generate the 32 bit offset
12574 code pattern. */
12575 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
12576 && (offset_expr.X_op != O_constant
12577 || offset_expr.X_add_number >= 0x8000
12578 || offset_expr.X_add_number < -0x8000))
12579 break;
12580
12581 s = expr_end;
12582 continue;
12583
12584 case 'p': /* PC-relative offset. */
12585 *offset_reloc = BFD_RELOC_16_PCREL_S2;
12586 my_getExpression (&offset_expr, s);
12587 s = expr_end;
12588 continue;
12589
12590 case 'u': /* Upper 16 bits. */
5821951c 12591 *imm_reloc = BFD_RELOC_LO16;
df58fc94
RS
12592 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
12593 && imm_expr.X_op == O_constant
12594 && (imm_expr.X_add_number < 0
12595 || imm_expr.X_add_number >= 0x10000))
12596 as_bad (_("lui expression (%lu) not in range 0..65535"),
12597 (unsigned long) imm_expr.X_add_number);
12598 s = expr_end;
12599 continue;
12600
12601 case 'a': /* 26-bit address. */
12602 *offset_reloc = BFD_RELOC_MIPS_JMP;
12603 my_getExpression (&offset_expr, s);
12604 s = expr_end;
12605 continue;
12606
12607 case 'N': /* 3-bit branch condition code. */
12608 case 'M': /* 3-bit compare condition code. */
12609 rtype = RTYPE_CCC;
12610 if (ip->insn_mo->pinfo & (FP_D | FP_S))
12611 rtype |= RTYPE_FCC;
12612 if (!reg_lookup (&s, rtype, &regno))
12613 break;
12614 if ((strcmp (str + strlen (str) - 3, ".ps") == 0
12615 || strcmp (str + strlen (str) - 5, "any2f") == 0
12616 || strcmp (str + strlen (str) - 5, "any2t") == 0)
12617 && (regno & 1) != 0)
12618 as_warn (_("Condition code register should be even for %s, "
12619 "was %d"),
12620 str, regno);
12621 if ((strcmp (str + strlen (str) - 5, "any4f") == 0
12622 || strcmp (str + strlen (str) - 5, "any4t") == 0)
12623 && (regno & 3) != 0)
12624 as_warn (_("Condition code register should be 0 or 4 for %s, "
12625 "was %d"),
12626 str, regno);
12627 if (*args == 'N')
12628 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
12629 else
12630 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
12631 continue;
12632
12633 case 'H':
12634 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
12635 s += 2;
12636 if (ISDIGIT (*s))
12637 {
12638 c = 0;
12639 do
12640 {
12641 c *= 10;
12642 c += *s - '0';
12643 ++s;
12644 }
12645 while (ISDIGIT (*s));
12646 }
12647 else
12648 c = 8; /* Invalid sel value. */
12649
12650 if (c > 7)
12651 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
12652 INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
12653 continue;
12654
12655 case 'e':
12656 gas_assert (!mips_opts.micromips);
12657 /* Must be at least one digit. */
12658 my_getExpression (&imm_expr, s);
12659 check_absolute_expr (ip, &imm_expr);
12660
12661 if ((unsigned long) imm_expr.X_add_number
12662 > (unsigned long) OP_MASK_VECBYTE)
12663 {
12664 as_bad (_("bad byte vector index (%ld)"),
12665 (long) imm_expr.X_add_number);
12666 imm_expr.X_add_number = 0;
12667 }
12668
12669 INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
12670 imm_expr.X_op = O_absent;
12671 s = expr_end;
12672 continue;
12673
12674 case '%':
12675 gas_assert (!mips_opts.micromips);
12676 my_getExpression (&imm_expr, s);
12677 check_absolute_expr (ip, &imm_expr);
12678
12679 if ((unsigned long) imm_expr.X_add_number
12680 > (unsigned long) OP_MASK_VECALIGN)
12681 {
12682 as_bad (_("bad byte vector index (%ld)"),
12683 (long) imm_expr.X_add_number);
12684 imm_expr.X_add_number = 0;
12685 }
12686
12687 INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
12688 imm_expr.X_op = O_absent;
12689 s = expr_end;
12690 continue;
12691
12692 case 'm': /* Opcode extension character. */
12693 gas_assert (mips_opts.micromips);
12694 c = *++args;
12695 switch (c)
12696 {
12697 case 'r':
12698 if (strncmp (s, "$pc", 3) == 0)
12699 {
12700 s += 3;
12701 continue;
12702 }
12703 break;
12704
12705 case 'a':
12706 case 'b':
12707 case 'c':
12708 case 'd':
12709 case 'e':
12710 case 'f':
12711 case 'g':
12712 case 'h':
12713 case 'i':
12714 case 'j':
12715 case 'l':
12716 case 'm':
12717 case 'n':
12718 case 'p':
12719 case 'q':
12720 case 's':
12721 case 't':
12722 case 'x':
12723 case 'y':
12724 case 'z':
12725 s_reset = s;
12726 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12727 if (regno == AT && mips_opts.at)
12728 {
12729 if (mips_opts.at == ATREG)
12730 as_warn (_("Used $at without \".set noat\""));
12731 else
12732 as_warn (_("Used $%u with \".set at=$%u\""),
12733 regno, mips_opts.at);
12734 }
12735 if (!ok)
12736 {
12737 if (c == 'c')
12738 {
12739 gas_assert (args[1] == ',');
12740 regno = lastregno;
12741 ++args;
12742 }
12743 else if (c == 't')
12744 {
12745 gas_assert (args[1] == ',');
12746 ++args;
12747 continue; /* Nothing to do. */
12748 }
12749 else
12750 break;
12751 }
12752
12753 if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
12754 {
12755 if (regno == lastregno)
12756 {
12757 insn_error
12758 = _("Source and destination must be different");
12759 continue;
12760 }
12761 if (regno == 31 && lastregno == 0xffffffff)
12762 {
12763 insn_error
12764 = _("A destination register must be supplied");
12765 continue;
12766 }
12767 }
12768
12769 if (*s == ' ')
12770 ++s;
12771 if (args[1] != *s)
12772 {
12773 if (c == 'e')
12774 {
12775 gas_assert (args[1] == ',');
12776 regno = lastregno;
12777 s = s_reset;
12778 ++args;
12779 }
12780 else if (c == 't')
12781 {
12782 gas_assert (args[1] == ',');
12783 s = s_reset;
12784 ++args;
12785 continue; /* Nothing to do. */
12786 }
12787 }
12788
12789 /* Make sure regno is the same as lastregno. */
12790 if (c == 't' && regno != lastregno)
12791 break;
12792
12793 /* Make sure regno is the same as destregno. */
12794 if (c == 'x' && regno != destregno)
12795 break;
12796
12797 /* We need to save regno, before regno maps to the
12798 microMIPS register encoding. */
12799 lastregno = regno;
12800
12801 if (c == 'f')
12802 destregno = regno;
12803
12804 switch (c)
12805 {
12806 case 'a':
12807 if (regno != GP)
12808 regno = ILLEGAL_REG;
12809 break;
12810
12811 case 'b':
12812 regno = mips32_to_micromips_reg_b_map[regno];
12813 break;
12814
12815 case 'c':
12816 regno = mips32_to_micromips_reg_c_map[regno];
12817 break;
12818
12819 case 'd':
12820 regno = mips32_to_micromips_reg_d_map[regno];
12821 break;
12822
12823 case 'e':
12824 regno = mips32_to_micromips_reg_e_map[regno];
12825 break;
12826
12827 case 'f':
12828 regno = mips32_to_micromips_reg_f_map[regno];
12829 break;
12830
12831 case 'g':
12832 regno = mips32_to_micromips_reg_g_map[regno];
12833 break;
12834
12835 case 'h':
12836 regno = mips32_to_micromips_reg_h_map[regno];
12837 break;
12838
12839 case 'i':
12840 switch (EXTRACT_OPERAND (1, MI, *ip))
12841 {
12842 case 4:
12843 if (regno == 21)
12844 regno = 3;
12845 else if (regno == 22)
12846 regno = 4;
12847 else if (regno == 5)
12848 regno = 5;
12849 else if (regno == 6)
12850 regno = 6;
12851 else if (regno == 7)
12852 regno = 7;
12853 else
12854 regno = ILLEGAL_REG;
12855 break;
12856
12857 case 5:
12858 if (regno == 6)
12859 regno = 0;
12860 else if (regno == 7)
12861 regno = 1;
12862 else
12863 regno = ILLEGAL_REG;
12864 break;
12865
12866 case 6:
12867 if (regno == 7)
12868 regno = 2;
12869 else
12870 regno = ILLEGAL_REG;
12871 break;
12872
12873 default:
12874 regno = ILLEGAL_REG;
12875 break;
12876 }
12877 break;
12878
12879 case 'l':
12880 regno = mips32_to_micromips_reg_l_map[regno];
12881 break;
12882
12883 case 'm':
12884 regno = mips32_to_micromips_reg_m_map[regno];
12885 break;
12886
12887 case 'n':
12888 regno = mips32_to_micromips_reg_n_map[regno];
12889 break;
12890
12891 case 'q':
12892 regno = mips32_to_micromips_reg_q_map[regno];
12893 break;
12894
12895 case 's':
12896 if (regno != SP)
12897 regno = ILLEGAL_REG;
12898 break;
12899
12900 case 'y':
12901 if (regno != 31)
12902 regno = ILLEGAL_REG;
12903 break;
12904
12905 case 'z':
12906 if (regno != ZERO)
12907 regno = ILLEGAL_REG;
12908 break;
12909
12910 case 'j': /* Do nothing. */
12911 case 'p':
12912 case 't':
12913 case 'x':
12914 break;
12915
12916 default:
b37df7c4 12917 abort ();
df58fc94
RS
12918 }
12919
12920 if (regno == ILLEGAL_REG)
12921 break;
12922
12923 switch (c)
12924 {
12925 case 'b':
12926 INSERT_OPERAND (1, MB, *ip, regno);
12927 break;
12928
12929 case 'c':
12930 INSERT_OPERAND (1, MC, *ip, regno);
12931 break;
12932
12933 case 'd':
12934 INSERT_OPERAND (1, MD, *ip, regno);
12935 break;
12936
12937 case 'e':
12938 INSERT_OPERAND (1, ME, *ip, regno);
12939 break;
12940
12941 case 'f':
12942 INSERT_OPERAND (1, MF, *ip, regno);
12943 break;
12944
12945 case 'g':
12946 INSERT_OPERAND (1, MG, *ip, regno);
12947 break;
12948
12949 case 'h':
12950 INSERT_OPERAND (1, MH, *ip, regno);
12951 break;
12952
12953 case 'i':
12954 INSERT_OPERAND (1, MI, *ip, regno);
12955 break;
12956
12957 case 'j':
12958 INSERT_OPERAND (1, MJ, *ip, regno);
12959 break;
12960
12961 case 'l':
12962 INSERT_OPERAND (1, ML, *ip, regno);
12963 break;
12964
12965 case 'm':
12966 INSERT_OPERAND (1, MM, *ip, regno);
12967 break;
12968
12969 case 'n':
12970 INSERT_OPERAND (1, MN, *ip, regno);
12971 break;
12972
12973 case 'p':
12974 INSERT_OPERAND (1, MP, *ip, regno);
12975 break;
12976
12977 case 'q':
12978 INSERT_OPERAND (1, MQ, *ip, regno);
12979 break;
12980
12981 case 'a': /* Do nothing. */
12982 case 's': /* Do nothing. */
12983 case 't': /* Do nothing. */
12984 case 'x': /* Do nothing. */
12985 case 'y': /* Do nothing. */
12986 case 'z': /* Do nothing. */
12987 break;
12988
12989 default:
b37df7c4 12990 abort ();
df58fc94
RS
12991 }
12992 continue;
12993
12994 case 'A':
12995 {
12996 bfd_reloc_code_real_type r[3];
12997 expressionS ep;
12998 int imm;
12999
13000 /* Check whether there is only a single bracketed
13001 expression left. If so, it must be the base register
13002 and the constant must be zero. */
13003 if (*s == '(' && strchr (s + 1, '(') == 0)
13004 {
13005 INSERT_OPERAND (1, IMMA, *ip, 0);
13006 continue;
13007 }
13008
13009 if (my_getSmallExpression (&ep, r, s) > 0
13010 || !expr_const_in_range (&ep, -64, 64, 2))
13011 break;
13012
13013 imm = ep.X_add_number >> 2;
13014 INSERT_OPERAND (1, IMMA, *ip, imm);
13015 }
13016 s = expr_end;
13017 continue;
13018
13019 case 'B':
13020 {
13021 bfd_reloc_code_real_type r[3];
13022 expressionS ep;
13023 int imm;
13024
13025 if (my_getSmallExpression (&ep, r, s) > 0
13026 || ep.X_op != O_constant)
13027 break;
13028
13029 for (imm = 0; imm < 8; imm++)
13030 if (micromips_imm_b_map[imm] == ep.X_add_number)
13031 break;
13032 if (imm >= 8)
13033 break;
13034
13035 INSERT_OPERAND (1, IMMB, *ip, imm);
13036 }
13037 s = expr_end;
13038 continue;
13039
13040 case 'C':
13041 {
13042 bfd_reloc_code_real_type r[3];
13043 expressionS ep;
13044 int imm;
13045
13046 if (my_getSmallExpression (&ep, r, s) > 0
13047 || ep.X_op != O_constant)
13048 break;
13049
13050 for (imm = 0; imm < 16; imm++)
13051 if (micromips_imm_c_map[imm] == ep.X_add_number)
13052 break;
13053 if (imm >= 16)
13054 break;
13055
13056 INSERT_OPERAND (1, IMMC, *ip, imm);
13057 }
13058 s = expr_end;
13059 continue;
13060
13061 case 'D': /* pc relative offset */
13062 case 'E': /* pc relative offset */
13063 my_getExpression (&offset_expr, s);
13064 if (offset_expr.X_op == O_register)
13065 break;
13066
40209cad
MR
13067 if (!forced_insn_length)
13068 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
13069 else if (c == 'D')
13070 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
13071 else
13072 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
df58fc94
RS
13073 s = expr_end;
13074 continue;
13075
13076 case 'F':
13077 {
13078 bfd_reloc_code_real_type r[3];
13079 expressionS ep;
13080 int imm;
13081
13082 if (my_getSmallExpression (&ep, r, s) > 0
13083 || !expr_const_in_range (&ep, 0, 16, 0))
13084 break;
13085
13086 imm = ep.X_add_number;
13087 INSERT_OPERAND (1, IMMF, *ip, imm);
13088 }
13089 s = expr_end;
13090 continue;
13091
13092 case 'G':
13093 {
13094 bfd_reloc_code_real_type r[3];
13095 expressionS ep;
13096 int imm;
13097
13098 /* Check whether there is only a single bracketed
13099 expression left. If so, it must be the base register
13100 and the constant must be zero. */
13101 if (*s == '(' && strchr (s + 1, '(') == 0)
13102 {
13103 INSERT_OPERAND (1, IMMG, *ip, 0);
13104 continue;
13105 }
13106
13107 if (my_getSmallExpression (&ep, r, s) > 0
13108 || !expr_const_in_range (&ep, -1, 15, 0))
13109 break;
13110
13111 imm = ep.X_add_number & 15;
13112 INSERT_OPERAND (1, IMMG, *ip, imm);
13113 }
13114 s = expr_end;
13115 continue;
13116
13117 case 'H':
13118 {
13119 bfd_reloc_code_real_type r[3];
13120 expressionS ep;
13121 int imm;
13122
13123 /* Check whether there is only a single bracketed
13124 expression left. If so, it must be the base register
13125 and the constant must be zero. */
13126 if (*s == '(' && strchr (s + 1, '(') == 0)
13127 {
13128 INSERT_OPERAND (1, IMMH, *ip, 0);
13129 continue;
13130 }
13131
13132 if (my_getSmallExpression (&ep, r, s) > 0
13133 || !expr_const_in_range (&ep, 0, 16, 1))
13134 break;
13135
13136 imm = ep.X_add_number >> 1;
13137 INSERT_OPERAND (1, IMMH, *ip, imm);
13138 }
13139 s = expr_end;
13140 continue;
13141
13142 case 'I':
13143 {
13144 bfd_reloc_code_real_type r[3];
13145 expressionS ep;
13146 int imm;
13147
13148 if (my_getSmallExpression (&ep, r, s) > 0
13149 || !expr_const_in_range (&ep, -1, 127, 0))
13150 break;
13151
13152 imm = ep.X_add_number & 127;
13153 INSERT_OPERAND (1, IMMI, *ip, imm);
13154 }
13155 s = expr_end;
13156 continue;
13157
13158 case 'J':
13159 {
13160 bfd_reloc_code_real_type r[3];
13161 expressionS ep;
13162 int imm;
13163
13164 /* Check whether there is only a single bracketed
13165 expression left. If so, it must be the base register
13166 and the constant must be zero. */
13167 if (*s == '(' && strchr (s + 1, '(') == 0)
13168 {
13169 INSERT_OPERAND (1, IMMJ, *ip, 0);
13170 continue;
13171 }
13172
13173 if (my_getSmallExpression (&ep, r, s) > 0
13174 || !expr_const_in_range (&ep, 0, 16, 2))
13175 break;
13176
13177 imm = ep.X_add_number >> 2;
13178 INSERT_OPERAND (1, IMMJ, *ip, imm);
13179 }
13180 s = expr_end;
13181 continue;
13182
13183 case 'L':
13184 {
13185 bfd_reloc_code_real_type r[3];
13186 expressionS ep;
13187 int imm;
13188
13189 /* Check whether there is only a single bracketed
13190 expression left. If so, it must be the base register
13191 and the constant must be zero. */
13192 if (*s == '(' && strchr (s + 1, '(') == 0)
13193 {
13194 INSERT_OPERAND (1, IMML, *ip, 0);
13195 continue;
13196 }
13197
13198 if (my_getSmallExpression (&ep, r, s) > 0
13199 || !expr_const_in_range (&ep, 0, 16, 0))
13200 break;
13201
13202 imm = ep.X_add_number;
13203 INSERT_OPERAND (1, IMML, *ip, imm);
13204 }
13205 s = expr_end;
13206 continue;
13207
13208 case 'M':
13209 {
13210 bfd_reloc_code_real_type r[3];
13211 expressionS ep;
13212 int imm;
13213
13214 if (my_getSmallExpression (&ep, r, s) > 0
13215 || !expr_const_in_range (&ep, 1, 9, 0))
13216 break;
13217
13218 imm = ep.X_add_number & 7;
13219 INSERT_OPERAND (1, IMMM, *ip, imm);
13220 }
13221 s = expr_end;
13222 continue;
13223
13224 case 'N': /* Register list for lwm and swm. */
13225 {
13226 /* A comma-separated list of registers and/or
13227 dash-separated contiguous ranges including
13228 both ra and a set of one or more registers
13229 starting at s0 up to s3 which have to be
13230 consecutive, e.g.:
13231
13232 s0, ra
13233 s0, s1, ra, s2, s3
13234 s0-s2, ra
13235
13236 and any permutations of these. */
13237 unsigned int reglist;
13238 int imm;
13239
13240 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13241 break;
13242
13243 if ((reglist & 0xfff1ffff) != 0x80010000)
13244 break;
13245
13246 reglist = (reglist >> 17) & 7;
13247 reglist += 1;
13248 if ((reglist & -reglist) != reglist)
13249 break;
252b5132 13250
df58fc94
RS
13251 imm = ffs (reglist) - 1;
13252 INSERT_OPERAND (1, IMMN, *ip, imm);
13253 }
13254 continue;
252b5132 13255
df58fc94
RS
13256 case 'O': /* sdbbp 4-bit code. */
13257 {
13258 bfd_reloc_code_real_type r[3];
13259 expressionS ep;
13260 int imm;
13261
13262 if (my_getSmallExpression (&ep, r, s) > 0
13263 || !expr_const_in_range (&ep, 0, 16, 0))
13264 break;
13265
13266 imm = ep.X_add_number;
13267 INSERT_OPERAND (1, IMMO, *ip, imm);
252b5132 13268 }
df58fc94
RS
13269 s = expr_end;
13270 continue;
252b5132 13271
df58fc94
RS
13272 case 'P':
13273 {
13274 bfd_reloc_code_real_type r[3];
13275 expressionS ep;
13276 int imm;
5e0116d5 13277
df58fc94
RS
13278 if (my_getSmallExpression (&ep, r, s) > 0
13279 || !expr_const_in_range (&ep, 0, 32, 2))
13280 break;
5e0116d5 13281
df58fc94
RS
13282 imm = ep.X_add_number >> 2;
13283 INSERT_OPERAND (1, IMMP, *ip, imm);
13284 }
13285 s = expr_end;
13286 continue;
5e0116d5 13287
df58fc94
RS
13288 case 'Q':
13289 {
13290 bfd_reloc_code_real_type r[3];
13291 expressionS ep;
13292 int imm;
5e0116d5 13293
df58fc94
RS
13294 if (my_getSmallExpression (&ep, r, s) > 0
13295 || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13296 break;
252b5132 13297
df58fc94
RS
13298 imm = ep.X_add_number >> 2;
13299 INSERT_OPERAND (1, IMMQ, *ip, imm);
13300 }
13301 s = expr_end;
13302 continue;
4614d845 13303
df58fc94
RS
13304 case 'U':
13305 {
13306 bfd_reloc_code_real_type r[3];
13307 expressionS ep;
13308 int imm;
13309
13310 /* Check whether there is only a single bracketed
13311 expression left. If so, it must be the base register
13312 and the constant must be zero. */
13313 if (*s == '(' && strchr (s + 1, '(') == 0)
13314 {
13315 INSERT_OPERAND (1, IMMU, *ip, 0);
13316 continue;
13317 }
13318
13319 if (my_getSmallExpression (&ep, r, s) > 0
13320 || !expr_const_in_range (&ep, 0, 32, 2))
13321 break;
13322
13323 imm = ep.X_add_number >> 2;
13324 INSERT_OPERAND (1, IMMU, *ip, imm);
13325 }
13326 s = expr_end;
5e0116d5 13327 continue;
252b5132 13328
df58fc94
RS
13329 case 'W':
13330 {
13331 bfd_reloc_code_real_type r[3];
13332 expressionS ep;
13333 int imm;
252b5132 13334
df58fc94
RS
13335 if (my_getSmallExpression (&ep, r, s) > 0
13336 || !expr_const_in_range (&ep, 0, 64, 2))
13337 break;
252b5132 13338
df58fc94
RS
13339 imm = ep.X_add_number >> 2;
13340 INSERT_OPERAND (1, IMMW, *ip, imm);
13341 }
13342 s = expr_end;
13343 continue;
252b5132 13344
df58fc94
RS
13345 case 'X':
13346 {
13347 bfd_reloc_code_real_type r[3];
13348 expressionS ep;
13349 int imm;
252b5132 13350
df58fc94
RS
13351 if (my_getSmallExpression (&ep, r, s) > 0
13352 || !expr_const_in_range (&ep, -8, 8, 0))
13353 break;
252b5132 13354
df58fc94
RS
13355 imm = ep.X_add_number;
13356 INSERT_OPERAND (1, IMMX, *ip, imm);
13357 }
13358 s = expr_end;
13359 continue;
252b5132 13360
df58fc94
RS
13361 case 'Y':
13362 {
13363 bfd_reloc_code_real_type r[3];
13364 expressionS ep;
13365 int imm;
156c2f8b 13366
df58fc94
RS
13367 if (my_getSmallExpression (&ep, r, s) > 0
13368 || expr_const_in_range (&ep, -2, 2, 2)
13369 || !expr_const_in_range (&ep, -258, 258, 2))
13370 break;
156c2f8b 13371
df58fc94
RS
13372 imm = ep.X_add_number >> 2;
13373 imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13374 INSERT_OPERAND (1, IMMY, *ip, imm);
13375 }
13376 s = expr_end;
13377 continue;
60b63b72 13378
df58fc94
RS
13379 case 'Z':
13380 {
13381 bfd_reloc_code_real_type r[3];
13382 expressionS ep;
13383
13384 if (my_getSmallExpression (&ep, r, s) > 0
13385 || !expr_const_in_range (&ep, 0, 1, 0))
13386 break;
13387 }
13388 s = expr_end;
13389 continue;
13390
13391 default:
13392 as_bad (_("Internal error: bad microMIPS opcode "
13393 "(unknown extension operand type `m%c'): %s %s"),
13394 *args, insn->name, insn->args);
13395 /* Further processing is fruitless. */
13396 return;
60b63b72 13397 }
df58fc94 13398 break;
60b63b72 13399
df58fc94
RS
13400 case 'n': /* Register list for 32-bit lwm and swm. */
13401 gas_assert (mips_opts.micromips);
13402 {
13403 /* A comma-separated list of registers and/or
13404 dash-separated contiguous ranges including
13405 at least one of ra and a set of one or more
13406 registers starting at s0 up to s7 and then
13407 s8 which have to be consecutive, e.g.:
13408
13409 ra
13410 s0
13411 ra, s0, s1, s2
13412 s0-s8
13413 s0-s5, ra
13414
13415 and any permutations of these. */
13416 unsigned int reglist;
13417 int imm;
13418 int ra;
13419
13420 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13421 break;
13422
13423 if ((reglist & 0x3f00ffff) != 0)
13424 break;
13425
13426 ra = (reglist >> 27) & 0x10;
13427 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13428 reglist += 1;
13429 if ((reglist & -reglist) != reglist)
13430 break;
13431
13432 imm = (ffs (reglist) - 1) | ra;
13433 INSERT_OPERAND (1, RT, *ip, imm);
13434 imm_expr.X_op = O_absent;
13435 }
60b63b72
RS
13436 continue;
13437
df58fc94
RS
13438 case '|': /* 4-bit trap code. */
13439 gas_assert (mips_opts.micromips);
60b63b72
RS
13440 my_getExpression (&imm_expr, s);
13441 check_absolute_expr (ip, &imm_expr);
60b63b72 13442 if ((unsigned long) imm_expr.X_add_number
df58fc94
RS
13443 > MICROMIPSOP_MASK_TRAP)
13444 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13445 (unsigned long) imm_expr.X_add_number,
13446 ip->insn_mo->name);
13447 INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
60b63b72
RS
13448 imm_expr.X_op = O_absent;
13449 s = expr_end;
13450 continue;
13451
252b5132 13452 default:
f71d0d44 13453 as_bad (_("Bad char = '%c'\n"), *args);
b37df7c4 13454 abort ();
252b5132
RH
13455 }
13456 break;
13457 }
13458 /* Args don't match. */
df58fc94
RS
13459 s = argsStart;
13460 insn_error = _("Illegal operands");
13461 if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
252b5132
RH
13462 {
13463 ++insn;
252b5132
RH
13464 continue;
13465 }
df58fc94
RS
13466 else if (wrong_delay_slot_insns && need_delay_slot_ok)
13467 {
13468 gas_assert (firstinsn);
13469 need_delay_slot_ok = FALSE;
13470 past = insn + 1;
13471 insn = firstinsn;
13472 continue;
13473 }
252b5132
RH
13474 return;
13475 }
13476}
13477
0499d65b
TS
13478#define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13479
252b5132
RH
13480/* This routine assembles an instruction into its binary format when
13481 assembling for the mips16. As a side effect, it sets one of the
df58fc94
RS
13482 global variables imm_reloc or offset_reloc to the type of relocation
13483 to do if one of the operands is an address expression. It also sets
13484 forced_insn_length to the resulting instruction size in bytes if the
13485 user explicitly requested a small or extended instruction. */
252b5132
RH
13486
13487static void
17a2f251 13488mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
13489{
13490 char *s;
13491 const char *args;
13492 struct mips_opcode *insn;
13493 char *argsstart;
13494 unsigned int regno;
13495 unsigned int lastregno = 0;
13496 char *s_reset;
d6f16593 13497 size_t i;
252b5132
RH
13498
13499 insn_error = NULL;
13500
df58fc94 13501 forced_insn_length = 0;
252b5132 13502
3882b010 13503 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
13504 ;
13505 switch (*s)
13506 {
13507 case '\0':
13508 break;
13509
13510 case ' ':
13511 *s++ = '\0';
13512 break;
13513
13514 case '.':
13515 if (s[1] == 't' && s[2] == ' ')
13516 {
13517 *s = '\0';
df58fc94 13518 forced_insn_length = 2;
252b5132
RH
13519 s += 3;
13520 break;
13521 }
13522 else if (s[1] == 'e' && s[2] == ' ')
13523 {
13524 *s = '\0';
df58fc94 13525 forced_insn_length = 4;
252b5132
RH
13526 s += 3;
13527 break;
13528 }
13529 /* Fall through. */
13530 default:
13531 insn_error = _("unknown opcode");
13532 return;
13533 }
13534
df58fc94
RS
13535 if (mips_opts.noautoextend && !forced_insn_length)
13536 forced_insn_length = 2;
252b5132
RH
13537
13538 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13539 {
13540 insn_error = _("unrecognized opcode");
13541 return;
13542 }
13543
13544 argsstart = s;
13545 for (;;)
13546 {
9b3f89ee
TS
13547 bfd_boolean ok;
13548
9c2799c2 13549 gas_assert (strcmp (insn->name, str) == 0);
252b5132 13550
037b32b9 13551 ok = is_opcode_valid_16 (insn);
9b3f89ee
TS
13552 if (! ok)
13553 {
13554 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13555 && strcmp (insn->name, insn[1].name) == 0)
13556 {
13557 ++insn;
13558 continue;
13559 }
13560 else
13561 {
13562 if (!insn_error)
13563 {
13564 static char buf[100];
13565 sprintf (buf,
7bd942df 13566 _("Opcode not supported on this processor: %s (%s)"),
9b3f89ee
TS
13567 mips_cpu_info_from_arch (mips_opts.arch)->name,
13568 mips_cpu_info_from_isa (mips_opts.isa)->name);
13569 insn_error = buf;
13570 }
13571 return;
13572 }
13573 }
13574
1e915849 13575 create_insn (ip, insn);
252b5132 13576 imm_expr.X_op = O_absent;
f6688943
TS
13577 imm_reloc[0] = BFD_RELOC_UNUSED;
13578 imm_reloc[1] = BFD_RELOC_UNUSED;
13579 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 13580 imm2_expr.X_op = O_absent;
252b5132 13581 offset_expr.X_op = O_absent;
f6688943
TS
13582 offset_reloc[0] = BFD_RELOC_UNUSED;
13583 offset_reloc[1] = BFD_RELOC_UNUSED;
13584 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
13585 for (args = insn->args; 1; ++args)
13586 {
13587 int c;
13588
13589 if (*s == ' ')
13590 ++s;
13591
13592 /* In this switch statement we call break if we did not find
13593 a match, continue if we did find a match, or return if we
13594 are done. */
13595
13596 c = *args;
13597 switch (c)
13598 {
13599 case '\0':
13600 if (*s == '\0')
13601 {
b886a2ab
RS
13602 offsetT value;
13603
252b5132
RH
13604 /* Stuff the immediate value in now, if we can. */
13605 if (imm_expr.X_op == O_constant
f6688943 13606 && *imm_reloc > BFD_RELOC_UNUSED
b886a2ab
RS
13607 && insn->pinfo != INSN_MACRO
13608 && calculate_reloc (*offset_reloc,
13609 imm_expr.X_add_number, &value))
252b5132 13610 {
c4e7957c 13611 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b886a2ab 13612 *offset_reloc, value, forced_insn_length,
43c0598f 13613 &ip->insn_opcode);
252b5132 13614 imm_expr.X_op = O_absent;
f6688943 13615 *imm_reloc = BFD_RELOC_UNUSED;
43c0598f 13616 *offset_reloc = BFD_RELOC_UNUSED;
252b5132
RH
13617 }
13618
13619 return;
13620 }
13621 break;
13622
13623 case ',':
13624 if (*s++ == c)
13625 continue;
13626 s--;
13627 switch (*++args)
13628 {
13629 case 'v':
bf12938e 13630 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
13631 continue;
13632 case 'w':
bf12938e 13633 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
13634 continue;
13635 }
13636 break;
13637
13638 case '(':
13639 case ')':
13640 if (*s++ == c)
13641 continue;
13642 break;
13643
13644 case 'v':
13645 case 'w':
13646 if (s[0] != '$')
13647 {
13648 if (c == 'v')
bf12938e 13649 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 13650 else
bf12938e 13651 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
13652 ++args;
13653 continue;
13654 }
13655 /* Fall through. */
13656 case 'x':
13657 case 'y':
13658 case 'z':
13659 case 'Z':
13660 case '0':
13661 case 'S':
13662 case 'R':
13663 case 'X':
13664 case 'Y':
707bfff6
TS
13665 s_reset = s;
13666 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
252b5132 13667 {
707bfff6 13668 if (c == 'v' || c == 'w')
85b51719 13669 {
707bfff6 13670 if (c == 'v')
a9e24354 13671 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
707bfff6 13672 else
a9e24354 13673 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
707bfff6
TS
13674 ++args;
13675 continue;
85b51719 13676 }
707bfff6 13677 break;
252b5132
RH
13678 }
13679
13680 if (*s == ' ')
13681 ++s;
13682 if (args[1] != *s)
13683 {
13684 if (c == 'v' || c == 'w')
13685 {
13686 regno = mips16_to_32_reg_map[lastregno];
13687 s = s_reset;
f9419b05 13688 ++args;
252b5132
RH
13689 }
13690 }
13691
13692 switch (c)
13693 {
13694 case 'x':
13695 case 'y':
13696 case 'z':
13697 case 'v':
13698 case 'w':
13699 case 'Z':
13700 regno = mips32_to_16_reg_map[regno];
13701 break;
13702
13703 case '0':
13704 if (regno != 0)
13705 regno = ILLEGAL_REG;
13706 break;
13707
13708 case 'S':
13709 if (regno != SP)
13710 regno = ILLEGAL_REG;
13711 break;
13712
13713 case 'R':
13714 if (regno != RA)
13715 regno = ILLEGAL_REG;
13716 break;
13717
13718 case 'X':
13719 case 'Y':
741fe287
MR
13720 if (regno == AT && mips_opts.at)
13721 {
13722 if (mips_opts.at == ATREG)
13723 as_warn (_("used $at without \".set noat\""));
13724 else
13725 as_warn (_("used $%u with \".set at=$%u\""),
13726 regno, mips_opts.at);
13727 }
252b5132
RH
13728 break;
13729
13730 default:
b37df7c4 13731 abort ();
252b5132
RH
13732 }
13733
13734 if (regno == ILLEGAL_REG)
13735 break;
13736
13737 switch (c)
13738 {
13739 case 'x':
13740 case 'v':
bf12938e 13741 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
13742 break;
13743 case 'y':
13744 case 'w':
bf12938e 13745 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
13746 break;
13747 case 'z':
bf12938e 13748 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
13749 break;
13750 case 'Z':
bf12938e 13751 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
13752 case '0':
13753 case 'S':
13754 case 'R':
13755 break;
13756 case 'X':
bf12938e 13757 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
13758 break;
13759 case 'Y':
13760 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 13761 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
13762 break;
13763 default:
b37df7c4 13764 abort ();
252b5132
RH
13765 }
13766
13767 lastregno = regno;
13768 continue;
13769
13770 case 'P':
13771 if (strncmp (s, "$pc", 3) == 0)
13772 {
13773 s += 3;
13774 continue;
13775 }
13776 break;
13777
252b5132
RH
13778 case '5':
13779 case 'H':
13780 case 'W':
13781 case 'D':
13782 case 'j':
252b5132
RH
13783 case 'V':
13784 case 'C':
13785 case 'U':
13786 case 'k':
13787 case 'K':
d6f16593
MR
13788 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
13789 if (i > 0)
252b5132 13790 {
d6f16593 13791 if (imm_expr.X_op != O_constant)
252b5132 13792 {
df58fc94 13793 forced_insn_length = 4;
5c04167a 13794 ip->insn_opcode |= MIPS16_EXTEND;
252b5132 13795 }
d6f16593
MR
13796 else
13797 {
13798 /* We need to relax this instruction. */
13799 *offset_reloc = *imm_reloc;
13800 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13801 }
13802 s = expr_end;
13803 continue;
252b5132 13804 }
d6f16593
MR
13805 *imm_reloc = BFD_RELOC_UNUSED;
13806 /* Fall through. */
13807 case '<':
13808 case '>':
13809 case '[':
13810 case ']':
13811 case '4':
13812 case '8':
13813 my_getExpression (&imm_expr, s);
252b5132
RH
13814 if (imm_expr.X_op == O_register)
13815 {
13816 /* What we thought was an expression turned out to
13817 be a register. */
13818
13819 if (s[0] == '(' && args[1] == '(')
13820 {
13821 /* It looks like the expression was omitted
13822 before a register indirection, which means
13823 that the expression is implicitly zero. We
13824 still set up imm_expr, so that we handle
13825 explicit extensions correctly. */
13826 imm_expr.X_op = O_constant;
13827 imm_expr.X_add_number = 0;
f6688943 13828 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13829 continue;
13830 }
13831
13832 break;
13833 }
13834
13835 /* We need to relax this instruction. */
f6688943 13836 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13837 s = expr_end;
13838 continue;
13839
13840 case 'p':
13841 case 'q':
13842 case 'A':
13843 case 'B':
13844 case 'E':
13845 /* We use offset_reloc rather than imm_reloc for the PC
13846 relative operands. This lets macros with both
13847 immediate and address operands work correctly. */
13848 my_getExpression (&offset_expr, s);
13849
13850 if (offset_expr.X_op == O_register)
13851 break;
13852
13853 /* We need to relax this instruction. */
f6688943 13854 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13855 s = expr_end;
13856 continue;
13857
13858 case '6': /* break code */
13859 my_getExpression (&imm_expr, s);
13860 check_absolute_expr (ip, &imm_expr);
13861 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
13862 as_warn (_("Invalid value for `%s' (%lu)"),
13863 ip->insn_mo->name,
13864 (unsigned long) imm_expr.X_add_number);
13865 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
13866 imm_expr.X_op = O_absent;
13867 s = expr_end;
13868 continue;
13869
13870 case 'a': /* 26 bit address */
13871 my_getExpression (&offset_expr, s);
13872 s = expr_end;
f6688943 13873 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
13874 ip->insn_opcode <<= 16;
13875 continue;
13876
13877 case 'l': /* register list for entry macro */
13878 case 'L': /* register list for exit macro */
13879 {
13880 int mask;
13881
13882 if (c == 'l')
13883 mask = 0;
13884 else
13885 mask = 7 << 3;
13886 while (*s != '\0')
13887 {
707bfff6 13888 unsigned int freg, reg1, reg2;
252b5132
RH
13889
13890 while (*s == ' ' || *s == ',')
13891 ++s;
707bfff6 13892 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
252b5132 13893 freg = 0;
707bfff6
TS
13894 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
13895 freg = 1;
252b5132
RH
13896 else
13897 {
707bfff6
TS
13898 as_bad (_("can't parse register list"));
13899 break;
252b5132
RH
13900 }
13901 if (*s == ' ')
13902 ++s;
13903 if (*s != '-')
13904 reg2 = reg1;
13905 else
13906 {
13907 ++s;
707bfff6
TS
13908 if (!reg_lookup (&s, freg ? RTYPE_FPU
13909 : (RTYPE_GP | RTYPE_NUM), &reg2))
252b5132 13910 {
707bfff6
TS
13911 as_bad (_("invalid register list"));
13912 break;
252b5132
RH
13913 }
13914 }
13915 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
13916 {
13917 mask &= ~ (7 << 3);
13918 mask |= 5 << 3;
13919 }
13920 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
13921 {
13922 mask &= ~ (7 << 3);
13923 mask |= 6 << 3;
13924 }
13925 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
13926 mask |= (reg2 - 3) << 3;
13927 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
13928 mask |= (reg2 - 15) << 1;
f9419b05 13929 else if (reg1 == RA && reg2 == RA)
252b5132
RH
13930 mask |= 1;
13931 else
13932 {
13933 as_bad (_("invalid register list"));
13934 break;
13935 }
13936 }
13937 /* The mask is filled in in the opcode table for the
13938 benefit of the disassembler. We remove it before
13939 applying the actual mask. */
13940 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
13941 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
13942 }
13943 continue;
13944
0499d65b
TS
13945 case 'm': /* Register list for save insn. */
13946 case 'M': /* Register list for restore insn. */
13947 {
5c04167a 13948 int opcode = ip->insn_opcode;
0499d65b 13949 int framesz = 0, seen_framesz = 0;
91d6fa6a 13950 int nargs = 0, statics = 0, sregs = 0;
0499d65b
TS
13951
13952 while (*s != '\0')
13953 {
13954 unsigned int reg1, reg2;
13955
13956 SKIP_SPACE_TABS (s);
13957 while (*s == ',')
13958 ++s;
13959 SKIP_SPACE_TABS (s);
13960
13961 my_getExpression (&imm_expr, s);
13962 if (imm_expr.X_op == O_constant)
13963 {
13964 /* Handle the frame size. */
13965 if (seen_framesz)
13966 {
13967 as_bad (_("more than one frame size in list"));
13968 break;
13969 }
13970 seen_framesz = 1;
13971 framesz = imm_expr.X_add_number;
13972 imm_expr.X_op = O_absent;
13973 s = expr_end;
13974 continue;
13975 }
13976
707bfff6 13977 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
0499d65b
TS
13978 {
13979 as_bad (_("can't parse register list"));
13980 break;
13981 }
0499d65b 13982
707bfff6
TS
13983 while (*s == ' ')
13984 ++s;
13985
0499d65b
TS
13986 if (*s != '-')
13987 reg2 = reg1;
13988 else
13989 {
13990 ++s;
707bfff6
TS
13991 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
13992 || reg2 < reg1)
0499d65b
TS
13993 {
13994 as_bad (_("can't parse register list"));
13995 break;
13996 }
0499d65b
TS
13997 }
13998
13999 while (reg1 <= reg2)
14000 {
14001 if (reg1 >= 4 && reg1 <= 7)
14002 {
3a93f742 14003 if (!seen_framesz)
0499d65b 14004 /* args $a0-$a3 */
91d6fa6a 14005 nargs |= 1 << (reg1 - 4);
0499d65b
TS
14006 else
14007 /* statics $a0-$a3 */
14008 statics |= 1 << (reg1 - 4);
14009 }
14010 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
14011 {
14012 /* $s0-$s8 */
14013 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
14014 }
14015 else if (reg1 == 31)
14016 {
14017 /* Add $ra to insn. */
14018 opcode |= 0x40;
14019 }
14020 else
14021 {
14022 as_bad (_("unexpected register in list"));
14023 break;
14024 }
14025 if (++reg1 == 24)
14026 reg1 = 30;
14027 }
14028 }
14029
14030 /* Encode args/statics combination. */
91d6fa6a 14031 if (nargs & statics)
0499d65b 14032 as_bad (_("arg/static registers overlap"));
91d6fa6a 14033 else if (nargs == 0xf)
0499d65b
TS
14034 /* All $a0-$a3 are args. */
14035 opcode |= MIPS16_ALL_ARGS << 16;
14036 else if (statics == 0xf)
14037 /* All $a0-$a3 are statics. */
14038 opcode |= MIPS16_ALL_STATICS << 16;
14039 else
14040 {
14041 int narg = 0, nstat = 0;
14042
14043 /* Count arg registers. */
91d6fa6a 14044 while (nargs & 0x1)
0499d65b 14045 {
91d6fa6a 14046 nargs >>= 1;
0499d65b
TS
14047 narg++;
14048 }
91d6fa6a 14049 if (nargs != 0)
0499d65b
TS
14050 as_bad (_("invalid arg register list"));
14051
14052 /* Count static registers. */
14053 while (statics & 0x8)
14054 {
14055 statics = (statics << 1) & 0xf;
14056 nstat++;
14057 }
14058 if (statics != 0)
14059 as_bad (_("invalid static register list"));
14060
14061 /* Encode args/statics. */
14062 opcode |= ((narg << 2) | nstat) << 16;
14063 }
14064
14065 /* Encode $s0/$s1. */
14066 if (sregs & (1 << 0)) /* $s0 */
14067 opcode |= 0x20;
14068 if (sregs & (1 << 1)) /* $s1 */
14069 opcode |= 0x10;
14070 sregs >>= 2;
14071
14072 if (sregs != 0)
14073 {
14074 /* Count regs $s2-$s8. */
14075 int nsreg = 0;
14076 while (sregs & 1)
14077 {
14078 sregs >>= 1;
14079 nsreg++;
14080 }
14081 if (sregs != 0)
14082 as_bad (_("invalid static register list"));
14083 /* Encode $s2-$s8. */
14084 opcode |= nsreg << 24;
14085 }
14086
14087 /* Encode frame size. */
14088 if (!seen_framesz)
14089 as_bad (_("missing frame size"));
14090 else if ((framesz & 7) != 0 || framesz < 0
14091 || framesz > 0xff * 8)
14092 as_bad (_("invalid frame size"));
14093 else if (framesz != 128 || (opcode >> 16) != 0)
14094 {
14095 framesz /= 8;
14096 opcode |= (((framesz & 0xf0) << 16)
14097 | (framesz & 0x0f));
14098 }
14099
14100 /* Finally build the instruction. */
14101 if ((opcode >> 16) != 0 || framesz == 0)
5c04167a
RS
14102 opcode |= MIPS16_EXTEND;
14103 ip->insn_opcode = opcode;
0499d65b
TS
14104 }
14105 continue;
14106
252b5132
RH
14107 case 'e': /* extend code */
14108 my_getExpression (&imm_expr, s);
14109 check_absolute_expr (ip, &imm_expr);
14110 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
14111 {
14112 as_warn (_("Invalid value for `%s' (%lu)"),
14113 ip->insn_mo->name,
14114 (unsigned long) imm_expr.X_add_number);
14115 imm_expr.X_add_number &= 0x7ff;
14116 }
14117 ip->insn_opcode |= imm_expr.X_add_number;
14118 imm_expr.X_op = O_absent;
14119 s = expr_end;
14120 continue;
14121
14122 default:
b37df7c4 14123 abort ();
252b5132
RH
14124 }
14125 break;
14126 }
14127
14128 /* Args don't match. */
14129 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
14130 strcmp (insn->name, insn[1].name) == 0)
14131 {
14132 ++insn;
14133 s = argsstart;
14134 continue;
14135 }
14136
14137 insn_error = _("illegal operands");
14138
14139 return;
14140 }
14141}
14142
14143/* This structure holds information we know about a mips16 immediate
14144 argument type. */
14145
e972090a
NC
14146struct mips16_immed_operand
14147{
252b5132
RH
14148 /* The type code used in the argument string in the opcode table. */
14149 int type;
14150 /* The number of bits in the short form of the opcode. */
14151 int nbits;
14152 /* The number of bits in the extended form of the opcode. */
14153 int extbits;
14154 /* The amount by which the short form is shifted when it is used;
14155 for example, the sw instruction has a shift count of 2. */
14156 int shift;
14157 /* The amount by which the short form is shifted when it is stored
14158 into the instruction code. */
14159 int op_shift;
14160 /* Non-zero if the short form is unsigned. */
14161 int unsp;
14162 /* Non-zero if the extended form is unsigned. */
14163 int extu;
14164 /* Non-zero if the value is PC relative. */
14165 int pcrel;
14166};
14167
14168/* The mips16 immediate operand types. */
14169
14170static const struct mips16_immed_operand mips16_immed_operands[] =
14171{
14172 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14173 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14174 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14175 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14176 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14177 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14178 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14179 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14180 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14181 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14182 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14183 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14184 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14185 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14186 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14187 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14188 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14189 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14190 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14191 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14192 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14193};
14194
14195#define MIPS16_NUM_IMMED \
14196 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14197
b886a2ab
RS
14198/* Marshal immediate value VAL for an extended MIPS16 instruction.
14199 NBITS is the number of significant bits in VAL. */
14200
14201static unsigned long
14202mips16_immed_extend (offsetT val, unsigned int nbits)
14203{
14204 int extval;
14205 if (nbits == 16)
14206 {
14207 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14208 val &= 0x1f;
14209 }
14210 else if (nbits == 15)
14211 {
14212 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14213 val &= 0xf;
14214 }
14215 else
14216 {
14217 extval = ((val & 0x1f) << 6) | (val & 0x20);
14218 val = 0;
14219 }
14220 return (extval << 16) | val;
14221}
14222
5c04167a
RS
14223/* Install immediate value VAL into MIPS16 instruction *INSN,
14224 extending it if necessary. The instruction in *INSN may
14225 already be extended.
14226
43c0598f
RS
14227 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14228 if none. In the former case, VAL is a 16-bit number with no
14229 defined signedness.
14230
14231 TYPE is the type of the immediate field. USER_INSN_LENGTH
14232 is the length that the user requested, or 0 if none. */
252b5132
RH
14233
14234static void
43c0598f
RS
14235mips16_immed (char *file, unsigned int line, int type,
14236 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14237 unsigned int user_insn_length, unsigned long *insn)
252b5132 14238{
3994f87e 14239 const struct mips16_immed_operand *op;
252b5132 14240 int mintiny, maxtiny;
252b5132
RH
14241
14242 op = mips16_immed_operands;
14243 while (op->type != type)
14244 {
14245 ++op;
9c2799c2 14246 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
14247 }
14248
14249 if (op->unsp)
14250 {
14251 if (type == '<' || type == '>' || type == '[' || type == ']')
14252 {
14253 mintiny = 1;
14254 maxtiny = 1 << op->nbits;
14255 }
14256 else
14257 {
14258 mintiny = 0;
14259 maxtiny = (1 << op->nbits) - 1;
14260 }
43c0598f
RS
14261 if (reloc != BFD_RELOC_UNUSED)
14262 val &= 0xffff;
252b5132
RH
14263 }
14264 else
14265 {
14266 mintiny = - (1 << (op->nbits - 1));
14267 maxtiny = (1 << (op->nbits - 1)) - 1;
43c0598f
RS
14268 if (reloc != BFD_RELOC_UNUSED)
14269 val = SEXT_16BIT (val);
252b5132
RH
14270 }
14271
14272 /* Branch offsets have an implicit 0 in the lowest bit. */
14273 if (type == 'p' || type == 'q')
14274 val /= 2;
14275
14276 if ((val & ((1 << op->shift) - 1)) != 0
14277 || val < (mintiny << op->shift)
14278 || val > (maxtiny << op->shift))
5c04167a
RS
14279 {
14280 /* We need an extended instruction. */
14281 if (user_insn_length == 2)
14282 as_bad_where (file, line, _("invalid unextended operand value"));
14283 else
14284 *insn |= MIPS16_EXTEND;
14285 }
14286 else if (user_insn_length == 4)
14287 {
14288 /* The operand doesn't force an unextended instruction to be extended.
14289 Warn if the user wanted an extended instruction anyway. */
14290 *insn |= MIPS16_EXTEND;
14291 as_warn_where (file, line,
14292 _("extended operand requested but not required"));
14293 }
252b5132 14294
5c04167a 14295 if (mips16_opcode_length (*insn) == 2)
252b5132
RH
14296 {
14297 int insnval;
14298
252b5132
RH
14299 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14300 insnval <<= op->op_shift;
14301 *insn |= insnval;
14302 }
14303 else
14304 {
14305 long minext, maxext;
252b5132 14306
43c0598f 14307 if (reloc == BFD_RELOC_UNUSED)
252b5132 14308 {
43c0598f
RS
14309 if (op->extu)
14310 {
14311 minext = 0;
14312 maxext = (1 << op->extbits) - 1;
14313 }
14314 else
14315 {
14316 minext = - (1 << (op->extbits - 1));
14317 maxext = (1 << (op->extbits - 1)) - 1;
14318 }
14319 if (val < minext || val > maxext)
14320 as_bad_where (file, line,
14321 _("operand value out of range for instruction"));
252b5132 14322 }
252b5132 14323
b886a2ab 14324 *insn |= mips16_immed_extend (val, op->extbits);
252b5132
RH
14325 }
14326}
14327\f
d6f16593 14328struct percent_op_match
ad8d3bb3 14329{
5e0116d5
RS
14330 const char *str;
14331 bfd_reloc_code_real_type reloc;
d6f16593
MR
14332};
14333
14334static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14335{
5e0116d5 14336 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 14337#ifdef OBJ_ELF
5e0116d5
RS
14338 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14339 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14340 {"%call16", BFD_RELOC_MIPS_CALL16},
14341 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14342 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14343 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14344 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14345 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14346 {"%got", BFD_RELOC_MIPS_GOT16},
14347 {"%gp_rel", BFD_RELOC_GPREL16},
14348 {"%half", BFD_RELOC_16},
14349 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14350 {"%higher", BFD_RELOC_MIPS_HIGHER},
14351 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14352 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14353 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14354 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14355 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14356 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14357 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14358 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
ad8d3bb3 14359#endif
5e0116d5 14360 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
14361};
14362
d6f16593
MR
14363static const struct percent_op_match mips16_percent_op[] =
14364{
14365 {"%lo", BFD_RELOC_MIPS16_LO16},
14366 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14367 {"%got", BFD_RELOC_MIPS16_GOT16},
14368 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14369 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14370 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14371 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14372 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14373 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14374 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14375 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14376 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14377};
14378
252b5132 14379
5e0116d5
RS
14380/* Return true if *STR points to a relocation operator. When returning true,
14381 move *STR over the operator and store its relocation code in *RELOC.
14382 Leave both *STR and *RELOC alone when returning false. */
14383
14384static bfd_boolean
17a2f251 14385parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14386{
d6f16593
MR
14387 const struct percent_op_match *percent_op;
14388 size_t limit, i;
14389
14390 if (mips_opts.mips16)
14391 {
14392 percent_op = mips16_percent_op;
14393 limit = ARRAY_SIZE (mips16_percent_op);
14394 }
14395 else
14396 {
14397 percent_op = mips_percent_op;
14398 limit = ARRAY_SIZE (mips_percent_op);
14399 }
76b3015f 14400
d6f16593 14401 for (i = 0; i < limit; i++)
5e0116d5 14402 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14403 {
3f98094e
DJ
14404 int len = strlen (percent_op[i].str);
14405
14406 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14407 continue;
14408
5e0116d5
RS
14409 *str += strlen (percent_op[i].str);
14410 *reloc = percent_op[i].reloc;
394f9b3a 14411
5e0116d5
RS
14412 /* Check whether the output BFD supports this relocation.
14413 If not, issue an error and fall back on something safe. */
14414 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14415 {
20203fb9 14416 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14417 percent_op[i].str);
01a3f561 14418 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14419 }
5e0116d5 14420 return TRUE;
394f9b3a 14421 }
5e0116d5 14422 return FALSE;
394f9b3a 14423}
ad8d3bb3 14424
ad8d3bb3 14425
5e0116d5
RS
14426/* Parse string STR as a 16-bit relocatable operand. Store the
14427 expression in *EP and the relocations in the array starting
14428 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14429
01a3f561 14430 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14431
5e0116d5 14432static size_t
17a2f251
TS
14433my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14434 char *str)
ad8d3bb3 14435{
5e0116d5
RS
14436 bfd_reloc_code_real_type reversed_reloc[3];
14437 size_t reloc_index, i;
09b8f35a
RS
14438 int crux_depth, str_depth;
14439 char *crux;
5e0116d5
RS
14440
14441 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14442 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14443 of the main expression and with CRUX_DEPTH containing the number
14444 of open brackets at that point. */
14445 reloc_index = -1;
14446 str_depth = 0;
14447 do
fb1b3232 14448 {
09b8f35a
RS
14449 reloc_index++;
14450 crux = str;
14451 crux_depth = str_depth;
14452
14453 /* Skip over whitespace and brackets, keeping count of the number
14454 of brackets. */
14455 while (*str == ' ' || *str == '\t' || *str == '(')
14456 if (*str++ == '(')
14457 str_depth++;
5e0116d5 14458 }
09b8f35a
RS
14459 while (*str == '%'
14460 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14461 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14462
09b8f35a 14463 my_getExpression (ep, crux);
5e0116d5 14464 str = expr_end;
394f9b3a 14465
5e0116d5 14466 /* Match every open bracket. */
09b8f35a 14467 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14468 if (*str++ == ')')
09b8f35a 14469 crux_depth--;
394f9b3a 14470
09b8f35a 14471 if (crux_depth > 0)
20203fb9 14472 as_bad (_("unclosed '('"));
394f9b3a 14473
5e0116d5 14474 expr_end = str;
252b5132 14475
01a3f561 14476 if (reloc_index != 0)
64bdfcaf
RS
14477 {
14478 prev_reloc_op_frag = frag_now;
14479 for (i = 0; i < reloc_index; i++)
14480 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14481 }
fb1b3232 14482
5e0116d5 14483 return reloc_index;
252b5132
RH
14484}
14485
14486static void
17a2f251 14487my_getExpression (expressionS *ep, char *str)
252b5132
RH
14488{
14489 char *save_in;
14490
14491 save_in = input_line_pointer;
14492 input_line_pointer = str;
14493 expression (ep);
14494 expr_end = input_line_pointer;
14495 input_line_pointer = save_in;
252b5132
RH
14496}
14497
252b5132 14498char *
17a2f251 14499md_atof (int type, char *litP, int *sizeP)
252b5132 14500{
499ac353 14501 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14502}
14503
14504void
17a2f251 14505md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14506{
14507 if (target_big_endian)
14508 number_to_chars_bigendian (buf, val, n);
14509 else
14510 number_to_chars_littleendian (buf, val, n);
14511}
14512\f
ae948b86 14513#ifdef OBJ_ELF
e013f690
TS
14514static int support_64bit_objects(void)
14515{
14516 const char **list, **l;
aa3d8fdf 14517 int yes;
e013f690
TS
14518
14519 list = bfd_target_list ();
14520 for (l = list; *l != NULL; l++)
aeffff67
RS
14521 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14522 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14523 break;
aa3d8fdf 14524 yes = (*l != NULL);
e013f690 14525 free (list);
aa3d8fdf 14526 return yes;
e013f690 14527}
ae948b86 14528#endif /* OBJ_ELF */
e013f690 14529
78849248 14530const char *md_shortopts = "O::g::G:";
252b5132 14531
23fce1e3
NC
14532enum options
14533 {
14534 OPTION_MARCH = OPTION_MD_BASE,
14535 OPTION_MTUNE,
14536 OPTION_MIPS1,
14537 OPTION_MIPS2,
14538 OPTION_MIPS3,
14539 OPTION_MIPS4,
14540 OPTION_MIPS5,
14541 OPTION_MIPS32,
14542 OPTION_MIPS64,
14543 OPTION_MIPS32R2,
14544 OPTION_MIPS64R2,
14545 OPTION_MIPS16,
14546 OPTION_NO_MIPS16,
14547 OPTION_MIPS3D,
14548 OPTION_NO_MIPS3D,
14549 OPTION_MDMX,
14550 OPTION_NO_MDMX,
14551 OPTION_DSP,
14552 OPTION_NO_DSP,
14553 OPTION_MT,
14554 OPTION_NO_MT,
b015e599
AP
14555 OPTION_VIRT,
14556 OPTION_NO_VIRT,
23fce1e3
NC
14557 OPTION_SMARTMIPS,
14558 OPTION_NO_SMARTMIPS,
14559 OPTION_DSPR2,
14560 OPTION_NO_DSPR2,
df58fc94
RS
14561 OPTION_MICROMIPS,
14562 OPTION_NO_MICROMIPS,
dec0624d
MR
14563 OPTION_MCU,
14564 OPTION_NO_MCU,
23fce1e3
NC
14565 OPTION_COMPAT_ARCH_BASE,
14566 OPTION_M4650,
14567 OPTION_NO_M4650,
14568 OPTION_M4010,
14569 OPTION_NO_M4010,
14570 OPTION_M4100,
14571 OPTION_NO_M4100,
14572 OPTION_M3900,
14573 OPTION_NO_M3900,
14574 OPTION_M7000_HILO_FIX,
6a32d874
CM
14575 OPTION_MNO_7000_HILO_FIX,
14576 OPTION_FIX_24K,
14577 OPTION_NO_FIX_24K,
c67a084a
NC
14578 OPTION_FIX_LOONGSON2F_JUMP,
14579 OPTION_NO_FIX_LOONGSON2F_JUMP,
14580 OPTION_FIX_LOONGSON2F_NOP,
14581 OPTION_NO_FIX_LOONGSON2F_NOP,
23fce1e3
NC
14582 OPTION_FIX_VR4120,
14583 OPTION_NO_FIX_VR4120,
14584 OPTION_FIX_VR4130,
14585 OPTION_NO_FIX_VR4130,
d954098f
DD
14586 OPTION_FIX_CN63XXP1,
14587 OPTION_NO_FIX_CN63XXP1,
23fce1e3
NC
14588 OPTION_TRAP,
14589 OPTION_BREAK,
14590 OPTION_EB,
14591 OPTION_EL,
14592 OPTION_FP32,
14593 OPTION_GP32,
14594 OPTION_CONSTRUCT_FLOATS,
14595 OPTION_NO_CONSTRUCT_FLOATS,
14596 OPTION_FP64,
14597 OPTION_GP64,
14598 OPTION_RELAX_BRANCH,
14599 OPTION_NO_RELAX_BRANCH,
14600 OPTION_MSHARED,
14601 OPTION_MNO_SHARED,
14602 OPTION_MSYM32,
14603 OPTION_MNO_SYM32,
14604 OPTION_SOFT_FLOAT,
14605 OPTION_HARD_FLOAT,
14606 OPTION_SINGLE_FLOAT,
14607 OPTION_DOUBLE_FLOAT,
14608 OPTION_32,
14609#ifdef OBJ_ELF
14610 OPTION_CALL_SHARED,
14611 OPTION_CALL_NONPIC,
14612 OPTION_NON_SHARED,
14613 OPTION_XGOT,
14614 OPTION_MABI,
14615 OPTION_N32,
14616 OPTION_64,
14617 OPTION_MDEBUG,
14618 OPTION_NO_MDEBUG,
14619 OPTION_PDR,
14620 OPTION_NO_PDR,
14621 OPTION_MVXWORKS_PIC,
14622#endif /* OBJ_ELF */
14623 OPTION_END_OF_ENUM
14624 };
14625
e972090a
NC
14626struct option md_longopts[] =
14627{
f9b4148d 14628 /* Options which specify architecture. */
f9b4148d 14629 {"march", required_argument, NULL, OPTION_MARCH},
f9b4148d 14630 {"mtune", required_argument, NULL, OPTION_MTUNE},
252b5132
RH
14631 {"mips0", no_argument, NULL, OPTION_MIPS1},
14632 {"mips1", no_argument, NULL, OPTION_MIPS1},
252b5132 14633 {"mips2", no_argument, NULL, OPTION_MIPS2},
252b5132 14634 {"mips3", no_argument, NULL, OPTION_MIPS3},
252b5132 14635 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86 14636 {"mips5", no_argument, NULL, OPTION_MIPS5},
ae948b86 14637 {"mips32", no_argument, NULL, OPTION_MIPS32},
ae948b86 14638 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d 14639 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13 14640 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
14641
14642 /* Options which specify Application Specific Extensions (ASEs). */
f9b4148d 14643 {"mips16", no_argument, NULL, OPTION_MIPS16},
f9b4148d 14644 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
f9b4148d 14645 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
f9b4148d 14646 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
f9b4148d 14647 {"mdmx", no_argument, NULL, OPTION_MDMX},
f9b4148d 14648 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
74cd071d 14649 {"mdsp", no_argument, NULL, OPTION_DSP},
74cd071d 14650 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
ef2e4d86 14651 {"mmt", no_argument, NULL, OPTION_MT},
ef2e4d86 14652 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
e16bfa71 14653 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
e16bfa71 14654 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
8b082fb1 14655 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
8b082fb1 14656 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
df58fc94
RS
14657 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
14658 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
dec0624d
MR
14659 {"mmcu", no_argument, NULL, OPTION_MCU},
14660 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
b015e599
AP
14661 {"mvirt", no_argument, NULL, OPTION_VIRT},
14662 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
f9b4148d
CD
14663
14664 /* Old-style architecture options. Don't add more of these. */
f9b4148d 14665 {"m4650", no_argument, NULL, OPTION_M4650},
f9b4148d 14666 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
f9b4148d 14667 {"m4010", no_argument, NULL, OPTION_M4010},
f9b4148d 14668 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
f9b4148d 14669 {"m4100", no_argument, NULL, OPTION_M4100},
f9b4148d 14670 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
f9b4148d 14671 {"m3900", no_argument, NULL, OPTION_M3900},
f9b4148d
CD
14672 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
14673
14674 /* Options which enable bug fixes. */
f9b4148d 14675 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
f9b4148d
CD
14676 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14677 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
c67a084a
NC
14678 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
14679 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
14680 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
14681 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
d766e8ec
RS
14682 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
14683 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
7d8e00cf
RS
14684 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
14685 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
6a32d874
CM
14686 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
14687 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
d954098f
DD
14688 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
14689 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
f9b4148d
CD
14690
14691 /* Miscellaneous options. */
252b5132
RH
14692 {"trap", no_argument, NULL, OPTION_TRAP},
14693 {"no-break", no_argument, NULL, OPTION_TRAP},
252b5132
RH
14694 {"break", no_argument, NULL, OPTION_BREAK},
14695 {"no-trap", no_argument, NULL, OPTION_BREAK},
252b5132 14696 {"EB", no_argument, NULL, OPTION_EB},
252b5132 14697 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 14698 {"mfp32", no_argument, NULL, OPTION_FP32},
c97ef257 14699 {"mgp32", no_argument, NULL, OPTION_GP32},
119d663a 14700 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
119d663a 14701 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
316f5878 14702 {"mfp64", no_argument, NULL, OPTION_FP64},
ae948b86 14703 {"mgp64", no_argument, NULL, OPTION_GP64},
4a6a3df4
AO
14704 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
14705 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
aa6975fb
ILT
14706 {"mshared", no_argument, NULL, OPTION_MSHARED},
14707 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
aed1a261
RS
14708 {"msym32", no_argument, NULL, OPTION_MSYM32},
14709 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
037b32b9
AN
14710 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
14711 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
037b32b9
AN
14712 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
14713 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
23fce1e3
NC
14714
14715 /* Strictly speaking this next option is ELF specific,
14716 but we allow it for other ports as well in order to
14717 make testing easier. */
14718 {"32", no_argument, NULL, OPTION_32},
037b32b9 14719
f9b4148d 14720 /* ELF-specific options. */
156c2f8b 14721#ifdef OBJ_ELF
156c2f8b
NC
14722 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
14723 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
861fb55a 14724 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
156c2f8b
NC
14725 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
14726 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86 14727 {"mabi", required_argument, NULL, OPTION_MABI},
e013f690 14728 {"n32", no_argument, NULL, OPTION_N32},
156c2f8b 14729 {"64", no_argument, NULL, OPTION_64},
ecb4347a 14730 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
ecb4347a 14731 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe 14732 {"mpdr", no_argument, NULL, OPTION_PDR},
dcd410fe 14733 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
0a44bf69 14734 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ae948b86 14735#endif /* OBJ_ELF */
f9b4148d 14736
252b5132
RH
14737 {NULL, no_argument, NULL, 0}
14738};
156c2f8b 14739size_t md_longopts_size = sizeof (md_longopts);
252b5132 14740
316f5878
RS
14741/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14742 NEW_VALUE. Warn if another value was already specified. Note:
14743 we have to defer parsing the -march and -mtune arguments in order
14744 to handle 'from-abi' correctly, since the ABI might be specified
14745 in a later argument. */
14746
14747static void
17a2f251 14748mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14749{
14750 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14751 as_warn (_("A different %s was already specified, is now %s"),
14752 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14753 new_value);
14754
14755 *string_ptr = new_value;
14756}
14757
252b5132 14758int
17a2f251 14759md_parse_option (int c, char *arg)
252b5132
RH
14760{
14761 switch (c)
14762 {
119d663a
NC
14763 case OPTION_CONSTRUCT_FLOATS:
14764 mips_disable_float_construction = 0;
14765 break;
bdaaa2e1 14766
119d663a
NC
14767 case OPTION_NO_CONSTRUCT_FLOATS:
14768 mips_disable_float_construction = 1;
14769 break;
bdaaa2e1 14770
252b5132
RH
14771 case OPTION_TRAP:
14772 mips_trap = 1;
14773 break;
14774
14775 case OPTION_BREAK:
14776 mips_trap = 0;
14777 break;
14778
14779 case OPTION_EB:
14780 target_big_endian = 1;
14781 break;
14782
14783 case OPTION_EL:
14784 target_big_endian = 0;
14785 break;
14786
14787 case 'O':
4ffff32f
TS
14788 if (arg == NULL)
14789 mips_optimize = 1;
14790 else if (arg[0] == '0')
14791 mips_optimize = 0;
14792 else if (arg[0] == '1')
252b5132
RH
14793 mips_optimize = 1;
14794 else
14795 mips_optimize = 2;
14796 break;
14797
14798 case 'g':
14799 if (arg == NULL)
14800 mips_debug = 2;
14801 else
14802 mips_debug = atoi (arg);
252b5132
RH
14803 break;
14804
14805 case OPTION_MIPS1:
316f5878 14806 file_mips_isa = ISA_MIPS1;
252b5132
RH
14807 break;
14808
14809 case OPTION_MIPS2:
316f5878 14810 file_mips_isa = ISA_MIPS2;
252b5132
RH
14811 break;
14812
14813 case OPTION_MIPS3:
316f5878 14814 file_mips_isa = ISA_MIPS3;
252b5132
RH
14815 break;
14816
14817 case OPTION_MIPS4:
316f5878 14818 file_mips_isa = ISA_MIPS4;
e7af610e
NC
14819 break;
14820
84ea6cf2 14821 case OPTION_MIPS5:
316f5878 14822 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
14823 break;
14824
e7af610e 14825 case OPTION_MIPS32:
316f5878 14826 file_mips_isa = ISA_MIPS32;
252b5132
RH
14827 break;
14828
af7ee8bf
CD
14829 case OPTION_MIPS32R2:
14830 file_mips_isa = ISA_MIPS32R2;
14831 break;
14832
5f74bc13
CD
14833 case OPTION_MIPS64R2:
14834 file_mips_isa = ISA_MIPS64R2;
14835 break;
14836
84ea6cf2 14837 case OPTION_MIPS64:
316f5878 14838 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
14839 break;
14840
ec68c924 14841 case OPTION_MTUNE:
316f5878
RS
14842 mips_set_option_string (&mips_tune_string, arg);
14843 break;
ec68c924 14844
316f5878
RS
14845 case OPTION_MARCH:
14846 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14847 break;
14848
14849 case OPTION_M4650:
316f5878
RS
14850 mips_set_option_string (&mips_arch_string, "4650");
14851 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14852 break;
14853
14854 case OPTION_NO_M4650:
14855 break;
14856
14857 case OPTION_M4010:
316f5878
RS
14858 mips_set_option_string (&mips_arch_string, "4010");
14859 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14860 break;
14861
14862 case OPTION_NO_M4010:
14863 break;
14864
14865 case OPTION_M4100:
316f5878
RS
14866 mips_set_option_string (&mips_arch_string, "4100");
14867 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14868 break;
14869
14870 case OPTION_NO_M4100:
14871 break;
14872
252b5132 14873 case OPTION_M3900:
316f5878
RS
14874 mips_set_option_string (&mips_arch_string, "3900");
14875 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14876 break;
bdaaa2e1 14877
252b5132
RH
14878 case OPTION_NO_M3900:
14879 break;
14880
deec1734
CD
14881 case OPTION_MDMX:
14882 mips_opts.ase_mdmx = 1;
14883 break;
14884
14885 case OPTION_NO_MDMX:
14886 mips_opts.ase_mdmx = 0;
14887 break;
14888
74cd071d
CF
14889 case OPTION_DSP:
14890 mips_opts.ase_dsp = 1;
8b082fb1 14891 mips_opts.ase_dspr2 = 0;
74cd071d
CF
14892 break;
14893
14894 case OPTION_NO_DSP:
8b082fb1
TS
14895 mips_opts.ase_dsp = 0;
14896 mips_opts.ase_dspr2 = 0;
14897 break;
14898
14899 case OPTION_DSPR2:
14900 mips_opts.ase_dspr2 = 1;
14901 mips_opts.ase_dsp = 1;
14902 break;
14903
14904 case OPTION_NO_DSPR2:
14905 mips_opts.ase_dspr2 = 0;
74cd071d
CF
14906 mips_opts.ase_dsp = 0;
14907 break;
14908
ef2e4d86
CF
14909 case OPTION_MT:
14910 mips_opts.ase_mt = 1;
14911 break;
14912
14913 case OPTION_NO_MT:
14914 mips_opts.ase_mt = 0;
14915 break;
14916
dec0624d
MR
14917 case OPTION_MCU:
14918 mips_opts.ase_mcu = 1;
14919 break;
14920
14921 case OPTION_NO_MCU:
14922 mips_opts.ase_mcu = 0;
14923 break;
14924
df58fc94
RS
14925 case OPTION_MICROMIPS:
14926 if (mips_opts.mips16 == 1)
14927 {
14928 as_bad (_("-mmicromips cannot be used with -mips16"));
14929 return 0;
14930 }
14931 mips_opts.micromips = 1;
14932 mips_no_prev_insn ();
14933 break;
14934
14935 case OPTION_NO_MICROMIPS:
14936 mips_opts.micromips = 0;
14937 mips_no_prev_insn ();
14938 break;
14939
b015e599
AP
14940 case OPTION_VIRT:
14941 mips_opts.ase_virt = 1;
14942 break;
14943
14944 case OPTION_NO_VIRT:
14945 mips_opts.ase_virt = 0;
14946 break;
14947
252b5132 14948 case OPTION_MIPS16:
df58fc94
RS
14949 if (mips_opts.micromips == 1)
14950 {
14951 as_bad (_("-mips16 cannot be used with -micromips"));
14952 return 0;
14953 }
252b5132 14954 mips_opts.mips16 = 1;
7d10b47d 14955 mips_no_prev_insn ();
252b5132
RH
14956 break;
14957
14958 case OPTION_NO_MIPS16:
14959 mips_opts.mips16 = 0;
7d10b47d 14960 mips_no_prev_insn ();
252b5132
RH
14961 break;
14962
1f25f5d3
CD
14963 case OPTION_MIPS3D:
14964 mips_opts.ase_mips3d = 1;
14965 break;
14966
14967 case OPTION_NO_MIPS3D:
14968 mips_opts.ase_mips3d = 0;
14969 break;
14970
e16bfa71
TS
14971 case OPTION_SMARTMIPS:
14972 mips_opts.ase_smartmips = 1;
14973 break;
14974
14975 case OPTION_NO_SMARTMIPS:
14976 mips_opts.ase_smartmips = 0;
14977 break;
14978
6a32d874
CM
14979 case OPTION_FIX_24K:
14980 mips_fix_24k = 1;
14981 break;
14982
14983 case OPTION_NO_FIX_24K:
14984 mips_fix_24k = 0;
14985 break;
14986
c67a084a
NC
14987 case OPTION_FIX_LOONGSON2F_JUMP:
14988 mips_fix_loongson2f_jump = TRUE;
14989 break;
14990
14991 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14992 mips_fix_loongson2f_jump = FALSE;
14993 break;
14994
14995 case OPTION_FIX_LOONGSON2F_NOP:
14996 mips_fix_loongson2f_nop = TRUE;
14997 break;
14998
14999 case OPTION_NO_FIX_LOONGSON2F_NOP:
15000 mips_fix_loongson2f_nop = FALSE;
15001 break;
15002
d766e8ec
RS
15003 case OPTION_FIX_VR4120:
15004 mips_fix_vr4120 = 1;
60b63b72
RS
15005 break;
15006
d766e8ec
RS
15007 case OPTION_NO_FIX_VR4120:
15008 mips_fix_vr4120 = 0;
60b63b72
RS
15009 break;
15010
7d8e00cf
RS
15011 case OPTION_FIX_VR4130:
15012 mips_fix_vr4130 = 1;
15013 break;
15014
15015 case OPTION_NO_FIX_VR4130:
15016 mips_fix_vr4130 = 0;
15017 break;
15018
d954098f
DD
15019 case OPTION_FIX_CN63XXP1:
15020 mips_fix_cn63xxp1 = TRUE;
15021 break;
15022
15023 case OPTION_NO_FIX_CN63XXP1:
15024 mips_fix_cn63xxp1 = FALSE;
15025 break;
15026
4a6a3df4
AO
15027 case OPTION_RELAX_BRANCH:
15028 mips_relax_branch = 1;
15029 break;
15030
15031 case OPTION_NO_RELAX_BRANCH:
15032 mips_relax_branch = 0;
15033 break;
15034
aa6975fb
ILT
15035 case OPTION_MSHARED:
15036 mips_in_shared = TRUE;
15037 break;
15038
15039 case OPTION_MNO_SHARED:
15040 mips_in_shared = FALSE;
15041 break;
15042
aed1a261
RS
15043 case OPTION_MSYM32:
15044 mips_opts.sym32 = TRUE;
15045 break;
15046
15047 case OPTION_MNO_SYM32:
15048 mips_opts.sym32 = FALSE;
15049 break;
15050
0f074f60 15051#ifdef OBJ_ELF
252b5132
RH
15052 /* When generating ELF code, we permit -KPIC and -call_shared to
15053 select SVR4_PIC, and -non_shared to select no PIC. This is
15054 intended to be compatible with Irix 5. */
15055 case OPTION_CALL_SHARED:
f43abd2b 15056 if (!IS_ELF)
252b5132
RH
15057 {
15058 as_bad (_("-call_shared is supported only for ELF format"));
15059 return 0;
15060 }
15061 mips_pic = SVR4_PIC;
143d77c5 15062 mips_abicalls = TRUE;
252b5132
RH
15063 break;
15064
861fb55a
DJ
15065 case OPTION_CALL_NONPIC:
15066 if (!IS_ELF)
15067 {
15068 as_bad (_("-call_nonpic is supported only for ELF format"));
15069 return 0;
15070 }
15071 mips_pic = NO_PIC;
15072 mips_abicalls = TRUE;
15073 break;
15074
252b5132 15075 case OPTION_NON_SHARED:
f43abd2b 15076 if (!IS_ELF)
252b5132
RH
15077 {
15078 as_bad (_("-non_shared is supported only for ELF format"));
15079 return 0;
15080 }
15081 mips_pic = NO_PIC;
143d77c5 15082 mips_abicalls = FALSE;
252b5132
RH
15083 break;
15084
44075ae2
TS
15085 /* The -xgot option tells the assembler to use 32 bit offsets
15086 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
15087 compatibility. */
15088 case OPTION_XGOT:
15089 mips_big_got = 1;
15090 break;
0f074f60 15091#endif /* OBJ_ELF */
252b5132
RH
15092
15093 case 'G':
6caf9ef4
TS
15094 g_switch_value = atoi (arg);
15095 g_switch_seen = 1;
252b5132
RH
15096 break;
15097
34ba82a8
TS
15098 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15099 and -mabi=64. */
252b5132 15100 case OPTION_32:
23fce1e3
NC
15101 if (IS_ELF)
15102 mips_abi = O32_ABI;
15103 /* We silently ignore -32 for non-ELF targets. This greatly
15104 simplifies the construction of the MIPS GAS test cases. */
252b5132
RH
15105 break;
15106
23fce1e3 15107#ifdef OBJ_ELF
e013f690 15108 case OPTION_N32:
f43abd2b 15109 if (!IS_ELF)
34ba82a8
TS
15110 {
15111 as_bad (_("-n32 is supported for ELF format only"));
15112 return 0;
15113 }
316f5878 15114 mips_abi = N32_ABI;
e013f690 15115 break;
252b5132 15116
e013f690 15117 case OPTION_64:
f43abd2b 15118 if (!IS_ELF)
34ba82a8
TS
15119 {
15120 as_bad (_("-64 is supported for ELF format only"));
15121 return 0;
15122 }
316f5878 15123 mips_abi = N64_ABI;
f43abd2b 15124 if (!support_64bit_objects())
e013f690 15125 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 15126 break;
ae948b86 15127#endif /* OBJ_ELF */
252b5132 15128
c97ef257 15129 case OPTION_GP32:
a325df1d 15130 file_mips_gp32 = 1;
c97ef257
AH
15131 break;
15132
15133 case OPTION_GP64:
a325df1d 15134 file_mips_gp32 = 0;
c97ef257 15135 break;
252b5132 15136
ca4e0257 15137 case OPTION_FP32:
a325df1d 15138 file_mips_fp32 = 1;
316f5878
RS
15139 break;
15140
15141 case OPTION_FP64:
15142 file_mips_fp32 = 0;
ca4e0257
RS
15143 break;
15144
037b32b9
AN
15145 case OPTION_SINGLE_FLOAT:
15146 file_mips_single_float = 1;
15147 break;
15148
15149 case OPTION_DOUBLE_FLOAT:
15150 file_mips_single_float = 0;
15151 break;
15152
15153 case OPTION_SOFT_FLOAT:
15154 file_mips_soft_float = 1;
15155 break;
15156
15157 case OPTION_HARD_FLOAT:
15158 file_mips_soft_float = 0;
15159 break;
15160
ae948b86 15161#ifdef OBJ_ELF
252b5132 15162 case OPTION_MABI:
f43abd2b 15163 if (!IS_ELF)
34ba82a8
TS
15164 {
15165 as_bad (_("-mabi is supported for ELF format only"));
15166 return 0;
15167 }
e013f690 15168 if (strcmp (arg, "32") == 0)
316f5878 15169 mips_abi = O32_ABI;
e013f690 15170 else if (strcmp (arg, "o64") == 0)
316f5878 15171 mips_abi = O64_ABI;
e013f690 15172 else if (strcmp (arg, "n32") == 0)
316f5878 15173 mips_abi = N32_ABI;
e013f690
TS
15174 else if (strcmp (arg, "64") == 0)
15175 {
316f5878 15176 mips_abi = N64_ABI;
e013f690
TS
15177 if (! support_64bit_objects())
15178 as_fatal (_("No compiled in support for 64 bit object file "
15179 "format"));
15180 }
15181 else if (strcmp (arg, "eabi") == 0)
316f5878 15182 mips_abi = EABI_ABI;
e013f690 15183 else
da0e507f
TS
15184 {
15185 as_fatal (_("invalid abi -mabi=%s"), arg);
15186 return 0;
15187 }
252b5132 15188 break;
e013f690 15189#endif /* OBJ_ELF */
252b5132 15190
6b76fefe 15191 case OPTION_M7000_HILO_FIX:
b34976b6 15192 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
15193 break;
15194
9ee72ff1 15195 case OPTION_MNO_7000_HILO_FIX:
b34976b6 15196 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
15197 break;
15198
ecb4347a
DJ
15199#ifdef OBJ_ELF
15200 case OPTION_MDEBUG:
b34976b6 15201 mips_flag_mdebug = TRUE;
ecb4347a
DJ
15202 break;
15203
15204 case OPTION_NO_MDEBUG:
b34976b6 15205 mips_flag_mdebug = FALSE;
ecb4347a 15206 break;
dcd410fe
RO
15207
15208 case OPTION_PDR:
15209 mips_flag_pdr = TRUE;
15210 break;
15211
15212 case OPTION_NO_PDR:
15213 mips_flag_pdr = FALSE;
15214 break;
0a44bf69
RS
15215
15216 case OPTION_MVXWORKS_PIC:
15217 mips_pic = VXWORKS_PIC;
15218 break;
ecb4347a
DJ
15219#endif /* OBJ_ELF */
15220
252b5132
RH
15221 default:
15222 return 0;
15223 }
15224
c67a084a
NC
15225 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15226
252b5132
RH
15227 return 1;
15228}
316f5878
RS
15229\f
15230/* Set up globals to generate code for the ISA or processor
15231 described by INFO. */
252b5132 15232
252b5132 15233static void
17a2f251 15234mips_set_architecture (const struct mips_cpu_info *info)
252b5132 15235{
316f5878 15236 if (info != 0)
252b5132 15237 {
fef14a42
TS
15238 file_mips_arch = info->cpu;
15239 mips_opts.arch = info->cpu;
316f5878 15240 mips_opts.isa = info->isa;
252b5132 15241 }
252b5132
RH
15242}
15243
252b5132 15244
316f5878 15245/* Likewise for tuning. */
252b5132 15246
316f5878 15247static void
17a2f251 15248mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
15249{
15250 if (info != 0)
fef14a42 15251 mips_tune = info->cpu;
316f5878 15252}
80cc45a5 15253
34ba82a8 15254
252b5132 15255void
17a2f251 15256mips_after_parse_args (void)
e9670677 15257{
fef14a42
TS
15258 const struct mips_cpu_info *arch_info = 0;
15259 const struct mips_cpu_info *tune_info = 0;
15260
e9670677 15261 /* GP relative stuff not working for PE */
6caf9ef4 15262 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 15263 {
6caf9ef4 15264 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
15265 as_bad (_("-G not supported in this configuration."));
15266 g_switch_value = 0;
15267 }
15268
cac012d6
AO
15269 if (mips_abi == NO_ABI)
15270 mips_abi = MIPS_DEFAULT_ABI;
15271
22923709
RS
15272 /* The following code determines the architecture and register size.
15273 Similar code was added to GCC 3.3 (see override_options() in
15274 config/mips/mips.c). The GAS and GCC code should be kept in sync
15275 as much as possible. */
e9670677 15276
316f5878 15277 if (mips_arch_string != 0)
fef14a42 15278 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 15279
316f5878 15280 if (file_mips_isa != ISA_UNKNOWN)
e9670677 15281 {
316f5878 15282 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 15283 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 15284 the -march selection (if any). */
fef14a42 15285 if (arch_info != 0)
e9670677 15286 {
316f5878
RS
15287 /* -march takes precedence over -mipsN, since it is more descriptive.
15288 There's no harm in specifying both as long as the ISA levels
15289 are the same. */
fef14a42 15290 if (file_mips_isa != arch_info->isa)
316f5878
RS
15291 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15292 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 15293 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15294 }
316f5878 15295 else
fef14a42 15296 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
15297 }
15298
fef14a42 15299 if (arch_info == 0)
95bfe26e
MF
15300 {
15301 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15302 gas_assert (arch_info);
15303 }
e9670677 15304
fef14a42 15305 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15306 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15307 arch_info->name);
15308
15309 mips_set_architecture (arch_info);
15310
15311 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
15312 if (mips_tune_string != 0)
15313 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15314
fef14a42
TS
15315 if (tune_info == 0)
15316 mips_set_tune (arch_info);
15317 else
15318 mips_set_tune (tune_info);
e9670677 15319
316f5878 15320 if (file_mips_gp32 >= 0)
e9670677 15321 {
316f5878
RS
15322 /* The user specified the size of the integer registers. Make sure
15323 it agrees with the ABI and ISA. */
15324 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15325 as_bad (_("-mgp64 used with a 32-bit processor"));
15326 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15327 as_bad (_("-mgp32 used with a 64-bit ABI"));
15328 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15329 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
15330 }
15331 else
15332 {
316f5878
RS
15333 /* Infer the integer register size from the ABI and processor.
15334 Restrict ourselves to 32-bit registers if that's all the
15335 processor has, or if the ABI cannot handle 64-bit registers. */
15336 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15337 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
15338 }
15339
ad3fea08
TS
15340 switch (file_mips_fp32)
15341 {
15342 default:
15343 case -1:
15344 /* No user specified float register size.
15345 ??? GAS treats single-float processors as though they had 64-bit
15346 float registers (although it complains when double-precision
15347 instructions are used). As things stand, saying they have 32-bit
15348 registers would lead to spurious "register must be even" messages.
15349 So here we assume float registers are never smaller than the
15350 integer ones. */
15351 if (file_mips_gp32 == 0)
15352 /* 64-bit integer registers implies 64-bit float registers. */
15353 file_mips_fp32 = 0;
15354 else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
15355 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15356 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
15357 file_mips_fp32 = 0;
15358 else
15359 /* 32-bit float registers. */
15360 file_mips_fp32 = 1;
15361 break;
15362
15363 /* The user specified the size of the float registers. Check if it
15364 agrees with the ABI and ISA. */
15365 case 0:
15366 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15367 as_bad (_("-mfp64 used with a 32-bit fpu"));
15368 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15369 && !ISA_HAS_MXHC1 (mips_opts.isa))
15370 as_warn (_("-mfp64 used with a 32-bit ABI"));
15371 break;
15372 case 1:
15373 if (ABI_NEEDS_64BIT_REGS (mips_abi))
15374 as_warn (_("-mfp32 used with a 64-bit ABI"));
15375 break;
15376 }
e9670677 15377
316f5878 15378 /* End of GCC-shared inference code. */
e9670677 15379
17a2f251
TS
15380 /* This flag is set when we have a 64-bit capable CPU but use only
15381 32-bit wide registers. Note that EABI does not use it. */
15382 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15383 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15384 || mips_abi == O32_ABI))
316f5878 15385 mips_32bitmode = 1;
e9670677
MR
15386
15387 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15388 as_bad (_("trap exception not supported at ISA 1"));
15389
e9670677
MR
15390 /* If the selected architecture includes support for ASEs, enable
15391 generation of code for them. */
a4672219 15392 if (mips_opts.mips16 == -1)
fef14a42 15393 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
df58fc94
RS
15394 if (mips_opts.micromips == -1)
15395 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
ffdefa66 15396 if (mips_opts.ase_mips3d == -1)
65263ce3 15397 mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
ad3fea08
TS
15398 && file_mips_fp32 == 0) ? 1 : 0;
15399 if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
15400 as_bad (_("-mfp32 used with -mips3d"));
15401
ffdefa66 15402 if (mips_opts.ase_mdmx == -1)
65263ce3 15403 mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
ad3fea08
TS
15404 && file_mips_fp32 == 0) ? 1 : 0;
15405 if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
15406 as_bad (_("-mfp32 used with -mdmx"));
15407
15408 if (mips_opts.ase_smartmips == -1)
15409 mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
15410 if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
20203fb9
NC
15411 as_warn (_("%s ISA does not support SmartMIPS"),
15412 mips_cpu_info_from_isa (mips_opts.isa)->name);
ad3fea08 15413
74cd071d 15414 if (mips_opts.ase_dsp == -1)
ad3fea08
TS
15415 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15416 if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
20203fb9
NC
15417 as_warn (_("%s ISA does not support DSP ASE"),
15418 mips_cpu_info_from_isa (mips_opts.isa)->name);
ad3fea08 15419
8b082fb1
TS
15420 if (mips_opts.ase_dspr2 == -1)
15421 {
15422 mips_opts.ase_dspr2 = (arch_info->flags & MIPS_CPU_ASE_DSPR2) ? 1 : 0;
15423 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15424 }
15425 if (mips_opts.ase_dspr2 && !ISA_SUPPORTS_DSPR2_ASE)
20203fb9
NC
15426 as_warn (_("%s ISA does not support DSP R2 ASE"),
15427 mips_cpu_info_from_isa (mips_opts.isa)->name);
8b082fb1 15428
ef2e4d86 15429 if (mips_opts.ase_mt == -1)
ad3fea08
TS
15430 mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
15431 if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
20203fb9
NC
15432 as_warn (_("%s ISA does not support MT ASE"),
15433 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 15434
dec0624d
MR
15435 if (mips_opts.ase_mcu == -1)
15436 mips_opts.ase_mcu = (arch_info->flags & MIPS_CPU_ASE_MCU) ? 1 : 0;
15437 if (mips_opts.ase_mcu && !ISA_SUPPORTS_MCU_ASE)
15438 as_warn (_("%s ISA does not support MCU ASE"),
15439 mips_cpu_info_from_isa (mips_opts.isa)->name);
15440
b015e599
AP
15441 if (mips_opts.ase_virt == -1)
15442 mips_opts.ase_virt = (arch_info->flags & MIPS_CPU_ASE_VIRT) ? 1 : 0;
15443 if (mips_opts.ase_virt && !ISA_SUPPORTS_VIRT_ASE)
15444 as_warn (_("%s ISA does not support Virtualization ASE"),
15445 mips_cpu_info_from_isa (mips_opts.isa)->name);
15446
e9670677 15447 file_mips_isa = mips_opts.isa;
e9670677
MR
15448 file_ase_mips3d = mips_opts.ase_mips3d;
15449 file_ase_mdmx = mips_opts.ase_mdmx;
e16bfa71 15450 file_ase_smartmips = mips_opts.ase_smartmips;
74cd071d 15451 file_ase_dsp = mips_opts.ase_dsp;
8b082fb1 15452 file_ase_dspr2 = mips_opts.ase_dspr2;
ef2e4d86 15453 file_ase_mt = mips_opts.ase_mt;
b015e599 15454 file_ase_virt = mips_opts.ase_virt;
e9670677
MR
15455 mips_opts.gp32 = file_mips_gp32;
15456 mips_opts.fp32 = file_mips_fp32;
037b32b9
AN
15457 mips_opts.soft_float = file_mips_soft_float;
15458 mips_opts.single_float = file_mips_single_float;
e9670677 15459
ecb4347a
DJ
15460 if (mips_flag_mdebug < 0)
15461 {
15462#ifdef OBJ_MAYBE_ECOFF
15463 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
15464 mips_flag_mdebug = 1;
15465 else
15466#endif /* OBJ_MAYBE_ECOFF */
15467 mips_flag_mdebug = 0;
15468 }
e9670677
MR
15469}
15470\f
15471void
17a2f251 15472mips_init_after_args (void)
252b5132
RH
15473{
15474 /* initialize opcodes */
15475 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15476 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15477}
15478
15479long
17a2f251 15480md_pcrel_from (fixS *fixP)
252b5132 15481{
a7ebbfdf
TS
15482 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15483 switch (fixP->fx_r_type)
15484 {
df58fc94
RS
15485 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15486 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15487 /* Return the address of the delay slot. */
15488 return addr + 2;
15489
15490 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15491 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf
TS
15492 case BFD_RELOC_16_PCREL_S2:
15493 case BFD_RELOC_MIPS_JMP:
15494 /* Return the address of the delay slot. */
15495 return addr + 4;
df58fc94 15496
b47468a6
CM
15497 case BFD_RELOC_32_PCREL:
15498 return addr;
15499
a7ebbfdf 15500 default:
58ea3d6a 15501 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
15502 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15503 as_bad_where (fixP->fx_file, fixP->fx_line,
15504 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
15505 return addr;
15506 }
252b5132
RH
15507}
15508
252b5132
RH
15509/* This is called before the symbol table is processed. In order to
15510 work with gcc when using mips-tfile, we must keep all local labels.
15511 However, in other cases, we want to discard them. If we were
15512 called with -g, but we didn't see any debugging information, it may
15513 mean that gcc is smuggling debugging information through to
15514 mips-tfile, in which case we must generate all local labels. */
15515
15516void
17a2f251 15517mips_frob_file_before_adjust (void)
252b5132
RH
15518{
15519#ifndef NO_ECOFF_DEBUGGING
15520 if (ECOFF_DEBUGGING
15521 && mips_debug != 0
15522 && ! ecoff_debugging_seen)
15523 flag_keep_locals = 1;
15524#endif
15525}
15526
3b91255e 15527/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15528 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15529 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15530 relocation operators.
15531
15532 For our purposes, a %lo() expression matches a %got() or %hi()
15533 expression if:
15534
15535 (a) it refers to the same symbol; and
15536 (b) the offset applied in the %lo() expression is no lower than
15537 the offset applied in the %got() or %hi().
15538
15539 (b) allows us to cope with code like:
15540
15541 lui $4,%hi(foo)
15542 lh $4,%lo(foo+2)($4)
15543
15544 ...which is legal on RELA targets, and has a well-defined behaviour
15545 if the user knows that adding 2 to "foo" will not induce a carry to
15546 the high 16 bits.
15547
15548 When several %lo()s match a particular %got() or %hi(), we use the
15549 following rules to distinguish them:
15550
15551 (1) %lo()s with smaller offsets are a better match than %lo()s with
15552 higher offsets.
15553
15554 (2) %lo()s with no matching %got() or %hi() are better than those
15555 that already have a matching %got() or %hi().
15556
15557 (3) later %lo()s are better than earlier %lo()s.
15558
15559 These rules are applied in order.
15560
15561 (1) means, among other things, that %lo()s with identical offsets are
15562 chosen if they exist.
15563
15564 (2) means that we won't associate several high-part relocations with
15565 the same low-part relocation unless there's no alternative. Having
15566 several high parts for the same low part is a GNU extension; this rule
15567 allows careful users to avoid it.
15568
15569 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15570 with the last high-part relocation being at the front of the list.
15571 It therefore makes sense to choose the last matching low-part
15572 relocation, all other things being equal. It's also easier
15573 to code that way. */
252b5132
RH
15574
15575void
17a2f251 15576mips_frob_file (void)
252b5132
RH
15577{
15578 struct mips_hi_fixup *l;
35903be0 15579 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15580
15581 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15582 {
15583 segment_info_type *seginfo;
3b91255e
RS
15584 bfd_boolean matched_lo_p;
15585 fixS **hi_pos, **lo_pos, **pos;
252b5132 15586
9c2799c2 15587 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15588
5919d012 15589 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15590 there isn't supposed to be a matching LO. Ignore %gots against
15591 constants; we'll report an error for those later. */
738e5348 15592 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab
RS
15593 && !(l->fixp->fx_addsy
15594 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
5919d012
RS
15595 continue;
15596
15597 /* Check quickly whether the next fixup happens to be a matching %lo. */
15598 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15599 continue;
15600
252b5132 15601 seginfo = seg_info (l->seg);
252b5132 15602
3b91255e
RS
15603 /* Set HI_POS to the position of this relocation in the chain.
15604 Set LO_POS to the position of the chosen low-part relocation.
15605 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15606 relocation that matches an immediately-preceding high-part
15607 relocation. */
15608 hi_pos = NULL;
15609 lo_pos = NULL;
15610 matched_lo_p = FALSE;
738e5348 15611 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15612
3b91255e
RS
15613 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15614 {
15615 if (*pos == l->fixp)
15616 hi_pos = pos;
15617
35903be0 15618 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15619 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15620 && (*pos)->fx_offset >= l->fixp->fx_offset
15621 && (lo_pos == NULL
15622 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15623 || (!matched_lo_p
15624 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15625 lo_pos = pos;
15626
15627 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15628 && fixup_has_matching_lo_p (*pos));
15629 }
15630
15631 /* If we found a match, remove the high-part relocation from its
15632 current position and insert it before the low-part relocation.
15633 Make the offsets match so that fixup_has_matching_lo_p()
15634 will return true.
15635
15636 We don't warn about unmatched high-part relocations since some
15637 versions of gcc have been known to emit dead "lui ...%hi(...)"
15638 instructions. */
15639 if (lo_pos != NULL)
15640 {
15641 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15642 if (l->fixp->fx_next != *lo_pos)
252b5132 15643 {
3b91255e
RS
15644 *hi_pos = l->fixp->fx_next;
15645 l->fixp->fx_next = *lo_pos;
15646 *lo_pos = l->fixp;
252b5132 15647 }
252b5132
RH
15648 }
15649 }
15650}
15651
252b5132 15652int
17a2f251 15653mips_force_relocation (fixS *fixp)
252b5132 15654{
ae6063d4 15655 if (generic_force_reloc (fixp))
252b5132
RH
15656 return 1;
15657
df58fc94
RS
15658 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15659 so that the linker relaxation can update targets. */
15660 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15661 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15662 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15663 return 1;
15664
3e722fb5 15665 return 0;
252b5132
RH
15666}
15667
b886a2ab
RS
15668/* Read the instruction associated with RELOC from BUF. */
15669
15670static unsigned int
15671read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15672{
15673 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15674 return read_compressed_insn (buf, 4);
15675 else
15676 return read_insn (buf);
15677}
15678
15679/* Write instruction INSN to BUF, given that it has been relocated
15680 by RELOC. */
15681
15682static void
15683write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15684 unsigned long insn)
15685{
15686 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15687 write_compressed_insn (buf, insn, 4);
15688 else
15689 write_insn (buf, insn);
15690}
15691
252b5132
RH
15692/* Apply a fixup to the object file. */
15693
94f592af 15694void
55cf6793 15695md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15696{
4d68580a 15697 char *buf;
b886a2ab 15698 unsigned long insn;
a7ebbfdf 15699 reloc_howto_type *howto;
252b5132 15700
a7ebbfdf
TS
15701 /* We ignore generic BFD relocations we don't know about. */
15702 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15703 if (! howto)
15704 return;
65551fa4 15705
df58fc94
RS
15706 gas_assert (fixP->fx_size == 2
15707 || fixP->fx_size == 4
90ecf173
MR
15708 || fixP->fx_r_type == BFD_RELOC_16
15709 || fixP->fx_r_type == BFD_RELOC_64
15710 || fixP->fx_r_type == BFD_RELOC_CTOR
15711 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15712 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15713 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15714 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15715 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 15716
4d68580a 15717 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15718
df58fc94
RS
15719 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15720 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15721 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
15722 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
15723 || fixP->fx_r_type == BFD_RELOC_32_PCREL);
b1dca8ee
RS
15724
15725 /* Don't treat parts of a composite relocation as done. There are two
15726 reasons for this:
15727
15728 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15729 should nevertheless be emitted if the first part is.
15730
15731 (2) In normal usage, composite relocations are never assembly-time
15732 constants. The easiest way of dealing with the pathological
15733 exceptions is to generate a relocation against STN_UNDEF and
15734 leave everything up to the linker. */
3994f87e 15735 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15736 fixP->fx_done = 1;
15737
15738 switch (fixP->fx_r_type)
15739 {
3f98094e
DJ
15740 case BFD_RELOC_MIPS_TLS_GD:
15741 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15742 case BFD_RELOC_MIPS_TLS_DTPREL32:
15743 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15744 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15745 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15746 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15747 case BFD_RELOC_MIPS_TLS_TPREL32:
15748 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15749 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15750 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15751 case BFD_RELOC_MICROMIPS_TLS_GD:
15752 case BFD_RELOC_MICROMIPS_TLS_LDM:
15753 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15754 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15755 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15756 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15757 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15758 case BFD_RELOC_MIPS16_TLS_GD:
15759 case BFD_RELOC_MIPS16_TLS_LDM:
15760 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15761 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15762 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15763 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15764 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
b886a2ab
RS
15765 if (!fixP->fx_addsy)
15766 {
15767 as_bad_where (fixP->fx_file, fixP->fx_line,
15768 _("TLS relocation against a constant"));
15769 break;
15770 }
3f98094e
DJ
15771 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15772 /* fall through */
15773
252b5132 15774 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
15775 case BFD_RELOC_MIPS_SHIFT5:
15776 case BFD_RELOC_MIPS_SHIFT6:
15777 case BFD_RELOC_MIPS_GOT_DISP:
15778 case BFD_RELOC_MIPS_GOT_PAGE:
15779 case BFD_RELOC_MIPS_GOT_OFST:
15780 case BFD_RELOC_MIPS_SUB:
15781 case BFD_RELOC_MIPS_INSERT_A:
15782 case BFD_RELOC_MIPS_INSERT_B:
15783 case BFD_RELOC_MIPS_DELETE:
15784 case BFD_RELOC_MIPS_HIGHEST:
15785 case BFD_RELOC_MIPS_HIGHER:
15786 case BFD_RELOC_MIPS_SCN_DISP:
15787 case BFD_RELOC_MIPS_REL16:
15788 case BFD_RELOC_MIPS_RELGOT:
15789 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15790 case BFD_RELOC_HI16:
15791 case BFD_RELOC_HI16_S:
b886a2ab 15792 case BFD_RELOC_LO16:
cdf6fd85 15793 case BFD_RELOC_GPREL16:
252b5132
RH
15794 case BFD_RELOC_MIPS_LITERAL:
15795 case BFD_RELOC_MIPS_CALL16:
15796 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15797 case BFD_RELOC_GPREL32:
252b5132
RH
15798 case BFD_RELOC_MIPS_GOT_HI16:
15799 case BFD_RELOC_MIPS_GOT_LO16:
15800 case BFD_RELOC_MIPS_CALL_HI16:
15801 case BFD_RELOC_MIPS_CALL_LO16:
15802 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15803 case BFD_RELOC_MIPS16_GOT16:
15804 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15805 case BFD_RELOC_MIPS16_HI16:
15806 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15807 case BFD_RELOC_MIPS16_LO16:
252b5132 15808 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
15809 case BFD_RELOC_MICROMIPS_JMP:
15810 case BFD_RELOC_MICROMIPS_GOT_DISP:
15811 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15812 case BFD_RELOC_MICROMIPS_GOT_OFST:
15813 case BFD_RELOC_MICROMIPS_SUB:
15814 case BFD_RELOC_MICROMIPS_HIGHEST:
15815 case BFD_RELOC_MICROMIPS_HIGHER:
15816 case BFD_RELOC_MICROMIPS_SCN_DISP:
15817 case BFD_RELOC_MICROMIPS_JALR:
15818 case BFD_RELOC_MICROMIPS_HI16:
15819 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15820 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15821 case BFD_RELOC_MICROMIPS_GPREL16:
15822 case BFD_RELOC_MICROMIPS_LITERAL:
15823 case BFD_RELOC_MICROMIPS_CALL16:
15824 case BFD_RELOC_MICROMIPS_GOT16:
15825 case BFD_RELOC_MICROMIPS_GOT_HI16:
15826 case BFD_RELOC_MICROMIPS_GOT_LO16:
15827 case BFD_RELOC_MICROMIPS_CALL_HI16:
15828 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15829 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15830 if (fixP->fx_done)
15831 {
15832 offsetT value;
15833
15834 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15835 {
15836 insn = read_reloc_insn (buf, fixP->fx_r_type);
15837 if (mips16_reloc_p (fixP->fx_r_type))
15838 insn |= mips16_immed_extend (value, 16);
15839 else
15840 insn |= (value & 0xffff);
15841 write_reloc_insn (buf, fixP->fx_r_type, insn);
15842 }
15843 else
15844 as_bad_where (fixP->fx_file, fixP->fx_line,
15845 _("Unsupported constant in relocation"));
15846 }
252b5132
RH
15847 break;
15848
252b5132
RH
15849 case BFD_RELOC_64:
15850 /* This is handled like BFD_RELOC_32, but we output a sign
15851 extended value if we are only 32 bits. */
3e722fb5 15852 if (fixP->fx_done)
252b5132
RH
15853 {
15854 if (8 <= sizeof (valueT))
4d68580a 15855 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15856 else
15857 {
a7ebbfdf 15858 valueT hiv;
252b5132 15859
a7ebbfdf 15860 if ((*valP & 0x80000000) != 0)
252b5132
RH
15861 hiv = 0xffffffff;
15862 else
15863 hiv = 0;
4d68580a
RS
15864 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15865 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15866 }
15867 }
15868 break;
15869
056350c6 15870 case BFD_RELOC_RVA:
252b5132 15871 case BFD_RELOC_32:
b47468a6 15872 case BFD_RELOC_32_PCREL:
252b5132
RH
15873 case BFD_RELOC_16:
15874 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15875 value now. This can happen if we have a .word which is not
15876 resolved when it appears but is later defined. */
252b5132 15877 if (fixP->fx_done)
4d68580a 15878 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15879 break;
15880
252b5132 15881 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 15882 if ((*valP & 0x3) != 0)
cb56d3d3 15883 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 15884 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 15885
54f4ddb3
TS
15886 /* We need to save the bits in the instruction since fixup_segment()
15887 might be deleting the relocation entry (i.e., a branch within
15888 the current segment). */
a7ebbfdf 15889 if (! fixP->fx_done)
bb2d6cd7 15890 break;
252b5132 15891
54f4ddb3 15892 /* Update old instruction data. */
4d68580a 15893 insn = read_insn (buf);
252b5132 15894
a7ebbfdf
TS
15895 if (*valP + 0x20000 <= 0x3ffff)
15896 {
15897 insn |= (*valP >> 2) & 0xffff;
4d68580a 15898 write_insn (buf, insn);
a7ebbfdf
TS
15899 }
15900 else if (mips_pic == NO_PIC
15901 && fixP->fx_done
15902 && fixP->fx_frag->fr_address >= text_section->vma
15903 && (fixP->fx_frag->fr_address
587aac4e 15904 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15905 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15906 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15907 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15908 {
15909 /* The branch offset is too large. If this is an
15910 unconditional branch, and we are not generating PIC code,
15911 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15912 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15913 insn = 0x0c000000; /* jal */
252b5132 15914 else
a7ebbfdf
TS
15915 insn = 0x08000000; /* j */
15916 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15917 fixP->fx_done = 0;
15918 fixP->fx_addsy = section_symbol (text_section);
15919 *valP += md_pcrel_from (fixP);
4d68580a 15920 write_insn (buf, insn);
a7ebbfdf
TS
15921 }
15922 else
15923 {
15924 /* If we got here, we have branch-relaxation disabled,
15925 and there's nothing we can do to fix this instruction
15926 without turning it into a longer sequence. */
15927 as_bad_where (fixP->fx_file, fixP->fx_line,
15928 _("Branch out of range"));
252b5132 15929 }
252b5132
RH
15930 break;
15931
df58fc94
RS
15932 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15933 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15934 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15935 /* We adjust the offset back to even. */
15936 if ((*valP & 0x1) != 0)
15937 --(*valP);
15938
15939 if (! fixP->fx_done)
15940 break;
15941
15942 /* Should never visit here, because we keep the relocation. */
15943 abort ();
15944 break;
15945
252b5132
RH
15946 case BFD_RELOC_VTABLE_INHERIT:
15947 fixP->fx_done = 0;
15948 if (fixP->fx_addsy
15949 && !S_IS_DEFINED (fixP->fx_addsy)
15950 && !S_IS_WEAK (fixP->fx_addsy))
15951 S_SET_WEAK (fixP->fx_addsy);
15952 break;
15953
15954 case BFD_RELOC_VTABLE_ENTRY:
15955 fixP->fx_done = 0;
15956 break;
15957
15958 default:
b37df7c4 15959 abort ();
252b5132 15960 }
a7ebbfdf
TS
15961
15962 /* Remember value for tc_gen_reloc. */
15963 fixP->fx_addnumber = *valP;
252b5132
RH
15964}
15965
252b5132 15966static symbolS *
17a2f251 15967get_symbol (void)
252b5132
RH
15968{
15969 int c;
15970 char *name;
15971 symbolS *p;
15972
15973 name = input_line_pointer;
15974 c = get_symbol_end ();
15975 p = (symbolS *) symbol_find_or_make (name);
15976 *input_line_pointer = c;
15977 return p;
15978}
15979
742a56fe
RS
15980/* Align the current frag to a given power of two. If a particular
15981 fill byte should be used, FILL points to an integer that contains
15982 that byte, otherwise FILL is null.
15983
462427c4
RS
15984 This function used to have the comment:
15985
15986 The MIPS assembler also automatically adjusts any preceding label.
15987
15988 The implementation therefore applied the adjustment to a maximum of
15989 one label. However, other label adjustments are applied to batches
15990 of labels, and adjusting just one caused problems when new labels
15991 were added for the sake of debugging or unwind information.
15992 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
15993
15994static void
462427c4 15995mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 15996{
7d10b47d 15997 mips_emit_delays ();
df58fc94 15998 mips_record_compressed_mode ();
742a56fe
RS
15999 if (fill == NULL && subseg_text_p (now_seg))
16000 frag_align_code (to, 0);
16001 else
16002 frag_align (to, fill ? *fill : 0, 0);
252b5132 16003 record_alignment (now_seg, to);
462427c4 16004 mips_move_labels (labels, FALSE);
252b5132
RH
16005}
16006
16007/* Align to a given power of two. .align 0 turns off the automatic
16008 alignment used by the data creating pseudo-ops. */
16009
16010static void
17a2f251 16011s_align (int x ATTRIBUTE_UNUSED)
252b5132 16012{
742a56fe 16013 int temp, fill_value, *fill_ptr;
49954fb4 16014 long max_alignment = 28;
252b5132 16015
54f4ddb3 16016 /* o Note that the assembler pulls down any immediately preceding label
252b5132 16017 to the aligned address.
54f4ddb3 16018 o It's not documented but auto alignment is reinstated by
252b5132 16019 a .align pseudo instruction.
54f4ddb3 16020 o Note also that after auto alignment is turned off the mips assembler
252b5132 16021 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 16022 We don't. */
252b5132
RH
16023
16024 temp = get_absolute_expression ();
16025 if (temp > max_alignment)
16026 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
16027 else if (temp < 0)
16028 {
16029 as_warn (_("Alignment negative: 0 assumed."));
16030 temp = 0;
16031 }
16032 if (*input_line_pointer == ',')
16033 {
f9419b05 16034 ++input_line_pointer;
742a56fe
RS
16035 fill_value = get_absolute_expression ();
16036 fill_ptr = &fill_value;
252b5132
RH
16037 }
16038 else
742a56fe 16039 fill_ptr = 0;
252b5132
RH
16040 if (temp)
16041 {
a8dbcb85
TS
16042 segment_info_type *si = seg_info (now_seg);
16043 struct insn_label_list *l = si->label_list;
54f4ddb3 16044 /* Auto alignment should be switched on by next section change. */
252b5132 16045 auto_align = 1;
462427c4 16046 mips_align (temp, fill_ptr, l);
252b5132
RH
16047 }
16048 else
16049 {
16050 auto_align = 0;
16051 }
16052
16053 demand_empty_rest_of_line ();
16054}
16055
252b5132 16056static void
17a2f251 16057s_change_sec (int sec)
252b5132
RH
16058{
16059 segT seg;
16060
252b5132
RH
16061#ifdef OBJ_ELF
16062 /* The ELF backend needs to know that we are changing sections, so
16063 that .previous works correctly. We could do something like check
b6ff326e 16064 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
16065 as it would not be appropriate to use it in the section changing
16066 functions in read.c, since obj-elf.c intercepts those. FIXME:
16067 This should be cleaner, somehow. */
f43abd2b
TS
16068 if (IS_ELF)
16069 obj_elf_section_change_hook ();
252b5132
RH
16070#endif
16071
7d10b47d 16072 mips_emit_delays ();
6a32d874 16073
252b5132
RH
16074 switch (sec)
16075 {
16076 case 't':
16077 s_text (0);
16078 break;
16079 case 'd':
16080 s_data (0);
16081 break;
16082 case 'b':
16083 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16084 demand_empty_rest_of_line ();
16085 break;
16086
16087 case 'r':
4d0d148d
TS
16088 seg = subseg_new (RDATA_SECTION_NAME,
16089 (subsegT) get_absolute_expression ());
f43abd2b 16090 if (IS_ELF)
252b5132 16091 {
4d0d148d
TS
16092 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16093 | SEC_READONLY | SEC_RELOC
16094 | SEC_DATA));
c41e87e3 16095 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 16096 record_alignment (seg, 4);
252b5132 16097 }
4d0d148d 16098 demand_empty_rest_of_line ();
252b5132
RH
16099 break;
16100
16101 case 's':
4d0d148d 16102 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f43abd2b 16103 if (IS_ELF)
252b5132 16104 {
4d0d148d
TS
16105 bfd_set_section_flags (stdoutput, seg,
16106 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
c41e87e3 16107 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 16108 record_alignment (seg, 4);
252b5132 16109 }
4d0d148d
TS
16110 demand_empty_rest_of_line ();
16111 break;
998b3c36
MR
16112
16113 case 'B':
16114 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16115 if (IS_ELF)
16116 {
16117 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16118 if (strncmp (TARGET_OS, "elf", 3) != 0)
16119 record_alignment (seg, 4);
16120 }
16121 demand_empty_rest_of_line ();
16122 break;
252b5132
RH
16123 }
16124
16125 auto_align = 1;
16126}
b34976b6 16127
cca86cc8 16128void
17a2f251 16129s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16130{
7ed4a06a 16131#ifdef OBJ_ELF
cca86cc8
SC
16132 char *section_name;
16133 char c;
684022ea 16134 char next_c = 0;
cca86cc8
SC
16135 int section_type;
16136 int section_flag;
16137 int section_entry_size;
16138 int section_alignment;
b34976b6 16139
f43abd2b 16140 if (!IS_ELF)
7ed4a06a
TS
16141 return;
16142
cca86cc8
SC
16143 section_name = input_line_pointer;
16144 c = get_symbol_end ();
a816d1ed
AO
16145 if (c)
16146 next_c = *(input_line_pointer + 1);
cca86cc8 16147
4cf0dd0d
TS
16148 /* Do we have .section Name<,"flags">? */
16149 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16150 {
4cf0dd0d
TS
16151 /* just after name is now '\0'. */
16152 *input_line_pointer = c;
cca86cc8
SC
16153 input_line_pointer = section_name;
16154 obj_elf_section (ignore);
16155 return;
16156 }
16157 input_line_pointer++;
16158
16159 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16160 if (c == ',')
16161 section_type = get_absolute_expression ();
16162 else
16163 section_type = 0;
16164 if (*input_line_pointer++ == ',')
16165 section_flag = get_absolute_expression ();
16166 else
16167 section_flag = 0;
16168 if (*input_line_pointer++ == ',')
16169 section_entry_size = get_absolute_expression ();
16170 else
16171 section_entry_size = 0;
16172 if (*input_line_pointer++ == ',')
16173 section_alignment = get_absolute_expression ();
16174 else
16175 section_alignment = 0;
87975d2a
AM
16176 /* FIXME: really ignore? */
16177 (void) section_alignment;
cca86cc8 16178
a816d1ed
AO
16179 section_name = xstrdup (section_name);
16180
8ab8a5c8
RS
16181 /* When using the generic form of .section (as implemented by obj-elf.c),
16182 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16183 traditionally had to fall back on the more common @progbits instead.
16184
16185 There's nothing really harmful in this, since bfd will correct
16186 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16187 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16188 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16189
16190 Even so, we shouldn't force users of the MIPS .section syntax to
16191 incorrectly label the sections as SHT_PROGBITS. The best compromise
16192 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16193 generic type-checking code. */
16194 if (section_type == SHT_MIPS_DWARF)
16195 section_type = SHT_PROGBITS;
16196
cca86cc8
SC
16197 obj_elf_change_section (section_name, section_type, section_flag,
16198 section_entry_size, 0, 0, 0);
a816d1ed
AO
16199
16200 if (now_seg->name != section_name)
16201 free (section_name);
7ed4a06a 16202#endif /* OBJ_ELF */
cca86cc8 16203}
252b5132
RH
16204
16205void
17a2f251 16206mips_enable_auto_align (void)
252b5132
RH
16207{
16208 auto_align = 1;
16209}
16210
16211static void
17a2f251 16212s_cons (int log_size)
252b5132 16213{
a8dbcb85
TS
16214 segment_info_type *si = seg_info (now_seg);
16215 struct insn_label_list *l = si->label_list;
252b5132 16216
7d10b47d 16217 mips_emit_delays ();
252b5132 16218 if (log_size > 0 && auto_align)
462427c4 16219 mips_align (log_size, 0, l);
252b5132 16220 cons (1 << log_size);
a1facbec 16221 mips_clear_insn_labels ();
252b5132
RH
16222}
16223
16224static void
17a2f251 16225s_float_cons (int type)
252b5132 16226{
a8dbcb85
TS
16227 segment_info_type *si = seg_info (now_seg);
16228 struct insn_label_list *l = si->label_list;
252b5132 16229
7d10b47d 16230 mips_emit_delays ();
252b5132
RH
16231
16232 if (auto_align)
49309057
ILT
16233 {
16234 if (type == 'd')
462427c4 16235 mips_align (3, 0, l);
49309057 16236 else
462427c4 16237 mips_align (2, 0, l);
49309057 16238 }
252b5132 16239
252b5132 16240 float_cons (type);
a1facbec 16241 mips_clear_insn_labels ();
252b5132
RH
16242}
16243
16244/* Handle .globl. We need to override it because on Irix 5 you are
16245 permitted to say
16246 .globl foo .text
16247 where foo is an undefined symbol, to mean that foo should be
16248 considered to be the address of a function. */
16249
16250static void
17a2f251 16251s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16252{
16253 char *name;
16254 int c;
16255 symbolS *symbolP;
16256 flagword flag;
16257
8a06b769 16258 do
252b5132 16259 {
8a06b769 16260 name = input_line_pointer;
252b5132 16261 c = get_symbol_end ();
8a06b769
TS
16262 symbolP = symbol_find_or_make (name);
16263 S_SET_EXTERNAL (symbolP);
16264
252b5132 16265 *input_line_pointer = c;
8a06b769 16266 SKIP_WHITESPACE ();
252b5132 16267
8a06b769
TS
16268 /* On Irix 5, every global symbol that is not explicitly labelled as
16269 being a function is apparently labelled as being an object. */
16270 flag = BSF_OBJECT;
252b5132 16271
8a06b769
TS
16272 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16273 && (*input_line_pointer != ','))
16274 {
16275 char *secname;
16276 asection *sec;
16277
16278 secname = input_line_pointer;
16279 c = get_symbol_end ();
16280 sec = bfd_get_section_by_name (stdoutput, secname);
16281 if (sec == NULL)
16282 as_bad (_("%s: no such section"), secname);
16283 *input_line_pointer = c;
16284
16285 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16286 flag = BSF_FUNCTION;
16287 }
16288
16289 symbol_get_bfdsym (symbolP)->flags |= flag;
16290
16291 c = *input_line_pointer;
16292 if (c == ',')
16293 {
16294 input_line_pointer++;
16295 SKIP_WHITESPACE ();
16296 if (is_end_of_line[(unsigned char) *input_line_pointer])
16297 c = '\n';
16298 }
16299 }
16300 while (c == ',');
252b5132 16301
252b5132
RH
16302 demand_empty_rest_of_line ();
16303}
16304
16305static void
17a2f251 16306s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16307{
16308 char *opt;
16309 char c;
16310
16311 opt = input_line_pointer;
16312 c = get_symbol_end ();
16313
16314 if (*opt == 'O')
16315 {
16316 /* FIXME: What does this mean? */
16317 }
16318 else if (strncmp (opt, "pic", 3) == 0)
16319 {
16320 int i;
16321
16322 i = atoi (opt + 3);
16323 if (i == 0)
16324 mips_pic = NO_PIC;
16325 else if (i == 2)
143d77c5 16326 {
8b828383 16327 mips_pic = SVR4_PIC;
143d77c5
EC
16328 mips_abicalls = TRUE;
16329 }
252b5132
RH
16330 else
16331 as_bad (_(".option pic%d not supported"), i);
16332
4d0d148d 16333 if (mips_pic == SVR4_PIC)
252b5132
RH
16334 {
16335 if (g_switch_seen && g_switch_value != 0)
16336 as_warn (_("-G may not be used with SVR4 PIC code"));
16337 g_switch_value = 0;
16338 bfd_set_gp_size (stdoutput, 0);
16339 }
16340 }
16341 else
16342 as_warn (_("Unrecognized option \"%s\""), opt);
16343
16344 *input_line_pointer = c;
16345 demand_empty_rest_of_line ();
16346}
16347
16348/* This structure is used to hold a stack of .set values. */
16349
e972090a
NC
16350struct mips_option_stack
16351{
252b5132
RH
16352 struct mips_option_stack *next;
16353 struct mips_set_options options;
16354};
16355
16356static struct mips_option_stack *mips_opts_stack;
16357
16358/* Handle the .set pseudo-op. */
16359
16360static void
17a2f251 16361s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
16362{
16363 char *name = input_line_pointer, ch;
16364
16365 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 16366 ++input_line_pointer;
252b5132
RH
16367 ch = *input_line_pointer;
16368 *input_line_pointer = '\0';
16369
16370 if (strcmp (name, "reorder") == 0)
16371 {
7d10b47d
RS
16372 if (mips_opts.noreorder)
16373 end_noreorder ();
252b5132
RH
16374 }
16375 else if (strcmp (name, "noreorder") == 0)
16376 {
7d10b47d
RS
16377 if (!mips_opts.noreorder)
16378 start_noreorder ();
252b5132 16379 }
741fe287
MR
16380 else if (strncmp (name, "at=", 3) == 0)
16381 {
16382 char *s = name + 3;
16383
16384 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16385 as_bad (_("Unrecognized register name `%s'"), s);
16386 }
252b5132
RH
16387 else if (strcmp (name, "at") == 0)
16388 {
741fe287 16389 mips_opts.at = ATREG;
252b5132
RH
16390 }
16391 else if (strcmp (name, "noat") == 0)
16392 {
741fe287 16393 mips_opts.at = ZERO;
252b5132
RH
16394 }
16395 else if (strcmp (name, "macro") == 0)
16396 {
16397 mips_opts.warn_about_macros = 0;
16398 }
16399 else if (strcmp (name, "nomacro") == 0)
16400 {
16401 if (mips_opts.noreorder == 0)
16402 as_bad (_("`noreorder' must be set before `nomacro'"));
16403 mips_opts.warn_about_macros = 1;
16404 }
16405 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16406 {
16407 mips_opts.nomove = 0;
16408 }
16409 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16410 {
16411 mips_opts.nomove = 1;
16412 }
16413 else if (strcmp (name, "bopt") == 0)
16414 {
16415 mips_opts.nobopt = 0;
16416 }
16417 else if (strcmp (name, "nobopt") == 0)
16418 {
16419 mips_opts.nobopt = 1;
16420 }
ad3fea08
TS
16421 else if (strcmp (name, "gp=default") == 0)
16422 mips_opts.gp32 = file_mips_gp32;
16423 else if (strcmp (name, "gp=32") == 0)
16424 mips_opts.gp32 = 1;
16425 else if (strcmp (name, "gp=64") == 0)
16426 {
16427 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 16428 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08
TS
16429 mips_cpu_info_from_isa (mips_opts.isa)->name);
16430 mips_opts.gp32 = 0;
16431 }
16432 else if (strcmp (name, "fp=default") == 0)
16433 mips_opts.fp32 = file_mips_fp32;
16434 else if (strcmp (name, "fp=32") == 0)
16435 mips_opts.fp32 = 1;
16436 else if (strcmp (name, "fp=64") == 0)
16437 {
16438 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 16439 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08
TS
16440 mips_cpu_info_from_isa (mips_opts.isa)->name);
16441 mips_opts.fp32 = 0;
16442 }
037b32b9
AN
16443 else if (strcmp (name, "softfloat") == 0)
16444 mips_opts.soft_float = 1;
16445 else if (strcmp (name, "hardfloat") == 0)
16446 mips_opts.soft_float = 0;
16447 else if (strcmp (name, "singlefloat") == 0)
16448 mips_opts.single_float = 1;
16449 else if (strcmp (name, "doublefloat") == 0)
16450 mips_opts.single_float = 0;
252b5132
RH
16451 else if (strcmp (name, "mips16") == 0
16452 || strcmp (name, "MIPS-16") == 0)
df58fc94
RS
16453 {
16454 if (mips_opts.micromips == 1)
16455 as_fatal (_("`mips16' cannot be used with `micromips'"));
16456 mips_opts.mips16 = 1;
16457 }
252b5132
RH
16458 else if (strcmp (name, "nomips16") == 0
16459 || strcmp (name, "noMIPS-16") == 0)
16460 mips_opts.mips16 = 0;
df58fc94
RS
16461 else if (strcmp (name, "micromips") == 0)
16462 {
16463 if (mips_opts.mips16 == 1)
16464 as_fatal (_("`micromips' cannot be used with `mips16'"));
16465 mips_opts.micromips = 1;
16466 }
16467 else if (strcmp (name, "nomicromips") == 0)
16468 mips_opts.micromips = 0;
e16bfa71
TS
16469 else if (strcmp (name, "smartmips") == 0)
16470 {
ad3fea08 16471 if (!ISA_SUPPORTS_SMARTMIPS)
20203fb9 16472 as_warn (_("%s ISA does not support SmartMIPS ASE"),
e16bfa71
TS
16473 mips_cpu_info_from_isa (mips_opts.isa)->name);
16474 mips_opts.ase_smartmips = 1;
16475 }
16476 else if (strcmp (name, "nosmartmips") == 0)
16477 mips_opts.ase_smartmips = 0;
1f25f5d3
CD
16478 else if (strcmp (name, "mips3d") == 0)
16479 mips_opts.ase_mips3d = 1;
16480 else if (strcmp (name, "nomips3d") == 0)
16481 mips_opts.ase_mips3d = 0;
a4672219
TS
16482 else if (strcmp (name, "mdmx") == 0)
16483 mips_opts.ase_mdmx = 1;
16484 else if (strcmp (name, "nomdmx") == 0)
16485 mips_opts.ase_mdmx = 0;
74cd071d 16486 else if (strcmp (name, "dsp") == 0)
ad3fea08
TS
16487 {
16488 if (!ISA_SUPPORTS_DSP_ASE)
20203fb9 16489 as_warn (_("%s ISA does not support DSP ASE"),
ad3fea08
TS
16490 mips_cpu_info_from_isa (mips_opts.isa)->name);
16491 mips_opts.ase_dsp = 1;
8b082fb1 16492 mips_opts.ase_dspr2 = 0;
ad3fea08 16493 }
74cd071d 16494 else if (strcmp (name, "nodsp") == 0)
8b082fb1
TS
16495 {
16496 mips_opts.ase_dsp = 0;
16497 mips_opts.ase_dspr2 = 0;
16498 }
16499 else if (strcmp (name, "dspr2") == 0)
16500 {
16501 if (!ISA_SUPPORTS_DSPR2_ASE)
20203fb9 16502 as_warn (_("%s ISA does not support DSP R2 ASE"),
8b082fb1
TS
16503 mips_cpu_info_from_isa (mips_opts.isa)->name);
16504 mips_opts.ase_dspr2 = 1;
16505 mips_opts.ase_dsp = 1;
16506 }
16507 else if (strcmp (name, "nodspr2") == 0)
16508 {
16509 mips_opts.ase_dspr2 = 0;
16510 mips_opts.ase_dsp = 0;
16511 }
ef2e4d86 16512 else if (strcmp (name, "mt") == 0)
ad3fea08
TS
16513 {
16514 if (!ISA_SUPPORTS_MT_ASE)
20203fb9 16515 as_warn (_("%s ISA does not support MT ASE"),
ad3fea08
TS
16516 mips_cpu_info_from_isa (mips_opts.isa)->name);
16517 mips_opts.ase_mt = 1;
16518 }
ef2e4d86
CF
16519 else if (strcmp (name, "nomt") == 0)
16520 mips_opts.ase_mt = 0;
dec0624d
MR
16521 else if (strcmp (name, "mcu") == 0)
16522 mips_opts.ase_mcu = 1;
16523 else if (strcmp (name, "nomcu") == 0)
16524 mips_opts.ase_mcu = 0;
b015e599
AP
16525 else if (strcmp (name, "virt") == 0)
16526 {
16527 if (!ISA_SUPPORTS_VIRT_ASE)
16528 as_warn (_("%s ISA does not support Virtualization ASE"),
16529 mips_cpu_info_from_isa (mips_opts.isa)->name);
16530 mips_opts.ase_virt = 1;
16531 }
16532 else if (strcmp (name, "novirt") == 0)
16533 mips_opts.ase_virt = 0;
1a2c1fad 16534 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16535 {
af7ee8bf 16536 int reset = 0;
252b5132 16537
1a2c1fad
CD
16538 /* Permit the user to change the ISA and architecture on the fly.
16539 Needless to say, misuse can cause serious problems. */
81a21e38 16540 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
16541 {
16542 reset = 1;
16543 mips_opts.isa = file_mips_isa;
1a2c1fad 16544 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
16545 }
16546 else if (strncmp (name, "arch=", 5) == 0)
16547 {
16548 const struct mips_cpu_info *p;
16549
16550 p = mips_parse_cpu("internal use", name + 5);
16551 if (!p)
16552 as_bad (_("unknown architecture %s"), name + 5);
16553 else
16554 {
16555 mips_opts.arch = p->cpu;
16556 mips_opts.isa = p->isa;
16557 }
16558 }
81a21e38
TS
16559 else if (strncmp (name, "mips", 4) == 0)
16560 {
16561 const struct mips_cpu_info *p;
16562
16563 p = mips_parse_cpu("internal use", name);
16564 if (!p)
16565 as_bad (_("unknown ISA level %s"), name + 4);
16566 else
16567 {
16568 mips_opts.arch = p->cpu;
16569 mips_opts.isa = p->isa;
16570 }
16571 }
af7ee8bf 16572 else
81a21e38 16573 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
16574
16575 switch (mips_opts.isa)
98d3f06f
KH
16576 {
16577 case 0:
98d3f06f 16578 break;
af7ee8bf
CD
16579 case ISA_MIPS1:
16580 case ISA_MIPS2:
16581 case ISA_MIPS32:
16582 case ISA_MIPS32R2:
98d3f06f
KH
16583 mips_opts.gp32 = 1;
16584 mips_opts.fp32 = 1;
16585 break;
af7ee8bf
CD
16586 case ISA_MIPS3:
16587 case ISA_MIPS4:
16588 case ISA_MIPS5:
16589 case ISA_MIPS64:
5f74bc13 16590 case ISA_MIPS64R2:
98d3f06f 16591 mips_opts.gp32 = 0;
e407c74b
NC
16592 if (mips_opts.arch == CPU_R5900)
16593 {
16594 mips_opts.fp32 = 1;
16595 }
16596 else
16597 {
98d3f06f 16598 mips_opts.fp32 = 0;
e407c74b 16599 }
98d3f06f
KH
16600 break;
16601 default:
16602 as_bad (_("unknown ISA level %s"), name + 4);
16603 break;
16604 }
af7ee8bf 16605 if (reset)
98d3f06f 16606 {
af7ee8bf
CD
16607 mips_opts.gp32 = file_mips_gp32;
16608 mips_opts.fp32 = file_mips_fp32;
98d3f06f 16609 }
252b5132
RH
16610 }
16611 else if (strcmp (name, "autoextend") == 0)
16612 mips_opts.noautoextend = 0;
16613 else if (strcmp (name, "noautoextend") == 0)
16614 mips_opts.noautoextend = 1;
16615 else if (strcmp (name, "push") == 0)
16616 {
16617 struct mips_option_stack *s;
16618
16619 s = (struct mips_option_stack *) xmalloc (sizeof *s);
16620 s->next = mips_opts_stack;
16621 s->options = mips_opts;
16622 mips_opts_stack = s;
16623 }
16624 else if (strcmp (name, "pop") == 0)
16625 {
16626 struct mips_option_stack *s;
16627
16628 s = mips_opts_stack;
16629 if (s == NULL)
16630 as_bad (_(".set pop with no .set push"));
16631 else
16632 {
16633 /* If we're changing the reorder mode we need to handle
16634 delay slots correctly. */
16635 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16636 start_noreorder ();
252b5132 16637 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16638 end_noreorder ();
252b5132
RH
16639
16640 mips_opts = s->options;
16641 mips_opts_stack = s->next;
16642 free (s);
16643 }
16644 }
aed1a261
RS
16645 else if (strcmp (name, "sym32") == 0)
16646 mips_opts.sym32 = TRUE;
16647 else if (strcmp (name, "nosym32") == 0)
16648 mips_opts.sym32 = FALSE;
e6559e01
JM
16649 else if (strchr (name, ','))
16650 {
16651 /* Generic ".set" directive; use the generic handler. */
16652 *input_line_pointer = ch;
16653 input_line_pointer = name;
16654 s_set (0);
16655 return;
16656 }
252b5132
RH
16657 else
16658 {
16659 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16660 }
16661 *input_line_pointer = ch;
16662 demand_empty_rest_of_line ();
16663}
16664
16665/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16666 .option pic2. It means to generate SVR4 PIC calls. */
16667
16668static void
17a2f251 16669s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16670{
16671 mips_pic = SVR4_PIC;
143d77c5 16672 mips_abicalls = TRUE;
4d0d148d
TS
16673
16674 if (g_switch_seen && g_switch_value != 0)
16675 as_warn (_("-G may not be used with SVR4 PIC code"));
16676 g_switch_value = 0;
16677
252b5132
RH
16678 bfd_set_gp_size (stdoutput, 0);
16679 demand_empty_rest_of_line ();
16680}
16681
16682/* Handle the .cpload pseudo-op. This is used when generating SVR4
16683 PIC code. It sets the $gp register for the function based on the
16684 function address, which is in the register named in the argument.
16685 This uses a relocation against _gp_disp, which is handled specially
16686 by the linker. The result is:
16687 lui $gp,%hi(_gp_disp)
16688 addiu $gp,$gp,%lo(_gp_disp)
16689 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16690 The .cpload argument is normally $25 == $t9.
16691
16692 The -mno-shared option changes this to:
bbe506e8
TS
16693 lui $gp,%hi(__gnu_local_gp)
16694 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16695 and the argument is ignored. This saves an instruction, but the
16696 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16697 address for __gnu_local_gp. Thus code assembled with -mno-shared
16698 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16699
16700static void
17a2f251 16701s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16702{
16703 expressionS ex;
aa6975fb
ILT
16704 int reg;
16705 int in_shared;
252b5132 16706
6478892d
TS
16707 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16708 .cpload is ignored. */
16709 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16710 {
16711 s_ignore (0);
16712 return;
16713 }
16714
a276b80c
MR
16715 if (mips_opts.mips16)
16716 {
16717 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16718 ignore_rest_of_line ();
16719 return;
16720 }
16721
d3ecfc59 16722 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16723 if (mips_opts.noreorder == 0)
16724 as_warn (_(".cpload not in noreorder section"));
16725
aa6975fb
ILT
16726 reg = tc_get_register (0);
16727
16728 /* If we need to produce a 64-bit address, we are better off using
16729 the default instruction sequence. */
aed1a261 16730 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16731
252b5132 16732 ex.X_op = O_symbol;
bbe506e8
TS
16733 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16734 "__gnu_local_gp");
252b5132
RH
16735 ex.X_op_symbol = NULL;
16736 ex.X_add_number = 0;
16737
16738 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16739 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16740
8a75745d
MR
16741 mips_mark_labels ();
16742 mips_assembling_insn = TRUE;
16743
584892a6 16744 macro_start ();
67c0d1eb
RS
16745 macro_build_lui (&ex, mips_gp_register);
16746 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16747 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16748 if (in_shared)
16749 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16750 mips_gp_register, reg);
584892a6 16751 macro_end ();
252b5132 16752
8a75745d 16753 mips_assembling_insn = FALSE;
252b5132
RH
16754 demand_empty_rest_of_line ();
16755}
16756
6478892d
TS
16757/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16758 .cpsetup $reg1, offset|$reg2, label
16759
16760 If offset is given, this results in:
16761 sd $gp, offset($sp)
956cd1d6 16762 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16763 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16764 daddu $gp, $gp, $reg1
6478892d
TS
16765
16766 If $reg2 is given, this results in:
16767 daddu $reg2, $gp, $0
956cd1d6 16768 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16769 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16770 daddu $gp, $gp, $reg1
aa6975fb
ILT
16771 $reg1 is normally $25 == $t9.
16772
16773 The -mno-shared option replaces the last three instructions with
16774 lui $gp,%hi(_gp)
54f4ddb3 16775 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16776
6478892d 16777static void
17a2f251 16778s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16779{
16780 expressionS ex_off;
16781 expressionS ex_sym;
16782 int reg1;
6478892d 16783
8586fc66 16784 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16785 We also need NewABI support. */
16786 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16787 {
16788 s_ignore (0);
16789 return;
16790 }
16791
a276b80c
MR
16792 if (mips_opts.mips16)
16793 {
16794 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16795 ignore_rest_of_line ();
16796 return;
16797 }
16798
6478892d
TS
16799 reg1 = tc_get_register (0);
16800 SKIP_WHITESPACE ();
16801 if (*input_line_pointer != ',')
16802 {
16803 as_bad (_("missing argument separator ',' for .cpsetup"));
16804 return;
16805 }
16806 else
80245285 16807 ++input_line_pointer;
6478892d
TS
16808 SKIP_WHITESPACE ();
16809 if (*input_line_pointer == '$')
80245285
TS
16810 {
16811 mips_cpreturn_register = tc_get_register (0);
16812 mips_cpreturn_offset = -1;
16813 }
6478892d 16814 else
80245285
TS
16815 {
16816 mips_cpreturn_offset = get_absolute_expression ();
16817 mips_cpreturn_register = -1;
16818 }
6478892d
TS
16819 SKIP_WHITESPACE ();
16820 if (*input_line_pointer != ',')
16821 {
16822 as_bad (_("missing argument separator ',' for .cpsetup"));
16823 return;
16824 }
16825 else
f9419b05 16826 ++input_line_pointer;
6478892d 16827 SKIP_WHITESPACE ();
f21f8242 16828 expression (&ex_sym);
6478892d 16829
8a75745d
MR
16830 mips_mark_labels ();
16831 mips_assembling_insn = TRUE;
16832
584892a6 16833 macro_start ();
6478892d
TS
16834 if (mips_cpreturn_register == -1)
16835 {
16836 ex_off.X_op = O_constant;
16837 ex_off.X_add_symbol = NULL;
16838 ex_off.X_op_symbol = NULL;
16839 ex_off.X_add_number = mips_cpreturn_offset;
16840
67c0d1eb 16841 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16842 BFD_RELOC_LO16, SP);
6478892d
TS
16843 }
16844 else
67c0d1eb 16845 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 16846 mips_gp_register, 0);
6478892d 16847
aed1a261 16848 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16849 {
df58fc94 16850 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16851 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16852 BFD_RELOC_HI16_S);
16853
16854 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16855 mips_gp_register, -1, BFD_RELOC_GPREL16,
16856 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16857
16858 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16859 mips_gp_register, reg1);
16860 }
16861 else
16862 {
16863 expressionS ex;
16864
16865 ex.X_op = O_symbol;
4184909a 16866 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16867 ex.X_op_symbol = NULL;
16868 ex.X_add_number = 0;
6e1304d8 16869
aa6975fb
ILT
16870 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16871 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16872
16873 macro_build_lui (&ex, mips_gp_register);
16874 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16875 mips_gp_register, BFD_RELOC_LO16);
16876 }
f21f8242 16877
584892a6 16878 macro_end ();
6478892d 16879
8a75745d 16880 mips_assembling_insn = FALSE;
6478892d
TS
16881 demand_empty_rest_of_line ();
16882}
16883
16884static void
17a2f251 16885s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16886{
16887 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16888 .cplocal is ignored. */
6478892d
TS
16889 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16890 {
16891 s_ignore (0);
16892 return;
16893 }
16894
a276b80c
MR
16895 if (mips_opts.mips16)
16896 {
16897 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16898 ignore_rest_of_line ();
16899 return;
16900 }
16901
6478892d 16902 mips_gp_register = tc_get_register (0);
85b51719 16903 demand_empty_rest_of_line ();
6478892d
TS
16904}
16905
252b5132
RH
16906/* Handle the .cprestore pseudo-op. This stores $gp into a given
16907 offset from $sp. The offset is remembered, and after making a PIC
16908 call $gp is restored from that location. */
16909
16910static void
17a2f251 16911s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16912{
16913 expressionS ex;
252b5132 16914
6478892d 16915 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16916 .cprestore is ignored. */
6478892d 16917 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16918 {
16919 s_ignore (0);
16920 return;
16921 }
16922
a276b80c
MR
16923 if (mips_opts.mips16)
16924 {
16925 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16926 ignore_rest_of_line ();
16927 return;
16928 }
16929
252b5132 16930 mips_cprestore_offset = get_absolute_expression ();
7a621144 16931 mips_cprestore_valid = 1;
252b5132
RH
16932
16933 ex.X_op = O_constant;
16934 ex.X_add_symbol = NULL;
16935 ex.X_op_symbol = NULL;
16936 ex.X_add_number = mips_cprestore_offset;
16937
8a75745d
MR
16938 mips_mark_labels ();
16939 mips_assembling_insn = TRUE;
16940
584892a6 16941 macro_start ();
67c0d1eb
RS
16942 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16943 SP, HAVE_64BIT_ADDRESSES);
584892a6 16944 macro_end ();
252b5132 16945
8a75745d 16946 mips_assembling_insn = FALSE;
252b5132
RH
16947 demand_empty_rest_of_line ();
16948}
16949
6478892d 16950/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16951 was given in the preceding .cpsetup, it results in:
6478892d 16952 ld $gp, offset($sp)
76b3015f 16953
6478892d 16954 If a register $reg2 was given there, it results in:
54f4ddb3
TS
16955 daddu $gp, $reg2, $0 */
16956
6478892d 16957static void
17a2f251 16958s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16959{
16960 expressionS ex;
6478892d
TS
16961
16962 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16963 We also need NewABI support. */
16964 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16965 {
16966 s_ignore (0);
16967 return;
16968 }
16969
a276b80c
MR
16970 if (mips_opts.mips16)
16971 {
16972 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16973 ignore_rest_of_line ();
16974 return;
16975 }
16976
8a75745d
MR
16977 mips_mark_labels ();
16978 mips_assembling_insn = TRUE;
16979
584892a6 16980 macro_start ();
6478892d
TS
16981 if (mips_cpreturn_register == -1)
16982 {
16983 ex.X_op = O_constant;
16984 ex.X_add_symbol = NULL;
16985 ex.X_op_symbol = NULL;
16986 ex.X_add_number = mips_cpreturn_offset;
16987
67c0d1eb 16988 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
16989 }
16990 else
67c0d1eb 16991 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 16992 mips_cpreturn_register, 0);
584892a6 16993 macro_end ();
6478892d 16994
8a75745d 16995 mips_assembling_insn = FALSE;
6478892d
TS
16996 demand_empty_rest_of_line ();
16997}
16998
d0f13682
CLT
16999/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17000 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17001 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17002 debug information or MIPS16 TLS. */
741d6ea8
JM
17003
17004static void
d0f13682
CLT
17005s_tls_rel_directive (const size_t bytes, const char *dirstr,
17006 bfd_reloc_code_real_type rtype)
741d6ea8
JM
17007{
17008 expressionS ex;
17009 char *p;
17010
17011 expression (&ex);
17012
17013 if (ex.X_op != O_symbol)
17014 {
d0f13682 17015 as_bad (_("Unsupported use of %s"), dirstr);
741d6ea8
JM
17016 ignore_rest_of_line ();
17017 }
17018
17019 p = frag_more (bytes);
17020 md_number_to_chars (p, 0, bytes);
d0f13682 17021 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 17022 demand_empty_rest_of_line ();
de64cffd 17023 mips_clear_insn_labels ();
741d6ea8
JM
17024}
17025
17026/* Handle .dtprelword. */
17027
17028static void
17029s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17030{
d0f13682 17031 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
17032}
17033
17034/* Handle .dtpreldword. */
17035
17036static void
17037s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17038{
d0f13682
CLT
17039 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17040}
17041
17042/* Handle .tprelword. */
17043
17044static void
17045s_tprelword (int ignore ATTRIBUTE_UNUSED)
17046{
17047 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17048}
17049
17050/* Handle .tpreldword. */
17051
17052static void
17053s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17054{
17055 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
17056}
17057
6478892d
TS
17058/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17059 code. It sets the offset to use in gp_rel relocations. */
17060
17061static void
17a2f251 17062s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17063{
17064 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17065 We also need NewABI support. */
17066 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17067 {
17068 s_ignore (0);
17069 return;
17070 }
17071
def2e0dd 17072 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
17073
17074 demand_empty_rest_of_line ();
17075}
17076
252b5132
RH
17077/* Handle the .gpword pseudo-op. This is used when generating PIC
17078 code. It generates a 32 bit GP relative reloc. */
17079
17080static void
17a2f251 17081s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 17082{
a8dbcb85
TS
17083 segment_info_type *si;
17084 struct insn_label_list *l;
252b5132
RH
17085 expressionS ex;
17086 char *p;
17087
17088 /* When not generating PIC code, this is treated as .word. */
17089 if (mips_pic != SVR4_PIC)
17090 {
17091 s_cons (2);
17092 return;
17093 }
17094
a8dbcb85
TS
17095 si = seg_info (now_seg);
17096 l = si->label_list;
7d10b47d 17097 mips_emit_delays ();
252b5132 17098 if (auto_align)
462427c4 17099 mips_align (2, 0, l);
252b5132
RH
17100
17101 expression (&ex);
a1facbec 17102 mips_clear_insn_labels ();
252b5132
RH
17103
17104 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17105 {
17106 as_bad (_("Unsupported use of .gpword"));
17107 ignore_rest_of_line ();
17108 }
17109
17110 p = frag_more (4);
17a2f251 17111 md_number_to_chars (p, 0, 4);
b34976b6 17112 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 17113 BFD_RELOC_GPREL32);
252b5132
RH
17114
17115 demand_empty_rest_of_line ();
17116}
17117
10181a0d 17118static void
17a2f251 17119s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 17120{
a8dbcb85
TS
17121 segment_info_type *si;
17122 struct insn_label_list *l;
10181a0d
AO
17123 expressionS ex;
17124 char *p;
17125
17126 /* When not generating PIC code, this is treated as .dword. */
17127 if (mips_pic != SVR4_PIC)
17128 {
17129 s_cons (3);
17130 return;
17131 }
17132
a8dbcb85
TS
17133 si = seg_info (now_seg);
17134 l = si->label_list;
7d10b47d 17135 mips_emit_delays ();
10181a0d 17136 if (auto_align)
462427c4 17137 mips_align (3, 0, l);
10181a0d
AO
17138
17139 expression (&ex);
a1facbec 17140 mips_clear_insn_labels ();
10181a0d
AO
17141
17142 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17143 {
17144 as_bad (_("Unsupported use of .gpdword"));
17145 ignore_rest_of_line ();
17146 }
17147
17148 p = frag_more (8);
17a2f251 17149 md_number_to_chars (p, 0, 8);
a105a300 17150 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17151 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17152
17153 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17154 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17155 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17156
17157 demand_empty_rest_of_line ();
17158}
17159
a3f278e2
CM
17160/* Handle the .ehword pseudo-op. This is used when generating unwinding
17161 tables. It generates a R_MIPS_EH reloc. */
17162
17163static void
17164s_ehword (int ignore ATTRIBUTE_UNUSED)
17165{
17166 expressionS ex;
17167 char *p;
17168
17169 mips_emit_delays ();
17170
17171 expression (&ex);
17172 mips_clear_insn_labels ();
17173
17174 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17175 {
17176 as_bad (_("Unsupported use of .ehword"));
17177 ignore_rest_of_line ();
17178 }
17179
17180 p = frag_more (4);
17181 md_number_to_chars (p, 0, 4);
17182 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17183 BFD_RELOC_MIPS_EH);
17184
17185 demand_empty_rest_of_line ();
17186}
17187
252b5132
RH
17188/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17189 tables in SVR4 PIC code. */
17190
17191static void
17a2f251 17192s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17193{
252b5132
RH
17194 int reg;
17195
10181a0d
AO
17196 /* This is ignored when not generating SVR4 PIC code. */
17197 if (mips_pic != SVR4_PIC)
252b5132
RH
17198 {
17199 s_ignore (0);
17200 return;
17201 }
17202
8a75745d
MR
17203 mips_mark_labels ();
17204 mips_assembling_insn = TRUE;
17205
252b5132 17206 /* Add $gp to the register named as an argument. */
584892a6 17207 macro_start ();
252b5132 17208 reg = tc_get_register (0);
67c0d1eb 17209 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17210 macro_end ();
252b5132 17211
8a75745d 17212 mips_assembling_insn = FALSE;
bdaaa2e1 17213 demand_empty_rest_of_line ();
252b5132
RH
17214}
17215
17216/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17217 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17218 such as generating jalx instructions when needed. We also make
17219 them odd for the duration of the assembly, in order to generate the
17220 right sort of code. We will make them even in the adjust_symtab
17221 routine, while leaving them marked. This is convenient for the
17222 debugger and the disassembler. The linker knows to make them odd
17223 again. */
17224
17225static void
17a2f251 17226s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17227{
df58fc94 17228 mips_mark_labels ();
252b5132
RH
17229
17230 demand_empty_rest_of_line ();
17231}
17232
754e2bb9
RS
17233/* Handle a .stab[snd] directive. Ideally these directives would be
17234 implemented in a transparent way, so that removing them would not
17235 have any effect on the generated instructions. However, s_stab
17236 internally changes the section, so in practice we need to decide
17237 now whether the preceding label marks compressed code. We do not
17238 support changing the compression mode of a label after a .stab*
17239 directive, such as in:
17240
17241 foo:
17242 .stabs ...
17243 .set mips16
17244
17245 so the current mode wins. */
252b5132
RH
17246
17247static void
17a2f251 17248s_mips_stab (int type)
252b5132 17249{
754e2bb9 17250 mips_mark_labels ();
252b5132
RH
17251 s_stab (type);
17252}
17253
54f4ddb3 17254/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17255
17256static void
17a2f251 17257s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17258{
17259 char *name;
17260 int c;
17261 symbolS *symbolP;
17262 expressionS exp;
17263
17264 name = input_line_pointer;
17265 c = get_symbol_end ();
17266 symbolP = symbol_find_or_make (name);
17267 S_SET_WEAK (symbolP);
17268 *input_line_pointer = c;
17269
17270 SKIP_WHITESPACE ();
17271
17272 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17273 {
17274 if (S_IS_DEFINED (symbolP))
17275 {
20203fb9 17276 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17277 S_GET_NAME (symbolP));
17278 ignore_rest_of_line ();
17279 return;
17280 }
bdaaa2e1 17281
252b5132
RH
17282 if (*input_line_pointer == ',')
17283 {
17284 ++input_line_pointer;
17285 SKIP_WHITESPACE ();
17286 }
bdaaa2e1 17287
252b5132
RH
17288 expression (&exp);
17289 if (exp.X_op != O_symbol)
17290 {
20203fb9 17291 as_bad (_("bad .weakext directive"));
98d3f06f 17292 ignore_rest_of_line ();
252b5132
RH
17293 return;
17294 }
49309057 17295 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17296 }
17297
17298 demand_empty_rest_of_line ();
17299}
17300
17301/* Parse a register string into a number. Called from the ECOFF code
17302 to parse .frame. The argument is non-zero if this is the frame
17303 register, so that we can record it in mips_frame_reg. */
17304
17305int
17a2f251 17306tc_get_register (int frame)
252b5132 17307{
707bfff6 17308 unsigned int reg;
252b5132
RH
17309
17310 SKIP_WHITESPACE ();
707bfff6
TS
17311 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17312 reg = 0;
252b5132 17313 if (frame)
7a621144
DJ
17314 {
17315 mips_frame_reg = reg != 0 ? reg : SP;
17316 mips_frame_reg_valid = 1;
17317 mips_cprestore_valid = 0;
17318 }
252b5132
RH
17319 return reg;
17320}
17321
17322valueT
17a2f251 17323md_section_align (asection *seg, valueT addr)
252b5132
RH
17324{
17325 int align = bfd_get_section_alignment (stdoutput, seg);
17326
b4c71f56
TS
17327 if (IS_ELF)
17328 {
17329 /* We don't need to align ELF sections to the full alignment.
17330 However, Irix 5 may prefer that we align them at least to a 16
17331 byte boundary. We don't bother to align the sections if we
17332 are targeted for an embedded system. */
c41e87e3 17333 if (strncmp (TARGET_OS, "elf", 3) == 0)
b4c71f56
TS
17334 return addr;
17335 if (align > 4)
17336 align = 4;
17337 }
252b5132
RH
17338
17339 return ((addr + (1 << align) - 1) & (-1 << align));
17340}
17341
17342/* Utility routine, called from above as well. If called while the
17343 input file is still being read, it's only an approximation. (For
17344 example, a symbol may later become defined which appeared to be
17345 undefined earlier.) */
17346
17347static int
17a2f251 17348nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17349{
17350 if (sym == 0)
17351 return 0;
17352
4d0d148d 17353 if (g_switch_value > 0)
252b5132
RH
17354 {
17355 const char *symname;
17356 int change;
17357
c9914766 17358 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17359 register. It can be if it is smaller than the -G size or if
17360 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17361 not be referenced off the $gp, although it appears as though
252b5132
RH
17362 they can. */
17363 symname = S_GET_NAME (sym);
17364 if (symname != (const char *) NULL
17365 && (strcmp (symname, "eprol") == 0
17366 || strcmp (symname, "etext") == 0
17367 || strcmp (symname, "_gp") == 0
17368 || strcmp (symname, "edata") == 0
17369 || strcmp (symname, "_fbss") == 0
17370 || strcmp (symname, "_fdata") == 0
17371 || strcmp (symname, "_ftext") == 0
17372 || strcmp (symname, "end") == 0
17373 || strcmp (symname, "_gp_disp") == 0))
17374 change = 1;
17375 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17376 && (0
17377#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17378 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17379 && (symbol_get_obj (sym)->ecoff_extern_size
17380 <= g_switch_value))
252b5132
RH
17381#endif
17382 /* We must defer this decision until after the whole
17383 file has been read, since there might be a .extern
17384 after the first use of this symbol. */
17385 || (before_relaxing
17386#ifndef NO_ECOFF_DEBUGGING
49309057 17387 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17388#endif
17389 && S_GET_VALUE (sym) == 0)
17390 || (S_GET_VALUE (sym) != 0
17391 && S_GET_VALUE (sym) <= g_switch_value)))
17392 change = 0;
17393 else
17394 {
17395 const char *segname;
17396
17397 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17398 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17399 && strcmp (segname, ".lit4") != 0);
17400 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17401 && strcmp (segname, ".sbss") != 0
17402 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17403 && strncmp (segname, ".sbss.", 6) != 0
17404 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17405 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17406 }
17407 return change;
17408 }
17409 else
c9914766 17410 /* We are not optimizing for the $gp register. */
252b5132
RH
17411 return 1;
17412}
17413
5919d012
RS
17414
17415/* Return true if the given symbol should be considered local for SVR4 PIC. */
17416
17417static bfd_boolean
17a2f251 17418pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
17419{
17420 asection *symsec;
5919d012
RS
17421
17422 /* Handle the case of a symbol equated to another symbol. */
17423 while (symbol_equated_reloc_p (sym))
17424 {
17425 symbolS *n;
17426
5f0fe04b 17427 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17428 n = symbol_get_value_expression (sym)->X_add_symbol;
17429 if (n == sym)
17430 break;
17431 sym = n;
17432 }
17433
df1f3cda
DD
17434 if (symbol_section_p (sym))
17435 return TRUE;
17436
5919d012
RS
17437 symsec = S_GET_SEGMENT (sym);
17438
5919d012 17439 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17440 return (!bfd_is_und_section (symsec)
17441 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
17442 && !bfd_is_com_section (symsec)
17443 && !s_is_linkonce (sym, segtype)
5919d012
RS
17444#ifdef OBJ_ELF
17445 /* A global or weak symbol is treated as external. */
f43abd2b 17446 && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
5919d012
RS
17447#endif
17448 );
17449}
17450
17451
252b5132
RH
17452/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17453 extended opcode. SEC is the section the frag is in. */
17454
17455static int
17a2f251 17456mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
17457{
17458 int type;
3994f87e 17459 const struct mips16_immed_operand *op;
252b5132
RH
17460 offsetT val;
17461 int mintiny, maxtiny;
17462 segT symsec;
98aa84af 17463 fragS *sym_frag;
252b5132
RH
17464
17465 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17466 return 0;
17467 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17468 return 1;
17469
17470 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17471 op = mips16_immed_operands;
17472 while (op->type != type)
17473 {
17474 ++op;
9c2799c2 17475 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
17476 }
17477
17478 if (op->unsp)
17479 {
17480 if (type == '<' || type == '>' || type == '[' || type == ']')
17481 {
17482 mintiny = 1;
17483 maxtiny = 1 << op->nbits;
17484 }
17485 else
17486 {
17487 mintiny = 0;
17488 maxtiny = (1 << op->nbits) - 1;
17489 }
17490 }
17491 else
17492 {
17493 mintiny = - (1 << (op->nbits - 1));
17494 maxtiny = (1 << (op->nbits - 1)) - 1;
17495 }
17496
98aa84af 17497 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 17498 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 17499 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
17500
17501 if (op->pcrel)
17502 {
17503 addressT addr;
17504
17505 /* We won't have the section when we are called from
17506 mips_relax_frag. However, we will always have been called
17507 from md_estimate_size_before_relax first. If this is a
17508 branch to a different section, we mark it as such. If SEC is
17509 NULL, and the frag is not marked, then it must be a branch to
17510 the same section. */
17511 if (sec == NULL)
17512 {
17513 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17514 return 1;
17515 }
17516 else
17517 {
98aa84af 17518 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
17519 if (symsec != sec)
17520 {
17521 fragp->fr_subtype =
17522 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17523
17524 /* FIXME: We should support this, and let the linker
17525 catch branches and loads that are out of range. */
17526 as_bad_where (fragp->fr_file, fragp->fr_line,
17527 _("unsupported PC relative reference to different section"));
17528
17529 return 1;
17530 }
98aa84af
AM
17531 if (fragp != sym_frag && sym_frag->fr_address == 0)
17532 /* Assume non-extended on the first relaxation pass.
17533 The address we have calculated will be bogus if this is
17534 a forward branch to another frag, as the forward frag
17535 will have fr_address == 0. */
17536 return 0;
252b5132
RH
17537 }
17538
17539 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
17540 the same section. If the relax_marker of the symbol fragment
17541 differs from the relax_marker of this fragment, we have not
17542 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
17543 in STRETCH in order to get a better estimate of the address.
17544 This particularly matters because of the shift bits. */
17545 if (stretch != 0
98aa84af 17546 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
17547 {
17548 fragS *f;
17549
17550 /* Adjust stretch for any alignment frag. Note that if have
17551 been expanding the earlier code, the symbol may be
17552 defined in what appears to be an earlier frag. FIXME:
17553 This doesn't handle the fr_subtype field, which specifies
17554 a maximum number of bytes to skip when doing an
17555 alignment. */
98aa84af 17556 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
17557 {
17558 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17559 {
17560 if (stretch < 0)
17561 stretch = - ((- stretch)
17562 & ~ ((1 << (int) f->fr_offset) - 1));
17563 else
17564 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17565 if (stretch == 0)
17566 break;
17567 }
17568 }
17569 if (f != NULL)
17570 val += stretch;
17571 }
17572
17573 addr = fragp->fr_address + fragp->fr_fix;
17574
17575 /* The base address rules are complicated. The base address of
17576 a branch is the following instruction. The base address of a
17577 PC relative load or add is the instruction itself, but if it
17578 is in a delay slot (in which case it can not be extended) use
17579 the address of the instruction whose delay slot it is in. */
17580 if (type == 'p' || type == 'q')
17581 {
17582 addr += 2;
17583
17584 /* If we are currently assuming that this frag should be
17585 extended, then, the current address is two bytes
bdaaa2e1 17586 higher. */
252b5132
RH
17587 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17588 addr += 2;
17589
17590 /* Ignore the low bit in the target, since it will be set
17591 for a text label. */
17592 if ((val & 1) != 0)
17593 --val;
17594 }
17595 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17596 addr -= 4;
17597 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17598 addr -= 2;
17599
17600 val -= addr & ~ ((1 << op->shift) - 1);
17601
17602 /* Branch offsets have an implicit 0 in the lowest bit. */
17603 if (type == 'p' || type == 'q')
17604 val /= 2;
17605
17606 /* If any of the shifted bits are set, we must use an extended
17607 opcode. If the address depends on the size of this
17608 instruction, this can lead to a loop, so we arrange to always
17609 use an extended opcode. We only check this when we are in
17610 the main relaxation loop, when SEC is NULL. */
17611 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17612 {
17613 fragp->fr_subtype =
17614 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17615 return 1;
17616 }
17617
17618 /* If we are about to mark a frag as extended because the value
17619 is precisely maxtiny + 1, then there is a chance of an
17620 infinite loop as in the following code:
17621 la $4,foo
17622 .skip 1020
17623 .align 2
17624 foo:
17625 In this case when the la is extended, foo is 0x3fc bytes
17626 away, so the la can be shrunk, but then foo is 0x400 away, so
17627 the la must be extended. To avoid this loop, we mark the
17628 frag as extended if it was small, and is about to become
17629 extended with a value of maxtiny + 1. */
17630 if (val == ((maxtiny + 1) << op->shift)
17631 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17632 && sec == NULL)
17633 {
17634 fragp->fr_subtype =
17635 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17636 return 1;
17637 }
17638 }
17639 else if (symsec != absolute_section && sec != NULL)
17640 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17641
17642 if ((val & ((1 << op->shift) - 1)) != 0
17643 || val < (mintiny << op->shift)
17644 || val > (maxtiny << op->shift))
17645 return 1;
17646 else
17647 return 0;
17648}
17649
4a6a3df4
AO
17650/* Compute the length of a branch sequence, and adjust the
17651 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17652 worst-case length is computed, with UPDATE being used to indicate
17653 whether an unconditional (-1), branch-likely (+1) or regular (0)
17654 branch is to be computed. */
17655static int
17a2f251 17656relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17657{
b34976b6 17658 bfd_boolean toofar;
4a6a3df4
AO
17659 int length;
17660
17661 if (fragp
17662 && S_IS_DEFINED (fragp->fr_symbol)
17663 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17664 {
17665 addressT addr;
17666 offsetT val;
17667
17668 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17669
17670 addr = fragp->fr_address + fragp->fr_fix + 4;
17671
17672 val -= addr;
17673
17674 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17675 }
17676 else if (fragp)
17677 /* If the symbol is not defined or it's in a different segment,
17678 assume the user knows what's going on and emit a short
17679 branch. */
b34976b6 17680 toofar = FALSE;
4a6a3df4 17681 else
b34976b6 17682 toofar = TRUE;
4a6a3df4
AO
17683
17684 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17685 fragp->fr_subtype
66b3e8da
MR
17686 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17687 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17688 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17689 RELAX_BRANCH_LINK (fragp->fr_subtype),
17690 toofar);
17691
17692 length = 4;
17693 if (toofar)
17694 {
17695 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17696 length += 8;
17697
17698 if (mips_pic != NO_PIC)
17699 {
17700 /* Additional space for PIC loading of target address. */
17701 length += 8;
17702 if (mips_opts.isa == ISA_MIPS1)
17703 /* Additional space for $at-stabilizing nop. */
17704 length += 4;
17705 }
17706
17707 /* If branch is conditional. */
17708 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17709 length += 8;
17710 }
b34976b6 17711
4a6a3df4
AO
17712 return length;
17713}
17714
df58fc94
RS
17715/* Compute the length of a branch sequence, and adjust the
17716 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17717 worst-case length is computed, with UPDATE being used to indicate
17718 whether an unconditional (-1), or regular (0) branch is to be
17719 computed. */
17720
17721static int
17722relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17723{
17724 bfd_boolean toofar;
17725 int length;
17726
17727 if (fragp
17728 && S_IS_DEFINED (fragp->fr_symbol)
17729 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17730 {
17731 addressT addr;
17732 offsetT val;
17733
17734 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17735 /* Ignore the low bit in the target, since it will be set
17736 for a text label. */
17737 if ((val & 1) != 0)
17738 --val;
17739
17740 addr = fragp->fr_address + fragp->fr_fix + 4;
17741
17742 val -= addr;
17743
17744 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17745 }
17746 else if (fragp)
17747 /* If the symbol is not defined or it's in a different segment,
17748 assume the user knows what's going on and emit a short
17749 branch. */
17750 toofar = FALSE;
17751 else
17752 toofar = TRUE;
17753
17754 if (fragp && update
17755 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17756 fragp->fr_subtype = (toofar
17757 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17758 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17759
17760 length = 4;
17761 if (toofar)
17762 {
17763 bfd_boolean compact_known = fragp != NULL;
17764 bfd_boolean compact = FALSE;
17765 bfd_boolean uncond;
17766
17767 if (compact_known)
17768 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17769 if (fragp)
17770 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17771 else
17772 uncond = update < 0;
17773
17774 /* If label is out of range, we turn branch <br>:
17775
17776 <br> label # 4 bytes
17777 0:
17778
17779 into:
17780
17781 j label # 4 bytes
17782 nop # 2 bytes if compact && !PIC
17783 0:
17784 */
17785 if (mips_pic == NO_PIC && (!compact_known || compact))
17786 length += 2;
17787
17788 /* If assembling PIC code, we further turn:
17789
17790 j label # 4 bytes
17791
17792 into:
17793
17794 lw/ld at, %got(label)(gp) # 4 bytes
17795 d/addiu at, %lo(label) # 4 bytes
17796 jr/c at # 2 bytes
17797 */
17798 if (mips_pic != NO_PIC)
17799 length += 6;
17800
17801 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17802
17803 <brneg> 0f # 4 bytes
17804 nop # 2 bytes if !compact
17805 */
17806 if (!uncond)
17807 length += (compact_known && compact) ? 4 : 6;
17808 }
17809
17810 return length;
17811}
17812
17813/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17814 bit accordingly. */
17815
17816static int
17817relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17818{
17819 bfd_boolean toofar;
17820
df58fc94
RS
17821 if (fragp
17822 && S_IS_DEFINED (fragp->fr_symbol)
17823 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17824 {
17825 addressT addr;
17826 offsetT val;
17827 int type;
17828
17829 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17830 /* Ignore the low bit in the target, since it will be set
17831 for a text label. */
17832 if ((val & 1) != 0)
17833 --val;
17834
17835 /* Assume this is a 2-byte branch. */
17836 addr = fragp->fr_address + fragp->fr_fix + 2;
17837
17838 /* We try to avoid the infinite loop by not adding 2 more bytes for
17839 long branches. */
17840
17841 val -= addr;
17842
17843 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17844 if (type == 'D')
17845 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17846 else if (type == 'E')
17847 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17848 else
17849 abort ();
17850 }
17851 else
17852 /* If the symbol is not defined or it's in a different segment,
17853 we emit a normal 32-bit branch. */
17854 toofar = TRUE;
17855
17856 if (fragp && update
17857 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17858 fragp->fr_subtype
17859 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17860 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17861
17862 if (toofar)
17863 return 4;
17864
17865 return 2;
17866}
17867
252b5132
RH
17868/* Estimate the size of a frag before relaxing. Unless this is the
17869 mips16, we are not really relaxing here, and the final size is
17870 encoded in the subtype information. For the mips16, we have to
17871 decide whether we are using an extended opcode or not. */
17872
252b5132 17873int
17a2f251 17874md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17875{
5919d012 17876 int change;
252b5132 17877
4a6a3df4
AO
17878 if (RELAX_BRANCH_P (fragp->fr_subtype))
17879 {
17880
b34976b6
AM
17881 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17882
4a6a3df4
AO
17883 return fragp->fr_var;
17884 }
17885
252b5132 17886 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
17887 /* We don't want to modify the EXTENDED bit here; it might get us
17888 into infinite loops. We change it only in mips_relax_frag(). */
17889 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 17890
df58fc94
RS
17891 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17892 {
17893 int length = 4;
17894
17895 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17896 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17897 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17898 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17899 fragp->fr_var = length;
17900
17901 return length;
17902 }
17903
252b5132 17904 if (mips_pic == NO_PIC)
5919d012 17905 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 17906 else if (mips_pic == SVR4_PIC)
5919d012 17907 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
17908 else if (mips_pic == VXWORKS_PIC)
17909 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17910 change = 0;
252b5132
RH
17911 else
17912 abort ();
17913
17914 if (change)
17915 {
4d7206a2 17916 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 17917 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 17918 }
4d7206a2
RS
17919 else
17920 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
17921}
17922
17923/* This is called to see whether a reloc against a defined symbol
de7e6852 17924 should be converted into a reloc against a section. */
252b5132
RH
17925
17926int
17a2f251 17927mips_fix_adjustable (fixS *fixp)
252b5132 17928{
252b5132
RH
17929 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17930 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17931 return 0;
a161fe53 17932
252b5132
RH
17933 if (fixp->fx_addsy == NULL)
17934 return 1;
a161fe53 17935
de7e6852
RS
17936 /* If symbol SYM is in a mergeable section, relocations of the form
17937 SYM + 0 can usually be made section-relative. The mergeable data
17938 is then identified by the section offset rather than by the symbol.
17939
17940 However, if we're generating REL LO16 relocations, the offset is split
17941 between the LO16 and parterning high part relocation. The linker will
17942 need to recalculate the complete offset in order to correctly identify
17943 the merge data.
17944
17945 The linker has traditionally not looked for the parterning high part
17946 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17947 placed anywhere. Rather than break backwards compatibility by changing
17948 this, it seems better not to force the issue, and instead keep the
17949 original symbol. This will work with either linker behavior. */
738e5348 17950 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 17951 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
17952 && HAVE_IN_PLACE_ADDENDS
17953 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17954 return 0;
17955
ce70d90a 17956 /* There is no place to store an in-place offset for JALR relocations.
2de39019
CM
17957 Likewise an in-range offset of limited PC-relative relocations may
17958 overflow the in-place relocatable field if recalculated against the
17959 start address of the symbol's containing section. */
ce70d90a 17960 if (HAVE_IN_PLACE_ADDENDS
2de39019
CM
17961 && (limited_pcrel_reloc_p (fixp->fx_r_type)
17962 || jalr_reloc_p (fixp->fx_r_type)))
1180b5a4
RS
17963 return 0;
17964
252b5132 17965#ifdef OBJ_ELF
b314ec0e
RS
17966 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17967 to a floating-point stub. The same is true for non-R_MIPS16_26
17968 relocations against MIPS16 functions; in this case, the stub becomes
17969 the function's canonical address.
17970
17971 Floating-point stubs are stored in unique .mips16.call.* or
17972 .mips16.fn.* sections. If a stub T for function F is in section S,
17973 the first relocation in section S must be against F; this is how the
17974 linker determines the target function. All relocations that might
17975 resolve to T must also be against F. We therefore have the following
17976 restrictions, which are given in an intentionally-redundant way:
17977
17978 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17979 symbols.
17980
17981 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17982 if that stub might be used.
17983
17984 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17985 symbols.
17986
17987 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17988 that stub might be used.
17989
17990 There is a further restriction:
17991
df58fc94
RS
17992 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17993 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17994 targets with in-place addends; the relocation field cannot
b314ec0e
RS
17995 encode the low bit.
17996
df58fc94
RS
17997 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17998 against a MIPS16 symbol. We deal with (5) by by not reducing any
17999 such relocations on REL targets.
b314ec0e
RS
18000
18001 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18002 relocation against some symbol R, no relocation against R may be
18003 reduced. (Note that this deals with (2) as well as (1) because
18004 relocations against global symbols will never be reduced on ELF
18005 targets.) This approach is a little simpler than trying to detect
18006 stub sections, and gives the "all or nothing" per-symbol consistency
18007 that we have for MIPS16 symbols. */
f43abd2b 18008 if (IS_ELF
b314ec0e 18009 && fixp->fx_subsy == NULL
30c09090 18010 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
df58fc94
RS
18011 || *symbol_get_tc (fixp->fx_addsy)
18012 || (HAVE_IN_PLACE_ADDENDS
18013 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
18014 && jmp_reloc_p (fixp->fx_r_type))))
252b5132
RH
18015 return 0;
18016#endif
a161fe53 18017
252b5132
RH
18018 return 1;
18019}
18020
18021/* Translate internal representation of relocation info to BFD target
18022 format. */
18023
18024arelent **
17a2f251 18025tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
18026{
18027 static arelent *retval[4];
18028 arelent *reloc;
18029 bfd_reloc_code_real_type code;
18030
4b0cff4e
TS
18031 memset (retval, 0, sizeof(retval));
18032 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
18033 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
18034 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
18035 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18036
bad36eac
DJ
18037 if (fixp->fx_pcrel)
18038 {
df58fc94
RS
18039 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18040 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18041 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
18042 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18043 || fixp->fx_r_type == BFD_RELOC_32_PCREL);
bad36eac
DJ
18044
18045 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18046 Relocations want only the symbol offset. */
18047 reloc->addend = fixp->fx_addnumber + reloc->address;
f43abd2b 18048 if (!IS_ELF)
bad36eac
DJ
18049 {
18050 /* A gruesome hack which is a result of the gruesome gas
18051 reloc handling. What's worse, for COFF (as opposed to
18052 ECOFF), we might need yet another copy of reloc->address.
18053 See bfd_install_relocation. */
18054 reloc->addend += reloc->address;
18055 }
18056 }
18057 else
18058 reloc->addend = fixp->fx_addnumber;
252b5132 18059
438c16b8
TS
18060 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18061 entry to be used in the relocation's section offset. */
18062 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18063 {
18064 reloc->address = reloc->addend;
18065 reloc->addend = 0;
18066 }
18067
252b5132 18068 code = fixp->fx_r_type;
252b5132 18069
bad36eac 18070 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18071 if (reloc->howto == NULL)
18072 {
18073 as_bad_where (fixp->fx_file, fixp->fx_line,
18074 _("Can not represent %s relocation in this object file format"),
18075 bfd_get_reloc_code_name (code));
18076 retval[0] = NULL;
18077 }
18078
18079 return retval;
18080}
18081
18082/* Relax a machine dependent frag. This returns the amount by which
18083 the current size of the frag should change. */
18084
18085int
17a2f251 18086mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18087{
4a6a3df4
AO
18088 if (RELAX_BRANCH_P (fragp->fr_subtype))
18089 {
18090 offsetT old_var = fragp->fr_var;
b34976b6
AM
18091
18092 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
18093
18094 return fragp->fr_var - old_var;
18095 }
18096
df58fc94
RS
18097 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18098 {
18099 offsetT old_var = fragp->fr_var;
18100 offsetT new_var = 4;
18101
18102 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18103 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18104 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18105 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18106 fragp->fr_var = new_var;
18107
18108 return new_var - old_var;
18109 }
18110
252b5132
RH
18111 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18112 return 0;
18113
c4e7957c 18114 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
18115 {
18116 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18117 return 0;
18118 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18119 return 2;
18120 }
18121 else
18122 {
18123 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18124 return 0;
18125 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18126 return -2;
18127 }
18128
18129 return 0;
18130}
18131
18132/* Convert a machine dependent frag. */
18133
18134void
17a2f251 18135md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18136{
4a6a3df4
AO
18137 if (RELAX_BRANCH_P (fragp->fr_subtype))
18138 {
4d68580a 18139 char *buf;
4a6a3df4
AO
18140 unsigned long insn;
18141 expressionS exp;
18142 fixS *fixp;
b34976b6 18143
4d68580a
RS
18144 buf = fragp->fr_literal + fragp->fr_fix;
18145 insn = read_insn (buf);
b34976b6 18146
4a6a3df4
AO
18147 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18148 {
18149 /* We generate a fixup instead of applying it right now
18150 because, if there are linker relaxations, we're going to
18151 need the relocations. */
18152 exp.X_op = O_symbol;
18153 exp.X_add_symbol = fragp->fr_symbol;
18154 exp.X_add_number = fragp->fr_offset;
18155
4d68580a
RS
18156 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18157 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18158 fixp->fx_file = fragp->fr_file;
18159 fixp->fx_line = fragp->fr_line;
b34976b6 18160
4d68580a 18161 buf = write_insn (buf, insn);
4a6a3df4
AO
18162 }
18163 else
18164 {
18165 int i;
18166
18167 as_warn_where (fragp->fr_file, fragp->fr_line,
5c4f07ba 18168 _("Relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18169
18170 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18171 goto uncond;
18172
18173 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18174 {
18175 /* Reverse the branch. */
18176 switch ((insn >> 28) & 0xf)
18177 {
18178 case 4:
18179 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
18180 have the condition reversed by tweaking a single
18181 bit, and their opcodes all have 0x4???????. */
9c2799c2 18182 gas_assert ((insn & 0xf1000000) == 0x41000000);
4a6a3df4
AO
18183 insn ^= 0x00010000;
18184 break;
18185
18186 case 0:
18187 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18188 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18189 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18190 insn ^= 0x00010000;
18191 break;
b34976b6 18192
4a6a3df4
AO
18193 case 1:
18194 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18195 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18196 insn ^= 0x04000000;
18197 break;
18198
18199 default:
18200 abort ();
18201 }
18202 }
18203
18204 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18205 {
18206 /* Clear the and-link bit. */
9c2799c2 18207 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18208
54f4ddb3
TS
18209 /* bltzal 0x04100000 bgezal 0x04110000
18210 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18211 insn &= ~0x00100000;
18212 }
18213
18214 /* Branch over the branch (if the branch was likely) or the
18215 full jump (not likely case). Compute the offset from the
18216 current instruction to branch to. */
18217 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18218 i = 16;
18219 else
18220 {
18221 /* How many bytes in instructions we've already emitted? */
4d68580a 18222 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18223 /* How many bytes in instructions from here to the end? */
18224 i = fragp->fr_var - i;
18225 }
18226 /* Convert to instruction count. */
18227 i >>= 2;
18228 /* Branch counts from the next instruction. */
b34976b6 18229 i--;
4a6a3df4
AO
18230 insn |= i;
18231 /* Branch over the jump. */
4d68580a 18232 buf = write_insn (buf, insn);
4a6a3df4 18233
54f4ddb3 18234 /* nop */
4d68580a 18235 buf = write_insn (buf, 0);
4a6a3df4
AO
18236
18237 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18238 {
18239 /* beql $0, $0, 2f */
18240 insn = 0x50000000;
18241 /* Compute the PC offset from the current instruction to
18242 the end of the variable frag. */
18243 /* How many bytes in instructions we've already emitted? */
4d68580a 18244 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18245 /* How many bytes in instructions from here to the end? */
18246 i = fragp->fr_var - i;
18247 /* Convert to instruction count. */
18248 i >>= 2;
18249 /* Don't decrement i, because we want to branch over the
18250 delay slot. */
4a6a3df4 18251 insn |= i;
4a6a3df4 18252
4d68580a
RS
18253 buf = write_insn (buf, insn);
18254 buf = write_insn (buf, 0);
4a6a3df4
AO
18255 }
18256
18257 uncond:
18258 if (mips_pic == NO_PIC)
18259 {
18260 /* j or jal. */
18261 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18262 ? 0x0c000000 : 0x08000000);
18263 exp.X_op = O_symbol;
18264 exp.X_add_symbol = fragp->fr_symbol;
18265 exp.X_add_number = fragp->fr_offset;
18266
4d68580a
RS
18267 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18268 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18269 fixp->fx_file = fragp->fr_file;
18270 fixp->fx_line = fragp->fr_line;
18271
4d68580a 18272 buf = write_insn (buf, insn);
4a6a3df4
AO
18273 }
18274 else
18275 {
66b3e8da
MR
18276 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18277
4a6a3df4 18278 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18279 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18280 insn |= at << OP_SH_RT;
4a6a3df4
AO
18281 exp.X_op = O_symbol;
18282 exp.X_add_symbol = fragp->fr_symbol;
18283 exp.X_add_number = fragp->fr_offset;
18284
18285 if (fragp->fr_offset)
18286 {
18287 exp.X_add_symbol = make_expr_symbol (&exp);
18288 exp.X_add_number = 0;
18289 }
18290
4d68580a
RS
18291 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18292 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18293 fixp->fx_file = fragp->fr_file;
18294 fixp->fx_line = fragp->fr_line;
18295
4d68580a 18296 buf = write_insn (buf, insn);
b34976b6 18297
4a6a3df4 18298 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18299 /* nop */
18300 buf = write_insn (buf, 0);
4a6a3df4
AO
18301
18302 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18303 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18304 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18305
4d68580a
RS
18306 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18307 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18308 fixp->fx_file = fragp->fr_file;
18309 fixp->fx_line = fragp->fr_line;
b34976b6 18310
4d68580a 18311 buf = write_insn (buf, insn);
4a6a3df4
AO
18312
18313 /* j(al)r $at. */
18314 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18315 insn = 0x0000f809;
4a6a3df4 18316 else
66b3e8da
MR
18317 insn = 0x00000008;
18318 insn |= at << OP_SH_RS;
4a6a3df4 18319
4d68580a 18320 buf = write_insn (buf, insn);
4a6a3df4
AO
18321 }
18322 }
18323
4a6a3df4 18324 fragp->fr_fix += fragp->fr_var;
4d68580a 18325 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18326 return;
18327 }
18328
df58fc94
RS
18329 /* Relax microMIPS branches. */
18330 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18331 {
4d68580a 18332 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18333 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18334 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18335 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18336 bfd_boolean short_ds;
df58fc94
RS
18337 unsigned long insn;
18338 expressionS exp;
18339 fixS *fixp;
18340
18341 exp.X_op = O_symbol;
18342 exp.X_add_symbol = fragp->fr_symbol;
18343 exp.X_add_number = fragp->fr_offset;
18344
18345 fragp->fr_fix += fragp->fr_var;
18346
18347 /* Handle 16-bit branches that fit or are forced to fit. */
18348 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18349 {
18350 /* We generate a fixup instead of applying it right now,
18351 because if there is linker relaxation, we're going to
18352 need the relocations. */
18353 if (type == 'D')
4d68580a 18354 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18355 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18356 else if (type == 'E')
4d68580a 18357 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18358 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18359 else
18360 abort ();
18361
18362 fixp->fx_file = fragp->fr_file;
18363 fixp->fx_line = fragp->fr_line;
18364
18365 /* These relocations can have an addend that won't fit in
18366 2 octets. */
18367 fixp->fx_no_overflow = 1;
18368
18369 return;
18370 }
18371
2309ddf2 18372 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18373 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18374 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18375 {
18376 /* We generate a fixup instead of applying it right now,
18377 because if there is linker relaxation, we're going to
18378 need the relocations. */
4d68580a
RS
18379 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18380 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18381 fixp->fx_file = fragp->fr_file;
18382 fixp->fx_line = fragp->fr_line;
18383
18384 if (type == 0)
18385 return;
18386 }
18387
18388 /* Relax 16-bit branches to 32-bit branches. */
18389 if (type != 0)
18390 {
4d68580a 18391 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18392
18393 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18394 insn = 0x94000000; /* beq */
18395 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18396 {
18397 unsigned long regno;
18398
18399 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18400 regno = micromips_to_32_reg_d_map [regno];
18401 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18402 insn |= regno << MICROMIPSOP_SH_RS;
18403 }
18404 else
18405 abort ();
18406
18407 /* Nothing else to do, just write it out. */
18408 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18409 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18410 {
4d68580a
RS
18411 buf = write_compressed_insn (buf, insn, 4);
18412 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18413 return;
18414 }
18415 }
18416 else
4d68580a 18417 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18418
18419 /* Relax 32-bit branches to a sequence of instructions. */
18420 as_warn_where (fragp->fr_file, fragp->fr_line,
18421 _("Relaxed out-of-range branch into a jump"));
18422
2309ddf2
MR
18423 /* Set the short-delay-slot bit. */
18424 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
18425
18426 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18427 {
18428 symbolS *l;
18429
18430 /* Reverse the branch. */
18431 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18432 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18433 insn ^= 0x20000000;
18434 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18435 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18436 || (insn & 0xffe00000) == 0x40800000 /* blez */
18437 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18438 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18439 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18440 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18441 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18442 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18443 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18444 insn ^= 0x00400000;
18445 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18446 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18447 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18448 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18449 insn ^= 0x00200000;
18450 else
18451 abort ();
18452
18453 if (al)
18454 {
18455 /* Clear the and-link and short-delay-slot bits. */
18456 gas_assert ((insn & 0xfda00000) == 0x40200000);
18457
18458 /* bltzal 0x40200000 bgezal 0x40600000 */
18459 /* bltzals 0x42200000 bgezals 0x42600000 */
18460 insn &= ~0x02200000;
18461 }
18462
18463 /* Make a label at the end for use with the branch. */
18464 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18465 micromips_label_inc ();
18466#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
18467 if (IS_ELF)
18468 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18469#endif
18470
18471 /* Refer to it. */
4d68580a
RS
18472 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18473 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18474 fixp->fx_file = fragp->fr_file;
18475 fixp->fx_line = fragp->fr_line;
18476
18477 /* Branch over the jump. */
4d68580a 18478 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18479 if (!compact)
4d68580a
RS
18480 /* nop */
18481 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
18482 }
18483
18484 if (mips_pic == NO_PIC)
18485 {
2309ddf2
MR
18486 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
18487
df58fc94
RS
18488 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18489 insn = al ? jal : 0xd4000000;
18490
4d68580a
RS
18491 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18492 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18493 fixp->fx_file = fragp->fr_file;
18494 fixp->fx_line = fragp->fr_line;
18495
4d68580a 18496 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18497 if (compact)
4d68580a
RS
18498 /* nop */
18499 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
18500 }
18501 else
18502 {
18503 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
18504 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18505 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
18506
18507 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18508 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18509 insn |= at << MICROMIPSOP_SH_RT;
18510
18511 if (exp.X_add_number)
18512 {
18513 exp.X_add_symbol = make_expr_symbol (&exp);
18514 exp.X_add_number = 0;
18515 }
18516
4d68580a
RS
18517 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18518 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18519 fixp->fx_file = fragp->fr_file;
18520 fixp->fx_line = fragp->fr_line;
18521
4d68580a 18522 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18523
18524 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18525 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18526 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18527
4d68580a
RS
18528 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18529 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18530 fixp->fx_file = fragp->fr_file;
18531 fixp->fx_line = fragp->fr_line;
18532
4d68580a 18533 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18534
18535 /* jr/jrc/jalr/jalrs $at */
18536 insn = al ? jalr : jr;
18537 insn |= at << MICROMIPSOP_SH_MJ;
18538
4d68580a 18539 buf = write_compressed_insn (buf, insn, 2);
df58fc94
RS
18540 }
18541
4d68580a 18542 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18543 return;
18544 }
18545
252b5132
RH
18546 if (RELAX_MIPS16_P (fragp->fr_subtype))
18547 {
18548 int type;
3994f87e 18549 const struct mips16_immed_operand *op;
252b5132 18550 offsetT val;
5c04167a
RS
18551 char *buf;
18552 unsigned int user_length, length;
252b5132 18553 unsigned long insn;
5c04167a 18554 bfd_boolean ext;
252b5132
RH
18555
18556 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18557 op = mips16_immed_operands;
18558 while (op->type != type)
18559 ++op;
18560
5c04167a 18561 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
5f5f22c0 18562 val = resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
18563 if (op->pcrel)
18564 {
18565 addressT addr;
18566
18567 addr = fragp->fr_address + fragp->fr_fix;
18568
18569 /* The rules for the base address of a PC relative reloc are
18570 complicated; see mips16_extended_frag. */
18571 if (type == 'p' || type == 'q')
18572 {
18573 addr += 2;
18574 if (ext)
18575 addr += 2;
18576 /* Ignore the low bit in the target, since it will be
18577 set for a text label. */
18578 if ((val & 1) != 0)
18579 --val;
18580 }
18581 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18582 addr -= 4;
18583 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18584 addr -= 2;
18585
18586 addr &= ~ (addressT) ((1 << op->shift) - 1);
18587 val -= addr;
18588
18589 /* Make sure the section winds up with the alignment we have
18590 assumed. */
18591 if (op->shift > 0)
18592 record_alignment (asec, op->shift);
18593 }
18594
18595 if (ext
18596 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18597 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18598 as_warn_where (fragp->fr_file, fragp->fr_line,
18599 _("extended instruction in delay slot"));
18600
5c04167a 18601 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 18602
4d68580a 18603 insn = read_compressed_insn (buf, 2);
5c04167a
RS
18604 if (ext)
18605 insn |= MIPS16_EXTEND;
252b5132 18606
5c04167a
RS
18607 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18608 user_length = 4;
18609 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18610 user_length = 2;
18611 else
18612 user_length = 0;
18613
43c0598f 18614 mips16_immed (fragp->fr_file, fragp->fr_line, type,
c150d1d2 18615 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 18616
5c04167a
RS
18617 length = (ext ? 4 : 2);
18618 gas_assert (mips16_opcode_length (insn) == length);
18619 write_compressed_insn (buf, insn, length);
18620 fragp->fr_fix += length;
252b5132
RH
18621 }
18622 else
18623 {
df58fc94
RS
18624 relax_substateT subtype = fragp->fr_subtype;
18625 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18626 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
18627 int first, second;
18628 fixS *fixp;
252b5132 18629
df58fc94
RS
18630 first = RELAX_FIRST (subtype);
18631 second = RELAX_SECOND (subtype);
4d7206a2 18632 fixp = (fixS *) fragp->fr_opcode;
252b5132 18633
df58fc94
RS
18634 /* If the delay slot chosen does not match the size of the instruction,
18635 then emit a warning. */
18636 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18637 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18638 {
18639 relax_substateT s;
18640 const char *msg;
18641
18642 s = subtype & (RELAX_DELAY_SLOT_16BIT
18643 | RELAX_DELAY_SLOT_SIZE_FIRST
18644 | RELAX_DELAY_SLOT_SIZE_SECOND);
18645 msg = macro_warning (s);
18646 if (msg != NULL)
db9b2be4 18647 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
18648 subtype &= ~s;
18649 }
18650
584892a6 18651 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 18652 if (use_second == second_longer)
584892a6 18653 {
df58fc94
RS
18654 relax_substateT s;
18655 const char *msg;
18656
18657 s = (subtype
18658 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18659 msg = macro_warning (s);
18660 if (msg != NULL)
db9b2be4 18661 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 18662 subtype &= ~s;
584892a6
RS
18663 }
18664
4d7206a2
RS
18665 /* Go through all the fixups for the first sequence. Disable them
18666 (by marking them as done) if we're going to use the second
18667 sequence instead. */
18668 while (fixp
18669 && fixp->fx_frag == fragp
18670 && fixp->fx_where < fragp->fr_fix - second)
18671 {
df58fc94 18672 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18673 fixp->fx_done = 1;
18674 fixp = fixp->fx_next;
18675 }
252b5132 18676
4d7206a2
RS
18677 /* Go through the fixups for the second sequence. Disable them if
18678 we're going to use the first sequence, otherwise adjust their
18679 addresses to account for the relaxation. */
18680 while (fixp && fixp->fx_frag == fragp)
18681 {
df58fc94 18682 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18683 fixp->fx_where -= first;
18684 else
18685 fixp->fx_done = 1;
18686 fixp = fixp->fx_next;
18687 }
18688
18689 /* Now modify the frag contents. */
df58fc94 18690 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18691 {
18692 char *start;
18693
18694 start = fragp->fr_literal + fragp->fr_fix - first - second;
18695 memmove (start, start + first, second);
18696 fragp->fr_fix -= first;
18697 }
18698 else
18699 fragp->fr_fix -= second;
252b5132
RH
18700 }
18701}
18702
18703#ifdef OBJ_ELF
18704
18705/* This function is called after the relocs have been generated.
18706 We've been storing mips16 text labels as odd. Here we convert them
18707 back to even for the convenience of the debugger. */
18708
18709void
17a2f251 18710mips_frob_file_after_relocs (void)
252b5132
RH
18711{
18712 asymbol **syms;
18713 unsigned int count, i;
18714
f43abd2b 18715 if (!IS_ELF)
252b5132
RH
18716 return;
18717
18718 syms = bfd_get_outsymbols (stdoutput);
18719 count = bfd_get_symcount (stdoutput);
18720 for (i = 0; i < count; i++, syms++)
df58fc94
RS
18721 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18722 && ((*syms)->value & 1) != 0)
18723 {
18724 (*syms)->value &= ~1;
18725 /* If the symbol has an odd size, it was probably computed
18726 incorrectly, so adjust that as well. */
18727 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18728 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18729 }
252b5132
RH
18730}
18731
18732#endif
18733
a1facbec
MR
18734/* This function is called whenever a label is defined, including fake
18735 labels instantiated off the dot special symbol. It is used when
18736 handling branch delays; if a branch has a label, we assume we cannot
18737 move it. This also bumps the value of the symbol by 1 in compressed
18738 code. */
252b5132 18739
e1b47bd5 18740static void
a1facbec 18741mips_record_label (symbolS *sym)
252b5132 18742{
a8dbcb85 18743 segment_info_type *si = seg_info (now_seg);
252b5132
RH
18744 struct insn_label_list *l;
18745
18746 if (free_insn_labels == NULL)
18747 l = (struct insn_label_list *) xmalloc (sizeof *l);
18748 else
18749 {
18750 l = free_insn_labels;
18751 free_insn_labels = l->next;
18752 }
18753
18754 l->label = sym;
a8dbcb85
TS
18755 l->next = si->label_list;
18756 si->label_list = l;
a1facbec 18757}
07a53e5c 18758
a1facbec
MR
18759/* This function is called as tc_frob_label() whenever a label is defined
18760 and adds a DWARF-2 record we only want for true labels. */
18761
18762void
18763mips_define_label (symbolS *sym)
18764{
18765 mips_record_label (sym);
07a53e5c
RH
18766#ifdef OBJ_ELF
18767 dwarf2_emit_label (sym);
18768#endif
252b5132 18769}
e1b47bd5
RS
18770
18771/* This function is called by tc_new_dot_label whenever a new dot symbol
18772 is defined. */
18773
18774void
18775mips_add_dot_label (symbolS *sym)
18776{
18777 mips_record_label (sym);
18778 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18779 mips_compressed_mark_label (sym);
18780}
252b5132
RH
18781\f
18782#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
18783
18784/* Some special processing for a MIPS ELF file. */
18785
18786void
17a2f251 18787mips_elf_final_processing (void)
252b5132
RH
18788{
18789 /* Write out the register information. */
316f5878 18790 if (mips_abi != N64_ABI)
252b5132
RH
18791 {
18792 Elf32_RegInfo s;
18793
18794 s.ri_gprmask = mips_gprmask;
18795 s.ri_cprmask[0] = mips_cprmask[0];
18796 s.ri_cprmask[1] = mips_cprmask[1];
18797 s.ri_cprmask[2] = mips_cprmask[2];
18798 s.ri_cprmask[3] = mips_cprmask[3];
18799 /* The gp_value field is set by the MIPS ELF backend. */
18800
18801 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18802 ((Elf32_External_RegInfo *)
18803 mips_regmask_frag));
18804 }
18805 else
18806 {
18807 Elf64_Internal_RegInfo s;
18808
18809 s.ri_gprmask = mips_gprmask;
18810 s.ri_pad = 0;
18811 s.ri_cprmask[0] = mips_cprmask[0];
18812 s.ri_cprmask[1] = mips_cprmask[1];
18813 s.ri_cprmask[2] = mips_cprmask[2];
18814 s.ri_cprmask[3] = mips_cprmask[3];
18815 /* The gp_value field is set by the MIPS ELF backend. */
18816
18817 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18818 ((Elf64_External_RegInfo *)
18819 mips_regmask_frag));
18820 }
18821
18822 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18823 sort of BFD interface for this. */
18824 if (mips_any_noreorder)
18825 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18826 if (mips_pic != NO_PIC)
143d77c5 18827 {
8b828383 18828 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
18829 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18830 }
18831 if (mips_abicalls)
18832 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 18833
b015e599
AP
18834 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
18835 defined at present; this might need to change in future. */
a4672219
TS
18836 if (file_ase_mips16)
18837 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
18838 if (file_ase_micromips)
18839 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
1f25f5d3
CD
18840#if 0 /* XXX FIXME */
18841 if (file_ase_mips3d)
18842 elf_elfheader (stdoutput)->e_flags |= ???;
18843#endif
deec1734
CD
18844 if (file_ase_mdmx)
18845 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 18846
bdaaa2e1 18847 /* Set the MIPS ELF ABI flags. */
316f5878 18848 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 18849 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 18850 else if (mips_abi == O64_ABI)
252b5132 18851 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 18852 else if (mips_abi == EABI_ABI)
252b5132 18853 {
316f5878 18854 if (!file_mips_gp32)
252b5132
RH
18855 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18856 else
18857 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18858 }
316f5878 18859 else if (mips_abi == N32_ABI)
be00bddd
TS
18860 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18861
c9914766 18862 /* Nothing to do for N64_ABI. */
252b5132
RH
18863
18864 if (mips_32bitmode)
18865 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08
TS
18866
18867#if 0 /* XXX FIXME */
18868 /* 32 bit code with 64 bit FP registers. */
18869 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18870 elf_elfheader (stdoutput)->e_flags |= ???;
18871#endif
252b5132
RH
18872}
18873
18874#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
18875\f
beae10d5 18876typedef struct proc {
9b2f1d35
EC
18877 symbolS *func_sym;
18878 symbolS *func_end_sym;
beae10d5
KH
18879 unsigned long reg_mask;
18880 unsigned long reg_offset;
18881 unsigned long fpreg_mask;
18882 unsigned long fpreg_offset;
18883 unsigned long frame_offset;
18884 unsigned long frame_reg;
18885 unsigned long pc_reg;
18886} procS;
252b5132
RH
18887
18888static procS cur_proc;
18889static procS *cur_proc_ptr;
18890static int numprocs;
18891
df58fc94
RS
18892/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
18893 as "2", and a normal nop as "0". */
18894
18895#define NOP_OPCODE_MIPS 0
18896#define NOP_OPCODE_MIPS16 1
18897#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
18898
18899char
18900mips_nop_opcode (void)
18901{
df58fc94
RS
18902 if (seg_info (now_seg)->tc_segment_info_data.micromips)
18903 return NOP_OPCODE_MICROMIPS;
18904 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18905 return NOP_OPCODE_MIPS16;
18906 else
18907 return NOP_OPCODE_MIPS;
742a56fe
RS
18908}
18909
df58fc94
RS
18910/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
18911 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 18912
0a9ef439 18913void
17a2f251 18914mips_handle_align (fragS *fragp)
a19d8eb0 18915{
df58fc94 18916 char nop_opcode;
742a56fe 18917 char *p;
c67a084a
NC
18918 int bytes, size, excess;
18919 valueT opcode;
742a56fe 18920
0a9ef439
RH
18921 if (fragp->fr_type != rs_align_code)
18922 return;
18923
742a56fe 18924 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18925 nop_opcode = *p;
18926 switch (nop_opcode)
a19d8eb0 18927 {
df58fc94
RS
18928 case NOP_OPCODE_MICROMIPS:
18929 opcode = micromips_nop32_insn.insn_opcode;
18930 size = 4;
18931 break;
18932 case NOP_OPCODE_MIPS16:
c67a084a
NC
18933 opcode = mips16_nop_insn.insn_opcode;
18934 size = 2;
df58fc94
RS
18935 break;
18936 case NOP_OPCODE_MIPS:
18937 default:
c67a084a
NC
18938 opcode = nop_insn.insn_opcode;
18939 size = 4;
df58fc94 18940 break;
c67a084a 18941 }
a19d8eb0 18942
c67a084a
NC
18943 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18944 excess = bytes % size;
df58fc94
RS
18945
18946 /* Handle the leading part if we're not inserting a whole number of
18947 instructions, and make it the end of the fixed part of the frag.
18948 Try to fit in a short microMIPS NOP if applicable and possible,
18949 and use zeroes otherwise. */
18950 gas_assert (excess < 4);
18951 fragp->fr_fix += excess;
18952 switch (excess)
c67a084a 18953 {
df58fc94
RS
18954 case 3:
18955 *p++ = '\0';
18956 /* Fall through. */
18957 case 2:
18958 if (nop_opcode == NOP_OPCODE_MICROMIPS)
18959 {
4d68580a 18960 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
18961 break;
18962 }
18963 *p++ = '\0';
18964 /* Fall through. */
18965 case 1:
18966 *p++ = '\0';
18967 /* Fall through. */
18968 case 0:
18969 break;
a19d8eb0 18970 }
c67a084a
NC
18971
18972 md_number_to_chars (p, opcode, size);
18973 fragp->fr_var = size;
a19d8eb0
CP
18974}
18975
252b5132 18976static void
17a2f251 18977md_obj_begin (void)
252b5132
RH
18978{
18979}
18980
18981static void
17a2f251 18982md_obj_end (void)
252b5132 18983{
54f4ddb3 18984 /* Check for premature end, nesting errors, etc. */
252b5132 18985 if (cur_proc_ptr)
9a41af64 18986 as_warn (_("missing .end at end of assembly"));
252b5132
RH
18987}
18988
18989static long
17a2f251 18990get_number (void)
252b5132
RH
18991{
18992 int negative = 0;
18993 long val = 0;
18994
18995 if (*input_line_pointer == '-')
18996 {
18997 ++input_line_pointer;
18998 negative = 1;
18999 }
3882b010 19000 if (!ISDIGIT (*input_line_pointer))
956cd1d6 19001 as_bad (_("expected simple number"));
252b5132
RH
19002 if (input_line_pointer[0] == '0')
19003 {
19004 if (input_line_pointer[1] == 'x')
19005 {
19006 input_line_pointer += 2;
3882b010 19007 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
19008 {
19009 val <<= 4;
19010 val |= hex_value (*input_line_pointer++);
19011 }
19012 return negative ? -val : val;
19013 }
19014 else
19015 {
19016 ++input_line_pointer;
3882b010 19017 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19018 {
19019 val <<= 3;
19020 val |= *input_line_pointer++ - '0';
19021 }
19022 return negative ? -val : val;
19023 }
19024 }
3882b010 19025 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19026 {
19027 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19028 *input_line_pointer, *input_line_pointer);
956cd1d6 19029 as_warn (_("invalid number"));
252b5132
RH
19030 return -1;
19031 }
3882b010 19032 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19033 {
19034 val *= 10;
19035 val += *input_line_pointer++ - '0';
19036 }
19037 return negative ? -val : val;
19038}
19039
19040/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19041 is an initial number which is the ECOFF file index. In the non-ECOFF
19042 case .file implies DWARF-2. */
19043
19044static void
17a2f251 19045s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19046{
ecb4347a
DJ
19047 static int first_file_directive = 0;
19048
c5dd6aab
DJ
19049 if (ECOFF_DEBUGGING)
19050 {
19051 get_number ();
19052 s_app_file (0);
19053 }
19054 else
ecb4347a
DJ
19055 {
19056 char *filename;
19057
19058 filename = dwarf2_directive_file (0);
19059
19060 /* Versions of GCC up to 3.1 start files with a ".file"
19061 directive even for stabs output. Make sure that this
19062 ".file" is handled. Note that you need a version of GCC
19063 after 3.1 in order to support DWARF-2 on MIPS. */
19064 if (filename != NULL && ! first_file_directive)
19065 {
19066 (void) new_logical_line (filename, -1);
c04f5787 19067 s_app_file_string (filename, 0);
ecb4347a
DJ
19068 }
19069 first_file_directive = 1;
19070 }
c5dd6aab
DJ
19071}
19072
19073/* The .loc directive, implying DWARF-2. */
252b5132
RH
19074
19075static void
17a2f251 19076s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19077{
c5dd6aab
DJ
19078 if (!ECOFF_DEBUGGING)
19079 dwarf2_directive_loc (0);
252b5132
RH
19080}
19081
252b5132
RH
19082/* The .end directive. */
19083
19084static void
17a2f251 19085s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19086{
19087 symbolS *p;
252b5132 19088
7a621144
DJ
19089 /* Following functions need their own .frame and .cprestore directives. */
19090 mips_frame_reg_valid = 0;
19091 mips_cprestore_valid = 0;
19092
252b5132
RH
19093 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19094 {
19095 p = get_symbol ();
19096 demand_empty_rest_of_line ();
19097 }
19098 else
19099 p = NULL;
19100
14949570 19101 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19102 as_warn (_(".end not in text section"));
19103
19104 if (!cur_proc_ptr)
19105 {
19106 as_warn (_(".end directive without a preceding .ent directive."));
19107 demand_empty_rest_of_line ();
19108 return;
19109 }
19110
19111 if (p != NULL)
19112 {
9c2799c2 19113 gas_assert (S_GET_NAME (p));
9b2f1d35 19114 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 19115 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
19116
19117 if (debug_type == DEBUG_STABS)
19118 stabs_generate_asm_endfunc (S_GET_NAME (p),
19119 S_GET_NAME (p));
252b5132
RH
19120 }
19121 else
19122 as_warn (_(".end directive missing or unknown symbol"));
19123
2132e3a3 19124#ifdef OBJ_ELF
9b2f1d35
EC
19125 /* Create an expression to calculate the size of the function. */
19126 if (p && cur_proc_ptr)
19127 {
19128 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19129 expressionS *exp = xmalloc (sizeof (expressionS));
19130
19131 obj->size = exp;
19132 exp->X_op = O_subtract;
19133 exp->X_add_symbol = symbol_temp_new_now ();
19134 exp->X_op_symbol = p;
19135 exp->X_add_number = 0;
19136
19137 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19138 }
19139
ecb4347a 19140 /* Generate a .pdr section. */
f43abd2b 19141 if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19142 {
19143 segT saved_seg = now_seg;
19144 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19145 expressionS exp;
19146 char *fragp;
252b5132 19147
252b5132 19148#ifdef md_flush_pending_output
ecb4347a 19149 md_flush_pending_output ();
252b5132
RH
19150#endif
19151
9c2799c2 19152 gas_assert (pdr_seg);
ecb4347a 19153 subseg_set (pdr_seg, 0);
252b5132 19154
ecb4347a
DJ
19155 /* Write the symbol. */
19156 exp.X_op = O_symbol;
19157 exp.X_add_symbol = p;
19158 exp.X_add_number = 0;
19159 emit_expr (&exp, 4);
252b5132 19160
ecb4347a 19161 fragp = frag_more (7 * 4);
252b5132 19162
17a2f251
TS
19163 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19164 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19165 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19166 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19167 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19168 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19169 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19170
ecb4347a
DJ
19171 subseg_set (saved_seg, saved_subseg);
19172 }
19173#endif /* OBJ_ELF */
252b5132
RH
19174
19175 cur_proc_ptr = NULL;
19176}
19177
19178/* The .aent and .ent directives. */
19179
19180static void
17a2f251 19181s_mips_ent (int aent)
252b5132 19182{
252b5132 19183 symbolS *symbolP;
252b5132
RH
19184
19185 symbolP = get_symbol ();
19186 if (*input_line_pointer == ',')
f9419b05 19187 ++input_line_pointer;
252b5132 19188 SKIP_WHITESPACE ();
3882b010 19189 if (ISDIGIT (*input_line_pointer)
d9a62219 19190 || *input_line_pointer == '-')
874e8986 19191 get_number ();
252b5132 19192
14949570 19193 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19194 as_warn (_(".ent or .aent not in text section."));
19195
19196 if (!aent && cur_proc_ptr)
9a41af64 19197 as_warn (_("missing .end"));
252b5132
RH
19198
19199 if (!aent)
19200 {
7a621144
DJ
19201 /* This function needs its own .frame and .cprestore directives. */
19202 mips_frame_reg_valid = 0;
19203 mips_cprestore_valid = 0;
19204
252b5132
RH
19205 cur_proc_ptr = &cur_proc;
19206 memset (cur_proc_ptr, '\0', sizeof (procS));
19207
9b2f1d35 19208 cur_proc_ptr->func_sym = symbolP;
252b5132 19209
f9419b05 19210 ++numprocs;
ecb4347a
DJ
19211
19212 if (debug_type == DEBUG_STABS)
19213 stabs_generate_asm_func (S_GET_NAME (symbolP),
19214 S_GET_NAME (symbolP));
252b5132
RH
19215 }
19216
7c0fc524
MR
19217 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19218
252b5132
RH
19219 demand_empty_rest_of_line ();
19220}
19221
19222/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19223 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19224 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19225 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19226 symbol table (in the mdebug section). */
19227
19228static void
17a2f251 19229s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19230{
ecb4347a 19231#ifdef OBJ_ELF
f43abd2b 19232 if (IS_ELF && !ECOFF_DEBUGGING)
ecb4347a
DJ
19233 {
19234 long val;
252b5132 19235
ecb4347a
DJ
19236 if (cur_proc_ptr == (procS *) NULL)
19237 {
19238 as_warn (_(".frame outside of .ent"));
19239 demand_empty_rest_of_line ();
19240 return;
19241 }
252b5132 19242
ecb4347a
DJ
19243 cur_proc_ptr->frame_reg = tc_get_register (1);
19244
19245 SKIP_WHITESPACE ();
19246 if (*input_line_pointer++ != ','
19247 || get_absolute_expression_and_terminator (&val) != ',')
19248 {
19249 as_warn (_("Bad .frame directive"));
19250 --input_line_pointer;
19251 demand_empty_rest_of_line ();
19252 return;
19253 }
252b5132 19254
ecb4347a
DJ
19255 cur_proc_ptr->frame_offset = val;
19256 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19257
252b5132 19258 demand_empty_rest_of_line ();
252b5132 19259 }
ecb4347a
DJ
19260 else
19261#endif /* OBJ_ELF */
19262 s_ignore (ignore);
252b5132
RH
19263}
19264
bdaaa2e1
KH
19265/* The .fmask and .mask directives. If the mdebug section is present
19266 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19267 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19268 information correctly. We can't use the ecoff routines because they
252b5132
RH
19269 make reference to the ecoff symbol table (in the mdebug section). */
19270
19271static void
17a2f251 19272s_mips_mask (int reg_type)
252b5132 19273{
ecb4347a 19274#ifdef OBJ_ELF
f43abd2b 19275 if (IS_ELF && !ECOFF_DEBUGGING)
252b5132 19276 {
ecb4347a 19277 long mask, off;
252b5132 19278
ecb4347a
DJ
19279 if (cur_proc_ptr == (procS *) NULL)
19280 {
19281 as_warn (_(".mask/.fmask outside of .ent"));
19282 demand_empty_rest_of_line ();
19283 return;
19284 }
252b5132 19285
ecb4347a
DJ
19286 if (get_absolute_expression_and_terminator (&mask) != ',')
19287 {
19288 as_warn (_("Bad .mask/.fmask directive"));
19289 --input_line_pointer;
19290 demand_empty_rest_of_line ();
19291 return;
19292 }
252b5132 19293
ecb4347a
DJ
19294 off = get_absolute_expression ();
19295
19296 if (reg_type == 'F')
19297 {
19298 cur_proc_ptr->fpreg_mask = mask;
19299 cur_proc_ptr->fpreg_offset = off;
19300 }
19301 else
19302 {
19303 cur_proc_ptr->reg_mask = mask;
19304 cur_proc_ptr->reg_offset = off;
19305 }
19306
19307 demand_empty_rest_of_line ();
252b5132
RH
19308 }
19309 else
ecb4347a
DJ
19310#endif /* OBJ_ELF */
19311 s_ignore (reg_type);
252b5132
RH
19312}
19313
316f5878
RS
19314/* A table describing all the processors gas knows about. Names are
19315 matched in the order listed.
e7af610e 19316
316f5878
RS
19317 To ease comparison, please keep this table in the same order as
19318 gcc's mips_cpu_info_table[]. */
e972090a
NC
19319static const struct mips_cpu_info mips_cpu_info_table[] =
19320{
316f5878 19321 /* Entries for generic ISAs */
ad3fea08
TS
19322 { "mips1", MIPS_CPU_IS_ISA, ISA_MIPS1, CPU_R3000 },
19323 { "mips2", MIPS_CPU_IS_ISA, ISA_MIPS2, CPU_R6000 },
19324 { "mips3", MIPS_CPU_IS_ISA, ISA_MIPS3, CPU_R4000 },
19325 { "mips4", MIPS_CPU_IS_ISA, ISA_MIPS4, CPU_R8000 },
19326 { "mips5", MIPS_CPU_IS_ISA, ISA_MIPS5, CPU_MIPS5 },
19327 { "mips32", MIPS_CPU_IS_ISA, ISA_MIPS32, CPU_MIPS32 },
19328 { "mips32r2", MIPS_CPU_IS_ISA, ISA_MIPS32R2, CPU_MIPS32R2 },
19329 { "mips64", MIPS_CPU_IS_ISA, ISA_MIPS64, CPU_MIPS64 },
19330 { "mips64r2", MIPS_CPU_IS_ISA, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
19331
19332 /* MIPS I */
ad3fea08
TS
19333 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
19334 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
19335 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19336
19337 /* MIPS II */
ad3fea08 19338 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19339
19340 /* MIPS III */
ad3fea08
TS
19341 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
19342 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
19343 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
19344 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
19345 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
19346 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
19347 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
19348 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
19349 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
19350 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
19351 { "orion", 0, ISA_MIPS3, CPU_R4600 },
19352 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
e407c74b 19353 { "r5900", 0, ISA_MIPS3, CPU_R5900 },
b15591bb
AN
19354 /* ST Microelectronics Loongson 2E and 2F cores */
19355 { "loongson2e", 0, ISA_MIPS3, CPU_LOONGSON_2E },
19356 { "loongson2f", 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19357
19358 /* MIPS IV */
ad3fea08
TS
19359 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
19360 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
19361 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
3aa3176b
TS
19362 { "r14000", 0, ISA_MIPS4, CPU_R14000 },
19363 { "r16000", 0, ISA_MIPS4, CPU_R16000 },
ad3fea08
TS
19364 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
19365 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
19366 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
19367 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
19368 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
19369 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
19370 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
19371 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
19372 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
19373 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19374
19375 /* MIPS 32 */
ad3fea08
TS
19376 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
19377 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
19378 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
19379 { "4ksc", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
19380
19381 /* MIPS 32 Release 2 */
19382 { "4kec", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19383 { "4kem", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19384 { "4kep", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19385 { "4ksd", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19386 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19387 { "m4kp", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
b5503c7b
MR
19388 { "m14k", MIPS_CPU_ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19389 { "m14kc", MIPS_CPU_ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
7a795ef4
MR
19390 { "m14ke", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19391 ISA_MIPS32R2, CPU_MIPS32R2 },
19392 { "m14kec", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19393 ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19394 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19395 { "24kf2_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19396 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19397 { "24kf1_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19398 /* Deprecated forms of the above. */
19399 { "24kfx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19400 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19401 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
ad3fea08 19402 { "24kec", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19403 { "24kef2_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19404 { "24kef", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19405 { "24kef1_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19406 /* Deprecated forms of the above. */
19407 { "24kefx", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
65263ce3 19408 { "24kex", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19409 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
a360e743
TS
19410 { "34kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19411 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19412 { "34kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19413 ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
19414 { "34kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19415 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19416 { "34kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19417 ISA_MIPS32R2, CPU_MIPS32R2 },
19418 /* Deprecated forms of the above. */
19419 { "34kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19420 ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
19421 { "34kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19422 ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4
SL
19423 /* 34Kn is a 34kc without DSP. */
19424 { "34kn", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19425 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
19426 { "74kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19427 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19428 { "74kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19429 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19430 { "74kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19431 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19432 { "74kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19433 ISA_MIPS32R2, CPU_MIPS32R2 },
19434 { "74kf3_2", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19435 ISA_MIPS32R2, CPU_MIPS32R2 },
19436 /* Deprecated forms of the above. */
19437 { "74kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19438 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19439 { "74kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19440 ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a
SL
19441 /* 1004K cores are multiprocessor versions of the 34K. */
19442 { "1004kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19443 ISA_MIPS32R2, CPU_MIPS32R2 },
19444 { "1004kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19445 ISA_MIPS32R2, CPU_MIPS32R2 },
19446 { "1004kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19447 ISA_MIPS32R2, CPU_MIPS32R2 },
19448 { "1004kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19449 ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 19450
316f5878 19451 /* MIPS 64 */
ad3fea08
TS
19452 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
19453 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
19454 { "20kc", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
7764b395 19455 { "25kf", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19456
c7a23324 19457 /* Broadcom SB-1 CPU core */
65263ce3
TS
19458 { "sb1", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19459 ISA_MIPS64, CPU_SB1 },
1e85aad8
JW
19460 /* Broadcom SB-1A CPU core */
19461 { "sb1a", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19462 ISA_MIPS64, CPU_SB1 },
d051516a
NC
19463
19464 { "loongson3a", 0, ISA_MIPS64, CPU_LOONGSON_3A },
e7af610e 19465
ed163775
MR
19466 /* MIPS 64 Release 2 */
19467
967344c6
AN
19468 /* Cavium Networks Octeon CPU core */
19469 { "octeon", 0, ISA_MIPS64R2, CPU_OCTEON },
dd6a37e7 19470 { "octeon+", 0, ISA_MIPS64R2, CPU_OCTEONP },
432233b3 19471 { "octeon2", 0, ISA_MIPS64R2, CPU_OCTEON2 },
967344c6 19472
52b6b6b9
JM
19473 /* RMI Xlr */
19474 { "xlr", 0, ISA_MIPS64, CPU_XLR },
19475
55a36193
MK
19476 /* Broadcom XLP.
19477 XLP is mostly like XLR, with the prominent exception that it is
19478 MIPS64R2 rather than MIPS64. */
19479 { "xlp", 0, ISA_MIPS64R2, CPU_XLR },
19480
316f5878
RS
19481 /* End marker */
19482 { NULL, 0, 0, 0 }
19483};
e7af610e 19484
84ea6cf2 19485
316f5878
RS
19486/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19487 with a final "000" replaced by "k". Ignore case.
e7af610e 19488
316f5878 19489 Note: this function is shared between GCC and GAS. */
c6c98b38 19490
b34976b6 19491static bfd_boolean
17a2f251 19492mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19493{
19494 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19495 given++, canonical++;
19496
19497 return ((*given == 0 && *canonical == 0)
19498 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19499}
19500
19501
19502/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19503 CPU name. We've traditionally allowed a lot of variation here.
19504
19505 Note: this function is shared between GCC and GAS. */
19506
b34976b6 19507static bfd_boolean
17a2f251 19508mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19509{
19510 /* First see if the name matches exactly, or with a final "000"
19511 turned into "k". */
19512 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19513 return TRUE;
316f5878
RS
19514
19515 /* If not, try comparing based on numerical designation alone.
19516 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19517 if (TOLOWER (*given) == 'r')
19518 given++;
19519 if (!ISDIGIT (*given))
b34976b6 19520 return FALSE;
316f5878
RS
19521
19522 /* Skip over some well-known prefixes in the canonical name,
19523 hoping to find a number there too. */
19524 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19525 canonical += 2;
19526 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19527 canonical += 2;
19528 else if (TOLOWER (canonical[0]) == 'r')
19529 canonical += 1;
19530
19531 return mips_strict_matching_cpu_name_p (canonical, given);
19532}
19533
19534
19535/* Parse an option that takes the name of a processor as its argument.
19536 OPTION is the name of the option and CPU_STRING is the argument.
19537 Return the corresponding processor enumeration if the CPU_STRING is
19538 recognized, otherwise report an error and return null.
19539
19540 A similar function exists in GCC. */
e7af610e
NC
19541
19542static const struct mips_cpu_info *
17a2f251 19543mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19544{
316f5878 19545 const struct mips_cpu_info *p;
e7af610e 19546
316f5878
RS
19547 /* 'from-abi' selects the most compatible architecture for the given
19548 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19549 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19550 version. Look first at the -mgp options, if given, otherwise base
19551 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19552
316f5878
RS
19553 Treat NO_ABI like the EABIs. One reason to do this is that the
19554 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19555 architecture. This code picks MIPS I for 'mips' and MIPS III for
19556 'mips64', just as we did in the days before 'from-abi'. */
19557 if (strcasecmp (cpu_string, "from-abi") == 0)
19558 {
19559 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19560 return mips_cpu_info_from_isa (ISA_MIPS1);
19561
19562 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19563 return mips_cpu_info_from_isa (ISA_MIPS3);
19564
19565 if (file_mips_gp32 >= 0)
19566 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19567
19568 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19569 ? ISA_MIPS3
19570 : ISA_MIPS1);
19571 }
19572
19573 /* 'default' has traditionally been a no-op. Probably not very useful. */
19574 if (strcasecmp (cpu_string, "default") == 0)
19575 return 0;
19576
19577 for (p = mips_cpu_info_table; p->name != 0; p++)
19578 if (mips_matching_cpu_name_p (p->name, cpu_string))
19579 return p;
19580
20203fb9 19581 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
316f5878 19582 return 0;
e7af610e
NC
19583}
19584
316f5878
RS
19585/* Return the canonical processor information for ISA (a member of the
19586 ISA_MIPS* enumeration). */
19587
e7af610e 19588static const struct mips_cpu_info *
17a2f251 19589mips_cpu_info_from_isa (int isa)
e7af610e
NC
19590{
19591 int i;
19592
19593 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19594 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19595 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19596 return (&mips_cpu_info_table[i]);
19597
e972090a 19598 return NULL;
e7af610e 19599}
fef14a42
TS
19600
19601static const struct mips_cpu_info *
17a2f251 19602mips_cpu_info_from_arch (int arch)
fef14a42
TS
19603{
19604 int i;
19605
19606 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19607 if (arch == mips_cpu_info_table[i].cpu)
19608 return (&mips_cpu_info_table[i]);
19609
19610 return NULL;
19611}
316f5878
RS
19612\f
19613static void
17a2f251 19614show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19615{
19616 if (*first_p)
19617 {
19618 fprintf (stream, "%24s", "");
19619 *col_p = 24;
19620 }
19621 else
19622 {
19623 fprintf (stream, ", ");
19624 *col_p += 2;
19625 }
e7af610e 19626
316f5878
RS
19627 if (*col_p + strlen (string) > 72)
19628 {
19629 fprintf (stream, "\n%24s", "");
19630 *col_p = 24;
19631 }
19632
19633 fprintf (stream, "%s", string);
19634 *col_p += strlen (string);
19635
19636 *first_p = 0;
19637}
19638
19639void
17a2f251 19640md_show_usage (FILE *stream)
e7af610e 19641{
316f5878
RS
19642 int column, first;
19643 size_t i;
19644
19645 fprintf (stream, _("\
19646MIPS options:\n\
316f5878
RS
19647-EB generate big endian output\n\
19648-EL generate little endian output\n\
19649-g, -g2 do not remove unneeded NOPs or swap branches\n\
19650-G NUM allow referencing objects up to NUM bytes\n\
19651 implicitly with the gp register [default 8]\n"));
19652 fprintf (stream, _("\
19653-mips1 generate MIPS ISA I instructions\n\
19654-mips2 generate MIPS ISA II instructions\n\
19655-mips3 generate MIPS ISA III instructions\n\
19656-mips4 generate MIPS ISA IV instructions\n\
19657-mips5 generate MIPS ISA V instructions\n\
19658-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 19659-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 19660-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 19661-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
19662-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19663
19664 first = 1;
e7af610e
NC
19665
19666 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
19667 show (stream, mips_cpu_info_table[i].name, &column, &first);
19668 show (stream, "from-abi", &column, &first);
19669 fputc ('\n', stream);
e7af610e 19670
316f5878
RS
19671 fprintf (stream, _("\
19672-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19673-no-mCPU don't generate code specific to CPU.\n\
19674 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19675
19676 first = 1;
19677
19678 show (stream, "3900", &column, &first);
19679 show (stream, "4010", &column, &first);
19680 show (stream, "4100", &column, &first);
19681 show (stream, "4650", &column, &first);
19682 fputc ('\n', stream);
19683
19684 fprintf (stream, _("\
19685-mips16 generate mips16 instructions\n\
19686-no-mips16 do not generate mips16 instructions\n"));
19687 fprintf (stream, _("\
df58fc94
RS
19688-mmicromips generate microMIPS instructions\n\
19689-mno-micromips do not generate microMIPS instructions\n"));
19690 fprintf (stream, _("\
e16bfa71
TS
19691-msmartmips generate smartmips instructions\n\
19692-mno-smartmips do not generate smartmips instructions\n"));
19693 fprintf (stream, _("\
74cd071d
CF
19694-mdsp generate DSP instructions\n\
19695-mno-dsp do not generate DSP instructions\n"));
19696 fprintf (stream, _("\
8b082fb1
TS
19697-mdspr2 generate DSP R2 instructions\n\
19698-mno-dspr2 do not generate DSP R2 instructions\n"));
19699 fprintf (stream, _("\
ef2e4d86
CF
19700-mmt generate MT instructions\n\
19701-mno-mt do not generate MT instructions\n"));
19702 fprintf (stream, _("\
dec0624d
MR
19703-mmcu generate MCU instructions\n\
19704-mno-mcu do not generate MCU instructions\n"));
19705 fprintf (stream, _("\
b015e599
AP
19706-mvirt generate Virtualization instructions\n\
19707-mno-virt do not generate Virtualization instructions\n"));
19708 fprintf (stream, _("\
c67a084a
NC
19709-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19710-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 19711-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 19712-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 19713-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 19714-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
19715-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19716-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 19717-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
19718-O0 remove unneeded NOPs, do not swap branches\n\
19719-O remove unneeded NOPs and swap branches\n\
316f5878
RS
19720--trap, --no-break trap exception on div by 0 and mult overflow\n\
19721--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
19722 fprintf (stream, _("\
19723-mhard-float allow floating-point instructions\n\
19724-msoft-float do not allow floating-point instructions\n\
19725-msingle-float only allow 32-bit floating-point operations\n\
19726-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
19727--[no-]construct-floats [dis]allow floating point values to be constructed\n"
19728 ));
316f5878
RS
19729#ifdef OBJ_ELF
19730 fprintf (stream, _("\
19731-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 19732-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 19733-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 19734-non_shared do not generate code that can operate with DSOs\n\
316f5878 19735-xgot assume a 32 bit GOT\n\
dcd410fe 19736-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 19737-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 19738 position dependent (non shared) code\n\
316f5878
RS
19739-mabi=ABI create ABI conformant object file for:\n"));
19740
19741 first = 1;
19742
19743 show (stream, "32", &column, &first);
19744 show (stream, "o64", &column, &first);
19745 show (stream, "n32", &column, &first);
19746 show (stream, "64", &column, &first);
19747 show (stream, "eabi", &column, &first);
19748
19749 fputc ('\n', stream);
19750
19751 fprintf (stream, _("\
19752-32 create o32 ABI object file (default)\n\
19753-n32 create n32 ABI object file\n\
19754-64 create 64 ABI object file\n"));
19755#endif
e7af610e 19756}
14e777e0 19757
1575952e 19758#ifdef TE_IRIX
14e777e0 19759enum dwarf2_format
413a266c 19760mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 19761{
369943fe 19762 if (HAVE_64BIT_SYMBOLS)
1575952e 19763 return dwarf2_format_64bit_irix;
14e777e0
KB
19764 else
19765 return dwarf2_format_32bit;
19766}
1575952e 19767#endif
73369e65
EC
19768
19769int
19770mips_dwarf2_addr_size (void)
19771{
6b6b3450 19772 if (HAVE_64BIT_OBJECTS)
73369e65 19773 return 8;
73369e65
EC
19774 else
19775 return 4;
19776}
5862107c
EC
19777
19778/* Standard calling conventions leave the CFA at SP on entry. */
19779void
19780mips_cfi_frame_initial_instructions (void)
19781{
19782 cfi_add_CFA_def_cfa_register (SP);
19783}
19784
707bfff6
TS
19785int
19786tc_mips_regname_to_dw2regnum (char *regname)
19787{
19788 unsigned int regnum = -1;
19789 unsigned int reg;
19790
19791 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19792 regnum = reg;
19793
19794 return regnum;
19795}