gas/
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
81912461 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
e407c74b 3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
c67a084a 4 Free Software Foundation, Inc.
252b5132
RH
5 Contributed by the OSF and Ralph Campbell.
6 Written by Keith Knowles and Ralph Campbell, working independently.
7 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
8 Support.
9
10 This file is part of GAS.
11
12 GAS is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
ec2655a6 14 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
15 any later version.
16
17 GAS is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
24 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
25 02110-1301, USA. */
252b5132
RH
26
27#include "as.h"
28#include "config.h"
29#include "subsegs.h"
3882b010 30#include "safe-ctype.h"
252b5132 31
252b5132
RH
32#include "opcode/mips.h"
33#include "itbl-ops.h"
c5dd6aab 34#include "dwarf2dbg.h"
5862107c 35#include "dw2gencfi.h"
252b5132
RH
36
37#ifdef DEBUG
38#define DBG(x) printf x
39#else
40#define DBG(x)
41#endif
42
43#ifdef OBJ_MAYBE_ELF
44/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
45static int mips_output_flavor (void);
46static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
47#undef OBJ_PROCESS_STAB
48#undef OUTPUT_FLAVOR
49#undef S_GET_ALIGN
50#undef S_GET_SIZE
51#undef S_SET_ALIGN
52#undef S_SET_SIZE
252b5132
RH
53#undef obj_frob_file
54#undef obj_frob_file_after_relocs
55#undef obj_frob_symbol
56#undef obj_pop_insert
57#undef obj_sec_sym_ok_for_reloc
58#undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60#include "obj-elf.h"
61/* Fix any of them that we actually care about. */
62#undef OUTPUT_FLAVOR
63#define OUTPUT_FLAVOR mips_output_flavor()
64#endif
65
66#if defined (OBJ_ELF)
67#include "elf/mips.h"
68#endif
69
70#ifndef ECOFF_DEBUGGING
71#define NO_ECOFF_DEBUGGING
72#define ECOFF_DEBUGGING 0
73#endif
74
ecb4347a
DJ
75int mips_flag_mdebug = -1;
76
dcd410fe
RO
77/* Control generation of .pdr sections. Off by default on IRIX: the native
78 linker doesn't know about and discards them, but relocations against them
79 remain, leading to rld crashes. */
80#ifdef TE_IRIX
81int mips_flag_pdr = FALSE;
82#else
83int mips_flag_pdr = TRUE;
84#endif
85
252b5132
RH
86#include "ecoff.h"
87
88#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89static char *mips_regmask_frag;
90#endif
91
85b51719 92#define ZERO 0
741fe287 93#define ATREG 1
df58fc94
RS
94#define S0 16
95#define S7 23
252b5132
RH
96#define TREG 24
97#define PIC_CALL_REG 25
98#define KT0 26
99#define KT1 27
100#define GP 28
101#define SP 29
102#define FP 30
103#define RA 31
104
105#define ILLEGAL_REG (32)
106
741fe287
MR
107#define AT mips_opts.at
108
252b5132
RH
109/* Allow override of standard little-endian ECOFF format. */
110
111#ifndef ECOFF_LITTLE_FORMAT
112#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
113#endif
114
115extern int target_big_endian;
116
252b5132 117/* The name of the readonly data section. */
4d0d148d 118#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
252b5132 119 ? ".rdata" \
056350c6
NC
120 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
121 ? ".rdata" \
252b5132
RH
122 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
123 ? ".rodata" \
124 : (abort (), ""))
125
a4e06468
RS
126/* Ways in which an instruction can be "appended" to the output. */
127enum append_method {
128 /* Just add it normally. */
129 APPEND_ADD,
130
131 /* Add it normally and then add a nop. */
132 APPEND_ADD_WITH_NOP,
133
134 /* Turn an instruction with a delay slot into a "compact" version. */
135 APPEND_ADD_COMPACT,
136
137 /* Insert the instruction before the last one. */
138 APPEND_SWAP
139};
140
47e39b9d
RS
141/* Information about an instruction, including its format, operands
142 and fixups. */
143struct mips_cl_insn
144{
145 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
146 const struct mips_opcode *insn_mo;
147
47e39b9d 148 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
149 a copy of INSN_MO->match with the operands filled in. If we have
150 decided to use an extended MIPS16 instruction, this includes the
151 extension. */
47e39b9d
RS
152 unsigned long insn_opcode;
153
154 /* The frag that contains the instruction. */
155 struct frag *frag;
156
157 /* The offset into FRAG of the first instruction byte. */
158 long where;
159
160 /* The relocs associated with the instruction, if any. */
161 fixS *fixp[3];
162
a38419a5
RS
163 /* True if this entry cannot be moved from its current position. */
164 unsigned int fixed_p : 1;
47e39b9d 165
708587a4 166 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
167 unsigned int noreorder_p : 1;
168
2fa15973
RS
169 /* True for mips16 instructions that jump to an absolute address. */
170 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
171
172 /* True if this instruction is complete. */
173 unsigned int complete_p : 1;
e407c74b
NC
174
175 /* True if this instruction is cleared from history by unconditional
176 branch. */
177 unsigned int cleared_p : 1;
47e39b9d
RS
178};
179
a325df1d
TS
180/* The ABI to use. */
181enum mips_abi_level
182{
183 NO_ABI = 0,
184 O32_ABI,
185 O64_ABI,
186 N32_ABI,
187 N64_ABI,
188 EABI_ABI
189};
190
191/* MIPS ABI we are using for this output file. */
316f5878 192static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 193
143d77c5
EC
194/* Whether or not we have code that can call pic code. */
195int mips_abicalls = FALSE;
196
aa6975fb
ILT
197/* Whether or not we have code which can be put into a shared
198 library. */
199static bfd_boolean mips_in_shared = TRUE;
200
252b5132
RH
201/* This is the set of options which may be modified by the .set
202 pseudo-op. We use a struct so that .set push and .set pop are more
203 reliable. */
204
e972090a
NC
205struct mips_set_options
206{
252b5132
RH
207 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
208 if it has not been initialized. Changed by `.set mipsN', and the
209 -mipsN command line option, and the default CPU. */
210 int isa;
846ef2d0
RS
211 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
212 <asename>', by command line options, and based on the default
213 architecture. */
214 int ase;
252b5132
RH
215 /* Whether we are assembling for the mips16 processor. 0 if we are
216 not, 1 if we are, and -1 if the value has not been initialized.
217 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
218 -nomips16 command line options, and the default CPU. */
219 int mips16;
df58fc94
RS
220 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
221 1 if we are, and -1 if the value has not been initialized. Changed
222 by `.set micromips' and `.set nomicromips', and the -mmicromips
223 and -mno-micromips command line options, and the default CPU. */
224 int micromips;
252b5132
RH
225 /* Non-zero if we should not reorder instructions. Changed by `.set
226 reorder' and `.set noreorder'. */
227 int noreorder;
741fe287
MR
228 /* Non-zero if we should not permit the register designated "assembler
229 temporary" to be used in instructions. The value is the register
230 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
231 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
232 unsigned int at;
252b5132
RH
233 /* Non-zero if we should warn when a macro instruction expands into
234 more than one machine instruction. Changed by `.set nomacro' and
235 `.set macro'. */
236 int warn_about_macros;
237 /* Non-zero if we should not move instructions. Changed by `.set
238 move', `.set volatile', `.set nomove', and `.set novolatile'. */
239 int nomove;
240 /* Non-zero if we should not optimize branches by moving the target
241 of the branch into the delay slot. Actually, we don't perform
242 this optimization anyhow. Changed by `.set bopt' and `.set
243 nobopt'. */
244 int nobopt;
245 /* Non-zero if we should not autoextend mips16 instructions.
246 Changed by `.set autoextend' and `.set noautoextend'. */
247 int noautoextend;
a325df1d
TS
248 /* Restrict general purpose registers and floating point registers
249 to 32 bit. This is initially determined when -mgp32 or -mfp32
250 is passed but can changed if the assembler code uses .set mipsN. */
251 int gp32;
252 int fp32;
fef14a42
TS
253 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
254 command line option, and the default CPU. */
255 int arch;
aed1a261
RS
256 /* True if ".set sym32" is in effect. */
257 bfd_boolean sym32;
037b32b9
AN
258 /* True if floating-point operations are not allowed. Changed by .set
259 softfloat or .set hardfloat, by command line options -msoft-float or
260 -mhard-float. The default is false. */
261 bfd_boolean soft_float;
262
263 /* True if only single-precision floating-point operations are allowed.
264 Changed by .set singlefloat or .set doublefloat, command-line options
265 -msingle-float or -mdouble-float. The default is false. */
266 bfd_boolean single_float;
252b5132
RH
267};
268
037b32b9
AN
269/* This is the struct we use to hold the current set of options. Note
270 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
271 -1 to indicate that they have not been initialized. */
272
a325df1d 273/* True if -mgp32 was passed. */
a8e8e863 274static int file_mips_gp32 = -1;
a325df1d
TS
275
276/* True if -mfp32 was passed. */
a8e8e863 277static int file_mips_fp32 = -1;
a325df1d 278
037b32b9
AN
279/* 1 if -msoft-float, 0 if -mhard-float. The default is 0. */
280static int file_mips_soft_float = 0;
281
282/* 1 if -msingle-float, 0 if -mdouble-float. The default is 0. */
283static int file_mips_single_float = 0;
252b5132 284
e972090a
NC
285static struct mips_set_options mips_opts =
286{
846ef2d0 287 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
b015e599
AP
288 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
289 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* gp32 */ 0,
290 /* fp32 */ 0, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
291 /* soft_float */ FALSE, /* single_float */ FALSE
e7af610e 292};
252b5132 293
846ef2d0
RS
294/* The set of ASEs that were selected on the command line, either
295 explicitly via ASE options or implicitly through things like -march. */
296static unsigned int file_ase;
297
298/* Which bits of file_ase were explicitly set or cleared by ASE options. */
299static unsigned int file_ase_explicit;
300
252b5132
RH
301/* These variables are filled in with the masks of registers used.
302 The object format code reads them and puts them in the appropriate
303 place. */
304unsigned long mips_gprmask;
305unsigned long mips_cprmask[4];
306
307/* MIPS ISA we are using for this output file. */
e7af610e 308static int file_mips_isa = ISA_UNKNOWN;
252b5132 309
738f4d98 310/* True if any MIPS16 code was produced. */
a4672219
TS
311static int file_ase_mips16;
312
3994f87e
TS
313#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
314 || mips_opts.isa == ISA_MIPS32R2 \
315 || mips_opts.isa == ISA_MIPS64 \
316 || mips_opts.isa == ISA_MIPS64R2)
317
df58fc94
RS
318/* True if any microMIPS code was produced. */
319static int file_ase_micromips;
320
b12dd2e4
CF
321/* True if we want to create R_MIPS_JALR for jalr $25. */
322#ifdef TE_IRIX
1180b5a4 323#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 324#else
1180b5a4
RS
325/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
326 because there's no place for any addend, the only acceptable
327 expression is a bare symbol. */
328#define MIPS_JALR_HINT_P(EXPR) \
329 (!HAVE_IN_PLACE_ADDENDS \
330 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
331#endif
332
ad3fea08
TS
333#define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32 \
334 || mips_opts.isa == ISA_MIPS32R2)
e16bfa71 335
ad3fea08 336#define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2 \
03f66e8a
MR
337 || mips_opts.isa == ISA_MIPS64R2 \
338 || mips_opts.micromips)
ad3fea08 339
65263ce3
TS
340#define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
341
8b082fb1 342#define ISA_SUPPORTS_DSPR2_ASE (mips_opts.isa == ISA_MIPS32R2 \
03f66e8a
MR
343 || mips_opts.isa == ISA_MIPS64R2 \
344 || mips_opts.micromips)
8b082fb1 345
7f3c4072
CM
346#define ISA_SUPPORTS_EVA_ASE (mips_opts.isa == ISA_MIPS32R2 \
347 || mips_opts.isa == ISA_MIPS64R2 \
348 || mips_opts.micromips)
349
ad3fea08
TS
350#define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2 \
351 || mips_opts.isa == ISA_MIPS64R2)
352
dec0624d 353#define ISA_SUPPORTS_MCU_ASE (mips_opts.isa == ISA_MIPS32R2 \
9ddc84cc
MR
354 || mips_opts.isa == ISA_MIPS64R2 \
355 || mips_opts.micromips)
dec0624d 356
b015e599 357#define ISA_SUPPORTS_VIRT_ASE (mips_opts.isa == ISA_MIPS32R2 \
f122319e
CF
358 || mips_opts.isa == ISA_MIPS64R2 \
359 || mips_opts.micromips)
b015e599 360
f122319e
CF
361#define ISA_SUPPORTS_VIRT64_ASE (mips_opts.isa == ISA_MIPS64R2 \
362 || (mips_opts.micromips \
363 && ISA_HAS_64BIT_REGS (mips_opts.isa)))
b015e599 364
ec68c924 365/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 366static int file_mips_arch = CPU_UNKNOWN;
316f5878 367static const char *mips_arch_string;
ec68c924
EC
368
369/* The argument of the -mtune= flag. The architecture for which we
370 are optimizing. */
371static int mips_tune = CPU_UNKNOWN;
316f5878 372static const char *mips_tune_string;
ec68c924 373
316f5878 374/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
375static int mips_32bitmode = 0;
376
316f5878
RS
377/* True if the given ABI requires 32-bit registers. */
378#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
379
380/* Likewise 64-bit registers. */
707bfff6
TS
381#define ABI_NEEDS_64BIT_REGS(ABI) \
382 ((ABI) == N32_ABI \
383 || (ABI) == N64_ABI \
316f5878
RS
384 || (ABI) == O64_ABI)
385
ad3fea08 386/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
387#define ISA_HAS_64BIT_REGS(ISA) \
388 ((ISA) == ISA_MIPS3 \
389 || (ISA) == ISA_MIPS4 \
390 || (ISA) == ISA_MIPS5 \
391 || (ISA) == ISA_MIPS64 \
392 || (ISA) == ISA_MIPS64R2)
9ce8a5dd 393
ad3fea08
TS
394/* Return true if ISA supports 64 bit wide float registers. */
395#define ISA_HAS_64BIT_FPRS(ISA) \
396 ((ISA) == ISA_MIPS3 \
397 || (ISA) == ISA_MIPS4 \
398 || (ISA) == ISA_MIPS5 \
399 || (ISA) == ISA_MIPS32R2 \
400 || (ISA) == ISA_MIPS64 \
401 || (ISA) == ISA_MIPS64R2)
402
af7ee8bf
CD
403/* Return true if ISA supports 64-bit right rotate (dror et al.)
404 instructions. */
707bfff6 405#define ISA_HAS_DROR(ISA) \
df58fc94
RS
406 ((ISA) == ISA_MIPS64R2 \
407 || (mips_opts.micromips \
408 && ISA_HAS_64BIT_REGS (ISA)) \
409 )
af7ee8bf
CD
410
411/* Return true if ISA supports 32-bit right rotate (ror et al.)
412 instructions. */
707bfff6
TS
413#define ISA_HAS_ROR(ISA) \
414 ((ISA) == ISA_MIPS32R2 \
415 || (ISA) == ISA_MIPS64R2 \
846ef2d0 416 || (mips_opts.ase & ASE_SMARTMIPS) \
df58fc94
RS
417 || mips_opts.micromips \
418 )
707bfff6 419
7455baf8
TS
420/* Return true if ISA supports single-precision floats in odd registers. */
421#define ISA_HAS_ODD_SINGLE_FPR(ISA) \
422 ((ISA) == ISA_MIPS32 \
423 || (ISA) == ISA_MIPS32R2 \
424 || (ISA) == ISA_MIPS64 \
425 || (ISA) == ISA_MIPS64R2)
af7ee8bf 426
ad3fea08
TS
427/* Return true if ISA supports move to/from high part of a 64-bit
428 floating-point register. */
429#define ISA_HAS_MXHC1(ISA) \
430 ((ISA) == ISA_MIPS32R2 \
431 || (ISA) == ISA_MIPS64R2)
432
e013f690 433#define HAVE_32BIT_GPRS \
ad3fea08 434 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 435
e013f690 436#define HAVE_32BIT_FPRS \
ad3fea08 437 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
ca4e0257 438
ad3fea08
TS
439#define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
440#define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
ca4e0257 441
316f5878 442#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 443
316f5878 444#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 445
3b91255e
RS
446/* True if relocations are stored in-place. */
447#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
448
aed1a261
RS
449/* The ABI-derived address size. */
450#define HAVE_64BIT_ADDRESSES \
451 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
452#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 453
aed1a261
RS
454/* The size of symbolic constants (i.e., expressions of the form
455 "SYMBOL" or "SYMBOL + OFFSET"). */
456#define HAVE_32BIT_SYMBOLS \
457 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
458#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 459
b7c7d6c1
TS
460/* Addresses are loaded in different ways, depending on the address size
461 in use. The n32 ABI Documentation also mandates the use of additions
462 with overflow checking, but existing implementations don't follow it. */
f899b4b8 463#define ADDRESS_ADD_INSN \
b7c7d6c1 464 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
465
466#define ADDRESS_ADDI_INSN \
b7c7d6c1 467 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
468
469#define ADDRESS_LOAD_INSN \
470 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
471
472#define ADDRESS_STORE_INSN \
473 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
474
a4672219 475/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
476#define CPU_HAS_MIPS16(cpu) \
477 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
478 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 479
2309ddf2 480/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
481#define CPU_HAS_MICROMIPS(cpu) 0
482
60b63b72
RS
483/* True if CPU has a dror instruction. */
484#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
485
486/* True if CPU has a ror instruction. */
487#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
488
dd6a37e7 489/* True if CPU is in the Octeon family */
432233b3 490#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
dd6a37e7 491
dd3cbb7e 492/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 493#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 494
0aa27725
RS
495/* True, if CPU has support for ldc1 and sdc1. */
496#define CPU_HAS_LDC1_SDC1(CPU) \
497 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
498
c8978940
CD
499/* True if mflo and mfhi can be immediately followed by instructions
500 which write to the HI and LO registers.
501
502 According to MIPS specifications, MIPS ISAs I, II, and III need
503 (at least) two instructions between the reads of HI/LO and
504 instructions which write them, and later ISAs do not. Contradicting
505 the MIPS specifications, some MIPS IV processor user manuals (e.g.
506 the UM for the NEC Vr5000) document needing the instructions between
507 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
508 MIPS64 and later ISAs to have the interlocks, plus any specific
509 earlier-ISA CPUs for which CPU documentation declares that the
510 instructions are really interlocked. */
511#define hilo_interlocks \
512 (mips_opts.isa == ISA_MIPS32 \
513 || mips_opts.isa == ISA_MIPS32R2 \
514 || mips_opts.isa == ISA_MIPS64 \
515 || mips_opts.isa == ISA_MIPS64R2 \
516 || mips_opts.arch == CPU_R4010 \
e407c74b 517 || mips_opts.arch == CPU_R5900 \
c8978940
CD
518 || mips_opts.arch == CPU_R10000 \
519 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
520 || mips_opts.arch == CPU_R14000 \
521 || mips_opts.arch == CPU_R16000 \
c8978940 522 || mips_opts.arch == CPU_RM7000 \
c8978940 523 || mips_opts.arch == CPU_VR5500 \
df58fc94 524 || mips_opts.micromips \
c8978940 525 )
252b5132
RH
526
527/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
528 from the GPRs after they are loaded from memory, and thus does not
529 require nops to be inserted. This applies to instructions marked
530 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
df58fc94
RS
531 level I and microMIPS mode instructions are always interlocked. */
532#define gpr_interlocks \
533 (mips_opts.isa != ISA_MIPS1 \
534 || mips_opts.arch == CPU_R3900 \
e407c74b 535 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
536 || mips_opts.micromips \
537 )
252b5132 538
81912461
ILT
539/* Whether the processor uses hardware interlocks to avoid delays
540 required by coprocessor instructions, and thus does not require
541 nops to be inserted. This applies to instructions marked
542 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
543 between instructions marked INSN_WRITE_COND_CODE and ones marked
544 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
545 levels I, II, and III and microMIPS mode instructions are always
546 interlocked. */
bdaaa2e1 547/* Itbl support may require additional care here. */
81912461
ILT
548#define cop_interlocks \
549 ((mips_opts.isa != ISA_MIPS1 \
550 && mips_opts.isa != ISA_MIPS2 \
551 && mips_opts.isa != ISA_MIPS3) \
552 || mips_opts.arch == CPU_R4300 \
df58fc94 553 || mips_opts.micromips \
81912461
ILT
554 )
555
556/* Whether the processor uses hardware interlocks to protect reads
557 from coprocessor registers after they are loaded from memory, and
558 thus does not require nops to be inserted. This applies to
559 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
560 requires at MIPS ISA level I and microMIPS mode instructions are
561 always interlocked. */
562#define cop_mem_interlocks \
563 (mips_opts.isa != ISA_MIPS1 \
564 || mips_opts.micromips \
565 )
252b5132 566
6b76fefe
CM
567/* Is this a mfhi or mflo instruction? */
568#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
569 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
570
df58fc94
RS
571/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
572 has been selected. This implies, in particular, that addresses of text
573 labels have their LSB set. */
574#define HAVE_CODE_COMPRESSION \
575 ((mips_opts.mips16 | mips_opts.micromips) != 0)
576
252b5132
RH
577/* MIPS PIC level. */
578
a161fe53 579enum mips_pic_level mips_pic;
252b5132 580
c9914766 581/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 582 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 583static int mips_big_got = 0;
252b5132
RH
584
585/* 1 if trap instructions should used for overflow rather than break
586 instructions. */
c9914766 587static int mips_trap = 0;
252b5132 588
119d663a 589/* 1 if double width floating point constants should not be constructed
b6ff326e 590 by assembling two single width halves into two single width floating
119d663a
NC
591 point registers which just happen to alias the double width destination
592 register. On some architectures this aliasing can be disabled by a bit
d547a75e 593 in the status register, and the setting of this bit cannot be determined
119d663a
NC
594 automatically at assemble time. */
595static int mips_disable_float_construction;
596
252b5132
RH
597/* Non-zero if any .set noreorder directives were used. */
598
599static int mips_any_noreorder;
600
6b76fefe
CM
601/* Non-zero if nops should be inserted when the register referenced in
602 an mfhi/mflo instruction is read in the next two instructions. */
603static int mips_7000_hilo_fix;
604
02ffd3e4 605/* The size of objects in the small data section. */
156c2f8b 606static unsigned int g_switch_value = 8;
252b5132
RH
607/* Whether the -G option was used. */
608static int g_switch_seen = 0;
609
610#define N_RMASK 0xc4
611#define N_VFP 0xd4
612
613/* If we can determine in advance that GP optimization won't be
614 possible, we can skip the relaxation stuff that tries to produce
615 GP-relative references. This makes delay slot optimization work
616 better.
617
618 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
619 gcc output. It needs to guess right for gcc, otherwise gcc
620 will put what it thinks is a GP-relative instruction in a branch
621 delay slot.
252b5132
RH
622
623 I don't know if a fix is needed for the SVR4_PIC mode. I've only
624 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 625static int nopic_need_relax (symbolS *, int);
252b5132
RH
626
627/* handle of the OPCODE hash table */
628static struct hash_control *op_hash = NULL;
629
630/* The opcode hash table we use for the mips16. */
631static struct hash_control *mips16_op_hash = NULL;
632
df58fc94
RS
633/* The opcode hash table we use for the microMIPS ASE. */
634static struct hash_control *micromips_op_hash = NULL;
635
252b5132
RH
636/* This array holds the chars that always start a comment. If the
637 pre-processor is disabled, these aren't very useful */
638const char comment_chars[] = "#";
639
640/* This array holds the chars that only start a comment at the beginning of
641 a line. If the line seems to have the form '# 123 filename'
642 .line and .file directives will appear in the pre-processed output */
643/* Note that input_file.c hand checks for '#' at the beginning of the
644 first line of the input file. This is because the compiler outputs
bdaaa2e1 645 #NO_APP at the beginning of its output. */
252b5132
RH
646/* Also note that C style comments are always supported. */
647const char line_comment_chars[] = "#";
648
bdaaa2e1 649/* This array holds machine specific line separator characters. */
63a0b638 650const char line_separator_chars[] = ";";
252b5132
RH
651
652/* Chars that can be used to separate mant from exp in floating point nums */
653const char EXP_CHARS[] = "eE";
654
655/* Chars that mean this number is a floating point constant */
656/* As in 0f12.456 */
657/* or 0d1.2345e12 */
658const char FLT_CHARS[] = "rRsSfFdDxXpP";
659
660/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
661 changed in read.c . Ideally it shouldn't have to know about it at all,
662 but nothing is ideal around here.
663 */
664
665static char *insn_error;
666
667static int auto_align = 1;
668
669/* When outputting SVR4 PIC code, the assembler needs to know the
670 offset in the stack frame from which to restore the $gp register.
671 This is set by the .cprestore pseudo-op, and saved in this
672 variable. */
673static offsetT mips_cprestore_offset = -1;
674
67c1ffbe 675/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 676 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 677 offset and even an other register than $gp as global pointer. */
6478892d
TS
678static offsetT mips_cpreturn_offset = -1;
679static int mips_cpreturn_register = -1;
680static int mips_gp_register = GP;
def2e0dd 681static int mips_gprel_offset = 0;
6478892d 682
7a621144
DJ
683/* Whether mips_cprestore_offset has been set in the current function
684 (or whether it has already been warned about, if not). */
685static int mips_cprestore_valid = 0;
686
252b5132
RH
687/* This is the register which holds the stack frame, as set by the
688 .frame pseudo-op. This is needed to implement .cprestore. */
689static int mips_frame_reg = SP;
690
7a621144
DJ
691/* Whether mips_frame_reg has been set in the current function
692 (or whether it has already been warned about, if not). */
693static int mips_frame_reg_valid = 0;
694
252b5132
RH
695/* To output NOP instructions correctly, we need to keep information
696 about the previous two instructions. */
697
698/* Whether we are optimizing. The default value of 2 means to remove
699 unneeded NOPs and swap branch instructions when possible. A value
700 of 1 means to not swap branches. A value of 0 means to always
701 insert NOPs. */
702static int mips_optimize = 2;
703
704/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
705 equivalent to seeing no -g option at all. */
706static int mips_debug = 0;
707
7d8e00cf
RS
708/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
709#define MAX_VR4130_NOPS 4
710
711/* The maximum number of NOPs needed to fill delay slots. */
712#define MAX_DELAY_NOPS 2
713
714/* The maximum number of NOPs needed for any purpose. */
715#define MAX_NOPS 4
71400594
RS
716
717/* A list of previous instructions, with index 0 being the most recent.
718 We need to look back MAX_NOPS instructions when filling delay slots
719 or working around processor errata. We need to look back one
720 instruction further if we're thinking about using history[0] to
721 fill a branch delay slot. */
722static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 723
1e915849 724/* Nop instructions used by emit_nop. */
df58fc94
RS
725static struct mips_cl_insn nop_insn;
726static struct mips_cl_insn mips16_nop_insn;
727static struct mips_cl_insn micromips_nop16_insn;
728static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
729
730/* The appropriate nop for the current mode. */
df58fc94
RS
731#define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn \
732 : (mips_opts.micromips ? &micromips_nop16_insn : &nop_insn))
733
734/* The size of NOP_INSN in bytes. */
735#define NOP_INSN_SIZE (HAVE_CODE_COMPRESSION ? 2 : 4)
252b5132 736
252b5132
RH
737/* If this is set, it points to a frag holding nop instructions which
738 were inserted before the start of a noreorder section. If those
739 nops turn out to be unnecessary, the size of the frag can be
740 decreased. */
741static fragS *prev_nop_frag;
742
743/* The number of nop instructions we created in prev_nop_frag. */
744static int prev_nop_frag_holds;
745
746/* The number of nop instructions that we know we need in
bdaaa2e1 747 prev_nop_frag. */
252b5132
RH
748static int prev_nop_frag_required;
749
750/* The number of instructions we've seen since prev_nop_frag. */
751static int prev_nop_frag_since;
752
753/* For ECOFF and ELF, relocations against symbols are done in two
754 parts, with a HI relocation and a LO relocation. Each relocation
755 has only 16 bits of space to store an addend. This means that in
756 order for the linker to handle carries correctly, it must be able
757 to locate both the HI and the LO relocation. This means that the
758 relocations must appear in order in the relocation table.
759
760 In order to implement this, we keep track of each unmatched HI
761 relocation. We then sort them so that they immediately precede the
bdaaa2e1 762 corresponding LO relocation. */
252b5132 763
e972090a
NC
764struct mips_hi_fixup
765{
252b5132
RH
766 /* Next HI fixup. */
767 struct mips_hi_fixup *next;
768 /* This fixup. */
769 fixS *fixp;
770 /* The section this fixup is in. */
771 segT seg;
772};
773
774/* The list of unmatched HI relocs. */
775
776static struct mips_hi_fixup *mips_hi_fixup_list;
777
64bdfcaf
RS
778/* The frag containing the last explicit relocation operator.
779 Null if explicit relocations have not been used. */
780
781static fragS *prev_reloc_op_frag;
782
252b5132
RH
783/* Map normal MIPS register numbers to mips16 register numbers. */
784
785#define X ILLEGAL_REG
e972090a
NC
786static const int mips32_to_16_reg_map[] =
787{
252b5132
RH
788 X, X, 2, 3, 4, 5, 6, 7,
789 X, X, X, X, X, X, X, X,
790 0, 1, X, X, X, X, X, X,
791 X, X, X, X, X, X, X, X
792};
793#undef X
794
795/* Map mips16 register numbers to normal MIPS register numbers. */
796
e972090a
NC
797static const unsigned int mips16_to_32_reg_map[] =
798{
252b5132
RH
799 16, 17, 2, 3, 4, 5, 6, 7
800};
60b63b72 801
df58fc94
RS
802/* Map normal MIPS register numbers to microMIPS register numbers. */
803
804#define mips32_to_micromips_reg_b_map mips32_to_16_reg_map
805#define mips32_to_micromips_reg_c_map mips32_to_16_reg_map
806#define mips32_to_micromips_reg_d_map mips32_to_16_reg_map
807#define mips32_to_micromips_reg_e_map mips32_to_16_reg_map
808#define mips32_to_micromips_reg_f_map mips32_to_16_reg_map
809#define mips32_to_micromips_reg_g_map mips32_to_16_reg_map
810#define mips32_to_micromips_reg_l_map mips32_to_16_reg_map
811
812#define X ILLEGAL_REG
813/* reg type h: 4, 5, 6. */
814static const int mips32_to_micromips_reg_h_map[] =
815{
816 X, X, X, X, 4, 5, 6, X,
817 X, X, X, X, X, X, X, X,
818 X, X, X, X, X, X, X, X,
819 X, X, X, X, X, X, X, X
820};
821
822/* reg type m: 0, 17, 2, 3, 16, 18, 19, 20. */
823static const int mips32_to_micromips_reg_m_map[] =
824{
825 0, X, 2, 3, X, X, X, X,
826 X, X, X, X, X, X, X, X,
827 4, 1, 5, 6, 7, X, X, X,
828 X, X, X, X, X, X, X, X
829};
830
831/* reg type q: 0, 2-7. 17. */
832static const int mips32_to_micromips_reg_q_map[] =
833{
834 0, X, 2, 3, 4, 5, 6, 7,
835 X, X, X, X, X, X, X, X,
836 X, 1, X, X, X, X, X, X,
837 X, X, X, X, X, X, X, X
838};
839
840#define mips32_to_micromips_reg_n_map mips32_to_micromips_reg_m_map
841#undef X
842
843/* Map microMIPS register numbers to normal MIPS register numbers. */
844
845#define micromips_to_32_reg_b_map mips16_to_32_reg_map
846#define micromips_to_32_reg_c_map mips16_to_32_reg_map
847#define micromips_to_32_reg_d_map mips16_to_32_reg_map
848#define micromips_to_32_reg_e_map mips16_to_32_reg_map
849#define micromips_to_32_reg_f_map mips16_to_32_reg_map
850#define micromips_to_32_reg_g_map mips16_to_32_reg_map
851
852/* The microMIPS registers with type h. */
853static const unsigned int micromips_to_32_reg_h_map[] =
854{
855 5, 5, 6, 4, 4, 4, 4, 4
856};
857
858/* The microMIPS registers with type i. */
859static const unsigned int micromips_to_32_reg_i_map[] =
860{
861 6, 7, 7, 21, 22, 5, 6, 7
862};
863
864#define micromips_to_32_reg_l_map mips16_to_32_reg_map
865
866/* The microMIPS registers with type m. */
867static const unsigned int micromips_to_32_reg_m_map[] =
868{
869 0, 17, 2, 3, 16, 18, 19, 20
870};
871
872#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
873
874/* The microMIPS registers with type q. */
875static const unsigned int micromips_to_32_reg_q_map[] =
876{
877 0, 17, 2, 3, 4, 5, 6, 7
878};
879
880/* microMIPS imm type B. */
881static const int micromips_imm_b_map[] =
882{
883 1, 4, 8, 12, 16, 20, 24, -1
884};
885
886/* microMIPS imm type C. */
887static const int micromips_imm_c_map[] =
888{
889 128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
890};
891
71400594
RS
892/* Classifies the kind of instructions we're interested in when
893 implementing -mfix-vr4120. */
c67a084a
NC
894enum fix_vr4120_class
895{
71400594
RS
896 FIX_VR4120_MACC,
897 FIX_VR4120_DMACC,
898 FIX_VR4120_MULT,
899 FIX_VR4120_DMULT,
900 FIX_VR4120_DIV,
901 FIX_VR4120_MTHILO,
902 NUM_FIX_VR4120_CLASSES
903};
904
c67a084a
NC
905/* ...likewise -mfix-loongson2f-jump. */
906static bfd_boolean mips_fix_loongson2f_jump;
907
908/* ...likewise -mfix-loongson2f-nop. */
909static bfd_boolean mips_fix_loongson2f_nop;
910
911/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
912static bfd_boolean mips_fix_loongson2f;
913
71400594
RS
914/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
915 there must be at least one other instruction between an instruction
916 of type X and an instruction of type Y. */
917static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
918
919/* True if -mfix-vr4120 is in force. */
d766e8ec 920static int mips_fix_vr4120;
4a6a3df4 921
7d8e00cf
RS
922/* ...likewise -mfix-vr4130. */
923static int mips_fix_vr4130;
924
6a32d874
CM
925/* ...likewise -mfix-24k. */
926static int mips_fix_24k;
927
d954098f
DD
928/* ...likewise -mfix-cn63xxp1 */
929static bfd_boolean mips_fix_cn63xxp1;
930
4a6a3df4
AO
931/* We don't relax branches by default, since this causes us to expand
932 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
933 fail to compute the offset before expanding the macro to the most
934 efficient expansion. */
935
936static int mips_relax_branch;
252b5132 937\f
4d7206a2
RS
938/* The expansion of many macros depends on the type of symbol that
939 they refer to. For example, when generating position-dependent code,
940 a macro that refers to a symbol may have two different expansions,
941 one which uses GP-relative addresses and one which uses absolute
942 addresses. When generating SVR4-style PIC, a macro may have
943 different expansions for local and global symbols.
944
945 We handle these situations by generating both sequences and putting
946 them in variant frags. In position-dependent code, the first sequence
947 will be the GP-relative one and the second sequence will be the
948 absolute one. In SVR4 PIC, the first sequence will be for global
949 symbols and the second will be for local symbols.
950
584892a6
RS
951 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
952 SECOND are the lengths of the two sequences in bytes. These fields
953 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
954 the subtype has the following flags:
4d7206a2 955
584892a6
RS
956 RELAX_USE_SECOND
957 Set if it has been decided that we should use the second
958 sequence instead of the first.
959
960 RELAX_SECOND_LONGER
961 Set in the first variant frag if the macro's second implementation
962 is longer than its first. This refers to the macro as a whole,
963 not an individual relaxation.
964
965 RELAX_NOMACRO
966 Set in the first variant frag if the macro appeared in a .set nomacro
967 block and if one alternative requires a warning but the other does not.
968
969 RELAX_DELAY_SLOT
970 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
971 delay slot.
4d7206a2 972
df58fc94
RS
973 RELAX_DELAY_SLOT_16BIT
974 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
975 16-bit instruction.
976
977 RELAX_DELAY_SLOT_SIZE_FIRST
978 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
979 the macro is of the wrong size for the branch delay slot.
980
981 RELAX_DELAY_SLOT_SIZE_SECOND
982 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
983 the macro is of the wrong size for the branch delay slot.
984
4d7206a2
RS
985 The frag's "opcode" points to the first fixup for relaxable code.
986
987 Relaxable macros are generated using a sequence such as:
988
989 relax_start (SYMBOL);
990 ... generate first expansion ...
991 relax_switch ();
992 ... generate second expansion ...
993 relax_end ();
994
995 The code and fixups for the unwanted alternative are discarded
996 by md_convert_frag. */
584892a6 997#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 998
584892a6
RS
999#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1000#define RELAX_SECOND(X) ((X) & 0xff)
1001#define RELAX_USE_SECOND 0x10000
1002#define RELAX_SECOND_LONGER 0x20000
1003#define RELAX_NOMACRO 0x40000
1004#define RELAX_DELAY_SLOT 0x80000
df58fc94
RS
1005#define RELAX_DELAY_SLOT_16BIT 0x100000
1006#define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
1007#define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
252b5132 1008
4a6a3df4
AO
1009/* Branch without likely bit. If label is out of range, we turn:
1010
1011 beq reg1, reg2, label
1012 delay slot
1013
1014 into
1015
1016 bne reg1, reg2, 0f
1017 nop
1018 j label
1019 0: delay slot
1020
1021 with the following opcode replacements:
1022
1023 beq <-> bne
1024 blez <-> bgtz
1025 bltz <-> bgez
1026 bc1f <-> bc1t
1027
1028 bltzal <-> bgezal (with jal label instead of j label)
1029
1030 Even though keeping the delay slot instruction in the delay slot of
1031 the branch would be more efficient, it would be very tricky to do
1032 correctly, because we'd have to introduce a variable frag *after*
1033 the delay slot instruction, and expand that instead. Let's do it
1034 the easy way for now, even if the branch-not-taken case now costs
1035 one additional instruction. Out-of-range branches are not supposed
1036 to be common, anyway.
1037
1038 Branch likely. If label is out of range, we turn:
1039
1040 beql reg1, reg2, label
1041 delay slot (annulled if branch not taken)
1042
1043 into
1044
1045 beql reg1, reg2, 1f
1046 nop
1047 beql $0, $0, 2f
1048 nop
1049 1: j[al] label
1050 delay slot (executed only if branch taken)
1051 2:
1052
1053 It would be possible to generate a shorter sequence by losing the
1054 likely bit, generating something like:
b34976b6 1055
4a6a3df4
AO
1056 bne reg1, reg2, 0f
1057 nop
1058 j[al] label
1059 delay slot (executed only if branch taken)
1060 0:
1061
1062 beql -> bne
1063 bnel -> beq
1064 blezl -> bgtz
1065 bgtzl -> blez
1066 bltzl -> bgez
1067 bgezl -> bltz
1068 bc1fl -> bc1t
1069 bc1tl -> bc1f
1070
1071 bltzall -> bgezal (with jal label instead of j label)
1072 bgezall -> bltzal (ditto)
1073
1074
1075 but it's not clear that it would actually improve performance. */
66b3e8da
MR
1076#define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1077 ((relax_substateT) \
1078 (0xc0000000 \
1079 | ((at) & 0x1f) \
1080 | ((toofar) ? 0x20 : 0) \
1081 | ((link) ? 0x40 : 0) \
1082 | ((likely) ? 0x80 : 0) \
1083 | ((uncond) ? 0x100 : 0)))
4a6a3df4 1084#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
66b3e8da
MR
1085#define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1086#define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1087#define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1088#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1089#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1090
252b5132
RH
1091/* For mips16 code, we use an entirely different form of relaxation.
1092 mips16 supports two versions of most instructions which take
1093 immediate values: a small one which takes some small value, and a
1094 larger one which takes a 16 bit value. Since branches also follow
1095 this pattern, relaxing these values is required.
1096
1097 We can assemble both mips16 and normal MIPS code in a single
1098 object. Therefore, we need to support this type of relaxation at
1099 the same time that we support the relaxation described above. We
1100 use the high bit of the subtype field to distinguish these cases.
1101
1102 The information we store for this type of relaxation is the
1103 argument code found in the opcode file for this relocation, whether
1104 the user explicitly requested a small or extended form, and whether
1105 the relocation is in a jump or jal delay slot. That tells us the
1106 size of the value, and how it should be stored. We also store
1107 whether the fragment is considered to be extended or not. We also
1108 store whether this is known to be a branch to a different section,
1109 whether we have tried to relax this frag yet, and whether we have
1110 ever extended a PC relative fragment because of a shift count. */
1111#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1112 (0x80000000 \
1113 | ((type) & 0xff) \
1114 | ((small) ? 0x100 : 0) \
1115 | ((ext) ? 0x200 : 0) \
1116 | ((dslot) ? 0x400 : 0) \
1117 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 1118#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
1119#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1120#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1121#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1122#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1123#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1124#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1125#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1126#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1127#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1128#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1129#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95 1130
df58fc94
RS
1131/* For microMIPS code, we use relaxation similar to one we use for
1132 MIPS16 code. Some instructions that take immediate values support
1133 two encodings: a small one which takes some small value, and a
1134 larger one which takes a 16 bit value. As some branches also follow
1135 this pattern, relaxing these values is required.
1136
1137 We can assemble both microMIPS and normal MIPS code in a single
1138 object. Therefore, we need to support this type of relaxation at
1139 the same time that we support the relaxation described above. We
1140 use one of the high bits of the subtype field to distinguish these
1141 cases.
1142
1143 The information we store for this type of relaxation is the argument
1144 code found in the opcode file for this relocation, the register
40209cad
MR
1145 selected as the assembler temporary, whether the branch is
1146 unconditional, whether it is compact, whether it stores the link
1147 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1148 branches to a sequence of instructions is enabled, and whether the
1149 displacement of a branch is too large to fit as an immediate argument
1150 of a 16-bit and a 32-bit branch, respectively. */
1151#define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1152 relax32, toofar16, toofar32) \
1153 (0x40000000 \
1154 | ((type) & 0xff) \
1155 | (((at) & 0x1f) << 8) \
1156 | ((uncond) ? 0x2000 : 0) \
1157 | ((compact) ? 0x4000 : 0) \
1158 | ((link) ? 0x8000 : 0) \
1159 | ((relax32) ? 0x10000 : 0) \
1160 | ((toofar16) ? 0x20000 : 0) \
1161 | ((toofar32) ? 0x40000 : 0))
df58fc94
RS
1162#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1163#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1164#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
40209cad
MR
1165#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1166#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1167#define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1168#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1169
1170#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1171#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1172#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1173#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1174#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1175#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
df58fc94 1176
43c0598f
RS
1177/* Sign-extend 16-bit value X. */
1178#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1179
885add95
CD
1180/* Is the given value a sign-extended 32-bit value? */
1181#define IS_SEXT_32BIT_NUM(x) \
1182 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1183 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1184
1185/* Is the given value a sign-extended 16-bit value? */
1186#define IS_SEXT_16BIT_NUM(x) \
1187 (((x) &~ (offsetT) 0x7fff) == 0 \
1188 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1189
df58fc94
RS
1190/* Is the given value a sign-extended 12-bit value? */
1191#define IS_SEXT_12BIT_NUM(x) \
1192 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1193
7f3c4072
CM
1194/* Is the given value a sign-extended 9-bit value? */
1195#define IS_SEXT_9BIT_NUM(x) \
1196 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1197
2051e8c4
MR
1198/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1199#define IS_ZEXT_32BIT_NUM(x) \
1200 (((x) &~ (offsetT) 0xffffffff) == 0 \
1201 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1202
bf12938e
RS
1203/* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1204 VALUE << SHIFT. VALUE is evaluated exactly once. */
1205#define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1206 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1207 | (((VALUE) & (MASK)) << (SHIFT)))
1208
1209/* Extract bits MASK << SHIFT from STRUCT and shift them right
1210 SHIFT places. */
1211#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1212 (((STRUCT) >> (SHIFT)) & (MASK))
1213
1214/* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1215 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1216
1217 include/opcode/mips.h specifies operand fields using the macros
1218 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
1219 with "MIPS16OP" instead of "OP". */
df58fc94
RS
1220#define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1221 do \
1222 if (!(MICROMIPS)) \
1223 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1224 OP_MASK_##FIELD, OP_SH_##FIELD); \
1225 else \
1226 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1227 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1228 while (0)
bf12938e
RS
1229#define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1230 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1231 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1232
1233/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1234#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1235 (!(MICROMIPS) \
1236 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1237 : EXTRACT_BITS ((INSN).insn_opcode, \
1238 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1239#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1240 EXTRACT_BITS ((INSN).insn_opcode, \
1241 MIPS16OP_MASK_##FIELD, \
1242 MIPS16OP_SH_##FIELD)
5c04167a
RS
1243
1244/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1245#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1246\f
df58fc94
RS
1247/* Whether or not we are emitting a branch-likely macro. */
1248static bfd_boolean emit_branch_likely_macro = FALSE;
1249
4d7206a2
RS
1250/* Global variables used when generating relaxable macros. See the
1251 comment above RELAX_ENCODE for more details about how relaxation
1252 is used. */
1253static struct {
1254 /* 0 if we're not emitting a relaxable macro.
1255 1 if we're emitting the first of the two relaxation alternatives.
1256 2 if we're emitting the second alternative. */
1257 int sequence;
1258
1259 /* The first relaxable fixup in the current frag. (In other words,
1260 the first fixup that refers to relaxable code.) */
1261 fixS *first_fixup;
1262
1263 /* sizes[0] says how many bytes of the first alternative are stored in
1264 the current frag. Likewise sizes[1] for the second alternative. */
1265 unsigned int sizes[2];
1266
1267 /* The symbol on which the choice of sequence depends. */
1268 symbolS *symbol;
1269} mips_relax;
252b5132 1270\f
584892a6
RS
1271/* Global variables used to decide whether a macro needs a warning. */
1272static struct {
1273 /* True if the macro is in a branch delay slot. */
1274 bfd_boolean delay_slot_p;
1275
df58fc94
RS
1276 /* Set to the length in bytes required if the macro is in a delay slot
1277 that requires a specific length of instruction, otherwise zero. */
1278 unsigned int delay_slot_length;
1279
584892a6
RS
1280 /* For relaxable macros, sizes[0] is the length of the first alternative
1281 in bytes and sizes[1] is the length of the second alternative.
1282 For non-relaxable macros, both elements give the length of the
1283 macro in bytes. */
1284 unsigned int sizes[2];
1285
df58fc94
RS
1286 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1287 instruction of the first alternative in bytes and first_insn_sizes[1]
1288 is the length of the first instruction of the second alternative.
1289 For non-relaxable macros, both elements give the length of the first
1290 instruction in bytes.
1291
1292 Set to zero if we haven't yet seen the first instruction. */
1293 unsigned int first_insn_sizes[2];
1294
1295 /* For relaxable macros, insns[0] is the number of instructions for the
1296 first alternative and insns[1] is the number of instructions for the
1297 second alternative.
1298
1299 For non-relaxable macros, both elements give the number of
1300 instructions for the macro. */
1301 unsigned int insns[2];
1302
584892a6
RS
1303 /* The first variant frag for this macro. */
1304 fragS *first_frag;
1305} mips_macro_warning;
1306\f
252b5132
RH
1307/* Prototypes for static functions. */
1308
252b5132
RH
1309enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1310
b34976b6 1311static void append_insn
df58fc94
RS
1312 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1313 bfd_boolean expansionp);
7d10b47d 1314static void mips_no_prev_insn (void);
c67a084a 1315static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1316static void mips16_macro_build
03ea81db 1317 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1318static void load_register (int, expressionS *, int);
584892a6
RS
1319static void macro_start (void);
1320static void macro_end (void);
17a2f251
TS
1321static void macro (struct mips_cl_insn * ip);
1322static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1323static void mips_ip (char *str, struct mips_cl_insn * ip);
1324static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1325static void mips16_immed
43c0598f
RS
1326 (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1327 unsigned int, unsigned long *);
5e0116d5 1328static size_t my_getSmallExpression
17a2f251
TS
1329 (expressionS *, bfd_reloc_code_real_type *, char *);
1330static void my_getExpression (expressionS *, char *);
1331static void s_align (int);
1332static void s_change_sec (int);
1333static void s_change_section (int);
1334static void s_cons (int);
1335static void s_float_cons (int);
1336static void s_mips_globl (int);
1337static void s_option (int);
1338static void s_mipsset (int);
1339static void s_abicalls (int);
1340static void s_cpload (int);
1341static void s_cpsetup (int);
1342static void s_cplocal (int);
1343static void s_cprestore (int);
1344static void s_cpreturn (int);
741d6ea8
JM
1345static void s_dtprelword (int);
1346static void s_dtpreldword (int);
d0f13682
CLT
1347static void s_tprelword (int);
1348static void s_tpreldword (int);
17a2f251
TS
1349static void s_gpvalue (int);
1350static void s_gpword (int);
1351static void s_gpdword (int);
a3f278e2 1352static void s_ehword (int);
17a2f251
TS
1353static void s_cpadd (int);
1354static void s_insn (int);
1355static void md_obj_begin (void);
1356static void md_obj_end (void);
1357static void s_mips_ent (int);
1358static void s_mips_end (int);
1359static void s_mips_frame (int);
1360static void s_mips_mask (int reg_type);
1361static void s_mips_stab (int);
1362static void s_mips_weakext (int);
1363static void s_mips_file (int);
1364static void s_mips_loc (int);
1365static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1366static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 1367static int validate_mips_insn (const struct mips_opcode *);
df58fc94
RS
1368static int validate_micromips_insn (const struct mips_opcode *);
1369static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1370static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
e7af610e
NC
1371
1372/* Table and functions used to map between CPU/ISA names, and
1373 ISA levels, and CPU numbers. */
1374
e972090a
NC
1375struct mips_cpu_info
1376{
e7af610e 1377 const char *name; /* CPU or ISA name. */
d16afab6
RS
1378 int flags; /* MIPS_CPU_* flags. */
1379 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1380 int isa; /* ISA level. */
1381 int cpu; /* CPU number (default CPU if ISA). */
1382};
1383
ad3fea08 1384#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1385
17a2f251
TS
1386static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1387static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1388static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
1389\f
1390/* Pseudo-op table.
1391
1392 The following pseudo-ops from the Kane and Heinrich MIPS book
1393 should be defined here, but are currently unsupported: .alias,
1394 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1395
1396 The following pseudo-ops from the Kane and Heinrich MIPS book are
1397 specific to the type of debugging information being generated, and
1398 should be defined by the object format: .aent, .begin, .bend,
1399 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1400 .vreg.
1401
1402 The following pseudo-ops from the Kane and Heinrich MIPS book are
1403 not MIPS CPU specific, but are also not specific to the object file
1404 format. This file is probably the best place to define them, but
d84bcf09 1405 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1406
e972090a
NC
1407static const pseudo_typeS mips_pseudo_table[] =
1408{
beae10d5 1409 /* MIPS specific pseudo-ops. */
252b5132
RH
1410 {"option", s_option, 0},
1411 {"set", s_mipsset, 0},
1412 {"rdata", s_change_sec, 'r'},
1413 {"sdata", s_change_sec, 's'},
1414 {"livereg", s_ignore, 0},
1415 {"abicalls", s_abicalls, 0},
1416 {"cpload", s_cpload, 0},
6478892d
TS
1417 {"cpsetup", s_cpsetup, 0},
1418 {"cplocal", s_cplocal, 0},
252b5132 1419 {"cprestore", s_cprestore, 0},
6478892d 1420 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1421 {"dtprelword", s_dtprelword, 0},
1422 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1423 {"tprelword", s_tprelword, 0},
1424 {"tpreldword", s_tpreldword, 0},
6478892d 1425 {"gpvalue", s_gpvalue, 0},
252b5132 1426 {"gpword", s_gpword, 0},
10181a0d 1427 {"gpdword", s_gpdword, 0},
a3f278e2 1428 {"ehword", s_ehword, 0},
252b5132
RH
1429 {"cpadd", s_cpadd, 0},
1430 {"insn", s_insn, 0},
1431
beae10d5 1432 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1433 chips. */
38a57ae7 1434 {"asciiz", stringer, 8 + 1},
252b5132
RH
1435 {"bss", s_change_sec, 'b'},
1436 {"err", s_err, 0},
1437 {"half", s_cons, 1},
1438 {"dword", s_cons, 3},
1439 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1440 {"origin", s_org, 0},
1441 {"repeat", s_rept, 0},
252b5132 1442
998b3c36
MR
1443 /* For MIPS this is non-standard, but we define it for consistency. */
1444 {"sbss", s_change_sec, 'B'},
1445
beae10d5 1446 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1447 here for one reason or another. */
1448 {"align", s_align, 0},
1449 {"byte", s_cons, 0},
1450 {"data", s_change_sec, 'd'},
1451 {"double", s_float_cons, 'd'},
1452 {"float", s_float_cons, 'f'},
1453 {"globl", s_mips_globl, 0},
1454 {"global", s_mips_globl, 0},
1455 {"hword", s_cons, 1},
1456 {"int", s_cons, 2},
1457 {"long", s_cons, 2},
1458 {"octa", s_cons, 4},
1459 {"quad", s_cons, 3},
cca86cc8 1460 {"section", s_change_section, 0},
252b5132
RH
1461 {"short", s_cons, 1},
1462 {"single", s_float_cons, 'f'},
754e2bb9 1463 {"stabd", s_mips_stab, 'd'},
252b5132 1464 {"stabn", s_mips_stab, 'n'},
754e2bb9 1465 {"stabs", s_mips_stab, 's'},
252b5132
RH
1466 {"text", s_change_sec, 't'},
1467 {"word", s_cons, 2},
add56521 1468
add56521 1469 { "extern", ecoff_directive_extern, 0},
add56521 1470
43841e91 1471 { NULL, NULL, 0 },
252b5132
RH
1472};
1473
e972090a
NC
1474static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1475{
beae10d5
KH
1476 /* These pseudo-ops should be defined by the object file format.
1477 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1478 {"aent", s_mips_ent, 1},
1479 {"bgnb", s_ignore, 0},
1480 {"end", s_mips_end, 0},
1481 {"endb", s_ignore, 0},
1482 {"ent", s_mips_ent, 0},
c5dd6aab 1483 {"file", s_mips_file, 0},
252b5132
RH
1484 {"fmask", s_mips_mask, 'F'},
1485 {"frame", s_mips_frame, 0},
c5dd6aab 1486 {"loc", s_mips_loc, 0},
252b5132
RH
1487 {"mask", s_mips_mask, 'R'},
1488 {"verstamp", s_ignore, 0},
43841e91 1489 { NULL, NULL, 0 },
252b5132
RH
1490};
1491
3ae8dd8d
MR
1492/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1493 purpose of the `.dc.a' internal pseudo-op. */
1494
1495int
1496mips_address_bytes (void)
1497{
1498 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1499}
1500
17a2f251 1501extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1502
1503void
17a2f251 1504mips_pop_insert (void)
252b5132
RH
1505{
1506 pop_insert (mips_pseudo_table);
1507 if (! ECOFF_DEBUGGING)
1508 pop_insert (mips_nonecoff_pseudo_table);
1509}
1510\f
1511/* Symbols labelling the current insn. */
1512
e972090a
NC
1513struct insn_label_list
1514{
252b5132
RH
1515 struct insn_label_list *next;
1516 symbolS *label;
1517};
1518
252b5132 1519static struct insn_label_list *free_insn_labels;
742a56fe 1520#define label_list tc_segment_info_data.labels
252b5132 1521
17a2f251 1522static void mips_clear_insn_labels (void);
df58fc94
RS
1523static void mips_mark_labels (void);
1524static void mips_compressed_mark_labels (void);
252b5132
RH
1525
1526static inline void
17a2f251 1527mips_clear_insn_labels (void)
252b5132
RH
1528{
1529 register struct insn_label_list **pl;
a8dbcb85 1530 segment_info_type *si;
252b5132 1531
a8dbcb85
TS
1532 if (now_seg)
1533 {
1534 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1535 ;
1536
1537 si = seg_info (now_seg);
1538 *pl = si->label_list;
1539 si->label_list = NULL;
1540 }
252b5132 1541}
a8dbcb85 1542
df58fc94
RS
1543/* Mark instruction labels in MIPS16/microMIPS mode. */
1544
1545static inline void
1546mips_mark_labels (void)
1547{
1548 if (HAVE_CODE_COMPRESSION)
1549 mips_compressed_mark_labels ();
1550}
252b5132
RH
1551\f
1552static char *expr_end;
1553
1554/* Expressions which appear in instructions. These are set by
1555 mips_ip. */
1556
1557static expressionS imm_expr;
5f74bc13 1558static expressionS imm2_expr;
252b5132
RH
1559static expressionS offset_expr;
1560
1561/* Relocs associated with imm_expr and offset_expr. */
1562
f6688943
TS
1563static bfd_reloc_code_real_type imm_reloc[3]
1564 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1565static bfd_reloc_code_real_type offset_reloc[3]
1566 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1567
df58fc94
RS
1568/* This is set to the resulting size of the instruction to be produced
1569 by mips16_ip if an explicit extension is used or by mips_ip if an
1570 explicit size is supplied. */
252b5132 1571
df58fc94 1572static unsigned int forced_insn_length;
252b5132 1573
e1b47bd5
RS
1574/* True if we are assembling an instruction. All dot symbols defined during
1575 this time should be treated as code labels. */
1576
1577static bfd_boolean mips_assembling_insn;
1578
7ed4a06a 1579#ifdef OBJ_ELF
ecb4347a
DJ
1580/* The pdr segment for per procedure frame/regmask info. Not used for
1581 ECOFF debugging. */
252b5132
RH
1582
1583static segT pdr_seg;
7ed4a06a 1584#endif
252b5132 1585
e013f690
TS
1586/* The default target format to use. */
1587
aeffff67
RS
1588#if defined (TE_FreeBSD)
1589#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1590#elif defined (TE_TMIPS)
1591#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1592#else
1593#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1594#endif
1595
e013f690 1596const char *
17a2f251 1597mips_target_format (void)
e013f690
TS
1598{
1599 switch (OUTPUT_FLAVOR)
1600 {
e013f690
TS
1601 case bfd_target_ecoff_flavour:
1602 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1603 case bfd_target_coff_flavour:
1604 return "pe-mips";
1605 case bfd_target_elf_flavour:
0a44bf69
RS
1606#ifdef TE_VXWORKS
1607 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1608 return (target_big_endian
1609 ? "elf32-bigmips-vxworks"
1610 : "elf32-littlemips-vxworks");
1611#endif
e013f690 1612 return (target_big_endian
cfe86eaa 1613 ? (HAVE_64BIT_OBJECTS
aeffff67 1614 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1615 : (HAVE_NEWABI
aeffff67
RS
1616 ? ELF_TARGET ("elf32-n", "big")
1617 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1618 : (HAVE_64BIT_OBJECTS
aeffff67 1619 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1620 : (HAVE_NEWABI
aeffff67
RS
1621 ? ELF_TARGET ("elf32-n", "little")
1622 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1623 default:
1624 abort ();
1625 return NULL;
1626 }
1627}
1628
df58fc94
RS
1629/* Return the length of a microMIPS instruction in bytes. If bits of
1630 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1631 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1632 major opcode) will require further modifications to the opcode
1633 table. */
1634
1635static inline unsigned int
1636micromips_insn_length (const struct mips_opcode *mo)
1637{
1638 return (mo->mask >> 16) == 0 ? 2 : 4;
1639}
1640
5c04167a
RS
1641/* Return the length of MIPS16 instruction OPCODE. */
1642
1643static inline unsigned int
1644mips16_opcode_length (unsigned long opcode)
1645{
1646 return (opcode >> 16) == 0 ? 2 : 4;
1647}
1648
1e915849
RS
1649/* Return the length of instruction INSN. */
1650
1651static inline unsigned int
1652insn_length (const struct mips_cl_insn *insn)
1653{
df58fc94
RS
1654 if (mips_opts.micromips)
1655 return micromips_insn_length (insn->insn_mo);
1656 else if (mips_opts.mips16)
5c04167a 1657 return mips16_opcode_length (insn->insn_opcode);
df58fc94 1658 else
1e915849 1659 return 4;
1e915849
RS
1660}
1661
1662/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1663
1664static void
1665create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1666{
1667 size_t i;
1668
1669 insn->insn_mo = mo;
1e915849
RS
1670 insn->insn_opcode = mo->match;
1671 insn->frag = NULL;
1672 insn->where = 0;
1673 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1674 insn->fixp[i] = NULL;
1675 insn->fixed_p = (mips_opts.noreorder > 0);
1676 insn->noreorder_p = (mips_opts.noreorder > 0);
1677 insn->mips16_absolute_jump_p = 0;
15be625d 1678 insn->complete_p = 0;
e407c74b 1679 insn->cleared_p = 0;
1e915849
RS
1680}
1681
df58fc94 1682/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
1683
1684static void
df58fc94 1685mips_record_compressed_mode (void)
742a56fe
RS
1686{
1687 segment_info_type *si;
1688
1689 si = seg_info (now_seg);
1690 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1691 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
1692 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
1693 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
1694}
1695
4d68580a
RS
1696/* Read a standard MIPS instruction from BUF. */
1697
1698static unsigned long
1699read_insn (char *buf)
1700{
1701 if (target_big_endian)
1702 return bfd_getb32 ((bfd_byte *) buf);
1703 else
1704 return bfd_getl32 ((bfd_byte *) buf);
1705}
1706
1707/* Write standard MIPS instruction INSN to BUF. Return a pointer to
1708 the next byte. */
1709
1710static char *
1711write_insn (char *buf, unsigned int insn)
1712{
1713 md_number_to_chars (buf, insn, 4);
1714 return buf + 4;
1715}
1716
1717/* Read a microMIPS or MIPS16 opcode from BUF, given that it
1718 has length LENGTH. */
1719
1720static unsigned long
1721read_compressed_insn (char *buf, unsigned int length)
1722{
1723 unsigned long insn;
1724 unsigned int i;
1725
1726 insn = 0;
1727 for (i = 0; i < length; i += 2)
1728 {
1729 insn <<= 16;
1730 if (target_big_endian)
1731 insn |= bfd_getb16 ((char *) buf);
1732 else
1733 insn |= bfd_getl16 ((char *) buf);
1734 buf += 2;
1735 }
1736 return insn;
1737}
1738
5c04167a
RS
1739/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
1740 instruction is LENGTH bytes long. Return a pointer to the next byte. */
1741
1742static char *
1743write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
1744{
1745 unsigned int i;
1746
1747 for (i = 0; i < length; i += 2)
1748 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
1749 return buf + length;
1750}
1751
1e915849
RS
1752/* Install INSN at the location specified by its "frag" and "where" fields. */
1753
1754static void
1755install_insn (const struct mips_cl_insn *insn)
1756{
1757 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
1758 if (HAVE_CODE_COMPRESSION)
1759 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 1760 else
4d68580a 1761 write_insn (f, insn->insn_opcode);
df58fc94 1762 mips_record_compressed_mode ();
1e915849
RS
1763}
1764
1765/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1766 and install the opcode in the new location. */
1767
1768static void
1769move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1770{
1771 size_t i;
1772
1773 insn->frag = frag;
1774 insn->where = where;
1775 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1776 if (insn->fixp[i] != NULL)
1777 {
1778 insn->fixp[i]->fx_frag = frag;
1779 insn->fixp[i]->fx_where = where;
1780 }
1781 install_insn (insn);
1782}
1783
1784/* Add INSN to the end of the output. */
1785
1786static void
1787add_fixed_insn (struct mips_cl_insn *insn)
1788{
1789 char *f = frag_more (insn_length (insn));
1790 move_insn (insn, frag_now, f - frag_now->fr_literal);
1791}
1792
1793/* Start a variant frag and move INSN to the start of the variant part,
1794 marking it as fixed. The other arguments are as for frag_var. */
1795
1796static void
1797add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1798 relax_substateT subtype, symbolS *symbol, offsetT offset)
1799{
1800 frag_grow (max_chars);
1801 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1802 insn->fixed_p = 1;
1803 frag_var (rs_machine_dependent, max_chars, var,
1804 subtype, symbol, offset, NULL);
1805}
1806
1807/* Insert N copies of INSN into the history buffer, starting at
1808 position FIRST. Neither FIRST nor N need to be clipped. */
1809
1810static void
1811insert_into_history (unsigned int first, unsigned int n,
1812 const struct mips_cl_insn *insn)
1813{
1814 if (mips_relax.sequence != 2)
1815 {
1816 unsigned int i;
1817
1818 for (i = ARRAY_SIZE (history); i-- > first;)
1819 if (i >= first + n)
1820 history[i] = history[i - n];
1821 else
1822 history[i] = *insn;
1823 }
1824}
1825
71400594
RS
1826/* Initialize vr4120_conflicts. There is a bit of duplication here:
1827 the idea is to make it obvious at a glance that each errata is
1828 included. */
1829
1830static void
1831init_vr4120_conflicts (void)
1832{
1833#define CONFLICT(FIRST, SECOND) \
1834 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1835
1836 /* Errata 21 - [D]DIV[U] after [D]MACC */
1837 CONFLICT (MACC, DIV);
1838 CONFLICT (DMACC, DIV);
1839
1840 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1841 CONFLICT (DMULT, DMULT);
1842 CONFLICT (DMULT, DMACC);
1843 CONFLICT (DMACC, DMULT);
1844 CONFLICT (DMACC, DMACC);
1845
1846 /* Errata 24 - MT{LO,HI} after [D]MACC */
1847 CONFLICT (MACC, MTHILO);
1848 CONFLICT (DMACC, MTHILO);
1849
1850 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1851 instruction is executed immediately after a MACC or DMACC
1852 instruction, the result of [either instruction] is incorrect." */
1853 CONFLICT (MACC, MULT);
1854 CONFLICT (MACC, DMULT);
1855 CONFLICT (DMACC, MULT);
1856 CONFLICT (DMACC, DMULT);
1857
1858 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1859 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1860 DDIV or DDIVU instruction, the result of the MACC or
1861 DMACC instruction is incorrect.". */
1862 CONFLICT (DMULT, MACC);
1863 CONFLICT (DMULT, DMACC);
1864 CONFLICT (DIV, MACC);
1865 CONFLICT (DIV, DMACC);
1866
1867#undef CONFLICT
1868}
1869
707bfff6
TS
1870struct regname {
1871 const char *name;
1872 unsigned int num;
1873};
1874
1875#define RTYPE_MASK 0x1ff00
1876#define RTYPE_NUM 0x00100
1877#define RTYPE_FPU 0x00200
1878#define RTYPE_FCC 0x00400
1879#define RTYPE_VEC 0x00800
1880#define RTYPE_GP 0x01000
1881#define RTYPE_CP0 0x02000
1882#define RTYPE_PC 0x04000
1883#define RTYPE_ACC 0x08000
1884#define RTYPE_CCC 0x10000
1885#define RNUM_MASK 0x000ff
1886#define RWARN 0x80000
1887
1888#define GENERIC_REGISTER_NUMBERS \
1889 {"$0", RTYPE_NUM | 0}, \
1890 {"$1", RTYPE_NUM | 1}, \
1891 {"$2", RTYPE_NUM | 2}, \
1892 {"$3", RTYPE_NUM | 3}, \
1893 {"$4", RTYPE_NUM | 4}, \
1894 {"$5", RTYPE_NUM | 5}, \
1895 {"$6", RTYPE_NUM | 6}, \
1896 {"$7", RTYPE_NUM | 7}, \
1897 {"$8", RTYPE_NUM | 8}, \
1898 {"$9", RTYPE_NUM | 9}, \
1899 {"$10", RTYPE_NUM | 10}, \
1900 {"$11", RTYPE_NUM | 11}, \
1901 {"$12", RTYPE_NUM | 12}, \
1902 {"$13", RTYPE_NUM | 13}, \
1903 {"$14", RTYPE_NUM | 14}, \
1904 {"$15", RTYPE_NUM | 15}, \
1905 {"$16", RTYPE_NUM | 16}, \
1906 {"$17", RTYPE_NUM | 17}, \
1907 {"$18", RTYPE_NUM | 18}, \
1908 {"$19", RTYPE_NUM | 19}, \
1909 {"$20", RTYPE_NUM | 20}, \
1910 {"$21", RTYPE_NUM | 21}, \
1911 {"$22", RTYPE_NUM | 22}, \
1912 {"$23", RTYPE_NUM | 23}, \
1913 {"$24", RTYPE_NUM | 24}, \
1914 {"$25", RTYPE_NUM | 25}, \
1915 {"$26", RTYPE_NUM | 26}, \
1916 {"$27", RTYPE_NUM | 27}, \
1917 {"$28", RTYPE_NUM | 28}, \
1918 {"$29", RTYPE_NUM | 29}, \
1919 {"$30", RTYPE_NUM | 30}, \
1920 {"$31", RTYPE_NUM | 31}
1921
1922#define FPU_REGISTER_NAMES \
1923 {"$f0", RTYPE_FPU | 0}, \
1924 {"$f1", RTYPE_FPU | 1}, \
1925 {"$f2", RTYPE_FPU | 2}, \
1926 {"$f3", RTYPE_FPU | 3}, \
1927 {"$f4", RTYPE_FPU | 4}, \
1928 {"$f5", RTYPE_FPU | 5}, \
1929 {"$f6", RTYPE_FPU | 6}, \
1930 {"$f7", RTYPE_FPU | 7}, \
1931 {"$f8", RTYPE_FPU | 8}, \
1932 {"$f9", RTYPE_FPU | 9}, \
1933 {"$f10", RTYPE_FPU | 10}, \
1934 {"$f11", RTYPE_FPU | 11}, \
1935 {"$f12", RTYPE_FPU | 12}, \
1936 {"$f13", RTYPE_FPU | 13}, \
1937 {"$f14", RTYPE_FPU | 14}, \
1938 {"$f15", RTYPE_FPU | 15}, \
1939 {"$f16", RTYPE_FPU | 16}, \
1940 {"$f17", RTYPE_FPU | 17}, \
1941 {"$f18", RTYPE_FPU | 18}, \
1942 {"$f19", RTYPE_FPU | 19}, \
1943 {"$f20", RTYPE_FPU | 20}, \
1944 {"$f21", RTYPE_FPU | 21}, \
1945 {"$f22", RTYPE_FPU | 22}, \
1946 {"$f23", RTYPE_FPU | 23}, \
1947 {"$f24", RTYPE_FPU | 24}, \
1948 {"$f25", RTYPE_FPU | 25}, \
1949 {"$f26", RTYPE_FPU | 26}, \
1950 {"$f27", RTYPE_FPU | 27}, \
1951 {"$f28", RTYPE_FPU | 28}, \
1952 {"$f29", RTYPE_FPU | 29}, \
1953 {"$f30", RTYPE_FPU | 30}, \
1954 {"$f31", RTYPE_FPU | 31}
1955
1956#define FPU_CONDITION_CODE_NAMES \
1957 {"$fcc0", RTYPE_FCC | 0}, \
1958 {"$fcc1", RTYPE_FCC | 1}, \
1959 {"$fcc2", RTYPE_FCC | 2}, \
1960 {"$fcc3", RTYPE_FCC | 3}, \
1961 {"$fcc4", RTYPE_FCC | 4}, \
1962 {"$fcc5", RTYPE_FCC | 5}, \
1963 {"$fcc6", RTYPE_FCC | 6}, \
1964 {"$fcc7", RTYPE_FCC | 7}
1965
1966#define COPROC_CONDITION_CODE_NAMES \
1967 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
1968 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
1969 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
1970 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
1971 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
1972 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
1973 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
1974 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
1975
1976#define N32N64_SYMBOLIC_REGISTER_NAMES \
1977 {"$a4", RTYPE_GP | 8}, \
1978 {"$a5", RTYPE_GP | 9}, \
1979 {"$a6", RTYPE_GP | 10}, \
1980 {"$a7", RTYPE_GP | 11}, \
1981 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
1982 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
1983 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
1984 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
1985 {"$t0", RTYPE_GP | 12}, \
1986 {"$t1", RTYPE_GP | 13}, \
1987 {"$t2", RTYPE_GP | 14}, \
1988 {"$t3", RTYPE_GP | 15}
1989
1990#define O32_SYMBOLIC_REGISTER_NAMES \
1991 {"$t0", RTYPE_GP | 8}, \
1992 {"$t1", RTYPE_GP | 9}, \
1993 {"$t2", RTYPE_GP | 10}, \
1994 {"$t3", RTYPE_GP | 11}, \
1995 {"$t4", RTYPE_GP | 12}, \
1996 {"$t5", RTYPE_GP | 13}, \
1997 {"$t6", RTYPE_GP | 14}, \
1998 {"$t7", RTYPE_GP | 15}, \
1999 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2000 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2001 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2002 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2003
2004/* Remaining symbolic register names */
2005#define SYMBOLIC_REGISTER_NAMES \
2006 {"$zero", RTYPE_GP | 0}, \
2007 {"$at", RTYPE_GP | 1}, \
2008 {"$AT", RTYPE_GP | 1}, \
2009 {"$v0", RTYPE_GP | 2}, \
2010 {"$v1", RTYPE_GP | 3}, \
2011 {"$a0", RTYPE_GP | 4}, \
2012 {"$a1", RTYPE_GP | 5}, \
2013 {"$a2", RTYPE_GP | 6}, \
2014 {"$a3", RTYPE_GP | 7}, \
2015 {"$s0", RTYPE_GP | 16}, \
2016 {"$s1", RTYPE_GP | 17}, \
2017 {"$s2", RTYPE_GP | 18}, \
2018 {"$s3", RTYPE_GP | 19}, \
2019 {"$s4", RTYPE_GP | 20}, \
2020 {"$s5", RTYPE_GP | 21}, \
2021 {"$s6", RTYPE_GP | 22}, \
2022 {"$s7", RTYPE_GP | 23}, \
2023 {"$t8", RTYPE_GP | 24}, \
2024 {"$t9", RTYPE_GP | 25}, \
2025 {"$k0", RTYPE_GP | 26}, \
2026 {"$kt0", RTYPE_GP | 26}, \
2027 {"$k1", RTYPE_GP | 27}, \
2028 {"$kt1", RTYPE_GP | 27}, \
2029 {"$gp", RTYPE_GP | 28}, \
2030 {"$sp", RTYPE_GP | 29}, \
2031 {"$s8", RTYPE_GP | 30}, \
2032 {"$fp", RTYPE_GP | 30}, \
2033 {"$ra", RTYPE_GP | 31}
2034
2035#define MIPS16_SPECIAL_REGISTER_NAMES \
2036 {"$pc", RTYPE_PC | 0}
2037
2038#define MDMX_VECTOR_REGISTER_NAMES \
2039 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2040 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2041 {"$v2", RTYPE_VEC | 2}, \
2042 {"$v3", RTYPE_VEC | 3}, \
2043 {"$v4", RTYPE_VEC | 4}, \
2044 {"$v5", RTYPE_VEC | 5}, \
2045 {"$v6", RTYPE_VEC | 6}, \
2046 {"$v7", RTYPE_VEC | 7}, \
2047 {"$v8", RTYPE_VEC | 8}, \
2048 {"$v9", RTYPE_VEC | 9}, \
2049 {"$v10", RTYPE_VEC | 10}, \
2050 {"$v11", RTYPE_VEC | 11}, \
2051 {"$v12", RTYPE_VEC | 12}, \
2052 {"$v13", RTYPE_VEC | 13}, \
2053 {"$v14", RTYPE_VEC | 14}, \
2054 {"$v15", RTYPE_VEC | 15}, \
2055 {"$v16", RTYPE_VEC | 16}, \
2056 {"$v17", RTYPE_VEC | 17}, \
2057 {"$v18", RTYPE_VEC | 18}, \
2058 {"$v19", RTYPE_VEC | 19}, \
2059 {"$v20", RTYPE_VEC | 20}, \
2060 {"$v21", RTYPE_VEC | 21}, \
2061 {"$v22", RTYPE_VEC | 22}, \
2062 {"$v23", RTYPE_VEC | 23}, \
2063 {"$v24", RTYPE_VEC | 24}, \
2064 {"$v25", RTYPE_VEC | 25}, \
2065 {"$v26", RTYPE_VEC | 26}, \
2066 {"$v27", RTYPE_VEC | 27}, \
2067 {"$v28", RTYPE_VEC | 28}, \
2068 {"$v29", RTYPE_VEC | 29}, \
2069 {"$v30", RTYPE_VEC | 30}, \
2070 {"$v31", RTYPE_VEC | 31}
2071
2072#define MIPS_DSP_ACCUMULATOR_NAMES \
2073 {"$ac0", RTYPE_ACC | 0}, \
2074 {"$ac1", RTYPE_ACC | 1}, \
2075 {"$ac2", RTYPE_ACC | 2}, \
2076 {"$ac3", RTYPE_ACC | 3}
2077
2078static const struct regname reg_names[] = {
2079 GENERIC_REGISTER_NUMBERS,
2080 FPU_REGISTER_NAMES,
2081 FPU_CONDITION_CODE_NAMES,
2082 COPROC_CONDITION_CODE_NAMES,
2083
2084 /* The $txx registers depends on the abi,
2085 these will be added later into the symbol table from
2086 one of the tables below once mips_abi is set after
2087 parsing of arguments from the command line. */
2088 SYMBOLIC_REGISTER_NAMES,
2089
2090 MIPS16_SPECIAL_REGISTER_NAMES,
2091 MDMX_VECTOR_REGISTER_NAMES,
2092 MIPS_DSP_ACCUMULATOR_NAMES,
2093 {0, 0}
2094};
2095
2096static const struct regname reg_names_o32[] = {
2097 O32_SYMBOLIC_REGISTER_NAMES,
2098 {0, 0}
2099};
2100
2101static const struct regname reg_names_n32n64[] = {
2102 N32N64_SYMBOLIC_REGISTER_NAMES,
2103 {0, 0}
2104};
2105
df58fc94
RS
2106/* Check if S points at a valid register specifier according to TYPES.
2107 If so, then return 1, advance S to consume the specifier and store
2108 the register's number in REGNOP, otherwise return 0. */
2109
707bfff6
TS
2110static int
2111reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2112{
2113 symbolS *symbolP;
2114 char *e;
2115 char save_c;
2116 int reg = -1;
2117
2118 /* Find end of name. */
2119 e = *s;
2120 if (is_name_beginner (*e))
2121 ++e;
2122 while (is_part_of_name (*e))
2123 ++e;
2124
2125 /* Terminate name. */
2126 save_c = *e;
2127 *e = '\0';
2128
2129 /* Look for a register symbol. */
2130 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2131 {
2132 int r = S_GET_VALUE (symbolP);
2133 if (r & types)
2134 reg = r & RNUM_MASK;
2135 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2136 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
2137 reg = (r & RNUM_MASK) - 2;
2138 }
2139 /* Else see if this is a register defined in an itbl entry. */
2140 else if ((types & RTYPE_GP) && itbl_have_entries)
2141 {
2142 char *n = *s;
2143 unsigned long r;
2144
2145 if (*n == '$')
2146 ++n;
2147 if (itbl_get_reg_val (n, &r))
2148 reg = r & RNUM_MASK;
2149 }
2150
2151 /* Advance to next token if a register was recognised. */
2152 if (reg >= 0)
2153 *s = e;
2154 else if (types & RWARN)
20203fb9 2155 as_warn (_("Unrecognized register name `%s'"), *s);
707bfff6
TS
2156
2157 *e = save_c;
2158 if (regnop)
2159 *regnop = reg;
2160 return reg >= 0;
2161}
2162
df58fc94
RS
2163/* Check if S points at a valid register list according to TYPES.
2164 If so, then return 1, advance S to consume the list and store
2165 the registers present on the list as a bitmask of ones in REGLISTP,
2166 otherwise return 0. A valid list comprises a comma-separated
2167 enumeration of valid single registers and/or dash-separated
2168 contiguous register ranges as determined by their numbers.
2169
2170 As a special exception if one of s0-s7 registers is specified as
2171 the range's lower delimiter and s8 (fp) is its upper one, then no
2172 registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2309ddf2 2173 are selected; they have to be listed separately if needed. */
df58fc94
RS
2174
2175static int
2176reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2177{
2178 unsigned int reglist = 0;
2179 unsigned int lastregno;
2180 bfd_boolean ok = TRUE;
2181 unsigned int regmask;
2309ddf2 2182 char *s_endlist = *s;
df58fc94 2183 char *s_reset = *s;
2309ddf2 2184 unsigned int regno;
df58fc94
RS
2185
2186 while (reg_lookup (s, types, &regno))
2187 {
2188 lastregno = regno;
2189 if (**s == '-')
2190 {
2191 (*s)++;
2192 ok = reg_lookup (s, types, &lastregno);
2193 if (ok && lastregno < regno)
2194 ok = FALSE;
2195 if (!ok)
2196 break;
2197 }
2198
2199 if (lastregno == FP && regno >= S0 && regno <= S7)
2200 {
2201 lastregno = S7;
2202 reglist |= 1 << FP;
2203 }
2204 regmask = 1 << lastregno;
2205 regmask = (regmask << 1) - 1;
2206 regmask ^= (1 << regno) - 1;
2207 reglist |= regmask;
2208
2309ddf2 2209 s_endlist = *s;
df58fc94
RS
2210 if (**s != ',')
2211 break;
2212 (*s)++;
2213 }
2214
2215 if (ok)
2309ddf2 2216 *s = s_endlist;
df58fc94
RS
2217 else
2218 *s = s_reset;
2219 if (reglistp)
2220 *reglistp = reglist;
2221 return ok && reglist != 0;
2222}
2223
d301a56b
RS
2224/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
2225 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
2226
2227static bfd_boolean
f79e2745 2228is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
2229{
2230 int isa = mips_opts.isa;
846ef2d0 2231 int ase = mips_opts.ase;
037b32b9
AN
2232 int fp_s, fp_d;
2233
846ef2d0 2234 if ((ase & ASE_DSP) && ISA_SUPPORTS_DSP64_ASE)
d301a56b 2235 ase |= ASE_DSP64;
846ef2d0 2236 if ((ase & ASE_VIRT) && ISA_SUPPORTS_VIRT64_ASE)
d301a56b 2237 ase |= ASE_VIRT64;
037b32b9 2238
d301a56b 2239 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
2240 return FALSE;
2241
2242 /* Check whether the instruction or macro requires single-precision or
2243 double-precision floating-point support. Note that this information is
2244 stored differently in the opcode table for insns and macros. */
2245 if (mo->pinfo == INSN_MACRO)
2246 {
2247 fp_s = mo->pinfo2 & INSN2_M_FP_S;
2248 fp_d = mo->pinfo2 & INSN2_M_FP_D;
2249 }
2250 else
2251 {
2252 fp_s = mo->pinfo & FP_S;
2253 fp_d = mo->pinfo & FP_D;
2254 }
2255
2256 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2257 return FALSE;
2258
2259 if (fp_s && mips_opts.soft_float)
2260 return FALSE;
2261
2262 return TRUE;
2263}
2264
2265/* Return TRUE if the MIPS16 opcode MO is valid on the currently
2266 selected ISA and architecture. */
2267
2268static bfd_boolean
2269is_opcode_valid_16 (const struct mips_opcode *mo)
2270{
d301a56b 2271 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
037b32b9
AN
2272}
2273
df58fc94
RS
2274/* Return TRUE if the size of the microMIPS opcode MO matches one
2275 explicitly requested. Always TRUE in the standard MIPS mode. */
2276
2277static bfd_boolean
2278is_size_valid (const struct mips_opcode *mo)
2279{
2280 if (!mips_opts.micromips)
2281 return TRUE;
2282
2283 if (!forced_insn_length)
2284 return TRUE;
2285 if (mo->pinfo == INSN_MACRO)
2286 return FALSE;
2287 return forced_insn_length == micromips_insn_length (mo);
2288}
2289
2290/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
2291 of the preceding instruction. Always TRUE in the standard MIPS mode.
2292
2293 We don't accept macros in 16-bit delay slots to avoid a case where
2294 a macro expansion fails because it relies on a preceding 32-bit real
2295 instruction to have matched and does not handle the operands correctly.
2296 The only macros that may expand to 16-bit instructions are JAL that
2297 cannot be placed in a delay slot anyway, and corner cases of BALIGN
2298 and BGT (that likewise cannot be placed in a delay slot) that decay to
2299 a NOP. In all these cases the macros precede any corresponding real
2300 instruction definitions in the opcode table, so they will match in the
2301 second pass where the size of the delay slot is ignored and therefore
2302 produce correct code. */
df58fc94
RS
2303
2304static bfd_boolean
2305is_delay_slot_valid (const struct mips_opcode *mo)
2306{
2307 if (!mips_opts.micromips)
2308 return TRUE;
2309
2310 if (mo->pinfo == INSN_MACRO)
c06dec14 2311 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
2312 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2313 && micromips_insn_length (mo) != 4)
2314 return FALSE;
2315 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2316 && micromips_insn_length (mo) != 2)
2317 return FALSE;
2318
2319 return TRUE;
2320}
2321
707bfff6
TS
2322/* This function is called once, at assembler startup time. It should set up
2323 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 2324
252b5132 2325void
17a2f251 2326md_begin (void)
252b5132 2327{
3994f87e 2328 const char *retval = NULL;
156c2f8b 2329 int i = 0;
252b5132 2330 int broken = 0;
1f25f5d3 2331
0a44bf69
RS
2332 if (mips_pic != NO_PIC)
2333 {
2334 if (g_switch_seen && g_switch_value != 0)
2335 as_bad (_("-G may not be used in position-independent code"));
2336 g_switch_value = 0;
2337 }
2338
fef14a42 2339 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
2340 as_warn (_("Could not set architecture and machine"));
2341
252b5132
RH
2342 op_hash = hash_new ();
2343
2344 for (i = 0; i < NUMOPCODES;)
2345 {
2346 const char *name = mips_opcodes[i].name;
2347
17a2f251 2348 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
2349 if (retval != NULL)
2350 {
2351 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2352 mips_opcodes[i].name, retval);
2353 /* Probably a memory allocation problem? Give up now. */
2354 as_fatal (_("Broken assembler. No assembly attempted."));
2355 }
2356 do
2357 {
2358 if (mips_opcodes[i].pinfo != INSN_MACRO)
2359 {
2360 if (!validate_mips_insn (&mips_opcodes[i]))
2361 broken = 1;
1e915849
RS
2362 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2363 {
2364 create_insn (&nop_insn, mips_opcodes + i);
c67a084a
NC
2365 if (mips_fix_loongson2f_nop)
2366 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
1e915849
RS
2367 nop_insn.fixed_p = 1;
2368 }
252b5132
RH
2369 }
2370 ++i;
2371 }
2372 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2373 }
2374
2375 mips16_op_hash = hash_new ();
2376
2377 i = 0;
2378 while (i < bfd_mips16_num_opcodes)
2379 {
2380 const char *name = mips16_opcodes[i].name;
2381
17a2f251 2382 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
2383 if (retval != NULL)
2384 as_fatal (_("internal: can't hash `%s': %s"),
2385 mips16_opcodes[i].name, retval);
2386 do
2387 {
2388 if (mips16_opcodes[i].pinfo != INSN_MACRO
2389 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2390 != mips16_opcodes[i].match))
2391 {
2392 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2393 mips16_opcodes[i].name, mips16_opcodes[i].args);
2394 broken = 1;
2395 }
1e915849
RS
2396 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2397 {
2398 create_insn (&mips16_nop_insn, mips16_opcodes + i);
2399 mips16_nop_insn.fixed_p = 1;
2400 }
252b5132
RH
2401 ++i;
2402 }
2403 while (i < bfd_mips16_num_opcodes
2404 && strcmp (mips16_opcodes[i].name, name) == 0);
2405 }
2406
df58fc94
RS
2407 micromips_op_hash = hash_new ();
2408
2409 i = 0;
2410 while (i < bfd_micromips_num_opcodes)
2411 {
2412 const char *name = micromips_opcodes[i].name;
2413
2414 retval = hash_insert (micromips_op_hash, name,
2415 (void *) &micromips_opcodes[i]);
2416 if (retval != NULL)
2417 as_fatal (_("internal: can't hash `%s': %s"),
2418 micromips_opcodes[i].name, retval);
2419 do
2420 if (micromips_opcodes[i].pinfo != INSN_MACRO)
2421 {
2422 struct mips_cl_insn *micromips_nop_insn;
2423
2424 if (!validate_micromips_insn (&micromips_opcodes[i]))
2425 broken = 1;
2426
2427 if (micromips_insn_length (micromips_opcodes + i) == 2)
2428 micromips_nop_insn = &micromips_nop16_insn;
2429 else if (micromips_insn_length (micromips_opcodes + i) == 4)
2430 micromips_nop_insn = &micromips_nop32_insn;
2431 else
2432 continue;
2433
2434 if (micromips_nop_insn->insn_mo == NULL
2435 && strcmp (name, "nop") == 0)
2436 {
2437 create_insn (micromips_nop_insn, micromips_opcodes + i);
2438 micromips_nop_insn->fixed_p = 1;
2439 }
2440 }
2441 while (++i < bfd_micromips_num_opcodes
2442 && strcmp (micromips_opcodes[i].name, name) == 0);
2443 }
2444
252b5132
RH
2445 if (broken)
2446 as_fatal (_("Broken assembler. No assembly attempted."));
2447
2448 /* We add all the general register names to the symbol table. This
2449 helps us detect invalid uses of them. */
707bfff6
TS
2450 for (i = 0; reg_names[i].name; i++)
2451 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 2452 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
2453 &zero_address_frag));
2454 if (HAVE_NEWABI)
2455 for (i = 0; reg_names_n32n64[i].name; i++)
2456 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 2457 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 2458 &zero_address_frag));
707bfff6
TS
2459 else
2460 for (i = 0; reg_names_o32[i].name; i++)
2461 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 2462 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 2463 &zero_address_frag));
6047c971 2464
7d10b47d 2465 mips_no_prev_insn ();
252b5132
RH
2466
2467 mips_gprmask = 0;
2468 mips_cprmask[0] = 0;
2469 mips_cprmask[1] = 0;
2470 mips_cprmask[2] = 0;
2471 mips_cprmask[3] = 0;
2472
2473 /* set the default alignment for the text section (2**2) */
2474 record_alignment (text_section, 2);
2475
4d0d148d 2476 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 2477
707bfff6 2478#ifdef OBJ_ELF
f43abd2b 2479 if (IS_ELF)
252b5132 2480 {
0a44bf69
RS
2481 /* On a native system other than VxWorks, sections must be aligned
2482 to 16 byte boundaries. When configured for an embedded ELF
2483 target, we don't bother. */
c41e87e3
CF
2484 if (strncmp (TARGET_OS, "elf", 3) != 0
2485 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132
RH
2486 {
2487 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2488 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2489 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2490 }
2491
2492 /* Create a .reginfo section for register masks and a .mdebug
2493 section for debugging information. */
2494 {
2495 segT seg;
2496 subsegT subseg;
2497 flagword flags;
2498 segT sec;
2499
2500 seg = now_seg;
2501 subseg = now_subseg;
2502
2503 /* The ABI says this section should be loaded so that the
2504 running program can access it. However, we don't load it
2505 if we are configured for an embedded target */
2506 flags = SEC_READONLY | SEC_DATA;
c41e87e3 2507 if (strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
2508 flags |= SEC_ALLOC | SEC_LOAD;
2509
316f5878 2510 if (mips_abi != N64_ABI)
252b5132
RH
2511 {
2512 sec = subseg_new (".reginfo", (subsegT) 0);
2513
195325d2
TS
2514 bfd_set_section_flags (stdoutput, sec, flags);
2515 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 2516
252b5132 2517 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
252b5132
RH
2518 }
2519 else
2520 {
2521 /* The 64-bit ABI uses a .MIPS.options section rather than
2522 .reginfo section. */
2523 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
2524 bfd_set_section_flags (stdoutput, sec, flags);
2525 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 2526
252b5132
RH
2527 /* Set up the option header. */
2528 {
2529 Elf_Internal_Options opthdr;
2530 char *f;
2531
2532 opthdr.kind = ODK_REGINFO;
2533 opthdr.size = (sizeof (Elf_External_Options)
2534 + sizeof (Elf64_External_RegInfo));
2535 opthdr.section = 0;
2536 opthdr.info = 0;
2537 f = frag_more (sizeof (Elf_External_Options));
2538 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2539 (Elf_External_Options *) f);
2540
2541 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2542 }
252b5132
RH
2543 }
2544
2545 if (ECOFF_DEBUGGING)
2546 {
2547 sec = subseg_new (".mdebug", (subsegT) 0);
2548 (void) bfd_set_section_flags (stdoutput, sec,
2549 SEC_HAS_CONTENTS | SEC_READONLY);
2550 (void) bfd_set_section_alignment (stdoutput, sec, 2);
2551 }
f43abd2b 2552 else if (mips_flag_pdr)
ecb4347a
DJ
2553 {
2554 pdr_seg = subseg_new (".pdr", (subsegT) 0);
2555 (void) bfd_set_section_flags (stdoutput, pdr_seg,
2556 SEC_READONLY | SEC_RELOC
2557 | SEC_DEBUGGING);
2558 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2559 }
252b5132
RH
2560
2561 subseg_set (seg, subseg);
2562 }
2563 }
707bfff6 2564#endif /* OBJ_ELF */
252b5132
RH
2565
2566 if (! ECOFF_DEBUGGING)
2567 md_obj_begin ();
71400594
RS
2568
2569 if (mips_fix_vr4120)
2570 init_vr4120_conflicts ();
252b5132
RH
2571}
2572
2573void
17a2f251 2574md_mips_end (void)
252b5132 2575{
02b1ab82 2576 mips_emit_delays ();
252b5132
RH
2577 if (! ECOFF_DEBUGGING)
2578 md_obj_end ();
2579}
2580
2581void
17a2f251 2582md_assemble (char *str)
252b5132
RH
2583{
2584 struct mips_cl_insn insn;
f6688943
TS
2585 bfd_reloc_code_real_type unused_reloc[3]
2586 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
2587
2588 imm_expr.X_op = O_absent;
5f74bc13 2589 imm2_expr.X_op = O_absent;
252b5132 2590 offset_expr.X_op = O_absent;
f6688943
TS
2591 imm_reloc[0] = BFD_RELOC_UNUSED;
2592 imm_reloc[1] = BFD_RELOC_UNUSED;
2593 imm_reloc[2] = BFD_RELOC_UNUSED;
2594 offset_reloc[0] = BFD_RELOC_UNUSED;
2595 offset_reloc[1] = BFD_RELOC_UNUSED;
2596 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 2597
e1b47bd5
RS
2598 mips_mark_labels ();
2599 mips_assembling_insn = TRUE;
2600
252b5132
RH
2601 if (mips_opts.mips16)
2602 mips16_ip (str, &insn);
2603 else
2604 {
2605 mips_ip (str, &insn);
beae10d5
KH
2606 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2607 str, insn.insn_opcode));
252b5132
RH
2608 }
2609
2610 if (insn_error)
e1b47bd5
RS
2611 as_bad ("%s `%s'", insn_error, str);
2612 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 2613 {
584892a6 2614 macro_start ();
252b5132
RH
2615 if (mips_opts.mips16)
2616 mips16_macro (&insn);
2617 else
2618 macro (&insn);
584892a6 2619 macro_end ();
252b5132
RH
2620 }
2621 else
2622 {
2623 if (imm_expr.X_op != O_absent)
df58fc94 2624 append_insn (&insn, &imm_expr, imm_reloc, FALSE);
252b5132 2625 else if (offset_expr.X_op != O_absent)
df58fc94 2626 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 2627 else
df58fc94 2628 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 2629 }
e1b47bd5
RS
2630
2631 mips_assembling_insn = FALSE;
252b5132
RH
2632}
2633
738e5348
RS
2634/* Convenience functions for abstracting away the differences between
2635 MIPS16 and non-MIPS16 relocations. */
2636
2637static inline bfd_boolean
2638mips16_reloc_p (bfd_reloc_code_real_type reloc)
2639{
2640 switch (reloc)
2641 {
2642 case BFD_RELOC_MIPS16_JMP:
2643 case BFD_RELOC_MIPS16_GPREL:
2644 case BFD_RELOC_MIPS16_GOT16:
2645 case BFD_RELOC_MIPS16_CALL16:
2646 case BFD_RELOC_MIPS16_HI16_S:
2647 case BFD_RELOC_MIPS16_HI16:
2648 case BFD_RELOC_MIPS16_LO16:
2649 return TRUE;
2650
2651 default:
2652 return FALSE;
2653 }
2654}
2655
df58fc94
RS
2656static inline bfd_boolean
2657micromips_reloc_p (bfd_reloc_code_real_type reloc)
2658{
2659 switch (reloc)
2660 {
2661 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2662 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2663 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2664 case BFD_RELOC_MICROMIPS_GPREL16:
2665 case BFD_RELOC_MICROMIPS_JMP:
2666 case BFD_RELOC_MICROMIPS_HI16:
2667 case BFD_RELOC_MICROMIPS_HI16_S:
2668 case BFD_RELOC_MICROMIPS_LO16:
2669 case BFD_RELOC_MICROMIPS_LITERAL:
2670 case BFD_RELOC_MICROMIPS_GOT16:
2671 case BFD_RELOC_MICROMIPS_CALL16:
2672 case BFD_RELOC_MICROMIPS_GOT_HI16:
2673 case BFD_RELOC_MICROMIPS_GOT_LO16:
2674 case BFD_RELOC_MICROMIPS_CALL_HI16:
2675 case BFD_RELOC_MICROMIPS_CALL_LO16:
2676 case BFD_RELOC_MICROMIPS_SUB:
2677 case BFD_RELOC_MICROMIPS_GOT_PAGE:
2678 case BFD_RELOC_MICROMIPS_GOT_OFST:
2679 case BFD_RELOC_MICROMIPS_GOT_DISP:
2680 case BFD_RELOC_MICROMIPS_HIGHEST:
2681 case BFD_RELOC_MICROMIPS_HIGHER:
2682 case BFD_RELOC_MICROMIPS_SCN_DISP:
2683 case BFD_RELOC_MICROMIPS_JALR:
2684 return TRUE;
2685
2686 default:
2687 return FALSE;
2688 }
2689}
2690
2309ddf2
MR
2691static inline bfd_boolean
2692jmp_reloc_p (bfd_reloc_code_real_type reloc)
2693{
2694 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
2695}
2696
738e5348
RS
2697static inline bfd_boolean
2698got16_reloc_p (bfd_reloc_code_real_type reloc)
2699{
2309ddf2 2700 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 2701 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
2702}
2703
2704static inline bfd_boolean
2705hi16_reloc_p (bfd_reloc_code_real_type reloc)
2706{
2309ddf2 2707 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 2708 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
2709}
2710
2711static inline bfd_boolean
2712lo16_reloc_p (bfd_reloc_code_real_type reloc)
2713{
2309ddf2 2714 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
2715 || reloc == BFD_RELOC_MICROMIPS_LO16);
2716}
2717
df58fc94
RS
2718static inline bfd_boolean
2719jalr_reloc_p (bfd_reloc_code_real_type reloc)
2720{
2309ddf2 2721 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
2722}
2723
2de39019
CM
2724/* Return true if RELOC is a PC-relative relocation that does not have
2725 full address range. */
2726
2727static inline bfd_boolean
2728limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
2729{
2730 switch (reloc)
2731 {
2732 case BFD_RELOC_16_PCREL_S2:
2733 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2734 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2735 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2736 return TRUE;
2737
b47468a6
CM
2738 case BFD_RELOC_32_PCREL:
2739 return HAVE_64BIT_ADDRESSES;
2740
2de39019
CM
2741 default:
2742 return FALSE;
2743 }
2744}
b47468a6 2745
5919d012 2746/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
2747 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
2748 need a matching %lo() when applied to local symbols. */
5919d012
RS
2749
2750static inline bfd_boolean
17a2f251 2751reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 2752{
3b91255e 2753 return (HAVE_IN_PLACE_ADDENDS
738e5348 2754 && (hi16_reloc_p (reloc)
0a44bf69
RS
2755 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2756 all GOT16 relocations evaluate to "G". */
738e5348
RS
2757 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
2758}
2759
2760/* Return the type of %lo() reloc needed by RELOC, given that
2761 reloc_needs_lo_p. */
2762
2763static inline bfd_reloc_code_real_type
2764matching_lo_reloc (bfd_reloc_code_real_type reloc)
2765{
df58fc94
RS
2766 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
2767 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
2768 : BFD_RELOC_LO16));
5919d012
RS
2769}
2770
2771/* Return true if the given fixup is followed by a matching R_MIPS_LO16
2772 relocation. */
2773
2774static inline bfd_boolean
17a2f251 2775fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
2776{
2777 return (fixp->fx_next != NULL
738e5348 2778 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
2779 && fixp->fx_addsy == fixp->fx_next->fx_addsy
2780 && fixp->fx_offset == fixp->fx_next->fx_offset);
2781}
2782
252b5132
RH
2783/* This function returns true if modifying a register requires a
2784 delay. */
2785
2786static int
17a2f251 2787reg_needs_delay (unsigned int reg)
252b5132
RH
2788{
2789 unsigned long prev_pinfo;
2790
47e39b9d 2791 prev_pinfo = history[0].insn_mo->pinfo;
252b5132 2792 if (! mips_opts.noreorder
81912461
ILT
2793 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2794 && ! gpr_interlocks)
2795 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2796 && ! cop_interlocks)))
252b5132 2797 {
81912461
ILT
2798 /* A load from a coprocessor or from memory. All load delays
2799 delay the use of general register rt for one instruction. */
bdaaa2e1 2800 /* Itbl support may require additional care here. */
252b5132 2801 know (prev_pinfo & INSN_WRITE_GPR_T);
df58fc94 2802 if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
252b5132
RH
2803 return 1;
2804 }
2805
2806 return 0;
2807}
2808
462427c4
RS
2809/* Move all labels in LABELS to the current insertion point. TEXT_P
2810 says whether the labels refer to text or data. */
404a8071
RS
2811
2812static void
462427c4 2813mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
2814{
2815 struct insn_label_list *l;
2816 valueT val;
2817
462427c4 2818 for (l = labels; l != NULL; l = l->next)
404a8071 2819 {
9c2799c2 2820 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
2821 symbol_set_frag (l->label, frag_now);
2822 val = (valueT) frag_now_fix ();
df58fc94 2823 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 2824 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
2825 ++val;
2826 S_SET_VALUE (l->label, val);
2827 }
2828}
2829
462427c4
RS
2830/* Move all labels in insn_labels to the current insertion point
2831 and treat them as text labels. */
2832
2833static void
2834mips_move_text_labels (void)
2835{
2836 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
2837}
2838
5f0fe04b
TS
2839static bfd_boolean
2840s_is_linkonce (symbolS *sym, segT from_seg)
2841{
2842 bfd_boolean linkonce = FALSE;
2843 segT symseg = S_GET_SEGMENT (sym);
2844
2845 if (symseg != from_seg && !S_IS_LOCAL (sym))
2846 {
2847 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2848 linkonce = TRUE;
2849#ifdef OBJ_ELF
2850 /* The GNU toolchain uses an extension for ELF: a section
2851 beginning with the magic string .gnu.linkonce is a
2852 linkonce section. */
2853 if (strncmp (segment_name (symseg), ".gnu.linkonce",
2854 sizeof ".gnu.linkonce" - 1) == 0)
2855 linkonce = TRUE;
2856#endif
2857 }
2858 return linkonce;
2859}
2860
e1b47bd5 2861/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
2862 linker to handle them specially, such as generating jalx instructions
2863 when needed. We also make them odd for the duration of the assembly,
2864 in order to generate the right sort of code. We will make them even
252b5132
RH
2865 in the adjust_symtab routine, while leaving them marked. This is
2866 convenient for the debugger and the disassembler. The linker knows
2867 to make them odd again. */
2868
2869static void
e1b47bd5 2870mips_compressed_mark_label (symbolS *label)
252b5132 2871{
df58fc94 2872 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 2873
a8dbcb85 2874#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
e1b47bd5
RS
2875 if (IS_ELF)
2876 {
2877 if (mips_opts.mips16)
2878 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
2879 else
2880 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
252b5132 2881 }
e1b47bd5
RS
2882#endif
2883 if ((S_GET_VALUE (label) & 1) == 0
2884 /* Don't adjust the address if the label is global or weak, or
2885 in a link-once section, since we'll be emitting symbol reloc
2886 references to it which will be patched up by the linker, and
2887 the final value of the symbol may or may not be MIPS16/microMIPS. */
2888 && !S_IS_WEAK (label)
2889 && !S_IS_EXTERNAL (label)
2890 && !s_is_linkonce (label, now_seg))
2891 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
2892}
2893
2894/* Mark preceding MIPS16 or microMIPS instruction labels. */
2895
2896static void
2897mips_compressed_mark_labels (void)
2898{
2899 struct insn_label_list *l;
2900
2901 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
2902 mips_compressed_mark_label (l->label);
252b5132
RH
2903}
2904
4d7206a2
RS
2905/* End the current frag. Make it a variant frag and record the
2906 relaxation info. */
2907
2908static void
2909relax_close_frag (void)
2910{
584892a6 2911 mips_macro_warning.first_frag = frag_now;
4d7206a2 2912 frag_var (rs_machine_dependent, 0, 0,
584892a6 2913 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
2914 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2915
2916 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2917 mips_relax.first_fixup = 0;
2918}
2919
2920/* Start a new relaxation sequence whose expansion depends on SYMBOL.
2921 See the comment above RELAX_ENCODE for more details. */
2922
2923static void
2924relax_start (symbolS *symbol)
2925{
9c2799c2 2926 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
2927 mips_relax.sequence = 1;
2928 mips_relax.symbol = symbol;
2929}
2930
2931/* Start generating the second version of a relaxable sequence.
2932 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
2933
2934static void
4d7206a2
RS
2935relax_switch (void)
2936{
9c2799c2 2937 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
2938 mips_relax.sequence = 2;
2939}
2940
2941/* End the current relaxable sequence. */
2942
2943static void
2944relax_end (void)
2945{
9c2799c2 2946 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
2947 relax_close_frag ();
2948 mips_relax.sequence = 0;
2949}
2950
11625dd8
RS
2951/* Return true if IP is a delayed branch or jump. */
2952
2953static inline bfd_boolean
2954delayed_branch_p (const struct mips_cl_insn *ip)
2955{
2956 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2957 | INSN_COND_BRANCH_DELAY
2958 | INSN_COND_BRANCH_LIKELY)) != 0;
2959}
2960
2961/* Return true if IP is a compact branch or jump. */
2962
2963static inline bfd_boolean
2964compact_branch_p (const struct mips_cl_insn *ip)
2965{
2966 if (mips_opts.mips16)
2967 return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
2968 | MIPS16_INSN_COND_BRANCH)) != 0;
2969 else
2970 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
2971 | INSN2_COND_BRANCH)) != 0;
2972}
2973
2974/* Return true if IP is an unconditional branch or jump. */
2975
2976static inline bfd_boolean
2977uncond_branch_p (const struct mips_cl_insn *ip)
2978{
2979 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
2980 || (mips_opts.mips16
2981 ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
2982 : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
2983}
2984
2985/* Return true if IP is a branch-likely instruction. */
2986
2987static inline bfd_boolean
2988branch_likely_p (const struct mips_cl_insn *ip)
2989{
2990 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
2991}
2992
14fe068b
RS
2993/* Return the type of nop that should be used to fill the delay slot
2994 of delayed branch IP. */
2995
2996static struct mips_cl_insn *
2997get_delay_slot_nop (const struct mips_cl_insn *ip)
2998{
2999 if (mips_opts.micromips
3000 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3001 return &micromips_nop32_insn;
3002 return NOP_INSN;
3003}
3004
2309ddf2 3005/* Return the mask of core registers that IP reads or writes. */
df58fc94
RS
3006
3007static unsigned int
3008gpr_mod_mask (const struct mips_cl_insn *ip)
3009{
2309ddf2 3010 unsigned long pinfo2;
df58fc94
RS
3011 unsigned int mask;
3012
3013 mask = 0;
df58fc94
RS
3014 pinfo2 = ip->insn_mo->pinfo2;
3015 if (mips_opts.micromips)
3016 {
df58fc94
RS
3017 if (pinfo2 & INSN2_MOD_GPR_MD)
3018 mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
df58fc94
RS
3019 if (pinfo2 & INSN2_MOD_GPR_MF)
3020 mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
df58fc94
RS
3021 if (pinfo2 & INSN2_MOD_SP)
3022 mask |= 1 << SP;
3023 }
3024 return mask;
3025}
3026
4c260379
RS
3027/* Return the mask of core registers that IP reads. */
3028
3029static unsigned int
3030gpr_read_mask (const struct mips_cl_insn *ip)
3031{
3032 unsigned long pinfo, pinfo2;
3033 unsigned int mask;
3034
df58fc94 3035 mask = gpr_mod_mask (ip);
4c260379
RS
3036 pinfo = ip->insn_mo->pinfo;
3037 pinfo2 = ip->insn_mo->pinfo2;
3038 if (mips_opts.mips16)
3039 {
3040 if (pinfo & MIPS16_INSN_READ_X)
3041 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3042 if (pinfo & MIPS16_INSN_READ_Y)
3043 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3044 if (pinfo & MIPS16_INSN_READ_T)
3045 mask |= 1 << TREG;
3046 if (pinfo & MIPS16_INSN_READ_SP)
3047 mask |= 1 << SP;
3048 if (pinfo & MIPS16_INSN_READ_31)
3049 mask |= 1 << RA;
3050 if (pinfo & MIPS16_INSN_READ_Z)
3051 mask |= 1 << (mips16_to_32_reg_map
3052 [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3053 if (pinfo & MIPS16_INSN_READ_GPR_X)
3054 mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3055 }
3056 else
3057 {
3058 if (pinfo2 & INSN2_READ_GPR_D)
2309ddf2 3059 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3060 if (pinfo & INSN_READ_GPR_T)
2309ddf2 3061 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4c260379 3062 if (pinfo & INSN_READ_GPR_S)
2309ddf2
MR
3063 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3064 if (pinfo2 & INSN2_READ_GP)
3065 mask |= 1 << GP;
3066 if (pinfo2 & INSN2_READ_GPR_31)
3067 mask |= 1 << RA;
4c260379 3068 if (pinfo2 & INSN2_READ_GPR_Z)
2309ddf2 3069 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3070 }
2b0c8b40
MR
3071 if (mips_opts.micromips)
3072 {
3073 if (pinfo2 & INSN2_READ_GPR_MC)
3074 mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3075 if (pinfo2 & INSN2_READ_GPR_ME)
3076 mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3077 if (pinfo2 & INSN2_READ_GPR_MG)
3078 mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3079 if (pinfo2 & INSN2_READ_GPR_MJ)
3080 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3081 if (pinfo2 & INSN2_READ_GPR_MMN)
3082 {
3083 mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3084 mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3085 }
3086 if (pinfo2 & INSN2_READ_GPR_MP)
3087 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3088 if (pinfo2 & INSN2_READ_GPR_MQ)
3089 mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3090 }
fe35f09f
RS
3091 /* Don't include register 0. */
3092 return mask & ~1;
4c260379
RS
3093}
3094
3095/* Return the mask of core registers that IP writes. */
3096
3097static unsigned int
3098gpr_write_mask (const struct mips_cl_insn *ip)
3099{
3100 unsigned long pinfo, pinfo2;
3101 unsigned int mask;
3102
df58fc94 3103 mask = gpr_mod_mask (ip);
4c260379
RS
3104 pinfo = ip->insn_mo->pinfo;
3105 pinfo2 = ip->insn_mo->pinfo2;
3106 if (mips_opts.mips16)
3107 {
3108 if (pinfo & MIPS16_INSN_WRITE_X)
3109 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3110 if (pinfo & MIPS16_INSN_WRITE_Y)
3111 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3112 if (pinfo & MIPS16_INSN_WRITE_Z)
3113 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3114 if (pinfo & MIPS16_INSN_WRITE_T)
3115 mask |= 1 << TREG;
3116 if (pinfo & MIPS16_INSN_WRITE_SP)
3117 mask |= 1 << SP;
3118 if (pinfo & MIPS16_INSN_WRITE_31)
3119 mask |= 1 << RA;
3120 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3121 mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3122 }
3123 else
3124 {
3125 if (pinfo & INSN_WRITE_GPR_D)
df58fc94 3126 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3127 if (pinfo & INSN_WRITE_GPR_T)
df58fc94 3128 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
2b0c8b40 3129 if (pinfo & INSN_WRITE_GPR_S)
2309ddf2 3130 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379
RS
3131 if (pinfo & INSN_WRITE_GPR_31)
3132 mask |= 1 << RA;
3133 if (pinfo2 & INSN2_WRITE_GPR_Z)
df58fc94 3134 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3135 }
2b0c8b40
MR
3136 if (mips_opts.micromips)
3137 {
3138 if (pinfo2 & INSN2_WRITE_GPR_MB)
3139 mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3140 if (pinfo2 & INSN2_WRITE_GPR_MHI)
3141 {
3142 mask |= 1 << micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
3143 mask |= 1 << micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
3144 }
3145 if (pinfo2 & INSN2_WRITE_GPR_MJ)
3146 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3147 if (pinfo2 & INSN2_WRITE_GPR_MP)
3148 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3149 }
fe35f09f
RS
3150 /* Don't include register 0. */
3151 return mask & ~1;
4c260379
RS
3152}
3153
3154/* Return the mask of floating-point registers that IP reads. */
3155
3156static unsigned int
3157fpr_read_mask (const struct mips_cl_insn *ip)
3158{
3159 unsigned long pinfo, pinfo2;
3160 unsigned int mask;
3161
3162 mask = 0;
3163 pinfo = ip->insn_mo->pinfo;
3164 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3165 if (!mips_opts.mips16)
df58fc94
RS
3166 {
3167 if (pinfo2 & INSN2_READ_FPR_D)
2309ddf2 3168 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3169 if (pinfo & INSN_READ_FPR_S)
df58fc94 3170 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3171 if (pinfo & INSN_READ_FPR_T)
df58fc94 3172 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3173 if (pinfo & INSN_READ_FPR_R)
df58fc94 3174 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
4c260379 3175 if (pinfo2 & INSN2_READ_FPR_Z)
df58fc94 3176 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3177 }
3178 /* Conservatively treat all operands to an FP_D instruction are doubles.
3179 (This is overly pessimistic for things like cvt.d.s.) */
3180 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3181 mask |= mask << 1;
3182 return mask;
3183}
3184
3185/* Return the mask of floating-point registers that IP writes. */
3186
3187static unsigned int
3188fpr_write_mask (const struct mips_cl_insn *ip)
3189{
3190 unsigned long pinfo, pinfo2;
3191 unsigned int mask;
3192
3193 mask = 0;
3194 pinfo = ip->insn_mo->pinfo;
3195 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3196 if (!mips_opts.mips16)
4c260379
RS
3197 {
3198 if (pinfo & INSN_WRITE_FPR_D)
df58fc94 3199 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3200 if (pinfo & INSN_WRITE_FPR_S)
df58fc94 3201 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3202 if (pinfo & INSN_WRITE_FPR_T)
df58fc94 3203 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3204 if (pinfo2 & INSN2_WRITE_FPR_Z)
df58fc94 3205 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3206 }
3207 /* Conservatively treat all operands to an FP_D instruction are doubles.
3208 (This is overly pessimistic for things like cvt.s.d.) */
3209 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3210 mask |= mask << 1;
3211 return mask;
3212}
3213
71400594
RS
3214/* Classify an instruction according to the FIX_VR4120_* enumeration.
3215 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3216 by VR4120 errata. */
4d7206a2 3217
71400594
RS
3218static unsigned int
3219classify_vr4120_insn (const char *name)
252b5132 3220{
71400594
RS
3221 if (strncmp (name, "macc", 4) == 0)
3222 return FIX_VR4120_MACC;
3223 if (strncmp (name, "dmacc", 5) == 0)
3224 return FIX_VR4120_DMACC;
3225 if (strncmp (name, "mult", 4) == 0)
3226 return FIX_VR4120_MULT;
3227 if (strncmp (name, "dmult", 5) == 0)
3228 return FIX_VR4120_DMULT;
3229 if (strstr (name, "div"))
3230 return FIX_VR4120_DIV;
3231 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3232 return FIX_VR4120_MTHILO;
3233 return NUM_FIX_VR4120_CLASSES;
3234}
252b5132 3235
ff239038
CM
3236#define INSN_ERET 0x42000018
3237#define INSN_DERET 0x4200001f
3238
71400594
RS
3239/* Return the number of instructions that must separate INSN1 and INSN2,
3240 where INSN1 is the earlier instruction. Return the worst-case value
3241 for any INSN2 if INSN2 is null. */
252b5132 3242
71400594
RS
3243static unsigned int
3244insns_between (const struct mips_cl_insn *insn1,
3245 const struct mips_cl_insn *insn2)
3246{
3247 unsigned long pinfo1, pinfo2;
4c260379 3248 unsigned int mask;
71400594
RS
3249
3250 /* This function needs to know which pinfo flags are set for INSN2
3251 and which registers INSN2 uses. The former is stored in PINFO2 and
4c260379
RS
3252 the latter is tested via INSN2_USES_GPR. If INSN2 is null, PINFO2
3253 will have every flag set and INSN2_USES_GPR will always return true. */
71400594
RS
3254 pinfo1 = insn1->insn_mo->pinfo;
3255 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 3256
4c260379
RS
3257#define INSN2_USES_GPR(REG) \
3258 (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
71400594
RS
3259
3260 /* For most targets, write-after-read dependencies on the HI and LO
3261 registers must be separated by at least two instructions. */
3262 if (!hilo_interlocks)
252b5132 3263 {
71400594
RS
3264 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3265 return 2;
3266 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3267 return 2;
3268 }
3269
3270 /* If we're working around r7000 errata, there must be two instructions
3271 between an mfhi or mflo and any instruction that uses the result. */
3272 if (mips_7000_hilo_fix
df58fc94 3273 && !mips_opts.micromips
71400594 3274 && MF_HILO_INSN (pinfo1)
df58fc94 3275 && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
71400594
RS
3276 return 2;
3277
ff239038
CM
3278 /* If we're working around 24K errata, one instruction is required
3279 if an ERET or DERET is followed by a branch instruction. */
df58fc94 3280 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
3281 {
3282 if (insn1->insn_opcode == INSN_ERET
3283 || insn1->insn_opcode == INSN_DERET)
3284 {
3285 if (insn2 == NULL
3286 || insn2->insn_opcode == INSN_ERET
3287 || insn2->insn_opcode == INSN_DERET
11625dd8 3288 || delayed_branch_p (insn2))
ff239038
CM
3289 return 1;
3290 }
3291 }
3292
71400594
RS
3293 /* If working around VR4120 errata, check for combinations that need
3294 a single intervening instruction. */
df58fc94 3295 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
3296 {
3297 unsigned int class1, class2;
252b5132 3298
71400594
RS
3299 class1 = classify_vr4120_insn (insn1->insn_mo->name);
3300 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 3301 {
71400594
RS
3302 if (insn2 == NULL)
3303 return 1;
3304 class2 = classify_vr4120_insn (insn2->insn_mo->name);
3305 if (vr4120_conflicts[class1] & (1 << class2))
3306 return 1;
252b5132 3307 }
71400594
RS
3308 }
3309
df58fc94 3310 if (!HAVE_CODE_COMPRESSION)
71400594
RS
3311 {
3312 /* Check for GPR or coprocessor load delays. All such delays
3313 are on the RT register. */
3314 /* Itbl support may require additional care here. */
3315 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3316 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 3317 {
71400594 3318 know (pinfo1 & INSN_WRITE_GPR_T);
df58fc94 3319 if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
71400594
RS
3320 return 1;
3321 }
3322
3323 /* Check for generic coprocessor hazards.
3324
3325 This case is not handled very well. There is no special
3326 knowledge of CP0 handling, and the coprocessors other than
3327 the floating point unit are not distinguished at all. */
3328 /* Itbl support may require additional care here. FIXME!
3329 Need to modify this to include knowledge about
3330 user specified delays! */
3331 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3332 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3333 {
3334 /* Handle cases where INSN1 writes to a known general coprocessor
3335 register. There must be a one instruction delay before INSN2
3336 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
3337 mask = fpr_write_mask (insn1);
3338 if (mask != 0)
252b5132 3339 {
4c260379 3340 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 3341 return 1;
252b5132
RH
3342 }
3343 else
3344 {
71400594
RS
3345 /* Read-after-write dependencies on the control registers
3346 require a two-instruction gap. */
3347 if ((pinfo1 & INSN_WRITE_COND_CODE)
3348 && (pinfo2 & INSN_READ_COND_CODE))
3349 return 2;
3350
3351 /* We don't know exactly what INSN1 does. If INSN2 is
3352 also a coprocessor instruction, assume there must be
3353 a one instruction gap. */
3354 if (pinfo2 & INSN_COP)
3355 return 1;
252b5132
RH
3356 }
3357 }
6b76fefe 3358
71400594
RS
3359 /* Check for read-after-write dependencies on the coprocessor
3360 control registers in cases where INSN1 does not need a general
3361 coprocessor delay. This means that INSN1 is a floating point
3362 comparison instruction. */
3363 /* Itbl support may require additional care here. */
3364 else if (!cop_interlocks
3365 && (pinfo1 & INSN_WRITE_COND_CODE)
3366 && (pinfo2 & INSN_READ_COND_CODE))
3367 return 1;
3368 }
6b76fefe 3369
4c260379 3370#undef INSN2_USES_GPR
6b76fefe 3371
71400594
RS
3372 return 0;
3373}
6b76fefe 3374
7d8e00cf
RS
3375/* Return the number of nops that would be needed to work around the
3376 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
3377 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
3378 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
3379
3380static int
932d1a1b 3381nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
3382 const struct mips_cl_insn *insn)
3383{
4c260379
RS
3384 int i, j;
3385 unsigned int mask;
7d8e00cf
RS
3386
3387 /* Check if the instruction writes to HI or LO. MTHI and MTLO
3388 are not affected by the errata. */
3389 if (insn != 0
3390 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3391 || strcmp (insn->insn_mo->name, "mtlo") == 0
3392 || strcmp (insn->insn_mo->name, "mthi") == 0))
3393 return 0;
3394
3395 /* Search for the first MFLO or MFHI. */
3396 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 3397 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
3398 {
3399 /* Extract the destination register. */
4c260379 3400 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
3401
3402 /* No nops are needed if INSN reads that register. */
4c260379 3403 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
3404 return 0;
3405
3406 /* ...or if any of the intervening instructions do. */
3407 for (j = 0; j < i; j++)
4c260379 3408 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
3409 return 0;
3410
932d1a1b
RS
3411 if (i >= ignore)
3412 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
3413 }
3414 return 0;
3415}
3416
15be625d
CM
3417#define BASE_REG_EQ(INSN1, INSN2) \
3418 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3419 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3420
3421/* Return the minimum alignment for this store instruction. */
3422
3423static int
3424fix_24k_align_to (const struct mips_opcode *mo)
3425{
3426 if (strcmp (mo->name, "sh") == 0)
3427 return 2;
3428
3429 if (strcmp (mo->name, "swc1") == 0
3430 || strcmp (mo->name, "swc2") == 0
3431 || strcmp (mo->name, "sw") == 0
3432 || strcmp (mo->name, "sc") == 0
3433 || strcmp (mo->name, "s.s") == 0)
3434 return 4;
3435
3436 if (strcmp (mo->name, "sdc1") == 0
3437 || strcmp (mo->name, "sdc2") == 0
3438 || strcmp (mo->name, "s.d") == 0)
3439 return 8;
3440
3441 /* sb, swl, swr */
3442 return 1;
3443}
3444
3445struct fix_24k_store_info
3446 {
3447 /* Immediate offset, if any, for this store instruction. */
3448 short off;
3449 /* Alignment required by this store instruction. */
3450 int align_to;
3451 /* True for register offsets. */
3452 int register_offset;
3453 };
3454
3455/* Comparison function used by qsort. */
3456
3457static int
3458fix_24k_sort (const void *a, const void *b)
3459{
3460 const struct fix_24k_store_info *pos1 = a;
3461 const struct fix_24k_store_info *pos2 = b;
3462
3463 return (pos1->off - pos2->off);
3464}
3465
3466/* INSN is a store instruction. Try to record the store information
3467 in STINFO. Return false if the information isn't known. */
3468
3469static bfd_boolean
3470fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 3471 const struct mips_cl_insn *insn)
15be625d
CM
3472{
3473 /* The instruction must have a known offset. */
3474 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3475 return FALSE;
3476
3477 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3478 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3479 return TRUE;
3480}
3481
932d1a1b
RS
3482/* Return the number of nops that would be needed to work around the 24k
3483 "lost data on stores during refill" errata if instruction INSN
3484 immediately followed the 2 instructions described by HIST.
3485 Ignore hazards that are contained within the first IGNORE
3486 instructions of HIST.
3487
3488 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3489 for the data cache refills and store data. The following describes
3490 the scenario where the store data could be lost.
3491
3492 * A data cache miss, due to either a load or a store, causing fill
3493 data to be supplied by the memory subsystem
3494 * The first three doublewords of fill data are returned and written
3495 into the cache
3496 * A sequence of four stores occurs in consecutive cycles around the
3497 final doubleword of the fill:
3498 * Store A
3499 * Store B
3500 * Store C
3501 * Zero, One or more instructions
3502 * Store D
3503
3504 The four stores A-D must be to different doublewords of the line that
3505 is being filled. The fourth instruction in the sequence above permits
3506 the fill of the final doubleword to be transferred from the FSB into
3507 the cache. In the sequence above, the stores may be either integer
3508 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3509 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3510 different doublewords on the line. If the floating point unit is
3511 running in 1:2 mode, it is not possible to create the sequence above
3512 using only floating point store instructions.
15be625d
CM
3513
3514 In this case, the cache line being filled is incorrectly marked
3515 invalid, thereby losing the data from any store to the line that
3516 occurs between the original miss and the completion of the five
3517 cycle sequence shown above.
3518
932d1a1b 3519 The workarounds are:
15be625d 3520
932d1a1b
RS
3521 * Run the data cache in write-through mode.
3522 * Insert a non-store instruction between
3523 Store A and Store B or Store B and Store C. */
15be625d
CM
3524
3525static int
932d1a1b 3526nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
3527 const struct mips_cl_insn *insn)
3528{
3529 struct fix_24k_store_info pos[3];
3530 int align, i, base_offset;
3531
932d1a1b
RS
3532 if (ignore >= 2)
3533 return 0;
3534
ab9794cf
RS
3535 /* If the previous instruction wasn't a store, there's nothing to
3536 worry about. */
15be625d
CM
3537 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3538 return 0;
3539
ab9794cf
RS
3540 /* If the instructions after the previous one are unknown, we have
3541 to assume the worst. */
3542 if (!insn)
15be625d
CM
3543 return 1;
3544
ab9794cf
RS
3545 /* Check whether we are dealing with three consecutive stores. */
3546 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3547 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
3548 return 0;
3549
3550 /* If we don't know the relationship between the store addresses,
3551 assume the worst. */
ab9794cf 3552 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
3553 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3554 return 1;
3555
3556 if (!fix_24k_record_store_info (&pos[0], insn)
3557 || !fix_24k_record_store_info (&pos[1], &hist[0])
3558 || !fix_24k_record_store_info (&pos[2], &hist[1]))
3559 return 1;
3560
3561 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3562
3563 /* Pick a value of ALIGN and X such that all offsets are adjusted by
3564 X bytes and such that the base register + X is known to be aligned
3565 to align bytes. */
3566
3567 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3568 align = 8;
3569 else
3570 {
3571 align = pos[0].align_to;
3572 base_offset = pos[0].off;
3573 for (i = 1; i < 3; i++)
3574 if (align < pos[i].align_to)
3575 {
3576 align = pos[i].align_to;
3577 base_offset = pos[i].off;
3578 }
3579 for (i = 0; i < 3; i++)
3580 pos[i].off -= base_offset;
3581 }
3582
3583 pos[0].off &= ~align + 1;
3584 pos[1].off &= ~align + 1;
3585 pos[2].off &= ~align + 1;
3586
3587 /* If any two stores write to the same chunk, they also write to the
3588 same doubleword. The offsets are still sorted at this point. */
3589 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3590 return 0;
3591
3592 /* A range of at least 9 bytes is needed for the stores to be in
3593 non-overlapping doublewords. */
3594 if (pos[2].off - pos[0].off <= 8)
3595 return 0;
3596
3597 if (pos[2].off - pos[1].off >= 24
3598 || pos[1].off - pos[0].off >= 24
3599 || pos[2].off - pos[0].off >= 32)
3600 return 0;
3601
3602 return 1;
3603}
3604
71400594 3605/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 3606 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
3607 where HIST[0] is the most recent instruction. Ignore hazards
3608 between INSN and the first IGNORE instructions in HIST.
3609
3610 If INSN is null, return the worse-case number of nops for any
3611 instruction. */
bdaaa2e1 3612
71400594 3613static int
932d1a1b 3614nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
3615 const struct mips_cl_insn *insn)
3616{
3617 int i, nops, tmp_nops;
bdaaa2e1 3618
71400594 3619 nops = 0;
932d1a1b 3620 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 3621 {
91d6fa6a 3622 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
3623 if (tmp_nops > nops)
3624 nops = tmp_nops;
3625 }
7d8e00cf 3626
df58fc94 3627 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 3628 {
932d1a1b 3629 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
3630 if (tmp_nops > nops)
3631 nops = tmp_nops;
3632 }
3633
df58fc94 3634 if (mips_fix_24k && !mips_opts.micromips)
15be625d 3635 {
932d1a1b 3636 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
3637 if (tmp_nops > nops)
3638 nops = tmp_nops;
3639 }
3640
71400594
RS
3641 return nops;
3642}
252b5132 3643
71400594 3644/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 3645 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
3646 would be needed after the extended sequence, ignoring hazards
3647 in the first IGNORE instructions. */
252b5132 3648
71400594 3649static int
932d1a1b
RS
3650nops_for_sequence (int num_insns, int ignore,
3651 const struct mips_cl_insn *hist, ...)
71400594
RS
3652{
3653 va_list args;
3654 struct mips_cl_insn buffer[MAX_NOPS];
3655 struct mips_cl_insn *cursor;
3656 int nops;
3657
91d6fa6a 3658 va_start (args, hist);
71400594 3659 cursor = buffer + num_insns;
91d6fa6a 3660 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
3661 while (cursor > buffer)
3662 *--cursor = *va_arg (args, const struct mips_cl_insn *);
3663
932d1a1b 3664 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
3665 va_end (args);
3666 return nops;
3667}
252b5132 3668
71400594
RS
3669/* Like nops_for_insn, but if INSN is a branch, take into account the
3670 worst-case delay for the branch target. */
252b5132 3671
71400594 3672static int
932d1a1b 3673nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
3674 const struct mips_cl_insn *insn)
3675{
3676 int nops, tmp_nops;
60b63b72 3677
932d1a1b 3678 nops = nops_for_insn (ignore, hist, insn);
11625dd8 3679 if (delayed_branch_p (insn))
71400594 3680 {
932d1a1b 3681 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 3682 hist, insn, get_delay_slot_nop (insn));
71400594
RS
3683 if (tmp_nops > nops)
3684 nops = tmp_nops;
3685 }
11625dd8 3686 else if (compact_branch_p (insn))
71400594 3687 {
932d1a1b 3688 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
3689 if (tmp_nops > nops)
3690 nops = tmp_nops;
3691 }
3692 return nops;
3693}
3694
c67a084a
NC
3695/* Fix NOP issue: Replace nops by "or at,at,zero". */
3696
3697static void
3698fix_loongson2f_nop (struct mips_cl_insn * ip)
3699{
df58fc94 3700 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
3701 if (strcmp (ip->insn_mo->name, "nop") == 0)
3702 ip->insn_opcode = LOONGSON2F_NOP_INSN;
3703}
3704
3705/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
3706 jr target pc &= 'hffff_ffff_cfff_ffff. */
3707
3708static void
3709fix_loongson2f_jump (struct mips_cl_insn * ip)
3710{
df58fc94 3711 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
3712 if (strcmp (ip->insn_mo->name, "j") == 0
3713 || strcmp (ip->insn_mo->name, "jr") == 0
3714 || strcmp (ip->insn_mo->name, "jalr") == 0)
3715 {
3716 int sreg;
3717 expressionS ep;
3718
3719 if (! mips_opts.at)
3720 return;
3721
df58fc94 3722 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
3723 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
3724 return;
3725
3726 ep.X_op = O_constant;
3727 ep.X_add_number = 0xcfff0000;
3728 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
3729 ep.X_add_number = 0xffff;
3730 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
3731 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
3732 }
3733}
3734
3735static void
3736fix_loongson2f (struct mips_cl_insn * ip)
3737{
3738 if (mips_fix_loongson2f_nop)
3739 fix_loongson2f_nop (ip);
3740
3741 if (mips_fix_loongson2f_jump)
3742 fix_loongson2f_jump (ip);
3743}
3744
a4e06468
RS
3745/* IP is a branch that has a delay slot, and we need to fill it
3746 automatically. Return true if we can do that by swapping IP
e407c74b
NC
3747 with the previous instruction.
3748 ADDRESS_EXPR is an operand of the instruction to be used with
3749 RELOC_TYPE. */
a4e06468
RS
3750
3751static bfd_boolean
e407c74b
NC
3752can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
3753 bfd_reloc_code_real_type *reloc_type)
a4e06468 3754{
2b0c8b40 3755 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468
RS
3756 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
3757
3758 /* -O2 and above is required for this optimization. */
3759 if (mips_optimize < 2)
3760 return FALSE;
3761
3762 /* If we have seen .set volatile or .set nomove, don't optimize. */
3763 if (mips_opts.nomove)
3764 return FALSE;
3765
3766 /* We can't swap if the previous instruction's position is fixed. */
3767 if (history[0].fixed_p)
3768 return FALSE;
3769
3770 /* If the previous previous insn was in a .set noreorder, we can't
3771 swap. Actually, the MIPS assembler will swap in this situation.
3772 However, gcc configured -with-gnu-as will generate code like
3773
3774 .set noreorder
3775 lw $4,XXX
3776 .set reorder
3777 INSN
3778 bne $4,$0,foo
3779
3780 in which we can not swap the bne and INSN. If gcc is not configured
3781 -with-gnu-as, it does not output the .set pseudo-ops. */
3782 if (history[1].noreorder_p)
3783 return FALSE;
3784
87333bb7
MR
3785 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
3786 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
3787 if (mips_opts.mips16 && history[0].fixp[0])
3788 return FALSE;
3789
3790 /* If the branch is itself the target of a branch, we can not swap.
3791 We cheat on this; all we check for is whether there is a label on
3792 this instruction. If there are any branches to anything other than
3793 a label, users must use .set noreorder. */
3794 if (seg_info (now_seg)->label_list)
3795 return FALSE;
3796
3797 /* If the previous instruction is in a variant frag other than this
2309ddf2 3798 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
3799 MIPS16 code, which uses variant frags for different purposes. */
3800 if (!mips_opts.mips16
a4e06468
RS
3801 && history[0].frag
3802 && history[0].frag->fr_type == rs_machine_dependent)
3803 return FALSE;
3804
bcd530a7
RS
3805 /* We do not swap with instructions that cannot architecturally
3806 be placed in a branch delay slot, such as SYNC or ERET. We
3807 also refrain from swapping with a trap instruction, since it
3808 complicates trap handlers to have the trap instruction be in
3809 a delay slot. */
a4e06468 3810 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 3811 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
3812 return FALSE;
3813
3814 /* Check for conflicts between the branch and the instructions
3815 before the candidate delay slot. */
3816 if (nops_for_insn (0, history + 1, ip) > 0)
3817 return FALSE;
3818
3819 /* Check for conflicts between the swapped sequence and the
3820 target of the branch. */
3821 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
3822 return FALSE;
3823
3824 /* If the branch reads a register that the previous
3825 instruction sets, we can not swap. */
3826 gpr_read = gpr_read_mask (ip);
3827 prev_gpr_write = gpr_write_mask (&history[0]);
3828 if (gpr_read & prev_gpr_write)
3829 return FALSE;
3830
3831 /* If the branch writes a register that the previous
3832 instruction sets, we can not swap. */
3833 gpr_write = gpr_write_mask (ip);
3834 if (gpr_write & prev_gpr_write)
3835 return FALSE;
3836
3837 /* If the branch writes a register that the previous
3838 instruction reads, we can not swap. */
3839 prev_gpr_read = gpr_read_mask (&history[0]);
3840 if (gpr_write & prev_gpr_read)
3841 return FALSE;
3842
3843 /* If one instruction sets a condition code and the
3844 other one uses a condition code, we can not swap. */
3845 pinfo = ip->insn_mo->pinfo;
3846 if ((pinfo & INSN_READ_COND_CODE)
3847 && (prev_pinfo & INSN_WRITE_COND_CODE))
3848 return FALSE;
3849 if ((pinfo & INSN_WRITE_COND_CODE)
3850 && (prev_pinfo & INSN_READ_COND_CODE))
3851 return FALSE;
3852
3853 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 3854 prev_pinfo2 = history[0].insn_mo->pinfo2;
a4e06468
RS
3855 if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
3856 return FALSE;
2b0c8b40
MR
3857 if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
3858 return FALSE;
a4e06468 3859
df58fc94
RS
3860 /* If the previous instruction has an incorrect size for a fixed
3861 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
3862 pinfo2 = ip->insn_mo->pinfo2;
3863 if (mips_opts.micromips
3864 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
3865 && insn_length (history) != 2)
3866 return FALSE;
3867 if (mips_opts.micromips
3868 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
3869 && insn_length (history) != 4)
3870 return FALSE;
3871
e407c74b
NC
3872 /* On R5900 short loops need to be fixed by inserting a nop in
3873 the branch delay slots.
3874 A short loop can be terminated too early. */
3875 if (mips_opts.arch == CPU_R5900
3876 /* Check if instruction has a parameter, ignore "j $31". */
3877 && (address_expr != NULL)
3878 /* Parameter must be 16 bit. */
3879 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
3880 /* Branch to same segment. */
3881 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
3882 /* Branch to same code fragment. */
3883 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
3884 /* Can only calculate branch offset if value is known. */
3885 && symbol_constant_p(address_expr->X_add_symbol)
3886 /* Check if branch is really conditional. */
3887 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
3888 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
3889 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
3890 {
3891 int distance;
3892 /* Check if loop is shorter than 6 instructions including
3893 branch and delay slot. */
3894 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
3895 if (distance <= 20)
3896 {
3897 int i;
3898 int rv;
3899
3900 rv = FALSE;
3901 /* When the loop includes branches or jumps,
3902 it is not a short loop. */
3903 for (i = 0; i < (distance / 4); i++)
3904 {
3905 if ((history[i].cleared_p)
3906 || delayed_branch_p(&history[i]))
3907 {
3908 rv = TRUE;
3909 break;
3910 }
3911 }
3912 if (rv == FALSE)
3913 {
3914 /* Insert nop after branch to fix short loop. */
3915 return FALSE;
3916 }
3917 }
3918 }
3919
a4e06468
RS
3920 return TRUE;
3921}
3922
e407c74b
NC
3923/* Decide how we should add IP to the instruction stream.
3924 ADDRESS_EXPR is an operand of the instruction to be used with
3925 RELOC_TYPE. */
a4e06468
RS
3926
3927static enum append_method
e407c74b
NC
3928get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
3929 bfd_reloc_code_real_type *reloc_type)
a4e06468
RS
3930{
3931 unsigned long pinfo;
3932
3933 /* The relaxed version of a macro sequence must be inherently
3934 hazard-free. */
3935 if (mips_relax.sequence == 2)
3936 return APPEND_ADD;
3937
3938 /* We must not dabble with instructions in a ".set norerorder" block. */
3939 if (mips_opts.noreorder)
3940 return APPEND_ADD;
3941
3942 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 3943 if (delayed_branch_p (ip))
a4e06468 3944 {
e407c74b
NC
3945 if (!branch_likely_p (ip)
3946 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
3947 return APPEND_SWAP;
3948
11625dd8 3949 pinfo = ip->insn_mo->pinfo;
a4e06468
RS
3950 if (mips_opts.mips16
3951 && ISA_SUPPORTS_MIPS16E
a4e06468
RS
3952 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
3953 return APPEND_ADD_COMPACT;
3954
3955 return APPEND_ADD_WITH_NOP;
3956 }
3957
a4e06468
RS
3958 return APPEND_ADD;
3959}
3960
ceb94aa5
RS
3961/* IP is a MIPS16 instruction whose opcode we have just changed.
3962 Point IP->insn_mo to the new opcode's definition. */
3963
3964static void
3965find_altered_mips16_opcode (struct mips_cl_insn *ip)
3966{
3967 const struct mips_opcode *mo, *end;
3968
3969 end = &mips16_opcodes[bfd_mips16_num_opcodes];
3970 for (mo = ip->insn_mo; mo < end; mo++)
3971 if ((ip->insn_opcode & mo->mask) == mo->match)
3972 {
3973 ip->insn_mo = mo;
3974 return;
3975 }
3976 abort ();
3977}
3978
df58fc94
RS
3979/* For microMIPS macros, we need to generate a local number label
3980 as the target of branches. */
3981#define MICROMIPS_LABEL_CHAR '\037'
3982static unsigned long micromips_target_label;
3983static char micromips_target_name[32];
3984
3985static char *
3986micromips_label_name (void)
3987{
3988 char *p = micromips_target_name;
3989 char symbol_name_temporary[24];
3990 unsigned long l;
3991 int i;
3992
3993 if (*p)
3994 return p;
3995
3996 i = 0;
3997 l = micromips_target_label;
3998#ifdef LOCAL_LABEL_PREFIX
3999 *p++ = LOCAL_LABEL_PREFIX;
4000#endif
4001 *p++ = 'L';
4002 *p++ = MICROMIPS_LABEL_CHAR;
4003 do
4004 {
4005 symbol_name_temporary[i++] = l % 10 + '0';
4006 l /= 10;
4007 }
4008 while (l != 0);
4009 while (i > 0)
4010 *p++ = symbol_name_temporary[--i];
4011 *p = '\0';
4012
4013 return micromips_target_name;
4014}
4015
4016static void
4017micromips_label_expr (expressionS *label_expr)
4018{
4019 label_expr->X_op = O_symbol;
4020 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
4021 label_expr->X_add_number = 0;
4022}
4023
4024static void
4025micromips_label_inc (void)
4026{
4027 micromips_target_label++;
4028 *micromips_target_name = '\0';
4029}
4030
4031static void
4032micromips_add_label (void)
4033{
4034 symbolS *s;
4035
4036 s = colon (micromips_label_name ());
4037 micromips_label_inc ();
4038#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
4039 if (IS_ELF)
4040 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
db9b2be4
AM
4041#else
4042 (void) s;
df58fc94
RS
4043#endif
4044}
4045
4046/* If assembling microMIPS code, then return the microMIPS reloc
4047 corresponding to the requested one if any. Otherwise return
4048 the reloc unchanged. */
4049
4050static bfd_reloc_code_real_type
4051micromips_map_reloc (bfd_reloc_code_real_type reloc)
4052{
4053 static const bfd_reloc_code_real_type relocs[][2] =
4054 {
4055 /* Keep sorted incrementally by the left-hand key. */
4056 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
4057 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
4058 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
4059 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
4060 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
4061 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
4062 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
4063 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
4064 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
4065 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
4066 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
4067 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
4068 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
4069 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
4070 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
4071 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
4072 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
4073 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
4074 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
4075 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
4076 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
4077 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
4078 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
4079 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
4080 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
4081 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
4082 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
4083 };
4084 bfd_reloc_code_real_type r;
4085 size_t i;
4086
4087 if (!mips_opts.micromips)
4088 return reloc;
4089 for (i = 0; i < ARRAY_SIZE (relocs); i++)
4090 {
4091 r = relocs[i][0];
4092 if (r > reloc)
4093 return reloc;
4094 if (r == reloc)
4095 return relocs[i][1];
4096 }
4097 return reloc;
4098}
4099
b886a2ab
RS
4100/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
4101 Return true on success, storing the resolved value in RESULT. */
4102
4103static bfd_boolean
4104calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
4105 offsetT *result)
4106{
4107 switch (reloc)
4108 {
4109 case BFD_RELOC_MIPS_HIGHEST:
4110 case BFD_RELOC_MICROMIPS_HIGHEST:
4111 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
4112 return TRUE;
4113
4114 case BFD_RELOC_MIPS_HIGHER:
4115 case BFD_RELOC_MICROMIPS_HIGHER:
4116 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
4117 return TRUE;
4118
4119 case BFD_RELOC_HI16_S:
4120 case BFD_RELOC_MICROMIPS_HI16_S:
4121 case BFD_RELOC_MIPS16_HI16_S:
4122 *result = ((operand + 0x8000) >> 16) & 0xffff;
4123 return TRUE;
4124
4125 case BFD_RELOC_HI16:
4126 case BFD_RELOC_MICROMIPS_HI16:
4127 case BFD_RELOC_MIPS16_HI16:
4128 *result = (operand >> 16) & 0xffff;
4129 return TRUE;
4130
4131 case BFD_RELOC_LO16:
4132 case BFD_RELOC_MICROMIPS_LO16:
4133 case BFD_RELOC_MIPS16_LO16:
4134 *result = operand & 0xffff;
4135 return TRUE;
4136
4137 case BFD_RELOC_UNUSED:
4138 *result = operand;
4139 return TRUE;
4140
4141 default:
4142 return FALSE;
4143 }
4144}
4145
71400594
RS
4146/* Output an instruction. IP is the instruction information.
4147 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
4148 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
4149 a macro expansion. */
71400594
RS
4150
4151static void
4152append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 4153 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 4154{
14fe068b 4155 unsigned long prev_pinfo2, pinfo;
71400594 4156 bfd_boolean relaxed_branch = FALSE;
a4e06468 4157 enum append_method method;
2309ddf2 4158 bfd_boolean relax32;
2b0c8b40 4159 int branch_disp;
71400594 4160
2309ddf2 4161 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
4162 fix_loongson2f (ip);
4163
738f4d98 4164 file_ase_mips16 |= mips_opts.mips16;
df58fc94 4165 file_ase_micromips |= mips_opts.micromips;
738f4d98 4166
df58fc94 4167 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 4168 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
4169
4170 if (mips_opts.micromips
4171 && !expansionp
4172 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4173 && micromips_insn_length (ip->insn_mo) != 2)
4174 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4175 && micromips_insn_length (ip->insn_mo) != 4)))
4176 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4177 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 4178
15be625d
CM
4179 if (address_expr == NULL)
4180 ip->complete_p = 1;
b886a2ab
RS
4181 else if (reloc_type[0] <= BFD_RELOC_UNUSED
4182 && reloc_type[1] == BFD_RELOC_UNUSED
4183 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
4184 && address_expr->X_op == O_constant)
4185 {
15be625d
CM
4186 switch (*reloc_type)
4187 {
15be625d 4188 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
4189 {
4190 int shift;
4191
4192 shift = mips_opts.micromips ? 1 : 2;
4193 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4194 as_bad (_("jump to misaligned address (0x%lx)"),
4195 (unsigned long) address_expr->X_add_number);
4196 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4197 & 0x3ffffff);
335574df 4198 ip->complete_p = 1;
df58fc94 4199 }
15be625d
CM
4200 break;
4201
4202 case BFD_RELOC_MIPS16_JMP:
4203 if ((address_expr->X_add_number & 3) != 0)
4204 as_bad (_("jump to misaligned address (0x%lx)"),
4205 (unsigned long) address_expr->X_add_number);
4206 ip->insn_opcode |=
4207 (((address_expr->X_add_number & 0x7c0000) << 3)
4208 | ((address_expr->X_add_number & 0xf800000) >> 7)
4209 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 4210 ip->complete_p = 1;
15be625d
CM
4211 break;
4212
4213 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
4214 {
4215 int shift;
4216
4217 shift = mips_opts.micromips ? 1 : 2;
4218 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4219 as_bad (_("branch to misaligned address (0x%lx)"),
4220 (unsigned long) address_expr->X_add_number);
4221 if (!mips_relax_branch)
4222 {
4223 if ((address_expr->X_add_number + (1 << (shift + 15)))
4224 & ~((1 << (shift + 16)) - 1))
4225 as_bad (_("branch address range overflow (0x%lx)"),
4226 (unsigned long) address_expr->X_add_number);
4227 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4228 & 0xffff);
4229 }
df58fc94 4230 }
15be625d
CM
4231 break;
4232
4233 default:
b886a2ab
RS
4234 {
4235 offsetT value;
4236
4237 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
4238 &value))
4239 {
4240 ip->insn_opcode |= value & 0xffff;
4241 ip->complete_p = 1;
4242 }
4243 }
4244 break;
4245 }
15be625d
CM
4246 }
4247
71400594
RS
4248 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4249 {
4250 /* There are a lot of optimizations we could do that we don't.
4251 In particular, we do not, in general, reorder instructions.
4252 If you use gcc with optimization, it will reorder
4253 instructions and generally do much more optimization then we
4254 do here; repeating all that work in the assembler would only
4255 benefit hand written assembly code, and does not seem worth
4256 it. */
4257 int nops = (mips_optimize == 0
932d1a1b
RS
4258 ? nops_for_insn (0, history, NULL)
4259 : nops_for_insn_or_target (0, history, ip));
71400594 4260 if (nops > 0)
252b5132
RH
4261 {
4262 fragS *old_frag;
4263 unsigned long old_frag_offset;
4264 int i;
252b5132
RH
4265
4266 old_frag = frag_now;
4267 old_frag_offset = frag_now_fix ();
4268
4269 for (i = 0; i < nops; i++)
14fe068b
RS
4270 add_fixed_insn (NOP_INSN);
4271 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
4272
4273 if (listing)
4274 {
4275 listing_prev_line ();
4276 /* We may be at the start of a variant frag. In case we
4277 are, make sure there is enough space for the frag
4278 after the frags created by listing_prev_line. The
4279 argument to frag_grow here must be at least as large
4280 as the argument to all other calls to frag_grow in
4281 this file. We don't have to worry about being in the
4282 middle of a variant frag, because the variants insert
4283 all needed nop instructions themselves. */
4284 frag_grow (40);
4285 }
4286
462427c4 4287 mips_move_text_labels ();
252b5132
RH
4288
4289#ifndef NO_ECOFF_DEBUGGING
4290 if (ECOFF_DEBUGGING)
4291 ecoff_fix_loc (old_frag, old_frag_offset);
4292#endif
4293 }
71400594
RS
4294 }
4295 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4296 {
932d1a1b
RS
4297 int nops;
4298
4299 /* Work out how many nops in prev_nop_frag are needed by IP,
4300 ignoring hazards generated by the first prev_nop_frag_since
4301 instructions. */
4302 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 4303 gas_assert (nops <= prev_nop_frag_holds);
252b5132 4304
71400594
RS
4305 /* Enforce NOPS as a minimum. */
4306 if (nops > prev_nop_frag_required)
4307 prev_nop_frag_required = nops;
252b5132 4308
71400594
RS
4309 if (prev_nop_frag_holds == prev_nop_frag_required)
4310 {
4311 /* Settle for the current number of nops. Update the history
4312 accordingly (for the benefit of any future .set reorder code). */
4313 prev_nop_frag = NULL;
4314 insert_into_history (prev_nop_frag_since,
4315 prev_nop_frag_holds, NOP_INSN);
4316 }
4317 else
4318 {
4319 /* Allow this instruction to replace one of the nops that was
4320 tentatively added to prev_nop_frag. */
df58fc94 4321 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
4322 prev_nop_frag_holds--;
4323 prev_nop_frag_since++;
252b5132
RH
4324 }
4325 }
4326
e407c74b 4327 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 4328 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 4329
58e2ea4d 4330#ifdef OBJ_ELF
e410add4
RS
4331 dwarf2_emit_insn (0);
4332 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
4333 so "move" the instruction address accordingly.
4334
4335 Also, it doesn't seem appropriate for the assembler to reorder .loc
4336 entries. If this instruction is a branch that we are going to swap
4337 with the previous instruction, the two instructions should be
4338 treated as a unit, and the debug information for both instructions
4339 should refer to the start of the branch sequence. Using the
4340 current position is certainly wrong when swapping a 32-bit branch
4341 and a 16-bit delay slot, since the current position would then be
4342 in the middle of a branch. */
4343 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d
MR
4344#endif
4345
df58fc94
RS
4346 relax32 = (mips_relax_branch
4347 /* Don't try branch relaxation within .set nomacro, or within
4348 .set noat if we use $at for PIC computations. If it turns
4349 out that the branch was out-of-range, we'll get an error. */
4350 && !mips_opts.warn_about_macros
4351 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
4352 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
4353 as they have no complementing branches. */
4354 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
4355
4356 if (!HAVE_CODE_COMPRESSION
4357 && address_expr
4358 && relax32
0b25d3e6 4359 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 4360 && delayed_branch_p (ip))
4a6a3df4 4361 {
895921c9 4362 relaxed_branch = TRUE;
1e915849
RS
4363 add_relaxed_insn (ip, (relaxed_branch_length
4364 (NULL, NULL,
11625dd8
RS
4365 uncond_branch_p (ip) ? -1
4366 : branch_likely_p (ip) ? 1
1e915849
RS
4367 : 0)), 4,
4368 RELAX_BRANCH_ENCODE
66b3e8da 4369 (AT,
11625dd8
RS
4370 uncond_branch_p (ip),
4371 branch_likely_p (ip),
1e915849
RS
4372 pinfo & INSN_WRITE_GPR_31,
4373 0),
4374 address_expr->X_add_symbol,
4375 address_expr->X_add_number);
4a6a3df4
AO
4376 *reloc_type = BFD_RELOC_UNUSED;
4377 }
df58fc94
RS
4378 else if (mips_opts.micromips
4379 && address_expr
4380 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4381 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
4382 && (delayed_branch_p (ip) || compact_branch_p (ip))
4383 /* Don't try branch relaxation when users specify
4384 16-bit/32-bit instructions. */
4385 && !forced_insn_length)
df58fc94
RS
4386 {
4387 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4388 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
4389 int uncond = uncond_branch_p (ip) ? -1 : 0;
4390 int compact = compact_branch_p (ip);
df58fc94
RS
4391 int al = pinfo & INSN_WRITE_GPR_31;
4392 int length32;
4393
4394 gas_assert (address_expr != NULL);
4395 gas_assert (!mips_relax.sequence);
4396
2b0c8b40 4397 relaxed_branch = TRUE;
df58fc94
RS
4398 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4399 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
4400 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4401 relax32, 0, 0),
df58fc94
RS
4402 address_expr->X_add_symbol,
4403 address_expr->X_add_number);
4404 *reloc_type = BFD_RELOC_UNUSED;
4405 }
4406 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
4407 {
4408 /* We need to set up a variant frag. */
df58fc94 4409 gas_assert (address_expr != NULL);
1e915849
RS
4410 add_relaxed_insn (ip, 4, 0,
4411 RELAX_MIPS16_ENCODE
4412 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 4413 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 4414 delayed_branch_p (&history[0]),
1e915849
RS
4415 history[0].mips16_absolute_jump_p),
4416 make_expr_symbol (address_expr), 0);
252b5132 4417 }
5c04167a 4418 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 4419 {
11625dd8 4420 if (!delayed_branch_p (ip))
b8ee1a6e
DU
4421 /* Make sure there is enough room to swap this instruction with
4422 a following jump instruction. */
4423 frag_grow (6);
1e915849 4424 add_fixed_insn (ip);
252b5132
RH
4425 }
4426 else
4427 {
4428 if (mips_opts.mips16
4429 && mips_opts.noreorder
11625dd8 4430 && delayed_branch_p (&history[0]))
252b5132
RH
4431 as_warn (_("extended instruction in delay slot"));
4432
4d7206a2
RS
4433 if (mips_relax.sequence)
4434 {
4435 /* If we've reached the end of this frag, turn it into a variant
4436 frag and record the information for the instructions we've
4437 written so far. */
4438 if (frag_room () < 4)
4439 relax_close_frag ();
df58fc94 4440 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
4441 }
4442
584892a6 4443 if (mips_relax.sequence != 2)
df58fc94
RS
4444 {
4445 if (mips_macro_warning.first_insn_sizes[0] == 0)
4446 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4447 mips_macro_warning.sizes[0] += insn_length (ip);
4448 mips_macro_warning.insns[0]++;
4449 }
584892a6 4450 if (mips_relax.sequence != 1)
df58fc94
RS
4451 {
4452 if (mips_macro_warning.first_insn_sizes[1] == 0)
4453 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4454 mips_macro_warning.sizes[1] += insn_length (ip);
4455 mips_macro_warning.insns[1]++;
4456 }
584892a6 4457
1e915849
RS
4458 if (mips_opts.mips16)
4459 {
4460 ip->fixed_p = 1;
4461 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4462 }
4463 add_fixed_insn (ip);
252b5132
RH
4464 }
4465
9fe77896 4466 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 4467 {
df58fc94 4468 bfd_reloc_code_real_type final_type[3];
2309ddf2 4469 reloc_howto_type *howto0;
9fe77896
RS
4470 reloc_howto_type *howto;
4471 int i;
34ce925e 4472
df58fc94
RS
4473 /* Perform any necessary conversion to microMIPS relocations
4474 and find out how many relocations there actually are. */
4475 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4476 final_type[i] = micromips_map_reloc (reloc_type[i]);
4477
9fe77896
RS
4478 /* In a compound relocation, it is the final (outermost)
4479 operator that determines the relocated field. */
2309ddf2
MR
4480 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
4481
9fe77896
RS
4482 if (howto == NULL)
4483 {
4484 /* To reproduce this failure try assembling gas/testsuites/
4485 gas/mips/mips16-intermix.s with a mips-ecoff targeted
4486 assembler. */
df58fc94
RS
4487 as_bad (_("Unsupported MIPS relocation number %d"),
4488 final_type[i - 1]);
9fe77896
RS
4489 howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
4490 }
2309ddf2
MR
4491
4492 if (i > 1)
4493 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
4494 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4495 bfd_get_reloc_size (howto),
4496 address_expr,
2309ddf2
MR
4497 howto0 && howto0->pc_relative,
4498 final_type[0]);
9fe77896
RS
4499
4500 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 4501 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
4502 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4503
4504 /* These relocations can have an addend that won't fit in
4505 4 octets for 64bit assembly. */
4506 if (HAVE_64BIT_GPRS
4507 && ! howto->partial_inplace
4508 && (reloc_type[0] == BFD_RELOC_16
4509 || reloc_type[0] == BFD_RELOC_32
4510 || reloc_type[0] == BFD_RELOC_MIPS_JMP
4511 || reloc_type[0] == BFD_RELOC_GPREL16
4512 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4513 || reloc_type[0] == BFD_RELOC_GPREL32
4514 || reloc_type[0] == BFD_RELOC_64
4515 || reloc_type[0] == BFD_RELOC_CTOR
4516 || reloc_type[0] == BFD_RELOC_MIPS_SUB
4517 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4518 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4519 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4520 || reloc_type[0] == BFD_RELOC_MIPS_REL16
4521 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4522 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4523 || hi16_reloc_p (reloc_type[0])
4524 || lo16_reloc_p (reloc_type[0])))
4525 ip->fixp[0]->fx_no_overflow = 1;
4526
ddaf2c41
MR
4527 /* These relocations can have an addend that won't fit in 2 octets. */
4528 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
4529 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
4530 ip->fixp[0]->fx_no_overflow = 1;
4531
9fe77896
RS
4532 if (mips_relax.sequence)
4533 {
4534 if (mips_relax.first_fixup == 0)
4535 mips_relax.first_fixup = ip->fixp[0];
4536 }
4537 else if (reloc_needs_lo_p (*reloc_type))
4538 {
4539 struct mips_hi_fixup *hi_fixup;
4540
4541 /* Reuse the last entry if it already has a matching %lo. */
4542 hi_fixup = mips_hi_fixup_list;
4543 if (hi_fixup == 0
4544 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 4545 {
9fe77896
RS
4546 hi_fixup = ((struct mips_hi_fixup *)
4547 xmalloc (sizeof (struct mips_hi_fixup)));
4548 hi_fixup->next = mips_hi_fixup_list;
4549 mips_hi_fixup_list = hi_fixup;
4d7206a2 4550 }
9fe77896
RS
4551 hi_fixup->fixp = ip->fixp[0];
4552 hi_fixup->seg = now_seg;
4553 }
252b5132 4554
9fe77896
RS
4555 /* Add fixups for the second and third relocations, if given.
4556 Note that the ABI allows the second relocation to be
4557 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
4558 moment we only use RSS_UNDEF, but we could add support
4559 for the others if it ever becomes necessary. */
4560 for (i = 1; i < 3; i++)
4561 if (reloc_type[i] != BFD_RELOC_UNUSED)
4562 {
4563 ip->fixp[i] = fix_new (ip->frag, ip->where,
4564 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 4565 FALSE, final_type[i]);
f6688943 4566
9fe77896
RS
4567 /* Use fx_tcbit to mark compound relocs. */
4568 ip->fixp[0]->fx_tcbit = 1;
4569 ip->fixp[i]->fx_tcbit = 1;
4570 }
252b5132 4571 }
1e915849 4572 install_insn (ip);
252b5132
RH
4573
4574 /* Update the register mask information. */
4c260379
RS
4575 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4576 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 4577
a4e06468 4578 switch (method)
252b5132 4579 {
a4e06468
RS
4580 case APPEND_ADD:
4581 insert_into_history (0, 1, ip);
4582 break;
4583
4584 case APPEND_ADD_WITH_NOP:
14fe068b
RS
4585 {
4586 struct mips_cl_insn *nop;
4587
4588 insert_into_history (0, 1, ip);
4589 nop = get_delay_slot_nop (ip);
4590 add_fixed_insn (nop);
4591 insert_into_history (0, 1, nop);
4592 if (mips_relax.sequence)
4593 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4594 }
a4e06468
RS
4595 break;
4596
4597 case APPEND_ADD_COMPACT:
4598 /* Convert MIPS16 jr/jalr into a "compact" jump. */
4599 gas_assert (mips_opts.mips16);
4600 ip->insn_opcode |= 0x0080;
4601 find_altered_mips16_opcode (ip);
4602 install_insn (ip);
4603 insert_into_history (0, 1, ip);
4604 break;
4605
4606 case APPEND_SWAP:
4607 {
4608 struct mips_cl_insn delay = history[0];
4609 if (mips_opts.mips16)
4610 {
4611 know (delay.frag == ip->frag);
4612 move_insn (ip, delay.frag, delay.where);
4613 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4614 }
464ab0e5 4615 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
4616 {
4617 /* Add the delay slot instruction to the end of the
4618 current frag and shrink the fixed part of the
4619 original frag. If the branch occupies the tail of
4620 the latter, move it backwards to cover the gap. */
2b0c8b40 4621 delay.frag->fr_fix -= branch_disp;
a4e06468 4622 if (delay.frag == ip->frag)
2b0c8b40 4623 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
4624 add_fixed_insn (&delay);
4625 }
4626 else
4627 {
2b0c8b40
MR
4628 move_insn (&delay, ip->frag,
4629 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
4630 move_insn (ip, history[0].frag, history[0].where);
4631 }
4632 history[0] = *ip;
4633 delay.fixed_p = 1;
4634 insert_into_history (0, 1, &delay);
4635 }
4636 break;
252b5132
RH
4637 }
4638
13408f1e 4639 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
4640 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4641 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
4642 {
4643 unsigned int i;
4644
79850f26 4645 mips_no_prev_insn ();
13408f1e 4646
e407c74b 4647 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 4648 history[i].cleared_p = 1;
e407c74b
NC
4649 }
4650
df58fc94
RS
4651 /* We need to emit a label at the end of branch-likely macros. */
4652 if (emit_branch_likely_macro)
4653 {
4654 emit_branch_likely_macro = FALSE;
4655 micromips_add_label ();
4656 }
4657
252b5132
RH
4658 /* We just output an insn, so the next one doesn't have a label. */
4659 mips_clear_insn_labels ();
252b5132
RH
4660}
4661
e407c74b
NC
4662/* Forget that there was any previous instruction or label.
4663 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
4664
4665static void
7d10b47d 4666mips_no_prev_insn (void)
252b5132 4667{
7d10b47d
RS
4668 prev_nop_frag = NULL;
4669 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
4670 mips_clear_insn_labels ();
4671}
4672
7d10b47d
RS
4673/* This function must be called before we emit something other than
4674 instructions. It is like mips_no_prev_insn except that it inserts
4675 any NOPS that might be needed by previous instructions. */
252b5132 4676
7d10b47d
RS
4677void
4678mips_emit_delays (void)
252b5132
RH
4679{
4680 if (! mips_opts.noreorder)
4681 {
932d1a1b 4682 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
4683 if (nops > 0)
4684 {
7d10b47d
RS
4685 while (nops-- > 0)
4686 add_fixed_insn (NOP_INSN);
462427c4 4687 mips_move_text_labels ();
7d10b47d
RS
4688 }
4689 }
4690 mips_no_prev_insn ();
4691}
4692
4693/* Start a (possibly nested) noreorder block. */
4694
4695static void
4696start_noreorder (void)
4697{
4698 if (mips_opts.noreorder == 0)
4699 {
4700 unsigned int i;
4701 int nops;
4702
4703 /* None of the instructions before the .set noreorder can be moved. */
4704 for (i = 0; i < ARRAY_SIZE (history); i++)
4705 history[i].fixed_p = 1;
4706
4707 /* Insert any nops that might be needed between the .set noreorder
4708 block and the previous instructions. We will later remove any
4709 nops that turn out not to be needed. */
932d1a1b 4710 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
4711 if (nops > 0)
4712 {
4713 if (mips_optimize != 0)
252b5132
RH
4714 {
4715 /* Record the frag which holds the nop instructions, so
4716 that we can remove them if we don't need them. */
df58fc94 4717 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
4718 prev_nop_frag = frag_now;
4719 prev_nop_frag_holds = nops;
4720 prev_nop_frag_required = 0;
4721 prev_nop_frag_since = 0;
4722 }
4723
4724 for (; nops > 0; --nops)
1e915849 4725 add_fixed_insn (NOP_INSN);
252b5132 4726
7d10b47d
RS
4727 /* Move on to a new frag, so that it is safe to simply
4728 decrease the size of prev_nop_frag. */
4729 frag_wane (frag_now);
4730 frag_new (0);
462427c4 4731 mips_move_text_labels ();
252b5132 4732 }
df58fc94 4733 mips_mark_labels ();
7d10b47d 4734 mips_clear_insn_labels ();
252b5132 4735 }
7d10b47d
RS
4736 mips_opts.noreorder++;
4737 mips_any_noreorder = 1;
4738}
252b5132 4739
7d10b47d 4740/* End a nested noreorder block. */
252b5132 4741
7d10b47d
RS
4742static void
4743end_noreorder (void)
4744{
4745 mips_opts.noreorder--;
4746 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
4747 {
4748 /* Commit to inserting prev_nop_frag_required nops and go back to
4749 handling nop insertion the .set reorder way. */
4750 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 4751 * NOP_INSN_SIZE);
7d10b47d
RS
4752 insert_into_history (prev_nop_frag_since,
4753 prev_nop_frag_required, NOP_INSN);
4754 prev_nop_frag = NULL;
4755 }
252b5132
RH
4756}
4757
584892a6
RS
4758/* Set up global variables for the start of a new macro. */
4759
4760static void
4761macro_start (void)
4762{
4763 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
4764 memset (&mips_macro_warning.first_insn_sizes, 0,
4765 sizeof (mips_macro_warning.first_insn_sizes));
4766 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 4767 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 4768 && delayed_branch_p (&history[0]));
df58fc94
RS
4769 switch (history[0].insn_mo->pinfo2
4770 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
4771 {
4772 case INSN2_BRANCH_DELAY_32BIT:
4773 mips_macro_warning.delay_slot_length = 4;
4774 break;
4775 case INSN2_BRANCH_DELAY_16BIT:
4776 mips_macro_warning.delay_slot_length = 2;
4777 break;
4778 default:
4779 mips_macro_warning.delay_slot_length = 0;
4780 break;
4781 }
4782 mips_macro_warning.first_frag = NULL;
584892a6
RS
4783}
4784
df58fc94
RS
4785/* Given that a macro is longer than one instruction or of the wrong size,
4786 return the appropriate warning for it. Return null if no warning is
4787 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
4788 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
4789 and RELAX_NOMACRO. */
584892a6
RS
4790
4791static const char *
4792macro_warning (relax_substateT subtype)
4793{
4794 if (subtype & RELAX_DELAY_SLOT)
4795 return _("Macro instruction expanded into multiple instructions"
4796 " in a branch delay slot");
4797 else if (subtype & RELAX_NOMACRO)
4798 return _("Macro instruction expanded into multiple instructions");
df58fc94
RS
4799 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
4800 | RELAX_DELAY_SLOT_SIZE_SECOND))
4801 return ((subtype & RELAX_DELAY_SLOT_16BIT)
4802 ? _("Macro instruction expanded into a wrong size instruction"
4803 " in a 16-bit branch delay slot")
4804 : _("Macro instruction expanded into a wrong size instruction"
4805 " in a 32-bit branch delay slot"));
584892a6
RS
4806 else
4807 return 0;
4808}
4809
4810/* Finish up a macro. Emit warnings as appropriate. */
4811
4812static void
4813macro_end (void)
4814{
df58fc94
RS
4815 /* Relaxation warning flags. */
4816 relax_substateT subtype = 0;
4817
4818 /* Check delay slot size requirements. */
4819 if (mips_macro_warning.delay_slot_length == 2)
4820 subtype |= RELAX_DELAY_SLOT_16BIT;
4821 if (mips_macro_warning.delay_slot_length != 0)
584892a6 4822 {
df58fc94
RS
4823 if (mips_macro_warning.delay_slot_length
4824 != mips_macro_warning.first_insn_sizes[0])
4825 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
4826 if (mips_macro_warning.delay_slot_length
4827 != mips_macro_warning.first_insn_sizes[1])
4828 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
4829 }
584892a6 4830
df58fc94
RS
4831 /* Check instruction count requirements. */
4832 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
4833 {
4834 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
4835 subtype |= RELAX_SECOND_LONGER;
4836 if (mips_opts.warn_about_macros)
4837 subtype |= RELAX_NOMACRO;
4838 if (mips_macro_warning.delay_slot_p)
4839 subtype |= RELAX_DELAY_SLOT;
df58fc94 4840 }
584892a6 4841
df58fc94
RS
4842 /* If both alternatives fail to fill a delay slot correctly,
4843 emit the warning now. */
4844 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
4845 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
4846 {
4847 relax_substateT s;
4848 const char *msg;
4849
4850 s = subtype & (RELAX_DELAY_SLOT_16BIT
4851 | RELAX_DELAY_SLOT_SIZE_FIRST
4852 | RELAX_DELAY_SLOT_SIZE_SECOND);
4853 msg = macro_warning (s);
4854 if (msg != NULL)
4855 as_warn ("%s", msg);
4856 subtype &= ~s;
4857 }
4858
4859 /* If both implementations are longer than 1 instruction, then emit the
4860 warning now. */
4861 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
4862 {
4863 relax_substateT s;
4864 const char *msg;
4865
4866 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
4867 msg = macro_warning (s);
4868 if (msg != NULL)
4869 as_warn ("%s", msg);
4870 subtype &= ~s;
584892a6 4871 }
df58fc94
RS
4872
4873 /* If any flags still set, then one implementation might need a warning
4874 and the other either will need one of a different kind or none at all.
4875 Pass any remaining flags over to relaxation. */
4876 if (mips_macro_warning.first_frag != NULL)
4877 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
4878}
4879
df58fc94
RS
4880/* Instruction operand formats used in macros that vary between
4881 standard MIPS and microMIPS code. */
4882
4883static const char * const brk_fmt[2] = { "c", "mF" };
4884static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
4885static const char * const jalr_fmt[2] = { "d,s", "t,s" };
4886static const char * const lui_fmt[2] = { "t,u", "s,u" };
4887static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
4888static const char * const mfhl_fmt[2] = { "d", "mj" };
4889static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
4890static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
4891
4892#define BRK_FMT (brk_fmt[mips_opts.micromips])
4893#define COP12_FMT (cop12_fmt[mips_opts.micromips])
4894#define JALR_FMT (jalr_fmt[mips_opts.micromips])
4895#define LUI_FMT (lui_fmt[mips_opts.micromips])
4896#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
4897#define MFHL_FMT (mfhl_fmt[mips_opts.micromips])
4898#define SHFT_FMT (shft_fmt[mips_opts.micromips])
4899#define TRAP_FMT (trap_fmt[mips_opts.micromips])
4900
6e1304d8
RS
4901/* Read a macro's relocation codes from *ARGS and store them in *R.
4902 The first argument in *ARGS will be either the code for a single
4903 relocation or -1 followed by the three codes that make up a
4904 composite relocation. */
4905
4906static void
4907macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
4908{
4909 int i, next;
4910
4911 next = va_arg (*args, int);
4912 if (next >= 0)
4913 r[0] = (bfd_reloc_code_real_type) next;
4914 else
4915 for (i = 0; i < 3; i++)
4916 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
4917}
4918
252b5132
RH
4919/* Build an instruction created by a macro expansion. This is passed
4920 a pointer to the count of instructions created so far, an
4921 expression, the name of the instruction to build, an operand format
4922 string, and corresponding arguments. */
4923
252b5132 4924static void
67c0d1eb 4925macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 4926{
df58fc94 4927 const struct mips_opcode *mo = NULL;
f6688943 4928 bfd_reloc_code_real_type r[3];
df58fc94
RS
4929 const struct mips_opcode *amo;
4930 struct hash_control *hash;
4931 struct mips_cl_insn insn;
252b5132 4932 va_list args;
252b5132 4933
252b5132 4934 va_start (args, fmt);
252b5132 4935
252b5132
RH
4936 if (mips_opts.mips16)
4937 {
03ea81db 4938 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
4939 va_end (args);
4940 return;
4941 }
4942
f6688943
TS
4943 r[0] = BFD_RELOC_UNUSED;
4944 r[1] = BFD_RELOC_UNUSED;
4945 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
4946 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
4947 amo = (struct mips_opcode *) hash_find (hash, name);
4948 gas_assert (amo);
4949 gas_assert (strcmp (name, amo->name) == 0);
1e915849 4950
df58fc94 4951 do
8b082fb1
TS
4952 {
4953 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
4954 macros will never generate MDMX, MIPS-3D, or MT instructions.
4955 We try to match an instruction that fulfils the branch delay
4956 slot instruction length requirement (if any) of the previous
4957 instruction. While doing this we record the first instruction
4958 seen that matches all the other conditions and use it anyway
4959 if the requirement cannot be met; we will issue an appropriate
4960 warning later on. */
4961 if (strcmp (fmt, amo->args) == 0
4962 && amo->pinfo != INSN_MACRO
4963 && is_opcode_valid (amo)
4964 && is_size_valid (amo))
4965 {
4966 if (is_delay_slot_valid (amo))
4967 {
4968 mo = amo;
4969 break;
4970 }
4971 else if (!mo)
4972 mo = amo;
4973 }
8b082fb1 4974
df58fc94
RS
4975 ++amo;
4976 gas_assert (amo->name);
252b5132 4977 }
df58fc94 4978 while (strcmp (name, amo->name) == 0);
252b5132 4979
df58fc94 4980 gas_assert (mo);
1e915849 4981 create_insn (&insn, mo);
252b5132
RH
4982 for (;;)
4983 {
4984 switch (*fmt++)
4985 {
4986 case '\0':
4987 break;
4988
4989 case ',':
4990 case '(':
4991 case ')':
4992 continue;
4993
5f74bc13
CD
4994 case '+':
4995 switch (*fmt++)
4996 {
4997 case 'A':
4998 case 'E':
df58fc94
RS
4999 INSERT_OPERAND (mips_opts.micromips,
5000 EXTLSB, insn, va_arg (args, int));
5f74bc13
CD
5001 continue;
5002
5003 case 'B':
5004 case 'F':
5005 /* Note that in the macro case, these arguments are already
5006 in MSB form. (When handling the instruction in the
5007 non-macro case, these arguments are sizes from which
5008 MSB values must be calculated.) */
df58fc94
RS
5009 INSERT_OPERAND (mips_opts.micromips,
5010 INSMSB, insn, va_arg (args, int));
5f74bc13
CD
5011 continue;
5012
b015e599
AP
5013 case 'J':
5014 gas_assert (!mips_opts.micromips);
5015 INSERT_OPERAND (0, CODE10, insn, va_arg (args, int));
5016 continue;
5017
5f74bc13
CD
5018 case 'C':
5019 case 'G':
5020 case 'H':
5021 /* Note that in the macro case, these arguments are already
5022 in MSBD form. (When handling the instruction in the
5023 non-macro case, these arguments are sizes from which
5024 MSBD values must be calculated.) */
df58fc94
RS
5025 INSERT_OPERAND (mips_opts.micromips,
5026 EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
5027 continue;
5028
dd3cbb7e 5029 case 'Q':
df58fc94
RS
5030 gas_assert (!mips_opts.micromips);
5031 INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
dd3cbb7e
NC
5032 continue;
5033
7f3c4072
CM
5034 case 'j':
5035 INSERT_OPERAND (mips_opts.micromips, EVAOFFSET, insn, va_arg (args, int));
5036 continue;
5037
5f74bc13 5038 default:
b37df7c4 5039 abort ();
5f74bc13
CD
5040 }
5041 continue;
5042
8b082fb1 5043 case '2':
03f66e8a 5044 INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
8b082fb1
TS
5045 continue;
5046
df58fc94
RS
5047 case 'n':
5048 gas_assert (mips_opts.micromips);
252b5132
RH
5049 case 't':
5050 case 'w':
5051 case 'E':
df58fc94 5052 INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
252b5132
RH
5053 continue;
5054
5055 case 'c':
df58fc94
RS
5056 gas_assert (!mips_opts.micromips);
5057 INSERT_OPERAND (0, CODE, insn, va_arg (args, int));
38487616
TS
5058 continue;
5059
252b5132 5060 case 'W':
df58fc94
RS
5061 gas_assert (!mips_opts.micromips);
5062 case 'T':
5063 INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
252b5132
RH
5064 continue;
5065
252b5132 5066 case 'G':
df58fc94
RS
5067 if (mips_opts.micromips)
5068 INSERT_OPERAND (1, RS, insn, va_arg (args, int));
5069 else
5070 INSERT_OPERAND (0, RD, insn, va_arg (args, int));
5071 continue;
5072
af7ee8bf 5073 case 'K':
df58fc94
RS
5074 gas_assert (!mips_opts.micromips);
5075 case 'd':
5076 INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
252b5132
RH
5077 continue;
5078
4372b673 5079 case 'U':
df58fc94 5080 gas_assert (!mips_opts.micromips);
4372b673
NC
5081 {
5082 int tmp = va_arg (args, int);
5083
df58fc94
RS
5084 INSERT_OPERAND (0, RT, insn, tmp);
5085 INSERT_OPERAND (0, RD, insn, tmp);
4372b673 5086 }
df58fc94 5087 continue;
4372b673 5088
252b5132
RH
5089 case 'V':
5090 case 'S':
df58fc94
RS
5091 gas_assert (!mips_opts.micromips);
5092 INSERT_OPERAND (0, FS, insn, va_arg (args, int));
252b5132
RH
5093 continue;
5094
5095 case 'z':
5096 continue;
5097
5098 case '<':
df58fc94
RS
5099 INSERT_OPERAND (mips_opts.micromips,
5100 SHAMT, insn, va_arg (args, int));
252b5132
RH
5101 continue;
5102
5103 case 'D':
df58fc94
RS
5104 gas_assert (!mips_opts.micromips);
5105 INSERT_OPERAND (0, FD, insn, va_arg (args, int));
252b5132
RH
5106 continue;
5107
5108 case 'B':
df58fc94
RS
5109 gas_assert (!mips_opts.micromips);
5110 INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
252b5132
RH
5111 continue;
5112
4372b673 5113 case 'J':
df58fc94
RS
5114 gas_assert (!mips_opts.micromips);
5115 INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
4372b673
NC
5116 continue;
5117
252b5132 5118 case 'q':
df58fc94
RS
5119 gas_assert (!mips_opts.micromips);
5120 INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
252b5132
RH
5121 continue;
5122
5123 case 'b':
5124 case 's':
5125 case 'r':
5126 case 'v':
df58fc94 5127 INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
252b5132
RH
5128 continue;
5129
5130 case 'i':
5131 case 'j':
6e1304d8 5132 macro_read_relocs (&args, r);
9c2799c2 5133 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
5134 || *r == BFD_RELOC_MIPS_HIGHER
5135 || *r == BFD_RELOC_HI16_S
5136 || *r == BFD_RELOC_LO16
5137 || *r == BFD_RELOC_MIPS_GOT_OFST);
5138 continue;
5139
5140 case 'o':
5141 macro_read_relocs (&args, r);
252b5132
RH
5142 continue;
5143
5144 case 'u':
6e1304d8 5145 macro_read_relocs (&args, r);
9c2799c2 5146 gas_assert (ep != NULL
90ecf173
MR
5147 && (ep->X_op == O_constant
5148 || (ep->X_op == O_symbol
5149 && (*r == BFD_RELOC_MIPS_HIGHEST
5150 || *r == BFD_RELOC_HI16_S
5151 || *r == BFD_RELOC_HI16
5152 || *r == BFD_RELOC_GPREL16
5153 || *r == BFD_RELOC_MIPS_GOT_HI16
5154 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
5155 continue;
5156
5157 case 'p':
9c2799c2 5158 gas_assert (ep != NULL);
bad36eac 5159
252b5132
RH
5160 /*
5161 * This allows macro() to pass an immediate expression for
5162 * creating short branches without creating a symbol.
bad36eac
DJ
5163 *
5164 * We don't allow branch relaxation for these branches, as
5165 * they should only appear in ".set nomacro" anyway.
252b5132
RH
5166 */
5167 if (ep->X_op == O_constant)
5168 {
df58fc94
RS
5169 /* For microMIPS we always use relocations for branches.
5170 So we should not resolve immediate values. */
5171 gas_assert (!mips_opts.micromips);
5172
bad36eac
DJ
5173 if ((ep->X_add_number & 3) != 0)
5174 as_bad (_("branch to misaligned address (0x%lx)"),
5175 (unsigned long) ep->X_add_number);
5176 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5177 as_bad (_("branch address range overflow (0x%lx)"),
5178 (unsigned long) ep->X_add_number);
252b5132
RH
5179 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5180 ep = NULL;
5181 }
5182 else
0b25d3e6 5183 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
5184 continue;
5185
5186 case 'a':
9c2799c2 5187 gas_assert (ep != NULL);
f6688943 5188 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
5189 continue;
5190
5191 case 'C':
df58fc94
RS
5192 gas_assert (!mips_opts.micromips);
5193 INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
252b5132
RH
5194 continue;
5195
d43b4baf 5196 case 'k':
df58fc94
RS
5197 INSERT_OPERAND (mips_opts.micromips,
5198 CACHE, insn, va_arg (args, unsigned long));
5199 continue;
5200
5201 case '|':
5202 gas_assert (mips_opts.micromips);
5203 INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5204 continue;
5205
5206 case '.':
5207 gas_assert (mips_opts.micromips);
5208 INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5209 continue;
5210
dec0624d
MR
5211 case '\\':
5212 INSERT_OPERAND (mips_opts.micromips,
5213 3BITPOS, insn, va_arg (args, unsigned int));
5214 continue;
5215
df58fc94 5216 case '~':
dec0624d
MR
5217 INSERT_OPERAND (mips_opts.micromips,
5218 OFFSET12, insn, va_arg (args, unsigned long));
df58fc94
RS
5219 continue;
5220
5221 case 'N':
5222 gas_assert (mips_opts.micromips);
5223 INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5224 continue;
5225
5226 case 'm': /* Opcode extension character. */
5227 gas_assert (mips_opts.micromips);
5228 switch (*fmt++)
5229 {
5230 case 'j':
5231 INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5232 break;
5233
5234 case 'p':
5235 INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5236 break;
5237
5238 case 'F':
5239 INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5240 break;
5241
5242 default:
b37df7c4 5243 abort ();
df58fc94 5244 }
d43b4baf
TS
5245 continue;
5246
252b5132 5247 default:
b37df7c4 5248 abort ();
252b5132
RH
5249 }
5250 break;
5251 }
5252 va_end (args);
9c2799c2 5253 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5254
df58fc94 5255 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5256}
5257
5258static void
67c0d1eb 5259mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 5260 va_list *args)
252b5132 5261{
1e915849 5262 struct mips_opcode *mo;
252b5132 5263 struct mips_cl_insn insn;
f6688943
TS
5264 bfd_reloc_code_real_type r[3]
5265 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 5266
1e915849 5267 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
5268 gas_assert (mo);
5269 gas_assert (strcmp (name, mo->name) == 0);
252b5132 5270
1e915849 5271 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 5272 {
1e915849 5273 ++mo;
9c2799c2
NC
5274 gas_assert (mo->name);
5275 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
5276 }
5277
1e915849 5278 create_insn (&insn, mo);
252b5132
RH
5279 for (;;)
5280 {
5281 int c;
5282
5283 c = *fmt++;
5284 switch (c)
5285 {
5286 case '\0':
5287 break;
5288
5289 case ',':
5290 case '(':
5291 case ')':
5292 continue;
5293
5294 case 'y':
5295 case 'w':
03ea81db 5296 MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
252b5132
RH
5297 continue;
5298
5299 case 'x':
5300 case 'v':
03ea81db 5301 MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
252b5132
RH
5302 continue;
5303
5304 case 'z':
03ea81db 5305 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
252b5132
RH
5306 continue;
5307
5308 case 'Z':
03ea81db 5309 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
252b5132
RH
5310 continue;
5311
5312 case '0':
5313 case 'S':
5314 case 'P':
5315 case 'R':
5316 continue;
5317
5318 case 'X':
03ea81db 5319 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
252b5132
RH
5320 continue;
5321
5322 case 'Y':
5323 {
5324 int regno;
5325
03ea81db 5326 regno = va_arg (*args, int);
252b5132 5327 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
a9e24354 5328 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
252b5132
RH
5329 }
5330 continue;
5331
5332 case '<':
5333 case '>':
5334 case '4':
5335 case '5':
5336 case 'H':
5337 case 'W':
5338 case 'D':
5339 case 'j':
5340 case '8':
5341 case 'V':
5342 case 'C':
5343 case 'U':
5344 case 'k':
5345 case 'K':
5346 case 'p':
5347 case 'q':
5348 {
b886a2ab
RS
5349 offsetT value;
5350
9c2799c2 5351 gas_assert (ep != NULL);
252b5132
RH
5352
5353 if (ep->X_op != O_constant)
874e8986 5354 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 5355 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 5356 {
b886a2ab 5357 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 5358 ep = NULL;
f6688943 5359 *r = BFD_RELOC_UNUSED;
252b5132
RH
5360 }
5361 }
5362 continue;
5363
5364 case '6':
03ea81db 5365 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
252b5132
RH
5366 continue;
5367 }
5368
5369 break;
5370 }
5371
9c2799c2 5372 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5373
df58fc94 5374 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5375}
5376
2051e8c4
MR
5377/*
5378 * Sign-extend 32-bit mode constants that have bit 31 set and all
5379 * higher bits unset.
5380 */
9f872bbe 5381static void
2051e8c4
MR
5382normalize_constant_expr (expressionS *ex)
5383{
9ee2a2d4 5384 if (ex->X_op == O_constant
2051e8c4
MR
5385 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5386 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5387 - 0x80000000);
5388}
5389
5390/*
5391 * Sign-extend 32-bit mode address offsets that have bit 31 set and
5392 * all higher bits unset.
5393 */
5394static void
5395normalize_address_expr (expressionS *ex)
5396{
5397 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5398 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5399 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5400 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5401 - 0x80000000);
5402}
5403
438c16b8
TS
5404/*
5405 * Generate a "jalr" instruction with a relocation hint to the called
5406 * function. This occurs in NewABI PIC code.
5407 */
5408static void
df58fc94 5409macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 5410{
df58fc94
RS
5411 static const bfd_reloc_code_real_type jalr_relocs[2]
5412 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5413 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5414 const char *jalr;
685736be 5415 char *f = NULL;
b34976b6 5416
1180b5a4 5417 if (MIPS_JALR_HINT_P (ep))
f21f8242 5418 {
cc3d92a5 5419 frag_grow (8);
f21f8242
AO
5420 f = frag_more (0);
5421 }
2906b037 5422 if (mips_opts.micromips)
df58fc94
RS
5423 {
5424 jalr = mips_opts.noreorder && !cprestore ? "jalr" : "jalrs";
e64af278
MR
5425 if (MIPS_JALR_HINT_P (ep)
5426 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
5427 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5428 else
5429 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5430 }
2906b037
MR
5431 else
5432 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 5433 if (MIPS_JALR_HINT_P (ep))
df58fc94 5434 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
5435}
5436
252b5132
RH
5437/*
5438 * Generate a "lui" instruction.
5439 */
5440static void
67c0d1eb 5441macro_build_lui (expressionS *ep, int regnum)
252b5132 5442{
9c2799c2 5443 gas_assert (! mips_opts.mips16);
252b5132 5444
df58fc94 5445 if (ep->X_op != O_constant)
252b5132 5446 {
9c2799c2 5447 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
5448 /* _gp_disp is a special case, used from s_cpload.
5449 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 5450 gas_assert (mips_pic == NO_PIC
78e1bb40 5451 || (! HAVE_NEWABI
aa6975fb
ILT
5452 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5453 || (! mips_in_shared
bbe506e8
TS
5454 && strcmp (S_GET_NAME (ep->X_add_symbol),
5455 "__gnu_local_gp") == 0));
252b5132
RH
5456 }
5457
df58fc94 5458 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
5459}
5460
885add95
CD
5461/* Generate a sequence of instructions to do a load or store from a constant
5462 offset off of a base register (breg) into/from a target register (treg),
5463 using AT if necessary. */
5464static void
67c0d1eb
RS
5465macro_build_ldst_constoffset (expressionS *ep, const char *op,
5466 int treg, int breg, int dbl)
885add95 5467{
9c2799c2 5468 gas_assert (ep->X_op == O_constant);
885add95 5469
256ab948 5470 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5471 if (!dbl)
5472 normalize_constant_expr (ep);
256ab948 5473
67c1ffbe 5474 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 5475 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
5476 as_warn (_("operand overflow"));
5477
5478 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5479 {
5480 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 5481 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
5482 }
5483 else
5484 {
5485 /* 32-bit offset, need multiple instructions and AT, like:
5486 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
5487 addu $tempreg,$tempreg,$breg
5488 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
5489 to handle the complete offset. */
67c0d1eb
RS
5490 macro_build_lui (ep, AT);
5491 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5492 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 5493
741fe287 5494 if (!mips_opts.at)
8fc2e39e 5495 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
5496 }
5497}
5498
252b5132
RH
5499/* set_at()
5500 * Generates code to set the $at register to true (one)
5501 * if reg is less than the immediate expression.
5502 */
5503static void
67c0d1eb 5504set_at (int reg, int unsignedp)
252b5132
RH
5505{
5506 if (imm_expr.X_op == O_constant
5507 && imm_expr.X_add_number >= -0x8000
5508 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
5509 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5510 AT, reg, BFD_RELOC_LO16);
252b5132
RH
5511 else
5512 {
67c0d1eb
RS
5513 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5514 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
5515 }
5516}
5517
5518/* Warn if an expression is not a constant. */
5519
5520static void
17a2f251 5521check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
5522{
5523 if (ex->X_op == O_big)
5524 as_bad (_("unsupported large constant"));
5525 else if (ex->X_op != O_constant)
9ee2a2d4
MR
5526 as_bad (_("Instruction %s requires absolute expression"),
5527 ip->insn_mo->name);
13757d0c 5528
9ee2a2d4
MR
5529 if (HAVE_32BIT_GPRS)
5530 normalize_constant_expr (ex);
252b5132
RH
5531}
5532
5533/* Count the leading zeroes by performing a binary chop. This is a
5534 bulky bit of source, but performance is a LOT better for the
5535 majority of values than a simple loop to count the bits:
5536 for (lcnt = 0; (lcnt < 32); lcnt++)
5537 if ((v) & (1 << (31 - lcnt)))
5538 break;
5539 However it is not code size friendly, and the gain will drop a bit
5540 on certain cached systems.
5541*/
5542#define COUNT_TOP_ZEROES(v) \
5543 (((v) & ~0xffff) == 0 \
5544 ? ((v) & ~0xff) == 0 \
5545 ? ((v) & ~0xf) == 0 \
5546 ? ((v) & ~0x3) == 0 \
5547 ? ((v) & ~0x1) == 0 \
5548 ? !(v) \
5549 ? 32 \
5550 : 31 \
5551 : 30 \
5552 : ((v) & ~0x7) == 0 \
5553 ? 29 \
5554 : 28 \
5555 : ((v) & ~0x3f) == 0 \
5556 ? ((v) & ~0x1f) == 0 \
5557 ? 27 \
5558 : 26 \
5559 : ((v) & ~0x7f) == 0 \
5560 ? 25 \
5561 : 24 \
5562 : ((v) & ~0xfff) == 0 \
5563 ? ((v) & ~0x3ff) == 0 \
5564 ? ((v) & ~0x1ff) == 0 \
5565 ? 23 \
5566 : 22 \
5567 : ((v) & ~0x7ff) == 0 \
5568 ? 21 \
5569 : 20 \
5570 : ((v) & ~0x3fff) == 0 \
5571 ? ((v) & ~0x1fff) == 0 \
5572 ? 19 \
5573 : 18 \
5574 : ((v) & ~0x7fff) == 0 \
5575 ? 17 \
5576 : 16 \
5577 : ((v) & ~0xffffff) == 0 \
5578 ? ((v) & ~0xfffff) == 0 \
5579 ? ((v) & ~0x3ffff) == 0 \
5580 ? ((v) & ~0x1ffff) == 0 \
5581 ? 15 \
5582 : 14 \
5583 : ((v) & ~0x7ffff) == 0 \
5584 ? 13 \
5585 : 12 \
5586 : ((v) & ~0x3fffff) == 0 \
5587 ? ((v) & ~0x1fffff) == 0 \
5588 ? 11 \
5589 : 10 \
5590 : ((v) & ~0x7fffff) == 0 \
5591 ? 9 \
5592 : 8 \
5593 : ((v) & ~0xfffffff) == 0 \
5594 ? ((v) & ~0x3ffffff) == 0 \
5595 ? ((v) & ~0x1ffffff) == 0 \
5596 ? 7 \
5597 : 6 \
5598 : ((v) & ~0x7ffffff) == 0 \
5599 ? 5 \
5600 : 4 \
5601 : ((v) & ~0x3fffffff) == 0 \
5602 ? ((v) & ~0x1fffffff) == 0 \
5603 ? 3 \
5604 : 2 \
5605 : ((v) & ~0x7fffffff) == 0 \
5606 ? 1 \
5607 : 0)
5608
5609/* load_register()
67c1ffbe 5610 * This routine generates the least number of instructions necessary to load
252b5132
RH
5611 * an absolute expression value into a register.
5612 */
5613static void
67c0d1eb 5614load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
5615{
5616 int freg;
5617 expressionS hi32, lo32;
5618
5619 if (ep->X_op != O_big)
5620 {
9c2799c2 5621 gas_assert (ep->X_op == O_constant);
256ab948
TS
5622
5623 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5624 if (!dbl)
5625 normalize_constant_expr (ep);
256ab948
TS
5626
5627 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
5628 {
5629 /* We can handle 16 bit signed values with an addiu to
5630 $zero. No need to ever use daddiu here, since $zero and
5631 the result are always correct in 32 bit mode. */
67c0d1eb 5632 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5633 return;
5634 }
5635 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5636 {
5637 /* We can handle 16 bit unsigned values with an ori to
5638 $zero. */
67c0d1eb 5639 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5640 return;
5641 }
256ab948 5642 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
5643 {
5644 /* 32 bit values require an lui. */
df58fc94 5645 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 5646 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 5647 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
5648 return;
5649 }
5650 }
5651
5652 /* The value is larger than 32 bits. */
5653
2051e8c4 5654 if (!dbl || HAVE_32BIT_GPRS)
252b5132 5655 {
55e08f71
NC
5656 char value[32];
5657
5658 sprintf_vma (value, ep->X_add_number);
20e1fcfd 5659 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 5660 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5661 return;
5662 }
5663
5664 if (ep->X_op != O_big)
5665 {
5666 hi32 = *ep;
5667 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5668 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5669 hi32.X_add_number &= 0xffffffff;
5670 lo32 = *ep;
5671 lo32.X_add_number &= 0xffffffff;
5672 }
5673 else
5674 {
9c2799c2 5675 gas_assert (ep->X_add_number > 2);
252b5132
RH
5676 if (ep->X_add_number == 3)
5677 generic_bignum[3] = 0;
5678 else if (ep->X_add_number > 4)
5679 as_bad (_("Number larger than 64 bits"));
5680 lo32.X_op = O_constant;
5681 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
5682 hi32.X_op = O_constant;
5683 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
5684 }
5685
5686 if (hi32.X_add_number == 0)
5687 freg = 0;
5688 else
5689 {
5690 int shift, bit;
5691 unsigned long hi, lo;
5692
956cd1d6 5693 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
5694 {
5695 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
5696 {
67c0d1eb 5697 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
5698 return;
5699 }
5700 if (lo32.X_add_number & 0x80000000)
5701 {
df58fc94 5702 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 5703 if (lo32.X_add_number & 0xffff)
67c0d1eb 5704 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
5705 return;
5706 }
5707 }
252b5132
RH
5708
5709 /* Check for 16bit shifted constant. We know that hi32 is
5710 non-zero, so start the mask on the first bit of the hi32
5711 value. */
5712 shift = 17;
5713 do
beae10d5
KH
5714 {
5715 unsigned long himask, lomask;
5716
5717 if (shift < 32)
5718 {
5719 himask = 0xffff >> (32 - shift);
5720 lomask = (0xffff << shift) & 0xffffffff;
5721 }
5722 else
5723 {
5724 himask = 0xffff << (shift - 32);
5725 lomask = 0;
5726 }
5727 if ((hi32.X_add_number & ~(offsetT) himask) == 0
5728 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
5729 {
5730 expressionS tmp;
5731
5732 tmp.X_op = O_constant;
5733 if (shift < 32)
5734 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
5735 | (lo32.X_add_number >> shift));
5736 else
5737 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 5738 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 5739 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 5740 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
5741 return;
5742 }
f9419b05 5743 ++shift;
beae10d5
KH
5744 }
5745 while (shift <= (64 - 16));
252b5132
RH
5746
5747 /* Find the bit number of the lowest one bit, and store the
5748 shifted value in hi/lo. */
5749 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
5750 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
5751 if (lo != 0)
5752 {
5753 bit = 0;
5754 while ((lo & 1) == 0)
5755 {
5756 lo >>= 1;
5757 ++bit;
5758 }
5759 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
5760 hi >>= bit;
5761 }
5762 else
5763 {
5764 bit = 32;
5765 while ((hi & 1) == 0)
5766 {
5767 hi >>= 1;
5768 ++bit;
5769 }
5770 lo = hi;
5771 hi = 0;
5772 }
5773
5774 /* Optimize if the shifted value is a (power of 2) - 1. */
5775 if ((hi == 0 && ((lo + 1) & lo) == 0)
5776 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
5777 {
5778 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 5779 if (shift != 0)
beae10d5 5780 {
252b5132
RH
5781 expressionS tmp;
5782
5783 /* This instruction will set the register to be all
5784 ones. */
beae10d5
KH
5785 tmp.X_op = O_constant;
5786 tmp.X_add_number = (offsetT) -1;
67c0d1eb 5787 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
5788 if (bit != 0)
5789 {
5790 bit += shift;
df58fc94 5791 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 5792 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 5793 }
df58fc94 5794 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 5795 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
5796 return;
5797 }
5798 }
252b5132
RH
5799
5800 /* Sign extend hi32 before calling load_register, because we can
5801 generally get better code when we load a sign extended value. */
5802 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 5803 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 5804 load_register (reg, &hi32, 0);
252b5132
RH
5805 freg = reg;
5806 }
5807 if ((lo32.X_add_number & 0xffff0000) == 0)
5808 {
5809 if (freg != 0)
5810 {
df58fc94 5811 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
5812 freg = reg;
5813 }
5814 }
5815 else
5816 {
5817 expressionS mid16;
5818
956cd1d6 5819 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 5820 {
df58fc94
RS
5821 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5822 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
5823 return;
5824 }
252b5132
RH
5825
5826 if (freg != 0)
5827 {
df58fc94 5828 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
5829 freg = reg;
5830 }
5831 mid16 = lo32;
5832 mid16.X_add_number >>= 16;
67c0d1eb 5833 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 5834 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
5835 freg = reg;
5836 }
5837 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 5838 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
5839}
5840
269137b2
TS
5841static inline void
5842load_delay_nop (void)
5843{
5844 if (!gpr_interlocks)
5845 macro_build (NULL, "nop", "");
5846}
5847
252b5132
RH
5848/* Load an address into a register. */
5849
5850static void
67c0d1eb 5851load_address (int reg, expressionS *ep, int *used_at)
252b5132 5852{
252b5132
RH
5853 if (ep->X_op != O_constant
5854 && ep->X_op != O_symbol)
5855 {
5856 as_bad (_("expression too complex"));
5857 ep->X_op = O_constant;
5858 }
5859
5860 if (ep->X_op == O_constant)
5861 {
67c0d1eb 5862 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
5863 return;
5864 }
5865
5866 if (mips_pic == NO_PIC)
5867 {
5868 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 5869 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5870 Otherwise we want
5871 lui $reg,<sym> (BFD_RELOC_HI16_S)
5872 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 5873 If we have an addend, we always use the latter form.
76b3015f 5874
d6bc6245
TS
5875 With 64bit address space and a usable $at we want
5876 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5877 lui $at,<sym> (BFD_RELOC_HI16_S)
5878 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5879 daddiu $at,<sym> (BFD_RELOC_LO16)
5880 dsll32 $reg,0
3a482fd5 5881 daddu $reg,$reg,$at
76b3015f 5882
c03099e6 5883 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
5884 on superscalar processors.
5885 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5886 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
5887 dsll $reg,16
5888 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
5889 dsll $reg,16
5890 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
5891
5892 For GP relative symbols in 64bit address space we can use
5893 the same sequence as in 32bit address space. */
aed1a261 5894 if (HAVE_64BIT_SYMBOLS)
d6bc6245 5895 {
6caf9ef4
TS
5896 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5897 && !nopic_need_relax (ep->X_add_symbol, 1))
5898 {
5899 relax_start (ep->X_add_symbol);
5900 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5901 mips_gp_register, BFD_RELOC_GPREL16);
5902 relax_switch ();
5903 }
d6bc6245 5904
741fe287 5905 if (*used_at == 0 && mips_opts.at)
d6bc6245 5906 {
df58fc94
RS
5907 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5908 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
5909 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5910 BFD_RELOC_MIPS_HIGHER);
5911 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 5912 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 5913 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
5914 *used_at = 1;
5915 }
5916 else
5917 {
df58fc94 5918 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
5919 macro_build (ep, "daddiu", "t,r,j", reg, reg,
5920 BFD_RELOC_MIPS_HIGHER);
df58fc94 5921 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 5922 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 5923 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 5924 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 5925 }
6caf9ef4
TS
5926
5927 if (mips_relax.sequence)
5928 relax_end ();
d6bc6245 5929 }
252b5132
RH
5930 else
5931 {
d6bc6245 5932 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 5933 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 5934 {
4d7206a2 5935 relax_start (ep->X_add_symbol);
67c0d1eb 5936 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 5937 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5938 relax_switch ();
d6bc6245 5939 }
67c0d1eb
RS
5940 macro_build_lui (ep, reg);
5941 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
5942 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
5943 if (mips_relax.sequence)
5944 relax_end ();
d6bc6245 5945 }
252b5132 5946 }
0a44bf69 5947 else if (!mips_big_got)
252b5132
RH
5948 {
5949 expressionS ex;
5950
5951 /* If this is a reference to an external symbol, we want
5952 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5953 Otherwise we want
5954 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5955 nop
5956 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
5957 If there is a constant, it must be added in after.
5958
ed6fb7bd 5959 If we have NewABI, we want
f5040a92
AO
5960 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5961 unless we're referencing a global symbol with a non-zero
5962 offset, in which case cst must be added separately. */
ed6fb7bd
SC
5963 if (HAVE_NEWABI)
5964 {
f5040a92
AO
5965 if (ep->X_add_number)
5966 {
4d7206a2 5967 ex.X_add_number = ep->X_add_number;
f5040a92 5968 ep->X_add_number = 0;
4d7206a2 5969 relax_start (ep->X_add_symbol);
67c0d1eb
RS
5970 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5971 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5972 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5973 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5974 ex.X_op = O_constant;
67c0d1eb 5975 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5976 reg, reg, BFD_RELOC_LO16);
f5040a92 5977 ep->X_add_number = ex.X_add_number;
4d7206a2 5978 relax_switch ();
f5040a92 5979 }
67c0d1eb 5980 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 5981 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
5982 if (mips_relax.sequence)
5983 relax_end ();
ed6fb7bd
SC
5984 }
5985 else
5986 {
f5040a92
AO
5987 ex.X_add_number = ep->X_add_number;
5988 ep->X_add_number = 0;
67c0d1eb
RS
5989 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5990 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5991 load_delay_nop ();
4d7206a2
RS
5992 relax_start (ep->X_add_symbol);
5993 relax_switch ();
67c0d1eb 5994 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 5995 BFD_RELOC_LO16);
4d7206a2 5996 relax_end ();
ed6fb7bd 5997
f5040a92
AO
5998 if (ex.X_add_number != 0)
5999 {
6000 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6001 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6002 ex.X_op = O_constant;
67c0d1eb 6003 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 6004 reg, reg, BFD_RELOC_LO16);
f5040a92 6005 }
252b5132
RH
6006 }
6007 }
0a44bf69 6008 else if (mips_big_got)
252b5132
RH
6009 {
6010 expressionS ex;
252b5132
RH
6011
6012 /* This is the large GOT case. If this is a reference to an
6013 external symbol, we want
6014 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6015 addu $reg,$reg,$gp
6016 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
6017
6018 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
6019 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6020 nop
6021 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 6022 If there is a constant, it must be added in after.
f5040a92
AO
6023
6024 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
6025 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6026 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 6027 */
438c16b8
TS
6028 if (HAVE_NEWABI)
6029 {
4d7206a2 6030 ex.X_add_number = ep->X_add_number;
f5040a92 6031 ep->X_add_number = 0;
4d7206a2 6032 relax_start (ep->X_add_symbol);
df58fc94 6033 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6034 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6035 reg, reg, mips_gp_register);
6036 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6037 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
6038 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6039 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6040 else if (ex.X_add_number)
6041 {
6042 ex.X_op = O_constant;
67c0d1eb
RS
6043 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6044 BFD_RELOC_LO16);
f5040a92
AO
6045 }
6046
6047 ep->X_add_number = ex.X_add_number;
4d7206a2 6048 relax_switch ();
67c0d1eb 6049 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6050 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
6051 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6052 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 6053 relax_end ();
438c16b8 6054 }
252b5132 6055 else
438c16b8 6056 {
f5040a92
AO
6057 ex.X_add_number = ep->X_add_number;
6058 ep->X_add_number = 0;
4d7206a2 6059 relax_start (ep->X_add_symbol);
df58fc94 6060 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6061 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6062 reg, reg, mips_gp_register);
6063 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6064 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
6065 relax_switch ();
6066 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
6067 {
6068 /* We need a nop before loading from $gp. This special
6069 check is required because the lui which starts the main
6070 instruction stream does not refer to $gp, and so will not
6071 insert the nop which may be required. */
67c0d1eb 6072 macro_build (NULL, "nop", "");
438c16b8 6073 }
67c0d1eb 6074 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6075 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6076 load_delay_nop ();
67c0d1eb 6077 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 6078 BFD_RELOC_LO16);
4d7206a2 6079 relax_end ();
438c16b8 6080
f5040a92
AO
6081 if (ex.X_add_number != 0)
6082 {
6083 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6084 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6085 ex.X_op = O_constant;
67c0d1eb
RS
6086 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6087 BFD_RELOC_LO16);
f5040a92 6088 }
252b5132
RH
6089 }
6090 }
252b5132
RH
6091 else
6092 abort ();
8fc2e39e 6093
741fe287 6094 if (!mips_opts.at && *used_at == 1)
8fc2e39e 6095 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
6096}
6097
ea1fb5dc
RS
6098/* Move the contents of register SOURCE into register DEST. */
6099
6100static void
67c0d1eb 6101move_register (int dest, int source)
ea1fb5dc 6102{
df58fc94
RS
6103 /* Prefer to use a 16-bit microMIPS instruction unless the previous
6104 instruction specifically requires a 32-bit one. */
6105 if (mips_opts.micromips
6106 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 6107 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94
RS
6108 else
6109 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
6110 dest, source, 0);
ea1fb5dc
RS
6111}
6112
4d7206a2 6113/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
6114 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
6115 The two alternatives are:
4d7206a2
RS
6116
6117 Global symbol Local sybmol
6118 ------------- ------------
6119 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
6120 ... ...
6121 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
6122
6123 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
6124 emits the second for a 16-bit offset or add_got_offset_hilo emits
6125 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
6126
6127static void
67c0d1eb 6128load_got_offset (int dest, expressionS *local)
4d7206a2
RS
6129{
6130 expressionS global;
6131
6132 global = *local;
6133 global.X_add_number = 0;
6134
6135 relax_start (local->X_add_symbol);
67c0d1eb
RS
6136 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6137 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 6138 relax_switch ();
67c0d1eb
RS
6139 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6140 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
6141 relax_end ();
6142}
6143
6144static void
67c0d1eb 6145add_got_offset (int dest, expressionS *local)
4d7206a2
RS
6146{
6147 expressionS global;
6148
6149 global.X_op = O_constant;
6150 global.X_op_symbol = NULL;
6151 global.X_add_symbol = NULL;
6152 global.X_add_number = local->X_add_number;
6153
6154 relax_start (local->X_add_symbol);
67c0d1eb 6155 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
6156 dest, dest, BFD_RELOC_LO16);
6157 relax_switch ();
67c0d1eb 6158 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
6159 relax_end ();
6160}
6161
f6a22291
MR
6162static void
6163add_got_offset_hilo (int dest, expressionS *local, int tmp)
6164{
6165 expressionS global;
6166 int hold_mips_optimize;
6167
6168 global.X_op = O_constant;
6169 global.X_op_symbol = NULL;
6170 global.X_add_symbol = NULL;
6171 global.X_add_number = local->X_add_number;
6172
6173 relax_start (local->X_add_symbol);
6174 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6175 relax_switch ();
6176 /* Set mips_optimize around the lui instruction to avoid
6177 inserting an unnecessary nop after the lw. */
6178 hold_mips_optimize = mips_optimize;
6179 mips_optimize = 2;
6180 macro_build_lui (&global, tmp);
6181 mips_optimize = hold_mips_optimize;
6182 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6183 relax_end ();
6184
6185 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6186}
6187
df58fc94
RS
6188/* Emit a sequence of instructions to emulate a branch likely operation.
6189 BR is an ordinary branch corresponding to one to be emulated. BRNEG
6190 is its complementing branch with the original condition negated.
6191 CALL is set if the original branch specified the link operation.
6192 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6193
6194 Code like this is produced in the noreorder mode:
6195
6196 BRNEG <args>, 1f
6197 nop
6198 b <sym>
6199 delay slot (executed only if branch taken)
6200 1:
6201
6202 or, if CALL is set:
6203
6204 BRNEG <args>, 1f
6205 nop
6206 bal <sym>
6207 delay slot (executed only if branch taken)
6208 1:
6209
6210 In the reorder mode the delay slot would be filled with a nop anyway,
6211 so code produced is simply:
6212
6213 BR <args>, <sym>
6214 nop
6215
6216 This function is used when producing code for the microMIPS ASE that
6217 does not implement branch likely instructions in hardware. */
6218
6219static void
6220macro_build_branch_likely (const char *br, const char *brneg,
6221 int call, expressionS *ep, const char *fmt,
6222 unsigned int sreg, unsigned int treg)
6223{
6224 int noreorder = mips_opts.noreorder;
6225 expressionS expr1;
6226
6227 gas_assert (mips_opts.micromips);
6228 start_noreorder ();
6229 if (noreorder)
6230 {
6231 micromips_label_expr (&expr1);
6232 macro_build (&expr1, brneg, fmt, sreg, treg);
6233 macro_build (NULL, "nop", "");
6234 macro_build (ep, call ? "bal" : "b", "p");
6235
6236 /* Set to true so that append_insn adds a label. */
6237 emit_branch_likely_macro = TRUE;
6238 }
6239 else
6240 {
6241 macro_build (ep, br, fmt, sreg, treg);
6242 macro_build (NULL, "nop", "");
6243 }
6244 end_noreorder ();
6245}
6246
6247/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6248 the condition code tested. EP specifies the branch target. */
6249
6250static void
6251macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6252{
6253 const int call = 0;
6254 const char *brneg;
6255 const char *br;
6256
6257 switch (type)
6258 {
6259 case M_BC1FL:
6260 br = "bc1f";
6261 brneg = "bc1t";
6262 break;
6263 case M_BC1TL:
6264 br = "bc1t";
6265 brneg = "bc1f";
6266 break;
6267 case M_BC2FL:
6268 br = "bc2f";
6269 brneg = "bc2t";
6270 break;
6271 case M_BC2TL:
6272 br = "bc2t";
6273 brneg = "bc2f";
6274 break;
6275 default:
6276 abort ();
6277 }
6278 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6279}
6280
6281/* Emit a two-argument branch macro specified by TYPE, using SREG as
6282 the register tested. EP specifies the branch target. */
6283
6284static void
6285macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6286{
6287 const char *brneg = NULL;
6288 const char *br;
6289 int call = 0;
6290
6291 switch (type)
6292 {
6293 case M_BGEZ:
6294 br = "bgez";
6295 break;
6296 case M_BGEZL:
6297 br = mips_opts.micromips ? "bgez" : "bgezl";
6298 brneg = "bltz";
6299 break;
6300 case M_BGEZALL:
6301 gas_assert (mips_opts.micromips);
6302 br = "bgezals";
6303 brneg = "bltz";
6304 call = 1;
6305 break;
6306 case M_BGTZ:
6307 br = "bgtz";
6308 break;
6309 case M_BGTZL:
6310 br = mips_opts.micromips ? "bgtz" : "bgtzl";
6311 brneg = "blez";
6312 break;
6313 case M_BLEZ:
6314 br = "blez";
6315 break;
6316 case M_BLEZL:
6317 br = mips_opts.micromips ? "blez" : "blezl";
6318 brneg = "bgtz";
6319 break;
6320 case M_BLTZ:
6321 br = "bltz";
6322 break;
6323 case M_BLTZL:
6324 br = mips_opts.micromips ? "bltz" : "bltzl";
6325 brneg = "bgez";
6326 break;
6327 case M_BLTZALL:
6328 gas_assert (mips_opts.micromips);
6329 br = "bltzals";
6330 brneg = "bgez";
6331 call = 1;
6332 break;
6333 default:
6334 abort ();
6335 }
6336 if (mips_opts.micromips && brneg)
6337 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6338 else
6339 macro_build (ep, br, "s,p", sreg);
6340}
6341
6342/* Emit a three-argument branch macro specified by TYPE, using SREG and
6343 TREG as the registers tested. EP specifies the branch target. */
6344
6345static void
6346macro_build_branch_rsrt (int type, expressionS *ep,
6347 unsigned int sreg, unsigned int treg)
6348{
6349 const char *brneg = NULL;
6350 const int call = 0;
6351 const char *br;
6352
6353 switch (type)
6354 {
6355 case M_BEQ:
6356 case M_BEQ_I:
6357 br = "beq";
6358 break;
6359 case M_BEQL:
6360 case M_BEQL_I:
6361 br = mips_opts.micromips ? "beq" : "beql";
6362 brneg = "bne";
6363 break;
6364 case M_BNE:
6365 case M_BNE_I:
6366 br = "bne";
6367 break;
6368 case M_BNEL:
6369 case M_BNEL_I:
6370 br = mips_opts.micromips ? "bne" : "bnel";
6371 brneg = "beq";
6372 break;
6373 default:
6374 abort ();
6375 }
6376 if (mips_opts.micromips && brneg)
6377 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6378 else
6379 macro_build (ep, br, "s,t,p", sreg, treg);
6380}
6381
252b5132
RH
6382/*
6383 * Build macros
6384 * This routine implements the seemingly endless macro or synthesized
6385 * instructions and addressing modes in the mips assembly language. Many
6386 * of these macros are simple and are similar to each other. These could
67c1ffbe 6387 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
6388 * this verbose method. Others are not simple macros but are more like
6389 * optimizing code generation.
6390 * One interesting optimization is when several store macros appear
67c1ffbe 6391 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
6392 * The ensuing load upper instructions are ommited. This implies some kind
6393 * of global optimization. We currently only optimize within a single macro.
6394 * For many of the load and store macros if the address is specified as a
6395 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6396 * first load register 'at' with zero and use it as the base register. The
6397 * mips assembler simply uses register $zero. Just one tiny optimization
6398 * we're missing.
6399 */
6400static void
17a2f251 6401macro (struct mips_cl_insn *ip)
252b5132 6402{
741fe287
MR
6403 unsigned int treg, sreg, dreg, breg;
6404 unsigned int tempreg;
252b5132 6405 int mask;
43841e91 6406 int used_at = 0;
df58fc94 6407 expressionS label_expr;
252b5132 6408 expressionS expr1;
df58fc94 6409 expressionS *ep;
252b5132
RH
6410 const char *s;
6411 const char *s2;
6412 const char *fmt;
6413 int likely = 0;
252b5132 6414 int coproc = 0;
7f3c4072 6415 int offbits = 16;
1abe91b1 6416 int call = 0;
df58fc94
RS
6417 int jals = 0;
6418 int dbl = 0;
6419 int imm = 0;
6420 int ust = 0;
6421 int lp = 0;
6422 int ab = 0;
252b5132 6423 int off;
67c0d1eb 6424 offsetT maxnum;
252b5132 6425 bfd_reloc_code_real_type r;
252b5132
RH
6426 int hold_mips_optimize;
6427
9c2799c2 6428 gas_assert (! mips_opts.mips16);
252b5132 6429
df58fc94
RS
6430 treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6431 dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6432 sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
252b5132
RH
6433 mask = ip->insn_mo->mask;
6434
df58fc94
RS
6435 label_expr.X_op = O_constant;
6436 label_expr.X_op_symbol = NULL;
6437 label_expr.X_add_symbol = NULL;
6438 label_expr.X_add_number = 0;
6439
252b5132
RH
6440 expr1.X_op = O_constant;
6441 expr1.X_op_symbol = NULL;
6442 expr1.X_add_symbol = NULL;
6443 expr1.X_add_number = 1;
6444
6445 switch (mask)
6446 {
6447 case M_DABS:
6448 dbl = 1;
6449 case M_ABS:
df58fc94
RS
6450 /* bgez $a0,1f
6451 move v0,$a0
6452 sub v0,$zero,$a0
6453 1:
6454 */
252b5132 6455
7d10b47d 6456 start_noreorder ();
252b5132 6457
df58fc94
RS
6458 if (mips_opts.micromips)
6459 micromips_label_expr (&label_expr);
6460 else
6461 label_expr.X_add_number = 8;
6462 macro_build (&label_expr, "bgez", "s,p", sreg);
252b5132 6463 if (dreg == sreg)
a605d2b3 6464 macro_build (NULL, "nop", "");
252b5132 6465 else
67c0d1eb
RS
6466 move_register (dreg, sreg);
6467 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
df58fc94
RS
6468 if (mips_opts.micromips)
6469 micromips_add_label ();
252b5132 6470
7d10b47d 6471 end_noreorder ();
8fc2e39e 6472 break;
252b5132
RH
6473
6474 case M_ADD_I:
6475 s = "addi";
6476 s2 = "add";
6477 goto do_addi;
6478 case M_ADDU_I:
6479 s = "addiu";
6480 s2 = "addu";
6481 goto do_addi;
6482 case M_DADD_I:
6483 dbl = 1;
6484 s = "daddi";
6485 s2 = "dadd";
df58fc94
RS
6486 if (!mips_opts.micromips)
6487 goto do_addi;
6488 if (imm_expr.X_op == O_constant
6489 && imm_expr.X_add_number >= -0x200
6490 && imm_expr.X_add_number < 0x200)
6491 {
6492 macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6493 break;
6494 }
6495 goto do_addi_i;
252b5132
RH
6496 case M_DADDU_I:
6497 dbl = 1;
6498 s = "daddiu";
6499 s2 = "daddu";
6500 do_addi:
6501 if (imm_expr.X_op == O_constant
6502 && imm_expr.X_add_number >= -0x8000
6503 && imm_expr.X_add_number < 0x8000)
6504 {
67c0d1eb 6505 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 6506 break;
252b5132 6507 }
df58fc94 6508 do_addi_i:
8fc2e39e 6509 used_at = 1;
67c0d1eb
RS
6510 load_register (AT, &imm_expr, dbl);
6511 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6512 break;
6513
6514 case M_AND_I:
6515 s = "andi";
6516 s2 = "and";
6517 goto do_bit;
6518 case M_OR_I:
6519 s = "ori";
6520 s2 = "or";
6521 goto do_bit;
6522 case M_NOR_I:
6523 s = "";
6524 s2 = "nor";
6525 goto do_bit;
6526 case M_XOR_I:
6527 s = "xori";
6528 s2 = "xor";
6529 do_bit:
6530 if (imm_expr.X_op == O_constant
6531 && imm_expr.X_add_number >= 0
6532 && imm_expr.X_add_number < 0x10000)
6533 {
6534 if (mask != M_NOR_I)
67c0d1eb 6535 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
6536 else
6537 {
67c0d1eb
RS
6538 macro_build (&imm_expr, "ori", "t,r,i",
6539 treg, sreg, BFD_RELOC_LO16);
6540 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 6541 }
8fc2e39e 6542 break;
252b5132
RH
6543 }
6544
8fc2e39e 6545 used_at = 1;
67c0d1eb
RS
6546 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6547 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6548 break;
6549
8b082fb1
TS
6550 case M_BALIGN:
6551 switch (imm_expr.X_add_number)
6552 {
6553 case 0:
6554 macro_build (NULL, "nop", "");
6555 break;
6556 case 2:
6557 macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6558 break;
03f66e8a
MR
6559 case 1:
6560 case 3:
8b082fb1 6561 macro_build (NULL, "balign", "t,s,2", treg, sreg,
90ecf173 6562 (int) imm_expr.X_add_number);
8b082fb1 6563 break;
03f66e8a
MR
6564 default:
6565 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6566 (unsigned long) imm_expr.X_add_number);
6567 break;
8b082fb1
TS
6568 }
6569 break;
6570
df58fc94
RS
6571 case M_BC1FL:
6572 case M_BC1TL:
6573 case M_BC2FL:
6574 case M_BC2TL:
6575 gas_assert (mips_opts.micromips);
6576 macro_build_branch_ccl (mask, &offset_expr,
6577 EXTRACT_OPERAND (1, BCC, *ip));
6578 break;
6579
252b5132 6580 case M_BEQ_I:
252b5132 6581 case M_BEQL_I:
252b5132 6582 case M_BNE_I:
252b5132 6583 case M_BNEL_I:
252b5132 6584 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
6585 treg = 0;
6586 else
252b5132 6587 {
df58fc94
RS
6588 treg = AT;
6589 used_at = 1;
6590 load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
252b5132 6591 }
df58fc94
RS
6592 /* Fall through. */
6593 case M_BEQL:
6594 case M_BNEL:
6595 macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
252b5132
RH
6596 break;
6597
6598 case M_BGEL:
6599 likely = 1;
6600 case M_BGE:
6601 if (treg == 0)
df58fc94
RS
6602 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6603 else if (sreg == 0)
6604 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6605 else
252b5132 6606 {
df58fc94
RS
6607 used_at = 1;
6608 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6609 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6610 &offset_expr, AT, ZERO);
252b5132 6611 }
df58fc94
RS
6612 break;
6613
6614 case M_BGEZL:
6615 case M_BGEZALL:
6616 case M_BGTZL:
6617 case M_BLEZL:
6618 case M_BLTZL:
6619 case M_BLTZALL:
6620 macro_build_branch_rs (mask, &offset_expr, sreg);
252b5132
RH
6621 break;
6622
6623 case M_BGTL_I:
6624 likely = 1;
6625 case M_BGT_I:
90ecf173 6626 /* Check for > max integer. */
252b5132 6627 maxnum = 0x7fffffff;
ca4e0257 6628 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6629 {
6630 maxnum <<= 16;
6631 maxnum |= 0xffff;
6632 maxnum <<= 16;
6633 maxnum |= 0xffff;
6634 }
6635 if (imm_expr.X_op == O_constant
6636 && imm_expr.X_add_number >= maxnum
ca4e0257 6637 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6638 {
6639 do_false:
90ecf173 6640 /* Result is always false. */
252b5132 6641 if (! likely)
a605d2b3 6642 macro_build (NULL, "nop", "");
252b5132 6643 else
df58fc94 6644 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 6645 break;
252b5132
RH
6646 }
6647 if (imm_expr.X_op != O_constant)
6648 as_bad (_("Unsupported large constant"));
f9419b05 6649 ++imm_expr.X_add_number;
252b5132
RH
6650 /* FALLTHROUGH */
6651 case M_BGE_I:
6652 case M_BGEL_I:
6653 if (mask == M_BGEL_I)
6654 likely = 1;
6655 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6656 {
df58fc94
RS
6657 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
6658 &offset_expr, sreg);
8fc2e39e 6659 break;
252b5132
RH
6660 }
6661 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6662 {
df58fc94
RS
6663 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
6664 &offset_expr, sreg);
8fc2e39e 6665 break;
252b5132
RH
6666 }
6667 maxnum = 0x7fffffff;
ca4e0257 6668 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6669 {
6670 maxnum <<= 16;
6671 maxnum |= 0xffff;
6672 maxnum <<= 16;
6673 maxnum |= 0xffff;
6674 }
6675 maxnum = - maxnum - 1;
6676 if (imm_expr.X_op == O_constant
6677 && imm_expr.X_add_number <= maxnum
ca4e0257 6678 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6679 {
6680 do_true:
6681 /* result is always true */
6682 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 6683 macro_build (&offset_expr, "b", "p");
8fc2e39e 6684 break;
252b5132 6685 }
8fc2e39e 6686 used_at = 1;
67c0d1eb 6687 set_at (sreg, 0);
df58fc94
RS
6688 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6689 &offset_expr, AT, ZERO);
252b5132
RH
6690 break;
6691
6692 case M_BGEUL:
6693 likely = 1;
6694 case M_BGEU:
6695 if (treg == 0)
6696 goto do_true;
df58fc94
RS
6697 else if (sreg == 0)
6698 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6699 &offset_expr, ZERO, treg);
6700 else
252b5132 6701 {
df58fc94
RS
6702 used_at = 1;
6703 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6704 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6705 &offset_expr, AT, ZERO);
252b5132 6706 }
252b5132
RH
6707 break;
6708
6709 case M_BGTUL_I:
6710 likely = 1;
6711 case M_BGTU_I:
6712 if (sreg == 0
ca4e0257 6713 || (HAVE_32BIT_GPRS
252b5132 6714 && imm_expr.X_op == O_constant
f01dc953 6715 && imm_expr.X_add_number == -1))
252b5132
RH
6716 goto do_false;
6717 if (imm_expr.X_op != O_constant)
6718 as_bad (_("Unsupported large constant"));
f9419b05 6719 ++imm_expr.X_add_number;
252b5132
RH
6720 /* FALLTHROUGH */
6721 case M_BGEU_I:
6722 case M_BGEUL_I:
6723 if (mask == M_BGEUL_I)
6724 likely = 1;
6725 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6726 goto do_true;
df58fc94
RS
6727 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6728 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6729 &offset_expr, sreg, ZERO);
6730 else
252b5132 6731 {
df58fc94
RS
6732 used_at = 1;
6733 set_at (sreg, 1);
6734 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6735 &offset_expr, AT, ZERO);
252b5132 6736 }
252b5132
RH
6737 break;
6738
6739 case M_BGTL:
6740 likely = 1;
6741 case M_BGT:
6742 if (treg == 0)
df58fc94
RS
6743 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
6744 else if (sreg == 0)
6745 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
6746 else
252b5132 6747 {
df58fc94
RS
6748 used_at = 1;
6749 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6750 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6751 &offset_expr, AT, ZERO);
252b5132 6752 }
252b5132
RH
6753 break;
6754
6755 case M_BGTUL:
6756 likely = 1;
6757 case M_BGTU:
6758 if (treg == 0)
df58fc94
RS
6759 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6760 &offset_expr, sreg, ZERO);
6761 else if (sreg == 0)
6762 goto do_false;
6763 else
252b5132 6764 {
df58fc94
RS
6765 used_at = 1;
6766 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6767 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6768 &offset_expr, AT, ZERO);
252b5132 6769 }
252b5132
RH
6770 break;
6771
6772 case M_BLEL:
6773 likely = 1;
6774 case M_BLE:
6775 if (treg == 0)
df58fc94
RS
6776 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6777 else if (sreg == 0)
6778 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
6779 else
252b5132 6780 {
df58fc94
RS
6781 used_at = 1;
6782 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6783 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6784 &offset_expr, AT, ZERO);
252b5132 6785 }
252b5132
RH
6786 break;
6787
6788 case M_BLEL_I:
6789 likely = 1;
6790 case M_BLE_I:
6791 maxnum = 0x7fffffff;
ca4e0257 6792 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
6793 {
6794 maxnum <<= 16;
6795 maxnum |= 0xffff;
6796 maxnum <<= 16;
6797 maxnum |= 0xffff;
6798 }
6799 if (imm_expr.X_op == O_constant
6800 && imm_expr.X_add_number >= maxnum
ca4e0257 6801 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
6802 goto do_true;
6803 if (imm_expr.X_op != O_constant)
6804 as_bad (_("Unsupported large constant"));
f9419b05 6805 ++imm_expr.X_add_number;
252b5132
RH
6806 /* FALLTHROUGH */
6807 case M_BLT_I:
6808 case M_BLTL_I:
6809 if (mask == M_BLTL_I)
6810 likely = 1;
6811 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
6812 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6813 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6814 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6815 else
252b5132 6816 {
df58fc94
RS
6817 used_at = 1;
6818 set_at (sreg, 0);
6819 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6820 &offset_expr, AT, ZERO);
252b5132 6821 }
252b5132
RH
6822 break;
6823
6824 case M_BLEUL:
6825 likely = 1;
6826 case M_BLEU:
6827 if (treg == 0)
df58fc94
RS
6828 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6829 &offset_expr, sreg, ZERO);
6830 else if (sreg == 0)
6831 goto do_true;
6832 else
252b5132 6833 {
df58fc94
RS
6834 used_at = 1;
6835 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6836 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6837 &offset_expr, AT, ZERO);
252b5132 6838 }
252b5132
RH
6839 break;
6840
6841 case M_BLEUL_I:
6842 likely = 1;
6843 case M_BLEU_I:
6844 if (sreg == 0
ca4e0257 6845 || (HAVE_32BIT_GPRS
252b5132 6846 && imm_expr.X_op == O_constant
f01dc953 6847 && imm_expr.X_add_number == -1))
252b5132
RH
6848 goto do_true;
6849 if (imm_expr.X_op != O_constant)
6850 as_bad (_("Unsupported large constant"));
f9419b05 6851 ++imm_expr.X_add_number;
252b5132
RH
6852 /* FALLTHROUGH */
6853 case M_BLTU_I:
6854 case M_BLTUL_I:
6855 if (mask == M_BLTUL_I)
6856 likely = 1;
6857 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6858 goto do_false;
df58fc94
RS
6859 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6860 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6861 &offset_expr, sreg, ZERO);
6862 else
252b5132 6863 {
df58fc94
RS
6864 used_at = 1;
6865 set_at (sreg, 1);
6866 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6867 &offset_expr, AT, ZERO);
252b5132 6868 }
252b5132
RH
6869 break;
6870
6871 case M_BLTL:
6872 likely = 1;
6873 case M_BLT:
6874 if (treg == 0)
df58fc94
RS
6875 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6876 else if (sreg == 0)
6877 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
6878 else
252b5132 6879 {
df58fc94
RS
6880 used_at = 1;
6881 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6882 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6883 &offset_expr, AT, ZERO);
252b5132 6884 }
252b5132
RH
6885 break;
6886
6887 case M_BLTUL:
6888 likely = 1;
6889 case M_BLTU:
6890 if (treg == 0)
6891 goto do_false;
df58fc94
RS
6892 else if (sreg == 0)
6893 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6894 &offset_expr, ZERO, treg);
6895 else
252b5132 6896 {
df58fc94
RS
6897 used_at = 1;
6898 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6899 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6900 &offset_expr, AT, ZERO);
252b5132 6901 }
252b5132
RH
6902 break;
6903
5f74bc13
CD
6904 case M_DEXT:
6905 {
d5818fca
MR
6906 /* Use unsigned arithmetic. */
6907 addressT pos;
6908 addressT size;
5f74bc13 6909
90ecf173 6910 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
6911 {
6912 as_bad (_("Unsupported large constant"));
6913 pos = size = 1;
6914 }
6915 else
6916 {
d5818fca
MR
6917 pos = imm_expr.X_add_number;
6918 size = imm2_expr.X_add_number;
5f74bc13
CD
6919 }
6920
6921 if (pos > 63)
6922 {
d5818fca 6923 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
6924 pos = 1;
6925 }
90ecf173 6926 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
6927 {
6928 as_bad (_("Improper extract size (%lu, position %lu)"),
d5818fca 6929 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
6930 size = 1;
6931 }
6932
6933 if (size <= 32 && pos < 32)
6934 {
6935 s = "dext";
6936 fmt = "t,r,+A,+C";
6937 }
6938 else if (size <= 32)
6939 {
6940 s = "dextu";
6941 fmt = "t,r,+E,+H";
6942 }
6943 else
6944 {
6945 s = "dextm";
6946 fmt = "t,r,+A,+G";
6947 }
d5818fca
MR
6948 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6949 (int) (size - 1));
5f74bc13 6950 }
8fc2e39e 6951 break;
5f74bc13
CD
6952
6953 case M_DINS:
6954 {
d5818fca
MR
6955 /* Use unsigned arithmetic. */
6956 addressT pos;
6957 addressT size;
5f74bc13 6958
90ecf173 6959 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
6960 {
6961 as_bad (_("Unsupported large constant"));
6962 pos = size = 1;
6963 }
6964 else
6965 {
d5818fca
MR
6966 pos = imm_expr.X_add_number;
6967 size = imm2_expr.X_add_number;
5f74bc13
CD
6968 }
6969
6970 if (pos > 63)
6971 {
d5818fca 6972 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
6973 pos = 1;
6974 }
90ecf173 6975 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
6976 {
6977 as_bad (_("Improper insert size (%lu, position %lu)"),
d5818fca 6978 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
6979 size = 1;
6980 }
6981
6982 if (pos < 32 && (pos + size - 1) < 32)
6983 {
6984 s = "dins";
6985 fmt = "t,r,+A,+B";
6986 }
6987 else if (pos >= 32)
6988 {
6989 s = "dinsu";
6990 fmt = "t,r,+E,+F";
6991 }
6992 else
6993 {
6994 s = "dinsm";
6995 fmt = "t,r,+A,+F";
6996 }
750bdd57
AS
6997 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6998 (int) (pos + size - 1));
5f74bc13 6999 }
8fc2e39e 7000 break;
5f74bc13 7001
252b5132
RH
7002 case M_DDIV_3:
7003 dbl = 1;
7004 case M_DIV_3:
7005 s = "mflo";
7006 goto do_div3;
7007 case M_DREM_3:
7008 dbl = 1;
7009 case M_REM_3:
7010 s = "mfhi";
7011 do_div3:
7012 if (treg == 0)
7013 {
7014 as_warn (_("Divide by zero."));
7015 if (mips_trap)
df58fc94 7016 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 7017 else
df58fc94 7018 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 7019 break;
252b5132
RH
7020 }
7021
7d10b47d 7022 start_noreorder ();
252b5132
RH
7023 if (mips_trap)
7024 {
df58fc94 7025 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 7026 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
7027 }
7028 else
7029 {
df58fc94
RS
7030 if (mips_opts.micromips)
7031 micromips_label_expr (&label_expr);
7032 else
7033 label_expr.X_add_number = 8;
7034 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 7035 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
df58fc94
RS
7036 macro_build (NULL, "break", BRK_FMT, 7);
7037 if (mips_opts.micromips)
7038 micromips_add_label ();
252b5132
RH
7039 }
7040 expr1.X_add_number = -1;
8fc2e39e 7041 used_at = 1;
f6a22291 7042 load_register (AT, &expr1, dbl);
df58fc94
RS
7043 if (mips_opts.micromips)
7044 micromips_label_expr (&label_expr);
7045 else
7046 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
7047 macro_build (&label_expr, "bne", "s,t,p", treg, AT);
252b5132
RH
7048 if (dbl)
7049 {
7050 expr1.X_add_number = 1;
f6a22291 7051 load_register (AT, &expr1, dbl);
df58fc94 7052 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
7053 }
7054 else
7055 {
7056 expr1.X_add_number = 0x80000000;
df58fc94 7057 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
7058 }
7059 if (mips_trap)
7060 {
df58fc94 7061 macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
252b5132
RH
7062 /* We want to close the noreorder block as soon as possible, so
7063 that later insns are available for delay slot filling. */
7d10b47d 7064 end_noreorder ();
252b5132
RH
7065 }
7066 else
7067 {
df58fc94
RS
7068 if (mips_opts.micromips)
7069 micromips_label_expr (&label_expr);
7070 else
7071 label_expr.X_add_number = 8;
7072 macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
a605d2b3 7073 macro_build (NULL, "nop", "");
252b5132
RH
7074
7075 /* We want to close the noreorder block as soon as possible, so
7076 that later insns are available for delay slot filling. */
7d10b47d 7077 end_noreorder ();
252b5132 7078
df58fc94 7079 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 7080 }
df58fc94
RS
7081 if (mips_opts.micromips)
7082 micromips_add_label ();
7083 macro_build (NULL, s, MFHL_FMT, dreg);
252b5132
RH
7084 break;
7085
7086 case M_DIV_3I:
7087 s = "div";
7088 s2 = "mflo";
7089 goto do_divi;
7090 case M_DIVU_3I:
7091 s = "divu";
7092 s2 = "mflo";
7093 goto do_divi;
7094 case M_REM_3I:
7095 s = "div";
7096 s2 = "mfhi";
7097 goto do_divi;
7098 case M_REMU_3I:
7099 s = "divu";
7100 s2 = "mfhi";
7101 goto do_divi;
7102 case M_DDIV_3I:
7103 dbl = 1;
7104 s = "ddiv";
7105 s2 = "mflo";
7106 goto do_divi;
7107 case M_DDIVU_3I:
7108 dbl = 1;
7109 s = "ddivu";
7110 s2 = "mflo";
7111 goto do_divi;
7112 case M_DREM_3I:
7113 dbl = 1;
7114 s = "ddiv";
7115 s2 = "mfhi";
7116 goto do_divi;
7117 case M_DREMU_3I:
7118 dbl = 1;
7119 s = "ddivu";
7120 s2 = "mfhi";
7121 do_divi:
7122 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7123 {
7124 as_warn (_("Divide by zero."));
7125 if (mips_trap)
df58fc94 7126 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 7127 else
df58fc94 7128 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 7129 break;
252b5132
RH
7130 }
7131 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7132 {
7133 if (strcmp (s2, "mflo") == 0)
67c0d1eb 7134 move_register (dreg, sreg);
252b5132 7135 else
c80c840e 7136 move_register (dreg, ZERO);
8fc2e39e 7137 break;
252b5132
RH
7138 }
7139 if (imm_expr.X_op == O_constant
7140 && imm_expr.X_add_number == -1
7141 && s[strlen (s) - 1] != 'u')
7142 {
7143 if (strcmp (s2, "mflo") == 0)
7144 {
67c0d1eb 7145 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
7146 }
7147 else
c80c840e 7148 move_register (dreg, ZERO);
8fc2e39e 7149 break;
252b5132
RH
7150 }
7151
8fc2e39e 7152 used_at = 1;
67c0d1eb
RS
7153 load_register (AT, &imm_expr, dbl);
7154 macro_build (NULL, s, "z,s,t", sreg, AT);
df58fc94 7155 macro_build (NULL, s2, MFHL_FMT, dreg);
252b5132
RH
7156 break;
7157
7158 case M_DIVU_3:
7159 s = "divu";
7160 s2 = "mflo";
7161 goto do_divu3;
7162 case M_REMU_3:
7163 s = "divu";
7164 s2 = "mfhi";
7165 goto do_divu3;
7166 case M_DDIVU_3:
7167 s = "ddivu";
7168 s2 = "mflo";
7169 goto do_divu3;
7170 case M_DREMU_3:
7171 s = "ddivu";
7172 s2 = "mfhi";
7173 do_divu3:
7d10b47d 7174 start_noreorder ();
252b5132
RH
7175 if (mips_trap)
7176 {
df58fc94 7177 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 7178 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7179 /* We want to close the noreorder block as soon as possible, so
7180 that later insns are available for delay slot filling. */
7d10b47d 7181 end_noreorder ();
252b5132
RH
7182 }
7183 else
7184 {
df58fc94
RS
7185 if (mips_opts.micromips)
7186 micromips_label_expr (&label_expr);
7187 else
7188 label_expr.X_add_number = 8;
7189 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 7190 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7191
7192 /* We want to close the noreorder block as soon as possible, so
7193 that later insns are available for delay slot filling. */
7d10b47d 7194 end_noreorder ();
df58fc94
RS
7195 macro_build (NULL, "break", BRK_FMT, 7);
7196 if (mips_opts.micromips)
7197 micromips_add_label ();
252b5132 7198 }
df58fc94 7199 macro_build (NULL, s2, MFHL_FMT, dreg);
8fc2e39e 7200 break;
252b5132 7201
1abe91b1
MR
7202 case M_DLCA_AB:
7203 dbl = 1;
7204 case M_LCA_AB:
7205 call = 1;
7206 goto do_la;
252b5132
RH
7207 case M_DLA_AB:
7208 dbl = 1;
7209 case M_LA_AB:
1abe91b1 7210 do_la:
252b5132
RH
7211 /* Load the address of a symbol into a register. If breg is not
7212 zero, we then add a base register to it. */
7213
3bec30a8
TS
7214 if (dbl && HAVE_32BIT_GPRS)
7215 as_warn (_("dla used to load 32-bit register"));
7216
90ecf173 7217 if (!dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
7218 as_warn (_("la used to load 64-bit address"));
7219
0c11417f
MR
7220 if (offset_expr.X_op == O_constant
7221 && offset_expr.X_add_number >= -0x8000
7222 && offset_expr.X_add_number < 0x8000)
7223 {
aed1a261 7224 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 7225 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 7226 break;
0c11417f
MR
7227 }
7228
741fe287 7229 if (mips_opts.at && (treg == breg))
afdbd6d0
CD
7230 {
7231 tempreg = AT;
7232 used_at = 1;
7233 }
7234 else
7235 {
7236 tempreg = treg;
afdbd6d0
CD
7237 }
7238
252b5132
RH
7239 if (offset_expr.X_op != O_symbol
7240 && offset_expr.X_op != O_constant)
7241 {
f71d0d44 7242 as_bad (_("Expression too complex"));
252b5132
RH
7243 offset_expr.X_op = O_constant;
7244 }
7245
252b5132 7246 if (offset_expr.X_op == O_constant)
aed1a261 7247 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
7248 else if (mips_pic == NO_PIC)
7249 {
d6bc6245 7250 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 7251 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
7252 Otherwise we want
7253 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
7254 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7255 If we have a constant, we need two instructions anyhow,
d6bc6245 7256 so we may as well always use the latter form.
76b3015f 7257
6caf9ef4
TS
7258 With 64bit address space and a usable $at we want
7259 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7260 lui $at,<sym> (BFD_RELOC_HI16_S)
7261 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7262 daddiu $at,<sym> (BFD_RELOC_LO16)
7263 dsll32 $tempreg,0
7264 daddu $tempreg,$tempreg,$at
7265
7266 If $at is already in use, we use a path which is suboptimal
7267 on superscalar processors.
7268 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7269 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7270 dsll $tempreg,16
7271 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
7272 dsll $tempreg,16
7273 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
7274
7275 For GP relative symbols in 64bit address space we can use
7276 the same sequence as in 32bit address space. */
aed1a261 7277 if (HAVE_64BIT_SYMBOLS)
252b5132 7278 {
6caf9ef4
TS
7279 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7280 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7281 {
7282 relax_start (offset_expr.X_add_symbol);
7283 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7284 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7285 relax_switch ();
7286 }
d6bc6245 7287
741fe287 7288 if (used_at == 0 && mips_opts.at)
98d3f06f 7289 {
df58fc94 7290 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7291 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 7292 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7293 AT, BFD_RELOC_HI16_S);
67c0d1eb 7294 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7295 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 7296 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7297 AT, AT, BFD_RELOC_LO16);
df58fc94 7298 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 7299 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
7300 used_at = 1;
7301 }
7302 else
7303 {
df58fc94 7304 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7305 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 7306 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7307 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 7308 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7309 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7310 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 7311 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7312 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7313 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 7314 }
6caf9ef4
TS
7315
7316 if (mips_relax.sequence)
7317 relax_end ();
98d3f06f
KH
7318 }
7319 else
7320 {
7321 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 7322 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 7323 {
4d7206a2 7324 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7325 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7326 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 7327 relax_switch ();
98d3f06f 7328 }
6943caf0 7329 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
f71d0d44 7330 as_bad (_("Offset too large"));
67c0d1eb
RS
7331 macro_build_lui (&offset_expr, tempreg);
7332 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7333 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
7334 if (mips_relax.sequence)
7335 relax_end ();
98d3f06f 7336 }
252b5132 7337 }
0a44bf69 7338 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 7339 {
9117d219
NC
7340 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7341
252b5132
RH
7342 /* If this is a reference to an external symbol, and there
7343 is no constant, we want
7344 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 7345 or for lca or if tempreg is PIC_CALL_REG
9117d219 7346 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
7347 For a local symbol, we want
7348 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7349 nop
7350 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7351
7352 If we have a small constant, and this is a reference to
7353 an external symbol, we want
7354 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7355 nop
7356 addiu $tempreg,$tempreg,<constant>
7357 For a local symbol, we want the same instruction
7358 sequence, but we output a BFD_RELOC_LO16 reloc on the
7359 addiu instruction.
7360
7361 If we have a large constant, and this is a reference to
7362 an external symbol, we want
7363 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7364 lui $at,<hiconstant>
7365 addiu $at,$at,<loconstant>
7366 addu $tempreg,$tempreg,$at
7367 For a local symbol, we want the same instruction
7368 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 7369 addiu instruction.
ed6fb7bd
SC
7370 */
7371
4d7206a2 7372 if (offset_expr.X_add_number == 0)
252b5132 7373 {
0a44bf69
RS
7374 if (mips_pic == SVR4_PIC
7375 && breg == 0
7376 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
7377 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7378
7379 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7380 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7381 lw_reloc_type, mips_gp_register);
4d7206a2 7382 if (breg != 0)
252b5132
RH
7383 {
7384 /* We're going to put in an addu instruction using
7385 tempreg, so we may as well insert the nop right
7386 now. */
269137b2 7387 load_delay_nop ();
252b5132 7388 }
4d7206a2 7389 relax_switch ();
67c0d1eb
RS
7390 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7391 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7392 load_delay_nop ();
67c0d1eb
RS
7393 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7394 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 7395 relax_end ();
252b5132
RH
7396 /* FIXME: If breg == 0, and the next instruction uses
7397 $tempreg, then if this variant case is used an extra
7398 nop will be generated. */
7399 }
4d7206a2
RS
7400 else if (offset_expr.X_add_number >= -0x8000
7401 && offset_expr.X_add_number < 0x8000)
252b5132 7402 {
67c0d1eb 7403 load_got_offset (tempreg, &offset_expr);
269137b2 7404 load_delay_nop ();
67c0d1eb 7405 add_got_offset (tempreg, &offset_expr);
252b5132
RH
7406 }
7407 else
7408 {
4d7206a2
RS
7409 expr1.X_add_number = offset_expr.X_add_number;
7410 offset_expr.X_add_number =
43c0598f 7411 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 7412 load_got_offset (tempreg, &offset_expr);
f6a22291 7413 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
7414 /* If we are going to add in a base register, and the
7415 target register and the base register are the same,
7416 then we are using AT as a temporary register. Since
7417 we want to load the constant into AT, we add our
7418 current AT (from the global offset table) and the
7419 register into the register now, and pretend we were
7420 not using a base register. */
67c0d1eb 7421 if (breg == treg)
252b5132 7422 {
269137b2 7423 load_delay_nop ();
67c0d1eb 7424 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7425 treg, AT, breg);
252b5132
RH
7426 breg = 0;
7427 tempreg = treg;
252b5132 7428 }
f6a22291 7429 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
7430 used_at = 1;
7431 }
7432 }
0a44bf69 7433 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 7434 {
67c0d1eb 7435 int add_breg_early = 0;
f5040a92
AO
7436
7437 /* If this is a reference to an external, and there is no
7438 constant, or local symbol (*), with or without a
7439 constant, we want
7440 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 7441 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
7442 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7443
7444 If we have a small constant, and this is a reference to
7445 an external symbol, we want
7446 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7447 addiu $tempreg,$tempreg,<constant>
7448
7449 If we have a large constant, and this is a reference to
7450 an external symbol, we want
7451 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7452 lui $at,<hiconstant>
7453 addiu $at,$at,<loconstant>
7454 addu $tempreg,$tempreg,$at
7455
7456 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7457 local symbols, even though it introduces an additional
7458 instruction. */
7459
f5040a92
AO
7460 if (offset_expr.X_add_number)
7461 {
4d7206a2 7462 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
7463 offset_expr.X_add_number = 0;
7464
4d7206a2 7465 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7466 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7467 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7468
7469 if (expr1.X_add_number >= -0x8000
7470 && expr1.X_add_number < 0x8000)
7471 {
67c0d1eb
RS
7472 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7473 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 7474 }
ecd13cd3 7475 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 7476 {
f5040a92
AO
7477 /* If we are going to add in a base register, and the
7478 target register and the base register are the same,
7479 then we are using AT as a temporary register. Since
7480 we want to load the constant into AT, we add our
7481 current AT (from the global offset table) and the
7482 register into the register now, and pretend we were
7483 not using a base register. */
7484 if (breg != treg)
7485 dreg = tempreg;
7486 else
7487 {
9c2799c2 7488 gas_assert (tempreg == AT);
67c0d1eb
RS
7489 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7490 treg, AT, breg);
f5040a92 7491 dreg = treg;
67c0d1eb 7492 add_breg_early = 1;
f5040a92
AO
7493 }
7494
f6a22291 7495 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7496 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7497 dreg, dreg, AT);
f5040a92 7498
f5040a92
AO
7499 used_at = 1;
7500 }
7501 else
7502 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7503
4d7206a2 7504 relax_switch ();
f5040a92
AO
7505 offset_expr.X_add_number = expr1.X_add_number;
7506
67c0d1eb
RS
7507 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7508 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7509 if (add_breg_early)
f5040a92 7510 {
67c0d1eb 7511 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 7512 treg, tempreg, breg);
f5040a92
AO
7513 breg = 0;
7514 tempreg = treg;
7515 }
4d7206a2 7516 relax_end ();
f5040a92 7517 }
4d7206a2 7518 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 7519 {
4d7206a2 7520 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7521 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7522 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 7523 relax_switch ();
67c0d1eb
RS
7524 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7525 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 7526 relax_end ();
f5040a92 7527 }
4d7206a2 7528 else
f5040a92 7529 {
67c0d1eb
RS
7530 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7531 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7532 }
7533 }
0a44bf69 7534 else if (mips_big_got && !HAVE_NEWABI)
252b5132 7535 {
67c0d1eb 7536 int gpdelay;
9117d219
NC
7537 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7538 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 7539 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
7540
7541 /* This is the large GOT case. If this is a reference to an
7542 external symbol, and there is no constant, we want
7543 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7544 addu $tempreg,$tempreg,$gp
7545 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 7546 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
7547 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7548 addu $tempreg,$tempreg,$gp
7549 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
7550 For a local symbol, we want
7551 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7552 nop
7553 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7554
7555 If we have a small constant, and this is a reference to
7556 an external symbol, we want
7557 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7558 addu $tempreg,$tempreg,$gp
7559 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7560 nop
7561 addiu $tempreg,$tempreg,<constant>
7562 For a local symbol, we want
7563 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7564 nop
7565 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7566
7567 If we have a large constant, and this is a reference to
7568 an external symbol, we want
7569 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7570 addu $tempreg,$tempreg,$gp
7571 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7572 lui $at,<hiconstant>
7573 addiu $at,$at,<loconstant>
7574 addu $tempreg,$tempreg,$at
7575 For a local symbol, we want
7576 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7577 lui $at,<hiconstant>
7578 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
7579 addu $tempreg,$tempreg,$at
f5040a92 7580 */
438c16b8 7581
252b5132
RH
7582 expr1.X_add_number = offset_expr.X_add_number;
7583 offset_expr.X_add_number = 0;
4d7206a2 7584 relax_start (offset_expr.X_add_symbol);
67c0d1eb 7585 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
7586 if (expr1.X_add_number == 0 && breg == 0
7587 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
7588 {
7589 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7590 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7591 }
df58fc94 7592 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 7593 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7594 tempreg, tempreg, mips_gp_register);
67c0d1eb 7595 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 7596 tempreg, lw_reloc_type, tempreg);
252b5132
RH
7597 if (expr1.X_add_number == 0)
7598 {
67c0d1eb 7599 if (breg != 0)
252b5132
RH
7600 {
7601 /* We're going to put in an addu instruction using
7602 tempreg, so we may as well insert the nop right
7603 now. */
269137b2 7604 load_delay_nop ();
252b5132 7605 }
252b5132
RH
7606 }
7607 else if (expr1.X_add_number >= -0x8000
7608 && expr1.X_add_number < 0x8000)
7609 {
269137b2 7610 load_delay_nop ();
67c0d1eb 7611 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7612 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
7613 }
7614 else
7615 {
252b5132
RH
7616 /* If we are going to add in a base register, and the
7617 target register and the base register are the same,
7618 then we are using AT as a temporary register. Since
7619 we want to load the constant into AT, we add our
7620 current AT (from the global offset table) and the
7621 register into the register now, and pretend we were
7622 not using a base register. */
7623 if (breg != treg)
67c0d1eb 7624 dreg = tempreg;
252b5132
RH
7625 else
7626 {
9c2799c2 7627 gas_assert (tempreg == AT);
269137b2 7628 load_delay_nop ();
67c0d1eb 7629 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7630 treg, AT, breg);
252b5132 7631 dreg = treg;
252b5132
RH
7632 }
7633
f6a22291 7634 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7635 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 7636
252b5132
RH
7637 used_at = 1;
7638 }
43c0598f 7639 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 7640 relax_switch ();
252b5132 7641
67c0d1eb 7642 if (gpdelay)
252b5132
RH
7643 {
7644 /* This is needed because this instruction uses $gp, but
f5040a92 7645 the first instruction on the main stream does not. */
67c0d1eb 7646 macro_build (NULL, "nop", "");
252b5132 7647 }
ed6fb7bd 7648
67c0d1eb
RS
7649 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7650 local_reloc_type, mips_gp_register);
f5040a92 7651 if (expr1.X_add_number >= -0x8000
252b5132
RH
7652 && expr1.X_add_number < 0x8000)
7653 {
269137b2 7654 load_delay_nop ();
67c0d1eb
RS
7655 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7656 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 7657 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
7658 register, the external symbol case ended with a load,
7659 so if the symbol turns out to not be external, and
7660 the next instruction uses tempreg, an unnecessary nop
7661 will be inserted. */
252b5132
RH
7662 }
7663 else
7664 {
7665 if (breg == treg)
7666 {
7667 /* We must add in the base register now, as in the
f5040a92 7668 external symbol case. */
9c2799c2 7669 gas_assert (tempreg == AT);
269137b2 7670 load_delay_nop ();
67c0d1eb 7671 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7672 treg, AT, breg);
252b5132
RH
7673 tempreg = treg;
7674 /* We set breg to 0 because we have arranged to add
f5040a92 7675 it in in both cases. */
252b5132
RH
7676 breg = 0;
7677 }
7678
67c0d1eb
RS
7679 macro_build_lui (&expr1, AT);
7680 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7681 AT, AT, BFD_RELOC_LO16);
67c0d1eb 7682 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7683 tempreg, tempreg, AT);
8fc2e39e 7684 used_at = 1;
252b5132 7685 }
4d7206a2 7686 relax_end ();
252b5132 7687 }
0a44bf69 7688 else if (mips_big_got && HAVE_NEWABI)
f5040a92 7689 {
f5040a92
AO
7690 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7691 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 7692 int add_breg_early = 0;
f5040a92
AO
7693
7694 /* This is the large GOT case. If this is a reference to an
7695 external symbol, and there is no constant, we want
7696 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7697 add $tempreg,$tempreg,$gp
7698 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 7699 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
7700 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7701 add $tempreg,$tempreg,$gp
7702 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7703
7704 If we have a small constant, and this is a reference to
7705 an external symbol, we want
7706 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7707 add $tempreg,$tempreg,$gp
7708 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7709 addi $tempreg,$tempreg,<constant>
7710
7711 If we have a large constant, and this is a reference to
7712 an external symbol, we want
7713 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7714 addu $tempreg,$tempreg,$gp
7715 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7716 lui $at,<hiconstant>
7717 addi $at,$at,<loconstant>
7718 add $tempreg,$tempreg,$at
7719
7720 If we have NewABI, and we know it's a local symbol, we want
7721 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
7722 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
7723 otherwise we have to resort to GOT_HI16/GOT_LO16. */
7724
4d7206a2 7725 relax_start (offset_expr.X_add_symbol);
f5040a92 7726
4d7206a2 7727 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
7728 offset_expr.X_add_number = 0;
7729
1abe91b1
MR
7730 if (expr1.X_add_number == 0 && breg == 0
7731 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
7732 {
7733 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7734 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7735 }
df58fc94 7736 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 7737 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7738 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
7739 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7740 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
7741
7742 if (expr1.X_add_number == 0)
4d7206a2 7743 ;
f5040a92
AO
7744 else if (expr1.X_add_number >= -0x8000
7745 && expr1.X_add_number < 0x8000)
7746 {
67c0d1eb 7747 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7748 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 7749 }
ecd13cd3 7750 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 7751 {
f5040a92
AO
7752 /* If we are going to add in a base register, and the
7753 target register and the base register are the same,
7754 then we are using AT as a temporary register. Since
7755 we want to load the constant into AT, we add our
7756 current AT (from the global offset table) and the
7757 register into the register now, and pretend we were
7758 not using a base register. */
7759 if (breg != treg)
7760 dreg = tempreg;
7761 else
7762 {
9c2799c2 7763 gas_assert (tempreg == AT);
67c0d1eb 7764 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7765 treg, AT, breg);
f5040a92 7766 dreg = treg;
67c0d1eb 7767 add_breg_early = 1;
f5040a92
AO
7768 }
7769
f6a22291 7770 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7771 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 7772
f5040a92
AO
7773 used_at = 1;
7774 }
7775 else
7776 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7777
4d7206a2 7778 relax_switch ();
f5040a92 7779 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
7780 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7781 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
7782 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
7783 tempreg, BFD_RELOC_MIPS_GOT_OFST);
7784 if (add_breg_early)
f5040a92 7785 {
67c0d1eb 7786 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7787 treg, tempreg, breg);
f5040a92
AO
7788 breg = 0;
7789 tempreg = treg;
7790 }
4d7206a2 7791 relax_end ();
f5040a92 7792 }
252b5132
RH
7793 else
7794 abort ();
7795
7796 if (breg != 0)
aed1a261 7797 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
7798 break;
7799
52b6b6b9 7800 case M_MSGSND:
df58fc94 7801 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7802 {
7803 unsigned long temp = (treg << 16) | (0x01);
7804 macro_build (NULL, "c2", "C", temp);
7805 }
c7af4273 7806 break;
52b6b6b9
JM
7807
7808 case M_MSGLD:
df58fc94 7809 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7810 {
7811 unsigned long temp = (0x02);
7812 macro_build (NULL, "c2", "C", temp);
7813 }
c7af4273 7814 break;
52b6b6b9
JM
7815
7816 case M_MSGLD_T:
df58fc94 7817 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7818 {
7819 unsigned long temp = (treg << 16) | (0x02);
7820 macro_build (NULL, "c2", "C", temp);
7821 }
c7af4273 7822 break;
52b6b6b9
JM
7823
7824 case M_MSGWAIT:
df58fc94 7825 gas_assert (!mips_opts.micromips);
52b6b6b9 7826 macro_build (NULL, "c2", "C", 3);
c7af4273 7827 break;
52b6b6b9
JM
7828
7829 case M_MSGWAIT_T:
df58fc94 7830 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
7831 {
7832 unsigned long temp = (treg << 16) | 0x03;
7833 macro_build (NULL, "c2", "C", temp);
7834 }
c7af4273 7835 break;
52b6b6b9 7836
252b5132
RH
7837 case M_J_A:
7838 /* The j instruction may not be used in PIC code, since it
7839 requires an absolute address. We convert it to a b
7840 instruction. */
7841 if (mips_pic == NO_PIC)
67c0d1eb 7842 macro_build (&offset_expr, "j", "a");
252b5132 7843 else
67c0d1eb 7844 macro_build (&offset_expr, "b", "p");
8fc2e39e 7845 break;
252b5132
RH
7846
7847 /* The jal instructions must be handled as macros because when
7848 generating PIC code they expand to multi-instruction
7849 sequences. Normally they are simple instructions. */
df58fc94
RS
7850 case M_JALS_1:
7851 dreg = RA;
7852 /* Fall through. */
7853 case M_JALS_2:
7854 gas_assert (mips_opts.micromips);
7855 jals = 1;
7856 goto jal;
252b5132
RH
7857 case M_JAL_1:
7858 dreg = RA;
7859 /* Fall through. */
7860 case M_JAL_2:
df58fc94 7861 jal:
3e722fb5 7862 if (mips_pic == NO_PIC)
df58fc94
RS
7863 {
7864 s = jals ? "jalrs" : "jalr";
e64af278
MR
7865 if (mips_opts.micromips
7866 && dreg == RA
7867 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
7868 macro_build (NULL, s, "mj", sreg);
7869 else
7870 macro_build (NULL, s, JALR_FMT, dreg, sreg);
7871 }
0a44bf69 7872 else
252b5132 7873 {
df58fc94
RS
7874 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
7875 && mips_cprestore_offset >= 0);
7876
252b5132
RH
7877 if (sreg != PIC_CALL_REG)
7878 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 7879
df58fc94
RS
7880 s = (mips_opts.micromips && (!mips_opts.noreorder || cprestore)
7881 ? "jalrs" : "jalr");
e64af278
MR
7882 if (mips_opts.micromips
7883 && dreg == RA
7884 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
7885 macro_build (NULL, s, "mj", sreg);
7886 else
7887 macro_build (NULL, s, JALR_FMT, dreg, sreg);
0a44bf69 7888 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 7889 {
6478892d
TS
7890 if (mips_cprestore_offset < 0)
7891 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7892 else
7893 {
90ecf173 7894 if (!mips_frame_reg_valid)
7a621144
DJ
7895 {
7896 as_warn (_("No .frame pseudo-op used in PIC code"));
7897 /* Quiet this warning. */
7898 mips_frame_reg_valid = 1;
7899 }
90ecf173 7900 if (!mips_cprestore_valid)
7a621144
DJ
7901 {
7902 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7903 /* Quiet this warning. */
7904 mips_cprestore_valid = 1;
7905 }
d3fca0b5
MR
7906 if (mips_opts.noreorder)
7907 macro_build (NULL, "nop", "");
6478892d 7908 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 7909 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 7910 mips_gp_register,
256ab948
TS
7911 mips_frame_reg,
7912 HAVE_64BIT_ADDRESSES);
6478892d 7913 }
252b5132
RH
7914 }
7915 }
252b5132 7916
8fc2e39e 7917 break;
252b5132 7918
df58fc94
RS
7919 case M_JALS_A:
7920 gas_assert (mips_opts.micromips);
7921 jals = 1;
7922 /* Fall through. */
252b5132
RH
7923 case M_JAL_A:
7924 if (mips_pic == NO_PIC)
df58fc94 7925 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
7926 else if (mips_pic == SVR4_PIC)
7927 {
7928 /* If this is a reference to an external symbol, and we are
7929 using a small GOT, we want
7930 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7931 nop
f9419b05 7932 jalr $ra,$25
252b5132
RH
7933 nop
7934 lw $gp,cprestore($sp)
7935 The cprestore value is set using the .cprestore
7936 pseudo-op. If we are using a big GOT, we want
7937 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7938 addu $25,$25,$gp
7939 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
7940 nop
f9419b05 7941 jalr $ra,$25
252b5132
RH
7942 nop
7943 lw $gp,cprestore($sp)
7944 If the symbol is not external, we want
7945 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7946 nop
7947 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 7948 jalr $ra,$25
252b5132 7949 nop
438c16b8 7950 lw $gp,cprestore($sp)
f5040a92
AO
7951
7952 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
7953 sequences above, minus nops, unless the symbol is local,
7954 which enables us to use GOT_PAGE/GOT_OFST (big got) or
7955 GOT_DISP. */
438c16b8 7956 if (HAVE_NEWABI)
252b5132 7957 {
90ecf173 7958 if (!mips_big_got)
f5040a92 7959 {
4d7206a2 7960 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7961 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7962 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 7963 mips_gp_register);
4d7206a2 7964 relax_switch ();
67c0d1eb
RS
7965 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7966 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
7967 mips_gp_register);
7968 relax_end ();
f5040a92
AO
7969 }
7970 else
7971 {
4d7206a2 7972 relax_start (offset_expr.X_add_symbol);
df58fc94 7973 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
7974 BFD_RELOC_MIPS_CALL_HI16);
7975 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
7976 PIC_CALL_REG, mips_gp_register);
7977 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7978 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
7979 PIC_CALL_REG);
4d7206a2 7980 relax_switch ();
67c0d1eb
RS
7981 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7982 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
7983 mips_gp_register);
7984 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7985 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 7986 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 7987 relax_end ();
f5040a92 7988 }
684022ea 7989
df58fc94 7990 macro_build_jalr (&offset_expr, 0);
252b5132
RH
7991 }
7992 else
7993 {
4d7206a2 7994 relax_start (offset_expr.X_add_symbol);
90ecf173 7995 if (!mips_big_got)
438c16b8 7996 {
67c0d1eb
RS
7997 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7998 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 7999 mips_gp_register);
269137b2 8000 load_delay_nop ();
4d7206a2 8001 relax_switch ();
438c16b8 8002 }
252b5132 8003 else
252b5132 8004 {
67c0d1eb
RS
8005 int gpdelay;
8006
8007 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 8008 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
8009 BFD_RELOC_MIPS_CALL_HI16);
8010 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8011 PIC_CALL_REG, mips_gp_register);
8012 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8013 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8014 PIC_CALL_REG);
269137b2 8015 load_delay_nop ();
4d7206a2 8016 relax_switch ();
67c0d1eb
RS
8017 if (gpdelay)
8018 macro_build (NULL, "nop", "");
252b5132 8019 }
67c0d1eb
RS
8020 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8021 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 8022 mips_gp_register);
269137b2 8023 load_delay_nop ();
67c0d1eb
RS
8024 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8025 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 8026 relax_end ();
df58fc94 8027 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 8028
6478892d
TS
8029 if (mips_cprestore_offset < 0)
8030 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8031 else
8032 {
90ecf173 8033 if (!mips_frame_reg_valid)
7a621144
DJ
8034 {
8035 as_warn (_("No .frame pseudo-op used in PIC code"));
8036 /* Quiet this warning. */
8037 mips_frame_reg_valid = 1;
8038 }
90ecf173 8039 if (!mips_cprestore_valid)
7a621144
DJ
8040 {
8041 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8042 /* Quiet this warning. */
8043 mips_cprestore_valid = 1;
8044 }
6478892d 8045 if (mips_opts.noreorder)
67c0d1eb 8046 macro_build (NULL, "nop", "");
6478892d 8047 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 8048 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 8049 mips_gp_register,
256ab948
TS
8050 mips_frame_reg,
8051 HAVE_64BIT_ADDRESSES);
6478892d 8052 }
252b5132
RH
8053 }
8054 }
0a44bf69
RS
8055 else if (mips_pic == VXWORKS_PIC)
8056 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
8057 else
8058 abort ();
8059
8fc2e39e 8060 break;
252b5132 8061
7f3c4072
CM
8062 case M_LBUE_AB:
8063 ab = 1;
8064 case M_LBUE_OB:
8065 s = "lbue";
8066 fmt = "t,+j(b)";
8067 offbits = 9;
8068 goto ld_st;
8069 case M_LHUE_AB:
8070 ab = 1;
8071 case M_LHUE_OB:
8072 s = "lhue";
8073 fmt = "t,+j(b)";
8074 offbits = 9;
8075 goto ld_st;
8076 case M_LBE_AB:
8077 ab = 1;
8078 case M_LBE_OB:
8079 s = "lbe";
8080 fmt = "t,+j(b)";
8081 offbits = 9;
8082 goto ld_st;
8083 case M_LHE_AB:
8084 ab = 1;
8085 case M_LHE_OB:
8086 s = "lhe";
8087 fmt = "t,+j(b)";
8088 offbits = 9;
8089 goto ld_st;
8090 case M_LLE_AB:
8091 ab = 1;
8092 case M_LLE_OB:
8093 s = "lle";
8094 fmt = "t,+j(b)";
8095 offbits = 9;
8096 goto ld_st;
8097 case M_LWE_AB:
8098 ab = 1;
8099 case M_LWE_OB:
8100 s = "lwe";
8101 fmt = "t,+j(b)";
8102 offbits = 9;
8103 goto ld_st;
8104 case M_LWLE_AB:
8105 ab = 1;
8106 case M_LWLE_OB:
8107 s = "lwle";
8108 fmt = "t,+j(b)";
8109 offbits = 9;
8110 goto ld_st;
8111 case M_LWRE_AB:
8112 ab = 1;
8113 case M_LWRE_OB:
8114 s = "lwre";
8115 fmt = "t,+j(b)";
8116 offbits = 9;
8117 goto ld_st;
8118 case M_SBE_AB:
8119 ab = 1;
8120 case M_SBE_OB:
8121 s = "sbe";
8122 fmt = "t,+j(b)";
8123 offbits = 9;
8124 goto ld_st;
8125 case M_SCE_AB:
8126 ab = 1;
8127 case M_SCE_OB:
8128 s = "sce";
8129 fmt = "t,+j(b)";
8130 offbits = 9;
8131 goto ld_st;
8132 case M_SHE_AB:
8133 ab = 1;
8134 case M_SHE_OB:
8135 s = "she";
8136 fmt = "t,+j(b)";
8137 offbits = 9;
8138 goto ld_st;
8139 case M_SWE_AB:
8140 ab = 1;
8141 case M_SWE_OB:
8142 s = "swe";
8143 fmt = "t,+j(b)";
8144 offbits = 9;
8145 goto ld_st;
8146 case M_SWLE_AB:
8147 ab = 1;
8148 case M_SWLE_OB:
8149 s = "swle";
8150 fmt = "t,+j(b)";
8151 offbits = 9;
8152 goto ld_st;
8153 case M_SWRE_AB:
8154 ab = 1;
8155 case M_SWRE_OB:
8156 s = "swre";
8157 fmt = "t,+j(b)";
8158 offbits = 9;
8159 goto ld_st;
dec0624d
MR
8160 case M_ACLR_AB:
8161 ab = 1;
8162 case M_ACLR_OB:
8163 s = "aclr";
8164 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8165 fmt = "\\,~(b)";
7f3c4072 8166 offbits = 12;
dec0624d
MR
8167 goto ld_st;
8168 case M_ASET_AB:
8169 ab = 1;
8170 case M_ASET_OB:
8171 s = "aset";
8172 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8173 fmt = "\\,~(b)";
7f3c4072 8174 offbits = 12;
dec0624d 8175 goto ld_st;
252b5132 8176 case M_LB_AB:
df58fc94 8177 ab = 1;
252b5132 8178 s = "lb";
df58fc94 8179 fmt = "t,o(b)";
252b5132
RH
8180 goto ld;
8181 case M_LBU_AB:
df58fc94 8182 ab = 1;
252b5132 8183 s = "lbu";
df58fc94 8184 fmt = "t,o(b)";
252b5132
RH
8185 goto ld;
8186 case M_LH_AB:
df58fc94 8187 ab = 1;
252b5132 8188 s = "lh";
df58fc94 8189 fmt = "t,o(b)";
252b5132
RH
8190 goto ld;
8191 case M_LHU_AB:
df58fc94 8192 ab = 1;
252b5132 8193 s = "lhu";
df58fc94 8194 fmt = "t,o(b)";
252b5132
RH
8195 goto ld;
8196 case M_LW_AB:
df58fc94 8197 ab = 1;
252b5132 8198 s = "lw";
df58fc94 8199 fmt = "t,o(b)";
252b5132
RH
8200 goto ld;
8201 case M_LWC0_AB:
df58fc94
RS
8202 ab = 1;
8203 gas_assert (!mips_opts.micromips);
252b5132 8204 s = "lwc0";
df58fc94 8205 fmt = "E,o(b)";
bdaaa2e1 8206 /* Itbl support may require additional care here. */
252b5132 8207 coproc = 1;
df58fc94 8208 goto ld_st;
252b5132 8209 case M_LWC1_AB:
df58fc94 8210 ab = 1;
252b5132 8211 s = "lwc1";
df58fc94 8212 fmt = "T,o(b)";
bdaaa2e1 8213 /* Itbl support may require additional care here. */
252b5132 8214 coproc = 1;
df58fc94 8215 goto ld_st;
252b5132 8216 case M_LWC2_AB:
df58fc94
RS
8217 ab = 1;
8218 case M_LWC2_OB:
252b5132 8219 s = "lwc2";
df58fc94 8220 fmt = COP12_FMT;
7f3c4072 8221 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 8222 /* Itbl support may require additional care here. */
252b5132 8223 coproc = 1;
df58fc94 8224 goto ld_st;
252b5132 8225 case M_LWC3_AB:
df58fc94
RS
8226 ab = 1;
8227 gas_assert (!mips_opts.micromips);
252b5132 8228 s = "lwc3";
df58fc94 8229 fmt = "E,o(b)";
bdaaa2e1 8230 /* Itbl support may require additional care here. */
252b5132 8231 coproc = 1;
df58fc94 8232 goto ld_st;
252b5132 8233 case M_LWL_AB:
df58fc94
RS
8234 ab = 1;
8235 case M_LWL_OB:
252b5132 8236 s = "lwl";
df58fc94 8237 fmt = MEM12_FMT;
7f3c4072 8238 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8239 goto ld_st;
252b5132 8240 case M_LWR_AB:
df58fc94
RS
8241 ab = 1;
8242 case M_LWR_OB:
252b5132 8243 s = "lwr";
df58fc94 8244 fmt = MEM12_FMT;
7f3c4072 8245 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8246 goto ld_st;
252b5132 8247 case M_LDC1_AB:
df58fc94 8248 ab = 1;
252b5132 8249 s = "ldc1";
df58fc94 8250 fmt = "T,o(b)";
bdaaa2e1 8251 /* Itbl support may require additional care here. */
252b5132 8252 coproc = 1;
df58fc94 8253 goto ld_st;
252b5132 8254 case M_LDC2_AB:
df58fc94
RS
8255 ab = 1;
8256 case M_LDC2_OB:
252b5132 8257 s = "ldc2";
df58fc94 8258 fmt = COP12_FMT;
7f3c4072 8259 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 8260 /* Itbl support may require additional care here. */
252b5132 8261 coproc = 1;
df58fc94 8262 goto ld_st;
c77c0862
RS
8263 case M_LQC2_AB:
8264 ab = 1;
8265 s = "lqc2";
8266 fmt = "E,o(b)";
8267 /* Itbl support may require additional care here. */
8268 coproc = 1;
8269 goto ld_st;
252b5132 8270 case M_LDC3_AB:
df58fc94 8271 ab = 1;
252b5132 8272 s = "ldc3";
df58fc94 8273 fmt = "E,o(b)";
bdaaa2e1 8274 /* Itbl support may require additional care here. */
252b5132 8275 coproc = 1;
df58fc94 8276 goto ld_st;
252b5132 8277 case M_LDL_AB:
df58fc94
RS
8278 ab = 1;
8279 case M_LDL_OB:
252b5132 8280 s = "ldl";
df58fc94 8281 fmt = MEM12_FMT;
7f3c4072 8282 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8283 goto ld_st;
252b5132 8284 case M_LDR_AB:
df58fc94
RS
8285 ab = 1;
8286 case M_LDR_OB:
252b5132 8287 s = "ldr";
df58fc94 8288 fmt = MEM12_FMT;
7f3c4072 8289 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8290 goto ld_st;
252b5132 8291 case M_LL_AB:
df58fc94
RS
8292 ab = 1;
8293 case M_LL_OB:
252b5132 8294 s = "ll";
df58fc94 8295 fmt = MEM12_FMT;
7f3c4072 8296 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
8297 goto ld;
8298 case M_LLD_AB:
df58fc94
RS
8299 ab = 1;
8300 case M_LLD_OB:
252b5132 8301 s = "lld";
df58fc94 8302 fmt = MEM12_FMT;
7f3c4072 8303 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
8304 goto ld;
8305 case M_LWU_AB:
df58fc94
RS
8306 ab = 1;
8307 case M_LWU_OB:
252b5132 8308 s = "lwu";
df58fc94 8309 fmt = MEM12_FMT;
7f3c4072 8310 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
8311 goto ld;
8312 case M_LWP_AB:
8313 ab = 1;
8314 case M_LWP_OB:
8315 gas_assert (mips_opts.micromips);
8316 s = "lwp";
8317 fmt = "t,~(b)";
7f3c4072 8318 offbits = 12;
df58fc94
RS
8319 lp = 1;
8320 goto ld;
8321 case M_LDP_AB:
8322 ab = 1;
8323 case M_LDP_OB:
8324 gas_assert (mips_opts.micromips);
8325 s = "ldp";
8326 fmt = "t,~(b)";
7f3c4072 8327 offbits = 12;
df58fc94
RS
8328 lp = 1;
8329 goto ld;
8330 case M_LWM_AB:
8331 ab = 1;
8332 case M_LWM_OB:
8333 gas_assert (mips_opts.micromips);
8334 s = "lwm";
8335 fmt = "n,~(b)";
7f3c4072 8336 offbits = 12;
df58fc94
RS
8337 goto ld_st;
8338 case M_LDM_AB:
8339 ab = 1;
8340 case M_LDM_OB:
8341 gas_assert (mips_opts.micromips);
8342 s = "ldm";
8343 fmt = "n,~(b)";
7f3c4072 8344 offbits = 12;
df58fc94
RS
8345 goto ld_st;
8346
252b5132 8347 ld:
f19ccbda
MR
8348 /* We don't want to use $0 as tempreg. */
8349 if (breg == treg + lp || treg + lp == ZERO)
df58fc94 8350 goto ld_st;
252b5132 8351 else
df58fc94
RS
8352 tempreg = treg + lp;
8353 goto ld_noat;
8354
252b5132 8355 case M_SB_AB:
df58fc94 8356 ab = 1;
252b5132 8357 s = "sb";
df58fc94
RS
8358 fmt = "t,o(b)";
8359 goto ld_st;
252b5132 8360 case M_SH_AB:
df58fc94 8361 ab = 1;
252b5132 8362 s = "sh";
df58fc94
RS
8363 fmt = "t,o(b)";
8364 goto ld_st;
252b5132 8365 case M_SW_AB:
df58fc94 8366 ab = 1;
252b5132 8367 s = "sw";
df58fc94
RS
8368 fmt = "t,o(b)";
8369 goto ld_st;
252b5132 8370 case M_SWC0_AB:
df58fc94
RS
8371 ab = 1;
8372 gas_assert (!mips_opts.micromips);
252b5132 8373 s = "swc0";
df58fc94 8374 fmt = "E,o(b)";
bdaaa2e1 8375 /* Itbl support may require additional care here. */
252b5132 8376 coproc = 1;
df58fc94 8377 goto ld_st;
252b5132 8378 case M_SWC1_AB:
df58fc94 8379 ab = 1;
252b5132 8380 s = "swc1";
df58fc94 8381 fmt = "T,o(b)";
bdaaa2e1 8382 /* Itbl support may require additional care here. */
252b5132 8383 coproc = 1;
df58fc94 8384 goto ld_st;
252b5132 8385 case M_SWC2_AB:
df58fc94
RS
8386 ab = 1;
8387 case M_SWC2_OB:
252b5132 8388 s = "swc2";
df58fc94 8389 fmt = COP12_FMT;
7f3c4072 8390 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 8391 /* Itbl support may require additional care here. */
252b5132 8392 coproc = 1;
df58fc94 8393 goto ld_st;
252b5132 8394 case M_SWC3_AB:
df58fc94
RS
8395 ab = 1;
8396 gas_assert (!mips_opts.micromips);
252b5132 8397 s = "swc3";
df58fc94 8398 fmt = "E,o(b)";
bdaaa2e1 8399 /* Itbl support may require additional care here. */
252b5132 8400 coproc = 1;
df58fc94 8401 goto ld_st;
252b5132 8402 case M_SWL_AB:
df58fc94
RS
8403 ab = 1;
8404 case M_SWL_OB:
252b5132 8405 s = "swl";
df58fc94 8406 fmt = MEM12_FMT;
7f3c4072 8407 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8408 goto ld_st;
252b5132 8409 case M_SWR_AB:
df58fc94
RS
8410 ab = 1;
8411 case M_SWR_OB:
252b5132 8412 s = "swr";
df58fc94 8413 fmt = MEM12_FMT;
7f3c4072 8414 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8415 goto ld_st;
252b5132 8416 case M_SC_AB:
df58fc94
RS
8417 ab = 1;
8418 case M_SC_OB:
252b5132 8419 s = "sc";
df58fc94 8420 fmt = MEM12_FMT;
7f3c4072 8421 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8422 goto ld_st;
252b5132 8423 case M_SCD_AB:
df58fc94
RS
8424 ab = 1;
8425 case M_SCD_OB:
252b5132 8426 s = "scd";
df58fc94 8427 fmt = MEM12_FMT;
7f3c4072 8428 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8429 goto ld_st;
d43b4baf 8430 case M_CACHE_AB:
df58fc94
RS
8431 ab = 1;
8432 case M_CACHE_OB:
d43b4baf 8433 s = "cache";
df58fc94 8434 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
7f3c4072
CM
8435 offbits = (mips_opts.micromips ? 12 : 16);
8436 goto ld_st;
8437 case M_CACHEE_AB:
8438 ab = 1;
8439 case M_CACHEE_OB:
8440 s = "cachee";
8441 fmt = "k,+j(b)";
8442 offbits = 9;
df58fc94 8443 goto ld_st;
3eebd5eb 8444 case M_PREF_AB:
df58fc94
RS
8445 ab = 1;
8446 case M_PREF_OB:
3eebd5eb 8447 s = "pref";
df58fc94 8448 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
7f3c4072
CM
8449 offbits = (mips_opts.micromips ? 12 : 16);
8450 goto ld_st;
8451 case M_PREFE_AB:
8452 ab = 1;
8453 case M_PREFE_OB:
8454 s = "prefe";
8455 fmt = "k,+j(b)";
8456 offbits = 9;
df58fc94 8457 goto ld_st;
252b5132 8458 case M_SDC1_AB:
df58fc94 8459 ab = 1;
252b5132 8460 s = "sdc1";
df58fc94 8461 fmt = "T,o(b)";
252b5132 8462 coproc = 1;
bdaaa2e1 8463 /* Itbl support may require additional care here. */
df58fc94 8464 goto ld_st;
252b5132 8465 case M_SDC2_AB:
df58fc94
RS
8466 ab = 1;
8467 case M_SDC2_OB:
252b5132 8468 s = "sdc2";
df58fc94 8469 fmt = COP12_FMT;
7f3c4072 8470 offbits = (mips_opts.micromips ? 12 : 16);
c77c0862
RS
8471 /* Itbl support may require additional care here. */
8472 coproc = 1;
8473 goto ld_st;
8474 case M_SQC2_AB:
8475 ab = 1;
8476 s = "sqc2";
8477 fmt = "E,o(b)";
bdaaa2e1 8478 /* Itbl support may require additional care here. */
252b5132 8479 coproc = 1;
df58fc94 8480 goto ld_st;
252b5132 8481 case M_SDC3_AB:
df58fc94
RS
8482 ab = 1;
8483 gas_assert (!mips_opts.micromips);
252b5132 8484 s = "sdc3";
df58fc94 8485 fmt = "E,o(b)";
bdaaa2e1 8486 /* Itbl support may require additional care here. */
252b5132 8487 coproc = 1;
df58fc94 8488 goto ld_st;
252b5132 8489 case M_SDL_AB:
df58fc94
RS
8490 ab = 1;
8491 case M_SDL_OB:
252b5132 8492 s = "sdl";
df58fc94 8493 fmt = MEM12_FMT;
7f3c4072 8494 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8495 goto ld_st;
252b5132 8496 case M_SDR_AB:
df58fc94
RS
8497 ab = 1;
8498 case M_SDR_OB:
252b5132 8499 s = "sdr";
df58fc94 8500 fmt = MEM12_FMT;
7f3c4072 8501 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
8502 goto ld_st;
8503 case M_SWP_AB:
8504 ab = 1;
8505 case M_SWP_OB:
8506 gas_assert (mips_opts.micromips);
8507 s = "swp";
8508 fmt = "t,~(b)";
7f3c4072 8509 offbits = 12;
df58fc94
RS
8510 goto ld_st;
8511 case M_SDP_AB:
8512 ab = 1;
8513 case M_SDP_OB:
8514 gas_assert (mips_opts.micromips);
8515 s = "sdp";
8516 fmt = "t,~(b)";
7f3c4072 8517 offbits = 12;
df58fc94
RS
8518 goto ld_st;
8519 case M_SWM_AB:
8520 ab = 1;
8521 case M_SWM_OB:
8522 gas_assert (mips_opts.micromips);
8523 s = "swm";
8524 fmt = "n,~(b)";
7f3c4072 8525 offbits = 12;
df58fc94
RS
8526 goto ld_st;
8527 case M_SDM_AB:
8528 ab = 1;
8529 case M_SDM_OB:
8530 gas_assert (mips_opts.micromips);
8531 s = "sdm";
8532 fmt = "n,~(b)";
7f3c4072 8533 offbits = 12;
df58fc94
RS
8534
8535 ld_st:
8fc2e39e
TS
8536 tempreg = AT;
8537 used_at = 1;
df58fc94 8538 ld_noat:
252b5132
RH
8539 if (offset_expr.X_op != O_constant
8540 && offset_expr.X_op != O_symbol)
8541 {
f71d0d44 8542 as_bad (_("Expression too complex"));
252b5132
RH
8543 offset_expr.X_op = O_constant;
8544 }
8545
2051e8c4
MR
8546 if (HAVE_32BIT_ADDRESSES
8547 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
8548 {
8549 char value [32];
8550
8551 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 8552 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 8553 }
2051e8c4 8554
252b5132
RH
8555 /* A constant expression in PIC code can be handled just as it
8556 is in non PIC code. */
aed1a261
RS
8557 if (offset_expr.X_op == O_constant)
8558 {
df58fc94
RS
8559 int hipart = 0;
8560
842f8b2a 8561 expr1.X_add_number = offset_expr.X_add_number;
2051e8c4 8562 normalize_address_expr (&expr1);
7f3c4072
CM
8563 if ((offbits == 0 || offbits == 16)
8564 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
842f8b2a
MR
8565 {
8566 expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8567 & ~(bfd_vma) 0xffff);
df58fc94
RS
8568 hipart = 1;
8569 }
7f3c4072 8570 else if (offbits == 12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
df58fc94
RS
8571 {
8572 expr1.X_add_number = ((expr1.X_add_number + 0x800)
8573 & ~(bfd_vma) 0xfff);
8574 hipart = 1;
8575 }
7f3c4072
CM
8576 else if (offbits == 9 && !IS_SEXT_9BIT_NUM (expr1.X_add_number))
8577 {
8578 expr1.X_add_number = ((expr1.X_add_number + 0x100)
8579 & ~(bfd_vma) 0x1ff);
8580 hipart = 1;
8581 }
df58fc94
RS
8582 if (hipart)
8583 {
842f8b2a
MR
8584 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8585 if (breg != 0)
8586 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8587 tempreg, tempreg, breg);
8588 breg = tempreg;
8589 }
7f3c4072 8590 if (offbits == 0)
dd6a37e7
AP
8591 {
8592 if (offset_expr.X_add_number == 0)
8593 tempreg = breg;
8594 else
8595 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8596 "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8597 macro_build (NULL, s, fmt, treg, tempreg);
8598 }
7f3c4072 8599 else if (offbits == 16)
df58fc94
RS
8600 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8601 else
8602 macro_build (NULL, s, fmt,
8603 treg, (unsigned long) offset_expr.X_add_number, breg);
8604 }
7f3c4072 8605 else if (offbits != 16)
df58fc94 8606 {
7f3c4072
CM
8607 /* The offset field is too narrow to be used for a low-part
8608 relocation, so load the whole address into the auxillary
8609 register. In the case of "A(b)" addresses, we first load
8610 absolute address "A" into the register and then add base
8611 register "b". In the case of "o(b)" addresses, we simply
8612 need to add 16-bit offset "o" to base register "b", and
df58fc94
RS
8613 offset_reloc already contains the relocations associated
8614 with "o". */
8615 if (ab)
8616 {
8617 load_address (tempreg, &offset_expr, &used_at);
8618 if (breg != 0)
8619 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8620 tempreg, tempreg, breg);
8621 }
8622 else
8623 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8624 tempreg, breg, -1,
8625 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8626 expr1.X_add_number = 0;
7f3c4072 8627 if (offbits == 0)
dd6a37e7
AP
8628 macro_build (NULL, s, fmt, treg, tempreg);
8629 else
8630 macro_build (NULL, s, fmt,
8631 treg, (unsigned long) expr1.X_add_number, tempreg);
aed1a261
RS
8632 }
8633 else if (mips_pic == NO_PIC)
252b5132
RH
8634 {
8635 /* If this is a reference to a GP relative symbol, and there
8636 is no base register, we want
cdf6fd85 8637 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
8638 Otherwise, if there is no base register, we want
8639 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8640 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8641 If we have a constant, we need two instructions anyhow,
8642 so we always use the latter form.
8643
8644 If we have a base register, and this is a reference to a
8645 GP relative symbol, we want
8646 addu $tempreg,$breg,$gp
cdf6fd85 8647 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
8648 Otherwise we want
8649 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8650 addu $tempreg,$tempreg,$breg
8651 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 8652 With a constant we always use the latter case.
76b3015f 8653
d6bc6245
TS
8654 With 64bit address space and no base register and $at usable,
8655 we want
8656 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8657 lui $at,<sym> (BFD_RELOC_HI16_S)
8658 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8659 dsll32 $tempreg,0
8660 daddu $tempreg,$at
8661 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8662 If we have a base register, we want
8663 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8664 lui $at,<sym> (BFD_RELOC_HI16_S)
8665 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8666 daddu $at,$breg
8667 dsll32 $tempreg,0
8668 daddu $tempreg,$at
8669 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8670
8671 Without $at we can't generate the optimal path for superscalar
8672 processors here since this would require two temporary registers.
8673 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8674 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8675 dsll $tempreg,16
8676 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8677 dsll $tempreg,16
8678 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8679 If we have a base register, we want
8680 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8681 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8682 dsll $tempreg,16
8683 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
8684 dsll $tempreg,16
8685 daddu $tempreg,$tempreg,$breg
8686 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 8687
6caf9ef4 8688 For GP relative symbols in 64bit address space we can use
aed1a261
RS
8689 the same sequence as in 32bit address space. */
8690 if (HAVE_64BIT_SYMBOLS)
d6bc6245 8691 {
aed1a261 8692 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
8693 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8694 {
8695 relax_start (offset_expr.X_add_symbol);
8696 if (breg == 0)
8697 {
8698 macro_build (&offset_expr, s, fmt, treg,
8699 BFD_RELOC_GPREL16, mips_gp_register);
8700 }
8701 else
8702 {
8703 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8704 tempreg, breg, mips_gp_register);
8705 macro_build (&offset_expr, s, fmt, treg,
8706 BFD_RELOC_GPREL16, tempreg);
8707 }
8708 relax_switch ();
8709 }
d6bc6245 8710
741fe287 8711 if (used_at == 0 && mips_opts.at)
d6bc6245 8712 {
df58fc94 8713 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 8714 BFD_RELOC_MIPS_HIGHEST);
df58fc94 8715 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
8716 BFD_RELOC_HI16_S);
8717 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8718 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 8719 if (breg != 0)
67c0d1eb 8720 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 8721 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb
RS
8722 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
8723 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
8724 tempreg);
d6bc6245
TS
8725 used_at = 1;
8726 }
8727 else
8728 {
df58fc94 8729 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
8730 BFD_RELOC_MIPS_HIGHEST);
8731 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8732 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 8733 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
8734 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8735 tempreg, BFD_RELOC_HI16_S);
df58fc94 8736 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 8737 if (breg != 0)
67c0d1eb 8738 macro_build (NULL, "daddu", "d,v,t",
17a2f251 8739 tempreg, tempreg, breg);
67c0d1eb 8740 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8741 BFD_RELOC_LO16, tempreg);
d6bc6245 8742 }
6caf9ef4
TS
8743
8744 if (mips_relax.sequence)
8745 relax_end ();
8fc2e39e 8746 break;
d6bc6245 8747 }
256ab948 8748
252b5132
RH
8749 if (breg == 0)
8750 {
67c0d1eb 8751 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8752 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 8753 {
4d7206a2 8754 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
8755 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
8756 mips_gp_register);
4d7206a2 8757 relax_switch ();
252b5132 8758 }
67c0d1eb
RS
8759 macro_build_lui (&offset_expr, tempreg);
8760 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8761 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
8762 if (mips_relax.sequence)
8763 relax_end ();
252b5132
RH
8764 }
8765 else
8766 {
67c0d1eb 8767 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 8768 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 8769 {
4d7206a2 8770 relax_start (offset_expr.X_add_symbol);
67c0d1eb 8771 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8772 tempreg, breg, mips_gp_register);
67c0d1eb 8773 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8774 BFD_RELOC_GPREL16, tempreg);
4d7206a2 8775 relax_switch ();
252b5132 8776 }
67c0d1eb
RS
8777 macro_build_lui (&offset_expr, tempreg);
8778 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8779 tempreg, tempreg, breg);
67c0d1eb 8780 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8781 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
8782 if (mips_relax.sequence)
8783 relax_end ();
252b5132
RH
8784 }
8785 }
0a44bf69 8786 else if (!mips_big_got)
252b5132 8787 {
ed6fb7bd 8788 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 8789
252b5132
RH
8790 /* If this is a reference to an external symbol, we want
8791 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8792 nop
8793 <op> $treg,0($tempreg)
8794 Otherwise we want
8795 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8796 nop
8797 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8798 <op> $treg,0($tempreg)
f5040a92
AO
8799
8800 For NewABI, we want
8801 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8802 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
8803
252b5132
RH
8804 If there is a base register, we add it to $tempreg before
8805 the <op>. If there is a constant, we stick it in the
8806 <op> instruction. We don't handle constants larger than
8807 16 bits, because we have no way to load the upper 16 bits
8808 (actually, we could handle them for the subset of cases
8809 in which we are not using $at). */
9c2799c2 8810 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
8811 if (HAVE_NEWABI)
8812 {
67c0d1eb
RS
8813 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8814 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 8815 if (breg != 0)
67c0d1eb 8816 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8817 tempreg, tempreg, breg);
67c0d1eb 8818 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8819 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
8820 break;
8821 }
252b5132
RH
8822 expr1.X_add_number = offset_expr.X_add_number;
8823 offset_expr.X_add_number = 0;
8824 if (expr1.X_add_number < -0x8000
8825 || expr1.X_add_number >= 0x8000)
8826 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
8827 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8828 lw_reloc_type, mips_gp_register);
269137b2 8829 load_delay_nop ();
4d7206a2
RS
8830 relax_start (offset_expr.X_add_symbol);
8831 relax_switch ();
67c0d1eb
RS
8832 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8833 tempreg, BFD_RELOC_LO16);
4d7206a2 8834 relax_end ();
252b5132 8835 if (breg != 0)
67c0d1eb 8836 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8837 tempreg, tempreg, breg);
67c0d1eb 8838 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 8839 }
0a44bf69 8840 else if (mips_big_got && !HAVE_NEWABI)
252b5132 8841 {
67c0d1eb 8842 int gpdelay;
252b5132
RH
8843
8844 /* If this is a reference to an external symbol, we want
8845 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8846 addu $tempreg,$tempreg,$gp
8847 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8848 <op> $treg,0($tempreg)
8849 Otherwise we want
8850 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8851 nop
8852 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8853 <op> $treg,0($tempreg)
8854 If there is a base register, we add it to $tempreg before
8855 the <op>. If there is a constant, we stick it in the
8856 <op> instruction. We don't handle constants larger than
8857 16 bits, because we have no way to load the upper 16 bits
8858 (actually, we could handle them for the subset of cases
f5040a92 8859 in which we are not using $at). */
9c2799c2 8860 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
8861 expr1.X_add_number = offset_expr.X_add_number;
8862 offset_expr.X_add_number = 0;
8863 if (expr1.X_add_number < -0x8000
8864 || expr1.X_add_number >= 0x8000)
8865 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 8866 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 8867 relax_start (offset_expr.X_add_symbol);
df58fc94 8868 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 8869 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8870 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8871 mips_gp_register);
8872 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8873 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 8874 relax_switch ();
67c0d1eb
RS
8875 if (gpdelay)
8876 macro_build (NULL, "nop", "");
8877 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8878 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 8879 load_delay_nop ();
67c0d1eb
RS
8880 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8881 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
8882 relax_end ();
8883
252b5132 8884 if (breg != 0)
67c0d1eb 8885 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8886 tempreg, tempreg, breg);
67c0d1eb 8887 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 8888 }
0a44bf69 8889 else if (mips_big_got && HAVE_NEWABI)
f5040a92 8890 {
f5040a92
AO
8891 /* If this is a reference to an external symbol, we want
8892 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8893 add $tempreg,$tempreg,$gp
8894 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8895 <op> $treg,<ofst>($tempreg)
8896 Otherwise, for local symbols, we want:
8897 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8898 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 8899 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 8900 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
8901 offset_expr.X_add_number = 0;
8902 if (expr1.X_add_number < -0x8000
8903 || expr1.X_add_number >= 0x8000)
8904 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 8905 relax_start (offset_expr.X_add_symbol);
df58fc94 8906 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 8907 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
8908 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8909 mips_gp_register);
8910 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8911 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 8912 if (breg != 0)
67c0d1eb 8913 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8914 tempreg, tempreg, breg);
67c0d1eb 8915 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 8916
4d7206a2 8917 relax_switch ();
f5040a92 8918 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
8919 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8920 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 8921 if (breg != 0)
67c0d1eb 8922 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8923 tempreg, tempreg, breg);
67c0d1eb 8924 macro_build (&offset_expr, s, fmt, treg,
17a2f251 8925 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 8926 relax_end ();
f5040a92 8927 }
252b5132
RH
8928 else
8929 abort ();
8930
252b5132
RH
8931 break;
8932
8933 case M_LI:
8934 case M_LI_S:
67c0d1eb 8935 load_register (treg, &imm_expr, 0);
8fc2e39e 8936 break;
252b5132
RH
8937
8938 case M_DLI:
67c0d1eb 8939 load_register (treg, &imm_expr, 1);
8fc2e39e 8940 break;
252b5132
RH
8941
8942 case M_LI_SS:
8943 if (imm_expr.X_op == O_constant)
8944 {
8fc2e39e 8945 used_at = 1;
67c0d1eb
RS
8946 load_register (AT, &imm_expr, 0);
8947 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
8948 break;
8949 }
8950 else
8951 {
9c2799c2 8952 gas_assert (offset_expr.X_op == O_symbol
90ecf173
MR
8953 && strcmp (segment_name (S_GET_SEGMENT
8954 (offset_expr.X_add_symbol)),
8955 ".lit4") == 0
8956 && offset_expr.X_add_number == 0);
67c0d1eb 8957 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 8958 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 8959 break;
252b5132
RH
8960 }
8961
8962 case M_LI_D:
ca4e0257
RS
8963 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
8964 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
8965 order 32 bits of the value and the low order 32 bits are either
8966 zero or in OFFSET_EXPR. */
252b5132
RH
8967 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8968 {
ca4e0257 8969 if (HAVE_64BIT_GPRS)
67c0d1eb 8970 load_register (treg, &imm_expr, 1);
252b5132
RH
8971 else
8972 {
8973 int hreg, lreg;
8974
8975 if (target_big_endian)
8976 {
8977 hreg = treg;
8978 lreg = treg + 1;
8979 }
8980 else
8981 {
8982 hreg = treg + 1;
8983 lreg = treg;
8984 }
8985
8986 if (hreg <= 31)
67c0d1eb 8987 load_register (hreg, &imm_expr, 0);
252b5132
RH
8988 if (lreg <= 31)
8989 {
8990 if (offset_expr.X_op == O_absent)
67c0d1eb 8991 move_register (lreg, 0);
252b5132
RH
8992 else
8993 {
9c2799c2 8994 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 8995 load_register (lreg, &offset_expr, 0);
252b5132
RH
8996 }
8997 }
8998 }
8fc2e39e 8999 break;
252b5132
RH
9000 }
9001
9002 /* We know that sym is in the .rdata section. First we get the
9003 upper 16 bits of the address. */
9004 if (mips_pic == NO_PIC)
9005 {
67c0d1eb 9006 macro_build_lui (&offset_expr, AT);
8fc2e39e 9007 used_at = 1;
252b5132 9008 }
0a44bf69 9009 else
252b5132 9010 {
67c0d1eb
RS
9011 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9012 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 9013 used_at = 1;
252b5132 9014 }
bdaaa2e1 9015
252b5132 9016 /* Now we load the register(s). */
ca4e0257 9017 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
9018 {
9019 used_at = 1;
9020 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
9021 }
252b5132
RH
9022 else
9023 {
8fc2e39e 9024 used_at = 1;
67c0d1eb 9025 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 9026 if (treg != RA)
252b5132
RH
9027 {
9028 /* FIXME: How in the world do we deal with the possible
9029 overflow here? */
9030 offset_expr.X_add_number += 4;
67c0d1eb 9031 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 9032 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
9033 }
9034 }
252b5132
RH
9035 break;
9036
9037 case M_LI_DD:
ca4e0257
RS
9038 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
9039 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
9040 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
9041 the value and the low order 32 bits are either zero or in
9042 OFFSET_EXPR. */
252b5132
RH
9043 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
9044 {
8fc2e39e 9045 used_at = 1;
67c0d1eb 9046 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
9047 if (HAVE_64BIT_FPRS)
9048 {
9c2799c2 9049 gas_assert (HAVE_64BIT_GPRS);
67c0d1eb 9050 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 9051 }
252b5132
RH
9052 else
9053 {
67c0d1eb 9054 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 9055 if (offset_expr.X_op == O_absent)
67c0d1eb 9056 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
9057 else
9058 {
9c2799c2 9059 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
9060 load_register (AT, &offset_expr, 0);
9061 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
9062 }
9063 }
9064 break;
9065 }
9066
9c2799c2 9067 gas_assert (offset_expr.X_op == O_symbol
90ecf173 9068 && offset_expr.X_add_number == 0);
252b5132
RH
9069 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
9070 if (strcmp (s, ".lit8") == 0)
9071 {
0aa27725 9072 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
252b5132 9073 {
67c0d1eb 9074 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 9075 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 9076 break;
252b5132 9077 }
c9914766 9078 breg = mips_gp_register;
252b5132
RH
9079 r = BFD_RELOC_MIPS_LITERAL;
9080 goto dob;
9081 }
9082 else
9083 {
9c2799c2 9084 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 9085 used_at = 1;
0a44bf69 9086 if (mips_pic != NO_PIC)
67c0d1eb
RS
9087 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9088 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
9089 else
9090 {
9091 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 9092 macro_build_lui (&offset_expr, AT);
252b5132 9093 }
bdaaa2e1 9094
0aa27725 9095 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
252b5132 9096 {
67c0d1eb
RS
9097 macro_build (&offset_expr, "ldc1", "T,o(b)",
9098 treg, BFD_RELOC_LO16, AT);
252b5132
RH
9099 break;
9100 }
9101 breg = AT;
9102 r = BFD_RELOC_LO16;
9103 goto dob;
9104 }
9105
9106 case M_L_DOB:
252b5132
RH
9107 /* Even on a big endian machine $fn comes before $fn+1. We have
9108 to adjust when loading from memory. */
9109 r = BFD_RELOC_LO16;
9110 dob:
df58fc94 9111 gas_assert (!mips_opts.micromips);
0aa27725 9112 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
67c0d1eb 9113 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 9114 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
9115 /* FIXME: A possible overflow which I don't know how to deal
9116 with. */
9117 offset_expr.X_add_number += 4;
67c0d1eb 9118 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 9119 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
9120 break;
9121
c4a68bea 9122 case M_S_DOB:
df58fc94 9123 gas_assert (!mips_opts.micromips);
0aa27725 9124 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
c4a68bea
MR
9125 /* Even on a big endian machine $fn comes before $fn+1. We have
9126 to adjust when storing to memory. */
9127 macro_build (&offset_expr, "swc1", "T,o(b)",
9128 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
9129 offset_expr.X_add_number += 4;
9130 macro_build (&offset_expr, "swc1", "T,o(b)",
9131 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
9132 break;
9133
252b5132 9134 case M_L_DAB:
df58fc94 9135 gas_assert (!mips_opts.micromips);
252b5132
RH
9136 /*
9137 * The MIPS assembler seems to check for X_add_number not
9138 * being double aligned and generating:
9139 * lui at,%hi(foo+1)
9140 * addu at,at,v1
9141 * addiu at,at,%lo(foo+1)
9142 * lwc1 f2,0(at)
9143 * lwc1 f3,4(at)
9144 * But, the resulting address is the same after relocation so why
9145 * generate the extra instruction?
9146 */
bdaaa2e1 9147 /* Itbl support may require additional care here. */
252b5132 9148 coproc = 1;
df58fc94 9149 fmt = "T,o(b)";
0aa27725 9150 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
9151 {
9152 s = "ldc1";
df58fc94 9153 goto ld_st;
252b5132 9154 }
252b5132 9155 s = "lwc1";
252b5132
RH
9156 goto ldd_std;
9157
9158 case M_S_DAB:
df58fc94
RS
9159 gas_assert (!mips_opts.micromips);
9160 /* Itbl support may require additional care here. */
9161 coproc = 1;
9162 fmt = "T,o(b)";
0aa27725 9163 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
9164 {
9165 s = "sdc1";
df58fc94 9166 goto ld_st;
252b5132 9167 }
252b5132 9168 s = "swc1";
252b5132
RH
9169 goto ldd_std;
9170
e407c74b
NC
9171 case M_LQ_AB:
9172 fmt = "t,o(b)";
9173 s = "lq";
9174 goto ld;
9175
9176 case M_SQ_AB:
9177 fmt = "t,o(b)";
9178 s = "sq";
9179 goto ld_st;
9180
252b5132 9181 case M_LD_AB:
df58fc94 9182 fmt = "t,o(b)";
ca4e0257 9183 if (HAVE_64BIT_GPRS)
252b5132
RH
9184 {
9185 s = "ld";
9186 goto ld;
9187 }
252b5132 9188 s = "lw";
252b5132
RH
9189 goto ldd_std;
9190
9191 case M_SD_AB:
df58fc94 9192 fmt = "t,o(b)";
ca4e0257 9193 if (HAVE_64BIT_GPRS)
252b5132
RH
9194 {
9195 s = "sd";
df58fc94 9196 goto ld_st;
252b5132 9197 }
252b5132 9198 s = "sw";
252b5132
RH
9199
9200 ldd_std:
9201 if (offset_expr.X_op != O_symbol
9202 && offset_expr.X_op != O_constant)
9203 {
f71d0d44 9204 as_bad (_("Expression too complex"));
252b5132
RH
9205 offset_expr.X_op = O_constant;
9206 }
9207
2051e8c4
MR
9208 if (HAVE_32BIT_ADDRESSES
9209 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
9210 {
9211 char value [32];
9212
9213 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 9214 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 9215 }
2051e8c4 9216
252b5132
RH
9217 /* Even on a big endian machine $fn comes before $fn+1. We have
9218 to adjust when loading from memory. We set coproc if we must
9219 load $fn+1 first. */
bdaaa2e1 9220 /* Itbl support may require additional care here. */
90ecf173 9221 if (!target_big_endian)
252b5132
RH
9222 coproc = 0;
9223
90ecf173 9224 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
9225 {
9226 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
9227 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
9228 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
9229 If we have a base register, we use this
9230 addu $at,$breg,$gp
cdf6fd85
TS
9231 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
9232 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
9233 If this is not a GP relative symbol, we want
9234 lui $at,<sym> (BFD_RELOC_HI16_S)
9235 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9236 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9237 If there is a base register, we add it to $at after the
9238 lui instruction. If there is a constant, we always use
9239 the last case. */
39a59cf8
MR
9240 if (offset_expr.X_op == O_symbol
9241 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9242 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 9243 {
4d7206a2 9244 relax_start (offset_expr.X_add_symbol);
252b5132
RH
9245 if (breg == 0)
9246 {
c9914766 9247 tempreg = mips_gp_register;
252b5132
RH
9248 }
9249 else
9250 {
67c0d1eb 9251 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9252 AT, breg, mips_gp_register);
252b5132 9253 tempreg = AT;
252b5132
RH
9254 used_at = 1;
9255 }
9256
beae10d5 9257 /* Itbl support may require additional care here. */
67c0d1eb 9258 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9259 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
9260 offset_expr.X_add_number += 4;
9261
9262 /* Set mips_optimize to 2 to avoid inserting an
9263 undesired nop. */
9264 hold_mips_optimize = mips_optimize;
9265 mips_optimize = 2;
beae10d5 9266 /* Itbl support may require additional care here. */
67c0d1eb 9267 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9268 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
9269 mips_optimize = hold_mips_optimize;
9270
4d7206a2 9271 relax_switch ();
252b5132 9272
0970e49e 9273 offset_expr.X_add_number -= 4;
252b5132 9274 }
8fc2e39e 9275 used_at = 1;
67c0d1eb 9276 macro_build_lui (&offset_expr, AT);
252b5132 9277 if (breg != 0)
67c0d1eb 9278 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9279 /* Itbl support may require additional care here. */
67c0d1eb 9280 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9281 BFD_RELOC_LO16, AT);
252b5132
RH
9282 /* FIXME: How do we handle overflow here? */
9283 offset_expr.X_add_number += 4;
beae10d5 9284 /* Itbl support may require additional care here. */
67c0d1eb 9285 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9286 BFD_RELOC_LO16, AT);
4d7206a2
RS
9287 if (mips_relax.sequence)
9288 relax_end ();
bdaaa2e1 9289 }
0a44bf69 9290 else if (!mips_big_got)
252b5132 9291 {
252b5132
RH
9292 /* If this is a reference to an external symbol, we want
9293 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9294 nop
9295 <op> $treg,0($at)
9296 <op> $treg+1,4($at)
9297 Otherwise we want
9298 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9299 nop
9300 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9301 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9302 If there is a base register we add it to $at before the
9303 lwc1 instructions. If there is a constant we include it
9304 in the lwc1 instructions. */
9305 used_at = 1;
9306 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
9307 if (expr1.X_add_number < -0x8000
9308 || expr1.X_add_number >= 0x8000 - 4)
9309 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9310 load_got_offset (AT, &offset_expr);
269137b2 9311 load_delay_nop ();
252b5132 9312 if (breg != 0)
67c0d1eb 9313 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
9314
9315 /* Set mips_optimize to 2 to avoid inserting an undesired
9316 nop. */
9317 hold_mips_optimize = mips_optimize;
9318 mips_optimize = 2;
4d7206a2 9319
beae10d5 9320 /* Itbl support may require additional care here. */
4d7206a2 9321 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9322 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9323 BFD_RELOC_LO16, AT);
4d7206a2 9324 expr1.X_add_number += 4;
67c0d1eb
RS
9325 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9326 BFD_RELOC_LO16, AT);
4d7206a2 9327 relax_switch ();
67c0d1eb
RS
9328 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9329 BFD_RELOC_LO16, AT);
4d7206a2 9330 offset_expr.X_add_number += 4;
67c0d1eb
RS
9331 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9332 BFD_RELOC_LO16, AT);
4d7206a2 9333 relax_end ();
252b5132 9334
4d7206a2 9335 mips_optimize = hold_mips_optimize;
252b5132 9336 }
0a44bf69 9337 else if (mips_big_got)
252b5132 9338 {
67c0d1eb 9339 int gpdelay;
252b5132
RH
9340
9341 /* If this is a reference to an external symbol, we want
9342 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9343 addu $at,$at,$gp
9344 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
9345 nop
9346 <op> $treg,0($at)
9347 <op> $treg+1,4($at)
9348 Otherwise we want
9349 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9350 nop
9351 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9352 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9353 If there is a base register we add it to $at before the
9354 lwc1 instructions. If there is a constant we include it
9355 in the lwc1 instructions. */
9356 used_at = 1;
9357 expr1.X_add_number = offset_expr.X_add_number;
9358 offset_expr.X_add_number = 0;
9359 if (expr1.X_add_number < -0x8000
9360 || expr1.X_add_number >= 0x8000 - 4)
9361 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9362 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 9363 relax_start (offset_expr.X_add_symbol);
df58fc94 9364 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
9365 AT, BFD_RELOC_MIPS_GOT_HI16);
9366 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9367 AT, AT, mips_gp_register);
67c0d1eb 9368 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 9369 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 9370 load_delay_nop ();
252b5132 9371 if (breg != 0)
67c0d1eb 9372 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9373 /* Itbl support may require additional care here. */
67c0d1eb 9374 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9375 BFD_RELOC_LO16, AT);
252b5132
RH
9376 expr1.X_add_number += 4;
9377
9378 /* Set mips_optimize to 2 to avoid inserting an undesired
9379 nop. */
9380 hold_mips_optimize = mips_optimize;
9381 mips_optimize = 2;
beae10d5 9382 /* Itbl support may require additional care here. */
67c0d1eb 9383 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 9384 BFD_RELOC_LO16, AT);
252b5132
RH
9385 mips_optimize = hold_mips_optimize;
9386 expr1.X_add_number -= 4;
9387
4d7206a2
RS
9388 relax_switch ();
9389 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
9390 if (gpdelay)
9391 macro_build (NULL, "nop", "");
9392 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9393 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9394 load_delay_nop ();
252b5132 9395 if (breg != 0)
67c0d1eb 9396 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9397 /* Itbl support may require additional care here. */
67c0d1eb
RS
9398 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9399 BFD_RELOC_LO16, AT);
4d7206a2 9400 offset_expr.X_add_number += 4;
252b5132
RH
9401
9402 /* Set mips_optimize to 2 to avoid inserting an undesired
9403 nop. */
9404 hold_mips_optimize = mips_optimize;
9405 mips_optimize = 2;
beae10d5 9406 /* Itbl support may require additional care here. */
67c0d1eb
RS
9407 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9408 BFD_RELOC_LO16, AT);
252b5132 9409 mips_optimize = hold_mips_optimize;
4d7206a2 9410 relax_end ();
252b5132 9411 }
252b5132
RH
9412 else
9413 abort ();
9414
252b5132
RH
9415 break;
9416
9417 case M_LD_OB:
704897fb 9418 s = HAVE_64BIT_GPRS ? "ld" : "lw";
252b5132
RH
9419 goto sd_ob;
9420 case M_SD_OB:
704897fb 9421 s = HAVE_64BIT_GPRS ? "sd" : "sw";
252b5132 9422 sd_ob:
4614d845
MR
9423 macro_build (&offset_expr, s, "t,o(b)", treg,
9424 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9425 breg);
704897fb
MR
9426 if (!HAVE_64BIT_GPRS)
9427 {
9428 offset_expr.X_add_number += 4;
9429 macro_build (&offset_expr, s, "t,o(b)", treg + 1,
4614d845
MR
9430 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9431 breg);
704897fb 9432 }
8fc2e39e 9433 break;
252b5132 9434
dd6a37e7
AP
9435
9436 case M_SAA_AB:
9437 ab = 1;
9438 case M_SAA_OB:
9439 s = "saa";
7f3c4072 9440 offbits = 0;
dd6a37e7
AP
9441 fmt = "t,(b)";
9442 goto ld_st;
9443 case M_SAAD_AB:
9444 ab = 1;
9445 case M_SAAD_OB:
9446 s = "saad";
7f3c4072 9447 offbits = 0;
dd6a37e7
AP
9448 fmt = "t,(b)";
9449 goto ld_st;
9450
252b5132
RH
9451 /* New code added to support COPZ instructions.
9452 This code builds table entries out of the macros in mip_opcodes.
9453 R4000 uses interlocks to handle coproc delays.
9454 Other chips (like the R3000) require nops to be inserted for delays.
9455
f72c8c98 9456 FIXME: Currently, we require that the user handle delays.
252b5132
RH
9457 In order to fill delay slots for non-interlocked chips,
9458 we must have a way to specify delays based on the coprocessor.
9459 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9460 What are the side-effects of the cop instruction?
9461 What cache support might we have and what are its effects?
9462 Both coprocessor & memory require delays. how long???
bdaaa2e1 9463 What registers are read/set/modified?
252b5132
RH
9464
9465 If an itbl is provided to interpret cop instructions,
bdaaa2e1 9466 this knowledge can be encoded in the itbl spec. */
252b5132
RH
9467
9468 case M_COP0:
9469 s = "c0";
9470 goto copz;
9471 case M_COP1:
9472 s = "c1";
9473 goto copz;
9474 case M_COP2:
9475 s = "c2";
9476 goto copz;
9477 case M_COP3:
9478 s = "c3";
9479 copz:
df58fc94 9480 gas_assert (!mips_opts.micromips);
252b5132
RH
9481 /* For now we just do C (same as Cz). The parameter will be
9482 stored in insn_opcode by mips_ip. */
67c0d1eb 9483 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 9484 break;
252b5132 9485
ea1fb5dc 9486 case M_MOVE:
67c0d1eb 9487 move_register (dreg, sreg);
8fc2e39e 9488 break;
ea1fb5dc 9489
252b5132
RH
9490 case M_DMUL:
9491 dbl = 1;
9492 case M_MUL:
e407c74b
NC
9493 if (mips_opts.arch == CPU_R5900)
9494 {
9495 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", dreg, sreg, treg);
9496 }
9497 else
9498 {
67c0d1eb 9499 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
df58fc94 9500 macro_build (NULL, "mflo", MFHL_FMT, dreg);
e407c74b 9501 }
8fc2e39e 9502 break;
252b5132
RH
9503
9504 case M_DMUL_I:
9505 dbl = 1;
9506 case M_MUL_I:
9507 /* The MIPS assembler some times generates shifts and adds. I'm
9508 not trying to be that fancy. GCC should do this for us
9509 anyway. */
8fc2e39e 9510 used_at = 1;
67c0d1eb
RS
9511 load_register (AT, &imm_expr, dbl);
9512 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
df58fc94 9513 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9514 break;
9515
9516 case M_DMULO_I:
9517 dbl = 1;
9518 case M_MULO_I:
9519 imm = 1;
9520 goto do_mulo;
9521
9522 case M_DMULO:
9523 dbl = 1;
9524 case M_MULO:
9525 do_mulo:
7d10b47d 9526 start_noreorder ();
8fc2e39e 9527 used_at = 1;
252b5132 9528 if (imm)
67c0d1eb
RS
9529 load_register (AT, &imm_expr, dbl);
9530 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
df58fc94
RS
9531 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9532 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9533 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 9534 if (mips_trap)
df58fc94 9535 macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
252b5132
RH
9536 else
9537 {
df58fc94
RS
9538 if (mips_opts.micromips)
9539 micromips_label_expr (&label_expr);
9540 else
9541 label_expr.X_add_number = 8;
9542 macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
a605d2b3 9543 macro_build (NULL, "nop", "");
df58fc94
RS
9544 macro_build (NULL, "break", BRK_FMT, 6);
9545 if (mips_opts.micromips)
9546 micromips_add_label ();
252b5132 9547 }
7d10b47d 9548 end_noreorder ();
df58fc94 9549 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9550 break;
9551
9552 case M_DMULOU_I:
9553 dbl = 1;
9554 case M_MULOU_I:
9555 imm = 1;
9556 goto do_mulou;
9557
9558 case M_DMULOU:
9559 dbl = 1;
9560 case M_MULOU:
9561 do_mulou:
7d10b47d 9562 start_noreorder ();
8fc2e39e 9563 used_at = 1;
252b5132 9564 if (imm)
67c0d1eb
RS
9565 load_register (AT, &imm_expr, dbl);
9566 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 9567 sreg, imm ? AT : treg);
df58fc94
RS
9568 macro_build (NULL, "mfhi", MFHL_FMT, AT);
9569 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132 9570 if (mips_trap)
df58fc94 9571 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
9572 else
9573 {
df58fc94
RS
9574 if (mips_opts.micromips)
9575 micromips_label_expr (&label_expr);
9576 else
9577 label_expr.X_add_number = 8;
9578 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 9579 macro_build (NULL, "nop", "");
df58fc94
RS
9580 macro_build (NULL, "break", BRK_FMT, 6);
9581 if (mips_opts.micromips)
9582 micromips_add_label ();
252b5132 9583 }
7d10b47d 9584 end_noreorder ();
252b5132
RH
9585 break;
9586
771c7ce4 9587 case M_DROL:
fef14a42 9588 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
9589 {
9590 if (dreg == sreg)
9591 {
9592 tempreg = AT;
9593 used_at = 1;
9594 }
9595 else
9596 {
9597 tempreg = dreg;
82dd0097 9598 }
67c0d1eb
RS
9599 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9600 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9601 break;
82dd0097 9602 }
8fc2e39e 9603 used_at = 1;
c80c840e 9604 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9605 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9606 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9607 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9608 break;
9609
252b5132 9610 case M_ROL:
fef14a42 9611 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
9612 {
9613 if (dreg == sreg)
9614 {
9615 tempreg = AT;
9616 used_at = 1;
9617 }
9618 else
9619 {
9620 tempreg = dreg;
82dd0097 9621 }
67c0d1eb
RS
9622 macro_build (NULL, "negu", "d,w", tempreg, treg);
9623 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9624 break;
82dd0097 9625 }
8fc2e39e 9626 used_at = 1;
c80c840e 9627 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9628 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9629 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9630 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9631 break;
9632
771c7ce4
TS
9633 case M_DROL_I:
9634 {
9635 unsigned int rot;
91d6fa6a
NC
9636 char *l;
9637 char *rr;
771c7ce4
TS
9638
9639 if (imm_expr.X_op != O_constant)
82dd0097 9640 as_bad (_("Improper rotate count"));
771c7ce4 9641 rot = imm_expr.X_add_number & 0x3f;
fef14a42 9642 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
9643 {
9644 rot = (64 - rot) & 0x3f;
9645 if (rot >= 32)
df58fc94 9646 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
60b63b72 9647 else
df58fc94 9648 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9649 break;
60b63b72 9650 }
483fc7cd 9651 if (rot == 0)
483fc7cd 9652 {
df58fc94 9653 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9654 break;
483fc7cd 9655 }
82dd0097 9656 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 9657 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 9658 rot &= 0x1f;
8fc2e39e 9659 used_at = 1;
df58fc94
RS
9660 macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
9661 macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9662 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9663 }
9664 break;
9665
252b5132 9666 case M_ROL_I:
771c7ce4
TS
9667 {
9668 unsigned int rot;
9669
9670 if (imm_expr.X_op != O_constant)
82dd0097 9671 as_bad (_("Improper rotate count"));
771c7ce4 9672 rot = imm_expr.X_add_number & 0x1f;
fef14a42 9673 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 9674 {
df58fc94 9675 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 9676 break;
60b63b72 9677 }
483fc7cd 9678 if (rot == 0)
483fc7cd 9679 {
df58fc94 9680 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9681 break;
483fc7cd 9682 }
8fc2e39e 9683 used_at = 1;
df58fc94
RS
9684 macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
9685 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9686 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9687 }
9688 break;
9689
9690 case M_DROR:
fef14a42 9691 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 9692 {
67c0d1eb 9693 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 9694 break;
82dd0097 9695 }
8fc2e39e 9696 used_at = 1;
c80c840e 9697 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9698 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
9699 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
9700 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9701 break;
9702
9703 case M_ROR:
fef14a42 9704 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 9705 {
67c0d1eb 9706 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 9707 break;
82dd0097 9708 }
8fc2e39e 9709 used_at = 1;
c80c840e 9710 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9711 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
9712 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
9713 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9714 break;
9715
771c7ce4
TS
9716 case M_DROR_I:
9717 {
9718 unsigned int rot;
91d6fa6a
NC
9719 char *l;
9720 char *rr;
771c7ce4
TS
9721
9722 if (imm_expr.X_op != O_constant)
82dd0097 9723 as_bad (_("Improper rotate count"));
771c7ce4 9724 rot = imm_expr.X_add_number & 0x3f;
fef14a42 9725 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
9726 {
9727 if (rot >= 32)
df58fc94 9728 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
82dd0097 9729 else
df58fc94 9730 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9731 break;
82dd0097 9732 }
483fc7cd 9733 if (rot == 0)
483fc7cd 9734 {
df58fc94 9735 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9736 break;
483fc7cd 9737 }
91d6fa6a 9738 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
9739 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
9740 rot &= 0x1f;
8fc2e39e 9741 used_at = 1;
df58fc94
RS
9742 macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
9743 macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9744 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9745 }
9746 break;
9747
252b5132 9748 case M_ROR_I:
771c7ce4
TS
9749 {
9750 unsigned int rot;
9751
9752 if (imm_expr.X_op != O_constant)
82dd0097 9753 as_bad (_("Improper rotate count"));
771c7ce4 9754 rot = imm_expr.X_add_number & 0x1f;
fef14a42 9755 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 9756 {
df58fc94 9757 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 9758 break;
82dd0097 9759 }
483fc7cd 9760 if (rot == 0)
483fc7cd 9761 {
df58fc94 9762 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 9763 break;
483fc7cd 9764 }
8fc2e39e 9765 used_at = 1;
df58fc94
RS
9766 macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
9767 macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 9768 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 9769 }
252b5132
RH
9770 break;
9771
252b5132
RH
9772 case M_SEQ:
9773 if (sreg == 0)
67c0d1eb 9774 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 9775 else if (treg == 0)
67c0d1eb 9776 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9777 else
9778 {
67c0d1eb
RS
9779 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9780 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 9781 }
8fc2e39e 9782 break;
252b5132
RH
9783
9784 case M_SEQ_I:
9785 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9786 {
67c0d1eb 9787 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9788 break;
252b5132
RH
9789 }
9790 if (sreg == 0)
9791 {
9792 as_warn (_("Instruction %s: result is always false"),
9793 ip->insn_mo->name);
67c0d1eb 9794 move_register (dreg, 0);
8fc2e39e 9795 break;
252b5132 9796 }
dd3cbb7e
NC
9797 if (CPU_HAS_SEQ (mips_opts.arch)
9798 && -512 <= imm_expr.X_add_number
9799 && imm_expr.X_add_number < 512)
9800 {
9801 macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
750bdd57 9802 (int) imm_expr.X_add_number);
dd3cbb7e
NC
9803 break;
9804 }
252b5132
RH
9805 if (imm_expr.X_op == O_constant
9806 && imm_expr.X_add_number >= 0
9807 && imm_expr.X_add_number < 0x10000)
9808 {
67c0d1eb 9809 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9810 }
9811 else if (imm_expr.X_op == O_constant
9812 && imm_expr.X_add_number > -0x8000
9813 && imm_expr.X_add_number < 0)
9814 {
9815 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 9816 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 9817 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 9818 }
dd3cbb7e
NC
9819 else if (CPU_HAS_SEQ (mips_opts.arch))
9820 {
9821 used_at = 1;
9822 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9823 macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
9824 break;
9825 }
252b5132
RH
9826 else
9827 {
67c0d1eb
RS
9828 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9829 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
9830 used_at = 1;
9831 }
67c0d1eb 9832 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9833 break;
252b5132
RH
9834
9835 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
9836 s = "slt";
9837 goto sge;
9838 case M_SGEU:
9839 s = "sltu";
9840 sge:
67c0d1eb
RS
9841 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
9842 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9843 break;
252b5132
RH
9844
9845 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
9846 case M_SGEU_I:
9847 if (imm_expr.X_op == O_constant
9848 && imm_expr.X_add_number >= -0x8000
9849 && imm_expr.X_add_number < 0x8000)
9850 {
67c0d1eb
RS
9851 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
9852 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9853 }
9854 else
9855 {
67c0d1eb
RS
9856 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9857 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
9858 dreg, sreg, AT);
252b5132
RH
9859 used_at = 1;
9860 }
67c0d1eb 9861 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9862 break;
252b5132
RH
9863
9864 case M_SGT: /* sreg > treg <==> treg < sreg */
9865 s = "slt";
9866 goto sgt;
9867 case M_SGTU:
9868 s = "sltu";
9869 sgt:
67c0d1eb 9870 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 9871 break;
252b5132
RH
9872
9873 case M_SGT_I: /* sreg > I <==> I < sreg */
9874 s = "slt";
9875 goto sgti;
9876 case M_SGTU_I:
9877 s = "sltu";
9878 sgti:
8fc2e39e 9879 used_at = 1;
67c0d1eb
RS
9880 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9881 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
9882 break;
9883
2396cfb9 9884 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
9885 s = "slt";
9886 goto sle;
9887 case M_SLEU:
9888 s = "sltu";
9889 sle:
67c0d1eb
RS
9890 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9891 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 9892 break;
252b5132 9893
2396cfb9 9894 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
9895 s = "slt";
9896 goto slei;
9897 case M_SLEU_I:
9898 s = "sltu";
9899 slei:
8fc2e39e 9900 used_at = 1;
67c0d1eb
RS
9901 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9902 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9903 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
9904 break;
9905
9906 case M_SLT_I:
9907 if (imm_expr.X_op == O_constant
9908 && imm_expr.X_add_number >= -0x8000
9909 && imm_expr.X_add_number < 0x8000)
9910 {
67c0d1eb 9911 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 9912 break;
252b5132 9913 }
8fc2e39e 9914 used_at = 1;
67c0d1eb
RS
9915 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9916 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
9917 break;
9918
9919 case M_SLTU_I:
9920 if (imm_expr.X_op == O_constant
9921 && imm_expr.X_add_number >= -0x8000
9922 && imm_expr.X_add_number < 0x8000)
9923 {
67c0d1eb 9924 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 9925 BFD_RELOC_LO16);
8fc2e39e 9926 break;
252b5132 9927 }
8fc2e39e 9928 used_at = 1;
67c0d1eb
RS
9929 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9930 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
9931 break;
9932
9933 case M_SNE:
9934 if (sreg == 0)
67c0d1eb 9935 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 9936 else if (treg == 0)
67c0d1eb 9937 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
9938 else
9939 {
67c0d1eb
RS
9940 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9941 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 9942 }
8fc2e39e 9943 break;
252b5132
RH
9944
9945 case M_SNE_I:
9946 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9947 {
67c0d1eb 9948 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 9949 break;
252b5132
RH
9950 }
9951 if (sreg == 0)
9952 {
9953 as_warn (_("Instruction %s: result is always true"),
9954 ip->insn_mo->name);
67c0d1eb
RS
9955 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
9956 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 9957 break;
252b5132 9958 }
dd3cbb7e
NC
9959 if (CPU_HAS_SEQ (mips_opts.arch)
9960 && -512 <= imm_expr.X_add_number
9961 && imm_expr.X_add_number < 512)
9962 {
9963 macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
750bdd57 9964 (int) imm_expr.X_add_number);
dd3cbb7e
NC
9965 break;
9966 }
252b5132
RH
9967 if (imm_expr.X_op == O_constant
9968 && imm_expr.X_add_number >= 0
9969 && imm_expr.X_add_number < 0x10000)
9970 {
67c0d1eb 9971 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
9972 }
9973 else if (imm_expr.X_op == O_constant
9974 && imm_expr.X_add_number > -0x8000
9975 && imm_expr.X_add_number < 0)
9976 {
9977 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 9978 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 9979 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 9980 }
dd3cbb7e
NC
9981 else if (CPU_HAS_SEQ (mips_opts.arch))
9982 {
9983 used_at = 1;
9984 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9985 macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
9986 break;
9987 }
252b5132
RH
9988 else
9989 {
67c0d1eb
RS
9990 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9991 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
9992 used_at = 1;
9993 }
67c0d1eb 9994 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 9995 break;
252b5132 9996
df58fc94
RS
9997 case M_SUB_I:
9998 s = "addi";
9999 s2 = "sub";
10000 goto do_subi;
10001 case M_SUBU_I:
10002 s = "addiu";
10003 s2 = "subu";
10004 goto do_subi;
252b5132
RH
10005 case M_DSUB_I:
10006 dbl = 1;
df58fc94
RS
10007 s = "daddi";
10008 s2 = "dsub";
10009 if (!mips_opts.micromips)
10010 goto do_subi;
252b5132 10011 if (imm_expr.X_op == O_constant
df58fc94
RS
10012 && imm_expr.X_add_number > -0x200
10013 && imm_expr.X_add_number <= 0x200)
252b5132 10014 {
df58fc94 10015 macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
8fc2e39e 10016 break;
252b5132 10017 }
df58fc94 10018 goto do_subi_i;
252b5132
RH
10019 case M_DSUBU_I:
10020 dbl = 1;
df58fc94
RS
10021 s = "daddiu";
10022 s2 = "dsubu";
10023 do_subi:
252b5132
RH
10024 if (imm_expr.X_op == O_constant
10025 && imm_expr.X_add_number > -0x8000
10026 && imm_expr.X_add_number <= 0x8000)
10027 {
10028 imm_expr.X_add_number = -imm_expr.X_add_number;
df58fc94 10029 macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 10030 break;
252b5132 10031 }
df58fc94 10032 do_subi_i:
8fc2e39e 10033 used_at = 1;
67c0d1eb 10034 load_register (AT, &imm_expr, dbl);
df58fc94 10035 macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
252b5132
RH
10036 break;
10037
10038 case M_TEQ_I:
10039 s = "teq";
10040 goto trap;
10041 case M_TGE_I:
10042 s = "tge";
10043 goto trap;
10044 case M_TGEU_I:
10045 s = "tgeu";
10046 goto trap;
10047 case M_TLT_I:
10048 s = "tlt";
10049 goto trap;
10050 case M_TLTU_I:
10051 s = "tltu";
10052 goto trap;
10053 case M_TNE_I:
10054 s = "tne";
10055 trap:
8fc2e39e 10056 used_at = 1;
67c0d1eb
RS
10057 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10058 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
10059 break;
10060
252b5132 10061 case M_TRUNCWS:
43841e91 10062 case M_TRUNCWD:
df58fc94 10063 gas_assert (!mips_opts.micromips);
0aa27725 10064 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 10065 used_at = 1;
252b5132
RH
10066 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
10067 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
10068
10069 /*
10070 * Is the double cfc1 instruction a bug in the mips assembler;
10071 * or is there a reason for it?
10072 */
7d10b47d 10073 start_noreorder ();
67c0d1eb
RS
10074 macro_build (NULL, "cfc1", "t,G", treg, RA);
10075 macro_build (NULL, "cfc1", "t,G", treg, RA);
10076 macro_build (NULL, "nop", "");
252b5132 10077 expr1.X_add_number = 3;
67c0d1eb 10078 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 10079 expr1.X_add_number = 2;
67c0d1eb
RS
10080 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
10081 macro_build (NULL, "ctc1", "t,G", AT, RA);
10082 macro_build (NULL, "nop", "");
10083 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
10084 dreg, sreg);
10085 macro_build (NULL, "ctc1", "t,G", treg, RA);
10086 macro_build (NULL, "nop", "");
7d10b47d 10087 end_noreorder ();
252b5132
RH
10088 break;
10089
df58fc94
RS
10090 case M_ULH_A:
10091 ab = 1;
252b5132
RH
10092 case M_ULH:
10093 s = "lb";
df58fc94
RS
10094 s2 = "lbu";
10095 off = 1;
10096 goto uld_st;
10097 case M_ULHU_A:
10098 ab = 1;
252b5132
RH
10099 case M_ULHU:
10100 s = "lbu";
df58fc94
RS
10101 s2 = "lbu";
10102 off = 1;
10103 goto uld_st;
10104 case M_ULW_A:
10105 ab = 1;
10106 case M_ULW:
10107 s = "lwl";
10108 s2 = "lwr";
7f3c4072 10109 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10110 off = 3;
10111 goto uld_st;
10112 case M_ULD_A:
10113 ab = 1;
252b5132
RH
10114 case M_ULD:
10115 s = "ldl";
10116 s2 = "ldr";
7f3c4072 10117 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 10118 off = 7;
df58fc94
RS
10119 goto uld_st;
10120 case M_USH_A:
10121 ab = 1;
10122 case M_USH:
10123 s = "sb";
10124 s2 = "sb";
10125 off = 1;
10126 ust = 1;
10127 goto uld_st;
10128 case M_USW_A:
10129 ab = 1;
10130 case M_USW:
10131 s = "swl";
10132 s2 = "swr";
7f3c4072 10133 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 10134 off = 3;
df58fc94
RS
10135 ust = 1;
10136 goto uld_st;
10137 case M_USD_A:
10138 ab = 1;
10139 case M_USD:
10140 s = "sdl";
10141 s2 = "sdr";
7f3c4072 10142 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10143 off = 7;
10144 ust = 1;
10145
10146 uld_st:
10147 if (!ab && offset_expr.X_add_number >= 0x8000 - off)
f71d0d44 10148 as_bad (_("Operand overflow"));
df58fc94
RS
10149
10150 ep = &offset_expr;
10151 expr1.X_add_number = 0;
10152 if (ab)
10153 {
10154 used_at = 1;
10155 tempreg = AT;
10156 load_address (tempreg, ep, &used_at);
10157 if (breg != 0)
10158 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10159 tempreg, tempreg, breg);
10160 breg = tempreg;
10161 tempreg = treg;
10162 ep = &expr1;
10163 }
7f3c4072 10164 else if (offbits == 12
df58fc94
RS
10165 && (offset_expr.X_op != O_constant
10166 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
10167 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
10168 {
10169 used_at = 1;
10170 tempreg = AT;
10171 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
10172 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10173 breg = tempreg;
10174 tempreg = treg;
10175 ep = &expr1;
10176 }
10177 else if (!ust && treg == breg)
8fc2e39e
TS
10178 {
10179 used_at = 1;
10180 tempreg = AT;
10181 }
252b5132 10182 else
df58fc94 10183 tempreg = treg;
af22f5b2 10184
df58fc94
RS
10185 if (off == 1)
10186 goto ulh_sh;
252b5132 10187
90ecf173 10188 if (!target_big_endian)
df58fc94 10189 ep->X_add_number += off;
7f3c4072 10190 if (offbits != 12)
df58fc94 10191 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 10192 else
df58fc94
RS
10193 macro_build (NULL, s, "t,~(b)",
10194 tempreg, (unsigned long) ep->X_add_number, breg);
10195
90ecf173 10196 if (!target_big_endian)
df58fc94 10197 ep->X_add_number -= off;
252b5132 10198 else
df58fc94 10199 ep->X_add_number += off;
7f3c4072 10200 if (offbits != 12)
df58fc94
RS
10201 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10202 else
10203 macro_build (NULL, s2, "t,~(b)",
10204 tempreg, (unsigned long) ep->X_add_number, breg);
252b5132 10205
df58fc94
RS
10206 /* If necessary, move the result in tempreg to the final destination. */
10207 if (!ust && treg != tempreg)
10208 {
10209 /* Protect second load's delay slot. */
10210 load_delay_nop ();
10211 move_register (treg, tempreg);
10212 }
8fc2e39e 10213 break;
252b5132 10214
df58fc94 10215 ulh_sh:
d6bc6245 10216 used_at = 1;
df58fc94
RS
10217 if (target_big_endian == ust)
10218 ep->X_add_number += off;
10219 tempreg = ust || ab ? treg : AT;
10220 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10221
10222 /* For halfword transfers we need a temporary register to shuffle
10223 bytes. Unfortunately for M_USH_A we have none available before
10224 the next store as AT holds the base address. We deal with this
10225 case by clobbering TREG and then restoring it as with ULH. */
10226 tempreg = ust == ab ? treg : AT;
10227 if (ust)
10228 macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10229
10230 if (target_big_endian == ust)
10231 ep->X_add_number -= off;
252b5132 10232 else
df58fc94
RS
10233 ep->X_add_number += off;
10234 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 10235
df58fc94
RS
10236 /* For M_USH_A re-retrieve the LSB. */
10237 if (ust && ab)
10238 {
10239 if (target_big_endian)
10240 ep->X_add_number += off;
10241 else
10242 ep->X_add_number -= off;
10243 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
10244 }
10245 /* For ULH and M_USH_A OR the LSB in. */
10246 if (!ust || ab)
10247 {
10248 tempreg = !ab ? AT : treg;
10249 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10250 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10251 }
252b5132
RH
10252 break;
10253
10254 default:
10255 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 10256 are added dynamically. */
252b5132
RH
10257 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10258 break;
10259 }
741fe287 10260 if (!mips_opts.at && used_at)
8fc2e39e 10261 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
10262}
10263
10264/* Implement macros in mips16 mode. */
10265
10266static void
17a2f251 10267mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
10268{
10269 int mask;
10270 int xreg, yreg, zreg, tmp;
252b5132
RH
10271 expressionS expr1;
10272 int dbl;
10273 const char *s, *s2, *s3;
10274
10275 mask = ip->insn_mo->mask;
10276
bf12938e
RS
10277 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10278 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10279 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 10280
252b5132
RH
10281 expr1.X_op = O_constant;
10282 expr1.X_op_symbol = NULL;
10283 expr1.X_add_symbol = NULL;
10284 expr1.X_add_number = 1;
10285
10286 dbl = 0;
10287
10288 switch (mask)
10289 {
10290 default:
b37df7c4 10291 abort ();
252b5132
RH
10292
10293 case M_DDIV_3:
10294 dbl = 1;
10295 case M_DIV_3:
10296 s = "mflo";
10297 goto do_div3;
10298 case M_DREM_3:
10299 dbl = 1;
10300 case M_REM_3:
10301 s = "mfhi";
10302 do_div3:
7d10b47d 10303 start_noreorder ();
67c0d1eb 10304 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 10305 expr1.X_add_number = 2;
67c0d1eb
RS
10306 macro_build (&expr1, "bnez", "x,p", yreg);
10307 macro_build (NULL, "break", "6", 7);
bdaaa2e1 10308
252b5132
RH
10309 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10310 since that causes an overflow. We should do that as well,
10311 but I don't see how to do the comparisons without a temporary
10312 register. */
7d10b47d 10313 end_noreorder ();
67c0d1eb 10314 macro_build (NULL, s, "x", zreg);
252b5132
RH
10315 break;
10316
10317 case M_DIVU_3:
10318 s = "divu";
10319 s2 = "mflo";
10320 goto do_divu3;
10321 case M_REMU_3:
10322 s = "divu";
10323 s2 = "mfhi";
10324 goto do_divu3;
10325 case M_DDIVU_3:
10326 s = "ddivu";
10327 s2 = "mflo";
10328 goto do_divu3;
10329 case M_DREMU_3:
10330 s = "ddivu";
10331 s2 = "mfhi";
10332 do_divu3:
7d10b47d 10333 start_noreorder ();
67c0d1eb 10334 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 10335 expr1.X_add_number = 2;
67c0d1eb
RS
10336 macro_build (&expr1, "bnez", "x,p", yreg);
10337 macro_build (NULL, "break", "6", 7);
7d10b47d 10338 end_noreorder ();
67c0d1eb 10339 macro_build (NULL, s2, "x", zreg);
252b5132
RH
10340 break;
10341
10342 case M_DMUL:
10343 dbl = 1;
10344 case M_MUL:
67c0d1eb
RS
10345 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10346 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 10347 break;
252b5132
RH
10348
10349 case M_DSUBU_I:
10350 dbl = 1;
10351 goto do_subu;
10352 case M_SUBU_I:
10353 do_subu:
10354 if (imm_expr.X_op != O_constant)
10355 as_bad (_("Unsupported large constant"));
10356 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10357 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
10358 break;
10359
10360 case M_SUBU_I_2:
10361 if (imm_expr.X_op != O_constant)
10362 as_bad (_("Unsupported large constant"));
10363 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10364 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
10365 break;
10366
10367 case M_DSUBU_I_2:
10368 if (imm_expr.X_op != O_constant)
10369 as_bad (_("Unsupported large constant"));
10370 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10371 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
10372 break;
10373
10374 case M_BEQ:
10375 s = "cmp";
10376 s2 = "bteqz";
10377 goto do_branch;
10378 case M_BNE:
10379 s = "cmp";
10380 s2 = "btnez";
10381 goto do_branch;
10382 case M_BLT:
10383 s = "slt";
10384 s2 = "btnez";
10385 goto do_branch;
10386 case M_BLTU:
10387 s = "sltu";
10388 s2 = "btnez";
10389 goto do_branch;
10390 case M_BLE:
10391 s = "slt";
10392 s2 = "bteqz";
10393 goto do_reverse_branch;
10394 case M_BLEU:
10395 s = "sltu";
10396 s2 = "bteqz";
10397 goto do_reverse_branch;
10398 case M_BGE:
10399 s = "slt";
10400 s2 = "bteqz";
10401 goto do_branch;
10402 case M_BGEU:
10403 s = "sltu";
10404 s2 = "bteqz";
10405 goto do_branch;
10406 case M_BGT:
10407 s = "slt";
10408 s2 = "btnez";
10409 goto do_reverse_branch;
10410 case M_BGTU:
10411 s = "sltu";
10412 s2 = "btnez";
10413
10414 do_reverse_branch:
10415 tmp = xreg;
10416 xreg = yreg;
10417 yreg = tmp;
10418
10419 do_branch:
67c0d1eb
RS
10420 macro_build (NULL, s, "x,y", xreg, yreg);
10421 macro_build (&offset_expr, s2, "p");
252b5132
RH
10422 break;
10423
10424 case M_BEQ_I:
10425 s = "cmpi";
10426 s2 = "bteqz";
10427 s3 = "x,U";
10428 goto do_branch_i;
10429 case M_BNE_I:
10430 s = "cmpi";
10431 s2 = "btnez";
10432 s3 = "x,U";
10433 goto do_branch_i;
10434 case M_BLT_I:
10435 s = "slti";
10436 s2 = "btnez";
10437 s3 = "x,8";
10438 goto do_branch_i;
10439 case M_BLTU_I:
10440 s = "sltiu";
10441 s2 = "btnez";
10442 s3 = "x,8";
10443 goto do_branch_i;
10444 case M_BLE_I:
10445 s = "slti";
10446 s2 = "btnez";
10447 s3 = "x,8";
10448 goto do_addone_branch_i;
10449 case M_BLEU_I:
10450 s = "sltiu";
10451 s2 = "btnez";
10452 s3 = "x,8";
10453 goto do_addone_branch_i;
10454 case M_BGE_I:
10455 s = "slti";
10456 s2 = "bteqz";
10457 s3 = "x,8";
10458 goto do_branch_i;
10459 case M_BGEU_I:
10460 s = "sltiu";
10461 s2 = "bteqz";
10462 s3 = "x,8";
10463 goto do_branch_i;
10464 case M_BGT_I:
10465 s = "slti";
10466 s2 = "bteqz";
10467 s3 = "x,8";
10468 goto do_addone_branch_i;
10469 case M_BGTU_I:
10470 s = "sltiu";
10471 s2 = "bteqz";
10472 s3 = "x,8";
10473
10474 do_addone_branch_i:
10475 if (imm_expr.X_op != O_constant)
10476 as_bad (_("Unsupported large constant"));
10477 ++imm_expr.X_add_number;
10478
10479 do_branch_i:
67c0d1eb
RS
10480 macro_build (&imm_expr, s, s3, xreg);
10481 macro_build (&offset_expr, s2, "p");
252b5132
RH
10482 break;
10483
10484 case M_ABS:
10485 expr1.X_add_number = 0;
67c0d1eb 10486 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 10487 if (xreg != yreg)
67c0d1eb 10488 move_register (xreg, yreg);
252b5132 10489 expr1.X_add_number = 2;
67c0d1eb
RS
10490 macro_build (&expr1, "bteqz", "p");
10491 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
10492 }
10493}
10494
10495/* For consistency checking, verify that all bits are specified either
10496 by the match/mask part of the instruction definition, or by the
10497 operand list. */
10498static int
17a2f251 10499validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
10500{
10501 const char *p = opc->args;
10502 char c;
10503 unsigned long used_bits = opc->mask;
10504
10505 if ((used_bits & opc->match) != opc->match)
10506 {
10507 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10508 opc->name, opc->args);
10509 return 0;
10510 }
10511#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
10512 while (*p)
10513 switch (c = *p++)
10514 {
10515 case ',': break;
10516 case '(': break;
10517 case ')': break;
af7ee8bf
CD
10518 case '+':
10519 switch (c = *p++)
10520 {
9bcd4f99
TS
10521 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
10522 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
10523 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
10524 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
af7ee8bf
CD
10525 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10526 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10527 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
10528 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
10529 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
10530 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10531 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10532 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10533 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10534 case 'I': break;
b015e599 10535 case 'J': USE_BITS (OP_MASK_CODE10, OP_SH_CODE10); break;
ef2e4d86
CF
10536 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10537 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
10538 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
bb35fb24
NC
10539 case 'x': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10540 case 'X': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10541 case 'p': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
10542 case 'P': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
dd3cbb7e 10543 case 'Q': USE_BITS (OP_MASK_SEQI, OP_SH_SEQI); break;
bb35fb24
NC
10544 case 's': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
10545 case 'S': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
98675402
RS
10546 case 'z': USE_BITS (OP_MASK_RZ, OP_SH_RZ); break;
10547 case 'Z': USE_BITS (OP_MASK_FZ, OP_SH_FZ); break;
10548 case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10549 case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10550 case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
7f3c4072 10551 case 'j': USE_BITS (OP_MASK_EVAOFFSET, OP_SH_EVAOFFSET); break;
bb35fb24 10552
af7ee8bf
CD
10553 default:
10554 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10555 c, opc->name, opc->args);
10556 return 0;
10557 }
10558 break;
252b5132
RH
10559 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10560 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10561 case 'A': break;
4372b673 10562 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
10563 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
10564 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10565 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10566 case 'F': break;
10567 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 10568 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 10569 case 'I': break;
e972090a 10570 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 10571 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10572 case 'L': break;
10573 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
10574 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
10575 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
10576 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
10577 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10578 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
10579 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10580 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10581 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10582 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
10583 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10584 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10585 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10586 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
10587 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10588 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
10589 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10590 case 'f': break;
10591 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
10592 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10593 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10594 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
10595 case 'l': break;
10596 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10597 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10598 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
10599 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10600 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10601 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10602 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10603 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10604 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10605 case 'x': break;
10606 case 'z': break;
10607 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
10608 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
10609 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
10610 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
10611 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
10612 case '[': break;
10613 case ']': break;
620edafd 10614 case '1': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8b082fb1 10615 case '2': USE_BITS (OP_MASK_BP, OP_SH_BP); break;
74cd071d
CF
10616 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
10617 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
10618 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
10619 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10620 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
10621 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
10622 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
10623 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
10624 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
10625 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
10626 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
10627 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
10628 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
10629 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
10630 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
dec0624d
MR
10631 case '\\': USE_BITS (OP_MASK_3BITPOS, OP_SH_3BITPOS); break;
10632 case '~': USE_BITS (OP_MASK_OFFSET12, OP_SH_OFFSET12); break;
ef2e4d86 10633 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10634 default:
10635 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10636 c, opc->name, opc->args);
10637 return 0;
10638 }
10639#undef USE_BITS
10640 if (used_bits != 0xffffffff)
10641 {
10642 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
10643 ~used_bits & 0xffffffff, opc->name, opc->args);
10644 return 0;
10645 }
10646 return 1;
10647}
10648
df58fc94
RS
10649/* For consistency checking, verify that the length implied matches the
10650 major opcode and that all bits are specified either by the match/mask
10651 part of the instruction definition, or by the operand list. */
10652
10653static int
10654validate_micromips_insn (const struct mips_opcode *opc)
10655{
10656 unsigned long match = opc->match;
10657 unsigned long mask = opc->mask;
10658 const char *p = opc->args;
10659 unsigned long insn_bits;
10660 unsigned long used_bits;
10661 unsigned long major;
10662 unsigned int length;
10663 char e;
10664 char c;
10665
10666 if ((mask & match) != match)
10667 {
10668 as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
10669 opc->name, opc->args);
10670 return 0;
10671 }
10672 length = micromips_insn_length (opc);
10673 if (length != 2 && length != 4)
10674 {
10675 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
10676 "%s %s"), length, opc->name, opc->args);
10677 return 0;
10678 }
10679 major = match >> (10 + 8 * (length - 2));
10680 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
10681 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
10682 {
10683 as_bad (_("Internal error: bad microMIPS opcode "
10684 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
10685 return 0;
10686 }
10687
10688 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
10689 insn_bits = 1 << 4 * length;
10690 insn_bits <<= 4 * length;
10691 insn_bits -= 1;
10692 used_bits = mask;
10693#define USE_BITS(field) \
10694 (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
10695 while (*p)
10696 switch (c = *p++)
10697 {
10698 case ',': break;
10699 case '(': break;
10700 case ')': break;
10701 case '+':
10702 e = c;
10703 switch (c = *p++)
10704 {
10705 case 'A': USE_BITS (EXTLSB); break;
10706 case 'B': USE_BITS (INSMSB); break;
10707 case 'C': USE_BITS (EXTMSBD); break;
10708 case 'D': USE_BITS (RS); USE_BITS (SEL); break;
10709 case 'E': USE_BITS (EXTLSB); break;
10710 case 'F': USE_BITS (INSMSB); break;
10711 case 'G': USE_BITS (EXTMSBD); break;
10712 case 'H': USE_BITS (EXTMSBD); break;
7f3c4072 10713 case 'j': USE_BITS (EVAOFFSET); break;
df58fc94
RS
10714 default:
10715 as_bad (_("Internal error: bad mips opcode "
10716 "(unknown extension operand type `%c%c'): %s %s"),
10717 e, c, opc->name, opc->args);
10718 return 0;
10719 }
10720 break;
10721 case 'm':
10722 e = c;
10723 switch (c = *p++)
10724 {
10725 case 'A': USE_BITS (IMMA); break;
10726 case 'B': USE_BITS (IMMB); break;
10727 case 'C': USE_BITS (IMMC); break;
10728 case 'D': USE_BITS (IMMD); break;
10729 case 'E': USE_BITS (IMME); break;
10730 case 'F': USE_BITS (IMMF); break;
10731 case 'G': USE_BITS (IMMG); break;
10732 case 'H': USE_BITS (IMMH); break;
10733 case 'I': USE_BITS (IMMI); break;
10734 case 'J': USE_BITS (IMMJ); break;
10735 case 'L': USE_BITS (IMML); break;
10736 case 'M': USE_BITS (IMMM); break;
10737 case 'N': USE_BITS (IMMN); break;
10738 case 'O': USE_BITS (IMMO); break;
10739 case 'P': USE_BITS (IMMP); break;
10740 case 'Q': USE_BITS (IMMQ); break;
10741 case 'U': USE_BITS (IMMU); break;
10742 case 'W': USE_BITS (IMMW); break;
10743 case 'X': USE_BITS (IMMX); break;
10744 case 'Y': USE_BITS (IMMY); break;
10745 case 'Z': break;
10746 case 'a': break;
10747 case 'b': USE_BITS (MB); break;
10748 case 'c': USE_BITS (MC); break;
10749 case 'd': USE_BITS (MD); break;
10750 case 'e': USE_BITS (ME); break;
10751 case 'f': USE_BITS (MF); break;
10752 case 'g': USE_BITS (MG); break;
10753 case 'h': USE_BITS (MH); break;
10754 case 'i': USE_BITS (MI); break;
10755 case 'j': USE_BITS (MJ); break;
10756 case 'l': USE_BITS (ML); break;
10757 case 'm': USE_BITS (MM); break;
10758 case 'n': USE_BITS (MN); break;
10759 case 'p': USE_BITS (MP); break;
10760 case 'q': USE_BITS (MQ); break;
10761 case 'r': break;
10762 case 's': break;
10763 case 't': break;
10764 case 'x': break;
10765 case 'y': break;
10766 case 'z': break;
10767 default:
10768 as_bad (_("Internal error: bad mips opcode "
10769 "(unknown extension operand type `%c%c'): %s %s"),
10770 e, c, opc->name, opc->args);
10771 return 0;
10772 }
10773 break;
10774 case '.': USE_BITS (OFFSET10); break;
10775 case '1': USE_BITS (STYPE); break;
03f66e8a
MR
10776 case '2': USE_BITS (BP); break;
10777 case '3': USE_BITS (SA3); break;
10778 case '4': USE_BITS (SA4); break;
10779 case '5': USE_BITS (IMM8); break;
10780 case '6': USE_BITS (RS); break;
10781 case '7': USE_BITS (DSPACC); break;
10782 case '8': USE_BITS (WRDSP); break;
10783 case '0': USE_BITS (DSPSFT); break;
df58fc94
RS
10784 case '<': USE_BITS (SHAMT); break;
10785 case '>': USE_BITS (SHAMT); break;
03f66e8a 10786 case '@': USE_BITS (IMM10); break;
df58fc94
RS
10787 case 'B': USE_BITS (CODE10); break;
10788 case 'C': USE_BITS (COPZ); break;
10789 case 'D': USE_BITS (FD); break;
10790 case 'E': USE_BITS (RT); break;
10791 case 'G': USE_BITS (RS); break;
444d75be 10792 case 'H': USE_BITS (SEL); break;
df58fc94
RS
10793 case 'K': USE_BITS (RS); break;
10794 case 'M': USE_BITS (CCC); break;
10795 case 'N': USE_BITS (BCC); break;
10796 case 'R': USE_BITS (FR); break;
10797 case 'S': USE_BITS (FS); break;
10798 case 'T': USE_BITS (FT); break;
10799 case 'V': USE_BITS (FS); break;
dec0624d 10800 case '\\': USE_BITS (3BITPOS); break;
03f66e8a 10801 case '^': USE_BITS (RD); break;
df58fc94
RS
10802 case 'a': USE_BITS (TARGET); break;
10803 case 'b': USE_BITS (RS); break;
10804 case 'c': USE_BITS (CODE); break;
10805 case 'd': USE_BITS (RD); break;
10806 case 'h': USE_BITS (PREFX); break;
10807 case 'i': USE_BITS (IMMEDIATE); break;
10808 case 'j': USE_BITS (DELTA); break;
10809 case 'k': USE_BITS (CACHE); break;
10810 case 'n': USE_BITS (RT); break;
10811 case 'o': USE_BITS (DELTA); break;
10812 case 'p': USE_BITS (DELTA); break;
10813 case 'q': USE_BITS (CODE2); break;
10814 case 'r': USE_BITS (RS); break;
10815 case 's': USE_BITS (RS); break;
10816 case 't': USE_BITS (RT); break;
10817 case 'u': USE_BITS (IMMEDIATE); break;
10818 case 'v': USE_BITS (RS); break;
10819 case 'w': USE_BITS (RT); break;
10820 case 'y': USE_BITS (RS3); break;
10821 case 'z': break;
10822 case '|': USE_BITS (TRAP); break;
10823 case '~': USE_BITS (OFFSET12); break;
10824 default:
10825 as_bad (_("Internal error: bad microMIPS opcode "
10826 "(unknown operand type `%c'): %s %s"),
10827 c, opc->name, opc->args);
10828 return 0;
10829 }
10830#undef USE_BITS
10831 if (used_bits != insn_bits)
10832 {
10833 if (~used_bits & insn_bits)
10834 as_bad (_("Internal error: bad microMIPS opcode "
10835 "(bits 0x%lx undefined): %s %s"),
10836 ~used_bits & insn_bits, opc->name, opc->args);
10837 if (used_bits & ~insn_bits)
10838 as_bad (_("Internal error: bad microMIPS opcode "
10839 "(bits 0x%lx defined): %s %s"),
10840 used_bits & ~insn_bits, opc->name, opc->args);
10841 return 0;
10842 }
10843 return 1;
10844}
10845
9bcd4f99
TS
10846/* UDI immediates. */
10847struct mips_immed {
10848 char type;
10849 unsigned int shift;
10850 unsigned long mask;
10851 const char * desc;
10852};
10853
10854static const struct mips_immed mips_immed[] = {
10855 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
10856 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
10857 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
10858 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
10859 { 0,0,0,0 }
10860};
10861
7455baf8
TS
10862/* Check whether an odd floating-point register is allowed. */
10863static int
10864mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
10865{
10866 const char *s = insn->name;
10867
10868 if (insn->pinfo == INSN_MACRO)
10869 /* Let a macro pass, we'll catch it later when it is expanded. */
10870 return 1;
10871
e407c74b 10872 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || (mips_opts.arch == CPU_R5900))
7455baf8
TS
10873 {
10874 /* Allow odd registers for single-precision ops. */
10875 switch (insn->pinfo & (FP_S | FP_D))
10876 {
10877 case FP_S:
10878 case 0:
10879 return 1; /* both single precision - ok */
10880 case FP_D:
10881 return 0; /* both double precision - fail */
10882 default:
10883 break;
10884 }
10885
10886 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
10887 s = strchr (insn->name, '.');
10888 if (argnum == 2)
10889 s = s != NULL ? strchr (s + 1, '.') : NULL;
10890 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
10891 }
10892
10893 /* Single-precision coprocessor loads and moves are OK too. */
10894 if ((insn->pinfo & FP_S)
10895 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
10896 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
10897 return 1;
10898
10899 return 0;
10900}
10901
df58fc94
RS
10902/* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
10903 taking bits from BIT up. */
10904static int
10905expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
10906{
10907 return (ep->X_op == O_constant
10908 && (ep->X_add_number & ((1 << bit) - 1)) == 0
10909 && ep->X_add_number >= min << bit
10910 && ep->X_add_number < max << bit);
10911}
10912
252b5132
RH
10913/* This routine assembles an instruction into its binary format. As a
10914 side effect, it sets one of the global variables imm_reloc or
10915 offset_reloc to the type of relocation to do if one of the operands
10916 is an address expression. */
10917
10918static void
17a2f251 10919mips_ip (char *str, struct mips_cl_insn *ip)
252b5132 10920{
df58fc94
RS
10921 bfd_boolean wrong_delay_slot_insns = FALSE;
10922 bfd_boolean need_delay_slot_ok = TRUE;
10923 struct mips_opcode *firstinsn = NULL;
10924 const struct mips_opcode *past;
10925 struct hash_control *hash;
252b5132
RH
10926 char *s;
10927 const char *args;
43841e91 10928 char c = 0;
252b5132
RH
10929 struct mips_opcode *insn;
10930 char *argsStart;
10931 unsigned int regno;
34224acf 10932 unsigned int lastregno;
df58fc94 10933 unsigned int destregno = 0;
af7ee8bf 10934 unsigned int lastpos = 0;
071742cf 10935 unsigned int limlo, limhi;
f02d8318 10936 int sizelo;
252b5132 10937 char *s_reset;
74cd071d 10938 offsetT min_range, max_range;
df58fc94 10939 long opend;
a40bc9dd 10940 char *name;
707bfff6
TS
10941 int argnum;
10942 unsigned int rtype;
df58fc94 10943 char *dot;
a40bc9dd 10944 long end;
252b5132
RH
10945
10946 insn_error = NULL;
10947
df58fc94
RS
10948 if (mips_opts.micromips)
10949 {
10950 hash = micromips_op_hash;
10951 past = &micromips_opcodes[bfd_micromips_num_opcodes];
10952 }
10953 else
10954 {
10955 hash = op_hash;
10956 past = &mips_opcodes[NUMOPCODES];
10957 }
10958 forced_insn_length = 0;
252b5132 10959 insn = NULL;
252b5132 10960
df58fc94 10961 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
10962 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
10963 continue;
bdaaa2e1 10964
a40bc9dd
RS
10965 /* Make a copy of the instruction so that we can fiddle with it. */
10966 name = alloca (end + 1);
10967 memcpy (name, str, end);
10968 name[end] = '\0';
252b5132 10969
df58fc94
RS
10970 for (;;)
10971 {
10972 insn = (struct mips_opcode *) hash_find (hash, name);
10973
10974 if (insn != NULL || !mips_opts.micromips)
10975 break;
10976 if (forced_insn_length)
10977 break;
10978
10979 /* See if there's an instruction size override suffix,
10980 either `16' or `32', at the end of the mnemonic proper,
10981 that defines the operation, i.e. before the first `.'
10982 character if any. Strip it and retry. */
10983 dot = strchr (name, '.');
10984 opend = dot != NULL ? dot - name : end;
10985 if (opend < 3)
10986 break;
10987 if (name[opend - 2] == '1' && name[opend - 1] == '6')
10988 forced_insn_length = 2;
10989 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
10990 forced_insn_length = 4;
10991 else
10992 break;
10993 memcpy (name + opend - 2, name + opend, end - opend + 1);
10994 }
252b5132
RH
10995 if (insn == NULL)
10996 {
a40bc9dd
RS
10997 insn_error = _("Unrecognized opcode");
10998 return;
252b5132
RH
10999 }
11000
df58fc94
RS
11001 /* For microMIPS instructions placed in a fixed-length branch delay slot
11002 we make up to two passes over the relevant fragment of the opcode
11003 table. First we try instructions that meet the delay slot's length
11004 requirement. If none matched, then we retry with the remaining ones
11005 and if one matches, then we use it and then issue an appropriate
11006 warning later on. */
a40bc9dd 11007 argsStart = s = str + end;
252b5132
RH
11008 for (;;)
11009 {
df58fc94
RS
11010 bfd_boolean delay_slot_ok;
11011 bfd_boolean size_ok;
b34976b6 11012 bfd_boolean ok;
252b5132 11013
a40bc9dd 11014 gas_assert (strcmp (insn->name, name) == 0);
252b5132 11015
f79e2745 11016 ok = is_opcode_valid (insn);
df58fc94
RS
11017 size_ok = is_size_valid (insn);
11018 delay_slot_ok = is_delay_slot_valid (insn);
11019 if (!delay_slot_ok && !wrong_delay_slot_insns)
252b5132 11020 {
df58fc94
RS
11021 firstinsn = insn;
11022 wrong_delay_slot_insns = TRUE;
11023 }
11024 if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
11025 {
11026 static char buf[256];
11027
11028 if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
252b5132
RH
11029 {
11030 ++insn;
11031 continue;
11032 }
df58fc94 11033 if (wrong_delay_slot_insns && need_delay_slot_ok)
beae10d5 11034 {
df58fc94
RS
11035 gas_assert (firstinsn);
11036 need_delay_slot_ok = FALSE;
11037 past = insn + 1;
11038 insn = firstinsn;
11039 continue;
252b5132 11040 }
df58fc94
RS
11041
11042 if (insn_error)
11043 return;
11044
11045 if (!ok)
7bd942df 11046 sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
df58fc94
RS
11047 mips_cpu_info_from_arch (mips_opts.arch)->name,
11048 mips_cpu_info_from_isa (mips_opts.isa)->name);
11049 else
11050 sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
11051 8 * forced_insn_length);
11052 insn_error = buf;
11053
11054 return;
252b5132
RH
11055 }
11056
1e915849 11057 create_insn (ip, insn);
268f6bed 11058 insn_error = NULL;
707bfff6 11059 argnum = 1;
24864476 11060 lastregno = 0xffffffff;
252b5132
RH
11061 for (args = insn->args;; ++args)
11062 {
deec1734
CD
11063 int is_mdmx;
11064
ad8d3bb3 11065 s += strspn (s, " \t");
deec1734 11066 is_mdmx = 0;
252b5132
RH
11067 switch (*args)
11068 {
11069 case '\0': /* end of args */
11070 if (*s == '\0')
11071 return;
11072 break;
11073
03f66e8a
MR
11074 case '2':
11075 /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
11076 code) or 14 (for microMIPS code). */
8b082fb1
TS
11077 my_getExpression (&imm_expr, s);
11078 check_absolute_expr (ip, &imm_expr);
11079 if ((unsigned long) imm_expr.X_add_number != 1
11080 && (unsigned long) imm_expr.X_add_number != 3)
11081 {
11082 as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
11083 (unsigned long) imm_expr.X_add_number);
11084 }
03f66e8a
MR
11085 INSERT_OPERAND (mips_opts.micromips,
11086 BP, *ip, imm_expr.X_add_number);
8b082fb1
TS
11087 imm_expr.X_op = O_absent;
11088 s = expr_end;
11089 continue;
11090
03f66e8a
MR
11091 case '3':
11092 /* DSP 3-bit unsigned immediate in bit 13 (for standard MIPS
11093 code) or 21 (for microMIPS code). */
11094 {
11095 unsigned long mask = (mips_opts.micromips
11096 ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
11097
11098 my_getExpression (&imm_expr, s);
11099 check_absolute_expr (ip, &imm_expr);
11100 if ((unsigned long) imm_expr.X_add_number > mask)
11101 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11102 mask, (unsigned long) imm_expr.X_add_number);
11103 INSERT_OPERAND (mips_opts.micromips,
11104 SA3, *ip, imm_expr.X_add_number);
11105 imm_expr.X_op = O_absent;
11106 s = expr_end;
11107 }
74cd071d
CF
11108 continue;
11109
03f66e8a
MR
11110 case '4':
11111 /* DSP 4-bit unsigned immediate in bit 12 (for standard MIPS
11112 code) or 21 (for microMIPS code). */
11113 {
11114 unsigned long mask = (mips_opts.micromips
11115 ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
11116
11117 my_getExpression (&imm_expr, s);
11118 check_absolute_expr (ip, &imm_expr);
11119 if ((unsigned long) imm_expr.X_add_number > mask)
11120 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11121 mask, (unsigned long) imm_expr.X_add_number);
11122 INSERT_OPERAND (mips_opts.micromips,
11123 SA4, *ip, imm_expr.X_add_number);
11124 imm_expr.X_op = O_absent;
11125 s = expr_end;
11126 }
74cd071d
CF
11127 continue;
11128
03f66e8a
MR
11129 case '5':
11130 /* DSP 8-bit unsigned immediate in bit 13 (for standard MIPS
11131 code) or 16 (for microMIPS code). */
11132 {
11133 unsigned long mask = (mips_opts.micromips
11134 ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
11135
11136 my_getExpression (&imm_expr, s);
11137 check_absolute_expr (ip, &imm_expr);
11138 if ((unsigned long) imm_expr.X_add_number > mask)
11139 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11140 mask, (unsigned long) imm_expr.X_add_number);
11141 INSERT_OPERAND (mips_opts.micromips,
11142 IMM8, *ip, imm_expr.X_add_number);
11143 imm_expr.X_op = O_absent;
11144 s = expr_end;
11145 }
74cd071d
CF
11146 continue;
11147
03f66e8a
MR
11148 case '6':
11149 /* DSP 5-bit unsigned immediate in bit 16 (for standard MIPS
11150 code) or 21 (for microMIPS code). */
11151 {
11152 unsigned long mask = (mips_opts.micromips
11153 ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
11154
11155 my_getExpression (&imm_expr, s);
11156 check_absolute_expr (ip, &imm_expr);
11157 if ((unsigned long) imm_expr.X_add_number > mask)
11158 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11159 mask, (unsigned long) imm_expr.X_add_number);
11160 INSERT_OPERAND (mips_opts.micromips,
11161 RS, *ip, imm_expr.X_add_number);
11162 imm_expr.X_op = O_absent;
11163 s = expr_end;
11164 }
74cd071d
CF
11165 continue;
11166
90ecf173 11167 case '7': /* Four DSP accumulators in bits 11,12. */
03f66e8a
MR
11168 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11169 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
11170 {
11171 regno = s[3] - '0';
11172 s += 4;
03f66e8a 11173 INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
74cd071d
CF
11174 continue;
11175 }
11176 else
11177 as_bad (_("Invalid dsp acc register"));
11178 break;
11179
03f66e8a
MR
11180 case '8':
11181 /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
11182 code) or 14 (for microMIPS code). */
11183 {
11184 unsigned long mask = (mips_opts.micromips
11185 ? MICROMIPSOP_MASK_WRDSP
11186 : OP_MASK_WRDSP);
11187
11188 my_getExpression (&imm_expr, s);
11189 check_absolute_expr (ip, &imm_expr);
11190 if ((unsigned long) imm_expr.X_add_number > mask)
11191 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11192 mask, (unsigned long) imm_expr.X_add_number);
11193 INSERT_OPERAND (mips_opts.micromips,
11194 WRDSP, *ip, imm_expr.X_add_number);
11195 imm_expr.X_op = O_absent;
11196 s = expr_end;
11197 }
74cd071d
CF
11198 continue;
11199
90ecf173 11200 case '9': /* Four DSP accumulators in bits 21,22. */
df58fc94 11201 gas_assert (!mips_opts.micromips);
03f66e8a
MR
11202 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11203 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
11204 {
11205 regno = s[3] - '0';
11206 s += 4;
df58fc94 11207 INSERT_OPERAND (0, DSPACC_S, *ip, regno);
74cd071d
CF
11208 continue;
11209 }
11210 else
11211 as_bad (_("Invalid dsp acc register"));
11212 break;
11213
03f66e8a
MR
11214 case '0':
11215 /* DSP 6-bit signed immediate in bit 16 (for standard MIPS
11216 code) or 20 (for microMIPS code). */
11217 {
11218 long mask = (mips_opts.micromips
11219 ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
11220
11221 my_getExpression (&imm_expr, s);
11222 check_absolute_expr (ip, &imm_expr);
11223 min_range = -((mask + 1) >> 1);
11224 max_range = ((mask + 1) >> 1) - 1;
11225 if (imm_expr.X_add_number < min_range
11226 || imm_expr.X_add_number > max_range)
a9e24354
TS
11227 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11228 (long) min_range, (long) max_range,
11229 (long) imm_expr.X_add_number);
03f66e8a
MR
11230 INSERT_OPERAND (mips_opts.micromips,
11231 DSPSFT, *ip, imm_expr.X_add_number);
11232 imm_expr.X_op = O_absent;
11233 s = expr_end;
11234 }
74cd071d
CF
11235 continue;
11236
90ecf173 11237 case '\'': /* DSP 6-bit unsigned immediate in bit 16. */
df58fc94 11238 gas_assert (!mips_opts.micromips);
74cd071d
CF
11239 my_getExpression (&imm_expr, s);
11240 check_absolute_expr (ip, &imm_expr);
11241 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
11242 {
a9e24354
TS
11243 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11244 OP_MASK_RDDSP,
11245 (unsigned long) imm_expr.X_add_number);
74cd071d 11246 }
df58fc94 11247 INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
11248 imm_expr.X_op = O_absent;
11249 s = expr_end;
11250 continue;
11251
90ecf173 11252 case ':': /* DSP 7-bit signed immediate in bit 19. */
df58fc94 11253 gas_assert (!mips_opts.micromips);
74cd071d
CF
11254 my_getExpression (&imm_expr, s);
11255 check_absolute_expr (ip, &imm_expr);
11256 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
11257 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
11258 if (imm_expr.X_add_number < min_range ||
11259 imm_expr.X_add_number > max_range)
11260 {
a9e24354
TS
11261 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11262 (long) min_range, (long) max_range,
11263 (long) imm_expr.X_add_number);
74cd071d 11264 }
df58fc94 11265 INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
74cd071d
CF
11266 imm_expr.X_op = O_absent;
11267 s = expr_end;
11268 continue;
11269
90ecf173 11270 case '@': /* DSP 10-bit signed immediate in bit 16. */
03f66e8a
MR
11271 {
11272 long mask = (mips_opts.micromips
11273 ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
11274
11275 my_getExpression (&imm_expr, s);
11276 check_absolute_expr (ip, &imm_expr);
11277 min_range = -((mask + 1) >> 1);
11278 max_range = ((mask + 1) >> 1) - 1;
11279 if (imm_expr.X_add_number < min_range
11280 || imm_expr.X_add_number > max_range)
a9e24354
TS
11281 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11282 (long) min_range, (long) max_range,
11283 (long) imm_expr.X_add_number);
03f66e8a
MR
11284 INSERT_OPERAND (mips_opts.micromips,
11285 IMM10, *ip, imm_expr.X_add_number);
11286 imm_expr.X_op = O_absent;
11287 s = expr_end;
11288 }
11289 continue;
11290
11291 case '^': /* DSP 5-bit unsigned immediate in bit 11. */
11292 gas_assert (mips_opts.micromips);
11293 my_getExpression (&imm_expr, s);
11294 check_absolute_expr (ip, &imm_expr);
11295 if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11296 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11297 MICROMIPSOP_MASK_RD,
11298 (unsigned long) imm_expr.X_add_number);
11299 INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
74cd071d
CF
11300 imm_expr.X_op = O_absent;
11301 s = expr_end;
11302 continue;
11303
a9e24354 11304 case '!': /* MT usermode flag bit. */
df58fc94 11305 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11306 my_getExpression (&imm_expr, s);
11307 check_absolute_expr (ip, &imm_expr);
11308 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
a9e24354
TS
11309 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11310 (unsigned long) imm_expr.X_add_number);
df58fc94 11311 INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11312 imm_expr.X_op = O_absent;
11313 s = expr_end;
11314 continue;
11315
a9e24354 11316 case '$': /* MT load high flag bit. */
df58fc94 11317 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11318 my_getExpression (&imm_expr, s);
11319 check_absolute_expr (ip, &imm_expr);
11320 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
a9e24354
TS
11321 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11322 (unsigned long) imm_expr.X_add_number);
df58fc94 11323 INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11324 imm_expr.X_op = O_absent;
11325 s = expr_end;
11326 continue;
11327
90ecf173 11328 case '*': /* Four DSP accumulators in bits 18,19. */
df58fc94 11329 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11330 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11331 s[3] >= '0' && s[3] <= '3')
11332 {
11333 regno = s[3] - '0';
11334 s += 4;
df58fc94 11335 INSERT_OPERAND (0, MTACC_T, *ip, regno);
ef2e4d86
CF
11336 continue;
11337 }
11338 else
11339 as_bad (_("Invalid dsp/smartmips acc register"));
11340 break;
11341
90ecf173 11342 case '&': /* Four DSP accumulators in bits 13,14. */
df58fc94 11343 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11344 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11345 s[3] >= '0' && s[3] <= '3')
11346 {
11347 regno = s[3] - '0';
11348 s += 4;
df58fc94 11349 INSERT_OPERAND (0, MTACC_D, *ip, regno);
ef2e4d86
CF
11350 continue;
11351 }
11352 else
11353 as_bad (_("Invalid dsp/smartmips acc register"));
11354 break;
11355
dec0624d
MR
11356 case '\\': /* 3-bit bit position. */
11357 {
2906b037
MR
11358 unsigned long mask = (mips_opts.micromips
11359 ? MICROMIPSOP_MASK_3BITPOS
11360 : OP_MASK_3BITPOS);
dec0624d
MR
11361
11362 my_getExpression (&imm_expr, s);
11363 check_absolute_expr (ip, &imm_expr);
11364 if ((unsigned long) imm_expr.X_add_number > mask)
11365 as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11366 ip->insn_mo->name,
11367 mask, (unsigned long) imm_expr.X_add_number);
11368 INSERT_OPERAND (mips_opts.micromips,
11369 3BITPOS, *ip, imm_expr.X_add_number);
11370 imm_expr.X_op = O_absent;
11371 s = expr_end;
11372 }
11373 continue;
11374
252b5132 11375 case ',':
a339155f 11376 ++argnum;
252b5132
RH
11377 if (*s++ == *args)
11378 continue;
11379 s--;
11380 switch (*++args)
11381 {
11382 case 'r':
11383 case 'v':
df58fc94 11384 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132
RH
11385 continue;
11386
11387 case 'w':
df58fc94 11388 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
38487616
TS
11389 continue;
11390
252b5132 11391 case 'W':
df58fc94
RS
11392 gas_assert (!mips_opts.micromips);
11393 INSERT_OPERAND (0, FT, *ip, lastregno);
252b5132
RH
11394 continue;
11395
11396 case 'V':
df58fc94 11397 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132
RH
11398 continue;
11399 }
11400 break;
11401
11402 case '(':
11403 /* Handle optional base register.
11404 Either the base register is omitted or
bdaaa2e1 11405 we must have a left paren. */
252b5132
RH
11406 /* This is dependent on the next operand specifier
11407 is a base register specification. */
df58fc94
RS
11408 gas_assert (args[1] == 'b'
11409 || (mips_opts.micromips
11410 && args[1] == 'm'
11411 && (args[2] == 'l' || args[2] == 'n'
11412 || args[2] == 's' || args[2] == 'a')));
11413 if (*s == '\0' && args[1] == 'b')
252b5132 11414 return;
df58fc94 11415 /* Fall through. */
252b5132 11416
90ecf173 11417 case ')': /* These must match exactly. */
df58fc94
RS
11418 if (*s++ == *args)
11419 continue;
11420 break;
11421
11422 case '[': /* These must match exactly. */
60b63b72 11423 case ']':
df58fc94 11424 gas_assert (!mips_opts.micromips);
252b5132
RH
11425 if (*s++ == *args)
11426 continue;
11427 break;
11428
af7ee8bf
CD
11429 case '+': /* Opcode extension character. */
11430 switch (*++args)
11431 {
9bcd4f99
TS
11432 case '1': /* UDI immediates. */
11433 case '2':
11434 case '3':
11435 case '4':
df58fc94 11436 gas_assert (!mips_opts.micromips);
9bcd4f99
TS
11437 {
11438 const struct mips_immed *imm = mips_immed;
11439
11440 while (imm->type && imm->type != *args)
11441 ++imm;
11442 if (! imm->type)
b37df7c4 11443 abort ();
9bcd4f99
TS
11444 my_getExpression (&imm_expr, s);
11445 check_absolute_expr (ip, &imm_expr);
11446 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11447 {
11448 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11449 imm->desc ? imm->desc : ip->insn_mo->name,
11450 (unsigned long) imm_expr.X_add_number,
11451 (unsigned long) imm_expr.X_add_number);
90ecf173 11452 imm_expr.X_add_number &= imm->mask;
9bcd4f99
TS
11453 }
11454 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11455 << imm->shift);
11456 imm_expr.X_op = O_absent;
11457 s = expr_end;
11458 }
11459 continue;
90ecf173 11460
b015e599
AP
11461 case 'J': /* 10-bit hypcall code. */
11462 gas_assert (!mips_opts.micromips);
11463 {
11464 unsigned long mask = OP_MASK_CODE10;
11465
11466 my_getExpression (&imm_expr, s);
11467 check_absolute_expr (ip, &imm_expr);
11468 if ((unsigned long) imm_expr.X_add_number > mask)
11469 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11470 ip->insn_mo->name,
11471 mask, (unsigned long) imm_expr.X_add_number);
11472 INSERT_OPERAND (0, CODE10, *ip, imm_expr.X_add_number);
11473 imm_expr.X_op = O_absent;
11474 s = expr_end;
11475 }
11476 continue;
11477
071742cf
CD
11478 case 'A': /* ins/ext position, becomes LSB. */
11479 limlo = 0;
11480 limhi = 31;
5f74bc13
CD
11481 goto do_lsb;
11482 case 'E':
11483 limlo = 32;
11484 limhi = 63;
11485 goto do_lsb;
90ecf173 11486 do_lsb:
071742cf
CD
11487 my_getExpression (&imm_expr, s);
11488 check_absolute_expr (ip, &imm_expr);
11489 if ((unsigned long) imm_expr.X_add_number < limlo
11490 || (unsigned long) imm_expr.X_add_number > limhi)
11491 {
11492 as_bad (_("Improper position (%lu)"),
11493 (unsigned long) imm_expr.X_add_number);
11494 imm_expr.X_add_number = limlo;
11495 }
11496 lastpos = imm_expr.X_add_number;
df58fc94
RS
11497 INSERT_OPERAND (mips_opts.micromips,
11498 EXTLSB, *ip, imm_expr.X_add_number);
071742cf
CD
11499 imm_expr.X_op = O_absent;
11500 s = expr_end;
11501 continue;
11502
11503 case 'B': /* ins size, becomes MSB. */
11504 limlo = 1;
11505 limhi = 32;
5f74bc13
CD
11506 goto do_msb;
11507 case 'F':
11508 limlo = 33;
11509 limhi = 64;
11510 goto do_msb;
90ecf173 11511 do_msb:
071742cf
CD
11512 my_getExpression (&imm_expr, s);
11513 check_absolute_expr (ip, &imm_expr);
11514 /* Check for negative input so that small negative numbers
11515 will not succeed incorrectly. The checks against
11516 (pos+size) transitively check "size" itself,
11517 assuming that "pos" is reasonable. */
11518 if ((long) imm_expr.X_add_number < 0
11519 || ((unsigned long) imm_expr.X_add_number
11520 + lastpos) < limlo
11521 || ((unsigned long) imm_expr.X_add_number
11522 + lastpos) > limhi)
11523 {
11524 as_bad (_("Improper insert size (%lu, position %lu)"),
11525 (unsigned long) imm_expr.X_add_number,
11526 (unsigned long) lastpos);
11527 imm_expr.X_add_number = limlo - lastpos;
11528 }
df58fc94
RS
11529 INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11530 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
11531 imm_expr.X_op = O_absent;
11532 s = expr_end;
11533 continue;
11534
11535 case 'C': /* ext size, becomes MSBD. */
11536 limlo = 1;
11537 limhi = 32;
f02d8318 11538 sizelo = 1;
5f74bc13
CD
11539 goto do_msbd;
11540 case 'G':
11541 limlo = 33;
11542 limhi = 64;
f02d8318 11543 sizelo = 33;
5f74bc13
CD
11544 goto do_msbd;
11545 case 'H':
11546 limlo = 33;
11547 limhi = 64;
f02d8318 11548 sizelo = 1;
5f74bc13 11549 goto do_msbd;
90ecf173 11550 do_msbd:
071742cf
CD
11551 my_getExpression (&imm_expr, s);
11552 check_absolute_expr (ip, &imm_expr);
f02d8318
CF
11553 /* The checks against (pos+size) don't transitively check
11554 "size" itself, assuming that "pos" is reasonable.
11555 We also need to check the lower bound of "size". */
11556 if ((long) imm_expr.X_add_number < sizelo
071742cf
CD
11557 || ((unsigned long) imm_expr.X_add_number
11558 + lastpos) < limlo
11559 || ((unsigned long) imm_expr.X_add_number
11560 + lastpos) > limhi)
11561 {
11562 as_bad (_("Improper extract size (%lu, position %lu)"),
11563 (unsigned long) imm_expr.X_add_number,
11564 (unsigned long) lastpos);
11565 imm_expr.X_add_number = limlo - lastpos;
11566 }
df58fc94
RS
11567 INSERT_OPERAND (mips_opts.micromips,
11568 EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
11569 imm_expr.X_op = O_absent;
11570 s = expr_end;
11571 continue;
af7ee8bf 11572
bbcc0807
CD
11573 case 'D':
11574 /* +D is for disassembly only; never match. */
11575 break;
11576
5f74bc13
CD
11577 case 'I':
11578 /* "+I" is like "I", except that imm2_expr is used. */
11579 my_getExpression (&imm2_expr, s);
11580 if (imm2_expr.X_op != O_big
11581 && imm2_expr.X_op != O_constant)
11582 insn_error = _("absolute expression required");
9ee2a2d4
MR
11583 if (HAVE_32BIT_GPRS)
11584 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
11585 s = expr_end;
11586 continue;
11587
707bfff6 11588 case 'T': /* Coprocessor register. */
df58fc94 11589 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11590 /* +T is for disassembly only; never match. */
11591 break;
11592
707bfff6 11593 case 't': /* Coprocessor register number. */
df58fc94 11594 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11595 if (s[0] == '$' && ISDIGIT (s[1]))
11596 {
11597 ++s;
11598 regno = 0;
11599 do
11600 {
11601 regno *= 10;
11602 regno += *s - '0';
11603 ++s;
11604 }
11605 while (ISDIGIT (*s));
11606 if (regno > 31)
11607 as_bad (_("Invalid register number (%d)"), regno);
11608 else
11609 {
df58fc94 11610 INSERT_OPERAND (0, RT, *ip, regno);
ef2e4d86
CF
11611 continue;
11612 }
11613 }
11614 else
11615 as_bad (_("Invalid coprocessor 0 register number"));
11616 break;
11617
bb35fb24
NC
11618 case 'x':
11619 /* bbit[01] and bbit[01]32 bit index. Give error if index
11620 is not in the valid range. */
df58fc94 11621 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11622 my_getExpression (&imm_expr, s);
11623 check_absolute_expr (ip, &imm_expr);
11624 if ((unsigned) imm_expr.X_add_number > 31)
11625 {
11626 as_bad (_("Improper bit index (%lu)"),
11627 (unsigned long) imm_expr.X_add_number);
11628 imm_expr.X_add_number = 0;
11629 }
df58fc94 11630 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
bb35fb24
NC
11631 imm_expr.X_op = O_absent;
11632 s = expr_end;
11633 continue;
11634
11635 case 'X':
11636 /* bbit[01] bit index when bbit is used but we generate
11637 bbit[01]32 because the index is over 32. Move to the
11638 next candidate if index is not in the valid range. */
df58fc94 11639 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11640 my_getExpression (&imm_expr, s);
11641 check_absolute_expr (ip, &imm_expr);
11642 if ((unsigned) imm_expr.X_add_number < 32
11643 || (unsigned) imm_expr.X_add_number > 63)
11644 break;
df58fc94 11645 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
11646 imm_expr.X_op = O_absent;
11647 s = expr_end;
11648 continue;
11649
11650 case 'p':
11651 /* cins, cins32, exts and exts32 position field. Give error
11652 if it's not in the valid range. */
df58fc94 11653 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11654 my_getExpression (&imm_expr, s);
11655 check_absolute_expr (ip, &imm_expr);
11656 if ((unsigned) imm_expr.X_add_number > 31)
11657 {
11658 as_bad (_("Improper position (%lu)"),
11659 (unsigned long) imm_expr.X_add_number);
11660 imm_expr.X_add_number = 0;
11661 }
11662 /* Make the pos explicit to simplify +S. */
11663 lastpos = imm_expr.X_add_number + 32;
df58fc94 11664 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
bb35fb24
NC
11665 imm_expr.X_op = O_absent;
11666 s = expr_end;
11667 continue;
11668
11669 case 'P':
11670 /* cins, cins32, exts and exts32 position field. Move to
11671 the next candidate if it's not in the valid range. */
df58fc94 11672 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11673 my_getExpression (&imm_expr, s);
11674 check_absolute_expr (ip, &imm_expr);
11675 if ((unsigned) imm_expr.X_add_number < 32
11676 || (unsigned) imm_expr.X_add_number > 63)
11677 break;
11678 lastpos = imm_expr.X_add_number;
df58fc94 11679 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
11680 imm_expr.X_op = O_absent;
11681 s = expr_end;
11682 continue;
11683
11684 case 's':
11685 /* cins and exts length-minus-one field. */
df58fc94 11686 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11687 my_getExpression (&imm_expr, s);
11688 check_absolute_expr (ip, &imm_expr);
11689 if ((unsigned long) imm_expr.X_add_number > 31)
11690 {
11691 as_bad (_("Improper size (%lu)"),
11692 (unsigned long) imm_expr.X_add_number);
11693 imm_expr.X_add_number = 0;
11694 }
df58fc94 11695 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
11696 imm_expr.X_op = O_absent;
11697 s = expr_end;
11698 continue;
11699
11700 case 'S':
11701 /* cins32/exts32 and cins/exts aliasing cint32/exts32
11702 length-minus-one field. */
df58fc94 11703 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11704 my_getExpression (&imm_expr, s);
11705 check_absolute_expr (ip, &imm_expr);
11706 if ((long) imm_expr.X_add_number < 0
11707 || (unsigned long) imm_expr.X_add_number + lastpos > 63)
11708 {
11709 as_bad (_("Improper size (%lu)"),
11710 (unsigned long) imm_expr.X_add_number);
11711 imm_expr.X_add_number = 0;
11712 }
df58fc94 11713 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
11714 imm_expr.X_op = O_absent;
11715 s = expr_end;
11716 continue;
11717
dd3cbb7e
NC
11718 case 'Q':
11719 /* seqi/snei immediate field. */
df58fc94 11720 gas_assert (!mips_opts.micromips);
dd3cbb7e
NC
11721 my_getExpression (&imm_expr, s);
11722 check_absolute_expr (ip, &imm_expr);
11723 if ((long) imm_expr.X_add_number < -512
11724 || (long) imm_expr.X_add_number >= 512)
11725 {
11726 as_bad (_("Improper immediate (%ld)"),
11727 (long) imm_expr.X_add_number);
11728 imm_expr.X_add_number = 0;
11729 }
df58fc94 11730 INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
dd3cbb7e
NC
11731 imm_expr.X_op = O_absent;
11732 s = expr_end;
11733 continue;
11734
98675402 11735 case 'a': /* 8-bit signed offset in bit 6 */
df58fc94 11736 gas_assert (!mips_opts.micromips);
98675402
RS
11737 my_getExpression (&imm_expr, s);
11738 check_absolute_expr (ip, &imm_expr);
11739 min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
11740 max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
11741 if (imm_expr.X_add_number < min_range
11742 || imm_expr.X_add_number > max_range)
11743 {
c95354ed 11744 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11745 (long) min_range, (long) max_range,
11746 (long) imm_expr.X_add_number);
11747 }
df58fc94 11748 INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
98675402
RS
11749 imm_expr.X_op = O_absent;
11750 s = expr_end;
11751 continue;
11752
11753 case 'b': /* 8-bit signed offset in bit 3 */
df58fc94 11754 gas_assert (!mips_opts.micromips);
98675402
RS
11755 my_getExpression (&imm_expr, s);
11756 check_absolute_expr (ip, &imm_expr);
11757 min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
11758 max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
11759 if (imm_expr.X_add_number < min_range
11760 || imm_expr.X_add_number > max_range)
11761 {
c95354ed 11762 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11763 (long) min_range, (long) max_range,
11764 (long) imm_expr.X_add_number);
11765 }
df58fc94 11766 INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
98675402
RS
11767 imm_expr.X_op = O_absent;
11768 s = expr_end;
11769 continue;
11770
11771 case 'c': /* 9-bit signed offset in bit 6 */
df58fc94 11772 gas_assert (!mips_opts.micromips);
98675402
RS
11773 my_getExpression (&imm_expr, s);
11774 check_absolute_expr (ip, &imm_expr);
11775 min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
11776 max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
c95354ed
MX
11777 /* We check the offset range before adjusted. */
11778 min_range <<= 4;
11779 max_range <<= 4;
98675402
RS
11780 if (imm_expr.X_add_number < min_range
11781 || imm_expr.X_add_number > max_range)
11782 {
c95354ed 11783 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
11784 (long) min_range, (long) max_range,
11785 (long) imm_expr.X_add_number);
11786 }
c95354ed
MX
11787 if (imm_expr.X_add_number & 0xf)
11788 {
11789 as_bad (_("Offset not 16 bytes alignment (%ld)"),
11790 (long) imm_expr.X_add_number);
11791 }
11792 /* Right shift 4 bits to adjust the offset operand. */
df58fc94
RS
11793 INSERT_OPERAND (0, OFFSET_C, *ip,
11794 imm_expr.X_add_number >> 4);
98675402
RS
11795 imm_expr.X_op = O_absent;
11796 s = expr_end;
11797 continue;
11798
11799 case 'z':
df58fc94 11800 gas_assert (!mips_opts.micromips);
98675402
RS
11801 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
11802 break;
11803 if (regno == AT && mips_opts.at)
11804 {
11805 if (mips_opts.at == ATREG)
11806 as_warn (_("used $at without \".set noat\""));
11807 else
11808 as_warn (_("used $%u with \".set at=$%u\""),
11809 regno, mips_opts.at);
11810 }
df58fc94 11811 INSERT_OPERAND (0, RZ, *ip, regno);
98675402
RS
11812 continue;
11813
11814 case 'Z':
df58fc94 11815 gas_assert (!mips_opts.micromips);
98675402
RS
11816 if (!reg_lookup (&s, RTYPE_FPU, &regno))
11817 break;
df58fc94 11818 INSERT_OPERAND (0, FZ, *ip, regno);
98675402
RS
11819 continue;
11820
7f3c4072
CM
11821 case 'j':
11822 {
11823 int shift = 8;
11824 size_t i;
11825 /* Check whether there is only a single bracketed expression
11826 left. If so, it must be the base register and the
11827 constant must be zero. */
11828 if (*s == '(' && strchr (s + 1, '(') == 0)
11829 continue;
11830
11831 /* If this value won't fit into the offset, then go find
11832 a macro that will generate a 16- or 32-bit offset code
11833 pattern. */
11834 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11835 if ((i == 0 && (imm_expr.X_op != O_constant
11836 || imm_expr.X_add_number >= 1 << shift
11837 || imm_expr.X_add_number < -1 << shift))
11838 || i > 0)
11839 {
11840 imm_expr.X_op = O_absent;
11841 break;
11842 }
11843 INSERT_OPERAND (mips_opts.micromips, EVAOFFSET, *ip,
11844 imm_expr.X_add_number);
11845 imm_expr.X_op = O_absent;
11846 s = expr_end;
11847 }
11848 continue;
11849
af7ee8bf 11850 default:
df58fc94 11851 as_bad (_("Internal error: bad %s opcode "
90ecf173 11852 "(unknown extension operand type `+%c'): %s %s"),
df58fc94 11853 mips_opts.micromips ? "microMIPS" : "MIPS",
90ecf173 11854 *args, insn->name, insn->args);
af7ee8bf
CD
11855 /* Further processing is fruitless. */
11856 return;
11857 }
11858 break;
11859
df58fc94 11860 case '.': /* 10-bit offset. */
df58fc94 11861 gas_assert (mips_opts.micromips);
dec0624d 11862 case '~': /* 12-bit offset. */
df58fc94
RS
11863 {
11864 int shift = *args == '.' ? 9 : 11;
11865 size_t i;
11866
11867 /* Check whether there is only a single bracketed expression
11868 left. If so, it must be the base register and the
11869 constant must be zero. */
11870 if (*s == '(' && strchr (s + 1, '(') == 0)
11871 continue;
11872
11873 /* If this value won't fit into the offset, then go find
11874 a macro that will generate a 16- or 32-bit offset code
11875 pattern. */
11876 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11877 if ((i == 0 && (imm_expr.X_op != O_constant
11878 || imm_expr.X_add_number >= 1 << shift
11879 || imm_expr.X_add_number < -1 << shift))
11880 || i > 0)
11881 {
11882 imm_expr.X_op = O_absent;
11883 break;
11884 }
11885 if (shift == 9)
11886 INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
11887 else
dec0624d
MR
11888 INSERT_OPERAND (mips_opts.micromips,
11889 OFFSET12, *ip, imm_expr.X_add_number);
df58fc94
RS
11890 imm_expr.X_op = O_absent;
11891 s = expr_end;
11892 }
11893 continue;
11894
252b5132
RH
11895 case '<': /* must be at least one digit */
11896 /*
11897 * According to the manual, if the shift amount is greater
b6ff326e
KH
11898 * than 31 or less than 0, then the shift amount should be
11899 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
11900 * We issue a warning and mask out all but the low 5 bits.
11901 */
11902 my_getExpression (&imm_expr, s);
11903 check_absolute_expr (ip, &imm_expr);
11904 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
11905 as_warn (_("Improper shift amount (%lu)"),
11906 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
11907 INSERT_OPERAND (mips_opts.micromips,
11908 SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
11909 imm_expr.X_op = O_absent;
11910 s = expr_end;
11911 continue;
11912
11913 case '>': /* shift amount minus 32 */
11914 my_getExpression (&imm_expr, s);
11915 check_absolute_expr (ip, &imm_expr);
11916 if ((unsigned long) imm_expr.X_add_number < 32
11917 || (unsigned long) imm_expr.X_add_number > 63)
11918 break;
df58fc94
RS
11919 INSERT_OPERAND (mips_opts.micromips,
11920 SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
11921 imm_expr.X_op = O_absent;
11922 s = expr_end;
11923 continue;
11924
90ecf173
MR
11925 case 'k': /* CACHE code. */
11926 case 'h': /* PREFX code. */
11927 case '1': /* SYNC type. */
252b5132
RH
11928 my_getExpression (&imm_expr, s);
11929 check_absolute_expr (ip, &imm_expr);
11930 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
11931 as_warn (_("Invalid value for `%s' (%lu)"),
11932 ip->insn_mo->name,
11933 (unsigned long) imm_expr.X_add_number);
df58fc94 11934 switch (*args)
d954098f 11935 {
df58fc94
RS
11936 case 'k':
11937 if (mips_fix_cn63xxp1
11938 && !mips_opts.micromips
11939 && strcmp ("pref", insn->name) == 0)
d954098f
DD
11940 switch (imm_expr.X_add_number)
11941 {
11942 case 5:
11943 case 25:
11944 case 26:
11945 case 27:
11946 case 28:
11947 case 29:
11948 case 30:
11949 case 31: /* These are ok. */
11950 break;
11951
11952 default: /* The rest must be changed to 28. */
11953 imm_expr.X_add_number = 28;
11954 break;
11955 }
df58fc94
RS
11956 INSERT_OPERAND (mips_opts.micromips,
11957 CACHE, *ip, imm_expr.X_add_number);
11958 break;
11959 case 'h':
11960 INSERT_OPERAND (mips_opts.micromips,
11961 PREFX, *ip, imm_expr.X_add_number);
11962 break;
11963 case '1':
11964 INSERT_OPERAND (mips_opts.micromips,
11965 STYPE, *ip, imm_expr.X_add_number);
11966 break;
d954098f 11967 }
252b5132
RH
11968 imm_expr.X_op = O_absent;
11969 s = expr_end;
11970 continue;
11971
90ecf173 11972 case 'c': /* BREAK code. */
df58fc94
RS
11973 {
11974 unsigned long mask = (mips_opts.micromips
11975 ? MICROMIPSOP_MASK_CODE
11976 : OP_MASK_CODE);
11977
11978 my_getExpression (&imm_expr, s);
11979 check_absolute_expr (ip, &imm_expr);
11980 if ((unsigned long) imm_expr.X_add_number > mask)
11981 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11982 ip->insn_mo->name,
11983 mask, (unsigned long) imm_expr.X_add_number);
11984 INSERT_OPERAND (mips_opts.micromips,
11985 CODE, *ip, imm_expr.X_add_number);
11986 imm_expr.X_op = O_absent;
11987 s = expr_end;
11988 }
252b5132
RH
11989 continue;
11990
90ecf173 11991 case 'q': /* Lower BREAK code. */
df58fc94
RS
11992 {
11993 unsigned long mask = (mips_opts.micromips
11994 ? MICROMIPSOP_MASK_CODE2
11995 : OP_MASK_CODE2);
11996
11997 my_getExpression (&imm_expr, s);
11998 check_absolute_expr (ip, &imm_expr);
11999 if ((unsigned long) imm_expr.X_add_number > mask)
12000 as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
12001 ip->insn_mo->name,
12002 mask, (unsigned long) imm_expr.X_add_number);
12003 INSERT_OPERAND (mips_opts.micromips,
12004 CODE2, *ip, imm_expr.X_add_number);
12005 imm_expr.X_op = O_absent;
12006 s = expr_end;
12007 }
252b5132
RH
12008 continue;
12009
df58fc94
RS
12010 case 'B': /* 20- or 10-bit syscall/break/wait code. */
12011 {
12012 unsigned long mask = (mips_opts.micromips
12013 ? MICROMIPSOP_MASK_CODE10
12014 : OP_MASK_CODE20);
12015
12016 my_getExpression (&imm_expr, s);
12017 check_absolute_expr (ip, &imm_expr);
12018 if ((unsigned long) imm_expr.X_add_number > mask)
12019 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
12020 ip->insn_mo->name,
12021 mask, (unsigned long) imm_expr.X_add_number);
12022 if (mips_opts.micromips)
12023 INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
12024 else
12025 INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
12026 imm_expr.X_op = O_absent;
12027 s = expr_end;
12028 }
252b5132
RH
12029 continue;
12030
df58fc94
RS
12031 case 'C': /* 25- or 23-bit coprocessor code. */
12032 {
12033 unsigned long mask = (mips_opts.micromips
12034 ? MICROMIPSOP_MASK_COPZ
12035 : OP_MASK_COPZ);
12036
12037 my_getExpression (&imm_expr, s);
12038 check_absolute_expr (ip, &imm_expr);
12039 if ((unsigned long) imm_expr.X_add_number > mask)
12040 as_warn (_("Coproccesor code > %u bits (%lu)"),
12041 mips_opts.micromips ? 23U : 25U,
793b27f4 12042 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
12043 INSERT_OPERAND (mips_opts.micromips,
12044 COPZ, *ip, imm_expr.X_add_number);
12045 imm_expr.X_op = O_absent;
12046 s = expr_end;
12047 }
beae10d5 12048 continue;
252b5132 12049
df58fc94
RS
12050 case 'J': /* 19-bit WAIT code. */
12051 gas_assert (!mips_opts.micromips);
4372b673
NC
12052 my_getExpression (&imm_expr, s);
12053 check_absolute_expr (ip, &imm_expr);
793b27f4 12054 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
df58fc94
RS
12055 {
12056 as_warn (_("Illegal 19-bit code (%lu)"),
a9e24354 12057 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
12058 imm_expr.X_add_number &= OP_MASK_CODE19;
12059 }
12060 INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
12061 imm_expr.X_op = O_absent;
12062 s = expr_end;
12063 continue;
12064
707bfff6 12065 case 'P': /* Performance register. */
df58fc94 12066 gas_assert (!mips_opts.micromips);
beae10d5 12067 my_getExpression (&imm_expr, s);
252b5132 12068 check_absolute_expr (ip, &imm_expr);
beae10d5 12069 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
12070 as_warn (_("Invalid performance register (%lu)"),
12071 (unsigned long) imm_expr.X_add_number);
e407c74b
NC
12072 if (imm_expr.X_add_number != 0 && mips_opts.arch == CPU_R5900
12073 && (!strcmp(insn->name,"mfps") || !strcmp(insn->name,"mtps")))
12074 as_warn (_("Invalid performance register (%lu)"),
12075 (unsigned long) imm_expr.X_add_number);
df58fc94 12076 INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
12077 imm_expr.X_op = O_absent;
12078 s = expr_end;
12079 continue;
252b5132 12080
707bfff6 12081 case 'G': /* Coprocessor destination register. */
df58fc94
RS
12082 {
12083 unsigned long opcode = ip->insn_opcode;
12084 unsigned long mask;
12085 unsigned int types;
12086 int cop0;
12087
12088 if (mips_opts.micromips)
12089 {
12090 mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
12091 | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
12092 | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
12093 opcode &= mask;
12094 switch (opcode)
12095 {
12096 case 0x000000fc: /* mfc0 */
12097 case 0x000002fc: /* mtc0 */
12098 case 0x580000fc: /* dmfc0 */
12099 case 0x580002fc: /* dmtc0 */
12100 cop0 = 1;
12101 break;
12102 default:
12103 cop0 = 0;
12104 break;
12105 }
12106 }
12107 else
12108 {
12109 opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
12110 cop0 = opcode == OP_OP_COP0;
12111 }
12112 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
12113 ok = reg_lookup (&s, types, &regno);
12114 if (mips_opts.micromips)
12115 INSERT_OPERAND (1, RS, *ip, regno);
12116 else
12117 INSERT_OPERAND (0, RD, *ip, regno);
12118 if (ok)
12119 {
12120 lastregno = regno;
12121 continue;
12122 }
12123 }
12124 break;
707bfff6 12125
df58fc94
RS
12126 case 'y': /* ALNV.PS source register. */
12127 gas_assert (mips_opts.micromips);
12128 goto do_reg;
12129 case 'x': /* Ignore register name. */
12130 case 'U': /* Destination register (CLO/CLZ). */
12131 case 'g': /* Coprocessor destination register. */
12132 gas_assert (!mips_opts.micromips);
90ecf173
MR
12133 case 'b': /* Base register. */
12134 case 'd': /* Destination register. */
12135 case 's': /* Source register. */
12136 case 't': /* Target register. */
12137 case 'r': /* Both target and source. */
12138 case 'v': /* Both dest and source. */
12139 case 'w': /* Both dest and target. */
12140 case 'E': /* Coprocessor target register. */
12141 case 'K': /* RDHWR destination register. */
90ecf173 12142 case 'z': /* Must be zero register. */
df58fc94 12143 do_reg:
90ecf173 12144 s_reset = s;
707bfff6
TS
12145 if (*args == 'E' || *args == 'K')
12146 ok = reg_lookup (&s, RTYPE_NUM, &regno);
12147 else
12148 {
12149 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
741fe287
MR
12150 if (regno == AT && mips_opts.at)
12151 {
12152 if (mips_opts.at == ATREG)
f71d0d44 12153 as_warn (_("Used $at without \".set noat\""));
741fe287 12154 else
f71d0d44 12155 as_warn (_("Used $%u with \".set at=$%u\""),
741fe287
MR
12156 regno, mips_opts.at);
12157 }
707bfff6
TS
12158 }
12159 if (ok)
252b5132 12160 {
252b5132
RH
12161 c = *args;
12162 if (*s == ' ')
f9419b05 12163 ++s;
252b5132
RH
12164 if (args[1] != *s)
12165 {
12166 if (c == 'r' || c == 'v' || c == 'w')
12167 {
12168 regno = lastregno;
12169 s = s_reset;
f9419b05 12170 ++args;
252b5132
RH
12171 }
12172 }
12173 /* 'z' only matches $0. */
12174 if (c == 'z' && regno != 0)
12175 break;
12176
24864476 12177 if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
e7c604dd
CM
12178 {
12179 if (regno == lastregno)
90ecf173
MR
12180 {
12181 insn_error
f71d0d44 12182 = _("Source and destination must be different");
e7c604dd 12183 continue;
90ecf173 12184 }
24864476 12185 if (regno == 31 && lastregno == 0xffffffff)
90ecf173
MR
12186 {
12187 insn_error
f71d0d44 12188 = _("A destination register must be supplied");
e7c604dd 12189 continue;
90ecf173 12190 }
e7c604dd 12191 }
90ecf173
MR
12192 /* Now that we have assembled one operand, we use the args
12193 string to figure out where it goes in the instruction. */
252b5132
RH
12194 switch (c)
12195 {
12196 case 'r':
12197 case 's':
12198 case 'v':
12199 case 'b':
df58fc94 12200 INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
252b5132 12201 break;
df58fc94 12202
af7ee8bf 12203 case 'K':
df58fc94
RS
12204 if (mips_opts.micromips)
12205 INSERT_OPERAND (1, RS, *ip, regno);
12206 else
12207 INSERT_OPERAND (0, RD, *ip, regno);
12208 break;
12209
12210 case 'd':
ef2e4d86 12211 case 'g':
df58fc94 12212 INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
252b5132 12213 break;
df58fc94 12214
4372b673 12215 case 'U':
df58fc94
RS
12216 gas_assert (!mips_opts.micromips);
12217 INSERT_OPERAND (0, RD, *ip, regno);
12218 INSERT_OPERAND (0, RT, *ip, regno);
4372b673 12219 break;
df58fc94 12220
252b5132
RH
12221 case 'w':
12222 case 't':
12223 case 'E':
df58fc94
RS
12224 INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
12225 break;
12226
12227 case 'y':
12228 gas_assert (mips_opts.micromips);
12229 INSERT_OPERAND (1, RS3, *ip, regno);
252b5132 12230 break;
df58fc94 12231
252b5132
RH
12232 case 'x':
12233 /* This case exists because on the r3000 trunc
12234 expands into a macro which requires a gp
12235 register. On the r6000 or r4000 it is
12236 assembled into a single instruction which
12237 ignores the register. Thus the insn version
12238 is MIPS_ISA2 and uses 'x', and the macro
12239 version is MIPS_ISA1 and uses 't'. */
12240 break;
df58fc94 12241
252b5132
RH
12242 case 'z':
12243 /* This case is for the div instruction, which
12244 acts differently if the destination argument
12245 is $0. This only matches $0, and is checked
12246 outside the switch. */
12247 break;
252b5132
RH
12248 }
12249 lastregno = regno;
12250 continue;
12251 }
252b5132
RH
12252 switch (*args++)
12253 {
12254 case 'r':
12255 case 'v':
df58fc94 12256 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132 12257 continue;
df58fc94 12258
252b5132 12259 case 'w':
df58fc94 12260 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
252b5132
RH
12261 continue;
12262 }
12263 break;
12264
deec1734 12265 case 'O': /* MDMX alignment immediate constant. */
df58fc94 12266 gas_assert (!mips_opts.micromips);
deec1734
CD
12267 my_getExpression (&imm_expr, s);
12268 check_absolute_expr (ip, &imm_expr);
12269 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
20203fb9 12270 as_warn (_("Improper align amount (%ld), using low bits"),
bf12938e 12271 (long) imm_expr.X_add_number);
df58fc94 12272 INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
deec1734
CD
12273 imm_expr.X_op = O_absent;
12274 s = expr_end;
12275 continue;
12276
12277 case 'Q': /* MDMX vector, element sel, or const. */
12278 if (s[0] != '$')
12279 {
12280 /* MDMX Immediate. */
df58fc94 12281 gas_assert (!mips_opts.micromips);
deec1734
CD
12282 my_getExpression (&imm_expr, s);
12283 check_absolute_expr (ip, &imm_expr);
12284 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
12285 as_warn (_("Invalid MDMX Immediate (%ld)"),
12286 (long) imm_expr.X_add_number);
df58fc94 12287 INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
deec1734
CD
12288 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12289 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12290 else
12291 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
12292 imm_expr.X_op = O_absent;
12293 s = expr_end;
12294 continue;
12295 }
12296 /* Not MDMX Immediate. Fall through. */
12297 case 'X': /* MDMX destination register. */
12298 case 'Y': /* MDMX source register. */
12299 case 'Z': /* MDMX target register. */
12300 is_mdmx = 1;
df58fc94
RS
12301 case 'W':
12302 gas_assert (!mips_opts.micromips);
90ecf173
MR
12303 case 'D': /* Floating point destination register. */
12304 case 'S': /* Floating point source register. */
12305 case 'T': /* Floating point target register. */
12306 case 'R': /* Floating point source register. */
252b5132 12307 case 'V':
707bfff6
TS
12308 rtype = RTYPE_FPU;
12309 if (is_mdmx
846ef2d0 12310 || ((mips_opts.ase & ASE_MDMX)
707bfff6
TS
12311 && (ip->insn_mo->pinfo & FP_D)
12312 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12313 | INSN_COPROC_MEMORY_DELAY
12314 | INSN_LOAD_COPROC_DELAY
12315 | INSN_LOAD_MEMORY_DELAY
12316 | INSN_STORE_MEMORY))))
12317 rtype |= RTYPE_VEC;
252b5132 12318 s_reset = s;
707bfff6 12319 if (reg_lookup (&s, rtype, &regno))
252b5132 12320 {
252b5132 12321 if ((regno & 1) != 0
ca4e0257 12322 && HAVE_32BIT_FPRS
90ecf173 12323 && !mips_oddfpreg_ok (ip->insn_mo, argnum))
252b5132
RH
12324 as_warn (_("Float register should be even, was %d"),
12325 regno);
12326
12327 c = *args;
12328 if (*s == ' ')
f9419b05 12329 ++s;
252b5132
RH
12330 if (args[1] != *s)
12331 {
12332 if (c == 'V' || c == 'W')
12333 {
12334 regno = lastregno;
12335 s = s_reset;
f9419b05 12336 ++args;
252b5132
RH
12337 }
12338 }
12339 switch (c)
12340 {
12341 case 'D':
deec1734 12342 case 'X':
df58fc94 12343 INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
252b5132 12344 break;
df58fc94 12345
252b5132
RH
12346 case 'V':
12347 case 'S':
deec1734 12348 case 'Y':
df58fc94 12349 INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
252b5132 12350 break;
df58fc94 12351
deec1734
CD
12352 case 'Q':
12353 /* This is like 'Z', but also needs to fix the MDMX
12354 vector/scalar select bits. Note that the
12355 scalar immediate case is handled above. */
12356 if (*s == '[')
12357 {
12358 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12359 int max_el = (is_qh ? 3 : 7);
12360 s++;
12361 my_getExpression(&imm_expr, s);
12362 check_absolute_expr (ip, &imm_expr);
12363 s = expr_end;
12364 if (imm_expr.X_add_number > max_el)
20203fb9
NC
12365 as_bad (_("Bad element selector %ld"),
12366 (long) imm_expr.X_add_number);
deec1734
CD
12367 imm_expr.X_add_number &= max_el;
12368 ip->insn_opcode |= (imm_expr.X_add_number
12369 << (OP_SH_VSEL +
12370 (is_qh ? 2 : 1)));
01a3f561 12371 imm_expr.X_op = O_absent;
deec1734 12372 if (*s != ']')
20203fb9 12373 as_warn (_("Expecting ']' found '%s'"), s);
deec1734
CD
12374 else
12375 s++;
12376 }
12377 else
12378 {
12379 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12380 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12381 << OP_SH_VSEL);
12382 else
12383 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12384 OP_SH_VSEL);
12385 }
90ecf173 12386 /* Fall through. */
252b5132
RH
12387 case 'W':
12388 case 'T':
deec1734 12389 case 'Z':
df58fc94 12390 INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
252b5132 12391 break;
df58fc94 12392
252b5132 12393 case 'R':
df58fc94 12394 INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
252b5132
RH
12395 break;
12396 }
12397 lastregno = regno;
12398 continue;
12399 }
12400
252b5132
RH
12401 switch (*args++)
12402 {
12403 case 'V':
df58fc94 12404 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132 12405 continue;
df58fc94 12406
252b5132 12407 case 'W':
df58fc94 12408 INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
252b5132
RH
12409 continue;
12410 }
12411 break;
12412
12413 case 'I':
12414 my_getExpression (&imm_expr, s);
12415 if (imm_expr.X_op != O_big
12416 && imm_expr.X_op != O_constant)
12417 insn_error = _("absolute expression required");
9ee2a2d4
MR
12418 if (HAVE_32BIT_GPRS)
12419 normalize_constant_expr (&imm_expr);
252b5132
RH
12420 s = expr_end;
12421 continue;
12422
12423 case 'A':
12424 my_getExpression (&offset_expr, s);
2051e8c4 12425 normalize_address_expr (&offset_expr);
f6688943 12426 *imm_reloc = BFD_RELOC_32;
252b5132
RH
12427 s = expr_end;
12428 continue;
12429
12430 case 'F':
12431 case 'L':
12432 case 'f':
12433 case 'l':
12434 {
12435 int f64;
ca4e0257 12436 int using_gprs;
252b5132
RH
12437 char *save_in;
12438 char *err;
12439 unsigned char temp[8];
12440 int len;
12441 unsigned int length;
12442 segT seg;
12443 subsegT subseg;
12444 char *p;
12445
12446 /* These only appear as the last operand in an
12447 instruction, and every instruction that accepts
12448 them in any variant accepts them in all variants.
12449 This means we don't have to worry about backing out
12450 any changes if the instruction does not match.
12451
12452 The difference between them is the size of the
12453 floating point constant and where it goes. For 'F'
12454 and 'L' the constant is 64 bits; for 'f' and 'l' it
12455 is 32 bits. Where the constant is placed is based
12456 on how the MIPS assembler does things:
12457 F -- .rdata
12458 L -- .lit8
12459 f -- immediate value
12460 l -- .lit4
12461
12462 The .lit4 and .lit8 sections are only used if
12463 permitted by the -G argument.
12464
ca4e0257
RS
12465 The code below needs to know whether the target register
12466 is 32 or 64 bits wide. It relies on the fact 'f' and
12467 'F' are used with GPR-based instructions and 'l' and
12468 'L' are used with FPR-based instructions. */
252b5132
RH
12469
12470 f64 = *args == 'F' || *args == 'L';
ca4e0257 12471 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
12472
12473 save_in = input_line_pointer;
12474 input_line_pointer = s;
12475 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12476 length = len;
12477 s = input_line_pointer;
12478 input_line_pointer = save_in;
12479 if (err != NULL && *err != '\0')
12480 {
12481 as_bad (_("Bad floating point constant: %s"), err);
12482 memset (temp, '\0', sizeof temp);
12483 length = f64 ? 8 : 4;
12484 }
12485
9c2799c2 12486 gas_assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
12487
12488 if (*args == 'f'
12489 || (*args == 'l'
3e722fb5 12490 && (g_switch_value < 4
252b5132
RH
12491 || (temp[0] == 0 && temp[1] == 0)
12492 || (temp[2] == 0 && temp[3] == 0))))
12493 {
12494 imm_expr.X_op = O_constant;
90ecf173 12495 if (!target_big_endian)
252b5132
RH
12496 imm_expr.X_add_number = bfd_getl32 (temp);
12497 else
12498 imm_expr.X_add_number = bfd_getb32 (temp);
12499 }
12500 else if (length > 4
90ecf173 12501 && !mips_disable_float_construction
ca4e0257
RS
12502 /* Constants can only be constructed in GPRs and
12503 copied to FPRs if the GPRs are at least as wide
12504 as the FPRs. Force the constant into memory if
12505 we are using 64-bit FPRs but the GPRs are only
12506 32 bits wide. */
12507 && (using_gprs
90ecf173 12508 || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
12509 && ((temp[0] == 0 && temp[1] == 0)
12510 || (temp[2] == 0 && temp[3] == 0))
12511 && ((temp[4] == 0 && temp[5] == 0)
12512 || (temp[6] == 0 && temp[7] == 0)))
12513 {
ca4e0257 12514 /* The value is simple enough to load with a couple of
90ecf173
MR
12515 instructions. If using 32-bit registers, set
12516 imm_expr to the high order 32 bits and offset_expr to
12517 the low order 32 bits. Otherwise, set imm_expr to
12518 the entire 64 bit constant. */
ca4e0257 12519 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
12520 {
12521 imm_expr.X_op = O_constant;
12522 offset_expr.X_op = O_constant;
90ecf173 12523 if (!target_big_endian)
252b5132
RH
12524 {
12525 imm_expr.X_add_number = bfd_getl32 (temp + 4);
12526 offset_expr.X_add_number = bfd_getl32 (temp);
12527 }
12528 else
12529 {
12530 imm_expr.X_add_number = bfd_getb32 (temp);
12531 offset_expr.X_add_number = bfd_getb32 (temp + 4);
12532 }
12533 if (offset_expr.X_add_number == 0)
12534 offset_expr.X_op = O_absent;
12535 }
12536 else if (sizeof (imm_expr.X_add_number) > 4)
12537 {
12538 imm_expr.X_op = O_constant;
90ecf173 12539 if (!target_big_endian)
252b5132
RH
12540 imm_expr.X_add_number = bfd_getl64 (temp);
12541 else
12542 imm_expr.X_add_number = bfd_getb64 (temp);
12543 }
12544 else
12545 {
12546 imm_expr.X_op = O_big;
12547 imm_expr.X_add_number = 4;
90ecf173 12548 if (!target_big_endian)
252b5132
RH
12549 {
12550 generic_bignum[0] = bfd_getl16 (temp);
12551 generic_bignum[1] = bfd_getl16 (temp + 2);
12552 generic_bignum[2] = bfd_getl16 (temp + 4);
12553 generic_bignum[3] = bfd_getl16 (temp + 6);
12554 }
12555 else
12556 {
12557 generic_bignum[0] = bfd_getb16 (temp + 6);
12558 generic_bignum[1] = bfd_getb16 (temp + 4);
12559 generic_bignum[2] = bfd_getb16 (temp + 2);
12560 generic_bignum[3] = bfd_getb16 (temp);
12561 }
12562 }
12563 }
12564 else
12565 {
12566 const char *newname;
12567 segT new_seg;
12568
12569 /* Switch to the right section. */
12570 seg = now_seg;
12571 subseg = now_subseg;
12572 switch (*args)
12573 {
12574 default: /* unused default case avoids warnings. */
12575 case 'L':
12576 newname = RDATA_SECTION_NAME;
3e722fb5 12577 if (g_switch_value >= 8)
252b5132
RH
12578 newname = ".lit8";
12579 break;
12580 case 'F':
3e722fb5 12581 newname = RDATA_SECTION_NAME;
252b5132
RH
12582 break;
12583 case 'l':
9c2799c2 12584 gas_assert (g_switch_value >= 4);
252b5132
RH
12585 newname = ".lit4";
12586 break;
12587 }
12588 new_seg = subseg_new (newname, (subsegT) 0);
f43abd2b 12589 if (IS_ELF)
252b5132
RH
12590 bfd_set_section_flags (stdoutput, new_seg,
12591 (SEC_ALLOC
12592 | SEC_LOAD
12593 | SEC_READONLY
12594 | SEC_DATA));
12595 frag_align (*args == 'l' ? 2 : 3, 0, 0);
c41e87e3 12596 if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
12597 record_alignment (new_seg, 4);
12598 else
12599 record_alignment (new_seg, *args == 'l' ? 2 : 3);
12600 if (seg == now_seg)
12601 as_bad (_("Can't use floating point insn in this section"));
12602
df58fc94
RS
12603 /* Set the argument to the current address in the
12604 section. */
12605 offset_expr.X_op = O_symbol;
12606 offset_expr.X_add_symbol = symbol_temp_new_now ();
12607 offset_expr.X_add_number = 0;
12608
12609 /* Put the floating point number into the section. */
12610 p = frag_more ((int) length);
12611 memcpy (p, temp, length);
12612
12613 /* Switch back to the original section. */
12614 subseg_set (seg, subseg);
12615 }
12616 }
12617 continue;
12618
12619 case 'i': /* 16-bit unsigned immediate. */
12620 case 'j': /* 16-bit signed immediate. */
12621 *imm_reloc = BFD_RELOC_LO16;
12622 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12623 {
12624 int more;
12625 offsetT minval, maxval;
12626
12627 more = (insn + 1 < past
12628 && strcmp (insn->name, insn[1].name) == 0);
12629
12630 /* If the expression was written as an unsigned number,
12631 only treat it as signed if there are no more
12632 alternatives. */
12633 if (more
12634 && *args == 'j'
12635 && sizeof (imm_expr.X_add_number) <= 4
12636 && imm_expr.X_op == O_constant
12637 && imm_expr.X_add_number < 0
12638 && imm_expr.X_unsigned
12639 && HAVE_64BIT_GPRS)
12640 break;
12641
12642 /* For compatibility with older assemblers, we accept
12643 0x8000-0xffff as signed 16-bit numbers when only
12644 signed numbers are allowed. */
12645 if (*args == 'i')
12646 minval = 0, maxval = 0xffff;
12647 else if (more)
12648 minval = -0x8000, maxval = 0x7fff;
12649 else
12650 minval = -0x8000, maxval = 0xffff;
12651
12652 if (imm_expr.X_op != O_constant
12653 || imm_expr.X_add_number < minval
12654 || imm_expr.X_add_number > maxval)
12655 {
12656 if (more)
12657 break;
12658 if (imm_expr.X_op == O_constant
12659 || imm_expr.X_op == O_big)
12660 as_bad (_("Expression out of range"));
12661 }
12662 }
12663 s = expr_end;
12664 continue;
12665
12666 case 'o': /* 16-bit offset. */
12667 offset_reloc[0] = BFD_RELOC_LO16;
12668 offset_reloc[1] = BFD_RELOC_UNUSED;
12669 offset_reloc[2] = BFD_RELOC_UNUSED;
12670
12671 /* Check whether there is only a single bracketed expression
12672 left. If so, it must be the base register and the
12673 constant must be zero. */
12674 if (*s == '(' && strchr (s + 1, '(') == 0)
12675 {
12676 offset_expr.X_op = O_constant;
12677 offset_expr.X_add_number = 0;
12678 continue;
12679 }
12680
12681 /* If this value won't fit into a 16 bit offset, then go
12682 find a macro that will generate the 32 bit offset
12683 code pattern. */
12684 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
12685 && (offset_expr.X_op != O_constant
12686 || offset_expr.X_add_number >= 0x8000
12687 || offset_expr.X_add_number < -0x8000))
12688 break;
12689
12690 s = expr_end;
12691 continue;
12692
12693 case 'p': /* PC-relative offset. */
12694 *offset_reloc = BFD_RELOC_16_PCREL_S2;
12695 my_getExpression (&offset_expr, s);
12696 s = expr_end;
12697 continue;
12698
12699 case 'u': /* Upper 16 bits. */
5821951c 12700 *imm_reloc = BFD_RELOC_LO16;
df58fc94
RS
12701 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
12702 && imm_expr.X_op == O_constant
12703 && (imm_expr.X_add_number < 0
12704 || imm_expr.X_add_number >= 0x10000))
12705 as_bad (_("lui expression (%lu) not in range 0..65535"),
12706 (unsigned long) imm_expr.X_add_number);
12707 s = expr_end;
12708 continue;
12709
12710 case 'a': /* 26-bit address. */
12711 *offset_reloc = BFD_RELOC_MIPS_JMP;
12712 my_getExpression (&offset_expr, s);
12713 s = expr_end;
12714 continue;
12715
12716 case 'N': /* 3-bit branch condition code. */
12717 case 'M': /* 3-bit compare condition code. */
12718 rtype = RTYPE_CCC;
12719 if (ip->insn_mo->pinfo & (FP_D | FP_S))
12720 rtype |= RTYPE_FCC;
12721 if (!reg_lookup (&s, rtype, &regno))
12722 break;
12723 if ((strcmp (str + strlen (str) - 3, ".ps") == 0
12724 || strcmp (str + strlen (str) - 5, "any2f") == 0
12725 || strcmp (str + strlen (str) - 5, "any2t") == 0)
12726 && (regno & 1) != 0)
12727 as_warn (_("Condition code register should be even for %s, "
12728 "was %d"),
12729 str, regno);
12730 if ((strcmp (str + strlen (str) - 5, "any4f") == 0
12731 || strcmp (str + strlen (str) - 5, "any4t") == 0)
12732 && (regno & 3) != 0)
12733 as_warn (_("Condition code register should be 0 or 4 for %s, "
12734 "was %d"),
12735 str, regno);
12736 if (*args == 'N')
12737 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
12738 else
12739 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
12740 continue;
12741
12742 case 'H':
12743 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
12744 s += 2;
12745 if (ISDIGIT (*s))
12746 {
12747 c = 0;
12748 do
12749 {
12750 c *= 10;
12751 c += *s - '0';
12752 ++s;
12753 }
12754 while (ISDIGIT (*s));
12755 }
12756 else
12757 c = 8; /* Invalid sel value. */
12758
12759 if (c > 7)
12760 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
12761 INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
12762 continue;
12763
12764 case 'e':
12765 gas_assert (!mips_opts.micromips);
12766 /* Must be at least one digit. */
12767 my_getExpression (&imm_expr, s);
12768 check_absolute_expr (ip, &imm_expr);
12769
12770 if ((unsigned long) imm_expr.X_add_number
12771 > (unsigned long) OP_MASK_VECBYTE)
12772 {
12773 as_bad (_("bad byte vector index (%ld)"),
12774 (long) imm_expr.X_add_number);
12775 imm_expr.X_add_number = 0;
12776 }
12777
12778 INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
12779 imm_expr.X_op = O_absent;
12780 s = expr_end;
12781 continue;
12782
12783 case '%':
12784 gas_assert (!mips_opts.micromips);
12785 my_getExpression (&imm_expr, s);
12786 check_absolute_expr (ip, &imm_expr);
12787
12788 if ((unsigned long) imm_expr.X_add_number
12789 > (unsigned long) OP_MASK_VECALIGN)
12790 {
12791 as_bad (_("bad byte vector index (%ld)"),
12792 (long) imm_expr.X_add_number);
12793 imm_expr.X_add_number = 0;
12794 }
12795
12796 INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
12797 imm_expr.X_op = O_absent;
12798 s = expr_end;
12799 continue;
12800
12801 case 'm': /* Opcode extension character. */
12802 gas_assert (mips_opts.micromips);
12803 c = *++args;
12804 switch (c)
12805 {
12806 case 'r':
12807 if (strncmp (s, "$pc", 3) == 0)
12808 {
12809 s += 3;
12810 continue;
12811 }
12812 break;
12813
12814 case 'a':
12815 case 'b':
12816 case 'c':
12817 case 'd':
12818 case 'e':
12819 case 'f':
12820 case 'g':
12821 case 'h':
12822 case 'i':
12823 case 'j':
12824 case 'l':
12825 case 'm':
12826 case 'n':
12827 case 'p':
12828 case 'q':
12829 case 's':
12830 case 't':
12831 case 'x':
12832 case 'y':
12833 case 'z':
12834 s_reset = s;
12835 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12836 if (regno == AT && mips_opts.at)
12837 {
12838 if (mips_opts.at == ATREG)
12839 as_warn (_("Used $at without \".set noat\""));
12840 else
12841 as_warn (_("Used $%u with \".set at=$%u\""),
12842 regno, mips_opts.at);
12843 }
12844 if (!ok)
12845 {
12846 if (c == 'c')
12847 {
12848 gas_assert (args[1] == ',');
12849 regno = lastregno;
12850 ++args;
12851 }
12852 else if (c == 't')
12853 {
12854 gas_assert (args[1] == ',');
12855 ++args;
12856 continue; /* Nothing to do. */
12857 }
12858 else
12859 break;
12860 }
12861
12862 if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
12863 {
12864 if (regno == lastregno)
12865 {
12866 insn_error
12867 = _("Source and destination must be different");
12868 continue;
12869 }
12870 if (regno == 31 && lastregno == 0xffffffff)
12871 {
12872 insn_error
12873 = _("A destination register must be supplied");
12874 continue;
12875 }
12876 }
12877
12878 if (*s == ' ')
12879 ++s;
12880 if (args[1] != *s)
12881 {
12882 if (c == 'e')
12883 {
12884 gas_assert (args[1] == ',');
12885 regno = lastregno;
12886 s = s_reset;
12887 ++args;
12888 }
12889 else if (c == 't')
12890 {
12891 gas_assert (args[1] == ',');
12892 s = s_reset;
12893 ++args;
12894 continue; /* Nothing to do. */
12895 }
12896 }
12897
12898 /* Make sure regno is the same as lastregno. */
12899 if (c == 't' && regno != lastregno)
12900 break;
12901
12902 /* Make sure regno is the same as destregno. */
12903 if (c == 'x' && regno != destregno)
12904 break;
12905
12906 /* We need to save regno, before regno maps to the
12907 microMIPS register encoding. */
12908 lastregno = regno;
12909
12910 if (c == 'f')
12911 destregno = regno;
12912
12913 switch (c)
12914 {
12915 case 'a':
12916 if (regno != GP)
12917 regno = ILLEGAL_REG;
12918 break;
12919
12920 case 'b':
12921 regno = mips32_to_micromips_reg_b_map[regno];
12922 break;
12923
12924 case 'c':
12925 regno = mips32_to_micromips_reg_c_map[regno];
12926 break;
12927
12928 case 'd':
12929 regno = mips32_to_micromips_reg_d_map[regno];
12930 break;
12931
12932 case 'e':
12933 regno = mips32_to_micromips_reg_e_map[regno];
12934 break;
12935
12936 case 'f':
12937 regno = mips32_to_micromips_reg_f_map[regno];
12938 break;
12939
12940 case 'g':
12941 regno = mips32_to_micromips_reg_g_map[regno];
12942 break;
12943
12944 case 'h':
12945 regno = mips32_to_micromips_reg_h_map[regno];
12946 break;
12947
12948 case 'i':
12949 switch (EXTRACT_OPERAND (1, MI, *ip))
12950 {
12951 case 4:
12952 if (regno == 21)
12953 regno = 3;
12954 else if (regno == 22)
12955 regno = 4;
12956 else if (regno == 5)
12957 regno = 5;
12958 else if (regno == 6)
12959 regno = 6;
12960 else if (regno == 7)
12961 regno = 7;
12962 else
12963 regno = ILLEGAL_REG;
12964 break;
12965
12966 case 5:
12967 if (regno == 6)
12968 regno = 0;
12969 else if (regno == 7)
12970 regno = 1;
12971 else
12972 regno = ILLEGAL_REG;
12973 break;
12974
12975 case 6:
12976 if (regno == 7)
12977 regno = 2;
12978 else
12979 regno = ILLEGAL_REG;
12980 break;
12981
12982 default:
12983 regno = ILLEGAL_REG;
12984 break;
12985 }
12986 break;
12987
12988 case 'l':
12989 regno = mips32_to_micromips_reg_l_map[regno];
12990 break;
12991
12992 case 'm':
12993 regno = mips32_to_micromips_reg_m_map[regno];
12994 break;
12995
12996 case 'n':
12997 regno = mips32_to_micromips_reg_n_map[regno];
12998 break;
12999
13000 case 'q':
13001 regno = mips32_to_micromips_reg_q_map[regno];
13002 break;
13003
13004 case 's':
13005 if (regno != SP)
13006 regno = ILLEGAL_REG;
13007 break;
13008
13009 case 'y':
13010 if (regno != 31)
13011 regno = ILLEGAL_REG;
13012 break;
13013
13014 case 'z':
13015 if (regno != ZERO)
13016 regno = ILLEGAL_REG;
13017 break;
13018
13019 case 'j': /* Do nothing. */
13020 case 'p':
13021 case 't':
13022 case 'x':
13023 break;
13024
13025 default:
b37df7c4 13026 abort ();
df58fc94
RS
13027 }
13028
13029 if (regno == ILLEGAL_REG)
13030 break;
13031
13032 switch (c)
13033 {
13034 case 'b':
13035 INSERT_OPERAND (1, MB, *ip, regno);
13036 break;
13037
13038 case 'c':
13039 INSERT_OPERAND (1, MC, *ip, regno);
13040 break;
13041
13042 case 'd':
13043 INSERT_OPERAND (1, MD, *ip, regno);
13044 break;
13045
13046 case 'e':
13047 INSERT_OPERAND (1, ME, *ip, regno);
13048 break;
13049
13050 case 'f':
13051 INSERT_OPERAND (1, MF, *ip, regno);
13052 break;
13053
13054 case 'g':
13055 INSERT_OPERAND (1, MG, *ip, regno);
13056 break;
13057
13058 case 'h':
13059 INSERT_OPERAND (1, MH, *ip, regno);
13060 break;
13061
13062 case 'i':
13063 INSERT_OPERAND (1, MI, *ip, regno);
13064 break;
13065
13066 case 'j':
13067 INSERT_OPERAND (1, MJ, *ip, regno);
13068 break;
13069
13070 case 'l':
13071 INSERT_OPERAND (1, ML, *ip, regno);
13072 break;
13073
13074 case 'm':
13075 INSERT_OPERAND (1, MM, *ip, regno);
13076 break;
13077
13078 case 'n':
13079 INSERT_OPERAND (1, MN, *ip, regno);
13080 break;
13081
13082 case 'p':
13083 INSERT_OPERAND (1, MP, *ip, regno);
13084 break;
13085
13086 case 'q':
13087 INSERT_OPERAND (1, MQ, *ip, regno);
13088 break;
13089
13090 case 'a': /* Do nothing. */
13091 case 's': /* Do nothing. */
13092 case 't': /* Do nothing. */
13093 case 'x': /* Do nothing. */
13094 case 'y': /* Do nothing. */
13095 case 'z': /* Do nothing. */
13096 break;
13097
13098 default:
b37df7c4 13099 abort ();
df58fc94
RS
13100 }
13101 continue;
13102
13103 case 'A':
13104 {
13105 bfd_reloc_code_real_type r[3];
13106 expressionS ep;
13107 int imm;
13108
13109 /* Check whether there is only a single bracketed
13110 expression left. If so, it must be the base register
13111 and the constant must be zero. */
13112 if (*s == '(' && strchr (s + 1, '(') == 0)
13113 {
13114 INSERT_OPERAND (1, IMMA, *ip, 0);
13115 continue;
13116 }
13117
13118 if (my_getSmallExpression (&ep, r, s) > 0
13119 || !expr_const_in_range (&ep, -64, 64, 2))
13120 break;
13121
13122 imm = ep.X_add_number >> 2;
13123 INSERT_OPERAND (1, IMMA, *ip, imm);
13124 }
13125 s = expr_end;
13126 continue;
13127
13128 case 'B':
13129 {
13130 bfd_reloc_code_real_type r[3];
13131 expressionS ep;
13132 int imm;
13133
13134 if (my_getSmallExpression (&ep, r, s) > 0
13135 || ep.X_op != O_constant)
13136 break;
13137
13138 for (imm = 0; imm < 8; imm++)
13139 if (micromips_imm_b_map[imm] == ep.X_add_number)
13140 break;
13141 if (imm >= 8)
13142 break;
13143
13144 INSERT_OPERAND (1, IMMB, *ip, imm);
13145 }
13146 s = expr_end;
13147 continue;
13148
13149 case 'C':
13150 {
13151 bfd_reloc_code_real_type r[3];
13152 expressionS ep;
13153 int imm;
13154
13155 if (my_getSmallExpression (&ep, r, s) > 0
13156 || ep.X_op != O_constant)
13157 break;
13158
13159 for (imm = 0; imm < 16; imm++)
13160 if (micromips_imm_c_map[imm] == ep.X_add_number)
13161 break;
13162 if (imm >= 16)
13163 break;
13164
13165 INSERT_OPERAND (1, IMMC, *ip, imm);
13166 }
13167 s = expr_end;
13168 continue;
13169
13170 case 'D': /* pc relative offset */
13171 case 'E': /* pc relative offset */
13172 my_getExpression (&offset_expr, s);
13173 if (offset_expr.X_op == O_register)
13174 break;
13175
40209cad
MR
13176 if (!forced_insn_length)
13177 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
13178 else if (c == 'D')
13179 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
13180 else
13181 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
df58fc94
RS
13182 s = expr_end;
13183 continue;
13184
13185 case 'F':
13186 {
13187 bfd_reloc_code_real_type r[3];
13188 expressionS ep;
13189 int imm;
13190
13191 if (my_getSmallExpression (&ep, r, s) > 0
13192 || !expr_const_in_range (&ep, 0, 16, 0))
13193 break;
13194
13195 imm = ep.X_add_number;
13196 INSERT_OPERAND (1, IMMF, *ip, imm);
13197 }
13198 s = expr_end;
13199 continue;
13200
13201 case 'G':
13202 {
13203 bfd_reloc_code_real_type r[3];
13204 expressionS ep;
13205 int imm;
13206
13207 /* Check whether there is only a single bracketed
13208 expression left. If so, it must be the base register
13209 and the constant must be zero. */
13210 if (*s == '(' && strchr (s + 1, '(') == 0)
13211 {
13212 INSERT_OPERAND (1, IMMG, *ip, 0);
13213 continue;
13214 }
13215
13216 if (my_getSmallExpression (&ep, r, s) > 0
13217 || !expr_const_in_range (&ep, -1, 15, 0))
13218 break;
13219
13220 imm = ep.X_add_number & 15;
13221 INSERT_OPERAND (1, IMMG, *ip, imm);
13222 }
13223 s = expr_end;
13224 continue;
13225
13226 case 'H':
13227 {
13228 bfd_reloc_code_real_type r[3];
13229 expressionS ep;
13230 int imm;
13231
13232 /* Check whether there is only a single bracketed
13233 expression left. If so, it must be the base register
13234 and the constant must be zero. */
13235 if (*s == '(' && strchr (s + 1, '(') == 0)
13236 {
13237 INSERT_OPERAND (1, IMMH, *ip, 0);
13238 continue;
13239 }
13240
13241 if (my_getSmallExpression (&ep, r, s) > 0
13242 || !expr_const_in_range (&ep, 0, 16, 1))
13243 break;
13244
13245 imm = ep.X_add_number >> 1;
13246 INSERT_OPERAND (1, IMMH, *ip, imm);
13247 }
13248 s = expr_end;
13249 continue;
13250
13251 case 'I':
13252 {
13253 bfd_reloc_code_real_type r[3];
13254 expressionS ep;
13255 int imm;
13256
13257 if (my_getSmallExpression (&ep, r, s) > 0
13258 || !expr_const_in_range (&ep, -1, 127, 0))
13259 break;
13260
13261 imm = ep.X_add_number & 127;
13262 INSERT_OPERAND (1, IMMI, *ip, imm);
13263 }
13264 s = expr_end;
13265 continue;
13266
13267 case 'J':
13268 {
13269 bfd_reloc_code_real_type r[3];
13270 expressionS ep;
13271 int imm;
13272
13273 /* Check whether there is only a single bracketed
13274 expression left. If so, it must be the base register
13275 and the constant must be zero. */
13276 if (*s == '(' && strchr (s + 1, '(') == 0)
13277 {
13278 INSERT_OPERAND (1, IMMJ, *ip, 0);
13279 continue;
13280 }
13281
13282 if (my_getSmallExpression (&ep, r, s) > 0
13283 || !expr_const_in_range (&ep, 0, 16, 2))
13284 break;
13285
13286 imm = ep.X_add_number >> 2;
13287 INSERT_OPERAND (1, IMMJ, *ip, imm);
13288 }
13289 s = expr_end;
13290 continue;
13291
13292 case 'L':
13293 {
13294 bfd_reloc_code_real_type r[3];
13295 expressionS ep;
13296 int imm;
13297
13298 /* Check whether there is only a single bracketed
13299 expression left. If so, it must be the base register
13300 and the constant must be zero. */
13301 if (*s == '(' && strchr (s + 1, '(') == 0)
13302 {
13303 INSERT_OPERAND (1, IMML, *ip, 0);
13304 continue;
13305 }
13306
13307 if (my_getSmallExpression (&ep, r, s) > 0
13308 || !expr_const_in_range (&ep, 0, 16, 0))
13309 break;
13310
13311 imm = ep.X_add_number;
13312 INSERT_OPERAND (1, IMML, *ip, imm);
13313 }
13314 s = expr_end;
13315 continue;
13316
13317 case 'M':
13318 {
13319 bfd_reloc_code_real_type r[3];
13320 expressionS ep;
13321 int imm;
13322
13323 if (my_getSmallExpression (&ep, r, s) > 0
13324 || !expr_const_in_range (&ep, 1, 9, 0))
13325 break;
13326
13327 imm = ep.X_add_number & 7;
13328 INSERT_OPERAND (1, IMMM, *ip, imm);
13329 }
13330 s = expr_end;
13331 continue;
13332
13333 case 'N': /* Register list for lwm and swm. */
13334 {
13335 /* A comma-separated list of registers and/or
13336 dash-separated contiguous ranges including
13337 both ra and a set of one or more registers
13338 starting at s0 up to s3 which have to be
13339 consecutive, e.g.:
13340
13341 s0, ra
13342 s0, s1, ra, s2, s3
13343 s0-s2, ra
13344
13345 and any permutations of these. */
13346 unsigned int reglist;
13347 int imm;
13348
13349 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13350 break;
13351
13352 if ((reglist & 0xfff1ffff) != 0x80010000)
13353 break;
13354
13355 reglist = (reglist >> 17) & 7;
13356 reglist += 1;
13357 if ((reglist & -reglist) != reglist)
13358 break;
252b5132 13359
df58fc94
RS
13360 imm = ffs (reglist) - 1;
13361 INSERT_OPERAND (1, IMMN, *ip, imm);
13362 }
13363 continue;
252b5132 13364
df58fc94
RS
13365 case 'O': /* sdbbp 4-bit code. */
13366 {
13367 bfd_reloc_code_real_type r[3];
13368 expressionS ep;
13369 int imm;
13370
13371 if (my_getSmallExpression (&ep, r, s) > 0
13372 || !expr_const_in_range (&ep, 0, 16, 0))
13373 break;
13374
13375 imm = ep.X_add_number;
13376 INSERT_OPERAND (1, IMMO, *ip, imm);
252b5132 13377 }
df58fc94
RS
13378 s = expr_end;
13379 continue;
252b5132 13380
df58fc94
RS
13381 case 'P':
13382 {
13383 bfd_reloc_code_real_type r[3];
13384 expressionS ep;
13385 int imm;
5e0116d5 13386
df58fc94
RS
13387 if (my_getSmallExpression (&ep, r, s) > 0
13388 || !expr_const_in_range (&ep, 0, 32, 2))
13389 break;
5e0116d5 13390
df58fc94
RS
13391 imm = ep.X_add_number >> 2;
13392 INSERT_OPERAND (1, IMMP, *ip, imm);
13393 }
13394 s = expr_end;
13395 continue;
5e0116d5 13396
df58fc94
RS
13397 case 'Q':
13398 {
13399 bfd_reloc_code_real_type r[3];
13400 expressionS ep;
13401 int imm;
5e0116d5 13402
df58fc94
RS
13403 if (my_getSmallExpression (&ep, r, s) > 0
13404 || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13405 break;
252b5132 13406
df58fc94
RS
13407 imm = ep.X_add_number >> 2;
13408 INSERT_OPERAND (1, IMMQ, *ip, imm);
13409 }
13410 s = expr_end;
13411 continue;
4614d845 13412
df58fc94
RS
13413 case 'U':
13414 {
13415 bfd_reloc_code_real_type r[3];
13416 expressionS ep;
13417 int imm;
13418
13419 /* Check whether there is only a single bracketed
13420 expression left. If so, it must be the base register
13421 and the constant must be zero. */
13422 if (*s == '(' && strchr (s + 1, '(') == 0)
13423 {
13424 INSERT_OPERAND (1, IMMU, *ip, 0);
13425 continue;
13426 }
13427
13428 if (my_getSmallExpression (&ep, r, s) > 0
13429 || !expr_const_in_range (&ep, 0, 32, 2))
13430 break;
13431
13432 imm = ep.X_add_number >> 2;
13433 INSERT_OPERAND (1, IMMU, *ip, imm);
13434 }
13435 s = expr_end;
5e0116d5 13436 continue;
252b5132 13437
df58fc94
RS
13438 case 'W':
13439 {
13440 bfd_reloc_code_real_type r[3];
13441 expressionS ep;
13442 int imm;
252b5132 13443
df58fc94
RS
13444 if (my_getSmallExpression (&ep, r, s) > 0
13445 || !expr_const_in_range (&ep, 0, 64, 2))
13446 break;
252b5132 13447
df58fc94
RS
13448 imm = ep.X_add_number >> 2;
13449 INSERT_OPERAND (1, IMMW, *ip, imm);
13450 }
13451 s = expr_end;
13452 continue;
252b5132 13453
df58fc94
RS
13454 case 'X':
13455 {
13456 bfd_reloc_code_real_type r[3];
13457 expressionS ep;
13458 int imm;
252b5132 13459
df58fc94
RS
13460 if (my_getSmallExpression (&ep, r, s) > 0
13461 || !expr_const_in_range (&ep, -8, 8, 0))
13462 break;
252b5132 13463
df58fc94
RS
13464 imm = ep.X_add_number;
13465 INSERT_OPERAND (1, IMMX, *ip, imm);
13466 }
13467 s = expr_end;
13468 continue;
252b5132 13469
df58fc94
RS
13470 case 'Y':
13471 {
13472 bfd_reloc_code_real_type r[3];
13473 expressionS ep;
13474 int imm;
156c2f8b 13475
df58fc94
RS
13476 if (my_getSmallExpression (&ep, r, s) > 0
13477 || expr_const_in_range (&ep, -2, 2, 2)
13478 || !expr_const_in_range (&ep, -258, 258, 2))
13479 break;
156c2f8b 13480
df58fc94
RS
13481 imm = ep.X_add_number >> 2;
13482 imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13483 INSERT_OPERAND (1, IMMY, *ip, imm);
13484 }
13485 s = expr_end;
13486 continue;
60b63b72 13487
df58fc94
RS
13488 case 'Z':
13489 {
13490 bfd_reloc_code_real_type r[3];
13491 expressionS ep;
13492
13493 if (my_getSmallExpression (&ep, r, s) > 0
13494 || !expr_const_in_range (&ep, 0, 1, 0))
13495 break;
13496 }
13497 s = expr_end;
13498 continue;
13499
13500 default:
13501 as_bad (_("Internal error: bad microMIPS opcode "
13502 "(unknown extension operand type `m%c'): %s %s"),
13503 *args, insn->name, insn->args);
13504 /* Further processing is fruitless. */
13505 return;
60b63b72 13506 }
df58fc94 13507 break;
60b63b72 13508
df58fc94
RS
13509 case 'n': /* Register list for 32-bit lwm and swm. */
13510 gas_assert (mips_opts.micromips);
13511 {
13512 /* A comma-separated list of registers and/or
13513 dash-separated contiguous ranges including
13514 at least one of ra and a set of one or more
13515 registers starting at s0 up to s7 and then
13516 s8 which have to be consecutive, e.g.:
13517
13518 ra
13519 s0
13520 ra, s0, s1, s2
13521 s0-s8
13522 s0-s5, ra
13523
13524 and any permutations of these. */
13525 unsigned int reglist;
13526 int imm;
13527 int ra;
13528
13529 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13530 break;
13531
13532 if ((reglist & 0x3f00ffff) != 0)
13533 break;
13534
13535 ra = (reglist >> 27) & 0x10;
13536 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13537 reglist += 1;
13538 if ((reglist & -reglist) != reglist)
13539 break;
13540
13541 imm = (ffs (reglist) - 1) | ra;
13542 INSERT_OPERAND (1, RT, *ip, imm);
13543 imm_expr.X_op = O_absent;
13544 }
60b63b72
RS
13545 continue;
13546
df58fc94
RS
13547 case '|': /* 4-bit trap code. */
13548 gas_assert (mips_opts.micromips);
60b63b72
RS
13549 my_getExpression (&imm_expr, s);
13550 check_absolute_expr (ip, &imm_expr);
60b63b72 13551 if ((unsigned long) imm_expr.X_add_number
df58fc94
RS
13552 > MICROMIPSOP_MASK_TRAP)
13553 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13554 (unsigned long) imm_expr.X_add_number,
13555 ip->insn_mo->name);
13556 INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
60b63b72
RS
13557 imm_expr.X_op = O_absent;
13558 s = expr_end;
13559 continue;
13560
252b5132 13561 default:
f71d0d44 13562 as_bad (_("Bad char = '%c'\n"), *args);
b37df7c4 13563 abort ();
252b5132
RH
13564 }
13565 break;
13566 }
13567 /* Args don't match. */
df58fc94
RS
13568 s = argsStart;
13569 insn_error = _("Illegal operands");
13570 if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
252b5132
RH
13571 {
13572 ++insn;
252b5132
RH
13573 continue;
13574 }
df58fc94
RS
13575 else if (wrong_delay_slot_insns && need_delay_slot_ok)
13576 {
13577 gas_assert (firstinsn);
13578 need_delay_slot_ok = FALSE;
13579 past = insn + 1;
13580 insn = firstinsn;
13581 continue;
13582 }
252b5132
RH
13583 return;
13584 }
13585}
13586
0499d65b
TS
13587#define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13588
252b5132
RH
13589/* This routine assembles an instruction into its binary format when
13590 assembling for the mips16. As a side effect, it sets one of the
df58fc94
RS
13591 global variables imm_reloc or offset_reloc to the type of relocation
13592 to do if one of the operands is an address expression. It also sets
13593 forced_insn_length to the resulting instruction size in bytes if the
13594 user explicitly requested a small or extended instruction. */
252b5132
RH
13595
13596static void
17a2f251 13597mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
13598{
13599 char *s;
13600 const char *args;
13601 struct mips_opcode *insn;
13602 char *argsstart;
13603 unsigned int regno;
13604 unsigned int lastregno = 0;
13605 char *s_reset;
d6f16593 13606 size_t i;
252b5132
RH
13607
13608 insn_error = NULL;
13609
df58fc94 13610 forced_insn_length = 0;
252b5132 13611
3882b010 13612 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
13613 ;
13614 switch (*s)
13615 {
13616 case '\0':
13617 break;
13618
13619 case ' ':
13620 *s++ = '\0';
13621 break;
13622
13623 case '.':
13624 if (s[1] == 't' && s[2] == ' ')
13625 {
13626 *s = '\0';
df58fc94 13627 forced_insn_length = 2;
252b5132
RH
13628 s += 3;
13629 break;
13630 }
13631 else if (s[1] == 'e' && s[2] == ' ')
13632 {
13633 *s = '\0';
df58fc94 13634 forced_insn_length = 4;
252b5132
RH
13635 s += 3;
13636 break;
13637 }
13638 /* Fall through. */
13639 default:
13640 insn_error = _("unknown opcode");
13641 return;
13642 }
13643
df58fc94
RS
13644 if (mips_opts.noautoextend && !forced_insn_length)
13645 forced_insn_length = 2;
252b5132
RH
13646
13647 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13648 {
13649 insn_error = _("unrecognized opcode");
13650 return;
13651 }
13652
13653 argsstart = s;
13654 for (;;)
13655 {
9b3f89ee
TS
13656 bfd_boolean ok;
13657
9c2799c2 13658 gas_assert (strcmp (insn->name, str) == 0);
252b5132 13659
037b32b9 13660 ok = is_opcode_valid_16 (insn);
9b3f89ee
TS
13661 if (! ok)
13662 {
13663 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13664 && strcmp (insn->name, insn[1].name) == 0)
13665 {
13666 ++insn;
13667 continue;
13668 }
13669 else
13670 {
13671 if (!insn_error)
13672 {
13673 static char buf[100];
13674 sprintf (buf,
7bd942df 13675 _("Opcode not supported on this processor: %s (%s)"),
9b3f89ee
TS
13676 mips_cpu_info_from_arch (mips_opts.arch)->name,
13677 mips_cpu_info_from_isa (mips_opts.isa)->name);
13678 insn_error = buf;
13679 }
13680 return;
13681 }
13682 }
13683
1e915849 13684 create_insn (ip, insn);
252b5132 13685 imm_expr.X_op = O_absent;
f6688943
TS
13686 imm_reloc[0] = BFD_RELOC_UNUSED;
13687 imm_reloc[1] = BFD_RELOC_UNUSED;
13688 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 13689 imm2_expr.X_op = O_absent;
252b5132 13690 offset_expr.X_op = O_absent;
f6688943
TS
13691 offset_reloc[0] = BFD_RELOC_UNUSED;
13692 offset_reloc[1] = BFD_RELOC_UNUSED;
13693 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
13694 for (args = insn->args; 1; ++args)
13695 {
13696 int c;
13697
13698 if (*s == ' ')
13699 ++s;
13700
13701 /* In this switch statement we call break if we did not find
13702 a match, continue if we did find a match, or return if we
13703 are done. */
13704
13705 c = *args;
13706 switch (c)
13707 {
13708 case '\0':
13709 if (*s == '\0')
13710 {
b886a2ab
RS
13711 offsetT value;
13712
252b5132
RH
13713 /* Stuff the immediate value in now, if we can. */
13714 if (imm_expr.X_op == O_constant
f6688943 13715 && *imm_reloc > BFD_RELOC_UNUSED
b886a2ab
RS
13716 && insn->pinfo != INSN_MACRO
13717 && calculate_reloc (*offset_reloc,
13718 imm_expr.X_add_number, &value))
252b5132 13719 {
c4e7957c 13720 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b886a2ab 13721 *offset_reloc, value, forced_insn_length,
43c0598f 13722 &ip->insn_opcode);
252b5132 13723 imm_expr.X_op = O_absent;
f6688943 13724 *imm_reloc = BFD_RELOC_UNUSED;
43c0598f 13725 *offset_reloc = BFD_RELOC_UNUSED;
252b5132
RH
13726 }
13727
13728 return;
13729 }
13730 break;
13731
13732 case ',':
13733 if (*s++ == c)
13734 continue;
13735 s--;
13736 switch (*++args)
13737 {
13738 case 'v':
bf12938e 13739 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
13740 continue;
13741 case 'w':
bf12938e 13742 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
13743 continue;
13744 }
13745 break;
13746
13747 case '(':
13748 case ')':
13749 if (*s++ == c)
13750 continue;
13751 break;
13752
13753 case 'v':
13754 case 'w':
13755 if (s[0] != '$')
13756 {
13757 if (c == 'v')
bf12938e 13758 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 13759 else
bf12938e 13760 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
13761 ++args;
13762 continue;
13763 }
13764 /* Fall through. */
13765 case 'x':
13766 case 'y':
13767 case 'z':
13768 case 'Z':
13769 case '0':
13770 case 'S':
13771 case 'R':
13772 case 'X':
13773 case 'Y':
707bfff6
TS
13774 s_reset = s;
13775 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
252b5132 13776 {
707bfff6 13777 if (c == 'v' || c == 'w')
85b51719 13778 {
707bfff6 13779 if (c == 'v')
a9e24354 13780 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
707bfff6 13781 else
a9e24354 13782 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
707bfff6
TS
13783 ++args;
13784 continue;
85b51719 13785 }
707bfff6 13786 break;
252b5132
RH
13787 }
13788
13789 if (*s == ' ')
13790 ++s;
13791 if (args[1] != *s)
13792 {
13793 if (c == 'v' || c == 'w')
13794 {
13795 regno = mips16_to_32_reg_map[lastregno];
13796 s = s_reset;
f9419b05 13797 ++args;
252b5132
RH
13798 }
13799 }
13800
13801 switch (c)
13802 {
13803 case 'x':
13804 case 'y':
13805 case 'z':
13806 case 'v':
13807 case 'w':
13808 case 'Z':
13809 regno = mips32_to_16_reg_map[regno];
13810 break;
13811
13812 case '0':
13813 if (regno != 0)
13814 regno = ILLEGAL_REG;
13815 break;
13816
13817 case 'S':
13818 if (regno != SP)
13819 regno = ILLEGAL_REG;
13820 break;
13821
13822 case 'R':
13823 if (regno != RA)
13824 regno = ILLEGAL_REG;
13825 break;
13826
13827 case 'X':
13828 case 'Y':
741fe287
MR
13829 if (regno == AT && mips_opts.at)
13830 {
13831 if (mips_opts.at == ATREG)
13832 as_warn (_("used $at without \".set noat\""));
13833 else
13834 as_warn (_("used $%u with \".set at=$%u\""),
13835 regno, mips_opts.at);
13836 }
252b5132
RH
13837 break;
13838
13839 default:
b37df7c4 13840 abort ();
252b5132
RH
13841 }
13842
13843 if (regno == ILLEGAL_REG)
13844 break;
13845
13846 switch (c)
13847 {
13848 case 'x':
13849 case 'v':
bf12938e 13850 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
13851 break;
13852 case 'y':
13853 case 'w':
bf12938e 13854 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
13855 break;
13856 case 'z':
bf12938e 13857 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
13858 break;
13859 case 'Z':
bf12938e 13860 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
13861 case '0':
13862 case 'S':
13863 case 'R':
13864 break;
13865 case 'X':
bf12938e 13866 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
13867 break;
13868 case 'Y':
13869 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 13870 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
13871 break;
13872 default:
b37df7c4 13873 abort ();
252b5132
RH
13874 }
13875
13876 lastregno = regno;
13877 continue;
13878
13879 case 'P':
13880 if (strncmp (s, "$pc", 3) == 0)
13881 {
13882 s += 3;
13883 continue;
13884 }
13885 break;
13886
252b5132
RH
13887 case '5':
13888 case 'H':
13889 case 'W':
13890 case 'D':
13891 case 'j':
252b5132
RH
13892 case 'V':
13893 case 'C':
13894 case 'U':
13895 case 'k':
13896 case 'K':
d6f16593
MR
13897 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
13898 if (i > 0)
252b5132 13899 {
d6f16593 13900 if (imm_expr.X_op != O_constant)
252b5132 13901 {
df58fc94 13902 forced_insn_length = 4;
5c04167a 13903 ip->insn_opcode |= MIPS16_EXTEND;
252b5132 13904 }
d6f16593
MR
13905 else
13906 {
13907 /* We need to relax this instruction. */
13908 *offset_reloc = *imm_reloc;
13909 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13910 }
13911 s = expr_end;
13912 continue;
252b5132 13913 }
d6f16593
MR
13914 *imm_reloc = BFD_RELOC_UNUSED;
13915 /* Fall through. */
13916 case '<':
13917 case '>':
13918 case '[':
13919 case ']':
13920 case '4':
13921 case '8':
13922 my_getExpression (&imm_expr, s);
252b5132
RH
13923 if (imm_expr.X_op == O_register)
13924 {
13925 /* What we thought was an expression turned out to
13926 be a register. */
13927
13928 if (s[0] == '(' && args[1] == '(')
13929 {
13930 /* It looks like the expression was omitted
13931 before a register indirection, which means
13932 that the expression is implicitly zero. We
13933 still set up imm_expr, so that we handle
13934 explicit extensions correctly. */
13935 imm_expr.X_op = O_constant;
13936 imm_expr.X_add_number = 0;
f6688943 13937 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13938 continue;
13939 }
13940
13941 break;
13942 }
13943
13944 /* We need to relax this instruction. */
f6688943 13945 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13946 s = expr_end;
13947 continue;
13948
13949 case 'p':
13950 case 'q':
13951 case 'A':
13952 case 'B':
13953 case 'E':
13954 /* We use offset_reloc rather than imm_reloc for the PC
13955 relative operands. This lets macros with both
13956 immediate and address operands work correctly. */
13957 my_getExpression (&offset_expr, s);
13958
13959 if (offset_expr.X_op == O_register)
13960 break;
13961
13962 /* We need to relax this instruction. */
f6688943 13963 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
13964 s = expr_end;
13965 continue;
13966
13967 case '6': /* break code */
13968 my_getExpression (&imm_expr, s);
13969 check_absolute_expr (ip, &imm_expr);
13970 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
13971 as_warn (_("Invalid value for `%s' (%lu)"),
13972 ip->insn_mo->name,
13973 (unsigned long) imm_expr.X_add_number);
13974 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
13975 imm_expr.X_op = O_absent;
13976 s = expr_end;
13977 continue;
13978
13979 case 'a': /* 26 bit address */
13980 my_getExpression (&offset_expr, s);
13981 s = expr_end;
f6688943 13982 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
13983 ip->insn_opcode <<= 16;
13984 continue;
13985
13986 case 'l': /* register list for entry macro */
13987 case 'L': /* register list for exit macro */
13988 {
13989 int mask;
13990
13991 if (c == 'l')
13992 mask = 0;
13993 else
13994 mask = 7 << 3;
13995 while (*s != '\0')
13996 {
707bfff6 13997 unsigned int freg, reg1, reg2;
252b5132
RH
13998
13999 while (*s == ' ' || *s == ',')
14000 ++s;
707bfff6 14001 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
252b5132 14002 freg = 0;
707bfff6
TS
14003 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
14004 freg = 1;
252b5132
RH
14005 else
14006 {
707bfff6
TS
14007 as_bad (_("can't parse register list"));
14008 break;
252b5132
RH
14009 }
14010 if (*s == ' ')
14011 ++s;
14012 if (*s != '-')
14013 reg2 = reg1;
14014 else
14015 {
14016 ++s;
707bfff6
TS
14017 if (!reg_lookup (&s, freg ? RTYPE_FPU
14018 : (RTYPE_GP | RTYPE_NUM), &reg2))
252b5132 14019 {
707bfff6
TS
14020 as_bad (_("invalid register list"));
14021 break;
252b5132
RH
14022 }
14023 }
14024 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
14025 {
14026 mask &= ~ (7 << 3);
14027 mask |= 5 << 3;
14028 }
14029 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
14030 {
14031 mask &= ~ (7 << 3);
14032 mask |= 6 << 3;
14033 }
14034 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
14035 mask |= (reg2 - 3) << 3;
14036 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
14037 mask |= (reg2 - 15) << 1;
f9419b05 14038 else if (reg1 == RA && reg2 == RA)
252b5132
RH
14039 mask |= 1;
14040 else
14041 {
14042 as_bad (_("invalid register list"));
14043 break;
14044 }
14045 }
14046 /* The mask is filled in in the opcode table for the
14047 benefit of the disassembler. We remove it before
14048 applying the actual mask. */
14049 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
14050 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
14051 }
14052 continue;
14053
0499d65b
TS
14054 case 'm': /* Register list for save insn. */
14055 case 'M': /* Register list for restore insn. */
14056 {
5c04167a 14057 int opcode = ip->insn_opcode;
0499d65b 14058 int framesz = 0, seen_framesz = 0;
91d6fa6a 14059 int nargs = 0, statics = 0, sregs = 0;
0499d65b
TS
14060
14061 while (*s != '\0')
14062 {
14063 unsigned int reg1, reg2;
14064
14065 SKIP_SPACE_TABS (s);
14066 while (*s == ',')
14067 ++s;
14068 SKIP_SPACE_TABS (s);
14069
14070 my_getExpression (&imm_expr, s);
14071 if (imm_expr.X_op == O_constant)
14072 {
14073 /* Handle the frame size. */
14074 if (seen_framesz)
14075 {
14076 as_bad (_("more than one frame size in list"));
14077 break;
14078 }
14079 seen_framesz = 1;
14080 framesz = imm_expr.X_add_number;
14081 imm_expr.X_op = O_absent;
14082 s = expr_end;
14083 continue;
14084 }
14085
707bfff6 14086 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
0499d65b
TS
14087 {
14088 as_bad (_("can't parse register list"));
14089 break;
14090 }
0499d65b 14091
707bfff6
TS
14092 while (*s == ' ')
14093 ++s;
14094
0499d65b
TS
14095 if (*s != '-')
14096 reg2 = reg1;
14097 else
14098 {
14099 ++s;
707bfff6
TS
14100 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
14101 || reg2 < reg1)
0499d65b
TS
14102 {
14103 as_bad (_("can't parse register list"));
14104 break;
14105 }
0499d65b
TS
14106 }
14107
14108 while (reg1 <= reg2)
14109 {
14110 if (reg1 >= 4 && reg1 <= 7)
14111 {
3a93f742 14112 if (!seen_framesz)
0499d65b 14113 /* args $a0-$a3 */
91d6fa6a 14114 nargs |= 1 << (reg1 - 4);
0499d65b
TS
14115 else
14116 /* statics $a0-$a3 */
14117 statics |= 1 << (reg1 - 4);
14118 }
14119 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
14120 {
14121 /* $s0-$s8 */
14122 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
14123 }
14124 else if (reg1 == 31)
14125 {
14126 /* Add $ra to insn. */
14127 opcode |= 0x40;
14128 }
14129 else
14130 {
14131 as_bad (_("unexpected register in list"));
14132 break;
14133 }
14134 if (++reg1 == 24)
14135 reg1 = 30;
14136 }
14137 }
14138
14139 /* Encode args/statics combination. */
91d6fa6a 14140 if (nargs & statics)
0499d65b 14141 as_bad (_("arg/static registers overlap"));
91d6fa6a 14142 else if (nargs == 0xf)
0499d65b
TS
14143 /* All $a0-$a3 are args. */
14144 opcode |= MIPS16_ALL_ARGS << 16;
14145 else if (statics == 0xf)
14146 /* All $a0-$a3 are statics. */
14147 opcode |= MIPS16_ALL_STATICS << 16;
14148 else
14149 {
14150 int narg = 0, nstat = 0;
14151
14152 /* Count arg registers. */
91d6fa6a 14153 while (nargs & 0x1)
0499d65b 14154 {
91d6fa6a 14155 nargs >>= 1;
0499d65b
TS
14156 narg++;
14157 }
91d6fa6a 14158 if (nargs != 0)
0499d65b
TS
14159 as_bad (_("invalid arg register list"));
14160
14161 /* Count static registers. */
14162 while (statics & 0x8)
14163 {
14164 statics = (statics << 1) & 0xf;
14165 nstat++;
14166 }
14167 if (statics != 0)
14168 as_bad (_("invalid static register list"));
14169
14170 /* Encode args/statics. */
14171 opcode |= ((narg << 2) | nstat) << 16;
14172 }
14173
14174 /* Encode $s0/$s1. */
14175 if (sregs & (1 << 0)) /* $s0 */
14176 opcode |= 0x20;
14177 if (sregs & (1 << 1)) /* $s1 */
14178 opcode |= 0x10;
14179 sregs >>= 2;
14180
14181 if (sregs != 0)
14182 {
14183 /* Count regs $s2-$s8. */
14184 int nsreg = 0;
14185 while (sregs & 1)
14186 {
14187 sregs >>= 1;
14188 nsreg++;
14189 }
14190 if (sregs != 0)
14191 as_bad (_("invalid static register list"));
14192 /* Encode $s2-$s8. */
14193 opcode |= nsreg << 24;
14194 }
14195
14196 /* Encode frame size. */
14197 if (!seen_framesz)
14198 as_bad (_("missing frame size"));
14199 else if ((framesz & 7) != 0 || framesz < 0
14200 || framesz > 0xff * 8)
14201 as_bad (_("invalid frame size"));
14202 else if (framesz != 128 || (opcode >> 16) != 0)
14203 {
14204 framesz /= 8;
14205 opcode |= (((framesz & 0xf0) << 16)
14206 | (framesz & 0x0f));
14207 }
14208
14209 /* Finally build the instruction. */
14210 if ((opcode >> 16) != 0 || framesz == 0)
5c04167a
RS
14211 opcode |= MIPS16_EXTEND;
14212 ip->insn_opcode = opcode;
0499d65b
TS
14213 }
14214 continue;
14215
252b5132
RH
14216 case 'e': /* extend code */
14217 my_getExpression (&imm_expr, s);
14218 check_absolute_expr (ip, &imm_expr);
14219 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
14220 {
14221 as_warn (_("Invalid value for `%s' (%lu)"),
14222 ip->insn_mo->name,
14223 (unsigned long) imm_expr.X_add_number);
14224 imm_expr.X_add_number &= 0x7ff;
14225 }
14226 ip->insn_opcode |= imm_expr.X_add_number;
14227 imm_expr.X_op = O_absent;
14228 s = expr_end;
14229 continue;
14230
14231 default:
b37df7c4 14232 abort ();
252b5132
RH
14233 }
14234 break;
14235 }
14236
14237 /* Args don't match. */
14238 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
14239 strcmp (insn->name, insn[1].name) == 0)
14240 {
14241 ++insn;
14242 s = argsstart;
14243 continue;
14244 }
14245
14246 insn_error = _("illegal operands");
14247
14248 return;
14249 }
14250}
14251
14252/* This structure holds information we know about a mips16 immediate
14253 argument type. */
14254
e972090a
NC
14255struct mips16_immed_operand
14256{
252b5132
RH
14257 /* The type code used in the argument string in the opcode table. */
14258 int type;
14259 /* The number of bits in the short form of the opcode. */
14260 int nbits;
14261 /* The number of bits in the extended form of the opcode. */
14262 int extbits;
14263 /* The amount by which the short form is shifted when it is used;
14264 for example, the sw instruction has a shift count of 2. */
14265 int shift;
14266 /* The amount by which the short form is shifted when it is stored
14267 into the instruction code. */
14268 int op_shift;
14269 /* Non-zero if the short form is unsigned. */
14270 int unsp;
14271 /* Non-zero if the extended form is unsigned. */
14272 int extu;
14273 /* Non-zero if the value is PC relative. */
14274 int pcrel;
14275};
14276
14277/* The mips16 immediate operand types. */
14278
14279static const struct mips16_immed_operand mips16_immed_operands[] =
14280{
14281 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14282 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14283 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14284 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14285 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14286 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14287 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14288 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14289 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14290 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14291 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14292 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14293 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14294 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14295 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14296 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14297 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14298 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14299 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14300 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14301 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14302};
14303
14304#define MIPS16_NUM_IMMED \
14305 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14306
b886a2ab
RS
14307/* Marshal immediate value VAL for an extended MIPS16 instruction.
14308 NBITS is the number of significant bits in VAL. */
14309
14310static unsigned long
14311mips16_immed_extend (offsetT val, unsigned int nbits)
14312{
14313 int extval;
14314 if (nbits == 16)
14315 {
14316 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14317 val &= 0x1f;
14318 }
14319 else if (nbits == 15)
14320 {
14321 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14322 val &= 0xf;
14323 }
14324 else
14325 {
14326 extval = ((val & 0x1f) << 6) | (val & 0x20);
14327 val = 0;
14328 }
14329 return (extval << 16) | val;
14330}
14331
5c04167a
RS
14332/* Install immediate value VAL into MIPS16 instruction *INSN,
14333 extending it if necessary. The instruction in *INSN may
14334 already be extended.
14335
43c0598f
RS
14336 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14337 if none. In the former case, VAL is a 16-bit number with no
14338 defined signedness.
14339
14340 TYPE is the type of the immediate field. USER_INSN_LENGTH
14341 is the length that the user requested, or 0 if none. */
252b5132
RH
14342
14343static void
43c0598f
RS
14344mips16_immed (char *file, unsigned int line, int type,
14345 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14346 unsigned int user_insn_length, unsigned long *insn)
252b5132 14347{
3994f87e 14348 const struct mips16_immed_operand *op;
252b5132 14349 int mintiny, maxtiny;
252b5132
RH
14350
14351 op = mips16_immed_operands;
14352 while (op->type != type)
14353 {
14354 ++op;
9c2799c2 14355 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
14356 }
14357
14358 if (op->unsp)
14359 {
14360 if (type == '<' || type == '>' || type == '[' || type == ']')
14361 {
14362 mintiny = 1;
14363 maxtiny = 1 << op->nbits;
14364 }
14365 else
14366 {
14367 mintiny = 0;
14368 maxtiny = (1 << op->nbits) - 1;
14369 }
43c0598f
RS
14370 if (reloc != BFD_RELOC_UNUSED)
14371 val &= 0xffff;
252b5132
RH
14372 }
14373 else
14374 {
14375 mintiny = - (1 << (op->nbits - 1));
14376 maxtiny = (1 << (op->nbits - 1)) - 1;
43c0598f
RS
14377 if (reloc != BFD_RELOC_UNUSED)
14378 val = SEXT_16BIT (val);
252b5132
RH
14379 }
14380
14381 /* Branch offsets have an implicit 0 in the lowest bit. */
14382 if (type == 'p' || type == 'q')
14383 val /= 2;
14384
14385 if ((val & ((1 << op->shift) - 1)) != 0
14386 || val < (mintiny << op->shift)
14387 || val > (maxtiny << op->shift))
5c04167a
RS
14388 {
14389 /* We need an extended instruction. */
14390 if (user_insn_length == 2)
14391 as_bad_where (file, line, _("invalid unextended operand value"));
14392 else
14393 *insn |= MIPS16_EXTEND;
14394 }
14395 else if (user_insn_length == 4)
14396 {
14397 /* The operand doesn't force an unextended instruction to be extended.
14398 Warn if the user wanted an extended instruction anyway. */
14399 *insn |= MIPS16_EXTEND;
14400 as_warn_where (file, line,
14401 _("extended operand requested but not required"));
14402 }
252b5132 14403
5c04167a 14404 if (mips16_opcode_length (*insn) == 2)
252b5132
RH
14405 {
14406 int insnval;
14407
252b5132
RH
14408 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14409 insnval <<= op->op_shift;
14410 *insn |= insnval;
14411 }
14412 else
14413 {
14414 long minext, maxext;
252b5132 14415
43c0598f 14416 if (reloc == BFD_RELOC_UNUSED)
252b5132 14417 {
43c0598f
RS
14418 if (op->extu)
14419 {
14420 minext = 0;
14421 maxext = (1 << op->extbits) - 1;
14422 }
14423 else
14424 {
14425 minext = - (1 << (op->extbits - 1));
14426 maxext = (1 << (op->extbits - 1)) - 1;
14427 }
14428 if (val < minext || val > maxext)
14429 as_bad_where (file, line,
14430 _("operand value out of range for instruction"));
252b5132 14431 }
252b5132 14432
b886a2ab 14433 *insn |= mips16_immed_extend (val, op->extbits);
252b5132
RH
14434 }
14435}
14436\f
d6f16593 14437struct percent_op_match
ad8d3bb3 14438{
5e0116d5
RS
14439 const char *str;
14440 bfd_reloc_code_real_type reloc;
d6f16593
MR
14441};
14442
14443static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14444{
5e0116d5 14445 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 14446#ifdef OBJ_ELF
5e0116d5
RS
14447 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14448 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14449 {"%call16", BFD_RELOC_MIPS_CALL16},
14450 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14451 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14452 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14453 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14454 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14455 {"%got", BFD_RELOC_MIPS_GOT16},
14456 {"%gp_rel", BFD_RELOC_GPREL16},
14457 {"%half", BFD_RELOC_16},
14458 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14459 {"%higher", BFD_RELOC_MIPS_HIGHER},
14460 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14461 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14462 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14463 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14464 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14465 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14466 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14467 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
ad8d3bb3 14468#endif
5e0116d5 14469 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
14470};
14471
d6f16593
MR
14472static const struct percent_op_match mips16_percent_op[] =
14473{
14474 {"%lo", BFD_RELOC_MIPS16_LO16},
14475 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14476 {"%got", BFD_RELOC_MIPS16_GOT16},
14477 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14478 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14479 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14480 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14481 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14482 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14483 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14484 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14485 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14486};
14487
252b5132 14488
5e0116d5
RS
14489/* Return true if *STR points to a relocation operator. When returning true,
14490 move *STR over the operator and store its relocation code in *RELOC.
14491 Leave both *STR and *RELOC alone when returning false. */
14492
14493static bfd_boolean
17a2f251 14494parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14495{
d6f16593
MR
14496 const struct percent_op_match *percent_op;
14497 size_t limit, i;
14498
14499 if (mips_opts.mips16)
14500 {
14501 percent_op = mips16_percent_op;
14502 limit = ARRAY_SIZE (mips16_percent_op);
14503 }
14504 else
14505 {
14506 percent_op = mips_percent_op;
14507 limit = ARRAY_SIZE (mips_percent_op);
14508 }
76b3015f 14509
d6f16593 14510 for (i = 0; i < limit; i++)
5e0116d5 14511 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14512 {
3f98094e
DJ
14513 int len = strlen (percent_op[i].str);
14514
14515 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14516 continue;
14517
5e0116d5
RS
14518 *str += strlen (percent_op[i].str);
14519 *reloc = percent_op[i].reloc;
394f9b3a 14520
5e0116d5
RS
14521 /* Check whether the output BFD supports this relocation.
14522 If not, issue an error and fall back on something safe. */
14523 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14524 {
20203fb9 14525 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14526 percent_op[i].str);
01a3f561 14527 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14528 }
5e0116d5 14529 return TRUE;
394f9b3a 14530 }
5e0116d5 14531 return FALSE;
394f9b3a 14532}
ad8d3bb3 14533
ad8d3bb3 14534
5e0116d5
RS
14535/* Parse string STR as a 16-bit relocatable operand. Store the
14536 expression in *EP and the relocations in the array starting
14537 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14538
01a3f561 14539 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14540
5e0116d5 14541static size_t
17a2f251
TS
14542my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14543 char *str)
ad8d3bb3 14544{
5e0116d5
RS
14545 bfd_reloc_code_real_type reversed_reloc[3];
14546 size_t reloc_index, i;
09b8f35a
RS
14547 int crux_depth, str_depth;
14548 char *crux;
5e0116d5
RS
14549
14550 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14551 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14552 of the main expression and with CRUX_DEPTH containing the number
14553 of open brackets at that point. */
14554 reloc_index = -1;
14555 str_depth = 0;
14556 do
fb1b3232 14557 {
09b8f35a
RS
14558 reloc_index++;
14559 crux = str;
14560 crux_depth = str_depth;
14561
14562 /* Skip over whitespace and brackets, keeping count of the number
14563 of brackets. */
14564 while (*str == ' ' || *str == '\t' || *str == '(')
14565 if (*str++ == '(')
14566 str_depth++;
5e0116d5 14567 }
09b8f35a
RS
14568 while (*str == '%'
14569 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14570 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14571
09b8f35a 14572 my_getExpression (ep, crux);
5e0116d5 14573 str = expr_end;
394f9b3a 14574
5e0116d5 14575 /* Match every open bracket. */
09b8f35a 14576 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14577 if (*str++ == ')')
09b8f35a 14578 crux_depth--;
394f9b3a 14579
09b8f35a 14580 if (crux_depth > 0)
20203fb9 14581 as_bad (_("unclosed '('"));
394f9b3a 14582
5e0116d5 14583 expr_end = str;
252b5132 14584
01a3f561 14585 if (reloc_index != 0)
64bdfcaf
RS
14586 {
14587 prev_reloc_op_frag = frag_now;
14588 for (i = 0; i < reloc_index; i++)
14589 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14590 }
fb1b3232 14591
5e0116d5 14592 return reloc_index;
252b5132
RH
14593}
14594
14595static void
17a2f251 14596my_getExpression (expressionS *ep, char *str)
252b5132
RH
14597{
14598 char *save_in;
14599
14600 save_in = input_line_pointer;
14601 input_line_pointer = str;
14602 expression (ep);
14603 expr_end = input_line_pointer;
14604 input_line_pointer = save_in;
252b5132
RH
14605}
14606
252b5132 14607char *
17a2f251 14608md_atof (int type, char *litP, int *sizeP)
252b5132 14609{
499ac353 14610 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14611}
14612
14613void
17a2f251 14614md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14615{
14616 if (target_big_endian)
14617 number_to_chars_bigendian (buf, val, n);
14618 else
14619 number_to_chars_littleendian (buf, val, n);
14620}
14621\f
ae948b86 14622#ifdef OBJ_ELF
e013f690
TS
14623static int support_64bit_objects(void)
14624{
14625 const char **list, **l;
aa3d8fdf 14626 int yes;
e013f690
TS
14627
14628 list = bfd_target_list ();
14629 for (l = list; *l != NULL; l++)
aeffff67
RS
14630 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14631 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14632 break;
aa3d8fdf 14633 yes = (*l != NULL);
e013f690 14634 free (list);
aa3d8fdf 14635 return yes;
e013f690 14636}
ae948b86 14637#endif /* OBJ_ELF */
e013f690 14638
78849248 14639const char *md_shortopts = "O::g::G:";
252b5132 14640
23fce1e3
NC
14641enum options
14642 {
14643 OPTION_MARCH = OPTION_MD_BASE,
14644 OPTION_MTUNE,
14645 OPTION_MIPS1,
14646 OPTION_MIPS2,
14647 OPTION_MIPS3,
14648 OPTION_MIPS4,
14649 OPTION_MIPS5,
14650 OPTION_MIPS32,
14651 OPTION_MIPS64,
14652 OPTION_MIPS32R2,
14653 OPTION_MIPS64R2,
14654 OPTION_MIPS16,
14655 OPTION_NO_MIPS16,
14656 OPTION_MIPS3D,
14657 OPTION_NO_MIPS3D,
14658 OPTION_MDMX,
14659 OPTION_NO_MDMX,
14660 OPTION_DSP,
14661 OPTION_NO_DSP,
14662 OPTION_MT,
14663 OPTION_NO_MT,
b015e599
AP
14664 OPTION_VIRT,
14665 OPTION_NO_VIRT,
23fce1e3
NC
14666 OPTION_SMARTMIPS,
14667 OPTION_NO_SMARTMIPS,
14668 OPTION_DSPR2,
14669 OPTION_NO_DSPR2,
7f3c4072
CM
14670 OPTION_EVA,
14671 OPTION_NO_EVA,
df58fc94
RS
14672 OPTION_MICROMIPS,
14673 OPTION_NO_MICROMIPS,
dec0624d
MR
14674 OPTION_MCU,
14675 OPTION_NO_MCU,
23fce1e3
NC
14676 OPTION_COMPAT_ARCH_BASE,
14677 OPTION_M4650,
14678 OPTION_NO_M4650,
14679 OPTION_M4010,
14680 OPTION_NO_M4010,
14681 OPTION_M4100,
14682 OPTION_NO_M4100,
14683 OPTION_M3900,
14684 OPTION_NO_M3900,
14685 OPTION_M7000_HILO_FIX,
6a32d874
CM
14686 OPTION_MNO_7000_HILO_FIX,
14687 OPTION_FIX_24K,
14688 OPTION_NO_FIX_24K,
c67a084a
NC
14689 OPTION_FIX_LOONGSON2F_JUMP,
14690 OPTION_NO_FIX_LOONGSON2F_JUMP,
14691 OPTION_FIX_LOONGSON2F_NOP,
14692 OPTION_NO_FIX_LOONGSON2F_NOP,
23fce1e3
NC
14693 OPTION_FIX_VR4120,
14694 OPTION_NO_FIX_VR4120,
14695 OPTION_FIX_VR4130,
14696 OPTION_NO_FIX_VR4130,
d954098f
DD
14697 OPTION_FIX_CN63XXP1,
14698 OPTION_NO_FIX_CN63XXP1,
23fce1e3
NC
14699 OPTION_TRAP,
14700 OPTION_BREAK,
14701 OPTION_EB,
14702 OPTION_EL,
14703 OPTION_FP32,
14704 OPTION_GP32,
14705 OPTION_CONSTRUCT_FLOATS,
14706 OPTION_NO_CONSTRUCT_FLOATS,
14707 OPTION_FP64,
14708 OPTION_GP64,
14709 OPTION_RELAX_BRANCH,
14710 OPTION_NO_RELAX_BRANCH,
14711 OPTION_MSHARED,
14712 OPTION_MNO_SHARED,
14713 OPTION_MSYM32,
14714 OPTION_MNO_SYM32,
14715 OPTION_SOFT_FLOAT,
14716 OPTION_HARD_FLOAT,
14717 OPTION_SINGLE_FLOAT,
14718 OPTION_DOUBLE_FLOAT,
14719 OPTION_32,
14720#ifdef OBJ_ELF
14721 OPTION_CALL_SHARED,
14722 OPTION_CALL_NONPIC,
14723 OPTION_NON_SHARED,
14724 OPTION_XGOT,
14725 OPTION_MABI,
14726 OPTION_N32,
14727 OPTION_64,
14728 OPTION_MDEBUG,
14729 OPTION_NO_MDEBUG,
14730 OPTION_PDR,
14731 OPTION_NO_PDR,
14732 OPTION_MVXWORKS_PIC,
14733#endif /* OBJ_ELF */
14734 OPTION_END_OF_ENUM
14735 };
14736
e972090a
NC
14737struct option md_longopts[] =
14738{
f9b4148d 14739 /* Options which specify architecture. */
f9b4148d 14740 {"march", required_argument, NULL, OPTION_MARCH},
f9b4148d 14741 {"mtune", required_argument, NULL, OPTION_MTUNE},
252b5132
RH
14742 {"mips0", no_argument, NULL, OPTION_MIPS1},
14743 {"mips1", no_argument, NULL, OPTION_MIPS1},
252b5132 14744 {"mips2", no_argument, NULL, OPTION_MIPS2},
252b5132 14745 {"mips3", no_argument, NULL, OPTION_MIPS3},
252b5132 14746 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86 14747 {"mips5", no_argument, NULL, OPTION_MIPS5},
ae948b86 14748 {"mips32", no_argument, NULL, OPTION_MIPS32},
ae948b86 14749 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d 14750 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13 14751 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
14752
14753 /* Options which specify Application Specific Extensions (ASEs). */
f9b4148d 14754 {"mips16", no_argument, NULL, OPTION_MIPS16},
f9b4148d 14755 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
f9b4148d 14756 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
f9b4148d 14757 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
f9b4148d 14758 {"mdmx", no_argument, NULL, OPTION_MDMX},
f9b4148d 14759 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
74cd071d 14760 {"mdsp", no_argument, NULL, OPTION_DSP},
74cd071d 14761 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
ef2e4d86 14762 {"mmt", no_argument, NULL, OPTION_MT},
ef2e4d86 14763 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
e16bfa71 14764 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
e16bfa71 14765 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
8b082fb1 14766 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
8b082fb1 14767 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
7f3c4072
CM
14768 {"meva", no_argument, NULL, OPTION_EVA},
14769 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
df58fc94
RS
14770 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
14771 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
dec0624d
MR
14772 {"mmcu", no_argument, NULL, OPTION_MCU},
14773 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
b015e599
AP
14774 {"mvirt", no_argument, NULL, OPTION_VIRT},
14775 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
f9b4148d
CD
14776
14777 /* Old-style architecture options. Don't add more of these. */
f9b4148d 14778 {"m4650", no_argument, NULL, OPTION_M4650},
f9b4148d 14779 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
f9b4148d 14780 {"m4010", no_argument, NULL, OPTION_M4010},
f9b4148d 14781 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
f9b4148d 14782 {"m4100", no_argument, NULL, OPTION_M4100},
f9b4148d 14783 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
f9b4148d 14784 {"m3900", no_argument, NULL, OPTION_M3900},
f9b4148d
CD
14785 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
14786
14787 /* Options which enable bug fixes. */
f9b4148d 14788 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
f9b4148d
CD
14789 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14790 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
c67a084a
NC
14791 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
14792 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
14793 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
14794 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
d766e8ec
RS
14795 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
14796 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
7d8e00cf
RS
14797 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
14798 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
6a32d874
CM
14799 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
14800 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
d954098f
DD
14801 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
14802 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
f9b4148d
CD
14803
14804 /* Miscellaneous options. */
252b5132
RH
14805 {"trap", no_argument, NULL, OPTION_TRAP},
14806 {"no-break", no_argument, NULL, OPTION_TRAP},
252b5132
RH
14807 {"break", no_argument, NULL, OPTION_BREAK},
14808 {"no-trap", no_argument, NULL, OPTION_BREAK},
252b5132 14809 {"EB", no_argument, NULL, OPTION_EB},
252b5132 14810 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 14811 {"mfp32", no_argument, NULL, OPTION_FP32},
c97ef257 14812 {"mgp32", no_argument, NULL, OPTION_GP32},
119d663a 14813 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
119d663a 14814 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
316f5878 14815 {"mfp64", no_argument, NULL, OPTION_FP64},
ae948b86 14816 {"mgp64", no_argument, NULL, OPTION_GP64},
4a6a3df4
AO
14817 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
14818 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
aa6975fb
ILT
14819 {"mshared", no_argument, NULL, OPTION_MSHARED},
14820 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
aed1a261
RS
14821 {"msym32", no_argument, NULL, OPTION_MSYM32},
14822 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
037b32b9
AN
14823 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
14824 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
037b32b9
AN
14825 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
14826 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
23fce1e3
NC
14827
14828 /* Strictly speaking this next option is ELF specific,
14829 but we allow it for other ports as well in order to
14830 make testing easier. */
14831 {"32", no_argument, NULL, OPTION_32},
037b32b9 14832
f9b4148d 14833 /* ELF-specific options. */
156c2f8b 14834#ifdef OBJ_ELF
156c2f8b
NC
14835 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
14836 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
861fb55a 14837 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
156c2f8b
NC
14838 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
14839 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86 14840 {"mabi", required_argument, NULL, OPTION_MABI},
e013f690 14841 {"n32", no_argument, NULL, OPTION_N32},
156c2f8b 14842 {"64", no_argument, NULL, OPTION_64},
ecb4347a 14843 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
ecb4347a 14844 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe 14845 {"mpdr", no_argument, NULL, OPTION_PDR},
dcd410fe 14846 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
0a44bf69 14847 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ae948b86 14848#endif /* OBJ_ELF */
f9b4148d 14849
252b5132
RH
14850 {NULL, no_argument, NULL, 0}
14851};
156c2f8b 14852size_t md_longopts_size = sizeof (md_longopts);
252b5132 14853
316f5878
RS
14854/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14855 NEW_VALUE. Warn if another value was already specified. Note:
14856 we have to defer parsing the -march and -mtune arguments in order
14857 to handle 'from-abi' correctly, since the ABI might be specified
14858 in a later argument. */
14859
14860static void
17a2f251 14861mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14862{
14863 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14864 as_warn (_("A different %s was already specified, is now %s"),
14865 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14866 new_value);
14867
14868 *string_ptr = new_value;
14869}
14870
252b5132 14871int
17a2f251 14872md_parse_option (int c, char *arg)
252b5132
RH
14873{
14874 switch (c)
14875 {
119d663a
NC
14876 case OPTION_CONSTRUCT_FLOATS:
14877 mips_disable_float_construction = 0;
14878 break;
bdaaa2e1 14879
119d663a
NC
14880 case OPTION_NO_CONSTRUCT_FLOATS:
14881 mips_disable_float_construction = 1;
14882 break;
bdaaa2e1 14883
252b5132
RH
14884 case OPTION_TRAP:
14885 mips_trap = 1;
14886 break;
14887
14888 case OPTION_BREAK:
14889 mips_trap = 0;
14890 break;
14891
14892 case OPTION_EB:
14893 target_big_endian = 1;
14894 break;
14895
14896 case OPTION_EL:
14897 target_big_endian = 0;
14898 break;
14899
14900 case 'O':
4ffff32f
TS
14901 if (arg == NULL)
14902 mips_optimize = 1;
14903 else if (arg[0] == '0')
14904 mips_optimize = 0;
14905 else if (arg[0] == '1')
252b5132
RH
14906 mips_optimize = 1;
14907 else
14908 mips_optimize = 2;
14909 break;
14910
14911 case 'g':
14912 if (arg == NULL)
14913 mips_debug = 2;
14914 else
14915 mips_debug = atoi (arg);
252b5132
RH
14916 break;
14917
14918 case OPTION_MIPS1:
316f5878 14919 file_mips_isa = ISA_MIPS1;
252b5132
RH
14920 break;
14921
14922 case OPTION_MIPS2:
316f5878 14923 file_mips_isa = ISA_MIPS2;
252b5132
RH
14924 break;
14925
14926 case OPTION_MIPS3:
316f5878 14927 file_mips_isa = ISA_MIPS3;
252b5132
RH
14928 break;
14929
14930 case OPTION_MIPS4:
316f5878 14931 file_mips_isa = ISA_MIPS4;
e7af610e
NC
14932 break;
14933
84ea6cf2 14934 case OPTION_MIPS5:
316f5878 14935 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
14936 break;
14937
e7af610e 14938 case OPTION_MIPS32:
316f5878 14939 file_mips_isa = ISA_MIPS32;
252b5132
RH
14940 break;
14941
af7ee8bf
CD
14942 case OPTION_MIPS32R2:
14943 file_mips_isa = ISA_MIPS32R2;
14944 break;
14945
5f74bc13
CD
14946 case OPTION_MIPS64R2:
14947 file_mips_isa = ISA_MIPS64R2;
14948 break;
14949
84ea6cf2 14950 case OPTION_MIPS64:
316f5878 14951 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
14952 break;
14953
ec68c924 14954 case OPTION_MTUNE:
316f5878
RS
14955 mips_set_option_string (&mips_tune_string, arg);
14956 break;
ec68c924 14957
316f5878
RS
14958 case OPTION_MARCH:
14959 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14960 break;
14961
14962 case OPTION_M4650:
316f5878
RS
14963 mips_set_option_string (&mips_arch_string, "4650");
14964 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14965 break;
14966
14967 case OPTION_NO_M4650:
14968 break;
14969
14970 case OPTION_M4010:
316f5878
RS
14971 mips_set_option_string (&mips_arch_string, "4010");
14972 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14973 break;
14974
14975 case OPTION_NO_M4010:
14976 break;
14977
14978 case OPTION_M4100:
316f5878
RS
14979 mips_set_option_string (&mips_arch_string, "4100");
14980 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14981 break;
14982
14983 case OPTION_NO_M4100:
14984 break;
14985
252b5132 14986 case OPTION_M3900:
316f5878
RS
14987 mips_set_option_string (&mips_arch_string, "3900");
14988 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14989 break;
bdaaa2e1 14990
252b5132
RH
14991 case OPTION_NO_M3900:
14992 break;
14993
deec1734 14994 case OPTION_MDMX:
846ef2d0
RS
14995 mips_opts.ase |= ASE_MDMX;
14996 file_ase_explicit |= ASE_MDMX;
deec1734
CD
14997 break;
14998
14999 case OPTION_NO_MDMX:
846ef2d0
RS
15000 mips_opts.ase &= ~ASE_MDMX;
15001 file_ase_explicit |= ASE_MDMX;
deec1734
CD
15002 break;
15003
74cd071d 15004 case OPTION_DSP:
846ef2d0
RS
15005 mips_opts.ase |= ASE_DSP;
15006 mips_opts.ase &= ~ASE_DSPR2;
15007 file_ase_explicit |= ASE_DSP | ASE_DSPR2;
8b082fb1
TS
15008 break;
15009
15010 case OPTION_DSPR2:
846ef2d0
RS
15011 mips_opts.ase |= ASE_DSP | ASE_DSPR2;
15012 file_ase_explicit |= ASE_DSP | ASE_DSPR2;
8b082fb1
TS
15013 break;
15014
846ef2d0 15015 case OPTION_NO_DSP:
8b082fb1 15016 case OPTION_NO_DSPR2:
846ef2d0
RS
15017 mips_opts.ase &= ~(ASE_DSP | ASE_DSPR2);
15018 file_ase_explicit |= ASE_DSP | ASE_DSPR2;
74cd071d
CF
15019 break;
15020
7f3c4072 15021 case OPTION_EVA:
846ef2d0
RS
15022 mips_opts.ase |= ASE_EVA;
15023 file_ase_explicit |= ASE_EVA;
7f3c4072
CM
15024 break;
15025
15026 case OPTION_NO_EVA:
846ef2d0
RS
15027 mips_opts.ase &= ~ASE_EVA;
15028 file_ase_explicit |= ASE_EVA;
7f3c4072
CM
15029 break;
15030
ef2e4d86 15031 case OPTION_MT:
846ef2d0
RS
15032 mips_opts.ase |= ASE_MT;
15033 file_ase_explicit |= ASE_MT;
ef2e4d86
CF
15034 break;
15035
15036 case OPTION_NO_MT:
846ef2d0
RS
15037 mips_opts.ase &= ~ASE_MT;
15038 file_ase_explicit |= ASE_MT;
ef2e4d86
CF
15039 break;
15040
dec0624d 15041 case OPTION_MCU:
846ef2d0
RS
15042 mips_opts.ase |= ASE_MCU;
15043 file_ase_explicit |= ASE_MCU;
dec0624d
MR
15044 break;
15045
15046 case OPTION_NO_MCU:
846ef2d0
RS
15047 mips_opts.ase &= ~ASE_MCU;
15048 file_ase_explicit |= ASE_MCU;
dec0624d
MR
15049 break;
15050
df58fc94
RS
15051 case OPTION_MICROMIPS:
15052 if (mips_opts.mips16 == 1)
15053 {
15054 as_bad (_("-mmicromips cannot be used with -mips16"));
15055 return 0;
15056 }
15057 mips_opts.micromips = 1;
15058 mips_no_prev_insn ();
15059 break;
15060
15061 case OPTION_NO_MICROMIPS:
15062 mips_opts.micromips = 0;
15063 mips_no_prev_insn ();
15064 break;
15065
b015e599 15066 case OPTION_VIRT:
846ef2d0
RS
15067 mips_opts.ase |= ASE_VIRT;
15068 file_ase_explicit |= ASE_VIRT;
b015e599
AP
15069 break;
15070
15071 case OPTION_NO_VIRT:
846ef2d0
RS
15072 mips_opts.ase &= ~ASE_VIRT;
15073 file_ase_explicit |= ASE_VIRT;
b015e599
AP
15074 break;
15075
252b5132 15076 case OPTION_MIPS16:
df58fc94
RS
15077 if (mips_opts.micromips == 1)
15078 {
15079 as_bad (_("-mips16 cannot be used with -micromips"));
15080 return 0;
15081 }
252b5132 15082 mips_opts.mips16 = 1;
7d10b47d 15083 mips_no_prev_insn ();
252b5132
RH
15084 break;
15085
15086 case OPTION_NO_MIPS16:
15087 mips_opts.mips16 = 0;
7d10b47d 15088 mips_no_prev_insn ();
252b5132
RH
15089 break;
15090
1f25f5d3 15091 case OPTION_MIPS3D:
846ef2d0
RS
15092 mips_opts.ase |= ASE_MIPS3D;
15093 file_ase_explicit |= ASE_MIPS3D;
1f25f5d3
CD
15094 break;
15095
15096 case OPTION_NO_MIPS3D:
846ef2d0
RS
15097 mips_opts.ase &= ~ASE_MIPS3D;
15098 file_ase_explicit |= ASE_MIPS3D;
1f25f5d3
CD
15099 break;
15100
e16bfa71 15101 case OPTION_SMARTMIPS:
846ef2d0
RS
15102 mips_opts.ase |= ASE_SMARTMIPS;
15103 file_ase_explicit |= ASE_SMARTMIPS;
e16bfa71
TS
15104 break;
15105
15106 case OPTION_NO_SMARTMIPS:
846ef2d0
RS
15107 mips_opts.ase &= ~ASE_SMARTMIPS;
15108 file_ase_explicit |= ASE_SMARTMIPS;
e16bfa71
TS
15109 break;
15110
6a32d874
CM
15111 case OPTION_FIX_24K:
15112 mips_fix_24k = 1;
15113 break;
15114
15115 case OPTION_NO_FIX_24K:
15116 mips_fix_24k = 0;
15117 break;
15118
c67a084a
NC
15119 case OPTION_FIX_LOONGSON2F_JUMP:
15120 mips_fix_loongson2f_jump = TRUE;
15121 break;
15122
15123 case OPTION_NO_FIX_LOONGSON2F_JUMP:
15124 mips_fix_loongson2f_jump = FALSE;
15125 break;
15126
15127 case OPTION_FIX_LOONGSON2F_NOP:
15128 mips_fix_loongson2f_nop = TRUE;
15129 break;
15130
15131 case OPTION_NO_FIX_LOONGSON2F_NOP:
15132 mips_fix_loongson2f_nop = FALSE;
15133 break;
15134
d766e8ec
RS
15135 case OPTION_FIX_VR4120:
15136 mips_fix_vr4120 = 1;
60b63b72
RS
15137 break;
15138
d766e8ec
RS
15139 case OPTION_NO_FIX_VR4120:
15140 mips_fix_vr4120 = 0;
60b63b72
RS
15141 break;
15142
7d8e00cf
RS
15143 case OPTION_FIX_VR4130:
15144 mips_fix_vr4130 = 1;
15145 break;
15146
15147 case OPTION_NO_FIX_VR4130:
15148 mips_fix_vr4130 = 0;
15149 break;
15150
d954098f
DD
15151 case OPTION_FIX_CN63XXP1:
15152 mips_fix_cn63xxp1 = TRUE;
15153 break;
15154
15155 case OPTION_NO_FIX_CN63XXP1:
15156 mips_fix_cn63xxp1 = FALSE;
15157 break;
15158
4a6a3df4
AO
15159 case OPTION_RELAX_BRANCH:
15160 mips_relax_branch = 1;
15161 break;
15162
15163 case OPTION_NO_RELAX_BRANCH:
15164 mips_relax_branch = 0;
15165 break;
15166
aa6975fb
ILT
15167 case OPTION_MSHARED:
15168 mips_in_shared = TRUE;
15169 break;
15170
15171 case OPTION_MNO_SHARED:
15172 mips_in_shared = FALSE;
15173 break;
15174
aed1a261
RS
15175 case OPTION_MSYM32:
15176 mips_opts.sym32 = TRUE;
15177 break;
15178
15179 case OPTION_MNO_SYM32:
15180 mips_opts.sym32 = FALSE;
15181 break;
15182
0f074f60 15183#ifdef OBJ_ELF
252b5132
RH
15184 /* When generating ELF code, we permit -KPIC and -call_shared to
15185 select SVR4_PIC, and -non_shared to select no PIC. This is
15186 intended to be compatible with Irix 5. */
15187 case OPTION_CALL_SHARED:
f43abd2b 15188 if (!IS_ELF)
252b5132
RH
15189 {
15190 as_bad (_("-call_shared is supported only for ELF format"));
15191 return 0;
15192 }
15193 mips_pic = SVR4_PIC;
143d77c5 15194 mips_abicalls = TRUE;
252b5132
RH
15195 break;
15196
861fb55a
DJ
15197 case OPTION_CALL_NONPIC:
15198 if (!IS_ELF)
15199 {
15200 as_bad (_("-call_nonpic is supported only for ELF format"));
15201 return 0;
15202 }
15203 mips_pic = NO_PIC;
15204 mips_abicalls = TRUE;
15205 break;
15206
252b5132 15207 case OPTION_NON_SHARED:
f43abd2b 15208 if (!IS_ELF)
252b5132
RH
15209 {
15210 as_bad (_("-non_shared is supported only for ELF format"));
15211 return 0;
15212 }
15213 mips_pic = NO_PIC;
143d77c5 15214 mips_abicalls = FALSE;
252b5132
RH
15215 break;
15216
44075ae2
TS
15217 /* The -xgot option tells the assembler to use 32 bit offsets
15218 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
15219 compatibility. */
15220 case OPTION_XGOT:
15221 mips_big_got = 1;
15222 break;
0f074f60 15223#endif /* OBJ_ELF */
252b5132
RH
15224
15225 case 'G':
6caf9ef4
TS
15226 g_switch_value = atoi (arg);
15227 g_switch_seen = 1;
252b5132
RH
15228 break;
15229
34ba82a8
TS
15230 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15231 and -mabi=64. */
252b5132 15232 case OPTION_32:
23fce1e3
NC
15233 if (IS_ELF)
15234 mips_abi = O32_ABI;
15235 /* We silently ignore -32 for non-ELF targets. This greatly
15236 simplifies the construction of the MIPS GAS test cases. */
252b5132
RH
15237 break;
15238
23fce1e3 15239#ifdef OBJ_ELF
e013f690 15240 case OPTION_N32:
f43abd2b 15241 if (!IS_ELF)
34ba82a8
TS
15242 {
15243 as_bad (_("-n32 is supported for ELF format only"));
15244 return 0;
15245 }
316f5878 15246 mips_abi = N32_ABI;
e013f690 15247 break;
252b5132 15248
e013f690 15249 case OPTION_64:
f43abd2b 15250 if (!IS_ELF)
34ba82a8
TS
15251 {
15252 as_bad (_("-64 is supported for ELF format only"));
15253 return 0;
15254 }
316f5878 15255 mips_abi = N64_ABI;
f43abd2b 15256 if (!support_64bit_objects())
e013f690 15257 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 15258 break;
ae948b86 15259#endif /* OBJ_ELF */
252b5132 15260
c97ef257 15261 case OPTION_GP32:
a325df1d 15262 file_mips_gp32 = 1;
c97ef257
AH
15263 break;
15264
15265 case OPTION_GP64:
a325df1d 15266 file_mips_gp32 = 0;
c97ef257 15267 break;
252b5132 15268
ca4e0257 15269 case OPTION_FP32:
a325df1d 15270 file_mips_fp32 = 1;
316f5878
RS
15271 break;
15272
15273 case OPTION_FP64:
15274 file_mips_fp32 = 0;
ca4e0257
RS
15275 break;
15276
037b32b9
AN
15277 case OPTION_SINGLE_FLOAT:
15278 file_mips_single_float = 1;
15279 break;
15280
15281 case OPTION_DOUBLE_FLOAT:
15282 file_mips_single_float = 0;
15283 break;
15284
15285 case OPTION_SOFT_FLOAT:
15286 file_mips_soft_float = 1;
15287 break;
15288
15289 case OPTION_HARD_FLOAT:
15290 file_mips_soft_float = 0;
15291 break;
15292
ae948b86 15293#ifdef OBJ_ELF
252b5132 15294 case OPTION_MABI:
f43abd2b 15295 if (!IS_ELF)
34ba82a8
TS
15296 {
15297 as_bad (_("-mabi is supported for ELF format only"));
15298 return 0;
15299 }
e013f690 15300 if (strcmp (arg, "32") == 0)
316f5878 15301 mips_abi = O32_ABI;
e013f690 15302 else if (strcmp (arg, "o64") == 0)
316f5878 15303 mips_abi = O64_ABI;
e013f690 15304 else if (strcmp (arg, "n32") == 0)
316f5878 15305 mips_abi = N32_ABI;
e013f690
TS
15306 else if (strcmp (arg, "64") == 0)
15307 {
316f5878 15308 mips_abi = N64_ABI;
e013f690
TS
15309 if (! support_64bit_objects())
15310 as_fatal (_("No compiled in support for 64 bit object file "
15311 "format"));
15312 }
15313 else if (strcmp (arg, "eabi") == 0)
316f5878 15314 mips_abi = EABI_ABI;
e013f690 15315 else
da0e507f
TS
15316 {
15317 as_fatal (_("invalid abi -mabi=%s"), arg);
15318 return 0;
15319 }
252b5132 15320 break;
e013f690 15321#endif /* OBJ_ELF */
252b5132 15322
6b76fefe 15323 case OPTION_M7000_HILO_FIX:
b34976b6 15324 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
15325 break;
15326
9ee72ff1 15327 case OPTION_MNO_7000_HILO_FIX:
b34976b6 15328 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
15329 break;
15330
ecb4347a
DJ
15331#ifdef OBJ_ELF
15332 case OPTION_MDEBUG:
b34976b6 15333 mips_flag_mdebug = TRUE;
ecb4347a
DJ
15334 break;
15335
15336 case OPTION_NO_MDEBUG:
b34976b6 15337 mips_flag_mdebug = FALSE;
ecb4347a 15338 break;
dcd410fe
RO
15339
15340 case OPTION_PDR:
15341 mips_flag_pdr = TRUE;
15342 break;
15343
15344 case OPTION_NO_PDR:
15345 mips_flag_pdr = FALSE;
15346 break;
0a44bf69
RS
15347
15348 case OPTION_MVXWORKS_PIC:
15349 mips_pic = VXWORKS_PIC;
15350 break;
ecb4347a
DJ
15351#endif /* OBJ_ELF */
15352
252b5132
RH
15353 default:
15354 return 0;
15355 }
15356
c67a084a
NC
15357 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15358
252b5132
RH
15359 return 1;
15360}
316f5878
RS
15361\f
15362/* Set up globals to generate code for the ISA or processor
15363 described by INFO. */
252b5132 15364
252b5132 15365static void
17a2f251 15366mips_set_architecture (const struct mips_cpu_info *info)
252b5132 15367{
316f5878 15368 if (info != 0)
252b5132 15369 {
fef14a42
TS
15370 file_mips_arch = info->cpu;
15371 mips_opts.arch = info->cpu;
316f5878 15372 mips_opts.isa = info->isa;
252b5132 15373 }
252b5132
RH
15374}
15375
252b5132 15376
316f5878 15377/* Likewise for tuning. */
252b5132 15378
316f5878 15379static void
17a2f251 15380mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
15381{
15382 if (info != 0)
fef14a42 15383 mips_tune = info->cpu;
316f5878 15384}
80cc45a5 15385
34ba82a8 15386
252b5132 15387void
17a2f251 15388mips_after_parse_args (void)
e9670677 15389{
fef14a42
TS
15390 const struct mips_cpu_info *arch_info = 0;
15391 const struct mips_cpu_info *tune_info = 0;
15392
e9670677 15393 /* GP relative stuff not working for PE */
6caf9ef4 15394 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 15395 {
6caf9ef4 15396 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
15397 as_bad (_("-G not supported in this configuration."));
15398 g_switch_value = 0;
15399 }
15400
cac012d6
AO
15401 if (mips_abi == NO_ABI)
15402 mips_abi = MIPS_DEFAULT_ABI;
15403
22923709
RS
15404 /* The following code determines the architecture and register size.
15405 Similar code was added to GCC 3.3 (see override_options() in
15406 config/mips/mips.c). The GAS and GCC code should be kept in sync
15407 as much as possible. */
e9670677 15408
316f5878 15409 if (mips_arch_string != 0)
fef14a42 15410 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 15411
316f5878 15412 if (file_mips_isa != ISA_UNKNOWN)
e9670677 15413 {
316f5878 15414 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 15415 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 15416 the -march selection (if any). */
fef14a42 15417 if (arch_info != 0)
e9670677 15418 {
316f5878
RS
15419 /* -march takes precedence over -mipsN, since it is more descriptive.
15420 There's no harm in specifying both as long as the ISA levels
15421 are the same. */
fef14a42 15422 if (file_mips_isa != arch_info->isa)
316f5878
RS
15423 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15424 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 15425 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15426 }
316f5878 15427 else
fef14a42 15428 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
15429 }
15430
fef14a42 15431 if (arch_info == 0)
95bfe26e
MF
15432 {
15433 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15434 gas_assert (arch_info);
15435 }
e9670677 15436
fef14a42 15437 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15438 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15439 arch_info->name);
15440
15441 mips_set_architecture (arch_info);
15442
15443 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
15444 if (mips_tune_string != 0)
15445 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15446
fef14a42
TS
15447 if (tune_info == 0)
15448 mips_set_tune (arch_info);
15449 else
15450 mips_set_tune (tune_info);
e9670677 15451
316f5878 15452 if (file_mips_gp32 >= 0)
e9670677 15453 {
316f5878
RS
15454 /* The user specified the size of the integer registers. Make sure
15455 it agrees with the ABI and ISA. */
15456 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15457 as_bad (_("-mgp64 used with a 32-bit processor"));
15458 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15459 as_bad (_("-mgp32 used with a 64-bit ABI"));
15460 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15461 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
15462 }
15463 else
15464 {
316f5878
RS
15465 /* Infer the integer register size from the ABI and processor.
15466 Restrict ourselves to 32-bit registers if that's all the
15467 processor has, or if the ABI cannot handle 64-bit registers. */
15468 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15469 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
15470 }
15471
ad3fea08
TS
15472 switch (file_mips_fp32)
15473 {
15474 default:
15475 case -1:
15476 /* No user specified float register size.
15477 ??? GAS treats single-float processors as though they had 64-bit
15478 float registers (although it complains when double-precision
15479 instructions are used). As things stand, saying they have 32-bit
15480 registers would lead to spurious "register must be even" messages.
15481 So here we assume float registers are never smaller than the
15482 integer ones. */
15483 if (file_mips_gp32 == 0)
15484 /* 64-bit integer registers implies 64-bit float registers. */
15485 file_mips_fp32 = 0;
846ef2d0 15486 else if ((mips_opts.ase & (ASE_MIPS3D | ASE_MDMX))
ad3fea08
TS
15487 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15488 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
15489 file_mips_fp32 = 0;
15490 else
15491 /* 32-bit float registers. */
15492 file_mips_fp32 = 1;
15493 break;
15494
15495 /* The user specified the size of the float registers. Check if it
15496 agrees with the ABI and ISA. */
15497 case 0:
15498 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15499 as_bad (_("-mfp64 used with a 32-bit fpu"));
15500 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15501 && !ISA_HAS_MXHC1 (mips_opts.isa))
15502 as_warn (_("-mfp64 used with a 32-bit ABI"));
15503 break;
15504 case 1:
15505 if (ABI_NEEDS_64BIT_REGS (mips_abi))
15506 as_warn (_("-mfp32 used with a 64-bit ABI"));
15507 break;
15508 }
e9670677 15509
316f5878 15510 /* End of GCC-shared inference code. */
e9670677 15511
17a2f251
TS
15512 /* This flag is set when we have a 64-bit capable CPU but use only
15513 32-bit wide registers. Note that EABI does not use it. */
15514 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15515 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15516 || mips_abi == O32_ABI))
316f5878 15517 mips_32bitmode = 1;
e9670677
MR
15518
15519 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15520 as_bad (_("trap exception not supported at ISA 1"));
15521
e9670677
MR
15522 /* If the selected architecture includes support for ASEs, enable
15523 generation of code for them. */
a4672219 15524 if (mips_opts.mips16 == -1)
fef14a42 15525 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
df58fc94
RS
15526 if (mips_opts.micromips == -1)
15527 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
846ef2d0
RS
15528
15529 /* MIPS3D and MDMX require 64-bit FPRs, so -mfp32 should stop those
15530 ASEs from being selected implicitly. */
15531 if (file_mips_fp32 == 1)
15532 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX;
15533
15534 /* If the user didn't explicitly select or deselect a particular ASE,
15535 use the default setting for the CPU. */
15536 mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
15537
15538 if ((mips_opts.ase & ASE_MIPS3D) && file_mips_fp32 == 1)
ad3fea08
TS
15539 as_bad (_("-mfp32 used with -mips3d"));
15540
846ef2d0 15541 if ((mips_opts.ase & ASE_MDMX) && file_mips_fp32 == 1)
ad3fea08
TS
15542 as_bad (_("-mfp32 used with -mdmx"));
15543
846ef2d0 15544 if ((mips_opts.ase & ASE_SMARTMIPS) && !ISA_SUPPORTS_SMARTMIPS)
20203fb9
NC
15545 as_warn (_("%s ISA does not support SmartMIPS"),
15546 mips_cpu_info_from_isa (mips_opts.isa)->name);
ad3fea08 15547
846ef2d0 15548 if ((mips_opts.ase & ASE_DSP) && !ISA_SUPPORTS_DSP_ASE)
20203fb9
NC
15549 as_warn (_("%s ISA does not support DSP ASE"),
15550 mips_cpu_info_from_isa (mips_opts.isa)->name);
ad3fea08 15551
846ef2d0 15552 if ((mips_opts.ase & ASE_DSPR2) && !ISA_SUPPORTS_DSPR2_ASE)
20203fb9
NC
15553 as_warn (_("%s ISA does not support DSP R2 ASE"),
15554 mips_cpu_info_from_isa (mips_opts.isa)->name);
8b082fb1 15555
846ef2d0 15556 if ((mips_opts.ase & ASE_EVA) && !ISA_SUPPORTS_EVA_ASE)
7f3c4072
CM
15557 as_warn (_("%s ISA does not support EVA ASE"),
15558 mips_cpu_info_from_isa (mips_opts.isa)->name);
15559
846ef2d0 15560 if ((mips_opts.ase & ASE_MT) && !ISA_SUPPORTS_MT_ASE)
20203fb9
NC
15561 as_warn (_("%s ISA does not support MT ASE"),
15562 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 15563
846ef2d0
RS
15564 if ((mips_opts.ase & ASE_MCU) && !ISA_SUPPORTS_MCU_ASE)
15565 as_warn (_("%s ISA does not support MCU ASE"),
15566 mips_cpu_info_from_isa (mips_opts.isa)->name);
dec0624d 15567
846ef2d0 15568 if ((mips_opts.ase & ASE_VIRT) && !ISA_SUPPORTS_VIRT_ASE)
b015e599
AP
15569 as_warn (_("%s ISA does not support Virtualization ASE"),
15570 mips_cpu_info_from_isa (mips_opts.isa)->name);
15571
e9670677 15572 file_mips_isa = mips_opts.isa;
846ef2d0 15573 file_ase = mips_opts.ase;
e9670677
MR
15574 mips_opts.gp32 = file_mips_gp32;
15575 mips_opts.fp32 = file_mips_fp32;
037b32b9
AN
15576 mips_opts.soft_float = file_mips_soft_float;
15577 mips_opts.single_float = file_mips_single_float;
e9670677 15578
ecb4347a
DJ
15579 if (mips_flag_mdebug < 0)
15580 {
15581#ifdef OBJ_MAYBE_ECOFF
15582 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
15583 mips_flag_mdebug = 1;
15584 else
15585#endif /* OBJ_MAYBE_ECOFF */
15586 mips_flag_mdebug = 0;
15587 }
e9670677
MR
15588}
15589\f
15590void
17a2f251 15591mips_init_after_args (void)
252b5132
RH
15592{
15593 /* initialize opcodes */
15594 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15595 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15596}
15597
15598long
17a2f251 15599md_pcrel_from (fixS *fixP)
252b5132 15600{
a7ebbfdf
TS
15601 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15602 switch (fixP->fx_r_type)
15603 {
df58fc94
RS
15604 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15605 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15606 /* Return the address of the delay slot. */
15607 return addr + 2;
15608
15609 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15610 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf
TS
15611 case BFD_RELOC_16_PCREL_S2:
15612 case BFD_RELOC_MIPS_JMP:
15613 /* Return the address of the delay slot. */
15614 return addr + 4;
df58fc94 15615
b47468a6
CM
15616 case BFD_RELOC_32_PCREL:
15617 return addr;
15618
a7ebbfdf 15619 default:
58ea3d6a 15620 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
15621 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15622 as_bad_where (fixP->fx_file, fixP->fx_line,
15623 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
15624 return addr;
15625 }
252b5132
RH
15626}
15627
252b5132
RH
15628/* This is called before the symbol table is processed. In order to
15629 work with gcc when using mips-tfile, we must keep all local labels.
15630 However, in other cases, we want to discard them. If we were
15631 called with -g, but we didn't see any debugging information, it may
15632 mean that gcc is smuggling debugging information through to
15633 mips-tfile, in which case we must generate all local labels. */
15634
15635void
17a2f251 15636mips_frob_file_before_adjust (void)
252b5132
RH
15637{
15638#ifndef NO_ECOFF_DEBUGGING
15639 if (ECOFF_DEBUGGING
15640 && mips_debug != 0
15641 && ! ecoff_debugging_seen)
15642 flag_keep_locals = 1;
15643#endif
15644}
15645
3b91255e 15646/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15647 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15648 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15649 relocation operators.
15650
15651 For our purposes, a %lo() expression matches a %got() or %hi()
15652 expression if:
15653
15654 (a) it refers to the same symbol; and
15655 (b) the offset applied in the %lo() expression is no lower than
15656 the offset applied in the %got() or %hi().
15657
15658 (b) allows us to cope with code like:
15659
15660 lui $4,%hi(foo)
15661 lh $4,%lo(foo+2)($4)
15662
15663 ...which is legal on RELA targets, and has a well-defined behaviour
15664 if the user knows that adding 2 to "foo" will not induce a carry to
15665 the high 16 bits.
15666
15667 When several %lo()s match a particular %got() or %hi(), we use the
15668 following rules to distinguish them:
15669
15670 (1) %lo()s with smaller offsets are a better match than %lo()s with
15671 higher offsets.
15672
15673 (2) %lo()s with no matching %got() or %hi() are better than those
15674 that already have a matching %got() or %hi().
15675
15676 (3) later %lo()s are better than earlier %lo()s.
15677
15678 These rules are applied in order.
15679
15680 (1) means, among other things, that %lo()s with identical offsets are
15681 chosen if they exist.
15682
15683 (2) means that we won't associate several high-part relocations with
15684 the same low-part relocation unless there's no alternative. Having
15685 several high parts for the same low part is a GNU extension; this rule
15686 allows careful users to avoid it.
15687
15688 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15689 with the last high-part relocation being at the front of the list.
15690 It therefore makes sense to choose the last matching low-part
15691 relocation, all other things being equal. It's also easier
15692 to code that way. */
252b5132
RH
15693
15694void
17a2f251 15695mips_frob_file (void)
252b5132
RH
15696{
15697 struct mips_hi_fixup *l;
35903be0 15698 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15699
15700 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15701 {
15702 segment_info_type *seginfo;
3b91255e
RS
15703 bfd_boolean matched_lo_p;
15704 fixS **hi_pos, **lo_pos, **pos;
252b5132 15705
9c2799c2 15706 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15707
5919d012 15708 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15709 there isn't supposed to be a matching LO. Ignore %gots against
15710 constants; we'll report an error for those later. */
738e5348 15711 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab
RS
15712 && !(l->fixp->fx_addsy
15713 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
5919d012
RS
15714 continue;
15715
15716 /* Check quickly whether the next fixup happens to be a matching %lo. */
15717 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15718 continue;
15719
252b5132 15720 seginfo = seg_info (l->seg);
252b5132 15721
3b91255e
RS
15722 /* Set HI_POS to the position of this relocation in the chain.
15723 Set LO_POS to the position of the chosen low-part relocation.
15724 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15725 relocation that matches an immediately-preceding high-part
15726 relocation. */
15727 hi_pos = NULL;
15728 lo_pos = NULL;
15729 matched_lo_p = FALSE;
738e5348 15730 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15731
3b91255e
RS
15732 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15733 {
15734 if (*pos == l->fixp)
15735 hi_pos = pos;
15736
35903be0 15737 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15738 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15739 && (*pos)->fx_offset >= l->fixp->fx_offset
15740 && (lo_pos == NULL
15741 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15742 || (!matched_lo_p
15743 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15744 lo_pos = pos;
15745
15746 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15747 && fixup_has_matching_lo_p (*pos));
15748 }
15749
15750 /* If we found a match, remove the high-part relocation from its
15751 current position and insert it before the low-part relocation.
15752 Make the offsets match so that fixup_has_matching_lo_p()
15753 will return true.
15754
15755 We don't warn about unmatched high-part relocations since some
15756 versions of gcc have been known to emit dead "lui ...%hi(...)"
15757 instructions. */
15758 if (lo_pos != NULL)
15759 {
15760 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15761 if (l->fixp->fx_next != *lo_pos)
252b5132 15762 {
3b91255e
RS
15763 *hi_pos = l->fixp->fx_next;
15764 l->fixp->fx_next = *lo_pos;
15765 *lo_pos = l->fixp;
252b5132 15766 }
252b5132
RH
15767 }
15768 }
15769}
15770
252b5132 15771int
17a2f251 15772mips_force_relocation (fixS *fixp)
252b5132 15773{
ae6063d4 15774 if (generic_force_reloc (fixp))
252b5132
RH
15775 return 1;
15776
df58fc94
RS
15777 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15778 so that the linker relaxation can update targets. */
15779 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15780 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15781 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15782 return 1;
15783
3e722fb5 15784 return 0;
252b5132
RH
15785}
15786
b886a2ab
RS
15787/* Read the instruction associated with RELOC from BUF. */
15788
15789static unsigned int
15790read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15791{
15792 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15793 return read_compressed_insn (buf, 4);
15794 else
15795 return read_insn (buf);
15796}
15797
15798/* Write instruction INSN to BUF, given that it has been relocated
15799 by RELOC. */
15800
15801static void
15802write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15803 unsigned long insn)
15804{
15805 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15806 write_compressed_insn (buf, insn, 4);
15807 else
15808 write_insn (buf, insn);
15809}
15810
252b5132
RH
15811/* Apply a fixup to the object file. */
15812
94f592af 15813void
55cf6793 15814md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15815{
4d68580a 15816 char *buf;
b886a2ab 15817 unsigned long insn;
a7ebbfdf 15818 reloc_howto_type *howto;
252b5132 15819
a7ebbfdf
TS
15820 /* We ignore generic BFD relocations we don't know about. */
15821 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15822 if (! howto)
15823 return;
65551fa4 15824
df58fc94
RS
15825 gas_assert (fixP->fx_size == 2
15826 || fixP->fx_size == 4
90ecf173
MR
15827 || fixP->fx_r_type == BFD_RELOC_16
15828 || fixP->fx_r_type == BFD_RELOC_64
15829 || fixP->fx_r_type == BFD_RELOC_CTOR
15830 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15831 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15832 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15833 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15834 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 15835
4d68580a 15836 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15837
df58fc94
RS
15838 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15839 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15840 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
15841 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
15842 || fixP->fx_r_type == BFD_RELOC_32_PCREL);
b1dca8ee
RS
15843
15844 /* Don't treat parts of a composite relocation as done. There are two
15845 reasons for this:
15846
15847 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15848 should nevertheless be emitted if the first part is.
15849
15850 (2) In normal usage, composite relocations are never assembly-time
15851 constants. The easiest way of dealing with the pathological
15852 exceptions is to generate a relocation against STN_UNDEF and
15853 leave everything up to the linker. */
3994f87e 15854 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15855 fixP->fx_done = 1;
15856
15857 switch (fixP->fx_r_type)
15858 {
3f98094e
DJ
15859 case BFD_RELOC_MIPS_TLS_GD:
15860 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15861 case BFD_RELOC_MIPS_TLS_DTPREL32:
15862 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15863 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15864 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15865 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15866 case BFD_RELOC_MIPS_TLS_TPREL32:
15867 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15868 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15869 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15870 case BFD_RELOC_MICROMIPS_TLS_GD:
15871 case BFD_RELOC_MICROMIPS_TLS_LDM:
15872 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15873 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15874 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15875 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15876 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15877 case BFD_RELOC_MIPS16_TLS_GD:
15878 case BFD_RELOC_MIPS16_TLS_LDM:
15879 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15880 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15881 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15882 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15883 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
b886a2ab
RS
15884 if (!fixP->fx_addsy)
15885 {
15886 as_bad_where (fixP->fx_file, fixP->fx_line,
15887 _("TLS relocation against a constant"));
15888 break;
15889 }
3f98094e
DJ
15890 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15891 /* fall through */
15892
252b5132 15893 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
15894 case BFD_RELOC_MIPS_SHIFT5:
15895 case BFD_RELOC_MIPS_SHIFT6:
15896 case BFD_RELOC_MIPS_GOT_DISP:
15897 case BFD_RELOC_MIPS_GOT_PAGE:
15898 case BFD_RELOC_MIPS_GOT_OFST:
15899 case BFD_RELOC_MIPS_SUB:
15900 case BFD_RELOC_MIPS_INSERT_A:
15901 case BFD_RELOC_MIPS_INSERT_B:
15902 case BFD_RELOC_MIPS_DELETE:
15903 case BFD_RELOC_MIPS_HIGHEST:
15904 case BFD_RELOC_MIPS_HIGHER:
15905 case BFD_RELOC_MIPS_SCN_DISP:
15906 case BFD_RELOC_MIPS_REL16:
15907 case BFD_RELOC_MIPS_RELGOT:
15908 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15909 case BFD_RELOC_HI16:
15910 case BFD_RELOC_HI16_S:
b886a2ab 15911 case BFD_RELOC_LO16:
cdf6fd85 15912 case BFD_RELOC_GPREL16:
252b5132
RH
15913 case BFD_RELOC_MIPS_LITERAL:
15914 case BFD_RELOC_MIPS_CALL16:
15915 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15916 case BFD_RELOC_GPREL32:
252b5132
RH
15917 case BFD_RELOC_MIPS_GOT_HI16:
15918 case BFD_RELOC_MIPS_GOT_LO16:
15919 case BFD_RELOC_MIPS_CALL_HI16:
15920 case BFD_RELOC_MIPS_CALL_LO16:
15921 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15922 case BFD_RELOC_MIPS16_GOT16:
15923 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15924 case BFD_RELOC_MIPS16_HI16:
15925 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15926 case BFD_RELOC_MIPS16_LO16:
252b5132 15927 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
15928 case BFD_RELOC_MICROMIPS_JMP:
15929 case BFD_RELOC_MICROMIPS_GOT_DISP:
15930 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15931 case BFD_RELOC_MICROMIPS_GOT_OFST:
15932 case BFD_RELOC_MICROMIPS_SUB:
15933 case BFD_RELOC_MICROMIPS_HIGHEST:
15934 case BFD_RELOC_MICROMIPS_HIGHER:
15935 case BFD_RELOC_MICROMIPS_SCN_DISP:
15936 case BFD_RELOC_MICROMIPS_JALR:
15937 case BFD_RELOC_MICROMIPS_HI16:
15938 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15939 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15940 case BFD_RELOC_MICROMIPS_GPREL16:
15941 case BFD_RELOC_MICROMIPS_LITERAL:
15942 case BFD_RELOC_MICROMIPS_CALL16:
15943 case BFD_RELOC_MICROMIPS_GOT16:
15944 case BFD_RELOC_MICROMIPS_GOT_HI16:
15945 case BFD_RELOC_MICROMIPS_GOT_LO16:
15946 case BFD_RELOC_MICROMIPS_CALL_HI16:
15947 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15948 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15949 if (fixP->fx_done)
15950 {
15951 offsetT value;
15952
15953 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15954 {
15955 insn = read_reloc_insn (buf, fixP->fx_r_type);
15956 if (mips16_reloc_p (fixP->fx_r_type))
15957 insn |= mips16_immed_extend (value, 16);
15958 else
15959 insn |= (value & 0xffff);
15960 write_reloc_insn (buf, fixP->fx_r_type, insn);
15961 }
15962 else
15963 as_bad_where (fixP->fx_file, fixP->fx_line,
15964 _("Unsupported constant in relocation"));
15965 }
252b5132
RH
15966 break;
15967
252b5132
RH
15968 case BFD_RELOC_64:
15969 /* This is handled like BFD_RELOC_32, but we output a sign
15970 extended value if we are only 32 bits. */
3e722fb5 15971 if (fixP->fx_done)
252b5132
RH
15972 {
15973 if (8 <= sizeof (valueT))
4d68580a 15974 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15975 else
15976 {
a7ebbfdf 15977 valueT hiv;
252b5132 15978
a7ebbfdf 15979 if ((*valP & 0x80000000) != 0)
252b5132
RH
15980 hiv = 0xffffffff;
15981 else
15982 hiv = 0;
4d68580a
RS
15983 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15984 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15985 }
15986 }
15987 break;
15988
056350c6 15989 case BFD_RELOC_RVA:
252b5132 15990 case BFD_RELOC_32:
b47468a6 15991 case BFD_RELOC_32_PCREL:
252b5132
RH
15992 case BFD_RELOC_16:
15993 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15994 value now. This can happen if we have a .word which is not
15995 resolved when it appears but is later defined. */
252b5132 15996 if (fixP->fx_done)
4d68580a 15997 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15998 break;
15999
252b5132 16000 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 16001 if ((*valP & 0x3) != 0)
cb56d3d3 16002 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 16003 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 16004
54f4ddb3
TS
16005 /* We need to save the bits in the instruction since fixup_segment()
16006 might be deleting the relocation entry (i.e., a branch within
16007 the current segment). */
a7ebbfdf 16008 if (! fixP->fx_done)
bb2d6cd7 16009 break;
252b5132 16010
54f4ddb3 16011 /* Update old instruction data. */
4d68580a 16012 insn = read_insn (buf);
252b5132 16013
a7ebbfdf
TS
16014 if (*valP + 0x20000 <= 0x3ffff)
16015 {
16016 insn |= (*valP >> 2) & 0xffff;
4d68580a 16017 write_insn (buf, insn);
a7ebbfdf
TS
16018 }
16019 else if (mips_pic == NO_PIC
16020 && fixP->fx_done
16021 && fixP->fx_frag->fr_address >= text_section->vma
16022 && (fixP->fx_frag->fr_address
587aac4e 16023 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
16024 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
16025 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
16026 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
16027 {
16028 /* The branch offset is too large. If this is an
16029 unconditional branch, and we are not generating PIC code,
16030 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
16031 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
16032 insn = 0x0c000000; /* jal */
252b5132 16033 else
a7ebbfdf
TS
16034 insn = 0x08000000; /* j */
16035 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
16036 fixP->fx_done = 0;
16037 fixP->fx_addsy = section_symbol (text_section);
16038 *valP += md_pcrel_from (fixP);
4d68580a 16039 write_insn (buf, insn);
a7ebbfdf
TS
16040 }
16041 else
16042 {
16043 /* If we got here, we have branch-relaxation disabled,
16044 and there's nothing we can do to fix this instruction
16045 without turning it into a longer sequence. */
16046 as_bad_where (fixP->fx_file, fixP->fx_line,
16047 _("Branch out of range"));
252b5132 16048 }
252b5132
RH
16049 break;
16050
df58fc94
RS
16051 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
16052 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
16053 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
16054 /* We adjust the offset back to even. */
16055 if ((*valP & 0x1) != 0)
16056 --(*valP);
16057
16058 if (! fixP->fx_done)
16059 break;
16060
16061 /* Should never visit here, because we keep the relocation. */
16062 abort ();
16063 break;
16064
252b5132
RH
16065 case BFD_RELOC_VTABLE_INHERIT:
16066 fixP->fx_done = 0;
16067 if (fixP->fx_addsy
16068 && !S_IS_DEFINED (fixP->fx_addsy)
16069 && !S_IS_WEAK (fixP->fx_addsy))
16070 S_SET_WEAK (fixP->fx_addsy);
16071 break;
16072
16073 case BFD_RELOC_VTABLE_ENTRY:
16074 fixP->fx_done = 0;
16075 break;
16076
16077 default:
b37df7c4 16078 abort ();
252b5132 16079 }
a7ebbfdf
TS
16080
16081 /* Remember value for tc_gen_reloc. */
16082 fixP->fx_addnumber = *valP;
252b5132
RH
16083}
16084
252b5132 16085static symbolS *
17a2f251 16086get_symbol (void)
252b5132
RH
16087{
16088 int c;
16089 char *name;
16090 symbolS *p;
16091
16092 name = input_line_pointer;
16093 c = get_symbol_end ();
16094 p = (symbolS *) symbol_find_or_make (name);
16095 *input_line_pointer = c;
16096 return p;
16097}
16098
742a56fe
RS
16099/* Align the current frag to a given power of two. If a particular
16100 fill byte should be used, FILL points to an integer that contains
16101 that byte, otherwise FILL is null.
16102
462427c4
RS
16103 This function used to have the comment:
16104
16105 The MIPS assembler also automatically adjusts any preceding label.
16106
16107 The implementation therefore applied the adjustment to a maximum of
16108 one label. However, other label adjustments are applied to batches
16109 of labels, and adjusting just one caused problems when new labels
16110 were added for the sake of debugging or unwind information.
16111 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
16112
16113static void
462427c4 16114mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 16115{
7d10b47d 16116 mips_emit_delays ();
df58fc94 16117 mips_record_compressed_mode ();
742a56fe
RS
16118 if (fill == NULL && subseg_text_p (now_seg))
16119 frag_align_code (to, 0);
16120 else
16121 frag_align (to, fill ? *fill : 0, 0);
252b5132 16122 record_alignment (now_seg, to);
462427c4 16123 mips_move_labels (labels, FALSE);
252b5132
RH
16124}
16125
16126/* Align to a given power of two. .align 0 turns off the automatic
16127 alignment used by the data creating pseudo-ops. */
16128
16129static void
17a2f251 16130s_align (int x ATTRIBUTE_UNUSED)
252b5132 16131{
742a56fe 16132 int temp, fill_value, *fill_ptr;
49954fb4 16133 long max_alignment = 28;
252b5132 16134
54f4ddb3 16135 /* o Note that the assembler pulls down any immediately preceding label
252b5132 16136 to the aligned address.
54f4ddb3 16137 o It's not documented but auto alignment is reinstated by
252b5132 16138 a .align pseudo instruction.
54f4ddb3 16139 o Note also that after auto alignment is turned off the mips assembler
252b5132 16140 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 16141 We don't. */
252b5132
RH
16142
16143 temp = get_absolute_expression ();
16144 if (temp > max_alignment)
16145 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
16146 else if (temp < 0)
16147 {
16148 as_warn (_("Alignment negative: 0 assumed."));
16149 temp = 0;
16150 }
16151 if (*input_line_pointer == ',')
16152 {
f9419b05 16153 ++input_line_pointer;
742a56fe
RS
16154 fill_value = get_absolute_expression ();
16155 fill_ptr = &fill_value;
252b5132
RH
16156 }
16157 else
742a56fe 16158 fill_ptr = 0;
252b5132
RH
16159 if (temp)
16160 {
a8dbcb85
TS
16161 segment_info_type *si = seg_info (now_seg);
16162 struct insn_label_list *l = si->label_list;
54f4ddb3 16163 /* Auto alignment should be switched on by next section change. */
252b5132 16164 auto_align = 1;
462427c4 16165 mips_align (temp, fill_ptr, l);
252b5132
RH
16166 }
16167 else
16168 {
16169 auto_align = 0;
16170 }
16171
16172 demand_empty_rest_of_line ();
16173}
16174
252b5132 16175static void
17a2f251 16176s_change_sec (int sec)
252b5132
RH
16177{
16178 segT seg;
16179
252b5132
RH
16180#ifdef OBJ_ELF
16181 /* The ELF backend needs to know that we are changing sections, so
16182 that .previous works correctly. We could do something like check
b6ff326e 16183 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
16184 as it would not be appropriate to use it in the section changing
16185 functions in read.c, since obj-elf.c intercepts those. FIXME:
16186 This should be cleaner, somehow. */
f43abd2b
TS
16187 if (IS_ELF)
16188 obj_elf_section_change_hook ();
252b5132
RH
16189#endif
16190
7d10b47d 16191 mips_emit_delays ();
6a32d874 16192
252b5132
RH
16193 switch (sec)
16194 {
16195 case 't':
16196 s_text (0);
16197 break;
16198 case 'd':
16199 s_data (0);
16200 break;
16201 case 'b':
16202 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16203 demand_empty_rest_of_line ();
16204 break;
16205
16206 case 'r':
4d0d148d
TS
16207 seg = subseg_new (RDATA_SECTION_NAME,
16208 (subsegT) get_absolute_expression ());
f43abd2b 16209 if (IS_ELF)
252b5132 16210 {
4d0d148d
TS
16211 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16212 | SEC_READONLY | SEC_RELOC
16213 | SEC_DATA));
c41e87e3 16214 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 16215 record_alignment (seg, 4);
252b5132 16216 }
4d0d148d 16217 demand_empty_rest_of_line ();
252b5132
RH
16218 break;
16219
16220 case 's':
4d0d148d 16221 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f43abd2b 16222 if (IS_ELF)
252b5132 16223 {
4d0d148d
TS
16224 bfd_set_section_flags (stdoutput, seg,
16225 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
c41e87e3 16226 if (strncmp (TARGET_OS, "elf", 3) != 0)
4d0d148d 16227 record_alignment (seg, 4);
252b5132 16228 }
4d0d148d
TS
16229 demand_empty_rest_of_line ();
16230 break;
998b3c36
MR
16231
16232 case 'B':
16233 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16234 if (IS_ELF)
16235 {
16236 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16237 if (strncmp (TARGET_OS, "elf", 3) != 0)
16238 record_alignment (seg, 4);
16239 }
16240 demand_empty_rest_of_line ();
16241 break;
252b5132
RH
16242 }
16243
16244 auto_align = 1;
16245}
b34976b6 16246
cca86cc8 16247void
17a2f251 16248s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16249{
7ed4a06a 16250#ifdef OBJ_ELF
cca86cc8
SC
16251 char *section_name;
16252 char c;
684022ea 16253 char next_c = 0;
cca86cc8
SC
16254 int section_type;
16255 int section_flag;
16256 int section_entry_size;
16257 int section_alignment;
b34976b6 16258
f43abd2b 16259 if (!IS_ELF)
7ed4a06a
TS
16260 return;
16261
cca86cc8
SC
16262 section_name = input_line_pointer;
16263 c = get_symbol_end ();
a816d1ed
AO
16264 if (c)
16265 next_c = *(input_line_pointer + 1);
cca86cc8 16266
4cf0dd0d
TS
16267 /* Do we have .section Name<,"flags">? */
16268 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16269 {
4cf0dd0d
TS
16270 /* just after name is now '\0'. */
16271 *input_line_pointer = c;
cca86cc8
SC
16272 input_line_pointer = section_name;
16273 obj_elf_section (ignore);
16274 return;
16275 }
16276 input_line_pointer++;
16277
16278 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16279 if (c == ',')
16280 section_type = get_absolute_expression ();
16281 else
16282 section_type = 0;
16283 if (*input_line_pointer++ == ',')
16284 section_flag = get_absolute_expression ();
16285 else
16286 section_flag = 0;
16287 if (*input_line_pointer++ == ',')
16288 section_entry_size = get_absolute_expression ();
16289 else
16290 section_entry_size = 0;
16291 if (*input_line_pointer++ == ',')
16292 section_alignment = get_absolute_expression ();
16293 else
16294 section_alignment = 0;
87975d2a
AM
16295 /* FIXME: really ignore? */
16296 (void) section_alignment;
cca86cc8 16297
a816d1ed
AO
16298 section_name = xstrdup (section_name);
16299
8ab8a5c8
RS
16300 /* When using the generic form of .section (as implemented by obj-elf.c),
16301 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16302 traditionally had to fall back on the more common @progbits instead.
16303
16304 There's nothing really harmful in this, since bfd will correct
16305 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16306 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16307 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16308
16309 Even so, we shouldn't force users of the MIPS .section syntax to
16310 incorrectly label the sections as SHT_PROGBITS. The best compromise
16311 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16312 generic type-checking code. */
16313 if (section_type == SHT_MIPS_DWARF)
16314 section_type = SHT_PROGBITS;
16315
cca86cc8
SC
16316 obj_elf_change_section (section_name, section_type, section_flag,
16317 section_entry_size, 0, 0, 0);
a816d1ed
AO
16318
16319 if (now_seg->name != section_name)
16320 free (section_name);
7ed4a06a 16321#endif /* OBJ_ELF */
cca86cc8 16322}
252b5132
RH
16323
16324void
17a2f251 16325mips_enable_auto_align (void)
252b5132
RH
16326{
16327 auto_align = 1;
16328}
16329
16330static void
17a2f251 16331s_cons (int log_size)
252b5132 16332{
a8dbcb85
TS
16333 segment_info_type *si = seg_info (now_seg);
16334 struct insn_label_list *l = si->label_list;
252b5132 16335
7d10b47d 16336 mips_emit_delays ();
252b5132 16337 if (log_size > 0 && auto_align)
462427c4 16338 mips_align (log_size, 0, l);
252b5132 16339 cons (1 << log_size);
a1facbec 16340 mips_clear_insn_labels ();
252b5132
RH
16341}
16342
16343static void
17a2f251 16344s_float_cons (int type)
252b5132 16345{
a8dbcb85
TS
16346 segment_info_type *si = seg_info (now_seg);
16347 struct insn_label_list *l = si->label_list;
252b5132 16348
7d10b47d 16349 mips_emit_delays ();
252b5132
RH
16350
16351 if (auto_align)
49309057
ILT
16352 {
16353 if (type == 'd')
462427c4 16354 mips_align (3, 0, l);
49309057 16355 else
462427c4 16356 mips_align (2, 0, l);
49309057 16357 }
252b5132 16358
252b5132 16359 float_cons (type);
a1facbec 16360 mips_clear_insn_labels ();
252b5132
RH
16361}
16362
16363/* Handle .globl. We need to override it because on Irix 5 you are
16364 permitted to say
16365 .globl foo .text
16366 where foo is an undefined symbol, to mean that foo should be
16367 considered to be the address of a function. */
16368
16369static void
17a2f251 16370s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16371{
16372 char *name;
16373 int c;
16374 symbolS *symbolP;
16375 flagword flag;
16376
8a06b769 16377 do
252b5132 16378 {
8a06b769 16379 name = input_line_pointer;
252b5132 16380 c = get_symbol_end ();
8a06b769
TS
16381 symbolP = symbol_find_or_make (name);
16382 S_SET_EXTERNAL (symbolP);
16383
252b5132 16384 *input_line_pointer = c;
8a06b769 16385 SKIP_WHITESPACE ();
252b5132 16386
8a06b769
TS
16387 /* On Irix 5, every global symbol that is not explicitly labelled as
16388 being a function is apparently labelled as being an object. */
16389 flag = BSF_OBJECT;
252b5132 16390
8a06b769
TS
16391 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16392 && (*input_line_pointer != ','))
16393 {
16394 char *secname;
16395 asection *sec;
16396
16397 secname = input_line_pointer;
16398 c = get_symbol_end ();
16399 sec = bfd_get_section_by_name (stdoutput, secname);
16400 if (sec == NULL)
16401 as_bad (_("%s: no such section"), secname);
16402 *input_line_pointer = c;
16403
16404 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16405 flag = BSF_FUNCTION;
16406 }
16407
16408 symbol_get_bfdsym (symbolP)->flags |= flag;
16409
16410 c = *input_line_pointer;
16411 if (c == ',')
16412 {
16413 input_line_pointer++;
16414 SKIP_WHITESPACE ();
16415 if (is_end_of_line[(unsigned char) *input_line_pointer])
16416 c = '\n';
16417 }
16418 }
16419 while (c == ',');
252b5132 16420
252b5132
RH
16421 demand_empty_rest_of_line ();
16422}
16423
16424static void
17a2f251 16425s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16426{
16427 char *opt;
16428 char c;
16429
16430 opt = input_line_pointer;
16431 c = get_symbol_end ();
16432
16433 if (*opt == 'O')
16434 {
16435 /* FIXME: What does this mean? */
16436 }
16437 else if (strncmp (opt, "pic", 3) == 0)
16438 {
16439 int i;
16440
16441 i = atoi (opt + 3);
16442 if (i == 0)
16443 mips_pic = NO_PIC;
16444 else if (i == 2)
143d77c5 16445 {
8b828383 16446 mips_pic = SVR4_PIC;
143d77c5
EC
16447 mips_abicalls = TRUE;
16448 }
252b5132
RH
16449 else
16450 as_bad (_(".option pic%d not supported"), i);
16451
4d0d148d 16452 if (mips_pic == SVR4_PIC)
252b5132
RH
16453 {
16454 if (g_switch_seen && g_switch_value != 0)
16455 as_warn (_("-G may not be used with SVR4 PIC code"));
16456 g_switch_value = 0;
16457 bfd_set_gp_size (stdoutput, 0);
16458 }
16459 }
16460 else
16461 as_warn (_("Unrecognized option \"%s\""), opt);
16462
16463 *input_line_pointer = c;
16464 demand_empty_rest_of_line ();
16465}
16466
16467/* This structure is used to hold a stack of .set values. */
16468
e972090a
NC
16469struct mips_option_stack
16470{
252b5132
RH
16471 struct mips_option_stack *next;
16472 struct mips_set_options options;
16473};
16474
16475static struct mips_option_stack *mips_opts_stack;
16476
16477/* Handle the .set pseudo-op. */
16478
16479static void
17a2f251 16480s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
16481{
16482 char *name = input_line_pointer, ch;
16483
16484 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 16485 ++input_line_pointer;
252b5132
RH
16486 ch = *input_line_pointer;
16487 *input_line_pointer = '\0';
16488
16489 if (strcmp (name, "reorder") == 0)
16490 {
7d10b47d
RS
16491 if (mips_opts.noreorder)
16492 end_noreorder ();
252b5132
RH
16493 }
16494 else if (strcmp (name, "noreorder") == 0)
16495 {
7d10b47d
RS
16496 if (!mips_opts.noreorder)
16497 start_noreorder ();
252b5132 16498 }
741fe287
MR
16499 else if (strncmp (name, "at=", 3) == 0)
16500 {
16501 char *s = name + 3;
16502
16503 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16504 as_bad (_("Unrecognized register name `%s'"), s);
16505 }
252b5132
RH
16506 else if (strcmp (name, "at") == 0)
16507 {
741fe287 16508 mips_opts.at = ATREG;
252b5132
RH
16509 }
16510 else if (strcmp (name, "noat") == 0)
16511 {
741fe287 16512 mips_opts.at = ZERO;
252b5132
RH
16513 }
16514 else if (strcmp (name, "macro") == 0)
16515 {
16516 mips_opts.warn_about_macros = 0;
16517 }
16518 else if (strcmp (name, "nomacro") == 0)
16519 {
16520 if (mips_opts.noreorder == 0)
16521 as_bad (_("`noreorder' must be set before `nomacro'"));
16522 mips_opts.warn_about_macros = 1;
16523 }
16524 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16525 {
16526 mips_opts.nomove = 0;
16527 }
16528 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16529 {
16530 mips_opts.nomove = 1;
16531 }
16532 else if (strcmp (name, "bopt") == 0)
16533 {
16534 mips_opts.nobopt = 0;
16535 }
16536 else if (strcmp (name, "nobopt") == 0)
16537 {
16538 mips_opts.nobopt = 1;
16539 }
ad3fea08
TS
16540 else if (strcmp (name, "gp=default") == 0)
16541 mips_opts.gp32 = file_mips_gp32;
16542 else if (strcmp (name, "gp=32") == 0)
16543 mips_opts.gp32 = 1;
16544 else if (strcmp (name, "gp=64") == 0)
16545 {
16546 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 16547 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08
TS
16548 mips_cpu_info_from_isa (mips_opts.isa)->name);
16549 mips_opts.gp32 = 0;
16550 }
16551 else if (strcmp (name, "fp=default") == 0)
16552 mips_opts.fp32 = file_mips_fp32;
16553 else if (strcmp (name, "fp=32") == 0)
16554 mips_opts.fp32 = 1;
16555 else if (strcmp (name, "fp=64") == 0)
16556 {
16557 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 16558 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08
TS
16559 mips_cpu_info_from_isa (mips_opts.isa)->name);
16560 mips_opts.fp32 = 0;
16561 }
037b32b9
AN
16562 else if (strcmp (name, "softfloat") == 0)
16563 mips_opts.soft_float = 1;
16564 else if (strcmp (name, "hardfloat") == 0)
16565 mips_opts.soft_float = 0;
16566 else if (strcmp (name, "singlefloat") == 0)
16567 mips_opts.single_float = 1;
16568 else if (strcmp (name, "doublefloat") == 0)
16569 mips_opts.single_float = 0;
252b5132
RH
16570 else if (strcmp (name, "mips16") == 0
16571 || strcmp (name, "MIPS-16") == 0)
df58fc94
RS
16572 {
16573 if (mips_opts.micromips == 1)
16574 as_fatal (_("`mips16' cannot be used with `micromips'"));
16575 mips_opts.mips16 = 1;
16576 }
252b5132
RH
16577 else if (strcmp (name, "nomips16") == 0
16578 || strcmp (name, "noMIPS-16") == 0)
16579 mips_opts.mips16 = 0;
df58fc94
RS
16580 else if (strcmp (name, "micromips") == 0)
16581 {
16582 if (mips_opts.mips16 == 1)
16583 as_fatal (_("`micromips' cannot be used with `mips16'"));
16584 mips_opts.micromips = 1;
16585 }
16586 else if (strcmp (name, "nomicromips") == 0)
16587 mips_opts.micromips = 0;
e16bfa71
TS
16588 else if (strcmp (name, "smartmips") == 0)
16589 {
ad3fea08 16590 if (!ISA_SUPPORTS_SMARTMIPS)
20203fb9 16591 as_warn (_("%s ISA does not support SmartMIPS ASE"),
e16bfa71 16592 mips_cpu_info_from_isa (mips_opts.isa)->name);
846ef2d0 16593 mips_opts.ase |= ASE_SMARTMIPS;
e16bfa71
TS
16594 }
16595 else if (strcmp (name, "nosmartmips") == 0)
846ef2d0 16596 mips_opts.ase &= ~ASE_SMARTMIPS;
1f25f5d3 16597 else if (strcmp (name, "mips3d") == 0)
846ef2d0 16598 mips_opts.ase |= ASE_MIPS3D;
1f25f5d3 16599 else if (strcmp (name, "nomips3d") == 0)
846ef2d0 16600 mips_opts.ase &= ~ASE_MIPS3D;
a4672219 16601 else if (strcmp (name, "mdmx") == 0)
846ef2d0 16602 mips_opts.ase |= ASE_MDMX;
a4672219 16603 else if (strcmp (name, "nomdmx") == 0)
846ef2d0 16604 mips_opts.ase &= ~ASE_MDMX;
74cd071d 16605 else if (strcmp (name, "dsp") == 0)
ad3fea08
TS
16606 {
16607 if (!ISA_SUPPORTS_DSP_ASE)
20203fb9 16608 as_warn (_("%s ISA does not support DSP ASE"),
ad3fea08 16609 mips_cpu_info_from_isa (mips_opts.isa)->name);
846ef2d0
RS
16610 mips_opts.ase |= ASE_DSP;
16611 mips_opts.ase &= ~ASE_DSPR2;
8b082fb1
TS
16612 }
16613 else if (strcmp (name, "dspr2") == 0)
16614 {
16615 if (!ISA_SUPPORTS_DSPR2_ASE)
20203fb9 16616 as_warn (_("%s ISA does not support DSP R2 ASE"),
8b082fb1 16617 mips_cpu_info_from_isa (mips_opts.isa)->name);
846ef2d0 16618 mips_opts.ase |= ASE_DSP | ASE_DSPR2;
8b082fb1 16619 }
846ef2d0
RS
16620 else if (strcmp (name, "nodsp") == 0
16621 || strcmp (name, "nodspr2") == 0)
16622 mips_opts.ase &= ~(ASE_DSP | ASE_DSPR2);
7f3c4072
CM
16623 else if (strcmp (name, "eva") == 0)
16624 {
16625 if (!ISA_SUPPORTS_EVA_ASE)
16626 as_warn (_("%s ISA does not support EVA ASE"),
16627 mips_cpu_info_from_isa (mips_opts.isa)->name);
846ef2d0 16628 mips_opts.ase |= ASE_EVA;
7f3c4072
CM
16629 }
16630 else if (strcmp (name, "noeva") == 0)
846ef2d0 16631 mips_opts.ase &= ~ASE_EVA;
ef2e4d86 16632 else if (strcmp (name, "mt") == 0)
ad3fea08
TS
16633 {
16634 if (!ISA_SUPPORTS_MT_ASE)
20203fb9 16635 as_warn (_("%s ISA does not support MT ASE"),
ad3fea08 16636 mips_cpu_info_from_isa (mips_opts.isa)->name);
846ef2d0 16637 mips_opts.ase |= ASE_MT;
ad3fea08 16638 }
ef2e4d86 16639 else if (strcmp (name, "nomt") == 0)
846ef2d0 16640 mips_opts.ase &= ~ASE_MT;
dec0624d 16641 else if (strcmp (name, "mcu") == 0)
846ef2d0 16642 mips_opts.ase |= ASE_MCU;
dec0624d 16643 else if (strcmp (name, "nomcu") == 0)
846ef2d0 16644 mips_opts.ase &= ~ASE_MCU;
b015e599
AP
16645 else if (strcmp (name, "virt") == 0)
16646 {
16647 if (!ISA_SUPPORTS_VIRT_ASE)
16648 as_warn (_("%s ISA does not support Virtualization ASE"),
16649 mips_cpu_info_from_isa (mips_opts.isa)->name);
846ef2d0 16650 mips_opts.ase |= ASE_VIRT;
b015e599
AP
16651 }
16652 else if (strcmp (name, "novirt") == 0)
846ef2d0 16653 mips_opts.ase &= ~ASE_VIRT;
1a2c1fad 16654 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16655 {
af7ee8bf 16656 int reset = 0;
252b5132 16657
1a2c1fad
CD
16658 /* Permit the user to change the ISA and architecture on the fly.
16659 Needless to say, misuse can cause serious problems. */
81a21e38 16660 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
16661 {
16662 reset = 1;
16663 mips_opts.isa = file_mips_isa;
1a2c1fad 16664 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
16665 }
16666 else if (strncmp (name, "arch=", 5) == 0)
16667 {
16668 const struct mips_cpu_info *p;
16669
16670 p = mips_parse_cpu("internal use", name + 5);
16671 if (!p)
16672 as_bad (_("unknown architecture %s"), name + 5);
16673 else
16674 {
16675 mips_opts.arch = p->cpu;
16676 mips_opts.isa = p->isa;
16677 }
16678 }
81a21e38
TS
16679 else if (strncmp (name, "mips", 4) == 0)
16680 {
16681 const struct mips_cpu_info *p;
16682
16683 p = mips_parse_cpu("internal use", name);
16684 if (!p)
16685 as_bad (_("unknown ISA level %s"), name + 4);
16686 else
16687 {
16688 mips_opts.arch = p->cpu;
16689 mips_opts.isa = p->isa;
16690 }
16691 }
af7ee8bf 16692 else
81a21e38 16693 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
16694
16695 switch (mips_opts.isa)
98d3f06f
KH
16696 {
16697 case 0:
98d3f06f 16698 break;
af7ee8bf
CD
16699 case ISA_MIPS1:
16700 case ISA_MIPS2:
16701 case ISA_MIPS32:
16702 case ISA_MIPS32R2:
98d3f06f
KH
16703 mips_opts.gp32 = 1;
16704 mips_opts.fp32 = 1;
16705 break;
af7ee8bf
CD
16706 case ISA_MIPS3:
16707 case ISA_MIPS4:
16708 case ISA_MIPS5:
16709 case ISA_MIPS64:
5f74bc13 16710 case ISA_MIPS64R2:
98d3f06f 16711 mips_opts.gp32 = 0;
e407c74b
NC
16712 if (mips_opts.arch == CPU_R5900)
16713 {
16714 mips_opts.fp32 = 1;
16715 }
16716 else
16717 {
98d3f06f 16718 mips_opts.fp32 = 0;
e407c74b 16719 }
98d3f06f
KH
16720 break;
16721 default:
16722 as_bad (_("unknown ISA level %s"), name + 4);
16723 break;
16724 }
af7ee8bf 16725 if (reset)
98d3f06f 16726 {
af7ee8bf
CD
16727 mips_opts.gp32 = file_mips_gp32;
16728 mips_opts.fp32 = file_mips_fp32;
98d3f06f 16729 }
252b5132
RH
16730 }
16731 else if (strcmp (name, "autoextend") == 0)
16732 mips_opts.noautoextend = 0;
16733 else if (strcmp (name, "noautoextend") == 0)
16734 mips_opts.noautoextend = 1;
16735 else if (strcmp (name, "push") == 0)
16736 {
16737 struct mips_option_stack *s;
16738
16739 s = (struct mips_option_stack *) xmalloc (sizeof *s);
16740 s->next = mips_opts_stack;
16741 s->options = mips_opts;
16742 mips_opts_stack = s;
16743 }
16744 else if (strcmp (name, "pop") == 0)
16745 {
16746 struct mips_option_stack *s;
16747
16748 s = mips_opts_stack;
16749 if (s == NULL)
16750 as_bad (_(".set pop with no .set push"));
16751 else
16752 {
16753 /* If we're changing the reorder mode we need to handle
16754 delay slots correctly. */
16755 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16756 start_noreorder ();
252b5132 16757 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16758 end_noreorder ();
252b5132
RH
16759
16760 mips_opts = s->options;
16761 mips_opts_stack = s->next;
16762 free (s);
16763 }
16764 }
aed1a261
RS
16765 else if (strcmp (name, "sym32") == 0)
16766 mips_opts.sym32 = TRUE;
16767 else if (strcmp (name, "nosym32") == 0)
16768 mips_opts.sym32 = FALSE;
e6559e01
JM
16769 else if (strchr (name, ','))
16770 {
16771 /* Generic ".set" directive; use the generic handler. */
16772 *input_line_pointer = ch;
16773 input_line_pointer = name;
16774 s_set (0);
16775 return;
16776 }
252b5132
RH
16777 else
16778 {
16779 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16780 }
16781 *input_line_pointer = ch;
16782 demand_empty_rest_of_line ();
16783}
16784
16785/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16786 .option pic2. It means to generate SVR4 PIC calls. */
16787
16788static void
17a2f251 16789s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16790{
16791 mips_pic = SVR4_PIC;
143d77c5 16792 mips_abicalls = TRUE;
4d0d148d
TS
16793
16794 if (g_switch_seen && g_switch_value != 0)
16795 as_warn (_("-G may not be used with SVR4 PIC code"));
16796 g_switch_value = 0;
16797
252b5132
RH
16798 bfd_set_gp_size (stdoutput, 0);
16799 demand_empty_rest_of_line ();
16800}
16801
16802/* Handle the .cpload pseudo-op. This is used when generating SVR4
16803 PIC code. It sets the $gp register for the function based on the
16804 function address, which is in the register named in the argument.
16805 This uses a relocation against _gp_disp, which is handled specially
16806 by the linker. The result is:
16807 lui $gp,%hi(_gp_disp)
16808 addiu $gp,$gp,%lo(_gp_disp)
16809 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16810 The .cpload argument is normally $25 == $t9.
16811
16812 The -mno-shared option changes this to:
bbe506e8
TS
16813 lui $gp,%hi(__gnu_local_gp)
16814 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16815 and the argument is ignored. This saves an instruction, but the
16816 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16817 address for __gnu_local_gp. Thus code assembled with -mno-shared
16818 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16819
16820static void
17a2f251 16821s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16822{
16823 expressionS ex;
aa6975fb
ILT
16824 int reg;
16825 int in_shared;
252b5132 16826
6478892d
TS
16827 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16828 .cpload is ignored. */
16829 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16830 {
16831 s_ignore (0);
16832 return;
16833 }
16834
a276b80c
MR
16835 if (mips_opts.mips16)
16836 {
16837 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16838 ignore_rest_of_line ();
16839 return;
16840 }
16841
d3ecfc59 16842 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16843 if (mips_opts.noreorder == 0)
16844 as_warn (_(".cpload not in noreorder section"));
16845
aa6975fb
ILT
16846 reg = tc_get_register (0);
16847
16848 /* If we need to produce a 64-bit address, we are better off using
16849 the default instruction sequence. */
aed1a261 16850 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16851
252b5132 16852 ex.X_op = O_symbol;
bbe506e8
TS
16853 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16854 "__gnu_local_gp");
252b5132
RH
16855 ex.X_op_symbol = NULL;
16856 ex.X_add_number = 0;
16857
16858 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16859 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16860
8a75745d
MR
16861 mips_mark_labels ();
16862 mips_assembling_insn = TRUE;
16863
584892a6 16864 macro_start ();
67c0d1eb
RS
16865 macro_build_lui (&ex, mips_gp_register);
16866 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16867 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16868 if (in_shared)
16869 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16870 mips_gp_register, reg);
584892a6 16871 macro_end ();
252b5132 16872
8a75745d 16873 mips_assembling_insn = FALSE;
252b5132
RH
16874 demand_empty_rest_of_line ();
16875}
16876
6478892d
TS
16877/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16878 .cpsetup $reg1, offset|$reg2, label
16879
16880 If offset is given, this results in:
16881 sd $gp, offset($sp)
956cd1d6 16882 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16883 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16884 daddu $gp, $gp, $reg1
6478892d
TS
16885
16886 If $reg2 is given, this results in:
16887 daddu $reg2, $gp, $0
956cd1d6 16888 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16889 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16890 daddu $gp, $gp, $reg1
aa6975fb
ILT
16891 $reg1 is normally $25 == $t9.
16892
16893 The -mno-shared option replaces the last three instructions with
16894 lui $gp,%hi(_gp)
54f4ddb3 16895 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16896
6478892d 16897static void
17a2f251 16898s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16899{
16900 expressionS ex_off;
16901 expressionS ex_sym;
16902 int reg1;
6478892d 16903
8586fc66 16904 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16905 We also need NewABI support. */
16906 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16907 {
16908 s_ignore (0);
16909 return;
16910 }
16911
a276b80c
MR
16912 if (mips_opts.mips16)
16913 {
16914 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16915 ignore_rest_of_line ();
16916 return;
16917 }
16918
6478892d
TS
16919 reg1 = tc_get_register (0);
16920 SKIP_WHITESPACE ();
16921 if (*input_line_pointer != ',')
16922 {
16923 as_bad (_("missing argument separator ',' for .cpsetup"));
16924 return;
16925 }
16926 else
80245285 16927 ++input_line_pointer;
6478892d
TS
16928 SKIP_WHITESPACE ();
16929 if (*input_line_pointer == '$')
80245285
TS
16930 {
16931 mips_cpreturn_register = tc_get_register (0);
16932 mips_cpreturn_offset = -1;
16933 }
6478892d 16934 else
80245285
TS
16935 {
16936 mips_cpreturn_offset = get_absolute_expression ();
16937 mips_cpreturn_register = -1;
16938 }
6478892d
TS
16939 SKIP_WHITESPACE ();
16940 if (*input_line_pointer != ',')
16941 {
16942 as_bad (_("missing argument separator ',' for .cpsetup"));
16943 return;
16944 }
16945 else
f9419b05 16946 ++input_line_pointer;
6478892d 16947 SKIP_WHITESPACE ();
f21f8242 16948 expression (&ex_sym);
6478892d 16949
8a75745d
MR
16950 mips_mark_labels ();
16951 mips_assembling_insn = TRUE;
16952
584892a6 16953 macro_start ();
6478892d
TS
16954 if (mips_cpreturn_register == -1)
16955 {
16956 ex_off.X_op = O_constant;
16957 ex_off.X_add_symbol = NULL;
16958 ex_off.X_op_symbol = NULL;
16959 ex_off.X_add_number = mips_cpreturn_offset;
16960
67c0d1eb 16961 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16962 BFD_RELOC_LO16, SP);
6478892d
TS
16963 }
16964 else
67c0d1eb 16965 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 16966 mips_gp_register, 0);
6478892d 16967
aed1a261 16968 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16969 {
df58fc94 16970 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16971 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16972 BFD_RELOC_HI16_S);
16973
16974 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16975 mips_gp_register, -1, BFD_RELOC_GPREL16,
16976 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16977
16978 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16979 mips_gp_register, reg1);
16980 }
16981 else
16982 {
16983 expressionS ex;
16984
16985 ex.X_op = O_symbol;
4184909a 16986 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16987 ex.X_op_symbol = NULL;
16988 ex.X_add_number = 0;
6e1304d8 16989
aa6975fb
ILT
16990 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16991 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16992
16993 macro_build_lui (&ex, mips_gp_register);
16994 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16995 mips_gp_register, BFD_RELOC_LO16);
16996 }
f21f8242 16997
584892a6 16998 macro_end ();
6478892d 16999
8a75745d 17000 mips_assembling_insn = FALSE;
6478892d
TS
17001 demand_empty_rest_of_line ();
17002}
17003
17004static void
17a2f251 17005s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17006{
17007 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 17008 .cplocal is ignored. */
6478892d
TS
17009 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17010 {
17011 s_ignore (0);
17012 return;
17013 }
17014
a276b80c
MR
17015 if (mips_opts.mips16)
17016 {
17017 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
17018 ignore_rest_of_line ();
17019 return;
17020 }
17021
6478892d 17022 mips_gp_register = tc_get_register (0);
85b51719 17023 demand_empty_rest_of_line ();
6478892d
TS
17024}
17025
252b5132
RH
17026/* Handle the .cprestore pseudo-op. This stores $gp into a given
17027 offset from $sp. The offset is remembered, and after making a PIC
17028 call $gp is restored from that location. */
17029
17030static void
17a2f251 17031s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17032{
17033 expressionS ex;
252b5132 17034
6478892d 17035 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 17036 .cprestore is ignored. */
6478892d 17037 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
17038 {
17039 s_ignore (0);
17040 return;
17041 }
17042
a276b80c
MR
17043 if (mips_opts.mips16)
17044 {
17045 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
17046 ignore_rest_of_line ();
17047 return;
17048 }
17049
252b5132 17050 mips_cprestore_offset = get_absolute_expression ();
7a621144 17051 mips_cprestore_valid = 1;
252b5132
RH
17052
17053 ex.X_op = O_constant;
17054 ex.X_add_symbol = NULL;
17055 ex.X_op_symbol = NULL;
17056 ex.X_add_number = mips_cprestore_offset;
17057
8a75745d
MR
17058 mips_mark_labels ();
17059 mips_assembling_insn = TRUE;
17060
584892a6 17061 macro_start ();
67c0d1eb
RS
17062 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
17063 SP, HAVE_64BIT_ADDRESSES);
584892a6 17064 macro_end ();
252b5132 17065
8a75745d 17066 mips_assembling_insn = FALSE;
252b5132
RH
17067 demand_empty_rest_of_line ();
17068}
17069
6478892d 17070/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 17071 was given in the preceding .cpsetup, it results in:
6478892d 17072 ld $gp, offset($sp)
76b3015f 17073
6478892d 17074 If a register $reg2 was given there, it results in:
54f4ddb3
TS
17075 daddu $gp, $reg2, $0 */
17076
6478892d 17077static void
17a2f251 17078s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17079{
17080 expressionS ex;
6478892d
TS
17081
17082 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
17083 We also need NewABI support. */
17084 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17085 {
17086 s_ignore (0);
17087 return;
17088 }
17089
a276b80c
MR
17090 if (mips_opts.mips16)
17091 {
17092 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
17093 ignore_rest_of_line ();
17094 return;
17095 }
17096
8a75745d
MR
17097 mips_mark_labels ();
17098 mips_assembling_insn = TRUE;
17099
584892a6 17100 macro_start ();
6478892d
TS
17101 if (mips_cpreturn_register == -1)
17102 {
17103 ex.X_op = O_constant;
17104 ex.X_add_symbol = NULL;
17105 ex.X_op_symbol = NULL;
17106 ex.X_add_number = mips_cpreturn_offset;
17107
67c0d1eb 17108 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
17109 }
17110 else
67c0d1eb 17111 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 17112 mips_cpreturn_register, 0);
584892a6 17113 macro_end ();
6478892d 17114
8a75745d 17115 mips_assembling_insn = FALSE;
6478892d
TS
17116 demand_empty_rest_of_line ();
17117}
17118
d0f13682
CLT
17119/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17120 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17121 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17122 debug information or MIPS16 TLS. */
741d6ea8
JM
17123
17124static void
d0f13682
CLT
17125s_tls_rel_directive (const size_t bytes, const char *dirstr,
17126 bfd_reloc_code_real_type rtype)
741d6ea8
JM
17127{
17128 expressionS ex;
17129 char *p;
17130
17131 expression (&ex);
17132
17133 if (ex.X_op != O_symbol)
17134 {
d0f13682 17135 as_bad (_("Unsupported use of %s"), dirstr);
741d6ea8
JM
17136 ignore_rest_of_line ();
17137 }
17138
17139 p = frag_more (bytes);
17140 md_number_to_chars (p, 0, bytes);
d0f13682 17141 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 17142 demand_empty_rest_of_line ();
de64cffd 17143 mips_clear_insn_labels ();
741d6ea8
JM
17144}
17145
17146/* Handle .dtprelword. */
17147
17148static void
17149s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17150{
d0f13682 17151 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
17152}
17153
17154/* Handle .dtpreldword. */
17155
17156static void
17157s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17158{
d0f13682
CLT
17159 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17160}
17161
17162/* Handle .tprelword. */
17163
17164static void
17165s_tprelword (int ignore ATTRIBUTE_UNUSED)
17166{
17167 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17168}
17169
17170/* Handle .tpreldword. */
17171
17172static void
17173s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17174{
17175 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
17176}
17177
6478892d
TS
17178/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17179 code. It sets the offset to use in gp_rel relocations. */
17180
17181static void
17a2f251 17182s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17183{
17184 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17185 We also need NewABI support. */
17186 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17187 {
17188 s_ignore (0);
17189 return;
17190 }
17191
def2e0dd 17192 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
17193
17194 demand_empty_rest_of_line ();
17195}
17196
252b5132
RH
17197/* Handle the .gpword pseudo-op. This is used when generating PIC
17198 code. It generates a 32 bit GP relative reloc. */
17199
17200static void
17a2f251 17201s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 17202{
a8dbcb85
TS
17203 segment_info_type *si;
17204 struct insn_label_list *l;
252b5132
RH
17205 expressionS ex;
17206 char *p;
17207
17208 /* When not generating PIC code, this is treated as .word. */
17209 if (mips_pic != SVR4_PIC)
17210 {
17211 s_cons (2);
17212 return;
17213 }
17214
a8dbcb85
TS
17215 si = seg_info (now_seg);
17216 l = si->label_list;
7d10b47d 17217 mips_emit_delays ();
252b5132 17218 if (auto_align)
462427c4 17219 mips_align (2, 0, l);
252b5132
RH
17220
17221 expression (&ex);
a1facbec 17222 mips_clear_insn_labels ();
252b5132
RH
17223
17224 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17225 {
17226 as_bad (_("Unsupported use of .gpword"));
17227 ignore_rest_of_line ();
17228 }
17229
17230 p = frag_more (4);
17a2f251 17231 md_number_to_chars (p, 0, 4);
b34976b6 17232 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 17233 BFD_RELOC_GPREL32);
252b5132
RH
17234
17235 demand_empty_rest_of_line ();
17236}
17237
10181a0d 17238static void
17a2f251 17239s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 17240{
a8dbcb85
TS
17241 segment_info_type *si;
17242 struct insn_label_list *l;
10181a0d
AO
17243 expressionS ex;
17244 char *p;
17245
17246 /* When not generating PIC code, this is treated as .dword. */
17247 if (mips_pic != SVR4_PIC)
17248 {
17249 s_cons (3);
17250 return;
17251 }
17252
a8dbcb85
TS
17253 si = seg_info (now_seg);
17254 l = si->label_list;
7d10b47d 17255 mips_emit_delays ();
10181a0d 17256 if (auto_align)
462427c4 17257 mips_align (3, 0, l);
10181a0d
AO
17258
17259 expression (&ex);
a1facbec 17260 mips_clear_insn_labels ();
10181a0d
AO
17261
17262 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17263 {
17264 as_bad (_("Unsupported use of .gpdword"));
17265 ignore_rest_of_line ();
17266 }
17267
17268 p = frag_more (8);
17a2f251 17269 md_number_to_chars (p, 0, 8);
a105a300 17270 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17271 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17272
17273 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17274 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17275 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17276
17277 demand_empty_rest_of_line ();
17278}
17279
a3f278e2
CM
17280/* Handle the .ehword pseudo-op. This is used when generating unwinding
17281 tables. It generates a R_MIPS_EH reloc. */
17282
17283static void
17284s_ehword (int ignore ATTRIBUTE_UNUSED)
17285{
17286 expressionS ex;
17287 char *p;
17288
17289 mips_emit_delays ();
17290
17291 expression (&ex);
17292 mips_clear_insn_labels ();
17293
17294 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17295 {
17296 as_bad (_("Unsupported use of .ehword"));
17297 ignore_rest_of_line ();
17298 }
17299
17300 p = frag_more (4);
17301 md_number_to_chars (p, 0, 4);
17302 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17303 BFD_RELOC_MIPS_EH);
17304
17305 demand_empty_rest_of_line ();
17306}
17307
252b5132
RH
17308/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17309 tables in SVR4 PIC code. */
17310
17311static void
17a2f251 17312s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17313{
252b5132
RH
17314 int reg;
17315
10181a0d
AO
17316 /* This is ignored when not generating SVR4 PIC code. */
17317 if (mips_pic != SVR4_PIC)
252b5132
RH
17318 {
17319 s_ignore (0);
17320 return;
17321 }
17322
8a75745d
MR
17323 mips_mark_labels ();
17324 mips_assembling_insn = TRUE;
17325
252b5132 17326 /* Add $gp to the register named as an argument. */
584892a6 17327 macro_start ();
252b5132 17328 reg = tc_get_register (0);
67c0d1eb 17329 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17330 macro_end ();
252b5132 17331
8a75745d 17332 mips_assembling_insn = FALSE;
bdaaa2e1 17333 demand_empty_rest_of_line ();
252b5132
RH
17334}
17335
17336/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17337 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17338 such as generating jalx instructions when needed. We also make
17339 them odd for the duration of the assembly, in order to generate the
17340 right sort of code. We will make them even in the adjust_symtab
17341 routine, while leaving them marked. This is convenient for the
17342 debugger and the disassembler. The linker knows to make them odd
17343 again. */
17344
17345static void
17a2f251 17346s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17347{
df58fc94 17348 mips_mark_labels ();
252b5132
RH
17349
17350 demand_empty_rest_of_line ();
17351}
17352
754e2bb9
RS
17353/* Handle a .stab[snd] directive. Ideally these directives would be
17354 implemented in a transparent way, so that removing them would not
17355 have any effect on the generated instructions. However, s_stab
17356 internally changes the section, so in practice we need to decide
17357 now whether the preceding label marks compressed code. We do not
17358 support changing the compression mode of a label after a .stab*
17359 directive, such as in:
17360
17361 foo:
17362 .stabs ...
17363 .set mips16
17364
17365 so the current mode wins. */
252b5132
RH
17366
17367static void
17a2f251 17368s_mips_stab (int type)
252b5132 17369{
754e2bb9 17370 mips_mark_labels ();
252b5132
RH
17371 s_stab (type);
17372}
17373
54f4ddb3 17374/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17375
17376static void
17a2f251 17377s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17378{
17379 char *name;
17380 int c;
17381 symbolS *symbolP;
17382 expressionS exp;
17383
17384 name = input_line_pointer;
17385 c = get_symbol_end ();
17386 symbolP = symbol_find_or_make (name);
17387 S_SET_WEAK (symbolP);
17388 *input_line_pointer = c;
17389
17390 SKIP_WHITESPACE ();
17391
17392 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17393 {
17394 if (S_IS_DEFINED (symbolP))
17395 {
20203fb9 17396 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17397 S_GET_NAME (symbolP));
17398 ignore_rest_of_line ();
17399 return;
17400 }
bdaaa2e1 17401
252b5132
RH
17402 if (*input_line_pointer == ',')
17403 {
17404 ++input_line_pointer;
17405 SKIP_WHITESPACE ();
17406 }
bdaaa2e1 17407
252b5132
RH
17408 expression (&exp);
17409 if (exp.X_op != O_symbol)
17410 {
20203fb9 17411 as_bad (_("bad .weakext directive"));
98d3f06f 17412 ignore_rest_of_line ();
252b5132
RH
17413 return;
17414 }
49309057 17415 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17416 }
17417
17418 demand_empty_rest_of_line ();
17419}
17420
17421/* Parse a register string into a number. Called from the ECOFF code
17422 to parse .frame. The argument is non-zero if this is the frame
17423 register, so that we can record it in mips_frame_reg. */
17424
17425int
17a2f251 17426tc_get_register (int frame)
252b5132 17427{
707bfff6 17428 unsigned int reg;
252b5132
RH
17429
17430 SKIP_WHITESPACE ();
707bfff6
TS
17431 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17432 reg = 0;
252b5132 17433 if (frame)
7a621144
DJ
17434 {
17435 mips_frame_reg = reg != 0 ? reg : SP;
17436 mips_frame_reg_valid = 1;
17437 mips_cprestore_valid = 0;
17438 }
252b5132
RH
17439 return reg;
17440}
17441
17442valueT
17a2f251 17443md_section_align (asection *seg, valueT addr)
252b5132
RH
17444{
17445 int align = bfd_get_section_alignment (stdoutput, seg);
17446
b4c71f56
TS
17447 if (IS_ELF)
17448 {
17449 /* We don't need to align ELF sections to the full alignment.
17450 However, Irix 5 may prefer that we align them at least to a 16
17451 byte boundary. We don't bother to align the sections if we
17452 are targeted for an embedded system. */
c41e87e3 17453 if (strncmp (TARGET_OS, "elf", 3) == 0)
b4c71f56
TS
17454 return addr;
17455 if (align > 4)
17456 align = 4;
17457 }
252b5132
RH
17458
17459 return ((addr + (1 << align) - 1) & (-1 << align));
17460}
17461
17462/* Utility routine, called from above as well. If called while the
17463 input file is still being read, it's only an approximation. (For
17464 example, a symbol may later become defined which appeared to be
17465 undefined earlier.) */
17466
17467static int
17a2f251 17468nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17469{
17470 if (sym == 0)
17471 return 0;
17472
4d0d148d 17473 if (g_switch_value > 0)
252b5132
RH
17474 {
17475 const char *symname;
17476 int change;
17477
c9914766 17478 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17479 register. It can be if it is smaller than the -G size or if
17480 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17481 not be referenced off the $gp, although it appears as though
252b5132
RH
17482 they can. */
17483 symname = S_GET_NAME (sym);
17484 if (symname != (const char *) NULL
17485 && (strcmp (symname, "eprol") == 0
17486 || strcmp (symname, "etext") == 0
17487 || strcmp (symname, "_gp") == 0
17488 || strcmp (symname, "edata") == 0
17489 || strcmp (symname, "_fbss") == 0
17490 || strcmp (symname, "_fdata") == 0
17491 || strcmp (symname, "_ftext") == 0
17492 || strcmp (symname, "end") == 0
17493 || strcmp (symname, "_gp_disp") == 0))
17494 change = 1;
17495 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17496 && (0
17497#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17498 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17499 && (symbol_get_obj (sym)->ecoff_extern_size
17500 <= g_switch_value))
252b5132
RH
17501#endif
17502 /* We must defer this decision until after the whole
17503 file has been read, since there might be a .extern
17504 after the first use of this symbol. */
17505 || (before_relaxing
17506#ifndef NO_ECOFF_DEBUGGING
49309057 17507 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17508#endif
17509 && S_GET_VALUE (sym) == 0)
17510 || (S_GET_VALUE (sym) != 0
17511 && S_GET_VALUE (sym) <= g_switch_value)))
17512 change = 0;
17513 else
17514 {
17515 const char *segname;
17516
17517 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17518 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17519 && strcmp (segname, ".lit4") != 0);
17520 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17521 && strcmp (segname, ".sbss") != 0
17522 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17523 && strncmp (segname, ".sbss.", 6) != 0
17524 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17525 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17526 }
17527 return change;
17528 }
17529 else
c9914766 17530 /* We are not optimizing for the $gp register. */
252b5132
RH
17531 return 1;
17532}
17533
5919d012
RS
17534
17535/* Return true if the given symbol should be considered local for SVR4 PIC. */
17536
17537static bfd_boolean
17a2f251 17538pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
17539{
17540 asection *symsec;
5919d012
RS
17541
17542 /* Handle the case of a symbol equated to another symbol. */
17543 while (symbol_equated_reloc_p (sym))
17544 {
17545 symbolS *n;
17546
5f0fe04b 17547 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17548 n = symbol_get_value_expression (sym)->X_add_symbol;
17549 if (n == sym)
17550 break;
17551 sym = n;
17552 }
17553
df1f3cda
DD
17554 if (symbol_section_p (sym))
17555 return TRUE;
17556
5919d012
RS
17557 symsec = S_GET_SEGMENT (sym);
17558
5919d012 17559 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17560 return (!bfd_is_und_section (symsec)
17561 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
17562 && !bfd_is_com_section (symsec)
17563 && !s_is_linkonce (sym, segtype)
5919d012
RS
17564#ifdef OBJ_ELF
17565 /* A global or weak symbol is treated as external. */
f43abd2b 17566 && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
5919d012
RS
17567#endif
17568 );
17569}
17570
17571
252b5132
RH
17572/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17573 extended opcode. SEC is the section the frag is in. */
17574
17575static int
17a2f251 17576mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
17577{
17578 int type;
3994f87e 17579 const struct mips16_immed_operand *op;
252b5132
RH
17580 offsetT val;
17581 int mintiny, maxtiny;
17582 segT symsec;
98aa84af 17583 fragS *sym_frag;
252b5132
RH
17584
17585 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17586 return 0;
17587 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17588 return 1;
17589
17590 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17591 op = mips16_immed_operands;
17592 while (op->type != type)
17593 {
17594 ++op;
9c2799c2 17595 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
17596 }
17597
17598 if (op->unsp)
17599 {
17600 if (type == '<' || type == '>' || type == '[' || type == ']')
17601 {
17602 mintiny = 1;
17603 maxtiny = 1 << op->nbits;
17604 }
17605 else
17606 {
17607 mintiny = 0;
17608 maxtiny = (1 << op->nbits) - 1;
17609 }
17610 }
17611 else
17612 {
17613 mintiny = - (1 << (op->nbits - 1));
17614 maxtiny = (1 << (op->nbits - 1)) - 1;
17615 }
17616
98aa84af 17617 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 17618 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 17619 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
17620
17621 if (op->pcrel)
17622 {
17623 addressT addr;
17624
17625 /* We won't have the section when we are called from
17626 mips_relax_frag. However, we will always have been called
17627 from md_estimate_size_before_relax first. If this is a
17628 branch to a different section, we mark it as such. If SEC is
17629 NULL, and the frag is not marked, then it must be a branch to
17630 the same section. */
17631 if (sec == NULL)
17632 {
17633 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17634 return 1;
17635 }
17636 else
17637 {
98aa84af 17638 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
17639 if (symsec != sec)
17640 {
17641 fragp->fr_subtype =
17642 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17643
17644 /* FIXME: We should support this, and let the linker
17645 catch branches and loads that are out of range. */
17646 as_bad_where (fragp->fr_file, fragp->fr_line,
17647 _("unsupported PC relative reference to different section"));
17648
17649 return 1;
17650 }
98aa84af
AM
17651 if (fragp != sym_frag && sym_frag->fr_address == 0)
17652 /* Assume non-extended on the first relaxation pass.
17653 The address we have calculated will be bogus if this is
17654 a forward branch to another frag, as the forward frag
17655 will have fr_address == 0. */
17656 return 0;
252b5132
RH
17657 }
17658
17659 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
17660 the same section. If the relax_marker of the symbol fragment
17661 differs from the relax_marker of this fragment, we have not
17662 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
17663 in STRETCH in order to get a better estimate of the address.
17664 This particularly matters because of the shift bits. */
17665 if (stretch != 0
98aa84af 17666 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
17667 {
17668 fragS *f;
17669
17670 /* Adjust stretch for any alignment frag. Note that if have
17671 been expanding the earlier code, the symbol may be
17672 defined in what appears to be an earlier frag. FIXME:
17673 This doesn't handle the fr_subtype field, which specifies
17674 a maximum number of bytes to skip when doing an
17675 alignment. */
98aa84af 17676 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
17677 {
17678 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17679 {
17680 if (stretch < 0)
17681 stretch = - ((- stretch)
17682 & ~ ((1 << (int) f->fr_offset) - 1));
17683 else
17684 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17685 if (stretch == 0)
17686 break;
17687 }
17688 }
17689 if (f != NULL)
17690 val += stretch;
17691 }
17692
17693 addr = fragp->fr_address + fragp->fr_fix;
17694
17695 /* The base address rules are complicated. The base address of
17696 a branch is the following instruction. The base address of a
17697 PC relative load or add is the instruction itself, but if it
17698 is in a delay slot (in which case it can not be extended) use
17699 the address of the instruction whose delay slot it is in. */
17700 if (type == 'p' || type == 'q')
17701 {
17702 addr += 2;
17703
17704 /* If we are currently assuming that this frag should be
17705 extended, then, the current address is two bytes
bdaaa2e1 17706 higher. */
252b5132
RH
17707 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17708 addr += 2;
17709
17710 /* Ignore the low bit in the target, since it will be set
17711 for a text label. */
17712 if ((val & 1) != 0)
17713 --val;
17714 }
17715 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17716 addr -= 4;
17717 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17718 addr -= 2;
17719
17720 val -= addr & ~ ((1 << op->shift) - 1);
17721
17722 /* Branch offsets have an implicit 0 in the lowest bit. */
17723 if (type == 'p' || type == 'q')
17724 val /= 2;
17725
17726 /* If any of the shifted bits are set, we must use an extended
17727 opcode. If the address depends on the size of this
17728 instruction, this can lead to a loop, so we arrange to always
17729 use an extended opcode. We only check this when we are in
17730 the main relaxation loop, when SEC is NULL. */
17731 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17732 {
17733 fragp->fr_subtype =
17734 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17735 return 1;
17736 }
17737
17738 /* If we are about to mark a frag as extended because the value
17739 is precisely maxtiny + 1, then there is a chance of an
17740 infinite loop as in the following code:
17741 la $4,foo
17742 .skip 1020
17743 .align 2
17744 foo:
17745 In this case when the la is extended, foo is 0x3fc bytes
17746 away, so the la can be shrunk, but then foo is 0x400 away, so
17747 the la must be extended. To avoid this loop, we mark the
17748 frag as extended if it was small, and is about to become
17749 extended with a value of maxtiny + 1. */
17750 if (val == ((maxtiny + 1) << op->shift)
17751 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17752 && sec == NULL)
17753 {
17754 fragp->fr_subtype =
17755 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17756 return 1;
17757 }
17758 }
17759 else if (symsec != absolute_section && sec != NULL)
17760 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17761
17762 if ((val & ((1 << op->shift) - 1)) != 0
17763 || val < (mintiny << op->shift)
17764 || val > (maxtiny << op->shift))
17765 return 1;
17766 else
17767 return 0;
17768}
17769
4a6a3df4
AO
17770/* Compute the length of a branch sequence, and adjust the
17771 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17772 worst-case length is computed, with UPDATE being used to indicate
17773 whether an unconditional (-1), branch-likely (+1) or regular (0)
17774 branch is to be computed. */
17775static int
17a2f251 17776relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17777{
b34976b6 17778 bfd_boolean toofar;
4a6a3df4
AO
17779 int length;
17780
17781 if (fragp
17782 && S_IS_DEFINED (fragp->fr_symbol)
17783 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17784 {
17785 addressT addr;
17786 offsetT val;
17787
17788 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17789
17790 addr = fragp->fr_address + fragp->fr_fix + 4;
17791
17792 val -= addr;
17793
17794 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17795 }
17796 else if (fragp)
17797 /* If the symbol is not defined or it's in a different segment,
17798 assume the user knows what's going on and emit a short
17799 branch. */
b34976b6 17800 toofar = FALSE;
4a6a3df4 17801 else
b34976b6 17802 toofar = TRUE;
4a6a3df4
AO
17803
17804 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17805 fragp->fr_subtype
66b3e8da
MR
17806 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17807 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17808 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17809 RELAX_BRANCH_LINK (fragp->fr_subtype),
17810 toofar);
17811
17812 length = 4;
17813 if (toofar)
17814 {
17815 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17816 length += 8;
17817
17818 if (mips_pic != NO_PIC)
17819 {
17820 /* Additional space for PIC loading of target address. */
17821 length += 8;
17822 if (mips_opts.isa == ISA_MIPS1)
17823 /* Additional space for $at-stabilizing nop. */
17824 length += 4;
17825 }
17826
17827 /* If branch is conditional. */
17828 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17829 length += 8;
17830 }
b34976b6 17831
4a6a3df4
AO
17832 return length;
17833}
17834
df58fc94
RS
17835/* Compute the length of a branch sequence, and adjust the
17836 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17837 worst-case length is computed, with UPDATE being used to indicate
17838 whether an unconditional (-1), or regular (0) branch is to be
17839 computed. */
17840
17841static int
17842relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17843{
17844 bfd_boolean toofar;
17845 int length;
17846
17847 if (fragp
17848 && S_IS_DEFINED (fragp->fr_symbol)
17849 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17850 {
17851 addressT addr;
17852 offsetT val;
17853
17854 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17855 /* Ignore the low bit in the target, since it will be set
17856 for a text label. */
17857 if ((val & 1) != 0)
17858 --val;
17859
17860 addr = fragp->fr_address + fragp->fr_fix + 4;
17861
17862 val -= addr;
17863
17864 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17865 }
17866 else if (fragp)
17867 /* If the symbol is not defined or it's in a different segment,
17868 assume the user knows what's going on and emit a short
17869 branch. */
17870 toofar = FALSE;
17871 else
17872 toofar = TRUE;
17873
17874 if (fragp && update
17875 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17876 fragp->fr_subtype = (toofar
17877 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17878 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17879
17880 length = 4;
17881 if (toofar)
17882 {
17883 bfd_boolean compact_known = fragp != NULL;
17884 bfd_boolean compact = FALSE;
17885 bfd_boolean uncond;
17886
17887 if (compact_known)
17888 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17889 if (fragp)
17890 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17891 else
17892 uncond = update < 0;
17893
17894 /* If label is out of range, we turn branch <br>:
17895
17896 <br> label # 4 bytes
17897 0:
17898
17899 into:
17900
17901 j label # 4 bytes
17902 nop # 2 bytes if compact && !PIC
17903 0:
17904 */
17905 if (mips_pic == NO_PIC && (!compact_known || compact))
17906 length += 2;
17907
17908 /* If assembling PIC code, we further turn:
17909
17910 j label # 4 bytes
17911
17912 into:
17913
17914 lw/ld at, %got(label)(gp) # 4 bytes
17915 d/addiu at, %lo(label) # 4 bytes
17916 jr/c at # 2 bytes
17917 */
17918 if (mips_pic != NO_PIC)
17919 length += 6;
17920
17921 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17922
17923 <brneg> 0f # 4 bytes
17924 nop # 2 bytes if !compact
17925 */
17926 if (!uncond)
17927 length += (compact_known && compact) ? 4 : 6;
17928 }
17929
17930 return length;
17931}
17932
17933/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17934 bit accordingly. */
17935
17936static int
17937relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17938{
17939 bfd_boolean toofar;
17940
df58fc94
RS
17941 if (fragp
17942 && S_IS_DEFINED (fragp->fr_symbol)
17943 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17944 {
17945 addressT addr;
17946 offsetT val;
17947 int type;
17948
17949 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17950 /* Ignore the low bit in the target, since it will be set
17951 for a text label. */
17952 if ((val & 1) != 0)
17953 --val;
17954
17955 /* Assume this is a 2-byte branch. */
17956 addr = fragp->fr_address + fragp->fr_fix + 2;
17957
17958 /* We try to avoid the infinite loop by not adding 2 more bytes for
17959 long branches. */
17960
17961 val -= addr;
17962
17963 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17964 if (type == 'D')
17965 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17966 else if (type == 'E')
17967 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17968 else
17969 abort ();
17970 }
17971 else
17972 /* If the symbol is not defined or it's in a different segment,
17973 we emit a normal 32-bit branch. */
17974 toofar = TRUE;
17975
17976 if (fragp && update
17977 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17978 fragp->fr_subtype
17979 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17980 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17981
17982 if (toofar)
17983 return 4;
17984
17985 return 2;
17986}
17987
252b5132
RH
17988/* Estimate the size of a frag before relaxing. Unless this is the
17989 mips16, we are not really relaxing here, and the final size is
17990 encoded in the subtype information. For the mips16, we have to
17991 decide whether we are using an extended opcode or not. */
17992
252b5132 17993int
17a2f251 17994md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17995{
5919d012 17996 int change;
252b5132 17997
4a6a3df4
AO
17998 if (RELAX_BRANCH_P (fragp->fr_subtype))
17999 {
18000
b34976b6
AM
18001 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
18002
4a6a3df4
AO
18003 return fragp->fr_var;
18004 }
18005
252b5132 18006 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
18007 /* We don't want to modify the EXTENDED bit here; it might get us
18008 into infinite loops. We change it only in mips_relax_frag(). */
18009 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 18010
df58fc94
RS
18011 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18012 {
18013 int length = 4;
18014
18015 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18016 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
18017 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18018 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
18019 fragp->fr_var = length;
18020
18021 return length;
18022 }
18023
252b5132 18024 if (mips_pic == NO_PIC)
5919d012 18025 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 18026 else if (mips_pic == SVR4_PIC)
5919d012 18027 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
18028 else if (mips_pic == VXWORKS_PIC)
18029 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
18030 change = 0;
252b5132
RH
18031 else
18032 abort ();
18033
18034 if (change)
18035 {
4d7206a2 18036 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 18037 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 18038 }
4d7206a2
RS
18039 else
18040 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
18041}
18042
18043/* This is called to see whether a reloc against a defined symbol
de7e6852 18044 should be converted into a reloc against a section. */
252b5132
RH
18045
18046int
17a2f251 18047mips_fix_adjustable (fixS *fixp)
252b5132 18048{
252b5132
RH
18049 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
18050 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18051 return 0;
a161fe53 18052
252b5132
RH
18053 if (fixp->fx_addsy == NULL)
18054 return 1;
a161fe53 18055
de7e6852
RS
18056 /* If symbol SYM is in a mergeable section, relocations of the form
18057 SYM + 0 can usually be made section-relative. The mergeable data
18058 is then identified by the section offset rather than by the symbol.
18059
18060 However, if we're generating REL LO16 relocations, the offset is split
18061 between the LO16 and parterning high part relocation. The linker will
18062 need to recalculate the complete offset in order to correctly identify
18063 the merge data.
18064
18065 The linker has traditionally not looked for the parterning high part
18066 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
18067 placed anywhere. Rather than break backwards compatibility by changing
18068 this, it seems better not to force the issue, and instead keep the
18069 original symbol. This will work with either linker behavior. */
738e5348 18070 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 18071 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
18072 && HAVE_IN_PLACE_ADDENDS
18073 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
18074 return 0;
18075
ce70d90a 18076 /* There is no place to store an in-place offset for JALR relocations.
2de39019
CM
18077 Likewise an in-range offset of limited PC-relative relocations may
18078 overflow the in-place relocatable field if recalculated against the
18079 start address of the symbol's containing section. */
ce70d90a 18080 if (HAVE_IN_PLACE_ADDENDS
2de39019
CM
18081 && (limited_pcrel_reloc_p (fixp->fx_r_type)
18082 || jalr_reloc_p (fixp->fx_r_type)))
1180b5a4
RS
18083 return 0;
18084
252b5132 18085#ifdef OBJ_ELF
b314ec0e
RS
18086 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
18087 to a floating-point stub. The same is true for non-R_MIPS16_26
18088 relocations against MIPS16 functions; in this case, the stub becomes
18089 the function's canonical address.
18090
18091 Floating-point stubs are stored in unique .mips16.call.* or
18092 .mips16.fn.* sections. If a stub T for function F is in section S,
18093 the first relocation in section S must be against F; this is how the
18094 linker determines the target function. All relocations that might
18095 resolve to T must also be against F. We therefore have the following
18096 restrictions, which are given in an intentionally-redundant way:
18097
18098 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18099 symbols.
18100
18101 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18102 if that stub might be used.
18103
18104 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18105 symbols.
18106
18107 4. We cannot reduce a stub's relocations against MIPS16 symbols if
18108 that stub might be used.
18109
18110 There is a further restriction:
18111
df58fc94
RS
18112 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
18113 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
18114 targets with in-place addends; the relocation field cannot
b314ec0e
RS
18115 encode the low bit.
18116
df58fc94
RS
18117 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
18118 against a MIPS16 symbol. We deal with (5) by by not reducing any
18119 such relocations on REL targets.
b314ec0e
RS
18120
18121 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18122 relocation against some symbol R, no relocation against R may be
18123 reduced. (Note that this deals with (2) as well as (1) because
18124 relocations against global symbols will never be reduced on ELF
18125 targets.) This approach is a little simpler than trying to detect
18126 stub sections, and gives the "all or nothing" per-symbol consistency
18127 that we have for MIPS16 symbols. */
f43abd2b 18128 if (IS_ELF
b314ec0e 18129 && fixp->fx_subsy == NULL
30c09090 18130 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
df58fc94
RS
18131 || *symbol_get_tc (fixp->fx_addsy)
18132 || (HAVE_IN_PLACE_ADDENDS
18133 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
18134 && jmp_reloc_p (fixp->fx_r_type))))
252b5132
RH
18135 return 0;
18136#endif
a161fe53 18137
252b5132
RH
18138 return 1;
18139}
18140
18141/* Translate internal representation of relocation info to BFD target
18142 format. */
18143
18144arelent **
17a2f251 18145tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
18146{
18147 static arelent *retval[4];
18148 arelent *reloc;
18149 bfd_reloc_code_real_type code;
18150
4b0cff4e
TS
18151 memset (retval, 0, sizeof(retval));
18152 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
18153 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
18154 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
18155 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18156
bad36eac
DJ
18157 if (fixp->fx_pcrel)
18158 {
df58fc94
RS
18159 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18160 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18161 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
18162 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18163 || fixp->fx_r_type == BFD_RELOC_32_PCREL);
bad36eac
DJ
18164
18165 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18166 Relocations want only the symbol offset. */
18167 reloc->addend = fixp->fx_addnumber + reloc->address;
f43abd2b 18168 if (!IS_ELF)
bad36eac
DJ
18169 {
18170 /* A gruesome hack which is a result of the gruesome gas
18171 reloc handling. What's worse, for COFF (as opposed to
18172 ECOFF), we might need yet another copy of reloc->address.
18173 See bfd_install_relocation. */
18174 reloc->addend += reloc->address;
18175 }
18176 }
18177 else
18178 reloc->addend = fixp->fx_addnumber;
252b5132 18179
438c16b8
TS
18180 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18181 entry to be used in the relocation's section offset. */
18182 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18183 {
18184 reloc->address = reloc->addend;
18185 reloc->addend = 0;
18186 }
18187
252b5132 18188 code = fixp->fx_r_type;
252b5132 18189
bad36eac 18190 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18191 if (reloc->howto == NULL)
18192 {
18193 as_bad_where (fixp->fx_file, fixp->fx_line,
18194 _("Can not represent %s relocation in this object file format"),
18195 bfd_get_reloc_code_name (code));
18196 retval[0] = NULL;
18197 }
18198
18199 return retval;
18200}
18201
18202/* Relax a machine dependent frag. This returns the amount by which
18203 the current size of the frag should change. */
18204
18205int
17a2f251 18206mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18207{
4a6a3df4
AO
18208 if (RELAX_BRANCH_P (fragp->fr_subtype))
18209 {
18210 offsetT old_var = fragp->fr_var;
b34976b6
AM
18211
18212 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
18213
18214 return fragp->fr_var - old_var;
18215 }
18216
df58fc94
RS
18217 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18218 {
18219 offsetT old_var = fragp->fr_var;
18220 offsetT new_var = 4;
18221
18222 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18223 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18224 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18225 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18226 fragp->fr_var = new_var;
18227
18228 return new_var - old_var;
18229 }
18230
252b5132
RH
18231 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18232 return 0;
18233
c4e7957c 18234 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
18235 {
18236 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18237 return 0;
18238 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18239 return 2;
18240 }
18241 else
18242 {
18243 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18244 return 0;
18245 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18246 return -2;
18247 }
18248
18249 return 0;
18250}
18251
18252/* Convert a machine dependent frag. */
18253
18254void
17a2f251 18255md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18256{
4a6a3df4
AO
18257 if (RELAX_BRANCH_P (fragp->fr_subtype))
18258 {
4d68580a 18259 char *buf;
4a6a3df4
AO
18260 unsigned long insn;
18261 expressionS exp;
18262 fixS *fixp;
b34976b6 18263
4d68580a
RS
18264 buf = fragp->fr_literal + fragp->fr_fix;
18265 insn = read_insn (buf);
b34976b6 18266
4a6a3df4
AO
18267 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18268 {
18269 /* We generate a fixup instead of applying it right now
18270 because, if there are linker relaxations, we're going to
18271 need the relocations. */
18272 exp.X_op = O_symbol;
18273 exp.X_add_symbol = fragp->fr_symbol;
18274 exp.X_add_number = fragp->fr_offset;
18275
4d68580a
RS
18276 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18277 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18278 fixp->fx_file = fragp->fr_file;
18279 fixp->fx_line = fragp->fr_line;
b34976b6 18280
4d68580a 18281 buf = write_insn (buf, insn);
4a6a3df4
AO
18282 }
18283 else
18284 {
18285 int i;
18286
18287 as_warn_where (fragp->fr_file, fragp->fr_line,
5c4f07ba 18288 _("Relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18289
18290 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18291 goto uncond;
18292
18293 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18294 {
18295 /* Reverse the branch. */
18296 switch ((insn >> 28) & 0xf)
18297 {
18298 case 4:
3bf0dbfb
MR
18299 /* bc[0-3][tf]l? instructions can have the condition
18300 reversed by tweaking a single TF bit, and their
18301 opcodes all have 0x4???????. */
18302 gas_assert ((insn & 0xf3e00000) == 0x41000000);
4a6a3df4
AO
18303 insn ^= 0x00010000;
18304 break;
18305
18306 case 0:
18307 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18308 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18309 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18310 insn ^= 0x00010000;
18311 break;
b34976b6 18312
4a6a3df4
AO
18313 case 1:
18314 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18315 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18316 insn ^= 0x04000000;
18317 break;
18318
18319 default:
18320 abort ();
18321 }
18322 }
18323
18324 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18325 {
18326 /* Clear the and-link bit. */
9c2799c2 18327 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18328
54f4ddb3
TS
18329 /* bltzal 0x04100000 bgezal 0x04110000
18330 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18331 insn &= ~0x00100000;
18332 }
18333
18334 /* Branch over the branch (if the branch was likely) or the
18335 full jump (not likely case). Compute the offset from the
18336 current instruction to branch to. */
18337 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18338 i = 16;
18339 else
18340 {
18341 /* How many bytes in instructions we've already emitted? */
4d68580a 18342 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18343 /* How many bytes in instructions from here to the end? */
18344 i = fragp->fr_var - i;
18345 }
18346 /* Convert to instruction count. */
18347 i >>= 2;
18348 /* Branch counts from the next instruction. */
b34976b6 18349 i--;
4a6a3df4
AO
18350 insn |= i;
18351 /* Branch over the jump. */
4d68580a 18352 buf = write_insn (buf, insn);
4a6a3df4 18353
54f4ddb3 18354 /* nop */
4d68580a 18355 buf = write_insn (buf, 0);
4a6a3df4
AO
18356
18357 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18358 {
18359 /* beql $0, $0, 2f */
18360 insn = 0x50000000;
18361 /* Compute the PC offset from the current instruction to
18362 the end of the variable frag. */
18363 /* How many bytes in instructions we've already emitted? */
4d68580a 18364 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18365 /* How many bytes in instructions from here to the end? */
18366 i = fragp->fr_var - i;
18367 /* Convert to instruction count. */
18368 i >>= 2;
18369 /* Don't decrement i, because we want to branch over the
18370 delay slot. */
4a6a3df4 18371 insn |= i;
4a6a3df4 18372
4d68580a
RS
18373 buf = write_insn (buf, insn);
18374 buf = write_insn (buf, 0);
4a6a3df4
AO
18375 }
18376
18377 uncond:
18378 if (mips_pic == NO_PIC)
18379 {
18380 /* j or jal. */
18381 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18382 ? 0x0c000000 : 0x08000000);
18383 exp.X_op = O_symbol;
18384 exp.X_add_symbol = fragp->fr_symbol;
18385 exp.X_add_number = fragp->fr_offset;
18386
4d68580a
RS
18387 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18388 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18389 fixp->fx_file = fragp->fr_file;
18390 fixp->fx_line = fragp->fr_line;
18391
4d68580a 18392 buf = write_insn (buf, insn);
4a6a3df4
AO
18393 }
18394 else
18395 {
66b3e8da
MR
18396 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18397
4a6a3df4 18398 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18399 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18400 insn |= at << OP_SH_RT;
4a6a3df4
AO
18401 exp.X_op = O_symbol;
18402 exp.X_add_symbol = fragp->fr_symbol;
18403 exp.X_add_number = fragp->fr_offset;
18404
18405 if (fragp->fr_offset)
18406 {
18407 exp.X_add_symbol = make_expr_symbol (&exp);
18408 exp.X_add_number = 0;
18409 }
18410
4d68580a
RS
18411 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18412 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18413 fixp->fx_file = fragp->fr_file;
18414 fixp->fx_line = fragp->fr_line;
18415
4d68580a 18416 buf = write_insn (buf, insn);
b34976b6 18417
4a6a3df4 18418 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18419 /* nop */
18420 buf = write_insn (buf, 0);
4a6a3df4
AO
18421
18422 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18423 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18424 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18425
4d68580a
RS
18426 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18427 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18428 fixp->fx_file = fragp->fr_file;
18429 fixp->fx_line = fragp->fr_line;
b34976b6 18430
4d68580a 18431 buf = write_insn (buf, insn);
4a6a3df4
AO
18432
18433 /* j(al)r $at. */
18434 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18435 insn = 0x0000f809;
4a6a3df4 18436 else
66b3e8da
MR
18437 insn = 0x00000008;
18438 insn |= at << OP_SH_RS;
4a6a3df4 18439
4d68580a 18440 buf = write_insn (buf, insn);
4a6a3df4
AO
18441 }
18442 }
18443
4a6a3df4 18444 fragp->fr_fix += fragp->fr_var;
4d68580a 18445 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18446 return;
18447 }
18448
df58fc94
RS
18449 /* Relax microMIPS branches. */
18450 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18451 {
4d68580a 18452 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18453 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18454 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18455 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18456 bfd_boolean short_ds;
df58fc94
RS
18457 unsigned long insn;
18458 expressionS exp;
18459 fixS *fixp;
18460
18461 exp.X_op = O_symbol;
18462 exp.X_add_symbol = fragp->fr_symbol;
18463 exp.X_add_number = fragp->fr_offset;
18464
18465 fragp->fr_fix += fragp->fr_var;
18466
18467 /* Handle 16-bit branches that fit or are forced to fit. */
18468 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18469 {
18470 /* We generate a fixup instead of applying it right now,
18471 because if there is linker relaxation, we're going to
18472 need the relocations. */
18473 if (type == 'D')
4d68580a 18474 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18475 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18476 else if (type == 'E')
4d68580a 18477 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18478 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18479 else
18480 abort ();
18481
18482 fixp->fx_file = fragp->fr_file;
18483 fixp->fx_line = fragp->fr_line;
18484
18485 /* These relocations can have an addend that won't fit in
18486 2 octets. */
18487 fixp->fx_no_overflow = 1;
18488
18489 return;
18490 }
18491
2309ddf2 18492 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18493 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18494 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18495 {
18496 /* We generate a fixup instead of applying it right now,
18497 because if there is linker relaxation, we're going to
18498 need the relocations. */
4d68580a
RS
18499 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18500 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18501 fixp->fx_file = fragp->fr_file;
18502 fixp->fx_line = fragp->fr_line;
18503
18504 if (type == 0)
18505 return;
18506 }
18507
18508 /* Relax 16-bit branches to 32-bit branches. */
18509 if (type != 0)
18510 {
4d68580a 18511 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18512
18513 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18514 insn = 0x94000000; /* beq */
18515 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18516 {
18517 unsigned long regno;
18518
18519 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18520 regno = micromips_to_32_reg_d_map [regno];
18521 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18522 insn |= regno << MICROMIPSOP_SH_RS;
18523 }
18524 else
18525 abort ();
18526
18527 /* Nothing else to do, just write it out. */
18528 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18529 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18530 {
4d68580a
RS
18531 buf = write_compressed_insn (buf, insn, 4);
18532 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18533 return;
18534 }
18535 }
18536 else
4d68580a 18537 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18538
18539 /* Relax 32-bit branches to a sequence of instructions. */
18540 as_warn_where (fragp->fr_file, fragp->fr_line,
18541 _("Relaxed out-of-range branch into a jump"));
18542
2309ddf2
MR
18543 /* Set the short-delay-slot bit. */
18544 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
18545
18546 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18547 {
18548 symbolS *l;
18549
18550 /* Reverse the branch. */
18551 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18552 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18553 insn ^= 0x20000000;
18554 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18555 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18556 || (insn & 0xffe00000) == 0x40800000 /* blez */
18557 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18558 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18559 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18560 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18561 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18562 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18563 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18564 insn ^= 0x00400000;
18565 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18566 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18567 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18568 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18569 insn ^= 0x00200000;
18570 else
18571 abort ();
18572
18573 if (al)
18574 {
18575 /* Clear the and-link and short-delay-slot bits. */
18576 gas_assert ((insn & 0xfda00000) == 0x40200000);
18577
18578 /* bltzal 0x40200000 bgezal 0x40600000 */
18579 /* bltzals 0x42200000 bgezals 0x42600000 */
18580 insn &= ~0x02200000;
18581 }
18582
18583 /* Make a label at the end for use with the branch. */
18584 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18585 micromips_label_inc ();
18586#if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
18587 if (IS_ELF)
18588 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18589#endif
18590
18591 /* Refer to it. */
4d68580a
RS
18592 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18593 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18594 fixp->fx_file = fragp->fr_file;
18595 fixp->fx_line = fragp->fr_line;
18596
18597 /* Branch over the jump. */
4d68580a 18598 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18599 if (!compact)
4d68580a
RS
18600 /* nop */
18601 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
18602 }
18603
18604 if (mips_pic == NO_PIC)
18605 {
2309ddf2
MR
18606 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
18607
df58fc94
RS
18608 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18609 insn = al ? jal : 0xd4000000;
18610
4d68580a
RS
18611 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18612 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18613 fixp->fx_file = fragp->fr_file;
18614 fixp->fx_line = fragp->fr_line;
18615
4d68580a 18616 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18617 if (compact)
4d68580a
RS
18618 /* nop */
18619 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
18620 }
18621 else
18622 {
18623 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
18624 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18625 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
18626
18627 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18628 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18629 insn |= at << MICROMIPSOP_SH_RT;
18630
18631 if (exp.X_add_number)
18632 {
18633 exp.X_add_symbol = make_expr_symbol (&exp);
18634 exp.X_add_number = 0;
18635 }
18636
4d68580a
RS
18637 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18638 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18639 fixp->fx_file = fragp->fr_file;
18640 fixp->fx_line = fragp->fr_line;
18641
4d68580a 18642 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18643
18644 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18645 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18646 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18647
4d68580a
RS
18648 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18649 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18650 fixp->fx_file = fragp->fr_file;
18651 fixp->fx_line = fragp->fr_line;
18652
4d68580a 18653 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18654
18655 /* jr/jrc/jalr/jalrs $at */
18656 insn = al ? jalr : jr;
18657 insn |= at << MICROMIPSOP_SH_MJ;
18658
4d68580a 18659 buf = write_compressed_insn (buf, insn, 2);
df58fc94
RS
18660 }
18661
4d68580a 18662 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18663 return;
18664 }
18665
252b5132
RH
18666 if (RELAX_MIPS16_P (fragp->fr_subtype))
18667 {
18668 int type;
3994f87e 18669 const struct mips16_immed_operand *op;
252b5132 18670 offsetT val;
5c04167a
RS
18671 char *buf;
18672 unsigned int user_length, length;
252b5132 18673 unsigned long insn;
5c04167a 18674 bfd_boolean ext;
252b5132
RH
18675
18676 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18677 op = mips16_immed_operands;
18678 while (op->type != type)
18679 ++op;
18680
5c04167a 18681 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
5f5f22c0 18682 val = resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
18683 if (op->pcrel)
18684 {
18685 addressT addr;
18686
18687 addr = fragp->fr_address + fragp->fr_fix;
18688
18689 /* The rules for the base address of a PC relative reloc are
18690 complicated; see mips16_extended_frag. */
18691 if (type == 'p' || type == 'q')
18692 {
18693 addr += 2;
18694 if (ext)
18695 addr += 2;
18696 /* Ignore the low bit in the target, since it will be
18697 set for a text label. */
18698 if ((val & 1) != 0)
18699 --val;
18700 }
18701 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18702 addr -= 4;
18703 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18704 addr -= 2;
18705
18706 addr &= ~ (addressT) ((1 << op->shift) - 1);
18707 val -= addr;
18708
18709 /* Make sure the section winds up with the alignment we have
18710 assumed. */
18711 if (op->shift > 0)
18712 record_alignment (asec, op->shift);
18713 }
18714
18715 if (ext
18716 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18717 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18718 as_warn_where (fragp->fr_file, fragp->fr_line,
18719 _("extended instruction in delay slot"));
18720
5c04167a 18721 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 18722
4d68580a 18723 insn = read_compressed_insn (buf, 2);
5c04167a
RS
18724 if (ext)
18725 insn |= MIPS16_EXTEND;
252b5132 18726
5c04167a
RS
18727 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18728 user_length = 4;
18729 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18730 user_length = 2;
18731 else
18732 user_length = 0;
18733
43c0598f 18734 mips16_immed (fragp->fr_file, fragp->fr_line, type,
c150d1d2 18735 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 18736
5c04167a
RS
18737 length = (ext ? 4 : 2);
18738 gas_assert (mips16_opcode_length (insn) == length);
18739 write_compressed_insn (buf, insn, length);
18740 fragp->fr_fix += length;
252b5132
RH
18741 }
18742 else
18743 {
df58fc94
RS
18744 relax_substateT subtype = fragp->fr_subtype;
18745 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18746 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
18747 int first, second;
18748 fixS *fixp;
252b5132 18749
df58fc94
RS
18750 first = RELAX_FIRST (subtype);
18751 second = RELAX_SECOND (subtype);
4d7206a2 18752 fixp = (fixS *) fragp->fr_opcode;
252b5132 18753
df58fc94
RS
18754 /* If the delay slot chosen does not match the size of the instruction,
18755 then emit a warning. */
18756 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18757 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18758 {
18759 relax_substateT s;
18760 const char *msg;
18761
18762 s = subtype & (RELAX_DELAY_SLOT_16BIT
18763 | RELAX_DELAY_SLOT_SIZE_FIRST
18764 | RELAX_DELAY_SLOT_SIZE_SECOND);
18765 msg = macro_warning (s);
18766 if (msg != NULL)
db9b2be4 18767 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
18768 subtype &= ~s;
18769 }
18770
584892a6 18771 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 18772 if (use_second == second_longer)
584892a6 18773 {
df58fc94
RS
18774 relax_substateT s;
18775 const char *msg;
18776
18777 s = (subtype
18778 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18779 msg = macro_warning (s);
18780 if (msg != NULL)
db9b2be4 18781 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 18782 subtype &= ~s;
584892a6
RS
18783 }
18784
4d7206a2
RS
18785 /* Go through all the fixups for the first sequence. Disable them
18786 (by marking them as done) if we're going to use the second
18787 sequence instead. */
18788 while (fixp
18789 && fixp->fx_frag == fragp
18790 && fixp->fx_where < fragp->fr_fix - second)
18791 {
df58fc94 18792 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18793 fixp->fx_done = 1;
18794 fixp = fixp->fx_next;
18795 }
252b5132 18796
4d7206a2
RS
18797 /* Go through the fixups for the second sequence. Disable them if
18798 we're going to use the first sequence, otherwise adjust their
18799 addresses to account for the relaxation. */
18800 while (fixp && fixp->fx_frag == fragp)
18801 {
df58fc94 18802 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18803 fixp->fx_where -= first;
18804 else
18805 fixp->fx_done = 1;
18806 fixp = fixp->fx_next;
18807 }
18808
18809 /* Now modify the frag contents. */
df58fc94 18810 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18811 {
18812 char *start;
18813
18814 start = fragp->fr_literal + fragp->fr_fix - first - second;
18815 memmove (start, start + first, second);
18816 fragp->fr_fix -= first;
18817 }
18818 else
18819 fragp->fr_fix -= second;
252b5132
RH
18820 }
18821}
18822
18823#ifdef OBJ_ELF
18824
18825/* This function is called after the relocs have been generated.
18826 We've been storing mips16 text labels as odd. Here we convert them
18827 back to even for the convenience of the debugger. */
18828
18829void
17a2f251 18830mips_frob_file_after_relocs (void)
252b5132
RH
18831{
18832 asymbol **syms;
18833 unsigned int count, i;
18834
f43abd2b 18835 if (!IS_ELF)
252b5132
RH
18836 return;
18837
18838 syms = bfd_get_outsymbols (stdoutput);
18839 count = bfd_get_symcount (stdoutput);
18840 for (i = 0; i < count; i++, syms++)
df58fc94
RS
18841 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18842 && ((*syms)->value & 1) != 0)
18843 {
18844 (*syms)->value &= ~1;
18845 /* If the symbol has an odd size, it was probably computed
18846 incorrectly, so adjust that as well. */
18847 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18848 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18849 }
252b5132
RH
18850}
18851
18852#endif
18853
a1facbec
MR
18854/* This function is called whenever a label is defined, including fake
18855 labels instantiated off the dot special symbol. It is used when
18856 handling branch delays; if a branch has a label, we assume we cannot
18857 move it. This also bumps the value of the symbol by 1 in compressed
18858 code. */
252b5132 18859
e1b47bd5 18860static void
a1facbec 18861mips_record_label (symbolS *sym)
252b5132 18862{
a8dbcb85 18863 segment_info_type *si = seg_info (now_seg);
252b5132
RH
18864 struct insn_label_list *l;
18865
18866 if (free_insn_labels == NULL)
18867 l = (struct insn_label_list *) xmalloc (sizeof *l);
18868 else
18869 {
18870 l = free_insn_labels;
18871 free_insn_labels = l->next;
18872 }
18873
18874 l->label = sym;
a8dbcb85
TS
18875 l->next = si->label_list;
18876 si->label_list = l;
a1facbec 18877}
07a53e5c 18878
a1facbec
MR
18879/* This function is called as tc_frob_label() whenever a label is defined
18880 and adds a DWARF-2 record we only want for true labels. */
18881
18882void
18883mips_define_label (symbolS *sym)
18884{
18885 mips_record_label (sym);
07a53e5c
RH
18886#ifdef OBJ_ELF
18887 dwarf2_emit_label (sym);
18888#endif
252b5132 18889}
e1b47bd5
RS
18890
18891/* This function is called by tc_new_dot_label whenever a new dot symbol
18892 is defined. */
18893
18894void
18895mips_add_dot_label (symbolS *sym)
18896{
18897 mips_record_label (sym);
18898 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18899 mips_compressed_mark_label (sym);
18900}
252b5132
RH
18901\f
18902#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
18903
18904/* Some special processing for a MIPS ELF file. */
18905
18906void
17a2f251 18907mips_elf_final_processing (void)
252b5132
RH
18908{
18909 /* Write out the register information. */
316f5878 18910 if (mips_abi != N64_ABI)
252b5132
RH
18911 {
18912 Elf32_RegInfo s;
18913
18914 s.ri_gprmask = mips_gprmask;
18915 s.ri_cprmask[0] = mips_cprmask[0];
18916 s.ri_cprmask[1] = mips_cprmask[1];
18917 s.ri_cprmask[2] = mips_cprmask[2];
18918 s.ri_cprmask[3] = mips_cprmask[3];
18919 /* The gp_value field is set by the MIPS ELF backend. */
18920
18921 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18922 ((Elf32_External_RegInfo *)
18923 mips_regmask_frag));
18924 }
18925 else
18926 {
18927 Elf64_Internal_RegInfo s;
18928
18929 s.ri_gprmask = mips_gprmask;
18930 s.ri_pad = 0;
18931 s.ri_cprmask[0] = mips_cprmask[0];
18932 s.ri_cprmask[1] = mips_cprmask[1];
18933 s.ri_cprmask[2] = mips_cprmask[2];
18934 s.ri_cprmask[3] = mips_cprmask[3];
18935 /* The gp_value field is set by the MIPS ELF backend. */
18936
18937 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18938 ((Elf64_External_RegInfo *)
18939 mips_regmask_frag));
18940 }
18941
18942 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18943 sort of BFD interface for this. */
18944 if (mips_any_noreorder)
18945 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18946 if (mips_pic != NO_PIC)
143d77c5 18947 {
8b828383 18948 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
18949 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18950 }
18951 if (mips_abicalls)
18952 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 18953
b015e599
AP
18954 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
18955 defined at present; this might need to change in future. */
a4672219
TS
18956 if (file_ase_mips16)
18957 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
18958 if (file_ase_micromips)
18959 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
846ef2d0 18960 if (file_ase & ASE_MDMX)
deec1734 18961 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 18962
bdaaa2e1 18963 /* Set the MIPS ELF ABI flags. */
316f5878 18964 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 18965 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 18966 else if (mips_abi == O64_ABI)
252b5132 18967 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 18968 else if (mips_abi == EABI_ABI)
252b5132 18969 {
316f5878 18970 if (!file_mips_gp32)
252b5132
RH
18971 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18972 else
18973 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18974 }
316f5878 18975 else if (mips_abi == N32_ABI)
be00bddd
TS
18976 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18977
c9914766 18978 /* Nothing to do for N64_ABI. */
252b5132
RH
18979
18980 if (mips_32bitmode)
18981 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08
TS
18982
18983#if 0 /* XXX FIXME */
18984 /* 32 bit code with 64 bit FP registers. */
18985 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18986 elf_elfheader (stdoutput)->e_flags |= ???;
18987#endif
252b5132
RH
18988}
18989
18990#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
18991\f
beae10d5 18992typedef struct proc {
9b2f1d35
EC
18993 symbolS *func_sym;
18994 symbolS *func_end_sym;
beae10d5
KH
18995 unsigned long reg_mask;
18996 unsigned long reg_offset;
18997 unsigned long fpreg_mask;
18998 unsigned long fpreg_offset;
18999 unsigned long frame_offset;
19000 unsigned long frame_reg;
19001 unsigned long pc_reg;
19002} procS;
252b5132
RH
19003
19004static procS cur_proc;
19005static procS *cur_proc_ptr;
19006static int numprocs;
19007
df58fc94
RS
19008/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19009 as "2", and a normal nop as "0". */
19010
19011#define NOP_OPCODE_MIPS 0
19012#define NOP_OPCODE_MIPS16 1
19013#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
19014
19015char
19016mips_nop_opcode (void)
19017{
df58fc94
RS
19018 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19019 return NOP_OPCODE_MICROMIPS;
19020 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19021 return NOP_OPCODE_MIPS16;
19022 else
19023 return NOP_OPCODE_MIPS;
742a56fe
RS
19024}
19025
df58fc94
RS
19026/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19027 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 19028
0a9ef439 19029void
17a2f251 19030mips_handle_align (fragS *fragp)
a19d8eb0 19031{
df58fc94 19032 char nop_opcode;
742a56fe 19033 char *p;
c67a084a
NC
19034 int bytes, size, excess;
19035 valueT opcode;
742a56fe 19036
0a9ef439
RH
19037 if (fragp->fr_type != rs_align_code)
19038 return;
19039
742a56fe 19040 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
19041 nop_opcode = *p;
19042 switch (nop_opcode)
a19d8eb0 19043 {
df58fc94
RS
19044 case NOP_OPCODE_MICROMIPS:
19045 opcode = micromips_nop32_insn.insn_opcode;
19046 size = 4;
19047 break;
19048 case NOP_OPCODE_MIPS16:
c67a084a
NC
19049 opcode = mips16_nop_insn.insn_opcode;
19050 size = 2;
df58fc94
RS
19051 break;
19052 case NOP_OPCODE_MIPS:
19053 default:
c67a084a
NC
19054 opcode = nop_insn.insn_opcode;
19055 size = 4;
df58fc94 19056 break;
c67a084a 19057 }
a19d8eb0 19058
c67a084a
NC
19059 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19060 excess = bytes % size;
df58fc94
RS
19061
19062 /* Handle the leading part if we're not inserting a whole number of
19063 instructions, and make it the end of the fixed part of the frag.
19064 Try to fit in a short microMIPS NOP if applicable and possible,
19065 and use zeroes otherwise. */
19066 gas_assert (excess < 4);
19067 fragp->fr_fix += excess;
19068 switch (excess)
c67a084a 19069 {
df58fc94
RS
19070 case 3:
19071 *p++ = '\0';
19072 /* Fall through. */
19073 case 2:
19074 if (nop_opcode == NOP_OPCODE_MICROMIPS)
19075 {
4d68580a 19076 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
19077 break;
19078 }
19079 *p++ = '\0';
19080 /* Fall through. */
19081 case 1:
19082 *p++ = '\0';
19083 /* Fall through. */
19084 case 0:
19085 break;
a19d8eb0 19086 }
c67a084a
NC
19087
19088 md_number_to_chars (p, opcode, size);
19089 fragp->fr_var = size;
a19d8eb0
CP
19090}
19091
252b5132 19092static void
17a2f251 19093md_obj_begin (void)
252b5132
RH
19094{
19095}
19096
19097static void
17a2f251 19098md_obj_end (void)
252b5132 19099{
54f4ddb3 19100 /* Check for premature end, nesting errors, etc. */
252b5132 19101 if (cur_proc_ptr)
9a41af64 19102 as_warn (_("missing .end at end of assembly"));
252b5132
RH
19103}
19104
19105static long
17a2f251 19106get_number (void)
252b5132
RH
19107{
19108 int negative = 0;
19109 long val = 0;
19110
19111 if (*input_line_pointer == '-')
19112 {
19113 ++input_line_pointer;
19114 negative = 1;
19115 }
3882b010 19116 if (!ISDIGIT (*input_line_pointer))
956cd1d6 19117 as_bad (_("expected simple number"));
252b5132
RH
19118 if (input_line_pointer[0] == '0')
19119 {
19120 if (input_line_pointer[1] == 'x')
19121 {
19122 input_line_pointer += 2;
3882b010 19123 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
19124 {
19125 val <<= 4;
19126 val |= hex_value (*input_line_pointer++);
19127 }
19128 return negative ? -val : val;
19129 }
19130 else
19131 {
19132 ++input_line_pointer;
3882b010 19133 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19134 {
19135 val <<= 3;
19136 val |= *input_line_pointer++ - '0';
19137 }
19138 return negative ? -val : val;
19139 }
19140 }
3882b010 19141 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19142 {
19143 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19144 *input_line_pointer, *input_line_pointer);
956cd1d6 19145 as_warn (_("invalid number"));
252b5132
RH
19146 return -1;
19147 }
3882b010 19148 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19149 {
19150 val *= 10;
19151 val += *input_line_pointer++ - '0';
19152 }
19153 return negative ? -val : val;
19154}
19155
19156/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19157 is an initial number which is the ECOFF file index. In the non-ECOFF
19158 case .file implies DWARF-2. */
19159
19160static void
17a2f251 19161s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19162{
ecb4347a
DJ
19163 static int first_file_directive = 0;
19164
c5dd6aab
DJ
19165 if (ECOFF_DEBUGGING)
19166 {
19167 get_number ();
19168 s_app_file (0);
19169 }
19170 else
ecb4347a
DJ
19171 {
19172 char *filename;
19173
19174 filename = dwarf2_directive_file (0);
19175
19176 /* Versions of GCC up to 3.1 start files with a ".file"
19177 directive even for stabs output. Make sure that this
19178 ".file" is handled. Note that you need a version of GCC
19179 after 3.1 in order to support DWARF-2 on MIPS. */
19180 if (filename != NULL && ! first_file_directive)
19181 {
19182 (void) new_logical_line (filename, -1);
c04f5787 19183 s_app_file_string (filename, 0);
ecb4347a
DJ
19184 }
19185 first_file_directive = 1;
19186 }
c5dd6aab
DJ
19187}
19188
19189/* The .loc directive, implying DWARF-2. */
252b5132
RH
19190
19191static void
17a2f251 19192s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19193{
c5dd6aab
DJ
19194 if (!ECOFF_DEBUGGING)
19195 dwarf2_directive_loc (0);
252b5132
RH
19196}
19197
252b5132
RH
19198/* The .end directive. */
19199
19200static void
17a2f251 19201s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19202{
19203 symbolS *p;
252b5132 19204
7a621144
DJ
19205 /* Following functions need their own .frame and .cprestore directives. */
19206 mips_frame_reg_valid = 0;
19207 mips_cprestore_valid = 0;
19208
252b5132
RH
19209 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19210 {
19211 p = get_symbol ();
19212 demand_empty_rest_of_line ();
19213 }
19214 else
19215 p = NULL;
19216
14949570 19217 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19218 as_warn (_(".end not in text section"));
19219
19220 if (!cur_proc_ptr)
19221 {
19222 as_warn (_(".end directive without a preceding .ent directive."));
19223 demand_empty_rest_of_line ();
19224 return;
19225 }
19226
19227 if (p != NULL)
19228 {
9c2799c2 19229 gas_assert (S_GET_NAME (p));
9b2f1d35 19230 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 19231 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
19232
19233 if (debug_type == DEBUG_STABS)
19234 stabs_generate_asm_endfunc (S_GET_NAME (p),
19235 S_GET_NAME (p));
252b5132
RH
19236 }
19237 else
19238 as_warn (_(".end directive missing or unknown symbol"));
19239
2132e3a3 19240#ifdef OBJ_ELF
9b2f1d35
EC
19241 /* Create an expression to calculate the size of the function. */
19242 if (p && cur_proc_ptr)
19243 {
19244 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19245 expressionS *exp = xmalloc (sizeof (expressionS));
19246
19247 obj->size = exp;
19248 exp->X_op = O_subtract;
19249 exp->X_add_symbol = symbol_temp_new_now ();
19250 exp->X_op_symbol = p;
19251 exp->X_add_number = 0;
19252
19253 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19254 }
19255
ecb4347a 19256 /* Generate a .pdr section. */
f43abd2b 19257 if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19258 {
19259 segT saved_seg = now_seg;
19260 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19261 expressionS exp;
19262 char *fragp;
252b5132 19263
252b5132 19264#ifdef md_flush_pending_output
ecb4347a 19265 md_flush_pending_output ();
252b5132
RH
19266#endif
19267
9c2799c2 19268 gas_assert (pdr_seg);
ecb4347a 19269 subseg_set (pdr_seg, 0);
252b5132 19270
ecb4347a
DJ
19271 /* Write the symbol. */
19272 exp.X_op = O_symbol;
19273 exp.X_add_symbol = p;
19274 exp.X_add_number = 0;
19275 emit_expr (&exp, 4);
252b5132 19276
ecb4347a 19277 fragp = frag_more (7 * 4);
252b5132 19278
17a2f251
TS
19279 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19280 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19281 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19282 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19283 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19284 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19285 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19286
ecb4347a
DJ
19287 subseg_set (saved_seg, saved_subseg);
19288 }
19289#endif /* OBJ_ELF */
252b5132
RH
19290
19291 cur_proc_ptr = NULL;
19292}
19293
19294/* The .aent and .ent directives. */
19295
19296static void
17a2f251 19297s_mips_ent (int aent)
252b5132 19298{
252b5132 19299 symbolS *symbolP;
252b5132
RH
19300
19301 symbolP = get_symbol ();
19302 if (*input_line_pointer == ',')
f9419b05 19303 ++input_line_pointer;
252b5132 19304 SKIP_WHITESPACE ();
3882b010 19305 if (ISDIGIT (*input_line_pointer)
d9a62219 19306 || *input_line_pointer == '-')
874e8986 19307 get_number ();
252b5132 19308
14949570 19309 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19310 as_warn (_(".ent or .aent not in text section."));
19311
19312 if (!aent && cur_proc_ptr)
9a41af64 19313 as_warn (_("missing .end"));
252b5132
RH
19314
19315 if (!aent)
19316 {
7a621144
DJ
19317 /* This function needs its own .frame and .cprestore directives. */
19318 mips_frame_reg_valid = 0;
19319 mips_cprestore_valid = 0;
19320
252b5132
RH
19321 cur_proc_ptr = &cur_proc;
19322 memset (cur_proc_ptr, '\0', sizeof (procS));
19323
9b2f1d35 19324 cur_proc_ptr->func_sym = symbolP;
252b5132 19325
f9419b05 19326 ++numprocs;
ecb4347a
DJ
19327
19328 if (debug_type == DEBUG_STABS)
19329 stabs_generate_asm_func (S_GET_NAME (symbolP),
19330 S_GET_NAME (symbolP));
252b5132
RH
19331 }
19332
7c0fc524
MR
19333 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19334
252b5132
RH
19335 demand_empty_rest_of_line ();
19336}
19337
19338/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19339 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19340 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19341 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19342 symbol table (in the mdebug section). */
19343
19344static void
17a2f251 19345s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19346{
ecb4347a 19347#ifdef OBJ_ELF
f43abd2b 19348 if (IS_ELF && !ECOFF_DEBUGGING)
ecb4347a
DJ
19349 {
19350 long val;
252b5132 19351
ecb4347a
DJ
19352 if (cur_proc_ptr == (procS *) NULL)
19353 {
19354 as_warn (_(".frame outside of .ent"));
19355 demand_empty_rest_of_line ();
19356 return;
19357 }
252b5132 19358
ecb4347a
DJ
19359 cur_proc_ptr->frame_reg = tc_get_register (1);
19360
19361 SKIP_WHITESPACE ();
19362 if (*input_line_pointer++ != ','
19363 || get_absolute_expression_and_terminator (&val) != ',')
19364 {
19365 as_warn (_("Bad .frame directive"));
19366 --input_line_pointer;
19367 demand_empty_rest_of_line ();
19368 return;
19369 }
252b5132 19370
ecb4347a
DJ
19371 cur_proc_ptr->frame_offset = val;
19372 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19373
252b5132 19374 demand_empty_rest_of_line ();
252b5132 19375 }
ecb4347a
DJ
19376 else
19377#endif /* OBJ_ELF */
19378 s_ignore (ignore);
252b5132
RH
19379}
19380
bdaaa2e1
KH
19381/* The .fmask and .mask directives. If the mdebug section is present
19382 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19383 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19384 information correctly. We can't use the ecoff routines because they
252b5132
RH
19385 make reference to the ecoff symbol table (in the mdebug section). */
19386
19387static void
17a2f251 19388s_mips_mask (int reg_type)
252b5132 19389{
ecb4347a 19390#ifdef OBJ_ELF
f43abd2b 19391 if (IS_ELF && !ECOFF_DEBUGGING)
252b5132 19392 {
ecb4347a 19393 long mask, off;
252b5132 19394
ecb4347a
DJ
19395 if (cur_proc_ptr == (procS *) NULL)
19396 {
19397 as_warn (_(".mask/.fmask outside of .ent"));
19398 demand_empty_rest_of_line ();
19399 return;
19400 }
252b5132 19401
ecb4347a
DJ
19402 if (get_absolute_expression_and_terminator (&mask) != ',')
19403 {
19404 as_warn (_("Bad .mask/.fmask directive"));
19405 --input_line_pointer;
19406 demand_empty_rest_of_line ();
19407 return;
19408 }
252b5132 19409
ecb4347a
DJ
19410 off = get_absolute_expression ();
19411
19412 if (reg_type == 'F')
19413 {
19414 cur_proc_ptr->fpreg_mask = mask;
19415 cur_proc_ptr->fpreg_offset = off;
19416 }
19417 else
19418 {
19419 cur_proc_ptr->reg_mask = mask;
19420 cur_proc_ptr->reg_offset = off;
19421 }
19422
19423 demand_empty_rest_of_line ();
252b5132
RH
19424 }
19425 else
ecb4347a
DJ
19426#endif /* OBJ_ELF */
19427 s_ignore (reg_type);
252b5132
RH
19428}
19429
316f5878
RS
19430/* A table describing all the processors gas knows about. Names are
19431 matched in the order listed.
e7af610e 19432
316f5878
RS
19433 To ease comparison, please keep this table in the same order as
19434 gcc's mips_cpu_info_table[]. */
e972090a
NC
19435static const struct mips_cpu_info mips_cpu_info_table[] =
19436{
316f5878 19437 /* Entries for generic ISAs */
d16afab6
RS
19438 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19439 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19440 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19441 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19442 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19443 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19444 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19445 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19446 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
19447
19448 /* MIPS I */
d16afab6
RS
19449 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19450 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19451 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19452
19453 /* MIPS II */
d16afab6 19454 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19455
19456 /* MIPS III */
d16afab6
RS
19457 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19458 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19459 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19460 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19461 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19462 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19463 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19464 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19465 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19466 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19467 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19468 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19469 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 19470 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
19471 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19472 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19473
19474 /* MIPS IV */
d16afab6
RS
19475 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19476 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19477 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19478 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19479 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19480 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19481 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19482 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19483 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19484 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19485 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19486 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19487 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19488 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19489 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19490
19491 /* MIPS 32 */
d16afab6
RS
19492 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19493 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19494 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19495 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
19496
19497 /* MIPS 32 Release 2 */
d16afab6
RS
19498 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19499 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19500 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19501 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19502 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19503 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19504 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19505 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19506 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19507 ISA_MIPS32R2, CPU_MIPS32R2 },
19508 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19509 ISA_MIPS32R2, CPU_MIPS32R2 },
19510 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19511 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19512 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19513 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19514 /* Deprecated forms of the above. */
d16afab6
RS
19515 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19516 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19517 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
19518 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19519 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19520 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19521 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19522 /* Deprecated forms of the above. */
d16afab6
RS
19523 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19524 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19525 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
19526 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19527 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19528 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19529 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19530 /* Deprecated forms of the above. */
d16afab6
RS
19531 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19532 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 19533 /* 34Kn is a 34kc without DSP. */
d16afab6 19534 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19535 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
19536 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19537 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19538 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19539 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19540 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19541 /* Deprecated forms of the above. */
d16afab6
RS
19542 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19543 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 19544 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
19545 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19546 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19547 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19548 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 19549
316f5878 19550 /* MIPS 64 */
d16afab6
RS
19551 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19552 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19553 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19554 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19555
c7a23324 19556 /* Broadcom SB-1 CPU core */
d16afab6 19557 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 19558 /* Broadcom SB-1A CPU core */
d16afab6 19559 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
d051516a 19560
d16afab6 19561 { "loongson3a", 0, 0, ISA_MIPS64, CPU_LOONGSON_3A },
e7af610e 19562
ed163775
MR
19563 /* MIPS 64 Release 2 */
19564
967344c6 19565 /* Cavium Networks Octeon CPU core */
d16afab6
RS
19566 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19567 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19568 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
967344c6 19569
52b6b6b9 19570 /* RMI Xlr */
d16afab6 19571 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 19572
55a36193
MK
19573 /* Broadcom XLP.
19574 XLP is mostly like XLR, with the prominent exception that it is
19575 MIPS64R2 rather than MIPS64. */
d16afab6 19576 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 19577
316f5878 19578 /* End marker */
d16afab6 19579 { NULL, 0, 0, 0, 0 }
316f5878 19580};
e7af610e 19581
84ea6cf2 19582
316f5878
RS
19583/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19584 with a final "000" replaced by "k". Ignore case.
e7af610e 19585
316f5878 19586 Note: this function is shared between GCC and GAS. */
c6c98b38 19587
b34976b6 19588static bfd_boolean
17a2f251 19589mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19590{
19591 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19592 given++, canonical++;
19593
19594 return ((*given == 0 && *canonical == 0)
19595 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19596}
19597
19598
19599/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19600 CPU name. We've traditionally allowed a lot of variation here.
19601
19602 Note: this function is shared between GCC and GAS. */
19603
b34976b6 19604static bfd_boolean
17a2f251 19605mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19606{
19607 /* First see if the name matches exactly, or with a final "000"
19608 turned into "k". */
19609 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19610 return TRUE;
316f5878
RS
19611
19612 /* If not, try comparing based on numerical designation alone.
19613 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19614 if (TOLOWER (*given) == 'r')
19615 given++;
19616 if (!ISDIGIT (*given))
b34976b6 19617 return FALSE;
316f5878
RS
19618
19619 /* Skip over some well-known prefixes in the canonical name,
19620 hoping to find a number there too. */
19621 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19622 canonical += 2;
19623 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19624 canonical += 2;
19625 else if (TOLOWER (canonical[0]) == 'r')
19626 canonical += 1;
19627
19628 return mips_strict_matching_cpu_name_p (canonical, given);
19629}
19630
19631
19632/* Parse an option that takes the name of a processor as its argument.
19633 OPTION is the name of the option and CPU_STRING is the argument.
19634 Return the corresponding processor enumeration if the CPU_STRING is
19635 recognized, otherwise report an error and return null.
19636
19637 A similar function exists in GCC. */
e7af610e
NC
19638
19639static const struct mips_cpu_info *
17a2f251 19640mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19641{
316f5878 19642 const struct mips_cpu_info *p;
e7af610e 19643
316f5878
RS
19644 /* 'from-abi' selects the most compatible architecture for the given
19645 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19646 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19647 version. Look first at the -mgp options, if given, otherwise base
19648 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19649
316f5878
RS
19650 Treat NO_ABI like the EABIs. One reason to do this is that the
19651 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19652 architecture. This code picks MIPS I for 'mips' and MIPS III for
19653 'mips64', just as we did in the days before 'from-abi'. */
19654 if (strcasecmp (cpu_string, "from-abi") == 0)
19655 {
19656 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19657 return mips_cpu_info_from_isa (ISA_MIPS1);
19658
19659 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19660 return mips_cpu_info_from_isa (ISA_MIPS3);
19661
19662 if (file_mips_gp32 >= 0)
19663 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19664
19665 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19666 ? ISA_MIPS3
19667 : ISA_MIPS1);
19668 }
19669
19670 /* 'default' has traditionally been a no-op. Probably not very useful. */
19671 if (strcasecmp (cpu_string, "default") == 0)
19672 return 0;
19673
19674 for (p = mips_cpu_info_table; p->name != 0; p++)
19675 if (mips_matching_cpu_name_p (p->name, cpu_string))
19676 return p;
19677
20203fb9 19678 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
316f5878 19679 return 0;
e7af610e
NC
19680}
19681
316f5878
RS
19682/* Return the canonical processor information for ISA (a member of the
19683 ISA_MIPS* enumeration). */
19684
e7af610e 19685static const struct mips_cpu_info *
17a2f251 19686mips_cpu_info_from_isa (int isa)
e7af610e
NC
19687{
19688 int i;
19689
19690 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19691 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19692 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19693 return (&mips_cpu_info_table[i]);
19694
e972090a 19695 return NULL;
e7af610e 19696}
fef14a42
TS
19697
19698static const struct mips_cpu_info *
17a2f251 19699mips_cpu_info_from_arch (int arch)
fef14a42
TS
19700{
19701 int i;
19702
19703 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19704 if (arch == mips_cpu_info_table[i].cpu)
19705 return (&mips_cpu_info_table[i]);
19706
19707 return NULL;
19708}
316f5878
RS
19709\f
19710static void
17a2f251 19711show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19712{
19713 if (*first_p)
19714 {
19715 fprintf (stream, "%24s", "");
19716 *col_p = 24;
19717 }
19718 else
19719 {
19720 fprintf (stream, ", ");
19721 *col_p += 2;
19722 }
e7af610e 19723
316f5878
RS
19724 if (*col_p + strlen (string) > 72)
19725 {
19726 fprintf (stream, "\n%24s", "");
19727 *col_p = 24;
19728 }
19729
19730 fprintf (stream, "%s", string);
19731 *col_p += strlen (string);
19732
19733 *first_p = 0;
19734}
19735
19736void
17a2f251 19737md_show_usage (FILE *stream)
e7af610e 19738{
316f5878
RS
19739 int column, first;
19740 size_t i;
19741
19742 fprintf (stream, _("\
19743MIPS options:\n\
316f5878
RS
19744-EB generate big endian output\n\
19745-EL generate little endian output\n\
19746-g, -g2 do not remove unneeded NOPs or swap branches\n\
19747-G NUM allow referencing objects up to NUM bytes\n\
19748 implicitly with the gp register [default 8]\n"));
19749 fprintf (stream, _("\
19750-mips1 generate MIPS ISA I instructions\n\
19751-mips2 generate MIPS ISA II instructions\n\
19752-mips3 generate MIPS ISA III instructions\n\
19753-mips4 generate MIPS ISA IV instructions\n\
19754-mips5 generate MIPS ISA V instructions\n\
19755-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 19756-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 19757-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 19758-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
19759-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19760
19761 first = 1;
e7af610e
NC
19762
19763 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
19764 show (stream, mips_cpu_info_table[i].name, &column, &first);
19765 show (stream, "from-abi", &column, &first);
19766 fputc ('\n', stream);
e7af610e 19767
316f5878
RS
19768 fprintf (stream, _("\
19769-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19770-no-mCPU don't generate code specific to CPU.\n\
19771 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19772
19773 first = 1;
19774
19775 show (stream, "3900", &column, &first);
19776 show (stream, "4010", &column, &first);
19777 show (stream, "4100", &column, &first);
19778 show (stream, "4650", &column, &first);
19779 fputc ('\n', stream);
19780
19781 fprintf (stream, _("\
19782-mips16 generate mips16 instructions\n\
19783-no-mips16 do not generate mips16 instructions\n"));
19784 fprintf (stream, _("\
df58fc94
RS
19785-mmicromips generate microMIPS instructions\n\
19786-mno-micromips do not generate microMIPS instructions\n"));
19787 fprintf (stream, _("\
e16bfa71
TS
19788-msmartmips generate smartmips instructions\n\
19789-mno-smartmips do not generate smartmips instructions\n"));
19790 fprintf (stream, _("\
74cd071d
CF
19791-mdsp generate DSP instructions\n\
19792-mno-dsp do not generate DSP instructions\n"));
19793 fprintf (stream, _("\
8b082fb1
TS
19794-mdspr2 generate DSP R2 instructions\n\
19795-mno-dspr2 do not generate DSP R2 instructions\n"));
19796 fprintf (stream, _("\
ef2e4d86
CF
19797-mmt generate MT instructions\n\
19798-mno-mt do not generate MT instructions\n"));
19799 fprintf (stream, _("\
dec0624d
MR
19800-mmcu generate MCU instructions\n\
19801-mno-mcu do not generate MCU instructions\n"));
19802 fprintf (stream, _("\
b015e599
AP
19803-mvirt generate Virtualization instructions\n\
19804-mno-virt do not generate Virtualization instructions\n"));
19805 fprintf (stream, _("\
c67a084a
NC
19806-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19807-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 19808-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 19809-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 19810-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 19811-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
19812-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19813-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 19814-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
19815-O0 remove unneeded NOPs, do not swap branches\n\
19816-O remove unneeded NOPs and swap branches\n\
316f5878
RS
19817--trap, --no-break trap exception on div by 0 and mult overflow\n\
19818--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
19819 fprintf (stream, _("\
19820-mhard-float allow floating-point instructions\n\
19821-msoft-float do not allow floating-point instructions\n\
19822-msingle-float only allow 32-bit floating-point operations\n\
19823-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb
MR
19824--[no-]construct-floats [dis]allow floating point values to be constructed\n\
19825--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n"
037b32b9 19826 ));
316f5878
RS
19827#ifdef OBJ_ELF
19828 fprintf (stream, _("\
19829-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 19830-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 19831-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 19832-non_shared do not generate code that can operate with DSOs\n\
316f5878 19833-xgot assume a 32 bit GOT\n\
dcd410fe 19834-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 19835-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 19836 position dependent (non shared) code\n\
316f5878
RS
19837-mabi=ABI create ABI conformant object file for:\n"));
19838
19839 first = 1;
19840
19841 show (stream, "32", &column, &first);
19842 show (stream, "o64", &column, &first);
19843 show (stream, "n32", &column, &first);
19844 show (stream, "64", &column, &first);
19845 show (stream, "eabi", &column, &first);
19846
19847 fputc ('\n', stream);
19848
19849 fprintf (stream, _("\
19850-32 create o32 ABI object file (default)\n\
19851-n32 create n32 ABI object file\n\
19852-64 create 64 ABI object file\n"));
19853#endif
e7af610e 19854}
14e777e0 19855
1575952e 19856#ifdef TE_IRIX
14e777e0 19857enum dwarf2_format
413a266c 19858mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 19859{
369943fe 19860 if (HAVE_64BIT_SYMBOLS)
1575952e 19861 return dwarf2_format_64bit_irix;
14e777e0
KB
19862 else
19863 return dwarf2_format_32bit;
19864}
1575952e 19865#endif
73369e65
EC
19866
19867int
19868mips_dwarf2_addr_size (void)
19869{
6b6b3450 19870 if (HAVE_64BIT_OBJECTS)
73369e65 19871 return 8;
73369e65
EC
19872 else
19873 return 4;
19874}
5862107c
EC
19875
19876/* Standard calling conventions leave the CFA at SP on entry. */
19877void
19878mips_cfi_frame_initial_instructions (void)
19879{
19880 cfi_add_CFA_def_cfa_register (SP);
19881}
19882
707bfff6
TS
19883int
19884tc_mips_regname_to_dw2regnum (char *regname)
19885{
19886 unsigned int regnum = -1;
19887 unsigned int reg;
19888
19889 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19890 regnum = reg;
19891
19892 return regnum;
19893}
This page took 2.42528 seconds and 4 git commands to generate.