maintenance_expand_symtabs leaks a cleanup
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
81912461 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
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);
1371static void s_cpadd (int);
1372static void s_insn (int);
1373static void md_obj_begin (void);
1374static void md_obj_end (void);
1375static void s_mips_ent (int);
1376static void s_mips_end (int);
1377static void s_mips_frame (int);
1378static void s_mips_mask (int reg_type);
1379static void s_mips_stab (int);
1380static void s_mips_weakext (int);
1381static void s_mips_file (int);
1382static void s_mips_loc (int);
1383static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1384static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 1385static int validate_mips_insn (const struct mips_opcode *);
df58fc94
RS
1386static int validate_micromips_insn (const struct mips_opcode *);
1387static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1388static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
e7af610e
NC
1389
1390/* Table and functions used to map between CPU/ISA names, and
1391 ISA levels, and CPU numbers. */
1392
e972090a
NC
1393struct mips_cpu_info
1394{
e7af610e 1395 const char *name; /* CPU or ISA name. */
ad3fea08 1396 int flags; /* ASEs available, or ISA flag. */
e7af610e
NC
1397 int isa; /* ISA level. */
1398 int cpu; /* CPU number (default CPU if ISA). */
1399};
1400
ad3fea08
TS
1401#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1402#define MIPS_CPU_ASE_SMARTMIPS 0x0002 /* CPU implements SmartMIPS ASE */
1403#define MIPS_CPU_ASE_DSP 0x0004 /* CPU implements DSP ASE */
1404#define MIPS_CPU_ASE_MT 0x0008 /* CPU implements MT ASE */
1405#define MIPS_CPU_ASE_MIPS3D 0x0010 /* CPU implements MIPS-3D ASE */
1406#define MIPS_CPU_ASE_MDMX 0x0020 /* CPU implements MDMX ASE */
8b082fb1 1407#define MIPS_CPU_ASE_DSPR2 0x0040 /* CPU implements DSP R2 ASE */
dec0624d 1408#define MIPS_CPU_ASE_MCU 0x0080 /* CPU implements MCU ASE */
b015e599 1409#define MIPS_CPU_ASE_VIRT 0x0100 /* CPU implements Virtualization ASE */
ad3fea08 1410
17a2f251
TS
1411static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1412static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1413static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
1414\f
1415/* Pseudo-op table.
1416
1417 The following pseudo-ops from the Kane and Heinrich MIPS book
1418 should be defined here, but are currently unsupported: .alias,
1419 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1420
1421 The following pseudo-ops from the Kane and Heinrich MIPS book are
1422 specific to the type of debugging information being generated, and
1423 should be defined by the object format: .aent, .begin, .bend,
1424 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1425 .vreg.
1426
1427 The following pseudo-ops from the Kane and Heinrich MIPS book are
1428 not MIPS CPU specific, but are also not specific to the object file
1429 format. This file is probably the best place to define them, but
d84bcf09 1430 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1431
e972090a
NC
1432static const pseudo_typeS mips_pseudo_table[] =
1433{
beae10d5 1434 /* MIPS specific pseudo-ops. */
252b5132
RH
1435 {"option", s_option, 0},
1436 {"set", s_mipsset, 0},
1437 {"rdata", s_change_sec, 'r'},
1438 {"sdata", s_change_sec, 's'},
1439 {"livereg", s_ignore, 0},
1440 {"abicalls", s_abicalls, 0},
1441 {"cpload", s_cpload, 0},
6478892d
TS
1442 {"cpsetup", s_cpsetup, 0},
1443 {"cplocal", s_cplocal, 0},
252b5132 1444 {"cprestore", s_cprestore, 0},
6478892d 1445 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1446 {"dtprelword", s_dtprelword, 0},
1447 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1448 {"tprelword", s_tprelword, 0},
1449 {"tpreldword", s_tpreldword, 0},
6478892d 1450 {"gpvalue", s_gpvalue, 0},
252b5132 1451 {"gpword", s_gpword, 0},
10181a0d 1452 {"gpdword", s_gpdword, 0},
252b5132
RH
1453 {"cpadd", s_cpadd, 0},
1454 {"insn", s_insn, 0},
1455
beae10d5 1456 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1457 chips. */
38a57ae7 1458 {"asciiz", stringer, 8 + 1},
252b5132
RH
1459 {"bss", s_change_sec, 'b'},
1460 {"err", s_err, 0},
1461 {"half", s_cons, 1},
1462 {"dword", s_cons, 3},
1463 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1464 {"origin", s_org, 0},
1465 {"repeat", s_rept, 0},
252b5132 1466
998b3c36
MR
1467 /* For MIPS this is non-standard, but we define it for consistency. */
1468 {"sbss", s_change_sec, 'B'},
1469
beae10d5 1470 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1471 here for one reason or another. */
1472 {"align", s_align, 0},
1473 {"byte", s_cons, 0},
1474 {"data", s_change_sec, 'd'},
1475 {"double", s_float_cons, 'd'},
1476 {"float", s_float_cons, 'f'},
1477 {"globl", s_mips_globl, 0},
1478 {"global", s_mips_globl, 0},
1479 {"hword", s_cons, 1},
1480 {"int", s_cons, 2},
1481 {"long", s_cons, 2},
1482 {"octa", s_cons, 4},
1483 {"quad", s_cons, 3},
cca86cc8 1484 {"section", s_change_section, 0},
252b5132
RH
1485 {"short", s_cons, 1},
1486 {"single", s_float_cons, 'f'},
754e2bb9 1487 {"stabd", s_mips_stab, 'd'},
252b5132 1488 {"stabn", s_mips_stab, 'n'},
754e2bb9 1489 {"stabs", s_mips_stab, 's'},
252b5132
RH
1490 {"text", s_change_sec, 't'},
1491 {"word", s_cons, 2},
add56521 1492
add56521 1493 { "extern", ecoff_directive_extern, 0},
add56521 1494
43841e91 1495 { NULL, NULL, 0 },
252b5132
RH
1496};
1497
e972090a
NC
1498static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1499{
beae10d5
KH
1500 /* These pseudo-ops should be defined by the object file format.
1501 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1502 {"aent", s_mips_ent, 1},
1503 {"bgnb", s_ignore, 0},
1504 {"end", s_mips_end, 0},
1505 {"endb", s_ignore, 0},
1506 {"ent", s_mips_ent, 0},
c5dd6aab 1507 {"file", s_mips_file, 0},
252b5132
RH
1508 {"fmask", s_mips_mask, 'F'},
1509 {"frame", s_mips_frame, 0},
c5dd6aab 1510 {"loc", s_mips_loc, 0},
252b5132
RH
1511 {"mask", s_mips_mask, 'R'},
1512 {"verstamp", s_ignore, 0},
43841e91 1513 { NULL, NULL, 0 },
252b5132
RH
1514};
1515
3ae8dd8d
MR
1516/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1517 purpose of the `.dc.a' internal pseudo-op. */
1518
1519int
1520mips_address_bytes (void)
1521{
1522 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1523}
1524
17a2f251 1525extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1526
1527void
17a2f251 1528mips_pop_insert (void)
252b5132
RH
1529{
1530 pop_insert (mips_pseudo_table);
1531 if (! ECOFF_DEBUGGING)
1532 pop_insert (mips_nonecoff_pseudo_table);
1533}
1534\f
1535/* Symbols labelling the current insn. */
1536
e972090a
NC
1537struct insn_label_list
1538{
252b5132
RH
1539 struct insn_label_list *next;
1540 symbolS *label;
1541};
1542
252b5132 1543static struct insn_label_list *free_insn_labels;
742a56fe 1544#define label_list tc_segment_info_data.labels
252b5132 1545
17a2f251 1546static void mips_clear_insn_labels (void);
df58fc94
RS
1547static void mips_mark_labels (void);
1548static void mips_compressed_mark_labels (void);
252b5132
RH
1549
1550static inline void
17a2f251 1551mips_clear_insn_labels (void)
252b5132
RH
1552{
1553 register struct insn_label_list **pl;
a8dbcb85 1554 segment_info_type *si;
252b5132 1555
a8dbcb85
TS
1556 if (now_seg)
1557 {
1558 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1559 ;
1560
1561 si = seg_info (now_seg);
1562 *pl = si->label_list;
1563 si->label_list = NULL;
1564 }
252b5132 1565}
a8dbcb85 1566
df58fc94
RS
1567/* Mark instruction labels in MIPS16/microMIPS mode. */
1568
1569static inline void
1570mips_mark_labels (void)
1571{
1572 if (HAVE_CODE_COMPRESSION)
1573 mips_compressed_mark_labels ();
1574}
252b5132
RH
1575\f
1576static char *expr_end;
1577
1578/* Expressions which appear in instructions. These are set by
1579 mips_ip. */
1580
1581static expressionS imm_expr;
5f74bc13 1582static expressionS imm2_expr;
252b5132
RH
1583static expressionS offset_expr;
1584
1585/* Relocs associated with imm_expr and offset_expr. */
1586
f6688943
TS
1587static bfd_reloc_code_real_type imm_reloc[3]
1588 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1589static bfd_reloc_code_real_type offset_reloc[3]
1590 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1591
df58fc94
RS
1592/* This is set to the resulting size of the instruction to be produced
1593 by mips16_ip if an explicit extension is used or by mips_ip if an
1594 explicit size is supplied. */
252b5132 1595
df58fc94 1596static unsigned int forced_insn_length;
252b5132 1597
e1b47bd5
RS
1598/* True if we are assembling an instruction. All dot symbols defined during
1599 this time should be treated as code labels. */
1600
1601static bfd_boolean mips_assembling_insn;
1602
7ed4a06a 1603#ifdef OBJ_ELF
ecb4347a
DJ
1604/* The pdr segment for per procedure frame/regmask info. Not used for
1605 ECOFF debugging. */
252b5132
RH
1606
1607static segT pdr_seg;
7ed4a06a 1608#endif
252b5132 1609
e013f690
TS
1610/* The default target format to use. */
1611
aeffff67
RS
1612#if defined (TE_FreeBSD)
1613#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1614#elif defined (TE_TMIPS)
1615#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1616#else
1617#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1618#endif
1619
e013f690 1620const char *
17a2f251 1621mips_target_format (void)
e013f690
TS
1622{
1623 switch (OUTPUT_FLAVOR)
1624 {
e013f690
TS
1625 case bfd_target_ecoff_flavour:
1626 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1627 case bfd_target_coff_flavour:
1628 return "pe-mips";
1629 case bfd_target_elf_flavour:
0a44bf69
RS
1630#ifdef TE_VXWORKS
1631 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1632 return (target_big_endian
1633 ? "elf32-bigmips-vxworks"
1634 : "elf32-littlemips-vxworks");
1635#endif
e013f690 1636 return (target_big_endian
cfe86eaa 1637 ? (HAVE_64BIT_OBJECTS
aeffff67 1638 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1639 : (HAVE_NEWABI
aeffff67
RS
1640 ? ELF_TARGET ("elf32-n", "big")
1641 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1642 : (HAVE_64BIT_OBJECTS
aeffff67 1643 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1644 : (HAVE_NEWABI
aeffff67
RS
1645 ? ELF_TARGET ("elf32-n", "little")
1646 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1647 default:
1648 abort ();
1649 return NULL;
1650 }
1651}
1652
df58fc94
RS
1653/* Return the length of a microMIPS instruction in bytes. If bits of
1654 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1655 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1656 major opcode) will require further modifications to the opcode
1657 table. */
1658
1659static inline unsigned int
1660micromips_insn_length (const struct mips_opcode *mo)
1661{
1662 return (mo->mask >> 16) == 0 ? 2 : 4;
1663}
1664
5c04167a
RS
1665/* Return the length of MIPS16 instruction OPCODE. */
1666
1667static inline unsigned int
1668mips16_opcode_length (unsigned long opcode)
1669{
1670 return (opcode >> 16) == 0 ? 2 : 4;
1671}
1672
1e915849
RS
1673/* Return the length of instruction INSN. */
1674
1675static inline unsigned int
1676insn_length (const struct mips_cl_insn *insn)
1677{
df58fc94
RS
1678 if (mips_opts.micromips)
1679 return micromips_insn_length (insn->insn_mo);
1680 else if (mips_opts.mips16)
5c04167a 1681 return mips16_opcode_length (insn->insn_opcode);
df58fc94 1682 else
1e915849 1683 return 4;
1e915849
RS
1684}
1685
1686/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1687
1688static void
1689create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1690{
1691 size_t i;
1692
1693 insn->insn_mo = mo;
1e915849
RS
1694 insn->insn_opcode = mo->match;
1695 insn->frag = NULL;
1696 insn->where = 0;
1697 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1698 insn->fixp[i] = NULL;
1699 insn->fixed_p = (mips_opts.noreorder > 0);
1700 insn->noreorder_p = (mips_opts.noreorder > 0);
1701 insn->mips16_absolute_jump_p = 0;
15be625d 1702 insn->complete_p = 0;
e407c74b 1703 insn->cleared_p = 0;
1e915849
RS
1704}
1705
df58fc94 1706/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
1707
1708static void
df58fc94 1709mips_record_compressed_mode (void)
742a56fe
RS
1710{
1711 segment_info_type *si;
1712
1713 si = seg_info (now_seg);
1714 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1715 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
1716 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
1717 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
1718}
1719
4d68580a
RS
1720/* Read a standard MIPS instruction from BUF. */
1721
1722static unsigned long
1723read_insn (char *buf)
1724{
1725 if (target_big_endian)
1726 return bfd_getb32 ((bfd_byte *) buf);
1727 else
1728 return bfd_getl32 ((bfd_byte *) buf);
1729}
1730
1731/* Write standard MIPS instruction INSN to BUF. Return a pointer to
1732 the next byte. */
1733
1734static char *
1735write_insn (char *buf, unsigned int insn)
1736{
1737 md_number_to_chars (buf, insn, 4);
1738 return buf + 4;
1739}
1740
1741/* Read a microMIPS or MIPS16 opcode from BUF, given that it
1742 has length LENGTH. */
1743
1744static unsigned long
1745read_compressed_insn (char *buf, unsigned int length)
1746{
1747 unsigned long insn;
1748 unsigned int i;
1749
1750 insn = 0;
1751 for (i = 0; i < length; i += 2)
1752 {
1753 insn <<= 16;
1754 if (target_big_endian)
1755 insn |= bfd_getb16 ((char *) buf);
1756 else
1757 insn |= bfd_getl16 ((char *) buf);
1758 buf += 2;
1759 }
1760 return insn;
1761}
1762
5c04167a
RS
1763/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
1764 instruction is LENGTH bytes long. Return a pointer to the next byte. */
1765
1766static char *
1767write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
1768{
1769 unsigned int i;
1770
1771 for (i = 0; i < length; i += 2)
1772 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
1773 return buf + length;
1774}
1775
1e915849
RS
1776/* Install INSN at the location specified by its "frag" and "where" fields. */
1777
1778static void
1779install_insn (const struct mips_cl_insn *insn)
1780{
1781 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
1782 if (HAVE_CODE_COMPRESSION)
1783 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 1784 else
4d68580a 1785 write_insn (f, insn->insn_opcode);
df58fc94 1786 mips_record_compressed_mode ();
1e915849
RS
1787}
1788
1789/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1790 and install the opcode in the new location. */
1791
1792static void
1793move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1794{
1795 size_t i;
1796
1797 insn->frag = frag;
1798 insn->where = where;
1799 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1800 if (insn->fixp[i] != NULL)
1801 {
1802 insn->fixp[i]->fx_frag = frag;
1803 insn->fixp[i]->fx_where = where;
1804 }
1805 install_insn (insn);
1806}
1807
1808/* Add INSN to the end of the output. */
1809
1810static void
1811add_fixed_insn (struct mips_cl_insn *insn)
1812{
1813 char *f = frag_more (insn_length (insn));
1814 move_insn (insn, frag_now, f - frag_now->fr_literal);
1815}
1816
1817/* Start a variant frag and move INSN to the start of the variant part,
1818 marking it as fixed. The other arguments are as for frag_var. */
1819
1820static void
1821add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1822 relax_substateT subtype, symbolS *symbol, offsetT offset)
1823{
1824 frag_grow (max_chars);
1825 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1826 insn->fixed_p = 1;
1827 frag_var (rs_machine_dependent, max_chars, var,
1828 subtype, symbol, offset, NULL);
1829}
1830
1831/* Insert N copies of INSN into the history buffer, starting at
1832 position FIRST. Neither FIRST nor N need to be clipped. */
1833
1834static void
1835insert_into_history (unsigned int first, unsigned int n,
1836 const struct mips_cl_insn *insn)
1837{
1838 if (mips_relax.sequence != 2)
1839 {
1840 unsigned int i;
1841
1842 for (i = ARRAY_SIZE (history); i-- > first;)
1843 if (i >= first + n)
1844 history[i] = history[i - n];
1845 else
1846 history[i] = *insn;
1847 }
1848}
1849
71400594
RS
1850/* Initialize vr4120_conflicts. There is a bit of duplication here:
1851 the idea is to make it obvious at a glance that each errata is
1852 included. */
1853
1854static void
1855init_vr4120_conflicts (void)
1856{
1857#define CONFLICT(FIRST, SECOND) \
1858 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1859
1860 /* Errata 21 - [D]DIV[U] after [D]MACC */
1861 CONFLICT (MACC, DIV);
1862 CONFLICT (DMACC, DIV);
1863
1864 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1865 CONFLICT (DMULT, DMULT);
1866 CONFLICT (DMULT, DMACC);
1867 CONFLICT (DMACC, DMULT);
1868 CONFLICT (DMACC, DMACC);
1869
1870 /* Errata 24 - MT{LO,HI} after [D]MACC */
1871 CONFLICT (MACC, MTHILO);
1872 CONFLICT (DMACC, MTHILO);
1873
1874 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1875 instruction is executed immediately after a MACC or DMACC
1876 instruction, the result of [either instruction] is incorrect." */
1877 CONFLICT (MACC, MULT);
1878 CONFLICT (MACC, DMULT);
1879 CONFLICT (DMACC, MULT);
1880 CONFLICT (DMACC, DMULT);
1881
1882 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1883 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1884 DDIV or DDIVU instruction, the result of the MACC or
1885 DMACC instruction is incorrect.". */
1886 CONFLICT (DMULT, MACC);
1887 CONFLICT (DMULT, DMACC);
1888 CONFLICT (DIV, MACC);
1889 CONFLICT (DIV, DMACC);
1890
1891#undef CONFLICT
1892}
1893
707bfff6
TS
1894struct regname {
1895 const char *name;
1896 unsigned int num;
1897};
1898
1899#define RTYPE_MASK 0x1ff00
1900#define RTYPE_NUM 0x00100
1901#define RTYPE_FPU 0x00200
1902#define RTYPE_FCC 0x00400
1903#define RTYPE_VEC 0x00800
1904#define RTYPE_GP 0x01000
1905#define RTYPE_CP0 0x02000
1906#define RTYPE_PC 0x04000
1907#define RTYPE_ACC 0x08000
1908#define RTYPE_CCC 0x10000
1909#define RNUM_MASK 0x000ff
1910#define RWARN 0x80000
1911
1912#define GENERIC_REGISTER_NUMBERS \
1913 {"$0", RTYPE_NUM | 0}, \
1914 {"$1", RTYPE_NUM | 1}, \
1915 {"$2", RTYPE_NUM | 2}, \
1916 {"$3", RTYPE_NUM | 3}, \
1917 {"$4", RTYPE_NUM | 4}, \
1918 {"$5", RTYPE_NUM | 5}, \
1919 {"$6", RTYPE_NUM | 6}, \
1920 {"$7", RTYPE_NUM | 7}, \
1921 {"$8", RTYPE_NUM | 8}, \
1922 {"$9", RTYPE_NUM | 9}, \
1923 {"$10", RTYPE_NUM | 10}, \
1924 {"$11", RTYPE_NUM | 11}, \
1925 {"$12", RTYPE_NUM | 12}, \
1926 {"$13", RTYPE_NUM | 13}, \
1927 {"$14", RTYPE_NUM | 14}, \
1928 {"$15", RTYPE_NUM | 15}, \
1929 {"$16", RTYPE_NUM | 16}, \
1930 {"$17", RTYPE_NUM | 17}, \
1931 {"$18", RTYPE_NUM | 18}, \
1932 {"$19", RTYPE_NUM | 19}, \
1933 {"$20", RTYPE_NUM | 20}, \
1934 {"$21", RTYPE_NUM | 21}, \
1935 {"$22", RTYPE_NUM | 22}, \
1936 {"$23", RTYPE_NUM | 23}, \
1937 {"$24", RTYPE_NUM | 24}, \
1938 {"$25", RTYPE_NUM | 25}, \
1939 {"$26", RTYPE_NUM | 26}, \
1940 {"$27", RTYPE_NUM | 27}, \
1941 {"$28", RTYPE_NUM | 28}, \
1942 {"$29", RTYPE_NUM | 29}, \
1943 {"$30", RTYPE_NUM | 30}, \
1944 {"$31", RTYPE_NUM | 31}
1945
1946#define FPU_REGISTER_NAMES \
1947 {"$f0", RTYPE_FPU | 0}, \
1948 {"$f1", RTYPE_FPU | 1}, \
1949 {"$f2", RTYPE_FPU | 2}, \
1950 {"$f3", RTYPE_FPU | 3}, \
1951 {"$f4", RTYPE_FPU | 4}, \
1952 {"$f5", RTYPE_FPU | 5}, \
1953 {"$f6", RTYPE_FPU | 6}, \
1954 {"$f7", RTYPE_FPU | 7}, \
1955 {"$f8", RTYPE_FPU | 8}, \
1956 {"$f9", RTYPE_FPU | 9}, \
1957 {"$f10", RTYPE_FPU | 10}, \
1958 {"$f11", RTYPE_FPU | 11}, \
1959 {"$f12", RTYPE_FPU | 12}, \
1960 {"$f13", RTYPE_FPU | 13}, \
1961 {"$f14", RTYPE_FPU | 14}, \
1962 {"$f15", RTYPE_FPU | 15}, \
1963 {"$f16", RTYPE_FPU | 16}, \
1964 {"$f17", RTYPE_FPU | 17}, \
1965 {"$f18", RTYPE_FPU | 18}, \
1966 {"$f19", RTYPE_FPU | 19}, \
1967 {"$f20", RTYPE_FPU | 20}, \
1968 {"$f21", RTYPE_FPU | 21}, \
1969 {"$f22", RTYPE_FPU | 22}, \
1970 {"$f23", RTYPE_FPU | 23}, \
1971 {"$f24", RTYPE_FPU | 24}, \
1972 {"$f25", RTYPE_FPU | 25}, \
1973 {"$f26", RTYPE_FPU | 26}, \
1974 {"$f27", RTYPE_FPU | 27}, \
1975 {"$f28", RTYPE_FPU | 28}, \
1976 {"$f29", RTYPE_FPU | 29}, \
1977 {"$f30", RTYPE_FPU | 30}, \
1978 {"$f31", RTYPE_FPU | 31}
1979
1980#define FPU_CONDITION_CODE_NAMES \
1981 {"$fcc0", RTYPE_FCC | 0}, \
1982 {"$fcc1", RTYPE_FCC | 1}, \
1983 {"$fcc2", RTYPE_FCC | 2}, \
1984 {"$fcc3", RTYPE_FCC | 3}, \
1985 {"$fcc4", RTYPE_FCC | 4}, \
1986 {"$fcc5", RTYPE_FCC | 5}, \
1987 {"$fcc6", RTYPE_FCC | 6}, \
1988 {"$fcc7", RTYPE_FCC | 7}
1989
1990#define COPROC_CONDITION_CODE_NAMES \
1991 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
1992 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
1993 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
1994 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
1995 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
1996 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
1997 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
1998 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
1999
2000#define N32N64_SYMBOLIC_REGISTER_NAMES \
2001 {"$a4", RTYPE_GP | 8}, \
2002 {"$a5", RTYPE_GP | 9}, \
2003 {"$a6", RTYPE_GP | 10}, \
2004 {"$a7", RTYPE_GP | 11}, \
2005 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2006 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2007 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2008 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2009 {"$t0", RTYPE_GP | 12}, \
2010 {"$t1", RTYPE_GP | 13}, \
2011 {"$t2", RTYPE_GP | 14}, \
2012 {"$t3", RTYPE_GP | 15}
2013
2014#define O32_SYMBOLIC_REGISTER_NAMES \
2015 {"$t0", RTYPE_GP | 8}, \
2016 {"$t1", RTYPE_GP | 9}, \
2017 {"$t2", RTYPE_GP | 10}, \
2018 {"$t3", RTYPE_GP | 11}, \
2019 {"$t4", RTYPE_GP | 12}, \
2020 {"$t5", RTYPE_GP | 13}, \
2021 {"$t6", RTYPE_GP | 14}, \
2022 {"$t7", RTYPE_GP | 15}, \
2023 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2024 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2025 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2026 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2027
2028/* Remaining symbolic register names */
2029#define SYMBOLIC_REGISTER_NAMES \
2030 {"$zero", RTYPE_GP | 0}, \
2031 {"$at", RTYPE_GP | 1}, \
2032 {"$AT", RTYPE_GP | 1}, \
2033 {"$v0", RTYPE_GP | 2}, \
2034 {"$v1", RTYPE_GP | 3}, \
2035 {"$a0", RTYPE_GP | 4}, \
2036 {"$a1", RTYPE_GP | 5}, \
2037 {"$a2", RTYPE_GP | 6}, \
2038 {"$a3", RTYPE_GP | 7}, \
2039 {"$s0", RTYPE_GP | 16}, \
2040 {"$s1", RTYPE_GP | 17}, \
2041 {"$s2", RTYPE_GP | 18}, \
2042 {"$s3", RTYPE_GP | 19}, \
2043 {"$s4", RTYPE_GP | 20}, \
2044 {"$s5", RTYPE_GP | 21}, \
2045 {"$s6", RTYPE_GP | 22}, \
2046 {"$s7", RTYPE_GP | 23}, \
2047 {"$t8", RTYPE_GP | 24}, \
2048 {"$t9", RTYPE_GP | 25}, \
2049 {"$k0", RTYPE_GP | 26}, \
2050 {"$kt0", RTYPE_GP | 26}, \
2051 {"$k1", RTYPE_GP | 27}, \
2052 {"$kt1", RTYPE_GP | 27}, \
2053 {"$gp", RTYPE_GP | 28}, \
2054 {"$sp", RTYPE_GP | 29}, \
2055 {"$s8", RTYPE_GP | 30}, \
2056 {"$fp", RTYPE_GP | 30}, \
2057 {"$ra", RTYPE_GP | 31}
2058
2059#define MIPS16_SPECIAL_REGISTER_NAMES \
2060 {"$pc", RTYPE_PC | 0}
2061
2062#define MDMX_VECTOR_REGISTER_NAMES \
2063 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2064 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2065 {"$v2", RTYPE_VEC | 2}, \
2066 {"$v3", RTYPE_VEC | 3}, \
2067 {"$v4", RTYPE_VEC | 4}, \
2068 {"$v5", RTYPE_VEC | 5}, \
2069 {"$v6", RTYPE_VEC | 6}, \
2070 {"$v7", RTYPE_VEC | 7}, \
2071 {"$v8", RTYPE_VEC | 8}, \
2072 {"$v9", RTYPE_VEC | 9}, \
2073 {"$v10", RTYPE_VEC | 10}, \
2074 {"$v11", RTYPE_VEC | 11}, \
2075 {"$v12", RTYPE_VEC | 12}, \
2076 {"$v13", RTYPE_VEC | 13}, \
2077 {"$v14", RTYPE_VEC | 14}, \
2078 {"$v15", RTYPE_VEC | 15}, \
2079 {"$v16", RTYPE_VEC | 16}, \
2080 {"$v17", RTYPE_VEC | 17}, \
2081 {"$v18", RTYPE_VEC | 18}, \
2082 {"$v19", RTYPE_VEC | 19}, \
2083 {"$v20", RTYPE_VEC | 20}, \
2084 {"$v21", RTYPE_VEC | 21}, \
2085 {"$v22", RTYPE_VEC | 22}, \
2086 {"$v23", RTYPE_VEC | 23}, \
2087 {"$v24", RTYPE_VEC | 24}, \
2088 {"$v25", RTYPE_VEC | 25}, \
2089 {"$v26", RTYPE_VEC | 26}, \
2090 {"$v27", RTYPE_VEC | 27}, \
2091 {"$v28", RTYPE_VEC | 28}, \
2092 {"$v29", RTYPE_VEC | 29}, \
2093 {"$v30", RTYPE_VEC | 30}, \
2094 {"$v31", RTYPE_VEC | 31}
2095
2096#define MIPS_DSP_ACCUMULATOR_NAMES \
2097 {"$ac0", RTYPE_ACC | 0}, \
2098 {"$ac1", RTYPE_ACC | 1}, \
2099 {"$ac2", RTYPE_ACC | 2}, \
2100 {"$ac3", RTYPE_ACC | 3}
2101
2102static const struct regname reg_names[] = {
2103 GENERIC_REGISTER_NUMBERS,
2104 FPU_REGISTER_NAMES,
2105 FPU_CONDITION_CODE_NAMES,
2106 COPROC_CONDITION_CODE_NAMES,
2107
2108 /* The $txx registers depends on the abi,
2109 these will be added later into the symbol table from
2110 one of the tables below once mips_abi is set after
2111 parsing of arguments from the command line. */
2112 SYMBOLIC_REGISTER_NAMES,
2113
2114 MIPS16_SPECIAL_REGISTER_NAMES,
2115 MDMX_VECTOR_REGISTER_NAMES,
2116 MIPS_DSP_ACCUMULATOR_NAMES,
2117 {0, 0}
2118};
2119
2120static const struct regname reg_names_o32[] = {
2121 O32_SYMBOLIC_REGISTER_NAMES,
2122 {0, 0}
2123};
2124
2125static const struct regname reg_names_n32n64[] = {
2126 N32N64_SYMBOLIC_REGISTER_NAMES,
2127 {0, 0}
2128};
2129
df58fc94
RS
2130/* Check if S points at a valid register specifier according to TYPES.
2131 If so, then return 1, advance S to consume the specifier and store
2132 the register's number in REGNOP, otherwise return 0. */
2133
707bfff6
TS
2134static int
2135reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2136{
2137 symbolS *symbolP;
2138 char *e;
2139 char save_c;
2140 int reg = -1;
2141
2142 /* Find end of name. */
2143 e = *s;
2144 if (is_name_beginner (*e))
2145 ++e;
2146 while (is_part_of_name (*e))
2147 ++e;
2148
2149 /* Terminate name. */
2150 save_c = *e;
2151 *e = '\0';
2152
2153 /* Look for a register symbol. */
2154 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2155 {
2156 int r = S_GET_VALUE (symbolP);
2157 if (r & types)
2158 reg = r & RNUM_MASK;
2159 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2160 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
2161 reg = (r & RNUM_MASK) - 2;
2162 }
2163 /* Else see if this is a register defined in an itbl entry. */
2164 else if ((types & RTYPE_GP) && itbl_have_entries)
2165 {
2166 char *n = *s;
2167 unsigned long r;
2168
2169 if (*n == '$')
2170 ++n;
2171 if (itbl_get_reg_val (n, &r))
2172 reg = r & RNUM_MASK;
2173 }
2174
2175 /* Advance to next token if a register was recognised. */
2176 if (reg >= 0)
2177 *s = e;
2178 else if (types & RWARN)
20203fb9 2179 as_warn (_("Unrecognized register name `%s'"), *s);
707bfff6
TS
2180
2181 *e = save_c;
2182 if (regnop)
2183 *regnop = reg;
2184 return reg >= 0;
2185}
2186
df58fc94
RS
2187/* Check if S points at a valid register list according to TYPES.
2188 If so, then return 1, advance S to consume the list and store
2189 the registers present on the list as a bitmask of ones in REGLISTP,
2190 otherwise return 0. A valid list comprises a comma-separated
2191 enumeration of valid single registers and/or dash-separated
2192 contiguous register ranges as determined by their numbers.
2193
2194 As a special exception if one of s0-s7 registers is specified as
2195 the range's lower delimiter and s8 (fp) is its upper one, then no
2196 registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2309ddf2 2197 are selected; they have to be listed separately if needed. */
df58fc94
RS
2198
2199static int
2200reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2201{
2202 unsigned int reglist = 0;
2203 unsigned int lastregno;
2204 bfd_boolean ok = TRUE;
2205 unsigned int regmask;
2309ddf2 2206 char *s_endlist = *s;
df58fc94 2207 char *s_reset = *s;
2309ddf2 2208 unsigned int regno;
df58fc94
RS
2209
2210 while (reg_lookup (s, types, &regno))
2211 {
2212 lastregno = regno;
2213 if (**s == '-')
2214 {
2215 (*s)++;
2216 ok = reg_lookup (s, types, &lastregno);
2217 if (ok && lastregno < regno)
2218 ok = FALSE;
2219 if (!ok)
2220 break;
2221 }
2222
2223 if (lastregno == FP && regno >= S0 && regno <= S7)
2224 {
2225 lastregno = S7;
2226 reglist |= 1 << FP;
2227 }
2228 regmask = 1 << lastregno;
2229 regmask = (regmask << 1) - 1;
2230 regmask ^= (1 << regno) - 1;
2231 reglist |= regmask;
2232
2309ddf2 2233 s_endlist = *s;
df58fc94
RS
2234 if (**s != ',')
2235 break;
2236 (*s)++;
2237 }
2238
2239 if (ok)
2309ddf2 2240 *s = s_endlist;
df58fc94
RS
2241 else
2242 *s = s_reset;
2243 if (reglistp)
2244 *reglistp = reglist;
2245 return ok && reglist != 0;
2246}
2247
037b32b9 2248/* Return TRUE if opcode MO is valid on the currently selected ISA and
f79e2745 2249 architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
2250
2251static bfd_boolean
f79e2745 2252is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
2253{
2254 int isa = mips_opts.isa;
2255 int fp_s, fp_d;
2256
2257 if (mips_opts.ase_mdmx)
2258 isa |= INSN_MDMX;
2259 if (mips_opts.ase_dsp)
2260 isa |= INSN_DSP;
2261 if (mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
2262 isa |= INSN_DSP64;
2263 if (mips_opts.ase_dspr2)
2264 isa |= INSN_DSPR2;
2265 if (mips_opts.ase_mt)
2266 isa |= INSN_MT;
2267 if (mips_opts.ase_mips3d)
2268 isa |= INSN_MIPS3D;
2269 if (mips_opts.ase_smartmips)
2270 isa |= INSN_SMARTMIPS;
dec0624d
MR
2271 if (mips_opts.ase_mcu)
2272 isa |= INSN_MCU;
b015e599
AP
2273 if (mips_opts.ase_virt)
2274 isa |= INSN_VIRT;
2275 if (mips_opts.ase_virt && ISA_SUPPORTS_VIRT64_ASE)
2276 isa |= INSN_VIRT64;
037b32b9 2277
35d0a169 2278 if (!opcode_is_member (mo, isa, mips_opts.arch))
037b32b9
AN
2279 return FALSE;
2280
2281 /* Check whether the instruction or macro requires single-precision or
2282 double-precision floating-point support. Note that this information is
2283 stored differently in the opcode table for insns and macros. */
2284 if (mo->pinfo == INSN_MACRO)
2285 {
2286 fp_s = mo->pinfo2 & INSN2_M_FP_S;
2287 fp_d = mo->pinfo2 & INSN2_M_FP_D;
2288 }
2289 else
2290 {
2291 fp_s = mo->pinfo & FP_S;
2292 fp_d = mo->pinfo & FP_D;
2293 }
2294
2295 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2296 return FALSE;
2297
2298 if (fp_s && mips_opts.soft_float)
2299 return FALSE;
2300
2301 return TRUE;
2302}
2303
2304/* Return TRUE if the MIPS16 opcode MO is valid on the currently
2305 selected ISA and architecture. */
2306
2307static bfd_boolean
2308is_opcode_valid_16 (const struct mips_opcode *mo)
2309{
35d0a169 2310 return opcode_is_member (mo, mips_opts.isa, mips_opts.arch);
037b32b9
AN
2311}
2312
df58fc94
RS
2313/* Return TRUE if the size of the microMIPS opcode MO matches one
2314 explicitly requested. Always TRUE in the standard MIPS mode. */
2315
2316static bfd_boolean
2317is_size_valid (const struct mips_opcode *mo)
2318{
2319 if (!mips_opts.micromips)
2320 return TRUE;
2321
2322 if (!forced_insn_length)
2323 return TRUE;
2324 if (mo->pinfo == INSN_MACRO)
2325 return FALSE;
2326 return forced_insn_length == micromips_insn_length (mo);
2327}
2328
2329/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
2330 of the preceding instruction. Always TRUE in the standard MIPS mode.
2331
2332 We don't accept macros in 16-bit delay slots to avoid a case where
2333 a macro expansion fails because it relies on a preceding 32-bit real
2334 instruction to have matched and does not handle the operands correctly.
2335 The only macros that may expand to 16-bit instructions are JAL that
2336 cannot be placed in a delay slot anyway, and corner cases of BALIGN
2337 and BGT (that likewise cannot be placed in a delay slot) that decay to
2338 a NOP. In all these cases the macros precede any corresponding real
2339 instruction definitions in the opcode table, so they will match in the
2340 second pass where the size of the delay slot is ignored and therefore
2341 produce correct code. */
df58fc94
RS
2342
2343static bfd_boolean
2344is_delay_slot_valid (const struct mips_opcode *mo)
2345{
2346 if (!mips_opts.micromips)
2347 return TRUE;
2348
2349 if (mo->pinfo == INSN_MACRO)
c06dec14 2350 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
2351 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2352 && micromips_insn_length (mo) != 4)
2353 return FALSE;
2354 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2355 && micromips_insn_length (mo) != 2)
2356 return FALSE;
2357
2358 return TRUE;
2359}
2360
707bfff6
TS
2361/* This function is called once, at assembler startup time. It should set up
2362 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 2363
252b5132 2364void
17a2f251 2365md_begin (void)
252b5132 2366{
3994f87e 2367 const char *retval = NULL;
156c2f8b 2368 int i = 0;
252b5132 2369 int broken = 0;
1f25f5d3 2370
0a44bf69
RS
2371 if (mips_pic != NO_PIC)
2372 {
2373 if (g_switch_seen && g_switch_value != 0)
2374 as_bad (_("-G may not be used in position-independent code"));
2375 g_switch_value = 0;
2376 }
2377
fef14a42 2378 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
2379 as_warn (_("Could not set architecture and machine"));
2380
252b5132
RH
2381 op_hash = hash_new ();
2382
2383 for (i = 0; i < NUMOPCODES;)
2384 {
2385 const char *name = mips_opcodes[i].name;
2386
17a2f251 2387 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
2388 if (retval != NULL)
2389 {
2390 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2391 mips_opcodes[i].name, retval);
2392 /* Probably a memory allocation problem? Give up now. */
2393 as_fatal (_("Broken assembler. No assembly attempted."));
2394 }
2395 do
2396 {
2397 if (mips_opcodes[i].pinfo != INSN_MACRO)
2398 {
2399 if (!validate_mips_insn (&mips_opcodes[i]))
2400 broken = 1;
1e915849
RS
2401 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2402 {
2403 create_insn (&nop_insn, mips_opcodes + i);
c67a084a
NC
2404 if (mips_fix_loongson2f_nop)
2405 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
1e915849
RS
2406 nop_insn.fixed_p = 1;
2407 }
252b5132
RH
2408 }
2409 ++i;
2410 }
2411 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2412 }
2413
2414 mips16_op_hash = hash_new ();
2415
2416 i = 0;
2417 while (i < bfd_mips16_num_opcodes)
2418 {
2419 const char *name = mips16_opcodes[i].name;
2420
17a2f251 2421 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
2422 if (retval != NULL)
2423 as_fatal (_("internal: can't hash `%s': %s"),
2424 mips16_opcodes[i].name, retval);
2425 do
2426 {
2427 if (mips16_opcodes[i].pinfo != INSN_MACRO
2428 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2429 != mips16_opcodes[i].match))
2430 {
2431 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2432 mips16_opcodes[i].name, mips16_opcodes[i].args);
2433 broken = 1;
2434 }
1e915849
RS
2435 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2436 {
2437 create_insn (&mips16_nop_insn, mips16_opcodes + i);
2438 mips16_nop_insn.fixed_p = 1;
2439 }
252b5132
RH
2440 ++i;
2441 }
2442 while (i < bfd_mips16_num_opcodes
2443 && strcmp (mips16_opcodes[i].name, name) == 0);
2444 }
2445
df58fc94
RS
2446 micromips_op_hash = hash_new ();
2447
2448 i = 0;
2449 while (i < bfd_micromips_num_opcodes)
2450 {
2451 const char *name = micromips_opcodes[i].name;
2452
2453 retval = hash_insert (micromips_op_hash, name,
2454 (void *) &micromips_opcodes[i]);
2455 if (retval != NULL)
2456 as_fatal (_("internal: can't hash `%s': %s"),
2457 micromips_opcodes[i].name, retval);
2458 do
2459 if (micromips_opcodes[i].pinfo != INSN_MACRO)
2460 {
2461 struct mips_cl_insn *micromips_nop_insn;
2462
2463 if (!validate_micromips_insn (&micromips_opcodes[i]))
2464 broken = 1;
2465
2466 if (micromips_insn_length (micromips_opcodes + i) == 2)
2467 micromips_nop_insn = &micromips_nop16_insn;
2468 else if (micromips_insn_length (micromips_opcodes + i) == 4)
2469 micromips_nop_insn = &micromips_nop32_insn;
2470 else
2471 continue;
2472
2473 if (micromips_nop_insn->insn_mo == NULL
2474 && strcmp (name, "nop") == 0)
2475 {
2476 create_insn (micromips_nop_insn, micromips_opcodes + i);
2477 micromips_nop_insn->fixed_p = 1;
2478 }
2479 }
2480 while (++i < bfd_micromips_num_opcodes
2481 && strcmp (micromips_opcodes[i].name, name) == 0);
2482 }
2483
252b5132
RH
2484 if (broken)
2485 as_fatal (_("Broken assembler. No assembly attempted."));
2486
2487 /* We add all the general register names to the symbol table. This
2488 helps us detect invalid uses of them. */
707bfff6
TS
2489 for (i = 0; reg_names[i].name; i++)
2490 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 2491 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
2492 &zero_address_frag));
2493 if (HAVE_NEWABI)
2494 for (i = 0; reg_names_n32n64[i].name; i++)
2495 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 2496 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 2497 &zero_address_frag));
707bfff6
TS
2498 else
2499 for (i = 0; reg_names_o32[i].name; i++)
2500 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 2501 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 2502 &zero_address_frag));
6047c971 2503
7d10b47d 2504 mips_no_prev_insn ();
252b5132
RH
2505
2506 mips_gprmask = 0;
2507 mips_cprmask[0] = 0;
2508 mips_cprmask[1] = 0;
2509 mips_cprmask[2] = 0;
2510 mips_cprmask[3] = 0;
2511
2512 /* set the default alignment for the text section (2**2) */
2513 record_alignment (text_section, 2);
2514
4d0d148d 2515 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 2516
707bfff6 2517#ifdef OBJ_ELF
f43abd2b 2518 if (IS_ELF)
252b5132 2519 {
0a44bf69
RS
2520 /* On a native system other than VxWorks, sections must be aligned
2521 to 16 byte boundaries. When configured for an embedded ELF
2522 target, we don't bother. */
c41e87e3
CF
2523 if (strncmp (TARGET_OS, "elf", 3) != 0
2524 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132
RH
2525 {
2526 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2527 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2528 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2529 }
2530
2531 /* Create a .reginfo section for register masks and a .mdebug
2532 section for debugging information. */
2533 {
2534 segT seg;
2535 subsegT subseg;
2536 flagword flags;
2537 segT sec;
2538
2539 seg = now_seg;
2540 subseg = now_subseg;
2541
2542 /* The ABI says this section should be loaded so that the
2543 running program can access it. However, we don't load it
2544 if we are configured for an embedded target */
2545 flags = SEC_READONLY | SEC_DATA;
c41e87e3 2546 if (strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
2547 flags |= SEC_ALLOC | SEC_LOAD;
2548
316f5878 2549 if (mips_abi != N64_ABI)
252b5132
RH
2550 {
2551 sec = subseg_new (".reginfo", (subsegT) 0);
2552
195325d2
TS
2553 bfd_set_section_flags (stdoutput, sec, flags);
2554 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 2555
252b5132 2556 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
252b5132
RH
2557 }
2558 else
2559 {
2560 /* The 64-bit ABI uses a .MIPS.options section rather than
2561 .reginfo section. */
2562 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
2563 bfd_set_section_flags (stdoutput, sec, flags);
2564 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 2565
252b5132
RH
2566 /* Set up the option header. */
2567 {
2568 Elf_Internal_Options opthdr;
2569 char *f;
2570
2571 opthdr.kind = ODK_REGINFO;
2572 opthdr.size = (sizeof (Elf_External_Options)
2573 + sizeof (Elf64_External_RegInfo));
2574 opthdr.section = 0;
2575 opthdr.info = 0;
2576 f = frag_more (sizeof (Elf_External_Options));
2577 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2578 (Elf_External_Options *) f);
2579
2580 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2581 }
252b5132
RH
2582 }
2583
2584 if (ECOFF_DEBUGGING)
2585 {
2586 sec = subseg_new (".mdebug", (subsegT) 0);
2587 (void) bfd_set_section_flags (stdoutput, sec,
2588 SEC_HAS_CONTENTS | SEC_READONLY);
2589 (void) bfd_set_section_alignment (stdoutput, sec, 2);
2590 }
f43abd2b 2591 else if (mips_flag_pdr)
ecb4347a
DJ
2592 {
2593 pdr_seg = subseg_new (".pdr", (subsegT) 0);
2594 (void) bfd_set_section_flags (stdoutput, pdr_seg,
2595 SEC_READONLY | SEC_RELOC
2596 | SEC_DEBUGGING);
2597 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2598 }
252b5132
RH
2599
2600 subseg_set (seg, subseg);
2601 }
2602 }
707bfff6 2603#endif /* OBJ_ELF */
252b5132
RH
2604
2605 if (! ECOFF_DEBUGGING)
2606 md_obj_begin ();
71400594
RS
2607
2608 if (mips_fix_vr4120)
2609 init_vr4120_conflicts ();
252b5132
RH
2610}
2611
2612void
17a2f251 2613md_mips_end (void)
252b5132 2614{
02b1ab82 2615 mips_emit_delays ();
252b5132
RH
2616 if (! ECOFF_DEBUGGING)
2617 md_obj_end ();
2618}
2619
2620void
17a2f251 2621md_assemble (char *str)
252b5132
RH
2622{
2623 struct mips_cl_insn insn;
f6688943
TS
2624 bfd_reloc_code_real_type unused_reloc[3]
2625 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
2626
2627 imm_expr.X_op = O_absent;
5f74bc13 2628 imm2_expr.X_op = O_absent;
252b5132 2629 offset_expr.X_op = O_absent;
f6688943
TS
2630 imm_reloc[0] = BFD_RELOC_UNUSED;
2631 imm_reloc[1] = BFD_RELOC_UNUSED;
2632 imm_reloc[2] = BFD_RELOC_UNUSED;
2633 offset_reloc[0] = BFD_RELOC_UNUSED;
2634 offset_reloc[1] = BFD_RELOC_UNUSED;
2635 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 2636
e1b47bd5
RS
2637 mips_mark_labels ();
2638 mips_assembling_insn = TRUE;
2639
252b5132
RH
2640 if (mips_opts.mips16)
2641 mips16_ip (str, &insn);
2642 else
2643 {
2644 mips_ip (str, &insn);
beae10d5
KH
2645 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2646 str, insn.insn_opcode));
252b5132
RH
2647 }
2648
2649 if (insn_error)
e1b47bd5
RS
2650 as_bad ("%s `%s'", insn_error, str);
2651 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 2652 {
584892a6 2653 macro_start ();
252b5132
RH
2654 if (mips_opts.mips16)
2655 mips16_macro (&insn);
2656 else
2657 macro (&insn);
584892a6 2658 macro_end ();
252b5132
RH
2659 }
2660 else
2661 {
2662 if (imm_expr.X_op != O_absent)
df58fc94 2663 append_insn (&insn, &imm_expr, imm_reloc, FALSE);
252b5132 2664 else if (offset_expr.X_op != O_absent)
df58fc94 2665 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 2666 else
df58fc94 2667 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 2668 }
e1b47bd5
RS
2669
2670 mips_assembling_insn = FALSE;
252b5132
RH
2671}
2672
738e5348
RS
2673/* Convenience functions for abstracting away the differences between
2674 MIPS16 and non-MIPS16 relocations. */
2675
2676static inline bfd_boolean
2677mips16_reloc_p (bfd_reloc_code_real_type reloc)
2678{
2679 switch (reloc)
2680 {
2681 case BFD_RELOC_MIPS16_JMP:
2682 case BFD_RELOC_MIPS16_GPREL:
2683 case BFD_RELOC_MIPS16_GOT16:
2684 case BFD_RELOC_MIPS16_CALL16:
2685 case BFD_RELOC_MIPS16_HI16_S:
2686 case BFD_RELOC_MIPS16_HI16:
2687 case BFD_RELOC_MIPS16_LO16:
2688 return TRUE;
2689
2690 default:
2691 return FALSE;
2692 }
2693}
2694
df58fc94
RS
2695static inline bfd_boolean
2696micromips_reloc_p (bfd_reloc_code_real_type reloc)
2697{
2698 switch (reloc)
2699 {
2700 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2701 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2702 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2703 case BFD_RELOC_MICROMIPS_GPREL16:
2704 case BFD_RELOC_MICROMIPS_JMP:
2705 case BFD_RELOC_MICROMIPS_HI16:
2706 case BFD_RELOC_MICROMIPS_HI16_S:
2707 case BFD_RELOC_MICROMIPS_LO16:
2708 case BFD_RELOC_MICROMIPS_LITERAL:
2709 case BFD_RELOC_MICROMIPS_GOT16:
2710 case BFD_RELOC_MICROMIPS_CALL16:
2711 case BFD_RELOC_MICROMIPS_GOT_HI16:
2712 case BFD_RELOC_MICROMIPS_GOT_LO16:
2713 case BFD_RELOC_MICROMIPS_CALL_HI16:
2714 case BFD_RELOC_MICROMIPS_CALL_LO16:
2715 case BFD_RELOC_MICROMIPS_SUB:
2716 case BFD_RELOC_MICROMIPS_GOT_PAGE:
2717 case BFD_RELOC_MICROMIPS_GOT_OFST:
2718 case BFD_RELOC_MICROMIPS_GOT_DISP:
2719 case BFD_RELOC_MICROMIPS_HIGHEST:
2720 case BFD_RELOC_MICROMIPS_HIGHER:
2721 case BFD_RELOC_MICROMIPS_SCN_DISP:
2722 case BFD_RELOC_MICROMIPS_JALR:
2723 return TRUE;
2724
2725 default:
2726 return FALSE;
2727 }
2728}
2729
2309ddf2
MR
2730static inline bfd_boolean
2731jmp_reloc_p (bfd_reloc_code_real_type reloc)
2732{
2733 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
2734}
2735
738e5348
RS
2736static inline bfd_boolean
2737got16_reloc_p (bfd_reloc_code_real_type reloc)
2738{
2309ddf2 2739 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 2740 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
2741}
2742
2743static inline bfd_boolean
2744hi16_reloc_p (bfd_reloc_code_real_type reloc)
2745{
2309ddf2 2746 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 2747 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
2748}
2749
2750static inline bfd_boolean
2751lo16_reloc_p (bfd_reloc_code_real_type reloc)
2752{
2309ddf2 2753 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
2754 || reloc == BFD_RELOC_MICROMIPS_LO16);
2755}
2756
df58fc94
RS
2757static inline bfd_boolean
2758jalr_reloc_p (bfd_reloc_code_real_type reloc)
2759{
2309ddf2 2760 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
2761}
2762
2de39019
CM
2763/* Return true if RELOC is a PC-relative relocation that does not have
2764 full address range. */
2765
2766static inline bfd_boolean
2767limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
2768{
2769 switch (reloc)
2770 {
2771 case BFD_RELOC_16_PCREL_S2:
2772 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2773 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2774 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2775 return TRUE;
2776
b47468a6
CM
2777 case BFD_RELOC_32_PCREL:
2778 return HAVE_64BIT_ADDRESSES;
2779
2de39019
CM
2780 default:
2781 return FALSE;
2782 }
2783}
b47468a6 2784
5919d012 2785/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
2786 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
2787 need a matching %lo() when applied to local symbols. */
5919d012
RS
2788
2789static inline bfd_boolean
17a2f251 2790reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 2791{
3b91255e 2792 return (HAVE_IN_PLACE_ADDENDS
738e5348 2793 && (hi16_reloc_p (reloc)
0a44bf69
RS
2794 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2795 all GOT16 relocations evaluate to "G". */
738e5348
RS
2796 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
2797}
2798
2799/* Return the type of %lo() reloc needed by RELOC, given that
2800 reloc_needs_lo_p. */
2801
2802static inline bfd_reloc_code_real_type
2803matching_lo_reloc (bfd_reloc_code_real_type reloc)
2804{
df58fc94
RS
2805 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
2806 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
2807 : BFD_RELOC_LO16));
5919d012
RS
2808}
2809
2810/* Return true if the given fixup is followed by a matching R_MIPS_LO16
2811 relocation. */
2812
2813static inline bfd_boolean
17a2f251 2814fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
2815{
2816 return (fixp->fx_next != NULL
738e5348 2817 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
2818 && fixp->fx_addsy == fixp->fx_next->fx_addsy
2819 && fixp->fx_offset == fixp->fx_next->fx_offset);
2820}
2821
252b5132
RH
2822/* This function returns true if modifying a register requires a
2823 delay. */
2824
2825static int
17a2f251 2826reg_needs_delay (unsigned int reg)
252b5132
RH
2827{
2828 unsigned long prev_pinfo;
2829
47e39b9d 2830 prev_pinfo = history[0].insn_mo->pinfo;
252b5132 2831 if (! mips_opts.noreorder
81912461
ILT
2832 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2833 && ! gpr_interlocks)
2834 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2835 && ! cop_interlocks)))
252b5132 2836 {
81912461
ILT
2837 /* A load from a coprocessor or from memory. All load delays
2838 delay the use of general register rt for one instruction. */
bdaaa2e1 2839 /* Itbl support may require additional care here. */
252b5132 2840 know (prev_pinfo & INSN_WRITE_GPR_T);
df58fc94 2841 if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
252b5132
RH
2842 return 1;
2843 }
2844
2845 return 0;
2846}
2847
462427c4
RS
2848/* Move all labels in LABELS to the current insertion point. TEXT_P
2849 says whether the labels refer to text or data. */
404a8071
RS
2850
2851static void
462427c4 2852mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
2853{
2854 struct insn_label_list *l;
2855 valueT val;
2856
462427c4 2857 for (l = labels; l != NULL; l = l->next)
404a8071 2858 {
9c2799c2 2859 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
2860 symbol_set_frag (l->label, frag_now);
2861 val = (valueT) frag_now_fix ();
df58fc94 2862 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 2863 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
2864 ++val;
2865 S_SET_VALUE (l->label, val);
2866 }
2867}
2868
462427c4
RS
2869/* Move all labels in insn_labels to the current insertion point
2870 and treat them as text labels. */
2871
2872static void
2873mips_move_text_labels (void)
2874{
2875 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
2876}
2877
5f0fe04b
TS
2878static bfd_boolean
2879s_is_linkonce (symbolS *sym, segT from_seg)
2880{
2881 bfd_boolean linkonce = FALSE;
2882 segT symseg = S_GET_SEGMENT (sym);
2883
2884 if (symseg != from_seg && !S_IS_LOCAL (sym))
2885 {
2886 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2887 linkonce = TRUE;
2888#ifdef OBJ_ELF
2889 /* The GNU toolchain uses an extension for ELF: a section
2890 beginning with the magic string .gnu.linkonce is a
2891 linkonce section. */
2892 if (strncmp (segment_name (symseg), ".gnu.linkonce",
2893 sizeof ".gnu.linkonce" - 1) == 0)
2894 linkonce = TRUE;
2895#endif
2896 }
2897 return linkonce;
2898}
2899
e1b47bd5 2900/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
2901 linker to handle them specially, such as generating jalx instructions
2902 when needed. We also make them odd for the duration of the assembly,
2903 in order to generate the right sort of code. We will make them even
252b5132
RH
2904 in the adjust_symtab routine, while leaving them marked. This is
2905 convenient for the debugger and the disassembler. The linker knows
2906 to make them odd again. */
2907
2908static void
e1b47bd5 2909mips_compressed_mark_label (symbolS *label)
252b5132 2910{
df58fc94 2911 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 2912
a8dbcb85 2913#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
e1b47bd5
RS
2914 if (IS_ELF)
2915 {
2916 if (mips_opts.mips16)
2917 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
2918 else
2919 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
252b5132 2920 }
e1b47bd5
RS
2921#endif
2922 if ((S_GET_VALUE (label) & 1) == 0
2923 /* Don't adjust the address if the label is global or weak, or
2924 in a link-once section, since we'll be emitting symbol reloc
2925 references to it which will be patched up by the linker, and
2926 the final value of the symbol may or may not be MIPS16/microMIPS. */
2927 && !S_IS_WEAK (label)
2928 && !S_IS_EXTERNAL (label)
2929 && !s_is_linkonce (label, now_seg))
2930 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
2931}
2932
2933/* Mark preceding MIPS16 or microMIPS instruction labels. */
2934
2935static void
2936mips_compressed_mark_labels (void)
2937{
2938 struct insn_label_list *l;
2939
2940 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
2941 mips_compressed_mark_label (l->label);
252b5132
RH
2942}
2943
4d7206a2
RS
2944/* End the current frag. Make it a variant frag and record the
2945 relaxation info. */
2946
2947static void
2948relax_close_frag (void)
2949{
584892a6 2950 mips_macro_warning.first_frag = frag_now;
4d7206a2 2951 frag_var (rs_machine_dependent, 0, 0,
584892a6 2952 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
2953 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2954
2955 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2956 mips_relax.first_fixup = 0;
2957}
2958
2959/* Start a new relaxation sequence whose expansion depends on SYMBOL.
2960 See the comment above RELAX_ENCODE for more details. */
2961
2962static void
2963relax_start (symbolS *symbol)
2964{
9c2799c2 2965 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
2966 mips_relax.sequence = 1;
2967 mips_relax.symbol = symbol;
2968}
2969
2970/* Start generating the second version of a relaxable sequence.
2971 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
2972
2973static void
4d7206a2
RS
2974relax_switch (void)
2975{
9c2799c2 2976 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
2977 mips_relax.sequence = 2;
2978}
2979
2980/* End the current relaxable sequence. */
2981
2982static void
2983relax_end (void)
2984{
9c2799c2 2985 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
2986 relax_close_frag ();
2987 mips_relax.sequence = 0;
2988}
2989
11625dd8
RS
2990/* Return true if IP is a delayed branch or jump. */
2991
2992static inline bfd_boolean
2993delayed_branch_p (const struct mips_cl_insn *ip)
2994{
2995 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2996 | INSN_COND_BRANCH_DELAY
2997 | INSN_COND_BRANCH_LIKELY)) != 0;
2998}
2999
3000/* Return true if IP is a compact branch or jump. */
3001
3002static inline bfd_boolean
3003compact_branch_p (const struct mips_cl_insn *ip)
3004{
3005 if (mips_opts.mips16)
3006 return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
3007 | MIPS16_INSN_COND_BRANCH)) != 0;
3008 else
3009 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
3010 | INSN2_COND_BRANCH)) != 0;
3011}
3012
3013/* Return true if IP is an unconditional branch or jump. */
3014
3015static inline bfd_boolean
3016uncond_branch_p (const struct mips_cl_insn *ip)
3017{
3018 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
3019 || (mips_opts.mips16
3020 ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
3021 : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
3022}
3023
3024/* Return true if IP is a branch-likely instruction. */
3025
3026static inline bfd_boolean
3027branch_likely_p (const struct mips_cl_insn *ip)
3028{
3029 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
3030}
3031
14fe068b
RS
3032/* Return the type of nop that should be used to fill the delay slot
3033 of delayed branch IP. */
3034
3035static struct mips_cl_insn *
3036get_delay_slot_nop (const struct mips_cl_insn *ip)
3037{
3038 if (mips_opts.micromips
3039 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3040 return &micromips_nop32_insn;
3041 return NOP_INSN;
3042}
3043
2309ddf2 3044/* Return the mask of core registers that IP reads or writes. */
df58fc94
RS
3045
3046static unsigned int
3047gpr_mod_mask (const struct mips_cl_insn *ip)
3048{
2309ddf2 3049 unsigned long pinfo2;
df58fc94
RS
3050 unsigned int mask;
3051
3052 mask = 0;
df58fc94
RS
3053 pinfo2 = ip->insn_mo->pinfo2;
3054 if (mips_opts.micromips)
3055 {
df58fc94
RS
3056 if (pinfo2 & INSN2_MOD_GPR_MD)
3057 mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
df58fc94
RS
3058 if (pinfo2 & INSN2_MOD_GPR_MF)
3059 mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
df58fc94
RS
3060 if (pinfo2 & INSN2_MOD_SP)
3061 mask |= 1 << SP;
3062 }
3063 return mask;
3064}
3065
4c260379
RS
3066/* Return the mask of core registers that IP reads. */
3067
3068static unsigned int
3069gpr_read_mask (const struct mips_cl_insn *ip)
3070{
3071 unsigned long pinfo, pinfo2;
3072 unsigned int mask;
3073
df58fc94 3074 mask = gpr_mod_mask (ip);
4c260379
RS
3075 pinfo = ip->insn_mo->pinfo;
3076 pinfo2 = ip->insn_mo->pinfo2;
3077 if (mips_opts.mips16)
3078 {
3079 if (pinfo & MIPS16_INSN_READ_X)
3080 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3081 if (pinfo & MIPS16_INSN_READ_Y)
3082 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3083 if (pinfo & MIPS16_INSN_READ_T)
3084 mask |= 1 << TREG;
3085 if (pinfo & MIPS16_INSN_READ_SP)
3086 mask |= 1 << SP;
3087 if (pinfo & MIPS16_INSN_READ_31)
3088 mask |= 1 << RA;
3089 if (pinfo & MIPS16_INSN_READ_Z)
3090 mask |= 1 << (mips16_to_32_reg_map
3091 [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3092 if (pinfo & MIPS16_INSN_READ_GPR_X)
3093 mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3094 }
3095 else
3096 {
3097 if (pinfo2 & INSN2_READ_GPR_D)
2309ddf2 3098 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3099 if (pinfo & INSN_READ_GPR_T)
2309ddf2 3100 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4c260379 3101 if (pinfo & INSN_READ_GPR_S)
2309ddf2
MR
3102 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3103 if (pinfo2 & INSN2_READ_GP)
3104 mask |= 1 << GP;
3105 if (pinfo2 & INSN2_READ_GPR_31)
3106 mask |= 1 << RA;
4c260379 3107 if (pinfo2 & INSN2_READ_GPR_Z)
2309ddf2 3108 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3109 }
2b0c8b40
MR
3110 if (mips_opts.micromips)
3111 {
3112 if (pinfo2 & INSN2_READ_GPR_MC)
3113 mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3114 if (pinfo2 & INSN2_READ_GPR_ME)
3115 mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3116 if (pinfo2 & INSN2_READ_GPR_MG)
3117 mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3118 if (pinfo2 & INSN2_READ_GPR_MJ)
3119 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3120 if (pinfo2 & INSN2_READ_GPR_MMN)
3121 {
3122 mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3123 mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3124 }
3125 if (pinfo2 & INSN2_READ_GPR_MP)
3126 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3127 if (pinfo2 & INSN2_READ_GPR_MQ)
3128 mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3129 }
fe35f09f
RS
3130 /* Don't include register 0. */
3131 return mask & ~1;
4c260379
RS
3132}
3133
3134/* Return the mask of core registers that IP writes. */
3135
3136static unsigned int
3137gpr_write_mask (const struct mips_cl_insn *ip)
3138{
3139 unsigned long pinfo, pinfo2;
3140 unsigned int mask;
3141
df58fc94 3142 mask = gpr_mod_mask (ip);
4c260379
RS
3143 pinfo = ip->insn_mo->pinfo;
3144 pinfo2 = ip->insn_mo->pinfo2;
3145 if (mips_opts.mips16)
3146 {
3147 if (pinfo & MIPS16_INSN_WRITE_X)
3148 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3149 if (pinfo & MIPS16_INSN_WRITE_Y)
3150 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3151 if (pinfo & MIPS16_INSN_WRITE_Z)
3152 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3153 if (pinfo & MIPS16_INSN_WRITE_T)
3154 mask |= 1 << TREG;
3155 if (pinfo & MIPS16_INSN_WRITE_SP)
3156 mask |= 1 << SP;
3157 if (pinfo & MIPS16_INSN_WRITE_31)
3158 mask |= 1 << RA;
3159 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3160 mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3161 }
3162 else
3163 {
3164 if (pinfo & INSN_WRITE_GPR_D)
df58fc94 3165 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3166 if (pinfo & INSN_WRITE_GPR_T)
df58fc94 3167 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
2b0c8b40 3168 if (pinfo & INSN_WRITE_GPR_S)
2309ddf2 3169 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379
RS
3170 if (pinfo & INSN_WRITE_GPR_31)
3171 mask |= 1 << RA;
3172 if (pinfo2 & INSN2_WRITE_GPR_Z)
df58fc94 3173 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3174 }
2b0c8b40
MR
3175 if (mips_opts.micromips)
3176 {
3177 if (pinfo2 & INSN2_WRITE_GPR_MB)
3178 mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3179 if (pinfo2 & INSN2_WRITE_GPR_MHI)
3180 {
3181 mask |= 1 << micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
3182 mask |= 1 << micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
3183 }
3184 if (pinfo2 & INSN2_WRITE_GPR_MJ)
3185 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3186 if (pinfo2 & INSN2_WRITE_GPR_MP)
3187 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3188 }
fe35f09f
RS
3189 /* Don't include register 0. */
3190 return mask & ~1;
4c260379
RS
3191}
3192
3193/* Return the mask of floating-point registers that IP reads. */
3194
3195static unsigned int
3196fpr_read_mask (const struct mips_cl_insn *ip)
3197{
3198 unsigned long pinfo, pinfo2;
3199 unsigned int mask;
3200
3201 mask = 0;
3202 pinfo = ip->insn_mo->pinfo;
3203 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3204 if (!mips_opts.mips16)
df58fc94
RS
3205 {
3206 if (pinfo2 & INSN2_READ_FPR_D)
2309ddf2 3207 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3208 if (pinfo & INSN_READ_FPR_S)
df58fc94 3209 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3210 if (pinfo & INSN_READ_FPR_T)
df58fc94 3211 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3212 if (pinfo & INSN_READ_FPR_R)
df58fc94 3213 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
4c260379 3214 if (pinfo2 & INSN2_READ_FPR_Z)
df58fc94 3215 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3216 }
3217 /* Conservatively treat all operands to an FP_D instruction are doubles.
3218 (This is overly pessimistic for things like cvt.d.s.) */
3219 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3220 mask |= mask << 1;
3221 return mask;
3222}
3223
3224/* Return the mask of floating-point registers that IP writes. */
3225
3226static unsigned int
3227fpr_write_mask (const struct mips_cl_insn *ip)
3228{
3229 unsigned long pinfo, pinfo2;
3230 unsigned int mask;
3231
3232 mask = 0;
3233 pinfo = ip->insn_mo->pinfo;
3234 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3235 if (!mips_opts.mips16)
4c260379
RS
3236 {
3237 if (pinfo & INSN_WRITE_FPR_D)
df58fc94 3238 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3239 if (pinfo & INSN_WRITE_FPR_S)
df58fc94 3240 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3241 if (pinfo & INSN_WRITE_FPR_T)
df58fc94 3242 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3243 if (pinfo2 & INSN2_WRITE_FPR_Z)
df58fc94 3244 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3245 }
3246 /* Conservatively treat all operands to an FP_D instruction are doubles.
3247 (This is overly pessimistic for things like cvt.s.d.) */
3248 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3249 mask |= mask << 1;
3250 return mask;
3251}
3252
71400594
RS
3253/* Classify an instruction according to the FIX_VR4120_* enumeration.
3254 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3255 by VR4120 errata. */
4d7206a2 3256
71400594
RS
3257static unsigned int
3258classify_vr4120_insn (const char *name)
252b5132 3259{
71400594
RS
3260 if (strncmp (name, "macc", 4) == 0)
3261 return FIX_VR4120_MACC;
3262 if (strncmp (name, "dmacc", 5) == 0)
3263 return FIX_VR4120_DMACC;
3264 if (strncmp (name, "mult", 4) == 0)
3265 return FIX_VR4120_MULT;
3266 if (strncmp (name, "dmult", 5) == 0)
3267 return FIX_VR4120_DMULT;
3268 if (strstr (name, "div"))
3269 return FIX_VR4120_DIV;
3270 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3271 return FIX_VR4120_MTHILO;
3272 return NUM_FIX_VR4120_CLASSES;
3273}
252b5132 3274
ff239038
CM
3275#define INSN_ERET 0x42000018
3276#define INSN_DERET 0x4200001f
3277
71400594
RS
3278/* Return the number of instructions that must separate INSN1 and INSN2,
3279 where INSN1 is the earlier instruction. Return the worst-case value
3280 for any INSN2 if INSN2 is null. */
252b5132 3281
71400594
RS
3282static unsigned int
3283insns_between (const struct mips_cl_insn *insn1,
3284 const struct mips_cl_insn *insn2)
3285{
3286 unsigned long pinfo1, pinfo2;
4c260379 3287 unsigned int mask;
71400594
RS
3288
3289 /* This function needs to know which pinfo flags are set for INSN2
3290 and which registers INSN2 uses. The former is stored in PINFO2 and
4c260379
RS
3291 the latter is tested via INSN2_USES_GPR. If INSN2 is null, PINFO2
3292 will have every flag set and INSN2_USES_GPR will always return true. */
71400594
RS
3293 pinfo1 = insn1->insn_mo->pinfo;
3294 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 3295
4c260379
RS
3296#define INSN2_USES_GPR(REG) \
3297 (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
71400594
RS
3298
3299 /* For most targets, write-after-read dependencies on the HI and LO
3300 registers must be separated by at least two instructions. */
3301 if (!hilo_interlocks)
252b5132 3302 {
71400594
RS
3303 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3304 return 2;
3305 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3306 return 2;
3307 }
3308
3309 /* If we're working around r7000 errata, there must be two instructions
3310 between an mfhi or mflo and any instruction that uses the result. */
3311 if (mips_7000_hilo_fix
df58fc94 3312 && !mips_opts.micromips
71400594 3313 && MF_HILO_INSN (pinfo1)
df58fc94 3314 && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
71400594
RS
3315 return 2;
3316
ff239038
CM
3317 /* If we're working around 24K errata, one instruction is required
3318 if an ERET or DERET is followed by a branch instruction. */
df58fc94 3319 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
3320 {
3321 if (insn1->insn_opcode == INSN_ERET
3322 || insn1->insn_opcode == INSN_DERET)
3323 {
3324 if (insn2 == NULL
3325 || insn2->insn_opcode == INSN_ERET
3326 || insn2->insn_opcode == INSN_DERET
11625dd8 3327 || delayed_branch_p (insn2))
ff239038
CM
3328 return 1;
3329 }
3330 }
3331
71400594
RS
3332 /* If working around VR4120 errata, check for combinations that need
3333 a single intervening instruction. */
df58fc94 3334 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
3335 {
3336 unsigned int class1, class2;
252b5132 3337
71400594
RS
3338 class1 = classify_vr4120_insn (insn1->insn_mo->name);
3339 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 3340 {
71400594
RS
3341 if (insn2 == NULL)
3342 return 1;
3343 class2 = classify_vr4120_insn (insn2->insn_mo->name);
3344 if (vr4120_conflicts[class1] & (1 << class2))
3345 return 1;
252b5132 3346 }
71400594
RS
3347 }
3348
df58fc94 3349 if (!HAVE_CODE_COMPRESSION)
71400594
RS
3350 {
3351 /* Check for GPR or coprocessor load delays. All such delays
3352 are on the RT register. */
3353 /* Itbl support may require additional care here. */
3354 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3355 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 3356 {
71400594 3357 know (pinfo1 & INSN_WRITE_GPR_T);
df58fc94 3358 if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
71400594
RS
3359 return 1;
3360 }
3361
3362 /* Check for generic coprocessor hazards.
3363
3364 This case is not handled very well. There is no special
3365 knowledge of CP0 handling, and the coprocessors other than
3366 the floating point unit are not distinguished at all. */
3367 /* Itbl support may require additional care here. FIXME!
3368 Need to modify this to include knowledge about
3369 user specified delays! */
3370 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3371 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3372 {
3373 /* Handle cases where INSN1 writes to a known general coprocessor
3374 register. There must be a one instruction delay before INSN2
3375 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
3376 mask = fpr_write_mask (insn1);
3377 if (mask != 0)
252b5132 3378 {
4c260379 3379 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 3380 return 1;
252b5132
RH
3381 }
3382 else
3383 {
71400594
RS
3384 /* Read-after-write dependencies on the control registers
3385 require a two-instruction gap. */
3386 if ((pinfo1 & INSN_WRITE_COND_CODE)
3387 && (pinfo2 & INSN_READ_COND_CODE))
3388 return 2;
3389
3390 /* We don't know exactly what INSN1 does. If INSN2 is
3391 also a coprocessor instruction, assume there must be
3392 a one instruction gap. */
3393 if (pinfo2 & INSN_COP)
3394 return 1;
252b5132
RH
3395 }
3396 }
6b76fefe 3397
71400594
RS
3398 /* Check for read-after-write dependencies on the coprocessor
3399 control registers in cases where INSN1 does not need a general
3400 coprocessor delay. This means that INSN1 is a floating point
3401 comparison instruction. */
3402 /* Itbl support may require additional care here. */
3403 else if (!cop_interlocks
3404 && (pinfo1 & INSN_WRITE_COND_CODE)
3405 && (pinfo2 & INSN_READ_COND_CODE))
3406 return 1;
3407 }
6b76fefe 3408
4c260379 3409#undef INSN2_USES_GPR
6b76fefe 3410
71400594
RS
3411 return 0;
3412}
6b76fefe 3413
7d8e00cf
RS
3414/* Return the number of nops that would be needed to work around the
3415 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
3416 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
3417 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
3418
3419static int
932d1a1b 3420nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
3421 const struct mips_cl_insn *insn)
3422{
4c260379
RS
3423 int i, j;
3424 unsigned int mask;
7d8e00cf
RS
3425
3426 /* Check if the instruction writes to HI or LO. MTHI and MTLO
3427 are not affected by the errata. */
3428 if (insn != 0
3429 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3430 || strcmp (insn->insn_mo->name, "mtlo") == 0
3431 || strcmp (insn->insn_mo->name, "mthi") == 0))
3432 return 0;
3433
3434 /* Search for the first MFLO or MFHI. */
3435 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 3436 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
3437 {
3438 /* Extract the destination register. */
4c260379 3439 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
3440
3441 /* No nops are needed if INSN reads that register. */
4c260379 3442 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
3443 return 0;
3444
3445 /* ...or if any of the intervening instructions do. */
3446 for (j = 0; j < i; j++)
4c260379 3447 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
3448 return 0;
3449
932d1a1b
RS
3450 if (i >= ignore)
3451 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
3452 }
3453 return 0;
3454}
3455
15be625d
CM
3456#define BASE_REG_EQ(INSN1, INSN2) \
3457 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3458 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3459
3460/* Return the minimum alignment for this store instruction. */
3461
3462static int
3463fix_24k_align_to (const struct mips_opcode *mo)
3464{
3465 if (strcmp (mo->name, "sh") == 0)
3466 return 2;
3467
3468 if (strcmp (mo->name, "swc1") == 0
3469 || strcmp (mo->name, "swc2") == 0
3470 || strcmp (mo->name, "sw") == 0
3471 || strcmp (mo->name, "sc") == 0
3472 || strcmp (mo->name, "s.s") == 0)
3473 return 4;
3474
3475 if (strcmp (mo->name, "sdc1") == 0
3476 || strcmp (mo->name, "sdc2") == 0
3477 || strcmp (mo->name, "s.d") == 0)
3478 return 8;
3479
3480 /* sb, swl, swr */
3481 return 1;
3482}
3483
3484struct fix_24k_store_info
3485 {
3486 /* Immediate offset, if any, for this store instruction. */
3487 short off;
3488 /* Alignment required by this store instruction. */
3489 int align_to;
3490 /* True for register offsets. */
3491 int register_offset;
3492 };
3493
3494/* Comparison function used by qsort. */
3495
3496static int
3497fix_24k_sort (const void *a, const void *b)
3498{
3499 const struct fix_24k_store_info *pos1 = a;
3500 const struct fix_24k_store_info *pos2 = b;
3501
3502 return (pos1->off - pos2->off);
3503}
3504
3505/* INSN is a store instruction. Try to record the store information
3506 in STINFO. Return false if the information isn't known. */
3507
3508static bfd_boolean
3509fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 3510 const struct mips_cl_insn *insn)
15be625d
CM
3511{
3512 /* The instruction must have a known offset. */
3513 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3514 return FALSE;
3515
3516 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3517 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3518 return TRUE;
3519}
3520
932d1a1b
RS
3521/* Return the number of nops that would be needed to work around the 24k
3522 "lost data on stores during refill" errata if instruction INSN
3523 immediately followed the 2 instructions described by HIST.
3524 Ignore hazards that are contained within the first IGNORE
3525 instructions of HIST.
3526
3527 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3528 for the data cache refills and store data. The following describes
3529 the scenario where the store data could be lost.
3530
3531 * A data cache miss, due to either a load or a store, causing fill
3532 data to be supplied by the memory subsystem
3533 * The first three doublewords of fill data are returned and written
3534 into the cache
3535 * A sequence of four stores occurs in consecutive cycles around the
3536 final doubleword of the fill:
3537 * Store A
3538 * Store B
3539 * Store C
3540 * Zero, One or more instructions
3541 * Store D
3542
3543 The four stores A-D must be to different doublewords of the line that
3544 is being filled. The fourth instruction in the sequence above permits
3545 the fill of the final doubleword to be transferred from the FSB into
3546 the cache. In the sequence above, the stores may be either integer
3547 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3548 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3549 different doublewords on the line. If the floating point unit is
3550 running in 1:2 mode, it is not possible to create the sequence above
3551 using only floating point store instructions.
15be625d
CM
3552
3553 In this case, the cache line being filled is incorrectly marked
3554 invalid, thereby losing the data from any store to the line that
3555 occurs between the original miss and the completion of the five
3556 cycle sequence shown above.
3557
932d1a1b 3558 The workarounds are:
15be625d 3559
932d1a1b
RS
3560 * Run the data cache in write-through mode.
3561 * Insert a non-store instruction between
3562 Store A and Store B or Store B and Store C. */
15be625d
CM
3563
3564static int
932d1a1b 3565nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
3566 const struct mips_cl_insn *insn)
3567{
3568 struct fix_24k_store_info pos[3];
3569 int align, i, base_offset;
3570
932d1a1b
RS
3571 if (ignore >= 2)
3572 return 0;
3573
ab9794cf
RS
3574 /* If the previous instruction wasn't a store, there's nothing to
3575 worry about. */
15be625d
CM
3576 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3577 return 0;
3578
ab9794cf
RS
3579 /* If the instructions after the previous one are unknown, we have
3580 to assume the worst. */
3581 if (!insn)
15be625d
CM
3582 return 1;
3583
ab9794cf
RS
3584 /* Check whether we are dealing with three consecutive stores. */
3585 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3586 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
3587 return 0;
3588
3589 /* If we don't know the relationship between the store addresses,
3590 assume the worst. */
ab9794cf 3591 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
3592 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3593 return 1;
3594
3595 if (!fix_24k_record_store_info (&pos[0], insn)
3596 || !fix_24k_record_store_info (&pos[1], &hist[0])
3597 || !fix_24k_record_store_info (&pos[2], &hist[1]))
3598 return 1;
3599
3600 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3601
3602 /* Pick a value of ALIGN and X such that all offsets are adjusted by
3603 X bytes and such that the base register + X is known to be aligned
3604 to align bytes. */
3605
3606 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3607 align = 8;
3608 else
3609 {
3610 align = pos[0].align_to;
3611 base_offset = pos[0].off;
3612 for (i = 1; i < 3; i++)
3613 if (align < pos[i].align_to)
3614 {
3615 align = pos[i].align_to;
3616 base_offset = pos[i].off;
3617 }
3618 for (i = 0; i < 3; i++)
3619 pos[i].off -= base_offset;
3620 }
3621
3622 pos[0].off &= ~align + 1;
3623 pos[1].off &= ~align + 1;
3624 pos[2].off &= ~align + 1;
3625
3626 /* If any two stores write to the same chunk, they also write to the
3627 same doubleword. The offsets are still sorted at this point. */
3628 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3629 return 0;
3630
3631 /* A range of at least 9 bytes is needed for the stores to be in
3632 non-overlapping doublewords. */
3633 if (pos[2].off - pos[0].off <= 8)
3634 return 0;
3635
3636 if (pos[2].off - pos[1].off >= 24
3637 || pos[1].off - pos[0].off >= 24
3638 || pos[2].off - pos[0].off >= 32)
3639 return 0;
3640
3641 return 1;
3642}
3643
71400594 3644/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 3645 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
3646 where HIST[0] is the most recent instruction. Ignore hazards
3647 between INSN and the first IGNORE instructions in HIST.
3648
3649 If INSN is null, return the worse-case number of nops for any
3650 instruction. */
bdaaa2e1 3651
71400594 3652static int
932d1a1b 3653nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
3654 const struct mips_cl_insn *insn)
3655{
3656 int i, nops, tmp_nops;
bdaaa2e1 3657
71400594 3658 nops = 0;
932d1a1b 3659 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 3660 {
91d6fa6a 3661 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
3662 if (tmp_nops > nops)
3663 nops = tmp_nops;
3664 }
7d8e00cf 3665
df58fc94 3666 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 3667 {
932d1a1b 3668 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
3669 if (tmp_nops > nops)
3670 nops = tmp_nops;
3671 }
3672
df58fc94 3673 if (mips_fix_24k && !mips_opts.micromips)
15be625d 3674 {
932d1a1b 3675 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
3676 if (tmp_nops > nops)
3677 nops = tmp_nops;
3678 }
3679
71400594
RS
3680 return nops;
3681}
252b5132 3682
71400594 3683/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 3684 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
3685 would be needed after the extended sequence, ignoring hazards
3686 in the first IGNORE instructions. */
252b5132 3687
71400594 3688static int
932d1a1b
RS
3689nops_for_sequence (int num_insns, int ignore,
3690 const struct mips_cl_insn *hist, ...)
71400594
RS
3691{
3692 va_list args;
3693 struct mips_cl_insn buffer[MAX_NOPS];
3694 struct mips_cl_insn *cursor;
3695 int nops;
3696
91d6fa6a 3697 va_start (args, hist);
71400594 3698 cursor = buffer + num_insns;
91d6fa6a 3699 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
3700 while (cursor > buffer)
3701 *--cursor = *va_arg (args, const struct mips_cl_insn *);
3702
932d1a1b 3703 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
3704 va_end (args);
3705 return nops;
3706}
252b5132 3707
71400594
RS
3708/* Like nops_for_insn, but if INSN is a branch, take into account the
3709 worst-case delay for the branch target. */
252b5132 3710
71400594 3711static int
932d1a1b 3712nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
3713 const struct mips_cl_insn *insn)
3714{
3715 int nops, tmp_nops;
60b63b72 3716
932d1a1b 3717 nops = nops_for_insn (ignore, hist, insn);
11625dd8 3718 if (delayed_branch_p (insn))
71400594 3719 {
932d1a1b 3720 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 3721 hist, insn, get_delay_slot_nop (insn));
71400594
RS
3722 if (tmp_nops > nops)
3723 nops = tmp_nops;
3724 }
11625dd8 3725 else if (compact_branch_p (insn))
71400594 3726 {
932d1a1b 3727 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
3728 if (tmp_nops > nops)
3729 nops = tmp_nops;
3730 }
3731 return nops;
3732}
3733
c67a084a
NC
3734/* Fix NOP issue: Replace nops by "or at,at,zero". */
3735
3736static void
3737fix_loongson2f_nop (struct mips_cl_insn * ip)
3738{
df58fc94 3739 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
3740 if (strcmp (ip->insn_mo->name, "nop") == 0)
3741 ip->insn_opcode = LOONGSON2F_NOP_INSN;
3742}
3743
3744/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
3745 jr target pc &= 'hffff_ffff_cfff_ffff. */
3746
3747static void
3748fix_loongson2f_jump (struct mips_cl_insn * ip)
3749{
df58fc94 3750 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
3751 if (strcmp (ip->insn_mo->name, "j") == 0
3752 || strcmp (ip->insn_mo->name, "jr") == 0
3753 || strcmp (ip->insn_mo->name, "jalr") == 0)
3754 {
3755 int sreg;
3756 expressionS ep;
3757
3758 if (! mips_opts.at)
3759 return;
3760
df58fc94 3761 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
3762 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
3763 return;
3764
3765 ep.X_op = O_constant;
3766 ep.X_add_number = 0xcfff0000;
3767 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
3768 ep.X_add_number = 0xffff;
3769 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
3770 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
3771 }
3772}
3773
3774static void
3775fix_loongson2f (struct mips_cl_insn * ip)
3776{
3777 if (mips_fix_loongson2f_nop)
3778 fix_loongson2f_nop (ip);
3779
3780 if (mips_fix_loongson2f_jump)
3781 fix_loongson2f_jump (ip);
3782}
3783
a4e06468
RS
3784/* IP is a branch that has a delay slot, and we need to fill it
3785 automatically. Return true if we can do that by swapping IP
e407c74b
NC
3786 with the previous instruction.
3787 ADDRESS_EXPR is an operand of the instruction to be used with
3788 RELOC_TYPE. */
a4e06468
RS
3789
3790static bfd_boolean
e407c74b
NC
3791can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
3792 bfd_reloc_code_real_type *reloc_type)
a4e06468 3793{
2b0c8b40 3794 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468
RS
3795 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
3796
3797 /* -O2 and above is required for this optimization. */
3798 if (mips_optimize < 2)
3799 return FALSE;
3800
3801 /* If we have seen .set volatile or .set nomove, don't optimize. */
3802 if (mips_opts.nomove)
3803 return FALSE;
3804
3805 /* We can't swap if the previous instruction's position is fixed. */
3806 if (history[0].fixed_p)
3807 return FALSE;
3808
3809 /* If the previous previous insn was in a .set noreorder, we can't
3810 swap. Actually, the MIPS assembler will swap in this situation.
3811 However, gcc configured -with-gnu-as will generate code like
3812
3813 .set noreorder
3814 lw $4,XXX
3815 .set reorder
3816 INSN
3817 bne $4,$0,foo
3818
3819 in which we can not swap the bne and INSN. If gcc is not configured
3820 -with-gnu-as, it does not output the .set pseudo-ops. */
3821 if (history[1].noreorder_p)
3822 return FALSE;
3823
87333bb7
MR
3824 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
3825 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
3826 if (mips_opts.mips16 && history[0].fixp[0])
3827 return FALSE;
3828
3829 /* If the branch is itself the target of a branch, we can not swap.
3830 We cheat on this; all we check for is whether there is a label on
3831 this instruction. If there are any branches to anything other than
3832 a label, users must use .set noreorder. */
3833 if (seg_info (now_seg)->label_list)
3834 return FALSE;
3835
3836 /* If the previous instruction is in a variant frag other than this
2309ddf2 3837 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
3838 MIPS16 code, which uses variant frags for different purposes. */
3839 if (!mips_opts.mips16
a4e06468
RS
3840 && history[0].frag
3841 && history[0].frag->fr_type == rs_machine_dependent)
3842 return FALSE;
3843
bcd530a7
RS
3844 /* We do not swap with instructions that cannot architecturally
3845 be placed in a branch delay slot, such as SYNC or ERET. We
3846 also refrain from swapping with a trap instruction, since it
3847 complicates trap handlers to have the trap instruction be in
3848 a delay slot. */
a4e06468 3849 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 3850 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
3851 return FALSE;
3852
3853 /* Check for conflicts between the branch and the instructions
3854 before the candidate delay slot. */
3855 if (nops_for_insn (0, history + 1, ip) > 0)
3856 return FALSE;
3857
3858 /* Check for conflicts between the swapped sequence and the
3859 target of the branch. */
3860 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
3861 return FALSE;
3862
3863 /* If the branch reads a register that the previous
3864 instruction sets, we can not swap. */
3865 gpr_read = gpr_read_mask (ip);
3866 prev_gpr_write = gpr_write_mask (&history[0]);
3867 if (gpr_read & prev_gpr_write)
3868 return FALSE;
3869
3870 /* If the branch writes a register that the previous
3871 instruction sets, we can not swap. */
3872 gpr_write = gpr_write_mask (ip);
3873 if (gpr_write & prev_gpr_write)
3874 return FALSE;
3875
3876 /* If the branch writes a register that the previous
3877 instruction reads, we can not swap. */
3878 prev_gpr_read = gpr_read_mask (&history[0]);
3879 if (gpr_write & prev_gpr_read)
3880 return FALSE;
3881
3882 /* If one instruction sets a condition code and the
3883 other one uses a condition code, we can not swap. */
3884 pinfo = ip->insn_mo->pinfo;
3885 if ((pinfo & INSN_READ_COND_CODE)
3886 && (prev_pinfo & INSN_WRITE_COND_CODE))
3887 return FALSE;
3888 if ((pinfo & INSN_WRITE_COND_CODE)
3889 && (prev_pinfo & INSN_READ_COND_CODE))
3890 return FALSE;
3891
3892 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 3893 prev_pinfo2 = history[0].insn_mo->pinfo2;
a4e06468
RS
3894 if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
3895 return FALSE;
2b0c8b40
MR
3896 if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
3897 return FALSE;
a4e06468 3898
df58fc94
RS
3899 /* If the previous instruction has an incorrect size for a fixed
3900 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
3901 pinfo2 = ip->insn_mo->pinfo2;
3902 if (mips_opts.micromips
3903 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
3904 && insn_length (history) != 2)
3905 return FALSE;
3906 if (mips_opts.micromips
3907 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
3908 && insn_length (history) != 4)
3909 return FALSE;
3910
e407c74b
NC
3911 /* On R5900 short loops need to be fixed by inserting a nop in
3912 the branch delay slots.
3913 A short loop can be terminated too early. */
3914 if (mips_opts.arch == CPU_R5900
3915 /* Check if instruction has a parameter, ignore "j $31". */
3916 && (address_expr != NULL)
3917 /* Parameter must be 16 bit. */
3918 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
3919 /* Branch to same segment. */
3920 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
3921 /* Branch to same code fragment. */
3922 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
3923 /* Can only calculate branch offset if value is known. */
3924 && symbol_constant_p(address_expr->X_add_symbol)
3925 /* Check if branch is really conditional. */
3926 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
3927 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
3928 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
3929 {
3930 int distance;
3931 /* Check if loop is shorter than 6 instructions including
3932 branch and delay slot. */
3933 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
3934 if (distance <= 20)
3935 {
3936 int i;
3937 int rv;
3938
3939 rv = FALSE;
3940 /* When the loop includes branches or jumps,
3941 it is not a short loop. */
3942 for (i = 0; i < (distance / 4); i++)
3943 {
3944 if ((history[i].cleared_p)
3945 || delayed_branch_p(&history[i]))
3946 {
3947 rv = TRUE;
3948 break;
3949 }
3950 }
3951 if (rv == FALSE)
3952 {
3953 /* Insert nop after branch to fix short loop. */
3954 return FALSE;
3955 }
3956 }
3957 }
3958
a4e06468
RS
3959 return TRUE;
3960}
3961
e407c74b
NC
3962/* Decide how we should add IP to the instruction stream.
3963 ADDRESS_EXPR is an operand of the instruction to be used with
3964 RELOC_TYPE. */
a4e06468
RS
3965
3966static enum append_method
e407c74b
NC
3967get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
3968 bfd_reloc_code_real_type *reloc_type)
a4e06468
RS
3969{
3970 unsigned long pinfo;
3971
3972 /* The relaxed version of a macro sequence must be inherently
3973 hazard-free. */
3974 if (mips_relax.sequence == 2)
3975 return APPEND_ADD;
3976
3977 /* We must not dabble with instructions in a ".set norerorder" block. */
3978 if (mips_opts.noreorder)
3979 return APPEND_ADD;
3980
3981 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 3982 if (delayed_branch_p (ip))
a4e06468 3983 {
e407c74b
NC
3984 if (!branch_likely_p (ip)
3985 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
3986 return APPEND_SWAP;
3987
11625dd8 3988 pinfo = ip->insn_mo->pinfo;
a4e06468
RS
3989 if (mips_opts.mips16
3990 && ISA_SUPPORTS_MIPS16E
a4e06468
RS
3991 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
3992 return APPEND_ADD_COMPACT;
3993
3994 return APPEND_ADD_WITH_NOP;
3995 }
3996
a4e06468
RS
3997 return APPEND_ADD;
3998}
3999
ceb94aa5
RS
4000/* IP is a MIPS16 instruction whose opcode we have just changed.
4001 Point IP->insn_mo to the new opcode's definition. */
4002
4003static void
4004find_altered_mips16_opcode (struct mips_cl_insn *ip)
4005{
4006 const struct mips_opcode *mo, *end;
4007
4008 end = &mips16_opcodes[bfd_mips16_num_opcodes];
4009 for (mo = ip->insn_mo; mo < end; mo++)
4010 if ((ip->insn_opcode & mo->mask) == mo->match)
4011 {
4012 ip->insn_mo = mo;
4013 return;
4014 }
4015 abort ();
4016}
4017
df58fc94
RS
4018/* For microMIPS macros, we need to generate a local number label
4019 as the target of branches. */
4020#define MICROMIPS_LABEL_CHAR '\037'
4021static unsigned long micromips_target_label;
4022static char micromips_target_name[32];
4023
4024static char *
4025micromips_label_name (void)
4026{
4027 char *p = micromips_target_name;
4028 char symbol_name_temporary[24];
4029 unsigned long l;
4030 int i;
4031
4032 if (*p)
4033 return p;
4034
4035 i = 0;
4036 l = micromips_target_label;
4037#ifdef LOCAL_LABEL_PREFIX
4038 *p++ = LOCAL_LABEL_PREFIX;
4039#endif
4040 *p++ = 'L';
4041 *p++ = MICROMIPS_LABEL_CHAR;
4042 do
4043 {
4044 symbol_name_temporary[i++] = l % 10 + '0';
4045 l /= 10;
4046 }
4047 while (l != 0);
4048 while (i > 0)
4049 *p++ = symbol_name_temporary[--i];
4050 *p = '\0';
4051
4052 return micromips_target_name;
4053}
4054
4055static void
4056micromips_label_expr (expressionS *label_expr)
4057{
4058 label_expr->X_op = O_symbol;
4059 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
4060 label_expr->X_add_number = 0;
4061}
4062
4063static void
4064micromips_label_inc (void)
4065{
4066 micromips_target_label++;
4067 *micromips_target_name = '\0';
4068}
4069
4070static void
4071micromips_add_label (void)
4072{
4073 symbolS *s;
4074
4075 s = colon (micromips_label_name ());
4076 micromips_label_inc ();
4077#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
4078 if (IS_ELF)
4079 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
db9b2be4
AM
4080#else
4081 (void) s;
df58fc94
RS
4082#endif
4083}
4084
4085/* If assembling microMIPS code, then return the microMIPS reloc
4086 corresponding to the requested one if any. Otherwise return
4087 the reloc unchanged. */
4088
4089static bfd_reloc_code_real_type
4090micromips_map_reloc (bfd_reloc_code_real_type reloc)
4091{
4092 static const bfd_reloc_code_real_type relocs[][2] =
4093 {
4094 /* Keep sorted incrementally by the left-hand key. */
4095 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
4096 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
4097 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
4098 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
4099 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
4100 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
4101 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
4102 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
4103 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
4104 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
4105 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
4106 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
4107 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
4108 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
4109 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
4110 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
4111 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
4112 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
4113 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
4114 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
4115 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
4116 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
4117 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
4118 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
4119 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
4120 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
4121 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
4122 };
4123 bfd_reloc_code_real_type r;
4124 size_t i;
4125
4126 if (!mips_opts.micromips)
4127 return reloc;
4128 for (i = 0; i < ARRAY_SIZE (relocs); i++)
4129 {
4130 r = relocs[i][0];
4131 if (r > reloc)
4132 return reloc;
4133 if (r == reloc)
4134 return relocs[i][1];
4135 }
4136 return reloc;
4137}
4138
b886a2ab
RS
4139/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
4140 Return true on success, storing the resolved value in RESULT. */
4141
4142static bfd_boolean
4143calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
4144 offsetT *result)
4145{
4146 switch (reloc)
4147 {
4148 case BFD_RELOC_MIPS_HIGHEST:
4149 case BFD_RELOC_MICROMIPS_HIGHEST:
4150 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
4151 return TRUE;
4152
4153 case BFD_RELOC_MIPS_HIGHER:
4154 case BFD_RELOC_MICROMIPS_HIGHER:
4155 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
4156 return TRUE;
4157
4158 case BFD_RELOC_HI16_S:
4159 case BFD_RELOC_MICROMIPS_HI16_S:
4160 case BFD_RELOC_MIPS16_HI16_S:
4161 *result = ((operand + 0x8000) >> 16) & 0xffff;
4162 return TRUE;
4163
4164 case BFD_RELOC_HI16:
4165 case BFD_RELOC_MICROMIPS_HI16:
4166 case BFD_RELOC_MIPS16_HI16:
4167 *result = (operand >> 16) & 0xffff;
4168 return TRUE;
4169
4170 case BFD_RELOC_LO16:
4171 case BFD_RELOC_MICROMIPS_LO16:
4172 case BFD_RELOC_MIPS16_LO16:
4173 *result = operand & 0xffff;
4174 return TRUE;
4175
4176 case BFD_RELOC_UNUSED:
4177 *result = operand;
4178 return TRUE;
4179
4180 default:
4181 return FALSE;
4182 }
4183}
4184
71400594
RS
4185/* Output an instruction. IP is the instruction information.
4186 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
4187 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
4188 a macro expansion. */
71400594
RS
4189
4190static void
4191append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 4192 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 4193{
14fe068b 4194 unsigned long prev_pinfo2, pinfo;
71400594 4195 bfd_boolean relaxed_branch = FALSE;
a4e06468 4196 enum append_method method;
2309ddf2 4197 bfd_boolean relax32;
2b0c8b40 4198 int branch_disp;
71400594 4199
2309ddf2 4200 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
4201 fix_loongson2f (ip);
4202
738f4d98 4203 file_ase_mips16 |= mips_opts.mips16;
df58fc94 4204 file_ase_micromips |= mips_opts.micromips;
738f4d98 4205
df58fc94 4206 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 4207 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
4208
4209 if (mips_opts.micromips
4210 && !expansionp
4211 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4212 && micromips_insn_length (ip->insn_mo) != 2)
4213 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4214 && micromips_insn_length (ip->insn_mo) != 4)))
4215 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4216 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 4217
15be625d
CM
4218 if (address_expr == NULL)
4219 ip->complete_p = 1;
b886a2ab
RS
4220 else if (reloc_type[0] <= BFD_RELOC_UNUSED
4221 && reloc_type[1] == BFD_RELOC_UNUSED
4222 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
4223 && address_expr->X_op == O_constant)
4224 {
15be625d
CM
4225 switch (*reloc_type)
4226 {
15be625d 4227 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
4228 {
4229 int shift;
4230
4231 shift = mips_opts.micromips ? 1 : 2;
4232 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4233 as_bad (_("jump to misaligned address (0x%lx)"),
4234 (unsigned long) address_expr->X_add_number);
4235 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4236 & 0x3ffffff);
335574df 4237 ip->complete_p = 1;
df58fc94 4238 }
15be625d
CM
4239 break;
4240
4241 case BFD_RELOC_MIPS16_JMP:
4242 if ((address_expr->X_add_number & 3) != 0)
4243 as_bad (_("jump to misaligned address (0x%lx)"),
4244 (unsigned long) address_expr->X_add_number);
4245 ip->insn_opcode |=
4246 (((address_expr->X_add_number & 0x7c0000) << 3)
4247 | ((address_expr->X_add_number & 0xf800000) >> 7)
4248 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 4249 ip->complete_p = 1;
15be625d
CM
4250 break;
4251
4252 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
4253 {
4254 int shift;
4255
4256 shift = mips_opts.micromips ? 1 : 2;
4257 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4258 as_bad (_("branch to misaligned address (0x%lx)"),
4259 (unsigned long) address_expr->X_add_number);
4260 if (!mips_relax_branch)
4261 {
4262 if ((address_expr->X_add_number + (1 << (shift + 15)))
4263 & ~((1 << (shift + 16)) - 1))
4264 as_bad (_("branch address range overflow (0x%lx)"),
4265 (unsigned long) address_expr->X_add_number);
4266 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4267 & 0xffff);
4268 }
df58fc94 4269 }
15be625d
CM
4270 break;
4271
4272 default:
b886a2ab
RS
4273 {
4274 offsetT value;
4275
4276 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
4277 &value))
4278 {
4279 ip->insn_opcode |= value & 0xffff;
4280 ip->complete_p = 1;
4281 }
4282 }
4283 break;
4284 }
15be625d
CM
4285 }
4286
71400594
RS
4287 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4288 {
4289 /* There are a lot of optimizations we could do that we don't.
4290 In particular, we do not, in general, reorder instructions.
4291 If you use gcc with optimization, it will reorder
4292 instructions and generally do much more optimization then we
4293 do here; repeating all that work in the assembler would only
4294 benefit hand written assembly code, and does not seem worth
4295 it. */
4296 int nops = (mips_optimize == 0
932d1a1b
RS
4297 ? nops_for_insn (0, history, NULL)
4298 : nops_for_insn_or_target (0, history, ip));
71400594 4299 if (nops > 0)
252b5132
RH
4300 {
4301 fragS *old_frag;
4302 unsigned long old_frag_offset;
4303 int i;
252b5132
RH
4304
4305 old_frag = frag_now;
4306 old_frag_offset = frag_now_fix ();
4307
4308 for (i = 0; i < nops; i++)
14fe068b
RS
4309 add_fixed_insn (NOP_INSN);
4310 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
4311
4312 if (listing)
4313 {
4314 listing_prev_line ();
4315 /* We may be at the start of a variant frag. In case we
4316 are, make sure there is enough space for the frag
4317 after the frags created by listing_prev_line. The
4318 argument to frag_grow here must be at least as large
4319 as the argument to all other calls to frag_grow in
4320 this file. We don't have to worry about being in the
4321 middle of a variant frag, because the variants insert
4322 all needed nop instructions themselves. */
4323 frag_grow (40);
4324 }
4325
462427c4 4326 mips_move_text_labels ();
252b5132
RH
4327
4328#ifndef NO_ECOFF_DEBUGGING
4329 if (ECOFF_DEBUGGING)
4330 ecoff_fix_loc (old_frag, old_frag_offset);
4331#endif
4332 }
71400594
RS
4333 }
4334 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4335 {
932d1a1b
RS
4336 int nops;
4337
4338 /* Work out how many nops in prev_nop_frag are needed by IP,
4339 ignoring hazards generated by the first prev_nop_frag_since
4340 instructions. */
4341 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 4342 gas_assert (nops <= prev_nop_frag_holds);
252b5132 4343
71400594
RS
4344 /* Enforce NOPS as a minimum. */
4345 if (nops > prev_nop_frag_required)
4346 prev_nop_frag_required = nops;
252b5132 4347
71400594
RS
4348 if (prev_nop_frag_holds == prev_nop_frag_required)
4349 {
4350 /* Settle for the current number of nops. Update the history
4351 accordingly (for the benefit of any future .set reorder code). */
4352 prev_nop_frag = NULL;
4353 insert_into_history (prev_nop_frag_since,
4354 prev_nop_frag_holds, NOP_INSN);
4355 }
4356 else
4357 {
4358 /* Allow this instruction to replace one of the nops that was
4359 tentatively added to prev_nop_frag. */
df58fc94 4360 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
4361 prev_nop_frag_holds--;
4362 prev_nop_frag_since++;
252b5132
RH
4363 }
4364 }
4365
e407c74b 4366 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 4367 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 4368
58e2ea4d
MR
4369#ifdef OBJ_ELF
4370 /* The value passed to dwarf2_emit_insn is the distance between
4371 the beginning of the current instruction and the address that
e3a82c8e
MR
4372 should be recorded in the debug tables. This is normally the
4373 current address.
4374
df58fc94
RS
4375 For MIPS16/microMIPS debug info we want to use ISA-encoded
4376 addresses, so we use -1 for an address higher by one than the
4377 current one.
e3a82c8e
MR
4378
4379 If the instruction produced is a branch that we will swap with
4380 the preceding instruction, then we add the displacement by which
4381 the branch will be moved backwards. This is more appropriate
2309ddf2
MR
4382 and for MIPS16/microMIPS code also prevents a debugger from
4383 placing a breakpoint in the middle of the branch (and corrupting
4384 code if software breakpoints are used). */
2b0c8b40 4385 dwarf2_emit_insn ((HAVE_CODE_COMPRESSION ? -1 : 0) + branch_disp);
58e2ea4d
MR
4386#endif
4387
df58fc94
RS
4388 relax32 = (mips_relax_branch
4389 /* Don't try branch relaxation within .set nomacro, or within
4390 .set noat if we use $at for PIC computations. If it turns
4391 out that the branch was out-of-range, we'll get an error. */
4392 && !mips_opts.warn_about_macros
4393 && (mips_opts.at || mips_pic == NO_PIC)
4394 /* Don't relax BPOSGE32/64 as they have no complementing
4395 branches. */
40209cad 4396 && !(ip->insn_mo->membership & (INSN_DSP64 | INSN_DSP)));
df58fc94
RS
4397
4398 if (!HAVE_CODE_COMPRESSION
4399 && address_expr
4400 && relax32
0b25d3e6 4401 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 4402 && delayed_branch_p (ip))
4a6a3df4 4403 {
895921c9 4404 relaxed_branch = TRUE;
1e915849
RS
4405 add_relaxed_insn (ip, (relaxed_branch_length
4406 (NULL, NULL,
11625dd8
RS
4407 uncond_branch_p (ip) ? -1
4408 : branch_likely_p (ip) ? 1
1e915849
RS
4409 : 0)), 4,
4410 RELAX_BRANCH_ENCODE
66b3e8da 4411 (AT,
11625dd8
RS
4412 uncond_branch_p (ip),
4413 branch_likely_p (ip),
1e915849
RS
4414 pinfo & INSN_WRITE_GPR_31,
4415 0),
4416 address_expr->X_add_symbol,
4417 address_expr->X_add_number);
4a6a3df4
AO
4418 *reloc_type = BFD_RELOC_UNUSED;
4419 }
df58fc94
RS
4420 else if (mips_opts.micromips
4421 && address_expr
4422 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4423 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
4424 && (delayed_branch_p (ip) || compact_branch_p (ip))
4425 /* Don't try branch relaxation when users specify
4426 16-bit/32-bit instructions. */
4427 && !forced_insn_length)
df58fc94
RS
4428 {
4429 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4430 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
4431 int uncond = uncond_branch_p (ip) ? -1 : 0;
4432 int compact = compact_branch_p (ip);
df58fc94
RS
4433 int al = pinfo & INSN_WRITE_GPR_31;
4434 int length32;
4435
4436 gas_assert (address_expr != NULL);
4437 gas_assert (!mips_relax.sequence);
4438
2b0c8b40 4439 relaxed_branch = TRUE;
df58fc94
RS
4440 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4441 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
4442 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4443 relax32, 0, 0),
df58fc94
RS
4444 address_expr->X_add_symbol,
4445 address_expr->X_add_number);
4446 *reloc_type = BFD_RELOC_UNUSED;
4447 }
4448 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
4449 {
4450 /* We need to set up a variant frag. */
df58fc94 4451 gas_assert (address_expr != NULL);
1e915849
RS
4452 add_relaxed_insn (ip, 4, 0,
4453 RELAX_MIPS16_ENCODE
4454 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 4455 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 4456 delayed_branch_p (&history[0]),
1e915849
RS
4457 history[0].mips16_absolute_jump_p),
4458 make_expr_symbol (address_expr), 0);
252b5132 4459 }
5c04167a 4460 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 4461 {
11625dd8 4462 if (!delayed_branch_p (ip))
b8ee1a6e
DU
4463 /* Make sure there is enough room to swap this instruction with
4464 a following jump instruction. */
4465 frag_grow (6);
1e915849 4466 add_fixed_insn (ip);
252b5132
RH
4467 }
4468 else
4469 {
4470 if (mips_opts.mips16
4471 && mips_opts.noreorder
11625dd8 4472 && delayed_branch_p (&history[0]))
252b5132
RH
4473 as_warn (_("extended instruction in delay slot"));
4474
4d7206a2
RS
4475 if (mips_relax.sequence)
4476 {
4477 /* If we've reached the end of this frag, turn it into a variant
4478 frag and record the information for the instructions we've
4479 written so far. */
4480 if (frag_room () < 4)
4481 relax_close_frag ();
df58fc94 4482 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
4483 }
4484
584892a6 4485 if (mips_relax.sequence != 2)
df58fc94
RS
4486 {
4487 if (mips_macro_warning.first_insn_sizes[0] == 0)
4488 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4489 mips_macro_warning.sizes[0] += insn_length (ip);
4490 mips_macro_warning.insns[0]++;
4491 }
584892a6 4492 if (mips_relax.sequence != 1)
df58fc94
RS
4493 {
4494 if (mips_macro_warning.first_insn_sizes[1] == 0)
4495 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4496 mips_macro_warning.sizes[1] += insn_length (ip);
4497 mips_macro_warning.insns[1]++;
4498 }
584892a6 4499
1e915849
RS
4500 if (mips_opts.mips16)
4501 {
4502 ip->fixed_p = 1;
4503 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4504 }
4505 add_fixed_insn (ip);
252b5132
RH
4506 }
4507
9fe77896 4508 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 4509 {
df58fc94 4510 bfd_reloc_code_real_type final_type[3];
2309ddf2 4511 reloc_howto_type *howto0;
9fe77896
RS
4512 reloc_howto_type *howto;
4513 int i;
34ce925e 4514
df58fc94
RS
4515 /* Perform any necessary conversion to microMIPS relocations
4516 and find out how many relocations there actually are. */
4517 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4518 final_type[i] = micromips_map_reloc (reloc_type[i]);
4519
9fe77896
RS
4520 /* In a compound relocation, it is the final (outermost)
4521 operator that determines the relocated field. */
2309ddf2
MR
4522 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
4523
9fe77896
RS
4524 if (howto == NULL)
4525 {
4526 /* To reproduce this failure try assembling gas/testsuites/
4527 gas/mips/mips16-intermix.s with a mips-ecoff targeted
4528 assembler. */
df58fc94
RS
4529 as_bad (_("Unsupported MIPS relocation number %d"),
4530 final_type[i - 1]);
9fe77896
RS
4531 howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
4532 }
2309ddf2
MR
4533
4534 if (i > 1)
4535 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
4536 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4537 bfd_get_reloc_size (howto),
4538 address_expr,
2309ddf2
MR
4539 howto0 && howto0->pc_relative,
4540 final_type[0]);
9fe77896
RS
4541
4542 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 4543 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
4544 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4545
4546 /* These relocations can have an addend that won't fit in
4547 4 octets for 64bit assembly. */
4548 if (HAVE_64BIT_GPRS
4549 && ! howto->partial_inplace
4550 && (reloc_type[0] == BFD_RELOC_16
4551 || reloc_type[0] == BFD_RELOC_32
4552 || reloc_type[0] == BFD_RELOC_MIPS_JMP
4553 || reloc_type[0] == BFD_RELOC_GPREL16
4554 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4555 || reloc_type[0] == BFD_RELOC_GPREL32
4556 || reloc_type[0] == BFD_RELOC_64
4557 || reloc_type[0] == BFD_RELOC_CTOR
4558 || reloc_type[0] == BFD_RELOC_MIPS_SUB
4559 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4560 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4561 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4562 || reloc_type[0] == BFD_RELOC_MIPS_REL16
4563 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4564 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4565 || hi16_reloc_p (reloc_type[0])
4566 || lo16_reloc_p (reloc_type[0])))
4567 ip->fixp[0]->fx_no_overflow = 1;
4568
ddaf2c41
MR
4569 /* These relocations can have an addend that won't fit in 2 octets. */
4570 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
4571 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
4572 ip->fixp[0]->fx_no_overflow = 1;
4573
9fe77896
RS
4574 if (mips_relax.sequence)
4575 {
4576 if (mips_relax.first_fixup == 0)
4577 mips_relax.first_fixup = ip->fixp[0];
4578 }
4579 else if (reloc_needs_lo_p (*reloc_type))
4580 {
4581 struct mips_hi_fixup *hi_fixup;
4582
4583 /* Reuse the last entry if it already has a matching %lo. */
4584 hi_fixup = mips_hi_fixup_list;
4585 if (hi_fixup == 0
4586 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 4587 {
9fe77896
RS
4588 hi_fixup = ((struct mips_hi_fixup *)
4589 xmalloc (sizeof (struct mips_hi_fixup)));
4590 hi_fixup->next = mips_hi_fixup_list;
4591 mips_hi_fixup_list = hi_fixup;
4d7206a2 4592 }
9fe77896
RS
4593 hi_fixup->fixp = ip->fixp[0];
4594 hi_fixup->seg = now_seg;
4595 }
252b5132 4596
9fe77896
RS
4597 /* Add fixups for the second and third relocations, if given.
4598 Note that the ABI allows the second relocation to be
4599 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
4600 moment we only use RSS_UNDEF, but we could add support
4601 for the others if it ever becomes necessary. */
4602 for (i = 1; i < 3; i++)
4603 if (reloc_type[i] != BFD_RELOC_UNUSED)
4604 {
4605 ip->fixp[i] = fix_new (ip->frag, ip->where,
4606 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 4607 FALSE, final_type[i]);
f6688943 4608
9fe77896
RS
4609 /* Use fx_tcbit to mark compound relocs. */
4610 ip->fixp[0]->fx_tcbit = 1;
4611 ip->fixp[i]->fx_tcbit = 1;
4612 }
252b5132 4613 }
1e915849 4614 install_insn (ip);
252b5132
RH
4615
4616 /* Update the register mask information. */
4c260379
RS
4617 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4618 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 4619
a4e06468 4620 switch (method)
252b5132 4621 {
a4e06468
RS
4622 case APPEND_ADD:
4623 insert_into_history (0, 1, ip);
4624 break;
4625
4626 case APPEND_ADD_WITH_NOP:
14fe068b
RS
4627 {
4628 struct mips_cl_insn *nop;
4629
4630 insert_into_history (0, 1, ip);
4631 nop = get_delay_slot_nop (ip);
4632 add_fixed_insn (nop);
4633 insert_into_history (0, 1, nop);
4634 if (mips_relax.sequence)
4635 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4636 }
a4e06468
RS
4637 break;
4638
4639 case APPEND_ADD_COMPACT:
4640 /* Convert MIPS16 jr/jalr into a "compact" jump. */
4641 gas_assert (mips_opts.mips16);
4642 ip->insn_opcode |= 0x0080;
4643 find_altered_mips16_opcode (ip);
4644 install_insn (ip);
4645 insert_into_history (0, 1, ip);
4646 break;
4647
4648 case APPEND_SWAP:
4649 {
4650 struct mips_cl_insn delay = history[0];
4651 if (mips_opts.mips16)
4652 {
4653 know (delay.frag == ip->frag);
4654 move_insn (ip, delay.frag, delay.where);
4655 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4656 }
464ab0e5 4657 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
4658 {
4659 /* Add the delay slot instruction to the end of the
4660 current frag and shrink the fixed part of the
4661 original frag. If the branch occupies the tail of
4662 the latter, move it backwards to cover the gap. */
2b0c8b40 4663 delay.frag->fr_fix -= branch_disp;
a4e06468 4664 if (delay.frag == ip->frag)
2b0c8b40 4665 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
4666 add_fixed_insn (&delay);
4667 }
4668 else
4669 {
2b0c8b40
MR
4670 move_insn (&delay, ip->frag,
4671 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
4672 move_insn (ip, history[0].frag, history[0].where);
4673 }
4674 history[0] = *ip;
4675 delay.fixed_p = 1;
4676 insert_into_history (0, 1, &delay);
4677 }
4678 break;
252b5132
RH
4679 }
4680
13408f1e 4681 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
4682 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4683 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
4684 {
4685 unsigned int i;
4686
79850f26 4687 mips_no_prev_insn ();
13408f1e 4688
e407c74b 4689 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 4690 history[i].cleared_p = 1;
e407c74b
NC
4691 }
4692
df58fc94
RS
4693 /* We need to emit a label at the end of branch-likely macros. */
4694 if (emit_branch_likely_macro)
4695 {
4696 emit_branch_likely_macro = FALSE;
4697 micromips_add_label ();
4698 }
4699
252b5132
RH
4700 /* We just output an insn, so the next one doesn't have a label. */
4701 mips_clear_insn_labels ();
252b5132
RH
4702}
4703
e407c74b
NC
4704/* Forget that there was any previous instruction or label.
4705 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
4706
4707static void
7d10b47d 4708mips_no_prev_insn (void)
252b5132 4709{
7d10b47d
RS
4710 prev_nop_frag = NULL;
4711 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
4712 mips_clear_insn_labels ();
4713}
4714
7d10b47d
RS
4715/* This function must be called before we emit something other than
4716 instructions. It is like mips_no_prev_insn except that it inserts
4717 any NOPS that might be needed by previous instructions. */
252b5132 4718
7d10b47d
RS
4719void
4720mips_emit_delays (void)
252b5132
RH
4721{
4722 if (! mips_opts.noreorder)
4723 {
932d1a1b 4724 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
4725 if (nops > 0)
4726 {
7d10b47d
RS
4727 while (nops-- > 0)
4728 add_fixed_insn (NOP_INSN);
462427c4 4729 mips_move_text_labels ();
7d10b47d
RS
4730 }
4731 }
4732 mips_no_prev_insn ();
4733}
4734
4735/* Start a (possibly nested) noreorder block. */
4736
4737static void
4738start_noreorder (void)
4739{
4740 if (mips_opts.noreorder == 0)
4741 {
4742 unsigned int i;
4743 int nops;
4744
4745 /* None of the instructions before the .set noreorder can be moved. */
4746 for (i = 0; i < ARRAY_SIZE (history); i++)
4747 history[i].fixed_p = 1;
4748
4749 /* Insert any nops that might be needed between the .set noreorder
4750 block and the previous instructions. We will later remove any
4751 nops that turn out not to be needed. */
932d1a1b 4752 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
4753 if (nops > 0)
4754 {
4755 if (mips_optimize != 0)
252b5132
RH
4756 {
4757 /* Record the frag which holds the nop instructions, so
4758 that we can remove them if we don't need them. */
df58fc94 4759 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
4760 prev_nop_frag = frag_now;
4761 prev_nop_frag_holds = nops;
4762 prev_nop_frag_required = 0;
4763 prev_nop_frag_since = 0;
4764 }
4765
4766 for (; nops > 0; --nops)
1e915849 4767 add_fixed_insn (NOP_INSN);
252b5132 4768
7d10b47d
RS
4769 /* Move on to a new frag, so that it is safe to simply
4770 decrease the size of prev_nop_frag. */
4771 frag_wane (frag_now);
4772 frag_new (0);
462427c4 4773 mips_move_text_labels ();
252b5132 4774 }
df58fc94 4775 mips_mark_labels ();
7d10b47d 4776 mips_clear_insn_labels ();
252b5132 4777 }
7d10b47d
RS
4778 mips_opts.noreorder++;
4779 mips_any_noreorder = 1;
4780}
252b5132 4781
7d10b47d 4782/* End a nested noreorder block. */
252b5132 4783
7d10b47d
RS
4784static void
4785end_noreorder (void)
4786{
4787 mips_opts.noreorder--;
4788 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
4789 {
4790 /* Commit to inserting prev_nop_frag_required nops and go back to
4791 handling nop insertion the .set reorder way. */
4792 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 4793 * NOP_INSN_SIZE);
7d10b47d
RS
4794 insert_into_history (prev_nop_frag_since,
4795 prev_nop_frag_required, NOP_INSN);
4796 prev_nop_frag = NULL;
4797 }
252b5132
RH
4798}
4799
584892a6
RS
4800/* Set up global variables for the start of a new macro. */
4801
4802static void
4803macro_start (void)
4804{
4805 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
4806 memset (&mips_macro_warning.first_insn_sizes, 0,
4807 sizeof (mips_macro_warning.first_insn_sizes));
4808 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 4809 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 4810 && delayed_branch_p (&history[0]));
df58fc94
RS
4811 switch (history[0].insn_mo->pinfo2
4812 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
4813 {
4814 case INSN2_BRANCH_DELAY_32BIT:
4815 mips_macro_warning.delay_slot_length = 4;
4816 break;
4817 case INSN2_BRANCH_DELAY_16BIT:
4818 mips_macro_warning.delay_slot_length = 2;
4819 break;
4820 default:
4821 mips_macro_warning.delay_slot_length = 0;
4822 break;
4823 }
4824 mips_macro_warning.first_frag = NULL;
584892a6
RS
4825}
4826
df58fc94
RS
4827/* Given that a macro is longer than one instruction or of the wrong size,
4828 return the appropriate warning for it. Return null if no warning is
4829 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
4830 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
4831 and RELAX_NOMACRO. */
584892a6
RS
4832
4833static const char *
4834macro_warning (relax_substateT subtype)
4835{
4836 if (subtype & RELAX_DELAY_SLOT)
4837 return _("Macro instruction expanded into multiple instructions"
4838 " in a branch delay slot");
4839 else if (subtype & RELAX_NOMACRO)
4840 return _("Macro instruction expanded into multiple instructions");
df58fc94
RS
4841 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
4842 | RELAX_DELAY_SLOT_SIZE_SECOND))
4843 return ((subtype & RELAX_DELAY_SLOT_16BIT)
4844 ? _("Macro instruction expanded into a wrong size instruction"
4845 " in a 16-bit branch delay slot")
4846 : _("Macro instruction expanded into a wrong size instruction"
4847 " in a 32-bit branch delay slot"));
584892a6
RS
4848 else
4849 return 0;
4850}
4851
4852/* Finish up a macro. Emit warnings as appropriate. */
4853
4854static void
4855macro_end (void)
4856{
df58fc94
RS
4857 /* Relaxation warning flags. */
4858 relax_substateT subtype = 0;
4859
4860 /* Check delay slot size requirements. */
4861 if (mips_macro_warning.delay_slot_length == 2)
4862 subtype |= RELAX_DELAY_SLOT_16BIT;
4863 if (mips_macro_warning.delay_slot_length != 0)
584892a6 4864 {
df58fc94
RS
4865 if (mips_macro_warning.delay_slot_length
4866 != mips_macro_warning.first_insn_sizes[0])
4867 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
4868 if (mips_macro_warning.delay_slot_length
4869 != mips_macro_warning.first_insn_sizes[1])
4870 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
4871 }
584892a6 4872
df58fc94
RS
4873 /* Check instruction count requirements. */
4874 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
4875 {
4876 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
4877 subtype |= RELAX_SECOND_LONGER;
4878 if (mips_opts.warn_about_macros)
4879 subtype |= RELAX_NOMACRO;
4880 if (mips_macro_warning.delay_slot_p)
4881 subtype |= RELAX_DELAY_SLOT;
df58fc94 4882 }
584892a6 4883
df58fc94
RS
4884 /* If both alternatives fail to fill a delay slot correctly,
4885 emit the warning now. */
4886 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
4887 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
4888 {
4889 relax_substateT s;
4890 const char *msg;
4891
4892 s = subtype & (RELAX_DELAY_SLOT_16BIT
4893 | RELAX_DELAY_SLOT_SIZE_FIRST
4894 | RELAX_DELAY_SLOT_SIZE_SECOND);
4895 msg = macro_warning (s);
4896 if (msg != NULL)
4897 as_warn ("%s", msg);
4898 subtype &= ~s;
4899 }
4900
4901 /* If both implementations are longer than 1 instruction, then emit the
4902 warning now. */
4903 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
4904 {
4905 relax_substateT s;
4906 const char *msg;
4907
4908 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
4909 msg = macro_warning (s);
4910 if (msg != NULL)
4911 as_warn ("%s", msg);
4912 subtype &= ~s;
584892a6 4913 }
df58fc94
RS
4914
4915 /* If any flags still set, then one implementation might need a warning
4916 and the other either will need one of a different kind or none at all.
4917 Pass any remaining flags over to relaxation. */
4918 if (mips_macro_warning.first_frag != NULL)
4919 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
4920}
4921
df58fc94
RS
4922/* Instruction operand formats used in macros that vary between
4923 standard MIPS and microMIPS code. */
4924
4925static const char * const brk_fmt[2] = { "c", "mF" };
4926static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
4927static const char * const jalr_fmt[2] = { "d,s", "t,s" };
4928static const char * const lui_fmt[2] = { "t,u", "s,u" };
4929static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
4930static const char * const mfhl_fmt[2] = { "d", "mj" };
4931static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
4932static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
4933
4934#define BRK_FMT (brk_fmt[mips_opts.micromips])
4935#define COP12_FMT (cop12_fmt[mips_opts.micromips])
4936#define JALR_FMT (jalr_fmt[mips_opts.micromips])
4937#define LUI_FMT (lui_fmt[mips_opts.micromips])
4938#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
4939#define MFHL_FMT (mfhl_fmt[mips_opts.micromips])
4940#define SHFT_FMT (shft_fmt[mips_opts.micromips])
4941#define TRAP_FMT (trap_fmt[mips_opts.micromips])
4942
6e1304d8
RS
4943/* Read a macro's relocation codes from *ARGS and store them in *R.
4944 The first argument in *ARGS will be either the code for a single
4945 relocation or -1 followed by the three codes that make up a
4946 composite relocation. */
4947
4948static void
4949macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
4950{
4951 int i, next;
4952
4953 next = va_arg (*args, int);
4954 if (next >= 0)
4955 r[0] = (bfd_reloc_code_real_type) next;
4956 else
4957 for (i = 0; i < 3; i++)
4958 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
4959}
4960
252b5132
RH
4961/* Build an instruction created by a macro expansion. This is passed
4962 a pointer to the count of instructions created so far, an
4963 expression, the name of the instruction to build, an operand format
4964 string, and corresponding arguments. */
4965
252b5132 4966static void
67c0d1eb 4967macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 4968{
df58fc94 4969 const struct mips_opcode *mo = NULL;
f6688943 4970 bfd_reloc_code_real_type r[3];
df58fc94
RS
4971 const struct mips_opcode *amo;
4972 struct hash_control *hash;
4973 struct mips_cl_insn insn;
252b5132 4974 va_list args;
252b5132 4975
252b5132 4976 va_start (args, fmt);
252b5132 4977
252b5132
RH
4978 if (mips_opts.mips16)
4979 {
03ea81db 4980 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
4981 va_end (args);
4982 return;
4983 }
4984
f6688943
TS
4985 r[0] = BFD_RELOC_UNUSED;
4986 r[1] = BFD_RELOC_UNUSED;
4987 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
4988 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
4989 amo = (struct mips_opcode *) hash_find (hash, name);
4990 gas_assert (amo);
4991 gas_assert (strcmp (name, amo->name) == 0);
1e915849 4992
df58fc94 4993 do
8b082fb1
TS
4994 {
4995 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
4996 macros will never generate MDMX, MIPS-3D, or MT instructions.
4997 We try to match an instruction that fulfils the branch delay
4998 slot instruction length requirement (if any) of the previous
4999 instruction. While doing this we record the first instruction
5000 seen that matches all the other conditions and use it anyway
5001 if the requirement cannot be met; we will issue an appropriate
5002 warning later on. */
5003 if (strcmp (fmt, amo->args) == 0
5004 && amo->pinfo != INSN_MACRO
5005 && is_opcode_valid (amo)
5006 && is_size_valid (amo))
5007 {
5008 if (is_delay_slot_valid (amo))
5009 {
5010 mo = amo;
5011 break;
5012 }
5013 else if (!mo)
5014 mo = amo;
5015 }
8b082fb1 5016
df58fc94
RS
5017 ++amo;
5018 gas_assert (amo->name);
252b5132 5019 }
df58fc94 5020 while (strcmp (name, amo->name) == 0);
252b5132 5021
df58fc94 5022 gas_assert (mo);
1e915849 5023 create_insn (&insn, mo);
252b5132
RH
5024 for (;;)
5025 {
5026 switch (*fmt++)
5027 {
5028 case '\0':
5029 break;
5030
5031 case ',':
5032 case '(':
5033 case ')':
5034 continue;
5035
5f74bc13
CD
5036 case '+':
5037 switch (*fmt++)
5038 {
5039 case 'A':
5040 case 'E':
df58fc94
RS
5041 INSERT_OPERAND (mips_opts.micromips,
5042 EXTLSB, insn, va_arg (args, int));
5f74bc13
CD
5043 continue;
5044
5045 case 'B':
5046 case 'F':
5047 /* Note that in the macro case, these arguments are already
5048 in MSB form. (When handling the instruction in the
5049 non-macro case, these arguments are sizes from which
5050 MSB values must be calculated.) */
df58fc94
RS
5051 INSERT_OPERAND (mips_opts.micromips,
5052 INSMSB, insn, va_arg (args, int));
5f74bc13
CD
5053 continue;
5054
b015e599
AP
5055 case 'J':
5056 gas_assert (!mips_opts.micromips);
5057 INSERT_OPERAND (0, CODE10, insn, va_arg (args, int));
5058 continue;
5059
5f74bc13
CD
5060 case 'C':
5061 case 'G':
5062 case 'H':
5063 /* Note that in the macro case, these arguments are already
5064 in MSBD form. (When handling the instruction in the
5065 non-macro case, these arguments are sizes from which
5066 MSBD values must be calculated.) */
df58fc94
RS
5067 INSERT_OPERAND (mips_opts.micromips,
5068 EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
5069 continue;
5070
dd3cbb7e 5071 case 'Q':
df58fc94
RS
5072 gas_assert (!mips_opts.micromips);
5073 INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
dd3cbb7e
NC
5074 continue;
5075
5f74bc13 5076 default:
b37df7c4 5077 abort ();
5f74bc13
CD
5078 }
5079 continue;
5080
8b082fb1 5081 case '2':
03f66e8a 5082 INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
8b082fb1
TS
5083 continue;
5084
df58fc94
RS
5085 case 'n':
5086 gas_assert (mips_opts.micromips);
252b5132
RH
5087 case 't':
5088 case 'w':
5089 case 'E':
df58fc94 5090 INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
252b5132
RH
5091 continue;
5092
5093 case 'c':
df58fc94
RS
5094 gas_assert (!mips_opts.micromips);
5095 INSERT_OPERAND (0, CODE, insn, va_arg (args, int));
38487616
TS
5096 continue;
5097
252b5132 5098 case 'W':
df58fc94
RS
5099 gas_assert (!mips_opts.micromips);
5100 case 'T':
5101 INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
252b5132
RH
5102 continue;
5103
252b5132 5104 case 'G':
df58fc94
RS
5105 if (mips_opts.micromips)
5106 INSERT_OPERAND (1, RS, insn, va_arg (args, int));
5107 else
5108 INSERT_OPERAND (0, RD, insn, va_arg (args, int));
5109 continue;
5110
af7ee8bf 5111 case 'K':
df58fc94
RS
5112 gas_assert (!mips_opts.micromips);
5113 case 'd':
5114 INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
252b5132
RH
5115 continue;
5116
4372b673 5117 case 'U':
df58fc94 5118 gas_assert (!mips_opts.micromips);
4372b673
NC
5119 {
5120 int tmp = va_arg (args, int);
5121
df58fc94
RS
5122 INSERT_OPERAND (0, RT, insn, tmp);
5123 INSERT_OPERAND (0, RD, insn, tmp);
4372b673 5124 }
df58fc94 5125 continue;
4372b673 5126
252b5132
RH
5127 case 'V':
5128 case 'S':
df58fc94
RS
5129 gas_assert (!mips_opts.micromips);
5130 INSERT_OPERAND (0, FS, insn, va_arg (args, int));
252b5132
RH
5131 continue;
5132
5133 case 'z':
5134 continue;
5135
5136 case '<':
df58fc94
RS
5137 INSERT_OPERAND (mips_opts.micromips,
5138 SHAMT, insn, va_arg (args, int));
252b5132
RH
5139 continue;
5140
5141 case 'D':
df58fc94
RS
5142 gas_assert (!mips_opts.micromips);
5143 INSERT_OPERAND (0, FD, insn, va_arg (args, int));
252b5132
RH
5144 continue;
5145
5146 case 'B':
df58fc94
RS
5147 gas_assert (!mips_opts.micromips);
5148 INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
252b5132
RH
5149 continue;
5150
4372b673 5151 case 'J':
df58fc94
RS
5152 gas_assert (!mips_opts.micromips);
5153 INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
4372b673
NC
5154 continue;
5155
252b5132 5156 case 'q':
df58fc94
RS
5157 gas_assert (!mips_opts.micromips);
5158 INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
252b5132
RH
5159 continue;
5160
5161 case 'b':
5162 case 's':
5163 case 'r':
5164 case 'v':
df58fc94 5165 INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
252b5132
RH
5166 continue;
5167
5168 case 'i':
5169 case 'j':
6e1304d8 5170 macro_read_relocs (&args, r);
9c2799c2 5171 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
5172 || *r == BFD_RELOC_MIPS_HIGHER
5173 || *r == BFD_RELOC_HI16_S
5174 || *r == BFD_RELOC_LO16
5175 || *r == BFD_RELOC_MIPS_GOT_OFST);
5176 continue;
5177
5178 case 'o':
5179 macro_read_relocs (&args, r);
252b5132
RH
5180 continue;
5181
5182 case 'u':
6e1304d8 5183 macro_read_relocs (&args, r);
9c2799c2 5184 gas_assert (ep != NULL
90ecf173
MR
5185 && (ep->X_op == O_constant
5186 || (ep->X_op == O_symbol
5187 && (*r == BFD_RELOC_MIPS_HIGHEST
5188 || *r == BFD_RELOC_HI16_S
5189 || *r == BFD_RELOC_HI16
5190 || *r == BFD_RELOC_GPREL16
5191 || *r == BFD_RELOC_MIPS_GOT_HI16
5192 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
5193 continue;
5194
5195 case 'p':
9c2799c2 5196 gas_assert (ep != NULL);
bad36eac 5197
252b5132
RH
5198 /*
5199 * This allows macro() to pass an immediate expression for
5200 * creating short branches without creating a symbol.
bad36eac
DJ
5201 *
5202 * We don't allow branch relaxation for these branches, as
5203 * they should only appear in ".set nomacro" anyway.
252b5132
RH
5204 */
5205 if (ep->X_op == O_constant)
5206 {
df58fc94
RS
5207 /* For microMIPS we always use relocations for branches.
5208 So we should not resolve immediate values. */
5209 gas_assert (!mips_opts.micromips);
5210
bad36eac
DJ
5211 if ((ep->X_add_number & 3) != 0)
5212 as_bad (_("branch to misaligned address (0x%lx)"),
5213 (unsigned long) ep->X_add_number);
5214 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5215 as_bad (_("branch address range overflow (0x%lx)"),
5216 (unsigned long) ep->X_add_number);
252b5132
RH
5217 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5218 ep = NULL;
5219 }
5220 else
0b25d3e6 5221 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
5222 continue;
5223
5224 case 'a':
9c2799c2 5225 gas_assert (ep != NULL);
f6688943 5226 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
5227 continue;
5228
5229 case 'C':
df58fc94
RS
5230 gas_assert (!mips_opts.micromips);
5231 INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
252b5132
RH
5232 continue;
5233
d43b4baf 5234 case 'k':
df58fc94
RS
5235 INSERT_OPERAND (mips_opts.micromips,
5236 CACHE, insn, va_arg (args, unsigned long));
5237 continue;
5238
5239 case '|':
5240 gas_assert (mips_opts.micromips);
5241 INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5242 continue;
5243
5244 case '.':
5245 gas_assert (mips_opts.micromips);
5246 INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5247 continue;
5248
dec0624d
MR
5249 case '\\':
5250 INSERT_OPERAND (mips_opts.micromips,
5251 3BITPOS, insn, va_arg (args, unsigned int));
5252 continue;
5253
df58fc94 5254 case '~':
dec0624d
MR
5255 INSERT_OPERAND (mips_opts.micromips,
5256 OFFSET12, insn, va_arg (args, unsigned long));
df58fc94
RS
5257 continue;
5258
5259 case 'N':
5260 gas_assert (mips_opts.micromips);
5261 INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5262 continue;
5263
5264 case 'm': /* Opcode extension character. */
5265 gas_assert (mips_opts.micromips);
5266 switch (*fmt++)
5267 {
5268 case 'j':
5269 INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5270 break;
5271
5272 case 'p':
5273 INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5274 break;
5275
5276 case 'F':
5277 INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5278 break;
5279
5280 default:
b37df7c4 5281 abort ();
df58fc94 5282 }
d43b4baf
TS
5283 continue;
5284
252b5132 5285 default:
b37df7c4 5286 abort ();
252b5132
RH
5287 }
5288 break;
5289 }
5290 va_end (args);
9c2799c2 5291 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5292
df58fc94 5293 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5294}
5295
5296static void
67c0d1eb 5297mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 5298 va_list *args)
252b5132 5299{
1e915849 5300 struct mips_opcode *mo;
252b5132 5301 struct mips_cl_insn insn;
f6688943
TS
5302 bfd_reloc_code_real_type r[3]
5303 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 5304
1e915849 5305 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
5306 gas_assert (mo);
5307 gas_assert (strcmp (name, mo->name) == 0);
252b5132 5308
1e915849 5309 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 5310 {
1e915849 5311 ++mo;
9c2799c2
NC
5312 gas_assert (mo->name);
5313 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
5314 }
5315
1e915849 5316 create_insn (&insn, mo);
252b5132
RH
5317 for (;;)
5318 {
5319 int c;
5320
5321 c = *fmt++;
5322 switch (c)
5323 {
5324 case '\0':
5325 break;
5326
5327 case ',':
5328 case '(':
5329 case ')':
5330 continue;
5331
5332 case 'y':
5333 case 'w':
03ea81db 5334 MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
252b5132
RH
5335 continue;
5336
5337 case 'x':
5338 case 'v':
03ea81db 5339 MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
252b5132
RH
5340 continue;
5341
5342 case 'z':
03ea81db 5343 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
252b5132
RH
5344 continue;
5345
5346 case 'Z':
03ea81db 5347 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
252b5132
RH
5348 continue;
5349
5350 case '0':
5351 case 'S':
5352 case 'P':
5353 case 'R':
5354 continue;
5355
5356 case 'X':
03ea81db 5357 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
252b5132
RH
5358 continue;
5359
5360 case 'Y':
5361 {
5362 int regno;
5363
03ea81db 5364 regno = va_arg (*args, int);
252b5132 5365 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
a9e24354 5366 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
252b5132
RH
5367 }
5368 continue;
5369
5370 case '<':
5371 case '>':
5372 case '4':
5373 case '5':
5374 case 'H':
5375 case 'W':
5376 case 'D':
5377 case 'j':
5378 case '8':
5379 case 'V':
5380 case 'C':
5381 case 'U':
5382 case 'k':
5383 case 'K':
5384 case 'p':
5385 case 'q':
5386 {
b886a2ab
RS
5387 offsetT value;
5388
9c2799c2 5389 gas_assert (ep != NULL);
252b5132
RH
5390
5391 if (ep->X_op != O_constant)
874e8986 5392 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 5393 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 5394 {
b886a2ab 5395 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 5396 ep = NULL;
f6688943 5397 *r = BFD_RELOC_UNUSED;
252b5132
RH
5398 }
5399 }
5400 continue;
5401
5402 case '6':
03ea81db 5403 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
252b5132
RH
5404 continue;
5405 }
5406
5407 break;
5408 }
5409
9c2799c2 5410 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5411
df58fc94 5412 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5413}
5414
2051e8c4
MR
5415/*
5416 * Sign-extend 32-bit mode constants that have bit 31 set and all
5417 * higher bits unset.
5418 */
9f872bbe 5419static void
2051e8c4
MR
5420normalize_constant_expr (expressionS *ex)
5421{
9ee2a2d4 5422 if (ex->X_op == O_constant
2051e8c4
MR
5423 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5424 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5425 - 0x80000000);
5426}
5427
5428/*
5429 * Sign-extend 32-bit mode address offsets that have bit 31 set and
5430 * all higher bits unset.
5431 */
5432static void
5433normalize_address_expr (expressionS *ex)
5434{
5435 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5436 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5437 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5438 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5439 - 0x80000000);
5440}
5441
438c16b8
TS
5442/*
5443 * Generate a "jalr" instruction with a relocation hint to the called
5444 * function. This occurs in NewABI PIC code.
5445 */
5446static void
df58fc94 5447macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 5448{
df58fc94
RS
5449 static const bfd_reloc_code_real_type jalr_relocs[2]
5450 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5451 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5452 const char *jalr;
685736be 5453 char *f = NULL;
b34976b6 5454
1180b5a4 5455 if (MIPS_JALR_HINT_P (ep))
f21f8242 5456 {
cc3d92a5 5457 frag_grow (8);
f21f8242
AO
5458 f = frag_more (0);
5459 }
2906b037 5460 if (mips_opts.micromips)
df58fc94
RS
5461 {
5462 jalr = mips_opts.noreorder && !cprestore ? "jalr" : "jalrs";
e64af278
MR
5463 if (MIPS_JALR_HINT_P (ep)
5464 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
5465 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5466 else
5467 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5468 }
2906b037
MR
5469 else
5470 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 5471 if (MIPS_JALR_HINT_P (ep))
df58fc94 5472 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
5473}
5474
252b5132
RH
5475/*
5476 * Generate a "lui" instruction.
5477 */
5478static void
67c0d1eb 5479macro_build_lui (expressionS *ep, int regnum)
252b5132 5480{
9c2799c2 5481 gas_assert (! mips_opts.mips16);
252b5132 5482
df58fc94 5483 if (ep->X_op != O_constant)
252b5132 5484 {
9c2799c2 5485 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
5486 /* _gp_disp is a special case, used from s_cpload.
5487 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 5488 gas_assert (mips_pic == NO_PIC
78e1bb40 5489 || (! HAVE_NEWABI
aa6975fb
ILT
5490 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5491 || (! mips_in_shared
bbe506e8
TS
5492 && strcmp (S_GET_NAME (ep->X_add_symbol),
5493 "__gnu_local_gp") == 0));
252b5132
RH
5494 }
5495
df58fc94 5496 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
5497}
5498
885add95
CD
5499/* Generate a sequence of instructions to do a load or store from a constant
5500 offset off of a base register (breg) into/from a target register (treg),
5501 using AT if necessary. */
5502static void
67c0d1eb
RS
5503macro_build_ldst_constoffset (expressionS *ep, const char *op,
5504 int treg, int breg, int dbl)
885add95 5505{
9c2799c2 5506 gas_assert (ep->X_op == O_constant);
885add95 5507
256ab948 5508 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5509 if (!dbl)
5510 normalize_constant_expr (ep);
256ab948 5511
67c1ffbe 5512 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 5513 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
5514 as_warn (_("operand overflow"));
5515
5516 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5517 {
5518 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 5519 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
5520 }
5521 else
5522 {
5523 /* 32-bit offset, need multiple instructions and AT, like:
5524 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
5525 addu $tempreg,$tempreg,$breg
5526 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
5527 to handle the complete offset. */
67c0d1eb
RS
5528 macro_build_lui (ep, AT);
5529 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5530 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 5531
741fe287 5532 if (!mips_opts.at)
8fc2e39e 5533 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
5534 }
5535}
5536
252b5132
RH
5537/* set_at()
5538 * Generates code to set the $at register to true (one)
5539 * if reg is less than the immediate expression.
5540 */
5541static void
67c0d1eb 5542set_at (int reg, int unsignedp)
252b5132
RH
5543{
5544 if (imm_expr.X_op == O_constant
5545 && imm_expr.X_add_number >= -0x8000
5546 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
5547 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5548 AT, reg, BFD_RELOC_LO16);
252b5132
RH
5549 else
5550 {
67c0d1eb
RS
5551 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5552 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
5553 }
5554}
5555
5556/* Warn if an expression is not a constant. */
5557
5558static void
17a2f251 5559check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
5560{
5561 if (ex->X_op == O_big)
5562 as_bad (_("unsupported large constant"));
5563 else if (ex->X_op != O_constant)
9ee2a2d4
MR
5564 as_bad (_("Instruction %s requires absolute expression"),
5565 ip->insn_mo->name);
13757d0c 5566
9ee2a2d4
MR
5567 if (HAVE_32BIT_GPRS)
5568 normalize_constant_expr (ex);
252b5132
RH
5569}
5570
5571/* Count the leading zeroes by performing a binary chop. This is a
5572 bulky bit of source, but performance is a LOT better for the
5573 majority of values than a simple loop to count the bits:
5574 for (lcnt = 0; (lcnt < 32); lcnt++)
5575 if ((v) & (1 << (31 - lcnt)))
5576 break;
5577 However it is not code size friendly, and the gain will drop a bit
5578 on certain cached systems.
5579*/
5580#define COUNT_TOP_ZEROES(v) \
5581 (((v) & ~0xffff) == 0 \
5582 ? ((v) & ~0xff) == 0 \
5583 ? ((v) & ~0xf) == 0 \
5584 ? ((v) & ~0x3) == 0 \
5585 ? ((v) & ~0x1) == 0 \
5586 ? !(v) \
5587 ? 32 \
5588 : 31 \
5589 : 30 \
5590 : ((v) & ~0x7) == 0 \
5591 ? 29 \
5592 : 28 \
5593 : ((v) & ~0x3f) == 0 \
5594 ? ((v) & ~0x1f) == 0 \
5595 ? 27 \
5596 : 26 \
5597 : ((v) & ~0x7f) == 0 \
5598 ? 25 \
5599 : 24 \
5600 : ((v) & ~0xfff) == 0 \
5601 ? ((v) & ~0x3ff) == 0 \
5602 ? ((v) & ~0x1ff) == 0 \
5603 ? 23 \
5604 : 22 \
5605 : ((v) & ~0x7ff) == 0 \
5606 ? 21 \
5607 : 20 \
5608 : ((v) & ~0x3fff) == 0 \
5609 ? ((v) & ~0x1fff) == 0 \
5610 ? 19 \
5611 : 18 \
5612 : ((v) & ~0x7fff) == 0 \
5613 ? 17 \
5614 : 16 \
5615 : ((v) & ~0xffffff) == 0 \
5616 ? ((v) & ~0xfffff) == 0 \
5617 ? ((v) & ~0x3ffff) == 0 \
5618 ? ((v) & ~0x1ffff) == 0 \
5619 ? 15 \
5620 : 14 \
5621 : ((v) & ~0x7ffff) == 0 \
5622 ? 13 \
5623 : 12 \
5624 : ((v) & ~0x3fffff) == 0 \
5625 ? ((v) & ~0x1fffff) == 0 \
5626 ? 11 \
5627 : 10 \
5628 : ((v) & ~0x7fffff) == 0 \
5629 ? 9 \
5630 : 8 \
5631 : ((v) & ~0xfffffff) == 0 \
5632 ? ((v) & ~0x3ffffff) == 0 \
5633 ? ((v) & ~0x1ffffff) == 0 \
5634 ? 7 \
5635 : 6 \
5636 : ((v) & ~0x7ffffff) == 0 \
5637 ? 5 \
5638 : 4 \
5639 : ((v) & ~0x3fffffff) == 0 \
5640 ? ((v) & ~0x1fffffff) == 0 \
5641 ? 3 \
5642 : 2 \
5643 : ((v) & ~0x7fffffff) == 0 \
5644 ? 1 \
5645 : 0)
5646
5647/* load_register()
67c1ffbe 5648 * This routine generates the least number of instructions necessary to load
252b5132
RH
5649 * an absolute expression value into a register.
5650 */
5651static void
67c0d1eb 5652load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
5653{
5654 int freg;
5655 expressionS hi32, lo32;
5656
5657 if (ep->X_op != O_big)
5658 {
9c2799c2 5659 gas_assert (ep->X_op == O_constant);
256ab948
TS
5660
5661 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5662 if (!dbl)
5663 normalize_constant_expr (ep);
256ab948
TS
5664
5665 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
5666 {
5667 /* We can handle 16 bit signed values with an addiu to
5668 $zero. No need to ever use daddiu here, since $zero and
5669 the result are always correct in 32 bit mode. */
67c0d1eb 5670 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5671 return;
5672 }
5673 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5674 {
5675 /* We can handle 16 bit unsigned values with an ori to
5676 $zero. */
67c0d1eb 5677 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5678 return;
5679 }
256ab948 5680 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
5681 {
5682 /* 32 bit values require an lui. */
df58fc94 5683 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 5684 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 5685 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
5686 return;
5687 }
5688 }
5689
5690 /* The value is larger than 32 bits. */
5691
2051e8c4 5692 if (!dbl || HAVE_32BIT_GPRS)
252b5132 5693 {
55e08f71
NC
5694 char value[32];
5695
5696 sprintf_vma (value, ep->X_add_number);
20e1fcfd 5697 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 5698 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5699 return;
5700 }
5701
5702 if (ep->X_op != O_big)
5703 {
5704 hi32 = *ep;
5705 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5706 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5707 hi32.X_add_number &= 0xffffffff;
5708 lo32 = *ep;
5709 lo32.X_add_number &= 0xffffffff;
5710 }
5711 else
5712 {
9c2799c2 5713 gas_assert (ep->X_add_number > 2);
252b5132
RH
5714 if (ep->X_add_number == 3)
5715 generic_bignum[3] = 0;
5716 else if (ep->X_add_number > 4)
5717 as_bad (_("Number larger than 64 bits"));
5718 lo32.X_op = O_constant;
5719 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
5720 hi32.X_op = O_constant;
5721 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
5722 }
5723
5724 if (hi32.X_add_number == 0)
5725 freg = 0;
5726 else
5727 {
5728 int shift, bit;
5729 unsigned long hi, lo;
5730
956cd1d6 5731 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
5732 {
5733 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
5734 {
67c0d1eb 5735 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
5736 return;
5737 }
5738 if (lo32.X_add_number & 0x80000000)
5739 {
df58fc94 5740 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 5741 if (lo32.X_add_number & 0xffff)
67c0d1eb 5742 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
5743 return;
5744 }
5745 }
252b5132
RH
5746
5747 /* Check for 16bit shifted constant. We know that hi32 is
5748 non-zero, so start the mask on the first bit of the hi32
5749 value. */
5750 shift = 17;
5751 do
beae10d5
KH
5752 {
5753 unsigned long himask, lomask;
5754
5755 if (shift < 32)
5756 {
5757 himask = 0xffff >> (32 - shift);
5758 lomask = (0xffff << shift) & 0xffffffff;
5759 }
5760 else
5761 {
5762 himask = 0xffff << (shift - 32);
5763 lomask = 0;
5764 }
5765 if ((hi32.X_add_number & ~(offsetT) himask) == 0
5766 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
5767 {
5768 expressionS tmp;
5769
5770 tmp.X_op = O_constant;
5771 if (shift < 32)
5772 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
5773 | (lo32.X_add_number >> shift));
5774 else
5775 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 5776 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 5777 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 5778 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
5779 return;
5780 }
f9419b05 5781 ++shift;
beae10d5
KH
5782 }
5783 while (shift <= (64 - 16));
252b5132
RH
5784
5785 /* Find the bit number of the lowest one bit, and store the
5786 shifted value in hi/lo. */
5787 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
5788 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
5789 if (lo != 0)
5790 {
5791 bit = 0;
5792 while ((lo & 1) == 0)
5793 {
5794 lo >>= 1;
5795 ++bit;
5796 }
5797 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
5798 hi >>= bit;
5799 }
5800 else
5801 {
5802 bit = 32;
5803 while ((hi & 1) == 0)
5804 {
5805 hi >>= 1;
5806 ++bit;
5807 }
5808 lo = hi;
5809 hi = 0;
5810 }
5811
5812 /* Optimize if the shifted value is a (power of 2) - 1. */
5813 if ((hi == 0 && ((lo + 1) & lo) == 0)
5814 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
5815 {
5816 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 5817 if (shift != 0)
beae10d5 5818 {
252b5132
RH
5819 expressionS tmp;
5820
5821 /* This instruction will set the register to be all
5822 ones. */
beae10d5
KH
5823 tmp.X_op = O_constant;
5824 tmp.X_add_number = (offsetT) -1;
67c0d1eb 5825 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
5826 if (bit != 0)
5827 {
5828 bit += shift;
df58fc94 5829 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 5830 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 5831 }
df58fc94 5832 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 5833 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
5834 return;
5835 }
5836 }
252b5132
RH
5837
5838 /* Sign extend hi32 before calling load_register, because we can
5839 generally get better code when we load a sign extended value. */
5840 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 5841 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 5842 load_register (reg, &hi32, 0);
252b5132
RH
5843 freg = reg;
5844 }
5845 if ((lo32.X_add_number & 0xffff0000) == 0)
5846 {
5847 if (freg != 0)
5848 {
df58fc94 5849 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
5850 freg = reg;
5851 }
5852 }
5853 else
5854 {
5855 expressionS mid16;
5856
956cd1d6 5857 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 5858 {
df58fc94
RS
5859 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5860 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
5861 return;
5862 }
252b5132
RH
5863
5864 if (freg != 0)
5865 {
df58fc94 5866 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
5867 freg = reg;
5868 }
5869 mid16 = lo32;
5870 mid16.X_add_number >>= 16;
67c0d1eb 5871 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 5872 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
5873 freg = reg;
5874 }
5875 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 5876 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
5877}
5878
269137b2
TS
5879static inline void
5880load_delay_nop (void)
5881{
5882 if (!gpr_interlocks)
5883 macro_build (NULL, "nop", "");
5884}
5885
252b5132
RH
5886/* Load an address into a register. */
5887
5888static void
67c0d1eb 5889load_address (int reg, expressionS *ep, int *used_at)
252b5132 5890{
252b5132
RH
5891 if (ep->X_op != O_constant
5892 && ep->X_op != O_symbol)
5893 {
5894 as_bad (_("expression too complex"));
5895 ep->X_op = O_constant;
5896 }
5897
5898 if (ep->X_op == O_constant)
5899 {
67c0d1eb 5900 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
5901 return;
5902 }
5903
5904 if (mips_pic == NO_PIC)
5905 {
5906 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 5907 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5908 Otherwise we want
5909 lui $reg,<sym> (BFD_RELOC_HI16_S)
5910 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 5911 If we have an addend, we always use the latter form.
76b3015f 5912
d6bc6245
TS
5913 With 64bit address space and a usable $at we want
5914 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5915 lui $at,<sym> (BFD_RELOC_HI16_S)
5916 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5917 daddiu $at,<sym> (BFD_RELOC_LO16)
5918 dsll32 $reg,0
3a482fd5 5919 daddu $reg,$reg,$at
76b3015f 5920
c03099e6 5921 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
5922 on superscalar processors.
5923 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5924 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5925 dsll $reg,16
5926 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
5927 dsll $reg,16
5928 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
5929
5930 For GP relative symbols in 64bit address space we can use
5931 the same sequence as in 32bit address space. */
aed1a261 5932 if (HAVE_64BIT_SYMBOLS)
d6bc6245 5933 {
6caf9ef4
TS
5934 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5935 && !nopic_need_relax (ep->X_add_symbol, 1))
5936 {
5937 relax_start (ep->X_add_symbol);
5938 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5939 mips_gp_register, BFD_RELOC_GPREL16);
5940 relax_switch ();
5941 }
d6bc6245 5942
741fe287 5943 if (*used_at == 0 && mips_opts.at)
d6bc6245 5944 {
df58fc94
RS
5945 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5946 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
5947 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5948 BFD_RELOC_MIPS_HIGHER);
5949 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 5950 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 5951 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
5952 *used_at = 1;
5953 }
5954 else
5955 {
df58fc94 5956 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
5957 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5958 BFD_RELOC_MIPS_HIGHER);
df58fc94 5959 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 5960 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 5961 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 5962 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 5963 }
6caf9ef4
TS
5964
5965 if (mips_relax.sequence)
5966 relax_end ();
d6bc6245 5967 }
252b5132
RH
5968 else
5969 {
d6bc6245 5970 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 5971 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 5972 {
4d7206a2 5973 relax_start (ep->X_add_symbol);
67c0d1eb 5974 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 5975 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5976 relax_switch ();
d6bc6245 5977 }
67c0d1eb
RS
5978 macro_build_lui (ep, reg);
5979 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
5980 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
5981 if (mips_relax.sequence)
5982 relax_end ();
d6bc6245 5983 }
252b5132 5984 }
0a44bf69 5985 else if (!mips_big_got)
252b5132
RH
5986 {
5987 expressionS ex;
5988
5989 /* If this is a reference to an external symbol, we want
5990 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5991 Otherwise we want
5992 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5993 nop
5994 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
5995 If there is a constant, it must be added in after.
5996
ed6fb7bd 5997 If we have NewABI, we want
f5040a92
AO
5998 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5999 unless we're referencing a global symbol with a non-zero
6000 offset, in which case cst must be added separately. */
ed6fb7bd
SC
6001 if (HAVE_NEWABI)
6002 {
f5040a92
AO
6003 if (ep->X_add_number)
6004 {
4d7206a2 6005 ex.X_add_number = ep->X_add_number;
f5040a92 6006 ep->X_add_number = 0;
4d7206a2 6007 relax_start (ep->X_add_symbol);
67c0d1eb
RS
6008 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6009 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
6010 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6011 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6012 ex.X_op = O_constant;
67c0d1eb 6013 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 6014 reg, reg, BFD_RELOC_LO16);
f5040a92 6015 ep->X_add_number = ex.X_add_number;
4d7206a2 6016 relax_switch ();
f5040a92 6017 }
67c0d1eb 6018 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6019 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
6020 if (mips_relax.sequence)
6021 relax_end ();
ed6fb7bd
SC
6022 }
6023 else
6024 {
f5040a92
AO
6025 ex.X_add_number = ep->X_add_number;
6026 ep->X_add_number = 0;
67c0d1eb
RS
6027 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6028 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6029 load_delay_nop ();
4d7206a2
RS
6030 relax_start (ep->X_add_symbol);
6031 relax_switch ();
67c0d1eb 6032 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 6033 BFD_RELOC_LO16);
4d7206a2 6034 relax_end ();
ed6fb7bd 6035
f5040a92
AO
6036 if (ex.X_add_number != 0)
6037 {
6038 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6039 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6040 ex.X_op = O_constant;
67c0d1eb 6041 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 6042 reg, reg, BFD_RELOC_LO16);
f5040a92 6043 }
252b5132
RH
6044 }
6045 }
0a44bf69 6046 else if (mips_big_got)
252b5132
RH
6047 {
6048 expressionS ex;
252b5132
RH
6049
6050 /* This is the large GOT case. If this is a reference to an
6051 external symbol, we want
6052 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6053 addu $reg,$reg,$gp
6054 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
6055
6056 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
6057 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6058 nop
6059 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 6060 If there is a constant, it must be added in after.
f5040a92
AO
6061
6062 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
6063 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6064 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 6065 */
438c16b8
TS
6066 if (HAVE_NEWABI)
6067 {
4d7206a2 6068 ex.X_add_number = ep->X_add_number;
f5040a92 6069 ep->X_add_number = 0;
4d7206a2 6070 relax_start (ep->X_add_symbol);
df58fc94 6071 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6072 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6073 reg, reg, mips_gp_register);
6074 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6075 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
6076 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6077 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6078 else if (ex.X_add_number)
6079 {
6080 ex.X_op = O_constant;
67c0d1eb
RS
6081 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6082 BFD_RELOC_LO16);
f5040a92
AO
6083 }
6084
6085 ep->X_add_number = ex.X_add_number;
4d7206a2 6086 relax_switch ();
67c0d1eb 6087 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6088 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
6089 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6090 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 6091 relax_end ();
438c16b8 6092 }
252b5132 6093 else
438c16b8 6094 {
f5040a92
AO
6095 ex.X_add_number = ep->X_add_number;
6096 ep->X_add_number = 0;
4d7206a2 6097 relax_start (ep->X_add_symbol);
df58fc94 6098 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6099 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6100 reg, reg, mips_gp_register);
6101 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6102 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
6103 relax_switch ();
6104 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
6105 {
6106 /* We need a nop before loading from $gp. This special
6107 check is required because the lui which starts the main
6108 instruction stream does not refer to $gp, and so will not
6109 insert the nop which may be required. */
67c0d1eb 6110 macro_build (NULL, "nop", "");
438c16b8 6111 }
67c0d1eb 6112 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6113 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6114 load_delay_nop ();
67c0d1eb 6115 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 6116 BFD_RELOC_LO16);
4d7206a2 6117 relax_end ();
438c16b8 6118
f5040a92
AO
6119 if (ex.X_add_number != 0)
6120 {
6121 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6122 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6123 ex.X_op = O_constant;
67c0d1eb
RS
6124 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6125 BFD_RELOC_LO16);
f5040a92 6126 }
252b5132
RH
6127 }
6128 }
252b5132
RH
6129 else
6130 abort ();
8fc2e39e 6131
741fe287 6132 if (!mips_opts.at && *used_at == 1)
8fc2e39e 6133 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
6134}
6135
ea1fb5dc
RS
6136/* Move the contents of register SOURCE into register DEST. */
6137
6138static void
67c0d1eb 6139move_register (int dest, int source)
ea1fb5dc 6140{
df58fc94
RS
6141 /* Prefer to use a 16-bit microMIPS instruction unless the previous
6142 instruction specifically requires a 32-bit one. */
6143 if (mips_opts.micromips
6144 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 6145 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94
RS
6146 else
6147 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
6148 dest, source, 0);
ea1fb5dc
RS
6149}
6150
4d7206a2 6151/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
6152 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
6153 The two alternatives are:
4d7206a2
RS
6154
6155 Global symbol Local sybmol
6156 ------------- ------------
6157 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
6158 ... ...
6159 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
6160
6161 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
6162 emits the second for a 16-bit offset or add_got_offset_hilo emits
6163 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
6164
6165static void
67c0d1eb 6166load_got_offset (int dest, expressionS *local)
4d7206a2
RS
6167{
6168 expressionS global;
6169
6170 global = *local;
6171 global.X_add_number = 0;
6172
6173 relax_start (local->X_add_symbol);
67c0d1eb
RS
6174 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6175 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 6176 relax_switch ();
67c0d1eb
RS
6177 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6178 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
6179 relax_end ();
6180}
6181
6182static void
67c0d1eb 6183add_got_offset (int dest, expressionS *local)
4d7206a2
RS
6184{
6185 expressionS global;
6186
6187 global.X_op = O_constant;
6188 global.X_op_symbol = NULL;
6189 global.X_add_symbol = NULL;
6190 global.X_add_number = local->X_add_number;
6191
6192 relax_start (local->X_add_symbol);
67c0d1eb 6193 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
6194 dest, dest, BFD_RELOC_LO16);
6195 relax_switch ();
67c0d1eb 6196 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
6197 relax_end ();
6198}
6199
f6a22291
MR
6200static void
6201add_got_offset_hilo (int dest, expressionS *local, int tmp)
6202{
6203 expressionS global;
6204 int hold_mips_optimize;
6205
6206 global.X_op = O_constant;
6207 global.X_op_symbol = NULL;
6208 global.X_add_symbol = NULL;
6209 global.X_add_number = local->X_add_number;
6210
6211 relax_start (local->X_add_symbol);
6212 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6213 relax_switch ();
6214 /* Set mips_optimize around the lui instruction to avoid
6215 inserting an unnecessary nop after the lw. */
6216 hold_mips_optimize = mips_optimize;
6217 mips_optimize = 2;
6218 macro_build_lui (&global, tmp);
6219 mips_optimize = hold_mips_optimize;
6220 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6221 relax_end ();
6222
6223 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6224}
6225
df58fc94
RS
6226/* Emit a sequence of instructions to emulate a branch likely operation.
6227 BR is an ordinary branch corresponding to one to be emulated. BRNEG
6228 is its complementing branch with the original condition negated.
6229 CALL is set if the original branch specified the link operation.
6230 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6231
6232 Code like this is produced in the noreorder mode:
6233
6234 BRNEG <args>, 1f
6235 nop
6236 b <sym>
6237 delay slot (executed only if branch taken)
6238 1:
6239
6240 or, if CALL is set:
6241
6242 BRNEG <args>, 1f
6243 nop
6244 bal <sym>
6245 delay slot (executed only if branch taken)
6246 1:
6247
6248 In the reorder mode the delay slot would be filled with a nop anyway,
6249 so code produced is simply:
6250
6251 BR <args>, <sym>
6252 nop
6253
6254 This function is used when producing code for the microMIPS ASE that
6255 does not implement branch likely instructions in hardware. */
6256
6257static void
6258macro_build_branch_likely (const char *br, const char *brneg,
6259 int call, expressionS *ep, const char *fmt,
6260 unsigned int sreg, unsigned int treg)
6261{
6262 int noreorder = mips_opts.noreorder;
6263 expressionS expr1;
6264
6265 gas_assert (mips_opts.micromips);
6266 start_noreorder ();
6267 if (noreorder)
6268 {
6269 micromips_label_expr (&expr1);
6270 macro_build (&expr1, brneg, fmt, sreg, treg);
6271 macro_build (NULL, "nop", "");
6272 macro_build (ep, call ? "bal" : "b", "p");
6273
6274 /* Set to true so that append_insn adds a label. */
6275 emit_branch_likely_macro = TRUE;
6276 }
6277 else
6278 {
6279 macro_build (ep, br, fmt, sreg, treg);
6280 macro_build (NULL, "nop", "");
6281 }
6282 end_noreorder ();
6283}
6284
6285/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6286 the condition code tested. EP specifies the branch target. */
6287
6288static void
6289macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6290{
6291 const int call = 0;
6292 const char *brneg;
6293 const char *br;
6294
6295 switch (type)
6296 {
6297 case M_BC1FL:
6298 br = "bc1f";
6299 brneg = "bc1t";
6300 break;
6301 case M_BC1TL:
6302 br = "bc1t";
6303 brneg = "bc1f";
6304 break;
6305 case M_BC2FL:
6306 br = "bc2f";
6307 brneg = "bc2t";
6308 break;
6309 case M_BC2TL:
6310 br = "bc2t";
6311 brneg = "bc2f";
6312 break;
6313 default:
6314 abort ();
6315 }
6316 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6317}
6318
6319/* Emit a two-argument branch macro specified by TYPE, using SREG as
6320 the register tested. EP specifies the branch target. */
6321
6322static void
6323macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6324{
6325 const char *brneg = NULL;
6326 const char *br;
6327 int call = 0;
6328
6329 switch (type)
6330 {
6331 case M_BGEZ:
6332 br = "bgez";
6333 break;
6334 case M_BGEZL:
6335 br = mips_opts.micromips ? "bgez" : "bgezl";
6336 brneg = "bltz";
6337 break;
6338 case M_BGEZALL:
6339 gas_assert (mips_opts.micromips);
6340 br = "bgezals";
6341 brneg = "bltz";
6342 call = 1;
6343 break;
6344 case M_BGTZ:
6345 br = "bgtz";
6346 break;
6347 case M_BGTZL:
6348 br = mips_opts.micromips ? "bgtz" : "bgtzl";
6349 brneg = "blez";
6350 break;
6351 case M_BLEZ:
6352 br = "blez";
6353 break;
6354 case M_BLEZL:
6355 br = mips_opts.micromips ? "blez" : "blezl";
6356 brneg = "bgtz";
6357 break;
6358 case M_BLTZ:
6359 br = "bltz";
6360 break;
6361 case M_BLTZL:
6362 br = mips_opts.micromips ? "bltz" : "bltzl";
6363 brneg = "bgez";
6364 break;
6365 case M_BLTZALL:
6366 gas_assert (mips_opts.micromips);
6367 br = "bltzals";
6368 brneg = "bgez";
6369 call = 1;
6370 break;
6371 default:
6372 abort ();
6373 }
6374 if (mips_opts.micromips && brneg)
6375 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6376 else
6377 macro_build (ep, br, "s,p", sreg);
6378}
6379
6380/* Emit a three-argument branch macro specified by TYPE, using SREG and
6381 TREG as the registers tested. EP specifies the branch target. */
6382
6383static void
6384macro_build_branch_rsrt (int type, expressionS *ep,
6385 unsigned int sreg, unsigned int treg)
6386{
6387 const char *brneg = NULL;
6388 const int call = 0;
6389 const char *br;
6390
6391 switch (type)
6392 {
6393 case M_BEQ:
6394 case M_BEQ_I:
6395 br = "beq";
6396 break;
6397 case M_BEQL:
6398 case M_BEQL_I:
6399 br = mips_opts.micromips ? "beq" : "beql";
6400 brneg = "bne";
6401 break;
6402 case M_BNE:
6403 case M_BNE_I:
6404 br = "bne";
6405 break;
6406 case M_BNEL:
6407 case M_BNEL_I:
6408 br = mips_opts.micromips ? "bne" : "bnel";
6409 brneg = "beq";
6410 break;
6411 default:
6412 abort ();
6413 }
6414 if (mips_opts.micromips && brneg)
6415 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6416 else
6417 macro_build (ep, br, "s,t,p", sreg, treg);
6418}
6419
252b5132
RH
6420/*
6421 * Build macros
6422 * This routine implements the seemingly endless macro or synthesized
6423 * instructions and addressing modes in the mips assembly language. Many
6424 * of these macros are simple and are similar to each other. These could
67c1ffbe 6425 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
6426 * this verbose method. Others are not simple macros but are more like
6427 * optimizing code generation.
6428 * One interesting optimization is when several store macros appear
67c1ffbe 6429 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
6430 * The ensuing load upper instructions are ommited. This implies some kind
6431 * of global optimization. We currently only optimize within a single macro.
6432 * For many of the load and store macros if the address is specified as a
6433 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6434 * first load register 'at' with zero and use it as the base register. The
6435 * mips assembler simply uses register $zero. Just one tiny optimization
6436 * we're missing.
6437 */
6438static void
17a2f251 6439macro (struct mips_cl_insn *ip)
252b5132 6440{
741fe287
MR
6441 unsigned int treg, sreg, dreg, breg;
6442 unsigned int tempreg;
252b5132 6443 int mask;
43841e91 6444 int used_at = 0;
df58fc94 6445 expressionS label_expr;
252b5132 6446 expressionS expr1;
df58fc94 6447 expressionS *ep;
252b5132
RH
6448 const char *s;
6449 const char *s2;
6450 const char *fmt;
6451 int likely = 0;
252b5132 6452 int coproc = 0;
df58fc94 6453 int off12 = 0;
1abe91b1 6454 int call = 0;
df58fc94
RS
6455 int jals = 0;
6456 int dbl = 0;
6457 int imm = 0;
6458 int ust = 0;
6459 int lp = 0;
6460 int ab = 0;
dd6a37e7 6461 int off0 = 0;
252b5132 6462 int off;
67c0d1eb 6463 offsetT maxnum;
252b5132 6464 bfd_reloc_code_real_type r;
252b5132
RH
6465 int hold_mips_optimize;
6466
9c2799c2 6467 gas_assert (! mips_opts.mips16);
252b5132 6468
df58fc94
RS
6469 treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6470 dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6471 sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
252b5132
RH
6472 mask = ip->insn_mo->mask;
6473
df58fc94
RS
6474 label_expr.X_op = O_constant;
6475 label_expr.X_op_symbol = NULL;
6476 label_expr.X_add_symbol = NULL;
6477 label_expr.X_add_number = 0;
6478
252b5132
RH
6479 expr1.X_op = O_constant;
6480 expr1.X_op_symbol = NULL;
6481 expr1.X_add_symbol = NULL;
6482 expr1.X_add_number = 1;
6483
6484 switch (mask)
6485 {
6486 case M_DABS:
6487 dbl = 1;
6488 case M_ABS:
df58fc94
RS
6489 /* bgez $a0,1f
6490 move v0,$a0
6491 sub v0,$zero,$a0
6492 1:
6493 */
252b5132 6494
7d10b47d 6495 start_noreorder ();
252b5132 6496
df58fc94
RS
6497 if (mips_opts.micromips)
6498 micromips_label_expr (&label_expr);
6499 else
6500 label_expr.X_add_number = 8;
6501 macro_build (&label_expr, "bgez", "s,p", sreg);
252b5132 6502 if (dreg == sreg)
a605d2b3 6503 macro_build (NULL, "nop", "");
252b5132 6504 else
67c0d1eb
RS
6505 move_register (dreg, sreg);
6506 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
df58fc94
RS
6507 if (mips_opts.micromips)
6508 micromips_add_label ();
252b5132 6509
7d10b47d 6510 end_noreorder ();
8fc2e39e 6511 break;
252b5132
RH
6512
6513 case M_ADD_I:
6514 s = "addi";
6515 s2 = "add";
6516 goto do_addi;
6517 case M_ADDU_I:
6518 s = "addiu";
6519 s2 = "addu";
6520 goto do_addi;
6521 case M_DADD_I:
6522 dbl = 1;
6523 s = "daddi";
6524 s2 = "dadd";
df58fc94
RS
6525 if (!mips_opts.micromips)
6526 goto do_addi;
6527 if (imm_expr.X_op == O_constant
6528 && imm_expr.X_add_number >= -0x200
6529 && imm_expr.X_add_number < 0x200)
6530 {
6531 macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6532 break;
6533 }
6534 goto do_addi_i;
252b5132
RH
6535 case M_DADDU_I:
6536 dbl = 1;
6537 s = "daddiu";
6538 s2 = "daddu";
6539 do_addi:
6540 if (imm_expr.X_op == O_constant
6541 && imm_expr.X_add_number >= -0x8000
6542 && imm_expr.X_add_number < 0x8000)
6543 {
67c0d1eb 6544 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 6545 break;
252b5132 6546 }
df58fc94 6547 do_addi_i:
8fc2e39e 6548 used_at = 1;
67c0d1eb
RS
6549 load_register (AT, &imm_expr, dbl);
6550 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6551 break;
6552
6553 case M_AND_I:
6554 s = "andi";
6555 s2 = "and";
6556 goto do_bit;
6557 case M_OR_I:
6558 s = "ori";
6559 s2 = "or";
6560 goto do_bit;
6561 case M_NOR_I:
6562 s = "";
6563 s2 = "nor";
6564 goto do_bit;
6565 case M_XOR_I:
6566 s = "xori";
6567 s2 = "xor";
6568 do_bit:
6569 if (imm_expr.X_op == O_constant
6570 && imm_expr.X_add_number >= 0
6571 && imm_expr.X_add_number < 0x10000)
6572 {
6573 if (mask != M_NOR_I)
67c0d1eb 6574 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
6575 else
6576 {
67c0d1eb
RS
6577 macro_build (&imm_expr, "ori", "t,r,i",
6578 treg, sreg, BFD_RELOC_LO16);
6579 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 6580 }
8fc2e39e 6581 break;
252b5132
RH
6582 }
6583
8fc2e39e 6584 used_at = 1;
67c0d1eb
RS
6585 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6586 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6587 break;
6588
8b082fb1
TS
6589 case M_BALIGN:
6590 switch (imm_expr.X_add_number)
6591 {
6592 case 0:
6593 macro_build (NULL, "nop", "");
6594 break;
6595 case 2:
6596 macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6597 break;
03f66e8a
MR
6598 case 1:
6599 case 3:
8b082fb1 6600 macro_build (NULL, "balign", "t,s,2", treg, sreg,
90ecf173 6601 (int) imm_expr.X_add_number);
8b082fb1 6602 break;
03f66e8a
MR
6603 default:
6604 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6605 (unsigned long) imm_expr.X_add_number);
6606 break;
8b082fb1
TS
6607 }
6608 break;
6609
df58fc94
RS
6610 case M_BC1FL:
6611 case M_BC1TL:
6612 case M_BC2FL:
6613 case M_BC2TL:
6614 gas_assert (mips_opts.micromips);
6615 macro_build_branch_ccl (mask, &offset_expr,
6616 EXTRACT_OPERAND (1, BCC, *ip));
6617 break;
6618
252b5132 6619 case M_BEQ_I:
252b5132 6620 case M_BEQL_I:
252b5132 6621 case M_BNE_I:
252b5132 6622 case M_BNEL_I:
252b5132 6623 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
6624 treg = 0;
6625 else
252b5132 6626 {
df58fc94
RS
6627 treg = AT;
6628 used_at = 1;
6629 load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
252b5132 6630 }
df58fc94
RS
6631 /* Fall through. */
6632 case M_BEQL:
6633 case M_BNEL:
6634 macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
252b5132
RH
6635 break;
6636
6637 case M_BGEL:
6638 likely = 1;
6639 case M_BGE:
6640 if (treg == 0)
df58fc94
RS
6641 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6642 else if (sreg == 0)
6643 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6644 else
252b5132 6645 {
df58fc94
RS
6646 used_at = 1;
6647 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6648 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6649 &offset_expr, AT, ZERO);
252b5132 6650 }
df58fc94
RS
6651 break;
6652
6653 case M_BGEZL:
6654 case M_BGEZALL:
6655 case M_BGTZL:
6656 case M_BLEZL:
6657 case M_BLTZL:
6658 case M_BLTZALL:
6659 macro_build_branch_rs (mask, &offset_expr, sreg);
252b5132
RH
6660 break;
6661
6662 case M_BGTL_I:
6663 likely = 1;
6664 case M_BGT_I:
90ecf173 6665 /* Check for > max integer. */
252b5132 6666 maxnum = 0x7fffffff;
ca4e0257 6667 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6668 {
6669 maxnum <<= 16;
6670 maxnum |= 0xffff;
6671 maxnum <<= 16;
6672 maxnum |= 0xffff;
6673 }
6674 if (imm_expr.X_op == O_constant
6675 && imm_expr.X_add_number >= maxnum
ca4e0257 6676 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6677 {
6678 do_false:
90ecf173 6679 /* Result is always false. */
252b5132 6680 if (! likely)
a605d2b3 6681 macro_build (NULL, "nop", "");
252b5132 6682 else
df58fc94 6683 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 6684 break;
252b5132
RH
6685 }
6686 if (imm_expr.X_op != O_constant)
6687 as_bad (_("Unsupported large constant"));
f9419b05 6688 ++imm_expr.X_add_number;
252b5132
RH
6689 /* FALLTHROUGH */
6690 case M_BGE_I:
6691 case M_BGEL_I:
6692 if (mask == M_BGEL_I)
6693 likely = 1;
6694 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6695 {
df58fc94
RS
6696 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
6697 &offset_expr, sreg);
8fc2e39e 6698 break;
252b5132
RH
6699 }
6700 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6701 {
df58fc94
RS
6702 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
6703 &offset_expr, sreg);
8fc2e39e 6704 break;
252b5132
RH
6705 }
6706 maxnum = 0x7fffffff;
ca4e0257 6707 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6708 {
6709 maxnum <<= 16;
6710 maxnum |= 0xffff;
6711 maxnum <<= 16;
6712 maxnum |= 0xffff;
6713 }
6714 maxnum = - maxnum - 1;
6715 if (imm_expr.X_op == O_constant
6716 && imm_expr.X_add_number <= maxnum
ca4e0257 6717 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6718 {
6719 do_true:
6720 /* result is always true */
6721 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 6722 macro_build (&offset_expr, "b", "p");
8fc2e39e 6723 break;
252b5132 6724 }
8fc2e39e 6725 used_at = 1;
67c0d1eb 6726 set_at (sreg, 0);
df58fc94
RS
6727 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6728 &offset_expr, AT, ZERO);
252b5132
RH
6729 break;
6730
6731 case M_BGEUL:
6732 likely = 1;
6733 case M_BGEU:
6734 if (treg == 0)
6735 goto do_true;
df58fc94
RS
6736 else if (sreg == 0)
6737 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6738 &offset_expr, ZERO, treg);
6739 else
252b5132 6740 {
df58fc94
RS
6741 used_at = 1;
6742 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6743 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6744 &offset_expr, AT, ZERO);
252b5132 6745 }
252b5132
RH
6746 break;
6747
6748 case M_BGTUL_I:
6749 likely = 1;
6750 case M_BGTU_I:
6751 if (sreg == 0
ca4e0257 6752 || (HAVE_32BIT_GPRS
252b5132 6753 && imm_expr.X_op == O_constant
f01dc953 6754 && imm_expr.X_add_number == -1))
252b5132
RH
6755 goto do_false;
6756 if (imm_expr.X_op != O_constant)
6757 as_bad (_("Unsupported large constant"));
f9419b05 6758 ++imm_expr.X_add_number;
252b5132
RH
6759 /* FALLTHROUGH */
6760 case M_BGEU_I:
6761 case M_BGEUL_I:
6762 if (mask == M_BGEUL_I)
6763 likely = 1;
6764 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6765 goto do_true;
df58fc94
RS
6766 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6767 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6768 &offset_expr, sreg, ZERO);
6769 else
252b5132 6770 {
df58fc94
RS
6771 used_at = 1;
6772 set_at (sreg, 1);
6773 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6774 &offset_expr, AT, ZERO);
252b5132 6775 }
252b5132
RH
6776 break;
6777
6778 case M_BGTL:
6779 likely = 1;
6780 case M_BGT:
6781 if (treg == 0)
df58fc94
RS
6782 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
6783 else if (sreg == 0)
6784 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
6785 else
252b5132 6786 {
df58fc94
RS
6787 used_at = 1;
6788 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6789 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6790 &offset_expr, AT, ZERO);
252b5132 6791 }
252b5132
RH
6792 break;
6793
6794 case M_BGTUL:
6795 likely = 1;
6796 case M_BGTU:
6797 if (treg == 0)
df58fc94
RS
6798 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6799 &offset_expr, sreg, ZERO);
6800 else if (sreg == 0)
6801 goto do_false;
6802 else
252b5132 6803 {
df58fc94
RS
6804 used_at = 1;
6805 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6806 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6807 &offset_expr, AT, ZERO);
252b5132 6808 }
252b5132
RH
6809 break;
6810
6811 case M_BLEL:
6812 likely = 1;
6813 case M_BLE:
6814 if (treg == 0)
df58fc94
RS
6815 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6816 else if (sreg == 0)
6817 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
6818 else
252b5132 6819 {
df58fc94
RS
6820 used_at = 1;
6821 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6822 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6823 &offset_expr, AT, ZERO);
252b5132 6824 }
252b5132
RH
6825 break;
6826
6827 case M_BLEL_I:
6828 likely = 1;
6829 case M_BLE_I:
6830 maxnum = 0x7fffffff;
ca4e0257 6831 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6832 {
6833 maxnum <<= 16;
6834 maxnum |= 0xffff;
6835 maxnum <<= 16;
6836 maxnum |= 0xffff;
6837 }
6838 if (imm_expr.X_op == O_constant
6839 && imm_expr.X_add_number >= maxnum
ca4e0257 6840 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6841 goto do_true;
6842 if (imm_expr.X_op != O_constant)
6843 as_bad (_("Unsupported large constant"));
f9419b05 6844 ++imm_expr.X_add_number;
252b5132
RH
6845 /* FALLTHROUGH */
6846 case M_BLT_I:
6847 case M_BLTL_I:
6848 if (mask == M_BLTL_I)
6849 likely = 1;
6850 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
6851 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6852 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6853 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6854 else
252b5132 6855 {
df58fc94
RS
6856 used_at = 1;
6857 set_at (sreg, 0);
6858 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6859 &offset_expr, AT, ZERO);
252b5132 6860 }
252b5132
RH
6861 break;
6862
6863 case M_BLEUL:
6864 likely = 1;
6865 case M_BLEU:
6866 if (treg == 0)
df58fc94
RS
6867 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6868 &offset_expr, sreg, ZERO);
6869 else if (sreg == 0)
6870 goto do_true;
6871 else
252b5132 6872 {
df58fc94
RS
6873 used_at = 1;
6874 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6875 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6876 &offset_expr, AT, ZERO);
252b5132 6877 }
252b5132
RH
6878 break;
6879
6880 case M_BLEUL_I:
6881 likely = 1;
6882 case M_BLEU_I:
6883 if (sreg == 0
ca4e0257 6884 || (HAVE_32BIT_GPRS
252b5132 6885 && imm_expr.X_op == O_constant
f01dc953 6886 && imm_expr.X_add_number == -1))
252b5132
RH
6887 goto do_true;
6888 if (imm_expr.X_op != O_constant)
6889 as_bad (_("Unsupported large constant"));
f9419b05 6890 ++imm_expr.X_add_number;
252b5132
RH
6891 /* FALLTHROUGH */
6892 case M_BLTU_I:
6893 case M_BLTUL_I:
6894 if (mask == M_BLTUL_I)
6895 likely = 1;
6896 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6897 goto do_false;
df58fc94
RS
6898 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6899 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6900 &offset_expr, sreg, ZERO);
6901 else
252b5132 6902 {
df58fc94
RS
6903 used_at = 1;
6904 set_at (sreg, 1);
6905 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6906 &offset_expr, AT, ZERO);
252b5132 6907 }
252b5132
RH
6908 break;
6909
6910 case M_BLTL:
6911 likely = 1;
6912 case M_BLT:
6913 if (treg == 0)
df58fc94
RS
6914 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6915 else if (sreg == 0)
6916 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
6917 else
252b5132 6918 {
df58fc94
RS
6919 used_at = 1;
6920 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6921 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6922 &offset_expr, AT, ZERO);
252b5132 6923 }
252b5132
RH
6924 break;
6925
6926 case M_BLTUL:
6927 likely = 1;
6928 case M_BLTU:
6929 if (treg == 0)
6930 goto do_false;
df58fc94
RS
6931 else if (sreg == 0)
6932 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6933 &offset_expr, ZERO, treg);
6934 else
252b5132 6935 {
df58fc94
RS
6936 used_at = 1;
6937 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6938 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6939 &offset_expr, AT, ZERO);
252b5132 6940 }
252b5132
RH
6941 break;
6942
5f74bc13
CD
6943 case M_DEXT:
6944 {
d5818fca
MR
6945 /* Use unsigned arithmetic. */
6946 addressT pos;
6947 addressT size;
5f74bc13 6948
90ecf173 6949 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
6950 {
6951 as_bad (_("Unsupported large constant"));
6952 pos = size = 1;
6953 }
6954 else
6955 {
d5818fca
MR
6956 pos = imm_expr.X_add_number;
6957 size = imm2_expr.X_add_number;
5f74bc13
CD
6958 }
6959
6960 if (pos > 63)
6961 {
d5818fca 6962 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
6963 pos = 1;
6964 }
90ecf173 6965 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
6966 {
6967 as_bad (_("Improper extract size (%lu, position %lu)"),
d5818fca 6968 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
6969 size = 1;
6970 }
6971
6972 if (size <= 32 && pos < 32)
6973 {
6974 s = "dext";
6975 fmt = "t,r,+A,+C";
6976 }
6977 else if (size <= 32)
6978 {
6979 s = "dextu";
6980 fmt = "t,r,+E,+H";
6981 }
6982 else
6983 {
6984 s = "dextm";
6985 fmt = "t,r,+A,+G";
6986 }
d5818fca
MR
6987 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6988 (int) (size - 1));
5f74bc13 6989 }
8fc2e39e 6990 break;
5f74bc13
CD
6991
6992 case M_DINS:
6993 {
d5818fca
MR
6994 /* Use unsigned arithmetic. */
6995 addressT pos;
6996 addressT size;
5f74bc13 6997
90ecf173 6998 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
6999 {
7000 as_bad (_("Unsupported large constant"));
7001 pos = size = 1;
7002 }
7003 else
7004 {
d5818fca
MR
7005 pos = imm_expr.X_add_number;
7006 size = imm2_expr.X_add_number;
5f74bc13
CD
7007 }
7008
7009 if (pos > 63)
7010 {
d5818fca 7011 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
7012 pos = 1;
7013 }
90ecf173 7014 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
7015 {
7016 as_bad (_("Improper insert size (%lu, position %lu)"),
d5818fca 7017 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
7018 size = 1;
7019 }
7020
7021 if (pos < 32 && (pos + size - 1) < 32)
7022 {
7023 s = "dins";
7024 fmt = "t,r,+A,+B";
7025 }
7026 else if (pos >= 32)
7027 {
7028 s = "dinsu";
7029 fmt = "t,r,+E,+F";
7030 }
7031 else
7032 {
7033 s = "dinsm";
7034 fmt = "t,r,+A,+F";
7035 }
750bdd57
AS
7036 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
7037 (int) (pos + size - 1));
5f74bc13 7038 }
8fc2e39e 7039 break;
5f74bc13 7040
252b5132
RH
7041 case M_DDIV_3:
7042 dbl = 1;
7043 case M_DIV_3:
7044 s = "mflo";
7045 goto do_div3;
7046 case M_DREM_3:
7047 dbl = 1;
7048 case M_REM_3:
7049 s = "mfhi";
7050 do_div3:
7051 if (treg == 0)
7052 {
7053 as_warn (_("Divide by zero."));
7054 if (mips_trap)
df58fc94 7055 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 7056 else
df58fc94 7057 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 7058 break;
252b5132
RH
7059 }
7060
7d10b47d 7061 start_noreorder ();
252b5132
RH
7062 if (mips_trap)
7063 {
df58fc94 7064 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 7065 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
7066 }
7067 else
7068 {
df58fc94
RS
7069 if (mips_opts.micromips)
7070 micromips_label_expr (&label_expr);
7071 else
7072 label_expr.X_add_number = 8;
7073 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 7074 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
df58fc94
RS
7075 macro_build (NULL, "break", BRK_FMT, 7);
7076 if (mips_opts.micromips)
7077 micromips_add_label ();
252b5132
RH
7078 }
7079 expr1.X_add_number = -1;
8fc2e39e 7080 used_at = 1;
f6a22291 7081 load_register (AT, &expr1, dbl);
df58fc94
RS
7082 if (mips_opts.micromips)
7083 micromips_label_expr (&label_expr);
7084 else
7085 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
7086 macro_build (&label_expr, "bne", "s,t,p", treg, AT);
252b5132
RH
7087 if (dbl)
7088 {
7089 expr1.X_add_number = 1;
f6a22291 7090 load_register (AT, &expr1, dbl);
df58fc94 7091 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
7092 }
7093 else
7094 {
7095 expr1.X_add_number = 0x80000000;
df58fc94 7096 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
7097 }
7098 if (mips_trap)
7099 {
df58fc94 7100 macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
252b5132
RH
7101 /* We want to close the noreorder block as soon as possible, so
7102 that later insns are available for delay slot filling. */
7d10b47d 7103 end_noreorder ();
252b5132
RH
7104 }
7105 else
7106 {
df58fc94
RS
7107 if (mips_opts.micromips)
7108 micromips_label_expr (&label_expr);
7109 else
7110 label_expr.X_add_number = 8;
7111 macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
a605d2b3 7112 macro_build (NULL, "nop", "");
252b5132
RH
7113
7114 /* We want to close the noreorder block as soon as possible, so
7115 that later insns are available for delay slot filling. */
7d10b47d 7116 end_noreorder ();
252b5132 7117
df58fc94 7118 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 7119 }
df58fc94
RS
7120 if (mips_opts.micromips)
7121 micromips_add_label ();
7122 macro_build (NULL, s, MFHL_FMT, dreg);
252b5132
RH
7123 break;
7124
7125 case M_DIV_3I:
7126 s = "div";
7127 s2 = "mflo";
7128 goto do_divi;
7129 case M_DIVU_3I:
7130 s = "divu";
7131 s2 = "mflo";
7132 goto do_divi;
7133 case M_REM_3I:
7134 s = "div";
7135 s2 = "mfhi";
7136 goto do_divi;
7137 case M_REMU_3I:
7138 s = "divu";
7139 s2 = "mfhi";
7140 goto do_divi;
7141 case M_DDIV_3I:
7142 dbl = 1;
7143 s = "ddiv";
7144 s2 = "mflo";
7145 goto do_divi;
7146 case M_DDIVU_3I:
7147 dbl = 1;
7148 s = "ddivu";
7149 s2 = "mflo";
7150 goto do_divi;
7151 case M_DREM_3I:
7152 dbl = 1;
7153 s = "ddiv";
7154 s2 = "mfhi";
7155 goto do_divi;
7156 case M_DREMU_3I:
7157 dbl = 1;
7158 s = "ddivu";
7159 s2 = "mfhi";
7160 do_divi:
7161 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7162 {
7163 as_warn (_("Divide by zero."));
7164 if (mips_trap)
df58fc94 7165 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 7166 else
df58fc94 7167 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 7168 break;
252b5132
RH
7169 }
7170 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7171 {
7172 if (strcmp (s2, "mflo") == 0)
67c0d1eb 7173 move_register (dreg, sreg);
252b5132 7174 else
c80c840e 7175 move_register (dreg, ZERO);
8fc2e39e 7176 break;
252b5132
RH
7177 }
7178 if (imm_expr.X_op == O_constant
7179 && imm_expr.X_add_number == -1
7180 && s[strlen (s) - 1] != 'u')
7181 {
7182 if (strcmp (s2, "mflo") == 0)
7183 {
67c0d1eb 7184 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
7185 }
7186 else
c80c840e 7187 move_register (dreg, ZERO);
8fc2e39e 7188 break;
252b5132
RH
7189 }
7190
8fc2e39e 7191 used_at = 1;
67c0d1eb
RS
7192 load_register (AT, &imm_expr, dbl);
7193 macro_build (NULL, s, "z,s,t", sreg, AT);
df58fc94 7194 macro_build (NULL, s2, MFHL_FMT, dreg);
252b5132
RH
7195 break;
7196
7197 case M_DIVU_3:
7198 s = "divu";
7199 s2 = "mflo";
7200 goto do_divu3;
7201 case M_REMU_3:
7202 s = "divu";
7203 s2 = "mfhi";
7204 goto do_divu3;
7205 case M_DDIVU_3:
7206 s = "ddivu";
7207 s2 = "mflo";
7208 goto do_divu3;
7209 case M_DREMU_3:
7210 s = "ddivu";
7211 s2 = "mfhi";
7212 do_divu3:
7d10b47d 7213 start_noreorder ();
252b5132
RH
7214 if (mips_trap)
7215 {
df58fc94 7216 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 7217 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7218 /* We want to close the noreorder block as soon as possible, so
7219 that later insns are available for delay slot filling. */
7d10b47d 7220 end_noreorder ();
252b5132
RH
7221 }
7222 else
7223 {
df58fc94
RS
7224 if (mips_opts.micromips)
7225 micromips_label_expr (&label_expr);
7226 else
7227 label_expr.X_add_number = 8;
7228 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 7229 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7230
7231 /* We want to close the noreorder block as soon as possible, so
7232 that later insns are available for delay slot filling. */
7d10b47d 7233 end_noreorder ();
df58fc94
RS
7234 macro_build (NULL, "break", BRK_FMT, 7);
7235 if (mips_opts.micromips)
7236 micromips_add_label ();
252b5132 7237 }
df58fc94 7238 macro_build (NULL, s2, MFHL_FMT, dreg);
8fc2e39e 7239 break;
252b5132 7240
1abe91b1
MR
7241 case M_DLCA_AB:
7242 dbl = 1;
7243 case M_LCA_AB:
7244 call = 1;
7245 goto do_la;
252b5132
RH
7246 case M_DLA_AB:
7247 dbl = 1;
7248 case M_LA_AB:
1abe91b1 7249 do_la:
252b5132
RH
7250 /* Load the address of a symbol into a register. If breg is not
7251 zero, we then add a base register to it. */
7252
3bec30a8
TS
7253 if (dbl && HAVE_32BIT_GPRS)
7254 as_warn (_("dla used to load 32-bit register"));
7255
90ecf173 7256 if (!dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
7257 as_warn (_("la used to load 64-bit address"));
7258
0c11417f
MR
7259 if (offset_expr.X_op == O_constant
7260 && offset_expr.X_add_number >= -0x8000
7261 && offset_expr.X_add_number < 0x8000)
7262 {
aed1a261 7263 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 7264 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 7265 break;
0c11417f
MR
7266 }
7267
741fe287 7268 if (mips_opts.at && (treg == breg))
afdbd6d0
CD
7269 {
7270 tempreg = AT;
7271 used_at = 1;
7272 }
7273 else
7274 {
7275 tempreg = treg;
afdbd6d0
CD
7276 }
7277
252b5132
RH
7278 if (offset_expr.X_op != O_symbol
7279 && offset_expr.X_op != O_constant)
7280 {
f71d0d44 7281 as_bad (_("Expression too complex"));
252b5132
RH
7282 offset_expr.X_op = O_constant;
7283 }
7284
252b5132 7285 if (offset_expr.X_op == O_constant)
aed1a261 7286 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
7287 else if (mips_pic == NO_PIC)
7288 {
d6bc6245 7289 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 7290 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
7291 Otherwise we want
7292 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
7293 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7294 If we have a constant, we need two instructions anyhow,
d6bc6245 7295 so we may as well always use the latter form.
76b3015f 7296
6caf9ef4
TS
7297 With 64bit address space and a usable $at we want
7298 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7299 lui $at,<sym> (BFD_RELOC_HI16_S)
7300 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7301 daddiu $at,<sym> (BFD_RELOC_LO16)
7302 dsll32 $tempreg,0
7303 daddu $tempreg,$tempreg,$at
7304
7305 If $at is already in use, we use a path which is suboptimal
7306 on superscalar processors.
7307 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7308 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7309 dsll $tempreg,16
7310 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
7311 dsll $tempreg,16
7312 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
7313
7314 For GP relative symbols in 64bit address space we can use
7315 the same sequence as in 32bit address space. */
aed1a261 7316 if (HAVE_64BIT_SYMBOLS)
252b5132 7317 {
6caf9ef4
TS
7318 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7319 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7320 {
7321 relax_start (offset_expr.X_add_symbol);
7322 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7323 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7324 relax_switch ();
7325 }
d6bc6245 7326
741fe287 7327 if (used_at == 0 && mips_opts.at)
98d3f06f 7328 {
df58fc94 7329 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7330 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 7331 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7332 AT, BFD_RELOC_HI16_S);
67c0d1eb 7333 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7334 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 7335 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7336 AT, AT, BFD_RELOC_LO16);
df58fc94 7337 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 7338 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
7339 used_at = 1;
7340 }
7341 else
7342 {
df58fc94 7343 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7344 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 7345 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7346 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 7347 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7348 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7349 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 7350 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7351 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7352 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 7353 }
6caf9ef4
TS
7354
7355 if (mips_relax.sequence)
7356 relax_end ();
98d3f06f
KH
7357 }
7358 else
7359 {
7360 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 7361 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 7362 {
4d7206a2 7363 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7364 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7365 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 7366 relax_switch ();
98d3f06f 7367 }
6943caf0 7368 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
f71d0d44 7369 as_bad (_("Offset too large"));
67c0d1eb
RS
7370 macro_build_lui (&offset_expr, tempreg);
7371 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7372 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
7373 if (mips_relax.sequence)
7374 relax_end ();
98d3f06f 7375 }
252b5132 7376 }
0a44bf69 7377 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 7378 {
9117d219
NC
7379 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7380
252b5132
RH
7381 /* If this is a reference to an external symbol, and there
7382 is no constant, we want
7383 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 7384 or for lca or if tempreg is PIC_CALL_REG
9117d219 7385 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
7386 For a local symbol, we want
7387 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7388 nop
7389 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7390
7391 If we have a small constant, and this is a reference to
7392 an external symbol, we want
7393 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7394 nop
7395 addiu $tempreg,$tempreg,<constant>
7396 For a local symbol, we want the same instruction
7397 sequence, but we output a BFD_RELOC_LO16 reloc on the
7398 addiu instruction.
7399
7400 If we have a large constant, and this is a reference to
7401 an external symbol, we want
7402 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7403 lui $at,<hiconstant>
7404 addiu $at,$at,<loconstant>
7405 addu $tempreg,$tempreg,$at
7406 For a local symbol, we want the same instruction
7407 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 7408 addiu instruction.
ed6fb7bd
SC
7409 */
7410
4d7206a2 7411 if (offset_expr.X_add_number == 0)
252b5132 7412 {
0a44bf69
RS
7413 if (mips_pic == SVR4_PIC
7414 && breg == 0
7415 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
7416 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7417
7418 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7419 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7420 lw_reloc_type, mips_gp_register);
4d7206a2 7421 if (breg != 0)
252b5132
RH
7422 {
7423 /* We're going to put in an addu instruction using
7424 tempreg, so we may as well insert the nop right
7425 now. */
269137b2 7426 load_delay_nop ();
252b5132 7427 }
4d7206a2 7428 relax_switch ();
67c0d1eb
RS
7429 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7430 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7431 load_delay_nop ();
67c0d1eb
RS
7432 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7433 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 7434 relax_end ();
252b5132
RH
7435 /* FIXME: If breg == 0, and the next instruction uses
7436 $tempreg, then if this variant case is used an extra
7437 nop will be generated. */
7438 }
4d7206a2
RS
7439 else if (offset_expr.X_add_number >= -0x8000
7440 && offset_expr.X_add_number < 0x8000)
252b5132 7441 {
67c0d1eb 7442 load_got_offset (tempreg, &offset_expr);
269137b2 7443 load_delay_nop ();
67c0d1eb 7444 add_got_offset (tempreg, &offset_expr);
252b5132
RH
7445 }
7446 else
7447 {
4d7206a2
RS
7448 expr1.X_add_number = offset_expr.X_add_number;
7449 offset_expr.X_add_number =
43c0598f 7450 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 7451 load_got_offset (tempreg, &offset_expr);
f6a22291 7452 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
7453 /* If we are going to add in a base register, and the
7454 target register and the base register are the same,
7455 then we are using AT as a temporary register. Since
7456 we want to load the constant into AT, we add our
7457 current AT (from the global offset table) and the
7458 register into the register now, and pretend we were
7459 not using a base register. */
67c0d1eb 7460 if (breg == treg)
252b5132 7461 {
269137b2 7462 load_delay_nop ();
67c0d1eb 7463 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7464 treg, AT, breg);
252b5132
RH
7465 breg = 0;
7466 tempreg = treg;
252b5132 7467 }
f6a22291 7468 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
7469 used_at = 1;
7470 }
7471 }
0a44bf69 7472 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 7473 {
67c0d1eb 7474 int add_breg_early = 0;
f5040a92
AO
7475
7476 /* If this is a reference to an external, and there is no
7477 constant, or local symbol (*), with or without a
7478 constant, we want
7479 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 7480 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
7481 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7482
7483 If we have a small constant, and this is a reference to
7484 an external symbol, we want
7485 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7486 addiu $tempreg,$tempreg,<constant>
7487
7488 If we have a large constant, and this is a reference to
7489 an external symbol, we want
7490 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7491 lui $at,<hiconstant>
7492 addiu $at,$at,<loconstant>
7493 addu $tempreg,$tempreg,$at
7494
7495 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7496 local symbols, even though it introduces an additional
7497 instruction. */
7498
f5040a92
AO
7499 if (offset_expr.X_add_number)
7500 {
4d7206a2 7501 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
7502 offset_expr.X_add_number = 0;
7503
4d7206a2 7504 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7505 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7506 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7507
7508 if (expr1.X_add_number >= -0x8000
7509 && expr1.X_add_number < 0x8000)
7510 {
67c0d1eb
RS
7511 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7512 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 7513 }
ecd13cd3 7514 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 7515 {
f5040a92
AO
7516 /* If we are going to add in a base register, and the
7517 target register and the base register are the same,
7518 then we are using AT as a temporary register. Since
7519 we want to load the constant into AT, we add our
7520 current AT (from the global offset table) and the
7521 register into the register now, and pretend we were
7522 not using a base register. */
7523 if (breg != treg)
7524 dreg = tempreg;
7525 else
7526 {
9c2799c2 7527 gas_assert (tempreg == AT);
67c0d1eb
RS
7528 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7529 treg, AT, breg);
f5040a92 7530 dreg = treg;
67c0d1eb 7531 add_breg_early = 1;
f5040a92
AO
7532 }
7533
f6a22291 7534 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7535 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7536 dreg, dreg, AT);
f5040a92 7537
f5040a92
AO
7538 used_at = 1;
7539 }
7540 else
7541 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7542
4d7206a2 7543 relax_switch ();
f5040a92
AO
7544 offset_expr.X_add_number = expr1.X_add_number;
7545
67c0d1eb
RS
7546 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7547 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7548 if (add_breg_early)
f5040a92 7549 {
67c0d1eb 7550 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 7551 treg, tempreg, breg);
f5040a92
AO
7552 breg = 0;
7553 tempreg = treg;
7554 }
4d7206a2 7555 relax_end ();
f5040a92 7556 }
4d7206a2 7557 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 7558 {
4d7206a2 7559 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7560 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7561 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 7562 relax_switch ();
67c0d1eb
RS
7563 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7564 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 7565 relax_end ();
f5040a92 7566 }
4d7206a2 7567 else
f5040a92 7568 {
67c0d1eb
RS
7569 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7570 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7571 }
7572 }
0a44bf69 7573 else if (mips_big_got && !HAVE_NEWABI)
252b5132 7574 {
67c0d1eb 7575 int gpdelay;
9117d219
NC
7576 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7577 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 7578 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
7579
7580 /* This is the large GOT case. If this is a reference to an
7581 external symbol, and there is no constant, we want
7582 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7583 addu $tempreg,$tempreg,$gp
7584 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 7585 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
7586 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7587 addu $tempreg,$tempreg,$gp
7588 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
7589 For a local symbol, we want
7590 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7591 nop
7592 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7593
7594 If we have a small constant, and this is a reference to
7595 an external symbol, we want
7596 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7597 addu $tempreg,$tempreg,$gp
7598 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7599 nop
7600 addiu $tempreg,$tempreg,<constant>
7601 For a local symbol, we want
7602 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7603 nop
7604 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7605
7606 If we have a large constant, and this is a reference to
7607 an external symbol, we want
7608 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7609 addu $tempreg,$tempreg,$gp
7610 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7611 lui $at,<hiconstant>
7612 addiu $at,$at,<loconstant>
7613 addu $tempreg,$tempreg,$at
7614 For a local symbol, we want
7615 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7616 lui $at,<hiconstant>
7617 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
7618 addu $tempreg,$tempreg,$at
f5040a92 7619 */
438c16b8 7620
252b5132
RH
7621 expr1.X_add_number = offset_expr.X_add_number;
7622 offset_expr.X_add_number = 0;
4d7206a2 7623 relax_start (offset_expr.X_add_symbol);
67c0d1eb 7624 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
7625 if (expr1.X_add_number == 0 && breg == 0
7626 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
7627 {
7628 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7629 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7630 }
df58fc94 7631 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 7632 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7633 tempreg, tempreg, mips_gp_register);
67c0d1eb 7634 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 7635 tempreg, lw_reloc_type, tempreg);
252b5132
RH
7636 if (expr1.X_add_number == 0)
7637 {
67c0d1eb 7638 if (breg != 0)
252b5132
RH
7639 {
7640 /* We're going to put in an addu instruction using
7641 tempreg, so we may as well insert the nop right
7642 now. */
269137b2 7643 load_delay_nop ();
252b5132 7644 }
252b5132
RH
7645 }
7646 else if (expr1.X_add_number >= -0x8000
7647 && expr1.X_add_number < 0x8000)
7648 {
269137b2 7649 load_delay_nop ();
67c0d1eb 7650 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7651 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
7652 }
7653 else
7654 {
252b5132
RH
7655 /* If we are going to add in a base register, and the
7656 target register and the base register are the same,
7657 then we are using AT as a temporary register. Since
7658 we want to load the constant into AT, we add our
7659 current AT (from the global offset table) and the
7660 register into the register now, and pretend we were
7661 not using a base register. */
7662 if (breg != treg)
67c0d1eb 7663 dreg = tempreg;
252b5132
RH
7664 else
7665 {
9c2799c2 7666 gas_assert (tempreg == AT);
269137b2 7667 load_delay_nop ();
67c0d1eb 7668 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7669 treg, AT, breg);
252b5132 7670 dreg = treg;
252b5132
RH
7671 }
7672
f6a22291 7673 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7674 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 7675
252b5132
RH
7676 used_at = 1;
7677 }
43c0598f 7678 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 7679 relax_switch ();
252b5132 7680
67c0d1eb 7681 if (gpdelay)
252b5132
RH
7682 {
7683 /* This is needed because this instruction uses $gp, but
f5040a92 7684 the first instruction on the main stream does not. */
67c0d1eb 7685 macro_build (NULL, "nop", "");
252b5132 7686 }
ed6fb7bd 7687
67c0d1eb
RS
7688 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7689 local_reloc_type, mips_gp_register);
f5040a92 7690 if (expr1.X_add_number >= -0x8000
252b5132
RH
7691 && expr1.X_add_number < 0x8000)
7692 {
269137b2 7693 load_delay_nop ();
67c0d1eb
RS
7694 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7695 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 7696 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
7697 register, the external symbol case ended with a load,
7698 so if the symbol turns out to not be external, and
7699 the next instruction uses tempreg, an unnecessary nop
7700 will be inserted. */
252b5132
RH
7701 }
7702 else
7703 {
7704 if (breg == treg)
7705 {
7706 /* We must add in the base register now, as in the
f5040a92 7707 external symbol case. */
9c2799c2 7708 gas_assert (tempreg == AT);
269137b2 7709 load_delay_nop ();
67c0d1eb 7710 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7711 treg, AT, breg);
252b5132
RH
7712 tempreg = treg;
7713 /* We set breg to 0 because we have arranged to add
f5040a92 7714 it in in both cases. */
252b5132
RH
7715 breg = 0;
7716 }
7717
67c0d1eb
RS
7718 macro_build_lui (&expr1, AT);
7719 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7720 AT, AT, BFD_RELOC_LO16);
67c0d1eb 7721 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7722 tempreg, tempreg, AT);
8fc2e39e 7723 used_at = 1;
252b5132 7724 }
4d7206a2 7725 relax_end ();
252b5132 7726 }
0a44bf69 7727 else if (mips_big_got && HAVE_NEWABI)
f5040a92 7728 {
f5040a92
AO
7729 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7730 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 7731 int add_breg_early = 0;
f5040a92
AO
7732
7733 /* This is the large GOT case. If this is a reference to an
7734 external symbol, and there is no constant, we want
7735 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7736 add $tempreg,$tempreg,$gp
7737 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 7738 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
7739 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7740 add $tempreg,$tempreg,$gp
7741 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7742
7743 If we have a small constant, and this is a reference to
7744 an external symbol, we want
7745 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7746 add $tempreg,$tempreg,$gp
7747 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7748 addi $tempreg,$tempreg,<constant>
7749
7750 If we have a large constant, and this is a reference to
7751 an external symbol, we want
7752 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7753 addu $tempreg,$tempreg,$gp
7754 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7755 lui $at,<hiconstant>
7756 addi $at,$at,<loconstant>
7757 add $tempreg,$tempreg,$at
7758
7759 If we have NewABI, and we know it's a local symbol, we want
7760 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
7761 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
7762 otherwise we have to resort to GOT_HI16/GOT_LO16. */
7763
4d7206a2 7764 relax_start (offset_expr.X_add_symbol);
f5040a92 7765
4d7206a2 7766 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
7767 offset_expr.X_add_number = 0;
7768
1abe91b1
MR
7769 if (expr1.X_add_number == 0 && breg == 0
7770 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
7771 {
7772 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7773 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7774 }
df58fc94 7775 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 7776 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7777 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
7778 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7779 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
7780
7781 if (expr1.X_add_number == 0)
4d7206a2 7782 ;
f5040a92
AO
7783 else if (expr1.X_add_number >= -0x8000
7784 && expr1.X_add_number < 0x8000)
7785 {
67c0d1eb 7786 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7787 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 7788 }
ecd13cd3 7789 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 7790 {
f5040a92
AO
7791 /* If we are going to add in a base register, and the
7792 target register and the base register are the same,
7793 then we are using AT as a temporary register. Since
7794 we want to load the constant into AT, we add our
7795 current AT (from the global offset table) and the
7796 register into the register now, and pretend we were
7797 not using a base register. */
7798 if (breg != treg)
7799 dreg = tempreg;
7800 else
7801 {
9c2799c2 7802 gas_assert (tempreg == AT);
67c0d1eb 7803 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7804 treg, AT, breg);
f5040a92 7805 dreg = treg;
67c0d1eb 7806 add_breg_early = 1;
f5040a92
AO
7807 }
7808
f6a22291 7809 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7810 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 7811
f5040a92
AO
7812 used_at = 1;
7813 }
7814 else
7815 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7816
4d7206a2 7817 relax_switch ();
f5040a92 7818 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
7819 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7820 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
7821 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
7822 tempreg, BFD_RELOC_MIPS_GOT_OFST);
7823 if (add_breg_early)
f5040a92 7824 {
67c0d1eb 7825 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7826 treg, tempreg, breg);
f5040a92
AO
7827 breg = 0;
7828 tempreg = treg;
7829 }
4d7206a2 7830 relax_end ();
f5040a92 7831 }
252b5132
RH
7832 else
7833 abort ();
7834
7835 if (breg != 0)
aed1a261 7836 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
7837 break;
7838
52b6b6b9 7839 case M_MSGSND:
df58fc94 7840 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7841 {
7842 unsigned long temp = (treg << 16) | (0x01);
7843 macro_build (NULL, "c2", "C", temp);
7844 }
c7af4273 7845 break;
52b6b6b9
JM
7846
7847 case M_MSGLD:
df58fc94 7848 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7849 {
7850 unsigned long temp = (0x02);
7851 macro_build (NULL, "c2", "C", temp);
7852 }
c7af4273 7853 break;
52b6b6b9
JM
7854
7855 case M_MSGLD_T:
df58fc94 7856 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7857 {
7858 unsigned long temp = (treg << 16) | (0x02);
7859 macro_build (NULL, "c2", "C", temp);
7860 }
c7af4273 7861 break;
52b6b6b9
JM
7862
7863 case M_MSGWAIT:
df58fc94 7864 gas_assert (!mips_opts.micromips);
52b6b6b9 7865 macro_build (NULL, "c2", "C", 3);
c7af4273 7866 break;
52b6b6b9
JM
7867
7868 case M_MSGWAIT_T:
df58fc94 7869 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7870 {
7871 unsigned long temp = (treg << 16) | 0x03;
7872 macro_build (NULL, "c2", "C", temp);
7873 }
c7af4273 7874 break;
52b6b6b9 7875
252b5132
RH
7876 case M_J_A:
7877 /* The j instruction may not be used in PIC code, since it
7878 requires an absolute address. We convert it to a b
7879 instruction. */
7880 if (mips_pic == NO_PIC)
67c0d1eb 7881 macro_build (&offset_expr, "j", "a");
252b5132 7882 else
67c0d1eb 7883 macro_build (&offset_expr, "b", "p");
8fc2e39e 7884 break;
252b5132
RH
7885
7886 /* The jal instructions must be handled as macros because when
7887 generating PIC code they expand to multi-instruction
7888 sequences. Normally they are simple instructions. */
df58fc94
RS
7889 case M_JALS_1:
7890 dreg = RA;
7891 /* Fall through. */
7892 case M_JALS_2:
7893 gas_assert (mips_opts.micromips);
7894 jals = 1;
7895 goto jal;
252b5132
RH
7896 case M_JAL_1:
7897 dreg = RA;
7898 /* Fall through. */
7899 case M_JAL_2:
df58fc94 7900 jal:
3e722fb5 7901 if (mips_pic == NO_PIC)
df58fc94
RS
7902 {
7903 s = jals ? "jalrs" : "jalr";
e64af278
MR
7904 if (mips_opts.micromips
7905 && dreg == RA
7906 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
7907 macro_build (NULL, s, "mj", sreg);
7908 else
7909 macro_build (NULL, s, JALR_FMT, dreg, sreg);
7910 }
0a44bf69 7911 else
252b5132 7912 {
df58fc94
RS
7913 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
7914 && mips_cprestore_offset >= 0);
7915
252b5132
RH
7916 if (sreg != PIC_CALL_REG)
7917 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 7918
df58fc94
RS
7919 s = (mips_opts.micromips && (!mips_opts.noreorder || cprestore)
7920 ? "jalrs" : "jalr");
e64af278
MR
7921 if (mips_opts.micromips
7922 && dreg == RA
7923 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
7924 macro_build (NULL, s, "mj", sreg);
7925 else
7926 macro_build (NULL, s, JALR_FMT, dreg, sreg);
0a44bf69 7927 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 7928 {
6478892d
TS
7929 if (mips_cprestore_offset < 0)
7930 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7931 else
7932 {
90ecf173 7933 if (!mips_frame_reg_valid)
7a621144
DJ
7934 {
7935 as_warn (_("No .frame pseudo-op used in PIC code"));
7936 /* Quiet this warning. */
7937 mips_frame_reg_valid = 1;
7938 }
90ecf173 7939 if (!mips_cprestore_valid)
7a621144
DJ
7940 {
7941 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7942 /* Quiet this warning. */
7943 mips_cprestore_valid = 1;
7944 }
d3fca0b5
MR
7945 if (mips_opts.noreorder)
7946 macro_build (NULL, "nop", "");
6478892d 7947 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 7948 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 7949 mips_gp_register,
256ab948
TS
7950 mips_frame_reg,
7951 HAVE_64BIT_ADDRESSES);
6478892d 7952 }
252b5132
RH
7953 }
7954 }
252b5132 7955
8fc2e39e 7956 break;
252b5132 7957
df58fc94
RS
7958 case M_JALS_A:
7959 gas_assert (mips_opts.micromips);
7960 jals = 1;
7961 /* Fall through. */
252b5132
RH
7962 case M_JAL_A:
7963 if (mips_pic == NO_PIC)
df58fc94 7964 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
7965 else if (mips_pic == SVR4_PIC)
7966 {
7967 /* If this is a reference to an external symbol, and we are
7968 using a small GOT, we want
7969 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7970 nop
f9419b05 7971 jalr $ra,$25
252b5132
RH
7972 nop
7973 lw $gp,cprestore($sp)
7974 The cprestore value is set using the .cprestore
7975 pseudo-op. If we are using a big GOT, we want
7976 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7977 addu $25,$25,$gp
7978 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
7979 nop
f9419b05 7980 jalr $ra,$25
252b5132
RH
7981 nop
7982 lw $gp,cprestore($sp)
7983 If the symbol is not external, we want
7984 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7985 nop
7986 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 7987 jalr $ra,$25
252b5132 7988 nop
438c16b8 7989 lw $gp,cprestore($sp)
f5040a92
AO
7990
7991 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
7992 sequences above, minus nops, unless the symbol is local,
7993 which enables us to use GOT_PAGE/GOT_OFST (big got) or
7994 GOT_DISP. */
438c16b8 7995 if (HAVE_NEWABI)
252b5132 7996 {
90ecf173 7997 if (!mips_big_got)
f5040a92 7998 {
4d7206a2 7999 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
8000 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8001 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 8002 mips_gp_register);
4d7206a2 8003 relax_switch ();
67c0d1eb
RS
8004 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8005 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
8006 mips_gp_register);
8007 relax_end ();
f5040a92
AO
8008 }
8009 else
8010 {
4d7206a2 8011 relax_start (offset_expr.X_add_symbol);
df58fc94 8012 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
8013 BFD_RELOC_MIPS_CALL_HI16);
8014 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8015 PIC_CALL_REG, mips_gp_register);
8016 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8017 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8018 PIC_CALL_REG);
4d7206a2 8019 relax_switch ();
67c0d1eb
RS
8020 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8021 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
8022 mips_gp_register);
8023 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8024 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 8025 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 8026 relax_end ();
f5040a92 8027 }
684022ea 8028
df58fc94 8029 macro_build_jalr (&offset_expr, 0);
252b5132
RH
8030 }
8031 else
8032 {
4d7206a2 8033 relax_start (offset_expr.X_add_symbol);
90ecf173 8034 if (!mips_big_got)
438c16b8 8035 {
67c0d1eb
RS
8036 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8037 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 8038 mips_gp_register);
269137b2 8039 load_delay_nop ();
4d7206a2 8040 relax_switch ();
438c16b8 8041 }
252b5132 8042 else
252b5132 8043 {
67c0d1eb
RS
8044 int gpdelay;
8045
8046 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 8047 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
8048 BFD_RELOC_MIPS_CALL_HI16);
8049 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8050 PIC_CALL_REG, mips_gp_register);
8051 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8052 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8053 PIC_CALL_REG);
269137b2 8054 load_delay_nop ();
4d7206a2 8055 relax_switch ();
67c0d1eb
RS
8056 if (gpdelay)
8057 macro_build (NULL, "nop", "");
252b5132 8058 }
67c0d1eb
RS
8059 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8060 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 8061 mips_gp_register);
269137b2 8062 load_delay_nop ();
67c0d1eb
RS
8063 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8064 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 8065 relax_end ();
df58fc94 8066 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 8067
6478892d
TS
8068 if (mips_cprestore_offset < 0)
8069 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8070 else
8071 {
90ecf173 8072 if (!mips_frame_reg_valid)
7a621144
DJ
8073 {
8074 as_warn (_("No .frame pseudo-op used in PIC code"));
8075 /* Quiet this warning. */
8076 mips_frame_reg_valid = 1;
8077 }
90ecf173 8078 if (!mips_cprestore_valid)
7a621144
DJ
8079 {
8080 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8081 /* Quiet this warning. */
8082 mips_cprestore_valid = 1;
8083 }
6478892d 8084 if (mips_opts.noreorder)
67c0d1eb 8085 macro_build (NULL, "nop", "");
6478892d 8086 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 8087 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 8088 mips_gp_register,
256ab948
TS
8089 mips_frame_reg,
8090 HAVE_64BIT_ADDRESSES);
6478892d 8091 }
252b5132
RH
8092 }
8093 }
0a44bf69
RS
8094 else if (mips_pic == VXWORKS_PIC)
8095 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
8096 else
8097 abort ();
8098
8fc2e39e 8099 break;
252b5132 8100
dec0624d
MR
8101 case M_ACLR_AB:
8102 ab = 1;
8103 case M_ACLR_OB:
8104 s = "aclr";
8105 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8106 fmt = "\\,~(b)";
8107 off12 = 1;
8108 goto ld_st;
8109 case M_ASET_AB:
8110 ab = 1;
8111 case M_ASET_OB:
8112 s = "aset";
8113 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8114 fmt = "\\,~(b)";
8115 off12 = 1;
8116 goto ld_st;
252b5132 8117 case M_LB_AB:
df58fc94 8118 ab = 1;
252b5132 8119 s = "lb";
df58fc94 8120 fmt = "t,o(b)";
252b5132
RH
8121 goto ld;
8122 case M_LBU_AB:
df58fc94 8123 ab = 1;
252b5132 8124 s = "lbu";
df58fc94 8125 fmt = "t,o(b)";
252b5132
RH
8126 goto ld;
8127 case M_LH_AB:
df58fc94 8128 ab = 1;
252b5132 8129 s = "lh";
df58fc94 8130 fmt = "t,o(b)";
252b5132
RH
8131 goto ld;
8132 case M_LHU_AB:
df58fc94 8133 ab = 1;
252b5132 8134 s = "lhu";
df58fc94 8135 fmt = "t,o(b)";
252b5132
RH
8136 goto ld;
8137 case M_LW_AB:
df58fc94 8138 ab = 1;
252b5132 8139 s = "lw";
df58fc94 8140 fmt = "t,o(b)";
252b5132
RH
8141 goto ld;
8142 case M_LWC0_AB:
df58fc94
RS
8143 ab = 1;
8144 gas_assert (!mips_opts.micromips);
252b5132 8145 s = "lwc0";
df58fc94 8146 fmt = "E,o(b)";
bdaaa2e1 8147 /* Itbl support may require additional care here. */
252b5132 8148 coproc = 1;
df58fc94 8149 goto ld_st;
252b5132 8150 case M_LWC1_AB:
df58fc94 8151 ab = 1;
252b5132 8152 s = "lwc1";
df58fc94 8153 fmt = "T,o(b)";
bdaaa2e1 8154 /* Itbl support may require additional care here. */
252b5132 8155 coproc = 1;
df58fc94 8156 goto ld_st;
252b5132 8157 case M_LWC2_AB:
df58fc94
RS
8158 ab = 1;
8159 case M_LWC2_OB:
252b5132 8160 s = "lwc2";
df58fc94
RS
8161 fmt = COP12_FMT;
8162 off12 = mips_opts.micromips;
bdaaa2e1 8163 /* Itbl support may require additional care here. */
252b5132 8164 coproc = 1;
df58fc94 8165 goto ld_st;
252b5132 8166 case M_LWC3_AB:
df58fc94
RS
8167 ab = 1;
8168 gas_assert (!mips_opts.micromips);
252b5132 8169 s = "lwc3";
df58fc94 8170 fmt = "E,o(b)";
bdaaa2e1 8171 /* Itbl support may require additional care here. */
252b5132 8172 coproc = 1;
df58fc94 8173 goto ld_st;
252b5132 8174 case M_LWL_AB:
df58fc94
RS
8175 ab = 1;
8176 case M_LWL_OB:
252b5132 8177 s = "lwl";
df58fc94
RS
8178 fmt = MEM12_FMT;
8179 off12 = mips_opts.micromips;
8180 goto ld_st;
252b5132 8181 case M_LWR_AB:
df58fc94
RS
8182 ab = 1;
8183 case M_LWR_OB:
252b5132 8184 s = "lwr";
df58fc94
RS
8185 fmt = MEM12_FMT;
8186 off12 = mips_opts.micromips;
8187 goto ld_st;
252b5132 8188 case M_LDC1_AB:
df58fc94 8189 ab = 1;
252b5132 8190 s = "ldc1";
df58fc94 8191 fmt = "T,o(b)";
bdaaa2e1 8192 /* Itbl support may require additional care here. */
252b5132 8193 coproc = 1;
df58fc94 8194 goto ld_st;
252b5132 8195 case M_LDC2_AB:
df58fc94
RS
8196 ab = 1;
8197 case M_LDC2_OB:
252b5132 8198 s = "ldc2";
df58fc94
RS
8199 fmt = COP12_FMT;
8200 off12 = mips_opts.micromips;
bdaaa2e1 8201 /* Itbl support may require additional care here. */
252b5132 8202 coproc = 1;
df58fc94 8203 goto ld_st;
c77c0862
RS
8204 case M_LQC2_AB:
8205 ab = 1;
8206 s = "lqc2";
8207 fmt = "E,o(b)";
8208 /* Itbl support may require additional care here. */
8209 coproc = 1;
8210 goto ld_st;
252b5132 8211 case M_LDC3_AB:
df58fc94 8212 ab = 1;
252b5132 8213 s = "ldc3";
df58fc94 8214 fmt = "E,o(b)";
bdaaa2e1 8215 /* Itbl support may require additional care here. */
252b5132 8216 coproc = 1;
df58fc94 8217 goto ld_st;
252b5132 8218 case M_LDL_AB:
df58fc94
RS
8219 ab = 1;
8220 case M_LDL_OB:
252b5132 8221 s = "ldl";
df58fc94
RS
8222 fmt = MEM12_FMT;
8223 off12 = mips_opts.micromips;
8224 goto ld_st;
252b5132 8225 case M_LDR_AB:
df58fc94
RS
8226 ab = 1;
8227 case M_LDR_OB:
252b5132 8228 s = "ldr";
df58fc94
RS
8229 fmt = MEM12_FMT;
8230 off12 = mips_opts.micromips;
8231 goto ld_st;
252b5132 8232 case M_LL_AB:
df58fc94
RS
8233 ab = 1;
8234 case M_LL_OB:
252b5132 8235 s = "ll";
df58fc94
RS
8236 fmt = MEM12_FMT;
8237 off12 = mips_opts.micromips;
252b5132
RH
8238 goto ld;
8239 case M_LLD_AB:
df58fc94
RS
8240 ab = 1;
8241 case M_LLD_OB:
252b5132 8242 s = "lld";
df58fc94
RS
8243 fmt = MEM12_FMT;
8244 off12 = mips_opts.micromips;
252b5132
RH
8245 goto ld;
8246 case M_LWU_AB:
df58fc94
RS
8247 ab = 1;
8248 case M_LWU_OB:
252b5132 8249 s = "lwu";
df58fc94
RS
8250 fmt = MEM12_FMT;
8251 off12 = mips_opts.micromips;
8252 goto ld;
8253 case M_LWP_AB:
8254 ab = 1;
8255 case M_LWP_OB:
8256 gas_assert (mips_opts.micromips);
8257 s = "lwp";
8258 fmt = "t,~(b)";
8259 off12 = 1;
8260 lp = 1;
8261 goto ld;
8262 case M_LDP_AB:
8263 ab = 1;
8264 case M_LDP_OB:
8265 gas_assert (mips_opts.micromips);
8266 s = "ldp";
8267 fmt = "t,~(b)";
8268 off12 = 1;
8269 lp = 1;
8270 goto ld;
8271 case M_LWM_AB:
8272 ab = 1;
8273 case M_LWM_OB:
8274 gas_assert (mips_opts.micromips);
8275 s = "lwm";
8276 fmt = "n,~(b)";
8277 off12 = 1;
8278 goto ld_st;
8279 case M_LDM_AB:
8280 ab = 1;
8281 case M_LDM_OB:
8282 gas_assert (mips_opts.micromips);
8283 s = "ldm";
8284 fmt = "n,~(b)";
8285 off12 = 1;
8286 goto ld_st;
8287
252b5132 8288 ld:
f19ccbda
MR
8289 /* We don't want to use $0 as tempreg. */
8290 if (breg == treg + lp || treg + lp == ZERO)
df58fc94 8291 goto ld_st;
252b5132 8292 else
df58fc94
RS
8293 tempreg = treg + lp;
8294 goto ld_noat;
8295
252b5132 8296 case M_SB_AB:
df58fc94 8297 ab = 1;
252b5132 8298 s = "sb";
df58fc94
RS
8299 fmt = "t,o(b)";
8300 goto ld_st;
252b5132 8301 case M_SH_AB:
df58fc94 8302 ab = 1;
252b5132 8303 s = "sh";
df58fc94
RS
8304 fmt = "t,o(b)";
8305 goto ld_st;
252b5132 8306 case M_SW_AB:
df58fc94 8307 ab = 1;
252b5132 8308 s = "sw";
df58fc94
RS
8309 fmt = "t,o(b)";
8310 goto ld_st;
252b5132 8311 case M_SWC0_AB:
df58fc94
RS
8312 ab = 1;
8313 gas_assert (!mips_opts.micromips);
252b5132 8314 s = "swc0";
df58fc94 8315 fmt = "E,o(b)";
bdaaa2e1 8316 /* Itbl support may require additional care here. */
252b5132 8317 coproc = 1;
df58fc94 8318 goto ld_st;
252b5132 8319 case M_SWC1_AB:
df58fc94 8320 ab = 1;
252b5132 8321 s = "swc1";
df58fc94 8322 fmt = "T,o(b)";
bdaaa2e1 8323 /* Itbl support may require additional care here. */
252b5132 8324 coproc = 1;
df58fc94 8325 goto ld_st;
252b5132 8326 case M_SWC2_AB:
df58fc94
RS
8327 ab = 1;
8328 case M_SWC2_OB:
252b5132 8329 s = "swc2";
df58fc94
RS
8330 fmt = COP12_FMT;
8331 off12 = mips_opts.micromips;
bdaaa2e1 8332 /* Itbl support may require additional care here. */
252b5132 8333 coproc = 1;
df58fc94 8334 goto ld_st;
252b5132 8335 case M_SWC3_AB:
df58fc94
RS
8336 ab = 1;
8337 gas_assert (!mips_opts.micromips);
252b5132 8338 s = "swc3";
df58fc94 8339 fmt = "E,o(b)";
bdaaa2e1 8340 /* Itbl support may require additional care here. */
252b5132 8341 coproc = 1;
df58fc94 8342 goto ld_st;
252b5132 8343 case M_SWL_AB:
df58fc94
RS
8344 ab = 1;
8345 case M_SWL_OB:
252b5132 8346 s = "swl";
df58fc94
RS
8347 fmt = MEM12_FMT;
8348 off12 = mips_opts.micromips;
8349 goto ld_st;
252b5132 8350 case M_SWR_AB:
df58fc94
RS
8351 ab = 1;
8352 case M_SWR_OB:
252b5132 8353 s = "swr";
df58fc94
RS
8354 fmt = MEM12_FMT;
8355 off12 = mips_opts.micromips;
8356 goto ld_st;
252b5132 8357 case M_SC_AB:
df58fc94
RS
8358 ab = 1;
8359 case M_SC_OB:
252b5132 8360 s = "sc";
df58fc94
RS
8361 fmt = MEM12_FMT;
8362 off12 = mips_opts.micromips;
8363 goto ld_st;
252b5132 8364 case M_SCD_AB:
df58fc94
RS
8365 ab = 1;
8366 case M_SCD_OB:
252b5132 8367 s = "scd";
df58fc94
RS
8368 fmt = MEM12_FMT;
8369 off12 = mips_opts.micromips;
8370 goto ld_st;
d43b4baf 8371 case M_CACHE_AB:
df58fc94
RS
8372 ab = 1;
8373 case M_CACHE_OB:
d43b4baf 8374 s = "cache";
df58fc94
RS
8375 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
8376 off12 = mips_opts.micromips;
8377 goto ld_st;
3eebd5eb 8378 case M_PREF_AB:
df58fc94
RS
8379 ab = 1;
8380 case M_PREF_OB:
3eebd5eb 8381 s = "pref";
df58fc94
RS
8382 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
8383 off12 = mips_opts.micromips;
8384 goto ld_st;
252b5132 8385 case M_SDC1_AB:
df58fc94 8386 ab = 1;
252b5132 8387 s = "sdc1";
df58fc94 8388 fmt = "T,o(b)";
252b5132 8389 coproc = 1;
bdaaa2e1 8390 /* Itbl support may require additional care here. */
df58fc94 8391 goto ld_st;
252b5132 8392 case M_SDC2_AB:
df58fc94
RS
8393 ab = 1;
8394 case M_SDC2_OB:
252b5132 8395 s = "sdc2";
df58fc94
RS
8396 fmt = COP12_FMT;
8397 off12 = mips_opts.micromips;
c77c0862
RS
8398 /* Itbl support may require additional care here. */
8399 coproc = 1;
8400 goto ld_st;
8401 case M_SQC2_AB:
8402 ab = 1;
8403 s = "sqc2";
8404 fmt = "E,o(b)";
bdaaa2e1 8405 /* Itbl support may require additional care here. */
252b5132 8406 coproc = 1;
df58fc94 8407 goto ld_st;
252b5132 8408 case M_SDC3_AB:
df58fc94
RS
8409 ab = 1;
8410 gas_assert (!mips_opts.micromips);
252b5132 8411 s = "sdc3";
df58fc94 8412 fmt = "E,o(b)";
bdaaa2e1 8413 /* Itbl support may require additional care here. */
252b5132 8414 coproc = 1;
df58fc94 8415 goto ld_st;
252b5132 8416 case M_SDL_AB:
df58fc94
RS
8417 ab = 1;
8418 case M_SDL_OB:
252b5132 8419 s = "sdl";
df58fc94
RS
8420 fmt = MEM12_FMT;
8421 off12 = mips_opts.micromips;
8422 goto ld_st;
252b5132 8423 case M_SDR_AB:
df58fc94
RS
8424 ab = 1;
8425 case M_SDR_OB:
252b5132 8426 s = "sdr";
df58fc94
RS
8427 fmt = MEM12_FMT;
8428 off12 = mips_opts.micromips;
8429 goto ld_st;
8430 case M_SWP_AB:
8431 ab = 1;
8432 case M_SWP_OB:
8433 gas_assert (mips_opts.micromips);
8434 s = "swp";
8435 fmt = "t,~(b)";
8436 off12 = 1;
8437 goto ld_st;
8438 case M_SDP_AB:
8439 ab = 1;
8440 case M_SDP_OB:
8441 gas_assert (mips_opts.micromips);
8442 s = "sdp";
8443 fmt = "t,~(b)";
8444 off12 = 1;
8445 goto ld_st;
8446 case M_SWM_AB:
8447 ab = 1;
8448 case M_SWM_OB:
8449 gas_assert (mips_opts.micromips);
8450 s = "swm";
8451 fmt = "n,~(b)";
8452 off12 = 1;
8453 goto ld_st;
8454 case M_SDM_AB:
8455 ab = 1;
8456 case M_SDM_OB:
8457 gas_assert (mips_opts.micromips);
8458 s = "sdm";
8459 fmt = "n,~(b)";
8460 off12 = 1;
8461
8462 ld_st:
8fc2e39e
TS
8463 tempreg = AT;
8464 used_at = 1;
df58fc94 8465 ld_noat:
252b5132
RH
8466 if (offset_expr.X_op != O_constant
8467 && offset_expr.X_op != O_symbol)
8468 {
f71d0d44 8469 as_bad (_("Expression too complex"));
252b5132
RH
8470 offset_expr.X_op = O_constant;
8471 }
8472
2051e8c4
MR
8473 if (HAVE_32BIT_ADDRESSES
8474 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
8475 {
8476 char value [32];
8477
8478 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 8479 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 8480 }
2051e8c4 8481
252b5132
RH
8482 /* A constant expression in PIC code can be handled just as it
8483 is in non PIC code. */
aed1a261
RS
8484 if (offset_expr.X_op == O_constant)
8485 {
df58fc94
RS
8486 int hipart = 0;
8487
842f8b2a 8488 expr1.X_add_number = offset_expr.X_add_number;
2051e8c4 8489 normalize_address_expr (&expr1);
df58fc94 8490 if (!off12 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
842f8b2a
MR
8491 {
8492 expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8493 & ~(bfd_vma) 0xffff);
df58fc94
RS
8494 hipart = 1;
8495 }
8496 else if (off12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
8497 {
8498 expr1.X_add_number = ((expr1.X_add_number + 0x800)
8499 & ~(bfd_vma) 0xfff);
8500 hipart = 1;
8501 }
8502 if (hipart)
8503 {
842f8b2a
MR
8504 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8505 if (breg != 0)
8506 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8507 tempreg, tempreg, breg);
8508 breg = tempreg;
8509 }
dd6a37e7
AP
8510 if (off0)
8511 {
8512 if (offset_expr.X_add_number == 0)
8513 tempreg = breg;
8514 else
8515 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8516 "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8517 macro_build (NULL, s, fmt, treg, tempreg);
8518 }
8519 else if (!off12)
df58fc94
RS
8520 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8521 else
8522 macro_build (NULL, s, fmt,
8523 treg, (unsigned long) offset_expr.X_add_number, breg);
8524 }
dd6a37e7 8525 else if (off12 || off0)
df58fc94 8526 {
dd6a37e7
AP
8527 /* A 12-bit or 0-bit offset field is too narrow to be used
8528 for a low-part relocation, so load the whole address into
8529 the auxillary register. In the case of "A(b)" addresses,
8530 we first load absolute address "A" into the register and
8531 then add base register "b". In the case of "o(b)" addresses,
8532 we simply need to add 16-bit offset "o" to base register "b", and
df58fc94
RS
8533 offset_reloc already contains the relocations associated
8534 with "o". */
8535 if (ab)
8536 {
8537 load_address (tempreg, &offset_expr, &used_at);
8538 if (breg != 0)
8539 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8540 tempreg, tempreg, breg);
8541 }
8542 else
8543 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8544 tempreg, breg, -1,
8545 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8546 expr1.X_add_number = 0;
dd6a37e7
AP
8547 if (off0)
8548 macro_build (NULL, s, fmt, treg, tempreg);
8549 else
8550 macro_build (NULL, s, fmt,
8551 treg, (unsigned long) expr1.X_add_number, tempreg);
aed1a261
RS
8552 }
8553 else if (mips_pic == NO_PIC)
252b5132
RH
8554 {
8555 /* If this is a reference to a GP relative symbol, and there
8556 is no base register, we want
cdf6fd85 8557 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
8558 Otherwise, if there is no base register, we want
8559 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8560 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8561 If we have a constant, we need two instructions anyhow,
8562 so we always use the latter form.
8563
8564 If we have a base register, and this is a reference to a
8565 GP relative symbol, we want
8566 addu $tempreg,$breg,$gp
cdf6fd85 8567 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
8568 Otherwise we want
8569 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8570 addu $tempreg,$tempreg,$breg
8571 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 8572 With a constant we always use the latter case.
76b3015f 8573
d6bc6245
TS
8574 With 64bit address space and no base register and $at usable,
8575 we want
8576 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8577 lui $at,<sym> (BFD_RELOC_HI16_S)
8578 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8579 dsll32 $tempreg,0
8580 daddu $tempreg,$at
8581 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8582 If we have a base register, we want
8583 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8584 lui $at,<sym> (BFD_RELOC_HI16_S)
8585 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8586 daddu $at,$breg
8587 dsll32 $tempreg,0
8588 daddu $tempreg,$at
8589 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8590
8591 Without $at we can't generate the optimal path for superscalar
8592 processors here since this would require two temporary registers.
8593 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8594 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8595 dsll $tempreg,16
8596 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8597 dsll $tempreg,16
8598 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8599 If we have a base register, we want
8600 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8601 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8602 dsll $tempreg,16
8603 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8604 dsll $tempreg,16
8605 daddu $tempreg,$tempreg,$breg
8606 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 8607
6caf9ef4 8608 For GP relative symbols in 64bit address space we can use
aed1a261
RS
8609 the same sequence as in 32bit address space. */
8610 if (HAVE_64BIT_SYMBOLS)
d6bc6245 8611 {
aed1a261 8612 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
8613 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8614 {
8615 relax_start (offset_expr.X_add_symbol);
8616 if (breg == 0)
8617 {
8618 macro_build (&offset_expr, s, fmt, treg,
8619 BFD_RELOC_GPREL16, mips_gp_register);
8620 }
8621 else
8622 {
8623 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8624 tempreg, breg, mips_gp_register);
8625 macro_build (&offset_expr, s, fmt, treg,
8626 BFD_RELOC_GPREL16, tempreg);
8627 }
8628 relax_switch ();
8629 }
d6bc6245 8630
741fe287 8631 if (used_at == 0 && mips_opts.at)
d6bc6245 8632 {
df58fc94 8633 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 8634 BFD_RELOC_MIPS_HIGHEST);
df58fc94 8635 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
8636 BFD_RELOC_HI16_S);
8637 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8638 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 8639 if (breg != 0)
67c0d1eb 8640 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 8641 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb
RS
8642 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
8643 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
8644 tempreg);
d6bc6245
TS
8645 used_at = 1;
8646 }
8647 else
8648 {
df58fc94 8649 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
8650 BFD_RELOC_MIPS_HIGHEST);
8651 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8652 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 8653 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
8654 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8655 tempreg, BFD_RELOC_HI16_S);
df58fc94 8656 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 8657 if (breg != 0)
67c0d1eb 8658 macro_build (NULL, "daddu", "d,v,t",
17a2f251 8659 tempreg, tempreg, breg);
67c0d1eb 8660 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8661 BFD_RELOC_LO16, tempreg);
d6bc6245 8662 }
6caf9ef4
TS
8663
8664 if (mips_relax.sequence)
8665 relax_end ();
8fc2e39e 8666 break;
d6bc6245 8667 }
256ab948 8668
252b5132
RH
8669 if (breg == 0)
8670 {
67c0d1eb 8671 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8672 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 8673 {
4d7206a2 8674 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
8675 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
8676 mips_gp_register);
4d7206a2 8677 relax_switch ();
252b5132 8678 }
67c0d1eb
RS
8679 macro_build_lui (&offset_expr, tempreg);
8680 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8681 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
8682 if (mips_relax.sequence)
8683 relax_end ();
252b5132
RH
8684 }
8685 else
8686 {
67c0d1eb 8687 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8688 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 8689 {
4d7206a2 8690 relax_start (offset_expr.X_add_symbol);
67c0d1eb 8691 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8692 tempreg, breg, mips_gp_register);
67c0d1eb 8693 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8694 BFD_RELOC_GPREL16, tempreg);
4d7206a2 8695 relax_switch ();
252b5132 8696 }
67c0d1eb
RS
8697 macro_build_lui (&offset_expr, tempreg);
8698 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8699 tempreg, tempreg, breg);
67c0d1eb 8700 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8701 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
8702 if (mips_relax.sequence)
8703 relax_end ();
252b5132
RH
8704 }
8705 }
0a44bf69 8706 else if (!mips_big_got)
252b5132 8707 {
ed6fb7bd 8708 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 8709
252b5132
RH
8710 /* If this is a reference to an external symbol, we want
8711 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8712 nop
8713 <op> $treg,0($tempreg)
8714 Otherwise we want
8715 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8716 nop
8717 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8718 <op> $treg,0($tempreg)
f5040a92
AO
8719
8720 For NewABI, we want
8721 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8722 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
8723
252b5132
RH
8724 If there is a base register, we add it to $tempreg before
8725 the <op>. If there is a constant, we stick it in the
8726 <op> instruction. We don't handle constants larger than
8727 16 bits, because we have no way to load the upper 16 bits
8728 (actually, we could handle them for the subset of cases
8729 in which we are not using $at). */
9c2799c2 8730 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
8731 if (HAVE_NEWABI)
8732 {
67c0d1eb
RS
8733 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8734 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 8735 if (breg != 0)
67c0d1eb 8736 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8737 tempreg, tempreg, breg);
67c0d1eb 8738 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8739 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
8740 break;
8741 }
252b5132
RH
8742 expr1.X_add_number = offset_expr.X_add_number;
8743 offset_expr.X_add_number = 0;
8744 if (expr1.X_add_number < -0x8000
8745 || expr1.X_add_number >= 0x8000)
8746 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
8747 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8748 lw_reloc_type, mips_gp_register);
269137b2 8749 load_delay_nop ();
4d7206a2
RS
8750 relax_start (offset_expr.X_add_symbol);
8751 relax_switch ();
67c0d1eb
RS
8752 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8753 tempreg, BFD_RELOC_LO16);
4d7206a2 8754 relax_end ();
252b5132 8755 if (breg != 0)
67c0d1eb 8756 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8757 tempreg, tempreg, breg);
67c0d1eb 8758 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 8759 }
0a44bf69 8760 else if (mips_big_got && !HAVE_NEWABI)
252b5132 8761 {
67c0d1eb 8762 int gpdelay;
252b5132
RH
8763
8764 /* If this is a reference to an external symbol, we want
8765 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8766 addu $tempreg,$tempreg,$gp
8767 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8768 <op> $treg,0($tempreg)
8769 Otherwise we want
8770 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8771 nop
8772 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8773 <op> $treg,0($tempreg)
8774 If there is a base register, we add it to $tempreg before
8775 the <op>. If there is a constant, we stick it in the
8776 <op> instruction. We don't handle constants larger than
8777 16 bits, because we have no way to load the upper 16 bits
8778 (actually, we could handle them for the subset of cases
f5040a92 8779 in which we are not using $at). */
9c2799c2 8780 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
8781 expr1.X_add_number = offset_expr.X_add_number;
8782 offset_expr.X_add_number = 0;
8783 if (expr1.X_add_number < -0x8000
8784 || expr1.X_add_number >= 0x8000)
8785 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 8786 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 8787 relax_start (offset_expr.X_add_symbol);
df58fc94 8788 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 8789 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8790 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8791 mips_gp_register);
8792 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8793 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 8794 relax_switch ();
67c0d1eb
RS
8795 if (gpdelay)
8796 macro_build (NULL, "nop", "");
8797 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8798 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 8799 load_delay_nop ();
67c0d1eb
RS
8800 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8801 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
8802 relax_end ();
8803
252b5132 8804 if (breg != 0)
67c0d1eb 8805 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8806 tempreg, tempreg, breg);
67c0d1eb 8807 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 8808 }
0a44bf69 8809 else if (mips_big_got && HAVE_NEWABI)
f5040a92 8810 {
f5040a92
AO
8811 /* If this is a reference to an external symbol, we want
8812 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8813 add $tempreg,$tempreg,$gp
8814 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8815 <op> $treg,<ofst>($tempreg)
8816 Otherwise, for local symbols, we want:
8817 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8818 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 8819 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 8820 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
8821 offset_expr.X_add_number = 0;
8822 if (expr1.X_add_number < -0x8000
8823 || expr1.X_add_number >= 0x8000)
8824 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 8825 relax_start (offset_expr.X_add_symbol);
df58fc94 8826 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 8827 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8828 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8829 mips_gp_register);
8830 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8831 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 8832 if (breg != 0)
67c0d1eb 8833 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8834 tempreg, tempreg, breg);
67c0d1eb 8835 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 8836
4d7206a2 8837 relax_switch ();
f5040a92 8838 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
8839 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8840 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 8841 if (breg != 0)
67c0d1eb 8842 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8843 tempreg, tempreg, breg);
67c0d1eb 8844 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8845 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 8846 relax_end ();
f5040a92 8847 }
252b5132
RH
8848 else
8849 abort ();
8850
252b5132
RH
8851 break;
8852
8853 case M_LI:
8854 case M_LI_S:
67c0d1eb 8855 load_register (treg, &imm_expr, 0);
8fc2e39e 8856 break;
252b5132
RH
8857
8858 case M_DLI:
67c0d1eb 8859 load_register (treg, &imm_expr, 1);
8fc2e39e 8860 break;
252b5132
RH
8861
8862 case M_LI_SS:
8863 if (imm_expr.X_op == O_constant)
8864 {
8fc2e39e 8865 used_at = 1;
67c0d1eb
RS
8866 load_register (AT, &imm_expr, 0);
8867 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
8868 break;
8869 }
8870 else
8871 {
9c2799c2 8872 gas_assert (offset_expr.X_op == O_symbol
90ecf173
MR
8873 && strcmp (segment_name (S_GET_SEGMENT
8874 (offset_expr.X_add_symbol)),
8875 ".lit4") == 0
8876 && offset_expr.X_add_number == 0);
67c0d1eb 8877 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 8878 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 8879 break;
252b5132
RH
8880 }
8881
8882 case M_LI_D:
ca4e0257
RS
8883 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
8884 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
8885 order 32 bits of the value and the low order 32 bits are either
8886 zero or in OFFSET_EXPR. */
252b5132
RH
8887 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8888 {
ca4e0257 8889 if (HAVE_64BIT_GPRS)
67c0d1eb 8890 load_register (treg, &imm_expr, 1);
252b5132
RH
8891 else
8892 {
8893 int hreg, lreg;
8894
8895 if (target_big_endian)
8896 {
8897 hreg = treg;
8898 lreg = treg + 1;
8899 }
8900 else
8901 {
8902 hreg = treg + 1;
8903 lreg = treg;
8904 }
8905
8906 if (hreg <= 31)
67c0d1eb 8907 load_register (hreg, &imm_expr, 0);
252b5132
RH
8908 if (lreg <= 31)
8909 {
8910 if (offset_expr.X_op == O_absent)
67c0d1eb 8911 move_register (lreg, 0);
252b5132
RH
8912 else
8913 {
9c2799c2 8914 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 8915 load_register (lreg, &offset_expr, 0);
252b5132
RH
8916 }
8917 }
8918 }
8fc2e39e 8919 break;
252b5132
RH
8920 }
8921
8922 /* We know that sym is in the .rdata section. First we get the
8923 upper 16 bits of the address. */
8924 if (mips_pic == NO_PIC)
8925 {
67c0d1eb 8926 macro_build_lui (&offset_expr, AT);
8fc2e39e 8927 used_at = 1;
252b5132 8928 }
0a44bf69 8929 else
252b5132 8930 {
67c0d1eb
RS
8931 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8932 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 8933 used_at = 1;
252b5132 8934 }
bdaaa2e1 8935
252b5132 8936 /* Now we load the register(s). */
ca4e0257 8937 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
8938 {
8939 used_at = 1;
8940 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8941 }
252b5132
RH
8942 else
8943 {
8fc2e39e 8944 used_at = 1;
67c0d1eb 8945 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 8946 if (treg != RA)
252b5132
RH
8947 {
8948 /* FIXME: How in the world do we deal with the possible
8949 overflow here? */
8950 offset_expr.X_add_number += 4;
67c0d1eb 8951 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 8952 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
8953 }
8954 }
252b5132
RH
8955 break;
8956
8957 case M_LI_DD:
ca4e0257
RS
8958 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
8959 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
8960 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
8961 the value and the low order 32 bits are either zero or in
8962 OFFSET_EXPR. */
252b5132
RH
8963 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8964 {
8fc2e39e 8965 used_at = 1;
67c0d1eb 8966 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
8967 if (HAVE_64BIT_FPRS)
8968 {
9c2799c2 8969 gas_assert (HAVE_64BIT_GPRS);
67c0d1eb 8970 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 8971 }
252b5132
RH
8972 else
8973 {
67c0d1eb 8974 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 8975 if (offset_expr.X_op == O_absent)
67c0d1eb 8976 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
8977 else
8978 {
9c2799c2 8979 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
8980 load_register (AT, &offset_expr, 0);
8981 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
8982 }
8983 }
8984 break;
8985 }
8986
9c2799c2 8987 gas_assert (offset_expr.X_op == O_symbol
90ecf173 8988 && offset_expr.X_add_number == 0);
252b5132
RH
8989 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
8990 if (strcmp (s, ".lit8") == 0)
8991 {
0aa27725 8992 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
252b5132 8993 {
67c0d1eb 8994 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 8995 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 8996 break;
252b5132 8997 }
c9914766 8998 breg = mips_gp_register;
252b5132
RH
8999 r = BFD_RELOC_MIPS_LITERAL;
9000 goto dob;
9001 }
9002 else
9003 {
9c2799c2 9004 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 9005 used_at = 1;
0a44bf69 9006 if (mips_pic != NO_PIC)
67c0d1eb
RS
9007 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9008 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
9009 else
9010 {
9011 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 9012 macro_build_lui (&offset_expr, AT);
252b5132 9013 }
bdaaa2e1 9014
0aa27725 9015 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
252b5132 9016 {
67c0d1eb
RS
9017 macro_build (&offset_expr, "ldc1", "T,o(b)",
9018 treg, BFD_RELOC_LO16, AT);
252b5132
RH
9019 break;
9020 }
9021 breg = AT;
9022 r = BFD_RELOC_LO16;
9023 goto dob;
9024 }
9025
9026 case M_L_DOB:
252b5132
RH
9027 /* Even on a big endian machine $fn comes before $fn+1. We have
9028 to adjust when loading from memory. */
9029 r = BFD_RELOC_LO16;
9030 dob:
df58fc94 9031 gas_assert (!mips_opts.micromips);
0aa27725 9032 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
67c0d1eb 9033 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 9034 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
9035 /* FIXME: A possible overflow which I don't know how to deal
9036 with. */
9037 offset_expr.X_add_number += 4;
67c0d1eb 9038 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 9039 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
9040 break;
9041
c4a68bea 9042 case M_S_DOB:
df58fc94 9043 gas_assert (!mips_opts.micromips);
0aa27725 9044 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
c4a68bea
MR
9045 /* Even on a big endian machine $fn comes before $fn+1. We have
9046 to adjust when storing to memory. */
9047 macro_build (&offset_expr, "swc1", "T,o(b)",
9048 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
9049 offset_expr.X_add_number += 4;
9050 macro_build (&offset_expr, "swc1", "T,o(b)",
9051 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
9052 break;
9053
252b5132 9054 case M_L_DAB:
df58fc94 9055 gas_assert (!mips_opts.micromips);
252b5132
RH
9056 /*
9057 * The MIPS assembler seems to check for X_add_number not
9058 * being double aligned and generating:
9059 * lui at,%hi(foo+1)
9060 * addu at,at,v1
9061 * addiu at,at,%lo(foo+1)
9062 * lwc1 f2,0(at)
9063 * lwc1 f3,4(at)
9064 * But, the resulting address is the same after relocation so why
9065 * generate the extra instruction?
9066 */
bdaaa2e1 9067 /* Itbl support may require additional care here. */
252b5132 9068 coproc = 1;
df58fc94 9069 fmt = "T,o(b)";
0aa27725 9070 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
9071 {
9072 s = "ldc1";
df58fc94 9073 goto ld_st;
252b5132 9074 }
252b5132 9075 s = "lwc1";
252b5132
RH
9076 goto ldd_std;
9077
9078 case M_S_DAB:
df58fc94
RS
9079 gas_assert (!mips_opts.micromips);
9080 /* Itbl support may require additional care here. */
9081 coproc = 1;
9082 fmt = "T,o(b)";
0aa27725 9083 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
9084 {
9085 s = "sdc1";
df58fc94 9086 goto ld_st;
252b5132 9087 }
252b5132 9088 s = "swc1";
252b5132
RH
9089 goto ldd_std;
9090
e407c74b
NC
9091 case M_LQ_AB:
9092 fmt = "t,o(b)";
9093 s = "lq";
9094 goto ld;
9095
9096 case M_SQ_AB:
9097 fmt = "t,o(b)";
9098 s = "sq";
9099 goto ld_st;
9100
252b5132 9101 case M_LD_AB:
df58fc94 9102 fmt = "t,o(b)";
ca4e0257 9103 if (HAVE_64BIT_GPRS)
252b5132
RH
9104 {
9105 s = "ld";
9106 goto ld;
9107 }
252b5132 9108 s = "lw";
252b5132
RH
9109 goto ldd_std;
9110
9111 case M_SD_AB:
df58fc94 9112 fmt = "t,o(b)";
ca4e0257 9113 if (HAVE_64BIT_GPRS)
252b5132
RH
9114 {
9115 s = "sd";
df58fc94 9116 goto ld_st;
252b5132 9117 }
252b5132 9118 s = "sw";
252b5132
RH
9119
9120 ldd_std:
9121 if (offset_expr.X_op != O_symbol
9122 && offset_expr.X_op != O_constant)
9123 {
f71d0d44 9124 as_bad (_("Expression too complex"));
252b5132
RH
9125 offset_expr.X_op = O_constant;
9126 }
9127
2051e8c4
MR
9128 if (HAVE_32BIT_ADDRESSES
9129 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
9130 {
9131 char value [32];
9132
9133 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 9134 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 9135 }
2051e8c4 9136
252b5132
RH
9137 /* Even on a big endian machine $fn comes before $fn+1. We have
9138 to adjust when loading from memory. We set coproc if we must
9139 load $fn+1 first. */
bdaaa2e1 9140 /* Itbl support may require additional care here. */
90ecf173 9141 if (!target_big_endian)
252b5132
RH
9142 coproc = 0;
9143
90ecf173 9144 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
9145 {
9146 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
9147 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
9148 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
9149 If we have a base register, we use this
9150 addu $at,$breg,$gp
cdf6fd85
TS
9151 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
9152 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
9153 If this is not a GP relative symbol, we want
9154 lui $at,<sym> (BFD_RELOC_HI16_S)
9155 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9156 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9157 If there is a base register, we add it to $at after the
9158 lui instruction. If there is a constant, we always use
9159 the last case. */
39a59cf8
MR
9160 if (offset_expr.X_op == O_symbol
9161 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9162 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 9163 {
4d7206a2 9164 relax_start (offset_expr.X_add_symbol);
252b5132
RH
9165 if (breg == 0)
9166 {
c9914766 9167 tempreg = mips_gp_register;
252b5132
RH
9168 }
9169 else
9170 {
67c0d1eb 9171 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9172 AT, breg, mips_gp_register);
252b5132 9173 tempreg = AT;
252b5132
RH
9174 used_at = 1;
9175 }
9176
beae10d5 9177 /* Itbl support may require additional care here. */
67c0d1eb 9178 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9179 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
9180 offset_expr.X_add_number += 4;
9181
9182 /* Set mips_optimize to 2 to avoid inserting an
9183 undesired nop. */
9184 hold_mips_optimize = mips_optimize;
9185 mips_optimize = 2;
beae10d5 9186 /* Itbl support may require additional care here. */
67c0d1eb 9187 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9188 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
9189 mips_optimize = hold_mips_optimize;
9190
4d7206a2 9191 relax_switch ();
252b5132 9192
0970e49e 9193 offset_expr.X_add_number -= 4;
252b5132 9194 }
8fc2e39e 9195 used_at = 1;
67c0d1eb 9196 macro_build_lui (&offset_expr, AT);
252b5132 9197 if (breg != 0)
67c0d1eb 9198 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9199 /* Itbl support may require additional care here. */
67c0d1eb 9200 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9201 BFD_RELOC_LO16, AT);
252b5132
RH
9202 /* FIXME: How do we handle overflow here? */
9203 offset_expr.X_add_number += 4;
beae10d5 9204 /* Itbl support may require additional care here. */
67c0d1eb 9205 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9206 BFD_RELOC_LO16, AT);
4d7206a2
RS
9207 if (mips_relax.sequence)
9208 relax_end ();
bdaaa2e1 9209 }
0a44bf69 9210 else if (!mips_big_got)
252b5132 9211 {
252b5132
RH
9212 /* If this is a reference to an external symbol, we want
9213 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9214 nop
9215 <op> $treg,0($at)
9216 <op> $treg+1,4($at)
9217 Otherwise we want
9218 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9219 nop
9220 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9221 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9222 If there is a base register we add it to $at before the
9223 lwc1 instructions. If there is a constant we include it
9224 in the lwc1 instructions. */
9225 used_at = 1;
9226 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
9227 if (expr1.X_add_number < -0x8000
9228 || expr1.X_add_number >= 0x8000 - 4)
9229 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9230 load_got_offset (AT, &offset_expr);
269137b2 9231 load_delay_nop ();
252b5132 9232 if (breg != 0)
67c0d1eb 9233 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
9234
9235 /* Set mips_optimize to 2 to avoid inserting an undesired
9236 nop. */
9237 hold_mips_optimize = mips_optimize;
9238 mips_optimize = 2;
4d7206a2 9239
beae10d5 9240 /* Itbl support may require additional care here. */
4d7206a2 9241 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9242 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9243 BFD_RELOC_LO16, AT);
4d7206a2 9244 expr1.X_add_number += 4;
67c0d1eb
RS
9245 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9246 BFD_RELOC_LO16, AT);
4d7206a2 9247 relax_switch ();
67c0d1eb
RS
9248 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9249 BFD_RELOC_LO16, AT);
4d7206a2 9250 offset_expr.X_add_number += 4;
67c0d1eb
RS
9251 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9252 BFD_RELOC_LO16, AT);
4d7206a2 9253 relax_end ();
252b5132 9254
4d7206a2 9255 mips_optimize = hold_mips_optimize;
252b5132 9256 }
0a44bf69 9257 else if (mips_big_got)
252b5132 9258 {
67c0d1eb 9259 int gpdelay;
252b5132
RH
9260
9261 /* If this is a reference to an external symbol, we want
9262 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9263 addu $at,$at,$gp
9264 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
9265 nop
9266 <op> $treg,0($at)
9267 <op> $treg+1,4($at)
9268 Otherwise we want
9269 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9270 nop
9271 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9272 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9273 If there is a base register we add it to $at before the
9274 lwc1 instructions. If there is a constant we include it
9275 in the lwc1 instructions. */
9276 used_at = 1;
9277 expr1.X_add_number = offset_expr.X_add_number;
9278 offset_expr.X_add_number = 0;
9279 if (expr1.X_add_number < -0x8000
9280 || expr1.X_add_number >= 0x8000 - 4)
9281 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9282 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 9283 relax_start (offset_expr.X_add_symbol);
df58fc94 9284 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
9285 AT, BFD_RELOC_MIPS_GOT_HI16);
9286 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9287 AT, AT, mips_gp_register);
67c0d1eb 9288 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 9289 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 9290 load_delay_nop ();
252b5132 9291 if (breg != 0)
67c0d1eb 9292 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9293 /* Itbl support may require additional care here. */
67c0d1eb 9294 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9295 BFD_RELOC_LO16, AT);
252b5132
RH
9296 expr1.X_add_number += 4;
9297
9298 /* Set mips_optimize to 2 to avoid inserting an undesired
9299 nop. */
9300 hold_mips_optimize = mips_optimize;
9301 mips_optimize = 2;
beae10d5 9302 /* Itbl support may require additional care here. */
67c0d1eb 9303 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 9304 BFD_RELOC_LO16, AT);
252b5132
RH
9305 mips_optimize = hold_mips_optimize;
9306 expr1.X_add_number -= 4;
9307
4d7206a2
RS
9308 relax_switch ();
9309 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
9310 if (gpdelay)
9311 macro_build (NULL, "nop", "");
9312 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9313 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9314 load_delay_nop ();
252b5132 9315 if (breg != 0)
67c0d1eb 9316 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9317 /* Itbl support may require additional care here. */
67c0d1eb
RS
9318 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9319 BFD_RELOC_LO16, AT);
4d7206a2 9320 offset_expr.X_add_number += 4;
252b5132
RH
9321
9322 /* Set mips_optimize to 2 to avoid inserting an undesired
9323 nop. */
9324 hold_mips_optimize = mips_optimize;
9325 mips_optimize = 2;
beae10d5 9326 /* Itbl support may require additional care here. */
67c0d1eb
RS
9327 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9328 BFD_RELOC_LO16, AT);
252b5132 9329 mips_optimize = hold_mips_optimize;
4d7206a2 9330 relax_end ();
252b5132 9331 }
252b5132
RH
9332 else
9333 abort ();
9334
252b5132
RH
9335 break;
9336
9337 case M_LD_OB:
704897fb 9338 s = HAVE_64BIT_GPRS ? "ld" : "lw";
252b5132
RH
9339 goto sd_ob;
9340 case M_SD_OB:
704897fb 9341 s = HAVE_64BIT_GPRS ? "sd" : "sw";
252b5132 9342 sd_ob:
4614d845
MR
9343 macro_build (&offset_expr, s, "t,o(b)", treg,
9344 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9345 breg);
704897fb
MR
9346 if (!HAVE_64BIT_GPRS)
9347 {
9348 offset_expr.X_add_number += 4;
9349 macro_build (&offset_expr, s, "t,o(b)", treg + 1,
4614d845
MR
9350 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9351 breg);
704897fb 9352 }
8fc2e39e 9353 break;
252b5132 9354
dd6a37e7
AP
9355
9356 case M_SAA_AB:
9357 ab = 1;
9358 case M_SAA_OB:
9359 s = "saa";
9360 off0 = 1;
9361 fmt = "t,(b)";
9362 goto ld_st;
9363 case M_SAAD_AB:
9364 ab = 1;
9365 case M_SAAD_OB:
9366 s = "saad";
9367 off0 = 1;
9368 fmt = "t,(b)";
9369 goto ld_st;
9370
252b5132
RH
9371 /* New code added to support COPZ instructions.
9372 This code builds table entries out of the macros in mip_opcodes.
9373 R4000 uses interlocks to handle coproc delays.
9374 Other chips (like the R3000) require nops to be inserted for delays.
9375
f72c8c98 9376 FIXME: Currently, we require that the user handle delays.
252b5132
RH
9377 In order to fill delay slots for non-interlocked chips,
9378 we must have a way to specify delays based on the coprocessor.
9379 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9380 What are the side-effects of the cop instruction?
9381 What cache support might we have and what are its effects?
9382 Both coprocessor & memory require delays. how long???
bdaaa2e1 9383 What registers are read/set/modified?
252b5132
RH
9384
9385 If an itbl is provided to interpret cop instructions,
bdaaa2e1 9386 this knowledge can be encoded in the itbl spec. */
252b5132
RH
9387
9388 case M_COP0:
9389 s = "c0";
9390 goto copz;
9391 case M_COP1:
9392 s = "c1";
9393 goto copz;
9394 case M_COP2:
9395 s = "c2";
9396 goto copz;
9397 case M_COP3:
9398 s = "c3";
9399 copz:
df58fc94 9400 gas_assert (!mips_opts.micromips);
252b5132
RH
9401 /* For now we just do C (same as Cz). The parameter will be
9402 stored in insn_opcode by mips_ip. */
67c0d1eb 9403 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 9404 break;
252b5132 9405
ea1fb5dc 9406 case M_MOVE:
67c0d1eb 9407 move_register (dreg, sreg);
8fc2e39e 9408 break;
ea1fb5dc 9409
252b5132
RH
9410 case M_DMUL:
9411 dbl = 1;
9412 case M_MUL:
e407c74b
NC
9413 if (mips_opts.arch == CPU_R5900)
9414 {
9415 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", dreg, sreg, treg);
9416 }
9417 else
9418 {
67c0d1eb 9419 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
df58fc94 9420 macro_build (NULL, "mflo", MFHL_FMT, dreg);
e407c74b 9421 }
8fc2e39e 9422 break;
252b5132
RH
9423
9424 case M_DMUL_I:
9425 dbl = 1;
9426 case M_MUL_I:
9427 /* The MIPS assembler some times generates shifts and adds. I'm
9428 not trying to be that fancy. GCC should do this for us
9429 anyway. */
8fc2e39e 9430 used_at = 1;
67c0d1eb
RS
9431 load_register (AT, &imm_expr, dbl);
9432 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
df58fc94 9433 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9434 break;
9435
9436 case M_DMULO_I:
9437 dbl = 1;
9438 case M_MULO_I:
9439 imm = 1;
9440 goto do_mulo;
9441
9442 case M_DMULO:
9443 dbl = 1;
9444 case M_MULO:
9445 do_mulo:
7d10b47d 9446 start_noreorder ();
8fc2e39e 9447 used_at = 1;
252b5132 9448 if (imm)
67c0d1eb
RS
9449 load_register (AT, &imm_expr, dbl);
9450 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
df58fc94
RS
9451 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9452 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9453 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 9454 if (mips_trap)
df58fc94 9455 macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
252b5132
RH
9456 else
9457 {
df58fc94
RS
9458 if (mips_opts.micromips)
9459 micromips_label_expr (&label_expr);
9460 else
9461 label_expr.X_add_number = 8;
9462 macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
a605d2b3 9463 macro_build (NULL, "nop", "");
df58fc94
RS
9464 macro_build (NULL, "break", BRK_FMT, 6);
9465 if (mips_opts.micromips)
9466 micromips_add_label ();
252b5132 9467 }
7d10b47d 9468 end_noreorder ();
df58fc94 9469 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9470 break;
9471
9472 case M_DMULOU_I:
9473 dbl = 1;
9474 case M_MULOU_I:
9475 imm = 1;
9476 goto do_mulou;
9477
9478 case M_DMULOU:
9479 dbl = 1;
9480 case M_MULOU:
9481 do_mulou:
7d10b47d 9482 start_noreorder ();
8fc2e39e 9483 used_at = 1;
252b5132 9484 if (imm)
67c0d1eb
RS
9485 load_register (AT, &imm_expr, dbl);
9486 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 9487 sreg, imm ? AT : treg);
df58fc94
RS
9488 macro_build (NULL, "mfhi", MFHL_FMT, AT);
9489 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132 9490 if (mips_trap)
df58fc94 9491 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
9492 else
9493 {
df58fc94
RS
9494 if (mips_opts.micromips)
9495 micromips_label_expr (&label_expr);
9496 else
9497 label_expr.X_add_number = 8;
9498 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 9499 macro_build (NULL, "nop", "");
df58fc94
RS
9500 macro_build (NULL, "break", BRK_FMT, 6);
9501 if (mips_opts.micromips)
9502 micromips_add_label ();
252b5132 9503 }
7d10b47d 9504 end_noreorder ();
252b5132
RH
9505 break;
9506
771c7ce4 9507 case M_DROL:
fef14a42 9508 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
9509 {
9510 if (dreg == sreg)
9511 {
9512 tempreg = AT;
9513 used_at = 1;
9514 }
9515 else
9516 {
9517 tempreg = dreg;
82dd0097 9518 }
67c0d1eb
RS
9519 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9520 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9521 break;
82dd0097 9522 }
8fc2e39e 9523 used_at = 1;
c80c840e 9524 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9525 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9526 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9527 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9528 break;
9529
252b5132 9530 case M_ROL:
fef14a42 9531 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
9532 {
9533 if (dreg == sreg)
9534 {
9535 tempreg = AT;
9536 used_at = 1;
9537 }
9538 else
9539 {
9540 tempreg = dreg;
82dd0097 9541 }
67c0d1eb
RS
9542 macro_build (NULL, "negu", "d,w", tempreg, treg);
9543 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9544 break;
82dd0097 9545 }
8fc2e39e 9546 used_at = 1;
c80c840e 9547 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9548 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9549 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9550 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9551 break;
9552
771c7ce4
TS
9553 case M_DROL_I:
9554 {
9555 unsigned int rot;
91d6fa6a
NC
9556 char *l;
9557 char *rr;
771c7ce4
TS
9558
9559 if (imm_expr.X_op != O_constant)
82dd0097 9560 as_bad (_("Improper rotate count"));
771c7ce4 9561 rot = imm_expr.X_add_number & 0x3f;
fef14a42 9562 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
9563 {
9564 rot = (64 - rot) & 0x3f;
9565 if (rot >= 32)
df58fc94 9566 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
60b63b72 9567 else
df58fc94 9568 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9569 break;
60b63b72 9570 }
483fc7cd 9571 if (rot == 0)
483fc7cd 9572 {
df58fc94 9573 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9574 break;
483fc7cd 9575 }
82dd0097 9576 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 9577 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 9578 rot &= 0x1f;
8fc2e39e 9579 used_at = 1;
df58fc94
RS
9580 macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
9581 macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9582 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9583 }
9584 break;
9585
252b5132 9586 case M_ROL_I:
771c7ce4
TS
9587 {
9588 unsigned int rot;
9589
9590 if (imm_expr.X_op != O_constant)
82dd0097 9591 as_bad (_("Improper rotate count"));
771c7ce4 9592 rot = imm_expr.X_add_number & 0x1f;
fef14a42 9593 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 9594 {
df58fc94 9595 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 9596 break;
60b63b72 9597 }
483fc7cd 9598 if (rot == 0)
483fc7cd 9599 {
df58fc94 9600 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9601 break;
483fc7cd 9602 }
8fc2e39e 9603 used_at = 1;
df58fc94
RS
9604 macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
9605 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9606 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9607 }
9608 break;
9609
9610 case M_DROR:
fef14a42 9611 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 9612 {
67c0d1eb 9613 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 9614 break;
82dd0097 9615 }
8fc2e39e 9616 used_at = 1;
c80c840e 9617 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9618 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
9619 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
9620 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9621 break;
9622
9623 case M_ROR:
fef14a42 9624 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 9625 {
67c0d1eb 9626 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 9627 break;
82dd0097 9628 }
8fc2e39e 9629 used_at = 1;
c80c840e 9630 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9631 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
9632 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
9633 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9634 break;
9635
771c7ce4
TS
9636 case M_DROR_I:
9637 {
9638 unsigned int rot;
91d6fa6a
NC
9639 char *l;
9640 char *rr;
771c7ce4
TS
9641
9642 if (imm_expr.X_op != O_constant)
82dd0097 9643 as_bad (_("Improper rotate count"));
771c7ce4 9644 rot = imm_expr.X_add_number & 0x3f;
fef14a42 9645 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
9646 {
9647 if (rot >= 32)
df58fc94 9648 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
82dd0097 9649 else
df58fc94 9650 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9651 break;
82dd0097 9652 }
483fc7cd 9653 if (rot == 0)
483fc7cd 9654 {
df58fc94 9655 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9656 break;
483fc7cd 9657 }
91d6fa6a 9658 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
9659 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
9660 rot &= 0x1f;
8fc2e39e 9661 used_at = 1;
df58fc94
RS
9662 macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
9663 macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9664 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9665 }
9666 break;
9667
252b5132 9668 case M_ROR_I:
771c7ce4
TS
9669 {
9670 unsigned int rot;
9671
9672 if (imm_expr.X_op != O_constant)
82dd0097 9673 as_bad (_("Improper rotate count"));
771c7ce4 9674 rot = imm_expr.X_add_number & 0x1f;
fef14a42 9675 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 9676 {
df58fc94 9677 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9678 break;
82dd0097 9679 }
483fc7cd 9680 if (rot == 0)
483fc7cd 9681 {
df58fc94 9682 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9683 break;
483fc7cd 9684 }
8fc2e39e 9685 used_at = 1;
df58fc94
RS
9686 macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
9687 macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9688 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 9689 }
252b5132
RH
9690 break;
9691
252b5132
RH
9692 case M_SEQ:
9693 if (sreg == 0)
67c0d1eb 9694 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 9695 else if (treg == 0)
67c0d1eb 9696 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9697 else
9698 {
67c0d1eb
RS
9699 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9700 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 9701 }
8fc2e39e 9702 break;
252b5132
RH
9703
9704 case M_SEQ_I:
9705 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9706 {
67c0d1eb 9707 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9708 break;
252b5132
RH
9709 }
9710 if (sreg == 0)
9711 {
9712 as_warn (_("Instruction %s: result is always false"),
9713 ip->insn_mo->name);
67c0d1eb 9714 move_register (dreg, 0);
8fc2e39e 9715 break;
252b5132 9716 }
dd3cbb7e
NC
9717 if (CPU_HAS_SEQ (mips_opts.arch)
9718 && -512 <= imm_expr.X_add_number
9719 && imm_expr.X_add_number < 512)
9720 {
9721 macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
750bdd57 9722 (int) imm_expr.X_add_number);
dd3cbb7e
NC
9723 break;
9724 }
252b5132
RH
9725 if (imm_expr.X_op == O_constant
9726 && imm_expr.X_add_number >= 0
9727 && imm_expr.X_add_number < 0x10000)
9728 {
67c0d1eb 9729 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9730 }
9731 else if (imm_expr.X_op == O_constant
9732 && imm_expr.X_add_number > -0x8000
9733 && imm_expr.X_add_number < 0)
9734 {
9735 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 9736 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 9737 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 9738 }
dd3cbb7e
NC
9739 else if (CPU_HAS_SEQ (mips_opts.arch))
9740 {
9741 used_at = 1;
9742 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9743 macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
9744 break;
9745 }
252b5132
RH
9746 else
9747 {
67c0d1eb
RS
9748 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9749 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
9750 used_at = 1;
9751 }
67c0d1eb 9752 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9753 break;
252b5132
RH
9754
9755 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
9756 s = "slt";
9757 goto sge;
9758 case M_SGEU:
9759 s = "sltu";
9760 sge:
67c0d1eb
RS
9761 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
9762 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9763 break;
252b5132
RH
9764
9765 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
9766 case M_SGEU_I:
9767 if (imm_expr.X_op == O_constant
9768 && imm_expr.X_add_number >= -0x8000
9769 && imm_expr.X_add_number < 0x8000)
9770 {
67c0d1eb
RS
9771 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
9772 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9773 }
9774 else
9775 {
67c0d1eb
RS
9776 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9777 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
9778 dreg, sreg, AT);
252b5132
RH
9779 used_at = 1;
9780 }
67c0d1eb 9781 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9782 break;
252b5132
RH
9783
9784 case M_SGT: /* sreg > treg <==> treg < sreg */
9785 s = "slt";
9786 goto sgt;
9787 case M_SGTU:
9788 s = "sltu";
9789 sgt:
67c0d1eb 9790 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 9791 break;
252b5132
RH
9792
9793 case M_SGT_I: /* sreg > I <==> I < sreg */
9794 s = "slt";
9795 goto sgti;
9796 case M_SGTU_I:
9797 s = "sltu";
9798 sgti:
8fc2e39e 9799 used_at = 1;
67c0d1eb
RS
9800 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9801 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
9802 break;
9803
2396cfb9 9804 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
9805 s = "slt";
9806 goto sle;
9807 case M_SLEU:
9808 s = "sltu";
9809 sle:
67c0d1eb
RS
9810 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9811 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9812 break;
252b5132 9813
2396cfb9 9814 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
9815 s = "slt";
9816 goto slei;
9817 case M_SLEU_I:
9818 s = "sltu";
9819 slei:
8fc2e39e 9820 used_at = 1;
67c0d1eb
RS
9821 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9822 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9823 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
9824 break;
9825
9826 case M_SLT_I:
9827 if (imm_expr.X_op == O_constant
9828 && imm_expr.X_add_number >= -0x8000
9829 && imm_expr.X_add_number < 0x8000)
9830 {
67c0d1eb 9831 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9832 break;
252b5132 9833 }
8fc2e39e 9834 used_at = 1;
67c0d1eb
RS
9835 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9836 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
9837 break;
9838
9839 case M_SLTU_I:
9840 if (imm_expr.X_op == O_constant
9841 && imm_expr.X_add_number >= -0x8000
9842 && imm_expr.X_add_number < 0x8000)
9843 {
67c0d1eb 9844 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 9845 BFD_RELOC_LO16);
8fc2e39e 9846 break;
252b5132 9847 }
8fc2e39e 9848 used_at = 1;
67c0d1eb
RS
9849 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9850 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
9851 break;
9852
9853 case M_SNE:
9854 if (sreg == 0)
67c0d1eb 9855 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 9856 else if (treg == 0)
67c0d1eb 9857 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
9858 else
9859 {
67c0d1eb
RS
9860 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9861 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 9862 }
8fc2e39e 9863 break;
252b5132
RH
9864
9865 case M_SNE_I:
9866 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9867 {
67c0d1eb 9868 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 9869 break;
252b5132
RH
9870 }
9871 if (sreg == 0)
9872 {
9873 as_warn (_("Instruction %s: result is always true"),
9874 ip->insn_mo->name);
67c0d1eb
RS
9875 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
9876 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 9877 break;
252b5132 9878 }
dd3cbb7e
NC
9879 if (CPU_HAS_SEQ (mips_opts.arch)
9880 && -512 <= imm_expr.X_add_number
9881 && imm_expr.X_add_number < 512)
9882 {
9883 macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
750bdd57 9884 (int) imm_expr.X_add_number);
dd3cbb7e
NC
9885 break;
9886 }
252b5132
RH
9887 if (imm_expr.X_op == O_constant
9888 && imm_expr.X_add_number >= 0
9889 && imm_expr.X_add_number < 0x10000)
9890 {
67c0d1eb 9891 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9892 }
9893 else if (imm_expr.X_op == O_constant
9894 && imm_expr.X_add_number > -0x8000
9895 && imm_expr.X_add_number < 0)
9896 {
9897 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 9898 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 9899 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 9900 }
dd3cbb7e
NC
9901 else if (CPU_HAS_SEQ (mips_opts.arch))
9902 {
9903 used_at = 1;
9904 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9905 macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
9906 break;
9907 }
252b5132
RH
9908 else
9909 {
67c0d1eb
RS
9910 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9911 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
9912 used_at = 1;
9913 }
67c0d1eb 9914 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 9915 break;
252b5132 9916
df58fc94
RS
9917 case M_SUB_I:
9918 s = "addi";
9919 s2 = "sub";
9920 goto do_subi;
9921 case M_SUBU_I:
9922 s = "addiu";
9923 s2 = "subu";
9924 goto do_subi;
252b5132
RH
9925 case M_DSUB_I:
9926 dbl = 1;
df58fc94
RS
9927 s = "daddi";
9928 s2 = "dsub";
9929 if (!mips_opts.micromips)
9930 goto do_subi;
252b5132 9931 if (imm_expr.X_op == O_constant
df58fc94
RS
9932 && imm_expr.X_add_number > -0x200
9933 && imm_expr.X_add_number <= 0x200)
252b5132 9934 {
df58fc94 9935 macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
8fc2e39e 9936 break;
252b5132 9937 }
df58fc94 9938 goto do_subi_i;
252b5132
RH
9939 case M_DSUBU_I:
9940 dbl = 1;
df58fc94
RS
9941 s = "daddiu";
9942 s2 = "dsubu";
9943 do_subi:
252b5132
RH
9944 if (imm_expr.X_op == O_constant
9945 && imm_expr.X_add_number > -0x8000
9946 && imm_expr.X_add_number <= 0x8000)
9947 {
9948 imm_expr.X_add_number = -imm_expr.X_add_number;
df58fc94 9949 macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9950 break;
252b5132 9951 }
df58fc94 9952 do_subi_i:
8fc2e39e 9953 used_at = 1;
67c0d1eb 9954 load_register (AT, &imm_expr, dbl);
df58fc94 9955 macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
252b5132
RH
9956 break;
9957
9958 case M_TEQ_I:
9959 s = "teq";
9960 goto trap;
9961 case M_TGE_I:
9962 s = "tge";
9963 goto trap;
9964 case M_TGEU_I:
9965 s = "tgeu";
9966 goto trap;
9967 case M_TLT_I:
9968 s = "tlt";
9969 goto trap;
9970 case M_TLTU_I:
9971 s = "tltu";
9972 goto trap;
9973 case M_TNE_I:
9974 s = "tne";
9975 trap:
8fc2e39e 9976 used_at = 1;
67c0d1eb
RS
9977 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9978 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
9979 break;
9980
252b5132 9981 case M_TRUNCWS:
43841e91 9982 case M_TRUNCWD:
df58fc94 9983 gas_assert (!mips_opts.micromips);
0aa27725 9984 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 9985 used_at = 1;
252b5132
RH
9986 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
9987 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
9988
9989 /*
9990 * Is the double cfc1 instruction a bug in the mips assembler;
9991 * or is there a reason for it?
9992 */
7d10b47d 9993 start_noreorder ();
67c0d1eb
RS
9994 macro_build (NULL, "cfc1", "t,G", treg, RA);
9995 macro_build (NULL, "cfc1", "t,G", treg, RA);
9996 macro_build (NULL, "nop", "");
252b5132 9997 expr1.X_add_number = 3;
67c0d1eb 9998 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 9999 expr1.X_add_number = 2;
67c0d1eb
RS
10000 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
10001 macro_build (NULL, "ctc1", "t,G", AT, RA);
10002 macro_build (NULL, "nop", "");
10003 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
10004 dreg, sreg);
10005 macro_build (NULL, "ctc1", "t,G", treg, RA);
10006 macro_build (NULL, "nop", "");
7d10b47d 10007 end_noreorder ();
252b5132
RH
10008 break;
10009
df58fc94
RS
10010 case M_ULH_A:
10011 ab = 1;
252b5132
RH
10012 case M_ULH:
10013 s = "lb";
df58fc94
RS
10014 s2 = "lbu";
10015 off = 1;
10016 goto uld_st;
10017 case M_ULHU_A:
10018 ab = 1;
252b5132
RH
10019 case M_ULHU:
10020 s = "lbu";
df58fc94
RS
10021 s2 = "lbu";
10022 off = 1;
10023 goto uld_st;
10024 case M_ULW_A:
10025 ab = 1;
10026 case M_ULW:
10027 s = "lwl";
10028 s2 = "lwr";
10029 off12 = mips_opts.micromips;
10030 off = 3;
10031 goto uld_st;
10032 case M_ULD_A:
10033 ab = 1;
252b5132
RH
10034 case M_ULD:
10035 s = "ldl";
10036 s2 = "ldr";
df58fc94 10037 off12 = mips_opts.micromips;
252b5132 10038 off = 7;
df58fc94
RS
10039 goto uld_st;
10040 case M_USH_A:
10041 ab = 1;
10042 case M_USH:
10043 s = "sb";
10044 s2 = "sb";
10045 off = 1;
10046 ust = 1;
10047 goto uld_st;
10048 case M_USW_A:
10049 ab = 1;
10050 case M_USW:
10051 s = "swl";
10052 s2 = "swr";
10053 off12 = mips_opts.micromips;
252b5132 10054 off = 3;
df58fc94
RS
10055 ust = 1;
10056 goto uld_st;
10057 case M_USD_A:
10058 ab = 1;
10059 case M_USD:
10060 s = "sdl";
10061 s2 = "sdr";
10062 off12 = mips_opts.micromips;
10063 off = 7;
10064 ust = 1;
10065
10066 uld_st:
10067 if (!ab && offset_expr.X_add_number >= 0x8000 - off)
f71d0d44 10068 as_bad (_("Operand overflow"));
df58fc94
RS
10069
10070 ep = &offset_expr;
10071 expr1.X_add_number = 0;
10072 if (ab)
10073 {
10074 used_at = 1;
10075 tempreg = AT;
10076 load_address (tempreg, ep, &used_at);
10077 if (breg != 0)
10078 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10079 tempreg, tempreg, breg);
10080 breg = tempreg;
10081 tempreg = treg;
10082 ep = &expr1;
10083 }
10084 else if (off12
10085 && (offset_expr.X_op != O_constant
10086 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
10087 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
10088 {
10089 used_at = 1;
10090 tempreg = AT;
10091 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
10092 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10093 breg = tempreg;
10094 tempreg = treg;
10095 ep = &expr1;
10096 }
10097 else if (!ust && treg == breg)
8fc2e39e
TS
10098 {
10099 used_at = 1;
10100 tempreg = AT;
10101 }
252b5132 10102 else
df58fc94 10103 tempreg = treg;
af22f5b2 10104
df58fc94
RS
10105 if (off == 1)
10106 goto ulh_sh;
252b5132 10107
90ecf173 10108 if (!target_big_endian)
df58fc94
RS
10109 ep->X_add_number += off;
10110 if (!off12)
10111 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 10112 else
df58fc94
RS
10113 macro_build (NULL, s, "t,~(b)",
10114 tempreg, (unsigned long) ep->X_add_number, breg);
10115
90ecf173 10116 if (!target_big_endian)
df58fc94 10117 ep->X_add_number -= off;
252b5132 10118 else
df58fc94
RS
10119 ep->X_add_number += off;
10120 if (!off12)
10121 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10122 else
10123 macro_build (NULL, s2, "t,~(b)",
10124 tempreg, (unsigned long) ep->X_add_number, breg);
252b5132 10125
df58fc94
RS
10126 /* If necessary, move the result in tempreg to the final destination. */
10127 if (!ust && treg != tempreg)
10128 {
10129 /* Protect second load's delay slot. */
10130 load_delay_nop ();
10131 move_register (treg, tempreg);
10132 }
8fc2e39e 10133 break;
252b5132 10134
df58fc94 10135 ulh_sh:
d6bc6245 10136 used_at = 1;
df58fc94
RS
10137 if (target_big_endian == ust)
10138 ep->X_add_number += off;
10139 tempreg = ust || ab ? treg : AT;
10140 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10141
10142 /* For halfword transfers we need a temporary register to shuffle
10143 bytes. Unfortunately for M_USH_A we have none available before
10144 the next store as AT holds the base address. We deal with this
10145 case by clobbering TREG and then restoring it as with ULH. */
10146 tempreg = ust == ab ? treg : AT;
10147 if (ust)
10148 macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10149
10150 if (target_big_endian == ust)
10151 ep->X_add_number -= off;
252b5132 10152 else
df58fc94
RS
10153 ep->X_add_number += off;
10154 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 10155
df58fc94
RS
10156 /* For M_USH_A re-retrieve the LSB. */
10157 if (ust && ab)
10158 {
10159 if (target_big_endian)
10160 ep->X_add_number += off;
10161 else
10162 ep->X_add_number -= off;
10163 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
10164 }
10165 /* For ULH and M_USH_A OR the LSB in. */
10166 if (!ust || ab)
10167 {
10168 tempreg = !ab ? AT : treg;
10169 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10170 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10171 }
252b5132
RH
10172 break;
10173
10174 default:
10175 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 10176 are added dynamically. */
252b5132
RH
10177 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10178 break;
10179 }
741fe287 10180 if (!mips_opts.at && used_at)
8fc2e39e 10181 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
10182}
10183
10184/* Implement macros in mips16 mode. */
10185
10186static void
17a2f251 10187mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
10188{
10189 int mask;
10190 int xreg, yreg, zreg, tmp;
252b5132
RH
10191 expressionS expr1;
10192 int dbl;
10193 const char *s, *s2, *s3;
10194
10195 mask = ip->insn_mo->mask;
10196
bf12938e
RS
10197 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10198 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10199 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 10200
252b5132
RH
10201 expr1.X_op = O_constant;
10202 expr1.X_op_symbol = NULL;
10203 expr1.X_add_symbol = NULL;
10204 expr1.X_add_number = 1;
10205
10206 dbl = 0;
10207
10208 switch (mask)
10209 {
10210 default:
b37df7c4 10211 abort ();
252b5132
RH
10212
10213 case M_DDIV_3:
10214 dbl = 1;
10215 case M_DIV_3:
10216 s = "mflo";
10217 goto do_div3;
10218 case M_DREM_3:
10219 dbl = 1;
10220 case M_REM_3:
10221 s = "mfhi";
10222 do_div3:
7d10b47d 10223 start_noreorder ();
67c0d1eb 10224 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 10225 expr1.X_add_number = 2;
67c0d1eb
RS
10226 macro_build (&expr1, "bnez", "x,p", yreg);
10227 macro_build (NULL, "break", "6", 7);
bdaaa2e1 10228
252b5132
RH
10229 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10230 since that causes an overflow. We should do that as well,
10231 but I don't see how to do the comparisons without a temporary
10232 register. */
7d10b47d 10233 end_noreorder ();
67c0d1eb 10234 macro_build (NULL, s, "x", zreg);
252b5132
RH
10235 break;
10236
10237 case M_DIVU_3:
10238 s = "divu";
10239 s2 = "mflo";
10240 goto do_divu3;
10241 case M_REMU_3:
10242 s = "divu";
10243 s2 = "mfhi";
10244 goto do_divu3;
10245 case M_DDIVU_3:
10246 s = "ddivu";
10247 s2 = "mflo";
10248 goto do_divu3;
10249 case M_DREMU_3:
10250 s = "ddivu";
10251 s2 = "mfhi";
10252 do_divu3:
7d10b47d 10253 start_noreorder ();
67c0d1eb 10254 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 10255 expr1.X_add_number = 2;
67c0d1eb
RS
10256 macro_build (&expr1, "bnez", "x,p", yreg);
10257 macro_build (NULL, "break", "6", 7);
7d10b47d 10258 end_noreorder ();
67c0d1eb 10259 macro_build (NULL, s2, "x", zreg);
252b5132
RH
10260 break;
10261
10262 case M_DMUL:
10263 dbl = 1;
10264 case M_MUL:
67c0d1eb
RS
10265 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10266 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 10267 break;
252b5132
RH
10268
10269 case M_DSUBU_I:
10270 dbl = 1;
10271 goto do_subu;
10272 case M_SUBU_I:
10273 do_subu:
10274 if (imm_expr.X_op != O_constant)
10275 as_bad (_("Unsupported large constant"));
10276 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10277 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
10278 break;
10279
10280 case M_SUBU_I_2:
10281 if (imm_expr.X_op != O_constant)
10282 as_bad (_("Unsupported large constant"));
10283 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10284 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
10285 break;
10286
10287 case M_DSUBU_I_2:
10288 if (imm_expr.X_op != O_constant)
10289 as_bad (_("Unsupported large constant"));
10290 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10291 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
10292 break;
10293
10294 case M_BEQ:
10295 s = "cmp";
10296 s2 = "bteqz";
10297 goto do_branch;
10298 case M_BNE:
10299 s = "cmp";
10300 s2 = "btnez";
10301 goto do_branch;
10302 case M_BLT:
10303 s = "slt";
10304 s2 = "btnez";
10305 goto do_branch;
10306 case M_BLTU:
10307 s = "sltu";
10308 s2 = "btnez";
10309 goto do_branch;
10310 case M_BLE:
10311 s = "slt";
10312 s2 = "bteqz";
10313 goto do_reverse_branch;
10314 case M_BLEU:
10315 s = "sltu";
10316 s2 = "bteqz";
10317 goto do_reverse_branch;
10318 case M_BGE:
10319 s = "slt";
10320 s2 = "bteqz";
10321 goto do_branch;
10322 case M_BGEU:
10323 s = "sltu";
10324 s2 = "bteqz";
10325 goto do_branch;
10326 case M_BGT:
10327 s = "slt";
10328 s2 = "btnez";
10329 goto do_reverse_branch;
10330 case M_BGTU:
10331 s = "sltu";
10332 s2 = "btnez";
10333
10334 do_reverse_branch:
10335 tmp = xreg;
10336 xreg = yreg;
10337 yreg = tmp;
10338
10339 do_branch:
67c0d1eb
RS
10340 macro_build (NULL, s, "x,y", xreg, yreg);
10341 macro_build (&offset_expr, s2, "p");
252b5132
RH
10342 break;
10343
10344 case M_BEQ_I:
10345 s = "cmpi";
10346 s2 = "bteqz";
10347 s3 = "x,U";
10348 goto do_branch_i;
10349 case M_BNE_I:
10350 s = "cmpi";
10351 s2 = "btnez";
10352 s3 = "x,U";
10353 goto do_branch_i;
10354 case M_BLT_I:
10355 s = "slti";
10356 s2 = "btnez";
10357 s3 = "x,8";
10358 goto do_branch_i;
10359 case M_BLTU_I:
10360 s = "sltiu";
10361 s2 = "btnez";
10362 s3 = "x,8";
10363 goto do_branch_i;
10364 case M_BLE_I:
10365 s = "slti";
10366 s2 = "btnez";
10367 s3 = "x,8";
10368 goto do_addone_branch_i;
10369 case M_BLEU_I:
10370 s = "sltiu";
10371 s2 = "btnez";
10372 s3 = "x,8";
10373 goto do_addone_branch_i;
10374 case M_BGE_I:
10375 s = "slti";
10376 s2 = "bteqz";
10377 s3 = "x,8";
10378 goto do_branch_i;
10379 case M_BGEU_I:
10380 s = "sltiu";
10381 s2 = "bteqz";
10382 s3 = "x,8";
10383 goto do_branch_i;
10384 case M_BGT_I:
10385 s = "slti";
10386 s2 = "bteqz";
10387 s3 = "x,8";
10388 goto do_addone_branch_i;
10389 case M_BGTU_I:
10390 s = "sltiu";
10391 s2 = "bteqz";
10392 s3 = "x,8";
10393
10394 do_addone_branch_i:
10395 if (imm_expr.X_op != O_constant)
10396 as_bad (_("Unsupported large constant"));
10397 ++imm_expr.X_add_number;
10398
10399 do_branch_i:
67c0d1eb
RS
10400 macro_build (&imm_expr, s, s3, xreg);
10401 macro_build (&offset_expr, s2, "p");
252b5132
RH
10402 break;
10403
10404 case M_ABS:
10405 expr1.X_add_number = 0;
67c0d1eb 10406 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 10407 if (xreg != yreg)
67c0d1eb 10408 move_register (xreg, yreg);
252b5132 10409 expr1.X_add_number = 2;
67c0d1eb
RS
10410 macro_build (&expr1, "bteqz", "p");
10411 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
10412 }
10413}
10414
10415/* For consistency checking, verify that all bits are specified either
10416 by the match/mask part of the instruction definition, or by the
10417 operand list. */
10418static int
17a2f251 10419validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
10420{
10421 const char *p = opc->args;
10422 char c;
10423 unsigned long used_bits = opc->mask;
10424
10425 if ((used_bits & opc->match) != opc->match)
10426 {
10427 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10428 opc->name, opc->args);
10429 return 0;
10430 }
10431#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
10432 while (*p)
10433 switch (c = *p++)
10434 {
10435 case ',': break;
10436 case '(': break;
10437 case ')': break;
af7ee8bf
CD
10438 case '+':
10439 switch (c = *p++)
10440 {
9bcd4f99
TS
10441 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
10442 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
10443 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
10444 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
af7ee8bf
CD
10445 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10446 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10447 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
10448 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
10449 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
10450 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10451 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10452 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10453 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10454 case 'I': break;
b015e599 10455 case 'J': USE_BITS (OP_MASK_CODE10, OP_SH_CODE10); break;
ef2e4d86
CF
10456 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10457 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
10458 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
bb35fb24
NC
10459 case 'x': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10460 case 'X': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10461 case 'p': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
10462 case 'P': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
dd3cbb7e 10463 case 'Q': USE_BITS (OP_MASK_SEQI, OP_SH_SEQI); break;
bb35fb24
NC
10464 case 's': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
10465 case 'S': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
98675402
RS
10466 case 'z': USE_BITS (OP_MASK_RZ, OP_SH_RZ); break;
10467 case 'Z': USE_BITS (OP_MASK_FZ, OP_SH_FZ); break;
10468 case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10469 case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10470 case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
bb35fb24 10471
af7ee8bf
CD
10472 default:
10473 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10474 c, opc->name, opc->args);
10475 return 0;
10476 }
10477 break;
252b5132
RH
10478 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10479 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10480 case 'A': break;
4372b673 10481 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
10482 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
10483 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10484 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10485 case 'F': break;
10486 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 10487 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 10488 case 'I': break;
e972090a 10489 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 10490 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10491 case 'L': break;
10492 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
10493 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
10494 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
10495 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
10496 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10497 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
10498 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10499 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10500 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10501 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
10502 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10503 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10504 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10505 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
10506 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10507 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
10508 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10509 case 'f': break;
10510 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
10511 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10512 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10513 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
10514 case 'l': break;
10515 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10516 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10517 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
10518 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10519 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10520 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10521 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10522 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10523 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10524 case 'x': break;
10525 case 'z': break;
10526 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
10527 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
10528 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
10529 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
10530 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
10531 case '[': break;
10532 case ']': break;
620edafd 10533 case '1': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8b082fb1 10534 case '2': USE_BITS (OP_MASK_BP, OP_SH_BP); break;
74cd071d
CF
10535 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
10536 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
10537 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
10538 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10539 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
10540 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
10541 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
10542 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
10543 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
10544 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
10545 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
10546 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
10547 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
10548 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
10549 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
dec0624d
MR
10550 case '\\': USE_BITS (OP_MASK_3BITPOS, OP_SH_3BITPOS); break;
10551 case '~': USE_BITS (OP_MASK_OFFSET12, OP_SH_OFFSET12); break;
ef2e4d86 10552 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10553 default:
10554 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10555 c, opc->name, opc->args);
10556 return 0;
10557 }
10558#undef USE_BITS
10559 if (used_bits != 0xffffffff)
10560 {
10561 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
10562 ~used_bits & 0xffffffff, opc->name, opc->args);
10563 return 0;
10564 }
10565 return 1;
10566}
10567
df58fc94
RS
10568/* For consistency checking, verify that the length implied matches the
10569 major opcode and that all bits are specified either by the match/mask
10570 part of the instruction definition, or by the operand list. */
10571
10572static int
10573validate_micromips_insn (const struct mips_opcode *opc)
10574{
10575 unsigned long match = opc->match;
10576 unsigned long mask = opc->mask;
10577 const char *p = opc->args;
10578 unsigned long insn_bits;
10579 unsigned long used_bits;
10580 unsigned long major;
10581 unsigned int length;
10582 char e;
10583 char c;
10584
10585 if ((mask & match) != match)
10586 {
10587 as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
10588 opc->name, opc->args);
10589 return 0;
10590 }
10591 length = micromips_insn_length (opc);
10592 if (length != 2 && length != 4)
10593 {
10594 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
10595 "%s %s"), length, opc->name, opc->args);
10596 return 0;
10597 }
10598 major = match >> (10 + 8 * (length - 2));
10599 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
10600 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
10601 {
10602 as_bad (_("Internal error: bad microMIPS opcode "
10603 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
10604 return 0;
10605 }
10606
10607 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
10608 insn_bits = 1 << 4 * length;
10609 insn_bits <<= 4 * length;
10610 insn_bits -= 1;
10611 used_bits = mask;
10612#define USE_BITS(field) \
10613 (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
10614 while (*p)
10615 switch (c = *p++)
10616 {
10617 case ',': break;
10618 case '(': break;
10619 case ')': break;
10620 case '+':
10621 e = c;
10622 switch (c = *p++)
10623 {
10624 case 'A': USE_BITS (EXTLSB); break;
10625 case 'B': USE_BITS (INSMSB); break;
10626 case 'C': USE_BITS (EXTMSBD); break;
10627 case 'D': USE_BITS (RS); USE_BITS (SEL); break;
10628 case 'E': USE_BITS (EXTLSB); break;
10629 case 'F': USE_BITS (INSMSB); break;
10630 case 'G': USE_BITS (EXTMSBD); break;
10631 case 'H': USE_BITS (EXTMSBD); break;
10632 default:
10633 as_bad (_("Internal error: bad mips opcode "
10634 "(unknown extension operand type `%c%c'): %s %s"),
10635 e, c, opc->name, opc->args);
10636 return 0;
10637 }
10638 break;
10639 case 'm':
10640 e = c;
10641 switch (c = *p++)
10642 {
10643 case 'A': USE_BITS (IMMA); break;
10644 case 'B': USE_BITS (IMMB); break;
10645 case 'C': USE_BITS (IMMC); break;
10646 case 'D': USE_BITS (IMMD); break;
10647 case 'E': USE_BITS (IMME); break;
10648 case 'F': USE_BITS (IMMF); break;
10649 case 'G': USE_BITS (IMMG); break;
10650 case 'H': USE_BITS (IMMH); break;
10651 case 'I': USE_BITS (IMMI); break;
10652 case 'J': USE_BITS (IMMJ); break;
10653 case 'L': USE_BITS (IMML); break;
10654 case 'M': USE_BITS (IMMM); break;
10655 case 'N': USE_BITS (IMMN); break;
10656 case 'O': USE_BITS (IMMO); break;
10657 case 'P': USE_BITS (IMMP); break;
10658 case 'Q': USE_BITS (IMMQ); break;
10659 case 'U': USE_BITS (IMMU); break;
10660 case 'W': USE_BITS (IMMW); break;
10661 case 'X': USE_BITS (IMMX); break;
10662 case 'Y': USE_BITS (IMMY); break;
10663 case 'Z': break;
10664 case 'a': break;
10665 case 'b': USE_BITS (MB); break;
10666 case 'c': USE_BITS (MC); break;
10667 case 'd': USE_BITS (MD); break;
10668 case 'e': USE_BITS (ME); break;
10669 case 'f': USE_BITS (MF); break;
10670 case 'g': USE_BITS (MG); break;
10671 case 'h': USE_BITS (MH); break;
10672 case 'i': USE_BITS (MI); break;
10673 case 'j': USE_BITS (MJ); break;
10674 case 'l': USE_BITS (ML); break;
10675 case 'm': USE_BITS (MM); break;
10676 case 'n': USE_BITS (MN); break;
10677 case 'p': USE_BITS (MP); break;
10678 case 'q': USE_BITS (MQ); break;
10679 case 'r': break;
10680 case 's': break;
10681 case 't': break;
10682 case 'x': break;
10683 case 'y': break;
10684 case 'z': break;
10685 default:
10686 as_bad (_("Internal error: bad mips opcode "
10687 "(unknown extension operand type `%c%c'): %s %s"),
10688 e, c, opc->name, opc->args);
10689 return 0;
10690 }
10691 break;
10692 case '.': USE_BITS (OFFSET10); break;
10693 case '1': USE_BITS (STYPE); break;
03f66e8a
MR
10694 case '2': USE_BITS (BP); break;
10695 case '3': USE_BITS (SA3); break;
10696 case '4': USE_BITS (SA4); break;
10697 case '5': USE_BITS (IMM8); break;
10698 case '6': USE_BITS (RS); break;
10699 case '7': USE_BITS (DSPACC); break;
10700 case '8': USE_BITS (WRDSP); break;
10701 case '0': USE_BITS (DSPSFT); break;
df58fc94
RS
10702 case '<': USE_BITS (SHAMT); break;
10703 case '>': USE_BITS (SHAMT); break;
03f66e8a 10704 case '@': USE_BITS (IMM10); break;
df58fc94
RS
10705 case 'B': USE_BITS (CODE10); break;
10706 case 'C': USE_BITS (COPZ); break;
10707 case 'D': USE_BITS (FD); break;
10708 case 'E': USE_BITS (RT); break;
10709 case 'G': USE_BITS (RS); break;
444d75be 10710 case 'H': USE_BITS (SEL); break;
df58fc94
RS
10711 case 'K': USE_BITS (RS); break;
10712 case 'M': USE_BITS (CCC); break;
10713 case 'N': USE_BITS (BCC); break;
10714 case 'R': USE_BITS (FR); break;
10715 case 'S': USE_BITS (FS); break;
10716 case 'T': USE_BITS (FT); break;
10717 case 'V': USE_BITS (FS); break;
dec0624d 10718 case '\\': USE_BITS (3BITPOS); break;
03f66e8a 10719 case '^': USE_BITS (RD); break;
df58fc94
RS
10720 case 'a': USE_BITS (TARGET); break;
10721 case 'b': USE_BITS (RS); break;
10722 case 'c': USE_BITS (CODE); break;
10723 case 'd': USE_BITS (RD); break;
10724 case 'h': USE_BITS (PREFX); break;
10725 case 'i': USE_BITS (IMMEDIATE); break;
10726 case 'j': USE_BITS (DELTA); break;
10727 case 'k': USE_BITS (CACHE); break;
10728 case 'n': USE_BITS (RT); break;
10729 case 'o': USE_BITS (DELTA); break;
10730 case 'p': USE_BITS (DELTA); break;
10731 case 'q': USE_BITS (CODE2); break;
10732 case 'r': USE_BITS (RS); break;
10733 case 's': USE_BITS (RS); break;
10734 case 't': USE_BITS (RT); break;
10735 case 'u': USE_BITS (IMMEDIATE); break;
10736 case 'v': USE_BITS (RS); break;
10737 case 'w': USE_BITS (RT); break;
10738 case 'y': USE_BITS (RS3); break;
10739 case 'z': break;
10740 case '|': USE_BITS (TRAP); break;
10741 case '~': USE_BITS (OFFSET12); break;
10742 default:
10743 as_bad (_("Internal error: bad microMIPS opcode "
10744 "(unknown operand type `%c'): %s %s"),
10745 c, opc->name, opc->args);
10746 return 0;
10747 }
10748#undef USE_BITS
10749 if (used_bits != insn_bits)
10750 {
10751 if (~used_bits & insn_bits)
10752 as_bad (_("Internal error: bad microMIPS opcode "
10753 "(bits 0x%lx undefined): %s %s"),
10754 ~used_bits & insn_bits, opc->name, opc->args);
10755 if (used_bits & ~insn_bits)
10756 as_bad (_("Internal error: bad microMIPS opcode "
10757 "(bits 0x%lx defined): %s %s"),
10758 used_bits & ~insn_bits, opc->name, opc->args);
10759 return 0;
10760 }
10761 return 1;
10762}
10763
9bcd4f99
TS
10764/* UDI immediates. */
10765struct mips_immed {
10766 char type;
10767 unsigned int shift;
10768 unsigned long mask;
10769 const char * desc;
10770};
10771
10772static const struct mips_immed mips_immed[] = {
10773 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
10774 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
10775 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
10776 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
10777 { 0,0,0,0 }
10778};
10779
7455baf8
TS
10780/* Check whether an odd floating-point register is allowed. */
10781static int
10782mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
10783{
10784 const char *s = insn->name;
10785
10786 if (insn->pinfo == INSN_MACRO)
10787 /* Let a macro pass, we'll catch it later when it is expanded. */
10788 return 1;
10789
e407c74b 10790 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || (mips_opts.arch == CPU_R5900))
7455baf8
TS
10791 {
10792 /* Allow odd registers for single-precision ops. */
10793 switch (insn->pinfo & (FP_S | FP_D))
10794 {
10795 case FP_S:
10796 case 0:
10797 return 1; /* both single precision - ok */
10798 case FP_D:
10799 return 0; /* both double precision - fail */
10800 default:
10801 break;
10802 }
10803
10804 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
10805 s = strchr (insn->name, '.');
10806 if (argnum == 2)
10807 s = s != NULL ? strchr (s + 1, '.') : NULL;
10808 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
10809 }
10810
10811 /* Single-precision coprocessor loads and moves are OK too. */
10812 if ((insn->pinfo & FP_S)
10813 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
10814 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
10815 return 1;
10816
10817 return 0;
10818}
10819
df58fc94
RS
10820/* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
10821 taking bits from BIT up. */
10822static int
10823expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
10824{
10825 return (ep->X_op == O_constant
10826 && (ep->X_add_number & ((1 << bit) - 1)) == 0
10827 && ep->X_add_number >= min << bit
10828 && ep->X_add_number < max << bit);
10829}
10830
252b5132
RH
10831/* This routine assembles an instruction into its binary format. As a
10832 side effect, it sets one of the global variables imm_reloc or
10833 offset_reloc to the type of relocation to do if one of the operands
10834 is an address expression. */
10835
10836static void
17a2f251 10837mips_ip (char *str, struct mips_cl_insn *ip)
252b5132 10838{
df58fc94
RS
10839 bfd_boolean wrong_delay_slot_insns = FALSE;
10840 bfd_boolean need_delay_slot_ok = TRUE;
10841 struct mips_opcode *firstinsn = NULL;
10842 const struct mips_opcode *past;
10843 struct hash_control *hash;
252b5132
RH
10844 char *s;
10845 const char *args;
43841e91 10846 char c = 0;
252b5132
RH
10847 struct mips_opcode *insn;
10848 char *argsStart;
10849 unsigned int regno;
34224acf 10850 unsigned int lastregno;
df58fc94 10851 unsigned int destregno = 0;
af7ee8bf 10852 unsigned int lastpos = 0;
071742cf 10853 unsigned int limlo, limhi;
f02d8318 10854 int sizelo;
252b5132 10855 char *s_reset;
74cd071d 10856 offsetT min_range, max_range;
df58fc94 10857 long opend;
a40bc9dd 10858 char *name;
707bfff6
TS
10859 int argnum;
10860 unsigned int rtype;
df58fc94 10861 char *dot;
a40bc9dd 10862 long end;
252b5132
RH
10863
10864 insn_error = NULL;
10865
df58fc94
RS
10866 if (mips_opts.micromips)
10867 {
10868 hash = micromips_op_hash;
10869 past = &micromips_opcodes[bfd_micromips_num_opcodes];
10870 }
10871 else
10872 {
10873 hash = op_hash;
10874 past = &mips_opcodes[NUMOPCODES];
10875 }
10876 forced_insn_length = 0;
252b5132 10877 insn = NULL;
252b5132 10878
df58fc94 10879 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
10880 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
10881 continue;
bdaaa2e1 10882
a40bc9dd
RS
10883 /* Make a copy of the instruction so that we can fiddle with it. */
10884 name = alloca (end + 1);
10885 memcpy (name, str, end);
10886 name[end] = '\0';
252b5132 10887
df58fc94
RS
10888 for (;;)
10889 {
10890 insn = (struct mips_opcode *) hash_find (hash, name);
10891
10892 if (insn != NULL || !mips_opts.micromips)
10893 break;
10894 if (forced_insn_length)
10895 break;
10896
10897 /* See if there's an instruction size override suffix,
10898 either `16' or `32', at the end of the mnemonic proper,
10899 that defines the operation, i.e. before the first `.'
10900 character if any. Strip it and retry. */
10901 dot = strchr (name, '.');
10902 opend = dot != NULL ? dot - name : end;
10903 if (opend < 3)
10904 break;
10905 if (name[opend - 2] == '1' && name[opend - 1] == '6')
10906 forced_insn_length = 2;
10907 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
10908 forced_insn_length = 4;
10909 else
10910 break;
10911 memcpy (name + opend - 2, name + opend, end - opend + 1);
10912 }
252b5132
RH
10913 if (insn == NULL)
10914 {
a40bc9dd
RS
10915 insn_error = _("Unrecognized opcode");
10916 return;
252b5132
RH
10917 }
10918
df58fc94
RS
10919 /* For microMIPS instructions placed in a fixed-length branch delay slot
10920 we make up to two passes over the relevant fragment of the opcode
10921 table. First we try instructions that meet the delay slot's length
10922 requirement. If none matched, then we retry with the remaining ones
10923 and if one matches, then we use it and then issue an appropriate
10924 warning later on. */
a40bc9dd 10925 argsStart = s = str + end;
252b5132
RH
10926 for (;;)
10927 {
df58fc94
RS
10928 bfd_boolean delay_slot_ok;
10929 bfd_boolean size_ok;
b34976b6 10930 bfd_boolean ok;
252b5132 10931
a40bc9dd 10932 gas_assert (strcmp (insn->name, name) == 0);
252b5132 10933
f79e2745 10934 ok = is_opcode_valid (insn);
df58fc94
RS
10935 size_ok = is_size_valid (insn);
10936 delay_slot_ok = is_delay_slot_valid (insn);
10937 if (!delay_slot_ok && !wrong_delay_slot_insns)
252b5132 10938 {
df58fc94
RS
10939 firstinsn = insn;
10940 wrong_delay_slot_insns = TRUE;
10941 }
10942 if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
10943 {
10944 static char buf[256];
10945
10946 if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
252b5132
RH
10947 {
10948 ++insn;
10949 continue;
10950 }
df58fc94 10951 if (wrong_delay_slot_insns && need_delay_slot_ok)
beae10d5 10952 {
df58fc94
RS
10953 gas_assert (firstinsn);
10954 need_delay_slot_ok = FALSE;
10955 past = insn + 1;
10956 insn = firstinsn;
10957 continue;
252b5132 10958 }
df58fc94
RS
10959
10960 if (insn_error)
10961 return;
10962
10963 if (!ok)
7bd942df 10964 sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
df58fc94
RS
10965 mips_cpu_info_from_arch (mips_opts.arch)->name,
10966 mips_cpu_info_from_isa (mips_opts.isa)->name);
10967 else
10968 sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
10969 8 * forced_insn_length);
10970 insn_error = buf;
10971
10972 return;
252b5132
RH
10973 }
10974
1e915849 10975 create_insn (ip, insn);
268f6bed 10976 insn_error = NULL;
707bfff6 10977 argnum = 1;
24864476 10978 lastregno = 0xffffffff;
252b5132
RH
10979 for (args = insn->args;; ++args)
10980 {
deec1734
CD
10981 int is_mdmx;
10982
ad8d3bb3 10983 s += strspn (s, " \t");
deec1734 10984 is_mdmx = 0;
252b5132
RH
10985 switch (*args)
10986 {
10987 case '\0': /* end of args */
10988 if (*s == '\0')
10989 return;
10990 break;
10991
03f66e8a
MR
10992 case '2':
10993 /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
10994 code) or 14 (for microMIPS code). */
8b082fb1
TS
10995 my_getExpression (&imm_expr, s);
10996 check_absolute_expr (ip, &imm_expr);
10997 if ((unsigned long) imm_expr.X_add_number != 1
10998 && (unsigned long) imm_expr.X_add_number != 3)
10999 {
11000 as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
11001 (unsigned long) imm_expr.X_add_number);
11002 }
03f66e8a
MR
11003 INSERT_OPERAND (mips_opts.micromips,
11004 BP, *ip, imm_expr.X_add_number);
8b082fb1
TS
11005 imm_expr.X_op = O_absent;
11006 s = expr_end;
11007 continue;
11008
03f66e8a
MR
11009 case '3':
11010 /* DSP 3-bit unsigned immediate in bit 13 (for standard MIPS
11011 code) or 21 (for microMIPS code). */
11012 {
11013 unsigned long mask = (mips_opts.micromips
11014 ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
11015
11016 my_getExpression (&imm_expr, s);
11017 check_absolute_expr (ip, &imm_expr);
11018 if ((unsigned long) imm_expr.X_add_number > mask)
11019 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11020 mask, (unsigned long) imm_expr.X_add_number);
11021 INSERT_OPERAND (mips_opts.micromips,
11022 SA3, *ip, imm_expr.X_add_number);
11023 imm_expr.X_op = O_absent;
11024 s = expr_end;
11025 }
74cd071d
CF
11026 continue;
11027
03f66e8a
MR
11028 case '4':
11029 /* DSP 4-bit unsigned immediate in bit 12 (for standard MIPS
11030 code) or 21 (for microMIPS code). */
11031 {
11032 unsigned long mask = (mips_opts.micromips
11033 ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
11034
11035 my_getExpression (&imm_expr, s);
11036 check_absolute_expr (ip, &imm_expr);
11037 if ((unsigned long) imm_expr.X_add_number > mask)
11038 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11039 mask, (unsigned long) imm_expr.X_add_number);
11040 INSERT_OPERAND (mips_opts.micromips,
11041 SA4, *ip, imm_expr.X_add_number);
11042 imm_expr.X_op = O_absent;
11043 s = expr_end;
11044 }
74cd071d
CF
11045 continue;
11046
03f66e8a
MR
11047 case '5':
11048 /* DSP 8-bit unsigned immediate in bit 13 (for standard MIPS
11049 code) or 16 (for microMIPS code). */
11050 {
11051 unsigned long mask = (mips_opts.micromips
11052 ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
11053
11054 my_getExpression (&imm_expr, s);
11055 check_absolute_expr (ip, &imm_expr);
11056 if ((unsigned long) imm_expr.X_add_number > mask)
11057 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11058 mask, (unsigned long) imm_expr.X_add_number);
11059 INSERT_OPERAND (mips_opts.micromips,
11060 IMM8, *ip, imm_expr.X_add_number);
11061 imm_expr.X_op = O_absent;
11062 s = expr_end;
11063 }
74cd071d
CF
11064 continue;
11065
03f66e8a
MR
11066 case '6':
11067 /* DSP 5-bit unsigned immediate in bit 16 (for standard MIPS
11068 code) or 21 (for microMIPS code). */
11069 {
11070 unsigned long mask = (mips_opts.micromips
11071 ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
11072
11073 my_getExpression (&imm_expr, s);
11074 check_absolute_expr (ip, &imm_expr);
11075 if ((unsigned long) imm_expr.X_add_number > mask)
11076 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11077 mask, (unsigned long) imm_expr.X_add_number);
11078 INSERT_OPERAND (mips_opts.micromips,
11079 RS, *ip, imm_expr.X_add_number);
11080 imm_expr.X_op = O_absent;
11081 s = expr_end;
11082 }
74cd071d
CF
11083 continue;
11084
90ecf173 11085 case '7': /* Four DSP accumulators in bits 11,12. */
03f66e8a
MR
11086 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11087 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
11088 {
11089 regno = s[3] - '0';
11090 s += 4;
03f66e8a 11091 INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
74cd071d
CF
11092 continue;
11093 }
11094 else
11095 as_bad (_("Invalid dsp acc register"));
11096 break;
11097
03f66e8a
MR
11098 case '8':
11099 /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
11100 code) or 14 (for microMIPS code). */
11101 {
11102 unsigned long mask = (mips_opts.micromips
11103 ? MICROMIPSOP_MASK_WRDSP
11104 : OP_MASK_WRDSP);
11105
11106 my_getExpression (&imm_expr, s);
11107 check_absolute_expr (ip, &imm_expr);
11108 if ((unsigned long) imm_expr.X_add_number > mask)
11109 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11110 mask, (unsigned long) imm_expr.X_add_number);
11111 INSERT_OPERAND (mips_opts.micromips,
11112 WRDSP, *ip, imm_expr.X_add_number);
11113 imm_expr.X_op = O_absent;
11114 s = expr_end;
11115 }
74cd071d
CF
11116 continue;
11117
90ecf173 11118 case '9': /* Four DSP accumulators in bits 21,22. */
df58fc94 11119 gas_assert (!mips_opts.micromips);
03f66e8a
MR
11120 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11121 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
11122 {
11123 regno = s[3] - '0';
11124 s += 4;
df58fc94 11125 INSERT_OPERAND (0, DSPACC_S, *ip, regno);
74cd071d
CF
11126 continue;
11127 }
11128 else
11129 as_bad (_("Invalid dsp acc register"));
11130 break;
11131
03f66e8a
MR
11132 case '0':
11133 /* DSP 6-bit signed immediate in bit 16 (for standard MIPS
11134 code) or 20 (for microMIPS code). */
11135 {
11136 long mask = (mips_opts.micromips
11137 ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
11138
11139 my_getExpression (&imm_expr, s);
11140 check_absolute_expr (ip, &imm_expr);
11141 min_range = -((mask + 1) >> 1);
11142 max_range = ((mask + 1) >> 1) - 1;
11143 if (imm_expr.X_add_number < min_range
11144 || imm_expr.X_add_number > max_range)
a9e24354
TS
11145 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11146 (long) min_range, (long) max_range,
11147 (long) imm_expr.X_add_number);
03f66e8a
MR
11148 INSERT_OPERAND (mips_opts.micromips,
11149 DSPSFT, *ip, imm_expr.X_add_number);
11150 imm_expr.X_op = O_absent;
11151 s = expr_end;
11152 }
74cd071d
CF
11153 continue;
11154
90ecf173 11155 case '\'': /* DSP 6-bit unsigned immediate in bit 16. */
df58fc94 11156 gas_assert (!mips_opts.micromips);
74cd071d
CF
11157 my_getExpression (&imm_expr, s);
11158 check_absolute_expr (ip, &imm_expr);
11159 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
11160 {
a9e24354
TS
11161 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11162 OP_MASK_RDDSP,
11163 (unsigned long) imm_expr.X_add_number);
74cd071d 11164 }
df58fc94 11165 INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
11166 imm_expr.X_op = O_absent;
11167 s = expr_end;
11168 continue;
11169
90ecf173 11170 case ':': /* DSP 7-bit signed immediate in bit 19. */
df58fc94 11171 gas_assert (!mips_opts.micromips);
74cd071d
CF
11172 my_getExpression (&imm_expr, s);
11173 check_absolute_expr (ip, &imm_expr);
11174 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
11175 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
11176 if (imm_expr.X_add_number < min_range ||
11177 imm_expr.X_add_number > max_range)
11178 {
a9e24354
TS
11179 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11180 (long) min_range, (long) max_range,
11181 (long) imm_expr.X_add_number);
74cd071d 11182 }
df58fc94 11183 INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
74cd071d
CF
11184 imm_expr.X_op = O_absent;
11185 s = expr_end;
11186 continue;
11187
90ecf173 11188 case '@': /* DSP 10-bit signed immediate in bit 16. */
03f66e8a
MR
11189 {
11190 long mask = (mips_opts.micromips
11191 ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
11192
11193 my_getExpression (&imm_expr, s);
11194 check_absolute_expr (ip, &imm_expr);
11195 min_range = -((mask + 1) >> 1);
11196 max_range = ((mask + 1) >> 1) - 1;
11197 if (imm_expr.X_add_number < min_range
11198 || imm_expr.X_add_number > max_range)
a9e24354
TS
11199 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11200 (long) min_range, (long) max_range,
11201 (long) imm_expr.X_add_number);
03f66e8a
MR
11202 INSERT_OPERAND (mips_opts.micromips,
11203 IMM10, *ip, imm_expr.X_add_number);
11204 imm_expr.X_op = O_absent;
11205 s = expr_end;
11206 }
11207 continue;
11208
11209 case '^': /* DSP 5-bit unsigned immediate in bit 11. */
11210 gas_assert (mips_opts.micromips);
11211 my_getExpression (&imm_expr, s);
11212 check_absolute_expr (ip, &imm_expr);
11213 if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11214 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11215 MICROMIPSOP_MASK_RD,
11216 (unsigned long) imm_expr.X_add_number);
11217 INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
74cd071d
CF
11218 imm_expr.X_op = O_absent;
11219 s = expr_end;
11220 continue;
11221
a9e24354 11222 case '!': /* MT usermode flag bit. */
df58fc94 11223 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11224 my_getExpression (&imm_expr, s);
11225 check_absolute_expr (ip, &imm_expr);
11226 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
a9e24354
TS
11227 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11228 (unsigned long) imm_expr.X_add_number);
df58fc94 11229 INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11230 imm_expr.X_op = O_absent;
11231 s = expr_end;
11232 continue;
11233
a9e24354 11234 case '$': /* MT load high flag bit. */
df58fc94 11235 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11236 my_getExpression (&imm_expr, s);
11237 check_absolute_expr (ip, &imm_expr);
11238 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
a9e24354
TS
11239 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11240 (unsigned long) imm_expr.X_add_number);
df58fc94 11241 INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11242 imm_expr.X_op = O_absent;
11243 s = expr_end;
11244 continue;
11245
90ecf173 11246 case '*': /* Four DSP accumulators in bits 18,19. */
df58fc94 11247 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11248 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11249 s[3] >= '0' && s[3] <= '3')
11250 {
11251 regno = s[3] - '0';
11252 s += 4;
df58fc94 11253 INSERT_OPERAND (0, MTACC_T, *ip, regno);
ef2e4d86
CF
11254 continue;
11255 }
11256 else
11257 as_bad (_("Invalid dsp/smartmips acc register"));
11258 break;
11259
90ecf173 11260 case '&': /* Four DSP accumulators in bits 13,14. */
df58fc94 11261 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11262 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11263 s[3] >= '0' && s[3] <= '3')
11264 {
11265 regno = s[3] - '0';
11266 s += 4;
df58fc94 11267 INSERT_OPERAND (0, MTACC_D, *ip, regno);
ef2e4d86
CF
11268 continue;
11269 }
11270 else
11271 as_bad (_("Invalid dsp/smartmips acc register"));
11272 break;
11273
dec0624d
MR
11274 case '\\': /* 3-bit bit position. */
11275 {
2906b037
MR
11276 unsigned long mask = (mips_opts.micromips
11277 ? MICROMIPSOP_MASK_3BITPOS
11278 : OP_MASK_3BITPOS);
dec0624d
MR
11279
11280 my_getExpression (&imm_expr, s);
11281 check_absolute_expr (ip, &imm_expr);
11282 if ((unsigned long) imm_expr.X_add_number > mask)
11283 as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11284 ip->insn_mo->name,
11285 mask, (unsigned long) imm_expr.X_add_number);
11286 INSERT_OPERAND (mips_opts.micromips,
11287 3BITPOS, *ip, imm_expr.X_add_number);
11288 imm_expr.X_op = O_absent;
11289 s = expr_end;
11290 }
11291 continue;
11292
252b5132 11293 case ',':
a339155f 11294 ++argnum;
252b5132
RH
11295 if (*s++ == *args)
11296 continue;
11297 s--;
11298 switch (*++args)
11299 {
11300 case 'r':
11301 case 'v':
df58fc94 11302 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132
RH
11303 continue;
11304
11305 case 'w':
df58fc94 11306 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
38487616
TS
11307 continue;
11308
252b5132 11309 case 'W':
df58fc94
RS
11310 gas_assert (!mips_opts.micromips);
11311 INSERT_OPERAND (0, FT, *ip, lastregno);
252b5132
RH
11312 continue;
11313
11314 case 'V':
df58fc94 11315 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132
RH
11316 continue;
11317 }
11318 break;
11319
11320 case '(':
11321 /* Handle optional base register.
11322 Either the base register is omitted or
bdaaa2e1 11323 we must have a left paren. */
252b5132
RH
11324 /* This is dependent on the next operand specifier
11325 is a base register specification. */
df58fc94
RS
11326 gas_assert (args[1] == 'b'
11327 || (mips_opts.micromips
11328 && args[1] == 'm'
11329 && (args[2] == 'l' || args[2] == 'n'
11330 || args[2] == 's' || args[2] == 'a')));
11331 if (*s == '\0' && args[1] == 'b')
252b5132 11332 return;
df58fc94 11333 /* Fall through. */
252b5132 11334
90ecf173 11335 case ')': /* These must match exactly. */
df58fc94
RS
11336 if (*s++ == *args)
11337 continue;
11338 break;
11339
11340 case '[': /* These must match exactly. */
60b63b72 11341 case ']':
df58fc94 11342 gas_assert (!mips_opts.micromips);
252b5132
RH
11343 if (*s++ == *args)
11344 continue;
11345 break;
11346
af7ee8bf
CD
11347 case '+': /* Opcode extension character. */
11348 switch (*++args)
11349 {
9bcd4f99
TS
11350 case '1': /* UDI immediates. */
11351 case '2':
11352 case '3':
11353 case '4':
df58fc94 11354 gas_assert (!mips_opts.micromips);
9bcd4f99
TS
11355 {
11356 const struct mips_immed *imm = mips_immed;
11357
11358 while (imm->type && imm->type != *args)
11359 ++imm;
11360 if (! imm->type)
b37df7c4 11361 abort ();
9bcd4f99
TS
11362 my_getExpression (&imm_expr, s);
11363 check_absolute_expr (ip, &imm_expr);
11364 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11365 {
11366 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11367 imm->desc ? imm->desc : ip->insn_mo->name,
11368 (unsigned long) imm_expr.X_add_number,
11369 (unsigned long) imm_expr.X_add_number);
90ecf173 11370 imm_expr.X_add_number &= imm->mask;
9bcd4f99
TS
11371 }
11372 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11373 << imm->shift);
11374 imm_expr.X_op = O_absent;
11375 s = expr_end;
11376 }
11377 continue;
90ecf173 11378
b015e599
AP
11379 case 'J': /* 10-bit hypcall code. */
11380 gas_assert (!mips_opts.micromips);
11381 {
11382 unsigned long mask = OP_MASK_CODE10;
11383
11384 my_getExpression (&imm_expr, s);
11385 check_absolute_expr (ip, &imm_expr);
11386 if ((unsigned long) imm_expr.X_add_number > mask)
11387 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11388 ip->insn_mo->name,
11389 mask, (unsigned long) imm_expr.X_add_number);
11390 INSERT_OPERAND (0, CODE10, *ip, imm_expr.X_add_number);
11391 imm_expr.X_op = O_absent;
11392 s = expr_end;
11393 }
11394 continue;
11395
071742cf
CD
11396 case 'A': /* ins/ext position, becomes LSB. */
11397 limlo = 0;
11398 limhi = 31;
5f74bc13
CD
11399 goto do_lsb;
11400 case 'E':
11401 limlo = 32;
11402 limhi = 63;
11403 goto do_lsb;
90ecf173 11404 do_lsb:
071742cf
CD
11405 my_getExpression (&imm_expr, s);
11406 check_absolute_expr (ip, &imm_expr);
11407 if ((unsigned long) imm_expr.X_add_number < limlo
11408 || (unsigned long) imm_expr.X_add_number > limhi)
11409 {
11410 as_bad (_("Improper position (%lu)"),
11411 (unsigned long) imm_expr.X_add_number);
11412 imm_expr.X_add_number = limlo;
11413 }
11414 lastpos = imm_expr.X_add_number;
df58fc94
RS
11415 INSERT_OPERAND (mips_opts.micromips,
11416 EXTLSB, *ip, imm_expr.X_add_number);
071742cf
CD
11417 imm_expr.X_op = O_absent;
11418 s = expr_end;
11419 continue;
11420
11421 case 'B': /* ins size, becomes MSB. */
11422 limlo = 1;
11423 limhi = 32;
5f74bc13
CD
11424 goto do_msb;
11425 case 'F':
11426 limlo = 33;
11427 limhi = 64;
11428 goto do_msb;
90ecf173 11429 do_msb:
071742cf
CD
11430 my_getExpression (&imm_expr, s);
11431 check_absolute_expr (ip, &imm_expr);
11432 /* Check for negative input so that small negative numbers
11433 will not succeed incorrectly. The checks against
11434 (pos+size) transitively check "size" itself,
11435 assuming that "pos" is reasonable. */
11436 if ((long) imm_expr.X_add_number < 0
11437 || ((unsigned long) imm_expr.X_add_number
11438 + lastpos) < limlo
11439 || ((unsigned long) imm_expr.X_add_number
11440 + lastpos) > limhi)
11441 {
11442 as_bad (_("Improper insert size (%lu, position %lu)"),
11443 (unsigned long) imm_expr.X_add_number,
11444 (unsigned long) lastpos);
11445 imm_expr.X_add_number = limlo - lastpos;
11446 }
df58fc94
RS
11447 INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11448 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
11449 imm_expr.X_op = O_absent;
11450 s = expr_end;
11451 continue;
11452
11453 case 'C': /* ext size, becomes MSBD. */
11454 limlo = 1;
11455 limhi = 32;
f02d8318 11456 sizelo = 1;
5f74bc13
CD
11457 goto do_msbd;
11458 case 'G':
11459 limlo = 33;
11460 limhi = 64;
f02d8318 11461 sizelo = 33;
5f74bc13
CD
11462 goto do_msbd;
11463 case 'H':
11464 limlo = 33;
11465 limhi = 64;
f02d8318 11466 sizelo = 1;
5f74bc13 11467 goto do_msbd;
90ecf173 11468 do_msbd:
071742cf
CD
11469 my_getExpression (&imm_expr, s);
11470 check_absolute_expr (ip, &imm_expr);
f02d8318
CF
11471 /* The checks against (pos+size) don't transitively check
11472 "size" itself, assuming that "pos" is reasonable.
11473 We also need to check the lower bound of "size". */
11474 if ((long) imm_expr.X_add_number < sizelo
071742cf
CD
11475 || ((unsigned long) imm_expr.X_add_number
11476 + lastpos) < limlo
11477 || ((unsigned long) imm_expr.X_add_number
11478 + lastpos) > limhi)
11479 {
11480 as_bad (_("Improper extract size (%lu, position %lu)"),
11481 (unsigned long) imm_expr.X_add_number,
11482 (unsigned long) lastpos);
11483 imm_expr.X_add_number = limlo - lastpos;
11484 }
df58fc94
RS
11485 INSERT_OPERAND (mips_opts.micromips,
11486 EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
11487 imm_expr.X_op = O_absent;
11488 s = expr_end;
11489 continue;
af7ee8bf 11490
bbcc0807
CD
11491 case 'D':
11492 /* +D is for disassembly only; never match. */
11493 break;
11494
5f74bc13
CD
11495 case 'I':
11496 /* "+I" is like "I", except that imm2_expr is used. */
11497 my_getExpression (&imm2_expr, s);
11498 if (imm2_expr.X_op != O_big
11499 && imm2_expr.X_op != O_constant)
11500 insn_error = _("absolute expression required");
9ee2a2d4
MR
11501 if (HAVE_32BIT_GPRS)
11502 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
11503 s = expr_end;
11504 continue;
11505
707bfff6 11506 case 'T': /* Coprocessor register. */
df58fc94 11507 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11508 /* +T is for disassembly only; never match. */
11509 break;
11510
707bfff6 11511 case 't': /* Coprocessor register number. */
df58fc94 11512 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11513 if (s[0] == '$' && ISDIGIT (s[1]))
11514 {
11515 ++s;
11516 regno = 0;
11517 do
11518 {
11519 regno *= 10;
11520 regno += *s - '0';
11521 ++s;
11522 }
11523 while (ISDIGIT (*s));
11524 if (regno > 31)
11525 as_bad (_("Invalid register number (%d)"), regno);
11526 else
11527 {
df58fc94 11528 INSERT_OPERAND (0, RT, *ip, regno);
ef2e4d86
CF
11529 continue;
11530 }
11531 }
11532 else
11533 as_bad (_("Invalid coprocessor 0 register number"));
11534 break;
11535
bb35fb24
NC
11536 case 'x':
11537 /* bbit[01] and bbit[01]32 bit index. Give error if index
11538 is not in the valid range. */
df58fc94 11539 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11540 my_getExpression (&imm_expr, s);
11541 check_absolute_expr (ip, &imm_expr);
11542 if ((unsigned) imm_expr.X_add_number > 31)
11543 {
11544 as_bad (_("Improper bit index (%lu)"),
11545 (unsigned long) imm_expr.X_add_number);
11546 imm_expr.X_add_number = 0;
11547 }
df58fc94 11548 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
bb35fb24
NC
11549 imm_expr.X_op = O_absent;
11550 s = expr_end;
11551 continue;
11552
11553 case 'X':
11554 /* bbit[01] bit index when bbit is used but we generate
11555 bbit[01]32 because the index is over 32. Move to the
11556 next candidate if index is not in the valid range. */
df58fc94 11557 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11558 my_getExpression (&imm_expr, s);
11559 check_absolute_expr (ip, &imm_expr);
11560 if ((unsigned) imm_expr.X_add_number < 32
11561 || (unsigned) imm_expr.X_add_number > 63)
11562 break;
df58fc94 11563 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
11564 imm_expr.X_op = O_absent;
11565 s = expr_end;
11566 continue;
11567
11568 case 'p':
11569 /* cins, cins32, exts and exts32 position field. Give error
11570 if it's not in the valid range. */
df58fc94 11571 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11572 my_getExpression (&imm_expr, s);
11573 check_absolute_expr (ip, &imm_expr);
11574 if ((unsigned) imm_expr.X_add_number > 31)
11575 {
11576 as_bad (_("Improper position (%lu)"),
11577 (unsigned long) imm_expr.X_add_number);
11578 imm_expr.X_add_number = 0;
11579 }
11580 /* Make the pos explicit to simplify +S. */
11581 lastpos = imm_expr.X_add_number + 32;
df58fc94 11582 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
bb35fb24
NC
11583 imm_expr.X_op = O_absent;
11584 s = expr_end;
11585 continue;
11586
11587 case 'P':
11588 /* cins, cins32, exts and exts32 position field. Move to
11589 the next candidate if it's not in the valid range. */
df58fc94 11590 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11591 my_getExpression (&imm_expr, s);
11592 check_absolute_expr (ip, &imm_expr);
11593 if ((unsigned) imm_expr.X_add_number < 32
11594 || (unsigned) imm_expr.X_add_number > 63)
11595 break;
11596 lastpos = imm_expr.X_add_number;
df58fc94 11597 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
11598 imm_expr.X_op = O_absent;
11599 s = expr_end;
11600 continue;
11601
11602 case 's':
11603 /* cins and exts length-minus-one field. */
df58fc94 11604 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11605 my_getExpression (&imm_expr, s);
11606 check_absolute_expr (ip, &imm_expr);
11607 if ((unsigned long) imm_expr.X_add_number > 31)
11608 {
11609 as_bad (_("Improper size (%lu)"),
11610 (unsigned long) imm_expr.X_add_number);
11611 imm_expr.X_add_number = 0;
11612 }
df58fc94 11613 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
11614 imm_expr.X_op = O_absent;
11615 s = expr_end;
11616 continue;
11617
11618 case 'S':
11619 /* cins32/exts32 and cins/exts aliasing cint32/exts32
11620 length-minus-one field. */
df58fc94 11621 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11622 my_getExpression (&imm_expr, s);
11623 check_absolute_expr (ip, &imm_expr);
11624 if ((long) imm_expr.X_add_number < 0
11625 || (unsigned long) imm_expr.X_add_number + lastpos > 63)
11626 {
11627 as_bad (_("Improper size (%lu)"),
11628 (unsigned long) imm_expr.X_add_number);
11629 imm_expr.X_add_number = 0;
11630 }
df58fc94 11631 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
11632 imm_expr.X_op = O_absent;
11633 s = expr_end;
11634 continue;
11635
dd3cbb7e
NC
11636 case 'Q':
11637 /* seqi/snei immediate field. */
df58fc94 11638 gas_assert (!mips_opts.micromips);
dd3cbb7e
NC
11639 my_getExpression (&imm_expr, s);
11640 check_absolute_expr (ip, &imm_expr);
11641 if ((long) imm_expr.X_add_number < -512
11642 || (long) imm_expr.X_add_number >= 512)
11643 {
11644 as_bad (_("Improper immediate (%ld)"),
11645 (long) imm_expr.X_add_number);
11646 imm_expr.X_add_number = 0;
11647 }
df58fc94 11648 INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
dd3cbb7e
NC
11649 imm_expr.X_op = O_absent;
11650 s = expr_end;
11651 continue;
11652
98675402 11653 case 'a': /* 8-bit signed offset in bit 6 */
df58fc94 11654 gas_assert (!mips_opts.micromips);
98675402
RS
11655 my_getExpression (&imm_expr, s);
11656 check_absolute_expr (ip, &imm_expr);
11657 min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
11658 max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
11659 if (imm_expr.X_add_number < min_range
11660 || imm_expr.X_add_number > max_range)
11661 {
c95354ed 11662 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11663 (long) min_range, (long) max_range,
11664 (long) imm_expr.X_add_number);
11665 }
df58fc94 11666 INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
98675402
RS
11667 imm_expr.X_op = O_absent;
11668 s = expr_end;
11669 continue;
11670
11671 case 'b': /* 8-bit signed offset in bit 3 */
df58fc94 11672 gas_assert (!mips_opts.micromips);
98675402
RS
11673 my_getExpression (&imm_expr, s);
11674 check_absolute_expr (ip, &imm_expr);
11675 min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
11676 max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
11677 if (imm_expr.X_add_number < min_range
11678 || imm_expr.X_add_number > max_range)
11679 {
c95354ed 11680 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11681 (long) min_range, (long) max_range,
11682 (long) imm_expr.X_add_number);
11683 }
df58fc94 11684 INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
98675402
RS
11685 imm_expr.X_op = O_absent;
11686 s = expr_end;
11687 continue;
11688
11689 case 'c': /* 9-bit signed offset in bit 6 */
df58fc94 11690 gas_assert (!mips_opts.micromips);
98675402
RS
11691 my_getExpression (&imm_expr, s);
11692 check_absolute_expr (ip, &imm_expr);
11693 min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
11694 max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
c95354ed
MX
11695 /* We check the offset range before adjusted. */
11696 min_range <<= 4;
11697 max_range <<= 4;
98675402
RS
11698 if (imm_expr.X_add_number < min_range
11699 || imm_expr.X_add_number > max_range)
11700 {
c95354ed 11701 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11702 (long) min_range, (long) max_range,
11703 (long) imm_expr.X_add_number);
11704 }
c95354ed
MX
11705 if (imm_expr.X_add_number & 0xf)
11706 {
11707 as_bad (_("Offset not 16 bytes alignment (%ld)"),
11708 (long) imm_expr.X_add_number);
11709 }
11710 /* Right shift 4 bits to adjust the offset operand. */
df58fc94
RS
11711 INSERT_OPERAND (0, OFFSET_C, *ip,
11712 imm_expr.X_add_number >> 4);
98675402
RS
11713 imm_expr.X_op = O_absent;
11714 s = expr_end;
11715 continue;
11716
11717 case 'z':
df58fc94 11718 gas_assert (!mips_opts.micromips);
98675402
RS
11719 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
11720 break;
11721 if (regno == AT && mips_opts.at)
11722 {
11723 if (mips_opts.at == ATREG)
11724 as_warn (_("used $at without \".set noat\""));
11725 else
11726 as_warn (_("used $%u with \".set at=$%u\""),
11727 regno, mips_opts.at);
11728 }
df58fc94 11729 INSERT_OPERAND (0, RZ, *ip, regno);
98675402
RS
11730 continue;
11731
11732 case 'Z':
df58fc94 11733 gas_assert (!mips_opts.micromips);
98675402
RS
11734 if (!reg_lookup (&s, RTYPE_FPU, &regno))
11735 break;
df58fc94 11736 INSERT_OPERAND (0, FZ, *ip, regno);
98675402
RS
11737 continue;
11738
af7ee8bf 11739 default:
df58fc94 11740 as_bad (_("Internal error: bad %s opcode "
90ecf173 11741 "(unknown extension operand type `+%c'): %s %s"),
df58fc94 11742 mips_opts.micromips ? "microMIPS" : "MIPS",
90ecf173 11743 *args, insn->name, insn->args);
af7ee8bf
CD
11744 /* Further processing is fruitless. */
11745 return;
11746 }
11747 break;
11748
df58fc94 11749 case '.': /* 10-bit offset. */
df58fc94 11750 gas_assert (mips_opts.micromips);
dec0624d 11751 case '~': /* 12-bit offset. */
df58fc94
RS
11752 {
11753 int shift = *args == '.' ? 9 : 11;
11754 size_t i;
11755
11756 /* Check whether there is only a single bracketed expression
11757 left. If so, it must be the base register and the
11758 constant must be zero. */
11759 if (*s == '(' && strchr (s + 1, '(') == 0)
11760 continue;
11761
11762 /* If this value won't fit into the offset, then go find
11763 a macro that will generate a 16- or 32-bit offset code
11764 pattern. */
11765 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11766 if ((i == 0 && (imm_expr.X_op != O_constant
11767 || imm_expr.X_add_number >= 1 << shift
11768 || imm_expr.X_add_number < -1 << shift))
11769 || i > 0)
11770 {
11771 imm_expr.X_op = O_absent;
11772 break;
11773 }
11774 if (shift == 9)
11775 INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
11776 else
dec0624d
MR
11777 INSERT_OPERAND (mips_opts.micromips,
11778 OFFSET12, *ip, imm_expr.X_add_number);
df58fc94
RS
11779 imm_expr.X_op = O_absent;
11780 s = expr_end;
11781 }
11782 continue;
11783
252b5132
RH
11784 case '<': /* must be at least one digit */
11785 /*
11786 * According to the manual, if the shift amount is greater
b6ff326e
KH
11787 * than 31 or less than 0, then the shift amount should be
11788 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
11789 * We issue a warning and mask out all but the low 5 bits.
11790 */
11791 my_getExpression (&imm_expr, s);
11792 check_absolute_expr (ip, &imm_expr);
11793 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
11794 as_warn (_("Improper shift amount (%lu)"),
11795 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11796 INSERT_OPERAND (mips_opts.micromips,
11797 SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
11798 imm_expr.X_op = O_absent;
11799 s = expr_end;
11800 continue;
11801
11802 case '>': /* shift amount minus 32 */
11803 my_getExpression (&imm_expr, s);
11804 check_absolute_expr (ip, &imm_expr);
11805 if ((unsigned long) imm_expr.X_add_number < 32
11806 || (unsigned long) imm_expr.X_add_number > 63)
11807 break;
df58fc94
RS
11808 INSERT_OPERAND (mips_opts.micromips,
11809 SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
11810 imm_expr.X_op = O_absent;
11811 s = expr_end;
11812 continue;
11813
90ecf173
MR
11814 case 'k': /* CACHE code. */
11815 case 'h': /* PREFX code. */
11816 case '1': /* SYNC type. */
252b5132
RH
11817 my_getExpression (&imm_expr, s);
11818 check_absolute_expr (ip, &imm_expr);
11819 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
11820 as_warn (_("Invalid value for `%s' (%lu)"),
11821 ip->insn_mo->name,
11822 (unsigned long) imm_expr.X_add_number);
df58fc94 11823 switch (*args)
d954098f 11824 {
df58fc94
RS
11825 case 'k':
11826 if (mips_fix_cn63xxp1
11827 && !mips_opts.micromips
11828 && strcmp ("pref", insn->name) == 0)
d954098f
DD
11829 switch (imm_expr.X_add_number)
11830 {
11831 case 5:
11832 case 25:
11833 case 26:
11834 case 27:
11835 case 28:
11836 case 29:
11837 case 30:
11838 case 31: /* These are ok. */
11839 break;
11840
11841 default: /* The rest must be changed to 28. */
11842 imm_expr.X_add_number = 28;
11843 break;
11844 }
df58fc94
RS
11845 INSERT_OPERAND (mips_opts.micromips,
11846 CACHE, *ip, imm_expr.X_add_number);
11847 break;
11848 case 'h':
11849 INSERT_OPERAND (mips_opts.micromips,
11850 PREFX, *ip, imm_expr.X_add_number);
11851 break;
11852 case '1':
11853 INSERT_OPERAND (mips_opts.micromips,
11854 STYPE, *ip, imm_expr.X_add_number);
11855 break;
d954098f 11856 }
252b5132
RH
11857 imm_expr.X_op = O_absent;
11858 s = expr_end;
11859 continue;
11860
90ecf173 11861 case 'c': /* BREAK code. */
df58fc94
RS
11862 {
11863 unsigned long mask = (mips_opts.micromips
11864 ? MICROMIPSOP_MASK_CODE
11865 : OP_MASK_CODE);
11866
11867 my_getExpression (&imm_expr, s);
11868 check_absolute_expr (ip, &imm_expr);
11869 if ((unsigned long) imm_expr.X_add_number > mask)
11870 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11871 ip->insn_mo->name,
11872 mask, (unsigned long) imm_expr.X_add_number);
11873 INSERT_OPERAND (mips_opts.micromips,
11874 CODE, *ip, imm_expr.X_add_number);
11875 imm_expr.X_op = O_absent;
11876 s = expr_end;
11877 }
252b5132
RH
11878 continue;
11879
90ecf173 11880 case 'q': /* Lower BREAK code. */
df58fc94
RS
11881 {
11882 unsigned long mask = (mips_opts.micromips
11883 ? MICROMIPSOP_MASK_CODE2
11884 : OP_MASK_CODE2);
11885
11886 my_getExpression (&imm_expr, s);
11887 check_absolute_expr (ip, &imm_expr);
11888 if ((unsigned long) imm_expr.X_add_number > mask)
11889 as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
11890 ip->insn_mo->name,
11891 mask, (unsigned long) imm_expr.X_add_number);
11892 INSERT_OPERAND (mips_opts.micromips,
11893 CODE2, *ip, imm_expr.X_add_number);
11894 imm_expr.X_op = O_absent;
11895 s = expr_end;
11896 }
252b5132
RH
11897 continue;
11898
df58fc94
RS
11899 case 'B': /* 20- or 10-bit syscall/break/wait code. */
11900 {
11901 unsigned long mask = (mips_opts.micromips
11902 ? MICROMIPSOP_MASK_CODE10
11903 : OP_MASK_CODE20);
11904
11905 my_getExpression (&imm_expr, s);
11906 check_absolute_expr (ip, &imm_expr);
11907 if ((unsigned long) imm_expr.X_add_number > mask)
11908 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11909 ip->insn_mo->name,
11910 mask, (unsigned long) imm_expr.X_add_number);
11911 if (mips_opts.micromips)
11912 INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
11913 else
11914 INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
11915 imm_expr.X_op = O_absent;
11916 s = expr_end;
11917 }
252b5132
RH
11918 continue;
11919
df58fc94
RS
11920 case 'C': /* 25- or 23-bit coprocessor code. */
11921 {
11922 unsigned long mask = (mips_opts.micromips
11923 ? MICROMIPSOP_MASK_COPZ
11924 : OP_MASK_COPZ);
11925
11926 my_getExpression (&imm_expr, s);
11927 check_absolute_expr (ip, &imm_expr);
11928 if ((unsigned long) imm_expr.X_add_number > mask)
11929 as_warn (_("Coproccesor code > %u bits (%lu)"),
11930 mips_opts.micromips ? 23U : 25U,
793b27f4 11931 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11932 INSERT_OPERAND (mips_opts.micromips,
11933 COPZ, *ip, imm_expr.X_add_number);
11934 imm_expr.X_op = O_absent;
11935 s = expr_end;
11936 }
beae10d5 11937 continue;
252b5132 11938
df58fc94
RS
11939 case 'J': /* 19-bit WAIT code. */
11940 gas_assert (!mips_opts.micromips);
4372b673
NC
11941 my_getExpression (&imm_expr, s);
11942 check_absolute_expr (ip, &imm_expr);
793b27f4 11943 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
df58fc94
RS
11944 {
11945 as_warn (_("Illegal 19-bit code (%lu)"),
a9e24354 11946 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11947 imm_expr.X_add_number &= OP_MASK_CODE19;
11948 }
11949 INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
11950 imm_expr.X_op = O_absent;
11951 s = expr_end;
11952 continue;
11953
707bfff6 11954 case 'P': /* Performance register. */
df58fc94 11955 gas_assert (!mips_opts.micromips);
beae10d5 11956 my_getExpression (&imm_expr, s);
252b5132 11957 check_absolute_expr (ip, &imm_expr);
beae10d5 11958 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
11959 as_warn (_("Invalid performance register (%lu)"),
11960 (unsigned long) imm_expr.X_add_number);
e407c74b
NC
11961 if (imm_expr.X_add_number != 0 && mips_opts.arch == CPU_R5900
11962 && (!strcmp(insn->name,"mfps") || !strcmp(insn->name,"mtps")))
11963 as_warn (_("Invalid performance register (%lu)"),
11964 (unsigned long) imm_expr.X_add_number);
df58fc94 11965 INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
11966 imm_expr.X_op = O_absent;
11967 s = expr_end;
11968 continue;
252b5132 11969
707bfff6 11970 case 'G': /* Coprocessor destination register. */
df58fc94
RS
11971 {
11972 unsigned long opcode = ip->insn_opcode;
11973 unsigned long mask;
11974 unsigned int types;
11975 int cop0;
11976
11977 if (mips_opts.micromips)
11978 {
11979 mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
11980 | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
11981 | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
11982 opcode &= mask;
11983 switch (opcode)
11984 {
11985 case 0x000000fc: /* mfc0 */
11986 case 0x000002fc: /* mtc0 */
11987 case 0x580000fc: /* dmfc0 */
11988 case 0x580002fc: /* dmtc0 */
11989 cop0 = 1;
11990 break;
11991 default:
11992 cop0 = 0;
11993 break;
11994 }
11995 }
11996 else
11997 {
11998 opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
11999 cop0 = opcode == OP_OP_COP0;
12000 }
12001 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
12002 ok = reg_lookup (&s, types, &regno);
12003 if (mips_opts.micromips)
12004 INSERT_OPERAND (1, RS, *ip, regno);
12005 else
12006 INSERT_OPERAND (0, RD, *ip, regno);
12007 if (ok)
12008 {
12009 lastregno = regno;
12010 continue;
12011 }
12012 }
12013 break;
707bfff6 12014
df58fc94
RS
12015 case 'y': /* ALNV.PS source register. */
12016 gas_assert (mips_opts.micromips);
12017 goto do_reg;
12018 case 'x': /* Ignore register name. */
12019 case 'U': /* Destination register (CLO/CLZ). */
12020 case 'g': /* Coprocessor destination register. */
12021 gas_assert (!mips_opts.micromips);
90ecf173
MR
12022 case 'b': /* Base register. */
12023 case 'd': /* Destination register. */
12024 case 's': /* Source register. */
12025 case 't': /* Target register. */
12026 case 'r': /* Both target and source. */
12027 case 'v': /* Both dest and source. */
12028 case 'w': /* Both dest and target. */
12029 case 'E': /* Coprocessor target register. */
12030 case 'K': /* RDHWR destination register. */
90ecf173 12031 case 'z': /* Must be zero register. */
df58fc94 12032 do_reg:
90ecf173 12033 s_reset = s;
707bfff6
TS
12034 if (*args == 'E' || *args == 'K')
12035 ok = reg_lookup (&s, RTYPE_NUM, &regno);
12036 else
12037 {
12038 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
741fe287
MR
12039 if (regno == AT && mips_opts.at)
12040 {
12041 if (mips_opts.at == ATREG)
f71d0d44 12042 as_warn (_("Used $at without \".set noat\""));
741fe287 12043 else
f71d0d44 12044 as_warn (_("Used $%u with \".set at=$%u\""),
741fe287
MR
12045 regno, mips_opts.at);
12046 }
707bfff6
TS
12047 }
12048 if (ok)
252b5132 12049 {
252b5132
RH
12050 c = *args;
12051 if (*s == ' ')
f9419b05 12052 ++s;
252b5132
RH
12053 if (args[1] != *s)
12054 {
12055 if (c == 'r' || c == 'v' || c == 'w')
12056 {
12057 regno = lastregno;
12058 s = s_reset;
f9419b05 12059 ++args;
252b5132
RH
12060 }
12061 }
12062 /* 'z' only matches $0. */
12063 if (c == 'z' && regno != 0)
12064 break;
12065
24864476 12066 if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
e7c604dd
CM
12067 {
12068 if (regno == lastregno)
90ecf173
MR
12069 {
12070 insn_error
f71d0d44 12071 = _("Source and destination must be different");
e7c604dd 12072 continue;
90ecf173 12073 }
24864476 12074 if (regno == 31 && lastregno == 0xffffffff)
90ecf173
MR
12075 {
12076 insn_error
f71d0d44 12077 = _("A destination register must be supplied");
e7c604dd 12078 continue;
90ecf173 12079 }
e7c604dd 12080 }
90ecf173
MR
12081 /* Now that we have assembled one operand, we use the args
12082 string to figure out where it goes in the instruction. */
252b5132
RH
12083 switch (c)
12084 {
12085 case 'r':
12086 case 's':
12087 case 'v':
12088 case 'b':
df58fc94 12089 INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
252b5132 12090 break;
df58fc94 12091
af7ee8bf 12092 case 'K':
df58fc94
RS
12093 if (mips_opts.micromips)
12094 INSERT_OPERAND (1, RS, *ip, regno);
12095 else
12096 INSERT_OPERAND (0, RD, *ip, regno);
12097 break;
12098
12099 case 'd':
ef2e4d86 12100 case 'g':
df58fc94 12101 INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
252b5132 12102 break;
df58fc94 12103
4372b673 12104 case 'U':
df58fc94
RS
12105 gas_assert (!mips_opts.micromips);
12106 INSERT_OPERAND (0, RD, *ip, regno);
12107 INSERT_OPERAND (0, RT, *ip, regno);
4372b673 12108 break;
df58fc94 12109
252b5132
RH
12110 case 'w':
12111 case 't':
12112 case 'E':
df58fc94
RS
12113 INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
12114 break;
12115
12116 case 'y':
12117 gas_assert (mips_opts.micromips);
12118 INSERT_OPERAND (1, RS3, *ip, regno);
252b5132 12119 break;
df58fc94 12120
252b5132
RH
12121 case 'x':
12122 /* This case exists because on the r3000 trunc
12123 expands into a macro which requires a gp
12124 register. On the r6000 or r4000 it is
12125 assembled into a single instruction which
12126 ignores the register. Thus the insn version
12127 is MIPS_ISA2 and uses 'x', and the macro
12128 version is MIPS_ISA1 and uses 't'. */
12129 break;
df58fc94 12130
252b5132
RH
12131 case 'z':
12132 /* This case is for the div instruction, which
12133 acts differently if the destination argument
12134 is $0. This only matches $0, and is checked
12135 outside the switch. */
12136 break;
252b5132
RH
12137 }
12138 lastregno = regno;
12139 continue;
12140 }
252b5132
RH
12141 switch (*args++)
12142 {
12143 case 'r':
12144 case 'v':
df58fc94 12145 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132 12146 continue;
df58fc94 12147
252b5132 12148 case 'w':
df58fc94 12149 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
252b5132
RH
12150 continue;
12151 }
12152 break;
12153
deec1734 12154 case 'O': /* MDMX alignment immediate constant. */
df58fc94 12155 gas_assert (!mips_opts.micromips);
deec1734
CD
12156 my_getExpression (&imm_expr, s);
12157 check_absolute_expr (ip, &imm_expr);
12158 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
20203fb9 12159 as_warn (_("Improper align amount (%ld), using low bits"),
bf12938e 12160 (long) imm_expr.X_add_number);
df58fc94 12161 INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
deec1734
CD
12162 imm_expr.X_op = O_absent;
12163 s = expr_end;
12164 continue;
12165
12166 case 'Q': /* MDMX vector, element sel, or const. */
12167 if (s[0] != '$')
12168 {
12169 /* MDMX Immediate. */
df58fc94 12170 gas_assert (!mips_opts.micromips);
deec1734
CD
12171 my_getExpression (&imm_expr, s);
12172 check_absolute_expr (ip, &imm_expr);
12173 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
12174 as_warn (_("Invalid MDMX Immediate (%ld)"),
12175 (long) imm_expr.X_add_number);
df58fc94 12176 INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
deec1734
CD
12177 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12178 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12179 else
12180 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
12181 imm_expr.X_op = O_absent;
12182 s = expr_end;
12183 continue;
12184 }
12185 /* Not MDMX Immediate. Fall through. */
12186 case 'X': /* MDMX destination register. */
12187 case 'Y': /* MDMX source register. */
12188 case 'Z': /* MDMX target register. */
12189 is_mdmx = 1;
df58fc94
RS
12190 case 'W':
12191 gas_assert (!mips_opts.micromips);
90ecf173
MR
12192 case 'D': /* Floating point destination register. */
12193 case 'S': /* Floating point source register. */
12194 case 'T': /* Floating point target register. */
12195 case 'R': /* Floating point source register. */
252b5132 12196 case 'V':
707bfff6
TS
12197 rtype = RTYPE_FPU;
12198 if (is_mdmx
12199 || (mips_opts.ase_mdmx
12200 && (ip->insn_mo->pinfo & FP_D)
12201 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12202 | INSN_COPROC_MEMORY_DELAY
12203 | INSN_LOAD_COPROC_DELAY
12204 | INSN_LOAD_MEMORY_DELAY
12205 | INSN_STORE_MEMORY))))
12206 rtype |= RTYPE_VEC;
252b5132 12207 s_reset = s;
707bfff6 12208 if (reg_lookup (&s, rtype, &regno))
252b5132 12209 {
252b5132 12210 if ((regno & 1) != 0
ca4e0257 12211 && HAVE_32BIT_FPRS
90ecf173 12212 && !mips_oddfpreg_ok (ip->insn_mo, argnum))
252b5132
RH
12213 as_warn (_("Float register should be even, was %d"),
12214 regno);
12215
12216 c = *args;
12217 if (*s == ' ')
f9419b05 12218 ++s;
252b5132
RH
12219 if (args[1] != *s)
12220 {
12221 if (c == 'V' || c == 'W')
12222 {
12223 regno = lastregno;
12224 s = s_reset;
f9419b05 12225 ++args;
252b5132
RH
12226 }
12227 }
12228 switch (c)
12229 {
12230 case 'D':
deec1734 12231 case 'X':
df58fc94 12232 INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
252b5132 12233 break;
df58fc94 12234
252b5132
RH
12235 case 'V':
12236 case 'S':
deec1734 12237 case 'Y':
df58fc94 12238 INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
252b5132 12239 break;
df58fc94 12240
deec1734
CD
12241 case 'Q':
12242 /* This is like 'Z', but also needs to fix the MDMX
12243 vector/scalar select bits. Note that the
12244 scalar immediate case is handled above. */
12245 if (*s == '[')
12246 {
12247 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12248 int max_el = (is_qh ? 3 : 7);
12249 s++;
12250 my_getExpression(&imm_expr, s);
12251 check_absolute_expr (ip, &imm_expr);
12252 s = expr_end;
12253 if (imm_expr.X_add_number > max_el)
20203fb9
NC
12254 as_bad (_("Bad element selector %ld"),
12255 (long) imm_expr.X_add_number);
deec1734
CD
12256 imm_expr.X_add_number &= max_el;
12257 ip->insn_opcode |= (imm_expr.X_add_number
12258 << (OP_SH_VSEL +
12259 (is_qh ? 2 : 1)));
01a3f561 12260 imm_expr.X_op = O_absent;
deec1734 12261 if (*s != ']')
20203fb9 12262 as_warn (_("Expecting ']' found '%s'"), s);
deec1734
CD
12263 else
12264 s++;
12265 }
12266 else
12267 {
12268 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12269 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12270 << OP_SH_VSEL);
12271 else
12272 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12273 OP_SH_VSEL);
12274 }
90ecf173 12275 /* Fall through. */
252b5132
RH
12276 case 'W':
12277 case 'T':
deec1734 12278 case 'Z':
df58fc94 12279 INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
252b5132 12280 break;
df58fc94 12281
252b5132 12282 case 'R':
df58fc94 12283 INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
252b5132
RH
12284 break;
12285 }
12286 lastregno = regno;
12287 continue;
12288 }
12289
252b5132
RH
12290 switch (*args++)
12291 {
12292 case 'V':
df58fc94 12293 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132 12294 continue;
df58fc94 12295
252b5132 12296 case 'W':
df58fc94 12297 INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
252b5132
RH
12298 continue;
12299 }
12300 break;
12301
12302 case 'I':
12303 my_getExpression (&imm_expr, s);
12304 if (imm_expr.X_op != O_big
12305 && imm_expr.X_op != O_constant)
12306 insn_error = _("absolute expression required");
9ee2a2d4
MR
12307 if (HAVE_32BIT_GPRS)
12308 normalize_constant_expr (&imm_expr);
252b5132
RH
12309 s = expr_end;
12310 continue;
12311
12312 case 'A':
12313 my_getExpression (&offset_expr, s);
2051e8c4 12314 normalize_address_expr (&offset_expr);
f6688943 12315 *imm_reloc = BFD_RELOC_32;
252b5132
RH
12316 s = expr_end;
12317 continue;
12318
12319 case 'F':
12320 case 'L':
12321 case 'f':
12322 case 'l':
12323 {
12324 int f64;
ca4e0257 12325 int using_gprs;
252b5132
RH
12326 char *save_in;
12327 char *err;
12328 unsigned char temp[8];
12329 int len;
12330 unsigned int length;
12331 segT seg;
12332 subsegT subseg;
12333 char *p;
12334
12335 /* These only appear as the last operand in an
12336 instruction, and every instruction that accepts
12337 them in any variant accepts them in all variants.
12338 This means we don't have to worry about backing out
12339 any changes if the instruction does not match.
12340
12341 The difference between them is the size of the
12342 floating point constant and where it goes. For 'F'
12343 and 'L' the constant is 64 bits; for 'f' and 'l' it
12344 is 32 bits. Where the constant is placed is based
12345 on how the MIPS assembler does things:
12346 F -- .rdata
12347 L -- .lit8
12348 f -- immediate value
12349 l -- .lit4
12350
12351 The .lit4 and .lit8 sections are only used if
12352 permitted by the -G argument.
12353
ca4e0257
RS
12354 The code below needs to know whether the target register
12355 is 32 or 64 bits wide. It relies on the fact 'f' and
12356 'F' are used with GPR-based instructions and 'l' and
12357 'L' are used with FPR-based instructions. */
252b5132
RH
12358
12359 f64 = *args == 'F' || *args == 'L';
ca4e0257 12360 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
12361
12362 save_in = input_line_pointer;
12363 input_line_pointer = s;
12364 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12365 length = len;
12366 s = input_line_pointer;
12367 input_line_pointer = save_in;
12368 if (err != NULL && *err != '\0')
12369 {
12370 as_bad (_("Bad floating point constant: %s"), err);
12371 memset (temp, '\0', sizeof temp);
12372 length = f64 ? 8 : 4;
12373 }
12374
9c2799c2 12375 gas_assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
12376
12377 if (*args == 'f'
12378 || (*args == 'l'
3e722fb5 12379 && (g_switch_value < 4
252b5132
RH
12380 || (temp[0] == 0 && temp[1] == 0)
12381 || (temp[2] == 0 && temp[3] == 0))))
12382 {
12383 imm_expr.X_op = O_constant;
90ecf173 12384 if (!target_big_endian)
252b5132
RH
12385 imm_expr.X_add_number = bfd_getl32 (temp);
12386 else
12387 imm_expr.X_add_number = bfd_getb32 (temp);
12388 }
12389 else if (length > 4
90ecf173 12390 && !mips_disable_float_construction
ca4e0257
RS
12391 /* Constants can only be constructed in GPRs and
12392 copied to FPRs if the GPRs are at least as wide
12393 as the FPRs. Force the constant into memory if
12394 we are using 64-bit FPRs but the GPRs are only
12395 32 bits wide. */
12396 && (using_gprs
90ecf173 12397 || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
12398 && ((temp[0] == 0 && temp[1] == 0)
12399 || (temp[2] == 0 && temp[3] == 0))
12400 && ((temp[4] == 0 && temp[5] == 0)
12401 || (temp[6] == 0 && temp[7] == 0)))
12402 {
ca4e0257 12403 /* The value is simple enough to load with a couple of
90ecf173
MR
12404 instructions. If using 32-bit registers, set
12405 imm_expr to the high order 32 bits and offset_expr to
12406 the low order 32 bits. Otherwise, set imm_expr to
12407 the entire 64 bit constant. */
ca4e0257 12408 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
12409 {
12410 imm_expr.X_op = O_constant;
12411 offset_expr.X_op = O_constant;
90ecf173 12412 if (!target_big_endian)
252b5132
RH
12413 {
12414 imm_expr.X_add_number = bfd_getl32 (temp + 4);
12415 offset_expr.X_add_number = bfd_getl32 (temp);
12416 }
12417 else
12418 {
12419 imm_expr.X_add_number = bfd_getb32 (temp);
12420 offset_expr.X_add_number = bfd_getb32 (temp + 4);
12421 }
12422 if (offset_expr.X_add_number == 0)
12423 offset_expr.X_op = O_absent;
12424 }
12425 else if (sizeof (imm_expr.X_add_number) > 4)
12426 {
12427 imm_expr.X_op = O_constant;
90ecf173 12428 if (!target_big_endian)
252b5132
RH
12429 imm_expr.X_add_number = bfd_getl64 (temp);
12430 else
12431 imm_expr.X_add_number = bfd_getb64 (temp);
12432 }
12433 else
12434 {
12435 imm_expr.X_op = O_big;
12436 imm_expr.X_add_number = 4;
90ecf173 12437 if (!target_big_endian)
252b5132
RH
12438 {
12439 generic_bignum[0] = bfd_getl16 (temp);
12440 generic_bignum[1] = bfd_getl16 (temp + 2);
12441 generic_bignum[2] = bfd_getl16 (temp + 4);
12442 generic_bignum[3] = bfd_getl16 (temp + 6);
12443 }
12444 else
12445 {
12446 generic_bignum[0] = bfd_getb16 (temp + 6);
12447 generic_bignum[1] = bfd_getb16 (temp + 4);
12448 generic_bignum[2] = bfd_getb16 (temp + 2);
12449 generic_bignum[3] = bfd_getb16 (temp);
12450 }
12451 }
12452 }
12453 else
12454 {
12455 const char *newname;
12456 segT new_seg;
12457
12458 /* Switch to the right section. */
12459 seg = now_seg;
12460 subseg = now_subseg;
12461 switch (*args)
12462 {
12463 default: /* unused default case avoids warnings. */
12464 case 'L':
12465 newname = RDATA_SECTION_NAME;
3e722fb5 12466 if (g_switch_value >= 8)
252b5132
RH
12467 newname = ".lit8";
12468 break;
12469 case 'F':
3e722fb5 12470 newname = RDATA_SECTION_NAME;
252b5132
RH
12471 break;
12472 case 'l':
9c2799c2 12473 gas_assert (g_switch_value >= 4);
252b5132
RH
12474 newname = ".lit4";
12475 break;
12476 }
12477 new_seg = subseg_new (newname, (subsegT) 0);
f43abd2b 12478 if (IS_ELF)
252b5132
RH
12479 bfd_set_section_flags (stdoutput, new_seg,
12480 (SEC_ALLOC
12481 | SEC_LOAD
12482 | SEC_READONLY
12483 | SEC_DATA));
12484 frag_align (*args == 'l' ? 2 : 3, 0, 0);
c41e87e3 12485 if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
12486 record_alignment (new_seg, 4);
12487 else
12488 record_alignment (new_seg, *args == 'l' ? 2 : 3);
12489 if (seg == now_seg)
12490 as_bad (_("Can't use floating point insn in this section"));
12491
df58fc94
RS
12492 /* Set the argument to the current address in the
12493 section. */
12494 offset_expr.X_op = O_symbol;
12495 offset_expr.X_add_symbol = symbol_temp_new_now ();
12496 offset_expr.X_add_number = 0;
12497
12498 /* Put the floating point number into the section. */
12499 p = frag_more ((int) length);
12500 memcpy (p, temp, length);
12501
12502 /* Switch back to the original section. */
12503 subseg_set (seg, subseg);
12504 }
12505 }
12506 continue;
12507
12508 case 'i': /* 16-bit unsigned immediate. */
12509 case 'j': /* 16-bit signed immediate. */
12510 *imm_reloc = BFD_RELOC_LO16;
12511 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12512 {
12513 int more;
12514 offsetT minval, maxval;
12515
12516 more = (insn + 1 < past
12517 && strcmp (insn->name, insn[1].name) == 0);
12518
12519 /* If the expression was written as an unsigned number,
12520 only treat it as signed if there are no more
12521 alternatives. */
12522 if (more
12523 && *args == 'j'
12524 && sizeof (imm_expr.X_add_number) <= 4
12525 && imm_expr.X_op == O_constant
12526 && imm_expr.X_add_number < 0
12527 && imm_expr.X_unsigned
12528 && HAVE_64BIT_GPRS)
12529 break;
12530
12531 /* For compatibility with older assemblers, we accept
12532 0x8000-0xffff as signed 16-bit numbers when only
12533 signed numbers are allowed. */
12534 if (*args == 'i')
12535 minval = 0, maxval = 0xffff;
12536 else if (more)
12537 minval = -0x8000, maxval = 0x7fff;
12538 else
12539 minval = -0x8000, maxval = 0xffff;
12540
12541 if (imm_expr.X_op != O_constant
12542 || imm_expr.X_add_number < minval
12543 || imm_expr.X_add_number > maxval)
12544 {
12545 if (more)
12546 break;
12547 if (imm_expr.X_op == O_constant
12548 || imm_expr.X_op == O_big)
12549 as_bad (_("Expression out of range"));
12550 }
12551 }
12552 s = expr_end;
12553 continue;
12554
12555 case 'o': /* 16-bit offset. */
12556 offset_reloc[0] = BFD_RELOC_LO16;
12557 offset_reloc[1] = BFD_RELOC_UNUSED;
12558 offset_reloc[2] = BFD_RELOC_UNUSED;
12559
12560 /* Check whether there is only a single bracketed expression
12561 left. If so, it must be the base register and the
12562 constant must be zero. */
12563 if (*s == '(' && strchr (s + 1, '(') == 0)
12564 {
12565 offset_expr.X_op = O_constant;
12566 offset_expr.X_add_number = 0;
12567 continue;
12568 }
12569
12570 /* If this value won't fit into a 16 bit offset, then go
12571 find a macro that will generate the 32 bit offset
12572 code pattern. */
12573 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
12574 && (offset_expr.X_op != O_constant
12575 || offset_expr.X_add_number >= 0x8000
12576 || offset_expr.X_add_number < -0x8000))
12577 break;
12578
12579 s = expr_end;
12580 continue;
12581
12582 case 'p': /* PC-relative offset. */
12583 *offset_reloc = BFD_RELOC_16_PCREL_S2;
12584 my_getExpression (&offset_expr, s);
12585 s = expr_end;
12586 continue;
12587
12588 case 'u': /* Upper 16 bits. */
5821951c 12589 *imm_reloc = BFD_RELOC_LO16;
df58fc94
RS
12590 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
12591 && imm_expr.X_op == O_constant
12592 && (imm_expr.X_add_number < 0
12593 || imm_expr.X_add_number >= 0x10000))
12594 as_bad (_("lui expression (%lu) not in range 0..65535"),
12595 (unsigned long) imm_expr.X_add_number);
12596 s = expr_end;
12597 continue;
12598
12599 case 'a': /* 26-bit address. */
12600 *offset_reloc = BFD_RELOC_MIPS_JMP;
12601 my_getExpression (&offset_expr, s);
12602 s = expr_end;
12603 continue;
12604
12605 case 'N': /* 3-bit branch condition code. */
12606 case 'M': /* 3-bit compare condition code. */
12607 rtype = RTYPE_CCC;
12608 if (ip->insn_mo->pinfo & (FP_D | FP_S))
12609 rtype |= RTYPE_FCC;
12610 if (!reg_lookup (&s, rtype, &regno))
12611 break;
12612 if ((strcmp (str + strlen (str) - 3, ".ps") == 0
12613 || strcmp (str + strlen (str) - 5, "any2f") == 0
12614 || strcmp (str + strlen (str) - 5, "any2t") == 0)
12615 && (regno & 1) != 0)
12616 as_warn (_("Condition code register should be even for %s, "
12617 "was %d"),
12618 str, regno);
12619 if ((strcmp (str + strlen (str) - 5, "any4f") == 0
12620 || strcmp (str + strlen (str) - 5, "any4t") == 0)
12621 && (regno & 3) != 0)
12622 as_warn (_("Condition code register should be 0 or 4 for %s, "
12623 "was %d"),
12624 str, regno);
12625 if (*args == 'N')
12626 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
12627 else
12628 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
12629 continue;
12630
12631 case 'H':
12632 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
12633 s += 2;
12634 if (ISDIGIT (*s))
12635 {
12636 c = 0;
12637 do
12638 {
12639 c *= 10;
12640 c += *s - '0';
12641 ++s;
12642 }
12643 while (ISDIGIT (*s));
12644 }
12645 else
12646 c = 8; /* Invalid sel value. */
12647
12648 if (c > 7)
12649 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
12650 INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
12651 continue;
12652
12653 case 'e':
12654 gas_assert (!mips_opts.micromips);
12655 /* Must be at least one digit. */
12656 my_getExpression (&imm_expr, s);
12657 check_absolute_expr (ip, &imm_expr);
12658
12659 if ((unsigned long) imm_expr.X_add_number
12660 > (unsigned long) OP_MASK_VECBYTE)
12661 {
12662 as_bad (_("bad byte vector index (%ld)"),
12663 (long) imm_expr.X_add_number);
12664 imm_expr.X_add_number = 0;
12665 }
12666
12667 INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
12668 imm_expr.X_op = O_absent;
12669 s = expr_end;
12670 continue;
12671
12672 case '%':
12673 gas_assert (!mips_opts.micromips);
12674 my_getExpression (&imm_expr, s);
12675 check_absolute_expr (ip, &imm_expr);
12676
12677 if ((unsigned long) imm_expr.X_add_number
12678 > (unsigned long) OP_MASK_VECALIGN)
12679 {
12680 as_bad (_("bad byte vector index (%ld)"),
12681 (long) imm_expr.X_add_number);
12682 imm_expr.X_add_number = 0;
12683 }
12684
12685 INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
12686 imm_expr.X_op = O_absent;
12687 s = expr_end;
12688 continue;
12689
12690 case 'm': /* Opcode extension character. */
12691 gas_assert (mips_opts.micromips);
12692 c = *++args;
12693 switch (c)
12694 {
12695 case 'r':
12696 if (strncmp (s, "$pc", 3) == 0)
12697 {
12698 s += 3;
12699 continue;
12700 }
12701 break;
12702
12703 case 'a':
12704 case 'b':
12705 case 'c':
12706 case 'd':
12707 case 'e':
12708 case 'f':
12709 case 'g':
12710 case 'h':
12711 case 'i':
12712 case 'j':
12713 case 'l':
12714 case 'm':
12715 case 'n':
12716 case 'p':
12717 case 'q':
12718 case 's':
12719 case 't':
12720 case 'x':
12721 case 'y':
12722 case 'z':
12723 s_reset = s;
12724 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12725 if (regno == AT && mips_opts.at)
12726 {
12727 if (mips_opts.at == ATREG)
12728 as_warn (_("Used $at without \".set noat\""));
12729 else
12730 as_warn (_("Used $%u with \".set at=$%u\""),
12731 regno, mips_opts.at);
12732 }
12733 if (!ok)
12734 {
12735 if (c == 'c')
12736 {
12737 gas_assert (args[1] == ',');
12738 regno = lastregno;
12739 ++args;
12740 }
12741 else if (c == 't')
12742 {
12743 gas_assert (args[1] == ',');
12744 ++args;
12745 continue; /* Nothing to do. */
12746 }
12747 else
12748 break;
12749 }
12750
12751 if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
12752 {
12753 if (regno == lastregno)
12754 {
12755 insn_error
12756 = _("Source and destination must be different");
12757 continue;
12758 }
12759 if (regno == 31 && lastregno == 0xffffffff)
12760 {
12761 insn_error
12762 = _("A destination register must be supplied");
12763 continue;
12764 }
12765 }
12766
12767 if (*s == ' ')
12768 ++s;
12769 if (args[1] != *s)
12770 {
12771 if (c == 'e')
12772 {
12773 gas_assert (args[1] == ',');
12774 regno = lastregno;
12775 s = s_reset;
12776 ++args;
12777 }
12778 else if (c == 't')
12779 {
12780 gas_assert (args[1] == ',');
12781 s = s_reset;
12782 ++args;
12783 continue; /* Nothing to do. */
12784 }
12785 }
12786
12787 /* Make sure regno is the same as lastregno. */
12788 if (c == 't' && regno != lastregno)
12789 break;
12790
12791 /* Make sure regno is the same as destregno. */
12792 if (c == 'x' && regno != destregno)
12793 break;
12794
12795 /* We need to save regno, before regno maps to the
12796 microMIPS register encoding. */
12797 lastregno = regno;
12798
12799 if (c == 'f')
12800 destregno = regno;
12801
12802 switch (c)
12803 {
12804 case 'a':
12805 if (regno != GP)
12806 regno = ILLEGAL_REG;
12807 break;
12808
12809 case 'b':
12810 regno = mips32_to_micromips_reg_b_map[regno];
12811 break;
12812
12813 case 'c':
12814 regno = mips32_to_micromips_reg_c_map[regno];
12815 break;
12816
12817 case 'd':
12818 regno = mips32_to_micromips_reg_d_map[regno];
12819 break;
12820
12821 case 'e':
12822 regno = mips32_to_micromips_reg_e_map[regno];
12823 break;
12824
12825 case 'f':
12826 regno = mips32_to_micromips_reg_f_map[regno];
12827 break;
12828
12829 case 'g':
12830 regno = mips32_to_micromips_reg_g_map[regno];
12831 break;
12832
12833 case 'h':
12834 regno = mips32_to_micromips_reg_h_map[regno];
12835 break;
12836
12837 case 'i':
12838 switch (EXTRACT_OPERAND (1, MI, *ip))
12839 {
12840 case 4:
12841 if (regno == 21)
12842 regno = 3;
12843 else if (regno == 22)
12844 regno = 4;
12845 else if (regno == 5)
12846 regno = 5;
12847 else if (regno == 6)
12848 regno = 6;
12849 else if (regno == 7)
12850 regno = 7;
12851 else
12852 regno = ILLEGAL_REG;
12853 break;
12854
12855 case 5:
12856 if (regno == 6)
12857 regno = 0;
12858 else if (regno == 7)
12859 regno = 1;
12860 else
12861 regno = ILLEGAL_REG;
12862 break;
12863
12864 case 6:
12865 if (regno == 7)
12866 regno = 2;
12867 else
12868 regno = ILLEGAL_REG;
12869 break;
12870
12871 default:
12872 regno = ILLEGAL_REG;
12873 break;
12874 }
12875 break;
12876
12877 case 'l':
12878 regno = mips32_to_micromips_reg_l_map[regno];
12879 break;
12880
12881 case 'm':
12882 regno = mips32_to_micromips_reg_m_map[regno];
12883 break;
12884
12885 case 'n':
12886 regno = mips32_to_micromips_reg_n_map[regno];
12887 break;
12888
12889 case 'q':
12890 regno = mips32_to_micromips_reg_q_map[regno];
12891 break;
12892
12893 case 's':
12894 if (regno != SP)
12895 regno = ILLEGAL_REG;
12896 break;
12897
12898 case 'y':
12899 if (regno != 31)
12900 regno = ILLEGAL_REG;
12901 break;
12902
12903 case 'z':
12904 if (regno != ZERO)
12905 regno = ILLEGAL_REG;
12906 break;
12907
12908 case 'j': /* Do nothing. */
12909 case 'p':
12910 case 't':
12911 case 'x':
12912 break;
12913
12914 default:
b37df7c4 12915 abort ();
df58fc94
RS
12916 }
12917
12918 if (regno == ILLEGAL_REG)
12919 break;
12920
12921 switch (c)
12922 {
12923 case 'b':
12924 INSERT_OPERAND (1, MB, *ip, regno);
12925 break;
12926
12927 case 'c':
12928 INSERT_OPERAND (1, MC, *ip, regno);
12929 break;
12930
12931 case 'd':
12932 INSERT_OPERAND (1, MD, *ip, regno);
12933 break;
12934
12935 case 'e':
12936 INSERT_OPERAND (1, ME, *ip, regno);
12937 break;
12938
12939 case 'f':
12940 INSERT_OPERAND (1, MF, *ip, regno);
12941 break;
12942
12943 case 'g':
12944 INSERT_OPERAND (1, MG, *ip, regno);
12945 break;
12946
12947 case 'h':
12948 INSERT_OPERAND (1, MH, *ip, regno);
12949 break;
12950
12951 case 'i':
12952 INSERT_OPERAND (1, MI, *ip, regno);
12953 break;
12954
12955 case 'j':
12956 INSERT_OPERAND (1, MJ, *ip, regno);
12957 break;
12958
12959 case 'l':
12960 INSERT_OPERAND (1, ML, *ip, regno);
12961 break;
12962
12963 case 'm':
12964 INSERT_OPERAND (1, MM, *ip, regno);
12965 break;
12966
12967 case 'n':
12968 INSERT_OPERAND (1, MN, *ip, regno);
12969 break;
12970
12971 case 'p':
12972 INSERT_OPERAND (1, MP, *ip, regno);
12973 break;
12974
12975 case 'q':
12976 INSERT_OPERAND (1, MQ, *ip, regno);
12977 break;
12978
12979 case 'a': /* Do nothing. */
12980 case 's': /* Do nothing. */
12981 case 't': /* Do nothing. */
12982 case 'x': /* Do nothing. */
12983 case 'y': /* Do nothing. */
12984 case 'z': /* Do nothing. */
12985 break;
12986
12987 default:
b37df7c4 12988 abort ();
df58fc94
RS
12989 }
12990 continue;
12991
12992 case 'A':
12993 {
12994 bfd_reloc_code_real_type r[3];
12995 expressionS ep;
12996 int imm;
12997
12998 /* Check whether there is only a single bracketed
12999 expression left. If so, it must be the base register
13000 and the constant must be zero. */
13001 if (*s == '(' && strchr (s + 1, '(') == 0)
13002 {
13003 INSERT_OPERAND (1, IMMA, *ip, 0);
13004 continue;
13005 }
13006
13007 if (my_getSmallExpression (&ep, r, s) > 0
13008 || !expr_const_in_range (&ep, -64, 64, 2))
13009 break;
13010
13011 imm = ep.X_add_number >> 2;
13012 INSERT_OPERAND (1, IMMA, *ip, imm);
13013 }
13014 s = expr_end;
13015 continue;
13016
13017 case 'B':
13018 {
13019 bfd_reloc_code_real_type r[3];
13020 expressionS ep;
13021 int imm;
13022
13023 if (my_getSmallExpression (&ep, r, s) > 0
13024 || ep.X_op != O_constant)
13025 break;
13026
13027 for (imm = 0; imm < 8; imm++)
13028 if (micromips_imm_b_map[imm] == ep.X_add_number)
13029 break;
13030 if (imm >= 8)
13031 break;
13032
13033 INSERT_OPERAND (1, IMMB, *ip, imm);
13034 }
13035 s = expr_end;
13036 continue;
13037
13038 case 'C':
13039 {
13040 bfd_reloc_code_real_type r[3];
13041 expressionS ep;
13042 int imm;
13043
13044 if (my_getSmallExpression (&ep, r, s) > 0
13045 || ep.X_op != O_constant)
13046 break;
13047
13048 for (imm = 0; imm < 16; imm++)
13049 if (micromips_imm_c_map[imm] == ep.X_add_number)
13050 break;
13051 if (imm >= 16)
13052 break;
13053
13054 INSERT_OPERAND (1, IMMC, *ip, imm);
13055 }
13056 s = expr_end;
13057 continue;
13058
13059 case 'D': /* pc relative offset */
13060 case 'E': /* pc relative offset */
13061 my_getExpression (&offset_expr, s);
13062 if (offset_expr.X_op == O_register)
13063 break;
13064
40209cad
MR
13065 if (!forced_insn_length)
13066 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
13067 else if (c == 'D')
13068 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
13069 else
13070 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
df58fc94
RS
13071 s = expr_end;
13072 continue;
13073
13074 case 'F':
13075 {
13076 bfd_reloc_code_real_type r[3];
13077 expressionS ep;
13078 int imm;
13079
13080 if (my_getSmallExpression (&ep, r, s) > 0
13081 || !expr_const_in_range (&ep, 0, 16, 0))
13082 break;
13083
13084 imm = ep.X_add_number;
13085 INSERT_OPERAND (1, IMMF, *ip, imm);
13086 }
13087 s = expr_end;
13088 continue;
13089
13090 case 'G':
13091 {
13092 bfd_reloc_code_real_type r[3];
13093 expressionS ep;
13094 int imm;
13095
13096 /* Check whether there is only a single bracketed
13097 expression left. If so, it must be the base register
13098 and the constant must be zero. */
13099 if (*s == '(' && strchr (s + 1, '(') == 0)
13100 {
13101 INSERT_OPERAND (1, IMMG, *ip, 0);
13102 continue;
13103 }
13104
13105 if (my_getSmallExpression (&ep, r, s) > 0
13106 || !expr_const_in_range (&ep, -1, 15, 0))
13107 break;
13108
13109 imm = ep.X_add_number & 15;
13110 INSERT_OPERAND (1, IMMG, *ip, imm);
13111 }
13112 s = expr_end;
13113 continue;
13114
13115 case 'H':
13116 {
13117 bfd_reloc_code_real_type r[3];
13118 expressionS ep;
13119 int imm;
13120
13121 /* Check whether there is only a single bracketed
13122 expression left. If so, it must be the base register
13123 and the constant must be zero. */
13124 if (*s == '(' && strchr (s + 1, '(') == 0)
13125 {
13126 INSERT_OPERAND (1, IMMH, *ip, 0);
13127 continue;
13128 }
13129
13130 if (my_getSmallExpression (&ep, r, s) > 0
13131 || !expr_const_in_range (&ep, 0, 16, 1))
13132 break;
13133
13134 imm = ep.X_add_number >> 1;
13135 INSERT_OPERAND (1, IMMH, *ip, imm);
13136 }
13137 s = expr_end;
13138 continue;
13139
13140 case 'I':
13141 {
13142 bfd_reloc_code_real_type r[3];
13143 expressionS ep;
13144 int imm;
13145
13146 if (my_getSmallExpression (&ep, r, s) > 0
13147 || !expr_const_in_range (&ep, -1, 127, 0))
13148 break;
13149
13150 imm = ep.X_add_number & 127;
13151 INSERT_OPERAND (1, IMMI, *ip, imm);
13152 }
13153 s = expr_end;
13154 continue;
13155
13156 case 'J':
13157 {
13158 bfd_reloc_code_real_type r[3];
13159 expressionS ep;
13160 int imm;
13161
13162 /* Check whether there is only a single bracketed
13163 expression left. If so, it must be the base register
13164 and the constant must be zero. */
13165 if (*s == '(' && strchr (s + 1, '(') == 0)
13166 {
13167 INSERT_OPERAND (1, IMMJ, *ip, 0);
13168 continue;
13169 }
13170
13171 if (my_getSmallExpression (&ep, r, s) > 0
13172 || !expr_const_in_range (&ep, 0, 16, 2))
13173 break;
13174
13175 imm = ep.X_add_number >> 2;
13176 INSERT_OPERAND (1, IMMJ, *ip, imm);
13177 }
13178 s = expr_end;
13179 continue;
13180
13181 case 'L':
13182 {
13183 bfd_reloc_code_real_type r[3];
13184 expressionS ep;
13185 int imm;
13186
13187 /* Check whether there is only a single bracketed
13188 expression left. If so, it must be the base register
13189 and the constant must be zero. */
13190 if (*s == '(' && strchr (s + 1, '(') == 0)
13191 {
13192 INSERT_OPERAND (1, IMML, *ip, 0);
13193 continue;
13194 }
13195
13196 if (my_getSmallExpression (&ep, r, s) > 0
13197 || !expr_const_in_range (&ep, 0, 16, 0))
13198 break;
13199
13200 imm = ep.X_add_number;
13201 INSERT_OPERAND (1, IMML, *ip, imm);
13202 }
13203 s = expr_end;
13204 continue;
13205
13206 case 'M':
13207 {
13208 bfd_reloc_code_real_type r[3];
13209 expressionS ep;
13210 int imm;
13211
13212 if (my_getSmallExpression (&ep, r, s) > 0
13213 || !expr_const_in_range (&ep, 1, 9, 0))
13214 break;
13215
13216 imm = ep.X_add_number & 7;
13217 INSERT_OPERAND (1, IMMM, *ip, imm);
13218 }
13219 s = expr_end;
13220 continue;
13221
13222 case 'N': /* Register list for lwm and swm. */
13223 {
13224 /* A comma-separated list of registers and/or
13225 dash-separated contiguous ranges including
13226 both ra and a set of one or more registers
13227 starting at s0 up to s3 which have to be
13228 consecutive, e.g.:
13229
13230 s0, ra
13231 s0, s1, ra, s2, s3
13232 s0-s2, ra
13233
13234 and any permutations of these. */
13235 unsigned int reglist;
13236 int imm;
13237
13238 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13239 break;
13240
13241 if ((reglist & 0xfff1ffff) != 0x80010000)
13242 break;
13243
13244 reglist = (reglist >> 17) & 7;
13245 reglist += 1;
13246 if ((reglist & -reglist) != reglist)
13247 break;
252b5132 13248
df58fc94
RS
13249 imm = ffs (reglist) - 1;
13250 INSERT_OPERAND (1, IMMN, *ip, imm);
13251 }
13252 continue;
252b5132 13253
df58fc94
RS
13254 case 'O': /* sdbbp 4-bit code. */
13255 {
13256 bfd_reloc_code_real_type r[3];
13257 expressionS ep;
13258 int imm;
13259
13260 if (my_getSmallExpression (&ep, r, s) > 0
13261 || !expr_const_in_range (&ep, 0, 16, 0))
13262 break;
13263
13264 imm = ep.X_add_number;
13265 INSERT_OPERAND (1, IMMO, *ip, imm);
252b5132 13266 }
df58fc94
RS
13267 s = expr_end;
13268 continue;
252b5132 13269
df58fc94
RS
13270 case 'P':
13271 {
13272 bfd_reloc_code_real_type r[3];
13273 expressionS ep;
13274 int imm;
5e0116d5 13275
df58fc94
RS
13276 if (my_getSmallExpression (&ep, r, s) > 0
13277 || !expr_const_in_range (&ep, 0, 32, 2))
13278 break;
5e0116d5 13279
df58fc94
RS
13280 imm = ep.X_add_number >> 2;
13281 INSERT_OPERAND (1, IMMP, *ip, imm);
13282 }
13283 s = expr_end;
13284 continue;
5e0116d5 13285
df58fc94
RS
13286 case 'Q':
13287 {
13288 bfd_reloc_code_real_type r[3];
13289 expressionS ep;
13290 int imm;
5e0116d5 13291
df58fc94
RS
13292 if (my_getSmallExpression (&ep, r, s) > 0
13293 || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13294 break;
252b5132 13295
df58fc94
RS
13296 imm = ep.X_add_number >> 2;
13297 INSERT_OPERAND (1, IMMQ, *ip, imm);
13298 }
13299 s = expr_end;
13300 continue;
4614d845 13301
df58fc94
RS
13302 case 'U':
13303 {
13304 bfd_reloc_code_real_type r[3];
13305 expressionS ep;
13306 int imm;
13307
13308 /* Check whether there is only a single bracketed
13309 expression left. If so, it must be the base register
13310 and the constant must be zero. */
13311 if (*s == '(' && strchr (s + 1, '(') == 0)
13312 {
13313 INSERT_OPERAND (1, IMMU, *ip, 0);
13314 continue;
13315 }
13316
13317 if (my_getSmallExpression (&ep, r, s) > 0
13318 || !expr_const_in_range (&ep, 0, 32, 2))
13319 break;
13320
13321 imm = ep.X_add_number >> 2;
13322 INSERT_OPERAND (1, IMMU, *ip, imm);
13323 }
13324 s = expr_end;
5e0116d5 13325 continue;
252b5132 13326
df58fc94
RS
13327 case 'W':
13328 {
13329 bfd_reloc_code_real_type r[3];
13330 expressionS ep;
13331 int imm;
252b5132 13332
df58fc94
RS
13333 if (my_getSmallExpression (&ep, r, s) > 0
13334 || !expr_const_in_range (&ep, 0, 64, 2))
13335 break;
252b5132 13336
df58fc94
RS
13337 imm = ep.X_add_number >> 2;
13338 INSERT_OPERAND (1, IMMW, *ip, imm);
13339 }
13340 s = expr_end;
13341 continue;
252b5132 13342
df58fc94
RS
13343 case 'X':
13344 {
13345 bfd_reloc_code_real_type r[3];
13346 expressionS ep;
13347 int imm;
252b5132 13348
df58fc94
RS
13349 if (my_getSmallExpression (&ep, r, s) > 0
13350 || !expr_const_in_range (&ep, -8, 8, 0))
13351 break;
252b5132 13352
df58fc94
RS
13353 imm = ep.X_add_number;
13354 INSERT_OPERAND (1, IMMX, *ip, imm);
13355 }
13356 s = expr_end;
13357 continue;
252b5132 13358
df58fc94
RS
13359 case 'Y':
13360 {
13361 bfd_reloc_code_real_type r[3];
13362 expressionS ep;
13363 int imm;
156c2f8b 13364
df58fc94
RS
13365 if (my_getSmallExpression (&ep, r, s) > 0
13366 || expr_const_in_range (&ep, -2, 2, 2)
13367 || !expr_const_in_range (&ep, -258, 258, 2))
13368 break;
156c2f8b 13369
df58fc94
RS
13370 imm = ep.X_add_number >> 2;
13371 imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13372 INSERT_OPERAND (1, IMMY, *ip, imm);
13373 }
13374 s = expr_end;
13375 continue;
60b63b72 13376
df58fc94
RS
13377 case 'Z':
13378 {
13379 bfd_reloc_code_real_type r[3];
13380 expressionS ep;
13381
13382 if (my_getSmallExpression (&ep, r, s) > 0
13383 || !expr_const_in_range (&ep, 0, 1, 0))
13384 break;
13385 }
13386 s = expr_end;
13387 continue;
13388
13389 default:
13390 as_bad (_("Internal error: bad microMIPS opcode "
13391 "(unknown extension operand type `m%c'): %s %s"),
13392 *args, insn->name, insn->args);
13393 /* Further processing is fruitless. */
13394 return;
60b63b72 13395 }
df58fc94 13396 break;
60b63b72 13397
df58fc94
RS
13398 case 'n': /* Register list for 32-bit lwm and swm. */
13399 gas_assert (mips_opts.micromips);
13400 {
13401 /* A comma-separated list of registers and/or
13402 dash-separated contiguous ranges including
13403 at least one of ra and a set of one or more
13404 registers starting at s0 up to s7 and then
13405 s8 which have to be consecutive, e.g.:
13406
13407 ra
13408 s0
13409 ra, s0, s1, s2
13410 s0-s8
13411 s0-s5, ra
13412
13413 and any permutations of these. */
13414 unsigned int reglist;
13415 int imm;
13416 int ra;
13417
13418 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13419 break;
13420
13421 if ((reglist & 0x3f00ffff) != 0)
13422 break;
13423
13424 ra = (reglist >> 27) & 0x10;
13425 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13426 reglist += 1;
13427 if ((reglist & -reglist) != reglist)
13428 break;
13429
13430 imm = (ffs (reglist) - 1) | ra;
13431 INSERT_OPERAND (1, RT, *ip, imm);
13432 imm_expr.X_op = O_absent;
13433 }
60b63b72
RS
13434 continue;
13435
df58fc94
RS
13436 case '|': /* 4-bit trap code. */
13437 gas_assert (mips_opts.micromips);
60b63b72
RS
13438 my_getExpression (&imm_expr, s);
13439 check_absolute_expr (ip, &imm_expr);
60b63b72 13440 if ((unsigned long) imm_expr.X_add_number
df58fc94
RS
13441 > MICROMIPSOP_MASK_TRAP)
13442 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13443 (unsigned long) imm_expr.X_add_number,
13444 ip->insn_mo->name);
13445 INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
60b63b72
RS
13446 imm_expr.X_op = O_absent;
13447 s = expr_end;
13448 continue;
13449
252b5132 13450 default:
f71d0d44 13451 as_bad (_("Bad char = '%c'\n"), *args);
b37df7c4 13452 abort ();
252b5132
RH
13453 }
13454 break;
13455 }
13456 /* Args don't match. */
df58fc94
RS
13457 s = argsStart;
13458 insn_error = _("Illegal operands");
13459 if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
252b5132
RH
13460 {
13461 ++insn;
252b5132
RH
13462 continue;
13463 }
df58fc94
RS
13464 else if (wrong_delay_slot_insns && need_delay_slot_ok)
13465 {
13466 gas_assert (firstinsn);
13467 need_delay_slot_ok = FALSE;
13468 past = insn + 1;
13469 insn = firstinsn;
13470 continue;
13471 }
252b5132
RH
13472 return;
13473 }
13474}
13475
0499d65b
TS
13476#define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13477
252b5132
RH
13478/* This routine assembles an instruction into its binary format when
13479 assembling for the mips16. As a side effect, it sets one of the
df58fc94
RS
13480 global variables imm_reloc or offset_reloc to the type of relocation
13481 to do if one of the operands is an address expression. It also sets
13482 forced_insn_length to the resulting instruction size in bytes if the
13483 user explicitly requested a small or extended instruction. */
252b5132
RH
13484
13485static void
17a2f251 13486mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
13487{
13488 char *s;
13489 const char *args;
13490 struct mips_opcode *insn;
13491 char *argsstart;
13492 unsigned int regno;
13493 unsigned int lastregno = 0;
13494 char *s_reset;
d6f16593 13495 size_t i;
252b5132
RH
13496
13497 insn_error = NULL;
13498
df58fc94 13499 forced_insn_length = 0;
252b5132 13500
3882b010 13501 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
13502 ;
13503 switch (*s)
13504 {
13505 case '\0':
13506 break;
13507
13508 case ' ':
13509 *s++ = '\0';
13510 break;
13511
13512 case '.':
13513 if (s[1] == 't' && s[2] == ' ')
13514 {
13515 *s = '\0';
df58fc94 13516 forced_insn_length = 2;
252b5132
RH
13517 s += 3;
13518 break;
13519 }
13520 else if (s[1] == 'e' && s[2] == ' ')
13521 {
13522 *s = '\0';
df58fc94 13523 forced_insn_length = 4;
252b5132
RH
13524 s += 3;
13525 break;
13526 }
13527 /* Fall through. */
13528 default:
13529 insn_error = _("unknown opcode");
13530 return;
13531 }
13532
df58fc94
RS
13533 if (mips_opts.noautoextend && !forced_insn_length)
13534 forced_insn_length = 2;
252b5132
RH
13535
13536 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13537 {
13538 insn_error = _("unrecognized opcode");
13539 return;
13540 }
13541
13542 argsstart = s;
13543 for (;;)
13544 {
9b3f89ee
TS
13545 bfd_boolean ok;
13546
9c2799c2 13547 gas_assert (strcmp (insn->name, str) == 0);
252b5132 13548
037b32b9 13549 ok = is_opcode_valid_16 (insn);
9b3f89ee
TS
13550 if (! ok)
13551 {
13552 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13553 && strcmp (insn->name, insn[1].name) == 0)
13554 {
13555 ++insn;
13556 continue;
13557 }
13558 else
13559 {
13560 if (!insn_error)
13561 {
13562 static char buf[100];
13563 sprintf (buf,
7bd942df 13564 _("Opcode not supported on this processor: %s (%s)"),
9b3f89ee
TS
13565 mips_cpu_info_from_arch (mips_opts.arch)->name,
13566 mips_cpu_info_from_isa (mips_opts.isa)->name);
13567 insn_error = buf;
13568 }
13569 return;
13570 }
13571 }
13572
1e915849 13573 create_insn (ip, insn);
252b5132 13574 imm_expr.X_op = O_absent;
f6688943
TS
13575 imm_reloc[0] = BFD_RELOC_UNUSED;
13576 imm_reloc[1] = BFD_RELOC_UNUSED;
13577 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 13578 imm2_expr.X_op = O_absent;
252b5132 13579 offset_expr.X_op = O_absent;
f6688943
TS
13580 offset_reloc[0] = BFD_RELOC_UNUSED;
13581 offset_reloc[1] = BFD_RELOC_UNUSED;
13582 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
13583 for (args = insn->args; 1; ++args)
13584 {
13585 int c;
13586
13587 if (*s == ' ')
13588 ++s;
13589
13590 /* In this switch statement we call break if we did not find
13591 a match, continue if we did find a match, or return if we
13592 are done. */
13593
13594 c = *args;
13595 switch (c)
13596 {
13597 case '\0':
13598 if (*s == '\0')
13599 {
b886a2ab
RS
13600 offsetT value;
13601
252b5132
RH
13602 /* Stuff the immediate value in now, if we can. */
13603 if (imm_expr.X_op == O_constant
f6688943 13604 && *imm_reloc > BFD_RELOC_UNUSED
b886a2ab
RS
13605 && insn->pinfo != INSN_MACRO
13606 && calculate_reloc (*offset_reloc,
13607 imm_expr.X_add_number, &value))
252b5132 13608 {
c4e7957c 13609 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b886a2ab 13610 *offset_reloc, value, forced_insn_length,
43c0598f 13611 &ip->insn_opcode);
252b5132 13612 imm_expr.X_op = O_absent;
f6688943 13613 *imm_reloc = BFD_RELOC_UNUSED;
43c0598f 13614 *offset_reloc = BFD_RELOC_UNUSED;
252b5132
RH
13615 }
13616
13617 return;
13618 }
13619 break;
13620
13621 case ',':
13622 if (*s++ == c)
13623 continue;
13624 s--;
13625 switch (*++args)
13626 {
13627 case 'v':
bf12938e 13628 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
13629 continue;
13630 case 'w':
bf12938e 13631 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
13632 continue;
13633 }
13634 break;
13635
13636 case '(':
13637 case ')':
13638 if (*s++ == c)
13639 continue;
13640 break;
13641
13642 case 'v':
13643 case 'w':
13644 if (s[0] != '$')
13645 {
13646 if (c == 'v')
bf12938e 13647 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 13648 else
bf12938e 13649 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
13650 ++args;
13651 continue;
13652 }
13653 /* Fall through. */
13654 case 'x':
13655 case 'y':
13656 case 'z':
13657 case 'Z':
13658 case '0':
13659 case 'S':
13660 case 'R':
13661 case 'X':
13662 case 'Y':
707bfff6
TS
13663 s_reset = s;
13664 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
252b5132 13665 {
707bfff6 13666 if (c == 'v' || c == 'w')
85b51719 13667 {
707bfff6 13668 if (c == 'v')
a9e24354 13669 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
707bfff6 13670 else
a9e24354 13671 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
707bfff6
TS
13672 ++args;
13673 continue;
85b51719 13674 }
707bfff6 13675 break;
252b5132
RH
13676 }
13677
13678 if (*s == ' ')
13679 ++s;
13680 if (args[1] != *s)
13681 {
13682 if (c == 'v' || c == 'w')
13683 {
13684 regno = mips16_to_32_reg_map[lastregno];
13685 s = s_reset;
f9419b05 13686 ++args;
252b5132
RH
13687 }
13688 }
13689
13690 switch (c)
13691 {
13692 case 'x':
13693 case 'y':
13694 case 'z':
13695 case 'v':
13696 case 'w':
13697 case 'Z':
13698 regno = mips32_to_16_reg_map[regno];
13699 break;
13700
13701 case '0':
13702 if (regno != 0)
13703 regno = ILLEGAL_REG;
13704 break;
13705
13706 case 'S':
13707 if (regno != SP)
13708 regno = ILLEGAL_REG;
13709 break;
13710
13711 case 'R':
13712 if (regno != RA)
13713 regno = ILLEGAL_REG;
13714 break;
13715
13716 case 'X':
13717 case 'Y':
741fe287
MR
13718 if (regno == AT && mips_opts.at)
13719 {
13720 if (mips_opts.at == ATREG)
13721 as_warn (_("used $at without \".set noat\""));
13722 else
13723 as_warn (_("used $%u with \".set at=$%u\""),
13724 regno, mips_opts.at);
13725 }
252b5132
RH
13726 break;
13727
13728 default:
b37df7c4 13729 abort ();
252b5132
RH
13730 }
13731
13732 if (regno == ILLEGAL_REG)
13733 break;
13734
13735 switch (c)
13736 {
13737 case 'x':
13738 case 'v':
bf12938e 13739 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
13740 break;
13741 case 'y':
13742 case 'w':
bf12938e 13743 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
13744 break;
13745 case 'z':
bf12938e 13746 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
13747 break;
13748 case 'Z':
bf12938e 13749 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
13750 case '0':
13751 case 'S':
13752 case 'R':
13753 break;
13754 case 'X':
bf12938e 13755 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
13756 break;
13757 case 'Y':
13758 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 13759 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
13760 break;
13761 default:
b37df7c4 13762 abort ();
252b5132
RH
13763 }
13764
13765 lastregno = regno;
13766 continue;
13767
13768 case 'P':
13769 if (strncmp (s, "$pc", 3) == 0)
13770 {
13771 s += 3;
13772 continue;
13773 }
13774 break;
13775
252b5132
RH
13776 case '5':
13777 case 'H':
13778 case 'W':
13779 case 'D':
13780 case 'j':
252b5132
RH
13781 case 'V':
13782 case 'C':
13783 case 'U':
13784 case 'k':
13785 case 'K':
d6f16593
MR
13786 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
13787 if (i > 0)
252b5132 13788 {
d6f16593 13789 if (imm_expr.X_op != O_constant)
252b5132 13790 {
df58fc94 13791 forced_insn_length = 4;
5c04167a 13792 ip->insn_opcode |= MIPS16_EXTEND;
252b5132 13793 }
d6f16593
MR
13794 else
13795 {
13796 /* We need to relax this instruction. */
13797 *offset_reloc = *imm_reloc;
13798 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13799 }
13800 s = expr_end;
13801 continue;
252b5132 13802 }
d6f16593
MR
13803 *imm_reloc = BFD_RELOC_UNUSED;
13804 /* Fall through. */
13805 case '<':
13806 case '>':
13807 case '[':
13808 case ']':
13809 case '4':
13810 case '8':
13811 my_getExpression (&imm_expr, s);
252b5132
RH
13812 if (imm_expr.X_op == O_register)
13813 {
13814 /* What we thought was an expression turned out to
13815 be a register. */
13816
13817 if (s[0] == '(' && args[1] == '(')
13818 {
13819 /* It looks like the expression was omitted
13820 before a register indirection, which means
13821 that the expression is implicitly zero. We
13822 still set up imm_expr, so that we handle
13823 explicit extensions correctly. */
13824 imm_expr.X_op = O_constant;
13825 imm_expr.X_add_number = 0;
f6688943 13826 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13827 continue;
13828 }
13829
13830 break;
13831 }
13832
13833 /* We need to relax this instruction. */
f6688943 13834 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13835 s = expr_end;
13836 continue;
13837
13838 case 'p':
13839 case 'q':
13840 case 'A':
13841 case 'B':
13842 case 'E':
13843 /* We use offset_reloc rather than imm_reloc for the PC
13844 relative operands. This lets macros with both
13845 immediate and address operands work correctly. */
13846 my_getExpression (&offset_expr, s);
13847
13848 if (offset_expr.X_op == O_register)
13849 break;
13850
13851 /* We need to relax this instruction. */
f6688943 13852 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13853 s = expr_end;
13854 continue;
13855
13856 case '6': /* break code */
13857 my_getExpression (&imm_expr, s);
13858 check_absolute_expr (ip, &imm_expr);
13859 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
13860 as_warn (_("Invalid value for `%s' (%lu)"),
13861 ip->insn_mo->name,
13862 (unsigned long) imm_expr.X_add_number);
13863 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
13864 imm_expr.X_op = O_absent;
13865 s = expr_end;
13866 continue;
13867
13868 case 'a': /* 26 bit address */
13869 my_getExpression (&offset_expr, s);
13870 s = expr_end;
f6688943 13871 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
13872 ip->insn_opcode <<= 16;
13873 continue;
13874
13875 case 'l': /* register list for entry macro */
13876 case 'L': /* register list for exit macro */
13877 {
13878 int mask;
13879
13880 if (c == 'l')
13881 mask = 0;
13882 else
13883 mask = 7 << 3;
13884 while (*s != '\0')
13885 {
707bfff6 13886 unsigned int freg, reg1, reg2;
252b5132
RH
13887
13888 while (*s == ' ' || *s == ',')
13889 ++s;
707bfff6 13890 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
252b5132 13891 freg = 0;
707bfff6
TS
13892 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
13893 freg = 1;
252b5132
RH
13894 else
13895 {
707bfff6
TS
13896 as_bad (_("can't parse register list"));
13897 break;
252b5132
RH
13898 }
13899 if (*s == ' ')
13900 ++s;
13901 if (*s != '-')
13902 reg2 = reg1;
13903 else
13904 {
13905 ++s;
707bfff6
TS
13906 if (!reg_lookup (&s, freg ? RTYPE_FPU
13907 : (RTYPE_GP | RTYPE_NUM), &reg2))
252b5132 13908 {
707bfff6
TS
13909 as_bad (_("invalid register list"));
13910 break;
252b5132
RH
13911 }
13912 }
13913 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
13914 {
13915 mask &= ~ (7 << 3);
13916 mask |= 5 << 3;
13917 }
13918 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
13919 {
13920 mask &= ~ (7 << 3);
13921 mask |= 6 << 3;
13922 }
13923 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
13924 mask |= (reg2 - 3) << 3;
13925 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
13926 mask |= (reg2 - 15) << 1;
f9419b05 13927 else if (reg1 == RA && reg2 == RA)
252b5132
RH
13928 mask |= 1;
13929 else
13930 {
13931 as_bad (_("invalid register list"));
13932 break;
13933 }
13934 }
13935 /* The mask is filled in in the opcode table for the
13936 benefit of the disassembler. We remove it before
13937 applying the actual mask. */
13938 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
13939 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
13940 }
13941 continue;
13942
0499d65b
TS
13943 case 'm': /* Register list for save insn. */
13944 case 'M': /* Register list for restore insn. */
13945 {
5c04167a 13946 int opcode = ip->insn_opcode;
0499d65b 13947 int framesz = 0, seen_framesz = 0;
91d6fa6a 13948 int nargs = 0, statics = 0, sregs = 0;
0499d65b
TS
13949
13950 while (*s != '\0')
13951 {
13952 unsigned int reg1, reg2;
13953
13954 SKIP_SPACE_TABS (s);
13955 while (*s == ',')
13956 ++s;
13957 SKIP_SPACE_TABS (s);
13958
13959 my_getExpression (&imm_expr, s);
13960 if (imm_expr.X_op == O_constant)
13961 {
13962 /* Handle the frame size. */
13963 if (seen_framesz)
13964 {
13965 as_bad (_("more than one frame size in list"));
13966 break;
13967 }
13968 seen_framesz = 1;
13969 framesz = imm_expr.X_add_number;
13970 imm_expr.X_op = O_absent;
13971 s = expr_end;
13972 continue;
13973 }
13974
707bfff6 13975 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
0499d65b
TS
13976 {
13977 as_bad (_("can't parse register list"));
13978 break;
13979 }
0499d65b 13980
707bfff6
TS
13981 while (*s == ' ')
13982 ++s;
13983
0499d65b
TS
13984 if (*s != '-')
13985 reg2 = reg1;
13986 else
13987 {
13988 ++s;
707bfff6
TS
13989 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
13990 || reg2 < reg1)
0499d65b
TS
13991 {
13992 as_bad (_("can't parse register list"));
13993 break;
13994 }
0499d65b
TS
13995 }
13996
13997 while (reg1 <= reg2)
13998 {
13999 if (reg1 >= 4 && reg1 <= 7)
14000 {
3a93f742 14001 if (!seen_framesz)
0499d65b 14002 /* args $a0-$a3 */
91d6fa6a 14003 nargs |= 1 << (reg1 - 4);
0499d65b
TS
14004 else
14005 /* statics $a0-$a3 */
14006 statics |= 1 << (reg1 - 4);
14007 }
14008 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
14009 {
14010 /* $s0-$s8 */
14011 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
14012 }
14013 else if (reg1 == 31)
14014 {
14015 /* Add $ra to insn. */
14016 opcode |= 0x40;
14017 }
14018 else
14019 {
14020 as_bad (_("unexpected register in list"));
14021 break;
14022 }
14023 if (++reg1 == 24)
14024 reg1 = 30;
14025 }
14026 }
14027
14028 /* Encode args/statics combination. */
91d6fa6a 14029 if (nargs & statics)
0499d65b 14030 as_bad (_("arg/static registers overlap"));
91d6fa6a 14031 else if (nargs == 0xf)
0499d65b
TS
14032 /* All $a0-$a3 are args. */
14033 opcode |= MIPS16_ALL_ARGS << 16;
14034 else if (statics == 0xf)
14035 /* All $a0-$a3 are statics. */
14036 opcode |= MIPS16_ALL_STATICS << 16;
14037 else
14038 {
14039 int narg = 0, nstat = 0;
14040
14041 /* Count arg registers. */
91d6fa6a 14042 while (nargs & 0x1)
0499d65b 14043 {
91d6fa6a 14044 nargs >>= 1;
0499d65b
TS
14045 narg++;
14046 }
91d6fa6a 14047 if (nargs != 0)
0499d65b
TS
14048 as_bad (_("invalid arg register list"));
14049
14050 /* Count static registers. */
14051 while (statics & 0x8)
14052 {
14053 statics = (statics << 1) & 0xf;
14054 nstat++;
14055 }
14056 if (statics != 0)
14057 as_bad (_("invalid static register list"));
14058
14059 /* Encode args/statics. */
14060 opcode |= ((narg << 2) | nstat) << 16;
14061 }
14062
14063 /* Encode $s0/$s1. */
14064 if (sregs & (1 << 0)) /* $s0 */
14065 opcode |= 0x20;
14066 if (sregs & (1 << 1)) /* $s1 */
14067 opcode |= 0x10;
14068 sregs >>= 2;
14069
14070 if (sregs != 0)
14071 {
14072 /* Count regs $s2-$s8. */
14073 int nsreg = 0;
14074 while (sregs & 1)
14075 {
14076 sregs >>= 1;
14077 nsreg++;
14078 }
14079 if (sregs != 0)
14080 as_bad (_("invalid static register list"));
14081 /* Encode $s2-$s8. */
14082 opcode |= nsreg << 24;
14083 }
14084
14085 /* Encode frame size. */
14086 if (!seen_framesz)
14087 as_bad (_("missing frame size"));
14088 else if ((framesz & 7) != 0 || framesz < 0
14089 || framesz > 0xff * 8)
14090 as_bad (_("invalid frame size"));
14091 else if (framesz != 128 || (opcode >> 16) != 0)
14092 {
14093 framesz /= 8;
14094 opcode |= (((framesz & 0xf0) << 16)
14095 | (framesz & 0x0f));
14096 }
14097
14098 /* Finally build the instruction. */
14099 if ((opcode >> 16) != 0 || framesz == 0)
5c04167a
RS
14100 opcode |= MIPS16_EXTEND;
14101 ip->insn_opcode = opcode;
0499d65b
TS
14102 }
14103 continue;
14104
252b5132
RH
14105 case 'e': /* extend code */
14106 my_getExpression (&imm_expr, s);
14107 check_absolute_expr (ip, &imm_expr);
14108 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
14109 {
14110 as_warn (_("Invalid value for `%s' (%lu)"),
14111 ip->insn_mo->name,
14112 (unsigned long) imm_expr.X_add_number);
14113 imm_expr.X_add_number &= 0x7ff;
14114 }
14115 ip->insn_opcode |= imm_expr.X_add_number;
14116 imm_expr.X_op = O_absent;
14117 s = expr_end;
14118 continue;
14119
14120 default:
b37df7c4 14121 abort ();
252b5132
RH
14122 }
14123 break;
14124 }
14125
14126 /* Args don't match. */
14127 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
14128 strcmp (insn->name, insn[1].name) == 0)
14129 {
14130 ++insn;
14131 s = argsstart;
14132 continue;
14133 }
14134
14135 insn_error = _("illegal operands");
14136
14137 return;
14138 }
14139}
14140
14141/* This structure holds information we know about a mips16 immediate
14142 argument type. */
14143
e972090a
NC
14144struct mips16_immed_operand
14145{
252b5132
RH
14146 /* The type code used in the argument string in the opcode table. */
14147 int type;
14148 /* The number of bits in the short form of the opcode. */
14149 int nbits;
14150 /* The number of bits in the extended form of the opcode. */
14151 int extbits;
14152 /* The amount by which the short form is shifted when it is used;
14153 for example, the sw instruction has a shift count of 2. */
14154 int shift;
14155 /* The amount by which the short form is shifted when it is stored
14156 into the instruction code. */
14157 int op_shift;
14158 /* Non-zero if the short form is unsigned. */
14159 int unsp;
14160 /* Non-zero if the extended form is unsigned. */
14161 int extu;
14162 /* Non-zero if the value is PC relative. */
14163 int pcrel;
14164};
14165
14166/* The mips16 immediate operand types. */
14167
14168static const struct mips16_immed_operand mips16_immed_operands[] =
14169{
14170 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14171 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14172 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14173 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14174 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14175 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14176 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14177 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14178 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14179 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14180 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14181 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14182 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14183 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14184 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14185 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14186 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14187 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14188 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14189 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14190 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14191};
14192
14193#define MIPS16_NUM_IMMED \
14194 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14195
b886a2ab
RS
14196/* Marshal immediate value VAL for an extended MIPS16 instruction.
14197 NBITS is the number of significant bits in VAL. */
14198
14199static unsigned long
14200mips16_immed_extend (offsetT val, unsigned int nbits)
14201{
14202 int extval;
14203 if (nbits == 16)
14204 {
14205 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14206 val &= 0x1f;
14207 }
14208 else if (nbits == 15)
14209 {
14210 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14211 val &= 0xf;
14212 }
14213 else
14214 {
14215 extval = ((val & 0x1f) << 6) | (val & 0x20);
14216 val = 0;
14217 }
14218 return (extval << 16) | val;
14219}
14220
5c04167a
RS
14221/* Install immediate value VAL into MIPS16 instruction *INSN,
14222 extending it if necessary. The instruction in *INSN may
14223 already be extended.
14224
43c0598f
RS
14225 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14226 if none. In the former case, VAL is a 16-bit number with no
14227 defined signedness.
14228
14229 TYPE is the type of the immediate field. USER_INSN_LENGTH
14230 is the length that the user requested, or 0 if none. */
252b5132
RH
14231
14232static void
43c0598f
RS
14233mips16_immed (char *file, unsigned int line, int type,
14234 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14235 unsigned int user_insn_length, unsigned long *insn)
252b5132 14236{
3994f87e 14237 const struct mips16_immed_operand *op;
252b5132 14238 int mintiny, maxtiny;
252b5132
RH
14239
14240 op = mips16_immed_operands;
14241 while (op->type != type)
14242 {
14243 ++op;
9c2799c2 14244 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
14245 }
14246
14247 if (op->unsp)
14248 {
14249 if (type == '<' || type == '>' || type == '[' || type == ']')
14250 {
14251 mintiny = 1;
14252 maxtiny = 1 << op->nbits;
14253 }
14254 else
14255 {
14256 mintiny = 0;
14257 maxtiny = (1 << op->nbits) - 1;
14258 }
43c0598f
RS
14259 if (reloc != BFD_RELOC_UNUSED)
14260 val &= 0xffff;
252b5132
RH
14261 }
14262 else
14263 {
14264 mintiny = - (1 << (op->nbits - 1));
14265 maxtiny = (1 << (op->nbits - 1)) - 1;
43c0598f
RS
14266 if (reloc != BFD_RELOC_UNUSED)
14267 val = SEXT_16BIT (val);
252b5132
RH
14268 }
14269
14270 /* Branch offsets have an implicit 0 in the lowest bit. */
14271 if (type == 'p' || type == 'q')
14272 val /= 2;
14273
14274 if ((val & ((1 << op->shift) - 1)) != 0
14275 || val < (mintiny << op->shift)
14276 || val > (maxtiny << op->shift))
5c04167a
RS
14277 {
14278 /* We need an extended instruction. */
14279 if (user_insn_length == 2)
14280 as_bad_where (file, line, _("invalid unextended operand value"));
14281 else
14282 *insn |= MIPS16_EXTEND;
14283 }
14284 else if (user_insn_length == 4)
14285 {
14286 /* The operand doesn't force an unextended instruction to be extended.
14287 Warn if the user wanted an extended instruction anyway. */
14288 *insn |= MIPS16_EXTEND;
14289 as_warn_where (file, line,
14290 _("extended operand requested but not required"));
14291 }
252b5132 14292
5c04167a 14293 if (mips16_opcode_length (*insn) == 2)
252b5132
RH
14294 {
14295 int insnval;
14296
252b5132
RH
14297 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14298 insnval <<= op->op_shift;
14299 *insn |= insnval;
14300 }
14301 else
14302 {
14303 long minext, maxext;
252b5132 14304
43c0598f 14305 if (reloc == BFD_RELOC_UNUSED)
252b5132 14306 {
43c0598f
RS
14307 if (op->extu)
14308 {
14309 minext = 0;
14310 maxext = (1 << op->extbits) - 1;
14311 }
14312 else
14313 {
14314 minext = - (1 << (op->extbits - 1));
14315 maxext = (1 << (op->extbits - 1)) - 1;
14316 }
14317 if (val < minext || val > maxext)
14318 as_bad_where (file, line,
14319 _("operand value out of range for instruction"));
252b5132 14320 }
252b5132 14321
b886a2ab 14322 *insn |= mips16_immed_extend (val, op->extbits);
252b5132
RH
14323 }
14324}
14325\f
d6f16593 14326struct percent_op_match
ad8d3bb3 14327{
5e0116d5
RS
14328 const char *str;
14329 bfd_reloc_code_real_type reloc;
d6f16593
MR
14330};
14331
14332static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14333{
5e0116d5 14334 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 14335#ifdef OBJ_ELF
5e0116d5
RS
14336 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14337 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14338 {"%call16", BFD_RELOC_MIPS_CALL16},
14339 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14340 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14341 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14342 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14343 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14344 {"%got", BFD_RELOC_MIPS_GOT16},
14345 {"%gp_rel", BFD_RELOC_GPREL16},
14346 {"%half", BFD_RELOC_16},
14347 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14348 {"%higher", BFD_RELOC_MIPS_HIGHER},
14349 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14350 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14351 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14352 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14353 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14354 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14355 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14356 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
ad8d3bb3 14357#endif
5e0116d5 14358 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
14359};
14360
d6f16593
MR
14361static const struct percent_op_match mips16_percent_op[] =
14362{
14363 {"%lo", BFD_RELOC_MIPS16_LO16},
14364 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14365 {"%got", BFD_RELOC_MIPS16_GOT16},
14366 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14367 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14368 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14369 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14370 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14371 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14372 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14373 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14374 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14375};
14376
252b5132 14377
5e0116d5
RS
14378/* Return true if *STR points to a relocation operator. When returning true,
14379 move *STR over the operator and store its relocation code in *RELOC.
14380 Leave both *STR and *RELOC alone when returning false. */
14381
14382static bfd_boolean
17a2f251 14383parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14384{
d6f16593
MR
14385 const struct percent_op_match *percent_op;
14386 size_t limit, i;
14387
14388 if (mips_opts.mips16)
14389 {
14390 percent_op = mips16_percent_op;
14391 limit = ARRAY_SIZE (mips16_percent_op);
14392 }
14393 else
14394 {
14395 percent_op = mips_percent_op;
14396 limit = ARRAY_SIZE (mips_percent_op);
14397 }
76b3015f 14398
d6f16593 14399 for (i = 0; i < limit; i++)
5e0116d5 14400 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14401 {
3f98094e
DJ
14402 int len = strlen (percent_op[i].str);
14403
14404 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14405 continue;
14406
5e0116d5
RS
14407 *str += strlen (percent_op[i].str);
14408 *reloc = percent_op[i].reloc;
394f9b3a 14409
5e0116d5
RS
14410 /* Check whether the output BFD supports this relocation.
14411 If not, issue an error and fall back on something safe. */
14412 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14413 {
20203fb9 14414 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14415 percent_op[i].str);
01a3f561 14416 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14417 }
5e0116d5 14418 return TRUE;
394f9b3a 14419 }
5e0116d5 14420 return FALSE;
394f9b3a 14421}
ad8d3bb3 14422
ad8d3bb3 14423
5e0116d5
RS
14424/* Parse string STR as a 16-bit relocatable operand. Store the
14425 expression in *EP and the relocations in the array starting
14426 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14427
01a3f561 14428 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14429
5e0116d5 14430static size_t
17a2f251
TS
14431my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14432 char *str)
ad8d3bb3 14433{
5e0116d5
RS
14434 bfd_reloc_code_real_type reversed_reloc[3];
14435 size_t reloc_index, i;
09b8f35a
RS
14436 int crux_depth, str_depth;
14437 char *crux;
5e0116d5
RS
14438
14439 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14440 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14441 of the main expression and with CRUX_DEPTH containing the number
14442 of open brackets at that point. */
14443 reloc_index = -1;
14444 str_depth = 0;
14445 do
fb1b3232 14446 {
09b8f35a
RS
14447 reloc_index++;
14448 crux = str;
14449 crux_depth = str_depth;
14450
14451 /* Skip over whitespace and brackets, keeping count of the number
14452 of brackets. */
14453 while (*str == ' ' || *str == '\t' || *str == '(')
14454 if (*str++ == '(')
14455 str_depth++;
5e0116d5 14456 }
09b8f35a
RS
14457 while (*str == '%'
14458 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14459 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14460
09b8f35a 14461 my_getExpression (ep, crux);
5e0116d5 14462 str = expr_end;
394f9b3a 14463
5e0116d5 14464 /* Match every open bracket. */
09b8f35a 14465 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14466 if (*str++ == ')')
09b8f35a 14467 crux_depth--;
394f9b3a 14468
09b8f35a 14469 if (crux_depth > 0)
20203fb9 14470 as_bad (_("unclosed '('"));
394f9b3a 14471
5e0116d5 14472 expr_end = str;
252b5132 14473
01a3f561 14474 if (reloc_index != 0)
64bdfcaf
RS
14475 {
14476 prev_reloc_op_frag = frag_now;
14477 for (i = 0; i < reloc_index; i++)
14478 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14479 }
fb1b3232 14480
5e0116d5 14481 return reloc_index;
252b5132
RH
14482}
14483
14484static void
17a2f251 14485my_getExpression (expressionS *ep, char *str)
252b5132
RH
14486{
14487 char *save_in;
14488
14489 save_in = input_line_pointer;
14490 input_line_pointer = str;
14491 expression (ep);
14492 expr_end = input_line_pointer;
14493 input_line_pointer = save_in;
252b5132
RH
14494}
14495
252b5132 14496char *
17a2f251 14497md_atof (int type, char *litP, int *sizeP)
252b5132 14498{
499ac353 14499 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14500}
14501
14502void
17a2f251 14503md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14504{
14505 if (target_big_endian)
14506 number_to_chars_bigendian (buf, val, n);
14507 else
14508 number_to_chars_littleendian (buf, val, n);
14509}
14510\f
ae948b86 14511#ifdef OBJ_ELF
e013f690
TS
14512static int support_64bit_objects(void)
14513{
14514 const char **list, **l;
aa3d8fdf 14515 int yes;
e013f690
TS
14516
14517 list = bfd_target_list ();
14518 for (l = list; *l != NULL; l++)
aeffff67
RS
14519 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14520 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14521 break;
aa3d8fdf 14522 yes = (*l != NULL);
e013f690 14523 free (list);
aa3d8fdf 14524 return yes;
e013f690 14525}
ae948b86 14526#endif /* OBJ_ELF */
e013f690 14527
78849248 14528const char *md_shortopts = "O::g::G:";
252b5132 14529
23fce1e3
NC
14530enum options
14531 {
14532 OPTION_MARCH = OPTION_MD_BASE,
14533 OPTION_MTUNE,
14534 OPTION_MIPS1,
14535 OPTION_MIPS2,
14536 OPTION_MIPS3,
14537 OPTION_MIPS4,
14538 OPTION_MIPS5,
14539 OPTION_MIPS32,
14540 OPTION_MIPS64,
14541 OPTION_MIPS32R2,
14542 OPTION_MIPS64R2,
14543 OPTION_MIPS16,
14544 OPTION_NO_MIPS16,
14545 OPTION_MIPS3D,
14546 OPTION_NO_MIPS3D,
14547 OPTION_MDMX,
14548 OPTION_NO_MDMX,
14549 OPTION_DSP,
14550 OPTION_NO_DSP,
14551 OPTION_MT,
14552 OPTION_NO_MT,
b015e599
AP
14553 OPTION_VIRT,
14554 OPTION_NO_VIRT,
23fce1e3
NC
14555 OPTION_SMARTMIPS,
14556 OPTION_NO_SMARTMIPS,
14557 OPTION_DSPR2,
14558 OPTION_NO_DSPR2,
df58fc94
RS
14559 OPTION_MICROMIPS,
14560 OPTION_NO_MICROMIPS,
dec0624d
MR
14561 OPTION_MCU,
14562 OPTION_NO_MCU,
23fce1e3
NC
14563 OPTION_COMPAT_ARCH_BASE,
14564 OPTION_M4650,
14565 OPTION_NO_M4650,
14566 OPTION_M4010,
14567 OPTION_NO_M4010,
14568 OPTION_M4100,
14569 OPTION_NO_M4100,
14570 OPTION_M3900,
14571 OPTION_NO_M3900,
14572 OPTION_M7000_HILO_FIX,
6a32d874
CM
14573 OPTION_MNO_7000_HILO_FIX,
14574 OPTION_FIX_24K,
14575 OPTION_NO_FIX_24K,
c67a084a
NC
14576 OPTION_FIX_LOONGSON2F_JUMP,
14577 OPTION_NO_FIX_LOONGSON2F_JUMP,
14578 OPTION_FIX_LOONGSON2F_NOP,
14579 OPTION_NO_FIX_LOONGSON2F_NOP,
23fce1e3
NC
14580 OPTION_FIX_VR4120,
14581 OPTION_NO_FIX_VR4120,
14582 OPTION_FIX_VR4130,
14583 OPTION_NO_FIX_VR4130,
d954098f
DD
14584 OPTION_FIX_CN63XXP1,
14585 OPTION_NO_FIX_CN63XXP1,
23fce1e3
NC
14586 OPTION_TRAP,
14587 OPTION_BREAK,
14588 OPTION_EB,
14589 OPTION_EL,
14590 OPTION_FP32,
14591 OPTION_GP32,
14592 OPTION_CONSTRUCT_FLOATS,
14593 OPTION_NO_CONSTRUCT_FLOATS,
14594 OPTION_FP64,
14595 OPTION_GP64,
14596 OPTION_RELAX_BRANCH,
14597 OPTION_NO_RELAX_BRANCH,
14598 OPTION_MSHARED,
14599 OPTION_MNO_SHARED,
14600 OPTION_MSYM32,
14601 OPTION_MNO_SYM32,
14602 OPTION_SOFT_FLOAT,
14603 OPTION_HARD_FLOAT,
14604 OPTION_SINGLE_FLOAT,
14605 OPTION_DOUBLE_FLOAT,
14606 OPTION_32,
14607#ifdef OBJ_ELF
14608 OPTION_CALL_SHARED,
14609 OPTION_CALL_NONPIC,
14610 OPTION_NON_SHARED,
14611 OPTION_XGOT,
14612 OPTION_MABI,
14613 OPTION_N32,
14614 OPTION_64,
14615 OPTION_MDEBUG,
14616 OPTION_NO_MDEBUG,
14617 OPTION_PDR,
14618 OPTION_NO_PDR,
14619 OPTION_MVXWORKS_PIC,
14620#endif /* OBJ_ELF */
14621 OPTION_END_OF_ENUM
14622 };
14623
e972090a
NC
14624struct option md_longopts[] =
14625{
f9b4148d 14626 /* Options which specify architecture. */
f9b4148d 14627 {"march", required_argument, NULL, OPTION_MARCH},
f9b4148d 14628 {"mtune", required_argument, NULL, OPTION_MTUNE},
252b5132
RH
14629 {"mips0", no_argument, NULL, OPTION_MIPS1},
14630 {"mips1", no_argument, NULL, OPTION_MIPS1},
252b5132 14631 {"mips2", no_argument, NULL, OPTION_MIPS2},
252b5132 14632 {"mips3", no_argument, NULL, OPTION_MIPS3},
252b5132 14633 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86 14634 {"mips5", no_argument, NULL, OPTION_MIPS5},
ae948b86 14635 {"mips32", no_argument, NULL, OPTION_MIPS32},
ae948b86 14636 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d 14637 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13 14638 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
14639
14640 /* Options which specify Application Specific Extensions (ASEs). */
f9b4148d 14641 {"mips16", no_argument, NULL, OPTION_MIPS16},
f9b4148d 14642 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
f9b4148d 14643 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
f9b4148d 14644 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
f9b4148d 14645 {"mdmx", no_argument, NULL, OPTION_MDMX},
f9b4148d 14646 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
74cd071d 14647 {"mdsp", no_argument, NULL, OPTION_DSP},
74cd071d 14648 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
ef2e4d86 14649 {"mmt", no_argument, NULL, OPTION_MT},
ef2e4d86 14650 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
e16bfa71 14651 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
e16bfa71 14652 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
8b082fb1 14653 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
8b082fb1 14654 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
df58fc94
RS
14655 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
14656 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
dec0624d
MR
14657 {"mmcu", no_argument, NULL, OPTION_MCU},
14658 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
b015e599
AP
14659 {"mvirt", no_argument, NULL, OPTION_VIRT},
14660 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
f9b4148d
CD
14661
14662 /* Old-style architecture options. Don't add more of these. */
f9b4148d 14663 {"m4650", no_argument, NULL, OPTION_M4650},
f9b4148d 14664 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
f9b4148d 14665 {"m4010", no_argument, NULL, OPTION_M4010},
f9b4148d 14666 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
f9b4148d 14667 {"m4100", no_argument, NULL, OPTION_M4100},
f9b4148d 14668 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
f9b4148d 14669 {"m3900", no_argument, NULL, OPTION_M3900},
f9b4148d
CD
14670 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
14671
14672 /* Options which enable bug fixes. */
f9b4148d 14673 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
f9b4148d
CD
14674 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14675 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
c67a084a
NC
14676 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
14677 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
14678 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
14679 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
d766e8ec
RS
14680 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
14681 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
7d8e00cf
RS
14682 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
14683 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
6a32d874
CM
14684 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
14685 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
d954098f
DD
14686 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
14687 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
f9b4148d
CD
14688
14689 /* Miscellaneous options. */
252b5132
RH
14690 {"trap", no_argument, NULL, OPTION_TRAP},
14691 {"no-break", no_argument, NULL, OPTION_TRAP},
252b5132
RH
14692 {"break", no_argument, NULL, OPTION_BREAK},
14693 {"no-trap", no_argument, NULL, OPTION_BREAK},
252b5132 14694 {"EB", no_argument, NULL, OPTION_EB},
252b5132 14695 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 14696 {"mfp32", no_argument, NULL, OPTION_FP32},
c97ef257 14697 {"mgp32", no_argument, NULL, OPTION_GP32},
119d663a 14698 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
119d663a 14699 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
316f5878 14700 {"mfp64", no_argument, NULL, OPTION_FP64},
ae948b86 14701 {"mgp64", no_argument, NULL, OPTION_GP64},
4a6a3df4
AO
14702 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
14703 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
aa6975fb
ILT
14704 {"mshared", no_argument, NULL, OPTION_MSHARED},
14705 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
aed1a261
RS
14706 {"msym32", no_argument, NULL, OPTION_MSYM32},
14707 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
037b32b9
AN
14708 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
14709 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
037b32b9
AN
14710 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
14711 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
23fce1e3
NC
14712
14713 /* Strictly speaking this next option is ELF specific,
14714 but we allow it for other ports as well in order to
14715 make testing easier. */
14716 {"32", no_argument, NULL, OPTION_32},
037b32b9 14717
f9b4148d 14718 /* ELF-specific options. */
156c2f8b 14719#ifdef OBJ_ELF
156c2f8b
NC
14720 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
14721 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
861fb55a 14722 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
156c2f8b
NC
14723 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
14724 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86 14725 {"mabi", required_argument, NULL, OPTION_MABI},
e013f690 14726 {"n32", no_argument, NULL, OPTION_N32},
156c2f8b 14727 {"64", no_argument, NULL, OPTION_64},
ecb4347a 14728 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
ecb4347a 14729 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe 14730 {"mpdr", no_argument, NULL, OPTION_PDR},
dcd410fe 14731 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
0a44bf69 14732 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ae948b86 14733#endif /* OBJ_ELF */
f9b4148d 14734
252b5132
RH
14735 {NULL, no_argument, NULL, 0}
14736};
156c2f8b 14737size_t md_longopts_size = sizeof (md_longopts);
252b5132 14738
316f5878
RS
14739/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14740 NEW_VALUE. Warn if another value was already specified. Note:
14741 we have to defer parsing the -march and -mtune arguments in order
14742 to handle 'from-abi' correctly, since the ABI might be specified
14743 in a later argument. */
14744
14745static void
17a2f251 14746mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14747{
14748 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14749 as_warn (_("A different %s was already specified, is now %s"),
14750 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14751 new_value);
14752
14753 *string_ptr = new_value;
14754}
14755
252b5132 14756int
17a2f251 14757md_parse_option (int c, char *arg)
252b5132
RH
14758{
14759 switch (c)
14760 {
119d663a
NC
14761 case OPTION_CONSTRUCT_FLOATS:
14762 mips_disable_float_construction = 0;
14763 break;
bdaaa2e1 14764
119d663a
NC
14765 case OPTION_NO_CONSTRUCT_FLOATS:
14766 mips_disable_float_construction = 1;
14767 break;
bdaaa2e1 14768
252b5132
RH
14769 case OPTION_TRAP:
14770 mips_trap = 1;
14771 break;
14772
14773 case OPTION_BREAK:
14774 mips_trap = 0;
14775 break;
14776
14777 case OPTION_EB:
14778 target_big_endian = 1;
14779 break;
14780
14781 case OPTION_EL:
14782 target_big_endian = 0;
14783 break;
14784
14785 case 'O':
4ffff32f
TS
14786 if (arg == NULL)
14787 mips_optimize = 1;
14788 else if (arg[0] == '0')
14789 mips_optimize = 0;
14790 else if (arg[0] == '1')
252b5132
RH
14791 mips_optimize = 1;
14792 else
14793 mips_optimize = 2;
14794 break;
14795
14796 case 'g':
14797 if (arg == NULL)
14798 mips_debug = 2;
14799 else
14800 mips_debug = atoi (arg);
252b5132
RH
14801 break;
14802
14803 case OPTION_MIPS1:
316f5878 14804 file_mips_isa = ISA_MIPS1;
252b5132
RH
14805 break;
14806
14807 case OPTION_MIPS2:
316f5878 14808 file_mips_isa = ISA_MIPS2;
252b5132
RH
14809 break;
14810
14811 case OPTION_MIPS3:
316f5878 14812 file_mips_isa = ISA_MIPS3;
252b5132
RH
14813 break;
14814
14815 case OPTION_MIPS4:
316f5878 14816 file_mips_isa = ISA_MIPS4;
e7af610e
NC
14817 break;
14818
84ea6cf2 14819 case OPTION_MIPS5:
316f5878 14820 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
14821 break;
14822
e7af610e 14823 case OPTION_MIPS32:
316f5878 14824 file_mips_isa = ISA_MIPS32;
252b5132
RH
14825 break;
14826
af7ee8bf
CD
14827 case OPTION_MIPS32R2:
14828 file_mips_isa = ISA_MIPS32R2;
14829 break;
14830
5f74bc13
CD
14831 case OPTION_MIPS64R2:
14832 file_mips_isa = ISA_MIPS64R2;
14833 break;
14834
84ea6cf2 14835 case OPTION_MIPS64:
316f5878 14836 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
14837 break;
14838
ec68c924 14839 case OPTION_MTUNE:
316f5878
RS
14840 mips_set_option_string (&mips_tune_string, arg);
14841 break;
ec68c924 14842
316f5878
RS
14843 case OPTION_MARCH:
14844 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14845 break;
14846
14847 case OPTION_M4650:
316f5878
RS
14848 mips_set_option_string (&mips_arch_string, "4650");
14849 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14850 break;
14851
14852 case OPTION_NO_M4650:
14853 break;
14854
14855 case OPTION_M4010:
316f5878
RS
14856 mips_set_option_string (&mips_arch_string, "4010");
14857 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14858 break;
14859
14860 case OPTION_NO_M4010:
14861 break;
14862
14863 case OPTION_M4100:
316f5878
RS
14864 mips_set_option_string (&mips_arch_string, "4100");
14865 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14866 break;
14867
14868 case OPTION_NO_M4100:
14869 break;
14870
252b5132 14871 case OPTION_M3900:
316f5878
RS
14872 mips_set_option_string (&mips_arch_string, "3900");
14873 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14874 break;
bdaaa2e1 14875
252b5132
RH
14876 case OPTION_NO_M3900:
14877 break;
14878
deec1734
CD
14879 case OPTION_MDMX:
14880 mips_opts.ase_mdmx = 1;
14881 break;
14882
14883 case OPTION_NO_MDMX:
14884 mips_opts.ase_mdmx = 0;
14885 break;
14886
74cd071d
CF
14887 case OPTION_DSP:
14888 mips_opts.ase_dsp = 1;
8b082fb1 14889 mips_opts.ase_dspr2 = 0;
74cd071d
CF
14890 break;
14891
14892 case OPTION_NO_DSP:
8b082fb1
TS
14893 mips_opts.ase_dsp = 0;
14894 mips_opts.ase_dspr2 = 0;
14895 break;
14896
14897 case OPTION_DSPR2:
14898 mips_opts.ase_dspr2 = 1;
14899 mips_opts.ase_dsp = 1;
14900 break;
14901
14902 case OPTION_NO_DSPR2:
14903 mips_opts.ase_dspr2 = 0;
74cd071d
CF
14904 mips_opts.ase_dsp = 0;
14905 break;
14906
ef2e4d86
CF
14907 case OPTION_MT:
14908 mips_opts.ase_mt = 1;
14909 break;
14910
14911 case OPTION_NO_MT:
14912 mips_opts.ase_mt = 0;
14913 break;
14914
dec0624d
MR
14915 case OPTION_MCU:
14916 mips_opts.ase_mcu = 1;
14917 break;
14918
14919 case OPTION_NO_MCU:
14920 mips_opts.ase_mcu = 0;
14921 break;
14922
df58fc94
RS
14923 case OPTION_MICROMIPS:
14924 if (mips_opts.mips16 == 1)
14925 {
14926 as_bad (_("-mmicromips cannot be used with -mips16"));
14927 return 0;
14928 }
14929 mips_opts.micromips = 1;
14930 mips_no_prev_insn ();
14931 break;
14932
14933 case OPTION_NO_MICROMIPS:
14934 mips_opts.micromips = 0;
14935 mips_no_prev_insn ();
14936 break;
14937
b015e599
AP
14938 case OPTION_VIRT:
14939 mips_opts.ase_virt = 1;
14940 break;
14941
14942 case OPTION_NO_VIRT:
14943 mips_opts.ase_virt = 0;
14944 break;
14945
252b5132 14946 case OPTION_MIPS16:
df58fc94
RS
14947 if (mips_opts.micromips == 1)
14948 {
14949 as_bad (_("-mips16 cannot be used with -micromips"));
14950 return 0;
14951 }
252b5132 14952 mips_opts.mips16 = 1;
7d10b47d 14953 mips_no_prev_insn ();
252b5132
RH
14954 break;
14955
14956 case OPTION_NO_MIPS16:
14957 mips_opts.mips16 = 0;
7d10b47d 14958 mips_no_prev_insn ();
252b5132
RH
14959 break;
14960
1f25f5d3
CD
14961 case OPTION_MIPS3D:
14962 mips_opts.ase_mips3d = 1;
14963 break;
14964
14965 case OPTION_NO_MIPS3D:
14966 mips_opts.ase_mips3d = 0;
14967 break;
14968
e16bfa71
TS
14969 case OPTION_SMARTMIPS:
14970 mips_opts.ase_smartmips = 1;
14971 break;
14972
14973 case OPTION_NO_SMARTMIPS:
14974 mips_opts.ase_smartmips = 0;
14975 break;
14976
6a32d874
CM
14977 case OPTION_FIX_24K:
14978 mips_fix_24k = 1;
14979 break;
14980
14981 case OPTION_NO_FIX_24K:
14982 mips_fix_24k = 0;
14983 break;
14984
c67a084a
NC
14985 case OPTION_FIX_LOONGSON2F_JUMP:
14986 mips_fix_loongson2f_jump = TRUE;
14987 break;
14988
14989 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14990 mips_fix_loongson2f_jump = FALSE;
14991 break;
14992
14993 case OPTION_FIX_LOONGSON2F_NOP:
14994 mips_fix_loongson2f_nop = TRUE;
14995 break;
14996
14997 case OPTION_NO_FIX_LOONGSON2F_NOP:
14998 mips_fix_loongson2f_nop = FALSE;
14999 break;
15000
d766e8ec
RS
15001 case OPTION_FIX_VR4120:
15002 mips_fix_vr4120 = 1;
60b63b72
RS
15003 break;
15004
d766e8ec
RS
15005 case OPTION_NO_FIX_VR4120:
15006 mips_fix_vr4120 = 0;
60b63b72
RS
15007 break;
15008
7d8e00cf
RS
15009 case OPTION_FIX_VR4130:
15010 mips_fix_vr4130 = 1;
15011 break;
15012
15013 case OPTION_NO_FIX_VR4130:
15014 mips_fix_vr4130 = 0;
15015 break;
15016
d954098f
DD
15017 case OPTION_FIX_CN63XXP1:
15018 mips_fix_cn63xxp1 = TRUE;
15019 break;
15020
15021 case OPTION_NO_FIX_CN63XXP1:
15022 mips_fix_cn63xxp1 = FALSE;
15023 break;
15024
4a6a3df4
AO
15025 case OPTION_RELAX_BRANCH:
15026 mips_relax_branch = 1;
15027 break;
15028
15029 case OPTION_NO_RELAX_BRANCH:
15030 mips_relax_branch = 0;
15031 break;
15032
aa6975fb
ILT
15033 case OPTION_MSHARED:
15034 mips_in_shared = TRUE;
15035 break;
15036
15037 case OPTION_MNO_SHARED:
15038 mips_in_shared = FALSE;
15039 break;
15040
aed1a261
RS
15041 case OPTION_MSYM32:
15042 mips_opts.sym32 = TRUE;
15043 break;
15044
15045 case OPTION_MNO_SYM32:
15046 mips_opts.sym32 = FALSE;
15047 break;
15048
0f074f60 15049#ifdef OBJ_ELF
252b5132
RH
15050 /* When generating ELF code, we permit -KPIC and -call_shared to
15051 select SVR4_PIC, and -non_shared to select no PIC. This is
15052 intended to be compatible with Irix 5. */
15053 case OPTION_CALL_SHARED:
f43abd2b 15054 if (!IS_ELF)
252b5132
RH
15055 {
15056 as_bad (_("-call_shared is supported only for ELF format"));
15057 return 0;
15058 }
15059 mips_pic = SVR4_PIC;
143d77c5 15060 mips_abicalls = TRUE;
252b5132
RH
15061 break;
15062
861fb55a
DJ
15063 case OPTION_CALL_NONPIC:
15064 if (!IS_ELF)
15065 {
15066 as_bad (_("-call_nonpic is supported only for ELF format"));
15067 return 0;
15068 }
15069 mips_pic = NO_PIC;
15070 mips_abicalls = TRUE;
15071 break;
15072
252b5132 15073 case OPTION_NON_SHARED:
f43abd2b 15074 if (!IS_ELF)
252b5132
RH
15075 {
15076 as_bad (_("-non_shared is supported only for ELF format"));
15077 return 0;
15078 }
15079 mips_pic = NO_PIC;
143d77c5 15080 mips_abicalls = FALSE;
252b5132
RH
15081 break;
15082
44075ae2
TS
15083 /* The -xgot option tells the assembler to use 32 bit offsets
15084 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
15085 compatibility. */
15086 case OPTION_XGOT:
15087 mips_big_got = 1;
15088 break;
0f074f60 15089#endif /* OBJ_ELF */
252b5132
RH
15090
15091 case 'G':
6caf9ef4
TS
15092 g_switch_value = atoi (arg);
15093 g_switch_seen = 1;
252b5132
RH
15094 break;
15095
34ba82a8
TS
15096 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15097 and -mabi=64. */
252b5132 15098 case OPTION_32:
23fce1e3
NC
15099 if (IS_ELF)
15100 mips_abi = O32_ABI;
15101 /* We silently ignore -32 for non-ELF targets. This greatly
15102 simplifies the construction of the MIPS GAS test cases. */
252b5132
RH
15103 break;
15104
23fce1e3 15105#ifdef OBJ_ELF
e013f690 15106 case OPTION_N32:
f43abd2b 15107 if (!IS_ELF)
34ba82a8
TS
15108 {
15109 as_bad (_("-n32 is supported for ELF format only"));
15110 return 0;
15111 }
316f5878 15112 mips_abi = N32_ABI;
e013f690 15113 break;
252b5132 15114
e013f690 15115 case OPTION_64:
f43abd2b 15116 if (!IS_ELF)
34ba82a8
TS
15117 {
15118 as_bad (_("-64 is supported for ELF format only"));
15119 return 0;
15120 }
316f5878 15121 mips_abi = N64_ABI;
f43abd2b 15122 if (!support_64bit_objects())
e013f690 15123 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 15124 break;
ae948b86 15125#endif /* OBJ_ELF */
252b5132 15126
c97ef257 15127 case OPTION_GP32:
a325df1d 15128 file_mips_gp32 = 1;
c97ef257
AH
15129 break;
15130
15131 case OPTION_GP64:
a325df1d 15132 file_mips_gp32 = 0;
c97ef257 15133 break;
252b5132 15134
ca4e0257 15135 case OPTION_FP32:
a325df1d 15136 file_mips_fp32 = 1;
316f5878
RS
15137 break;
15138
15139 case OPTION_FP64:
15140 file_mips_fp32 = 0;
ca4e0257
RS
15141 break;
15142
037b32b9
AN
15143 case OPTION_SINGLE_FLOAT:
15144 file_mips_single_float = 1;
15145 break;
15146
15147 case OPTION_DOUBLE_FLOAT:
15148 file_mips_single_float = 0;
15149 break;
15150
15151 case OPTION_SOFT_FLOAT:
15152 file_mips_soft_float = 1;
15153 break;
15154
15155 case OPTION_HARD_FLOAT:
15156 file_mips_soft_float = 0;
15157 break;
15158
ae948b86 15159#ifdef OBJ_ELF
252b5132 15160 case OPTION_MABI:
f43abd2b 15161 if (!IS_ELF)
34ba82a8
TS
15162 {
15163 as_bad (_("-mabi is supported for ELF format only"));
15164 return 0;
15165 }
e013f690 15166 if (strcmp (arg, "32") == 0)
316f5878 15167 mips_abi = O32_ABI;
e013f690 15168 else if (strcmp (arg, "o64") == 0)
316f5878 15169 mips_abi = O64_ABI;
e013f690 15170 else if (strcmp (arg, "n32") == 0)
316f5878 15171 mips_abi = N32_ABI;
e013f690
TS
15172 else if (strcmp (arg, "64") == 0)
15173 {
316f5878 15174 mips_abi = N64_ABI;
e013f690
TS
15175 if (! support_64bit_objects())
15176 as_fatal (_("No compiled in support for 64 bit object file "
15177 "format"));
15178 }
15179 else if (strcmp (arg, "eabi") == 0)
316f5878 15180 mips_abi = EABI_ABI;
e013f690 15181 else
da0e507f
TS
15182 {
15183 as_fatal (_("invalid abi -mabi=%s"), arg);
15184 return 0;
15185 }
252b5132 15186 break;
e013f690 15187#endif /* OBJ_ELF */
252b5132 15188
6b76fefe 15189 case OPTION_M7000_HILO_FIX:
b34976b6 15190 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
15191 break;
15192
9ee72ff1 15193 case OPTION_MNO_7000_HILO_FIX:
b34976b6 15194 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
15195 break;
15196
ecb4347a
DJ
15197#ifdef OBJ_ELF
15198 case OPTION_MDEBUG:
b34976b6 15199 mips_flag_mdebug = TRUE;
ecb4347a
DJ
15200 break;
15201
15202 case OPTION_NO_MDEBUG:
b34976b6 15203 mips_flag_mdebug = FALSE;
ecb4347a 15204 break;
dcd410fe
RO
15205
15206 case OPTION_PDR:
15207 mips_flag_pdr = TRUE;
15208 break;
15209
15210 case OPTION_NO_PDR:
15211 mips_flag_pdr = FALSE;
15212 break;
0a44bf69
RS
15213
15214 case OPTION_MVXWORKS_PIC:
15215 mips_pic = VXWORKS_PIC;
15216 break;
ecb4347a
DJ
15217#endif /* OBJ_ELF */
15218
252b5132
RH
15219 default:
15220 return 0;
15221 }
15222
c67a084a
NC
15223 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15224
252b5132
RH
15225 return 1;
15226}
316f5878
RS
15227\f
15228/* Set up globals to generate code for the ISA or processor
15229 described by INFO. */
252b5132 15230
252b5132 15231static void
17a2f251 15232mips_set_architecture (const struct mips_cpu_info *info)
252b5132 15233{
316f5878 15234 if (info != 0)
252b5132 15235 {
fef14a42
TS
15236 file_mips_arch = info->cpu;
15237 mips_opts.arch = info->cpu;
316f5878 15238 mips_opts.isa = info->isa;
252b5132 15239 }
252b5132
RH
15240}
15241
252b5132 15242
316f5878 15243/* Likewise for tuning. */
252b5132 15244
316f5878 15245static void
17a2f251 15246mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
15247{
15248 if (info != 0)
fef14a42 15249 mips_tune = info->cpu;
316f5878 15250}
80cc45a5 15251
34ba82a8 15252
252b5132 15253void
17a2f251 15254mips_after_parse_args (void)
e9670677 15255{
fef14a42
TS
15256 const struct mips_cpu_info *arch_info = 0;
15257 const struct mips_cpu_info *tune_info = 0;
15258
e9670677 15259 /* GP relative stuff not working for PE */
6caf9ef4 15260 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 15261 {
6caf9ef4 15262 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
15263 as_bad (_("-G not supported in this configuration."));
15264 g_switch_value = 0;
15265 }
15266
cac012d6
AO
15267 if (mips_abi == NO_ABI)
15268 mips_abi = MIPS_DEFAULT_ABI;
15269
22923709
RS
15270 /* The following code determines the architecture and register size.
15271 Similar code was added to GCC 3.3 (see override_options() in
15272 config/mips/mips.c). The GAS and GCC code should be kept in sync
15273 as much as possible. */
e9670677 15274
316f5878 15275 if (mips_arch_string != 0)
fef14a42 15276 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 15277
316f5878 15278 if (file_mips_isa != ISA_UNKNOWN)
e9670677 15279 {
316f5878 15280 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 15281 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 15282 the -march selection (if any). */
fef14a42 15283 if (arch_info != 0)
e9670677 15284 {
316f5878
RS
15285 /* -march takes precedence over -mipsN, since it is more descriptive.
15286 There's no harm in specifying both as long as the ISA levels
15287 are the same. */
fef14a42 15288 if (file_mips_isa != arch_info->isa)
316f5878
RS
15289 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15290 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 15291 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15292 }
316f5878 15293 else
fef14a42 15294 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
15295 }
15296
fef14a42 15297 if (arch_info == 0)
95bfe26e
MF
15298 {
15299 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15300 gas_assert (arch_info);
15301 }
e9670677 15302
fef14a42 15303 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15304 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15305 arch_info->name);
15306
15307 mips_set_architecture (arch_info);
15308
15309 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
15310 if (mips_tune_string != 0)
15311 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15312
fef14a42
TS
15313 if (tune_info == 0)
15314 mips_set_tune (arch_info);
15315 else
15316 mips_set_tune (tune_info);
e9670677 15317
316f5878 15318 if (file_mips_gp32 >= 0)
e9670677 15319 {
316f5878
RS
15320 /* The user specified the size of the integer registers. Make sure
15321 it agrees with the ABI and ISA. */
15322 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15323 as_bad (_("-mgp64 used with a 32-bit processor"));
15324 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15325 as_bad (_("-mgp32 used with a 64-bit ABI"));
15326 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15327 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
15328 }
15329 else
15330 {
316f5878
RS
15331 /* Infer the integer register size from the ABI and processor.
15332 Restrict ourselves to 32-bit registers if that's all the
15333 processor has, or if the ABI cannot handle 64-bit registers. */
15334 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15335 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
15336 }
15337
ad3fea08
TS
15338 switch (file_mips_fp32)
15339 {
15340 default:
15341 case -1:
15342 /* No user specified float register size.
15343 ??? GAS treats single-float processors as though they had 64-bit
15344 float registers (although it complains when double-precision
15345 instructions are used). As things stand, saying they have 32-bit
15346 registers would lead to spurious "register must be even" messages.
15347 So here we assume float registers are never smaller than the
15348 integer ones. */
15349 if (file_mips_gp32 == 0)
15350 /* 64-bit integer registers implies 64-bit float registers. */
15351 file_mips_fp32 = 0;
15352 else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
15353 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15354 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
15355 file_mips_fp32 = 0;
15356 else
15357 /* 32-bit float registers. */
15358 file_mips_fp32 = 1;
15359 break;
15360
15361 /* The user specified the size of the float registers. Check if it
15362 agrees with the ABI and ISA. */
15363 case 0:
15364 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15365 as_bad (_("-mfp64 used with a 32-bit fpu"));
15366 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15367 && !ISA_HAS_MXHC1 (mips_opts.isa))
15368 as_warn (_("-mfp64 used with a 32-bit ABI"));
15369 break;
15370 case 1:
15371 if (ABI_NEEDS_64BIT_REGS (mips_abi))
15372 as_warn (_("-mfp32 used with a 64-bit ABI"));
15373 break;
15374 }
e9670677 15375
316f5878 15376 /* End of GCC-shared inference code. */
e9670677 15377
17a2f251
TS
15378 /* This flag is set when we have a 64-bit capable CPU but use only
15379 32-bit wide registers. Note that EABI does not use it. */
15380 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15381 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15382 || mips_abi == O32_ABI))
316f5878 15383 mips_32bitmode = 1;
e9670677
MR
15384
15385 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15386 as_bad (_("trap exception not supported at ISA 1"));
15387
e9670677
MR
15388 /* If the selected architecture includes support for ASEs, enable
15389 generation of code for them. */
a4672219 15390 if (mips_opts.mips16 == -1)
fef14a42 15391 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
df58fc94
RS
15392 if (mips_opts.micromips == -1)
15393 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
ffdefa66 15394 if (mips_opts.ase_mips3d == -1)
65263ce3 15395 mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
ad3fea08
TS
15396 && file_mips_fp32 == 0) ? 1 : 0;
15397 if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
15398 as_bad (_("-mfp32 used with -mips3d"));
15399
ffdefa66 15400 if (mips_opts.ase_mdmx == -1)
65263ce3 15401 mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
ad3fea08
TS
15402 && file_mips_fp32 == 0) ? 1 : 0;
15403 if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
15404 as_bad (_("-mfp32 used with -mdmx"));
15405
15406 if (mips_opts.ase_smartmips == -1)
15407 mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
15408 if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
20203fb9
NC
15409 as_warn (_("%s ISA does not support SmartMIPS"),
15410 mips_cpu_info_from_isa (mips_opts.isa)->name);
ad3fea08 15411
74cd071d 15412 if (mips_opts.ase_dsp == -1)
ad3fea08
TS
15413 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15414 if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
20203fb9
NC
15415 as_warn (_("%s ISA does not support DSP ASE"),
15416 mips_cpu_info_from_isa (mips_opts.isa)->name);
ad3fea08 15417
8b082fb1
TS
15418 if (mips_opts.ase_dspr2 == -1)
15419 {
15420 mips_opts.ase_dspr2 = (arch_info->flags & MIPS_CPU_ASE_DSPR2) ? 1 : 0;
15421 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15422 }
15423 if (mips_opts.ase_dspr2 && !ISA_SUPPORTS_DSPR2_ASE)
20203fb9
NC
15424 as_warn (_("%s ISA does not support DSP R2 ASE"),
15425 mips_cpu_info_from_isa (mips_opts.isa)->name);
8b082fb1 15426
ef2e4d86 15427 if (mips_opts.ase_mt == -1)
ad3fea08
TS
15428 mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
15429 if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
20203fb9
NC
15430 as_warn (_("%s ISA does not support MT ASE"),
15431 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 15432
dec0624d
MR
15433 if (mips_opts.ase_mcu == -1)
15434 mips_opts.ase_mcu = (arch_info->flags & MIPS_CPU_ASE_MCU) ? 1 : 0;
15435 if (mips_opts.ase_mcu && !ISA_SUPPORTS_MCU_ASE)
15436 as_warn (_("%s ISA does not support MCU ASE"),
15437 mips_cpu_info_from_isa (mips_opts.isa)->name);
15438
b015e599
AP
15439 if (mips_opts.ase_virt == -1)
15440 mips_opts.ase_virt = (arch_info->flags & MIPS_CPU_ASE_VIRT) ? 1 : 0;
15441 if (mips_opts.ase_virt && !ISA_SUPPORTS_VIRT_ASE)
15442 as_warn (_("%s ISA does not support Virtualization ASE"),
15443 mips_cpu_info_from_isa (mips_opts.isa)->name);
15444
e9670677 15445 file_mips_isa = mips_opts.isa;
e9670677
MR
15446 file_ase_mips3d = mips_opts.ase_mips3d;
15447 file_ase_mdmx = mips_opts.ase_mdmx;
e16bfa71 15448 file_ase_smartmips = mips_opts.ase_smartmips;
74cd071d 15449 file_ase_dsp = mips_opts.ase_dsp;
8b082fb1 15450 file_ase_dspr2 = mips_opts.ase_dspr2;
ef2e4d86 15451 file_ase_mt = mips_opts.ase_mt;
b015e599 15452 file_ase_virt = mips_opts.ase_virt;
e9670677
MR
15453 mips_opts.gp32 = file_mips_gp32;
15454 mips_opts.fp32 = file_mips_fp32;
037b32b9
AN
15455 mips_opts.soft_float = file_mips_soft_float;
15456 mips_opts.single_float = file_mips_single_float;
e9670677 15457
ecb4347a
DJ
15458 if (mips_flag_mdebug < 0)
15459 {
15460#ifdef OBJ_MAYBE_ECOFF
15461 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
15462 mips_flag_mdebug = 1;
15463 else
15464#endif /* OBJ_MAYBE_ECOFF */
15465 mips_flag_mdebug = 0;
15466 }
e9670677
MR
15467}
15468\f
15469void
17a2f251 15470mips_init_after_args (void)
252b5132
RH
15471{
15472 /* initialize opcodes */
15473 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15474 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15475}
15476
15477long
17a2f251 15478md_pcrel_from (fixS *fixP)
252b5132 15479{
a7ebbfdf
TS
15480 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15481 switch (fixP->fx_r_type)
15482 {
df58fc94
RS
15483 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15484 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15485 /* Return the address of the delay slot. */
15486 return addr + 2;
15487
15488 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15489 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf
TS
15490 case BFD_RELOC_16_PCREL_S2:
15491 case BFD_RELOC_MIPS_JMP:
15492 /* Return the address of the delay slot. */
15493 return addr + 4;
df58fc94 15494
b47468a6
CM
15495 case BFD_RELOC_32_PCREL:
15496 return addr;
15497
a7ebbfdf 15498 default:
58ea3d6a 15499 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
15500 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15501 as_bad_where (fixP->fx_file, fixP->fx_line,
15502 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
15503 return addr;
15504 }
252b5132
RH
15505}
15506
252b5132
RH
15507/* This is called before the symbol table is processed. In order to
15508 work with gcc when using mips-tfile, we must keep all local labels.
15509 However, in other cases, we want to discard them. If we were
15510 called with -g, but we didn't see any debugging information, it may
15511 mean that gcc is smuggling debugging information through to
15512 mips-tfile, in which case we must generate all local labels. */
15513
15514void
17a2f251 15515mips_frob_file_before_adjust (void)
252b5132
RH
15516{
15517#ifndef NO_ECOFF_DEBUGGING
15518 if (ECOFF_DEBUGGING
15519 && mips_debug != 0
15520 && ! ecoff_debugging_seen)
15521 flag_keep_locals = 1;
15522#endif
15523}
15524
3b91255e 15525/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15526 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15527 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15528 relocation operators.
15529
15530 For our purposes, a %lo() expression matches a %got() or %hi()
15531 expression if:
15532
15533 (a) it refers to the same symbol; and
15534 (b) the offset applied in the %lo() expression is no lower than
15535 the offset applied in the %got() or %hi().
15536
15537 (b) allows us to cope with code like:
15538
15539 lui $4,%hi(foo)
15540 lh $4,%lo(foo+2)($4)
15541
15542 ...which is legal on RELA targets, and has a well-defined behaviour
15543 if the user knows that adding 2 to "foo" will not induce a carry to
15544 the high 16 bits.
15545
15546 When several %lo()s match a particular %got() or %hi(), we use the
15547 following rules to distinguish them:
15548
15549 (1) %lo()s with smaller offsets are a better match than %lo()s with
15550 higher offsets.
15551
15552 (2) %lo()s with no matching %got() or %hi() are better than those
15553 that already have a matching %got() or %hi().
15554
15555 (3) later %lo()s are better than earlier %lo()s.
15556
15557 These rules are applied in order.
15558
15559 (1) means, among other things, that %lo()s with identical offsets are
15560 chosen if they exist.
15561
15562 (2) means that we won't associate several high-part relocations with
15563 the same low-part relocation unless there's no alternative. Having
15564 several high parts for the same low part is a GNU extension; this rule
15565 allows careful users to avoid it.
15566
15567 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15568 with the last high-part relocation being at the front of the list.
15569 It therefore makes sense to choose the last matching low-part
15570 relocation, all other things being equal. It's also easier
15571 to code that way. */
252b5132
RH
15572
15573void
17a2f251 15574mips_frob_file (void)
252b5132
RH
15575{
15576 struct mips_hi_fixup *l;
35903be0 15577 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15578
15579 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15580 {
15581 segment_info_type *seginfo;
3b91255e
RS
15582 bfd_boolean matched_lo_p;
15583 fixS **hi_pos, **lo_pos, **pos;
252b5132 15584
9c2799c2 15585 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15586
5919d012 15587 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15588 there isn't supposed to be a matching LO. Ignore %gots against
15589 constants; we'll report an error for those later. */
738e5348 15590 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab
RS
15591 && !(l->fixp->fx_addsy
15592 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
5919d012
RS
15593 continue;
15594
15595 /* Check quickly whether the next fixup happens to be a matching %lo. */
15596 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15597 continue;
15598
252b5132 15599 seginfo = seg_info (l->seg);
252b5132 15600
3b91255e
RS
15601 /* Set HI_POS to the position of this relocation in the chain.
15602 Set LO_POS to the position of the chosen low-part relocation.
15603 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15604 relocation that matches an immediately-preceding high-part
15605 relocation. */
15606 hi_pos = NULL;
15607 lo_pos = NULL;
15608 matched_lo_p = FALSE;
738e5348 15609 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15610
3b91255e
RS
15611 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15612 {
15613 if (*pos == l->fixp)
15614 hi_pos = pos;
15615
35903be0 15616 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15617 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15618 && (*pos)->fx_offset >= l->fixp->fx_offset
15619 && (lo_pos == NULL
15620 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15621 || (!matched_lo_p
15622 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15623 lo_pos = pos;
15624
15625 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15626 && fixup_has_matching_lo_p (*pos));
15627 }
15628
15629 /* If we found a match, remove the high-part relocation from its
15630 current position and insert it before the low-part relocation.
15631 Make the offsets match so that fixup_has_matching_lo_p()
15632 will return true.
15633
15634 We don't warn about unmatched high-part relocations since some
15635 versions of gcc have been known to emit dead "lui ...%hi(...)"
15636 instructions. */
15637 if (lo_pos != NULL)
15638 {
15639 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15640 if (l->fixp->fx_next != *lo_pos)
252b5132 15641 {
3b91255e
RS
15642 *hi_pos = l->fixp->fx_next;
15643 l->fixp->fx_next = *lo_pos;
15644 *lo_pos = l->fixp;
252b5132 15645 }
252b5132
RH
15646 }
15647 }
15648}
15649
252b5132 15650int
17a2f251 15651mips_force_relocation (fixS *fixp)
252b5132 15652{
ae6063d4 15653 if (generic_force_reloc (fixp))
252b5132
RH
15654 return 1;
15655
df58fc94
RS
15656 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15657 so that the linker relaxation can update targets. */
15658 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15659 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15660 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15661 return 1;
15662
3e722fb5 15663 return 0;
252b5132
RH
15664}
15665
b886a2ab
RS
15666/* Read the instruction associated with RELOC from BUF. */
15667
15668static unsigned int
15669read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15670{
15671 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15672 return read_compressed_insn (buf, 4);
15673 else
15674 return read_insn (buf);
15675}
15676
15677/* Write instruction INSN to BUF, given that it has been relocated
15678 by RELOC. */
15679
15680static void
15681write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15682 unsigned long insn)
15683{
15684 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15685 write_compressed_insn (buf, insn, 4);
15686 else
15687 write_insn (buf, insn);
15688}
15689
252b5132
RH
15690/* Apply a fixup to the object file. */
15691
94f592af 15692void
55cf6793 15693md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15694{
4d68580a 15695 char *buf;
b886a2ab 15696 unsigned long insn;
a7ebbfdf 15697 reloc_howto_type *howto;
252b5132 15698
a7ebbfdf
TS
15699 /* We ignore generic BFD relocations we don't know about. */
15700 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15701 if (! howto)
15702 return;
65551fa4 15703
df58fc94
RS
15704 gas_assert (fixP->fx_size == 2
15705 || fixP->fx_size == 4
90ecf173
MR
15706 || fixP->fx_r_type == BFD_RELOC_16
15707 || fixP->fx_r_type == BFD_RELOC_64
15708 || fixP->fx_r_type == BFD_RELOC_CTOR
15709 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15710 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15711 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15712 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15713 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 15714
4d68580a 15715 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15716
df58fc94
RS
15717 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15718 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15719 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
15720 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
15721 || fixP->fx_r_type == BFD_RELOC_32_PCREL);
b1dca8ee
RS
15722
15723 /* Don't treat parts of a composite relocation as done. There are two
15724 reasons for this:
15725
15726 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15727 should nevertheless be emitted if the first part is.
15728
15729 (2) In normal usage, composite relocations are never assembly-time
15730 constants. The easiest way of dealing with the pathological
15731 exceptions is to generate a relocation against STN_UNDEF and
15732 leave everything up to the linker. */
3994f87e 15733 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15734 fixP->fx_done = 1;
15735
15736 switch (fixP->fx_r_type)
15737 {
3f98094e
DJ
15738 case BFD_RELOC_MIPS_TLS_GD:
15739 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15740 case BFD_RELOC_MIPS_TLS_DTPREL32:
15741 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15742 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15743 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15744 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15745 case BFD_RELOC_MIPS_TLS_TPREL32:
15746 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15747 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15748 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15749 case BFD_RELOC_MICROMIPS_TLS_GD:
15750 case BFD_RELOC_MICROMIPS_TLS_LDM:
15751 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15752 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15753 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15754 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15755 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15756 case BFD_RELOC_MIPS16_TLS_GD:
15757 case BFD_RELOC_MIPS16_TLS_LDM:
15758 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15759 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15760 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15761 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15762 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
b886a2ab
RS
15763 if (!fixP->fx_addsy)
15764 {
15765 as_bad_where (fixP->fx_file, fixP->fx_line,
15766 _("TLS relocation against a constant"));
15767 break;
15768 }
3f98094e
DJ
15769 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15770 /* fall through */
15771
252b5132 15772 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
15773 case BFD_RELOC_MIPS_SHIFT5:
15774 case BFD_RELOC_MIPS_SHIFT6:
15775 case BFD_RELOC_MIPS_GOT_DISP:
15776 case BFD_RELOC_MIPS_GOT_PAGE:
15777 case BFD_RELOC_MIPS_GOT_OFST:
15778 case BFD_RELOC_MIPS_SUB:
15779 case BFD_RELOC_MIPS_INSERT_A:
15780 case BFD_RELOC_MIPS_INSERT_B:
15781 case BFD_RELOC_MIPS_DELETE:
15782 case BFD_RELOC_MIPS_HIGHEST:
15783 case BFD_RELOC_MIPS_HIGHER:
15784 case BFD_RELOC_MIPS_SCN_DISP:
15785 case BFD_RELOC_MIPS_REL16:
15786 case BFD_RELOC_MIPS_RELGOT:
15787 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15788 case BFD_RELOC_HI16:
15789 case BFD_RELOC_HI16_S:
b886a2ab 15790 case BFD_RELOC_LO16:
cdf6fd85 15791 case BFD_RELOC_GPREL16:
252b5132
RH
15792 case BFD_RELOC_MIPS_LITERAL:
15793 case BFD_RELOC_MIPS_CALL16:
15794 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15795 case BFD_RELOC_GPREL32:
252b5132
RH
15796 case BFD_RELOC_MIPS_GOT_HI16:
15797 case BFD_RELOC_MIPS_GOT_LO16:
15798 case BFD_RELOC_MIPS_CALL_HI16:
15799 case BFD_RELOC_MIPS_CALL_LO16:
15800 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15801 case BFD_RELOC_MIPS16_GOT16:
15802 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15803 case BFD_RELOC_MIPS16_HI16:
15804 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15805 case BFD_RELOC_MIPS16_LO16:
252b5132 15806 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
15807 case BFD_RELOC_MICROMIPS_JMP:
15808 case BFD_RELOC_MICROMIPS_GOT_DISP:
15809 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15810 case BFD_RELOC_MICROMIPS_GOT_OFST:
15811 case BFD_RELOC_MICROMIPS_SUB:
15812 case BFD_RELOC_MICROMIPS_HIGHEST:
15813 case BFD_RELOC_MICROMIPS_HIGHER:
15814 case BFD_RELOC_MICROMIPS_SCN_DISP:
15815 case BFD_RELOC_MICROMIPS_JALR:
15816 case BFD_RELOC_MICROMIPS_HI16:
15817 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15818 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15819 case BFD_RELOC_MICROMIPS_GPREL16:
15820 case BFD_RELOC_MICROMIPS_LITERAL:
15821 case BFD_RELOC_MICROMIPS_CALL16:
15822 case BFD_RELOC_MICROMIPS_GOT16:
15823 case BFD_RELOC_MICROMIPS_GOT_HI16:
15824 case BFD_RELOC_MICROMIPS_GOT_LO16:
15825 case BFD_RELOC_MICROMIPS_CALL_HI16:
15826 case BFD_RELOC_MICROMIPS_CALL_LO16:
b886a2ab
RS
15827 if (fixP->fx_done)
15828 {
15829 offsetT value;
15830
15831 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15832 {
15833 insn = read_reloc_insn (buf, fixP->fx_r_type);
15834 if (mips16_reloc_p (fixP->fx_r_type))
15835 insn |= mips16_immed_extend (value, 16);
15836 else
15837 insn |= (value & 0xffff);
15838 write_reloc_insn (buf, fixP->fx_r_type, insn);
15839 }
15840 else
15841 as_bad_where (fixP->fx_file, fixP->fx_line,
15842 _("Unsupported constant in relocation"));
15843 }
252b5132
RH
15844 break;
15845
252b5132
RH
15846 case BFD_RELOC_64:
15847 /* This is handled like BFD_RELOC_32, but we output a sign
15848 extended value if we are only 32 bits. */
3e722fb5 15849 if (fixP->fx_done)
252b5132
RH
15850 {
15851 if (8 <= sizeof (valueT))
4d68580a 15852 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15853 else
15854 {
a7ebbfdf 15855 valueT hiv;
252b5132 15856
a7ebbfdf 15857 if ((*valP & 0x80000000) != 0)
252b5132
RH
15858 hiv = 0xffffffff;
15859 else
15860 hiv = 0;
4d68580a
RS
15861 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15862 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15863 }
15864 }
15865 break;
15866
056350c6 15867 case BFD_RELOC_RVA:
252b5132 15868 case BFD_RELOC_32:
b47468a6 15869 case BFD_RELOC_32_PCREL:
252b5132
RH
15870 case BFD_RELOC_16:
15871 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15872 value now. This can happen if we have a .word which is not
15873 resolved when it appears but is later defined. */
252b5132 15874 if (fixP->fx_done)
4d68580a 15875 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15876 break;
15877
252b5132 15878 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 15879 if ((*valP & 0x3) != 0)
cb56d3d3 15880 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 15881 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 15882
54f4ddb3
TS
15883 /* We need to save the bits in the instruction since fixup_segment()
15884 might be deleting the relocation entry (i.e., a branch within
15885 the current segment). */
a7ebbfdf 15886 if (! fixP->fx_done)
bb2d6cd7 15887 break;
252b5132 15888
54f4ddb3 15889 /* Update old instruction data. */
4d68580a 15890 insn = read_insn (buf);
252b5132 15891
a7ebbfdf
TS
15892 if (*valP + 0x20000 <= 0x3ffff)
15893 {
15894 insn |= (*valP >> 2) & 0xffff;
4d68580a 15895 write_insn (buf, insn);
a7ebbfdf
TS
15896 }
15897 else if (mips_pic == NO_PIC
15898 && fixP->fx_done
15899 && fixP->fx_frag->fr_address >= text_section->vma
15900 && (fixP->fx_frag->fr_address
587aac4e 15901 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15902 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15903 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15904 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15905 {
15906 /* The branch offset is too large. If this is an
15907 unconditional branch, and we are not generating PIC code,
15908 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15909 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15910 insn = 0x0c000000; /* jal */
252b5132 15911 else
a7ebbfdf
TS
15912 insn = 0x08000000; /* j */
15913 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15914 fixP->fx_done = 0;
15915 fixP->fx_addsy = section_symbol (text_section);
15916 *valP += md_pcrel_from (fixP);
4d68580a 15917 write_insn (buf, insn);
a7ebbfdf
TS
15918 }
15919 else
15920 {
15921 /* If we got here, we have branch-relaxation disabled,
15922 and there's nothing we can do to fix this instruction
15923 without turning it into a longer sequence. */
15924 as_bad_where (fixP->fx_file, fixP->fx_line,
15925 _("Branch out of range"));
252b5132 15926 }
252b5132
RH
15927 break;
15928
df58fc94
RS
15929 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15930 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15931 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15932 /* We adjust the offset back to even. */
15933 if ((*valP & 0x1) != 0)
15934 --(*valP);
15935
15936 if (! fixP->fx_done)
15937 break;
15938
15939 /* Should never visit here, because we keep the relocation. */
15940 abort ();
15941 break;
15942
252b5132
RH
15943 case BFD_RELOC_VTABLE_INHERIT:
15944 fixP->fx_done = 0;
15945 if (fixP->fx_addsy
15946 && !S_IS_DEFINED (fixP->fx_addsy)
15947 && !S_IS_WEAK (fixP->fx_addsy))
15948 S_SET_WEAK (fixP->fx_addsy);
15949 break;
15950
15951 case BFD_RELOC_VTABLE_ENTRY:
15952 fixP->fx_done = 0;
15953 break;
15954
15955 default:
b37df7c4 15956 abort ();
252b5132 15957 }
a7ebbfdf
TS
15958
15959 /* Remember value for tc_gen_reloc. */
15960 fixP->fx_addnumber = *valP;
252b5132
RH
15961}
15962
252b5132 15963static symbolS *
17a2f251 15964get_symbol (void)
252b5132
RH
15965{
15966 int c;
15967 char *name;
15968 symbolS *p;
15969
15970 name = input_line_pointer;
15971 c = get_symbol_end ();
15972 p = (symbolS *) symbol_find_or_make (name);
15973 *input_line_pointer = c;
15974 return p;
15975}
15976
742a56fe
RS
15977/* Align the current frag to a given power of two. If a particular
15978 fill byte should be used, FILL points to an integer that contains
15979 that byte, otherwise FILL is null.
15980
462427c4
RS
15981 This function used to have the comment:
15982
15983 The MIPS assembler also automatically adjusts any preceding label.
15984
15985 The implementation therefore applied the adjustment to a maximum of
15986 one label. However, other label adjustments are applied to batches
15987 of labels, and adjusting just one caused problems when new labels
15988 were added for the sake of debugging or unwind information.
15989 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
15990
15991static void
462427c4 15992mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 15993{
7d10b47d 15994 mips_emit_delays ();
df58fc94 15995 mips_record_compressed_mode ();
742a56fe
RS
15996 if (fill == NULL && subseg_text_p (now_seg))
15997 frag_align_code (to, 0);
15998 else
15999 frag_align (to, fill ? *fill : 0, 0);
252b5132 16000 record_alignment (now_seg, to);
462427c4 16001 mips_move_labels (labels, FALSE);
252b5132
RH
16002}
16003
16004/* Align to a given power of two. .align 0 turns off the automatic
16005 alignment used by the data creating pseudo-ops. */
16006
16007static void
17a2f251 16008s_align (int x ATTRIBUTE_UNUSED)
252b5132 16009{
742a56fe 16010 int temp, fill_value, *fill_ptr;
49954fb4 16011 long max_alignment = 28;
252b5132 16012
54f4ddb3 16013 /* o Note that the assembler pulls down any immediately preceding label
252b5132 16014 to the aligned address.
54f4ddb3 16015 o It's not documented but auto alignment is reinstated by
252b5132 16016 a .align pseudo instruction.
54f4ddb3 16017 o Note also that after auto alignment is turned off the mips assembler
252b5132 16018 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 16019 We don't. */
252b5132
RH
16020
16021 temp = get_absolute_expression ();
16022 if (temp > max_alignment)
16023 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
16024 else if (temp < 0)
16025 {
16026 as_warn (_("Alignment negative: 0 assumed."));
16027 temp = 0;
16028 }
16029 if (*input_line_pointer == ',')
16030 {
f9419b05 16031 ++input_line_pointer;
742a56fe
RS
16032 fill_value = get_absolute_expression ();
16033 fill_ptr = &fill_value;
252b5132
RH
16034 }
16035 else
742a56fe 16036 fill_ptr = 0;
252b5132
RH
16037 if (temp)
16038 {
a8dbcb85
TS
16039 segment_info_type *si = seg_info (now_seg);
16040 struct insn_label_list *l = si->label_list;
54f4ddb3 16041 /* Auto alignment should be switched on by next section change. */
252b5132 16042 auto_align = 1;
462427c4 16043 mips_align (temp, fill_ptr, l);
252b5132
RH
16044 }
16045 else
16046 {
16047 auto_align = 0;
16048 }
16049
16050 demand_empty_rest_of_line ();
16051}
16052
252b5132 16053static void
17a2f251 16054s_change_sec (int sec)
252b5132
RH
16055{
16056 segT seg;
16057
252b5132
RH
16058#ifdef OBJ_ELF
16059 /* The ELF backend needs to know that we are changing sections, so
16060 that .previous works correctly. We could do something like check
b6ff326e 16061 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
16062 as it would not be appropriate to use it in the section changing
16063 functions in read.c, since obj-elf.c intercepts those. FIXME:
16064 This should be cleaner, somehow. */
f43abd2b
TS
16065 if (IS_ELF)
16066 obj_elf_section_change_hook ();
252b5132
RH
16067#endif
16068
7d10b47d 16069 mips_emit_delays ();
6a32d874 16070
252b5132
RH
16071 switch (sec)
16072 {
16073 case 't':
16074 s_text (0);
16075 break;
16076 case 'd':
16077 s_data (0);
16078 break;
16079 case 'b':
16080 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16081 demand_empty_rest_of_line ();
16082 break;
16083
16084 case 'r':
4d0d148d
TS
16085 seg = subseg_new (RDATA_SECTION_NAME,
16086 (subsegT) get_absolute_expression ());
f43abd2b 16087 if (IS_ELF)
252b5132 16088 {
4d0d148d
TS
16089 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16090 | SEC_READONLY | SEC_RELOC
16091 | SEC_DATA));
c41e87e3 16092 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 16093 record_alignment (seg, 4);
252b5132 16094 }
4d0d148d 16095 demand_empty_rest_of_line ();
252b5132
RH
16096 break;
16097
16098 case 's':
4d0d148d 16099 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f43abd2b 16100 if (IS_ELF)
252b5132 16101 {
4d0d148d
TS
16102 bfd_set_section_flags (stdoutput, seg,
16103 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
c41e87e3 16104 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 16105 record_alignment (seg, 4);
252b5132 16106 }
4d0d148d
TS
16107 demand_empty_rest_of_line ();
16108 break;
998b3c36
MR
16109
16110 case 'B':
16111 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16112 if (IS_ELF)
16113 {
16114 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16115 if (strncmp (TARGET_OS, "elf", 3) != 0)
16116 record_alignment (seg, 4);
16117 }
16118 demand_empty_rest_of_line ();
16119 break;
252b5132
RH
16120 }
16121
16122 auto_align = 1;
16123}
b34976b6 16124
cca86cc8 16125void
17a2f251 16126s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16127{
7ed4a06a 16128#ifdef OBJ_ELF
cca86cc8
SC
16129 char *section_name;
16130 char c;
684022ea 16131 char next_c = 0;
cca86cc8
SC
16132 int section_type;
16133 int section_flag;
16134 int section_entry_size;
16135 int section_alignment;
b34976b6 16136
f43abd2b 16137 if (!IS_ELF)
7ed4a06a
TS
16138 return;
16139
cca86cc8
SC
16140 section_name = input_line_pointer;
16141 c = get_symbol_end ();
a816d1ed
AO
16142 if (c)
16143 next_c = *(input_line_pointer + 1);
cca86cc8 16144
4cf0dd0d
TS
16145 /* Do we have .section Name<,"flags">? */
16146 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16147 {
4cf0dd0d
TS
16148 /* just after name is now '\0'. */
16149 *input_line_pointer = c;
cca86cc8
SC
16150 input_line_pointer = section_name;
16151 obj_elf_section (ignore);
16152 return;
16153 }
16154 input_line_pointer++;
16155
16156 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16157 if (c == ',')
16158 section_type = get_absolute_expression ();
16159 else
16160 section_type = 0;
16161 if (*input_line_pointer++ == ',')
16162 section_flag = get_absolute_expression ();
16163 else
16164 section_flag = 0;
16165 if (*input_line_pointer++ == ',')
16166 section_entry_size = get_absolute_expression ();
16167 else
16168 section_entry_size = 0;
16169 if (*input_line_pointer++ == ',')
16170 section_alignment = get_absolute_expression ();
16171 else
16172 section_alignment = 0;
87975d2a
AM
16173 /* FIXME: really ignore? */
16174 (void) section_alignment;
cca86cc8 16175
a816d1ed
AO
16176 section_name = xstrdup (section_name);
16177
8ab8a5c8
RS
16178 /* When using the generic form of .section (as implemented by obj-elf.c),
16179 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16180 traditionally had to fall back on the more common @progbits instead.
16181
16182 There's nothing really harmful in this, since bfd will correct
16183 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16184 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16185 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16186
16187 Even so, we shouldn't force users of the MIPS .section syntax to
16188 incorrectly label the sections as SHT_PROGBITS. The best compromise
16189 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16190 generic type-checking code. */
16191 if (section_type == SHT_MIPS_DWARF)
16192 section_type = SHT_PROGBITS;
16193
cca86cc8
SC
16194 obj_elf_change_section (section_name, section_type, section_flag,
16195 section_entry_size, 0, 0, 0);
a816d1ed
AO
16196
16197 if (now_seg->name != section_name)
16198 free (section_name);
7ed4a06a 16199#endif /* OBJ_ELF */
cca86cc8 16200}
252b5132
RH
16201
16202void
17a2f251 16203mips_enable_auto_align (void)
252b5132
RH
16204{
16205 auto_align = 1;
16206}
16207
16208static void
17a2f251 16209s_cons (int log_size)
252b5132 16210{
a8dbcb85
TS
16211 segment_info_type *si = seg_info (now_seg);
16212 struct insn_label_list *l = si->label_list;
252b5132 16213
7d10b47d 16214 mips_emit_delays ();
252b5132 16215 if (log_size > 0 && auto_align)
462427c4 16216 mips_align (log_size, 0, l);
252b5132 16217 cons (1 << log_size);
a1facbec 16218 mips_clear_insn_labels ();
252b5132
RH
16219}
16220
16221static void
17a2f251 16222s_float_cons (int type)
252b5132 16223{
a8dbcb85
TS
16224 segment_info_type *si = seg_info (now_seg);
16225 struct insn_label_list *l = si->label_list;
252b5132 16226
7d10b47d 16227 mips_emit_delays ();
252b5132
RH
16228
16229 if (auto_align)
49309057
ILT
16230 {
16231 if (type == 'd')
462427c4 16232 mips_align (3, 0, l);
49309057 16233 else
462427c4 16234 mips_align (2, 0, l);
49309057 16235 }
252b5132 16236
252b5132 16237 float_cons (type);
a1facbec 16238 mips_clear_insn_labels ();
252b5132
RH
16239}
16240
16241/* Handle .globl. We need to override it because on Irix 5 you are
16242 permitted to say
16243 .globl foo .text
16244 where foo is an undefined symbol, to mean that foo should be
16245 considered to be the address of a function. */
16246
16247static void
17a2f251 16248s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16249{
16250 char *name;
16251 int c;
16252 symbolS *symbolP;
16253 flagword flag;
16254
8a06b769 16255 do
252b5132 16256 {
8a06b769 16257 name = input_line_pointer;
252b5132 16258 c = get_symbol_end ();
8a06b769
TS
16259 symbolP = symbol_find_or_make (name);
16260 S_SET_EXTERNAL (symbolP);
16261
252b5132 16262 *input_line_pointer = c;
8a06b769 16263 SKIP_WHITESPACE ();
252b5132 16264
8a06b769
TS
16265 /* On Irix 5, every global symbol that is not explicitly labelled as
16266 being a function is apparently labelled as being an object. */
16267 flag = BSF_OBJECT;
252b5132 16268
8a06b769
TS
16269 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16270 && (*input_line_pointer != ','))
16271 {
16272 char *secname;
16273 asection *sec;
16274
16275 secname = input_line_pointer;
16276 c = get_symbol_end ();
16277 sec = bfd_get_section_by_name (stdoutput, secname);
16278 if (sec == NULL)
16279 as_bad (_("%s: no such section"), secname);
16280 *input_line_pointer = c;
16281
16282 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16283 flag = BSF_FUNCTION;
16284 }
16285
16286 symbol_get_bfdsym (symbolP)->flags |= flag;
16287
16288 c = *input_line_pointer;
16289 if (c == ',')
16290 {
16291 input_line_pointer++;
16292 SKIP_WHITESPACE ();
16293 if (is_end_of_line[(unsigned char) *input_line_pointer])
16294 c = '\n';
16295 }
16296 }
16297 while (c == ',');
252b5132 16298
252b5132
RH
16299 demand_empty_rest_of_line ();
16300}
16301
16302static void
17a2f251 16303s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16304{
16305 char *opt;
16306 char c;
16307
16308 opt = input_line_pointer;
16309 c = get_symbol_end ();
16310
16311 if (*opt == 'O')
16312 {
16313 /* FIXME: What does this mean? */
16314 }
16315 else if (strncmp (opt, "pic", 3) == 0)
16316 {
16317 int i;
16318
16319 i = atoi (opt + 3);
16320 if (i == 0)
16321 mips_pic = NO_PIC;
16322 else if (i == 2)
143d77c5 16323 {
8b828383 16324 mips_pic = SVR4_PIC;
143d77c5
EC
16325 mips_abicalls = TRUE;
16326 }
252b5132
RH
16327 else
16328 as_bad (_(".option pic%d not supported"), i);
16329
4d0d148d 16330 if (mips_pic == SVR4_PIC)
252b5132
RH
16331 {
16332 if (g_switch_seen && g_switch_value != 0)
16333 as_warn (_("-G may not be used with SVR4 PIC code"));
16334 g_switch_value = 0;
16335 bfd_set_gp_size (stdoutput, 0);
16336 }
16337 }
16338 else
16339 as_warn (_("Unrecognized option \"%s\""), opt);
16340
16341 *input_line_pointer = c;
16342 demand_empty_rest_of_line ();
16343}
16344
16345/* This structure is used to hold a stack of .set values. */
16346
e972090a
NC
16347struct mips_option_stack
16348{
252b5132
RH
16349 struct mips_option_stack *next;
16350 struct mips_set_options options;
16351};
16352
16353static struct mips_option_stack *mips_opts_stack;
16354
16355/* Handle the .set pseudo-op. */
16356
16357static void
17a2f251 16358s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
16359{
16360 char *name = input_line_pointer, ch;
16361
16362 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 16363 ++input_line_pointer;
252b5132
RH
16364 ch = *input_line_pointer;
16365 *input_line_pointer = '\0';
16366
16367 if (strcmp (name, "reorder") == 0)
16368 {
7d10b47d
RS
16369 if (mips_opts.noreorder)
16370 end_noreorder ();
252b5132
RH
16371 }
16372 else if (strcmp (name, "noreorder") == 0)
16373 {
7d10b47d
RS
16374 if (!mips_opts.noreorder)
16375 start_noreorder ();
252b5132 16376 }
741fe287
MR
16377 else if (strncmp (name, "at=", 3) == 0)
16378 {
16379 char *s = name + 3;
16380
16381 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16382 as_bad (_("Unrecognized register name `%s'"), s);
16383 }
252b5132
RH
16384 else if (strcmp (name, "at") == 0)
16385 {
741fe287 16386 mips_opts.at = ATREG;
252b5132
RH
16387 }
16388 else if (strcmp (name, "noat") == 0)
16389 {
741fe287 16390 mips_opts.at = ZERO;
252b5132
RH
16391 }
16392 else if (strcmp (name, "macro") == 0)
16393 {
16394 mips_opts.warn_about_macros = 0;
16395 }
16396 else if (strcmp (name, "nomacro") == 0)
16397 {
16398 if (mips_opts.noreorder == 0)
16399 as_bad (_("`noreorder' must be set before `nomacro'"));
16400 mips_opts.warn_about_macros = 1;
16401 }
16402 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16403 {
16404 mips_opts.nomove = 0;
16405 }
16406 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16407 {
16408 mips_opts.nomove = 1;
16409 }
16410 else if (strcmp (name, "bopt") == 0)
16411 {
16412 mips_opts.nobopt = 0;
16413 }
16414 else if (strcmp (name, "nobopt") == 0)
16415 {
16416 mips_opts.nobopt = 1;
16417 }
ad3fea08
TS
16418 else if (strcmp (name, "gp=default") == 0)
16419 mips_opts.gp32 = file_mips_gp32;
16420 else if (strcmp (name, "gp=32") == 0)
16421 mips_opts.gp32 = 1;
16422 else if (strcmp (name, "gp=64") == 0)
16423 {
16424 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 16425 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08
TS
16426 mips_cpu_info_from_isa (mips_opts.isa)->name);
16427 mips_opts.gp32 = 0;
16428 }
16429 else if (strcmp (name, "fp=default") == 0)
16430 mips_opts.fp32 = file_mips_fp32;
16431 else if (strcmp (name, "fp=32") == 0)
16432 mips_opts.fp32 = 1;
16433 else if (strcmp (name, "fp=64") == 0)
16434 {
16435 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 16436 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08
TS
16437 mips_cpu_info_from_isa (mips_opts.isa)->name);
16438 mips_opts.fp32 = 0;
16439 }
037b32b9
AN
16440 else if (strcmp (name, "softfloat") == 0)
16441 mips_opts.soft_float = 1;
16442 else if (strcmp (name, "hardfloat") == 0)
16443 mips_opts.soft_float = 0;
16444 else if (strcmp (name, "singlefloat") == 0)
16445 mips_opts.single_float = 1;
16446 else if (strcmp (name, "doublefloat") == 0)
16447 mips_opts.single_float = 0;
252b5132
RH
16448 else if (strcmp (name, "mips16") == 0
16449 || strcmp (name, "MIPS-16") == 0)
df58fc94
RS
16450 {
16451 if (mips_opts.micromips == 1)
16452 as_fatal (_("`mips16' cannot be used with `micromips'"));
16453 mips_opts.mips16 = 1;
16454 }
252b5132
RH
16455 else if (strcmp (name, "nomips16") == 0
16456 || strcmp (name, "noMIPS-16") == 0)
16457 mips_opts.mips16 = 0;
df58fc94
RS
16458 else if (strcmp (name, "micromips") == 0)
16459 {
16460 if (mips_opts.mips16 == 1)
16461 as_fatal (_("`micromips' cannot be used with `mips16'"));
16462 mips_opts.micromips = 1;
16463 }
16464 else if (strcmp (name, "nomicromips") == 0)
16465 mips_opts.micromips = 0;
e16bfa71
TS
16466 else if (strcmp (name, "smartmips") == 0)
16467 {
ad3fea08 16468 if (!ISA_SUPPORTS_SMARTMIPS)
20203fb9 16469 as_warn (_("%s ISA does not support SmartMIPS ASE"),
e16bfa71
TS
16470 mips_cpu_info_from_isa (mips_opts.isa)->name);
16471 mips_opts.ase_smartmips = 1;
16472 }
16473 else if (strcmp (name, "nosmartmips") == 0)
16474 mips_opts.ase_smartmips = 0;
1f25f5d3
CD
16475 else if (strcmp (name, "mips3d") == 0)
16476 mips_opts.ase_mips3d = 1;
16477 else if (strcmp (name, "nomips3d") == 0)
16478 mips_opts.ase_mips3d = 0;
a4672219
TS
16479 else if (strcmp (name, "mdmx") == 0)
16480 mips_opts.ase_mdmx = 1;
16481 else if (strcmp (name, "nomdmx") == 0)
16482 mips_opts.ase_mdmx = 0;
74cd071d 16483 else if (strcmp (name, "dsp") == 0)
ad3fea08
TS
16484 {
16485 if (!ISA_SUPPORTS_DSP_ASE)
20203fb9 16486 as_warn (_("%s ISA does not support DSP ASE"),
ad3fea08
TS
16487 mips_cpu_info_from_isa (mips_opts.isa)->name);
16488 mips_opts.ase_dsp = 1;
8b082fb1 16489 mips_opts.ase_dspr2 = 0;
ad3fea08 16490 }
74cd071d 16491 else if (strcmp (name, "nodsp") == 0)
8b082fb1
TS
16492 {
16493 mips_opts.ase_dsp = 0;
16494 mips_opts.ase_dspr2 = 0;
16495 }
16496 else if (strcmp (name, "dspr2") == 0)
16497 {
16498 if (!ISA_SUPPORTS_DSPR2_ASE)
20203fb9 16499 as_warn (_("%s ISA does not support DSP R2 ASE"),
8b082fb1
TS
16500 mips_cpu_info_from_isa (mips_opts.isa)->name);
16501 mips_opts.ase_dspr2 = 1;
16502 mips_opts.ase_dsp = 1;
16503 }
16504 else if (strcmp (name, "nodspr2") == 0)
16505 {
16506 mips_opts.ase_dspr2 = 0;
16507 mips_opts.ase_dsp = 0;
16508 }
ef2e4d86 16509 else if (strcmp (name, "mt") == 0)
ad3fea08
TS
16510 {
16511 if (!ISA_SUPPORTS_MT_ASE)
20203fb9 16512 as_warn (_("%s ISA does not support MT ASE"),
ad3fea08
TS
16513 mips_cpu_info_from_isa (mips_opts.isa)->name);
16514 mips_opts.ase_mt = 1;
16515 }
ef2e4d86
CF
16516 else if (strcmp (name, "nomt") == 0)
16517 mips_opts.ase_mt = 0;
dec0624d
MR
16518 else if (strcmp (name, "mcu") == 0)
16519 mips_opts.ase_mcu = 1;
16520 else if (strcmp (name, "nomcu") == 0)
16521 mips_opts.ase_mcu = 0;
b015e599
AP
16522 else if (strcmp (name, "virt") == 0)
16523 {
16524 if (!ISA_SUPPORTS_VIRT_ASE)
16525 as_warn (_("%s ISA does not support Virtualization ASE"),
16526 mips_cpu_info_from_isa (mips_opts.isa)->name);
16527 mips_opts.ase_virt = 1;
16528 }
16529 else if (strcmp (name, "novirt") == 0)
16530 mips_opts.ase_virt = 0;
1a2c1fad 16531 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16532 {
af7ee8bf 16533 int reset = 0;
252b5132 16534
1a2c1fad
CD
16535 /* Permit the user to change the ISA and architecture on the fly.
16536 Needless to say, misuse can cause serious problems. */
81a21e38 16537 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
16538 {
16539 reset = 1;
16540 mips_opts.isa = file_mips_isa;
1a2c1fad 16541 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
16542 }
16543 else if (strncmp (name, "arch=", 5) == 0)
16544 {
16545 const struct mips_cpu_info *p;
16546
16547 p = mips_parse_cpu("internal use", name + 5);
16548 if (!p)
16549 as_bad (_("unknown architecture %s"), name + 5);
16550 else
16551 {
16552 mips_opts.arch = p->cpu;
16553 mips_opts.isa = p->isa;
16554 }
16555 }
81a21e38
TS
16556 else if (strncmp (name, "mips", 4) == 0)
16557 {
16558 const struct mips_cpu_info *p;
16559
16560 p = mips_parse_cpu("internal use", name);
16561 if (!p)
16562 as_bad (_("unknown ISA level %s"), name + 4);
16563 else
16564 {
16565 mips_opts.arch = p->cpu;
16566 mips_opts.isa = p->isa;
16567 }
16568 }
af7ee8bf 16569 else
81a21e38 16570 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
16571
16572 switch (mips_opts.isa)
98d3f06f
KH
16573 {
16574 case 0:
98d3f06f 16575 break;
af7ee8bf
CD
16576 case ISA_MIPS1:
16577 case ISA_MIPS2:
16578 case ISA_MIPS32:
16579 case ISA_MIPS32R2:
98d3f06f
KH
16580 mips_opts.gp32 = 1;
16581 mips_opts.fp32 = 1;
16582 break;
af7ee8bf
CD
16583 case ISA_MIPS3:
16584 case ISA_MIPS4:
16585 case ISA_MIPS5:
16586 case ISA_MIPS64:
5f74bc13 16587 case ISA_MIPS64R2:
98d3f06f 16588 mips_opts.gp32 = 0;
e407c74b
NC
16589 if (mips_opts.arch == CPU_R5900)
16590 {
16591 mips_opts.fp32 = 1;
16592 }
16593 else
16594 {
98d3f06f 16595 mips_opts.fp32 = 0;
e407c74b 16596 }
98d3f06f
KH
16597 break;
16598 default:
16599 as_bad (_("unknown ISA level %s"), name + 4);
16600 break;
16601 }
af7ee8bf 16602 if (reset)
98d3f06f 16603 {
af7ee8bf
CD
16604 mips_opts.gp32 = file_mips_gp32;
16605 mips_opts.fp32 = file_mips_fp32;
98d3f06f 16606 }
252b5132
RH
16607 }
16608 else if (strcmp (name, "autoextend") == 0)
16609 mips_opts.noautoextend = 0;
16610 else if (strcmp (name, "noautoextend") == 0)
16611 mips_opts.noautoextend = 1;
16612 else if (strcmp (name, "push") == 0)
16613 {
16614 struct mips_option_stack *s;
16615
16616 s = (struct mips_option_stack *) xmalloc (sizeof *s);
16617 s->next = mips_opts_stack;
16618 s->options = mips_opts;
16619 mips_opts_stack = s;
16620 }
16621 else if (strcmp (name, "pop") == 0)
16622 {
16623 struct mips_option_stack *s;
16624
16625 s = mips_opts_stack;
16626 if (s == NULL)
16627 as_bad (_(".set pop with no .set push"));
16628 else
16629 {
16630 /* If we're changing the reorder mode we need to handle
16631 delay slots correctly. */
16632 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16633 start_noreorder ();
252b5132 16634 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16635 end_noreorder ();
252b5132
RH
16636
16637 mips_opts = s->options;
16638 mips_opts_stack = s->next;
16639 free (s);
16640 }
16641 }
aed1a261
RS
16642 else if (strcmp (name, "sym32") == 0)
16643 mips_opts.sym32 = TRUE;
16644 else if (strcmp (name, "nosym32") == 0)
16645 mips_opts.sym32 = FALSE;
e6559e01
JM
16646 else if (strchr (name, ','))
16647 {
16648 /* Generic ".set" directive; use the generic handler. */
16649 *input_line_pointer = ch;
16650 input_line_pointer = name;
16651 s_set (0);
16652 return;
16653 }
252b5132
RH
16654 else
16655 {
16656 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16657 }
16658 *input_line_pointer = ch;
16659 demand_empty_rest_of_line ();
16660}
16661
16662/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16663 .option pic2. It means to generate SVR4 PIC calls. */
16664
16665static void
17a2f251 16666s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16667{
16668 mips_pic = SVR4_PIC;
143d77c5 16669 mips_abicalls = TRUE;
4d0d148d
TS
16670
16671 if (g_switch_seen && g_switch_value != 0)
16672 as_warn (_("-G may not be used with SVR4 PIC code"));
16673 g_switch_value = 0;
16674
252b5132
RH
16675 bfd_set_gp_size (stdoutput, 0);
16676 demand_empty_rest_of_line ();
16677}
16678
16679/* Handle the .cpload pseudo-op. This is used when generating SVR4
16680 PIC code. It sets the $gp register for the function based on the
16681 function address, which is in the register named in the argument.
16682 This uses a relocation against _gp_disp, which is handled specially
16683 by the linker. The result is:
16684 lui $gp,%hi(_gp_disp)
16685 addiu $gp,$gp,%lo(_gp_disp)
16686 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16687 The .cpload argument is normally $25 == $t9.
16688
16689 The -mno-shared option changes this to:
bbe506e8
TS
16690 lui $gp,%hi(__gnu_local_gp)
16691 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16692 and the argument is ignored. This saves an instruction, but the
16693 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16694 address for __gnu_local_gp. Thus code assembled with -mno-shared
16695 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16696
16697static void
17a2f251 16698s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16699{
16700 expressionS ex;
aa6975fb
ILT
16701 int reg;
16702 int in_shared;
252b5132 16703
6478892d
TS
16704 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16705 .cpload is ignored. */
16706 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16707 {
16708 s_ignore (0);
16709 return;
16710 }
16711
a276b80c
MR
16712 if (mips_opts.mips16)
16713 {
16714 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16715 ignore_rest_of_line ();
16716 return;
16717 }
16718
d3ecfc59 16719 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16720 if (mips_opts.noreorder == 0)
16721 as_warn (_(".cpload not in noreorder section"));
16722
aa6975fb
ILT
16723 reg = tc_get_register (0);
16724
16725 /* If we need to produce a 64-bit address, we are better off using
16726 the default instruction sequence. */
aed1a261 16727 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16728
252b5132 16729 ex.X_op = O_symbol;
bbe506e8
TS
16730 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16731 "__gnu_local_gp");
252b5132
RH
16732 ex.X_op_symbol = NULL;
16733 ex.X_add_number = 0;
16734
16735 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16736 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16737
8a75745d
MR
16738 mips_mark_labels ();
16739 mips_assembling_insn = TRUE;
16740
584892a6 16741 macro_start ();
67c0d1eb
RS
16742 macro_build_lui (&ex, mips_gp_register);
16743 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16744 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16745 if (in_shared)
16746 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16747 mips_gp_register, reg);
584892a6 16748 macro_end ();
252b5132 16749
8a75745d 16750 mips_assembling_insn = FALSE;
252b5132
RH
16751 demand_empty_rest_of_line ();
16752}
16753
6478892d
TS
16754/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16755 .cpsetup $reg1, offset|$reg2, label
16756
16757 If offset is given, this results in:
16758 sd $gp, offset($sp)
956cd1d6 16759 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16760 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16761 daddu $gp, $gp, $reg1
6478892d
TS
16762
16763 If $reg2 is given, this results in:
16764 daddu $reg2, $gp, $0
956cd1d6 16765 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16766 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16767 daddu $gp, $gp, $reg1
aa6975fb
ILT
16768 $reg1 is normally $25 == $t9.
16769
16770 The -mno-shared option replaces the last three instructions with
16771 lui $gp,%hi(_gp)
54f4ddb3 16772 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16773
6478892d 16774static void
17a2f251 16775s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16776{
16777 expressionS ex_off;
16778 expressionS ex_sym;
16779 int reg1;
6478892d 16780
8586fc66 16781 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16782 We also need NewABI support. */
16783 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16784 {
16785 s_ignore (0);
16786 return;
16787 }
16788
a276b80c
MR
16789 if (mips_opts.mips16)
16790 {
16791 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16792 ignore_rest_of_line ();
16793 return;
16794 }
16795
6478892d
TS
16796 reg1 = tc_get_register (0);
16797 SKIP_WHITESPACE ();
16798 if (*input_line_pointer != ',')
16799 {
16800 as_bad (_("missing argument separator ',' for .cpsetup"));
16801 return;
16802 }
16803 else
80245285 16804 ++input_line_pointer;
6478892d
TS
16805 SKIP_WHITESPACE ();
16806 if (*input_line_pointer == '$')
80245285
TS
16807 {
16808 mips_cpreturn_register = tc_get_register (0);
16809 mips_cpreturn_offset = -1;
16810 }
6478892d 16811 else
80245285
TS
16812 {
16813 mips_cpreturn_offset = get_absolute_expression ();
16814 mips_cpreturn_register = -1;
16815 }
6478892d
TS
16816 SKIP_WHITESPACE ();
16817 if (*input_line_pointer != ',')
16818 {
16819 as_bad (_("missing argument separator ',' for .cpsetup"));
16820 return;
16821 }
16822 else
f9419b05 16823 ++input_line_pointer;
6478892d 16824 SKIP_WHITESPACE ();
f21f8242 16825 expression (&ex_sym);
6478892d 16826
8a75745d
MR
16827 mips_mark_labels ();
16828 mips_assembling_insn = TRUE;
16829
584892a6 16830 macro_start ();
6478892d
TS
16831 if (mips_cpreturn_register == -1)
16832 {
16833 ex_off.X_op = O_constant;
16834 ex_off.X_add_symbol = NULL;
16835 ex_off.X_op_symbol = NULL;
16836 ex_off.X_add_number = mips_cpreturn_offset;
16837
67c0d1eb 16838 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16839 BFD_RELOC_LO16, SP);
6478892d
TS
16840 }
16841 else
67c0d1eb 16842 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 16843 mips_gp_register, 0);
6478892d 16844
aed1a261 16845 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16846 {
df58fc94 16847 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16848 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16849 BFD_RELOC_HI16_S);
16850
16851 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16852 mips_gp_register, -1, BFD_RELOC_GPREL16,
16853 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16854
16855 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16856 mips_gp_register, reg1);
16857 }
16858 else
16859 {
16860 expressionS ex;
16861
16862 ex.X_op = O_symbol;
4184909a 16863 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16864 ex.X_op_symbol = NULL;
16865 ex.X_add_number = 0;
6e1304d8 16866
aa6975fb
ILT
16867 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16868 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16869
16870 macro_build_lui (&ex, mips_gp_register);
16871 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16872 mips_gp_register, BFD_RELOC_LO16);
16873 }
f21f8242 16874
584892a6 16875 macro_end ();
6478892d 16876
8a75745d 16877 mips_assembling_insn = FALSE;
6478892d
TS
16878 demand_empty_rest_of_line ();
16879}
16880
16881static void
17a2f251 16882s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16883{
16884 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16885 .cplocal is ignored. */
6478892d
TS
16886 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16887 {
16888 s_ignore (0);
16889 return;
16890 }
16891
a276b80c
MR
16892 if (mips_opts.mips16)
16893 {
16894 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16895 ignore_rest_of_line ();
16896 return;
16897 }
16898
6478892d 16899 mips_gp_register = tc_get_register (0);
85b51719 16900 demand_empty_rest_of_line ();
6478892d
TS
16901}
16902
252b5132
RH
16903/* Handle the .cprestore pseudo-op. This stores $gp into a given
16904 offset from $sp. The offset is remembered, and after making a PIC
16905 call $gp is restored from that location. */
16906
16907static void
17a2f251 16908s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16909{
16910 expressionS ex;
252b5132 16911
6478892d 16912 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16913 .cprestore is ignored. */
6478892d 16914 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16915 {
16916 s_ignore (0);
16917 return;
16918 }
16919
a276b80c
MR
16920 if (mips_opts.mips16)
16921 {
16922 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16923 ignore_rest_of_line ();
16924 return;
16925 }
16926
252b5132 16927 mips_cprestore_offset = get_absolute_expression ();
7a621144 16928 mips_cprestore_valid = 1;
252b5132
RH
16929
16930 ex.X_op = O_constant;
16931 ex.X_add_symbol = NULL;
16932 ex.X_op_symbol = NULL;
16933 ex.X_add_number = mips_cprestore_offset;
16934
8a75745d
MR
16935 mips_mark_labels ();
16936 mips_assembling_insn = TRUE;
16937
584892a6 16938 macro_start ();
67c0d1eb
RS
16939 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16940 SP, HAVE_64BIT_ADDRESSES);
584892a6 16941 macro_end ();
252b5132 16942
8a75745d 16943 mips_assembling_insn = FALSE;
252b5132
RH
16944 demand_empty_rest_of_line ();
16945}
16946
6478892d 16947/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16948 was given in the preceding .cpsetup, it results in:
6478892d 16949 ld $gp, offset($sp)
76b3015f 16950
6478892d 16951 If a register $reg2 was given there, it results in:
54f4ddb3
TS
16952 daddu $gp, $reg2, $0 */
16953
6478892d 16954static void
17a2f251 16955s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16956{
16957 expressionS ex;
6478892d
TS
16958
16959 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16960 We also need NewABI support. */
16961 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16962 {
16963 s_ignore (0);
16964 return;
16965 }
16966
a276b80c
MR
16967 if (mips_opts.mips16)
16968 {
16969 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16970 ignore_rest_of_line ();
16971 return;
16972 }
16973
8a75745d
MR
16974 mips_mark_labels ();
16975 mips_assembling_insn = TRUE;
16976
584892a6 16977 macro_start ();
6478892d
TS
16978 if (mips_cpreturn_register == -1)
16979 {
16980 ex.X_op = O_constant;
16981 ex.X_add_symbol = NULL;
16982 ex.X_op_symbol = NULL;
16983 ex.X_add_number = mips_cpreturn_offset;
16984
67c0d1eb 16985 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
16986 }
16987 else
67c0d1eb 16988 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 16989 mips_cpreturn_register, 0);
584892a6 16990 macro_end ();
6478892d 16991
8a75745d 16992 mips_assembling_insn = FALSE;
6478892d
TS
16993 demand_empty_rest_of_line ();
16994}
16995
d0f13682
CLT
16996/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16997 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16998 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16999 debug information or MIPS16 TLS. */
741d6ea8
JM
17000
17001static void
d0f13682
CLT
17002s_tls_rel_directive (const size_t bytes, const char *dirstr,
17003 bfd_reloc_code_real_type rtype)
741d6ea8
JM
17004{
17005 expressionS ex;
17006 char *p;
17007
17008 expression (&ex);
17009
17010 if (ex.X_op != O_symbol)
17011 {
d0f13682 17012 as_bad (_("Unsupported use of %s"), dirstr);
741d6ea8
JM
17013 ignore_rest_of_line ();
17014 }
17015
17016 p = frag_more (bytes);
17017 md_number_to_chars (p, 0, bytes);
d0f13682 17018 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 17019 demand_empty_rest_of_line ();
de64cffd 17020 mips_clear_insn_labels ();
741d6ea8
JM
17021}
17022
17023/* Handle .dtprelword. */
17024
17025static void
17026s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17027{
d0f13682 17028 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
17029}
17030
17031/* Handle .dtpreldword. */
17032
17033static void
17034s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17035{
d0f13682
CLT
17036 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17037}
17038
17039/* Handle .tprelword. */
17040
17041static void
17042s_tprelword (int ignore ATTRIBUTE_UNUSED)
17043{
17044 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17045}
17046
17047/* Handle .tpreldword. */
17048
17049static void
17050s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17051{
17052 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
17053}
17054
6478892d
TS
17055/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17056 code. It sets the offset to use in gp_rel relocations. */
17057
17058static void
17a2f251 17059s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17060{
17061 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17062 We also need NewABI support. */
17063 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17064 {
17065 s_ignore (0);
17066 return;
17067 }
17068
def2e0dd 17069 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
17070
17071 demand_empty_rest_of_line ();
17072}
17073
252b5132
RH
17074/* Handle the .gpword pseudo-op. This is used when generating PIC
17075 code. It generates a 32 bit GP relative reloc. */
17076
17077static void
17a2f251 17078s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 17079{
a8dbcb85
TS
17080 segment_info_type *si;
17081 struct insn_label_list *l;
252b5132
RH
17082 expressionS ex;
17083 char *p;
17084
17085 /* When not generating PIC code, this is treated as .word. */
17086 if (mips_pic != SVR4_PIC)
17087 {
17088 s_cons (2);
17089 return;
17090 }
17091
a8dbcb85
TS
17092 si = seg_info (now_seg);
17093 l = si->label_list;
7d10b47d 17094 mips_emit_delays ();
252b5132 17095 if (auto_align)
462427c4 17096 mips_align (2, 0, l);
252b5132
RH
17097
17098 expression (&ex);
a1facbec 17099 mips_clear_insn_labels ();
252b5132
RH
17100
17101 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17102 {
17103 as_bad (_("Unsupported use of .gpword"));
17104 ignore_rest_of_line ();
17105 }
17106
17107 p = frag_more (4);
17a2f251 17108 md_number_to_chars (p, 0, 4);
b34976b6 17109 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 17110 BFD_RELOC_GPREL32);
252b5132
RH
17111
17112 demand_empty_rest_of_line ();
17113}
17114
10181a0d 17115static void
17a2f251 17116s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 17117{
a8dbcb85
TS
17118 segment_info_type *si;
17119 struct insn_label_list *l;
10181a0d
AO
17120 expressionS ex;
17121 char *p;
17122
17123 /* When not generating PIC code, this is treated as .dword. */
17124 if (mips_pic != SVR4_PIC)
17125 {
17126 s_cons (3);
17127 return;
17128 }
17129
a8dbcb85
TS
17130 si = seg_info (now_seg);
17131 l = si->label_list;
7d10b47d 17132 mips_emit_delays ();
10181a0d 17133 if (auto_align)
462427c4 17134 mips_align (3, 0, l);
10181a0d
AO
17135
17136 expression (&ex);
a1facbec 17137 mips_clear_insn_labels ();
10181a0d
AO
17138
17139 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17140 {
17141 as_bad (_("Unsupported use of .gpdword"));
17142 ignore_rest_of_line ();
17143 }
17144
17145 p = frag_more (8);
17a2f251 17146 md_number_to_chars (p, 0, 8);
a105a300 17147 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17148 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17149
17150 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17151 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17152 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17153
17154 demand_empty_rest_of_line ();
17155}
17156
252b5132
RH
17157/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17158 tables in SVR4 PIC code. */
17159
17160static void
17a2f251 17161s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17162{
252b5132
RH
17163 int reg;
17164
10181a0d
AO
17165 /* This is ignored when not generating SVR4 PIC code. */
17166 if (mips_pic != SVR4_PIC)
252b5132
RH
17167 {
17168 s_ignore (0);
17169 return;
17170 }
17171
8a75745d
MR
17172 mips_mark_labels ();
17173 mips_assembling_insn = TRUE;
17174
252b5132 17175 /* Add $gp to the register named as an argument. */
584892a6 17176 macro_start ();
252b5132 17177 reg = tc_get_register (0);
67c0d1eb 17178 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17179 macro_end ();
252b5132 17180
8a75745d 17181 mips_assembling_insn = FALSE;
bdaaa2e1 17182 demand_empty_rest_of_line ();
252b5132
RH
17183}
17184
17185/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17186 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17187 such as generating jalx instructions when needed. We also make
17188 them odd for the duration of the assembly, in order to generate the
17189 right sort of code. We will make them even in the adjust_symtab
17190 routine, while leaving them marked. This is convenient for the
17191 debugger and the disassembler. The linker knows to make them odd
17192 again. */
17193
17194static void
17a2f251 17195s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17196{
df58fc94 17197 mips_mark_labels ();
252b5132
RH
17198
17199 demand_empty_rest_of_line ();
17200}
17201
754e2bb9
RS
17202/* Handle a .stab[snd] directive. Ideally these directives would be
17203 implemented in a transparent way, so that removing them would not
17204 have any effect on the generated instructions. However, s_stab
17205 internally changes the section, so in practice we need to decide
17206 now whether the preceding label marks compressed code. We do not
17207 support changing the compression mode of a label after a .stab*
17208 directive, such as in:
17209
17210 foo:
17211 .stabs ...
17212 .set mips16
17213
17214 so the current mode wins. */
252b5132
RH
17215
17216static void
17a2f251 17217s_mips_stab (int type)
252b5132 17218{
754e2bb9 17219 mips_mark_labels ();
252b5132
RH
17220 s_stab (type);
17221}
17222
54f4ddb3 17223/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17224
17225static void
17a2f251 17226s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17227{
17228 char *name;
17229 int c;
17230 symbolS *symbolP;
17231 expressionS exp;
17232
17233 name = input_line_pointer;
17234 c = get_symbol_end ();
17235 symbolP = symbol_find_or_make (name);
17236 S_SET_WEAK (symbolP);
17237 *input_line_pointer = c;
17238
17239 SKIP_WHITESPACE ();
17240
17241 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17242 {
17243 if (S_IS_DEFINED (symbolP))
17244 {
20203fb9 17245 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17246 S_GET_NAME (symbolP));
17247 ignore_rest_of_line ();
17248 return;
17249 }
bdaaa2e1 17250
252b5132
RH
17251 if (*input_line_pointer == ',')
17252 {
17253 ++input_line_pointer;
17254 SKIP_WHITESPACE ();
17255 }
bdaaa2e1 17256
252b5132
RH
17257 expression (&exp);
17258 if (exp.X_op != O_symbol)
17259 {
20203fb9 17260 as_bad (_("bad .weakext directive"));
98d3f06f 17261 ignore_rest_of_line ();
252b5132
RH
17262 return;
17263 }
49309057 17264 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17265 }
17266
17267 demand_empty_rest_of_line ();
17268}
17269
17270/* Parse a register string into a number. Called from the ECOFF code
17271 to parse .frame. The argument is non-zero if this is the frame
17272 register, so that we can record it in mips_frame_reg. */
17273
17274int
17a2f251 17275tc_get_register (int frame)
252b5132 17276{
707bfff6 17277 unsigned int reg;
252b5132
RH
17278
17279 SKIP_WHITESPACE ();
707bfff6
TS
17280 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17281 reg = 0;
252b5132 17282 if (frame)
7a621144
DJ
17283 {
17284 mips_frame_reg = reg != 0 ? reg : SP;
17285 mips_frame_reg_valid = 1;
17286 mips_cprestore_valid = 0;
17287 }
252b5132
RH
17288 return reg;
17289}
17290
17291valueT
17a2f251 17292md_section_align (asection *seg, valueT addr)
252b5132
RH
17293{
17294 int align = bfd_get_section_alignment (stdoutput, seg);
17295
b4c71f56
TS
17296 if (IS_ELF)
17297 {
17298 /* We don't need to align ELF sections to the full alignment.
17299 However, Irix 5 may prefer that we align them at least to a 16
17300 byte boundary. We don't bother to align the sections if we
17301 are targeted for an embedded system. */
c41e87e3 17302 if (strncmp (TARGET_OS, "elf", 3) == 0)
b4c71f56
TS
17303 return addr;
17304 if (align > 4)
17305 align = 4;
17306 }
252b5132
RH
17307
17308 return ((addr + (1 << align) - 1) & (-1 << align));
17309}
17310
17311/* Utility routine, called from above as well. If called while the
17312 input file is still being read, it's only an approximation. (For
17313 example, a symbol may later become defined which appeared to be
17314 undefined earlier.) */
17315
17316static int
17a2f251 17317nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17318{
17319 if (sym == 0)
17320 return 0;
17321
4d0d148d 17322 if (g_switch_value > 0)
252b5132
RH
17323 {
17324 const char *symname;
17325 int change;
17326
c9914766 17327 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17328 register. It can be if it is smaller than the -G size or if
17329 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17330 not be referenced off the $gp, although it appears as though
252b5132
RH
17331 they can. */
17332 symname = S_GET_NAME (sym);
17333 if (symname != (const char *) NULL
17334 && (strcmp (symname, "eprol") == 0
17335 || strcmp (symname, "etext") == 0
17336 || strcmp (symname, "_gp") == 0
17337 || strcmp (symname, "edata") == 0
17338 || strcmp (symname, "_fbss") == 0
17339 || strcmp (symname, "_fdata") == 0
17340 || strcmp (symname, "_ftext") == 0
17341 || strcmp (symname, "end") == 0
17342 || strcmp (symname, "_gp_disp") == 0))
17343 change = 1;
17344 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17345 && (0
17346#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17347 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17348 && (symbol_get_obj (sym)->ecoff_extern_size
17349 <= g_switch_value))
252b5132
RH
17350#endif
17351 /* We must defer this decision until after the whole
17352 file has been read, since there might be a .extern
17353 after the first use of this symbol. */
17354 || (before_relaxing
17355#ifndef NO_ECOFF_DEBUGGING
49309057 17356 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17357#endif
17358 && S_GET_VALUE (sym) == 0)
17359 || (S_GET_VALUE (sym) != 0
17360 && S_GET_VALUE (sym) <= g_switch_value)))
17361 change = 0;
17362 else
17363 {
17364 const char *segname;
17365
17366 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17367 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17368 && strcmp (segname, ".lit4") != 0);
17369 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17370 && strcmp (segname, ".sbss") != 0
17371 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17372 && strncmp (segname, ".sbss.", 6) != 0
17373 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17374 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17375 }
17376 return change;
17377 }
17378 else
c9914766 17379 /* We are not optimizing for the $gp register. */
252b5132
RH
17380 return 1;
17381}
17382
5919d012
RS
17383
17384/* Return true if the given symbol should be considered local for SVR4 PIC. */
17385
17386static bfd_boolean
17a2f251 17387pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
17388{
17389 asection *symsec;
5919d012
RS
17390
17391 /* Handle the case of a symbol equated to another symbol. */
17392 while (symbol_equated_reloc_p (sym))
17393 {
17394 symbolS *n;
17395
5f0fe04b 17396 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17397 n = symbol_get_value_expression (sym)->X_add_symbol;
17398 if (n == sym)
17399 break;
17400 sym = n;
17401 }
17402
df1f3cda
DD
17403 if (symbol_section_p (sym))
17404 return TRUE;
17405
5919d012
RS
17406 symsec = S_GET_SEGMENT (sym);
17407
5919d012 17408 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17409 return (!bfd_is_und_section (symsec)
17410 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
17411 && !bfd_is_com_section (symsec)
17412 && !s_is_linkonce (sym, segtype)
5919d012
RS
17413#ifdef OBJ_ELF
17414 /* A global or weak symbol is treated as external. */
f43abd2b 17415 && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
5919d012
RS
17416#endif
17417 );
17418}
17419
17420
252b5132
RH
17421/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17422 extended opcode. SEC is the section the frag is in. */
17423
17424static int
17a2f251 17425mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
17426{
17427 int type;
3994f87e 17428 const struct mips16_immed_operand *op;
252b5132
RH
17429 offsetT val;
17430 int mintiny, maxtiny;
17431 segT symsec;
98aa84af 17432 fragS *sym_frag;
252b5132
RH
17433
17434 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17435 return 0;
17436 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17437 return 1;
17438
17439 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17440 op = mips16_immed_operands;
17441 while (op->type != type)
17442 {
17443 ++op;
9c2799c2 17444 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
17445 }
17446
17447 if (op->unsp)
17448 {
17449 if (type == '<' || type == '>' || type == '[' || type == ']')
17450 {
17451 mintiny = 1;
17452 maxtiny = 1 << op->nbits;
17453 }
17454 else
17455 {
17456 mintiny = 0;
17457 maxtiny = (1 << op->nbits) - 1;
17458 }
17459 }
17460 else
17461 {
17462 mintiny = - (1 << (op->nbits - 1));
17463 maxtiny = (1 << (op->nbits - 1)) - 1;
17464 }
17465
98aa84af 17466 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 17467 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 17468 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
17469
17470 if (op->pcrel)
17471 {
17472 addressT addr;
17473
17474 /* We won't have the section when we are called from
17475 mips_relax_frag. However, we will always have been called
17476 from md_estimate_size_before_relax first. If this is a
17477 branch to a different section, we mark it as such. If SEC is
17478 NULL, and the frag is not marked, then it must be a branch to
17479 the same section. */
17480 if (sec == NULL)
17481 {
17482 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17483 return 1;
17484 }
17485 else
17486 {
98aa84af 17487 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
17488 if (symsec != sec)
17489 {
17490 fragp->fr_subtype =
17491 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17492
17493 /* FIXME: We should support this, and let the linker
17494 catch branches and loads that are out of range. */
17495 as_bad_where (fragp->fr_file, fragp->fr_line,
17496 _("unsupported PC relative reference to different section"));
17497
17498 return 1;
17499 }
98aa84af
AM
17500 if (fragp != sym_frag && sym_frag->fr_address == 0)
17501 /* Assume non-extended on the first relaxation pass.
17502 The address we have calculated will be bogus if this is
17503 a forward branch to another frag, as the forward frag
17504 will have fr_address == 0. */
17505 return 0;
252b5132
RH
17506 }
17507
17508 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
17509 the same section. If the relax_marker of the symbol fragment
17510 differs from the relax_marker of this fragment, we have not
17511 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
17512 in STRETCH in order to get a better estimate of the address.
17513 This particularly matters because of the shift bits. */
17514 if (stretch != 0
98aa84af 17515 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
17516 {
17517 fragS *f;
17518
17519 /* Adjust stretch for any alignment frag. Note that if have
17520 been expanding the earlier code, the symbol may be
17521 defined in what appears to be an earlier frag. FIXME:
17522 This doesn't handle the fr_subtype field, which specifies
17523 a maximum number of bytes to skip when doing an
17524 alignment. */
98aa84af 17525 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
17526 {
17527 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17528 {
17529 if (stretch < 0)
17530 stretch = - ((- stretch)
17531 & ~ ((1 << (int) f->fr_offset) - 1));
17532 else
17533 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17534 if (stretch == 0)
17535 break;
17536 }
17537 }
17538 if (f != NULL)
17539 val += stretch;
17540 }
17541
17542 addr = fragp->fr_address + fragp->fr_fix;
17543
17544 /* The base address rules are complicated. The base address of
17545 a branch is the following instruction. The base address of a
17546 PC relative load or add is the instruction itself, but if it
17547 is in a delay slot (in which case it can not be extended) use
17548 the address of the instruction whose delay slot it is in. */
17549 if (type == 'p' || type == 'q')
17550 {
17551 addr += 2;
17552
17553 /* If we are currently assuming that this frag should be
17554 extended, then, the current address is two bytes
bdaaa2e1 17555 higher. */
252b5132
RH
17556 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17557 addr += 2;
17558
17559 /* Ignore the low bit in the target, since it will be set
17560 for a text label. */
17561 if ((val & 1) != 0)
17562 --val;
17563 }
17564 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17565 addr -= 4;
17566 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17567 addr -= 2;
17568
17569 val -= addr & ~ ((1 << op->shift) - 1);
17570
17571 /* Branch offsets have an implicit 0 in the lowest bit. */
17572 if (type == 'p' || type == 'q')
17573 val /= 2;
17574
17575 /* If any of the shifted bits are set, we must use an extended
17576 opcode. If the address depends on the size of this
17577 instruction, this can lead to a loop, so we arrange to always
17578 use an extended opcode. We only check this when we are in
17579 the main relaxation loop, when SEC is NULL. */
17580 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17581 {
17582 fragp->fr_subtype =
17583 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17584 return 1;
17585 }
17586
17587 /* If we are about to mark a frag as extended because the value
17588 is precisely maxtiny + 1, then there is a chance of an
17589 infinite loop as in the following code:
17590 la $4,foo
17591 .skip 1020
17592 .align 2
17593 foo:
17594 In this case when the la is extended, foo is 0x3fc bytes
17595 away, so the la can be shrunk, but then foo is 0x400 away, so
17596 the la must be extended. To avoid this loop, we mark the
17597 frag as extended if it was small, and is about to become
17598 extended with a value of maxtiny + 1. */
17599 if (val == ((maxtiny + 1) << op->shift)
17600 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17601 && sec == NULL)
17602 {
17603 fragp->fr_subtype =
17604 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17605 return 1;
17606 }
17607 }
17608 else if (symsec != absolute_section && sec != NULL)
17609 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17610
17611 if ((val & ((1 << op->shift) - 1)) != 0
17612 || val < (mintiny << op->shift)
17613 || val > (maxtiny << op->shift))
17614 return 1;
17615 else
17616 return 0;
17617}
17618
4a6a3df4
AO
17619/* Compute the length of a branch sequence, and adjust the
17620 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17621 worst-case length is computed, with UPDATE being used to indicate
17622 whether an unconditional (-1), branch-likely (+1) or regular (0)
17623 branch is to be computed. */
17624static int
17a2f251 17625relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17626{
b34976b6 17627 bfd_boolean toofar;
4a6a3df4
AO
17628 int length;
17629
17630 if (fragp
17631 && S_IS_DEFINED (fragp->fr_symbol)
17632 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17633 {
17634 addressT addr;
17635 offsetT val;
17636
17637 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17638
17639 addr = fragp->fr_address + fragp->fr_fix + 4;
17640
17641 val -= addr;
17642
17643 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17644 }
17645 else if (fragp)
17646 /* If the symbol is not defined or it's in a different segment,
17647 assume the user knows what's going on and emit a short
17648 branch. */
b34976b6 17649 toofar = FALSE;
4a6a3df4 17650 else
b34976b6 17651 toofar = TRUE;
4a6a3df4
AO
17652
17653 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17654 fragp->fr_subtype
66b3e8da
MR
17655 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17656 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17657 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17658 RELAX_BRANCH_LINK (fragp->fr_subtype),
17659 toofar);
17660
17661 length = 4;
17662 if (toofar)
17663 {
17664 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17665 length += 8;
17666
17667 if (mips_pic != NO_PIC)
17668 {
17669 /* Additional space for PIC loading of target address. */
17670 length += 8;
17671 if (mips_opts.isa == ISA_MIPS1)
17672 /* Additional space for $at-stabilizing nop. */
17673 length += 4;
17674 }
17675
17676 /* If branch is conditional. */
17677 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17678 length += 8;
17679 }
b34976b6 17680
4a6a3df4
AO
17681 return length;
17682}
17683
df58fc94
RS
17684/* Compute the length of a branch sequence, and adjust the
17685 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17686 worst-case length is computed, with UPDATE being used to indicate
17687 whether an unconditional (-1), or regular (0) branch is to be
17688 computed. */
17689
17690static int
17691relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17692{
17693 bfd_boolean toofar;
17694 int length;
17695
17696 if (fragp
17697 && S_IS_DEFINED (fragp->fr_symbol)
17698 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17699 {
17700 addressT addr;
17701 offsetT val;
17702
17703 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17704 /* Ignore the low bit in the target, since it will be set
17705 for a text label. */
17706 if ((val & 1) != 0)
17707 --val;
17708
17709 addr = fragp->fr_address + fragp->fr_fix + 4;
17710
17711 val -= addr;
17712
17713 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17714 }
17715 else if (fragp)
17716 /* If the symbol is not defined or it's in a different segment,
17717 assume the user knows what's going on and emit a short
17718 branch. */
17719 toofar = FALSE;
17720 else
17721 toofar = TRUE;
17722
17723 if (fragp && update
17724 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17725 fragp->fr_subtype = (toofar
17726 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17727 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17728
17729 length = 4;
17730 if (toofar)
17731 {
17732 bfd_boolean compact_known = fragp != NULL;
17733 bfd_boolean compact = FALSE;
17734 bfd_boolean uncond;
17735
17736 if (compact_known)
17737 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17738 if (fragp)
17739 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17740 else
17741 uncond = update < 0;
17742
17743 /* If label is out of range, we turn branch <br>:
17744
17745 <br> label # 4 bytes
17746 0:
17747
17748 into:
17749
17750 j label # 4 bytes
17751 nop # 2 bytes if compact && !PIC
17752 0:
17753 */
17754 if (mips_pic == NO_PIC && (!compact_known || compact))
17755 length += 2;
17756
17757 /* If assembling PIC code, we further turn:
17758
17759 j label # 4 bytes
17760
17761 into:
17762
17763 lw/ld at, %got(label)(gp) # 4 bytes
17764 d/addiu at, %lo(label) # 4 bytes
17765 jr/c at # 2 bytes
17766 */
17767 if (mips_pic != NO_PIC)
17768 length += 6;
17769
17770 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17771
17772 <brneg> 0f # 4 bytes
17773 nop # 2 bytes if !compact
17774 */
17775 if (!uncond)
17776 length += (compact_known && compact) ? 4 : 6;
17777 }
17778
17779 return length;
17780}
17781
17782/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17783 bit accordingly. */
17784
17785static int
17786relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17787{
17788 bfd_boolean toofar;
17789
df58fc94
RS
17790 if (fragp
17791 && S_IS_DEFINED (fragp->fr_symbol)
17792 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17793 {
17794 addressT addr;
17795 offsetT val;
17796 int type;
17797
17798 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17799 /* Ignore the low bit in the target, since it will be set
17800 for a text label. */
17801 if ((val & 1) != 0)
17802 --val;
17803
17804 /* Assume this is a 2-byte branch. */
17805 addr = fragp->fr_address + fragp->fr_fix + 2;
17806
17807 /* We try to avoid the infinite loop by not adding 2 more bytes for
17808 long branches. */
17809
17810 val -= addr;
17811
17812 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17813 if (type == 'D')
17814 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17815 else if (type == 'E')
17816 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17817 else
17818 abort ();
17819 }
17820 else
17821 /* If the symbol is not defined or it's in a different segment,
17822 we emit a normal 32-bit branch. */
17823 toofar = TRUE;
17824
17825 if (fragp && update
17826 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17827 fragp->fr_subtype
17828 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17829 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17830
17831 if (toofar)
17832 return 4;
17833
17834 return 2;
17835}
17836
252b5132
RH
17837/* Estimate the size of a frag before relaxing. Unless this is the
17838 mips16, we are not really relaxing here, and the final size is
17839 encoded in the subtype information. For the mips16, we have to
17840 decide whether we are using an extended opcode or not. */
17841
252b5132 17842int
17a2f251 17843md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17844{
5919d012 17845 int change;
252b5132 17846
4a6a3df4
AO
17847 if (RELAX_BRANCH_P (fragp->fr_subtype))
17848 {
17849
b34976b6
AM
17850 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17851
4a6a3df4
AO
17852 return fragp->fr_var;
17853 }
17854
252b5132 17855 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
17856 /* We don't want to modify the EXTENDED bit here; it might get us
17857 into infinite loops. We change it only in mips_relax_frag(). */
17858 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 17859
df58fc94
RS
17860 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17861 {
17862 int length = 4;
17863
17864 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17865 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17866 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17867 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17868 fragp->fr_var = length;
17869
17870 return length;
17871 }
17872
252b5132 17873 if (mips_pic == NO_PIC)
5919d012 17874 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 17875 else if (mips_pic == SVR4_PIC)
5919d012 17876 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
17877 else if (mips_pic == VXWORKS_PIC)
17878 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17879 change = 0;
252b5132
RH
17880 else
17881 abort ();
17882
17883 if (change)
17884 {
4d7206a2 17885 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 17886 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 17887 }
4d7206a2
RS
17888 else
17889 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
17890}
17891
17892/* This is called to see whether a reloc against a defined symbol
de7e6852 17893 should be converted into a reloc against a section. */
252b5132
RH
17894
17895int
17a2f251 17896mips_fix_adjustable (fixS *fixp)
252b5132 17897{
252b5132
RH
17898 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17899 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17900 return 0;
a161fe53 17901
252b5132
RH
17902 if (fixp->fx_addsy == NULL)
17903 return 1;
a161fe53 17904
de7e6852
RS
17905 /* If symbol SYM is in a mergeable section, relocations of the form
17906 SYM + 0 can usually be made section-relative. The mergeable data
17907 is then identified by the section offset rather than by the symbol.
17908
17909 However, if we're generating REL LO16 relocations, the offset is split
17910 between the LO16 and parterning high part relocation. The linker will
17911 need to recalculate the complete offset in order to correctly identify
17912 the merge data.
17913
17914 The linker has traditionally not looked for the parterning high part
17915 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17916 placed anywhere. Rather than break backwards compatibility by changing
17917 this, it seems better not to force the issue, and instead keep the
17918 original symbol. This will work with either linker behavior. */
738e5348 17919 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 17920 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
17921 && HAVE_IN_PLACE_ADDENDS
17922 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17923 return 0;
17924
ce70d90a 17925 /* There is no place to store an in-place offset for JALR relocations.
2de39019
CM
17926 Likewise an in-range offset of limited PC-relative relocations may
17927 overflow the in-place relocatable field if recalculated against the
17928 start address of the symbol's containing section. */
ce70d90a 17929 if (HAVE_IN_PLACE_ADDENDS
2de39019
CM
17930 && (limited_pcrel_reloc_p (fixp->fx_r_type)
17931 || jalr_reloc_p (fixp->fx_r_type)))
1180b5a4
RS
17932 return 0;
17933
252b5132 17934#ifdef OBJ_ELF
b314ec0e
RS
17935 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17936 to a floating-point stub. The same is true for non-R_MIPS16_26
17937 relocations against MIPS16 functions; in this case, the stub becomes
17938 the function's canonical address.
17939
17940 Floating-point stubs are stored in unique .mips16.call.* or
17941 .mips16.fn.* sections. If a stub T for function F is in section S,
17942 the first relocation in section S must be against F; this is how the
17943 linker determines the target function. All relocations that might
17944 resolve to T must also be against F. We therefore have the following
17945 restrictions, which are given in an intentionally-redundant way:
17946
17947 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17948 symbols.
17949
17950 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17951 if that stub might be used.
17952
17953 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17954 symbols.
17955
17956 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17957 that stub might be used.
17958
17959 There is a further restriction:
17960
df58fc94
RS
17961 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17962 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17963 targets with in-place addends; the relocation field cannot
b314ec0e
RS
17964 encode the low bit.
17965
df58fc94
RS
17966 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17967 against a MIPS16 symbol. We deal with (5) by by not reducing any
17968 such relocations on REL targets.
b314ec0e
RS
17969
17970 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17971 relocation against some symbol R, no relocation against R may be
17972 reduced. (Note that this deals with (2) as well as (1) because
17973 relocations against global symbols will never be reduced on ELF
17974 targets.) This approach is a little simpler than trying to detect
17975 stub sections, and gives the "all or nothing" per-symbol consistency
17976 that we have for MIPS16 symbols. */
f43abd2b 17977 if (IS_ELF
b314ec0e 17978 && fixp->fx_subsy == NULL
30c09090 17979 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
df58fc94
RS
17980 || *symbol_get_tc (fixp->fx_addsy)
17981 || (HAVE_IN_PLACE_ADDENDS
17982 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17983 && jmp_reloc_p (fixp->fx_r_type))))
252b5132
RH
17984 return 0;
17985#endif
a161fe53 17986
252b5132
RH
17987 return 1;
17988}
17989
17990/* Translate internal representation of relocation info to BFD target
17991 format. */
17992
17993arelent **
17a2f251 17994tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
17995{
17996 static arelent *retval[4];
17997 arelent *reloc;
17998 bfd_reloc_code_real_type code;
17999
4b0cff4e
TS
18000 memset (retval, 0, sizeof(retval));
18001 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
18002 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
18003 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
18004 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18005
bad36eac
DJ
18006 if (fixp->fx_pcrel)
18007 {
df58fc94
RS
18008 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18009 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18010 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
18011 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18012 || fixp->fx_r_type == BFD_RELOC_32_PCREL);
bad36eac
DJ
18013
18014 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18015 Relocations want only the symbol offset. */
18016 reloc->addend = fixp->fx_addnumber + reloc->address;
f43abd2b 18017 if (!IS_ELF)
bad36eac
DJ
18018 {
18019 /* A gruesome hack which is a result of the gruesome gas
18020 reloc handling. What's worse, for COFF (as opposed to
18021 ECOFF), we might need yet another copy of reloc->address.
18022 See bfd_install_relocation. */
18023 reloc->addend += reloc->address;
18024 }
18025 }
18026 else
18027 reloc->addend = fixp->fx_addnumber;
252b5132 18028
438c16b8
TS
18029 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18030 entry to be used in the relocation's section offset. */
18031 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18032 {
18033 reloc->address = reloc->addend;
18034 reloc->addend = 0;
18035 }
18036
252b5132 18037 code = fixp->fx_r_type;
252b5132 18038
bad36eac 18039 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18040 if (reloc->howto == NULL)
18041 {
18042 as_bad_where (fixp->fx_file, fixp->fx_line,
18043 _("Can not represent %s relocation in this object file format"),
18044 bfd_get_reloc_code_name (code));
18045 retval[0] = NULL;
18046 }
18047
18048 return retval;
18049}
18050
18051/* Relax a machine dependent frag. This returns the amount by which
18052 the current size of the frag should change. */
18053
18054int
17a2f251 18055mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18056{
4a6a3df4
AO
18057 if (RELAX_BRANCH_P (fragp->fr_subtype))
18058 {
18059 offsetT old_var = fragp->fr_var;
b34976b6
AM
18060
18061 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
18062
18063 return fragp->fr_var - old_var;
18064 }
18065
df58fc94
RS
18066 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18067 {
18068 offsetT old_var = fragp->fr_var;
18069 offsetT new_var = 4;
18070
18071 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18072 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18073 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18074 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18075 fragp->fr_var = new_var;
18076
18077 return new_var - old_var;
18078 }
18079
252b5132
RH
18080 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18081 return 0;
18082
c4e7957c 18083 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
18084 {
18085 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18086 return 0;
18087 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18088 return 2;
18089 }
18090 else
18091 {
18092 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18093 return 0;
18094 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18095 return -2;
18096 }
18097
18098 return 0;
18099}
18100
18101/* Convert a machine dependent frag. */
18102
18103void
17a2f251 18104md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18105{
4a6a3df4
AO
18106 if (RELAX_BRANCH_P (fragp->fr_subtype))
18107 {
4d68580a 18108 char *buf;
4a6a3df4
AO
18109 unsigned long insn;
18110 expressionS exp;
18111 fixS *fixp;
b34976b6 18112
4d68580a
RS
18113 buf = fragp->fr_literal + fragp->fr_fix;
18114 insn = read_insn (buf);
b34976b6 18115
4a6a3df4
AO
18116 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18117 {
18118 /* We generate a fixup instead of applying it right now
18119 because, if there are linker relaxations, we're going to
18120 need the relocations. */
18121 exp.X_op = O_symbol;
18122 exp.X_add_symbol = fragp->fr_symbol;
18123 exp.X_add_number = fragp->fr_offset;
18124
4d68580a
RS
18125 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18126 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18127 fixp->fx_file = fragp->fr_file;
18128 fixp->fx_line = fragp->fr_line;
b34976b6 18129
4d68580a 18130 buf = write_insn (buf, insn);
4a6a3df4
AO
18131 }
18132 else
18133 {
18134 int i;
18135
18136 as_warn_where (fragp->fr_file, fragp->fr_line,
5c4f07ba 18137 _("Relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18138
18139 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18140 goto uncond;
18141
18142 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18143 {
18144 /* Reverse the branch. */
18145 switch ((insn >> 28) & 0xf)
18146 {
18147 case 4:
18148 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
18149 have the condition reversed by tweaking a single
18150 bit, and their opcodes all have 0x4???????. */
9c2799c2 18151 gas_assert ((insn & 0xf1000000) == 0x41000000);
4a6a3df4
AO
18152 insn ^= 0x00010000;
18153 break;
18154
18155 case 0:
18156 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18157 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18158 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18159 insn ^= 0x00010000;
18160 break;
b34976b6 18161
4a6a3df4
AO
18162 case 1:
18163 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18164 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18165 insn ^= 0x04000000;
18166 break;
18167
18168 default:
18169 abort ();
18170 }
18171 }
18172
18173 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18174 {
18175 /* Clear the and-link bit. */
9c2799c2 18176 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18177
54f4ddb3
TS
18178 /* bltzal 0x04100000 bgezal 0x04110000
18179 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18180 insn &= ~0x00100000;
18181 }
18182
18183 /* Branch over the branch (if the branch was likely) or the
18184 full jump (not likely case). Compute the offset from the
18185 current instruction to branch to. */
18186 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18187 i = 16;
18188 else
18189 {
18190 /* How many bytes in instructions we've already emitted? */
4d68580a 18191 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18192 /* How many bytes in instructions from here to the end? */
18193 i = fragp->fr_var - i;
18194 }
18195 /* Convert to instruction count. */
18196 i >>= 2;
18197 /* Branch counts from the next instruction. */
b34976b6 18198 i--;
4a6a3df4
AO
18199 insn |= i;
18200 /* Branch over the jump. */
4d68580a 18201 buf = write_insn (buf, insn);
4a6a3df4 18202
54f4ddb3 18203 /* nop */
4d68580a 18204 buf = write_insn (buf, 0);
4a6a3df4
AO
18205
18206 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18207 {
18208 /* beql $0, $0, 2f */
18209 insn = 0x50000000;
18210 /* Compute the PC offset from the current instruction to
18211 the end of the variable frag. */
18212 /* How many bytes in instructions we've already emitted? */
4d68580a 18213 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18214 /* How many bytes in instructions from here to the end? */
18215 i = fragp->fr_var - i;
18216 /* Convert to instruction count. */
18217 i >>= 2;
18218 /* Don't decrement i, because we want to branch over the
18219 delay slot. */
4a6a3df4 18220 insn |= i;
4a6a3df4 18221
4d68580a
RS
18222 buf = write_insn (buf, insn);
18223 buf = write_insn (buf, 0);
4a6a3df4
AO
18224 }
18225
18226 uncond:
18227 if (mips_pic == NO_PIC)
18228 {
18229 /* j or jal. */
18230 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18231 ? 0x0c000000 : 0x08000000);
18232 exp.X_op = O_symbol;
18233 exp.X_add_symbol = fragp->fr_symbol;
18234 exp.X_add_number = fragp->fr_offset;
18235
4d68580a
RS
18236 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18237 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18238 fixp->fx_file = fragp->fr_file;
18239 fixp->fx_line = fragp->fr_line;
18240
4d68580a 18241 buf = write_insn (buf, insn);
4a6a3df4
AO
18242 }
18243 else
18244 {
66b3e8da
MR
18245 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18246
4a6a3df4 18247 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18248 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18249 insn |= at << OP_SH_RT;
4a6a3df4
AO
18250 exp.X_op = O_symbol;
18251 exp.X_add_symbol = fragp->fr_symbol;
18252 exp.X_add_number = fragp->fr_offset;
18253
18254 if (fragp->fr_offset)
18255 {
18256 exp.X_add_symbol = make_expr_symbol (&exp);
18257 exp.X_add_number = 0;
18258 }
18259
4d68580a
RS
18260 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18261 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18262 fixp->fx_file = fragp->fr_file;
18263 fixp->fx_line = fragp->fr_line;
18264
4d68580a 18265 buf = write_insn (buf, insn);
b34976b6 18266
4a6a3df4 18267 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18268 /* nop */
18269 buf = write_insn (buf, 0);
4a6a3df4
AO
18270
18271 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18272 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18273 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18274
4d68580a
RS
18275 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18276 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18277 fixp->fx_file = fragp->fr_file;
18278 fixp->fx_line = fragp->fr_line;
b34976b6 18279
4d68580a 18280 buf = write_insn (buf, insn);
4a6a3df4
AO
18281
18282 /* j(al)r $at. */
18283 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18284 insn = 0x0000f809;
4a6a3df4 18285 else
66b3e8da
MR
18286 insn = 0x00000008;
18287 insn |= at << OP_SH_RS;
4a6a3df4 18288
4d68580a 18289 buf = write_insn (buf, insn);
4a6a3df4
AO
18290 }
18291 }
18292
4a6a3df4 18293 fragp->fr_fix += fragp->fr_var;
4d68580a 18294 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18295 return;
18296 }
18297
df58fc94
RS
18298 /* Relax microMIPS branches. */
18299 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18300 {
4d68580a 18301 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18302 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18303 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18304 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18305 bfd_boolean short_ds;
df58fc94
RS
18306 unsigned long insn;
18307 expressionS exp;
18308 fixS *fixp;
18309
18310 exp.X_op = O_symbol;
18311 exp.X_add_symbol = fragp->fr_symbol;
18312 exp.X_add_number = fragp->fr_offset;
18313
18314 fragp->fr_fix += fragp->fr_var;
18315
18316 /* Handle 16-bit branches that fit or are forced to fit. */
18317 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18318 {
18319 /* We generate a fixup instead of applying it right now,
18320 because if there is linker relaxation, we're going to
18321 need the relocations. */
18322 if (type == 'D')
4d68580a 18323 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18324 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18325 else if (type == 'E')
4d68580a 18326 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18327 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18328 else
18329 abort ();
18330
18331 fixp->fx_file = fragp->fr_file;
18332 fixp->fx_line = fragp->fr_line;
18333
18334 /* These relocations can have an addend that won't fit in
18335 2 octets. */
18336 fixp->fx_no_overflow = 1;
18337
18338 return;
18339 }
18340
2309ddf2 18341 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18342 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18343 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18344 {
18345 /* We generate a fixup instead of applying it right now,
18346 because if there is linker relaxation, we're going to
18347 need the relocations. */
4d68580a
RS
18348 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18349 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18350 fixp->fx_file = fragp->fr_file;
18351 fixp->fx_line = fragp->fr_line;
18352
18353 if (type == 0)
18354 return;
18355 }
18356
18357 /* Relax 16-bit branches to 32-bit branches. */
18358 if (type != 0)
18359 {
4d68580a 18360 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18361
18362 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18363 insn = 0x94000000; /* beq */
18364 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18365 {
18366 unsigned long regno;
18367
18368 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18369 regno = micromips_to_32_reg_d_map [regno];
18370 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18371 insn |= regno << MICROMIPSOP_SH_RS;
18372 }
18373 else
18374 abort ();
18375
18376 /* Nothing else to do, just write it out. */
18377 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18378 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18379 {
4d68580a
RS
18380 buf = write_compressed_insn (buf, insn, 4);
18381 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18382 return;
18383 }
18384 }
18385 else
4d68580a 18386 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18387
18388 /* Relax 32-bit branches to a sequence of instructions. */
18389 as_warn_where (fragp->fr_file, fragp->fr_line,
18390 _("Relaxed out-of-range branch into a jump"));
18391
2309ddf2
MR
18392 /* Set the short-delay-slot bit. */
18393 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
18394
18395 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18396 {
18397 symbolS *l;
18398
18399 /* Reverse the branch. */
18400 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18401 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18402 insn ^= 0x20000000;
18403 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18404 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18405 || (insn & 0xffe00000) == 0x40800000 /* blez */
18406 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18407 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18408 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18409 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18410 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18411 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18412 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18413 insn ^= 0x00400000;
18414 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18415 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18416 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18417 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18418 insn ^= 0x00200000;
18419 else
18420 abort ();
18421
18422 if (al)
18423 {
18424 /* Clear the and-link and short-delay-slot bits. */
18425 gas_assert ((insn & 0xfda00000) == 0x40200000);
18426
18427 /* bltzal 0x40200000 bgezal 0x40600000 */
18428 /* bltzals 0x42200000 bgezals 0x42600000 */
18429 insn &= ~0x02200000;
18430 }
18431
18432 /* Make a label at the end for use with the branch. */
18433 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18434 micromips_label_inc ();
18435#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
18436 if (IS_ELF)
18437 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18438#endif
18439
18440 /* Refer to it. */
4d68580a
RS
18441 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18442 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18443 fixp->fx_file = fragp->fr_file;
18444 fixp->fx_line = fragp->fr_line;
18445
18446 /* Branch over the jump. */
4d68580a 18447 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18448 if (!compact)
4d68580a
RS
18449 /* nop */
18450 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
18451 }
18452
18453 if (mips_pic == NO_PIC)
18454 {
2309ddf2
MR
18455 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
18456
df58fc94
RS
18457 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18458 insn = al ? jal : 0xd4000000;
18459
4d68580a
RS
18460 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18461 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18462 fixp->fx_file = fragp->fr_file;
18463 fixp->fx_line = fragp->fr_line;
18464
4d68580a 18465 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18466 if (compact)
4d68580a
RS
18467 /* nop */
18468 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
18469 }
18470 else
18471 {
18472 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
18473 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18474 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
18475
18476 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18477 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18478 insn |= at << MICROMIPSOP_SH_RT;
18479
18480 if (exp.X_add_number)
18481 {
18482 exp.X_add_symbol = make_expr_symbol (&exp);
18483 exp.X_add_number = 0;
18484 }
18485
4d68580a
RS
18486 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18487 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18488 fixp->fx_file = fragp->fr_file;
18489 fixp->fx_line = fragp->fr_line;
18490
4d68580a 18491 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18492
18493 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18494 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18495 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18496
4d68580a
RS
18497 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18498 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18499 fixp->fx_file = fragp->fr_file;
18500 fixp->fx_line = fragp->fr_line;
18501
4d68580a 18502 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18503
18504 /* jr/jrc/jalr/jalrs $at */
18505 insn = al ? jalr : jr;
18506 insn |= at << MICROMIPSOP_SH_MJ;
18507
4d68580a 18508 buf = write_compressed_insn (buf, insn, 2);
df58fc94
RS
18509 }
18510
4d68580a 18511 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18512 return;
18513 }
18514
252b5132
RH
18515 if (RELAX_MIPS16_P (fragp->fr_subtype))
18516 {
18517 int type;
3994f87e 18518 const struct mips16_immed_operand *op;
252b5132 18519 offsetT val;
5c04167a
RS
18520 char *buf;
18521 unsigned int user_length, length;
252b5132 18522 unsigned long insn;
5c04167a 18523 bfd_boolean ext;
252b5132
RH
18524
18525 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18526 op = mips16_immed_operands;
18527 while (op->type != type)
18528 ++op;
18529
5c04167a 18530 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
5f5f22c0 18531 val = resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
18532 if (op->pcrel)
18533 {
18534 addressT addr;
18535
18536 addr = fragp->fr_address + fragp->fr_fix;
18537
18538 /* The rules for the base address of a PC relative reloc are
18539 complicated; see mips16_extended_frag. */
18540 if (type == 'p' || type == 'q')
18541 {
18542 addr += 2;
18543 if (ext)
18544 addr += 2;
18545 /* Ignore the low bit in the target, since it will be
18546 set for a text label. */
18547 if ((val & 1) != 0)
18548 --val;
18549 }
18550 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18551 addr -= 4;
18552 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18553 addr -= 2;
18554
18555 addr &= ~ (addressT) ((1 << op->shift) - 1);
18556 val -= addr;
18557
18558 /* Make sure the section winds up with the alignment we have
18559 assumed. */
18560 if (op->shift > 0)
18561 record_alignment (asec, op->shift);
18562 }
18563
18564 if (ext
18565 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18566 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18567 as_warn_where (fragp->fr_file, fragp->fr_line,
18568 _("extended instruction in delay slot"));
18569
5c04167a 18570 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 18571
4d68580a 18572 insn = read_compressed_insn (buf, 2);
5c04167a
RS
18573 if (ext)
18574 insn |= MIPS16_EXTEND;
252b5132 18575
5c04167a
RS
18576 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18577 user_length = 4;
18578 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18579 user_length = 2;
18580 else
18581 user_length = 0;
18582
43c0598f 18583 mips16_immed (fragp->fr_file, fragp->fr_line, type,
c150d1d2 18584 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 18585
5c04167a
RS
18586 length = (ext ? 4 : 2);
18587 gas_assert (mips16_opcode_length (insn) == length);
18588 write_compressed_insn (buf, insn, length);
18589 fragp->fr_fix += length;
252b5132
RH
18590 }
18591 else
18592 {
df58fc94
RS
18593 relax_substateT subtype = fragp->fr_subtype;
18594 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18595 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
18596 int first, second;
18597 fixS *fixp;
252b5132 18598
df58fc94
RS
18599 first = RELAX_FIRST (subtype);
18600 second = RELAX_SECOND (subtype);
4d7206a2 18601 fixp = (fixS *) fragp->fr_opcode;
252b5132 18602
df58fc94
RS
18603 /* If the delay slot chosen does not match the size of the instruction,
18604 then emit a warning. */
18605 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18606 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18607 {
18608 relax_substateT s;
18609 const char *msg;
18610
18611 s = subtype & (RELAX_DELAY_SLOT_16BIT
18612 | RELAX_DELAY_SLOT_SIZE_FIRST
18613 | RELAX_DELAY_SLOT_SIZE_SECOND);
18614 msg = macro_warning (s);
18615 if (msg != NULL)
db9b2be4 18616 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
18617 subtype &= ~s;
18618 }
18619
584892a6 18620 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 18621 if (use_second == second_longer)
584892a6 18622 {
df58fc94
RS
18623 relax_substateT s;
18624 const char *msg;
18625
18626 s = (subtype
18627 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18628 msg = macro_warning (s);
18629 if (msg != NULL)
db9b2be4 18630 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 18631 subtype &= ~s;
584892a6
RS
18632 }
18633
4d7206a2
RS
18634 /* Go through all the fixups for the first sequence. Disable them
18635 (by marking them as done) if we're going to use the second
18636 sequence instead. */
18637 while (fixp
18638 && fixp->fx_frag == fragp
18639 && fixp->fx_where < fragp->fr_fix - second)
18640 {
df58fc94 18641 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18642 fixp->fx_done = 1;
18643 fixp = fixp->fx_next;
18644 }
252b5132 18645
4d7206a2
RS
18646 /* Go through the fixups for the second sequence. Disable them if
18647 we're going to use the first sequence, otherwise adjust their
18648 addresses to account for the relaxation. */
18649 while (fixp && fixp->fx_frag == fragp)
18650 {
df58fc94 18651 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18652 fixp->fx_where -= first;
18653 else
18654 fixp->fx_done = 1;
18655 fixp = fixp->fx_next;
18656 }
18657
18658 /* Now modify the frag contents. */
df58fc94 18659 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18660 {
18661 char *start;
18662
18663 start = fragp->fr_literal + fragp->fr_fix - first - second;
18664 memmove (start, start + first, second);
18665 fragp->fr_fix -= first;
18666 }
18667 else
18668 fragp->fr_fix -= second;
252b5132
RH
18669 }
18670}
18671
18672#ifdef OBJ_ELF
18673
18674/* This function is called after the relocs have been generated.
18675 We've been storing mips16 text labels as odd. Here we convert them
18676 back to even for the convenience of the debugger. */
18677
18678void
17a2f251 18679mips_frob_file_after_relocs (void)
252b5132
RH
18680{
18681 asymbol **syms;
18682 unsigned int count, i;
18683
f43abd2b 18684 if (!IS_ELF)
252b5132
RH
18685 return;
18686
18687 syms = bfd_get_outsymbols (stdoutput);
18688 count = bfd_get_symcount (stdoutput);
18689 for (i = 0; i < count; i++, syms++)
df58fc94
RS
18690 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18691 && ((*syms)->value & 1) != 0)
18692 {
18693 (*syms)->value &= ~1;
18694 /* If the symbol has an odd size, it was probably computed
18695 incorrectly, so adjust that as well. */
18696 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18697 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18698 }
252b5132
RH
18699}
18700
18701#endif
18702
a1facbec
MR
18703/* This function is called whenever a label is defined, including fake
18704 labels instantiated off the dot special symbol. It is used when
18705 handling branch delays; if a branch has a label, we assume we cannot
18706 move it. This also bumps the value of the symbol by 1 in compressed
18707 code. */
252b5132 18708
e1b47bd5 18709static void
a1facbec 18710mips_record_label (symbolS *sym)
252b5132 18711{
a8dbcb85 18712 segment_info_type *si = seg_info (now_seg);
252b5132
RH
18713 struct insn_label_list *l;
18714
18715 if (free_insn_labels == NULL)
18716 l = (struct insn_label_list *) xmalloc (sizeof *l);
18717 else
18718 {
18719 l = free_insn_labels;
18720 free_insn_labels = l->next;
18721 }
18722
18723 l->label = sym;
a8dbcb85
TS
18724 l->next = si->label_list;
18725 si->label_list = l;
a1facbec 18726}
07a53e5c 18727
a1facbec
MR
18728/* This function is called as tc_frob_label() whenever a label is defined
18729 and adds a DWARF-2 record we only want for true labels. */
18730
18731void
18732mips_define_label (symbolS *sym)
18733{
18734 mips_record_label (sym);
07a53e5c
RH
18735#ifdef OBJ_ELF
18736 dwarf2_emit_label (sym);
18737#endif
252b5132 18738}
e1b47bd5
RS
18739
18740/* This function is called by tc_new_dot_label whenever a new dot symbol
18741 is defined. */
18742
18743void
18744mips_add_dot_label (symbolS *sym)
18745{
18746 mips_record_label (sym);
18747 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18748 mips_compressed_mark_label (sym);
18749}
252b5132
RH
18750\f
18751#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
18752
18753/* Some special processing for a MIPS ELF file. */
18754
18755void
17a2f251 18756mips_elf_final_processing (void)
252b5132
RH
18757{
18758 /* Write out the register information. */
316f5878 18759 if (mips_abi != N64_ABI)
252b5132
RH
18760 {
18761 Elf32_RegInfo s;
18762
18763 s.ri_gprmask = mips_gprmask;
18764 s.ri_cprmask[0] = mips_cprmask[0];
18765 s.ri_cprmask[1] = mips_cprmask[1];
18766 s.ri_cprmask[2] = mips_cprmask[2];
18767 s.ri_cprmask[3] = mips_cprmask[3];
18768 /* The gp_value field is set by the MIPS ELF backend. */
18769
18770 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18771 ((Elf32_External_RegInfo *)
18772 mips_regmask_frag));
18773 }
18774 else
18775 {
18776 Elf64_Internal_RegInfo s;
18777
18778 s.ri_gprmask = mips_gprmask;
18779 s.ri_pad = 0;
18780 s.ri_cprmask[0] = mips_cprmask[0];
18781 s.ri_cprmask[1] = mips_cprmask[1];
18782 s.ri_cprmask[2] = mips_cprmask[2];
18783 s.ri_cprmask[3] = mips_cprmask[3];
18784 /* The gp_value field is set by the MIPS ELF backend. */
18785
18786 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18787 ((Elf64_External_RegInfo *)
18788 mips_regmask_frag));
18789 }
18790
18791 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18792 sort of BFD interface for this. */
18793 if (mips_any_noreorder)
18794 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18795 if (mips_pic != NO_PIC)
143d77c5 18796 {
8b828383 18797 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
18798 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18799 }
18800 if (mips_abicalls)
18801 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 18802
b015e599
AP
18803 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
18804 defined at present; this might need to change in future. */
a4672219
TS
18805 if (file_ase_mips16)
18806 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
18807 if (file_ase_micromips)
18808 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
1f25f5d3
CD
18809#if 0 /* XXX FIXME */
18810 if (file_ase_mips3d)
18811 elf_elfheader (stdoutput)->e_flags |= ???;
18812#endif
deec1734
CD
18813 if (file_ase_mdmx)
18814 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 18815
bdaaa2e1 18816 /* Set the MIPS ELF ABI flags. */
316f5878 18817 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 18818 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 18819 else if (mips_abi == O64_ABI)
252b5132 18820 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 18821 else if (mips_abi == EABI_ABI)
252b5132 18822 {
316f5878 18823 if (!file_mips_gp32)
252b5132
RH
18824 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18825 else
18826 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18827 }
316f5878 18828 else if (mips_abi == N32_ABI)
be00bddd
TS
18829 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18830
c9914766 18831 /* Nothing to do for N64_ABI. */
252b5132
RH
18832
18833 if (mips_32bitmode)
18834 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08
TS
18835
18836#if 0 /* XXX FIXME */
18837 /* 32 bit code with 64 bit FP registers. */
18838 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18839 elf_elfheader (stdoutput)->e_flags |= ???;
18840#endif
252b5132
RH
18841}
18842
18843#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
18844\f
beae10d5 18845typedef struct proc {
9b2f1d35
EC
18846 symbolS *func_sym;
18847 symbolS *func_end_sym;
beae10d5
KH
18848 unsigned long reg_mask;
18849 unsigned long reg_offset;
18850 unsigned long fpreg_mask;
18851 unsigned long fpreg_offset;
18852 unsigned long frame_offset;
18853 unsigned long frame_reg;
18854 unsigned long pc_reg;
18855} procS;
252b5132
RH
18856
18857static procS cur_proc;
18858static procS *cur_proc_ptr;
18859static int numprocs;
18860
df58fc94
RS
18861/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
18862 as "2", and a normal nop as "0". */
18863
18864#define NOP_OPCODE_MIPS 0
18865#define NOP_OPCODE_MIPS16 1
18866#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
18867
18868char
18869mips_nop_opcode (void)
18870{
df58fc94
RS
18871 if (seg_info (now_seg)->tc_segment_info_data.micromips)
18872 return NOP_OPCODE_MICROMIPS;
18873 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18874 return NOP_OPCODE_MIPS16;
18875 else
18876 return NOP_OPCODE_MIPS;
742a56fe
RS
18877}
18878
df58fc94
RS
18879/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
18880 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 18881
0a9ef439 18882void
17a2f251 18883mips_handle_align (fragS *fragp)
a19d8eb0 18884{
df58fc94 18885 char nop_opcode;
742a56fe 18886 char *p;
c67a084a
NC
18887 int bytes, size, excess;
18888 valueT opcode;
742a56fe 18889
0a9ef439
RH
18890 if (fragp->fr_type != rs_align_code)
18891 return;
18892
742a56fe 18893 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18894 nop_opcode = *p;
18895 switch (nop_opcode)
a19d8eb0 18896 {
df58fc94
RS
18897 case NOP_OPCODE_MICROMIPS:
18898 opcode = micromips_nop32_insn.insn_opcode;
18899 size = 4;
18900 break;
18901 case NOP_OPCODE_MIPS16:
c67a084a
NC
18902 opcode = mips16_nop_insn.insn_opcode;
18903 size = 2;
df58fc94
RS
18904 break;
18905 case NOP_OPCODE_MIPS:
18906 default:
c67a084a
NC
18907 opcode = nop_insn.insn_opcode;
18908 size = 4;
df58fc94 18909 break;
c67a084a 18910 }
a19d8eb0 18911
c67a084a
NC
18912 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18913 excess = bytes % size;
df58fc94
RS
18914
18915 /* Handle the leading part if we're not inserting a whole number of
18916 instructions, and make it the end of the fixed part of the frag.
18917 Try to fit in a short microMIPS NOP if applicable and possible,
18918 and use zeroes otherwise. */
18919 gas_assert (excess < 4);
18920 fragp->fr_fix += excess;
18921 switch (excess)
c67a084a 18922 {
df58fc94
RS
18923 case 3:
18924 *p++ = '\0';
18925 /* Fall through. */
18926 case 2:
18927 if (nop_opcode == NOP_OPCODE_MICROMIPS)
18928 {
4d68580a 18929 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
18930 break;
18931 }
18932 *p++ = '\0';
18933 /* Fall through. */
18934 case 1:
18935 *p++ = '\0';
18936 /* Fall through. */
18937 case 0:
18938 break;
a19d8eb0 18939 }
c67a084a
NC
18940
18941 md_number_to_chars (p, opcode, size);
18942 fragp->fr_var = size;
a19d8eb0
CP
18943}
18944
252b5132 18945static void
17a2f251 18946md_obj_begin (void)
252b5132
RH
18947{
18948}
18949
18950static void
17a2f251 18951md_obj_end (void)
252b5132 18952{
54f4ddb3 18953 /* Check for premature end, nesting errors, etc. */
252b5132 18954 if (cur_proc_ptr)
9a41af64 18955 as_warn (_("missing .end at end of assembly"));
252b5132
RH
18956}
18957
18958static long
17a2f251 18959get_number (void)
252b5132
RH
18960{
18961 int negative = 0;
18962 long val = 0;
18963
18964 if (*input_line_pointer == '-')
18965 {
18966 ++input_line_pointer;
18967 negative = 1;
18968 }
3882b010 18969 if (!ISDIGIT (*input_line_pointer))
956cd1d6 18970 as_bad (_("expected simple number"));
252b5132
RH
18971 if (input_line_pointer[0] == '0')
18972 {
18973 if (input_line_pointer[1] == 'x')
18974 {
18975 input_line_pointer += 2;
3882b010 18976 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
18977 {
18978 val <<= 4;
18979 val |= hex_value (*input_line_pointer++);
18980 }
18981 return negative ? -val : val;
18982 }
18983 else
18984 {
18985 ++input_line_pointer;
3882b010 18986 while (ISDIGIT (*input_line_pointer))
252b5132
RH
18987 {
18988 val <<= 3;
18989 val |= *input_line_pointer++ - '0';
18990 }
18991 return negative ? -val : val;
18992 }
18993 }
3882b010 18994 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
18995 {
18996 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18997 *input_line_pointer, *input_line_pointer);
956cd1d6 18998 as_warn (_("invalid number"));
252b5132
RH
18999 return -1;
19000 }
3882b010 19001 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19002 {
19003 val *= 10;
19004 val += *input_line_pointer++ - '0';
19005 }
19006 return negative ? -val : val;
19007}
19008
19009/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19010 is an initial number which is the ECOFF file index. In the non-ECOFF
19011 case .file implies DWARF-2. */
19012
19013static void
17a2f251 19014s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19015{
ecb4347a
DJ
19016 static int first_file_directive = 0;
19017
c5dd6aab
DJ
19018 if (ECOFF_DEBUGGING)
19019 {
19020 get_number ();
19021 s_app_file (0);
19022 }
19023 else
ecb4347a
DJ
19024 {
19025 char *filename;
19026
19027 filename = dwarf2_directive_file (0);
19028
19029 /* Versions of GCC up to 3.1 start files with a ".file"
19030 directive even for stabs output. Make sure that this
19031 ".file" is handled. Note that you need a version of GCC
19032 after 3.1 in order to support DWARF-2 on MIPS. */
19033 if (filename != NULL && ! first_file_directive)
19034 {
19035 (void) new_logical_line (filename, -1);
c04f5787 19036 s_app_file_string (filename, 0);
ecb4347a
DJ
19037 }
19038 first_file_directive = 1;
19039 }
c5dd6aab
DJ
19040}
19041
19042/* The .loc directive, implying DWARF-2. */
252b5132
RH
19043
19044static void
17a2f251 19045s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19046{
c5dd6aab
DJ
19047 if (!ECOFF_DEBUGGING)
19048 dwarf2_directive_loc (0);
252b5132
RH
19049}
19050
252b5132
RH
19051/* The .end directive. */
19052
19053static void
17a2f251 19054s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19055{
19056 symbolS *p;
252b5132 19057
7a621144
DJ
19058 /* Following functions need their own .frame and .cprestore directives. */
19059 mips_frame_reg_valid = 0;
19060 mips_cprestore_valid = 0;
19061
252b5132
RH
19062 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19063 {
19064 p = get_symbol ();
19065 demand_empty_rest_of_line ();
19066 }
19067 else
19068 p = NULL;
19069
14949570 19070 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19071 as_warn (_(".end not in text section"));
19072
19073 if (!cur_proc_ptr)
19074 {
19075 as_warn (_(".end directive without a preceding .ent directive."));
19076 demand_empty_rest_of_line ();
19077 return;
19078 }
19079
19080 if (p != NULL)
19081 {
9c2799c2 19082 gas_assert (S_GET_NAME (p));
9b2f1d35 19083 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 19084 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
19085
19086 if (debug_type == DEBUG_STABS)
19087 stabs_generate_asm_endfunc (S_GET_NAME (p),
19088 S_GET_NAME (p));
252b5132
RH
19089 }
19090 else
19091 as_warn (_(".end directive missing or unknown symbol"));
19092
2132e3a3 19093#ifdef OBJ_ELF
9b2f1d35
EC
19094 /* Create an expression to calculate the size of the function. */
19095 if (p && cur_proc_ptr)
19096 {
19097 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19098 expressionS *exp = xmalloc (sizeof (expressionS));
19099
19100 obj->size = exp;
19101 exp->X_op = O_subtract;
19102 exp->X_add_symbol = symbol_temp_new_now ();
19103 exp->X_op_symbol = p;
19104 exp->X_add_number = 0;
19105
19106 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19107 }
19108
ecb4347a 19109 /* Generate a .pdr section. */
f43abd2b 19110 if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19111 {
19112 segT saved_seg = now_seg;
19113 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19114 expressionS exp;
19115 char *fragp;
252b5132 19116
252b5132 19117#ifdef md_flush_pending_output
ecb4347a 19118 md_flush_pending_output ();
252b5132
RH
19119#endif
19120
9c2799c2 19121 gas_assert (pdr_seg);
ecb4347a 19122 subseg_set (pdr_seg, 0);
252b5132 19123
ecb4347a
DJ
19124 /* Write the symbol. */
19125 exp.X_op = O_symbol;
19126 exp.X_add_symbol = p;
19127 exp.X_add_number = 0;
19128 emit_expr (&exp, 4);
252b5132 19129
ecb4347a 19130 fragp = frag_more (7 * 4);
252b5132 19131
17a2f251
TS
19132 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19133 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19134 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19135 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19136 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19137 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19138 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19139
ecb4347a
DJ
19140 subseg_set (saved_seg, saved_subseg);
19141 }
19142#endif /* OBJ_ELF */
252b5132
RH
19143
19144 cur_proc_ptr = NULL;
19145}
19146
19147/* The .aent and .ent directives. */
19148
19149static void
17a2f251 19150s_mips_ent (int aent)
252b5132 19151{
252b5132 19152 symbolS *symbolP;
252b5132
RH
19153
19154 symbolP = get_symbol ();
19155 if (*input_line_pointer == ',')
f9419b05 19156 ++input_line_pointer;
252b5132 19157 SKIP_WHITESPACE ();
3882b010 19158 if (ISDIGIT (*input_line_pointer)
d9a62219 19159 || *input_line_pointer == '-')
874e8986 19160 get_number ();
252b5132 19161
14949570 19162 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19163 as_warn (_(".ent or .aent not in text section."));
19164
19165 if (!aent && cur_proc_ptr)
9a41af64 19166 as_warn (_("missing .end"));
252b5132
RH
19167
19168 if (!aent)
19169 {
7a621144
DJ
19170 /* This function needs its own .frame and .cprestore directives. */
19171 mips_frame_reg_valid = 0;
19172 mips_cprestore_valid = 0;
19173
252b5132
RH
19174 cur_proc_ptr = &cur_proc;
19175 memset (cur_proc_ptr, '\0', sizeof (procS));
19176
9b2f1d35 19177 cur_proc_ptr->func_sym = symbolP;
252b5132 19178
f9419b05 19179 ++numprocs;
ecb4347a
DJ
19180
19181 if (debug_type == DEBUG_STABS)
19182 stabs_generate_asm_func (S_GET_NAME (symbolP),
19183 S_GET_NAME (symbolP));
252b5132
RH
19184 }
19185
7c0fc524
MR
19186 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19187
252b5132
RH
19188 demand_empty_rest_of_line ();
19189}
19190
19191/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19192 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19193 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19194 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19195 symbol table (in the mdebug section). */
19196
19197static void
17a2f251 19198s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19199{
ecb4347a 19200#ifdef OBJ_ELF
f43abd2b 19201 if (IS_ELF && !ECOFF_DEBUGGING)
ecb4347a
DJ
19202 {
19203 long val;
252b5132 19204
ecb4347a
DJ
19205 if (cur_proc_ptr == (procS *) NULL)
19206 {
19207 as_warn (_(".frame outside of .ent"));
19208 demand_empty_rest_of_line ();
19209 return;
19210 }
252b5132 19211
ecb4347a
DJ
19212 cur_proc_ptr->frame_reg = tc_get_register (1);
19213
19214 SKIP_WHITESPACE ();
19215 if (*input_line_pointer++ != ','
19216 || get_absolute_expression_and_terminator (&val) != ',')
19217 {
19218 as_warn (_("Bad .frame directive"));
19219 --input_line_pointer;
19220 demand_empty_rest_of_line ();
19221 return;
19222 }
252b5132 19223
ecb4347a
DJ
19224 cur_proc_ptr->frame_offset = val;
19225 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19226
252b5132 19227 demand_empty_rest_of_line ();
252b5132 19228 }
ecb4347a
DJ
19229 else
19230#endif /* OBJ_ELF */
19231 s_ignore (ignore);
252b5132
RH
19232}
19233
bdaaa2e1
KH
19234/* The .fmask and .mask directives. If the mdebug section is present
19235 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19236 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19237 information correctly. We can't use the ecoff routines because they
252b5132
RH
19238 make reference to the ecoff symbol table (in the mdebug section). */
19239
19240static void
17a2f251 19241s_mips_mask (int reg_type)
252b5132 19242{
ecb4347a 19243#ifdef OBJ_ELF
f43abd2b 19244 if (IS_ELF && !ECOFF_DEBUGGING)
252b5132 19245 {
ecb4347a 19246 long mask, off;
252b5132 19247
ecb4347a
DJ
19248 if (cur_proc_ptr == (procS *) NULL)
19249 {
19250 as_warn (_(".mask/.fmask outside of .ent"));
19251 demand_empty_rest_of_line ();
19252 return;
19253 }
252b5132 19254
ecb4347a
DJ
19255 if (get_absolute_expression_and_terminator (&mask) != ',')
19256 {
19257 as_warn (_("Bad .mask/.fmask directive"));
19258 --input_line_pointer;
19259 demand_empty_rest_of_line ();
19260 return;
19261 }
252b5132 19262
ecb4347a
DJ
19263 off = get_absolute_expression ();
19264
19265 if (reg_type == 'F')
19266 {
19267 cur_proc_ptr->fpreg_mask = mask;
19268 cur_proc_ptr->fpreg_offset = off;
19269 }
19270 else
19271 {
19272 cur_proc_ptr->reg_mask = mask;
19273 cur_proc_ptr->reg_offset = off;
19274 }
19275
19276 demand_empty_rest_of_line ();
252b5132
RH
19277 }
19278 else
ecb4347a
DJ
19279#endif /* OBJ_ELF */
19280 s_ignore (reg_type);
252b5132
RH
19281}
19282
316f5878
RS
19283/* A table describing all the processors gas knows about. Names are
19284 matched in the order listed.
e7af610e 19285
316f5878
RS
19286 To ease comparison, please keep this table in the same order as
19287 gcc's mips_cpu_info_table[]. */
e972090a
NC
19288static const struct mips_cpu_info mips_cpu_info_table[] =
19289{
316f5878 19290 /* Entries for generic ISAs */
ad3fea08
TS
19291 { "mips1", MIPS_CPU_IS_ISA, ISA_MIPS1, CPU_R3000 },
19292 { "mips2", MIPS_CPU_IS_ISA, ISA_MIPS2, CPU_R6000 },
19293 { "mips3", MIPS_CPU_IS_ISA, ISA_MIPS3, CPU_R4000 },
19294 { "mips4", MIPS_CPU_IS_ISA, ISA_MIPS4, CPU_R8000 },
19295 { "mips5", MIPS_CPU_IS_ISA, ISA_MIPS5, CPU_MIPS5 },
19296 { "mips32", MIPS_CPU_IS_ISA, ISA_MIPS32, CPU_MIPS32 },
19297 { "mips32r2", MIPS_CPU_IS_ISA, ISA_MIPS32R2, CPU_MIPS32R2 },
19298 { "mips64", MIPS_CPU_IS_ISA, ISA_MIPS64, CPU_MIPS64 },
19299 { "mips64r2", MIPS_CPU_IS_ISA, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
19300
19301 /* MIPS I */
ad3fea08
TS
19302 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
19303 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
19304 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19305
19306 /* MIPS II */
ad3fea08 19307 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19308
19309 /* MIPS III */
ad3fea08
TS
19310 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
19311 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
19312 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
19313 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
19314 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
19315 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
19316 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
19317 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
19318 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
19319 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
19320 { "orion", 0, ISA_MIPS3, CPU_R4600 },
19321 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
e407c74b 19322 { "r5900", 0, ISA_MIPS3, CPU_R5900 },
b15591bb
AN
19323 /* ST Microelectronics Loongson 2E and 2F cores */
19324 { "loongson2e", 0, ISA_MIPS3, CPU_LOONGSON_2E },
19325 { "loongson2f", 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19326
19327 /* MIPS IV */
ad3fea08
TS
19328 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
19329 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
19330 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
3aa3176b
TS
19331 { "r14000", 0, ISA_MIPS4, CPU_R14000 },
19332 { "r16000", 0, ISA_MIPS4, CPU_R16000 },
ad3fea08
TS
19333 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
19334 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
19335 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
19336 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
19337 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
19338 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
19339 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
19340 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
19341 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
19342 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19343
19344 /* MIPS 32 */
ad3fea08
TS
19345 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
19346 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
19347 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
19348 { "4ksc", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
19349
19350 /* MIPS 32 Release 2 */
19351 { "4kec", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19352 { "4kem", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19353 { "4kep", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19354 { "4ksd", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19355 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19356 { "m4kp", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
b5503c7b
MR
19357 { "m14k", MIPS_CPU_ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19358 { "m14kc", MIPS_CPU_ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
7a795ef4
MR
19359 { "m14ke", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19360 ISA_MIPS32R2, CPU_MIPS32R2 },
19361 { "m14kec", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19362 ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19363 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19364 { "24kf2_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19365 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19366 { "24kf1_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19367 /* Deprecated forms of the above. */
19368 { "24kfx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19369 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19370 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
ad3fea08 19371 { "24kec", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19372 { "24kef2_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19373 { "24kef", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19374 { "24kef1_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19375 /* Deprecated forms of the above. */
19376 { "24kefx", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
65263ce3 19377 { "24kex", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19378 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
a360e743
TS
19379 { "34kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19380 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19381 { "34kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19382 ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
19383 { "34kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19384 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19385 { "34kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19386 ISA_MIPS32R2, CPU_MIPS32R2 },
19387 /* Deprecated forms of the above. */
19388 { "34kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19389 ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
19390 { "34kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19391 ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4
SL
19392 /* 34Kn is a 34kc without DSP. */
19393 { "34kn", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19394 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
19395 { "74kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19396 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19397 { "74kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19398 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19399 { "74kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19400 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19401 { "74kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19402 ISA_MIPS32R2, CPU_MIPS32R2 },
19403 { "74kf3_2", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19404 ISA_MIPS32R2, CPU_MIPS32R2 },
19405 /* Deprecated forms of the above. */
19406 { "74kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19407 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19408 { "74kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19409 ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a
SL
19410 /* 1004K cores are multiprocessor versions of the 34K. */
19411 { "1004kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19412 ISA_MIPS32R2, CPU_MIPS32R2 },
19413 { "1004kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19414 ISA_MIPS32R2, CPU_MIPS32R2 },
19415 { "1004kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19416 ISA_MIPS32R2, CPU_MIPS32R2 },
19417 { "1004kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19418 ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 19419
316f5878 19420 /* MIPS 64 */
ad3fea08
TS
19421 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
19422 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
19423 { "20kc", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
7764b395 19424 { "25kf", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19425
c7a23324 19426 /* Broadcom SB-1 CPU core */
65263ce3
TS
19427 { "sb1", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19428 ISA_MIPS64, CPU_SB1 },
1e85aad8
JW
19429 /* Broadcom SB-1A CPU core */
19430 { "sb1a", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19431 ISA_MIPS64, CPU_SB1 },
d051516a
NC
19432
19433 { "loongson3a", 0, ISA_MIPS64, CPU_LOONGSON_3A },
e7af610e 19434
ed163775
MR
19435 /* MIPS 64 Release 2 */
19436
967344c6
AN
19437 /* Cavium Networks Octeon CPU core */
19438 { "octeon", 0, ISA_MIPS64R2, CPU_OCTEON },
dd6a37e7 19439 { "octeon+", 0, ISA_MIPS64R2, CPU_OCTEONP },
432233b3 19440 { "octeon2", 0, ISA_MIPS64R2, CPU_OCTEON2 },
967344c6 19441
52b6b6b9
JM
19442 /* RMI Xlr */
19443 { "xlr", 0, ISA_MIPS64, CPU_XLR },
19444
55a36193
MK
19445 /* Broadcom XLP.
19446 XLP is mostly like XLR, with the prominent exception that it is
19447 MIPS64R2 rather than MIPS64. */
19448 { "xlp", 0, ISA_MIPS64R2, CPU_XLR },
19449
316f5878
RS
19450 /* End marker */
19451 { NULL, 0, 0, 0 }
19452};
e7af610e 19453
84ea6cf2 19454
316f5878
RS
19455/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19456 with a final "000" replaced by "k". Ignore case.
e7af610e 19457
316f5878 19458 Note: this function is shared between GCC and GAS. */
c6c98b38 19459
b34976b6 19460static bfd_boolean
17a2f251 19461mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19462{
19463 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19464 given++, canonical++;
19465
19466 return ((*given == 0 && *canonical == 0)
19467 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19468}
19469
19470
19471/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19472 CPU name. We've traditionally allowed a lot of variation here.
19473
19474 Note: this function is shared between GCC and GAS. */
19475
b34976b6 19476static bfd_boolean
17a2f251 19477mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19478{
19479 /* First see if the name matches exactly, or with a final "000"
19480 turned into "k". */
19481 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19482 return TRUE;
316f5878
RS
19483
19484 /* If not, try comparing based on numerical designation alone.
19485 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19486 if (TOLOWER (*given) == 'r')
19487 given++;
19488 if (!ISDIGIT (*given))
b34976b6 19489 return FALSE;
316f5878
RS
19490
19491 /* Skip over some well-known prefixes in the canonical name,
19492 hoping to find a number there too. */
19493 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19494 canonical += 2;
19495 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19496 canonical += 2;
19497 else if (TOLOWER (canonical[0]) == 'r')
19498 canonical += 1;
19499
19500 return mips_strict_matching_cpu_name_p (canonical, given);
19501}
19502
19503
19504/* Parse an option that takes the name of a processor as its argument.
19505 OPTION is the name of the option and CPU_STRING is the argument.
19506 Return the corresponding processor enumeration if the CPU_STRING is
19507 recognized, otherwise report an error and return null.
19508
19509 A similar function exists in GCC. */
e7af610e
NC
19510
19511static const struct mips_cpu_info *
17a2f251 19512mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19513{
316f5878 19514 const struct mips_cpu_info *p;
e7af610e 19515
316f5878
RS
19516 /* 'from-abi' selects the most compatible architecture for the given
19517 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19518 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19519 version. Look first at the -mgp options, if given, otherwise base
19520 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19521
316f5878
RS
19522 Treat NO_ABI like the EABIs. One reason to do this is that the
19523 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19524 architecture. This code picks MIPS I for 'mips' and MIPS III for
19525 'mips64', just as we did in the days before 'from-abi'. */
19526 if (strcasecmp (cpu_string, "from-abi") == 0)
19527 {
19528 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19529 return mips_cpu_info_from_isa (ISA_MIPS1);
19530
19531 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19532 return mips_cpu_info_from_isa (ISA_MIPS3);
19533
19534 if (file_mips_gp32 >= 0)
19535 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19536
19537 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19538 ? ISA_MIPS3
19539 : ISA_MIPS1);
19540 }
19541
19542 /* 'default' has traditionally been a no-op. Probably not very useful. */
19543 if (strcasecmp (cpu_string, "default") == 0)
19544 return 0;
19545
19546 for (p = mips_cpu_info_table; p->name != 0; p++)
19547 if (mips_matching_cpu_name_p (p->name, cpu_string))
19548 return p;
19549
20203fb9 19550 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
316f5878 19551 return 0;
e7af610e
NC
19552}
19553
316f5878
RS
19554/* Return the canonical processor information for ISA (a member of the
19555 ISA_MIPS* enumeration). */
19556
e7af610e 19557static const struct mips_cpu_info *
17a2f251 19558mips_cpu_info_from_isa (int isa)
e7af610e
NC
19559{
19560 int i;
19561
19562 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19563 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19564 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19565 return (&mips_cpu_info_table[i]);
19566
e972090a 19567 return NULL;
e7af610e 19568}
fef14a42
TS
19569
19570static const struct mips_cpu_info *
17a2f251 19571mips_cpu_info_from_arch (int arch)
fef14a42
TS
19572{
19573 int i;
19574
19575 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19576 if (arch == mips_cpu_info_table[i].cpu)
19577 return (&mips_cpu_info_table[i]);
19578
19579 return NULL;
19580}
316f5878
RS
19581\f
19582static void
17a2f251 19583show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19584{
19585 if (*first_p)
19586 {
19587 fprintf (stream, "%24s", "");
19588 *col_p = 24;
19589 }
19590 else
19591 {
19592 fprintf (stream, ", ");
19593 *col_p += 2;
19594 }
e7af610e 19595
316f5878
RS
19596 if (*col_p + strlen (string) > 72)
19597 {
19598 fprintf (stream, "\n%24s", "");
19599 *col_p = 24;
19600 }
19601
19602 fprintf (stream, "%s", string);
19603 *col_p += strlen (string);
19604
19605 *first_p = 0;
19606}
19607
19608void
17a2f251 19609md_show_usage (FILE *stream)
e7af610e 19610{
316f5878
RS
19611 int column, first;
19612 size_t i;
19613
19614 fprintf (stream, _("\
19615MIPS options:\n\
316f5878
RS
19616-EB generate big endian output\n\
19617-EL generate little endian output\n\
19618-g, -g2 do not remove unneeded NOPs or swap branches\n\
19619-G NUM allow referencing objects up to NUM bytes\n\
19620 implicitly with the gp register [default 8]\n"));
19621 fprintf (stream, _("\
19622-mips1 generate MIPS ISA I instructions\n\
19623-mips2 generate MIPS ISA II instructions\n\
19624-mips3 generate MIPS ISA III instructions\n\
19625-mips4 generate MIPS ISA IV instructions\n\
19626-mips5 generate MIPS ISA V instructions\n\
19627-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 19628-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 19629-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 19630-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
19631-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19632
19633 first = 1;
e7af610e
NC
19634
19635 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
19636 show (stream, mips_cpu_info_table[i].name, &column, &first);
19637 show (stream, "from-abi", &column, &first);
19638 fputc ('\n', stream);
e7af610e 19639
316f5878
RS
19640 fprintf (stream, _("\
19641-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19642-no-mCPU don't generate code specific to CPU.\n\
19643 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19644
19645 first = 1;
19646
19647 show (stream, "3900", &column, &first);
19648 show (stream, "4010", &column, &first);
19649 show (stream, "4100", &column, &first);
19650 show (stream, "4650", &column, &first);
19651 fputc ('\n', stream);
19652
19653 fprintf (stream, _("\
19654-mips16 generate mips16 instructions\n\
19655-no-mips16 do not generate mips16 instructions\n"));
19656 fprintf (stream, _("\
df58fc94
RS
19657-mmicromips generate microMIPS instructions\n\
19658-mno-micromips do not generate microMIPS instructions\n"));
19659 fprintf (stream, _("\
e16bfa71
TS
19660-msmartmips generate smartmips instructions\n\
19661-mno-smartmips do not generate smartmips instructions\n"));
19662 fprintf (stream, _("\
74cd071d
CF
19663-mdsp generate DSP instructions\n\
19664-mno-dsp do not generate DSP instructions\n"));
19665 fprintf (stream, _("\
8b082fb1
TS
19666-mdspr2 generate DSP R2 instructions\n\
19667-mno-dspr2 do not generate DSP R2 instructions\n"));
19668 fprintf (stream, _("\
ef2e4d86
CF
19669-mmt generate MT instructions\n\
19670-mno-mt do not generate MT instructions\n"));
19671 fprintf (stream, _("\
dec0624d
MR
19672-mmcu generate MCU instructions\n\
19673-mno-mcu do not generate MCU instructions\n"));
19674 fprintf (stream, _("\
b015e599
AP
19675-mvirt generate Virtualization instructions\n\
19676-mno-virt do not generate Virtualization instructions\n"));
19677 fprintf (stream, _("\
c67a084a
NC
19678-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19679-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 19680-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 19681-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 19682-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 19683-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
19684-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19685-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 19686-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
19687-O0 remove unneeded NOPs, do not swap branches\n\
19688-O remove unneeded NOPs and swap branches\n\
316f5878
RS
19689--trap, --no-break trap exception on div by 0 and mult overflow\n\
19690--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
19691 fprintf (stream, _("\
19692-mhard-float allow floating-point instructions\n\
19693-msoft-float do not allow floating-point instructions\n\
19694-msingle-float only allow 32-bit floating-point operations\n\
19695-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
19696--[no-]construct-floats [dis]allow floating point values to be constructed\n"
19697 ));
316f5878
RS
19698#ifdef OBJ_ELF
19699 fprintf (stream, _("\
19700-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 19701-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 19702-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 19703-non_shared do not generate code that can operate with DSOs\n\
316f5878 19704-xgot assume a 32 bit GOT\n\
dcd410fe 19705-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 19706-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 19707 position dependent (non shared) code\n\
316f5878
RS
19708-mabi=ABI create ABI conformant object file for:\n"));
19709
19710 first = 1;
19711
19712 show (stream, "32", &column, &first);
19713 show (stream, "o64", &column, &first);
19714 show (stream, "n32", &column, &first);
19715 show (stream, "64", &column, &first);
19716 show (stream, "eabi", &column, &first);
19717
19718 fputc ('\n', stream);
19719
19720 fprintf (stream, _("\
19721-32 create o32 ABI object file (default)\n\
19722-n32 create n32 ABI object file\n\
19723-64 create 64 ABI object file\n"));
19724#endif
e7af610e 19725}
14e777e0 19726
1575952e 19727#ifdef TE_IRIX
14e777e0 19728enum dwarf2_format
413a266c 19729mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 19730{
369943fe 19731 if (HAVE_64BIT_SYMBOLS)
1575952e 19732 return dwarf2_format_64bit_irix;
14e777e0
KB
19733 else
19734 return dwarf2_format_32bit;
19735}
1575952e 19736#endif
73369e65
EC
19737
19738int
19739mips_dwarf2_addr_size (void)
19740{
6b6b3450 19741 if (HAVE_64BIT_OBJECTS)
73369e65 19742 return 8;
73369e65
EC
19743 else
19744 return 4;
19745}
5862107c
EC
19746
19747/* Standard calling conventions leave the CFA at SP on entry. */
19748void
19749mips_cfi_frame_initial_instructions (void)
19750{
19751 cfi_add_CFA_def_cfa_register (SP);
19752}
19753
707bfff6
TS
19754int
19755tc_mips_regname_to_dw2regnum (char *regname)
19756{
19757 unsigned int regnum = -1;
19758 unsigned int reg;
19759
19760 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19761 regnum = reg;
19762
19763 return regnum;
19764}
This page took 2.544474 seconds and 4 git commands to generate.