PR ld/12549
[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;
252b5132
RH
221 /* Whether we are assembling for the mips16 processor. 0 if we are
222 not, 1 if we are, and -1 if the value has not been initialized.
223 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
224 -nomips16 command line options, and the default CPU. */
225 int mips16;
df58fc94
RS
226 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
227 1 if we are, and -1 if the value has not been initialized. Changed
228 by `.set micromips' and `.set nomicromips', and the -mmicromips
229 and -mno-micromips command line options, and the default CPU. */
230 int micromips;
252b5132
RH
231 /* Non-zero if we should not reorder instructions. Changed by `.set
232 reorder' and `.set noreorder'. */
233 int noreorder;
741fe287
MR
234 /* Non-zero if we should not permit the register designated "assembler
235 temporary" to be used in instructions. The value is the register
236 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
237 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
238 unsigned int at;
252b5132
RH
239 /* Non-zero if we should warn when a macro instruction expands into
240 more than one machine instruction. Changed by `.set nomacro' and
241 `.set macro'. */
242 int warn_about_macros;
243 /* Non-zero if we should not move instructions. Changed by `.set
244 move', `.set volatile', `.set nomove', and `.set novolatile'. */
245 int nomove;
246 /* Non-zero if we should not optimize branches by moving the target
247 of the branch into the delay slot. Actually, we don't perform
248 this optimization anyhow. Changed by `.set bopt' and `.set
249 nobopt'. */
250 int nobopt;
251 /* Non-zero if we should not autoextend mips16 instructions.
252 Changed by `.set autoextend' and `.set noautoextend'. */
253 int noautoextend;
a325df1d
TS
254 /* Restrict general purpose registers and floating point registers
255 to 32 bit. This is initially determined when -mgp32 or -mfp32
256 is passed but can changed if the assembler code uses .set mipsN. */
257 int gp32;
258 int fp32;
fef14a42
TS
259 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
260 command line option, and the default CPU. */
261 int arch;
aed1a261
RS
262 /* True if ".set sym32" is in effect. */
263 bfd_boolean sym32;
037b32b9
AN
264 /* True if floating-point operations are not allowed. Changed by .set
265 softfloat or .set hardfloat, by command line options -msoft-float or
266 -mhard-float. The default is false. */
267 bfd_boolean soft_float;
268
269 /* True if only single-precision floating-point operations are allowed.
270 Changed by .set singlefloat or .set doublefloat, command-line options
271 -msingle-float or -mdouble-float. The default is false. */
272 bfd_boolean single_float;
252b5132
RH
273};
274
037b32b9
AN
275/* This is the struct we use to hold the current set of options. Note
276 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
277 -1 to indicate that they have not been initialized. */
278
a325df1d 279/* True if -mgp32 was passed. */
a8e8e863 280static int file_mips_gp32 = -1;
a325df1d
TS
281
282/* True if -mfp32 was passed. */
a8e8e863 283static int file_mips_fp32 = -1;
a325df1d 284
037b32b9
AN
285/* 1 if -msoft-float, 0 if -mhard-float. The default is 0. */
286static int file_mips_soft_float = 0;
287
288/* 1 if -msingle-float, 0 if -mdouble-float. The default is 0. */
289static int file_mips_single_float = 0;
252b5132 290
e972090a
NC
291static struct mips_set_options mips_opts =
292{
037b32b9
AN
293 /* isa */ ISA_UNKNOWN, /* ase_mips3d */ -1, /* ase_mdmx */ -1,
294 /* ase_smartmips */ 0, /* ase_dsp */ -1, /* ase_dspr2 */ -1, /* ase_mt */ -1,
dec0624d
MR
295 /* ase_mcu */ -1, /* mips16 */ -1, /* micromips */ -1, /* noreorder */ 0,
296 /* at */ ATREG, /* warn_about_macros */ 0, /* nomove */ 0, /* nobopt */ 0,
037b32b9
AN
297 /* noautoextend */ 0, /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN,
298 /* sym32 */ FALSE, /* soft_float */ FALSE, /* single_float */ FALSE
e7af610e 299};
252b5132
RH
300
301/* These variables are filled in with the masks of registers used.
302 The object format code reads them and puts them in the appropriate
303 place. */
304unsigned long mips_gprmask;
305unsigned long mips_cprmask[4];
306
307/* MIPS ISA we are using for this output file. */
e7af610e 308static int file_mips_isa = ISA_UNKNOWN;
252b5132 309
738f4d98 310/* True if any MIPS16 code was produced. */
a4672219
TS
311static int file_ase_mips16;
312
3994f87e
TS
313#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
314 || mips_opts.isa == ISA_MIPS32R2 \
315 || mips_opts.isa == ISA_MIPS64 \
316 || mips_opts.isa == ISA_MIPS64R2)
317
df58fc94
RS
318/* True if any microMIPS code was produced. */
319static int file_ase_micromips;
320
b12dd2e4
CF
321/* True if we want to create R_MIPS_JALR for jalr $25. */
322#ifdef TE_IRIX
1180b5a4 323#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 324#else
1180b5a4
RS
325/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
326 because there's no place for any addend, the only acceptable
327 expression is a bare symbol. */
328#define MIPS_JALR_HINT_P(EXPR) \
329 (!HAVE_IN_PLACE_ADDENDS \
330 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
331#endif
332
1f25f5d3
CD
333/* True if -mips3d was passed or implied by arguments passed on the
334 command line (e.g., by -march). */
335static int file_ase_mips3d;
336
deec1734
CD
337/* True if -mdmx was passed or implied by arguments passed on the
338 command line (e.g., by -march). */
339static int file_ase_mdmx;
340
e16bfa71
TS
341/* True if -msmartmips was passed or implied by arguments passed on the
342 command line (e.g., by -march). */
343static int file_ase_smartmips;
344
ad3fea08
TS
345#define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32 \
346 || mips_opts.isa == ISA_MIPS32R2)
e16bfa71 347
74cd071d
CF
348/* True if -mdsp was passed or implied by arguments passed on the
349 command line (e.g., by -march). */
350static int file_ase_dsp;
351
ad3fea08 352#define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2 \
03f66e8a
MR
353 || mips_opts.isa == ISA_MIPS64R2 \
354 || mips_opts.micromips)
ad3fea08 355
65263ce3
TS
356#define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
357
8b082fb1
TS
358/* True if -mdspr2 was passed or implied by arguments passed on the
359 command line (e.g., by -march). */
360static int file_ase_dspr2;
361
362#define ISA_SUPPORTS_DSPR2_ASE (mips_opts.isa == ISA_MIPS32R2 \
03f66e8a
MR
363 || mips_opts.isa == ISA_MIPS64R2 \
364 || mips_opts.micromips)
8b082fb1 365
ef2e4d86
CF
366/* True if -mmt was passed or implied by arguments passed on the
367 command line (e.g., by -march). */
368static int file_ase_mt;
369
ad3fea08
TS
370#define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2 \
371 || mips_opts.isa == ISA_MIPS64R2)
372
dec0624d 373#define ISA_SUPPORTS_MCU_ASE (mips_opts.isa == ISA_MIPS32R2 \
9ddc84cc
MR
374 || mips_opts.isa == ISA_MIPS64R2 \
375 || mips_opts.micromips)
dec0624d 376
ec68c924 377/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 378static int file_mips_arch = CPU_UNKNOWN;
316f5878 379static const char *mips_arch_string;
ec68c924
EC
380
381/* The argument of the -mtune= flag. The architecture for which we
382 are optimizing. */
383static int mips_tune = CPU_UNKNOWN;
316f5878 384static const char *mips_tune_string;
ec68c924 385
316f5878 386/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
387static int mips_32bitmode = 0;
388
316f5878
RS
389/* True if the given ABI requires 32-bit registers. */
390#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
391
392/* Likewise 64-bit registers. */
707bfff6
TS
393#define ABI_NEEDS_64BIT_REGS(ABI) \
394 ((ABI) == N32_ABI \
395 || (ABI) == N64_ABI \
316f5878
RS
396 || (ABI) == O64_ABI)
397
ad3fea08 398/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
399#define ISA_HAS_64BIT_REGS(ISA) \
400 ((ISA) == ISA_MIPS3 \
401 || (ISA) == ISA_MIPS4 \
402 || (ISA) == ISA_MIPS5 \
403 || (ISA) == ISA_MIPS64 \
404 || (ISA) == ISA_MIPS64R2)
9ce8a5dd 405
ad3fea08
TS
406/* Return true if ISA supports 64 bit wide float registers. */
407#define ISA_HAS_64BIT_FPRS(ISA) \
408 ((ISA) == ISA_MIPS3 \
409 || (ISA) == ISA_MIPS4 \
410 || (ISA) == ISA_MIPS5 \
411 || (ISA) == ISA_MIPS32R2 \
412 || (ISA) == ISA_MIPS64 \
413 || (ISA) == ISA_MIPS64R2)
414
af7ee8bf
CD
415/* Return true if ISA supports 64-bit right rotate (dror et al.)
416 instructions. */
707bfff6 417#define ISA_HAS_DROR(ISA) \
df58fc94
RS
418 ((ISA) == ISA_MIPS64R2 \
419 || (mips_opts.micromips \
420 && ISA_HAS_64BIT_REGS (ISA)) \
421 )
af7ee8bf
CD
422
423/* Return true if ISA supports 32-bit right rotate (ror et al.)
424 instructions. */
707bfff6
TS
425#define ISA_HAS_ROR(ISA) \
426 ((ISA) == ISA_MIPS32R2 \
427 || (ISA) == ISA_MIPS64R2 \
df58fc94
RS
428 || mips_opts.ase_smartmips \
429 || mips_opts.micromips \
430 )
707bfff6 431
7455baf8
TS
432/* Return true if ISA supports single-precision floats in odd registers. */
433#define ISA_HAS_ODD_SINGLE_FPR(ISA) \
434 ((ISA) == ISA_MIPS32 \
435 || (ISA) == ISA_MIPS32R2 \
436 || (ISA) == ISA_MIPS64 \
437 || (ISA) == ISA_MIPS64R2)
af7ee8bf 438
ad3fea08
TS
439/* Return true if ISA supports move to/from high part of a 64-bit
440 floating-point register. */
441#define ISA_HAS_MXHC1(ISA) \
442 ((ISA) == ISA_MIPS32R2 \
443 || (ISA) == ISA_MIPS64R2)
444
e013f690 445#define HAVE_32BIT_GPRS \
ad3fea08 446 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 447
e013f690 448#define HAVE_32BIT_FPRS \
ad3fea08 449 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
ca4e0257 450
ad3fea08
TS
451#define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
452#define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
ca4e0257 453
316f5878 454#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 455
316f5878 456#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 457
3b91255e
RS
458/* True if relocations are stored in-place. */
459#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
460
aed1a261
RS
461/* The ABI-derived address size. */
462#define HAVE_64BIT_ADDRESSES \
463 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
464#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 465
aed1a261
RS
466/* The size of symbolic constants (i.e., expressions of the form
467 "SYMBOL" or "SYMBOL + OFFSET"). */
468#define HAVE_32BIT_SYMBOLS \
469 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
470#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 471
b7c7d6c1
TS
472/* Addresses are loaded in different ways, depending on the address size
473 in use. The n32 ABI Documentation also mandates the use of additions
474 with overflow checking, but existing implementations don't follow it. */
f899b4b8 475#define ADDRESS_ADD_INSN \
b7c7d6c1 476 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
477
478#define ADDRESS_ADDI_INSN \
b7c7d6c1 479 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
480
481#define ADDRESS_LOAD_INSN \
482 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
483
484#define ADDRESS_STORE_INSN \
485 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
486
a4672219 487/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
488#define CPU_HAS_MIPS16(cpu) \
489 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
490 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 491
2309ddf2 492/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
493#define CPU_HAS_MICROMIPS(cpu) 0
494
60b63b72
RS
495/* True if CPU has a dror instruction. */
496#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
497
498/* True if CPU has a ror instruction. */
499#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
500
dd6a37e7 501/* True if CPU is in the Octeon family */
432233b3 502#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
dd6a37e7 503
dd3cbb7e 504/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 505#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 506
0aa27725
RS
507/* True, if CPU has support for ldc1 and sdc1. */
508#define CPU_HAS_LDC1_SDC1(CPU) \
509 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
510
c8978940
CD
511/* True if mflo and mfhi can be immediately followed by instructions
512 which write to the HI and LO registers.
513
514 According to MIPS specifications, MIPS ISAs I, II, and III need
515 (at least) two instructions between the reads of HI/LO and
516 instructions which write them, and later ISAs do not. Contradicting
517 the MIPS specifications, some MIPS IV processor user manuals (e.g.
518 the UM for the NEC Vr5000) document needing the instructions between
519 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
520 MIPS64 and later ISAs to have the interlocks, plus any specific
521 earlier-ISA CPUs for which CPU documentation declares that the
522 instructions are really interlocked. */
523#define hilo_interlocks \
524 (mips_opts.isa == ISA_MIPS32 \
525 || mips_opts.isa == ISA_MIPS32R2 \
526 || mips_opts.isa == ISA_MIPS64 \
527 || mips_opts.isa == ISA_MIPS64R2 \
528 || mips_opts.arch == CPU_R4010 \
e407c74b 529 || mips_opts.arch == CPU_R5900 \
c8978940
CD
530 || mips_opts.arch == CPU_R10000 \
531 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
532 || mips_opts.arch == CPU_R14000 \
533 || mips_opts.arch == CPU_R16000 \
c8978940 534 || mips_opts.arch == CPU_RM7000 \
c8978940 535 || mips_opts.arch == CPU_VR5500 \
df58fc94 536 || mips_opts.micromips \
c8978940 537 )
252b5132
RH
538
539/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
540 from the GPRs after they are loaded from memory, and thus does not
541 require nops to be inserted. This applies to instructions marked
542 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
df58fc94
RS
543 level I and microMIPS mode instructions are always interlocked. */
544#define gpr_interlocks \
545 (mips_opts.isa != ISA_MIPS1 \
546 || mips_opts.arch == CPU_R3900 \
e407c74b 547 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
548 || mips_opts.micromips \
549 )
252b5132 550
81912461
ILT
551/* Whether the processor uses hardware interlocks to avoid delays
552 required by coprocessor instructions, and thus does not require
553 nops to be inserted. This applies to instructions marked
554 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
555 between instructions marked INSN_WRITE_COND_CODE and ones marked
556 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
557 levels I, II, and III and microMIPS mode instructions are always
558 interlocked. */
bdaaa2e1 559/* Itbl support may require additional care here. */
81912461
ILT
560#define cop_interlocks \
561 ((mips_opts.isa != ISA_MIPS1 \
562 && mips_opts.isa != ISA_MIPS2 \
563 && mips_opts.isa != ISA_MIPS3) \
564 || mips_opts.arch == CPU_R4300 \
df58fc94 565 || mips_opts.micromips \
81912461
ILT
566 )
567
568/* Whether the processor uses hardware interlocks to protect reads
569 from coprocessor registers after they are loaded from memory, and
570 thus does not require nops to be inserted. This applies to
571 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
572 requires at MIPS ISA level I and microMIPS mode instructions are
573 always interlocked. */
574#define cop_mem_interlocks \
575 (mips_opts.isa != ISA_MIPS1 \
576 || mips_opts.micromips \
577 )
252b5132 578
6b76fefe
CM
579/* Is this a mfhi or mflo instruction? */
580#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
581 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
582
df58fc94
RS
583/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
584 has been selected. This implies, in particular, that addresses of text
585 labels have their LSB set. */
586#define HAVE_CODE_COMPRESSION \
587 ((mips_opts.mips16 | mips_opts.micromips) != 0)
588
252b5132
RH
589/* MIPS PIC level. */
590
a161fe53 591enum mips_pic_level mips_pic;
252b5132 592
c9914766 593/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 594 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 595static int mips_big_got = 0;
252b5132
RH
596
597/* 1 if trap instructions should used for overflow rather than break
598 instructions. */
c9914766 599static int mips_trap = 0;
252b5132 600
119d663a 601/* 1 if double width floating point constants should not be constructed
b6ff326e 602 by assembling two single width halves into two single width floating
119d663a
NC
603 point registers which just happen to alias the double width destination
604 register. On some architectures this aliasing can be disabled by a bit
d547a75e 605 in the status register, and the setting of this bit cannot be determined
119d663a
NC
606 automatically at assemble time. */
607static int mips_disable_float_construction;
608
252b5132
RH
609/* Non-zero if any .set noreorder directives were used. */
610
611static int mips_any_noreorder;
612
6b76fefe
CM
613/* Non-zero if nops should be inserted when the register referenced in
614 an mfhi/mflo instruction is read in the next two instructions. */
615static int mips_7000_hilo_fix;
616
02ffd3e4 617/* The size of objects in the small data section. */
156c2f8b 618static unsigned int g_switch_value = 8;
252b5132
RH
619/* Whether the -G option was used. */
620static int g_switch_seen = 0;
621
622#define N_RMASK 0xc4
623#define N_VFP 0xd4
624
625/* If we can determine in advance that GP optimization won't be
626 possible, we can skip the relaxation stuff that tries to produce
627 GP-relative references. This makes delay slot optimization work
628 better.
629
630 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
631 gcc output. It needs to guess right for gcc, otherwise gcc
632 will put what it thinks is a GP-relative instruction in a branch
633 delay slot.
252b5132
RH
634
635 I don't know if a fix is needed for the SVR4_PIC mode. I've only
636 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 637static int nopic_need_relax (symbolS *, int);
252b5132
RH
638
639/* handle of the OPCODE hash table */
640static struct hash_control *op_hash = NULL;
641
642/* The opcode hash table we use for the mips16. */
643static struct hash_control *mips16_op_hash = NULL;
644
df58fc94
RS
645/* The opcode hash table we use for the microMIPS ASE. */
646static struct hash_control *micromips_op_hash = NULL;
647
252b5132
RH
648/* This array holds the chars that always start a comment. If the
649 pre-processor is disabled, these aren't very useful */
650const char comment_chars[] = "#";
651
652/* This array holds the chars that only start a comment at the beginning of
653 a line. If the line seems to have the form '# 123 filename'
654 .line and .file directives will appear in the pre-processed output */
655/* Note that input_file.c hand checks for '#' at the beginning of the
656 first line of the input file. This is because the compiler outputs
bdaaa2e1 657 #NO_APP at the beginning of its output. */
252b5132
RH
658/* Also note that C style comments are always supported. */
659const char line_comment_chars[] = "#";
660
bdaaa2e1 661/* This array holds machine specific line separator characters. */
63a0b638 662const char line_separator_chars[] = ";";
252b5132
RH
663
664/* Chars that can be used to separate mant from exp in floating point nums */
665const char EXP_CHARS[] = "eE";
666
667/* Chars that mean this number is a floating point constant */
668/* As in 0f12.456 */
669/* or 0d1.2345e12 */
670const char FLT_CHARS[] = "rRsSfFdDxXpP";
671
672/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
673 changed in read.c . Ideally it shouldn't have to know about it at all,
674 but nothing is ideal around here.
675 */
676
677static char *insn_error;
678
679static int auto_align = 1;
680
681/* When outputting SVR4 PIC code, the assembler needs to know the
682 offset in the stack frame from which to restore the $gp register.
683 This is set by the .cprestore pseudo-op, and saved in this
684 variable. */
685static offsetT mips_cprestore_offset = -1;
686
67c1ffbe 687/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 688 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 689 offset and even an other register than $gp as global pointer. */
6478892d
TS
690static offsetT mips_cpreturn_offset = -1;
691static int mips_cpreturn_register = -1;
692static int mips_gp_register = GP;
def2e0dd 693static int mips_gprel_offset = 0;
6478892d 694
7a621144
DJ
695/* Whether mips_cprestore_offset has been set in the current function
696 (or whether it has already been warned about, if not). */
697static int mips_cprestore_valid = 0;
698
252b5132
RH
699/* This is the register which holds the stack frame, as set by the
700 .frame pseudo-op. This is needed to implement .cprestore. */
701static int mips_frame_reg = SP;
702
7a621144
DJ
703/* Whether mips_frame_reg has been set in the current function
704 (or whether it has already been warned about, if not). */
705static int mips_frame_reg_valid = 0;
706
252b5132
RH
707/* To output NOP instructions correctly, we need to keep information
708 about the previous two instructions. */
709
710/* Whether we are optimizing. The default value of 2 means to remove
711 unneeded NOPs and swap branch instructions when possible. A value
712 of 1 means to not swap branches. A value of 0 means to always
713 insert NOPs. */
714static int mips_optimize = 2;
715
716/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
717 equivalent to seeing no -g option at all. */
718static int mips_debug = 0;
719
7d8e00cf
RS
720/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
721#define MAX_VR4130_NOPS 4
722
723/* The maximum number of NOPs needed to fill delay slots. */
724#define MAX_DELAY_NOPS 2
725
726/* The maximum number of NOPs needed for any purpose. */
727#define MAX_NOPS 4
71400594
RS
728
729/* A list of previous instructions, with index 0 being the most recent.
730 We need to look back MAX_NOPS instructions when filling delay slots
731 or working around processor errata. We need to look back one
732 instruction further if we're thinking about using history[0] to
733 fill a branch delay slot. */
734static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 735
1e915849 736/* Nop instructions used by emit_nop. */
df58fc94
RS
737static struct mips_cl_insn nop_insn;
738static struct mips_cl_insn mips16_nop_insn;
739static struct mips_cl_insn micromips_nop16_insn;
740static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
741
742/* The appropriate nop for the current mode. */
df58fc94
RS
743#define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn \
744 : (mips_opts.micromips ? &micromips_nop16_insn : &nop_insn))
745
746/* The size of NOP_INSN in bytes. */
747#define NOP_INSN_SIZE (HAVE_CODE_COMPRESSION ? 2 : 4)
252b5132 748
252b5132
RH
749/* If this is set, it points to a frag holding nop instructions which
750 were inserted before the start of a noreorder section. If those
751 nops turn out to be unnecessary, the size of the frag can be
752 decreased. */
753static fragS *prev_nop_frag;
754
755/* The number of nop instructions we created in prev_nop_frag. */
756static int prev_nop_frag_holds;
757
758/* The number of nop instructions that we know we need in
bdaaa2e1 759 prev_nop_frag. */
252b5132
RH
760static int prev_nop_frag_required;
761
762/* The number of instructions we've seen since prev_nop_frag. */
763static int prev_nop_frag_since;
764
765/* For ECOFF and ELF, relocations against symbols are done in two
766 parts, with a HI relocation and a LO relocation. Each relocation
767 has only 16 bits of space to store an addend. This means that in
768 order for the linker to handle carries correctly, it must be able
769 to locate both the HI and the LO relocation. This means that the
770 relocations must appear in order in the relocation table.
771
772 In order to implement this, we keep track of each unmatched HI
773 relocation. We then sort them so that they immediately precede the
bdaaa2e1 774 corresponding LO relocation. */
252b5132 775
e972090a
NC
776struct mips_hi_fixup
777{
252b5132
RH
778 /* Next HI fixup. */
779 struct mips_hi_fixup *next;
780 /* This fixup. */
781 fixS *fixp;
782 /* The section this fixup is in. */
783 segT seg;
784};
785
786/* The list of unmatched HI relocs. */
787
788static struct mips_hi_fixup *mips_hi_fixup_list;
789
64bdfcaf
RS
790/* The frag containing the last explicit relocation operator.
791 Null if explicit relocations have not been used. */
792
793static fragS *prev_reloc_op_frag;
794
252b5132
RH
795/* Map normal MIPS register numbers to mips16 register numbers. */
796
797#define X ILLEGAL_REG
e972090a
NC
798static const int mips32_to_16_reg_map[] =
799{
252b5132
RH
800 X, X, 2, 3, 4, 5, 6, 7,
801 X, X, X, X, X, X, X, X,
802 0, 1, X, X, X, X, X, X,
803 X, X, X, X, X, X, X, X
804};
805#undef X
806
807/* Map mips16 register numbers to normal MIPS register numbers. */
808
e972090a
NC
809static const unsigned int mips16_to_32_reg_map[] =
810{
252b5132
RH
811 16, 17, 2, 3, 4, 5, 6, 7
812};
60b63b72 813
df58fc94
RS
814/* Map normal MIPS register numbers to microMIPS register numbers. */
815
816#define mips32_to_micromips_reg_b_map mips32_to_16_reg_map
817#define mips32_to_micromips_reg_c_map mips32_to_16_reg_map
818#define mips32_to_micromips_reg_d_map mips32_to_16_reg_map
819#define mips32_to_micromips_reg_e_map mips32_to_16_reg_map
820#define mips32_to_micromips_reg_f_map mips32_to_16_reg_map
821#define mips32_to_micromips_reg_g_map mips32_to_16_reg_map
822#define mips32_to_micromips_reg_l_map mips32_to_16_reg_map
823
824#define X ILLEGAL_REG
825/* reg type h: 4, 5, 6. */
826static const int mips32_to_micromips_reg_h_map[] =
827{
828 X, X, X, X, 4, 5, 6, X,
829 X, X, X, X, X, X, X, X,
830 X, X, X, X, X, X, X, X,
831 X, X, X, X, X, X, X, X
832};
833
834/* reg type m: 0, 17, 2, 3, 16, 18, 19, 20. */
835static const int mips32_to_micromips_reg_m_map[] =
836{
837 0, X, 2, 3, X, X, X, X,
838 X, X, X, X, X, X, X, X,
839 4, 1, 5, 6, 7, X, X, X,
840 X, X, X, X, X, X, X, X
841};
842
843/* reg type q: 0, 2-7. 17. */
844static const int mips32_to_micromips_reg_q_map[] =
845{
846 0, X, 2, 3, 4, 5, 6, 7,
847 X, X, X, X, X, X, X, X,
848 X, 1, X, X, X, X, X, X,
849 X, X, X, X, X, X, X, X
850};
851
852#define mips32_to_micromips_reg_n_map mips32_to_micromips_reg_m_map
853#undef X
854
855/* Map microMIPS register numbers to normal MIPS register numbers. */
856
857#define micromips_to_32_reg_b_map mips16_to_32_reg_map
858#define micromips_to_32_reg_c_map mips16_to_32_reg_map
859#define micromips_to_32_reg_d_map mips16_to_32_reg_map
860#define micromips_to_32_reg_e_map mips16_to_32_reg_map
861#define micromips_to_32_reg_f_map mips16_to_32_reg_map
862#define micromips_to_32_reg_g_map mips16_to_32_reg_map
863
864/* The microMIPS registers with type h. */
865static const unsigned int micromips_to_32_reg_h_map[] =
866{
867 5, 5, 6, 4, 4, 4, 4, 4
868};
869
870/* The microMIPS registers with type i. */
871static const unsigned int micromips_to_32_reg_i_map[] =
872{
873 6, 7, 7, 21, 22, 5, 6, 7
874};
875
876#define micromips_to_32_reg_l_map mips16_to_32_reg_map
877
878/* The microMIPS registers with type m. */
879static const unsigned int micromips_to_32_reg_m_map[] =
880{
881 0, 17, 2, 3, 16, 18, 19, 20
882};
883
884#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
885
886/* The microMIPS registers with type q. */
887static const unsigned int micromips_to_32_reg_q_map[] =
888{
889 0, 17, 2, 3, 4, 5, 6, 7
890};
891
892/* microMIPS imm type B. */
893static const int micromips_imm_b_map[] =
894{
895 1, 4, 8, 12, 16, 20, 24, -1
896};
897
898/* microMIPS imm type C. */
899static const int micromips_imm_c_map[] =
900{
901 128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
902};
903
71400594
RS
904/* Classifies the kind of instructions we're interested in when
905 implementing -mfix-vr4120. */
c67a084a
NC
906enum fix_vr4120_class
907{
71400594
RS
908 FIX_VR4120_MACC,
909 FIX_VR4120_DMACC,
910 FIX_VR4120_MULT,
911 FIX_VR4120_DMULT,
912 FIX_VR4120_DIV,
913 FIX_VR4120_MTHILO,
914 NUM_FIX_VR4120_CLASSES
915};
916
c67a084a
NC
917/* ...likewise -mfix-loongson2f-jump. */
918static bfd_boolean mips_fix_loongson2f_jump;
919
920/* ...likewise -mfix-loongson2f-nop. */
921static bfd_boolean mips_fix_loongson2f_nop;
922
923/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
924static bfd_boolean mips_fix_loongson2f;
925
71400594
RS
926/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
927 there must be at least one other instruction between an instruction
928 of type X and an instruction of type Y. */
929static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
930
931/* True if -mfix-vr4120 is in force. */
d766e8ec 932static int mips_fix_vr4120;
4a6a3df4 933
7d8e00cf
RS
934/* ...likewise -mfix-vr4130. */
935static int mips_fix_vr4130;
936
6a32d874
CM
937/* ...likewise -mfix-24k. */
938static int mips_fix_24k;
939
d954098f
DD
940/* ...likewise -mfix-cn63xxp1 */
941static bfd_boolean mips_fix_cn63xxp1;
942
4a6a3df4
AO
943/* We don't relax branches by default, since this causes us to expand
944 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
945 fail to compute the offset before expanding the macro to the most
946 efficient expansion. */
947
948static int mips_relax_branch;
252b5132 949\f
4d7206a2
RS
950/* The expansion of many macros depends on the type of symbol that
951 they refer to. For example, when generating position-dependent code,
952 a macro that refers to a symbol may have two different expansions,
953 one which uses GP-relative addresses and one which uses absolute
954 addresses. When generating SVR4-style PIC, a macro may have
955 different expansions for local and global symbols.
956
957 We handle these situations by generating both sequences and putting
958 them in variant frags. In position-dependent code, the first sequence
959 will be the GP-relative one and the second sequence will be the
960 absolute one. In SVR4 PIC, the first sequence will be for global
961 symbols and the second will be for local symbols.
962
584892a6
RS
963 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
964 SECOND are the lengths of the two sequences in bytes. These fields
965 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
966 the subtype has the following flags:
4d7206a2 967
584892a6
RS
968 RELAX_USE_SECOND
969 Set if it has been decided that we should use the second
970 sequence instead of the first.
971
972 RELAX_SECOND_LONGER
973 Set in the first variant frag if the macro's second implementation
974 is longer than its first. This refers to the macro as a whole,
975 not an individual relaxation.
976
977 RELAX_NOMACRO
978 Set in the first variant frag if the macro appeared in a .set nomacro
979 block and if one alternative requires a warning but the other does not.
980
981 RELAX_DELAY_SLOT
982 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
983 delay slot.
4d7206a2 984
df58fc94
RS
985 RELAX_DELAY_SLOT_16BIT
986 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
987 16-bit instruction.
988
989 RELAX_DELAY_SLOT_SIZE_FIRST
990 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
991 the macro is of the wrong size for the branch delay slot.
992
993 RELAX_DELAY_SLOT_SIZE_SECOND
994 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
995 the macro is of the wrong size for the branch delay slot.
996
4d7206a2
RS
997 The frag's "opcode" points to the first fixup for relaxable code.
998
999 Relaxable macros are generated using a sequence such as:
1000
1001 relax_start (SYMBOL);
1002 ... generate first expansion ...
1003 relax_switch ();
1004 ... generate second expansion ...
1005 relax_end ();
1006
1007 The code and fixups for the unwanted alternative are discarded
1008 by md_convert_frag. */
584892a6 1009#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 1010
584892a6
RS
1011#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1012#define RELAX_SECOND(X) ((X) & 0xff)
1013#define RELAX_USE_SECOND 0x10000
1014#define RELAX_SECOND_LONGER 0x20000
1015#define RELAX_NOMACRO 0x40000
1016#define RELAX_DELAY_SLOT 0x80000
df58fc94
RS
1017#define RELAX_DELAY_SLOT_16BIT 0x100000
1018#define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
1019#define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
252b5132 1020
4a6a3df4
AO
1021/* Branch without likely bit. If label is out of range, we turn:
1022
1023 beq reg1, reg2, label
1024 delay slot
1025
1026 into
1027
1028 bne reg1, reg2, 0f
1029 nop
1030 j label
1031 0: delay slot
1032
1033 with the following opcode replacements:
1034
1035 beq <-> bne
1036 blez <-> bgtz
1037 bltz <-> bgez
1038 bc1f <-> bc1t
1039
1040 bltzal <-> bgezal (with jal label instead of j label)
1041
1042 Even though keeping the delay slot instruction in the delay slot of
1043 the branch would be more efficient, it would be very tricky to do
1044 correctly, because we'd have to introduce a variable frag *after*
1045 the delay slot instruction, and expand that instead. Let's do it
1046 the easy way for now, even if the branch-not-taken case now costs
1047 one additional instruction. Out-of-range branches are not supposed
1048 to be common, anyway.
1049
1050 Branch likely. If label is out of range, we turn:
1051
1052 beql reg1, reg2, label
1053 delay slot (annulled if branch not taken)
1054
1055 into
1056
1057 beql reg1, reg2, 1f
1058 nop
1059 beql $0, $0, 2f
1060 nop
1061 1: j[al] label
1062 delay slot (executed only if branch taken)
1063 2:
1064
1065 It would be possible to generate a shorter sequence by losing the
1066 likely bit, generating something like:
b34976b6 1067
4a6a3df4
AO
1068 bne reg1, reg2, 0f
1069 nop
1070 j[al] label
1071 delay slot (executed only if branch taken)
1072 0:
1073
1074 beql -> bne
1075 bnel -> beq
1076 blezl -> bgtz
1077 bgtzl -> blez
1078 bltzl -> bgez
1079 bgezl -> bltz
1080 bc1fl -> bc1t
1081 bc1tl -> bc1f
1082
1083 bltzall -> bgezal (with jal label instead of j label)
1084 bgezall -> bltzal (ditto)
1085
1086
1087 but it's not clear that it would actually improve performance. */
66b3e8da
MR
1088#define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1089 ((relax_substateT) \
1090 (0xc0000000 \
1091 | ((at) & 0x1f) \
1092 | ((toofar) ? 0x20 : 0) \
1093 | ((link) ? 0x40 : 0) \
1094 | ((likely) ? 0x80 : 0) \
1095 | ((uncond) ? 0x100 : 0)))
4a6a3df4 1096#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
66b3e8da
MR
1097#define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1098#define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1099#define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1100#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1101#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1102
252b5132
RH
1103/* For mips16 code, we use an entirely different form of relaxation.
1104 mips16 supports two versions of most instructions which take
1105 immediate values: a small one which takes some small value, and a
1106 larger one which takes a 16 bit value. Since branches also follow
1107 this pattern, relaxing these values is required.
1108
1109 We can assemble both mips16 and normal MIPS code in a single
1110 object. Therefore, we need to support this type of relaxation at
1111 the same time that we support the relaxation described above. We
1112 use the high bit of the subtype field to distinguish these cases.
1113
1114 The information we store for this type of relaxation is the
1115 argument code found in the opcode file for this relocation, whether
1116 the user explicitly requested a small or extended form, and whether
1117 the relocation is in a jump or jal delay slot. That tells us the
1118 size of the value, and how it should be stored. We also store
1119 whether the fragment is considered to be extended or not. We also
1120 store whether this is known to be a branch to a different section,
1121 whether we have tried to relax this frag yet, and whether we have
1122 ever extended a PC relative fragment because of a shift count. */
1123#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1124 (0x80000000 \
1125 | ((type) & 0xff) \
1126 | ((small) ? 0x100 : 0) \
1127 | ((ext) ? 0x200 : 0) \
1128 | ((dslot) ? 0x400 : 0) \
1129 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 1130#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
1131#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1132#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1133#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1134#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1135#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1136#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1137#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1138#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1139#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1140#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1141#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95 1142
df58fc94
RS
1143/* For microMIPS code, we use relaxation similar to one we use for
1144 MIPS16 code. Some instructions that take immediate values support
1145 two encodings: a small one which takes some small value, and a
1146 larger one which takes a 16 bit value. As some branches also follow
1147 this pattern, relaxing these values is required.
1148
1149 We can assemble both microMIPS and normal MIPS code in a single
1150 object. Therefore, we need to support this type of relaxation at
1151 the same time that we support the relaxation described above. We
1152 use one of the high bits of the subtype field to distinguish these
1153 cases.
1154
1155 The information we store for this type of relaxation is the argument
1156 code found in the opcode file for this relocation, the register
40209cad
MR
1157 selected as the assembler temporary, whether the branch is
1158 unconditional, whether it is compact, whether it stores the link
1159 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1160 branches to a sequence of instructions is enabled, and whether the
1161 displacement of a branch is too large to fit as an immediate argument
1162 of a 16-bit and a 32-bit branch, respectively. */
1163#define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1164 relax32, toofar16, toofar32) \
1165 (0x40000000 \
1166 | ((type) & 0xff) \
1167 | (((at) & 0x1f) << 8) \
1168 | ((uncond) ? 0x2000 : 0) \
1169 | ((compact) ? 0x4000 : 0) \
1170 | ((link) ? 0x8000 : 0) \
1171 | ((relax32) ? 0x10000 : 0) \
1172 | ((toofar16) ? 0x20000 : 0) \
1173 | ((toofar32) ? 0x40000 : 0))
df58fc94
RS
1174#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1175#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1176#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
40209cad
MR
1177#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1178#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1179#define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1180#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1181
1182#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1183#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1184#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1185#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1186#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1187#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
df58fc94 1188
43c0598f
RS
1189/* Sign-extend 16-bit value X. */
1190#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1191
885add95
CD
1192/* Is the given value a sign-extended 32-bit value? */
1193#define IS_SEXT_32BIT_NUM(x) \
1194 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1195 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1196
1197/* Is the given value a sign-extended 16-bit value? */
1198#define IS_SEXT_16BIT_NUM(x) \
1199 (((x) &~ (offsetT) 0x7fff) == 0 \
1200 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1201
df58fc94
RS
1202/* Is the given value a sign-extended 12-bit value? */
1203#define IS_SEXT_12BIT_NUM(x) \
1204 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1205
2051e8c4
MR
1206/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1207#define IS_ZEXT_32BIT_NUM(x) \
1208 (((x) &~ (offsetT) 0xffffffff) == 0 \
1209 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1210
bf12938e
RS
1211/* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1212 VALUE << SHIFT. VALUE is evaluated exactly once. */
1213#define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1214 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1215 | (((VALUE) & (MASK)) << (SHIFT)))
1216
1217/* Extract bits MASK << SHIFT from STRUCT and shift them right
1218 SHIFT places. */
1219#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1220 (((STRUCT) >> (SHIFT)) & (MASK))
1221
1222/* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1223 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1224
1225 include/opcode/mips.h specifies operand fields using the macros
1226 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
1227 with "MIPS16OP" instead of "OP". */
df58fc94
RS
1228#define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1229 do \
1230 if (!(MICROMIPS)) \
1231 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1232 OP_MASK_##FIELD, OP_SH_##FIELD); \
1233 else \
1234 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1235 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1236 while (0)
bf12938e
RS
1237#define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1238 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1239 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1240
1241/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1242#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1243 (!(MICROMIPS) \
1244 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1245 : EXTRACT_BITS ((INSN).insn_opcode, \
1246 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1247#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1248 EXTRACT_BITS ((INSN).insn_opcode, \
1249 MIPS16OP_MASK_##FIELD, \
1250 MIPS16OP_SH_##FIELD)
5c04167a
RS
1251
1252/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1253#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1254\f
df58fc94
RS
1255/* Whether or not we are emitting a branch-likely macro. */
1256static bfd_boolean emit_branch_likely_macro = FALSE;
1257
4d7206a2
RS
1258/* Global variables used when generating relaxable macros. See the
1259 comment above RELAX_ENCODE for more details about how relaxation
1260 is used. */
1261static struct {
1262 /* 0 if we're not emitting a relaxable macro.
1263 1 if we're emitting the first of the two relaxation alternatives.
1264 2 if we're emitting the second alternative. */
1265 int sequence;
1266
1267 /* The first relaxable fixup in the current frag. (In other words,
1268 the first fixup that refers to relaxable code.) */
1269 fixS *first_fixup;
1270
1271 /* sizes[0] says how many bytes of the first alternative are stored in
1272 the current frag. Likewise sizes[1] for the second alternative. */
1273 unsigned int sizes[2];
1274
1275 /* The symbol on which the choice of sequence depends. */
1276 symbolS *symbol;
1277} mips_relax;
252b5132 1278\f
584892a6
RS
1279/* Global variables used to decide whether a macro needs a warning. */
1280static struct {
1281 /* True if the macro is in a branch delay slot. */
1282 bfd_boolean delay_slot_p;
1283
df58fc94
RS
1284 /* Set to the length in bytes required if the macro is in a delay slot
1285 that requires a specific length of instruction, otherwise zero. */
1286 unsigned int delay_slot_length;
1287
584892a6
RS
1288 /* For relaxable macros, sizes[0] is the length of the first alternative
1289 in bytes and sizes[1] is the length of the second alternative.
1290 For non-relaxable macros, both elements give the length of the
1291 macro in bytes. */
1292 unsigned int sizes[2];
1293
df58fc94
RS
1294 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1295 instruction of the first alternative in bytes and first_insn_sizes[1]
1296 is the length of the first instruction of the second alternative.
1297 For non-relaxable macros, both elements give the length of the first
1298 instruction in bytes.
1299
1300 Set to zero if we haven't yet seen the first instruction. */
1301 unsigned int first_insn_sizes[2];
1302
1303 /* For relaxable macros, insns[0] is the number of instructions for the
1304 first alternative and insns[1] is the number of instructions for the
1305 second alternative.
1306
1307 For non-relaxable macros, both elements give the number of
1308 instructions for the macro. */
1309 unsigned int insns[2];
1310
584892a6
RS
1311 /* The first variant frag for this macro. */
1312 fragS *first_frag;
1313} mips_macro_warning;
1314\f
252b5132
RH
1315/* Prototypes for static functions. */
1316
252b5132
RH
1317enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1318
b34976b6 1319static void append_insn
df58fc94
RS
1320 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1321 bfd_boolean expansionp);
7d10b47d 1322static void mips_no_prev_insn (void);
c67a084a 1323static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1324static void mips16_macro_build
03ea81db 1325 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1326static void load_register (int, expressionS *, int);
584892a6
RS
1327static void macro_start (void);
1328static void macro_end (void);
17a2f251
TS
1329static void macro (struct mips_cl_insn * ip);
1330static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1331static void mips_ip (char *str, struct mips_cl_insn * ip);
1332static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1333static void mips16_immed
43c0598f
RS
1334 (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1335 unsigned int, unsigned long *);
5e0116d5 1336static size_t my_getSmallExpression
17a2f251
TS
1337 (expressionS *, bfd_reloc_code_real_type *, char *);
1338static void my_getExpression (expressionS *, char *);
1339static void s_align (int);
1340static void s_change_sec (int);
1341static void s_change_section (int);
1342static void s_cons (int);
1343static void s_float_cons (int);
1344static void s_mips_globl (int);
1345static void s_option (int);
1346static void s_mipsset (int);
1347static void s_abicalls (int);
1348static void s_cpload (int);
1349static void s_cpsetup (int);
1350static void s_cplocal (int);
1351static void s_cprestore (int);
1352static void s_cpreturn (int);
741d6ea8
JM
1353static void s_dtprelword (int);
1354static void s_dtpreldword (int);
d0f13682
CLT
1355static void s_tprelword (int);
1356static void s_tpreldword (int);
17a2f251
TS
1357static void s_gpvalue (int);
1358static void s_gpword (int);
1359static void s_gpdword (int);
1360static void s_cpadd (int);
1361static void s_insn (int);
1362static void md_obj_begin (void);
1363static void md_obj_end (void);
1364static void s_mips_ent (int);
1365static void s_mips_end (int);
1366static void s_mips_frame (int);
1367static void s_mips_mask (int reg_type);
1368static void s_mips_stab (int);
1369static void s_mips_weakext (int);
1370static void s_mips_file (int);
1371static void s_mips_loc (int);
1372static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1373static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 1374static int validate_mips_insn (const struct mips_opcode *);
df58fc94
RS
1375static int validate_micromips_insn (const struct mips_opcode *);
1376static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1377static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
e7af610e
NC
1378
1379/* Table and functions used to map between CPU/ISA names, and
1380 ISA levels, and CPU numbers. */
1381
e972090a
NC
1382struct mips_cpu_info
1383{
e7af610e 1384 const char *name; /* CPU or ISA name. */
ad3fea08 1385 int flags; /* ASEs available, or ISA flag. */
e7af610e
NC
1386 int isa; /* ISA level. */
1387 int cpu; /* CPU number (default CPU if ISA). */
1388};
1389
ad3fea08
TS
1390#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1391#define MIPS_CPU_ASE_SMARTMIPS 0x0002 /* CPU implements SmartMIPS ASE */
1392#define MIPS_CPU_ASE_DSP 0x0004 /* CPU implements DSP ASE */
1393#define MIPS_CPU_ASE_MT 0x0008 /* CPU implements MT ASE */
1394#define MIPS_CPU_ASE_MIPS3D 0x0010 /* CPU implements MIPS-3D ASE */
1395#define MIPS_CPU_ASE_MDMX 0x0020 /* CPU implements MDMX ASE */
8b082fb1 1396#define MIPS_CPU_ASE_DSPR2 0x0040 /* CPU implements DSP R2 ASE */
dec0624d 1397#define MIPS_CPU_ASE_MCU 0x0080 /* CPU implements MCU ASE */
ad3fea08 1398
17a2f251
TS
1399static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1400static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1401static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
1402\f
1403/* Pseudo-op table.
1404
1405 The following pseudo-ops from the Kane and Heinrich MIPS book
1406 should be defined here, but are currently unsupported: .alias,
1407 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1408
1409 The following pseudo-ops from the Kane and Heinrich MIPS book are
1410 specific to the type of debugging information being generated, and
1411 should be defined by the object format: .aent, .begin, .bend,
1412 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1413 .vreg.
1414
1415 The following pseudo-ops from the Kane and Heinrich MIPS book are
1416 not MIPS CPU specific, but are also not specific to the object file
1417 format. This file is probably the best place to define them, but
d84bcf09 1418 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1419
e972090a
NC
1420static const pseudo_typeS mips_pseudo_table[] =
1421{
beae10d5 1422 /* MIPS specific pseudo-ops. */
252b5132
RH
1423 {"option", s_option, 0},
1424 {"set", s_mipsset, 0},
1425 {"rdata", s_change_sec, 'r'},
1426 {"sdata", s_change_sec, 's'},
1427 {"livereg", s_ignore, 0},
1428 {"abicalls", s_abicalls, 0},
1429 {"cpload", s_cpload, 0},
6478892d
TS
1430 {"cpsetup", s_cpsetup, 0},
1431 {"cplocal", s_cplocal, 0},
252b5132 1432 {"cprestore", s_cprestore, 0},
6478892d 1433 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1434 {"dtprelword", s_dtprelword, 0},
1435 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1436 {"tprelword", s_tprelword, 0},
1437 {"tpreldword", s_tpreldword, 0},
6478892d 1438 {"gpvalue", s_gpvalue, 0},
252b5132 1439 {"gpword", s_gpword, 0},
10181a0d 1440 {"gpdword", s_gpdword, 0},
252b5132
RH
1441 {"cpadd", s_cpadd, 0},
1442 {"insn", s_insn, 0},
1443
beae10d5 1444 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1445 chips. */
38a57ae7 1446 {"asciiz", stringer, 8 + 1},
252b5132
RH
1447 {"bss", s_change_sec, 'b'},
1448 {"err", s_err, 0},
1449 {"half", s_cons, 1},
1450 {"dword", s_cons, 3},
1451 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1452 {"origin", s_org, 0},
1453 {"repeat", s_rept, 0},
252b5132 1454
998b3c36
MR
1455 /* For MIPS this is non-standard, but we define it for consistency. */
1456 {"sbss", s_change_sec, 'B'},
1457
beae10d5 1458 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1459 here for one reason or another. */
1460 {"align", s_align, 0},
1461 {"byte", s_cons, 0},
1462 {"data", s_change_sec, 'd'},
1463 {"double", s_float_cons, 'd'},
1464 {"float", s_float_cons, 'f'},
1465 {"globl", s_mips_globl, 0},
1466 {"global", s_mips_globl, 0},
1467 {"hword", s_cons, 1},
1468 {"int", s_cons, 2},
1469 {"long", s_cons, 2},
1470 {"octa", s_cons, 4},
1471 {"quad", s_cons, 3},
cca86cc8 1472 {"section", s_change_section, 0},
252b5132
RH
1473 {"short", s_cons, 1},
1474 {"single", s_float_cons, 'f'},
1475 {"stabn", s_mips_stab, 'n'},
1476 {"text", s_change_sec, 't'},
1477 {"word", s_cons, 2},
add56521 1478
add56521 1479 { "extern", ecoff_directive_extern, 0},
add56521 1480
43841e91 1481 { NULL, NULL, 0 },
252b5132
RH
1482};
1483
e972090a
NC
1484static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1485{
beae10d5
KH
1486 /* These pseudo-ops should be defined by the object file format.
1487 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1488 {"aent", s_mips_ent, 1},
1489 {"bgnb", s_ignore, 0},
1490 {"end", s_mips_end, 0},
1491 {"endb", s_ignore, 0},
1492 {"ent", s_mips_ent, 0},
c5dd6aab 1493 {"file", s_mips_file, 0},
252b5132
RH
1494 {"fmask", s_mips_mask, 'F'},
1495 {"frame", s_mips_frame, 0},
c5dd6aab 1496 {"loc", s_mips_loc, 0},
252b5132
RH
1497 {"mask", s_mips_mask, 'R'},
1498 {"verstamp", s_ignore, 0},
43841e91 1499 { NULL, NULL, 0 },
252b5132
RH
1500};
1501
3ae8dd8d
MR
1502/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1503 purpose of the `.dc.a' internal pseudo-op. */
1504
1505int
1506mips_address_bytes (void)
1507{
1508 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1509}
1510
17a2f251 1511extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1512
1513void
17a2f251 1514mips_pop_insert (void)
252b5132
RH
1515{
1516 pop_insert (mips_pseudo_table);
1517 if (! ECOFF_DEBUGGING)
1518 pop_insert (mips_nonecoff_pseudo_table);
1519}
1520\f
1521/* Symbols labelling the current insn. */
1522
e972090a
NC
1523struct insn_label_list
1524{
252b5132
RH
1525 struct insn_label_list *next;
1526 symbolS *label;
1527};
1528
252b5132 1529static struct insn_label_list *free_insn_labels;
742a56fe 1530#define label_list tc_segment_info_data.labels
252b5132 1531
17a2f251 1532static void mips_clear_insn_labels (void);
df58fc94
RS
1533static void mips_mark_labels (void);
1534static void mips_compressed_mark_labels (void);
252b5132
RH
1535
1536static inline void
17a2f251 1537mips_clear_insn_labels (void)
252b5132
RH
1538{
1539 register struct insn_label_list **pl;
a8dbcb85 1540 segment_info_type *si;
252b5132 1541
a8dbcb85
TS
1542 if (now_seg)
1543 {
1544 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1545 ;
1546
1547 si = seg_info (now_seg);
1548 *pl = si->label_list;
1549 si->label_list = NULL;
1550 }
252b5132 1551}
a8dbcb85 1552
df58fc94
RS
1553/* Mark instruction labels in MIPS16/microMIPS mode. */
1554
1555static inline void
1556mips_mark_labels (void)
1557{
1558 if (HAVE_CODE_COMPRESSION)
1559 mips_compressed_mark_labels ();
1560}
252b5132
RH
1561\f
1562static char *expr_end;
1563
1564/* Expressions which appear in instructions. These are set by
1565 mips_ip. */
1566
1567static expressionS imm_expr;
5f74bc13 1568static expressionS imm2_expr;
252b5132
RH
1569static expressionS offset_expr;
1570
1571/* Relocs associated with imm_expr and offset_expr. */
1572
f6688943
TS
1573static bfd_reloc_code_real_type imm_reloc[3]
1574 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1575static bfd_reloc_code_real_type offset_reloc[3]
1576 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1577
df58fc94
RS
1578/* This is set to the resulting size of the instruction to be produced
1579 by mips16_ip if an explicit extension is used or by mips_ip if an
1580 explicit size is supplied. */
252b5132 1581
df58fc94 1582static unsigned int forced_insn_length;
252b5132 1583
e1b47bd5
RS
1584/* True if we are assembling an instruction. All dot symbols defined during
1585 this time should be treated as code labels. */
1586
1587static bfd_boolean mips_assembling_insn;
1588
7ed4a06a 1589#ifdef OBJ_ELF
ecb4347a
DJ
1590/* The pdr segment for per procedure frame/regmask info. Not used for
1591 ECOFF debugging. */
252b5132
RH
1592
1593static segT pdr_seg;
7ed4a06a 1594#endif
252b5132 1595
e013f690
TS
1596/* The default target format to use. */
1597
aeffff67
RS
1598#if defined (TE_FreeBSD)
1599#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1600#elif defined (TE_TMIPS)
1601#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1602#else
1603#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1604#endif
1605
e013f690 1606const char *
17a2f251 1607mips_target_format (void)
e013f690
TS
1608{
1609 switch (OUTPUT_FLAVOR)
1610 {
e013f690
TS
1611 case bfd_target_ecoff_flavour:
1612 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1613 case bfd_target_coff_flavour:
1614 return "pe-mips";
1615 case bfd_target_elf_flavour:
0a44bf69
RS
1616#ifdef TE_VXWORKS
1617 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1618 return (target_big_endian
1619 ? "elf32-bigmips-vxworks"
1620 : "elf32-littlemips-vxworks");
1621#endif
e013f690 1622 return (target_big_endian
cfe86eaa 1623 ? (HAVE_64BIT_OBJECTS
aeffff67 1624 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1625 : (HAVE_NEWABI
aeffff67
RS
1626 ? ELF_TARGET ("elf32-n", "big")
1627 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1628 : (HAVE_64BIT_OBJECTS
aeffff67 1629 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1630 : (HAVE_NEWABI
aeffff67
RS
1631 ? ELF_TARGET ("elf32-n", "little")
1632 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1633 default:
1634 abort ();
1635 return NULL;
1636 }
1637}
1638
df58fc94
RS
1639/* Return the length of a microMIPS instruction in bytes. If bits of
1640 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1641 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1642 major opcode) will require further modifications to the opcode
1643 table. */
1644
1645static inline unsigned int
1646micromips_insn_length (const struct mips_opcode *mo)
1647{
1648 return (mo->mask >> 16) == 0 ? 2 : 4;
1649}
1650
5c04167a
RS
1651/* Return the length of MIPS16 instruction OPCODE. */
1652
1653static inline unsigned int
1654mips16_opcode_length (unsigned long opcode)
1655{
1656 return (opcode >> 16) == 0 ? 2 : 4;
1657}
1658
1e915849
RS
1659/* Return the length of instruction INSN. */
1660
1661static inline unsigned int
1662insn_length (const struct mips_cl_insn *insn)
1663{
df58fc94
RS
1664 if (mips_opts.micromips)
1665 return micromips_insn_length (insn->insn_mo);
1666 else if (mips_opts.mips16)
5c04167a 1667 return mips16_opcode_length (insn->insn_opcode);
df58fc94 1668 else
1e915849 1669 return 4;
1e915849
RS
1670}
1671
1672/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1673
1674static void
1675create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1676{
1677 size_t i;
1678
1679 insn->insn_mo = mo;
1e915849
RS
1680 insn->insn_opcode = mo->match;
1681 insn->frag = NULL;
1682 insn->where = 0;
1683 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1684 insn->fixp[i] = NULL;
1685 insn->fixed_p = (mips_opts.noreorder > 0);
1686 insn->noreorder_p = (mips_opts.noreorder > 0);
1687 insn->mips16_absolute_jump_p = 0;
15be625d 1688 insn->complete_p = 0;
e407c74b 1689 insn->cleared_p = 0;
1e915849
RS
1690}
1691
df58fc94 1692/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
1693
1694static void
df58fc94 1695mips_record_compressed_mode (void)
742a56fe
RS
1696{
1697 segment_info_type *si;
1698
1699 si = seg_info (now_seg);
1700 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1701 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
1702 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
1703 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
1704}
1705
4d68580a
RS
1706/* Read a standard MIPS instruction from BUF. */
1707
1708static unsigned long
1709read_insn (char *buf)
1710{
1711 if (target_big_endian)
1712 return bfd_getb32 ((bfd_byte *) buf);
1713 else
1714 return bfd_getl32 ((bfd_byte *) buf);
1715}
1716
1717/* Write standard MIPS instruction INSN to BUF. Return a pointer to
1718 the next byte. */
1719
1720static char *
1721write_insn (char *buf, unsigned int insn)
1722{
1723 md_number_to_chars (buf, insn, 4);
1724 return buf + 4;
1725}
1726
1727/* Read a microMIPS or MIPS16 opcode from BUF, given that it
1728 has length LENGTH. */
1729
1730static unsigned long
1731read_compressed_insn (char *buf, unsigned int length)
1732{
1733 unsigned long insn;
1734 unsigned int i;
1735
1736 insn = 0;
1737 for (i = 0; i < length; i += 2)
1738 {
1739 insn <<= 16;
1740 if (target_big_endian)
1741 insn |= bfd_getb16 ((char *) buf);
1742 else
1743 insn |= bfd_getl16 ((char *) buf);
1744 buf += 2;
1745 }
1746 return insn;
1747}
1748
5c04167a
RS
1749/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
1750 instruction is LENGTH bytes long. Return a pointer to the next byte. */
1751
1752static char *
1753write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
1754{
1755 unsigned int i;
1756
1757 for (i = 0; i < length; i += 2)
1758 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
1759 return buf + length;
1760}
1761
1e915849
RS
1762/* Install INSN at the location specified by its "frag" and "where" fields. */
1763
1764static void
1765install_insn (const struct mips_cl_insn *insn)
1766{
1767 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
1768 if (HAVE_CODE_COMPRESSION)
1769 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 1770 else
4d68580a 1771 write_insn (f, insn->insn_opcode);
df58fc94 1772 mips_record_compressed_mode ();
1e915849
RS
1773}
1774
1775/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1776 and install the opcode in the new location. */
1777
1778static void
1779move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1780{
1781 size_t i;
1782
1783 insn->frag = frag;
1784 insn->where = where;
1785 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1786 if (insn->fixp[i] != NULL)
1787 {
1788 insn->fixp[i]->fx_frag = frag;
1789 insn->fixp[i]->fx_where = where;
1790 }
1791 install_insn (insn);
1792}
1793
1794/* Add INSN to the end of the output. */
1795
1796static void
1797add_fixed_insn (struct mips_cl_insn *insn)
1798{
1799 char *f = frag_more (insn_length (insn));
1800 move_insn (insn, frag_now, f - frag_now->fr_literal);
1801}
1802
1803/* Start a variant frag and move INSN to the start of the variant part,
1804 marking it as fixed. The other arguments are as for frag_var. */
1805
1806static void
1807add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1808 relax_substateT subtype, symbolS *symbol, offsetT offset)
1809{
1810 frag_grow (max_chars);
1811 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1812 insn->fixed_p = 1;
1813 frag_var (rs_machine_dependent, max_chars, var,
1814 subtype, symbol, offset, NULL);
1815}
1816
1817/* Insert N copies of INSN into the history buffer, starting at
1818 position FIRST. Neither FIRST nor N need to be clipped. */
1819
1820static void
1821insert_into_history (unsigned int first, unsigned int n,
1822 const struct mips_cl_insn *insn)
1823{
1824 if (mips_relax.sequence != 2)
1825 {
1826 unsigned int i;
1827
1828 for (i = ARRAY_SIZE (history); i-- > first;)
1829 if (i >= first + n)
1830 history[i] = history[i - n];
1831 else
1832 history[i] = *insn;
1833 }
1834}
1835
71400594
RS
1836/* Initialize vr4120_conflicts. There is a bit of duplication here:
1837 the idea is to make it obvious at a glance that each errata is
1838 included. */
1839
1840static void
1841init_vr4120_conflicts (void)
1842{
1843#define CONFLICT(FIRST, SECOND) \
1844 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1845
1846 /* Errata 21 - [D]DIV[U] after [D]MACC */
1847 CONFLICT (MACC, DIV);
1848 CONFLICT (DMACC, DIV);
1849
1850 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1851 CONFLICT (DMULT, DMULT);
1852 CONFLICT (DMULT, DMACC);
1853 CONFLICT (DMACC, DMULT);
1854 CONFLICT (DMACC, DMACC);
1855
1856 /* Errata 24 - MT{LO,HI} after [D]MACC */
1857 CONFLICT (MACC, MTHILO);
1858 CONFLICT (DMACC, MTHILO);
1859
1860 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1861 instruction is executed immediately after a MACC or DMACC
1862 instruction, the result of [either instruction] is incorrect." */
1863 CONFLICT (MACC, MULT);
1864 CONFLICT (MACC, DMULT);
1865 CONFLICT (DMACC, MULT);
1866 CONFLICT (DMACC, DMULT);
1867
1868 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1869 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1870 DDIV or DDIVU instruction, the result of the MACC or
1871 DMACC instruction is incorrect.". */
1872 CONFLICT (DMULT, MACC);
1873 CONFLICT (DMULT, DMACC);
1874 CONFLICT (DIV, MACC);
1875 CONFLICT (DIV, DMACC);
1876
1877#undef CONFLICT
1878}
1879
707bfff6
TS
1880struct regname {
1881 const char *name;
1882 unsigned int num;
1883};
1884
1885#define RTYPE_MASK 0x1ff00
1886#define RTYPE_NUM 0x00100
1887#define RTYPE_FPU 0x00200
1888#define RTYPE_FCC 0x00400
1889#define RTYPE_VEC 0x00800
1890#define RTYPE_GP 0x01000
1891#define RTYPE_CP0 0x02000
1892#define RTYPE_PC 0x04000
1893#define RTYPE_ACC 0x08000
1894#define RTYPE_CCC 0x10000
1895#define RNUM_MASK 0x000ff
1896#define RWARN 0x80000
1897
1898#define GENERIC_REGISTER_NUMBERS \
1899 {"$0", RTYPE_NUM | 0}, \
1900 {"$1", RTYPE_NUM | 1}, \
1901 {"$2", RTYPE_NUM | 2}, \
1902 {"$3", RTYPE_NUM | 3}, \
1903 {"$4", RTYPE_NUM | 4}, \
1904 {"$5", RTYPE_NUM | 5}, \
1905 {"$6", RTYPE_NUM | 6}, \
1906 {"$7", RTYPE_NUM | 7}, \
1907 {"$8", RTYPE_NUM | 8}, \
1908 {"$9", RTYPE_NUM | 9}, \
1909 {"$10", RTYPE_NUM | 10}, \
1910 {"$11", RTYPE_NUM | 11}, \
1911 {"$12", RTYPE_NUM | 12}, \
1912 {"$13", RTYPE_NUM | 13}, \
1913 {"$14", RTYPE_NUM | 14}, \
1914 {"$15", RTYPE_NUM | 15}, \
1915 {"$16", RTYPE_NUM | 16}, \
1916 {"$17", RTYPE_NUM | 17}, \
1917 {"$18", RTYPE_NUM | 18}, \
1918 {"$19", RTYPE_NUM | 19}, \
1919 {"$20", RTYPE_NUM | 20}, \
1920 {"$21", RTYPE_NUM | 21}, \
1921 {"$22", RTYPE_NUM | 22}, \
1922 {"$23", RTYPE_NUM | 23}, \
1923 {"$24", RTYPE_NUM | 24}, \
1924 {"$25", RTYPE_NUM | 25}, \
1925 {"$26", RTYPE_NUM | 26}, \
1926 {"$27", RTYPE_NUM | 27}, \
1927 {"$28", RTYPE_NUM | 28}, \
1928 {"$29", RTYPE_NUM | 29}, \
1929 {"$30", RTYPE_NUM | 30}, \
1930 {"$31", RTYPE_NUM | 31}
1931
1932#define FPU_REGISTER_NAMES \
1933 {"$f0", RTYPE_FPU | 0}, \
1934 {"$f1", RTYPE_FPU | 1}, \
1935 {"$f2", RTYPE_FPU | 2}, \
1936 {"$f3", RTYPE_FPU | 3}, \
1937 {"$f4", RTYPE_FPU | 4}, \
1938 {"$f5", RTYPE_FPU | 5}, \
1939 {"$f6", RTYPE_FPU | 6}, \
1940 {"$f7", RTYPE_FPU | 7}, \
1941 {"$f8", RTYPE_FPU | 8}, \
1942 {"$f9", RTYPE_FPU | 9}, \
1943 {"$f10", RTYPE_FPU | 10}, \
1944 {"$f11", RTYPE_FPU | 11}, \
1945 {"$f12", RTYPE_FPU | 12}, \
1946 {"$f13", RTYPE_FPU | 13}, \
1947 {"$f14", RTYPE_FPU | 14}, \
1948 {"$f15", RTYPE_FPU | 15}, \
1949 {"$f16", RTYPE_FPU | 16}, \
1950 {"$f17", RTYPE_FPU | 17}, \
1951 {"$f18", RTYPE_FPU | 18}, \
1952 {"$f19", RTYPE_FPU | 19}, \
1953 {"$f20", RTYPE_FPU | 20}, \
1954 {"$f21", RTYPE_FPU | 21}, \
1955 {"$f22", RTYPE_FPU | 22}, \
1956 {"$f23", RTYPE_FPU | 23}, \
1957 {"$f24", RTYPE_FPU | 24}, \
1958 {"$f25", RTYPE_FPU | 25}, \
1959 {"$f26", RTYPE_FPU | 26}, \
1960 {"$f27", RTYPE_FPU | 27}, \
1961 {"$f28", RTYPE_FPU | 28}, \
1962 {"$f29", RTYPE_FPU | 29}, \
1963 {"$f30", RTYPE_FPU | 30}, \
1964 {"$f31", RTYPE_FPU | 31}
1965
1966#define FPU_CONDITION_CODE_NAMES \
1967 {"$fcc0", RTYPE_FCC | 0}, \
1968 {"$fcc1", RTYPE_FCC | 1}, \
1969 {"$fcc2", RTYPE_FCC | 2}, \
1970 {"$fcc3", RTYPE_FCC | 3}, \
1971 {"$fcc4", RTYPE_FCC | 4}, \
1972 {"$fcc5", RTYPE_FCC | 5}, \
1973 {"$fcc6", RTYPE_FCC | 6}, \
1974 {"$fcc7", RTYPE_FCC | 7}
1975
1976#define COPROC_CONDITION_CODE_NAMES \
1977 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
1978 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
1979 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
1980 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
1981 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
1982 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
1983 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
1984 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
1985
1986#define N32N64_SYMBOLIC_REGISTER_NAMES \
1987 {"$a4", RTYPE_GP | 8}, \
1988 {"$a5", RTYPE_GP | 9}, \
1989 {"$a6", RTYPE_GP | 10}, \
1990 {"$a7", RTYPE_GP | 11}, \
1991 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
1992 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
1993 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
1994 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
1995 {"$t0", RTYPE_GP | 12}, \
1996 {"$t1", RTYPE_GP | 13}, \
1997 {"$t2", RTYPE_GP | 14}, \
1998 {"$t3", RTYPE_GP | 15}
1999
2000#define O32_SYMBOLIC_REGISTER_NAMES \
2001 {"$t0", RTYPE_GP | 8}, \
2002 {"$t1", RTYPE_GP | 9}, \
2003 {"$t2", RTYPE_GP | 10}, \
2004 {"$t3", RTYPE_GP | 11}, \
2005 {"$t4", RTYPE_GP | 12}, \
2006 {"$t5", RTYPE_GP | 13}, \
2007 {"$t6", RTYPE_GP | 14}, \
2008 {"$t7", RTYPE_GP | 15}, \
2009 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2010 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2011 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2012 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2013
2014/* Remaining symbolic register names */
2015#define SYMBOLIC_REGISTER_NAMES \
2016 {"$zero", RTYPE_GP | 0}, \
2017 {"$at", RTYPE_GP | 1}, \
2018 {"$AT", RTYPE_GP | 1}, \
2019 {"$v0", RTYPE_GP | 2}, \
2020 {"$v1", RTYPE_GP | 3}, \
2021 {"$a0", RTYPE_GP | 4}, \
2022 {"$a1", RTYPE_GP | 5}, \
2023 {"$a2", RTYPE_GP | 6}, \
2024 {"$a3", RTYPE_GP | 7}, \
2025 {"$s0", RTYPE_GP | 16}, \
2026 {"$s1", RTYPE_GP | 17}, \
2027 {"$s2", RTYPE_GP | 18}, \
2028 {"$s3", RTYPE_GP | 19}, \
2029 {"$s4", RTYPE_GP | 20}, \
2030 {"$s5", RTYPE_GP | 21}, \
2031 {"$s6", RTYPE_GP | 22}, \
2032 {"$s7", RTYPE_GP | 23}, \
2033 {"$t8", RTYPE_GP | 24}, \
2034 {"$t9", RTYPE_GP | 25}, \
2035 {"$k0", RTYPE_GP | 26}, \
2036 {"$kt0", RTYPE_GP | 26}, \
2037 {"$k1", RTYPE_GP | 27}, \
2038 {"$kt1", RTYPE_GP | 27}, \
2039 {"$gp", RTYPE_GP | 28}, \
2040 {"$sp", RTYPE_GP | 29}, \
2041 {"$s8", RTYPE_GP | 30}, \
2042 {"$fp", RTYPE_GP | 30}, \
2043 {"$ra", RTYPE_GP | 31}
2044
2045#define MIPS16_SPECIAL_REGISTER_NAMES \
2046 {"$pc", RTYPE_PC | 0}
2047
2048#define MDMX_VECTOR_REGISTER_NAMES \
2049 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2050 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2051 {"$v2", RTYPE_VEC | 2}, \
2052 {"$v3", RTYPE_VEC | 3}, \
2053 {"$v4", RTYPE_VEC | 4}, \
2054 {"$v5", RTYPE_VEC | 5}, \
2055 {"$v6", RTYPE_VEC | 6}, \
2056 {"$v7", RTYPE_VEC | 7}, \
2057 {"$v8", RTYPE_VEC | 8}, \
2058 {"$v9", RTYPE_VEC | 9}, \
2059 {"$v10", RTYPE_VEC | 10}, \
2060 {"$v11", RTYPE_VEC | 11}, \
2061 {"$v12", RTYPE_VEC | 12}, \
2062 {"$v13", RTYPE_VEC | 13}, \
2063 {"$v14", RTYPE_VEC | 14}, \
2064 {"$v15", RTYPE_VEC | 15}, \
2065 {"$v16", RTYPE_VEC | 16}, \
2066 {"$v17", RTYPE_VEC | 17}, \
2067 {"$v18", RTYPE_VEC | 18}, \
2068 {"$v19", RTYPE_VEC | 19}, \
2069 {"$v20", RTYPE_VEC | 20}, \
2070 {"$v21", RTYPE_VEC | 21}, \
2071 {"$v22", RTYPE_VEC | 22}, \
2072 {"$v23", RTYPE_VEC | 23}, \
2073 {"$v24", RTYPE_VEC | 24}, \
2074 {"$v25", RTYPE_VEC | 25}, \
2075 {"$v26", RTYPE_VEC | 26}, \
2076 {"$v27", RTYPE_VEC | 27}, \
2077 {"$v28", RTYPE_VEC | 28}, \
2078 {"$v29", RTYPE_VEC | 29}, \
2079 {"$v30", RTYPE_VEC | 30}, \
2080 {"$v31", RTYPE_VEC | 31}
2081
2082#define MIPS_DSP_ACCUMULATOR_NAMES \
2083 {"$ac0", RTYPE_ACC | 0}, \
2084 {"$ac1", RTYPE_ACC | 1}, \
2085 {"$ac2", RTYPE_ACC | 2}, \
2086 {"$ac3", RTYPE_ACC | 3}
2087
2088static const struct regname reg_names[] = {
2089 GENERIC_REGISTER_NUMBERS,
2090 FPU_REGISTER_NAMES,
2091 FPU_CONDITION_CODE_NAMES,
2092 COPROC_CONDITION_CODE_NAMES,
2093
2094 /* The $txx registers depends on the abi,
2095 these will be added later into the symbol table from
2096 one of the tables below once mips_abi is set after
2097 parsing of arguments from the command line. */
2098 SYMBOLIC_REGISTER_NAMES,
2099
2100 MIPS16_SPECIAL_REGISTER_NAMES,
2101 MDMX_VECTOR_REGISTER_NAMES,
2102 MIPS_DSP_ACCUMULATOR_NAMES,
2103 {0, 0}
2104};
2105
2106static const struct regname reg_names_o32[] = {
2107 O32_SYMBOLIC_REGISTER_NAMES,
2108 {0, 0}
2109};
2110
2111static const struct regname reg_names_n32n64[] = {
2112 N32N64_SYMBOLIC_REGISTER_NAMES,
2113 {0, 0}
2114};
2115
df58fc94
RS
2116/* Check if S points at a valid register specifier according to TYPES.
2117 If so, then return 1, advance S to consume the specifier and store
2118 the register's number in REGNOP, otherwise return 0. */
2119
707bfff6
TS
2120static int
2121reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2122{
2123 symbolS *symbolP;
2124 char *e;
2125 char save_c;
2126 int reg = -1;
2127
2128 /* Find end of name. */
2129 e = *s;
2130 if (is_name_beginner (*e))
2131 ++e;
2132 while (is_part_of_name (*e))
2133 ++e;
2134
2135 /* Terminate name. */
2136 save_c = *e;
2137 *e = '\0';
2138
2139 /* Look for a register symbol. */
2140 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2141 {
2142 int r = S_GET_VALUE (symbolP);
2143 if (r & types)
2144 reg = r & RNUM_MASK;
2145 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2146 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
2147 reg = (r & RNUM_MASK) - 2;
2148 }
2149 /* Else see if this is a register defined in an itbl entry. */
2150 else if ((types & RTYPE_GP) && itbl_have_entries)
2151 {
2152 char *n = *s;
2153 unsigned long r;
2154
2155 if (*n == '$')
2156 ++n;
2157 if (itbl_get_reg_val (n, &r))
2158 reg = r & RNUM_MASK;
2159 }
2160
2161 /* Advance to next token if a register was recognised. */
2162 if (reg >= 0)
2163 *s = e;
2164 else if (types & RWARN)
20203fb9 2165 as_warn (_("Unrecognized register name `%s'"), *s);
707bfff6
TS
2166
2167 *e = save_c;
2168 if (regnop)
2169 *regnop = reg;
2170 return reg >= 0;
2171}
2172
df58fc94
RS
2173/* Check if S points at a valid register list according to TYPES.
2174 If so, then return 1, advance S to consume the list and store
2175 the registers present on the list as a bitmask of ones in REGLISTP,
2176 otherwise return 0. A valid list comprises a comma-separated
2177 enumeration of valid single registers and/or dash-separated
2178 contiguous register ranges as determined by their numbers.
2179
2180 As a special exception if one of s0-s7 registers is specified as
2181 the range's lower delimiter and s8 (fp) is its upper one, then no
2182 registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2309ddf2 2183 are selected; they have to be listed separately if needed. */
df58fc94
RS
2184
2185static int
2186reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2187{
2188 unsigned int reglist = 0;
2189 unsigned int lastregno;
2190 bfd_boolean ok = TRUE;
2191 unsigned int regmask;
2309ddf2 2192 char *s_endlist = *s;
df58fc94 2193 char *s_reset = *s;
2309ddf2 2194 unsigned int regno;
df58fc94
RS
2195
2196 while (reg_lookup (s, types, &regno))
2197 {
2198 lastregno = regno;
2199 if (**s == '-')
2200 {
2201 (*s)++;
2202 ok = reg_lookup (s, types, &lastregno);
2203 if (ok && lastregno < regno)
2204 ok = FALSE;
2205 if (!ok)
2206 break;
2207 }
2208
2209 if (lastregno == FP && regno >= S0 && regno <= S7)
2210 {
2211 lastregno = S7;
2212 reglist |= 1 << FP;
2213 }
2214 regmask = 1 << lastregno;
2215 regmask = (regmask << 1) - 1;
2216 regmask ^= (1 << regno) - 1;
2217 reglist |= regmask;
2218
2309ddf2 2219 s_endlist = *s;
df58fc94
RS
2220 if (**s != ',')
2221 break;
2222 (*s)++;
2223 }
2224
2225 if (ok)
2309ddf2 2226 *s = s_endlist;
df58fc94
RS
2227 else
2228 *s = s_reset;
2229 if (reglistp)
2230 *reglistp = reglist;
2231 return ok && reglist != 0;
2232}
2233
037b32b9 2234/* Return TRUE if opcode MO is valid on the currently selected ISA and
f79e2745 2235 architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
2236
2237static bfd_boolean
f79e2745 2238is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
2239{
2240 int isa = mips_opts.isa;
2241 int fp_s, fp_d;
2242
2243 if (mips_opts.ase_mdmx)
2244 isa |= INSN_MDMX;
2245 if (mips_opts.ase_dsp)
2246 isa |= INSN_DSP;
2247 if (mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
2248 isa |= INSN_DSP64;
2249 if (mips_opts.ase_dspr2)
2250 isa |= INSN_DSPR2;
2251 if (mips_opts.ase_mt)
2252 isa |= INSN_MT;
2253 if (mips_opts.ase_mips3d)
2254 isa |= INSN_MIPS3D;
2255 if (mips_opts.ase_smartmips)
2256 isa |= INSN_SMARTMIPS;
dec0624d
MR
2257 if (mips_opts.ase_mcu)
2258 isa |= INSN_MCU;
037b32b9 2259
35d0a169 2260 if (!opcode_is_member (mo, isa, mips_opts.arch))
037b32b9
AN
2261 return FALSE;
2262
2263 /* Check whether the instruction or macro requires single-precision or
2264 double-precision floating-point support. Note that this information is
2265 stored differently in the opcode table for insns and macros. */
2266 if (mo->pinfo == INSN_MACRO)
2267 {
2268 fp_s = mo->pinfo2 & INSN2_M_FP_S;
2269 fp_d = mo->pinfo2 & INSN2_M_FP_D;
2270 }
2271 else
2272 {
2273 fp_s = mo->pinfo & FP_S;
2274 fp_d = mo->pinfo & FP_D;
2275 }
2276
2277 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2278 return FALSE;
2279
2280 if (fp_s && mips_opts.soft_float)
2281 return FALSE;
2282
2283 return TRUE;
2284}
2285
2286/* Return TRUE if the MIPS16 opcode MO is valid on the currently
2287 selected ISA and architecture. */
2288
2289static bfd_boolean
2290is_opcode_valid_16 (const struct mips_opcode *mo)
2291{
35d0a169 2292 return opcode_is_member (mo, mips_opts.isa, mips_opts.arch);
037b32b9
AN
2293}
2294
df58fc94
RS
2295/* Return TRUE if the size of the microMIPS opcode MO matches one
2296 explicitly requested. Always TRUE in the standard MIPS mode. */
2297
2298static bfd_boolean
2299is_size_valid (const struct mips_opcode *mo)
2300{
2301 if (!mips_opts.micromips)
2302 return TRUE;
2303
2304 if (!forced_insn_length)
2305 return TRUE;
2306 if (mo->pinfo == INSN_MACRO)
2307 return FALSE;
2308 return forced_insn_length == micromips_insn_length (mo);
2309}
2310
2311/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
2312 of the preceding instruction. Always TRUE in the standard MIPS mode.
2313
2314 We don't accept macros in 16-bit delay slots to avoid a case where
2315 a macro expansion fails because it relies on a preceding 32-bit real
2316 instruction to have matched and does not handle the operands correctly.
2317 The only macros that may expand to 16-bit instructions are JAL that
2318 cannot be placed in a delay slot anyway, and corner cases of BALIGN
2319 and BGT (that likewise cannot be placed in a delay slot) that decay to
2320 a NOP. In all these cases the macros precede any corresponding real
2321 instruction definitions in the opcode table, so they will match in the
2322 second pass where the size of the delay slot is ignored and therefore
2323 produce correct code. */
df58fc94
RS
2324
2325static bfd_boolean
2326is_delay_slot_valid (const struct mips_opcode *mo)
2327{
2328 if (!mips_opts.micromips)
2329 return TRUE;
2330
2331 if (mo->pinfo == INSN_MACRO)
c06dec14 2332 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
2333 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2334 && micromips_insn_length (mo) != 4)
2335 return FALSE;
2336 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2337 && micromips_insn_length (mo) != 2)
2338 return FALSE;
2339
2340 return TRUE;
2341}
2342
707bfff6
TS
2343/* This function is called once, at assembler startup time. It should set up
2344 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 2345
252b5132 2346void
17a2f251 2347md_begin (void)
252b5132 2348{
3994f87e 2349 const char *retval = NULL;
156c2f8b 2350 int i = 0;
252b5132 2351 int broken = 0;
1f25f5d3 2352
0a44bf69
RS
2353 if (mips_pic != NO_PIC)
2354 {
2355 if (g_switch_seen && g_switch_value != 0)
2356 as_bad (_("-G may not be used in position-independent code"));
2357 g_switch_value = 0;
2358 }
2359
fef14a42 2360 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
2361 as_warn (_("Could not set architecture and machine"));
2362
252b5132
RH
2363 op_hash = hash_new ();
2364
2365 for (i = 0; i < NUMOPCODES;)
2366 {
2367 const char *name = mips_opcodes[i].name;
2368
17a2f251 2369 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
2370 if (retval != NULL)
2371 {
2372 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2373 mips_opcodes[i].name, retval);
2374 /* Probably a memory allocation problem? Give up now. */
2375 as_fatal (_("Broken assembler. No assembly attempted."));
2376 }
2377 do
2378 {
2379 if (mips_opcodes[i].pinfo != INSN_MACRO)
2380 {
2381 if (!validate_mips_insn (&mips_opcodes[i]))
2382 broken = 1;
1e915849
RS
2383 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2384 {
2385 create_insn (&nop_insn, mips_opcodes + i);
c67a084a
NC
2386 if (mips_fix_loongson2f_nop)
2387 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
1e915849
RS
2388 nop_insn.fixed_p = 1;
2389 }
252b5132
RH
2390 }
2391 ++i;
2392 }
2393 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2394 }
2395
2396 mips16_op_hash = hash_new ();
2397
2398 i = 0;
2399 while (i < bfd_mips16_num_opcodes)
2400 {
2401 const char *name = mips16_opcodes[i].name;
2402
17a2f251 2403 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
2404 if (retval != NULL)
2405 as_fatal (_("internal: can't hash `%s': %s"),
2406 mips16_opcodes[i].name, retval);
2407 do
2408 {
2409 if (mips16_opcodes[i].pinfo != INSN_MACRO
2410 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2411 != mips16_opcodes[i].match))
2412 {
2413 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2414 mips16_opcodes[i].name, mips16_opcodes[i].args);
2415 broken = 1;
2416 }
1e915849
RS
2417 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2418 {
2419 create_insn (&mips16_nop_insn, mips16_opcodes + i);
2420 mips16_nop_insn.fixed_p = 1;
2421 }
252b5132
RH
2422 ++i;
2423 }
2424 while (i < bfd_mips16_num_opcodes
2425 && strcmp (mips16_opcodes[i].name, name) == 0);
2426 }
2427
df58fc94
RS
2428 micromips_op_hash = hash_new ();
2429
2430 i = 0;
2431 while (i < bfd_micromips_num_opcodes)
2432 {
2433 const char *name = micromips_opcodes[i].name;
2434
2435 retval = hash_insert (micromips_op_hash, name,
2436 (void *) &micromips_opcodes[i]);
2437 if (retval != NULL)
2438 as_fatal (_("internal: can't hash `%s': %s"),
2439 micromips_opcodes[i].name, retval);
2440 do
2441 if (micromips_opcodes[i].pinfo != INSN_MACRO)
2442 {
2443 struct mips_cl_insn *micromips_nop_insn;
2444
2445 if (!validate_micromips_insn (&micromips_opcodes[i]))
2446 broken = 1;
2447
2448 if (micromips_insn_length (micromips_opcodes + i) == 2)
2449 micromips_nop_insn = &micromips_nop16_insn;
2450 else if (micromips_insn_length (micromips_opcodes + i) == 4)
2451 micromips_nop_insn = &micromips_nop32_insn;
2452 else
2453 continue;
2454
2455 if (micromips_nop_insn->insn_mo == NULL
2456 && strcmp (name, "nop") == 0)
2457 {
2458 create_insn (micromips_nop_insn, micromips_opcodes + i);
2459 micromips_nop_insn->fixed_p = 1;
2460 }
2461 }
2462 while (++i < bfd_micromips_num_opcodes
2463 && strcmp (micromips_opcodes[i].name, name) == 0);
2464 }
2465
252b5132
RH
2466 if (broken)
2467 as_fatal (_("Broken assembler. No assembly attempted."));
2468
2469 /* We add all the general register names to the symbol table. This
2470 helps us detect invalid uses of them. */
707bfff6
TS
2471 for (i = 0; reg_names[i].name; i++)
2472 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 2473 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
2474 &zero_address_frag));
2475 if (HAVE_NEWABI)
2476 for (i = 0; reg_names_n32n64[i].name; i++)
2477 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 2478 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 2479 &zero_address_frag));
707bfff6
TS
2480 else
2481 for (i = 0; reg_names_o32[i].name; i++)
2482 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 2483 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 2484 &zero_address_frag));
6047c971 2485
7d10b47d 2486 mips_no_prev_insn ();
252b5132
RH
2487
2488 mips_gprmask = 0;
2489 mips_cprmask[0] = 0;
2490 mips_cprmask[1] = 0;
2491 mips_cprmask[2] = 0;
2492 mips_cprmask[3] = 0;
2493
2494 /* set the default alignment for the text section (2**2) */
2495 record_alignment (text_section, 2);
2496
4d0d148d 2497 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 2498
707bfff6 2499#ifdef OBJ_ELF
f43abd2b 2500 if (IS_ELF)
252b5132 2501 {
0a44bf69
RS
2502 /* On a native system other than VxWorks, sections must be aligned
2503 to 16 byte boundaries. When configured for an embedded ELF
2504 target, we don't bother. */
c41e87e3
CF
2505 if (strncmp (TARGET_OS, "elf", 3) != 0
2506 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132
RH
2507 {
2508 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2509 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2510 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2511 }
2512
2513 /* Create a .reginfo section for register masks and a .mdebug
2514 section for debugging information. */
2515 {
2516 segT seg;
2517 subsegT subseg;
2518 flagword flags;
2519 segT sec;
2520
2521 seg = now_seg;
2522 subseg = now_subseg;
2523
2524 /* The ABI says this section should be loaded so that the
2525 running program can access it. However, we don't load it
2526 if we are configured for an embedded target */
2527 flags = SEC_READONLY | SEC_DATA;
c41e87e3 2528 if (strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
2529 flags |= SEC_ALLOC | SEC_LOAD;
2530
316f5878 2531 if (mips_abi != N64_ABI)
252b5132
RH
2532 {
2533 sec = subseg_new (".reginfo", (subsegT) 0);
2534
195325d2
TS
2535 bfd_set_section_flags (stdoutput, sec, flags);
2536 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 2537
252b5132 2538 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
252b5132
RH
2539 }
2540 else
2541 {
2542 /* The 64-bit ABI uses a .MIPS.options section rather than
2543 .reginfo section. */
2544 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
2545 bfd_set_section_flags (stdoutput, sec, flags);
2546 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 2547
252b5132
RH
2548 /* Set up the option header. */
2549 {
2550 Elf_Internal_Options opthdr;
2551 char *f;
2552
2553 opthdr.kind = ODK_REGINFO;
2554 opthdr.size = (sizeof (Elf_External_Options)
2555 + sizeof (Elf64_External_RegInfo));
2556 opthdr.section = 0;
2557 opthdr.info = 0;
2558 f = frag_more (sizeof (Elf_External_Options));
2559 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2560 (Elf_External_Options *) f);
2561
2562 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2563 }
252b5132
RH
2564 }
2565
2566 if (ECOFF_DEBUGGING)
2567 {
2568 sec = subseg_new (".mdebug", (subsegT) 0);
2569 (void) bfd_set_section_flags (stdoutput, sec,
2570 SEC_HAS_CONTENTS | SEC_READONLY);
2571 (void) bfd_set_section_alignment (stdoutput, sec, 2);
2572 }
f43abd2b 2573 else if (mips_flag_pdr)
ecb4347a
DJ
2574 {
2575 pdr_seg = subseg_new (".pdr", (subsegT) 0);
2576 (void) bfd_set_section_flags (stdoutput, pdr_seg,
2577 SEC_READONLY | SEC_RELOC
2578 | SEC_DEBUGGING);
2579 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2580 }
252b5132
RH
2581
2582 subseg_set (seg, subseg);
2583 }
2584 }
707bfff6 2585#endif /* OBJ_ELF */
252b5132
RH
2586
2587 if (! ECOFF_DEBUGGING)
2588 md_obj_begin ();
71400594
RS
2589
2590 if (mips_fix_vr4120)
2591 init_vr4120_conflicts ();
252b5132
RH
2592}
2593
2594void
17a2f251 2595md_mips_end (void)
252b5132 2596{
02b1ab82 2597 mips_emit_delays ();
252b5132
RH
2598 if (! ECOFF_DEBUGGING)
2599 md_obj_end ();
2600}
2601
2602void
17a2f251 2603md_assemble (char *str)
252b5132
RH
2604{
2605 struct mips_cl_insn insn;
f6688943
TS
2606 bfd_reloc_code_real_type unused_reloc[3]
2607 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
2608
2609 imm_expr.X_op = O_absent;
5f74bc13 2610 imm2_expr.X_op = O_absent;
252b5132 2611 offset_expr.X_op = O_absent;
f6688943
TS
2612 imm_reloc[0] = BFD_RELOC_UNUSED;
2613 imm_reloc[1] = BFD_RELOC_UNUSED;
2614 imm_reloc[2] = BFD_RELOC_UNUSED;
2615 offset_reloc[0] = BFD_RELOC_UNUSED;
2616 offset_reloc[1] = BFD_RELOC_UNUSED;
2617 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 2618
e1b47bd5
RS
2619 mips_mark_labels ();
2620 mips_assembling_insn = TRUE;
2621
252b5132
RH
2622 if (mips_opts.mips16)
2623 mips16_ip (str, &insn);
2624 else
2625 {
2626 mips_ip (str, &insn);
beae10d5
KH
2627 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2628 str, insn.insn_opcode));
252b5132
RH
2629 }
2630
2631 if (insn_error)
e1b47bd5
RS
2632 as_bad ("%s `%s'", insn_error, str);
2633 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 2634 {
584892a6 2635 macro_start ();
252b5132
RH
2636 if (mips_opts.mips16)
2637 mips16_macro (&insn);
2638 else
2639 macro (&insn);
584892a6 2640 macro_end ();
252b5132
RH
2641 }
2642 else
2643 {
2644 if (imm_expr.X_op != O_absent)
df58fc94 2645 append_insn (&insn, &imm_expr, imm_reloc, FALSE);
252b5132 2646 else if (offset_expr.X_op != O_absent)
df58fc94 2647 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 2648 else
df58fc94 2649 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 2650 }
e1b47bd5
RS
2651
2652 mips_assembling_insn = FALSE;
252b5132
RH
2653}
2654
738e5348
RS
2655/* Convenience functions for abstracting away the differences between
2656 MIPS16 and non-MIPS16 relocations. */
2657
2658static inline bfd_boolean
2659mips16_reloc_p (bfd_reloc_code_real_type reloc)
2660{
2661 switch (reloc)
2662 {
2663 case BFD_RELOC_MIPS16_JMP:
2664 case BFD_RELOC_MIPS16_GPREL:
2665 case BFD_RELOC_MIPS16_GOT16:
2666 case BFD_RELOC_MIPS16_CALL16:
2667 case BFD_RELOC_MIPS16_HI16_S:
2668 case BFD_RELOC_MIPS16_HI16:
2669 case BFD_RELOC_MIPS16_LO16:
2670 return TRUE;
2671
2672 default:
2673 return FALSE;
2674 }
2675}
2676
df58fc94
RS
2677static inline bfd_boolean
2678micromips_reloc_p (bfd_reloc_code_real_type reloc)
2679{
2680 switch (reloc)
2681 {
2682 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2683 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2684 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2685 case BFD_RELOC_MICROMIPS_GPREL16:
2686 case BFD_RELOC_MICROMIPS_JMP:
2687 case BFD_RELOC_MICROMIPS_HI16:
2688 case BFD_RELOC_MICROMIPS_HI16_S:
2689 case BFD_RELOC_MICROMIPS_LO16:
2690 case BFD_RELOC_MICROMIPS_LITERAL:
2691 case BFD_RELOC_MICROMIPS_GOT16:
2692 case BFD_RELOC_MICROMIPS_CALL16:
2693 case BFD_RELOC_MICROMIPS_GOT_HI16:
2694 case BFD_RELOC_MICROMIPS_GOT_LO16:
2695 case BFD_RELOC_MICROMIPS_CALL_HI16:
2696 case BFD_RELOC_MICROMIPS_CALL_LO16:
2697 case BFD_RELOC_MICROMIPS_SUB:
2698 case BFD_RELOC_MICROMIPS_GOT_PAGE:
2699 case BFD_RELOC_MICROMIPS_GOT_OFST:
2700 case BFD_RELOC_MICROMIPS_GOT_DISP:
2701 case BFD_RELOC_MICROMIPS_HIGHEST:
2702 case BFD_RELOC_MICROMIPS_HIGHER:
2703 case BFD_RELOC_MICROMIPS_SCN_DISP:
2704 case BFD_RELOC_MICROMIPS_JALR:
2705 return TRUE;
2706
2707 default:
2708 return FALSE;
2709 }
2710}
2711
2309ddf2
MR
2712static inline bfd_boolean
2713jmp_reloc_p (bfd_reloc_code_real_type reloc)
2714{
2715 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
2716}
2717
738e5348
RS
2718static inline bfd_boolean
2719got16_reloc_p (bfd_reloc_code_real_type reloc)
2720{
2309ddf2 2721 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 2722 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
2723}
2724
2725static inline bfd_boolean
2726hi16_reloc_p (bfd_reloc_code_real_type reloc)
2727{
2309ddf2 2728 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 2729 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
2730}
2731
2732static inline bfd_boolean
2733lo16_reloc_p (bfd_reloc_code_real_type reloc)
2734{
2309ddf2 2735 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
2736 || reloc == BFD_RELOC_MICROMIPS_LO16);
2737}
2738
df58fc94
RS
2739static inline bfd_boolean
2740jalr_reloc_p (bfd_reloc_code_real_type reloc)
2741{
2309ddf2 2742 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
2743}
2744
5919d012 2745/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
2746 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
2747 need a matching %lo() when applied to local symbols. */
5919d012
RS
2748
2749static inline bfd_boolean
17a2f251 2750reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 2751{
3b91255e 2752 return (HAVE_IN_PLACE_ADDENDS
738e5348 2753 && (hi16_reloc_p (reloc)
0a44bf69
RS
2754 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2755 all GOT16 relocations evaluate to "G". */
738e5348
RS
2756 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
2757}
2758
2759/* Return the type of %lo() reloc needed by RELOC, given that
2760 reloc_needs_lo_p. */
2761
2762static inline bfd_reloc_code_real_type
2763matching_lo_reloc (bfd_reloc_code_real_type reloc)
2764{
df58fc94
RS
2765 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
2766 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
2767 : BFD_RELOC_LO16));
5919d012
RS
2768}
2769
2770/* Return true if the given fixup is followed by a matching R_MIPS_LO16
2771 relocation. */
2772
2773static inline bfd_boolean
17a2f251 2774fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
2775{
2776 return (fixp->fx_next != NULL
738e5348 2777 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
2778 && fixp->fx_addsy == fixp->fx_next->fx_addsy
2779 && fixp->fx_offset == fixp->fx_next->fx_offset);
2780}
2781
252b5132
RH
2782/* This function returns true if modifying a register requires a
2783 delay. */
2784
2785static int
17a2f251 2786reg_needs_delay (unsigned int reg)
252b5132
RH
2787{
2788 unsigned long prev_pinfo;
2789
47e39b9d 2790 prev_pinfo = history[0].insn_mo->pinfo;
252b5132 2791 if (! mips_opts.noreorder
81912461
ILT
2792 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2793 && ! gpr_interlocks)
2794 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2795 && ! cop_interlocks)))
252b5132 2796 {
81912461
ILT
2797 /* A load from a coprocessor or from memory. All load delays
2798 delay the use of general register rt for one instruction. */
bdaaa2e1 2799 /* Itbl support may require additional care here. */
252b5132 2800 know (prev_pinfo & INSN_WRITE_GPR_T);
df58fc94 2801 if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
252b5132
RH
2802 return 1;
2803 }
2804
2805 return 0;
2806}
2807
462427c4
RS
2808/* Move all labels in LABELS to the current insertion point. TEXT_P
2809 says whether the labels refer to text or data. */
404a8071
RS
2810
2811static void
462427c4 2812mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
2813{
2814 struct insn_label_list *l;
2815 valueT val;
2816
462427c4 2817 for (l = labels; l != NULL; l = l->next)
404a8071 2818 {
9c2799c2 2819 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
2820 symbol_set_frag (l->label, frag_now);
2821 val = (valueT) frag_now_fix ();
df58fc94 2822 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 2823 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
2824 ++val;
2825 S_SET_VALUE (l->label, val);
2826 }
2827}
2828
462427c4
RS
2829/* Move all labels in insn_labels to the current insertion point
2830 and treat them as text labels. */
2831
2832static void
2833mips_move_text_labels (void)
2834{
2835 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
2836}
2837
5f0fe04b
TS
2838static bfd_boolean
2839s_is_linkonce (symbolS *sym, segT from_seg)
2840{
2841 bfd_boolean linkonce = FALSE;
2842 segT symseg = S_GET_SEGMENT (sym);
2843
2844 if (symseg != from_seg && !S_IS_LOCAL (sym))
2845 {
2846 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2847 linkonce = TRUE;
2848#ifdef OBJ_ELF
2849 /* The GNU toolchain uses an extension for ELF: a section
2850 beginning with the magic string .gnu.linkonce is a
2851 linkonce section. */
2852 if (strncmp (segment_name (symseg), ".gnu.linkonce",
2853 sizeof ".gnu.linkonce" - 1) == 0)
2854 linkonce = TRUE;
2855#endif
2856 }
2857 return linkonce;
2858}
2859
e1b47bd5 2860/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
2861 linker to handle them specially, such as generating jalx instructions
2862 when needed. We also make them odd for the duration of the assembly,
2863 in order to generate the right sort of code. We will make them even
252b5132
RH
2864 in the adjust_symtab routine, while leaving them marked. This is
2865 convenient for the debugger and the disassembler. The linker knows
2866 to make them odd again. */
2867
2868static void
e1b47bd5 2869mips_compressed_mark_label (symbolS *label)
252b5132 2870{
df58fc94 2871 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 2872
a8dbcb85 2873#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
e1b47bd5
RS
2874 if (IS_ELF)
2875 {
2876 if (mips_opts.mips16)
2877 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
2878 else
2879 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
252b5132 2880 }
e1b47bd5
RS
2881#endif
2882 if ((S_GET_VALUE (label) & 1) == 0
2883 /* Don't adjust the address if the label is global or weak, or
2884 in a link-once section, since we'll be emitting symbol reloc
2885 references to it which will be patched up by the linker, and
2886 the final value of the symbol may or may not be MIPS16/microMIPS. */
2887 && !S_IS_WEAK (label)
2888 && !S_IS_EXTERNAL (label)
2889 && !s_is_linkonce (label, now_seg))
2890 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
2891}
2892
2893/* Mark preceding MIPS16 or microMIPS instruction labels. */
2894
2895static void
2896mips_compressed_mark_labels (void)
2897{
2898 struct insn_label_list *l;
2899
2900 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
2901 mips_compressed_mark_label (l->label);
252b5132
RH
2902}
2903
4d7206a2
RS
2904/* End the current frag. Make it a variant frag and record the
2905 relaxation info. */
2906
2907static void
2908relax_close_frag (void)
2909{
584892a6 2910 mips_macro_warning.first_frag = frag_now;
4d7206a2 2911 frag_var (rs_machine_dependent, 0, 0,
584892a6 2912 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
2913 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2914
2915 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2916 mips_relax.first_fixup = 0;
2917}
2918
2919/* Start a new relaxation sequence whose expansion depends on SYMBOL.
2920 See the comment above RELAX_ENCODE for more details. */
2921
2922static void
2923relax_start (symbolS *symbol)
2924{
9c2799c2 2925 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
2926 mips_relax.sequence = 1;
2927 mips_relax.symbol = symbol;
2928}
2929
2930/* Start generating the second version of a relaxable sequence.
2931 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
2932
2933static void
4d7206a2
RS
2934relax_switch (void)
2935{
9c2799c2 2936 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
2937 mips_relax.sequence = 2;
2938}
2939
2940/* End the current relaxable sequence. */
2941
2942static void
2943relax_end (void)
2944{
9c2799c2 2945 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
2946 relax_close_frag ();
2947 mips_relax.sequence = 0;
2948}
2949
11625dd8
RS
2950/* Return true if IP is a delayed branch or jump. */
2951
2952static inline bfd_boolean
2953delayed_branch_p (const struct mips_cl_insn *ip)
2954{
2955 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2956 | INSN_COND_BRANCH_DELAY
2957 | INSN_COND_BRANCH_LIKELY)) != 0;
2958}
2959
2960/* Return true if IP is a compact branch or jump. */
2961
2962static inline bfd_boolean
2963compact_branch_p (const struct mips_cl_insn *ip)
2964{
2965 if (mips_opts.mips16)
2966 return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
2967 | MIPS16_INSN_COND_BRANCH)) != 0;
2968 else
2969 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
2970 | INSN2_COND_BRANCH)) != 0;
2971}
2972
2973/* Return true if IP is an unconditional branch or jump. */
2974
2975static inline bfd_boolean
2976uncond_branch_p (const struct mips_cl_insn *ip)
2977{
2978 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
2979 || (mips_opts.mips16
2980 ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
2981 : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
2982}
2983
2984/* Return true if IP is a branch-likely instruction. */
2985
2986static inline bfd_boolean
2987branch_likely_p (const struct mips_cl_insn *ip)
2988{
2989 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
2990}
2991
14fe068b
RS
2992/* Return the type of nop that should be used to fill the delay slot
2993 of delayed branch IP. */
2994
2995static struct mips_cl_insn *
2996get_delay_slot_nop (const struct mips_cl_insn *ip)
2997{
2998 if (mips_opts.micromips
2999 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3000 return &micromips_nop32_insn;
3001 return NOP_INSN;
3002}
3003
2309ddf2 3004/* Return the mask of core registers that IP reads or writes. */
df58fc94
RS
3005
3006static unsigned int
3007gpr_mod_mask (const struct mips_cl_insn *ip)
3008{
2309ddf2 3009 unsigned long pinfo2;
df58fc94
RS
3010 unsigned int mask;
3011
3012 mask = 0;
df58fc94
RS
3013 pinfo2 = ip->insn_mo->pinfo2;
3014 if (mips_opts.micromips)
3015 {
df58fc94
RS
3016 if (pinfo2 & INSN2_MOD_GPR_MD)
3017 mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
df58fc94
RS
3018 if (pinfo2 & INSN2_MOD_GPR_MF)
3019 mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
df58fc94
RS
3020 if (pinfo2 & INSN2_MOD_SP)
3021 mask |= 1 << SP;
3022 }
3023 return mask;
3024}
3025
4c260379
RS
3026/* Return the mask of core registers that IP reads. */
3027
3028static unsigned int
3029gpr_read_mask (const struct mips_cl_insn *ip)
3030{
3031 unsigned long pinfo, pinfo2;
3032 unsigned int mask;
3033
df58fc94 3034 mask = gpr_mod_mask (ip);
4c260379
RS
3035 pinfo = ip->insn_mo->pinfo;
3036 pinfo2 = ip->insn_mo->pinfo2;
3037 if (mips_opts.mips16)
3038 {
3039 if (pinfo & MIPS16_INSN_READ_X)
3040 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3041 if (pinfo & MIPS16_INSN_READ_Y)
3042 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3043 if (pinfo & MIPS16_INSN_READ_T)
3044 mask |= 1 << TREG;
3045 if (pinfo & MIPS16_INSN_READ_SP)
3046 mask |= 1 << SP;
3047 if (pinfo & MIPS16_INSN_READ_31)
3048 mask |= 1 << RA;
3049 if (pinfo & MIPS16_INSN_READ_Z)
3050 mask |= 1 << (mips16_to_32_reg_map
3051 [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3052 if (pinfo & MIPS16_INSN_READ_GPR_X)
3053 mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3054 }
3055 else
3056 {
3057 if (pinfo2 & INSN2_READ_GPR_D)
2309ddf2 3058 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3059 if (pinfo & INSN_READ_GPR_T)
2309ddf2 3060 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4c260379 3061 if (pinfo & INSN_READ_GPR_S)
2309ddf2
MR
3062 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3063 if (pinfo2 & INSN2_READ_GP)
3064 mask |= 1 << GP;
3065 if (pinfo2 & INSN2_READ_GPR_31)
3066 mask |= 1 << RA;
4c260379 3067 if (pinfo2 & INSN2_READ_GPR_Z)
2309ddf2 3068 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3069 }
2b0c8b40
MR
3070 if (mips_opts.micromips)
3071 {
3072 if (pinfo2 & INSN2_READ_GPR_MC)
3073 mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3074 if (pinfo2 & INSN2_READ_GPR_ME)
3075 mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3076 if (pinfo2 & INSN2_READ_GPR_MG)
3077 mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3078 if (pinfo2 & INSN2_READ_GPR_MJ)
3079 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3080 if (pinfo2 & INSN2_READ_GPR_MMN)
3081 {
3082 mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3083 mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3084 }
3085 if (pinfo2 & INSN2_READ_GPR_MP)
3086 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3087 if (pinfo2 & INSN2_READ_GPR_MQ)
3088 mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3089 }
fe35f09f
RS
3090 /* Don't include register 0. */
3091 return mask & ~1;
4c260379
RS
3092}
3093
3094/* Return the mask of core registers that IP writes. */
3095
3096static unsigned int
3097gpr_write_mask (const struct mips_cl_insn *ip)
3098{
3099 unsigned long pinfo, pinfo2;
3100 unsigned int mask;
3101
df58fc94 3102 mask = gpr_mod_mask (ip);
4c260379
RS
3103 pinfo = ip->insn_mo->pinfo;
3104 pinfo2 = ip->insn_mo->pinfo2;
3105 if (mips_opts.mips16)
3106 {
3107 if (pinfo & MIPS16_INSN_WRITE_X)
3108 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3109 if (pinfo & MIPS16_INSN_WRITE_Y)
3110 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3111 if (pinfo & MIPS16_INSN_WRITE_Z)
3112 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3113 if (pinfo & MIPS16_INSN_WRITE_T)
3114 mask |= 1 << TREG;
3115 if (pinfo & MIPS16_INSN_WRITE_SP)
3116 mask |= 1 << SP;
3117 if (pinfo & MIPS16_INSN_WRITE_31)
3118 mask |= 1 << RA;
3119 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3120 mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3121 }
3122 else
3123 {
3124 if (pinfo & INSN_WRITE_GPR_D)
df58fc94 3125 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3126 if (pinfo & INSN_WRITE_GPR_T)
df58fc94 3127 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
2b0c8b40 3128 if (pinfo & INSN_WRITE_GPR_S)
2309ddf2 3129 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379
RS
3130 if (pinfo & INSN_WRITE_GPR_31)
3131 mask |= 1 << RA;
3132 if (pinfo2 & INSN2_WRITE_GPR_Z)
df58fc94 3133 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3134 }
2b0c8b40
MR
3135 if (mips_opts.micromips)
3136 {
3137 if (pinfo2 & INSN2_WRITE_GPR_MB)
3138 mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3139 if (pinfo2 & INSN2_WRITE_GPR_MHI)
3140 {
3141 mask |= 1 << micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
3142 mask |= 1 << micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
3143 }
3144 if (pinfo2 & INSN2_WRITE_GPR_MJ)
3145 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3146 if (pinfo2 & INSN2_WRITE_GPR_MP)
3147 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3148 }
fe35f09f
RS
3149 /* Don't include register 0. */
3150 return mask & ~1;
4c260379
RS
3151}
3152
3153/* Return the mask of floating-point registers that IP reads. */
3154
3155static unsigned int
3156fpr_read_mask (const struct mips_cl_insn *ip)
3157{
3158 unsigned long pinfo, pinfo2;
3159 unsigned int mask;
3160
3161 mask = 0;
3162 pinfo = ip->insn_mo->pinfo;
3163 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3164 if (!mips_opts.mips16)
df58fc94
RS
3165 {
3166 if (pinfo2 & INSN2_READ_FPR_D)
2309ddf2 3167 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3168 if (pinfo & INSN_READ_FPR_S)
df58fc94 3169 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3170 if (pinfo & INSN_READ_FPR_T)
df58fc94 3171 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3172 if (pinfo & INSN_READ_FPR_R)
df58fc94 3173 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
4c260379 3174 if (pinfo2 & INSN2_READ_FPR_Z)
df58fc94 3175 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3176 }
3177 /* Conservatively treat all operands to an FP_D instruction are doubles.
3178 (This is overly pessimistic for things like cvt.d.s.) */
3179 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3180 mask |= mask << 1;
3181 return mask;
3182}
3183
3184/* Return the mask of floating-point registers that IP writes. */
3185
3186static unsigned int
3187fpr_write_mask (const struct mips_cl_insn *ip)
3188{
3189 unsigned long pinfo, pinfo2;
3190 unsigned int mask;
3191
3192 mask = 0;
3193 pinfo = ip->insn_mo->pinfo;
3194 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3195 if (!mips_opts.mips16)
4c260379
RS
3196 {
3197 if (pinfo & INSN_WRITE_FPR_D)
df58fc94 3198 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3199 if (pinfo & INSN_WRITE_FPR_S)
df58fc94 3200 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3201 if (pinfo & INSN_WRITE_FPR_T)
df58fc94 3202 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3203 if (pinfo2 & INSN2_WRITE_FPR_Z)
df58fc94 3204 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3205 }
3206 /* Conservatively treat all operands to an FP_D instruction are doubles.
3207 (This is overly pessimistic for things like cvt.s.d.) */
3208 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3209 mask |= mask << 1;
3210 return mask;
3211}
3212
71400594
RS
3213/* Classify an instruction according to the FIX_VR4120_* enumeration.
3214 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3215 by VR4120 errata. */
4d7206a2 3216
71400594
RS
3217static unsigned int
3218classify_vr4120_insn (const char *name)
252b5132 3219{
71400594
RS
3220 if (strncmp (name, "macc", 4) == 0)
3221 return FIX_VR4120_MACC;
3222 if (strncmp (name, "dmacc", 5) == 0)
3223 return FIX_VR4120_DMACC;
3224 if (strncmp (name, "mult", 4) == 0)
3225 return FIX_VR4120_MULT;
3226 if (strncmp (name, "dmult", 5) == 0)
3227 return FIX_VR4120_DMULT;
3228 if (strstr (name, "div"))
3229 return FIX_VR4120_DIV;
3230 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3231 return FIX_VR4120_MTHILO;
3232 return NUM_FIX_VR4120_CLASSES;
3233}
252b5132 3234
ff239038
CM
3235#define INSN_ERET 0x42000018
3236#define INSN_DERET 0x4200001f
3237
71400594
RS
3238/* Return the number of instructions that must separate INSN1 and INSN2,
3239 where INSN1 is the earlier instruction. Return the worst-case value
3240 for any INSN2 if INSN2 is null. */
252b5132 3241
71400594
RS
3242static unsigned int
3243insns_between (const struct mips_cl_insn *insn1,
3244 const struct mips_cl_insn *insn2)
3245{
3246 unsigned long pinfo1, pinfo2;
4c260379 3247 unsigned int mask;
71400594
RS
3248
3249 /* This function needs to know which pinfo flags are set for INSN2
3250 and which registers INSN2 uses. The former is stored in PINFO2 and
4c260379
RS
3251 the latter is tested via INSN2_USES_GPR. If INSN2 is null, PINFO2
3252 will have every flag set and INSN2_USES_GPR will always return true. */
71400594
RS
3253 pinfo1 = insn1->insn_mo->pinfo;
3254 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 3255
4c260379
RS
3256#define INSN2_USES_GPR(REG) \
3257 (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
71400594
RS
3258
3259 /* For most targets, write-after-read dependencies on the HI and LO
3260 registers must be separated by at least two instructions. */
3261 if (!hilo_interlocks)
252b5132 3262 {
71400594
RS
3263 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3264 return 2;
3265 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3266 return 2;
3267 }
3268
3269 /* If we're working around r7000 errata, there must be two instructions
3270 between an mfhi or mflo and any instruction that uses the result. */
3271 if (mips_7000_hilo_fix
df58fc94 3272 && !mips_opts.micromips
71400594 3273 && MF_HILO_INSN (pinfo1)
df58fc94 3274 && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
71400594
RS
3275 return 2;
3276
ff239038
CM
3277 /* If we're working around 24K errata, one instruction is required
3278 if an ERET or DERET is followed by a branch instruction. */
df58fc94 3279 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
3280 {
3281 if (insn1->insn_opcode == INSN_ERET
3282 || insn1->insn_opcode == INSN_DERET)
3283 {
3284 if (insn2 == NULL
3285 || insn2->insn_opcode == INSN_ERET
3286 || insn2->insn_opcode == INSN_DERET
11625dd8 3287 || delayed_branch_p (insn2))
ff239038
CM
3288 return 1;
3289 }
3290 }
3291
71400594
RS
3292 /* If working around VR4120 errata, check for combinations that need
3293 a single intervening instruction. */
df58fc94 3294 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
3295 {
3296 unsigned int class1, class2;
252b5132 3297
71400594
RS
3298 class1 = classify_vr4120_insn (insn1->insn_mo->name);
3299 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 3300 {
71400594
RS
3301 if (insn2 == NULL)
3302 return 1;
3303 class2 = classify_vr4120_insn (insn2->insn_mo->name);
3304 if (vr4120_conflicts[class1] & (1 << class2))
3305 return 1;
252b5132 3306 }
71400594
RS
3307 }
3308
df58fc94 3309 if (!HAVE_CODE_COMPRESSION)
71400594
RS
3310 {
3311 /* Check for GPR or coprocessor load delays. All such delays
3312 are on the RT register. */
3313 /* Itbl support may require additional care here. */
3314 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3315 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 3316 {
71400594 3317 know (pinfo1 & INSN_WRITE_GPR_T);
df58fc94 3318 if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
71400594
RS
3319 return 1;
3320 }
3321
3322 /* Check for generic coprocessor hazards.
3323
3324 This case is not handled very well. There is no special
3325 knowledge of CP0 handling, and the coprocessors other than
3326 the floating point unit are not distinguished at all. */
3327 /* Itbl support may require additional care here. FIXME!
3328 Need to modify this to include knowledge about
3329 user specified delays! */
3330 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3331 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3332 {
3333 /* Handle cases where INSN1 writes to a known general coprocessor
3334 register. There must be a one instruction delay before INSN2
3335 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
3336 mask = fpr_write_mask (insn1);
3337 if (mask != 0)
252b5132 3338 {
4c260379 3339 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 3340 return 1;
252b5132
RH
3341 }
3342 else
3343 {
71400594
RS
3344 /* Read-after-write dependencies on the control registers
3345 require a two-instruction gap. */
3346 if ((pinfo1 & INSN_WRITE_COND_CODE)
3347 && (pinfo2 & INSN_READ_COND_CODE))
3348 return 2;
3349
3350 /* We don't know exactly what INSN1 does. If INSN2 is
3351 also a coprocessor instruction, assume there must be
3352 a one instruction gap. */
3353 if (pinfo2 & INSN_COP)
3354 return 1;
252b5132
RH
3355 }
3356 }
6b76fefe 3357
71400594
RS
3358 /* Check for read-after-write dependencies on the coprocessor
3359 control registers in cases where INSN1 does not need a general
3360 coprocessor delay. This means that INSN1 is a floating point
3361 comparison instruction. */
3362 /* Itbl support may require additional care here. */
3363 else if (!cop_interlocks
3364 && (pinfo1 & INSN_WRITE_COND_CODE)
3365 && (pinfo2 & INSN_READ_COND_CODE))
3366 return 1;
3367 }
6b76fefe 3368
4c260379 3369#undef INSN2_USES_GPR
6b76fefe 3370
71400594
RS
3371 return 0;
3372}
6b76fefe 3373
7d8e00cf
RS
3374/* Return the number of nops that would be needed to work around the
3375 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
3376 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
3377 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
3378
3379static int
932d1a1b 3380nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
3381 const struct mips_cl_insn *insn)
3382{
4c260379
RS
3383 int i, j;
3384 unsigned int mask;
7d8e00cf
RS
3385
3386 /* Check if the instruction writes to HI or LO. MTHI and MTLO
3387 are not affected by the errata. */
3388 if (insn != 0
3389 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3390 || strcmp (insn->insn_mo->name, "mtlo") == 0
3391 || strcmp (insn->insn_mo->name, "mthi") == 0))
3392 return 0;
3393
3394 /* Search for the first MFLO or MFHI. */
3395 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 3396 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
3397 {
3398 /* Extract the destination register. */
4c260379 3399 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
3400
3401 /* No nops are needed if INSN reads that register. */
4c260379 3402 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
3403 return 0;
3404
3405 /* ...or if any of the intervening instructions do. */
3406 for (j = 0; j < i; j++)
4c260379 3407 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
3408 return 0;
3409
932d1a1b
RS
3410 if (i >= ignore)
3411 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
3412 }
3413 return 0;
3414}
3415
15be625d
CM
3416#define BASE_REG_EQ(INSN1, INSN2) \
3417 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3418 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3419
3420/* Return the minimum alignment for this store instruction. */
3421
3422static int
3423fix_24k_align_to (const struct mips_opcode *mo)
3424{
3425 if (strcmp (mo->name, "sh") == 0)
3426 return 2;
3427
3428 if (strcmp (mo->name, "swc1") == 0
3429 || strcmp (mo->name, "swc2") == 0
3430 || strcmp (mo->name, "sw") == 0
3431 || strcmp (mo->name, "sc") == 0
3432 || strcmp (mo->name, "s.s") == 0)
3433 return 4;
3434
3435 if (strcmp (mo->name, "sdc1") == 0
3436 || strcmp (mo->name, "sdc2") == 0
3437 || strcmp (mo->name, "s.d") == 0)
3438 return 8;
3439
3440 /* sb, swl, swr */
3441 return 1;
3442}
3443
3444struct fix_24k_store_info
3445 {
3446 /* Immediate offset, if any, for this store instruction. */
3447 short off;
3448 /* Alignment required by this store instruction. */
3449 int align_to;
3450 /* True for register offsets. */
3451 int register_offset;
3452 };
3453
3454/* Comparison function used by qsort. */
3455
3456static int
3457fix_24k_sort (const void *a, const void *b)
3458{
3459 const struct fix_24k_store_info *pos1 = a;
3460 const struct fix_24k_store_info *pos2 = b;
3461
3462 return (pos1->off - pos2->off);
3463}
3464
3465/* INSN is a store instruction. Try to record the store information
3466 in STINFO. Return false if the information isn't known. */
3467
3468static bfd_boolean
3469fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 3470 const struct mips_cl_insn *insn)
15be625d
CM
3471{
3472 /* The instruction must have a known offset. */
3473 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3474 return FALSE;
3475
3476 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3477 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3478 return TRUE;
3479}
3480
932d1a1b
RS
3481/* Return the number of nops that would be needed to work around the 24k
3482 "lost data on stores during refill" errata if instruction INSN
3483 immediately followed the 2 instructions described by HIST.
3484 Ignore hazards that are contained within the first IGNORE
3485 instructions of HIST.
3486
3487 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3488 for the data cache refills and store data. The following describes
3489 the scenario where the store data could be lost.
3490
3491 * A data cache miss, due to either a load or a store, causing fill
3492 data to be supplied by the memory subsystem
3493 * The first three doublewords of fill data are returned and written
3494 into the cache
3495 * A sequence of four stores occurs in consecutive cycles around the
3496 final doubleword of the fill:
3497 * Store A
3498 * Store B
3499 * Store C
3500 * Zero, One or more instructions
3501 * Store D
3502
3503 The four stores A-D must be to different doublewords of the line that
3504 is being filled. The fourth instruction in the sequence above permits
3505 the fill of the final doubleword to be transferred from the FSB into
3506 the cache. In the sequence above, the stores may be either integer
3507 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3508 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3509 different doublewords on the line. If the floating point unit is
3510 running in 1:2 mode, it is not possible to create the sequence above
3511 using only floating point store instructions.
15be625d
CM
3512
3513 In this case, the cache line being filled is incorrectly marked
3514 invalid, thereby losing the data from any store to the line that
3515 occurs between the original miss and the completion of the five
3516 cycle sequence shown above.
3517
932d1a1b 3518 The workarounds are:
15be625d 3519
932d1a1b
RS
3520 * Run the data cache in write-through mode.
3521 * Insert a non-store instruction between
3522 Store A and Store B or Store B and Store C. */
15be625d
CM
3523
3524static int
932d1a1b 3525nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
3526 const struct mips_cl_insn *insn)
3527{
3528 struct fix_24k_store_info pos[3];
3529 int align, i, base_offset;
3530
932d1a1b
RS
3531 if (ignore >= 2)
3532 return 0;
3533
ab9794cf
RS
3534 /* If the previous instruction wasn't a store, there's nothing to
3535 worry about. */
15be625d
CM
3536 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3537 return 0;
3538
ab9794cf
RS
3539 /* If the instructions after the previous one are unknown, we have
3540 to assume the worst. */
3541 if (!insn)
15be625d
CM
3542 return 1;
3543
ab9794cf
RS
3544 /* Check whether we are dealing with three consecutive stores. */
3545 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3546 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
3547 return 0;
3548
3549 /* If we don't know the relationship between the store addresses,
3550 assume the worst. */
ab9794cf 3551 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
3552 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3553 return 1;
3554
3555 if (!fix_24k_record_store_info (&pos[0], insn)
3556 || !fix_24k_record_store_info (&pos[1], &hist[0])
3557 || !fix_24k_record_store_info (&pos[2], &hist[1]))
3558 return 1;
3559
3560 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3561
3562 /* Pick a value of ALIGN and X such that all offsets are adjusted by
3563 X bytes and such that the base register + X is known to be aligned
3564 to align bytes. */
3565
3566 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3567 align = 8;
3568 else
3569 {
3570 align = pos[0].align_to;
3571 base_offset = pos[0].off;
3572 for (i = 1; i < 3; i++)
3573 if (align < pos[i].align_to)
3574 {
3575 align = pos[i].align_to;
3576 base_offset = pos[i].off;
3577 }
3578 for (i = 0; i < 3; i++)
3579 pos[i].off -= base_offset;
3580 }
3581
3582 pos[0].off &= ~align + 1;
3583 pos[1].off &= ~align + 1;
3584 pos[2].off &= ~align + 1;
3585
3586 /* If any two stores write to the same chunk, they also write to the
3587 same doubleword. The offsets are still sorted at this point. */
3588 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3589 return 0;
3590
3591 /* A range of at least 9 bytes is needed for the stores to be in
3592 non-overlapping doublewords. */
3593 if (pos[2].off - pos[0].off <= 8)
3594 return 0;
3595
3596 if (pos[2].off - pos[1].off >= 24
3597 || pos[1].off - pos[0].off >= 24
3598 || pos[2].off - pos[0].off >= 32)
3599 return 0;
3600
3601 return 1;
3602}
3603
71400594 3604/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 3605 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
3606 where HIST[0] is the most recent instruction. Ignore hazards
3607 between INSN and the first IGNORE instructions in HIST.
3608
3609 If INSN is null, return the worse-case number of nops for any
3610 instruction. */
bdaaa2e1 3611
71400594 3612static int
932d1a1b 3613nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
3614 const struct mips_cl_insn *insn)
3615{
3616 int i, nops, tmp_nops;
bdaaa2e1 3617
71400594 3618 nops = 0;
932d1a1b 3619 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 3620 {
91d6fa6a 3621 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
3622 if (tmp_nops > nops)
3623 nops = tmp_nops;
3624 }
7d8e00cf 3625
df58fc94 3626 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 3627 {
932d1a1b 3628 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
3629 if (tmp_nops > nops)
3630 nops = tmp_nops;
3631 }
3632
df58fc94 3633 if (mips_fix_24k && !mips_opts.micromips)
15be625d 3634 {
932d1a1b 3635 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
3636 if (tmp_nops > nops)
3637 nops = tmp_nops;
3638 }
3639
71400594
RS
3640 return nops;
3641}
252b5132 3642
71400594 3643/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 3644 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
3645 would be needed after the extended sequence, ignoring hazards
3646 in the first IGNORE instructions. */
252b5132 3647
71400594 3648static int
932d1a1b
RS
3649nops_for_sequence (int num_insns, int ignore,
3650 const struct mips_cl_insn *hist, ...)
71400594
RS
3651{
3652 va_list args;
3653 struct mips_cl_insn buffer[MAX_NOPS];
3654 struct mips_cl_insn *cursor;
3655 int nops;
3656
91d6fa6a 3657 va_start (args, hist);
71400594 3658 cursor = buffer + num_insns;
91d6fa6a 3659 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
3660 while (cursor > buffer)
3661 *--cursor = *va_arg (args, const struct mips_cl_insn *);
3662
932d1a1b 3663 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
3664 va_end (args);
3665 return nops;
3666}
252b5132 3667
71400594
RS
3668/* Like nops_for_insn, but if INSN is a branch, take into account the
3669 worst-case delay for the branch target. */
252b5132 3670
71400594 3671static int
932d1a1b 3672nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
3673 const struct mips_cl_insn *insn)
3674{
3675 int nops, tmp_nops;
60b63b72 3676
932d1a1b 3677 nops = nops_for_insn (ignore, hist, insn);
11625dd8 3678 if (delayed_branch_p (insn))
71400594 3679 {
932d1a1b 3680 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 3681 hist, insn, get_delay_slot_nop (insn));
71400594
RS
3682 if (tmp_nops > nops)
3683 nops = tmp_nops;
3684 }
11625dd8 3685 else if (compact_branch_p (insn))
71400594 3686 {
932d1a1b 3687 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
3688 if (tmp_nops > nops)
3689 nops = tmp_nops;
3690 }
3691 return nops;
3692}
3693
c67a084a
NC
3694/* Fix NOP issue: Replace nops by "or at,at,zero". */
3695
3696static void
3697fix_loongson2f_nop (struct mips_cl_insn * ip)
3698{
df58fc94 3699 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
3700 if (strcmp (ip->insn_mo->name, "nop") == 0)
3701 ip->insn_opcode = LOONGSON2F_NOP_INSN;
3702}
3703
3704/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
3705 jr target pc &= 'hffff_ffff_cfff_ffff. */
3706
3707static void
3708fix_loongson2f_jump (struct mips_cl_insn * ip)
3709{
df58fc94 3710 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
3711 if (strcmp (ip->insn_mo->name, "j") == 0
3712 || strcmp (ip->insn_mo->name, "jr") == 0
3713 || strcmp (ip->insn_mo->name, "jalr") == 0)
3714 {
3715 int sreg;
3716 expressionS ep;
3717
3718 if (! mips_opts.at)
3719 return;
3720
df58fc94 3721 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
3722 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
3723 return;
3724
3725 ep.X_op = O_constant;
3726 ep.X_add_number = 0xcfff0000;
3727 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
3728 ep.X_add_number = 0xffff;
3729 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
3730 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
3731 }
3732}
3733
3734static void
3735fix_loongson2f (struct mips_cl_insn * ip)
3736{
3737 if (mips_fix_loongson2f_nop)
3738 fix_loongson2f_nop (ip);
3739
3740 if (mips_fix_loongson2f_jump)
3741 fix_loongson2f_jump (ip);
3742}
3743
a4e06468
RS
3744/* IP is a branch that has a delay slot, and we need to fill it
3745 automatically. Return true if we can do that by swapping IP
e407c74b
NC
3746 with the previous instruction.
3747 ADDRESS_EXPR is an operand of the instruction to be used with
3748 RELOC_TYPE. */
a4e06468
RS
3749
3750static bfd_boolean
e407c74b
NC
3751can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
3752 bfd_reloc_code_real_type *reloc_type)
a4e06468 3753{
2b0c8b40 3754 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468
RS
3755 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
3756
3757 /* -O2 and above is required for this optimization. */
3758 if (mips_optimize < 2)
3759 return FALSE;
3760
3761 /* If we have seen .set volatile or .set nomove, don't optimize. */
3762 if (mips_opts.nomove)
3763 return FALSE;
3764
3765 /* We can't swap if the previous instruction's position is fixed. */
3766 if (history[0].fixed_p)
3767 return FALSE;
3768
3769 /* If the previous previous insn was in a .set noreorder, we can't
3770 swap. Actually, the MIPS assembler will swap in this situation.
3771 However, gcc configured -with-gnu-as will generate code like
3772
3773 .set noreorder
3774 lw $4,XXX
3775 .set reorder
3776 INSN
3777 bne $4,$0,foo
3778
3779 in which we can not swap the bne and INSN. If gcc is not configured
3780 -with-gnu-as, it does not output the .set pseudo-ops. */
3781 if (history[1].noreorder_p)
3782 return FALSE;
3783
87333bb7
MR
3784 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
3785 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
3786 if (mips_opts.mips16 && history[0].fixp[0])
3787 return FALSE;
3788
3789 /* If the branch is itself the target of a branch, we can not swap.
3790 We cheat on this; all we check for is whether there is a label on
3791 this instruction. If there are any branches to anything other than
3792 a label, users must use .set noreorder. */
3793 if (seg_info (now_seg)->label_list)
3794 return FALSE;
3795
3796 /* If the previous instruction is in a variant frag other than this
2309ddf2 3797 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
3798 MIPS16 code, which uses variant frags for different purposes. */
3799 if (!mips_opts.mips16
a4e06468
RS
3800 && history[0].frag
3801 && history[0].frag->fr_type == rs_machine_dependent)
3802 return FALSE;
3803
bcd530a7
RS
3804 /* We do not swap with instructions that cannot architecturally
3805 be placed in a branch delay slot, such as SYNC or ERET. We
3806 also refrain from swapping with a trap instruction, since it
3807 complicates trap handlers to have the trap instruction be in
3808 a delay slot. */
a4e06468 3809 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 3810 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
3811 return FALSE;
3812
3813 /* Check for conflicts between the branch and the instructions
3814 before the candidate delay slot. */
3815 if (nops_for_insn (0, history + 1, ip) > 0)
3816 return FALSE;
3817
3818 /* Check for conflicts between the swapped sequence and the
3819 target of the branch. */
3820 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
3821 return FALSE;
3822
3823 /* If the branch reads a register that the previous
3824 instruction sets, we can not swap. */
3825 gpr_read = gpr_read_mask (ip);
3826 prev_gpr_write = gpr_write_mask (&history[0]);
3827 if (gpr_read & prev_gpr_write)
3828 return FALSE;
3829
3830 /* If the branch writes a register that the previous
3831 instruction sets, we can not swap. */
3832 gpr_write = gpr_write_mask (ip);
3833 if (gpr_write & prev_gpr_write)
3834 return FALSE;
3835
3836 /* If the branch writes a register that the previous
3837 instruction reads, we can not swap. */
3838 prev_gpr_read = gpr_read_mask (&history[0]);
3839 if (gpr_write & prev_gpr_read)
3840 return FALSE;
3841
3842 /* If one instruction sets a condition code and the
3843 other one uses a condition code, we can not swap. */
3844 pinfo = ip->insn_mo->pinfo;
3845 if ((pinfo & INSN_READ_COND_CODE)
3846 && (prev_pinfo & INSN_WRITE_COND_CODE))
3847 return FALSE;
3848 if ((pinfo & INSN_WRITE_COND_CODE)
3849 && (prev_pinfo & INSN_READ_COND_CODE))
3850 return FALSE;
3851
3852 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 3853 prev_pinfo2 = history[0].insn_mo->pinfo2;
a4e06468
RS
3854 if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
3855 return FALSE;
2b0c8b40
MR
3856 if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
3857 return FALSE;
a4e06468 3858
df58fc94
RS
3859 /* If the previous instruction has an incorrect size for a fixed
3860 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
3861 pinfo2 = ip->insn_mo->pinfo2;
3862 if (mips_opts.micromips
3863 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
3864 && insn_length (history) != 2)
3865 return FALSE;
3866 if (mips_opts.micromips
3867 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
3868 && insn_length (history) != 4)
3869 return FALSE;
3870
e407c74b
NC
3871 /* On R5900 short loops need to be fixed by inserting a nop in
3872 the branch delay slots.
3873 A short loop can be terminated too early. */
3874 if (mips_opts.arch == CPU_R5900
3875 /* Check if instruction has a parameter, ignore "j $31". */
3876 && (address_expr != NULL)
3877 /* Parameter must be 16 bit. */
3878 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
3879 /* Branch to same segment. */
3880 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
3881 /* Branch to same code fragment. */
3882 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
3883 /* Can only calculate branch offset if value is known. */
3884 && symbol_constant_p(address_expr->X_add_symbol)
3885 /* Check if branch is really conditional. */
3886 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
3887 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
3888 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
3889 {
3890 int distance;
3891 /* Check if loop is shorter than 6 instructions including
3892 branch and delay slot. */
3893 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
3894 if (distance <= 20)
3895 {
3896 int i;
3897 int rv;
3898
3899 rv = FALSE;
3900 /* When the loop includes branches or jumps,
3901 it is not a short loop. */
3902 for (i = 0; i < (distance / 4); i++)
3903 {
3904 if ((history[i].cleared_p)
3905 || delayed_branch_p(&history[i]))
3906 {
3907 rv = TRUE;
3908 break;
3909 }
3910 }
3911 if (rv == FALSE)
3912 {
3913 /* Insert nop after branch to fix short loop. */
3914 return FALSE;
3915 }
3916 }
3917 }
3918
a4e06468
RS
3919 return TRUE;
3920}
3921
e407c74b
NC
3922/* Decide how we should add IP to the instruction stream.
3923 ADDRESS_EXPR is an operand of the instruction to be used with
3924 RELOC_TYPE. */
a4e06468
RS
3925
3926static enum append_method
e407c74b
NC
3927get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
3928 bfd_reloc_code_real_type *reloc_type)
a4e06468
RS
3929{
3930 unsigned long pinfo;
3931
3932 /* The relaxed version of a macro sequence must be inherently
3933 hazard-free. */
3934 if (mips_relax.sequence == 2)
3935 return APPEND_ADD;
3936
3937 /* We must not dabble with instructions in a ".set norerorder" block. */
3938 if (mips_opts.noreorder)
3939 return APPEND_ADD;
3940
3941 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 3942 if (delayed_branch_p (ip))
a4e06468 3943 {
e407c74b
NC
3944 if (!branch_likely_p (ip)
3945 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
3946 return APPEND_SWAP;
3947
11625dd8 3948 pinfo = ip->insn_mo->pinfo;
a4e06468
RS
3949 if (mips_opts.mips16
3950 && ISA_SUPPORTS_MIPS16E
a4e06468
RS
3951 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
3952 return APPEND_ADD_COMPACT;
3953
3954 return APPEND_ADD_WITH_NOP;
3955 }
3956
a4e06468
RS
3957 return APPEND_ADD;
3958}
3959
ceb94aa5
RS
3960/* IP is a MIPS16 instruction whose opcode we have just changed.
3961 Point IP->insn_mo to the new opcode's definition. */
3962
3963static void
3964find_altered_mips16_opcode (struct mips_cl_insn *ip)
3965{
3966 const struct mips_opcode *mo, *end;
3967
3968 end = &mips16_opcodes[bfd_mips16_num_opcodes];
3969 for (mo = ip->insn_mo; mo < end; mo++)
3970 if ((ip->insn_opcode & mo->mask) == mo->match)
3971 {
3972 ip->insn_mo = mo;
3973 return;
3974 }
3975 abort ();
3976}
3977
df58fc94
RS
3978/* For microMIPS macros, we need to generate a local number label
3979 as the target of branches. */
3980#define MICROMIPS_LABEL_CHAR '\037'
3981static unsigned long micromips_target_label;
3982static char micromips_target_name[32];
3983
3984static char *
3985micromips_label_name (void)
3986{
3987 char *p = micromips_target_name;
3988 char symbol_name_temporary[24];
3989 unsigned long l;
3990 int i;
3991
3992 if (*p)
3993 return p;
3994
3995 i = 0;
3996 l = micromips_target_label;
3997#ifdef LOCAL_LABEL_PREFIX
3998 *p++ = LOCAL_LABEL_PREFIX;
3999#endif
4000 *p++ = 'L';
4001 *p++ = MICROMIPS_LABEL_CHAR;
4002 do
4003 {
4004 symbol_name_temporary[i++] = l % 10 + '0';
4005 l /= 10;
4006 }
4007 while (l != 0);
4008 while (i > 0)
4009 *p++ = symbol_name_temporary[--i];
4010 *p = '\0';
4011
4012 return micromips_target_name;
4013}
4014
4015static void
4016micromips_label_expr (expressionS *label_expr)
4017{
4018 label_expr->X_op = O_symbol;
4019 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
4020 label_expr->X_add_number = 0;
4021}
4022
4023static void
4024micromips_label_inc (void)
4025{
4026 micromips_target_label++;
4027 *micromips_target_name = '\0';
4028}
4029
4030static void
4031micromips_add_label (void)
4032{
4033 symbolS *s;
4034
4035 s = colon (micromips_label_name ());
4036 micromips_label_inc ();
4037#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
4038 if (IS_ELF)
4039 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
db9b2be4
AM
4040#else
4041 (void) s;
df58fc94
RS
4042#endif
4043}
4044
4045/* If assembling microMIPS code, then return the microMIPS reloc
4046 corresponding to the requested one if any. Otherwise return
4047 the reloc unchanged. */
4048
4049static bfd_reloc_code_real_type
4050micromips_map_reloc (bfd_reloc_code_real_type reloc)
4051{
4052 static const bfd_reloc_code_real_type relocs[][2] =
4053 {
4054 /* Keep sorted incrementally by the left-hand key. */
4055 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
4056 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
4057 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
4058 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
4059 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
4060 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
4061 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
4062 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
4063 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
4064 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
4065 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
4066 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
4067 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
4068 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
4069 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
4070 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
4071 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
4072 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
4073 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
4074 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
4075 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
4076 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
4077 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
4078 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
4079 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
4080 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
4081 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
4082 };
4083 bfd_reloc_code_real_type r;
4084 size_t i;
4085
4086 if (!mips_opts.micromips)
4087 return reloc;
4088 for (i = 0; i < ARRAY_SIZE (relocs); i++)
4089 {
4090 r = relocs[i][0];
4091 if (r > reloc)
4092 return reloc;
4093 if (r == reloc)
4094 return relocs[i][1];
4095 }
4096 return reloc;
4097}
4098
b886a2ab
RS
4099/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
4100 Return true on success, storing the resolved value in RESULT. */
4101
4102static bfd_boolean
4103calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
4104 offsetT *result)
4105{
4106 switch (reloc)
4107 {
4108 case BFD_RELOC_MIPS_HIGHEST:
4109 case BFD_RELOC_MICROMIPS_HIGHEST:
4110 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
4111 return TRUE;
4112
4113 case BFD_RELOC_MIPS_HIGHER:
4114 case BFD_RELOC_MICROMIPS_HIGHER:
4115 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
4116 return TRUE;
4117
4118 case BFD_RELOC_HI16_S:
4119 case BFD_RELOC_MICROMIPS_HI16_S:
4120 case BFD_RELOC_MIPS16_HI16_S:
4121 *result = ((operand + 0x8000) >> 16) & 0xffff;
4122 return TRUE;
4123
4124 case BFD_RELOC_HI16:
4125 case BFD_RELOC_MICROMIPS_HI16:
4126 case BFD_RELOC_MIPS16_HI16:
4127 *result = (operand >> 16) & 0xffff;
4128 return TRUE;
4129
4130 case BFD_RELOC_LO16:
4131 case BFD_RELOC_MICROMIPS_LO16:
4132 case BFD_RELOC_MIPS16_LO16:
4133 *result = operand & 0xffff;
4134 return TRUE;
4135
4136 case BFD_RELOC_UNUSED:
4137 *result = operand;
4138 return TRUE;
4139
4140 default:
4141 return FALSE;
4142 }
4143}
4144
71400594
RS
4145/* Output an instruction. IP is the instruction information.
4146 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
4147 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
4148 a macro expansion. */
71400594
RS
4149
4150static void
4151append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 4152 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 4153{
14fe068b 4154 unsigned long prev_pinfo2, pinfo;
71400594 4155 bfd_boolean relaxed_branch = FALSE;
a4e06468 4156 enum append_method method;
2309ddf2 4157 bfd_boolean relax32;
2b0c8b40 4158 int branch_disp;
71400594 4159
2309ddf2 4160 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
4161 fix_loongson2f (ip);
4162
738f4d98 4163 file_ase_mips16 |= mips_opts.mips16;
df58fc94 4164 file_ase_micromips |= mips_opts.micromips;
738f4d98 4165
df58fc94 4166 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 4167 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
4168
4169 if (mips_opts.micromips
4170 && !expansionp
4171 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4172 && micromips_insn_length (ip->insn_mo) != 2)
4173 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4174 && micromips_insn_length (ip->insn_mo) != 4)))
4175 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4176 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 4177
15be625d
CM
4178 if (address_expr == NULL)
4179 ip->complete_p = 1;
b886a2ab
RS
4180 else if (reloc_type[0] <= BFD_RELOC_UNUSED
4181 && reloc_type[1] == BFD_RELOC_UNUSED
4182 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
4183 && address_expr->X_op == O_constant)
4184 {
15be625d
CM
4185 switch (*reloc_type)
4186 {
15be625d 4187 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
4188 {
4189 int shift;
4190
4191 shift = mips_opts.micromips ? 1 : 2;
4192 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4193 as_bad (_("jump to misaligned address (0x%lx)"),
4194 (unsigned long) address_expr->X_add_number);
4195 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4196 & 0x3ffffff);
335574df 4197 ip->complete_p = 1;
df58fc94 4198 }
15be625d
CM
4199 break;
4200
4201 case BFD_RELOC_MIPS16_JMP:
4202 if ((address_expr->X_add_number & 3) != 0)
4203 as_bad (_("jump to misaligned address (0x%lx)"),
4204 (unsigned long) address_expr->X_add_number);
4205 ip->insn_opcode |=
4206 (((address_expr->X_add_number & 0x7c0000) << 3)
4207 | ((address_expr->X_add_number & 0xf800000) >> 7)
4208 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 4209 ip->complete_p = 1;
15be625d
CM
4210 break;
4211
4212 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
4213 {
4214 int shift;
4215
4216 shift = mips_opts.micromips ? 1 : 2;
4217 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4218 as_bad (_("branch to misaligned address (0x%lx)"),
4219 (unsigned long) address_expr->X_add_number);
4220 if (!mips_relax_branch)
4221 {
4222 if ((address_expr->X_add_number + (1 << (shift + 15)))
4223 & ~((1 << (shift + 16)) - 1))
4224 as_bad (_("branch address range overflow (0x%lx)"),
4225 (unsigned long) address_expr->X_add_number);
4226 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4227 & 0xffff);
4228 }
df58fc94 4229 }
15be625d
CM
4230 break;
4231
4232 default:
b886a2ab
RS
4233 {
4234 offsetT value;
4235
4236 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
4237 &value))
4238 {
4239 ip->insn_opcode |= value & 0xffff;
4240 ip->complete_p = 1;
4241 }
4242 }
4243 break;
4244 }
15be625d
CM
4245 }
4246
71400594
RS
4247 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4248 {
4249 /* There are a lot of optimizations we could do that we don't.
4250 In particular, we do not, in general, reorder instructions.
4251 If you use gcc with optimization, it will reorder
4252 instructions and generally do much more optimization then we
4253 do here; repeating all that work in the assembler would only
4254 benefit hand written assembly code, and does not seem worth
4255 it. */
4256 int nops = (mips_optimize == 0
932d1a1b
RS
4257 ? nops_for_insn (0, history, NULL)
4258 : nops_for_insn_or_target (0, history, ip));
71400594 4259 if (nops > 0)
252b5132
RH
4260 {
4261 fragS *old_frag;
4262 unsigned long old_frag_offset;
4263 int i;
252b5132
RH
4264
4265 old_frag = frag_now;
4266 old_frag_offset = frag_now_fix ();
4267
4268 for (i = 0; i < nops; i++)
14fe068b
RS
4269 add_fixed_insn (NOP_INSN);
4270 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
4271
4272 if (listing)
4273 {
4274 listing_prev_line ();
4275 /* We may be at the start of a variant frag. In case we
4276 are, make sure there is enough space for the frag
4277 after the frags created by listing_prev_line. The
4278 argument to frag_grow here must be at least as large
4279 as the argument to all other calls to frag_grow in
4280 this file. We don't have to worry about being in the
4281 middle of a variant frag, because the variants insert
4282 all needed nop instructions themselves. */
4283 frag_grow (40);
4284 }
4285
462427c4 4286 mips_move_text_labels ();
252b5132
RH
4287
4288#ifndef NO_ECOFF_DEBUGGING
4289 if (ECOFF_DEBUGGING)
4290 ecoff_fix_loc (old_frag, old_frag_offset);
4291#endif
4292 }
71400594
RS
4293 }
4294 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4295 {
932d1a1b
RS
4296 int nops;
4297
4298 /* Work out how many nops in prev_nop_frag are needed by IP,
4299 ignoring hazards generated by the first prev_nop_frag_since
4300 instructions. */
4301 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 4302 gas_assert (nops <= prev_nop_frag_holds);
252b5132 4303
71400594
RS
4304 /* Enforce NOPS as a minimum. */
4305 if (nops > prev_nop_frag_required)
4306 prev_nop_frag_required = nops;
252b5132 4307
71400594
RS
4308 if (prev_nop_frag_holds == prev_nop_frag_required)
4309 {
4310 /* Settle for the current number of nops. Update the history
4311 accordingly (for the benefit of any future .set reorder code). */
4312 prev_nop_frag = NULL;
4313 insert_into_history (prev_nop_frag_since,
4314 prev_nop_frag_holds, NOP_INSN);
4315 }
4316 else
4317 {
4318 /* Allow this instruction to replace one of the nops that was
4319 tentatively added to prev_nop_frag. */
df58fc94 4320 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
4321 prev_nop_frag_holds--;
4322 prev_nop_frag_since++;
252b5132
RH
4323 }
4324 }
4325
e407c74b 4326 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 4327 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 4328
58e2ea4d
MR
4329#ifdef OBJ_ELF
4330 /* The value passed to dwarf2_emit_insn is the distance between
4331 the beginning of the current instruction and the address that
e3a82c8e
MR
4332 should be recorded in the debug tables. This is normally the
4333 current address.
4334
df58fc94
RS
4335 For MIPS16/microMIPS debug info we want to use ISA-encoded
4336 addresses, so we use -1 for an address higher by one than the
4337 current one.
e3a82c8e
MR
4338
4339 If the instruction produced is a branch that we will swap with
4340 the preceding instruction, then we add the displacement by which
4341 the branch will be moved backwards. This is more appropriate
2309ddf2
MR
4342 and for MIPS16/microMIPS code also prevents a debugger from
4343 placing a breakpoint in the middle of the branch (and corrupting
4344 code if software breakpoints are used). */
2b0c8b40 4345 dwarf2_emit_insn ((HAVE_CODE_COMPRESSION ? -1 : 0) + branch_disp);
58e2ea4d
MR
4346#endif
4347
df58fc94
RS
4348 relax32 = (mips_relax_branch
4349 /* Don't try branch relaxation within .set nomacro, or within
4350 .set noat if we use $at for PIC computations. If it turns
4351 out that the branch was out-of-range, we'll get an error. */
4352 && !mips_opts.warn_about_macros
4353 && (mips_opts.at || mips_pic == NO_PIC)
4354 /* Don't relax BPOSGE32/64 as they have no complementing
4355 branches. */
40209cad 4356 && !(ip->insn_mo->membership & (INSN_DSP64 | INSN_DSP)));
df58fc94
RS
4357
4358 if (!HAVE_CODE_COMPRESSION
4359 && address_expr
4360 && relax32
0b25d3e6 4361 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 4362 && delayed_branch_p (ip))
4a6a3df4 4363 {
895921c9 4364 relaxed_branch = TRUE;
1e915849
RS
4365 add_relaxed_insn (ip, (relaxed_branch_length
4366 (NULL, NULL,
11625dd8
RS
4367 uncond_branch_p (ip) ? -1
4368 : branch_likely_p (ip) ? 1
1e915849
RS
4369 : 0)), 4,
4370 RELAX_BRANCH_ENCODE
66b3e8da 4371 (AT,
11625dd8
RS
4372 uncond_branch_p (ip),
4373 branch_likely_p (ip),
1e915849
RS
4374 pinfo & INSN_WRITE_GPR_31,
4375 0),
4376 address_expr->X_add_symbol,
4377 address_expr->X_add_number);
4a6a3df4
AO
4378 *reloc_type = BFD_RELOC_UNUSED;
4379 }
df58fc94
RS
4380 else if (mips_opts.micromips
4381 && address_expr
4382 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4383 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
4384 && (delayed_branch_p (ip) || compact_branch_p (ip))
4385 /* Don't try branch relaxation when users specify
4386 16-bit/32-bit instructions. */
4387 && !forced_insn_length)
df58fc94
RS
4388 {
4389 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4390 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
4391 int uncond = uncond_branch_p (ip) ? -1 : 0;
4392 int compact = compact_branch_p (ip);
df58fc94
RS
4393 int al = pinfo & INSN_WRITE_GPR_31;
4394 int length32;
4395
4396 gas_assert (address_expr != NULL);
4397 gas_assert (!mips_relax.sequence);
4398
2b0c8b40 4399 relaxed_branch = TRUE;
df58fc94
RS
4400 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4401 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
4402 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4403 relax32, 0, 0),
df58fc94
RS
4404 address_expr->X_add_symbol,
4405 address_expr->X_add_number);
4406 *reloc_type = BFD_RELOC_UNUSED;
4407 }
4408 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
4409 {
4410 /* We need to set up a variant frag. */
df58fc94 4411 gas_assert (address_expr != NULL);
1e915849
RS
4412 add_relaxed_insn (ip, 4, 0,
4413 RELAX_MIPS16_ENCODE
4414 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 4415 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 4416 delayed_branch_p (&history[0]),
1e915849
RS
4417 history[0].mips16_absolute_jump_p),
4418 make_expr_symbol (address_expr), 0);
252b5132 4419 }
5c04167a 4420 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 4421 {
11625dd8 4422 if (!delayed_branch_p (ip))
b8ee1a6e
DU
4423 /* Make sure there is enough room to swap this instruction with
4424 a following jump instruction. */
4425 frag_grow (6);
1e915849 4426 add_fixed_insn (ip);
252b5132
RH
4427 }
4428 else
4429 {
4430 if (mips_opts.mips16
4431 && mips_opts.noreorder
11625dd8 4432 && delayed_branch_p (&history[0]))
252b5132
RH
4433 as_warn (_("extended instruction in delay slot"));
4434
4d7206a2
RS
4435 if (mips_relax.sequence)
4436 {
4437 /* If we've reached the end of this frag, turn it into a variant
4438 frag and record the information for the instructions we've
4439 written so far. */
4440 if (frag_room () < 4)
4441 relax_close_frag ();
df58fc94 4442 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
4443 }
4444
584892a6 4445 if (mips_relax.sequence != 2)
df58fc94
RS
4446 {
4447 if (mips_macro_warning.first_insn_sizes[0] == 0)
4448 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4449 mips_macro_warning.sizes[0] += insn_length (ip);
4450 mips_macro_warning.insns[0]++;
4451 }
584892a6 4452 if (mips_relax.sequence != 1)
df58fc94
RS
4453 {
4454 if (mips_macro_warning.first_insn_sizes[1] == 0)
4455 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4456 mips_macro_warning.sizes[1] += insn_length (ip);
4457 mips_macro_warning.insns[1]++;
4458 }
584892a6 4459
1e915849
RS
4460 if (mips_opts.mips16)
4461 {
4462 ip->fixed_p = 1;
4463 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4464 }
4465 add_fixed_insn (ip);
252b5132
RH
4466 }
4467
9fe77896 4468 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 4469 {
df58fc94 4470 bfd_reloc_code_real_type final_type[3];
2309ddf2 4471 reloc_howto_type *howto0;
9fe77896
RS
4472 reloc_howto_type *howto;
4473 int i;
34ce925e 4474
df58fc94
RS
4475 /* Perform any necessary conversion to microMIPS relocations
4476 and find out how many relocations there actually are. */
4477 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4478 final_type[i] = micromips_map_reloc (reloc_type[i]);
4479
9fe77896
RS
4480 /* In a compound relocation, it is the final (outermost)
4481 operator that determines the relocated field. */
2309ddf2
MR
4482 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
4483
9fe77896
RS
4484 if (howto == NULL)
4485 {
4486 /* To reproduce this failure try assembling gas/testsuites/
4487 gas/mips/mips16-intermix.s with a mips-ecoff targeted
4488 assembler. */
df58fc94
RS
4489 as_bad (_("Unsupported MIPS relocation number %d"),
4490 final_type[i - 1]);
9fe77896
RS
4491 howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
4492 }
2309ddf2
MR
4493
4494 if (i > 1)
4495 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
4496 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4497 bfd_get_reloc_size (howto),
4498 address_expr,
2309ddf2
MR
4499 howto0 && howto0->pc_relative,
4500 final_type[0]);
9fe77896
RS
4501
4502 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 4503 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
4504 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4505
4506 /* These relocations can have an addend that won't fit in
4507 4 octets for 64bit assembly. */
4508 if (HAVE_64BIT_GPRS
4509 && ! howto->partial_inplace
4510 && (reloc_type[0] == BFD_RELOC_16
4511 || reloc_type[0] == BFD_RELOC_32
4512 || reloc_type[0] == BFD_RELOC_MIPS_JMP
4513 || reloc_type[0] == BFD_RELOC_GPREL16
4514 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4515 || reloc_type[0] == BFD_RELOC_GPREL32
4516 || reloc_type[0] == BFD_RELOC_64
4517 || reloc_type[0] == BFD_RELOC_CTOR
4518 || reloc_type[0] == BFD_RELOC_MIPS_SUB
4519 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4520 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4521 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4522 || reloc_type[0] == BFD_RELOC_MIPS_REL16
4523 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4524 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4525 || hi16_reloc_p (reloc_type[0])
4526 || lo16_reloc_p (reloc_type[0])))
4527 ip->fixp[0]->fx_no_overflow = 1;
4528
ddaf2c41
MR
4529 /* These relocations can have an addend that won't fit in 2 octets. */
4530 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
4531 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
4532 ip->fixp[0]->fx_no_overflow = 1;
4533
9fe77896
RS
4534 if (mips_relax.sequence)
4535 {
4536 if (mips_relax.first_fixup == 0)
4537 mips_relax.first_fixup = ip->fixp[0];
4538 }
4539 else if (reloc_needs_lo_p (*reloc_type))
4540 {
4541 struct mips_hi_fixup *hi_fixup;
4542
4543 /* Reuse the last entry if it already has a matching %lo. */
4544 hi_fixup = mips_hi_fixup_list;
4545 if (hi_fixup == 0
4546 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 4547 {
9fe77896
RS
4548 hi_fixup = ((struct mips_hi_fixup *)
4549 xmalloc (sizeof (struct mips_hi_fixup)));
4550 hi_fixup->next = mips_hi_fixup_list;
4551 mips_hi_fixup_list = hi_fixup;
4d7206a2 4552 }
9fe77896
RS
4553 hi_fixup->fixp = ip->fixp[0];
4554 hi_fixup->seg = now_seg;
4555 }
252b5132 4556
9fe77896
RS
4557 /* Add fixups for the second and third relocations, if given.
4558 Note that the ABI allows the second relocation to be
4559 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
4560 moment we only use RSS_UNDEF, but we could add support
4561 for the others if it ever becomes necessary. */
4562 for (i = 1; i < 3; i++)
4563 if (reloc_type[i] != BFD_RELOC_UNUSED)
4564 {
4565 ip->fixp[i] = fix_new (ip->frag, ip->where,
4566 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 4567 FALSE, final_type[i]);
f6688943 4568
9fe77896
RS
4569 /* Use fx_tcbit to mark compound relocs. */
4570 ip->fixp[0]->fx_tcbit = 1;
4571 ip->fixp[i]->fx_tcbit = 1;
4572 }
252b5132 4573 }
1e915849 4574 install_insn (ip);
252b5132
RH
4575
4576 /* Update the register mask information. */
4c260379
RS
4577 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4578 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 4579
a4e06468 4580 switch (method)
252b5132 4581 {
a4e06468
RS
4582 case APPEND_ADD:
4583 insert_into_history (0, 1, ip);
4584 break;
4585
4586 case APPEND_ADD_WITH_NOP:
14fe068b
RS
4587 {
4588 struct mips_cl_insn *nop;
4589
4590 insert_into_history (0, 1, ip);
4591 nop = get_delay_slot_nop (ip);
4592 add_fixed_insn (nop);
4593 insert_into_history (0, 1, nop);
4594 if (mips_relax.sequence)
4595 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4596 }
a4e06468
RS
4597 break;
4598
4599 case APPEND_ADD_COMPACT:
4600 /* Convert MIPS16 jr/jalr into a "compact" jump. */
4601 gas_assert (mips_opts.mips16);
4602 ip->insn_opcode |= 0x0080;
4603 find_altered_mips16_opcode (ip);
4604 install_insn (ip);
4605 insert_into_history (0, 1, ip);
4606 break;
4607
4608 case APPEND_SWAP:
4609 {
4610 struct mips_cl_insn delay = history[0];
4611 if (mips_opts.mips16)
4612 {
4613 know (delay.frag == ip->frag);
4614 move_insn (ip, delay.frag, delay.where);
4615 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4616 }
464ab0e5 4617 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
4618 {
4619 /* Add the delay slot instruction to the end of the
4620 current frag and shrink the fixed part of the
4621 original frag. If the branch occupies the tail of
4622 the latter, move it backwards to cover the gap. */
2b0c8b40 4623 delay.frag->fr_fix -= branch_disp;
a4e06468 4624 if (delay.frag == ip->frag)
2b0c8b40 4625 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
4626 add_fixed_insn (&delay);
4627 }
4628 else
4629 {
2b0c8b40
MR
4630 move_insn (&delay, ip->frag,
4631 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
4632 move_insn (ip, history[0].frag, history[0].where);
4633 }
4634 history[0] = *ip;
4635 delay.fixed_p = 1;
4636 insert_into_history (0, 1, &delay);
4637 }
4638 break;
252b5132
RH
4639 }
4640
13408f1e 4641 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
4642 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4643 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
4644 {
4645 unsigned int i;
4646
13408f1e
RS
4647 mips_no_prev_insn ();
4648
e407c74b
NC
4649 for (i = 0; i < ARRAY_SIZE (history); i++)
4650 {
4651 history[i].cleared_p = 1;
4652 }
4653 }
4654
df58fc94
RS
4655 /* We need to emit a label at the end of branch-likely macros. */
4656 if (emit_branch_likely_macro)
4657 {
4658 emit_branch_likely_macro = FALSE;
4659 micromips_add_label ();
4660 }
4661
252b5132
RH
4662 /* We just output an insn, so the next one doesn't have a label. */
4663 mips_clear_insn_labels ();
252b5132
RH
4664}
4665
e407c74b
NC
4666/* Forget that there was any previous instruction or label.
4667 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
4668
4669static void
7d10b47d 4670mips_no_prev_insn (void)
252b5132 4671{
7d10b47d
RS
4672 prev_nop_frag = NULL;
4673 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
4674 mips_clear_insn_labels ();
4675}
4676
7d10b47d
RS
4677/* This function must be called before we emit something other than
4678 instructions. It is like mips_no_prev_insn except that it inserts
4679 any NOPS that might be needed by previous instructions. */
252b5132 4680
7d10b47d
RS
4681void
4682mips_emit_delays (void)
252b5132
RH
4683{
4684 if (! mips_opts.noreorder)
4685 {
932d1a1b 4686 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
4687 if (nops > 0)
4688 {
7d10b47d
RS
4689 while (nops-- > 0)
4690 add_fixed_insn (NOP_INSN);
462427c4 4691 mips_move_text_labels ();
7d10b47d
RS
4692 }
4693 }
4694 mips_no_prev_insn ();
4695}
4696
4697/* Start a (possibly nested) noreorder block. */
4698
4699static void
4700start_noreorder (void)
4701{
4702 if (mips_opts.noreorder == 0)
4703 {
4704 unsigned int i;
4705 int nops;
4706
4707 /* None of the instructions before the .set noreorder can be moved. */
4708 for (i = 0; i < ARRAY_SIZE (history); i++)
4709 history[i].fixed_p = 1;
4710
4711 /* Insert any nops that might be needed between the .set noreorder
4712 block and the previous instructions. We will later remove any
4713 nops that turn out not to be needed. */
932d1a1b 4714 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
4715 if (nops > 0)
4716 {
4717 if (mips_optimize != 0)
252b5132
RH
4718 {
4719 /* Record the frag which holds the nop instructions, so
4720 that we can remove them if we don't need them. */
df58fc94 4721 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
4722 prev_nop_frag = frag_now;
4723 prev_nop_frag_holds = nops;
4724 prev_nop_frag_required = 0;
4725 prev_nop_frag_since = 0;
4726 }
4727
4728 for (; nops > 0; --nops)
1e915849 4729 add_fixed_insn (NOP_INSN);
252b5132 4730
7d10b47d
RS
4731 /* Move on to a new frag, so that it is safe to simply
4732 decrease the size of prev_nop_frag. */
4733 frag_wane (frag_now);
4734 frag_new (0);
462427c4 4735 mips_move_text_labels ();
252b5132 4736 }
df58fc94 4737 mips_mark_labels ();
7d10b47d 4738 mips_clear_insn_labels ();
252b5132 4739 }
7d10b47d
RS
4740 mips_opts.noreorder++;
4741 mips_any_noreorder = 1;
4742}
252b5132 4743
7d10b47d 4744/* End a nested noreorder block. */
252b5132 4745
7d10b47d
RS
4746static void
4747end_noreorder (void)
4748{
4749 mips_opts.noreorder--;
4750 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
4751 {
4752 /* Commit to inserting prev_nop_frag_required nops and go back to
4753 handling nop insertion the .set reorder way. */
4754 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 4755 * NOP_INSN_SIZE);
7d10b47d
RS
4756 insert_into_history (prev_nop_frag_since,
4757 prev_nop_frag_required, NOP_INSN);
4758 prev_nop_frag = NULL;
4759 }
252b5132
RH
4760}
4761
584892a6
RS
4762/* Set up global variables for the start of a new macro. */
4763
4764static void
4765macro_start (void)
4766{
4767 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
4768 memset (&mips_macro_warning.first_insn_sizes, 0,
4769 sizeof (mips_macro_warning.first_insn_sizes));
4770 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 4771 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 4772 && delayed_branch_p (&history[0]));
df58fc94
RS
4773 switch (history[0].insn_mo->pinfo2
4774 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
4775 {
4776 case INSN2_BRANCH_DELAY_32BIT:
4777 mips_macro_warning.delay_slot_length = 4;
4778 break;
4779 case INSN2_BRANCH_DELAY_16BIT:
4780 mips_macro_warning.delay_slot_length = 2;
4781 break;
4782 default:
4783 mips_macro_warning.delay_slot_length = 0;
4784 break;
4785 }
4786 mips_macro_warning.first_frag = NULL;
584892a6
RS
4787}
4788
df58fc94
RS
4789/* Given that a macro is longer than one instruction or of the wrong size,
4790 return the appropriate warning for it. Return null if no warning is
4791 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
4792 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
4793 and RELAX_NOMACRO. */
584892a6
RS
4794
4795static const char *
4796macro_warning (relax_substateT subtype)
4797{
4798 if (subtype & RELAX_DELAY_SLOT)
4799 return _("Macro instruction expanded into multiple instructions"
4800 " in a branch delay slot");
4801 else if (subtype & RELAX_NOMACRO)
4802 return _("Macro instruction expanded into multiple instructions");
df58fc94
RS
4803 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
4804 | RELAX_DELAY_SLOT_SIZE_SECOND))
4805 return ((subtype & RELAX_DELAY_SLOT_16BIT)
4806 ? _("Macro instruction expanded into a wrong size instruction"
4807 " in a 16-bit branch delay slot")
4808 : _("Macro instruction expanded into a wrong size instruction"
4809 " in a 32-bit branch delay slot"));
584892a6
RS
4810 else
4811 return 0;
4812}
4813
4814/* Finish up a macro. Emit warnings as appropriate. */
4815
4816static void
4817macro_end (void)
4818{
df58fc94
RS
4819 /* Relaxation warning flags. */
4820 relax_substateT subtype = 0;
4821
4822 /* Check delay slot size requirements. */
4823 if (mips_macro_warning.delay_slot_length == 2)
4824 subtype |= RELAX_DELAY_SLOT_16BIT;
4825 if (mips_macro_warning.delay_slot_length != 0)
584892a6 4826 {
df58fc94
RS
4827 if (mips_macro_warning.delay_slot_length
4828 != mips_macro_warning.first_insn_sizes[0])
4829 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
4830 if (mips_macro_warning.delay_slot_length
4831 != mips_macro_warning.first_insn_sizes[1])
4832 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
4833 }
584892a6 4834
df58fc94
RS
4835 /* Check instruction count requirements. */
4836 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
4837 {
4838 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
4839 subtype |= RELAX_SECOND_LONGER;
4840 if (mips_opts.warn_about_macros)
4841 subtype |= RELAX_NOMACRO;
4842 if (mips_macro_warning.delay_slot_p)
4843 subtype |= RELAX_DELAY_SLOT;
df58fc94 4844 }
584892a6 4845
df58fc94
RS
4846 /* If both alternatives fail to fill a delay slot correctly,
4847 emit the warning now. */
4848 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
4849 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
4850 {
4851 relax_substateT s;
4852 const char *msg;
4853
4854 s = subtype & (RELAX_DELAY_SLOT_16BIT
4855 | RELAX_DELAY_SLOT_SIZE_FIRST
4856 | RELAX_DELAY_SLOT_SIZE_SECOND);
4857 msg = macro_warning (s);
4858 if (msg != NULL)
4859 as_warn ("%s", msg);
4860 subtype &= ~s;
4861 }
4862
4863 /* If both implementations are longer than 1 instruction, then emit the
4864 warning now. */
4865 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
4866 {
4867 relax_substateT s;
4868 const char *msg;
4869
4870 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
4871 msg = macro_warning (s);
4872 if (msg != NULL)
4873 as_warn ("%s", msg);
4874 subtype &= ~s;
584892a6 4875 }
df58fc94
RS
4876
4877 /* If any flags still set, then one implementation might need a warning
4878 and the other either will need one of a different kind or none at all.
4879 Pass any remaining flags over to relaxation. */
4880 if (mips_macro_warning.first_frag != NULL)
4881 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
4882}
4883
df58fc94
RS
4884/* Instruction operand formats used in macros that vary between
4885 standard MIPS and microMIPS code. */
4886
4887static const char * const brk_fmt[2] = { "c", "mF" };
4888static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
4889static const char * const jalr_fmt[2] = { "d,s", "t,s" };
4890static const char * const lui_fmt[2] = { "t,u", "s,u" };
4891static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
4892static const char * const mfhl_fmt[2] = { "d", "mj" };
4893static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
4894static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
4895
4896#define BRK_FMT (brk_fmt[mips_opts.micromips])
4897#define COP12_FMT (cop12_fmt[mips_opts.micromips])
4898#define JALR_FMT (jalr_fmt[mips_opts.micromips])
4899#define LUI_FMT (lui_fmt[mips_opts.micromips])
4900#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
4901#define MFHL_FMT (mfhl_fmt[mips_opts.micromips])
4902#define SHFT_FMT (shft_fmt[mips_opts.micromips])
4903#define TRAP_FMT (trap_fmt[mips_opts.micromips])
4904
6e1304d8
RS
4905/* Read a macro's relocation codes from *ARGS and store them in *R.
4906 The first argument in *ARGS will be either the code for a single
4907 relocation or -1 followed by the three codes that make up a
4908 composite relocation. */
4909
4910static void
4911macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
4912{
4913 int i, next;
4914
4915 next = va_arg (*args, int);
4916 if (next >= 0)
4917 r[0] = (bfd_reloc_code_real_type) next;
4918 else
4919 for (i = 0; i < 3; i++)
4920 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
4921}
4922
252b5132
RH
4923/* Build an instruction created by a macro expansion. This is passed
4924 a pointer to the count of instructions created so far, an
4925 expression, the name of the instruction to build, an operand format
4926 string, and corresponding arguments. */
4927
252b5132 4928static void
67c0d1eb 4929macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 4930{
df58fc94 4931 const struct mips_opcode *mo = NULL;
f6688943 4932 bfd_reloc_code_real_type r[3];
df58fc94
RS
4933 const struct mips_opcode *amo;
4934 struct hash_control *hash;
4935 struct mips_cl_insn insn;
252b5132 4936 va_list args;
252b5132 4937
252b5132 4938 va_start (args, fmt);
252b5132 4939
252b5132
RH
4940 if (mips_opts.mips16)
4941 {
03ea81db 4942 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
4943 va_end (args);
4944 return;
4945 }
4946
f6688943
TS
4947 r[0] = BFD_RELOC_UNUSED;
4948 r[1] = BFD_RELOC_UNUSED;
4949 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
4950 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
4951 amo = (struct mips_opcode *) hash_find (hash, name);
4952 gas_assert (amo);
4953 gas_assert (strcmp (name, amo->name) == 0);
1e915849 4954
df58fc94 4955 do
8b082fb1
TS
4956 {
4957 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
4958 macros will never generate MDMX, MIPS-3D, or MT instructions.
4959 We try to match an instruction that fulfils the branch delay
4960 slot instruction length requirement (if any) of the previous
4961 instruction. While doing this we record the first instruction
4962 seen that matches all the other conditions and use it anyway
4963 if the requirement cannot be met; we will issue an appropriate
4964 warning later on. */
4965 if (strcmp (fmt, amo->args) == 0
4966 && amo->pinfo != INSN_MACRO
4967 && is_opcode_valid (amo)
4968 && is_size_valid (amo))
4969 {
4970 if (is_delay_slot_valid (amo))
4971 {
4972 mo = amo;
4973 break;
4974 }
4975 else if (!mo)
4976 mo = amo;
4977 }
8b082fb1 4978
df58fc94
RS
4979 ++amo;
4980 gas_assert (amo->name);
252b5132 4981 }
df58fc94 4982 while (strcmp (name, amo->name) == 0);
252b5132 4983
df58fc94 4984 gas_assert (mo);
1e915849 4985 create_insn (&insn, mo);
252b5132
RH
4986 for (;;)
4987 {
4988 switch (*fmt++)
4989 {
4990 case '\0':
4991 break;
4992
4993 case ',':
4994 case '(':
4995 case ')':
4996 continue;
4997
5f74bc13
CD
4998 case '+':
4999 switch (*fmt++)
5000 {
5001 case 'A':
5002 case 'E':
df58fc94
RS
5003 INSERT_OPERAND (mips_opts.micromips,
5004 EXTLSB, insn, va_arg (args, int));
5f74bc13
CD
5005 continue;
5006
5007 case 'B':
5008 case 'F':
5009 /* Note that in the macro case, these arguments are already
5010 in MSB form. (When handling the instruction in the
5011 non-macro case, these arguments are sizes from which
5012 MSB values must be calculated.) */
df58fc94
RS
5013 INSERT_OPERAND (mips_opts.micromips,
5014 INSMSB, insn, va_arg (args, int));
5f74bc13
CD
5015 continue;
5016
5017 case 'C':
5018 case 'G':
5019 case 'H':
5020 /* Note that in the macro case, these arguments are already
5021 in MSBD form. (When handling the instruction in the
5022 non-macro case, these arguments are sizes from which
5023 MSBD values must be calculated.) */
df58fc94
RS
5024 INSERT_OPERAND (mips_opts.micromips,
5025 EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
5026 continue;
5027
dd3cbb7e 5028 case 'Q':
df58fc94
RS
5029 gas_assert (!mips_opts.micromips);
5030 INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
dd3cbb7e
NC
5031 continue;
5032
5f74bc13 5033 default:
b37df7c4 5034 abort ();
5f74bc13
CD
5035 }
5036 continue;
5037
8b082fb1 5038 case '2':
03f66e8a 5039 INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
8b082fb1
TS
5040 continue;
5041
df58fc94
RS
5042 case 'n':
5043 gas_assert (mips_opts.micromips);
252b5132
RH
5044 case 't':
5045 case 'w':
5046 case 'E':
df58fc94 5047 INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
252b5132
RH
5048 continue;
5049
5050 case 'c':
df58fc94
RS
5051 gas_assert (!mips_opts.micromips);
5052 INSERT_OPERAND (0, CODE, insn, va_arg (args, int));
38487616
TS
5053 continue;
5054
252b5132 5055 case 'W':
df58fc94
RS
5056 gas_assert (!mips_opts.micromips);
5057 case 'T':
5058 INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
252b5132
RH
5059 continue;
5060
252b5132 5061 case 'G':
df58fc94
RS
5062 if (mips_opts.micromips)
5063 INSERT_OPERAND (1, RS, insn, va_arg (args, int));
5064 else
5065 INSERT_OPERAND (0, RD, insn, va_arg (args, int));
5066 continue;
5067
af7ee8bf 5068 case 'K':
df58fc94
RS
5069 gas_assert (!mips_opts.micromips);
5070 case 'd':
5071 INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
252b5132
RH
5072 continue;
5073
4372b673 5074 case 'U':
df58fc94 5075 gas_assert (!mips_opts.micromips);
4372b673
NC
5076 {
5077 int tmp = va_arg (args, int);
5078
df58fc94
RS
5079 INSERT_OPERAND (0, RT, insn, tmp);
5080 INSERT_OPERAND (0, RD, insn, tmp);
4372b673 5081 }
df58fc94 5082 continue;
4372b673 5083
252b5132
RH
5084 case 'V':
5085 case 'S':
df58fc94
RS
5086 gas_assert (!mips_opts.micromips);
5087 INSERT_OPERAND (0, FS, insn, va_arg (args, int));
252b5132
RH
5088 continue;
5089
5090 case 'z':
5091 continue;
5092
5093 case '<':
df58fc94
RS
5094 INSERT_OPERAND (mips_opts.micromips,
5095 SHAMT, insn, va_arg (args, int));
252b5132
RH
5096 continue;
5097
5098 case 'D':
df58fc94
RS
5099 gas_assert (!mips_opts.micromips);
5100 INSERT_OPERAND (0, FD, insn, va_arg (args, int));
252b5132
RH
5101 continue;
5102
5103 case 'B':
df58fc94
RS
5104 gas_assert (!mips_opts.micromips);
5105 INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
252b5132
RH
5106 continue;
5107
4372b673 5108 case 'J':
df58fc94
RS
5109 gas_assert (!mips_opts.micromips);
5110 INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
4372b673
NC
5111 continue;
5112
252b5132 5113 case 'q':
df58fc94
RS
5114 gas_assert (!mips_opts.micromips);
5115 INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
252b5132
RH
5116 continue;
5117
5118 case 'b':
5119 case 's':
5120 case 'r':
5121 case 'v':
df58fc94 5122 INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
252b5132
RH
5123 continue;
5124
5125 case 'i':
5126 case 'j':
6e1304d8 5127 macro_read_relocs (&args, r);
9c2799c2 5128 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
5129 || *r == BFD_RELOC_MIPS_HIGHER
5130 || *r == BFD_RELOC_HI16_S
5131 || *r == BFD_RELOC_LO16
5132 || *r == BFD_RELOC_MIPS_GOT_OFST);
5133 continue;
5134
5135 case 'o':
5136 macro_read_relocs (&args, r);
252b5132
RH
5137 continue;
5138
5139 case 'u':
6e1304d8 5140 macro_read_relocs (&args, r);
9c2799c2 5141 gas_assert (ep != NULL
90ecf173
MR
5142 && (ep->X_op == O_constant
5143 || (ep->X_op == O_symbol
5144 && (*r == BFD_RELOC_MIPS_HIGHEST
5145 || *r == BFD_RELOC_HI16_S
5146 || *r == BFD_RELOC_HI16
5147 || *r == BFD_RELOC_GPREL16
5148 || *r == BFD_RELOC_MIPS_GOT_HI16
5149 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
5150 continue;
5151
5152 case 'p':
9c2799c2 5153 gas_assert (ep != NULL);
bad36eac 5154
252b5132
RH
5155 /*
5156 * This allows macro() to pass an immediate expression for
5157 * creating short branches without creating a symbol.
bad36eac
DJ
5158 *
5159 * We don't allow branch relaxation for these branches, as
5160 * they should only appear in ".set nomacro" anyway.
252b5132
RH
5161 */
5162 if (ep->X_op == O_constant)
5163 {
df58fc94
RS
5164 /* For microMIPS we always use relocations for branches.
5165 So we should not resolve immediate values. */
5166 gas_assert (!mips_opts.micromips);
5167
bad36eac
DJ
5168 if ((ep->X_add_number & 3) != 0)
5169 as_bad (_("branch to misaligned address (0x%lx)"),
5170 (unsigned long) ep->X_add_number);
5171 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5172 as_bad (_("branch address range overflow (0x%lx)"),
5173 (unsigned long) ep->X_add_number);
252b5132
RH
5174 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5175 ep = NULL;
5176 }
5177 else
0b25d3e6 5178 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
5179 continue;
5180
5181 case 'a':
9c2799c2 5182 gas_assert (ep != NULL);
f6688943 5183 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
5184 continue;
5185
5186 case 'C':
df58fc94
RS
5187 gas_assert (!mips_opts.micromips);
5188 INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
252b5132
RH
5189 continue;
5190
d43b4baf 5191 case 'k':
df58fc94
RS
5192 INSERT_OPERAND (mips_opts.micromips,
5193 CACHE, insn, va_arg (args, unsigned long));
5194 continue;
5195
5196 case '|':
5197 gas_assert (mips_opts.micromips);
5198 INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5199 continue;
5200
5201 case '.':
5202 gas_assert (mips_opts.micromips);
5203 INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5204 continue;
5205
dec0624d
MR
5206 case '\\':
5207 INSERT_OPERAND (mips_opts.micromips,
5208 3BITPOS, insn, va_arg (args, unsigned int));
5209 continue;
5210
df58fc94 5211 case '~':
dec0624d
MR
5212 INSERT_OPERAND (mips_opts.micromips,
5213 OFFSET12, insn, va_arg (args, unsigned long));
df58fc94
RS
5214 continue;
5215
5216 case 'N':
5217 gas_assert (mips_opts.micromips);
5218 INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5219 continue;
5220
5221 case 'm': /* Opcode extension character. */
5222 gas_assert (mips_opts.micromips);
5223 switch (*fmt++)
5224 {
5225 case 'j':
5226 INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5227 break;
5228
5229 case 'p':
5230 INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5231 break;
5232
5233 case 'F':
5234 INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5235 break;
5236
5237 default:
b37df7c4 5238 abort ();
df58fc94 5239 }
d43b4baf
TS
5240 continue;
5241
252b5132 5242 default:
b37df7c4 5243 abort ();
252b5132
RH
5244 }
5245 break;
5246 }
5247 va_end (args);
9c2799c2 5248 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5249
df58fc94 5250 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5251}
5252
5253static void
67c0d1eb 5254mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 5255 va_list *args)
252b5132 5256{
1e915849 5257 struct mips_opcode *mo;
252b5132 5258 struct mips_cl_insn insn;
f6688943
TS
5259 bfd_reloc_code_real_type r[3]
5260 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 5261
1e915849 5262 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
5263 gas_assert (mo);
5264 gas_assert (strcmp (name, mo->name) == 0);
252b5132 5265
1e915849 5266 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 5267 {
1e915849 5268 ++mo;
9c2799c2
NC
5269 gas_assert (mo->name);
5270 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
5271 }
5272
1e915849 5273 create_insn (&insn, mo);
252b5132
RH
5274 for (;;)
5275 {
5276 int c;
5277
5278 c = *fmt++;
5279 switch (c)
5280 {
5281 case '\0':
5282 break;
5283
5284 case ',':
5285 case '(':
5286 case ')':
5287 continue;
5288
5289 case 'y':
5290 case 'w':
03ea81db 5291 MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
252b5132
RH
5292 continue;
5293
5294 case 'x':
5295 case 'v':
03ea81db 5296 MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
252b5132
RH
5297 continue;
5298
5299 case 'z':
03ea81db 5300 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
252b5132
RH
5301 continue;
5302
5303 case 'Z':
03ea81db 5304 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
252b5132
RH
5305 continue;
5306
5307 case '0':
5308 case 'S':
5309 case 'P':
5310 case 'R':
5311 continue;
5312
5313 case 'X':
03ea81db 5314 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
252b5132
RH
5315 continue;
5316
5317 case 'Y':
5318 {
5319 int regno;
5320
03ea81db 5321 regno = va_arg (*args, int);
252b5132 5322 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
a9e24354 5323 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
252b5132
RH
5324 }
5325 continue;
5326
5327 case '<':
5328 case '>':
5329 case '4':
5330 case '5':
5331 case 'H':
5332 case 'W':
5333 case 'D':
5334 case 'j':
5335 case '8':
5336 case 'V':
5337 case 'C':
5338 case 'U':
5339 case 'k':
5340 case 'K':
5341 case 'p':
5342 case 'q':
5343 {
b886a2ab
RS
5344 offsetT value;
5345
9c2799c2 5346 gas_assert (ep != NULL);
252b5132
RH
5347
5348 if (ep->X_op != O_constant)
874e8986 5349 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 5350 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 5351 {
b886a2ab 5352 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 5353 ep = NULL;
f6688943 5354 *r = BFD_RELOC_UNUSED;
252b5132
RH
5355 }
5356 }
5357 continue;
5358
5359 case '6':
03ea81db 5360 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
252b5132
RH
5361 continue;
5362 }
5363
5364 break;
5365 }
5366
9c2799c2 5367 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5368
df58fc94 5369 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5370}
5371
2051e8c4
MR
5372/*
5373 * Sign-extend 32-bit mode constants that have bit 31 set and all
5374 * higher bits unset.
5375 */
9f872bbe 5376static void
2051e8c4
MR
5377normalize_constant_expr (expressionS *ex)
5378{
9ee2a2d4 5379 if (ex->X_op == O_constant
2051e8c4
MR
5380 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5381 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5382 - 0x80000000);
5383}
5384
5385/*
5386 * Sign-extend 32-bit mode address offsets that have bit 31 set and
5387 * all higher bits unset.
5388 */
5389static void
5390normalize_address_expr (expressionS *ex)
5391{
5392 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5393 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5394 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5395 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5396 - 0x80000000);
5397}
5398
438c16b8
TS
5399/*
5400 * Generate a "jalr" instruction with a relocation hint to the called
5401 * function. This occurs in NewABI PIC code.
5402 */
5403static void
df58fc94 5404macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 5405{
df58fc94
RS
5406 static const bfd_reloc_code_real_type jalr_relocs[2]
5407 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5408 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5409 const char *jalr;
685736be 5410 char *f = NULL;
b34976b6 5411
1180b5a4 5412 if (MIPS_JALR_HINT_P (ep))
f21f8242 5413 {
cc3d92a5 5414 frag_grow (8);
f21f8242
AO
5415 f = frag_more (0);
5416 }
2906b037 5417 if (mips_opts.micromips)
df58fc94
RS
5418 {
5419 jalr = mips_opts.noreorder && !cprestore ? "jalr" : "jalrs";
e64af278
MR
5420 if (MIPS_JALR_HINT_P (ep)
5421 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
5422 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5423 else
5424 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5425 }
2906b037
MR
5426 else
5427 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 5428 if (MIPS_JALR_HINT_P (ep))
df58fc94 5429 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
5430}
5431
252b5132
RH
5432/*
5433 * Generate a "lui" instruction.
5434 */
5435static void
67c0d1eb 5436macro_build_lui (expressionS *ep, int regnum)
252b5132 5437{
9c2799c2 5438 gas_assert (! mips_opts.mips16);
252b5132 5439
df58fc94 5440 if (ep->X_op != O_constant)
252b5132 5441 {
9c2799c2 5442 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
5443 /* _gp_disp is a special case, used from s_cpload.
5444 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 5445 gas_assert (mips_pic == NO_PIC
78e1bb40 5446 || (! HAVE_NEWABI
aa6975fb
ILT
5447 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5448 || (! mips_in_shared
bbe506e8
TS
5449 && strcmp (S_GET_NAME (ep->X_add_symbol),
5450 "__gnu_local_gp") == 0));
252b5132
RH
5451 }
5452
df58fc94 5453 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
5454}
5455
885add95
CD
5456/* Generate a sequence of instructions to do a load or store from a constant
5457 offset off of a base register (breg) into/from a target register (treg),
5458 using AT if necessary. */
5459static void
67c0d1eb
RS
5460macro_build_ldst_constoffset (expressionS *ep, const char *op,
5461 int treg, int breg, int dbl)
885add95 5462{
9c2799c2 5463 gas_assert (ep->X_op == O_constant);
885add95 5464
256ab948 5465 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5466 if (!dbl)
5467 normalize_constant_expr (ep);
256ab948 5468
67c1ffbe 5469 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 5470 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
5471 as_warn (_("operand overflow"));
5472
5473 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5474 {
5475 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 5476 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
5477 }
5478 else
5479 {
5480 /* 32-bit offset, need multiple instructions and AT, like:
5481 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
5482 addu $tempreg,$tempreg,$breg
5483 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
5484 to handle the complete offset. */
67c0d1eb
RS
5485 macro_build_lui (ep, AT);
5486 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5487 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 5488
741fe287 5489 if (!mips_opts.at)
8fc2e39e 5490 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
5491 }
5492}
5493
252b5132
RH
5494/* set_at()
5495 * Generates code to set the $at register to true (one)
5496 * if reg is less than the immediate expression.
5497 */
5498static void
67c0d1eb 5499set_at (int reg, int unsignedp)
252b5132
RH
5500{
5501 if (imm_expr.X_op == O_constant
5502 && imm_expr.X_add_number >= -0x8000
5503 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
5504 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5505 AT, reg, BFD_RELOC_LO16);
252b5132
RH
5506 else
5507 {
67c0d1eb
RS
5508 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5509 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
5510 }
5511}
5512
5513/* Warn if an expression is not a constant. */
5514
5515static void
17a2f251 5516check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
5517{
5518 if (ex->X_op == O_big)
5519 as_bad (_("unsupported large constant"));
5520 else if (ex->X_op != O_constant)
9ee2a2d4
MR
5521 as_bad (_("Instruction %s requires absolute expression"),
5522 ip->insn_mo->name);
13757d0c 5523
9ee2a2d4
MR
5524 if (HAVE_32BIT_GPRS)
5525 normalize_constant_expr (ex);
252b5132
RH
5526}
5527
5528/* Count the leading zeroes by performing a binary chop. This is a
5529 bulky bit of source, but performance is a LOT better for the
5530 majority of values than a simple loop to count the bits:
5531 for (lcnt = 0; (lcnt < 32); lcnt++)
5532 if ((v) & (1 << (31 - lcnt)))
5533 break;
5534 However it is not code size friendly, and the gain will drop a bit
5535 on certain cached systems.
5536*/
5537#define COUNT_TOP_ZEROES(v) \
5538 (((v) & ~0xffff) == 0 \
5539 ? ((v) & ~0xff) == 0 \
5540 ? ((v) & ~0xf) == 0 \
5541 ? ((v) & ~0x3) == 0 \
5542 ? ((v) & ~0x1) == 0 \
5543 ? !(v) \
5544 ? 32 \
5545 : 31 \
5546 : 30 \
5547 : ((v) & ~0x7) == 0 \
5548 ? 29 \
5549 : 28 \
5550 : ((v) & ~0x3f) == 0 \
5551 ? ((v) & ~0x1f) == 0 \
5552 ? 27 \
5553 : 26 \
5554 : ((v) & ~0x7f) == 0 \
5555 ? 25 \
5556 : 24 \
5557 : ((v) & ~0xfff) == 0 \
5558 ? ((v) & ~0x3ff) == 0 \
5559 ? ((v) & ~0x1ff) == 0 \
5560 ? 23 \
5561 : 22 \
5562 : ((v) & ~0x7ff) == 0 \
5563 ? 21 \
5564 : 20 \
5565 : ((v) & ~0x3fff) == 0 \
5566 ? ((v) & ~0x1fff) == 0 \
5567 ? 19 \
5568 : 18 \
5569 : ((v) & ~0x7fff) == 0 \
5570 ? 17 \
5571 : 16 \
5572 : ((v) & ~0xffffff) == 0 \
5573 ? ((v) & ~0xfffff) == 0 \
5574 ? ((v) & ~0x3ffff) == 0 \
5575 ? ((v) & ~0x1ffff) == 0 \
5576 ? 15 \
5577 : 14 \
5578 : ((v) & ~0x7ffff) == 0 \
5579 ? 13 \
5580 : 12 \
5581 : ((v) & ~0x3fffff) == 0 \
5582 ? ((v) & ~0x1fffff) == 0 \
5583 ? 11 \
5584 : 10 \
5585 : ((v) & ~0x7fffff) == 0 \
5586 ? 9 \
5587 : 8 \
5588 : ((v) & ~0xfffffff) == 0 \
5589 ? ((v) & ~0x3ffffff) == 0 \
5590 ? ((v) & ~0x1ffffff) == 0 \
5591 ? 7 \
5592 : 6 \
5593 : ((v) & ~0x7ffffff) == 0 \
5594 ? 5 \
5595 : 4 \
5596 : ((v) & ~0x3fffffff) == 0 \
5597 ? ((v) & ~0x1fffffff) == 0 \
5598 ? 3 \
5599 : 2 \
5600 : ((v) & ~0x7fffffff) == 0 \
5601 ? 1 \
5602 : 0)
5603
5604/* load_register()
67c1ffbe 5605 * This routine generates the least number of instructions necessary to load
252b5132
RH
5606 * an absolute expression value into a register.
5607 */
5608static void
67c0d1eb 5609load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
5610{
5611 int freg;
5612 expressionS hi32, lo32;
5613
5614 if (ep->X_op != O_big)
5615 {
9c2799c2 5616 gas_assert (ep->X_op == O_constant);
256ab948
TS
5617
5618 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5619 if (!dbl)
5620 normalize_constant_expr (ep);
256ab948
TS
5621
5622 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
5623 {
5624 /* We can handle 16 bit signed values with an addiu to
5625 $zero. No need to ever use daddiu here, since $zero and
5626 the result are always correct in 32 bit mode. */
67c0d1eb 5627 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5628 return;
5629 }
5630 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5631 {
5632 /* We can handle 16 bit unsigned values with an ori to
5633 $zero. */
67c0d1eb 5634 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5635 return;
5636 }
256ab948 5637 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
5638 {
5639 /* 32 bit values require an lui. */
df58fc94 5640 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 5641 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 5642 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
5643 return;
5644 }
5645 }
5646
5647 /* The value is larger than 32 bits. */
5648
2051e8c4 5649 if (!dbl || HAVE_32BIT_GPRS)
252b5132 5650 {
55e08f71
NC
5651 char value[32];
5652
5653 sprintf_vma (value, ep->X_add_number);
20e1fcfd 5654 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 5655 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5656 return;
5657 }
5658
5659 if (ep->X_op != O_big)
5660 {
5661 hi32 = *ep;
5662 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5663 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5664 hi32.X_add_number &= 0xffffffff;
5665 lo32 = *ep;
5666 lo32.X_add_number &= 0xffffffff;
5667 }
5668 else
5669 {
9c2799c2 5670 gas_assert (ep->X_add_number > 2);
252b5132
RH
5671 if (ep->X_add_number == 3)
5672 generic_bignum[3] = 0;
5673 else if (ep->X_add_number > 4)
5674 as_bad (_("Number larger than 64 bits"));
5675 lo32.X_op = O_constant;
5676 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
5677 hi32.X_op = O_constant;
5678 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
5679 }
5680
5681 if (hi32.X_add_number == 0)
5682 freg = 0;
5683 else
5684 {
5685 int shift, bit;
5686 unsigned long hi, lo;
5687
956cd1d6 5688 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
5689 {
5690 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
5691 {
67c0d1eb 5692 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
5693 return;
5694 }
5695 if (lo32.X_add_number & 0x80000000)
5696 {
df58fc94 5697 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 5698 if (lo32.X_add_number & 0xffff)
67c0d1eb 5699 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
5700 return;
5701 }
5702 }
252b5132
RH
5703
5704 /* Check for 16bit shifted constant. We know that hi32 is
5705 non-zero, so start the mask on the first bit of the hi32
5706 value. */
5707 shift = 17;
5708 do
beae10d5
KH
5709 {
5710 unsigned long himask, lomask;
5711
5712 if (shift < 32)
5713 {
5714 himask = 0xffff >> (32 - shift);
5715 lomask = (0xffff << shift) & 0xffffffff;
5716 }
5717 else
5718 {
5719 himask = 0xffff << (shift - 32);
5720 lomask = 0;
5721 }
5722 if ((hi32.X_add_number & ~(offsetT) himask) == 0
5723 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
5724 {
5725 expressionS tmp;
5726
5727 tmp.X_op = O_constant;
5728 if (shift < 32)
5729 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
5730 | (lo32.X_add_number >> shift));
5731 else
5732 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 5733 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 5734 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 5735 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
5736 return;
5737 }
f9419b05 5738 ++shift;
beae10d5
KH
5739 }
5740 while (shift <= (64 - 16));
252b5132
RH
5741
5742 /* Find the bit number of the lowest one bit, and store the
5743 shifted value in hi/lo. */
5744 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
5745 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
5746 if (lo != 0)
5747 {
5748 bit = 0;
5749 while ((lo & 1) == 0)
5750 {
5751 lo >>= 1;
5752 ++bit;
5753 }
5754 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
5755 hi >>= bit;
5756 }
5757 else
5758 {
5759 bit = 32;
5760 while ((hi & 1) == 0)
5761 {
5762 hi >>= 1;
5763 ++bit;
5764 }
5765 lo = hi;
5766 hi = 0;
5767 }
5768
5769 /* Optimize if the shifted value is a (power of 2) - 1. */
5770 if ((hi == 0 && ((lo + 1) & lo) == 0)
5771 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
5772 {
5773 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 5774 if (shift != 0)
beae10d5 5775 {
252b5132
RH
5776 expressionS tmp;
5777
5778 /* This instruction will set the register to be all
5779 ones. */
beae10d5
KH
5780 tmp.X_op = O_constant;
5781 tmp.X_add_number = (offsetT) -1;
67c0d1eb 5782 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
5783 if (bit != 0)
5784 {
5785 bit += shift;
df58fc94 5786 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 5787 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 5788 }
df58fc94 5789 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 5790 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
5791 return;
5792 }
5793 }
252b5132
RH
5794
5795 /* Sign extend hi32 before calling load_register, because we can
5796 generally get better code when we load a sign extended value. */
5797 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 5798 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 5799 load_register (reg, &hi32, 0);
252b5132
RH
5800 freg = reg;
5801 }
5802 if ((lo32.X_add_number & 0xffff0000) == 0)
5803 {
5804 if (freg != 0)
5805 {
df58fc94 5806 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
5807 freg = reg;
5808 }
5809 }
5810 else
5811 {
5812 expressionS mid16;
5813
956cd1d6 5814 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 5815 {
df58fc94
RS
5816 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5817 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
5818 return;
5819 }
252b5132
RH
5820
5821 if (freg != 0)
5822 {
df58fc94 5823 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
5824 freg = reg;
5825 }
5826 mid16 = lo32;
5827 mid16.X_add_number >>= 16;
67c0d1eb 5828 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 5829 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
5830 freg = reg;
5831 }
5832 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 5833 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
5834}
5835
269137b2
TS
5836static inline void
5837load_delay_nop (void)
5838{
5839 if (!gpr_interlocks)
5840 macro_build (NULL, "nop", "");
5841}
5842
252b5132
RH
5843/* Load an address into a register. */
5844
5845static void
67c0d1eb 5846load_address (int reg, expressionS *ep, int *used_at)
252b5132 5847{
252b5132
RH
5848 if (ep->X_op != O_constant
5849 && ep->X_op != O_symbol)
5850 {
5851 as_bad (_("expression too complex"));
5852 ep->X_op = O_constant;
5853 }
5854
5855 if (ep->X_op == O_constant)
5856 {
67c0d1eb 5857 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
5858 return;
5859 }
5860
5861 if (mips_pic == NO_PIC)
5862 {
5863 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 5864 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5865 Otherwise we want
5866 lui $reg,<sym> (BFD_RELOC_HI16_S)
5867 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 5868 If we have an addend, we always use the latter form.
76b3015f 5869
d6bc6245
TS
5870 With 64bit address space and a usable $at we want
5871 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5872 lui $at,<sym> (BFD_RELOC_HI16_S)
5873 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5874 daddiu $at,<sym> (BFD_RELOC_LO16)
5875 dsll32 $reg,0
3a482fd5 5876 daddu $reg,$reg,$at
76b3015f 5877
c03099e6 5878 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
5879 on superscalar processors.
5880 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5881 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5882 dsll $reg,16
5883 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
5884 dsll $reg,16
5885 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
5886
5887 For GP relative symbols in 64bit address space we can use
5888 the same sequence as in 32bit address space. */
aed1a261 5889 if (HAVE_64BIT_SYMBOLS)
d6bc6245 5890 {
6caf9ef4
TS
5891 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5892 && !nopic_need_relax (ep->X_add_symbol, 1))
5893 {
5894 relax_start (ep->X_add_symbol);
5895 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5896 mips_gp_register, BFD_RELOC_GPREL16);
5897 relax_switch ();
5898 }
d6bc6245 5899
741fe287 5900 if (*used_at == 0 && mips_opts.at)
d6bc6245 5901 {
df58fc94
RS
5902 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5903 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
5904 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5905 BFD_RELOC_MIPS_HIGHER);
5906 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 5907 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 5908 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
5909 *used_at = 1;
5910 }
5911 else
5912 {
df58fc94 5913 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
5914 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5915 BFD_RELOC_MIPS_HIGHER);
df58fc94 5916 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 5917 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 5918 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 5919 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 5920 }
6caf9ef4
TS
5921
5922 if (mips_relax.sequence)
5923 relax_end ();
d6bc6245 5924 }
252b5132
RH
5925 else
5926 {
d6bc6245 5927 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 5928 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 5929 {
4d7206a2 5930 relax_start (ep->X_add_symbol);
67c0d1eb 5931 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 5932 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5933 relax_switch ();
d6bc6245 5934 }
67c0d1eb
RS
5935 macro_build_lui (ep, reg);
5936 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
5937 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
5938 if (mips_relax.sequence)
5939 relax_end ();
d6bc6245 5940 }
252b5132 5941 }
0a44bf69 5942 else if (!mips_big_got)
252b5132
RH
5943 {
5944 expressionS ex;
5945
5946 /* If this is a reference to an external symbol, we want
5947 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5948 Otherwise we want
5949 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5950 nop
5951 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
5952 If there is a constant, it must be added in after.
5953
ed6fb7bd 5954 If we have NewABI, we want
f5040a92
AO
5955 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5956 unless we're referencing a global symbol with a non-zero
5957 offset, in which case cst must be added separately. */
ed6fb7bd
SC
5958 if (HAVE_NEWABI)
5959 {
f5040a92
AO
5960 if (ep->X_add_number)
5961 {
4d7206a2 5962 ex.X_add_number = ep->X_add_number;
f5040a92 5963 ep->X_add_number = 0;
4d7206a2 5964 relax_start (ep->X_add_symbol);
67c0d1eb
RS
5965 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5966 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5967 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5968 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5969 ex.X_op = O_constant;
67c0d1eb 5970 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5971 reg, reg, BFD_RELOC_LO16);
f5040a92 5972 ep->X_add_number = ex.X_add_number;
4d7206a2 5973 relax_switch ();
f5040a92 5974 }
67c0d1eb 5975 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 5976 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
5977 if (mips_relax.sequence)
5978 relax_end ();
ed6fb7bd
SC
5979 }
5980 else
5981 {
f5040a92
AO
5982 ex.X_add_number = ep->X_add_number;
5983 ep->X_add_number = 0;
67c0d1eb
RS
5984 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5985 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5986 load_delay_nop ();
4d7206a2
RS
5987 relax_start (ep->X_add_symbol);
5988 relax_switch ();
67c0d1eb 5989 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 5990 BFD_RELOC_LO16);
4d7206a2 5991 relax_end ();
ed6fb7bd 5992
f5040a92
AO
5993 if (ex.X_add_number != 0)
5994 {
5995 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5996 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5997 ex.X_op = O_constant;
67c0d1eb 5998 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5999 reg, reg, BFD_RELOC_LO16);
f5040a92 6000 }
252b5132
RH
6001 }
6002 }
0a44bf69 6003 else if (mips_big_got)
252b5132
RH
6004 {
6005 expressionS ex;
252b5132
RH
6006
6007 /* This is the large GOT case. If this is a reference to an
6008 external symbol, we want
6009 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6010 addu $reg,$reg,$gp
6011 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
6012
6013 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
6014 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6015 nop
6016 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 6017 If there is a constant, it must be added in after.
f5040a92
AO
6018
6019 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
6020 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6021 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 6022 */
438c16b8
TS
6023 if (HAVE_NEWABI)
6024 {
4d7206a2 6025 ex.X_add_number = ep->X_add_number;
f5040a92 6026 ep->X_add_number = 0;
4d7206a2 6027 relax_start (ep->X_add_symbol);
df58fc94 6028 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6029 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6030 reg, reg, mips_gp_register);
6031 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6032 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
6033 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6034 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6035 else if (ex.X_add_number)
6036 {
6037 ex.X_op = O_constant;
67c0d1eb
RS
6038 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6039 BFD_RELOC_LO16);
f5040a92
AO
6040 }
6041
6042 ep->X_add_number = ex.X_add_number;
4d7206a2 6043 relax_switch ();
67c0d1eb 6044 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6045 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
6046 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6047 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 6048 relax_end ();
438c16b8 6049 }
252b5132 6050 else
438c16b8 6051 {
f5040a92
AO
6052 ex.X_add_number = ep->X_add_number;
6053 ep->X_add_number = 0;
4d7206a2 6054 relax_start (ep->X_add_symbol);
df58fc94 6055 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6056 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6057 reg, reg, mips_gp_register);
6058 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6059 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
6060 relax_switch ();
6061 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
6062 {
6063 /* We need a nop before loading from $gp. This special
6064 check is required because the lui which starts the main
6065 instruction stream does not refer to $gp, and so will not
6066 insert the nop which may be required. */
67c0d1eb 6067 macro_build (NULL, "nop", "");
438c16b8 6068 }
67c0d1eb 6069 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6070 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6071 load_delay_nop ();
67c0d1eb 6072 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 6073 BFD_RELOC_LO16);
4d7206a2 6074 relax_end ();
438c16b8 6075
f5040a92
AO
6076 if (ex.X_add_number != 0)
6077 {
6078 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6079 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
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 6083 }
252b5132
RH
6084 }
6085 }
252b5132
RH
6086 else
6087 abort ();
8fc2e39e 6088
741fe287 6089 if (!mips_opts.at && *used_at == 1)
8fc2e39e 6090 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
6091}
6092
ea1fb5dc
RS
6093/* Move the contents of register SOURCE into register DEST. */
6094
6095static void
67c0d1eb 6096move_register (int dest, int source)
ea1fb5dc 6097{
df58fc94
RS
6098 /* Prefer to use a 16-bit microMIPS instruction unless the previous
6099 instruction specifically requires a 32-bit one. */
6100 if (mips_opts.micromips
6101 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 6102 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94
RS
6103 else
6104 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
6105 dest, source, 0);
ea1fb5dc
RS
6106}
6107
4d7206a2 6108/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
6109 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
6110 The two alternatives are:
4d7206a2
RS
6111
6112 Global symbol Local sybmol
6113 ------------- ------------
6114 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
6115 ... ...
6116 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
6117
6118 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
6119 emits the second for a 16-bit offset or add_got_offset_hilo emits
6120 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
6121
6122static void
67c0d1eb 6123load_got_offset (int dest, expressionS *local)
4d7206a2
RS
6124{
6125 expressionS global;
6126
6127 global = *local;
6128 global.X_add_number = 0;
6129
6130 relax_start (local->X_add_symbol);
67c0d1eb
RS
6131 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6132 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 6133 relax_switch ();
67c0d1eb
RS
6134 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6135 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
6136 relax_end ();
6137}
6138
6139static void
67c0d1eb 6140add_got_offset (int dest, expressionS *local)
4d7206a2
RS
6141{
6142 expressionS global;
6143
6144 global.X_op = O_constant;
6145 global.X_op_symbol = NULL;
6146 global.X_add_symbol = NULL;
6147 global.X_add_number = local->X_add_number;
6148
6149 relax_start (local->X_add_symbol);
67c0d1eb 6150 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
6151 dest, dest, BFD_RELOC_LO16);
6152 relax_switch ();
67c0d1eb 6153 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
6154 relax_end ();
6155}
6156
f6a22291
MR
6157static void
6158add_got_offset_hilo (int dest, expressionS *local, int tmp)
6159{
6160 expressionS global;
6161 int hold_mips_optimize;
6162
6163 global.X_op = O_constant;
6164 global.X_op_symbol = NULL;
6165 global.X_add_symbol = NULL;
6166 global.X_add_number = local->X_add_number;
6167
6168 relax_start (local->X_add_symbol);
6169 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6170 relax_switch ();
6171 /* Set mips_optimize around the lui instruction to avoid
6172 inserting an unnecessary nop after the lw. */
6173 hold_mips_optimize = mips_optimize;
6174 mips_optimize = 2;
6175 macro_build_lui (&global, tmp);
6176 mips_optimize = hold_mips_optimize;
6177 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6178 relax_end ();
6179
6180 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6181}
6182
df58fc94
RS
6183/* Emit a sequence of instructions to emulate a branch likely operation.
6184 BR is an ordinary branch corresponding to one to be emulated. BRNEG
6185 is its complementing branch with the original condition negated.
6186 CALL is set if the original branch specified the link operation.
6187 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6188
6189 Code like this is produced in the noreorder mode:
6190
6191 BRNEG <args>, 1f
6192 nop
6193 b <sym>
6194 delay slot (executed only if branch taken)
6195 1:
6196
6197 or, if CALL is set:
6198
6199 BRNEG <args>, 1f
6200 nop
6201 bal <sym>
6202 delay slot (executed only if branch taken)
6203 1:
6204
6205 In the reorder mode the delay slot would be filled with a nop anyway,
6206 so code produced is simply:
6207
6208 BR <args>, <sym>
6209 nop
6210
6211 This function is used when producing code for the microMIPS ASE that
6212 does not implement branch likely instructions in hardware. */
6213
6214static void
6215macro_build_branch_likely (const char *br, const char *brneg,
6216 int call, expressionS *ep, const char *fmt,
6217 unsigned int sreg, unsigned int treg)
6218{
6219 int noreorder = mips_opts.noreorder;
6220 expressionS expr1;
6221
6222 gas_assert (mips_opts.micromips);
6223 start_noreorder ();
6224 if (noreorder)
6225 {
6226 micromips_label_expr (&expr1);
6227 macro_build (&expr1, brneg, fmt, sreg, treg);
6228 macro_build (NULL, "nop", "");
6229 macro_build (ep, call ? "bal" : "b", "p");
6230
6231 /* Set to true so that append_insn adds a label. */
6232 emit_branch_likely_macro = TRUE;
6233 }
6234 else
6235 {
6236 macro_build (ep, br, fmt, sreg, treg);
6237 macro_build (NULL, "nop", "");
6238 }
6239 end_noreorder ();
6240}
6241
6242/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6243 the condition code tested. EP specifies the branch target. */
6244
6245static void
6246macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6247{
6248 const int call = 0;
6249 const char *brneg;
6250 const char *br;
6251
6252 switch (type)
6253 {
6254 case M_BC1FL:
6255 br = "bc1f";
6256 brneg = "bc1t";
6257 break;
6258 case M_BC1TL:
6259 br = "bc1t";
6260 brneg = "bc1f";
6261 break;
6262 case M_BC2FL:
6263 br = "bc2f";
6264 brneg = "bc2t";
6265 break;
6266 case M_BC2TL:
6267 br = "bc2t";
6268 brneg = "bc2f";
6269 break;
6270 default:
6271 abort ();
6272 }
6273 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6274}
6275
6276/* Emit a two-argument branch macro specified by TYPE, using SREG as
6277 the register tested. EP specifies the branch target. */
6278
6279static void
6280macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6281{
6282 const char *brneg = NULL;
6283 const char *br;
6284 int call = 0;
6285
6286 switch (type)
6287 {
6288 case M_BGEZ:
6289 br = "bgez";
6290 break;
6291 case M_BGEZL:
6292 br = mips_opts.micromips ? "bgez" : "bgezl";
6293 brneg = "bltz";
6294 break;
6295 case M_BGEZALL:
6296 gas_assert (mips_opts.micromips);
6297 br = "bgezals";
6298 brneg = "bltz";
6299 call = 1;
6300 break;
6301 case M_BGTZ:
6302 br = "bgtz";
6303 break;
6304 case M_BGTZL:
6305 br = mips_opts.micromips ? "bgtz" : "bgtzl";
6306 brneg = "blez";
6307 break;
6308 case M_BLEZ:
6309 br = "blez";
6310 break;
6311 case M_BLEZL:
6312 br = mips_opts.micromips ? "blez" : "blezl";
6313 brneg = "bgtz";
6314 break;
6315 case M_BLTZ:
6316 br = "bltz";
6317 break;
6318 case M_BLTZL:
6319 br = mips_opts.micromips ? "bltz" : "bltzl";
6320 brneg = "bgez";
6321 break;
6322 case M_BLTZALL:
6323 gas_assert (mips_opts.micromips);
6324 br = "bltzals";
6325 brneg = "bgez";
6326 call = 1;
6327 break;
6328 default:
6329 abort ();
6330 }
6331 if (mips_opts.micromips && brneg)
6332 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6333 else
6334 macro_build (ep, br, "s,p", sreg);
6335}
6336
6337/* Emit a three-argument branch macro specified by TYPE, using SREG and
6338 TREG as the registers tested. EP specifies the branch target. */
6339
6340static void
6341macro_build_branch_rsrt (int type, expressionS *ep,
6342 unsigned int sreg, unsigned int treg)
6343{
6344 const char *brneg = NULL;
6345 const int call = 0;
6346 const char *br;
6347
6348 switch (type)
6349 {
6350 case M_BEQ:
6351 case M_BEQ_I:
6352 br = "beq";
6353 break;
6354 case M_BEQL:
6355 case M_BEQL_I:
6356 br = mips_opts.micromips ? "beq" : "beql";
6357 brneg = "bne";
6358 break;
6359 case M_BNE:
6360 case M_BNE_I:
6361 br = "bne";
6362 break;
6363 case M_BNEL:
6364 case M_BNEL_I:
6365 br = mips_opts.micromips ? "bne" : "bnel";
6366 brneg = "beq";
6367 break;
6368 default:
6369 abort ();
6370 }
6371 if (mips_opts.micromips && brneg)
6372 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6373 else
6374 macro_build (ep, br, "s,t,p", sreg, treg);
6375}
6376
252b5132
RH
6377/*
6378 * Build macros
6379 * This routine implements the seemingly endless macro or synthesized
6380 * instructions and addressing modes in the mips assembly language. Many
6381 * of these macros are simple and are similar to each other. These could
67c1ffbe 6382 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
6383 * this verbose method. Others are not simple macros but are more like
6384 * optimizing code generation.
6385 * One interesting optimization is when several store macros appear
67c1ffbe 6386 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
6387 * The ensuing load upper instructions are ommited. This implies some kind
6388 * of global optimization. We currently only optimize within a single macro.
6389 * For many of the load and store macros if the address is specified as a
6390 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6391 * first load register 'at' with zero and use it as the base register. The
6392 * mips assembler simply uses register $zero. Just one tiny optimization
6393 * we're missing.
6394 */
6395static void
17a2f251 6396macro (struct mips_cl_insn *ip)
252b5132 6397{
741fe287
MR
6398 unsigned int treg, sreg, dreg, breg;
6399 unsigned int tempreg;
252b5132 6400 int mask;
43841e91 6401 int used_at = 0;
df58fc94 6402 expressionS label_expr;
252b5132 6403 expressionS expr1;
df58fc94 6404 expressionS *ep;
252b5132
RH
6405 const char *s;
6406 const char *s2;
6407 const char *fmt;
6408 int likely = 0;
252b5132 6409 int coproc = 0;
df58fc94 6410 int off12 = 0;
1abe91b1 6411 int call = 0;
df58fc94
RS
6412 int jals = 0;
6413 int dbl = 0;
6414 int imm = 0;
6415 int ust = 0;
6416 int lp = 0;
6417 int ab = 0;
dd6a37e7 6418 int off0 = 0;
252b5132 6419 int off;
67c0d1eb 6420 offsetT maxnum;
252b5132 6421 bfd_reloc_code_real_type r;
252b5132
RH
6422 int hold_mips_optimize;
6423
9c2799c2 6424 gas_assert (! mips_opts.mips16);
252b5132 6425
df58fc94
RS
6426 treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6427 dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6428 sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
252b5132
RH
6429 mask = ip->insn_mo->mask;
6430
df58fc94
RS
6431 label_expr.X_op = O_constant;
6432 label_expr.X_op_symbol = NULL;
6433 label_expr.X_add_symbol = NULL;
6434 label_expr.X_add_number = 0;
6435
252b5132
RH
6436 expr1.X_op = O_constant;
6437 expr1.X_op_symbol = NULL;
6438 expr1.X_add_symbol = NULL;
6439 expr1.X_add_number = 1;
6440
6441 switch (mask)
6442 {
6443 case M_DABS:
6444 dbl = 1;
6445 case M_ABS:
df58fc94
RS
6446 /* bgez $a0,1f
6447 move v0,$a0
6448 sub v0,$zero,$a0
6449 1:
6450 */
252b5132 6451
7d10b47d 6452 start_noreorder ();
252b5132 6453
df58fc94
RS
6454 if (mips_opts.micromips)
6455 micromips_label_expr (&label_expr);
6456 else
6457 label_expr.X_add_number = 8;
6458 macro_build (&label_expr, "bgez", "s,p", sreg);
252b5132 6459 if (dreg == sreg)
a605d2b3 6460 macro_build (NULL, "nop", "");
252b5132 6461 else
67c0d1eb
RS
6462 move_register (dreg, sreg);
6463 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
df58fc94
RS
6464 if (mips_opts.micromips)
6465 micromips_add_label ();
252b5132 6466
7d10b47d 6467 end_noreorder ();
8fc2e39e 6468 break;
252b5132
RH
6469
6470 case M_ADD_I:
6471 s = "addi";
6472 s2 = "add";
6473 goto do_addi;
6474 case M_ADDU_I:
6475 s = "addiu";
6476 s2 = "addu";
6477 goto do_addi;
6478 case M_DADD_I:
6479 dbl = 1;
6480 s = "daddi";
6481 s2 = "dadd";
df58fc94
RS
6482 if (!mips_opts.micromips)
6483 goto do_addi;
6484 if (imm_expr.X_op == O_constant
6485 && imm_expr.X_add_number >= -0x200
6486 && imm_expr.X_add_number < 0x200)
6487 {
6488 macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6489 break;
6490 }
6491 goto do_addi_i;
252b5132
RH
6492 case M_DADDU_I:
6493 dbl = 1;
6494 s = "daddiu";
6495 s2 = "daddu";
6496 do_addi:
6497 if (imm_expr.X_op == O_constant
6498 && imm_expr.X_add_number >= -0x8000
6499 && imm_expr.X_add_number < 0x8000)
6500 {
67c0d1eb 6501 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 6502 break;
252b5132 6503 }
df58fc94 6504 do_addi_i:
8fc2e39e 6505 used_at = 1;
67c0d1eb
RS
6506 load_register (AT, &imm_expr, dbl);
6507 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6508 break;
6509
6510 case M_AND_I:
6511 s = "andi";
6512 s2 = "and";
6513 goto do_bit;
6514 case M_OR_I:
6515 s = "ori";
6516 s2 = "or";
6517 goto do_bit;
6518 case M_NOR_I:
6519 s = "";
6520 s2 = "nor";
6521 goto do_bit;
6522 case M_XOR_I:
6523 s = "xori";
6524 s2 = "xor";
6525 do_bit:
6526 if (imm_expr.X_op == O_constant
6527 && imm_expr.X_add_number >= 0
6528 && imm_expr.X_add_number < 0x10000)
6529 {
6530 if (mask != M_NOR_I)
67c0d1eb 6531 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
6532 else
6533 {
67c0d1eb
RS
6534 macro_build (&imm_expr, "ori", "t,r,i",
6535 treg, sreg, BFD_RELOC_LO16);
6536 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 6537 }
8fc2e39e 6538 break;
252b5132
RH
6539 }
6540
8fc2e39e 6541 used_at = 1;
67c0d1eb
RS
6542 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6543 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6544 break;
6545
8b082fb1
TS
6546 case M_BALIGN:
6547 switch (imm_expr.X_add_number)
6548 {
6549 case 0:
6550 macro_build (NULL, "nop", "");
6551 break;
6552 case 2:
6553 macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6554 break;
03f66e8a
MR
6555 case 1:
6556 case 3:
8b082fb1 6557 macro_build (NULL, "balign", "t,s,2", treg, sreg,
90ecf173 6558 (int) imm_expr.X_add_number);
8b082fb1 6559 break;
03f66e8a
MR
6560 default:
6561 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6562 (unsigned long) imm_expr.X_add_number);
6563 break;
8b082fb1
TS
6564 }
6565 break;
6566
df58fc94
RS
6567 case M_BC1FL:
6568 case M_BC1TL:
6569 case M_BC2FL:
6570 case M_BC2TL:
6571 gas_assert (mips_opts.micromips);
6572 macro_build_branch_ccl (mask, &offset_expr,
6573 EXTRACT_OPERAND (1, BCC, *ip));
6574 break;
6575
252b5132 6576 case M_BEQ_I:
252b5132 6577 case M_BEQL_I:
252b5132 6578 case M_BNE_I:
252b5132 6579 case M_BNEL_I:
252b5132 6580 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
6581 treg = 0;
6582 else
252b5132 6583 {
df58fc94
RS
6584 treg = AT;
6585 used_at = 1;
6586 load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
252b5132 6587 }
df58fc94
RS
6588 /* Fall through. */
6589 case M_BEQL:
6590 case M_BNEL:
6591 macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
252b5132
RH
6592 break;
6593
6594 case M_BGEL:
6595 likely = 1;
6596 case M_BGE:
6597 if (treg == 0)
df58fc94
RS
6598 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6599 else if (sreg == 0)
6600 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6601 else
252b5132 6602 {
df58fc94
RS
6603 used_at = 1;
6604 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6605 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6606 &offset_expr, AT, ZERO);
252b5132 6607 }
df58fc94
RS
6608 break;
6609
6610 case M_BGEZL:
6611 case M_BGEZALL:
6612 case M_BGTZL:
6613 case M_BLEZL:
6614 case M_BLTZL:
6615 case M_BLTZALL:
6616 macro_build_branch_rs (mask, &offset_expr, sreg);
252b5132
RH
6617 break;
6618
6619 case M_BGTL_I:
6620 likely = 1;
6621 case M_BGT_I:
90ecf173 6622 /* Check for > max integer. */
252b5132 6623 maxnum = 0x7fffffff;
ca4e0257 6624 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6625 {
6626 maxnum <<= 16;
6627 maxnum |= 0xffff;
6628 maxnum <<= 16;
6629 maxnum |= 0xffff;
6630 }
6631 if (imm_expr.X_op == O_constant
6632 && imm_expr.X_add_number >= maxnum
ca4e0257 6633 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6634 {
6635 do_false:
90ecf173 6636 /* Result is always false. */
252b5132 6637 if (! likely)
a605d2b3 6638 macro_build (NULL, "nop", "");
252b5132 6639 else
df58fc94 6640 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 6641 break;
252b5132
RH
6642 }
6643 if (imm_expr.X_op != O_constant)
6644 as_bad (_("Unsupported large constant"));
f9419b05 6645 ++imm_expr.X_add_number;
252b5132
RH
6646 /* FALLTHROUGH */
6647 case M_BGE_I:
6648 case M_BGEL_I:
6649 if (mask == M_BGEL_I)
6650 likely = 1;
6651 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6652 {
df58fc94
RS
6653 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
6654 &offset_expr, sreg);
8fc2e39e 6655 break;
252b5132
RH
6656 }
6657 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6658 {
df58fc94
RS
6659 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
6660 &offset_expr, sreg);
8fc2e39e 6661 break;
252b5132
RH
6662 }
6663 maxnum = 0x7fffffff;
ca4e0257 6664 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6665 {
6666 maxnum <<= 16;
6667 maxnum |= 0xffff;
6668 maxnum <<= 16;
6669 maxnum |= 0xffff;
6670 }
6671 maxnum = - maxnum - 1;
6672 if (imm_expr.X_op == O_constant
6673 && imm_expr.X_add_number <= maxnum
ca4e0257 6674 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6675 {
6676 do_true:
6677 /* result is always true */
6678 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 6679 macro_build (&offset_expr, "b", "p");
8fc2e39e 6680 break;
252b5132 6681 }
8fc2e39e 6682 used_at = 1;
67c0d1eb 6683 set_at (sreg, 0);
df58fc94
RS
6684 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6685 &offset_expr, AT, ZERO);
252b5132
RH
6686 break;
6687
6688 case M_BGEUL:
6689 likely = 1;
6690 case M_BGEU:
6691 if (treg == 0)
6692 goto do_true;
df58fc94
RS
6693 else if (sreg == 0)
6694 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6695 &offset_expr, ZERO, treg);
6696 else
252b5132 6697 {
df58fc94
RS
6698 used_at = 1;
6699 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6700 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6701 &offset_expr, AT, ZERO);
252b5132 6702 }
252b5132
RH
6703 break;
6704
6705 case M_BGTUL_I:
6706 likely = 1;
6707 case M_BGTU_I:
6708 if (sreg == 0
ca4e0257 6709 || (HAVE_32BIT_GPRS
252b5132 6710 && imm_expr.X_op == O_constant
f01dc953 6711 && imm_expr.X_add_number == -1))
252b5132
RH
6712 goto do_false;
6713 if (imm_expr.X_op != O_constant)
6714 as_bad (_("Unsupported large constant"));
f9419b05 6715 ++imm_expr.X_add_number;
252b5132
RH
6716 /* FALLTHROUGH */
6717 case M_BGEU_I:
6718 case M_BGEUL_I:
6719 if (mask == M_BGEUL_I)
6720 likely = 1;
6721 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6722 goto do_true;
df58fc94
RS
6723 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6724 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6725 &offset_expr, sreg, ZERO);
6726 else
252b5132 6727 {
df58fc94
RS
6728 used_at = 1;
6729 set_at (sreg, 1);
6730 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6731 &offset_expr, AT, ZERO);
252b5132 6732 }
252b5132
RH
6733 break;
6734
6735 case M_BGTL:
6736 likely = 1;
6737 case M_BGT:
6738 if (treg == 0)
df58fc94
RS
6739 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
6740 else if (sreg == 0)
6741 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
6742 else
252b5132 6743 {
df58fc94
RS
6744 used_at = 1;
6745 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6746 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6747 &offset_expr, AT, ZERO);
252b5132 6748 }
252b5132
RH
6749 break;
6750
6751 case M_BGTUL:
6752 likely = 1;
6753 case M_BGTU:
6754 if (treg == 0)
df58fc94
RS
6755 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6756 &offset_expr, sreg, ZERO);
6757 else if (sreg == 0)
6758 goto do_false;
6759 else
252b5132 6760 {
df58fc94
RS
6761 used_at = 1;
6762 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6763 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6764 &offset_expr, AT, ZERO);
252b5132 6765 }
252b5132
RH
6766 break;
6767
6768 case M_BLEL:
6769 likely = 1;
6770 case M_BLE:
6771 if (treg == 0)
df58fc94
RS
6772 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6773 else if (sreg == 0)
6774 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
6775 else
252b5132 6776 {
df58fc94
RS
6777 used_at = 1;
6778 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6779 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6780 &offset_expr, AT, ZERO);
252b5132 6781 }
252b5132
RH
6782 break;
6783
6784 case M_BLEL_I:
6785 likely = 1;
6786 case M_BLE_I:
6787 maxnum = 0x7fffffff;
ca4e0257 6788 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6789 {
6790 maxnum <<= 16;
6791 maxnum |= 0xffff;
6792 maxnum <<= 16;
6793 maxnum |= 0xffff;
6794 }
6795 if (imm_expr.X_op == O_constant
6796 && imm_expr.X_add_number >= maxnum
ca4e0257 6797 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6798 goto do_true;
6799 if (imm_expr.X_op != O_constant)
6800 as_bad (_("Unsupported large constant"));
f9419b05 6801 ++imm_expr.X_add_number;
252b5132
RH
6802 /* FALLTHROUGH */
6803 case M_BLT_I:
6804 case M_BLTL_I:
6805 if (mask == M_BLTL_I)
6806 likely = 1;
6807 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
6808 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6809 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6810 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6811 else
252b5132 6812 {
df58fc94
RS
6813 used_at = 1;
6814 set_at (sreg, 0);
6815 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6816 &offset_expr, AT, ZERO);
252b5132 6817 }
252b5132
RH
6818 break;
6819
6820 case M_BLEUL:
6821 likely = 1;
6822 case M_BLEU:
6823 if (treg == 0)
df58fc94
RS
6824 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6825 &offset_expr, sreg, ZERO);
6826 else if (sreg == 0)
6827 goto do_true;
6828 else
252b5132 6829 {
df58fc94
RS
6830 used_at = 1;
6831 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6832 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6833 &offset_expr, AT, ZERO);
252b5132 6834 }
252b5132
RH
6835 break;
6836
6837 case M_BLEUL_I:
6838 likely = 1;
6839 case M_BLEU_I:
6840 if (sreg == 0
ca4e0257 6841 || (HAVE_32BIT_GPRS
252b5132 6842 && imm_expr.X_op == O_constant
f01dc953 6843 && imm_expr.X_add_number == -1))
252b5132
RH
6844 goto do_true;
6845 if (imm_expr.X_op != O_constant)
6846 as_bad (_("Unsupported large constant"));
f9419b05 6847 ++imm_expr.X_add_number;
252b5132
RH
6848 /* FALLTHROUGH */
6849 case M_BLTU_I:
6850 case M_BLTUL_I:
6851 if (mask == M_BLTUL_I)
6852 likely = 1;
6853 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6854 goto do_false;
df58fc94
RS
6855 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6856 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6857 &offset_expr, sreg, ZERO);
6858 else
252b5132 6859 {
df58fc94
RS
6860 used_at = 1;
6861 set_at (sreg, 1);
6862 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6863 &offset_expr, AT, ZERO);
252b5132 6864 }
252b5132
RH
6865 break;
6866
6867 case M_BLTL:
6868 likely = 1;
6869 case M_BLT:
6870 if (treg == 0)
df58fc94
RS
6871 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6872 else if (sreg == 0)
6873 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
6874 else
252b5132 6875 {
df58fc94
RS
6876 used_at = 1;
6877 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6878 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6879 &offset_expr, AT, ZERO);
252b5132 6880 }
252b5132
RH
6881 break;
6882
6883 case M_BLTUL:
6884 likely = 1;
6885 case M_BLTU:
6886 if (treg == 0)
6887 goto do_false;
df58fc94
RS
6888 else if (sreg == 0)
6889 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6890 &offset_expr, ZERO, treg);
6891 else
252b5132 6892 {
df58fc94
RS
6893 used_at = 1;
6894 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6895 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6896 &offset_expr, AT, ZERO);
252b5132 6897 }
252b5132
RH
6898 break;
6899
5f74bc13
CD
6900 case M_DEXT:
6901 {
d5818fca
MR
6902 /* Use unsigned arithmetic. */
6903 addressT pos;
6904 addressT size;
5f74bc13 6905
90ecf173 6906 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
6907 {
6908 as_bad (_("Unsupported large constant"));
6909 pos = size = 1;
6910 }
6911 else
6912 {
d5818fca
MR
6913 pos = imm_expr.X_add_number;
6914 size = imm2_expr.X_add_number;
5f74bc13
CD
6915 }
6916
6917 if (pos > 63)
6918 {
d5818fca 6919 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
6920 pos = 1;
6921 }
90ecf173 6922 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
6923 {
6924 as_bad (_("Improper extract size (%lu, position %lu)"),
d5818fca 6925 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
6926 size = 1;
6927 }
6928
6929 if (size <= 32 && pos < 32)
6930 {
6931 s = "dext";
6932 fmt = "t,r,+A,+C";
6933 }
6934 else if (size <= 32)
6935 {
6936 s = "dextu";
6937 fmt = "t,r,+E,+H";
6938 }
6939 else
6940 {
6941 s = "dextm";
6942 fmt = "t,r,+A,+G";
6943 }
d5818fca
MR
6944 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6945 (int) (size - 1));
5f74bc13 6946 }
8fc2e39e 6947 break;
5f74bc13
CD
6948
6949 case M_DINS:
6950 {
d5818fca
MR
6951 /* Use unsigned arithmetic. */
6952 addressT pos;
6953 addressT size;
5f74bc13 6954
90ecf173 6955 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
6956 {
6957 as_bad (_("Unsupported large constant"));
6958 pos = size = 1;
6959 }
6960 else
6961 {
d5818fca
MR
6962 pos = imm_expr.X_add_number;
6963 size = imm2_expr.X_add_number;
5f74bc13
CD
6964 }
6965
6966 if (pos > 63)
6967 {
d5818fca 6968 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
6969 pos = 1;
6970 }
90ecf173 6971 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
6972 {
6973 as_bad (_("Improper insert size (%lu, position %lu)"),
d5818fca 6974 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
6975 size = 1;
6976 }
6977
6978 if (pos < 32 && (pos + size - 1) < 32)
6979 {
6980 s = "dins";
6981 fmt = "t,r,+A,+B";
6982 }
6983 else if (pos >= 32)
6984 {
6985 s = "dinsu";
6986 fmt = "t,r,+E,+F";
6987 }
6988 else
6989 {
6990 s = "dinsm";
6991 fmt = "t,r,+A,+F";
6992 }
750bdd57
AS
6993 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6994 (int) (pos + size - 1));
5f74bc13 6995 }
8fc2e39e 6996 break;
5f74bc13 6997
252b5132
RH
6998 case M_DDIV_3:
6999 dbl = 1;
7000 case M_DIV_3:
7001 s = "mflo";
7002 goto do_div3;
7003 case M_DREM_3:
7004 dbl = 1;
7005 case M_REM_3:
7006 s = "mfhi";
7007 do_div3:
7008 if (treg == 0)
7009 {
7010 as_warn (_("Divide by zero."));
7011 if (mips_trap)
df58fc94 7012 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 7013 else
df58fc94 7014 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 7015 break;
252b5132
RH
7016 }
7017
7d10b47d 7018 start_noreorder ();
252b5132
RH
7019 if (mips_trap)
7020 {
df58fc94 7021 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 7022 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
7023 }
7024 else
7025 {
df58fc94
RS
7026 if (mips_opts.micromips)
7027 micromips_label_expr (&label_expr);
7028 else
7029 label_expr.X_add_number = 8;
7030 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 7031 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
df58fc94
RS
7032 macro_build (NULL, "break", BRK_FMT, 7);
7033 if (mips_opts.micromips)
7034 micromips_add_label ();
252b5132
RH
7035 }
7036 expr1.X_add_number = -1;
8fc2e39e 7037 used_at = 1;
f6a22291 7038 load_register (AT, &expr1, dbl);
df58fc94
RS
7039 if (mips_opts.micromips)
7040 micromips_label_expr (&label_expr);
7041 else
7042 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
7043 macro_build (&label_expr, "bne", "s,t,p", treg, AT);
252b5132
RH
7044 if (dbl)
7045 {
7046 expr1.X_add_number = 1;
f6a22291 7047 load_register (AT, &expr1, dbl);
df58fc94 7048 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
7049 }
7050 else
7051 {
7052 expr1.X_add_number = 0x80000000;
df58fc94 7053 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
7054 }
7055 if (mips_trap)
7056 {
df58fc94 7057 macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
252b5132
RH
7058 /* We want to close the noreorder block as soon as possible, so
7059 that later insns are available for delay slot filling. */
7d10b47d 7060 end_noreorder ();
252b5132
RH
7061 }
7062 else
7063 {
df58fc94
RS
7064 if (mips_opts.micromips)
7065 micromips_label_expr (&label_expr);
7066 else
7067 label_expr.X_add_number = 8;
7068 macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
a605d2b3 7069 macro_build (NULL, "nop", "");
252b5132
RH
7070
7071 /* We want to close the noreorder block as soon as possible, so
7072 that later insns are available for delay slot filling. */
7d10b47d 7073 end_noreorder ();
252b5132 7074
df58fc94 7075 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 7076 }
df58fc94
RS
7077 if (mips_opts.micromips)
7078 micromips_add_label ();
7079 macro_build (NULL, s, MFHL_FMT, dreg);
252b5132
RH
7080 break;
7081
7082 case M_DIV_3I:
7083 s = "div";
7084 s2 = "mflo";
7085 goto do_divi;
7086 case M_DIVU_3I:
7087 s = "divu";
7088 s2 = "mflo";
7089 goto do_divi;
7090 case M_REM_3I:
7091 s = "div";
7092 s2 = "mfhi";
7093 goto do_divi;
7094 case M_REMU_3I:
7095 s = "divu";
7096 s2 = "mfhi";
7097 goto do_divi;
7098 case M_DDIV_3I:
7099 dbl = 1;
7100 s = "ddiv";
7101 s2 = "mflo";
7102 goto do_divi;
7103 case M_DDIVU_3I:
7104 dbl = 1;
7105 s = "ddivu";
7106 s2 = "mflo";
7107 goto do_divi;
7108 case M_DREM_3I:
7109 dbl = 1;
7110 s = "ddiv";
7111 s2 = "mfhi";
7112 goto do_divi;
7113 case M_DREMU_3I:
7114 dbl = 1;
7115 s = "ddivu";
7116 s2 = "mfhi";
7117 do_divi:
7118 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7119 {
7120 as_warn (_("Divide by zero."));
7121 if (mips_trap)
df58fc94 7122 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 7123 else
df58fc94 7124 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 7125 break;
252b5132
RH
7126 }
7127 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7128 {
7129 if (strcmp (s2, "mflo") == 0)
67c0d1eb 7130 move_register (dreg, sreg);
252b5132 7131 else
c80c840e 7132 move_register (dreg, ZERO);
8fc2e39e 7133 break;
252b5132
RH
7134 }
7135 if (imm_expr.X_op == O_constant
7136 && imm_expr.X_add_number == -1
7137 && s[strlen (s) - 1] != 'u')
7138 {
7139 if (strcmp (s2, "mflo") == 0)
7140 {
67c0d1eb 7141 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
7142 }
7143 else
c80c840e 7144 move_register (dreg, ZERO);
8fc2e39e 7145 break;
252b5132
RH
7146 }
7147
8fc2e39e 7148 used_at = 1;
67c0d1eb
RS
7149 load_register (AT, &imm_expr, dbl);
7150 macro_build (NULL, s, "z,s,t", sreg, AT);
df58fc94 7151 macro_build (NULL, s2, MFHL_FMT, dreg);
252b5132
RH
7152 break;
7153
7154 case M_DIVU_3:
7155 s = "divu";
7156 s2 = "mflo";
7157 goto do_divu3;
7158 case M_REMU_3:
7159 s = "divu";
7160 s2 = "mfhi";
7161 goto do_divu3;
7162 case M_DDIVU_3:
7163 s = "ddivu";
7164 s2 = "mflo";
7165 goto do_divu3;
7166 case M_DREMU_3:
7167 s = "ddivu";
7168 s2 = "mfhi";
7169 do_divu3:
7d10b47d 7170 start_noreorder ();
252b5132
RH
7171 if (mips_trap)
7172 {
df58fc94 7173 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 7174 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7175 /* We want to close the noreorder block as soon as possible, so
7176 that later insns are available for delay slot filling. */
7d10b47d 7177 end_noreorder ();
252b5132
RH
7178 }
7179 else
7180 {
df58fc94
RS
7181 if (mips_opts.micromips)
7182 micromips_label_expr (&label_expr);
7183 else
7184 label_expr.X_add_number = 8;
7185 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 7186 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7187
7188 /* We want to close the noreorder block as soon as possible, so
7189 that later insns are available for delay slot filling. */
7d10b47d 7190 end_noreorder ();
df58fc94
RS
7191 macro_build (NULL, "break", BRK_FMT, 7);
7192 if (mips_opts.micromips)
7193 micromips_add_label ();
252b5132 7194 }
df58fc94 7195 macro_build (NULL, s2, MFHL_FMT, dreg);
8fc2e39e 7196 break;
252b5132 7197
1abe91b1
MR
7198 case M_DLCA_AB:
7199 dbl = 1;
7200 case M_LCA_AB:
7201 call = 1;
7202 goto do_la;
252b5132
RH
7203 case M_DLA_AB:
7204 dbl = 1;
7205 case M_LA_AB:
1abe91b1 7206 do_la:
252b5132
RH
7207 /* Load the address of a symbol into a register. If breg is not
7208 zero, we then add a base register to it. */
7209
3bec30a8
TS
7210 if (dbl && HAVE_32BIT_GPRS)
7211 as_warn (_("dla used to load 32-bit register"));
7212
90ecf173 7213 if (!dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
7214 as_warn (_("la used to load 64-bit address"));
7215
0c11417f
MR
7216 if (offset_expr.X_op == O_constant
7217 && offset_expr.X_add_number >= -0x8000
7218 && offset_expr.X_add_number < 0x8000)
7219 {
aed1a261 7220 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 7221 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 7222 break;
0c11417f
MR
7223 }
7224
741fe287 7225 if (mips_opts.at && (treg == breg))
afdbd6d0
CD
7226 {
7227 tempreg = AT;
7228 used_at = 1;
7229 }
7230 else
7231 {
7232 tempreg = treg;
afdbd6d0
CD
7233 }
7234
252b5132
RH
7235 if (offset_expr.X_op != O_symbol
7236 && offset_expr.X_op != O_constant)
7237 {
f71d0d44 7238 as_bad (_("Expression too complex"));
252b5132
RH
7239 offset_expr.X_op = O_constant;
7240 }
7241
252b5132 7242 if (offset_expr.X_op == O_constant)
aed1a261 7243 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
7244 else if (mips_pic == NO_PIC)
7245 {
d6bc6245 7246 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 7247 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
7248 Otherwise we want
7249 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
7250 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7251 If we have a constant, we need two instructions anyhow,
d6bc6245 7252 so we may as well always use the latter form.
76b3015f 7253
6caf9ef4
TS
7254 With 64bit address space and a usable $at we want
7255 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7256 lui $at,<sym> (BFD_RELOC_HI16_S)
7257 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7258 daddiu $at,<sym> (BFD_RELOC_LO16)
7259 dsll32 $tempreg,0
7260 daddu $tempreg,$tempreg,$at
7261
7262 If $at is already in use, we use a path which is suboptimal
7263 on superscalar processors.
7264 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7265 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7266 dsll $tempreg,16
7267 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
7268 dsll $tempreg,16
7269 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
7270
7271 For GP relative symbols in 64bit address space we can use
7272 the same sequence as in 32bit address space. */
aed1a261 7273 if (HAVE_64BIT_SYMBOLS)
252b5132 7274 {
6caf9ef4
TS
7275 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7276 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7277 {
7278 relax_start (offset_expr.X_add_symbol);
7279 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7280 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7281 relax_switch ();
7282 }
d6bc6245 7283
741fe287 7284 if (used_at == 0 && mips_opts.at)
98d3f06f 7285 {
df58fc94 7286 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7287 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 7288 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7289 AT, BFD_RELOC_HI16_S);
67c0d1eb 7290 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7291 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 7292 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7293 AT, AT, BFD_RELOC_LO16);
df58fc94 7294 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 7295 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
7296 used_at = 1;
7297 }
7298 else
7299 {
df58fc94 7300 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7301 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 7302 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7303 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 7304 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7305 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7306 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 7307 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7308 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7309 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 7310 }
6caf9ef4
TS
7311
7312 if (mips_relax.sequence)
7313 relax_end ();
98d3f06f
KH
7314 }
7315 else
7316 {
7317 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 7318 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 7319 {
4d7206a2 7320 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7321 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7322 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 7323 relax_switch ();
98d3f06f 7324 }
6943caf0 7325 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
f71d0d44 7326 as_bad (_("Offset too large"));
67c0d1eb
RS
7327 macro_build_lui (&offset_expr, tempreg);
7328 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7329 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
7330 if (mips_relax.sequence)
7331 relax_end ();
98d3f06f 7332 }
252b5132 7333 }
0a44bf69 7334 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 7335 {
9117d219
NC
7336 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7337
252b5132
RH
7338 /* If this is a reference to an external symbol, and there
7339 is no constant, we want
7340 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 7341 or for lca or if tempreg is PIC_CALL_REG
9117d219 7342 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
7343 For a local symbol, we want
7344 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7345 nop
7346 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7347
7348 If we have a small constant, and this is a reference to
7349 an external symbol, we want
7350 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7351 nop
7352 addiu $tempreg,$tempreg,<constant>
7353 For a local symbol, we want the same instruction
7354 sequence, but we output a BFD_RELOC_LO16 reloc on the
7355 addiu instruction.
7356
7357 If we have a large constant, and this is a reference to
7358 an external symbol, we want
7359 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7360 lui $at,<hiconstant>
7361 addiu $at,$at,<loconstant>
7362 addu $tempreg,$tempreg,$at
7363 For a local symbol, we want the same instruction
7364 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 7365 addiu instruction.
ed6fb7bd
SC
7366 */
7367
4d7206a2 7368 if (offset_expr.X_add_number == 0)
252b5132 7369 {
0a44bf69
RS
7370 if (mips_pic == SVR4_PIC
7371 && breg == 0
7372 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
7373 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7374
7375 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7376 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7377 lw_reloc_type, mips_gp_register);
4d7206a2 7378 if (breg != 0)
252b5132
RH
7379 {
7380 /* We're going to put in an addu instruction using
7381 tempreg, so we may as well insert the nop right
7382 now. */
269137b2 7383 load_delay_nop ();
252b5132 7384 }
4d7206a2 7385 relax_switch ();
67c0d1eb
RS
7386 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7387 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7388 load_delay_nop ();
67c0d1eb
RS
7389 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7390 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 7391 relax_end ();
252b5132
RH
7392 /* FIXME: If breg == 0, and the next instruction uses
7393 $tempreg, then if this variant case is used an extra
7394 nop will be generated. */
7395 }
4d7206a2
RS
7396 else if (offset_expr.X_add_number >= -0x8000
7397 && offset_expr.X_add_number < 0x8000)
252b5132 7398 {
67c0d1eb 7399 load_got_offset (tempreg, &offset_expr);
269137b2 7400 load_delay_nop ();
67c0d1eb 7401 add_got_offset (tempreg, &offset_expr);
252b5132
RH
7402 }
7403 else
7404 {
4d7206a2
RS
7405 expr1.X_add_number = offset_expr.X_add_number;
7406 offset_expr.X_add_number =
43c0598f 7407 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 7408 load_got_offset (tempreg, &offset_expr);
f6a22291 7409 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
7410 /* If we are going to add in a base register, and the
7411 target register and the base register are the same,
7412 then we are using AT as a temporary register. Since
7413 we want to load the constant into AT, we add our
7414 current AT (from the global offset table) and the
7415 register into the register now, and pretend we were
7416 not using a base register. */
67c0d1eb 7417 if (breg == treg)
252b5132 7418 {
269137b2 7419 load_delay_nop ();
67c0d1eb 7420 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7421 treg, AT, breg);
252b5132
RH
7422 breg = 0;
7423 tempreg = treg;
252b5132 7424 }
f6a22291 7425 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
7426 used_at = 1;
7427 }
7428 }
0a44bf69 7429 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 7430 {
67c0d1eb 7431 int add_breg_early = 0;
f5040a92
AO
7432
7433 /* If this is a reference to an external, and there is no
7434 constant, or local symbol (*), with or without a
7435 constant, we want
7436 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 7437 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
7438 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7439
7440 If we have a small constant, and this is a reference to
7441 an external symbol, we want
7442 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7443 addiu $tempreg,$tempreg,<constant>
7444
7445 If we have a large constant, and this is a reference to
7446 an external symbol, we want
7447 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7448 lui $at,<hiconstant>
7449 addiu $at,$at,<loconstant>
7450 addu $tempreg,$tempreg,$at
7451
7452 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7453 local symbols, even though it introduces an additional
7454 instruction. */
7455
f5040a92
AO
7456 if (offset_expr.X_add_number)
7457 {
4d7206a2 7458 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
7459 offset_expr.X_add_number = 0;
7460
4d7206a2 7461 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7462 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7463 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7464
7465 if (expr1.X_add_number >= -0x8000
7466 && expr1.X_add_number < 0x8000)
7467 {
67c0d1eb
RS
7468 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7469 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 7470 }
ecd13cd3 7471 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 7472 {
f5040a92
AO
7473 /* If we are going to add in a base register, and the
7474 target register and the base register are the same,
7475 then we are using AT as a temporary register. Since
7476 we want to load the constant into AT, we add our
7477 current AT (from the global offset table) and the
7478 register into the register now, and pretend we were
7479 not using a base register. */
7480 if (breg != treg)
7481 dreg = tempreg;
7482 else
7483 {
9c2799c2 7484 gas_assert (tempreg == AT);
67c0d1eb
RS
7485 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7486 treg, AT, breg);
f5040a92 7487 dreg = treg;
67c0d1eb 7488 add_breg_early = 1;
f5040a92
AO
7489 }
7490
f6a22291 7491 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7492 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7493 dreg, dreg, AT);
f5040a92 7494
f5040a92
AO
7495 used_at = 1;
7496 }
7497 else
7498 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7499
4d7206a2 7500 relax_switch ();
f5040a92
AO
7501 offset_expr.X_add_number = expr1.X_add_number;
7502
67c0d1eb
RS
7503 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7504 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7505 if (add_breg_early)
f5040a92 7506 {
67c0d1eb 7507 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 7508 treg, tempreg, breg);
f5040a92
AO
7509 breg = 0;
7510 tempreg = treg;
7511 }
4d7206a2 7512 relax_end ();
f5040a92 7513 }
4d7206a2 7514 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 7515 {
4d7206a2 7516 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7517 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7518 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 7519 relax_switch ();
67c0d1eb
RS
7520 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7521 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 7522 relax_end ();
f5040a92 7523 }
4d7206a2 7524 else
f5040a92 7525 {
67c0d1eb
RS
7526 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7527 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7528 }
7529 }
0a44bf69 7530 else if (mips_big_got && !HAVE_NEWABI)
252b5132 7531 {
67c0d1eb 7532 int gpdelay;
9117d219
NC
7533 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7534 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 7535 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
7536
7537 /* This is the large GOT case. If this is a reference to an
7538 external symbol, and there is no constant, we want
7539 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7540 addu $tempreg,$tempreg,$gp
7541 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 7542 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
7543 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7544 addu $tempreg,$tempreg,$gp
7545 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
7546 For a local symbol, we want
7547 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7548 nop
7549 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7550
7551 If we have a small constant, and this is a reference to
7552 an external symbol, we want
7553 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7554 addu $tempreg,$tempreg,$gp
7555 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7556 nop
7557 addiu $tempreg,$tempreg,<constant>
7558 For a local symbol, we want
7559 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7560 nop
7561 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7562
7563 If we have a large constant, and this is a reference to
7564 an external symbol, we want
7565 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7566 addu $tempreg,$tempreg,$gp
7567 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7568 lui $at,<hiconstant>
7569 addiu $at,$at,<loconstant>
7570 addu $tempreg,$tempreg,$at
7571 For a local symbol, we want
7572 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7573 lui $at,<hiconstant>
7574 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
7575 addu $tempreg,$tempreg,$at
f5040a92 7576 */
438c16b8 7577
252b5132
RH
7578 expr1.X_add_number = offset_expr.X_add_number;
7579 offset_expr.X_add_number = 0;
4d7206a2 7580 relax_start (offset_expr.X_add_symbol);
67c0d1eb 7581 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
7582 if (expr1.X_add_number == 0 && breg == 0
7583 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
7584 {
7585 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7586 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7587 }
df58fc94 7588 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 7589 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7590 tempreg, tempreg, mips_gp_register);
67c0d1eb 7591 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 7592 tempreg, lw_reloc_type, tempreg);
252b5132
RH
7593 if (expr1.X_add_number == 0)
7594 {
67c0d1eb 7595 if (breg != 0)
252b5132
RH
7596 {
7597 /* We're going to put in an addu instruction using
7598 tempreg, so we may as well insert the nop right
7599 now. */
269137b2 7600 load_delay_nop ();
252b5132 7601 }
252b5132
RH
7602 }
7603 else if (expr1.X_add_number >= -0x8000
7604 && expr1.X_add_number < 0x8000)
7605 {
269137b2 7606 load_delay_nop ();
67c0d1eb 7607 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7608 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
7609 }
7610 else
7611 {
252b5132
RH
7612 /* If we are going to add in a base register, and the
7613 target register and the base register are the same,
7614 then we are using AT as a temporary register. Since
7615 we want to load the constant into AT, we add our
7616 current AT (from the global offset table) and the
7617 register into the register now, and pretend we were
7618 not using a base register. */
7619 if (breg != treg)
67c0d1eb 7620 dreg = tempreg;
252b5132
RH
7621 else
7622 {
9c2799c2 7623 gas_assert (tempreg == AT);
269137b2 7624 load_delay_nop ();
67c0d1eb 7625 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7626 treg, AT, breg);
252b5132 7627 dreg = treg;
252b5132
RH
7628 }
7629
f6a22291 7630 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7631 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 7632
252b5132
RH
7633 used_at = 1;
7634 }
43c0598f 7635 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 7636 relax_switch ();
252b5132 7637
67c0d1eb 7638 if (gpdelay)
252b5132
RH
7639 {
7640 /* This is needed because this instruction uses $gp, but
f5040a92 7641 the first instruction on the main stream does not. */
67c0d1eb 7642 macro_build (NULL, "nop", "");
252b5132 7643 }
ed6fb7bd 7644
67c0d1eb
RS
7645 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7646 local_reloc_type, mips_gp_register);
f5040a92 7647 if (expr1.X_add_number >= -0x8000
252b5132
RH
7648 && expr1.X_add_number < 0x8000)
7649 {
269137b2 7650 load_delay_nop ();
67c0d1eb
RS
7651 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7652 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 7653 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
7654 register, the external symbol case ended with a load,
7655 so if the symbol turns out to not be external, and
7656 the next instruction uses tempreg, an unnecessary nop
7657 will be inserted. */
252b5132
RH
7658 }
7659 else
7660 {
7661 if (breg == treg)
7662 {
7663 /* We must add in the base register now, as in the
f5040a92 7664 external symbol case. */
9c2799c2 7665 gas_assert (tempreg == AT);
269137b2 7666 load_delay_nop ();
67c0d1eb 7667 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7668 treg, AT, breg);
252b5132
RH
7669 tempreg = treg;
7670 /* We set breg to 0 because we have arranged to add
f5040a92 7671 it in in both cases. */
252b5132
RH
7672 breg = 0;
7673 }
7674
67c0d1eb
RS
7675 macro_build_lui (&expr1, AT);
7676 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7677 AT, AT, BFD_RELOC_LO16);
67c0d1eb 7678 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7679 tempreg, tempreg, AT);
8fc2e39e 7680 used_at = 1;
252b5132 7681 }
4d7206a2 7682 relax_end ();
252b5132 7683 }
0a44bf69 7684 else if (mips_big_got && HAVE_NEWABI)
f5040a92 7685 {
f5040a92
AO
7686 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7687 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 7688 int add_breg_early = 0;
f5040a92
AO
7689
7690 /* This is the large GOT case. If this is a reference to an
7691 external symbol, and there is no constant, we want
7692 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7693 add $tempreg,$tempreg,$gp
7694 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 7695 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
7696 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7697 add $tempreg,$tempreg,$gp
7698 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7699
7700 If we have a small constant, and this is a reference to
7701 an external symbol, we want
7702 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7703 add $tempreg,$tempreg,$gp
7704 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7705 addi $tempreg,$tempreg,<constant>
7706
7707 If we have a large constant, and this is a reference to
7708 an external symbol, we want
7709 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7710 addu $tempreg,$tempreg,$gp
7711 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7712 lui $at,<hiconstant>
7713 addi $at,$at,<loconstant>
7714 add $tempreg,$tempreg,$at
7715
7716 If we have NewABI, and we know it's a local symbol, we want
7717 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
7718 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
7719 otherwise we have to resort to GOT_HI16/GOT_LO16. */
7720
4d7206a2 7721 relax_start (offset_expr.X_add_symbol);
f5040a92 7722
4d7206a2 7723 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
7724 offset_expr.X_add_number = 0;
7725
1abe91b1
MR
7726 if (expr1.X_add_number == 0 && breg == 0
7727 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
7728 {
7729 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7730 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7731 }
df58fc94 7732 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 7733 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7734 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
7735 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7736 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
7737
7738 if (expr1.X_add_number == 0)
4d7206a2 7739 ;
f5040a92
AO
7740 else if (expr1.X_add_number >= -0x8000
7741 && expr1.X_add_number < 0x8000)
7742 {
67c0d1eb 7743 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7744 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 7745 }
ecd13cd3 7746 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 7747 {
f5040a92
AO
7748 /* If we are going to add in a base register, and the
7749 target register and the base register are the same,
7750 then we are using AT as a temporary register. Since
7751 we want to load the constant into AT, we add our
7752 current AT (from the global offset table) and the
7753 register into the register now, and pretend we were
7754 not using a base register. */
7755 if (breg != treg)
7756 dreg = tempreg;
7757 else
7758 {
9c2799c2 7759 gas_assert (tempreg == AT);
67c0d1eb 7760 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7761 treg, AT, breg);
f5040a92 7762 dreg = treg;
67c0d1eb 7763 add_breg_early = 1;
f5040a92
AO
7764 }
7765
f6a22291 7766 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7767 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 7768
f5040a92
AO
7769 used_at = 1;
7770 }
7771 else
7772 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7773
4d7206a2 7774 relax_switch ();
f5040a92 7775 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
7776 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7777 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
7778 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
7779 tempreg, BFD_RELOC_MIPS_GOT_OFST);
7780 if (add_breg_early)
f5040a92 7781 {
67c0d1eb 7782 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7783 treg, tempreg, breg);
f5040a92
AO
7784 breg = 0;
7785 tempreg = treg;
7786 }
4d7206a2 7787 relax_end ();
f5040a92 7788 }
252b5132
RH
7789 else
7790 abort ();
7791
7792 if (breg != 0)
aed1a261 7793 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
7794 break;
7795
52b6b6b9 7796 case M_MSGSND:
df58fc94 7797 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7798 {
7799 unsigned long temp = (treg << 16) | (0x01);
7800 macro_build (NULL, "c2", "C", temp);
7801 }
c7af4273 7802 break;
52b6b6b9
JM
7803
7804 case M_MSGLD:
df58fc94 7805 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7806 {
7807 unsigned long temp = (0x02);
7808 macro_build (NULL, "c2", "C", temp);
7809 }
c7af4273 7810 break;
52b6b6b9
JM
7811
7812 case M_MSGLD_T:
df58fc94 7813 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7814 {
7815 unsigned long temp = (treg << 16) | (0x02);
7816 macro_build (NULL, "c2", "C", temp);
7817 }
c7af4273 7818 break;
52b6b6b9
JM
7819
7820 case M_MSGWAIT:
df58fc94 7821 gas_assert (!mips_opts.micromips);
52b6b6b9 7822 macro_build (NULL, "c2", "C", 3);
c7af4273 7823 break;
52b6b6b9
JM
7824
7825 case M_MSGWAIT_T:
df58fc94 7826 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7827 {
7828 unsigned long temp = (treg << 16) | 0x03;
7829 macro_build (NULL, "c2", "C", temp);
7830 }
c7af4273 7831 break;
52b6b6b9 7832
252b5132
RH
7833 case M_J_A:
7834 /* The j instruction may not be used in PIC code, since it
7835 requires an absolute address. We convert it to a b
7836 instruction. */
7837 if (mips_pic == NO_PIC)
67c0d1eb 7838 macro_build (&offset_expr, "j", "a");
252b5132 7839 else
67c0d1eb 7840 macro_build (&offset_expr, "b", "p");
8fc2e39e 7841 break;
252b5132
RH
7842
7843 /* The jal instructions must be handled as macros because when
7844 generating PIC code they expand to multi-instruction
7845 sequences. Normally they are simple instructions. */
df58fc94
RS
7846 case M_JALS_1:
7847 dreg = RA;
7848 /* Fall through. */
7849 case M_JALS_2:
7850 gas_assert (mips_opts.micromips);
7851 jals = 1;
7852 goto jal;
252b5132
RH
7853 case M_JAL_1:
7854 dreg = RA;
7855 /* Fall through. */
7856 case M_JAL_2:
df58fc94 7857 jal:
3e722fb5 7858 if (mips_pic == NO_PIC)
df58fc94
RS
7859 {
7860 s = jals ? "jalrs" : "jalr";
e64af278
MR
7861 if (mips_opts.micromips
7862 && dreg == RA
7863 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
7864 macro_build (NULL, s, "mj", sreg);
7865 else
7866 macro_build (NULL, s, JALR_FMT, dreg, sreg);
7867 }
0a44bf69 7868 else
252b5132 7869 {
df58fc94
RS
7870 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
7871 && mips_cprestore_offset >= 0);
7872
252b5132
RH
7873 if (sreg != PIC_CALL_REG)
7874 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 7875
df58fc94
RS
7876 s = (mips_opts.micromips && (!mips_opts.noreorder || cprestore)
7877 ? "jalrs" : "jalr");
e64af278
MR
7878 if (mips_opts.micromips
7879 && dreg == RA
7880 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
7881 macro_build (NULL, s, "mj", sreg);
7882 else
7883 macro_build (NULL, s, JALR_FMT, dreg, sreg);
0a44bf69 7884 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 7885 {
6478892d
TS
7886 if (mips_cprestore_offset < 0)
7887 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7888 else
7889 {
90ecf173 7890 if (!mips_frame_reg_valid)
7a621144
DJ
7891 {
7892 as_warn (_("No .frame pseudo-op used in PIC code"));
7893 /* Quiet this warning. */
7894 mips_frame_reg_valid = 1;
7895 }
90ecf173 7896 if (!mips_cprestore_valid)
7a621144
DJ
7897 {
7898 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7899 /* Quiet this warning. */
7900 mips_cprestore_valid = 1;
7901 }
d3fca0b5
MR
7902 if (mips_opts.noreorder)
7903 macro_build (NULL, "nop", "");
6478892d 7904 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 7905 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 7906 mips_gp_register,
256ab948
TS
7907 mips_frame_reg,
7908 HAVE_64BIT_ADDRESSES);
6478892d 7909 }
252b5132
RH
7910 }
7911 }
252b5132 7912
8fc2e39e 7913 break;
252b5132 7914
df58fc94
RS
7915 case M_JALS_A:
7916 gas_assert (mips_opts.micromips);
7917 jals = 1;
7918 /* Fall through. */
252b5132
RH
7919 case M_JAL_A:
7920 if (mips_pic == NO_PIC)
df58fc94 7921 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
7922 else if (mips_pic == SVR4_PIC)
7923 {
7924 /* If this is a reference to an external symbol, and we are
7925 using a small GOT, we want
7926 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7927 nop
f9419b05 7928 jalr $ra,$25
252b5132
RH
7929 nop
7930 lw $gp,cprestore($sp)
7931 The cprestore value is set using the .cprestore
7932 pseudo-op. If we are using a big GOT, we want
7933 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7934 addu $25,$25,$gp
7935 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
7936 nop
f9419b05 7937 jalr $ra,$25
252b5132
RH
7938 nop
7939 lw $gp,cprestore($sp)
7940 If the symbol is not external, we want
7941 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7942 nop
7943 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 7944 jalr $ra,$25
252b5132 7945 nop
438c16b8 7946 lw $gp,cprestore($sp)
f5040a92
AO
7947
7948 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
7949 sequences above, minus nops, unless the symbol is local,
7950 which enables us to use GOT_PAGE/GOT_OFST (big got) or
7951 GOT_DISP. */
438c16b8 7952 if (HAVE_NEWABI)
252b5132 7953 {
90ecf173 7954 if (!mips_big_got)
f5040a92 7955 {
4d7206a2 7956 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7957 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7958 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 7959 mips_gp_register);
4d7206a2 7960 relax_switch ();
67c0d1eb
RS
7961 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7962 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
7963 mips_gp_register);
7964 relax_end ();
f5040a92
AO
7965 }
7966 else
7967 {
4d7206a2 7968 relax_start (offset_expr.X_add_symbol);
df58fc94 7969 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
7970 BFD_RELOC_MIPS_CALL_HI16);
7971 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
7972 PIC_CALL_REG, mips_gp_register);
7973 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7974 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
7975 PIC_CALL_REG);
4d7206a2 7976 relax_switch ();
67c0d1eb
RS
7977 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7978 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
7979 mips_gp_register);
7980 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7981 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 7982 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 7983 relax_end ();
f5040a92 7984 }
684022ea 7985
df58fc94 7986 macro_build_jalr (&offset_expr, 0);
252b5132
RH
7987 }
7988 else
7989 {
4d7206a2 7990 relax_start (offset_expr.X_add_symbol);
90ecf173 7991 if (!mips_big_got)
438c16b8 7992 {
67c0d1eb
RS
7993 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7994 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 7995 mips_gp_register);
269137b2 7996 load_delay_nop ();
4d7206a2 7997 relax_switch ();
438c16b8 7998 }
252b5132 7999 else
252b5132 8000 {
67c0d1eb
RS
8001 int gpdelay;
8002
8003 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 8004 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
8005 BFD_RELOC_MIPS_CALL_HI16);
8006 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8007 PIC_CALL_REG, mips_gp_register);
8008 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8009 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8010 PIC_CALL_REG);
269137b2 8011 load_delay_nop ();
4d7206a2 8012 relax_switch ();
67c0d1eb
RS
8013 if (gpdelay)
8014 macro_build (NULL, "nop", "");
252b5132 8015 }
67c0d1eb
RS
8016 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8017 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 8018 mips_gp_register);
269137b2 8019 load_delay_nop ();
67c0d1eb
RS
8020 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8021 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 8022 relax_end ();
df58fc94 8023 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 8024
6478892d
TS
8025 if (mips_cprestore_offset < 0)
8026 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8027 else
8028 {
90ecf173 8029 if (!mips_frame_reg_valid)
7a621144
DJ
8030 {
8031 as_warn (_("No .frame pseudo-op used in PIC code"));
8032 /* Quiet this warning. */
8033 mips_frame_reg_valid = 1;
8034 }
90ecf173 8035 if (!mips_cprestore_valid)
7a621144
DJ
8036 {
8037 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8038 /* Quiet this warning. */
8039 mips_cprestore_valid = 1;
8040 }
6478892d 8041 if (mips_opts.noreorder)
67c0d1eb 8042 macro_build (NULL, "nop", "");
6478892d 8043 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 8044 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 8045 mips_gp_register,
256ab948
TS
8046 mips_frame_reg,
8047 HAVE_64BIT_ADDRESSES);
6478892d 8048 }
252b5132
RH
8049 }
8050 }
0a44bf69
RS
8051 else if (mips_pic == VXWORKS_PIC)
8052 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
8053 else
8054 abort ();
8055
8fc2e39e 8056 break;
252b5132 8057
dec0624d
MR
8058 case M_ACLR_AB:
8059 ab = 1;
8060 case M_ACLR_OB:
8061 s = "aclr";
8062 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8063 fmt = "\\,~(b)";
8064 off12 = 1;
8065 goto ld_st;
8066 case M_ASET_AB:
8067 ab = 1;
8068 case M_ASET_OB:
8069 s = "aset";
8070 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8071 fmt = "\\,~(b)";
8072 off12 = 1;
8073 goto ld_st;
252b5132 8074 case M_LB_AB:
df58fc94 8075 ab = 1;
252b5132 8076 s = "lb";
df58fc94 8077 fmt = "t,o(b)";
252b5132
RH
8078 goto ld;
8079 case M_LBU_AB:
df58fc94 8080 ab = 1;
252b5132 8081 s = "lbu";
df58fc94 8082 fmt = "t,o(b)";
252b5132
RH
8083 goto ld;
8084 case M_LH_AB:
df58fc94 8085 ab = 1;
252b5132 8086 s = "lh";
df58fc94 8087 fmt = "t,o(b)";
252b5132
RH
8088 goto ld;
8089 case M_LHU_AB:
df58fc94 8090 ab = 1;
252b5132 8091 s = "lhu";
df58fc94 8092 fmt = "t,o(b)";
252b5132
RH
8093 goto ld;
8094 case M_LW_AB:
df58fc94 8095 ab = 1;
252b5132 8096 s = "lw";
df58fc94 8097 fmt = "t,o(b)";
252b5132
RH
8098 goto ld;
8099 case M_LWC0_AB:
df58fc94
RS
8100 ab = 1;
8101 gas_assert (!mips_opts.micromips);
252b5132 8102 s = "lwc0";
df58fc94 8103 fmt = "E,o(b)";
bdaaa2e1 8104 /* Itbl support may require additional care here. */
252b5132 8105 coproc = 1;
df58fc94 8106 goto ld_st;
252b5132 8107 case M_LWC1_AB:
df58fc94 8108 ab = 1;
252b5132 8109 s = "lwc1";
df58fc94 8110 fmt = "T,o(b)";
bdaaa2e1 8111 /* Itbl support may require additional care here. */
252b5132 8112 coproc = 1;
df58fc94 8113 goto ld_st;
252b5132 8114 case M_LWC2_AB:
df58fc94
RS
8115 ab = 1;
8116 case M_LWC2_OB:
252b5132 8117 s = "lwc2";
df58fc94
RS
8118 fmt = COP12_FMT;
8119 off12 = mips_opts.micromips;
bdaaa2e1 8120 /* Itbl support may require additional care here. */
252b5132 8121 coproc = 1;
df58fc94 8122 goto ld_st;
252b5132 8123 case M_LWC3_AB:
df58fc94
RS
8124 ab = 1;
8125 gas_assert (!mips_opts.micromips);
252b5132 8126 s = "lwc3";
df58fc94 8127 fmt = "E,o(b)";
bdaaa2e1 8128 /* Itbl support may require additional care here. */
252b5132 8129 coproc = 1;
df58fc94 8130 goto ld_st;
252b5132 8131 case M_LWL_AB:
df58fc94
RS
8132 ab = 1;
8133 case M_LWL_OB:
252b5132 8134 s = "lwl";
df58fc94
RS
8135 fmt = MEM12_FMT;
8136 off12 = mips_opts.micromips;
8137 goto ld_st;
252b5132 8138 case M_LWR_AB:
df58fc94
RS
8139 ab = 1;
8140 case M_LWR_OB:
252b5132 8141 s = "lwr";
df58fc94
RS
8142 fmt = MEM12_FMT;
8143 off12 = mips_opts.micromips;
8144 goto ld_st;
252b5132 8145 case M_LDC1_AB:
df58fc94 8146 ab = 1;
252b5132 8147 s = "ldc1";
df58fc94 8148 fmt = "T,o(b)";
bdaaa2e1 8149 /* Itbl support may require additional care here. */
252b5132 8150 coproc = 1;
df58fc94 8151 goto ld_st;
252b5132 8152 case M_LDC2_AB:
df58fc94
RS
8153 ab = 1;
8154 case M_LDC2_OB:
252b5132 8155 s = "ldc2";
df58fc94
RS
8156 fmt = COP12_FMT;
8157 off12 = mips_opts.micromips;
bdaaa2e1 8158 /* Itbl support may require additional care here. */
252b5132 8159 coproc = 1;
df58fc94 8160 goto ld_st;
252b5132 8161 case M_LDC3_AB:
df58fc94 8162 ab = 1;
252b5132 8163 s = "ldc3";
df58fc94 8164 fmt = "E,o(b)";
bdaaa2e1 8165 /* Itbl support may require additional care here. */
252b5132 8166 coproc = 1;
df58fc94 8167 goto ld_st;
252b5132 8168 case M_LDL_AB:
df58fc94
RS
8169 ab = 1;
8170 case M_LDL_OB:
252b5132 8171 s = "ldl";
df58fc94
RS
8172 fmt = MEM12_FMT;
8173 off12 = mips_opts.micromips;
8174 goto ld_st;
252b5132 8175 case M_LDR_AB:
df58fc94
RS
8176 ab = 1;
8177 case M_LDR_OB:
252b5132 8178 s = "ldr";
df58fc94
RS
8179 fmt = MEM12_FMT;
8180 off12 = mips_opts.micromips;
8181 goto ld_st;
252b5132 8182 case M_LL_AB:
df58fc94
RS
8183 ab = 1;
8184 case M_LL_OB:
252b5132 8185 s = "ll";
df58fc94
RS
8186 fmt = MEM12_FMT;
8187 off12 = mips_opts.micromips;
252b5132
RH
8188 goto ld;
8189 case M_LLD_AB:
df58fc94
RS
8190 ab = 1;
8191 case M_LLD_OB:
252b5132 8192 s = "lld";
df58fc94
RS
8193 fmt = MEM12_FMT;
8194 off12 = mips_opts.micromips;
252b5132
RH
8195 goto ld;
8196 case M_LWU_AB:
df58fc94
RS
8197 ab = 1;
8198 case M_LWU_OB:
252b5132 8199 s = "lwu";
df58fc94
RS
8200 fmt = MEM12_FMT;
8201 off12 = mips_opts.micromips;
8202 goto ld;
8203 case M_LWP_AB:
8204 ab = 1;
8205 case M_LWP_OB:
8206 gas_assert (mips_opts.micromips);
8207 s = "lwp";
8208 fmt = "t,~(b)";
8209 off12 = 1;
8210 lp = 1;
8211 goto ld;
8212 case M_LDP_AB:
8213 ab = 1;
8214 case M_LDP_OB:
8215 gas_assert (mips_opts.micromips);
8216 s = "ldp";
8217 fmt = "t,~(b)";
8218 off12 = 1;
8219 lp = 1;
8220 goto ld;
8221 case M_LWM_AB:
8222 ab = 1;
8223 case M_LWM_OB:
8224 gas_assert (mips_opts.micromips);
8225 s = "lwm";
8226 fmt = "n,~(b)";
8227 off12 = 1;
8228 goto ld_st;
8229 case M_LDM_AB:
8230 ab = 1;
8231 case M_LDM_OB:
8232 gas_assert (mips_opts.micromips);
8233 s = "ldm";
8234 fmt = "n,~(b)";
8235 off12 = 1;
8236 goto ld_st;
8237
252b5132 8238 ld:
df58fc94
RS
8239 if (breg == treg + lp)
8240 goto ld_st;
252b5132 8241 else
df58fc94
RS
8242 tempreg = treg + lp;
8243 goto ld_noat;
8244
252b5132 8245 case M_SB_AB:
df58fc94 8246 ab = 1;
252b5132 8247 s = "sb";
df58fc94
RS
8248 fmt = "t,o(b)";
8249 goto ld_st;
252b5132 8250 case M_SH_AB:
df58fc94 8251 ab = 1;
252b5132 8252 s = "sh";
df58fc94
RS
8253 fmt = "t,o(b)";
8254 goto ld_st;
252b5132 8255 case M_SW_AB:
df58fc94 8256 ab = 1;
252b5132 8257 s = "sw";
df58fc94
RS
8258 fmt = "t,o(b)";
8259 goto ld_st;
252b5132 8260 case M_SWC0_AB:
df58fc94
RS
8261 ab = 1;
8262 gas_assert (!mips_opts.micromips);
252b5132 8263 s = "swc0";
df58fc94 8264 fmt = "E,o(b)";
bdaaa2e1 8265 /* Itbl support may require additional care here. */
252b5132 8266 coproc = 1;
df58fc94 8267 goto ld_st;
252b5132 8268 case M_SWC1_AB:
df58fc94 8269 ab = 1;
252b5132 8270 s = "swc1";
df58fc94 8271 fmt = "T,o(b)";
bdaaa2e1 8272 /* Itbl support may require additional care here. */
252b5132 8273 coproc = 1;
df58fc94 8274 goto ld_st;
252b5132 8275 case M_SWC2_AB:
df58fc94
RS
8276 ab = 1;
8277 case M_SWC2_OB:
252b5132 8278 s = "swc2";
df58fc94
RS
8279 fmt = COP12_FMT;
8280 off12 = mips_opts.micromips;
bdaaa2e1 8281 /* Itbl support may require additional care here. */
252b5132 8282 coproc = 1;
df58fc94 8283 goto ld_st;
252b5132 8284 case M_SWC3_AB:
df58fc94
RS
8285 ab = 1;
8286 gas_assert (!mips_opts.micromips);
252b5132 8287 s = "swc3";
df58fc94 8288 fmt = "E,o(b)";
bdaaa2e1 8289 /* Itbl support may require additional care here. */
252b5132 8290 coproc = 1;
df58fc94 8291 goto ld_st;
252b5132 8292 case M_SWL_AB:
df58fc94
RS
8293 ab = 1;
8294 case M_SWL_OB:
252b5132 8295 s = "swl";
df58fc94
RS
8296 fmt = MEM12_FMT;
8297 off12 = mips_opts.micromips;
8298 goto ld_st;
252b5132 8299 case M_SWR_AB:
df58fc94
RS
8300 ab = 1;
8301 case M_SWR_OB:
252b5132 8302 s = "swr";
df58fc94
RS
8303 fmt = MEM12_FMT;
8304 off12 = mips_opts.micromips;
8305 goto ld_st;
252b5132 8306 case M_SC_AB:
df58fc94
RS
8307 ab = 1;
8308 case M_SC_OB:
252b5132 8309 s = "sc";
df58fc94
RS
8310 fmt = MEM12_FMT;
8311 off12 = mips_opts.micromips;
8312 goto ld_st;
252b5132 8313 case M_SCD_AB:
df58fc94
RS
8314 ab = 1;
8315 case M_SCD_OB:
252b5132 8316 s = "scd";
df58fc94
RS
8317 fmt = MEM12_FMT;
8318 off12 = mips_opts.micromips;
8319 goto ld_st;
d43b4baf 8320 case M_CACHE_AB:
df58fc94
RS
8321 ab = 1;
8322 case M_CACHE_OB:
d43b4baf 8323 s = "cache";
df58fc94
RS
8324 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
8325 off12 = mips_opts.micromips;
8326 goto ld_st;
3eebd5eb 8327 case M_PREF_AB:
df58fc94
RS
8328 ab = 1;
8329 case M_PREF_OB:
3eebd5eb 8330 s = "pref";
df58fc94
RS
8331 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
8332 off12 = mips_opts.micromips;
8333 goto ld_st;
252b5132 8334 case M_SDC1_AB:
df58fc94 8335 ab = 1;
252b5132 8336 s = "sdc1";
df58fc94 8337 fmt = "T,o(b)";
252b5132 8338 coproc = 1;
bdaaa2e1 8339 /* Itbl support may require additional care here. */
df58fc94 8340 goto ld_st;
252b5132 8341 case M_SDC2_AB:
df58fc94
RS
8342 ab = 1;
8343 case M_SDC2_OB:
252b5132 8344 s = "sdc2";
df58fc94
RS
8345 fmt = COP12_FMT;
8346 off12 = mips_opts.micromips;
bdaaa2e1 8347 /* Itbl support may require additional care here. */
252b5132 8348 coproc = 1;
df58fc94 8349 goto ld_st;
252b5132 8350 case M_SDC3_AB:
df58fc94
RS
8351 ab = 1;
8352 gas_assert (!mips_opts.micromips);
252b5132 8353 s = "sdc3";
df58fc94 8354 fmt = "E,o(b)";
bdaaa2e1 8355 /* Itbl support may require additional care here. */
252b5132 8356 coproc = 1;
df58fc94 8357 goto ld_st;
252b5132 8358 case M_SDL_AB:
df58fc94
RS
8359 ab = 1;
8360 case M_SDL_OB:
252b5132 8361 s = "sdl";
df58fc94
RS
8362 fmt = MEM12_FMT;
8363 off12 = mips_opts.micromips;
8364 goto ld_st;
252b5132 8365 case M_SDR_AB:
df58fc94
RS
8366 ab = 1;
8367 case M_SDR_OB:
252b5132 8368 s = "sdr";
df58fc94
RS
8369 fmt = MEM12_FMT;
8370 off12 = mips_opts.micromips;
8371 goto ld_st;
8372 case M_SWP_AB:
8373 ab = 1;
8374 case M_SWP_OB:
8375 gas_assert (mips_opts.micromips);
8376 s = "swp";
8377 fmt = "t,~(b)";
8378 off12 = 1;
8379 goto ld_st;
8380 case M_SDP_AB:
8381 ab = 1;
8382 case M_SDP_OB:
8383 gas_assert (mips_opts.micromips);
8384 s = "sdp";
8385 fmt = "t,~(b)";
8386 off12 = 1;
8387 goto ld_st;
8388 case M_SWM_AB:
8389 ab = 1;
8390 case M_SWM_OB:
8391 gas_assert (mips_opts.micromips);
8392 s = "swm";
8393 fmt = "n,~(b)";
8394 off12 = 1;
8395 goto ld_st;
8396 case M_SDM_AB:
8397 ab = 1;
8398 case M_SDM_OB:
8399 gas_assert (mips_opts.micromips);
8400 s = "sdm";
8401 fmt = "n,~(b)";
8402 off12 = 1;
8403
8404 ld_st:
8fc2e39e
TS
8405 tempreg = AT;
8406 used_at = 1;
df58fc94 8407 ld_noat:
252b5132
RH
8408 if (offset_expr.X_op != O_constant
8409 && offset_expr.X_op != O_symbol)
8410 {
f71d0d44 8411 as_bad (_("Expression too complex"));
252b5132
RH
8412 offset_expr.X_op = O_constant;
8413 }
8414
2051e8c4
MR
8415 if (HAVE_32BIT_ADDRESSES
8416 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
8417 {
8418 char value [32];
8419
8420 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 8421 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 8422 }
2051e8c4 8423
252b5132
RH
8424 /* A constant expression in PIC code can be handled just as it
8425 is in non PIC code. */
aed1a261
RS
8426 if (offset_expr.X_op == O_constant)
8427 {
df58fc94
RS
8428 int hipart = 0;
8429
842f8b2a 8430 expr1.X_add_number = offset_expr.X_add_number;
2051e8c4 8431 normalize_address_expr (&expr1);
df58fc94 8432 if (!off12 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
842f8b2a
MR
8433 {
8434 expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8435 & ~(bfd_vma) 0xffff);
df58fc94
RS
8436 hipart = 1;
8437 }
8438 else if (off12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
8439 {
8440 expr1.X_add_number = ((expr1.X_add_number + 0x800)
8441 & ~(bfd_vma) 0xfff);
8442 hipart = 1;
8443 }
8444 if (hipart)
8445 {
842f8b2a
MR
8446 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8447 if (breg != 0)
8448 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8449 tempreg, tempreg, breg);
8450 breg = tempreg;
8451 }
dd6a37e7
AP
8452 if (off0)
8453 {
8454 if (offset_expr.X_add_number == 0)
8455 tempreg = breg;
8456 else
8457 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8458 "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8459 macro_build (NULL, s, fmt, treg, tempreg);
8460 }
8461 else if (!off12)
df58fc94
RS
8462 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8463 else
8464 macro_build (NULL, s, fmt,
8465 treg, (unsigned long) offset_expr.X_add_number, breg);
8466 }
dd6a37e7 8467 else if (off12 || off0)
df58fc94 8468 {
dd6a37e7
AP
8469 /* A 12-bit or 0-bit offset field is too narrow to be used
8470 for a low-part relocation, so load the whole address into
8471 the auxillary register. In the case of "A(b)" addresses,
8472 we first load absolute address "A" into the register and
8473 then add base register "b". In the case of "o(b)" addresses,
8474 we simply need to add 16-bit offset "o" to base register "b", and
df58fc94
RS
8475 offset_reloc already contains the relocations associated
8476 with "o". */
8477 if (ab)
8478 {
8479 load_address (tempreg, &offset_expr, &used_at);
8480 if (breg != 0)
8481 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8482 tempreg, tempreg, breg);
8483 }
8484 else
8485 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8486 tempreg, breg, -1,
8487 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8488 expr1.X_add_number = 0;
dd6a37e7
AP
8489 if (off0)
8490 macro_build (NULL, s, fmt, treg, tempreg);
8491 else
8492 macro_build (NULL, s, fmt,
8493 treg, (unsigned long) expr1.X_add_number, tempreg);
aed1a261
RS
8494 }
8495 else if (mips_pic == NO_PIC)
252b5132
RH
8496 {
8497 /* If this is a reference to a GP relative symbol, and there
8498 is no base register, we want
cdf6fd85 8499 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
8500 Otherwise, if there is no base register, we want
8501 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8502 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8503 If we have a constant, we need two instructions anyhow,
8504 so we always use the latter form.
8505
8506 If we have a base register, and this is a reference to a
8507 GP relative symbol, we want
8508 addu $tempreg,$breg,$gp
cdf6fd85 8509 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
8510 Otherwise we want
8511 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8512 addu $tempreg,$tempreg,$breg
8513 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 8514 With a constant we always use the latter case.
76b3015f 8515
d6bc6245
TS
8516 With 64bit address space and no base register and $at usable,
8517 we want
8518 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8519 lui $at,<sym> (BFD_RELOC_HI16_S)
8520 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8521 dsll32 $tempreg,0
8522 daddu $tempreg,$at
8523 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8524 If we have a base register, we want
8525 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8526 lui $at,<sym> (BFD_RELOC_HI16_S)
8527 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8528 daddu $at,$breg
8529 dsll32 $tempreg,0
8530 daddu $tempreg,$at
8531 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8532
8533 Without $at we can't generate the optimal path for superscalar
8534 processors here since this would require two temporary registers.
8535 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8536 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8537 dsll $tempreg,16
8538 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8539 dsll $tempreg,16
8540 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8541 If we have a base register, we want
8542 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8543 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8544 dsll $tempreg,16
8545 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8546 dsll $tempreg,16
8547 daddu $tempreg,$tempreg,$breg
8548 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 8549
6caf9ef4 8550 For GP relative symbols in 64bit address space we can use
aed1a261
RS
8551 the same sequence as in 32bit address space. */
8552 if (HAVE_64BIT_SYMBOLS)
d6bc6245 8553 {
aed1a261 8554 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
8555 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8556 {
8557 relax_start (offset_expr.X_add_symbol);
8558 if (breg == 0)
8559 {
8560 macro_build (&offset_expr, s, fmt, treg,
8561 BFD_RELOC_GPREL16, mips_gp_register);
8562 }
8563 else
8564 {
8565 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8566 tempreg, breg, mips_gp_register);
8567 macro_build (&offset_expr, s, fmt, treg,
8568 BFD_RELOC_GPREL16, tempreg);
8569 }
8570 relax_switch ();
8571 }
d6bc6245 8572
741fe287 8573 if (used_at == 0 && mips_opts.at)
d6bc6245 8574 {
df58fc94 8575 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 8576 BFD_RELOC_MIPS_HIGHEST);
df58fc94 8577 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
8578 BFD_RELOC_HI16_S);
8579 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8580 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 8581 if (breg != 0)
67c0d1eb 8582 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 8583 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb
RS
8584 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
8585 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
8586 tempreg);
d6bc6245
TS
8587 used_at = 1;
8588 }
8589 else
8590 {
df58fc94 8591 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
8592 BFD_RELOC_MIPS_HIGHEST);
8593 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8594 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 8595 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
8596 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8597 tempreg, BFD_RELOC_HI16_S);
df58fc94 8598 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 8599 if (breg != 0)
67c0d1eb 8600 macro_build (NULL, "daddu", "d,v,t",
17a2f251 8601 tempreg, tempreg, breg);
67c0d1eb 8602 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8603 BFD_RELOC_LO16, tempreg);
d6bc6245 8604 }
6caf9ef4
TS
8605
8606 if (mips_relax.sequence)
8607 relax_end ();
8fc2e39e 8608 break;
d6bc6245 8609 }
256ab948 8610
252b5132
RH
8611 if (breg == 0)
8612 {
67c0d1eb 8613 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8614 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 8615 {
4d7206a2 8616 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
8617 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
8618 mips_gp_register);
4d7206a2 8619 relax_switch ();
252b5132 8620 }
67c0d1eb
RS
8621 macro_build_lui (&offset_expr, tempreg);
8622 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8623 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
8624 if (mips_relax.sequence)
8625 relax_end ();
252b5132
RH
8626 }
8627 else
8628 {
67c0d1eb 8629 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8630 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 8631 {
4d7206a2 8632 relax_start (offset_expr.X_add_symbol);
67c0d1eb 8633 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8634 tempreg, breg, mips_gp_register);
67c0d1eb 8635 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8636 BFD_RELOC_GPREL16, tempreg);
4d7206a2 8637 relax_switch ();
252b5132 8638 }
67c0d1eb
RS
8639 macro_build_lui (&offset_expr, tempreg);
8640 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8641 tempreg, tempreg, breg);
67c0d1eb 8642 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8643 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
8644 if (mips_relax.sequence)
8645 relax_end ();
252b5132
RH
8646 }
8647 }
0a44bf69 8648 else if (!mips_big_got)
252b5132 8649 {
ed6fb7bd 8650 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 8651
252b5132
RH
8652 /* If this is a reference to an external symbol, we want
8653 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8654 nop
8655 <op> $treg,0($tempreg)
8656 Otherwise we want
8657 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8658 nop
8659 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8660 <op> $treg,0($tempreg)
f5040a92
AO
8661
8662 For NewABI, we want
8663 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8664 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
8665
252b5132
RH
8666 If there is a base register, we add it to $tempreg before
8667 the <op>. If there is a constant, we stick it in the
8668 <op> instruction. We don't handle constants larger than
8669 16 bits, because we have no way to load the upper 16 bits
8670 (actually, we could handle them for the subset of cases
8671 in which we are not using $at). */
9c2799c2 8672 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
8673 if (HAVE_NEWABI)
8674 {
67c0d1eb
RS
8675 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8676 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 8677 if (breg != 0)
67c0d1eb 8678 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8679 tempreg, tempreg, breg);
67c0d1eb 8680 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8681 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
8682 break;
8683 }
252b5132
RH
8684 expr1.X_add_number = offset_expr.X_add_number;
8685 offset_expr.X_add_number = 0;
8686 if (expr1.X_add_number < -0x8000
8687 || expr1.X_add_number >= 0x8000)
8688 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
8689 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8690 lw_reloc_type, mips_gp_register);
269137b2 8691 load_delay_nop ();
4d7206a2
RS
8692 relax_start (offset_expr.X_add_symbol);
8693 relax_switch ();
67c0d1eb
RS
8694 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8695 tempreg, BFD_RELOC_LO16);
4d7206a2 8696 relax_end ();
252b5132 8697 if (breg != 0)
67c0d1eb 8698 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8699 tempreg, tempreg, breg);
67c0d1eb 8700 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 8701 }
0a44bf69 8702 else if (mips_big_got && !HAVE_NEWABI)
252b5132 8703 {
67c0d1eb 8704 int gpdelay;
252b5132
RH
8705
8706 /* If this is a reference to an external symbol, we want
8707 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8708 addu $tempreg,$tempreg,$gp
8709 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8710 <op> $treg,0($tempreg)
8711 Otherwise we want
8712 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8713 nop
8714 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8715 <op> $treg,0($tempreg)
8716 If there is a base register, we add it to $tempreg before
8717 the <op>. If there is a constant, we stick it in the
8718 <op> instruction. We don't handle constants larger than
8719 16 bits, because we have no way to load the upper 16 bits
8720 (actually, we could handle them for the subset of cases
f5040a92 8721 in which we are not using $at). */
9c2799c2 8722 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
8723 expr1.X_add_number = offset_expr.X_add_number;
8724 offset_expr.X_add_number = 0;
8725 if (expr1.X_add_number < -0x8000
8726 || expr1.X_add_number >= 0x8000)
8727 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 8728 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 8729 relax_start (offset_expr.X_add_symbol);
df58fc94 8730 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 8731 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8732 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8733 mips_gp_register);
8734 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8735 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 8736 relax_switch ();
67c0d1eb
RS
8737 if (gpdelay)
8738 macro_build (NULL, "nop", "");
8739 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8740 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 8741 load_delay_nop ();
67c0d1eb
RS
8742 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8743 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
8744 relax_end ();
8745
252b5132 8746 if (breg != 0)
67c0d1eb 8747 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8748 tempreg, tempreg, breg);
67c0d1eb 8749 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 8750 }
0a44bf69 8751 else if (mips_big_got && HAVE_NEWABI)
f5040a92 8752 {
f5040a92
AO
8753 /* If this is a reference to an external symbol, we want
8754 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8755 add $tempreg,$tempreg,$gp
8756 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8757 <op> $treg,<ofst>($tempreg)
8758 Otherwise, for local symbols, we want:
8759 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8760 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 8761 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 8762 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
8763 offset_expr.X_add_number = 0;
8764 if (expr1.X_add_number < -0x8000
8765 || expr1.X_add_number >= 0x8000)
8766 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 8767 relax_start (offset_expr.X_add_symbol);
df58fc94 8768 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 8769 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8770 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8771 mips_gp_register);
8772 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8773 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 8774 if (breg != 0)
67c0d1eb 8775 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8776 tempreg, tempreg, breg);
67c0d1eb 8777 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 8778
4d7206a2 8779 relax_switch ();
f5040a92 8780 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
8781 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8782 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 8783 if (breg != 0)
67c0d1eb 8784 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8785 tempreg, tempreg, breg);
67c0d1eb 8786 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8787 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 8788 relax_end ();
f5040a92 8789 }
252b5132
RH
8790 else
8791 abort ();
8792
252b5132
RH
8793 break;
8794
8795 case M_LI:
8796 case M_LI_S:
67c0d1eb 8797 load_register (treg, &imm_expr, 0);
8fc2e39e 8798 break;
252b5132
RH
8799
8800 case M_DLI:
67c0d1eb 8801 load_register (treg, &imm_expr, 1);
8fc2e39e 8802 break;
252b5132
RH
8803
8804 case M_LI_SS:
8805 if (imm_expr.X_op == O_constant)
8806 {
8fc2e39e 8807 used_at = 1;
67c0d1eb
RS
8808 load_register (AT, &imm_expr, 0);
8809 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
8810 break;
8811 }
8812 else
8813 {
9c2799c2 8814 gas_assert (offset_expr.X_op == O_symbol
90ecf173
MR
8815 && strcmp (segment_name (S_GET_SEGMENT
8816 (offset_expr.X_add_symbol)),
8817 ".lit4") == 0
8818 && offset_expr.X_add_number == 0);
67c0d1eb 8819 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 8820 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 8821 break;
252b5132
RH
8822 }
8823
8824 case M_LI_D:
ca4e0257
RS
8825 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
8826 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
8827 order 32 bits of the value and the low order 32 bits are either
8828 zero or in OFFSET_EXPR. */
252b5132
RH
8829 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8830 {
ca4e0257 8831 if (HAVE_64BIT_GPRS)
67c0d1eb 8832 load_register (treg, &imm_expr, 1);
252b5132
RH
8833 else
8834 {
8835 int hreg, lreg;
8836
8837 if (target_big_endian)
8838 {
8839 hreg = treg;
8840 lreg = treg + 1;
8841 }
8842 else
8843 {
8844 hreg = treg + 1;
8845 lreg = treg;
8846 }
8847
8848 if (hreg <= 31)
67c0d1eb 8849 load_register (hreg, &imm_expr, 0);
252b5132
RH
8850 if (lreg <= 31)
8851 {
8852 if (offset_expr.X_op == O_absent)
67c0d1eb 8853 move_register (lreg, 0);
252b5132
RH
8854 else
8855 {
9c2799c2 8856 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 8857 load_register (lreg, &offset_expr, 0);
252b5132
RH
8858 }
8859 }
8860 }
8fc2e39e 8861 break;
252b5132
RH
8862 }
8863
8864 /* We know that sym is in the .rdata section. First we get the
8865 upper 16 bits of the address. */
8866 if (mips_pic == NO_PIC)
8867 {
67c0d1eb 8868 macro_build_lui (&offset_expr, AT);
8fc2e39e 8869 used_at = 1;
252b5132 8870 }
0a44bf69 8871 else
252b5132 8872 {
67c0d1eb
RS
8873 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8874 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 8875 used_at = 1;
252b5132 8876 }
bdaaa2e1 8877
252b5132 8878 /* Now we load the register(s). */
ca4e0257 8879 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
8880 {
8881 used_at = 1;
8882 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8883 }
252b5132
RH
8884 else
8885 {
8fc2e39e 8886 used_at = 1;
67c0d1eb 8887 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 8888 if (treg != RA)
252b5132
RH
8889 {
8890 /* FIXME: How in the world do we deal with the possible
8891 overflow here? */
8892 offset_expr.X_add_number += 4;
67c0d1eb 8893 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 8894 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
8895 }
8896 }
252b5132
RH
8897 break;
8898
8899 case M_LI_DD:
ca4e0257
RS
8900 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
8901 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
8902 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
8903 the value and the low order 32 bits are either zero or in
8904 OFFSET_EXPR. */
252b5132
RH
8905 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8906 {
8fc2e39e 8907 used_at = 1;
67c0d1eb 8908 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
8909 if (HAVE_64BIT_FPRS)
8910 {
9c2799c2 8911 gas_assert (HAVE_64BIT_GPRS);
67c0d1eb 8912 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 8913 }
252b5132
RH
8914 else
8915 {
67c0d1eb 8916 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 8917 if (offset_expr.X_op == O_absent)
67c0d1eb 8918 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
8919 else
8920 {
9c2799c2 8921 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
8922 load_register (AT, &offset_expr, 0);
8923 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
8924 }
8925 }
8926 break;
8927 }
8928
9c2799c2 8929 gas_assert (offset_expr.X_op == O_symbol
90ecf173 8930 && offset_expr.X_add_number == 0);
252b5132
RH
8931 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
8932 if (strcmp (s, ".lit8") == 0)
8933 {
0aa27725 8934 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
252b5132 8935 {
67c0d1eb 8936 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 8937 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 8938 break;
252b5132 8939 }
c9914766 8940 breg = mips_gp_register;
252b5132
RH
8941 r = BFD_RELOC_MIPS_LITERAL;
8942 goto dob;
8943 }
8944 else
8945 {
9c2799c2 8946 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 8947 used_at = 1;
0a44bf69 8948 if (mips_pic != NO_PIC)
67c0d1eb
RS
8949 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8950 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
8951 else
8952 {
8953 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 8954 macro_build_lui (&offset_expr, AT);
252b5132 8955 }
bdaaa2e1 8956
0aa27725 8957 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
252b5132 8958 {
67c0d1eb
RS
8959 macro_build (&offset_expr, "ldc1", "T,o(b)",
8960 treg, BFD_RELOC_LO16, AT);
252b5132
RH
8961 break;
8962 }
8963 breg = AT;
8964 r = BFD_RELOC_LO16;
8965 goto dob;
8966 }
8967
8968 case M_L_DOB:
252b5132
RH
8969 /* Even on a big endian machine $fn comes before $fn+1. We have
8970 to adjust when loading from memory. */
8971 r = BFD_RELOC_LO16;
8972 dob:
df58fc94 8973 gas_assert (!mips_opts.micromips);
0aa27725 8974 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
67c0d1eb 8975 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 8976 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
8977 /* FIXME: A possible overflow which I don't know how to deal
8978 with. */
8979 offset_expr.X_add_number += 4;
67c0d1eb 8980 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 8981 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
8982 break;
8983
c4a68bea 8984 case M_S_DOB:
df58fc94 8985 gas_assert (!mips_opts.micromips);
0aa27725 8986 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
c4a68bea
MR
8987 /* Even on a big endian machine $fn comes before $fn+1. We have
8988 to adjust when storing to memory. */
8989 macro_build (&offset_expr, "swc1", "T,o(b)",
8990 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
8991 offset_expr.X_add_number += 4;
8992 macro_build (&offset_expr, "swc1", "T,o(b)",
8993 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8994 break;
8995
252b5132 8996 case M_L_DAB:
df58fc94 8997 gas_assert (!mips_opts.micromips);
252b5132
RH
8998 /*
8999 * The MIPS assembler seems to check for X_add_number not
9000 * being double aligned and generating:
9001 * lui at,%hi(foo+1)
9002 * addu at,at,v1
9003 * addiu at,at,%lo(foo+1)
9004 * lwc1 f2,0(at)
9005 * lwc1 f3,4(at)
9006 * But, the resulting address is the same after relocation so why
9007 * generate the extra instruction?
9008 */
bdaaa2e1 9009 /* Itbl support may require additional care here. */
252b5132 9010 coproc = 1;
df58fc94 9011 fmt = "T,o(b)";
0aa27725 9012 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
9013 {
9014 s = "ldc1";
df58fc94 9015 goto ld_st;
252b5132 9016 }
252b5132 9017 s = "lwc1";
252b5132
RH
9018 goto ldd_std;
9019
9020 case M_S_DAB:
df58fc94
RS
9021 gas_assert (!mips_opts.micromips);
9022 /* Itbl support may require additional care here. */
9023 coproc = 1;
9024 fmt = "T,o(b)";
0aa27725 9025 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
9026 {
9027 s = "sdc1";
df58fc94 9028 goto ld_st;
252b5132 9029 }
252b5132 9030 s = "swc1";
252b5132
RH
9031 goto ldd_std;
9032
e407c74b
NC
9033 case M_LQ_AB:
9034 fmt = "t,o(b)";
9035 s = "lq";
9036 goto ld;
9037
9038 case M_SQ_AB:
9039 fmt = "t,o(b)";
9040 s = "sq";
9041 goto ld_st;
9042
252b5132 9043 case M_LD_AB:
df58fc94 9044 fmt = "t,o(b)";
ca4e0257 9045 if (HAVE_64BIT_GPRS)
252b5132
RH
9046 {
9047 s = "ld";
9048 goto ld;
9049 }
252b5132 9050 s = "lw";
252b5132
RH
9051 goto ldd_std;
9052
9053 case M_SD_AB:
df58fc94 9054 fmt = "t,o(b)";
ca4e0257 9055 if (HAVE_64BIT_GPRS)
252b5132
RH
9056 {
9057 s = "sd";
df58fc94 9058 goto ld_st;
252b5132 9059 }
252b5132 9060 s = "sw";
252b5132
RH
9061
9062 ldd_std:
9063 if (offset_expr.X_op != O_symbol
9064 && offset_expr.X_op != O_constant)
9065 {
f71d0d44 9066 as_bad (_("Expression too complex"));
252b5132
RH
9067 offset_expr.X_op = O_constant;
9068 }
9069
2051e8c4
MR
9070 if (HAVE_32BIT_ADDRESSES
9071 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
9072 {
9073 char value [32];
9074
9075 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 9076 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 9077 }
2051e8c4 9078
252b5132
RH
9079 /* Even on a big endian machine $fn comes before $fn+1. We have
9080 to adjust when loading from memory. We set coproc if we must
9081 load $fn+1 first. */
bdaaa2e1 9082 /* Itbl support may require additional care here. */
90ecf173 9083 if (!target_big_endian)
252b5132
RH
9084 coproc = 0;
9085
90ecf173 9086 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
9087 {
9088 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
9089 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
9090 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
9091 If we have a base register, we use this
9092 addu $at,$breg,$gp
cdf6fd85
TS
9093 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
9094 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
9095 If this is not a GP relative symbol, we want
9096 lui $at,<sym> (BFD_RELOC_HI16_S)
9097 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9098 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9099 If there is a base register, we add it to $at after the
9100 lui instruction. If there is a constant, we always use
9101 the last case. */
39a59cf8
MR
9102 if (offset_expr.X_op == O_symbol
9103 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9104 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 9105 {
4d7206a2 9106 relax_start (offset_expr.X_add_symbol);
252b5132
RH
9107 if (breg == 0)
9108 {
c9914766 9109 tempreg = mips_gp_register;
252b5132
RH
9110 }
9111 else
9112 {
67c0d1eb 9113 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9114 AT, breg, mips_gp_register);
252b5132 9115 tempreg = AT;
252b5132
RH
9116 used_at = 1;
9117 }
9118
beae10d5 9119 /* Itbl support may require additional care here. */
67c0d1eb 9120 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9121 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
9122 offset_expr.X_add_number += 4;
9123
9124 /* Set mips_optimize to 2 to avoid inserting an
9125 undesired nop. */
9126 hold_mips_optimize = mips_optimize;
9127 mips_optimize = 2;
beae10d5 9128 /* Itbl support may require additional care here. */
67c0d1eb 9129 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9130 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
9131 mips_optimize = hold_mips_optimize;
9132
4d7206a2 9133 relax_switch ();
252b5132 9134
0970e49e 9135 offset_expr.X_add_number -= 4;
252b5132 9136 }
8fc2e39e 9137 used_at = 1;
67c0d1eb 9138 macro_build_lui (&offset_expr, AT);
252b5132 9139 if (breg != 0)
67c0d1eb 9140 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9141 /* Itbl support may require additional care here. */
67c0d1eb 9142 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9143 BFD_RELOC_LO16, AT);
252b5132
RH
9144 /* FIXME: How do we handle overflow here? */
9145 offset_expr.X_add_number += 4;
beae10d5 9146 /* Itbl support may require additional care here. */
67c0d1eb 9147 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9148 BFD_RELOC_LO16, AT);
4d7206a2
RS
9149 if (mips_relax.sequence)
9150 relax_end ();
bdaaa2e1 9151 }
0a44bf69 9152 else if (!mips_big_got)
252b5132 9153 {
252b5132
RH
9154 /* If this is a reference to an external symbol, we want
9155 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9156 nop
9157 <op> $treg,0($at)
9158 <op> $treg+1,4($at)
9159 Otherwise we want
9160 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9161 nop
9162 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9163 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9164 If there is a base register we add it to $at before the
9165 lwc1 instructions. If there is a constant we include it
9166 in the lwc1 instructions. */
9167 used_at = 1;
9168 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
9169 if (expr1.X_add_number < -0x8000
9170 || expr1.X_add_number >= 0x8000 - 4)
9171 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9172 load_got_offset (AT, &offset_expr);
269137b2 9173 load_delay_nop ();
252b5132 9174 if (breg != 0)
67c0d1eb 9175 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
9176
9177 /* Set mips_optimize to 2 to avoid inserting an undesired
9178 nop. */
9179 hold_mips_optimize = mips_optimize;
9180 mips_optimize = 2;
4d7206a2 9181
beae10d5 9182 /* Itbl support may require additional care here. */
4d7206a2 9183 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9184 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9185 BFD_RELOC_LO16, AT);
4d7206a2 9186 expr1.X_add_number += 4;
67c0d1eb
RS
9187 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9188 BFD_RELOC_LO16, AT);
4d7206a2 9189 relax_switch ();
67c0d1eb
RS
9190 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9191 BFD_RELOC_LO16, AT);
4d7206a2 9192 offset_expr.X_add_number += 4;
67c0d1eb
RS
9193 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9194 BFD_RELOC_LO16, AT);
4d7206a2 9195 relax_end ();
252b5132 9196
4d7206a2 9197 mips_optimize = hold_mips_optimize;
252b5132 9198 }
0a44bf69 9199 else if (mips_big_got)
252b5132 9200 {
67c0d1eb 9201 int gpdelay;
252b5132
RH
9202
9203 /* If this is a reference to an external symbol, we want
9204 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9205 addu $at,$at,$gp
9206 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
9207 nop
9208 <op> $treg,0($at)
9209 <op> $treg+1,4($at)
9210 Otherwise we want
9211 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9212 nop
9213 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9214 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9215 If there is a base register we add it to $at before the
9216 lwc1 instructions. If there is a constant we include it
9217 in the lwc1 instructions. */
9218 used_at = 1;
9219 expr1.X_add_number = offset_expr.X_add_number;
9220 offset_expr.X_add_number = 0;
9221 if (expr1.X_add_number < -0x8000
9222 || expr1.X_add_number >= 0x8000 - 4)
9223 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9224 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 9225 relax_start (offset_expr.X_add_symbol);
df58fc94 9226 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
9227 AT, BFD_RELOC_MIPS_GOT_HI16);
9228 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9229 AT, AT, mips_gp_register);
67c0d1eb 9230 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 9231 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 9232 load_delay_nop ();
252b5132 9233 if (breg != 0)
67c0d1eb 9234 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9235 /* Itbl support may require additional care here. */
67c0d1eb 9236 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9237 BFD_RELOC_LO16, AT);
252b5132
RH
9238 expr1.X_add_number += 4;
9239
9240 /* Set mips_optimize to 2 to avoid inserting an undesired
9241 nop. */
9242 hold_mips_optimize = mips_optimize;
9243 mips_optimize = 2;
beae10d5 9244 /* Itbl support may require additional care here. */
67c0d1eb 9245 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 9246 BFD_RELOC_LO16, AT);
252b5132
RH
9247 mips_optimize = hold_mips_optimize;
9248 expr1.X_add_number -= 4;
9249
4d7206a2
RS
9250 relax_switch ();
9251 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
9252 if (gpdelay)
9253 macro_build (NULL, "nop", "");
9254 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9255 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9256 load_delay_nop ();
252b5132 9257 if (breg != 0)
67c0d1eb 9258 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9259 /* Itbl support may require additional care here. */
67c0d1eb
RS
9260 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9261 BFD_RELOC_LO16, AT);
4d7206a2 9262 offset_expr.X_add_number += 4;
252b5132
RH
9263
9264 /* Set mips_optimize to 2 to avoid inserting an undesired
9265 nop. */
9266 hold_mips_optimize = mips_optimize;
9267 mips_optimize = 2;
beae10d5 9268 /* Itbl support may require additional care here. */
67c0d1eb
RS
9269 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9270 BFD_RELOC_LO16, AT);
252b5132 9271 mips_optimize = hold_mips_optimize;
4d7206a2 9272 relax_end ();
252b5132 9273 }
252b5132
RH
9274 else
9275 abort ();
9276
252b5132
RH
9277 break;
9278
9279 case M_LD_OB:
704897fb 9280 s = HAVE_64BIT_GPRS ? "ld" : "lw";
252b5132
RH
9281 goto sd_ob;
9282 case M_SD_OB:
704897fb 9283 s = HAVE_64BIT_GPRS ? "sd" : "sw";
252b5132 9284 sd_ob:
4614d845
MR
9285 macro_build (&offset_expr, s, "t,o(b)", treg,
9286 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9287 breg);
704897fb
MR
9288 if (!HAVE_64BIT_GPRS)
9289 {
9290 offset_expr.X_add_number += 4;
9291 macro_build (&offset_expr, s, "t,o(b)", treg + 1,
4614d845
MR
9292 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9293 breg);
704897fb 9294 }
8fc2e39e 9295 break;
252b5132 9296
dd6a37e7
AP
9297
9298 case M_SAA_AB:
9299 ab = 1;
9300 case M_SAA_OB:
9301 s = "saa";
9302 off0 = 1;
9303 fmt = "t,(b)";
9304 goto ld_st;
9305 case M_SAAD_AB:
9306 ab = 1;
9307 case M_SAAD_OB:
9308 s = "saad";
9309 off0 = 1;
9310 fmt = "t,(b)";
9311 goto ld_st;
9312
252b5132
RH
9313 /* New code added to support COPZ instructions.
9314 This code builds table entries out of the macros in mip_opcodes.
9315 R4000 uses interlocks to handle coproc delays.
9316 Other chips (like the R3000) require nops to be inserted for delays.
9317
f72c8c98 9318 FIXME: Currently, we require that the user handle delays.
252b5132
RH
9319 In order to fill delay slots for non-interlocked chips,
9320 we must have a way to specify delays based on the coprocessor.
9321 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9322 What are the side-effects of the cop instruction?
9323 What cache support might we have and what are its effects?
9324 Both coprocessor & memory require delays. how long???
bdaaa2e1 9325 What registers are read/set/modified?
252b5132
RH
9326
9327 If an itbl is provided to interpret cop instructions,
bdaaa2e1 9328 this knowledge can be encoded in the itbl spec. */
252b5132
RH
9329
9330 case M_COP0:
9331 s = "c0";
9332 goto copz;
9333 case M_COP1:
9334 s = "c1";
9335 goto copz;
9336 case M_COP2:
9337 s = "c2";
9338 goto copz;
9339 case M_COP3:
9340 s = "c3";
9341 copz:
df58fc94 9342 gas_assert (!mips_opts.micromips);
252b5132
RH
9343 /* For now we just do C (same as Cz). The parameter will be
9344 stored in insn_opcode by mips_ip. */
67c0d1eb 9345 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 9346 break;
252b5132 9347
ea1fb5dc 9348 case M_MOVE:
67c0d1eb 9349 move_register (dreg, sreg);
8fc2e39e 9350 break;
ea1fb5dc 9351
252b5132
RH
9352 case M_DMUL:
9353 dbl = 1;
9354 case M_MUL:
e407c74b
NC
9355 if (mips_opts.arch == CPU_R5900)
9356 {
9357 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", dreg, sreg, treg);
9358 }
9359 else
9360 {
67c0d1eb 9361 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
df58fc94 9362 macro_build (NULL, "mflo", MFHL_FMT, dreg);
e407c74b 9363 }
8fc2e39e 9364 break;
252b5132
RH
9365
9366 case M_DMUL_I:
9367 dbl = 1;
9368 case M_MUL_I:
9369 /* The MIPS assembler some times generates shifts and adds. I'm
9370 not trying to be that fancy. GCC should do this for us
9371 anyway. */
8fc2e39e 9372 used_at = 1;
67c0d1eb
RS
9373 load_register (AT, &imm_expr, dbl);
9374 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
df58fc94 9375 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9376 break;
9377
9378 case M_DMULO_I:
9379 dbl = 1;
9380 case M_MULO_I:
9381 imm = 1;
9382 goto do_mulo;
9383
9384 case M_DMULO:
9385 dbl = 1;
9386 case M_MULO:
9387 do_mulo:
7d10b47d 9388 start_noreorder ();
8fc2e39e 9389 used_at = 1;
252b5132 9390 if (imm)
67c0d1eb
RS
9391 load_register (AT, &imm_expr, dbl);
9392 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
df58fc94
RS
9393 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9394 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9395 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 9396 if (mips_trap)
df58fc94 9397 macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
252b5132
RH
9398 else
9399 {
df58fc94
RS
9400 if (mips_opts.micromips)
9401 micromips_label_expr (&label_expr);
9402 else
9403 label_expr.X_add_number = 8;
9404 macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
a605d2b3 9405 macro_build (NULL, "nop", "");
df58fc94
RS
9406 macro_build (NULL, "break", BRK_FMT, 6);
9407 if (mips_opts.micromips)
9408 micromips_add_label ();
252b5132 9409 }
7d10b47d 9410 end_noreorder ();
df58fc94 9411 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9412 break;
9413
9414 case M_DMULOU_I:
9415 dbl = 1;
9416 case M_MULOU_I:
9417 imm = 1;
9418 goto do_mulou;
9419
9420 case M_DMULOU:
9421 dbl = 1;
9422 case M_MULOU:
9423 do_mulou:
7d10b47d 9424 start_noreorder ();
8fc2e39e 9425 used_at = 1;
252b5132 9426 if (imm)
67c0d1eb
RS
9427 load_register (AT, &imm_expr, dbl);
9428 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 9429 sreg, imm ? AT : treg);
df58fc94
RS
9430 macro_build (NULL, "mfhi", MFHL_FMT, AT);
9431 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132 9432 if (mips_trap)
df58fc94 9433 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
9434 else
9435 {
df58fc94
RS
9436 if (mips_opts.micromips)
9437 micromips_label_expr (&label_expr);
9438 else
9439 label_expr.X_add_number = 8;
9440 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 9441 macro_build (NULL, "nop", "");
df58fc94
RS
9442 macro_build (NULL, "break", BRK_FMT, 6);
9443 if (mips_opts.micromips)
9444 micromips_add_label ();
252b5132 9445 }
7d10b47d 9446 end_noreorder ();
252b5132
RH
9447 break;
9448
771c7ce4 9449 case M_DROL:
fef14a42 9450 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
9451 {
9452 if (dreg == sreg)
9453 {
9454 tempreg = AT;
9455 used_at = 1;
9456 }
9457 else
9458 {
9459 tempreg = dreg;
82dd0097 9460 }
67c0d1eb
RS
9461 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9462 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9463 break;
82dd0097 9464 }
8fc2e39e 9465 used_at = 1;
c80c840e 9466 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9467 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9468 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9469 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9470 break;
9471
252b5132 9472 case M_ROL:
fef14a42 9473 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
9474 {
9475 if (dreg == sreg)
9476 {
9477 tempreg = AT;
9478 used_at = 1;
9479 }
9480 else
9481 {
9482 tempreg = dreg;
82dd0097 9483 }
67c0d1eb
RS
9484 macro_build (NULL, "negu", "d,w", tempreg, treg);
9485 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9486 break;
82dd0097 9487 }
8fc2e39e 9488 used_at = 1;
c80c840e 9489 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9490 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9491 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9492 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9493 break;
9494
771c7ce4
TS
9495 case M_DROL_I:
9496 {
9497 unsigned int rot;
91d6fa6a
NC
9498 char *l;
9499 char *rr;
771c7ce4
TS
9500
9501 if (imm_expr.X_op != O_constant)
82dd0097 9502 as_bad (_("Improper rotate count"));
771c7ce4 9503 rot = imm_expr.X_add_number & 0x3f;
fef14a42 9504 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
9505 {
9506 rot = (64 - rot) & 0x3f;
9507 if (rot >= 32)
df58fc94 9508 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
60b63b72 9509 else
df58fc94 9510 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9511 break;
60b63b72 9512 }
483fc7cd 9513 if (rot == 0)
483fc7cd 9514 {
df58fc94 9515 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9516 break;
483fc7cd 9517 }
82dd0097 9518 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 9519 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 9520 rot &= 0x1f;
8fc2e39e 9521 used_at = 1;
df58fc94
RS
9522 macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
9523 macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9524 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9525 }
9526 break;
9527
252b5132 9528 case M_ROL_I:
771c7ce4
TS
9529 {
9530 unsigned int rot;
9531
9532 if (imm_expr.X_op != O_constant)
82dd0097 9533 as_bad (_("Improper rotate count"));
771c7ce4 9534 rot = imm_expr.X_add_number & 0x1f;
fef14a42 9535 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 9536 {
df58fc94 9537 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 9538 break;
60b63b72 9539 }
483fc7cd 9540 if (rot == 0)
483fc7cd 9541 {
df58fc94 9542 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9543 break;
483fc7cd 9544 }
8fc2e39e 9545 used_at = 1;
df58fc94
RS
9546 macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
9547 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9548 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9549 }
9550 break;
9551
9552 case M_DROR:
fef14a42 9553 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 9554 {
67c0d1eb 9555 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 9556 break;
82dd0097 9557 }
8fc2e39e 9558 used_at = 1;
c80c840e 9559 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9560 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
9561 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
9562 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9563 break;
9564
9565 case M_ROR:
fef14a42 9566 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 9567 {
67c0d1eb 9568 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 9569 break;
82dd0097 9570 }
8fc2e39e 9571 used_at = 1;
c80c840e 9572 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9573 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
9574 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
9575 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9576 break;
9577
771c7ce4
TS
9578 case M_DROR_I:
9579 {
9580 unsigned int rot;
91d6fa6a
NC
9581 char *l;
9582 char *rr;
771c7ce4
TS
9583
9584 if (imm_expr.X_op != O_constant)
82dd0097 9585 as_bad (_("Improper rotate count"));
771c7ce4 9586 rot = imm_expr.X_add_number & 0x3f;
fef14a42 9587 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
9588 {
9589 if (rot >= 32)
df58fc94 9590 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
82dd0097 9591 else
df58fc94 9592 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9593 break;
82dd0097 9594 }
483fc7cd 9595 if (rot == 0)
483fc7cd 9596 {
df58fc94 9597 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9598 break;
483fc7cd 9599 }
91d6fa6a 9600 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
9601 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
9602 rot &= 0x1f;
8fc2e39e 9603 used_at = 1;
df58fc94
RS
9604 macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
9605 macro_build (NULL, l, 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
252b5132 9610 case M_ROR_I:
771c7ce4
TS
9611 {
9612 unsigned int rot;
9613
9614 if (imm_expr.X_op != O_constant)
82dd0097 9615 as_bad (_("Improper rotate count"));
771c7ce4 9616 rot = imm_expr.X_add_number & 0x1f;
fef14a42 9617 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 9618 {
df58fc94 9619 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9620 break;
82dd0097 9621 }
483fc7cd 9622 if (rot == 0)
483fc7cd 9623 {
df58fc94 9624 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9625 break;
483fc7cd 9626 }
8fc2e39e 9627 used_at = 1;
df58fc94
RS
9628 macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
9629 macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9630 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 9631 }
252b5132
RH
9632 break;
9633
252b5132
RH
9634 case M_SEQ:
9635 if (sreg == 0)
67c0d1eb 9636 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 9637 else if (treg == 0)
67c0d1eb 9638 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9639 else
9640 {
67c0d1eb
RS
9641 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9642 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 9643 }
8fc2e39e 9644 break;
252b5132
RH
9645
9646 case M_SEQ_I:
9647 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9648 {
67c0d1eb 9649 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9650 break;
252b5132
RH
9651 }
9652 if (sreg == 0)
9653 {
9654 as_warn (_("Instruction %s: result is always false"),
9655 ip->insn_mo->name);
67c0d1eb 9656 move_register (dreg, 0);
8fc2e39e 9657 break;
252b5132 9658 }
dd3cbb7e
NC
9659 if (CPU_HAS_SEQ (mips_opts.arch)
9660 && -512 <= imm_expr.X_add_number
9661 && imm_expr.X_add_number < 512)
9662 {
9663 macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
750bdd57 9664 (int) imm_expr.X_add_number);
dd3cbb7e
NC
9665 break;
9666 }
252b5132
RH
9667 if (imm_expr.X_op == O_constant
9668 && imm_expr.X_add_number >= 0
9669 && imm_expr.X_add_number < 0x10000)
9670 {
67c0d1eb 9671 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9672 }
9673 else if (imm_expr.X_op == O_constant
9674 && imm_expr.X_add_number > -0x8000
9675 && imm_expr.X_add_number < 0)
9676 {
9677 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 9678 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 9679 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 9680 }
dd3cbb7e
NC
9681 else if (CPU_HAS_SEQ (mips_opts.arch))
9682 {
9683 used_at = 1;
9684 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9685 macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
9686 break;
9687 }
252b5132
RH
9688 else
9689 {
67c0d1eb
RS
9690 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9691 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
9692 used_at = 1;
9693 }
67c0d1eb 9694 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9695 break;
252b5132
RH
9696
9697 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
9698 s = "slt";
9699 goto sge;
9700 case M_SGEU:
9701 s = "sltu";
9702 sge:
67c0d1eb
RS
9703 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
9704 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9705 break;
252b5132
RH
9706
9707 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
9708 case M_SGEU_I:
9709 if (imm_expr.X_op == O_constant
9710 && imm_expr.X_add_number >= -0x8000
9711 && imm_expr.X_add_number < 0x8000)
9712 {
67c0d1eb
RS
9713 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
9714 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9715 }
9716 else
9717 {
67c0d1eb
RS
9718 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9719 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
9720 dreg, sreg, AT);
252b5132
RH
9721 used_at = 1;
9722 }
67c0d1eb 9723 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9724 break;
252b5132
RH
9725
9726 case M_SGT: /* sreg > treg <==> treg < sreg */
9727 s = "slt";
9728 goto sgt;
9729 case M_SGTU:
9730 s = "sltu";
9731 sgt:
67c0d1eb 9732 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 9733 break;
252b5132
RH
9734
9735 case M_SGT_I: /* sreg > I <==> I < sreg */
9736 s = "slt";
9737 goto sgti;
9738 case M_SGTU_I:
9739 s = "sltu";
9740 sgti:
8fc2e39e 9741 used_at = 1;
67c0d1eb
RS
9742 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9743 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
9744 break;
9745
2396cfb9 9746 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
9747 s = "slt";
9748 goto sle;
9749 case M_SLEU:
9750 s = "sltu";
9751 sle:
67c0d1eb
RS
9752 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9753 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9754 break;
252b5132 9755
2396cfb9 9756 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
9757 s = "slt";
9758 goto slei;
9759 case M_SLEU_I:
9760 s = "sltu";
9761 slei:
8fc2e39e 9762 used_at = 1;
67c0d1eb
RS
9763 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9764 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9765 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
9766 break;
9767
9768 case M_SLT_I:
9769 if (imm_expr.X_op == O_constant
9770 && imm_expr.X_add_number >= -0x8000
9771 && imm_expr.X_add_number < 0x8000)
9772 {
67c0d1eb 9773 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9774 break;
252b5132 9775 }
8fc2e39e 9776 used_at = 1;
67c0d1eb
RS
9777 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9778 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
9779 break;
9780
9781 case M_SLTU_I:
9782 if (imm_expr.X_op == O_constant
9783 && imm_expr.X_add_number >= -0x8000
9784 && imm_expr.X_add_number < 0x8000)
9785 {
67c0d1eb 9786 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 9787 BFD_RELOC_LO16);
8fc2e39e 9788 break;
252b5132 9789 }
8fc2e39e 9790 used_at = 1;
67c0d1eb
RS
9791 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9792 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
9793 break;
9794
9795 case M_SNE:
9796 if (sreg == 0)
67c0d1eb 9797 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 9798 else if (treg == 0)
67c0d1eb 9799 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
9800 else
9801 {
67c0d1eb
RS
9802 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9803 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 9804 }
8fc2e39e 9805 break;
252b5132
RH
9806
9807 case M_SNE_I:
9808 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9809 {
67c0d1eb 9810 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 9811 break;
252b5132
RH
9812 }
9813 if (sreg == 0)
9814 {
9815 as_warn (_("Instruction %s: result is always true"),
9816 ip->insn_mo->name);
67c0d1eb
RS
9817 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
9818 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 9819 break;
252b5132 9820 }
dd3cbb7e
NC
9821 if (CPU_HAS_SEQ (mips_opts.arch)
9822 && -512 <= imm_expr.X_add_number
9823 && imm_expr.X_add_number < 512)
9824 {
9825 macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
750bdd57 9826 (int) imm_expr.X_add_number);
dd3cbb7e
NC
9827 break;
9828 }
252b5132
RH
9829 if (imm_expr.X_op == O_constant
9830 && imm_expr.X_add_number >= 0
9831 && imm_expr.X_add_number < 0x10000)
9832 {
67c0d1eb 9833 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9834 }
9835 else if (imm_expr.X_op == O_constant
9836 && imm_expr.X_add_number > -0x8000
9837 && imm_expr.X_add_number < 0)
9838 {
9839 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 9840 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 9841 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 9842 }
dd3cbb7e
NC
9843 else if (CPU_HAS_SEQ (mips_opts.arch))
9844 {
9845 used_at = 1;
9846 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9847 macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
9848 break;
9849 }
252b5132
RH
9850 else
9851 {
67c0d1eb
RS
9852 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9853 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
9854 used_at = 1;
9855 }
67c0d1eb 9856 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 9857 break;
252b5132 9858
df58fc94
RS
9859 case M_SUB_I:
9860 s = "addi";
9861 s2 = "sub";
9862 goto do_subi;
9863 case M_SUBU_I:
9864 s = "addiu";
9865 s2 = "subu";
9866 goto do_subi;
252b5132
RH
9867 case M_DSUB_I:
9868 dbl = 1;
df58fc94
RS
9869 s = "daddi";
9870 s2 = "dsub";
9871 if (!mips_opts.micromips)
9872 goto do_subi;
252b5132 9873 if (imm_expr.X_op == O_constant
df58fc94
RS
9874 && imm_expr.X_add_number > -0x200
9875 && imm_expr.X_add_number <= 0x200)
252b5132 9876 {
df58fc94 9877 macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
8fc2e39e 9878 break;
252b5132 9879 }
df58fc94 9880 goto do_subi_i;
252b5132
RH
9881 case M_DSUBU_I:
9882 dbl = 1;
df58fc94
RS
9883 s = "daddiu";
9884 s2 = "dsubu";
9885 do_subi:
252b5132
RH
9886 if (imm_expr.X_op == O_constant
9887 && imm_expr.X_add_number > -0x8000
9888 && imm_expr.X_add_number <= 0x8000)
9889 {
9890 imm_expr.X_add_number = -imm_expr.X_add_number;
df58fc94 9891 macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9892 break;
252b5132 9893 }
df58fc94 9894 do_subi_i:
8fc2e39e 9895 used_at = 1;
67c0d1eb 9896 load_register (AT, &imm_expr, dbl);
df58fc94 9897 macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
252b5132
RH
9898 break;
9899
9900 case M_TEQ_I:
9901 s = "teq";
9902 goto trap;
9903 case M_TGE_I:
9904 s = "tge";
9905 goto trap;
9906 case M_TGEU_I:
9907 s = "tgeu";
9908 goto trap;
9909 case M_TLT_I:
9910 s = "tlt";
9911 goto trap;
9912 case M_TLTU_I:
9913 s = "tltu";
9914 goto trap;
9915 case M_TNE_I:
9916 s = "tne";
9917 trap:
8fc2e39e 9918 used_at = 1;
67c0d1eb
RS
9919 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9920 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
9921 break;
9922
252b5132 9923 case M_TRUNCWS:
43841e91 9924 case M_TRUNCWD:
df58fc94 9925 gas_assert (!mips_opts.micromips);
0aa27725 9926 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 9927 used_at = 1;
252b5132
RH
9928 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
9929 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
9930
9931 /*
9932 * Is the double cfc1 instruction a bug in the mips assembler;
9933 * or is there a reason for it?
9934 */
7d10b47d 9935 start_noreorder ();
67c0d1eb
RS
9936 macro_build (NULL, "cfc1", "t,G", treg, RA);
9937 macro_build (NULL, "cfc1", "t,G", treg, RA);
9938 macro_build (NULL, "nop", "");
252b5132 9939 expr1.X_add_number = 3;
67c0d1eb 9940 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 9941 expr1.X_add_number = 2;
67c0d1eb
RS
9942 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
9943 macro_build (NULL, "ctc1", "t,G", AT, RA);
9944 macro_build (NULL, "nop", "");
9945 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
9946 dreg, sreg);
9947 macro_build (NULL, "ctc1", "t,G", treg, RA);
9948 macro_build (NULL, "nop", "");
7d10b47d 9949 end_noreorder ();
252b5132
RH
9950 break;
9951
df58fc94
RS
9952 case M_ULH_A:
9953 ab = 1;
252b5132
RH
9954 case M_ULH:
9955 s = "lb";
df58fc94
RS
9956 s2 = "lbu";
9957 off = 1;
9958 goto uld_st;
9959 case M_ULHU_A:
9960 ab = 1;
252b5132
RH
9961 case M_ULHU:
9962 s = "lbu";
df58fc94
RS
9963 s2 = "lbu";
9964 off = 1;
9965 goto uld_st;
9966 case M_ULW_A:
9967 ab = 1;
9968 case M_ULW:
9969 s = "lwl";
9970 s2 = "lwr";
9971 off12 = mips_opts.micromips;
9972 off = 3;
9973 goto uld_st;
9974 case M_ULD_A:
9975 ab = 1;
252b5132
RH
9976 case M_ULD:
9977 s = "ldl";
9978 s2 = "ldr";
df58fc94 9979 off12 = mips_opts.micromips;
252b5132 9980 off = 7;
df58fc94
RS
9981 goto uld_st;
9982 case M_USH_A:
9983 ab = 1;
9984 case M_USH:
9985 s = "sb";
9986 s2 = "sb";
9987 off = 1;
9988 ust = 1;
9989 goto uld_st;
9990 case M_USW_A:
9991 ab = 1;
9992 case M_USW:
9993 s = "swl";
9994 s2 = "swr";
9995 off12 = mips_opts.micromips;
252b5132 9996 off = 3;
df58fc94
RS
9997 ust = 1;
9998 goto uld_st;
9999 case M_USD_A:
10000 ab = 1;
10001 case M_USD:
10002 s = "sdl";
10003 s2 = "sdr";
10004 off12 = mips_opts.micromips;
10005 off = 7;
10006 ust = 1;
10007
10008 uld_st:
10009 if (!ab && offset_expr.X_add_number >= 0x8000 - off)
f71d0d44 10010 as_bad (_("Operand overflow"));
df58fc94
RS
10011
10012 ep = &offset_expr;
10013 expr1.X_add_number = 0;
10014 if (ab)
10015 {
10016 used_at = 1;
10017 tempreg = AT;
10018 load_address (tempreg, ep, &used_at);
10019 if (breg != 0)
10020 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10021 tempreg, tempreg, breg);
10022 breg = tempreg;
10023 tempreg = treg;
10024 ep = &expr1;
10025 }
10026 else if (off12
10027 && (offset_expr.X_op != O_constant
10028 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
10029 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
10030 {
10031 used_at = 1;
10032 tempreg = AT;
10033 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
10034 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10035 breg = tempreg;
10036 tempreg = treg;
10037 ep = &expr1;
10038 }
10039 else if (!ust && treg == breg)
8fc2e39e
TS
10040 {
10041 used_at = 1;
10042 tempreg = AT;
10043 }
252b5132 10044 else
df58fc94 10045 tempreg = treg;
af22f5b2 10046
df58fc94
RS
10047 if (off == 1)
10048 goto ulh_sh;
252b5132 10049
90ecf173 10050 if (!target_big_endian)
df58fc94
RS
10051 ep->X_add_number += off;
10052 if (!off12)
10053 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 10054 else
df58fc94
RS
10055 macro_build (NULL, s, "t,~(b)",
10056 tempreg, (unsigned long) ep->X_add_number, breg);
10057
90ecf173 10058 if (!target_big_endian)
df58fc94 10059 ep->X_add_number -= off;
252b5132 10060 else
df58fc94
RS
10061 ep->X_add_number += off;
10062 if (!off12)
10063 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10064 else
10065 macro_build (NULL, s2, "t,~(b)",
10066 tempreg, (unsigned long) ep->X_add_number, breg);
252b5132 10067
df58fc94
RS
10068 /* If necessary, move the result in tempreg to the final destination. */
10069 if (!ust && treg != tempreg)
10070 {
10071 /* Protect second load's delay slot. */
10072 load_delay_nop ();
10073 move_register (treg, tempreg);
10074 }
8fc2e39e 10075 break;
252b5132 10076
df58fc94 10077 ulh_sh:
d6bc6245 10078 used_at = 1;
df58fc94
RS
10079 if (target_big_endian == ust)
10080 ep->X_add_number += off;
10081 tempreg = ust || ab ? treg : AT;
10082 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10083
10084 /* For halfword transfers we need a temporary register to shuffle
10085 bytes. Unfortunately for M_USH_A we have none available before
10086 the next store as AT holds the base address. We deal with this
10087 case by clobbering TREG and then restoring it as with ULH. */
10088 tempreg = ust == ab ? treg : AT;
10089 if (ust)
10090 macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10091
10092 if (target_big_endian == ust)
10093 ep->X_add_number -= off;
252b5132 10094 else
df58fc94
RS
10095 ep->X_add_number += off;
10096 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 10097
df58fc94
RS
10098 /* For M_USH_A re-retrieve the LSB. */
10099 if (ust && ab)
10100 {
10101 if (target_big_endian)
10102 ep->X_add_number += off;
10103 else
10104 ep->X_add_number -= off;
10105 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
10106 }
10107 /* For ULH and M_USH_A OR the LSB in. */
10108 if (!ust || ab)
10109 {
10110 tempreg = !ab ? AT : treg;
10111 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10112 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10113 }
252b5132
RH
10114 break;
10115
10116 default:
10117 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 10118 are added dynamically. */
252b5132
RH
10119 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10120 break;
10121 }
741fe287 10122 if (!mips_opts.at && used_at)
8fc2e39e 10123 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
10124}
10125
10126/* Implement macros in mips16 mode. */
10127
10128static void
17a2f251 10129mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
10130{
10131 int mask;
10132 int xreg, yreg, zreg, tmp;
252b5132
RH
10133 expressionS expr1;
10134 int dbl;
10135 const char *s, *s2, *s3;
10136
10137 mask = ip->insn_mo->mask;
10138
bf12938e
RS
10139 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10140 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10141 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 10142
252b5132
RH
10143 expr1.X_op = O_constant;
10144 expr1.X_op_symbol = NULL;
10145 expr1.X_add_symbol = NULL;
10146 expr1.X_add_number = 1;
10147
10148 dbl = 0;
10149
10150 switch (mask)
10151 {
10152 default:
b37df7c4 10153 abort ();
252b5132
RH
10154
10155 case M_DDIV_3:
10156 dbl = 1;
10157 case M_DIV_3:
10158 s = "mflo";
10159 goto do_div3;
10160 case M_DREM_3:
10161 dbl = 1;
10162 case M_REM_3:
10163 s = "mfhi";
10164 do_div3:
7d10b47d 10165 start_noreorder ();
67c0d1eb 10166 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 10167 expr1.X_add_number = 2;
67c0d1eb
RS
10168 macro_build (&expr1, "bnez", "x,p", yreg);
10169 macro_build (NULL, "break", "6", 7);
bdaaa2e1 10170
252b5132
RH
10171 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10172 since that causes an overflow. We should do that as well,
10173 but I don't see how to do the comparisons without a temporary
10174 register. */
7d10b47d 10175 end_noreorder ();
67c0d1eb 10176 macro_build (NULL, s, "x", zreg);
252b5132
RH
10177 break;
10178
10179 case M_DIVU_3:
10180 s = "divu";
10181 s2 = "mflo";
10182 goto do_divu3;
10183 case M_REMU_3:
10184 s = "divu";
10185 s2 = "mfhi";
10186 goto do_divu3;
10187 case M_DDIVU_3:
10188 s = "ddivu";
10189 s2 = "mflo";
10190 goto do_divu3;
10191 case M_DREMU_3:
10192 s = "ddivu";
10193 s2 = "mfhi";
10194 do_divu3:
7d10b47d 10195 start_noreorder ();
67c0d1eb 10196 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 10197 expr1.X_add_number = 2;
67c0d1eb
RS
10198 macro_build (&expr1, "bnez", "x,p", yreg);
10199 macro_build (NULL, "break", "6", 7);
7d10b47d 10200 end_noreorder ();
67c0d1eb 10201 macro_build (NULL, s2, "x", zreg);
252b5132
RH
10202 break;
10203
10204 case M_DMUL:
10205 dbl = 1;
10206 case M_MUL:
67c0d1eb
RS
10207 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10208 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 10209 break;
252b5132
RH
10210
10211 case M_DSUBU_I:
10212 dbl = 1;
10213 goto do_subu;
10214 case M_SUBU_I:
10215 do_subu:
10216 if (imm_expr.X_op != O_constant)
10217 as_bad (_("Unsupported large constant"));
10218 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10219 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
10220 break;
10221
10222 case M_SUBU_I_2:
10223 if (imm_expr.X_op != O_constant)
10224 as_bad (_("Unsupported large constant"));
10225 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10226 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
10227 break;
10228
10229 case M_DSUBU_I_2:
10230 if (imm_expr.X_op != O_constant)
10231 as_bad (_("Unsupported large constant"));
10232 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10233 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
10234 break;
10235
10236 case M_BEQ:
10237 s = "cmp";
10238 s2 = "bteqz";
10239 goto do_branch;
10240 case M_BNE:
10241 s = "cmp";
10242 s2 = "btnez";
10243 goto do_branch;
10244 case M_BLT:
10245 s = "slt";
10246 s2 = "btnez";
10247 goto do_branch;
10248 case M_BLTU:
10249 s = "sltu";
10250 s2 = "btnez";
10251 goto do_branch;
10252 case M_BLE:
10253 s = "slt";
10254 s2 = "bteqz";
10255 goto do_reverse_branch;
10256 case M_BLEU:
10257 s = "sltu";
10258 s2 = "bteqz";
10259 goto do_reverse_branch;
10260 case M_BGE:
10261 s = "slt";
10262 s2 = "bteqz";
10263 goto do_branch;
10264 case M_BGEU:
10265 s = "sltu";
10266 s2 = "bteqz";
10267 goto do_branch;
10268 case M_BGT:
10269 s = "slt";
10270 s2 = "btnez";
10271 goto do_reverse_branch;
10272 case M_BGTU:
10273 s = "sltu";
10274 s2 = "btnez";
10275
10276 do_reverse_branch:
10277 tmp = xreg;
10278 xreg = yreg;
10279 yreg = tmp;
10280
10281 do_branch:
67c0d1eb
RS
10282 macro_build (NULL, s, "x,y", xreg, yreg);
10283 macro_build (&offset_expr, s2, "p");
252b5132
RH
10284 break;
10285
10286 case M_BEQ_I:
10287 s = "cmpi";
10288 s2 = "bteqz";
10289 s3 = "x,U";
10290 goto do_branch_i;
10291 case M_BNE_I:
10292 s = "cmpi";
10293 s2 = "btnez";
10294 s3 = "x,U";
10295 goto do_branch_i;
10296 case M_BLT_I:
10297 s = "slti";
10298 s2 = "btnez";
10299 s3 = "x,8";
10300 goto do_branch_i;
10301 case M_BLTU_I:
10302 s = "sltiu";
10303 s2 = "btnez";
10304 s3 = "x,8";
10305 goto do_branch_i;
10306 case M_BLE_I:
10307 s = "slti";
10308 s2 = "btnez";
10309 s3 = "x,8";
10310 goto do_addone_branch_i;
10311 case M_BLEU_I:
10312 s = "sltiu";
10313 s2 = "btnez";
10314 s3 = "x,8";
10315 goto do_addone_branch_i;
10316 case M_BGE_I:
10317 s = "slti";
10318 s2 = "bteqz";
10319 s3 = "x,8";
10320 goto do_branch_i;
10321 case M_BGEU_I:
10322 s = "sltiu";
10323 s2 = "bteqz";
10324 s3 = "x,8";
10325 goto do_branch_i;
10326 case M_BGT_I:
10327 s = "slti";
10328 s2 = "bteqz";
10329 s3 = "x,8";
10330 goto do_addone_branch_i;
10331 case M_BGTU_I:
10332 s = "sltiu";
10333 s2 = "bteqz";
10334 s3 = "x,8";
10335
10336 do_addone_branch_i:
10337 if (imm_expr.X_op != O_constant)
10338 as_bad (_("Unsupported large constant"));
10339 ++imm_expr.X_add_number;
10340
10341 do_branch_i:
67c0d1eb
RS
10342 macro_build (&imm_expr, s, s3, xreg);
10343 macro_build (&offset_expr, s2, "p");
252b5132
RH
10344 break;
10345
10346 case M_ABS:
10347 expr1.X_add_number = 0;
67c0d1eb 10348 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 10349 if (xreg != yreg)
67c0d1eb 10350 move_register (xreg, yreg);
252b5132 10351 expr1.X_add_number = 2;
67c0d1eb
RS
10352 macro_build (&expr1, "bteqz", "p");
10353 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
10354 }
10355}
10356
10357/* For consistency checking, verify that all bits are specified either
10358 by the match/mask part of the instruction definition, or by the
10359 operand list. */
10360static int
17a2f251 10361validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
10362{
10363 const char *p = opc->args;
10364 char c;
10365 unsigned long used_bits = opc->mask;
10366
10367 if ((used_bits & opc->match) != opc->match)
10368 {
10369 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10370 opc->name, opc->args);
10371 return 0;
10372 }
10373#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
10374 while (*p)
10375 switch (c = *p++)
10376 {
10377 case ',': break;
10378 case '(': break;
10379 case ')': break;
af7ee8bf
CD
10380 case '+':
10381 switch (c = *p++)
10382 {
9bcd4f99
TS
10383 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
10384 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
10385 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
10386 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
af7ee8bf
CD
10387 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10388 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10389 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
10390 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
10391 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
10392 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10393 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10394 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10395 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10396 case 'I': break;
ef2e4d86
CF
10397 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10398 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
10399 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
bb35fb24
NC
10400 case 'x': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10401 case 'X': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10402 case 'p': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
10403 case 'P': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
dd3cbb7e 10404 case 'Q': USE_BITS (OP_MASK_SEQI, OP_SH_SEQI); break;
bb35fb24
NC
10405 case 's': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
10406 case 'S': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
98675402
RS
10407 case 'z': USE_BITS (OP_MASK_RZ, OP_SH_RZ); break;
10408 case 'Z': USE_BITS (OP_MASK_FZ, OP_SH_FZ); break;
10409 case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10410 case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10411 case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
bb35fb24 10412
af7ee8bf
CD
10413 default:
10414 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10415 c, opc->name, opc->args);
10416 return 0;
10417 }
10418 break;
252b5132
RH
10419 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10420 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10421 case 'A': break;
4372b673 10422 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
10423 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
10424 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10425 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10426 case 'F': break;
10427 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 10428 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 10429 case 'I': break;
e972090a 10430 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 10431 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10432 case 'L': break;
10433 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
10434 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
10435 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
10436 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
10437 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10438 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
10439 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10440 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10441 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10442 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
10443 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10444 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10445 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10446 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
10447 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10448 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
10449 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10450 case 'f': break;
10451 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
10452 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10453 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10454 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
10455 case 'l': break;
10456 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10457 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10458 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
10459 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10460 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10461 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10462 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10463 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10464 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10465 case 'x': break;
10466 case 'z': break;
10467 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
10468 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
10469 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
10470 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
10471 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
10472 case '[': break;
10473 case ']': break;
620edafd 10474 case '1': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8b082fb1 10475 case '2': USE_BITS (OP_MASK_BP, OP_SH_BP); break;
74cd071d
CF
10476 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
10477 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
10478 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
10479 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10480 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
10481 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
10482 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
10483 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
10484 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
10485 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
10486 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
10487 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
10488 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
10489 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
10490 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
dec0624d
MR
10491 case '\\': USE_BITS (OP_MASK_3BITPOS, OP_SH_3BITPOS); break;
10492 case '~': USE_BITS (OP_MASK_OFFSET12, OP_SH_OFFSET12); break;
ef2e4d86 10493 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10494 default:
10495 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10496 c, opc->name, opc->args);
10497 return 0;
10498 }
10499#undef USE_BITS
10500 if (used_bits != 0xffffffff)
10501 {
10502 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
10503 ~used_bits & 0xffffffff, opc->name, opc->args);
10504 return 0;
10505 }
10506 return 1;
10507}
10508
df58fc94
RS
10509/* For consistency checking, verify that the length implied matches the
10510 major opcode and that all bits are specified either by the match/mask
10511 part of the instruction definition, or by the operand list. */
10512
10513static int
10514validate_micromips_insn (const struct mips_opcode *opc)
10515{
10516 unsigned long match = opc->match;
10517 unsigned long mask = opc->mask;
10518 const char *p = opc->args;
10519 unsigned long insn_bits;
10520 unsigned long used_bits;
10521 unsigned long major;
10522 unsigned int length;
10523 char e;
10524 char c;
10525
10526 if ((mask & match) != match)
10527 {
10528 as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
10529 opc->name, opc->args);
10530 return 0;
10531 }
10532 length = micromips_insn_length (opc);
10533 if (length != 2 && length != 4)
10534 {
10535 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
10536 "%s %s"), length, opc->name, opc->args);
10537 return 0;
10538 }
10539 major = match >> (10 + 8 * (length - 2));
10540 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
10541 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
10542 {
10543 as_bad (_("Internal error: bad microMIPS opcode "
10544 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
10545 return 0;
10546 }
10547
10548 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
10549 insn_bits = 1 << 4 * length;
10550 insn_bits <<= 4 * length;
10551 insn_bits -= 1;
10552 used_bits = mask;
10553#define USE_BITS(field) \
10554 (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
10555 while (*p)
10556 switch (c = *p++)
10557 {
10558 case ',': break;
10559 case '(': break;
10560 case ')': break;
10561 case '+':
10562 e = c;
10563 switch (c = *p++)
10564 {
10565 case 'A': USE_BITS (EXTLSB); break;
10566 case 'B': USE_BITS (INSMSB); break;
10567 case 'C': USE_BITS (EXTMSBD); break;
10568 case 'D': USE_BITS (RS); USE_BITS (SEL); break;
10569 case 'E': USE_BITS (EXTLSB); break;
10570 case 'F': USE_BITS (INSMSB); break;
10571 case 'G': USE_BITS (EXTMSBD); break;
10572 case 'H': USE_BITS (EXTMSBD); break;
10573 default:
10574 as_bad (_("Internal error: bad mips opcode "
10575 "(unknown extension operand type `%c%c'): %s %s"),
10576 e, c, opc->name, opc->args);
10577 return 0;
10578 }
10579 break;
10580 case 'm':
10581 e = c;
10582 switch (c = *p++)
10583 {
10584 case 'A': USE_BITS (IMMA); break;
10585 case 'B': USE_BITS (IMMB); break;
10586 case 'C': USE_BITS (IMMC); break;
10587 case 'D': USE_BITS (IMMD); break;
10588 case 'E': USE_BITS (IMME); break;
10589 case 'F': USE_BITS (IMMF); break;
10590 case 'G': USE_BITS (IMMG); break;
10591 case 'H': USE_BITS (IMMH); break;
10592 case 'I': USE_BITS (IMMI); break;
10593 case 'J': USE_BITS (IMMJ); break;
10594 case 'L': USE_BITS (IMML); break;
10595 case 'M': USE_BITS (IMMM); break;
10596 case 'N': USE_BITS (IMMN); break;
10597 case 'O': USE_BITS (IMMO); break;
10598 case 'P': USE_BITS (IMMP); break;
10599 case 'Q': USE_BITS (IMMQ); break;
10600 case 'U': USE_BITS (IMMU); break;
10601 case 'W': USE_BITS (IMMW); break;
10602 case 'X': USE_BITS (IMMX); break;
10603 case 'Y': USE_BITS (IMMY); break;
10604 case 'Z': break;
10605 case 'a': break;
10606 case 'b': USE_BITS (MB); break;
10607 case 'c': USE_BITS (MC); break;
10608 case 'd': USE_BITS (MD); break;
10609 case 'e': USE_BITS (ME); break;
10610 case 'f': USE_BITS (MF); break;
10611 case 'g': USE_BITS (MG); break;
10612 case 'h': USE_BITS (MH); break;
10613 case 'i': USE_BITS (MI); break;
10614 case 'j': USE_BITS (MJ); break;
10615 case 'l': USE_BITS (ML); break;
10616 case 'm': USE_BITS (MM); break;
10617 case 'n': USE_BITS (MN); break;
10618 case 'p': USE_BITS (MP); break;
10619 case 'q': USE_BITS (MQ); break;
10620 case 'r': break;
10621 case 's': break;
10622 case 't': break;
10623 case 'x': break;
10624 case 'y': break;
10625 case 'z': break;
10626 default:
10627 as_bad (_("Internal error: bad mips opcode "
10628 "(unknown extension operand type `%c%c'): %s %s"),
10629 e, c, opc->name, opc->args);
10630 return 0;
10631 }
10632 break;
10633 case '.': USE_BITS (OFFSET10); break;
10634 case '1': USE_BITS (STYPE); break;
03f66e8a
MR
10635 case '2': USE_BITS (BP); break;
10636 case '3': USE_BITS (SA3); break;
10637 case '4': USE_BITS (SA4); break;
10638 case '5': USE_BITS (IMM8); break;
10639 case '6': USE_BITS (RS); break;
10640 case '7': USE_BITS (DSPACC); break;
10641 case '8': USE_BITS (WRDSP); break;
10642 case '0': USE_BITS (DSPSFT); break;
df58fc94
RS
10643 case '<': USE_BITS (SHAMT); break;
10644 case '>': USE_BITS (SHAMT); break;
03f66e8a 10645 case '@': USE_BITS (IMM10); break;
df58fc94
RS
10646 case 'B': USE_BITS (CODE10); break;
10647 case 'C': USE_BITS (COPZ); break;
10648 case 'D': USE_BITS (FD); break;
10649 case 'E': USE_BITS (RT); break;
10650 case 'G': USE_BITS (RS); break;
444d75be 10651 case 'H': USE_BITS (SEL); break;
df58fc94
RS
10652 case 'K': USE_BITS (RS); break;
10653 case 'M': USE_BITS (CCC); break;
10654 case 'N': USE_BITS (BCC); break;
10655 case 'R': USE_BITS (FR); break;
10656 case 'S': USE_BITS (FS); break;
10657 case 'T': USE_BITS (FT); break;
10658 case 'V': USE_BITS (FS); break;
dec0624d 10659 case '\\': USE_BITS (3BITPOS); break;
03f66e8a 10660 case '^': USE_BITS (RD); break;
df58fc94
RS
10661 case 'a': USE_BITS (TARGET); break;
10662 case 'b': USE_BITS (RS); break;
10663 case 'c': USE_BITS (CODE); break;
10664 case 'd': USE_BITS (RD); break;
10665 case 'h': USE_BITS (PREFX); break;
10666 case 'i': USE_BITS (IMMEDIATE); break;
10667 case 'j': USE_BITS (DELTA); break;
10668 case 'k': USE_BITS (CACHE); break;
10669 case 'n': USE_BITS (RT); break;
10670 case 'o': USE_BITS (DELTA); break;
10671 case 'p': USE_BITS (DELTA); break;
10672 case 'q': USE_BITS (CODE2); break;
10673 case 'r': USE_BITS (RS); break;
10674 case 's': USE_BITS (RS); break;
10675 case 't': USE_BITS (RT); break;
10676 case 'u': USE_BITS (IMMEDIATE); break;
10677 case 'v': USE_BITS (RS); break;
10678 case 'w': USE_BITS (RT); break;
10679 case 'y': USE_BITS (RS3); break;
10680 case 'z': break;
10681 case '|': USE_BITS (TRAP); break;
10682 case '~': USE_BITS (OFFSET12); break;
10683 default:
10684 as_bad (_("Internal error: bad microMIPS opcode "
10685 "(unknown operand type `%c'): %s %s"),
10686 c, opc->name, opc->args);
10687 return 0;
10688 }
10689#undef USE_BITS
10690 if (used_bits != insn_bits)
10691 {
10692 if (~used_bits & insn_bits)
10693 as_bad (_("Internal error: bad microMIPS opcode "
10694 "(bits 0x%lx undefined): %s %s"),
10695 ~used_bits & insn_bits, opc->name, opc->args);
10696 if (used_bits & ~insn_bits)
10697 as_bad (_("Internal error: bad microMIPS opcode "
10698 "(bits 0x%lx defined): %s %s"),
10699 used_bits & ~insn_bits, opc->name, opc->args);
10700 return 0;
10701 }
10702 return 1;
10703}
10704
9bcd4f99
TS
10705/* UDI immediates. */
10706struct mips_immed {
10707 char type;
10708 unsigned int shift;
10709 unsigned long mask;
10710 const char * desc;
10711};
10712
10713static const struct mips_immed mips_immed[] = {
10714 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
10715 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
10716 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
10717 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
10718 { 0,0,0,0 }
10719};
10720
7455baf8
TS
10721/* Check whether an odd floating-point register is allowed. */
10722static int
10723mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
10724{
10725 const char *s = insn->name;
10726
10727 if (insn->pinfo == INSN_MACRO)
10728 /* Let a macro pass, we'll catch it later when it is expanded. */
10729 return 1;
10730
e407c74b 10731 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || (mips_opts.arch == CPU_R5900))
7455baf8
TS
10732 {
10733 /* Allow odd registers for single-precision ops. */
10734 switch (insn->pinfo & (FP_S | FP_D))
10735 {
10736 case FP_S:
10737 case 0:
10738 return 1; /* both single precision - ok */
10739 case FP_D:
10740 return 0; /* both double precision - fail */
10741 default:
10742 break;
10743 }
10744
10745 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
10746 s = strchr (insn->name, '.');
10747 if (argnum == 2)
10748 s = s != NULL ? strchr (s + 1, '.') : NULL;
10749 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
10750 }
10751
10752 /* Single-precision coprocessor loads and moves are OK too. */
10753 if ((insn->pinfo & FP_S)
10754 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
10755 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
10756 return 1;
10757
10758 return 0;
10759}
10760
df58fc94
RS
10761/* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
10762 taking bits from BIT up. */
10763static int
10764expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
10765{
10766 return (ep->X_op == O_constant
10767 && (ep->X_add_number & ((1 << bit) - 1)) == 0
10768 && ep->X_add_number >= min << bit
10769 && ep->X_add_number < max << bit);
10770}
10771
252b5132
RH
10772/* This routine assembles an instruction into its binary format. As a
10773 side effect, it sets one of the global variables imm_reloc or
10774 offset_reloc to the type of relocation to do if one of the operands
10775 is an address expression. */
10776
10777static void
17a2f251 10778mips_ip (char *str, struct mips_cl_insn *ip)
252b5132 10779{
df58fc94
RS
10780 bfd_boolean wrong_delay_slot_insns = FALSE;
10781 bfd_boolean need_delay_slot_ok = TRUE;
10782 struct mips_opcode *firstinsn = NULL;
10783 const struct mips_opcode *past;
10784 struct hash_control *hash;
252b5132
RH
10785 char *s;
10786 const char *args;
43841e91 10787 char c = 0;
252b5132
RH
10788 struct mips_opcode *insn;
10789 char *argsStart;
10790 unsigned int regno;
34224acf 10791 unsigned int lastregno;
df58fc94 10792 unsigned int destregno = 0;
af7ee8bf 10793 unsigned int lastpos = 0;
071742cf 10794 unsigned int limlo, limhi;
252b5132 10795 char *s_reset;
74cd071d 10796 offsetT min_range, max_range;
df58fc94 10797 long opend;
a40bc9dd 10798 char *name;
707bfff6
TS
10799 int argnum;
10800 unsigned int rtype;
df58fc94 10801 char *dot;
a40bc9dd 10802 long end;
252b5132
RH
10803
10804 insn_error = NULL;
10805
df58fc94
RS
10806 if (mips_opts.micromips)
10807 {
10808 hash = micromips_op_hash;
10809 past = &micromips_opcodes[bfd_micromips_num_opcodes];
10810 }
10811 else
10812 {
10813 hash = op_hash;
10814 past = &mips_opcodes[NUMOPCODES];
10815 }
10816 forced_insn_length = 0;
252b5132 10817 insn = NULL;
252b5132 10818
df58fc94 10819 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
10820 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
10821 continue;
bdaaa2e1 10822
a40bc9dd
RS
10823 /* Make a copy of the instruction so that we can fiddle with it. */
10824 name = alloca (end + 1);
10825 memcpy (name, str, end);
10826 name[end] = '\0';
252b5132 10827
df58fc94
RS
10828 for (;;)
10829 {
10830 insn = (struct mips_opcode *) hash_find (hash, name);
10831
10832 if (insn != NULL || !mips_opts.micromips)
10833 break;
10834 if (forced_insn_length)
10835 break;
10836
10837 /* See if there's an instruction size override suffix,
10838 either `16' or `32', at the end of the mnemonic proper,
10839 that defines the operation, i.e. before the first `.'
10840 character if any. Strip it and retry. */
10841 dot = strchr (name, '.');
10842 opend = dot != NULL ? dot - name : end;
10843 if (opend < 3)
10844 break;
10845 if (name[opend - 2] == '1' && name[opend - 1] == '6')
10846 forced_insn_length = 2;
10847 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
10848 forced_insn_length = 4;
10849 else
10850 break;
10851 memcpy (name + opend - 2, name + opend, end - opend + 1);
10852 }
252b5132
RH
10853 if (insn == NULL)
10854 {
a40bc9dd
RS
10855 insn_error = _("Unrecognized opcode");
10856 return;
252b5132
RH
10857 }
10858
df58fc94
RS
10859 /* For microMIPS instructions placed in a fixed-length branch delay slot
10860 we make up to two passes over the relevant fragment of the opcode
10861 table. First we try instructions that meet the delay slot's length
10862 requirement. If none matched, then we retry with the remaining ones
10863 and if one matches, then we use it and then issue an appropriate
10864 warning later on. */
a40bc9dd 10865 argsStart = s = str + end;
252b5132
RH
10866 for (;;)
10867 {
df58fc94
RS
10868 bfd_boolean delay_slot_ok;
10869 bfd_boolean size_ok;
b34976b6 10870 bfd_boolean ok;
252b5132 10871
a40bc9dd 10872 gas_assert (strcmp (insn->name, name) == 0);
252b5132 10873
f79e2745 10874 ok = is_opcode_valid (insn);
df58fc94
RS
10875 size_ok = is_size_valid (insn);
10876 delay_slot_ok = is_delay_slot_valid (insn);
10877 if (!delay_slot_ok && !wrong_delay_slot_insns)
252b5132 10878 {
df58fc94
RS
10879 firstinsn = insn;
10880 wrong_delay_slot_insns = TRUE;
10881 }
10882 if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
10883 {
10884 static char buf[256];
10885
10886 if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
252b5132
RH
10887 {
10888 ++insn;
10889 continue;
10890 }
df58fc94 10891 if (wrong_delay_slot_insns && need_delay_slot_ok)
beae10d5 10892 {
df58fc94
RS
10893 gas_assert (firstinsn);
10894 need_delay_slot_ok = FALSE;
10895 past = insn + 1;
10896 insn = firstinsn;
10897 continue;
252b5132 10898 }
df58fc94
RS
10899
10900 if (insn_error)
10901 return;
10902
10903 if (!ok)
7bd942df 10904 sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
df58fc94
RS
10905 mips_cpu_info_from_arch (mips_opts.arch)->name,
10906 mips_cpu_info_from_isa (mips_opts.isa)->name);
10907 else
10908 sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
10909 8 * forced_insn_length);
10910 insn_error = buf;
10911
10912 return;
252b5132
RH
10913 }
10914
1e915849 10915 create_insn (ip, insn);
268f6bed 10916 insn_error = NULL;
707bfff6 10917 argnum = 1;
24864476 10918 lastregno = 0xffffffff;
252b5132
RH
10919 for (args = insn->args;; ++args)
10920 {
deec1734
CD
10921 int is_mdmx;
10922
ad8d3bb3 10923 s += strspn (s, " \t");
deec1734 10924 is_mdmx = 0;
252b5132
RH
10925 switch (*args)
10926 {
10927 case '\0': /* end of args */
10928 if (*s == '\0')
10929 return;
10930 break;
10931
03f66e8a
MR
10932 case '2':
10933 /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
10934 code) or 14 (for microMIPS code). */
8b082fb1
TS
10935 my_getExpression (&imm_expr, s);
10936 check_absolute_expr (ip, &imm_expr);
10937 if ((unsigned long) imm_expr.X_add_number != 1
10938 && (unsigned long) imm_expr.X_add_number != 3)
10939 {
10940 as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
10941 (unsigned long) imm_expr.X_add_number);
10942 }
03f66e8a
MR
10943 INSERT_OPERAND (mips_opts.micromips,
10944 BP, *ip, imm_expr.X_add_number);
8b082fb1
TS
10945 imm_expr.X_op = O_absent;
10946 s = expr_end;
10947 continue;
10948
03f66e8a
MR
10949 case '3':
10950 /* DSP 3-bit unsigned immediate in bit 13 (for standard MIPS
10951 code) or 21 (for microMIPS code). */
10952 {
10953 unsigned long mask = (mips_opts.micromips
10954 ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
10955
10956 my_getExpression (&imm_expr, s);
10957 check_absolute_expr (ip, &imm_expr);
10958 if ((unsigned long) imm_expr.X_add_number > mask)
10959 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10960 mask, (unsigned long) imm_expr.X_add_number);
10961 INSERT_OPERAND (mips_opts.micromips,
10962 SA3, *ip, imm_expr.X_add_number);
10963 imm_expr.X_op = O_absent;
10964 s = expr_end;
10965 }
74cd071d
CF
10966 continue;
10967
03f66e8a
MR
10968 case '4':
10969 /* DSP 4-bit unsigned immediate in bit 12 (for standard MIPS
10970 code) or 21 (for microMIPS code). */
10971 {
10972 unsigned long mask = (mips_opts.micromips
10973 ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
10974
10975 my_getExpression (&imm_expr, s);
10976 check_absolute_expr (ip, &imm_expr);
10977 if ((unsigned long) imm_expr.X_add_number > mask)
10978 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10979 mask, (unsigned long) imm_expr.X_add_number);
10980 INSERT_OPERAND (mips_opts.micromips,
10981 SA4, *ip, imm_expr.X_add_number);
10982 imm_expr.X_op = O_absent;
10983 s = expr_end;
10984 }
74cd071d
CF
10985 continue;
10986
03f66e8a
MR
10987 case '5':
10988 /* DSP 8-bit unsigned immediate in bit 13 (for standard MIPS
10989 code) or 16 (for microMIPS code). */
10990 {
10991 unsigned long mask = (mips_opts.micromips
10992 ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
10993
10994 my_getExpression (&imm_expr, s);
10995 check_absolute_expr (ip, &imm_expr);
10996 if ((unsigned long) imm_expr.X_add_number > mask)
10997 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10998 mask, (unsigned long) imm_expr.X_add_number);
10999 INSERT_OPERAND (mips_opts.micromips,
11000 IMM8, *ip, imm_expr.X_add_number);
11001 imm_expr.X_op = O_absent;
11002 s = expr_end;
11003 }
74cd071d
CF
11004 continue;
11005
03f66e8a
MR
11006 case '6':
11007 /* DSP 5-bit unsigned immediate in bit 16 (for standard MIPS
11008 code) or 21 (for microMIPS code). */
11009 {
11010 unsigned long mask = (mips_opts.micromips
11011 ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
11012
11013 my_getExpression (&imm_expr, s);
11014 check_absolute_expr (ip, &imm_expr);
11015 if ((unsigned long) imm_expr.X_add_number > mask)
11016 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11017 mask, (unsigned long) imm_expr.X_add_number);
11018 INSERT_OPERAND (mips_opts.micromips,
11019 RS, *ip, imm_expr.X_add_number);
11020 imm_expr.X_op = O_absent;
11021 s = expr_end;
11022 }
74cd071d
CF
11023 continue;
11024
90ecf173 11025 case '7': /* Four DSP accumulators in bits 11,12. */
03f66e8a
MR
11026 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11027 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
11028 {
11029 regno = s[3] - '0';
11030 s += 4;
03f66e8a 11031 INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
74cd071d
CF
11032 continue;
11033 }
11034 else
11035 as_bad (_("Invalid dsp acc register"));
11036 break;
11037
03f66e8a
MR
11038 case '8':
11039 /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
11040 code) or 14 (for microMIPS code). */
11041 {
11042 unsigned long mask = (mips_opts.micromips
11043 ? MICROMIPSOP_MASK_WRDSP
11044 : OP_MASK_WRDSP);
11045
11046 my_getExpression (&imm_expr, s);
11047 check_absolute_expr (ip, &imm_expr);
11048 if ((unsigned long) imm_expr.X_add_number > mask)
11049 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11050 mask, (unsigned long) imm_expr.X_add_number);
11051 INSERT_OPERAND (mips_opts.micromips,
11052 WRDSP, *ip, imm_expr.X_add_number);
11053 imm_expr.X_op = O_absent;
11054 s = expr_end;
11055 }
74cd071d
CF
11056 continue;
11057
90ecf173 11058 case '9': /* Four DSP accumulators in bits 21,22. */
df58fc94 11059 gas_assert (!mips_opts.micromips);
03f66e8a
MR
11060 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11061 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
11062 {
11063 regno = s[3] - '0';
11064 s += 4;
df58fc94 11065 INSERT_OPERAND (0, DSPACC_S, *ip, regno);
74cd071d
CF
11066 continue;
11067 }
11068 else
11069 as_bad (_("Invalid dsp acc register"));
11070 break;
11071
03f66e8a
MR
11072 case '0':
11073 /* DSP 6-bit signed immediate in bit 16 (for standard MIPS
11074 code) or 20 (for microMIPS code). */
11075 {
11076 long mask = (mips_opts.micromips
11077 ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
11078
11079 my_getExpression (&imm_expr, s);
11080 check_absolute_expr (ip, &imm_expr);
11081 min_range = -((mask + 1) >> 1);
11082 max_range = ((mask + 1) >> 1) - 1;
11083 if (imm_expr.X_add_number < min_range
11084 || imm_expr.X_add_number > max_range)
a9e24354
TS
11085 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11086 (long) min_range, (long) max_range,
11087 (long) imm_expr.X_add_number);
03f66e8a
MR
11088 INSERT_OPERAND (mips_opts.micromips,
11089 DSPSFT, *ip, imm_expr.X_add_number);
11090 imm_expr.X_op = O_absent;
11091 s = expr_end;
11092 }
74cd071d
CF
11093 continue;
11094
90ecf173 11095 case '\'': /* DSP 6-bit unsigned immediate in bit 16. */
df58fc94 11096 gas_assert (!mips_opts.micromips);
74cd071d
CF
11097 my_getExpression (&imm_expr, s);
11098 check_absolute_expr (ip, &imm_expr);
11099 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
11100 {
a9e24354
TS
11101 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11102 OP_MASK_RDDSP,
11103 (unsigned long) imm_expr.X_add_number);
74cd071d 11104 }
df58fc94 11105 INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
11106 imm_expr.X_op = O_absent;
11107 s = expr_end;
11108 continue;
11109
90ecf173 11110 case ':': /* DSP 7-bit signed immediate in bit 19. */
df58fc94 11111 gas_assert (!mips_opts.micromips);
74cd071d
CF
11112 my_getExpression (&imm_expr, s);
11113 check_absolute_expr (ip, &imm_expr);
11114 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
11115 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
11116 if (imm_expr.X_add_number < min_range ||
11117 imm_expr.X_add_number > max_range)
11118 {
a9e24354
TS
11119 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11120 (long) min_range, (long) max_range,
11121 (long) imm_expr.X_add_number);
74cd071d 11122 }
df58fc94 11123 INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
74cd071d
CF
11124 imm_expr.X_op = O_absent;
11125 s = expr_end;
11126 continue;
11127
90ecf173 11128 case '@': /* DSP 10-bit signed immediate in bit 16. */
03f66e8a
MR
11129 {
11130 long mask = (mips_opts.micromips
11131 ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
11132
11133 my_getExpression (&imm_expr, s);
11134 check_absolute_expr (ip, &imm_expr);
11135 min_range = -((mask + 1) >> 1);
11136 max_range = ((mask + 1) >> 1) - 1;
11137 if (imm_expr.X_add_number < min_range
11138 || imm_expr.X_add_number > max_range)
a9e24354
TS
11139 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11140 (long) min_range, (long) max_range,
11141 (long) imm_expr.X_add_number);
03f66e8a
MR
11142 INSERT_OPERAND (mips_opts.micromips,
11143 IMM10, *ip, imm_expr.X_add_number);
11144 imm_expr.X_op = O_absent;
11145 s = expr_end;
11146 }
11147 continue;
11148
11149 case '^': /* DSP 5-bit unsigned immediate in bit 11. */
11150 gas_assert (mips_opts.micromips);
11151 my_getExpression (&imm_expr, s);
11152 check_absolute_expr (ip, &imm_expr);
11153 if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11154 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11155 MICROMIPSOP_MASK_RD,
11156 (unsigned long) imm_expr.X_add_number);
11157 INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
74cd071d
CF
11158 imm_expr.X_op = O_absent;
11159 s = expr_end;
11160 continue;
11161
a9e24354 11162 case '!': /* MT usermode flag bit. */
df58fc94 11163 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11164 my_getExpression (&imm_expr, s);
11165 check_absolute_expr (ip, &imm_expr);
11166 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
a9e24354
TS
11167 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11168 (unsigned long) imm_expr.X_add_number);
df58fc94 11169 INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11170 imm_expr.X_op = O_absent;
11171 s = expr_end;
11172 continue;
11173
a9e24354 11174 case '$': /* MT load high flag bit. */
df58fc94 11175 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11176 my_getExpression (&imm_expr, s);
11177 check_absolute_expr (ip, &imm_expr);
11178 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
a9e24354
TS
11179 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11180 (unsigned long) imm_expr.X_add_number);
df58fc94 11181 INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11182 imm_expr.X_op = O_absent;
11183 s = expr_end;
11184 continue;
11185
90ecf173 11186 case '*': /* Four DSP accumulators in bits 18,19. */
df58fc94 11187 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11188 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11189 s[3] >= '0' && s[3] <= '3')
11190 {
11191 regno = s[3] - '0';
11192 s += 4;
df58fc94 11193 INSERT_OPERAND (0, MTACC_T, *ip, regno);
ef2e4d86
CF
11194 continue;
11195 }
11196 else
11197 as_bad (_("Invalid dsp/smartmips acc register"));
11198 break;
11199
90ecf173 11200 case '&': /* Four DSP accumulators in bits 13,14. */
df58fc94 11201 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11202 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11203 s[3] >= '0' && s[3] <= '3')
11204 {
11205 regno = s[3] - '0';
11206 s += 4;
df58fc94 11207 INSERT_OPERAND (0, MTACC_D, *ip, regno);
ef2e4d86
CF
11208 continue;
11209 }
11210 else
11211 as_bad (_("Invalid dsp/smartmips acc register"));
11212 break;
11213
dec0624d
MR
11214 case '\\': /* 3-bit bit position. */
11215 {
2906b037
MR
11216 unsigned long mask = (mips_opts.micromips
11217 ? MICROMIPSOP_MASK_3BITPOS
11218 : OP_MASK_3BITPOS);
dec0624d
MR
11219
11220 my_getExpression (&imm_expr, s);
11221 check_absolute_expr (ip, &imm_expr);
11222 if ((unsigned long) imm_expr.X_add_number > mask)
11223 as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11224 ip->insn_mo->name,
11225 mask, (unsigned long) imm_expr.X_add_number);
11226 INSERT_OPERAND (mips_opts.micromips,
11227 3BITPOS, *ip, imm_expr.X_add_number);
11228 imm_expr.X_op = O_absent;
11229 s = expr_end;
11230 }
11231 continue;
11232
252b5132 11233 case ',':
a339155f 11234 ++argnum;
252b5132
RH
11235 if (*s++ == *args)
11236 continue;
11237 s--;
11238 switch (*++args)
11239 {
11240 case 'r':
11241 case 'v':
df58fc94 11242 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132
RH
11243 continue;
11244
11245 case 'w':
df58fc94 11246 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
38487616
TS
11247 continue;
11248
252b5132 11249 case 'W':
df58fc94
RS
11250 gas_assert (!mips_opts.micromips);
11251 INSERT_OPERAND (0, FT, *ip, lastregno);
252b5132
RH
11252 continue;
11253
11254 case 'V':
df58fc94 11255 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132
RH
11256 continue;
11257 }
11258 break;
11259
11260 case '(':
11261 /* Handle optional base register.
11262 Either the base register is omitted or
bdaaa2e1 11263 we must have a left paren. */
252b5132
RH
11264 /* This is dependent on the next operand specifier
11265 is a base register specification. */
df58fc94
RS
11266 gas_assert (args[1] == 'b'
11267 || (mips_opts.micromips
11268 && args[1] == 'm'
11269 && (args[2] == 'l' || args[2] == 'n'
11270 || args[2] == 's' || args[2] == 'a')));
11271 if (*s == '\0' && args[1] == 'b')
252b5132 11272 return;
df58fc94 11273 /* Fall through. */
252b5132 11274
90ecf173 11275 case ')': /* These must match exactly. */
df58fc94
RS
11276 if (*s++ == *args)
11277 continue;
11278 break;
11279
11280 case '[': /* These must match exactly. */
60b63b72 11281 case ']':
df58fc94 11282 gas_assert (!mips_opts.micromips);
252b5132
RH
11283 if (*s++ == *args)
11284 continue;
11285 break;
11286
af7ee8bf
CD
11287 case '+': /* Opcode extension character. */
11288 switch (*++args)
11289 {
9bcd4f99
TS
11290 case '1': /* UDI immediates. */
11291 case '2':
11292 case '3':
11293 case '4':
df58fc94 11294 gas_assert (!mips_opts.micromips);
9bcd4f99
TS
11295 {
11296 const struct mips_immed *imm = mips_immed;
11297
11298 while (imm->type && imm->type != *args)
11299 ++imm;
11300 if (! imm->type)
b37df7c4 11301 abort ();
9bcd4f99
TS
11302 my_getExpression (&imm_expr, s);
11303 check_absolute_expr (ip, &imm_expr);
11304 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11305 {
11306 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11307 imm->desc ? imm->desc : ip->insn_mo->name,
11308 (unsigned long) imm_expr.X_add_number,
11309 (unsigned long) imm_expr.X_add_number);
90ecf173 11310 imm_expr.X_add_number &= imm->mask;
9bcd4f99
TS
11311 }
11312 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11313 << imm->shift);
11314 imm_expr.X_op = O_absent;
11315 s = expr_end;
11316 }
11317 continue;
90ecf173 11318
071742cf
CD
11319 case 'A': /* ins/ext position, becomes LSB. */
11320 limlo = 0;
11321 limhi = 31;
5f74bc13
CD
11322 goto do_lsb;
11323 case 'E':
11324 limlo = 32;
11325 limhi = 63;
11326 goto do_lsb;
90ecf173 11327 do_lsb:
071742cf
CD
11328 my_getExpression (&imm_expr, s);
11329 check_absolute_expr (ip, &imm_expr);
11330 if ((unsigned long) imm_expr.X_add_number < limlo
11331 || (unsigned long) imm_expr.X_add_number > limhi)
11332 {
11333 as_bad (_("Improper position (%lu)"),
11334 (unsigned long) imm_expr.X_add_number);
11335 imm_expr.X_add_number = limlo;
11336 }
11337 lastpos = imm_expr.X_add_number;
df58fc94
RS
11338 INSERT_OPERAND (mips_opts.micromips,
11339 EXTLSB, *ip, imm_expr.X_add_number);
071742cf
CD
11340 imm_expr.X_op = O_absent;
11341 s = expr_end;
11342 continue;
11343
11344 case 'B': /* ins size, becomes MSB. */
11345 limlo = 1;
11346 limhi = 32;
5f74bc13
CD
11347 goto do_msb;
11348 case 'F':
11349 limlo = 33;
11350 limhi = 64;
11351 goto do_msb;
90ecf173 11352 do_msb:
071742cf
CD
11353 my_getExpression (&imm_expr, s);
11354 check_absolute_expr (ip, &imm_expr);
11355 /* Check for negative input so that small negative numbers
11356 will not succeed incorrectly. The checks against
11357 (pos+size) transitively check "size" itself,
11358 assuming that "pos" is reasonable. */
11359 if ((long) imm_expr.X_add_number < 0
11360 || ((unsigned long) imm_expr.X_add_number
11361 + lastpos) < limlo
11362 || ((unsigned long) imm_expr.X_add_number
11363 + lastpos) > limhi)
11364 {
11365 as_bad (_("Improper insert size (%lu, position %lu)"),
11366 (unsigned long) imm_expr.X_add_number,
11367 (unsigned long) lastpos);
11368 imm_expr.X_add_number = limlo - lastpos;
11369 }
df58fc94
RS
11370 INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11371 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
11372 imm_expr.X_op = O_absent;
11373 s = expr_end;
11374 continue;
11375
11376 case 'C': /* ext size, becomes MSBD. */
11377 limlo = 1;
11378 limhi = 32;
5f74bc13
CD
11379 goto do_msbd;
11380 case 'G':
11381 limlo = 33;
11382 limhi = 64;
11383 goto do_msbd;
11384 case 'H':
11385 limlo = 33;
11386 limhi = 64;
11387 goto do_msbd;
90ecf173 11388 do_msbd:
071742cf
CD
11389 my_getExpression (&imm_expr, s);
11390 check_absolute_expr (ip, &imm_expr);
11391 /* Check for negative input so that small negative numbers
11392 will not succeed incorrectly. The checks against
11393 (pos+size) transitively check "size" itself,
11394 assuming that "pos" is reasonable. */
11395 if ((long) imm_expr.X_add_number < 0
11396 || ((unsigned long) imm_expr.X_add_number
11397 + lastpos) < limlo
11398 || ((unsigned long) imm_expr.X_add_number
11399 + lastpos) > limhi)
11400 {
11401 as_bad (_("Improper extract size (%lu, position %lu)"),
11402 (unsigned long) imm_expr.X_add_number,
11403 (unsigned long) lastpos);
11404 imm_expr.X_add_number = limlo - lastpos;
11405 }
df58fc94
RS
11406 INSERT_OPERAND (mips_opts.micromips,
11407 EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
11408 imm_expr.X_op = O_absent;
11409 s = expr_end;
11410 continue;
af7ee8bf 11411
bbcc0807
CD
11412 case 'D':
11413 /* +D is for disassembly only; never match. */
11414 break;
11415
5f74bc13
CD
11416 case 'I':
11417 /* "+I" is like "I", except that imm2_expr is used. */
11418 my_getExpression (&imm2_expr, s);
11419 if (imm2_expr.X_op != O_big
11420 && imm2_expr.X_op != O_constant)
11421 insn_error = _("absolute expression required");
9ee2a2d4
MR
11422 if (HAVE_32BIT_GPRS)
11423 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
11424 s = expr_end;
11425 continue;
11426
707bfff6 11427 case 'T': /* Coprocessor register. */
df58fc94 11428 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11429 /* +T is for disassembly only; never match. */
11430 break;
11431
707bfff6 11432 case 't': /* Coprocessor register number. */
df58fc94 11433 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11434 if (s[0] == '$' && ISDIGIT (s[1]))
11435 {
11436 ++s;
11437 regno = 0;
11438 do
11439 {
11440 regno *= 10;
11441 regno += *s - '0';
11442 ++s;
11443 }
11444 while (ISDIGIT (*s));
11445 if (regno > 31)
11446 as_bad (_("Invalid register number (%d)"), regno);
11447 else
11448 {
df58fc94 11449 INSERT_OPERAND (0, RT, *ip, regno);
ef2e4d86
CF
11450 continue;
11451 }
11452 }
11453 else
11454 as_bad (_("Invalid coprocessor 0 register number"));
11455 break;
11456
bb35fb24
NC
11457 case 'x':
11458 /* bbit[01] and bbit[01]32 bit index. Give error if index
11459 is not in the valid range. */
df58fc94 11460 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11461 my_getExpression (&imm_expr, s);
11462 check_absolute_expr (ip, &imm_expr);
11463 if ((unsigned) imm_expr.X_add_number > 31)
11464 {
11465 as_bad (_("Improper bit index (%lu)"),
11466 (unsigned long) imm_expr.X_add_number);
11467 imm_expr.X_add_number = 0;
11468 }
df58fc94 11469 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
bb35fb24
NC
11470 imm_expr.X_op = O_absent;
11471 s = expr_end;
11472 continue;
11473
11474 case 'X':
11475 /* bbit[01] bit index when bbit is used but we generate
11476 bbit[01]32 because the index is over 32. Move to the
11477 next candidate if index is not in the valid range. */
df58fc94 11478 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11479 my_getExpression (&imm_expr, s);
11480 check_absolute_expr (ip, &imm_expr);
11481 if ((unsigned) imm_expr.X_add_number < 32
11482 || (unsigned) imm_expr.X_add_number > 63)
11483 break;
df58fc94 11484 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
11485 imm_expr.X_op = O_absent;
11486 s = expr_end;
11487 continue;
11488
11489 case 'p':
11490 /* cins, cins32, exts and exts32 position field. Give error
11491 if it's not in the valid range. */
df58fc94 11492 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11493 my_getExpression (&imm_expr, s);
11494 check_absolute_expr (ip, &imm_expr);
11495 if ((unsigned) imm_expr.X_add_number > 31)
11496 {
11497 as_bad (_("Improper position (%lu)"),
11498 (unsigned long) imm_expr.X_add_number);
11499 imm_expr.X_add_number = 0;
11500 }
11501 /* Make the pos explicit to simplify +S. */
11502 lastpos = imm_expr.X_add_number + 32;
df58fc94 11503 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
bb35fb24
NC
11504 imm_expr.X_op = O_absent;
11505 s = expr_end;
11506 continue;
11507
11508 case 'P':
11509 /* cins, cins32, exts and exts32 position field. Move to
11510 the next candidate if it's not in the valid range. */
df58fc94 11511 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11512 my_getExpression (&imm_expr, s);
11513 check_absolute_expr (ip, &imm_expr);
11514 if ((unsigned) imm_expr.X_add_number < 32
11515 || (unsigned) imm_expr.X_add_number > 63)
11516 break;
11517 lastpos = imm_expr.X_add_number;
df58fc94 11518 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
11519 imm_expr.X_op = O_absent;
11520 s = expr_end;
11521 continue;
11522
11523 case 's':
11524 /* cins and exts length-minus-one field. */
df58fc94 11525 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11526 my_getExpression (&imm_expr, s);
11527 check_absolute_expr (ip, &imm_expr);
11528 if ((unsigned long) imm_expr.X_add_number > 31)
11529 {
11530 as_bad (_("Improper size (%lu)"),
11531 (unsigned long) imm_expr.X_add_number);
11532 imm_expr.X_add_number = 0;
11533 }
df58fc94 11534 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
11535 imm_expr.X_op = O_absent;
11536 s = expr_end;
11537 continue;
11538
11539 case 'S':
11540 /* cins32/exts32 and cins/exts aliasing cint32/exts32
11541 length-minus-one field. */
df58fc94 11542 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11543 my_getExpression (&imm_expr, s);
11544 check_absolute_expr (ip, &imm_expr);
11545 if ((long) imm_expr.X_add_number < 0
11546 || (unsigned long) imm_expr.X_add_number + lastpos > 63)
11547 {
11548 as_bad (_("Improper size (%lu)"),
11549 (unsigned long) imm_expr.X_add_number);
11550 imm_expr.X_add_number = 0;
11551 }
df58fc94 11552 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
11553 imm_expr.X_op = O_absent;
11554 s = expr_end;
11555 continue;
11556
dd3cbb7e
NC
11557 case 'Q':
11558 /* seqi/snei immediate field. */
df58fc94 11559 gas_assert (!mips_opts.micromips);
dd3cbb7e
NC
11560 my_getExpression (&imm_expr, s);
11561 check_absolute_expr (ip, &imm_expr);
11562 if ((long) imm_expr.X_add_number < -512
11563 || (long) imm_expr.X_add_number >= 512)
11564 {
11565 as_bad (_("Improper immediate (%ld)"),
11566 (long) imm_expr.X_add_number);
11567 imm_expr.X_add_number = 0;
11568 }
df58fc94 11569 INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
dd3cbb7e
NC
11570 imm_expr.X_op = O_absent;
11571 s = expr_end;
11572 continue;
11573
98675402 11574 case 'a': /* 8-bit signed offset in bit 6 */
df58fc94 11575 gas_assert (!mips_opts.micromips);
98675402
RS
11576 my_getExpression (&imm_expr, s);
11577 check_absolute_expr (ip, &imm_expr);
11578 min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
11579 max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
11580 if (imm_expr.X_add_number < min_range
11581 || imm_expr.X_add_number > max_range)
11582 {
c95354ed 11583 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11584 (long) min_range, (long) max_range,
11585 (long) imm_expr.X_add_number);
11586 }
df58fc94 11587 INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
98675402
RS
11588 imm_expr.X_op = O_absent;
11589 s = expr_end;
11590 continue;
11591
11592 case 'b': /* 8-bit signed offset in bit 3 */
df58fc94 11593 gas_assert (!mips_opts.micromips);
98675402
RS
11594 my_getExpression (&imm_expr, s);
11595 check_absolute_expr (ip, &imm_expr);
11596 min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
11597 max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
11598 if (imm_expr.X_add_number < min_range
11599 || imm_expr.X_add_number > max_range)
11600 {
c95354ed 11601 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11602 (long) min_range, (long) max_range,
11603 (long) imm_expr.X_add_number);
11604 }
df58fc94 11605 INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
98675402
RS
11606 imm_expr.X_op = O_absent;
11607 s = expr_end;
11608 continue;
11609
11610 case 'c': /* 9-bit signed offset in bit 6 */
df58fc94 11611 gas_assert (!mips_opts.micromips);
98675402
RS
11612 my_getExpression (&imm_expr, s);
11613 check_absolute_expr (ip, &imm_expr);
11614 min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
11615 max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
c95354ed
MX
11616 /* We check the offset range before adjusted. */
11617 min_range <<= 4;
11618 max_range <<= 4;
98675402
RS
11619 if (imm_expr.X_add_number < min_range
11620 || imm_expr.X_add_number > max_range)
11621 {
c95354ed 11622 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11623 (long) min_range, (long) max_range,
11624 (long) imm_expr.X_add_number);
11625 }
c95354ed
MX
11626 if (imm_expr.X_add_number & 0xf)
11627 {
11628 as_bad (_("Offset not 16 bytes alignment (%ld)"),
11629 (long) imm_expr.X_add_number);
11630 }
11631 /* Right shift 4 bits to adjust the offset operand. */
df58fc94
RS
11632 INSERT_OPERAND (0, OFFSET_C, *ip,
11633 imm_expr.X_add_number >> 4);
98675402
RS
11634 imm_expr.X_op = O_absent;
11635 s = expr_end;
11636 continue;
11637
11638 case 'z':
df58fc94 11639 gas_assert (!mips_opts.micromips);
98675402
RS
11640 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
11641 break;
11642 if (regno == AT && mips_opts.at)
11643 {
11644 if (mips_opts.at == ATREG)
11645 as_warn (_("used $at without \".set noat\""));
11646 else
11647 as_warn (_("used $%u with \".set at=$%u\""),
11648 regno, mips_opts.at);
11649 }
df58fc94 11650 INSERT_OPERAND (0, RZ, *ip, regno);
98675402
RS
11651 continue;
11652
11653 case 'Z':
df58fc94 11654 gas_assert (!mips_opts.micromips);
98675402
RS
11655 if (!reg_lookup (&s, RTYPE_FPU, &regno))
11656 break;
df58fc94 11657 INSERT_OPERAND (0, FZ, *ip, regno);
98675402
RS
11658 continue;
11659
af7ee8bf 11660 default:
df58fc94 11661 as_bad (_("Internal error: bad %s opcode "
90ecf173 11662 "(unknown extension operand type `+%c'): %s %s"),
df58fc94 11663 mips_opts.micromips ? "microMIPS" : "MIPS",
90ecf173 11664 *args, insn->name, insn->args);
af7ee8bf
CD
11665 /* Further processing is fruitless. */
11666 return;
11667 }
11668 break;
11669
df58fc94 11670 case '.': /* 10-bit offset. */
df58fc94 11671 gas_assert (mips_opts.micromips);
dec0624d 11672 case '~': /* 12-bit offset. */
df58fc94
RS
11673 {
11674 int shift = *args == '.' ? 9 : 11;
11675 size_t i;
11676
11677 /* Check whether there is only a single bracketed expression
11678 left. If so, it must be the base register and the
11679 constant must be zero. */
11680 if (*s == '(' && strchr (s + 1, '(') == 0)
11681 continue;
11682
11683 /* If this value won't fit into the offset, then go find
11684 a macro that will generate a 16- or 32-bit offset code
11685 pattern. */
11686 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11687 if ((i == 0 && (imm_expr.X_op != O_constant
11688 || imm_expr.X_add_number >= 1 << shift
11689 || imm_expr.X_add_number < -1 << shift))
11690 || i > 0)
11691 {
11692 imm_expr.X_op = O_absent;
11693 break;
11694 }
11695 if (shift == 9)
11696 INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
11697 else
dec0624d
MR
11698 INSERT_OPERAND (mips_opts.micromips,
11699 OFFSET12, *ip, imm_expr.X_add_number);
df58fc94
RS
11700 imm_expr.X_op = O_absent;
11701 s = expr_end;
11702 }
11703 continue;
11704
252b5132
RH
11705 case '<': /* must be at least one digit */
11706 /*
11707 * According to the manual, if the shift amount is greater
b6ff326e
KH
11708 * than 31 or less than 0, then the shift amount should be
11709 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
11710 * We issue a warning and mask out all but the low 5 bits.
11711 */
11712 my_getExpression (&imm_expr, s);
11713 check_absolute_expr (ip, &imm_expr);
11714 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
11715 as_warn (_("Improper shift amount (%lu)"),
11716 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11717 INSERT_OPERAND (mips_opts.micromips,
11718 SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
11719 imm_expr.X_op = O_absent;
11720 s = expr_end;
11721 continue;
11722
11723 case '>': /* shift amount minus 32 */
11724 my_getExpression (&imm_expr, s);
11725 check_absolute_expr (ip, &imm_expr);
11726 if ((unsigned long) imm_expr.X_add_number < 32
11727 || (unsigned long) imm_expr.X_add_number > 63)
11728 break;
df58fc94
RS
11729 INSERT_OPERAND (mips_opts.micromips,
11730 SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
11731 imm_expr.X_op = O_absent;
11732 s = expr_end;
11733 continue;
11734
90ecf173
MR
11735 case 'k': /* CACHE code. */
11736 case 'h': /* PREFX code. */
11737 case '1': /* SYNC type. */
252b5132
RH
11738 my_getExpression (&imm_expr, s);
11739 check_absolute_expr (ip, &imm_expr);
11740 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
11741 as_warn (_("Invalid value for `%s' (%lu)"),
11742 ip->insn_mo->name,
11743 (unsigned long) imm_expr.X_add_number);
df58fc94 11744 switch (*args)
d954098f 11745 {
df58fc94
RS
11746 case 'k':
11747 if (mips_fix_cn63xxp1
11748 && !mips_opts.micromips
11749 && strcmp ("pref", insn->name) == 0)
d954098f
DD
11750 switch (imm_expr.X_add_number)
11751 {
11752 case 5:
11753 case 25:
11754 case 26:
11755 case 27:
11756 case 28:
11757 case 29:
11758 case 30:
11759 case 31: /* These are ok. */
11760 break;
11761
11762 default: /* The rest must be changed to 28. */
11763 imm_expr.X_add_number = 28;
11764 break;
11765 }
df58fc94
RS
11766 INSERT_OPERAND (mips_opts.micromips,
11767 CACHE, *ip, imm_expr.X_add_number);
11768 break;
11769 case 'h':
11770 INSERT_OPERAND (mips_opts.micromips,
11771 PREFX, *ip, imm_expr.X_add_number);
11772 break;
11773 case '1':
11774 INSERT_OPERAND (mips_opts.micromips,
11775 STYPE, *ip, imm_expr.X_add_number);
11776 break;
d954098f 11777 }
252b5132
RH
11778 imm_expr.X_op = O_absent;
11779 s = expr_end;
11780 continue;
11781
90ecf173 11782 case 'c': /* BREAK code. */
df58fc94
RS
11783 {
11784 unsigned long mask = (mips_opts.micromips
11785 ? MICROMIPSOP_MASK_CODE
11786 : OP_MASK_CODE);
11787
11788 my_getExpression (&imm_expr, s);
11789 check_absolute_expr (ip, &imm_expr);
11790 if ((unsigned long) imm_expr.X_add_number > mask)
11791 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11792 ip->insn_mo->name,
11793 mask, (unsigned long) imm_expr.X_add_number);
11794 INSERT_OPERAND (mips_opts.micromips,
11795 CODE, *ip, imm_expr.X_add_number);
11796 imm_expr.X_op = O_absent;
11797 s = expr_end;
11798 }
252b5132
RH
11799 continue;
11800
90ecf173 11801 case 'q': /* Lower BREAK code. */
df58fc94
RS
11802 {
11803 unsigned long mask = (mips_opts.micromips
11804 ? MICROMIPSOP_MASK_CODE2
11805 : OP_MASK_CODE2);
11806
11807 my_getExpression (&imm_expr, s);
11808 check_absolute_expr (ip, &imm_expr);
11809 if ((unsigned long) imm_expr.X_add_number > mask)
11810 as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
11811 ip->insn_mo->name,
11812 mask, (unsigned long) imm_expr.X_add_number);
11813 INSERT_OPERAND (mips_opts.micromips,
11814 CODE2, *ip, imm_expr.X_add_number);
11815 imm_expr.X_op = O_absent;
11816 s = expr_end;
11817 }
252b5132
RH
11818 continue;
11819
df58fc94
RS
11820 case 'B': /* 20- or 10-bit syscall/break/wait code. */
11821 {
11822 unsigned long mask = (mips_opts.micromips
11823 ? MICROMIPSOP_MASK_CODE10
11824 : OP_MASK_CODE20);
11825
11826 my_getExpression (&imm_expr, s);
11827 check_absolute_expr (ip, &imm_expr);
11828 if ((unsigned long) imm_expr.X_add_number > mask)
11829 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11830 ip->insn_mo->name,
11831 mask, (unsigned long) imm_expr.X_add_number);
11832 if (mips_opts.micromips)
11833 INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
11834 else
11835 INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
11836 imm_expr.X_op = O_absent;
11837 s = expr_end;
11838 }
252b5132
RH
11839 continue;
11840
df58fc94
RS
11841 case 'C': /* 25- or 23-bit coprocessor code. */
11842 {
11843 unsigned long mask = (mips_opts.micromips
11844 ? MICROMIPSOP_MASK_COPZ
11845 : OP_MASK_COPZ);
11846
11847 my_getExpression (&imm_expr, s);
11848 check_absolute_expr (ip, &imm_expr);
11849 if ((unsigned long) imm_expr.X_add_number > mask)
11850 as_warn (_("Coproccesor code > %u bits (%lu)"),
11851 mips_opts.micromips ? 23U : 25U,
793b27f4 11852 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11853 INSERT_OPERAND (mips_opts.micromips,
11854 COPZ, *ip, imm_expr.X_add_number);
11855 imm_expr.X_op = O_absent;
11856 s = expr_end;
11857 }
beae10d5 11858 continue;
252b5132 11859
df58fc94
RS
11860 case 'J': /* 19-bit WAIT code. */
11861 gas_assert (!mips_opts.micromips);
4372b673
NC
11862 my_getExpression (&imm_expr, s);
11863 check_absolute_expr (ip, &imm_expr);
793b27f4 11864 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
df58fc94
RS
11865 {
11866 as_warn (_("Illegal 19-bit code (%lu)"),
a9e24354 11867 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11868 imm_expr.X_add_number &= OP_MASK_CODE19;
11869 }
11870 INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
11871 imm_expr.X_op = O_absent;
11872 s = expr_end;
11873 continue;
11874
707bfff6 11875 case 'P': /* Performance register. */
df58fc94 11876 gas_assert (!mips_opts.micromips);
beae10d5 11877 my_getExpression (&imm_expr, s);
252b5132 11878 check_absolute_expr (ip, &imm_expr);
beae10d5 11879 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
11880 as_warn (_("Invalid performance register (%lu)"),
11881 (unsigned long) imm_expr.X_add_number);
e407c74b
NC
11882 if (imm_expr.X_add_number != 0 && mips_opts.arch == CPU_R5900
11883 && (!strcmp(insn->name,"mfps") || !strcmp(insn->name,"mtps")))
11884 as_warn (_("Invalid performance register (%lu)"),
11885 (unsigned long) imm_expr.X_add_number);
df58fc94 11886 INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
11887 imm_expr.X_op = O_absent;
11888 s = expr_end;
11889 continue;
252b5132 11890
707bfff6 11891 case 'G': /* Coprocessor destination register. */
df58fc94
RS
11892 {
11893 unsigned long opcode = ip->insn_opcode;
11894 unsigned long mask;
11895 unsigned int types;
11896 int cop0;
11897
11898 if (mips_opts.micromips)
11899 {
11900 mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
11901 | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
11902 | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
11903 opcode &= mask;
11904 switch (opcode)
11905 {
11906 case 0x000000fc: /* mfc0 */
11907 case 0x000002fc: /* mtc0 */
11908 case 0x580000fc: /* dmfc0 */
11909 case 0x580002fc: /* dmtc0 */
11910 cop0 = 1;
11911 break;
11912 default:
11913 cop0 = 0;
11914 break;
11915 }
11916 }
11917 else
11918 {
11919 opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
11920 cop0 = opcode == OP_OP_COP0;
11921 }
11922 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
11923 ok = reg_lookup (&s, types, &regno);
11924 if (mips_opts.micromips)
11925 INSERT_OPERAND (1, RS, *ip, regno);
11926 else
11927 INSERT_OPERAND (0, RD, *ip, regno);
11928 if (ok)
11929 {
11930 lastregno = regno;
11931 continue;
11932 }
11933 }
11934 break;
707bfff6 11935
df58fc94
RS
11936 case 'y': /* ALNV.PS source register. */
11937 gas_assert (mips_opts.micromips);
11938 goto do_reg;
11939 case 'x': /* Ignore register name. */
11940 case 'U': /* Destination register (CLO/CLZ). */
11941 case 'g': /* Coprocessor destination register. */
11942 gas_assert (!mips_opts.micromips);
90ecf173
MR
11943 case 'b': /* Base register. */
11944 case 'd': /* Destination register. */
11945 case 's': /* Source register. */
11946 case 't': /* Target register. */
11947 case 'r': /* Both target and source. */
11948 case 'v': /* Both dest and source. */
11949 case 'w': /* Both dest and target. */
11950 case 'E': /* Coprocessor target register. */
11951 case 'K': /* RDHWR destination register. */
90ecf173 11952 case 'z': /* Must be zero register. */
df58fc94 11953 do_reg:
90ecf173 11954 s_reset = s;
707bfff6
TS
11955 if (*args == 'E' || *args == 'K')
11956 ok = reg_lookup (&s, RTYPE_NUM, &regno);
11957 else
11958 {
11959 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
741fe287
MR
11960 if (regno == AT && mips_opts.at)
11961 {
11962 if (mips_opts.at == ATREG)
f71d0d44 11963 as_warn (_("Used $at without \".set noat\""));
741fe287 11964 else
f71d0d44 11965 as_warn (_("Used $%u with \".set at=$%u\""),
741fe287
MR
11966 regno, mips_opts.at);
11967 }
707bfff6
TS
11968 }
11969 if (ok)
252b5132 11970 {
252b5132
RH
11971 c = *args;
11972 if (*s == ' ')
f9419b05 11973 ++s;
252b5132
RH
11974 if (args[1] != *s)
11975 {
11976 if (c == 'r' || c == 'v' || c == 'w')
11977 {
11978 regno = lastregno;
11979 s = s_reset;
f9419b05 11980 ++args;
252b5132
RH
11981 }
11982 }
11983 /* 'z' only matches $0. */
11984 if (c == 'z' && regno != 0)
11985 break;
11986
24864476 11987 if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
e7c604dd
CM
11988 {
11989 if (regno == lastregno)
90ecf173
MR
11990 {
11991 insn_error
f71d0d44 11992 = _("Source and destination must be different");
e7c604dd 11993 continue;
90ecf173 11994 }
24864476 11995 if (regno == 31 && lastregno == 0xffffffff)
90ecf173
MR
11996 {
11997 insn_error
f71d0d44 11998 = _("A destination register must be supplied");
e7c604dd 11999 continue;
90ecf173 12000 }
e7c604dd 12001 }
90ecf173
MR
12002 /* Now that we have assembled one operand, we use the args
12003 string to figure out where it goes in the instruction. */
252b5132
RH
12004 switch (c)
12005 {
12006 case 'r':
12007 case 's':
12008 case 'v':
12009 case 'b':
df58fc94 12010 INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
252b5132 12011 break;
df58fc94 12012
af7ee8bf 12013 case 'K':
df58fc94
RS
12014 if (mips_opts.micromips)
12015 INSERT_OPERAND (1, RS, *ip, regno);
12016 else
12017 INSERT_OPERAND (0, RD, *ip, regno);
12018 break;
12019
12020 case 'd':
ef2e4d86 12021 case 'g':
df58fc94 12022 INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
252b5132 12023 break;
df58fc94 12024
4372b673 12025 case 'U':
df58fc94
RS
12026 gas_assert (!mips_opts.micromips);
12027 INSERT_OPERAND (0, RD, *ip, regno);
12028 INSERT_OPERAND (0, RT, *ip, regno);
4372b673 12029 break;
df58fc94 12030
252b5132
RH
12031 case 'w':
12032 case 't':
12033 case 'E':
df58fc94
RS
12034 INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
12035 break;
12036
12037 case 'y':
12038 gas_assert (mips_opts.micromips);
12039 INSERT_OPERAND (1, RS3, *ip, regno);
252b5132 12040 break;
df58fc94 12041
252b5132
RH
12042 case 'x':
12043 /* This case exists because on the r3000 trunc
12044 expands into a macro which requires a gp
12045 register. On the r6000 or r4000 it is
12046 assembled into a single instruction which
12047 ignores the register. Thus the insn version
12048 is MIPS_ISA2 and uses 'x', and the macro
12049 version is MIPS_ISA1 and uses 't'. */
12050 break;
df58fc94 12051
252b5132
RH
12052 case 'z':
12053 /* This case is for the div instruction, which
12054 acts differently if the destination argument
12055 is $0. This only matches $0, and is checked
12056 outside the switch. */
12057 break;
252b5132
RH
12058 }
12059 lastregno = regno;
12060 continue;
12061 }
252b5132
RH
12062 switch (*args++)
12063 {
12064 case 'r':
12065 case 'v':
df58fc94 12066 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132 12067 continue;
df58fc94 12068
252b5132 12069 case 'w':
df58fc94 12070 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
252b5132
RH
12071 continue;
12072 }
12073 break;
12074
deec1734 12075 case 'O': /* MDMX alignment immediate constant. */
df58fc94 12076 gas_assert (!mips_opts.micromips);
deec1734
CD
12077 my_getExpression (&imm_expr, s);
12078 check_absolute_expr (ip, &imm_expr);
12079 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
20203fb9 12080 as_warn (_("Improper align amount (%ld), using low bits"),
bf12938e 12081 (long) imm_expr.X_add_number);
df58fc94 12082 INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
deec1734
CD
12083 imm_expr.X_op = O_absent;
12084 s = expr_end;
12085 continue;
12086
12087 case 'Q': /* MDMX vector, element sel, or const. */
12088 if (s[0] != '$')
12089 {
12090 /* MDMX Immediate. */
df58fc94 12091 gas_assert (!mips_opts.micromips);
deec1734
CD
12092 my_getExpression (&imm_expr, s);
12093 check_absolute_expr (ip, &imm_expr);
12094 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
12095 as_warn (_("Invalid MDMX Immediate (%ld)"),
12096 (long) imm_expr.X_add_number);
df58fc94 12097 INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
deec1734
CD
12098 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12099 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12100 else
12101 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
12102 imm_expr.X_op = O_absent;
12103 s = expr_end;
12104 continue;
12105 }
12106 /* Not MDMX Immediate. Fall through. */
12107 case 'X': /* MDMX destination register. */
12108 case 'Y': /* MDMX source register. */
12109 case 'Z': /* MDMX target register. */
12110 is_mdmx = 1;
df58fc94
RS
12111 case 'W':
12112 gas_assert (!mips_opts.micromips);
90ecf173
MR
12113 case 'D': /* Floating point destination register. */
12114 case 'S': /* Floating point source register. */
12115 case 'T': /* Floating point target register. */
12116 case 'R': /* Floating point source register. */
252b5132 12117 case 'V':
707bfff6
TS
12118 rtype = RTYPE_FPU;
12119 if (is_mdmx
12120 || (mips_opts.ase_mdmx
12121 && (ip->insn_mo->pinfo & FP_D)
12122 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12123 | INSN_COPROC_MEMORY_DELAY
12124 | INSN_LOAD_COPROC_DELAY
12125 | INSN_LOAD_MEMORY_DELAY
12126 | INSN_STORE_MEMORY))))
12127 rtype |= RTYPE_VEC;
252b5132 12128 s_reset = s;
707bfff6 12129 if (reg_lookup (&s, rtype, &regno))
252b5132 12130 {
252b5132 12131 if ((regno & 1) != 0
ca4e0257 12132 && HAVE_32BIT_FPRS
90ecf173 12133 && !mips_oddfpreg_ok (ip->insn_mo, argnum))
252b5132
RH
12134 as_warn (_("Float register should be even, was %d"),
12135 regno);
12136
12137 c = *args;
12138 if (*s == ' ')
f9419b05 12139 ++s;
252b5132
RH
12140 if (args[1] != *s)
12141 {
12142 if (c == 'V' || c == 'W')
12143 {
12144 regno = lastregno;
12145 s = s_reset;
f9419b05 12146 ++args;
252b5132
RH
12147 }
12148 }
12149 switch (c)
12150 {
12151 case 'D':
deec1734 12152 case 'X':
df58fc94 12153 INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
252b5132 12154 break;
df58fc94 12155
252b5132
RH
12156 case 'V':
12157 case 'S':
deec1734 12158 case 'Y':
df58fc94 12159 INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
252b5132 12160 break;
df58fc94 12161
deec1734
CD
12162 case 'Q':
12163 /* This is like 'Z', but also needs to fix the MDMX
12164 vector/scalar select bits. Note that the
12165 scalar immediate case is handled above. */
12166 if (*s == '[')
12167 {
12168 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12169 int max_el = (is_qh ? 3 : 7);
12170 s++;
12171 my_getExpression(&imm_expr, s);
12172 check_absolute_expr (ip, &imm_expr);
12173 s = expr_end;
12174 if (imm_expr.X_add_number > max_el)
20203fb9
NC
12175 as_bad (_("Bad element selector %ld"),
12176 (long) imm_expr.X_add_number);
deec1734
CD
12177 imm_expr.X_add_number &= max_el;
12178 ip->insn_opcode |= (imm_expr.X_add_number
12179 << (OP_SH_VSEL +
12180 (is_qh ? 2 : 1)));
01a3f561 12181 imm_expr.X_op = O_absent;
deec1734 12182 if (*s != ']')
20203fb9 12183 as_warn (_("Expecting ']' found '%s'"), s);
deec1734
CD
12184 else
12185 s++;
12186 }
12187 else
12188 {
12189 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12190 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12191 << OP_SH_VSEL);
12192 else
12193 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12194 OP_SH_VSEL);
12195 }
90ecf173 12196 /* Fall through. */
252b5132
RH
12197 case 'W':
12198 case 'T':
deec1734 12199 case 'Z':
df58fc94 12200 INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
252b5132 12201 break;
df58fc94 12202
252b5132 12203 case 'R':
df58fc94 12204 INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
252b5132
RH
12205 break;
12206 }
12207 lastregno = regno;
12208 continue;
12209 }
12210
252b5132
RH
12211 switch (*args++)
12212 {
12213 case 'V':
df58fc94 12214 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132 12215 continue;
df58fc94 12216
252b5132 12217 case 'W':
df58fc94 12218 INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
252b5132
RH
12219 continue;
12220 }
12221 break;
12222
12223 case 'I':
12224 my_getExpression (&imm_expr, s);
12225 if (imm_expr.X_op != O_big
12226 && imm_expr.X_op != O_constant)
12227 insn_error = _("absolute expression required");
9ee2a2d4
MR
12228 if (HAVE_32BIT_GPRS)
12229 normalize_constant_expr (&imm_expr);
252b5132
RH
12230 s = expr_end;
12231 continue;
12232
12233 case 'A':
12234 my_getExpression (&offset_expr, s);
2051e8c4 12235 normalize_address_expr (&offset_expr);
f6688943 12236 *imm_reloc = BFD_RELOC_32;
252b5132
RH
12237 s = expr_end;
12238 continue;
12239
12240 case 'F':
12241 case 'L':
12242 case 'f':
12243 case 'l':
12244 {
12245 int f64;
ca4e0257 12246 int using_gprs;
252b5132
RH
12247 char *save_in;
12248 char *err;
12249 unsigned char temp[8];
12250 int len;
12251 unsigned int length;
12252 segT seg;
12253 subsegT subseg;
12254 char *p;
12255
12256 /* These only appear as the last operand in an
12257 instruction, and every instruction that accepts
12258 them in any variant accepts them in all variants.
12259 This means we don't have to worry about backing out
12260 any changes if the instruction does not match.
12261
12262 The difference between them is the size of the
12263 floating point constant and where it goes. For 'F'
12264 and 'L' the constant is 64 bits; for 'f' and 'l' it
12265 is 32 bits. Where the constant is placed is based
12266 on how the MIPS assembler does things:
12267 F -- .rdata
12268 L -- .lit8
12269 f -- immediate value
12270 l -- .lit4
12271
12272 The .lit4 and .lit8 sections are only used if
12273 permitted by the -G argument.
12274
ca4e0257
RS
12275 The code below needs to know whether the target register
12276 is 32 or 64 bits wide. It relies on the fact 'f' and
12277 'F' are used with GPR-based instructions and 'l' and
12278 'L' are used with FPR-based instructions. */
252b5132
RH
12279
12280 f64 = *args == 'F' || *args == 'L';
ca4e0257 12281 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
12282
12283 save_in = input_line_pointer;
12284 input_line_pointer = s;
12285 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12286 length = len;
12287 s = input_line_pointer;
12288 input_line_pointer = save_in;
12289 if (err != NULL && *err != '\0')
12290 {
12291 as_bad (_("Bad floating point constant: %s"), err);
12292 memset (temp, '\0', sizeof temp);
12293 length = f64 ? 8 : 4;
12294 }
12295
9c2799c2 12296 gas_assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
12297
12298 if (*args == 'f'
12299 || (*args == 'l'
3e722fb5 12300 && (g_switch_value < 4
252b5132
RH
12301 || (temp[0] == 0 && temp[1] == 0)
12302 || (temp[2] == 0 && temp[3] == 0))))
12303 {
12304 imm_expr.X_op = O_constant;
90ecf173 12305 if (!target_big_endian)
252b5132
RH
12306 imm_expr.X_add_number = bfd_getl32 (temp);
12307 else
12308 imm_expr.X_add_number = bfd_getb32 (temp);
12309 }
12310 else if (length > 4
90ecf173 12311 && !mips_disable_float_construction
ca4e0257
RS
12312 /* Constants can only be constructed in GPRs and
12313 copied to FPRs if the GPRs are at least as wide
12314 as the FPRs. Force the constant into memory if
12315 we are using 64-bit FPRs but the GPRs are only
12316 32 bits wide. */
12317 && (using_gprs
90ecf173 12318 || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
12319 && ((temp[0] == 0 && temp[1] == 0)
12320 || (temp[2] == 0 && temp[3] == 0))
12321 && ((temp[4] == 0 && temp[5] == 0)
12322 || (temp[6] == 0 && temp[7] == 0)))
12323 {
ca4e0257 12324 /* The value is simple enough to load with a couple of
90ecf173
MR
12325 instructions. If using 32-bit registers, set
12326 imm_expr to the high order 32 bits and offset_expr to
12327 the low order 32 bits. Otherwise, set imm_expr to
12328 the entire 64 bit constant. */
ca4e0257 12329 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
12330 {
12331 imm_expr.X_op = O_constant;
12332 offset_expr.X_op = O_constant;
90ecf173 12333 if (!target_big_endian)
252b5132
RH
12334 {
12335 imm_expr.X_add_number = bfd_getl32 (temp + 4);
12336 offset_expr.X_add_number = bfd_getl32 (temp);
12337 }
12338 else
12339 {
12340 imm_expr.X_add_number = bfd_getb32 (temp);
12341 offset_expr.X_add_number = bfd_getb32 (temp + 4);
12342 }
12343 if (offset_expr.X_add_number == 0)
12344 offset_expr.X_op = O_absent;
12345 }
12346 else if (sizeof (imm_expr.X_add_number) > 4)
12347 {
12348 imm_expr.X_op = O_constant;
90ecf173 12349 if (!target_big_endian)
252b5132
RH
12350 imm_expr.X_add_number = bfd_getl64 (temp);
12351 else
12352 imm_expr.X_add_number = bfd_getb64 (temp);
12353 }
12354 else
12355 {
12356 imm_expr.X_op = O_big;
12357 imm_expr.X_add_number = 4;
90ecf173 12358 if (!target_big_endian)
252b5132
RH
12359 {
12360 generic_bignum[0] = bfd_getl16 (temp);
12361 generic_bignum[1] = bfd_getl16 (temp + 2);
12362 generic_bignum[2] = bfd_getl16 (temp + 4);
12363 generic_bignum[3] = bfd_getl16 (temp + 6);
12364 }
12365 else
12366 {
12367 generic_bignum[0] = bfd_getb16 (temp + 6);
12368 generic_bignum[1] = bfd_getb16 (temp + 4);
12369 generic_bignum[2] = bfd_getb16 (temp + 2);
12370 generic_bignum[3] = bfd_getb16 (temp);
12371 }
12372 }
12373 }
12374 else
12375 {
12376 const char *newname;
12377 segT new_seg;
12378
12379 /* Switch to the right section. */
12380 seg = now_seg;
12381 subseg = now_subseg;
12382 switch (*args)
12383 {
12384 default: /* unused default case avoids warnings. */
12385 case 'L':
12386 newname = RDATA_SECTION_NAME;
3e722fb5 12387 if (g_switch_value >= 8)
252b5132
RH
12388 newname = ".lit8";
12389 break;
12390 case 'F':
3e722fb5 12391 newname = RDATA_SECTION_NAME;
252b5132
RH
12392 break;
12393 case 'l':
9c2799c2 12394 gas_assert (g_switch_value >= 4);
252b5132
RH
12395 newname = ".lit4";
12396 break;
12397 }
12398 new_seg = subseg_new (newname, (subsegT) 0);
f43abd2b 12399 if (IS_ELF)
252b5132
RH
12400 bfd_set_section_flags (stdoutput, new_seg,
12401 (SEC_ALLOC
12402 | SEC_LOAD
12403 | SEC_READONLY
12404 | SEC_DATA));
12405 frag_align (*args == 'l' ? 2 : 3, 0, 0);
c41e87e3 12406 if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
12407 record_alignment (new_seg, 4);
12408 else
12409 record_alignment (new_seg, *args == 'l' ? 2 : 3);
12410 if (seg == now_seg)
12411 as_bad (_("Can't use floating point insn in this section"));
12412
df58fc94
RS
12413 /* Set the argument to the current address in the
12414 section. */
12415 offset_expr.X_op = O_symbol;
12416 offset_expr.X_add_symbol = symbol_temp_new_now ();
12417 offset_expr.X_add_number = 0;
12418
12419 /* Put the floating point number into the section. */
12420 p = frag_more ((int) length);
12421 memcpy (p, temp, length);
12422
12423 /* Switch back to the original section. */
12424 subseg_set (seg, subseg);
12425 }
12426 }
12427 continue;
12428
12429 case 'i': /* 16-bit unsigned immediate. */
12430 case 'j': /* 16-bit signed immediate. */
12431 *imm_reloc = BFD_RELOC_LO16;
12432 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12433 {
12434 int more;
12435 offsetT minval, maxval;
12436
12437 more = (insn + 1 < past
12438 && strcmp (insn->name, insn[1].name) == 0);
12439
12440 /* If the expression was written as an unsigned number,
12441 only treat it as signed if there are no more
12442 alternatives. */
12443 if (more
12444 && *args == 'j'
12445 && sizeof (imm_expr.X_add_number) <= 4
12446 && imm_expr.X_op == O_constant
12447 && imm_expr.X_add_number < 0
12448 && imm_expr.X_unsigned
12449 && HAVE_64BIT_GPRS)
12450 break;
12451
12452 /* For compatibility with older assemblers, we accept
12453 0x8000-0xffff as signed 16-bit numbers when only
12454 signed numbers are allowed. */
12455 if (*args == 'i')
12456 minval = 0, maxval = 0xffff;
12457 else if (more)
12458 minval = -0x8000, maxval = 0x7fff;
12459 else
12460 minval = -0x8000, maxval = 0xffff;
12461
12462 if (imm_expr.X_op != O_constant
12463 || imm_expr.X_add_number < minval
12464 || imm_expr.X_add_number > maxval)
12465 {
12466 if (more)
12467 break;
12468 if (imm_expr.X_op == O_constant
12469 || imm_expr.X_op == O_big)
12470 as_bad (_("Expression out of range"));
12471 }
12472 }
12473 s = expr_end;
12474 continue;
12475
12476 case 'o': /* 16-bit offset. */
12477 offset_reloc[0] = BFD_RELOC_LO16;
12478 offset_reloc[1] = BFD_RELOC_UNUSED;
12479 offset_reloc[2] = BFD_RELOC_UNUSED;
12480
12481 /* Check whether there is only a single bracketed expression
12482 left. If so, it must be the base register and the
12483 constant must be zero. */
12484 if (*s == '(' && strchr (s + 1, '(') == 0)
12485 {
12486 offset_expr.X_op = O_constant;
12487 offset_expr.X_add_number = 0;
12488 continue;
12489 }
12490
12491 /* If this value won't fit into a 16 bit offset, then go
12492 find a macro that will generate the 32 bit offset
12493 code pattern. */
12494 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
12495 && (offset_expr.X_op != O_constant
12496 || offset_expr.X_add_number >= 0x8000
12497 || offset_expr.X_add_number < -0x8000))
12498 break;
12499
12500 s = expr_end;
12501 continue;
12502
12503 case 'p': /* PC-relative offset. */
12504 *offset_reloc = BFD_RELOC_16_PCREL_S2;
12505 my_getExpression (&offset_expr, s);
12506 s = expr_end;
12507 continue;
12508
12509 case 'u': /* Upper 16 bits. */
5821951c 12510 *imm_reloc = BFD_RELOC_LO16;
df58fc94
RS
12511 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
12512 && imm_expr.X_op == O_constant
12513 && (imm_expr.X_add_number < 0
12514 || imm_expr.X_add_number >= 0x10000))
12515 as_bad (_("lui expression (%lu) not in range 0..65535"),
12516 (unsigned long) imm_expr.X_add_number);
12517 s = expr_end;
12518 continue;
12519
12520 case 'a': /* 26-bit address. */
12521 *offset_reloc = BFD_RELOC_MIPS_JMP;
12522 my_getExpression (&offset_expr, s);
12523 s = expr_end;
12524 continue;
12525
12526 case 'N': /* 3-bit branch condition code. */
12527 case 'M': /* 3-bit compare condition code. */
12528 rtype = RTYPE_CCC;
12529 if (ip->insn_mo->pinfo & (FP_D | FP_S))
12530 rtype |= RTYPE_FCC;
12531 if (!reg_lookup (&s, rtype, &regno))
12532 break;
12533 if ((strcmp (str + strlen (str) - 3, ".ps") == 0
12534 || strcmp (str + strlen (str) - 5, "any2f") == 0
12535 || strcmp (str + strlen (str) - 5, "any2t") == 0)
12536 && (regno & 1) != 0)
12537 as_warn (_("Condition code register should be even for %s, "
12538 "was %d"),
12539 str, regno);
12540 if ((strcmp (str + strlen (str) - 5, "any4f") == 0
12541 || strcmp (str + strlen (str) - 5, "any4t") == 0)
12542 && (regno & 3) != 0)
12543 as_warn (_("Condition code register should be 0 or 4 for %s, "
12544 "was %d"),
12545 str, regno);
12546 if (*args == 'N')
12547 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
12548 else
12549 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
12550 continue;
12551
12552 case 'H':
12553 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
12554 s += 2;
12555 if (ISDIGIT (*s))
12556 {
12557 c = 0;
12558 do
12559 {
12560 c *= 10;
12561 c += *s - '0';
12562 ++s;
12563 }
12564 while (ISDIGIT (*s));
12565 }
12566 else
12567 c = 8; /* Invalid sel value. */
12568
12569 if (c > 7)
12570 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
12571 INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
12572 continue;
12573
12574 case 'e':
12575 gas_assert (!mips_opts.micromips);
12576 /* Must be at least one digit. */
12577 my_getExpression (&imm_expr, s);
12578 check_absolute_expr (ip, &imm_expr);
12579
12580 if ((unsigned long) imm_expr.X_add_number
12581 > (unsigned long) OP_MASK_VECBYTE)
12582 {
12583 as_bad (_("bad byte vector index (%ld)"),
12584 (long) imm_expr.X_add_number);
12585 imm_expr.X_add_number = 0;
12586 }
12587
12588 INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
12589 imm_expr.X_op = O_absent;
12590 s = expr_end;
12591 continue;
12592
12593 case '%':
12594 gas_assert (!mips_opts.micromips);
12595 my_getExpression (&imm_expr, s);
12596 check_absolute_expr (ip, &imm_expr);
12597
12598 if ((unsigned long) imm_expr.X_add_number
12599 > (unsigned long) OP_MASK_VECALIGN)
12600 {
12601 as_bad (_("bad byte vector index (%ld)"),
12602 (long) imm_expr.X_add_number);
12603 imm_expr.X_add_number = 0;
12604 }
12605
12606 INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
12607 imm_expr.X_op = O_absent;
12608 s = expr_end;
12609 continue;
12610
12611 case 'm': /* Opcode extension character. */
12612 gas_assert (mips_opts.micromips);
12613 c = *++args;
12614 switch (c)
12615 {
12616 case 'r':
12617 if (strncmp (s, "$pc", 3) == 0)
12618 {
12619 s += 3;
12620 continue;
12621 }
12622 break;
12623
12624 case 'a':
12625 case 'b':
12626 case 'c':
12627 case 'd':
12628 case 'e':
12629 case 'f':
12630 case 'g':
12631 case 'h':
12632 case 'i':
12633 case 'j':
12634 case 'l':
12635 case 'm':
12636 case 'n':
12637 case 'p':
12638 case 'q':
12639 case 's':
12640 case 't':
12641 case 'x':
12642 case 'y':
12643 case 'z':
12644 s_reset = s;
12645 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12646 if (regno == AT && mips_opts.at)
12647 {
12648 if (mips_opts.at == ATREG)
12649 as_warn (_("Used $at without \".set noat\""));
12650 else
12651 as_warn (_("Used $%u with \".set at=$%u\""),
12652 regno, mips_opts.at);
12653 }
12654 if (!ok)
12655 {
12656 if (c == 'c')
12657 {
12658 gas_assert (args[1] == ',');
12659 regno = lastregno;
12660 ++args;
12661 }
12662 else if (c == 't')
12663 {
12664 gas_assert (args[1] == ',');
12665 ++args;
12666 continue; /* Nothing to do. */
12667 }
12668 else
12669 break;
12670 }
12671
12672 if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
12673 {
12674 if (regno == lastregno)
12675 {
12676 insn_error
12677 = _("Source and destination must be different");
12678 continue;
12679 }
12680 if (regno == 31 && lastregno == 0xffffffff)
12681 {
12682 insn_error
12683 = _("A destination register must be supplied");
12684 continue;
12685 }
12686 }
12687
12688 if (*s == ' ')
12689 ++s;
12690 if (args[1] != *s)
12691 {
12692 if (c == 'e')
12693 {
12694 gas_assert (args[1] == ',');
12695 regno = lastregno;
12696 s = s_reset;
12697 ++args;
12698 }
12699 else if (c == 't')
12700 {
12701 gas_assert (args[1] == ',');
12702 s = s_reset;
12703 ++args;
12704 continue; /* Nothing to do. */
12705 }
12706 }
12707
12708 /* Make sure regno is the same as lastregno. */
12709 if (c == 't' && regno != lastregno)
12710 break;
12711
12712 /* Make sure regno is the same as destregno. */
12713 if (c == 'x' && regno != destregno)
12714 break;
12715
12716 /* We need to save regno, before regno maps to the
12717 microMIPS register encoding. */
12718 lastregno = regno;
12719
12720 if (c == 'f')
12721 destregno = regno;
12722
12723 switch (c)
12724 {
12725 case 'a':
12726 if (regno != GP)
12727 regno = ILLEGAL_REG;
12728 break;
12729
12730 case 'b':
12731 regno = mips32_to_micromips_reg_b_map[regno];
12732 break;
12733
12734 case 'c':
12735 regno = mips32_to_micromips_reg_c_map[regno];
12736 break;
12737
12738 case 'd':
12739 regno = mips32_to_micromips_reg_d_map[regno];
12740 break;
12741
12742 case 'e':
12743 regno = mips32_to_micromips_reg_e_map[regno];
12744 break;
12745
12746 case 'f':
12747 regno = mips32_to_micromips_reg_f_map[regno];
12748 break;
12749
12750 case 'g':
12751 regno = mips32_to_micromips_reg_g_map[regno];
12752 break;
12753
12754 case 'h':
12755 regno = mips32_to_micromips_reg_h_map[regno];
12756 break;
12757
12758 case 'i':
12759 switch (EXTRACT_OPERAND (1, MI, *ip))
12760 {
12761 case 4:
12762 if (regno == 21)
12763 regno = 3;
12764 else if (regno == 22)
12765 regno = 4;
12766 else if (regno == 5)
12767 regno = 5;
12768 else if (regno == 6)
12769 regno = 6;
12770 else if (regno == 7)
12771 regno = 7;
12772 else
12773 regno = ILLEGAL_REG;
12774 break;
12775
12776 case 5:
12777 if (regno == 6)
12778 regno = 0;
12779 else if (regno == 7)
12780 regno = 1;
12781 else
12782 regno = ILLEGAL_REG;
12783 break;
12784
12785 case 6:
12786 if (regno == 7)
12787 regno = 2;
12788 else
12789 regno = ILLEGAL_REG;
12790 break;
12791
12792 default:
12793 regno = ILLEGAL_REG;
12794 break;
12795 }
12796 break;
12797
12798 case 'l':
12799 regno = mips32_to_micromips_reg_l_map[regno];
12800 break;
12801
12802 case 'm':
12803 regno = mips32_to_micromips_reg_m_map[regno];
12804 break;
12805
12806 case 'n':
12807 regno = mips32_to_micromips_reg_n_map[regno];
12808 break;
12809
12810 case 'q':
12811 regno = mips32_to_micromips_reg_q_map[regno];
12812 break;
12813
12814 case 's':
12815 if (regno != SP)
12816 regno = ILLEGAL_REG;
12817 break;
12818
12819 case 'y':
12820 if (regno != 31)
12821 regno = ILLEGAL_REG;
12822 break;
12823
12824 case 'z':
12825 if (regno != ZERO)
12826 regno = ILLEGAL_REG;
12827 break;
12828
12829 case 'j': /* Do nothing. */
12830 case 'p':
12831 case 't':
12832 case 'x':
12833 break;
12834
12835 default:
b37df7c4 12836 abort ();
df58fc94
RS
12837 }
12838
12839 if (regno == ILLEGAL_REG)
12840 break;
12841
12842 switch (c)
12843 {
12844 case 'b':
12845 INSERT_OPERAND (1, MB, *ip, regno);
12846 break;
12847
12848 case 'c':
12849 INSERT_OPERAND (1, MC, *ip, regno);
12850 break;
12851
12852 case 'd':
12853 INSERT_OPERAND (1, MD, *ip, regno);
12854 break;
12855
12856 case 'e':
12857 INSERT_OPERAND (1, ME, *ip, regno);
12858 break;
12859
12860 case 'f':
12861 INSERT_OPERAND (1, MF, *ip, regno);
12862 break;
12863
12864 case 'g':
12865 INSERT_OPERAND (1, MG, *ip, regno);
12866 break;
12867
12868 case 'h':
12869 INSERT_OPERAND (1, MH, *ip, regno);
12870 break;
12871
12872 case 'i':
12873 INSERT_OPERAND (1, MI, *ip, regno);
12874 break;
12875
12876 case 'j':
12877 INSERT_OPERAND (1, MJ, *ip, regno);
12878 break;
12879
12880 case 'l':
12881 INSERT_OPERAND (1, ML, *ip, regno);
12882 break;
12883
12884 case 'm':
12885 INSERT_OPERAND (1, MM, *ip, regno);
12886 break;
12887
12888 case 'n':
12889 INSERT_OPERAND (1, MN, *ip, regno);
12890 break;
12891
12892 case 'p':
12893 INSERT_OPERAND (1, MP, *ip, regno);
12894 break;
12895
12896 case 'q':
12897 INSERT_OPERAND (1, MQ, *ip, regno);
12898 break;
12899
12900 case 'a': /* Do nothing. */
12901 case 's': /* Do nothing. */
12902 case 't': /* Do nothing. */
12903 case 'x': /* Do nothing. */
12904 case 'y': /* Do nothing. */
12905 case 'z': /* Do nothing. */
12906 break;
12907
12908 default:
b37df7c4 12909 abort ();
df58fc94
RS
12910 }
12911 continue;
12912
12913 case 'A':
12914 {
12915 bfd_reloc_code_real_type r[3];
12916 expressionS ep;
12917 int imm;
12918
12919 /* Check whether there is only a single bracketed
12920 expression left. If so, it must be the base register
12921 and the constant must be zero. */
12922 if (*s == '(' && strchr (s + 1, '(') == 0)
12923 {
12924 INSERT_OPERAND (1, IMMA, *ip, 0);
12925 continue;
12926 }
12927
12928 if (my_getSmallExpression (&ep, r, s) > 0
12929 || !expr_const_in_range (&ep, -64, 64, 2))
12930 break;
12931
12932 imm = ep.X_add_number >> 2;
12933 INSERT_OPERAND (1, IMMA, *ip, imm);
12934 }
12935 s = expr_end;
12936 continue;
12937
12938 case 'B':
12939 {
12940 bfd_reloc_code_real_type r[3];
12941 expressionS ep;
12942 int imm;
12943
12944 if (my_getSmallExpression (&ep, r, s) > 0
12945 || ep.X_op != O_constant)
12946 break;
12947
12948 for (imm = 0; imm < 8; imm++)
12949 if (micromips_imm_b_map[imm] == ep.X_add_number)
12950 break;
12951 if (imm >= 8)
12952 break;
12953
12954 INSERT_OPERAND (1, IMMB, *ip, imm);
12955 }
12956 s = expr_end;
12957 continue;
12958
12959 case 'C':
12960 {
12961 bfd_reloc_code_real_type r[3];
12962 expressionS ep;
12963 int imm;
12964
12965 if (my_getSmallExpression (&ep, r, s) > 0
12966 || ep.X_op != O_constant)
12967 break;
12968
12969 for (imm = 0; imm < 16; imm++)
12970 if (micromips_imm_c_map[imm] == ep.X_add_number)
12971 break;
12972 if (imm >= 16)
12973 break;
12974
12975 INSERT_OPERAND (1, IMMC, *ip, imm);
12976 }
12977 s = expr_end;
12978 continue;
12979
12980 case 'D': /* pc relative offset */
12981 case 'E': /* pc relative offset */
12982 my_getExpression (&offset_expr, s);
12983 if (offset_expr.X_op == O_register)
12984 break;
12985
40209cad
MR
12986 if (!forced_insn_length)
12987 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
12988 else if (c == 'D')
12989 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
12990 else
12991 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
df58fc94
RS
12992 s = expr_end;
12993 continue;
12994
12995 case 'F':
12996 {
12997 bfd_reloc_code_real_type r[3];
12998 expressionS ep;
12999 int imm;
13000
13001 if (my_getSmallExpression (&ep, r, s) > 0
13002 || !expr_const_in_range (&ep, 0, 16, 0))
13003 break;
13004
13005 imm = ep.X_add_number;
13006 INSERT_OPERAND (1, IMMF, *ip, imm);
13007 }
13008 s = expr_end;
13009 continue;
13010
13011 case 'G':
13012 {
13013 bfd_reloc_code_real_type r[3];
13014 expressionS ep;
13015 int imm;
13016
13017 /* Check whether there is only a single bracketed
13018 expression left. If so, it must be the base register
13019 and the constant must be zero. */
13020 if (*s == '(' && strchr (s + 1, '(') == 0)
13021 {
13022 INSERT_OPERAND (1, IMMG, *ip, 0);
13023 continue;
13024 }
13025
13026 if (my_getSmallExpression (&ep, r, s) > 0
13027 || !expr_const_in_range (&ep, -1, 15, 0))
13028 break;
13029
13030 imm = ep.X_add_number & 15;
13031 INSERT_OPERAND (1, IMMG, *ip, imm);
13032 }
13033 s = expr_end;
13034 continue;
13035
13036 case 'H':
13037 {
13038 bfd_reloc_code_real_type r[3];
13039 expressionS ep;
13040 int imm;
13041
13042 /* Check whether there is only a single bracketed
13043 expression left. If so, it must be the base register
13044 and the constant must be zero. */
13045 if (*s == '(' && strchr (s + 1, '(') == 0)
13046 {
13047 INSERT_OPERAND (1, IMMH, *ip, 0);
13048 continue;
13049 }
13050
13051 if (my_getSmallExpression (&ep, r, s) > 0
13052 || !expr_const_in_range (&ep, 0, 16, 1))
13053 break;
13054
13055 imm = ep.X_add_number >> 1;
13056 INSERT_OPERAND (1, IMMH, *ip, imm);
13057 }
13058 s = expr_end;
13059 continue;
13060
13061 case 'I':
13062 {
13063 bfd_reloc_code_real_type r[3];
13064 expressionS ep;
13065 int imm;
13066
13067 if (my_getSmallExpression (&ep, r, s) > 0
13068 || !expr_const_in_range (&ep, -1, 127, 0))
13069 break;
13070
13071 imm = ep.X_add_number & 127;
13072 INSERT_OPERAND (1, IMMI, *ip, imm);
13073 }
13074 s = expr_end;
13075 continue;
13076
13077 case 'J':
13078 {
13079 bfd_reloc_code_real_type r[3];
13080 expressionS ep;
13081 int imm;
13082
13083 /* Check whether there is only a single bracketed
13084 expression left. If so, it must be the base register
13085 and the constant must be zero. */
13086 if (*s == '(' && strchr (s + 1, '(') == 0)
13087 {
13088 INSERT_OPERAND (1, IMMJ, *ip, 0);
13089 continue;
13090 }
13091
13092 if (my_getSmallExpression (&ep, r, s) > 0
13093 || !expr_const_in_range (&ep, 0, 16, 2))
13094 break;
13095
13096 imm = ep.X_add_number >> 2;
13097 INSERT_OPERAND (1, IMMJ, *ip, imm);
13098 }
13099 s = expr_end;
13100 continue;
13101
13102 case 'L':
13103 {
13104 bfd_reloc_code_real_type r[3];
13105 expressionS ep;
13106 int imm;
13107
13108 /* Check whether there is only a single bracketed
13109 expression left. If so, it must be the base register
13110 and the constant must be zero. */
13111 if (*s == '(' && strchr (s + 1, '(') == 0)
13112 {
13113 INSERT_OPERAND (1, IMML, *ip, 0);
13114 continue;
13115 }
13116
13117 if (my_getSmallExpression (&ep, r, s) > 0
13118 || !expr_const_in_range (&ep, 0, 16, 0))
13119 break;
13120
13121 imm = ep.X_add_number;
13122 INSERT_OPERAND (1, IMML, *ip, imm);
13123 }
13124 s = expr_end;
13125 continue;
13126
13127 case 'M':
13128 {
13129 bfd_reloc_code_real_type r[3];
13130 expressionS ep;
13131 int imm;
13132
13133 if (my_getSmallExpression (&ep, r, s) > 0
13134 || !expr_const_in_range (&ep, 1, 9, 0))
13135 break;
13136
13137 imm = ep.X_add_number & 7;
13138 INSERT_OPERAND (1, IMMM, *ip, imm);
13139 }
13140 s = expr_end;
13141 continue;
13142
13143 case 'N': /* Register list for lwm and swm. */
13144 {
13145 /* A comma-separated list of registers and/or
13146 dash-separated contiguous ranges including
13147 both ra and a set of one or more registers
13148 starting at s0 up to s3 which have to be
13149 consecutive, e.g.:
13150
13151 s0, ra
13152 s0, s1, ra, s2, s3
13153 s0-s2, ra
13154
13155 and any permutations of these. */
13156 unsigned int reglist;
13157 int imm;
13158
13159 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13160 break;
13161
13162 if ((reglist & 0xfff1ffff) != 0x80010000)
13163 break;
13164
13165 reglist = (reglist >> 17) & 7;
13166 reglist += 1;
13167 if ((reglist & -reglist) != reglist)
13168 break;
252b5132 13169
df58fc94
RS
13170 imm = ffs (reglist) - 1;
13171 INSERT_OPERAND (1, IMMN, *ip, imm);
13172 }
13173 continue;
252b5132 13174
df58fc94
RS
13175 case 'O': /* sdbbp 4-bit code. */
13176 {
13177 bfd_reloc_code_real_type r[3];
13178 expressionS ep;
13179 int imm;
13180
13181 if (my_getSmallExpression (&ep, r, s) > 0
13182 || !expr_const_in_range (&ep, 0, 16, 0))
13183 break;
13184
13185 imm = ep.X_add_number;
13186 INSERT_OPERAND (1, IMMO, *ip, imm);
252b5132 13187 }
df58fc94
RS
13188 s = expr_end;
13189 continue;
252b5132 13190
df58fc94
RS
13191 case 'P':
13192 {
13193 bfd_reloc_code_real_type r[3];
13194 expressionS ep;
13195 int imm;
5e0116d5 13196
df58fc94
RS
13197 if (my_getSmallExpression (&ep, r, s) > 0
13198 || !expr_const_in_range (&ep, 0, 32, 2))
13199 break;
5e0116d5 13200
df58fc94
RS
13201 imm = ep.X_add_number >> 2;
13202 INSERT_OPERAND (1, IMMP, *ip, imm);
13203 }
13204 s = expr_end;
13205 continue;
5e0116d5 13206
df58fc94
RS
13207 case 'Q':
13208 {
13209 bfd_reloc_code_real_type r[3];
13210 expressionS ep;
13211 int imm;
5e0116d5 13212
df58fc94
RS
13213 if (my_getSmallExpression (&ep, r, s) > 0
13214 || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13215 break;
252b5132 13216
df58fc94
RS
13217 imm = ep.X_add_number >> 2;
13218 INSERT_OPERAND (1, IMMQ, *ip, imm);
13219 }
13220 s = expr_end;
13221 continue;
4614d845 13222
df58fc94
RS
13223 case 'U':
13224 {
13225 bfd_reloc_code_real_type r[3];
13226 expressionS ep;
13227 int imm;
13228
13229 /* Check whether there is only a single bracketed
13230 expression left. If so, it must be the base register
13231 and the constant must be zero. */
13232 if (*s == '(' && strchr (s + 1, '(') == 0)
13233 {
13234 INSERT_OPERAND (1, IMMU, *ip, 0);
13235 continue;
13236 }
13237
13238 if (my_getSmallExpression (&ep, r, s) > 0
13239 || !expr_const_in_range (&ep, 0, 32, 2))
13240 break;
13241
13242 imm = ep.X_add_number >> 2;
13243 INSERT_OPERAND (1, IMMU, *ip, imm);
13244 }
13245 s = expr_end;
5e0116d5 13246 continue;
252b5132 13247
df58fc94
RS
13248 case 'W':
13249 {
13250 bfd_reloc_code_real_type r[3];
13251 expressionS ep;
13252 int imm;
252b5132 13253
df58fc94
RS
13254 if (my_getSmallExpression (&ep, r, s) > 0
13255 || !expr_const_in_range (&ep, 0, 64, 2))
13256 break;
252b5132 13257
df58fc94
RS
13258 imm = ep.X_add_number >> 2;
13259 INSERT_OPERAND (1, IMMW, *ip, imm);
13260 }
13261 s = expr_end;
13262 continue;
252b5132 13263
df58fc94
RS
13264 case 'X':
13265 {
13266 bfd_reloc_code_real_type r[3];
13267 expressionS ep;
13268 int imm;
252b5132 13269
df58fc94
RS
13270 if (my_getSmallExpression (&ep, r, s) > 0
13271 || !expr_const_in_range (&ep, -8, 8, 0))
13272 break;
252b5132 13273
df58fc94
RS
13274 imm = ep.X_add_number;
13275 INSERT_OPERAND (1, IMMX, *ip, imm);
13276 }
13277 s = expr_end;
13278 continue;
252b5132 13279
df58fc94
RS
13280 case 'Y':
13281 {
13282 bfd_reloc_code_real_type r[3];
13283 expressionS ep;
13284 int imm;
156c2f8b 13285
df58fc94
RS
13286 if (my_getSmallExpression (&ep, r, s) > 0
13287 || expr_const_in_range (&ep, -2, 2, 2)
13288 || !expr_const_in_range (&ep, -258, 258, 2))
13289 break;
156c2f8b 13290
df58fc94
RS
13291 imm = ep.X_add_number >> 2;
13292 imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13293 INSERT_OPERAND (1, IMMY, *ip, imm);
13294 }
13295 s = expr_end;
13296 continue;
60b63b72 13297
df58fc94
RS
13298 case 'Z':
13299 {
13300 bfd_reloc_code_real_type r[3];
13301 expressionS ep;
13302
13303 if (my_getSmallExpression (&ep, r, s) > 0
13304 || !expr_const_in_range (&ep, 0, 1, 0))
13305 break;
13306 }
13307 s = expr_end;
13308 continue;
13309
13310 default:
13311 as_bad (_("Internal error: bad microMIPS opcode "
13312 "(unknown extension operand type `m%c'): %s %s"),
13313 *args, insn->name, insn->args);
13314 /* Further processing is fruitless. */
13315 return;
60b63b72 13316 }
df58fc94 13317 break;
60b63b72 13318
df58fc94
RS
13319 case 'n': /* Register list for 32-bit lwm and swm. */
13320 gas_assert (mips_opts.micromips);
13321 {
13322 /* A comma-separated list of registers and/or
13323 dash-separated contiguous ranges including
13324 at least one of ra and a set of one or more
13325 registers starting at s0 up to s7 and then
13326 s8 which have to be consecutive, e.g.:
13327
13328 ra
13329 s0
13330 ra, s0, s1, s2
13331 s0-s8
13332 s0-s5, ra
13333
13334 and any permutations of these. */
13335 unsigned int reglist;
13336 int imm;
13337 int ra;
13338
13339 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13340 break;
13341
13342 if ((reglist & 0x3f00ffff) != 0)
13343 break;
13344
13345 ra = (reglist >> 27) & 0x10;
13346 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13347 reglist += 1;
13348 if ((reglist & -reglist) != reglist)
13349 break;
13350
13351 imm = (ffs (reglist) - 1) | ra;
13352 INSERT_OPERAND (1, RT, *ip, imm);
13353 imm_expr.X_op = O_absent;
13354 }
60b63b72
RS
13355 continue;
13356
df58fc94
RS
13357 case '|': /* 4-bit trap code. */
13358 gas_assert (mips_opts.micromips);
60b63b72
RS
13359 my_getExpression (&imm_expr, s);
13360 check_absolute_expr (ip, &imm_expr);
60b63b72 13361 if ((unsigned long) imm_expr.X_add_number
df58fc94
RS
13362 > MICROMIPSOP_MASK_TRAP)
13363 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13364 (unsigned long) imm_expr.X_add_number,
13365 ip->insn_mo->name);
13366 INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
60b63b72
RS
13367 imm_expr.X_op = O_absent;
13368 s = expr_end;
13369 continue;
13370
252b5132 13371 default:
f71d0d44 13372 as_bad (_("Bad char = '%c'\n"), *args);
b37df7c4 13373 abort ();
252b5132
RH
13374 }
13375 break;
13376 }
13377 /* Args don't match. */
df58fc94
RS
13378 s = argsStart;
13379 insn_error = _("Illegal operands");
13380 if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
252b5132
RH
13381 {
13382 ++insn;
252b5132
RH
13383 continue;
13384 }
df58fc94
RS
13385 else if (wrong_delay_slot_insns && need_delay_slot_ok)
13386 {
13387 gas_assert (firstinsn);
13388 need_delay_slot_ok = FALSE;
13389 past = insn + 1;
13390 insn = firstinsn;
13391 continue;
13392 }
252b5132
RH
13393 return;
13394 }
13395}
13396
0499d65b
TS
13397#define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13398
252b5132
RH
13399/* This routine assembles an instruction into its binary format when
13400 assembling for the mips16. As a side effect, it sets one of the
df58fc94
RS
13401 global variables imm_reloc or offset_reloc to the type of relocation
13402 to do if one of the operands is an address expression. It also sets
13403 forced_insn_length to the resulting instruction size in bytes if the
13404 user explicitly requested a small or extended instruction. */
252b5132
RH
13405
13406static void
17a2f251 13407mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
13408{
13409 char *s;
13410 const char *args;
13411 struct mips_opcode *insn;
13412 char *argsstart;
13413 unsigned int regno;
13414 unsigned int lastregno = 0;
13415 char *s_reset;
d6f16593 13416 size_t i;
252b5132
RH
13417
13418 insn_error = NULL;
13419
df58fc94 13420 forced_insn_length = 0;
252b5132 13421
3882b010 13422 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
13423 ;
13424 switch (*s)
13425 {
13426 case '\0':
13427 break;
13428
13429 case ' ':
13430 *s++ = '\0';
13431 break;
13432
13433 case '.':
13434 if (s[1] == 't' && s[2] == ' ')
13435 {
13436 *s = '\0';
df58fc94 13437 forced_insn_length = 2;
252b5132
RH
13438 s += 3;
13439 break;
13440 }
13441 else if (s[1] == 'e' && s[2] == ' ')
13442 {
13443 *s = '\0';
df58fc94 13444 forced_insn_length = 4;
252b5132
RH
13445 s += 3;
13446 break;
13447 }
13448 /* Fall through. */
13449 default:
13450 insn_error = _("unknown opcode");
13451 return;
13452 }
13453
df58fc94
RS
13454 if (mips_opts.noautoextend && !forced_insn_length)
13455 forced_insn_length = 2;
252b5132
RH
13456
13457 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13458 {
13459 insn_error = _("unrecognized opcode");
13460 return;
13461 }
13462
13463 argsstart = s;
13464 for (;;)
13465 {
9b3f89ee
TS
13466 bfd_boolean ok;
13467
9c2799c2 13468 gas_assert (strcmp (insn->name, str) == 0);
252b5132 13469
037b32b9 13470 ok = is_opcode_valid_16 (insn);
9b3f89ee
TS
13471 if (! ok)
13472 {
13473 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13474 && strcmp (insn->name, insn[1].name) == 0)
13475 {
13476 ++insn;
13477 continue;
13478 }
13479 else
13480 {
13481 if (!insn_error)
13482 {
13483 static char buf[100];
13484 sprintf (buf,
7bd942df 13485 _("Opcode not supported on this processor: %s (%s)"),
9b3f89ee
TS
13486 mips_cpu_info_from_arch (mips_opts.arch)->name,
13487 mips_cpu_info_from_isa (mips_opts.isa)->name);
13488 insn_error = buf;
13489 }
13490 return;
13491 }
13492 }
13493
1e915849 13494 create_insn (ip, insn);
252b5132 13495 imm_expr.X_op = O_absent;
f6688943
TS
13496 imm_reloc[0] = BFD_RELOC_UNUSED;
13497 imm_reloc[1] = BFD_RELOC_UNUSED;
13498 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 13499 imm2_expr.X_op = O_absent;
252b5132 13500 offset_expr.X_op = O_absent;
f6688943
TS
13501 offset_reloc[0] = BFD_RELOC_UNUSED;
13502 offset_reloc[1] = BFD_RELOC_UNUSED;
13503 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
13504 for (args = insn->args; 1; ++args)
13505 {
13506 int c;
13507
13508 if (*s == ' ')
13509 ++s;
13510
13511 /* In this switch statement we call break if we did not find
13512 a match, continue if we did find a match, or return if we
13513 are done. */
13514
13515 c = *args;
13516 switch (c)
13517 {
13518 case '\0':
13519 if (*s == '\0')
13520 {
b886a2ab
RS
13521 offsetT value;
13522
252b5132
RH
13523 /* Stuff the immediate value in now, if we can. */
13524 if (imm_expr.X_op == O_constant
f6688943 13525 && *imm_reloc > BFD_RELOC_UNUSED
b886a2ab
RS
13526 && insn->pinfo != INSN_MACRO
13527 && calculate_reloc (*offset_reloc,
13528 imm_expr.X_add_number, &value))
252b5132 13529 {
c4e7957c 13530 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b886a2ab 13531 *offset_reloc, value, forced_insn_length,
43c0598f 13532 &ip->insn_opcode);
252b5132 13533 imm_expr.X_op = O_absent;
f6688943 13534 *imm_reloc = BFD_RELOC_UNUSED;
43c0598f 13535 *offset_reloc = BFD_RELOC_UNUSED;
252b5132
RH
13536 }
13537
13538 return;
13539 }
13540 break;
13541
13542 case ',':
13543 if (*s++ == c)
13544 continue;
13545 s--;
13546 switch (*++args)
13547 {
13548 case 'v':
bf12938e 13549 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
13550 continue;
13551 case 'w':
bf12938e 13552 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
13553 continue;
13554 }
13555 break;
13556
13557 case '(':
13558 case ')':
13559 if (*s++ == c)
13560 continue;
13561 break;
13562
13563 case 'v':
13564 case 'w':
13565 if (s[0] != '$')
13566 {
13567 if (c == 'v')
bf12938e 13568 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 13569 else
bf12938e 13570 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
13571 ++args;
13572 continue;
13573 }
13574 /* Fall through. */
13575 case 'x':
13576 case 'y':
13577 case 'z':
13578 case 'Z':
13579 case '0':
13580 case 'S':
13581 case 'R':
13582 case 'X':
13583 case 'Y':
707bfff6
TS
13584 s_reset = s;
13585 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
252b5132 13586 {
707bfff6 13587 if (c == 'v' || c == 'w')
85b51719 13588 {
707bfff6 13589 if (c == 'v')
a9e24354 13590 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
707bfff6 13591 else
a9e24354 13592 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
707bfff6
TS
13593 ++args;
13594 continue;
85b51719 13595 }
707bfff6 13596 break;
252b5132
RH
13597 }
13598
13599 if (*s == ' ')
13600 ++s;
13601 if (args[1] != *s)
13602 {
13603 if (c == 'v' || c == 'w')
13604 {
13605 regno = mips16_to_32_reg_map[lastregno];
13606 s = s_reset;
f9419b05 13607 ++args;
252b5132
RH
13608 }
13609 }
13610
13611 switch (c)
13612 {
13613 case 'x':
13614 case 'y':
13615 case 'z':
13616 case 'v':
13617 case 'w':
13618 case 'Z':
13619 regno = mips32_to_16_reg_map[regno];
13620 break;
13621
13622 case '0':
13623 if (regno != 0)
13624 regno = ILLEGAL_REG;
13625 break;
13626
13627 case 'S':
13628 if (regno != SP)
13629 regno = ILLEGAL_REG;
13630 break;
13631
13632 case 'R':
13633 if (regno != RA)
13634 regno = ILLEGAL_REG;
13635 break;
13636
13637 case 'X':
13638 case 'Y':
741fe287
MR
13639 if (regno == AT && mips_opts.at)
13640 {
13641 if (mips_opts.at == ATREG)
13642 as_warn (_("used $at without \".set noat\""));
13643 else
13644 as_warn (_("used $%u with \".set at=$%u\""),
13645 regno, mips_opts.at);
13646 }
252b5132
RH
13647 break;
13648
13649 default:
b37df7c4 13650 abort ();
252b5132
RH
13651 }
13652
13653 if (regno == ILLEGAL_REG)
13654 break;
13655
13656 switch (c)
13657 {
13658 case 'x':
13659 case 'v':
bf12938e 13660 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
13661 break;
13662 case 'y':
13663 case 'w':
bf12938e 13664 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
13665 break;
13666 case 'z':
bf12938e 13667 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
13668 break;
13669 case 'Z':
bf12938e 13670 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
13671 case '0':
13672 case 'S':
13673 case 'R':
13674 break;
13675 case 'X':
bf12938e 13676 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
13677 break;
13678 case 'Y':
13679 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 13680 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
13681 break;
13682 default:
b37df7c4 13683 abort ();
252b5132
RH
13684 }
13685
13686 lastregno = regno;
13687 continue;
13688
13689 case 'P':
13690 if (strncmp (s, "$pc", 3) == 0)
13691 {
13692 s += 3;
13693 continue;
13694 }
13695 break;
13696
252b5132
RH
13697 case '5':
13698 case 'H':
13699 case 'W':
13700 case 'D':
13701 case 'j':
252b5132
RH
13702 case 'V':
13703 case 'C':
13704 case 'U':
13705 case 'k':
13706 case 'K':
d6f16593
MR
13707 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
13708 if (i > 0)
252b5132 13709 {
d6f16593 13710 if (imm_expr.X_op != O_constant)
252b5132 13711 {
df58fc94 13712 forced_insn_length = 4;
5c04167a 13713 ip->insn_opcode |= MIPS16_EXTEND;
252b5132 13714 }
d6f16593
MR
13715 else
13716 {
13717 /* We need to relax this instruction. */
13718 *offset_reloc = *imm_reloc;
13719 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13720 }
13721 s = expr_end;
13722 continue;
252b5132 13723 }
d6f16593
MR
13724 *imm_reloc = BFD_RELOC_UNUSED;
13725 /* Fall through. */
13726 case '<':
13727 case '>':
13728 case '[':
13729 case ']':
13730 case '4':
13731 case '8':
13732 my_getExpression (&imm_expr, s);
252b5132
RH
13733 if (imm_expr.X_op == O_register)
13734 {
13735 /* What we thought was an expression turned out to
13736 be a register. */
13737
13738 if (s[0] == '(' && args[1] == '(')
13739 {
13740 /* It looks like the expression was omitted
13741 before a register indirection, which means
13742 that the expression is implicitly zero. We
13743 still set up imm_expr, so that we handle
13744 explicit extensions correctly. */
13745 imm_expr.X_op = O_constant;
13746 imm_expr.X_add_number = 0;
f6688943 13747 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13748 continue;
13749 }
13750
13751 break;
13752 }
13753
13754 /* We need to relax this instruction. */
f6688943 13755 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13756 s = expr_end;
13757 continue;
13758
13759 case 'p':
13760 case 'q':
13761 case 'A':
13762 case 'B':
13763 case 'E':
13764 /* We use offset_reloc rather than imm_reloc for the PC
13765 relative operands. This lets macros with both
13766 immediate and address operands work correctly. */
13767 my_getExpression (&offset_expr, s);
13768
13769 if (offset_expr.X_op == O_register)
13770 break;
13771
13772 /* We need to relax this instruction. */
f6688943 13773 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13774 s = expr_end;
13775 continue;
13776
13777 case '6': /* break code */
13778 my_getExpression (&imm_expr, s);
13779 check_absolute_expr (ip, &imm_expr);
13780 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
13781 as_warn (_("Invalid value for `%s' (%lu)"),
13782 ip->insn_mo->name,
13783 (unsigned long) imm_expr.X_add_number);
13784 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
13785 imm_expr.X_op = O_absent;
13786 s = expr_end;
13787 continue;
13788
13789 case 'a': /* 26 bit address */
13790 my_getExpression (&offset_expr, s);
13791 s = expr_end;
f6688943 13792 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
13793 ip->insn_opcode <<= 16;
13794 continue;
13795
13796 case 'l': /* register list for entry macro */
13797 case 'L': /* register list for exit macro */
13798 {
13799 int mask;
13800
13801 if (c == 'l')
13802 mask = 0;
13803 else
13804 mask = 7 << 3;
13805 while (*s != '\0')
13806 {
707bfff6 13807 unsigned int freg, reg1, reg2;
252b5132
RH
13808
13809 while (*s == ' ' || *s == ',')
13810 ++s;
707bfff6 13811 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
252b5132 13812 freg = 0;
707bfff6
TS
13813 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
13814 freg = 1;
252b5132
RH
13815 else
13816 {
707bfff6
TS
13817 as_bad (_("can't parse register list"));
13818 break;
252b5132
RH
13819 }
13820 if (*s == ' ')
13821 ++s;
13822 if (*s != '-')
13823 reg2 = reg1;
13824 else
13825 {
13826 ++s;
707bfff6
TS
13827 if (!reg_lookup (&s, freg ? RTYPE_FPU
13828 : (RTYPE_GP | RTYPE_NUM), &reg2))
252b5132 13829 {
707bfff6
TS
13830 as_bad (_("invalid register list"));
13831 break;
252b5132
RH
13832 }
13833 }
13834 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
13835 {
13836 mask &= ~ (7 << 3);
13837 mask |= 5 << 3;
13838 }
13839 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
13840 {
13841 mask &= ~ (7 << 3);
13842 mask |= 6 << 3;
13843 }
13844 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
13845 mask |= (reg2 - 3) << 3;
13846 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
13847 mask |= (reg2 - 15) << 1;
f9419b05 13848 else if (reg1 == RA && reg2 == RA)
252b5132
RH
13849 mask |= 1;
13850 else
13851 {
13852 as_bad (_("invalid register list"));
13853 break;
13854 }
13855 }
13856 /* The mask is filled in in the opcode table for the
13857 benefit of the disassembler. We remove it before
13858 applying the actual mask. */
13859 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
13860 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
13861 }
13862 continue;
13863
0499d65b
TS
13864 case 'm': /* Register list for save insn. */
13865 case 'M': /* Register list for restore insn. */
13866 {
5c04167a 13867 int opcode = ip->insn_opcode;
0499d65b 13868 int framesz = 0, seen_framesz = 0;
91d6fa6a 13869 int nargs = 0, statics = 0, sregs = 0;
0499d65b
TS
13870
13871 while (*s != '\0')
13872 {
13873 unsigned int reg1, reg2;
13874
13875 SKIP_SPACE_TABS (s);
13876 while (*s == ',')
13877 ++s;
13878 SKIP_SPACE_TABS (s);
13879
13880 my_getExpression (&imm_expr, s);
13881 if (imm_expr.X_op == O_constant)
13882 {
13883 /* Handle the frame size. */
13884 if (seen_framesz)
13885 {
13886 as_bad (_("more than one frame size in list"));
13887 break;
13888 }
13889 seen_framesz = 1;
13890 framesz = imm_expr.X_add_number;
13891 imm_expr.X_op = O_absent;
13892 s = expr_end;
13893 continue;
13894 }
13895
707bfff6 13896 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
0499d65b
TS
13897 {
13898 as_bad (_("can't parse register list"));
13899 break;
13900 }
0499d65b 13901
707bfff6
TS
13902 while (*s == ' ')
13903 ++s;
13904
0499d65b
TS
13905 if (*s != '-')
13906 reg2 = reg1;
13907 else
13908 {
13909 ++s;
707bfff6
TS
13910 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
13911 || reg2 < reg1)
0499d65b
TS
13912 {
13913 as_bad (_("can't parse register list"));
13914 break;
13915 }
0499d65b
TS
13916 }
13917
13918 while (reg1 <= reg2)
13919 {
13920 if (reg1 >= 4 && reg1 <= 7)
13921 {
3a93f742 13922 if (!seen_framesz)
0499d65b 13923 /* args $a0-$a3 */
91d6fa6a 13924 nargs |= 1 << (reg1 - 4);
0499d65b
TS
13925 else
13926 /* statics $a0-$a3 */
13927 statics |= 1 << (reg1 - 4);
13928 }
13929 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
13930 {
13931 /* $s0-$s8 */
13932 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
13933 }
13934 else if (reg1 == 31)
13935 {
13936 /* Add $ra to insn. */
13937 opcode |= 0x40;
13938 }
13939 else
13940 {
13941 as_bad (_("unexpected register in list"));
13942 break;
13943 }
13944 if (++reg1 == 24)
13945 reg1 = 30;
13946 }
13947 }
13948
13949 /* Encode args/statics combination. */
91d6fa6a 13950 if (nargs & statics)
0499d65b 13951 as_bad (_("arg/static registers overlap"));
91d6fa6a 13952 else if (nargs == 0xf)
0499d65b
TS
13953 /* All $a0-$a3 are args. */
13954 opcode |= MIPS16_ALL_ARGS << 16;
13955 else if (statics == 0xf)
13956 /* All $a0-$a3 are statics. */
13957 opcode |= MIPS16_ALL_STATICS << 16;
13958 else
13959 {
13960 int narg = 0, nstat = 0;
13961
13962 /* Count arg registers. */
91d6fa6a 13963 while (nargs & 0x1)
0499d65b 13964 {
91d6fa6a 13965 nargs >>= 1;
0499d65b
TS
13966 narg++;
13967 }
91d6fa6a 13968 if (nargs != 0)
0499d65b
TS
13969 as_bad (_("invalid arg register list"));
13970
13971 /* Count static registers. */
13972 while (statics & 0x8)
13973 {
13974 statics = (statics << 1) & 0xf;
13975 nstat++;
13976 }
13977 if (statics != 0)
13978 as_bad (_("invalid static register list"));
13979
13980 /* Encode args/statics. */
13981 opcode |= ((narg << 2) | nstat) << 16;
13982 }
13983
13984 /* Encode $s0/$s1. */
13985 if (sregs & (1 << 0)) /* $s0 */
13986 opcode |= 0x20;
13987 if (sregs & (1 << 1)) /* $s1 */
13988 opcode |= 0x10;
13989 sregs >>= 2;
13990
13991 if (sregs != 0)
13992 {
13993 /* Count regs $s2-$s8. */
13994 int nsreg = 0;
13995 while (sregs & 1)
13996 {
13997 sregs >>= 1;
13998 nsreg++;
13999 }
14000 if (sregs != 0)
14001 as_bad (_("invalid static register list"));
14002 /* Encode $s2-$s8. */
14003 opcode |= nsreg << 24;
14004 }
14005
14006 /* Encode frame size. */
14007 if (!seen_framesz)
14008 as_bad (_("missing frame size"));
14009 else if ((framesz & 7) != 0 || framesz < 0
14010 || framesz > 0xff * 8)
14011 as_bad (_("invalid frame size"));
14012 else if (framesz != 128 || (opcode >> 16) != 0)
14013 {
14014 framesz /= 8;
14015 opcode |= (((framesz & 0xf0) << 16)
14016 | (framesz & 0x0f));
14017 }
14018
14019 /* Finally build the instruction. */
14020 if ((opcode >> 16) != 0 || framesz == 0)
5c04167a
RS
14021 opcode |= MIPS16_EXTEND;
14022 ip->insn_opcode = opcode;
0499d65b
TS
14023 }
14024 continue;
14025
252b5132
RH
14026 case 'e': /* extend code */
14027 my_getExpression (&imm_expr, s);
14028 check_absolute_expr (ip, &imm_expr);
14029 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
14030 {
14031 as_warn (_("Invalid value for `%s' (%lu)"),
14032 ip->insn_mo->name,
14033 (unsigned long) imm_expr.X_add_number);
14034 imm_expr.X_add_number &= 0x7ff;
14035 }
14036 ip->insn_opcode |= imm_expr.X_add_number;
14037 imm_expr.X_op = O_absent;
14038 s = expr_end;
14039 continue;
14040
14041 default:
b37df7c4 14042 abort ();
252b5132
RH
14043 }
14044 break;
14045 }
14046
14047 /* Args don't match. */
14048 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
14049 strcmp (insn->name, insn[1].name) == 0)
14050 {
14051 ++insn;
14052 s = argsstart;
14053 continue;
14054 }
14055
14056 insn_error = _("illegal operands");
14057
14058 return;
14059 }
14060}
14061
14062/* This structure holds information we know about a mips16 immediate
14063 argument type. */
14064
e972090a
NC
14065struct mips16_immed_operand
14066{
252b5132
RH
14067 /* The type code used in the argument string in the opcode table. */
14068 int type;
14069 /* The number of bits in the short form of the opcode. */
14070 int nbits;
14071 /* The number of bits in the extended form of the opcode. */
14072 int extbits;
14073 /* The amount by which the short form is shifted when it is used;
14074 for example, the sw instruction has a shift count of 2. */
14075 int shift;
14076 /* The amount by which the short form is shifted when it is stored
14077 into the instruction code. */
14078 int op_shift;
14079 /* Non-zero if the short form is unsigned. */
14080 int unsp;
14081 /* Non-zero if the extended form is unsigned. */
14082 int extu;
14083 /* Non-zero if the value is PC relative. */
14084 int pcrel;
14085};
14086
14087/* The mips16 immediate operand types. */
14088
14089static const struct mips16_immed_operand mips16_immed_operands[] =
14090{
14091 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14092 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14093 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14094 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14095 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14096 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14097 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14098 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14099 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14100 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14101 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14102 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14103 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14104 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14105 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14106 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14107 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14108 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14109 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14110 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14111 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14112};
14113
14114#define MIPS16_NUM_IMMED \
14115 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14116
b886a2ab
RS
14117/* Marshal immediate value VAL for an extended MIPS16 instruction.
14118 NBITS is the number of significant bits in VAL. */
14119
14120static unsigned long
14121mips16_immed_extend (offsetT val, unsigned int nbits)
14122{
14123 int extval;
14124 if (nbits == 16)
14125 {
14126 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14127 val &= 0x1f;
14128 }
14129 else if (nbits == 15)
14130 {
14131 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14132 val &= 0xf;
14133 }
14134 else
14135 {
14136 extval = ((val & 0x1f) << 6) | (val & 0x20);
14137 val = 0;
14138 }
14139 return (extval << 16) | val;
14140}
14141
5c04167a
RS
14142/* Install immediate value VAL into MIPS16 instruction *INSN,
14143 extending it if necessary. The instruction in *INSN may
14144 already be extended.
14145
43c0598f
RS
14146 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14147 if none. In the former case, VAL is a 16-bit number with no
14148 defined signedness.
14149
14150 TYPE is the type of the immediate field. USER_INSN_LENGTH
14151 is the length that the user requested, or 0 if none. */
252b5132
RH
14152
14153static void
43c0598f
RS
14154mips16_immed (char *file, unsigned int line, int type,
14155 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14156 unsigned int user_insn_length, unsigned long *insn)
252b5132 14157{
3994f87e 14158 const struct mips16_immed_operand *op;
252b5132 14159 int mintiny, maxtiny;
252b5132
RH
14160
14161 op = mips16_immed_operands;
14162 while (op->type != type)
14163 {
14164 ++op;
9c2799c2 14165 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
14166 }
14167
14168 if (op->unsp)
14169 {
14170 if (type == '<' || type == '>' || type == '[' || type == ']')
14171 {
14172 mintiny = 1;
14173 maxtiny = 1 << op->nbits;
14174 }
14175 else
14176 {
14177 mintiny = 0;
14178 maxtiny = (1 << op->nbits) - 1;
14179 }
43c0598f
RS
14180 if (reloc != BFD_RELOC_UNUSED)
14181 val &= 0xffff;
252b5132
RH
14182 }
14183 else
14184 {
14185 mintiny = - (1 << (op->nbits - 1));
14186 maxtiny = (1 << (op->nbits - 1)) - 1;
43c0598f
RS
14187 if (reloc != BFD_RELOC_UNUSED)
14188 val = SEXT_16BIT (val);
252b5132
RH
14189 }
14190
14191 /* Branch offsets have an implicit 0 in the lowest bit. */
14192 if (type == 'p' || type == 'q')
14193 val /= 2;
14194
14195 if ((val & ((1 << op->shift) - 1)) != 0
14196 || val < (mintiny << op->shift)
14197 || val > (maxtiny << op->shift))
5c04167a
RS
14198 {
14199 /* We need an extended instruction. */
14200 if (user_insn_length == 2)
14201 as_bad_where (file, line, _("invalid unextended operand value"));
14202 else
14203 *insn |= MIPS16_EXTEND;
14204 }
14205 else if (user_insn_length == 4)
14206 {
14207 /* The operand doesn't force an unextended instruction to be extended.
14208 Warn if the user wanted an extended instruction anyway. */
14209 *insn |= MIPS16_EXTEND;
14210 as_warn_where (file, line,
14211 _("extended operand requested but not required"));
14212 }
252b5132 14213
5c04167a 14214 if (mips16_opcode_length (*insn) == 2)
252b5132
RH
14215 {
14216 int insnval;
14217
252b5132
RH
14218 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14219 insnval <<= op->op_shift;
14220 *insn |= insnval;
14221 }
14222 else
14223 {
14224 long minext, maxext;
252b5132 14225
43c0598f 14226 if (reloc == BFD_RELOC_UNUSED)
252b5132 14227 {
43c0598f
RS
14228 if (op->extu)
14229 {
14230 minext = 0;
14231 maxext = (1 << op->extbits) - 1;
14232 }
14233 else
14234 {
14235 minext = - (1 << (op->extbits - 1));
14236 maxext = (1 << (op->extbits - 1)) - 1;
14237 }
14238 if (val < minext || val > maxext)
14239 as_bad_where (file, line,
14240 _("operand value out of range for instruction"));
252b5132 14241 }
252b5132 14242
b886a2ab 14243 *insn |= mips16_immed_extend (val, op->extbits);
252b5132
RH
14244 }
14245}
14246\f
d6f16593 14247struct percent_op_match
ad8d3bb3 14248{
5e0116d5
RS
14249 const char *str;
14250 bfd_reloc_code_real_type reloc;
d6f16593
MR
14251};
14252
14253static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14254{
5e0116d5 14255 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 14256#ifdef OBJ_ELF
5e0116d5
RS
14257 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14258 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14259 {"%call16", BFD_RELOC_MIPS_CALL16},
14260 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14261 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14262 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14263 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14264 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14265 {"%got", BFD_RELOC_MIPS_GOT16},
14266 {"%gp_rel", BFD_RELOC_GPREL16},
14267 {"%half", BFD_RELOC_16},
14268 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14269 {"%higher", BFD_RELOC_MIPS_HIGHER},
14270 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14271 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14272 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14273 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14274 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14275 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14276 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14277 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
ad8d3bb3 14278#endif
5e0116d5 14279 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
14280};
14281
d6f16593
MR
14282static const struct percent_op_match mips16_percent_op[] =
14283{
14284 {"%lo", BFD_RELOC_MIPS16_LO16},
14285 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14286 {"%got", BFD_RELOC_MIPS16_GOT16},
14287 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14288 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14289 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14290 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14291 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14292 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14293 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14294 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14295 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14296};
14297
252b5132 14298
5e0116d5
RS
14299/* Return true if *STR points to a relocation operator. When returning true,
14300 move *STR over the operator and store its relocation code in *RELOC.
14301 Leave both *STR and *RELOC alone when returning false. */
14302
14303static bfd_boolean
17a2f251 14304parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14305{
d6f16593
MR
14306 const struct percent_op_match *percent_op;
14307 size_t limit, i;
14308
14309 if (mips_opts.mips16)
14310 {
14311 percent_op = mips16_percent_op;
14312 limit = ARRAY_SIZE (mips16_percent_op);
14313 }
14314 else
14315 {
14316 percent_op = mips_percent_op;
14317 limit = ARRAY_SIZE (mips_percent_op);
14318 }
76b3015f 14319
d6f16593 14320 for (i = 0; i < limit; i++)
5e0116d5 14321 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14322 {
3f98094e
DJ
14323 int len = strlen (percent_op[i].str);
14324
14325 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14326 continue;
14327
5e0116d5
RS
14328 *str += strlen (percent_op[i].str);
14329 *reloc = percent_op[i].reloc;
394f9b3a 14330
5e0116d5
RS
14331 /* Check whether the output BFD supports this relocation.
14332 If not, issue an error and fall back on something safe. */
14333 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14334 {
20203fb9 14335 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14336 percent_op[i].str);
01a3f561 14337 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14338 }
5e0116d5 14339 return TRUE;
394f9b3a 14340 }
5e0116d5 14341 return FALSE;
394f9b3a 14342}
ad8d3bb3 14343
ad8d3bb3 14344
5e0116d5
RS
14345/* Parse string STR as a 16-bit relocatable operand. Store the
14346 expression in *EP and the relocations in the array starting
14347 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14348
01a3f561 14349 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14350
5e0116d5 14351static size_t
17a2f251
TS
14352my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14353 char *str)
ad8d3bb3 14354{
5e0116d5
RS
14355 bfd_reloc_code_real_type reversed_reloc[3];
14356 size_t reloc_index, i;
09b8f35a
RS
14357 int crux_depth, str_depth;
14358 char *crux;
5e0116d5
RS
14359
14360 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14361 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14362 of the main expression and with CRUX_DEPTH containing the number
14363 of open brackets at that point. */
14364 reloc_index = -1;
14365 str_depth = 0;
14366 do
fb1b3232 14367 {
09b8f35a
RS
14368 reloc_index++;
14369 crux = str;
14370 crux_depth = str_depth;
14371
14372 /* Skip over whitespace and brackets, keeping count of the number
14373 of brackets. */
14374 while (*str == ' ' || *str == '\t' || *str == '(')
14375 if (*str++ == '(')
14376 str_depth++;
5e0116d5 14377 }
09b8f35a
RS
14378 while (*str == '%'
14379 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14380 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14381
09b8f35a 14382 my_getExpression (ep, crux);
5e0116d5 14383 str = expr_end;
394f9b3a 14384
5e0116d5 14385 /* Match every open bracket. */
09b8f35a 14386 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14387 if (*str++ == ')')
09b8f35a 14388 crux_depth--;
394f9b3a 14389
09b8f35a 14390 if (crux_depth > 0)
20203fb9 14391 as_bad (_("unclosed '('"));
394f9b3a 14392
5e0116d5 14393 expr_end = str;
252b5132 14394
01a3f561 14395 if (reloc_index != 0)
64bdfcaf
RS
14396 {
14397 prev_reloc_op_frag = frag_now;
14398 for (i = 0; i < reloc_index; i++)
14399 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14400 }
fb1b3232 14401
5e0116d5 14402 return reloc_index;
252b5132
RH
14403}
14404
14405static void
17a2f251 14406my_getExpression (expressionS *ep, char *str)
252b5132
RH
14407{
14408 char *save_in;
14409
14410 save_in = input_line_pointer;
14411 input_line_pointer = str;
14412 expression (ep);
14413 expr_end = input_line_pointer;
14414 input_line_pointer = save_in;
252b5132
RH
14415}
14416
252b5132 14417char *
17a2f251 14418md_atof (int type, char *litP, int *sizeP)
252b5132 14419{
499ac353 14420 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14421}
14422
14423void
17a2f251 14424md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14425{
14426 if (target_big_endian)
14427 number_to_chars_bigendian (buf, val, n);
14428 else
14429 number_to_chars_littleendian (buf, val, n);
14430}
14431\f
ae948b86 14432#ifdef OBJ_ELF
e013f690
TS
14433static int support_64bit_objects(void)
14434{
14435 const char **list, **l;
aa3d8fdf 14436 int yes;
e013f690
TS
14437
14438 list = bfd_target_list ();
14439 for (l = list; *l != NULL; l++)
aeffff67
RS
14440 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14441 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14442 break;
aa3d8fdf 14443 yes = (*l != NULL);
e013f690 14444 free (list);
aa3d8fdf 14445 return yes;
e013f690 14446}
ae948b86 14447#endif /* OBJ_ELF */
e013f690 14448
78849248 14449const char *md_shortopts = "O::g::G:";
252b5132 14450
23fce1e3
NC
14451enum options
14452 {
14453 OPTION_MARCH = OPTION_MD_BASE,
14454 OPTION_MTUNE,
14455 OPTION_MIPS1,
14456 OPTION_MIPS2,
14457 OPTION_MIPS3,
14458 OPTION_MIPS4,
14459 OPTION_MIPS5,
14460 OPTION_MIPS32,
14461 OPTION_MIPS64,
14462 OPTION_MIPS32R2,
14463 OPTION_MIPS64R2,
14464 OPTION_MIPS16,
14465 OPTION_NO_MIPS16,
14466 OPTION_MIPS3D,
14467 OPTION_NO_MIPS3D,
14468 OPTION_MDMX,
14469 OPTION_NO_MDMX,
14470 OPTION_DSP,
14471 OPTION_NO_DSP,
14472 OPTION_MT,
14473 OPTION_NO_MT,
14474 OPTION_SMARTMIPS,
14475 OPTION_NO_SMARTMIPS,
14476 OPTION_DSPR2,
14477 OPTION_NO_DSPR2,
df58fc94
RS
14478 OPTION_MICROMIPS,
14479 OPTION_NO_MICROMIPS,
dec0624d
MR
14480 OPTION_MCU,
14481 OPTION_NO_MCU,
23fce1e3
NC
14482 OPTION_COMPAT_ARCH_BASE,
14483 OPTION_M4650,
14484 OPTION_NO_M4650,
14485 OPTION_M4010,
14486 OPTION_NO_M4010,
14487 OPTION_M4100,
14488 OPTION_NO_M4100,
14489 OPTION_M3900,
14490 OPTION_NO_M3900,
14491 OPTION_M7000_HILO_FIX,
6a32d874
CM
14492 OPTION_MNO_7000_HILO_FIX,
14493 OPTION_FIX_24K,
14494 OPTION_NO_FIX_24K,
c67a084a
NC
14495 OPTION_FIX_LOONGSON2F_JUMP,
14496 OPTION_NO_FIX_LOONGSON2F_JUMP,
14497 OPTION_FIX_LOONGSON2F_NOP,
14498 OPTION_NO_FIX_LOONGSON2F_NOP,
23fce1e3
NC
14499 OPTION_FIX_VR4120,
14500 OPTION_NO_FIX_VR4120,
14501 OPTION_FIX_VR4130,
14502 OPTION_NO_FIX_VR4130,
d954098f
DD
14503 OPTION_FIX_CN63XXP1,
14504 OPTION_NO_FIX_CN63XXP1,
23fce1e3
NC
14505 OPTION_TRAP,
14506 OPTION_BREAK,
14507 OPTION_EB,
14508 OPTION_EL,
14509 OPTION_FP32,
14510 OPTION_GP32,
14511 OPTION_CONSTRUCT_FLOATS,
14512 OPTION_NO_CONSTRUCT_FLOATS,
14513 OPTION_FP64,
14514 OPTION_GP64,
14515 OPTION_RELAX_BRANCH,
14516 OPTION_NO_RELAX_BRANCH,
14517 OPTION_MSHARED,
14518 OPTION_MNO_SHARED,
14519 OPTION_MSYM32,
14520 OPTION_MNO_SYM32,
14521 OPTION_SOFT_FLOAT,
14522 OPTION_HARD_FLOAT,
14523 OPTION_SINGLE_FLOAT,
14524 OPTION_DOUBLE_FLOAT,
14525 OPTION_32,
14526#ifdef OBJ_ELF
14527 OPTION_CALL_SHARED,
14528 OPTION_CALL_NONPIC,
14529 OPTION_NON_SHARED,
14530 OPTION_XGOT,
14531 OPTION_MABI,
14532 OPTION_N32,
14533 OPTION_64,
14534 OPTION_MDEBUG,
14535 OPTION_NO_MDEBUG,
14536 OPTION_PDR,
14537 OPTION_NO_PDR,
14538 OPTION_MVXWORKS_PIC,
14539#endif /* OBJ_ELF */
14540 OPTION_END_OF_ENUM
14541 };
14542
e972090a
NC
14543struct option md_longopts[] =
14544{
f9b4148d 14545 /* Options which specify architecture. */
f9b4148d 14546 {"march", required_argument, NULL, OPTION_MARCH},
f9b4148d 14547 {"mtune", required_argument, NULL, OPTION_MTUNE},
252b5132
RH
14548 {"mips0", no_argument, NULL, OPTION_MIPS1},
14549 {"mips1", no_argument, NULL, OPTION_MIPS1},
252b5132 14550 {"mips2", no_argument, NULL, OPTION_MIPS2},
252b5132 14551 {"mips3", no_argument, NULL, OPTION_MIPS3},
252b5132 14552 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86 14553 {"mips5", no_argument, NULL, OPTION_MIPS5},
ae948b86 14554 {"mips32", no_argument, NULL, OPTION_MIPS32},
ae948b86 14555 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d 14556 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13 14557 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
14558
14559 /* Options which specify Application Specific Extensions (ASEs). */
f9b4148d 14560 {"mips16", no_argument, NULL, OPTION_MIPS16},
f9b4148d 14561 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
f9b4148d 14562 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
f9b4148d 14563 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
f9b4148d 14564 {"mdmx", no_argument, NULL, OPTION_MDMX},
f9b4148d 14565 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
74cd071d 14566 {"mdsp", no_argument, NULL, OPTION_DSP},
74cd071d 14567 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
ef2e4d86 14568 {"mmt", no_argument, NULL, OPTION_MT},
ef2e4d86 14569 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
e16bfa71 14570 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
e16bfa71 14571 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
8b082fb1 14572 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
8b082fb1 14573 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
df58fc94
RS
14574 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
14575 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
dec0624d
MR
14576 {"mmcu", no_argument, NULL, OPTION_MCU},
14577 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
f9b4148d
CD
14578
14579 /* Old-style architecture options. Don't add more of these. */
f9b4148d 14580 {"m4650", no_argument, NULL, OPTION_M4650},
f9b4148d 14581 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
f9b4148d 14582 {"m4010", no_argument, NULL, OPTION_M4010},
f9b4148d 14583 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
f9b4148d 14584 {"m4100", no_argument, NULL, OPTION_M4100},
f9b4148d 14585 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
f9b4148d 14586 {"m3900", no_argument, NULL, OPTION_M3900},
f9b4148d
CD
14587 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
14588
14589 /* Options which enable bug fixes. */
f9b4148d 14590 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
f9b4148d
CD
14591 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14592 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
c67a084a
NC
14593 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
14594 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
14595 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
14596 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
d766e8ec
RS
14597 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
14598 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
7d8e00cf
RS
14599 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
14600 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
6a32d874
CM
14601 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
14602 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
d954098f
DD
14603 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
14604 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
f9b4148d
CD
14605
14606 /* Miscellaneous options. */
252b5132
RH
14607 {"trap", no_argument, NULL, OPTION_TRAP},
14608 {"no-break", no_argument, NULL, OPTION_TRAP},
252b5132
RH
14609 {"break", no_argument, NULL, OPTION_BREAK},
14610 {"no-trap", no_argument, NULL, OPTION_BREAK},
252b5132 14611 {"EB", no_argument, NULL, OPTION_EB},
252b5132 14612 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 14613 {"mfp32", no_argument, NULL, OPTION_FP32},
c97ef257 14614 {"mgp32", no_argument, NULL, OPTION_GP32},
119d663a 14615 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
119d663a 14616 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
316f5878 14617 {"mfp64", no_argument, NULL, OPTION_FP64},
ae948b86 14618 {"mgp64", no_argument, NULL, OPTION_GP64},
4a6a3df4
AO
14619 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
14620 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
aa6975fb
ILT
14621 {"mshared", no_argument, NULL, OPTION_MSHARED},
14622 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
aed1a261
RS
14623 {"msym32", no_argument, NULL, OPTION_MSYM32},
14624 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
037b32b9
AN
14625 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
14626 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
037b32b9
AN
14627 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
14628 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
23fce1e3
NC
14629
14630 /* Strictly speaking this next option is ELF specific,
14631 but we allow it for other ports as well in order to
14632 make testing easier. */
14633 {"32", no_argument, NULL, OPTION_32},
037b32b9 14634
f9b4148d 14635 /* ELF-specific options. */
156c2f8b 14636#ifdef OBJ_ELF
156c2f8b
NC
14637 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
14638 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
861fb55a 14639 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
156c2f8b
NC
14640 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
14641 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86 14642 {"mabi", required_argument, NULL, OPTION_MABI},
e013f690 14643 {"n32", no_argument, NULL, OPTION_N32},
156c2f8b 14644 {"64", no_argument, NULL, OPTION_64},
ecb4347a 14645 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
ecb4347a 14646 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe 14647 {"mpdr", no_argument, NULL, OPTION_PDR},
dcd410fe 14648 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
0a44bf69 14649 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ae948b86 14650#endif /* OBJ_ELF */
f9b4148d 14651
252b5132
RH
14652 {NULL, no_argument, NULL, 0}
14653};
156c2f8b 14654size_t md_longopts_size = sizeof (md_longopts);
252b5132 14655
316f5878
RS
14656/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14657 NEW_VALUE. Warn if another value was already specified. Note:
14658 we have to defer parsing the -march and -mtune arguments in order
14659 to handle 'from-abi' correctly, since the ABI might be specified
14660 in a later argument. */
14661
14662static void
17a2f251 14663mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14664{
14665 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14666 as_warn (_("A different %s was already specified, is now %s"),
14667 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14668 new_value);
14669
14670 *string_ptr = new_value;
14671}
14672
252b5132 14673int
17a2f251 14674md_parse_option (int c, char *arg)
252b5132
RH
14675{
14676 switch (c)
14677 {
119d663a
NC
14678 case OPTION_CONSTRUCT_FLOATS:
14679 mips_disable_float_construction = 0;
14680 break;
bdaaa2e1 14681
119d663a
NC
14682 case OPTION_NO_CONSTRUCT_FLOATS:
14683 mips_disable_float_construction = 1;
14684 break;
bdaaa2e1 14685
252b5132
RH
14686 case OPTION_TRAP:
14687 mips_trap = 1;
14688 break;
14689
14690 case OPTION_BREAK:
14691 mips_trap = 0;
14692 break;
14693
14694 case OPTION_EB:
14695 target_big_endian = 1;
14696 break;
14697
14698 case OPTION_EL:
14699 target_big_endian = 0;
14700 break;
14701
14702 case 'O':
4ffff32f
TS
14703 if (arg == NULL)
14704 mips_optimize = 1;
14705 else if (arg[0] == '0')
14706 mips_optimize = 0;
14707 else if (arg[0] == '1')
252b5132
RH
14708 mips_optimize = 1;
14709 else
14710 mips_optimize = 2;
14711 break;
14712
14713 case 'g':
14714 if (arg == NULL)
14715 mips_debug = 2;
14716 else
14717 mips_debug = atoi (arg);
252b5132
RH
14718 break;
14719
14720 case OPTION_MIPS1:
316f5878 14721 file_mips_isa = ISA_MIPS1;
252b5132
RH
14722 break;
14723
14724 case OPTION_MIPS2:
316f5878 14725 file_mips_isa = ISA_MIPS2;
252b5132
RH
14726 break;
14727
14728 case OPTION_MIPS3:
316f5878 14729 file_mips_isa = ISA_MIPS3;
252b5132
RH
14730 break;
14731
14732 case OPTION_MIPS4:
316f5878 14733 file_mips_isa = ISA_MIPS4;
e7af610e
NC
14734 break;
14735
84ea6cf2 14736 case OPTION_MIPS5:
316f5878 14737 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
14738 break;
14739
e7af610e 14740 case OPTION_MIPS32:
316f5878 14741 file_mips_isa = ISA_MIPS32;
252b5132
RH
14742 break;
14743
af7ee8bf
CD
14744 case OPTION_MIPS32R2:
14745 file_mips_isa = ISA_MIPS32R2;
14746 break;
14747
5f74bc13
CD
14748 case OPTION_MIPS64R2:
14749 file_mips_isa = ISA_MIPS64R2;
14750 break;
14751
84ea6cf2 14752 case OPTION_MIPS64:
316f5878 14753 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
14754 break;
14755
ec68c924 14756 case OPTION_MTUNE:
316f5878
RS
14757 mips_set_option_string (&mips_tune_string, arg);
14758 break;
ec68c924 14759
316f5878
RS
14760 case OPTION_MARCH:
14761 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14762 break;
14763
14764 case OPTION_M4650:
316f5878
RS
14765 mips_set_option_string (&mips_arch_string, "4650");
14766 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14767 break;
14768
14769 case OPTION_NO_M4650:
14770 break;
14771
14772 case OPTION_M4010:
316f5878
RS
14773 mips_set_option_string (&mips_arch_string, "4010");
14774 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14775 break;
14776
14777 case OPTION_NO_M4010:
14778 break;
14779
14780 case OPTION_M4100:
316f5878
RS
14781 mips_set_option_string (&mips_arch_string, "4100");
14782 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14783 break;
14784
14785 case OPTION_NO_M4100:
14786 break;
14787
252b5132 14788 case OPTION_M3900:
316f5878
RS
14789 mips_set_option_string (&mips_arch_string, "3900");
14790 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14791 break;
bdaaa2e1 14792
252b5132
RH
14793 case OPTION_NO_M3900:
14794 break;
14795
deec1734
CD
14796 case OPTION_MDMX:
14797 mips_opts.ase_mdmx = 1;
14798 break;
14799
14800 case OPTION_NO_MDMX:
14801 mips_opts.ase_mdmx = 0;
14802 break;
14803
74cd071d
CF
14804 case OPTION_DSP:
14805 mips_opts.ase_dsp = 1;
8b082fb1 14806 mips_opts.ase_dspr2 = 0;
74cd071d
CF
14807 break;
14808
14809 case OPTION_NO_DSP:
8b082fb1
TS
14810 mips_opts.ase_dsp = 0;
14811 mips_opts.ase_dspr2 = 0;
14812 break;
14813
14814 case OPTION_DSPR2:
14815 mips_opts.ase_dspr2 = 1;
14816 mips_opts.ase_dsp = 1;
14817 break;
14818
14819 case OPTION_NO_DSPR2:
14820 mips_opts.ase_dspr2 = 0;
74cd071d
CF
14821 mips_opts.ase_dsp = 0;
14822 break;
14823
ef2e4d86
CF
14824 case OPTION_MT:
14825 mips_opts.ase_mt = 1;
14826 break;
14827
14828 case OPTION_NO_MT:
14829 mips_opts.ase_mt = 0;
14830 break;
14831
dec0624d
MR
14832 case OPTION_MCU:
14833 mips_opts.ase_mcu = 1;
14834 break;
14835
14836 case OPTION_NO_MCU:
14837 mips_opts.ase_mcu = 0;
14838 break;
14839
df58fc94
RS
14840 case OPTION_MICROMIPS:
14841 if (mips_opts.mips16 == 1)
14842 {
14843 as_bad (_("-mmicromips cannot be used with -mips16"));
14844 return 0;
14845 }
14846 mips_opts.micromips = 1;
14847 mips_no_prev_insn ();
14848 break;
14849
14850 case OPTION_NO_MICROMIPS:
14851 mips_opts.micromips = 0;
14852 mips_no_prev_insn ();
14853 break;
14854
252b5132 14855 case OPTION_MIPS16:
df58fc94
RS
14856 if (mips_opts.micromips == 1)
14857 {
14858 as_bad (_("-mips16 cannot be used with -micromips"));
14859 return 0;
14860 }
252b5132 14861 mips_opts.mips16 = 1;
7d10b47d 14862 mips_no_prev_insn ();
252b5132
RH
14863 break;
14864
14865 case OPTION_NO_MIPS16:
14866 mips_opts.mips16 = 0;
7d10b47d 14867 mips_no_prev_insn ();
252b5132
RH
14868 break;
14869
1f25f5d3
CD
14870 case OPTION_MIPS3D:
14871 mips_opts.ase_mips3d = 1;
14872 break;
14873
14874 case OPTION_NO_MIPS3D:
14875 mips_opts.ase_mips3d = 0;
14876 break;
14877
e16bfa71
TS
14878 case OPTION_SMARTMIPS:
14879 mips_opts.ase_smartmips = 1;
14880 break;
14881
14882 case OPTION_NO_SMARTMIPS:
14883 mips_opts.ase_smartmips = 0;
14884 break;
14885
6a32d874
CM
14886 case OPTION_FIX_24K:
14887 mips_fix_24k = 1;
14888 break;
14889
14890 case OPTION_NO_FIX_24K:
14891 mips_fix_24k = 0;
14892 break;
14893
c67a084a
NC
14894 case OPTION_FIX_LOONGSON2F_JUMP:
14895 mips_fix_loongson2f_jump = TRUE;
14896 break;
14897
14898 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14899 mips_fix_loongson2f_jump = FALSE;
14900 break;
14901
14902 case OPTION_FIX_LOONGSON2F_NOP:
14903 mips_fix_loongson2f_nop = TRUE;
14904 break;
14905
14906 case OPTION_NO_FIX_LOONGSON2F_NOP:
14907 mips_fix_loongson2f_nop = FALSE;
14908 break;
14909
d766e8ec
RS
14910 case OPTION_FIX_VR4120:
14911 mips_fix_vr4120 = 1;
60b63b72
RS
14912 break;
14913
d766e8ec
RS
14914 case OPTION_NO_FIX_VR4120:
14915 mips_fix_vr4120 = 0;
60b63b72
RS
14916 break;
14917
7d8e00cf
RS
14918 case OPTION_FIX_VR4130:
14919 mips_fix_vr4130 = 1;
14920 break;
14921
14922 case OPTION_NO_FIX_VR4130:
14923 mips_fix_vr4130 = 0;
14924 break;
14925
d954098f
DD
14926 case OPTION_FIX_CN63XXP1:
14927 mips_fix_cn63xxp1 = TRUE;
14928 break;
14929
14930 case OPTION_NO_FIX_CN63XXP1:
14931 mips_fix_cn63xxp1 = FALSE;
14932 break;
14933
4a6a3df4
AO
14934 case OPTION_RELAX_BRANCH:
14935 mips_relax_branch = 1;
14936 break;
14937
14938 case OPTION_NO_RELAX_BRANCH:
14939 mips_relax_branch = 0;
14940 break;
14941
aa6975fb
ILT
14942 case OPTION_MSHARED:
14943 mips_in_shared = TRUE;
14944 break;
14945
14946 case OPTION_MNO_SHARED:
14947 mips_in_shared = FALSE;
14948 break;
14949
aed1a261
RS
14950 case OPTION_MSYM32:
14951 mips_opts.sym32 = TRUE;
14952 break;
14953
14954 case OPTION_MNO_SYM32:
14955 mips_opts.sym32 = FALSE;
14956 break;
14957
0f074f60 14958#ifdef OBJ_ELF
252b5132
RH
14959 /* When generating ELF code, we permit -KPIC and -call_shared to
14960 select SVR4_PIC, and -non_shared to select no PIC. This is
14961 intended to be compatible with Irix 5. */
14962 case OPTION_CALL_SHARED:
f43abd2b 14963 if (!IS_ELF)
252b5132
RH
14964 {
14965 as_bad (_("-call_shared is supported only for ELF format"));
14966 return 0;
14967 }
14968 mips_pic = SVR4_PIC;
143d77c5 14969 mips_abicalls = TRUE;
252b5132
RH
14970 break;
14971
861fb55a
DJ
14972 case OPTION_CALL_NONPIC:
14973 if (!IS_ELF)
14974 {
14975 as_bad (_("-call_nonpic is supported only for ELF format"));
14976 return 0;
14977 }
14978 mips_pic = NO_PIC;
14979 mips_abicalls = TRUE;
14980 break;
14981
252b5132 14982 case OPTION_NON_SHARED:
f43abd2b 14983 if (!IS_ELF)
252b5132
RH
14984 {
14985 as_bad (_("-non_shared is supported only for ELF format"));
14986 return 0;
14987 }
14988 mips_pic = NO_PIC;
143d77c5 14989 mips_abicalls = FALSE;
252b5132
RH
14990 break;
14991
44075ae2
TS
14992 /* The -xgot option tells the assembler to use 32 bit offsets
14993 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
14994 compatibility. */
14995 case OPTION_XGOT:
14996 mips_big_got = 1;
14997 break;
0f074f60 14998#endif /* OBJ_ELF */
252b5132
RH
14999
15000 case 'G':
6caf9ef4
TS
15001 g_switch_value = atoi (arg);
15002 g_switch_seen = 1;
252b5132
RH
15003 break;
15004
34ba82a8
TS
15005 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15006 and -mabi=64. */
252b5132 15007 case OPTION_32:
23fce1e3
NC
15008 if (IS_ELF)
15009 mips_abi = O32_ABI;
15010 /* We silently ignore -32 for non-ELF targets. This greatly
15011 simplifies the construction of the MIPS GAS test cases. */
252b5132
RH
15012 break;
15013
23fce1e3 15014#ifdef OBJ_ELF
e013f690 15015 case OPTION_N32:
f43abd2b 15016 if (!IS_ELF)
34ba82a8
TS
15017 {
15018 as_bad (_("-n32 is supported for ELF format only"));
15019 return 0;
15020 }
316f5878 15021 mips_abi = N32_ABI;
e013f690 15022 break;
252b5132 15023
e013f690 15024 case OPTION_64:
f43abd2b 15025 if (!IS_ELF)
34ba82a8
TS
15026 {
15027 as_bad (_("-64 is supported for ELF format only"));
15028 return 0;
15029 }
316f5878 15030 mips_abi = N64_ABI;
f43abd2b 15031 if (!support_64bit_objects())
e013f690 15032 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 15033 break;
ae948b86 15034#endif /* OBJ_ELF */
252b5132 15035
c97ef257 15036 case OPTION_GP32:
a325df1d 15037 file_mips_gp32 = 1;
c97ef257
AH
15038 break;
15039
15040 case OPTION_GP64:
a325df1d 15041 file_mips_gp32 = 0;
c97ef257 15042 break;
252b5132 15043
ca4e0257 15044 case OPTION_FP32:
a325df1d 15045 file_mips_fp32 = 1;
316f5878
RS
15046 break;
15047
15048 case OPTION_FP64:
15049 file_mips_fp32 = 0;
ca4e0257
RS
15050 break;
15051
037b32b9
AN
15052 case OPTION_SINGLE_FLOAT:
15053 file_mips_single_float = 1;
15054 break;
15055
15056 case OPTION_DOUBLE_FLOAT:
15057 file_mips_single_float = 0;
15058 break;
15059
15060 case OPTION_SOFT_FLOAT:
15061 file_mips_soft_float = 1;
15062 break;
15063
15064 case OPTION_HARD_FLOAT:
15065 file_mips_soft_float = 0;
15066 break;
15067
ae948b86 15068#ifdef OBJ_ELF
252b5132 15069 case OPTION_MABI:
f43abd2b 15070 if (!IS_ELF)
34ba82a8
TS
15071 {
15072 as_bad (_("-mabi is supported for ELF format only"));
15073 return 0;
15074 }
e013f690 15075 if (strcmp (arg, "32") == 0)
316f5878 15076 mips_abi = O32_ABI;
e013f690 15077 else if (strcmp (arg, "o64") == 0)
316f5878 15078 mips_abi = O64_ABI;
e013f690 15079 else if (strcmp (arg, "n32") == 0)
316f5878 15080 mips_abi = N32_ABI;
e013f690
TS
15081 else if (strcmp (arg, "64") == 0)
15082 {
316f5878 15083 mips_abi = N64_ABI;
e013f690
TS
15084 if (! support_64bit_objects())
15085 as_fatal (_("No compiled in support for 64 bit object file "
15086 "format"));
15087 }
15088 else if (strcmp (arg, "eabi") == 0)
316f5878 15089 mips_abi = EABI_ABI;
e013f690 15090 else
da0e507f
TS
15091 {
15092 as_fatal (_("invalid abi -mabi=%s"), arg);
15093 return 0;
15094 }
252b5132 15095 break;
e013f690 15096#endif /* OBJ_ELF */
252b5132 15097
6b76fefe 15098 case OPTION_M7000_HILO_FIX:
b34976b6 15099 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
15100 break;
15101
9ee72ff1 15102 case OPTION_MNO_7000_HILO_FIX:
b34976b6 15103 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
15104 break;
15105
ecb4347a
DJ
15106#ifdef OBJ_ELF
15107 case OPTION_MDEBUG:
b34976b6 15108 mips_flag_mdebug = TRUE;
ecb4347a
DJ
15109 break;
15110
15111 case OPTION_NO_MDEBUG:
b34976b6 15112 mips_flag_mdebug = FALSE;
ecb4347a 15113 break;
dcd410fe
RO
15114
15115 case OPTION_PDR:
15116 mips_flag_pdr = TRUE;
15117 break;
15118
15119 case OPTION_NO_PDR:
15120 mips_flag_pdr = FALSE;
15121 break;
0a44bf69
RS
15122
15123 case OPTION_MVXWORKS_PIC:
15124 mips_pic = VXWORKS_PIC;
15125 break;
ecb4347a
DJ
15126#endif /* OBJ_ELF */
15127
252b5132
RH
15128 default:
15129 return 0;
15130 }
15131
c67a084a
NC
15132 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15133
252b5132
RH
15134 return 1;
15135}
316f5878
RS
15136\f
15137/* Set up globals to generate code for the ISA or processor
15138 described by INFO. */
252b5132 15139
252b5132 15140static void
17a2f251 15141mips_set_architecture (const struct mips_cpu_info *info)
252b5132 15142{
316f5878 15143 if (info != 0)
252b5132 15144 {
fef14a42
TS
15145 file_mips_arch = info->cpu;
15146 mips_opts.arch = info->cpu;
316f5878 15147 mips_opts.isa = info->isa;
252b5132 15148 }
252b5132
RH
15149}
15150
252b5132 15151
316f5878 15152/* Likewise for tuning. */
252b5132 15153
316f5878 15154static void
17a2f251 15155mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
15156{
15157 if (info != 0)
fef14a42 15158 mips_tune = info->cpu;
316f5878 15159}
80cc45a5 15160
34ba82a8 15161
252b5132 15162void
17a2f251 15163mips_after_parse_args (void)
e9670677 15164{
fef14a42
TS
15165 const struct mips_cpu_info *arch_info = 0;
15166 const struct mips_cpu_info *tune_info = 0;
15167
e9670677 15168 /* GP relative stuff not working for PE */
6caf9ef4 15169 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 15170 {
6caf9ef4 15171 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
15172 as_bad (_("-G not supported in this configuration."));
15173 g_switch_value = 0;
15174 }
15175
cac012d6
AO
15176 if (mips_abi == NO_ABI)
15177 mips_abi = MIPS_DEFAULT_ABI;
15178
22923709
RS
15179 /* The following code determines the architecture and register size.
15180 Similar code was added to GCC 3.3 (see override_options() in
15181 config/mips/mips.c). The GAS and GCC code should be kept in sync
15182 as much as possible. */
e9670677 15183
316f5878 15184 if (mips_arch_string != 0)
fef14a42 15185 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 15186
316f5878 15187 if (file_mips_isa != ISA_UNKNOWN)
e9670677 15188 {
316f5878 15189 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 15190 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 15191 the -march selection (if any). */
fef14a42 15192 if (arch_info != 0)
e9670677 15193 {
316f5878
RS
15194 /* -march takes precedence over -mipsN, since it is more descriptive.
15195 There's no harm in specifying both as long as the ISA levels
15196 are the same. */
fef14a42 15197 if (file_mips_isa != arch_info->isa)
316f5878
RS
15198 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15199 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 15200 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15201 }
316f5878 15202 else
fef14a42 15203 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
15204 }
15205
fef14a42 15206 if (arch_info == 0)
95bfe26e
MF
15207 {
15208 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15209 gas_assert (arch_info);
15210 }
e9670677 15211
fef14a42 15212 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15213 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15214 arch_info->name);
15215
15216 mips_set_architecture (arch_info);
15217
15218 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
15219 if (mips_tune_string != 0)
15220 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15221
fef14a42
TS
15222 if (tune_info == 0)
15223 mips_set_tune (arch_info);
15224 else
15225 mips_set_tune (tune_info);
e9670677 15226
316f5878 15227 if (file_mips_gp32 >= 0)
e9670677 15228 {
316f5878
RS
15229 /* The user specified the size of the integer registers. Make sure
15230 it agrees with the ABI and ISA. */
15231 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15232 as_bad (_("-mgp64 used with a 32-bit processor"));
15233 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15234 as_bad (_("-mgp32 used with a 64-bit ABI"));
15235 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15236 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
15237 }
15238 else
15239 {
316f5878
RS
15240 /* Infer the integer register size from the ABI and processor.
15241 Restrict ourselves to 32-bit registers if that's all the
15242 processor has, or if the ABI cannot handle 64-bit registers. */
15243 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15244 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
15245 }
15246
ad3fea08
TS
15247 switch (file_mips_fp32)
15248 {
15249 default:
15250 case -1:
15251 /* No user specified float register size.
15252 ??? GAS treats single-float processors as though they had 64-bit
15253 float registers (although it complains when double-precision
15254 instructions are used). As things stand, saying they have 32-bit
15255 registers would lead to spurious "register must be even" messages.
15256 So here we assume float registers are never smaller than the
15257 integer ones. */
15258 if (file_mips_gp32 == 0)
15259 /* 64-bit integer registers implies 64-bit float registers. */
15260 file_mips_fp32 = 0;
15261 else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
15262 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15263 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
15264 file_mips_fp32 = 0;
15265 else
15266 /* 32-bit float registers. */
15267 file_mips_fp32 = 1;
15268 break;
15269
15270 /* The user specified the size of the float registers. Check if it
15271 agrees with the ABI and ISA. */
15272 case 0:
15273 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15274 as_bad (_("-mfp64 used with a 32-bit fpu"));
15275 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15276 && !ISA_HAS_MXHC1 (mips_opts.isa))
15277 as_warn (_("-mfp64 used with a 32-bit ABI"));
15278 break;
15279 case 1:
15280 if (ABI_NEEDS_64BIT_REGS (mips_abi))
15281 as_warn (_("-mfp32 used with a 64-bit ABI"));
15282 break;
15283 }
e9670677 15284
316f5878 15285 /* End of GCC-shared inference code. */
e9670677 15286
17a2f251
TS
15287 /* This flag is set when we have a 64-bit capable CPU but use only
15288 32-bit wide registers. Note that EABI does not use it. */
15289 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15290 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15291 || mips_abi == O32_ABI))
316f5878 15292 mips_32bitmode = 1;
e9670677
MR
15293
15294 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15295 as_bad (_("trap exception not supported at ISA 1"));
15296
e9670677
MR
15297 /* If the selected architecture includes support for ASEs, enable
15298 generation of code for them. */
a4672219 15299 if (mips_opts.mips16 == -1)
fef14a42 15300 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
df58fc94
RS
15301 if (mips_opts.micromips == -1)
15302 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
ffdefa66 15303 if (mips_opts.ase_mips3d == -1)
65263ce3 15304 mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
ad3fea08
TS
15305 && file_mips_fp32 == 0) ? 1 : 0;
15306 if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
15307 as_bad (_("-mfp32 used with -mips3d"));
15308
ffdefa66 15309 if (mips_opts.ase_mdmx == -1)
65263ce3 15310 mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
ad3fea08
TS
15311 && file_mips_fp32 == 0) ? 1 : 0;
15312 if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
15313 as_bad (_("-mfp32 used with -mdmx"));
15314
15315 if (mips_opts.ase_smartmips == -1)
15316 mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
15317 if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
20203fb9
NC
15318 as_warn (_("%s ISA does not support SmartMIPS"),
15319 mips_cpu_info_from_isa (mips_opts.isa)->name);
ad3fea08 15320
74cd071d 15321 if (mips_opts.ase_dsp == -1)
ad3fea08
TS
15322 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15323 if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
20203fb9
NC
15324 as_warn (_("%s ISA does not support DSP ASE"),
15325 mips_cpu_info_from_isa (mips_opts.isa)->name);
ad3fea08 15326
8b082fb1
TS
15327 if (mips_opts.ase_dspr2 == -1)
15328 {
15329 mips_opts.ase_dspr2 = (arch_info->flags & MIPS_CPU_ASE_DSPR2) ? 1 : 0;
15330 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15331 }
15332 if (mips_opts.ase_dspr2 && !ISA_SUPPORTS_DSPR2_ASE)
20203fb9
NC
15333 as_warn (_("%s ISA does not support DSP R2 ASE"),
15334 mips_cpu_info_from_isa (mips_opts.isa)->name);
8b082fb1 15335
ef2e4d86 15336 if (mips_opts.ase_mt == -1)
ad3fea08
TS
15337 mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
15338 if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
20203fb9
NC
15339 as_warn (_("%s ISA does not support MT ASE"),
15340 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 15341
dec0624d
MR
15342 if (mips_opts.ase_mcu == -1)
15343 mips_opts.ase_mcu = (arch_info->flags & MIPS_CPU_ASE_MCU) ? 1 : 0;
15344 if (mips_opts.ase_mcu && !ISA_SUPPORTS_MCU_ASE)
15345 as_warn (_("%s ISA does not support MCU ASE"),
15346 mips_cpu_info_from_isa (mips_opts.isa)->name);
15347
e9670677 15348 file_mips_isa = mips_opts.isa;
e9670677
MR
15349 file_ase_mips3d = mips_opts.ase_mips3d;
15350 file_ase_mdmx = mips_opts.ase_mdmx;
e16bfa71 15351 file_ase_smartmips = mips_opts.ase_smartmips;
74cd071d 15352 file_ase_dsp = mips_opts.ase_dsp;
8b082fb1 15353 file_ase_dspr2 = mips_opts.ase_dspr2;
ef2e4d86 15354 file_ase_mt = mips_opts.ase_mt;
e9670677
MR
15355 mips_opts.gp32 = file_mips_gp32;
15356 mips_opts.fp32 = file_mips_fp32;
037b32b9
AN
15357 mips_opts.soft_float = file_mips_soft_float;
15358 mips_opts.single_float = file_mips_single_float;
e9670677 15359
ecb4347a
DJ
15360 if (mips_flag_mdebug < 0)
15361 {
15362#ifdef OBJ_MAYBE_ECOFF
15363 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
15364 mips_flag_mdebug = 1;
15365 else
15366#endif /* OBJ_MAYBE_ECOFF */
15367 mips_flag_mdebug = 0;
15368 }
e9670677
MR
15369}
15370\f
15371void
17a2f251 15372mips_init_after_args (void)
252b5132
RH
15373{
15374 /* initialize opcodes */
15375 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15376 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15377}
15378
15379long
17a2f251 15380md_pcrel_from (fixS *fixP)
252b5132 15381{
a7ebbfdf
TS
15382 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15383 switch (fixP->fx_r_type)
15384 {
df58fc94
RS
15385 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15386 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15387 /* Return the address of the delay slot. */
15388 return addr + 2;
15389
15390 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15391 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf
TS
15392 case BFD_RELOC_16_PCREL_S2:
15393 case BFD_RELOC_MIPS_JMP:
15394 /* Return the address of the delay slot. */
15395 return addr + 4;
df58fc94 15396
a7ebbfdf 15397 default:
58ea3d6a 15398 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
15399 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15400 as_bad_where (fixP->fx_file, fixP->fx_line,
15401 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
15402 return addr;
15403 }
252b5132
RH
15404}
15405
252b5132
RH
15406/* This is called before the symbol table is processed. In order to
15407 work with gcc when using mips-tfile, we must keep all local labels.
15408 However, in other cases, we want to discard them. If we were
15409 called with -g, but we didn't see any debugging information, it may
15410 mean that gcc is smuggling debugging information through to
15411 mips-tfile, in which case we must generate all local labels. */
15412
15413void
17a2f251 15414mips_frob_file_before_adjust (void)
252b5132
RH
15415{
15416#ifndef NO_ECOFF_DEBUGGING
15417 if (ECOFF_DEBUGGING
15418 && mips_debug != 0
15419 && ! ecoff_debugging_seen)
15420 flag_keep_locals = 1;
15421#endif
15422}
15423
3b91255e 15424/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15425 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15426 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15427 relocation operators.
15428
15429 For our purposes, a %lo() expression matches a %got() or %hi()
15430 expression if:
15431
15432 (a) it refers to the same symbol; and
15433 (b) the offset applied in the %lo() expression is no lower than
15434 the offset applied in the %got() or %hi().
15435
15436 (b) allows us to cope with code like:
15437
15438 lui $4,%hi(foo)
15439 lh $4,%lo(foo+2)($4)
15440
15441 ...which is legal on RELA targets, and has a well-defined behaviour
15442 if the user knows that adding 2 to "foo" will not induce a carry to
15443 the high 16 bits.
15444
15445 When several %lo()s match a particular %got() or %hi(), we use the
15446 following rules to distinguish them:
15447
15448 (1) %lo()s with smaller offsets are a better match than %lo()s with
15449 higher offsets.
15450
15451 (2) %lo()s with no matching %got() or %hi() are better than those
15452 that already have a matching %got() or %hi().
15453
15454 (3) later %lo()s are better than earlier %lo()s.
15455
15456 These rules are applied in order.
15457
15458 (1) means, among other things, that %lo()s with identical offsets are
15459 chosen if they exist.
15460
15461 (2) means that we won't associate several high-part relocations with
15462 the same low-part relocation unless there's no alternative. Having
15463 several high parts for the same low part is a GNU extension; this rule
15464 allows careful users to avoid it.
15465
15466 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15467 with the last high-part relocation being at the front of the list.
15468 It therefore makes sense to choose the last matching low-part
15469 relocation, all other things being equal. It's also easier
15470 to code that way. */
252b5132
RH
15471
15472void
17a2f251 15473mips_frob_file (void)
252b5132
RH
15474{
15475 struct mips_hi_fixup *l;
35903be0 15476 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15477
15478 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15479 {
15480 segment_info_type *seginfo;
3b91255e
RS
15481 bfd_boolean matched_lo_p;
15482 fixS **hi_pos, **lo_pos, **pos;
252b5132 15483
9c2799c2 15484 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15485
5919d012 15486 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15487 there isn't supposed to be a matching LO. Ignore %gots against
15488 constants; we'll report an error for those later. */
738e5348 15489 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab
RS
15490 && !(l->fixp->fx_addsy
15491 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
5919d012
RS
15492 continue;
15493
15494 /* Check quickly whether the next fixup happens to be a matching %lo. */
15495 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15496 continue;
15497
252b5132 15498 seginfo = seg_info (l->seg);
252b5132 15499
3b91255e
RS
15500 /* Set HI_POS to the position of this relocation in the chain.
15501 Set LO_POS to the position of the chosen low-part relocation.
15502 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15503 relocation that matches an immediately-preceding high-part
15504 relocation. */
15505 hi_pos = NULL;
15506 lo_pos = NULL;
15507 matched_lo_p = FALSE;
738e5348 15508 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15509
3b91255e
RS
15510 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15511 {
15512 if (*pos == l->fixp)
15513 hi_pos = pos;
15514
35903be0 15515 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15516 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15517 && (*pos)->fx_offset >= l->fixp->fx_offset
15518 && (lo_pos == NULL
15519 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15520 || (!matched_lo_p
15521 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15522 lo_pos = pos;
15523
15524 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15525 && fixup_has_matching_lo_p (*pos));
15526 }
15527
15528 /* If we found a match, remove the high-part relocation from its
15529 current position and insert it before the low-part relocation.
15530 Make the offsets match so that fixup_has_matching_lo_p()
15531 will return true.
15532
15533 We don't warn about unmatched high-part relocations since some
15534 versions of gcc have been known to emit dead "lui ...%hi(...)"
15535 instructions. */
15536 if (lo_pos != NULL)
15537 {
15538 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15539 if (l->fixp->fx_next != *lo_pos)
252b5132 15540 {
3b91255e
RS
15541 *hi_pos = l->fixp->fx_next;
15542 l->fixp->fx_next = *lo_pos;
15543 *lo_pos = l->fixp;
252b5132 15544 }
252b5132
RH
15545 }
15546 }
15547}
15548
252b5132 15549int
17a2f251 15550mips_force_relocation (fixS *fixp)
252b5132 15551{
ae6063d4 15552 if (generic_force_reloc (fixp))
252b5132
RH
15553 return 1;
15554
df58fc94
RS
15555 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15556 so that the linker relaxation can update targets. */
15557 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15558 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15559 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15560 return 1;
15561
3e722fb5 15562 return 0;
252b5132
RH
15563}
15564
b886a2ab
RS
15565/* Read the instruction associated with RELOC from BUF. */
15566
15567static unsigned int
15568read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15569{
15570 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15571 return read_compressed_insn (buf, 4);
15572 else
15573 return read_insn (buf);
15574}
15575
15576/* Write instruction INSN to BUF, given that it has been relocated
15577 by RELOC. */
15578
15579static void
15580write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15581 unsigned long insn)
15582{
15583 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15584 write_compressed_insn (buf, insn, 4);
15585 else
15586 write_insn (buf, insn);
15587}
15588
252b5132
RH
15589/* Apply a fixup to the object file. */
15590
94f592af 15591void
55cf6793 15592md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15593{
4d68580a 15594 char *buf;
b886a2ab 15595 unsigned long insn;
a7ebbfdf 15596 reloc_howto_type *howto;
252b5132 15597
a7ebbfdf
TS
15598 /* We ignore generic BFD relocations we don't know about. */
15599 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15600 if (! howto)
15601 return;
65551fa4 15602
df58fc94
RS
15603 gas_assert (fixP->fx_size == 2
15604 || fixP->fx_size == 4
90ecf173
MR
15605 || fixP->fx_r_type == BFD_RELOC_16
15606 || fixP->fx_r_type == BFD_RELOC_64
15607 || fixP->fx_r_type == BFD_RELOC_CTOR
15608 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15609 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15610 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15611 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15612 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 15613
4d68580a 15614 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15615
df58fc94
RS
15616 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15617 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15618 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15619 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
b1dca8ee
RS
15620
15621 /* Don't treat parts of a composite relocation as done. There are two
15622 reasons for this:
15623
15624 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15625 should nevertheless be emitted if the first part is.
15626
15627 (2) In normal usage, composite relocations are never assembly-time
15628 constants. The easiest way of dealing with the pathological
15629 exceptions is to generate a relocation against STN_UNDEF and
15630 leave everything up to the linker. */
3994f87e 15631 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15632 fixP->fx_done = 1;
15633
15634 switch (fixP->fx_r_type)
15635 {
3f98094e
DJ
15636 case BFD_RELOC_MIPS_TLS_GD:
15637 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15638 case BFD_RELOC_MIPS_TLS_DTPREL32:
15639 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15640 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15641 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15642 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15643 case BFD_RELOC_MIPS_TLS_TPREL32:
15644 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15645 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15646 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15647 case BFD_RELOC_MICROMIPS_TLS_GD:
15648 case BFD_RELOC_MICROMIPS_TLS_LDM:
15649 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15650 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15651 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15652 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15653 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15654 case BFD_RELOC_MIPS16_TLS_GD:
15655 case BFD_RELOC_MIPS16_TLS_LDM:
15656 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15657 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15658 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15659 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15660 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
b886a2ab
RS
15661 if (!fixP->fx_addsy)
15662 {
15663 as_bad_where (fixP->fx_file, fixP->fx_line,
15664 _("TLS relocation against a constant"));
15665 break;
15666 }
3f98094e
DJ
15667 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15668 /* fall through */
15669
252b5132 15670 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
15671 case BFD_RELOC_MIPS_SHIFT5:
15672 case BFD_RELOC_MIPS_SHIFT6:
15673 case BFD_RELOC_MIPS_GOT_DISP:
15674 case BFD_RELOC_MIPS_GOT_PAGE:
15675 case BFD_RELOC_MIPS_GOT_OFST:
15676 case BFD_RELOC_MIPS_SUB:
15677 case BFD_RELOC_MIPS_INSERT_A:
15678 case BFD_RELOC_MIPS_INSERT_B:
15679 case BFD_RELOC_MIPS_DELETE:
15680 case BFD_RELOC_MIPS_HIGHEST:
15681 case BFD_RELOC_MIPS_HIGHER:
15682 case BFD_RELOC_MIPS_SCN_DISP:
15683 case BFD_RELOC_MIPS_REL16:
15684 case BFD_RELOC_MIPS_RELGOT:
15685 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15686 case BFD_RELOC_HI16:
15687 case BFD_RELOC_HI16_S:
b886a2ab 15688 case BFD_RELOC_LO16:
cdf6fd85 15689 case BFD_RELOC_GPREL16:
252b5132
RH
15690 case BFD_RELOC_MIPS_LITERAL:
15691 case BFD_RELOC_MIPS_CALL16:
15692 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15693 case BFD_RELOC_GPREL32:
252b5132
RH
15694 case BFD_RELOC_MIPS_GOT_HI16:
15695 case BFD_RELOC_MIPS_GOT_LO16:
15696 case BFD_RELOC_MIPS_CALL_HI16:
15697 case BFD_RELOC_MIPS_CALL_LO16:
15698 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15699 case BFD_RELOC_MIPS16_GOT16:
15700 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15701 case BFD_RELOC_MIPS16_HI16:
15702 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15703 case BFD_RELOC_MIPS16_LO16:
252b5132 15704 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
15705 case BFD_RELOC_MICROMIPS_JMP:
15706 case BFD_RELOC_MICROMIPS_GOT_DISP:
15707 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15708 case BFD_RELOC_MICROMIPS_GOT_OFST:
15709 case BFD_RELOC_MICROMIPS_SUB:
15710 case BFD_RELOC_MICROMIPS_HIGHEST:
15711 case BFD_RELOC_MICROMIPS_HIGHER:
15712 case BFD_RELOC_MICROMIPS_SCN_DISP:
15713 case BFD_RELOC_MICROMIPS_JALR:
15714 case BFD_RELOC_MICROMIPS_HI16:
15715 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15716 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15717 case BFD_RELOC_MICROMIPS_GPREL16:
15718 case BFD_RELOC_MICROMIPS_LITERAL:
15719 case BFD_RELOC_MICROMIPS_CALL16:
15720 case BFD_RELOC_MICROMIPS_GOT16:
15721 case BFD_RELOC_MICROMIPS_GOT_HI16:
15722 case BFD_RELOC_MICROMIPS_GOT_LO16:
15723 case BFD_RELOC_MICROMIPS_CALL_HI16:
15724 case BFD_RELOC_MICROMIPS_CALL_LO16:
b886a2ab
RS
15725 if (fixP->fx_done)
15726 {
15727 offsetT value;
15728
15729 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15730 {
15731 insn = read_reloc_insn (buf, fixP->fx_r_type);
15732 if (mips16_reloc_p (fixP->fx_r_type))
15733 insn |= mips16_immed_extend (value, 16);
15734 else
15735 insn |= (value & 0xffff);
15736 write_reloc_insn (buf, fixP->fx_r_type, insn);
15737 }
15738 else
15739 as_bad_where (fixP->fx_file, fixP->fx_line,
15740 _("Unsupported constant in relocation"));
15741 }
252b5132
RH
15742 break;
15743
252b5132
RH
15744 case BFD_RELOC_64:
15745 /* This is handled like BFD_RELOC_32, but we output a sign
15746 extended value if we are only 32 bits. */
3e722fb5 15747 if (fixP->fx_done)
252b5132
RH
15748 {
15749 if (8 <= sizeof (valueT))
4d68580a 15750 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15751 else
15752 {
a7ebbfdf 15753 valueT hiv;
252b5132 15754
a7ebbfdf 15755 if ((*valP & 0x80000000) != 0)
252b5132
RH
15756 hiv = 0xffffffff;
15757 else
15758 hiv = 0;
4d68580a
RS
15759 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15760 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15761 }
15762 }
15763 break;
15764
056350c6 15765 case BFD_RELOC_RVA:
252b5132 15766 case BFD_RELOC_32:
252b5132
RH
15767 case BFD_RELOC_16:
15768 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15769 value now. This can happen if we have a .word which is not
15770 resolved when it appears but is later defined. */
252b5132 15771 if (fixP->fx_done)
4d68580a 15772 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15773 break;
15774
252b5132 15775 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 15776 if ((*valP & 0x3) != 0)
cb56d3d3 15777 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 15778 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 15779
54f4ddb3
TS
15780 /* We need to save the bits in the instruction since fixup_segment()
15781 might be deleting the relocation entry (i.e., a branch within
15782 the current segment). */
a7ebbfdf 15783 if (! fixP->fx_done)
bb2d6cd7 15784 break;
252b5132 15785
54f4ddb3 15786 /* Update old instruction data. */
4d68580a 15787 insn = read_insn (buf);
252b5132 15788
a7ebbfdf
TS
15789 if (*valP + 0x20000 <= 0x3ffff)
15790 {
15791 insn |= (*valP >> 2) & 0xffff;
4d68580a 15792 write_insn (buf, insn);
a7ebbfdf
TS
15793 }
15794 else if (mips_pic == NO_PIC
15795 && fixP->fx_done
15796 && fixP->fx_frag->fr_address >= text_section->vma
15797 && (fixP->fx_frag->fr_address
587aac4e 15798 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15799 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15800 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15801 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15802 {
15803 /* The branch offset is too large. If this is an
15804 unconditional branch, and we are not generating PIC code,
15805 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15806 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15807 insn = 0x0c000000; /* jal */
252b5132 15808 else
a7ebbfdf
TS
15809 insn = 0x08000000; /* j */
15810 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15811 fixP->fx_done = 0;
15812 fixP->fx_addsy = section_symbol (text_section);
15813 *valP += md_pcrel_from (fixP);
4d68580a 15814 write_insn (buf, insn);
a7ebbfdf
TS
15815 }
15816 else
15817 {
15818 /* If we got here, we have branch-relaxation disabled,
15819 and there's nothing we can do to fix this instruction
15820 without turning it into a longer sequence. */
15821 as_bad_where (fixP->fx_file, fixP->fx_line,
15822 _("Branch out of range"));
252b5132 15823 }
252b5132
RH
15824 break;
15825
df58fc94
RS
15826 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15827 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15828 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15829 /* We adjust the offset back to even. */
15830 if ((*valP & 0x1) != 0)
15831 --(*valP);
15832
15833 if (! fixP->fx_done)
15834 break;
15835
15836 /* Should never visit here, because we keep the relocation. */
15837 abort ();
15838 break;
15839
252b5132
RH
15840 case BFD_RELOC_VTABLE_INHERIT:
15841 fixP->fx_done = 0;
15842 if (fixP->fx_addsy
15843 && !S_IS_DEFINED (fixP->fx_addsy)
15844 && !S_IS_WEAK (fixP->fx_addsy))
15845 S_SET_WEAK (fixP->fx_addsy);
15846 break;
15847
15848 case BFD_RELOC_VTABLE_ENTRY:
15849 fixP->fx_done = 0;
15850 break;
15851
15852 default:
b37df7c4 15853 abort ();
252b5132 15854 }
a7ebbfdf
TS
15855
15856 /* Remember value for tc_gen_reloc. */
15857 fixP->fx_addnumber = *valP;
252b5132
RH
15858}
15859
252b5132 15860static symbolS *
17a2f251 15861get_symbol (void)
252b5132
RH
15862{
15863 int c;
15864 char *name;
15865 symbolS *p;
15866
15867 name = input_line_pointer;
15868 c = get_symbol_end ();
15869 p = (symbolS *) symbol_find_or_make (name);
15870 *input_line_pointer = c;
15871 return p;
15872}
15873
742a56fe
RS
15874/* Align the current frag to a given power of two. If a particular
15875 fill byte should be used, FILL points to an integer that contains
15876 that byte, otherwise FILL is null.
15877
462427c4
RS
15878 This function used to have the comment:
15879
15880 The MIPS assembler also automatically adjusts any preceding label.
15881
15882 The implementation therefore applied the adjustment to a maximum of
15883 one label. However, other label adjustments are applied to batches
15884 of labels, and adjusting just one caused problems when new labels
15885 were added for the sake of debugging or unwind information.
15886 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
15887
15888static void
462427c4 15889mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 15890{
7d10b47d 15891 mips_emit_delays ();
df58fc94 15892 mips_record_compressed_mode ();
742a56fe
RS
15893 if (fill == NULL && subseg_text_p (now_seg))
15894 frag_align_code (to, 0);
15895 else
15896 frag_align (to, fill ? *fill : 0, 0);
252b5132 15897 record_alignment (now_seg, to);
462427c4 15898 mips_move_labels (labels, FALSE);
252b5132
RH
15899}
15900
15901/* Align to a given power of two. .align 0 turns off the automatic
15902 alignment used by the data creating pseudo-ops. */
15903
15904static void
17a2f251 15905s_align (int x ATTRIBUTE_UNUSED)
252b5132 15906{
742a56fe 15907 int temp, fill_value, *fill_ptr;
49954fb4 15908 long max_alignment = 28;
252b5132 15909
54f4ddb3 15910 /* o Note that the assembler pulls down any immediately preceding label
252b5132 15911 to the aligned address.
54f4ddb3 15912 o It's not documented but auto alignment is reinstated by
252b5132 15913 a .align pseudo instruction.
54f4ddb3 15914 o Note also that after auto alignment is turned off the mips assembler
252b5132 15915 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 15916 We don't. */
252b5132
RH
15917
15918 temp = get_absolute_expression ();
15919 if (temp > max_alignment)
15920 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
15921 else if (temp < 0)
15922 {
15923 as_warn (_("Alignment negative: 0 assumed."));
15924 temp = 0;
15925 }
15926 if (*input_line_pointer == ',')
15927 {
f9419b05 15928 ++input_line_pointer;
742a56fe
RS
15929 fill_value = get_absolute_expression ();
15930 fill_ptr = &fill_value;
252b5132
RH
15931 }
15932 else
742a56fe 15933 fill_ptr = 0;
252b5132
RH
15934 if (temp)
15935 {
a8dbcb85
TS
15936 segment_info_type *si = seg_info (now_seg);
15937 struct insn_label_list *l = si->label_list;
54f4ddb3 15938 /* Auto alignment should be switched on by next section change. */
252b5132 15939 auto_align = 1;
462427c4 15940 mips_align (temp, fill_ptr, l);
252b5132
RH
15941 }
15942 else
15943 {
15944 auto_align = 0;
15945 }
15946
15947 demand_empty_rest_of_line ();
15948}
15949
252b5132 15950static void
17a2f251 15951s_change_sec (int sec)
252b5132
RH
15952{
15953 segT seg;
15954
252b5132
RH
15955#ifdef OBJ_ELF
15956 /* The ELF backend needs to know that we are changing sections, so
15957 that .previous works correctly. We could do something like check
b6ff326e 15958 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
15959 as it would not be appropriate to use it in the section changing
15960 functions in read.c, since obj-elf.c intercepts those. FIXME:
15961 This should be cleaner, somehow. */
f43abd2b
TS
15962 if (IS_ELF)
15963 obj_elf_section_change_hook ();
252b5132
RH
15964#endif
15965
7d10b47d 15966 mips_emit_delays ();
6a32d874 15967
252b5132
RH
15968 switch (sec)
15969 {
15970 case 't':
15971 s_text (0);
15972 break;
15973 case 'd':
15974 s_data (0);
15975 break;
15976 case 'b':
15977 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15978 demand_empty_rest_of_line ();
15979 break;
15980
15981 case 'r':
4d0d148d
TS
15982 seg = subseg_new (RDATA_SECTION_NAME,
15983 (subsegT) get_absolute_expression ());
f43abd2b 15984 if (IS_ELF)
252b5132 15985 {
4d0d148d
TS
15986 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15987 | SEC_READONLY | SEC_RELOC
15988 | SEC_DATA));
c41e87e3 15989 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 15990 record_alignment (seg, 4);
252b5132 15991 }
4d0d148d 15992 demand_empty_rest_of_line ();
252b5132
RH
15993 break;
15994
15995 case 's':
4d0d148d 15996 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f43abd2b 15997 if (IS_ELF)
252b5132 15998 {
4d0d148d
TS
15999 bfd_set_section_flags (stdoutput, seg,
16000 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
c41e87e3 16001 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 16002 record_alignment (seg, 4);
252b5132 16003 }
4d0d148d
TS
16004 demand_empty_rest_of_line ();
16005 break;
998b3c36
MR
16006
16007 case 'B':
16008 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16009 if (IS_ELF)
16010 {
16011 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16012 if (strncmp (TARGET_OS, "elf", 3) != 0)
16013 record_alignment (seg, 4);
16014 }
16015 demand_empty_rest_of_line ();
16016 break;
252b5132
RH
16017 }
16018
16019 auto_align = 1;
16020}
b34976b6 16021
cca86cc8 16022void
17a2f251 16023s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16024{
7ed4a06a 16025#ifdef OBJ_ELF
cca86cc8
SC
16026 char *section_name;
16027 char c;
684022ea 16028 char next_c = 0;
cca86cc8
SC
16029 int section_type;
16030 int section_flag;
16031 int section_entry_size;
16032 int section_alignment;
b34976b6 16033
f43abd2b 16034 if (!IS_ELF)
7ed4a06a
TS
16035 return;
16036
cca86cc8
SC
16037 section_name = input_line_pointer;
16038 c = get_symbol_end ();
a816d1ed
AO
16039 if (c)
16040 next_c = *(input_line_pointer + 1);
cca86cc8 16041
4cf0dd0d
TS
16042 /* Do we have .section Name<,"flags">? */
16043 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16044 {
4cf0dd0d
TS
16045 /* just after name is now '\0'. */
16046 *input_line_pointer = c;
cca86cc8
SC
16047 input_line_pointer = section_name;
16048 obj_elf_section (ignore);
16049 return;
16050 }
16051 input_line_pointer++;
16052
16053 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16054 if (c == ',')
16055 section_type = get_absolute_expression ();
16056 else
16057 section_type = 0;
16058 if (*input_line_pointer++ == ',')
16059 section_flag = get_absolute_expression ();
16060 else
16061 section_flag = 0;
16062 if (*input_line_pointer++ == ',')
16063 section_entry_size = get_absolute_expression ();
16064 else
16065 section_entry_size = 0;
16066 if (*input_line_pointer++ == ',')
16067 section_alignment = get_absolute_expression ();
16068 else
16069 section_alignment = 0;
87975d2a
AM
16070 /* FIXME: really ignore? */
16071 (void) section_alignment;
cca86cc8 16072
a816d1ed
AO
16073 section_name = xstrdup (section_name);
16074
8ab8a5c8
RS
16075 /* When using the generic form of .section (as implemented by obj-elf.c),
16076 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16077 traditionally had to fall back on the more common @progbits instead.
16078
16079 There's nothing really harmful in this, since bfd will correct
16080 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16081 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16082 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16083
16084 Even so, we shouldn't force users of the MIPS .section syntax to
16085 incorrectly label the sections as SHT_PROGBITS. The best compromise
16086 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16087 generic type-checking code. */
16088 if (section_type == SHT_MIPS_DWARF)
16089 section_type = SHT_PROGBITS;
16090
cca86cc8
SC
16091 obj_elf_change_section (section_name, section_type, section_flag,
16092 section_entry_size, 0, 0, 0);
a816d1ed
AO
16093
16094 if (now_seg->name != section_name)
16095 free (section_name);
7ed4a06a 16096#endif /* OBJ_ELF */
cca86cc8 16097}
252b5132
RH
16098
16099void
17a2f251 16100mips_enable_auto_align (void)
252b5132
RH
16101{
16102 auto_align = 1;
16103}
16104
16105static void
17a2f251 16106s_cons (int log_size)
252b5132 16107{
a8dbcb85
TS
16108 segment_info_type *si = seg_info (now_seg);
16109 struct insn_label_list *l = si->label_list;
252b5132 16110
7d10b47d 16111 mips_emit_delays ();
252b5132 16112 if (log_size > 0 && auto_align)
462427c4 16113 mips_align (log_size, 0, l);
252b5132 16114 cons (1 << log_size);
a1facbec 16115 mips_clear_insn_labels ();
252b5132
RH
16116}
16117
16118static void
17a2f251 16119s_float_cons (int type)
252b5132 16120{
a8dbcb85
TS
16121 segment_info_type *si = seg_info (now_seg);
16122 struct insn_label_list *l = si->label_list;
252b5132 16123
7d10b47d 16124 mips_emit_delays ();
252b5132
RH
16125
16126 if (auto_align)
49309057
ILT
16127 {
16128 if (type == 'd')
462427c4 16129 mips_align (3, 0, l);
49309057 16130 else
462427c4 16131 mips_align (2, 0, l);
49309057 16132 }
252b5132 16133
252b5132 16134 float_cons (type);
a1facbec 16135 mips_clear_insn_labels ();
252b5132
RH
16136}
16137
16138/* Handle .globl. We need to override it because on Irix 5 you are
16139 permitted to say
16140 .globl foo .text
16141 where foo is an undefined symbol, to mean that foo should be
16142 considered to be the address of a function. */
16143
16144static void
17a2f251 16145s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16146{
16147 char *name;
16148 int c;
16149 symbolS *symbolP;
16150 flagword flag;
16151
8a06b769 16152 do
252b5132 16153 {
8a06b769 16154 name = input_line_pointer;
252b5132 16155 c = get_symbol_end ();
8a06b769
TS
16156 symbolP = symbol_find_or_make (name);
16157 S_SET_EXTERNAL (symbolP);
16158
252b5132 16159 *input_line_pointer = c;
8a06b769 16160 SKIP_WHITESPACE ();
252b5132 16161
8a06b769
TS
16162 /* On Irix 5, every global symbol that is not explicitly labelled as
16163 being a function is apparently labelled as being an object. */
16164 flag = BSF_OBJECT;
252b5132 16165
8a06b769
TS
16166 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16167 && (*input_line_pointer != ','))
16168 {
16169 char *secname;
16170 asection *sec;
16171
16172 secname = input_line_pointer;
16173 c = get_symbol_end ();
16174 sec = bfd_get_section_by_name (stdoutput, secname);
16175 if (sec == NULL)
16176 as_bad (_("%s: no such section"), secname);
16177 *input_line_pointer = c;
16178
16179 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16180 flag = BSF_FUNCTION;
16181 }
16182
16183 symbol_get_bfdsym (symbolP)->flags |= flag;
16184
16185 c = *input_line_pointer;
16186 if (c == ',')
16187 {
16188 input_line_pointer++;
16189 SKIP_WHITESPACE ();
16190 if (is_end_of_line[(unsigned char) *input_line_pointer])
16191 c = '\n';
16192 }
16193 }
16194 while (c == ',');
252b5132 16195
252b5132
RH
16196 demand_empty_rest_of_line ();
16197}
16198
16199static void
17a2f251 16200s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16201{
16202 char *opt;
16203 char c;
16204
16205 opt = input_line_pointer;
16206 c = get_symbol_end ();
16207
16208 if (*opt == 'O')
16209 {
16210 /* FIXME: What does this mean? */
16211 }
16212 else if (strncmp (opt, "pic", 3) == 0)
16213 {
16214 int i;
16215
16216 i = atoi (opt + 3);
16217 if (i == 0)
16218 mips_pic = NO_PIC;
16219 else if (i == 2)
143d77c5 16220 {
8b828383 16221 mips_pic = SVR4_PIC;
143d77c5
EC
16222 mips_abicalls = TRUE;
16223 }
252b5132
RH
16224 else
16225 as_bad (_(".option pic%d not supported"), i);
16226
4d0d148d 16227 if (mips_pic == SVR4_PIC)
252b5132
RH
16228 {
16229 if (g_switch_seen && g_switch_value != 0)
16230 as_warn (_("-G may not be used with SVR4 PIC code"));
16231 g_switch_value = 0;
16232 bfd_set_gp_size (stdoutput, 0);
16233 }
16234 }
16235 else
16236 as_warn (_("Unrecognized option \"%s\""), opt);
16237
16238 *input_line_pointer = c;
16239 demand_empty_rest_of_line ();
16240}
16241
16242/* This structure is used to hold a stack of .set values. */
16243
e972090a
NC
16244struct mips_option_stack
16245{
252b5132
RH
16246 struct mips_option_stack *next;
16247 struct mips_set_options options;
16248};
16249
16250static struct mips_option_stack *mips_opts_stack;
16251
16252/* Handle the .set pseudo-op. */
16253
16254static void
17a2f251 16255s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
16256{
16257 char *name = input_line_pointer, ch;
16258
16259 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 16260 ++input_line_pointer;
252b5132
RH
16261 ch = *input_line_pointer;
16262 *input_line_pointer = '\0';
16263
16264 if (strcmp (name, "reorder") == 0)
16265 {
7d10b47d
RS
16266 if (mips_opts.noreorder)
16267 end_noreorder ();
252b5132
RH
16268 }
16269 else if (strcmp (name, "noreorder") == 0)
16270 {
7d10b47d
RS
16271 if (!mips_opts.noreorder)
16272 start_noreorder ();
252b5132 16273 }
741fe287
MR
16274 else if (strncmp (name, "at=", 3) == 0)
16275 {
16276 char *s = name + 3;
16277
16278 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16279 as_bad (_("Unrecognized register name `%s'"), s);
16280 }
252b5132
RH
16281 else if (strcmp (name, "at") == 0)
16282 {
741fe287 16283 mips_opts.at = ATREG;
252b5132
RH
16284 }
16285 else if (strcmp (name, "noat") == 0)
16286 {
741fe287 16287 mips_opts.at = ZERO;
252b5132
RH
16288 }
16289 else if (strcmp (name, "macro") == 0)
16290 {
16291 mips_opts.warn_about_macros = 0;
16292 }
16293 else if (strcmp (name, "nomacro") == 0)
16294 {
16295 if (mips_opts.noreorder == 0)
16296 as_bad (_("`noreorder' must be set before `nomacro'"));
16297 mips_opts.warn_about_macros = 1;
16298 }
16299 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16300 {
16301 mips_opts.nomove = 0;
16302 }
16303 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16304 {
16305 mips_opts.nomove = 1;
16306 }
16307 else if (strcmp (name, "bopt") == 0)
16308 {
16309 mips_opts.nobopt = 0;
16310 }
16311 else if (strcmp (name, "nobopt") == 0)
16312 {
16313 mips_opts.nobopt = 1;
16314 }
ad3fea08
TS
16315 else if (strcmp (name, "gp=default") == 0)
16316 mips_opts.gp32 = file_mips_gp32;
16317 else if (strcmp (name, "gp=32") == 0)
16318 mips_opts.gp32 = 1;
16319 else if (strcmp (name, "gp=64") == 0)
16320 {
16321 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 16322 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08
TS
16323 mips_cpu_info_from_isa (mips_opts.isa)->name);
16324 mips_opts.gp32 = 0;
16325 }
16326 else if (strcmp (name, "fp=default") == 0)
16327 mips_opts.fp32 = file_mips_fp32;
16328 else if (strcmp (name, "fp=32") == 0)
16329 mips_opts.fp32 = 1;
16330 else if (strcmp (name, "fp=64") == 0)
16331 {
16332 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 16333 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08
TS
16334 mips_cpu_info_from_isa (mips_opts.isa)->name);
16335 mips_opts.fp32 = 0;
16336 }
037b32b9
AN
16337 else if (strcmp (name, "softfloat") == 0)
16338 mips_opts.soft_float = 1;
16339 else if (strcmp (name, "hardfloat") == 0)
16340 mips_opts.soft_float = 0;
16341 else if (strcmp (name, "singlefloat") == 0)
16342 mips_opts.single_float = 1;
16343 else if (strcmp (name, "doublefloat") == 0)
16344 mips_opts.single_float = 0;
252b5132
RH
16345 else if (strcmp (name, "mips16") == 0
16346 || strcmp (name, "MIPS-16") == 0)
df58fc94
RS
16347 {
16348 if (mips_opts.micromips == 1)
16349 as_fatal (_("`mips16' cannot be used with `micromips'"));
16350 mips_opts.mips16 = 1;
16351 }
252b5132
RH
16352 else if (strcmp (name, "nomips16") == 0
16353 || strcmp (name, "noMIPS-16") == 0)
16354 mips_opts.mips16 = 0;
df58fc94
RS
16355 else if (strcmp (name, "micromips") == 0)
16356 {
16357 if (mips_opts.mips16 == 1)
16358 as_fatal (_("`micromips' cannot be used with `mips16'"));
16359 mips_opts.micromips = 1;
16360 }
16361 else if (strcmp (name, "nomicromips") == 0)
16362 mips_opts.micromips = 0;
e16bfa71
TS
16363 else if (strcmp (name, "smartmips") == 0)
16364 {
ad3fea08 16365 if (!ISA_SUPPORTS_SMARTMIPS)
20203fb9 16366 as_warn (_("%s ISA does not support SmartMIPS ASE"),
e16bfa71
TS
16367 mips_cpu_info_from_isa (mips_opts.isa)->name);
16368 mips_opts.ase_smartmips = 1;
16369 }
16370 else if (strcmp (name, "nosmartmips") == 0)
16371 mips_opts.ase_smartmips = 0;
1f25f5d3
CD
16372 else if (strcmp (name, "mips3d") == 0)
16373 mips_opts.ase_mips3d = 1;
16374 else if (strcmp (name, "nomips3d") == 0)
16375 mips_opts.ase_mips3d = 0;
a4672219
TS
16376 else if (strcmp (name, "mdmx") == 0)
16377 mips_opts.ase_mdmx = 1;
16378 else if (strcmp (name, "nomdmx") == 0)
16379 mips_opts.ase_mdmx = 0;
74cd071d 16380 else if (strcmp (name, "dsp") == 0)
ad3fea08
TS
16381 {
16382 if (!ISA_SUPPORTS_DSP_ASE)
20203fb9 16383 as_warn (_("%s ISA does not support DSP ASE"),
ad3fea08
TS
16384 mips_cpu_info_from_isa (mips_opts.isa)->name);
16385 mips_opts.ase_dsp = 1;
8b082fb1 16386 mips_opts.ase_dspr2 = 0;
ad3fea08 16387 }
74cd071d 16388 else if (strcmp (name, "nodsp") == 0)
8b082fb1
TS
16389 {
16390 mips_opts.ase_dsp = 0;
16391 mips_opts.ase_dspr2 = 0;
16392 }
16393 else if (strcmp (name, "dspr2") == 0)
16394 {
16395 if (!ISA_SUPPORTS_DSPR2_ASE)
20203fb9 16396 as_warn (_("%s ISA does not support DSP R2 ASE"),
8b082fb1
TS
16397 mips_cpu_info_from_isa (mips_opts.isa)->name);
16398 mips_opts.ase_dspr2 = 1;
16399 mips_opts.ase_dsp = 1;
16400 }
16401 else if (strcmp (name, "nodspr2") == 0)
16402 {
16403 mips_opts.ase_dspr2 = 0;
16404 mips_opts.ase_dsp = 0;
16405 }
ef2e4d86 16406 else if (strcmp (name, "mt") == 0)
ad3fea08
TS
16407 {
16408 if (!ISA_SUPPORTS_MT_ASE)
20203fb9 16409 as_warn (_("%s ISA does not support MT ASE"),
ad3fea08
TS
16410 mips_cpu_info_from_isa (mips_opts.isa)->name);
16411 mips_opts.ase_mt = 1;
16412 }
ef2e4d86
CF
16413 else if (strcmp (name, "nomt") == 0)
16414 mips_opts.ase_mt = 0;
dec0624d
MR
16415 else if (strcmp (name, "mcu") == 0)
16416 mips_opts.ase_mcu = 1;
16417 else if (strcmp (name, "nomcu") == 0)
16418 mips_opts.ase_mcu = 0;
1a2c1fad 16419 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16420 {
af7ee8bf 16421 int reset = 0;
252b5132 16422
1a2c1fad
CD
16423 /* Permit the user to change the ISA and architecture on the fly.
16424 Needless to say, misuse can cause serious problems. */
81a21e38 16425 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
16426 {
16427 reset = 1;
16428 mips_opts.isa = file_mips_isa;
1a2c1fad 16429 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
16430 }
16431 else if (strncmp (name, "arch=", 5) == 0)
16432 {
16433 const struct mips_cpu_info *p;
16434
16435 p = mips_parse_cpu("internal use", name + 5);
16436 if (!p)
16437 as_bad (_("unknown architecture %s"), name + 5);
16438 else
16439 {
16440 mips_opts.arch = p->cpu;
16441 mips_opts.isa = p->isa;
16442 }
16443 }
81a21e38
TS
16444 else if (strncmp (name, "mips", 4) == 0)
16445 {
16446 const struct mips_cpu_info *p;
16447
16448 p = mips_parse_cpu("internal use", name);
16449 if (!p)
16450 as_bad (_("unknown ISA level %s"), name + 4);
16451 else
16452 {
16453 mips_opts.arch = p->cpu;
16454 mips_opts.isa = p->isa;
16455 }
16456 }
af7ee8bf 16457 else
81a21e38 16458 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
16459
16460 switch (mips_opts.isa)
98d3f06f
KH
16461 {
16462 case 0:
98d3f06f 16463 break;
af7ee8bf
CD
16464 case ISA_MIPS1:
16465 case ISA_MIPS2:
16466 case ISA_MIPS32:
16467 case ISA_MIPS32R2:
98d3f06f
KH
16468 mips_opts.gp32 = 1;
16469 mips_opts.fp32 = 1;
16470 break;
af7ee8bf
CD
16471 case ISA_MIPS3:
16472 case ISA_MIPS4:
16473 case ISA_MIPS5:
16474 case ISA_MIPS64:
5f74bc13 16475 case ISA_MIPS64R2:
98d3f06f 16476 mips_opts.gp32 = 0;
e407c74b
NC
16477 if (mips_opts.arch == CPU_R5900)
16478 {
16479 mips_opts.fp32 = 1;
16480 }
16481 else
16482 {
98d3f06f 16483 mips_opts.fp32 = 0;
e407c74b 16484 }
98d3f06f
KH
16485 break;
16486 default:
16487 as_bad (_("unknown ISA level %s"), name + 4);
16488 break;
16489 }
af7ee8bf 16490 if (reset)
98d3f06f 16491 {
af7ee8bf
CD
16492 mips_opts.gp32 = file_mips_gp32;
16493 mips_opts.fp32 = file_mips_fp32;
98d3f06f 16494 }
252b5132
RH
16495 }
16496 else if (strcmp (name, "autoextend") == 0)
16497 mips_opts.noautoextend = 0;
16498 else if (strcmp (name, "noautoextend") == 0)
16499 mips_opts.noautoextend = 1;
16500 else if (strcmp (name, "push") == 0)
16501 {
16502 struct mips_option_stack *s;
16503
16504 s = (struct mips_option_stack *) xmalloc (sizeof *s);
16505 s->next = mips_opts_stack;
16506 s->options = mips_opts;
16507 mips_opts_stack = s;
16508 }
16509 else if (strcmp (name, "pop") == 0)
16510 {
16511 struct mips_option_stack *s;
16512
16513 s = mips_opts_stack;
16514 if (s == NULL)
16515 as_bad (_(".set pop with no .set push"));
16516 else
16517 {
16518 /* If we're changing the reorder mode we need to handle
16519 delay slots correctly. */
16520 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16521 start_noreorder ();
252b5132 16522 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16523 end_noreorder ();
252b5132
RH
16524
16525 mips_opts = s->options;
16526 mips_opts_stack = s->next;
16527 free (s);
16528 }
16529 }
aed1a261
RS
16530 else if (strcmp (name, "sym32") == 0)
16531 mips_opts.sym32 = TRUE;
16532 else if (strcmp (name, "nosym32") == 0)
16533 mips_opts.sym32 = FALSE;
e6559e01
JM
16534 else if (strchr (name, ','))
16535 {
16536 /* Generic ".set" directive; use the generic handler. */
16537 *input_line_pointer = ch;
16538 input_line_pointer = name;
16539 s_set (0);
16540 return;
16541 }
252b5132
RH
16542 else
16543 {
16544 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16545 }
16546 *input_line_pointer = ch;
16547 demand_empty_rest_of_line ();
16548}
16549
16550/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16551 .option pic2. It means to generate SVR4 PIC calls. */
16552
16553static void
17a2f251 16554s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16555{
16556 mips_pic = SVR4_PIC;
143d77c5 16557 mips_abicalls = TRUE;
4d0d148d
TS
16558
16559 if (g_switch_seen && g_switch_value != 0)
16560 as_warn (_("-G may not be used with SVR4 PIC code"));
16561 g_switch_value = 0;
16562
252b5132
RH
16563 bfd_set_gp_size (stdoutput, 0);
16564 demand_empty_rest_of_line ();
16565}
16566
16567/* Handle the .cpload pseudo-op. This is used when generating SVR4
16568 PIC code. It sets the $gp register for the function based on the
16569 function address, which is in the register named in the argument.
16570 This uses a relocation against _gp_disp, which is handled specially
16571 by the linker. The result is:
16572 lui $gp,%hi(_gp_disp)
16573 addiu $gp,$gp,%lo(_gp_disp)
16574 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16575 The .cpload argument is normally $25 == $t9.
16576
16577 The -mno-shared option changes this to:
bbe506e8
TS
16578 lui $gp,%hi(__gnu_local_gp)
16579 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16580 and the argument is ignored. This saves an instruction, but the
16581 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16582 address for __gnu_local_gp. Thus code assembled with -mno-shared
16583 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16584
16585static void
17a2f251 16586s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16587{
16588 expressionS ex;
aa6975fb
ILT
16589 int reg;
16590 int in_shared;
252b5132 16591
6478892d
TS
16592 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16593 .cpload is ignored. */
16594 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16595 {
16596 s_ignore (0);
16597 return;
16598 }
16599
a276b80c
MR
16600 if (mips_opts.mips16)
16601 {
16602 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16603 ignore_rest_of_line ();
16604 return;
16605 }
16606
d3ecfc59 16607 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16608 if (mips_opts.noreorder == 0)
16609 as_warn (_(".cpload not in noreorder section"));
16610
aa6975fb
ILT
16611 reg = tc_get_register (0);
16612
16613 /* If we need to produce a 64-bit address, we are better off using
16614 the default instruction sequence. */
aed1a261 16615 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16616
252b5132 16617 ex.X_op = O_symbol;
bbe506e8
TS
16618 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16619 "__gnu_local_gp");
252b5132
RH
16620 ex.X_op_symbol = NULL;
16621 ex.X_add_number = 0;
16622
16623 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16624 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16625
584892a6 16626 macro_start ();
67c0d1eb
RS
16627 macro_build_lui (&ex, mips_gp_register);
16628 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16629 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16630 if (in_shared)
16631 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16632 mips_gp_register, reg);
584892a6 16633 macro_end ();
252b5132
RH
16634
16635 demand_empty_rest_of_line ();
16636}
16637
6478892d
TS
16638/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16639 .cpsetup $reg1, offset|$reg2, label
16640
16641 If offset is given, this results in:
16642 sd $gp, offset($sp)
956cd1d6 16643 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16644 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16645 daddu $gp, $gp, $reg1
6478892d
TS
16646
16647 If $reg2 is given, this results in:
16648 daddu $reg2, $gp, $0
956cd1d6 16649 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16650 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16651 daddu $gp, $gp, $reg1
aa6975fb
ILT
16652 $reg1 is normally $25 == $t9.
16653
16654 The -mno-shared option replaces the last three instructions with
16655 lui $gp,%hi(_gp)
54f4ddb3 16656 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16657
6478892d 16658static void
17a2f251 16659s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16660{
16661 expressionS ex_off;
16662 expressionS ex_sym;
16663 int reg1;
6478892d 16664
8586fc66 16665 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16666 We also need NewABI support. */
16667 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16668 {
16669 s_ignore (0);
16670 return;
16671 }
16672
a276b80c
MR
16673 if (mips_opts.mips16)
16674 {
16675 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16676 ignore_rest_of_line ();
16677 return;
16678 }
16679
6478892d
TS
16680 reg1 = tc_get_register (0);
16681 SKIP_WHITESPACE ();
16682 if (*input_line_pointer != ',')
16683 {
16684 as_bad (_("missing argument separator ',' for .cpsetup"));
16685 return;
16686 }
16687 else
80245285 16688 ++input_line_pointer;
6478892d
TS
16689 SKIP_WHITESPACE ();
16690 if (*input_line_pointer == '$')
80245285
TS
16691 {
16692 mips_cpreturn_register = tc_get_register (0);
16693 mips_cpreturn_offset = -1;
16694 }
6478892d 16695 else
80245285
TS
16696 {
16697 mips_cpreturn_offset = get_absolute_expression ();
16698 mips_cpreturn_register = -1;
16699 }
6478892d
TS
16700 SKIP_WHITESPACE ();
16701 if (*input_line_pointer != ',')
16702 {
16703 as_bad (_("missing argument separator ',' for .cpsetup"));
16704 return;
16705 }
16706 else
f9419b05 16707 ++input_line_pointer;
6478892d 16708 SKIP_WHITESPACE ();
f21f8242 16709 expression (&ex_sym);
6478892d 16710
584892a6 16711 macro_start ();
6478892d
TS
16712 if (mips_cpreturn_register == -1)
16713 {
16714 ex_off.X_op = O_constant;
16715 ex_off.X_add_symbol = NULL;
16716 ex_off.X_op_symbol = NULL;
16717 ex_off.X_add_number = mips_cpreturn_offset;
16718
67c0d1eb 16719 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16720 BFD_RELOC_LO16, SP);
6478892d
TS
16721 }
16722 else
67c0d1eb 16723 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 16724 mips_gp_register, 0);
6478892d 16725
aed1a261 16726 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16727 {
df58fc94 16728 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16729 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16730 BFD_RELOC_HI16_S);
16731
16732 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16733 mips_gp_register, -1, BFD_RELOC_GPREL16,
16734 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16735
16736 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16737 mips_gp_register, reg1);
16738 }
16739 else
16740 {
16741 expressionS ex;
16742
16743 ex.X_op = O_symbol;
4184909a 16744 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16745 ex.X_op_symbol = NULL;
16746 ex.X_add_number = 0;
6e1304d8 16747
aa6975fb
ILT
16748 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16749 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16750
16751 macro_build_lui (&ex, mips_gp_register);
16752 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16753 mips_gp_register, BFD_RELOC_LO16);
16754 }
f21f8242 16755
584892a6 16756 macro_end ();
6478892d
TS
16757
16758 demand_empty_rest_of_line ();
16759}
16760
16761static void
17a2f251 16762s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16763{
16764 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16765 .cplocal is ignored. */
6478892d
TS
16766 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16767 {
16768 s_ignore (0);
16769 return;
16770 }
16771
a276b80c
MR
16772 if (mips_opts.mips16)
16773 {
16774 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16775 ignore_rest_of_line ();
16776 return;
16777 }
16778
6478892d 16779 mips_gp_register = tc_get_register (0);
85b51719 16780 demand_empty_rest_of_line ();
6478892d
TS
16781}
16782
252b5132
RH
16783/* Handle the .cprestore pseudo-op. This stores $gp into a given
16784 offset from $sp. The offset is remembered, and after making a PIC
16785 call $gp is restored from that location. */
16786
16787static void
17a2f251 16788s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16789{
16790 expressionS ex;
252b5132 16791
6478892d 16792 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16793 .cprestore is ignored. */
6478892d 16794 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16795 {
16796 s_ignore (0);
16797 return;
16798 }
16799
a276b80c
MR
16800 if (mips_opts.mips16)
16801 {
16802 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16803 ignore_rest_of_line ();
16804 return;
16805 }
16806
252b5132 16807 mips_cprestore_offset = get_absolute_expression ();
7a621144 16808 mips_cprestore_valid = 1;
252b5132
RH
16809
16810 ex.X_op = O_constant;
16811 ex.X_add_symbol = NULL;
16812 ex.X_op_symbol = NULL;
16813 ex.X_add_number = mips_cprestore_offset;
16814
584892a6 16815 macro_start ();
67c0d1eb
RS
16816 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16817 SP, HAVE_64BIT_ADDRESSES);
584892a6 16818 macro_end ();
252b5132
RH
16819
16820 demand_empty_rest_of_line ();
16821}
16822
6478892d 16823/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16824 was given in the preceding .cpsetup, it results in:
6478892d 16825 ld $gp, offset($sp)
76b3015f 16826
6478892d 16827 If a register $reg2 was given there, it results in:
54f4ddb3
TS
16828 daddu $gp, $reg2, $0 */
16829
6478892d 16830static void
17a2f251 16831s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16832{
16833 expressionS ex;
6478892d
TS
16834
16835 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16836 We also need NewABI support. */
16837 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16838 {
16839 s_ignore (0);
16840 return;
16841 }
16842
a276b80c
MR
16843 if (mips_opts.mips16)
16844 {
16845 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16846 ignore_rest_of_line ();
16847 return;
16848 }
16849
584892a6 16850 macro_start ();
6478892d
TS
16851 if (mips_cpreturn_register == -1)
16852 {
16853 ex.X_op = O_constant;
16854 ex.X_add_symbol = NULL;
16855 ex.X_op_symbol = NULL;
16856 ex.X_add_number = mips_cpreturn_offset;
16857
67c0d1eb 16858 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
16859 }
16860 else
67c0d1eb 16861 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 16862 mips_cpreturn_register, 0);
584892a6 16863 macro_end ();
6478892d
TS
16864
16865 demand_empty_rest_of_line ();
16866}
16867
d0f13682
CLT
16868/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16869 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16870 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16871 debug information or MIPS16 TLS. */
741d6ea8
JM
16872
16873static void
d0f13682
CLT
16874s_tls_rel_directive (const size_t bytes, const char *dirstr,
16875 bfd_reloc_code_real_type rtype)
741d6ea8
JM
16876{
16877 expressionS ex;
16878 char *p;
16879
16880 expression (&ex);
16881
16882 if (ex.X_op != O_symbol)
16883 {
d0f13682 16884 as_bad (_("Unsupported use of %s"), dirstr);
741d6ea8
JM
16885 ignore_rest_of_line ();
16886 }
16887
16888 p = frag_more (bytes);
16889 md_number_to_chars (p, 0, bytes);
d0f13682 16890 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 16891 demand_empty_rest_of_line ();
de64cffd 16892 mips_clear_insn_labels ();
741d6ea8
JM
16893}
16894
16895/* Handle .dtprelword. */
16896
16897static void
16898s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16899{
d0f13682 16900 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
16901}
16902
16903/* Handle .dtpreldword. */
16904
16905static void
16906s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16907{
d0f13682
CLT
16908 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16909}
16910
16911/* Handle .tprelword. */
16912
16913static void
16914s_tprelword (int ignore ATTRIBUTE_UNUSED)
16915{
16916 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16917}
16918
16919/* Handle .tpreldword. */
16920
16921static void
16922s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16923{
16924 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
16925}
16926
6478892d
TS
16927/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16928 code. It sets the offset to use in gp_rel relocations. */
16929
16930static void
17a2f251 16931s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16932{
16933 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16934 We also need NewABI support. */
16935 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16936 {
16937 s_ignore (0);
16938 return;
16939 }
16940
def2e0dd 16941 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
16942
16943 demand_empty_rest_of_line ();
16944}
16945
252b5132
RH
16946/* Handle the .gpword pseudo-op. This is used when generating PIC
16947 code. It generates a 32 bit GP relative reloc. */
16948
16949static void
17a2f251 16950s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 16951{
a8dbcb85
TS
16952 segment_info_type *si;
16953 struct insn_label_list *l;
252b5132
RH
16954 expressionS ex;
16955 char *p;
16956
16957 /* When not generating PIC code, this is treated as .word. */
16958 if (mips_pic != SVR4_PIC)
16959 {
16960 s_cons (2);
16961 return;
16962 }
16963
a8dbcb85
TS
16964 si = seg_info (now_seg);
16965 l = si->label_list;
7d10b47d 16966 mips_emit_delays ();
252b5132 16967 if (auto_align)
462427c4 16968 mips_align (2, 0, l);
252b5132
RH
16969
16970 expression (&ex);
a1facbec 16971 mips_clear_insn_labels ();
252b5132
RH
16972
16973 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16974 {
16975 as_bad (_("Unsupported use of .gpword"));
16976 ignore_rest_of_line ();
16977 }
16978
16979 p = frag_more (4);
17a2f251 16980 md_number_to_chars (p, 0, 4);
b34976b6 16981 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 16982 BFD_RELOC_GPREL32);
252b5132
RH
16983
16984 demand_empty_rest_of_line ();
16985}
16986
10181a0d 16987static void
17a2f251 16988s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 16989{
a8dbcb85
TS
16990 segment_info_type *si;
16991 struct insn_label_list *l;
10181a0d
AO
16992 expressionS ex;
16993 char *p;
16994
16995 /* When not generating PIC code, this is treated as .dword. */
16996 if (mips_pic != SVR4_PIC)
16997 {
16998 s_cons (3);
16999 return;
17000 }
17001
a8dbcb85
TS
17002 si = seg_info (now_seg);
17003 l = si->label_list;
7d10b47d 17004 mips_emit_delays ();
10181a0d 17005 if (auto_align)
462427c4 17006 mips_align (3, 0, l);
10181a0d
AO
17007
17008 expression (&ex);
a1facbec 17009 mips_clear_insn_labels ();
10181a0d
AO
17010
17011 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17012 {
17013 as_bad (_("Unsupported use of .gpdword"));
17014 ignore_rest_of_line ();
17015 }
17016
17017 p = frag_more (8);
17a2f251 17018 md_number_to_chars (p, 0, 8);
a105a300 17019 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17020 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17021
17022 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17023 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17024 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17025
17026 demand_empty_rest_of_line ();
17027}
17028
252b5132
RH
17029/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17030 tables in SVR4 PIC code. */
17031
17032static void
17a2f251 17033s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17034{
252b5132
RH
17035 int reg;
17036
10181a0d
AO
17037 /* This is ignored when not generating SVR4 PIC code. */
17038 if (mips_pic != SVR4_PIC)
252b5132
RH
17039 {
17040 s_ignore (0);
17041 return;
17042 }
17043
17044 /* Add $gp to the register named as an argument. */
584892a6 17045 macro_start ();
252b5132 17046 reg = tc_get_register (0);
67c0d1eb 17047 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17048 macro_end ();
252b5132 17049
bdaaa2e1 17050 demand_empty_rest_of_line ();
252b5132
RH
17051}
17052
17053/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17054 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17055 such as generating jalx instructions when needed. We also make
17056 them odd for the duration of the assembly, in order to generate the
17057 right sort of code. We will make them even in the adjust_symtab
17058 routine, while leaving them marked. This is convenient for the
17059 debugger and the disassembler. The linker knows to make them odd
17060 again. */
17061
17062static void
17a2f251 17063s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17064{
df58fc94 17065 mips_mark_labels ();
252b5132
RH
17066
17067 demand_empty_rest_of_line ();
17068}
17069
17070/* Handle a .stabn directive. We need these in order to mark a label
17071 as being a mips16 text label correctly. Sometimes the compiler
17072 will emit a label, followed by a .stabn, and then switch sections.
17073 If the label and .stabn are in mips16 mode, then the label is
17074 really a mips16 text label. */
17075
17076static void
17a2f251 17077s_mips_stab (int type)
252b5132 17078{
f9419b05 17079 if (type == 'n')
df58fc94 17080 mips_mark_labels ();
252b5132
RH
17081
17082 s_stab (type);
17083}
17084
54f4ddb3 17085/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17086
17087static void
17a2f251 17088s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17089{
17090 char *name;
17091 int c;
17092 symbolS *symbolP;
17093 expressionS exp;
17094
17095 name = input_line_pointer;
17096 c = get_symbol_end ();
17097 symbolP = symbol_find_or_make (name);
17098 S_SET_WEAK (symbolP);
17099 *input_line_pointer = c;
17100
17101 SKIP_WHITESPACE ();
17102
17103 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17104 {
17105 if (S_IS_DEFINED (symbolP))
17106 {
20203fb9 17107 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17108 S_GET_NAME (symbolP));
17109 ignore_rest_of_line ();
17110 return;
17111 }
bdaaa2e1 17112
252b5132
RH
17113 if (*input_line_pointer == ',')
17114 {
17115 ++input_line_pointer;
17116 SKIP_WHITESPACE ();
17117 }
bdaaa2e1 17118
252b5132
RH
17119 expression (&exp);
17120 if (exp.X_op != O_symbol)
17121 {
20203fb9 17122 as_bad (_("bad .weakext directive"));
98d3f06f 17123 ignore_rest_of_line ();
252b5132
RH
17124 return;
17125 }
49309057 17126 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17127 }
17128
17129 demand_empty_rest_of_line ();
17130}
17131
17132/* Parse a register string into a number. Called from the ECOFF code
17133 to parse .frame. The argument is non-zero if this is the frame
17134 register, so that we can record it in mips_frame_reg. */
17135
17136int
17a2f251 17137tc_get_register (int frame)
252b5132 17138{
707bfff6 17139 unsigned int reg;
252b5132
RH
17140
17141 SKIP_WHITESPACE ();
707bfff6
TS
17142 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17143 reg = 0;
252b5132 17144 if (frame)
7a621144
DJ
17145 {
17146 mips_frame_reg = reg != 0 ? reg : SP;
17147 mips_frame_reg_valid = 1;
17148 mips_cprestore_valid = 0;
17149 }
252b5132
RH
17150 return reg;
17151}
17152
17153valueT
17a2f251 17154md_section_align (asection *seg, valueT addr)
252b5132
RH
17155{
17156 int align = bfd_get_section_alignment (stdoutput, seg);
17157
b4c71f56
TS
17158 if (IS_ELF)
17159 {
17160 /* We don't need to align ELF sections to the full alignment.
17161 However, Irix 5 may prefer that we align them at least to a 16
17162 byte boundary. We don't bother to align the sections if we
17163 are targeted for an embedded system. */
c41e87e3 17164 if (strncmp (TARGET_OS, "elf", 3) == 0)
b4c71f56
TS
17165 return addr;
17166 if (align > 4)
17167 align = 4;
17168 }
252b5132
RH
17169
17170 return ((addr + (1 << align) - 1) & (-1 << align));
17171}
17172
17173/* Utility routine, called from above as well. If called while the
17174 input file is still being read, it's only an approximation. (For
17175 example, a symbol may later become defined which appeared to be
17176 undefined earlier.) */
17177
17178static int
17a2f251 17179nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17180{
17181 if (sym == 0)
17182 return 0;
17183
4d0d148d 17184 if (g_switch_value > 0)
252b5132
RH
17185 {
17186 const char *symname;
17187 int change;
17188
c9914766 17189 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17190 register. It can be if it is smaller than the -G size or if
17191 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17192 not be referenced off the $gp, although it appears as though
252b5132
RH
17193 they can. */
17194 symname = S_GET_NAME (sym);
17195 if (symname != (const char *) NULL
17196 && (strcmp (symname, "eprol") == 0
17197 || strcmp (symname, "etext") == 0
17198 || strcmp (symname, "_gp") == 0
17199 || strcmp (symname, "edata") == 0
17200 || strcmp (symname, "_fbss") == 0
17201 || strcmp (symname, "_fdata") == 0
17202 || strcmp (symname, "_ftext") == 0
17203 || strcmp (symname, "end") == 0
17204 || strcmp (symname, "_gp_disp") == 0))
17205 change = 1;
17206 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17207 && (0
17208#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17209 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17210 && (symbol_get_obj (sym)->ecoff_extern_size
17211 <= g_switch_value))
252b5132
RH
17212#endif
17213 /* We must defer this decision until after the whole
17214 file has been read, since there might be a .extern
17215 after the first use of this symbol. */
17216 || (before_relaxing
17217#ifndef NO_ECOFF_DEBUGGING
49309057 17218 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17219#endif
17220 && S_GET_VALUE (sym) == 0)
17221 || (S_GET_VALUE (sym) != 0
17222 && S_GET_VALUE (sym) <= g_switch_value)))
17223 change = 0;
17224 else
17225 {
17226 const char *segname;
17227
17228 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17229 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17230 && strcmp (segname, ".lit4") != 0);
17231 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17232 && strcmp (segname, ".sbss") != 0
17233 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17234 && strncmp (segname, ".sbss.", 6) != 0
17235 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17236 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17237 }
17238 return change;
17239 }
17240 else
c9914766 17241 /* We are not optimizing for the $gp register. */
252b5132
RH
17242 return 1;
17243}
17244
5919d012
RS
17245
17246/* Return true if the given symbol should be considered local for SVR4 PIC. */
17247
17248static bfd_boolean
17a2f251 17249pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
17250{
17251 asection *symsec;
5919d012
RS
17252
17253 /* Handle the case of a symbol equated to another symbol. */
17254 while (symbol_equated_reloc_p (sym))
17255 {
17256 symbolS *n;
17257
5f0fe04b 17258 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17259 n = symbol_get_value_expression (sym)->X_add_symbol;
17260 if (n == sym)
17261 break;
17262 sym = n;
17263 }
17264
df1f3cda
DD
17265 if (symbol_section_p (sym))
17266 return TRUE;
17267
5919d012
RS
17268 symsec = S_GET_SEGMENT (sym);
17269
5919d012 17270 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17271 return (!bfd_is_und_section (symsec)
17272 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
17273 && !bfd_is_com_section (symsec)
17274 && !s_is_linkonce (sym, segtype)
5919d012
RS
17275#ifdef OBJ_ELF
17276 /* A global or weak symbol is treated as external. */
f43abd2b 17277 && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
5919d012
RS
17278#endif
17279 );
17280}
17281
17282
252b5132
RH
17283/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17284 extended opcode. SEC is the section the frag is in. */
17285
17286static int
17a2f251 17287mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
17288{
17289 int type;
3994f87e 17290 const struct mips16_immed_operand *op;
252b5132
RH
17291 offsetT val;
17292 int mintiny, maxtiny;
17293 segT symsec;
98aa84af 17294 fragS *sym_frag;
252b5132
RH
17295
17296 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17297 return 0;
17298 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17299 return 1;
17300
17301 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17302 op = mips16_immed_operands;
17303 while (op->type != type)
17304 {
17305 ++op;
9c2799c2 17306 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
17307 }
17308
17309 if (op->unsp)
17310 {
17311 if (type == '<' || type == '>' || type == '[' || type == ']')
17312 {
17313 mintiny = 1;
17314 maxtiny = 1 << op->nbits;
17315 }
17316 else
17317 {
17318 mintiny = 0;
17319 maxtiny = (1 << op->nbits) - 1;
17320 }
17321 }
17322 else
17323 {
17324 mintiny = - (1 << (op->nbits - 1));
17325 maxtiny = (1 << (op->nbits - 1)) - 1;
17326 }
17327
98aa84af 17328 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 17329 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 17330 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
17331
17332 if (op->pcrel)
17333 {
17334 addressT addr;
17335
17336 /* We won't have the section when we are called from
17337 mips_relax_frag. However, we will always have been called
17338 from md_estimate_size_before_relax first. If this is a
17339 branch to a different section, we mark it as such. If SEC is
17340 NULL, and the frag is not marked, then it must be a branch to
17341 the same section. */
17342 if (sec == NULL)
17343 {
17344 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17345 return 1;
17346 }
17347 else
17348 {
98aa84af 17349 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
17350 if (symsec != sec)
17351 {
17352 fragp->fr_subtype =
17353 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17354
17355 /* FIXME: We should support this, and let the linker
17356 catch branches and loads that are out of range. */
17357 as_bad_where (fragp->fr_file, fragp->fr_line,
17358 _("unsupported PC relative reference to different section"));
17359
17360 return 1;
17361 }
98aa84af
AM
17362 if (fragp != sym_frag && sym_frag->fr_address == 0)
17363 /* Assume non-extended on the first relaxation pass.
17364 The address we have calculated will be bogus if this is
17365 a forward branch to another frag, as the forward frag
17366 will have fr_address == 0. */
17367 return 0;
252b5132
RH
17368 }
17369
17370 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
17371 the same section. If the relax_marker of the symbol fragment
17372 differs from the relax_marker of this fragment, we have not
17373 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
17374 in STRETCH in order to get a better estimate of the address.
17375 This particularly matters because of the shift bits. */
17376 if (stretch != 0
98aa84af 17377 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
17378 {
17379 fragS *f;
17380
17381 /* Adjust stretch for any alignment frag. Note that if have
17382 been expanding the earlier code, the symbol may be
17383 defined in what appears to be an earlier frag. FIXME:
17384 This doesn't handle the fr_subtype field, which specifies
17385 a maximum number of bytes to skip when doing an
17386 alignment. */
98aa84af 17387 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
17388 {
17389 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17390 {
17391 if (stretch < 0)
17392 stretch = - ((- stretch)
17393 & ~ ((1 << (int) f->fr_offset) - 1));
17394 else
17395 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17396 if (stretch == 0)
17397 break;
17398 }
17399 }
17400 if (f != NULL)
17401 val += stretch;
17402 }
17403
17404 addr = fragp->fr_address + fragp->fr_fix;
17405
17406 /* The base address rules are complicated. The base address of
17407 a branch is the following instruction. The base address of a
17408 PC relative load or add is the instruction itself, but if it
17409 is in a delay slot (in which case it can not be extended) use
17410 the address of the instruction whose delay slot it is in. */
17411 if (type == 'p' || type == 'q')
17412 {
17413 addr += 2;
17414
17415 /* If we are currently assuming that this frag should be
17416 extended, then, the current address is two bytes
bdaaa2e1 17417 higher. */
252b5132
RH
17418 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17419 addr += 2;
17420
17421 /* Ignore the low bit in the target, since it will be set
17422 for a text label. */
17423 if ((val & 1) != 0)
17424 --val;
17425 }
17426 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17427 addr -= 4;
17428 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17429 addr -= 2;
17430
17431 val -= addr & ~ ((1 << op->shift) - 1);
17432
17433 /* Branch offsets have an implicit 0 in the lowest bit. */
17434 if (type == 'p' || type == 'q')
17435 val /= 2;
17436
17437 /* If any of the shifted bits are set, we must use an extended
17438 opcode. If the address depends on the size of this
17439 instruction, this can lead to a loop, so we arrange to always
17440 use an extended opcode. We only check this when we are in
17441 the main relaxation loop, when SEC is NULL. */
17442 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17443 {
17444 fragp->fr_subtype =
17445 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17446 return 1;
17447 }
17448
17449 /* If we are about to mark a frag as extended because the value
17450 is precisely maxtiny + 1, then there is a chance of an
17451 infinite loop as in the following code:
17452 la $4,foo
17453 .skip 1020
17454 .align 2
17455 foo:
17456 In this case when the la is extended, foo is 0x3fc bytes
17457 away, so the la can be shrunk, but then foo is 0x400 away, so
17458 the la must be extended. To avoid this loop, we mark the
17459 frag as extended if it was small, and is about to become
17460 extended with a value of maxtiny + 1. */
17461 if (val == ((maxtiny + 1) << op->shift)
17462 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17463 && sec == NULL)
17464 {
17465 fragp->fr_subtype =
17466 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17467 return 1;
17468 }
17469 }
17470 else if (symsec != absolute_section && sec != NULL)
17471 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17472
17473 if ((val & ((1 << op->shift) - 1)) != 0
17474 || val < (mintiny << op->shift)
17475 || val > (maxtiny << op->shift))
17476 return 1;
17477 else
17478 return 0;
17479}
17480
4a6a3df4
AO
17481/* Compute the length of a branch sequence, and adjust the
17482 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17483 worst-case length is computed, with UPDATE being used to indicate
17484 whether an unconditional (-1), branch-likely (+1) or regular (0)
17485 branch is to be computed. */
17486static int
17a2f251 17487relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17488{
b34976b6 17489 bfd_boolean toofar;
4a6a3df4
AO
17490 int length;
17491
17492 if (fragp
17493 && S_IS_DEFINED (fragp->fr_symbol)
17494 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17495 {
17496 addressT addr;
17497 offsetT val;
17498
17499 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17500
17501 addr = fragp->fr_address + fragp->fr_fix + 4;
17502
17503 val -= addr;
17504
17505 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17506 }
17507 else if (fragp)
17508 /* If the symbol is not defined or it's in a different segment,
17509 assume the user knows what's going on and emit a short
17510 branch. */
b34976b6 17511 toofar = FALSE;
4a6a3df4 17512 else
b34976b6 17513 toofar = TRUE;
4a6a3df4
AO
17514
17515 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17516 fragp->fr_subtype
66b3e8da
MR
17517 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17518 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17519 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17520 RELAX_BRANCH_LINK (fragp->fr_subtype),
17521 toofar);
17522
17523 length = 4;
17524 if (toofar)
17525 {
17526 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17527 length += 8;
17528
17529 if (mips_pic != NO_PIC)
17530 {
17531 /* Additional space for PIC loading of target address. */
17532 length += 8;
17533 if (mips_opts.isa == ISA_MIPS1)
17534 /* Additional space for $at-stabilizing nop. */
17535 length += 4;
17536 }
17537
17538 /* If branch is conditional. */
17539 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17540 length += 8;
17541 }
b34976b6 17542
4a6a3df4
AO
17543 return length;
17544}
17545
df58fc94
RS
17546/* Compute the length of a branch sequence, and adjust the
17547 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17548 worst-case length is computed, with UPDATE being used to indicate
17549 whether an unconditional (-1), or regular (0) branch is to be
17550 computed. */
17551
17552static int
17553relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17554{
17555 bfd_boolean toofar;
17556 int length;
17557
17558 if (fragp
17559 && S_IS_DEFINED (fragp->fr_symbol)
17560 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17561 {
17562 addressT addr;
17563 offsetT val;
17564
17565 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17566 /* Ignore the low bit in the target, since it will be set
17567 for a text label. */
17568 if ((val & 1) != 0)
17569 --val;
17570
17571 addr = fragp->fr_address + fragp->fr_fix + 4;
17572
17573 val -= addr;
17574
17575 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17576 }
17577 else if (fragp)
17578 /* If the symbol is not defined or it's in a different segment,
17579 assume the user knows what's going on and emit a short
17580 branch. */
17581 toofar = FALSE;
17582 else
17583 toofar = TRUE;
17584
17585 if (fragp && update
17586 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17587 fragp->fr_subtype = (toofar
17588 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17589 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17590
17591 length = 4;
17592 if (toofar)
17593 {
17594 bfd_boolean compact_known = fragp != NULL;
17595 bfd_boolean compact = FALSE;
17596 bfd_boolean uncond;
17597
17598 if (compact_known)
17599 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17600 if (fragp)
17601 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17602 else
17603 uncond = update < 0;
17604
17605 /* If label is out of range, we turn branch <br>:
17606
17607 <br> label # 4 bytes
17608 0:
17609
17610 into:
17611
17612 j label # 4 bytes
17613 nop # 2 bytes if compact && !PIC
17614 0:
17615 */
17616 if (mips_pic == NO_PIC && (!compact_known || compact))
17617 length += 2;
17618
17619 /* If assembling PIC code, we further turn:
17620
17621 j label # 4 bytes
17622
17623 into:
17624
17625 lw/ld at, %got(label)(gp) # 4 bytes
17626 d/addiu at, %lo(label) # 4 bytes
17627 jr/c at # 2 bytes
17628 */
17629 if (mips_pic != NO_PIC)
17630 length += 6;
17631
17632 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17633
17634 <brneg> 0f # 4 bytes
17635 nop # 2 bytes if !compact
17636 */
17637 if (!uncond)
17638 length += (compact_known && compact) ? 4 : 6;
17639 }
17640
17641 return length;
17642}
17643
17644/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17645 bit accordingly. */
17646
17647static int
17648relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17649{
17650 bfd_boolean toofar;
17651
df58fc94
RS
17652 if (fragp
17653 && S_IS_DEFINED (fragp->fr_symbol)
17654 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17655 {
17656 addressT addr;
17657 offsetT val;
17658 int type;
17659
17660 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17661 /* Ignore the low bit in the target, since it will be set
17662 for a text label. */
17663 if ((val & 1) != 0)
17664 --val;
17665
17666 /* Assume this is a 2-byte branch. */
17667 addr = fragp->fr_address + fragp->fr_fix + 2;
17668
17669 /* We try to avoid the infinite loop by not adding 2 more bytes for
17670 long branches. */
17671
17672 val -= addr;
17673
17674 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17675 if (type == 'D')
17676 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17677 else if (type == 'E')
17678 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17679 else
17680 abort ();
17681 }
17682 else
17683 /* If the symbol is not defined or it's in a different segment,
17684 we emit a normal 32-bit branch. */
17685 toofar = TRUE;
17686
17687 if (fragp && update
17688 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17689 fragp->fr_subtype
17690 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17691 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17692
17693 if (toofar)
17694 return 4;
17695
17696 return 2;
17697}
17698
252b5132
RH
17699/* Estimate the size of a frag before relaxing. Unless this is the
17700 mips16, we are not really relaxing here, and the final size is
17701 encoded in the subtype information. For the mips16, we have to
17702 decide whether we are using an extended opcode or not. */
17703
252b5132 17704int
17a2f251 17705md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17706{
5919d012 17707 int change;
252b5132 17708
4a6a3df4
AO
17709 if (RELAX_BRANCH_P (fragp->fr_subtype))
17710 {
17711
b34976b6
AM
17712 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17713
4a6a3df4
AO
17714 return fragp->fr_var;
17715 }
17716
252b5132 17717 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
17718 /* We don't want to modify the EXTENDED bit here; it might get us
17719 into infinite loops. We change it only in mips_relax_frag(). */
17720 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 17721
df58fc94
RS
17722 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17723 {
17724 int length = 4;
17725
17726 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17727 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17728 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17729 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17730 fragp->fr_var = length;
17731
17732 return length;
17733 }
17734
252b5132 17735 if (mips_pic == NO_PIC)
5919d012 17736 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 17737 else if (mips_pic == SVR4_PIC)
5919d012 17738 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
17739 else if (mips_pic == VXWORKS_PIC)
17740 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17741 change = 0;
252b5132
RH
17742 else
17743 abort ();
17744
17745 if (change)
17746 {
4d7206a2 17747 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 17748 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 17749 }
4d7206a2
RS
17750 else
17751 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
17752}
17753
17754/* This is called to see whether a reloc against a defined symbol
de7e6852 17755 should be converted into a reloc against a section. */
252b5132
RH
17756
17757int
17a2f251 17758mips_fix_adjustable (fixS *fixp)
252b5132 17759{
252b5132
RH
17760 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17761 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17762 return 0;
a161fe53 17763
252b5132
RH
17764 if (fixp->fx_addsy == NULL)
17765 return 1;
a161fe53 17766
de7e6852
RS
17767 /* If symbol SYM is in a mergeable section, relocations of the form
17768 SYM + 0 can usually be made section-relative. The mergeable data
17769 is then identified by the section offset rather than by the symbol.
17770
17771 However, if we're generating REL LO16 relocations, the offset is split
17772 between the LO16 and parterning high part relocation. The linker will
17773 need to recalculate the complete offset in order to correctly identify
17774 the merge data.
17775
17776 The linker has traditionally not looked for the parterning high part
17777 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17778 placed anywhere. Rather than break backwards compatibility by changing
17779 this, it seems better not to force the issue, and instead keep the
17780 original symbol. This will work with either linker behavior. */
738e5348 17781 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 17782 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
17783 && HAVE_IN_PLACE_ADDENDS
17784 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17785 return 0;
17786
ce70d90a
MR
17787 /* There is no place to store an in-place offset for JALR relocations.
17788 Likewise an in-range offset of PC-relative relocations may overflow
17789 the in-place relocatable field if recalculated against the start
17790 address of the symbol's containing section. */
17791 if (HAVE_IN_PLACE_ADDENDS
df58fc94 17792 && (fixp->fx_pcrel || jalr_reloc_p (fixp->fx_r_type)))
1180b5a4
RS
17793 return 0;
17794
252b5132 17795#ifdef OBJ_ELF
b314ec0e
RS
17796 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17797 to a floating-point stub. The same is true for non-R_MIPS16_26
17798 relocations against MIPS16 functions; in this case, the stub becomes
17799 the function's canonical address.
17800
17801 Floating-point stubs are stored in unique .mips16.call.* or
17802 .mips16.fn.* sections. If a stub T for function F is in section S,
17803 the first relocation in section S must be against F; this is how the
17804 linker determines the target function. All relocations that might
17805 resolve to T must also be against F. We therefore have the following
17806 restrictions, which are given in an intentionally-redundant way:
17807
17808 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17809 symbols.
17810
17811 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17812 if that stub might be used.
17813
17814 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17815 symbols.
17816
17817 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17818 that stub might be used.
17819
17820 There is a further restriction:
17821
df58fc94
RS
17822 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17823 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17824 targets with in-place addends; the relocation field cannot
b314ec0e
RS
17825 encode the low bit.
17826
df58fc94
RS
17827 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17828 against a MIPS16 symbol. We deal with (5) by by not reducing any
17829 such relocations on REL targets.
b314ec0e
RS
17830
17831 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17832 relocation against some symbol R, no relocation against R may be
17833 reduced. (Note that this deals with (2) as well as (1) because
17834 relocations against global symbols will never be reduced on ELF
17835 targets.) This approach is a little simpler than trying to detect
17836 stub sections, and gives the "all or nothing" per-symbol consistency
17837 that we have for MIPS16 symbols. */
f43abd2b 17838 if (IS_ELF
b314ec0e 17839 && fixp->fx_subsy == NULL
30c09090 17840 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
df58fc94
RS
17841 || *symbol_get_tc (fixp->fx_addsy)
17842 || (HAVE_IN_PLACE_ADDENDS
17843 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17844 && jmp_reloc_p (fixp->fx_r_type))))
252b5132
RH
17845 return 0;
17846#endif
a161fe53 17847
252b5132
RH
17848 return 1;
17849}
17850
17851/* Translate internal representation of relocation info to BFD target
17852 format. */
17853
17854arelent **
17a2f251 17855tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
17856{
17857 static arelent *retval[4];
17858 arelent *reloc;
17859 bfd_reloc_code_real_type code;
17860
4b0cff4e
TS
17861 memset (retval, 0, sizeof(retval));
17862 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
17863 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
17864 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
17865 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17866
bad36eac
DJ
17867 if (fixp->fx_pcrel)
17868 {
df58fc94
RS
17869 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17870 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17871 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
17872 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
bad36eac
DJ
17873
17874 /* At this point, fx_addnumber is "symbol offset - pcrel address".
17875 Relocations want only the symbol offset. */
17876 reloc->addend = fixp->fx_addnumber + reloc->address;
f43abd2b 17877 if (!IS_ELF)
bad36eac
DJ
17878 {
17879 /* A gruesome hack which is a result of the gruesome gas
17880 reloc handling. What's worse, for COFF (as opposed to
17881 ECOFF), we might need yet another copy of reloc->address.
17882 See bfd_install_relocation. */
17883 reloc->addend += reloc->address;
17884 }
17885 }
17886 else
17887 reloc->addend = fixp->fx_addnumber;
252b5132 17888
438c16b8
TS
17889 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17890 entry to be used in the relocation's section offset. */
17891 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
17892 {
17893 reloc->address = reloc->addend;
17894 reloc->addend = 0;
17895 }
17896
252b5132 17897 code = fixp->fx_r_type;
252b5132 17898
bad36eac 17899 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
17900 if (reloc->howto == NULL)
17901 {
17902 as_bad_where (fixp->fx_file, fixp->fx_line,
17903 _("Can not represent %s relocation in this object file format"),
17904 bfd_get_reloc_code_name (code));
17905 retval[0] = NULL;
17906 }
17907
17908 return retval;
17909}
17910
17911/* Relax a machine dependent frag. This returns the amount by which
17912 the current size of the frag should change. */
17913
17914int
17a2f251 17915mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 17916{
4a6a3df4
AO
17917 if (RELAX_BRANCH_P (fragp->fr_subtype))
17918 {
17919 offsetT old_var = fragp->fr_var;
b34976b6
AM
17920
17921 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
17922
17923 return fragp->fr_var - old_var;
17924 }
17925
df58fc94
RS
17926 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17927 {
17928 offsetT old_var = fragp->fr_var;
17929 offsetT new_var = 4;
17930
17931 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17932 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17933 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17934 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17935 fragp->fr_var = new_var;
17936
17937 return new_var - old_var;
17938 }
17939
252b5132
RH
17940 if (! RELAX_MIPS16_P (fragp->fr_subtype))
17941 return 0;
17942
c4e7957c 17943 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
17944 {
17945 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17946 return 0;
17947 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17948 return 2;
17949 }
17950 else
17951 {
17952 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17953 return 0;
17954 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17955 return -2;
17956 }
17957
17958 return 0;
17959}
17960
17961/* Convert a machine dependent frag. */
17962
17963void
17a2f251 17964md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 17965{
4a6a3df4
AO
17966 if (RELAX_BRANCH_P (fragp->fr_subtype))
17967 {
4d68580a 17968 char *buf;
4a6a3df4
AO
17969 unsigned long insn;
17970 expressionS exp;
17971 fixS *fixp;
b34976b6 17972
4d68580a
RS
17973 buf = fragp->fr_literal + fragp->fr_fix;
17974 insn = read_insn (buf);
b34976b6 17975
4a6a3df4
AO
17976 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17977 {
17978 /* We generate a fixup instead of applying it right now
17979 because, if there are linker relaxations, we're going to
17980 need the relocations. */
17981 exp.X_op = O_symbol;
17982 exp.X_add_symbol = fragp->fr_symbol;
17983 exp.X_add_number = fragp->fr_offset;
17984
4d68580a
RS
17985 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
17986 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
17987 fixp->fx_file = fragp->fr_file;
17988 fixp->fx_line = fragp->fr_line;
b34976b6 17989
4d68580a 17990 buf = write_insn (buf, insn);
4a6a3df4
AO
17991 }
17992 else
17993 {
17994 int i;
17995
17996 as_warn_where (fragp->fr_file, fragp->fr_line,
5c4f07ba 17997 _("Relaxed out-of-range branch into a jump"));
4a6a3df4
AO
17998
17999 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18000 goto uncond;
18001
18002 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18003 {
18004 /* Reverse the branch. */
18005 switch ((insn >> 28) & 0xf)
18006 {
18007 case 4:
18008 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
18009 have the condition reversed by tweaking a single
18010 bit, and their opcodes all have 0x4???????. */
9c2799c2 18011 gas_assert ((insn & 0xf1000000) == 0x41000000);
4a6a3df4
AO
18012 insn ^= 0x00010000;
18013 break;
18014
18015 case 0:
18016 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18017 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18018 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18019 insn ^= 0x00010000;
18020 break;
b34976b6 18021
4a6a3df4
AO
18022 case 1:
18023 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18024 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18025 insn ^= 0x04000000;
18026 break;
18027
18028 default:
18029 abort ();
18030 }
18031 }
18032
18033 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18034 {
18035 /* Clear the and-link bit. */
9c2799c2 18036 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18037
54f4ddb3
TS
18038 /* bltzal 0x04100000 bgezal 0x04110000
18039 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18040 insn &= ~0x00100000;
18041 }
18042
18043 /* Branch over the branch (if the branch was likely) or the
18044 full jump (not likely case). Compute the offset from the
18045 current instruction to branch to. */
18046 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18047 i = 16;
18048 else
18049 {
18050 /* How many bytes in instructions we've already emitted? */
4d68580a 18051 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18052 /* How many bytes in instructions from here to the end? */
18053 i = fragp->fr_var - i;
18054 }
18055 /* Convert to instruction count. */
18056 i >>= 2;
18057 /* Branch counts from the next instruction. */
b34976b6 18058 i--;
4a6a3df4
AO
18059 insn |= i;
18060 /* Branch over the jump. */
4d68580a 18061 buf = write_insn (buf, insn);
4a6a3df4 18062
54f4ddb3 18063 /* nop */
4d68580a 18064 buf = write_insn (buf, 0);
4a6a3df4
AO
18065
18066 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18067 {
18068 /* beql $0, $0, 2f */
18069 insn = 0x50000000;
18070 /* Compute the PC offset from the current instruction to
18071 the end of the variable frag. */
18072 /* How many bytes in instructions we've already emitted? */
4d68580a 18073 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18074 /* How many bytes in instructions from here to the end? */
18075 i = fragp->fr_var - i;
18076 /* Convert to instruction count. */
18077 i >>= 2;
18078 /* Don't decrement i, because we want to branch over the
18079 delay slot. */
4a6a3df4 18080 insn |= i;
4a6a3df4 18081
4d68580a
RS
18082 buf = write_insn (buf, insn);
18083 buf = write_insn (buf, 0);
4a6a3df4
AO
18084 }
18085
18086 uncond:
18087 if (mips_pic == NO_PIC)
18088 {
18089 /* j or jal. */
18090 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18091 ? 0x0c000000 : 0x08000000);
18092 exp.X_op = O_symbol;
18093 exp.X_add_symbol = fragp->fr_symbol;
18094 exp.X_add_number = fragp->fr_offset;
18095
4d68580a
RS
18096 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18097 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18098 fixp->fx_file = fragp->fr_file;
18099 fixp->fx_line = fragp->fr_line;
18100
4d68580a 18101 buf = write_insn (buf, insn);
4a6a3df4
AO
18102 }
18103 else
18104 {
66b3e8da
MR
18105 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18106
4a6a3df4 18107 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18108 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18109 insn |= at << OP_SH_RT;
4a6a3df4
AO
18110 exp.X_op = O_symbol;
18111 exp.X_add_symbol = fragp->fr_symbol;
18112 exp.X_add_number = fragp->fr_offset;
18113
18114 if (fragp->fr_offset)
18115 {
18116 exp.X_add_symbol = make_expr_symbol (&exp);
18117 exp.X_add_number = 0;
18118 }
18119
4d68580a
RS
18120 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18121 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18122 fixp->fx_file = fragp->fr_file;
18123 fixp->fx_line = fragp->fr_line;
18124
4d68580a 18125 buf = write_insn (buf, insn);
b34976b6 18126
4a6a3df4 18127 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18128 /* nop */
18129 buf = write_insn (buf, 0);
4a6a3df4
AO
18130
18131 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18132 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18133 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18134
4d68580a
RS
18135 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18136 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18137 fixp->fx_file = fragp->fr_file;
18138 fixp->fx_line = fragp->fr_line;
b34976b6 18139
4d68580a 18140 buf = write_insn (buf, insn);
4a6a3df4
AO
18141
18142 /* j(al)r $at. */
18143 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18144 insn = 0x0000f809;
4a6a3df4 18145 else
66b3e8da
MR
18146 insn = 0x00000008;
18147 insn |= at << OP_SH_RS;
4a6a3df4 18148
4d68580a 18149 buf = write_insn (buf, insn);
4a6a3df4
AO
18150 }
18151 }
18152
4a6a3df4 18153 fragp->fr_fix += fragp->fr_var;
4d68580a 18154 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18155 return;
18156 }
18157
df58fc94
RS
18158 /* Relax microMIPS branches. */
18159 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18160 {
4d68580a 18161 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18162 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18163 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18164 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18165 bfd_boolean short_ds;
df58fc94
RS
18166 unsigned long insn;
18167 expressionS exp;
18168 fixS *fixp;
18169
18170 exp.X_op = O_symbol;
18171 exp.X_add_symbol = fragp->fr_symbol;
18172 exp.X_add_number = fragp->fr_offset;
18173
18174 fragp->fr_fix += fragp->fr_var;
18175
18176 /* Handle 16-bit branches that fit or are forced to fit. */
18177 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18178 {
18179 /* We generate a fixup instead of applying it right now,
18180 because if there is linker relaxation, we're going to
18181 need the relocations. */
18182 if (type == 'D')
4d68580a 18183 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18184 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18185 else if (type == 'E')
4d68580a 18186 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18187 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18188 else
18189 abort ();
18190
18191 fixp->fx_file = fragp->fr_file;
18192 fixp->fx_line = fragp->fr_line;
18193
18194 /* These relocations can have an addend that won't fit in
18195 2 octets. */
18196 fixp->fx_no_overflow = 1;
18197
18198 return;
18199 }
18200
2309ddf2 18201 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18202 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18203 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18204 {
18205 /* We generate a fixup instead of applying it right now,
18206 because if there is linker relaxation, we're going to
18207 need the relocations. */
4d68580a
RS
18208 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18209 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18210 fixp->fx_file = fragp->fr_file;
18211 fixp->fx_line = fragp->fr_line;
18212
18213 if (type == 0)
18214 return;
18215 }
18216
18217 /* Relax 16-bit branches to 32-bit branches. */
18218 if (type != 0)
18219 {
4d68580a 18220 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18221
18222 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18223 insn = 0x94000000; /* beq */
18224 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18225 {
18226 unsigned long regno;
18227
18228 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18229 regno = micromips_to_32_reg_d_map [regno];
18230 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18231 insn |= regno << MICROMIPSOP_SH_RS;
18232 }
18233 else
18234 abort ();
18235
18236 /* Nothing else to do, just write it out. */
18237 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18238 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18239 {
4d68580a
RS
18240 buf = write_compressed_insn (buf, insn, 4);
18241 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18242 return;
18243 }
18244 }
18245 else
4d68580a 18246 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18247
18248 /* Relax 32-bit branches to a sequence of instructions. */
18249 as_warn_where (fragp->fr_file, fragp->fr_line,
18250 _("Relaxed out-of-range branch into a jump"));
18251
2309ddf2
MR
18252 /* Set the short-delay-slot bit. */
18253 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
18254
18255 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18256 {
18257 symbolS *l;
18258
18259 /* Reverse the branch. */
18260 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18261 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18262 insn ^= 0x20000000;
18263 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18264 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18265 || (insn & 0xffe00000) == 0x40800000 /* blez */
18266 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18267 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18268 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18269 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18270 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18271 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18272 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18273 insn ^= 0x00400000;
18274 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18275 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18276 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18277 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18278 insn ^= 0x00200000;
18279 else
18280 abort ();
18281
18282 if (al)
18283 {
18284 /* Clear the and-link and short-delay-slot bits. */
18285 gas_assert ((insn & 0xfda00000) == 0x40200000);
18286
18287 /* bltzal 0x40200000 bgezal 0x40600000 */
18288 /* bltzals 0x42200000 bgezals 0x42600000 */
18289 insn &= ~0x02200000;
18290 }
18291
18292 /* Make a label at the end for use with the branch. */
18293 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18294 micromips_label_inc ();
18295#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
18296 if (IS_ELF)
18297 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18298#endif
18299
18300 /* Refer to it. */
4d68580a
RS
18301 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18302 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18303 fixp->fx_file = fragp->fr_file;
18304 fixp->fx_line = fragp->fr_line;
18305
18306 /* Branch over the jump. */
4d68580a 18307 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18308 if (!compact)
4d68580a
RS
18309 /* nop */
18310 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
18311 }
18312
18313 if (mips_pic == NO_PIC)
18314 {
2309ddf2
MR
18315 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
18316
df58fc94
RS
18317 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18318 insn = al ? jal : 0xd4000000;
18319
4d68580a
RS
18320 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18321 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18322 fixp->fx_file = fragp->fr_file;
18323 fixp->fx_line = fragp->fr_line;
18324
4d68580a 18325 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18326 if (compact)
4d68580a
RS
18327 /* nop */
18328 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
18329 }
18330 else
18331 {
18332 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
18333 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18334 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
18335
18336 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18337 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18338 insn |= at << MICROMIPSOP_SH_RT;
18339
18340 if (exp.X_add_number)
18341 {
18342 exp.X_add_symbol = make_expr_symbol (&exp);
18343 exp.X_add_number = 0;
18344 }
18345
4d68580a
RS
18346 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18347 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18348 fixp->fx_file = fragp->fr_file;
18349 fixp->fx_line = fragp->fr_line;
18350
4d68580a 18351 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18352
18353 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18354 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18355 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18356
4d68580a
RS
18357 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18358 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18359 fixp->fx_file = fragp->fr_file;
18360 fixp->fx_line = fragp->fr_line;
18361
4d68580a 18362 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18363
18364 /* jr/jrc/jalr/jalrs $at */
18365 insn = al ? jalr : jr;
18366 insn |= at << MICROMIPSOP_SH_MJ;
18367
4d68580a 18368 buf = write_compressed_insn (buf, insn, 2);
df58fc94
RS
18369 }
18370
4d68580a 18371 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18372 return;
18373 }
18374
252b5132
RH
18375 if (RELAX_MIPS16_P (fragp->fr_subtype))
18376 {
18377 int type;
3994f87e 18378 const struct mips16_immed_operand *op;
252b5132 18379 offsetT val;
5c04167a
RS
18380 char *buf;
18381 unsigned int user_length, length;
252b5132 18382 unsigned long insn;
5c04167a 18383 bfd_boolean ext;
252b5132
RH
18384
18385 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18386 op = mips16_immed_operands;
18387 while (op->type != type)
18388 ++op;
18389
5c04167a 18390 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
5f5f22c0 18391 val = resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
18392 if (op->pcrel)
18393 {
18394 addressT addr;
18395
18396 addr = fragp->fr_address + fragp->fr_fix;
18397
18398 /* The rules for the base address of a PC relative reloc are
18399 complicated; see mips16_extended_frag. */
18400 if (type == 'p' || type == 'q')
18401 {
18402 addr += 2;
18403 if (ext)
18404 addr += 2;
18405 /* Ignore the low bit in the target, since it will be
18406 set for a text label. */
18407 if ((val & 1) != 0)
18408 --val;
18409 }
18410 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18411 addr -= 4;
18412 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18413 addr -= 2;
18414
18415 addr &= ~ (addressT) ((1 << op->shift) - 1);
18416 val -= addr;
18417
18418 /* Make sure the section winds up with the alignment we have
18419 assumed. */
18420 if (op->shift > 0)
18421 record_alignment (asec, op->shift);
18422 }
18423
18424 if (ext
18425 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18426 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18427 as_warn_where (fragp->fr_file, fragp->fr_line,
18428 _("extended instruction in delay slot"));
18429
5c04167a 18430 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 18431
4d68580a 18432 insn = read_compressed_insn (buf, 2);
5c04167a
RS
18433 if (ext)
18434 insn |= MIPS16_EXTEND;
252b5132 18435
5c04167a
RS
18436 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18437 user_length = 4;
18438 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18439 user_length = 2;
18440 else
18441 user_length = 0;
18442
43c0598f 18443 mips16_immed (fragp->fr_file, fragp->fr_line, type,
c150d1d2 18444 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 18445
5c04167a
RS
18446 length = (ext ? 4 : 2);
18447 gas_assert (mips16_opcode_length (insn) == length);
18448 write_compressed_insn (buf, insn, length);
18449 fragp->fr_fix += length;
252b5132
RH
18450 }
18451 else
18452 {
df58fc94
RS
18453 relax_substateT subtype = fragp->fr_subtype;
18454 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18455 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
18456 int first, second;
18457 fixS *fixp;
252b5132 18458
df58fc94
RS
18459 first = RELAX_FIRST (subtype);
18460 second = RELAX_SECOND (subtype);
4d7206a2 18461 fixp = (fixS *) fragp->fr_opcode;
252b5132 18462
df58fc94
RS
18463 /* If the delay slot chosen does not match the size of the instruction,
18464 then emit a warning. */
18465 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18466 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18467 {
18468 relax_substateT s;
18469 const char *msg;
18470
18471 s = subtype & (RELAX_DELAY_SLOT_16BIT
18472 | RELAX_DELAY_SLOT_SIZE_FIRST
18473 | RELAX_DELAY_SLOT_SIZE_SECOND);
18474 msg = macro_warning (s);
18475 if (msg != NULL)
db9b2be4 18476 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
18477 subtype &= ~s;
18478 }
18479
584892a6 18480 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 18481 if (use_second == second_longer)
584892a6 18482 {
df58fc94
RS
18483 relax_substateT s;
18484 const char *msg;
18485
18486 s = (subtype
18487 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18488 msg = macro_warning (s);
18489 if (msg != NULL)
db9b2be4 18490 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 18491 subtype &= ~s;
584892a6
RS
18492 }
18493
4d7206a2
RS
18494 /* Go through all the fixups for the first sequence. Disable them
18495 (by marking them as done) if we're going to use the second
18496 sequence instead. */
18497 while (fixp
18498 && fixp->fx_frag == fragp
18499 && fixp->fx_where < fragp->fr_fix - second)
18500 {
df58fc94 18501 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18502 fixp->fx_done = 1;
18503 fixp = fixp->fx_next;
18504 }
252b5132 18505
4d7206a2
RS
18506 /* Go through the fixups for the second sequence. Disable them if
18507 we're going to use the first sequence, otherwise adjust their
18508 addresses to account for the relaxation. */
18509 while (fixp && fixp->fx_frag == fragp)
18510 {
df58fc94 18511 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18512 fixp->fx_where -= first;
18513 else
18514 fixp->fx_done = 1;
18515 fixp = fixp->fx_next;
18516 }
18517
18518 /* Now modify the frag contents. */
df58fc94 18519 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18520 {
18521 char *start;
18522
18523 start = fragp->fr_literal + fragp->fr_fix - first - second;
18524 memmove (start, start + first, second);
18525 fragp->fr_fix -= first;
18526 }
18527 else
18528 fragp->fr_fix -= second;
252b5132
RH
18529 }
18530}
18531
18532#ifdef OBJ_ELF
18533
18534/* This function is called after the relocs have been generated.
18535 We've been storing mips16 text labels as odd. Here we convert them
18536 back to even for the convenience of the debugger. */
18537
18538void
17a2f251 18539mips_frob_file_after_relocs (void)
252b5132
RH
18540{
18541 asymbol **syms;
18542 unsigned int count, i;
18543
f43abd2b 18544 if (!IS_ELF)
252b5132
RH
18545 return;
18546
18547 syms = bfd_get_outsymbols (stdoutput);
18548 count = bfd_get_symcount (stdoutput);
18549 for (i = 0; i < count; i++, syms++)
df58fc94
RS
18550 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18551 && ((*syms)->value & 1) != 0)
18552 {
18553 (*syms)->value &= ~1;
18554 /* If the symbol has an odd size, it was probably computed
18555 incorrectly, so adjust that as well. */
18556 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18557 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18558 }
252b5132
RH
18559}
18560
18561#endif
18562
a1facbec
MR
18563/* This function is called whenever a label is defined, including fake
18564 labels instantiated off the dot special symbol. It is used when
18565 handling branch delays; if a branch has a label, we assume we cannot
18566 move it. This also bumps the value of the symbol by 1 in compressed
18567 code. */
252b5132 18568
e1b47bd5 18569static void
a1facbec 18570mips_record_label (symbolS *sym)
252b5132 18571{
a8dbcb85 18572 segment_info_type *si = seg_info (now_seg);
252b5132
RH
18573 struct insn_label_list *l;
18574
18575 if (free_insn_labels == NULL)
18576 l = (struct insn_label_list *) xmalloc (sizeof *l);
18577 else
18578 {
18579 l = free_insn_labels;
18580 free_insn_labels = l->next;
18581 }
18582
18583 l->label = sym;
a8dbcb85
TS
18584 l->next = si->label_list;
18585 si->label_list = l;
a1facbec 18586}
07a53e5c 18587
a1facbec
MR
18588/* This function is called as tc_frob_label() whenever a label is defined
18589 and adds a DWARF-2 record we only want for true labels. */
18590
18591void
18592mips_define_label (symbolS *sym)
18593{
18594 mips_record_label (sym);
07a53e5c
RH
18595#ifdef OBJ_ELF
18596 dwarf2_emit_label (sym);
18597#endif
252b5132 18598}
e1b47bd5
RS
18599
18600/* This function is called by tc_new_dot_label whenever a new dot symbol
18601 is defined. */
18602
18603void
18604mips_add_dot_label (symbolS *sym)
18605{
18606 mips_record_label (sym);
18607 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18608 mips_compressed_mark_label (sym);
18609}
252b5132
RH
18610\f
18611#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
18612
18613/* Some special processing for a MIPS ELF file. */
18614
18615void
17a2f251 18616mips_elf_final_processing (void)
252b5132
RH
18617{
18618 /* Write out the register information. */
316f5878 18619 if (mips_abi != N64_ABI)
252b5132
RH
18620 {
18621 Elf32_RegInfo s;
18622
18623 s.ri_gprmask = mips_gprmask;
18624 s.ri_cprmask[0] = mips_cprmask[0];
18625 s.ri_cprmask[1] = mips_cprmask[1];
18626 s.ri_cprmask[2] = mips_cprmask[2];
18627 s.ri_cprmask[3] = mips_cprmask[3];
18628 /* The gp_value field is set by the MIPS ELF backend. */
18629
18630 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18631 ((Elf32_External_RegInfo *)
18632 mips_regmask_frag));
18633 }
18634 else
18635 {
18636 Elf64_Internal_RegInfo s;
18637
18638 s.ri_gprmask = mips_gprmask;
18639 s.ri_pad = 0;
18640 s.ri_cprmask[0] = mips_cprmask[0];
18641 s.ri_cprmask[1] = mips_cprmask[1];
18642 s.ri_cprmask[2] = mips_cprmask[2];
18643 s.ri_cprmask[3] = mips_cprmask[3];
18644 /* The gp_value field is set by the MIPS ELF backend. */
18645
18646 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18647 ((Elf64_External_RegInfo *)
18648 mips_regmask_frag));
18649 }
18650
18651 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18652 sort of BFD interface for this. */
18653 if (mips_any_noreorder)
18654 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18655 if (mips_pic != NO_PIC)
143d77c5 18656 {
8b828383 18657 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
18658 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18659 }
18660 if (mips_abicalls)
18661 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 18662
98d3f06f 18663 /* Set MIPS ELF flags for ASEs. */
74cd071d
CF
18664 /* We may need to define a new flag for DSP ASE, and set this flag when
18665 file_ase_dsp is true. */
8b082fb1 18666 /* Same for DSP R2. */
ef2e4d86
CF
18667 /* We may need to define a new flag for MT ASE, and set this flag when
18668 file_ase_mt is true. */
a4672219
TS
18669 if (file_ase_mips16)
18670 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
18671 if (file_ase_micromips)
18672 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
1f25f5d3
CD
18673#if 0 /* XXX FIXME */
18674 if (file_ase_mips3d)
18675 elf_elfheader (stdoutput)->e_flags |= ???;
18676#endif
deec1734
CD
18677 if (file_ase_mdmx)
18678 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 18679
bdaaa2e1 18680 /* Set the MIPS ELF ABI flags. */
316f5878 18681 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 18682 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 18683 else if (mips_abi == O64_ABI)
252b5132 18684 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 18685 else if (mips_abi == EABI_ABI)
252b5132 18686 {
316f5878 18687 if (!file_mips_gp32)
252b5132
RH
18688 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18689 else
18690 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18691 }
316f5878 18692 else if (mips_abi == N32_ABI)
be00bddd
TS
18693 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18694
c9914766 18695 /* Nothing to do for N64_ABI. */
252b5132
RH
18696
18697 if (mips_32bitmode)
18698 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08
TS
18699
18700#if 0 /* XXX FIXME */
18701 /* 32 bit code with 64 bit FP registers. */
18702 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18703 elf_elfheader (stdoutput)->e_flags |= ???;
18704#endif
252b5132
RH
18705}
18706
18707#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
18708\f
beae10d5 18709typedef struct proc {
9b2f1d35
EC
18710 symbolS *func_sym;
18711 symbolS *func_end_sym;
beae10d5
KH
18712 unsigned long reg_mask;
18713 unsigned long reg_offset;
18714 unsigned long fpreg_mask;
18715 unsigned long fpreg_offset;
18716 unsigned long frame_offset;
18717 unsigned long frame_reg;
18718 unsigned long pc_reg;
18719} procS;
252b5132
RH
18720
18721static procS cur_proc;
18722static procS *cur_proc_ptr;
18723static int numprocs;
18724
df58fc94
RS
18725/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
18726 as "2", and a normal nop as "0". */
18727
18728#define NOP_OPCODE_MIPS 0
18729#define NOP_OPCODE_MIPS16 1
18730#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
18731
18732char
18733mips_nop_opcode (void)
18734{
df58fc94
RS
18735 if (seg_info (now_seg)->tc_segment_info_data.micromips)
18736 return NOP_OPCODE_MICROMIPS;
18737 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18738 return NOP_OPCODE_MIPS16;
18739 else
18740 return NOP_OPCODE_MIPS;
742a56fe
RS
18741}
18742
df58fc94
RS
18743/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
18744 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 18745
0a9ef439 18746void
17a2f251 18747mips_handle_align (fragS *fragp)
a19d8eb0 18748{
df58fc94 18749 char nop_opcode;
742a56fe 18750 char *p;
c67a084a
NC
18751 int bytes, size, excess;
18752 valueT opcode;
742a56fe 18753
0a9ef439
RH
18754 if (fragp->fr_type != rs_align_code)
18755 return;
18756
742a56fe 18757 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18758 nop_opcode = *p;
18759 switch (nop_opcode)
a19d8eb0 18760 {
df58fc94
RS
18761 case NOP_OPCODE_MICROMIPS:
18762 opcode = micromips_nop32_insn.insn_opcode;
18763 size = 4;
18764 break;
18765 case NOP_OPCODE_MIPS16:
c67a084a
NC
18766 opcode = mips16_nop_insn.insn_opcode;
18767 size = 2;
df58fc94
RS
18768 break;
18769 case NOP_OPCODE_MIPS:
18770 default:
c67a084a
NC
18771 opcode = nop_insn.insn_opcode;
18772 size = 4;
df58fc94 18773 break;
c67a084a 18774 }
a19d8eb0 18775
c67a084a
NC
18776 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18777 excess = bytes % size;
df58fc94
RS
18778
18779 /* Handle the leading part if we're not inserting a whole number of
18780 instructions, and make it the end of the fixed part of the frag.
18781 Try to fit in a short microMIPS NOP if applicable and possible,
18782 and use zeroes otherwise. */
18783 gas_assert (excess < 4);
18784 fragp->fr_fix += excess;
18785 switch (excess)
c67a084a 18786 {
df58fc94
RS
18787 case 3:
18788 *p++ = '\0';
18789 /* Fall through. */
18790 case 2:
18791 if (nop_opcode == NOP_OPCODE_MICROMIPS)
18792 {
4d68580a 18793 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
18794 break;
18795 }
18796 *p++ = '\0';
18797 /* Fall through. */
18798 case 1:
18799 *p++ = '\0';
18800 /* Fall through. */
18801 case 0:
18802 break;
a19d8eb0 18803 }
c67a084a
NC
18804
18805 md_number_to_chars (p, opcode, size);
18806 fragp->fr_var = size;
a19d8eb0
CP
18807}
18808
252b5132 18809static void
17a2f251 18810md_obj_begin (void)
252b5132
RH
18811{
18812}
18813
18814static void
17a2f251 18815md_obj_end (void)
252b5132 18816{
54f4ddb3 18817 /* Check for premature end, nesting errors, etc. */
252b5132 18818 if (cur_proc_ptr)
9a41af64 18819 as_warn (_("missing .end at end of assembly"));
252b5132
RH
18820}
18821
18822static long
17a2f251 18823get_number (void)
252b5132
RH
18824{
18825 int negative = 0;
18826 long val = 0;
18827
18828 if (*input_line_pointer == '-')
18829 {
18830 ++input_line_pointer;
18831 negative = 1;
18832 }
3882b010 18833 if (!ISDIGIT (*input_line_pointer))
956cd1d6 18834 as_bad (_("expected simple number"));
252b5132
RH
18835 if (input_line_pointer[0] == '0')
18836 {
18837 if (input_line_pointer[1] == 'x')
18838 {
18839 input_line_pointer += 2;
3882b010 18840 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
18841 {
18842 val <<= 4;
18843 val |= hex_value (*input_line_pointer++);
18844 }
18845 return negative ? -val : val;
18846 }
18847 else
18848 {
18849 ++input_line_pointer;
3882b010 18850 while (ISDIGIT (*input_line_pointer))
252b5132
RH
18851 {
18852 val <<= 3;
18853 val |= *input_line_pointer++ - '0';
18854 }
18855 return negative ? -val : val;
18856 }
18857 }
3882b010 18858 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
18859 {
18860 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18861 *input_line_pointer, *input_line_pointer);
956cd1d6 18862 as_warn (_("invalid number"));
252b5132
RH
18863 return -1;
18864 }
3882b010 18865 while (ISDIGIT (*input_line_pointer))
252b5132
RH
18866 {
18867 val *= 10;
18868 val += *input_line_pointer++ - '0';
18869 }
18870 return negative ? -val : val;
18871}
18872
18873/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
18874 is an initial number which is the ECOFF file index. In the non-ECOFF
18875 case .file implies DWARF-2. */
18876
18877static void
17a2f251 18878s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 18879{
ecb4347a
DJ
18880 static int first_file_directive = 0;
18881
c5dd6aab
DJ
18882 if (ECOFF_DEBUGGING)
18883 {
18884 get_number ();
18885 s_app_file (0);
18886 }
18887 else
ecb4347a
DJ
18888 {
18889 char *filename;
18890
18891 filename = dwarf2_directive_file (0);
18892
18893 /* Versions of GCC up to 3.1 start files with a ".file"
18894 directive even for stabs output. Make sure that this
18895 ".file" is handled. Note that you need a version of GCC
18896 after 3.1 in order to support DWARF-2 on MIPS. */
18897 if (filename != NULL && ! first_file_directive)
18898 {
18899 (void) new_logical_line (filename, -1);
c04f5787 18900 s_app_file_string (filename, 0);
ecb4347a
DJ
18901 }
18902 first_file_directive = 1;
18903 }
c5dd6aab
DJ
18904}
18905
18906/* The .loc directive, implying DWARF-2. */
252b5132
RH
18907
18908static void
17a2f251 18909s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 18910{
c5dd6aab
DJ
18911 if (!ECOFF_DEBUGGING)
18912 dwarf2_directive_loc (0);
252b5132
RH
18913}
18914
252b5132
RH
18915/* The .end directive. */
18916
18917static void
17a2f251 18918s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
18919{
18920 symbolS *p;
252b5132 18921
7a621144
DJ
18922 /* Following functions need their own .frame and .cprestore directives. */
18923 mips_frame_reg_valid = 0;
18924 mips_cprestore_valid = 0;
18925
252b5132
RH
18926 if (!is_end_of_line[(unsigned char) *input_line_pointer])
18927 {
18928 p = get_symbol ();
18929 demand_empty_rest_of_line ();
18930 }
18931 else
18932 p = NULL;
18933
14949570 18934 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
18935 as_warn (_(".end not in text section"));
18936
18937 if (!cur_proc_ptr)
18938 {
18939 as_warn (_(".end directive without a preceding .ent directive."));
18940 demand_empty_rest_of_line ();
18941 return;
18942 }
18943
18944 if (p != NULL)
18945 {
9c2799c2 18946 gas_assert (S_GET_NAME (p));
9b2f1d35 18947 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 18948 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
18949
18950 if (debug_type == DEBUG_STABS)
18951 stabs_generate_asm_endfunc (S_GET_NAME (p),
18952 S_GET_NAME (p));
252b5132
RH
18953 }
18954 else
18955 as_warn (_(".end directive missing or unknown symbol"));
18956
2132e3a3 18957#ifdef OBJ_ELF
9b2f1d35
EC
18958 /* Create an expression to calculate the size of the function. */
18959 if (p && cur_proc_ptr)
18960 {
18961 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
18962 expressionS *exp = xmalloc (sizeof (expressionS));
18963
18964 obj->size = exp;
18965 exp->X_op = O_subtract;
18966 exp->X_add_symbol = symbol_temp_new_now ();
18967 exp->X_op_symbol = p;
18968 exp->X_add_number = 0;
18969
18970 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
18971 }
18972
ecb4347a 18973 /* Generate a .pdr section. */
f43abd2b 18974 if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
18975 {
18976 segT saved_seg = now_seg;
18977 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
18978 expressionS exp;
18979 char *fragp;
252b5132 18980
252b5132 18981#ifdef md_flush_pending_output
ecb4347a 18982 md_flush_pending_output ();
252b5132
RH
18983#endif
18984
9c2799c2 18985 gas_assert (pdr_seg);
ecb4347a 18986 subseg_set (pdr_seg, 0);
252b5132 18987
ecb4347a
DJ
18988 /* Write the symbol. */
18989 exp.X_op = O_symbol;
18990 exp.X_add_symbol = p;
18991 exp.X_add_number = 0;
18992 emit_expr (&exp, 4);
252b5132 18993
ecb4347a 18994 fragp = frag_more (7 * 4);
252b5132 18995
17a2f251
TS
18996 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
18997 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
18998 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
18999 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19000 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19001 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19002 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19003
ecb4347a
DJ
19004 subseg_set (saved_seg, saved_subseg);
19005 }
19006#endif /* OBJ_ELF */
252b5132
RH
19007
19008 cur_proc_ptr = NULL;
19009}
19010
19011/* The .aent and .ent directives. */
19012
19013static void
17a2f251 19014s_mips_ent (int aent)
252b5132 19015{
252b5132 19016 symbolS *symbolP;
252b5132
RH
19017
19018 symbolP = get_symbol ();
19019 if (*input_line_pointer == ',')
f9419b05 19020 ++input_line_pointer;
252b5132 19021 SKIP_WHITESPACE ();
3882b010 19022 if (ISDIGIT (*input_line_pointer)
d9a62219 19023 || *input_line_pointer == '-')
874e8986 19024 get_number ();
252b5132 19025
14949570 19026 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19027 as_warn (_(".ent or .aent not in text section."));
19028
19029 if (!aent && cur_proc_ptr)
9a41af64 19030 as_warn (_("missing .end"));
252b5132
RH
19031
19032 if (!aent)
19033 {
7a621144
DJ
19034 /* This function needs its own .frame and .cprestore directives. */
19035 mips_frame_reg_valid = 0;
19036 mips_cprestore_valid = 0;
19037
252b5132
RH
19038 cur_proc_ptr = &cur_proc;
19039 memset (cur_proc_ptr, '\0', sizeof (procS));
19040
9b2f1d35 19041 cur_proc_ptr->func_sym = symbolP;
252b5132 19042
f9419b05 19043 ++numprocs;
ecb4347a
DJ
19044
19045 if (debug_type == DEBUG_STABS)
19046 stabs_generate_asm_func (S_GET_NAME (symbolP),
19047 S_GET_NAME (symbolP));
252b5132
RH
19048 }
19049
7c0fc524
MR
19050 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19051
252b5132
RH
19052 demand_empty_rest_of_line ();
19053}
19054
19055/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19056 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19057 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19058 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19059 symbol table (in the mdebug section). */
19060
19061static void
17a2f251 19062s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19063{
ecb4347a 19064#ifdef OBJ_ELF
f43abd2b 19065 if (IS_ELF && !ECOFF_DEBUGGING)
ecb4347a
DJ
19066 {
19067 long val;
252b5132 19068
ecb4347a
DJ
19069 if (cur_proc_ptr == (procS *) NULL)
19070 {
19071 as_warn (_(".frame outside of .ent"));
19072 demand_empty_rest_of_line ();
19073 return;
19074 }
252b5132 19075
ecb4347a
DJ
19076 cur_proc_ptr->frame_reg = tc_get_register (1);
19077
19078 SKIP_WHITESPACE ();
19079 if (*input_line_pointer++ != ','
19080 || get_absolute_expression_and_terminator (&val) != ',')
19081 {
19082 as_warn (_("Bad .frame directive"));
19083 --input_line_pointer;
19084 demand_empty_rest_of_line ();
19085 return;
19086 }
252b5132 19087
ecb4347a
DJ
19088 cur_proc_ptr->frame_offset = val;
19089 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19090
252b5132 19091 demand_empty_rest_of_line ();
252b5132 19092 }
ecb4347a
DJ
19093 else
19094#endif /* OBJ_ELF */
19095 s_ignore (ignore);
252b5132
RH
19096}
19097
bdaaa2e1
KH
19098/* The .fmask and .mask directives. If the mdebug section is present
19099 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19100 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19101 information correctly. We can't use the ecoff routines because they
252b5132
RH
19102 make reference to the ecoff symbol table (in the mdebug section). */
19103
19104static void
17a2f251 19105s_mips_mask (int reg_type)
252b5132 19106{
ecb4347a 19107#ifdef OBJ_ELF
f43abd2b 19108 if (IS_ELF && !ECOFF_DEBUGGING)
252b5132 19109 {
ecb4347a 19110 long mask, off;
252b5132 19111
ecb4347a
DJ
19112 if (cur_proc_ptr == (procS *) NULL)
19113 {
19114 as_warn (_(".mask/.fmask outside of .ent"));
19115 demand_empty_rest_of_line ();
19116 return;
19117 }
252b5132 19118
ecb4347a
DJ
19119 if (get_absolute_expression_and_terminator (&mask) != ',')
19120 {
19121 as_warn (_("Bad .mask/.fmask directive"));
19122 --input_line_pointer;
19123 demand_empty_rest_of_line ();
19124 return;
19125 }
252b5132 19126
ecb4347a
DJ
19127 off = get_absolute_expression ();
19128
19129 if (reg_type == 'F')
19130 {
19131 cur_proc_ptr->fpreg_mask = mask;
19132 cur_proc_ptr->fpreg_offset = off;
19133 }
19134 else
19135 {
19136 cur_proc_ptr->reg_mask = mask;
19137 cur_proc_ptr->reg_offset = off;
19138 }
19139
19140 demand_empty_rest_of_line ();
252b5132
RH
19141 }
19142 else
ecb4347a
DJ
19143#endif /* OBJ_ELF */
19144 s_ignore (reg_type);
252b5132
RH
19145}
19146
316f5878
RS
19147/* A table describing all the processors gas knows about. Names are
19148 matched in the order listed.
e7af610e 19149
316f5878
RS
19150 To ease comparison, please keep this table in the same order as
19151 gcc's mips_cpu_info_table[]. */
e972090a
NC
19152static const struct mips_cpu_info mips_cpu_info_table[] =
19153{
316f5878 19154 /* Entries for generic ISAs */
ad3fea08
TS
19155 { "mips1", MIPS_CPU_IS_ISA, ISA_MIPS1, CPU_R3000 },
19156 { "mips2", MIPS_CPU_IS_ISA, ISA_MIPS2, CPU_R6000 },
19157 { "mips3", MIPS_CPU_IS_ISA, ISA_MIPS3, CPU_R4000 },
19158 { "mips4", MIPS_CPU_IS_ISA, ISA_MIPS4, CPU_R8000 },
19159 { "mips5", MIPS_CPU_IS_ISA, ISA_MIPS5, CPU_MIPS5 },
19160 { "mips32", MIPS_CPU_IS_ISA, ISA_MIPS32, CPU_MIPS32 },
19161 { "mips32r2", MIPS_CPU_IS_ISA, ISA_MIPS32R2, CPU_MIPS32R2 },
19162 { "mips64", MIPS_CPU_IS_ISA, ISA_MIPS64, CPU_MIPS64 },
19163 { "mips64r2", MIPS_CPU_IS_ISA, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
19164
19165 /* MIPS I */
ad3fea08
TS
19166 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
19167 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
19168 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19169
19170 /* MIPS II */
ad3fea08 19171 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19172
19173 /* MIPS III */
ad3fea08
TS
19174 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
19175 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
19176 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
19177 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
19178 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
19179 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
19180 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
19181 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
19182 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
19183 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
19184 { "orion", 0, ISA_MIPS3, CPU_R4600 },
19185 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
e407c74b 19186 { "r5900", 0, ISA_MIPS3, CPU_R5900 },
b15591bb
AN
19187 /* ST Microelectronics Loongson 2E and 2F cores */
19188 { "loongson2e", 0, ISA_MIPS3, CPU_LOONGSON_2E },
19189 { "loongson2f", 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19190
19191 /* MIPS IV */
ad3fea08
TS
19192 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
19193 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
19194 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
3aa3176b
TS
19195 { "r14000", 0, ISA_MIPS4, CPU_R14000 },
19196 { "r16000", 0, ISA_MIPS4, CPU_R16000 },
ad3fea08
TS
19197 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
19198 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
19199 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
19200 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
19201 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
19202 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
19203 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
19204 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
19205 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
19206 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19207
19208 /* MIPS 32 */
ad3fea08
TS
19209 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
19210 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
19211 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
19212 { "4ksc", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
19213
19214 /* MIPS 32 Release 2 */
19215 { "4kec", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19216 { "4kem", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19217 { "4kep", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19218 { "4ksd", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19219 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19220 { "m4kp", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
b5503c7b
MR
19221 { "m14k", MIPS_CPU_ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19222 { "m14kc", MIPS_CPU_ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
7a795ef4
MR
19223 { "m14ke", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19224 ISA_MIPS32R2, CPU_MIPS32R2 },
19225 { "m14kec", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19226 ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19227 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19228 { "24kf2_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19229 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19230 { "24kf1_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19231 /* Deprecated forms of the above. */
19232 { "24kfx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19233 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19234 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
ad3fea08 19235 { "24kec", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19236 { "24kef2_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19237 { "24kef", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19238 { "24kef1_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19239 /* Deprecated forms of the above. */
19240 { "24kefx", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
65263ce3 19241 { "24kex", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19242 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
a360e743
TS
19243 { "34kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19244 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19245 { "34kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19246 ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
19247 { "34kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19248 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19249 { "34kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19250 ISA_MIPS32R2, CPU_MIPS32R2 },
19251 /* Deprecated forms of the above. */
19252 { "34kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19253 ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
19254 { "34kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19255 ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4
SL
19256 /* 34Kn is a 34kc without DSP. */
19257 { "34kn", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19258 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
19259 { "74kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19260 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19261 { "74kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19262 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19263 { "74kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19264 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19265 { "74kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19266 ISA_MIPS32R2, CPU_MIPS32R2 },
19267 { "74kf3_2", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19268 ISA_MIPS32R2, CPU_MIPS32R2 },
19269 /* Deprecated forms of the above. */
19270 { "74kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19271 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19272 { "74kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19273 ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a
SL
19274 /* 1004K cores are multiprocessor versions of the 34K. */
19275 { "1004kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19276 ISA_MIPS32R2, CPU_MIPS32R2 },
19277 { "1004kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19278 ISA_MIPS32R2, CPU_MIPS32R2 },
19279 { "1004kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19280 ISA_MIPS32R2, CPU_MIPS32R2 },
19281 { "1004kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19282 ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 19283
316f5878 19284 /* MIPS 64 */
ad3fea08
TS
19285 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
19286 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
19287 { "20kc", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
7764b395 19288 { "25kf", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19289
c7a23324 19290 /* Broadcom SB-1 CPU core */
65263ce3
TS
19291 { "sb1", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19292 ISA_MIPS64, CPU_SB1 },
1e85aad8
JW
19293 /* Broadcom SB-1A CPU core */
19294 { "sb1a", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19295 ISA_MIPS64, CPU_SB1 },
d051516a
NC
19296
19297 { "loongson3a", 0, ISA_MIPS64, CPU_LOONGSON_3A },
e7af610e 19298
ed163775
MR
19299 /* MIPS 64 Release 2 */
19300
967344c6
AN
19301 /* Cavium Networks Octeon CPU core */
19302 { "octeon", 0, ISA_MIPS64R2, CPU_OCTEON },
dd6a37e7 19303 { "octeon+", 0, ISA_MIPS64R2, CPU_OCTEONP },
432233b3 19304 { "octeon2", 0, ISA_MIPS64R2, CPU_OCTEON2 },
967344c6 19305
52b6b6b9
JM
19306 /* RMI Xlr */
19307 { "xlr", 0, ISA_MIPS64, CPU_XLR },
19308
55a36193
MK
19309 /* Broadcom XLP.
19310 XLP is mostly like XLR, with the prominent exception that it is
19311 MIPS64R2 rather than MIPS64. */
19312 { "xlp", 0, ISA_MIPS64R2, CPU_XLR },
19313
316f5878
RS
19314 /* End marker */
19315 { NULL, 0, 0, 0 }
19316};
e7af610e 19317
84ea6cf2 19318
316f5878
RS
19319/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19320 with a final "000" replaced by "k". Ignore case.
e7af610e 19321
316f5878 19322 Note: this function is shared between GCC and GAS. */
c6c98b38 19323
b34976b6 19324static bfd_boolean
17a2f251 19325mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19326{
19327 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19328 given++, canonical++;
19329
19330 return ((*given == 0 && *canonical == 0)
19331 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19332}
19333
19334
19335/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19336 CPU name. We've traditionally allowed a lot of variation here.
19337
19338 Note: this function is shared between GCC and GAS. */
19339
b34976b6 19340static bfd_boolean
17a2f251 19341mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19342{
19343 /* First see if the name matches exactly, or with a final "000"
19344 turned into "k". */
19345 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19346 return TRUE;
316f5878
RS
19347
19348 /* If not, try comparing based on numerical designation alone.
19349 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19350 if (TOLOWER (*given) == 'r')
19351 given++;
19352 if (!ISDIGIT (*given))
b34976b6 19353 return FALSE;
316f5878
RS
19354
19355 /* Skip over some well-known prefixes in the canonical name,
19356 hoping to find a number there too. */
19357 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19358 canonical += 2;
19359 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19360 canonical += 2;
19361 else if (TOLOWER (canonical[0]) == 'r')
19362 canonical += 1;
19363
19364 return mips_strict_matching_cpu_name_p (canonical, given);
19365}
19366
19367
19368/* Parse an option that takes the name of a processor as its argument.
19369 OPTION is the name of the option and CPU_STRING is the argument.
19370 Return the corresponding processor enumeration if the CPU_STRING is
19371 recognized, otherwise report an error and return null.
19372
19373 A similar function exists in GCC. */
e7af610e
NC
19374
19375static const struct mips_cpu_info *
17a2f251 19376mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19377{
316f5878 19378 const struct mips_cpu_info *p;
e7af610e 19379
316f5878
RS
19380 /* 'from-abi' selects the most compatible architecture for the given
19381 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19382 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19383 version. Look first at the -mgp options, if given, otherwise base
19384 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19385
316f5878
RS
19386 Treat NO_ABI like the EABIs. One reason to do this is that the
19387 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19388 architecture. This code picks MIPS I for 'mips' and MIPS III for
19389 'mips64', just as we did in the days before 'from-abi'. */
19390 if (strcasecmp (cpu_string, "from-abi") == 0)
19391 {
19392 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19393 return mips_cpu_info_from_isa (ISA_MIPS1);
19394
19395 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19396 return mips_cpu_info_from_isa (ISA_MIPS3);
19397
19398 if (file_mips_gp32 >= 0)
19399 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19400
19401 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19402 ? ISA_MIPS3
19403 : ISA_MIPS1);
19404 }
19405
19406 /* 'default' has traditionally been a no-op. Probably not very useful. */
19407 if (strcasecmp (cpu_string, "default") == 0)
19408 return 0;
19409
19410 for (p = mips_cpu_info_table; p->name != 0; p++)
19411 if (mips_matching_cpu_name_p (p->name, cpu_string))
19412 return p;
19413
20203fb9 19414 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
316f5878 19415 return 0;
e7af610e
NC
19416}
19417
316f5878
RS
19418/* Return the canonical processor information for ISA (a member of the
19419 ISA_MIPS* enumeration). */
19420
e7af610e 19421static const struct mips_cpu_info *
17a2f251 19422mips_cpu_info_from_isa (int isa)
e7af610e
NC
19423{
19424 int i;
19425
19426 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19427 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19428 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19429 return (&mips_cpu_info_table[i]);
19430
e972090a 19431 return NULL;
e7af610e 19432}
fef14a42
TS
19433
19434static const struct mips_cpu_info *
17a2f251 19435mips_cpu_info_from_arch (int arch)
fef14a42
TS
19436{
19437 int i;
19438
19439 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19440 if (arch == mips_cpu_info_table[i].cpu)
19441 return (&mips_cpu_info_table[i]);
19442
19443 return NULL;
19444}
316f5878
RS
19445\f
19446static void
17a2f251 19447show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19448{
19449 if (*first_p)
19450 {
19451 fprintf (stream, "%24s", "");
19452 *col_p = 24;
19453 }
19454 else
19455 {
19456 fprintf (stream, ", ");
19457 *col_p += 2;
19458 }
e7af610e 19459
316f5878
RS
19460 if (*col_p + strlen (string) > 72)
19461 {
19462 fprintf (stream, "\n%24s", "");
19463 *col_p = 24;
19464 }
19465
19466 fprintf (stream, "%s", string);
19467 *col_p += strlen (string);
19468
19469 *first_p = 0;
19470}
19471
19472void
17a2f251 19473md_show_usage (FILE *stream)
e7af610e 19474{
316f5878
RS
19475 int column, first;
19476 size_t i;
19477
19478 fprintf (stream, _("\
19479MIPS options:\n\
316f5878
RS
19480-EB generate big endian output\n\
19481-EL generate little endian output\n\
19482-g, -g2 do not remove unneeded NOPs or swap branches\n\
19483-G NUM allow referencing objects up to NUM bytes\n\
19484 implicitly with the gp register [default 8]\n"));
19485 fprintf (stream, _("\
19486-mips1 generate MIPS ISA I instructions\n\
19487-mips2 generate MIPS ISA II instructions\n\
19488-mips3 generate MIPS ISA III instructions\n\
19489-mips4 generate MIPS ISA IV instructions\n\
19490-mips5 generate MIPS ISA V instructions\n\
19491-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 19492-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 19493-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 19494-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
19495-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19496
19497 first = 1;
e7af610e
NC
19498
19499 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
19500 show (stream, mips_cpu_info_table[i].name, &column, &first);
19501 show (stream, "from-abi", &column, &first);
19502 fputc ('\n', stream);
e7af610e 19503
316f5878
RS
19504 fprintf (stream, _("\
19505-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19506-no-mCPU don't generate code specific to CPU.\n\
19507 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19508
19509 first = 1;
19510
19511 show (stream, "3900", &column, &first);
19512 show (stream, "4010", &column, &first);
19513 show (stream, "4100", &column, &first);
19514 show (stream, "4650", &column, &first);
19515 fputc ('\n', stream);
19516
19517 fprintf (stream, _("\
19518-mips16 generate mips16 instructions\n\
19519-no-mips16 do not generate mips16 instructions\n"));
19520 fprintf (stream, _("\
df58fc94
RS
19521-mmicromips generate microMIPS instructions\n\
19522-mno-micromips do not generate microMIPS instructions\n"));
19523 fprintf (stream, _("\
e16bfa71
TS
19524-msmartmips generate smartmips instructions\n\
19525-mno-smartmips do not generate smartmips instructions\n"));
19526 fprintf (stream, _("\
74cd071d
CF
19527-mdsp generate DSP instructions\n\
19528-mno-dsp do not generate DSP instructions\n"));
19529 fprintf (stream, _("\
8b082fb1
TS
19530-mdspr2 generate DSP R2 instructions\n\
19531-mno-dspr2 do not generate DSP R2 instructions\n"));
19532 fprintf (stream, _("\
ef2e4d86
CF
19533-mmt generate MT instructions\n\
19534-mno-mt do not generate MT instructions\n"));
19535 fprintf (stream, _("\
dec0624d
MR
19536-mmcu generate MCU instructions\n\
19537-mno-mcu do not generate MCU instructions\n"));
19538 fprintf (stream, _("\
c67a084a
NC
19539-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19540-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 19541-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 19542-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 19543-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 19544-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
19545-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19546-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 19547-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
19548-O0 remove unneeded NOPs, do not swap branches\n\
19549-O remove unneeded NOPs and swap branches\n\
316f5878
RS
19550--trap, --no-break trap exception on div by 0 and mult overflow\n\
19551--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
19552 fprintf (stream, _("\
19553-mhard-float allow floating-point instructions\n\
19554-msoft-float do not allow floating-point instructions\n\
19555-msingle-float only allow 32-bit floating-point operations\n\
19556-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
19557--[no-]construct-floats [dis]allow floating point values to be constructed\n"
19558 ));
316f5878
RS
19559#ifdef OBJ_ELF
19560 fprintf (stream, _("\
19561-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 19562-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 19563-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 19564-non_shared do not generate code that can operate with DSOs\n\
316f5878 19565-xgot assume a 32 bit GOT\n\
dcd410fe 19566-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 19567-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 19568 position dependent (non shared) code\n\
316f5878
RS
19569-mabi=ABI create ABI conformant object file for:\n"));
19570
19571 first = 1;
19572
19573 show (stream, "32", &column, &first);
19574 show (stream, "o64", &column, &first);
19575 show (stream, "n32", &column, &first);
19576 show (stream, "64", &column, &first);
19577 show (stream, "eabi", &column, &first);
19578
19579 fputc ('\n', stream);
19580
19581 fprintf (stream, _("\
19582-32 create o32 ABI object file (default)\n\
19583-n32 create n32 ABI object file\n\
19584-64 create 64 ABI object file\n"));
19585#endif
e7af610e 19586}
14e777e0 19587
1575952e 19588#ifdef TE_IRIX
14e777e0 19589enum dwarf2_format
413a266c 19590mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 19591{
369943fe 19592 if (HAVE_64BIT_SYMBOLS)
1575952e 19593 return dwarf2_format_64bit_irix;
14e777e0
KB
19594 else
19595 return dwarf2_format_32bit;
19596}
1575952e 19597#endif
73369e65
EC
19598
19599int
19600mips_dwarf2_addr_size (void)
19601{
6b6b3450 19602 if (HAVE_64BIT_OBJECTS)
73369e65 19603 return 8;
73369e65
EC
19604 else
19605 return 4;
19606}
5862107c
EC
19607
19608/* Standard calling conventions leave the CFA at SP on entry. */
19609void
19610mips_cfi_frame_initial_instructions (void)
19611{
19612 cfi_add_CFA_def_cfa_register (SP);
19613}
19614
707bfff6
TS
19615int
19616tc_mips_regname_to_dw2regnum (char *regname)
19617{
19618 unsigned int regnum = -1;
19619 unsigned int reg;
19620
19621 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19622 regnum = reg;
19623
19624 return regnum;
19625}
This page took 2.471866 seconds and 4 git commands to generate.