* elfxx-mips.c (mips_elf_calculate_relocation): Fix the handling
[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,
45dfa85a 3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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
148 /* True if this is a mips16 instruction and if we want the extended
149 form of INSN_MO. */
150 bfd_boolean use_extend;
151
152 /* The 16-bit extension instruction to use when USE_EXTEND is true. */
153 unsigned short extend;
154
155 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
156 a copy of INSN_MO->match with the operands filled in. */
157 unsigned long insn_opcode;
158
159 /* The frag that contains the instruction. */
160 struct frag *frag;
161
162 /* The offset into FRAG of the first instruction byte. */
163 long where;
164
165 /* The relocs associated with the instruction, if any. */
166 fixS *fixp[3];
167
a38419a5
RS
168 /* True if this entry cannot be moved from its current position. */
169 unsigned int fixed_p : 1;
47e39b9d 170
708587a4 171 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
172 unsigned int noreorder_p : 1;
173
2fa15973
RS
174 /* True for mips16 instructions that jump to an absolute address. */
175 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
176
177 /* True if this instruction is complete. */
178 unsigned int complete_p : 1;
47e39b9d
RS
179};
180
a325df1d
TS
181/* The ABI to use. */
182enum mips_abi_level
183{
184 NO_ABI = 0,
185 O32_ABI,
186 O64_ABI,
187 N32_ABI,
188 N64_ABI,
189 EABI_ABI
190};
191
192/* MIPS ABI we are using for this output file. */
316f5878 193static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 194
143d77c5
EC
195/* Whether or not we have code that can call pic code. */
196int mips_abicalls = FALSE;
197
aa6975fb
ILT
198/* Whether or not we have code which can be put into a shared
199 library. */
200static bfd_boolean mips_in_shared = TRUE;
201
252b5132
RH
202/* This is the set of options which may be modified by the .set
203 pseudo-op. We use a struct so that .set push and .set pop are more
204 reliable. */
205
e972090a
NC
206struct mips_set_options
207{
252b5132
RH
208 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
209 if it has not been initialized. Changed by `.set mipsN', and the
210 -mipsN command line option, and the default CPU. */
211 int isa;
1f25f5d3
CD
212 /* Enabled Application Specific Extensions (ASEs). These are set to -1
213 if they have not been initialized. Changed by `.set <asename>', by
214 command line options, and based on the default architecture. */
215 int ase_mips3d;
deec1734 216 int ase_mdmx;
e16bfa71 217 int ase_smartmips;
74cd071d 218 int ase_dsp;
8b082fb1 219 int ase_dspr2;
ef2e4d86 220 int ase_mt;
dec0624d 221 int ase_mcu;
252b5132
RH
222 /* Whether we are assembling for the mips16 processor. 0 if we are
223 not, 1 if we are, and -1 if the value has not been initialized.
224 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
225 -nomips16 command line options, and the default CPU. */
226 int mips16;
df58fc94
RS
227 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
228 1 if we are, and -1 if the value has not been initialized. Changed
229 by `.set micromips' and `.set nomicromips', and the -mmicromips
230 and -mno-micromips command line options, and the default CPU. */
231 int micromips;
252b5132
RH
232 /* Non-zero if we should not reorder instructions. Changed by `.set
233 reorder' and `.set noreorder'. */
234 int noreorder;
741fe287
MR
235 /* Non-zero if we should not permit the register designated "assembler
236 temporary" to be used in instructions. The value is the register
237 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
238 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
239 unsigned int at;
252b5132
RH
240 /* Non-zero if we should warn when a macro instruction expands into
241 more than one machine instruction. Changed by `.set nomacro' and
242 `.set macro'. */
243 int warn_about_macros;
244 /* Non-zero if we should not move instructions. Changed by `.set
245 move', `.set volatile', `.set nomove', and `.set novolatile'. */
246 int nomove;
247 /* Non-zero if we should not optimize branches by moving the target
248 of the branch into the delay slot. Actually, we don't perform
249 this optimization anyhow. Changed by `.set bopt' and `.set
250 nobopt'. */
251 int nobopt;
252 /* Non-zero if we should not autoextend mips16 instructions.
253 Changed by `.set autoextend' and `.set noautoextend'. */
254 int noautoextend;
a325df1d
TS
255 /* Restrict general purpose registers and floating point registers
256 to 32 bit. This is initially determined when -mgp32 or -mfp32
257 is passed but can changed if the assembler code uses .set mipsN. */
258 int gp32;
259 int fp32;
fef14a42
TS
260 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
261 command line option, and the default CPU. */
262 int arch;
aed1a261
RS
263 /* True if ".set sym32" is in effect. */
264 bfd_boolean sym32;
037b32b9
AN
265 /* True if floating-point operations are not allowed. Changed by .set
266 softfloat or .set hardfloat, by command line options -msoft-float or
267 -mhard-float. The default is false. */
268 bfd_boolean soft_float;
269
270 /* True if only single-precision floating-point operations are allowed.
271 Changed by .set singlefloat or .set doublefloat, command-line options
272 -msingle-float or -mdouble-float. The default is false. */
273 bfd_boolean single_float;
252b5132
RH
274};
275
037b32b9
AN
276/* This is the struct we use to hold the current set of options. Note
277 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
278 -1 to indicate that they have not been initialized. */
279
a325df1d 280/* True if -mgp32 was passed. */
a8e8e863 281static int file_mips_gp32 = -1;
a325df1d
TS
282
283/* True if -mfp32 was passed. */
a8e8e863 284static int file_mips_fp32 = -1;
a325df1d 285
037b32b9
AN
286/* 1 if -msoft-float, 0 if -mhard-float. The default is 0. */
287static int file_mips_soft_float = 0;
288
289/* 1 if -msingle-float, 0 if -mdouble-float. The default is 0. */
290static int file_mips_single_float = 0;
252b5132 291
e972090a
NC
292static struct mips_set_options mips_opts =
293{
037b32b9
AN
294 /* isa */ ISA_UNKNOWN, /* ase_mips3d */ -1, /* ase_mdmx */ -1,
295 /* ase_smartmips */ 0, /* ase_dsp */ -1, /* ase_dspr2 */ -1, /* ase_mt */ -1,
dec0624d
MR
296 /* ase_mcu */ -1, /* mips16 */ -1, /* micromips */ -1, /* noreorder */ 0,
297 /* at */ ATREG, /* warn_about_macros */ 0, /* nomove */ 0, /* nobopt */ 0,
037b32b9
AN
298 /* noautoextend */ 0, /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN,
299 /* sym32 */ FALSE, /* soft_float */ FALSE, /* single_float */ FALSE
e7af610e 300};
252b5132
RH
301
302/* These variables are filled in with the masks of registers used.
303 The object format code reads them and puts them in the appropriate
304 place. */
305unsigned long mips_gprmask;
306unsigned long mips_cprmask[4];
307
308/* MIPS ISA we are using for this output file. */
e7af610e 309static int file_mips_isa = ISA_UNKNOWN;
252b5132 310
738f4d98 311/* True if any MIPS16 code was produced. */
a4672219
TS
312static int file_ase_mips16;
313
3994f87e
TS
314#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
315 || mips_opts.isa == ISA_MIPS32R2 \
316 || mips_opts.isa == ISA_MIPS64 \
317 || mips_opts.isa == ISA_MIPS64R2)
318
df58fc94
RS
319/* True if any microMIPS code was produced. */
320static int file_ase_micromips;
321
b12dd2e4
CF
322/* True if we want to create R_MIPS_JALR for jalr $25. */
323#ifdef TE_IRIX
1180b5a4 324#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 325#else
1180b5a4
RS
326/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
327 because there's no place for any addend, the only acceptable
328 expression is a bare symbol. */
329#define MIPS_JALR_HINT_P(EXPR) \
330 (!HAVE_IN_PLACE_ADDENDS \
331 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
332#endif
333
1f25f5d3
CD
334/* True if -mips3d was passed or implied by arguments passed on the
335 command line (e.g., by -march). */
336static int file_ase_mips3d;
337
deec1734
CD
338/* True if -mdmx was passed or implied by arguments passed on the
339 command line (e.g., by -march). */
340static int file_ase_mdmx;
341
e16bfa71
TS
342/* True if -msmartmips was passed or implied by arguments passed on the
343 command line (e.g., by -march). */
344static int file_ase_smartmips;
345
ad3fea08
TS
346#define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32 \
347 || mips_opts.isa == ISA_MIPS32R2)
e16bfa71 348
74cd071d
CF
349/* True if -mdsp was passed or implied by arguments passed on the
350 command line (e.g., by -march). */
351static int file_ase_dsp;
352
ad3fea08 353#define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2 \
03f66e8a
MR
354 || mips_opts.isa == ISA_MIPS64R2 \
355 || mips_opts.micromips)
ad3fea08 356
65263ce3
TS
357#define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
358
8b082fb1
TS
359/* True if -mdspr2 was passed or implied by arguments passed on the
360 command line (e.g., by -march). */
361static int file_ase_dspr2;
362
363#define ISA_SUPPORTS_DSPR2_ASE (mips_opts.isa == ISA_MIPS32R2 \
03f66e8a
MR
364 || mips_opts.isa == ISA_MIPS64R2 \
365 || mips_opts.micromips)
8b082fb1 366
ef2e4d86
CF
367/* True if -mmt was passed or implied by arguments passed on the
368 command line (e.g., by -march). */
369static int file_ase_mt;
370
ad3fea08
TS
371#define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2 \
372 || mips_opts.isa == ISA_MIPS64R2)
373
dec0624d 374#define ISA_SUPPORTS_MCU_ASE (mips_opts.isa == ISA_MIPS32R2 \
9ddc84cc
MR
375 || mips_opts.isa == ISA_MIPS64R2 \
376 || mips_opts.micromips)
dec0624d 377
ec68c924 378/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 379static int file_mips_arch = CPU_UNKNOWN;
316f5878 380static const char *mips_arch_string;
ec68c924
EC
381
382/* The argument of the -mtune= flag. The architecture for which we
383 are optimizing. */
384static int mips_tune = CPU_UNKNOWN;
316f5878 385static const char *mips_tune_string;
ec68c924 386
316f5878 387/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
388static int mips_32bitmode = 0;
389
316f5878
RS
390/* True if the given ABI requires 32-bit registers. */
391#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
392
393/* Likewise 64-bit registers. */
707bfff6
TS
394#define ABI_NEEDS_64BIT_REGS(ABI) \
395 ((ABI) == N32_ABI \
396 || (ABI) == N64_ABI \
316f5878
RS
397 || (ABI) == O64_ABI)
398
ad3fea08 399/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
400#define ISA_HAS_64BIT_REGS(ISA) \
401 ((ISA) == ISA_MIPS3 \
402 || (ISA) == ISA_MIPS4 \
403 || (ISA) == ISA_MIPS5 \
404 || (ISA) == ISA_MIPS64 \
405 || (ISA) == ISA_MIPS64R2)
9ce8a5dd 406
ad3fea08
TS
407/* Return true if ISA supports 64 bit wide float registers. */
408#define ISA_HAS_64BIT_FPRS(ISA) \
409 ((ISA) == ISA_MIPS3 \
410 || (ISA) == ISA_MIPS4 \
411 || (ISA) == ISA_MIPS5 \
412 || (ISA) == ISA_MIPS32R2 \
413 || (ISA) == ISA_MIPS64 \
414 || (ISA) == ISA_MIPS64R2)
415
af7ee8bf
CD
416/* Return true if ISA supports 64-bit right rotate (dror et al.)
417 instructions. */
707bfff6 418#define ISA_HAS_DROR(ISA) \
df58fc94
RS
419 ((ISA) == ISA_MIPS64R2 \
420 || (mips_opts.micromips \
421 && ISA_HAS_64BIT_REGS (ISA)) \
422 )
af7ee8bf
CD
423
424/* Return true if ISA supports 32-bit right rotate (ror et al.)
425 instructions. */
707bfff6
TS
426#define ISA_HAS_ROR(ISA) \
427 ((ISA) == ISA_MIPS32R2 \
428 || (ISA) == ISA_MIPS64R2 \
df58fc94
RS
429 || mips_opts.ase_smartmips \
430 || mips_opts.micromips \
431 )
707bfff6 432
7455baf8
TS
433/* Return true if ISA supports single-precision floats in odd registers. */
434#define ISA_HAS_ODD_SINGLE_FPR(ISA) \
435 ((ISA) == ISA_MIPS32 \
436 || (ISA) == ISA_MIPS32R2 \
437 || (ISA) == ISA_MIPS64 \
438 || (ISA) == ISA_MIPS64R2)
af7ee8bf 439
ad3fea08
TS
440/* Return true if ISA supports move to/from high part of a 64-bit
441 floating-point register. */
442#define ISA_HAS_MXHC1(ISA) \
443 ((ISA) == ISA_MIPS32R2 \
444 || (ISA) == ISA_MIPS64R2)
445
e013f690 446#define HAVE_32BIT_GPRS \
ad3fea08 447 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 448
e013f690 449#define HAVE_32BIT_FPRS \
ad3fea08 450 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
ca4e0257 451
ad3fea08
TS
452#define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
453#define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
ca4e0257 454
316f5878 455#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 456
316f5878 457#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 458
3b91255e
RS
459/* True if relocations are stored in-place. */
460#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
461
aed1a261
RS
462/* The ABI-derived address size. */
463#define HAVE_64BIT_ADDRESSES \
464 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
465#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 466
aed1a261
RS
467/* The size of symbolic constants (i.e., expressions of the form
468 "SYMBOL" or "SYMBOL + OFFSET"). */
469#define HAVE_32BIT_SYMBOLS \
470 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
471#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 472
b7c7d6c1
TS
473/* Addresses are loaded in different ways, depending on the address size
474 in use. The n32 ABI Documentation also mandates the use of additions
475 with overflow checking, but existing implementations don't follow it. */
f899b4b8 476#define ADDRESS_ADD_INSN \
b7c7d6c1 477 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
478
479#define ADDRESS_ADDI_INSN \
b7c7d6c1 480 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
481
482#define ADDRESS_LOAD_INSN \
483 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
484
485#define ADDRESS_STORE_INSN \
486 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
487
a4672219 488/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
489#define CPU_HAS_MIPS16(cpu) \
490 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
491 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 492
2309ddf2 493/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
494#define CPU_HAS_MICROMIPS(cpu) 0
495
60b63b72
RS
496/* True if CPU has a dror instruction. */
497#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
498
499/* True if CPU has a ror instruction. */
500#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
501
dd6a37e7 502/* True if CPU is in the Octeon family */
432233b3 503#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
dd6a37e7 504
dd3cbb7e 505/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 506#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 507
b19e8a9b
AN
508/* True if CPU does not implement the all the coprocessor insns. For these
509 CPUs only those COP insns are accepted that are explicitly marked to be
510 available on the CPU. ISA membership for COP insns is ignored. */
dd6a37e7 511#define NO_ISA_COP(CPU) (CPU_IS_OCTEON (CPU))
b19e8a9b 512
c8978940
CD
513/* True if mflo and mfhi can be immediately followed by instructions
514 which write to the HI and LO registers.
515
516 According to MIPS specifications, MIPS ISAs I, II, and III need
517 (at least) two instructions between the reads of HI/LO and
518 instructions which write them, and later ISAs do not. Contradicting
519 the MIPS specifications, some MIPS IV processor user manuals (e.g.
520 the UM for the NEC Vr5000) document needing the instructions between
521 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
522 MIPS64 and later ISAs to have the interlocks, plus any specific
523 earlier-ISA CPUs for which CPU documentation declares that the
524 instructions are really interlocked. */
525#define hilo_interlocks \
526 (mips_opts.isa == ISA_MIPS32 \
527 || mips_opts.isa == ISA_MIPS32R2 \
528 || mips_opts.isa == ISA_MIPS64 \
529 || mips_opts.isa == ISA_MIPS64R2 \
530 || mips_opts.arch == CPU_R4010 \
531 || mips_opts.arch == CPU_R10000 \
532 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
533 || mips_opts.arch == CPU_R14000 \
534 || mips_opts.arch == CPU_R16000 \
c8978940 535 || mips_opts.arch == CPU_RM7000 \
c8978940 536 || mips_opts.arch == CPU_VR5500 \
df58fc94 537 || mips_opts.micromips \
c8978940 538 )
252b5132
RH
539
540/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
541 from the GPRs after they are loaded from memory, and thus does not
542 require nops to be inserted. This applies to instructions marked
543 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
df58fc94
RS
544 level I and microMIPS mode instructions are always interlocked. */
545#define gpr_interlocks \
546 (mips_opts.isa != ISA_MIPS1 \
547 || mips_opts.arch == CPU_R3900 \
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
583/* Returns true for a (non floating-point) coprocessor instruction. Reading
584 or writing the condition code is only possible on the coprocessors and
585 these insns are not marked with INSN_COP. Thus for these insns use the
a242dc0d 586 condition-code flags. */
b19e8a9b
AN
587#define COP_INSN(PINFO) \
588 (PINFO != INSN_MACRO \
a242dc0d
AN
589 && ((PINFO) & (FP_S | FP_D)) == 0 \
590 && ((PINFO) & (INSN_COP | INSN_READ_COND_CODE | INSN_WRITE_COND_CODE)))
6b76fefe 591
df58fc94
RS
592/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
593 has been selected. This implies, in particular, that addresses of text
594 labels have their LSB set. */
595#define HAVE_CODE_COMPRESSION \
596 ((mips_opts.mips16 | mips_opts.micromips) != 0)
597
252b5132
RH
598/* MIPS PIC level. */
599
a161fe53 600enum mips_pic_level mips_pic;
252b5132 601
c9914766 602/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 603 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 604static int mips_big_got = 0;
252b5132
RH
605
606/* 1 if trap instructions should used for overflow rather than break
607 instructions. */
c9914766 608static int mips_trap = 0;
252b5132 609
119d663a 610/* 1 if double width floating point constants should not be constructed
b6ff326e 611 by assembling two single width halves into two single width floating
119d663a
NC
612 point registers which just happen to alias the double width destination
613 register. On some architectures this aliasing can be disabled by a bit
d547a75e 614 in the status register, and the setting of this bit cannot be determined
119d663a
NC
615 automatically at assemble time. */
616static int mips_disable_float_construction;
617
252b5132
RH
618/* Non-zero if any .set noreorder directives were used. */
619
620static int mips_any_noreorder;
621
6b76fefe
CM
622/* Non-zero if nops should be inserted when the register referenced in
623 an mfhi/mflo instruction is read in the next two instructions. */
624static int mips_7000_hilo_fix;
625
02ffd3e4 626/* The size of objects in the small data section. */
156c2f8b 627static unsigned int g_switch_value = 8;
252b5132
RH
628/* Whether the -G option was used. */
629static int g_switch_seen = 0;
630
631#define N_RMASK 0xc4
632#define N_VFP 0xd4
633
634/* If we can determine in advance that GP optimization won't be
635 possible, we can skip the relaxation stuff that tries to produce
636 GP-relative references. This makes delay slot optimization work
637 better.
638
639 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
640 gcc output. It needs to guess right for gcc, otherwise gcc
641 will put what it thinks is a GP-relative instruction in a branch
642 delay slot.
252b5132
RH
643
644 I don't know if a fix is needed for the SVR4_PIC mode. I've only
645 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 646static int nopic_need_relax (symbolS *, int);
252b5132
RH
647
648/* handle of the OPCODE hash table */
649static struct hash_control *op_hash = NULL;
650
651/* The opcode hash table we use for the mips16. */
652static struct hash_control *mips16_op_hash = NULL;
653
df58fc94
RS
654/* The opcode hash table we use for the microMIPS ASE. */
655static struct hash_control *micromips_op_hash = NULL;
656
252b5132
RH
657/* This array holds the chars that always start a comment. If the
658 pre-processor is disabled, these aren't very useful */
659const char comment_chars[] = "#";
660
661/* This array holds the chars that only start a comment at the beginning of
662 a line. If the line seems to have the form '# 123 filename'
663 .line and .file directives will appear in the pre-processed output */
664/* Note that input_file.c hand checks for '#' at the beginning of the
665 first line of the input file. This is because the compiler outputs
bdaaa2e1 666 #NO_APP at the beginning of its output. */
252b5132
RH
667/* Also note that C style comments are always supported. */
668const char line_comment_chars[] = "#";
669
bdaaa2e1 670/* This array holds machine specific line separator characters. */
63a0b638 671const char line_separator_chars[] = ";";
252b5132
RH
672
673/* Chars that can be used to separate mant from exp in floating point nums */
674const char EXP_CHARS[] = "eE";
675
676/* Chars that mean this number is a floating point constant */
677/* As in 0f12.456 */
678/* or 0d1.2345e12 */
679const char FLT_CHARS[] = "rRsSfFdDxXpP";
680
681/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
682 changed in read.c . Ideally it shouldn't have to know about it at all,
683 but nothing is ideal around here.
684 */
685
686static char *insn_error;
687
688static int auto_align = 1;
689
690/* When outputting SVR4 PIC code, the assembler needs to know the
691 offset in the stack frame from which to restore the $gp register.
692 This is set by the .cprestore pseudo-op, and saved in this
693 variable. */
694static offsetT mips_cprestore_offset = -1;
695
67c1ffbe 696/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 697 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 698 offset and even an other register than $gp as global pointer. */
6478892d
TS
699static offsetT mips_cpreturn_offset = -1;
700static int mips_cpreturn_register = -1;
701static int mips_gp_register = GP;
def2e0dd 702static int mips_gprel_offset = 0;
6478892d 703
7a621144
DJ
704/* Whether mips_cprestore_offset has been set in the current function
705 (or whether it has already been warned about, if not). */
706static int mips_cprestore_valid = 0;
707
252b5132
RH
708/* This is the register which holds the stack frame, as set by the
709 .frame pseudo-op. This is needed to implement .cprestore. */
710static int mips_frame_reg = SP;
711
7a621144
DJ
712/* Whether mips_frame_reg has been set in the current function
713 (or whether it has already been warned about, if not). */
714static int mips_frame_reg_valid = 0;
715
252b5132
RH
716/* To output NOP instructions correctly, we need to keep information
717 about the previous two instructions. */
718
719/* Whether we are optimizing. The default value of 2 means to remove
720 unneeded NOPs and swap branch instructions when possible. A value
721 of 1 means to not swap branches. A value of 0 means to always
722 insert NOPs. */
723static int mips_optimize = 2;
724
725/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
726 equivalent to seeing no -g option at all. */
727static int mips_debug = 0;
728
7d8e00cf
RS
729/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
730#define MAX_VR4130_NOPS 4
731
732/* The maximum number of NOPs needed to fill delay slots. */
733#define MAX_DELAY_NOPS 2
734
735/* The maximum number of NOPs needed for any purpose. */
736#define MAX_NOPS 4
71400594
RS
737
738/* A list of previous instructions, with index 0 being the most recent.
739 We need to look back MAX_NOPS instructions when filling delay slots
740 or working around processor errata. We need to look back one
741 instruction further if we're thinking about using history[0] to
742 fill a branch delay slot. */
743static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 744
1e915849 745/* Nop instructions used by emit_nop. */
df58fc94
RS
746static struct mips_cl_insn nop_insn;
747static struct mips_cl_insn mips16_nop_insn;
748static struct mips_cl_insn micromips_nop16_insn;
749static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
750
751/* The appropriate nop for the current mode. */
df58fc94
RS
752#define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn \
753 : (mips_opts.micromips ? &micromips_nop16_insn : &nop_insn))
754
755/* The size of NOP_INSN in bytes. */
756#define NOP_INSN_SIZE (HAVE_CODE_COMPRESSION ? 2 : 4)
252b5132 757
252b5132
RH
758/* If this is set, it points to a frag holding nop instructions which
759 were inserted before the start of a noreorder section. If those
760 nops turn out to be unnecessary, the size of the frag can be
761 decreased. */
762static fragS *prev_nop_frag;
763
764/* The number of nop instructions we created in prev_nop_frag. */
765static int prev_nop_frag_holds;
766
767/* The number of nop instructions that we know we need in
bdaaa2e1 768 prev_nop_frag. */
252b5132
RH
769static int prev_nop_frag_required;
770
771/* The number of instructions we've seen since prev_nop_frag. */
772static int prev_nop_frag_since;
773
774/* For ECOFF and ELF, relocations against symbols are done in two
775 parts, with a HI relocation and a LO relocation. Each relocation
776 has only 16 bits of space to store an addend. This means that in
777 order for the linker to handle carries correctly, it must be able
778 to locate both the HI and the LO relocation. This means that the
779 relocations must appear in order in the relocation table.
780
781 In order to implement this, we keep track of each unmatched HI
782 relocation. We then sort them so that they immediately precede the
bdaaa2e1 783 corresponding LO relocation. */
252b5132 784
e972090a
NC
785struct mips_hi_fixup
786{
252b5132
RH
787 /* Next HI fixup. */
788 struct mips_hi_fixup *next;
789 /* This fixup. */
790 fixS *fixp;
791 /* The section this fixup is in. */
792 segT seg;
793};
794
795/* The list of unmatched HI relocs. */
796
797static struct mips_hi_fixup *mips_hi_fixup_list;
798
64bdfcaf
RS
799/* The frag containing the last explicit relocation operator.
800 Null if explicit relocations have not been used. */
801
802static fragS *prev_reloc_op_frag;
803
252b5132
RH
804/* Map normal MIPS register numbers to mips16 register numbers. */
805
806#define X ILLEGAL_REG
e972090a
NC
807static const int mips32_to_16_reg_map[] =
808{
252b5132
RH
809 X, X, 2, 3, 4, 5, 6, 7,
810 X, X, X, X, X, X, X, X,
811 0, 1, X, X, X, X, X, X,
812 X, X, X, X, X, X, X, X
813};
814#undef X
815
816/* Map mips16 register numbers to normal MIPS register numbers. */
817
e972090a
NC
818static const unsigned int mips16_to_32_reg_map[] =
819{
252b5132
RH
820 16, 17, 2, 3, 4, 5, 6, 7
821};
60b63b72 822
df58fc94
RS
823/* Map normal MIPS register numbers to microMIPS register numbers. */
824
825#define mips32_to_micromips_reg_b_map mips32_to_16_reg_map
826#define mips32_to_micromips_reg_c_map mips32_to_16_reg_map
827#define mips32_to_micromips_reg_d_map mips32_to_16_reg_map
828#define mips32_to_micromips_reg_e_map mips32_to_16_reg_map
829#define mips32_to_micromips_reg_f_map mips32_to_16_reg_map
830#define mips32_to_micromips_reg_g_map mips32_to_16_reg_map
831#define mips32_to_micromips_reg_l_map mips32_to_16_reg_map
832
833#define X ILLEGAL_REG
834/* reg type h: 4, 5, 6. */
835static const int mips32_to_micromips_reg_h_map[] =
836{
837 X, X, X, X, 4, 5, 6, X,
838 X, X, X, X, X, X, X, X,
839 X, X, X, X, X, X, X, X,
840 X, X, X, X, X, X, X, X
841};
842
843/* reg type m: 0, 17, 2, 3, 16, 18, 19, 20. */
844static const int mips32_to_micromips_reg_m_map[] =
845{
846 0, X, 2, 3, X, X, X, X,
847 X, X, X, X, X, X, X, X,
848 4, 1, 5, 6, 7, X, X, X,
849 X, X, X, X, X, X, X, X
850};
851
852/* reg type q: 0, 2-7. 17. */
853static const int mips32_to_micromips_reg_q_map[] =
854{
855 0, X, 2, 3, 4, 5, 6, 7,
856 X, X, X, X, X, X, X, X,
857 X, 1, X, X, X, X, X, X,
858 X, X, X, X, X, X, X, X
859};
860
861#define mips32_to_micromips_reg_n_map mips32_to_micromips_reg_m_map
862#undef X
863
864/* Map microMIPS register numbers to normal MIPS register numbers. */
865
866#define micromips_to_32_reg_b_map mips16_to_32_reg_map
867#define micromips_to_32_reg_c_map mips16_to_32_reg_map
868#define micromips_to_32_reg_d_map mips16_to_32_reg_map
869#define micromips_to_32_reg_e_map mips16_to_32_reg_map
870#define micromips_to_32_reg_f_map mips16_to_32_reg_map
871#define micromips_to_32_reg_g_map mips16_to_32_reg_map
872
873/* The microMIPS registers with type h. */
874static const unsigned int micromips_to_32_reg_h_map[] =
875{
876 5, 5, 6, 4, 4, 4, 4, 4
877};
878
879/* The microMIPS registers with type i. */
880static const unsigned int micromips_to_32_reg_i_map[] =
881{
882 6, 7, 7, 21, 22, 5, 6, 7
883};
884
885#define micromips_to_32_reg_l_map mips16_to_32_reg_map
886
887/* The microMIPS registers with type m. */
888static const unsigned int micromips_to_32_reg_m_map[] =
889{
890 0, 17, 2, 3, 16, 18, 19, 20
891};
892
893#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
894
895/* The microMIPS registers with type q. */
896static const unsigned int micromips_to_32_reg_q_map[] =
897{
898 0, 17, 2, 3, 4, 5, 6, 7
899};
900
901/* microMIPS imm type B. */
902static const int micromips_imm_b_map[] =
903{
904 1, 4, 8, 12, 16, 20, 24, -1
905};
906
907/* microMIPS imm type C. */
908static const int micromips_imm_c_map[] =
909{
910 128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
911};
912
71400594
RS
913/* Classifies the kind of instructions we're interested in when
914 implementing -mfix-vr4120. */
c67a084a
NC
915enum fix_vr4120_class
916{
71400594
RS
917 FIX_VR4120_MACC,
918 FIX_VR4120_DMACC,
919 FIX_VR4120_MULT,
920 FIX_VR4120_DMULT,
921 FIX_VR4120_DIV,
922 FIX_VR4120_MTHILO,
923 NUM_FIX_VR4120_CLASSES
924};
925
c67a084a
NC
926/* ...likewise -mfix-loongson2f-jump. */
927static bfd_boolean mips_fix_loongson2f_jump;
928
929/* ...likewise -mfix-loongson2f-nop. */
930static bfd_boolean mips_fix_loongson2f_nop;
931
932/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
933static bfd_boolean mips_fix_loongson2f;
934
71400594
RS
935/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
936 there must be at least one other instruction between an instruction
937 of type X and an instruction of type Y. */
938static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
939
940/* True if -mfix-vr4120 is in force. */
d766e8ec 941static int mips_fix_vr4120;
4a6a3df4 942
7d8e00cf
RS
943/* ...likewise -mfix-vr4130. */
944static int mips_fix_vr4130;
945
6a32d874
CM
946/* ...likewise -mfix-24k. */
947static int mips_fix_24k;
948
d954098f
DD
949/* ...likewise -mfix-cn63xxp1 */
950static bfd_boolean mips_fix_cn63xxp1;
951
4a6a3df4
AO
952/* We don't relax branches by default, since this causes us to expand
953 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
954 fail to compute the offset before expanding the macro to the most
955 efficient expansion. */
956
957static int mips_relax_branch;
252b5132 958\f
4d7206a2
RS
959/* The expansion of many macros depends on the type of symbol that
960 they refer to. For example, when generating position-dependent code,
961 a macro that refers to a symbol may have two different expansions,
962 one which uses GP-relative addresses and one which uses absolute
963 addresses. When generating SVR4-style PIC, a macro may have
964 different expansions for local and global symbols.
965
966 We handle these situations by generating both sequences and putting
967 them in variant frags. In position-dependent code, the first sequence
968 will be the GP-relative one and the second sequence will be the
969 absolute one. In SVR4 PIC, the first sequence will be for global
970 symbols and the second will be for local symbols.
971
584892a6
RS
972 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
973 SECOND are the lengths of the two sequences in bytes. These fields
974 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
975 the subtype has the following flags:
4d7206a2 976
584892a6
RS
977 RELAX_USE_SECOND
978 Set if it has been decided that we should use the second
979 sequence instead of the first.
980
981 RELAX_SECOND_LONGER
982 Set in the first variant frag if the macro's second implementation
983 is longer than its first. This refers to the macro as a whole,
984 not an individual relaxation.
985
986 RELAX_NOMACRO
987 Set in the first variant frag if the macro appeared in a .set nomacro
988 block and if one alternative requires a warning but the other does not.
989
990 RELAX_DELAY_SLOT
991 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
992 delay slot.
4d7206a2 993
df58fc94
RS
994 RELAX_DELAY_SLOT_16BIT
995 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
996 16-bit instruction.
997
998 RELAX_DELAY_SLOT_SIZE_FIRST
999 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
1000 the macro is of the wrong size for the branch delay slot.
1001
1002 RELAX_DELAY_SLOT_SIZE_SECOND
1003 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1004 the macro is of the wrong size for the branch delay slot.
1005
4d7206a2
RS
1006 The frag's "opcode" points to the first fixup for relaxable code.
1007
1008 Relaxable macros are generated using a sequence such as:
1009
1010 relax_start (SYMBOL);
1011 ... generate first expansion ...
1012 relax_switch ();
1013 ... generate second expansion ...
1014 relax_end ();
1015
1016 The code and fixups for the unwanted alternative are discarded
1017 by md_convert_frag. */
584892a6 1018#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 1019
584892a6
RS
1020#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1021#define RELAX_SECOND(X) ((X) & 0xff)
1022#define RELAX_USE_SECOND 0x10000
1023#define RELAX_SECOND_LONGER 0x20000
1024#define RELAX_NOMACRO 0x40000
1025#define RELAX_DELAY_SLOT 0x80000
df58fc94
RS
1026#define RELAX_DELAY_SLOT_16BIT 0x100000
1027#define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
1028#define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
252b5132 1029
4a6a3df4
AO
1030/* Branch without likely bit. If label is out of range, we turn:
1031
1032 beq reg1, reg2, label
1033 delay slot
1034
1035 into
1036
1037 bne reg1, reg2, 0f
1038 nop
1039 j label
1040 0: delay slot
1041
1042 with the following opcode replacements:
1043
1044 beq <-> bne
1045 blez <-> bgtz
1046 bltz <-> bgez
1047 bc1f <-> bc1t
1048
1049 bltzal <-> bgezal (with jal label instead of j label)
1050
1051 Even though keeping the delay slot instruction in the delay slot of
1052 the branch would be more efficient, it would be very tricky to do
1053 correctly, because we'd have to introduce a variable frag *after*
1054 the delay slot instruction, and expand that instead. Let's do it
1055 the easy way for now, even if the branch-not-taken case now costs
1056 one additional instruction. Out-of-range branches are not supposed
1057 to be common, anyway.
1058
1059 Branch likely. If label is out of range, we turn:
1060
1061 beql reg1, reg2, label
1062 delay slot (annulled if branch not taken)
1063
1064 into
1065
1066 beql reg1, reg2, 1f
1067 nop
1068 beql $0, $0, 2f
1069 nop
1070 1: j[al] label
1071 delay slot (executed only if branch taken)
1072 2:
1073
1074 It would be possible to generate a shorter sequence by losing the
1075 likely bit, generating something like:
b34976b6 1076
4a6a3df4
AO
1077 bne reg1, reg2, 0f
1078 nop
1079 j[al] label
1080 delay slot (executed only if branch taken)
1081 0:
1082
1083 beql -> bne
1084 bnel -> beq
1085 blezl -> bgtz
1086 bgtzl -> blez
1087 bltzl -> bgez
1088 bgezl -> bltz
1089 bc1fl -> bc1t
1090 bc1tl -> bc1f
1091
1092 bltzall -> bgezal (with jal label instead of j label)
1093 bgezall -> bltzal (ditto)
1094
1095
1096 but it's not clear that it would actually improve performance. */
66b3e8da
MR
1097#define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1098 ((relax_substateT) \
1099 (0xc0000000 \
1100 | ((at) & 0x1f) \
1101 | ((toofar) ? 0x20 : 0) \
1102 | ((link) ? 0x40 : 0) \
1103 | ((likely) ? 0x80 : 0) \
1104 | ((uncond) ? 0x100 : 0)))
4a6a3df4 1105#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
66b3e8da
MR
1106#define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1107#define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1108#define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1109#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1110#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1111
252b5132
RH
1112/* For mips16 code, we use an entirely different form of relaxation.
1113 mips16 supports two versions of most instructions which take
1114 immediate values: a small one which takes some small value, and a
1115 larger one which takes a 16 bit value. Since branches also follow
1116 this pattern, relaxing these values is required.
1117
1118 We can assemble both mips16 and normal MIPS code in a single
1119 object. Therefore, we need to support this type of relaxation at
1120 the same time that we support the relaxation described above. We
1121 use the high bit of the subtype field to distinguish these cases.
1122
1123 The information we store for this type of relaxation is the
1124 argument code found in the opcode file for this relocation, whether
1125 the user explicitly requested a small or extended form, and whether
1126 the relocation is in a jump or jal delay slot. That tells us the
1127 size of the value, and how it should be stored. We also store
1128 whether the fragment is considered to be extended or not. We also
1129 store whether this is known to be a branch to a different section,
1130 whether we have tried to relax this frag yet, and whether we have
1131 ever extended a PC relative fragment because of a shift count. */
1132#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1133 (0x80000000 \
1134 | ((type) & 0xff) \
1135 | ((small) ? 0x100 : 0) \
1136 | ((ext) ? 0x200 : 0) \
1137 | ((dslot) ? 0x400 : 0) \
1138 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 1139#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
1140#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1141#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1142#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1143#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1144#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1145#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1146#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1147#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1148#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1149#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1150#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95 1151
df58fc94
RS
1152/* For microMIPS code, we use relaxation similar to one we use for
1153 MIPS16 code. Some instructions that take immediate values support
1154 two encodings: a small one which takes some small value, and a
1155 larger one which takes a 16 bit value. As some branches also follow
1156 this pattern, relaxing these values is required.
1157
1158 We can assemble both microMIPS and normal MIPS code in a single
1159 object. Therefore, we need to support this type of relaxation at
1160 the same time that we support the relaxation described above. We
1161 use one of the high bits of the subtype field to distinguish these
1162 cases.
1163
1164 The information we store for this type of relaxation is the argument
1165 code found in the opcode file for this relocation, the register
40209cad
MR
1166 selected as the assembler temporary, whether the branch is
1167 unconditional, whether it is compact, whether it stores the link
1168 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1169 branches to a sequence of instructions is enabled, and whether the
1170 displacement of a branch is too large to fit as an immediate argument
1171 of a 16-bit and a 32-bit branch, respectively. */
1172#define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1173 relax32, toofar16, toofar32) \
1174 (0x40000000 \
1175 | ((type) & 0xff) \
1176 | (((at) & 0x1f) << 8) \
1177 | ((uncond) ? 0x2000 : 0) \
1178 | ((compact) ? 0x4000 : 0) \
1179 | ((link) ? 0x8000 : 0) \
1180 | ((relax32) ? 0x10000 : 0) \
1181 | ((toofar16) ? 0x20000 : 0) \
1182 | ((toofar32) ? 0x40000 : 0))
df58fc94
RS
1183#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1184#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1185#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
40209cad
MR
1186#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1187#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1188#define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1189#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1190
1191#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1192#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1193#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1194#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1195#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1196#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
df58fc94 1197
885add95
CD
1198/* Is the given value a sign-extended 32-bit value? */
1199#define IS_SEXT_32BIT_NUM(x) \
1200 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1201 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1202
1203/* Is the given value a sign-extended 16-bit value? */
1204#define IS_SEXT_16BIT_NUM(x) \
1205 (((x) &~ (offsetT) 0x7fff) == 0 \
1206 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1207
df58fc94
RS
1208/* Is the given value a sign-extended 12-bit value? */
1209#define IS_SEXT_12BIT_NUM(x) \
1210 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1211
2051e8c4
MR
1212/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1213#define IS_ZEXT_32BIT_NUM(x) \
1214 (((x) &~ (offsetT) 0xffffffff) == 0 \
1215 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1216
bf12938e
RS
1217/* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1218 VALUE << SHIFT. VALUE is evaluated exactly once. */
1219#define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1220 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1221 | (((VALUE) & (MASK)) << (SHIFT)))
1222
1223/* Extract bits MASK << SHIFT from STRUCT and shift them right
1224 SHIFT places. */
1225#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1226 (((STRUCT) >> (SHIFT)) & (MASK))
1227
1228/* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1229 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1230
1231 include/opcode/mips.h specifies operand fields using the macros
1232 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
1233 with "MIPS16OP" instead of "OP". */
df58fc94
RS
1234#define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1235 do \
1236 if (!(MICROMIPS)) \
1237 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1238 OP_MASK_##FIELD, OP_SH_##FIELD); \
1239 else \
1240 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1241 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1242 while (0)
bf12938e
RS
1243#define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1244 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1245 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1246
1247/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1248#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1249 (!(MICROMIPS) \
1250 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1251 : EXTRACT_BITS ((INSN).insn_opcode, \
1252 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1253#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1254 EXTRACT_BITS ((INSN).insn_opcode, \
1255 MIPS16OP_MASK_##FIELD, \
1256 MIPS16OP_SH_##FIELD)
4d7206a2 1257\f
df58fc94
RS
1258/* Whether or not we are emitting a branch-likely macro. */
1259static bfd_boolean emit_branch_likely_macro = FALSE;
1260
4d7206a2
RS
1261/* Global variables used when generating relaxable macros. See the
1262 comment above RELAX_ENCODE for more details about how relaxation
1263 is used. */
1264static struct {
1265 /* 0 if we're not emitting a relaxable macro.
1266 1 if we're emitting the first of the two relaxation alternatives.
1267 2 if we're emitting the second alternative. */
1268 int sequence;
1269
1270 /* The first relaxable fixup in the current frag. (In other words,
1271 the first fixup that refers to relaxable code.) */
1272 fixS *first_fixup;
1273
1274 /* sizes[0] says how many bytes of the first alternative are stored in
1275 the current frag. Likewise sizes[1] for the second alternative. */
1276 unsigned int sizes[2];
1277
1278 /* The symbol on which the choice of sequence depends. */
1279 symbolS *symbol;
1280} mips_relax;
252b5132 1281\f
584892a6
RS
1282/* Global variables used to decide whether a macro needs a warning. */
1283static struct {
1284 /* True if the macro is in a branch delay slot. */
1285 bfd_boolean delay_slot_p;
1286
df58fc94
RS
1287 /* Set to the length in bytes required if the macro is in a delay slot
1288 that requires a specific length of instruction, otherwise zero. */
1289 unsigned int delay_slot_length;
1290
584892a6
RS
1291 /* For relaxable macros, sizes[0] is the length of the first alternative
1292 in bytes and sizes[1] is the length of the second alternative.
1293 For non-relaxable macros, both elements give the length of the
1294 macro in bytes. */
1295 unsigned int sizes[2];
1296
df58fc94
RS
1297 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1298 instruction of the first alternative in bytes and first_insn_sizes[1]
1299 is the length of the first instruction of the second alternative.
1300 For non-relaxable macros, both elements give the length of the first
1301 instruction in bytes.
1302
1303 Set to zero if we haven't yet seen the first instruction. */
1304 unsigned int first_insn_sizes[2];
1305
1306 /* For relaxable macros, insns[0] is the number of instructions for the
1307 first alternative and insns[1] is the number of instructions for the
1308 second alternative.
1309
1310 For non-relaxable macros, both elements give the number of
1311 instructions for the macro. */
1312 unsigned int insns[2];
1313
584892a6
RS
1314 /* The first variant frag for this macro. */
1315 fragS *first_frag;
1316} mips_macro_warning;
1317\f
252b5132
RH
1318/* Prototypes for static functions. */
1319
17a2f251 1320#define internalError() \
252b5132 1321 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
252b5132
RH
1322
1323enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1324
b34976b6 1325static void append_insn
df58fc94
RS
1326 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1327 bfd_boolean expansionp);
7d10b47d 1328static void mips_no_prev_insn (void);
c67a084a 1329static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1330static void mips16_macro_build
03ea81db 1331 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1332static void load_register (int, expressionS *, int);
584892a6
RS
1333static void macro_start (void);
1334static void macro_end (void);
17a2f251
TS
1335static void macro (struct mips_cl_insn * ip);
1336static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1337static void mips_ip (char *str, struct mips_cl_insn * ip);
1338static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1339static void mips16_immed
17a2f251
TS
1340 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
1341 unsigned long *, bfd_boolean *, unsigned short *);
5e0116d5 1342static size_t my_getSmallExpression
17a2f251
TS
1343 (expressionS *, bfd_reloc_code_real_type *, char *);
1344static void my_getExpression (expressionS *, char *);
1345static void s_align (int);
1346static void s_change_sec (int);
1347static void s_change_section (int);
1348static void s_cons (int);
1349static void s_float_cons (int);
1350static void s_mips_globl (int);
1351static void s_option (int);
1352static void s_mipsset (int);
1353static void s_abicalls (int);
1354static void s_cpload (int);
1355static void s_cpsetup (int);
1356static void s_cplocal (int);
1357static void s_cprestore (int);
1358static void s_cpreturn (int);
741d6ea8
JM
1359static void s_dtprelword (int);
1360static void s_dtpreldword (int);
d0f13682
CLT
1361static void s_tprelword (int);
1362static void s_tpreldword (int);
17a2f251
TS
1363static void s_gpvalue (int);
1364static void s_gpword (int);
1365static void s_gpdword (int);
1366static void s_cpadd (int);
1367static void s_insn (int);
1368static void md_obj_begin (void);
1369static void md_obj_end (void);
1370static void s_mips_ent (int);
1371static void s_mips_end (int);
1372static void s_mips_frame (int);
1373static void s_mips_mask (int reg_type);
1374static void s_mips_stab (int);
1375static void s_mips_weakext (int);
1376static void s_mips_file (int);
1377static void s_mips_loc (int);
1378static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1379static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 1380static int validate_mips_insn (const struct mips_opcode *);
df58fc94
RS
1381static int validate_micromips_insn (const struct mips_opcode *);
1382static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1383static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
e7af610e
NC
1384
1385/* Table and functions used to map between CPU/ISA names, and
1386 ISA levels, and CPU numbers. */
1387
e972090a
NC
1388struct mips_cpu_info
1389{
e7af610e 1390 const char *name; /* CPU or ISA name. */
ad3fea08 1391 int flags; /* ASEs available, or ISA flag. */
e7af610e
NC
1392 int isa; /* ISA level. */
1393 int cpu; /* CPU number (default CPU if ISA). */
1394};
1395
ad3fea08
TS
1396#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
1397#define MIPS_CPU_ASE_SMARTMIPS 0x0002 /* CPU implements SmartMIPS ASE */
1398#define MIPS_CPU_ASE_DSP 0x0004 /* CPU implements DSP ASE */
1399#define MIPS_CPU_ASE_MT 0x0008 /* CPU implements MT ASE */
1400#define MIPS_CPU_ASE_MIPS3D 0x0010 /* CPU implements MIPS-3D ASE */
1401#define MIPS_CPU_ASE_MDMX 0x0020 /* CPU implements MDMX ASE */
8b082fb1 1402#define MIPS_CPU_ASE_DSPR2 0x0040 /* CPU implements DSP R2 ASE */
dec0624d 1403#define MIPS_CPU_ASE_MCU 0x0080 /* CPU implements MCU ASE */
ad3fea08 1404
17a2f251
TS
1405static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1406static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1407static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
1408\f
1409/* Pseudo-op table.
1410
1411 The following pseudo-ops from the Kane and Heinrich MIPS book
1412 should be defined here, but are currently unsupported: .alias,
1413 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1414
1415 The following pseudo-ops from the Kane and Heinrich MIPS book are
1416 specific to the type of debugging information being generated, and
1417 should be defined by the object format: .aent, .begin, .bend,
1418 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1419 .vreg.
1420
1421 The following pseudo-ops from the Kane and Heinrich MIPS book are
1422 not MIPS CPU specific, but are also not specific to the object file
1423 format. This file is probably the best place to define them, but
d84bcf09 1424 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1425
e972090a
NC
1426static const pseudo_typeS mips_pseudo_table[] =
1427{
beae10d5 1428 /* MIPS specific pseudo-ops. */
252b5132
RH
1429 {"option", s_option, 0},
1430 {"set", s_mipsset, 0},
1431 {"rdata", s_change_sec, 'r'},
1432 {"sdata", s_change_sec, 's'},
1433 {"livereg", s_ignore, 0},
1434 {"abicalls", s_abicalls, 0},
1435 {"cpload", s_cpload, 0},
6478892d
TS
1436 {"cpsetup", s_cpsetup, 0},
1437 {"cplocal", s_cplocal, 0},
252b5132 1438 {"cprestore", s_cprestore, 0},
6478892d 1439 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1440 {"dtprelword", s_dtprelword, 0},
1441 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1442 {"tprelword", s_tprelword, 0},
1443 {"tpreldword", s_tpreldword, 0},
6478892d 1444 {"gpvalue", s_gpvalue, 0},
252b5132 1445 {"gpword", s_gpword, 0},
10181a0d 1446 {"gpdword", s_gpdword, 0},
252b5132
RH
1447 {"cpadd", s_cpadd, 0},
1448 {"insn", s_insn, 0},
1449
beae10d5 1450 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1451 chips. */
38a57ae7 1452 {"asciiz", stringer, 8 + 1},
252b5132
RH
1453 {"bss", s_change_sec, 'b'},
1454 {"err", s_err, 0},
1455 {"half", s_cons, 1},
1456 {"dword", s_cons, 3},
1457 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1458 {"origin", s_org, 0},
1459 {"repeat", s_rept, 0},
252b5132 1460
998b3c36
MR
1461 /* For MIPS this is non-standard, but we define it for consistency. */
1462 {"sbss", s_change_sec, 'B'},
1463
beae10d5 1464 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1465 here for one reason or another. */
1466 {"align", s_align, 0},
1467 {"byte", s_cons, 0},
1468 {"data", s_change_sec, 'd'},
1469 {"double", s_float_cons, 'd'},
1470 {"float", s_float_cons, 'f'},
1471 {"globl", s_mips_globl, 0},
1472 {"global", s_mips_globl, 0},
1473 {"hword", s_cons, 1},
1474 {"int", s_cons, 2},
1475 {"long", s_cons, 2},
1476 {"octa", s_cons, 4},
1477 {"quad", s_cons, 3},
cca86cc8 1478 {"section", s_change_section, 0},
252b5132
RH
1479 {"short", s_cons, 1},
1480 {"single", s_float_cons, 'f'},
1481 {"stabn", s_mips_stab, 'n'},
1482 {"text", s_change_sec, 't'},
1483 {"word", s_cons, 2},
add56521 1484
add56521 1485 { "extern", ecoff_directive_extern, 0},
add56521 1486
43841e91 1487 { NULL, NULL, 0 },
252b5132
RH
1488};
1489
e972090a
NC
1490static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1491{
beae10d5
KH
1492 /* These pseudo-ops should be defined by the object file format.
1493 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1494 {"aent", s_mips_ent, 1},
1495 {"bgnb", s_ignore, 0},
1496 {"end", s_mips_end, 0},
1497 {"endb", s_ignore, 0},
1498 {"ent", s_mips_ent, 0},
c5dd6aab 1499 {"file", s_mips_file, 0},
252b5132
RH
1500 {"fmask", s_mips_mask, 'F'},
1501 {"frame", s_mips_frame, 0},
c5dd6aab 1502 {"loc", s_mips_loc, 0},
252b5132
RH
1503 {"mask", s_mips_mask, 'R'},
1504 {"verstamp", s_ignore, 0},
43841e91 1505 { NULL, NULL, 0 },
252b5132
RH
1506};
1507
3ae8dd8d
MR
1508/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1509 purpose of the `.dc.a' internal pseudo-op. */
1510
1511int
1512mips_address_bytes (void)
1513{
1514 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1515}
1516
17a2f251 1517extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1518
1519void
17a2f251 1520mips_pop_insert (void)
252b5132
RH
1521{
1522 pop_insert (mips_pseudo_table);
1523 if (! ECOFF_DEBUGGING)
1524 pop_insert (mips_nonecoff_pseudo_table);
1525}
1526\f
1527/* Symbols labelling the current insn. */
1528
e972090a
NC
1529struct insn_label_list
1530{
252b5132
RH
1531 struct insn_label_list *next;
1532 symbolS *label;
1533};
1534
252b5132 1535static struct insn_label_list *free_insn_labels;
742a56fe 1536#define label_list tc_segment_info_data.labels
252b5132 1537
17a2f251 1538static void mips_clear_insn_labels (void);
df58fc94
RS
1539static void mips_mark_labels (void);
1540static void mips_compressed_mark_labels (void);
252b5132
RH
1541
1542static inline void
17a2f251 1543mips_clear_insn_labels (void)
252b5132
RH
1544{
1545 register struct insn_label_list **pl;
a8dbcb85 1546 segment_info_type *si;
252b5132 1547
a8dbcb85
TS
1548 if (now_seg)
1549 {
1550 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1551 ;
1552
1553 si = seg_info (now_seg);
1554 *pl = si->label_list;
1555 si->label_list = NULL;
1556 }
252b5132 1557}
a8dbcb85 1558
df58fc94
RS
1559/* Mark instruction labels in MIPS16/microMIPS mode. */
1560
1561static inline void
1562mips_mark_labels (void)
1563{
1564 if (HAVE_CODE_COMPRESSION)
1565 mips_compressed_mark_labels ();
1566}
252b5132
RH
1567\f
1568static char *expr_end;
1569
1570/* Expressions which appear in instructions. These are set by
1571 mips_ip. */
1572
1573static expressionS imm_expr;
5f74bc13 1574static expressionS imm2_expr;
252b5132
RH
1575static expressionS offset_expr;
1576
1577/* Relocs associated with imm_expr and offset_expr. */
1578
f6688943
TS
1579static bfd_reloc_code_real_type imm_reloc[3]
1580 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1581static bfd_reloc_code_real_type offset_reloc[3]
1582 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1583
df58fc94
RS
1584/* This is set to the resulting size of the instruction to be produced
1585 by mips16_ip if an explicit extension is used or by mips_ip if an
1586 explicit size is supplied. */
252b5132 1587
df58fc94 1588static unsigned int forced_insn_length;
252b5132 1589
7ed4a06a 1590#ifdef OBJ_ELF
ecb4347a
DJ
1591/* The pdr segment for per procedure frame/regmask info. Not used for
1592 ECOFF debugging. */
252b5132
RH
1593
1594static segT pdr_seg;
7ed4a06a 1595#endif
252b5132 1596
e013f690
TS
1597/* The default target format to use. */
1598
aeffff67
RS
1599#if defined (TE_FreeBSD)
1600#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1601#elif defined (TE_TMIPS)
1602#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1603#else
1604#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1605#endif
1606
e013f690 1607const char *
17a2f251 1608mips_target_format (void)
e013f690
TS
1609{
1610 switch (OUTPUT_FLAVOR)
1611 {
e013f690
TS
1612 case bfd_target_ecoff_flavour:
1613 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1614 case bfd_target_coff_flavour:
1615 return "pe-mips";
1616 case bfd_target_elf_flavour:
0a44bf69
RS
1617#ifdef TE_VXWORKS
1618 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1619 return (target_big_endian
1620 ? "elf32-bigmips-vxworks"
1621 : "elf32-littlemips-vxworks");
1622#endif
e013f690 1623 return (target_big_endian
cfe86eaa 1624 ? (HAVE_64BIT_OBJECTS
aeffff67 1625 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1626 : (HAVE_NEWABI
aeffff67
RS
1627 ? ELF_TARGET ("elf32-n", "big")
1628 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1629 : (HAVE_64BIT_OBJECTS
aeffff67 1630 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1631 : (HAVE_NEWABI
aeffff67
RS
1632 ? ELF_TARGET ("elf32-n", "little")
1633 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1634 default:
1635 abort ();
1636 return NULL;
1637 }
1638}
1639
df58fc94
RS
1640/* Return the length of a microMIPS instruction in bytes. If bits of
1641 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1642 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1643 major opcode) will require further modifications to the opcode
1644 table. */
1645
1646static inline unsigned int
1647micromips_insn_length (const struct mips_opcode *mo)
1648{
1649 return (mo->mask >> 16) == 0 ? 2 : 4;
1650}
1651
1e915849
RS
1652/* Return the length of instruction INSN. */
1653
1654static inline unsigned int
1655insn_length (const struct mips_cl_insn *insn)
1656{
df58fc94
RS
1657 if (mips_opts.micromips)
1658 return micromips_insn_length (insn->insn_mo);
1659 else if (mips_opts.mips16)
1660 return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1661 else
1e915849 1662 return 4;
1e915849
RS
1663}
1664
1665/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1666
1667static void
1668create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1669{
1670 size_t i;
1671
1672 insn->insn_mo = mo;
1673 insn->use_extend = FALSE;
1674 insn->extend = 0;
1675 insn->insn_opcode = mo->match;
1676 insn->frag = NULL;
1677 insn->where = 0;
1678 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1679 insn->fixp[i] = NULL;
1680 insn->fixed_p = (mips_opts.noreorder > 0);
1681 insn->noreorder_p = (mips_opts.noreorder > 0);
1682 insn->mips16_absolute_jump_p = 0;
15be625d 1683 insn->complete_p = 0;
1e915849
RS
1684}
1685
df58fc94 1686/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
1687
1688static void
df58fc94 1689mips_record_compressed_mode (void)
742a56fe
RS
1690{
1691 segment_info_type *si;
1692
1693 si = seg_info (now_seg);
1694 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1695 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
1696 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
1697 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
1698}
1699
1e915849
RS
1700/* Install INSN at the location specified by its "frag" and "where" fields. */
1701
1702static void
1703install_insn (const struct mips_cl_insn *insn)
1704{
1705 char *f = insn->frag->fr_literal + insn->where;
df58fc94 1706 if (!HAVE_CODE_COMPRESSION)
1e915849 1707 md_number_to_chars (f, insn->insn_opcode, 4);
df58fc94
RS
1708 else if (mips_opts.micromips)
1709 {
1710 unsigned int length = insn_length (insn);
1711 if (length == 2)
1712 md_number_to_chars (f, insn->insn_opcode, 2);
1713 else if (length == 4)
1714 {
1715 md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1716 f += 2;
1717 md_number_to_chars (f, insn->insn_opcode & 0xffff, 2);
1718 }
1719 else
1720 as_bad (_("48-bit microMIPS instructions are not supported"));
1721 }
1e915849
RS
1722 else if (insn->mips16_absolute_jump_p)
1723 {
1724 md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1725 md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1726 }
1727 else
1728 {
1729 if (insn->use_extend)
1730 {
1731 md_number_to_chars (f, 0xf000 | insn->extend, 2);
1732 f += 2;
1733 }
1734 md_number_to_chars (f, insn->insn_opcode, 2);
1735 }
df58fc94 1736 mips_record_compressed_mode ();
1e915849
RS
1737}
1738
1739/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1740 and install the opcode in the new location. */
1741
1742static void
1743move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1744{
1745 size_t i;
1746
1747 insn->frag = frag;
1748 insn->where = where;
1749 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1750 if (insn->fixp[i] != NULL)
1751 {
1752 insn->fixp[i]->fx_frag = frag;
1753 insn->fixp[i]->fx_where = where;
1754 }
1755 install_insn (insn);
1756}
1757
1758/* Add INSN to the end of the output. */
1759
1760static void
1761add_fixed_insn (struct mips_cl_insn *insn)
1762{
1763 char *f = frag_more (insn_length (insn));
1764 move_insn (insn, frag_now, f - frag_now->fr_literal);
1765}
1766
1767/* Start a variant frag and move INSN to the start of the variant part,
1768 marking it as fixed. The other arguments are as for frag_var. */
1769
1770static void
1771add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1772 relax_substateT subtype, symbolS *symbol, offsetT offset)
1773{
1774 frag_grow (max_chars);
1775 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1776 insn->fixed_p = 1;
1777 frag_var (rs_machine_dependent, max_chars, var,
1778 subtype, symbol, offset, NULL);
1779}
1780
1781/* Insert N copies of INSN into the history buffer, starting at
1782 position FIRST. Neither FIRST nor N need to be clipped. */
1783
1784static void
1785insert_into_history (unsigned int first, unsigned int n,
1786 const struct mips_cl_insn *insn)
1787{
1788 if (mips_relax.sequence != 2)
1789 {
1790 unsigned int i;
1791
1792 for (i = ARRAY_SIZE (history); i-- > first;)
1793 if (i >= first + n)
1794 history[i] = history[i - n];
1795 else
1796 history[i] = *insn;
1797 }
1798}
1799
71400594
RS
1800/* Initialize vr4120_conflicts. There is a bit of duplication here:
1801 the idea is to make it obvious at a glance that each errata is
1802 included. */
1803
1804static void
1805init_vr4120_conflicts (void)
1806{
1807#define CONFLICT(FIRST, SECOND) \
1808 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1809
1810 /* Errata 21 - [D]DIV[U] after [D]MACC */
1811 CONFLICT (MACC, DIV);
1812 CONFLICT (DMACC, DIV);
1813
1814 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1815 CONFLICT (DMULT, DMULT);
1816 CONFLICT (DMULT, DMACC);
1817 CONFLICT (DMACC, DMULT);
1818 CONFLICT (DMACC, DMACC);
1819
1820 /* Errata 24 - MT{LO,HI} after [D]MACC */
1821 CONFLICT (MACC, MTHILO);
1822 CONFLICT (DMACC, MTHILO);
1823
1824 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1825 instruction is executed immediately after a MACC or DMACC
1826 instruction, the result of [either instruction] is incorrect." */
1827 CONFLICT (MACC, MULT);
1828 CONFLICT (MACC, DMULT);
1829 CONFLICT (DMACC, MULT);
1830 CONFLICT (DMACC, DMULT);
1831
1832 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1833 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1834 DDIV or DDIVU instruction, the result of the MACC or
1835 DMACC instruction is incorrect.". */
1836 CONFLICT (DMULT, MACC);
1837 CONFLICT (DMULT, DMACC);
1838 CONFLICT (DIV, MACC);
1839 CONFLICT (DIV, DMACC);
1840
1841#undef CONFLICT
1842}
1843
707bfff6
TS
1844struct regname {
1845 const char *name;
1846 unsigned int num;
1847};
1848
1849#define RTYPE_MASK 0x1ff00
1850#define RTYPE_NUM 0x00100
1851#define RTYPE_FPU 0x00200
1852#define RTYPE_FCC 0x00400
1853#define RTYPE_VEC 0x00800
1854#define RTYPE_GP 0x01000
1855#define RTYPE_CP0 0x02000
1856#define RTYPE_PC 0x04000
1857#define RTYPE_ACC 0x08000
1858#define RTYPE_CCC 0x10000
1859#define RNUM_MASK 0x000ff
1860#define RWARN 0x80000
1861
1862#define GENERIC_REGISTER_NUMBERS \
1863 {"$0", RTYPE_NUM | 0}, \
1864 {"$1", RTYPE_NUM | 1}, \
1865 {"$2", RTYPE_NUM | 2}, \
1866 {"$3", RTYPE_NUM | 3}, \
1867 {"$4", RTYPE_NUM | 4}, \
1868 {"$5", RTYPE_NUM | 5}, \
1869 {"$6", RTYPE_NUM | 6}, \
1870 {"$7", RTYPE_NUM | 7}, \
1871 {"$8", RTYPE_NUM | 8}, \
1872 {"$9", RTYPE_NUM | 9}, \
1873 {"$10", RTYPE_NUM | 10}, \
1874 {"$11", RTYPE_NUM | 11}, \
1875 {"$12", RTYPE_NUM | 12}, \
1876 {"$13", RTYPE_NUM | 13}, \
1877 {"$14", RTYPE_NUM | 14}, \
1878 {"$15", RTYPE_NUM | 15}, \
1879 {"$16", RTYPE_NUM | 16}, \
1880 {"$17", RTYPE_NUM | 17}, \
1881 {"$18", RTYPE_NUM | 18}, \
1882 {"$19", RTYPE_NUM | 19}, \
1883 {"$20", RTYPE_NUM | 20}, \
1884 {"$21", RTYPE_NUM | 21}, \
1885 {"$22", RTYPE_NUM | 22}, \
1886 {"$23", RTYPE_NUM | 23}, \
1887 {"$24", RTYPE_NUM | 24}, \
1888 {"$25", RTYPE_NUM | 25}, \
1889 {"$26", RTYPE_NUM | 26}, \
1890 {"$27", RTYPE_NUM | 27}, \
1891 {"$28", RTYPE_NUM | 28}, \
1892 {"$29", RTYPE_NUM | 29}, \
1893 {"$30", RTYPE_NUM | 30}, \
1894 {"$31", RTYPE_NUM | 31}
1895
1896#define FPU_REGISTER_NAMES \
1897 {"$f0", RTYPE_FPU | 0}, \
1898 {"$f1", RTYPE_FPU | 1}, \
1899 {"$f2", RTYPE_FPU | 2}, \
1900 {"$f3", RTYPE_FPU | 3}, \
1901 {"$f4", RTYPE_FPU | 4}, \
1902 {"$f5", RTYPE_FPU | 5}, \
1903 {"$f6", RTYPE_FPU | 6}, \
1904 {"$f7", RTYPE_FPU | 7}, \
1905 {"$f8", RTYPE_FPU | 8}, \
1906 {"$f9", RTYPE_FPU | 9}, \
1907 {"$f10", RTYPE_FPU | 10}, \
1908 {"$f11", RTYPE_FPU | 11}, \
1909 {"$f12", RTYPE_FPU | 12}, \
1910 {"$f13", RTYPE_FPU | 13}, \
1911 {"$f14", RTYPE_FPU | 14}, \
1912 {"$f15", RTYPE_FPU | 15}, \
1913 {"$f16", RTYPE_FPU | 16}, \
1914 {"$f17", RTYPE_FPU | 17}, \
1915 {"$f18", RTYPE_FPU | 18}, \
1916 {"$f19", RTYPE_FPU | 19}, \
1917 {"$f20", RTYPE_FPU | 20}, \
1918 {"$f21", RTYPE_FPU | 21}, \
1919 {"$f22", RTYPE_FPU | 22}, \
1920 {"$f23", RTYPE_FPU | 23}, \
1921 {"$f24", RTYPE_FPU | 24}, \
1922 {"$f25", RTYPE_FPU | 25}, \
1923 {"$f26", RTYPE_FPU | 26}, \
1924 {"$f27", RTYPE_FPU | 27}, \
1925 {"$f28", RTYPE_FPU | 28}, \
1926 {"$f29", RTYPE_FPU | 29}, \
1927 {"$f30", RTYPE_FPU | 30}, \
1928 {"$f31", RTYPE_FPU | 31}
1929
1930#define FPU_CONDITION_CODE_NAMES \
1931 {"$fcc0", RTYPE_FCC | 0}, \
1932 {"$fcc1", RTYPE_FCC | 1}, \
1933 {"$fcc2", RTYPE_FCC | 2}, \
1934 {"$fcc3", RTYPE_FCC | 3}, \
1935 {"$fcc4", RTYPE_FCC | 4}, \
1936 {"$fcc5", RTYPE_FCC | 5}, \
1937 {"$fcc6", RTYPE_FCC | 6}, \
1938 {"$fcc7", RTYPE_FCC | 7}
1939
1940#define COPROC_CONDITION_CODE_NAMES \
1941 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
1942 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
1943 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
1944 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
1945 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
1946 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
1947 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
1948 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
1949
1950#define N32N64_SYMBOLIC_REGISTER_NAMES \
1951 {"$a4", RTYPE_GP | 8}, \
1952 {"$a5", RTYPE_GP | 9}, \
1953 {"$a6", RTYPE_GP | 10}, \
1954 {"$a7", RTYPE_GP | 11}, \
1955 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
1956 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
1957 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
1958 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
1959 {"$t0", RTYPE_GP | 12}, \
1960 {"$t1", RTYPE_GP | 13}, \
1961 {"$t2", RTYPE_GP | 14}, \
1962 {"$t3", RTYPE_GP | 15}
1963
1964#define O32_SYMBOLIC_REGISTER_NAMES \
1965 {"$t0", RTYPE_GP | 8}, \
1966 {"$t1", RTYPE_GP | 9}, \
1967 {"$t2", RTYPE_GP | 10}, \
1968 {"$t3", RTYPE_GP | 11}, \
1969 {"$t4", RTYPE_GP | 12}, \
1970 {"$t5", RTYPE_GP | 13}, \
1971 {"$t6", RTYPE_GP | 14}, \
1972 {"$t7", RTYPE_GP | 15}, \
1973 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
1974 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
1975 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
1976 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
1977
1978/* Remaining symbolic register names */
1979#define SYMBOLIC_REGISTER_NAMES \
1980 {"$zero", RTYPE_GP | 0}, \
1981 {"$at", RTYPE_GP | 1}, \
1982 {"$AT", RTYPE_GP | 1}, \
1983 {"$v0", RTYPE_GP | 2}, \
1984 {"$v1", RTYPE_GP | 3}, \
1985 {"$a0", RTYPE_GP | 4}, \
1986 {"$a1", RTYPE_GP | 5}, \
1987 {"$a2", RTYPE_GP | 6}, \
1988 {"$a3", RTYPE_GP | 7}, \
1989 {"$s0", RTYPE_GP | 16}, \
1990 {"$s1", RTYPE_GP | 17}, \
1991 {"$s2", RTYPE_GP | 18}, \
1992 {"$s3", RTYPE_GP | 19}, \
1993 {"$s4", RTYPE_GP | 20}, \
1994 {"$s5", RTYPE_GP | 21}, \
1995 {"$s6", RTYPE_GP | 22}, \
1996 {"$s7", RTYPE_GP | 23}, \
1997 {"$t8", RTYPE_GP | 24}, \
1998 {"$t9", RTYPE_GP | 25}, \
1999 {"$k0", RTYPE_GP | 26}, \
2000 {"$kt0", RTYPE_GP | 26}, \
2001 {"$k1", RTYPE_GP | 27}, \
2002 {"$kt1", RTYPE_GP | 27}, \
2003 {"$gp", RTYPE_GP | 28}, \
2004 {"$sp", RTYPE_GP | 29}, \
2005 {"$s8", RTYPE_GP | 30}, \
2006 {"$fp", RTYPE_GP | 30}, \
2007 {"$ra", RTYPE_GP | 31}
2008
2009#define MIPS16_SPECIAL_REGISTER_NAMES \
2010 {"$pc", RTYPE_PC | 0}
2011
2012#define MDMX_VECTOR_REGISTER_NAMES \
2013 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2014 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2015 {"$v2", RTYPE_VEC | 2}, \
2016 {"$v3", RTYPE_VEC | 3}, \
2017 {"$v4", RTYPE_VEC | 4}, \
2018 {"$v5", RTYPE_VEC | 5}, \
2019 {"$v6", RTYPE_VEC | 6}, \
2020 {"$v7", RTYPE_VEC | 7}, \
2021 {"$v8", RTYPE_VEC | 8}, \
2022 {"$v9", RTYPE_VEC | 9}, \
2023 {"$v10", RTYPE_VEC | 10}, \
2024 {"$v11", RTYPE_VEC | 11}, \
2025 {"$v12", RTYPE_VEC | 12}, \
2026 {"$v13", RTYPE_VEC | 13}, \
2027 {"$v14", RTYPE_VEC | 14}, \
2028 {"$v15", RTYPE_VEC | 15}, \
2029 {"$v16", RTYPE_VEC | 16}, \
2030 {"$v17", RTYPE_VEC | 17}, \
2031 {"$v18", RTYPE_VEC | 18}, \
2032 {"$v19", RTYPE_VEC | 19}, \
2033 {"$v20", RTYPE_VEC | 20}, \
2034 {"$v21", RTYPE_VEC | 21}, \
2035 {"$v22", RTYPE_VEC | 22}, \
2036 {"$v23", RTYPE_VEC | 23}, \
2037 {"$v24", RTYPE_VEC | 24}, \
2038 {"$v25", RTYPE_VEC | 25}, \
2039 {"$v26", RTYPE_VEC | 26}, \
2040 {"$v27", RTYPE_VEC | 27}, \
2041 {"$v28", RTYPE_VEC | 28}, \
2042 {"$v29", RTYPE_VEC | 29}, \
2043 {"$v30", RTYPE_VEC | 30}, \
2044 {"$v31", RTYPE_VEC | 31}
2045
2046#define MIPS_DSP_ACCUMULATOR_NAMES \
2047 {"$ac0", RTYPE_ACC | 0}, \
2048 {"$ac1", RTYPE_ACC | 1}, \
2049 {"$ac2", RTYPE_ACC | 2}, \
2050 {"$ac3", RTYPE_ACC | 3}
2051
2052static const struct regname reg_names[] = {
2053 GENERIC_REGISTER_NUMBERS,
2054 FPU_REGISTER_NAMES,
2055 FPU_CONDITION_CODE_NAMES,
2056 COPROC_CONDITION_CODE_NAMES,
2057
2058 /* The $txx registers depends on the abi,
2059 these will be added later into the symbol table from
2060 one of the tables below once mips_abi is set after
2061 parsing of arguments from the command line. */
2062 SYMBOLIC_REGISTER_NAMES,
2063
2064 MIPS16_SPECIAL_REGISTER_NAMES,
2065 MDMX_VECTOR_REGISTER_NAMES,
2066 MIPS_DSP_ACCUMULATOR_NAMES,
2067 {0, 0}
2068};
2069
2070static const struct regname reg_names_o32[] = {
2071 O32_SYMBOLIC_REGISTER_NAMES,
2072 {0, 0}
2073};
2074
2075static const struct regname reg_names_n32n64[] = {
2076 N32N64_SYMBOLIC_REGISTER_NAMES,
2077 {0, 0}
2078};
2079
df58fc94
RS
2080/* Check if S points at a valid register specifier according to TYPES.
2081 If so, then return 1, advance S to consume the specifier and store
2082 the register's number in REGNOP, otherwise return 0. */
2083
707bfff6
TS
2084static int
2085reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2086{
2087 symbolS *symbolP;
2088 char *e;
2089 char save_c;
2090 int reg = -1;
2091
2092 /* Find end of name. */
2093 e = *s;
2094 if (is_name_beginner (*e))
2095 ++e;
2096 while (is_part_of_name (*e))
2097 ++e;
2098
2099 /* Terminate name. */
2100 save_c = *e;
2101 *e = '\0';
2102
2103 /* Look for a register symbol. */
2104 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2105 {
2106 int r = S_GET_VALUE (symbolP);
2107 if (r & types)
2108 reg = r & RNUM_MASK;
2109 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2110 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
2111 reg = (r & RNUM_MASK) - 2;
2112 }
2113 /* Else see if this is a register defined in an itbl entry. */
2114 else if ((types & RTYPE_GP) && itbl_have_entries)
2115 {
2116 char *n = *s;
2117 unsigned long r;
2118
2119 if (*n == '$')
2120 ++n;
2121 if (itbl_get_reg_val (n, &r))
2122 reg = r & RNUM_MASK;
2123 }
2124
2125 /* Advance to next token if a register was recognised. */
2126 if (reg >= 0)
2127 *s = e;
2128 else if (types & RWARN)
20203fb9 2129 as_warn (_("Unrecognized register name `%s'"), *s);
707bfff6
TS
2130
2131 *e = save_c;
2132 if (regnop)
2133 *regnop = reg;
2134 return reg >= 0;
2135}
2136
df58fc94
RS
2137/* Check if S points at a valid register list according to TYPES.
2138 If so, then return 1, advance S to consume the list and store
2139 the registers present on the list as a bitmask of ones in REGLISTP,
2140 otherwise return 0. A valid list comprises a comma-separated
2141 enumeration of valid single registers and/or dash-separated
2142 contiguous register ranges as determined by their numbers.
2143
2144 As a special exception if one of s0-s7 registers is specified as
2145 the range's lower delimiter and s8 (fp) is its upper one, then no
2146 registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2309ddf2 2147 are selected; they have to be listed separately if needed. */
df58fc94
RS
2148
2149static int
2150reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2151{
2152 unsigned int reglist = 0;
2153 unsigned int lastregno;
2154 bfd_boolean ok = TRUE;
2155 unsigned int regmask;
2309ddf2 2156 char *s_endlist = *s;
df58fc94 2157 char *s_reset = *s;
2309ddf2 2158 unsigned int regno;
df58fc94
RS
2159
2160 while (reg_lookup (s, types, &regno))
2161 {
2162 lastregno = regno;
2163 if (**s == '-')
2164 {
2165 (*s)++;
2166 ok = reg_lookup (s, types, &lastregno);
2167 if (ok && lastregno < regno)
2168 ok = FALSE;
2169 if (!ok)
2170 break;
2171 }
2172
2173 if (lastregno == FP && regno >= S0 && regno <= S7)
2174 {
2175 lastregno = S7;
2176 reglist |= 1 << FP;
2177 }
2178 regmask = 1 << lastregno;
2179 regmask = (regmask << 1) - 1;
2180 regmask ^= (1 << regno) - 1;
2181 reglist |= regmask;
2182
2309ddf2 2183 s_endlist = *s;
df58fc94
RS
2184 if (**s != ',')
2185 break;
2186 (*s)++;
2187 }
2188
2189 if (ok)
2309ddf2 2190 *s = s_endlist;
df58fc94
RS
2191 else
2192 *s = s_reset;
2193 if (reglistp)
2194 *reglistp = reglist;
2195 return ok && reglist != 0;
2196}
2197
037b32b9 2198/* Return TRUE if opcode MO is valid on the currently selected ISA and
f79e2745 2199 architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
2200
2201static bfd_boolean
f79e2745 2202is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
2203{
2204 int isa = mips_opts.isa;
2205 int fp_s, fp_d;
2206
2207 if (mips_opts.ase_mdmx)
2208 isa |= INSN_MDMX;
2209 if (mips_opts.ase_dsp)
2210 isa |= INSN_DSP;
2211 if (mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
2212 isa |= INSN_DSP64;
2213 if (mips_opts.ase_dspr2)
2214 isa |= INSN_DSPR2;
2215 if (mips_opts.ase_mt)
2216 isa |= INSN_MT;
2217 if (mips_opts.ase_mips3d)
2218 isa |= INSN_MIPS3D;
2219 if (mips_opts.ase_smartmips)
2220 isa |= INSN_SMARTMIPS;
dec0624d
MR
2221 if (mips_opts.ase_mcu)
2222 isa |= INSN_MCU;
037b32b9 2223
b19e8a9b
AN
2224 /* Don't accept instructions based on the ISA if the CPU does not implement
2225 all the coprocessor insns. */
2226 if (NO_ISA_COP (mips_opts.arch)
2227 && COP_INSN (mo->pinfo))
2228 isa = 0;
2229
037b32b9
AN
2230 if (!OPCODE_IS_MEMBER (mo, isa, mips_opts.arch))
2231 return FALSE;
2232
2233 /* Check whether the instruction or macro requires single-precision or
2234 double-precision floating-point support. Note that this information is
2235 stored differently in the opcode table for insns and macros. */
2236 if (mo->pinfo == INSN_MACRO)
2237 {
2238 fp_s = mo->pinfo2 & INSN2_M_FP_S;
2239 fp_d = mo->pinfo2 & INSN2_M_FP_D;
2240 }
2241 else
2242 {
2243 fp_s = mo->pinfo & FP_S;
2244 fp_d = mo->pinfo & FP_D;
2245 }
2246
2247 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2248 return FALSE;
2249
2250 if (fp_s && mips_opts.soft_float)
2251 return FALSE;
2252
2253 return TRUE;
2254}
2255
2256/* Return TRUE if the MIPS16 opcode MO is valid on the currently
2257 selected ISA and architecture. */
2258
2259static bfd_boolean
2260is_opcode_valid_16 (const struct mips_opcode *mo)
2261{
2262 return OPCODE_IS_MEMBER (mo, mips_opts.isa, mips_opts.arch) ? TRUE : FALSE;
2263}
2264
df58fc94
RS
2265/* Return TRUE if the size of the microMIPS opcode MO matches one
2266 explicitly requested. Always TRUE in the standard MIPS mode. */
2267
2268static bfd_boolean
2269is_size_valid (const struct mips_opcode *mo)
2270{
2271 if (!mips_opts.micromips)
2272 return TRUE;
2273
2274 if (!forced_insn_length)
2275 return TRUE;
2276 if (mo->pinfo == INSN_MACRO)
2277 return FALSE;
2278 return forced_insn_length == micromips_insn_length (mo);
2279}
2280
2281/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
2282 of the preceding instruction. Always TRUE in the standard MIPS mode. */
2283
2284static bfd_boolean
2285is_delay_slot_valid (const struct mips_opcode *mo)
2286{
2287 if (!mips_opts.micromips)
2288 return TRUE;
2289
2290 if (mo->pinfo == INSN_MACRO)
2291 return TRUE;
2292 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2293 && micromips_insn_length (mo) != 4)
2294 return FALSE;
2295 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2296 && micromips_insn_length (mo) != 2)
2297 return FALSE;
2298
2299 return TRUE;
2300}
2301
707bfff6
TS
2302/* This function is called once, at assembler startup time. It should set up
2303 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 2304
252b5132 2305void
17a2f251 2306md_begin (void)
252b5132 2307{
3994f87e 2308 const char *retval = NULL;
156c2f8b 2309 int i = 0;
252b5132 2310 int broken = 0;
1f25f5d3 2311
0a44bf69
RS
2312 if (mips_pic != NO_PIC)
2313 {
2314 if (g_switch_seen && g_switch_value != 0)
2315 as_bad (_("-G may not be used in position-independent code"));
2316 g_switch_value = 0;
2317 }
2318
fef14a42 2319 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
2320 as_warn (_("Could not set architecture and machine"));
2321
252b5132
RH
2322 op_hash = hash_new ();
2323
2324 for (i = 0; i < NUMOPCODES;)
2325 {
2326 const char *name = mips_opcodes[i].name;
2327
17a2f251 2328 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
2329 if (retval != NULL)
2330 {
2331 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2332 mips_opcodes[i].name, retval);
2333 /* Probably a memory allocation problem? Give up now. */
2334 as_fatal (_("Broken assembler. No assembly attempted."));
2335 }
2336 do
2337 {
2338 if (mips_opcodes[i].pinfo != INSN_MACRO)
2339 {
2340 if (!validate_mips_insn (&mips_opcodes[i]))
2341 broken = 1;
1e915849
RS
2342 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2343 {
2344 create_insn (&nop_insn, mips_opcodes + i);
c67a084a
NC
2345 if (mips_fix_loongson2f_nop)
2346 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
1e915849
RS
2347 nop_insn.fixed_p = 1;
2348 }
252b5132
RH
2349 }
2350 ++i;
2351 }
2352 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2353 }
2354
2355 mips16_op_hash = hash_new ();
2356
2357 i = 0;
2358 while (i < bfd_mips16_num_opcodes)
2359 {
2360 const char *name = mips16_opcodes[i].name;
2361
17a2f251 2362 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
2363 if (retval != NULL)
2364 as_fatal (_("internal: can't hash `%s': %s"),
2365 mips16_opcodes[i].name, retval);
2366 do
2367 {
2368 if (mips16_opcodes[i].pinfo != INSN_MACRO
2369 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2370 != mips16_opcodes[i].match))
2371 {
2372 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2373 mips16_opcodes[i].name, mips16_opcodes[i].args);
2374 broken = 1;
2375 }
1e915849
RS
2376 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2377 {
2378 create_insn (&mips16_nop_insn, mips16_opcodes + i);
2379 mips16_nop_insn.fixed_p = 1;
2380 }
252b5132
RH
2381 ++i;
2382 }
2383 while (i < bfd_mips16_num_opcodes
2384 && strcmp (mips16_opcodes[i].name, name) == 0);
2385 }
2386
df58fc94
RS
2387 micromips_op_hash = hash_new ();
2388
2389 i = 0;
2390 while (i < bfd_micromips_num_opcodes)
2391 {
2392 const char *name = micromips_opcodes[i].name;
2393
2394 retval = hash_insert (micromips_op_hash, name,
2395 (void *) &micromips_opcodes[i]);
2396 if (retval != NULL)
2397 as_fatal (_("internal: can't hash `%s': %s"),
2398 micromips_opcodes[i].name, retval);
2399 do
2400 if (micromips_opcodes[i].pinfo != INSN_MACRO)
2401 {
2402 struct mips_cl_insn *micromips_nop_insn;
2403
2404 if (!validate_micromips_insn (&micromips_opcodes[i]))
2405 broken = 1;
2406
2407 if (micromips_insn_length (micromips_opcodes + i) == 2)
2408 micromips_nop_insn = &micromips_nop16_insn;
2409 else if (micromips_insn_length (micromips_opcodes + i) == 4)
2410 micromips_nop_insn = &micromips_nop32_insn;
2411 else
2412 continue;
2413
2414 if (micromips_nop_insn->insn_mo == NULL
2415 && strcmp (name, "nop") == 0)
2416 {
2417 create_insn (micromips_nop_insn, micromips_opcodes + i);
2418 micromips_nop_insn->fixed_p = 1;
2419 }
2420 }
2421 while (++i < bfd_micromips_num_opcodes
2422 && strcmp (micromips_opcodes[i].name, name) == 0);
2423 }
2424
252b5132
RH
2425 if (broken)
2426 as_fatal (_("Broken assembler. No assembly attempted."));
2427
2428 /* We add all the general register names to the symbol table. This
2429 helps us detect invalid uses of them. */
707bfff6
TS
2430 for (i = 0; reg_names[i].name; i++)
2431 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 2432 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
2433 &zero_address_frag));
2434 if (HAVE_NEWABI)
2435 for (i = 0; reg_names_n32n64[i].name; i++)
2436 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 2437 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 2438 &zero_address_frag));
707bfff6
TS
2439 else
2440 for (i = 0; reg_names_o32[i].name; i++)
2441 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 2442 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 2443 &zero_address_frag));
6047c971 2444
7d10b47d 2445 mips_no_prev_insn ();
252b5132
RH
2446
2447 mips_gprmask = 0;
2448 mips_cprmask[0] = 0;
2449 mips_cprmask[1] = 0;
2450 mips_cprmask[2] = 0;
2451 mips_cprmask[3] = 0;
2452
2453 /* set the default alignment for the text section (2**2) */
2454 record_alignment (text_section, 2);
2455
4d0d148d 2456 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 2457
707bfff6 2458#ifdef OBJ_ELF
f43abd2b 2459 if (IS_ELF)
252b5132 2460 {
0a44bf69
RS
2461 /* On a native system other than VxWorks, sections must be aligned
2462 to 16 byte boundaries. When configured for an embedded ELF
2463 target, we don't bother. */
c41e87e3
CF
2464 if (strncmp (TARGET_OS, "elf", 3) != 0
2465 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132
RH
2466 {
2467 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2468 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2469 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2470 }
2471
2472 /* Create a .reginfo section for register masks and a .mdebug
2473 section for debugging information. */
2474 {
2475 segT seg;
2476 subsegT subseg;
2477 flagword flags;
2478 segT sec;
2479
2480 seg = now_seg;
2481 subseg = now_subseg;
2482
2483 /* The ABI says this section should be loaded so that the
2484 running program can access it. However, we don't load it
2485 if we are configured for an embedded target */
2486 flags = SEC_READONLY | SEC_DATA;
c41e87e3 2487 if (strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
2488 flags |= SEC_ALLOC | SEC_LOAD;
2489
316f5878 2490 if (mips_abi != N64_ABI)
252b5132
RH
2491 {
2492 sec = subseg_new (".reginfo", (subsegT) 0);
2493
195325d2
TS
2494 bfd_set_section_flags (stdoutput, sec, flags);
2495 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 2496
252b5132 2497 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
252b5132
RH
2498 }
2499 else
2500 {
2501 /* The 64-bit ABI uses a .MIPS.options section rather than
2502 .reginfo section. */
2503 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
2504 bfd_set_section_flags (stdoutput, sec, flags);
2505 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 2506
252b5132
RH
2507 /* Set up the option header. */
2508 {
2509 Elf_Internal_Options opthdr;
2510 char *f;
2511
2512 opthdr.kind = ODK_REGINFO;
2513 opthdr.size = (sizeof (Elf_External_Options)
2514 + sizeof (Elf64_External_RegInfo));
2515 opthdr.section = 0;
2516 opthdr.info = 0;
2517 f = frag_more (sizeof (Elf_External_Options));
2518 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2519 (Elf_External_Options *) f);
2520
2521 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2522 }
252b5132
RH
2523 }
2524
2525 if (ECOFF_DEBUGGING)
2526 {
2527 sec = subseg_new (".mdebug", (subsegT) 0);
2528 (void) bfd_set_section_flags (stdoutput, sec,
2529 SEC_HAS_CONTENTS | SEC_READONLY);
2530 (void) bfd_set_section_alignment (stdoutput, sec, 2);
2531 }
f43abd2b 2532 else if (mips_flag_pdr)
ecb4347a
DJ
2533 {
2534 pdr_seg = subseg_new (".pdr", (subsegT) 0);
2535 (void) bfd_set_section_flags (stdoutput, pdr_seg,
2536 SEC_READONLY | SEC_RELOC
2537 | SEC_DEBUGGING);
2538 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2539 }
252b5132
RH
2540
2541 subseg_set (seg, subseg);
2542 }
2543 }
707bfff6 2544#endif /* OBJ_ELF */
252b5132
RH
2545
2546 if (! ECOFF_DEBUGGING)
2547 md_obj_begin ();
71400594
RS
2548
2549 if (mips_fix_vr4120)
2550 init_vr4120_conflicts ();
252b5132
RH
2551}
2552
2553void
17a2f251 2554md_mips_end (void)
252b5132 2555{
02b1ab82 2556 mips_emit_delays ();
252b5132
RH
2557 if (! ECOFF_DEBUGGING)
2558 md_obj_end ();
2559}
2560
2561void
17a2f251 2562md_assemble (char *str)
252b5132
RH
2563{
2564 struct mips_cl_insn insn;
f6688943
TS
2565 bfd_reloc_code_real_type unused_reloc[3]
2566 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
2567
2568 imm_expr.X_op = O_absent;
5f74bc13 2569 imm2_expr.X_op = O_absent;
252b5132 2570 offset_expr.X_op = O_absent;
f6688943
TS
2571 imm_reloc[0] = BFD_RELOC_UNUSED;
2572 imm_reloc[1] = BFD_RELOC_UNUSED;
2573 imm_reloc[2] = BFD_RELOC_UNUSED;
2574 offset_reloc[0] = BFD_RELOC_UNUSED;
2575 offset_reloc[1] = BFD_RELOC_UNUSED;
2576 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
2577
2578 if (mips_opts.mips16)
2579 mips16_ip (str, &insn);
2580 else
2581 {
2582 mips_ip (str, &insn);
beae10d5
KH
2583 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2584 str, insn.insn_opcode));
252b5132
RH
2585 }
2586
2587 if (insn_error)
2588 {
2589 as_bad ("%s `%s'", insn_error, str);
2590 return;
2591 }
2592
2593 if (insn.insn_mo->pinfo == INSN_MACRO)
2594 {
584892a6 2595 macro_start ();
252b5132
RH
2596 if (mips_opts.mips16)
2597 mips16_macro (&insn);
2598 else
2599 macro (&insn);
584892a6 2600 macro_end ();
252b5132
RH
2601 }
2602 else
2603 {
2604 if (imm_expr.X_op != O_absent)
df58fc94 2605 append_insn (&insn, &imm_expr, imm_reloc, FALSE);
252b5132 2606 else if (offset_expr.X_op != O_absent)
df58fc94 2607 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 2608 else
df58fc94 2609 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132
RH
2610 }
2611}
2612
738e5348
RS
2613/* Convenience functions for abstracting away the differences between
2614 MIPS16 and non-MIPS16 relocations. */
2615
2616static inline bfd_boolean
2617mips16_reloc_p (bfd_reloc_code_real_type reloc)
2618{
2619 switch (reloc)
2620 {
2621 case BFD_RELOC_MIPS16_JMP:
2622 case BFD_RELOC_MIPS16_GPREL:
2623 case BFD_RELOC_MIPS16_GOT16:
2624 case BFD_RELOC_MIPS16_CALL16:
2625 case BFD_RELOC_MIPS16_HI16_S:
2626 case BFD_RELOC_MIPS16_HI16:
2627 case BFD_RELOC_MIPS16_LO16:
2628 return TRUE;
2629
2630 default:
2631 return FALSE;
2632 }
2633}
2634
df58fc94
RS
2635static inline bfd_boolean
2636micromips_reloc_p (bfd_reloc_code_real_type reloc)
2637{
2638 switch (reloc)
2639 {
2640 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2641 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2642 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2643 case BFD_RELOC_MICROMIPS_GPREL16:
2644 case BFD_RELOC_MICROMIPS_JMP:
2645 case BFD_RELOC_MICROMIPS_HI16:
2646 case BFD_RELOC_MICROMIPS_HI16_S:
2647 case BFD_RELOC_MICROMIPS_LO16:
2648 case BFD_RELOC_MICROMIPS_LITERAL:
2649 case BFD_RELOC_MICROMIPS_GOT16:
2650 case BFD_RELOC_MICROMIPS_CALL16:
2651 case BFD_RELOC_MICROMIPS_GOT_HI16:
2652 case BFD_RELOC_MICROMIPS_GOT_LO16:
2653 case BFD_RELOC_MICROMIPS_CALL_HI16:
2654 case BFD_RELOC_MICROMIPS_CALL_LO16:
2655 case BFD_RELOC_MICROMIPS_SUB:
2656 case BFD_RELOC_MICROMIPS_GOT_PAGE:
2657 case BFD_RELOC_MICROMIPS_GOT_OFST:
2658 case BFD_RELOC_MICROMIPS_GOT_DISP:
2659 case BFD_RELOC_MICROMIPS_HIGHEST:
2660 case BFD_RELOC_MICROMIPS_HIGHER:
2661 case BFD_RELOC_MICROMIPS_SCN_DISP:
2662 case BFD_RELOC_MICROMIPS_JALR:
2663 return TRUE;
2664
2665 default:
2666 return FALSE;
2667 }
2668}
2669
2309ddf2
MR
2670static inline bfd_boolean
2671jmp_reloc_p (bfd_reloc_code_real_type reloc)
2672{
2673 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
2674}
2675
738e5348
RS
2676static inline bfd_boolean
2677got16_reloc_p (bfd_reloc_code_real_type reloc)
2678{
2309ddf2 2679 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 2680 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
2681}
2682
2683static inline bfd_boolean
2684hi16_reloc_p (bfd_reloc_code_real_type reloc)
2685{
2309ddf2 2686 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 2687 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
2688}
2689
2690static inline bfd_boolean
2691lo16_reloc_p (bfd_reloc_code_real_type reloc)
2692{
2309ddf2 2693 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
2694 || reloc == BFD_RELOC_MICROMIPS_LO16);
2695}
2696
df58fc94
RS
2697static inline bfd_boolean
2698jalr_reloc_p (bfd_reloc_code_real_type reloc)
2699{
2309ddf2 2700 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
2701}
2702
5919d012 2703/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
2704 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
2705 need a matching %lo() when applied to local symbols. */
5919d012
RS
2706
2707static inline bfd_boolean
17a2f251 2708reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 2709{
3b91255e 2710 return (HAVE_IN_PLACE_ADDENDS
738e5348 2711 && (hi16_reloc_p (reloc)
0a44bf69
RS
2712 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2713 all GOT16 relocations evaluate to "G". */
738e5348
RS
2714 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
2715}
2716
2717/* Return the type of %lo() reloc needed by RELOC, given that
2718 reloc_needs_lo_p. */
2719
2720static inline bfd_reloc_code_real_type
2721matching_lo_reloc (bfd_reloc_code_real_type reloc)
2722{
df58fc94
RS
2723 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
2724 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
2725 : BFD_RELOC_LO16));
5919d012
RS
2726}
2727
2728/* Return true if the given fixup is followed by a matching R_MIPS_LO16
2729 relocation. */
2730
2731static inline bfd_boolean
17a2f251 2732fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
2733{
2734 return (fixp->fx_next != NULL
738e5348 2735 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
2736 && fixp->fx_addsy == fixp->fx_next->fx_addsy
2737 && fixp->fx_offset == fixp->fx_next->fx_offset);
2738}
2739
252b5132
RH
2740/* This function returns true if modifying a register requires a
2741 delay. */
2742
2743static int
17a2f251 2744reg_needs_delay (unsigned int reg)
252b5132
RH
2745{
2746 unsigned long prev_pinfo;
2747
47e39b9d 2748 prev_pinfo = history[0].insn_mo->pinfo;
252b5132 2749 if (! mips_opts.noreorder
81912461
ILT
2750 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2751 && ! gpr_interlocks)
2752 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2753 && ! cop_interlocks)))
252b5132 2754 {
81912461
ILT
2755 /* A load from a coprocessor or from memory. All load delays
2756 delay the use of general register rt for one instruction. */
bdaaa2e1 2757 /* Itbl support may require additional care here. */
252b5132 2758 know (prev_pinfo & INSN_WRITE_GPR_T);
df58fc94 2759 if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
252b5132
RH
2760 return 1;
2761 }
2762
2763 return 0;
2764}
2765
462427c4
RS
2766/* Move all labels in LABELS to the current insertion point. TEXT_P
2767 says whether the labels refer to text or data. */
404a8071
RS
2768
2769static void
462427c4 2770mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
2771{
2772 struct insn_label_list *l;
2773 valueT val;
2774
462427c4 2775 for (l = labels; l != NULL; l = l->next)
404a8071 2776 {
9c2799c2 2777 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
2778 symbol_set_frag (l->label, frag_now);
2779 val = (valueT) frag_now_fix ();
df58fc94 2780 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 2781 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
2782 ++val;
2783 S_SET_VALUE (l->label, val);
2784 }
2785}
2786
462427c4
RS
2787/* Move all labels in insn_labels to the current insertion point
2788 and treat them as text labels. */
2789
2790static void
2791mips_move_text_labels (void)
2792{
2793 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
2794}
2795
5f0fe04b
TS
2796static bfd_boolean
2797s_is_linkonce (symbolS *sym, segT from_seg)
2798{
2799 bfd_boolean linkonce = FALSE;
2800 segT symseg = S_GET_SEGMENT (sym);
2801
2802 if (symseg != from_seg && !S_IS_LOCAL (sym))
2803 {
2804 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2805 linkonce = TRUE;
2806#ifdef OBJ_ELF
2807 /* The GNU toolchain uses an extension for ELF: a section
2808 beginning with the magic string .gnu.linkonce is a
2809 linkonce section. */
2810 if (strncmp (segment_name (symseg), ".gnu.linkonce",
2811 sizeof ".gnu.linkonce" - 1) == 0)
2812 linkonce = TRUE;
2813#endif
2814 }
2815 return linkonce;
2816}
2817
df58fc94
RS
2818/* Mark instruction labels in MIPS16/microMIPS mode. This permits the
2819 linker to handle them specially, such as generating jalx instructions
2820 when needed. We also make them odd for the duration of the assembly,
2821 in order to generate the right sort of code. We will make them even
252b5132
RH
2822 in the adjust_symtab routine, while leaving them marked. This is
2823 convenient for the debugger and the disassembler. The linker knows
2824 to make them odd again. */
2825
2826static void
df58fc94 2827mips_compressed_mark_labels (void)
252b5132 2828{
a8dbcb85
TS
2829 segment_info_type *si = seg_info (now_seg);
2830 struct insn_label_list *l;
252b5132 2831
df58fc94 2832 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85
TS
2833
2834 for (l = si->label_list; l != NULL; l = l->next)
2835 {
2836 symbolS *label = l->label;
2837
2838#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
f43abd2b 2839 if (IS_ELF)
df58fc94
RS
2840 {
2841 if (mips_opts.mips16)
2842 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
2843 else
2844 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
2845 }
252b5132 2846#endif
5f0fe04b
TS
2847 if ((S_GET_VALUE (label) & 1) == 0
2848 /* Don't adjust the address if the label is global or weak, or
2849 in a link-once section, since we'll be emitting symbol reloc
2850 references to it which will be patched up by the linker, and
df58fc94 2851 the final value of the symbol may or may not be MIPS16/microMIPS. */
5f0fe04b
TS
2852 && ! S_IS_WEAK (label)
2853 && ! S_IS_EXTERNAL (label)
2854 && ! s_is_linkonce (label, now_seg))
a8dbcb85 2855 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
252b5132
RH
2856 }
2857}
2858
4d7206a2
RS
2859/* End the current frag. Make it a variant frag and record the
2860 relaxation info. */
2861
2862static void
2863relax_close_frag (void)
2864{
584892a6 2865 mips_macro_warning.first_frag = frag_now;
4d7206a2 2866 frag_var (rs_machine_dependent, 0, 0,
584892a6 2867 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
2868 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2869
2870 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2871 mips_relax.first_fixup = 0;
2872}
2873
2874/* Start a new relaxation sequence whose expansion depends on SYMBOL.
2875 See the comment above RELAX_ENCODE for more details. */
2876
2877static void
2878relax_start (symbolS *symbol)
2879{
9c2799c2 2880 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
2881 mips_relax.sequence = 1;
2882 mips_relax.symbol = symbol;
2883}
2884
2885/* Start generating the second version of a relaxable sequence.
2886 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
2887
2888static void
4d7206a2
RS
2889relax_switch (void)
2890{
9c2799c2 2891 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
2892 mips_relax.sequence = 2;
2893}
2894
2895/* End the current relaxable sequence. */
2896
2897static void
2898relax_end (void)
2899{
9c2799c2 2900 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
2901 relax_close_frag ();
2902 mips_relax.sequence = 0;
2903}
2904
11625dd8
RS
2905/* Return true if IP is a delayed branch or jump. */
2906
2907static inline bfd_boolean
2908delayed_branch_p (const struct mips_cl_insn *ip)
2909{
2910 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2911 | INSN_COND_BRANCH_DELAY
2912 | INSN_COND_BRANCH_LIKELY)) != 0;
2913}
2914
2915/* Return true if IP is a compact branch or jump. */
2916
2917static inline bfd_boolean
2918compact_branch_p (const struct mips_cl_insn *ip)
2919{
2920 if (mips_opts.mips16)
2921 return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
2922 | MIPS16_INSN_COND_BRANCH)) != 0;
2923 else
2924 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
2925 | INSN2_COND_BRANCH)) != 0;
2926}
2927
2928/* Return true if IP is an unconditional branch or jump. */
2929
2930static inline bfd_boolean
2931uncond_branch_p (const struct mips_cl_insn *ip)
2932{
2933 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
2934 || (mips_opts.mips16
2935 ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
2936 : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
2937}
2938
2939/* Return true if IP is a branch-likely instruction. */
2940
2941static inline bfd_boolean
2942branch_likely_p (const struct mips_cl_insn *ip)
2943{
2944 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
2945}
2946
14fe068b
RS
2947/* Return the type of nop that should be used to fill the delay slot
2948 of delayed branch IP. */
2949
2950static struct mips_cl_insn *
2951get_delay_slot_nop (const struct mips_cl_insn *ip)
2952{
2953 if (mips_opts.micromips
2954 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
2955 return &micromips_nop32_insn;
2956 return NOP_INSN;
2957}
2958
2309ddf2 2959/* Return the mask of core registers that IP reads or writes. */
df58fc94
RS
2960
2961static unsigned int
2962gpr_mod_mask (const struct mips_cl_insn *ip)
2963{
2309ddf2 2964 unsigned long pinfo2;
df58fc94
RS
2965 unsigned int mask;
2966
2967 mask = 0;
df58fc94
RS
2968 pinfo2 = ip->insn_mo->pinfo2;
2969 if (mips_opts.micromips)
2970 {
df58fc94
RS
2971 if (pinfo2 & INSN2_MOD_GPR_MD)
2972 mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
df58fc94
RS
2973 if (pinfo2 & INSN2_MOD_GPR_MF)
2974 mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
df58fc94
RS
2975 if (pinfo2 & INSN2_MOD_SP)
2976 mask |= 1 << SP;
2977 }
2978 return mask;
2979}
2980
4c260379
RS
2981/* Return the mask of core registers that IP reads. */
2982
2983static unsigned int
2984gpr_read_mask (const struct mips_cl_insn *ip)
2985{
2986 unsigned long pinfo, pinfo2;
2987 unsigned int mask;
2988
df58fc94 2989 mask = gpr_mod_mask (ip);
4c260379
RS
2990 pinfo = ip->insn_mo->pinfo;
2991 pinfo2 = ip->insn_mo->pinfo2;
2992 if (mips_opts.mips16)
2993 {
2994 if (pinfo & MIPS16_INSN_READ_X)
2995 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
2996 if (pinfo & MIPS16_INSN_READ_Y)
2997 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
2998 if (pinfo & MIPS16_INSN_READ_T)
2999 mask |= 1 << TREG;
3000 if (pinfo & MIPS16_INSN_READ_SP)
3001 mask |= 1 << SP;
3002 if (pinfo & MIPS16_INSN_READ_31)
3003 mask |= 1 << RA;
3004 if (pinfo & MIPS16_INSN_READ_Z)
3005 mask |= 1 << (mips16_to_32_reg_map
3006 [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3007 if (pinfo & MIPS16_INSN_READ_GPR_X)
3008 mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3009 }
3010 else
3011 {
3012 if (pinfo2 & INSN2_READ_GPR_D)
2309ddf2 3013 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3014 if (pinfo & INSN_READ_GPR_T)
2309ddf2 3015 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4c260379 3016 if (pinfo & INSN_READ_GPR_S)
2309ddf2
MR
3017 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3018 if (pinfo2 & INSN2_READ_GP)
3019 mask |= 1 << GP;
3020 if (pinfo2 & INSN2_READ_GPR_31)
3021 mask |= 1 << RA;
4c260379 3022 if (pinfo2 & INSN2_READ_GPR_Z)
2309ddf2 3023 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3024 }
2b0c8b40
MR
3025 if (mips_opts.micromips)
3026 {
3027 if (pinfo2 & INSN2_READ_GPR_MC)
3028 mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3029 if (pinfo2 & INSN2_READ_GPR_ME)
3030 mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3031 if (pinfo2 & INSN2_READ_GPR_MG)
3032 mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3033 if (pinfo2 & INSN2_READ_GPR_MJ)
3034 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3035 if (pinfo2 & INSN2_READ_GPR_MMN)
3036 {
3037 mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3038 mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3039 }
3040 if (pinfo2 & INSN2_READ_GPR_MP)
3041 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3042 if (pinfo2 & INSN2_READ_GPR_MQ)
3043 mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3044 }
fe35f09f
RS
3045 /* Don't include register 0. */
3046 return mask & ~1;
4c260379
RS
3047}
3048
3049/* Return the mask of core registers that IP writes. */
3050
3051static unsigned int
3052gpr_write_mask (const struct mips_cl_insn *ip)
3053{
3054 unsigned long pinfo, pinfo2;
3055 unsigned int mask;
3056
df58fc94 3057 mask = gpr_mod_mask (ip);
4c260379
RS
3058 pinfo = ip->insn_mo->pinfo;
3059 pinfo2 = ip->insn_mo->pinfo2;
3060 if (mips_opts.mips16)
3061 {
3062 if (pinfo & MIPS16_INSN_WRITE_X)
3063 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3064 if (pinfo & MIPS16_INSN_WRITE_Y)
3065 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3066 if (pinfo & MIPS16_INSN_WRITE_Z)
3067 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3068 if (pinfo & MIPS16_INSN_WRITE_T)
3069 mask |= 1 << TREG;
3070 if (pinfo & MIPS16_INSN_WRITE_SP)
3071 mask |= 1 << SP;
3072 if (pinfo & MIPS16_INSN_WRITE_31)
3073 mask |= 1 << RA;
3074 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3075 mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3076 }
3077 else
3078 {
3079 if (pinfo & INSN_WRITE_GPR_D)
df58fc94 3080 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3081 if (pinfo & INSN_WRITE_GPR_T)
df58fc94 3082 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
2b0c8b40 3083 if (pinfo & INSN_WRITE_GPR_S)
2309ddf2 3084 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379
RS
3085 if (pinfo & INSN_WRITE_GPR_31)
3086 mask |= 1 << RA;
3087 if (pinfo2 & INSN2_WRITE_GPR_Z)
df58fc94 3088 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3089 }
2b0c8b40
MR
3090 if (mips_opts.micromips)
3091 {
3092 if (pinfo2 & INSN2_WRITE_GPR_MB)
3093 mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3094 if (pinfo2 & INSN2_WRITE_GPR_MHI)
3095 {
3096 mask |= 1 << micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
3097 mask |= 1 << micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
3098 }
3099 if (pinfo2 & INSN2_WRITE_GPR_MJ)
3100 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3101 if (pinfo2 & INSN2_WRITE_GPR_MP)
3102 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3103 }
fe35f09f
RS
3104 /* Don't include register 0. */
3105 return mask & ~1;
4c260379
RS
3106}
3107
3108/* Return the mask of floating-point registers that IP reads. */
3109
3110static unsigned int
3111fpr_read_mask (const struct mips_cl_insn *ip)
3112{
3113 unsigned long pinfo, pinfo2;
3114 unsigned int mask;
3115
3116 mask = 0;
3117 pinfo = ip->insn_mo->pinfo;
3118 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3119 if (!mips_opts.mips16)
df58fc94
RS
3120 {
3121 if (pinfo2 & INSN2_READ_FPR_D)
2309ddf2 3122 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3123 if (pinfo & INSN_READ_FPR_S)
df58fc94 3124 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3125 if (pinfo & INSN_READ_FPR_T)
df58fc94 3126 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3127 if (pinfo & INSN_READ_FPR_R)
df58fc94 3128 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
4c260379 3129 if (pinfo2 & INSN2_READ_FPR_Z)
df58fc94 3130 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3131 }
3132 /* Conservatively treat all operands to an FP_D instruction are doubles.
3133 (This is overly pessimistic for things like cvt.d.s.) */
3134 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3135 mask |= mask << 1;
3136 return mask;
3137}
3138
3139/* Return the mask of floating-point registers that IP writes. */
3140
3141static unsigned int
3142fpr_write_mask (const struct mips_cl_insn *ip)
3143{
3144 unsigned long pinfo, pinfo2;
3145 unsigned int mask;
3146
3147 mask = 0;
3148 pinfo = ip->insn_mo->pinfo;
3149 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3150 if (!mips_opts.mips16)
4c260379
RS
3151 {
3152 if (pinfo & INSN_WRITE_FPR_D)
df58fc94 3153 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3154 if (pinfo & INSN_WRITE_FPR_S)
df58fc94 3155 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3156 if (pinfo & INSN_WRITE_FPR_T)
df58fc94 3157 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3158 if (pinfo2 & INSN2_WRITE_FPR_Z)
df58fc94 3159 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3160 }
3161 /* Conservatively treat all operands to an FP_D instruction are doubles.
3162 (This is overly pessimistic for things like cvt.s.d.) */
3163 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3164 mask |= mask << 1;
3165 return mask;
3166}
3167
71400594
RS
3168/* Classify an instruction according to the FIX_VR4120_* enumeration.
3169 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3170 by VR4120 errata. */
4d7206a2 3171
71400594
RS
3172static unsigned int
3173classify_vr4120_insn (const char *name)
252b5132 3174{
71400594
RS
3175 if (strncmp (name, "macc", 4) == 0)
3176 return FIX_VR4120_MACC;
3177 if (strncmp (name, "dmacc", 5) == 0)
3178 return FIX_VR4120_DMACC;
3179 if (strncmp (name, "mult", 4) == 0)
3180 return FIX_VR4120_MULT;
3181 if (strncmp (name, "dmult", 5) == 0)
3182 return FIX_VR4120_DMULT;
3183 if (strstr (name, "div"))
3184 return FIX_VR4120_DIV;
3185 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3186 return FIX_VR4120_MTHILO;
3187 return NUM_FIX_VR4120_CLASSES;
3188}
252b5132 3189
ff239038
CM
3190#define INSN_ERET 0x42000018
3191#define INSN_DERET 0x4200001f
3192
71400594
RS
3193/* Return the number of instructions that must separate INSN1 and INSN2,
3194 where INSN1 is the earlier instruction. Return the worst-case value
3195 for any INSN2 if INSN2 is null. */
252b5132 3196
71400594
RS
3197static unsigned int
3198insns_between (const struct mips_cl_insn *insn1,
3199 const struct mips_cl_insn *insn2)
3200{
3201 unsigned long pinfo1, pinfo2;
4c260379 3202 unsigned int mask;
71400594
RS
3203
3204 /* This function needs to know which pinfo flags are set for INSN2
3205 and which registers INSN2 uses. The former is stored in PINFO2 and
4c260379
RS
3206 the latter is tested via INSN2_USES_GPR. If INSN2 is null, PINFO2
3207 will have every flag set and INSN2_USES_GPR will always return true. */
71400594
RS
3208 pinfo1 = insn1->insn_mo->pinfo;
3209 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 3210
4c260379
RS
3211#define INSN2_USES_GPR(REG) \
3212 (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
71400594
RS
3213
3214 /* For most targets, write-after-read dependencies on the HI and LO
3215 registers must be separated by at least two instructions. */
3216 if (!hilo_interlocks)
252b5132 3217 {
71400594
RS
3218 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3219 return 2;
3220 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3221 return 2;
3222 }
3223
3224 /* If we're working around r7000 errata, there must be two instructions
3225 between an mfhi or mflo and any instruction that uses the result. */
3226 if (mips_7000_hilo_fix
df58fc94 3227 && !mips_opts.micromips
71400594 3228 && MF_HILO_INSN (pinfo1)
df58fc94 3229 && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
71400594
RS
3230 return 2;
3231
ff239038
CM
3232 /* If we're working around 24K errata, one instruction is required
3233 if an ERET or DERET is followed by a branch instruction. */
df58fc94 3234 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
3235 {
3236 if (insn1->insn_opcode == INSN_ERET
3237 || insn1->insn_opcode == INSN_DERET)
3238 {
3239 if (insn2 == NULL
3240 || insn2->insn_opcode == INSN_ERET
3241 || insn2->insn_opcode == INSN_DERET
11625dd8 3242 || delayed_branch_p (insn2))
ff239038
CM
3243 return 1;
3244 }
3245 }
3246
71400594
RS
3247 /* If working around VR4120 errata, check for combinations that need
3248 a single intervening instruction. */
df58fc94 3249 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
3250 {
3251 unsigned int class1, class2;
252b5132 3252
71400594
RS
3253 class1 = classify_vr4120_insn (insn1->insn_mo->name);
3254 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 3255 {
71400594
RS
3256 if (insn2 == NULL)
3257 return 1;
3258 class2 = classify_vr4120_insn (insn2->insn_mo->name);
3259 if (vr4120_conflicts[class1] & (1 << class2))
3260 return 1;
252b5132 3261 }
71400594
RS
3262 }
3263
df58fc94 3264 if (!HAVE_CODE_COMPRESSION)
71400594
RS
3265 {
3266 /* Check for GPR or coprocessor load delays. All such delays
3267 are on the RT register. */
3268 /* Itbl support may require additional care here. */
3269 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3270 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 3271 {
71400594 3272 know (pinfo1 & INSN_WRITE_GPR_T);
df58fc94 3273 if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
71400594
RS
3274 return 1;
3275 }
3276
3277 /* Check for generic coprocessor hazards.
3278
3279 This case is not handled very well. There is no special
3280 knowledge of CP0 handling, and the coprocessors other than
3281 the floating point unit are not distinguished at all. */
3282 /* Itbl support may require additional care here. FIXME!
3283 Need to modify this to include knowledge about
3284 user specified delays! */
3285 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3286 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3287 {
3288 /* Handle cases where INSN1 writes to a known general coprocessor
3289 register. There must be a one instruction delay before INSN2
3290 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
3291 mask = fpr_write_mask (insn1);
3292 if (mask != 0)
252b5132 3293 {
4c260379 3294 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 3295 return 1;
252b5132
RH
3296 }
3297 else
3298 {
71400594
RS
3299 /* Read-after-write dependencies on the control registers
3300 require a two-instruction gap. */
3301 if ((pinfo1 & INSN_WRITE_COND_CODE)
3302 && (pinfo2 & INSN_READ_COND_CODE))
3303 return 2;
3304
3305 /* We don't know exactly what INSN1 does. If INSN2 is
3306 also a coprocessor instruction, assume there must be
3307 a one instruction gap. */
3308 if (pinfo2 & INSN_COP)
3309 return 1;
252b5132
RH
3310 }
3311 }
6b76fefe 3312
71400594
RS
3313 /* Check for read-after-write dependencies on the coprocessor
3314 control registers in cases where INSN1 does not need a general
3315 coprocessor delay. This means that INSN1 is a floating point
3316 comparison instruction. */
3317 /* Itbl support may require additional care here. */
3318 else if (!cop_interlocks
3319 && (pinfo1 & INSN_WRITE_COND_CODE)
3320 && (pinfo2 & INSN_READ_COND_CODE))
3321 return 1;
3322 }
6b76fefe 3323
4c260379 3324#undef INSN2_USES_GPR
6b76fefe 3325
71400594
RS
3326 return 0;
3327}
6b76fefe 3328
7d8e00cf
RS
3329/* Return the number of nops that would be needed to work around the
3330 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
3331 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
3332 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
3333
3334static int
932d1a1b 3335nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
3336 const struct mips_cl_insn *insn)
3337{
4c260379
RS
3338 int i, j;
3339 unsigned int mask;
7d8e00cf
RS
3340
3341 /* Check if the instruction writes to HI or LO. MTHI and MTLO
3342 are not affected by the errata. */
3343 if (insn != 0
3344 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3345 || strcmp (insn->insn_mo->name, "mtlo") == 0
3346 || strcmp (insn->insn_mo->name, "mthi") == 0))
3347 return 0;
3348
3349 /* Search for the first MFLO or MFHI. */
3350 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 3351 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
3352 {
3353 /* Extract the destination register. */
4c260379 3354 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
3355
3356 /* No nops are needed if INSN reads that register. */
4c260379 3357 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
3358 return 0;
3359
3360 /* ...or if any of the intervening instructions do. */
3361 for (j = 0; j < i; j++)
4c260379 3362 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
3363 return 0;
3364
932d1a1b
RS
3365 if (i >= ignore)
3366 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
3367 }
3368 return 0;
3369}
3370
15be625d
CM
3371#define BASE_REG_EQ(INSN1, INSN2) \
3372 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3373 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3374
3375/* Return the minimum alignment for this store instruction. */
3376
3377static int
3378fix_24k_align_to (const struct mips_opcode *mo)
3379{
3380 if (strcmp (mo->name, "sh") == 0)
3381 return 2;
3382
3383 if (strcmp (mo->name, "swc1") == 0
3384 || strcmp (mo->name, "swc2") == 0
3385 || strcmp (mo->name, "sw") == 0
3386 || strcmp (mo->name, "sc") == 0
3387 || strcmp (mo->name, "s.s") == 0)
3388 return 4;
3389
3390 if (strcmp (mo->name, "sdc1") == 0
3391 || strcmp (mo->name, "sdc2") == 0
3392 || strcmp (mo->name, "s.d") == 0)
3393 return 8;
3394
3395 /* sb, swl, swr */
3396 return 1;
3397}
3398
3399struct fix_24k_store_info
3400 {
3401 /* Immediate offset, if any, for this store instruction. */
3402 short off;
3403 /* Alignment required by this store instruction. */
3404 int align_to;
3405 /* True for register offsets. */
3406 int register_offset;
3407 };
3408
3409/* Comparison function used by qsort. */
3410
3411static int
3412fix_24k_sort (const void *a, const void *b)
3413{
3414 const struct fix_24k_store_info *pos1 = a;
3415 const struct fix_24k_store_info *pos2 = b;
3416
3417 return (pos1->off - pos2->off);
3418}
3419
3420/* INSN is a store instruction. Try to record the store information
3421 in STINFO. Return false if the information isn't known. */
3422
3423static bfd_boolean
3424fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 3425 const struct mips_cl_insn *insn)
15be625d
CM
3426{
3427 /* The instruction must have a known offset. */
3428 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3429 return FALSE;
3430
3431 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3432 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3433 return TRUE;
3434}
3435
932d1a1b
RS
3436/* Return the number of nops that would be needed to work around the 24k
3437 "lost data on stores during refill" errata if instruction INSN
3438 immediately followed the 2 instructions described by HIST.
3439 Ignore hazards that are contained within the first IGNORE
3440 instructions of HIST.
3441
3442 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3443 for the data cache refills and store data. The following describes
3444 the scenario where the store data could be lost.
3445
3446 * A data cache miss, due to either a load or a store, causing fill
3447 data to be supplied by the memory subsystem
3448 * The first three doublewords of fill data are returned and written
3449 into the cache
3450 * A sequence of four stores occurs in consecutive cycles around the
3451 final doubleword of the fill:
3452 * Store A
3453 * Store B
3454 * Store C
3455 * Zero, One or more instructions
3456 * Store D
3457
3458 The four stores A-D must be to different doublewords of the line that
3459 is being filled. The fourth instruction in the sequence above permits
3460 the fill of the final doubleword to be transferred from the FSB into
3461 the cache. In the sequence above, the stores may be either integer
3462 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3463 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3464 different doublewords on the line. If the floating point unit is
3465 running in 1:2 mode, it is not possible to create the sequence above
3466 using only floating point store instructions.
15be625d
CM
3467
3468 In this case, the cache line being filled is incorrectly marked
3469 invalid, thereby losing the data from any store to the line that
3470 occurs between the original miss and the completion of the five
3471 cycle sequence shown above.
3472
932d1a1b 3473 The workarounds are:
15be625d 3474
932d1a1b
RS
3475 * Run the data cache in write-through mode.
3476 * Insert a non-store instruction between
3477 Store A and Store B or Store B and Store C. */
15be625d
CM
3478
3479static int
932d1a1b 3480nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
3481 const struct mips_cl_insn *insn)
3482{
3483 struct fix_24k_store_info pos[3];
3484 int align, i, base_offset;
3485
932d1a1b
RS
3486 if (ignore >= 2)
3487 return 0;
3488
ab9794cf
RS
3489 /* If the previous instruction wasn't a store, there's nothing to
3490 worry about. */
15be625d
CM
3491 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3492 return 0;
3493
ab9794cf
RS
3494 /* If the instructions after the previous one are unknown, we have
3495 to assume the worst. */
3496 if (!insn)
15be625d
CM
3497 return 1;
3498
ab9794cf
RS
3499 /* Check whether we are dealing with three consecutive stores. */
3500 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3501 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
3502 return 0;
3503
3504 /* If we don't know the relationship between the store addresses,
3505 assume the worst. */
ab9794cf 3506 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
3507 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3508 return 1;
3509
3510 if (!fix_24k_record_store_info (&pos[0], insn)
3511 || !fix_24k_record_store_info (&pos[1], &hist[0])
3512 || !fix_24k_record_store_info (&pos[2], &hist[1]))
3513 return 1;
3514
3515 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3516
3517 /* Pick a value of ALIGN and X such that all offsets are adjusted by
3518 X bytes and such that the base register + X is known to be aligned
3519 to align bytes. */
3520
3521 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3522 align = 8;
3523 else
3524 {
3525 align = pos[0].align_to;
3526 base_offset = pos[0].off;
3527 for (i = 1; i < 3; i++)
3528 if (align < pos[i].align_to)
3529 {
3530 align = pos[i].align_to;
3531 base_offset = pos[i].off;
3532 }
3533 for (i = 0; i < 3; i++)
3534 pos[i].off -= base_offset;
3535 }
3536
3537 pos[0].off &= ~align + 1;
3538 pos[1].off &= ~align + 1;
3539 pos[2].off &= ~align + 1;
3540
3541 /* If any two stores write to the same chunk, they also write to the
3542 same doubleword. The offsets are still sorted at this point. */
3543 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3544 return 0;
3545
3546 /* A range of at least 9 bytes is needed for the stores to be in
3547 non-overlapping doublewords. */
3548 if (pos[2].off - pos[0].off <= 8)
3549 return 0;
3550
3551 if (pos[2].off - pos[1].off >= 24
3552 || pos[1].off - pos[0].off >= 24
3553 || pos[2].off - pos[0].off >= 32)
3554 return 0;
3555
3556 return 1;
3557}
3558
71400594 3559/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 3560 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
3561 where HIST[0] is the most recent instruction. Ignore hazards
3562 between INSN and the first IGNORE instructions in HIST.
3563
3564 If INSN is null, return the worse-case number of nops for any
3565 instruction. */
bdaaa2e1 3566
71400594 3567static int
932d1a1b 3568nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
3569 const struct mips_cl_insn *insn)
3570{
3571 int i, nops, tmp_nops;
bdaaa2e1 3572
71400594 3573 nops = 0;
932d1a1b 3574 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 3575 {
91d6fa6a 3576 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
3577 if (tmp_nops > nops)
3578 nops = tmp_nops;
3579 }
7d8e00cf 3580
df58fc94 3581 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 3582 {
932d1a1b 3583 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
3584 if (tmp_nops > nops)
3585 nops = tmp_nops;
3586 }
3587
df58fc94 3588 if (mips_fix_24k && !mips_opts.micromips)
15be625d 3589 {
932d1a1b 3590 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
3591 if (tmp_nops > nops)
3592 nops = tmp_nops;
3593 }
3594
71400594
RS
3595 return nops;
3596}
252b5132 3597
71400594 3598/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 3599 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
3600 would be needed after the extended sequence, ignoring hazards
3601 in the first IGNORE instructions. */
252b5132 3602
71400594 3603static int
932d1a1b
RS
3604nops_for_sequence (int num_insns, int ignore,
3605 const struct mips_cl_insn *hist, ...)
71400594
RS
3606{
3607 va_list args;
3608 struct mips_cl_insn buffer[MAX_NOPS];
3609 struct mips_cl_insn *cursor;
3610 int nops;
3611
91d6fa6a 3612 va_start (args, hist);
71400594 3613 cursor = buffer + num_insns;
91d6fa6a 3614 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
3615 while (cursor > buffer)
3616 *--cursor = *va_arg (args, const struct mips_cl_insn *);
3617
932d1a1b 3618 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
3619 va_end (args);
3620 return nops;
3621}
252b5132 3622
71400594
RS
3623/* Like nops_for_insn, but if INSN is a branch, take into account the
3624 worst-case delay for the branch target. */
252b5132 3625
71400594 3626static int
932d1a1b 3627nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
3628 const struct mips_cl_insn *insn)
3629{
3630 int nops, tmp_nops;
60b63b72 3631
932d1a1b 3632 nops = nops_for_insn (ignore, hist, insn);
11625dd8 3633 if (delayed_branch_p (insn))
71400594 3634 {
932d1a1b 3635 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 3636 hist, insn, get_delay_slot_nop (insn));
71400594
RS
3637 if (tmp_nops > nops)
3638 nops = tmp_nops;
3639 }
11625dd8 3640 else if (compact_branch_p (insn))
71400594 3641 {
932d1a1b 3642 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
3643 if (tmp_nops > nops)
3644 nops = tmp_nops;
3645 }
3646 return nops;
3647}
3648
c67a084a
NC
3649/* Fix NOP issue: Replace nops by "or at,at,zero". */
3650
3651static void
3652fix_loongson2f_nop (struct mips_cl_insn * ip)
3653{
df58fc94 3654 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
3655 if (strcmp (ip->insn_mo->name, "nop") == 0)
3656 ip->insn_opcode = LOONGSON2F_NOP_INSN;
3657}
3658
3659/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
3660 jr target pc &= 'hffff_ffff_cfff_ffff. */
3661
3662static void
3663fix_loongson2f_jump (struct mips_cl_insn * ip)
3664{
df58fc94 3665 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
3666 if (strcmp (ip->insn_mo->name, "j") == 0
3667 || strcmp (ip->insn_mo->name, "jr") == 0
3668 || strcmp (ip->insn_mo->name, "jalr") == 0)
3669 {
3670 int sreg;
3671 expressionS ep;
3672
3673 if (! mips_opts.at)
3674 return;
3675
df58fc94 3676 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
3677 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
3678 return;
3679
3680 ep.X_op = O_constant;
3681 ep.X_add_number = 0xcfff0000;
3682 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
3683 ep.X_add_number = 0xffff;
3684 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
3685 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
3686 }
3687}
3688
3689static void
3690fix_loongson2f (struct mips_cl_insn * ip)
3691{
3692 if (mips_fix_loongson2f_nop)
3693 fix_loongson2f_nop (ip);
3694
3695 if (mips_fix_loongson2f_jump)
3696 fix_loongson2f_jump (ip);
3697}
3698
a4e06468
RS
3699/* IP is a branch that has a delay slot, and we need to fill it
3700 automatically. Return true if we can do that by swapping IP
3701 with the previous instruction. */
3702
3703static bfd_boolean
3704can_swap_branch_p (struct mips_cl_insn *ip)
3705{
2b0c8b40 3706 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468
RS
3707 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
3708
3709 /* -O2 and above is required for this optimization. */
3710 if (mips_optimize < 2)
3711 return FALSE;
3712
3713 /* If we have seen .set volatile or .set nomove, don't optimize. */
3714 if (mips_opts.nomove)
3715 return FALSE;
3716
3717 /* We can't swap if the previous instruction's position is fixed. */
3718 if (history[0].fixed_p)
3719 return FALSE;
3720
3721 /* If the previous previous insn was in a .set noreorder, we can't
3722 swap. Actually, the MIPS assembler will swap in this situation.
3723 However, gcc configured -with-gnu-as will generate code like
3724
3725 .set noreorder
3726 lw $4,XXX
3727 .set reorder
3728 INSN
3729 bne $4,$0,foo
3730
3731 in which we can not swap the bne and INSN. If gcc is not configured
3732 -with-gnu-as, it does not output the .set pseudo-ops. */
3733 if (history[1].noreorder_p)
3734 return FALSE;
3735
87333bb7
MR
3736 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
3737 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
3738 if (mips_opts.mips16 && history[0].fixp[0])
3739 return FALSE;
3740
3741 /* If the branch is itself the target of a branch, we can not swap.
3742 We cheat on this; all we check for is whether there is a label on
3743 this instruction. If there are any branches to anything other than
3744 a label, users must use .set noreorder. */
3745 if (seg_info (now_seg)->label_list)
3746 return FALSE;
3747
3748 /* If the previous instruction is in a variant frag other than this
2309ddf2 3749 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
3750 MIPS16 code, which uses variant frags for different purposes. */
3751 if (!mips_opts.mips16
a4e06468
RS
3752 && history[0].frag
3753 && history[0].frag->fr_type == rs_machine_dependent)
3754 return FALSE;
3755
bcd530a7
RS
3756 /* We do not swap with instructions that cannot architecturally
3757 be placed in a branch delay slot, such as SYNC or ERET. We
3758 also refrain from swapping with a trap instruction, since it
3759 complicates trap handlers to have the trap instruction be in
3760 a delay slot. */
a4e06468 3761 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 3762 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
3763 return FALSE;
3764
3765 /* Check for conflicts between the branch and the instructions
3766 before the candidate delay slot. */
3767 if (nops_for_insn (0, history + 1, ip) > 0)
3768 return FALSE;
3769
3770 /* Check for conflicts between the swapped sequence and the
3771 target of the branch. */
3772 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
3773 return FALSE;
3774
3775 /* If the branch reads a register that the previous
3776 instruction sets, we can not swap. */
3777 gpr_read = gpr_read_mask (ip);
3778 prev_gpr_write = gpr_write_mask (&history[0]);
3779 if (gpr_read & prev_gpr_write)
3780 return FALSE;
3781
3782 /* If the branch writes a register that the previous
3783 instruction sets, we can not swap. */
3784 gpr_write = gpr_write_mask (ip);
3785 if (gpr_write & prev_gpr_write)
3786 return FALSE;
3787
3788 /* If the branch writes a register that the previous
3789 instruction reads, we can not swap. */
3790 prev_gpr_read = gpr_read_mask (&history[0]);
3791 if (gpr_write & prev_gpr_read)
3792 return FALSE;
3793
3794 /* If one instruction sets a condition code and the
3795 other one uses a condition code, we can not swap. */
3796 pinfo = ip->insn_mo->pinfo;
3797 if ((pinfo & INSN_READ_COND_CODE)
3798 && (prev_pinfo & INSN_WRITE_COND_CODE))
3799 return FALSE;
3800 if ((pinfo & INSN_WRITE_COND_CODE)
3801 && (prev_pinfo & INSN_READ_COND_CODE))
3802 return FALSE;
3803
3804 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 3805 prev_pinfo2 = history[0].insn_mo->pinfo2;
a4e06468
RS
3806 if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
3807 return FALSE;
2b0c8b40
MR
3808 if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
3809 return FALSE;
a4e06468 3810
df58fc94
RS
3811 /* If the previous instruction has an incorrect size for a fixed
3812 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
3813 pinfo2 = ip->insn_mo->pinfo2;
3814 if (mips_opts.micromips
3815 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
3816 && insn_length (history) != 2)
3817 return FALSE;
3818 if (mips_opts.micromips
3819 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
3820 && insn_length (history) != 4)
3821 return FALSE;
3822
a4e06468
RS
3823 return TRUE;
3824}
3825
3826/* Decide how we should add IP to the instruction stream. */
3827
3828static enum append_method
3829get_append_method (struct mips_cl_insn *ip)
3830{
3831 unsigned long pinfo;
3832
3833 /* The relaxed version of a macro sequence must be inherently
3834 hazard-free. */
3835 if (mips_relax.sequence == 2)
3836 return APPEND_ADD;
3837
3838 /* We must not dabble with instructions in a ".set norerorder" block. */
3839 if (mips_opts.noreorder)
3840 return APPEND_ADD;
3841
3842 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 3843 if (delayed_branch_p (ip))
a4e06468 3844 {
11625dd8 3845 if (!branch_likely_p (ip) && can_swap_branch_p (ip))
a4e06468
RS
3846 return APPEND_SWAP;
3847
11625dd8 3848 pinfo = ip->insn_mo->pinfo;
a4e06468
RS
3849 if (mips_opts.mips16
3850 && ISA_SUPPORTS_MIPS16E
a4e06468
RS
3851 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
3852 return APPEND_ADD_COMPACT;
3853
3854 return APPEND_ADD_WITH_NOP;
3855 }
3856
a4e06468
RS
3857 return APPEND_ADD;
3858}
3859
ceb94aa5
RS
3860/* IP is a MIPS16 instruction whose opcode we have just changed.
3861 Point IP->insn_mo to the new opcode's definition. */
3862
3863static void
3864find_altered_mips16_opcode (struct mips_cl_insn *ip)
3865{
3866 const struct mips_opcode *mo, *end;
3867
3868 end = &mips16_opcodes[bfd_mips16_num_opcodes];
3869 for (mo = ip->insn_mo; mo < end; mo++)
3870 if ((ip->insn_opcode & mo->mask) == mo->match)
3871 {
3872 ip->insn_mo = mo;
3873 return;
3874 }
3875 abort ();
3876}
3877
df58fc94
RS
3878/* For microMIPS macros, we need to generate a local number label
3879 as the target of branches. */
3880#define MICROMIPS_LABEL_CHAR '\037'
3881static unsigned long micromips_target_label;
3882static char micromips_target_name[32];
3883
3884static char *
3885micromips_label_name (void)
3886{
3887 char *p = micromips_target_name;
3888 char symbol_name_temporary[24];
3889 unsigned long l;
3890 int i;
3891
3892 if (*p)
3893 return p;
3894
3895 i = 0;
3896 l = micromips_target_label;
3897#ifdef LOCAL_LABEL_PREFIX
3898 *p++ = LOCAL_LABEL_PREFIX;
3899#endif
3900 *p++ = 'L';
3901 *p++ = MICROMIPS_LABEL_CHAR;
3902 do
3903 {
3904 symbol_name_temporary[i++] = l % 10 + '0';
3905 l /= 10;
3906 }
3907 while (l != 0);
3908 while (i > 0)
3909 *p++ = symbol_name_temporary[--i];
3910 *p = '\0';
3911
3912 return micromips_target_name;
3913}
3914
3915static void
3916micromips_label_expr (expressionS *label_expr)
3917{
3918 label_expr->X_op = O_symbol;
3919 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
3920 label_expr->X_add_number = 0;
3921}
3922
3923static void
3924micromips_label_inc (void)
3925{
3926 micromips_target_label++;
3927 *micromips_target_name = '\0';
3928}
3929
3930static void
3931micromips_add_label (void)
3932{
3933 symbolS *s;
3934
3935 s = colon (micromips_label_name ());
3936 micromips_label_inc ();
3937#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
3938 if (IS_ELF)
3939 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
db9b2be4
AM
3940#else
3941 (void) s;
df58fc94
RS
3942#endif
3943}
3944
3945/* If assembling microMIPS code, then return the microMIPS reloc
3946 corresponding to the requested one if any. Otherwise return
3947 the reloc unchanged. */
3948
3949static bfd_reloc_code_real_type
3950micromips_map_reloc (bfd_reloc_code_real_type reloc)
3951{
3952 static const bfd_reloc_code_real_type relocs[][2] =
3953 {
3954 /* Keep sorted incrementally by the left-hand key. */
3955 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
3956 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
3957 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
3958 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
3959 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
3960 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
3961 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
3962 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
3963 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
3964 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
3965 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
3966 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
3967 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
3968 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
3969 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
3970 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
3971 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
3972 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
3973 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
3974 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
3975 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
3976 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
3977 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
3978 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
3979 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
3980 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
3981 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
3982 };
3983 bfd_reloc_code_real_type r;
3984 size_t i;
3985
3986 if (!mips_opts.micromips)
3987 return reloc;
3988 for (i = 0; i < ARRAY_SIZE (relocs); i++)
3989 {
3990 r = relocs[i][0];
3991 if (r > reloc)
3992 return reloc;
3993 if (r == reloc)
3994 return relocs[i][1];
3995 }
3996 return reloc;
3997}
3998
71400594
RS
3999/* Output an instruction. IP is the instruction information.
4000 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
4001 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
4002 a macro expansion. */
71400594
RS
4003
4004static void
4005append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 4006 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 4007{
14fe068b 4008 unsigned long prev_pinfo2, pinfo;
71400594 4009 bfd_boolean relaxed_branch = FALSE;
a4e06468 4010 enum append_method method;
2309ddf2 4011 bfd_boolean relax32;
2b0c8b40 4012 int branch_disp;
71400594 4013
2309ddf2 4014 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
4015 fix_loongson2f (ip);
4016
df58fc94 4017 mips_mark_labels ();
71400594 4018
738f4d98 4019 file_ase_mips16 |= mips_opts.mips16;
df58fc94 4020 file_ase_micromips |= mips_opts.micromips;
738f4d98 4021
df58fc94 4022 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 4023 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
4024
4025 if (mips_opts.micromips
4026 && !expansionp
4027 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4028 && micromips_insn_length (ip->insn_mo) != 2)
4029 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4030 && micromips_insn_length (ip->insn_mo) != 4)))
4031 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4032 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 4033
15be625d
CM
4034 if (address_expr == NULL)
4035 ip->complete_p = 1;
4036 else if (*reloc_type <= BFD_RELOC_UNUSED
4037 && address_expr->X_op == O_constant)
4038 {
4039 unsigned int tmp;
4040
4041 ip->complete_p = 1;
4042 switch (*reloc_type)
4043 {
4044 case BFD_RELOC_32:
4045 ip->insn_opcode |= address_expr->X_add_number;
4046 break;
4047
4048 case BFD_RELOC_MIPS_HIGHEST:
4049 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
4050 ip->insn_opcode |= tmp & 0xffff;
4051 break;
4052
4053 case BFD_RELOC_MIPS_HIGHER:
4054 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
4055 ip->insn_opcode |= tmp & 0xffff;
4056 break;
4057
4058 case BFD_RELOC_HI16_S:
4059 tmp = (address_expr->X_add_number + 0x8000) >> 16;
4060 ip->insn_opcode |= tmp & 0xffff;
4061 break;
4062
4063 case BFD_RELOC_HI16:
4064 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
4065 break;
4066
4067 case BFD_RELOC_UNUSED:
4068 case BFD_RELOC_LO16:
4069 case BFD_RELOC_MIPS_GOT_DISP:
4070 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
4071 break;
4072
4073 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
4074 {
4075 int shift;
4076
4077 shift = mips_opts.micromips ? 1 : 2;
4078 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4079 as_bad (_("jump to misaligned address (0x%lx)"),
4080 (unsigned long) address_expr->X_add_number);
4081 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4082 & 0x3ffffff);
4083 ip->complete_p = 0;
4084 }
15be625d
CM
4085 break;
4086
4087 case BFD_RELOC_MIPS16_JMP:
4088 if ((address_expr->X_add_number & 3) != 0)
4089 as_bad (_("jump to misaligned address (0x%lx)"),
4090 (unsigned long) address_expr->X_add_number);
4091 ip->insn_opcode |=
4092 (((address_expr->X_add_number & 0x7c0000) << 3)
4093 | ((address_expr->X_add_number & 0xf800000) >> 7)
4094 | ((address_expr->X_add_number & 0x3fffc) >> 2));
4095 ip->complete_p = 0;
4096 break;
4097
4098 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
4099 {
4100 int shift;
4101
4102 shift = mips_opts.micromips ? 1 : 2;
4103 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4104 as_bad (_("branch to misaligned address (0x%lx)"),
4105 (unsigned long) address_expr->X_add_number);
4106 if (!mips_relax_branch)
4107 {
4108 if ((address_expr->X_add_number + (1 << (shift + 15)))
4109 & ~((1 << (shift + 16)) - 1))
4110 as_bad (_("branch address range overflow (0x%lx)"),
4111 (unsigned long) address_expr->X_add_number);
4112 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4113 & 0xffff);
4114 }
4115 ip->complete_p = 0;
4116 }
15be625d
CM
4117 break;
4118
4119 default:
4120 internalError ();
4121 }
4122 }
4123
71400594
RS
4124 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4125 {
4126 /* There are a lot of optimizations we could do that we don't.
4127 In particular, we do not, in general, reorder instructions.
4128 If you use gcc with optimization, it will reorder
4129 instructions and generally do much more optimization then we
4130 do here; repeating all that work in the assembler would only
4131 benefit hand written assembly code, and does not seem worth
4132 it. */
4133 int nops = (mips_optimize == 0
932d1a1b
RS
4134 ? nops_for_insn (0, history, NULL)
4135 : nops_for_insn_or_target (0, history, ip));
71400594 4136 if (nops > 0)
252b5132
RH
4137 {
4138 fragS *old_frag;
4139 unsigned long old_frag_offset;
4140 int i;
252b5132
RH
4141
4142 old_frag = frag_now;
4143 old_frag_offset = frag_now_fix ();
4144
4145 for (i = 0; i < nops; i++)
14fe068b
RS
4146 add_fixed_insn (NOP_INSN);
4147 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
4148
4149 if (listing)
4150 {
4151 listing_prev_line ();
4152 /* We may be at the start of a variant frag. In case we
4153 are, make sure there is enough space for the frag
4154 after the frags created by listing_prev_line. The
4155 argument to frag_grow here must be at least as large
4156 as the argument to all other calls to frag_grow in
4157 this file. We don't have to worry about being in the
4158 middle of a variant frag, because the variants insert
4159 all needed nop instructions themselves. */
4160 frag_grow (40);
4161 }
4162
462427c4 4163 mips_move_text_labels ();
252b5132
RH
4164
4165#ifndef NO_ECOFF_DEBUGGING
4166 if (ECOFF_DEBUGGING)
4167 ecoff_fix_loc (old_frag, old_frag_offset);
4168#endif
4169 }
71400594
RS
4170 }
4171 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4172 {
932d1a1b
RS
4173 int nops;
4174
4175 /* Work out how many nops in prev_nop_frag are needed by IP,
4176 ignoring hazards generated by the first prev_nop_frag_since
4177 instructions. */
4178 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 4179 gas_assert (nops <= prev_nop_frag_holds);
252b5132 4180
71400594
RS
4181 /* Enforce NOPS as a minimum. */
4182 if (nops > prev_nop_frag_required)
4183 prev_nop_frag_required = nops;
252b5132 4184
71400594
RS
4185 if (prev_nop_frag_holds == prev_nop_frag_required)
4186 {
4187 /* Settle for the current number of nops. Update the history
4188 accordingly (for the benefit of any future .set reorder code). */
4189 prev_nop_frag = NULL;
4190 insert_into_history (prev_nop_frag_since,
4191 prev_nop_frag_holds, NOP_INSN);
4192 }
4193 else
4194 {
4195 /* Allow this instruction to replace one of the nops that was
4196 tentatively added to prev_nop_frag. */
df58fc94 4197 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
4198 prev_nop_frag_holds--;
4199 prev_nop_frag_since++;
252b5132
RH
4200 }
4201 }
4202
a4e06468 4203 method = get_append_method (ip);
2b0c8b40 4204 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 4205
58e2ea4d
MR
4206#ifdef OBJ_ELF
4207 /* The value passed to dwarf2_emit_insn is the distance between
4208 the beginning of the current instruction and the address that
e3a82c8e
MR
4209 should be recorded in the debug tables. This is normally the
4210 current address.
4211
df58fc94
RS
4212 For MIPS16/microMIPS debug info we want to use ISA-encoded
4213 addresses, so we use -1 for an address higher by one than the
4214 current one.
e3a82c8e
MR
4215
4216 If the instruction produced is a branch that we will swap with
4217 the preceding instruction, then we add the displacement by which
4218 the branch will be moved backwards. This is more appropriate
2309ddf2
MR
4219 and for MIPS16/microMIPS code also prevents a debugger from
4220 placing a breakpoint in the middle of the branch (and corrupting
4221 code if software breakpoints are used). */
2b0c8b40 4222 dwarf2_emit_insn ((HAVE_CODE_COMPRESSION ? -1 : 0) + branch_disp);
58e2ea4d
MR
4223#endif
4224
df58fc94
RS
4225 relax32 = (mips_relax_branch
4226 /* Don't try branch relaxation within .set nomacro, or within
4227 .set noat if we use $at for PIC computations. If it turns
4228 out that the branch was out-of-range, we'll get an error. */
4229 && !mips_opts.warn_about_macros
4230 && (mips_opts.at || mips_pic == NO_PIC)
4231 /* Don't relax BPOSGE32/64 as they have no complementing
4232 branches. */
40209cad 4233 && !(ip->insn_mo->membership & (INSN_DSP64 | INSN_DSP)));
df58fc94
RS
4234
4235 if (!HAVE_CODE_COMPRESSION
4236 && address_expr
4237 && relax32
0b25d3e6 4238 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 4239 && delayed_branch_p (ip))
4a6a3df4 4240 {
895921c9 4241 relaxed_branch = TRUE;
1e915849
RS
4242 add_relaxed_insn (ip, (relaxed_branch_length
4243 (NULL, NULL,
11625dd8
RS
4244 uncond_branch_p (ip) ? -1
4245 : branch_likely_p (ip) ? 1
1e915849
RS
4246 : 0)), 4,
4247 RELAX_BRANCH_ENCODE
66b3e8da 4248 (AT,
11625dd8
RS
4249 uncond_branch_p (ip),
4250 branch_likely_p (ip),
1e915849
RS
4251 pinfo & INSN_WRITE_GPR_31,
4252 0),
4253 address_expr->X_add_symbol,
4254 address_expr->X_add_number);
4a6a3df4
AO
4255 *reloc_type = BFD_RELOC_UNUSED;
4256 }
df58fc94
RS
4257 else if (mips_opts.micromips
4258 && address_expr
4259 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4260 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
4261 && (delayed_branch_p (ip) || compact_branch_p (ip))
4262 /* Don't try branch relaxation when users specify
4263 16-bit/32-bit instructions. */
4264 && !forced_insn_length)
df58fc94
RS
4265 {
4266 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4267 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
4268 int uncond = uncond_branch_p (ip) ? -1 : 0;
4269 int compact = compact_branch_p (ip);
df58fc94
RS
4270 int al = pinfo & INSN_WRITE_GPR_31;
4271 int length32;
4272
4273 gas_assert (address_expr != NULL);
4274 gas_assert (!mips_relax.sequence);
4275
2b0c8b40 4276 relaxed_branch = TRUE;
df58fc94
RS
4277 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4278 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
4279 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4280 relax32, 0, 0),
df58fc94
RS
4281 address_expr->X_add_symbol,
4282 address_expr->X_add_number);
4283 *reloc_type = BFD_RELOC_UNUSED;
4284 }
4285 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
4286 {
4287 /* We need to set up a variant frag. */
df58fc94 4288 gas_assert (address_expr != NULL);
1e915849
RS
4289 add_relaxed_insn (ip, 4, 0,
4290 RELAX_MIPS16_ENCODE
4291 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 4292 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 4293 delayed_branch_p (&history[0]),
1e915849
RS
4294 history[0].mips16_absolute_jump_p),
4295 make_expr_symbol (address_expr), 0);
252b5132 4296 }
252b5132
RH
4297 else if (mips_opts.mips16
4298 && ! ip->use_extend
f6688943 4299 && *reloc_type != BFD_RELOC_MIPS16_JMP)
9497f5ac 4300 {
11625dd8 4301 if (!delayed_branch_p (ip))
b8ee1a6e
DU
4302 /* Make sure there is enough room to swap this instruction with
4303 a following jump instruction. */
4304 frag_grow (6);
1e915849 4305 add_fixed_insn (ip);
252b5132
RH
4306 }
4307 else
4308 {
4309 if (mips_opts.mips16
4310 && mips_opts.noreorder
11625dd8 4311 && delayed_branch_p (&history[0]))
252b5132
RH
4312 as_warn (_("extended instruction in delay slot"));
4313
4d7206a2
RS
4314 if (mips_relax.sequence)
4315 {
4316 /* If we've reached the end of this frag, turn it into a variant
4317 frag and record the information for the instructions we've
4318 written so far. */
4319 if (frag_room () < 4)
4320 relax_close_frag ();
df58fc94 4321 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
4322 }
4323
584892a6 4324 if (mips_relax.sequence != 2)
df58fc94
RS
4325 {
4326 if (mips_macro_warning.first_insn_sizes[0] == 0)
4327 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4328 mips_macro_warning.sizes[0] += insn_length (ip);
4329 mips_macro_warning.insns[0]++;
4330 }
584892a6 4331 if (mips_relax.sequence != 1)
df58fc94
RS
4332 {
4333 if (mips_macro_warning.first_insn_sizes[1] == 0)
4334 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4335 mips_macro_warning.sizes[1] += insn_length (ip);
4336 mips_macro_warning.insns[1]++;
4337 }
584892a6 4338
1e915849
RS
4339 if (mips_opts.mips16)
4340 {
4341 ip->fixed_p = 1;
4342 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4343 }
4344 add_fixed_insn (ip);
252b5132
RH
4345 }
4346
9fe77896 4347 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 4348 {
df58fc94 4349 bfd_reloc_code_real_type final_type[3];
2309ddf2 4350 reloc_howto_type *howto0;
9fe77896
RS
4351 reloc_howto_type *howto;
4352 int i;
34ce925e 4353
df58fc94
RS
4354 /* Perform any necessary conversion to microMIPS relocations
4355 and find out how many relocations there actually are. */
4356 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4357 final_type[i] = micromips_map_reloc (reloc_type[i]);
4358
9fe77896
RS
4359 /* In a compound relocation, it is the final (outermost)
4360 operator that determines the relocated field. */
2309ddf2
MR
4361 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
4362
9fe77896
RS
4363 if (howto == NULL)
4364 {
4365 /* To reproduce this failure try assembling gas/testsuites/
4366 gas/mips/mips16-intermix.s with a mips-ecoff targeted
4367 assembler. */
df58fc94
RS
4368 as_bad (_("Unsupported MIPS relocation number %d"),
4369 final_type[i - 1]);
9fe77896
RS
4370 howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
4371 }
2309ddf2
MR
4372
4373 if (i > 1)
4374 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
4375 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4376 bfd_get_reloc_size (howto),
4377 address_expr,
2309ddf2
MR
4378 howto0 && howto0->pc_relative,
4379 final_type[0]);
9fe77896
RS
4380
4381 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 4382 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
4383 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4384
4385 /* These relocations can have an addend that won't fit in
4386 4 octets for 64bit assembly. */
4387 if (HAVE_64BIT_GPRS
4388 && ! howto->partial_inplace
4389 && (reloc_type[0] == BFD_RELOC_16
4390 || reloc_type[0] == BFD_RELOC_32
4391 || reloc_type[0] == BFD_RELOC_MIPS_JMP
4392 || reloc_type[0] == BFD_RELOC_GPREL16
4393 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4394 || reloc_type[0] == BFD_RELOC_GPREL32
4395 || reloc_type[0] == BFD_RELOC_64
4396 || reloc_type[0] == BFD_RELOC_CTOR
4397 || reloc_type[0] == BFD_RELOC_MIPS_SUB
4398 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4399 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4400 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4401 || reloc_type[0] == BFD_RELOC_MIPS_REL16
4402 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4403 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4404 || hi16_reloc_p (reloc_type[0])
4405 || lo16_reloc_p (reloc_type[0])))
4406 ip->fixp[0]->fx_no_overflow = 1;
4407
4408 if (mips_relax.sequence)
4409 {
4410 if (mips_relax.first_fixup == 0)
4411 mips_relax.first_fixup = ip->fixp[0];
4412 }
4413 else if (reloc_needs_lo_p (*reloc_type))
4414 {
4415 struct mips_hi_fixup *hi_fixup;
4416
4417 /* Reuse the last entry if it already has a matching %lo. */
4418 hi_fixup = mips_hi_fixup_list;
4419 if (hi_fixup == 0
4420 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 4421 {
9fe77896
RS
4422 hi_fixup = ((struct mips_hi_fixup *)
4423 xmalloc (sizeof (struct mips_hi_fixup)));
4424 hi_fixup->next = mips_hi_fixup_list;
4425 mips_hi_fixup_list = hi_fixup;
4d7206a2 4426 }
9fe77896
RS
4427 hi_fixup->fixp = ip->fixp[0];
4428 hi_fixup->seg = now_seg;
4429 }
252b5132 4430
9fe77896
RS
4431 /* Add fixups for the second and third relocations, if given.
4432 Note that the ABI allows the second relocation to be
4433 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
4434 moment we only use RSS_UNDEF, but we could add support
4435 for the others if it ever becomes necessary. */
4436 for (i = 1; i < 3; i++)
4437 if (reloc_type[i] != BFD_RELOC_UNUSED)
4438 {
4439 ip->fixp[i] = fix_new (ip->frag, ip->where,
4440 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 4441 FALSE, final_type[i]);
f6688943 4442
9fe77896
RS
4443 /* Use fx_tcbit to mark compound relocs. */
4444 ip->fixp[0]->fx_tcbit = 1;
4445 ip->fixp[i]->fx_tcbit = 1;
4446 }
252b5132 4447 }
1e915849 4448 install_insn (ip);
252b5132
RH
4449
4450 /* Update the register mask information. */
4c260379
RS
4451 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4452 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 4453
a4e06468 4454 switch (method)
252b5132 4455 {
a4e06468
RS
4456 case APPEND_ADD:
4457 insert_into_history (0, 1, ip);
4458 break;
4459
4460 case APPEND_ADD_WITH_NOP:
14fe068b
RS
4461 {
4462 struct mips_cl_insn *nop;
4463
4464 insert_into_history (0, 1, ip);
4465 nop = get_delay_slot_nop (ip);
4466 add_fixed_insn (nop);
4467 insert_into_history (0, 1, nop);
4468 if (mips_relax.sequence)
4469 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4470 }
a4e06468
RS
4471 break;
4472
4473 case APPEND_ADD_COMPACT:
4474 /* Convert MIPS16 jr/jalr into a "compact" jump. */
4475 gas_assert (mips_opts.mips16);
4476 ip->insn_opcode |= 0x0080;
4477 find_altered_mips16_opcode (ip);
4478 install_insn (ip);
4479 insert_into_history (0, 1, ip);
4480 break;
4481
4482 case APPEND_SWAP:
4483 {
4484 struct mips_cl_insn delay = history[0];
4485 if (mips_opts.mips16)
4486 {
4487 know (delay.frag == ip->frag);
4488 move_insn (ip, delay.frag, delay.where);
4489 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4490 }
464ab0e5 4491 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
4492 {
4493 /* Add the delay slot instruction to the end of the
4494 current frag and shrink the fixed part of the
4495 original frag. If the branch occupies the tail of
4496 the latter, move it backwards to cover the gap. */
2b0c8b40 4497 delay.frag->fr_fix -= branch_disp;
a4e06468 4498 if (delay.frag == ip->frag)
2b0c8b40 4499 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
4500 add_fixed_insn (&delay);
4501 }
4502 else
4503 {
2b0c8b40
MR
4504 move_insn (&delay, ip->frag,
4505 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
4506 move_insn (ip, history[0].frag, history[0].where);
4507 }
4508 history[0] = *ip;
4509 delay.fixed_p = 1;
4510 insert_into_history (0, 1, &delay);
4511 }
4512 break;
252b5132
RH
4513 }
4514
13408f1e 4515 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
4516 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4517 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
13408f1e
RS
4518 mips_no_prev_insn ();
4519
df58fc94
RS
4520 /* We need to emit a label at the end of branch-likely macros. */
4521 if (emit_branch_likely_macro)
4522 {
4523 emit_branch_likely_macro = FALSE;
4524 micromips_add_label ();
4525 }
4526
252b5132
RH
4527 /* We just output an insn, so the next one doesn't have a label. */
4528 mips_clear_insn_labels ();
252b5132
RH
4529}
4530
7d10b47d 4531/* Forget that there was any previous instruction or label. */
252b5132
RH
4532
4533static void
7d10b47d 4534mips_no_prev_insn (void)
252b5132 4535{
7d10b47d
RS
4536 prev_nop_frag = NULL;
4537 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
4538 mips_clear_insn_labels ();
4539}
4540
7d10b47d
RS
4541/* This function must be called before we emit something other than
4542 instructions. It is like mips_no_prev_insn except that it inserts
4543 any NOPS that might be needed by previous instructions. */
252b5132 4544
7d10b47d
RS
4545void
4546mips_emit_delays (void)
252b5132
RH
4547{
4548 if (! mips_opts.noreorder)
4549 {
932d1a1b 4550 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
4551 if (nops > 0)
4552 {
7d10b47d
RS
4553 while (nops-- > 0)
4554 add_fixed_insn (NOP_INSN);
462427c4 4555 mips_move_text_labels ();
7d10b47d
RS
4556 }
4557 }
4558 mips_no_prev_insn ();
4559}
4560
4561/* Start a (possibly nested) noreorder block. */
4562
4563static void
4564start_noreorder (void)
4565{
4566 if (mips_opts.noreorder == 0)
4567 {
4568 unsigned int i;
4569 int nops;
4570
4571 /* None of the instructions before the .set noreorder can be moved. */
4572 for (i = 0; i < ARRAY_SIZE (history); i++)
4573 history[i].fixed_p = 1;
4574
4575 /* Insert any nops that might be needed between the .set noreorder
4576 block and the previous instructions. We will later remove any
4577 nops that turn out not to be needed. */
932d1a1b 4578 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
4579 if (nops > 0)
4580 {
4581 if (mips_optimize != 0)
252b5132
RH
4582 {
4583 /* Record the frag which holds the nop instructions, so
4584 that we can remove them if we don't need them. */
df58fc94 4585 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
4586 prev_nop_frag = frag_now;
4587 prev_nop_frag_holds = nops;
4588 prev_nop_frag_required = 0;
4589 prev_nop_frag_since = 0;
4590 }
4591
4592 for (; nops > 0; --nops)
1e915849 4593 add_fixed_insn (NOP_INSN);
252b5132 4594
7d10b47d
RS
4595 /* Move on to a new frag, so that it is safe to simply
4596 decrease the size of prev_nop_frag. */
4597 frag_wane (frag_now);
4598 frag_new (0);
462427c4 4599 mips_move_text_labels ();
252b5132 4600 }
df58fc94 4601 mips_mark_labels ();
7d10b47d 4602 mips_clear_insn_labels ();
252b5132 4603 }
7d10b47d
RS
4604 mips_opts.noreorder++;
4605 mips_any_noreorder = 1;
4606}
252b5132 4607
7d10b47d 4608/* End a nested noreorder block. */
252b5132 4609
7d10b47d
RS
4610static void
4611end_noreorder (void)
4612{
4613 mips_opts.noreorder--;
4614 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
4615 {
4616 /* Commit to inserting prev_nop_frag_required nops and go back to
4617 handling nop insertion the .set reorder way. */
4618 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 4619 * NOP_INSN_SIZE);
7d10b47d
RS
4620 insert_into_history (prev_nop_frag_since,
4621 prev_nop_frag_required, NOP_INSN);
4622 prev_nop_frag = NULL;
4623 }
252b5132
RH
4624}
4625
584892a6
RS
4626/* Set up global variables for the start of a new macro. */
4627
4628static void
4629macro_start (void)
4630{
4631 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
4632 memset (&mips_macro_warning.first_insn_sizes, 0,
4633 sizeof (mips_macro_warning.first_insn_sizes));
4634 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 4635 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 4636 && delayed_branch_p (&history[0]));
df58fc94
RS
4637 switch (history[0].insn_mo->pinfo2
4638 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
4639 {
4640 case INSN2_BRANCH_DELAY_32BIT:
4641 mips_macro_warning.delay_slot_length = 4;
4642 break;
4643 case INSN2_BRANCH_DELAY_16BIT:
4644 mips_macro_warning.delay_slot_length = 2;
4645 break;
4646 default:
4647 mips_macro_warning.delay_slot_length = 0;
4648 break;
4649 }
4650 mips_macro_warning.first_frag = NULL;
584892a6
RS
4651}
4652
df58fc94
RS
4653/* Given that a macro is longer than one instruction or of the wrong size,
4654 return the appropriate warning for it. Return null if no warning is
4655 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
4656 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
4657 and RELAX_NOMACRO. */
584892a6
RS
4658
4659static const char *
4660macro_warning (relax_substateT subtype)
4661{
4662 if (subtype & RELAX_DELAY_SLOT)
4663 return _("Macro instruction expanded into multiple instructions"
4664 " in a branch delay slot");
4665 else if (subtype & RELAX_NOMACRO)
4666 return _("Macro instruction expanded into multiple instructions");
df58fc94
RS
4667 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
4668 | RELAX_DELAY_SLOT_SIZE_SECOND))
4669 return ((subtype & RELAX_DELAY_SLOT_16BIT)
4670 ? _("Macro instruction expanded into a wrong size instruction"
4671 " in a 16-bit branch delay slot")
4672 : _("Macro instruction expanded into a wrong size instruction"
4673 " in a 32-bit branch delay slot"));
584892a6
RS
4674 else
4675 return 0;
4676}
4677
4678/* Finish up a macro. Emit warnings as appropriate. */
4679
4680static void
4681macro_end (void)
4682{
df58fc94
RS
4683 /* Relaxation warning flags. */
4684 relax_substateT subtype = 0;
4685
4686 /* Check delay slot size requirements. */
4687 if (mips_macro_warning.delay_slot_length == 2)
4688 subtype |= RELAX_DELAY_SLOT_16BIT;
4689 if (mips_macro_warning.delay_slot_length != 0)
584892a6 4690 {
df58fc94
RS
4691 if (mips_macro_warning.delay_slot_length
4692 != mips_macro_warning.first_insn_sizes[0])
4693 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
4694 if (mips_macro_warning.delay_slot_length
4695 != mips_macro_warning.first_insn_sizes[1])
4696 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
4697 }
584892a6 4698
df58fc94
RS
4699 /* Check instruction count requirements. */
4700 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
4701 {
4702 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
4703 subtype |= RELAX_SECOND_LONGER;
4704 if (mips_opts.warn_about_macros)
4705 subtype |= RELAX_NOMACRO;
4706 if (mips_macro_warning.delay_slot_p)
4707 subtype |= RELAX_DELAY_SLOT;
df58fc94 4708 }
584892a6 4709
df58fc94
RS
4710 /* If both alternatives fail to fill a delay slot correctly,
4711 emit the warning now. */
4712 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
4713 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
4714 {
4715 relax_substateT s;
4716 const char *msg;
4717
4718 s = subtype & (RELAX_DELAY_SLOT_16BIT
4719 | RELAX_DELAY_SLOT_SIZE_FIRST
4720 | RELAX_DELAY_SLOT_SIZE_SECOND);
4721 msg = macro_warning (s);
4722 if (msg != NULL)
4723 as_warn ("%s", msg);
4724 subtype &= ~s;
4725 }
4726
4727 /* If both implementations are longer than 1 instruction, then emit the
4728 warning now. */
4729 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
4730 {
4731 relax_substateT s;
4732 const char *msg;
4733
4734 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
4735 msg = macro_warning (s);
4736 if (msg != NULL)
4737 as_warn ("%s", msg);
4738 subtype &= ~s;
584892a6 4739 }
df58fc94
RS
4740
4741 /* If any flags still set, then one implementation might need a warning
4742 and the other either will need one of a different kind or none at all.
4743 Pass any remaining flags over to relaxation. */
4744 if (mips_macro_warning.first_frag != NULL)
4745 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
4746}
4747
df58fc94
RS
4748/* Instruction operand formats used in macros that vary between
4749 standard MIPS and microMIPS code. */
4750
4751static const char * const brk_fmt[2] = { "c", "mF" };
4752static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
4753static const char * const jalr_fmt[2] = { "d,s", "t,s" };
4754static const char * const lui_fmt[2] = { "t,u", "s,u" };
4755static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
4756static const char * const mfhl_fmt[2] = { "d", "mj" };
4757static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
4758static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
4759
4760#define BRK_FMT (brk_fmt[mips_opts.micromips])
4761#define COP12_FMT (cop12_fmt[mips_opts.micromips])
4762#define JALR_FMT (jalr_fmt[mips_opts.micromips])
4763#define LUI_FMT (lui_fmt[mips_opts.micromips])
4764#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
4765#define MFHL_FMT (mfhl_fmt[mips_opts.micromips])
4766#define SHFT_FMT (shft_fmt[mips_opts.micromips])
4767#define TRAP_FMT (trap_fmt[mips_opts.micromips])
4768
6e1304d8
RS
4769/* Read a macro's relocation codes from *ARGS and store them in *R.
4770 The first argument in *ARGS will be either the code for a single
4771 relocation or -1 followed by the three codes that make up a
4772 composite relocation. */
4773
4774static void
4775macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
4776{
4777 int i, next;
4778
4779 next = va_arg (*args, int);
4780 if (next >= 0)
4781 r[0] = (bfd_reloc_code_real_type) next;
4782 else
4783 for (i = 0; i < 3; i++)
4784 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
4785}
4786
252b5132
RH
4787/* Build an instruction created by a macro expansion. This is passed
4788 a pointer to the count of instructions created so far, an
4789 expression, the name of the instruction to build, an operand format
4790 string, and corresponding arguments. */
4791
252b5132 4792static void
67c0d1eb 4793macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 4794{
df58fc94 4795 const struct mips_opcode *mo = NULL;
f6688943 4796 bfd_reloc_code_real_type r[3];
df58fc94
RS
4797 const struct mips_opcode *amo;
4798 struct hash_control *hash;
4799 struct mips_cl_insn insn;
252b5132 4800 va_list args;
252b5132 4801
252b5132 4802 va_start (args, fmt);
252b5132 4803
252b5132
RH
4804 if (mips_opts.mips16)
4805 {
03ea81db 4806 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
4807 va_end (args);
4808 return;
4809 }
4810
f6688943
TS
4811 r[0] = BFD_RELOC_UNUSED;
4812 r[1] = BFD_RELOC_UNUSED;
4813 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
4814 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
4815 amo = (struct mips_opcode *) hash_find (hash, name);
4816 gas_assert (amo);
4817 gas_assert (strcmp (name, amo->name) == 0);
1e915849 4818
df58fc94 4819 do
8b082fb1
TS
4820 {
4821 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
4822 macros will never generate MDMX, MIPS-3D, or MT instructions.
4823 We try to match an instruction that fulfils the branch delay
4824 slot instruction length requirement (if any) of the previous
4825 instruction. While doing this we record the first instruction
4826 seen that matches all the other conditions and use it anyway
4827 if the requirement cannot be met; we will issue an appropriate
4828 warning later on. */
4829 if (strcmp (fmt, amo->args) == 0
4830 && amo->pinfo != INSN_MACRO
4831 && is_opcode_valid (amo)
4832 && is_size_valid (amo))
4833 {
4834 if (is_delay_slot_valid (amo))
4835 {
4836 mo = amo;
4837 break;
4838 }
4839 else if (!mo)
4840 mo = amo;
4841 }
8b082fb1 4842
df58fc94
RS
4843 ++amo;
4844 gas_assert (amo->name);
252b5132 4845 }
df58fc94 4846 while (strcmp (name, amo->name) == 0);
252b5132 4847
df58fc94 4848 gas_assert (mo);
1e915849 4849 create_insn (&insn, mo);
252b5132
RH
4850 for (;;)
4851 {
4852 switch (*fmt++)
4853 {
4854 case '\0':
4855 break;
4856
4857 case ',':
4858 case '(':
4859 case ')':
4860 continue;
4861
5f74bc13
CD
4862 case '+':
4863 switch (*fmt++)
4864 {
4865 case 'A':
4866 case 'E':
df58fc94
RS
4867 INSERT_OPERAND (mips_opts.micromips,
4868 EXTLSB, insn, va_arg (args, int));
5f74bc13
CD
4869 continue;
4870
4871 case 'B':
4872 case 'F':
4873 /* Note that in the macro case, these arguments are already
4874 in MSB form. (When handling the instruction in the
4875 non-macro case, these arguments are sizes from which
4876 MSB values must be calculated.) */
df58fc94
RS
4877 INSERT_OPERAND (mips_opts.micromips,
4878 INSMSB, insn, va_arg (args, int));
5f74bc13
CD
4879 continue;
4880
4881 case 'C':
4882 case 'G':
4883 case 'H':
4884 /* Note that in the macro case, these arguments are already
4885 in MSBD form. (When handling the instruction in the
4886 non-macro case, these arguments are sizes from which
4887 MSBD values must be calculated.) */
df58fc94
RS
4888 INSERT_OPERAND (mips_opts.micromips,
4889 EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
4890 continue;
4891
dd3cbb7e 4892 case 'Q':
df58fc94
RS
4893 gas_assert (!mips_opts.micromips);
4894 INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
dd3cbb7e
NC
4895 continue;
4896
5f74bc13
CD
4897 default:
4898 internalError ();
4899 }
4900 continue;
4901
8b082fb1 4902 case '2':
03f66e8a 4903 INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
8b082fb1
TS
4904 continue;
4905
df58fc94
RS
4906 case 'n':
4907 gas_assert (mips_opts.micromips);
252b5132
RH
4908 case 't':
4909 case 'w':
4910 case 'E':
df58fc94 4911 INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
252b5132
RH
4912 continue;
4913
4914 case 'c':
df58fc94
RS
4915 gas_assert (!mips_opts.micromips);
4916 INSERT_OPERAND (0, CODE, insn, va_arg (args, int));
38487616
TS
4917 continue;
4918
252b5132 4919 case 'W':
df58fc94
RS
4920 gas_assert (!mips_opts.micromips);
4921 case 'T':
4922 INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
252b5132
RH
4923 continue;
4924
252b5132 4925 case 'G':
df58fc94
RS
4926 if (mips_opts.micromips)
4927 INSERT_OPERAND (1, RS, insn, va_arg (args, int));
4928 else
4929 INSERT_OPERAND (0, RD, insn, va_arg (args, int));
4930 continue;
4931
af7ee8bf 4932 case 'K':
df58fc94
RS
4933 gas_assert (!mips_opts.micromips);
4934 case 'd':
4935 INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
252b5132
RH
4936 continue;
4937
4372b673 4938 case 'U':
df58fc94 4939 gas_assert (!mips_opts.micromips);
4372b673
NC
4940 {
4941 int tmp = va_arg (args, int);
4942
df58fc94
RS
4943 INSERT_OPERAND (0, RT, insn, tmp);
4944 INSERT_OPERAND (0, RD, insn, tmp);
4372b673 4945 }
df58fc94 4946 continue;
4372b673 4947
252b5132
RH
4948 case 'V':
4949 case 'S':
df58fc94
RS
4950 gas_assert (!mips_opts.micromips);
4951 INSERT_OPERAND (0, FS, insn, va_arg (args, int));
252b5132
RH
4952 continue;
4953
4954 case 'z':
4955 continue;
4956
4957 case '<':
df58fc94
RS
4958 INSERT_OPERAND (mips_opts.micromips,
4959 SHAMT, insn, va_arg (args, int));
252b5132
RH
4960 continue;
4961
4962 case 'D':
df58fc94
RS
4963 gas_assert (!mips_opts.micromips);
4964 INSERT_OPERAND (0, FD, insn, va_arg (args, int));
252b5132
RH
4965 continue;
4966
4967 case 'B':
df58fc94
RS
4968 gas_assert (!mips_opts.micromips);
4969 INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
252b5132
RH
4970 continue;
4971
4372b673 4972 case 'J':
df58fc94
RS
4973 gas_assert (!mips_opts.micromips);
4974 INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
4372b673
NC
4975 continue;
4976
252b5132 4977 case 'q':
df58fc94
RS
4978 gas_assert (!mips_opts.micromips);
4979 INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
252b5132
RH
4980 continue;
4981
4982 case 'b':
4983 case 's':
4984 case 'r':
4985 case 'v':
df58fc94 4986 INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
252b5132
RH
4987 continue;
4988
4989 case 'i':
4990 case 'j':
6e1304d8 4991 macro_read_relocs (&args, r);
9c2799c2 4992 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
4993 || *r == BFD_RELOC_MIPS_HIGHER
4994 || *r == BFD_RELOC_HI16_S
4995 || *r == BFD_RELOC_LO16
4996 || *r == BFD_RELOC_MIPS_GOT_OFST);
4997 continue;
4998
4999 case 'o':
5000 macro_read_relocs (&args, r);
252b5132
RH
5001 continue;
5002
5003 case 'u':
6e1304d8 5004 macro_read_relocs (&args, r);
9c2799c2 5005 gas_assert (ep != NULL
90ecf173
MR
5006 && (ep->X_op == O_constant
5007 || (ep->X_op == O_symbol
5008 && (*r == BFD_RELOC_MIPS_HIGHEST
5009 || *r == BFD_RELOC_HI16_S
5010 || *r == BFD_RELOC_HI16
5011 || *r == BFD_RELOC_GPREL16
5012 || *r == BFD_RELOC_MIPS_GOT_HI16
5013 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
5014 continue;
5015
5016 case 'p':
9c2799c2 5017 gas_assert (ep != NULL);
bad36eac 5018
252b5132
RH
5019 /*
5020 * This allows macro() to pass an immediate expression for
5021 * creating short branches without creating a symbol.
bad36eac
DJ
5022 *
5023 * We don't allow branch relaxation for these branches, as
5024 * they should only appear in ".set nomacro" anyway.
252b5132
RH
5025 */
5026 if (ep->X_op == O_constant)
5027 {
df58fc94
RS
5028 /* For microMIPS we always use relocations for branches.
5029 So we should not resolve immediate values. */
5030 gas_assert (!mips_opts.micromips);
5031
bad36eac
DJ
5032 if ((ep->X_add_number & 3) != 0)
5033 as_bad (_("branch to misaligned address (0x%lx)"),
5034 (unsigned long) ep->X_add_number);
5035 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5036 as_bad (_("branch address range overflow (0x%lx)"),
5037 (unsigned long) ep->X_add_number);
252b5132
RH
5038 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5039 ep = NULL;
5040 }
5041 else
0b25d3e6 5042 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
5043 continue;
5044
5045 case 'a':
9c2799c2 5046 gas_assert (ep != NULL);
f6688943 5047 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
5048 continue;
5049
5050 case 'C':
df58fc94
RS
5051 gas_assert (!mips_opts.micromips);
5052 INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
252b5132
RH
5053 continue;
5054
d43b4baf 5055 case 'k':
df58fc94
RS
5056 INSERT_OPERAND (mips_opts.micromips,
5057 CACHE, insn, va_arg (args, unsigned long));
5058 continue;
5059
5060 case '|':
5061 gas_assert (mips_opts.micromips);
5062 INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5063 continue;
5064
5065 case '.':
5066 gas_assert (mips_opts.micromips);
5067 INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5068 continue;
5069
dec0624d
MR
5070 case '\\':
5071 INSERT_OPERAND (mips_opts.micromips,
5072 3BITPOS, insn, va_arg (args, unsigned int));
5073 continue;
5074
df58fc94 5075 case '~':
dec0624d
MR
5076 INSERT_OPERAND (mips_opts.micromips,
5077 OFFSET12, insn, va_arg (args, unsigned long));
df58fc94
RS
5078 continue;
5079
5080 case 'N':
5081 gas_assert (mips_opts.micromips);
5082 INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5083 continue;
5084
5085 case 'm': /* Opcode extension character. */
5086 gas_assert (mips_opts.micromips);
5087 switch (*fmt++)
5088 {
5089 case 'j':
5090 INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5091 break;
5092
5093 case 'p':
5094 INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5095 break;
5096
5097 case 'F':
5098 INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5099 break;
5100
5101 default:
5102 internalError ();
5103 }
d43b4baf
TS
5104 continue;
5105
252b5132
RH
5106 default:
5107 internalError ();
5108 }
5109 break;
5110 }
5111 va_end (args);
9c2799c2 5112 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5113
df58fc94 5114 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5115}
5116
5117static void
67c0d1eb 5118mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 5119 va_list *args)
252b5132 5120{
1e915849 5121 struct mips_opcode *mo;
252b5132 5122 struct mips_cl_insn insn;
f6688943
TS
5123 bfd_reloc_code_real_type r[3]
5124 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 5125
1e915849 5126 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
5127 gas_assert (mo);
5128 gas_assert (strcmp (name, mo->name) == 0);
252b5132 5129
1e915849 5130 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 5131 {
1e915849 5132 ++mo;
9c2799c2
NC
5133 gas_assert (mo->name);
5134 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
5135 }
5136
1e915849 5137 create_insn (&insn, mo);
252b5132
RH
5138 for (;;)
5139 {
5140 int c;
5141
5142 c = *fmt++;
5143 switch (c)
5144 {
5145 case '\0':
5146 break;
5147
5148 case ',':
5149 case '(':
5150 case ')':
5151 continue;
5152
5153 case 'y':
5154 case 'w':
03ea81db 5155 MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
252b5132
RH
5156 continue;
5157
5158 case 'x':
5159 case 'v':
03ea81db 5160 MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
252b5132
RH
5161 continue;
5162
5163 case 'z':
03ea81db 5164 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
252b5132
RH
5165 continue;
5166
5167 case 'Z':
03ea81db 5168 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
252b5132
RH
5169 continue;
5170
5171 case '0':
5172 case 'S':
5173 case 'P':
5174 case 'R':
5175 continue;
5176
5177 case 'X':
03ea81db 5178 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
252b5132
RH
5179 continue;
5180
5181 case 'Y':
5182 {
5183 int regno;
5184
03ea81db 5185 regno = va_arg (*args, int);
252b5132 5186 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
a9e24354 5187 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
252b5132
RH
5188 }
5189 continue;
5190
5191 case '<':
5192 case '>':
5193 case '4':
5194 case '5':
5195 case 'H':
5196 case 'W':
5197 case 'D':
5198 case 'j':
5199 case '8':
5200 case 'V':
5201 case 'C':
5202 case 'U':
5203 case 'k':
5204 case 'K':
5205 case 'p':
5206 case 'q':
5207 {
9c2799c2 5208 gas_assert (ep != NULL);
252b5132
RH
5209
5210 if (ep->X_op != O_constant)
874e8986 5211 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
5212 else
5213 {
b34976b6
AM
5214 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
5215 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 5216 &insn.extend);
252b5132 5217 ep = NULL;
f6688943 5218 *r = BFD_RELOC_UNUSED;
252b5132
RH
5219 }
5220 }
5221 continue;
5222
5223 case '6':
03ea81db 5224 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
252b5132
RH
5225 continue;
5226 }
5227
5228 break;
5229 }
5230
9c2799c2 5231 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5232
df58fc94 5233 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5234}
5235
2051e8c4
MR
5236/*
5237 * Sign-extend 32-bit mode constants that have bit 31 set and all
5238 * higher bits unset.
5239 */
9f872bbe 5240static void
2051e8c4
MR
5241normalize_constant_expr (expressionS *ex)
5242{
9ee2a2d4 5243 if (ex->X_op == O_constant
2051e8c4
MR
5244 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5245 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5246 - 0x80000000);
5247}
5248
5249/*
5250 * Sign-extend 32-bit mode address offsets that have bit 31 set and
5251 * all higher bits unset.
5252 */
5253static void
5254normalize_address_expr (expressionS *ex)
5255{
5256 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5257 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5258 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5259 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5260 - 0x80000000);
5261}
5262
438c16b8
TS
5263/*
5264 * Generate a "jalr" instruction with a relocation hint to the called
5265 * function. This occurs in NewABI PIC code.
5266 */
5267static void
df58fc94 5268macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 5269{
df58fc94
RS
5270 static const bfd_reloc_code_real_type jalr_relocs[2]
5271 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5272 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5273 const char *jalr;
685736be 5274 char *f = NULL;
b34976b6 5275
1180b5a4 5276 if (MIPS_JALR_HINT_P (ep))
f21f8242 5277 {
cc3d92a5 5278 frag_grow (8);
f21f8242
AO
5279 f = frag_more (0);
5280 }
2906b037 5281 if (mips_opts.micromips)
df58fc94
RS
5282 {
5283 jalr = mips_opts.noreorder && !cprestore ? "jalr" : "jalrs";
5284 if (MIPS_JALR_HINT_P (ep))
5285 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5286 else
5287 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5288 }
2906b037
MR
5289 else
5290 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 5291 if (MIPS_JALR_HINT_P (ep))
df58fc94 5292 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
5293}
5294
252b5132
RH
5295/*
5296 * Generate a "lui" instruction.
5297 */
5298static void
67c0d1eb 5299macro_build_lui (expressionS *ep, int regnum)
252b5132 5300{
9c2799c2 5301 gas_assert (! mips_opts.mips16);
252b5132 5302
df58fc94 5303 if (ep->X_op != O_constant)
252b5132 5304 {
9c2799c2 5305 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
5306 /* _gp_disp is a special case, used from s_cpload.
5307 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 5308 gas_assert (mips_pic == NO_PIC
78e1bb40 5309 || (! HAVE_NEWABI
aa6975fb
ILT
5310 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5311 || (! mips_in_shared
bbe506e8
TS
5312 && strcmp (S_GET_NAME (ep->X_add_symbol),
5313 "__gnu_local_gp") == 0));
252b5132
RH
5314 }
5315
df58fc94 5316 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
5317}
5318
885add95
CD
5319/* Generate a sequence of instructions to do a load or store from a constant
5320 offset off of a base register (breg) into/from a target register (treg),
5321 using AT if necessary. */
5322static void
67c0d1eb
RS
5323macro_build_ldst_constoffset (expressionS *ep, const char *op,
5324 int treg, int breg, int dbl)
885add95 5325{
9c2799c2 5326 gas_assert (ep->X_op == O_constant);
885add95 5327
256ab948 5328 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5329 if (!dbl)
5330 normalize_constant_expr (ep);
256ab948 5331
67c1ffbe 5332 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 5333 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
5334 as_warn (_("operand overflow"));
5335
5336 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5337 {
5338 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 5339 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
5340 }
5341 else
5342 {
5343 /* 32-bit offset, need multiple instructions and AT, like:
5344 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
5345 addu $tempreg,$tempreg,$breg
5346 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
5347 to handle the complete offset. */
67c0d1eb
RS
5348 macro_build_lui (ep, AT);
5349 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5350 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 5351
741fe287 5352 if (!mips_opts.at)
8fc2e39e 5353 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
5354 }
5355}
5356
252b5132
RH
5357/* set_at()
5358 * Generates code to set the $at register to true (one)
5359 * if reg is less than the immediate expression.
5360 */
5361static void
67c0d1eb 5362set_at (int reg, int unsignedp)
252b5132
RH
5363{
5364 if (imm_expr.X_op == O_constant
5365 && imm_expr.X_add_number >= -0x8000
5366 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
5367 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5368 AT, reg, BFD_RELOC_LO16);
252b5132
RH
5369 else
5370 {
67c0d1eb
RS
5371 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5372 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
5373 }
5374}
5375
5376/* Warn if an expression is not a constant. */
5377
5378static void
17a2f251 5379check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
5380{
5381 if (ex->X_op == O_big)
5382 as_bad (_("unsupported large constant"));
5383 else if (ex->X_op != O_constant)
9ee2a2d4
MR
5384 as_bad (_("Instruction %s requires absolute expression"),
5385 ip->insn_mo->name);
13757d0c 5386
9ee2a2d4
MR
5387 if (HAVE_32BIT_GPRS)
5388 normalize_constant_expr (ex);
252b5132
RH
5389}
5390
5391/* Count the leading zeroes by performing a binary chop. This is a
5392 bulky bit of source, but performance is a LOT better for the
5393 majority of values than a simple loop to count the bits:
5394 for (lcnt = 0; (lcnt < 32); lcnt++)
5395 if ((v) & (1 << (31 - lcnt)))
5396 break;
5397 However it is not code size friendly, and the gain will drop a bit
5398 on certain cached systems.
5399*/
5400#define COUNT_TOP_ZEROES(v) \
5401 (((v) & ~0xffff) == 0 \
5402 ? ((v) & ~0xff) == 0 \
5403 ? ((v) & ~0xf) == 0 \
5404 ? ((v) & ~0x3) == 0 \
5405 ? ((v) & ~0x1) == 0 \
5406 ? !(v) \
5407 ? 32 \
5408 : 31 \
5409 : 30 \
5410 : ((v) & ~0x7) == 0 \
5411 ? 29 \
5412 : 28 \
5413 : ((v) & ~0x3f) == 0 \
5414 ? ((v) & ~0x1f) == 0 \
5415 ? 27 \
5416 : 26 \
5417 : ((v) & ~0x7f) == 0 \
5418 ? 25 \
5419 : 24 \
5420 : ((v) & ~0xfff) == 0 \
5421 ? ((v) & ~0x3ff) == 0 \
5422 ? ((v) & ~0x1ff) == 0 \
5423 ? 23 \
5424 : 22 \
5425 : ((v) & ~0x7ff) == 0 \
5426 ? 21 \
5427 : 20 \
5428 : ((v) & ~0x3fff) == 0 \
5429 ? ((v) & ~0x1fff) == 0 \
5430 ? 19 \
5431 : 18 \
5432 : ((v) & ~0x7fff) == 0 \
5433 ? 17 \
5434 : 16 \
5435 : ((v) & ~0xffffff) == 0 \
5436 ? ((v) & ~0xfffff) == 0 \
5437 ? ((v) & ~0x3ffff) == 0 \
5438 ? ((v) & ~0x1ffff) == 0 \
5439 ? 15 \
5440 : 14 \
5441 : ((v) & ~0x7ffff) == 0 \
5442 ? 13 \
5443 : 12 \
5444 : ((v) & ~0x3fffff) == 0 \
5445 ? ((v) & ~0x1fffff) == 0 \
5446 ? 11 \
5447 : 10 \
5448 : ((v) & ~0x7fffff) == 0 \
5449 ? 9 \
5450 : 8 \
5451 : ((v) & ~0xfffffff) == 0 \
5452 ? ((v) & ~0x3ffffff) == 0 \
5453 ? ((v) & ~0x1ffffff) == 0 \
5454 ? 7 \
5455 : 6 \
5456 : ((v) & ~0x7ffffff) == 0 \
5457 ? 5 \
5458 : 4 \
5459 : ((v) & ~0x3fffffff) == 0 \
5460 ? ((v) & ~0x1fffffff) == 0 \
5461 ? 3 \
5462 : 2 \
5463 : ((v) & ~0x7fffffff) == 0 \
5464 ? 1 \
5465 : 0)
5466
5467/* load_register()
67c1ffbe 5468 * This routine generates the least number of instructions necessary to load
252b5132
RH
5469 * an absolute expression value into a register.
5470 */
5471static void
67c0d1eb 5472load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
5473{
5474 int freg;
5475 expressionS hi32, lo32;
5476
5477 if (ep->X_op != O_big)
5478 {
9c2799c2 5479 gas_assert (ep->X_op == O_constant);
256ab948
TS
5480
5481 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5482 if (!dbl)
5483 normalize_constant_expr (ep);
256ab948
TS
5484
5485 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
5486 {
5487 /* We can handle 16 bit signed values with an addiu to
5488 $zero. No need to ever use daddiu here, since $zero and
5489 the result are always correct in 32 bit mode. */
67c0d1eb 5490 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5491 return;
5492 }
5493 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5494 {
5495 /* We can handle 16 bit unsigned values with an ori to
5496 $zero. */
67c0d1eb 5497 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5498 return;
5499 }
256ab948 5500 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
5501 {
5502 /* 32 bit values require an lui. */
df58fc94 5503 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 5504 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 5505 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
5506 return;
5507 }
5508 }
5509
5510 /* The value is larger than 32 bits. */
5511
2051e8c4 5512 if (!dbl || HAVE_32BIT_GPRS)
252b5132 5513 {
55e08f71
NC
5514 char value[32];
5515
5516 sprintf_vma (value, ep->X_add_number);
20e1fcfd 5517 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 5518 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5519 return;
5520 }
5521
5522 if (ep->X_op != O_big)
5523 {
5524 hi32 = *ep;
5525 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5526 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5527 hi32.X_add_number &= 0xffffffff;
5528 lo32 = *ep;
5529 lo32.X_add_number &= 0xffffffff;
5530 }
5531 else
5532 {
9c2799c2 5533 gas_assert (ep->X_add_number > 2);
252b5132
RH
5534 if (ep->X_add_number == 3)
5535 generic_bignum[3] = 0;
5536 else if (ep->X_add_number > 4)
5537 as_bad (_("Number larger than 64 bits"));
5538 lo32.X_op = O_constant;
5539 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
5540 hi32.X_op = O_constant;
5541 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
5542 }
5543
5544 if (hi32.X_add_number == 0)
5545 freg = 0;
5546 else
5547 {
5548 int shift, bit;
5549 unsigned long hi, lo;
5550
956cd1d6 5551 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
5552 {
5553 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
5554 {
67c0d1eb 5555 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
5556 return;
5557 }
5558 if (lo32.X_add_number & 0x80000000)
5559 {
df58fc94 5560 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 5561 if (lo32.X_add_number & 0xffff)
67c0d1eb 5562 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
5563 return;
5564 }
5565 }
252b5132
RH
5566
5567 /* Check for 16bit shifted constant. We know that hi32 is
5568 non-zero, so start the mask on the first bit of the hi32
5569 value. */
5570 shift = 17;
5571 do
beae10d5
KH
5572 {
5573 unsigned long himask, lomask;
5574
5575 if (shift < 32)
5576 {
5577 himask = 0xffff >> (32 - shift);
5578 lomask = (0xffff << shift) & 0xffffffff;
5579 }
5580 else
5581 {
5582 himask = 0xffff << (shift - 32);
5583 lomask = 0;
5584 }
5585 if ((hi32.X_add_number & ~(offsetT) himask) == 0
5586 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
5587 {
5588 expressionS tmp;
5589
5590 tmp.X_op = O_constant;
5591 if (shift < 32)
5592 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
5593 | (lo32.X_add_number >> shift));
5594 else
5595 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 5596 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 5597 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 5598 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
5599 return;
5600 }
f9419b05 5601 ++shift;
beae10d5
KH
5602 }
5603 while (shift <= (64 - 16));
252b5132
RH
5604
5605 /* Find the bit number of the lowest one bit, and store the
5606 shifted value in hi/lo. */
5607 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
5608 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
5609 if (lo != 0)
5610 {
5611 bit = 0;
5612 while ((lo & 1) == 0)
5613 {
5614 lo >>= 1;
5615 ++bit;
5616 }
5617 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
5618 hi >>= bit;
5619 }
5620 else
5621 {
5622 bit = 32;
5623 while ((hi & 1) == 0)
5624 {
5625 hi >>= 1;
5626 ++bit;
5627 }
5628 lo = hi;
5629 hi = 0;
5630 }
5631
5632 /* Optimize if the shifted value is a (power of 2) - 1. */
5633 if ((hi == 0 && ((lo + 1) & lo) == 0)
5634 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
5635 {
5636 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 5637 if (shift != 0)
beae10d5 5638 {
252b5132
RH
5639 expressionS tmp;
5640
5641 /* This instruction will set the register to be all
5642 ones. */
beae10d5
KH
5643 tmp.X_op = O_constant;
5644 tmp.X_add_number = (offsetT) -1;
67c0d1eb 5645 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
5646 if (bit != 0)
5647 {
5648 bit += shift;
df58fc94 5649 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 5650 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 5651 }
df58fc94 5652 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 5653 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
5654 return;
5655 }
5656 }
252b5132
RH
5657
5658 /* Sign extend hi32 before calling load_register, because we can
5659 generally get better code when we load a sign extended value. */
5660 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 5661 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 5662 load_register (reg, &hi32, 0);
252b5132
RH
5663 freg = reg;
5664 }
5665 if ((lo32.X_add_number & 0xffff0000) == 0)
5666 {
5667 if (freg != 0)
5668 {
df58fc94 5669 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
5670 freg = reg;
5671 }
5672 }
5673 else
5674 {
5675 expressionS mid16;
5676
956cd1d6 5677 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 5678 {
df58fc94
RS
5679 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5680 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
5681 return;
5682 }
252b5132
RH
5683
5684 if (freg != 0)
5685 {
df58fc94 5686 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
5687 freg = reg;
5688 }
5689 mid16 = lo32;
5690 mid16.X_add_number >>= 16;
67c0d1eb 5691 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 5692 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
5693 freg = reg;
5694 }
5695 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 5696 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
5697}
5698
269137b2
TS
5699static inline void
5700load_delay_nop (void)
5701{
5702 if (!gpr_interlocks)
5703 macro_build (NULL, "nop", "");
5704}
5705
252b5132
RH
5706/* Load an address into a register. */
5707
5708static void
67c0d1eb 5709load_address (int reg, expressionS *ep, int *used_at)
252b5132 5710{
252b5132
RH
5711 if (ep->X_op != O_constant
5712 && ep->X_op != O_symbol)
5713 {
5714 as_bad (_("expression too complex"));
5715 ep->X_op = O_constant;
5716 }
5717
5718 if (ep->X_op == O_constant)
5719 {
67c0d1eb 5720 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
5721 return;
5722 }
5723
5724 if (mips_pic == NO_PIC)
5725 {
5726 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 5727 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5728 Otherwise we want
5729 lui $reg,<sym> (BFD_RELOC_HI16_S)
5730 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 5731 If we have an addend, we always use the latter form.
76b3015f 5732
d6bc6245
TS
5733 With 64bit address space and a usable $at we want
5734 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5735 lui $at,<sym> (BFD_RELOC_HI16_S)
5736 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5737 daddiu $at,<sym> (BFD_RELOC_LO16)
5738 dsll32 $reg,0
3a482fd5 5739 daddu $reg,$reg,$at
76b3015f 5740
c03099e6 5741 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
5742 on superscalar processors.
5743 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5744 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5745 dsll $reg,16
5746 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
5747 dsll $reg,16
5748 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
5749
5750 For GP relative symbols in 64bit address space we can use
5751 the same sequence as in 32bit address space. */
aed1a261 5752 if (HAVE_64BIT_SYMBOLS)
d6bc6245 5753 {
6caf9ef4
TS
5754 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5755 && !nopic_need_relax (ep->X_add_symbol, 1))
5756 {
5757 relax_start (ep->X_add_symbol);
5758 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5759 mips_gp_register, BFD_RELOC_GPREL16);
5760 relax_switch ();
5761 }
d6bc6245 5762
741fe287 5763 if (*used_at == 0 && mips_opts.at)
d6bc6245 5764 {
df58fc94
RS
5765 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5766 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
5767 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5768 BFD_RELOC_MIPS_HIGHER);
5769 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 5770 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 5771 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
5772 *used_at = 1;
5773 }
5774 else
5775 {
df58fc94 5776 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
5777 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5778 BFD_RELOC_MIPS_HIGHER);
df58fc94 5779 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 5780 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 5781 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 5782 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 5783 }
6caf9ef4
TS
5784
5785 if (mips_relax.sequence)
5786 relax_end ();
d6bc6245 5787 }
252b5132
RH
5788 else
5789 {
d6bc6245 5790 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 5791 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 5792 {
4d7206a2 5793 relax_start (ep->X_add_symbol);
67c0d1eb 5794 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 5795 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5796 relax_switch ();
d6bc6245 5797 }
67c0d1eb
RS
5798 macro_build_lui (ep, reg);
5799 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
5800 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
5801 if (mips_relax.sequence)
5802 relax_end ();
d6bc6245 5803 }
252b5132 5804 }
0a44bf69 5805 else if (!mips_big_got)
252b5132
RH
5806 {
5807 expressionS ex;
5808
5809 /* If this is a reference to an external symbol, we want
5810 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5811 Otherwise we want
5812 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5813 nop
5814 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
5815 If there is a constant, it must be added in after.
5816
ed6fb7bd 5817 If we have NewABI, we want
f5040a92
AO
5818 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5819 unless we're referencing a global symbol with a non-zero
5820 offset, in which case cst must be added separately. */
ed6fb7bd
SC
5821 if (HAVE_NEWABI)
5822 {
f5040a92
AO
5823 if (ep->X_add_number)
5824 {
4d7206a2 5825 ex.X_add_number = ep->X_add_number;
f5040a92 5826 ep->X_add_number = 0;
4d7206a2 5827 relax_start (ep->X_add_symbol);
67c0d1eb
RS
5828 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5829 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5830 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5831 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5832 ex.X_op = O_constant;
67c0d1eb 5833 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5834 reg, reg, BFD_RELOC_LO16);
f5040a92 5835 ep->X_add_number = ex.X_add_number;
4d7206a2 5836 relax_switch ();
f5040a92 5837 }
67c0d1eb 5838 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 5839 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
5840 if (mips_relax.sequence)
5841 relax_end ();
ed6fb7bd
SC
5842 }
5843 else
5844 {
f5040a92
AO
5845 ex.X_add_number = ep->X_add_number;
5846 ep->X_add_number = 0;
67c0d1eb
RS
5847 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5848 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5849 load_delay_nop ();
4d7206a2
RS
5850 relax_start (ep->X_add_symbol);
5851 relax_switch ();
67c0d1eb 5852 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 5853 BFD_RELOC_LO16);
4d7206a2 5854 relax_end ();
ed6fb7bd 5855
f5040a92
AO
5856 if (ex.X_add_number != 0)
5857 {
5858 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5859 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5860 ex.X_op = O_constant;
67c0d1eb 5861 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5862 reg, reg, BFD_RELOC_LO16);
f5040a92 5863 }
252b5132
RH
5864 }
5865 }
0a44bf69 5866 else if (mips_big_got)
252b5132
RH
5867 {
5868 expressionS ex;
252b5132
RH
5869
5870 /* This is the large GOT case. If this is a reference to an
5871 external symbol, we want
5872 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5873 addu $reg,$reg,$gp
5874 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
5875
5876 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
5877 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5878 nop
5879 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 5880 If there is a constant, it must be added in after.
f5040a92
AO
5881
5882 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
5883 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5884 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 5885 */
438c16b8
TS
5886 if (HAVE_NEWABI)
5887 {
4d7206a2 5888 ex.X_add_number = ep->X_add_number;
f5040a92 5889 ep->X_add_number = 0;
4d7206a2 5890 relax_start (ep->X_add_symbol);
df58fc94 5891 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
5892 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5893 reg, reg, mips_gp_register);
5894 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
5895 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
5896 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5897 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5898 else if (ex.X_add_number)
5899 {
5900 ex.X_op = O_constant;
67c0d1eb
RS
5901 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5902 BFD_RELOC_LO16);
f5040a92
AO
5903 }
5904
5905 ep->X_add_number = ex.X_add_number;
4d7206a2 5906 relax_switch ();
67c0d1eb 5907 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 5908 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
5909 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5910 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 5911 relax_end ();
438c16b8 5912 }
252b5132 5913 else
438c16b8 5914 {
f5040a92
AO
5915 ex.X_add_number = ep->X_add_number;
5916 ep->X_add_number = 0;
4d7206a2 5917 relax_start (ep->X_add_symbol);
df58fc94 5918 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
5919 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5920 reg, reg, mips_gp_register);
5921 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
5922 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
5923 relax_switch ();
5924 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
5925 {
5926 /* We need a nop before loading from $gp. This special
5927 check is required because the lui which starts the main
5928 instruction stream does not refer to $gp, and so will not
5929 insert the nop which may be required. */
67c0d1eb 5930 macro_build (NULL, "nop", "");
438c16b8 5931 }
67c0d1eb 5932 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 5933 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5934 load_delay_nop ();
67c0d1eb 5935 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 5936 BFD_RELOC_LO16);
4d7206a2 5937 relax_end ();
438c16b8 5938
f5040a92
AO
5939 if (ex.X_add_number != 0)
5940 {
5941 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5942 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5943 ex.X_op = O_constant;
67c0d1eb
RS
5944 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5945 BFD_RELOC_LO16);
f5040a92 5946 }
252b5132
RH
5947 }
5948 }
252b5132
RH
5949 else
5950 abort ();
8fc2e39e 5951
741fe287 5952 if (!mips_opts.at && *used_at == 1)
8fc2e39e 5953 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
5954}
5955
ea1fb5dc
RS
5956/* Move the contents of register SOURCE into register DEST. */
5957
5958static void
67c0d1eb 5959move_register (int dest, int source)
ea1fb5dc 5960{
df58fc94
RS
5961 /* Prefer to use a 16-bit microMIPS instruction unless the previous
5962 instruction specifically requires a 32-bit one. */
5963 if (mips_opts.micromips
5964 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 5965 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94
RS
5966 else
5967 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
5968 dest, source, 0);
ea1fb5dc
RS
5969}
5970
4d7206a2 5971/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
5972 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
5973 The two alternatives are:
4d7206a2
RS
5974
5975 Global symbol Local sybmol
5976 ------------- ------------
5977 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
5978 ... ...
5979 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
5980
5981 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
5982 emits the second for a 16-bit offset or add_got_offset_hilo emits
5983 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
5984
5985static void
67c0d1eb 5986load_got_offset (int dest, expressionS *local)
4d7206a2
RS
5987{
5988 expressionS global;
5989
5990 global = *local;
5991 global.X_add_number = 0;
5992
5993 relax_start (local->X_add_symbol);
67c0d1eb
RS
5994 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
5995 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 5996 relax_switch ();
67c0d1eb
RS
5997 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
5998 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
5999 relax_end ();
6000}
6001
6002static void
67c0d1eb 6003add_got_offset (int dest, expressionS *local)
4d7206a2
RS
6004{
6005 expressionS global;
6006
6007 global.X_op = O_constant;
6008 global.X_op_symbol = NULL;
6009 global.X_add_symbol = NULL;
6010 global.X_add_number = local->X_add_number;
6011
6012 relax_start (local->X_add_symbol);
67c0d1eb 6013 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
6014 dest, dest, BFD_RELOC_LO16);
6015 relax_switch ();
67c0d1eb 6016 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
6017 relax_end ();
6018}
6019
f6a22291
MR
6020static void
6021add_got_offset_hilo (int dest, expressionS *local, int tmp)
6022{
6023 expressionS global;
6024 int hold_mips_optimize;
6025
6026 global.X_op = O_constant;
6027 global.X_op_symbol = NULL;
6028 global.X_add_symbol = NULL;
6029 global.X_add_number = local->X_add_number;
6030
6031 relax_start (local->X_add_symbol);
6032 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6033 relax_switch ();
6034 /* Set mips_optimize around the lui instruction to avoid
6035 inserting an unnecessary nop after the lw. */
6036 hold_mips_optimize = mips_optimize;
6037 mips_optimize = 2;
6038 macro_build_lui (&global, tmp);
6039 mips_optimize = hold_mips_optimize;
6040 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6041 relax_end ();
6042
6043 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6044}
6045
df58fc94
RS
6046/* Emit a sequence of instructions to emulate a branch likely operation.
6047 BR is an ordinary branch corresponding to one to be emulated. BRNEG
6048 is its complementing branch with the original condition negated.
6049 CALL is set if the original branch specified the link operation.
6050 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6051
6052 Code like this is produced in the noreorder mode:
6053
6054 BRNEG <args>, 1f
6055 nop
6056 b <sym>
6057 delay slot (executed only if branch taken)
6058 1:
6059
6060 or, if CALL is set:
6061
6062 BRNEG <args>, 1f
6063 nop
6064 bal <sym>
6065 delay slot (executed only if branch taken)
6066 1:
6067
6068 In the reorder mode the delay slot would be filled with a nop anyway,
6069 so code produced is simply:
6070
6071 BR <args>, <sym>
6072 nop
6073
6074 This function is used when producing code for the microMIPS ASE that
6075 does not implement branch likely instructions in hardware. */
6076
6077static void
6078macro_build_branch_likely (const char *br, const char *brneg,
6079 int call, expressionS *ep, const char *fmt,
6080 unsigned int sreg, unsigned int treg)
6081{
6082 int noreorder = mips_opts.noreorder;
6083 expressionS expr1;
6084
6085 gas_assert (mips_opts.micromips);
6086 start_noreorder ();
6087 if (noreorder)
6088 {
6089 micromips_label_expr (&expr1);
6090 macro_build (&expr1, brneg, fmt, sreg, treg);
6091 macro_build (NULL, "nop", "");
6092 macro_build (ep, call ? "bal" : "b", "p");
6093
6094 /* Set to true so that append_insn adds a label. */
6095 emit_branch_likely_macro = TRUE;
6096 }
6097 else
6098 {
6099 macro_build (ep, br, fmt, sreg, treg);
6100 macro_build (NULL, "nop", "");
6101 }
6102 end_noreorder ();
6103}
6104
6105/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6106 the condition code tested. EP specifies the branch target. */
6107
6108static void
6109macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6110{
6111 const int call = 0;
6112 const char *brneg;
6113 const char *br;
6114
6115 switch (type)
6116 {
6117 case M_BC1FL:
6118 br = "bc1f";
6119 brneg = "bc1t";
6120 break;
6121 case M_BC1TL:
6122 br = "bc1t";
6123 brneg = "bc1f";
6124 break;
6125 case M_BC2FL:
6126 br = "bc2f";
6127 brneg = "bc2t";
6128 break;
6129 case M_BC2TL:
6130 br = "bc2t";
6131 brneg = "bc2f";
6132 break;
6133 default:
6134 abort ();
6135 }
6136 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6137}
6138
6139/* Emit a two-argument branch macro specified by TYPE, using SREG as
6140 the register tested. EP specifies the branch target. */
6141
6142static void
6143macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6144{
6145 const char *brneg = NULL;
6146 const char *br;
6147 int call = 0;
6148
6149 switch (type)
6150 {
6151 case M_BGEZ:
6152 br = "bgez";
6153 break;
6154 case M_BGEZL:
6155 br = mips_opts.micromips ? "bgez" : "bgezl";
6156 brneg = "bltz";
6157 break;
6158 case M_BGEZALL:
6159 gas_assert (mips_opts.micromips);
6160 br = "bgezals";
6161 brneg = "bltz";
6162 call = 1;
6163 break;
6164 case M_BGTZ:
6165 br = "bgtz";
6166 break;
6167 case M_BGTZL:
6168 br = mips_opts.micromips ? "bgtz" : "bgtzl";
6169 brneg = "blez";
6170 break;
6171 case M_BLEZ:
6172 br = "blez";
6173 break;
6174 case M_BLEZL:
6175 br = mips_opts.micromips ? "blez" : "blezl";
6176 brneg = "bgtz";
6177 break;
6178 case M_BLTZ:
6179 br = "bltz";
6180 break;
6181 case M_BLTZL:
6182 br = mips_opts.micromips ? "bltz" : "bltzl";
6183 brneg = "bgez";
6184 break;
6185 case M_BLTZALL:
6186 gas_assert (mips_opts.micromips);
6187 br = "bltzals";
6188 brneg = "bgez";
6189 call = 1;
6190 break;
6191 default:
6192 abort ();
6193 }
6194 if (mips_opts.micromips && brneg)
6195 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6196 else
6197 macro_build (ep, br, "s,p", sreg);
6198}
6199
6200/* Emit a three-argument branch macro specified by TYPE, using SREG and
6201 TREG as the registers tested. EP specifies the branch target. */
6202
6203static void
6204macro_build_branch_rsrt (int type, expressionS *ep,
6205 unsigned int sreg, unsigned int treg)
6206{
6207 const char *brneg = NULL;
6208 const int call = 0;
6209 const char *br;
6210
6211 switch (type)
6212 {
6213 case M_BEQ:
6214 case M_BEQ_I:
6215 br = "beq";
6216 break;
6217 case M_BEQL:
6218 case M_BEQL_I:
6219 br = mips_opts.micromips ? "beq" : "beql";
6220 brneg = "bne";
6221 break;
6222 case M_BNE:
6223 case M_BNE_I:
6224 br = "bne";
6225 break;
6226 case M_BNEL:
6227 case M_BNEL_I:
6228 br = mips_opts.micromips ? "bne" : "bnel";
6229 brneg = "beq";
6230 break;
6231 default:
6232 abort ();
6233 }
6234 if (mips_opts.micromips && brneg)
6235 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6236 else
6237 macro_build (ep, br, "s,t,p", sreg, treg);
6238}
6239
252b5132
RH
6240/*
6241 * Build macros
6242 * This routine implements the seemingly endless macro or synthesized
6243 * instructions and addressing modes in the mips assembly language. Many
6244 * of these macros are simple and are similar to each other. These could
67c1ffbe 6245 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
6246 * this verbose method. Others are not simple macros but are more like
6247 * optimizing code generation.
6248 * One interesting optimization is when several store macros appear
67c1ffbe 6249 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
6250 * The ensuing load upper instructions are ommited. This implies some kind
6251 * of global optimization. We currently only optimize within a single macro.
6252 * For many of the load and store macros if the address is specified as a
6253 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6254 * first load register 'at' with zero and use it as the base register. The
6255 * mips assembler simply uses register $zero. Just one tiny optimization
6256 * we're missing.
6257 */
6258static void
17a2f251 6259macro (struct mips_cl_insn *ip)
252b5132 6260{
741fe287
MR
6261 unsigned int treg, sreg, dreg, breg;
6262 unsigned int tempreg;
252b5132 6263 int mask;
43841e91 6264 int used_at = 0;
df58fc94 6265 expressionS label_expr;
252b5132 6266 expressionS expr1;
df58fc94 6267 expressionS *ep;
252b5132
RH
6268 const char *s;
6269 const char *s2;
6270 const char *fmt;
6271 int likely = 0;
252b5132 6272 int coproc = 0;
df58fc94 6273 int off12 = 0;
1abe91b1 6274 int call = 0;
df58fc94
RS
6275 int jals = 0;
6276 int dbl = 0;
6277 int imm = 0;
6278 int ust = 0;
6279 int lp = 0;
6280 int ab = 0;
dd6a37e7 6281 int off0 = 0;
252b5132 6282 int off;
67c0d1eb 6283 offsetT maxnum;
252b5132 6284 bfd_reloc_code_real_type r;
252b5132
RH
6285 int hold_mips_optimize;
6286
9c2799c2 6287 gas_assert (! mips_opts.mips16);
252b5132 6288
df58fc94
RS
6289 treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6290 dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6291 sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
252b5132
RH
6292 mask = ip->insn_mo->mask;
6293
df58fc94
RS
6294 label_expr.X_op = O_constant;
6295 label_expr.X_op_symbol = NULL;
6296 label_expr.X_add_symbol = NULL;
6297 label_expr.X_add_number = 0;
6298
252b5132
RH
6299 expr1.X_op = O_constant;
6300 expr1.X_op_symbol = NULL;
6301 expr1.X_add_symbol = NULL;
6302 expr1.X_add_number = 1;
6303
6304 switch (mask)
6305 {
6306 case M_DABS:
6307 dbl = 1;
6308 case M_ABS:
df58fc94
RS
6309 /* bgez $a0,1f
6310 move v0,$a0
6311 sub v0,$zero,$a0
6312 1:
6313 */
252b5132 6314
7d10b47d 6315 start_noreorder ();
252b5132 6316
df58fc94
RS
6317 if (mips_opts.micromips)
6318 micromips_label_expr (&label_expr);
6319 else
6320 label_expr.X_add_number = 8;
6321 macro_build (&label_expr, "bgez", "s,p", sreg);
252b5132 6322 if (dreg == sreg)
a605d2b3 6323 macro_build (NULL, "nop", "");
252b5132 6324 else
67c0d1eb
RS
6325 move_register (dreg, sreg);
6326 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
df58fc94
RS
6327 if (mips_opts.micromips)
6328 micromips_add_label ();
252b5132 6329
7d10b47d 6330 end_noreorder ();
8fc2e39e 6331 break;
252b5132
RH
6332
6333 case M_ADD_I:
6334 s = "addi";
6335 s2 = "add";
6336 goto do_addi;
6337 case M_ADDU_I:
6338 s = "addiu";
6339 s2 = "addu";
6340 goto do_addi;
6341 case M_DADD_I:
6342 dbl = 1;
6343 s = "daddi";
6344 s2 = "dadd";
df58fc94
RS
6345 if (!mips_opts.micromips)
6346 goto do_addi;
6347 if (imm_expr.X_op == O_constant
6348 && imm_expr.X_add_number >= -0x200
6349 && imm_expr.X_add_number < 0x200)
6350 {
6351 macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6352 break;
6353 }
6354 goto do_addi_i;
252b5132
RH
6355 case M_DADDU_I:
6356 dbl = 1;
6357 s = "daddiu";
6358 s2 = "daddu";
6359 do_addi:
6360 if (imm_expr.X_op == O_constant
6361 && imm_expr.X_add_number >= -0x8000
6362 && imm_expr.X_add_number < 0x8000)
6363 {
67c0d1eb 6364 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 6365 break;
252b5132 6366 }
df58fc94 6367 do_addi_i:
8fc2e39e 6368 used_at = 1;
67c0d1eb
RS
6369 load_register (AT, &imm_expr, dbl);
6370 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6371 break;
6372
6373 case M_AND_I:
6374 s = "andi";
6375 s2 = "and";
6376 goto do_bit;
6377 case M_OR_I:
6378 s = "ori";
6379 s2 = "or";
6380 goto do_bit;
6381 case M_NOR_I:
6382 s = "";
6383 s2 = "nor";
6384 goto do_bit;
6385 case M_XOR_I:
6386 s = "xori";
6387 s2 = "xor";
6388 do_bit:
6389 if (imm_expr.X_op == O_constant
6390 && imm_expr.X_add_number >= 0
6391 && imm_expr.X_add_number < 0x10000)
6392 {
6393 if (mask != M_NOR_I)
67c0d1eb 6394 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
6395 else
6396 {
67c0d1eb
RS
6397 macro_build (&imm_expr, "ori", "t,r,i",
6398 treg, sreg, BFD_RELOC_LO16);
6399 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 6400 }
8fc2e39e 6401 break;
252b5132
RH
6402 }
6403
8fc2e39e 6404 used_at = 1;
67c0d1eb
RS
6405 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6406 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6407 break;
6408
8b082fb1
TS
6409 case M_BALIGN:
6410 switch (imm_expr.X_add_number)
6411 {
6412 case 0:
6413 macro_build (NULL, "nop", "");
6414 break;
6415 case 2:
6416 macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6417 break;
03f66e8a
MR
6418 case 1:
6419 case 3:
8b082fb1 6420 macro_build (NULL, "balign", "t,s,2", treg, sreg,
90ecf173 6421 (int) imm_expr.X_add_number);
8b082fb1 6422 break;
03f66e8a
MR
6423 default:
6424 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6425 (unsigned long) imm_expr.X_add_number);
6426 break;
8b082fb1
TS
6427 }
6428 break;
6429
df58fc94
RS
6430 case M_BC1FL:
6431 case M_BC1TL:
6432 case M_BC2FL:
6433 case M_BC2TL:
6434 gas_assert (mips_opts.micromips);
6435 macro_build_branch_ccl (mask, &offset_expr,
6436 EXTRACT_OPERAND (1, BCC, *ip));
6437 break;
6438
252b5132 6439 case M_BEQ_I:
252b5132 6440 case M_BEQL_I:
252b5132 6441 case M_BNE_I:
252b5132 6442 case M_BNEL_I:
252b5132 6443 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
6444 treg = 0;
6445 else
252b5132 6446 {
df58fc94
RS
6447 treg = AT;
6448 used_at = 1;
6449 load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
252b5132 6450 }
df58fc94
RS
6451 /* Fall through. */
6452 case M_BEQL:
6453 case M_BNEL:
6454 macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
252b5132
RH
6455 break;
6456
6457 case M_BGEL:
6458 likely = 1;
6459 case M_BGE:
6460 if (treg == 0)
df58fc94
RS
6461 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6462 else if (sreg == 0)
6463 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6464 else
252b5132 6465 {
df58fc94
RS
6466 used_at = 1;
6467 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6468 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6469 &offset_expr, AT, ZERO);
252b5132 6470 }
df58fc94
RS
6471 break;
6472
6473 case M_BGEZL:
6474 case M_BGEZALL:
6475 case M_BGTZL:
6476 case M_BLEZL:
6477 case M_BLTZL:
6478 case M_BLTZALL:
6479 macro_build_branch_rs (mask, &offset_expr, sreg);
252b5132
RH
6480 break;
6481
6482 case M_BGTL_I:
6483 likely = 1;
6484 case M_BGT_I:
90ecf173 6485 /* Check for > max integer. */
252b5132 6486 maxnum = 0x7fffffff;
ca4e0257 6487 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6488 {
6489 maxnum <<= 16;
6490 maxnum |= 0xffff;
6491 maxnum <<= 16;
6492 maxnum |= 0xffff;
6493 }
6494 if (imm_expr.X_op == O_constant
6495 && imm_expr.X_add_number >= maxnum
ca4e0257 6496 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6497 {
6498 do_false:
90ecf173 6499 /* Result is always false. */
252b5132 6500 if (! likely)
a605d2b3 6501 macro_build (NULL, "nop", "");
252b5132 6502 else
df58fc94 6503 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 6504 break;
252b5132
RH
6505 }
6506 if (imm_expr.X_op != O_constant)
6507 as_bad (_("Unsupported large constant"));
f9419b05 6508 ++imm_expr.X_add_number;
252b5132
RH
6509 /* FALLTHROUGH */
6510 case M_BGE_I:
6511 case M_BGEL_I:
6512 if (mask == M_BGEL_I)
6513 likely = 1;
6514 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6515 {
df58fc94
RS
6516 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
6517 &offset_expr, sreg);
8fc2e39e 6518 break;
252b5132
RH
6519 }
6520 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6521 {
df58fc94
RS
6522 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
6523 &offset_expr, sreg);
8fc2e39e 6524 break;
252b5132
RH
6525 }
6526 maxnum = 0x7fffffff;
ca4e0257 6527 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6528 {
6529 maxnum <<= 16;
6530 maxnum |= 0xffff;
6531 maxnum <<= 16;
6532 maxnum |= 0xffff;
6533 }
6534 maxnum = - maxnum - 1;
6535 if (imm_expr.X_op == O_constant
6536 && imm_expr.X_add_number <= maxnum
ca4e0257 6537 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6538 {
6539 do_true:
6540 /* result is always true */
6541 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 6542 macro_build (&offset_expr, "b", "p");
8fc2e39e 6543 break;
252b5132 6544 }
8fc2e39e 6545 used_at = 1;
67c0d1eb 6546 set_at (sreg, 0);
df58fc94
RS
6547 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6548 &offset_expr, AT, ZERO);
252b5132
RH
6549 break;
6550
6551 case M_BGEUL:
6552 likely = 1;
6553 case M_BGEU:
6554 if (treg == 0)
6555 goto do_true;
df58fc94
RS
6556 else if (sreg == 0)
6557 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6558 &offset_expr, ZERO, treg);
6559 else
252b5132 6560 {
df58fc94
RS
6561 used_at = 1;
6562 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6563 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6564 &offset_expr, AT, ZERO);
252b5132 6565 }
252b5132
RH
6566 break;
6567
6568 case M_BGTUL_I:
6569 likely = 1;
6570 case M_BGTU_I:
6571 if (sreg == 0
ca4e0257 6572 || (HAVE_32BIT_GPRS
252b5132 6573 && imm_expr.X_op == O_constant
f01dc953 6574 && imm_expr.X_add_number == -1))
252b5132
RH
6575 goto do_false;
6576 if (imm_expr.X_op != O_constant)
6577 as_bad (_("Unsupported large constant"));
f9419b05 6578 ++imm_expr.X_add_number;
252b5132
RH
6579 /* FALLTHROUGH */
6580 case M_BGEU_I:
6581 case M_BGEUL_I:
6582 if (mask == M_BGEUL_I)
6583 likely = 1;
6584 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6585 goto do_true;
df58fc94
RS
6586 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6587 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6588 &offset_expr, sreg, ZERO);
6589 else
252b5132 6590 {
df58fc94
RS
6591 used_at = 1;
6592 set_at (sreg, 1);
6593 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6594 &offset_expr, AT, ZERO);
252b5132 6595 }
252b5132
RH
6596 break;
6597
6598 case M_BGTL:
6599 likely = 1;
6600 case M_BGT:
6601 if (treg == 0)
df58fc94
RS
6602 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
6603 else if (sreg == 0)
6604 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
6605 else
252b5132 6606 {
df58fc94
RS
6607 used_at = 1;
6608 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6609 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6610 &offset_expr, AT, ZERO);
252b5132 6611 }
252b5132
RH
6612 break;
6613
6614 case M_BGTUL:
6615 likely = 1;
6616 case M_BGTU:
6617 if (treg == 0)
df58fc94
RS
6618 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6619 &offset_expr, sreg, ZERO);
6620 else if (sreg == 0)
6621 goto do_false;
6622 else
252b5132 6623 {
df58fc94
RS
6624 used_at = 1;
6625 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6626 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6627 &offset_expr, AT, ZERO);
252b5132 6628 }
252b5132
RH
6629 break;
6630
6631 case M_BLEL:
6632 likely = 1;
6633 case M_BLE:
6634 if (treg == 0)
df58fc94
RS
6635 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6636 else if (sreg == 0)
6637 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
6638 else
252b5132 6639 {
df58fc94
RS
6640 used_at = 1;
6641 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6642 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6643 &offset_expr, AT, ZERO);
252b5132 6644 }
252b5132
RH
6645 break;
6646
6647 case M_BLEL_I:
6648 likely = 1;
6649 case M_BLE_I:
6650 maxnum = 0x7fffffff;
ca4e0257 6651 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6652 {
6653 maxnum <<= 16;
6654 maxnum |= 0xffff;
6655 maxnum <<= 16;
6656 maxnum |= 0xffff;
6657 }
6658 if (imm_expr.X_op == O_constant
6659 && imm_expr.X_add_number >= maxnum
ca4e0257 6660 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6661 goto do_true;
6662 if (imm_expr.X_op != O_constant)
6663 as_bad (_("Unsupported large constant"));
f9419b05 6664 ++imm_expr.X_add_number;
252b5132
RH
6665 /* FALLTHROUGH */
6666 case M_BLT_I:
6667 case M_BLTL_I:
6668 if (mask == M_BLTL_I)
6669 likely = 1;
6670 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
6671 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6672 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6673 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6674 else
252b5132 6675 {
df58fc94
RS
6676 used_at = 1;
6677 set_at (sreg, 0);
6678 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6679 &offset_expr, AT, ZERO);
252b5132 6680 }
252b5132
RH
6681 break;
6682
6683 case M_BLEUL:
6684 likely = 1;
6685 case M_BLEU:
6686 if (treg == 0)
df58fc94
RS
6687 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6688 &offset_expr, sreg, ZERO);
6689 else if (sreg == 0)
6690 goto do_true;
6691 else
252b5132 6692 {
df58fc94
RS
6693 used_at = 1;
6694 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6695 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6696 &offset_expr, AT, ZERO);
252b5132 6697 }
252b5132
RH
6698 break;
6699
6700 case M_BLEUL_I:
6701 likely = 1;
6702 case M_BLEU_I:
6703 if (sreg == 0
ca4e0257 6704 || (HAVE_32BIT_GPRS
252b5132 6705 && imm_expr.X_op == O_constant
f01dc953 6706 && imm_expr.X_add_number == -1))
252b5132
RH
6707 goto do_true;
6708 if (imm_expr.X_op != O_constant)
6709 as_bad (_("Unsupported large constant"));
f9419b05 6710 ++imm_expr.X_add_number;
252b5132
RH
6711 /* FALLTHROUGH */
6712 case M_BLTU_I:
6713 case M_BLTUL_I:
6714 if (mask == M_BLTUL_I)
6715 likely = 1;
6716 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6717 goto do_false;
df58fc94
RS
6718 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6719 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6720 &offset_expr, sreg, ZERO);
6721 else
252b5132 6722 {
df58fc94
RS
6723 used_at = 1;
6724 set_at (sreg, 1);
6725 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6726 &offset_expr, AT, ZERO);
252b5132 6727 }
252b5132
RH
6728 break;
6729
6730 case M_BLTL:
6731 likely = 1;
6732 case M_BLT:
6733 if (treg == 0)
df58fc94
RS
6734 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6735 else if (sreg == 0)
6736 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
6737 else
252b5132 6738 {
df58fc94
RS
6739 used_at = 1;
6740 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6741 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6742 &offset_expr, AT, ZERO);
252b5132 6743 }
252b5132
RH
6744 break;
6745
6746 case M_BLTUL:
6747 likely = 1;
6748 case M_BLTU:
6749 if (treg == 0)
6750 goto do_false;
df58fc94
RS
6751 else if (sreg == 0)
6752 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6753 &offset_expr, ZERO, treg);
6754 else
252b5132 6755 {
df58fc94
RS
6756 used_at = 1;
6757 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6758 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6759 &offset_expr, AT, ZERO);
252b5132 6760 }
252b5132
RH
6761 break;
6762
5f74bc13
CD
6763 case M_DEXT:
6764 {
d5818fca
MR
6765 /* Use unsigned arithmetic. */
6766 addressT pos;
6767 addressT size;
5f74bc13 6768
90ecf173 6769 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
6770 {
6771 as_bad (_("Unsupported large constant"));
6772 pos = size = 1;
6773 }
6774 else
6775 {
d5818fca
MR
6776 pos = imm_expr.X_add_number;
6777 size = imm2_expr.X_add_number;
5f74bc13
CD
6778 }
6779
6780 if (pos > 63)
6781 {
d5818fca 6782 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
6783 pos = 1;
6784 }
90ecf173 6785 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
6786 {
6787 as_bad (_("Improper extract size (%lu, position %lu)"),
d5818fca 6788 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
6789 size = 1;
6790 }
6791
6792 if (size <= 32 && pos < 32)
6793 {
6794 s = "dext";
6795 fmt = "t,r,+A,+C";
6796 }
6797 else if (size <= 32)
6798 {
6799 s = "dextu";
6800 fmt = "t,r,+E,+H";
6801 }
6802 else
6803 {
6804 s = "dextm";
6805 fmt = "t,r,+A,+G";
6806 }
d5818fca
MR
6807 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6808 (int) (size - 1));
5f74bc13 6809 }
8fc2e39e 6810 break;
5f74bc13
CD
6811
6812 case M_DINS:
6813 {
d5818fca
MR
6814 /* Use unsigned arithmetic. */
6815 addressT pos;
6816 addressT size;
5f74bc13 6817
90ecf173 6818 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
6819 {
6820 as_bad (_("Unsupported large constant"));
6821 pos = size = 1;
6822 }
6823 else
6824 {
d5818fca
MR
6825 pos = imm_expr.X_add_number;
6826 size = imm2_expr.X_add_number;
5f74bc13
CD
6827 }
6828
6829 if (pos > 63)
6830 {
d5818fca 6831 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
6832 pos = 1;
6833 }
90ecf173 6834 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
6835 {
6836 as_bad (_("Improper insert size (%lu, position %lu)"),
d5818fca 6837 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
6838 size = 1;
6839 }
6840
6841 if (pos < 32 && (pos + size - 1) < 32)
6842 {
6843 s = "dins";
6844 fmt = "t,r,+A,+B";
6845 }
6846 else if (pos >= 32)
6847 {
6848 s = "dinsu";
6849 fmt = "t,r,+E,+F";
6850 }
6851 else
6852 {
6853 s = "dinsm";
6854 fmt = "t,r,+A,+F";
6855 }
750bdd57
AS
6856 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6857 (int) (pos + size - 1));
5f74bc13 6858 }
8fc2e39e 6859 break;
5f74bc13 6860
252b5132
RH
6861 case M_DDIV_3:
6862 dbl = 1;
6863 case M_DIV_3:
6864 s = "mflo";
6865 goto do_div3;
6866 case M_DREM_3:
6867 dbl = 1;
6868 case M_REM_3:
6869 s = "mfhi";
6870 do_div3:
6871 if (treg == 0)
6872 {
6873 as_warn (_("Divide by zero."));
6874 if (mips_trap)
df58fc94 6875 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 6876 else
df58fc94 6877 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 6878 break;
252b5132
RH
6879 }
6880
7d10b47d 6881 start_noreorder ();
252b5132
RH
6882 if (mips_trap)
6883 {
df58fc94 6884 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 6885 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
6886 }
6887 else
6888 {
df58fc94
RS
6889 if (mips_opts.micromips)
6890 micromips_label_expr (&label_expr);
6891 else
6892 label_expr.X_add_number = 8;
6893 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 6894 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
df58fc94
RS
6895 macro_build (NULL, "break", BRK_FMT, 7);
6896 if (mips_opts.micromips)
6897 micromips_add_label ();
252b5132
RH
6898 }
6899 expr1.X_add_number = -1;
8fc2e39e 6900 used_at = 1;
f6a22291 6901 load_register (AT, &expr1, dbl);
df58fc94
RS
6902 if (mips_opts.micromips)
6903 micromips_label_expr (&label_expr);
6904 else
6905 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
6906 macro_build (&label_expr, "bne", "s,t,p", treg, AT);
252b5132
RH
6907 if (dbl)
6908 {
6909 expr1.X_add_number = 1;
f6a22291 6910 load_register (AT, &expr1, dbl);
df58fc94 6911 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
6912 }
6913 else
6914 {
6915 expr1.X_add_number = 0x80000000;
df58fc94 6916 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
6917 }
6918 if (mips_trap)
6919 {
df58fc94 6920 macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
252b5132
RH
6921 /* We want to close the noreorder block as soon as possible, so
6922 that later insns are available for delay slot filling. */
7d10b47d 6923 end_noreorder ();
252b5132
RH
6924 }
6925 else
6926 {
df58fc94
RS
6927 if (mips_opts.micromips)
6928 micromips_label_expr (&label_expr);
6929 else
6930 label_expr.X_add_number = 8;
6931 macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
a605d2b3 6932 macro_build (NULL, "nop", "");
252b5132
RH
6933
6934 /* We want to close the noreorder block as soon as possible, so
6935 that later insns are available for delay slot filling. */
7d10b47d 6936 end_noreorder ();
252b5132 6937
df58fc94 6938 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 6939 }
df58fc94
RS
6940 if (mips_opts.micromips)
6941 micromips_add_label ();
6942 macro_build (NULL, s, MFHL_FMT, dreg);
252b5132
RH
6943 break;
6944
6945 case M_DIV_3I:
6946 s = "div";
6947 s2 = "mflo";
6948 goto do_divi;
6949 case M_DIVU_3I:
6950 s = "divu";
6951 s2 = "mflo";
6952 goto do_divi;
6953 case M_REM_3I:
6954 s = "div";
6955 s2 = "mfhi";
6956 goto do_divi;
6957 case M_REMU_3I:
6958 s = "divu";
6959 s2 = "mfhi";
6960 goto do_divi;
6961 case M_DDIV_3I:
6962 dbl = 1;
6963 s = "ddiv";
6964 s2 = "mflo";
6965 goto do_divi;
6966 case M_DDIVU_3I:
6967 dbl = 1;
6968 s = "ddivu";
6969 s2 = "mflo";
6970 goto do_divi;
6971 case M_DREM_3I:
6972 dbl = 1;
6973 s = "ddiv";
6974 s2 = "mfhi";
6975 goto do_divi;
6976 case M_DREMU_3I:
6977 dbl = 1;
6978 s = "ddivu";
6979 s2 = "mfhi";
6980 do_divi:
6981 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6982 {
6983 as_warn (_("Divide by zero."));
6984 if (mips_trap)
df58fc94 6985 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 6986 else
df58fc94 6987 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 6988 break;
252b5132
RH
6989 }
6990 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6991 {
6992 if (strcmp (s2, "mflo") == 0)
67c0d1eb 6993 move_register (dreg, sreg);
252b5132 6994 else
c80c840e 6995 move_register (dreg, ZERO);
8fc2e39e 6996 break;
252b5132
RH
6997 }
6998 if (imm_expr.X_op == O_constant
6999 && imm_expr.X_add_number == -1
7000 && s[strlen (s) - 1] != 'u')
7001 {
7002 if (strcmp (s2, "mflo") == 0)
7003 {
67c0d1eb 7004 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
7005 }
7006 else
c80c840e 7007 move_register (dreg, ZERO);
8fc2e39e 7008 break;
252b5132
RH
7009 }
7010
8fc2e39e 7011 used_at = 1;
67c0d1eb
RS
7012 load_register (AT, &imm_expr, dbl);
7013 macro_build (NULL, s, "z,s,t", sreg, AT);
df58fc94 7014 macro_build (NULL, s2, MFHL_FMT, dreg);
252b5132
RH
7015 break;
7016
7017 case M_DIVU_3:
7018 s = "divu";
7019 s2 = "mflo";
7020 goto do_divu3;
7021 case M_REMU_3:
7022 s = "divu";
7023 s2 = "mfhi";
7024 goto do_divu3;
7025 case M_DDIVU_3:
7026 s = "ddivu";
7027 s2 = "mflo";
7028 goto do_divu3;
7029 case M_DREMU_3:
7030 s = "ddivu";
7031 s2 = "mfhi";
7032 do_divu3:
7d10b47d 7033 start_noreorder ();
252b5132
RH
7034 if (mips_trap)
7035 {
df58fc94 7036 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 7037 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7038 /* We want to close the noreorder block as soon as possible, so
7039 that later insns are available for delay slot filling. */
7d10b47d 7040 end_noreorder ();
252b5132
RH
7041 }
7042 else
7043 {
df58fc94
RS
7044 if (mips_opts.micromips)
7045 micromips_label_expr (&label_expr);
7046 else
7047 label_expr.X_add_number = 8;
7048 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 7049 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7050
7051 /* We want to close the noreorder block as soon as possible, so
7052 that later insns are available for delay slot filling. */
7d10b47d 7053 end_noreorder ();
df58fc94
RS
7054 macro_build (NULL, "break", BRK_FMT, 7);
7055 if (mips_opts.micromips)
7056 micromips_add_label ();
252b5132 7057 }
df58fc94 7058 macro_build (NULL, s2, MFHL_FMT, dreg);
8fc2e39e 7059 break;
252b5132 7060
1abe91b1
MR
7061 case M_DLCA_AB:
7062 dbl = 1;
7063 case M_LCA_AB:
7064 call = 1;
7065 goto do_la;
252b5132
RH
7066 case M_DLA_AB:
7067 dbl = 1;
7068 case M_LA_AB:
1abe91b1 7069 do_la:
252b5132
RH
7070 /* Load the address of a symbol into a register. If breg is not
7071 zero, we then add a base register to it. */
7072
3bec30a8
TS
7073 if (dbl && HAVE_32BIT_GPRS)
7074 as_warn (_("dla used to load 32-bit register"));
7075
90ecf173 7076 if (!dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
7077 as_warn (_("la used to load 64-bit address"));
7078
0c11417f
MR
7079 if (offset_expr.X_op == O_constant
7080 && offset_expr.X_add_number >= -0x8000
7081 && offset_expr.X_add_number < 0x8000)
7082 {
aed1a261 7083 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 7084 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 7085 break;
0c11417f
MR
7086 }
7087
741fe287 7088 if (mips_opts.at && (treg == breg))
afdbd6d0
CD
7089 {
7090 tempreg = AT;
7091 used_at = 1;
7092 }
7093 else
7094 {
7095 tempreg = treg;
afdbd6d0
CD
7096 }
7097
252b5132
RH
7098 if (offset_expr.X_op != O_symbol
7099 && offset_expr.X_op != O_constant)
7100 {
f71d0d44 7101 as_bad (_("Expression too complex"));
252b5132
RH
7102 offset_expr.X_op = O_constant;
7103 }
7104
252b5132 7105 if (offset_expr.X_op == O_constant)
aed1a261 7106 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
7107 else if (mips_pic == NO_PIC)
7108 {
d6bc6245 7109 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 7110 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
7111 Otherwise we want
7112 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
7113 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7114 If we have a constant, we need two instructions anyhow,
d6bc6245 7115 so we may as well always use the latter form.
76b3015f 7116
6caf9ef4
TS
7117 With 64bit address space and a usable $at we want
7118 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7119 lui $at,<sym> (BFD_RELOC_HI16_S)
7120 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7121 daddiu $at,<sym> (BFD_RELOC_LO16)
7122 dsll32 $tempreg,0
7123 daddu $tempreg,$tempreg,$at
7124
7125 If $at is already in use, we use a path which is suboptimal
7126 on superscalar processors.
7127 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7128 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7129 dsll $tempreg,16
7130 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
7131 dsll $tempreg,16
7132 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
7133
7134 For GP relative symbols in 64bit address space we can use
7135 the same sequence as in 32bit address space. */
aed1a261 7136 if (HAVE_64BIT_SYMBOLS)
252b5132 7137 {
6caf9ef4
TS
7138 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7139 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7140 {
7141 relax_start (offset_expr.X_add_symbol);
7142 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7143 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7144 relax_switch ();
7145 }
d6bc6245 7146
741fe287 7147 if (used_at == 0 && mips_opts.at)
98d3f06f 7148 {
df58fc94 7149 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7150 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 7151 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7152 AT, BFD_RELOC_HI16_S);
67c0d1eb 7153 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7154 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 7155 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7156 AT, AT, BFD_RELOC_LO16);
df58fc94 7157 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 7158 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
7159 used_at = 1;
7160 }
7161 else
7162 {
df58fc94 7163 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7164 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 7165 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7166 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 7167 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7168 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7169 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 7170 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7171 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7172 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 7173 }
6caf9ef4
TS
7174
7175 if (mips_relax.sequence)
7176 relax_end ();
98d3f06f
KH
7177 }
7178 else
7179 {
7180 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 7181 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 7182 {
4d7206a2 7183 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7184 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7185 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 7186 relax_switch ();
98d3f06f 7187 }
6943caf0 7188 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
f71d0d44 7189 as_bad (_("Offset too large"));
67c0d1eb
RS
7190 macro_build_lui (&offset_expr, tempreg);
7191 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7192 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
7193 if (mips_relax.sequence)
7194 relax_end ();
98d3f06f 7195 }
252b5132 7196 }
0a44bf69 7197 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 7198 {
9117d219
NC
7199 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7200
252b5132
RH
7201 /* If this is a reference to an external symbol, and there
7202 is no constant, we want
7203 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 7204 or for lca or if tempreg is PIC_CALL_REG
9117d219 7205 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
7206 For a local symbol, we want
7207 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7208 nop
7209 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7210
7211 If we have a small constant, and this is a reference to
7212 an external symbol, we want
7213 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7214 nop
7215 addiu $tempreg,$tempreg,<constant>
7216 For a local symbol, we want the same instruction
7217 sequence, but we output a BFD_RELOC_LO16 reloc on the
7218 addiu instruction.
7219
7220 If we have a large constant, and this is a reference to
7221 an external symbol, we want
7222 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7223 lui $at,<hiconstant>
7224 addiu $at,$at,<loconstant>
7225 addu $tempreg,$tempreg,$at
7226 For a local symbol, we want the same instruction
7227 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 7228 addiu instruction.
ed6fb7bd
SC
7229 */
7230
4d7206a2 7231 if (offset_expr.X_add_number == 0)
252b5132 7232 {
0a44bf69
RS
7233 if (mips_pic == SVR4_PIC
7234 && breg == 0
7235 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
7236 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7237
7238 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7239 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7240 lw_reloc_type, mips_gp_register);
4d7206a2 7241 if (breg != 0)
252b5132
RH
7242 {
7243 /* We're going to put in an addu instruction using
7244 tempreg, so we may as well insert the nop right
7245 now. */
269137b2 7246 load_delay_nop ();
252b5132 7247 }
4d7206a2 7248 relax_switch ();
67c0d1eb
RS
7249 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7250 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7251 load_delay_nop ();
67c0d1eb
RS
7252 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7253 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 7254 relax_end ();
252b5132
RH
7255 /* FIXME: If breg == 0, and the next instruction uses
7256 $tempreg, then if this variant case is used an extra
7257 nop will be generated. */
7258 }
4d7206a2
RS
7259 else if (offset_expr.X_add_number >= -0x8000
7260 && offset_expr.X_add_number < 0x8000)
252b5132 7261 {
67c0d1eb 7262 load_got_offset (tempreg, &offset_expr);
269137b2 7263 load_delay_nop ();
67c0d1eb 7264 add_got_offset (tempreg, &offset_expr);
252b5132
RH
7265 }
7266 else
7267 {
4d7206a2
RS
7268 expr1.X_add_number = offset_expr.X_add_number;
7269 offset_expr.X_add_number =
7270 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
67c0d1eb 7271 load_got_offset (tempreg, &offset_expr);
f6a22291 7272 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
7273 /* If we are going to add in a base register, and the
7274 target register and the base register are the same,
7275 then we are using AT as a temporary register. Since
7276 we want to load the constant into AT, we add our
7277 current AT (from the global offset table) and the
7278 register into the register now, and pretend we were
7279 not using a base register. */
67c0d1eb 7280 if (breg == treg)
252b5132 7281 {
269137b2 7282 load_delay_nop ();
67c0d1eb 7283 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7284 treg, AT, breg);
252b5132
RH
7285 breg = 0;
7286 tempreg = treg;
252b5132 7287 }
f6a22291 7288 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
7289 used_at = 1;
7290 }
7291 }
0a44bf69 7292 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 7293 {
67c0d1eb 7294 int add_breg_early = 0;
f5040a92
AO
7295
7296 /* If this is a reference to an external, and there is no
7297 constant, or local symbol (*), with or without a
7298 constant, we want
7299 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 7300 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
7301 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7302
7303 If we have a small constant, and this is a reference to
7304 an external symbol, we want
7305 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7306 addiu $tempreg,$tempreg,<constant>
7307
7308 If we have a large constant, and this is a reference to
7309 an external symbol, we want
7310 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7311 lui $at,<hiconstant>
7312 addiu $at,$at,<loconstant>
7313 addu $tempreg,$tempreg,$at
7314
7315 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7316 local symbols, even though it introduces an additional
7317 instruction. */
7318
f5040a92
AO
7319 if (offset_expr.X_add_number)
7320 {
4d7206a2 7321 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
7322 offset_expr.X_add_number = 0;
7323
4d7206a2 7324 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7325 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7326 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7327
7328 if (expr1.X_add_number >= -0x8000
7329 && expr1.X_add_number < 0x8000)
7330 {
67c0d1eb
RS
7331 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7332 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 7333 }
ecd13cd3 7334 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 7335 {
f5040a92
AO
7336 /* If we are going to add in a base register, and the
7337 target register and the base register are the same,
7338 then we are using AT as a temporary register. Since
7339 we want to load the constant into AT, we add our
7340 current AT (from the global offset table) and the
7341 register into the register now, and pretend we were
7342 not using a base register. */
7343 if (breg != treg)
7344 dreg = tempreg;
7345 else
7346 {
9c2799c2 7347 gas_assert (tempreg == AT);
67c0d1eb
RS
7348 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7349 treg, AT, breg);
f5040a92 7350 dreg = treg;
67c0d1eb 7351 add_breg_early = 1;
f5040a92
AO
7352 }
7353
f6a22291 7354 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7355 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7356 dreg, dreg, AT);
f5040a92 7357
f5040a92
AO
7358 used_at = 1;
7359 }
7360 else
7361 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7362
4d7206a2 7363 relax_switch ();
f5040a92
AO
7364 offset_expr.X_add_number = expr1.X_add_number;
7365
67c0d1eb
RS
7366 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7367 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7368 if (add_breg_early)
f5040a92 7369 {
67c0d1eb 7370 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 7371 treg, tempreg, breg);
f5040a92
AO
7372 breg = 0;
7373 tempreg = treg;
7374 }
4d7206a2 7375 relax_end ();
f5040a92 7376 }
4d7206a2 7377 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 7378 {
4d7206a2 7379 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7380 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7381 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 7382 relax_switch ();
67c0d1eb
RS
7383 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7384 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 7385 relax_end ();
f5040a92 7386 }
4d7206a2 7387 else
f5040a92 7388 {
67c0d1eb
RS
7389 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7390 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7391 }
7392 }
0a44bf69 7393 else if (mips_big_got && !HAVE_NEWABI)
252b5132 7394 {
67c0d1eb 7395 int gpdelay;
9117d219
NC
7396 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7397 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 7398 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
7399
7400 /* This is the large GOT case. If this is a reference to an
7401 external symbol, and there is no constant, we want
7402 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7403 addu $tempreg,$tempreg,$gp
7404 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 7405 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
7406 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7407 addu $tempreg,$tempreg,$gp
7408 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
7409 For a local symbol, we want
7410 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7411 nop
7412 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7413
7414 If we have a small constant, and this is a reference to
7415 an external symbol, we want
7416 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7417 addu $tempreg,$tempreg,$gp
7418 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7419 nop
7420 addiu $tempreg,$tempreg,<constant>
7421 For a local symbol, we want
7422 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7423 nop
7424 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7425
7426 If we have a large constant, and this is a reference to
7427 an external symbol, we want
7428 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7429 addu $tempreg,$tempreg,$gp
7430 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7431 lui $at,<hiconstant>
7432 addiu $at,$at,<loconstant>
7433 addu $tempreg,$tempreg,$at
7434 For a local symbol, we want
7435 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7436 lui $at,<hiconstant>
7437 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
7438 addu $tempreg,$tempreg,$at
f5040a92 7439 */
438c16b8 7440
252b5132
RH
7441 expr1.X_add_number = offset_expr.X_add_number;
7442 offset_expr.X_add_number = 0;
4d7206a2 7443 relax_start (offset_expr.X_add_symbol);
67c0d1eb 7444 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
7445 if (expr1.X_add_number == 0 && breg == 0
7446 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
7447 {
7448 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7449 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7450 }
df58fc94 7451 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 7452 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7453 tempreg, tempreg, mips_gp_register);
67c0d1eb 7454 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 7455 tempreg, lw_reloc_type, tempreg);
252b5132
RH
7456 if (expr1.X_add_number == 0)
7457 {
67c0d1eb 7458 if (breg != 0)
252b5132
RH
7459 {
7460 /* We're going to put in an addu instruction using
7461 tempreg, so we may as well insert the nop right
7462 now. */
269137b2 7463 load_delay_nop ();
252b5132 7464 }
252b5132
RH
7465 }
7466 else if (expr1.X_add_number >= -0x8000
7467 && expr1.X_add_number < 0x8000)
7468 {
269137b2 7469 load_delay_nop ();
67c0d1eb 7470 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7471 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
7472 }
7473 else
7474 {
252b5132
RH
7475 /* If we are going to add in a base register, and the
7476 target register and the base register are the same,
7477 then we are using AT as a temporary register. Since
7478 we want to load the constant into AT, we add our
7479 current AT (from the global offset table) and the
7480 register into the register now, and pretend we were
7481 not using a base register. */
7482 if (breg != treg)
67c0d1eb 7483 dreg = tempreg;
252b5132
RH
7484 else
7485 {
9c2799c2 7486 gas_assert (tempreg == AT);
269137b2 7487 load_delay_nop ();
67c0d1eb 7488 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7489 treg, AT, breg);
252b5132 7490 dreg = treg;
252b5132
RH
7491 }
7492
f6a22291 7493 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7494 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 7495
252b5132
RH
7496 used_at = 1;
7497 }
4d7206a2
RS
7498 offset_expr.X_add_number =
7499 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
7500 relax_switch ();
252b5132 7501
67c0d1eb 7502 if (gpdelay)
252b5132
RH
7503 {
7504 /* This is needed because this instruction uses $gp, but
f5040a92 7505 the first instruction on the main stream does not. */
67c0d1eb 7506 macro_build (NULL, "nop", "");
252b5132 7507 }
ed6fb7bd 7508
67c0d1eb
RS
7509 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7510 local_reloc_type, mips_gp_register);
f5040a92 7511 if (expr1.X_add_number >= -0x8000
252b5132
RH
7512 && expr1.X_add_number < 0x8000)
7513 {
269137b2 7514 load_delay_nop ();
67c0d1eb
RS
7515 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7516 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 7517 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
7518 register, the external symbol case ended with a load,
7519 so if the symbol turns out to not be external, and
7520 the next instruction uses tempreg, an unnecessary nop
7521 will be inserted. */
252b5132
RH
7522 }
7523 else
7524 {
7525 if (breg == treg)
7526 {
7527 /* We must add in the base register now, as in the
f5040a92 7528 external symbol case. */
9c2799c2 7529 gas_assert (tempreg == AT);
269137b2 7530 load_delay_nop ();
67c0d1eb 7531 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7532 treg, AT, breg);
252b5132
RH
7533 tempreg = treg;
7534 /* We set breg to 0 because we have arranged to add
f5040a92 7535 it in in both cases. */
252b5132
RH
7536 breg = 0;
7537 }
7538
67c0d1eb
RS
7539 macro_build_lui (&expr1, AT);
7540 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7541 AT, AT, BFD_RELOC_LO16);
67c0d1eb 7542 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7543 tempreg, tempreg, AT);
8fc2e39e 7544 used_at = 1;
252b5132 7545 }
4d7206a2 7546 relax_end ();
252b5132 7547 }
0a44bf69 7548 else if (mips_big_got && HAVE_NEWABI)
f5040a92 7549 {
f5040a92
AO
7550 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7551 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 7552 int add_breg_early = 0;
f5040a92
AO
7553
7554 /* This is the large GOT case. If this is a reference to an
7555 external symbol, and there is no constant, we want
7556 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7557 add $tempreg,$tempreg,$gp
7558 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 7559 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
7560 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7561 add $tempreg,$tempreg,$gp
7562 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7563
7564 If we have a small constant, and this is a reference to
7565 an external symbol, we want
7566 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7567 add $tempreg,$tempreg,$gp
7568 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7569 addi $tempreg,$tempreg,<constant>
7570
7571 If we have a large constant, and this is a reference to
7572 an external symbol, we want
7573 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7574 addu $tempreg,$tempreg,$gp
7575 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7576 lui $at,<hiconstant>
7577 addi $at,$at,<loconstant>
7578 add $tempreg,$tempreg,$at
7579
7580 If we have NewABI, and we know it's a local symbol, we want
7581 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
7582 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
7583 otherwise we have to resort to GOT_HI16/GOT_LO16. */
7584
4d7206a2 7585 relax_start (offset_expr.X_add_symbol);
f5040a92 7586
4d7206a2 7587 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
7588 offset_expr.X_add_number = 0;
7589
1abe91b1
MR
7590 if (expr1.X_add_number == 0 && breg == 0
7591 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
7592 {
7593 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7594 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7595 }
df58fc94 7596 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 7597 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7598 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
7599 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7600 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
7601
7602 if (expr1.X_add_number == 0)
4d7206a2 7603 ;
f5040a92
AO
7604 else if (expr1.X_add_number >= -0x8000
7605 && expr1.X_add_number < 0x8000)
7606 {
67c0d1eb 7607 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7608 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 7609 }
ecd13cd3 7610 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 7611 {
f5040a92
AO
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)
7620 dreg = tempreg;
7621 else
7622 {
9c2799c2 7623 gas_assert (tempreg == AT);
67c0d1eb 7624 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7625 treg, AT, breg);
f5040a92 7626 dreg = treg;
67c0d1eb 7627 add_breg_early = 1;
f5040a92
AO
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);
f5040a92 7632
f5040a92
AO
7633 used_at = 1;
7634 }
7635 else
7636 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7637
4d7206a2 7638 relax_switch ();
f5040a92 7639 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
7640 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7641 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
7642 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
7643 tempreg, BFD_RELOC_MIPS_GOT_OFST);
7644 if (add_breg_early)
f5040a92 7645 {
67c0d1eb 7646 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7647 treg, tempreg, breg);
f5040a92
AO
7648 breg = 0;
7649 tempreg = treg;
7650 }
4d7206a2 7651 relax_end ();
f5040a92 7652 }
252b5132
RH
7653 else
7654 abort ();
7655
7656 if (breg != 0)
aed1a261 7657 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
7658 break;
7659
52b6b6b9 7660 case M_MSGSND:
df58fc94 7661 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7662 {
7663 unsigned long temp = (treg << 16) | (0x01);
7664 macro_build (NULL, "c2", "C", temp);
7665 }
c7af4273 7666 break;
52b6b6b9
JM
7667
7668 case M_MSGLD:
df58fc94 7669 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7670 {
7671 unsigned long temp = (0x02);
7672 macro_build (NULL, "c2", "C", temp);
7673 }
c7af4273 7674 break;
52b6b6b9
JM
7675
7676 case M_MSGLD_T:
df58fc94 7677 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7678 {
7679 unsigned long temp = (treg << 16) | (0x02);
7680 macro_build (NULL, "c2", "C", temp);
7681 }
c7af4273 7682 break;
52b6b6b9
JM
7683
7684 case M_MSGWAIT:
df58fc94 7685 gas_assert (!mips_opts.micromips);
52b6b6b9 7686 macro_build (NULL, "c2", "C", 3);
c7af4273 7687 break;
52b6b6b9
JM
7688
7689 case M_MSGWAIT_T:
df58fc94 7690 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7691 {
7692 unsigned long temp = (treg << 16) | 0x03;
7693 macro_build (NULL, "c2", "C", temp);
7694 }
c7af4273 7695 break;
52b6b6b9 7696
252b5132
RH
7697 case M_J_A:
7698 /* The j instruction may not be used in PIC code, since it
7699 requires an absolute address. We convert it to a b
7700 instruction. */
7701 if (mips_pic == NO_PIC)
67c0d1eb 7702 macro_build (&offset_expr, "j", "a");
252b5132 7703 else
67c0d1eb 7704 macro_build (&offset_expr, "b", "p");
8fc2e39e 7705 break;
252b5132
RH
7706
7707 /* The jal instructions must be handled as macros because when
7708 generating PIC code they expand to multi-instruction
7709 sequences. Normally they are simple instructions. */
df58fc94
RS
7710 case M_JALS_1:
7711 dreg = RA;
7712 /* Fall through. */
7713 case M_JALS_2:
7714 gas_assert (mips_opts.micromips);
7715 jals = 1;
7716 goto jal;
252b5132
RH
7717 case M_JAL_1:
7718 dreg = RA;
7719 /* Fall through. */
7720 case M_JAL_2:
df58fc94 7721 jal:
3e722fb5 7722 if (mips_pic == NO_PIC)
df58fc94
RS
7723 {
7724 s = jals ? "jalrs" : "jalr";
7725 if (mips_opts.micromips && dreg == RA)
7726 macro_build (NULL, s, "mj", sreg);
7727 else
7728 macro_build (NULL, s, JALR_FMT, dreg, sreg);
7729 }
0a44bf69 7730 else
252b5132 7731 {
df58fc94
RS
7732 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
7733 && mips_cprestore_offset >= 0);
7734
252b5132
RH
7735 if (sreg != PIC_CALL_REG)
7736 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 7737
df58fc94
RS
7738 s = (mips_opts.micromips && (!mips_opts.noreorder || cprestore)
7739 ? "jalrs" : "jalr");
7740 if (mips_opts.micromips && dreg == RA)
7741 macro_build (NULL, s, "mj", sreg);
7742 else
7743 macro_build (NULL, s, JALR_FMT, dreg, sreg);
0a44bf69 7744 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 7745 {
6478892d
TS
7746 if (mips_cprestore_offset < 0)
7747 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7748 else
7749 {
90ecf173 7750 if (!mips_frame_reg_valid)
7a621144
DJ
7751 {
7752 as_warn (_("No .frame pseudo-op used in PIC code"));
7753 /* Quiet this warning. */
7754 mips_frame_reg_valid = 1;
7755 }
90ecf173 7756 if (!mips_cprestore_valid)
7a621144
DJ
7757 {
7758 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7759 /* Quiet this warning. */
7760 mips_cprestore_valid = 1;
7761 }
d3fca0b5
MR
7762 if (mips_opts.noreorder)
7763 macro_build (NULL, "nop", "");
6478892d 7764 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 7765 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 7766 mips_gp_register,
256ab948
TS
7767 mips_frame_reg,
7768 HAVE_64BIT_ADDRESSES);
6478892d 7769 }
252b5132
RH
7770 }
7771 }
252b5132 7772
8fc2e39e 7773 break;
252b5132 7774
df58fc94
RS
7775 case M_JALS_A:
7776 gas_assert (mips_opts.micromips);
7777 jals = 1;
7778 /* Fall through. */
252b5132
RH
7779 case M_JAL_A:
7780 if (mips_pic == NO_PIC)
df58fc94 7781 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
7782 else if (mips_pic == SVR4_PIC)
7783 {
7784 /* If this is a reference to an external symbol, and we are
7785 using a small GOT, we want
7786 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7787 nop
f9419b05 7788 jalr $ra,$25
252b5132
RH
7789 nop
7790 lw $gp,cprestore($sp)
7791 The cprestore value is set using the .cprestore
7792 pseudo-op. If we are using a big GOT, we want
7793 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7794 addu $25,$25,$gp
7795 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
7796 nop
f9419b05 7797 jalr $ra,$25
252b5132
RH
7798 nop
7799 lw $gp,cprestore($sp)
7800 If the symbol is not external, we want
7801 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7802 nop
7803 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 7804 jalr $ra,$25
252b5132 7805 nop
438c16b8 7806 lw $gp,cprestore($sp)
f5040a92
AO
7807
7808 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
7809 sequences above, minus nops, unless the symbol is local,
7810 which enables us to use GOT_PAGE/GOT_OFST (big got) or
7811 GOT_DISP. */
438c16b8 7812 if (HAVE_NEWABI)
252b5132 7813 {
90ecf173 7814 if (!mips_big_got)
f5040a92 7815 {
4d7206a2 7816 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7817 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7818 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 7819 mips_gp_register);
4d7206a2 7820 relax_switch ();
67c0d1eb
RS
7821 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7822 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
7823 mips_gp_register);
7824 relax_end ();
f5040a92
AO
7825 }
7826 else
7827 {
4d7206a2 7828 relax_start (offset_expr.X_add_symbol);
df58fc94 7829 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
7830 BFD_RELOC_MIPS_CALL_HI16);
7831 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
7832 PIC_CALL_REG, mips_gp_register);
7833 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7834 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
7835 PIC_CALL_REG);
4d7206a2 7836 relax_switch ();
67c0d1eb
RS
7837 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7838 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
7839 mips_gp_register);
7840 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7841 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 7842 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 7843 relax_end ();
f5040a92 7844 }
684022ea 7845
df58fc94 7846 macro_build_jalr (&offset_expr, 0);
252b5132
RH
7847 }
7848 else
7849 {
4d7206a2 7850 relax_start (offset_expr.X_add_symbol);
90ecf173 7851 if (!mips_big_got)
438c16b8 7852 {
67c0d1eb
RS
7853 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7854 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 7855 mips_gp_register);
269137b2 7856 load_delay_nop ();
4d7206a2 7857 relax_switch ();
438c16b8 7858 }
252b5132 7859 else
252b5132 7860 {
67c0d1eb
RS
7861 int gpdelay;
7862
7863 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 7864 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
7865 BFD_RELOC_MIPS_CALL_HI16);
7866 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
7867 PIC_CALL_REG, mips_gp_register);
7868 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7869 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
7870 PIC_CALL_REG);
269137b2 7871 load_delay_nop ();
4d7206a2 7872 relax_switch ();
67c0d1eb
RS
7873 if (gpdelay)
7874 macro_build (NULL, "nop", "");
252b5132 7875 }
67c0d1eb
RS
7876 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7877 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 7878 mips_gp_register);
269137b2 7879 load_delay_nop ();
67c0d1eb
RS
7880 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7881 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 7882 relax_end ();
df58fc94 7883 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 7884
6478892d
TS
7885 if (mips_cprestore_offset < 0)
7886 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7887 else
7888 {
90ecf173 7889 if (!mips_frame_reg_valid)
7a621144
DJ
7890 {
7891 as_warn (_("No .frame pseudo-op used in PIC code"));
7892 /* Quiet this warning. */
7893 mips_frame_reg_valid = 1;
7894 }
90ecf173 7895 if (!mips_cprestore_valid)
7a621144
DJ
7896 {
7897 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7898 /* Quiet this warning. */
7899 mips_cprestore_valid = 1;
7900 }
6478892d 7901 if (mips_opts.noreorder)
67c0d1eb 7902 macro_build (NULL, "nop", "");
6478892d 7903 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 7904 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 7905 mips_gp_register,
256ab948
TS
7906 mips_frame_reg,
7907 HAVE_64BIT_ADDRESSES);
6478892d 7908 }
252b5132
RH
7909 }
7910 }
0a44bf69
RS
7911 else if (mips_pic == VXWORKS_PIC)
7912 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
7913 else
7914 abort ();
7915
8fc2e39e 7916 break;
252b5132 7917
dec0624d
MR
7918 case M_ACLR_AB:
7919 ab = 1;
7920 case M_ACLR_OB:
7921 s = "aclr";
7922 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
7923 fmt = "\\,~(b)";
7924 off12 = 1;
7925 goto ld_st;
7926 case M_ASET_AB:
7927 ab = 1;
7928 case M_ASET_OB:
7929 s = "aset";
7930 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
7931 fmt = "\\,~(b)";
7932 off12 = 1;
7933 goto ld_st;
252b5132 7934 case M_LB_AB:
df58fc94 7935 ab = 1;
252b5132 7936 s = "lb";
df58fc94 7937 fmt = "t,o(b)";
252b5132
RH
7938 goto ld;
7939 case M_LBU_AB:
df58fc94 7940 ab = 1;
252b5132 7941 s = "lbu";
df58fc94 7942 fmt = "t,o(b)";
252b5132
RH
7943 goto ld;
7944 case M_LH_AB:
df58fc94 7945 ab = 1;
252b5132 7946 s = "lh";
df58fc94 7947 fmt = "t,o(b)";
252b5132
RH
7948 goto ld;
7949 case M_LHU_AB:
df58fc94 7950 ab = 1;
252b5132 7951 s = "lhu";
df58fc94 7952 fmt = "t,o(b)";
252b5132
RH
7953 goto ld;
7954 case M_LW_AB:
df58fc94 7955 ab = 1;
252b5132 7956 s = "lw";
df58fc94 7957 fmt = "t,o(b)";
252b5132
RH
7958 goto ld;
7959 case M_LWC0_AB:
df58fc94
RS
7960 ab = 1;
7961 gas_assert (!mips_opts.micromips);
252b5132 7962 s = "lwc0";
df58fc94 7963 fmt = "E,o(b)";
bdaaa2e1 7964 /* Itbl support may require additional care here. */
252b5132 7965 coproc = 1;
df58fc94 7966 goto ld_st;
252b5132 7967 case M_LWC1_AB:
df58fc94 7968 ab = 1;
252b5132 7969 s = "lwc1";
df58fc94 7970 fmt = "T,o(b)";
bdaaa2e1 7971 /* Itbl support may require additional care here. */
252b5132 7972 coproc = 1;
df58fc94 7973 goto ld_st;
252b5132 7974 case M_LWC2_AB:
df58fc94
RS
7975 ab = 1;
7976 case M_LWC2_OB:
252b5132 7977 s = "lwc2";
df58fc94
RS
7978 fmt = COP12_FMT;
7979 off12 = mips_opts.micromips;
bdaaa2e1 7980 /* Itbl support may require additional care here. */
252b5132 7981 coproc = 1;
df58fc94 7982 goto ld_st;
252b5132 7983 case M_LWC3_AB:
df58fc94
RS
7984 ab = 1;
7985 gas_assert (!mips_opts.micromips);
252b5132 7986 s = "lwc3";
df58fc94 7987 fmt = "E,o(b)";
bdaaa2e1 7988 /* Itbl support may require additional care here. */
252b5132 7989 coproc = 1;
df58fc94 7990 goto ld_st;
252b5132 7991 case M_LWL_AB:
df58fc94
RS
7992 ab = 1;
7993 case M_LWL_OB:
252b5132 7994 s = "lwl";
df58fc94
RS
7995 fmt = MEM12_FMT;
7996 off12 = mips_opts.micromips;
7997 goto ld_st;
252b5132 7998 case M_LWR_AB:
df58fc94
RS
7999 ab = 1;
8000 case M_LWR_OB:
252b5132 8001 s = "lwr";
df58fc94
RS
8002 fmt = MEM12_FMT;
8003 off12 = mips_opts.micromips;
8004 goto ld_st;
252b5132 8005 case M_LDC1_AB:
df58fc94 8006 ab = 1;
252b5132 8007 s = "ldc1";
df58fc94 8008 fmt = "T,o(b)";
bdaaa2e1 8009 /* Itbl support may require additional care here. */
252b5132 8010 coproc = 1;
df58fc94 8011 goto ld_st;
252b5132 8012 case M_LDC2_AB:
df58fc94
RS
8013 ab = 1;
8014 case M_LDC2_OB:
252b5132 8015 s = "ldc2";
df58fc94
RS
8016 fmt = COP12_FMT;
8017 off12 = mips_opts.micromips;
bdaaa2e1 8018 /* Itbl support may require additional care here. */
252b5132 8019 coproc = 1;
df58fc94 8020 goto ld_st;
252b5132 8021 case M_LDC3_AB:
df58fc94 8022 ab = 1;
252b5132 8023 s = "ldc3";
df58fc94 8024 fmt = "E,o(b)";
bdaaa2e1 8025 /* Itbl support may require additional care here. */
252b5132 8026 coproc = 1;
df58fc94 8027 goto ld_st;
252b5132 8028 case M_LDL_AB:
df58fc94
RS
8029 ab = 1;
8030 case M_LDL_OB:
252b5132 8031 s = "ldl";
df58fc94
RS
8032 fmt = MEM12_FMT;
8033 off12 = mips_opts.micromips;
8034 goto ld_st;
252b5132 8035 case M_LDR_AB:
df58fc94
RS
8036 ab = 1;
8037 case M_LDR_OB:
252b5132 8038 s = "ldr";
df58fc94
RS
8039 fmt = MEM12_FMT;
8040 off12 = mips_opts.micromips;
8041 goto ld_st;
252b5132 8042 case M_LL_AB:
df58fc94
RS
8043 ab = 1;
8044 case M_LL_OB:
252b5132 8045 s = "ll";
df58fc94
RS
8046 fmt = MEM12_FMT;
8047 off12 = mips_opts.micromips;
252b5132
RH
8048 goto ld;
8049 case M_LLD_AB:
df58fc94
RS
8050 ab = 1;
8051 case M_LLD_OB:
252b5132 8052 s = "lld";
df58fc94
RS
8053 fmt = MEM12_FMT;
8054 off12 = mips_opts.micromips;
252b5132
RH
8055 goto ld;
8056 case M_LWU_AB:
df58fc94
RS
8057 ab = 1;
8058 case M_LWU_OB:
252b5132 8059 s = "lwu";
df58fc94
RS
8060 fmt = MEM12_FMT;
8061 off12 = mips_opts.micromips;
8062 goto ld;
8063 case M_LWP_AB:
8064 ab = 1;
8065 case M_LWP_OB:
8066 gas_assert (mips_opts.micromips);
8067 s = "lwp";
8068 fmt = "t,~(b)";
8069 off12 = 1;
8070 lp = 1;
8071 goto ld;
8072 case M_LDP_AB:
8073 ab = 1;
8074 case M_LDP_OB:
8075 gas_assert (mips_opts.micromips);
8076 s = "ldp";
8077 fmt = "t,~(b)";
8078 off12 = 1;
8079 lp = 1;
8080 goto ld;
8081 case M_LWM_AB:
8082 ab = 1;
8083 case M_LWM_OB:
8084 gas_assert (mips_opts.micromips);
8085 s = "lwm";
8086 fmt = "n,~(b)";
8087 off12 = 1;
8088 goto ld_st;
8089 case M_LDM_AB:
8090 ab = 1;
8091 case M_LDM_OB:
8092 gas_assert (mips_opts.micromips);
8093 s = "ldm";
8094 fmt = "n,~(b)";
8095 off12 = 1;
8096 goto ld_st;
8097
252b5132 8098 ld:
df58fc94
RS
8099 if (breg == treg + lp)
8100 goto ld_st;
252b5132 8101 else
df58fc94
RS
8102 tempreg = treg + lp;
8103 goto ld_noat;
8104
252b5132 8105 case M_SB_AB:
df58fc94 8106 ab = 1;
252b5132 8107 s = "sb";
df58fc94
RS
8108 fmt = "t,o(b)";
8109 goto ld_st;
252b5132 8110 case M_SH_AB:
df58fc94 8111 ab = 1;
252b5132 8112 s = "sh";
df58fc94
RS
8113 fmt = "t,o(b)";
8114 goto ld_st;
252b5132 8115 case M_SW_AB:
df58fc94 8116 ab = 1;
252b5132 8117 s = "sw";
df58fc94
RS
8118 fmt = "t,o(b)";
8119 goto ld_st;
252b5132 8120 case M_SWC0_AB:
df58fc94
RS
8121 ab = 1;
8122 gas_assert (!mips_opts.micromips);
252b5132 8123 s = "swc0";
df58fc94 8124 fmt = "E,o(b)";
bdaaa2e1 8125 /* Itbl support may require additional care here. */
252b5132 8126 coproc = 1;
df58fc94 8127 goto ld_st;
252b5132 8128 case M_SWC1_AB:
df58fc94 8129 ab = 1;
252b5132 8130 s = "swc1";
df58fc94 8131 fmt = "T,o(b)";
bdaaa2e1 8132 /* Itbl support may require additional care here. */
252b5132 8133 coproc = 1;
df58fc94 8134 goto ld_st;
252b5132 8135 case M_SWC2_AB:
df58fc94
RS
8136 ab = 1;
8137 case M_SWC2_OB:
252b5132 8138 s = "swc2";
df58fc94
RS
8139 fmt = COP12_FMT;
8140 off12 = mips_opts.micromips;
bdaaa2e1 8141 /* Itbl support may require additional care here. */
252b5132 8142 coproc = 1;
df58fc94 8143 goto ld_st;
252b5132 8144 case M_SWC3_AB:
df58fc94
RS
8145 ab = 1;
8146 gas_assert (!mips_opts.micromips);
252b5132 8147 s = "swc3";
df58fc94 8148 fmt = "E,o(b)";
bdaaa2e1 8149 /* Itbl support may require additional care here. */
252b5132 8150 coproc = 1;
df58fc94 8151 goto ld_st;
252b5132 8152 case M_SWL_AB:
df58fc94
RS
8153 ab = 1;
8154 case M_SWL_OB:
252b5132 8155 s = "swl";
df58fc94
RS
8156 fmt = MEM12_FMT;
8157 off12 = mips_opts.micromips;
8158 goto ld_st;
252b5132 8159 case M_SWR_AB:
df58fc94
RS
8160 ab = 1;
8161 case M_SWR_OB:
252b5132 8162 s = "swr";
df58fc94
RS
8163 fmt = MEM12_FMT;
8164 off12 = mips_opts.micromips;
8165 goto ld_st;
252b5132 8166 case M_SC_AB:
df58fc94
RS
8167 ab = 1;
8168 case M_SC_OB:
252b5132 8169 s = "sc";
df58fc94
RS
8170 fmt = MEM12_FMT;
8171 off12 = mips_opts.micromips;
8172 goto ld_st;
252b5132 8173 case M_SCD_AB:
df58fc94
RS
8174 ab = 1;
8175 case M_SCD_OB:
252b5132 8176 s = "scd";
df58fc94
RS
8177 fmt = MEM12_FMT;
8178 off12 = mips_opts.micromips;
8179 goto ld_st;
d43b4baf 8180 case M_CACHE_AB:
df58fc94
RS
8181 ab = 1;
8182 case M_CACHE_OB:
d43b4baf 8183 s = "cache";
df58fc94
RS
8184 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
8185 off12 = mips_opts.micromips;
8186 goto ld_st;
3eebd5eb 8187 case M_PREF_AB:
df58fc94
RS
8188 ab = 1;
8189 case M_PREF_OB:
3eebd5eb 8190 s = "pref";
df58fc94
RS
8191 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
8192 off12 = mips_opts.micromips;
8193 goto ld_st;
252b5132 8194 case M_SDC1_AB:
df58fc94 8195 ab = 1;
252b5132 8196 s = "sdc1";
df58fc94 8197 fmt = "T,o(b)";
252b5132 8198 coproc = 1;
bdaaa2e1 8199 /* Itbl support may require additional care here. */
df58fc94 8200 goto ld_st;
252b5132 8201 case M_SDC2_AB:
df58fc94
RS
8202 ab = 1;
8203 case M_SDC2_OB:
252b5132 8204 s = "sdc2";
df58fc94
RS
8205 fmt = COP12_FMT;
8206 off12 = mips_opts.micromips;
bdaaa2e1 8207 /* Itbl support may require additional care here. */
252b5132 8208 coproc = 1;
df58fc94 8209 goto ld_st;
252b5132 8210 case M_SDC3_AB:
df58fc94
RS
8211 ab = 1;
8212 gas_assert (!mips_opts.micromips);
252b5132 8213 s = "sdc3";
df58fc94 8214 fmt = "E,o(b)";
bdaaa2e1 8215 /* Itbl support may require additional care here. */
252b5132 8216 coproc = 1;
df58fc94 8217 goto ld_st;
252b5132 8218 case M_SDL_AB:
df58fc94
RS
8219 ab = 1;
8220 case M_SDL_OB:
252b5132 8221 s = "sdl";
df58fc94
RS
8222 fmt = MEM12_FMT;
8223 off12 = mips_opts.micromips;
8224 goto ld_st;
252b5132 8225 case M_SDR_AB:
df58fc94
RS
8226 ab = 1;
8227 case M_SDR_OB:
252b5132 8228 s = "sdr";
df58fc94
RS
8229 fmt = MEM12_FMT;
8230 off12 = mips_opts.micromips;
8231 goto ld_st;
8232 case M_SWP_AB:
8233 ab = 1;
8234 case M_SWP_OB:
8235 gas_assert (mips_opts.micromips);
8236 s = "swp";
8237 fmt = "t,~(b)";
8238 off12 = 1;
8239 goto ld_st;
8240 case M_SDP_AB:
8241 ab = 1;
8242 case M_SDP_OB:
8243 gas_assert (mips_opts.micromips);
8244 s = "sdp";
8245 fmt = "t,~(b)";
8246 off12 = 1;
8247 goto ld_st;
8248 case M_SWM_AB:
8249 ab = 1;
8250 case M_SWM_OB:
8251 gas_assert (mips_opts.micromips);
8252 s = "swm";
8253 fmt = "n,~(b)";
8254 off12 = 1;
8255 goto ld_st;
8256 case M_SDM_AB:
8257 ab = 1;
8258 case M_SDM_OB:
8259 gas_assert (mips_opts.micromips);
8260 s = "sdm";
8261 fmt = "n,~(b)";
8262 off12 = 1;
8263
8264 ld_st:
8fc2e39e
TS
8265 tempreg = AT;
8266 used_at = 1;
df58fc94 8267 ld_noat:
b19e8a9b
AN
8268 if (coproc
8269 && NO_ISA_COP (mips_opts.arch)
8270 && (ip->insn_mo->pinfo2 & (INSN2_M_FP_S | INSN2_M_FP_D)) == 0)
8271 {
f71d0d44 8272 as_bad (_("Opcode not supported on this processor: %s"),
b19e8a9b
AN
8273 mips_cpu_info_from_arch (mips_opts.arch)->name);
8274 break;
8275 }
8276
252b5132
RH
8277 if (offset_expr.X_op != O_constant
8278 && offset_expr.X_op != O_symbol)
8279 {
f71d0d44 8280 as_bad (_("Expression too complex"));
252b5132
RH
8281 offset_expr.X_op = O_constant;
8282 }
8283
2051e8c4
MR
8284 if (HAVE_32BIT_ADDRESSES
8285 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
8286 {
8287 char value [32];
8288
8289 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 8290 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 8291 }
2051e8c4 8292
252b5132
RH
8293 /* A constant expression in PIC code can be handled just as it
8294 is in non PIC code. */
aed1a261
RS
8295 if (offset_expr.X_op == O_constant)
8296 {
df58fc94
RS
8297 int hipart = 0;
8298
842f8b2a 8299 expr1.X_add_number = offset_expr.X_add_number;
2051e8c4 8300 normalize_address_expr (&expr1);
df58fc94 8301 if (!off12 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
842f8b2a
MR
8302 {
8303 expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8304 & ~(bfd_vma) 0xffff);
df58fc94
RS
8305 hipart = 1;
8306 }
8307 else if (off12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
8308 {
8309 expr1.X_add_number = ((expr1.X_add_number + 0x800)
8310 & ~(bfd_vma) 0xfff);
8311 hipart = 1;
8312 }
8313 if (hipart)
8314 {
842f8b2a
MR
8315 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8316 if (breg != 0)
8317 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8318 tempreg, tempreg, breg);
8319 breg = tempreg;
8320 }
dd6a37e7
AP
8321 if (off0)
8322 {
8323 if (offset_expr.X_add_number == 0)
8324 tempreg = breg;
8325 else
8326 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8327 "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8328 macro_build (NULL, s, fmt, treg, tempreg);
8329 }
8330 else if (!off12)
df58fc94
RS
8331 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8332 else
8333 macro_build (NULL, s, fmt,
8334 treg, (unsigned long) offset_expr.X_add_number, breg);
8335 }
dd6a37e7 8336 else if (off12 || off0)
df58fc94 8337 {
dd6a37e7
AP
8338 /* A 12-bit or 0-bit offset field is too narrow to be used
8339 for a low-part relocation, so load the whole address into
8340 the auxillary register. In the case of "A(b)" addresses,
8341 we first load absolute address "A" into the register and
8342 then add base register "b". In the case of "o(b)" addresses,
8343 we simply need to add 16-bit offset "o" to base register "b", and
df58fc94
RS
8344 offset_reloc already contains the relocations associated
8345 with "o". */
8346 if (ab)
8347 {
8348 load_address (tempreg, &offset_expr, &used_at);
8349 if (breg != 0)
8350 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8351 tempreg, tempreg, breg);
8352 }
8353 else
8354 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8355 tempreg, breg, -1,
8356 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8357 expr1.X_add_number = 0;
dd6a37e7
AP
8358 if (off0)
8359 macro_build (NULL, s, fmt, treg, tempreg);
8360 else
8361 macro_build (NULL, s, fmt,
8362 treg, (unsigned long) expr1.X_add_number, tempreg);
aed1a261
RS
8363 }
8364 else if (mips_pic == NO_PIC)
252b5132
RH
8365 {
8366 /* If this is a reference to a GP relative symbol, and there
8367 is no base register, we want
cdf6fd85 8368 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
8369 Otherwise, if there is no base register, we want
8370 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8371 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8372 If we have a constant, we need two instructions anyhow,
8373 so we always use the latter form.
8374
8375 If we have a base register, and this is a reference to a
8376 GP relative symbol, we want
8377 addu $tempreg,$breg,$gp
cdf6fd85 8378 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
8379 Otherwise we want
8380 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8381 addu $tempreg,$tempreg,$breg
8382 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 8383 With a constant we always use the latter case.
76b3015f 8384
d6bc6245
TS
8385 With 64bit address space and no base register and $at usable,
8386 we want
8387 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8388 lui $at,<sym> (BFD_RELOC_HI16_S)
8389 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8390 dsll32 $tempreg,0
8391 daddu $tempreg,$at
8392 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8393 If we have a base register, we want
8394 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8395 lui $at,<sym> (BFD_RELOC_HI16_S)
8396 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8397 daddu $at,$breg
8398 dsll32 $tempreg,0
8399 daddu $tempreg,$at
8400 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8401
8402 Without $at we can't generate the optimal path for superscalar
8403 processors here since this would require two temporary registers.
8404 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8405 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8406 dsll $tempreg,16
8407 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8408 dsll $tempreg,16
8409 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8410 If we have a base register, we want
8411 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8412 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8413 dsll $tempreg,16
8414 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8415 dsll $tempreg,16
8416 daddu $tempreg,$tempreg,$breg
8417 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 8418
6caf9ef4 8419 For GP relative symbols in 64bit address space we can use
aed1a261
RS
8420 the same sequence as in 32bit address space. */
8421 if (HAVE_64BIT_SYMBOLS)
d6bc6245 8422 {
aed1a261 8423 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
8424 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8425 {
8426 relax_start (offset_expr.X_add_symbol);
8427 if (breg == 0)
8428 {
8429 macro_build (&offset_expr, s, fmt, treg,
8430 BFD_RELOC_GPREL16, mips_gp_register);
8431 }
8432 else
8433 {
8434 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8435 tempreg, breg, mips_gp_register);
8436 macro_build (&offset_expr, s, fmt, treg,
8437 BFD_RELOC_GPREL16, tempreg);
8438 }
8439 relax_switch ();
8440 }
d6bc6245 8441
741fe287 8442 if (used_at == 0 && mips_opts.at)
d6bc6245 8443 {
df58fc94 8444 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 8445 BFD_RELOC_MIPS_HIGHEST);
df58fc94 8446 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
8447 BFD_RELOC_HI16_S);
8448 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8449 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 8450 if (breg != 0)
67c0d1eb 8451 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 8452 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb
RS
8453 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
8454 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
8455 tempreg);
d6bc6245
TS
8456 used_at = 1;
8457 }
8458 else
8459 {
df58fc94 8460 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
8461 BFD_RELOC_MIPS_HIGHEST);
8462 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8463 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 8464 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
8465 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8466 tempreg, BFD_RELOC_HI16_S);
df58fc94 8467 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 8468 if (breg != 0)
67c0d1eb 8469 macro_build (NULL, "daddu", "d,v,t",
17a2f251 8470 tempreg, tempreg, breg);
67c0d1eb 8471 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8472 BFD_RELOC_LO16, tempreg);
d6bc6245 8473 }
6caf9ef4
TS
8474
8475 if (mips_relax.sequence)
8476 relax_end ();
8fc2e39e 8477 break;
d6bc6245 8478 }
256ab948 8479
252b5132
RH
8480 if (breg == 0)
8481 {
67c0d1eb 8482 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8483 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 8484 {
4d7206a2 8485 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
8486 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
8487 mips_gp_register);
4d7206a2 8488 relax_switch ();
252b5132 8489 }
67c0d1eb
RS
8490 macro_build_lui (&offset_expr, tempreg);
8491 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8492 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
8493 if (mips_relax.sequence)
8494 relax_end ();
252b5132
RH
8495 }
8496 else
8497 {
67c0d1eb 8498 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8499 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 8500 {
4d7206a2 8501 relax_start (offset_expr.X_add_symbol);
67c0d1eb 8502 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8503 tempreg, breg, mips_gp_register);
67c0d1eb 8504 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8505 BFD_RELOC_GPREL16, tempreg);
4d7206a2 8506 relax_switch ();
252b5132 8507 }
67c0d1eb
RS
8508 macro_build_lui (&offset_expr, tempreg);
8509 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8510 tempreg, tempreg, breg);
67c0d1eb 8511 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8512 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
8513 if (mips_relax.sequence)
8514 relax_end ();
252b5132
RH
8515 }
8516 }
0a44bf69 8517 else if (!mips_big_got)
252b5132 8518 {
ed6fb7bd 8519 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 8520
252b5132
RH
8521 /* If this is a reference to an external symbol, we want
8522 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8523 nop
8524 <op> $treg,0($tempreg)
8525 Otherwise we want
8526 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8527 nop
8528 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8529 <op> $treg,0($tempreg)
f5040a92
AO
8530
8531 For NewABI, we want
8532 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8533 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
8534
252b5132
RH
8535 If there is a base register, we add it to $tempreg before
8536 the <op>. If there is a constant, we stick it in the
8537 <op> instruction. We don't handle constants larger than
8538 16 bits, because we have no way to load the upper 16 bits
8539 (actually, we could handle them for the subset of cases
8540 in which we are not using $at). */
9c2799c2 8541 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
8542 if (HAVE_NEWABI)
8543 {
67c0d1eb
RS
8544 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8545 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 8546 if (breg != 0)
67c0d1eb 8547 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8548 tempreg, tempreg, breg);
67c0d1eb 8549 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8550 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
8551 break;
8552 }
252b5132
RH
8553 expr1.X_add_number = offset_expr.X_add_number;
8554 offset_expr.X_add_number = 0;
8555 if (expr1.X_add_number < -0x8000
8556 || expr1.X_add_number >= 0x8000)
8557 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
8558 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8559 lw_reloc_type, mips_gp_register);
269137b2 8560 load_delay_nop ();
4d7206a2
RS
8561 relax_start (offset_expr.X_add_symbol);
8562 relax_switch ();
67c0d1eb
RS
8563 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8564 tempreg, BFD_RELOC_LO16);
4d7206a2 8565 relax_end ();
252b5132 8566 if (breg != 0)
67c0d1eb 8567 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8568 tempreg, tempreg, breg);
67c0d1eb 8569 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 8570 }
0a44bf69 8571 else if (mips_big_got && !HAVE_NEWABI)
252b5132 8572 {
67c0d1eb 8573 int gpdelay;
252b5132
RH
8574
8575 /* If this is a reference to an external symbol, we want
8576 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8577 addu $tempreg,$tempreg,$gp
8578 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8579 <op> $treg,0($tempreg)
8580 Otherwise we want
8581 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8582 nop
8583 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8584 <op> $treg,0($tempreg)
8585 If there is a base register, we add it to $tempreg before
8586 the <op>. If there is a constant, we stick it in the
8587 <op> instruction. We don't handle constants larger than
8588 16 bits, because we have no way to load the upper 16 bits
8589 (actually, we could handle them for the subset of cases
f5040a92 8590 in which we are not using $at). */
9c2799c2 8591 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
8592 expr1.X_add_number = offset_expr.X_add_number;
8593 offset_expr.X_add_number = 0;
8594 if (expr1.X_add_number < -0x8000
8595 || expr1.X_add_number >= 0x8000)
8596 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 8597 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 8598 relax_start (offset_expr.X_add_symbol);
df58fc94 8599 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 8600 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8601 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8602 mips_gp_register);
8603 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8604 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 8605 relax_switch ();
67c0d1eb
RS
8606 if (gpdelay)
8607 macro_build (NULL, "nop", "");
8608 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8609 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 8610 load_delay_nop ();
67c0d1eb
RS
8611 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8612 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
8613 relax_end ();
8614
252b5132 8615 if (breg != 0)
67c0d1eb 8616 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8617 tempreg, tempreg, breg);
67c0d1eb 8618 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 8619 }
0a44bf69 8620 else if (mips_big_got && HAVE_NEWABI)
f5040a92 8621 {
f5040a92
AO
8622 /* If this is a reference to an external symbol, we want
8623 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8624 add $tempreg,$tempreg,$gp
8625 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8626 <op> $treg,<ofst>($tempreg)
8627 Otherwise, for local symbols, we want:
8628 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8629 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 8630 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 8631 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
8632 offset_expr.X_add_number = 0;
8633 if (expr1.X_add_number < -0x8000
8634 || expr1.X_add_number >= 0x8000)
8635 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 8636 relax_start (offset_expr.X_add_symbol);
df58fc94 8637 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 8638 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8639 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8640 mips_gp_register);
8641 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8642 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 8643 if (breg != 0)
67c0d1eb 8644 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8645 tempreg, tempreg, breg);
67c0d1eb 8646 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 8647
4d7206a2 8648 relax_switch ();
f5040a92 8649 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
8650 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8651 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 8652 if (breg != 0)
67c0d1eb 8653 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8654 tempreg, tempreg, breg);
67c0d1eb 8655 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8656 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 8657 relax_end ();
f5040a92 8658 }
252b5132
RH
8659 else
8660 abort ();
8661
252b5132
RH
8662 break;
8663
8664 case M_LI:
8665 case M_LI_S:
67c0d1eb 8666 load_register (treg, &imm_expr, 0);
8fc2e39e 8667 break;
252b5132
RH
8668
8669 case M_DLI:
67c0d1eb 8670 load_register (treg, &imm_expr, 1);
8fc2e39e 8671 break;
252b5132
RH
8672
8673 case M_LI_SS:
8674 if (imm_expr.X_op == O_constant)
8675 {
8fc2e39e 8676 used_at = 1;
67c0d1eb
RS
8677 load_register (AT, &imm_expr, 0);
8678 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
8679 break;
8680 }
8681 else
8682 {
9c2799c2 8683 gas_assert (offset_expr.X_op == O_symbol
90ecf173
MR
8684 && strcmp (segment_name (S_GET_SEGMENT
8685 (offset_expr.X_add_symbol)),
8686 ".lit4") == 0
8687 && offset_expr.X_add_number == 0);
67c0d1eb 8688 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 8689 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 8690 break;
252b5132
RH
8691 }
8692
8693 case M_LI_D:
ca4e0257
RS
8694 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
8695 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
8696 order 32 bits of the value and the low order 32 bits are either
8697 zero or in OFFSET_EXPR. */
252b5132
RH
8698 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8699 {
ca4e0257 8700 if (HAVE_64BIT_GPRS)
67c0d1eb 8701 load_register (treg, &imm_expr, 1);
252b5132
RH
8702 else
8703 {
8704 int hreg, lreg;
8705
8706 if (target_big_endian)
8707 {
8708 hreg = treg;
8709 lreg = treg + 1;
8710 }
8711 else
8712 {
8713 hreg = treg + 1;
8714 lreg = treg;
8715 }
8716
8717 if (hreg <= 31)
67c0d1eb 8718 load_register (hreg, &imm_expr, 0);
252b5132
RH
8719 if (lreg <= 31)
8720 {
8721 if (offset_expr.X_op == O_absent)
67c0d1eb 8722 move_register (lreg, 0);
252b5132
RH
8723 else
8724 {
9c2799c2 8725 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 8726 load_register (lreg, &offset_expr, 0);
252b5132
RH
8727 }
8728 }
8729 }
8fc2e39e 8730 break;
252b5132
RH
8731 }
8732
8733 /* We know that sym is in the .rdata section. First we get the
8734 upper 16 bits of the address. */
8735 if (mips_pic == NO_PIC)
8736 {
67c0d1eb 8737 macro_build_lui (&offset_expr, AT);
8fc2e39e 8738 used_at = 1;
252b5132 8739 }
0a44bf69 8740 else
252b5132 8741 {
67c0d1eb
RS
8742 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8743 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 8744 used_at = 1;
252b5132 8745 }
bdaaa2e1 8746
252b5132 8747 /* Now we load the register(s). */
ca4e0257 8748 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
8749 {
8750 used_at = 1;
8751 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8752 }
252b5132
RH
8753 else
8754 {
8fc2e39e 8755 used_at = 1;
67c0d1eb 8756 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 8757 if (treg != RA)
252b5132
RH
8758 {
8759 /* FIXME: How in the world do we deal with the possible
8760 overflow here? */
8761 offset_expr.X_add_number += 4;
67c0d1eb 8762 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 8763 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
8764 }
8765 }
252b5132
RH
8766 break;
8767
8768 case M_LI_DD:
ca4e0257
RS
8769 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
8770 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
8771 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
8772 the value and the low order 32 bits are either zero or in
8773 OFFSET_EXPR. */
252b5132
RH
8774 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8775 {
8fc2e39e 8776 used_at = 1;
67c0d1eb 8777 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
8778 if (HAVE_64BIT_FPRS)
8779 {
9c2799c2 8780 gas_assert (HAVE_64BIT_GPRS);
67c0d1eb 8781 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 8782 }
252b5132
RH
8783 else
8784 {
67c0d1eb 8785 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 8786 if (offset_expr.X_op == O_absent)
67c0d1eb 8787 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
8788 else
8789 {
9c2799c2 8790 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
8791 load_register (AT, &offset_expr, 0);
8792 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
8793 }
8794 }
8795 break;
8796 }
8797
9c2799c2 8798 gas_assert (offset_expr.X_op == O_symbol
90ecf173 8799 && offset_expr.X_add_number == 0);
252b5132
RH
8800 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
8801 if (strcmp (s, ".lit8") == 0)
8802 {
df58fc94 8803 if (mips_opts.isa != ISA_MIPS1 || mips_opts.micromips)
252b5132 8804 {
67c0d1eb 8805 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 8806 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 8807 break;
252b5132 8808 }
c9914766 8809 breg = mips_gp_register;
252b5132
RH
8810 r = BFD_RELOC_MIPS_LITERAL;
8811 goto dob;
8812 }
8813 else
8814 {
9c2799c2 8815 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 8816 used_at = 1;
0a44bf69 8817 if (mips_pic != NO_PIC)
67c0d1eb
RS
8818 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8819 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
8820 else
8821 {
8822 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 8823 macro_build_lui (&offset_expr, AT);
252b5132 8824 }
bdaaa2e1 8825
df58fc94 8826 if (mips_opts.isa != ISA_MIPS1 || mips_opts.micromips)
252b5132 8827 {
67c0d1eb
RS
8828 macro_build (&offset_expr, "ldc1", "T,o(b)",
8829 treg, BFD_RELOC_LO16, AT);
252b5132
RH
8830 break;
8831 }
8832 breg = AT;
8833 r = BFD_RELOC_LO16;
8834 goto dob;
8835 }
8836
8837 case M_L_DOB:
252b5132
RH
8838 /* Even on a big endian machine $fn comes before $fn+1. We have
8839 to adjust when loading from memory. */
8840 r = BFD_RELOC_LO16;
8841 dob:
df58fc94 8842 gas_assert (!mips_opts.micromips);
9c2799c2 8843 gas_assert (mips_opts.isa == ISA_MIPS1);
67c0d1eb 8844 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 8845 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
8846 /* FIXME: A possible overflow which I don't know how to deal
8847 with. */
8848 offset_expr.X_add_number += 4;
67c0d1eb 8849 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 8850 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
8851 break;
8852
c4a68bea 8853 case M_S_DOB:
df58fc94 8854 gas_assert (!mips_opts.micromips);
c4a68bea
MR
8855 gas_assert (mips_opts.isa == ISA_MIPS1);
8856 /* Even on a big endian machine $fn comes before $fn+1. We have
8857 to adjust when storing to memory. */
8858 macro_build (&offset_expr, "swc1", "T,o(b)",
8859 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
8860 offset_expr.X_add_number += 4;
8861 macro_build (&offset_expr, "swc1", "T,o(b)",
8862 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8863 break;
8864
252b5132 8865 case M_L_DAB:
df58fc94 8866 gas_assert (!mips_opts.micromips);
252b5132
RH
8867 /*
8868 * The MIPS assembler seems to check for X_add_number not
8869 * being double aligned and generating:
8870 * lui at,%hi(foo+1)
8871 * addu at,at,v1
8872 * addiu at,at,%lo(foo+1)
8873 * lwc1 f2,0(at)
8874 * lwc1 f3,4(at)
8875 * But, the resulting address is the same after relocation so why
8876 * generate the extra instruction?
8877 */
bdaaa2e1 8878 /* Itbl support may require additional care here. */
252b5132 8879 coproc = 1;
df58fc94 8880 fmt = "T,o(b)";
e7af610e 8881 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
8882 {
8883 s = "ldc1";
df58fc94 8884 goto ld_st;
252b5132 8885 }
252b5132 8886 s = "lwc1";
252b5132
RH
8887 goto ldd_std;
8888
8889 case M_S_DAB:
df58fc94
RS
8890 gas_assert (!mips_opts.micromips);
8891 /* Itbl support may require additional care here. */
8892 coproc = 1;
8893 fmt = "T,o(b)";
e7af610e 8894 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
8895 {
8896 s = "sdc1";
df58fc94 8897 goto ld_st;
252b5132 8898 }
252b5132 8899 s = "swc1";
252b5132
RH
8900 goto ldd_std;
8901
8902 case M_LD_AB:
df58fc94 8903 fmt = "t,o(b)";
ca4e0257 8904 if (HAVE_64BIT_GPRS)
252b5132
RH
8905 {
8906 s = "ld";
8907 goto ld;
8908 }
252b5132 8909 s = "lw";
252b5132
RH
8910 goto ldd_std;
8911
8912 case M_SD_AB:
df58fc94 8913 fmt = "t,o(b)";
ca4e0257 8914 if (HAVE_64BIT_GPRS)
252b5132
RH
8915 {
8916 s = "sd";
df58fc94 8917 goto ld_st;
252b5132 8918 }
252b5132 8919 s = "sw";
252b5132
RH
8920
8921 ldd_std:
8922 if (offset_expr.X_op != O_symbol
8923 && offset_expr.X_op != O_constant)
8924 {
f71d0d44 8925 as_bad (_("Expression too complex"));
252b5132
RH
8926 offset_expr.X_op = O_constant;
8927 }
8928
2051e8c4
MR
8929 if (HAVE_32BIT_ADDRESSES
8930 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
8931 {
8932 char value [32];
8933
8934 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 8935 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 8936 }
2051e8c4 8937
252b5132
RH
8938 /* Even on a big endian machine $fn comes before $fn+1. We have
8939 to adjust when loading from memory. We set coproc if we must
8940 load $fn+1 first. */
bdaaa2e1 8941 /* Itbl support may require additional care here. */
90ecf173 8942 if (!target_big_endian)
252b5132
RH
8943 coproc = 0;
8944
90ecf173 8945 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
8946 {
8947 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
8948 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
8949 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
8950 If we have a base register, we use this
8951 addu $at,$breg,$gp
cdf6fd85
TS
8952 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
8953 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
8954 If this is not a GP relative symbol, we want
8955 lui $at,<sym> (BFD_RELOC_HI16_S)
8956 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
8957 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
8958 If there is a base register, we add it to $at after the
8959 lui instruction. If there is a constant, we always use
8960 the last case. */
39a59cf8
MR
8961 if (offset_expr.X_op == O_symbol
8962 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8963 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 8964 {
4d7206a2 8965 relax_start (offset_expr.X_add_symbol);
252b5132
RH
8966 if (breg == 0)
8967 {
c9914766 8968 tempreg = mips_gp_register;
252b5132
RH
8969 }
8970 else
8971 {
67c0d1eb 8972 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8973 AT, breg, mips_gp_register);
252b5132 8974 tempreg = AT;
252b5132
RH
8975 used_at = 1;
8976 }
8977
beae10d5 8978 /* Itbl support may require additional care here. */
67c0d1eb 8979 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 8980 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
8981 offset_expr.X_add_number += 4;
8982
8983 /* Set mips_optimize to 2 to avoid inserting an
8984 undesired nop. */
8985 hold_mips_optimize = mips_optimize;
8986 mips_optimize = 2;
beae10d5 8987 /* Itbl support may require additional care here. */
67c0d1eb 8988 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 8989 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
8990 mips_optimize = hold_mips_optimize;
8991
4d7206a2 8992 relax_switch ();
252b5132 8993
0970e49e 8994 offset_expr.X_add_number -= 4;
252b5132 8995 }
8fc2e39e 8996 used_at = 1;
67c0d1eb 8997 macro_build_lui (&offset_expr, AT);
252b5132 8998 if (breg != 0)
67c0d1eb 8999 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9000 /* Itbl support may require additional care here. */
67c0d1eb 9001 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9002 BFD_RELOC_LO16, AT);
252b5132
RH
9003 /* FIXME: How do we handle overflow here? */
9004 offset_expr.X_add_number += 4;
beae10d5 9005 /* Itbl support may require additional care here. */
67c0d1eb 9006 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9007 BFD_RELOC_LO16, AT);
4d7206a2
RS
9008 if (mips_relax.sequence)
9009 relax_end ();
bdaaa2e1 9010 }
0a44bf69 9011 else if (!mips_big_got)
252b5132 9012 {
252b5132
RH
9013 /* If this is a reference to an external symbol, we want
9014 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9015 nop
9016 <op> $treg,0($at)
9017 <op> $treg+1,4($at)
9018 Otherwise we want
9019 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9020 nop
9021 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9022 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9023 If there is a base register we add it to $at before the
9024 lwc1 instructions. If there is a constant we include it
9025 in the lwc1 instructions. */
9026 used_at = 1;
9027 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
9028 if (expr1.X_add_number < -0x8000
9029 || expr1.X_add_number >= 0x8000 - 4)
9030 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9031 load_got_offset (AT, &offset_expr);
269137b2 9032 load_delay_nop ();
252b5132 9033 if (breg != 0)
67c0d1eb 9034 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
9035
9036 /* Set mips_optimize to 2 to avoid inserting an undesired
9037 nop. */
9038 hold_mips_optimize = mips_optimize;
9039 mips_optimize = 2;
4d7206a2 9040
beae10d5 9041 /* Itbl support may require additional care here. */
4d7206a2 9042 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9043 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9044 BFD_RELOC_LO16, AT);
4d7206a2 9045 expr1.X_add_number += 4;
67c0d1eb
RS
9046 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9047 BFD_RELOC_LO16, AT);
4d7206a2 9048 relax_switch ();
67c0d1eb
RS
9049 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9050 BFD_RELOC_LO16, AT);
4d7206a2 9051 offset_expr.X_add_number += 4;
67c0d1eb
RS
9052 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9053 BFD_RELOC_LO16, AT);
4d7206a2 9054 relax_end ();
252b5132 9055
4d7206a2 9056 mips_optimize = hold_mips_optimize;
252b5132 9057 }
0a44bf69 9058 else if (mips_big_got)
252b5132 9059 {
67c0d1eb 9060 int gpdelay;
252b5132
RH
9061
9062 /* If this is a reference to an external symbol, we want
9063 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9064 addu $at,$at,$gp
9065 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
9066 nop
9067 <op> $treg,0($at)
9068 <op> $treg+1,4($at)
9069 Otherwise we want
9070 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9071 nop
9072 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9073 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9074 If there is a base register we add it to $at before the
9075 lwc1 instructions. If there is a constant we include it
9076 in the lwc1 instructions. */
9077 used_at = 1;
9078 expr1.X_add_number = offset_expr.X_add_number;
9079 offset_expr.X_add_number = 0;
9080 if (expr1.X_add_number < -0x8000
9081 || expr1.X_add_number >= 0x8000 - 4)
9082 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9083 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 9084 relax_start (offset_expr.X_add_symbol);
df58fc94 9085 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
9086 AT, BFD_RELOC_MIPS_GOT_HI16);
9087 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9088 AT, AT, mips_gp_register);
67c0d1eb 9089 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 9090 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 9091 load_delay_nop ();
252b5132 9092 if (breg != 0)
67c0d1eb 9093 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9094 /* Itbl support may require additional care here. */
67c0d1eb 9095 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9096 BFD_RELOC_LO16, AT);
252b5132
RH
9097 expr1.X_add_number += 4;
9098
9099 /* Set mips_optimize to 2 to avoid inserting an undesired
9100 nop. */
9101 hold_mips_optimize = mips_optimize;
9102 mips_optimize = 2;
beae10d5 9103 /* Itbl support may require additional care here. */
67c0d1eb 9104 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 9105 BFD_RELOC_LO16, AT);
252b5132
RH
9106 mips_optimize = hold_mips_optimize;
9107 expr1.X_add_number -= 4;
9108
4d7206a2
RS
9109 relax_switch ();
9110 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
9111 if (gpdelay)
9112 macro_build (NULL, "nop", "");
9113 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9114 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9115 load_delay_nop ();
252b5132 9116 if (breg != 0)
67c0d1eb 9117 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9118 /* Itbl support may require additional care here. */
67c0d1eb
RS
9119 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9120 BFD_RELOC_LO16, AT);
4d7206a2 9121 offset_expr.X_add_number += 4;
252b5132
RH
9122
9123 /* Set mips_optimize to 2 to avoid inserting an undesired
9124 nop. */
9125 hold_mips_optimize = mips_optimize;
9126 mips_optimize = 2;
beae10d5 9127 /* Itbl support may require additional care here. */
67c0d1eb
RS
9128 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9129 BFD_RELOC_LO16, AT);
252b5132 9130 mips_optimize = hold_mips_optimize;
4d7206a2 9131 relax_end ();
252b5132 9132 }
252b5132
RH
9133 else
9134 abort ();
9135
252b5132
RH
9136 break;
9137
9138 case M_LD_OB:
704897fb 9139 s = HAVE_64BIT_GPRS ? "ld" : "lw";
252b5132
RH
9140 goto sd_ob;
9141 case M_SD_OB:
704897fb 9142 s = HAVE_64BIT_GPRS ? "sd" : "sw";
252b5132 9143 sd_ob:
4614d845
MR
9144 macro_build (&offset_expr, s, "t,o(b)", treg,
9145 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9146 breg);
704897fb
MR
9147 if (!HAVE_64BIT_GPRS)
9148 {
9149 offset_expr.X_add_number += 4;
9150 macro_build (&offset_expr, s, "t,o(b)", treg + 1,
4614d845
MR
9151 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9152 breg);
704897fb 9153 }
8fc2e39e 9154 break;
252b5132 9155
dd6a37e7
AP
9156
9157 case M_SAA_AB:
9158 ab = 1;
9159 case M_SAA_OB:
9160 s = "saa";
9161 off0 = 1;
9162 fmt = "t,(b)";
9163 goto ld_st;
9164 case M_SAAD_AB:
9165 ab = 1;
9166 case M_SAAD_OB:
9167 s = "saad";
9168 off0 = 1;
9169 fmt = "t,(b)";
9170 goto ld_st;
9171
252b5132
RH
9172 /* New code added to support COPZ instructions.
9173 This code builds table entries out of the macros in mip_opcodes.
9174 R4000 uses interlocks to handle coproc delays.
9175 Other chips (like the R3000) require nops to be inserted for delays.
9176
f72c8c98 9177 FIXME: Currently, we require that the user handle delays.
252b5132
RH
9178 In order to fill delay slots for non-interlocked chips,
9179 we must have a way to specify delays based on the coprocessor.
9180 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9181 What are the side-effects of the cop instruction?
9182 What cache support might we have and what are its effects?
9183 Both coprocessor & memory require delays. how long???
bdaaa2e1 9184 What registers are read/set/modified?
252b5132
RH
9185
9186 If an itbl is provided to interpret cop instructions,
bdaaa2e1 9187 this knowledge can be encoded in the itbl spec. */
252b5132
RH
9188
9189 case M_COP0:
9190 s = "c0";
9191 goto copz;
9192 case M_COP1:
9193 s = "c1";
9194 goto copz;
9195 case M_COP2:
9196 s = "c2";
9197 goto copz;
9198 case M_COP3:
9199 s = "c3";
9200 copz:
df58fc94 9201 gas_assert (!mips_opts.micromips);
b19e8a9b
AN
9202 if (NO_ISA_COP (mips_opts.arch)
9203 && (ip->insn_mo->pinfo2 & INSN2_M_FP_S) == 0)
9204 {
7bd942df 9205 as_bad (_("Opcode not supported on this processor: %s"),
b19e8a9b
AN
9206 mips_cpu_info_from_arch (mips_opts.arch)->name);
9207 break;
9208 }
9209
252b5132
RH
9210 /* For now we just do C (same as Cz). The parameter will be
9211 stored in insn_opcode by mips_ip. */
67c0d1eb 9212 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 9213 break;
252b5132 9214
ea1fb5dc 9215 case M_MOVE:
67c0d1eb 9216 move_register (dreg, sreg);
8fc2e39e 9217 break;
ea1fb5dc 9218
252b5132
RH
9219 case M_DMUL:
9220 dbl = 1;
9221 case M_MUL:
67c0d1eb 9222 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
df58fc94 9223 macro_build (NULL, "mflo", MFHL_FMT, dreg);
8fc2e39e 9224 break;
252b5132
RH
9225
9226 case M_DMUL_I:
9227 dbl = 1;
9228 case M_MUL_I:
9229 /* The MIPS assembler some times generates shifts and adds. I'm
9230 not trying to be that fancy. GCC should do this for us
9231 anyway. */
8fc2e39e 9232 used_at = 1;
67c0d1eb
RS
9233 load_register (AT, &imm_expr, dbl);
9234 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
df58fc94 9235 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9236 break;
9237
9238 case M_DMULO_I:
9239 dbl = 1;
9240 case M_MULO_I:
9241 imm = 1;
9242 goto do_mulo;
9243
9244 case M_DMULO:
9245 dbl = 1;
9246 case M_MULO:
9247 do_mulo:
7d10b47d 9248 start_noreorder ();
8fc2e39e 9249 used_at = 1;
252b5132 9250 if (imm)
67c0d1eb
RS
9251 load_register (AT, &imm_expr, dbl);
9252 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
df58fc94
RS
9253 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9254 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9255 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 9256 if (mips_trap)
df58fc94 9257 macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
252b5132
RH
9258 else
9259 {
df58fc94
RS
9260 if (mips_opts.micromips)
9261 micromips_label_expr (&label_expr);
9262 else
9263 label_expr.X_add_number = 8;
9264 macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
a605d2b3 9265 macro_build (NULL, "nop", "");
df58fc94
RS
9266 macro_build (NULL, "break", BRK_FMT, 6);
9267 if (mips_opts.micromips)
9268 micromips_add_label ();
252b5132 9269 }
7d10b47d 9270 end_noreorder ();
df58fc94 9271 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9272 break;
9273
9274 case M_DMULOU_I:
9275 dbl = 1;
9276 case M_MULOU_I:
9277 imm = 1;
9278 goto do_mulou;
9279
9280 case M_DMULOU:
9281 dbl = 1;
9282 case M_MULOU:
9283 do_mulou:
7d10b47d 9284 start_noreorder ();
8fc2e39e 9285 used_at = 1;
252b5132 9286 if (imm)
67c0d1eb
RS
9287 load_register (AT, &imm_expr, dbl);
9288 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 9289 sreg, imm ? AT : treg);
df58fc94
RS
9290 macro_build (NULL, "mfhi", MFHL_FMT, AT);
9291 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132 9292 if (mips_trap)
df58fc94 9293 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
9294 else
9295 {
df58fc94
RS
9296 if (mips_opts.micromips)
9297 micromips_label_expr (&label_expr);
9298 else
9299 label_expr.X_add_number = 8;
9300 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 9301 macro_build (NULL, "nop", "");
df58fc94
RS
9302 macro_build (NULL, "break", BRK_FMT, 6);
9303 if (mips_opts.micromips)
9304 micromips_add_label ();
252b5132 9305 }
7d10b47d 9306 end_noreorder ();
252b5132
RH
9307 break;
9308
771c7ce4 9309 case M_DROL:
fef14a42 9310 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
9311 {
9312 if (dreg == sreg)
9313 {
9314 tempreg = AT;
9315 used_at = 1;
9316 }
9317 else
9318 {
9319 tempreg = dreg;
82dd0097 9320 }
67c0d1eb
RS
9321 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9322 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9323 break;
82dd0097 9324 }
8fc2e39e 9325 used_at = 1;
c80c840e 9326 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9327 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9328 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9329 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9330 break;
9331
252b5132 9332 case M_ROL:
fef14a42 9333 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
9334 {
9335 if (dreg == sreg)
9336 {
9337 tempreg = AT;
9338 used_at = 1;
9339 }
9340 else
9341 {
9342 tempreg = dreg;
82dd0097 9343 }
67c0d1eb
RS
9344 macro_build (NULL, "negu", "d,w", tempreg, treg);
9345 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9346 break;
82dd0097 9347 }
8fc2e39e 9348 used_at = 1;
c80c840e 9349 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9350 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9351 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9352 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9353 break;
9354
771c7ce4
TS
9355 case M_DROL_I:
9356 {
9357 unsigned int rot;
91d6fa6a
NC
9358 char *l;
9359 char *rr;
771c7ce4
TS
9360
9361 if (imm_expr.X_op != O_constant)
82dd0097 9362 as_bad (_("Improper rotate count"));
771c7ce4 9363 rot = imm_expr.X_add_number & 0x3f;
fef14a42 9364 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
9365 {
9366 rot = (64 - rot) & 0x3f;
9367 if (rot >= 32)
df58fc94 9368 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
60b63b72 9369 else
df58fc94 9370 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9371 break;
60b63b72 9372 }
483fc7cd 9373 if (rot == 0)
483fc7cd 9374 {
df58fc94 9375 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9376 break;
483fc7cd 9377 }
82dd0097 9378 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 9379 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 9380 rot &= 0x1f;
8fc2e39e 9381 used_at = 1;
df58fc94
RS
9382 macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
9383 macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9384 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9385 }
9386 break;
9387
252b5132 9388 case M_ROL_I:
771c7ce4
TS
9389 {
9390 unsigned int rot;
9391
9392 if (imm_expr.X_op != O_constant)
82dd0097 9393 as_bad (_("Improper rotate count"));
771c7ce4 9394 rot = imm_expr.X_add_number & 0x1f;
fef14a42 9395 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 9396 {
df58fc94 9397 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 9398 break;
60b63b72 9399 }
483fc7cd 9400 if (rot == 0)
483fc7cd 9401 {
df58fc94 9402 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9403 break;
483fc7cd 9404 }
8fc2e39e 9405 used_at = 1;
df58fc94
RS
9406 macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
9407 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9408 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9409 }
9410 break;
9411
9412 case M_DROR:
fef14a42 9413 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 9414 {
67c0d1eb 9415 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 9416 break;
82dd0097 9417 }
8fc2e39e 9418 used_at = 1;
c80c840e 9419 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9420 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
9421 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
9422 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9423 break;
9424
9425 case M_ROR:
fef14a42 9426 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 9427 {
67c0d1eb 9428 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 9429 break;
82dd0097 9430 }
8fc2e39e 9431 used_at = 1;
c80c840e 9432 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9433 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
9434 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
9435 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9436 break;
9437
771c7ce4
TS
9438 case M_DROR_I:
9439 {
9440 unsigned int rot;
91d6fa6a
NC
9441 char *l;
9442 char *rr;
771c7ce4
TS
9443
9444 if (imm_expr.X_op != O_constant)
82dd0097 9445 as_bad (_("Improper rotate count"));
771c7ce4 9446 rot = imm_expr.X_add_number & 0x3f;
fef14a42 9447 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
9448 {
9449 if (rot >= 32)
df58fc94 9450 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
82dd0097 9451 else
df58fc94 9452 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9453 break;
82dd0097 9454 }
483fc7cd 9455 if (rot == 0)
483fc7cd 9456 {
df58fc94 9457 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9458 break;
483fc7cd 9459 }
91d6fa6a 9460 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
9461 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
9462 rot &= 0x1f;
8fc2e39e 9463 used_at = 1;
df58fc94
RS
9464 macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
9465 macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9466 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9467 }
9468 break;
9469
252b5132 9470 case M_ROR_I:
771c7ce4
TS
9471 {
9472 unsigned int rot;
9473
9474 if (imm_expr.X_op != O_constant)
82dd0097 9475 as_bad (_("Improper rotate count"));
771c7ce4 9476 rot = imm_expr.X_add_number & 0x1f;
fef14a42 9477 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 9478 {
df58fc94 9479 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9480 break;
82dd0097 9481 }
483fc7cd 9482 if (rot == 0)
483fc7cd 9483 {
df58fc94 9484 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9485 break;
483fc7cd 9486 }
8fc2e39e 9487 used_at = 1;
df58fc94
RS
9488 macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
9489 macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9490 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 9491 }
252b5132
RH
9492 break;
9493
252b5132
RH
9494 case M_SEQ:
9495 if (sreg == 0)
67c0d1eb 9496 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 9497 else if (treg == 0)
67c0d1eb 9498 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9499 else
9500 {
67c0d1eb
RS
9501 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9502 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 9503 }
8fc2e39e 9504 break;
252b5132
RH
9505
9506 case M_SEQ_I:
9507 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9508 {
67c0d1eb 9509 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9510 break;
252b5132
RH
9511 }
9512 if (sreg == 0)
9513 {
9514 as_warn (_("Instruction %s: result is always false"),
9515 ip->insn_mo->name);
67c0d1eb 9516 move_register (dreg, 0);
8fc2e39e 9517 break;
252b5132 9518 }
dd3cbb7e
NC
9519 if (CPU_HAS_SEQ (mips_opts.arch)
9520 && -512 <= imm_expr.X_add_number
9521 && imm_expr.X_add_number < 512)
9522 {
9523 macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
750bdd57 9524 (int) imm_expr.X_add_number);
dd3cbb7e
NC
9525 break;
9526 }
252b5132
RH
9527 if (imm_expr.X_op == O_constant
9528 && imm_expr.X_add_number >= 0
9529 && imm_expr.X_add_number < 0x10000)
9530 {
67c0d1eb 9531 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9532 }
9533 else if (imm_expr.X_op == O_constant
9534 && imm_expr.X_add_number > -0x8000
9535 && imm_expr.X_add_number < 0)
9536 {
9537 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 9538 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 9539 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 9540 }
dd3cbb7e
NC
9541 else if (CPU_HAS_SEQ (mips_opts.arch))
9542 {
9543 used_at = 1;
9544 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9545 macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
9546 break;
9547 }
252b5132
RH
9548 else
9549 {
67c0d1eb
RS
9550 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9551 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
9552 used_at = 1;
9553 }
67c0d1eb 9554 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9555 break;
252b5132
RH
9556
9557 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
9558 s = "slt";
9559 goto sge;
9560 case M_SGEU:
9561 s = "sltu";
9562 sge:
67c0d1eb
RS
9563 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
9564 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9565 break;
252b5132
RH
9566
9567 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
9568 case M_SGEU_I:
9569 if (imm_expr.X_op == O_constant
9570 && imm_expr.X_add_number >= -0x8000
9571 && imm_expr.X_add_number < 0x8000)
9572 {
67c0d1eb
RS
9573 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
9574 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9575 }
9576 else
9577 {
67c0d1eb
RS
9578 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9579 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
9580 dreg, sreg, AT);
252b5132
RH
9581 used_at = 1;
9582 }
67c0d1eb 9583 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9584 break;
252b5132
RH
9585
9586 case M_SGT: /* sreg > treg <==> treg < sreg */
9587 s = "slt";
9588 goto sgt;
9589 case M_SGTU:
9590 s = "sltu";
9591 sgt:
67c0d1eb 9592 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 9593 break;
252b5132
RH
9594
9595 case M_SGT_I: /* sreg > I <==> I < sreg */
9596 s = "slt";
9597 goto sgti;
9598 case M_SGTU_I:
9599 s = "sltu";
9600 sgti:
8fc2e39e 9601 used_at = 1;
67c0d1eb
RS
9602 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9603 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
9604 break;
9605
2396cfb9 9606 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
9607 s = "slt";
9608 goto sle;
9609 case M_SLEU:
9610 s = "sltu";
9611 sle:
67c0d1eb
RS
9612 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9613 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9614 break;
252b5132 9615
2396cfb9 9616 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
9617 s = "slt";
9618 goto slei;
9619 case M_SLEU_I:
9620 s = "sltu";
9621 slei:
8fc2e39e 9622 used_at = 1;
67c0d1eb
RS
9623 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9624 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9625 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
9626 break;
9627
9628 case M_SLT_I:
9629 if (imm_expr.X_op == O_constant
9630 && imm_expr.X_add_number >= -0x8000
9631 && imm_expr.X_add_number < 0x8000)
9632 {
67c0d1eb 9633 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9634 break;
252b5132 9635 }
8fc2e39e 9636 used_at = 1;
67c0d1eb
RS
9637 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9638 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
9639 break;
9640
9641 case M_SLTU_I:
9642 if (imm_expr.X_op == O_constant
9643 && imm_expr.X_add_number >= -0x8000
9644 && imm_expr.X_add_number < 0x8000)
9645 {
67c0d1eb 9646 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 9647 BFD_RELOC_LO16);
8fc2e39e 9648 break;
252b5132 9649 }
8fc2e39e 9650 used_at = 1;
67c0d1eb
RS
9651 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9652 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
9653 break;
9654
9655 case M_SNE:
9656 if (sreg == 0)
67c0d1eb 9657 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 9658 else if (treg == 0)
67c0d1eb 9659 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
9660 else
9661 {
67c0d1eb
RS
9662 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9663 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 9664 }
8fc2e39e 9665 break;
252b5132
RH
9666
9667 case M_SNE_I:
9668 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9669 {
67c0d1eb 9670 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 9671 break;
252b5132
RH
9672 }
9673 if (sreg == 0)
9674 {
9675 as_warn (_("Instruction %s: result is always true"),
9676 ip->insn_mo->name);
67c0d1eb
RS
9677 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
9678 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 9679 break;
252b5132 9680 }
dd3cbb7e
NC
9681 if (CPU_HAS_SEQ (mips_opts.arch)
9682 && -512 <= imm_expr.X_add_number
9683 && imm_expr.X_add_number < 512)
9684 {
9685 macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
750bdd57 9686 (int) imm_expr.X_add_number);
dd3cbb7e
NC
9687 break;
9688 }
252b5132
RH
9689 if (imm_expr.X_op == O_constant
9690 && imm_expr.X_add_number >= 0
9691 && imm_expr.X_add_number < 0x10000)
9692 {
67c0d1eb 9693 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9694 }
9695 else if (imm_expr.X_op == O_constant
9696 && imm_expr.X_add_number > -0x8000
9697 && imm_expr.X_add_number < 0)
9698 {
9699 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 9700 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 9701 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 9702 }
dd3cbb7e
NC
9703 else if (CPU_HAS_SEQ (mips_opts.arch))
9704 {
9705 used_at = 1;
9706 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9707 macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
9708 break;
9709 }
252b5132
RH
9710 else
9711 {
67c0d1eb
RS
9712 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9713 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
9714 used_at = 1;
9715 }
67c0d1eb 9716 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 9717 break;
252b5132 9718
df58fc94
RS
9719 case M_SUB_I:
9720 s = "addi";
9721 s2 = "sub";
9722 goto do_subi;
9723 case M_SUBU_I:
9724 s = "addiu";
9725 s2 = "subu";
9726 goto do_subi;
252b5132
RH
9727 case M_DSUB_I:
9728 dbl = 1;
df58fc94
RS
9729 s = "daddi";
9730 s2 = "dsub";
9731 if (!mips_opts.micromips)
9732 goto do_subi;
252b5132 9733 if (imm_expr.X_op == O_constant
df58fc94
RS
9734 && imm_expr.X_add_number > -0x200
9735 && imm_expr.X_add_number <= 0x200)
252b5132 9736 {
df58fc94 9737 macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
8fc2e39e 9738 break;
252b5132 9739 }
df58fc94 9740 goto do_subi_i;
252b5132
RH
9741 case M_DSUBU_I:
9742 dbl = 1;
df58fc94
RS
9743 s = "daddiu";
9744 s2 = "dsubu";
9745 do_subi:
252b5132
RH
9746 if (imm_expr.X_op == O_constant
9747 && imm_expr.X_add_number > -0x8000
9748 && imm_expr.X_add_number <= 0x8000)
9749 {
9750 imm_expr.X_add_number = -imm_expr.X_add_number;
df58fc94 9751 macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9752 break;
252b5132 9753 }
df58fc94 9754 do_subi_i:
8fc2e39e 9755 used_at = 1;
67c0d1eb 9756 load_register (AT, &imm_expr, dbl);
df58fc94 9757 macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
252b5132
RH
9758 break;
9759
9760 case M_TEQ_I:
9761 s = "teq";
9762 goto trap;
9763 case M_TGE_I:
9764 s = "tge";
9765 goto trap;
9766 case M_TGEU_I:
9767 s = "tgeu";
9768 goto trap;
9769 case M_TLT_I:
9770 s = "tlt";
9771 goto trap;
9772 case M_TLTU_I:
9773 s = "tltu";
9774 goto trap;
9775 case M_TNE_I:
9776 s = "tne";
9777 trap:
8fc2e39e 9778 used_at = 1;
67c0d1eb
RS
9779 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9780 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
9781 break;
9782
252b5132 9783 case M_TRUNCWS:
43841e91 9784 case M_TRUNCWD:
df58fc94 9785 gas_assert (!mips_opts.micromips);
9c2799c2 9786 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 9787 used_at = 1;
252b5132
RH
9788 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
9789 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
9790
9791 /*
9792 * Is the double cfc1 instruction a bug in the mips assembler;
9793 * or is there a reason for it?
9794 */
7d10b47d 9795 start_noreorder ();
67c0d1eb
RS
9796 macro_build (NULL, "cfc1", "t,G", treg, RA);
9797 macro_build (NULL, "cfc1", "t,G", treg, RA);
9798 macro_build (NULL, "nop", "");
252b5132 9799 expr1.X_add_number = 3;
67c0d1eb 9800 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 9801 expr1.X_add_number = 2;
67c0d1eb
RS
9802 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
9803 macro_build (NULL, "ctc1", "t,G", AT, RA);
9804 macro_build (NULL, "nop", "");
9805 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
9806 dreg, sreg);
9807 macro_build (NULL, "ctc1", "t,G", treg, RA);
9808 macro_build (NULL, "nop", "");
7d10b47d 9809 end_noreorder ();
252b5132
RH
9810 break;
9811
df58fc94
RS
9812 case M_ULH_A:
9813 ab = 1;
252b5132
RH
9814 case M_ULH:
9815 s = "lb";
df58fc94
RS
9816 s2 = "lbu";
9817 off = 1;
9818 goto uld_st;
9819 case M_ULHU_A:
9820 ab = 1;
252b5132
RH
9821 case M_ULHU:
9822 s = "lbu";
df58fc94
RS
9823 s2 = "lbu";
9824 off = 1;
9825 goto uld_st;
9826 case M_ULW_A:
9827 ab = 1;
9828 case M_ULW:
9829 s = "lwl";
9830 s2 = "lwr";
9831 off12 = mips_opts.micromips;
9832 off = 3;
9833 goto uld_st;
9834 case M_ULD_A:
9835 ab = 1;
252b5132
RH
9836 case M_ULD:
9837 s = "ldl";
9838 s2 = "ldr";
df58fc94 9839 off12 = mips_opts.micromips;
252b5132 9840 off = 7;
df58fc94
RS
9841 goto uld_st;
9842 case M_USH_A:
9843 ab = 1;
9844 case M_USH:
9845 s = "sb";
9846 s2 = "sb";
9847 off = 1;
9848 ust = 1;
9849 goto uld_st;
9850 case M_USW_A:
9851 ab = 1;
9852 case M_USW:
9853 s = "swl";
9854 s2 = "swr";
9855 off12 = mips_opts.micromips;
252b5132 9856 off = 3;
df58fc94
RS
9857 ust = 1;
9858 goto uld_st;
9859 case M_USD_A:
9860 ab = 1;
9861 case M_USD:
9862 s = "sdl";
9863 s2 = "sdr";
9864 off12 = mips_opts.micromips;
9865 off = 7;
9866 ust = 1;
9867
9868 uld_st:
9869 if (!ab && offset_expr.X_add_number >= 0x8000 - off)
f71d0d44 9870 as_bad (_("Operand overflow"));
df58fc94
RS
9871
9872 ep = &offset_expr;
9873 expr1.X_add_number = 0;
9874 if (ab)
9875 {
9876 used_at = 1;
9877 tempreg = AT;
9878 load_address (tempreg, ep, &used_at);
9879 if (breg != 0)
9880 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9881 tempreg, tempreg, breg);
9882 breg = tempreg;
9883 tempreg = treg;
9884 ep = &expr1;
9885 }
9886 else if (off12
9887 && (offset_expr.X_op != O_constant
9888 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
9889 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
9890 {
9891 used_at = 1;
9892 tempreg = AT;
9893 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
9894 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
9895 breg = tempreg;
9896 tempreg = treg;
9897 ep = &expr1;
9898 }
9899 else if (!ust && treg == breg)
8fc2e39e
TS
9900 {
9901 used_at = 1;
9902 tempreg = AT;
9903 }
252b5132 9904 else
df58fc94 9905 tempreg = treg;
af22f5b2 9906
df58fc94
RS
9907 if (off == 1)
9908 goto ulh_sh;
252b5132 9909
90ecf173 9910 if (!target_big_endian)
df58fc94
RS
9911 ep->X_add_number += off;
9912 if (!off12)
9913 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 9914 else
df58fc94
RS
9915 macro_build (NULL, s, "t,~(b)",
9916 tempreg, (unsigned long) ep->X_add_number, breg);
9917
90ecf173 9918 if (!target_big_endian)
df58fc94 9919 ep->X_add_number -= off;
252b5132 9920 else
df58fc94
RS
9921 ep->X_add_number += off;
9922 if (!off12)
9923 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
9924 else
9925 macro_build (NULL, s2, "t,~(b)",
9926 tempreg, (unsigned long) ep->X_add_number, breg);
252b5132 9927
df58fc94
RS
9928 /* If necessary, move the result in tempreg to the final destination. */
9929 if (!ust && treg != tempreg)
9930 {
9931 /* Protect second load's delay slot. */
9932 load_delay_nop ();
9933 move_register (treg, tempreg);
9934 }
8fc2e39e 9935 break;
252b5132 9936
df58fc94 9937 ulh_sh:
d6bc6245 9938 used_at = 1;
df58fc94
RS
9939 if (target_big_endian == ust)
9940 ep->X_add_number += off;
9941 tempreg = ust || ab ? treg : AT;
9942 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
9943
9944 /* For halfword transfers we need a temporary register to shuffle
9945 bytes. Unfortunately for M_USH_A we have none available before
9946 the next store as AT holds the base address. We deal with this
9947 case by clobbering TREG and then restoring it as with ULH. */
9948 tempreg = ust == ab ? treg : AT;
9949 if (ust)
9950 macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
9951
9952 if (target_big_endian == ust)
9953 ep->X_add_number -= off;
252b5132 9954 else
df58fc94
RS
9955 ep->X_add_number += off;
9956 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 9957
df58fc94
RS
9958 /* For M_USH_A re-retrieve the LSB. */
9959 if (ust && ab)
9960 {
9961 if (target_big_endian)
9962 ep->X_add_number += off;
9963 else
9964 ep->X_add_number -= off;
9965 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
9966 }
9967 /* For ULH and M_USH_A OR the LSB in. */
9968 if (!ust || ab)
9969 {
9970 tempreg = !ab ? AT : treg;
9971 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
9972 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
9973 }
252b5132
RH
9974 break;
9975
9976 default:
9977 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 9978 are added dynamically. */
252b5132
RH
9979 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
9980 break;
9981 }
741fe287 9982 if (!mips_opts.at && used_at)
8fc2e39e 9983 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
9984}
9985
9986/* Implement macros in mips16 mode. */
9987
9988static void
17a2f251 9989mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
9990{
9991 int mask;
9992 int xreg, yreg, zreg, tmp;
252b5132
RH
9993 expressionS expr1;
9994 int dbl;
9995 const char *s, *s2, *s3;
9996
9997 mask = ip->insn_mo->mask;
9998
bf12938e
RS
9999 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10000 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10001 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 10002
252b5132
RH
10003 expr1.X_op = O_constant;
10004 expr1.X_op_symbol = NULL;
10005 expr1.X_add_symbol = NULL;
10006 expr1.X_add_number = 1;
10007
10008 dbl = 0;
10009
10010 switch (mask)
10011 {
10012 default:
10013 internalError ();
10014
10015 case M_DDIV_3:
10016 dbl = 1;
10017 case M_DIV_3:
10018 s = "mflo";
10019 goto do_div3;
10020 case M_DREM_3:
10021 dbl = 1;
10022 case M_REM_3:
10023 s = "mfhi";
10024 do_div3:
7d10b47d 10025 start_noreorder ();
67c0d1eb 10026 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 10027 expr1.X_add_number = 2;
67c0d1eb
RS
10028 macro_build (&expr1, "bnez", "x,p", yreg);
10029 macro_build (NULL, "break", "6", 7);
bdaaa2e1 10030
252b5132
RH
10031 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10032 since that causes an overflow. We should do that as well,
10033 but I don't see how to do the comparisons without a temporary
10034 register. */
7d10b47d 10035 end_noreorder ();
67c0d1eb 10036 macro_build (NULL, s, "x", zreg);
252b5132
RH
10037 break;
10038
10039 case M_DIVU_3:
10040 s = "divu";
10041 s2 = "mflo";
10042 goto do_divu3;
10043 case M_REMU_3:
10044 s = "divu";
10045 s2 = "mfhi";
10046 goto do_divu3;
10047 case M_DDIVU_3:
10048 s = "ddivu";
10049 s2 = "mflo";
10050 goto do_divu3;
10051 case M_DREMU_3:
10052 s = "ddivu";
10053 s2 = "mfhi";
10054 do_divu3:
7d10b47d 10055 start_noreorder ();
67c0d1eb 10056 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 10057 expr1.X_add_number = 2;
67c0d1eb
RS
10058 macro_build (&expr1, "bnez", "x,p", yreg);
10059 macro_build (NULL, "break", "6", 7);
7d10b47d 10060 end_noreorder ();
67c0d1eb 10061 macro_build (NULL, s2, "x", zreg);
252b5132
RH
10062 break;
10063
10064 case M_DMUL:
10065 dbl = 1;
10066 case M_MUL:
67c0d1eb
RS
10067 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10068 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 10069 break;
252b5132
RH
10070
10071 case M_DSUBU_I:
10072 dbl = 1;
10073 goto do_subu;
10074 case M_SUBU_I:
10075 do_subu:
10076 if (imm_expr.X_op != O_constant)
10077 as_bad (_("Unsupported large constant"));
10078 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10079 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
10080 break;
10081
10082 case M_SUBU_I_2:
10083 if (imm_expr.X_op != O_constant)
10084 as_bad (_("Unsupported large constant"));
10085 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10086 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
10087 break;
10088
10089 case M_DSUBU_I_2:
10090 if (imm_expr.X_op != O_constant)
10091 as_bad (_("Unsupported large constant"));
10092 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10093 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
10094 break;
10095
10096 case M_BEQ:
10097 s = "cmp";
10098 s2 = "bteqz";
10099 goto do_branch;
10100 case M_BNE:
10101 s = "cmp";
10102 s2 = "btnez";
10103 goto do_branch;
10104 case M_BLT:
10105 s = "slt";
10106 s2 = "btnez";
10107 goto do_branch;
10108 case M_BLTU:
10109 s = "sltu";
10110 s2 = "btnez";
10111 goto do_branch;
10112 case M_BLE:
10113 s = "slt";
10114 s2 = "bteqz";
10115 goto do_reverse_branch;
10116 case M_BLEU:
10117 s = "sltu";
10118 s2 = "bteqz";
10119 goto do_reverse_branch;
10120 case M_BGE:
10121 s = "slt";
10122 s2 = "bteqz";
10123 goto do_branch;
10124 case M_BGEU:
10125 s = "sltu";
10126 s2 = "bteqz";
10127 goto do_branch;
10128 case M_BGT:
10129 s = "slt";
10130 s2 = "btnez";
10131 goto do_reverse_branch;
10132 case M_BGTU:
10133 s = "sltu";
10134 s2 = "btnez";
10135
10136 do_reverse_branch:
10137 tmp = xreg;
10138 xreg = yreg;
10139 yreg = tmp;
10140
10141 do_branch:
67c0d1eb
RS
10142 macro_build (NULL, s, "x,y", xreg, yreg);
10143 macro_build (&offset_expr, s2, "p");
252b5132
RH
10144 break;
10145
10146 case M_BEQ_I:
10147 s = "cmpi";
10148 s2 = "bteqz";
10149 s3 = "x,U";
10150 goto do_branch_i;
10151 case M_BNE_I:
10152 s = "cmpi";
10153 s2 = "btnez";
10154 s3 = "x,U";
10155 goto do_branch_i;
10156 case M_BLT_I:
10157 s = "slti";
10158 s2 = "btnez";
10159 s3 = "x,8";
10160 goto do_branch_i;
10161 case M_BLTU_I:
10162 s = "sltiu";
10163 s2 = "btnez";
10164 s3 = "x,8";
10165 goto do_branch_i;
10166 case M_BLE_I:
10167 s = "slti";
10168 s2 = "btnez";
10169 s3 = "x,8";
10170 goto do_addone_branch_i;
10171 case M_BLEU_I:
10172 s = "sltiu";
10173 s2 = "btnez";
10174 s3 = "x,8";
10175 goto do_addone_branch_i;
10176 case M_BGE_I:
10177 s = "slti";
10178 s2 = "bteqz";
10179 s3 = "x,8";
10180 goto do_branch_i;
10181 case M_BGEU_I:
10182 s = "sltiu";
10183 s2 = "bteqz";
10184 s3 = "x,8";
10185 goto do_branch_i;
10186 case M_BGT_I:
10187 s = "slti";
10188 s2 = "bteqz";
10189 s3 = "x,8";
10190 goto do_addone_branch_i;
10191 case M_BGTU_I:
10192 s = "sltiu";
10193 s2 = "bteqz";
10194 s3 = "x,8";
10195
10196 do_addone_branch_i:
10197 if (imm_expr.X_op != O_constant)
10198 as_bad (_("Unsupported large constant"));
10199 ++imm_expr.X_add_number;
10200
10201 do_branch_i:
67c0d1eb
RS
10202 macro_build (&imm_expr, s, s3, xreg);
10203 macro_build (&offset_expr, s2, "p");
252b5132
RH
10204 break;
10205
10206 case M_ABS:
10207 expr1.X_add_number = 0;
67c0d1eb 10208 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 10209 if (xreg != yreg)
67c0d1eb 10210 move_register (xreg, yreg);
252b5132 10211 expr1.X_add_number = 2;
67c0d1eb
RS
10212 macro_build (&expr1, "bteqz", "p");
10213 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
10214 }
10215}
10216
10217/* For consistency checking, verify that all bits are specified either
10218 by the match/mask part of the instruction definition, or by the
10219 operand list. */
10220static int
17a2f251 10221validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
10222{
10223 const char *p = opc->args;
10224 char c;
10225 unsigned long used_bits = opc->mask;
10226
10227 if ((used_bits & opc->match) != opc->match)
10228 {
10229 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10230 opc->name, opc->args);
10231 return 0;
10232 }
10233#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
10234 while (*p)
10235 switch (c = *p++)
10236 {
10237 case ',': break;
10238 case '(': break;
10239 case ')': break;
af7ee8bf
CD
10240 case '+':
10241 switch (c = *p++)
10242 {
9bcd4f99
TS
10243 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
10244 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
10245 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
10246 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
af7ee8bf
CD
10247 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10248 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10249 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
10250 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
10251 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
10252 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10253 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10254 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10255 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10256 case 'I': break;
ef2e4d86
CF
10257 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10258 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
10259 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
bb35fb24
NC
10260 case 'x': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10261 case 'X': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10262 case 'p': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
10263 case 'P': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
dd3cbb7e 10264 case 'Q': USE_BITS (OP_MASK_SEQI, OP_SH_SEQI); break;
bb35fb24
NC
10265 case 's': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
10266 case 'S': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
98675402
RS
10267 case 'z': USE_BITS (OP_MASK_RZ, OP_SH_RZ); break;
10268 case 'Z': USE_BITS (OP_MASK_FZ, OP_SH_FZ); break;
10269 case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10270 case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10271 case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
bb35fb24 10272
af7ee8bf
CD
10273 default:
10274 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10275 c, opc->name, opc->args);
10276 return 0;
10277 }
10278 break;
252b5132
RH
10279 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10280 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10281 case 'A': break;
4372b673 10282 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
10283 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
10284 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10285 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10286 case 'F': break;
10287 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 10288 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 10289 case 'I': break;
e972090a 10290 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 10291 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10292 case 'L': break;
10293 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
10294 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
10295 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
10296 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
10297 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10298 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
10299 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10300 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10301 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10302 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
10303 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10304 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10305 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10306 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
10307 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10308 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
10309 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10310 case 'f': break;
10311 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
10312 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10313 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10314 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
10315 case 'l': break;
10316 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10317 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10318 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
10319 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10320 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10321 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10322 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10323 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10324 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10325 case 'x': break;
10326 case 'z': break;
10327 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
10328 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
10329 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
10330 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
10331 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
10332 case '[': break;
10333 case ']': break;
620edafd 10334 case '1': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8b082fb1 10335 case '2': USE_BITS (OP_MASK_BP, OP_SH_BP); break;
74cd071d
CF
10336 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
10337 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
10338 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
10339 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10340 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
10341 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
10342 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
10343 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
10344 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
10345 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
10346 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
10347 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
10348 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
10349 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
10350 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
dec0624d
MR
10351 case '\\': USE_BITS (OP_MASK_3BITPOS, OP_SH_3BITPOS); break;
10352 case '~': USE_BITS (OP_MASK_OFFSET12, OP_SH_OFFSET12); break;
ef2e4d86 10353 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10354 default:
10355 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10356 c, opc->name, opc->args);
10357 return 0;
10358 }
10359#undef USE_BITS
10360 if (used_bits != 0xffffffff)
10361 {
10362 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
10363 ~used_bits & 0xffffffff, opc->name, opc->args);
10364 return 0;
10365 }
10366 return 1;
10367}
10368
df58fc94
RS
10369/* For consistency checking, verify that the length implied matches the
10370 major opcode and that all bits are specified either by the match/mask
10371 part of the instruction definition, or by the operand list. */
10372
10373static int
10374validate_micromips_insn (const struct mips_opcode *opc)
10375{
10376 unsigned long match = opc->match;
10377 unsigned long mask = opc->mask;
10378 const char *p = opc->args;
10379 unsigned long insn_bits;
10380 unsigned long used_bits;
10381 unsigned long major;
10382 unsigned int length;
10383 char e;
10384 char c;
10385
10386 if ((mask & match) != match)
10387 {
10388 as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
10389 opc->name, opc->args);
10390 return 0;
10391 }
10392 length = micromips_insn_length (opc);
10393 if (length != 2 && length != 4)
10394 {
10395 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
10396 "%s %s"), length, opc->name, opc->args);
10397 return 0;
10398 }
10399 major = match >> (10 + 8 * (length - 2));
10400 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
10401 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
10402 {
10403 as_bad (_("Internal error: bad microMIPS opcode "
10404 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
10405 return 0;
10406 }
10407
10408 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
10409 insn_bits = 1 << 4 * length;
10410 insn_bits <<= 4 * length;
10411 insn_bits -= 1;
10412 used_bits = mask;
10413#define USE_BITS(field) \
10414 (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
10415 while (*p)
10416 switch (c = *p++)
10417 {
10418 case ',': break;
10419 case '(': break;
10420 case ')': break;
10421 case '+':
10422 e = c;
10423 switch (c = *p++)
10424 {
10425 case 'A': USE_BITS (EXTLSB); break;
10426 case 'B': USE_BITS (INSMSB); break;
10427 case 'C': USE_BITS (EXTMSBD); break;
10428 case 'D': USE_BITS (RS); USE_BITS (SEL); break;
10429 case 'E': USE_BITS (EXTLSB); break;
10430 case 'F': USE_BITS (INSMSB); break;
10431 case 'G': USE_BITS (EXTMSBD); break;
10432 case 'H': USE_BITS (EXTMSBD); break;
10433 default:
10434 as_bad (_("Internal error: bad mips opcode "
10435 "(unknown extension operand type `%c%c'): %s %s"),
10436 e, c, opc->name, opc->args);
10437 return 0;
10438 }
10439 break;
10440 case 'm':
10441 e = c;
10442 switch (c = *p++)
10443 {
10444 case 'A': USE_BITS (IMMA); break;
10445 case 'B': USE_BITS (IMMB); break;
10446 case 'C': USE_BITS (IMMC); break;
10447 case 'D': USE_BITS (IMMD); break;
10448 case 'E': USE_BITS (IMME); break;
10449 case 'F': USE_BITS (IMMF); break;
10450 case 'G': USE_BITS (IMMG); break;
10451 case 'H': USE_BITS (IMMH); break;
10452 case 'I': USE_BITS (IMMI); break;
10453 case 'J': USE_BITS (IMMJ); break;
10454 case 'L': USE_BITS (IMML); break;
10455 case 'M': USE_BITS (IMMM); break;
10456 case 'N': USE_BITS (IMMN); break;
10457 case 'O': USE_BITS (IMMO); break;
10458 case 'P': USE_BITS (IMMP); break;
10459 case 'Q': USE_BITS (IMMQ); break;
10460 case 'U': USE_BITS (IMMU); break;
10461 case 'W': USE_BITS (IMMW); break;
10462 case 'X': USE_BITS (IMMX); break;
10463 case 'Y': USE_BITS (IMMY); break;
10464 case 'Z': break;
10465 case 'a': break;
10466 case 'b': USE_BITS (MB); break;
10467 case 'c': USE_BITS (MC); break;
10468 case 'd': USE_BITS (MD); break;
10469 case 'e': USE_BITS (ME); break;
10470 case 'f': USE_BITS (MF); break;
10471 case 'g': USE_BITS (MG); break;
10472 case 'h': USE_BITS (MH); break;
10473 case 'i': USE_BITS (MI); break;
10474 case 'j': USE_BITS (MJ); break;
10475 case 'l': USE_BITS (ML); break;
10476 case 'm': USE_BITS (MM); break;
10477 case 'n': USE_BITS (MN); break;
10478 case 'p': USE_BITS (MP); break;
10479 case 'q': USE_BITS (MQ); break;
10480 case 'r': break;
10481 case 's': break;
10482 case 't': break;
10483 case 'x': break;
10484 case 'y': break;
10485 case 'z': break;
10486 default:
10487 as_bad (_("Internal error: bad mips opcode "
10488 "(unknown extension operand type `%c%c'): %s %s"),
10489 e, c, opc->name, opc->args);
10490 return 0;
10491 }
10492 break;
10493 case '.': USE_BITS (OFFSET10); break;
10494 case '1': USE_BITS (STYPE); break;
03f66e8a
MR
10495 case '2': USE_BITS (BP); break;
10496 case '3': USE_BITS (SA3); break;
10497 case '4': USE_BITS (SA4); break;
10498 case '5': USE_BITS (IMM8); break;
10499 case '6': USE_BITS (RS); break;
10500 case '7': USE_BITS (DSPACC); break;
10501 case '8': USE_BITS (WRDSP); break;
10502 case '0': USE_BITS (DSPSFT); break;
df58fc94
RS
10503 case '<': USE_BITS (SHAMT); break;
10504 case '>': USE_BITS (SHAMT); break;
03f66e8a 10505 case '@': USE_BITS (IMM10); break;
df58fc94
RS
10506 case 'B': USE_BITS (CODE10); break;
10507 case 'C': USE_BITS (COPZ); break;
10508 case 'D': USE_BITS (FD); break;
10509 case 'E': USE_BITS (RT); break;
10510 case 'G': USE_BITS (RS); break;
444d75be 10511 case 'H': USE_BITS (SEL); break;
df58fc94
RS
10512 case 'K': USE_BITS (RS); break;
10513 case 'M': USE_BITS (CCC); break;
10514 case 'N': USE_BITS (BCC); break;
10515 case 'R': USE_BITS (FR); break;
10516 case 'S': USE_BITS (FS); break;
10517 case 'T': USE_BITS (FT); break;
10518 case 'V': USE_BITS (FS); break;
dec0624d 10519 case '\\': USE_BITS (3BITPOS); break;
03f66e8a 10520 case '^': USE_BITS (RD); break;
df58fc94
RS
10521 case 'a': USE_BITS (TARGET); break;
10522 case 'b': USE_BITS (RS); break;
10523 case 'c': USE_BITS (CODE); break;
10524 case 'd': USE_BITS (RD); break;
10525 case 'h': USE_BITS (PREFX); break;
10526 case 'i': USE_BITS (IMMEDIATE); break;
10527 case 'j': USE_BITS (DELTA); break;
10528 case 'k': USE_BITS (CACHE); break;
10529 case 'n': USE_BITS (RT); break;
10530 case 'o': USE_BITS (DELTA); break;
10531 case 'p': USE_BITS (DELTA); break;
10532 case 'q': USE_BITS (CODE2); break;
10533 case 'r': USE_BITS (RS); break;
10534 case 's': USE_BITS (RS); break;
10535 case 't': USE_BITS (RT); break;
10536 case 'u': USE_BITS (IMMEDIATE); break;
10537 case 'v': USE_BITS (RS); break;
10538 case 'w': USE_BITS (RT); break;
10539 case 'y': USE_BITS (RS3); break;
10540 case 'z': break;
10541 case '|': USE_BITS (TRAP); break;
10542 case '~': USE_BITS (OFFSET12); break;
10543 default:
10544 as_bad (_("Internal error: bad microMIPS opcode "
10545 "(unknown operand type `%c'): %s %s"),
10546 c, opc->name, opc->args);
10547 return 0;
10548 }
10549#undef USE_BITS
10550 if (used_bits != insn_bits)
10551 {
10552 if (~used_bits & insn_bits)
10553 as_bad (_("Internal error: bad microMIPS opcode "
10554 "(bits 0x%lx undefined): %s %s"),
10555 ~used_bits & insn_bits, opc->name, opc->args);
10556 if (used_bits & ~insn_bits)
10557 as_bad (_("Internal error: bad microMIPS opcode "
10558 "(bits 0x%lx defined): %s %s"),
10559 used_bits & ~insn_bits, opc->name, opc->args);
10560 return 0;
10561 }
10562 return 1;
10563}
10564
9bcd4f99
TS
10565/* UDI immediates. */
10566struct mips_immed {
10567 char type;
10568 unsigned int shift;
10569 unsigned long mask;
10570 const char * desc;
10571};
10572
10573static const struct mips_immed mips_immed[] = {
10574 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
10575 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
10576 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
10577 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
10578 { 0,0,0,0 }
10579};
10580
7455baf8
TS
10581/* Check whether an odd floating-point register is allowed. */
10582static int
10583mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
10584{
10585 const char *s = insn->name;
10586
10587 if (insn->pinfo == INSN_MACRO)
10588 /* Let a macro pass, we'll catch it later when it is expanded. */
10589 return 1;
10590
10591 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa))
10592 {
10593 /* Allow odd registers for single-precision ops. */
10594 switch (insn->pinfo & (FP_S | FP_D))
10595 {
10596 case FP_S:
10597 case 0:
10598 return 1; /* both single precision - ok */
10599 case FP_D:
10600 return 0; /* both double precision - fail */
10601 default:
10602 break;
10603 }
10604
10605 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
10606 s = strchr (insn->name, '.');
10607 if (argnum == 2)
10608 s = s != NULL ? strchr (s + 1, '.') : NULL;
10609 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
10610 }
10611
10612 /* Single-precision coprocessor loads and moves are OK too. */
10613 if ((insn->pinfo & FP_S)
10614 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
10615 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
10616 return 1;
10617
10618 return 0;
10619}
10620
df58fc94
RS
10621/* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
10622 taking bits from BIT up. */
10623static int
10624expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
10625{
10626 return (ep->X_op == O_constant
10627 && (ep->X_add_number & ((1 << bit) - 1)) == 0
10628 && ep->X_add_number >= min << bit
10629 && ep->X_add_number < max << bit);
10630}
10631
252b5132
RH
10632/* This routine assembles an instruction into its binary format. As a
10633 side effect, it sets one of the global variables imm_reloc or
10634 offset_reloc to the type of relocation to do if one of the operands
10635 is an address expression. */
10636
10637static void
17a2f251 10638mips_ip (char *str, struct mips_cl_insn *ip)
252b5132 10639{
df58fc94
RS
10640 bfd_boolean wrong_delay_slot_insns = FALSE;
10641 bfd_boolean need_delay_slot_ok = TRUE;
10642 struct mips_opcode *firstinsn = NULL;
10643 const struct mips_opcode *past;
10644 struct hash_control *hash;
252b5132
RH
10645 char *s;
10646 const char *args;
43841e91 10647 char c = 0;
252b5132
RH
10648 struct mips_opcode *insn;
10649 char *argsStart;
10650 unsigned int regno;
34224acf 10651 unsigned int lastregno;
df58fc94 10652 unsigned int destregno = 0;
af7ee8bf 10653 unsigned int lastpos = 0;
071742cf 10654 unsigned int limlo, limhi;
252b5132 10655 char *s_reset;
74cd071d 10656 offsetT min_range, max_range;
df58fc94 10657 long opend;
a40bc9dd 10658 char *name;
707bfff6
TS
10659 int argnum;
10660 unsigned int rtype;
df58fc94 10661 char *dot;
a40bc9dd 10662 long end;
252b5132
RH
10663
10664 insn_error = NULL;
10665
df58fc94
RS
10666 if (mips_opts.micromips)
10667 {
10668 hash = micromips_op_hash;
10669 past = &micromips_opcodes[bfd_micromips_num_opcodes];
10670 }
10671 else
10672 {
10673 hash = op_hash;
10674 past = &mips_opcodes[NUMOPCODES];
10675 }
10676 forced_insn_length = 0;
252b5132 10677 insn = NULL;
252b5132 10678
df58fc94 10679 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
10680 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
10681 continue;
bdaaa2e1 10682
a40bc9dd
RS
10683 /* Make a copy of the instruction so that we can fiddle with it. */
10684 name = alloca (end + 1);
10685 memcpy (name, str, end);
10686 name[end] = '\0';
252b5132 10687
df58fc94
RS
10688 for (;;)
10689 {
10690 insn = (struct mips_opcode *) hash_find (hash, name);
10691
10692 if (insn != NULL || !mips_opts.micromips)
10693 break;
10694 if (forced_insn_length)
10695 break;
10696
10697 /* See if there's an instruction size override suffix,
10698 either `16' or `32', at the end of the mnemonic proper,
10699 that defines the operation, i.e. before the first `.'
10700 character if any. Strip it and retry. */
10701 dot = strchr (name, '.');
10702 opend = dot != NULL ? dot - name : end;
10703 if (opend < 3)
10704 break;
10705 if (name[opend - 2] == '1' && name[opend - 1] == '6')
10706 forced_insn_length = 2;
10707 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
10708 forced_insn_length = 4;
10709 else
10710 break;
10711 memcpy (name + opend - 2, name + opend, end - opend + 1);
10712 }
252b5132
RH
10713 if (insn == NULL)
10714 {
a40bc9dd
RS
10715 insn_error = _("Unrecognized opcode");
10716 return;
252b5132
RH
10717 }
10718
df58fc94
RS
10719 /* For microMIPS instructions placed in a fixed-length branch delay slot
10720 we make up to two passes over the relevant fragment of the opcode
10721 table. First we try instructions that meet the delay slot's length
10722 requirement. If none matched, then we retry with the remaining ones
10723 and if one matches, then we use it and then issue an appropriate
10724 warning later on. */
a40bc9dd 10725 argsStart = s = str + end;
252b5132
RH
10726 for (;;)
10727 {
df58fc94
RS
10728 bfd_boolean delay_slot_ok;
10729 bfd_boolean size_ok;
b34976b6 10730 bfd_boolean ok;
252b5132 10731
a40bc9dd 10732 gas_assert (strcmp (insn->name, name) == 0);
252b5132 10733
f79e2745 10734 ok = is_opcode_valid (insn);
df58fc94
RS
10735 size_ok = is_size_valid (insn);
10736 delay_slot_ok = is_delay_slot_valid (insn);
10737 if (!delay_slot_ok && !wrong_delay_slot_insns)
252b5132 10738 {
df58fc94
RS
10739 firstinsn = insn;
10740 wrong_delay_slot_insns = TRUE;
10741 }
10742 if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
10743 {
10744 static char buf[256];
10745
10746 if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
252b5132
RH
10747 {
10748 ++insn;
10749 continue;
10750 }
df58fc94 10751 if (wrong_delay_slot_insns && need_delay_slot_ok)
beae10d5 10752 {
df58fc94
RS
10753 gas_assert (firstinsn);
10754 need_delay_slot_ok = FALSE;
10755 past = insn + 1;
10756 insn = firstinsn;
10757 continue;
252b5132 10758 }
df58fc94
RS
10759
10760 if (insn_error)
10761 return;
10762
10763 if (!ok)
7bd942df 10764 sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
df58fc94
RS
10765 mips_cpu_info_from_arch (mips_opts.arch)->name,
10766 mips_cpu_info_from_isa (mips_opts.isa)->name);
10767 else
10768 sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
10769 8 * forced_insn_length);
10770 insn_error = buf;
10771
10772 return;
252b5132
RH
10773 }
10774
1e915849 10775 create_insn (ip, insn);
268f6bed 10776 insn_error = NULL;
707bfff6 10777 argnum = 1;
24864476 10778 lastregno = 0xffffffff;
252b5132
RH
10779 for (args = insn->args;; ++args)
10780 {
deec1734
CD
10781 int is_mdmx;
10782
ad8d3bb3 10783 s += strspn (s, " \t");
deec1734 10784 is_mdmx = 0;
252b5132
RH
10785 switch (*args)
10786 {
10787 case '\0': /* end of args */
10788 if (*s == '\0')
10789 return;
10790 break;
10791
03f66e8a
MR
10792 case '2':
10793 /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
10794 code) or 14 (for microMIPS code). */
8b082fb1
TS
10795 my_getExpression (&imm_expr, s);
10796 check_absolute_expr (ip, &imm_expr);
10797 if ((unsigned long) imm_expr.X_add_number != 1
10798 && (unsigned long) imm_expr.X_add_number != 3)
10799 {
10800 as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
10801 (unsigned long) imm_expr.X_add_number);
10802 }
03f66e8a
MR
10803 INSERT_OPERAND (mips_opts.micromips,
10804 BP, *ip, imm_expr.X_add_number);
8b082fb1
TS
10805 imm_expr.X_op = O_absent;
10806 s = expr_end;
10807 continue;
10808
03f66e8a
MR
10809 case '3':
10810 /* DSP 3-bit unsigned immediate in bit 13 (for standard MIPS
10811 code) or 21 (for microMIPS code). */
10812 {
10813 unsigned long mask = (mips_opts.micromips
10814 ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
10815
10816 my_getExpression (&imm_expr, s);
10817 check_absolute_expr (ip, &imm_expr);
10818 if ((unsigned long) imm_expr.X_add_number > mask)
10819 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10820 mask, (unsigned long) imm_expr.X_add_number);
10821 INSERT_OPERAND (mips_opts.micromips,
10822 SA3, *ip, imm_expr.X_add_number);
10823 imm_expr.X_op = O_absent;
10824 s = expr_end;
10825 }
74cd071d
CF
10826 continue;
10827
03f66e8a
MR
10828 case '4':
10829 /* DSP 4-bit unsigned immediate in bit 12 (for standard MIPS
10830 code) or 21 (for microMIPS code). */
10831 {
10832 unsigned long mask = (mips_opts.micromips
10833 ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
10834
10835 my_getExpression (&imm_expr, s);
10836 check_absolute_expr (ip, &imm_expr);
10837 if ((unsigned long) imm_expr.X_add_number > mask)
10838 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10839 mask, (unsigned long) imm_expr.X_add_number);
10840 INSERT_OPERAND (mips_opts.micromips,
10841 SA4, *ip, imm_expr.X_add_number);
10842 imm_expr.X_op = O_absent;
10843 s = expr_end;
10844 }
74cd071d
CF
10845 continue;
10846
03f66e8a
MR
10847 case '5':
10848 /* DSP 8-bit unsigned immediate in bit 13 (for standard MIPS
10849 code) or 16 (for microMIPS code). */
10850 {
10851 unsigned long mask = (mips_opts.micromips
10852 ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
10853
10854 my_getExpression (&imm_expr, s);
10855 check_absolute_expr (ip, &imm_expr);
10856 if ((unsigned long) imm_expr.X_add_number > mask)
10857 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10858 mask, (unsigned long) imm_expr.X_add_number);
10859 INSERT_OPERAND (mips_opts.micromips,
10860 IMM8, *ip, imm_expr.X_add_number);
10861 imm_expr.X_op = O_absent;
10862 s = expr_end;
10863 }
74cd071d
CF
10864 continue;
10865
03f66e8a
MR
10866 case '6':
10867 /* DSP 5-bit unsigned immediate in bit 16 (for standard MIPS
10868 code) or 21 (for microMIPS code). */
10869 {
10870 unsigned long mask = (mips_opts.micromips
10871 ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
10872
10873 my_getExpression (&imm_expr, s);
10874 check_absolute_expr (ip, &imm_expr);
10875 if ((unsigned long) imm_expr.X_add_number > mask)
10876 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10877 mask, (unsigned long) imm_expr.X_add_number);
10878 INSERT_OPERAND (mips_opts.micromips,
10879 RS, *ip, imm_expr.X_add_number);
10880 imm_expr.X_op = O_absent;
10881 s = expr_end;
10882 }
74cd071d
CF
10883 continue;
10884
90ecf173 10885 case '7': /* Four DSP accumulators in bits 11,12. */
03f66e8a
MR
10886 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
10887 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
10888 {
10889 regno = s[3] - '0';
10890 s += 4;
03f66e8a 10891 INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
74cd071d
CF
10892 continue;
10893 }
10894 else
10895 as_bad (_("Invalid dsp acc register"));
10896 break;
10897
03f66e8a
MR
10898 case '8':
10899 /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
10900 code) or 14 (for microMIPS code). */
10901 {
10902 unsigned long mask = (mips_opts.micromips
10903 ? MICROMIPSOP_MASK_WRDSP
10904 : OP_MASK_WRDSP);
10905
10906 my_getExpression (&imm_expr, s);
10907 check_absolute_expr (ip, &imm_expr);
10908 if ((unsigned long) imm_expr.X_add_number > mask)
10909 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10910 mask, (unsigned long) imm_expr.X_add_number);
10911 INSERT_OPERAND (mips_opts.micromips,
10912 WRDSP, *ip, imm_expr.X_add_number);
10913 imm_expr.X_op = O_absent;
10914 s = expr_end;
10915 }
74cd071d
CF
10916 continue;
10917
90ecf173 10918 case '9': /* Four DSP accumulators in bits 21,22. */
df58fc94 10919 gas_assert (!mips_opts.micromips);
03f66e8a
MR
10920 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
10921 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
10922 {
10923 regno = s[3] - '0';
10924 s += 4;
df58fc94 10925 INSERT_OPERAND (0, DSPACC_S, *ip, regno);
74cd071d
CF
10926 continue;
10927 }
10928 else
10929 as_bad (_("Invalid dsp acc register"));
10930 break;
10931
03f66e8a
MR
10932 case '0':
10933 /* DSP 6-bit signed immediate in bit 16 (for standard MIPS
10934 code) or 20 (for microMIPS code). */
10935 {
10936 long mask = (mips_opts.micromips
10937 ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
10938
10939 my_getExpression (&imm_expr, s);
10940 check_absolute_expr (ip, &imm_expr);
10941 min_range = -((mask + 1) >> 1);
10942 max_range = ((mask + 1) >> 1) - 1;
10943 if (imm_expr.X_add_number < min_range
10944 || imm_expr.X_add_number > max_range)
a9e24354
TS
10945 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
10946 (long) min_range, (long) max_range,
10947 (long) imm_expr.X_add_number);
03f66e8a
MR
10948 INSERT_OPERAND (mips_opts.micromips,
10949 DSPSFT, *ip, imm_expr.X_add_number);
10950 imm_expr.X_op = O_absent;
10951 s = expr_end;
10952 }
74cd071d
CF
10953 continue;
10954
90ecf173 10955 case '\'': /* DSP 6-bit unsigned immediate in bit 16. */
df58fc94 10956 gas_assert (!mips_opts.micromips);
74cd071d
CF
10957 my_getExpression (&imm_expr, s);
10958 check_absolute_expr (ip, &imm_expr);
10959 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
10960 {
a9e24354
TS
10961 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
10962 OP_MASK_RDDSP,
10963 (unsigned long) imm_expr.X_add_number);
74cd071d 10964 }
df58fc94 10965 INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
10966 imm_expr.X_op = O_absent;
10967 s = expr_end;
10968 continue;
10969
90ecf173 10970 case ':': /* DSP 7-bit signed immediate in bit 19. */
df58fc94 10971 gas_assert (!mips_opts.micromips);
74cd071d
CF
10972 my_getExpression (&imm_expr, s);
10973 check_absolute_expr (ip, &imm_expr);
10974 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
10975 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
10976 if (imm_expr.X_add_number < min_range ||
10977 imm_expr.X_add_number > max_range)
10978 {
a9e24354
TS
10979 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
10980 (long) min_range, (long) max_range,
10981 (long) imm_expr.X_add_number);
74cd071d 10982 }
df58fc94 10983 INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
74cd071d
CF
10984 imm_expr.X_op = O_absent;
10985 s = expr_end;
10986 continue;
10987
90ecf173 10988 case '@': /* DSP 10-bit signed immediate in bit 16. */
03f66e8a
MR
10989 {
10990 long mask = (mips_opts.micromips
10991 ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
10992
10993 my_getExpression (&imm_expr, s);
10994 check_absolute_expr (ip, &imm_expr);
10995 min_range = -((mask + 1) >> 1);
10996 max_range = ((mask + 1) >> 1) - 1;
10997 if (imm_expr.X_add_number < min_range
10998 || imm_expr.X_add_number > max_range)
a9e24354
TS
10999 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11000 (long) min_range, (long) max_range,
11001 (long) imm_expr.X_add_number);
03f66e8a
MR
11002 INSERT_OPERAND (mips_opts.micromips,
11003 IMM10, *ip, imm_expr.X_add_number);
11004 imm_expr.X_op = O_absent;
11005 s = expr_end;
11006 }
11007 continue;
11008
11009 case '^': /* DSP 5-bit unsigned immediate in bit 11. */
11010 gas_assert (mips_opts.micromips);
11011 my_getExpression (&imm_expr, s);
11012 check_absolute_expr (ip, &imm_expr);
11013 if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11014 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11015 MICROMIPSOP_MASK_RD,
11016 (unsigned long) imm_expr.X_add_number);
11017 INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
74cd071d
CF
11018 imm_expr.X_op = O_absent;
11019 s = expr_end;
11020 continue;
11021
a9e24354 11022 case '!': /* MT usermode flag bit. */
df58fc94 11023 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11024 my_getExpression (&imm_expr, s);
11025 check_absolute_expr (ip, &imm_expr);
11026 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
a9e24354
TS
11027 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11028 (unsigned long) imm_expr.X_add_number);
df58fc94 11029 INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11030 imm_expr.X_op = O_absent;
11031 s = expr_end;
11032 continue;
11033
a9e24354 11034 case '$': /* MT load high flag bit. */
df58fc94 11035 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11036 my_getExpression (&imm_expr, s);
11037 check_absolute_expr (ip, &imm_expr);
11038 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
a9e24354
TS
11039 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11040 (unsigned long) imm_expr.X_add_number);
df58fc94 11041 INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11042 imm_expr.X_op = O_absent;
11043 s = expr_end;
11044 continue;
11045
90ecf173 11046 case '*': /* Four DSP accumulators in bits 18,19. */
df58fc94 11047 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11048 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11049 s[3] >= '0' && s[3] <= '3')
11050 {
11051 regno = s[3] - '0';
11052 s += 4;
df58fc94 11053 INSERT_OPERAND (0, MTACC_T, *ip, regno);
ef2e4d86
CF
11054 continue;
11055 }
11056 else
11057 as_bad (_("Invalid dsp/smartmips acc register"));
11058 break;
11059
90ecf173 11060 case '&': /* Four DSP accumulators in bits 13,14. */
df58fc94 11061 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11062 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11063 s[3] >= '0' && s[3] <= '3')
11064 {
11065 regno = s[3] - '0';
11066 s += 4;
df58fc94 11067 INSERT_OPERAND (0, MTACC_D, *ip, regno);
ef2e4d86
CF
11068 continue;
11069 }
11070 else
11071 as_bad (_("Invalid dsp/smartmips acc register"));
11072 break;
11073
dec0624d
MR
11074 case '\\': /* 3-bit bit position. */
11075 {
2906b037
MR
11076 unsigned long mask = (mips_opts.micromips
11077 ? MICROMIPSOP_MASK_3BITPOS
11078 : OP_MASK_3BITPOS);
dec0624d
MR
11079
11080 my_getExpression (&imm_expr, s);
11081 check_absolute_expr (ip, &imm_expr);
11082 if ((unsigned long) imm_expr.X_add_number > mask)
11083 as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11084 ip->insn_mo->name,
11085 mask, (unsigned long) imm_expr.X_add_number);
11086 INSERT_OPERAND (mips_opts.micromips,
11087 3BITPOS, *ip, imm_expr.X_add_number);
11088 imm_expr.X_op = O_absent;
11089 s = expr_end;
11090 }
11091 continue;
11092
252b5132 11093 case ',':
a339155f 11094 ++argnum;
252b5132
RH
11095 if (*s++ == *args)
11096 continue;
11097 s--;
11098 switch (*++args)
11099 {
11100 case 'r':
11101 case 'v':
df58fc94 11102 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132
RH
11103 continue;
11104
11105 case 'w':
df58fc94 11106 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
38487616
TS
11107 continue;
11108
252b5132 11109 case 'W':
df58fc94
RS
11110 gas_assert (!mips_opts.micromips);
11111 INSERT_OPERAND (0, FT, *ip, lastregno);
252b5132
RH
11112 continue;
11113
11114 case 'V':
df58fc94 11115 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132
RH
11116 continue;
11117 }
11118 break;
11119
11120 case '(':
11121 /* Handle optional base register.
11122 Either the base register is omitted or
bdaaa2e1 11123 we must have a left paren. */
252b5132
RH
11124 /* This is dependent on the next operand specifier
11125 is a base register specification. */
df58fc94
RS
11126 gas_assert (args[1] == 'b'
11127 || (mips_opts.micromips
11128 && args[1] == 'm'
11129 && (args[2] == 'l' || args[2] == 'n'
11130 || args[2] == 's' || args[2] == 'a')));
11131 if (*s == '\0' && args[1] == 'b')
252b5132 11132 return;
df58fc94 11133 /* Fall through. */
252b5132 11134
90ecf173 11135 case ')': /* These must match exactly. */
df58fc94
RS
11136 if (*s++ == *args)
11137 continue;
11138 break;
11139
11140 case '[': /* These must match exactly. */
60b63b72 11141 case ']':
df58fc94 11142 gas_assert (!mips_opts.micromips);
252b5132
RH
11143 if (*s++ == *args)
11144 continue;
11145 break;
11146
af7ee8bf
CD
11147 case '+': /* Opcode extension character. */
11148 switch (*++args)
11149 {
9bcd4f99
TS
11150 case '1': /* UDI immediates. */
11151 case '2':
11152 case '3':
11153 case '4':
df58fc94 11154 gas_assert (!mips_opts.micromips);
9bcd4f99
TS
11155 {
11156 const struct mips_immed *imm = mips_immed;
11157
11158 while (imm->type && imm->type != *args)
11159 ++imm;
11160 if (! imm->type)
11161 internalError ();
11162 my_getExpression (&imm_expr, s);
11163 check_absolute_expr (ip, &imm_expr);
11164 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11165 {
11166 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11167 imm->desc ? imm->desc : ip->insn_mo->name,
11168 (unsigned long) imm_expr.X_add_number,
11169 (unsigned long) imm_expr.X_add_number);
90ecf173 11170 imm_expr.X_add_number &= imm->mask;
9bcd4f99
TS
11171 }
11172 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11173 << imm->shift);
11174 imm_expr.X_op = O_absent;
11175 s = expr_end;
11176 }
11177 continue;
90ecf173 11178
071742cf
CD
11179 case 'A': /* ins/ext position, becomes LSB. */
11180 limlo = 0;
11181 limhi = 31;
5f74bc13
CD
11182 goto do_lsb;
11183 case 'E':
11184 limlo = 32;
11185 limhi = 63;
11186 goto do_lsb;
90ecf173 11187 do_lsb:
071742cf
CD
11188 my_getExpression (&imm_expr, s);
11189 check_absolute_expr (ip, &imm_expr);
11190 if ((unsigned long) imm_expr.X_add_number < limlo
11191 || (unsigned long) imm_expr.X_add_number > limhi)
11192 {
11193 as_bad (_("Improper position (%lu)"),
11194 (unsigned long) imm_expr.X_add_number);
11195 imm_expr.X_add_number = limlo;
11196 }
11197 lastpos = imm_expr.X_add_number;
df58fc94
RS
11198 INSERT_OPERAND (mips_opts.micromips,
11199 EXTLSB, *ip, imm_expr.X_add_number);
071742cf
CD
11200 imm_expr.X_op = O_absent;
11201 s = expr_end;
11202 continue;
11203
11204 case 'B': /* ins size, becomes MSB. */
11205 limlo = 1;
11206 limhi = 32;
5f74bc13
CD
11207 goto do_msb;
11208 case 'F':
11209 limlo = 33;
11210 limhi = 64;
11211 goto do_msb;
90ecf173 11212 do_msb:
071742cf
CD
11213 my_getExpression (&imm_expr, s);
11214 check_absolute_expr (ip, &imm_expr);
11215 /* Check for negative input so that small negative numbers
11216 will not succeed incorrectly. The checks against
11217 (pos+size) transitively check "size" itself,
11218 assuming that "pos" is reasonable. */
11219 if ((long) imm_expr.X_add_number < 0
11220 || ((unsigned long) imm_expr.X_add_number
11221 + lastpos) < limlo
11222 || ((unsigned long) imm_expr.X_add_number
11223 + lastpos) > limhi)
11224 {
11225 as_bad (_("Improper insert size (%lu, position %lu)"),
11226 (unsigned long) imm_expr.X_add_number,
11227 (unsigned long) lastpos);
11228 imm_expr.X_add_number = limlo - lastpos;
11229 }
df58fc94
RS
11230 INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11231 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
11232 imm_expr.X_op = O_absent;
11233 s = expr_end;
11234 continue;
11235
11236 case 'C': /* ext size, becomes MSBD. */
11237 limlo = 1;
11238 limhi = 32;
5f74bc13
CD
11239 goto do_msbd;
11240 case 'G':
11241 limlo = 33;
11242 limhi = 64;
11243 goto do_msbd;
11244 case 'H':
11245 limlo = 33;
11246 limhi = 64;
11247 goto do_msbd;
90ecf173 11248 do_msbd:
071742cf
CD
11249 my_getExpression (&imm_expr, s);
11250 check_absolute_expr (ip, &imm_expr);
11251 /* Check for negative input so that small negative numbers
11252 will not succeed incorrectly. The checks against
11253 (pos+size) transitively check "size" itself,
11254 assuming that "pos" is reasonable. */
11255 if ((long) imm_expr.X_add_number < 0
11256 || ((unsigned long) imm_expr.X_add_number
11257 + lastpos) < limlo
11258 || ((unsigned long) imm_expr.X_add_number
11259 + lastpos) > limhi)
11260 {
11261 as_bad (_("Improper extract size (%lu, position %lu)"),
11262 (unsigned long) imm_expr.X_add_number,
11263 (unsigned long) lastpos);
11264 imm_expr.X_add_number = limlo - lastpos;
11265 }
df58fc94
RS
11266 INSERT_OPERAND (mips_opts.micromips,
11267 EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
11268 imm_expr.X_op = O_absent;
11269 s = expr_end;
11270 continue;
af7ee8bf 11271
bbcc0807
CD
11272 case 'D':
11273 /* +D is for disassembly only; never match. */
11274 break;
11275
5f74bc13
CD
11276 case 'I':
11277 /* "+I" is like "I", except that imm2_expr is used. */
11278 my_getExpression (&imm2_expr, s);
11279 if (imm2_expr.X_op != O_big
11280 && imm2_expr.X_op != O_constant)
11281 insn_error = _("absolute expression required");
9ee2a2d4
MR
11282 if (HAVE_32BIT_GPRS)
11283 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
11284 s = expr_end;
11285 continue;
11286
707bfff6 11287 case 'T': /* Coprocessor register. */
df58fc94 11288 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11289 /* +T is for disassembly only; never match. */
11290 break;
11291
707bfff6 11292 case 't': /* Coprocessor register number. */
df58fc94 11293 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11294 if (s[0] == '$' && ISDIGIT (s[1]))
11295 {
11296 ++s;
11297 regno = 0;
11298 do
11299 {
11300 regno *= 10;
11301 regno += *s - '0';
11302 ++s;
11303 }
11304 while (ISDIGIT (*s));
11305 if (regno > 31)
11306 as_bad (_("Invalid register number (%d)"), regno);
11307 else
11308 {
df58fc94 11309 INSERT_OPERAND (0, RT, *ip, regno);
ef2e4d86
CF
11310 continue;
11311 }
11312 }
11313 else
11314 as_bad (_("Invalid coprocessor 0 register number"));
11315 break;
11316
bb35fb24
NC
11317 case 'x':
11318 /* bbit[01] and bbit[01]32 bit index. Give error if index
11319 is not in the valid range. */
df58fc94 11320 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11321 my_getExpression (&imm_expr, s);
11322 check_absolute_expr (ip, &imm_expr);
11323 if ((unsigned) imm_expr.X_add_number > 31)
11324 {
11325 as_bad (_("Improper bit index (%lu)"),
11326 (unsigned long) imm_expr.X_add_number);
11327 imm_expr.X_add_number = 0;
11328 }
df58fc94 11329 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
bb35fb24
NC
11330 imm_expr.X_op = O_absent;
11331 s = expr_end;
11332 continue;
11333
11334 case 'X':
11335 /* bbit[01] bit index when bbit is used but we generate
11336 bbit[01]32 because the index is over 32. Move to the
11337 next candidate if index is not in the valid range. */
df58fc94 11338 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11339 my_getExpression (&imm_expr, s);
11340 check_absolute_expr (ip, &imm_expr);
11341 if ((unsigned) imm_expr.X_add_number < 32
11342 || (unsigned) imm_expr.X_add_number > 63)
11343 break;
df58fc94 11344 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
11345 imm_expr.X_op = O_absent;
11346 s = expr_end;
11347 continue;
11348
11349 case 'p':
11350 /* cins, cins32, exts and exts32 position field. Give error
11351 if it's not in the valid range. */
df58fc94 11352 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11353 my_getExpression (&imm_expr, s);
11354 check_absolute_expr (ip, &imm_expr);
11355 if ((unsigned) imm_expr.X_add_number > 31)
11356 {
11357 as_bad (_("Improper position (%lu)"),
11358 (unsigned long) imm_expr.X_add_number);
11359 imm_expr.X_add_number = 0;
11360 }
11361 /* Make the pos explicit to simplify +S. */
11362 lastpos = imm_expr.X_add_number + 32;
df58fc94 11363 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
bb35fb24
NC
11364 imm_expr.X_op = O_absent;
11365 s = expr_end;
11366 continue;
11367
11368 case 'P':
11369 /* cins, cins32, exts and exts32 position field. Move to
11370 the next candidate if it's not in the valid range. */
df58fc94 11371 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11372 my_getExpression (&imm_expr, s);
11373 check_absolute_expr (ip, &imm_expr);
11374 if ((unsigned) imm_expr.X_add_number < 32
11375 || (unsigned) imm_expr.X_add_number > 63)
11376 break;
11377 lastpos = imm_expr.X_add_number;
df58fc94 11378 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
11379 imm_expr.X_op = O_absent;
11380 s = expr_end;
11381 continue;
11382
11383 case 's':
11384 /* cins and exts length-minus-one field. */
df58fc94 11385 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11386 my_getExpression (&imm_expr, s);
11387 check_absolute_expr (ip, &imm_expr);
11388 if ((unsigned long) imm_expr.X_add_number > 31)
11389 {
11390 as_bad (_("Improper size (%lu)"),
11391 (unsigned long) imm_expr.X_add_number);
11392 imm_expr.X_add_number = 0;
11393 }
df58fc94 11394 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
11395 imm_expr.X_op = O_absent;
11396 s = expr_end;
11397 continue;
11398
11399 case 'S':
11400 /* cins32/exts32 and cins/exts aliasing cint32/exts32
11401 length-minus-one field. */
df58fc94 11402 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11403 my_getExpression (&imm_expr, s);
11404 check_absolute_expr (ip, &imm_expr);
11405 if ((long) imm_expr.X_add_number < 0
11406 || (unsigned long) imm_expr.X_add_number + lastpos > 63)
11407 {
11408 as_bad (_("Improper size (%lu)"),
11409 (unsigned long) imm_expr.X_add_number);
11410 imm_expr.X_add_number = 0;
11411 }
df58fc94 11412 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
11413 imm_expr.X_op = O_absent;
11414 s = expr_end;
11415 continue;
11416
dd3cbb7e
NC
11417 case 'Q':
11418 /* seqi/snei immediate field. */
df58fc94 11419 gas_assert (!mips_opts.micromips);
dd3cbb7e
NC
11420 my_getExpression (&imm_expr, s);
11421 check_absolute_expr (ip, &imm_expr);
11422 if ((long) imm_expr.X_add_number < -512
11423 || (long) imm_expr.X_add_number >= 512)
11424 {
11425 as_bad (_("Improper immediate (%ld)"),
11426 (long) imm_expr.X_add_number);
11427 imm_expr.X_add_number = 0;
11428 }
df58fc94 11429 INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
dd3cbb7e
NC
11430 imm_expr.X_op = O_absent;
11431 s = expr_end;
11432 continue;
11433
98675402 11434 case 'a': /* 8-bit signed offset in bit 6 */
df58fc94 11435 gas_assert (!mips_opts.micromips);
98675402
RS
11436 my_getExpression (&imm_expr, s);
11437 check_absolute_expr (ip, &imm_expr);
11438 min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
11439 max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
11440 if (imm_expr.X_add_number < min_range
11441 || imm_expr.X_add_number > max_range)
11442 {
c95354ed 11443 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11444 (long) min_range, (long) max_range,
11445 (long) imm_expr.X_add_number);
11446 }
df58fc94 11447 INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
98675402
RS
11448 imm_expr.X_op = O_absent;
11449 s = expr_end;
11450 continue;
11451
11452 case 'b': /* 8-bit signed offset in bit 3 */
df58fc94 11453 gas_assert (!mips_opts.micromips);
98675402
RS
11454 my_getExpression (&imm_expr, s);
11455 check_absolute_expr (ip, &imm_expr);
11456 min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
11457 max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
11458 if (imm_expr.X_add_number < min_range
11459 || imm_expr.X_add_number > max_range)
11460 {
c95354ed 11461 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11462 (long) min_range, (long) max_range,
11463 (long) imm_expr.X_add_number);
11464 }
df58fc94 11465 INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
98675402
RS
11466 imm_expr.X_op = O_absent;
11467 s = expr_end;
11468 continue;
11469
11470 case 'c': /* 9-bit signed offset in bit 6 */
df58fc94 11471 gas_assert (!mips_opts.micromips);
98675402
RS
11472 my_getExpression (&imm_expr, s);
11473 check_absolute_expr (ip, &imm_expr);
11474 min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
11475 max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
c95354ed
MX
11476 /* We check the offset range before adjusted. */
11477 min_range <<= 4;
11478 max_range <<= 4;
98675402
RS
11479 if (imm_expr.X_add_number < min_range
11480 || imm_expr.X_add_number > max_range)
11481 {
c95354ed 11482 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11483 (long) min_range, (long) max_range,
11484 (long) imm_expr.X_add_number);
11485 }
c95354ed
MX
11486 if (imm_expr.X_add_number & 0xf)
11487 {
11488 as_bad (_("Offset not 16 bytes alignment (%ld)"),
11489 (long) imm_expr.X_add_number);
11490 }
11491 /* Right shift 4 bits to adjust the offset operand. */
df58fc94
RS
11492 INSERT_OPERAND (0, OFFSET_C, *ip,
11493 imm_expr.X_add_number >> 4);
98675402
RS
11494 imm_expr.X_op = O_absent;
11495 s = expr_end;
11496 continue;
11497
11498 case 'z':
df58fc94 11499 gas_assert (!mips_opts.micromips);
98675402
RS
11500 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
11501 break;
11502 if (regno == AT && mips_opts.at)
11503 {
11504 if (mips_opts.at == ATREG)
11505 as_warn (_("used $at without \".set noat\""));
11506 else
11507 as_warn (_("used $%u with \".set at=$%u\""),
11508 regno, mips_opts.at);
11509 }
df58fc94 11510 INSERT_OPERAND (0, RZ, *ip, regno);
98675402
RS
11511 continue;
11512
11513 case 'Z':
df58fc94 11514 gas_assert (!mips_opts.micromips);
98675402
RS
11515 if (!reg_lookup (&s, RTYPE_FPU, &regno))
11516 break;
df58fc94 11517 INSERT_OPERAND (0, FZ, *ip, regno);
98675402
RS
11518 continue;
11519
af7ee8bf 11520 default:
df58fc94 11521 as_bad (_("Internal error: bad %s opcode "
90ecf173 11522 "(unknown extension operand type `+%c'): %s %s"),
df58fc94 11523 mips_opts.micromips ? "microMIPS" : "MIPS",
90ecf173 11524 *args, insn->name, insn->args);
af7ee8bf
CD
11525 /* Further processing is fruitless. */
11526 return;
11527 }
11528 break;
11529
df58fc94 11530 case '.': /* 10-bit offset. */
df58fc94 11531 gas_assert (mips_opts.micromips);
dec0624d 11532 case '~': /* 12-bit offset. */
df58fc94
RS
11533 {
11534 int shift = *args == '.' ? 9 : 11;
11535 size_t i;
11536
11537 /* Check whether there is only a single bracketed expression
11538 left. If so, it must be the base register and the
11539 constant must be zero. */
11540 if (*s == '(' && strchr (s + 1, '(') == 0)
11541 continue;
11542
11543 /* If this value won't fit into the offset, then go find
11544 a macro that will generate a 16- or 32-bit offset code
11545 pattern. */
11546 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11547 if ((i == 0 && (imm_expr.X_op != O_constant
11548 || imm_expr.X_add_number >= 1 << shift
11549 || imm_expr.X_add_number < -1 << shift))
11550 || i > 0)
11551 {
11552 imm_expr.X_op = O_absent;
11553 break;
11554 }
11555 if (shift == 9)
11556 INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
11557 else
dec0624d
MR
11558 INSERT_OPERAND (mips_opts.micromips,
11559 OFFSET12, *ip, imm_expr.X_add_number);
df58fc94
RS
11560 imm_expr.X_op = O_absent;
11561 s = expr_end;
11562 }
11563 continue;
11564
252b5132
RH
11565 case '<': /* must be at least one digit */
11566 /*
11567 * According to the manual, if the shift amount is greater
b6ff326e
KH
11568 * than 31 or less than 0, then the shift amount should be
11569 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
11570 * We issue a warning and mask out all but the low 5 bits.
11571 */
11572 my_getExpression (&imm_expr, s);
11573 check_absolute_expr (ip, &imm_expr);
11574 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
11575 as_warn (_("Improper shift amount (%lu)"),
11576 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11577 INSERT_OPERAND (mips_opts.micromips,
11578 SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
11579 imm_expr.X_op = O_absent;
11580 s = expr_end;
11581 continue;
11582
11583 case '>': /* shift amount minus 32 */
11584 my_getExpression (&imm_expr, s);
11585 check_absolute_expr (ip, &imm_expr);
11586 if ((unsigned long) imm_expr.X_add_number < 32
11587 || (unsigned long) imm_expr.X_add_number > 63)
11588 break;
df58fc94
RS
11589 INSERT_OPERAND (mips_opts.micromips,
11590 SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
11591 imm_expr.X_op = O_absent;
11592 s = expr_end;
11593 continue;
11594
90ecf173
MR
11595 case 'k': /* CACHE code. */
11596 case 'h': /* PREFX code. */
11597 case '1': /* SYNC type. */
252b5132
RH
11598 my_getExpression (&imm_expr, s);
11599 check_absolute_expr (ip, &imm_expr);
11600 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
11601 as_warn (_("Invalid value for `%s' (%lu)"),
11602 ip->insn_mo->name,
11603 (unsigned long) imm_expr.X_add_number);
df58fc94 11604 switch (*args)
d954098f 11605 {
df58fc94
RS
11606 case 'k':
11607 if (mips_fix_cn63xxp1
11608 && !mips_opts.micromips
11609 && strcmp ("pref", insn->name) == 0)
d954098f
DD
11610 switch (imm_expr.X_add_number)
11611 {
11612 case 5:
11613 case 25:
11614 case 26:
11615 case 27:
11616 case 28:
11617 case 29:
11618 case 30:
11619 case 31: /* These are ok. */
11620 break;
11621
11622 default: /* The rest must be changed to 28. */
11623 imm_expr.X_add_number = 28;
11624 break;
11625 }
df58fc94
RS
11626 INSERT_OPERAND (mips_opts.micromips,
11627 CACHE, *ip, imm_expr.X_add_number);
11628 break;
11629 case 'h':
11630 INSERT_OPERAND (mips_opts.micromips,
11631 PREFX, *ip, imm_expr.X_add_number);
11632 break;
11633 case '1':
11634 INSERT_OPERAND (mips_opts.micromips,
11635 STYPE, *ip, imm_expr.X_add_number);
11636 break;
d954098f 11637 }
252b5132
RH
11638 imm_expr.X_op = O_absent;
11639 s = expr_end;
11640 continue;
11641
90ecf173 11642 case 'c': /* BREAK code. */
df58fc94
RS
11643 {
11644 unsigned long mask = (mips_opts.micromips
11645 ? MICROMIPSOP_MASK_CODE
11646 : OP_MASK_CODE);
11647
11648 my_getExpression (&imm_expr, s);
11649 check_absolute_expr (ip, &imm_expr);
11650 if ((unsigned long) imm_expr.X_add_number > mask)
11651 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11652 ip->insn_mo->name,
11653 mask, (unsigned long) imm_expr.X_add_number);
11654 INSERT_OPERAND (mips_opts.micromips,
11655 CODE, *ip, imm_expr.X_add_number);
11656 imm_expr.X_op = O_absent;
11657 s = expr_end;
11658 }
252b5132
RH
11659 continue;
11660
90ecf173 11661 case 'q': /* Lower BREAK code. */
df58fc94
RS
11662 {
11663 unsigned long mask = (mips_opts.micromips
11664 ? MICROMIPSOP_MASK_CODE2
11665 : OP_MASK_CODE2);
11666
11667 my_getExpression (&imm_expr, s);
11668 check_absolute_expr (ip, &imm_expr);
11669 if ((unsigned long) imm_expr.X_add_number > mask)
11670 as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
11671 ip->insn_mo->name,
11672 mask, (unsigned long) imm_expr.X_add_number);
11673 INSERT_OPERAND (mips_opts.micromips,
11674 CODE2, *ip, imm_expr.X_add_number);
11675 imm_expr.X_op = O_absent;
11676 s = expr_end;
11677 }
252b5132
RH
11678 continue;
11679
df58fc94
RS
11680 case 'B': /* 20- or 10-bit syscall/break/wait code. */
11681 {
11682 unsigned long mask = (mips_opts.micromips
11683 ? MICROMIPSOP_MASK_CODE10
11684 : OP_MASK_CODE20);
11685
11686 my_getExpression (&imm_expr, s);
11687 check_absolute_expr (ip, &imm_expr);
11688 if ((unsigned long) imm_expr.X_add_number > mask)
11689 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11690 ip->insn_mo->name,
11691 mask, (unsigned long) imm_expr.X_add_number);
11692 if (mips_opts.micromips)
11693 INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
11694 else
11695 INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
11696 imm_expr.X_op = O_absent;
11697 s = expr_end;
11698 }
252b5132
RH
11699 continue;
11700
df58fc94
RS
11701 case 'C': /* 25- or 23-bit coprocessor code. */
11702 {
11703 unsigned long mask = (mips_opts.micromips
11704 ? MICROMIPSOP_MASK_COPZ
11705 : OP_MASK_COPZ);
11706
11707 my_getExpression (&imm_expr, s);
11708 check_absolute_expr (ip, &imm_expr);
11709 if ((unsigned long) imm_expr.X_add_number > mask)
11710 as_warn (_("Coproccesor code > %u bits (%lu)"),
11711 mips_opts.micromips ? 23U : 25U,
793b27f4 11712 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11713 INSERT_OPERAND (mips_opts.micromips,
11714 COPZ, *ip, imm_expr.X_add_number);
11715 imm_expr.X_op = O_absent;
11716 s = expr_end;
11717 }
beae10d5 11718 continue;
252b5132 11719
df58fc94
RS
11720 case 'J': /* 19-bit WAIT code. */
11721 gas_assert (!mips_opts.micromips);
4372b673
NC
11722 my_getExpression (&imm_expr, s);
11723 check_absolute_expr (ip, &imm_expr);
793b27f4 11724 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
df58fc94
RS
11725 {
11726 as_warn (_("Illegal 19-bit code (%lu)"),
a9e24354 11727 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11728 imm_expr.X_add_number &= OP_MASK_CODE19;
11729 }
11730 INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
11731 imm_expr.X_op = O_absent;
11732 s = expr_end;
11733 continue;
11734
707bfff6 11735 case 'P': /* Performance register. */
df58fc94 11736 gas_assert (!mips_opts.micromips);
beae10d5 11737 my_getExpression (&imm_expr, s);
252b5132 11738 check_absolute_expr (ip, &imm_expr);
beae10d5 11739 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
11740 as_warn (_("Invalid performance register (%lu)"),
11741 (unsigned long) imm_expr.X_add_number);
df58fc94 11742 INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
11743 imm_expr.X_op = O_absent;
11744 s = expr_end;
11745 continue;
252b5132 11746
707bfff6 11747 case 'G': /* Coprocessor destination register. */
df58fc94
RS
11748 {
11749 unsigned long opcode = ip->insn_opcode;
11750 unsigned long mask;
11751 unsigned int types;
11752 int cop0;
11753
11754 if (mips_opts.micromips)
11755 {
11756 mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
11757 | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
11758 | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
11759 opcode &= mask;
11760 switch (opcode)
11761 {
11762 case 0x000000fc: /* mfc0 */
11763 case 0x000002fc: /* mtc0 */
11764 case 0x580000fc: /* dmfc0 */
11765 case 0x580002fc: /* dmtc0 */
11766 cop0 = 1;
11767 break;
11768 default:
11769 cop0 = 0;
11770 break;
11771 }
11772 }
11773 else
11774 {
11775 opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
11776 cop0 = opcode == OP_OP_COP0;
11777 }
11778 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
11779 ok = reg_lookup (&s, types, &regno);
11780 if (mips_opts.micromips)
11781 INSERT_OPERAND (1, RS, *ip, regno);
11782 else
11783 INSERT_OPERAND (0, RD, *ip, regno);
11784 if (ok)
11785 {
11786 lastregno = regno;
11787 continue;
11788 }
11789 }
11790 break;
707bfff6 11791
df58fc94
RS
11792 case 'y': /* ALNV.PS source register. */
11793 gas_assert (mips_opts.micromips);
11794 goto do_reg;
11795 case 'x': /* Ignore register name. */
11796 case 'U': /* Destination register (CLO/CLZ). */
11797 case 'g': /* Coprocessor destination register. */
11798 gas_assert (!mips_opts.micromips);
90ecf173
MR
11799 case 'b': /* Base register. */
11800 case 'd': /* Destination register. */
11801 case 's': /* Source register. */
11802 case 't': /* Target register. */
11803 case 'r': /* Both target and source. */
11804 case 'v': /* Both dest and source. */
11805 case 'w': /* Both dest and target. */
11806 case 'E': /* Coprocessor target register. */
11807 case 'K': /* RDHWR destination register. */
90ecf173 11808 case 'z': /* Must be zero register. */
df58fc94 11809 do_reg:
90ecf173 11810 s_reset = s;
707bfff6
TS
11811 if (*args == 'E' || *args == 'K')
11812 ok = reg_lookup (&s, RTYPE_NUM, &regno);
11813 else
11814 {
11815 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
741fe287
MR
11816 if (regno == AT && mips_opts.at)
11817 {
11818 if (mips_opts.at == ATREG)
f71d0d44 11819 as_warn (_("Used $at without \".set noat\""));
741fe287 11820 else
f71d0d44 11821 as_warn (_("Used $%u with \".set at=$%u\""),
741fe287
MR
11822 regno, mips_opts.at);
11823 }
707bfff6
TS
11824 }
11825 if (ok)
252b5132 11826 {
252b5132
RH
11827 c = *args;
11828 if (*s == ' ')
f9419b05 11829 ++s;
252b5132
RH
11830 if (args[1] != *s)
11831 {
11832 if (c == 'r' || c == 'v' || c == 'w')
11833 {
11834 regno = lastregno;
11835 s = s_reset;
f9419b05 11836 ++args;
252b5132
RH
11837 }
11838 }
11839 /* 'z' only matches $0. */
11840 if (c == 'z' && regno != 0)
11841 break;
11842
24864476 11843 if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
e7c604dd
CM
11844 {
11845 if (regno == lastregno)
90ecf173
MR
11846 {
11847 insn_error
f71d0d44 11848 = _("Source and destination must be different");
e7c604dd 11849 continue;
90ecf173 11850 }
24864476 11851 if (regno == 31 && lastregno == 0xffffffff)
90ecf173
MR
11852 {
11853 insn_error
f71d0d44 11854 = _("A destination register must be supplied");
e7c604dd 11855 continue;
90ecf173 11856 }
e7c604dd 11857 }
90ecf173
MR
11858 /* Now that we have assembled one operand, we use the args
11859 string to figure out where it goes in the instruction. */
252b5132
RH
11860 switch (c)
11861 {
11862 case 'r':
11863 case 's':
11864 case 'v':
11865 case 'b':
df58fc94 11866 INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
252b5132 11867 break;
df58fc94 11868
af7ee8bf 11869 case 'K':
df58fc94
RS
11870 if (mips_opts.micromips)
11871 INSERT_OPERAND (1, RS, *ip, regno);
11872 else
11873 INSERT_OPERAND (0, RD, *ip, regno);
11874 break;
11875
11876 case 'd':
ef2e4d86 11877 case 'g':
df58fc94 11878 INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
252b5132 11879 break;
df58fc94 11880
4372b673 11881 case 'U':
df58fc94
RS
11882 gas_assert (!mips_opts.micromips);
11883 INSERT_OPERAND (0, RD, *ip, regno);
11884 INSERT_OPERAND (0, RT, *ip, regno);
4372b673 11885 break;
df58fc94 11886
252b5132
RH
11887 case 'w':
11888 case 't':
11889 case 'E':
df58fc94
RS
11890 INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
11891 break;
11892
11893 case 'y':
11894 gas_assert (mips_opts.micromips);
11895 INSERT_OPERAND (1, RS3, *ip, regno);
252b5132 11896 break;
df58fc94 11897
252b5132
RH
11898 case 'x':
11899 /* This case exists because on the r3000 trunc
11900 expands into a macro which requires a gp
11901 register. On the r6000 or r4000 it is
11902 assembled into a single instruction which
11903 ignores the register. Thus the insn version
11904 is MIPS_ISA2 and uses 'x', and the macro
11905 version is MIPS_ISA1 and uses 't'. */
11906 break;
df58fc94 11907
252b5132
RH
11908 case 'z':
11909 /* This case is for the div instruction, which
11910 acts differently if the destination argument
11911 is $0. This only matches $0, and is checked
11912 outside the switch. */
11913 break;
252b5132
RH
11914 }
11915 lastregno = regno;
11916 continue;
11917 }
252b5132
RH
11918 switch (*args++)
11919 {
11920 case 'r':
11921 case 'v':
df58fc94 11922 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132 11923 continue;
df58fc94 11924
252b5132 11925 case 'w':
df58fc94 11926 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
252b5132
RH
11927 continue;
11928 }
11929 break;
11930
deec1734 11931 case 'O': /* MDMX alignment immediate constant. */
df58fc94 11932 gas_assert (!mips_opts.micromips);
deec1734
CD
11933 my_getExpression (&imm_expr, s);
11934 check_absolute_expr (ip, &imm_expr);
11935 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
20203fb9 11936 as_warn (_("Improper align amount (%ld), using low bits"),
bf12938e 11937 (long) imm_expr.X_add_number);
df58fc94 11938 INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
deec1734
CD
11939 imm_expr.X_op = O_absent;
11940 s = expr_end;
11941 continue;
11942
11943 case 'Q': /* MDMX vector, element sel, or const. */
11944 if (s[0] != '$')
11945 {
11946 /* MDMX Immediate. */
df58fc94 11947 gas_assert (!mips_opts.micromips);
deec1734
CD
11948 my_getExpression (&imm_expr, s);
11949 check_absolute_expr (ip, &imm_expr);
11950 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
11951 as_warn (_("Invalid MDMX Immediate (%ld)"),
11952 (long) imm_expr.X_add_number);
df58fc94 11953 INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
deec1734
CD
11954 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
11955 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
11956 else
11957 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
11958 imm_expr.X_op = O_absent;
11959 s = expr_end;
11960 continue;
11961 }
11962 /* Not MDMX Immediate. Fall through. */
11963 case 'X': /* MDMX destination register. */
11964 case 'Y': /* MDMX source register. */
11965 case 'Z': /* MDMX target register. */
11966 is_mdmx = 1;
df58fc94
RS
11967 case 'W':
11968 gas_assert (!mips_opts.micromips);
90ecf173
MR
11969 case 'D': /* Floating point destination register. */
11970 case 'S': /* Floating point source register. */
11971 case 'T': /* Floating point target register. */
11972 case 'R': /* Floating point source register. */
252b5132 11973 case 'V':
707bfff6
TS
11974 rtype = RTYPE_FPU;
11975 if (is_mdmx
11976 || (mips_opts.ase_mdmx
11977 && (ip->insn_mo->pinfo & FP_D)
11978 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
11979 | INSN_COPROC_MEMORY_DELAY
11980 | INSN_LOAD_COPROC_DELAY
11981 | INSN_LOAD_MEMORY_DELAY
11982 | INSN_STORE_MEMORY))))
11983 rtype |= RTYPE_VEC;
252b5132 11984 s_reset = s;
707bfff6 11985 if (reg_lookup (&s, rtype, &regno))
252b5132 11986 {
252b5132 11987 if ((regno & 1) != 0
ca4e0257 11988 && HAVE_32BIT_FPRS
90ecf173 11989 && !mips_oddfpreg_ok (ip->insn_mo, argnum))
252b5132
RH
11990 as_warn (_("Float register should be even, was %d"),
11991 regno);
11992
11993 c = *args;
11994 if (*s == ' ')
f9419b05 11995 ++s;
252b5132
RH
11996 if (args[1] != *s)
11997 {
11998 if (c == 'V' || c == 'W')
11999 {
12000 regno = lastregno;
12001 s = s_reset;
f9419b05 12002 ++args;
252b5132
RH
12003 }
12004 }
12005 switch (c)
12006 {
12007 case 'D':
deec1734 12008 case 'X':
df58fc94 12009 INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
252b5132 12010 break;
df58fc94 12011
252b5132
RH
12012 case 'V':
12013 case 'S':
deec1734 12014 case 'Y':
df58fc94 12015 INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
252b5132 12016 break;
df58fc94 12017
deec1734
CD
12018 case 'Q':
12019 /* This is like 'Z', but also needs to fix the MDMX
12020 vector/scalar select bits. Note that the
12021 scalar immediate case is handled above. */
12022 if (*s == '[')
12023 {
12024 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12025 int max_el = (is_qh ? 3 : 7);
12026 s++;
12027 my_getExpression(&imm_expr, s);
12028 check_absolute_expr (ip, &imm_expr);
12029 s = expr_end;
12030 if (imm_expr.X_add_number > max_el)
20203fb9
NC
12031 as_bad (_("Bad element selector %ld"),
12032 (long) imm_expr.X_add_number);
deec1734
CD
12033 imm_expr.X_add_number &= max_el;
12034 ip->insn_opcode |= (imm_expr.X_add_number
12035 << (OP_SH_VSEL +
12036 (is_qh ? 2 : 1)));
01a3f561 12037 imm_expr.X_op = O_absent;
deec1734 12038 if (*s != ']')
20203fb9 12039 as_warn (_("Expecting ']' found '%s'"), s);
deec1734
CD
12040 else
12041 s++;
12042 }
12043 else
12044 {
12045 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12046 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12047 << OP_SH_VSEL);
12048 else
12049 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12050 OP_SH_VSEL);
12051 }
90ecf173 12052 /* Fall through. */
252b5132
RH
12053 case 'W':
12054 case 'T':
deec1734 12055 case 'Z':
df58fc94 12056 INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
252b5132 12057 break;
df58fc94 12058
252b5132 12059 case 'R':
df58fc94 12060 INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
252b5132
RH
12061 break;
12062 }
12063 lastregno = regno;
12064 continue;
12065 }
12066
252b5132
RH
12067 switch (*args++)
12068 {
12069 case 'V':
df58fc94 12070 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132 12071 continue;
df58fc94 12072
252b5132 12073 case 'W':
df58fc94 12074 INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
252b5132
RH
12075 continue;
12076 }
12077 break;
12078
12079 case 'I':
12080 my_getExpression (&imm_expr, s);
12081 if (imm_expr.X_op != O_big
12082 && imm_expr.X_op != O_constant)
12083 insn_error = _("absolute expression required");
9ee2a2d4
MR
12084 if (HAVE_32BIT_GPRS)
12085 normalize_constant_expr (&imm_expr);
252b5132
RH
12086 s = expr_end;
12087 continue;
12088
12089 case 'A':
12090 my_getExpression (&offset_expr, s);
2051e8c4 12091 normalize_address_expr (&offset_expr);
f6688943 12092 *imm_reloc = BFD_RELOC_32;
252b5132
RH
12093 s = expr_end;
12094 continue;
12095
12096 case 'F':
12097 case 'L':
12098 case 'f':
12099 case 'l':
12100 {
12101 int f64;
ca4e0257 12102 int using_gprs;
252b5132
RH
12103 char *save_in;
12104 char *err;
12105 unsigned char temp[8];
12106 int len;
12107 unsigned int length;
12108 segT seg;
12109 subsegT subseg;
12110 char *p;
12111
12112 /* These only appear as the last operand in an
12113 instruction, and every instruction that accepts
12114 them in any variant accepts them in all variants.
12115 This means we don't have to worry about backing out
12116 any changes if the instruction does not match.
12117
12118 The difference between them is the size of the
12119 floating point constant and where it goes. For 'F'
12120 and 'L' the constant is 64 bits; for 'f' and 'l' it
12121 is 32 bits. Where the constant is placed is based
12122 on how the MIPS assembler does things:
12123 F -- .rdata
12124 L -- .lit8
12125 f -- immediate value
12126 l -- .lit4
12127
12128 The .lit4 and .lit8 sections are only used if
12129 permitted by the -G argument.
12130
ca4e0257
RS
12131 The code below needs to know whether the target register
12132 is 32 or 64 bits wide. It relies on the fact 'f' and
12133 'F' are used with GPR-based instructions and 'l' and
12134 'L' are used with FPR-based instructions. */
252b5132
RH
12135
12136 f64 = *args == 'F' || *args == 'L';
ca4e0257 12137 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
12138
12139 save_in = input_line_pointer;
12140 input_line_pointer = s;
12141 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12142 length = len;
12143 s = input_line_pointer;
12144 input_line_pointer = save_in;
12145 if (err != NULL && *err != '\0')
12146 {
12147 as_bad (_("Bad floating point constant: %s"), err);
12148 memset (temp, '\0', sizeof temp);
12149 length = f64 ? 8 : 4;
12150 }
12151
9c2799c2 12152 gas_assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
12153
12154 if (*args == 'f'
12155 || (*args == 'l'
3e722fb5 12156 && (g_switch_value < 4
252b5132
RH
12157 || (temp[0] == 0 && temp[1] == 0)
12158 || (temp[2] == 0 && temp[3] == 0))))
12159 {
12160 imm_expr.X_op = O_constant;
90ecf173 12161 if (!target_big_endian)
252b5132
RH
12162 imm_expr.X_add_number = bfd_getl32 (temp);
12163 else
12164 imm_expr.X_add_number = bfd_getb32 (temp);
12165 }
12166 else if (length > 4
90ecf173 12167 && !mips_disable_float_construction
ca4e0257
RS
12168 /* Constants can only be constructed in GPRs and
12169 copied to FPRs if the GPRs are at least as wide
12170 as the FPRs. Force the constant into memory if
12171 we are using 64-bit FPRs but the GPRs are only
12172 32 bits wide. */
12173 && (using_gprs
90ecf173 12174 || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
12175 && ((temp[0] == 0 && temp[1] == 0)
12176 || (temp[2] == 0 && temp[3] == 0))
12177 && ((temp[4] == 0 && temp[5] == 0)
12178 || (temp[6] == 0 && temp[7] == 0)))
12179 {
ca4e0257 12180 /* The value is simple enough to load with a couple of
90ecf173
MR
12181 instructions. If using 32-bit registers, set
12182 imm_expr to the high order 32 bits and offset_expr to
12183 the low order 32 bits. Otherwise, set imm_expr to
12184 the entire 64 bit constant. */
ca4e0257 12185 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
12186 {
12187 imm_expr.X_op = O_constant;
12188 offset_expr.X_op = O_constant;
90ecf173 12189 if (!target_big_endian)
252b5132
RH
12190 {
12191 imm_expr.X_add_number = bfd_getl32 (temp + 4);
12192 offset_expr.X_add_number = bfd_getl32 (temp);
12193 }
12194 else
12195 {
12196 imm_expr.X_add_number = bfd_getb32 (temp);
12197 offset_expr.X_add_number = bfd_getb32 (temp + 4);
12198 }
12199 if (offset_expr.X_add_number == 0)
12200 offset_expr.X_op = O_absent;
12201 }
12202 else if (sizeof (imm_expr.X_add_number) > 4)
12203 {
12204 imm_expr.X_op = O_constant;
90ecf173 12205 if (!target_big_endian)
252b5132
RH
12206 imm_expr.X_add_number = bfd_getl64 (temp);
12207 else
12208 imm_expr.X_add_number = bfd_getb64 (temp);
12209 }
12210 else
12211 {
12212 imm_expr.X_op = O_big;
12213 imm_expr.X_add_number = 4;
90ecf173 12214 if (!target_big_endian)
252b5132
RH
12215 {
12216 generic_bignum[0] = bfd_getl16 (temp);
12217 generic_bignum[1] = bfd_getl16 (temp + 2);
12218 generic_bignum[2] = bfd_getl16 (temp + 4);
12219 generic_bignum[3] = bfd_getl16 (temp + 6);
12220 }
12221 else
12222 {
12223 generic_bignum[0] = bfd_getb16 (temp + 6);
12224 generic_bignum[1] = bfd_getb16 (temp + 4);
12225 generic_bignum[2] = bfd_getb16 (temp + 2);
12226 generic_bignum[3] = bfd_getb16 (temp);
12227 }
12228 }
12229 }
12230 else
12231 {
12232 const char *newname;
12233 segT new_seg;
12234
12235 /* Switch to the right section. */
12236 seg = now_seg;
12237 subseg = now_subseg;
12238 switch (*args)
12239 {
12240 default: /* unused default case avoids warnings. */
12241 case 'L':
12242 newname = RDATA_SECTION_NAME;
3e722fb5 12243 if (g_switch_value >= 8)
252b5132
RH
12244 newname = ".lit8";
12245 break;
12246 case 'F':
3e722fb5 12247 newname = RDATA_SECTION_NAME;
252b5132
RH
12248 break;
12249 case 'l':
9c2799c2 12250 gas_assert (g_switch_value >= 4);
252b5132
RH
12251 newname = ".lit4";
12252 break;
12253 }
12254 new_seg = subseg_new (newname, (subsegT) 0);
f43abd2b 12255 if (IS_ELF)
252b5132
RH
12256 bfd_set_section_flags (stdoutput, new_seg,
12257 (SEC_ALLOC
12258 | SEC_LOAD
12259 | SEC_READONLY
12260 | SEC_DATA));
12261 frag_align (*args == 'l' ? 2 : 3, 0, 0);
c41e87e3 12262 if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
12263 record_alignment (new_seg, 4);
12264 else
12265 record_alignment (new_seg, *args == 'l' ? 2 : 3);
12266 if (seg == now_seg)
12267 as_bad (_("Can't use floating point insn in this section"));
12268
df58fc94
RS
12269 /* Set the argument to the current address in the
12270 section. */
12271 offset_expr.X_op = O_symbol;
12272 offset_expr.X_add_symbol = symbol_temp_new_now ();
12273 offset_expr.X_add_number = 0;
12274
12275 /* Put the floating point number into the section. */
12276 p = frag_more ((int) length);
12277 memcpy (p, temp, length);
12278
12279 /* Switch back to the original section. */
12280 subseg_set (seg, subseg);
12281 }
12282 }
12283 continue;
12284
12285 case 'i': /* 16-bit unsigned immediate. */
12286 case 'j': /* 16-bit signed immediate. */
12287 *imm_reloc = BFD_RELOC_LO16;
12288 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12289 {
12290 int more;
12291 offsetT minval, maxval;
12292
12293 more = (insn + 1 < past
12294 && strcmp (insn->name, insn[1].name) == 0);
12295
12296 /* If the expression was written as an unsigned number,
12297 only treat it as signed if there are no more
12298 alternatives. */
12299 if (more
12300 && *args == 'j'
12301 && sizeof (imm_expr.X_add_number) <= 4
12302 && imm_expr.X_op == O_constant
12303 && imm_expr.X_add_number < 0
12304 && imm_expr.X_unsigned
12305 && HAVE_64BIT_GPRS)
12306 break;
12307
12308 /* For compatibility with older assemblers, we accept
12309 0x8000-0xffff as signed 16-bit numbers when only
12310 signed numbers are allowed. */
12311 if (*args == 'i')
12312 minval = 0, maxval = 0xffff;
12313 else if (more)
12314 minval = -0x8000, maxval = 0x7fff;
12315 else
12316 minval = -0x8000, maxval = 0xffff;
12317
12318 if (imm_expr.X_op != O_constant
12319 || imm_expr.X_add_number < minval
12320 || imm_expr.X_add_number > maxval)
12321 {
12322 if (more)
12323 break;
12324 if (imm_expr.X_op == O_constant
12325 || imm_expr.X_op == O_big)
12326 as_bad (_("Expression out of range"));
12327 }
12328 }
12329 s = expr_end;
12330 continue;
12331
12332 case 'o': /* 16-bit offset. */
12333 offset_reloc[0] = BFD_RELOC_LO16;
12334 offset_reloc[1] = BFD_RELOC_UNUSED;
12335 offset_reloc[2] = BFD_RELOC_UNUSED;
12336
12337 /* Check whether there is only a single bracketed expression
12338 left. If so, it must be the base register and the
12339 constant must be zero. */
12340 if (*s == '(' && strchr (s + 1, '(') == 0)
12341 {
12342 offset_expr.X_op = O_constant;
12343 offset_expr.X_add_number = 0;
12344 continue;
12345 }
12346
12347 /* If this value won't fit into a 16 bit offset, then go
12348 find a macro that will generate the 32 bit offset
12349 code pattern. */
12350 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
12351 && (offset_expr.X_op != O_constant
12352 || offset_expr.X_add_number >= 0x8000
12353 || offset_expr.X_add_number < -0x8000))
12354 break;
12355
12356 s = expr_end;
12357 continue;
12358
12359 case 'p': /* PC-relative offset. */
12360 *offset_reloc = BFD_RELOC_16_PCREL_S2;
12361 my_getExpression (&offset_expr, s);
12362 s = expr_end;
12363 continue;
12364
12365 case 'u': /* Upper 16 bits. */
12366 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
12367 && imm_expr.X_op == O_constant
12368 && (imm_expr.X_add_number < 0
12369 || imm_expr.X_add_number >= 0x10000))
12370 as_bad (_("lui expression (%lu) not in range 0..65535"),
12371 (unsigned long) imm_expr.X_add_number);
12372 s = expr_end;
12373 continue;
12374
12375 case 'a': /* 26-bit address. */
12376 *offset_reloc = BFD_RELOC_MIPS_JMP;
12377 my_getExpression (&offset_expr, s);
12378 s = expr_end;
12379 continue;
12380
12381 case 'N': /* 3-bit branch condition code. */
12382 case 'M': /* 3-bit compare condition code. */
12383 rtype = RTYPE_CCC;
12384 if (ip->insn_mo->pinfo & (FP_D | FP_S))
12385 rtype |= RTYPE_FCC;
12386 if (!reg_lookup (&s, rtype, &regno))
12387 break;
12388 if ((strcmp (str + strlen (str) - 3, ".ps") == 0
12389 || strcmp (str + strlen (str) - 5, "any2f") == 0
12390 || strcmp (str + strlen (str) - 5, "any2t") == 0)
12391 && (regno & 1) != 0)
12392 as_warn (_("Condition code register should be even for %s, "
12393 "was %d"),
12394 str, regno);
12395 if ((strcmp (str + strlen (str) - 5, "any4f") == 0
12396 || strcmp (str + strlen (str) - 5, "any4t") == 0)
12397 && (regno & 3) != 0)
12398 as_warn (_("Condition code register should be 0 or 4 for %s, "
12399 "was %d"),
12400 str, regno);
12401 if (*args == 'N')
12402 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
12403 else
12404 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
12405 continue;
12406
12407 case 'H':
12408 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
12409 s += 2;
12410 if (ISDIGIT (*s))
12411 {
12412 c = 0;
12413 do
12414 {
12415 c *= 10;
12416 c += *s - '0';
12417 ++s;
12418 }
12419 while (ISDIGIT (*s));
12420 }
12421 else
12422 c = 8; /* Invalid sel value. */
12423
12424 if (c > 7)
12425 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
12426 INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
12427 continue;
12428
12429 case 'e':
12430 gas_assert (!mips_opts.micromips);
12431 /* Must be at least one digit. */
12432 my_getExpression (&imm_expr, s);
12433 check_absolute_expr (ip, &imm_expr);
12434
12435 if ((unsigned long) imm_expr.X_add_number
12436 > (unsigned long) OP_MASK_VECBYTE)
12437 {
12438 as_bad (_("bad byte vector index (%ld)"),
12439 (long) imm_expr.X_add_number);
12440 imm_expr.X_add_number = 0;
12441 }
12442
12443 INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
12444 imm_expr.X_op = O_absent;
12445 s = expr_end;
12446 continue;
12447
12448 case '%':
12449 gas_assert (!mips_opts.micromips);
12450 my_getExpression (&imm_expr, s);
12451 check_absolute_expr (ip, &imm_expr);
12452
12453 if ((unsigned long) imm_expr.X_add_number
12454 > (unsigned long) OP_MASK_VECALIGN)
12455 {
12456 as_bad (_("bad byte vector index (%ld)"),
12457 (long) imm_expr.X_add_number);
12458 imm_expr.X_add_number = 0;
12459 }
12460
12461 INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
12462 imm_expr.X_op = O_absent;
12463 s = expr_end;
12464 continue;
12465
12466 case 'm': /* Opcode extension character. */
12467 gas_assert (mips_opts.micromips);
12468 c = *++args;
12469 switch (c)
12470 {
12471 case 'r':
12472 if (strncmp (s, "$pc", 3) == 0)
12473 {
12474 s += 3;
12475 continue;
12476 }
12477 break;
12478
12479 case 'a':
12480 case 'b':
12481 case 'c':
12482 case 'd':
12483 case 'e':
12484 case 'f':
12485 case 'g':
12486 case 'h':
12487 case 'i':
12488 case 'j':
12489 case 'l':
12490 case 'm':
12491 case 'n':
12492 case 'p':
12493 case 'q':
12494 case 's':
12495 case 't':
12496 case 'x':
12497 case 'y':
12498 case 'z':
12499 s_reset = s;
12500 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12501 if (regno == AT && mips_opts.at)
12502 {
12503 if (mips_opts.at == ATREG)
12504 as_warn (_("Used $at without \".set noat\""));
12505 else
12506 as_warn (_("Used $%u with \".set at=$%u\""),
12507 regno, mips_opts.at);
12508 }
12509 if (!ok)
12510 {
12511 if (c == 'c')
12512 {
12513 gas_assert (args[1] == ',');
12514 regno = lastregno;
12515 ++args;
12516 }
12517 else if (c == 't')
12518 {
12519 gas_assert (args[1] == ',');
12520 ++args;
12521 continue; /* Nothing to do. */
12522 }
12523 else
12524 break;
12525 }
12526
12527 if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
12528 {
12529 if (regno == lastregno)
12530 {
12531 insn_error
12532 = _("Source and destination must be different");
12533 continue;
12534 }
12535 if (regno == 31 && lastregno == 0xffffffff)
12536 {
12537 insn_error
12538 = _("A destination register must be supplied");
12539 continue;
12540 }
12541 }
12542
12543 if (*s == ' ')
12544 ++s;
12545 if (args[1] != *s)
12546 {
12547 if (c == 'e')
12548 {
12549 gas_assert (args[1] == ',');
12550 regno = lastregno;
12551 s = s_reset;
12552 ++args;
12553 }
12554 else if (c == 't')
12555 {
12556 gas_assert (args[1] == ',');
12557 s = s_reset;
12558 ++args;
12559 continue; /* Nothing to do. */
12560 }
12561 }
12562
12563 /* Make sure regno is the same as lastregno. */
12564 if (c == 't' && regno != lastregno)
12565 break;
12566
12567 /* Make sure regno is the same as destregno. */
12568 if (c == 'x' && regno != destregno)
12569 break;
12570
12571 /* We need to save regno, before regno maps to the
12572 microMIPS register encoding. */
12573 lastregno = regno;
12574
12575 if (c == 'f')
12576 destregno = regno;
12577
12578 switch (c)
12579 {
12580 case 'a':
12581 if (regno != GP)
12582 regno = ILLEGAL_REG;
12583 break;
12584
12585 case 'b':
12586 regno = mips32_to_micromips_reg_b_map[regno];
12587 break;
12588
12589 case 'c':
12590 regno = mips32_to_micromips_reg_c_map[regno];
12591 break;
12592
12593 case 'd':
12594 regno = mips32_to_micromips_reg_d_map[regno];
12595 break;
12596
12597 case 'e':
12598 regno = mips32_to_micromips_reg_e_map[regno];
12599 break;
12600
12601 case 'f':
12602 regno = mips32_to_micromips_reg_f_map[regno];
12603 break;
12604
12605 case 'g':
12606 regno = mips32_to_micromips_reg_g_map[regno];
12607 break;
12608
12609 case 'h':
12610 regno = mips32_to_micromips_reg_h_map[regno];
12611 break;
12612
12613 case 'i':
12614 switch (EXTRACT_OPERAND (1, MI, *ip))
12615 {
12616 case 4:
12617 if (regno == 21)
12618 regno = 3;
12619 else if (regno == 22)
12620 regno = 4;
12621 else if (regno == 5)
12622 regno = 5;
12623 else if (regno == 6)
12624 regno = 6;
12625 else if (regno == 7)
12626 regno = 7;
12627 else
12628 regno = ILLEGAL_REG;
12629 break;
12630
12631 case 5:
12632 if (regno == 6)
12633 regno = 0;
12634 else if (regno == 7)
12635 regno = 1;
12636 else
12637 regno = ILLEGAL_REG;
12638 break;
12639
12640 case 6:
12641 if (regno == 7)
12642 regno = 2;
12643 else
12644 regno = ILLEGAL_REG;
12645 break;
12646
12647 default:
12648 regno = ILLEGAL_REG;
12649 break;
12650 }
12651 break;
12652
12653 case 'l':
12654 regno = mips32_to_micromips_reg_l_map[regno];
12655 break;
12656
12657 case 'm':
12658 regno = mips32_to_micromips_reg_m_map[regno];
12659 break;
12660
12661 case 'n':
12662 regno = mips32_to_micromips_reg_n_map[regno];
12663 break;
12664
12665 case 'q':
12666 regno = mips32_to_micromips_reg_q_map[regno];
12667 break;
12668
12669 case 's':
12670 if (regno != SP)
12671 regno = ILLEGAL_REG;
12672 break;
12673
12674 case 'y':
12675 if (regno != 31)
12676 regno = ILLEGAL_REG;
12677 break;
12678
12679 case 'z':
12680 if (regno != ZERO)
12681 regno = ILLEGAL_REG;
12682 break;
12683
12684 case 'j': /* Do nothing. */
12685 case 'p':
12686 case 't':
12687 case 'x':
12688 break;
12689
12690 default:
12691 internalError ();
12692 }
12693
12694 if (regno == ILLEGAL_REG)
12695 break;
12696
12697 switch (c)
12698 {
12699 case 'b':
12700 INSERT_OPERAND (1, MB, *ip, regno);
12701 break;
12702
12703 case 'c':
12704 INSERT_OPERAND (1, MC, *ip, regno);
12705 break;
12706
12707 case 'd':
12708 INSERT_OPERAND (1, MD, *ip, regno);
12709 break;
12710
12711 case 'e':
12712 INSERT_OPERAND (1, ME, *ip, regno);
12713 break;
12714
12715 case 'f':
12716 INSERT_OPERAND (1, MF, *ip, regno);
12717 break;
12718
12719 case 'g':
12720 INSERT_OPERAND (1, MG, *ip, regno);
12721 break;
12722
12723 case 'h':
12724 INSERT_OPERAND (1, MH, *ip, regno);
12725 break;
12726
12727 case 'i':
12728 INSERT_OPERAND (1, MI, *ip, regno);
12729 break;
12730
12731 case 'j':
12732 INSERT_OPERAND (1, MJ, *ip, regno);
12733 break;
12734
12735 case 'l':
12736 INSERT_OPERAND (1, ML, *ip, regno);
12737 break;
12738
12739 case 'm':
12740 INSERT_OPERAND (1, MM, *ip, regno);
12741 break;
12742
12743 case 'n':
12744 INSERT_OPERAND (1, MN, *ip, regno);
12745 break;
12746
12747 case 'p':
12748 INSERT_OPERAND (1, MP, *ip, regno);
12749 break;
12750
12751 case 'q':
12752 INSERT_OPERAND (1, MQ, *ip, regno);
12753 break;
12754
12755 case 'a': /* Do nothing. */
12756 case 's': /* Do nothing. */
12757 case 't': /* Do nothing. */
12758 case 'x': /* Do nothing. */
12759 case 'y': /* Do nothing. */
12760 case 'z': /* Do nothing. */
12761 break;
12762
12763 default:
12764 internalError ();
12765 }
12766 continue;
12767
12768 case 'A':
12769 {
12770 bfd_reloc_code_real_type r[3];
12771 expressionS ep;
12772 int imm;
12773
12774 /* Check whether there is only a single bracketed
12775 expression left. If so, it must be the base register
12776 and the constant must be zero. */
12777 if (*s == '(' && strchr (s + 1, '(') == 0)
12778 {
12779 INSERT_OPERAND (1, IMMA, *ip, 0);
12780 continue;
12781 }
12782
12783 if (my_getSmallExpression (&ep, r, s) > 0
12784 || !expr_const_in_range (&ep, -64, 64, 2))
12785 break;
12786
12787 imm = ep.X_add_number >> 2;
12788 INSERT_OPERAND (1, IMMA, *ip, imm);
12789 }
12790 s = expr_end;
12791 continue;
12792
12793 case 'B':
12794 {
12795 bfd_reloc_code_real_type r[3];
12796 expressionS ep;
12797 int imm;
12798
12799 if (my_getSmallExpression (&ep, r, s) > 0
12800 || ep.X_op != O_constant)
12801 break;
12802
12803 for (imm = 0; imm < 8; imm++)
12804 if (micromips_imm_b_map[imm] == ep.X_add_number)
12805 break;
12806 if (imm >= 8)
12807 break;
12808
12809 INSERT_OPERAND (1, IMMB, *ip, imm);
12810 }
12811 s = expr_end;
12812 continue;
12813
12814 case 'C':
12815 {
12816 bfd_reloc_code_real_type r[3];
12817 expressionS ep;
12818 int imm;
12819
12820 if (my_getSmallExpression (&ep, r, s) > 0
12821 || ep.X_op != O_constant)
12822 break;
12823
12824 for (imm = 0; imm < 16; imm++)
12825 if (micromips_imm_c_map[imm] == ep.X_add_number)
12826 break;
12827 if (imm >= 16)
12828 break;
12829
12830 INSERT_OPERAND (1, IMMC, *ip, imm);
12831 }
12832 s = expr_end;
12833 continue;
12834
12835 case 'D': /* pc relative offset */
12836 case 'E': /* pc relative offset */
12837 my_getExpression (&offset_expr, s);
12838 if (offset_expr.X_op == O_register)
12839 break;
12840
40209cad
MR
12841 if (!forced_insn_length)
12842 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
12843 else if (c == 'D')
12844 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
12845 else
12846 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
df58fc94
RS
12847 s = expr_end;
12848 continue;
12849
12850 case 'F':
12851 {
12852 bfd_reloc_code_real_type r[3];
12853 expressionS ep;
12854 int imm;
12855
12856 if (my_getSmallExpression (&ep, r, s) > 0
12857 || !expr_const_in_range (&ep, 0, 16, 0))
12858 break;
12859
12860 imm = ep.X_add_number;
12861 INSERT_OPERAND (1, IMMF, *ip, imm);
12862 }
12863 s = expr_end;
12864 continue;
12865
12866 case 'G':
12867 {
12868 bfd_reloc_code_real_type r[3];
12869 expressionS ep;
12870 int imm;
12871
12872 /* Check whether there is only a single bracketed
12873 expression left. If so, it must be the base register
12874 and the constant must be zero. */
12875 if (*s == '(' && strchr (s + 1, '(') == 0)
12876 {
12877 INSERT_OPERAND (1, IMMG, *ip, 0);
12878 continue;
12879 }
12880
12881 if (my_getSmallExpression (&ep, r, s) > 0
12882 || !expr_const_in_range (&ep, -1, 15, 0))
12883 break;
12884
12885 imm = ep.X_add_number & 15;
12886 INSERT_OPERAND (1, IMMG, *ip, imm);
12887 }
12888 s = expr_end;
12889 continue;
12890
12891 case 'H':
12892 {
12893 bfd_reloc_code_real_type r[3];
12894 expressionS ep;
12895 int imm;
12896
12897 /* Check whether there is only a single bracketed
12898 expression left. If so, it must be the base register
12899 and the constant must be zero. */
12900 if (*s == '(' && strchr (s + 1, '(') == 0)
12901 {
12902 INSERT_OPERAND (1, IMMH, *ip, 0);
12903 continue;
12904 }
12905
12906 if (my_getSmallExpression (&ep, r, s) > 0
12907 || !expr_const_in_range (&ep, 0, 16, 1))
12908 break;
12909
12910 imm = ep.X_add_number >> 1;
12911 INSERT_OPERAND (1, IMMH, *ip, imm);
12912 }
12913 s = expr_end;
12914 continue;
12915
12916 case 'I':
12917 {
12918 bfd_reloc_code_real_type r[3];
12919 expressionS ep;
12920 int imm;
12921
12922 if (my_getSmallExpression (&ep, r, s) > 0
12923 || !expr_const_in_range (&ep, -1, 127, 0))
12924 break;
12925
12926 imm = ep.X_add_number & 127;
12927 INSERT_OPERAND (1, IMMI, *ip, imm);
12928 }
12929 s = expr_end;
12930 continue;
12931
12932 case 'J':
12933 {
12934 bfd_reloc_code_real_type r[3];
12935 expressionS ep;
12936 int imm;
12937
12938 /* Check whether there is only a single bracketed
12939 expression left. If so, it must be the base register
12940 and the constant must be zero. */
12941 if (*s == '(' && strchr (s + 1, '(') == 0)
12942 {
12943 INSERT_OPERAND (1, IMMJ, *ip, 0);
12944 continue;
12945 }
12946
12947 if (my_getSmallExpression (&ep, r, s) > 0
12948 || !expr_const_in_range (&ep, 0, 16, 2))
12949 break;
12950
12951 imm = ep.X_add_number >> 2;
12952 INSERT_OPERAND (1, IMMJ, *ip, imm);
12953 }
12954 s = expr_end;
12955 continue;
12956
12957 case 'L':
12958 {
12959 bfd_reloc_code_real_type r[3];
12960 expressionS ep;
12961 int imm;
12962
12963 /* Check whether there is only a single bracketed
12964 expression left. If so, it must be the base register
12965 and the constant must be zero. */
12966 if (*s == '(' && strchr (s + 1, '(') == 0)
12967 {
12968 INSERT_OPERAND (1, IMML, *ip, 0);
12969 continue;
12970 }
12971
12972 if (my_getSmallExpression (&ep, r, s) > 0
12973 || !expr_const_in_range (&ep, 0, 16, 0))
12974 break;
12975
12976 imm = ep.X_add_number;
12977 INSERT_OPERAND (1, IMML, *ip, imm);
12978 }
12979 s = expr_end;
12980 continue;
12981
12982 case 'M':
12983 {
12984 bfd_reloc_code_real_type r[3];
12985 expressionS ep;
12986 int imm;
12987
12988 if (my_getSmallExpression (&ep, r, s) > 0
12989 || !expr_const_in_range (&ep, 1, 9, 0))
12990 break;
12991
12992 imm = ep.X_add_number & 7;
12993 INSERT_OPERAND (1, IMMM, *ip, imm);
12994 }
12995 s = expr_end;
12996 continue;
12997
12998 case 'N': /* Register list for lwm and swm. */
12999 {
13000 /* A comma-separated list of registers and/or
13001 dash-separated contiguous ranges including
13002 both ra and a set of one or more registers
13003 starting at s0 up to s3 which have to be
13004 consecutive, e.g.:
13005
13006 s0, ra
13007 s0, s1, ra, s2, s3
13008 s0-s2, ra
13009
13010 and any permutations of these. */
13011 unsigned int reglist;
13012 int imm;
13013
13014 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13015 break;
13016
13017 if ((reglist & 0xfff1ffff) != 0x80010000)
13018 break;
13019
13020 reglist = (reglist >> 17) & 7;
13021 reglist += 1;
13022 if ((reglist & -reglist) != reglist)
13023 break;
252b5132 13024
df58fc94
RS
13025 imm = ffs (reglist) - 1;
13026 INSERT_OPERAND (1, IMMN, *ip, imm);
13027 }
13028 continue;
252b5132 13029
df58fc94
RS
13030 case 'O': /* sdbbp 4-bit code. */
13031 {
13032 bfd_reloc_code_real_type r[3];
13033 expressionS ep;
13034 int imm;
13035
13036 if (my_getSmallExpression (&ep, r, s) > 0
13037 || !expr_const_in_range (&ep, 0, 16, 0))
13038 break;
13039
13040 imm = ep.X_add_number;
13041 INSERT_OPERAND (1, IMMO, *ip, imm);
252b5132 13042 }
df58fc94
RS
13043 s = expr_end;
13044 continue;
252b5132 13045
df58fc94
RS
13046 case 'P':
13047 {
13048 bfd_reloc_code_real_type r[3];
13049 expressionS ep;
13050 int imm;
5e0116d5 13051
df58fc94
RS
13052 if (my_getSmallExpression (&ep, r, s) > 0
13053 || !expr_const_in_range (&ep, 0, 32, 2))
13054 break;
5e0116d5 13055
df58fc94
RS
13056 imm = ep.X_add_number >> 2;
13057 INSERT_OPERAND (1, IMMP, *ip, imm);
13058 }
13059 s = expr_end;
13060 continue;
5e0116d5 13061
df58fc94
RS
13062 case 'Q':
13063 {
13064 bfd_reloc_code_real_type r[3];
13065 expressionS ep;
13066 int imm;
5e0116d5 13067
df58fc94
RS
13068 if (my_getSmallExpression (&ep, r, s) > 0
13069 || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13070 break;
252b5132 13071
df58fc94
RS
13072 imm = ep.X_add_number >> 2;
13073 INSERT_OPERAND (1, IMMQ, *ip, imm);
13074 }
13075 s = expr_end;
13076 continue;
4614d845 13077
df58fc94
RS
13078 case 'U':
13079 {
13080 bfd_reloc_code_real_type r[3];
13081 expressionS ep;
13082 int imm;
13083
13084 /* Check whether there is only a single bracketed
13085 expression left. If so, it must be the base register
13086 and the constant must be zero. */
13087 if (*s == '(' && strchr (s + 1, '(') == 0)
13088 {
13089 INSERT_OPERAND (1, IMMU, *ip, 0);
13090 continue;
13091 }
13092
13093 if (my_getSmallExpression (&ep, r, s) > 0
13094 || !expr_const_in_range (&ep, 0, 32, 2))
13095 break;
13096
13097 imm = ep.X_add_number >> 2;
13098 INSERT_OPERAND (1, IMMU, *ip, imm);
13099 }
13100 s = expr_end;
5e0116d5 13101 continue;
252b5132 13102
df58fc94
RS
13103 case 'W':
13104 {
13105 bfd_reloc_code_real_type r[3];
13106 expressionS ep;
13107 int imm;
252b5132 13108
df58fc94
RS
13109 if (my_getSmallExpression (&ep, r, s) > 0
13110 || !expr_const_in_range (&ep, 0, 64, 2))
13111 break;
252b5132 13112
df58fc94
RS
13113 imm = ep.X_add_number >> 2;
13114 INSERT_OPERAND (1, IMMW, *ip, imm);
13115 }
13116 s = expr_end;
13117 continue;
252b5132 13118
df58fc94
RS
13119 case 'X':
13120 {
13121 bfd_reloc_code_real_type r[3];
13122 expressionS ep;
13123 int imm;
252b5132 13124
df58fc94
RS
13125 if (my_getSmallExpression (&ep, r, s) > 0
13126 || !expr_const_in_range (&ep, -8, 8, 0))
13127 break;
252b5132 13128
df58fc94
RS
13129 imm = ep.X_add_number;
13130 INSERT_OPERAND (1, IMMX, *ip, imm);
13131 }
13132 s = expr_end;
13133 continue;
252b5132 13134
df58fc94
RS
13135 case 'Y':
13136 {
13137 bfd_reloc_code_real_type r[3];
13138 expressionS ep;
13139 int imm;
156c2f8b 13140
df58fc94
RS
13141 if (my_getSmallExpression (&ep, r, s) > 0
13142 || expr_const_in_range (&ep, -2, 2, 2)
13143 || !expr_const_in_range (&ep, -258, 258, 2))
13144 break;
156c2f8b 13145
df58fc94
RS
13146 imm = ep.X_add_number >> 2;
13147 imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13148 INSERT_OPERAND (1, IMMY, *ip, imm);
13149 }
13150 s = expr_end;
13151 continue;
60b63b72 13152
df58fc94
RS
13153 case 'Z':
13154 {
13155 bfd_reloc_code_real_type r[3];
13156 expressionS ep;
13157
13158 if (my_getSmallExpression (&ep, r, s) > 0
13159 || !expr_const_in_range (&ep, 0, 1, 0))
13160 break;
13161 }
13162 s = expr_end;
13163 continue;
13164
13165 default:
13166 as_bad (_("Internal error: bad microMIPS opcode "
13167 "(unknown extension operand type `m%c'): %s %s"),
13168 *args, insn->name, insn->args);
13169 /* Further processing is fruitless. */
13170 return;
60b63b72 13171 }
df58fc94 13172 break;
60b63b72 13173
df58fc94
RS
13174 case 'n': /* Register list for 32-bit lwm and swm. */
13175 gas_assert (mips_opts.micromips);
13176 {
13177 /* A comma-separated list of registers and/or
13178 dash-separated contiguous ranges including
13179 at least one of ra and a set of one or more
13180 registers starting at s0 up to s7 and then
13181 s8 which have to be consecutive, e.g.:
13182
13183 ra
13184 s0
13185 ra, s0, s1, s2
13186 s0-s8
13187 s0-s5, ra
13188
13189 and any permutations of these. */
13190 unsigned int reglist;
13191 int imm;
13192 int ra;
13193
13194 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13195 break;
13196
13197 if ((reglist & 0x3f00ffff) != 0)
13198 break;
13199
13200 ra = (reglist >> 27) & 0x10;
13201 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13202 reglist += 1;
13203 if ((reglist & -reglist) != reglist)
13204 break;
13205
13206 imm = (ffs (reglist) - 1) | ra;
13207 INSERT_OPERAND (1, RT, *ip, imm);
13208 imm_expr.X_op = O_absent;
13209 }
60b63b72
RS
13210 continue;
13211
df58fc94
RS
13212 case '|': /* 4-bit trap code. */
13213 gas_assert (mips_opts.micromips);
60b63b72
RS
13214 my_getExpression (&imm_expr, s);
13215 check_absolute_expr (ip, &imm_expr);
60b63b72 13216 if ((unsigned long) imm_expr.X_add_number
df58fc94
RS
13217 > MICROMIPSOP_MASK_TRAP)
13218 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13219 (unsigned long) imm_expr.X_add_number,
13220 ip->insn_mo->name);
13221 INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
60b63b72
RS
13222 imm_expr.X_op = O_absent;
13223 s = expr_end;
13224 continue;
13225
252b5132 13226 default:
f71d0d44 13227 as_bad (_("Bad char = '%c'\n"), *args);
252b5132
RH
13228 internalError ();
13229 }
13230 break;
13231 }
13232 /* Args don't match. */
df58fc94
RS
13233 s = argsStart;
13234 insn_error = _("Illegal operands");
13235 if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
252b5132
RH
13236 {
13237 ++insn;
252b5132
RH
13238 continue;
13239 }
df58fc94
RS
13240 else if (wrong_delay_slot_insns && need_delay_slot_ok)
13241 {
13242 gas_assert (firstinsn);
13243 need_delay_slot_ok = FALSE;
13244 past = insn + 1;
13245 insn = firstinsn;
13246 continue;
13247 }
252b5132
RH
13248 return;
13249 }
13250}
13251
0499d65b
TS
13252#define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13253
252b5132
RH
13254/* This routine assembles an instruction into its binary format when
13255 assembling for the mips16. As a side effect, it sets one of the
df58fc94
RS
13256 global variables imm_reloc or offset_reloc to the type of relocation
13257 to do if one of the operands is an address expression. It also sets
13258 forced_insn_length to the resulting instruction size in bytes if the
13259 user explicitly requested a small or extended instruction. */
252b5132
RH
13260
13261static void
17a2f251 13262mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
13263{
13264 char *s;
13265 const char *args;
13266 struct mips_opcode *insn;
13267 char *argsstart;
13268 unsigned int regno;
13269 unsigned int lastregno = 0;
13270 char *s_reset;
d6f16593 13271 size_t i;
252b5132
RH
13272
13273 insn_error = NULL;
13274
df58fc94 13275 forced_insn_length = 0;
252b5132 13276
3882b010 13277 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
13278 ;
13279 switch (*s)
13280 {
13281 case '\0':
13282 break;
13283
13284 case ' ':
13285 *s++ = '\0';
13286 break;
13287
13288 case '.':
13289 if (s[1] == 't' && s[2] == ' ')
13290 {
13291 *s = '\0';
df58fc94 13292 forced_insn_length = 2;
252b5132
RH
13293 s += 3;
13294 break;
13295 }
13296 else if (s[1] == 'e' && s[2] == ' ')
13297 {
13298 *s = '\0';
df58fc94 13299 forced_insn_length = 4;
252b5132
RH
13300 s += 3;
13301 break;
13302 }
13303 /* Fall through. */
13304 default:
13305 insn_error = _("unknown opcode");
13306 return;
13307 }
13308
df58fc94
RS
13309 if (mips_opts.noautoextend && !forced_insn_length)
13310 forced_insn_length = 2;
252b5132
RH
13311
13312 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13313 {
13314 insn_error = _("unrecognized opcode");
13315 return;
13316 }
13317
13318 argsstart = s;
13319 for (;;)
13320 {
9b3f89ee
TS
13321 bfd_boolean ok;
13322
9c2799c2 13323 gas_assert (strcmp (insn->name, str) == 0);
252b5132 13324
037b32b9 13325 ok = is_opcode_valid_16 (insn);
9b3f89ee
TS
13326 if (! ok)
13327 {
13328 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13329 && strcmp (insn->name, insn[1].name) == 0)
13330 {
13331 ++insn;
13332 continue;
13333 }
13334 else
13335 {
13336 if (!insn_error)
13337 {
13338 static char buf[100];
13339 sprintf (buf,
7bd942df 13340 _("Opcode not supported on this processor: %s (%s)"),
9b3f89ee
TS
13341 mips_cpu_info_from_arch (mips_opts.arch)->name,
13342 mips_cpu_info_from_isa (mips_opts.isa)->name);
13343 insn_error = buf;
13344 }
13345 return;
13346 }
13347 }
13348
1e915849 13349 create_insn (ip, insn);
252b5132 13350 imm_expr.X_op = O_absent;
f6688943
TS
13351 imm_reloc[0] = BFD_RELOC_UNUSED;
13352 imm_reloc[1] = BFD_RELOC_UNUSED;
13353 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 13354 imm2_expr.X_op = O_absent;
252b5132 13355 offset_expr.X_op = O_absent;
f6688943
TS
13356 offset_reloc[0] = BFD_RELOC_UNUSED;
13357 offset_reloc[1] = BFD_RELOC_UNUSED;
13358 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
13359 for (args = insn->args; 1; ++args)
13360 {
13361 int c;
13362
13363 if (*s == ' ')
13364 ++s;
13365
13366 /* In this switch statement we call break if we did not find
13367 a match, continue if we did find a match, or return if we
13368 are done. */
13369
13370 c = *args;
13371 switch (c)
13372 {
13373 case '\0':
13374 if (*s == '\0')
13375 {
13376 /* Stuff the immediate value in now, if we can. */
13377 if (imm_expr.X_op == O_constant
f6688943 13378 && *imm_reloc > BFD_RELOC_UNUSED
738e5348
RS
13379 && *imm_reloc != BFD_RELOC_MIPS16_GOT16
13380 && *imm_reloc != BFD_RELOC_MIPS16_CALL16
252b5132
RH
13381 && insn->pinfo != INSN_MACRO)
13382 {
d6f16593
MR
13383 valueT tmp;
13384
13385 switch (*offset_reloc)
13386 {
13387 case BFD_RELOC_MIPS16_HI16_S:
13388 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
13389 break;
13390
13391 case BFD_RELOC_MIPS16_HI16:
13392 tmp = imm_expr.X_add_number >> 16;
13393 break;
13394
13395 case BFD_RELOC_MIPS16_LO16:
13396 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
13397 - 0x8000;
13398 break;
13399
13400 case BFD_RELOC_UNUSED:
13401 tmp = imm_expr.X_add_number;
13402 break;
13403
13404 default:
13405 internalError ();
13406 }
13407 *offset_reloc = BFD_RELOC_UNUSED;
13408
c4e7957c 13409 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
df58fc94
RS
13410 tmp, TRUE, forced_insn_length == 2,
13411 forced_insn_length == 4, &ip->insn_opcode,
252b5132
RH
13412 &ip->use_extend, &ip->extend);
13413 imm_expr.X_op = O_absent;
f6688943 13414 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
13415 }
13416
13417 return;
13418 }
13419 break;
13420
13421 case ',':
13422 if (*s++ == c)
13423 continue;
13424 s--;
13425 switch (*++args)
13426 {
13427 case 'v':
bf12938e 13428 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
13429 continue;
13430 case 'w':
bf12938e 13431 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
13432 continue;
13433 }
13434 break;
13435
13436 case '(':
13437 case ')':
13438 if (*s++ == c)
13439 continue;
13440 break;
13441
13442 case 'v':
13443 case 'w':
13444 if (s[0] != '$')
13445 {
13446 if (c == 'v')
bf12938e 13447 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 13448 else
bf12938e 13449 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
13450 ++args;
13451 continue;
13452 }
13453 /* Fall through. */
13454 case 'x':
13455 case 'y':
13456 case 'z':
13457 case 'Z':
13458 case '0':
13459 case 'S':
13460 case 'R':
13461 case 'X':
13462 case 'Y':
707bfff6
TS
13463 s_reset = s;
13464 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
252b5132 13465 {
707bfff6 13466 if (c == 'v' || c == 'w')
85b51719 13467 {
707bfff6 13468 if (c == 'v')
a9e24354 13469 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
707bfff6 13470 else
a9e24354 13471 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
707bfff6
TS
13472 ++args;
13473 continue;
85b51719 13474 }
707bfff6 13475 break;
252b5132
RH
13476 }
13477
13478 if (*s == ' ')
13479 ++s;
13480 if (args[1] != *s)
13481 {
13482 if (c == 'v' || c == 'w')
13483 {
13484 regno = mips16_to_32_reg_map[lastregno];
13485 s = s_reset;
f9419b05 13486 ++args;
252b5132
RH
13487 }
13488 }
13489
13490 switch (c)
13491 {
13492 case 'x':
13493 case 'y':
13494 case 'z':
13495 case 'v':
13496 case 'w':
13497 case 'Z':
13498 regno = mips32_to_16_reg_map[regno];
13499 break;
13500
13501 case '0':
13502 if (regno != 0)
13503 regno = ILLEGAL_REG;
13504 break;
13505
13506 case 'S':
13507 if (regno != SP)
13508 regno = ILLEGAL_REG;
13509 break;
13510
13511 case 'R':
13512 if (regno != RA)
13513 regno = ILLEGAL_REG;
13514 break;
13515
13516 case 'X':
13517 case 'Y':
741fe287
MR
13518 if (regno == AT && mips_opts.at)
13519 {
13520 if (mips_opts.at == ATREG)
13521 as_warn (_("used $at without \".set noat\""));
13522 else
13523 as_warn (_("used $%u with \".set at=$%u\""),
13524 regno, mips_opts.at);
13525 }
252b5132
RH
13526 break;
13527
13528 default:
13529 internalError ();
13530 }
13531
13532 if (regno == ILLEGAL_REG)
13533 break;
13534
13535 switch (c)
13536 {
13537 case 'x':
13538 case 'v':
bf12938e 13539 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
13540 break;
13541 case 'y':
13542 case 'w':
bf12938e 13543 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
13544 break;
13545 case 'z':
bf12938e 13546 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
13547 break;
13548 case 'Z':
bf12938e 13549 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
13550 case '0':
13551 case 'S':
13552 case 'R':
13553 break;
13554 case 'X':
bf12938e 13555 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
13556 break;
13557 case 'Y':
13558 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 13559 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
13560 break;
13561 default:
13562 internalError ();
13563 }
13564
13565 lastregno = regno;
13566 continue;
13567
13568 case 'P':
13569 if (strncmp (s, "$pc", 3) == 0)
13570 {
13571 s += 3;
13572 continue;
13573 }
13574 break;
13575
252b5132
RH
13576 case '5':
13577 case 'H':
13578 case 'W':
13579 case 'D':
13580 case 'j':
252b5132
RH
13581 case 'V':
13582 case 'C':
13583 case 'U':
13584 case 'k':
13585 case 'K':
d6f16593
MR
13586 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
13587 if (i > 0)
252b5132 13588 {
d6f16593 13589 if (imm_expr.X_op != O_constant)
252b5132 13590 {
df58fc94 13591 forced_insn_length = 4;
b34976b6 13592 ip->use_extend = TRUE;
252b5132 13593 ip->extend = 0;
252b5132 13594 }
d6f16593
MR
13595 else
13596 {
13597 /* We need to relax this instruction. */
13598 *offset_reloc = *imm_reloc;
13599 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13600 }
13601 s = expr_end;
13602 continue;
252b5132 13603 }
d6f16593
MR
13604 *imm_reloc = BFD_RELOC_UNUSED;
13605 /* Fall through. */
13606 case '<':
13607 case '>':
13608 case '[':
13609 case ']':
13610 case '4':
13611 case '8':
13612 my_getExpression (&imm_expr, s);
252b5132
RH
13613 if (imm_expr.X_op == O_register)
13614 {
13615 /* What we thought was an expression turned out to
13616 be a register. */
13617
13618 if (s[0] == '(' && args[1] == '(')
13619 {
13620 /* It looks like the expression was omitted
13621 before a register indirection, which means
13622 that the expression is implicitly zero. We
13623 still set up imm_expr, so that we handle
13624 explicit extensions correctly. */
13625 imm_expr.X_op = O_constant;
13626 imm_expr.X_add_number = 0;
f6688943 13627 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13628 continue;
13629 }
13630
13631 break;
13632 }
13633
13634 /* We need to relax this instruction. */
f6688943 13635 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13636 s = expr_end;
13637 continue;
13638
13639 case 'p':
13640 case 'q':
13641 case 'A':
13642 case 'B':
13643 case 'E':
13644 /* We use offset_reloc rather than imm_reloc for the PC
13645 relative operands. This lets macros with both
13646 immediate and address operands work correctly. */
13647 my_getExpression (&offset_expr, s);
13648
13649 if (offset_expr.X_op == O_register)
13650 break;
13651
13652 /* We need to relax this instruction. */
f6688943 13653 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13654 s = expr_end;
13655 continue;
13656
13657 case '6': /* break code */
13658 my_getExpression (&imm_expr, s);
13659 check_absolute_expr (ip, &imm_expr);
13660 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
13661 as_warn (_("Invalid value for `%s' (%lu)"),
13662 ip->insn_mo->name,
13663 (unsigned long) imm_expr.X_add_number);
13664 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
13665 imm_expr.X_op = O_absent;
13666 s = expr_end;
13667 continue;
13668
13669 case 'a': /* 26 bit address */
13670 my_getExpression (&offset_expr, s);
13671 s = expr_end;
f6688943 13672 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
13673 ip->insn_opcode <<= 16;
13674 continue;
13675
13676 case 'l': /* register list for entry macro */
13677 case 'L': /* register list for exit macro */
13678 {
13679 int mask;
13680
13681 if (c == 'l')
13682 mask = 0;
13683 else
13684 mask = 7 << 3;
13685 while (*s != '\0')
13686 {
707bfff6 13687 unsigned int freg, reg1, reg2;
252b5132
RH
13688
13689 while (*s == ' ' || *s == ',')
13690 ++s;
707bfff6 13691 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
252b5132 13692 freg = 0;
707bfff6
TS
13693 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
13694 freg = 1;
252b5132
RH
13695 else
13696 {
707bfff6
TS
13697 as_bad (_("can't parse register list"));
13698 break;
252b5132
RH
13699 }
13700 if (*s == ' ')
13701 ++s;
13702 if (*s != '-')
13703 reg2 = reg1;
13704 else
13705 {
13706 ++s;
707bfff6
TS
13707 if (!reg_lookup (&s, freg ? RTYPE_FPU
13708 : (RTYPE_GP | RTYPE_NUM), &reg2))
252b5132 13709 {
707bfff6
TS
13710 as_bad (_("invalid register list"));
13711 break;
252b5132
RH
13712 }
13713 }
13714 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
13715 {
13716 mask &= ~ (7 << 3);
13717 mask |= 5 << 3;
13718 }
13719 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
13720 {
13721 mask &= ~ (7 << 3);
13722 mask |= 6 << 3;
13723 }
13724 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
13725 mask |= (reg2 - 3) << 3;
13726 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
13727 mask |= (reg2 - 15) << 1;
f9419b05 13728 else if (reg1 == RA && reg2 == RA)
252b5132
RH
13729 mask |= 1;
13730 else
13731 {
13732 as_bad (_("invalid register list"));
13733 break;
13734 }
13735 }
13736 /* The mask is filled in in the opcode table for the
13737 benefit of the disassembler. We remove it before
13738 applying the actual mask. */
13739 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
13740 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
13741 }
13742 continue;
13743
0499d65b
TS
13744 case 'm': /* Register list for save insn. */
13745 case 'M': /* Register list for restore insn. */
13746 {
13747 int opcode = 0;
13748 int framesz = 0, seen_framesz = 0;
91d6fa6a 13749 int nargs = 0, statics = 0, sregs = 0;
0499d65b
TS
13750
13751 while (*s != '\0')
13752 {
13753 unsigned int reg1, reg2;
13754
13755 SKIP_SPACE_TABS (s);
13756 while (*s == ',')
13757 ++s;
13758 SKIP_SPACE_TABS (s);
13759
13760 my_getExpression (&imm_expr, s);
13761 if (imm_expr.X_op == O_constant)
13762 {
13763 /* Handle the frame size. */
13764 if (seen_framesz)
13765 {
13766 as_bad (_("more than one frame size in list"));
13767 break;
13768 }
13769 seen_framesz = 1;
13770 framesz = imm_expr.X_add_number;
13771 imm_expr.X_op = O_absent;
13772 s = expr_end;
13773 continue;
13774 }
13775
707bfff6 13776 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
0499d65b
TS
13777 {
13778 as_bad (_("can't parse register list"));
13779 break;
13780 }
0499d65b 13781
707bfff6
TS
13782 while (*s == ' ')
13783 ++s;
13784
0499d65b
TS
13785 if (*s != '-')
13786 reg2 = reg1;
13787 else
13788 {
13789 ++s;
707bfff6
TS
13790 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
13791 || reg2 < reg1)
0499d65b
TS
13792 {
13793 as_bad (_("can't parse register list"));
13794 break;
13795 }
0499d65b
TS
13796 }
13797
13798 while (reg1 <= reg2)
13799 {
13800 if (reg1 >= 4 && reg1 <= 7)
13801 {
3a93f742 13802 if (!seen_framesz)
0499d65b 13803 /* args $a0-$a3 */
91d6fa6a 13804 nargs |= 1 << (reg1 - 4);
0499d65b
TS
13805 else
13806 /* statics $a0-$a3 */
13807 statics |= 1 << (reg1 - 4);
13808 }
13809 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
13810 {
13811 /* $s0-$s8 */
13812 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
13813 }
13814 else if (reg1 == 31)
13815 {
13816 /* Add $ra to insn. */
13817 opcode |= 0x40;
13818 }
13819 else
13820 {
13821 as_bad (_("unexpected register in list"));
13822 break;
13823 }
13824 if (++reg1 == 24)
13825 reg1 = 30;
13826 }
13827 }
13828
13829 /* Encode args/statics combination. */
91d6fa6a 13830 if (nargs & statics)
0499d65b 13831 as_bad (_("arg/static registers overlap"));
91d6fa6a 13832 else if (nargs == 0xf)
0499d65b
TS
13833 /* All $a0-$a3 are args. */
13834 opcode |= MIPS16_ALL_ARGS << 16;
13835 else if (statics == 0xf)
13836 /* All $a0-$a3 are statics. */
13837 opcode |= MIPS16_ALL_STATICS << 16;
13838 else
13839 {
13840 int narg = 0, nstat = 0;
13841
13842 /* Count arg registers. */
91d6fa6a 13843 while (nargs & 0x1)
0499d65b 13844 {
91d6fa6a 13845 nargs >>= 1;
0499d65b
TS
13846 narg++;
13847 }
91d6fa6a 13848 if (nargs != 0)
0499d65b
TS
13849 as_bad (_("invalid arg register list"));
13850
13851 /* Count static registers. */
13852 while (statics & 0x8)
13853 {
13854 statics = (statics << 1) & 0xf;
13855 nstat++;
13856 }
13857 if (statics != 0)
13858 as_bad (_("invalid static register list"));
13859
13860 /* Encode args/statics. */
13861 opcode |= ((narg << 2) | nstat) << 16;
13862 }
13863
13864 /* Encode $s0/$s1. */
13865 if (sregs & (1 << 0)) /* $s0 */
13866 opcode |= 0x20;
13867 if (sregs & (1 << 1)) /* $s1 */
13868 opcode |= 0x10;
13869 sregs >>= 2;
13870
13871 if (sregs != 0)
13872 {
13873 /* Count regs $s2-$s8. */
13874 int nsreg = 0;
13875 while (sregs & 1)
13876 {
13877 sregs >>= 1;
13878 nsreg++;
13879 }
13880 if (sregs != 0)
13881 as_bad (_("invalid static register list"));
13882 /* Encode $s2-$s8. */
13883 opcode |= nsreg << 24;
13884 }
13885
13886 /* Encode frame size. */
13887 if (!seen_framesz)
13888 as_bad (_("missing frame size"));
13889 else if ((framesz & 7) != 0 || framesz < 0
13890 || framesz > 0xff * 8)
13891 as_bad (_("invalid frame size"));
13892 else if (framesz != 128 || (opcode >> 16) != 0)
13893 {
13894 framesz /= 8;
13895 opcode |= (((framesz & 0xf0) << 16)
13896 | (framesz & 0x0f));
13897 }
13898
13899 /* Finally build the instruction. */
13900 if ((opcode >> 16) != 0 || framesz == 0)
13901 {
13902 ip->use_extend = TRUE;
13903 ip->extend = opcode >> 16;
13904 }
13905 ip->insn_opcode |= opcode & 0x7f;
13906 }
13907 continue;
13908
252b5132
RH
13909 case 'e': /* extend code */
13910 my_getExpression (&imm_expr, s);
13911 check_absolute_expr (ip, &imm_expr);
13912 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
13913 {
13914 as_warn (_("Invalid value for `%s' (%lu)"),
13915 ip->insn_mo->name,
13916 (unsigned long) imm_expr.X_add_number);
13917 imm_expr.X_add_number &= 0x7ff;
13918 }
13919 ip->insn_opcode |= imm_expr.X_add_number;
13920 imm_expr.X_op = O_absent;
13921 s = expr_end;
13922 continue;
13923
13924 default:
13925 internalError ();
13926 }
13927 break;
13928 }
13929
13930 /* Args don't match. */
13931 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
13932 strcmp (insn->name, insn[1].name) == 0)
13933 {
13934 ++insn;
13935 s = argsstart;
13936 continue;
13937 }
13938
13939 insn_error = _("illegal operands");
13940
13941 return;
13942 }
13943}
13944
13945/* This structure holds information we know about a mips16 immediate
13946 argument type. */
13947
e972090a
NC
13948struct mips16_immed_operand
13949{
252b5132
RH
13950 /* The type code used in the argument string in the opcode table. */
13951 int type;
13952 /* The number of bits in the short form of the opcode. */
13953 int nbits;
13954 /* The number of bits in the extended form of the opcode. */
13955 int extbits;
13956 /* The amount by which the short form is shifted when it is used;
13957 for example, the sw instruction has a shift count of 2. */
13958 int shift;
13959 /* The amount by which the short form is shifted when it is stored
13960 into the instruction code. */
13961 int op_shift;
13962 /* Non-zero if the short form is unsigned. */
13963 int unsp;
13964 /* Non-zero if the extended form is unsigned. */
13965 int extu;
13966 /* Non-zero if the value is PC relative. */
13967 int pcrel;
13968};
13969
13970/* The mips16 immediate operand types. */
13971
13972static const struct mips16_immed_operand mips16_immed_operands[] =
13973{
13974 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
13975 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
13976 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
13977 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
13978 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
13979 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
13980 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
13981 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
13982 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
13983 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
13984 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
13985 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
13986 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
13987 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
13988 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
13989 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
13990 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
13991 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
13992 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
13993 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
13994 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
13995};
13996
13997#define MIPS16_NUM_IMMED \
13998 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
13999
14000/* Handle a mips16 instruction with an immediate value. This or's the
14001 small immediate value into *INSN. It sets *USE_EXTEND to indicate
14002 whether an extended value is needed; if one is needed, it sets
14003 *EXTEND to the value. The argument type is TYPE. The value is VAL.
14004 If SMALL is true, an unextended opcode was explicitly requested.
14005 If EXT is true, an extended opcode was explicitly requested. If
14006 WARN is true, warn if EXT does not match reality. */
14007
14008static void
17a2f251
TS
14009mips16_immed (char *file, unsigned int line, int type, offsetT val,
14010 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
14011 unsigned long *insn, bfd_boolean *use_extend,
14012 unsigned short *extend)
252b5132 14013{
3994f87e 14014 const struct mips16_immed_operand *op;
252b5132 14015 int mintiny, maxtiny;
b34976b6 14016 bfd_boolean needext;
252b5132
RH
14017
14018 op = mips16_immed_operands;
14019 while (op->type != type)
14020 {
14021 ++op;
9c2799c2 14022 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
14023 }
14024
14025 if (op->unsp)
14026 {
14027 if (type == '<' || type == '>' || type == '[' || type == ']')
14028 {
14029 mintiny = 1;
14030 maxtiny = 1 << op->nbits;
14031 }
14032 else
14033 {
14034 mintiny = 0;
14035 maxtiny = (1 << op->nbits) - 1;
14036 }
14037 }
14038 else
14039 {
14040 mintiny = - (1 << (op->nbits - 1));
14041 maxtiny = (1 << (op->nbits - 1)) - 1;
14042 }
14043
14044 /* Branch offsets have an implicit 0 in the lowest bit. */
14045 if (type == 'p' || type == 'q')
14046 val /= 2;
14047
14048 if ((val & ((1 << op->shift) - 1)) != 0
14049 || val < (mintiny << op->shift)
14050 || val > (maxtiny << op->shift))
b34976b6 14051 needext = TRUE;
252b5132 14052 else
b34976b6 14053 needext = FALSE;
252b5132
RH
14054
14055 if (warn && ext && ! needext)
beae10d5
KH
14056 as_warn_where (file, line,
14057 _("extended operand requested but not required"));
252b5132
RH
14058 if (small && needext)
14059 as_bad_where (file, line, _("invalid unextended operand value"));
14060
14061 if (small || (! ext && ! needext))
14062 {
14063 int insnval;
14064
b34976b6 14065 *use_extend = FALSE;
252b5132
RH
14066 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14067 insnval <<= op->op_shift;
14068 *insn |= insnval;
14069 }
14070 else
14071 {
14072 long minext, maxext;
14073 int extval;
14074
14075 if (op->extu)
14076 {
14077 minext = 0;
14078 maxext = (1 << op->extbits) - 1;
14079 }
14080 else
14081 {
14082 minext = - (1 << (op->extbits - 1));
14083 maxext = (1 << (op->extbits - 1)) - 1;
14084 }
14085 if (val < minext || val > maxext)
14086 as_bad_where (file, line,
14087 _("operand value out of range for instruction"));
14088
b34976b6 14089 *use_extend = TRUE;
252b5132
RH
14090 if (op->extbits == 16)
14091 {
14092 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14093 val &= 0x1f;
14094 }
14095 else if (op->extbits == 15)
14096 {
14097 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14098 val &= 0xf;
14099 }
14100 else
14101 {
14102 extval = ((val & 0x1f) << 6) | (val & 0x20);
14103 val = 0;
14104 }
14105
14106 *extend = (unsigned short) extval;
14107 *insn |= val;
14108 }
14109}
14110\f
d6f16593 14111struct percent_op_match
ad8d3bb3 14112{
5e0116d5
RS
14113 const char *str;
14114 bfd_reloc_code_real_type reloc;
d6f16593
MR
14115};
14116
14117static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14118{
5e0116d5 14119 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 14120#ifdef OBJ_ELF
5e0116d5
RS
14121 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14122 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14123 {"%call16", BFD_RELOC_MIPS_CALL16},
14124 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14125 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14126 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14127 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14128 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14129 {"%got", BFD_RELOC_MIPS_GOT16},
14130 {"%gp_rel", BFD_RELOC_GPREL16},
14131 {"%half", BFD_RELOC_16},
14132 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14133 {"%higher", BFD_RELOC_MIPS_HIGHER},
14134 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14135 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14136 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14137 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14138 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14139 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14140 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14141 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
ad8d3bb3 14142#endif
5e0116d5 14143 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
14144};
14145
d6f16593
MR
14146static const struct percent_op_match mips16_percent_op[] =
14147{
14148 {"%lo", BFD_RELOC_MIPS16_LO16},
14149 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14150 {"%got", BFD_RELOC_MIPS16_GOT16},
14151 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14152 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14153 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14154 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14155 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14156 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14157 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14158 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14159 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14160};
14161
252b5132 14162
5e0116d5
RS
14163/* Return true if *STR points to a relocation operator. When returning true,
14164 move *STR over the operator and store its relocation code in *RELOC.
14165 Leave both *STR and *RELOC alone when returning false. */
14166
14167static bfd_boolean
17a2f251 14168parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14169{
d6f16593
MR
14170 const struct percent_op_match *percent_op;
14171 size_t limit, i;
14172
14173 if (mips_opts.mips16)
14174 {
14175 percent_op = mips16_percent_op;
14176 limit = ARRAY_SIZE (mips16_percent_op);
14177 }
14178 else
14179 {
14180 percent_op = mips_percent_op;
14181 limit = ARRAY_SIZE (mips_percent_op);
14182 }
76b3015f 14183
d6f16593 14184 for (i = 0; i < limit; i++)
5e0116d5 14185 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14186 {
3f98094e
DJ
14187 int len = strlen (percent_op[i].str);
14188
14189 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14190 continue;
14191
5e0116d5
RS
14192 *str += strlen (percent_op[i].str);
14193 *reloc = percent_op[i].reloc;
394f9b3a 14194
5e0116d5
RS
14195 /* Check whether the output BFD supports this relocation.
14196 If not, issue an error and fall back on something safe. */
14197 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14198 {
20203fb9 14199 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14200 percent_op[i].str);
01a3f561 14201 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14202 }
5e0116d5 14203 return TRUE;
394f9b3a 14204 }
5e0116d5 14205 return FALSE;
394f9b3a 14206}
ad8d3bb3 14207
ad8d3bb3 14208
5e0116d5
RS
14209/* Parse string STR as a 16-bit relocatable operand. Store the
14210 expression in *EP and the relocations in the array starting
14211 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14212
01a3f561 14213 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14214
5e0116d5 14215static size_t
17a2f251
TS
14216my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14217 char *str)
ad8d3bb3 14218{
5e0116d5
RS
14219 bfd_reloc_code_real_type reversed_reloc[3];
14220 size_t reloc_index, i;
09b8f35a
RS
14221 int crux_depth, str_depth;
14222 char *crux;
5e0116d5
RS
14223
14224 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14225 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14226 of the main expression and with CRUX_DEPTH containing the number
14227 of open brackets at that point. */
14228 reloc_index = -1;
14229 str_depth = 0;
14230 do
fb1b3232 14231 {
09b8f35a
RS
14232 reloc_index++;
14233 crux = str;
14234 crux_depth = str_depth;
14235
14236 /* Skip over whitespace and brackets, keeping count of the number
14237 of brackets. */
14238 while (*str == ' ' || *str == '\t' || *str == '(')
14239 if (*str++ == '(')
14240 str_depth++;
5e0116d5 14241 }
09b8f35a
RS
14242 while (*str == '%'
14243 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14244 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14245
09b8f35a 14246 my_getExpression (ep, crux);
5e0116d5 14247 str = expr_end;
394f9b3a 14248
5e0116d5 14249 /* Match every open bracket. */
09b8f35a 14250 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14251 if (*str++ == ')')
09b8f35a 14252 crux_depth--;
394f9b3a 14253
09b8f35a 14254 if (crux_depth > 0)
20203fb9 14255 as_bad (_("unclosed '('"));
394f9b3a 14256
5e0116d5 14257 expr_end = str;
252b5132 14258
01a3f561 14259 if (reloc_index != 0)
64bdfcaf
RS
14260 {
14261 prev_reloc_op_frag = frag_now;
14262 for (i = 0; i < reloc_index; i++)
14263 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14264 }
fb1b3232 14265
5e0116d5 14266 return reloc_index;
252b5132
RH
14267}
14268
14269static void
17a2f251 14270my_getExpression (expressionS *ep, char *str)
252b5132
RH
14271{
14272 char *save_in;
14273
14274 save_in = input_line_pointer;
14275 input_line_pointer = str;
14276 expression (ep);
14277 expr_end = input_line_pointer;
14278 input_line_pointer = save_in;
252b5132
RH
14279}
14280
252b5132 14281char *
17a2f251 14282md_atof (int type, char *litP, int *sizeP)
252b5132 14283{
499ac353 14284 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14285}
14286
14287void
17a2f251 14288md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14289{
14290 if (target_big_endian)
14291 number_to_chars_bigendian (buf, val, n);
14292 else
14293 number_to_chars_littleendian (buf, val, n);
14294}
14295\f
ae948b86 14296#ifdef OBJ_ELF
e013f690
TS
14297static int support_64bit_objects(void)
14298{
14299 const char **list, **l;
aa3d8fdf 14300 int yes;
e013f690
TS
14301
14302 list = bfd_target_list ();
14303 for (l = list; *l != NULL; l++)
aeffff67
RS
14304 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14305 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14306 break;
aa3d8fdf 14307 yes = (*l != NULL);
e013f690 14308 free (list);
aa3d8fdf 14309 return yes;
e013f690 14310}
ae948b86 14311#endif /* OBJ_ELF */
e013f690 14312
78849248 14313const char *md_shortopts = "O::g::G:";
252b5132 14314
23fce1e3
NC
14315enum options
14316 {
14317 OPTION_MARCH = OPTION_MD_BASE,
14318 OPTION_MTUNE,
14319 OPTION_MIPS1,
14320 OPTION_MIPS2,
14321 OPTION_MIPS3,
14322 OPTION_MIPS4,
14323 OPTION_MIPS5,
14324 OPTION_MIPS32,
14325 OPTION_MIPS64,
14326 OPTION_MIPS32R2,
14327 OPTION_MIPS64R2,
14328 OPTION_MIPS16,
14329 OPTION_NO_MIPS16,
14330 OPTION_MIPS3D,
14331 OPTION_NO_MIPS3D,
14332 OPTION_MDMX,
14333 OPTION_NO_MDMX,
14334 OPTION_DSP,
14335 OPTION_NO_DSP,
14336 OPTION_MT,
14337 OPTION_NO_MT,
14338 OPTION_SMARTMIPS,
14339 OPTION_NO_SMARTMIPS,
14340 OPTION_DSPR2,
14341 OPTION_NO_DSPR2,
df58fc94
RS
14342 OPTION_MICROMIPS,
14343 OPTION_NO_MICROMIPS,
dec0624d
MR
14344 OPTION_MCU,
14345 OPTION_NO_MCU,
23fce1e3
NC
14346 OPTION_COMPAT_ARCH_BASE,
14347 OPTION_M4650,
14348 OPTION_NO_M4650,
14349 OPTION_M4010,
14350 OPTION_NO_M4010,
14351 OPTION_M4100,
14352 OPTION_NO_M4100,
14353 OPTION_M3900,
14354 OPTION_NO_M3900,
14355 OPTION_M7000_HILO_FIX,
6a32d874
CM
14356 OPTION_MNO_7000_HILO_FIX,
14357 OPTION_FIX_24K,
14358 OPTION_NO_FIX_24K,
c67a084a
NC
14359 OPTION_FIX_LOONGSON2F_JUMP,
14360 OPTION_NO_FIX_LOONGSON2F_JUMP,
14361 OPTION_FIX_LOONGSON2F_NOP,
14362 OPTION_NO_FIX_LOONGSON2F_NOP,
23fce1e3
NC
14363 OPTION_FIX_VR4120,
14364 OPTION_NO_FIX_VR4120,
14365 OPTION_FIX_VR4130,
14366 OPTION_NO_FIX_VR4130,
d954098f
DD
14367 OPTION_FIX_CN63XXP1,
14368 OPTION_NO_FIX_CN63XXP1,
23fce1e3
NC
14369 OPTION_TRAP,
14370 OPTION_BREAK,
14371 OPTION_EB,
14372 OPTION_EL,
14373 OPTION_FP32,
14374 OPTION_GP32,
14375 OPTION_CONSTRUCT_FLOATS,
14376 OPTION_NO_CONSTRUCT_FLOATS,
14377 OPTION_FP64,
14378 OPTION_GP64,
14379 OPTION_RELAX_BRANCH,
14380 OPTION_NO_RELAX_BRANCH,
14381 OPTION_MSHARED,
14382 OPTION_MNO_SHARED,
14383 OPTION_MSYM32,
14384 OPTION_MNO_SYM32,
14385 OPTION_SOFT_FLOAT,
14386 OPTION_HARD_FLOAT,
14387 OPTION_SINGLE_FLOAT,
14388 OPTION_DOUBLE_FLOAT,
14389 OPTION_32,
14390#ifdef OBJ_ELF
14391 OPTION_CALL_SHARED,
14392 OPTION_CALL_NONPIC,
14393 OPTION_NON_SHARED,
14394 OPTION_XGOT,
14395 OPTION_MABI,
14396 OPTION_N32,
14397 OPTION_64,
14398 OPTION_MDEBUG,
14399 OPTION_NO_MDEBUG,
14400 OPTION_PDR,
14401 OPTION_NO_PDR,
14402 OPTION_MVXWORKS_PIC,
14403#endif /* OBJ_ELF */
14404 OPTION_END_OF_ENUM
14405 };
14406
e972090a
NC
14407struct option md_longopts[] =
14408{
f9b4148d 14409 /* Options which specify architecture. */
f9b4148d 14410 {"march", required_argument, NULL, OPTION_MARCH},
f9b4148d 14411 {"mtune", required_argument, NULL, OPTION_MTUNE},
252b5132
RH
14412 {"mips0", no_argument, NULL, OPTION_MIPS1},
14413 {"mips1", no_argument, NULL, OPTION_MIPS1},
252b5132 14414 {"mips2", no_argument, NULL, OPTION_MIPS2},
252b5132 14415 {"mips3", no_argument, NULL, OPTION_MIPS3},
252b5132 14416 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86 14417 {"mips5", no_argument, NULL, OPTION_MIPS5},
ae948b86 14418 {"mips32", no_argument, NULL, OPTION_MIPS32},
ae948b86 14419 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d 14420 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13 14421 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
14422
14423 /* Options which specify Application Specific Extensions (ASEs). */
f9b4148d 14424 {"mips16", no_argument, NULL, OPTION_MIPS16},
f9b4148d 14425 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
f9b4148d 14426 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
f9b4148d 14427 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
f9b4148d 14428 {"mdmx", no_argument, NULL, OPTION_MDMX},
f9b4148d 14429 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
74cd071d 14430 {"mdsp", no_argument, NULL, OPTION_DSP},
74cd071d 14431 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
ef2e4d86 14432 {"mmt", no_argument, NULL, OPTION_MT},
ef2e4d86 14433 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
e16bfa71 14434 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
e16bfa71 14435 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
8b082fb1 14436 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
8b082fb1 14437 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
df58fc94
RS
14438 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
14439 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
dec0624d
MR
14440 {"mmcu", no_argument, NULL, OPTION_MCU},
14441 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
f9b4148d
CD
14442
14443 /* Old-style architecture options. Don't add more of these. */
f9b4148d 14444 {"m4650", no_argument, NULL, OPTION_M4650},
f9b4148d 14445 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
f9b4148d 14446 {"m4010", no_argument, NULL, OPTION_M4010},
f9b4148d 14447 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
f9b4148d 14448 {"m4100", no_argument, NULL, OPTION_M4100},
f9b4148d 14449 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
f9b4148d 14450 {"m3900", no_argument, NULL, OPTION_M3900},
f9b4148d
CD
14451 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
14452
14453 /* Options which enable bug fixes. */
f9b4148d 14454 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
f9b4148d
CD
14455 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14456 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
c67a084a
NC
14457 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
14458 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
14459 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
14460 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
d766e8ec
RS
14461 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
14462 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
7d8e00cf
RS
14463 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
14464 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
6a32d874
CM
14465 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
14466 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
d954098f
DD
14467 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
14468 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
f9b4148d
CD
14469
14470 /* Miscellaneous options. */
252b5132
RH
14471 {"trap", no_argument, NULL, OPTION_TRAP},
14472 {"no-break", no_argument, NULL, OPTION_TRAP},
252b5132
RH
14473 {"break", no_argument, NULL, OPTION_BREAK},
14474 {"no-trap", no_argument, NULL, OPTION_BREAK},
252b5132 14475 {"EB", no_argument, NULL, OPTION_EB},
252b5132 14476 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 14477 {"mfp32", no_argument, NULL, OPTION_FP32},
c97ef257 14478 {"mgp32", no_argument, NULL, OPTION_GP32},
119d663a 14479 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
119d663a 14480 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
316f5878 14481 {"mfp64", no_argument, NULL, OPTION_FP64},
ae948b86 14482 {"mgp64", no_argument, NULL, OPTION_GP64},
4a6a3df4
AO
14483 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
14484 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
aa6975fb
ILT
14485 {"mshared", no_argument, NULL, OPTION_MSHARED},
14486 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
aed1a261
RS
14487 {"msym32", no_argument, NULL, OPTION_MSYM32},
14488 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
037b32b9
AN
14489 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
14490 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
037b32b9
AN
14491 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
14492 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
23fce1e3
NC
14493
14494 /* Strictly speaking this next option is ELF specific,
14495 but we allow it for other ports as well in order to
14496 make testing easier. */
14497 {"32", no_argument, NULL, OPTION_32},
037b32b9 14498
f9b4148d 14499 /* ELF-specific options. */
156c2f8b 14500#ifdef OBJ_ELF
156c2f8b
NC
14501 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
14502 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
861fb55a 14503 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
156c2f8b
NC
14504 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
14505 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86 14506 {"mabi", required_argument, NULL, OPTION_MABI},
e013f690 14507 {"n32", no_argument, NULL, OPTION_N32},
156c2f8b 14508 {"64", no_argument, NULL, OPTION_64},
ecb4347a 14509 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
ecb4347a 14510 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe 14511 {"mpdr", no_argument, NULL, OPTION_PDR},
dcd410fe 14512 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
0a44bf69 14513 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ae948b86 14514#endif /* OBJ_ELF */
f9b4148d 14515
252b5132
RH
14516 {NULL, no_argument, NULL, 0}
14517};
156c2f8b 14518size_t md_longopts_size = sizeof (md_longopts);
252b5132 14519
316f5878
RS
14520/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14521 NEW_VALUE. Warn if another value was already specified. Note:
14522 we have to defer parsing the -march and -mtune arguments in order
14523 to handle 'from-abi' correctly, since the ABI might be specified
14524 in a later argument. */
14525
14526static void
17a2f251 14527mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14528{
14529 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14530 as_warn (_("A different %s was already specified, is now %s"),
14531 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14532 new_value);
14533
14534 *string_ptr = new_value;
14535}
14536
252b5132 14537int
17a2f251 14538md_parse_option (int c, char *arg)
252b5132
RH
14539{
14540 switch (c)
14541 {
119d663a
NC
14542 case OPTION_CONSTRUCT_FLOATS:
14543 mips_disable_float_construction = 0;
14544 break;
bdaaa2e1 14545
119d663a
NC
14546 case OPTION_NO_CONSTRUCT_FLOATS:
14547 mips_disable_float_construction = 1;
14548 break;
bdaaa2e1 14549
252b5132
RH
14550 case OPTION_TRAP:
14551 mips_trap = 1;
14552 break;
14553
14554 case OPTION_BREAK:
14555 mips_trap = 0;
14556 break;
14557
14558 case OPTION_EB:
14559 target_big_endian = 1;
14560 break;
14561
14562 case OPTION_EL:
14563 target_big_endian = 0;
14564 break;
14565
14566 case 'O':
4ffff32f
TS
14567 if (arg == NULL)
14568 mips_optimize = 1;
14569 else if (arg[0] == '0')
14570 mips_optimize = 0;
14571 else if (arg[0] == '1')
252b5132
RH
14572 mips_optimize = 1;
14573 else
14574 mips_optimize = 2;
14575 break;
14576
14577 case 'g':
14578 if (arg == NULL)
14579 mips_debug = 2;
14580 else
14581 mips_debug = atoi (arg);
252b5132
RH
14582 break;
14583
14584 case OPTION_MIPS1:
316f5878 14585 file_mips_isa = ISA_MIPS1;
252b5132
RH
14586 break;
14587
14588 case OPTION_MIPS2:
316f5878 14589 file_mips_isa = ISA_MIPS2;
252b5132
RH
14590 break;
14591
14592 case OPTION_MIPS3:
316f5878 14593 file_mips_isa = ISA_MIPS3;
252b5132
RH
14594 break;
14595
14596 case OPTION_MIPS4:
316f5878 14597 file_mips_isa = ISA_MIPS4;
e7af610e
NC
14598 break;
14599
84ea6cf2 14600 case OPTION_MIPS5:
316f5878 14601 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
14602 break;
14603
e7af610e 14604 case OPTION_MIPS32:
316f5878 14605 file_mips_isa = ISA_MIPS32;
252b5132
RH
14606 break;
14607
af7ee8bf
CD
14608 case OPTION_MIPS32R2:
14609 file_mips_isa = ISA_MIPS32R2;
14610 break;
14611
5f74bc13
CD
14612 case OPTION_MIPS64R2:
14613 file_mips_isa = ISA_MIPS64R2;
14614 break;
14615
84ea6cf2 14616 case OPTION_MIPS64:
316f5878 14617 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
14618 break;
14619
ec68c924 14620 case OPTION_MTUNE:
316f5878
RS
14621 mips_set_option_string (&mips_tune_string, arg);
14622 break;
ec68c924 14623
316f5878
RS
14624 case OPTION_MARCH:
14625 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14626 break;
14627
14628 case OPTION_M4650:
316f5878
RS
14629 mips_set_option_string (&mips_arch_string, "4650");
14630 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14631 break;
14632
14633 case OPTION_NO_M4650:
14634 break;
14635
14636 case OPTION_M4010:
316f5878
RS
14637 mips_set_option_string (&mips_arch_string, "4010");
14638 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14639 break;
14640
14641 case OPTION_NO_M4010:
14642 break;
14643
14644 case OPTION_M4100:
316f5878
RS
14645 mips_set_option_string (&mips_arch_string, "4100");
14646 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14647 break;
14648
14649 case OPTION_NO_M4100:
14650 break;
14651
252b5132 14652 case OPTION_M3900:
316f5878
RS
14653 mips_set_option_string (&mips_arch_string, "3900");
14654 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14655 break;
bdaaa2e1 14656
252b5132
RH
14657 case OPTION_NO_M3900:
14658 break;
14659
deec1734
CD
14660 case OPTION_MDMX:
14661 mips_opts.ase_mdmx = 1;
14662 break;
14663
14664 case OPTION_NO_MDMX:
14665 mips_opts.ase_mdmx = 0;
14666 break;
14667
74cd071d
CF
14668 case OPTION_DSP:
14669 mips_opts.ase_dsp = 1;
8b082fb1 14670 mips_opts.ase_dspr2 = 0;
74cd071d
CF
14671 break;
14672
14673 case OPTION_NO_DSP:
8b082fb1
TS
14674 mips_opts.ase_dsp = 0;
14675 mips_opts.ase_dspr2 = 0;
14676 break;
14677
14678 case OPTION_DSPR2:
14679 mips_opts.ase_dspr2 = 1;
14680 mips_opts.ase_dsp = 1;
14681 break;
14682
14683 case OPTION_NO_DSPR2:
14684 mips_opts.ase_dspr2 = 0;
74cd071d
CF
14685 mips_opts.ase_dsp = 0;
14686 break;
14687
ef2e4d86
CF
14688 case OPTION_MT:
14689 mips_opts.ase_mt = 1;
14690 break;
14691
14692 case OPTION_NO_MT:
14693 mips_opts.ase_mt = 0;
14694 break;
14695
dec0624d
MR
14696 case OPTION_MCU:
14697 mips_opts.ase_mcu = 1;
14698 break;
14699
14700 case OPTION_NO_MCU:
14701 mips_opts.ase_mcu = 0;
14702 break;
14703
df58fc94
RS
14704 case OPTION_MICROMIPS:
14705 if (mips_opts.mips16 == 1)
14706 {
14707 as_bad (_("-mmicromips cannot be used with -mips16"));
14708 return 0;
14709 }
14710 mips_opts.micromips = 1;
14711 mips_no_prev_insn ();
14712 break;
14713
14714 case OPTION_NO_MICROMIPS:
14715 mips_opts.micromips = 0;
14716 mips_no_prev_insn ();
14717 break;
14718
252b5132 14719 case OPTION_MIPS16:
df58fc94
RS
14720 if (mips_opts.micromips == 1)
14721 {
14722 as_bad (_("-mips16 cannot be used with -micromips"));
14723 return 0;
14724 }
252b5132 14725 mips_opts.mips16 = 1;
7d10b47d 14726 mips_no_prev_insn ();
252b5132
RH
14727 break;
14728
14729 case OPTION_NO_MIPS16:
14730 mips_opts.mips16 = 0;
7d10b47d 14731 mips_no_prev_insn ();
252b5132
RH
14732 break;
14733
1f25f5d3
CD
14734 case OPTION_MIPS3D:
14735 mips_opts.ase_mips3d = 1;
14736 break;
14737
14738 case OPTION_NO_MIPS3D:
14739 mips_opts.ase_mips3d = 0;
14740 break;
14741
e16bfa71
TS
14742 case OPTION_SMARTMIPS:
14743 mips_opts.ase_smartmips = 1;
14744 break;
14745
14746 case OPTION_NO_SMARTMIPS:
14747 mips_opts.ase_smartmips = 0;
14748 break;
14749
6a32d874
CM
14750 case OPTION_FIX_24K:
14751 mips_fix_24k = 1;
14752 break;
14753
14754 case OPTION_NO_FIX_24K:
14755 mips_fix_24k = 0;
14756 break;
14757
c67a084a
NC
14758 case OPTION_FIX_LOONGSON2F_JUMP:
14759 mips_fix_loongson2f_jump = TRUE;
14760 break;
14761
14762 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14763 mips_fix_loongson2f_jump = FALSE;
14764 break;
14765
14766 case OPTION_FIX_LOONGSON2F_NOP:
14767 mips_fix_loongson2f_nop = TRUE;
14768 break;
14769
14770 case OPTION_NO_FIX_LOONGSON2F_NOP:
14771 mips_fix_loongson2f_nop = FALSE;
14772 break;
14773
d766e8ec
RS
14774 case OPTION_FIX_VR4120:
14775 mips_fix_vr4120 = 1;
60b63b72
RS
14776 break;
14777
d766e8ec
RS
14778 case OPTION_NO_FIX_VR4120:
14779 mips_fix_vr4120 = 0;
60b63b72
RS
14780 break;
14781
7d8e00cf
RS
14782 case OPTION_FIX_VR4130:
14783 mips_fix_vr4130 = 1;
14784 break;
14785
14786 case OPTION_NO_FIX_VR4130:
14787 mips_fix_vr4130 = 0;
14788 break;
14789
d954098f
DD
14790 case OPTION_FIX_CN63XXP1:
14791 mips_fix_cn63xxp1 = TRUE;
14792 break;
14793
14794 case OPTION_NO_FIX_CN63XXP1:
14795 mips_fix_cn63xxp1 = FALSE;
14796 break;
14797
4a6a3df4
AO
14798 case OPTION_RELAX_BRANCH:
14799 mips_relax_branch = 1;
14800 break;
14801
14802 case OPTION_NO_RELAX_BRANCH:
14803 mips_relax_branch = 0;
14804 break;
14805
aa6975fb
ILT
14806 case OPTION_MSHARED:
14807 mips_in_shared = TRUE;
14808 break;
14809
14810 case OPTION_MNO_SHARED:
14811 mips_in_shared = FALSE;
14812 break;
14813
aed1a261
RS
14814 case OPTION_MSYM32:
14815 mips_opts.sym32 = TRUE;
14816 break;
14817
14818 case OPTION_MNO_SYM32:
14819 mips_opts.sym32 = FALSE;
14820 break;
14821
0f074f60 14822#ifdef OBJ_ELF
252b5132
RH
14823 /* When generating ELF code, we permit -KPIC and -call_shared to
14824 select SVR4_PIC, and -non_shared to select no PIC. This is
14825 intended to be compatible with Irix 5. */
14826 case OPTION_CALL_SHARED:
f43abd2b 14827 if (!IS_ELF)
252b5132
RH
14828 {
14829 as_bad (_("-call_shared is supported only for ELF format"));
14830 return 0;
14831 }
14832 mips_pic = SVR4_PIC;
143d77c5 14833 mips_abicalls = TRUE;
252b5132
RH
14834 break;
14835
861fb55a
DJ
14836 case OPTION_CALL_NONPIC:
14837 if (!IS_ELF)
14838 {
14839 as_bad (_("-call_nonpic is supported only for ELF format"));
14840 return 0;
14841 }
14842 mips_pic = NO_PIC;
14843 mips_abicalls = TRUE;
14844 break;
14845
252b5132 14846 case OPTION_NON_SHARED:
f43abd2b 14847 if (!IS_ELF)
252b5132
RH
14848 {
14849 as_bad (_("-non_shared is supported only for ELF format"));
14850 return 0;
14851 }
14852 mips_pic = NO_PIC;
143d77c5 14853 mips_abicalls = FALSE;
252b5132
RH
14854 break;
14855
44075ae2
TS
14856 /* The -xgot option tells the assembler to use 32 bit offsets
14857 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
14858 compatibility. */
14859 case OPTION_XGOT:
14860 mips_big_got = 1;
14861 break;
0f074f60 14862#endif /* OBJ_ELF */
252b5132
RH
14863
14864 case 'G':
6caf9ef4
TS
14865 g_switch_value = atoi (arg);
14866 g_switch_seen = 1;
252b5132
RH
14867 break;
14868
34ba82a8
TS
14869 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14870 and -mabi=64. */
252b5132 14871 case OPTION_32:
23fce1e3
NC
14872 if (IS_ELF)
14873 mips_abi = O32_ABI;
14874 /* We silently ignore -32 for non-ELF targets. This greatly
14875 simplifies the construction of the MIPS GAS test cases. */
252b5132
RH
14876 break;
14877
23fce1e3 14878#ifdef OBJ_ELF
e013f690 14879 case OPTION_N32:
f43abd2b 14880 if (!IS_ELF)
34ba82a8
TS
14881 {
14882 as_bad (_("-n32 is supported for ELF format only"));
14883 return 0;
14884 }
316f5878 14885 mips_abi = N32_ABI;
e013f690 14886 break;
252b5132 14887
e013f690 14888 case OPTION_64:
f43abd2b 14889 if (!IS_ELF)
34ba82a8
TS
14890 {
14891 as_bad (_("-64 is supported for ELF format only"));
14892 return 0;
14893 }
316f5878 14894 mips_abi = N64_ABI;
f43abd2b 14895 if (!support_64bit_objects())
e013f690 14896 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 14897 break;
ae948b86 14898#endif /* OBJ_ELF */
252b5132 14899
c97ef257 14900 case OPTION_GP32:
a325df1d 14901 file_mips_gp32 = 1;
c97ef257
AH
14902 break;
14903
14904 case OPTION_GP64:
a325df1d 14905 file_mips_gp32 = 0;
c97ef257 14906 break;
252b5132 14907
ca4e0257 14908 case OPTION_FP32:
a325df1d 14909 file_mips_fp32 = 1;
316f5878
RS
14910 break;
14911
14912 case OPTION_FP64:
14913 file_mips_fp32 = 0;
ca4e0257
RS
14914 break;
14915
037b32b9
AN
14916 case OPTION_SINGLE_FLOAT:
14917 file_mips_single_float = 1;
14918 break;
14919
14920 case OPTION_DOUBLE_FLOAT:
14921 file_mips_single_float = 0;
14922 break;
14923
14924 case OPTION_SOFT_FLOAT:
14925 file_mips_soft_float = 1;
14926 break;
14927
14928 case OPTION_HARD_FLOAT:
14929 file_mips_soft_float = 0;
14930 break;
14931
ae948b86 14932#ifdef OBJ_ELF
252b5132 14933 case OPTION_MABI:
f43abd2b 14934 if (!IS_ELF)
34ba82a8
TS
14935 {
14936 as_bad (_("-mabi is supported for ELF format only"));
14937 return 0;
14938 }
e013f690 14939 if (strcmp (arg, "32") == 0)
316f5878 14940 mips_abi = O32_ABI;
e013f690 14941 else if (strcmp (arg, "o64") == 0)
316f5878 14942 mips_abi = O64_ABI;
e013f690 14943 else if (strcmp (arg, "n32") == 0)
316f5878 14944 mips_abi = N32_ABI;
e013f690
TS
14945 else if (strcmp (arg, "64") == 0)
14946 {
316f5878 14947 mips_abi = N64_ABI;
e013f690
TS
14948 if (! support_64bit_objects())
14949 as_fatal (_("No compiled in support for 64 bit object file "
14950 "format"));
14951 }
14952 else if (strcmp (arg, "eabi") == 0)
316f5878 14953 mips_abi = EABI_ABI;
e013f690 14954 else
da0e507f
TS
14955 {
14956 as_fatal (_("invalid abi -mabi=%s"), arg);
14957 return 0;
14958 }
252b5132 14959 break;
e013f690 14960#endif /* OBJ_ELF */
252b5132 14961
6b76fefe 14962 case OPTION_M7000_HILO_FIX:
b34976b6 14963 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
14964 break;
14965
9ee72ff1 14966 case OPTION_MNO_7000_HILO_FIX:
b34976b6 14967 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
14968 break;
14969
ecb4347a
DJ
14970#ifdef OBJ_ELF
14971 case OPTION_MDEBUG:
b34976b6 14972 mips_flag_mdebug = TRUE;
ecb4347a
DJ
14973 break;
14974
14975 case OPTION_NO_MDEBUG:
b34976b6 14976 mips_flag_mdebug = FALSE;
ecb4347a 14977 break;
dcd410fe
RO
14978
14979 case OPTION_PDR:
14980 mips_flag_pdr = TRUE;
14981 break;
14982
14983 case OPTION_NO_PDR:
14984 mips_flag_pdr = FALSE;
14985 break;
0a44bf69
RS
14986
14987 case OPTION_MVXWORKS_PIC:
14988 mips_pic = VXWORKS_PIC;
14989 break;
ecb4347a
DJ
14990#endif /* OBJ_ELF */
14991
252b5132
RH
14992 default:
14993 return 0;
14994 }
14995
c67a084a
NC
14996 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14997
252b5132
RH
14998 return 1;
14999}
316f5878
RS
15000\f
15001/* Set up globals to generate code for the ISA or processor
15002 described by INFO. */
252b5132 15003
252b5132 15004static void
17a2f251 15005mips_set_architecture (const struct mips_cpu_info *info)
252b5132 15006{
316f5878 15007 if (info != 0)
252b5132 15008 {
fef14a42
TS
15009 file_mips_arch = info->cpu;
15010 mips_opts.arch = info->cpu;
316f5878 15011 mips_opts.isa = info->isa;
252b5132 15012 }
252b5132
RH
15013}
15014
252b5132 15015
316f5878 15016/* Likewise for tuning. */
252b5132 15017
316f5878 15018static void
17a2f251 15019mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
15020{
15021 if (info != 0)
fef14a42 15022 mips_tune = info->cpu;
316f5878 15023}
80cc45a5 15024
34ba82a8 15025
252b5132 15026void
17a2f251 15027mips_after_parse_args (void)
e9670677 15028{
fef14a42
TS
15029 const struct mips_cpu_info *arch_info = 0;
15030 const struct mips_cpu_info *tune_info = 0;
15031
e9670677 15032 /* GP relative stuff not working for PE */
6caf9ef4 15033 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 15034 {
6caf9ef4 15035 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
15036 as_bad (_("-G not supported in this configuration."));
15037 g_switch_value = 0;
15038 }
15039
cac012d6
AO
15040 if (mips_abi == NO_ABI)
15041 mips_abi = MIPS_DEFAULT_ABI;
15042
22923709
RS
15043 /* The following code determines the architecture and register size.
15044 Similar code was added to GCC 3.3 (see override_options() in
15045 config/mips/mips.c). The GAS and GCC code should be kept in sync
15046 as much as possible. */
e9670677 15047
316f5878 15048 if (mips_arch_string != 0)
fef14a42 15049 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 15050
316f5878 15051 if (file_mips_isa != ISA_UNKNOWN)
e9670677 15052 {
316f5878 15053 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 15054 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 15055 the -march selection (if any). */
fef14a42 15056 if (arch_info != 0)
e9670677 15057 {
316f5878
RS
15058 /* -march takes precedence over -mipsN, since it is more descriptive.
15059 There's no harm in specifying both as long as the ISA levels
15060 are the same. */
fef14a42 15061 if (file_mips_isa != arch_info->isa)
316f5878
RS
15062 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15063 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 15064 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15065 }
316f5878 15066 else
fef14a42 15067 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
15068 }
15069
fef14a42 15070 if (arch_info == 0)
95bfe26e
MF
15071 {
15072 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15073 gas_assert (arch_info);
15074 }
e9670677 15075
fef14a42 15076 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15077 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15078 arch_info->name);
15079
15080 mips_set_architecture (arch_info);
15081
15082 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
15083 if (mips_tune_string != 0)
15084 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15085
fef14a42
TS
15086 if (tune_info == 0)
15087 mips_set_tune (arch_info);
15088 else
15089 mips_set_tune (tune_info);
e9670677 15090
316f5878 15091 if (file_mips_gp32 >= 0)
e9670677 15092 {
316f5878
RS
15093 /* The user specified the size of the integer registers. Make sure
15094 it agrees with the ABI and ISA. */
15095 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15096 as_bad (_("-mgp64 used with a 32-bit processor"));
15097 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15098 as_bad (_("-mgp32 used with a 64-bit ABI"));
15099 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15100 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
15101 }
15102 else
15103 {
316f5878
RS
15104 /* Infer the integer register size from the ABI and processor.
15105 Restrict ourselves to 32-bit registers if that's all the
15106 processor has, or if the ABI cannot handle 64-bit registers. */
15107 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15108 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
15109 }
15110
ad3fea08
TS
15111 switch (file_mips_fp32)
15112 {
15113 default:
15114 case -1:
15115 /* No user specified float register size.
15116 ??? GAS treats single-float processors as though they had 64-bit
15117 float registers (although it complains when double-precision
15118 instructions are used). As things stand, saying they have 32-bit
15119 registers would lead to spurious "register must be even" messages.
15120 So here we assume float registers are never smaller than the
15121 integer ones. */
15122 if (file_mips_gp32 == 0)
15123 /* 64-bit integer registers implies 64-bit float registers. */
15124 file_mips_fp32 = 0;
15125 else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
15126 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15127 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
15128 file_mips_fp32 = 0;
15129 else
15130 /* 32-bit float registers. */
15131 file_mips_fp32 = 1;
15132 break;
15133
15134 /* The user specified the size of the float registers. Check if it
15135 agrees with the ABI and ISA. */
15136 case 0:
15137 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15138 as_bad (_("-mfp64 used with a 32-bit fpu"));
15139 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15140 && !ISA_HAS_MXHC1 (mips_opts.isa))
15141 as_warn (_("-mfp64 used with a 32-bit ABI"));
15142 break;
15143 case 1:
15144 if (ABI_NEEDS_64BIT_REGS (mips_abi))
15145 as_warn (_("-mfp32 used with a 64-bit ABI"));
15146 break;
15147 }
e9670677 15148
316f5878 15149 /* End of GCC-shared inference code. */
e9670677 15150
17a2f251
TS
15151 /* This flag is set when we have a 64-bit capable CPU but use only
15152 32-bit wide registers. Note that EABI does not use it. */
15153 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15154 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15155 || mips_abi == O32_ABI))
316f5878 15156 mips_32bitmode = 1;
e9670677
MR
15157
15158 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15159 as_bad (_("trap exception not supported at ISA 1"));
15160
e9670677
MR
15161 /* If the selected architecture includes support for ASEs, enable
15162 generation of code for them. */
a4672219 15163 if (mips_opts.mips16 == -1)
fef14a42 15164 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
df58fc94
RS
15165 if (mips_opts.micromips == -1)
15166 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
ffdefa66 15167 if (mips_opts.ase_mips3d == -1)
65263ce3 15168 mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
ad3fea08
TS
15169 && file_mips_fp32 == 0) ? 1 : 0;
15170 if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
15171 as_bad (_("-mfp32 used with -mips3d"));
15172
ffdefa66 15173 if (mips_opts.ase_mdmx == -1)
65263ce3 15174 mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
ad3fea08
TS
15175 && file_mips_fp32 == 0) ? 1 : 0;
15176 if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
15177 as_bad (_("-mfp32 used with -mdmx"));
15178
15179 if (mips_opts.ase_smartmips == -1)
15180 mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
15181 if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
20203fb9
NC
15182 as_warn (_("%s ISA does not support SmartMIPS"),
15183 mips_cpu_info_from_isa (mips_opts.isa)->name);
ad3fea08 15184
74cd071d 15185 if (mips_opts.ase_dsp == -1)
ad3fea08
TS
15186 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15187 if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
20203fb9
NC
15188 as_warn (_("%s ISA does not support DSP ASE"),
15189 mips_cpu_info_from_isa (mips_opts.isa)->name);
ad3fea08 15190
8b082fb1
TS
15191 if (mips_opts.ase_dspr2 == -1)
15192 {
15193 mips_opts.ase_dspr2 = (arch_info->flags & MIPS_CPU_ASE_DSPR2) ? 1 : 0;
15194 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15195 }
15196 if (mips_opts.ase_dspr2 && !ISA_SUPPORTS_DSPR2_ASE)
20203fb9
NC
15197 as_warn (_("%s ISA does not support DSP R2 ASE"),
15198 mips_cpu_info_from_isa (mips_opts.isa)->name);
8b082fb1 15199
ef2e4d86 15200 if (mips_opts.ase_mt == -1)
ad3fea08
TS
15201 mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
15202 if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
20203fb9
NC
15203 as_warn (_("%s ISA does not support MT ASE"),
15204 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 15205
dec0624d
MR
15206 if (mips_opts.ase_mcu == -1)
15207 mips_opts.ase_mcu = (arch_info->flags & MIPS_CPU_ASE_MCU) ? 1 : 0;
15208 if (mips_opts.ase_mcu && !ISA_SUPPORTS_MCU_ASE)
15209 as_warn (_("%s ISA does not support MCU ASE"),
15210 mips_cpu_info_from_isa (mips_opts.isa)->name);
15211
e9670677 15212 file_mips_isa = mips_opts.isa;
e9670677
MR
15213 file_ase_mips3d = mips_opts.ase_mips3d;
15214 file_ase_mdmx = mips_opts.ase_mdmx;
e16bfa71 15215 file_ase_smartmips = mips_opts.ase_smartmips;
74cd071d 15216 file_ase_dsp = mips_opts.ase_dsp;
8b082fb1 15217 file_ase_dspr2 = mips_opts.ase_dspr2;
ef2e4d86 15218 file_ase_mt = mips_opts.ase_mt;
e9670677
MR
15219 mips_opts.gp32 = file_mips_gp32;
15220 mips_opts.fp32 = file_mips_fp32;
037b32b9
AN
15221 mips_opts.soft_float = file_mips_soft_float;
15222 mips_opts.single_float = file_mips_single_float;
e9670677 15223
ecb4347a
DJ
15224 if (mips_flag_mdebug < 0)
15225 {
15226#ifdef OBJ_MAYBE_ECOFF
15227 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
15228 mips_flag_mdebug = 1;
15229 else
15230#endif /* OBJ_MAYBE_ECOFF */
15231 mips_flag_mdebug = 0;
15232 }
e9670677
MR
15233}
15234\f
15235void
17a2f251 15236mips_init_after_args (void)
252b5132
RH
15237{
15238 /* initialize opcodes */
15239 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15240 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15241}
15242
15243long
17a2f251 15244md_pcrel_from (fixS *fixP)
252b5132 15245{
a7ebbfdf
TS
15246 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15247 switch (fixP->fx_r_type)
15248 {
df58fc94
RS
15249 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15250 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15251 /* Return the address of the delay slot. */
15252 return addr + 2;
15253
15254 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15255 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf
TS
15256 case BFD_RELOC_16_PCREL_S2:
15257 case BFD_RELOC_MIPS_JMP:
15258 /* Return the address of the delay slot. */
15259 return addr + 4;
df58fc94 15260
a7ebbfdf 15261 default:
58ea3d6a 15262 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
15263 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15264 as_bad_where (fixP->fx_file, fixP->fx_line,
15265 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
15266 return addr;
15267 }
252b5132
RH
15268}
15269
252b5132
RH
15270/* This is called before the symbol table is processed. In order to
15271 work with gcc when using mips-tfile, we must keep all local labels.
15272 However, in other cases, we want to discard them. If we were
15273 called with -g, but we didn't see any debugging information, it may
15274 mean that gcc is smuggling debugging information through to
15275 mips-tfile, in which case we must generate all local labels. */
15276
15277void
17a2f251 15278mips_frob_file_before_adjust (void)
252b5132
RH
15279{
15280#ifndef NO_ECOFF_DEBUGGING
15281 if (ECOFF_DEBUGGING
15282 && mips_debug != 0
15283 && ! ecoff_debugging_seen)
15284 flag_keep_locals = 1;
15285#endif
15286}
15287
3b91255e 15288/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15289 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15290 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15291 relocation operators.
15292
15293 For our purposes, a %lo() expression matches a %got() or %hi()
15294 expression if:
15295
15296 (a) it refers to the same symbol; and
15297 (b) the offset applied in the %lo() expression is no lower than
15298 the offset applied in the %got() or %hi().
15299
15300 (b) allows us to cope with code like:
15301
15302 lui $4,%hi(foo)
15303 lh $4,%lo(foo+2)($4)
15304
15305 ...which is legal on RELA targets, and has a well-defined behaviour
15306 if the user knows that adding 2 to "foo" will not induce a carry to
15307 the high 16 bits.
15308
15309 When several %lo()s match a particular %got() or %hi(), we use the
15310 following rules to distinguish them:
15311
15312 (1) %lo()s with smaller offsets are a better match than %lo()s with
15313 higher offsets.
15314
15315 (2) %lo()s with no matching %got() or %hi() are better than those
15316 that already have a matching %got() or %hi().
15317
15318 (3) later %lo()s are better than earlier %lo()s.
15319
15320 These rules are applied in order.
15321
15322 (1) means, among other things, that %lo()s with identical offsets are
15323 chosen if they exist.
15324
15325 (2) means that we won't associate several high-part relocations with
15326 the same low-part relocation unless there's no alternative. Having
15327 several high parts for the same low part is a GNU extension; this rule
15328 allows careful users to avoid it.
15329
15330 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15331 with the last high-part relocation being at the front of the list.
15332 It therefore makes sense to choose the last matching low-part
15333 relocation, all other things being equal. It's also easier
15334 to code that way. */
252b5132
RH
15335
15336void
17a2f251 15337mips_frob_file (void)
252b5132
RH
15338{
15339 struct mips_hi_fixup *l;
35903be0 15340 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15341
15342 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15343 {
15344 segment_info_type *seginfo;
3b91255e
RS
15345 bfd_boolean matched_lo_p;
15346 fixS **hi_pos, **lo_pos, **pos;
252b5132 15347
9c2799c2 15348 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15349
5919d012
RS
15350 /* If a GOT16 relocation turns out to be against a global symbol,
15351 there isn't supposed to be a matching LO. */
738e5348 15352 if (got16_reloc_p (l->fixp->fx_r_type)
5919d012
RS
15353 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
15354 continue;
15355
15356 /* Check quickly whether the next fixup happens to be a matching %lo. */
15357 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15358 continue;
15359
252b5132 15360 seginfo = seg_info (l->seg);
252b5132 15361
3b91255e
RS
15362 /* Set HI_POS to the position of this relocation in the chain.
15363 Set LO_POS to the position of the chosen low-part relocation.
15364 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15365 relocation that matches an immediately-preceding high-part
15366 relocation. */
15367 hi_pos = NULL;
15368 lo_pos = NULL;
15369 matched_lo_p = FALSE;
738e5348 15370 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15371
3b91255e
RS
15372 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15373 {
15374 if (*pos == l->fixp)
15375 hi_pos = pos;
15376
35903be0 15377 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15378 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15379 && (*pos)->fx_offset >= l->fixp->fx_offset
15380 && (lo_pos == NULL
15381 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15382 || (!matched_lo_p
15383 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15384 lo_pos = pos;
15385
15386 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15387 && fixup_has_matching_lo_p (*pos));
15388 }
15389
15390 /* If we found a match, remove the high-part relocation from its
15391 current position and insert it before the low-part relocation.
15392 Make the offsets match so that fixup_has_matching_lo_p()
15393 will return true.
15394
15395 We don't warn about unmatched high-part relocations since some
15396 versions of gcc have been known to emit dead "lui ...%hi(...)"
15397 instructions. */
15398 if (lo_pos != NULL)
15399 {
15400 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15401 if (l->fixp->fx_next != *lo_pos)
252b5132 15402 {
3b91255e
RS
15403 *hi_pos = l->fixp->fx_next;
15404 l->fixp->fx_next = *lo_pos;
15405 *lo_pos = l->fixp;
252b5132 15406 }
252b5132
RH
15407 }
15408 }
15409}
15410
3e722fb5 15411/* We may have combined relocations without symbols in the N32/N64 ABI.
f6688943 15412 We have to prevent gas from dropping them. */
252b5132 15413
252b5132 15414int
17a2f251 15415mips_force_relocation (fixS *fixp)
252b5132 15416{
ae6063d4 15417 if (generic_force_reloc (fixp))
252b5132
RH
15418 return 1;
15419
df58fc94
RS
15420 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15421 so that the linker relaxation can update targets. */
15422 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15423 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15424 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15425 return 1;
15426
f6688943
TS
15427 if (HAVE_NEWABI
15428 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
15429 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
738e5348
RS
15430 || hi16_reloc_p (fixp->fx_r_type)
15431 || lo16_reloc_p (fixp->fx_r_type)))
f6688943
TS
15432 return 1;
15433
3e722fb5 15434 return 0;
252b5132
RH
15435}
15436
15437/* Apply a fixup to the object file. */
15438
94f592af 15439void
55cf6793 15440md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15441{
874e8986 15442 bfd_byte *buf;
98aa84af 15443 long insn;
a7ebbfdf 15444 reloc_howto_type *howto;
252b5132 15445
a7ebbfdf
TS
15446 /* We ignore generic BFD relocations we don't know about. */
15447 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15448 if (! howto)
15449 return;
65551fa4 15450
df58fc94
RS
15451 gas_assert (fixP->fx_size == 2
15452 || fixP->fx_size == 4
90ecf173
MR
15453 || fixP->fx_r_type == BFD_RELOC_16
15454 || fixP->fx_r_type == BFD_RELOC_64
15455 || fixP->fx_r_type == BFD_RELOC_CTOR
15456 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15457 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15458 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15459 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15460 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 15461
a7ebbfdf 15462 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132 15463
df58fc94
RS
15464 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15465 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15466 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15467 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
b1dca8ee
RS
15468
15469 /* Don't treat parts of a composite relocation as done. There are two
15470 reasons for this:
15471
15472 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15473 should nevertheless be emitted if the first part is.
15474
15475 (2) In normal usage, composite relocations are never assembly-time
15476 constants. The easiest way of dealing with the pathological
15477 exceptions is to generate a relocation against STN_UNDEF and
15478 leave everything up to the linker. */
3994f87e 15479 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15480 fixP->fx_done = 1;
15481
15482 switch (fixP->fx_r_type)
15483 {
3f98094e
DJ
15484 case BFD_RELOC_MIPS_TLS_GD:
15485 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15486 case BFD_RELOC_MIPS_TLS_DTPREL32:
15487 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15488 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15489 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15490 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15491 case BFD_RELOC_MIPS_TLS_TPREL32:
15492 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15493 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15494 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15495 case BFD_RELOC_MICROMIPS_TLS_GD:
15496 case BFD_RELOC_MICROMIPS_TLS_LDM:
15497 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15498 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15499 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15500 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15501 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15502 case BFD_RELOC_MIPS16_TLS_GD:
15503 case BFD_RELOC_MIPS16_TLS_LDM:
15504 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15505 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15506 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15507 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15508 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
3f98094e
DJ
15509 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15510 /* fall through */
15511
252b5132 15512 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
15513 case BFD_RELOC_MIPS_SHIFT5:
15514 case BFD_RELOC_MIPS_SHIFT6:
15515 case BFD_RELOC_MIPS_GOT_DISP:
15516 case BFD_RELOC_MIPS_GOT_PAGE:
15517 case BFD_RELOC_MIPS_GOT_OFST:
15518 case BFD_RELOC_MIPS_SUB:
15519 case BFD_RELOC_MIPS_INSERT_A:
15520 case BFD_RELOC_MIPS_INSERT_B:
15521 case BFD_RELOC_MIPS_DELETE:
15522 case BFD_RELOC_MIPS_HIGHEST:
15523 case BFD_RELOC_MIPS_HIGHER:
15524 case BFD_RELOC_MIPS_SCN_DISP:
15525 case BFD_RELOC_MIPS_REL16:
15526 case BFD_RELOC_MIPS_RELGOT:
15527 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15528 case BFD_RELOC_HI16:
15529 case BFD_RELOC_HI16_S:
cdf6fd85 15530 case BFD_RELOC_GPREL16:
252b5132
RH
15531 case BFD_RELOC_MIPS_LITERAL:
15532 case BFD_RELOC_MIPS_CALL16:
15533 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15534 case BFD_RELOC_GPREL32:
252b5132
RH
15535 case BFD_RELOC_MIPS_GOT_HI16:
15536 case BFD_RELOC_MIPS_GOT_LO16:
15537 case BFD_RELOC_MIPS_CALL_HI16:
15538 case BFD_RELOC_MIPS_CALL_LO16:
15539 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15540 case BFD_RELOC_MIPS16_GOT16:
15541 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15542 case BFD_RELOC_MIPS16_HI16:
15543 case BFD_RELOC_MIPS16_HI16_S:
252b5132 15544 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
15545 case BFD_RELOC_MICROMIPS_JMP:
15546 case BFD_RELOC_MICROMIPS_GOT_DISP:
15547 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15548 case BFD_RELOC_MICROMIPS_GOT_OFST:
15549 case BFD_RELOC_MICROMIPS_SUB:
15550 case BFD_RELOC_MICROMIPS_HIGHEST:
15551 case BFD_RELOC_MICROMIPS_HIGHER:
15552 case BFD_RELOC_MICROMIPS_SCN_DISP:
15553 case BFD_RELOC_MICROMIPS_JALR:
15554 case BFD_RELOC_MICROMIPS_HI16:
15555 case BFD_RELOC_MICROMIPS_HI16_S:
15556 case BFD_RELOC_MICROMIPS_GPREL16:
15557 case BFD_RELOC_MICROMIPS_LITERAL:
15558 case BFD_RELOC_MICROMIPS_CALL16:
15559 case BFD_RELOC_MICROMIPS_GOT16:
15560 case BFD_RELOC_MICROMIPS_GOT_HI16:
15561 case BFD_RELOC_MICROMIPS_GOT_LO16:
15562 case BFD_RELOC_MICROMIPS_CALL_HI16:
15563 case BFD_RELOC_MICROMIPS_CALL_LO16:
54f4ddb3 15564 /* Nothing needed to do. The value comes from the reloc entry. */
252b5132
RH
15565 break;
15566
252b5132
RH
15567 case BFD_RELOC_64:
15568 /* This is handled like BFD_RELOC_32, but we output a sign
15569 extended value if we are only 32 bits. */
3e722fb5 15570 if (fixP->fx_done)
252b5132
RH
15571 {
15572 if (8 <= sizeof (valueT))
2132e3a3 15573 md_number_to_chars ((char *) buf, *valP, 8);
252b5132
RH
15574 else
15575 {
a7ebbfdf 15576 valueT hiv;
252b5132 15577
a7ebbfdf 15578 if ((*valP & 0x80000000) != 0)
252b5132
RH
15579 hiv = 0xffffffff;
15580 else
15581 hiv = 0;
b215186b 15582 md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
a7ebbfdf 15583 *valP, 4);
b215186b 15584 md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
a7ebbfdf 15585 hiv, 4);
252b5132
RH
15586 }
15587 }
15588 break;
15589
056350c6 15590 case BFD_RELOC_RVA:
252b5132 15591 case BFD_RELOC_32:
252b5132
RH
15592 case BFD_RELOC_16:
15593 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15594 value now. This can happen if we have a .word which is not
15595 resolved when it appears but is later defined. */
252b5132 15596 if (fixP->fx_done)
54f4ddb3 15597 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
252b5132
RH
15598 break;
15599
15600 case BFD_RELOC_LO16:
d6f16593 15601 case BFD_RELOC_MIPS16_LO16:
df58fc94 15602 case BFD_RELOC_MICROMIPS_LO16:
3e722fb5
CD
15603 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
15604 may be safe to remove, but if so it's not obvious. */
252b5132
RH
15605 /* When handling an embedded PIC switch statement, we can wind
15606 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
15607 if (fixP->fx_done)
15608 {
a7ebbfdf 15609 if (*valP + 0x8000 > 0xffff)
252b5132
RH
15610 as_bad_where (fixP->fx_file, fixP->fx_line,
15611 _("relocation overflow"));
df58fc94
RS
15612 /* 32-bit microMIPS instructions are divided into two halfwords.
15613 Relocations always refer to the second halfword, regardless
15614 of endianness. */
15615 if (target_big_endian || fixP->fx_r_type == BFD_RELOC_MICROMIPS_LO16)
252b5132 15616 buf += 2;
2132e3a3 15617 md_number_to_chars ((char *) buf, *valP, 2);
252b5132
RH
15618 }
15619 break;
15620
15621 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 15622 if ((*valP & 0x3) != 0)
cb56d3d3 15623 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 15624 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 15625
54f4ddb3
TS
15626 /* We need to save the bits in the instruction since fixup_segment()
15627 might be deleting the relocation entry (i.e., a branch within
15628 the current segment). */
a7ebbfdf 15629 if (! fixP->fx_done)
bb2d6cd7 15630 break;
252b5132 15631
54f4ddb3 15632 /* Update old instruction data. */
252b5132
RH
15633 if (target_big_endian)
15634 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
15635 else
15636 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
15637
a7ebbfdf
TS
15638 if (*valP + 0x20000 <= 0x3ffff)
15639 {
15640 insn |= (*valP >> 2) & 0xffff;
2132e3a3 15641 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
15642 }
15643 else if (mips_pic == NO_PIC
15644 && fixP->fx_done
15645 && fixP->fx_frag->fr_address >= text_section->vma
15646 && (fixP->fx_frag->fr_address
587aac4e 15647 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15648 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15649 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15650 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15651 {
15652 /* The branch offset is too large. If this is an
15653 unconditional branch, and we are not generating PIC code,
15654 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15655 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15656 insn = 0x0c000000; /* jal */
252b5132 15657 else
a7ebbfdf
TS
15658 insn = 0x08000000; /* j */
15659 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15660 fixP->fx_done = 0;
15661 fixP->fx_addsy = section_symbol (text_section);
15662 *valP += md_pcrel_from (fixP);
2132e3a3 15663 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
15664 }
15665 else
15666 {
15667 /* If we got here, we have branch-relaxation disabled,
15668 and there's nothing we can do to fix this instruction
15669 without turning it into a longer sequence. */
15670 as_bad_where (fixP->fx_file, fixP->fx_line,
15671 _("Branch out of range"));
252b5132 15672 }
252b5132
RH
15673 break;
15674
df58fc94
RS
15675 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15676 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15677 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15678 /* We adjust the offset back to even. */
15679 if ((*valP & 0x1) != 0)
15680 --(*valP);
15681
15682 if (! fixP->fx_done)
15683 break;
15684
15685 /* Should never visit here, because we keep the relocation. */
15686 abort ();
15687 break;
15688
252b5132
RH
15689 case BFD_RELOC_VTABLE_INHERIT:
15690 fixP->fx_done = 0;
15691 if (fixP->fx_addsy
15692 && !S_IS_DEFINED (fixP->fx_addsy)
15693 && !S_IS_WEAK (fixP->fx_addsy))
15694 S_SET_WEAK (fixP->fx_addsy);
15695 break;
15696
15697 case BFD_RELOC_VTABLE_ENTRY:
15698 fixP->fx_done = 0;
15699 break;
15700
15701 default:
15702 internalError ();
15703 }
a7ebbfdf
TS
15704
15705 /* Remember value for tc_gen_reloc. */
15706 fixP->fx_addnumber = *valP;
252b5132
RH
15707}
15708
252b5132 15709static symbolS *
17a2f251 15710get_symbol (void)
252b5132
RH
15711{
15712 int c;
15713 char *name;
15714 symbolS *p;
15715
15716 name = input_line_pointer;
15717 c = get_symbol_end ();
15718 p = (symbolS *) symbol_find_or_make (name);
15719 *input_line_pointer = c;
15720 return p;
15721}
15722
742a56fe
RS
15723/* Align the current frag to a given power of two. If a particular
15724 fill byte should be used, FILL points to an integer that contains
15725 that byte, otherwise FILL is null.
15726
462427c4
RS
15727 This function used to have the comment:
15728
15729 The MIPS assembler also automatically adjusts any preceding label.
15730
15731 The implementation therefore applied the adjustment to a maximum of
15732 one label. However, other label adjustments are applied to batches
15733 of labels, and adjusting just one caused problems when new labels
15734 were added for the sake of debugging or unwind information.
15735 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
15736
15737static void
462427c4 15738mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 15739{
7d10b47d 15740 mips_emit_delays ();
df58fc94 15741 mips_record_compressed_mode ();
742a56fe
RS
15742 if (fill == NULL && subseg_text_p (now_seg))
15743 frag_align_code (to, 0);
15744 else
15745 frag_align (to, fill ? *fill : 0, 0);
252b5132 15746 record_alignment (now_seg, to);
462427c4 15747 mips_move_labels (labels, FALSE);
252b5132
RH
15748}
15749
15750/* Align to a given power of two. .align 0 turns off the automatic
15751 alignment used by the data creating pseudo-ops. */
15752
15753static void
17a2f251 15754s_align (int x ATTRIBUTE_UNUSED)
252b5132 15755{
742a56fe 15756 int temp, fill_value, *fill_ptr;
49954fb4 15757 long max_alignment = 28;
252b5132 15758
54f4ddb3 15759 /* o Note that the assembler pulls down any immediately preceding label
252b5132 15760 to the aligned address.
54f4ddb3 15761 o It's not documented but auto alignment is reinstated by
252b5132 15762 a .align pseudo instruction.
54f4ddb3 15763 o Note also that after auto alignment is turned off the mips assembler
252b5132 15764 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 15765 We don't. */
252b5132
RH
15766
15767 temp = get_absolute_expression ();
15768 if (temp > max_alignment)
15769 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
15770 else if (temp < 0)
15771 {
15772 as_warn (_("Alignment negative: 0 assumed."));
15773 temp = 0;
15774 }
15775 if (*input_line_pointer == ',')
15776 {
f9419b05 15777 ++input_line_pointer;
742a56fe
RS
15778 fill_value = get_absolute_expression ();
15779 fill_ptr = &fill_value;
252b5132
RH
15780 }
15781 else
742a56fe 15782 fill_ptr = 0;
252b5132
RH
15783 if (temp)
15784 {
a8dbcb85
TS
15785 segment_info_type *si = seg_info (now_seg);
15786 struct insn_label_list *l = si->label_list;
54f4ddb3 15787 /* Auto alignment should be switched on by next section change. */
252b5132 15788 auto_align = 1;
462427c4 15789 mips_align (temp, fill_ptr, l);
252b5132
RH
15790 }
15791 else
15792 {
15793 auto_align = 0;
15794 }
15795
15796 demand_empty_rest_of_line ();
15797}
15798
252b5132 15799static void
17a2f251 15800s_change_sec (int sec)
252b5132
RH
15801{
15802 segT seg;
15803
252b5132
RH
15804#ifdef OBJ_ELF
15805 /* The ELF backend needs to know that we are changing sections, so
15806 that .previous works correctly. We could do something like check
b6ff326e 15807 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
15808 as it would not be appropriate to use it in the section changing
15809 functions in read.c, since obj-elf.c intercepts those. FIXME:
15810 This should be cleaner, somehow. */
f43abd2b
TS
15811 if (IS_ELF)
15812 obj_elf_section_change_hook ();
252b5132
RH
15813#endif
15814
7d10b47d 15815 mips_emit_delays ();
6a32d874 15816
252b5132
RH
15817 switch (sec)
15818 {
15819 case 't':
15820 s_text (0);
15821 break;
15822 case 'd':
15823 s_data (0);
15824 break;
15825 case 'b':
15826 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15827 demand_empty_rest_of_line ();
15828 break;
15829
15830 case 'r':
4d0d148d
TS
15831 seg = subseg_new (RDATA_SECTION_NAME,
15832 (subsegT) get_absolute_expression ());
f43abd2b 15833 if (IS_ELF)
252b5132 15834 {
4d0d148d
TS
15835 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15836 | SEC_READONLY | SEC_RELOC
15837 | SEC_DATA));
c41e87e3 15838 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 15839 record_alignment (seg, 4);
252b5132 15840 }
4d0d148d 15841 demand_empty_rest_of_line ();
252b5132
RH
15842 break;
15843
15844 case 's':
4d0d148d 15845 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f43abd2b 15846 if (IS_ELF)
252b5132 15847 {
4d0d148d
TS
15848 bfd_set_section_flags (stdoutput, seg,
15849 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
c41e87e3 15850 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 15851 record_alignment (seg, 4);
252b5132 15852 }
4d0d148d
TS
15853 demand_empty_rest_of_line ();
15854 break;
998b3c36
MR
15855
15856 case 'B':
15857 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
15858 if (IS_ELF)
15859 {
15860 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15861 if (strncmp (TARGET_OS, "elf", 3) != 0)
15862 record_alignment (seg, 4);
15863 }
15864 demand_empty_rest_of_line ();
15865 break;
252b5132
RH
15866 }
15867
15868 auto_align = 1;
15869}
b34976b6 15870
cca86cc8 15871void
17a2f251 15872s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 15873{
7ed4a06a 15874#ifdef OBJ_ELF
cca86cc8
SC
15875 char *section_name;
15876 char c;
684022ea 15877 char next_c = 0;
cca86cc8
SC
15878 int section_type;
15879 int section_flag;
15880 int section_entry_size;
15881 int section_alignment;
b34976b6 15882
f43abd2b 15883 if (!IS_ELF)
7ed4a06a
TS
15884 return;
15885
cca86cc8
SC
15886 section_name = input_line_pointer;
15887 c = get_symbol_end ();
a816d1ed
AO
15888 if (c)
15889 next_c = *(input_line_pointer + 1);
cca86cc8 15890
4cf0dd0d
TS
15891 /* Do we have .section Name<,"flags">? */
15892 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 15893 {
4cf0dd0d
TS
15894 /* just after name is now '\0'. */
15895 *input_line_pointer = c;
cca86cc8
SC
15896 input_line_pointer = section_name;
15897 obj_elf_section (ignore);
15898 return;
15899 }
15900 input_line_pointer++;
15901
15902 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
15903 if (c == ',')
15904 section_type = get_absolute_expression ();
15905 else
15906 section_type = 0;
15907 if (*input_line_pointer++ == ',')
15908 section_flag = get_absolute_expression ();
15909 else
15910 section_flag = 0;
15911 if (*input_line_pointer++ == ',')
15912 section_entry_size = get_absolute_expression ();
15913 else
15914 section_entry_size = 0;
15915 if (*input_line_pointer++ == ',')
15916 section_alignment = get_absolute_expression ();
15917 else
15918 section_alignment = 0;
87975d2a
AM
15919 /* FIXME: really ignore? */
15920 (void) section_alignment;
cca86cc8 15921
a816d1ed
AO
15922 section_name = xstrdup (section_name);
15923
8ab8a5c8
RS
15924 /* When using the generic form of .section (as implemented by obj-elf.c),
15925 there's no way to set the section type to SHT_MIPS_DWARF. Users have
15926 traditionally had to fall back on the more common @progbits instead.
15927
15928 There's nothing really harmful in this, since bfd will correct
15929 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 15930 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
15931 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
15932
15933 Even so, we shouldn't force users of the MIPS .section syntax to
15934 incorrectly label the sections as SHT_PROGBITS. The best compromise
15935 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
15936 generic type-checking code. */
15937 if (section_type == SHT_MIPS_DWARF)
15938 section_type = SHT_PROGBITS;
15939
cca86cc8
SC
15940 obj_elf_change_section (section_name, section_type, section_flag,
15941 section_entry_size, 0, 0, 0);
a816d1ed
AO
15942
15943 if (now_seg->name != section_name)
15944 free (section_name);
7ed4a06a 15945#endif /* OBJ_ELF */
cca86cc8 15946}
252b5132
RH
15947
15948void
17a2f251 15949mips_enable_auto_align (void)
252b5132
RH
15950{
15951 auto_align = 1;
15952}
15953
15954static void
17a2f251 15955s_cons (int log_size)
252b5132 15956{
a8dbcb85
TS
15957 segment_info_type *si = seg_info (now_seg);
15958 struct insn_label_list *l = si->label_list;
252b5132 15959
7d10b47d 15960 mips_emit_delays ();
252b5132 15961 if (log_size > 0 && auto_align)
462427c4 15962 mips_align (log_size, 0, l);
252b5132 15963 cons (1 << log_size);
a1facbec 15964 mips_clear_insn_labels ();
252b5132
RH
15965}
15966
15967static void
17a2f251 15968s_float_cons (int type)
252b5132 15969{
a8dbcb85
TS
15970 segment_info_type *si = seg_info (now_seg);
15971 struct insn_label_list *l = si->label_list;
252b5132 15972
7d10b47d 15973 mips_emit_delays ();
252b5132
RH
15974
15975 if (auto_align)
49309057
ILT
15976 {
15977 if (type == 'd')
462427c4 15978 mips_align (3, 0, l);
49309057 15979 else
462427c4 15980 mips_align (2, 0, l);
49309057 15981 }
252b5132 15982
252b5132 15983 float_cons (type);
a1facbec 15984 mips_clear_insn_labels ();
252b5132
RH
15985}
15986
15987/* Handle .globl. We need to override it because on Irix 5 you are
15988 permitted to say
15989 .globl foo .text
15990 where foo is an undefined symbol, to mean that foo should be
15991 considered to be the address of a function. */
15992
15993static void
17a2f251 15994s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
15995{
15996 char *name;
15997 int c;
15998 symbolS *symbolP;
15999 flagword flag;
16000
8a06b769 16001 do
252b5132 16002 {
8a06b769 16003 name = input_line_pointer;
252b5132 16004 c = get_symbol_end ();
8a06b769
TS
16005 symbolP = symbol_find_or_make (name);
16006 S_SET_EXTERNAL (symbolP);
16007
252b5132 16008 *input_line_pointer = c;
8a06b769 16009 SKIP_WHITESPACE ();
252b5132 16010
8a06b769
TS
16011 /* On Irix 5, every global symbol that is not explicitly labelled as
16012 being a function is apparently labelled as being an object. */
16013 flag = BSF_OBJECT;
252b5132 16014
8a06b769
TS
16015 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16016 && (*input_line_pointer != ','))
16017 {
16018 char *secname;
16019 asection *sec;
16020
16021 secname = input_line_pointer;
16022 c = get_symbol_end ();
16023 sec = bfd_get_section_by_name (stdoutput, secname);
16024 if (sec == NULL)
16025 as_bad (_("%s: no such section"), secname);
16026 *input_line_pointer = c;
16027
16028 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16029 flag = BSF_FUNCTION;
16030 }
16031
16032 symbol_get_bfdsym (symbolP)->flags |= flag;
16033
16034 c = *input_line_pointer;
16035 if (c == ',')
16036 {
16037 input_line_pointer++;
16038 SKIP_WHITESPACE ();
16039 if (is_end_of_line[(unsigned char) *input_line_pointer])
16040 c = '\n';
16041 }
16042 }
16043 while (c == ',');
252b5132 16044
252b5132
RH
16045 demand_empty_rest_of_line ();
16046}
16047
16048static void
17a2f251 16049s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16050{
16051 char *opt;
16052 char c;
16053
16054 opt = input_line_pointer;
16055 c = get_symbol_end ();
16056
16057 if (*opt == 'O')
16058 {
16059 /* FIXME: What does this mean? */
16060 }
16061 else if (strncmp (opt, "pic", 3) == 0)
16062 {
16063 int i;
16064
16065 i = atoi (opt + 3);
16066 if (i == 0)
16067 mips_pic = NO_PIC;
16068 else if (i == 2)
143d77c5 16069 {
8b828383 16070 mips_pic = SVR4_PIC;
143d77c5
EC
16071 mips_abicalls = TRUE;
16072 }
252b5132
RH
16073 else
16074 as_bad (_(".option pic%d not supported"), i);
16075
4d0d148d 16076 if (mips_pic == SVR4_PIC)
252b5132
RH
16077 {
16078 if (g_switch_seen && g_switch_value != 0)
16079 as_warn (_("-G may not be used with SVR4 PIC code"));
16080 g_switch_value = 0;
16081 bfd_set_gp_size (stdoutput, 0);
16082 }
16083 }
16084 else
16085 as_warn (_("Unrecognized option \"%s\""), opt);
16086
16087 *input_line_pointer = c;
16088 demand_empty_rest_of_line ();
16089}
16090
16091/* This structure is used to hold a stack of .set values. */
16092
e972090a
NC
16093struct mips_option_stack
16094{
252b5132
RH
16095 struct mips_option_stack *next;
16096 struct mips_set_options options;
16097};
16098
16099static struct mips_option_stack *mips_opts_stack;
16100
16101/* Handle the .set pseudo-op. */
16102
16103static void
17a2f251 16104s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
16105{
16106 char *name = input_line_pointer, ch;
16107
16108 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 16109 ++input_line_pointer;
252b5132
RH
16110 ch = *input_line_pointer;
16111 *input_line_pointer = '\0';
16112
16113 if (strcmp (name, "reorder") == 0)
16114 {
7d10b47d
RS
16115 if (mips_opts.noreorder)
16116 end_noreorder ();
252b5132
RH
16117 }
16118 else if (strcmp (name, "noreorder") == 0)
16119 {
7d10b47d
RS
16120 if (!mips_opts.noreorder)
16121 start_noreorder ();
252b5132 16122 }
741fe287
MR
16123 else if (strncmp (name, "at=", 3) == 0)
16124 {
16125 char *s = name + 3;
16126
16127 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16128 as_bad (_("Unrecognized register name `%s'"), s);
16129 }
252b5132
RH
16130 else if (strcmp (name, "at") == 0)
16131 {
741fe287 16132 mips_opts.at = ATREG;
252b5132
RH
16133 }
16134 else if (strcmp (name, "noat") == 0)
16135 {
741fe287 16136 mips_opts.at = ZERO;
252b5132
RH
16137 }
16138 else if (strcmp (name, "macro") == 0)
16139 {
16140 mips_opts.warn_about_macros = 0;
16141 }
16142 else if (strcmp (name, "nomacro") == 0)
16143 {
16144 if (mips_opts.noreorder == 0)
16145 as_bad (_("`noreorder' must be set before `nomacro'"));
16146 mips_opts.warn_about_macros = 1;
16147 }
16148 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16149 {
16150 mips_opts.nomove = 0;
16151 }
16152 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16153 {
16154 mips_opts.nomove = 1;
16155 }
16156 else if (strcmp (name, "bopt") == 0)
16157 {
16158 mips_opts.nobopt = 0;
16159 }
16160 else if (strcmp (name, "nobopt") == 0)
16161 {
16162 mips_opts.nobopt = 1;
16163 }
ad3fea08
TS
16164 else if (strcmp (name, "gp=default") == 0)
16165 mips_opts.gp32 = file_mips_gp32;
16166 else if (strcmp (name, "gp=32") == 0)
16167 mips_opts.gp32 = 1;
16168 else if (strcmp (name, "gp=64") == 0)
16169 {
16170 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 16171 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08
TS
16172 mips_cpu_info_from_isa (mips_opts.isa)->name);
16173 mips_opts.gp32 = 0;
16174 }
16175 else if (strcmp (name, "fp=default") == 0)
16176 mips_opts.fp32 = file_mips_fp32;
16177 else if (strcmp (name, "fp=32") == 0)
16178 mips_opts.fp32 = 1;
16179 else if (strcmp (name, "fp=64") == 0)
16180 {
16181 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 16182 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08
TS
16183 mips_cpu_info_from_isa (mips_opts.isa)->name);
16184 mips_opts.fp32 = 0;
16185 }
037b32b9
AN
16186 else if (strcmp (name, "softfloat") == 0)
16187 mips_opts.soft_float = 1;
16188 else if (strcmp (name, "hardfloat") == 0)
16189 mips_opts.soft_float = 0;
16190 else if (strcmp (name, "singlefloat") == 0)
16191 mips_opts.single_float = 1;
16192 else if (strcmp (name, "doublefloat") == 0)
16193 mips_opts.single_float = 0;
252b5132
RH
16194 else if (strcmp (name, "mips16") == 0
16195 || strcmp (name, "MIPS-16") == 0)
df58fc94
RS
16196 {
16197 if (mips_opts.micromips == 1)
16198 as_fatal (_("`mips16' cannot be used with `micromips'"));
16199 mips_opts.mips16 = 1;
16200 }
252b5132
RH
16201 else if (strcmp (name, "nomips16") == 0
16202 || strcmp (name, "noMIPS-16") == 0)
16203 mips_opts.mips16 = 0;
df58fc94
RS
16204 else if (strcmp (name, "micromips") == 0)
16205 {
16206 if (mips_opts.mips16 == 1)
16207 as_fatal (_("`micromips' cannot be used with `mips16'"));
16208 mips_opts.micromips = 1;
16209 }
16210 else if (strcmp (name, "nomicromips") == 0)
16211 mips_opts.micromips = 0;
e16bfa71
TS
16212 else if (strcmp (name, "smartmips") == 0)
16213 {
ad3fea08 16214 if (!ISA_SUPPORTS_SMARTMIPS)
20203fb9 16215 as_warn (_("%s ISA does not support SmartMIPS ASE"),
e16bfa71
TS
16216 mips_cpu_info_from_isa (mips_opts.isa)->name);
16217 mips_opts.ase_smartmips = 1;
16218 }
16219 else if (strcmp (name, "nosmartmips") == 0)
16220 mips_opts.ase_smartmips = 0;
1f25f5d3
CD
16221 else if (strcmp (name, "mips3d") == 0)
16222 mips_opts.ase_mips3d = 1;
16223 else if (strcmp (name, "nomips3d") == 0)
16224 mips_opts.ase_mips3d = 0;
a4672219
TS
16225 else if (strcmp (name, "mdmx") == 0)
16226 mips_opts.ase_mdmx = 1;
16227 else if (strcmp (name, "nomdmx") == 0)
16228 mips_opts.ase_mdmx = 0;
74cd071d 16229 else if (strcmp (name, "dsp") == 0)
ad3fea08
TS
16230 {
16231 if (!ISA_SUPPORTS_DSP_ASE)
20203fb9 16232 as_warn (_("%s ISA does not support DSP ASE"),
ad3fea08
TS
16233 mips_cpu_info_from_isa (mips_opts.isa)->name);
16234 mips_opts.ase_dsp = 1;
8b082fb1 16235 mips_opts.ase_dspr2 = 0;
ad3fea08 16236 }
74cd071d 16237 else if (strcmp (name, "nodsp") == 0)
8b082fb1
TS
16238 {
16239 mips_opts.ase_dsp = 0;
16240 mips_opts.ase_dspr2 = 0;
16241 }
16242 else if (strcmp (name, "dspr2") == 0)
16243 {
16244 if (!ISA_SUPPORTS_DSPR2_ASE)
20203fb9 16245 as_warn (_("%s ISA does not support DSP R2 ASE"),
8b082fb1
TS
16246 mips_cpu_info_from_isa (mips_opts.isa)->name);
16247 mips_opts.ase_dspr2 = 1;
16248 mips_opts.ase_dsp = 1;
16249 }
16250 else if (strcmp (name, "nodspr2") == 0)
16251 {
16252 mips_opts.ase_dspr2 = 0;
16253 mips_opts.ase_dsp = 0;
16254 }
ef2e4d86 16255 else if (strcmp (name, "mt") == 0)
ad3fea08
TS
16256 {
16257 if (!ISA_SUPPORTS_MT_ASE)
20203fb9 16258 as_warn (_("%s ISA does not support MT ASE"),
ad3fea08
TS
16259 mips_cpu_info_from_isa (mips_opts.isa)->name);
16260 mips_opts.ase_mt = 1;
16261 }
ef2e4d86
CF
16262 else if (strcmp (name, "nomt") == 0)
16263 mips_opts.ase_mt = 0;
dec0624d
MR
16264 else if (strcmp (name, "mcu") == 0)
16265 mips_opts.ase_mcu = 1;
16266 else if (strcmp (name, "nomcu") == 0)
16267 mips_opts.ase_mcu = 0;
1a2c1fad 16268 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16269 {
af7ee8bf 16270 int reset = 0;
252b5132 16271
1a2c1fad
CD
16272 /* Permit the user to change the ISA and architecture on the fly.
16273 Needless to say, misuse can cause serious problems. */
81a21e38 16274 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
16275 {
16276 reset = 1;
16277 mips_opts.isa = file_mips_isa;
1a2c1fad 16278 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
16279 }
16280 else if (strncmp (name, "arch=", 5) == 0)
16281 {
16282 const struct mips_cpu_info *p;
16283
16284 p = mips_parse_cpu("internal use", name + 5);
16285 if (!p)
16286 as_bad (_("unknown architecture %s"), name + 5);
16287 else
16288 {
16289 mips_opts.arch = p->cpu;
16290 mips_opts.isa = p->isa;
16291 }
16292 }
81a21e38
TS
16293 else if (strncmp (name, "mips", 4) == 0)
16294 {
16295 const struct mips_cpu_info *p;
16296
16297 p = mips_parse_cpu("internal use", name);
16298 if (!p)
16299 as_bad (_("unknown ISA level %s"), name + 4);
16300 else
16301 {
16302 mips_opts.arch = p->cpu;
16303 mips_opts.isa = p->isa;
16304 }
16305 }
af7ee8bf 16306 else
81a21e38 16307 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
16308
16309 switch (mips_opts.isa)
98d3f06f
KH
16310 {
16311 case 0:
98d3f06f 16312 break;
af7ee8bf
CD
16313 case ISA_MIPS1:
16314 case ISA_MIPS2:
16315 case ISA_MIPS32:
16316 case ISA_MIPS32R2:
98d3f06f
KH
16317 mips_opts.gp32 = 1;
16318 mips_opts.fp32 = 1;
16319 break;
af7ee8bf
CD
16320 case ISA_MIPS3:
16321 case ISA_MIPS4:
16322 case ISA_MIPS5:
16323 case ISA_MIPS64:
5f74bc13 16324 case ISA_MIPS64R2:
98d3f06f
KH
16325 mips_opts.gp32 = 0;
16326 mips_opts.fp32 = 0;
16327 break;
16328 default:
16329 as_bad (_("unknown ISA level %s"), name + 4);
16330 break;
16331 }
af7ee8bf 16332 if (reset)
98d3f06f 16333 {
af7ee8bf
CD
16334 mips_opts.gp32 = file_mips_gp32;
16335 mips_opts.fp32 = file_mips_fp32;
98d3f06f 16336 }
252b5132
RH
16337 }
16338 else if (strcmp (name, "autoextend") == 0)
16339 mips_opts.noautoextend = 0;
16340 else if (strcmp (name, "noautoextend") == 0)
16341 mips_opts.noautoextend = 1;
16342 else if (strcmp (name, "push") == 0)
16343 {
16344 struct mips_option_stack *s;
16345
16346 s = (struct mips_option_stack *) xmalloc (sizeof *s);
16347 s->next = mips_opts_stack;
16348 s->options = mips_opts;
16349 mips_opts_stack = s;
16350 }
16351 else if (strcmp (name, "pop") == 0)
16352 {
16353 struct mips_option_stack *s;
16354
16355 s = mips_opts_stack;
16356 if (s == NULL)
16357 as_bad (_(".set pop with no .set push"));
16358 else
16359 {
16360 /* If we're changing the reorder mode we need to handle
16361 delay slots correctly. */
16362 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16363 start_noreorder ();
252b5132 16364 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16365 end_noreorder ();
252b5132
RH
16366
16367 mips_opts = s->options;
16368 mips_opts_stack = s->next;
16369 free (s);
16370 }
16371 }
aed1a261
RS
16372 else if (strcmp (name, "sym32") == 0)
16373 mips_opts.sym32 = TRUE;
16374 else if (strcmp (name, "nosym32") == 0)
16375 mips_opts.sym32 = FALSE;
e6559e01
JM
16376 else if (strchr (name, ','))
16377 {
16378 /* Generic ".set" directive; use the generic handler. */
16379 *input_line_pointer = ch;
16380 input_line_pointer = name;
16381 s_set (0);
16382 return;
16383 }
252b5132
RH
16384 else
16385 {
16386 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16387 }
16388 *input_line_pointer = ch;
16389 demand_empty_rest_of_line ();
16390}
16391
16392/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16393 .option pic2. It means to generate SVR4 PIC calls. */
16394
16395static void
17a2f251 16396s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16397{
16398 mips_pic = SVR4_PIC;
143d77c5 16399 mips_abicalls = TRUE;
4d0d148d
TS
16400
16401 if (g_switch_seen && g_switch_value != 0)
16402 as_warn (_("-G may not be used with SVR4 PIC code"));
16403 g_switch_value = 0;
16404
252b5132
RH
16405 bfd_set_gp_size (stdoutput, 0);
16406 demand_empty_rest_of_line ();
16407}
16408
16409/* Handle the .cpload pseudo-op. This is used when generating SVR4
16410 PIC code. It sets the $gp register for the function based on the
16411 function address, which is in the register named in the argument.
16412 This uses a relocation against _gp_disp, which is handled specially
16413 by the linker. The result is:
16414 lui $gp,%hi(_gp_disp)
16415 addiu $gp,$gp,%lo(_gp_disp)
16416 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16417 The .cpload argument is normally $25 == $t9.
16418
16419 The -mno-shared option changes this to:
bbe506e8
TS
16420 lui $gp,%hi(__gnu_local_gp)
16421 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16422 and the argument is ignored. This saves an instruction, but the
16423 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16424 address for __gnu_local_gp. Thus code assembled with -mno-shared
16425 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16426
16427static void
17a2f251 16428s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16429{
16430 expressionS ex;
aa6975fb
ILT
16431 int reg;
16432 int in_shared;
252b5132 16433
6478892d
TS
16434 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16435 .cpload is ignored. */
16436 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16437 {
16438 s_ignore (0);
16439 return;
16440 }
16441
d3ecfc59 16442 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16443 if (mips_opts.noreorder == 0)
16444 as_warn (_(".cpload not in noreorder section"));
16445
aa6975fb
ILT
16446 reg = tc_get_register (0);
16447
16448 /* If we need to produce a 64-bit address, we are better off using
16449 the default instruction sequence. */
aed1a261 16450 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16451
252b5132 16452 ex.X_op = O_symbol;
bbe506e8
TS
16453 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16454 "__gnu_local_gp");
252b5132
RH
16455 ex.X_op_symbol = NULL;
16456 ex.X_add_number = 0;
16457
16458 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16459 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16460
584892a6 16461 macro_start ();
67c0d1eb
RS
16462 macro_build_lui (&ex, mips_gp_register);
16463 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16464 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16465 if (in_shared)
16466 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16467 mips_gp_register, reg);
584892a6 16468 macro_end ();
252b5132
RH
16469
16470 demand_empty_rest_of_line ();
16471}
16472
6478892d
TS
16473/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16474 .cpsetup $reg1, offset|$reg2, label
16475
16476 If offset is given, this results in:
16477 sd $gp, offset($sp)
956cd1d6 16478 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16479 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16480 daddu $gp, $gp, $reg1
6478892d
TS
16481
16482 If $reg2 is given, this results in:
16483 daddu $reg2, $gp, $0
956cd1d6 16484 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16485 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16486 daddu $gp, $gp, $reg1
aa6975fb
ILT
16487 $reg1 is normally $25 == $t9.
16488
16489 The -mno-shared option replaces the last three instructions with
16490 lui $gp,%hi(_gp)
54f4ddb3 16491 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16492
6478892d 16493static void
17a2f251 16494s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16495{
16496 expressionS ex_off;
16497 expressionS ex_sym;
16498 int reg1;
6478892d 16499
8586fc66 16500 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16501 We also need NewABI support. */
16502 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16503 {
16504 s_ignore (0);
16505 return;
16506 }
16507
16508 reg1 = tc_get_register (0);
16509 SKIP_WHITESPACE ();
16510 if (*input_line_pointer != ',')
16511 {
16512 as_bad (_("missing argument separator ',' for .cpsetup"));
16513 return;
16514 }
16515 else
80245285 16516 ++input_line_pointer;
6478892d
TS
16517 SKIP_WHITESPACE ();
16518 if (*input_line_pointer == '$')
80245285
TS
16519 {
16520 mips_cpreturn_register = tc_get_register (0);
16521 mips_cpreturn_offset = -1;
16522 }
6478892d 16523 else
80245285
TS
16524 {
16525 mips_cpreturn_offset = get_absolute_expression ();
16526 mips_cpreturn_register = -1;
16527 }
6478892d
TS
16528 SKIP_WHITESPACE ();
16529 if (*input_line_pointer != ',')
16530 {
16531 as_bad (_("missing argument separator ',' for .cpsetup"));
16532 return;
16533 }
16534 else
f9419b05 16535 ++input_line_pointer;
6478892d 16536 SKIP_WHITESPACE ();
f21f8242 16537 expression (&ex_sym);
6478892d 16538
584892a6 16539 macro_start ();
6478892d
TS
16540 if (mips_cpreturn_register == -1)
16541 {
16542 ex_off.X_op = O_constant;
16543 ex_off.X_add_symbol = NULL;
16544 ex_off.X_op_symbol = NULL;
16545 ex_off.X_add_number = mips_cpreturn_offset;
16546
67c0d1eb 16547 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16548 BFD_RELOC_LO16, SP);
6478892d
TS
16549 }
16550 else
67c0d1eb 16551 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 16552 mips_gp_register, 0);
6478892d 16553
aed1a261 16554 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16555 {
df58fc94 16556 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16557 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16558 BFD_RELOC_HI16_S);
16559
16560 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16561 mips_gp_register, -1, BFD_RELOC_GPREL16,
16562 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16563
16564 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16565 mips_gp_register, reg1);
16566 }
16567 else
16568 {
16569 expressionS ex;
16570
16571 ex.X_op = O_symbol;
4184909a 16572 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16573 ex.X_op_symbol = NULL;
16574 ex.X_add_number = 0;
6e1304d8 16575
aa6975fb
ILT
16576 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16577 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16578
16579 macro_build_lui (&ex, mips_gp_register);
16580 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16581 mips_gp_register, BFD_RELOC_LO16);
16582 }
f21f8242 16583
584892a6 16584 macro_end ();
6478892d
TS
16585
16586 demand_empty_rest_of_line ();
16587}
16588
16589static void
17a2f251 16590s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16591{
16592 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16593 .cplocal is ignored. */
6478892d
TS
16594 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16595 {
16596 s_ignore (0);
16597 return;
16598 }
16599
16600 mips_gp_register = tc_get_register (0);
85b51719 16601 demand_empty_rest_of_line ();
6478892d
TS
16602}
16603
252b5132
RH
16604/* Handle the .cprestore pseudo-op. This stores $gp into a given
16605 offset from $sp. The offset is remembered, and after making a PIC
16606 call $gp is restored from that location. */
16607
16608static void
17a2f251 16609s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16610{
16611 expressionS ex;
252b5132 16612
6478892d 16613 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16614 .cprestore is ignored. */
6478892d 16615 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16616 {
16617 s_ignore (0);
16618 return;
16619 }
16620
16621 mips_cprestore_offset = get_absolute_expression ();
7a621144 16622 mips_cprestore_valid = 1;
252b5132
RH
16623
16624 ex.X_op = O_constant;
16625 ex.X_add_symbol = NULL;
16626 ex.X_op_symbol = NULL;
16627 ex.X_add_number = mips_cprestore_offset;
16628
584892a6 16629 macro_start ();
67c0d1eb
RS
16630 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16631 SP, HAVE_64BIT_ADDRESSES);
584892a6 16632 macro_end ();
252b5132
RH
16633
16634 demand_empty_rest_of_line ();
16635}
16636
6478892d 16637/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16638 was given in the preceding .cpsetup, it results in:
6478892d 16639 ld $gp, offset($sp)
76b3015f 16640
6478892d 16641 If a register $reg2 was given there, it results in:
54f4ddb3
TS
16642 daddu $gp, $reg2, $0 */
16643
6478892d 16644static void
17a2f251 16645s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16646{
16647 expressionS ex;
6478892d
TS
16648
16649 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16650 We also need NewABI support. */
16651 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16652 {
16653 s_ignore (0);
16654 return;
16655 }
16656
584892a6 16657 macro_start ();
6478892d
TS
16658 if (mips_cpreturn_register == -1)
16659 {
16660 ex.X_op = O_constant;
16661 ex.X_add_symbol = NULL;
16662 ex.X_op_symbol = NULL;
16663 ex.X_add_number = mips_cpreturn_offset;
16664
67c0d1eb 16665 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
16666 }
16667 else
67c0d1eb 16668 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 16669 mips_cpreturn_register, 0);
584892a6 16670 macro_end ();
6478892d
TS
16671
16672 demand_empty_rest_of_line ();
16673}
16674
d0f13682
CLT
16675/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16676 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16677 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16678 debug information or MIPS16 TLS. */
741d6ea8
JM
16679
16680static void
d0f13682
CLT
16681s_tls_rel_directive (const size_t bytes, const char *dirstr,
16682 bfd_reloc_code_real_type rtype)
741d6ea8
JM
16683{
16684 expressionS ex;
16685 char *p;
16686
16687 expression (&ex);
16688
16689 if (ex.X_op != O_symbol)
16690 {
d0f13682 16691 as_bad (_("Unsupported use of %s"), dirstr);
741d6ea8
JM
16692 ignore_rest_of_line ();
16693 }
16694
16695 p = frag_more (bytes);
16696 md_number_to_chars (p, 0, bytes);
d0f13682 16697 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 16698 demand_empty_rest_of_line ();
de64cffd 16699 mips_clear_insn_labels ();
741d6ea8
JM
16700}
16701
16702/* Handle .dtprelword. */
16703
16704static void
16705s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16706{
d0f13682 16707 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
16708}
16709
16710/* Handle .dtpreldword. */
16711
16712static void
16713s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16714{
d0f13682
CLT
16715 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16716}
16717
16718/* Handle .tprelword. */
16719
16720static void
16721s_tprelword (int ignore ATTRIBUTE_UNUSED)
16722{
16723 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16724}
16725
16726/* Handle .tpreldword. */
16727
16728static void
16729s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16730{
16731 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
16732}
16733
6478892d
TS
16734/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16735 code. It sets the offset to use in gp_rel relocations. */
16736
16737static void
17a2f251 16738s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16739{
16740 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16741 We also need NewABI support. */
16742 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16743 {
16744 s_ignore (0);
16745 return;
16746 }
16747
def2e0dd 16748 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
16749
16750 demand_empty_rest_of_line ();
16751}
16752
252b5132
RH
16753/* Handle the .gpword pseudo-op. This is used when generating PIC
16754 code. It generates a 32 bit GP relative reloc. */
16755
16756static void
17a2f251 16757s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 16758{
a8dbcb85
TS
16759 segment_info_type *si;
16760 struct insn_label_list *l;
252b5132
RH
16761 expressionS ex;
16762 char *p;
16763
16764 /* When not generating PIC code, this is treated as .word. */
16765 if (mips_pic != SVR4_PIC)
16766 {
16767 s_cons (2);
16768 return;
16769 }
16770
a8dbcb85
TS
16771 si = seg_info (now_seg);
16772 l = si->label_list;
7d10b47d 16773 mips_emit_delays ();
252b5132 16774 if (auto_align)
462427c4 16775 mips_align (2, 0, l);
252b5132
RH
16776
16777 expression (&ex);
a1facbec 16778 mips_clear_insn_labels ();
252b5132
RH
16779
16780 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16781 {
16782 as_bad (_("Unsupported use of .gpword"));
16783 ignore_rest_of_line ();
16784 }
16785
16786 p = frag_more (4);
17a2f251 16787 md_number_to_chars (p, 0, 4);
b34976b6 16788 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 16789 BFD_RELOC_GPREL32);
252b5132
RH
16790
16791 demand_empty_rest_of_line ();
16792}
16793
10181a0d 16794static void
17a2f251 16795s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 16796{
a8dbcb85
TS
16797 segment_info_type *si;
16798 struct insn_label_list *l;
10181a0d
AO
16799 expressionS ex;
16800 char *p;
16801
16802 /* When not generating PIC code, this is treated as .dword. */
16803 if (mips_pic != SVR4_PIC)
16804 {
16805 s_cons (3);
16806 return;
16807 }
16808
a8dbcb85
TS
16809 si = seg_info (now_seg);
16810 l = si->label_list;
7d10b47d 16811 mips_emit_delays ();
10181a0d 16812 if (auto_align)
462427c4 16813 mips_align (3, 0, l);
10181a0d
AO
16814
16815 expression (&ex);
a1facbec 16816 mips_clear_insn_labels ();
10181a0d
AO
16817
16818 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16819 {
16820 as_bad (_("Unsupported use of .gpdword"));
16821 ignore_rest_of_line ();
16822 }
16823
16824 p = frag_more (8);
17a2f251 16825 md_number_to_chars (p, 0, 8);
a105a300 16826 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 16827 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
16828
16829 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
16830 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
16831 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
16832
16833 demand_empty_rest_of_line ();
16834}
16835
252b5132
RH
16836/* Handle the .cpadd pseudo-op. This is used when dealing with switch
16837 tables in SVR4 PIC code. */
16838
16839static void
17a2f251 16840s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 16841{
252b5132
RH
16842 int reg;
16843
10181a0d
AO
16844 /* This is ignored when not generating SVR4 PIC code. */
16845 if (mips_pic != SVR4_PIC)
252b5132
RH
16846 {
16847 s_ignore (0);
16848 return;
16849 }
16850
16851 /* Add $gp to the register named as an argument. */
584892a6 16852 macro_start ();
252b5132 16853 reg = tc_get_register (0);
67c0d1eb 16854 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 16855 macro_end ();
252b5132 16856
bdaaa2e1 16857 demand_empty_rest_of_line ();
252b5132
RH
16858}
16859
16860/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 16861 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
16862 such as generating jalx instructions when needed. We also make
16863 them odd for the duration of the assembly, in order to generate the
16864 right sort of code. We will make them even in the adjust_symtab
16865 routine, while leaving them marked. This is convenient for the
16866 debugger and the disassembler. The linker knows to make them odd
16867 again. */
16868
16869static void
17a2f251 16870s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 16871{
df58fc94 16872 mips_mark_labels ();
252b5132
RH
16873
16874 demand_empty_rest_of_line ();
16875}
16876
16877/* Handle a .stabn directive. We need these in order to mark a label
16878 as being a mips16 text label correctly. Sometimes the compiler
16879 will emit a label, followed by a .stabn, and then switch sections.
16880 If the label and .stabn are in mips16 mode, then the label is
16881 really a mips16 text label. */
16882
16883static void
17a2f251 16884s_mips_stab (int type)
252b5132 16885{
f9419b05 16886 if (type == 'n')
df58fc94 16887 mips_mark_labels ();
252b5132
RH
16888
16889 s_stab (type);
16890}
16891
54f4ddb3 16892/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
16893
16894static void
17a2f251 16895s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16896{
16897 char *name;
16898 int c;
16899 symbolS *symbolP;
16900 expressionS exp;
16901
16902 name = input_line_pointer;
16903 c = get_symbol_end ();
16904 symbolP = symbol_find_or_make (name);
16905 S_SET_WEAK (symbolP);
16906 *input_line_pointer = c;
16907
16908 SKIP_WHITESPACE ();
16909
16910 if (! is_end_of_line[(unsigned char) *input_line_pointer])
16911 {
16912 if (S_IS_DEFINED (symbolP))
16913 {
20203fb9 16914 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
16915 S_GET_NAME (symbolP));
16916 ignore_rest_of_line ();
16917 return;
16918 }
bdaaa2e1 16919
252b5132
RH
16920 if (*input_line_pointer == ',')
16921 {
16922 ++input_line_pointer;
16923 SKIP_WHITESPACE ();
16924 }
bdaaa2e1 16925
252b5132
RH
16926 expression (&exp);
16927 if (exp.X_op != O_symbol)
16928 {
20203fb9 16929 as_bad (_("bad .weakext directive"));
98d3f06f 16930 ignore_rest_of_line ();
252b5132
RH
16931 return;
16932 }
49309057 16933 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
16934 }
16935
16936 demand_empty_rest_of_line ();
16937}
16938
16939/* Parse a register string into a number. Called from the ECOFF code
16940 to parse .frame. The argument is non-zero if this is the frame
16941 register, so that we can record it in mips_frame_reg. */
16942
16943int
17a2f251 16944tc_get_register (int frame)
252b5132 16945{
707bfff6 16946 unsigned int reg;
252b5132
RH
16947
16948 SKIP_WHITESPACE ();
707bfff6
TS
16949 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
16950 reg = 0;
252b5132 16951 if (frame)
7a621144
DJ
16952 {
16953 mips_frame_reg = reg != 0 ? reg : SP;
16954 mips_frame_reg_valid = 1;
16955 mips_cprestore_valid = 0;
16956 }
252b5132
RH
16957 return reg;
16958}
16959
16960valueT
17a2f251 16961md_section_align (asection *seg, valueT addr)
252b5132
RH
16962{
16963 int align = bfd_get_section_alignment (stdoutput, seg);
16964
b4c71f56
TS
16965 if (IS_ELF)
16966 {
16967 /* We don't need to align ELF sections to the full alignment.
16968 However, Irix 5 may prefer that we align them at least to a 16
16969 byte boundary. We don't bother to align the sections if we
16970 are targeted for an embedded system. */
c41e87e3 16971 if (strncmp (TARGET_OS, "elf", 3) == 0)
b4c71f56
TS
16972 return addr;
16973 if (align > 4)
16974 align = 4;
16975 }
252b5132
RH
16976
16977 return ((addr + (1 << align) - 1) & (-1 << align));
16978}
16979
16980/* Utility routine, called from above as well. If called while the
16981 input file is still being read, it's only an approximation. (For
16982 example, a symbol may later become defined which appeared to be
16983 undefined earlier.) */
16984
16985static int
17a2f251 16986nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
16987{
16988 if (sym == 0)
16989 return 0;
16990
4d0d148d 16991 if (g_switch_value > 0)
252b5132
RH
16992 {
16993 const char *symname;
16994 int change;
16995
c9914766 16996 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
16997 register. It can be if it is smaller than the -G size or if
16998 it is in the .sdata or .sbss section. Certain symbols can
c9914766 16999 not be referenced off the $gp, although it appears as though
252b5132
RH
17000 they can. */
17001 symname = S_GET_NAME (sym);
17002 if (symname != (const char *) NULL
17003 && (strcmp (symname, "eprol") == 0
17004 || strcmp (symname, "etext") == 0
17005 || strcmp (symname, "_gp") == 0
17006 || strcmp (symname, "edata") == 0
17007 || strcmp (symname, "_fbss") == 0
17008 || strcmp (symname, "_fdata") == 0
17009 || strcmp (symname, "_ftext") == 0
17010 || strcmp (symname, "end") == 0
17011 || strcmp (symname, "_gp_disp") == 0))
17012 change = 1;
17013 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17014 && (0
17015#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17016 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17017 && (symbol_get_obj (sym)->ecoff_extern_size
17018 <= g_switch_value))
252b5132
RH
17019#endif
17020 /* We must defer this decision until after the whole
17021 file has been read, since there might be a .extern
17022 after the first use of this symbol. */
17023 || (before_relaxing
17024#ifndef NO_ECOFF_DEBUGGING
49309057 17025 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17026#endif
17027 && S_GET_VALUE (sym) == 0)
17028 || (S_GET_VALUE (sym) != 0
17029 && S_GET_VALUE (sym) <= g_switch_value)))
17030 change = 0;
17031 else
17032 {
17033 const char *segname;
17034
17035 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17036 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17037 && strcmp (segname, ".lit4") != 0);
17038 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17039 && strcmp (segname, ".sbss") != 0
17040 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17041 && strncmp (segname, ".sbss.", 6) != 0
17042 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17043 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17044 }
17045 return change;
17046 }
17047 else
c9914766 17048 /* We are not optimizing for the $gp register. */
252b5132
RH
17049 return 1;
17050}
17051
5919d012
RS
17052
17053/* Return true if the given symbol should be considered local for SVR4 PIC. */
17054
17055static bfd_boolean
17a2f251 17056pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
17057{
17058 asection *symsec;
5919d012
RS
17059
17060 /* Handle the case of a symbol equated to another symbol. */
17061 while (symbol_equated_reloc_p (sym))
17062 {
17063 symbolS *n;
17064
5f0fe04b 17065 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17066 n = symbol_get_value_expression (sym)->X_add_symbol;
17067 if (n == sym)
17068 break;
17069 sym = n;
17070 }
17071
df1f3cda
DD
17072 if (symbol_section_p (sym))
17073 return TRUE;
17074
5919d012
RS
17075 symsec = S_GET_SEGMENT (sym);
17076
5919d012 17077 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17078 return (!bfd_is_und_section (symsec)
17079 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
17080 && !bfd_is_com_section (symsec)
17081 && !s_is_linkonce (sym, segtype)
5919d012
RS
17082#ifdef OBJ_ELF
17083 /* A global or weak symbol is treated as external. */
f43abd2b 17084 && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
5919d012
RS
17085#endif
17086 );
17087}
17088
17089
252b5132
RH
17090/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17091 extended opcode. SEC is the section the frag is in. */
17092
17093static int
17a2f251 17094mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
17095{
17096 int type;
3994f87e 17097 const struct mips16_immed_operand *op;
252b5132
RH
17098 offsetT val;
17099 int mintiny, maxtiny;
17100 segT symsec;
98aa84af 17101 fragS *sym_frag;
252b5132
RH
17102
17103 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17104 return 0;
17105 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17106 return 1;
17107
17108 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17109 op = mips16_immed_operands;
17110 while (op->type != type)
17111 {
17112 ++op;
9c2799c2 17113 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
17114 }
17115
17116 if (op->unsp)
17117 {
17118 if (type == '<' || type == '>' || type == '[' || type == ']')
17119 {
17120 mintiny = 1;
17121 maxtiny = 1 << op->nbits;
17122 }
17123 else
17124 {
17125 mintiny = 0;
17126 maxtiny = (1 << op->nbits) - 1;
17127 }
17128 }
17129 else
17130 {
17131 mintiny = - (1 << (op->nbits - 1));
17132 maxtiny = (1 << (op->nbits - 1)) - 1;
17133 }
17134
98aa84af 17135 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 17136 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 17137 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
17138
17139 if (op->pcrel)
17140 {
17141 addressT addr;
17142
17143 /* We won't have the section when we are called from
17144 mips_relax_frag. However, we will always have been called
17145 from md_estimate_size_before_relax first. If this is a
17146 branch to a different section, we mark it as such. If SEC is
17147 NULL, and the frag is not marked, then it must be a branch to
17148 the same section. */
17149 if (sec == NULL)
17150 {
17151 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17152 return 1;
17153 }
17154 else
17155 {
98aa84af 17156 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
17157 if (symsec != sec)
17158 {
17159 fragp->fr_subtype =
17160 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17161
17162 /* FIXME: We should support this, and let the linker
17163 catch branches and loads that are out of range. */
17164 as_bad_where (fragp->fr_file, fragp->fr_line,
17165 _("unsupported PC relative reference to different section"));
17166
17167 return 1;
17168 }
98aa84af
AM
17169 if (fragp != sym_frag && sym_frag->fr_address == 0)
17170 /* Assume non-extended on the first relaxation pass.
17171 The address we have calculated will be bogus if this is
17172 a forward branch to another frag, as the forward frag
17173 will have fr_address == 0. */
17174 return 0;
252b5132
RH
17175 }
17176
17177 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
17178 the same section. If the relax_marker of the symbol fragment
17179 differs from the relax_marker of this fragment, we have not
17180 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
17181 in STRETCH in order to get a better estimate of the address.
17182 This particularly matters because of the shift bits. */
17183 if (stretch != 0
98aa84af 17184 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
17185 {
17186 fragS *f;
17187
17188 /* Adjust stretch for any alignment frag. Note that if have
17189 been expanding the earlier code, the symbol may be
17190 defined in what appears to be an earlier frag. FIXME:
17191 This doesn't handle the fr_subtype field, which specifies
17192 a maximum number of bytes to skip when doing an
17193 alignment. */
98aa84af 17194 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
17195 {
17196 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17197 {
17198 if (stretch < 0)
17199 stretch = - ((- stretch)
17200 & ~ ((1 << (int) f->fr_offset) - 1));
17201 else
17202 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17203 if (stretch == 0)
17204 break;
17205 }
17206 }
17207 if (f != NULL)
17208 val += stretch;
17209 }
17210
17211 addr = fragp->fr_address + fragp->fr_fix;
17212
17213 /* The base address rules are complicated. The base address of
17214 a branch is the following instruction. The base address of a
17215 PC relative load or add is the instruction itself, but if it
17216 is in a delay slot (in which case it can not be extended) use
17217 the address of the instruction whose delay slot it is in. */
17218 if (type == 'p' || type == 'q')
17219 {
17220 addr += 2;
17221
17222 /* If we are currently assuming that this frag should be
17223 extended, then, the current address is two bytes
bdaaa2e1 17224 higher. */
252b5132
RH
17225 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17226 addr += 2;
17227
17228 /* Ignore the low bit in the target, since it will be set
17229 for a text label. */
17230 if ((val & 1) != 0)
17231 --val;
17232 }
17233 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17234 addr -= 4;
17235 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17236 addr -= 2;
17237
17238 val -= addr & ~ ((1 << op->shift) - 1);
17239
17240 /* Branch offsets have an implicit 0 in the lowest bit. */
17241 if (type == 'p' || type == 'q')
17242 val /= 2;
17243
17244 /* If any of the shifted bits are set, we must use an extended
17245 opcode. If the address depends on the size of this
17246 instruction, this can lead to a loop, so we arrange to always
17247 use an extended opcode. We only check this when we are in
17248 the main relaxation loop, when SEC is NULL. */
17249 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17250 {
17251 fragp->fr_subtype =
17252 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17253 return 1;
17254 }
17255
17256 /* If we are about to mark a frag as extended because the value
17257 is precisely maxtiny + 1, then there is a chance of an
17258 infinite loop as in the following code:
17259 la $4,foo
17260 .skip 1020
17261 .align 2
17262 foo:
17263 In this case when the la is extended, foo is 0x3fc bytes
17264 away, so the la can be shrunk, but then foo is 0x400 away, so
17265 the la must be extended. To avoid this loop, we mark the
17266 frag as extended if it was small, and is about to become
17267 extended with a value of maxtiny + 1. */
17268 if (val == ((maxtiny + 1) << op->shift)
17269 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17270 && sec == NULL)
17271 {
17272 fragp->fr_subtype =
17273 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17274 return 1;
17275 }
17276 }
17277 else if (symsec != absolute_section && sec != NULL)
17278 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17279
17280 if ((val & ((1 << op->shift) - 1)) != 0
17281 || val < (mintiny << op->shift)
17282 || val > (maxtiny << op->shift))
17283 return 1;
17284 else
17285 return 0;
17286}
17287
4a6a3df4
AO
17288/* Compute the length of a branch sequence, and adjust the
17289 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17290 worst-case length is computed, with UPDATE being used to indicate
17291 whether an unconditional (-1), branch-likely (+1) or regular (0)
17292 branch is to be computed. */
17293static int
17a2f251 17294relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17295{
b34976b6 17296 bfd_boolean toofar;
4a6a3df4
AO
17297 int length;
17298
17299 if (fragp
17300 && S_IS_DEFINED (fragp->fr_symbol)
17301 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17302 {
17303 addressT addr;
17304 offsetT val;
17305
17306 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17307
17308 addr = fragp->fr_address + fragp->fr_fix + 4;
17309
17310 val -= addr;
17311
17312 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17313 }
17314 else if (fragp)
17315 /* If the symbol is not defined or it's in a different segment,
17316 assume the user knows what's going on and emit a short
17317 branch. */
b34976b6 17318 toofar = FALSE;
4a6a3df4 17319 else
b34976b6 17320 toofar = TRUE;
4a6a3df4
AO
17321
17322 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17323 fragp->fr_subtype
66b3e8da
MR
17324 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17325 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17326 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17327 RELAX_BRANCH_LINK (fragp->fr_subtype),
17328 toofar);
17329
17330 length = 4;
17331 if (toofar)
17332 {
17333 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17334 length += 8;
17335
17336 if (mips_pic != NO_PIC)
17337 {
17338 /* Additional space for PIC loading of target address. */
17339 length += 8;
17340 if (mips_opts.isa == ISA_MIPS1)
17341 /* Additional space for $at-stabilizing nop. */
17342 length += 4;
17343 }
17344
17345 /* If branch is conditional. */
17346 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17347 length += 8;
17348 }
b34976b6 17349
4a6a3df4
AO
17350 return length;
17351}
17352
df58fc94
RS
17353/* Compute the length of a branch sequence, and adjust the
17354 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17355 worst-case length is computed, with UPDATE being used to indicate
17356 whether an unconditional (-1), or regular (0) branch is to be
17357 computed. */
17358
17359static int
17360relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17361{
17362 bfd_boolean toofar;
17363 int length;
17364
17365 if (fragp
17366 && S_IS_DEFINED (fragp->fr_symbol)
17367 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17368 {
17369 addressT addr;
17370 offsetT val;
17371
17372 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17373 /* Ignore the low bit in the target, since it will be set
17374 for a text label. */
17375 if ((val & 1) != 0)
17376 --val;
17377
17378 addr = fragp->fr_address + fragp->fr_fix + 4;
17379
17380 val -= addr;
17381
17382 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17383 }
17384 else if (fragp)
17385 /* If the symbol is not defined or it's in a different segment,
17386 assume the user knows what's going on and emit a short
17387 branch. */
17388 toofar = FALSE;
17389 else
17390 toofar = TRUE;
17391
17392 if (fragp && update
17393 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17394 fragp->fr_subtype = (toofar
17395 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17396 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17397
17398 length = 4;
17399 if (toofar)
17400 {
17401 bfd_boolean compact_known = fragp != NULL;
17402 bfd_boolean compact = FALSE;
17403 bfd_boolean uncond;
17404
17405 if (compact_known)
17406 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17407 if (fragp)
17408 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17409 else
17410 uncond = update < 0;
17411
17412 /* If label is out of range, we turn branch <br>:
17413
17414 <br> label # 4 bytes
17415 0:
17416
17417 into:
17418
17419 j label # 4 bytes
17420 nop # 2 bytes if compact && !PIC
17421 0:
17422 */
17423 if (mips_pic == NO_PIC && (!compact_known || compact))
17424 length += 2;
17425
17426 /* If assembling PIC code, we further turn:
17427
17428 j label # 4 bytes
17429
17430 into:
17431
17432 lw/ld at, %got(label)(gp) # 4 bytes
17433 d/addiu at, %lo(label) # 4 bytes
17434 jr/c at # 2 bytes
17435 */
17436 if (mips_pic != NO_PIC)
17437 length += 6;
17438
17439 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17440
17441 <brneg> 0f # 4 bytes
17442 nop # 2 bytes if !compact
17443 */
17444 if (!uncond)
17445 length += (compact_known && compact) ? 4 : 6;
17446 }
17447
17448 return length;
17449}
17450
17451/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17452 bit accordingly. */
17453
17454static int
17455relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17456{
17457 bfd_boolean toofar;
17458
df58fc94
RS
17459 if (fragp
17460 && S_IS_DEFINED (fragp->fr_symbol)
17461 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17462 {
17463 addressT addr;
17464 offsetT val;
17465 int type;
17466
17467 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17468 /* Ignore the low bit in the target, since it will be set
17469 for a text label. */
17470 if ((val & 1) != 0)
17471 --val;
17472
17473 /* Assume this is a 2-byte branch. */
17474 addr = fragp->fr_address + fragp->fr_fix + 2;
17475
17476 /* We try to avoid the infinite loop by not adding 2 more bytes for
17477 long branches. */
17478
17479 val -= addr;
17480
17481 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17482 if (type == 'D')
17483 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17484 else if (type == 'E')
17485 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17486 else
17487 abort ();
17488 }
17489 else
17490 /* If the symbol is not defined or it's in a different segment,
17491 we emit a normal 32-bit branch. */
17492 toofar = TRUE;
17493
17494 if (fragp && update
17495 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17496 fragp->fr_subtype
17497 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17498 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17499
17500 if (toofar)
17501 return 4;
17502
17503 return 2;
17504}
17505
252b5132
RH
17506/* Estimate the size of a frag before relaxing. Unless this is the
17507 mips16, we are not really relaxing here, and the final size is
17508 encoded in the subtype information. For the mips16, we have to
17509 decide whether we are using an extended opcode or not. */
17510
252b5132 17511int
17a2f251 17512md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17513{
5919d012 17514 int change;
252b5132 17515
4a6a3df4
AO
17516 if (RELAX_BRANCH_P (fragp->fr_subtype))
17517 {
17518
b34976b6
AM
17519 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17520
4a6a3df4
AO
17521 return fragp->fr_var;
17522 }
17523
252b5132 17524 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
17525 /* We don't want to modify the EXTENDED bit here; it might get us
17526 into infinite loops. We change it only in mips_relax_frag(). */
17527 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 17528
df58fc94
RS
17529 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17530 {
17531 int length = 4;
17532
17533 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17534 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17535 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17536 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17537 fragp->fr_var = length;
17538
17539 return length;
17540 }
17541
252b5132 17542 if (mips_pic == NO_PIC)
5919d012 17543 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 17544 else if (mips_pic == SVR4_PIC)
5919d012 17545 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
17546 else if (mips_pic == VXWORKS_PIC)
17547 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17548 change = 0;
252b5132
RH
17549 else
17550 abort ();
17551
17552 if (change)
17553 {
4d7206a2 17554 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 17555 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 17556 }
4d7206a2
RS
17557 else
17558 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
17559}
17560
17561/* This is called to see whether a reloc against a defined symbol
de7e6852 17562 should be converted into a reloc against a section. */
252b5132
RH
17563
17564int
17a2f251 17565mips_fix_adjustable (fixS *fixp)
252b5132 17566{
252b5132
RH
17567 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17568 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17569 return 0;
a161fe53 17570
252b5132
RH
17571 if (fixp->fx_addsy == NULL)
17572 return 1;
a161fe53 17573
de7e6852
RS
17574 /* If symbol SYM is in a mergeable section, relocations of the form
17575 SYM + 0 can usually be made section-relative. The mergeable data
17576 is then identified by the section offset rather than by the symbol.
17577
17578 However, if we're generating REL LO16 relocations, the offset is split
17579 between the LO16 and parterning high part relocation. The linker will
17580 need to recalculate the complete offset in order to correctly identify
17581 the merge data.
17582
17583 The linker has traditionally not looked for the parterning high part
17584 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17585 placed anywhere. Rather than break backwards compatibility by changing
17586 this, it seems better not to force the issue, and instead keep the
17587 original symbol. This will work with either linker behavior. */
738e5348 17588 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 17589 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
17590 && HAVE_IN_PLACE_ADDENDS
17591 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17592 return 0;
17593
ce70d90a
MR
17594 /* There is no place to store an in-place offset for JALR relocations.
17595 Likewise an in-range offset of PC-relative relocations may overflow
17596 the in-place relocatable field if recalculated against the start
17597 address of the symbol's containing section. */
17598 if (HAVE_IN_PLACE_ADDENDS
df58fc94 17599 && (fixp->fx_pcrel || jalr_reloc_p (fixp->fx_r_type)))
1180b5a4
RS
17600 return 0;
17601
252b5132 17602#ifdef OBJ_ELF
b314ec0e
RS
17603 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17604 to a floating-point stub. The same is true for non-R_MIPS16_26
17605 relocations against MIPS16 functions; in this case, the stub becomes
17606 the function's canonical address.
17607
17608 Floating-point stubs are stored in unique .mips16.call.* or
17609 .mips16.fn.* sections. If a stub T for function F is in section S,
17610 the first relocation in section S must be against F; this is how the
17611 linker determines the target function. All relocations that might
17612 resolve to T must also be against F. We therefore have the following
17613 restrictions, which are given in an intentionally-redundant way:
17614
17615 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17616 symbols.
17617
17618 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17619 if that stub might be used.
17620
17621 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17622 symbols.
17623
17624 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17625 that stub might be used.
17626
17627 There is a further restriction:
17628
df58fc94
RS
17629 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17630 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17631 targets with in-place addends; the relocation field cannot
b314ec0e
RS
17632 encode the low bit.
17633
df58fc94
RS
17634 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17635 against a MIPS16 symbol. We deal with (5) by by not reducing any
17636 such relocations on REL targets.
b314ec0e
RS
17637
17638 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17639 relocation against some symbol R, no relocation against R may be
17640 reduced. (Note that this deals with (2) as well as (1) because
17641 relocations against global symbols will never be reduced on ELF
17642 targets.) This approach is a little simpler than trying to detect
17643 stub sections, and gives the "all or nothing" per-symbol consistency
17644 that we have for MIPS16 symbols. */
f43abd2b 17645 if (IS_ELF
b314ec0e 17646 && fixp->fx_subsy == NULL
30c09090 17647 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
df58fc94
RS
17648 || *symbol_get_tc (fixp->fx_addsy)
17649 || (HAVE_IN_PLACE_ADDENDS
17650 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17651 && jmp_reloc_p (fixp->fx_r_type))))
252b5132
RH
17652 return 0;
17653#endif
a161fe53 17654
252b5132
RH
17655 return 1;
17656}
17657
17658/* Translate internal representation of relocation info to BFD target
17659 format. */
17660
17661arelent **
17a2f251 17662tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
17663{
17664 static arelent *retval[4];
17665 arelent *reloc;
17666 bfd_reloc_code_real_type code;
17667
4b0cff4e
TS
17668 memset (retval, 0, sizeof(retval));
17669 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
17670 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
17671 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
17672 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17673
bad36eac
DJ
17674 if (fixp->fx_pcrel)
17675 {
df58fc94
RS
17676 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17677 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17678 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
17679 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
bad36eac
DJ
17680
17681 /* At this point, fx_addnumber is "symbol offset - pcrel address".
17682 Relocations want only the symbol offset. */
17683 reloc->addend = fixp->fx_addnumber + reloc->address;
f43abd2b 17684 if (!IS_ELF)
bad36eac
DJ
17685 {
17686 /* A gruesome hack which is a result of the gruesome gas
17687 reloc handling. What's worse, for COFF (as opposed to
17688 ECOFF), we might need yet another copy of reloc->address.
17689 See bfd_install_relocation. */
17690 reloc->addend += reloc->address;
17691 }
17692 }
17693 else
17694 reloc->addend = fixp->fx_addnumber;
252b5132 17695
438c16b8
TS
17696 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17697 entry to be used in the relocation's section offset. */
17698 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
17699 {
17700 reloc->address = reloc->addend;
17701 reloc->addend = 0;
17702 }
17703
252b5132 17704 code = fixp->fx_r_type;
252b5132 17705
bad36eac 17706 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
17707 if (reloc->howto == NULL)
17708 {
17709 as_bad_where (fixp->fx_file, fixp->fx_line,
17710 _("Can not represent %s relocation in this object file format"),
17711 bfd_get_reloc_code_name (code));
17712 retval[0] = NULL;
17713 }
17714
17715 return retval;
17716}
17717
17718/* Relax a machine dependent frag. This returns the amount by which
17719 the current size of the frag should change. */
17720
17721int
17a2f251 17722mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 17723{
4a6a3df4
AO
17724 if (RELAX_BRANCH_P (fragp->fr_subtype))
17725 {
17726 offsetT old_var = fragp->fr_var;
b34976b6
AM
17727
17728 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
17729
17730 return fragp->fr_var - old_var;
17731 }
17732
df58fc94
RS
17733 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17734 {
17735 offsetT old_var = fragp->fr_var;
17736 offsetT new_var = 4;
17737
17738 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17739 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17740 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17741 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17742 fragp->fr_var = new_var;
17743
17744 return new_var - old_var;
17745 }
17746
252b5132
RH
17747 if (! RELAX_MIPS16_P (fragp->fr_subtype))
17748 return 0;
17749
c4e7957c 17750 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
17751 {
17752 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17753 return 0;
17754 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17755 return 2;
17756 }
17757 else
17758 {
17759 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17760 return 0;
17761 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17762 return -2;
17763 }
17764
17765 return 0;
17766}
17767
17768/* Convert a machine dependent frag. */
17769
17770void
17a2f251 17771md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 17772{
4a6a3df4
AO
17773 if (RELAX_BRANCH_P (fragp->fr_subtype))
17774 {
17775 bfd_byte *buf;
17776 unsigned long insn;
17777 expressionS exp;
17778 fixS *fixp;
b34976b6 17779
4a6a3df4
AO
17780 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
17781
17782 if (target_big_endian)
17783 insn = bfd_getb32 (buf);
17784 else
17785 insn = bfd_getl32 (buf);
b34976b6 17786
4a6a3df4
AO
17787 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17788 {
17789 /* We generate a fixup instead of applying it right now
17790 because, if there are linker relaxations, we're going to
17791 need the relocations. */
17792 exp.X_op = O_symbol;
17793 exp.X_add_symbol = fragp->fr_symbol;
17794 exp.X_add_number = fragp->fr_offset;
17795
17796 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 17797 4, &exp, TRUE, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
17798 fixp->fx_file = fragp->fr_file;
17799 fixp->fx_line = fragp->fr_line;
b34976b6 17800
2132e3a3 17801 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
17802 buf += 4;
17803 }
17804 else
17805 {
17806 int i;
17807
17808 as_warn_where (fragp->fr_file, fragp->fr_line,
5c4f07ba 17809 _("Relaxed out-of-range branch into a jump"));
4a6a3df4
AO
17810
17811 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
17812 goto uncond;
17813
17814 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17815 {
17816 /* Reverse the branch. */
17817 switch ((insn >> 28) & 0xf)
17818 {
17819 case 4:
17820 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
17821 have the condition reversed by tweaking a single
17822 bit, and their opcodes all have 0x4???????. */
9c2799c2 17823 gas_assert ((insn & 0xf1000000) == 0x41000000);
4a6a3df4
AO
17824 insn ^= 0x00010000;
17825 break;
17826
17827 case 0:
17828 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 17829 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 17830 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
17831 insn ^= 0x00010000;
17832 break;
b34976b6 17833
4a6a3df4
AO
17834 case 1:
17835 /* beq 0x10000000 bne 0x14000000
54f4ddb3 17836 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
17837 insn ^= 0x04000000;
17838 break;
17839
17840 default:
17841 abort ();
17842 }
17843 }
17844
17845 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
17846 {
17847 /* Clear the and-link bit. */
9c2799c2 17848 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 17849
54f4ddb3
TS
17850 /* bltzal 0x04100000 bgezal 0x04110000
17851 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
17852 insn &= ~0x00100000;
17853 }
17854
17855 /* Branch over the branch (if the branch was likely) or the
17856 full jump (not likely case). Compute the offset from the
17857 current instruction to branch to. */
17858 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17859 i = 16;
17860 else
17861 {
17862 /* How many bytes in instructions we've already emitted? */
17863 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
17864 /* How many bytes in instructions from here to the end? */
17865 i = fragp->fr_var - i;
17866 }
17867 /* Convert to instruction count. */
17868 i >>= 2;
17869 /* Branch counts from the next instruction. */
b34976b6 17870 i--;
4a6a3df4
AO
17871 insn |= i;
17872 /* Branch over the jump. */
2132e3a3 17873 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
17874 buf += 4;
17875
54f4ddb3 17876 /* nop */
2132e3a3 17877 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
17878 buf += 4;
17879
17880 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17881 {
17882 /* beql $0, $0, 2f */
17883 insn = 0x50000000;
17884 /* Compute the PC offset from the current instruction to
17885 the end of the variable frag. */
17886 /* How many bytes in instructions we've already emitted? */
17887 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
17888 /* How many bytes in instructions from here to the end? */
17889 i = fragp->fr_var - i;
17890 /* Convert to instruction count. */
17891 i >>= 2;
17892 /* Don't decrement i, because we want to branch over the
17893 delay slot. */
17894
17895 insn |= i;
2132e3a3 17896 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
17897 buf += 4;
17898
2132e3a3 17899 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
17900 buf += 4;
17901 }
17902
17903 uncond:
17904 if (mips_pic == NO_PIC)
17905 {
17906 /* j or jal. */
17907 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
17908 ? 0x0c000000 : 0x08000000);
17909 exp.X_op = O_symbol;
17910 exp.X_add_symbol = fragp->fr_symbol;
17911 exp.X_add_number = fragp->fr_offset;
17912
17913 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 17914 4, &exp, FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
17915 fixp->fx_file = fragp->fr_file;
17916 fixp->fx_line = fragp->fr_line;
17917
2132e3a3 17918 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
17919 buf += 4;
17920 }
17921 else
17922 {
66b3e8da
MR
17923 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
17924
4a6a3df4 17925 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
17926 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
17927 insn |= at << OP_SH_RT;
4a6a3df4
AO
17928 exp.X_op = O_symbol;
17929 exp.X_add_symbol = fragp->fr_symbol;
17930 exp.X_add_number = fragp->fr_offset;
17931
17932 if (fragp->fr_offset)
17933 {
17934 exp.X_add_symbol = make_expr_symbol (&exp);
17935 exp.X_add_number = 0;
17936 }
17937
17938 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 17939 4, &exp, FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
17940 fixp->fx_file = fragp->fr_file;
17941 fixp->fx_line = fragp->fr_line;
17942
2132e3a3 17943 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4 17944 buf += 4;
b34976b6 17945
4a6a3df4
AO
17946 if (mips_opts.isa == ISA_MIPS1)
17947 {
17948 /* nop */
2132e3a3 17949 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
17950 buf += 4;
17951 }
17952
17953 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
17954 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
17955 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4
AO
17956
17957 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 17958 4, &exp, FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
17959 fixp->fx_file = fragp->fr_file;
17960 fixp->fx_line = fragp->fr_line;
b34976b6 17961
2132e3a3 17962 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
17963 buf += 4;
17964
17965 /* j(al)r $at. */
17966 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 17967 insn = 0x0000f809;
4a6a3df4 17968 else
66b3e8da
MR
17969 insn = 0x00000008;
17970 insn |= at << OP_SH_RS;
4a6a3df4 17971
2132e3a3 17972 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
17973 buf += 4;
17974 }
17975 }
17976
9c2799c2 17977 gas_assert (buf == (bfd_byte *)fragp->fr_literal
4a6a3df4
AO
17978 + fragp->fr_fix + fragp->fr_var);
17979
17980 fragp->fr_fix += fragp->fr_var;
17981
17982 return;
17983 }
17984
df58fc94
RS
17985 /* Relax microMIPS branches. */
17986 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17987 {
17988 bfd_byte *buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
17989 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17990 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17991 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 17992 bfd_boolean short_ds;
df58fc94
RS
17993 unsigned long insn;
17994 expressionS exp;
17995 fixS *fixp;
17996
17997 exp.X_op = O_symbol;
17998 exp.X_add_symbol = fragp->fr_symbol;
17999 exp.X_add_number = fragp->fr_offset;
18000
18001 fragp->fr_fix += fragp->fr_var;
18002
18003 /* Handle 16-bit branches that fit or are forced to fit. */
18004 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18005 {
18006 /* We generate a fixup instead of applying it right now,
18007 because if there is linker relaxation, we're going to
18008 need the relocations. */
18009 if (type == 'D')
18010 fixp = fix_new_exp (fragp,
18011 buf - (bfd_byte *) fragp->fr_literal,
18012 2, &exp, TRUE,
18013 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18014 else if (type == 'E')
18015 fixp = fix_new_exp (fragp,
18016 buf - (bfd_byte *) fragp->fr_literal,
18017 2, &exp, TRUE,
18018 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18019 else
18020 abort ();
18021
18022 fixp->fx_file = fragp->fr_file;
18023 fixp->fx_line = fragp->fr_line;
18024
18025 /* These relocations can have an addend that won't fit in
18026 2 octets. */
18027 fixp->fx_no_overflow = 1;
18028
18029 return;
18030 }
18031
2309ddf2 18032 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18033 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18034 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18035 {
18036 /* We generate a fixup instead of applying it right now,
18037 because if there is linker relaxation, we're going to
18038 need the relocations. */
18039 fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18040 4, &exp, TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18041 fixp->fx_file = fragp->fr_file;
18042 fixp->fx_line = fragp->fr_line;
18043
18044 if (type == 0)
18045 return;
18046 }
18047
18048 /* Relax 16-bit branches to 32-bit branches. */
18049 if (type != 0)
18050 {
18051 if (target_big_endian)
18052 insn = bfd_getb16 (buf);
18053 else
18054 insn = bfd_getl16 (buf);
18055
18056 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18057 insn = 0x94000000; /* beq */
18058 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18059 {
18060 unsigned long regno;
18061
18062 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18063 regno = micromips_to_32_reg_d_map [regno];
18064 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18065 insn |= regno << MICROMIPSOP_SH_RS;
18066 }
18067 else
18068 abort ();
18069
18070 /* Nothing else to do, just write it out. */
18071 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18072 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18073 {
18074 md_number_to_chars ((char *) buf, insn >> 16, 2);
18075 buf += 2;
18076 md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18077 buf += 2;
18078
18079 gas_assert (buf == ((bfd_byte *) fragp->fr_literal
18080 + fragp->fr_fix));
18081 return;
18082 }
18083 }
18084 else
18085 {
18086 unsigned long next;
18087
18088 if (target_big_endian)
18089 {
18090 insn = bfd_getb16 (buf);
18091 next = bfd_getb16 (buf + 2);
18092 }
18093 else
18094 {
18095 insn = bfd_getl16 (buf);
18096 next = bfd_getl16 (buf + 2);
18097 }
18098 insn = (insn << 16) | next;
18099 }
18100
18101 /* Relax 32-bit branches to a sequence of instructions. */
18102 as_warn_where (fragp->fr_file, fragp->fr_line,
18103 _("Relaxed out-of-range branch into a jump"));
18104
2309ddf2
MR
18105 /* Set the short-delay-slot bit. */
18106 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
18107
18108 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18109 {
18110 symbolS *l;
18111
18112 /* Reverse the branch. */
18113 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18114 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18115 insn ^= 0x20000000;
18116 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18117 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18118 || (insn & 0xffe00000) == 0x40800000 /* blez */
18119 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18120 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18121 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18122 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18123 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18124 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18125 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18126 insn ^= 0x00400000;
18127 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18128 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18129 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18130 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18131 insn ^= 0x00200000;
18132 else
18133 abort ();
18134
18135 if (al)
18136 {
18137 /* Clear the and-link and short-delay-slot bits. */
18138 gas_assert ((insn & 0xfda00000) == 0x40200000);
18139
18140 /* bltzal 0x40200000 bgezal 0x40600000 */
18141 /* bltzals 0x42200000 bgezals 0x42600000 */
18142 insn &= ~0x02200000;
18143 }
18144
18145 /* Make a label at the end for use with the branch. */
18146 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18147 micromips_label_inc ();
18148#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
18149 if (IS_ELF)
18150 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18151#endif
18152
18153 /* Refer to it. */
18154 fixp = fix_new (fragp, buf - (bfd_byte *) fragp->fr_literal,
18155 4, l, 0, TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18156 fixp->fx_file = fragp->fr_file;
18157 fixp->fx_line = fragp->fr_line;
18158
18159 /* Branch over the jump. */
18160 md_number_to_chars ((char *) buf, insn >> 16, 2);
18161 buf += 2;
18162 md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18163 buf += 2;
18164
18165 if (!compact)
18166 {
18167 /* nop */
18168 insn = 0x0c00;
18169 md_number_to_chars ((char *) buf, insn, 2);
18170 buf += 2;
18171 }
18172 }
18173
18174 if (mips_pic == NO_PIC)
18175 {
2309ddf2
MR
18176 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
18177
df58fc94
RS
18178 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18179 insn = al ? jal : 0xd4000000;
18180
18181 fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18182 4, &exp, FALSE, BFD_RELOC_MICROMIPS_JMP);
18183 fixp->fx_file = fragp->fr_file;
18184 fixp->fx_line = fragp->fr_line;
18185
18186 md_number_to_chars ((char *) buf, insn >> 16, 2);
18187 buf += 2;
18188 md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18189 buf += 2;
18190
18191 if (compact)
18192 {
18193 /* nop */
18194 insn = 0x0c00;
18195 md_number_to_chars ((char *) buf, insn, 2);
18196 buf += 2;
18197 }
18198 }
18199 else
18200 {
18201 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
18202 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18203 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
18204
18205 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18206 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18207 insn |= at << MICROMIPSOP_SH_RT;
18208
18209 if (exp.X_add_number)
18210 {
18211 exp.X_add_symbol = make_expr_symbol (&exp);
18212 exp.X_add_number = 0;
18213 }
18214
18215 fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18216 4, &exp, FALSE, BFD_RELOC_MICROMIPS_GOT16);
18217 fixp->fx_file = fragp->fr_file;
18218 fixp->fx_line = fragp->fr_line;
18219
18220 md_number_to_chars ((char *) buf, insn >> 16, 2);
18221 buf += 2;
18222 md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18223 buf += 2;
18224
18225 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18226 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18227 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18228
18229 fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18230 4, &exp, FALSE, BFD_RELOC_MICROMIPS_LO16);
18231 fixp->fx_file = fragp->fr_file;
18232 fixp->fx_line = fragp->fr_line;
18233
18234 md_number_to_chars ((char *) buf, insn >> 16, 2);
18235 buf += 2;
18236 md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18237 buf += 2;
18238
18239 /* jr/jrc/jalr/jalrs $at */
18240 insn = al ? jalr : jr;
18241 insn |= at << MICROMIPSOP_SH_MJ;
18242
18243 md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18244 buf += 2;
18245 }
18246
18247 gas_assert (buf == (bfd_byte *) fragp->fr_literal + fragp->fr_fix);
18248 return;
18249 }
18250
252b5132
RH
18251 if (RELAX_MIPS16_P (fragp->fr_subtype))
18252 {
18253 int type;
3994f87e 18254 const struct mips16_immed_operand *op;
b34976b6 18255 bfd_boolean small, ext;
252b5132
RH
18256 offsetT val;
18257 bfd_byte *buf;
18258 unsigned long insn;
b34976b6 18259 bfd_boolean use_extend;
252b5132
RH
18260 unsigned short extend;
18261
18262 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18263 op = mips16_immed_operands;
18264 while (op->type != type)
18265 ++op;
18266
18267 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18268 {
b34976b6
AM
18269 small = FALSE;
18270 ext = TRUE;
252b5132
RH
18271 }
18272 else
18273 {
b34976b6
AM
18274 small = TRUE;
18275 ext = FALSE;
252b5132
RH
18276 }
18277
5f5f22c0 18278 val = resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
18279 if (op->pcrel)
18280 {
18281 addressT addr;
18282
18283 addr = fragp->fr_address + fragp->fr_fix;
18284
18285 /* The rules for the base address of a PC relative reloc are
18286 complicated; see mips16_extended_frag. */
18287 if (type == 'p' || type == 'q')
18288 {
18289 addr += 2;
18290 if (ext)
18291 addr += 2;
18292 /* Ignore the low bit in the target, since it will be
18293 set for a text label. */
18294 if ((val & 1) != 0)
18295 --val;
18296 }
18297 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18298 addr -= 4;
18299 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18300 addr -= 2;
18301
18302 addr &= ~ (addressT) ((1 << op->shift) - 1);
18303 val -= addr;
18304
18305 /* Make sure the section winds up with the alignment we have
18306 assumed. */
18307 if (op->shift > 0)
18308 record_alignment (asec, op->shift);
18309 }
18310
18311 if (ext
18312 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18313 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18314 as_warn_where (fragp->fr_file, fragp->fr_line,
18315 _("extended instruction in delay slot"));
18316
18317 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
18318
18319 if (target_big_endian)
18320 insn = bfd_getb16 (buf);
18321 else
18322 insn = bfd_getl16 (buf);
18323
18324 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
18325 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
18326 small, ext, &insn, &use_extend, &extend);
18327
18328 if (use_extend)
18329 {
2132e3a3 18330 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
18331 fragp->fr_fix += 2;
18332 buf += 2;
18333 }
18334
2132e3a3 18335 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
18336 fragp->fr_fix += 2;
18337 buf += 2;
18338 }
18339 else
18340 {
df58fc94
RS
18341 relax_substateT subtype = fragp->fr_subtype;
18342 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18343 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
18344 int first, second;
18345 fixS *fixp;
252b5132 18346
df58fc94
RS
18347 first = RELAX_FIRST (subtype);
18348 second = RELAX_SECOND (subtype);
4d7206a2 18349 fixp = (fixS *) fragp->fr_opcode;
252b5132 18350
df58fc94
RS
18351 /* If the delay slot chosen does not match the size of the instruction,
18352 then emit a warning. */
18353 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18354 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18355 {
18356 relax_substateT s;
18357 const char *msg;
18358
18359 s = subtype & (RELAX_DELAY_SLOT_16BIT
18360 | RELAX_DELAY_SLOT_SIZE_FIRST
18361 | RELAX_DELAY_SLOT_SIZE_SECOND);
18362 msg = macro_warning (s);
18363 if (msg != NULL)
db9b2be4 18364 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
18365 subtype &= ~s;
18366 }
18367
584892a6 18368 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 18369 if (use_second == second_longer)
584892a6 18370 {
df58fc94
RS
18371 relax_substateT s;
18372 const char *msg;
18373
18374 s = (subtype
18375 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18376 msg = macro_warning (s);
18377 if (msg != NULL)
db9b2be4 18378 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 18379 subtype &= ~s;
584892a6
RS
18380 }
18381
4d7206a2
RS
18382 /* Go through all the fixups for the first sequence. Disable them
18383 (by marking them as done) if we're going to use the second
18384 sequence instead. */
18385 while (fixp
18386 && fixp->fx_frag == fragp
18387 && fixp->fx_where < fragp->fr_fix - second)
18388 {
df58fc94 18389 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18390 fixp->fx_done = 1;
18391 fixp = fixp->fx_next;
18392 }
252b5132 18393
4d7206a2
RS
18394 /* Go through the fixups for the second sequence. Disable them if
18395 we're going to use the first sequence, otherwise adjust their
18396 addresses to account for the relaxation. */
18397 while (fixp && fixp->fx_frag == fragp)
18398 {
df58fc94 18399 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18400 fixp->fx_where -= first;
18401 else
18402 fixp->fx_done = 1;
18403 fixp = fixp->fx_next;
18404 }
18405
18406 /* Now modify the frag contents. */
df58fc94 18407 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18408 {
18409 char *start;
18410
18411 start = fragp->fr_literal + fragp->fr_fix - first - second;
18412 memmove (start, start + first, second);
18413 fragp->fr_fix -= first;
18414 }
18415 else
18416 fragp->fr_fix -= second;
252b5132
RH
18417 }
18418}
18419
18420#ifdef OBJ_ELF
18421
18422/* This function is called after the relocs have been generated.
18423 We've been storing mips16 text labels as odd. Here we convert them
18424 back to even for the convenience of the debugger. */
18425
18426void
17a2f251 18427mips_frob_file_after_relocs (void)
252b5132
RH
18428{
18429 asymbol **syms;
18430 unsigned int count, i;
18431
f43abd2b 18432 if (!IS_ELF)
252b5132
RH
18433 return;
18434
18435 syms = bfd_get_outsymbols (stdoutput);
18436 count = bfd_get_symcount (stdoutput);
18437 for (i = 0; i < count; i++, syms++)
df58fc94
RS
18438 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18439 && ((*syms)->value & 1) != 0)
18440 {
18441 (*syms)->value &= ~1;
18442 /* If the symbol has an odd size, it was probably computed
18443 incorrectly, so adjust that as well. */
18444 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18445 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18446 }
252b5132
RH
18447}
18448
18449#endif
18450
a1facbec
MR
18451/* This function is called whenever a label is defined, including fake
18452 labels instantiated off the dot special symbol. It is used when
18453 handling branch delays; if a branch has a label, we assume we cannot
18454 move it. This also bumps the value of the symbol by 1 in compressed
18455 code. */
252b5132
RH
18456
18457void
a1facbec 18458mips_record_label (symbolS *sym)
252b5132 18459{
a8dbcb85 18460 segment_info_type *si = seg_info (now_seg);
252b5132
RH
18461 struct insn_label_list *l;
18462
18463 if (free_insn_labels == NULL)
18464 l = (struct insn_label_list *) xmalloc (sizeof *l);
18465 else
18466 {
18467 l = free_insn_labels;
18468 free_insn_labels = l->next;
18469 }
18470
18471 l->label = sym;
a8dbcb85
TS
18472 l->next = si->label_list;
18473 si->label_list = l;
a1facbec 18474}
07a53e5c 18475
a1facbec
MR
18476/* This function is called as tc_frob_label() whenever a label is defined
18477 and adds a DWARF-2 record we only want for true labels. */
18478
18479void
18480mips_define_label (symbolS *sym)
18481{
18482 mips_record_label (sym);
07a53e5c
RH
18483#ifdef OBJ_ELF
18484 dwarf2_emit_label (sym);
18485#endif
252b5132
RH
18486}
18487\f
18488#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
18489
18490/* Some special processing for a MIPS ELF file. */
18491
18492void
17a2f251 18493mips_elf_final_processing (void)
252b5132
RH
18494{
18495 /* Write out the register information. */
316f5878 18496 if (mips_abi != N64_ABI)
252b5132
RH
18497 {
18498 Elf32_RegInfo s;
18499
18500 s.ri_gprmask = mips_gprmask;
18501 s.ri_cprmask[0] = mips_cprmask[0];
18502 s.ri_cprmask[1] = mips_cprmask[1];
18503 s.ri_cprmask[2] = mips_cprmask[2];
18504 s.ri_cprmask[3] = mips_cprmask[3];
18505 /* The gp_value field is set by the MIPS ELF backend. */
18506
18507 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18508 ((Elf32_External_RegInfo *)
18509 mips_regmask_frag));
18510 }
18511 else
18512 {
18513 Elf64_Internal_RegInfo s;
18514
18515 s.ri_gprmask = mips_gprmask;
18516 s.ri_pad = 0;
18517 s.ri_cprmask[0] = mips_cprmask[0];
18518 s.ri_cprmask[1] = mips_cprmask[1];
18519 s.ri_cprmask[2] = mips_cprmask[2];
18520 s.ri_cprmask[3] = mips_cprmask[3];
18521 /* The gp_value field is set by the MIPS ELF backend. */
18522
18523 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18524 ((Elf64_External_RegInfo *)
18525 mips_regmask_frag));
18526 }
18527
18528 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18529 sort of BFD interface for this. */
18530 if (mips_any_noreorder)
18531 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18532 if (mips_pic != NO_PIC)
143d77c5 18533 {
8b828383 18534 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
18535 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18536 }
18537 if (mips_abicalls)
18538 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 18539
98d3f06f 18540 /* Set MIPS ELF flags for ASEs. */
74cd071d
CF
18541 /* We may need to define a new flag for DSP ASE, and set this flag when
18542 file_ase_dsp is true. */
8b082fb1 18543 /* Same for DSP R2. */
ef2e4d86
CF
18544 /* We may need to define a new flag for MT ASE, and set this flag when
18545 file_ase_mt is true. */
a4672219
TS
18546 if (file_ase_mips16)
18547 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
18548 if (file_ase_micromips)
18549 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
1f25f5d3
CD
18550#if 0 /* XXX FIXME */
18551 if (file_ase_mips3d)
18552 elf_elfheader (stdoutput)->e_flags |= ???;
18553#endif
deec1734
CD
18554 if (file_ase_mdmx)
18555 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 18556
bdaaa2e1 18557 /* Set the MIPS ELF ABI flags. */
316f5878 18558 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 18559 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 18560 else if (mips_abi == O64_ABI)
252b5132 18561 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 18562 else if (mips_abi == EABI_ABI)
252b5132 18563 {
316f5878 18564 if (!file_mips_gp32)
252b5132
RH
18565 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18566 else
18567 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18568 }
316f5878 18569 else if (mips_abi == N32_ABI)
be00bddd
TS
18570 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18571
c9914766 18572 /* Nothing to do for N64_ABI. */
252b5132
RH
18573
18574 if (mips_32bitmode)
18575 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08
TS
18576
18577#if 0 /* XXX FIXME */
18578 /* 32 bit code with 64 bit FP registers. */
18579 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18580 elf_elfheader (stdoutput)->e_flags |= ???;
18581#endif
252b5132
RH
18582}
18583
18584#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
18585\f
beae10d5 18586typedef struct proc {
9b2f1d35
EC
18587 symbolS *func_sym;
18588 symbolS *func_end_sym;
beae10d5
KH
18589 unsigned long reg_mask;
18590 unsigned long reg_offset;
18591 unsigned long fpreg_mask;
18592 unsigned long fpreg_offset;
18593 unsigned long frame_offset;
18594 unsigned long frame_reg;
18595 unsigned long pc_reg;
18596} procS;
252b5132
RH
18597
18598static procS cur_proc;
18599static procS *cur_proc_ptr;
18600static int numprocs;
18601
df58fc94
RS
18602/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
18603 as "2", and a normal nop as "0". */
18604
18605#define NOP_OPCODE_MIPS 0
18606#define NOP_OPCODE_MIPS16 1
18607#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
18608
18609char
18610mips_nop_opcode (void)
18611{
df58fc94
RS
18612 if (seg_info (now_seg)->tc_segment_info_data.micromips)
18613 return NOP_OPCODE_MICROMIPS;
18614 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18615 return NOP_OPCODE_MIPS16;
18616 else
18617 return NOP_OPCODE_MIPS;
742a56fe
RS
18618}
18619
df58fc94
RS
18620/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
18621 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 18622
0a9ef439 18623void
17a2f251 18624mips_handle_align (fragS *fragp)
a19d8eb0 18625{
df58fc94 18626 char nop_opcode;
742a56fe 18627 char *p;
c67a084a
NC
18628 int bytes, size, excess;
18629 valueT opcode;
742a56fe 18630
0a9ef439
RH
18631 if (fragp->fr_type != rs_align_code)
18632 return;
18633
742a56fe 18634 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18635 nop_opcode = *p;
18636 switch (nop_opcode)
a19d8eb0 18637 {
df58fc94
RS
18638 case NOP_OPCODE_MICROMIPS:
18639 opcode = micromips_nop32_insn.insn_opcode;
18640 size = 4;
18641 break;
18642 case NOP_OPCODE_MIPS16:
c67a084a
NC
18643 opcode = mips16_nop_insn.insn_opcode;
18644 size = 2;
df58fc94
RS
18645 break;
18646 case NOP_OPCODE_MIPS:
18647 default:
c67a084a
NC
18648 opcode = nop_insn.insn_opcode;
18649 size = 4;
df58fc94 18650 break;
c67a084a 18651 }
a19d8eb0 18652
c67a084a
NC
18653 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18654 excess = bytes % size;
df58fc94
RS
18655
18656 /* Handle the leading part if we're not inserting a whole number of
18657 instructions, and make it the end of the fixed part of the frag.
18658 Try to fit in a short microMIPS NOP if applicable and possible,
18659 and use zeroes otherwise. */
18660 gas_assert (excess < 4);
18661 fragp->fr_fix += excess;
18662 switch (excess)
c67a084a 18663 {
df58fc94
RS
18664 case 3:
18665 *p++ = '\0';
18666 /* Fall through. */
18667 case 2:
18668 if (nop_opcode == NOP_OPCODE_MICROMIPS)
18669 {
18670 md_number_to_chars (p, micromips_nop16_insn.insn_opcode, 2);
18671 p += 2;
18672 break;
18673 }
18674 *p++ = '\0';
18675 /* Fall through. */
18676 case 1:
18677 *p++ = '\0';
18678 /* Fall through. */
18679 case 0:
18680 break;
a19d8eb0 18681 }
c67a084a
NC
18682
18683 md_number_to_chars (p, opcode, size);
18684 fragp->fr_var = size;
a19d8eb0
CP
18685}
18686
252b5132 18687static void
17a2f251 18688md_obj_begin (void)
252b5132
RH
18689{
18690}
18691
18692static void
17a2f251 18693md_obj_end (void)
252b5132 18694{
54f4ddb3 18695 /* Check for premature end, nesting errors, etc. */
252b5132 18696 if (cur_proc_ptr)
9a41af64 18697 as_warn (_("missing .end at end of assembly"));
252b5132
RH
18698}
18699
18700static long
17a2f251 18701get_number (void)
252b5132
RH
18702{
18703 int negative = 0;
18704 long val = 0;
18705
18706 if (*input_line_pointer == '-')
18707 {
18708 ++input_line_pointer;
18709 negative = 1;
18710 }
3882b010 18711 if (!ISDIGIT (*input_line_pointer))
956cd1d6 18712 as_bad (_("expected simple number"));
252b5132
RH
18713 if (input_line_pointer[0] == '0')
18714 {
18715 if (input_line_pointer[1] == 'x')
18716 {
18717 input_line_pointer += 2;
3882b010 18718 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
18719 {
18720 val <<= 4;
18721 val |= hex_value (*input_line_pointer++);
18722 }
18723 return negative ? -val : val;
18724 }
18725 else
18726 {
18727 ++input_line_pointer;
3882b010 18728 while (ISDIGIT (*input_line_pointer))
252b5132
RH
18729 {
18730 val <<= 3;
18731 val |= *input_line_pointer++ - '0';
18732 }
18733 return negative ? -val : val;
18734 }
18735 }
3882b010 18736 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
18737 {
18738 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18739 *input_line_pointer, *input_line_pointer);
956cd1d6 18740 as_warn (_("invalid number"));
252b5132
RH
18741 return -1;
18742 }
3882b010 18743 while (ISDIGIT (*input_line_pointer))
252b5132
RH
18744 {
18745 val *= 10;
18746 val += *input_line_pointer++ - '0';
18747 }
18748 return negative ? -val : val;
18749}
18750
18751/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
18752 is an initial number which is the ECOFF file index. In the non-ECOFF
18753 case .file implies DWARF-2. */
18754
18755static void
17a2f251 18756s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 18757{
ecb4347a
DJ
18758 static int first_file_directive = 0;
18759
c5dd6aab
DJ
18760 if (ECOFF_DEBUGGING)
18761 {
18762 get_number ();
18763 s_app_file (0);
18764 }
18765 else
ecb4347a
DJ
18766 {
18767 char *filename;
18768
18769 filename = dwarf2_directive_file (0);
18770
18771 /* Versions of GCC up to 3.1 start files with a ".file"
18772 directive even for stabs output. Make sure that this
18773 ".file" is handled. Note that you need a version of GCC
18774 after 3.1 in order to support DWARF-2 on MIPS. */
18775 if (filename != NULL && ! first_file_directive)
18776 {
18777 (void) new_logical_line (filename, -1);
c04f5787 18778 s_app_file_string (filename, 0);
ecb4347a
DJ
18779 }
18780 first_file_directive = 1;
18781 }
c5dd6aab
DJ
18782}
18783
18784/* The .loc directive, implying DWARF-2. */
252b5132
RH
18785
18786static void
17a2f251 18787s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 18788{
c5dd6aab
DJ
18789 if (!ECOFF_DEBUGGING)
18790 dwarf2_directive_loc (0);
252b5132
RH
18791}
18792
252b5132
RH
18793/* The .end directive. */
18794
18795static void
17a2f251 18796s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
18797{
18798 symbolS *p;
252b5132 18799
7a621144
DJ
18800 /* Following functions need their own .frame and .cprestore directives. */
18801 mips_frame_reg_valid = 0;
18802 mips_cprestore_valid = 0;
18803
252b5132
RH
18804 if (!is_end_of_line[(unsigned char) *input_line_pointer])
18805 {
18806 p = get_symbol ();
18807 demand_empty_rest_of_line ();
18808 }
18809 else
18810 p = NULL;
18811
14949570 18812 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
18813 as_warn (_(".end not in text section"));
18814
18815 if (!cur_proc_ptr)
18816 {
18817 as_warn (_(".end directive without a preceding .ent directive."));
18818 demand_empty_rest_of_line ();
18819 return;
18820 }
18821
18822 if (p != NULL)
18823 {
9c2799c2 18824 gas_assert (S_GET_NAME (p));
9b2f1d35 18825 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 18826 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
18827
18828 if (debug_type == DEBUG_STABS)
18829 stabs_generate_asm_endfunc (S_GET_NAME (p),
18830 S_GET_NAME (p));
252b5132
RH
18831 }
18832 else
18833 as_warn (_(".end directive missing or unknown symbol"));
18834
2132e3a3 18835#ifdef OBJ_ELF
9b2f1d35
EC
18836 /* Create an expression to calculate the size of the function. */
18837 if (p && cur_proc_ptr)
18838 {
18839 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
18840 expressionS *exp = xmalloc (sizeof (expressionS));
18841
18842 obj->size = exp;
18843 exp->X_op = O_subtract;
18844 exp->X_add_symbol = symbol_temp_new_now ();
18845 exp->X_op_symbol = p;
18846 exp->X_add_number = 0;
18847
18848 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
18849 }
18850
ecb4347a 18851 /* Generate a .pdr section. */
f43abd2b 18852 if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
18853 {
18854 segT saved_seg = now_seg;
18855 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
18856 expressionS exp;
18857 char *fragp;
252b5132 18858
252b5132 18859#ifdef md_flush_pending_output
ecb4347a 18860 md_flush_pending_output ();
252b5132
RH
18861#endif
18862
9c2799c2 18863 gas_assert (pdr_seg);
ecb4347a 18864 subseg_set (pdr_seg, 0);
252b5132 18865
ecb4347a
DJ
18866 /* Write the symbol. */
18867 exp.X_op = O_symbol;
18868 exp.X_add_symbol = p;
18869 exp.X_add_number = 0;
18870 emit_expr (&exp, 4);
252b5132 18871
ecb4347a 18872 fragp = frag_more (7 * 4);
252b5132 18873
17a2f251
TS
18874 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
18875 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
18876 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
18877 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
18878 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
18879 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
18880 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 18881
ecb4347a
DJ
18882 subseg_set (saved_seg, saved_subseg);
18883 }
18884#endif /* OBJ_ELF */
252b5132
RH
18885
18886 cur_proc_ptr = NULL;
18887}
18888
18889/* The .aent and .ent directives. */
18890
18891static void
17a2f251 18892s_mips_ent (int aent)
252b5132 18893{
252b5132 18894 symbolS *symbolP;
252b5132
RH
18895
18896 symbolP = get_symbol ();
18897 if (*input_line_pointer == ',')
f9419b05 18898 ++input_line_pointer;
252b5132 18899 SKIP_WHITESPACE ();
3882b010 18900 if (ISDIGIT (*input_line_pointer)
d9a62219 18901 || *input_line_pointer == '-')
874e8986 18902 get_number ();
252b5132 18903
14949570 18904 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
18905 as_warn (_(".ent or .aent not in text section."));
18906
18907 if (!aent && cur_proc_ptr)
9a41af64 18908 as_warn (_("missing .end"));
252b5132
RH
18909
18910 if (!aent)
18911 {
7a621144
DJ
18912 /* This function needs its own .frame and .cprestore directives. */
18913 mips_frame_reg_valid = 0;
18914 mips_cprestore_valid = 0;
18915
252b5132
RH
18916 cur_proc_ptr = &cur_proc;
18917 memset (cur_proc_ptr, '\0', sizeof (procS));
18918
9b2f1d35 18919 cur_proc_ptr->func_sym = symbolP;
252b5132 18920
f9419b05 18921 ++numprocs;
ecb4347a
DJ
18922
18923 if (debug_type == DEBUG_STABS)
18924 stabs_generate_asm_func (S_GET_NAME (symbolP),
18925 S_GET_NAME (symbolP));
252b5132
RH
18926 }
18927
7c0fc524
MR
18928 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
18929
252b5132
RH
18930 demand_empty_rest_of_line ();
18931}
18932
18933/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 18934 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 18935 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 18936 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
18937 symbol table (in the mdebug section). */
18938
18939static void
17a2f251 18940s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 18941{
ecb4347a 18942#ifdef OBJ_ELF
f43abd2b 18943 if (IS_ELF && !ECOFF_DEBUGGING)
ecb4347a
DJ
18944 {
18945 long val;
252b5132 18946
ecb4347a
DJ
18947 if (cur_proc_ptr == (procS *) NULL)
18948 {
18949 as_warn (_(".frame outside of .ent"));
18950 demand_empty_rest_of_line ();
18951 return;
18952 }
252b5132 18953
ecb4347a
DJ
18954 cur_proc_ptr->frame_reg = tc_get_register (1);
18955
18956 SKIP_WHITESPACE ();
18957 if (*input_line_pointer++ != ','
18958 || get_absolute_expression_and_terminator (&val) != ',')
18959 {
18960 as_warn (_("Bad .frame directive"));
18961 --input_line_pointer;
18962 demand_empty_rest_of_line ();
18963 return;
18964 }
252b5132 18965
ecb4347a
DJ
18966 cur_proc_ptr->frame_offset = val;
18967 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 18968
252b5132 18969 demand_empty_rest_of_line ();
252b5132 18970 }
ecb4347a
DJ
18971 else
18972#endif /* OBJ_ELF */
18973 s_ignore (ignore);
252b5132
RH
18974}
18975
bdaaa2e1
KH
18976/* The .fmask and .mask directives. If the mdebug section is present
18977 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 18978 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 18979 information correctly. We can't use the ecoff routines because they
252b5132
RH
18980 make reference to the ecoff symbol table (in the mdebug section). */
18981
18982static void
17a2f251 18983s_mips_mask (int reg_type)
252b5132 18984{
ecb4347a 18985#ifdef OBJ_ELF
f43abd2b 18986 if (IS_ELF && !ECOFF_DEBUGGING)
252b5132 18987 {
ecb4347a 18988 long mask, off;
252b5132 18989
ecb4347a
DJ
18990 if (cur_proc_ptr == (procS *) NULL)
18991 {
18992 as_warn (_(".mask/.fmask outside of .ent"));
18993 demand_empty_rest_of_line ();
18994 return;
18995 }
252b5132 18996
ecb4347a
DJ
18997 if (get_absolute_expression_and_terminator (&mask) != ',')
18998 {
18999 as_warn (_("Bad .mask/.fmask directive"));
19000 --input_line_pointer;
19001 demand_empty_rest_of_line ();
19002 return;
19003 }
252b5132 19004
ecb4347a
DJ
19005 off = get_absolute_expression ();
19006
19007 if (reg_type == 'F')
19008 {
19009 cur_proc_ptr->fpreg_mask = mask;
19010 cur_proc_ptr->fpreg_offset = off;
19011 }
19012 else
19013 {
19014 cur_proc_ptr->reg_mask = mask;
19015 cur_proc_ptr->reg_offset = off;
19016 }
19017
19018 demand_empty_rest_of_line ();
252b5132
RH
19019 }
19020 else
ecb4347a
DJ
19021#endif /* OBJ_ELF */
19022 s_ignore (reg_type);
252b5132
RH
19023}
19024
316f5878
RS
19025/* A table describing all the processors gas knows about. Names are
19026 matched in the order listed.
e7af610e 19027
316f5878
RS
19028 To ease comparison, please keep this table in the same order as
19029 gcc's mips_cpu_info_table[]. */
e972090a
NC
19030static const struct mips_cpu_info mips_cpu_info_table[] =
19031{
316f5878 19032 /* Entries for generic ISAs */
ad3fea08
TS
19033 { "mips1", MIPS_CPU_IS_ISA, ISA_MIPS1, CPU_R3000 },
19034 { "mips2", MIPS_CPU_IS_ISA, ISA_MIPS2, CPU_R6000 },
19035 { "mips3", MIPS_CPU_IS_ISA, ISA_MIPS3, CPU_R4000 },
19036 { "mips4", MIPS_CPU_IS_ISA, ISA_MIPS4, CPU_R8000 },
19037 { "mips5", MIPS_CPU_IS_ISA, ISA_MIPS5, CPU_MIPS5 },
19038 { "mips32", MIPS_CPU_IS_ISA, ISA_MIPS32, CPU_MIPS32 },
19039 { "mips32r2", MIPS_CPU_IS_ISA, ISA_MIPS32R2, CPU_MIPS32R2 },
19040 { "mips64", MIPS_CPU_IS_ISA, ISA_MIPS64, CPU_MIPS64 },
19041 { "mips64r2", MIPS_CPU_IS_ISA, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
19042
19043 /* MIPS I */
ad3fea08
TS
19044 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
19045 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
19046 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19047
19048 /* MIPS II */
ad3fea08 19049 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19050
19051 /* MIPS III */
ad3fea08
TS
19052 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
19053 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
19054 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
19055 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
19056 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
19057 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
19058 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
19059 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
19060 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
19061 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
19062 { "orion", 0, ISA_MIPS3, CPU_R4600 },
19063 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
b15591bb
AN
19064 /* ST Microelectronics Loongson 2E and 2F cores */
19065 { "loongson2e", 0, ISA_MIPS3, CPU_LOONGSON_2E },
19066 { "loongson2f", 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19067
19068 /* MIPS IV */
ad3fea08
TS
19069 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
19070 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
19071 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
3aa3176b
TS
19072 { "r14000", 0, ISA_MIPS4, CPU_R14000 },
19073 { "r16000", 0, ISA_MIPS4, CPU_R16000 },
ad3fea08
TS
19074 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
19075 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
19076 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
19077 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
19078 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
19079 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
19080 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
19081 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
19082 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
19083 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19084
19085 /* MIPS 32 */
ad3fea08
TS
19086 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
19087 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
19088 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
19089 { "4ksc", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
19090
19091 /* MIPS 32 Release 2 */
19092 { "4kec", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19093 { "4kem", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19094 { "4kep", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19095 { "4ksd", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19096 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19097 { "m4kp", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
b5503c7b
MR
19098 { "m14k", MIPS_CPU_ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19099 { "m14kc", MIPS_CPU_ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
7a795ef4
MR
19100 { "m14ke", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19101 ISA_MIPS32R2, CPU_MIPS32R2 },
19102 { "m14kec", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19103 ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19104 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19105 { "24kf2_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19106 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19107 { "24kf1_1", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19108 /* Deprecated forms of the above. */
19109 { "24kfx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19110 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19111 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
ad3fea08 19112 { "24kec", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19113 { "24kef2_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08 19114 { "24kef", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19115 { "24kef1_1", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19116 /* Deprecated forms of the above. */
19117 { "24kefx", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
65263ce3 19118 { "24kex", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19119 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
a360e743
TS
19120 { "34kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19121 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19122 { "34kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19123 ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
19124 { "34kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19125 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19126 { "34kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19127 ISA_MIPS32R2, CPU_MIPS32R2 },
19128 /* Deprecated forms of the above. */
19129 { "34kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19130 ISA_MIPS32R2, CPU_MIPS32R2 },
a360e743
TS
19131 { "34kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19132 ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4
SL
19133 /* 34Kn is a 34kc without DSP. */
19134 { "34kn", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19135 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
19136 { "74kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19137 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19138 { "74kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19139 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19140 { "74kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19141 ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951
RS
19142 { "74kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19143 ISA_MIPS32R2, CPU_MIPS32R2 },
19144 { "74kf3_2", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19145 ISA_MIPS32R2, CPU_MIPS32R2 },
19146 /* Deprecated forms of the above. */
19147 { "74kfx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19148 ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f
TS
19149 { "74kx", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19150 ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a
SL
19151 /* 1004K cores are multiprocessor versions of the 34K. */
19152 { "1004kc", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19153 ISA_MIPS32R2, CPU_MIPS32R2 },
19154 { "1004kf2_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19155 ISA_MIPS32R2, CPU_MIPS32R2 },
19156 { "1004kf", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19157 ISA_MIPS32R2, CPU_MIPS32R2 },
19158 { "1004kf1_1", MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19159 ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 19160
316f5878 19161 /* MIPS 64 */
ad3fea08
TS
19162 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
19163 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
19164 { "20kc", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
7764b395 19165 { "25kf", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19166
c7a23324 19167 /* Broadcom SB-1 CPU core */
65263ce3
TS
19168 { "sb1", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19169 ISA_MIPS64, CPU_SB1 },
1e85aad8
JW
19170 /* Broadcom SB-1A CPU core */
19171 { "sb1a", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19172 ISA_MIPS64, CPU_SB1 },
d051516a
NC
19173
19174 { "loongson3a", 0, ISA_MIPS64, CPU_LOONGSON_3A },
e7af610e 19175
ed163775
MR
19176 /* MIPS 64 Release 2 */
19177
967344c6
AN
19178 /* Cavium Networks Octeon CPU core */
19179 { "octeon", 0, ISA_MIPS64R2, CPU_OCTEON },
dd6a37e7 19180 { "octeon+", 0, ISA_MIPS64R2, CPU_OCTEONP },
432233b3 19181 { "octeon2", 0, ISA_MIPS64R2, CPU_OCTEON2 },
967344c6 19182
52b6b6b9
JM
19183 /* RMI Xlr */
19184 { "xlr", 0, ISA_MIPS64, CPU_XLR },
19185
55a36193
MK
19186 /* Broadcom XLP.
19187 XLP is mostly like XLR, with the prominent exception that it is
19188 MIPS64R2 rather than MIPS64. */
19189 { "xlp", 0, ISA_MIPS64R2, CPU_XLR },
19190
316f5878
RS
19191 /* End marker */
19192 { NULL, 0, 0, 0 }
19193};
e7af610e 19194
84ea6cf2 19195
316f5878
RS
19196/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19197 with a final "000" replaced by "k". Ignore case.
e7af610e 19198
316f5878 19199 Note: this function is shared between GCC and GAS. */
c6c98b38 19200
b34976b6 19201static bfd_boolean
17a2f251 19202mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19203{
19204 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19205 given++, canonical++;
19206
19207 return ((*given == 0 && *canonical == 0)
19208 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19209}
19210
19211
19212/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19213 CPU name. We've traditionally allowed a lot of variation here.
19214
19215 Note: this function is shared between GCC and GAS. */
19216
b34976b6 19217static bfd_boolean
17a2f251 19218mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19219{
19220 /* First see if the name matches exactly, or with a final "000"
19221 turned into "k". */
19222 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19223 return TRUE;
316f5878
RS
19224
19225 /* If not, try comparing based on numerical designation alone.
19226 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19227 if (TOLOWER (*given) == 'r')
19228 given++;
19229 if (!ISDIGIT (*given))
b34976b6 19230 return FALSE;
316f5878
RS
19231
19232 /* Skip over some well-known prefixes in the canonical name,
19233 hoping to find a number there too. */
19234 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19235 canonical += 2;
19236 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19237 canonical += 2;
19238 else if (TOLOWER (canonical[0]) == 'r')
19239 canonical += 1;
19240
19241 return mips_strict_matching_cpu_name_p (canonical, given);
19242}
19243
19244
19245/* Parse an option that takes the name of a processor as its argument.
19246 OPTION is the name of the option and CPU_STRING is the argument.
19247 Return the corresponding processor enumeration if the CPU_STRING is
19248 recognized, otherwise report an error and return null.
19249
19250 A similar function exists in GCC. */
e7af610e
NC
19251
19252static const struct mips_cpu_info *
17a2f251 19253mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19254{
316f5878 19255 const struct mips_cpu_info *p;
e7af610e 19256
316f5878
RS
19257 /* 'from-abi' selects the most compatible architecture for the given
19258 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19259 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19260 version. Look first at the -mgp options, if given, otherwise base
19261 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19262
316f5878
RS
19263 Treat NO_ABI like the EABIs. One reason to do this is that the
19264 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19265 architecture. This code picks MIPS I for 'mips' and MIPS III for
19266 'mips64', just as we did in the days before 'from-abi'. */
19267 if (strcasecmp (cpu_string, "from-abi") == 0)
19268 {
19269 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19270 return mips_cpu_info_from_isa (ISA_MIPS1);
19271
19272 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19273 return mips_cpu_info_from_isa (ISA_MIPS3);
19274
19275 if (file_mips_gp32 >= 0)
19276 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19277
19278 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19279 ? ISA_MIPS3
19280 : ISA_MIPS1);
19281 }
19282
19283 /* 'default' has traditionally been a no-op. Probably not very useful. */
19284 if (strcasecmp (cpu_string, "default") == 0)
19285 return 0;
19286
19287 for (p = mips_cpu_info_table; p->name != 0; p++)
19288 if (mips_matching_cpu_name_p (p->name, cpu_string))
19289 return p;
19290
20203fb9 19291 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
316f5878 19292 return 0;
e7af610e
NC
19293}
19294
316f5878
RS
19295/* Return the canonical processor information for ISA (a member of the
19296 ISA_MIPS* enumeration). */
19297
e7af610e 19298static const struct mips_cpu_info *
17a2f251 19299mips_cpu_info_from_isa (int isa)
e7af610e
NC
19300{
19301 int i;
19302
19303 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19304 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19305 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19306 return (&mips_cpu_info_table[i]);
19307
e972090a 19308 return NULL;
e7af610e 19309}
fef14a42
TS
19310
19311static const struct mips_cpu_info *
17a2f251 19312mips_cpu_info_from_arch (int arch)
fef14a42
TS
19313{
19314 int i;
19315
19316 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19317 if (arch == mips_cpu_info_table[i].cpu)
19318 return (&mips_cpu_info_table[i]);
19319
19320 return NULL;
19321}
316f5878
RS
19322\f
19323static void
17a2f251 19324show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19325{
19326 if (*first_p)
19327 {
19328 fprintf (stream, "%24s", "");
19329 *col_p = 24;
19330 }
19331 else
19332 {
19333 fprintf (stream, ", ");
19334 *col_p += 2;
19335 }
e7af610e 19336
316f5878
RS
19337 if (*col_p + strlen (string) > 72)
19338 {
19339 fprintf (stream, "\n%24s", "");
19340 *col_p = 24;
19341 }
19342
19343 fprintf (stream, "%s", string);
19344 *col_p += strlen (string);
19345
19346 *first_p = 0;
19347}
19348
19349void
17a2f251 19350md_show_usage (FILE *stream)
e7af610e 19351{
316f5878
RS
19352 int column, first;
19353 size_t i;
19354
19355 fprintf (stream, _("\
19356MIPS options:\n\
316f5878
RS
19357-EB generate big endian output\n\
19358-EL generate little endian output\n\
19359-g, -g2 do not remove unneeded NOPs or swap branches\n\
19360-G NUM allow referencing objects up to NUM bytes\n\
19361 implicitly with the gp register [default 8]\n"));
19362 fprintf (stream, _("\
19363-mips1 generate MIPS ISA I instructions\n\
19364-mips2 generate MIPS ISA II instructions\n\
19365-mips3 generate MIPS ISA III instructions\n\
19366-mips4 generate MIPS ISA IV instructions\n\
19367-mips5 generate MIPS ISA V instructions\n\
19368-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 19369-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 19370-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 19371-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
19372-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19373
19374 first = 1;
e7af610e
NC
19375
19376 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
19377 show (stream, mips_cpu_info_table[i].name, &column, &first);
19378 show (stream, "from-abi", &column, &first);
19379 fputc ('\n', stream);
e7af610e 19380
316f5878
RS
19381 fprintf (stream, _("\
19382-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19383-no-mCPU don't generate code specific to CPU.\n\
19384 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19385
19386 first = 1;
19387
19388 show (stream, "3900", &column, &first);
19389 show (stream, "4010", &column, &first);
19390 show (stream, "4100", &column, &first);
19391 show (stream, "4650", &column, &first);
19392 fputc ('\n', stream);
19393
19394 fprintf (stream, _("\
19395-mips16 generate mips16 instructions\n\
19396-no-mips16 do not generate mips16 instructions\n"));
19397 fprintf (stream, _("\
df58fc94
RS
19398-mmicromips generate microMIPS instructions\n\
19399-mno-micromips do not generate microMIPS instructions\n"));
19400 fprintf (stream, _("\
e16bfa71
TS
19401-msmartmips generate smartmips instructions\n\
19402-mno-smartmips do not generate smartmips instructions\n"));
19403 fprintf (stream, _("\
74cd071d
CF
19404-mdsp generate DSP instructions\n\
19405-mno-dsp do not generate DSP instructions\n"));
19406 fprintf (stream, _("\
8b082fb1
TS
19407-mdspr2 generate DSP R2 instructions\n\
19408-mno-dspr2 do not generate DSP R2 instructions\n"));
19409 fprintf (stream, _("\
ef2e4d86
CF
19410-mmt generate MT instructions\n\
19411-mno-mt do not generate MT instructions\n"));
19412 fprintf (stream, _("\
dec0624d
MR
19413-mmcu generate MCU instructions\n\
19414-mno-mcu do not generate MCU instructions\n"));
19415 fprintf (stream, _("\
c67a084a
NC
19416-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19417-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 19418-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 19419-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 19420-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 19421-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
19422-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19423-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 19424-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
19425-O0 remove unneeded NOPs, do not swap branches\n\
19426-O remove unneeded NOPs and swap branches\n\
316f5878
RS
19427--trap, --no-break trap exception on div by 0 and mult overflow\n\
19428--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
19429 fprintf (stream, _("\
19430-mhard-float allow floating-point instructions\n\
19431-msoft-float do not allow floating-point instructions\n\
19432-msingle-float only allow 32-bit floating-point operations\n\
19433-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
19434--[no-]construct-floats [dis]allow floating point values to be constructed\n"
19435 ));
316f5878
RS
19436#ifdef OBJ_ELF
19437 fprintf (stream, _("\
19438-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 19439-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 19440-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 19441-non_shared do not generate code that can operate with DSOs\n\
316f5878 19442-xgot assume a 32 bit GOT\n\
dcd410fe 19443-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 19444-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 19445 position dependent (non shared) code\n\
316f5878
RS
19446-mabi=ABI create ABI conformant object file for:\n"));
19447
19448 first = 1;
19449
19450 show (stream, "32", &column, &first);
19451 show (stream, "o64", &column, &first);
19452 show (stream, "n32", &column, &first);
19453 show (stream, "64", &column, &first);
19454 show (stream, "eabi", &column, &first);
19455
19456 fputc ('\n', stream);
19457
19458 fprintf (stream, _("\
19459-32 create o32 ABI object file (default)\n\
19460-n32 create n32 ABI object file\n\
19461-64 create 64 ABI object file\n"));
19462#endif
e7af610e 19463}
14e777e0 19464
1575952e 19465#ifdef TE_IRIX
14e777e0 19466enum dwarf2_format
413a266c 19467mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 19468{
369943fe 19469 if (HAVE_64BIT_SYMBOLS)
1575952e 19470 return dwarf2_format_64bit_irix;
14e777e0
KB
19471 else
19472 return dwarf2_format_32bit;
19473}
1575952e 19474#endif
73369e65
EC
19475
19476int
19477mips_dwarf2_addr_size (void)
19478{
6b6b3450 19479 if (HAVE_64BIT_OBJECTS)
73369e65 19480 return 8;
73369e65
EC
19481 else
19482 return 4;
19483}
5862107c
EC
19484
19485/* Standard calling conventions leave the CFA at SP on entry. */
19486void
19487mips_cfi_frame_initial_instructions (void)
19488{
19489 cfi_add_CFA_def_cfa_register (SP);
19490}
19491
707bfff6
TS
19492int
19493tc_mips_regname_to_dw2regnum (char *regname)
19494{
19495 unsigned int regnum = -1;
19496 unsigned int reg;
19497
19498 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19499 regnum = reg;
19500
19501 return regnum;
19502}
This page took 2.322335 seconds and 4 git commands to generate.